Skip to content

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 27 commits into from
May 9, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
534978d
fixing beginTransaction() thread safety
smarq8 Mar 13, 2022
957f818
fixing beginTransaction thread safety
smarq8 Mar 13, 2022
db409b9
fixing beginTransaction()
smarq8 Mar 13, 2022
3480fa9
Merge branch 'master' into master
smarq8 Mar 13, 2022
7c2d808
fixing beginTransaction() by protecting _freq and _div by additional …
smarq8 Mar 14, 2022
5f4bc38
Update esp32-hal-spi.h
smarq8 Mar 14, 2022
70431bb
Update SPI.cpp
smarq8 Mar 14, 2022
5b2e387
add missing code
smarq8 Mar 14, 2022
8be51a4
ensure to lock params for whole transaction
smarq8 Mar 14, 2022
41be2fc
fix incorrect mutex macro at spiSimpleTransaction
smarq8 Mar 29, 2022
2d1098f
added paramLock destructor
smarq8 Mar 29, 2022
15fcc21
move create/delete to contructor/destructor
smarq8 Mar 29, 2022
28943f4
moving paramLock' delete from end() to destructor
smarq8 Mar 29, 2022
5a30b8a
.
smarq8 Mar 29, 2022
262a000
Merge branch 'master' of https://github.com/smarq8/arduino-esp32
smarq8 Mar 29, 2022
eaca28f
paramLock assign to null
smarq8 Mar 29, 2022
d0cc215
Merge branch 'master' of https://github.com/smarq8/arduino-esp32
smarq8 Mar 29, 2022
cf1363a
fix type typo and include files
smarq8 Mar 29, 2022
3e42a87
added paramLock initializer
smarq8 Mar 29, 2022
b4a6329
fix type typo
smarq8 Mar 29, 2022
22e2881
missing include for log_e
smarq8 Mar 29, 2022
64cb9c8
fix destructor declaration
smarq8 Mar 29, 2022
d157e52
missing paranthases
smarq8 Mar 29, 2022
beda8cd
missing paranthases
smarq8 Mar 29, 2022
a4950f2
Merge branch 'dev1' of https://github.com/smarq8/arduino-esp32 into dev1
smarq8 Mar 29, 2022
e91e10a
Merge branch 'master' into master
me-no-dev May 4, 2022
1b3c34a
Fix indentation
me-no-dev May 9, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions cores/esp32/esp32-hal-spi.c
Original file line number Diff line number Diff line change
Expand Up @@ -1002,13 +1002,23 @@ void spiTransferBits(spi_t * spi, uint32_t data, uint32_t * out, uint8_t bits)
#define MSB_24_SET(var, val) { uint8_t * d = (uint8_t *)&(val); (var) = d[2] | (d[1] << 8) | (d[0] << 16); }
#define MSB_16_SET(var, val) { (var) = (((val) & 0xFF00) >> 8) | (((val) & 0xFF) << 8); }
#define MSB_PIX_SET(var, val) { uint8_t * d = (uint8_t *)&(val); (var) = d[1] | (d[0] << 8) | (d[3] << 16) | (d[2] << 24); }

void spiLock(spi_t * spi){
if(!spi) {
return;
}
SPI_MUTEX_LOCK();
}
void spiUnlock(spi_t * spi){
if(!spi) {
return;
}
SPI_MUTEX_UNLOCK();
}
void spiTransaction(spi_t * spi, uint32_t clockDiv, uint8_t dataMode, uint8_t bitOrder)
{
if(!spi) {
return;
}
SPI_MUTEX_LOCK();
spi->dev->clock.val = clockDiv;
switch (dataMode) {
case SPI_MODE1:
Expand Down Expand Up @@ -1059,7 +1069,6 @@ void spiSimpleTransaction(spi_t * spi)
if(!spi) {
return;
}
SPI_MUTEX_LOCK();
}

void spiEndTransaction(spi_t * spi)
Expand Down
2 changes: 2 additions & 0 deletions cores/esp32/esp32-hal-spi.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ void spiTransferBits(spi_t * spi, uint32_t data, uint32_t * out, uint8_t bits);
/*
* New (EXPERIMENTAL) Transaction lock based API (lock once until endTransaction)
* */
void spiLock(spi_t * spi);
void spiUnlock(spi_t * spi);
void spiTransaction(spi_t * spi, uint32_t clockDiv, uint8_t dataMode, uint8_t bitOrder);
void spiSimpleTransaction(spi_t * spi);
void spiEndTransaction(spi_t * spi);
Expand Down
2 changes: 2 additions & 0 deletions libraries/SPI/src/SPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ void SPIClass::setBitOrder(uint8_t bitOrder)

void SPIClass::beginTransaction(SPISettings settings)
{
spiLock(_spi);
//check if last freq changed
uint32_t cdiv = spiGetClockDiv(_spi);
if(_freq != settings._clock || _div != cdiv) {
Expand All @@ -153,6 +154,7 @@ void SPIClass::endTransaction()
if(_inTransaction){
_inTransaction = false;
spiEndTransaction(_spi);
spiUnlock(_spi);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possible bug. spiEndTransaction() is the same as calling spiUnlock(), so you are calling unlock twice.

}
}

Expand Down