Mercurial > public > ostc4
annotate Small_CPU/Src/i2c.c @ 86:cc41b5eaf1a7 kittz
fix time&date after wakeup
author | Dmitry Romanov <kitt@bk.ru> |
---|---|
date | Wed, 21 Nov 2018 13:12:44 +0300 |
parents | e6abbef57475 |
children | 3db7389d49cc |
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; | |
82 | 72 I2cHandle.Init.ClockSpeed = 400000;//400000; |
38 | 73 I2cHandle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLED; |
74 I2cHandle.Init.DutyCycle = I2C_DUTYCYCLE_16_9; | |
75 I2cHandle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLED; | |
76 I2cHandle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLED; | |
77 I2cHandle.Init.OwnAddress1 = 0x01; | |
78 | |
79 | |
80 global.I2C_SystemStatus = HAL_I2C_Init(&I2cHandle); | |
82 | 81 HAL_I2CEx_AnalogFilter_Config(&I2cHandle, I2C_ANALOGFILTER_ENABLED); |
82 | |
38 | 83 |
84
e6abbef57475
Remove unusable code!!! prepare data for SPI in ONLY 1 IRQ!!!!
Dmitry Romanov <kitt@bk.ru>
parents:
82
diff
changeset
|
84 // if(global.dataSendToSlavePending) |
e6abbef57475
Remove unusable code!!! prepare data for SPI in ONLY 1 IRQ!!!!
Dmitry Romanov <kitt@bk.ru>
parents:
82
diff
changeset
|
85 // { |
e6abbef57475
Remove unusable code!!! prepare data for SPI in ONLY 1 IRQ!!!!
Dmitry Romanov <kitt@bk.ru>
parents:
82
diff
changeset
|
86 // scheduleSpecial_Evaluate_DataSendToSlave(); |
e6abbef57475
Remove unusable code!!! prepare data for SPI in ONLY 1 IRQ!!!!
Dmitry Romanov <kitt@bk.ru>
parents:
82
diff
changeset
|
87 // } |
e6abbef57475
Remove unusable code!!! prepare data for SPI in ONLY 1 IRQ!!!!
Dmitry Romanov <kitt@bk.ru>
parents:
82
diff
changeset
|
88 global.dataUpdateIsNeeded = 1; |
38 | 89 return (HAL_StatusTypeDef)global.I2C_SystemStatus; |
90 } | |
91 | |
92 | |
93 void I2C_DeInit(void) | |
94 { | |
95 HAL_I2C_DeInit(&I2cHandle); | |
96 } | |
97 | |
98 | |
99 uint8_t i2c_errors = 0; | |
100 | |
101 void I2C_Error_count(void) | |
102 { | |
103 i2c_errors++; | |
104 } | |
105 | |
106 | |
107 HAL_StatusTypeDef I2C_Master_TransmitNoStop( uint16_t DevAddress, uint8_t *pData, uint16_t Size) | |
108 { | |
109 if(global.I2C_SystemStatus != HAL_OK) | |
110 return (HAL_StatusTypeDef)global.I2C_SystemStatus; | |
111 | |
112 | |
84
e6abbef57475
Remove unusable code!!! prepare data for SPI in ONLY 1 IRQ!!!!
Dmitry Romanov <kitt@bk.ru>
parents:
82
diff
changeset
|
113 |
38 | 114 global.I2C_SystemStatus = HAL_I2C_Master_Transmit(&I2cHandle, DevAddress, pData, Size, 100 /*FIXME , 0*/); |
115 if(global.I2C_SystemStatus != HAL_OK) | |
116 { | |
117 I2C_Error_count(); | |
118 } | |
84
e6abbef57475
Remove unusable code!!! prepare data for SPI in ONLY 1 IRQ!!!!
Dmitry Romanov <kitt@bk.ru>
parents:
82
diff
changeset
|
119 |
e6abbef57475
Remove unusable code!!! prepare data for SPI in ONLY 1 IRQ!!!!
Dmitry Romanov <kitt@bk.ru>
parents:
82
diff
changeset
|
120 // if(global.dataSendToSlavePending) |
e6abbef57475
Remove unusable code!!! prepare data for SPI in ONLY 1 IRQ!!!!
Dmitry Romanov <kitt@bk.ru>
parents:
82
diff
changeset
|
121 // { |
e6abbef57475
Remove unusable code!!! prepare data for SPI in ONLY 1 IRQ!!!!
Dmitry Romanov <kitt@bk.ru>
parents:
82
diff
changeset
|
122 // scheduleSpecial_Evaluate_DataSendToSlave(); |
e6abbef57475
Remove unusable code!!! prepare data for SPI in ONLY 1 IRQ!!!!
Dmitry Romanov <kitt@bk.ru>
parents:
82
diff
changeset
|
123 // } |
e6abbef57475
Remove unusable code!!! prepare data for SPI in ONLY 1 IRQ!!!!
Dmitry Romanov <kitt@bk.ru>
parents:
82
diff
changeset
|
124 global.dataUpdateIsNeeded = 1; |
38 | 125 return (HAL_StatusTypeDef)global.I2C_SystemStatus; |
126 } | |
127 | |
128 | |
129 HAL_StatusTypeDef I2C_Master_Transmit( uint16_t DevAddress, uint8_t *pData, uint16_t Size) | |
130 { | |
131 if(global.I2C_SystemStatus != HAL_OK) | |
132 return (HAL_StatusTypeDef)(global.I2C_SystemStatus & 0x03); | |
133 | |
134 | |
84
e6abbef57475
Remove unusable code!!! prepare data for SPI in ONLY 1 IRQ!!!!
Dmitry Romanov <kitt@bk.ru>
parents:
82
diff
changeset
|
135 |
38 | 136 |
137 global.I2C_SystemStatus = HAL_I2C_Master_Transmit(&I2cHandle, DevAddress, pData, Size, 100 /*FIXME , 1*/); | |
138 if(global.I2C_SystemStatus != HAL_OK) | |
139 { | |
140 I2C_Error_count(); | |
141 } | |
142 | |
84
e6abbef57475
Remove unusable code!!! prepare data for SPI in ONLY 1 IRQ!!!!
Dmitry Romanov <kitt@bk.ru>
parents:
82
diff
changeset
|
143 // if(global.dataSendToSlavePending) |
e6abbef57475
Remove unusable code!!! prepare data for SPI in ONLY 1 IRQ!!!!
Dmitry Romanov <kitt@bk.ru>
parents:
82
diff
changeset
|
144 // { |
e6abbef57475
Remove unusable code!!! prepare data for SPI in ONLY 1 IRQ!!!!
Dmitry Romanov <kitt@bk.ru>
parents:
82
diff
changeset
|
145 // scheduleSpecial_Evaluate_DataSendToSlave(); |
e6abbef57475
Remove unusable code!!! prepare data for SPI in ONLY 1 IRQ!!!!
Dmitry Romanov <kitt@bk.ru>
parents:
82
diff
changeset
|
146 // } |
e6abbef57475
Remove unusable code!!! prepare data for SPI in ONLY 1 IRQ!!!!
Dmitry Romanov <kitt@bk.ru>
parents:
82
diff
changeset
|
147 // |
e6abbef57475
Remove unusable code!!! prepare data for SPI in ONLY 1 IRQ!!!!
Dmitry Romanov <kitt@bk.ru>
parents:
82
diff
changeset
|
148 global.dataUpdateIsNeeded = 1; |
38 | 149 return (HAL_StatusTypeDef)global.I2C_SystemStatus; |
150 } | |
151 | |
152 | |
153 HAL_StatusTypeDef I2C_Master_Receive( uint16_t DevAddress, uint8_t *pData, uint16_t Size) | |
154 { | |
155 if(global.I2C_SystemStatus != HAL_OK) | |
156 return (HAL_StatusTypeDef)global.I2C_SystemStatus; | |
157 | |
158 uint8_t localHALstatusReturn = 0xFF; | |
159 | |
160 | |
161 localHALstatusReturn = HAL_I2C_Master_Receive(&I2cHandle, DevAddress, pData, Size, 100); | |
162 if(localHALstatusReturn != HAL_OK) | |
163 { | |
164 I2C_Error_count(); | |
165 } | |
166 | |
84
e6abbef57475
Remove unusable code!!! prepare data for SPI in ONLY 1 IRQ!!!!
Dmitry Romanov <kitt@bk.ru>
parents:
82
diff
changeset
|
167 global.dataUpdateIsNeeded = 1; |
e6abbef57475
Remove unusable code!!! prepare data for SPI in ONLY 1 IRQ!!!!
Dmitry Romanov <kitt@bk.ru>
parents:
82
diff
changeset
|
168 // if(global.dataSendToSlavePending) |
e6abbef57475
Remove unusable code!!! prepare data for SPI in ONLY 1 IRQ!!!!
Dmitry Romanov <kitt@bk.ru>
parents:
82
diff
changeset
|
169 // { |
e6abbef57475
Remove unusable code!!! prepare data for SPI in ONLY 1 IRQ!!!!
Dmitry Romanov <kitt@bk.ru>
parents:
82
diff
changeset
|
170 // scheduleSpecial_Evaluate_DataSendToSlave(); |
e6abbef57475
Remove unusable code!!! prepare data for SPI in ONLY 1 IRQ!!!!
Dmitry Romanov <kitt@bk.ru>
parents:
82
diff
changeset
|
171 // } |
38 | 172 return (HAL_StatusTypeDef)localHALstatusReturn; |
173 } | |
174 |