-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
298 lines (261 loc) · 12.7 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
/**********************************************************************************************************************
* \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 "cy_pdl.h"
#include "cybsp.h"
#include "cy_retarget_io.h"
#include "cyhal.h"
/*********************************************************************************************************************/
/*------------------------------------------------------Macros-------------------------------------------------------*/
/*********************************************************************************************************************/
/* Result output format */
enum OutputFmt
{
UNSIGNED_RIGHT_ALIGNED,
SIGNED_RIGHT_ALIGNED,
LEFT_ALIGNED,
FORMAT_NUM
};
/* Lower level of average count */
#define AVERAGE_COUNT_MIN (1u)
/* Upper level of average count */
#define AVERAGE_COUNT_MAX (256u)
/* Internal band gap reference voltage */
#define BAND_GAP_MV (900u)
/*********************************************************************************************************************/
/*-------------------------------------------------Global variables--------------------------------------------------*/
/*********************************************************************************************************************/
/* Interrupt configuration */
const cy_stc_sysint_t IRQ_CFG =
{
.intrSrc = ((NvicMux3_IRQn << CY_SYSINT_INTRSRC_MUXIRQ_SHIFT) | CE_SAR2_CH1_IRQ),
.intrPriority = 2UL
};
int32_t g_nextOutputFormat = UNSIGNED_RIGHT_ALIGNED;
int32_t g_nextAverageCount = 1;
int32_t g_outputFormat = -1;
int32_t g_averageCount = -1;
const char *OUTPUT_FORMAT_STR[FORMAT_NUM] =
{
"Unsigned/Right Aligned",
"Signed/Right Aligned ",
"Left Aligned "
};
/*********************************************************************************************************************/
/*------------------------------------------------Function Prototypes------------------------------------------------*/
/*********************************************************************************************************************/
void handle_SAR_ADC_IRQ(void);
void configure_SAR_ADC(int32_t outputFormat, int32_t averageCount);
/*********************************************************************************************************************/
/*---------------------------------------------Function Implementations----------------------------------------------*/
/*********************************************************************************************************************/
/**********************************************************************************************************************
* Function Name: main
* Summary:
* This is the main function.
* It sets up SAR ADC with default setting then inputs software trigger to start AD conversion.
* The main while loop captures the command from terminal and stores the value to global variable.
* Parameters:
* none
* Return:
* int
**********************************************************************************************************************
*/
int main(void)
{
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 /* #if defined (CY_DEVICE_SECURE) */
/* 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);
}
/* \x1b[2J\x1b[;H - ANSI ESC sequence for clear screen */
printf("\x1b[2J\x1b[;H");
printf("****************** Code Example: SAR ADC Various Processing of Conversion Result ******************\r\n");
printf("Press 'a' key to decrease the average count:\r\n"
" [256 -> 128 -> 64 -> 32 -> 16 -> 8 -> 4 -> 2 -> 1]\r\n"
"Press 'd' key to increase the average count:\r\n"
" [1 -> 2 -> 4 -> 8 -> 16 -> 32 -> 64 -> 128 -> 256]\r\n"
"Press 's' key to change the output format:\r\n"
" [(Unsigned/Right Aligned) -> (Signed/Right Aligned) -> (Left Aligned) -> (Unsigned/Right Aligned)...]\r\n\n");
/* \x1b[?25l - ESC sequence for clear cursor (not a pure VT100 escape sequence, but it works in TeraTerm) */
printf("\x1b[?25l");
/* Configure SAR-ADC */
configure_SAR_ADC(g_nextOutputFormat, g_nextAverageCount);
for (;;)
{
uint8_t uartReadValue;
/* Check if any of valid keys was pressed */
if (cyhal_uart_getc(&cy_retarget_io_uart_obj, &uartReadValue, 1) == CY_RSLT_SUCCESS)
{
if ((uartReadValue == 'a') || (uartReadValue == 'd'))
{
/* Check for limits and increment/decrement accordingly */
if ((uartReadValue == 'a') && (g_nextAverageCount != AVERAGE_COUNT_MIN))
{
g_nextAverageCount >>= 1;
}
else if ((uartReadValue == 'd') && (g_nextAverageCount != AVERAGE_COUNT_MAX))
{
g_nextAverageCount <<= 1;
}
}
else if (uartReadValue == 's')
{
/* change the output format to next one */
if (++g_nextOutputFormat > LEFT_ALIGNED)
{
g_nextOutputFormat = UNSIGNED_RIGHT_ALIGNED;
}
}
}
}
}
/**********************************************************************************************************************
* Function Name: handle_SAR_ADC_IRQ
* Summary:
* SAR ADC interrupt handler function, will display the potentiometer voltage in milli volt.
* Then it reconfigures SAR ADC according to the value of global variable if changes are there.
* Parameters:
* none
* Return:
* none
**********************************************************************************************************************
*/
void handle_SAR_ADC_IRQ(void)
{
/* Get interrupt source */
uint32_t intr = Cy_SAR2_Channel_GetInterruptStatus(PASS0_SAR0, CE_SAR2_AN0_IDX);
/* Clear interrupt source */
Cy_SAR2_Channel_ClearInterrupt(PASS0_SAR0, CE_SAR2_AN0_IDX, intr);
/* if the interrupt is group-done */
if (intr == CY_SAR2_INT_GRP_DONE)
{
/* Get conversion results in counts, do not obtain or analyze status here */
uint16_t resultVBG = Cy_SAR2_Channel_GetResult(PASS0_SAR0, CE_SAR2_VBG_IDX, NULL);
uint16_t resultAN0_raw = Cy_SAR2_Channel_GetResult(PASS0_SAR0, CE_SAR2_AN0_IDX, NULL);
uint16_t resultAN0 = resultAN0_raw;
if (g_outputFormat == SIGNED_RIGHT_ALIGNED)
{
resultAN0 = (resultAN0_raw & 0xFFF);
/* The 12-bit code for a signal at VREFH/2 is 0x800.
* This means 0x800 is considered 0, any value below 0x800 is on considered negative,
* and values above 0x800 are considered positive
*/
if ((resultAN0 & 0x800) != 0)
{
resultAN0 -= 0x800;
}
else
{
resultAN0 += 0x800;
}
}
else if (g_outputFormat == LEFT_ALIGNED)
{
resultAN0 = (resultAN0_raw >> 4) & 0xFFF;
}
/* Update the current configuration and the conversion result then move cursor to previous line */
printf("Output format: %s\r\nAverage count: %3ld\r\n", OUTPUT_FORMAT_STR[g_outputFormat], g_averageCount);
printf("Conversion result raw value: 0x%04x\r\n", resultAN0_raw);
printf("Potentiometer voltage: %4ldmV\r\n", ((uint32_t)resultAN0 * BAND_GAP_MV) / (uint32_t)resultVBG);
printf("\x1b[4F");
configure_SAR_ADC(g_nextOutputFormat, g_nextAverageCount);
}
}
/**********************************************************************************************************************
* Function Name: configure_SAR_ADC
* Summary:
* This function configures SAR ADC with specified setting.
* Parameters:
* int32_t outputFormat - The received output format from user input
* int32_t averageCount - The received average count from user input
* Return:
* none
**********************************************************************************************************************
*/
void configure_SAR_ADC(int32_t outputFormat, int32_t averageCount)
{
if ((g_outputFormat != outputFormat) || (g_averageCount != averageCount))
{
/* De-initialize the SAR2 module */
Cy_SAR2_DeInit(PASS0_SAR0);
/* Reflect specified configuration into the structure value */
CE_SAR2_AN0_config.rightShift = log(averageCount) / log(2);
CE_SAR2_AN0_config.averageCount = (uint16_t)averageCount;
if (outputFormat == UNSIGNED_RIGHT_ALIGNED)
{
CE_SAR2_AN0_config.resultAlignment = CY_SAR2_RESULT_ALIGNMENT_RIGHT;
CE_SAR2_AN0_config.signExtention = CY_SAR2_SIGN_EXTENTION_UNSIGNED;
}
else if (outputFormat == SIGNED_RIGHT_ALIGNED)
{
CE_SAR2_AN0_config.resultAlignment = CY_SAR2_RESULT_ALIGNMENT_RIGHT;
CE_SAR2_AN0_config.signExtention = CY_SAR2_SIGN_EXTENTION_SIGNED;
}
else
{
CE_SAR2_AN0_config.resultAlignment = CY_SAR2_RESULT_ALIGNMENT_LEFT;
CE_SAR2_AN0_config.signExtention = CY_SAR2_SIGN_EXTENTION_UNSIGNED;
}
/* Initialize the SAR2 module */
Cy_SAR2_Init(PASS0_SAR0, &CE_SAR2_config);
/* Set ePASS MMIO reference buffer mode for bangap voltage */
Cy_SAR2_SetReferenceBufferMode(PASS0_EPASS_MMIO, CY_SAR2_REF_BUF_MODE_ON);
/* Interrupt settings */
Cy_SAR2_Channel_SetInterruptMask(PASS0_SAR0, CE_SAR2_AN0_IDX, CY_SAR2_INT_GRP_DONE);
Cy_SysInt_Init(&IRQ_CFG, &handle_SAR_ADC_IRQ);
NVIC_SetPriority((IRQn_Type) NvicMux3_IRQn, 2UL);
NVIC_EnableIRQ((IRQn_Type) NvicMux3_IRQn);
}
/* Update current configuration */
g_outputFormat = outputFormat;
g_averageCount = averageCount;
/* Scenario: Obtaining conversion results in counts */
Cy_SAR2_Channel_SoftwareTrigger(PASS0_SAR0, CE_SAR2_VBG_IDX);
}
/* [] END OF FILE */