annotate Small_CPU/Src/i2c.c @ 296:87f83879cecb cleanup-3

Possible bugfix: do not use bitwise and (&) This fixes a possible bug. A bitwise and (&) was used instead of the intended boolean and (&&). I found this while code reading, and I'm not aware of any user visible bug related to this, but it feels wrong and possibly dangerous to use this construct here. Signed-off-by: Jan Mulder <jlmulder@xs4all.nl>
author Jan Mulder <jlmulder@xs4all.nl>
date Thu, 16 May 2019 08:54:33 +0200
parents 2b9775f71e30
children 4fe5400567e7
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);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
34 HAL_Delay(10);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
35 HAL_GPIO_WritePin(I2Cx_SCL_GPIO_PORT, I2Cx_SCL_PIN, GPIO_PIN_SET);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
36 HAL_Delay(10);
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 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
41 I2C_DeInit();
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
42 HAL_I2C_ManualControl_MspInit();
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
43 for(int i=0; i<9;i++)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
44 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
45 if(HAL_I2C_Read_Data_PIN() == GPIO_PIN_RESET)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
46 HAL_I2C_Send_One_CLOCK();
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
47 else
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
48 break;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
49 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
50 return HAL_I2C_Read_Data_PIN();
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
51 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
52
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
53 HAL_StatusTypeDef MX_I2C1_Init(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
54 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
55 I2cHandle.Instance = I2Cx;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
56 I2cHandle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
104
22a1094545f3 Tested and alive.
Dmitry Romanov <kitt@bk.ru>
parents: 100
diff changeset
57 I2cHandle.Init.ClockSpeed = 100000;//400000; REDUCED for compatibility with HMC5583L + MMA8452Q
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
58 I2cHandle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLED;
104
22a1094545f3 Tested and alive.
Dmitry Romanov <kitt@bk.ru>
parents: 100
diff changeset
59 I2cHandle.Init.DutyCycle = I2C_DUTYCYCLE_2;
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
60 I2cHandle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLED;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
61 I2cHandle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLED;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
62 I2cHandle.Init.OwnAddress1 = 0x01;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
63
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
64 global.I2C_SystemStatus = HAL_I2C_Init(&I2cHandle);
82
a6f0881074a4 +i2c analog noise filtering
Dmitry Romanov <kitt@bk.ru>
parents: 38
diff changeset
65 HAL_I2CEx_AnalogFilter_Config(&I2cHandle, I2C_ANALOGFILTER_ENABLED);
100
b364c75005bb Stable.fix.
Dmitry Romanov <kitt@bk.ru>
parents: 94
diff changeset
66 HAL_I2CEx_ConfigDigitalFilter(&I2cHandle,0x0F);
b364c75005bb Stable.fix.
Dmitry Romanov <kitt@bk.ru>
parents: 94
diff changeset
67
b364c75005bb Stable.fix.
Dmitry Romanov <kitt@bk.ru>
parents: 94
diff changeset
68 if(global.dataSendToSlavePending)
b364c75005bb Stable.fix.
Dmitry Romanov <kitt@bk.ru>
parents: 94
diff changeset
69 {
b364c75005bb Stable.fix.
Dmitry Romanov <kitt@bk.ru>
parents: 94
diff changeset
70 scheduleSpecial_Evaluate_DataSendToSlave();
b364c75005bb Stable.fix.
Dmitry Romanov <kitt@bk.ru>
parents: 94
diff changeset
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
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
73 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
74
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
75
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
76 void I2C_DeInit(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
77 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
78 HAL_I2C_DeInit(&I2cHandle);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
79 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
80
239
e4207f0aaa4b cleanup: factor out dataSendToSlaveStopEval
Jan Mulder <jlmulder@xs4all.nl>
parents: 238
diff changeset
81 static uint8_t i2c_errors = 0;
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
82
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
83 void I2C_Error_count(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
84 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
85 i2c_errors++;
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 HAL_StatusTypeDef I2C_Master_Transmit( uint16_t DevAddress, uint8_t *pData, uint16_t Size)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
89 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
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
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
92
182
7e749084f347 Resolve fixme comments
ideenmodellierer
parents: 165
diff changeset
93 global.I2C_SystemStatus = HAL_I2C_Master_Transmit(&I2cHandle, DevAddress, pData, Size, 2);
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
94 if(global.I2C_SystemStatus != HAL_OK)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
95 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
96 I2C_Error_count();
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
97 }
88
Dmitry Romanov <kitt@bk.ru>
parents: 84
diff changeset
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
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
100 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
101
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
102 HAL_StatusTypeDef I2C_Master_Receive( uint16_t DevAddress, uint8_t *pData, uint16_t Size)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
103 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
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
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
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
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
109 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
110 I2C_Error_count();
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
111 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
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
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
114 }