annotate Small_CPU/Src/i2c.c @ 101:83feba2ad1c3 kittz

globals
author Dmitry Romanov <kitt@bk.ru>
date Tue, 27 Nov 2018 13:18:59 +0300
parents b364c75005bb
children 22a1094545f3
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;
100
b364c75005bb Stable.fix.
Dmitry Romanov <kitt@bk.ru>
parents: 94
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;
100
b364c75005bb Stable.fix.
Dmitry Romanov <kitt@bk.ru>
parents: 94
diff changeset
74 I2cHandle.Init.DutyCycle = I2C_DUTYCYCLE_16_9;
38
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);
100
b364c75005bb Stable.fix.
Dmitry Romanov <kitt@bk.ru>
parents: 94
diff changeset
83 HAL_I2CEx_ConfigDigitalFilter(&I2cHandle,0x0F);
b364c75005bb Stable.fix.
Dmitry Romanov <kitt@bk.ru>
parents: 94
diff changeset
84
82
a6f0881074a4 +i2c analog noise filtering
Dmitry Romanov <kitt@bk.ru>
parents: 38
diff changeset
85
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
86
88
Dmitry Romanov <kitt@bk.ru>
parents: 84
diff changeset
87 global.dataSendToSlaveStopEval = 0;
100
b364c75005bb Stable.fix.
Dmitry Romanov <kitt@bk.ru>
parents: 94
diff changeset
88 if(global.dataSendToSlavePending)
b364c75005bb Stable.fix.
Dmitry Romanov <kitt@bk.ru>
parents: 94
diff changeset
89 {
b364c75005bb Stable.fix.
Dmitry Romanov <kitt@bk.ru>
parents: 94
diff changeset
90 scheduleSpecial_Evaluate_DataSendToSlave();
b364c75005bb Stable.fix.
Dmitry Romanov <kitt@bk.ru>
parents: 94
diff changeset
91 }
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
92 return (HAL_StatusTypeDef)global.I2C_SystemStatus;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
93 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
94
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
95
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
96 void I2C_DeInit(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
97 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
98 HAL_I2C_DeInit(&I2cHandle);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
99 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
100
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
101
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
102 uint8_t i2c_errors = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
103
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
104 void I2C_Error_count(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
105 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
106 i2c_errors++;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
107 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
108
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
109
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
110 HAL_StatusTypeDef I2C_Master_TransmitNoStop( uint16_t DevAddress, uint8_t *pData, uint16_t Size)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
111 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
112 if(global.I2C_SystemStatus != HAL_OK)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
113 return (HAL_StatusTypeDef)global.I2C_SystemStatus;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
114
88
Dmitry Romanov <kitt@bk.ru>
parents: 84
diff changeset
115 global.dataSendToSlaveStopEval = 1;
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
116
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
117 global.I2C_SystemStatus = HAL_I2C_Master_Transmit(&I2cHandle, DevAddress, pData, Size, 100 /*FIXME , 0*/);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
118 if(global.I2C_SystemStatus != HAL_OK)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
119 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
120 I2C_Error_count();
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
121 }
88
Dmitry Romanov <kitt@bk.ru>
parents: 84
diff changeset
122 global.dataSendToSlaveStopEval = 0;
100
b364c75005bb Stable.fix.
Dmitry Romanov <kitt@bk.ru>
parents: 94
diff changeset
123 // if(global.dataSendToSlavePending)
b364c75005bb Stable.fix.
Dmitry Romanov <kitt@bk.ru>
parents: 94
diff changeset
124 // {
b364c75005bb Stable.fix.
Dmitry Romanov <kitt@bk.ru>
parents: 94
diff changeset
125 // scheduleSpecial_Evaluate_DataSendToSlave();
b364c75005bb Stable.fix.
Dmitry Romanov <kitt@bk.ru>
parents: 94
diff changeset
126 // }
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
127 return (HAL_StatusTypeDef)global.I2C_SystemStatus;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
128 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
129
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
130
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
131 HAL_StatusTypeDef I2C_Master_Transmit( uint16_t DevAddress, uint8_t *pData, uint16_t Size)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
132 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
133 if(global.I2C_SystemStatus != HAL_OK)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
134 return (HAL_StatusTypeDef)(global.I2C_SystemStatus & 0x03);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
135
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
136
88
Dmitry Romanov <kitt@bk.ru>
parents: 84
diff changeset
137 global.dataSendToSlaveStopEval = 1;
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
138
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
139 global.I2C_SystemStatus = HAL_I2C_Master_Transmit(&I2cHandle, DevAddress, pData, Size, 100 /*FIXME , 1*/);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
140 if(global.I2C_SystemStatus != HAL_OK)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
141 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
142 I2C_Error_count();
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
143 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
144
88
Dmitry Romanov <kitt@bk.ru>
parents: 84
diff changeset
145 global.dataSendToSlaveStopEval = 0;
100
b364c75005bb Stable.fix.
Dmitry Romanov <kitt@bk.ru>
parents: 94
diff changeset
146 // if(global.dataSendToSlavePending)
b364c75005bb Stable.fix.
Dmitry Romanov <kitt@bk.ru>
parents: 94
diff changeset
147 // {
b364c75005bb Stable.fix.
Dmitry Romanov <kitt@bk.ru>
parents: 94
diff changeset
148 // scheduleSpecial_Evaluate_DataSendToSlave();
b364c75005bb Stable.fix.
Dmitry Romanov <kitt@bk.ru>
parents: 94
diff changeset
149 // }
88
Dmitry Romanov <kitt@bk.ru>
parents: 84
diff changeset
150
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
151 return (HAL_StatusTypeDef)global.I2C_SystemStatus;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
152 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
153
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
154
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
155 HAL_StatusTypeDef I2C_Master_Receive( uint16_t DevAddress, uint8_t *pData, uint16_t Size)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
156 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
157 if(global.I2C_SystemStatus != HAL_OK)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
158 return (HAL_StatusTypeDef)global.I2C_SystemStatus;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
159
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
160 uint8_t localHALstatusReturn = 0xFF;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
161
88
Dmitry Romanov <kitt@bk.ru>
parents: 84
diff changeset
162 global.dataSendToSlaveStopEval = 1;
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
163
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
164 localHALstatusReturn = HAL_I2C_Master_Receive(&I2cHandle, DevAddress, pData, Size, 100);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
165 if(localHALstatusReturn != HAL_OK)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
166 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
167 I2C_Error_count();
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
168 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
169
88
Dmitry Romanov <kitt@bk.ru>
parents: 84
diff changeset
170 global.dataSendToSlaveStopEval = 0;
94
c6d284ea265b reduce i2c speed and dutycycle for compass stability
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
171 // if(global.dataSendToSlavePending)
c6d284ea265b reduce i2c speed and dutycycle for compass stability
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
172 // {
100
b364c75005bb Stable.fix.
Dmitry Romanov <kitt@bk.ru>
parents: 94
diff changeset
173 // scheduleSpecial_Evaluate_DataSendToSlave();
94
c6d284ea265b reduce i2c speed and dutycycle for compass stability
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
174 // }
100
b364c75005bb Stable.fix.
Dmitry Romanov <kitt@bk.ru>
parents: 94
diff changeset
175
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
176 return (HAL_StatusTypeDef)localHALstatusReturn;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
177 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
178