-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
289 lines (241 loc) · 9.83 KB
/
Copy pathmain.c
File metadata and controls
289 lines (241 loc) · 9.83 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
/******************************************************************************
* File Name: main.c
*
* Description: This is the source code for the PSoC 4: MSCLP low-power self-capacitance button
* Example for ModusToolbox.
*
* Related Document: See README.md
*
*
*******************************************************************************
* $ Copyright 2021-2023 Cypress Semiconductor $
*******************************************************************************/
/*******************************************************************************
* Include header files
******************************************************************************/
#include "cy_pdl.h"
#include "cybsp.h"
#include "cycfg.h"
#include "cycfg_capsense.h"
/*******************************************************************************
* User configurable Macros
********************************************************************************/
/*******************************************************************************
* Fixed Macros
*******************************************************************************/
#define CAPSENSE_MSC0_INTR_PRIORITY (3u)
#define CY_ASSERT_FAILED (0u)
#define CAPSENSE_WIDGET_INACTIVE (1u)
#define CYBSP_LED_ON (0u)
#define CYBSP_LED_OFF (1u)
/* EZI2C interrupt priority must be higher than CAPSENSE™ interrupt. */
#define EZI2C_INTR_PRIORITY (2u)
/*******************************************************************************
* Global Variables
*******************************************************************************/
cy_stc_scb_ezi2c_context_t ezi2c_context;
/*******************************************************************************
* Function Prototypes
*******************************************************************************/
static void initialize_capsense(void);
static void capsense_msc0_isr(void);
static void ezi2c_isr(void);
static void initialize_capsense_tuner(void);
void led_control();
/*******************************************************************************
* Function Name: main
********************************************************************************
* Summary:
* System entrance point. This function performs
* - initial setup of device
* - initialize CAPSENSE™
* - initialize tuner communication
* - scan touch input continuously
* - user LED for touch indication
*
* Return:
* int
*
*******************************************************************************/
int main(void)
{
cy_rslt_t result;
/* Initialize the device and board peripherals */
result = cybsp_init();
/* Board init failed. Stop program execution */
if (result != CY_RSLT_SUCCESS)
{
CY_ASSERT(CY_ASSERT_FAILED);
}
/* Enable global interrupts */
__enable_irq();
/* Initialize EZI2C */
initialize_capsense_tuner();
/* Initialize MSC CAPSENSE */
initialize_capsense();
for (;;)
{
/*Scan all slots corresponding to the ISX buttons*/
Cy_CapSense_ScanSlots(0u,4u,&cy_capsense_context);
while(Cy_CapSense_IsBusy(&cy_capsense_context)){}
/*Scan the slots corresponding to the CSD button*/
Cy_CapSense_ScanSlots(4u,1u,&cy_capsense_context);
while(Cy_CapSense_IsBusy(&cy_capsense_context)){}
/*Scan the slots corresponding to the CSX button*/
Cy_CapSense_ScanSlots(5u,1u,&cy_capsense_context);
while(Cy_CapSense_IsBusy(&cy_capsense_context)){}
/* Process all the widgets */
Cy_CapSense_ProcessAllWidgets(&cy_capsense_context);
/* Send capsense data to the Tuner */
Cy_CapSense_RunTuner(&cy_capsense_context);
led_control();
}
}
/*******************************************************************************
* Function Name: initialize_capsense
********************************************************************************
* Summary:
* This function initializes the CAPSENSE™ and configures the CAPSENSE™
* interrupt.
*
*******************************************************************************/
static void initialize_capsense(void)
{
cy_capsense_status_t status = CY_CAPSENSE_STATUS_SUCCESS;
/* CAPSENSE™ interrupt configuration MSCLP 0 */
const cy_stc_sysint_t capsense_msc0_interrupt_config =
{
.intrSrc = CY_MSCLP0_LP_IRQ,
.intrPriority = CAPSENSE_MSC0_INTR_PRIORITY,
};
/* Capture the MSC HW block and initialize it to the default state. */
status = Cy_CapSense_Init(&cy_capsense_context);
if (CY_CAPSENSE_STATUS_SUCCESS == status)
{
/* Initialize CAPSENSE™ interrupt for MSC 0 */
Cy_SysInt_Init(&capsense_msc0_interrupt_config, capsense_msc0_isr);
NVIC_ClearPendingIRQ(capsense_msc0_interrupt_config.intrSrc);
NVIC_EnableIRQ(capsense_msc0_interrupt_config.intrSrc);
status = Cy_CapSense_Enable(&cy_capsense_context);
}
if(status != CY_CAPSENSE_STATUS_SUCCESS)
{
/* This status could fail before tuning the sensors correctly.
* Ensure that this function passes after the CAPSENSE™ sensors are tuned
* as per procedure give in the Readme.md file */
}
}
/*******************************************************************************
* Function Name: capsense_msc0_isr
********************************************************************************
* Summary:
* Wrapper function for handling interrupts from CAPSENSE™ MSC0 block.
*
*******************************************************************************/
static void capsense_msc0_isr(void)
{
Cy_CapSense_InterruptHandler(CY_MSCLP0_HW, &cy_capsense_context);
}
/*******************************************************************************
* Function Name: initialize_capsense_tuner
********************************************************************************
* Summary:
* EZI2C module to communicate with the CAPSENSE™ Tuner tool.
*
*******************************************************************************/
static void initialize_capsense_tuner(void)
{
cy_en_scb_ezi2c_status_t status = CY_SCB_EZI2C_SUCCESS;
/* EZI2C interrupt configuration structure */
const cy_stc_sysint_t ezi2c_intr_config =
{
.intrSrc = CYBSP_EZI2C_IRQ,
.intrPriority = EZI2C_INTR_PRIORITY,
};
/* Initialize the EzI2C firmware module */
status = Cy_SCB_EZI2C_Init(CYBSP_EZI2C_HW, &CYBSP_EZI2C_config, &ezi2c_context);
if(status != CY_SCB_EZI2C_SUCCESS)
{
CY_ASSERT(CY_ASSERT_FAILED);
}
Cy_SysInt_Init(&ezi2c_intr_config, ezi2c_isr);
NVIC_EnableIRQ(ezi2c_intr_config.intrSrc);
/* Set the CAPSENSE™ data structure as the I2C buffer to be exposed to the
* master on primary slave address interface. Any I2C host tools such as
* the Tuner or the Bridge Control Panel can read this buffer but you can
* connect only one tool at a time.
*/
Cy_SCB_EZI2C_SetBuffer1(CYBSP_EZI2C_HW, (uint8_t *)&cy_capsense_tuner,
sizeof(cy_capsense_tuner), sizeof(cy_capsense_tuner),
&ezi2c_context);
Cy_SCB_EZI2C_Enable(CYBSP_EZI2C_HW);
}
/*******************************************************************************
* Function Name: ezi2c_isr
********************************************************************************
* Summary:
* Wrapper function for handling interrupts from EZI2C block.
*
*******************************************************************************/
static void ezi2c_isr(void)
{
Cy_SCB_EZI2C_Interrupt(CYBSP_EZI2C_HW, &ezi2c_context);
}
/*******************************************************************************
* Function Name: led_control
********************************************************************************
* Summary:
* Control LEDs in the kit to show touch activation of different sensors
*******************************************************************************/
void led_control()
{
if (CAPSENSE_WIDGET_INACTIVE != Cy_CapSense_IsWidgetActive(CY_CAPSENSE_CSD_BUTTON_WDGT_ID, &cy_capsense_context))
{
Cy_GPIO_Write(CYBSP_USER_LED2_PORT, CYBSP_USER_LED2_NUM, CYBSP_LED_ON);
}
else
{
Cy_GPIO_Write(CYBSP_USER_LED2_PORT, CYBSP_USER_LED2_NUM, CYBSP_LED_OFF);
}
if (CAPSENSE_WIDGET_INACTIVE != Cy_CapSense_IsWidgetActive(CY_CAPSENSE_CSX_BUTTON_WDGT_ID, &cy_capsense_context))
{
Cy_GPIO_Write(CYBSP_USER_LED1_PORT, CYBSP_USER_LED1_NUM, CYBSP_LED_ON);
}
else
{
Cy_GPIO_Write(CYBSP_USER_LED1_PORT, CYBSP_USER_LED1_NUM, CYBSP_LED_OFF);
}
if (CAPSENSE_WIDGET_INACTIVE != Cy_CapSense_IsWidgetActive(CY_CAPSENSE_ISX_BUTTON1_WDGT_ID, &cy_capsense_context))
{
Cy_GPIO_Write(CYBSP_ISX_LED1_PORT, CYBSP_ISX_LED1_NUM, CYBSP_LED_ON);
}
else
{
Cy_GPIO_Write(CYBSP_ISX_LED1_PORT, CYBSP_ISX_LED1_NUM, CYBSP_LED_OFF);
}
if (CAPSENSE_WIDGET_INACTIVE != Cy_CapSense_IsWidgetActive(CY_CAPSENSE_ISX_BUTTON2_WDGT_ID, &cy_capsense_context))
{
Cy_GPIO_Write(CYBSP_ISX_LED2_PORT, CYBSP_ISX_LED2_NUM, CYBSP_LED_ON);
}
else
{
Cy_GPIO_Write(CYBSP_ISX_LED2_PORT, CYBSP_ISX_LED2_NUM, CYBSP_LED_OFF);
}
if (CAPSENSE_WIDGET_INACTIVE != Cy_CapSense_IsWidgetActive(CY_CAPSENSE_ISX_BUTTON3_WDGT_ID, &cy_capsense_context))
{
Cy_GPIO_Write(CYBSP_ISX_LED3_PORT, CYBSP_ISX_LED3_NUM, CYBSP_LED_ON);
}
else
{
Cy_GPIO_Write(CYBSP_ISX_LED3_PORT, CYBSP_ISX_LED3_NUM, CYBSP_LED_OFF);
}
if (CAPSENSE_WIDGET_INACTIVE != Cy_CapSense_IsWidgetActive(CY_CAPSENSE_ISX_BUTTON4_WDGT_ID, &cy_capsense_context))
{
Cy_GPIO_Write(CYBSP_ISX_LED4_PORT, CYBSP_ISX_LED4_NUM, CYBSP_LED_ON);
}
else
{
Cy_GPIO_Write(CYBSP_ISX_LED4_PORT, CYBSP_ISX_LED4_NUM, CYBSP_LED_OFF);
}
}
/* [] END OF FILE */