|
USBVirtualSerial_FreeRTOS
Communicate with Micropendous board using Serial Port software and techniques
The latest version of this firmware is available in the latest Micropendous Distribution or via SVN in the /Micropendous/Firmware/USBVirtualSerial_FreeRTOS directory. Purpose: USBVirtualSerial_FreeRTOS combines the LUFA USB library with the preemptive FreeRTOS kernel to enable programming Micropendous boards without worrying about USB. All USB functionality is hidden. The preemptive FreeRTOS kernel allows for multitasking user code and LUFA USB functions. USB Mode: Device-mode enumerating as USB Virtual Serial Port Targets: Micropendous2, Micropendous3, or Micropendous4, or other USB AVR boards with more than 2kbytes of SRAM. Status: Working. Please post any problems you encounter to the Micropendous Group. UsageAssuming you have completed all setup tasks in the QuickStart tutorial, simply compile and upload the USBVirtualSerial-FreeRTOS firmware onto your Micropendous by following the ProgramAndTest tutorial. USBVirtualSerial-FreeRTOS programmed devices will enumerate as a Virtual Serial Port and any data sent to the device using Serial will be echoed back. Firmware DesignThis firmware is based on USBVirtualSerial. The preemptive http://www.freertos.org/FreeRTOS kernel was added to allow multi-tasking and hide USB functionality from the programmer. Standard stdin and stdout/stderr streams are created in main() and linked to the USB Virtual Serial Port. This means you can use standard printf and getchar functions to read and write data over the USB connection. Just place your code in Main_Task(). See avr-libc stdio for detailed instructions on using these functions. Note: DO NOT alter Timer1 settings as it is used by FreeRTOS. Connect to a board running this firmware with a Serial Terminal and any data you send will be looped back. The example is very simple. haveData() checks if data has been received from the host and then sends it back over the USB serial connection. /* Main_Task will run periodically once initialization is complete */
void MainTask(void)
{
int tempInt = 0; // temporary storage - integer (16-bit)
// If the host has sent data then process it
// Example 1 - using standard IO functions
while (haveData()) { // need to check that data exists before processing it
// store data temporarily then send it right back - raw binary - using standard IO functions
tempInt = getchar();
putchar(tempInt);
}
// Example 2 - using formatted standard IO functions
/*while (haveData()) {
// as long as getchar() is returning data, process it
printf_P(PSTR("\r\nUSBVirtualSerial\r\n")); // send a string that is constant and stored in FLASH
tempInt = getchar(); // receive a character
printf("Received char = %3d\r\n", tempInt); // send a string that is dynamic and stored in SRAM
// avoid long dynamic strings as there is little SRAM
}
*/
}
The above example is very simple. haveData() checks if data has been received from the host and then stdio functions getchar() and putchar(...) are used to read and resend the data over the USB serial connection. You can also use printf(...) functions but note that printf() is stored entirely in SRAM so it is best to use printf_P(PSTR("..."); to store and print any constant strings. See the printf tutorial for more information. FreeRTOS use is accomplished by using xTaskCreate() to add USB, CDC, and Main_Task functions to the FreeRTOS kernel and then start the kernel with vTaskStartScheduler() in main(). Thus FreeRTOS handles the USB and user functions and separates their functionality. User code (even if it busy-loops) cannot interfere with USB functionality as FreeRTOS pre-empts it by running USB functions periodically. On the host side, serialpy.py is an example of serial communication and is explained in Serial. |