Mercurial > public > ostc4
annotate Small_CPU/Src/i2c.c @ 179:a492a7a457b7 Cleanup_Compass_Wireless
Starting 'Cleanup_Compass_Wireless' branch
author | ideenmodellierer |
---|---|
date | Wed, 13 Mar 2019 20:53:05 +0100 |
parents | e9cce686fe41 |
children | 7e749084f347 |
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 | |
165
e9cce686fe41
Minor: Some documentation for new hardware
heinrichsweikamp
parents:
104
diff
changeset
|
12 // =============================================================================== |
38 | 13 |
14 I2C_HandleTypeDef I2cHandle; | |
15 | |
16 | |
17 /* | |
18 static void I2C_Error_Handler(void) | |
19 { | |
20 while(1) | |
21 { | |
22 } | |
23 } | |
24 */ | |
25 | |
26 HAL_StatusTypeDef I2C1_Status(void) | |
27 { | |
28 return (HAL_StatusTypeDef)global.I2C_SystemStatus; | |
29 } | |
30 | |
31 | |
32 GPIO_PinState HAL_I2C_Read_Data_PIN(void) | |
33 { | |
34 return HAL_GPIO_ReadPin(I2Cx_SDA_GPIO_PORT,I2Cx_SDA_PIN); | |
35 } | |
36 | |
37 void HAL_I2C_Send_One_CLOCK(void) | |
38 { | |
39 HAL_GPIO_WritePin(I2Cx_SCL_GPIO_PORT, I2Cx_SCL_PIN, GPIO_PIN_RESET); | |
40 HAL_Delay(10); | |
41 HAL_GPIO_WritePin(I2Cx_SCL_GPIO_PORT, I2Cx_SCL_PIN, GPIO_PIN_SET); | |
42 HAL_Delay(10); | |
43 } | |
44 | |
45 GPIO_PinState MX_I2C1_TestAndClear(void) | |
46 { | |
47 I2C_DeInit(); | |
48 HAL_I2C_ManualControl_MspInit(); | |
49 for(int i=0; i<9;i++) | |
50 { | |
51 if(HAL_I2C_Read_Data_PIN() == GPIO_PIN_RESET) | |
52 HAL_I2C_Send_One_CLOCK(); | |
53 else | |
54 break; | |
55 } | |
56 return HAL_I2C_Read_Data_PIN(); | |
57 } | |
58 | |
59 HAL_StatusTypeDef MX_I2C1_Init(void) | |
60 { | |
61 I2cHandle.Instance = I2Cx; | |
62 I2cHandle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; | |
104 | 63 I2cHandle.Init.ClockSpeed = 100000;//400000; REDUCED for compatibility with HMC5583L + MMA8452Q |
38 | 64 I2cHandle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLED; |
104 | 65 I2cHandle.Init.DutyCycle = I2C_DUTYCYCLE_2; |
38 | 66 I2cHandle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLED; |
67 I2cHandle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLED; | |
68 I2cHandle.Init.OwnAddress1 = 0x01; | |
69 | |
88 | 70 global.dataSendToSlaveStopEval = 1; |
38 | 71 |
72 global.I2C_SystemStatus = HAL_I2C_Init(&I2cHandle); | |
82 | 73 HAL_I2CEx_AnalogFilter_Config(&I2cHandle, I2C_ANALOGFILTER_ENABLED); |
100 | 74 HAL_I2CEx_ConfigDigitalFilter(&I2cHandle,0x0F); |
75 | |
82 | 76 |
38 | 77 |
88 | 78 global.dataSendToSlaveStopEval = 0; |
100 | 79 if(global.dataSendToSlavePending) |
80 { | |
81 scheduleSpecial_Evaluate_DataSendToSlave(); | |
82 } | |
38 | 83 return (HAL_StatusTypeDef)global.I2C_SystemStatus; |
84 } | |
85 | |
86 | |
87 void I2C_DeInit(void) | |
88 { | |
89 HAL_I2C_DeInit(&I2cHandle); | |
90 } | |
91 | |
92 | |
93 uint8_t i2c_errors = 0; | |
94 | |
95 void I2C_Error_count(void) | |
96 { | |
97 i2c_errors++; | |
98 } | |
99 | |
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 | |
88 | 106 global.dataSendToSlaveStopEval = 1; |
38 | 107 |
108 global.I2C_SystemStatus = HAL_I2C_Master_Transmit(&I2cHandle, DevAddress, pData, Size, 100 /*FIXME , 0*/); | |
109 if(global.I2C_SystemStatus != HAL_OK) | |
110 { | |
111 I2C_Error_count(); | |
112 } | |
88 | 113 global.dataSendToSlaveStopEval = 0; |
104 | 114 //TODO: REMOVE. |
100 | 115 // if(global.dataSendToSlavePending) |
116 // { | |
117 // scheduleSpecial_Evaluate_DataSendToSlave(); | |
118 // } | |
38 | 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) | |
124 { | |
125 if(global.I2C_SystemStatus != HAL_OK) | |
126 return (HAL_StatusTypeDef)(global.I2C_SystemStatus & 0x03); | |
127 | |
128 | |
88 | 129 global.dataSendToSlaveStopEval = 1; |
38 | 130 |
104 | 131 global.I2C_SystemStatus = HAL_I2C_Master_Transmit(&I2cHandle, DevAddress, pData, Size,100 /*FIXME , 1*/); |
38 | 132 if(global.I2C_SystemStatus != HAL_OK) |
133 { | |
134 I2C_Error_count(); | |
135 } | |
136 | |
88 | 137 global.dataSendToSlaveStopEval = 0; |
104 | 138 //TODO: REMOVE. |
100 | 139 // if(global.dataSendToSlavePending) |
140 // { | |
141 // scheduleSpecial_Evaluate_DataSendToSlave(); | |
142 // } | |
88 | 143 |
38 | 144 return (HAL_StatusTypeDef)global.I2C_SystemStatus; |
145 } | |
146 | |
147 | |
148 HAL_StatusTypeDef I2C_Master_Receive( uint16_t DevAddress, uint8_t *pData, uint16_t Size) | |
149 { | |
150 if(global.I2C_SystemStatus != HAL_OK) | |
151 return (HAL_StatusTypeDef)global.I2C_SystemStatus; | |
152 | |
153 uint8_t localHALstatusReturn = 0xFF; | |
154 | |
88 | 155 global.dataSendToSlaveStopEval = 1; |
38 | 156 |
104 | 157 localHALstatusReturn = HAL_I2C_Master_Receive(&I2cHandle, DevAddress, pData, Size, 10); |
38 | 158 if(localHALstatusReturn != HAL_OK) |
159 { | |
160 I2C_Error_count(); | |
161 } | |
162 | |
88 | 163 global.dataSendToSlaveStopEval = 0; |
104 | 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 | 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 | 169 |
38 | 170 return (HAL_StatusTypeDef)localHALstatusReturn; |
171 } | |
172 |