mbed library sources. Supersedes mbed-src.
Dependents: Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more
Diff: targets/TARGET_STM/i2c_api.c
- Revision:
- 187:0387e8f68319
- Parent:
- 180:96ed750bd169
- Child:
- 188:bcfe06ba3d64
--- a/targets/TARGET_STM/i2c_api.c Fri Jun 22 16:45:37 2018 +0100 +++ b/targets/TARGET_STM/i2c_api.c Thu Sep 06 13:40:20 2018 +0100 @@ -52,14 +52,14 @@ #endif #if DEVICE_I2C_ASYNCH - #define I2C_S(obj) (struct i2c_s *) (&((obj)->i2c)) +#define I2C_S(obj) (struct i2c_s *) (&((obj)->i2c)) #else - #define I2C_S(obj) (struct i2c_s *) (obj) +#define I2C_S(obj) (struct i2c_s *) (obj) #endif /* Family specific description for I2C */ #define I2C_NUM (5) -static I2C_HandleTypeDef* i2c_handles[I2C_NUM]; +static I2C_HandleTypeDef *i2c_handles[I2C_NUM]; /* Timeout values are based on core clock and I2C clock. The BYTE_TIMEOUT is computed as twice the number of cycles it would @@ -75,7 +75,7 @@ #define BYTE_TIMEOUT_US ((SystemCoreClock / obj_s->hz) * 3 * 10) /* Timeout values for flags and events waiting loops. These timeouts are not based on accurate values, they just guarantee that the application will - not remain stuck if the I2C communication is corrupted. + not remain stuck if the I2C communication is corrupted. */ #define FLAG_TIMEOUT ((int)0x1000) @@ -84,7 +84,7 @@ #if defined(I2C1_BASE) static void i2c1_irq(void) { - I2C_HandleTypeDef * handle = i2c_handles[0]; + I2C_HandleTypeDef *handle = i2c_handles[0]; HAL_I2C_EV_IRQHandler(handle); HAL_I2C_ER_IRQHandler(handle); } @@ -92,7 +92,7 @@ #if defined(I2C2_BASE) static void i2c2_irq(void) { - I2C_HandleTypeDef * handle = i2c_handles[1]; + I2C_HandleTypeDef *handle = i2c_handles[1]; HAL_I2C_EV_IRQHandler(handle); HAL_I2C_ER_IRQHandler(handle); } @@ -100,7 +100,7 @@ #if defined(I2C3_BASE) static void i2c3_irq(void) { - I2C_HandleTypeDef * handle = i2c_handles[2]; + I2C_HandleTypeDef *handle = i2c_handles[2]; HAL_I2C_EV_IRQHandler(handle); HAL_I2C_ER_IRQHandler(handle); } @@ -108,7 +108,7 @@ #if defined(I2C4_BASE) static void i2c4_irq(void) { - I2C_HandleTypeDef * handle = i2c_handles[3]; + I2C_HandleTypeDef *handle = i2c_handles[3]; HAL_I2C_EV_IRQHandler(handle); HAL_I2C_ER_IRQHandler(handle); } @@ -116,13 +116,14 @@ #if defined(FMPI2C1_BASE) static void i2c5_irq(void) { - I2C_HandleTypeDef * handle = i2c_handles[4]; + I2C_HandleTypeDef *handle = i2c_handles[4]; HAL_I2C_EV_IRQHandler(handle); HAL_I2C_ER_IRQHandler(handle); } #endif -void i2c_ev_err_enable(i2c_t *obj, uint32_t handler) { +void i2c_ev_err_enable(i2c_t *obj, uint32_t handler) +{ struct i2c_s *obj_s = I2C_S(obj); IRQn_Type irq_event_n = obj_s->event_i2cIRQ; IRQn_Type irq_error_n = obj_s->error_i2cIRQ; @@ -149,7 +150,8 @@ NVIC_EnableIRQ(irq_error_n); } -void i2c_ev_err_disable(i2c_t *obj) { +void i2c_ev_err_disable(i2c_t *obj) +{ struct i2c_s *obj_s = I2C_S(obj); IRQn_Type irq_event_n = obj_s->event_i2cIRQ; IRQn_Type irq_error_n = obj_s->error_i2cIRQ; @@ -196,7 +198,8 @@ return handler; } -void i2c_hw_reset(i2c_t *obj) { +void i2c_hw_reset(i2c_t *obj) +{ int timeout; struct i2c_s *obj_s = I2C_S(obj); I2C_HandleTypeDef *handle = &(obj_s->handle); @@ -251,11 +254,12 @@ * - Write PE=1. */ handle->Instance->CR1 &= ~I2C_CR1_PE; - while(handle->Instance->CR1 & I2C_CR1_PE); + while (handle->Instance->CR1 & I2C_CR1_PE); handle->Instance->CR1 |= I2C_CR1_PE; } -void i2c_init(i2c_t *obj, PinName sda, PinName scl) { +void i2c_init(i2c_t *obj, PinName sda, PinName scl) +{ struct i2c_s *obj_s = I2C_S(obj); @@ -323,12 +327,13 @@ // I2C configuration // Default hz value used for timeout computation - if(!obj_s->hz) - obj_s->hz = 100000; // 100 kHz per default + if (!obj_s->hz) { + obj_s->hz = 100000; // 100 kHz per default + } // Reset to clear pending flags if any i2c_hw_reset(obj); - i2c_frequency(obj, obj_s->hz ); + i2c_frequency(obj, obj_s->hz); #if DEVICE_I2CSLAVE // I2C master by default @@ -413,7 +418,7 @@ #ifdef I2C_ANALOGFILTER_ENABLE /* Enable the Analog I2C Filter */ - HAL_I2CEx_ConfigAnalogFilter(handle,I2C_ANALOGFILTER_ENABLE); + HAL_I2CEx_ConfigAnalogFilter(handle, I2C_ANALOGFILTER_ENABLE); #endif // I2C configuration @@ -429,20 +434,22 @@ obj_s->hz = hz; } -i2c_t *get_i2c_obj(I2C_HandleTypeDef *hi2c){ +i2c_t *get_i2c_obj(I2C_HandleTypeDef *hi2c) +{ /* Aim of the function is to get i2c_s pointer using hi2c pointer */ /* Highly inspired from magical linux kernel's "container_of" */ /* (which was not directly used since not compatible with IAR toolchain) */ struct i2c_s *obj_s; i2c_t *obj; - obj_s = (struct i2c_s *)( (char *)hi2c - offsetof(struct i2c_s,handle)); - obj = (i2c_t *)( (char *)obj_s - offsetof(i2c_t,i2c)); + obj_s = (struct i2c_s *)((char *)hi2c - offsetof(struct i2c_s, handle)); + obj = (i2c_t *)((char *)obj_s - offsetof(i2c_t, i2c)); return (obj); } -void i2c_reset(i2c_t *obj) { +void i2c_reset(i2c_t *obj) +{ struct i2c_s *obj_s = I2C_S(obj); /* As recommended in i2c_api.h, mainly send stop */ i2c_stop(obj); @@ -456,7 +463,8 @@ * There are 2 different IPs version that need to be supported */ #ifdef I2C_IP_VERSION_V1 -int i2c_start(i2c_t *obj) { +int i2c_start(i2c_t *obj) +{ int timeout; struct i2c_s *obj_s = I2C_S(obj); @@ -488,7 +496,8 @@ return 0; } -int i2c_stop(i2c_t *obj) { +int i2c_stop(i2c_t *obj) +{ struct i2c_s *obj_s = I2C_S(obj); I2C_TypeDef *i2c = (I2C_TypeDef *)obj_s->i2c; @@ -498,13 +507,15 @@ /* In case of mixed usage of the APIs (unitary + SYNC) * re-init HAL state */ - if(obj_s->XferOperation != I2C_FIRST_AND_LAST_FRAME) - i2c_init(obj, obj_s->sda, obj_s->scl); + if (obj_s->XferOperation != I2C_FIRST_AND_LAST_FRAME) { + i2c_init(obj, obj_s->sda, obj_s->scl); + } return 0; } -int i2c_byte_read(i2c_t *obj, int last) { +int i2c_byte_read(i2c_t *obj, int last) +{ int timeout; struct i2c_s *obj_s = I2C_S(obj); @@ -529,7 +540,8 @@ return (int)handle->Instance->DR; } -int i2c_byte_write(i2c_t *obj, int data) { +int i2c_byte_write(i2c_t *obj, int data) +{ int timeout; struct i2c_s *obj_s = I2C_S(obj); @@ -541,30 +553,31 @@ timeout = FLAG_TIMEOUT; while ((__HAL_I2C_GET_FLAG(handle, I2C_FLAG_TXE) == RESET) && (__HAL_I2C_GET_FLAG(handle, I2C_FLAG_BTF) == RESET) && - (__HAL_I2C_GET_FLAG(handle, I2C_FLAG_ADDR) == RESET)) { + (__HAL_I2C_GET_FLAG(handle, I2C_FLAG_ADDR) == RESET)) { if ((timeout--) == 0) { return 2; } } - if (__HAL_I2C_GET_FLAG(handle, I2C_FLAG_ADDR) != RESET) - { - __HAL_I2C_CLEAR_ADDRFLAG(handle); - } + if (__HAL_I2C_GET_FLAG(handle, I2C_FLAG_ADDR) != RESET) { + __HAL_I2C_CLEAR_ADDRFLAG(handle); + } return 1; } #endif //I2C_IP_VERSION_V1 #ifdef I2C_IP_VERSION_V2 -int i2c_start(i2c_t *obj) { +int i2c_start(i2c_t *obj) +{ struct i2c_s *obj_s = I2C_S(obj); /* This I2C IP doesn't */ obj_s->pending_start = 1; return 0; } -int i2c_stop(i2c_t *obj) { +int i2c_stop(i2c_t *obj) +{ struct i2c_s *obj_s = I2C_S(obj); I2C_HandleTypeDef *handle = &(obj_s->handle); int timeout = FLAG_TIMEOUT; @@ -609,7 +622,8 @@ return 0; } -int i2c_byte_read(i2c_t *obj, int last) { +int i2c_byte_read(i2c_t *obj, int last) +{ struct i2c_s *obj_s = I2C_S(obj); I2C_HandleTypeDef *handle = &(obj_s->handle); int timeout = FLAG_TIMEOUT; @@ -654,7 +668,8 @@ return data; } -int i2c_byte_write(i2c_t *obj, int data) { +int i2c_byte_write(i2c_t *obj, int data) +{ struct i2c_s *obj_s = I2C_S(obj); I2C_HandleTypeDef *handle = &(obj_s->handle); int timeout = FLAG_TIMEOUT; @@ -669,10 +684,10 @@ //* First byte after the start is the address */ tmpreg |= (uint32_t)((uint32_t)data & I2C_CR2_SADD); if (data & 0x01) { - tmpreg |= I2C_CR2_START | I2C_CR2_RD_WRN; + tmpreg |= I2C_CR2_START | I2C_CR2_RD_WRN; } else { - tmpreg |= I2C_CR2_START; - tmpreg &= ~I2C_CR2_RD_WRN; + tmpreg |= I2C_CR2_START; + tmpreg &= ~I2C_CR2_RD_WRN; } /* Disable reload first to use it later */ tmpreg &= ~I2C_CR2_RELOAD; @@ -697,9 +712,9 @@ } /* Enable reload mode as we don't know how many bytes will eb sent */ tmpreg |= I2C_CR2_RELOAD; - /* Set transfer size to 1 */ + /* Set transfer size to 1 */ tmpreg |= (I2C_CR2_NBYTES & (1 << 16)); - /* Set the prepared configuration */ + /* Set the prepared configuration */ handle->Instance->CR2 = tmpreg; /* Prepare next write */ timeout = FLAG_TIMEOUT; @@ -719,7 +734,8 @@ /* * SYNC APIS */ -int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) { +int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) +{ struct i2c_s *obj_s = I2C_S(obj); I2C_HandleTypeDef *handle = &(obj_s->handle); int count = I2C_ERROR_BUS_BUSY, ret = 0; @@ -729,37 +745,39 @@ uint32_t op1 = I2C_FIRST_AND_LAST_FRAME; uint32_t op2 = I2C_LAST_FRAME; if ((obj_s->XferOperation == op1) || (obj_s->XferOperation == op2)) { - if (stop) + if (stop) { obj_s->XferOperation = I2C_FIRST_AND_LAST_FRAME; - else + } else { obj_s->XferOperation = I2C_FIRST_FRAME; + } } else if ((obj_s->XferOperation == I2C_FIRST_FRAME) || - (obj_s->XferOperation == I2C_NEXT_FRAME)) { - if (stop) + (obj_s->XferOperation == I2C_NEXT_FRAME)) { + if (stop) { obj_s->XferOperation = I2C_LAST_FRAME; - else + } else { obj_s->XferOperation = I2C_NEXT_FRAME; + } } obj_s->event = 0; - /* Activate default IRQ handlers for sync mode - * which would be overwritten in async mode - */ + /* Activate default IRQ handlers for sync mode + * which would be overwritten in async mode + */ i2c_ev_err_enable(obj, i2c_get_irq_handler(obj)); ret = HAL_I2C_Master_Sequential_Receive_IT(handle, address, (uint8_t *) data, length, obj_s->XferOperation); - if(ret == HAL_OK) { + if (ret == HAL_OK) { timeout = BYTE_TIMEOUT_US * (length + 1); /* transfer started : wait completion or timeout */ - while(!(obj_s->event & I2C_EVENT_ALL) && (--timeout != 0)) { + while (!(obj_s->event & I2C_EVENT_ALL) && (--timeout != 0)) { wait_us(1); } i2c_ev_err_disable(obj); - if((timeout == 0) || (obj_s->event != I2C_EVENT_TRANSFER_COMPLETE)) { + if ((timeout == 0) || (obj_s->event != I2C_EVENT_TRANSFER_COMPLETE)) { DEBUG_PRINTF(" TIMEOUT or error in i2c_read\r\n"); /* re-init IP to try and get back in a working state */ i2c_init(obj, obj_s->sda, obj_s->scl); @@ -773,7 +791,8 @@ return count; } -int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) { +int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) +{ struct i2c_s *obj_s = I2C_S(obj); I2C_HandleTypeDef *handle = &(obj_s->handle); int count = I2C_ERROR_BUS_BUSY, ret = 0; @@ -783,40 +802,42 @@ uint32_t op1 = I2C_FIRST_AND_LAST_FRAME; uint32_t op2 = I2C_LAST_FRAME; if ((obj_s->XferOperation == op1) || (obj_s->XferOperation == op2)) { - if (stop) + if (stop) { obj_s->XferOperation = I2C_FIRST_AND_LAST_FRAME; - else + } else { obj_s->XferOperation = I2C_FIRST_FRAME; + } } else if ((obj_s->XferOperation == I2C_FIRST_FRAME) || - (obj_s->XferOperation == I2C_NEXT_FRAME)) { - if (stop) + (obj_s->XferOperation == I2C_NEXT_FRAME)) { + if (stop) { obj_s->XferOperation = I2C_LAST_FRAME; - else + } else { obj_s->XferOperation = I2C_NEXT_FRAME; + } } obj_s->event = 0; i2c_ev_err_enable(obj, i2c_get_irq_handler(obj)); - ret = HAL_I2C_Master_Sequential_Transmit_IT(handle, address, (uint8_t *) data, length, obj_s->XferOperation); + ret = HAL_I2C_Master_Sequential_Transmit_IT(handle, address, (uint8_t *) data, length, obj_s->XferOperation); - if(ret == HAL_OK) { + if (ret == HAL_OK) { timeout = BYTE_TIMEOUT_US * (length + 1); /* transfer started : wait completion or timeout */ - while(!(obj_s->event & I2C_EVENT_ALL) && (--timeout != 0)) { + while (!(obj_s->event & I2C_EVENT_ALL) && (--timeout != 0)) { wait_us(1); } i2c_ev_err_disable(obj); - if((timeout == 0) || (obj_s->event != I2C_EVENT_TRANSFER_COMPLETE)) { + if ((timeout == 0) || (obj_s->event != I2C_EVENT_TRANSFER_COMPLETE)) { DEBUG_PRINTF(" TIMEOUT or error in i2c_write\r\n"); /* re-init IP to try and get back in a working state */ i2c_init(obj, obj_s->sda, obj_s->scl); - } else { + } else { count = length; - } + } } else { DEBUG_PRINTF("ERROR in i2c_read\r\n"); } @@ -824,7 +845,8 @@ return count; } -void HAL_I2C_MasterTxCpltCallback(I2C_HandleTypeDef *hi2c){ +void HAL_I2C_MasterTxCpltCallback(I2C_HandleTypeDef *hi2c) +{ /* Get object ptr based on handler ptr */ i2c_t *obj = get_i2c_obj(hi2c); struct i2c_s *obj_s = I2C_S(obj); @@ -838,9 +860,8 @@ obj_s->XferOperation = I2C_NEXT_FRAME; } - HAL_I2C_Master_Sequential_Receive_IT(hi2c, obj_s->address, (uint8_t*)obj->rx_buff.buffer , obj->rx_buff.length, obj_s->XferOperation); - } - else + HAL_I2C_Master_Sequential_Receive_IT(hi2c, obj_s->address, (uint8_t *)obj->rx_buff.buffer, obj->rx_buff.length, obj_s->XferOperation); + } else #endif { /* Set event flag */ @@ -848,7 +869,8 @@ } } -void HAL_I2C_MasterRxCpltCallback(I2C_HandleTypeDef *hi2c){ +void HAL_I2C_MasterRxCpltCallback(I2C_HandleTypeDef *hi2c) +{ /* Get object ptr based on handler ptr */ i2c_t *obj = get_i2c_obj(hi2c); struct i2c_s *obj_s = I2C_S(obj); @@ -857,7 +879,8 @@ obj_s->event = I2C_EVENT_TRANSFER_COMPLETE; } -void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *hi2c){ +void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *hi2c) +{ /* Get object ptr based on handler ptr */ i2c_t *obj = get_i2c_obj(hi2c); struct i2c_s *obj_s = I2C_S(obj); @@ -865,8 +888,9 @@ I2C_HandleTypeDef *handle = &(obj_s->handle); uint32_t address = 0; /* Store address to handle it after reset */ - if(obj_s->slave) + if (obj_s->slave) { address = handle->Init.OwnAddress1; + } #endif DEBUG_PRINTF("HAL_I2C_ErrorCallback:%d, index=%d\r\n", (int) hi2c->ErrorCode, obj_s->index); @@ -888,7 +912,8 @@ #if DEVICE_I2CSLAVE /* SLAVE API FUNCTIONS */ -void i2c_slave_address(i2c_t *obj, int idx, uint32_t address, uint32_t mask) { +void i2c_slave_address(i2c_t *obj, int idx, uint32_t address, uint32_t mask) +{ struct i2c_s *obj_s = I2C_S(obj); I2C_HandleTypeDef *handle = &(obj_s->handle); @@ -901,7 +926,8 @@ HAL_I2C_EnableListen_IT(handle); } -void i2c_slave_mode(i2c_t *obj, int enable_slave) { +void i2c_slave_mode(i2c_t *obj, int enable_slave) +{ struct i2c_s *obj_s = I2C_S(obj); I2C_HandleTypeDef *handle = &(obj_s->handle); @@ -922,29 +948,32 @@ #define WriteAddressed 3 // the master is writing to this slave (slave = receiver) -void HAL_I2C_AddrCallback(I2C_HandleTypeDef *hi2c, uint8_t TransferDirection, uint16_t AddrMatchCode) { +void HAL_I2C_AddrCallback(I2C_HandleTypeDef *hi2c, uint8_t TransferDirection, uint16_t AddrMatchCode) +{ /* Get object ptr based on handler ptr */ i2c_t *obj = get_i2c_obj(hi2c); struct i2c_s *obj_s = I2C_S(obj); /* Transfer direction in HAL is from Master point of view */ - if(TransferDirection == I2C_DIRECTION_RECEIVE) { + if (TransferDirection == I2C_DIRECTION_RECEIVE) { obj_s->pending_slave_tx_master_rx = 1; } - if(TransferDirection == I2C_DIRECTION_TRANSMIT) { + if (TransferDirection == I2C_DIRECTION_TRANSMIT) { obj_s->pending_slave_rx_maxter_tx = 1; } } -void HAL_I2C_SlaveTxCpltCallback(I2C_HandleTypeDef *I2cHandle){ +void HAL_I2C_SlaveTxCpltCallback(I2C_HandleTypeDef *I2cHandle) +{ /* Get object ptr based on handler ptr */ i2c_t *obj = get_i2c_obj(I2cHandle); struct i2c_s *obj_s = I2C_S(obj); obj_s->pending_slave_tx_master_rx = 0; } -void HAL_I2C_SlaveRxCpltCallback(I2C_HandleTypeDef *I2cHandle){ +void HAL_I2C_SlaveRxCpltCallback(I2C_HandleTypeDef *I2cHandle) +{ /* Get object ptr based on handler ptr */ i2c_t *obj = get_i2c_obj(I2cHandle); struct i2c_s *obj_s = I2C_S(obj); @@ -957,23 +986,25 @@ HAL_I2C_EnableListen_IT(hi2c); } -int i2c_slave_receive(i2c_t *obj) { +int i2c_slave_receive(i2c_t *obj) +{ struct i2c_s *obj_s = I2C_S(obj); int retValue = NoData; - if(obj_s->pending_slave_rx_maxter_tx) { - retValue = WriteAddressed; - } + if (obj_s->pending_slave_rx_maxter_tx) { + retValue = WriteAddressed; + } - if(obj_s->pending_slave_tx_master_rx) { - retValue = ReadAddressed; - } + if (obj_s->pending_slave_tx_master_rx) { + retValue = ReadAddressed; + } return (retValue); } -int i2c_slave_read(i2c_t *obj, char *data, int length) { +int i2c_slave_read(i2c_t *obj, char *data, int length) +{ struct i2c_s *obj_s = I2C_S(obj); I2C_HandleTypeDef *handle = &(obj_s->handle); int count = 0; @@ -983,22 +1014,23 @@ /* Always use I2C_NEXT_FRAME as slave will just adapt to master requests */ ret = HAL_I2C_Slave_Sequential_Receive_IT(handle, (uint8_t *) data, length, I2C_NEXT_FRAME); - if(ret == HAL_OK) { + if (ret == HAL_OK) { timeout = BYTE_TIMEOUT_US * (length + 1); - while(obj_s->pending_slave_rx_maxter_tx && (--timeout != 0)) { + while (obj_s->pending_slave_rx_maxter_tx && (--timeout != 0)) { wait_us(1); } - if(timeout != 0) { - count = length; - } else { - DEBUG_PRINTF("TIMEOUT or error in i2c_slave_read\r\n"); - } + if (timeout != 0) { + count = length; + } else { + DEBUG_PRINTF("TIMEOUT or error in i2c_slave_read\r\n"); + } } return count; } -int i2c_slave_write(i2c_t *obj, const char *data, int length) { +int i2c_slave_write(i2c_t *obj, const char *data, int length) +{ struct i2c_s *obj_s = I2C_S(obj); I2C_HandleTypeDef *handle = &(obj_s->handle); int count = 0; @@ -1008,17 +1040,17 @@ /* Always use I2C_NEXT_FRAME as slave will just adapt to master requests */ ret = HAL_I2C_Slave_Sequential_Transmit_IT(handle, (uint8_t *) data, length, I2C_NEXT_FRAME); - if(ret == HAL_OK) { + if (ret == HAL_OK) { timeout = BYTE_TIMEOUT_US * (length + 1); - while(obj_s->pending_slave_tx_master_rx && (--timeout != 0)) { + while (obj_s->pending_slave_tx_master_rx && (--timeout != 0)) { wait_us(1); } - if(timeout != 0) { - count = length; - } else { - DEBUG_PRINTF("TIMEOUT or error in i2c_slave_write\r\n"); - } + if (timeout != 0) { + count = length; + } else { + DEBUG_PRINTF("TIMEOUT or error in i2c_slave_write\r\n"); + } } return count; @@ -1027,7 +1059,8 @@ #if DEVICE_I2C_ASYNCH /* ASYNCH MASTER API FUNCTIONS */ -void HAL_I2C_AbortCpltCallback(I2C_HandleTypeDef *hi2c){ +void HAL_I2C_AbortCpltCallback(I2C_HandleTypeDef *hi2c) +{ /* Get object ptr based on handler ptr */ i2c_t *obj = get_i2c_obj(hi2c); struct i2c_s *obj_s = I2C_S(obj); @@ -1041,7 +1074,8 @@ obj_s->event = I2C_EVENT_ERROR; } -void i2c_transfer_asynch(i2c_t *obj, const void *tx, size_t tx_length, void *rx, size_t rx_length, uint32_t address, uint32_t stop, uint32_t handler, uint32_t event, DMAUsage hint) { +void i2c_transfer_asynch(i2c_t *obj, const void *tx, size_t tx_length, void *rx, size_t rx_length, uint32_t address, uint32_t stop, uint32_t handler, uint32_t event, DMAUsage hint) +{ // TODO: DMA usage is currently ignored by this way (void) hint; @@ -1073,41 +1107,43 @@ uint32_t op1 = I2C_FIRST_AND_LAST_FRAME; uint32_t op2 = I2C_LAST_FRAME; if ((obj_s->XferOperation == op1) || (obj_s->XferOperation == op2)) { - if (stop) + if (stop) { obj_s->XferOperation = I2C_FIRST_AND_LAST_FRAME; - else + } else { obj_s->XferOperation = I2C_FIRST_FRAME; + } } else if ((obj_s->XferOperation == I2C_FIRST_FRAME) || - (obj_s->XferOperation == I2C_NEXT_FRAME)) { - if (stop) + (obj_s->XferOperation == I2C_NEXT_FRAME)) { + if (stop) { obj_s->XferOperation = I2C_LAST_FRAME; - else + } else { obj_s->XferOperation = I2C_NEXT_FRAME; + } } if (tx_length > 0) { - HAL_I2C_Master_Sequential_Transmit_IT(handle, address, (uint8_t*)tx, tx_length, obj_s->XferOperation); + HAL_I2C_Master_Sequential_Transmit_IT(handle, address, (uint8_t *)tx, tx_length, obj_s->XferOperation); } if (rx_length > 0) { - HAL_I2C_Master_Sequential_Receive_IT(handle, address, (uint8_t*)rx, rx_length, obj_s->XferOperation); + HAL_I2C_Master_Sequential_Receive_IT(handle, address, (uint8_t *)rx, rx_length, obj_s->XferOperation); } - } - else if (tx_length && rx_length) { + } else if (tx_length && rx_length) { /* Two steps operation, don't modify XferOperation, keep it for next step */ // Trick to remove compiler warning "left and right operands are identical" in some cases uint32_t op1 = I2C_FIRST_AND_LAST_FRAME; uint32_t op2 = I2C_LAST_FRAME; if ((obj_s->XferOperation == op1) || (obj_s->XferOperation == op2)) { - HAL_I2C_Master_Sequential_Transmit_IT(handle, address, (uint8_t*)tx, tx_length, I2C_FIRST_FRAME); + HAL_I2C_Master_Sequential_Transmit_IT(handle, address, (uint8_t *)tx, tx_length, I2C_FIRST_FRAME); } else if ((obj_s->XferOperation == I2C_FIRST_FRAME) || - (obj_s->XferOperation == I2C_NEXT_FRAME)) { - HAL_I2C_Master_Sequential_Transmit_IT(handle, address, (uint8_t*)tx, tx_length, I2C_NEXT_FRAME); + (obj_s->XferOperation == I2C_NEXT_FRAME)) { + HAL_I2C_Master_Sequential_Transmit_IT(handle, address, (uint8_t *)tx, tx_length, I2C_NEXT_FRAME); } } } -uint32_t i2c_irq_handler_asynch(i2c_t *obj) { +uint32_t i2c_irq_handler_asynch(i2c_t *obj) +{ struct i2c_s *obj_s = I2C_S(obj); I2C_HandleTypeDef *handle = &(obj_s->handle); @@ -1115,24 +1151,25 @@ HAL_I2C_EV_IRQHandler(handle); HAL_I2C_ER_IRQHandler(handle); - /* Return I2C event status */ + /* Return I2C event status */ return (obj_s->event & obj_s->available_events); } -uint8_t i2c_active(i2c_t *obj) { +uint8_t i2c_active(i2c_t *obj) +{ struct i2c_s *obj_s = I2C_S(obj); I2C_HandleTypeDef *handle = &(obj_s->handle); if (handle->State == HAL_I2C_STATE_READY) { return 0; - } - else { + } else { return 1; } } -void i2c_abort_asynch(i2c_t *obj) { +void i2c_abort_asynch(i2c_t *obj) +{ struct i2c_s *obj_s = I2C_S(obj); I2C_HandleTypeDef *handle = &(obj_s->handle);