Skip to content

Commit 45fa92d

Browse files
authored
Merge pull request #3427 from LMESTM/fix_stm_i2c_slave
Fix stm i2c slave
2 parents cf75543 + 77e202f commit 45fa92d

File tree

1 file changed

+30
-6
lines changed

1 file changed

+30
-6
lines changed

targets/TARGET_STM/i2c_api.c

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,15 +128,27 @@ void i2c_ev_err_enable(i2c_t *obj, uint32_t handler) {
128128
struct i2c_s *obj_s = I2C_S(obj);
129129
IRQn_Type irq_event_n = obj_s->event_i2cIRQ;
130130
IRQn_Type irq_error_n = obj_s->error_i2cIRQ;
131+
/* default prio in master case is set to 2 */
132+
uint32_t prio = 2;
131133

132-
/* Set up event IT using IRQ and handler tables */
134+
/* Set up ITs using IRQ and handler tables */
133135
NVIC_SetVector(irq_event_n, handler);
134-
HAL_NVIC_SetPriority(irq_event_n, 0, 0);
135-
HAL_NVIC_EnableIRQ(irq_event_n);
136-
/* Set up error IT using IRQ and handler tables */
137136
NVIC_SetVector(irq_error_n, handler);
138-
HAL_NVIC_SetPriority(irq_error_n, 0, 1);
139-
HAL_NVIC_EnableIRQ(irq_error_n);
137+
138+
#if DEVICE_I2CSLAVE
139+
/* Set higher priority to slave device than master.
140+
* In case a device makes use of both master and slave, the
141+
* slave needs higher responsiveness.
142+
*/
143+
if (obj_s->slave) {
144+
prio = 1;
145+
}
146+
#endif
147+
148+
NVIC_SetPriority(irq_event_n, prio);
149+
NVIC_SetPriority(irq_error_n, prio);
150+
NVIC_EnableIRQ(irq_event_n);
151+
NVIC_EnableIRQ(irq_error_n);
140152
}
141153

142154
void i2c_ev_err_disable(i2c_t *obj) {
@@ -751,12 +763,24 @@ void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *hi2c){
751763
/* Get object ptr based on handler ptr */
752764
i2c_t *obj = get_i2c_obj(hi2c);
753765
struct i2c_s *obj_s = I2C_S(obj);
766+
#if DEVICE_I2CSLAVE
767+
I2C_HandleTypeDef *handle = &(obj_s->handle);
768+
uint32_t address = 0;
769+
/* Store address to handle it after reset */
770+
if(obj_s->slave)
771+
address = handle->Init.OwnAddress1;
772+
#endif
754773

755774
DEBUG_PRINTF("HAL_I2C_ErrorCallback:%d, index=%d\r\n", (int) hi2c->ErrorCode, obj_s->index);
756775

757776
/* re-init IP to try and get back in a working state */
758777
i2c_init(obj, obj_s->sda, obj_s->scl);
759778

779+
#if DEVICE_I2CSLAVE
780+
/* restore slave address */
781+
i2c_slave_address(obj, 0, address, 0);
782+
#endif
783+
760784
/* Keep Set event flag */
761785
obj_s->event = I2C_EVENT_ERROR;
762786
}

0 commit comments

Comments
 (0)