Presented by: 2015 Jacob Beningo All Rights Reserved Writing Portable and Robust Firmware in C...

18
Presented by: © 2015 Jacob Beningo All Rights Reserved Writing Portable and Robust Firmware in C September 2, 2015 Jacob Beningo, CSDP Class 3: Uart Driver and Stm32CubeMx Embedded System Design Techniques™

description

Presented by: © 2015 Jacob Beningo All Rights Reserved Session Overview STM32CubeMx Uart Driver Architecture Uart Driver HAL Uart Driver Implementation Uart Application Code 3

Transcript of Presented by: 2015 Jacob Beningo All Rights Reserved Writing Portable and Robust Firmware in C...

Page 1: Presented by:  2015 Jacob Beningo All Rights Reserved Writing Portable and Robust Firmware in C September 2, 2015 Jacob Beningo, CSDP Class 3: Uart Driver.

Presented by:

© 2015 Jacob BeningoAll Rights Reserved

Writing Portable and Robust Firmware in C

September 2, 2015Jacob Beningo, CSDP

Class 3: Uart Driver and Stm32CubeMx

Embedded System Design Techniques™

Page 2: Presented by:  2015 Jacob Beningo All Rights Reserved Writing Portable and Robust Firmware in C September 2, 2015 Jacob Beningo, CSDP Class 3: Uart Driver.

Presented by:

© 2015 Jacob BeningoAll Rights Reserved

Course Overview

• Portable C Concepts• Doxygen and PC-Lint• Uart Driver and Stm32CubeMx• Assertions and printf• Robust Firmware Concepts

2

Page 3: Presented by:  2015 Jacob Beningo All Rights Reserved Writing Portable and Robust Firmware in C September 2, 2015 Jacob Beningo, CSDP Class 3: Uart Driver.

Presented by:

© 2015 Jacob BeningoAll Rights Reserved

Session Overview

• STM32CubeMx• Uart Driver Architecture• Uart Driver HAL• Uart Driver Implementation• Uart Application Code

3

Page 4: Presented by:  2015 Jacob Beningo All Rights Reserved Writing Portable and Robust Firmware in C September 2, 2015 Jacob Beningo, CSDP Class 3: Uart Driver.

Presented by:

© 2015 Jacob BeningoAll Rights Reserved

Setup Stm32CubeMx

4

USART Settings

Keil from Project->Settings

GENERATE CODE!

Page 5: Presented by:  2015 Jacob Beningo All Rights Reserved Writing Portable and Robust Firmware in C September 2, 2015 Jacob Beningo, CSDP Class 3: Uart Driver.

Presented by:

© 2015 Jacob BeningoAll Rights Reserved

Architecture of a UART Driver

5

• 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

Page 6: Presented by:  2015 Jacob Beningo All Rights Reserved Writing Portable and Robust Firmware in C September 2, 2015 Jacob Beningo, CSDP Class 3: Uart Driver.

Presented by:

© 2015 Jacob BeningoAll Rights Reserved

UART Driver - Design

6

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 taskUSART 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

Page 7: Presented by:  2015 Jacob Beningo All Rights Reserved Writing Portable and Robust Firmware in C September 2, 2015 Jacob Beningo, CSDP Class 3: Uart Driver.

Presented by:

© 2015 Jacob BeningoAll Rights Reserved

UART Driver - Design

7

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

Page 8: Presented by:  2015 Jacob Beningo All Rights Reserved Writing Portable and Robust Firmware in C September 2, 2015 Jacob Beningo, CSDP Class 3: Uart Driver.

Presented by:

© 2015 Jacob BeningoAll Rights Reserved

UART Driver HAL

8

void Uart_Init(const UartConfig_t *Config);uint8_t Uart_PutChar(UartChannel_t Channel, char Ch );uint8_t Uart_GetChar(UartChannel_t Channel);

void Uart_SetBaud(const UartConfig_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( int ToPrint, UartChannel_t Channel );void Uart_PutHexWord( unsigned int ToPrint, 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);

Step 4 – Convert interface to a header file

Page 9: Presented by:  2015 Jacob Beningo All Rights Reserved Writing Portable and Robust Firmware in C September 2, 2015 Jacob Beningo, CSDP Class 3: Uart Driver.

Presented by:

© 2015 Jacob BeningoAll Rights Reserved

UART Driver - Design

9

typedef enum{ UART1, UART2, UART_MAX_CHANNEL}UartChannel_t;

typedef struct{ UartChannel_t UartChannel; UartState_t UartState; . . UartStop_t StopBits; }UartConfig_t;

Step 5 – Define underlying types and definitions

typedef enum{ UART_DISABLE, UART_ENABLE, UART_MAX_STATE}UartState_t;

typedef enum{ UART_STOP1, UART_STOP2, UART_MAX_STOP}UartStop_t;

Consult the Datasheet!

Page 10: Presented by:  2015 Jacob Beningo All Rights Reserved Writing Portable and Robust Firmware in C September 2, 2015 Jacob Beningo, CSDP Class 3: Uart Driver.

Presented by:

© 2015 Jacob BeningoAll Rights Reserved

Uart Driver Init

10

Page 11: Presented by:  2015 Jacob Beningo All Rights Reserved Writing Portable and Robust Firmware in C September 2, 2015 Jacob Beningo, CSDP Class 3: Uart Driver.

Presented by:

© 2015 Jacob BeningoAll Rights Reserved

Uart Driver Tx

11

Page 12: Presented by:  2015 Jacob Beningo All Rights Reserved Writing Portable and Robust Firmware in C September 2, 2015 Jacob Beningo, CSDP Class 3: Uart Driver.

Presented by:

© 2015 Jacob BeningoAll Rights Reserved

Uart Driver Rx

12

Page 13: Presented by:  2015 Jacob Beningo All Rights Reserved Writing Portable and Robust Firmware in C September 2, 2015 Jacob Beningo, CSDP Class 3: Uart Driver.

Presented by:

© 2015 Jacob BeningoAll Rights Reserved

Driver Testing

13

// Add to Task_100ms.cstatic uint8_t Buffer[14] = "Hello World!\n";static uint8_t Index = 0;

Uart_PutChar(UART1, Buffer[Index]);Index = (Index + 1) % 14;

TxPin = PA_9RxPin = PA_10

Page 14: Presented by:  2015 Jacob Beningo All Rights Reserved Writing Portable and Robust Firmware in C September 2, 2015 Jacob Beningo, CSDP Class 3: Uart Driver.

Presented by:

© 2015 Jacob BeningoAll Rights Reserved

A Ring Buffer Application

14

Page 15: Presented by:  2015 Jacob Beningo All Rights Reserved Writing Portable and Robust Firmware in C September 2, 2015 Jacob Beningo, CSDP Class 3: Uart Driver.

Presented by:

© 2015 Jacob BeningoAll Rights Reserved

A Ring Buffer Application

15

Page 16: Presented by:  2015 Jacob Beningo All Rights Reserved Writing Portable and Robust Firmware in C September 2, 2015 Jacob Beningo, CSDP Class 3: Uart Driver.

Presented by:

© 2015 Jacob BeningoAll Rights Reserved

Testing the Application

16

// tsk_100ms.cUart_AppTx(Buffer, 14);

// main.cUart_AppInit();

Page 17: Presented by:  2015 Jacob Beningo All Rights Reserved Writing Portable and Robust Firmware in C September 2, 2015 Jacob Beningo, CSDP Class 3: Uart Driver.

Presented by:

© 2015 Jacob BeningoAll Rights Reserved

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

17

Page 18: Presented by:  2015 Jacob Beningo All Rights Reserved Writing Portable and Robust Firmware in C September 2, 2015 Jacob Beningo, CSDP Class 3: Uart Driver.

Presented by:

© 2015 Jacob BeningoAll Rights Reserved

Jacob Beningo Principal Consultant

P.O. Box 400 Linden, Michigan 48451

www.beningo.com

: [email protected]

: 810-844-1522

: Jacob_Beningo

: Beningo Engineering

: JacobBeningo

: Embedded Basics

Newsletters•Embedded Bytes

http://bit.ly/1BAHYXm

Training•MicroPython•Bootloaders•Low Power Design•Real-time Software•C/C++ Embedded

18