Mercurial > public > ostc4
comparison Small_CPU/Src/i2c.c @ 244:c20c73b0d034
Merged in janlmulder/ostc4/div-fixes-5 (pull request #15)
Improvement SPI stability/recoverability and cleanup, and trivial bugfixes
author | heinrichsweikamp <bitbucket@heinrichsweikamp.com> |
---|---|
date | Tue, 09 Apr 2019 08:52:44 +0000 |
parents | 2b9775f71e30 |
children | 4fe5400567e7 |
comparison
equal
deleted
inserted
replaced
236:ad6ddc4aabcd | 244:c20c73b0d034 |
---|---|
20 while(1) | 20 while(1) |
21 { | 21 { |
22 } | 22 } |
23 } | 23 } |
24 */ | 24 */ |
25 | |
26 HAL_StatusTypeDef I2C1_Status(void) | |
27 { | |
28 return (HAL_StatusTypeDef)global.I2C_SystemStatus; | |
29 } | |
30 | |
31 | 25 |
32 GPIO_PinState HAL_I2C_Read_Data_PIN(void) | 26 GPIO_PinState HAL_I2C_Read_Data_PIN(void) |
33 { | 27 { |
34 return HAL_GPIO_ReadPin(I2Cx_SDA_GPIO_PORT,I2Cx_SDA_PIN); | 28 return HAL_GPIO_ReadPin(I2Cx_SDA_GPIO_PORT,I2Cx_SDA_PIN); |
35 } | 29 } |
64 I2cHandle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLED; | 58 I2cHandle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLED; |
65 I2cHandle.Init.DutyCycle = I2C_DUTYCYCLE_2; | 59 I2cHandle.Init.DutyCycle = I2C_DUTYCYCLE_2; |
66 I2cHandle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLED; | 60 I2cHandle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLED; |
67 I2cHandle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLED; | 61 I2cHandle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLED; |
68 I2cHandle.Init.OwnAddress1 = 0x01; | 62 I2cHandle.Init.OwnAddress1 = 0x01; |
69 | |
70 global.dataSendToSlaveStopEval = 1; | |
71 | 63 |
72 global.I2C_SystemStatus = HAL_I2C_Init(&I2cHandle); | 64 global.I2C_SystemStatus = HAL_I2C_Init(&I2cHandle); |
73 HAL_I2CEx_AnalogFilter_Config(&I2cHandle, I2C_ANALOGFILTER_ENABLED); | 65 HAL_I2CEx_AnalogFilter_Config(&I2cHandle, I2C_ANALOGFILTER_ENABLED); |
74 HAL_I2CEx_ConfigDigitalFilter(&I2cHandle,0x0F); | 66 HAL_I2CEx_ConfigDigitalFilter(&I2cHandle,0x0F); |
75 | 67 |
76 | |
77 | |
78 global.dataSendToSlaveStopEval = 0; | |
79 if(global.dataSendToSlavePending) | 68 if(global.dataSendToSlavePending) |
80 { | 69 { |
81 scheduleSpecial_Evaluate_DataSendToSlave(); | 70 scheduleSpecial_Evaluate_DataSendToSlave(); |
82 } | 71 } |
83 return (HAL_StatusTypeDef)global.I2C_SystemStatus; | 72 return global.I2C_SystemStatus; |
84 } | 73 } |
85 | 74 |
86 | 75 |
87 void I2C_DeInit(void) | 76 void I2C_DeInit(void) |
88 { | 77 { |
89 HAL_I2C_DeInit(&I2cHandle); | 78 HAL_I2C_DeInit(&I2cHandle); |
90 } | 79 } |
91 | 80 |
92 | 81 static uint8_t i2c_errors = 0; |
93 uint8_t i2c_errors = 0; | |
94 | 82 |
95 void I2C_Error_count(void) | 83 void I2C_Error_count(void) |
96 { | 84 { |
97 i2c_errors++; | 85 i2c_errors++; |
98 } | 86 } |
99 | 87 |
100 | |
101 HAL_StatusTypeDef I2C_Master_TransmitNoStop( uint16_t DevAddress, uint8_t *pData, uint16_t Size) | |
102 { | |
103 if(global.I2C_SystemStatus != HAL_OK) | |
104 return (HAL_StatusTypeDef)global.I2C_SystemStatus; | |
105 | |
106 global.dataSendToSlaveStopEval = 1; | |
107 | |
108 global.I2C_SystemStatus = HAL_I2C_Master_Transmit(&I2cHandle, DevAddress, pData, Size, 0); | |
109 if(global.I2C_SystemStatus != HAL_OK) | |
110 { | |
111 I2C_Error_count(); | |
112 } | |
113 global.dataSendToSlaveStopEval = 0; | |
114 //TODO: REMOVE. | |
115 // if(global.dataSendToSlavePending) | |
116 // { | |
117 // scheduleSpecial_Evaluate_DataSendToSlave(); | |
118 // } | |
119 return (HAL_StatusTypeDef)global.I2C_SystemStatus; | |
120 } | |
121 | |
122 | |
123 HAL_StatusTypeDef I2C_Master_Transmit( uint16_t DevAddress, uint8_t *pData, uint16_t Size) | 88 HAL_StatusTypeDef I2C_Master_Transmit( uint16_t DevAddress, uint8_t *pData, uint16_t Size) |
124 { | 89 { |
125 if(global.I2C_SystemStatus != HAL_OK) | 90 if(global.I2C_SystemStatus != HAL_OK) |
126 return (HAL_StatusTypeDef)(global.I2C_SystemStatus & 0x03); | 91 return global.I2C_SystemStatus; |
127 | |
128 | |
129 global.dataSendToSlaveStopEval = 1; | |
130 | 92 |
131 global.I2C_SystemStatus = HAL_I2C_Master_Transmit(&I2cHandle, DevAddress, pData, Size, 2); | 93 global.I2C_SystemStatus = HAL_I2C_Master_Transmit(&I2cHandle, DevAddress, pData, Size, 2); |
132 if(global.I2C_SystemStatus != HAL_OK) | 94 if(global.I2C_SystemStatus != HAL_OK) |
133 { | 95 { |
134 I2C_Error_count(); | 96 I2C_Error_count(); |
135 } | 97 } |
136 | |
137 global.dataSendToSlaveStopEval = 0; | |
138 //TODO: REMOVE. | |
139 // if(global.dataSendToSlavePending) | |
140 // { | |
141 // scheduleSpecial_Evaluate_DataSendToSlave(); | |
142 // } | |
143 | 98 |
144 return (HAL_StatusTypeDef)global.I2C_SystemStatus; | 99 return global.I2C_SystemStatus; |
145 } | 100 } |
146 | |
147 | 101 |
148 HAL_StatusTypeDef I2C_Master_Receive( uint16_t DevAddress, uint8_t *pData, uint16_t Size) | 102 HAL_StatusTypeDef I2C_Master_Receive( uint16_t DevAddress, uint8_t *pData, uint16_t Size) |
149 { | 103 { |
150 if(global.I2C_SystemStatus != HAL_OK) | 104 if(global.I2C_SystemStatus != HAL_OK) |
151 return (HAL_StatusTypeDef)global.I2C_SystemStatus; | 105 return global.I2C_SystemStatus; |
152 | 106 |
153 uint8_t localHALstatusReturn = 0xFF; | 107 global.I2C_SystemStatus = HAL_I2C_Master_Receive(&I2cHandle, DevAddress, pData, Size, 10); |
154 | 108 if(global.I2C_SystemStatus != HAL_OK) |
155 global.dataSendToSlaveStopEval = 1; | |
156 | |
157 localHALstatusReturn = HAL_I2C_Master_Receive(&I2cHandle, DevAddress, pData, Size, 10); | |
158 if(localHALstatusReturn != HAL_OK) | |
159 { | 109 { |
160 I2C_Error_count(); | 110 I2C_Error_count(); |
161 } | 111 } |
162 | 112 |
163 global.dataSendToSlaveStopEval = 0; | 113 return global.I2C_SystemStatus; |
164 //TODO: REMOVE. | |
165 // if(global.dataSendToSlavePending) | |
166 // { | |
167 // scheduleSpecial_Evaluate_DataSendToSlave(); | |
168 // } | |
169 | |
170 return (HAL_StatusTypeDef)localHALstatusReturn; | |
171 } | 114 } |
172 |