annotate Small_CPU/Src/i2c.c @ 165:e9cce686fe41

Minor: Some documentation for new hardware
author heinrichsweikamp
date Fri, 08 Mar 2019 10:31:32 +0100
parents 22a1094545f3
children 7e749084f347
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
165
e9cce686fe41 Minor: Some documentation for new hardware
heinrichsweikamp
parents: 104
diff changeset
12 // ===============================================================================
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
13
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
14 I2C_HandleTypeDef I2cHandle;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
15
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
16
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
17 /*
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
18 static void I2C_Error_Handler(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
19 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
20 while(1)
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 }
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 HAL_StatusTypeDef I2C1_Status(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
27 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
28 return (HAL_StatusTypeDef)global.I2C_SystemStatus;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
29 }
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 GPIO_PinState HAL_I2C_Read_Data_PIN(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
33 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
34 return HAL_GPIO_ReadPin(I2Cx_SDA_GPIO_PORT,I2Cx_SDA_PIN);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
35 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
36
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
37 void HAL_I2C_Send_One_CLOCK(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
38 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
39 HAL_GPIO_WritePin(I2Cx_SCL_GPIO_PORT, I2Cx_SCL_PIN, GPIO_PIN_RESET);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
40 HAL_Delay(10);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
41 HAL_GPIO_WritePin(I2Cx_SCL_GPIO_PORT, I2Cx_SCL_PIN, GPIO_PIN_SET);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
42 HAL_Delay(10);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
43 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
44
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
45 GPIO_PinState MX_I2C1_TestAndClear(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
46 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
47 I2C_DeInit();
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
48 HAL_I2C_ManualControl_MspInit();
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
49 for(int i=0; i<9;i++)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
50 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
51 if(HAL_I2C_Read_Data_PIN() == GPIO_PIN_RESET)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
52 HAL_I2C_Send_One_CLOCK();
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
53 else
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
54 break;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
55 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
56 return HAL_I2C_Read_Data_PIN();
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
57 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
58
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
59 HAL_StatusTypeDef MX_I2C1_Init(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
60 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
61 I2cHandle.Instance = I2Cx;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
62 I2cHandle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
104
22a1094545f3 Tested and alive.
Dmitry Romanov <kitt@bk.ru>
parents: 100
diff changeset
63 I2cHandle.Init.ClockSpeed = 100000;//400000; REDUCED for compatibility with HMC5583L + MMA8452Q
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
64 I2cHandle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLED;
104
22a1094545f3 Tested and alive.
Dmitry Romanov <kitt@bk.ru>
parents: 100
diff changeset
65 I2cHandle.Init.DutyCycle = I2C_DUTYCYCLE_2;
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
66 I2cHandle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLED;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
67 I2cHandle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLED;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
68 I2cHandle.Init.OwnAddress1 = 0x01;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
69
88
Dmitry Romanov <kitt@bk.ru>
parents: 84
diff changeset
70 global.dataSendToSlaveStopEval = 1;
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
71
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
72 global.I2C_SystemStatus = HAL_I2C_Init(&I2cHandle);
82
a6f0881074a4 +i2c analog noise filtering
Dmitry Romanov <kitt@bk.ru>
parents: 38
diff changeset
73 HAL_I2CEx_AnalogFilter_Config(&I2cHandle, I2C_ANALOGFILTER_ENABLED);
100
b364c75005bb Stable.fix.
Dmitry Romanov <kitt@bk.ru>
parents: 94
diff changeset
74 HAL_I2CEx_ConfigDigitalFilter(&I2cHandle,0x0F);
b364c75005bb Stable.fix.
Dmitry Romanov <kitt@bk.ru>
parents: 94
diff changeset
75
82
a6f0881074a4 +i2c analog noise filtering
Dmitry Romanov <kitt@bk.ru>
parents: 38
diff changeset
76
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
77
88
Dmitry Romanov <kitt@bk.ru>
parents: 84
diff changeset
78 global.dataSendToSlaveStopEval = 0;
100
b364c75005bb Stable.fix.
Dmitry Romanov <kitt@bk.ru>
parents: 94
diff changeset
79 if(global.dataSendToSlavePending)
b364c75005bb Stable.fix.
Dmitry Romanov <kitt@bk.ru>
parents: 94
diff changeset
80 {
b364c75005bb Stable.fix.
Dmitry Romanov <kitt@bk.ru>
parents: 94
diff changeset
81 scheduleSpecial_Evaluate_DataSendToSlave();
b364c75005bb Stable.fix.
Dmitry Romanov <kitt@bk.ru>
parents: 94
diff changeset
82 }
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
83 return (HAL_StatusTypeDef)global.I2C_SystemStatus;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
84 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
85
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
86
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
87 void I2C_DeInit(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
88 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
89 HAL_I2C_DeInit(&I2cHandle);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
90 }
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 uint8_t i2c_errors = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
94
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
95 void I2C_Error_count(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
96 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
97 i2c_errors++;
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
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
101 HAL_StatusTypeDef I2C_Master_TransmitNoStop( uint16_t DevAddress, uint8_t *pData, uint16_t Size)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
102 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
103 if(global.I2C_SystemStatus != HAL_OK)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
104 return (HAL_StatusTypeDef)global.I2C_SystemStatus;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
105
88
Dmitry Romanov <kitt@bk.ru>
parents: 84
diff changeset
106 global.dataSendToSlaveStopEval = 1;
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
107
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
108 global.I2C_SystemStatus = HAL_I2C_Master_Transmit(&I2cHandle, DevAddress, pData, Size, 100 /*FIXME , 0*/);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
109 if(global.I2C_SystemStatus != HAL_OK)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
110 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
111 I2C_Error_count();
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
112 }
88
Dmitry Romanov <kitt@bk.ru>
parents: 84
diff changeset
113 global.dataSendToSlaveStopEval = 0;
104
22a1094545f3 Tested and alive.
Dmitry Romanov <kitt@bk.ru>
parents: 100
diff changeset
114 //TODO: REMOVE.
100
b364c75005bb Stable.fix.
Dmitry Romanov <kitt@bk.ru>
parents: 94
diff changeset
115 // if(global.dataSendToSlavePending)
b364c75005bb Stable.fix.
Dmitry Romanov <kitt@bk.ru>
parents: 94
diff changeset
116 // {
b364c75005bb Stable.fix.
Dmitry Romanov <kitt@bk.ru>
parents: 94
diff changeset
117 // scheduleSpecial_Evaluate_DataSendToSlave();
b364c75005bb Stable.fix.
Dmitry Romanov <kitt@bk.ru>
parents: 94
diff changeset
118 // }
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
119 return (HAL_StatusTypeDef)global.I2C_SystemStatus;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
120 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
121
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
122
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
123 HAL_StatusTypeDef I2C_Master_Transmit( uint16_t DevAddress, uint8_t *pData, uint16_t Size)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
124 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
125 if(global.I2C_SystemStatus != HAL_OK)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
126 return (HAL_StatusTypeDef)(global.I2C_SystemStatus & 0x03);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
127
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
128
88
Dmitry Romanov <kitt@bk.ru>
parents: 84
diff changeset
129 global.dataSendToSlaveStopEval = 1;
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
130
104
22a1094545f3 Tested and alive.
Dmitry Romanov <kitt@bk.ru>
parents: 100
diff changeset
131 global.I2C_SystemStatus = HAL_I2C_Master_Transmit(&I2cHandle, DevAddress, pData, Size,100 /*FIXME , 1*/);
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
132 if(global.I2C_SystemStatus != HAL_OK)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
133 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
134 I2C_Error_count();
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 = 0;
104
22a1094545f3 Tested and alive.
Dmitry Romanov <kitt@bk.ru>
parents: 100
diff changeset
138 //TODO: REMOVE.
100
b364c75005bb Stable.fix.
Dmitry Romanov <kitt@bk.ru>
parents: 94
diff changeset
139 // if(global.dataSendToSlavePending)
b364c75005bb Stable.fix.
Dmitry Romanov <kitt@bk.ru>
parents: 94
diff changeset
140 // {
b364c75005bb Stable.fix.
Dmitry Romanov <kitt@bk.ru>
parents: 94
diff changeset
141 // scheduleSpecial_Evaluate_DataSendToSlave();
b364c75005bb Stable.fix.
Dmitry Romanov <kitt@bk.ru>
parents: 94
diff changeset
142 // }
88
Dmitry Romanov <kitt@bk.ru>
parents: 84
diff changeset
143
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
144 return (HAL_StatusTypeDef)global.I2C_SystemStatus;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
145 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
146
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
147
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
148 HAL_StatusTypeDef I2C_Master_Receive( uint16_t DevAddress, uint8_t *pData, uint16_t Size)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
149 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
150 if(global.I2C_SystemStatus != HAL_OK)
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 uint8_t localHALstatusReturn = 0xFF;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
154
88
Dmitry Romanov <kitt@bk.ru>
parents: 84
diff changeset
155 global.dataSendToSlaveStopEval = 1;
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
156
104
22a1094545f3 Tested and alive.
Dmitry Romanov <kitt@bk.ru>
parents: 100
diff changeset
157 localHALstatusReturn = HAL_I2C_Master_Receive(&I2cHandle, DevAddress, pData, Size, 10);
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
158 if(localHALstatusReturn != HAL_OK)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
159 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
160 I2C_Error_count();
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
161 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
162
88
Dmitry Romanov <kitt@bk.ru>
parents: 84
diff changeset
163 global.dataSendToSlaveStopEval = 0;
104
22a1094545f3 Tested and alive.
Dmitry Romanov <kitt@bk.ru>
parents: 100
diff changeset
164 //TODO: REMOVE.
94
c6d284ea265b reduce i2c speed and dutycycle for compass stability
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
165 // if(global.dataSendToSlavePending)
c6d284ea265b reduce i2c speed and dutycycle for compass stability
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
166 // {
100
b364c75005bb Stable.fix.
Dmitry Romanov <kitt@bk.ru>
parents: 94
diff changeset
167 // scheduleSpecial_Evaluate_DataSendToSlave();
94
c6d284ea265b reduce i2c speed and dutycycle for compass stability
Dmitry Romanov <kitt@bk.ru>
parents: 88
diff changeset
168 // }
100
b364c75005bb Stable.fix.
Dmitry Romanov <kitt@bk.ru>
parents: 94
diff changeset
169
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
170 return (HAL_StatusTypeDef)localHALstatusReturn;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
171 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
172