annotate Small_CPU/Src/externalInterface.c @ 697:d55817a11f4c

Improvment key event detection: In previous version the evaluation of a button event was called in the 100ms callback. Assuming an event is signaled at the in line for 100ms there is a potential risk to loose a button event. To avoid this scenarion the evaluation function was moved into the extint callback (called ~50+xms) and the line readback tim was reduced to 40ms. Reminder: This function is not called in the main loop because the main loop is not executed in UART mode => movement would cause key events to not beeing detected
author Ideenmodellierer
date Sun, 25 Sep 2022 21:09:18 +0200
parents 52d68cf9994c
children f1b40364b0af
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"
662
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
30 #include "uart.h"
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
31 #include "data_exchange.h"
554
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
32
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
33 extern SGlobal global;
662
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
34 extern UART_HandleTypeDef huart1;
554
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
35
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
36 #define ADC_ANSWER_LENGTH (5u) /* 3424 will provide addr + 4 data bytes */
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
37 #define ADC_TIMEOUT (10u) /* conversion stuck for unknown reason => restart */
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
38 #define ADC_REF_VOLTAGE_MV (2048.0f) /* reference voltage of MPC3424*/
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
39
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
40 #define ADC_START_CONVERSION (0x80)
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
41 #define ADC_GAIN_4 (0x02)
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
42 #define ADC_GAIN_4_VALUE (4.0f)
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
43 #define ADC_GAIN_8 (0x03)
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
44 #define ADC_GAIN_8_VALUE (8.0f)
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
45 #define ADC_RESOLUTION_16BIT (0x08)
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
46 #define ADC_RESOLUTION_16BIT_VALUE (16u)
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
47 #define ADC_RESOLUTION_18BIT (0x0C)
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
48 #define ADC_RESOLUTION_18BIT_VALUE (18u)
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
49
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
50 #define ANSWER_CONFBYTE_INDEX (4u)
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
51
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
52 static uint8_t activeChannel = 0; /* channel which is in request */
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
53 static uint8_t recBuf[ADC_ANSWER_LENGTH];
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
54 static uint8_t timeoutCnt = 0;
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
55 static uint8_t externalInterfacePresent = 0;
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
56
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
57 float externalChannel_mV[MAX_ADC_CHANNEL];
662
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
58 static uint8_t externalV33_On = 0;
691
52d68cf9994c Improvment external interface usage:
Ideenmodellierer
parents: 662
diff changeset
59 static uint8_t externalADC_On = 0;
662
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
60 static uint16_t externalCO2Value;
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
61 static uint16_t externalCO2SignalStrength;
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
62 static uint16_t externalCO2Status = 0;
554
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
63
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
64
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
65 void externalInterface_Init(void)
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
66 {
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
67 activeChannel = 0;
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
68 timeoutCnt = 0;
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
69 externalInterfacePresent = 0;
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
70 if(externalInterface_StartConversion(activeChannel) == HAL_OK)
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
71 {
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
72 externalInterfacePresent = 1;
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
73 global.deviceDataSendToMaster.hw_Info.extADC = 1;
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
74 }
559
84a4e1200726 Check if HW evaluation was performed:
Ideenmodellierer
parents: 554
diff changeset
75 global.deviceDataSendToMaster.hw_Info.checkADC = 1;
662
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
76
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
77 /* init data values */
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
78 externalV33_On = 0;
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
79 externalCO2Value = 0;
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
80 externalCO2SignalStrength = 0;
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
81 externalCO2Status = 0;
554
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
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
85 uint8_t externalInterface_StartConversion(uint8_t channel)
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 = 0;
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
88 uint8_t confByte = 0;
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
89
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
90 if(channel < MAX_ADC_CHANNEL)
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
91 {
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
92 confByte = ADC_START_CONVERSION | ADC_RESOLUTION_16BIT | ADC_GAIN_8;
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
93 confByte |= channel << 5;
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
94 retval = I2C_Master_Transmit(DEVICE_EXTERNAL_ADC, &confByte, 1);
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
95 }
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
96 return retval;
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
97 }
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
98
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
99 /* Check if conversion is done and trigger measurement of next channel */
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
100 uint8_t externalInterface_ReadAndSwitch()
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
101 {
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
102 uint8_t retval = EXTERNAL_ADC_NO_DATA;
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
103
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
104 if(externalInterfacePresent)
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(I2C_Master_Receive(DEVICE_EXTERNAL_ADC, recBuf, ADC_ANSWER_LENGTH) == HAL_OK)
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
107 {
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
108 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
109 {
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
110 retval = activeChannel; /* return channel number providing new data */
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
111 activeChannel++;
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
112 if(activeChannel == MAX_ADC_CHANNEL)
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
113 {
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
114 activeChannel = 0;
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
115 }
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
116 externalInterface_StartConversion(activeChannel);
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
117 timeoutCnt = 0;
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
118 }
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
119 else
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
120 {
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
121 if(timeoutCnt++ >= ADC_TIMEOUT)
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
122 {
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
123 externalInterface_StartConversion(activeChannel);
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
124 timeoutCnt = 0;
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
125 }
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
126 }
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
127 }
581
011d8f9f5ddb Added ex Interface measurement restart in case of i2c disturbance:
Ideenmodellierer
parents: 559
diff changeset
128 else /* take also i2c bus disturb into account */
011d8f9f5ddb Added ex Interface measurement restart in case of i2c disturbance:
Ideenmodellierer
parents: 559
diff changeset
129 {
011d8f9f5ddb Added ex Interface measurement restart in case of i2c disturbance:
Ideenmodellierer
parents: 559
diff changeset
130 if(timeoutCnt++ >= ADC_TIMEOUT)
011d8f9f5ddb Added ex Interface measurement restart in case of i2c disturbance:
Ideenmodellierer
parents: 559
diff changeset
131 {
011d8f9f5ddb Added ex Interface measurement restart in case of i2c disturbance:
Ideenmodellierer
parents: 559
diff changeset
132 externalInterface_StartConversion(activeChannel);
011d8f9f5ddb Added ex Interface measurement restart in case of i2c disturbance:
Ideenmodellierer
parents: 559
diff changeset
133 timeoutCnt = 0;
011d8f9f5ddb Added ex Interface measurement restart in case of i2c disturbance:
Ideenmodellierer
parents: 559
diff changeset
134 }
011d8f9f5ddb Added ex Interface measurement restart in case of i2c disturbance:
Ideenmodellierer
parents: 559
diff changeset
135 }
554
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
136 }
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
137 return retval;
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
138 }
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
139 float externalInterface_CalculateADCValue(uint8_t channel)
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
140 {
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
141 int32_t rawvalue = 0;
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
142 float retValue = 0.0;
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
143 if(channel < MAX_ADC_CHANNEL)
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
144 {
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
145
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
146 rawvalue = ((recBuf[0] << 16) | (recBuf[1] << 8) | (recBuf[2]));
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
147
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
148 switch(recBuf[3] & 0x0C) /* confbyte => Resolution bits*/
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
149 {
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
150 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
151 if(rawvalue & (0x1 << (ADC_RESOLUTION_16BIT_VALUE-1))) /* MSB set => negative number */
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
152 {
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
153 rawvalue |= 0xFFFF0000; /* set MSB for int32 */
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
154 }
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
155 else
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
156 {
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
157 rawvalue &= 0x0000FFFF;
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
158 }
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
159 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
160 break;
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
161 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
162 {
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
163 rawvalue |= 0xFFFE0000; /* set MSB for int32 */
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
164 }
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
165 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
166 break;
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
167 default: rawvalue = 0;
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
168 break;
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
169 }
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
170 externalChannel_mV[channel] = externalChannel_mV[channel] * rawvalue / ADC_GAIN_8_VALUE;
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
171 retValue = externalChannel_mV[channel];
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
172 }
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
173 return retValue;
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
174 }
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
175 float getExternalInterfaceChannel(uint8_t channel)
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
176 {
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
177 float retval = 0;
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
178
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
179 if(channel < MAX_ADC_CHANNEL)
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
180 {
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
181 retval = externalChannel_mV[channel];
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
182 }
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
183 return retval;
3328189786e7 Added external ADC interface functionality (MCP3424):
Ideenmodellierer
parents:
diff changeset
184 }
662
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
185
691
52d68cf9994c Improvment external interface usage:
Ideenmodellierer
parents: 662
diff changeset
186 uint8_t setExternalInterfaceChannel(uint8_t channel, float value)
52d68cf9994c Improvment external interface usage:
Ideenmodellierer
parents: 662
diff changeset
187 {
52d68cf9994c Improvment external interface usage:
Ideenmodellierer
parents: 662
diff changeset
188 uint8_t retval = 0;
52d68cf9994c Improvment external interface usage:
Ideenmodellierer
parents: 662
diff changeset
189
52d68cf9994c Improvment external interface usage:
Ideenmodellierer
parents: 662
diff changeset
190 if(channel < MAX_ADC_CHANNEL)
52d68cf9994c Improvment external interface usage:
Ideenmodellierer
parents: 662
diff changeset
191 {
52d68cf9994c Improvment external interface usage:
Ideenmodellierer
parents: 662
diff changeset
192 externalChannel_mV[channel] = value;
52d68cf9994c Improvment external interface usage:
Ideenmodellierer
parents: 662
diff changeset
193 retval = 1;
52d68cf9994c Improvment external interface usage:
Ideenmodellierer
parents: 662
diff changeset
194 }
52d68cf9994c Improvment external interface usage:
Ideenmodellierer
parents: 662
diff changeset
195 return retval;
52d68cf9994c Improvment external interface usage:
Ideenmodellierer
parents: 662
diff changeset
196 }
52d68cf9994c Improvment external interface usage:
Ideenmodellierer
parents: 662
diff changeset
197
662
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
198 void externalInterface_InitPower33(void)
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
199 {
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
200 GPIO_InitTypeDef GPIO_InitStructure;
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
201
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
202 GPIO_InitStructure.Pin = GPIO_PIN_7;
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
203 GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
204 GPIO_InitStructure.Pull = GPIO_PULLUP;
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
205 GPIO_InitStructure.Speed = GPIO_SPEED_LOW;
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
206 HAL_GPIO_Init(GPIOC, &GPIO_InitStructure);
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
207 HAL_GPIO_WritePin(GPIOC,GPIO_PIN_7,GPIO_PIN_SET);
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
208 }
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
209
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
210
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
211 uint8_t externalInterface_isEnabledPower33()
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
212 {
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
213 return externalV33_On;
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
214 }
691
52d68cf9994c Improvment external interface usage:
Ideenmodellierer
parents: 662
diff changeset
215
52d68cf9994c Improvment external interface usage:
Ideenmodellierer
parents: 662
diff changeset
216 uint8_t externalInterface_isEnabledADC()
52d68cf9994c Improvment external interface usage:
Ideenmodellierer
parents: 662
diff changeset
217 {
52d68cf9994c Improvment external interface usage:
Ideenmodellierer
parents: 662
diff changeset
218 return externalADC_On;
52d68cf9994c Improvment external interface usage:
Ideenmodellierer
parents: 662
diff changeset
219 }
52d68cf9994c Improvment external interface usage:
Ideenmodellierer
parents: 662
diff changeset
220
662
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
221 void externalInterface_SwitchPower33(uint8_t state)
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
222 {
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
223 if(state != externalV33_On)
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
224 {
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
225 if(state)
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
226 {
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
227 HAL_GPIO_WritePin(GPIOC,GPIO_PIN_7,GPIO_PIN_RESET);
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
228 externalV33_On = 1;
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
229 MX_USART1_UART_Init();
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
230 }
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
231 else
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
232 {
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
233 HAL_GPIO_WritePin(GPIOC,GPIO_PIN_7,GPIO_PIN_SET);
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
234 externalV33_On = 0;
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
235 externalInterface_SetCO2Value(0);
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
236 externalInterface_SetCO2SignalStrength(0);
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
237 MX_USART1_UART_DeInit();
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
238 }
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
239 }
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
240 }
691
52d68cf9994c Improvment external interface usage:
Ideenmodellierer
parents: 662
diff changeset
241 void externalInterface_SwitchADC(uint8_t state)
52d68cf9994c Improvment external interface usage:
Ideenmodellierer
parents: 662
diff changeset
242 {
52d68cf9994c Improvment external interface usage:
Ideenmodellierer
parents: 662
diff changeset
243 if((state) && (externalInterfacePresent))
52d68cf9994c Improvment external interface usage:
Ideenmodellierer
parents: 662
diff changeset
244 {
52d68cf9994c Improvment external interface usage:
Ideenmodellierer
parents: 662
diff changeset
245 externalInterface_StartConversion(activeChannel);
52d68cf9994c Improvment external interface usage:
Ideenmodellierer
parents: 662
diff changeset
246 externalADC_On = 1;
52d68cf9994c Improvment external interface usage:
Ideenmodellierer
parents: 662
diff changeset
247 }
52d68cf9994c Improvment external interface usage:
Ideenmodellierer
parents: 662
diff changeset
248 else
52d68cf9994c Improvment external interface usage:
Ideenmodellierer
parents: 662
diff changeset
249 {
52d68cf9994c Improvment external interface usage:
Ideenmodellierer
parents: 662
diff changeset
250 externalADC_On = 0;
52d68cf9994c Improvment external interface usage:
Ideenmodellierer
parents: 662
diff changeset
251 }
52d68cf9994c Improvment external interface usage:
Ideenmodellierer
parents: 662
diff changeset
252 }
662
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
253
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
254 void externalInterface_SetCO2Value(uint16_t CO2_ppm)
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
255 {
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
256 externalCO2Value = CO2_ppm;
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
257 }
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
258
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
259 void externalInterface_SetCO2SignalStrength(uint16_t LED_qa)
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
260 {
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
261 externalCO2SignalStrength = LED_qa;
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
262 }
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
263
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
264 uint16_t externalInterface_GetCO2Value(void)
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
265 {
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
266 return externalCO2Value;
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
267 }
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
268
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
269 uint16_t externalInterface_GetCO2SignalStrength(void)
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
270 {
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
271 return externalCO2SignalStrength;
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
272 }
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
273
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
274
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
275 void externalInterface_SetCO2State(uint16_t state)
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
276 {
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
277 externalCO2Status = state;
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
278 }
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
279
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
280 uint16_t externalInterface_GetCO2State(void)
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
281 {
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
282 return externalCO2Status;
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
283 }
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
284
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
285 void externalInterface_ExecuteCmd(uint16_t Cmd)
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
286 {
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
287 char cmdString[10];
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
288 uint8_t cmdLength = 0;
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
289
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
290 switch(Cmd & 0x00FF) /* lower byte is reserved for commands */
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
291 {
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
292 case EXT_INTERFACE_CO2_CALIB: cmdLength = snprintf(cmdString, 10, "G\r\n");
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
293 break;
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
294 default:
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
295 break;
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
296 }
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
297 if(cmdLength != 0)
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
298 {
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
299 HAL_UART_Transmit(&huart1,(uint8_t*)cmdString,cmdLength,10);
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
300 }
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
301 return;
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
302 }
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 581
diff changeset
303