Skip to content

Commit d4df713

Browse files
authored
Merge pull request #15350 from mbed-ce/upstreamed/rethink-stm32-i2c-hal
Rethink STM32 I2C v2 HAL
2 parents 7d16811 + d59c34b commit d4df713

File tree

16 files changed

+477
-517
lines changed

16 files changed

+477
-517
lines changed

hal/include/hal/i2c_api.h

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,36 @@
3535
*
3636
* @{
3737
*/
38+
39+
/**
40+
* Indicates that an unspecified error has occurred in the transfer. This usually means
41+
* either an internal error in the Mbed MCU's I2C module, or something like an arbitration loss.
42+
* Does not indicate a NACK.
43+
*/
3844
#define I2C_EVENT_ERROR (1 << 1)
45+
46+
/**
47+
* Indicates that the slave did not respond to the address byte of the transfer.
48+
*/
3949
#define I2C_EVENT_ERROR_NO_SLAVE (1 << 2)
50+
51+
/**
52+
* Indicates that the transfer completed successfully.
53+
*/
4054
#define I2C_EVENT_TRANSFER_COMPLETE (1 << 3)
55+
56+
/**
57+
* Indicates that a NACK was received after the address byte, but before the requested number of bytes
58+
* could be transferred.
59+
*
60+
* Note: Not every manufacturer HAL is able to make a distinction between this flag and #I2C_EVENT_ERROR_NO_SLAVE.
61+
* On a NACK, you might conceivably get one or both of these flags.
62+
*/
4163
#define I2C_EVENT_TRANSFER_EARLY_NACK (1 << 4)
64+
65+
/**
66+
* Use this macro to request all possible I2C events.
67+
*/
4268
#define I2C_EVENT_ALL (I2C_EVENT_ERROR | I2C_EVENT_TRANSFER_COMPLETE | I2C_EVENT_ERROR_NO_SLAVE | I2C_EVENT_TRANSFER_EARLY_NACK)
4369

4470
/**@}*/
@@ -61,7 +87,8 @@ typedef struct i2c_s i2c_t;
6187

6288
enum {
6389
I2C_ERROR_NO_SLAVE = -1,
64-
I2C_ERROR_BUS_BUSY = -2
90+
I2C_ERROR_BUS_BUSY = -2,
91+
I2C_ERROR_INVALID_USAGE = -3 ///< Invalid usage of the I2C API, e.g. by mixing single-byte and transactional function calls.
6592
};
6693

6794
typedef struct {
@@ -229,7 +256,7 @@ int i2c_byte_read(i2c_t *obj, int last);
229256
*
230257
* @param obj The I2C object
231258
* @param data Byte to be written
232-
* @return 0 if NAK was received, 1 if ACK was received, 2 for timeout.
259+
* @return 0 if NAK was received, 1 if ACK was received, 2 for timeout, or 3 for other error.
233260
*/
234261
int i2c_byte_write(i2c_t *obj, int data);
235262

targets/TARGET_STM/TARGET_STM32F0/objects.h

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -77,39 +77,6 @@ struct serial_s {
7777
#endif
7878
};
7979

80-
struct i2c_s {
81-
/* The 1st 2 members I2CName i2c
82-
* and I2C_HandleTypeDef handle should
83-
* be kept as the first members of this struct
84-
* to ensure i2c_get_obj to work as expected
85-
*/
86-
I2CName i2c;
87-
I2C_HandleTypeDef handle;
88-
uint8_t index;
89-
int hz;
90-
PinName sda;
91-
PinName scl;
92-
IRQn_Type event_i2cIRQ;
93-
IRQn_Type error_i2cIRQ;
94-
uint32_t XferOperation;
95-
volatile uint8_t event;
96-
volatile int pending_start;
97-
int current_hz;
98-
#if DEVICE_I2CSLAVE
99-
uint8_t slave;
100-
volatile uint8_t pending_slave_tx_master_rx;
101-
volatile uint8_t pending_slave_rx_maxter_tx;
102-
uint8_t *slave_rx_buffer;
103-
volatile uint16_t slave_rx_buffer_size;
104-
volatile uint16_t slave_rx_count;
105-
#endif
106-
#if DEVICE_I2C_ASYNCH
107-
uint32_t address;
108-
uint8_t stop;
109-
uint8_t available_events;
110-
#endif
111-
};
112-
11380
struct analogin_s {
11481
ADC_HandleTypeDef handle;
11582
PinName pin;

targets/TARGET_STM/TARGET_STM32F3/objects.h

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -90,41 +90,6 @@ struct serial_s {
9090
#endif
9191
};
9292

93-
struct i2c_s {
94-
/* The 1st 2 members I2CName i2c
95-
* and I2C_HandleTypeDef handle should
96-
* be kept as the first members of this struct
97-
* to ensure i2c_get_obj to work as expected
98-
*/
99-
I2CName i2c;
100-
I2C_HandleTypeDef handle;
101-
uint8_t index;
102-
int hz;
103-
PinName sda;
104-
PinName scl;
105-
int sda_func;
106-
int scl_func;
107-
IRQn_Type event_i2cIRQ;
108-
IRQn_Type error_i2cIRQ;
109-
uint32_t XferOperation;
110-
volatile uint8_t event;
111-
volatile int pending_start;
112-
int current_hz;
113-
#if DEVICE_I2CSLAVE
114-
uint8_t slave;
115-
volatile uint8_t pending_slave_tx_master_rx;
116-
volatile uint8_t pending_slave_rx_maxter_tx;
117-
uint8_t *slave_rx_buffer;
118-
volatile uint16_t slave_rx_buffer_size;
119-
volatile uint16_t slave_rx_count;
120-
#endif
121-
#if DEVICE_I2C_ASYNCH
122-
uint32_t address;
123-
uint8_t stop;
124-
uint8_t available_events;
125-
#endif
126-
};
127-
12893
struct dac_s {
12994
DACName dac;
13095
PinName pin;

targets/TARGET_STM/TARGET_STM32F7/objects.h

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -108,39 +108,6 @@ struct serial_s {
108108
#endif
109109
};
110110

111-
struct i2c_s {
112-
/* The 1st 2 members I2CName i2c
113-
* and I2C_HandleTypeDef handle should
114-
* be kept as the first members of this struct
115-
* to ensure i2c_get_obj to work as expected
116-
*/
117-
I2CName i2c;
118-
I2C_HandleTypeDef handle;
119-
uint8_t index;
120-
int hz;
121-
PinName sda;
122-
PinName scl;
123-
IRQn_Type event_i2cIRQ;
124-
IRQn_Type error_i2cIRQ;
125-
uint32_t XferOperation;
126-
volatile uint8_t event;
127-
volatile int pending_start;
128-
int current_hz;
129-
#if DEVICE_I2CSLAVE
130-
uint8_t slave;
131-
volatile uint8_t pending_slave_tx_master_rx;
132-
volatile uint8_t pending_slave_rx_maxter_tx;
133-
uint8_t *slave_rx_buffer;
134-
volatile uint16_t slave_rx_buffer_size;
135-
volatile uint16_t slave_rx_count;
136-
#endif
137-
#if DEVICE_I2C_ASYNCH
138-
uint32_t address;
139-
uint8_t stop;
140-
uint8_t available_events;
141-
#endif
142-
};
143-
144111
struct analogin_s {
145112
ADC_HandleTypeDef handle;
146113
PinName pin;

targets/TARGET_STM/TARGET_STM32G0/objects.h

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -89,41 +89,6 @@ struct serial_s {
8989
#endif
9090
};
9191

92-
struct i2c_s {
93-
/* The 1st 2 members I2CName i2c
94-
* and I2C_HandleTypeDef handle should
95-
* be kept as the first members of this struct
96-
* to ensure i2c_get_obj to work as expected
97-
*/
98-
I2CName i2c;
99-
I2C_HandleTypeDef handle;
100-
uint8_t index;
101-
int hz;
102-
PinName sda;
103-
PinName scl;
104-
int sda_func;
105-
int scl_func;
106-
IRQn_Type event_i2cIRQ;
107-
IRQn_Type error_i2cIRQ;
108-
uint32_t XferOperation;
109-
volatile uint8_t event;
110-
volatile int pending_start;
111-
int current_hz;
112-
#if DEVICE_I2CSLAVE
113-
uint8_t slave;
114-
volatile uint8_t pending_slave_tx_master_rx;
115-
volatile uint8_t pending_slave_rx_maxter_tx;
116-
uint8_t *slave_rx_buffer;
117-
volatile uint16_t slave_rx_buffer_size;
118-
volatile uint16_t slave_rx_count;
119-
#endif
120-
#if DEVICE_I2C_ASYNCH
121-
uint32_t address;
122-
uint8_t stop;
123-
uint8_t available_events;
124-
#endif
125-
};
126-
12792
struct flash_s {
12893
/* nothing to be stored for now */
12994
uint32_t dummy;

targets/TARGET_STM/TARGET_STM32G4/objects.h

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -88,41 +88,6 @@ struct serial_s {
8888
#endif
8989
};
9090

91-
struct i2c_s {
92-
/* The 1st 2 members I2CName i2c
93-
* and I2C_HandleTypeDef handle should
94-
* be kept as the first members of this struct
95-
* to ensure i2c_get_obj to work as expected
96-
*/
97-
I2CName i2c;
98-
I2C_HandleTypeDef handle;
99-
uint8_t index;
100-
int hz;
101-
PinName sda;
102-
PinName scl;
103-
int sda_func;
104-
int scl_func;
105-
IRQn_Type event_i2cIRQ;
106-
IRQn_Type error_i2cIRQ;
107-
uint32_t XferOperation;
108-
volatile uint8_t event;
109-
volatile int pending_start;
110-
int current_hz;
111-
#if DEVICE_I2CSLAVE
112-
uint8_t slave;
113-
volatile uint8_t pending_slave_tx_master_rx;
114-
volatile uint8_t pending_slave_rx_maxter_tx;
115-
uint8_t *slave_rx_buffer;
116-
volatile uint16_t slave_rx_buffer_size;
117-
volatile uint16_t slave_rx_count;
118-
#endif
119-
#if DEVICE_I2C_ASYNCH
120-
uint32_t address;
121-
uint8_t stop;
122-
uint8_t available_events;
123-
#endif
124-
};
125-
12691
struct dac_s {
12792
DACName dac;
12893
PinName pin;

targets/TARGET_STM/TARGET_STM32H7/objects.h

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -97,39 +97,6 @@ struct serial_s {
9797
#endif
9898
};
9999

100-
struct i2c_s {
101-
/* The 1st 2 members I2CName i2c
102-
* and I2C_HandleTypeDef handle should
103-
* be kept as the first members of this struct
104-
* to ensure i2c_get_obj to work as expected
105-
*/
106-
I2CName i2c;
107-
I2C_HandleTypeDef handle;
108-
uint8_t index;
109-
int hz;
110-
PinName sda;
111-
PinName scl;
112-
IRQn_Type event_i2cIRQ;
113-
IRQn_Type error_i2cIRQ;
114-
uint32_t XferOperation;
115-
volatile uint8_t event;
116-
volatile int pending_start;
117-
int current_hz;
118-
#if DEVICE_I2CSLAVE
119-
uint8_t slave;
120-
volatile uint8_t pending_slave_tx_master_rx;
121-
volatile uint8_t pending_slave_rx_maxter_tx;
122-
uint8_t *slave_rx_buffer;
123-
volatile uint16_t slave_rx_buffer_size;
124-
volatile uint16_t slave_rx_count;
125-
#endif
126-
#if DEVICE_I2C_ASYNCH
127-
uint32_t address;
128-
uint8_t stop;
129-
uint8_t available_events;
130-
#endif
131-
};
132-
133100
struct analogin_s {
134101
ADC_HandleTypeDef handle;
135102
PinName pin;

targets/TARGET_STM/TARGET_STM32L0/objects.h

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -91,41 +91,6 @@ struct serial_s {
9191
#endif
9292
};
9393

94-
struct i2c_s {
95-
/* The 1st 2 members I2CName i2c
96-
* and I2C_HandleTypeDef handle should
97-
* be kept as the first members of this struct
98-
* to ensure i2c_get_obj to work as expected
99-
*/
100-
I2CName i2c;
101-
I2C_HandleTypeDef handle;
102-
uint8_t index;
103-
int hz;
104-
PinName sda;
105-
PinName scl;
106-
int sda_func;
107-
int scl_func;
108-
IRQn_Type event_i2cIRQ;
109-
IRQn_Type error_i2cIRQ;
110-
uint32_t XferOperation;
111-
volatile uint8_t event;
112-
volatile int pending_start;
113-
int current_hz;
114-
#if DEVICE_I2CSLAVE
115-
uint8_t slave;
116-
volatile uint8_t pending_slave_tx_master_rx;
117-
volatile uint8_t pending_slave_rx_maxter_tx;
118-
uint8_t *slave_rx_buffer;
119-
volatile uint16_t slave_rx_buffer_size;
120-
volatile uint16_t slave_rx_count;
121-
#endif
122-
#if DEVICE_I2C_ASYNCH
123-
uint32_t address;
124-
uint8_t stop;
125-
uint8_t available_events;
126-
#endif
127-
};
128-
12994
#if DEVICE_FLASH
13095
struct flash_s {
13196
/* nothing to be stored for now */

targets/TARGET_STM/TARGET_STM32L4/objects.h

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -87,41 +87,6 @@ struct serial_s {
8787
#endif
8888
};
8989

90-
struct i2c_s {
91-
/* The 1st 2 members I2CName i2c
92-
* and I2C_HandleTypeDef handle should
93-
* be kept as the first members of this struct
94-
* to ensure i2c_get_obj to work as expected
95-
*/
96-
I2CName i2c;
97-
I2C_HandleTypeDef handle;
98-
uint8_t index;
99-
int hz;
100-
PinName sda;
101-
PinName scl;
102-
int sda_func;
103-
int scl_func;
104-
IRQn_Type event_i2cIRQ;
105-
IRQn_Type error_i2cIRQ;
106-
uint32_t XferOperation;
107-
volatile uint8_t event;
108-
volatile int pending_start;
109-
int current_hz;
110-
#if DEVICE_I2CSLAVE
111-
uint8_t slave;
112-
volatile uint8_t pending_slave_tx_master_rx;
113-
volatile uint8_t pending_slave_rx_maxter_tx;
114-
uint8_t *slave_rx_buffer;
115-
volatile uint16_t slave_rx_buffer_size;
116-
volatile uint16_t slave_rx_count;
117-
#endif
118-
#if DEVICE_I2C_ASYNCH
119-
uint32_t address;
120-
uint8_t stop;
121-
uint8_t available_events;
122-
#endif
123-
};
124-
12590
struct flash_s {
12691
/* nothing to be stored for now */
12792
uint32_t dummy;

0 commit comments

Comments
 (0)