-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
293 lines (255 loc) · 12.7 KB
/
Copy pathmain.c
File metadata and controls
293 lines (255 loc) · 12.7 KB
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
/**********************************************************************************************************************
* \file main.c
* \copyright Copyright (C) Infineon Technologies AG 2019
*
* 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 "cyhal.h"
#include "cy_retarget_io.h"
#include <inttypes.h>
#include "cy_tcpwm.h"
/*********************************************************************************************************************/
/*------------------------------------------------------Macros-------------------------------------------------------*/
/*********************************************************************************************************************/
/* Number of ADC channels */
#define ADC_NUM_OF_CHANNELS (3)
/* Assign the ADC interrupt number and priority */
#define ADC_IRQ_NUM (NvicMux2_IRQn)
#define ADC_INTR_CH0_NUM (((ADC_IRQ_NUM << 16u) | SAR0_CH_0_IRQ))
#define ADC_INTR_PRIORITY (0u)
/*********************************************************************************************************************/
/*-------------------------------------------------Global variables--------------------------------------------------*/
/*********************************************************************************************************************/
/* ADC interrupt configuration */
const cy_stc_sysint_t IRQ_CFG_SAR =
{
.intrSrc = ADC_INTR_CH0_NUM,
.intrPriority = ADC_INTR_PRIORITY,
};
/* ADC channel result */
static uint16_t g_adcResultBuffer[ADC_NUM_OF_CHANNELS] = {0};
/* ADC channel EoS interrupt flag */
static bool g_flagSarGroupEoS = false;
/*********************************************************************************************************************/
/*------------------------------------------------Function Prototypes------------------------------------------------*/
/*********************************************************************************************************************/
/* SAR multiple channel initialization function */
void InitAdcMultiChannel(void);
/* Function to handle the ADL callback events */
void HandleAdcGrpInt(void);
/* Trigger ADC conversion */
void TriggerCounterForConversion(void);
/* PWM channel initialization function */
void InitAllTcpwm(void);
/*********************************************************************************************************************/
/*---------------------------------------------Function Implementations----------------------------------------------*/
/*********************************************************************************************************************/
/**********************************************************************************************************************
* Function Name: HandleError
* Summary:
* User defined error handling function.
* Parameters:
* status - status for evaluation.
* Return:
* void
**********************************************************************************************************************
*/
void HandleError(cy_rslt_t status)
{
if (CY_RSLT_SUCCESS != status)
{
/* Halt the CPU while debugging */
CY_ASSERT(0);
}
}
/**********************************************************************************************************************
* Function Name: main
* Summary:
* This is the main function.
* Parameters:
* none
* Return:
* int
**********************************************************************************************************************
*/
int main(void)
{
/* API return code */
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();
HandleError(result);
/* Enable global interrupts */
__enable_irq();
/* Initialize the 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);
HandleError(result);
/* \x1b[2J\x1b[;H - ANSI ESC sequence for clear screen */
printf("\x1b[2J\x1b[;H");
printf("********************************************************************************\r\n");
printf(" Three channels simultaneous ADC conversion triggered by the TCPWM counter\r\n");
printf("********************************************************************************\r\n");
/* Initialize the SAR-ADC channels */
InitAdcMultiChannel();
/* Initialize the pwm counter channel */
InitAllTcpwm();
/* Trigger ADC conversion using TCPWM */
TriggerCounterForConversion();
for (;;)
{
/* Sample input voltage at for all SARADC 0, 1 and 2 */
if (g_flagSarGroupEoS == true)
{
/* Clear EoS interrupt flag */
g_flagSarGroupEoS = false;
/* Print the conversion value */
printf("\x1b[1F");
printf("\n\r ADC results:\t0/CH0-%4d\t1/CH0-%4d\t2/CH0-%4d", g_adcResultBuffer[0], g_adcResultBuffer[1], g_adcResultBuffer[2]);
}
}
}
/**********************************************************************************************************************
* Function Name: HandleAdcGrpInt
* Summary:
* This is the interrupt handler function for the ADC group conversion.
* Parameters:
* none
* Return:
* void
**********************************************************************************************************************
*/
void HandleAdcGrpInt (void)
{
uint32_t adcStatus[3];
/* Get interrupt status */
uint32_t intrSource = Cy_SAR2_Channel_GetInterruptStatusMasked(SAR0_HW, SAR0_CH_0_IDX);
/* if the interrupt is group-done */
if (CY_SAR2_INT_GRP_DONE == (intrSource & CY_SAR2_INT_GRP_DONE))
{
/* Get the ADC conversion result and status */
g_adcResultBuffer[0] = Cy_SAR2_Channel_GetResult(SAR0_HW, SAR0_CH_0_IDX, &adcStatus[0]);
g_adcResultBuffer[1] = Cy_SAR2_Channel_GetResult(SAR1_HW, SAR1_CH_0_IDX, &adcStatus[1]);
g_adcResultBuffer[2] = Cy_SAR2_Channel_GetResult(SAR2_HW, SAR2_CH_0_IDX, &adcStatus[2]);
/* Check the status and raise conversion finish flag */
if ((CY_SAR2_STATUS_VALID == (adcStatus[0] & CY_SAR2_STATUS_VALID)) &&
(CY_SAR2_STATUS_VALID == (adcStatus[1] & CY_SAR2_STATUS_VALID)) &&
(CY_SAR2_STATUS_VALID == (adcStatus[2] & CY_SAR2_STATUS_VALID)))
{
g_flagSarGroupEoS = true;
}
else
{
CY_ASSERT(0);
}
/* Clear interrupt source */
Cy_SAR2_Channel_ClearInterrupt(SAR0_HW, SAR0_CH_0_IDX, CY_SAR2_INT_GRP_DONE);
}
}
/**********************************************************************************************************************
* Function Name: InitAdcMultiChannel
* Summary:
* ADC initialization function.
* This function initializes and configures the three channels belonging to SARADC 0, 1, and 2, respectively.
* Parameters:
* none
* Return:
* void
**********************************************************************************************************************
*/
void InitAdcMultiChannel(void)
{
/* De-initialize the SAR2 module */
Cy_SAR2_DeInit(SAR0_HW);
/* Initialize ADC */
Cy_SAR2_Init(SAR0_HW, &SAR0_config);
Cy_SAR2_Init(SAR1_HW, &SAR1_config);
Cy_SAR2_Init(SAR2_HW, &SAR2_config);
/* Set ADC group done interrupt */
Cy_SAR2_Channel_SetInterruptMask(SAR0_HW, SAR0_CH_0_IDX, CY_SAR2_INT_GRP_DONE);
/* Register ADC interrupt handler */
Cy_SysInt_Init(&IRQ_CFG_SAR, &HandleAdcGrpInt);
/* Enable interrupt */
NVIC_ClearPendingIRQ(ADC_IRQ_NUM);
NVIC_EnableIRQ((IRQn_Type)ADC_IRQ_NUM);
printf(" Three SARADC are configured\r\n");
printf(" SARADCx Channel Pin \t Trigger by\t\t End pulse\r\n");
printf(" 0 CH0 P%d_%d \t TCPWM1_TROUT0[%ld]\t P%d_%d\r\n", SAR0_IN_PORT_NUM, SAR0_IN_NUM, TCPWM_TRG_SAR_NUM, SAR0_FIN_OUT_PORT_NUM, SAR0_FIN_OUT_NUM);
printf(" 1 CH0 P%d_%d \t TCPWM1_TROUT0[%ld]\t P%d_%d\r\n", SAR1_IN_PORT_NUM, SAR1_IN_NUM, TCPWM_TRG_SAR_NUM, SAR1_FIN_OUT_PORT_NUM, SAR1_FIN_OUT_NUM);
printf(" 2 CH0 P%d_%d \t TCPWM1_TROUT0[%ld]\t P%d_%d\r\n\n", SAR2_IN_PORT_NUM, SAR2_IN_NUM, TCPWM_TRG_SAR_NUM, SAR2_FIN_OUT_PORT_NUM, SAR2_FIN_OUT_NUM);
}
/**********************************************************************************************************************
* Function Name: InitAllTcpwm
* Summary:
* TCPWM channel initialization function.
* This function initializes and configures TCPWM counter to trigger the SAR-ADC channels.
* In addition, three TCPWM PWM used to indicate the completion of the AD conversion is initialized as well.
* Parameters:
* none
* Return:
* void
**********************************************************************************************************************
*/
void InitAllTcpwm(void)
{
/* Initialize the TCPWM counter to use as a trigger for all the SARADCs */
Cy_TCPWM_Counter_Init(TCPWM_TRG_SAR_HW, TCPWM_TRG_SAR_NUM, &TCPWM_TRG_SAR_config);
/* Enable the TCPWM counter */
Cy_TCPWM_Counter_Enable(TCPWM_TRG_SAR_HW, TCPWM_TRG_SAR_NUM);
/* Initialize all the TCPWM PWM to use as indicator of conversion of each SARADCs are finished */
Cy_TCPWM_PWM_Init(TCPWM_SAR0_FIN_HW, TCPWM_SAR0_FIN_NUM, &TCPWM_SAR0_FIN_config);
Cy_TCPWM_PWM_Init(TCPWM_SAR1_FIN_HW, TCPWM_SAR1_FIN_NUM, &TCPWM_SAR1_FIN_config);
Cy_TCPWM_PWM_Init(TCPWM_SAR2_FIN_HW, TCPWM_SAR2_FIN_NUM, &TCPWM_SAR2_FIN_config);
/* Enable all the TCPWM PWM */
Cy_TCPWM_PWM_Enable(TCPWM_SAR0_FIN_HW, TCPWM_SAR0_FIN_NUM);
Cy_TCPWM_PWM_Enable(TCPWM_SAR1_FIN_HW, TCPWM_SAR1_FIN_NUM);
Cy_TCPWM_PWM_Enable(TCPWM_SAR2_FIN_HW, TCPWM_SAR2_FIN_NUM);
printf("TCPWM counter and PWM are configured...\r\n");
}
/**********************************************************************************************************************
* Function Name: TriggerCounterForConversion
* Summary:
* This function is used to trigger the counter to start the ADC conversion on three channels simultaneously.
* Parameters:
* none
* Return:
* void
**********************************************************************************************************************
*/
void TriggerCounterForConversion(void)
{
/* Start the TCPWM counter */
Cy_TCPWM_TriggerStart_Single(TCPWM_TRG_SAR_HW, TCPWM_TRG_SAR_NUM);
printf("PWM trigger started successfully...\r\n");
}
/* [] END OF FILE */