1
0
forked from me/IronOS

I2C wrapper cleanup

This commit is contained in:
Ben V. Brown
2020-09-05 20:04:07 +10:00
parent 372f8e3565
commit d9c05db058
4 changed files with 63 additions and 102 deletions

View File

@@ -7,8 +7,7 @@
#include <I2C_Wrapper.hpp> #include <I2C_Wrapper.hpp>
#include "BSP.h" #include "BSP.h"
#include "Setup.h" #include "Setup.h"
#define I2CUSESDMA SemaphoreHandle_t FRToSI2C::I2CSemaphore = nullptr;
SemaphoreHandle_t FRToSI2C::I2CSemaphore;
StaticSemaphore_t FRToSI2C::xSemaphoreBuffer; StaticSemaphore_t FRToSI2C::xSemaphoreBuffer;
void FRToSI2C::CpltCallback() { void FRToSI2C::CpltCallback() {
@@ -21,45 +20,22 @@ void FRToSI2C::CpltCallback() {
bool FRToSI2C::Mem_Read(uint16_t DevAddress, uint16_t MemAddress, bool FRToSI2C::Mem_Read(uint16_t DevAddress, uint16_t MemAddress,
uint8_t *pData, uint16_t Size) { uint8_t *pData, uint16_t Size) {
if (I2CSemaphore == NULL) { if (!lock())
// no RToS, run blocking code return false;
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, if (HAL_I2C_Mem_Read(&hi2c1, DevAddress, MemAddress,
I2C_MEMADD_SIZE_8BIT, pData, Size, 500) != HAL_OK) { I2C_MEMADD_SIZE_8BIT, pData, Size, 500) != HAL_OK) {
I2C_Unstick(); I2C_Unstick();
xSemaphoreGive(I2CSemaphore); unlock();
return false; return false;
} else {
xSemaphoreGive(I2CSemaphore);
return true;
} }
#else
if (HAL_I2C_Mem_Read(&hi2c1, DevAddress, MemAddress, I2C_MEMADD_SIZE_8BIT, pData, Size, unlock();
5000)==HAL_OK){
xSemaphoreGive(I2CSemaphore);
return true; return true;
}
xSemaphoreGive(I2CSemaphore);
return false;
#endif
} else {
return false;
}
}
} }
void FRToSI2C::I2C_RegisterWrite(uint8_t address, uint8_t reg, uint8_t data) { bool FRToSI2C::I2C_RegisterWrite(uint8_t address, uint8_t reg, uint8_t data) {
Mem_Write(address, reg, &data, 1); return Mem_Write(address, reg, &data, 1);
} }
uint8_t FRToSI2C::I2C_RegisterRead(uint8_t add, uint8_t reg) { 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); Mem_Read(add, reg, tx_data, 1);
return tx_data[0]; 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) { uint8_t *pData, uint16_t Size) {
if (I2CSemaphore == NULL) { if (!lock())
// no RToS, run blocking code return false;
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, if (HAL_I2C_Mem_Write(&hi2c1, DevAddress, MemAddress,
I2C_MEMADD_SIZE_8BIT, pData, Size, 500) != HAL_OK) { I2C_MEMADD_SIZE_8BIT, pData, Size, 500) != HAL_OK) {
I2C_Unstick(); I2C_Unstick();
xSemaphoreGive(I2CSemaphore); unlock();
} return false;
xSemaphoreGive(I2CSemaphore);
}
} }
unlock();
return true;
} }
void FRToSI2C::Transmit(uint16_t DevAddress, uint8_t *pData, uint16_t Size) { bool FRToSI2C::Transmit(uint16_t DevAddress, uint8_t *pData, uint16_t Size) {
if (I2CSemaphore == NULL) { if (!lock())
// no RToS, run blocking code return false;
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) if (HAL_I2C_Master_Transmit_DMA(&hi2c1, DevAddress, pData, Size)
!= HAL_OK) { != HAL_OK) {
I2C_Unstick(); I2C_Unstick();
xSemaphoreGive(I2CSemaphore); unlock();
return false;
} }
#else return true;
HAL_I2C_Master_Transmit(&hi2c1, DevAddress, pData, Size, 5000);
xSemaphoreGive(I2CSemaphore);
#endif
} else {
}
}
} }
bool FRToSI2C::probe(uint16_t DevAddress) { bool FRToSI2C::probe(uint16_t DevAddress) {
if (!lock())
return false;
uint8_t buffer[1]; uint8_t buffer[1];
return HAL_I2C_Mem_Read(&hi2c1, DevAddress, 0x0F, I2C_MEMADD_SIZE_8BIT, bool worked = HAL_I2C_Mem_Read(&hi2c1, DevAddress, 0x0F,
buffer, 1, 1000) == HAL_OK; I2C_MEMADD_SIZE_8BIT, buffer, 1, 1000) == HAL_OK;
unlock();
return worked;
} }
void FRToSI2C::I2C_Unstick() { void FRToSI2C::I2C_Unstick() {
unstick_I2C(); unstick_I2C();
} }
void FRToSI2C::unlock() {
xSemaphoreGive(I2CSemaphore);
}
bool FRToSI2C::lock() {
return xSemaphoreTake(I2CSemaphore, (TickType_t)50) == pdTRUE;
}

View File

@@ -152,11 +152,9 @@ void fusb_setup() {
HAL_NVIC_SetPriority(EXTI9_5_IRQn, 12, 0); HAL_NVIC_SetPriority(EXTI9_5_IRQn, 12, 0);
HAL_NVIC_EnableIRQ(EXTI9_5_IRQn); HAL_NVIC_EnableIRQ(EXTI9_5_IRQn);
if (xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED) {
if (!I2CBB::lock2()) { if (!I2CBB::lock2()) {
return; return;
} }
}
/* Fully reset the FUSB302B */ /* Fully reset the FUSB302B */
fusb_write_byte( FUSB_RESET, FUSB_RESET_SW_RES); fusb_write_byte( FUSB_RESET, FUSB_RESET_SW_RES);
osDelay(2); osDelay(2);
@@ -200,9 +198,7 @@ void fusb_setup() {
fusb_write_byte( FUSB_SWITCHES1, 0x26); fusb_write_byte( FUSB_SWITCHES1, 0x26);
fusb_write_byte( FUSB_SWITCHES0, 0x0B); fusb_write_byte( FUSB_SWITCHES0, 0x0B);
} }
if (xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED) {
I2CBB::unlock2(); I2CBB::unlock2();
}
fusb_reset(); fusb_reset();
} }

View File

@@ -17,7 +17,7 @@ void I2CBB::init() {
GPIO_InitTypeDef GPIO_InitStruct; GPIO_InitTypeDef GPIO_InitStruct;
__HAL_RCC_GPIOA_CLK_ENABLE(); __HAL_RCC_GPIOA_CLK_ENABLE();
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM; 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.Mode = GPIO_MODE_OUTPUT_OD;
GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Pull = GPIO_PULLUP;
HAL_GPIO_Init(SDA2_GPIO_Port, &GPIO_InitStruct); HAL_GPIO_Init(SDA2_GPIO_Port, &GPIO_InitStruct);
@@ -28,17 +28,20 @@ void I2CBB::init() {
HAL_GPIO_Init(SCL2_GPIO_Port, &GPIO_InitStruct); HAL_GPIO_Init(SCL2_GPIO_Port, &GPIO_InitStruct);
SOFT_SDA_HIGH(); SOFT_SDA_HIGH();
SOFT_SCL_HIGH(); SOFT_SCL_HIGH();
I2CSemaphore = xSemaphoreCreateMutexStatic (&xSemaphoreBuffer); I2CSemaphore = xSemaphoreCreateMutexStatic(&xSemaphoreBuffer);
I2CSemaphore2 = xSemaphoreCreateMutexStatic (&xSemaphoreBuffer2); I2CSemaphore2 = xSemaphoreCreateMutexStatic(&xSemaphoreBuffer2);
unlock(); unlock();
unlock2(); unlock2();
} }
bool I2CBB::probe(uint8_t address) { bool I2CBB::probe(uint8_t address) {
if (!lock())
return false;
start(); start();
bool ack = send(address); bool ack = send(address);
stop(); stop();
unlock();
return ack; return ack;
} }

View File

@@ -21,10 +21,6 @@
class FRToSI2C { class FRToSI2C {
public: public:
static void init() {
I2CSemaphore = nullptr;
}
static void FRToSInit() { static void FRToSInit() {
I2CSemaphore = xSemaphoreCreateBinaryStatic(&xSemaphoreBuffer); I2CSemaphore = xSemaphoreCreateBinaryStatic(&xSemaphoreBuffer);
xSemaphoreGive(I2CSemaphore); xSemaphoreGive(I2CSemaphore);
@@ -34,19 +30,21 @@ public:
static bool Mem_Read(uint16_t DevAddress, uint16_t MemAddress, static bool Mem_Read(uint16_t DevAddress, uint16_t MemAddress,
uint8_t *pData, uint16_t Size); 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); uint8_t *pData, uint16_t Size);
//Returns true if device ACK's being addressed //Returns true if device ACK's being addressed
static bool probe(uint16_t DevAddress); 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 Receive(uint16_t DevAddress, uint8_t *pData, uint16_t Size);
static void TransmitReceive(uint16_t DevAddress, uint8_t *pData_tx, static void TransmitReceive(uint16_t DevAddress, uint8_t *pData_tx,
uint16_t Size_tx, uint8_t *pData_rx, uint16_t Size_rx); 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); static uint8_t I2C_RegisterRead(uint8_t address, uint8_t reg);
private: private:
static void unlock();
static bool lock();
static void I2C_Unstick(); static void I2C_Unstick();
static SemaphoreHandle_t I2CSemaphore; static SemaphoreHandle_t I2CSemaphore;
static StaticSemaphore_t xSemaphoreBuffer; static StaticSemaphore_t xSemaphoreBuffer;