Mercurial > public > ostc4
diff Small_CPU/Src/i2c.c @ 238:a9d798e8c11f div-fixes-5
cleanup, bugfix: do not mask I2C_SystemStatus with local variable
This commit is partly cleanup, and partly possible bugfix. Masking
the global I2C_SystemStatus with a local variable is (very) bad practice,
but more importantly, dangerous, as other code uses this I2C_SystemStatus
to base decisions on. So, this is definitely non-trivial, as it can
possibly change the flow of control. This said, its tested and seems to
have no negative effects (but also no positive, as I sort of hoped for),
so that is why I mark it cleanup as well. Constructs like this shall
be heavily documented in the code, when there is a reason to do things
like this.
Further, remove a 2nd rather useless construct. There is no reason
to & 0x03 the output of I2C_SystemStatus. This is the only location
in the entire code base where this is done, so, its not only useless
but also inconsistent and confusing the true intentions here.
Finally, littered to code with todo's that I will take care of in
next commits.
Signed-off-by: Jan Mulder <jlmulder@xs4all.nl>
author | Jan Mulder <jlmulder@xs4all.nl> |
---|---|
date | Mon, 08 Apr 2019 10:16:17 +0200 |
parents | 7e749084f347 |
children | e4207f0aaa4b |
line wrap: on
line diff
--- a/Small_CPU/Src/i2c.c Sat Apr 06 20:35:21 2019 +0200 +++ b/Small_CPU/Src/i2c.c Mon Apr 08 10:16:17 2019 +0200 @@ -23,6 +23,7 @@ } */ +//TODO: remove this function. Why return a global variable? HAL_StatusTypeDef I2C1_Status(void) { return (HAL_StatusTypeDef)global.I2C_SystemStatus; @@ -56,6 +57,7 @@ return HAL_I2C_Read_Data_PIN(); } +//TODO: make this void. return is never used HAL_StatusTypeDef MX_I2C1_Init(void) { I2cHandle.Instance = I2Cx; @@ -97,7 +99,7 @@ i2c_errors++; } - +//TODO: not used, remove HAL_StatusTypeDef I2C_Master_TransmitNoStop( uint16_t DevAddress, uint8_t *pData, uint16_t Size) { if(global.I2C_SystemStatus != HAL_OK) @@ -123,8 +125,7 @@ HAL_StatusTypeDef I2C_Master_Transmit( uint16_t DevAddress, uint8_t *pData, uint16_t Size) { if(global.I2C_SystemStatus != HAL_OK) - return (HAL_StatusTypeDef)(global.I2C_SystemStatus & 0x03); - + return global.I2C_SystemStatus; global.dataSendToSlaveStopEval = 1; @@ -135,38 +136,26 @@ } global.dataSendToSlaveStopEval = 0; - //TODO: REMOVE. -// if(global.dataSendToSlavePending) -// { -// scheduleSpecial_Evaluate_DataSendToSlave(); -// } return (HAL_StatusTypeDef)global.I2C_SystemStatus; } - +// TODO: return value never used HAL_StatusTypeDef I2C_Master_Receive( uint16_t DevAddress, uint8_t *pData, uint16_t Size) { if(global.I2C_SystemStatus != HAL_OK) - return (HAL_StatusTypeDef)global.I2C_SystemStatus; - - uint8_t localHALstatusReturn = 0xFF; + return global.I2C_SystemStatus; global.dataSendToSlaveStopEval = 1; - localHALstatusReturn = HAL_I2C_Master_Receive(&I2cHandle, DevAddress, pData, Size, 10); - if(localHALstatusReturn != HAL_OK) + global.I2C_SystemStatus = HAL_I2C_Master_Receive(&I2cHandle, DevAddress, pData, Size, 10); + if(global.I2C_SystemStatus != HAL_OK) { I2C_Error_count(); } global.dataSendToSlaveStopEval = 0; - //TODO: REMOVE. -// if(global.dataSendToSlavePending) -// { -// scheduleSpecial_Evaluate_DataSendToSlave(); -// } - return (HAL_StatusTypeDef)localHALstatusReturn; + return global.I2C_SystemStatus; }