-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
276 lines (238 loc) · 9.92 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
/*******************************************************************************
* File Name: main.c
*
* Description: This example project demonstrates the basic operation of the
* I2C resource as Slave using HAL APIs. The I2C slave receives the
* command packets from the I2C master to control an user LED.
*
* Related Document: See README.md
*
*
********************************************************************************
* Copyright 2019-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.
*******************************************************************************/
/*******************************************************************************
* Header Files
*******************************************************************************/
#include "cyhal.h"
#include "cybsp.h"
#include "cy_retarget_io.h"
/*******************************************************************************
* Macros
*******************************************************************************/
/* Packet positions */
#define PACKET_SOP_POS (0UL)
#define PACKET_CMD_POS (1UL)
#define PACKET_EOP_POS (2UL)
#define PACKET_STATUS_POS (1UL)
/* Start and end of packet markers */
#define PACKET_SOP (0x01UL)
#define PACKET_EOP (0x17UL)
/* I2C slave address to communicate with */
#define I2C_SLAVE_ADDR (0x24UL)
/* I2C bus frequency */
#define I2C_FREQ (400000UL)
/* I2C slave interrupt priority */
#define I2C_SLAVE_IRQ_PRIORITY (7u)
/* Command valid status */
#define STATUS_CMD_DONE (0x00UL)
#define STATUS_CMD_FAIL (0xFFUL)
/* Packet size */
#define PACKET_SIZE (3UL)
#ifdef XMC7200D_E272K8384
#define KIT_XMC72
#endif
/*******************************************************************************
* Global Variables
*******************************************************************************/
cyhal_i2c_t sI2C;
uint8_t i2c_write_buffer[PACKET_SIZE] = {0};
uint8_t i2c_read_buffer[PACKET_SIZE] = {PACKET_SOP,STATUS_CMD_FAIL,PACKET_EOP};
/*******************************************************************************
* Function Prototypes
*******************************************************************************/
/*******************************************************************************
* Function Definitions
*******************************************************************************/
/*******************************************************************************
* Function Name: handle_error
********************************************************************************
* Summary:
* User defined error handling function
*
* Parameters:
* uint32_t status - status indicates success or failure
*
* Return:
* void
*
*******************************************************************************/
void handle_error(uint32_t status)
{
if (status != CY_RSLT_SUCCESS)
{
CY_ASSERT(0);
}
}
/*******************************************************************************
* Function Name: i2c_slave_handle_event
********************************************************************************
* Summary:
* This is a callback function for I2C slave events. If a write event occurs,
* the command packet is verified and executed.
*
* Parameters:
* callback_arg : extra argument that can be passed to callback
* event : I2C event
*
* Return:
* void
*
*******************************************************************************/
void i2c_slave_handle_event(void *callback_arg, cyhal_i2c_event_t event)
{
if (0UL == (CYHAL_I2C_SLAVE_ERR_EVENT & event))
{
if (0UL != (CYHAL_I2C_SLAVE_WR_CMPLT_EVENT & event))
{
/* Check start and end of packet markers */
if ((i2c_write_buffer[PACKET_SOP_POS] == PACKET_SOP) &&
(i2c_write_buffer[PACKET_EOP_POS] == PACKET_EOP))
{
printf("User LED should start blinking \r\n");
/* Execute command */
cyhal_gpio_write(CYBSP_USER_LED,
i2c_write_buffer[PACKET_CMD_POS]);
/* Update status of received command */
i2c_read_buffer[PACKET_STATUS_POS] = STATUS_CMD_DONE;
}
/* Configure read buffer for the next write */
i2c_write_buffer[PACKET_SOP_POS] = 0;
i2c_write_buffer[PACKET_EOP_POS] = 0;
cyhal_i2c_slave_config_write_buffer(&sI2C, i2c_write_buffer,
PACKET_SIZE);
}
if (0UL != (CYHAL_I2C_SLAVE_RD_CMPLT_EVENT & event))
{
/* Configure write buffer for the next read */
i2c_read_buffer[PACKET_STATUS_POS] = STATUS_CMD_FAIL;
cyhal_i2c_slave_config_read_buffer(&sI2C, i2c_read_buffer,
PACKET_SIZE);
}
}
}
/*******************************************************************************
* Function Name: main
********************************************************************************
* Summary:
* This is the main function.
* 1. Initializes the board, retarget-io and led
* 2. Configures the I2C slave to receive packet from the master
*
* Parameters:
* void
*
* Return:
* int
*
*******************************************************************************/
int main(void)
{
cy_rslt_t result;
cyhal_i2c_cfg_t sI2C_cfg;
/* Initialize the device and board peripherals */
result = cybsp_init();
/* Board init failed. Stop program execution */
handle_error(result);
#if defined(KIT_XMC72)
/*Configure clock settings for KIT_XMC72_EVK */
cyhal_clock_t clock_fll, clock_hf, clock_peri;
result = cyhal_clock_reserve(&clock_hf, &CYHAL_CLOCK_HF[0]);
result = cyhal_clock_reserve(&clock_fll, &CYHAL_CLOCK_FLL);
if(result == CY_RSLT_SUCCESS){
result = cyhal_clock_set_source(&clock_hf, &clock_fll);
}
/* Set divider to 1 for Peripheral Clock */
result = cyhal_clock_reserve(&clock_peri, CYHAL_CLOCK_PERI);
if(result == CY_RSLT_SUCCESS){
result = cyhal_clock_set_divider(&clock_peri,1);
}
#endif
/* Initialize the retarget-io */
result = cy_retarget_io_init(CYBSP_DEBUG_UART_TX, CYBSP_DEBUG_UART_RX,
CY_RETARGET_IO_BAUDRATE);
/* Retarget-io init failed. Stop program execution */
handle_error(result);
/* \x1b[2J\x1b[;H - ANSI ESC sequence for clear screen */
printf("\x1b[2J\x1b[;H");
printf("************** "
"HAL: I2C Slave "
"************** \r\n\n");
/* Initialize user LED */
printf(">> Configuring user LED..... ");
result = cyhal_gpio_init(CYBSP_USER_LED, CYHAL_GPIO_DIR_OUTPUT,
CYHAL_GPIO_DRIVE_STRONG, CYBSP_LED_STATE_OFF);
/* LED init failed. Stop program execution */
handle_error(result);
printf("Done\r\n");
/* I2C Slave configuration settings */
printf(">> Configuring I2C Slave..... ");
sI2C_cfg.is_slave = true;
sI2C_cfg.address = I2C_SLAVE_ADDR;
sI2C_cfg.frequencyhal_hz = I2C_FREQ;
/* Init I2C slave */
result = cyhal_i2c_init(&sI2C, CYBSP_I2C_SDA, CYBSP_I2C_SCL, NULL);
/* I2C slave init failed. Stop program execution */
handle_error(result);
/* Configure I2C slave */
result = cyhal_i2c_configure(&sI2C, &sI2C_cfg);
/* I2C slave configuration failed. Stop program execution */
handle_error(result);
printf("Done\r\n");
/* Configure the read buffer on an I2C Slave */
cyhal_i2c_slave_config_read_buffer(&sI2C, i2c_read_buffer, PACKET_SIZE);
/* Configure the write buffer on an I2C Slave */
cyhal_i2c_slave_config_write_buffer(&sI2C, i2c_write_buffer, PACKET_SIZE);
/* Register an I2C event callback handler */
cyhal_i2c_register_callback(&sI2C, i2c_slave_handle_event, NULL);
/* Configure and Enable I2C Interrupt */
cyhal_i2c_enable_event(&sI2C,
(cyhal_i2c_event_t)(CYHAL_I2C_SLAVE_WR_CMPLT_EVENT
| CYHAL_I2C_SLAVE_RD_CMPLT_EVENT
| CYHAL_I2C_SLAVE_ERR_EVENT),
I2C_SLAVE_IRQ_PRIORITY, true);
/* Enable interrupts */
__enable_irq();
for (;;)
{
}
}