Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
26 views

Basics - Embedded - Programming - and - GPIO

Basics_Embedded _Programming_and_GPIO

Uploaded by

shankskyfall
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Basics - Embedded - Programming - and - GPIO

Basics_Embedded _Programming_and_GPIO

Uploaded by

shankskyfall
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 45

UNIT –II Preamble to Embedded C data Types and

Programming
Software Development flow in Embedded System :
Steps- Software Development
• Compile and link: Many files will be compiled in a project and .obj files
are generated, there will be separate linking operation to create an image
file
• Flash programming: The image file is downloaded into flash memory

• Execute program and debug: The execution of the code , one line at a
time, can be provided in an Integrated Development Environment (IDE)
and any bug is seen can be rectified.
Common Software Compilation Flow
Various Tools for Embedded Development Suite
• Compiler: To compile C program files into object files
• Assembler: To assemble assembly code files into object files
• Linker: A tool to join multiple object files together and define memory
configuration
• Flash programmer :A tool to program the compiled program image to the
flash memory of the microcontroller
• Debugger: A tool to control the operation of the microcontroller and to access
internal operation information so that status of the system can be examined
and the program operations can be checked
• Simulator: A tool to allow the program execution to be simulated without
real hardware
The Cortex microcontroller software interface standard (CMSIS)
• CMSIS was developed by ARM to allow microcontroller and software vendors to
use a consistent software infrastructure to develop software solutions for Cortex -
M microcontrollers

• The standardization of the way the software infrastructure works becomes


necessary to ensure software compatibility with various development tools and
between different software solutions .
• The CMSIS allows easy access to processor core features from the C language.
• In addition, once it is learnt to use one Cortex-M microcontroller product, starting
to use another Cortex-M product is much easier because of the consistency in
software setup
CMSIS Contd….
• The CMSIS allows easy access to processor core features from the C
language
• Openness - the source code for CMSIS core files can be downloaded and
accessed by everyone, and everyone can develop software products with
CMSIS
• These include the registers in the Nested Vector Interrupt Controller (NVIC),
a system tick timer in the processor (SysTick), an optional Memory
Protection Unit (MPU), various programmable registers in the System
Control Block (SCB)
IAR Workbench – IDE For STM
Data types in Embedded C programming:
• The C programming language supports a number of “standard” data
types: int, char, double, long…
• The way data item is represented in hardware depends on processor
architecture as well as compiler
• Therefore the data types in Embedded ‘C’ programming will be
controller /processor specific
• For example ‘__IO uint32_t’ indicates a 32 bit unsigned integer, the “__IO”
is defined in a standardized header file in CMSIS. It implies a volatile data item
(e.g., a peripheral register), which can be read or written to by CMSIS
software
• Data types supported in Embedded ‘C’ Programming
C and C99 (stdint.h) Data Number of Bits Range (Signed) Range (Unsigned)
Type

char, int8_t, uint8_t 8 -128 to 127 0 to 255


short int16_t, uint16_t 16 -32768 to 32767 0 to 65535
int, int32_t, uint32_t 32 -2147483648 to 0 to 4294967295
2147483647
Long 32 -2147483648 to 0 to 4294967295
2147483647

Byte - 8-bit
Half word - 16-bit
Word -32-bit
Double word -64-bit
Embedded ‘C’ Data types used in Hardware programming.
• The data types ,to get recognised need Header files. To use this data
type, the project needs to include the standard data type header
Programming STM 32 Controller
• Microcontrollers have various Input/Output (I/O) interfaces and
peripherals such as timers, Real-time Clock (RTC) etc.
• GPIO, SPI, UART, I2C , ADCs (Analog to Digital Converters) and
DACs (Digital to Analog Converters) are the main peripherals of the
STM Microcontroller
• The peripherals are memory-mapped, which means the registers are
accessible from the system memory map
• The peripherals registers in C programs can be accessed using pointers
• Typically, a peripheral requires an initialization process before it can be
used
Software Device Driver
• In order to help microcontroller software developers, microcontroller vendors usually
provide header files and C codes that include:

• Definitionsof peripheral registers


• Access functions for configuring and accessing the peripherals

• By adding these files to software projects, one can access various peripheral functions
via function calls and access peripheral registers easily
Steps to program a Peripheral

• Programming the clock control circuitry to enable the clock signal


connection to the peripheral, and clock signal connection to
corresponding I/O pins
• Clocks to peripherals are turned off by default and you need to enable the clock before
programming the peripheral
• In some cases you might also need to enable the clock to the peripheral bus system

• I/O pins are multiplexed that can be used for multiple purposes
• Hence configuration the operation mode of the I/O pins is needed
• Peripherals contain a number of programmable registers that need
configuration before using the peripheral.
Steps to program a Peripheral
• Peripherals on 32-bit microcontroller, STM 32 are much more
sophisticated than peripherals on 8-bit/16-bit systems

• Hence vendors will provide device-driver library code and one can use
these driver functions to reduce the programming work required

• If a peripheral is to be used with interrupt operations, the interrupt


controller on the Cortex-M3/M4 processor (NVIC) will be programed
to enable the interrupt and to configure the interrupt priority level
Memorymapped peripheral registers acting as pointers
• /* STM32F 100RBT6B e GPIO A Port Configuration Register Low */
• #define GPIOA_CRL (*((volatile unsigned long *) (0x40010800)))
• /* STM32F 100RBT6B e GPIO A Port Configuration Register High */
• #define GPIOA_CRH (*((volatile unsigned long *) (0x40010804)))
• /* STM32F 100RBT6B e GPIO A Port Input Data Register */
• #define GPIOA_IDR (*((volatile unsigned long *) (0x40010808)))
• /* STM32F 100RBT6B e GPIO A Port Output Data Register */
• #define GPIOA_ODR (*((volatile unsigned long *) (0x4001080C)))
• /* STM32F 100RBT6B e GPIO A Port Bit Set/Reset Register */
• #define GPIOA_BSRR(*((volatile unsigned long *) (0x40010810)))
• /* STM32F 100RBT6B e GPIO A Port Bit Reset Register */
• #define GPIOA_BRR (*((volatile unsigned long *) (0x40010814)))
• /* STM32F 100RBT6B e GPIO A Port Configuration Lock Register */
• #define GPIOA_LCKR (*((volatile unsigned long *) (0x40010818)))
• void GPIOA_reset(void) /* Reset GPIO A */
• {
• // Set all pins as analog input mode
• GPIOA_CRL = 0; // Bit 0 to 7, all set as analog input
• GPIOA_CRH = 0; // Bit 8 to 15, all set as analog input
• GPIOA_ODR = 0; // Default output value is 0
• return;
•}
Structure definition in Embedded C coding

• When there are multiple instantiations of the same peripheral, for


example, the STM32 microcontroller has five GPIO peripherals, and
the same definition has to be repeated for each of the instantiations.
This is not scalable and makes it hard for software maintenance

• It is not easy to create a function that can be shared between multiple


instantiations of the same peripheral.
Structure Definition in Peripherals

• typedef struct
• { __IO uint32_t CRL;
• __IO uint32_t CRH;
• __IO uint32_t IDR;
• __IO uint32_t ODR;
• __IO uint32_t BSRR;
• __IO uint32_t BRR;
• __IO uint32_t LCKR; } GPIO_TypeDef;
Each peripheral base address (GPIO A to GPIO G) is
defined as pointers to the data structure
• For Example:
• #define PERIPH_BASE ((uint32_t)0x40000000)
• /*!< Peripheral base address in the bit-band region */
• #define GPIOA ((GPIO_TypeDef *) GPIOA_BASE)
Using Peripheral with a pointer

• void GPIO_reset(GPIO_TypeDef* GPIOx)


• {
• // Set all pins as analog input mode
• GPIOx->CRL = 0; // Bit 0 to 7, all set as analog input
• GPIOx->CRH = 0; // Bit 8 to 15, all set as analog input
• GPIOx->ODR = 0; // Default output value is 0
• return;
• }
• To use this function, we just need to pass the peripheral base pointer to the function:
• GPIO_reset(GPIOA); /* Reset GPIO A */
• GPIO_reset(GPIOB); /* Reset GPIO B */
GPIO -STM
16th April 2024
General Purpose Input Output
• GPIO
• AMR – Absolute Maximum Rating
• PP – Push Pull
• PU – Pull Up
• PD – Pull Down
• OD –Open Drain
• AF – Alternate Functions
X= A to H
• GPIOx_MODER: GPIO port mode register
• GPIOx_OTYPER: GPIO output type register
• GPIOx_OSPEEDR: GPIO output speed register
• GPIOx_PUPDR: GPIO port pull-up / pull-down register
• GPIOx_IDR: GPIO port input data register
• GPIOx_ODR: GPIO port output data register
• GPIOx_BSRR: GPIIO port it set / reset register
• GPIOx_LCKR: GPIO port configuration lock register
• GPIOx_LCKR: GPIO port configuration lock register
• GPIOx_AFRL: GPIO alternate function low register
• GPIOx_AFRH: GPIO alternate function high register
• GPIOx_ASCR: GPIO port analog switch control register
Types of Registers
• Control Registers
• Data Registers
• Locking Registers
• Alternate Function Registers
1.Input data register (GPIOx_IDR)
2.Output data register (GPIOx_ODR)
3.Bit Set/Reset register (GPIOx_BSRR)

This is the Input Data Register. When you configure the GPIO ports as
input using GPIOx_MODER register,
this register is used to get the value from the GPIO pin. This register is a
read-only register. So you cannot write into it.

Let’s assume that I have configured PORT B as input, using


the GPIOB_MODER register and other control registers.
Now we can read the GPIO pins like below
Output Data Register
This is the Output Data Register. When you have configured
GPIO Port as output using GPIOx_MODER register,
this register is used to set the value to the GPIO pin. We can
read and Write
Making Port B as input
Open Drain Mode
• In open-drain mode, inside the microcontroller one switch
(transistor/MOSFET) is connected to the GPIO pin and the
ground.
• So If you write high to the GPIO pin using software, it will be
connected to the ground through the switch. Which means the
original output is low.
• If you write low to the GPIO pin, it will be left floating since the
switch will be turned off. That’s why we are using a pullup
resistor for the open-drain pins.
Push – Pull Mode
Push-pull mode, two switches (transistor/MOSFET) will be there
inside of the microcontroller. One switch is connected to Vcc/Vdd
and another switch is connected to the ground. So when you
write High to the GPIO pin, the switch will be connected to the
Vcc/Vdd.

The resulting output will be high (1). And if you write low to the
GPIO, then the switch will be connected to the ground. The
resulting output will be low (0).
STM32 GPIO exhibits the following features:
• Output states: push-pull, or open drain + pull-up / pull-down according to
GPIOx_MODER, GPIOx_OTYPER, and GPIOx_PUPDR registers settings
• Output data from output data register GPIOx_ODR or peripheral (alternate function
output)
• Speed selection for each I/O (GPIOx_OSPEEDR)
• Input states: floating, pull-up / pull-down, analog according to GPIOx_MODER,
GPIOx_PUPDR and GPIOx_ASCR registers settings
• Input data to input data register (GPIOx_IDR) or peripheral (alternate function input)
• Bit set and reset register (GPIOx_ BSRR) for bitwise write access to GPIOx_ODR
• Locking mechanism (GPIOx_LCKR) provided to freeze the I/O port configurations
• Analog function selection registers (GPIOx_MODER and GPIOx_ASCR)
• Alternate function selection registers (GPIOx_MODER, GPIOx_AFRL, and
GPIOx_AFRH)
• Fast toggle capable of changing every two clock cycles
• Highly flexible pin multiplexing allowing the use of I/O pins as GPIO or as one of
several peripheral function
Functioning of port
• Input floating
• Input pull-up
• Input-pull-down
• Analog
• Output open-drain with pull-up or pull-down capability
• Output push-pull with pull-up or pull-down capability
• Alternate function push-pull with pull-up or pull-down capability
• Alternate function open-drain with pull-up or pull-down capability
GPIO Mode Description
• When a STM32 device I/O pin is configured as input, one of three options must be
selected:
• Input with internal pull-up. Pull-up resistors are used in STM32 devices to ensure a well-
defined logical level in case of floating input signal. Depending on application
requirements, an external pull-up can be used instead.

• Input with internal pull-down. Pull-down resistors are used in STM32 devices to ensure
a well-defined logical level in case of floating input signal. Depending on application
requirements, an external pull-down can be used instead.

• Floating input. Signal level follows the external signal. When no external signal is
present, the Schmitt trigger randomly toggles between the logical levels induced by the
external noise. This increases the overall consumption
Programmed as Input
• The output buffer is disabled
• The Schmitt trigger input is activated
• The pull-up or pull-down resistors are activated depending on the
value in the GPIOx_PUPDR register
• The data present on the I/O pin is sampled into the input data
register at each AHB clock cycle
• The I/O state is obtained by reading the GPIOx_IDR input data
register
• Input Configuration
• Output Configuration
• Alternate function Configuration
• HSE or LSE – External Clock is on, corresponding port functions are
ignored
Configuration Registers for Ports
Configurations
•Up to 16 I/Os under control
•Output states: push-pull or open-drain + pull-up/down
•Output data from output data register (GPIOx_ODR) or peripheral (alternate function output)
•Speed selection for each I/O
•Input states: floating, pull-up/down, analog
•Input data to input data register (GPIOx_IDR) or peripheral (alternate function input)
•Bit set and reset register (GPIOx_BSRR) for bitwise write access to GPIOx_ODR
•Locking mechanism (GPIOx_LCKR) provided to freeze the I/O configuration
•Alternate function input/output selection registers (at most 16 AFs per I/O)
•Fast toggle capable of changing every two clock cycles
•Highly flexible pin multiplexing allows the use of I/O pins as GPIOs or as one of several peripheral
function

•Analog function
Code to toggle an LED with a delay in Bit Set Reset Mode
/* Includes ------------------------------------------------------------------*/
#include "stm32f0xx.h"
#include "Config.h"

/**
* @brief Main program.
* @param None
* @retval None
*/
int main(void)
{

/* Initialising syetm clocks */


SysClockConfiguration();

/* GPIO Configuration */
GPIOConfig();

/* Configure TIM6 for Delay function */


DelayTimerConfig();

/* Infinite loop */
while (1)
{
/* SET Pin 8 */
GPIOC->BSRR |= (1U<<8U);
/* Delay of 1 second */
Delay_ms(1000);
/* RESET Pin 8 */
GPIOC->BSRR |= (1U<<24U);
/* Delay of 1 second */
Delay_ms(1000);

/* SET Pin 9 */
GPIOC->BSRR |= (1U<<9U);
/* Delay of 1 second */
Delay_ms(1000);
/* SET Pin 9 */
GPIOC->BSRR |= (1U<<25U);
/* Delay of 1 second */
Delay_ms(1000);
}
}

You might also like