Step 7: Add Code to USB Debug Application Task
The application is already developed and is available in the following files.
- app_usb.c
- app_usb.h
They are available under
<your unzipped folder path>\getting_started_pic32_wfi32e\Lab3\dev_files.
The application file 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\Lab3\dev_files folder and copy the pre-developed files:
- app_usb.c
- app_usb.h
Paste and replace (overwrite) your project files in <your harmony 3 project folder path>\PIC32_WFI32E\firmware\src with the copied files.
Tip: Search for the string "Step #" in the target file to locate the position where you are supposed to write the code.
2
Open app_usb.c and follow the steps below.
1
Create a queue for print operations over USB.
Search for "Step #4a":
- Create a queue using the FreeRTOS function xQueueCreate(uxQueueLength, uxItemSize), capable of containing 10 pointers to char (string).
- uxQueueLength is the size of the queue. Use the pre-defined macro APP_USB_QUEUE_SIZE (for this queue, the size is 10).
- uxItemSize is the item size in the queue. Use the pre-defined macro APP_USB_QUEUE_ITEM_SIZE (for this queue, it is a size of a pointer to a char).
- This function returns the reference of the queue. Use USBQueueHandle to store the handler.
2
Fill the USB queue with the string to print.
Search for "Step #4b":
- Here you will fill the content of the function bool APP_PRINT_STRING(char *str, bool isrFlag).
- Because this queue doesn't have a size of one element, the function xQueueOverwrite() cannot be used.
- So here, you will use either the FreeRTOS function xQueueSendToBack() or xQueueSendToBackFromISR() if the function is called from an Interrupt Service Routine (ISR).
- Check the status of the variable isr, which is available in the scope of the function APP_PRINT_STRING().
- xQueueSendToBack( xQueue, pvItemToQueue, xTicksToWait )
- xQueue is the handle of the queue. Use USBQueueHandle.
- pvItemToQueue is a pointer to the item that is to be placed on the queue. Use the address of the pointer str (&str), which is passed from the other applications through APP_PRINT_STRING().
- xTicksToWait is the maximum time that the function should take to write the item into the queue. Here, use one sec, which is pre-defined in APP_QUEUE_DELAY.
- The function returns pdTRUE if the data was successfully sent to the queue.
- xQueueSendToBackFromISR( xQueue, pvItemToQueue, pxHigherPriorityTaskWoken )
- xQueue is the handle of the queue. Use USBQueueHandle.
- pvItemToQueue is a pointer to the item that is to be placed on the queue. Use the address of the pointer str (&str), which is passed from the other applications through APP_PRINT_STRING().
- pxHigherPriorityTaskWoken to be set to pdFALSE because the USB task does not have a higher priority than the other tasks that can call this function.
- The function returns pdTRUE if the data was successfully sent to the queue.
3
Locate the state machine code in APP_USB_Tasks().
Search for "APP_USB_STATE_NOT_READY"
- The code for the states APP_USB_STATE_NOT_READY and APP_USB_STATE_IDLE have been already implemented.
- In APP_USB_STATE_IDLE, the code implements the following FreeRTOS functions:
- uxQueueMessagesWaiting() to wait until the queue is filled.
- uxQueueReceive() to read from the message queue.
Search for "Step #5":
- In the APP_USB_STATE_PRINT state, use the function SYS_DEBUG_MESSAGE() to print the received string.