Mercurial > public > ostc4
comparison 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 |
comparison
equal
deleted
inserted
replaced
237:ec16fd26e280 | 238:a9d798e8c11f |
---|---|
21 { | 21 { |
22 } | 22 } |
23 } | 23 } |
24 */ | 24 */ |
25 | 25 |
26 //TODO: remove this function. Why return a global variable? | |
26 HAL_StatusTypeDef I2C1_Status(void) | 27 HAL_StatusTypeDef I2C1_Status(void) |
27 { | 28 { |
28 return (HAL_StatusTypeDef)global.I2C_SystemStatus; | 29 return (HAL_StatusTypeDef)global.I2C_SystemStatus; |
29 } | 30 } |
30 | 31 |
54 break; | 55 break; |
55 } | 56 } |
56 return HAL_I2C_Read_Data_PIN(); | 57 return HAL_I2C_Read_Data_PIN(); |
57 } | 58 } |
58 | 59 |
60 //TODO: make this void. return is never used | |
59 HAL_StatusTypeDef MX_I2C1_Init(void) | 61 HAL_StatusTypeDef MX_I2C1_Init(void) |
60 { | 62 { |
61 I2cHandle.Instance = I2Cx; | 63 I2cHandle.Instance = I2Cx; |
62 I2cHandle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; | 64 I2cHandle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; |
63 I2cHandle.Init.ClockSpeed = 100000;//400000; REDUCED for compatibility with HMC5583L + MMA8452Q | 65 I2cHandle.Init.ClockSpeed = 100000;//400000; REDUCED for compatibility with HMC5583L + MMA8452Q |
95 void I2C_Error_count(void) | 97 void I2C_Error_count(void) |
96 { | 98 { |
97 i2c_errors++; | 99 i2c_errors++; |
98 } | 100 } |
99 | 101 |
100 | 102 //TODO: not used, remove |
101 HAL_StatusTypeDef I2C_Master_TransmitNoStop( uint16_t DevAddress, uint8_t *pData, uint16_t Size) | 103 HAL_StatusTypeDef I2C_Master_TransmitNoStop( uint16_t DevAddress, uint8_t *pData, uint16_t Size) |
102 { | 104 { |
103 if(global.I2C_SystemStatus != HAL_OK) | 105 if(global.I2C_SystemStatus != HAL_OK) |
104 return (HAL_StatusTypeDef)global.I2C_SystemStatus; | 106 return (HAL_StatusTypeDef)global.I2C_SystemStatus; |
105 | 107 |
121 | 123 |
122 | 124 |
123 HAL_StatusTypeDef I2C_Master_Transmit( uint16_t DevAddress, uint8_t *pData, uint16_t Size) | 125 HAL_StatusTypeDef I2C_Master_Transmit( uint16_t DevAddress, uint8_t *pData, uint16_t Size) |
124 { | 126 { |
125 if(global.I2C_SystemStatus != HAL_OK) | 127 if(global.I2C_SystemStatus != HAL_OK) |
126 return (HAL_StatusTypeDef)(global.I2C_SystemStatus & 0x03); | 128 return global.I2C_SystemStatus; |
127 | |
128 | 129 |
129 global.dataSendToSlaveStopEval = 1; | 130 global.dataSendToSlaveStopEval = 1; |
130 | 131 |
131 global.I2C_SystemStatus = HAL_I2C_Master_Transmit(&I2cHandle, DevAddress, pData, Size, 2); | 132 global.I2C_SystemStatus = HAL_I2C_Master_Transmit(&I2cHandle, DevAddress, pData, Size, 2); |
132 if(global.I2C_SystemStatus != HAL_OK) | 133 if(global.I2C_SystemStatus != HAL_OK) |
133 { | 134 { |
134 I2C_Error_count(); | 135 I2C_Error_count(); |
135 } | 136 } |
136 | 137 |
137 global.dataSendToSlaveStopEval = 0; | 138 global.dataSendToSlaveStopEval = 0; |
138 //TODO: REMOVE. | |
139 // if(global.dataSendToSlavePending) | |
140 // { | |
141 // scheduleSpecial_Evaluate_DataSendToSlave(); | |
142 // } | |
143 | 139 |
144 return (HAL_StatusTypeDef)global.I2C_SystemStatus; | 140 return (HAL_StatusTypeDef)global.I2C_SystemStatus; |
145 } | 141 } |
146 | 142 |
147 | 143 // TODO: return value never used |
148 HAL_StatusTypeDef I2C_Master_Receive( uint16_t DevAddress, uint8_t *pData, uint16_t Size) | 144 HAL_StatusTypeDef I2C_Master_Receive( uint16_t DevAddress, uint8_t *pData, uint16_t Size) |
149 { | 145 { |
150 if(global.I2C_SystemStatus != HAL_OK) | 146 if(global.I2C_SystemStatus != HAL_OK) |
151 return (HAL_StatusTypeDef)global.I2C_SystemStatus; | 147 return global.I2C_SystemStatus; |
152 | |
153 uint8_t localHALstatusReturn = 0xFF; | |
154 | 148 |
155 global.dataSendToSlaveStopEval = 1; | 149 global.dataSendToSlaveStopEval = 1; |
156 | 150 |
157 localHALstatusReturn = HAL_I2C_Master_Receive(&I2cHandle, DevAddress, pData, Size, 10); | 151 global.I2C_SystemStatus = HAL_I2C_Master_Receive(&I2cHandle, DevAddress, pData, Size, 10); |
158 if(localHALstatusReturn != HAL_OK) | 152 if(global.I2C_SystemStatus != HAL_OK) |
159 { | 153 { |
160 I2C_Error_count(); | 154 I2C_Error_count(); |
161 } | 155 } |
162 | 156 |
163 global.dataSendToSlaveStopEval = 0; | 157 global.dataSendToSlaveStopEval = 0; |
164 //TODO: REMOVE. | |
165 // if(global.dataSendToSlavePending) | |
166 // { | |
167 // scheduleSpecial_Evaluate_DataSendToSlave(); | |
168 // } | |
169 | 158 |
170 return (HAL_StatusTypeDef)localHALstatusReturn; | 159 return global.I2C_SystemStatus; |
171 } | 160 } |
172 | 161 |