From 98bc190ac4b1b370dfc1f3a182fd760597761610 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=82=E3=81=8F?= Date: Sun, 5 Dec 2021 14:47:02 +0300 Subject: Hackathone session: bugfixes and documentation update (#869) * ReadMe: update flashing scripts section * Furi: add record exists method to record store. * FuriHal: early OS init and i2c timeouts based on os ticks. * Storage: replace malloc with furi_alloc, fix errors found by pvs. * iButton: properly handle shutdown in cli search command * SubGhz: proper argument type in sscanf and incorrect position of logging in switch. --- firmware/targets/f6/fatfs/stm32_adafruit_sd.c | 4 +- firmware/targets/f6/furi-hal/furi-hal-i2c.c | 109 +++++++++++++++----------- firmware/targets/f6/furi-hal/furi-hal.c | 6 +- firmware/targets/f7/fatfs/stm32_adafruit_sd.c | 4 +- firmware/targets/f7/furi-hal/furi-hal-i2c.c | 109 +++++++++++++++----------- firmware/targets/f7/furi-hal/furi-hal.c | 6 +- 6 files changed, 140 insertions(+), 98 deletions(-) (limited to 'firmware') diff --git a/firmware/targets/f6/fatfs/stm32_adafruit_sd.c b/firmware/targets/f6/fatfs/stm32_adafruit_sd.c index 0d7ff4c0..1c571d63 100644 --- a/firmware/targets/f6/fatfs/stm32_adafruit_sd.c +++ b/firmware/targets/f6/fatfs/stm32_adafruit_sd.c @@ -405,7 +405,7 @@ BSP_SD_ReadBlocks(uint32_t* pData, uint32_t ReadAddr, uint32_t NumOfBlocks, uint goto error; } - ptr = malloc(sizeof(uint8_t) * BlockSize); + ptr = furi_alloc(sizeof(uint8_t) * BlockSize); if(ptr == NULL) { goto error; } @@ -483,7 +483,7 @@ BSP_SD_WriteBlocks(uint32_t* pData, uint32_t WriteAddr, uint32_t NumOfBlocks, ui goto error; } - ptr = malloc(sizeof(uint8_t) * BlockSize); + ptr = furi_alloc(sizeof(uint8_t) * BlockSize); if(ptr == NULL) { goto error; } diff --git a/firmware/targets/f6/furi-hal/furi-hal-i2c.c b/firmware/targets/f6/furi-hal/furi-hal-i2c.c index 2fd5c5e3..3e64b390 100644 --- a/firmware/targets/f6/furi-hal/furi-hal-i2c.c +++ b/firmware/targets/f6/furi-hal/furi-hal-i2c.c @@ -46,38 +46,48 @@ bool furi_hal_i2c_tx( const uint8_t* data, uint8_t size, uint32_t timeout) { + furi_check(handle->bus->current_handle == handle); - uint32_t time_left = timeout; + furi_assert(timeout > 0); + bool ret = true; + uint32_t timeout_tick = osKernelGetTickCount() + timeout; - while(LL_I2C_IsActiveFlag_BUSY(handle->bus->i2c)) - ; - - LL_I2C_HandleTransfer( - handle->bus->i2c, - address, - LL_I2C_ADDRSLAVE_7BIT, - size, - LL_I2C_MODE_AUTOEND, - LL_I2C_GENERATE_START_WRITE); - - while(!LL_I2C_IsActiveFlag_STOP(handle->bus->i2c) || size > 0) { - if(LL_I2C_IsActiveFlag_TXIS(handle->bus->i2c)) { - LL_I2C_TransmitData8(handle->bus->i2c, (*data)); - data++; - size--; - time_left = timeout; + do { + while(LL_I2C_IsActiveFlag_BUSY(handle->bus->i2c)) { + if(osKernelGetTickCount() >= timeout_tick) { + ret = false; + break; + } } - if(LL_SYSTICK_IsActiveCounterFlag()) { - if(--time_left == 0) { + if(!ret) { + break; + } + + LL_I2C_HandleTransfer( + handle->bus->i2c, + address, + LL_I2C_ADDRSLAVE_7BIT, + size, + LL_I2C_MODE_AUTOEND, + LL_I2C_GENERATE_START_WRITE); + + while(!LL_I2C_IsActiveFlag_STOP(handle->bus->i2c) || size > 0) { + if(LL_I2C_IsActiveFlag_TXIS(handle->bus->i2c)) { + LL_I2C_TransmitData8(handle->bus->i2c, (*data)); + data++; + size--; + } + + if(osKernelGetTickCount() >= timeout_tick) { ret = false; break; } } - } - LL_I2C_ClearFlag_STOP(handle->bus->i2c); + LL_I2C_ClearFlag_STOP(handle->bus->i2c); + } while(0); return ret; } @@ -88,38 +98,48 @@ bool furi_hal_i2c_rx( uint8_t* data, uint8_t size, uint32_t timeout) { + furi_check(handle->bus->current_handle == handle); - uint32_t time_left = timeout; + furi_assert(timeout > 0); + bool ret = true; + uint32_t timeout_tick = osKernelGetTickCount() + timeout; + + do { + while(LL_I2C_IsActiveFlag_BUSY(handle->bus->i2c)) { + if(osKernelGetTickCount() >= timeout_tick) { + ret = false; + break; + } + } - while(LL_I2C_IsActiveFlag_BUSY(handle->bus->i2c)) - ; - - LL_I2C_HandleTransfer( - handle->bus->i2c, - address, - LL_I2C_ADDRSLAVE_7BIT, - size, - LL_I2C_MODE_AUTOEND, - LL_I2C_GENERATE_START_READ); - - while(!LL_I2C_IsActiveFlag_STOP(handle->bus->i2c) || size > 0) { - if(LL_I2C_IsActiveFlag_RXNE(handle->bus->i2c)) { - *data = LL_I2C_ReceiveData8(handle->bus->i2c); - data++; - size--; - time_left = timeout; + if(!ret) { + break; } - if(LL_SYSTICK_IsActiveCounterFlag()) { - if(--time_left == 0) { + LL_I2C_HandleTransfer( + handle->bus->i2c, + address, + LL_I2C_ADDRSLAVE_7BIT, + size, + LL_I2C_MODE_AUTOEND, + LL_I2C_GENERATE_START_READ); + + while(!LL_I2C_IsActiveFlag_STOP(handle->bus->i2c) || size > 0) { + if(LL_I2C_IsActiveFlag_RXNE(handle->bus->i2c)) { + *data = LL_I2C_ReceiveData8(handle->bus->i2c); + data++; + size--; + } + + if(osKernelGetTickCount() >= timeout_tick) { ret = false; break; } } - } - LL_I2C_ClearFlag_STOP(handle->bus->i2c); + LL_I2C_ClearFlag_STOP(handle->bus->i2c); + } while(0); return ret; } @@ -132,6 +152,7 @@ bool furi_hal_i2c_trx( uint8_t* rx_data, uint8_t rx_size, uint32_t timeout) { + if(furi_hal_i2c_tx(handle, address, tx_data, tx_size, timeout) && furi_hal_i2c_rx(handle, address, rx_data, rx_size, timeout)) { return true; diff --git a/firmware/targets/f6/furi-hal/furi-hal.c b/firmware/targets/f6/furi-hal/furi-hal.c index 491cdf0d..d213af95 100644 --- a/firmware/targets/f6/furi-hal/furi-hal.c +++ b/firmware/targets/f6/furi-hal/furi-hal.c @@ -17,6 +17,9 @@ void furi_hal_init() { furi_hal_interrupt_init(); furi_hal_delay_init(); + // FreeRTOS glue + furi_hal_os_init(); + MX_GPIO_Init(); FURI_LOG_I(TAG, "GPIO OK"); @@ -56,9 +59,6 @@ void furi_hal_init() { furi_hal_bt_init(); furi_hal_compress_icon_init(); - // FreeRTOS glue - furi_hal_os_init(); - // FatFS driver initialization MX_FATFS_Init(); FURI_LOG_I(TAG, "FATFS OK"); diff --git a/firmware/targets/f7/fatfs/stm32_adafruit_sd.c b/firmware/targets/f7/fatfs/stm32_adafruit_sd.c index 0d7ff4c0..1c571d63 100644 --- a/firmware/targets/f7/fatfs/stm32_adafruit_sd.c +++ b/firmware/targets/f7/fatfs/stm32_adafruit_sd.c @@ -405,7 +405,7 @@ BSP_SD_ReadBlocks(uint32_t* pData, uint32_t ReadAddr, uint32_t NumOfBlocks, uint goto error; } - ptr = malloc(sizeof(uint8_t) * BlockSize); + ptr = furi_alloc(sizeof(uint8_t) * BlockSize); if(ptr == NULL) { goto error; } @@ -483,7 +483,7 @@ BSP_SD_WriteBlocks(uint32_t* pData, uint32_t WriteAddr, uint32_t NumOfBlocks, ui goto error; } - ptr = malloc(sizeof(uint8_t) * BlockSize); + ptr = furi_alloc(sizeof(uint8_t) * BlockSize); if(ptr == NULL) { goto error; } diff --git a/firmware/targets/f7/furi-hal/furi-hal-i2c.c b/firmware/targets/f7/furi-hal/furi-hal-i2c.c index 2fd5c5e3..3e64b390 100644 --- a/firmware/targets/f7/furi-hal/furi-hal-i2c.c +++ b/firmware/targets/f7/furi-hal/furi-hal-i2c.c @@ -46,38 +46,48 @@ bool furi_hal_i2c_tx( const uint8_t* data, uint8_t size, uint32_t timeout) { + furi_check(handle->bus->current_handle == handle); - uint32_t time_left = timeout; + furi_assert(timeout > 0); + bool ret = true; + uint32_t timeout_tick = osKernelGetTickCount() + timeout; - while(LL_I2C_IsActiveFlag_BUSY(handle->bus->i2c)) - ; - - LL_I2C_HandleTransfer( - handle->bus->i2c, - address, - LL_I2C_ADDRSLAVE_7BIT, - size, - LL_I2C_MODE_AUTOEND, - LL_I2C_GENERATE_START_WRITE); - - while(!LL_I2C_IsActiveFlag_STOP(handle->bus->i2c) || size > 0) { - if(LL_I2C_IsActiveFlag_TXIS(handle->bus->i2c)) { - LL_I2C_TransmitData8(handle->bus->i2c, (*data)); - data++; - size--; - time_left = timeout; + do { + while(LL_I2C_IsActiveFlag_BUSY(handle->bus->i2c)) { + if(osKernelGetTickCount() >= timeout_tick) { + ret = false; + break; + } } - if(LL_SYSTICK_IsActiveCounterFlag()) { - if(--time_left == 0) { + if(!ret) { + break; + } + + LL_I2C_HandleTransfer( + handle->bus->i2c, + address, + LL_I2C_ADDRSLAVE_7BIT, + size, + LL_I2C_MODE_AUTOEND, + LL_I2C_GENERATE_START_WRITE); + + while(!LL_I2C_IsActiveFlag_STOP(handle->bus->i2c) || size > 0) { + if(LL_I2C_IsActiveFlag_TXIS(handle->bus->i2c)) { + LL_I2C_TransmitData8(handle->bus->i2c, (*data)); + data++; + size--; + } + + if(osKernelGetTickCount() >= timeout_tick) { ret = false; break; } } - } - LL_I2C_ClearFlag_STOP(handle->bus->i2c); + LL_I2C_ClearFlag_STOP(handle->bus->i2c); + } while(0); return ret; } @@ -88,38 +98,48 @@ bool furi_hal_i2c_rx( uint8_t* data, uint8_t size, uint32_t timeout) { + furi_check(handle->bus->current_handle == handle); - uint32_t time_left = timeout; + furi_assert(timeout > 0); + bool ret = true; + uint32_t timeout_tick = osKernelGetTickCount() + timeout; + + do { + while(LL_I2C_IsActiveFlag_BUSY(handle->bus->i2c)) { + if(osKernelGetTickCount() >= timeout_tick) { + ret = false; + break; + } + } - while(LL_I2C_IsActiveFlag_BUSY(handle->bus->i2c)) - ; - - LL_I2C_HandleTransfer( - handle->bus->i2c, - address, - LL_I2C_ADDRSLAVE_7BIT, - size, - LL_I2C_MODE_AUTOEND, - LL_I2C_GENERATE_START_READ); - - while(!LL_I2C_IsActiveFlag_STOP(handle->bus->i2c) || size > 0) { - if(LL_I2C_IsActiveFlag_RXNE(handle->bus->i2c)) { - *data = LL_I2C_ReceiveData8(handle->bus->i2c); - data++; - size--; - time_left = timeout; + if(!ret) { + break; } - if(LL_SYSTICK_IsActiveCounterFlag()) { - if(--time_left == 0) { + LL_I2C_HandleTransfer( + handle->bus->i2c, + address, + LL_I2C_ADDRSLAVE_7BIT, + size, + LL_I2C_MODE_AUTOEND, + LL_I2C_GENERATE_START_READ); + + while(!LL_I2C_IsActiveFlag_STOP(handle->bus->i2c) || size > 0) { + if(LL_I2C_IsActiveFlag_RXNE(handle->bus->i2c)) { + *data = LL_I2C_ReceiveData8(handle->bus->i2c); + data++; + size--; + } + + if(osKernelGetTickCount() >= timeout_tick) { ret = false; break; } } - } - LL_I2C_ClearFlag_STOP(handle->bus->i2c); + LL_I2C_ClearFlag_STOP(handle->bus->i2c); + } while(0); return ret; } @@ -132,6 +152,7 @@ bool furi_hal_i2c_trx( uint8_t* rx_data, uint8_t rx_size, uint32_t timeout) { + if(furi_hal_i2c_tx(handle, address, tx_data, tx_size, timeout) && furi_hal_i2c_rx(handle, address, rx_data, rx_size, timeout)) { return true; diff --git a/firmware/targets/f7/furi-hal/furi-hal.c b/firmware/targets/f7/furi-hal/furi-hal.c index 491cdf0d..d213af95 100644 --- a/firmware/targets/f7/furi-hal/furi-hal.c +++ b/firmware/targets/f7/furi-hal/furi-hal.c @@ -17,6 +17,9 @@ void furi_hal_init() { furi_hal_interrupt_init(); furi_hal_delay_init(); + // FreeRTOS glue + furi_hal_os_init(); + MX_GPIO_Init(); FURI_LOG_I(TAG, "GPIO OK"); @@ -56,9 +59,6 @@ void furi_hal_init() { furi_hal_bt_init(); furi_hal_compress_icon_init(); - // FreeRTOS glue - furi_hal_os_init(); - // FatFS driver initialization MX_FATFS_Init(); FURI_LOG_I(TAG, "FATFS OK"); -- cgit v1.2.3