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

Allow disabling of the HAL locks

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