@@ -128,15 +128,27 @@ void i2c_ev_err_enable(i2c_t *obj, uint32_t handler) {
128
128
struct i2c_s * obj_s = I2C_S (obj );
129
129
IRQn_Type irq_event_n = obj_s -> event_i2cIRQ ;
130
130
IRQn_Type irq_error_n = obj_s -> error_i2cIRQ ;
131
+ /* default prio in master case is set to 2 */
132
+ uint32_t prio = 2 ;
131
133
132
- /* Set up event IT using IRQ and handler tables */
134
+ /* Set up ITs using IRQ and handler tables */
133
135
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 */
137
136
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 );
140
152
}
141
153
142
154
void i2c_ev_err_disable (i2c_t * obj ) {
@@ -751,12 +763,24 @@ void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *hi2c){
751
763
/* Get object ptr based on handler ptr */
752
764
i2c_t * obj = get_i2c_obj (hi2c );
753
765
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
754
773
755
774
DEBUG_PRINTF ("HAL_I2C_ErrorCallback:%d, index=%d\r\n" , (int ) hi2c -> ErrorCode , obj_s -> index );
756
775
757
776
/* re-init IP to try and get back in a working state */
758
777
i2c_init (obj , obj_s -> sda , obj_s -> scl );
759
778
779
+ #if DEVICE_I2CSLAVE
780
+ /* restore slave address */
781
+ i2c_slave_address (obj , 0 , address , 0 );
782
+ #endif
783
+
760
784
/* Keep Set event flag */
761
785
obj_s -> event = I2C_EVENT_ERROR ;
762
786
}
0 commit comments