提交 12d370a1 编写于 作者: M me-no-dev

Allow disabling of the HAL locks

上级 9244da45
......@@ -14,9 +14,11 @@
#include "esp32-hal-i2c.h"
#include "esp32-hal.h"
#if !CONFIG_DISABLE_HAL_LOCKS
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/semphr.h"
#endif
#include "esp_attr.h"
#include "esp_system.h"
#include "soc/soc_caps.h"
......@@ -27,7 +29,9 @@
typedef volatile struct {
bool initialized;
uint32_t frequency;
#if !CONFIG_DISABLE_HAL_LOCKS
SemaphoreHandle_t lock;
#endif
} i2c_bus_t;
static i2c_bus_t bus[SOC_I2C_NUM];
......@@ -43,6 +47,7 @@ esp_err_t i2cInit(uint8_t i2c_num, int8_t sda, int8_t scl, uint32_t frequency){
if(i2c_num >= SOC_I2C_NUM){
return ESP_ERR_INVALID_ARG;
}
#if !CONFIG_DISABLE_HAL_LOCKS
if(bus[i2c_num].lock == NULL){
bus[i2c_num].lock = xSemaphoreCreateMutex();
if(bus[i2c_num].lock == NULL){
......@@ -55,6 +60,7 @@ esp_err_t i2cInit(uint8_t i2c_num, int8_t sda, int8_t scl, uint32_t frequency){
log_e("could not acquire lock");
return ESP_FAIL;
}
#endif
if(bus[i2c_num].initialized){
log_e("bus is already initialized");
return ESP_FAIL;
......@@ -86,8 +92,10 @@ esp_err_t i2cInit(uint8_t i2c_num, int8_t sda, int8_t scl, uint32_t frequency){
bus[i2c_num].frequency = frequency;
}
}
#if !CONFIG_DISABLE_HAL_LOCKS
//release lock
xSemaphoreGive(bus[i2c_num].lock);
#endif
return ret;
}
......@@ -96,11 +104,13 @@ esp_err_t i2cDeinit(uint8_t i2c_num){
if(i2c_num >= SOC_I2C_NUM){
return ESP_ERR_INVALID_ARG;
}
#if !CONFIG_DISABLE_HAL_LOCKS
//acquire lock
if(bus[i2c_num].lock == NULL || xSemaphoreTake(bus[i2c_num].lock, portMAX_DELAY) != pdTRUE){
log_e("could not acquire lock");
return err;
}
#endif
if(!bus[i2c_num].initialized){
log_e("bus is not initialized");
} else {
......@@ -109,8 +119,10 @@ esp_err_t i2cDeinit(uint8_t i2c_num){
bus[i2c_num].initialized = false;
}
}
#if !CONFIG_DISABLE_HAL_LOCKS
//release lock
xSemaphoreGive(bus[i2c_num].lock);
#endif
return err;
}
......@@ -120,11 +132,13 @@ esp_err_t i2cWrite(uint8_t i2c_num, uint16_t address, const uint8_t* buff, size_
if(i2c_num >= SOC_I2C_NUM){
return ESP_ERR_INVALID_ARG;
}
#if !CONFIG_DISABLE_HAL_LOCKS
//acquire lock
if(bus[i2c_num].lock == NULL || xSemaphoreTake(bus[i2c_num].lock, portMAX_DELAY) != pdTRUE){
log_e("could not acquire lock");
return ret;
}
#endif
if(!bus[i2c_num].initialized){
log_e("bus is not initialized");
goto end;
......@@ -160,8 +174,10 @@ end:
if(cmd != NULL){
i2c_cmd_link_delete_static(cmd);
}
#if !CONFIG_DISABLE_HAL_LOCKS
//release lock
xSemaphoreGive(bus[i2c_num].lock);
#endif
return ret;
}
......@@ -170,11 +186,13 @@ esp_err_t i2cRead(uint8_t i2c_num, uint16_t address, uint8_t* buff, size_t size,
if(i2c_num >= SOC_I2C_NUM){
return ESP_ERR_INVALID_ARG;
}
#if !CONFIG_DISABLE_HAL_LOCKS
//acquire lock
if(bus[i2c_num].lock == NULL || xSemaphoreTake(bus[i2c_num].lock, portMAX_DELAY) != pdTRUE){
log_e("could not acquire lock");
return ret;
}
#endif
if(!bus[i2c_num].initialized){
log_e("bus is not initialized");
} else {
......@@ -185,8 +203,10 @@ esp_err_t i2cRead(uint8_t i2c_num, uint16_t address, uint8_t* buff, size_t size,
*readCount = 0;
}
}
#if !CONFIG_DISABLE_HAL_LOCKS
//release lock
xSemaphoreGive(bus[i2c_num].lock);
#endif
return ret;
}
......@@ -195,11 +215,13 @@ esp_err_t i2cWriteReadNonStop(uint8_t i2c_num, uint16_t address, const uint8_t*
if(i2c_num >= SOC_I2C_NUM){
return ESP_ERR_INVALID_ARG;
}
#if !CONFIG_DISABLE_HAL_LOCKS
//acquire lock
if(bus[i2c_num].lock == NULL || xSemaphoreTake(bus[i2c_num].lock, portMAX_DELAY) != pdTRUE){
log_e("could not acquire lock");
return ret;
}
#endif
if(!bus[i2c_num].initialized){
log_e("bus is not initialized");
} else {
......@@ -210,8 +232,10 @@ esp_err_t i2cWriteReadNonStop(uint8_t i2c_num, uint16_t address, const uint8_t*
*readCount = 0;
}
}
#if !CONFIG_DISABLE_HAL_LOCKS
//release lock
xSemaphoreGive(bus[i2c_num].lock);
#endif
return ret;
}
......@@ -220,11 +244,13 @@ esp_err_t i2cSetClock(uint8_t i2c_num, uint32_t frequency){
if(i2c_num >= SOC_I2C_NUM){
return ESP_ERR_INVALID_ARG;
}
#if !CONFIG_DISABLE_HAL_LOCKS
//acquire lock
if(bus[i2c_num].lock == NULL || xSemaphoreTake(bus[i2c_num].lock, portMAX_DELAY) != pdTRUE){
log_e("could not acquire lock");
return ret;
}
#endif
if(!bus[i2c_num].initialized){
log_e("bus is not initialized");
goto end;
......@@ -285,8 +311,10 @@ esp_err_t i2cSetClock(uint8_t i2c_num, uint32_t frequency){
}
end:
#if !CONFIG_DISABLE_HAL_LOCKS
//release lock
xSemaphoreGive(bus[i2c_num].lock);
#endif
return ret;
}
......
......@@ -43,20 +43,25 @@ TwoWire::TwoWire(uint8_t bus_num)
,txAddress(0)
,_timeOutMillis(50)
,nonStop(false)
#if !CONFIG_DISABLE_HAL_LOCKS
,nonStopTask(NULL)
,lock(NULL)
#endif
{}
TwoWire::~TwoWire()
{
end();
#if !CONFIG_DISABLE_HAL_LOCKS
if(lock != NULL){
vSemaphoreDelete(lock);
}
#endif
}
bool TwoWire::setPins(int sdaPin, int sclPin)
{
#if !CONFIG_DISABLE_HAL_LOCKS
if(lock == NULL){
lock = xSemaphoreCreateMutex();
if(lock == NULL){
......@@ -69,14 +74,17 @@ bool TwoWire::setPins(int sdaPin, int sclPin)
log_e("could not acquire lock");
return false;
}
#endif
if(!i2cIsInit(num)){
sda = sdaPin;
scl = sclPin;
} else {
log_e("bus already initialized. change pins only when not.");
}
#if !CONFIG_DISABLE_HAL_LOCKS
//release lock
xSemaphoreGive(lock);
#endif
return !i2cIsInit(num);
}
......@@ -84,6 +92,7 @@ bool TwoWire::begin(int sdaPin, int sclPin, uint32_t frequency)
{
bool started = false;
esp_err_t err = ESP_OK;
#if !CONFIG_DISABLE_HAL_LOCKS
if(lock == NULL){
lock = xSemaphoreCreateMutex();
if(lock == NULL){
......@@ -96,6 +105,7 @@ bool TwoWire::begin(int sdaPin, int sclPin, uint32_t frequency)
log_e("could not acquire lock");
return false;
}
#endif
if(i2cIsInit(num)){
started = true;
goto end;
......@@ -140,8 +150,10 @@ bool TwoWire::begin(int sdaPin, int sclPin, uint32_t frequency)
started = (err == ESP_OK);
end:
#if !CONFIG_DISABLE_HAL_LOCKS
//release lock
xSemaphoreGive(lock);
#endif
return started;
}
......@@ -149,46 +161,58 @@ end:
bool TwoWire::end()
{
esp_err_t err = ESP_OK;
#if !CONFIG_DISABLE_HAL_LOCKS
if(lock != NULL){
//acquire lock
if(xSemaphoreTake(lock, portMAX_DELAY) != pdTRUE){
log_e("could not acquire lock");
return false;
}
#endif
if(i2cIsInit(num)){
err = i2cDeinit(num);
}
#if !CONFIG_DISABLE_HAL_LOCKS
//release lock
xSemaphoreGive(lock);
}
#endif
return (err == ESP_OK);
}
uint32_t TwoWire::getClock()
{
uint32_t frequency = 0;
#if !CONFIG_DISABLE_HAL_LOCKS
//acquire lock
if(lock == NULL || xSemaphoreTake(lock, portMAX_DELAY) != pdTRUE){
log_e("could not acquire lock");
} else {
#endif
i2cGetClock(num, &frequency);
#if !CONFIG_DISABLE_HAL_LOCKS
//release lock
xSemaphoreGive(lock);
}
#endif
return frequency;
}
bool TwoWire::setClock(uint32_t frequency)
{
esp_err_t err = ESP_OK;
#if !CONFIG_DISABLE_HAL_LOCKS
//acquire lock
if(lock == NULL || xSemaphoreTake(lock, portMAX_DELAY) != pdTRUE){
log_e("could not acquire lock");
return false;
}
#endif
err = i2cSetClock(num, frequency);
#if !CONFIG_DISABLE_HAL_LOCKS
//release lock
xSemaphoreGive(lock);
#endif
return (err == ESP_OK);
}
......@@ -204,9 +228,9 @@ uint16_t TwoWire::getTimeOut()
void TwoWire::beginTransmission(uint16_t address)
{
#if !CONFIG_DISABLE_HAL_LOCKS
if(nonStop && nonStopTask == xTaskGetCurrentTaskHandle()){
log_e("Unfinished Repeated Start transaction! Expected requestFrom, not beginTransmission! Clearing...");
nonStop = false;
//release lock
xSemaphoreGive(lock);
}
......@@ -215,6 +239,8 @@ void TwoWire::beginTransmission(uint16_t address)
log_e("could not acquire lock");
return;
}
#endif
nonStop = false;
txAddress = address;
txLength = 0;
}
......@@ -224,12 +250,16 @@ uint8_t TwoWire::endTransmission(bool sendStop)
esp_err_t err = ESP_OK;
if(sendStop){
err = i2cWrite(num, txAddress, txBuffer, txLength, _timeOutMillis);
#if !CONFIG_DISABLE_HAL_LOCKS
//release lock
xSemaphoreGive(lock);
#endif
} else {
//mark as non-stop
nonStop = true;
#if !CONFIG_DISABLE_HAL_LOCKS
nonStopTask = xTaskGetCurrentTaskHandle();
#endif
}
switch(err){
case ESP_OK: return 0;
......@@ -243,7 +273,11 @@ uint8_t TwoWire::endTransmission(bool sendStop)
uint8_t TwoWire::requestFrom(uint16_t address, uint8_t size, bool sendStop)
{
esp_err_t err = ESP_OK;
if(nonStop && nonStopTask == xTaskGetCurrentTaskHandle()){
if(nonStop
#if !CONFIG_DISABLE_HAL_LOCKS
&& nonStopTask == xTaskGetCurrentTaskHandle()
#endif
){
if(address != txAddress){
log_e("Unfinished Repeated Start transaction! Expected address do not match! %u != %u", address, txAddress);
return 0;
......@@ -253,17 +287,21 @@ uint8_t TwoWire::requestFrom(uint16_t address, uint8_t size, bool sendStop)
rxLength = 0;
err = i2cWriteReadNonStop(num, address, txBuffer, txLength, rxBuffer, size, _timeOutMillis, &rxLength);
} else {
#if !CONFIG_DISABLE_HAL_LOCKS
//acquire lock
if(lock == NULL || xSemaphoreTake(lock, portMAX_DELAY) != pdTRUE){
log_e("could not acquire lock");
return 0;
}
#endif
rxIndex = 0;
rxLength = 0;
err = i2cRead(num, address, rxBuffer, size, _timeOutMillis, &rxLength);
}
#if !CONFIG_DISABLE_HAL_LOCKS
//release lock
xSemaphoreGive(lock);
#endif
return rxLength;
}
......
......@@ -27,9 +27,11 @@
#define TwoWire_h
#include <esp32-hal.h>
#if !CONFIG_DISABLE_HAL_LOCKS
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/semphr.h"
#endif
#include "Stream.h"
#ifndef I2C_BUFFER_LENGTH
......@@ -55,8 +57,10 @@ protected:
uint32_t _timeOutMillis;
bool nonStop;
#if !CONFIG_DISABLE_HAL_LOCKS
TaskHandle_t nonStopTask;
SemaphoreHandle_t lock;
#endif
public:
TwoWire(uint8_t bus_num);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册