-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.c
493 lines (416 loc) · 17.4 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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
/******************************************************************************
* File Name: main.c
*
* Description: In this code example, ADC HAL (Hardware Abstraction Layer) driver
* is configured to sample input voltage and the sampled input
* voltage is displayed on the UART.
*
* Related Document: See README.md
*
*
*******************************************************************************
* Copyright 2020-2024, Cypress Semiconductor Corporation (an Infineon company) or
* an affiliate of Cypress Semiconductor Corporation. All rights reserved.
*
* This software, including source code, documentation and related
* materials ("Software") is owned by Cypress Semiconductor Corporation
* or one of its affiliates ("Cypress") and is protected by and subject to
* worldwide patent protection (United States and foreign),
* United States copyright laws and international treaty provisions.
* Therefore, you may use this Software only as provided in the license
* agreement accompanying the software package from which you
* obtained this Software ("EULA").
* If no EULA applies, Cypress hereby grants you a personal, non-exclusive,
* non-transferable license to copy, modify, and compile the Software
* source code solely for use in connection with Cypress's
* integrated circuit products. Any reproduction, modification, translation,
* compilation, or representation of this Software except as specified
* above is prohibited without the express written permission of Cypress.
*
* Disclaimer: THIS SOFTWARE IS PROVIDED AS-IS, WITH NO WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, NONINFRINGEMENT, IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. Cypress
* reserves the right to make changes to the Software without notice. Cypress
* does not assume any liability arising out of the application or use of the
* Software or any product or circuit described in the Software. Cypress does
* not authorize its products for use in any products where a malfunction or
* failure of the Cypress product may reasonably be expected to result in
* significant property damage, injury or death ("High Risk Product"). By
* including Cypress's product in a High Risk Product, the manufacturer
* of such system or application assumes all risk of such use and in doing
* so agrees to indemnify Cypress against all liability.
*******************************************************************************/
#include "cy_pdl.h"
#include "cyhal.h"
#include "cybsp.h"
#include "cy_retarget_io.h"
/*******************************************************************************
* Macros
*******************************************************************************/
/* Macro for ADC Channel configuration*/
#define SINGLE_CHANNEL 1
#define MULTI_CHANNEL 2
/*
* Macro to choose between single channel and multiple channel configuration of
* ADC. Single channel configuration uses channel 0 in single ended mode.
* Multiple channel configuration uses two channels, channel 0 in single ended
* mode and channel 1 in differential mode.
*
* The default configuration is set to use single channel.
* To use multiple channel configuration set ADC_EXAMPLE_MODE macro to MULTI_CHANNEL.
*
*/
#define ADC_EXAMPLE_MODE SINGLE_CHANNEL
#if defined(CY_DEVICE_PSOC6A512K) /* if the target is CY8CPROTO-062S3-4343W or CY8CPROTO-064B0S3*/
/* Channel 0 input pin */
#define VPLUS_CHANNEL_0 (P10_3)
#else
#define VPLUS_CHANNEL_0 (P10_0)
#endif
#if ADC_EXAMPLE_MODE == MULTI_CHANNEL
#if defined(CY_DEVICE_PSOC6A256K) /* if the target is CY8CKIT-062S4 */
/* Channel 1 VPLUS input pin */
#define VPLUS_CHANNEL_1 (P10_1)
/* Channel 1 VREF input pin */
#define VREF_CHANNEL_1 (P10_2)
#else
/* Channel 1 VPLUS input pin */
#define VPLUS_CHANNEL_1 (P10_4)
/* Channel 1 VREF input pin */
#define VREF_CHANNEL_1 (P10_5)
#endif
/* Number of scans every time ADC read is initiated */
#define NUM_SCAN (1)
#endif /* ADC_EXAMPLE_MODE == MULTI_CHANNEL */
/* Conversion factor */
#define MICRO_TO_MILLI_CONV_RATIO (int32_t)(1000u)
/* Acquistion time in nanosecond */
#define ACQUISITION_TIME_NS (1000u)
/* ADC Scan delay in millisecond */
#define ADC_SCAN_DELAY_MS (200u)
/*******************************************************************************
* Enumerated Types
*******************************************************************************/
/* ADC Channel constants*/
enum ADC_CHANNELS
{
CHANNEL_0 = 0,
CHANNEL_1,
NUM_CHANNELS
} adc_channel;
/*******************************************************************************
* Function Prototypes
*******************************************************************************/
#if ADC_EXAMPLE_MODE == MULTI_CHANNEL
/* Multichannel initialization function */
void adc_multi_channel_init(void);
/* Function to read input voltage from multiple channels */
void adc_multi_channel_process(void);
/* ADC Event Handler */
static void adc_event_handler(void* arg, cyhal_adc_event_t event);
#else /* ADC_EXAMPLE_MODE == SINGLE_CHANNEL */
/* Single channel initialization function*/
void adc_single_channel_init(void);
/* Function to read input voltage from channel 0 */
void adc_single_channel_process(void);
#endif /* ADC_EXAMPLE_MODE == MULTI_CHANNEL */
/*******************************************************************************
* Global Variables
*******************************************************************************/
/* ADC Object */
cyhal_adc_t adc_obj;
/* ADC Channel 0 Object */
cyhal_adc_channel_t adc_chan_0_obj;
/* Default ADC configuration */
const cyhal_adc_config_t adc_config = {
.continuous_scanning=false, // Continuous Scanning is disabled
.average_count=1, // Average count disabled
.vref=CYHAL_ADC_REF_VDDA, // VREF for Single ended channel set to VDDA
.vneg=CYHAL_ADC_VNEG_VSSA, // VNEG for Single ended channel set to VSSA
.resolution = 12u, // 12-bit resolution
.ext_vref = NC, // No connection
.bypass_pin = NC }; // No connection
#if ADC_EXAMPLE_MODE == MULTI_CHANNEL
/* Asynchronous read complete flag, used in Event Handler */
static bool async_read_complete = false;
/* ADC Channel 1 Object */
cyhal_adc_channel_t adc_chan_1_obj;
/* Variable to store results from multiple channels during asynchronous read*/
int32_t result_arr[NUM_CHANNELS * NUM_SCAN] = {0};
#endif /* ADC_EXAMPLE_MODE == MULTI_CHANNEL */
/*******************************************************************************
* Function Name: main
********************************************************************************
* Summary:
* This is the main function for CM4 CPU. It does...
* 1. Configure and initialize ADC.
* 2. Every 200ms read the input voltage and display input voltage on UART.
*
* Parameters:
* none
*
* Return:
* int
*
*******************************************************************************/
int main(void)
{
/* Variable to capture return value of functions */
cy_rslt_t result;
#if defined(CY_DEVICE_SECURE)
cyhal_wdt_t wdt_obj;
/* Clear watchdog timer so that it doesn't trigger a reset */
result = cyhal_wdt_init(&wdt_obj, cyhal_wdt_get_max_timeout_ms());
CY_ASSERT(CY_RSLT_SUCCESS == result);
cyhal_wdt_free(&wdt_obj);
#endif
/* Initialize the device and board peripherals */
result = cybsp_init();
/* Board init failed. Stop program execution */
if (result != CY_RSLT_SUCCESS)
{
CY_ASSERT(0);
}
/* Enable global interrupts */
__enable_irq();
/* Initialize retarget-io to use the debug UART port */
result = cy_retarget_io_init(CYBSP_DEBUG_UART_TX, CYBSP_DEBUG_UART_RX,
CY_RETARGET_IO_BAUDRATE);
/* retarget-io init failed. Stop program execution */
if (result != CY_RSLT_SUCCESS)
{
CY_ASSERT(0);
}
/* Print message */
/* \x1b[2J\x1b[;H - ANSI ESC sequence for clear screen */
printf("\x1b[2J\x1b[;H");
printf("-----------------------------------------------------------\r\n");
printf("HAL: ADC using HAL\r\n");
printf("-----------------------------------------------------------\r\n\n");
#if ADC_EXAMPLE_MODE == MULTI_CHANNEL
/* Initialize Channel 0 and Channel 1 */
adc_multi_channel_init();
#else /* ADC_EXAMPLE_MODE == SINGLE_CHANNEL */
/* Initialize Channel 0 */
adc_single_channel_init();
#endif /* ADC_EXAMPLE_MODE == MULTI_CHANNEL */
/* Update ADC configuration */
result = cyhal_adc_configure(&adc_obj, &adc_config);
if(result != CY_RSLT_SUCCESS)
{
printf("ADC configuration update failed. Error: %ld\n", (long unsigned int)result);
CY_ASSERT(0);
}
for (;;)
{
#if ADC_EXAMPLE_MODE == MULTI_CHANNEL
/* Sample input voltage at channel 0 and channel 1*/
adc_multi_channel_process();
#else /* ADC_EXAMPLE_MODE == SINGLE_CHANNEL */
/* Sample input voltage at channel 0 */
adc_single_channel_process();
#endif /* ADC_EXAMPLE_MODE == MULTI_CHANNEL */
/* 200ms delay between scans */
cyhal_system_delay_ms(ADC_SCAN_DELAY_MS);
}
}
#if ADC_EXAMPLE_MODE == MULTI_CHANNEL
/*******************************************************************************
* Function Name: adc_multi_channel_init
*******************************************************************************
*
* Summary:
* ADC Multichannel initilization. This function initializes and configures
* channel 0 and channel 1 of ADC.
*
* Parameters:
* void
*
* Return:
* void
*
*******************************************************************************/
void adc_multi_channel_init(void)
{
/* Variable to capture return value of functions */
cy_rslt_t result;
/* Initialize ADC. The ADC block which can connect to the channel 0 input pin is selected */
result = cyhal_adc_init(&adc_obj, VPLUS_CHANNEL_0, NULL);
if(result != CY_RSLT_SUCCESS)
{
printf("ADC initialization failed. Error: %ld\n", (long unsigned int)result);
CY_ASSERT(0);
}
/* ADC channel configuration */
const cyhal_adc_channel_config_t channel_config = {
.enable_averaging = false, // Disable averaging for channel
.min_acquisition_ns = ACQUISITION_TIME_NS, // Minimum acquisition time set to 1us
.enabled = true }; // Sample this channel when ADC performs a scan
/* Initialize a channel 0 and configure it to scan the channel 0 input pin in single ended mode. */
result = cyhal_adc_channel_init_diff(&adc_chan_0_obj, &adc_obj, VPLUS_CHANNEL_0,
CYHAL_ADC_VNEG, &channel_config);
if(result != CY_RSLT_SUCCESS)
{
printf("ADC first channel initialization failed. Error: %ld\n", (long unsigned int)result);
CY_ASSERT(0);
}
/*
* For multichannel configuration use to same channel configuration structure
* "channel_config" to configure the second channel.
* For second channel to be set to differential mode, two inputs from Pins
* channel 1 input pin and channel 1 voltage reference pin are configured to be inputs.
*
*/
/* Initialize channel 1 in differential mode with VPLUS and VMINUS input pins */
result = cyhal_adc_channel_init_diff(&adc_chan_1_obj, &adc_obj, VPLUS_CHANNEL_1,
VREF_CHANNEL_1, &channel_config);
if(result != CY_RSLT_SUCCESS)
{
printf("ADC second channel initialization failed. Error: %ld\n", (long unsigned int)result);
CY_ASSERT(0);
}
/* Register a callback to handle asynchronous read completion */
cyhal_adc_register_callback(&adc_obj, &adc_event_handler, result_arr);
/* Subscribe to the async read complete event to process the results */
cyhal_adc_enable_event(&adc_obj, CYHAL_ADC_ASYNC_READ_COMPLETE, CYHAL_ISR_PRIORITY_DEFAULT, true);
printf("ADC is configured in multichannel configuration.\r\n\n");
printf("Channel 0 is configured in single ended mode, connected to the \r\n");
printf("channel 0 input pin. Provide input voltage at the channel 0 input pin \r\n");
printf("Channel 1 is configured in differential mode, connected to the \r\n");
printf("channel 1 input pin and channel 1 voltage reference pin. Provide input voltage at the channel 1 input pin and reference \r\n");
printf("voltage at the Channel 1 voltage reference pin \r\n\n");
}
/*******************************************************************************
* Function Name: adc_multi_channel_process
*******************************************************************************
*
* Summary:
* ADC single channel process function. This function reads the input voltage
* from channel 0 and channel 1. Prints the input voltage on UART.
*
* Parameters:
* void
*
* Return:
* void
*
*******************************************************************************/
void adc_multi_channel_process(void)
{
/* Variable to capture return value of functions */
cy_rslt_t result;
/* Variable to store ADC conversion result from channel 0 */
int32_t adc_result_0 = 0;
/* Variable to store ADC conversion result from channel 1 */
int32_t adc_result_1 = 0;
/* Initiate an asynchronous read operation. The event handler will be called
* when it is complete. */
result = cyhal_adc_read_async_uv(&adc_obj, NUM_SCAN, result_arr);
if(result != CY_RSLT_SUCCESS)
{
printf("ADC async read failed. Error: %ld\n", (long unsigned int)result);
CY_ASSERT(0);
}
/*
* Read data from result list, input voltage in the result list is in
* microvolts. Convert it millivolts and print input voltage
*
*/
adc_result_0 = result_arr[CHANNEL_0] / MICRO_TO_MILLI_CONV_RATIO;
adc_result_1 = result_arr[CHANNEL_1] / MICRO_TO_MILLI_CONV_RATIO;
printf("Channel 0 input: %4ldmV \t Channel 1 input: %4ldmV\r\n", (long int)adc_result_0, (long int)adc_result_1);
/* Clear async read complete flag */
async_read_complete = false;
}
/*******************************************************************************
* Function Name: adc_event_handler
*******************************************************************************
*
* Summary:
* ADC event handler. This function handles the asynchronous read complete event
* and sets the async_read_complete flag to true.
*
* Parameters:
* void *arg : pointer to result list
* cyhal_adc_event_t event : ADC event type
*
* Return:
* void
*
*******************************************************************************/
static void adc_event_handler(void* arg, cyhal_adc_event_t event)
{
if(0u != (event & CYHAL_ADC_ASYNC_READ_COMPLETE))
{
/* Set async read complete flag to true */
async_read_complete = true;
}
}
#else
/*******************************************************************************
* Function Name: adc_single_channel_init
*******************************************************************************
*
* Summary:
* ADC single channel initialization function. This function initializes and
* configures channel 0 of ADC.
*
* Parameters:
* void
*
* Return:
* void
*
*******************************************************************************/
void adc_single_channel_init(void)
{
/* Variable to capture return value of functions */
cy_rslt_t result;
/* Initialize ADC. The ADC block which can connect to the channel 0 input pin is selected */
result = cyhal_adc_init(&adc_obj, VPLUS_CHANNEL_0, NULL);
if(result != CY_RSLT_SUCCESS)
{
printf("ADC initialization failed. Error: %ld\n", (long unsigned int)result);
CY_ASSERT(0);
}
/* ADC channel configuration */
const cyhal_adc_channel_config_t channel_config = {
.enable_averaging = false, // Disable averaging for channel
.min_acquisition_ns = ACQUISITION_TIME_NS, // Minimum acquisition time set to 1us
.enabled = true }; // Sample this channel when ADC performs a scan
/* Initialize a channel 0 and configure it to scan the channel 0 input pin in single ended mode. */
result = cyhal_adc_channel_init_diff(&adc_chan_0_obj, &adc_obj, VPLUS_CHANNEL_0,
CYHAL_ADC_VNEG, &channel_config);
if(result != CY_RSLT_SUCCESS)
{
printf("ADC single ended channel initialization failed. Error: %ld\n", (long unsigned int)result);
CY_ASSERT(0);
}
printf("ADC is configured in single channel configuration\r\n\n");
printf("Provide input voltage at the channel 0 input pin. \r\n\n");
}
/*******************************************************************************
* Function Name: adc_single_channel_process
*******************************************************************************
*
* Summary:
* ADC single channel process function. This function reads the input voltage
* and prints the input voltage on UART.
*
* Parameters:
* void
*
* Return:
* void
*
*******************************************************************************/
void adc_single_channel_process(void)
{
/* Variable to store ADC conversion result from channel 0 */
int32_t adc_result_0 = 0;
/* Read input voltage, convert it to millivolts and print input voltage */
adc_result_0 = cyhal_adc_read_uv(&adc_chan_0_obj) / MICRO_TO_MILLI_CONV_RATIO;
printf("Channel 0 input: %4ldmV\r\n", (long int)adc_result_0);
}
#endif /* ADC_EXAMPLE_MODE == MULTI_CHANNEL */
/* [] END OF FILE */