180 likes | 202 Views
Embedded System Design Techniques™. Writing Portable and Robust Firmware in C. Class 3: Uart Driver and Stm32CubeMx. September 2, 2015 Jacob Beningo, CSDP. Course Overview. Portable C Concepts Doxygen and PC-Lint Uart Driver and Stm32CubeMx Assertions and printf Robust Firmware Concepts.
E N D
Embedded System Design Techniques™ Writing Portable and Robust Firmware in C Class 3: Uart Driver and Stm32CubeMx September 2, 2015Jacob Beningo, CSDP
Course Overview • Portable C Concepts • Doxygen and PC-Lint • Uart Driver and Stm32CubeMx • Assertions and printf • Robust Firmware Concepts
Session Overview • STM32CubeMx • Uart Driver Architecture • Uart Driver HAL • Uart Driver Implementation • Uart Application Code
Setup Stm32CubeMx USART Settings Keil from Project->Settings GENERATE CODE!
Architecture of a UART Driver • Many different ways to organize code • Break the code up into different layers • Driver Layer • Contains MCU peripheral drivers • Generic initialization functions • Mcu start-up code and copy down • Possibly BSP for external chips
UART Driver - Design Step 1 – Identify WHAT the module needs to do • USART Driver • Initialize USART Pins and Peripheral • Transmit byte data • Receive byte data Step 2 – Identify WHAT is needed by each module task • USART Driver • Initialize USART Pins and Peripheral • Input: configuration data • Transmit byte data • Input: char byte • Input: USART channel • Receive byte data • Input: USART channel • Output: char byte
UART Driver - Design Step 3 – Determine the data types • USART Driver • Initialize USART Pins and Peripheral • Input: configuration data • Type: UartConfig_t • Transmit byte data • Input: char byte • uint8_t / char • Input: USART channel • UartChannel_t • Receive byte data • Input: USART channel • Type: UartChannel_t • Output: char byte • Type: chart / uint8_t
UART Driver HAL Step 4 – Convert interface to a header file void Uart_Init(constUartConfig_t *Config); uint8_t Uart_PutChar(UartChannel_t Channel, char Ch ); uint8_t Uart_GetChar(UartChannel_t Channel); void Uart_SetBaud(constUartConfig_t *Config, uint8_t Channel); uint8_t Uart_IsDataPresent(UartChannel_t Channel); void Uart_PrintString( char *Str, UartChannel_t Channel ); void Uart_PutDec(uint8_t Dec, UartChannel_t Channel ); void Uart_PutHex( intToPrint, UartChannel_t Channel ); void Uart_PutHexWord( unsigned intToPrint, UartChannel_tChannel ); void Uart_PutHexDWord( unsigned long ToPrint, UartChannel_t Channel ); uint8_t Uart_Char2Hex(char Ch); uint8_t Uart_Hex2Char(char Hex); void Uart_TxIFClear(UartChannel_t Channel);
UART Driver - Design Step 5 – Define underlying types and definitions typedefenum { UART1, UART2, UART_MAX_CHANNEL }UartChannel_t; typedefenum { UART_STOP1, UART_STOP2, UART_MAX_STOP }UartStop_t; Consult the Datasheet! typedefstruct { UartChannel_tUartChannel; UartState_tUartState; . . UartStop_tStopBits; }UartConfig_t; typedefenum { UART_DISABLE, UART_ENABLE, UART_MAX_STATE }UartState_t;
Driver Testing // Add to Task_100ms.c static uint8_t Buffer[14] = "Hello World!\n"; static uint8_t Index = 0; Uart_PutChar(UART1, Buffer[Index]); Index = (Index + 1) % 14; TxPin = PA_9 RxPin = PA_10
Testing the Application // main.c Uart_AppInit(); // tsk_100ms.c Uart_AppTx(Buffer, 14);
Additional Resources • Download Course Material for • Updated C Doxygen Templates (May 2015) • Example source code • Templates • Microcontroller API Standard • EDN Embedded Basics Articles • Embedded Bytes Newsletter From www.beningo.com under - Blog and Articles > Software Techniques > CEC Writing Portable and Robust Firmware in C
Newsletters • Embedded Bytes • http://bit.ly/1BAHYXm • Training • MicroPython • Bootloaders • Low Power Design • Real-time Software • C/C++ Embedded P.O. Box 400 Linden, Michigan 48451 www.beningo.com : jacob@beningo.com : 810-844-1522 : Jacob_Beningo : Beningo Engineering : JacobBeningo : Embedded Basics Jacob Beningo Principal Consultant