Basics - Embedded - Programming - and - GPIO
Basics - Embedded - Programming - and - GPIO
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
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:
• 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
• 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
• 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
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.
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)
{
/* GPIO Configuration */
GPIOConfig();
/* 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);
}
}