Step 7: Add code to the SDCARD application
The application is already developed and is available in the following files.
- app_sdcard.c
- app_sdcard.h
They are available under <your unzipped folder path>\getting_started_pic32_wfi32e\Lab2\dev_files
The application file app_sdcard.c contains the application logic. It also contains placeholders that you will populate with the necessary code.
1
Go to the <your unzipped folder path>\getting_started_pic32_wfi32e\Lab2\dev_files folder and copy the pre-developed files:
- app_sdcard.c
- app_sdcard.h
Paste and replace (overwrite) the files of your project at <your harmony 3 project folder path>\PIC32_WFI32E\firmware\src with the copied files.
2
Open app_sdcard.c in MPLAB X IDE and add the application code as shown in the following steps.
Tip: Search for the string "Step #" in the target file to locate the position where you are supposed to write the code.
Note: The code snippets have not been added to the application. You need to write them according to the lab recommendations to understand how to use the drivers, system services, peripheral libraries, registers, handle callbacks, and the overall application flow.
a
The Sensor Task will trigger the data log into the SD card by using the custom function APP_SDCARD_Notify(). This function takes the temperature value as an argument and transmits it to the SD Card Task.
Search for Step #1:
- Fill the function APP_SDCARD_Notify() to make the Sensor Task able to communicate with the SD Card Task.
- Store the temperature in the global variable appSDcardData.temperature.
- Set the flag appSDcardData.isTemperatureReady.
- Open app_sensor.c and make a call to the function APP_SDCARD_Notify() at the end of the state APP_SENSOR_STATE_USB_PRINT in the state machine. This will trigger the SD Card Task when the temperature value is ready to be logged into the SD card.
b
Set the RTC time to current system time. The current system time is the time when the file is compiled. The value is read using the compiler macro TIME from the XC32 Compiler.
The macros have been already pre-defined in app_sdcard.h.
Open app_sdcard.c.
Search for Step #2a:
- Fill the element tm_hour of the structure sys_time with the hour of the system.
Search for Step #2b:
- Fill the element tm_min of the structure sys_time with the minute of the system.
Search for Step #2c:
- Fill the element tm_sec of the structure sys_time with the second of the system.
Search for Step #2d:
- Set the RTCC Time.
c
Register an event handler (callback) for interrupt on GPIO pin RB2 to detect the SD card plugged into the IO1 Xplained Pro Board.
Search for Step #3a:
- Register a callback function that helps to retrieve the state of the GPIO pin RB2 and monitor if the SD card is plugged or unplugged.
Search for Step #3b:
- Enable the interrupt on GPIO pin RB2.
d
Implement the necessary code in the GPIO Event Handler function void APP_SDCARD_DETECT_EventHandler(GPIO_PIN pin, uintptr_t context) to detect if the SD card is present on the slot and check if the SD card was removed during a long session.
Search for Step #4:
- Check if the event occurred for the pin GPIO_PIN_RB2.
- Get the state of the GPIO pin RB2 using the function GPIO_RB2_Get().
- When the SD card is present in the slot, the pin is grounded.
- When there is no SD card plugged, the level on the pin is high via the pull-up configured.
- Store the state of the SD card using the flag appSDcardData.isThereSDcard.
- Optionally, you can implement the necessary code to detect if the SD card is removed during an active state (APP_SDCARD_STATE_MOUNT, APP_SDCARD_STATE_UNMOUNT, APP_SDCARD_STATE_OPEN, APP_SDCARD_STATE_CLOSE, APP_SDCARD_STATE_WRITE).
e
Initialize the flag appSDcardData.isThereSDcard at power-up.
Search for Step #5:
- Get the state of the GPIO pin RB2 using GPIO_RB2_Get() and store the result in appSDcardData.isThereSDcard.
f
Now, it is time to implement the state machine of the SD Card Task and fill the function APP_SDCARD_Tasks().
Search for Step #6:
- In the state APP_SDCARD_STATE_IDLE, jump to the next step (APP_SDCARD_STATE_MOUNT) if the flag appSDcardData.isTemperatureReady has been toggled by the Sensor Task.
- Optionally, turn on the embedded Red LED using the BSP function LED_RED_On() to indicate the application is running.
g
It is required to mount the SD card first before using it.
Search for Step #7:
- Make sure the SD card is plugged into the slot by reading the flag appSDcardData.isThereSDcard.
- To mount the SD card, you will use the following API:
- You will populate the API with the following arguments:
- devName is the volume name to mount. You will use "/dev/mmcblka1" pre-defined in the macro APP_DEV_NAME.
- mountName is the name that refers to the path for the file. You will use "/mnt/mydrive1" pre-defined in the macro APP_MOUNT_NAME.
- filesystemtype is the type of file system. You will use FAT.
- mountflags is reserved. Therefore, you will use .
- data is reserved. There you will use NULL.
- It is possible the mount does not succeed the first time. It is recommended to try several times by monitoring the result returned by the API SYS_FS_Mount. The API should return SYS_FS_RES_SUCCESS if the SD card is successfully mounted.
- Once the SD card is mounted:
- Set the drive APP_MOUNT_NAME by using the API SYS_FS_CurrentDriveSet(). With that, you don't need to specify the volume in the path when accessing your files.
- Create the directory "dir1" which is pre-defined in the macro APP_DIR_NAME with the help of the function SYS_FS_DirectoryMake().
- Finally, jump to the next state (APP_SDCARD_STATE_OPEN) if successfully mounted.
h
Once the SD card is mounted, it is time to open a file.
Search for Step #8:
- To open a file, you will use the following API:
- You will populate the API with the following arguments:
- fName is the name of the file to be opened along with the path. You will use the pre-defined macro APP_LOG_FILE_PATH.
- attributes is the access mode of the file. You will use the mode SYS_FS_FILE_OPEN_APPEND to overwrite the file.
- On success, the function returns a valid file handle. You will store it in appSDcardData.FShandle.
- As the previous step, it is recommended to try opening the file several times by monitoring the result of the handle. If it fails, the API should return SYS_FS_INVALID.
- Move the state machine to the next state (APP_SDCARD_STATE_WRITE) if the operation succeeds. Otherwise, move the state machine to APP_SDCARD_STATE_ERROR.
i
From here, you will be able to write into the file opened.
Search for Step #9a:
- First, record the RTC time with the help of the PLIB function TRCC_TimeGet(struct tm *Time).
- Use the pre-defined structure sys_time.
Search for Step #9b:
- Use the function to prepare the payload to be written into the file.
- You will populate the API with the following arguments:
- log is the string of the log. You will use log_data.
- time is the structure filled by the call to the function RTCC_TimeGet(). You will use sys_time.
- temperature is the value to add to the log. Here, you will use appSDcardData.temperature.
Search for Step #9c:
- Use the API SYS_FS_FileStringPut() to append the string to the file without erasing the previous log.
- Also here, before moving to the next state (APP_SDCARD_STATE_CLOSE), it is recommended to try writing into the file several times by monitoring the value returned by the API.
j
Once the log is written into the file, the next step is to close the file.
Search for Step #10:
- Use the function SYS_FS_FileClose() with the handle stored in appSDcardData.handle.
- Also here, before you move to the next state (APP_SDCARD_STATE_UNMOUNT), it is recommended to try closing the file several times by monitoring the value returned by the API.
k
Once the file is closed, unmount the SD card.
Search for Step #11:
- Use the function SYS_FS_Unmount() with the volume name pre-defined in APP_MOUNT_NAME.
- Also here, before you move back to the IDLE state (APP_SDCARD_STATE_IDLE) and being ready for the next log, it is recommended to try to unmount the SD card several times by monitoring the value returned by the function.
l
Handle error will inform the user when the machine is entering into the APP_SDCARD_STATE_ERROR state.
Search for Step #12:
- Add necessary code to inform the user using LED toggle and message printing.