annotate Small_CPU/Src/i2c.c @ 240:625d20070261 div-fixes-5

Improvement SPI stability/recoverability The core part of this commit comes from careful code reading. The core is the swap of Scheduler_Request_sync_with_SPI(SPI_SYNC_METHOD_SOFT) and SPI_Start_single_TxRx_with_Master(). This code is sitting in an if-clause that is triggered on SPI comms failure. Instead of blindly trying to communicate again (which will very likely fail again), first try to reset the comms link, and then try to communicate again. That simply makes more sense in this case. This is heavily tested, on 2 simple dives, and 5 very long deco schedules from the simulator (10+ hour deco's), and a lot of small simulated dives (upto 2h runtime). Of all these tests, only one long session failed after 9 out of 11h runtime. Analyzing that one failure, suggests that the RTE is looping in some error handler, which (obviously) results in a SPI comms failure as a result. I consider this not part of this change. Additionally, some more cleanup is done in this code. Signed-off-by: Jan Mulder <jlmulder@xs4all.nl>
author Jan Mulder <jlmulder@xs4all.nl>
date Mon, 08 Apr 2019 11:49:13 +0200
parents e4207f0aaa4b
children 2b9775f71e30
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
238
a9d798e8c11f cleanup, bugfix: do not mask I2C_SystemStatus with local variable
Jan Mulder <jlmulder@xs4all.nl>
parents: 182
diff changeset
26 //TODO: remove this function. Why return a global variable?
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
27 HAL_StatusTypeDef I2C1_Status(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
28 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
29 return (HAL_StatusTypeDef)global.I2C_SystemStatus;
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
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
33 GPIO_PinState HAL_I2C_Read_Data_PIN(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
34 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
35 return HAL_GPIO_ReadPin(I2Cx_SDA_GPIO_PORT,I2Cx_SDA_PIN);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
36 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
37
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
38 void HAL_I2C_Send_One_CLOCK(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
39 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
40 HAL_GPIO_WritePin(I2Cx_SCL_GPIO_PORT, I2Cx_SCL_PIN, GPIO_PIN_RESET);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
41 HAL_Delay(10);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
42 HAL_GPIO_WritePin(I2Cx_SCL_GPIO_PORT, I2Cx_SCL_PIN, GPIO_PIN_SET);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
43 HAL_Delay(10);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
44 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
45
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
46 GPIO_PinState MX_I2C1_TestAndClear(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
47 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
48 I2C_DeInit();
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
49 HAL_I2C_ManualControl_MspInit();
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
50 for(int i=0; i<9;i++)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
51 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
52 if(HAL_I2C_Read_Data_PIN() == GPIO_PIN_RESET)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
53 HAL_I2C_Send_One_CLOCK();
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
54 else
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
55 break;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
56 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
57 return HAL_I2C_Read_Data_PIN();
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
58 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
59
238
a9d798e8c11f cleanup, bugfix: do not mask I2C_SystemStatus with local variable
Jan Mulder <jlmulder@xs4all.nl>
parents: 182
diff changeset
60 //TODO: make this void. return is never used
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
61 HAL_StatusTypeDef MX_I2C1_Init(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
62 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
63 I2cHandle.Instance = I2Cx;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
64 I2cHandle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
104
22a1094545f3 Tested and alive.
Dmitry Romanov <kitt@bk.ru>
parents: 100
diff changeset
65 I2cHandle.Init.ClockSpeed = 100000;//400000; REDUCED for compatibility with HMC5583L + MMA8452Q
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
66 I2cHandle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLED;
104
22a1094545f3 Tested and alive.
Dmitry Romanov <kitt@bk.ru>
parents: 100
diff changeset
67 I2cHandle.Init.DutyCycle = I2C_DUTYCYCLE_2;
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
68 I2cHandle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLED;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
69 I2cHandle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLED;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
70 I2cHandle.Init.OwnAddress1 = 0x01;
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
b364c75005bb Stable.fix.
Dmitry Romanov <kitt@bk.ru>
parents: 94
diff changeset
76 if(global.dataSendToSlavePending)
b364c75005bb Stable.fix.
Dmitry Romanov <kitt@bk.ru>
parents: 94
diff changeset
77 {
b364c75005bb Stable.fix.
Dmitry Romanov <kitt@bk.ru>
parents: 94
diff changeset
78 scheduleSpecial_Evaluate_DataSendToSlave();
b364c75005bb Stable.fix.
Dmitry Romanov <kitt@bk.ru>
parents: 94
diff changeset
79 }
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
80 return (HAL_StatusTypeDef)global.I2C_SystemStatus;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
81 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
82
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
83
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
84 void I2C_DeInit(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
85 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
86 HAL_I2C_DeInit(&I2cHandle);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
87 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
88
239
e4207f0aaa4b cleanup: factor out dataSendToSlaveStopEval
Jan Mulder <jlmulder@xs4all.nl>
parents: 238
diff changeset
89 static uint8_t i2c_errors = 0;
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
90
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
91 void I2C_Error_count(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
92 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
93 i2c_errors++;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
94 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
95
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
96 HAL_StatusTypeDef I2C_Master_Transmit( uint16_t DevAddress, uint8_t *pData, uint16_t Size)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
97 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
98 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
99 return global.I2C_SystemStatus;
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
100
182
7e749084f347 Resolve fixme comments
ideenmodellierer
parents: 165
diff changeset
101 global.I2C_SystemStatus = HAL_I2C_Master_Transmit(&I2cHandle, DevAddress, pData, Size, 2);
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
102 if(global.I2C_SystemStatus != HAL_OK)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
103 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
104 I2C_Error_count();
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
105 }
88
Dmitry Romanov <kitt@bk.ru>
parents: 84
diff changeset
106
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
107 return (HAL_StatusTypeDef)global.I2C_SystemStatus;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
108 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
109
238
a9d798e8c11f cleanup, bugfix: do not mask I2C_SystemStatus with local variable
Jan Mulder <jlmulder@xs4all.nl>
parents: 182
diff changeset
110 // TODO: return value never used
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
111 HAL_StatusTypeDef I2C_Master_Receive( uint16_t DevAddress, uint8_t *pData, uint16_t Size)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
112 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
113 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
114 return global.I2C_SystemStatus;
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
115
238
a9d798e8c11f cleanup, bugfix: do not mask I2C_SystemStatus with local variable
Jan Mulder <jlmulder@xs4all.nl>
parents: 182
diff changeset
116 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
117 if(global.I2C_SystemStatus != HAL_OK)
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
118 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
119 I2C_Error_count();
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
120 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
121
238
a9d798e8c11f cleanup, bugfix: do not mask I2C_SystemStatus with local variable
Jan Mulder <jlmulder@xs4all.nl>
parents: 182
diff changeset
122 return global.I2C_SystemStatus;
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
123 }