8000 stmhal/i2c: provide custom IRQ handlers · micropython/micropython@8fa0733 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8fa0733

Browse files
author
Krzysztof Blazewicz
committed
stmhal/i2c: provide custom IRQ handlers
Use custom handlers providing minimal required functionality because those provided by ST increase code size by almost 2 KiB.
1 parent 7604de3 commit 8fa0733

File tree

3 files changed

+97
-6
lines changed

3 files changed

+97
-6
lines changed

stmhal/i2c.c

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,95 @@ STATIC void i2c_reset_after_error(I2C_HandleTypeDef *i2c) {
317317
i2c_init(i2c);
318318
}
319319

320+
void i2c_ev_irq_handler(mp_uint_t i2c_id) {
321+
I2C_HandleTypeDef *hi2c;
322+
323+
switch (i2c_id) {
324+
#if defined(MICROPY_HW_I2C1_SCL)
325+
case 1:
326+
hi2c = &I2CHandle1;
327+
break;
328+
#endif
329+
#if defined(MICROPY_HW_I2C2_SCL)
330+
case 2:
331+
hi2c = &I2CHandle2;
332+
break;
333+
#endif
334+
#if defined(MICROPY_HW_I2C3_SCL)
335+
case 3:
336+
hi2c = &I2CHandle3;
337+
break;
338+
#endif
339+
default:
340+
return;
341+
}
342+
343+
if (hi2c->Instance->SR1 & I2C_FLAG_BTF && hi2c->State == HAL_I2C_STATE_BUSY_TX) {
344+
if (hi2c->XferCount != 0U) {
345+
hi2c->Instance->DR = *hi2c->pBuffPtr++;
346+
hi2c->XferCount--;
347+
} else {
348+
__HAL_I2C_DISABLE_IT(hi2c, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR);
349+
if (hi2c->XferOptions != I2C_FIRST_FRAME) {
350+
hi2c->Instance->CR1 |= I2C_CR1_STOP;
351+
}
352+
hi2c->Mode = HAL_I2C_MODE_NONE;
353+
hi2c->State = HAL_I2C_STATE_READY;
354+
}
355+
}
356+
}
357+
358+
void i2c_er_irq_handler(mp_uint_t i2c_id) {
359+
I2C_HandleTypeDef *hi2c;
360+
361+
switch (i2c_id) {
362+
#if defined(MICROPY_HW_I2C1_SCL)
363+
case 1:
364+
hi2c = &I2CHandle1;
365+
break;
366+
#endif
367+
#if defined(MICROPY_HW_I2C2_SCL)
368+
case 2:
369+
hi2c = &I2CHandle2;
370+
break;
371+
#endif
372+
#if defined(MICROPY_HW_I2C3_SCL)
373+
case 3:
374+
hi2c = &I2CHandle3;
375+
break;
376+
#endif
377+
default:
378+
return;
379+
}
380+
381+
uint32_t sr1 = hi2c->Instance->SR1;
382+
383+
// I2C Bus error
384+
if (sr1 & I2C_FLAG_BERR) {
385+
hi2c->ErrorCode |= HAL_I2C_ERROR_BERR;
386+
__HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_BERR);
387+
}
388+
389+
// I2C Arbitration Loss error
390+
if (sr1 & I2C_FLAG_ARLO) {
391+
hi2c->ErrorCode |= HAL_I2C_ERROR_ARLO;
392+
__HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_ARLO);
393+
}
394+
395+
// I2C Acknowledge failure
396+
if (sr1 & I2C_FLAG_AF) {
397+
hi2c->ErrorCode |= HAL_I2C_ERROR_AF;
398+
SET_BIT(hi2c->Instance->CR1,I2C_CR1_STOP);
399+
__HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);
400+
}
401+
402+
// I2C Over-Run/Under-Run
403+
if (sr1 & I2C_FLAG_OVR) {
404+
hi2c->ErrorCode |= HAL_I2C_ERROR_OVR;
405+
__HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_OVR);
406+
}
407+
}
408+
320409
STATIC HAL_StatusTypeDef i2c_wait_dma_finished(I2C_HandleTypeDef *i2c, uint32_t timeout) {
321410
// Note: we can't use WFI to idle in this loop because the DMA completion
322411
// interrupt may occur before the WFI. Hence we miss it and have to wait

stmhal/i2c.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,5 @@ extern const mp_obj_type_t pyb_i2c_type;
3434

3535
void i2c_init0(void);
3636
void i2c_init(I2C_HandleTypeDef *i2c);
37+
void i2c_ev_irq_handler(mp_uint_t i2c_id);
38+
void i2c_er_irq_handler(mp_uint_t i2c_id);

stmhal/stm32_it.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -706,41 +706,41 @@ void CAN2_RX1_IRQHandler(void) {
706706
#if defined(MICROPY_HW_I2C1_SCL)
707707
void I2C1_EV_IRQHandler(void) {
708708
IRQ_ENTER(I2C1_EV_IRQn);
709-
HAL_I2C_EV_IRQHandler(&I2CHandle1);
709+
i2c_ev_irq_handler(1);
710710
IRQ_EXIT(I2C1_EV_IRQn);
711711
}
712712

713713
void I2C1_ER_IRQHandler(void) {
714714
IRQ_ENTER(I2C1_ER_IRQn);
715-
HAL_I2C_ER_IRQHandler(&I2CHandle1);
715+
i2c_er_irq_handler(1);
716716
IRQ_EXIT(I2C1_ER_IRQn);
717717
}
718718
#endif // defined(MICROPY_HW_I2C1_SCL)
719719

720720
#if defined(MICROPY_HW_I2C2_SCL)
721721
void I2C2_EV_IRQHandler(void) {
722722
IRQ_ENTER(I2C2_EV_IRQn);
723-
HAL_I2C_EV_IRQHandler(&I2CHandle2);
723+
i2c_ev_irq_handler(2);
724724
IRQ_EXIT(I2C2_EV_IRQn);
725725
}
726726

727727
void I2C2_ER_IRQHandler(void) {
728728
IRQ_ENTER(I2C2_ER_IRQn);
729-
HAL_I2C_ER_IRQHandler(&I2CHandle2);
729+
i2c_er_irq_handler(2);
730730
IRQ_EXIT(I2C2_ER_IRQn);
731731
}
732732
#endif // defined(MICROPY_HW_I2C2_SCL)
733733

734734
#if defined(MICROPY_HW_I2C3_SCL)
735735
void I2C3_EV_IRQHandler(void) {
736736
IRQ_ENTER(I2C3_EV_IRQn);
737-
HAL_I2C_EV_IRQHandler(&I2CHandle3);
737+
i2c_ev_irq_handler(3);
738738
IRQ_EXIT(I2C3_EV_IRQn);
739739
}
740740

741741
void I2C3_ER_IRQHandler(void) {
742742
IRQ_ENTER(I2C3_ER_IRQn);
743-
HAL_I2C_ER_IRQHandler(&I2CHandle3);
743+
i2c_er_irq_handler(3);
744744
IRQ_EXIT(I2C3_ER_IRQn);
745745
}
746746
#endif // defined(MICROPY_HW_I2C3_SCL)

0 commit comments

Comments
 (0)
0