Mercurial > public > ostc4
annotate Small_CPU/Src/i2c.c @ 308:1203255481e4 cleanup-4
cleanup: introduce function setAvgDepth
The simulator and the realtime code shared a literally identical piece
of code to compute a running depth average. This is simply poor coding
style, so factor this out and create a function to do this work.
Signed-off-by: Jan Mulder <jlmulder@xs4all.nl>
author | Jan Mulder <jlmulder@xs4all.nl> |
---|---|
date | Fri, 24 May 2019 09:02:46 +0200 |
parents | 2b9775f71e30 |
children | 4fe5400567e7 |
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 GPIO_PinState HAL_I2C_Read_Data_PIN(void) | |
27 { | |
28 return HAL_GPIO_ReadPin(I2Cx_SDA_GPIO_PORT,I2Cx_SDA_PIN); | |
29 } | |
30 | |
31 void HAL_I2C_Send_One_CLOCK(void) | |
32 { | |
33 HAL_GPIO_WritePin(I2Cx_SCL_GPIO_PORT, I2Cx_SCL_PIN, GPIO_PIN_RESET); | |
34 HAL_Delay(10); | |
35 HAL_GPIO_WritePin(I2Cx_SCL_GPIO_PORT, I2Cx_SCL_PIN, GPIO_PIN_SET); | |
36 HAL_Delay(10); | |
37 } | |
38 | |
39 GPIO_PinState MX_I2C1_TestAndClear(void) | |
40 { | |
41 I2C_DeInit(); | |
42 HAL_I2C_ManualControl_MspInit(); | |
43 for(int i=0; i<9;i++) | |
44 { | |
45 if(HAL_I2C_Read_Data_PIN() == GPIO_PIN_RESET) | |
46 HAL_I2C_Send_One_CLOCK(); | |
47 else | |
48 break; | |
49 } | |
50 return HAL_I2C_Read_Data_PIN(); | |
51 } | |
52 | |
53 HAL_StatusTypeDef MX_I2C1_Init(void) | |
54 { | |
55 I2cHandle.Instance = I2Cx; | |
56 I2cHandle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; | |
104 | 57 I2cHandle.Init.ClockSpeed = 100000;//400000; REDUCED for compatibility with HMC5583L + MMA8452Q |
38 | 58 I2cHandle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLED; |
104 | 59 I2cHandle.Init.DutyCycle = I2C_DUTYCYCLE_2; |
38 | 60 I2cHandle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLED; |
61 I2cHandle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLED; | |
62 I2cHandle.Init.OwnAddress1 = 0x01; | |
63 | |
64 global.I2C_SystemStatus = HAL_I2C_Init(&I2cHandle); | |
82 | 65 HAL_I2CEx_AnalogFilter_Config(&I2cHandle, I2C_ANALOGFILTER_ENABLED); |
100 | 66 HAL_I2CEx_ConfigDigitalFilter(&I2cHandle,0x0F); |
67 | |
68 if(global.dataSendToSlavePending) | |
69 { | |
70 scheduleSpecial_Evaluate_DataSendToSlave(); | |
71 } | |
241
2b9775f71e30
cleanup: factor out I2C1_Status() and cleanup type
Jan Mulder <jlmulder@xs4all.nl>
parents:
239
diff
changeset
|
72 return global.I2C_SystemStatus; |
38 | 73 } |
74 | |
75 | |
76 void I2C_DeInit(void) | |
77 { | |
78 HAL_I2C_DeInit(&I2cHandle); | |
79 } | |
80 | |
239
e4207f0aaa4b
cleanup: factor out dataSendToSlaveStopEval
Jan Mulder <jlmulder@xs4all.nl>
parents:
238
diff
changeset
|
81 static uint8_t i2c_errors = 0; |
38 | 82 |
83 void I2C_Error_count(void) | |
84 { | |
85 i2c_errors++; | |
86 } | |
87 | |
88 HAL_StatusTypeDef I2C_Master_Transmit( uint16_t DevAddress, uint8_t *pData, uint16_t Size) | |
89 { | |
90 if(global.I2C_SystemStatus != HAL_OK) | |
238
a9d798e8c11f
cleanup, bugfix: do not mask I2C_SystemStatus with local variable
Jan Mulder <jlmulder@xs4all.nl>
parents:
182
diff
changeset
|
91 return global.I2C_SystemStatus; |
38 | 92 |
182 | 93 global.I2C_SystemStatus = HAL_I2C_Master_Transmit(&I2cHandle, DevAddress, pData, Size, 2); |
38 | 94 if(global.I2C_SystemStatus != HAL_OK) |
95 { | |
96 I2C_Error_count(); | |
97 } | |
88 | 98 |
241
2b9775f71e30
cleanup: factor out I2C1_Status() and cleanup type
Jan Mulder <jlmulder@xs4all.nl>
parents:
239
diff
changeset
|
99 return global.I2C_SystemStatus; |
38 | 100 } |
101 | |
102 HAL_StatusTypeDef I2C_Master_Receive( uint16_t DevAddress, uint8_t *pData, uint16_t Size) | |
103 { | |
104 if(global.I2C_SystemStatus != HAL_OK) | |
238
a9d798e8c11f
cleanup, bugfix: do not mask I2C_SystemStatus with local variable
Jan Mulder <jlmulder@xs4all.nl>
parents:
182
diff
changeset
|
105 return global.I2C_SystemStatus; |
38 | 106 |
238
a9d798e8c11f
cleanup, bugfix: do not mask I2C_SystemStatus with local variable
Jan Mulder <jlmulder@xs4all.nl>
parents:
182
diff
changeset
|
107 global.I2C_SystemStatus = HAL_I2C_Master_Receive(&I2cHandle, DevAddress, pData, Size, 10); |
a9d798e8c11f
cleanup, bugfix: do not mask I2C_SystemStatus with local variable
Jan Mulder <jlmulder@xs4all.nl>
parents:
182
diff
changeset
|
108 if(global.I2C_SystemStatus != HAL_OK) |
38 | 109 { |
110 I2C_Error_count(); | |
111 } | |
112 | |
238
a9d798e8c11f
cleanup, bugfix: do not mask I2C_SystemStatus with local variable
Jan Mulder <jlmulder@xs4all.nl>
parents:
182
diff
changeset
|
113 return global.I2C_SystemStatus; |
38 | 114 } |