Mercurial > public > ostc4
annotate Small_CPU/Src/i2c.c @ 146:5c0f80e6b37a FlipDisplay
RTE Version 1.7 needed because of protocoll and baud rate change
author | Ideenmodellierer |
---|---|
date | Sat, 02 Mar 2019 16:56:58 +0100 |
parents | 22a1094545f3 |
children | e9cce686fe41 |
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 | |
12 /// | |
13 /// #define DEVICE_PRESSURE 0xEE (0x77) Write 0xEE Read 0xEF | |
14 /// #define DEVICE_COMPASS_HMC5883L 0x3C (0x1E) Write 0x3C Read 0x3D | |
15 /// #define DEVICE_ACCELARATOR_MMA8452Q 0x38 (0x1C) Write 0x38 Read 0x39 | |
16 /// #define DEVICE_BATTERYGAUGE 0xC8 (0x64) Write 0xC8 | |
17 | |
18 /// #define DEVICE_COMPASS_303D 0x3C // 0x1E // x0011110 // SA0 to GND | |
19 /// | |
20 /// 0x6E 0x48 -> 0x6F 3d d1 86 3f 0x66 58 | |
21 /// | |
22 // =============================================================================== | |
23 I2C_HandleTypeDef I2cHandle; | |
24 | |
25 | |
26 /* | |
27 static void I2C_Error_Handler(void) | |
28 { | |
29 while(1) | |
30 { | |
31 } | |
32 } | |
33 */ | |
34 | |
35 HAL_StatusTypeDef I2C1_Status(void) | |
36 { | |
37 return (HAL_StatusTypeDef)global.I2C_SystemStatus; | |
38 } | |
39 | |
40 | |
41 GPIO_PinState HAL_I2C_Read_Data_PIN(void) | |
42 { | |
43 return HAL_GPIO_ReadPin(I2Cx_SDA_GPIO_PORT,I2Cx_SDA_PIN); | |
44 } | |
45 | |
46 void HAL_I2C_Send_One_CLOCK(void) | |
47 { | |
48 HAL_GPIO_WritePin(I2Cx_SCL_GPIO_PORT, I2Cx_SCL_PIN, GPIO_PIN_RESET); | |
49 HAL_Delay(10); | |
50 HAL_GPIO_WritePin(I2Cx_SCL_GPIO_PORT, I2Cx_SCL_PIN, GPIO_PIN_SET); | |
51 HAL_Delay(10); | |
52 } | |
53 | |
54 GPIO_PinState MX_I2C1_TestAndClear(void) | |
55 { | |
56 I2C_DeInit(); | |
57 HAL_I2C_ManualControl_MspInit(); | |
58 for(int i=0; i<9;i++) | |
59 { | |
60 if(HAL_I2C_Read_Data_PIN() == GPIO_PIN_RESET) | |
61 HAL_I2C_Send_One_CLOCK(); | |
62 else | |
63 break; | |
64 } | |
65 return HAL_I2C_Read_Data_PIN(); | |
66 } | |
67 | |
68 HAL_StatusTypeDef MX_I2C1_Init(void) | |
69 { | |
70 I2cHandle.Instance = I2Cx; | |
71 I2cHandle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; | |
104 | 72 I2cHandle.Init.ClockSpeed = 100000;//400000; REDUCED for compatibility with HMC5583L + MMA8452Q |
38 | 73 I2cHandle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLED; |
104 | 74 I2cHandle.Init.DutyCycle = I2C_DUTYCYCLE_2; |
38 | 75 I2cHandle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLED; |
76 I2cHandle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLED; | |
77 I2cHandle.Init.OwnAddress1 = 0x01; | |
78 | |
88 | 79 global.dataSendToSlaveStopEval = 1; |
38 | 80 |
81 global.I2C_SystemStatus = HAL_I2C_Init(&I2cHandle); | |
82 | 82 HAL_I2CEx_AnalogFilter_Config(&I2cHandle, I2C_ANALOGFILTER_ENABLED); |
100 | 83 HAL_I2CEx_ConfigDigitalFilter(&I2cHandle,0x0F); |
84 | |
82 | 85 |
38 | 86 |
88 | 87 global.dataSendToSlaveStopEval = 0; |
100 | 88 if(global.dataSendToSlavePending) |
89 { | |
90 scheduleSpecial_Evaluate_DataSendToSlave(); | |
91 } | |
38 | 92 return (HAL_StatusTypeDef)global.I2C_SystemStatus; |
93 } | |
94 | |
95 | |
96 void I2C_DeInit(void) | |
97 { | |
98 HAL_I2C_DeInit(&I2cHandle); | |
99 } | |
100 | |
101 | |
102 uint8_t i2c_errors = 0; | |
103 | |
104 void I2C_Error_count(void) | |
105 { | |
106 i2c_errors++; | |
107 } | |
108 | |
109 | |
110 HAL_StatusTypeDef I2C_Master_TransmitNoStop( uint16_t DevAddress, uint8_t *pData, uint16_t Size) | |
111 { | |
112 if(global.I2C_SystemStatus != HAL_OK) | |
113 return (HAL_StatusTypeDef)global.I2C_SystemStatus; | |
114 | |
88 | 115 global.dataSendToSlaveStopEval = 1; |
38 | 116 |
117 global.I2C_SystemStatus = HAL_I2C_Master_Transmit(&I2cHandle, DevAddress, pData, Size, 100 /*FIXME , 0*/); | |
118 if(global.I2C_SystemStatus != HAL_OK) | |
119 { | |
120 I2C_Error_count(); | |
121 } | |
88 | 122 global.dataSendToSlaveStopEval = 0; |
104 | 123 //TODO: REMOVE. |
100 | 124 // if(global.dataSendToSlavePending) |
125 // { | |
126 // scheduleSpecial_Evaluate_DataSendToSlave(); | |
127 // } | |
38 | 128 return (HAL_StatusTypeDef)global.I2C_SystemStatus; |
129 } | |
130 | |
131 | |
132 HAL_StatusTypeDef I2C_Master_Transmit( uint16_t DevAddress, uint8_t *pData, uint16_t Size) | |
133 { | |
134 if(global.I2C_SystemStatus != HAL_OK) | |
135 return (HAL_StatusTypeDef)(global.I2C_SystemStatus & 0x03); | |
136 | |
137 | |
88 | 138 global.dataSendToSlaveStopEval = 1; |
38 | 139 |
104 | 140 global.I2C_SystemStatus = HAL_I2C_Master_Transmit(&I2cHandle, DevAddress, pData, Size,100 /*FIXME , 1*/); |
38 | 141 if(global.I2C_SystemStatus != HAL_OK) |
142 { | |
143 I2C_Error_count(); | |
144 } | |
145 | |
88 | 146 global.dataSendToSlaveStopEval = 0; |
104 | 147 //TODO: REMOVE. |
100 | 148 // if(global.dataSendToSlavePending) |
149 // { | |
150 // scheduleSpecial_Evaluate_DataSendToSlave(); | |
151 // } | |
88 | 152 |
38 | 153 return (HAL_StatusTypeDef)global.I2C_SystemStatus; |
154 } | |
155 | |
156 | |
157 HAL_StatusTypeDef I2C_Master_Receive( uint16_t DevAddress, uint8_t *pData, uint16_t Size) | |
158 { | |
159 if(global.I2C_SystemStatus != HAL_OK) | |
160 return (HAL_StatusTypeDef)global.I2C_SystemStatus; | |
161 | |
162 uint8_t localHALstatusReturn = 0xFF; | |
163 | |
88 | 164 global.dataSendToSlaveStopEval = 1; |
38 | 165 |
104 | 166 localHALstatusReturn = HAL_I2C_Master_Receive(&I2cHandle, DevAddress, pData, Size, 10); |
38 | 167 if(localHALstatusReturn != HAL_OK) |
168 { | |
169 I2C_Error_count(); | |
170 } | |
171 | |
88 | 172 global.dataSendToSlaveStopEval = 0; |
104 | 173 //TODO: REMOVE. |
94
c6d284ea265b
reduce i2c speed and dutycycle for compass stability
Dmitry Romanov <kitt@bk.ru>
parents:
88
diff
changeset
|
174 // if(global.dataSendToSlavePending) |
c6d284ea265b
reduce i2c speed and dutycycle for compass stability
Dmitry Romanov <kitt@bk.ru>
parents:
88
diff
changeset
|
175 // { |
100 | 176 // scheduleSpecial_Evaluate_DataSendToSlave(); |
94
c6d284ea265b
reduce i2c speed and dutycycle for compass stability
Dmitry Romanov <kitt@bk.ru>
parents:
88
diff
changeset
|
177 // } |
100 | 178 |
38 | 179 return (HAL_StatusTypeDef)localHALstatusReturn; |
180 } | |
181 |