Skip to content

Commit 865d4a7

Browse files
author
Max 'MaxMax' Mönikes
committed
Added changes to allow a thread safe implementation if using an RTOS
1 parent 214fa32 commit 865d4a7

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

TLE9012.cpp

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -947,6 +947,7 @@ void TLE9012::wakeUp()
947947
*/
948948
iso_uart_status_t TLE9012::readRegisterSingle(uint8_t nodeID, uint16_t regaddress, uint16_t* result)
949949
{
950+
ISOUART_LOCK();
950951
iso_uart_status_t status;
951952
uint8_t response_buffer[9];
952953

@@ -970,6 +971,7 @@ iso_uart_status_t TLE9012::readRegisterSingle(uint8_t nodeID, uint16_t regaddres
970971
if(status != isoUART_OK)
971972
{
972973
status = isoUART_TIMEOUT;
974+
ISOUART_UNLOCK();
973975
return status;
974976
}
975977

@@ -980,9 +982,10 @@ iso_uart_status_t TLE9012::readRegisterSingle(uint8_t nodeID, uint16_t regaddres
980982

981983
*result = (((uint16_t) response_buffer[6])<<8) | ((uint16_t) response_buffer[7]);
982984

983-
if(crc != response_buffer[8]);
984-
//status = isoUART_CRC_ERROR;
985+
if(crc != response_buffer[8])
986+
status = isoUART_CRC_ERROR;
985987

988+
ISOUART_UNLOCK();
986989
return status;
987990
}
988991

@@ -996,7 +999,7 @@ iso_uart_status_t TLE9012::readRegisterSingle(uint8_t nodeID, uint16_t regaddres
996999
*/
9971000
iso_uart_status_t TLE9012::writeRegisterSingle(uint8_t nodeID, uint16_t regaddress, uint16_t databuffer) //Write data to a single register
9981001
{
999-
1002+
ISOUART_LOCK();
10001003
iso_uart_status_t status;
10011004
uint8_t response_buffer[7];
10021005

@@ -1020,6 +1023,7 @@ iso_uart_status_t TLE9012::writeRegisterSingle(uint8_t nodeID, uint16_t regaddre
10201023
if(status != isoUART_OK)
10211024
{
10221025
status = isoUART_TIMEOUT;
1026+
ISOUART_UNLOCK();
10231027
return status;
10241028
}
10251029

@@ -1030,9 +1034,10 @@ iso_uart_status_t TLE9012::writeRegisterSingle(uint8_t nodeID, uint16_t regaddre
10301034
if(!crc3(response_buffer[6]))
10311035
{
10321036
status = isoUART_CRC_ERROR;
1037+
ISOUART_UNLOCK();
10331038
return status;
10341039
}
1035-
1040+
ISOUART_UNLOCK();
10361041
return status;
10371042
}
10381043

@@ -1045,6 +1050,7 @@ iso_uart_status_t TLE9012::writeRegisterSingle(uint8_t nodeID, uint16_t regaddre
10451050
*/
10461051
iso_uart_status_t TLE9012::readRegisterBroadcast(uint16_t regaddress, uint16_t* result) //Read a broadcast to all devices in the daisy chain
10471052
{
1053+
ISOUART_LOCK();
10481054
iso_uart_status_t status;
10491055
uint8_t response_buffer[N_DEVICES*5+4];
10501056

@@ -1068,6 +1074,7 @@ iso_uart_status_t TLE9012::readRegisterBroadcast(uint16_t regaddress, uint16_t*
10681074
if(status != isoUART_OK)
10691075
{
10701076
status = isoUART_TIMEOUT;
1077+
ISOUART_UNLOCK();
10711078
return status;
10721079
}
10731080

@@ -1083,7 +1090,7 @@ iso_uart_status_t TLE9012::readRegisterBroadcast(uint16_t regaddress, uint16_t*
10831090
status = isoUART_CRC_ERROR;
10841091
}
10851092

1086-
1093+
ISOUART_UNLOCK();
10871094
return status;
10881095
}
10891096

@@ -1096,7 +1103,7 @@ iso_uart_status_t TLE9012::readRegisterBroadcast(uint16_t regaddress, uint16_t*
10961103
*/
10971104
iso_uart_status_t TLE9012::writeRegisterBroadcast(uint16_t regaddress, uint16_t databuffer) //Write a register as broadcast from all devices in the chain
10981105
{
1099-
1106+
ISOUART_LOCK();
11001107
iso_uart_status_t status;
11011108
uint8_t response_buffer[7];
11021109

@@ -1119,6 +1126,7 @@ iso_uart_status_t TLE9012::writeRegisterBroadcast(uint16_t regaddress, uint16_t
11191126
if(status != isoUART_OK)
11201127
{
11211128
status = isoUART_TIMEOUT;
1129+
ISOUART_UNLOCK();
11221130
return status;
11231131
}
11241132

@@ -1130,9 +1138,11 @@ iso_uart_status_t TLE9012::writeRegisterBroadcast(uint16_t regaddress, uint16_t
11301138
if(!crc3(response_buffer[6]))
11311139
{
11321140
status = isoUART_CRC_ERROR;
1141+
ISOUART_UNLOCK();
11331142
return status;
11341143
}
11351144

1145+
ISOUART_UNLOCK();
11361146
return status;
11371147
}
11381148

@@ -1159,6 +1169,9 @@ iso_uart_status_t TLE9012::configureMultiread(multiread_cfg_t cfg) //Write a mu
11591169
*/
11601170
iso_uart_status_t TLE9012::multiRead(multread_result_t* databuffer) //Multiread command from all devices in the chain
11611171
{
1172+
ISOUART_LOCK();
1173+
1174+
ISOUART_UNLOCK();
11621175
return isoUART_OK; //Multiread is unsuported for the moment
11631176
}
11641177

TLE9012.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ SOFTWARE.
4040
#define ISOUART_TIMEOUT 100 //IsoUART Timeout in Milliseconds
4141

4242
#define SOFT_MSB_FIRST //undef in case the hardware serial port can be configured to handle MSB First in Hardware
43+
//#define THREAD_SAFE //Uncomment if Thread safety is required -> add code to mutex lock makros for your RTOS/Scheduler
4344

4445
//-----------------------------------------------------------------------------
4546
// Useful conversion makros
@@ -63,6 +64,16 @@ const float current_source_exp_lookup[] = {1.0, 4.0, 16.0, 64.0};
6364
#define WRITECOMMAND 0x80
6465
#define BROADCAST_ID 0x3F
6566

67+
68+
//Define calls to Mutex Locks here
69+
#ifdef THREAD_SAFE
70+
#define ISOUART_LOCK()
71+
#define ISOUART_UNLOCK()
72+
#else
73+
#define ISOUART_LOCK()
74+
#define ISOUART_UNLOCK()
75+
#endif
76+
6677
//-----------------------------------------------------------------------------
6778
// Typedefs start here
6879
//-----------------------------------------------------------------------------

0 commit comments

Comments
 (0)