From d9c05db05821843f6d0dcf0e9e09ef0e123d0d17 Mon Sep 17 00:00:00 2001 From: "Ben V. Brown" Date: Sat, 5 Sep 2020 20:04:07 +1000 Subject: [PATCH] I2C wrapper cleanup --- .../TS100/Core/BSP/Miniware/I2C_Wrapper.cpp | 132 +++++++----------- .../TS100/Core/BSP/Miniware/fusb302b.cpp | 10 +- workspace/TS100/Core/Drivers/I2CBB.cpp | 11 +- workspace/TS100/Core/Drivers/I2C_Wrapper.hpp | 12 +- 4 files changed, 63 insertions(+), 102 deletions(-) diff --git a/workspace/TS100/Core/BSP/Miniware/I2C_Wrapper.cpp b/workspace/TS100/Core/BSP/Miniware/I2C_Wrapper.cpp index 785e3b25..5eb60c81 100644 --- a/workspace/TS100/Core/BSP/Miniware/I2C_Wrapper.cpp +++ b/workspace/TS100/Core/BSP/Miniware/I2C_Wrapper.cpp @@ -7,8 +7,7 @@ #include #include "BSP.h" #include "Setup.h" -#define I2CUSESDMA -SemaphoreHandle_t FRToSI2C::I2CSemaphore; +SemaphoreHandle_t FRToSI2C::I2CSemaphore = nullptr; StaticSemaphore_t FRToSI2C::xSemaphoreBuffer; void FRToSI2C::CpltCallback() { @@ -21,45 +20,22 @@ void FRToSI2C::CpltCallback() { bool FRToSI2C::Mem_Read(uint16_t DevAddress, uint16_t MemAddress, uint8_t *pData, uint16_t Size) { - if (I2CSemaphore == NULL) { - // no RToS, run blocking code - HAL_I2C_Mem_Read(&hi2c1, DevAddress, MemAddress, I2C_MEMADD_SIZE_8BIT, - pData, Size, 5000); - return true; - } else { - // RToS is active, run threading - // Get the mutex so we can use the I2C port - // Wait up to 1 second for the mutex - if (xSemaphoreTake(I2CSemaphore, (TickType_t)500) == pdTRUE) { -#ifdef I2CUSESDMA - if (HAL_I2C_Mem_Read(&hi2c1, DevAddress, MemAddress, - I2C_MEMADD_SIZE_8BIT, pData, Size, 500) != HAL_OK) { + if (!lock()) + return false; + if (HAL_I2C_Mem_Read(&hi2c1, DevAddress, MemAddress, + I2C_MEMADD_SIZE_8BIT, pData, Size, 500) != HAL_OK) { - I2C_Unstick(); - xSemaphoreGive(I2CSemaphore); - return false; - } else { - xSemaphoreGive(I2CSemaphore); - return true; - } -#else - - if (HAL_I2C_Mem_Read(&hi2c1, DevAddress, MemAddress, I2C_MEMADD_SIZE_8BIT, pData, Size, - 5000)==HAL_OK){ - xSemaphoreGive(I2CSemaphore); - return true; - } - xSemaphoreGive(I2CSemaphore); - return false; -#endif - } else { - return false; - } + I2C_Unstick(); + unlock(); + return false; } + unlock(); + return true; + } -void FRToSI2C::I2C_RegisterWrite(uint8_t address, uint8_t reg, uint8_t data) { - Mem_Write(address, reg, &data, 1); +bool FRToSI2C::I2C_RegisterWrite(uint8_t address, uint8_t reg, uint8_t data) { + return Mem_Write(address, reg, &data, 1); } uint8_t FRToSI2C::I2C_RegisterRead(uint8_t add, uint8_t reg) { @@ -67,67 +43,55 @@ uint8_t FRToSI2C::I2C_RegisterRead(uint8_t add, uint8_t reg) { Mem_Read(add, reg, tx_data, 1); return tx_data[0]; } -void FRToSI2C::Mem_Write(uint16_t DevAddress, uint16_t MemAddress, +bool FRToSI2C::Mem_Write(uint16_t DevAddress, uint16_t MemAddress, uint8_t *pData, uint16_t Size) { - if (I2CSemaphore == NULL) { - // no RToS, run blocking code - HAL_I2C_Mem_Write(&hi2c1, DevAddress, MemAddress, I2C_MEMADD_SIZE_8BIT, - pData, Size, 5000); - } else { - // RToS is active, run threading - // Get the mutex so we can use the I2C port - // Wait up to 1 second for the mutex - if (xSemaphoreTake(I2CSemaphore, (TickType_t)500) == pdTRUE) { - if (HAL_I2C_Mem_Write(&hi2c1, DevAddress, MemAddress, - I2C_MEMADD_SIZE_8BIT, pData, Size, 500) != HAL_OK) { + if (!lock()) + return false; + if (HAL_I2C_Mem_Write(&hi2c1, DevAddress, MemAddress, + I2C_MEMADD_SIZE_8BIT, pData, Size, 500) != HAL_OK) { - I2C_Unstick(); - xSemaphoreGive(I2CSemaphore); - } - xSemaphoreGive(I2CSemaphore); - - } + I2C_Unstick(); + unlock(); + return false; } + unlock(); + return true; } -void FRToSI2C::Transmit(uint16_t DevAddress, uint8_t *pData, uint16_t Size) { - if (I2CSemaphore == NULL) { - // no RToS, run blocking code - HAL_I2C_Master_Transmit(&hi2c1, DevAddress, pData, Size, 5000); - } else { - // RToS is active, run threading - // Get the mutex so we can use the I2C port - // Wait up to 1 second for the mutex - if (xSemaphoreTake(I2CSemaphore, (TickType_t)50) == pdTRUE) { -#ifdef I2CUSESDMA - - if (HAL_I2C_Master_Transmit_DMA(&hi2c1, DevAddress, pData, Size) - != HAL_OK) { - - I2C_Unstick(); - xSemaphoreGive(I2CSemaphore); - - } -#else - HAL_I2C_Master_Transmit(&hi2c1, DevAddress, pData, Size, 5000); - xSemaphoreGive(I2CSemaphore); -#endif - - } else { - } +bool FRToSI2C::Transmit(uint16_t DevAddress, uint8_t *pData, uint16_t Size) { + if (!lock()) + return false; + if (HAL_I2C_Master_Transmit_DMA(&hi2c1, DevAddress, pData, Size) + != HAL_OK) { + I2C_Unstick(); + unlock(); + return false; } - + return true; } bool FRToSI2C::probe(uint16_t DevAddress) { + if (!lock()) + return false; uint8_t buffer[1]; - return HAL_I2C_Mem_Read(&hi2c1, DevAddress, 0x0F, I2C_MEMADD_SIZE_8BIT, - buffer, 1, 1000) == HAL_OK; - + bool worked = HAL_I2C_Mem_Read(&hi2c1, DevAddress, 0x0F, + I2C_MEMADD_SIZE_8BIT, buffer, 1, 1000) == HAL_OK; + unlock(); + return worked; } void FRToSI2C::I2C_Unstick() { unstick_I2C(); } + +void FRToSI2C::unlock() { + + xSemaphoreGive(I2CSemaphore); +} + +bool FRToSI2C::lock() { + + return xSemaphoreTake(I2CSemaphore, (TickType_t)50) == pdTRUE; +} diff --git a/workspace/TS100/Core/BSP/Miniware/fusb302b.cpp b/workspace/TS100/Core/BSP/Miniware/fusb302b.cpp index 9deb8c2f..7ccfc644 100644 --- a/workspace/TS100/Core/BSP/Miniware/fusb302b.cpp +++ b/workspace/TS100/Core/BSP/Miniware/fusb302b.cpp @@ -152,10 +152,8 @@ void fusb_setup() { HAL_NVIC_SetPriority(EXTI9_5_IRQn, 12, 0); HAL_NVIC_EnableIRQ(EXTI9_5_IRQn); - if (xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED) { - if (!I2CBB::lock2()) { - return; - } + if (!I2CBB::lock2()) { + return; } /* Fully reset the FUSB302B */ fusb_write_byte( FUSB_RESET, FUSB_RESET_SW_RES); @@ -200,9 +198,7 @@ void fusb_setup() { fusb_write_byte( FUSB_SWITCHES1, 0x26); fusb_write_byte( FUSB_SWITCHES0, 0x0B); } - if (xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED) { - I2CBB::unlock2(); - } + I2CBB::unlock2(); fusb_reset(); } diff --git a/workspace/TS100/Core/Drivers/I2CBB.cpp b/workspace/TS100/Core/Drivers/I2CBB.cpp index c21a3a97..200fecfe 100644 --- a/workspace/TS100/Core/Drivers/I2CBB.cpp +++ b/workspace/TS100/Core/Drivers/I2CBB.cpp @@ -17,28 +17,31 @@ void I2CBB::init() { GPIO_InitTypeDef GPIO_InitStruct; __HAL_RCC_GPIOA_CLK_ENABLE(); GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM; - GPIO_InitStruct.Pin = SDA2_Pin ; + GPIO_InitStruct.Pin = SDA2_Pin; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD; GPIO_InitStruct.Pull = GPIO_PULLUP; HAL_GPIO_Init(SDA2_GPIO_Port, &GPIO_InitStruct); GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM; - GPIO_InitStruct.Pin = SCL2_Pin; + GPIO_InitStruct.Pin = SCL2_Pin; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD; GPIO_InitStruct.Pull = GPIO_PULLUP; HAL_GPIO_Init(SCL2_GPIO_Port, &GPIO_InitStruct); SOFT_SDA_HIGH(); SOFT_SCL_HIGH(); - I2CSemaphore = xSemaphoreCreateMutexStatic (&xSemaphoreBuffer); - I2CSemaphore2 = xSemaphoreCreateMutexStatic (&xSemaphoreBuffer2); + I2CSemaphore = xSemaphoreCreateMutexStatic(&xSemaphoreBuffer); + I2CSemaphore2 = xSemaphoreCreateMutexStatic(&xSemaphoreBuffer2); unlock(); unlock2(); } bool I2CBB::probe(uint8_t address) { + if (!lock()) + return false; start(); bool ack = send(address); stop(); + unlock(); return ack; } diff --git a/workspace/TS100/Core/Drivers/I2C_Wrapper.hpp b/workspace/TS100/Core/Drivers/I2C_Wrapper.hpp index 779ddc20..f8981414 100644 --- a/workspace/TS100/Core/Drivers/I2C_Wrapper.hpp +++ b/workspace/TS100/Core/Drivers/I2C_Wrapper.hpp @@ -21,10 +21,6 @@ class FRToSI2C { public: - static void init() { - I2CSemaphore = nullptr; - } - static void FRToSInit() { I2CSemaphore = xSemaphoreCreateBinaryStatic(&xSemaphoreBuffer); xSemaphoreGive(I2CSemaphore); @@ -34,19 +30,21 @@ public: static bool Mem_Read(uint16_t DevAddress, uint16_t MemAddress, uint8_t *pData, uint16_t Size); - static void Mem_Write(uint16_t DevAddress, uint16_t MemAddress, + static bool Mem_Write(uint16_t DevAddress, uint16_t MemAddress, uint8_t *pData, uint16_t Size); //Returns true if device ACK's being addressed static bool probe(uint16_t DevAddress); - static void Transmit(uint16_t DevAddress, uint8_t *pData, uint16_t Size); + static bool Transmit(uint16_t DevAddress, uint8_t *pData, uint16_t Size); static void Receive(uint16_t DevAddress, uint8_t *pData, uint16_t Size); static void TransmitReceive(uint16_t DevAddress, uint8_t *pData_tx, uint16_t Size_tx, uint8_t *pData_rx, uint16_t Size_rx); - static void I2C_RegisterWrite(uint8_t address, uint8_t reg, uint8_t data); + static bool I2C_RegisterWrite(uint8_t address, uint8_t reg, uint8_t data); static uint8_t I2C_RegisterRead(uint8_t address, uint8_t reg); private: + static void unlock(); + static bool lock(); static void I2C_Unstick(); static SemaphoreHandle_t I2CSemaphore; static StaticSemaphore_t xSemaphoreBuffer;