-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
288 lines (251 loc) · 13.6 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/**********************************************************************************************************************
* \file main.c
* \copyright Copyright (C) Infineon Technologies AG 2024
*
* Use of this file is subject to the terms of use agreed between (i) you or the company in which ordinary course of
* business you are acting and (ii) Infineon Technologies AG or its licensees. If and as long as no such terms of use
* are agreed, use of this file is subject to following:
*
* Boost Software License - Version 1.0 - August 17th, 2003
*
* Permission is hereby granted, free of charge, to any person or organization obtaining a copy of the software and
* accompanying documentation covered by this license (the "Software") to use, reproduce, display, distribute, execute,
* and transmit the Software, and to prepare derivative works of the Software, and to permit third-parties to whom the
* Software is furnished to do so, all subject to the following:
*
* The copyright notices in the Software and this entire statement, including the above license grant, this restriction
* and the following disclaimer, must be included in all copies of the Software, in whole or in part, and all
* derivative works of the Software, unless such copies or derivative works are solely in the form of
* machine-executable object code generated by a source language processor.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*********************************************************************************************************************/
/*********************************************************************************************************************/
/*-----------------------------------------------------Includes------------------------------------------------------*/
/*********************************************************************************************************************/
#include "cybsp.h"
#include "cy_retarget_io.h"
/*********************************************************************************************************************/
/*------------------------------------------------------Macros-------------------------------------------------------*/
/*********************************************************************************************************************/
/* ADC interrupt priority */
#define IRQ_PRIORITY 2
/* Shift value for CPU IRQ number ('intSrc' of cy_stc_sysint_t consists of CPU IRQ number and system IRQ number) */
#define CPU_IRQ_NUMBER_SHIFT 16
/* ADC voltage levels */
#define ADC_4_5V_LEVEL ((uint16_t) (4095 * 4.5 / 5))
#define ADC_3_5V_LEVEL ((uint16_t) (4095 * 3.5 / 5))
#define ADC_2_5V_LEVEL ((uint16_t) (4095 * 2.5 / 5))
#define ADC_1_5V_LEVEL ((uint16_t) (4095 * 1.5 / 5))
#define ADC_0_5V_LEVEL ((uint16_t) (4095 * 0.5 / 5))
/*********************************************************************************************************************/
/*-------------------------------------------------Global variables--------------------------------------------------*/
/*********************************************************************************************************************/
/* Flag set when the ADC output voltage is out of range */
bool g_rangeDetected = false;
/*********************************************************************************************************************/
/*---------------------------------------------Function Implementations----------------------------------------------*/
/*********************************************************************************************************************/
/**********************************************************************************************************************
* Function Name: handleAdcIrq
* Summary:
* ADC Interrupt Handler
* Parameters:
* none
* Return:
* none
**********************************************************************************************************************
*/
void handleAdcIrq(void)
{
/* Get interrupt source */
uint32_t irqStatus = Cy_SAR2_Channel_GetInterruptStatusMasked(SARADC_HW, SARADC_RangeDetectionChannel_IDX);
if (irqStatus == CY_SAR2_INT_CH_RANGE)
{
/* Set a flag to inform the main loop of the event */
g_rangeDetected = true;
/* Clear interrupt source */
Cy_SAR2_Channel_ClearInterrupt(SARADC_HW, SARADC_RangeDetectionChannel_IDX, CY_SAR2_INT_CH_RANGE);
}
else
{
/* Unexpected interrupt */
CY_ASSERT(false);
}
}
/**********************************************************************************************************************
* Function Name: configureAdcRangeDetectionMode
* Summary:
* Function to configure ADC range detection parameters, initialize SAR ADC module, enable interrupt and start
* conversion.
* Parameters:
* detectionMode - range detection mode selection
* upperThreshold - value for upper threshold
* lowerThreshold - value for lower threshold
* Return:
* none
**********************************************************************************************************************
*/
static void configureAdcRangeDetectionMode(cy_en_sar2_range_detection_mode_t detectionMode,
uint16_t upperThreshold, uint16_t lowerThreshold)
{
/* SAR block is disabled for setting up new range detection parameters */
Cy_SAR2_Disable(SARADC_HW);
SARADC_RangeDetectionChannel_config.rangeDetectionMode = detectionMode;
SARADC_RangeDetectionChannel_config.rangeDetectionHiThreshold = upperThreshold;
SARADC_RangeDetectionChannel_config.rangeDetectionLoThreshold = lowerThreshold;
/* Initialize the SAR module */
Cy_SAR2_Init(SARADC_HW, &SARADC_config);
/* (Re-)Enable SAR block */
Cy_SAR2_Enable(SARADC_HW);
/* Set up the interrupt processing for ADC */
cy_stc_sysint_t irqCfg =
{
.intrSrc = ((NvicMux3_IRQn << CPU_IRQ_NUMBER_SHIFT) | SARADC_RangeDetectionChannel_IRQ),
.intrPriority = IRQ_PRIORITY,
};
if (Cy_SysInt_Init(&irqCfg, &handleAdcIrq) != CY_SYSINT_SUCCESS)
{
CY_ASSERT(0);
}
NVIC_EnableIRQ((IRQn_Type) NvicMux3_IRQn);
Cy_SAR2_Channel_SetInterruptMask(SARADC_HW, SARADC_RangeDetectionChannel_IDX, CY_SAR2_INT_CH_RANGE);
/* Start first ADC conversion, the ADC will then be re-triggered by hardware in continuous mode */
Cy_SAR2_Channel_SoftwareTrigger(SARADC_HW, SARADC_RangeDetectionChannel_IDX);
}
/**********************************************************************************************************************
* Function Name: main
* Summary:
* This is the main function. It initializes the BSP and the retarget-io library.
* Later, it shows a menu and prompts the user to select a test case to run.
* Parameters:
* none
* Return:
* int
**********************************************************************************************************************
*/
int main(void)
{
/* Variable to store the received character through terminal */
uint8_t uartReadValue;
/* Initialize the device and board peripherals */
if (cybsp_init() != CY_RSLT_SUCCESS)
{
CY_ASSERT(0);
}
/* Initialize retarget-io to use the debug UART port */
Cy_SCB_UART_Init(UART_HW, &UART_config, NULL);
Cy_SCB_UART_Enable(UART_HW);
cy_retarget_io_init(UART_HW);
/* Initialize the SAR module */
uint16_t upperThreshold = ADC_3_5V_LEVEL;
uint16_t lowerThreshold = ADC_1_5V_LEVEL;
cy_en_sar2_range_detection_mode_t rangeDetectionMode = CY_SAR2_RANGE_DETECTION_MODE_OUTSIDE_RANGE;
configureAdcRangeDetectionMode(rangeDetectionMode, upperThreshold, lowerThreshold);
/* Enable global interrupts */
__enable_irq();
/* \x1b[2J\x1b[;H - ANSI ESC sequence for clear screen */
printf("\x1b[2J\x1b[;H");
printf("***********************************************************\r\n");
printf("SAR ADC Range Detection\r\n");
printf("***********************************************************\r\n");
printf(">> Select one of the options as listed below\r\n");
printf(">> 0 : Upper Range Threshold : 4.5V / Lower Range Threshold : 3.5V\r\n");
printf(">> 1 : Upper Range Threshold : 4.5V / Lower Range Threshold : 2.5V\r\n");
printf(">> 2 : Upper Range Threshold : 4.5V / Lower Range Threshold : 1.5V\r\n");
printf(">> 3 : Upper Range Threshold : 4.5V / Lower Range Threshold : 0.5V\r\n");
printf(">> 4 : Upper Range Threshold : 3.5V / Lower Range Threshold : 1.5V\r\n");
printf(">> 5 : Upper Range Threshold : 2.5V / Lower Range Threshold : 0.5V\r\n");
printf(">> a : Above Upper Threshold Detection\r\n");
printf(">> b : Below Lower Threshold Detection\r\n");
printf(">> i : Inside Range Detection\r\n");
printf(">> o : Outside Range Detection\r\n\r\n");
for (;;)
{
/* Set user led port pin to high when range condition was detected in the ADC interrupt handler */
if (g_rangeDetected)
{
g_rangeDetected = false;
Cy_GPIO_Set(CYBSP_USER_LED_PORT, CYBSP_USER_LED_PIN);
Cy_SysLib_Delay(200);
}
/* Set user led port pin to low when no range condition was detected */
else
{
Cy_GPIO_Clr(CYBSP_USER_LED_PORT, CYBSP_USER_LED_PIN);
}
/* Check if key was pressed and change range detection parameter accordingly */
uartReadValue = Cy_SCB_UART_Get(UART_HW);
if(uartReadValue != 0xff)
{
switch (uartReadValue)
{
/* Change upper and lower threshold of range detection */
case '0':
printf("Upper Threshold Set to 4.5V - Lower Threshold Set to 3.5V\r\n");
upperThreshold = ADC_4_5V_LEVEL;
lowerThreshold = ADC_3_5V_LEVEL;
configureAdcRangeDetectionMode(rangeDetectionMode, upperThreshold, lowerThreshold);
break;
case '1':
printf("Upper Threshold Set to 4.5V - Lower Threshold Set to 2.5V\r\n");
upperThreshold = ADC_4_5V_LEVEL;
lowerThreshold = ADC_2_5V_LEVEL;
configureAdcRangeDetectionMode(rangeDetectionMode, upperThreshold, lowerThreshold);
break;
case '2':
printf("Upper Threshold Set to 4.5V - Lower Threshold Set to 1.5V\r\n");
upperThreshold = ADC_4_5V_LEVEL;
lowerThreshold = ADC_1_5V_LEVEL;
configureAdcRangeDetectionMode(rangeDetectionMode, upperThreshold, lowerThreshold);
break;
case '3':
printf("Upper Threshold Set to 4.5V - Lower Threshold Set to 0.5V\r\n");
upperThreshold = ADC_4_5V_LEVEL;
lowerThreshold = ADC_0_5V_LEVEL;
configureAdcRangeDetectionMode(rangeDetectionMode, upperThreshold, lowerThreshold);
break;
case '4':
printf("Upper Threshold Set to 3.5V - Lower Threshold Set to 1.5V\r\n");
upperThreshold = ADC_3_5V_LEVEL;
lowerThreshold = ADC_1_5V_LEVEL;
configureAdcRangeDetectionMode(rangeDetectionMode, upperThreshold, lowerThreshold);
break;
case '5':
printf("Upper Threshold Set to 2.5V - Lower Threshold Set to 0.5V\r\n");
upperThreshold = ADC_2_5V_LEVEL;
lowerThreshold = ADC_0_5V_LEVEL;
configureAdcRangeDetectionMode(rangeDetectionMode, upperThreshold, lowerThreshold);
break;
/* Change range detection mode */
case 'a':
printf("Above Upper Threshold Detection activated\r\n");
rangeDetectionMode = CY_SAR2_RANGE_DETECTION_MODE_ABOVE_HI;
configureAdcRangeDetectionMode(rangeDetectionMode, upperThreshold, lowerThreshold);
break;
case 'b':
printf("Below Lower Threshold Detection activated\r\n");
rangeDetectionMode = CY_SAR2_RANGE_DETECTION_MODE_BELOW_LO;
configureAdcRangeDetectionMode(rangeDetectionMode, upperThreshold, lowerThreshold);
break;
case 'i':
printf("Inside Range Detection activated\r\n");
rangeDetectionMode = CY_SAR2_RANGE_DETECTION_MODE_INSIDE_RANGE;
configureAdcRangeDetectionMode(rangeDetectionMode, upperThreshold, lowerThreshold);
break;
case 'o':
printf("Outside Range Detection activated\r\n");
rangeDetectionMode = CY_SAR2_RANGE_DETECTION_MODE_OUTSIDE_RANGE;
configureAdcRangeDetectionMode(rangeDetectionMode, upperThreshold, lowerThreshold);
break;
default:
break;
}
}
}
}
/* [] END OF FILE */