annotate Small_CPU/Src/i2c.c @ 88:3db7389d49cc kittz

revert
author Dmitry Romanov <kitt@bk.ru>
date Thu, 22 Nov 2018 12:02:55 +0300
parents e6abbef57475
children c6d284ea265b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1 #include "baseCPU2.h"
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2 #include "i2c.h"
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3 #include "scheduler.h"
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
4
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
5 /* Private typedef -----------------------------------------------------------*/
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
6 /* Private define ------------------------------------------------------------*/
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
7 /* Private macro -------------------------------------------------------------*/
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
8
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
9
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
10 // ===============================================================================
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
11 // I2C addresses - see i2c.h
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
12 ///
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
13 /// #define DEVICE_PRESSURE 0xEE (0x77) Write 0xEE Read 0xEF
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
14 /// #define DEVICE_COMPASS_HMC5883L 0x3C (0x1E) Write 0x3C Read 0x3D
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
15 /// #define DEVICE_ACCELARATOR_MMA8452Q 0x38 (0x1C) Write 0x38 Read 0x39
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
16 /// #define DEVICE_BATTERYGAUGE 0xC8 (0x64) Write 0xC8
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
17
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
18 /// #define DEVICE_COMPASS_303D 0x3C // 0x1E // x0011110 // SA0 to GND
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
19 ///
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
20 /// 0x6E 0x48 -> 0x6F 3d d1 86 3f 0x66 58
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
21 ///
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
22 // ===============================================================================
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
23 I2C_HandleTypeDef I2cHandle;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
24
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
25
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
26 /*
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
27 static void I2C_Error_Handler(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
28 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
29 while(1)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
30 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
31 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
32 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
33 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
34
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
35 HAL_StatusTypeDef I2C1_Status(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
36 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
37 return (HAL_StatusTypeDef)global.I2C_SystemStatus;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
38 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
39
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
40
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
41 GPIO_PinState HAL_I2C_Read_Data_PIN(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
42 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
43 return HAL_GPIO_ReadPin(I2Cx_SDA_GPIO_PORT,I2Cx_SDA_PIN);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
44 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
45
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
46 void HAL_I2C_Send_One_CLOCK(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
47 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
48 HAL_GPIO_WritePin(I2Cx_SCL_GPIO_PORT, I2Cx_SCL_PIN, GPIO_PIN_RESET);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
49 HAL_Delay(10);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
50 HAL_GPIO_WritePin(I2Cx_SCL_GPIO_PORT, I2Cx_SCL_PIN, GPIO_PIN_SET);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
51 HAL_Delay(10);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
52 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
53
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
54 GPIO_PinState MX_I2C1_TestAndClear(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
55 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
56 I2C_DeInit();
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
57 HAL_I2C_ManualControl_MspInit();
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
58 for(int i=0; i<9;i++)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
59 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
60 if(HAL_I2C_Read_Data_PIN() == GPIO_PIN_RESET)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
61 HAL_I2C_Send_One_CLOCK();
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
62 else
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
63 break;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
64 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
65 return HAL_I2C_Read_Data_PIN();
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
66 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
67
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
68 HAL_StatusTypeDef MX_I2C1_Init(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
69 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
70 I2cHandle.Instance = I2Cx;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
71 I2cHandle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
82
a6f0881074a4 +i2c analog noise filtering
Dmitry Romanov <kitt@bk.ru>
parents: 38
diff changeset
72 I2cHandle.Init.ClockSpeed = 400000;//400000;
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
73 I2cHandle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLED;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
74 I2cHandle.Init.DutyCycle = I2C_DUTYCYCLE_16_9;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
75 I2cHandle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLED;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
76 I2cHandle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLED;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
77 I2cHandle.Init.OwnAddress1 = 0x01;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
78
88
Dmitry Romanov <kitt@bk.ru>
parents: 84
diff changeset
79 global.dataSendToSlaveStopEval = 1;
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
80
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
81 global.I2C_SystemStatus = HAL_I2C_Init(&I2cHandle);
82
a6f0881074a4 +i2c analog noise filtering
Dmitry Romanov <kitt@bk.ru>
parents: 38
diff changeset
82 HAL_I2CEx_AnalogFilter_Config(&I2cHandle, I2C_ANALOGFILTER_ENABLED);
a6f0881074a4 +i2c analog noise filtering
Dmitry Romanov <kitt@bk.ru>
parents: 38
diff changeset
83
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
84
88
Dmitry Romanov <kitt@bk.ru>
parents: 84
diff changeset
85 global.dataSendToSlaveStopEval = 0;
Dmitry Romanov <kitt@bk.ru>
parents: 84
diff changeset
86 if(global.dataSendToSlavePending)
Dmitry Romanov <kitt@bk.ru>
parents: 84
diff changeset
87 {
Dmitry Romanov <kitt@bk.ru>
parents: 84
diff changeset
88 scheduleSpecial_Evaluate_DataSendToSlave();
Dmitry Romanov <kitt@bk.ru>
parents: 84
diff changeset
89 }
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
90 return (HAL_StatusTypeDef)global.I2C_SystemStatus;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
91 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
92
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
93
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
94 void I2C_DeInit(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
95 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
96 HAL_I2C_DeInit(&I2cHandle);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
97 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
98
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
99
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
100 uint8_t i2c_errors = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
101
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
102 void I2C_Error_count(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
103 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
104 i2c_errors++;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
105 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
106
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
107
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
108 HAL_StatusTypeDef I2C_Master_TransmitNoStop( uint16_t DevAddress, uint8_t *pData, uint16_t Size)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
109 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
110 if(global.I2C_SystemStatus != HAL_OK)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
111 return (HAL_StatusTypeDef)global.I2C_SystemStatus;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
112
88
Dmitry Romanov <kitt@bk.ru>
parents: 84
diff changeset
113 global.dataSendToSlaveStopEval = 1;
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
114
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
115 global.I2C_SystemStatus = HAL_I2C_Master_Transmit(&I2cHandle, DevAddress, pData, Size, 100 /*FIXME , 0*/);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
116 if(global.I2C_SystemStatus != HAL_OK)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
117 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
118 I2C_Error_count();
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
119 }
88
Dmitry Romanov <kitt@bk.ru>
parents: 84
diff changeset
120 global.dataSendToSlaveStopEval = 0;
Dmitry Romanov <kitt@bk.ru>
parents: 84
diff changeset
121 if(global.dataSendToSlavePending)
Dmitry Romanov <kitt@bk.ru>
parents: 84
diff changeset
122 {
Dmitry Romanov <kitt@bk.ru>
parents: 84
diff changeset
123 scheduleSpecial_Evaluate_DataSendToSlave();
Dmitry Romanov <kitt@bk.ru>
parents: 84
diff changeset
124 }
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
125 return (HAL_StatusTypeDef)global.I2C_SystemStatus;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
126 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
127
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
128
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
129 HAL_StatusTypeDef I2C_Master_Transmit( uint16_t DevAddress, uint8_t *pData, uint16_t Size)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
130 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
131 if(global.I2C_SystemStatus != HAL_OK)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
132 return (HAL_StatusTypeDef)(global.I2C_SystemStatus & 0x03);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
133
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
134
88
Dmitry Romanov <kitt@bk.ru>
parents: 84
diff changeset
135 global.dataSendToSlaveStopEval = 1;
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
136
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
137 global.I2C_SystemStatus = HAL_I2C_Master_Transmit(&I2cHandle, DevAddress, pData, Size, 100 /*FIXME , 1*/);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
138 if(global.I2C_SystemStatus != HAL_OK)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
139 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
140 I2C_Error_count();
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
141 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
142
88
Dmitry Romanov <kitt@bk.ru>
parents: 84
diff changeset
143 global.dataSendToSlaveStopEval = 0;
Dmitry Romanov <kitt@bk.ru>
parents: 84
diff changeset
144 if(global.dataSendToSlavePending)
Dmitry Romanov <kitt@bk.ru>
parents: 84
diff changeset
145 {
Dmitry Romanov <kitt@bk.ru>
parents: 84
diff changeset
146 scheduleSpecial_Evaluate_DataSendToSlave();
Dmitry Romanov <kitt@bk.ru>
parents: 84
diff changeset
147 }
Dmitry Romanov <kitt@bk.ru>
parents: 84
diff changeset
148
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
149 return (HAL_StatusTypeDef)global.I2C_SystemStatus;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
150 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
151
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
152
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
153 HAL_StatusTypeDef I2C_Master_Receive( uint16_t DevAddress, uint8_t *pData, uint16_t Size)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
154 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
155 if(global.I2C_SystemStatus != HAL_OK)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
156 return (HAL_StatusTypeDef)global.I2C_SystemStatus;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
157
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
158 uint8_t localHALstatusReturn = 0xFF;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
159
88
Dmitry Romanov <kitt@bk.ru>
parents: 84
diff changeset
160 global.dataSendToSlaveStopEval = 1;
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
161
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
162 localHALstatusReturn = HAL_I2C_Master_Receive(&I2cHandle, DevAddress, pData, Size, 100);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
163 if(localHALstatusReturn != HAL_OK)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
164 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
165 I2C_Error_count();
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
166 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
167
88
Dmitry Romanov <kitt@bk.ru>
parents: 84
diff changeset
168 global.dataSendToSlaveStopEval = 0;
Dmitry Romanov <kitt@bk.ru>
parents: 84
diff changeset
169 if(global.dataSendToSlavePending)
Dmitry Romanov <kitt@bk.ru>
parents: 84
diff changeset
170 {
Dmitry Romanov <kitt@bk.ru>
parents: 84
diff changeset
171 scheduleSpecial_Evaluate_DataSendToSlave();
Dmitry Romanov <kitt@bk.ru>
parents: 84
diff changeset
172 }
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
173 return (HAL_StatusTypeDef)localHALstatusReturn;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
174 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
175