annotate Small_CPU/Src/externalInterface.c @ 554:3328189786e7

Added external ADC interface functionality (MCP3424): Added communication protocoll to read data from external ADC. At the moment 16bit and 18bit resolutions are supported. External data will be read and forwarded depending on the conversion time of the ADC.
author Ideenmodellierer
date Thu, 12 Nov 2020 19:46:03 +0100
parents
children 84a4e1200726
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
554
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
1 /**
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
2 ******************************************************************************
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
3 * @file externalInterface.c
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
4 * @author heinrichs weikamp gmbh
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
5 * @version V0.0.1
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
6 * @date 07-Nov-2020
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
7 * @brief Interface functionality to proceed external analog signal via i2c connection
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
8 *
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
9 @verbatim
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
10 ==============================================================================
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
11 ##### stm32f4xx_hal_i2c.c modification #####
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
12 ==============================================================================
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
13 The LTC2942 requires an repeated start condition without stop condition
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
14 for data reception.
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
15
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
16 @endverbatim
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
17 ******************************************************************************
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
18 * @attention
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
19 *
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
20 * <h2><center>&copy; COPYRIGHT(c) 2014 heinrichs weikamp</center></h2>
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
21 *
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
22 ******************************************************************************
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
23 */
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
24 /* Includes ------------------------------------------------------------------*/
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
25
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
26 #include <math.h>
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
27 #include "i2c.h"
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
28 #include "externalInterface.h"
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
29 #include "scheduler.h"
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
30
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
31 extern SGlobal global;
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
32
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
33 #define ADC_ANSWER_LENGTH (5u) /* 3424 will provide addr + 4 data bytes */
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
34 #define ADC_TIMEOUT (10u) /* conversion stuck for unknown reason => restart */
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
35 #define ADC_REF_VOLTAGE_MV (2048.0f) /* reference voltage of MPC3424*/
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
36
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
37 #define ADC_START_CONVERSION (0x80)
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
38 #define ADC_GAIN_4 (0x02)
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
39 #define ADC_GAIN_4_VALUE (4.0f)
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
40 #define ADC_GAIN_8 (0x03)
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
41 #define ADC_GAIN_8_VALUE (8.0f)
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
42 #define ADC_RESOLUTION_16BIT (0x08)
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
43 #define ADC_RESOLUTION_16BIT_VALUE (16u)
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
44 #define ADC_RESOLUTION_18BIT (0x0C)
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
45 #define ADC_RESOLUTION_18BIT_VALUE (18u)
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
46
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
47 #define ANSWER_CONFBYTE_INDEX (4u)
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
48
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
49 static uint8_t activeChannel = 0; /* channel which is in request */
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
50 static uint8_t recBuf[ADC_ANSWER_LENGTH];
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
51 static uint8_t timeoutCnt = 0;
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
52 static uint8_t externalInterfacePresent = 0;
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
53
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
54 float externalChannel_mV[MAX_ADC_CHANNEL];
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
55
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
56
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
57 void externalInterface_Init(void)
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
58 {
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
59 activeChannel = 0;
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
60 timeoutCnt = 0;
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
61 externalInterfacePresent = 0;
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
62 if(externalInterface_StartConversion(activeChannel) == HAL_OK)
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
63 {
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
64 externalInterfacePresent = 1;
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
65 global.deviceDataSendToMaster.hw_Info.extADC = 1;
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
66 }
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
67 }
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
68
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
69
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
70 uint8_t externalInterface_StartConversion(uint8_t channel)
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
71 {
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
72 uint8_t retval = 0;
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
73 uint8_t confByte = 0;
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
74
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
75 if(channel < MAX_ADC_CHANNEL)
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
76 {
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
77 confByte = ADC_START_CONVERSION | ADC_RESOLUTION_16BIT | ADC_GAIN_8;
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
78 confByte |= channel << 5;
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
79 retval = I2C_Master_Transmit(DEVICE_EXTERNAL_ADC, &confByte, 1);
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
80 }
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
81 return retval;
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
82 }
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
83
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
84 /* Check if conversion is done and trigger measurement of next channel */
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
85 uint8_t externalInterface_ReadAndSwitch()
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
86 {
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
87 uint8_t retval = EXTERNAL_ADC_NO_DATA;
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
88
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
89 if(externalInterfacePresent)
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
90 {
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
91 if(I2C_Master_Receive(DEVICE_EXTERNAL_ADC, recBuf, ADC_ANSWER_LENGTH) == HAL_OK)
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
92 {
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
93 if((recBuf[ANSWER_CONFBYTE_INDEX] & ADC_START_CONVERSION) == 0) /* !ready set => received data contains new value */
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
94 {
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
95 retval = activeChannel; /* return channel number providing new data */
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
96 activeChannel++;
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
97 if(activeChannel == MAX_ADC_CHANNEL)
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
98 {
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
99 activeChannel = 0;
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
100 }
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
101 externalInterface_StartConversion(activeChannel);
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
102 timeoutCnt = 0;
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
103 }
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
104 else
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
105 {
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
106 if(timeoutCnt++ >= ADC_TIMEOUT)
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
107 {
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
108 externalInterface_StartConversion(activeChannel);
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
109 timeoutCnt = 0;
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
110 }
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
111 }
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
112 }
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
113 }
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
114 return retval;
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
115 }
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
116 float externalInterface_CalculateADCValue(uint8_t channel)
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
117 {
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
118 int32_t rawvalue = 0;
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
119 float retValue = 0.0;
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
120 if(channel < MAX_ADC_CHANNEL)
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
121 {
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
122
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
123 rawvalue = ((recBuf[0] << 16) | (recBuf[1] << 8) | (recBuf[2]));
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
124
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
125 switch(recBuf[3] & 0x0C) /* confbyte => Resolution bits*/
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
126 {
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
127 case ADC_RESOLUTION_16BIT: rawvalue = rawvalue >> 8; /* only 2 databytes received shift out confbyte*/
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
128 if(rawvalue & (0x1 << (ADC_RESOLUTION_16BIT_VALUE-1))) /* MSB set => negative number */
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
129 {
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
130 rawvalue |= 0xFFFF0000; /* set MSB for int32 */
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
131 }
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
132 else
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
133 {
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
134 rawvalue &= 0x0000FFFF;
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
135 }
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
136 externalChannel_mV[channel] = ADC_REF_VOLTAGE_MV * 2.0 / (float) pow(2,ADC_RESOLUTION_16BIT_VALUE); /* calculate bit resolution */
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
137 break;
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
138 case ADC_RESOLUTION_18BIT: if(rawvalue & (0x1 << (ADC_RESOLUTION_18BIT_VALUE-1))) /* MSB set => negative number */
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
139 {
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
140 rawvalue |= 0xFFFE0000; /* set MSB for int32 */
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
141 }
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
142 externalChannel_mV[channel] = ADC_REF_VOLTAGE_MV * 2.0 / (float) pow(2,ADC_RESOLUTION_18BIT_VALUE); /* calculate bit resolution */
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
143 break;
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
144 default: rawvalue = 0;
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
145 break;
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
146 }
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
147 externalChannel_mV[channel] = externalChannel_mV[channel] * rawvalue / ADC_GAIN_8_VALUE;
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
148 retValue = externalChannel_mV[channel];
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
149 }
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
150 return retValue;
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
151 }
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
152 float getExternalInterfaceChannel(uint8_t channel)
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
153 {
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
154 float retval = 0;
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
155
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
156 if(channel < MAX_ADC_CHANNEL)
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
157 {
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
158 retval = externalChannel_mV[channel];
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
159 }
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
160 return retval;
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
161 }