====== Lab07 - Nucleo Communication stack ====== The communication between Nucleo board and PC uses RS232 data link. The C language allows you to use multiple levels of abstraction as it is described in the following figure. The operating system (OS) level uses open, write, read, close function and the C standard library uses fopen, fwrite, fread and fclose functions. While the C standard library provides the same behavior on the all platforms the OS level can vary with implementation. The NUCLEO side uses direct access to the CPU registers with functions USART_ReceiveData and USART_SendData. These functions have two parameters: the first is a pointer to the memory location with USART peripheral and the second is the character to be sent and received. {{ :courses:be5b99cpl:labs:rs232.png?direct |}} ===== The NUCLEO stack ===== The NUCLEO side is expected to wait for the characters in a pooling mode which will be implemented as: void main( void ) { init(); while(1) { getCharacter(); insertCharacterIntoBuffer(); } } The reception management of the characters requires a data structure which is implemented as: #define cRECV_BUFF_MAX 10 typedef struct tRecvBuff { char chArrRecvBuff[cRECV_BUFF_MAX]; int iCnt; } tRecvBuff; tRecvBuff oRecvBuff; The structure is initialized as: oRecvBuff.chArrRecvBuff[0] = 0; oRecvBuff.iCnt = 0; And the reception is performed through: while(1) // Don't want to exit { uint16_t Data; while(USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == RESET); // Wait for Char Data = USART_ReceiveData(USART2); // Collect Char oRecvBuff.chArrRecvBuff[ oRecvBuff.iCnt ] = (char)Data; oRecvBuff.iCnt++; -- check the overflow of the array !! cRECV_BUFF_MAX -- wait for the end of the command !! propose a proper ending of the command, e.g. '\r' and '\n' } // END while(1) ===== Exercise ===== * Define and initialize necessary data structures to hold string commands sent over RS232 (such as "*IDN?\r\n"). * Define the mechanism of a "complete" command detection (based on '\r' and '\n'). * Define the mechanism of buffer clearing after successful processing of the received command. * Detect the "*IDN?" command and reply with a description of your "device". * Detect the "LED ON" and "LED OFF" commands to be able to control the User LED of the Nucleo board. * Detect the "BUTTON?" command to be able to read the User Button state.