-
Notifications
You must be signed in to change notification settings - Fork 7.6k
fixing beginTransaction() thread safety #6425
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 22 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
534978d
fixing beginTransaction() thread safety
smarq8 957f818
fixing beginTransaction thread safety
smarq8 db409b9
fixing beginTransaction()
smarq8 3480fa9
Merge branch 'master' into master
smarq8 7c2d808
fixing beginTransaction() by protecting _freq and _div by additional …
smarq8 5f4bc38
Update esp32-hal-spi.h
smarq8 70431bb
Update SPI.cpp
smarq8 5b2e387
add missing code
smarq8 8be51a4
ensure to lock params for whole transaction
smarq8 41be2fc
fix incorrect mutex macro at spiSimpleTransaction
smarq8 2d1098f
added paramLock destructor
smarq8 15fcc21
move create/delete to contructor/destructor
smarq8 28943f4
moving paramLock' delete from end() to destructor
smarq8 5a30b8a
.
smarq8 262a000
Merge branch 'master' of https://github.com/smarq8/arduino-esp32
smarq8 eaca28f
paramLock assign to null
smarq8 d0cc215
Merge branch 'master' of https://github.com/smarq8/arduino-esp32
smarq8 cf1363a
fix type typo and include files
smarq8 3e42a87
added paramLock initializer
smarq8 b4a6329
fix type typo
smarq8 22e2881
missing include for log_e
smarq8 64cb9c8
fix destructor declaration
smarq8 d157e52
missing paranthases
smarq8 beda8cd
missing paranthases
smarq8 a4950f2
Merge branch 'dev1' of https://github.com/smarq8/arduino-esp32 into dev1
smarq8 e91e10a
Merge branch 'master' into master
me-no-dev 1b3c34a
Fix indentation
me-no-dev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,15 @@ | |
*/ | ||
|
||
#include "SPI.h" | ||
#include "esp32-hal-log.h" | ||
|
||
#if !CONFIG_DISABLE_HAL_LOCKS | ||
#define SPI_PARAM_LOCK() do {} while (xSemaphoreTake(paramLock, portMAX_DELAY) != pdPASS) | ||
#define SPI_PARAM_UNLOCK() xSemaphoreGive(paramLock) | ||
#else | ||
#define SPI_PARAM_LOCK() | ||
#define SPI_PARAM_UNLOCK() | ||
#endif | ||
|
||
SPIClass::SPIClass(uint8_t spi_bus) | ||
:_spi_num(spi_bus) | ||
|
@@ -32,7 +41,30 @@ SPIClass::SPIClass(uint8_t spi_bus) | |
,_div(0) | ||
,_freq(1000000) | ||
,_inTransaction(false) | ||
{} | ||
#if !CONFIG_DISABLE_HAL_LOCKS | ||
,paramLock(NULL) | ||
#endif | ||
{ | ||
#if !CONFIG_DISABLE_HAL_LOCKS | ||
if(paramLock==NULL){ | ||
paramLock = xSemaphoreCreateMutex(); | ||
if(paramLock==NULL){ | ||
log_e("xSemaphoreCreateMutex failed"); | ||
return; | ||
} | ||
#endif | ||
} | ||
|
||
SPIClass::~SPIClass() | ||
{ | ||
end(); | ||
#if !CONFIG_DISABLE_HAL_LOCKS | ||
if(paramLock!=NULL){ | ||
vSemaphoreDelete(paramLock); | ||
paramLock = NULL; | ||
} | ||
#endif | ||
} | ||
|
||
void SPIClass::begin(int8_t sck, int8_t miso, int8_t mosi, int8_t ss) | ||
{ | ||
|
@@ -106,19 +138,23 @@ void SPIClass::setHwCs(bool use) | |
|
||
void SPIClass::setFrequency(uint32_t freq) | ||
{ | ||
//check if last freq changed | ||
SPI_PARAM_LOCK(); | ||
//check if last freq changed | ||
uint32_t cdiv = spiGetClockDiv(_spi); | ||
if(_freq != freq || _div != cdiv) { | ||
_freq = freq; | ||
_div = spiFrequencyToClockDiv(_freq); | ||
spiSetClockDiv(_spi, _div); | ||
} | ||
SPI_PARAM_UNLOCK(); | ||
} | ||
|
||
void SPIClass::setClockDivider(uint32_t clockDiv) | ||
{ | ||
_div = clockDiv; | ||
SPI_PARAM_LOCK(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Protects "_div" from cross-thread modification. OK |
||
_div = clockDiv; | ||
spiSetClockDiv(_spi, _div); | ||
SPI_PARAM_UNLOCK(); | ||
} | ||
|
||
uint32_t SPIClass::getClockDivider() | ||
|
@@ -138,7 +174,8 @@ void SPIClass::setBitOrder(uint8_t bitOrder) | |
|
||
void SPIClass::beginTransaction(SPISettings settings) | ||
{ | ||
//check if last freq changed | ||
SPI_PARAM_LOCK(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Protects "_div" from cross-thread modification. OK |
||
//check if last freq changed | ||
uint32_t cdiv = spiGetClockDiv(_spi); | ||
if(_freq != settings._clock || _div != cdiv) { | ||
_freq = settings._clock; | ||
|
@@ -153,6 +190,7 @@ void SPIClass::endTransaction() | |
if(_inTransaction){ | ||
_inTransaction = false; | ||
spiEndTransaction(_spi); | ||
SPI_PARAM_UNLOCK(); // <-- Im not sure should it be here or right after spiTransaction() | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Protects "_div" from cross-thread modification. OK