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