@@ -34,6 +34,28 @@ void i2c_irq_handler(I2CBus *bus) {
3434 portEND_SWITCHING_ISR (woken );
3535}
3636
37+ void i2c_dma_tx_irq_handler (I2CBus * bus ) {
38+ I2CBusHal * hal = bus -> hal ;
39+ I2C_HandleTypeDef * hdl = & hal -> state -> hdl ;
40+
41+ if (hdl -> State == HAL_I2C_STATE_BUSY_TX ) {
42+ HAL_DMA_IRQHandler (& hal -> state -> hdma_tx );
43+ } else if (hal -> state -> hdma_tx .State == HAL_DMA_STATE_BUSY ) {
44+ HAL_DMA_IRQHandler (& hal -> state -> hdma_tx );
45+ }
46+ }
47+
48+ void i2c_dma_rx_irq_handler (I2CBus * bus ) {
49+ I2CBusHal * hal = bus -> hal ;
50+ I2C_HandleTypeDef * hdl = & hal -> state -> hdl ;
51+
52+ if (hdl -> State == HAL_I2C_STATE_BUSY_RX ) {
53+ HAL_DMA_IRQHandler (& hal -> state -> hdma_rx );
54+ } else if (hal -> state -> hdma_rx .State == HAL_DMA_STATE_BUSY ) {
55+ HAL_DMA_IRQHandler (& hal -> state -> hdma_rx );
56+ }
57+ }
58+
3759void i2c_hal_init_transfer (I2CBus * bus ) {}
3860
3961void i2c_hal_abort_transfer (I2CBus * bus ) {
@@ -51,19 +73,35 @@ void i2c_hal_start_transfer(I2CBus *bus) {
5173
5274 if (transfer -> type == I2CTransferType_SendRegisterAddress ) {
5375 if (transfer -> direction == I2CTransferDirection_Read ) {
54- ret = HAL_I2C_Mem_Read_IT (hdl , transfer -> device_address , transfer -> register_address ,
55- I2C_MEMADD_SIZE_8BIT , transfer -> data , transfer -> size );
76+ if (hal -> state -> hdma_rx .Instance != NULL ) {
77+ ret = HAL_I2C_Mem_Read_DMA (hdl , transfer -> device_address , transfer -> register_address ,
78+ I2C_MEMADD_SIZE_8BIT , transfer -> data , transfer -> size );
79+ } else {
80+ ret = HAL_I2C_Mem_Read_IT (hdl , transfer -> device_address , transfer -> register_address ,
81+ I2C_MEMADD_SIZE_8BIT , transfer -> data , transfer -> size );
82+ }
5683 } else {
57- ret = HAL_I2C_Mem_Write_IT (hdl , transfer -> device_address , transfer -> register_address ,
58- I2C_MEMADD_SIZE_8BIT , transfer -> data , transfer -> size );
84+ if (hal -> state -> hdma_tx .Instance != NULL ) {
85+ ret = HAL_I2C_Mem_Write_DMA (hdl , transfer -> device_address , transfer -> register_address ,
86+ I2C_MEMADD_SIZE_8BIT , transfer -> data , transfer -> size );
87+ } else {
88+ ret = HAL_I2C_Mem_Write_IT (hdl , transfer -> device_address , transfer -> register_address ,
89+ I2C_MEMADD_SIZE_8BIT , transfer -> data , transfer -> size );
90+ }
5991 }
6092 } else {
6193 if (transfer -> direction == I2CTransferDirection_Read ) {
62- ret =
63- HAL_I2C_Master_Receive_IT (hdl , transfer -> device_address , transfer -> data , transfer -> size );
94+ if (hal -> state -> hdma_rx .Instance != NULL ) {
95+ ret = HAL_I2C_Master_Receive_DMA (hdl , transfer -> device_address , transfer -> data , transfer -> size );
96+ } else {
97+ ret = HAL_I2C_Master_Receive_IT (hdl , transfer -> device_address , transfer -> data , transfer -> size );
98+ }
6499 } else {
65- ret =
66- HAL_I2C_Master_Transmit_IT (hdl , transfer -> device_address , transfer -> data , transfer -> size );
100+ if (hal -> state -> hdma_tx .Instance != NULL ) {
101+ ret = HAL_I2C_Master_Transmit_DMA (hdl , transfer -> device_address , transfer -> data , transfer -> size );
102+ } else {
103+ ret = HAL_I2C_Master_Transmit_IT (hdl , transfer -> device_address , transfer -> data , transfer -> size );
104+ }
67105 }
68106 }
69107
@@ -106,9 +144,23 @@ void i2c_hal_init(I2CBus *bus) {
106144 HAL_PIN_Set (hal -> sda .pad , hal -> sda .func , hal -> sda .flags , 1 );
107145
108146 HAL_RCC_EnableModule (hal -> module );
147+
109148 ret = HAL_I2C_Init (hdl );
110149 PBL_ASSERTN (ret == HAL_OK );
111150
151+ if (hal -> state -> hdma_tx .Instance != NULL && hal -> state -> hdma_rx .Instance != NULL ) {
152+ __HAL_LINKDMA (hdl , hdmatx , hal -> state -> hdma_tx );
153+ __HAL_LINKDMA (hdl , hdmarx , hal -> state -> hdma_rx );
154+
155+ HAL_I2C_DMA_Init (hdl , & hal -> state -> dma_rx , & hal -> state -> dma_tx );
156+
157+ HAL_NVIC_SetPriority (hal -> dma_tx_irqn , hal -> dma_irq_priority , 0 );
158+ NVIC_EnableIRQ (hal -> dma_tx_irqn );
159+
160+ HAL_NVIC_SetPriority (hal -> dma_rx_irqn , hal -> dma_irq_priority , 0 );
161+ NVIC_EnableIRQ (hal -> dma_rx_irqn );
162+ }
163+
112164 HAL_NVIC_SetPriority (hal -> irqn , hal -> irq_priority , 0 );
113165 NVIC_EnableIRQ (hal -> irqn );
114166}
0 commit comments