Baremetal C Programming for Embedded Systems

Slides:



Advertisements
Similar presentations
purpose Search : automation methods for device driver development in IP-based embedded systems in order to achieve high reliability, productivity, reusability.
Advertisements

Refer to Chapter 6, 9 in the reference book
Programming the ATmega16
AVR32 GPIO CS-423 Dick Steflik. What is a GPIO GPIO – General Purpose Input/Output  Flexible software control digital signal  Each GPIO represents a.
Programmable logic and FPGA
AVR Programming CS-212 Dick Steflik. ATmega328P I/O for our labs To get data into and out of our Arduino its a little trickier than using printf and.
Getting the O in I/O to work on a typical microcontroller Activating a FLASH memory “output line” Part 1 Main part of Laboratory 1 Also needed for “voice.
ECE Lecture 1 1 L15 – Digital I/O Department of Electrical and Computer Engineering The Ohio State University ECE 2560.
Development. Development Environment Editor Assembler or compiler Embedded emulator/debugger IAR Embedded Workbench Kickstart Code Composer Essentials.
1 EKT 225 MICROCONTROLLER I CHAPTER 3 I/O PORT PROGRAMMING.
OOPic Programming Fundamentals Compiled vs. Interpreted programs –Compiled program converts high level code into native machine language e.g., y=x+5; Machine.
UART and UART Driver B. Ramamurthy.
1 ARM University Program Copyright © ARM Ltd 2013 General Purpose I/O.
Teachers Name : Suman Sarker Telecommunication Technology Subject Name : Microcontroller & Embedded System Subject Code : 6871 Semester : 7th Department.
C Tokens Identifiers Keywords Constants Operators Special symbols.
1 ARM University Program Copyright © ARM Ltd 2013 General Purpose I/O.
Microcontroller based system design Asst. Prof. Dr. Alper ŞİŞMAN.
ECS642U Embedded Systems Digital I/O William Marsh.
ECS642U Embedded Systems Cyclic Execution and Polling William Marsh.
Embedded Network Interface (ENI). What is ENI? Embedded Network Interface Originally called DPO (Digital Product Option) card Printer without network.
1-3 GPIO_Output(LuminaryLibrary) 1.Alter the output current to 4mA 2.Let LED0 turn on but LED 1 turn off. Modify your program in E:\yourname\arm.
LHO 22 C and the  The Silicon Labs ISE uses the Keil C51 compiler.  The code size is limiter to 2K  C has replaced PL/M (the original Intel high.
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.
© 2008, Renesas Technology America, Inc., All Rights Reserved 1 Introduction Purpose  This training course demonstrates the Project Generator function.
Presented by: © 2015 Jacob Beningo All Rights Reserved Writing Portable and Robust Firmware in C September 4, 2015 Jacob Beningo, CSDP Class 5: Robust.
ARM Embedded Programming Lecture 4 Accessing Memory Mapped Registers.
1 The LPC1768 Architecture (with focus on Cortex-M3)
C++ Lesson 1.
Embedded System Design Techniques™:
Chapter 3 General-Purpose Processors: Software
CS4101 嵌入式系統概論 General Purpose IO
Basic Computer Organization and Design
Embedded Software Development with Python and the Raspberry Pi
Struct Properties The C struct mechanism is vaguely similar to the Java/C++ class mechanisms: - supports the creation of user-defined data types - struct.
Struct Properties The C struct mechanism is vaguely similar to the Java/C++ class mechanisms: - supports the creation of user-defined data types - struct.
Introduction to C Programming
Build a microSD Bootloader using a PIC microcontroller
Data types Data types Basic types
Embedded Software Development with Python and the Raspberry Pi
Microcontroller Applications
Lesson Outline Peripherals Memory-Mapped IO Ports GPIO Multiplexing
Refer to Chapter 10 in the reference book
CS4101 嵌入式系統概論 General Purpose IO
Freescale ARM I/O Programming
UART and UART Driver B. Ramamurthy.
Student Book An Introduction
C Basics.
Introduction to C Programming Language
Blinking an LED Using MSP430ware to Control GPIO
An Introduction to Embedded Software Architecture and Design
Baremetal C Programming for Embedded Systems
UART and UART Driver B. Ramamurthy.
Embedded Software Development with Python and the Raspberry Pi
An Introduction to Embedded Software Architecture and Design
Writing Portable and Robust Firmware in C
Writing Portable and Robust Firmware in C
February 26, 2015 Jacob Beningo, CSDP
ECE 3567 Microcontrollers Dr. Gregg J Chapman
Introducing the PIC Mid-Range Family and the 16F84A
EE 319K Introduction to Embedded Systems
Baremetal C Programming for Embedded Systems
8051 Micro Controller.
Explaining issues with DCremoval( )
An Introduction to Embedded Software Architecture and Design
2. Second Step for Learning C++ Programming • Data Type • Char • Float
An Introduction to Embedded Software Architecture and Design
An Introduction to Embedded Software Architecture and Design
Embedded System Design Techniques™:
February 24, 2015 Jacob Beningo, CSDP
Speaker: Yu-Ju Cho 卓余儒 Advisor: Prof. An-Yeu Wu 吳安宇教授
Presentation transcript:

Baremetal C Programming for Embedded Systems Class 3: Driver Design Techniques February 25, 2015 Jacob Beningo, CSDP

Course Overview C Concepts for Embedded Systems Baremetal Scheduling Techniques Driver Design Techniques Design Patterns for Firmware Writing Portable Code

Session Overview Reviewing the bits Direct Register Access Drivers using structs Portable Driver Concepts A DIO (GPIO) Driver

Reviewing the Bits Bit Manipulation Operator Example Clear bit 4 Reg &=~0x10; 0b10111111 (Original) 0b10101111 (Negated) 0b10101111 (& Result) Set bit 4 Reg |= 0x10; 0b10101111 (Original) 0b10111111 (OR’d) 0b10111111 (| Result) Toggle bit 4 Reg ^= 0x10; 0b10111111 (Original) 0b10101111 (^ Result) Shift left 2 then right 2 Reg <<= 2; 0b10101111 (Original) 0b10111100 (<< Result) Reg >>= 2; 0b10111100 (New Original) 0b00101111 (>> Result)

Direct Register Access // GPIO Port A is located at 0x1000 uint32_t * Gpio_PortA = (uint32_t *) 0x1000U; // Set the 0 bit high on PortA *Gpio_PortA |= 0x01; // Valid Gpio_PortA++; // Invalid! ------------------------------------------------------------------------------------------------------------ uint32_t * volatile const Gpio_PortA = (uint32_t *) 0x1000U;

Struct Based Drivers /** GPIO - Peripheral register structure */ typedef struct GPIO_MemMap { uint32_t PDOR; /**< Port Data Output Register, offset: 0x0 */ uint32_t PSOR; /**< Port Set Output Register, offset: 0x4 */ uint32_t PCOR; /**< Port Clear Output Register, offset: 0x8 */ uint32_t PTOR; /**< Port Toggle Output Register, offset: 0xC */ uint32_t PDIR; /**< Port Data Input Register, offset: 0x10 */ uint32_t PDDR; /**< Port Data Direction Register, offset: 0x14 */ } volatile *GPIO_MemMapPtr; Example #define PORTA_BASE_PTR ( (GPIO_MemMapPtr)0xF80FF000u) PORTA_BASE_PTR->PDIR |= (1UL << 23); PORTA_BASE_PTR++;

Portable Driver Concepts 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

Portable Driver Concepts Application Layer Product features Scheduler Applications for drivers Ex Button response behavior Configuration Layer Configuration for drivers Configuration for applications Application settings

GPIO Driver Example // GPIO_CONFIG.h /** * Defines the digital input/output configuration table elements that are used * by Dio_Init to configure the Dio peripheral. */ typedef struct { uint8_t Channel; /**< The I/O channel */ uint8_t Implement; /**< Pin Implemented - TRUE or FALSE */ uint8_t Resistor; /**< Pullup/Pulldown Resistor - DISABLED, PULLUP, or PULLDOWN */ uint8_t SlewRate; /**< Pin Slew Rate - SLOW or FAST */ uint8_t DriveStrength; /**< Pin Drive Strength - HIGH or LOW */ uint8_t PassiveFilter; /**< Passive Input Filter - ENABLED or DISABLED */ uint8_t Direction; /**< Data Direction - OUTPUT or INPUT */ uint8_t Data; /**< Data - HIGH or LOW */ uint8_t Function; /**< Mux Function - Dio_FunctionSelect */ }Gpio_ConfigType;

GPIO Driver Example const Gpio_ConfigType Dio_Config[] = { // // Channel, Direction, Data, Function { SWD_CLK, OUTPUT, HIGH, GPIO, }, { PORTA_1, OUTPUT, HIGH, GPIO, }, { PORTA_2, OUTPUT, HIGH, GPIO, }, { PORTA_3, OUTPUT, HIGH, GPIO, }, { PORTA_4, OUTPUT, HIGH, GPIO, }, { PORTA_5, OUTPUT, HIGH, GPIO, }, { PORTA_6, OUTPUT, HIGH, GPIO, }, { PORTA_7, OUTPUT, HIGH, GPIO, } }; typedef enum { SWD_CLK, PORTA_1, PORTA_2, PORTA_3, PORTA_4, PORTA_5, PORTA_6, PORTA_7 }GpioChannelType;

GPIO Driver Example #ifdef __cplusplus extern "C"{ #endif void Gpio_Init(const Gpio_ConfigType *Config); uint8_t Gpio_ReadChannel(Gpio_ChannelType Channel); void Gpio_WriteChannel(Gpio_ChannelType Channel, uint8_t State); void Gpio_ToggleChannel(Gpio_ChannelType Channel); void Gpio_SetChannelMode(Gpio_ChannelType Channel, uint8_t Mode); void Gpio_SetFuncMode(Gpio_ChannelType Channel, Gpio_FunctionSelect Func); } // extern "C"

Pointer Arrays What are Pointer Arrays? A pointer array is an array where each element of the array is a pointer to a peripheral register of a particular type. How do they help? Groups registers into logical channels Simplifies initialization functions Easily ported Cross platform through templates Abstracts the register into something readable and understandable by a human. Creates design patterns that can be reused

GPIO Driver Example /** * Defines a table of pointers to the Port Data Output Register on the * microcontroller */ uint32_t volatile * const ports[NUM_PORTS] = { (uint32_t*)&GPIOA_PDOR, (uint32_t*)&GPIOB_PDOR, (uint32_t*)&GPIOC_PDOR, (uint32_t*)&GPIOD_PDOR, (uint32_t*)&GPIOE_PDOR };

Pointer Arrays Key Features of the declaration portsddr is a const pointer The register that is being pointed to in each index of the pointer array will always be the same so it is declared as a const portsddr points to a volatile uint16 The volatile keyword here is used to identify that the uint16 that is being pointed to (hardware register) may change without the software modifying it. Depending on how the registers are declared by the compiler, the register addresses may need to be cast to a pointer in order to silence the compilers objections.

GPIO Driver Example void Dio_Init(const Dio_ConfigType * Config) { uint8_t i = 0; // Loop counter variable uint8_t PortIndex = 0; // Port Number uint8_t PinIndex = 0; // Pin Number // Loop through all pins, set the data register bit and the data direction // register bit according to the dio configuration table values for (i = 0; i < NUMBER_PORT_PINS; i++) PortIndex = Config[i].Channel / NUMBER_PORT_PINS; PinIndex = Config[i].Channel % NUMBER_PORT_PINS; // Set the Data register bit for this channel if (Config[i].Data == HIGH) *ports[number] |= (1UL<<(PinIndex)); } else *ports[number] &= ~(1UL<<(PinIndex));

GPIO Driver Example uint8_t Dio_ReadChannel(Dio_ChannelType Channel) { return ((*portsin[Channel/NUM_PINS_PER_PORT] & (1UL<<(Channel%NUM_PINS_PER_PORT))) ? HIGH : LOW); } void Dio_WriteChannel(Dio_ChannelType Channel, uint8_t State) if (State == HIGH) *ports[Channel/NUM_PINS_PER_PORT] |= (1UL<<(Channel%NUM_PINS_PER_PORT)); else *ports[Channel/NUM_PINS_PER_PORT] &=

Additional Resources Download Course Material for Updated C Doxygen Templates (Feb 2015) Example drivers source code Microcontroller API Standard EDN Embedded Basics Articles Embedded Bytes Newsletter From www.beningo.com under - Blog and Articles > Software Techniques > CEC Baremetal C Programming

Jacob Beningo Newsletters P.O. Box 400 Embedded Bytes Linden, Michigan 48451 www.beningo.com Newsletters Embedded Bytes http://bit.ly/1BAHYXm Training MicroPython Bootloaders Low Power Design Real-time Software C/C++ Embedded : jacob@beningo.com : 810-844-1522 : Jacob_Beningo : Beningo Engineering : JacobBeningo : Embedded Basics Jacob Beningo Principal Consultant 18