annotate Small_CPU/Src/i2c.c @ 414:6886aeeca454 Improment_NVM

Added compile switch for restoring of last known date after power lost: In case timing date was lost the RTC has to be set to a proper time anyhow. Maybe this function does not add as much value as expected... => Added compile switch (deactivated) to allow easy reactivation if this function is wanted.
author ideenmodellierer
date Tue, 14 Jan 2020 21:38:45 +0100
parents 4fe5400567e7
children
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
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
26 GPIO_PinState HAL_I2C_Read_Data_PIN(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
27 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
28 return HAL_GPIO_ReadPin(I2Cx_SDA_GPIO_PORT,I2Cx_SDA_PIN);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
29 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
30
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
31 void HAL_I2C_Send_One_CLOCK(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
32 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
33 HAL_GPIO_WritePin(I2Cx_SCL_GPIO_PORT, I2Cx_SCL_PIN, GPIO_PIN_RESET);
328
4fe5400567e7 Set I2C speed to 88kHz, use digital filter only and reworked idle clock recovery
ideenmodellierer
parents: 241
diff changeset
34 HAL_Delay(1);
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
35 HAL_GPIO_WritePin(I2Cx_SCL_GPIO_PORT, I2Cx_SCL_PIN, GPIO_PIN_SET);
328
4fe5400567e7 Set I2C speed to 88kHz, use digital filter only and reworked idle clock recovery
ideenmodellierer
parents: 241
diff changeset
36 HAL_Delay(1);
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
37 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
39 GPIO_PinState MX_I2C1_TestAndClear(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
40 {
328
4fe5400567e7 Set I2C speed to 88kHz, use digital filter only and reworked idle clock recovery
ideenmodellierer
parents: 241
diff changeset
41 GPIO_PinState retval;
4fe5400567e7 Set I2C speed to 88kHz, use digital filter only and reworked idle clock recovery
ideenmodellierer
parents: 241
diff changeset
42 uint8_t repeatcnt = 3;
4fe5400567e7 Set I2C speed to 88kHz, use digital filter only and reworked idle clock recovery
ideenmodellierer
parents: 241
diff changeset
43
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
44 I2C_DeInit();
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
45 HAL_I2C_ManualControl_MspInit();
328
4fe5400567e7 Set I2C speed to 88kHz, use digital filter only and reworked idle clock recovery
ideenmodellierer
parents: 241
diff changeset
46
4fe5400567e7 Set I2C speed to 88kHz, use digital filter only and reworked idle clock recovery
ideenmodellierer
parents: 241
diff changeset
47 /* The SDA line is expected to be HIGH if no com is pending => send dummy clock signals if that is not the case */
4fe5400567e7 Set I2C speed to 88kHz, use digital filter only and reworked idle clock recovery
ideenmodellierer
parents: 241
diff changeset
48 do
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
49 {
328
4fe5400567e7 Set I2C speed to 88kHz, use digital filter only and reworked idle clock recovery
ideenmodellierer
parents: 241
diff changeset
50 for(int i=0; i<20;i++)
4fe5400567e7 Set I2C speed to 88kHz, use digital filter only and reworked idle clock recovery
ideenmodellierer
parents: 241
diff changeset
51 {
4fe5400567e7 Set I2C speed to 88kHz, use digital filter only and reworked idle clock recovery
ideenmodellierer
parents: 241
diff changeset
52 if(HAL_I2C_Read_Data_PIN() == GPIO_PIN_RESET)
4fe5400567e7 Set I2C speed to 88kHz, use digital filter only and reworked idle clock recovery
ideenmodellierer
parents: 241
diff changeset
53 HAL_I2C_Send_One_CLOCK();
4fe5400567e7 Set I2C speed to 88kHz, use digital filter only and reworked idle clock recovery
ideenmodellierer
parents: 241
diff changeset
54 else
4fe5400567e7 Set I2C speed to 88kHz, use digital filter only and reworked idle clock recovery
ideenmodellierer
parents: 241
diff changeset
55 break;
4fe5400567e7 Set I2C speed to 88kHz, use digital filter only and reworked idle clock recovery
ideenmodellierer
parents: 241
diff changeset
56 }
4fe5400567e7 Set I2C speed to 88kHz, use digital filter only and reworked idle clock recovery
ideenmodellierer
parents: 241
diff changeset
57 retval = HAL_I2C_Read_Data_PIN();
4fe5400567e7 Set I2C speed to 88kHz, use digital filter only and reworked idle clock recovery
ideenmodellierer
parents: 241
diff changeset
58 }while ((repeatcnt-- > 0) && (retval != GPIO_PIN_SET));
4fe5400567e7 Set I2C speed to 88kHz, use digital filter only and reworked idle clock recovery
ideenmodellierer
parents: 241
diff changeset
59
4fe5400567e7 Set I2C speed to 88kHz, use digital filter only and reworked idle clock recovery
ideenmodellierer
parents: 241
diff changeset
60 return retval;
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
61 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
62
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
63 HAL_StatusTypeDef MX_I2C1_Init(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
64 {
328
4fe5400567e7 Set I2C speed to 88kHz, use digital filter only and reworked idle clock recovery
ideenmodellierer
parents: 241
diff changeset
65 I2cHandle.Instance = I2Cx;
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
66 I2cHandle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
328
4fe5400567e7 Set I2C speed to 88kHz, use digital filter only and reworked idle clock recovery
ideenmodellierer
parents: 241
diff changeset
67 I2cHandle.Init.ClockSpeed = 88000; /* Reduced to avoid behavior described in errata: Mismatch on the “Setup time for a repeated Start condition” */
4fe5400567e7 Set I2C speed to 88kHz, use digital filter only and reworked idle clock recovery
ideenmodellierer
parents: 241
diff changeset
68 I2cHandle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
4fe5400567e7 Set I2C speed to 88kHz, use digital filter only and reworked idle clock recovery
ideenmodellierer
parents: 241
diff changeset
69 I2cHandle.Init.DutyCycle = I2C_DUTYCYCLE_2; /* don't care if not in fast mode */
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
70 I2cHandle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLED;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
71 I2cHandle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLED;
328
4fe5400567e7 Set I2C speed to 88kHz, use digital filter only and reworked idle clock recovery
ideenmodellierer
parents: 241
diff changeset
72 I2cHandle.Init.OwnAddress1 = 0x01; /* don't care because of master mode */
4fe5400567e7 Set I2C speed to 88kHz, use digital filter only and reworked idle clock recovery
ideenmodellierer
parents: 241
diff changeset
73
4fe5400567e7 Set I2C speed to 88kHz, use digital filter only and reworked idle clock recovery
ideenmodellierer
parents: 241
diff changeset
74 /* According to documentation setting filters before I2C initialization is recommended */
4fe5400567e7 Set I2C speed to 88kHz, use digital filter only and reworked idle clock recovery
ideenmodellierer
parents: 241
diff changeset
75 /* HAL_I2CEx_AnalogFilter_Config(&I2cHandle, I2C_ANALOGFILTER_ENABLED); */
4fe5400567e7 Set I2C speed to 88kHz, use digital filter only and reworked idle clock recovery
ideenmodellierer
parents: 241
diff changeset
76 HAL_I2CEx_ConfigDigitalFilter(&I2cHandle,0x0F);
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
77
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
78 global.I2C_SystemStatus = HAL_I2C_Init(&I2cHandle);
100
b364c75005bb Stable.fix.
Dmitry Romanov <kitt@bk.ru>
parents: 94
diff changeset
79
b364c75005bb Stable.fix.
Dmitry Romanov <kitt@bk.ru>
parents: 94
diff changeset
80 if(global.dataSendToSlavePending)
b364c75005bb Stable.fix.
Dmitry Romanov <kitt@bk.ru>
parents: 94
diff changeset
81 {
b364c75005bb Stable.fix.
Dmitry Romanov <kitt@bk.ru>
parents: 94
diff changeset
82 scheduleSpecial_Evaluate_DataSendToSlave();
b364c75005bb Stable.fix.
Dmitry Romanov <kitt@bk.ru>
parents: 94
diff changeset
83 }
241
2b9775f71e30 cleanup: factor out I2C1_Status() and cleanup type
Jan Mulder <jlmulder@xs4all.nl>
parents: 239
diff changeset
84 return global.I2C_SystemStatus;
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
85 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
86
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
87
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
88 void I2C_DeInit(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
89 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
90 HAL_I2C_DeInit(&I2cHandle);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
91 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
92
239
e4207f0aaa4b cleanup: factor out dataSendToSlaveStopEval
Jan Mulder <jlmulder@xs4all.nl>
parents: 238
diff changeset
93 static uint8_t i2c_errors = 0;
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
94
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
95 void I2C_Error_count(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
96 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
97 i2c_errors++;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
98 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
99
328
4fe5400567e7 Set I2C speed to 88kHz, use digital filter only and reworked idle clock recovery
ideenmodellierer
parents: 241
diff changeset
100
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
101 HAL_StatusTypeDef I2C_Master_Transmit( uint16_t DevAddress, uint8_t *pData, uint16_t Size)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
102 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
103 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
104 return global.I2C_SystemStatus;
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
105
328
4fe5400567e7 Set I2C speed to 88kHz, use digital filter only and reworked idle clock recovery
ideenmodellierer
parents: 241
diff changeset
106 global.I2C_SystemStatus = HAL_I2C_Master_Transmit(&I2cHandle, DevAddress, pData, Size, 10);
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
107 if(global.I2C_SystemStatus != HAL_OK)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
108 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
109 I2C_Error_count();
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
110 }
88
Dmitry Romanov <kitt@bk.ru>
parents: 84
diff changeset
111
241
2b9775f71e30 cleanup: factor out I2C1_Status() and cleanup type
Jan Mulder <jlmulder@xs4all.nl>
parents: 239
diff changeset
112 return global.I2C_SystemStatus;
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
113 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
114
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
115 HAL_StatusTypeDef I2C_Master_Receive( uint16_t DevAddress, uint8_t *pData, uint16_t Size)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
116 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
117 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
118 return global.I2C_SystemStatus;
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
119
238
a9d798e8c11f cleanup, bugfix: do not mask I2C_SystemStatus with local variable
Jan Mulder <jlmulder@xs4all.nl>
parents: 182
diff changeset
120 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
121 if(global.I2C_SystemStatus != HAL_OK)
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
122 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
123 I2C_Error_count();
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
124 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
125
238
a9d798e8c11f cleanup, bugfix: do not mask I2C_SystemStatus with local variable
Jan Mulder <jlmulder@xs4all.nl>
parents: 182
diff changeset
126 return global.I2C_SystemStatus;
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
127 }