-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
366 lines (301 loc) · 12.9 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
/******************************************************************************
* File Name: main.c
*
* Description: This is the source code for the emFile Filesystem on SD Card
* and QSPI NOR Flash code example for ModusToolbox.
*
* Related Document: See README.md
*
*
*******************************************************************************
* Copyright 2021-2023, 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 "cyhal.h"
#include "cybsp.h"
#include "cy_retarget_io.h"
#include "FS.h"
#include <string.h>
/* FreeRTOS headers */
#include "FreeRTOS.h"
#include "task.h"
#include <inttypes.h>
/*******************************************************************************
* Macros
********************************************************************************/
/* Last byte in the buffer is used for terminating the string using a NULL
* character. Therefore, the number of bytes read from the file is one less than
* the value of the macro.
*/
#define NUM_BYTES_TO_READ_FROM_FILE (256U)
#define STRING_TO_WRITE "This is an emFile filesystem example for ModusToolbox."
#define FILE_NAME "File.txt"
#define EMFILE_TASK_STACK_SIZE (512U)
#define USER_BUTTON_INTERRUPT_PRIORITY (7U)
/* Debounce delay for the user button. */
#define DEBOUNCE_DELAY_MS (50U)
/*******************************************************************************
* Global Variables
******************************************************************************/
static char file_data[NUM_BYTES_TO_READ_FROM_FILE];
static TaskHandle_t emfile_task_handle;
cyhal_gpio_callback_data_t user_btn_callback_data;
/* This enables RTOS aware debugging */
volatile int uxTopUsedPriority;
/*******************************************************************************
* Function Prototypes
*******************************************************************************/
static void user_button_interrupt_handler(void *handler_arg, cyhal_gpio_event_t event);
/*******************************************************************************
* Function Name: check_error
****************************************************************************//**
* Summary:
* Prints the message and halts the execution by running an infinite loop, when
* the error value is negative.
*
* Parameters:
* message - message to print if error value is negative.
* error - error value for evaluation.
*
*******************************************************************************/
static void check_error(char *message, int error)
{
if (error < 0)
{
printf("\n================================================================================\n");
printf("\nFAIL: %s\n", message);
printf("Error Value: %d\n", error);
printf("emFile-defined Error Message: %s", FS_ErrorNo2Text(error));
printf("\n================================================================================\n");
while(true);
}
}
/*******************************************************************************
* Function Name: user_button_interrupt_handler
********************************************************************************
* Summary:
* User button interrupt handler.
*
* Parameters:
* void *handler_arg (unused)
* cyhal_gpio_irq_event_t (unused)
*
*******************************************************************************/
static void user_button_interrupt_handler(void *handler_arg, cyhal_gpio_event_t event)
{
(void) handler_arg;
(void) event;
BaseType_t higher_priority_task_woken = pdFALSE;
vTaskNotifyGiveFromISR(emfile_task_handle, &higher_priority_task_woken);
/* Yield if xHigherPriorityTaskWoken was set to true */
portYIELD_FROM_ISR( higher_priority_task_woken );
}
/*******************************************************************************
* Function Name: emfile_task
********************************************************************************
* Summary:
* Formats the storage device, reads the content from a file and prints the
* content to the UART terminal, writes a message to the same file, and waits
* for the user button press. When the button is pressed, deletes the file and
* returns.
*
* Parameters:
* arg - Unused.
*
*******************************************************************************/
static void emfile_task(void* arg)
{
U32 volume_size;
U32 num_bytes_to_read;
int error;
FS_FILE *file_ptr;
const char *volume_name = "";
#if defined(USE_SD_CARD)
printf("Using SD card as storage device\n");
#else
printf("Using NOR flash as storage device\n");
#endif /* #if defined(USE_SD_CARD) */
/* Initialize the file system. */
FS_Init();
#if !defined(USE_SD_CARD)
/* Check if low-level format is required. Applicable only for NOR flash. */
error = FS_FormatLLIfRequired(volume_name);
check_error("Error in low-level formatting", error);
#endif /* #if !defined(USE_SD_CARD) */
/* Check if volume needs to be high-level formatted. */
error = FS_IsHLFormatted(volume_name);
check_error("Error in checking if volume is high-level formatted", error);
/* Return value of 0 indicates that high-level format is required. */
if (error == 0)
{
printf("Perform high-level format\n");
error = FS_Format(volume_name, NULL);
check_error("Error in high-level formatting", error);
}
volume_size = FS_GetVolumeSizeKB(volume_name);
printf("Volume size: %"PRIu32" KB\n\n", volume_size);
if(0U == volume_size)
{
printf("Error in checking the volume size\n");
CY_ASSERT(0U);
}
printf("Opening the file for reading...\n");
/* Open the file for reading. */
file_ptr = FS_FOpen(FILE_NAME, "r");
if (file_ptr != NULL)
{
/* Last byte is for storing the NULL character. */
num_bytes_to_read = sizeof(file_data) - 1U;
volume_size = FS_GetFileSize(file_ptr);
if(volume_size < num_bytes_to_read)
{
num_bytes_to_read = volume_size;
}
printf("Reading %"PRIu32" bytes from the file. ", num_bytes_to_read);
volume_size = FS_Read(file_ptr, file_data, num_bytes_to_read);
if(volume_size != num_bytes_to_read)
{
error = FS_FError(file_ptr);
check_error("Error in reading from the file", error);
}
/* Terminate the string using NULL. */
file_data[num_bytes_to_read] = '\0';
/* Display the file content. */
printf("File Content:\n\"%s\"\n", file_data);
error = FS_FClose(file_ptr);
check_error("Error in closing the file", error);
printf("\nOpening the file for overwriting...\n");
}
else
{
printf("Unable to read. File not found.\n");
printf("\nOpening the file for writing...\n");
}
/* Mode 'w' truncates the file size to zero if the file exists otherwise
* creates a new file.
*/
file_ptr = FS_FOpen(FILE_NAME, "w");
if(file_ptr != NULL)
{
volume_size = FS_Write(file_ptr, STRING_TO_WRITE, strlen(STRING_TO_WRITE));
if(volume_size != strlen(STRING_TO_WRITE))
{
error = FS_FError(file_ptr);
check_error("Error in writing to the file", error);
}
printf("File is written with the following message:\n");
printf("\"%s\"\n\n", STRING_TO_WRITE);
#if defined(USE_SD_CARD)
printf("You can now view the file content in your PC. File name is \"%s\"\n", FILE_NAME);
#endif /* #if defined(USE_SD_CARD) */
error = FS_FClose(file_ptr);
check_error("Error in closing the file", error);
/* Enable the user button interrupt */
cyhal_gpio_enable_event(CYBSP_USER_BTN, CYHAL_GPIO_IRQ_FALL, USER_BUTTON_INTERRUPT_PRIORITY, true);
printf("\nPress the user button to delete the file or press reset to run the example again.\n\n");
/* Wait until the user button press is notified through the interrupt */
while (true)
{
if(1UL == ulTaskNotifyTake(pdTRUE, portMAX_DELAY))
{
/* Debounce the button press. */
vTaskDelay(pdMS_TO_TICKS(DEBOUNCE_DELAY_MS));
if(!cyhal_gpio_read(CYBSP_USER_BTN)) { break; }
}
}
printf("Deleting the file... \n");
/* User button is pressed. Delete the file. */
error = FS_Remove(FILE_NAME);
check_error("Error in deleting the file", error);
FS_Unmount(volume_name);
printf("Filesystem operations completed successfully!\n");
printf("Press reset to the run the example again.\n");
}
else
{
printf("Unable to open the file for writing! Exiting...\n");
}
}
/*******************************************************************************
* Function Name: main
********************************************************************************
* Summary:
* This is the main function for CM4 CPU. It does...
* 1. Initializes the UART for redirecting printf output
* 2. Intializes the user button GPIO
* 3. Creates a FreeRTOS task to perform file I/O operations
* 4. Starts the FreeRTOS scheduler
*
* Parameters:
* void
*
* Return:
* int
*
*******************************************************************************/
int main(void)
{
cy_rslt_t result;
/* This enables RTOS aware debugging in OpenOCD */
uxTopUsedPriority = configMAX_PRIORITIES - 1;
/* Initialize the device and board peripherals */
result = cybsp_init() ;
CY_ASSERT (result == CY_RSLT_SUCCESS);
/* 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);
CY_ASSERT (result == CY_RSLT_SUCCESS);
/* Initialize the user button used for erasing the block device. */
result = cyhal_gpio_init(CYBSP_USER_BTN, CYHAL_GPIO_DIR_INPUT, CYHAL_GPIO_DRIVE_PULLUP, CYBSP_BTN_OFF);
CY_ASSERT (result == CY_RSLT_SUCCESS);
/* Configure user button interrupt */
user_btn_callback_data.callback = user_button_interrupt_handler;
/* Configure & the user button interrupt */
cyhal_gpio_register_callback(CYBSP_USER_BTN, &user_btn_callback_data);
/* Enable global interrupts */
__enable_irq();
/* \x1b[2J\x1b[;H - ANSI ESC sequence for clear screen */
printf("\x1b[2J\x1b[;H");
printf("************* "
"emFile FAT Filesystem on SD Card and QSPI NOR Flash "
"************* \n\n");
/* Create the user tasks. See the respective task definition for more
* details of these tasks.
*/
xTaskCreate(emfile_task, "emFile Task", EMFILE_TASK_STACK_SIZE,
NULL, (configMAX_PRIORITIES - 1), &emfile_task_handle);
/* Start the RTOS scheduler. This function should never return */
vTaskStartScheduler();
(void) result; /* To avoid compiler warning */
for (;;)
{
}
}
/* [] END OF FILE */