/
i2c_soft.h
130 lines (119 loc) · 4.01 KB
/
i2c_soft.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/**
* @file drivers/i2c_soft.h
*
* @section License
* Copyright (C) 2014-2016, Erik Moqvist
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* This file is part of the Simba project.
*/
#ifndef __DRIVERS_I2C_SOFT_H__
#define __DRIVERS_I2C_SOFT_H__
#include "simba.h"
struct i2c_soft_driver_t {
struct pin_device_t *scl_p;
struct pin_device_t *sda_p;
long baudrate;
long baudrate_us;
long max_clock_stretching_us;
long clock_stretching_sleep_us;
};
/**
* Initialize the i2c soft module. This function must be called before
* calling any other function in this module.
*
* The module will only be initialized once even if this function is
* called multiple times.
*
* @return zero(0) or negative error code.
*/
int i2c_soft_module_init(void);
/**
* Initialize given driver object.
*
* @param[out] self_p Driver object to initialize.
* @param[in] scl_dev_p The I2C clock pin (SCL).
* @param[in] sda_dev_p The I2C data pin (SDA).
* @param[in] baudrate Bus baudrate.
* @param[in] max_clock_stretching_us Maximum number of microseconds
* to wait for the clock stretching
* to end.
* @param[in] clock_stretching_sleep_us SCL poll interval in number of
* microseconds waiting for clock
* stretching to end.
*
* @return zero(0) or negative error code.
*/
int i2c_soft_init(struct i2c_soft_driver_t *self_p,
struct pin_device_t *scl_dev_p,
struct pin_device_t *sda_dev_p,
long baudrate,
long max_clock_stretching_us,
long clock_stretching_sleep_us);
/**
* Start given driver object. Enables data reception and transmission,
* but does not start any transmission. Data tarnsfers are started by
* calling the `i2c_soft_read()` and `i2c_soft_write()`.
*
* @param[in] self_p Driver object to initialize.
*
* @return zero(0) or negative error code.
*/
int i2c_soft_start(struct i2c_soft_driver_t *self_p);
/**
* Stop given driver object. Disables data reception and transmission.
*
* @param[in] self_p Driver object to initialize.
*
* @return zero(0) or negative error code.
*/
int i2c_soft_stop(struct i2c_soft_driver_t *self_p);
/**
* Read given number of bytes into given buffer from given slave.
*
* @param[in] self_p Driver object.
* @param[in] address Slave address to read from.
* @param[out] buf_p Buffer to read into.
* @param[in] size Number of bytes to read.
*
* @return Number of bytes read or negative error code.
*/
ssize_t i2c_soft_read(struct i2c_soft_driver_t *self_p,
int address,
void *buf_p,
size_t size);
/**
* Write given number of bytes from given buffer to given slave.
*
* @param[in] self_p Driver object.
* @param[in] address Slave address to write to.
* @param[in] buf_p Buffer to write.
* @param[in] size Number of bytes to write.
*
* @return Number of bytes written or negative error code.
*/
ssize_t i2c_soft_write(struct i2c_soft_driver_t *self_p,
int address,
const void *buf_p,
size_t size);
/**
* Scan the i2c bus for a slave with given address.
*
* @param[in] self_p Driver object.
* @param[in] address Address of the slave to scan for.
*
* @return true(1) if a slave responded to given address, otherwise
* false(0) or negative error code.
*/
int i2c_soft_scan(struct i2c_soft_driver_t *self_p,
int address);
#endif