Mercurial > public > ostc4
annotate 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 |
rev | line source |
---|---|
38 | 1 #include "baseCPU2.h" |
2 #include "i2c.h" | |
3 #include "scheduler.h" | |
4 | |
5 /* Private typedef -----------------------------------------------------------*/ | |
6 /* Private define ------------------------------------------------------------*/ | |
7 /* Private macro -------------------------------------------------------------*/ | |
8 | |
9 | |
10 // =============================================================================== | |
11 // I2C addresses - see i2c.h | |
165
e9cce686fe41
Minor: Some documentation for new hardware
heinrichsweikamp
parents:
104
diff
changeset
|
12 // =============================================================================== |
38 | 13 |
14 I2C_HandleTypeDef I2cHandle; | |
15 | |
16 | |
17 /* | |
18 static void I2C_Error_Handler(void) | |
19 { | |
20 while(1) | |
21 { | |
22 } | |
23 } | |
24 */ | |
25 | |
238
a9d798e8c11f
cleanup, bugfix: do not mask I2C_SystemStatus with local variable
Jan Mulder <jlmulder@xs4all.nl>
parents:
182
diff
changeset
|
26 //TODO: remove this function. Why return a global variable? |
38 | 27 HAL_StatusTypeDef I2C1_Status(void) |
28 { | |
29 return (HAL_StatusTypeDef)global.I2C_SystemStatus; | |
30 } | |
31 | |
32 | |
33 GPIO_PinState HAL_I2C_Read_Data_PIN(void) | |
34 { | |
35 return HAL_GPIO_ReadPin(I2Cx_SDA_GPIO_PORT,I2Cx_SDA_PIN); | |
36 } | |
37 | |
38 void HAL_I2C_Send_One_CLOCK(void) | |
39 { | |
40 HAL_GPIO_WritePin(I2Cx_SCL_GPIO_PORT, I2Cx_SCL_PIN, GPIO_PIN_RESET); | |
41 HAL_Delay(10); | |
42 HAL_GPIO_WritePin(I2Cx_SCL_GPIO_PORT, I2Cx_SCL_PIN, GPIO_PIN_SET); | |
43 HAL_Delay(10); | |
44 } | |
45 | |
46 GPIO_PinState MX_I2C1_TestAndClear(void) | |
47 { | |
48 I2C_DeInit(); | |
49 HAL_I2C_ManualControl_MspInit(); | |
50 for(int i=0; i<9;i++) | |
51 { | |
52 if(HAL_I2C_Read_Data_PIN() == GPIO_PIN_RESET) | |
53 HAL_I2C_Send_One_CLOCK(); | |
54 else | |
55 break; | |
56 } | |
57 return HAL_I2C_Read_Data_PIN(); | |
58 } | |
59 | |
238
a9d798e8c11f
cleanup, bugfix: do not mask I2C_SystemStatus with local variable
Jan Mulder <jlmulder@xs4all.nl>
parents:
182
diff
changeset
|
60 //TODO: make this void. return is never used |
38 | 61 HAL_StatusTypeDef MX_I2C1_Init(void) |
62 { | |
63 I2cHandle.Instance = I2Cx; | |
64 I2cHandle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; | |
104 | 65 I2cHandle.Init.ClockSpeed = 100000;//400000; REDUCED for compatibility with HMC5583L + MMA8452Q |
38 | 66 I2cHandle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLED; |
104 | 67 I2cHandle.Init.DutyCycle = I2C_DUTYCYCLE_2; |
38 | 68 I2cHandle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLED; |
69 I2cHandle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLED; | |
70 I2cHandle.Init.OwnAddress1 = 0x01; | |
71 | |
88 | 72 global.dataSendToSlaveStopEval = 1; |
38 | 73 |
74 global.I2C_SystemStatus = HAL_I2C_Init(&I2cHandle); | |
82 | 75 HAL_I2CEx_AnalogFilter_Config(&I2cHandle, I2C_ANALOGFILTER_ENABLED); |
100 | 76 HAL_I2CEx_ConfigDigitalFilter(&I2cHandle,0x0F); |
77 | |
82 | 78 |
38 | 79 |
88 | 80 global.dataSendToSlaveStopEval = 0; |
100 | 81 if(global.dataSendToSlavePending) |
82 { | |
83 scheduleSpecial_Evaluate_DataSendToSlave(); | |
84 } | |
38 | 85 return (HAL_StatusTypeDef)global.I2C_SystemStatus; |
86 } | |
87 | |
88 | |
89 void I2C_DeInit(void) | |
90 { | |
91 HAL_I2C_DeInit(&I2cHandle); | |
92 } | |
93 | |
94 | |
95 uint8_t i2c_errors = 0; | |
96 | |
97 void I2C_Error_count(void) | |
98 { | |
99 i2c_errors++; | |
100 } | |
101 | |
238
a9d798e8c11f
cleanup, bugfix: do not mask I2C_SystemStatus with local variable
Jan Mulder <jlmulder@xs4all.nl>
parents:
182
diff
changeset
|
102 //TODO: not used, remove |
38 | 103 HAL_StatusTypeDef I2C_Master_TransmitNoStop( uint16_t DevAddress, uint8_t *pData, uint16_t Size) |
104 { | |
105 if(global.I2C_SystemStatus != HAL_OK) | |
106 return (HAL_StatusTypeDef)global.I2C_SystemStatus; | |
107 | |
88 | 108 global.dataSendToSlaveStopEval = 1; |
38 | 109 |
182 | 110 global.I2C_SystemStatus = HAL_I2C_Master_Transmit(&I2cHandle, DevAddress, pData, Size, 0); |
38 | 111 if(global.I2C_SystemStatus != HAL_OK) |
112 { | |
113 I2C_Error_count(); | |
114 } | |
88 | 115 global.dataSendToSlaveStopEval = 0; |
104 | 116 //TODO: REMOVE. |
100 | 117 // if(global.dataSendToSlavePending) |
118 // { | |
119 // scheduleSpecial_Evaluate_DataSendToSlave(); | |
120 // } | |
38 | 121 return (HAL_StatusTypeDef)global.I2C_SystemStatus; |
122 } | |
123 | |
124 | |
125 HAL_StatusTypeDef I2C_Master_Transmit( uint16_t DevAddress, uint8_t *pData, uint16_t Size) | |
126 { | |
127 if(global.I2C_SystemStatus != HAL_OK) | |
238
a9d798e8c11f
cleanup, bugfix: do not mask I2C_SystemStatus with local variable
Jan Mulder <jlmulder@xs4all.nl>
parents:
182
diff
changeset
|
128 return global.I2C_SystemStatus; |
38 | 129 |
88 | 130 global.dataSendToSlaveStopEval = 1; |
38 | 131 |
182 | 132 global.I2C_SystemStatus = HAL_I2C_Master_Transmit(&I2cHandle, DevAddress, pData, Size, 2); |
38 | 133 if(global.I2C_SystemStatus != HAL_OK) |
134 { | |
135 I2C_Error_count(); | |
136 } | |
137 | |
88 | 138 global.dataSendToSlaveStopEval = 0; |
139 | |
38 | 140 return (HAL_StatusTypeDef)global.I2C_SystemStatus; |
141 } | |
142 | |
238
a9d798e8c11f
cleanup, bugfix: do not mask I2C_SystemStatus with local variable
Jan Mulder <jlmulder@xs4all.nl>
parents:
182
diff
changeset
|
143 // TODO: return value never used |
38 | 144 HAL_StatusTypeDef I2C_Master_Receive( uint16_t DevAddress, uint8_t *pData, uint16_t Size) |
145 { | |
146 if(global.I2C_SystemStatus != HAL_OK) | |
238
a9d798e8c11f
cleanup, bugfix: do not mask I2C_SystemStatus with local variable
Jan Mulder <jlmulder@xs4all.nl>
parents:
182
diff
changeset
|
147 return global.I2C_SystemStatus; |
38 | 148 |
88 | 149 global.dataSendToSlaveStopEval = 1; |
38 | 150 |
238
a9d798e8c11f
cleanup, bugfix: do not mask I2C_SystemStatus with local variable
Jan Mulder <jlmulder@xs4all.nl>
parents:
182
diff
changeset
|
151 global.I2C_SystemStatus = HAL_I2C_Master_Receive(&I2cHandle, DevAddress, pData, Size, 10); |
a9d798e8c11f
cleanup, bugfix: do not mask I2C_SystemStatus with local variable
Jan Mulder <jlmulder@xs4all.nl>
parents:
182
diff
changeset
|
152 if(global.I2C_SystemStatus != HAL_OK) |
38 | 153 { |
154 I2C_Error_count(); | |
155 } | |
156 | |
88 | 157 global.dataSendToSlaveStopEval = 0; |
100 | 158 |
238
a9d798e8c11f
cleanup, bugfix: do not mask I2C_SystemStatus with local variable
Jan Mulder <jlmulder@xs4all.nl>
parents:
182
diff
changeset
|
159 return global.I2C_SystemStatus; |
38 | 160 } |
161 |