Step 5: Review the Application Code

Last modified by Microchip on 2024/01/29 21:51

Application File: app_tone_textfile_sdcard.c

This file remains the same as in audio_player_lab2, except for the condition added in the previous step, which makes sure that APP_TONE_TEXTFILE_SDCARD_Tasks runs only when the application is in Audio Player mode.

Application File: app_tone_textfile_sdcard.h

This file remains the same as in audio_player_lab2.

Application File: app_sdcard_reader_task.h

Open file app_sdcard_reader_task.h. This file defines application enums, data, and Application Programming Interfaces (APIs).

The following enumeration defines the states of the SD card reader task.

SD card enumeration

Only two states are needed as the entire functionality is handled by the USB MSD class driver.


The SD card reader data structure holds information on the USB device handle, the current state of the task, and whether the USB interface is detached or not.

SD card structure


Application File: app_sdcard_reader_task.c

Open the app_sdcard_reader_task.c file. This file contains the SD card reader task initialization routine and the SD card task function.

The APP_SDCARD_READER_Initialize routine initializes the task to a known state.

The APP_USBDeviceEventHandler function notifies the application of USB MSD events.

The APP_SDCARD_READER_Tasks function opens the USB device and registers the USB device layer event handler. After this, the task enters the run state. From here on, the USB MSD driver handles all the events and there is nothing that the application needs to handle for USB SD card read/write functionality.

The APP_SDCARD_READER_AttachDevice and APP_SDCARD_READER_DetachDevice functions attach and detach the USB device respectively.

Application File: app_button_press_task.h

Open the app_button_press_task.h file. This file defines the enums, data, and APIs related to the button press task.

The APP_BUTTON_PRESS_TASK_STATE enum helps maintain the states of the button task.

Button press enumeration


The APP_BUTTON_PRESS_TASK_DATA data structure holds the button task's state and button de-bounce related data.

Button press structure


Application File: app_button_press_task.c

Open the app_button_press_task.c file. This file initializes and runs the button press task.

The Press Task button uses the system timer service to implement de-bouncing. When the user presses the button, while the application is in Player mode, it suspends the audio streaming and un-mounts the file system. It then attaches the USB device and switches to the SD Card Reader mode. Similarly, when the user presses the button, while the application is in the SD Card Reader mode, the USB device is first detached, the tone text file SD card task is initialized and the application mode is switched back to SD Card Playback mode.

Back to Top