Getting Started and Peripheral Coding STM32 MCU's STM32F1

GPIO Peripheral in STM32F103 MCU

Table of Contents

GPIO Theory

So now  we’ll talk about GPIO pins . GPIO stands for general purpose input output pins; they are the means for communication between the microcontroller and the external world (using sensors etc).It is signal that arrives on these pins or a signal that is sent or written on these pins that facilitates this communication. These pins can be configured to act as input or output  via the application software(in our case stm32cube ide). The programmer can configure the pins as LOW or 0V or HIGH or 3.3V(or 5V ). 

These GPIO pins can also be configured to act as  special purpose pins as well where their alternate functionality is exploited . These alternate functionality includes UART , ADC , SPI etc . In the case of UART communication, Transmitter(Tx) and Receiver(Rx) pins are required. GPIO pins can be configured to act as TX or RX pins . Even in ADC the pins are configured to act as Analog pins having 12 bits of resolution . The alternate functions of various pins are shown below:

  • Input floating 

  • Input pull-up

  • Input pull-down

  • Analog

  • Output open-drain

  • Output push-pull

  • Alternate function push-pull

  • Alternate function open-drain

GPIO Peripheral in STM32F103

All the pins of STM32F103 are grouped in multiple ports as PORT A, PORT B, PORT C As can be seen from Pin configuration chart in the PA1 stands for Port A Pin 1. There are 37 GPIO pins in stm32f103 which are divided as PORT A with 16 pins, PORT B with 16 pins, PORT C with 3 pins and PORT D with 2 pins.

  • Each GPIO  port  has two 32-bit configuration registers (GPIOx_CRL, GPIOx_CRH), two 32-bit data registers (GPIOx_IDR, GPIOx_ODR), a 32-bit set/reset register (GPIOx_BSRR), a 16-bit reset register (GPIOx_BRR) and a 32-bit locking register (GPIOx_LCKR).
  • In the register names, x stands for the port to which pin belongs. If we are configuring pin PA1, it has Port A then registers would be accessed by GPIOA_CRL and etc.
  • Out of above-mentioned registers, GPIO peripheral has 2 most important registers:

GPIO Peripheral Block Diagram in STM32F103

  1. First after selecting the pin the port is decided
  2. Then after following the arrow the busses are selected based on it: APB/APB1 or AHB
  3. After which the clock is enabled to the particular port using either __HAL_RCC_GPIOX_CLK_ENABLE() function or using the RCC AHB1 peripheral clock enable register and selecting the port to which clock has to be provided by enabling it.

Methods to configure the GPIO Peripheral

Output configuration of the pin
Input configuration of the pin
Enter the label or the name of the pin
Configure the pin according to the various parameters
  • Configuring the busses i.e AHB1, AHB2, APB1, APB2 . The AHB bus is faster than APB bus and in case of certain modules they are connected to the same bus .Hence it depends upon the application which bus to use. As can be seen from the picture below the AHB1 takes clock to PORT A , PORT B , PORT C etc . Hence to initialize a pin to a particular port the in RCC AHB1 clock enable register GPIOEN is set to 1 (For Port A GPIOAEN , For Port B GPIOBEN etc)
  • Enabling the clock to that port otherwise the particular pin will not be functional
  •  Creating an instance of the structure and then using the members of the structure set the following:-
  1. PIN – Takes the pin no as input GPIO_PIN_X {where X -0 to 15} 
  2. MODE– Selects the mode the specified pin is supposed to work in . It takes in value Output Push Pull ,Output Open drain
  3. PULL- It selects the initial value of the pin and takes value no pull up no pull down, pull up or pull down
  4. SPEED- Selects the speed of the working of the specified pin i.e low, medium or high
  5. ALTERNATE- Specifies the alternate function performed by the pin UART TX OR RX , ADC etc,

GPIO Peripheral SDK using STM32HAL

We are going to use STM32 HAL SDK for using the GPIO peripheral of the STM32F103. STM32HAL is a very versatile and robust Software package for using Peripherals of the STM32 Microcontroller family. To know more about STM32HAL, refer to this link.

STM32 HAL Folder Structure

Each STM32 HAL has drivers for all the peripherals of the STM32 Microcontroller(One can navigate to the Driver folder in the STM32F1 HAL local repo installed). These drivers can be configured and enabled to use in the project via the STM32 CubeMX configuration tool, which is also integrated into STM32CubeIDE( just like told in the above section for configuring GPIO peripherals). Will be digging into that part, in the next section. For now, let’s understand the STM32HAL GPIO SDK for STM32F103 MCU.

  • stm32f1xx_hal_gpio.c: This file consists of various macros and  is responsible for the intialization and configuration of the functions which in turn configures the peripheral.
  • stm32f1xx_hal_gpio.h: consists of various structure definitions that help configure various parameters of the pin, enumeration, and various macros
  • stm32f1xx_ll_gpio.c & stm32f1xx_ll_gpio.h: GPIO Low-level driver source/header file, contains functions that configure the GPIO Peripheral registers at the hardware level. These files are the ones that actually interact with the hardware and make it configurable to our needs. 

STM32 HAL Functions for GPIO Peripheral

Functions are set of instructions that required to perform certain tasks. In general, a function is first declared in header file(.h) and then it is definied in source file(.c) and then called in main.c or application code.

It is of the form function return data type, function name and function arguments.

				
					// Basic introduction of functions in c
void func(int x);
int main()
{
func(x);
}
void func(int x)
{/*function body/*}
				
			

In Embedded functions are required to initialize a peripheral or configure it on the basis of various parameters which are passed on using arguments. This information is then passed on to the registers. 

List of functions used for GPIO HAL:

 

FUNCTION NAME 

  •   HAL_RCC_GPIOX_CLK_ENABLE()

FUNCTION DESCRIPTION

  This API is used to enable the clock to a particular port that will be delivered using AHB OR  APB busses. This function basically sets the APBENR bit in RCC register and after a delay enables the clock.

RETURN VALUE

              NONE

USAGE

				
					static void MX_GPIO_Init(void)
{
 

  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOC_CLK_ENABLE();
}
				
			

 FUNCTION NAME

  • void HAL_GPIO_Init (GPIO_TypeDef* GPIOx, GPIO_InitTypeDef *GPIO_Init)

 FUNCTION DESCRIPTION

This API takes 2 arguments as input that are pointer to structure GPIO_TypeDef* GPIOx this is usually used to specify the port (A,B etc) and GPIO_InitTypeDef *GPIO_Init which is basically the pointer to the structure GPIO_InitTypeDef this contains information of the pin configuration such as pin , mode , speed , pull , alternate.  This API basically initializes the specified pin according to configuration specified in GPIO_Init.

PARAMETERS (ARGUMENTS)

                           GPIOx:specifies the PORT ie X(A,B,C)

                           GPIO_Init: Pointer to GPIO_InitTypeDef that has the configuration information of the GPIO peripheral

RETURN VALUE

              NONE

USAGE

				
					static void MX_GPIO_Init(void){
 /*STEP 2*/
HAL_GPIO_WritePin(led_GPIO_Port, led_Pin, GPIO_PIN_RESET); 
GPIO_InitStruct.Pin = led_Pin; 
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(led_GPIO_Port, &GPIO_InitStruct);
  }

				
			

 FUNCTION NAME

  • GPIO_PinState  HAL_GPIO_ReadPin (GPIO_TypeDef * GPIOx, uint16_t GPIO_Pin)

 FUNCTION DESCRIPTION

This API  is used to Read the value from a specified pin and it returns value of the type GPIO_PinState which is an enumeration in which GPIO_PIN _RESET has been assigned the value 0U and anything apart from this is GPIO_PIN_SET. This API takes in 2 arguments as input that are pointer to structure GPIO_TypeDef* GPIOx this is usually used to specify the port (A,B etc) and uint16_t GPIO_Pin this specifies the pin to be chosen and takes value from 0-15.

PARAMETERS (ARGUMENTS)

              GPIOx:specifies the PORT ie X(A,B,C) 

             GPIO_Pin:specifies the pin or the port bit to read from i.e from 0-15.

RETURN VALUE

             The input pin state value

USAGE

				
					uint8_t buttonval = 0;
  while (1)
  {
	buttonval=HAL_GPIO_ReadPin(BTN_GPIO_Port, BTN_PIN);
}
				
			

 FUNCTION NAME            

  • void HAL_GPIO_WritePin (GPIO_TypeDef * GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState)

  FUNCTION DESCRIPTION

This API is used to write either a HIGH or LOW at the specified pin . It takes in 3 arguments as input that are pointer to structure GPIO_TypeDef* GPIOx this is usually used to specify the port (A,B etc) , uint16_t GPIO_Pin this specifies the pin to be chosen and takes value from 0-15 and GPIO_PinState PinState which is an enum of the type GPIO_PinState which takes in value SET or RESET.

   PARAMETERS (ARGUMENTS)

              GPIOx:specifies the PORT ie X(A,B,C) 

              GPIO_Pin:specifies the pin or the port bit to read from i.e from 0-15.

               PinState:specifies the state to be written on the specified pin.These values are:

                            SET- To write a HIGH on the pin

                            RESET- To the clear or write LOW on the pin 

    RETURN VALUE

             NONE

USAGE

				
					while (1)
  {
    /* USER CODE END WHILE */
	  HAL_GPIO_WritePin(led_GPIO_Port, led_Pin, GPIO_PIN_SET);
}
				
			

FUNCTION NAME

  • void HAL_GPIO_TogglePin (GPIO_TypeDef * GPIOx, uint16_t GPIO_Pin)

FUNCTION DESCRIPTION

This API is used to toggle or switch the state of the specified pin high to low or low to high. This API also takes in 2 arguments as input that are pointer to structure GPIO_TypeDef* GPIOx this is usually used to specify the port (A,B etc) and uint16_t GPIO_Pin this specifies the pin to be chosen and takes value from 0-15.

 PARAMETERS (ARGUMENTS)

              GPIOx:specifies the PORT ie X(A,B,C) 

              GPIO_Pin:specifies the pin or the port bit to read from i.e from 0-15.

 RETURN VALUE

             NONE

USAGE

				
					while (1)
  {
    /* USER CODE END WHILE */
	  HAL_GPIO_TogglePin(led_GPIO_Port, led_Pin, GPIO_PIN_SET);
}
				
			

Data Types Involved

  • uint8_t
  • uint16_t
  • uint32_t
  • GPIO_InitTypeDef
  • GPIO_PinState
  1. uint8-t– This data type is used to store unsigned value of 8 bits which means that only positive or zero value can be stored in the data type .Ranging from 0-255. This data type can be used to store state of a pin in a variable
  2. uint16_t– This data type stores unsinged values of 16 bits . The range in this case is 0-65535.This data type can be used to define pins of a port as there are 16 pins in port A
  3. uint32_t-This data types stores unsigned values of 32 bits i.e it stores non-negative values in the range 0 to 4,294,967,295. This data type stores 4 bytes per element.
  4. GPIO_InitTypeDef- A structure that helps configure pin state such as:-
  • PIN-It defines the pin to be selected
  • MODE-It defines the mode the specified pin has to be set in such as input , output , alternate function and even in these modes push pull , open drain etc 
  • PULL- It sets the pin in 3 modes which are nopull , pull up , pull down
  • SPEED- It defines the speed or frequency  of the operation which can be low, medium , high

    5.GPIO_Pinstate- An enumerator that stores the pin state which can be SET or RESET

DEMO EXERCISE

1  CODE FOR BLINKING THE ON BOARD LED PC13 WITH A DELAY OF 1s

				
					MX_GPIO_Init();/*STEP 1*/
 while (1)
  {
    /* USER CODE END WHILE */
	  HAL_GPIO_WritePin(led_GPIO_Port, led_Pin, GPIO_PIN_SET);/ *STEP 6*/
	  HAL_Delay(1000);/*STEP 7*/
	  HAL_GPIO_WritePin(led_GPIO_Port, led_Pin, GPIO_PIN_RESET); /*STEP 8*/
	  HAL_Delay(1000);
    /* USER CODE BEGIN 3 */
  }
static void MX_GPIO_Init(void){
_HAL_RCC_GPIOC_CLK_ENABLE(); /*STEP 2*/
HAL_GPIO_WritePin(led_GPIO_Port, led_Pin, GPIO_PIN_RESET); /*STEP 3*/
GPIO_InitStruct.Pin = led_Pin; /*STEP 4*/
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(led_GPIO_Port, &GPIO_InitStruct); /*STEP 5*/

				
			

STEPS INVOLVED

Create a function to configure all  the  pins involved in the application  . Configuration such as clock enable  ,  pin state initially, Pin no , mode, pull, speed. This function also involves HAL_GPIO_Init function that initializes the pin and has the information of its configurations.

Enable clock to the specified port in this case PORTC

Specify the initial state of the pin

 

Specify the pin no  ( used a label for the pin led_Pin)  the original pin no is 13 in this case. Also configure the pin mode which takes value output push pull or output open drain. Next configure the pull method which takes value no pull up  and no pull down in our case.Configure the speed of the peripheral according to requirement in this case we are choosing low frequency . 

 

Create the function specifying the port and all the information about the pin configuration

Write value to the pin as SET or HIGH 

Include a delay in our case it is 1000ms or 1s using HAL_Delay() function

Write value to the pin as RESET or LOW . Since all this is in a while (1) loop the led will continue to blink


 

NOTE-The above exercise can also be performed by replacing the 2  HAL_GPIO_WritePin(led_GPIO_Port, led_Pin, GPIO_PIN_SET),  HAL_GPIO_WritePin(led_GPIO_Port, led_Pin, GPIO_PIN_RESET) with the HAL_GPIO_TogglePin(led_GPIO_Port, led_Pin) and since the HAL API is called in the while loop the led will blink forever in a loop.

2 TURNING THE LED ON OR OFF USING A PUSH BUTTON

				
					MX_GPIO_Init();/*STEP 1*/
uint8_t buttonval = 0;
  while (1)
  {
	buttonval=HAL_GPIO_ReadPin(BTN_GPIO_Port, BTN_PIN);/ *STEP 7*/
    /* USER CODE END WHILE */
    if(buttonval == 0){
    	HAL_GPIO_WritePin(led_GPIO_Port, led_Pin, GPIO_PIN_SET);/ *STEP 8*/

    }
    else{
    	HAL_GPIO_WritePin(led_GPIO_Port, led_Pin, GPIO_PIN_RESET);/ *STEP 9*/
    }
    /* USER CODE BEGIN 3 */
  }


static void MX_GPIO_Init(void)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};

  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOC_CLK_ENABLE();/*STEP 2*/
  __HAL_RCC_GPIOA_CLK_ENABLE();

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(led_GPIO_Port, led_Pin, GPIO_PIN_RESET);/*STEP 3*/

  /*Configure GPIO pin : led_Pin */
  GPIO_InitStruct.Pin = led_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(led_GPIO_Port, &GPIO_InitStruct);/*STEP 5*/

  /*Configure GPIO pin : BTN_Pin */
  GPIO_InitStruct.Pin = BTN_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  HAL_GPIO_Init(BTN_GPIO_Port, &GPIO_InitStruct);/*STEP 6*/

}

				
			

STEPS INVOLVED

Create a function to configure all  the  pins involved in the application  . Configuration such as clock enable  ,  pin state initially, Pin no , mode, pull, speed. This function also involves HAL_GPIO_Init function that initializes the pin and has the information of its configurations 

Enable clock to the specified port in this case PORTC

Specify the initial state of the pin

Specify the pin no  ( used a label for the pin led_Pin)  the original pin no is 13 in this case. Also configure the pin mode which takes value output push pull or output open drain. Next configure the pull method which takes value no pull up  and no pull down in our case.Configure the speed of the peripheral according to requirement in this case we are choosing low frequency . 

Create the function specifying the port and all the information about the pin configuration(PORT C PIN 13)

Create the function specifying the port and all the information about the pin configuration(PORT A ,PIN 0)

Initialize a variable buttonval to read the state of the Pin PA0 using HAL api GPIO_ReadPin

Write value to the pin as SET or HIGH when the pin is pressed

Write value to the pin as RESET or LOW when the button is not pressed . Since all this is in a while (1) loop the led will continue to blink


 

Author

UART Peripheral in STM32F103

For the people looking to start with STM32 and looking for its full tutorial blog series on different peripherals of the STM32 Microcontrollers. In This blog get to know about, UART HAL API’s, how to generate the UART code and demo examples based on UART peripheral of STM32.

Read More »

Introduction on embedded c

Embedded C is an extremely popular programming language when we talk about electronic devices. If you wanna go for robotics it would be a good start for you. In our day-to-day life, we use mobile, laptops, and fridges every electronic device we use is made up of using embedded C. Now without wasting any time let\’s dive a bit deep into it by looking into a very basic example Blinking LED using Embedded C. Blinking Led is a hello world for embedded C which means this is the first basic code that takes us into the world of embedded C. #define F_CPU 16000000UL#include <avr/io.h>#include <util/delay.h>int main(void){DDRB|= (0B00100000);while (1){PORTB=0B00100000;_delay_ms(1000);PORTB=0B11011111;_delay_ms(1000);}return(0);} Let’s understand our code from top to bottom. #define F_CPU 16000000UL Let’s break it into 3 parts #define F_CPU Here we are defining the clock speed of the processor. 16000000 Here we are setting the clock speed at 16Mhz as our atmega328p’s default value is 1Mhz hence 16Mhz will make it 16 times slower. UL Here we are doing nothing but declaring the data type of the clock speed. We used unsigned long because clock speed cant is negative. #include <avr/io.h> Whenever we want to use libraries of some function from some other source or even we wanna use our own code we use #include.  #include <avr/io.h>  includes a header file that contains code for using pins, ports, etc. for the Avr microcontroller. #include <util/delay.h> This library is used to put delays in our code. int main(void) The main function is when the AVR starts executing code. While(1) While loops execute the code inside it until the condition inside the parentheses remains true. We all know in C 1 refers to true and 0 refers to false. Here the code inside the while loop will run again and again because 1 can never be false. return(0); If we don\’t write return(0) at the end of the code we will get an error for sure. This happens because our operating system needs confirmation that the code we ran is running properly. This is more of a line of confirmation. You must be wondering what these words DDRB or PORTB are? Well, these are called hardware registers which are extremely important to understand so let\’s get to know what they are and what they do? Hardware Registers In our Atmega series of Avr, we have mainly 3 hardware registers. DDRx PORTx PINx Here x is referred to the bank. We have three of’em B, C, D.Every bank contains 8 pins. Depending on which bank’s pin you are using we chose the bank. DDRx[Data-Direction registers] DDRx configures the pins as output or input. As we are using an 8-bit microcontroller. The default value of the DDR is 0 which means whenever we give power it is not giving any output. If we want to set it for the output we need to set the bit we want to use for sending the output as 1. Output = 1Input    = 0DDRB = 0b00000000       DDRB = 0b00100000 Here 0b is telling us that we are writing the number in binary. We all know 0 refers to the ‘off’ and 1 refers to the ‘on’. Hence the above example is telling us at first all pins were off later on we set pin 5 as 1 for giving output. If you are familiar with Arduino Ide then you can relate DDR with pinMode.      PORTx[Port x data registers] After setting the DDRx bits to the 1(output), The port registers the voltage as HIGH or LOW. When we say the voltage is Low it means the voltage is 0V and if we say voltage is HIGH we consider the voltage as 5V.  HIGH = 1LOW    = 0PORTB = 0b00000000  PORTB = 0b00100000 If we take the example of our code we have seen above we are setting pb5 as output. In the picture you can see above PB5 is connected with pin 13 of Arduino. Now you can see we are turning we Led on which is connected with pin 13 which is connected with PB5 on Atmega 328p. If you are familiar with Arduino Ide then you can relate PORTx with digitalWrite. PINx[port c input data pin address] The pin register addresses are used when we want to read the digital voltage values for each pin we set as input in DDRX. You can relate this pin with digitalRead from Arduino IDE. I believe now it\’s clear what we did in the  above example. In the next blog, we will see how we can write a function and use it for calling. So that we can reduce our writing. Author: Kunal Gupta

Read More »

STM32 Overview

What is STM32 ? STM32 is a family of 32-bit Microcontrollers and development boards manufactured by ST Microelectronics which offer very high performance for a relatively low price. All STM32 microcontrollers are based on ARM Cortex-M processor which make them much better in terms of computing power than most of the common microcontrollers/microcontroller development boards which are available in the market. These microcontrollers also have been provided with software support by ST for developers for free. If you have experience with microcontrollers like Arduino, this is a great step forward in understanding microcontrollers in depth. The STM32 framework allows you to control all peripherals and allows you to tweak them as per your wish, whereas in Arduino IDE, when you program in C++, it comes with in-built functions and libraries which don’t allow you to understand the backend working behind everything. This is very crucial, as it allows you to use the microcontroller to its full potential. STM32 FAMILY The STM32 family of microcontrollers is vast and expansive, offering a variety of options for the consumer to choose from. The family of microcontrollers is broadly classified into 4 categories- High performance ST offers a series of microcontrollers which have the fastest code execution time, high speed data transfer and relatively higher flash memory. Along with this, the feature the most advance peripherals that ST offers. This makes these controllers very useful for real-time applications such as running an RTOS. STM32H7x The H7 series controllers are based on the ARM Cortex M7 offering very high speeds of upto 550Mhz. The microcontroller series comes in two variants, single core and dual core. Dual core H7 series controllers come with an ARM Cortex M4 core as well as an ARM Cortex M7 core which allows for processing work distribution, very useful for applications requiring many peripherals where response time is critical. The dual Core variants also come with an inbuilt SMPS as well. The single core series comes with an ARM Cortex M7 core with clock speed between 220-550Mhz, depending upon the variant. STM32F7x The F7 series controllers are based on the ARM Cortex M7, Thanks to an L1 (level 1) cache, the series delivers the maximum possible theoretical performance of the Cortex-M7 core. Cache memory temporarily stores instructions frequently accessed by the processor STM32F4x – The F4 series is based on the ARM Cortex M4. These controllers have less processing power then H7 and F7 series but still come with advanced peripherals and high flash memory. A very useful line of microcontrollers which offer a wide variety of features and come at a relatively lower cost than that of an F7/H7 controllers. The F4 series is broadly divided into 3 segments- 1.F4 Advance line- Offers up to 180Mhz of clock speed and a maximum of 2 Mb of dual bank flash memory, with advanced peripherals, consisting of communication buses such as QSPI. 2.F4 foundation line- Offer lower flash memory (between 512Kb-1Mb) , with few controllers offering advance peripherals and features such as camera interface and Ethernet MAC. 3.F4 Access line- This line of F4 controllers consists of entry level controller which can be termed ‘High Performance’. These controllers come with lower clock speeds, lower RAM but feature very good power efficiency, while also not compromising on peripherals. Wireless The main focus of the STM32Wx microcontroller family is wireless connectivity. They cover sub-GHz as well as 2.4Ghz frequency range operation The STM32 wireless MCUs are further divided into 2 categories– STM32WB These MCUs have a dual core processor unit, Cortex M4 & Cortex M0+, which allows for processing distribution, making it a great candidate for real-time processing and execution. The MCU series is based on STM32L4 architecture, which offers dynamic voltage scaling, reducing the power consumption of the device. They also support BLE 5.3, Open Thread and Zigbee protocols, making them a great choice for IOT applications, providing a lot of functionality onboard the chip itself. STM32WL These MCUs also come with a dual core processor unit like the STM32WB series, and some variants also come with a single ARM Cortex M4 as their processing unit. The key highlight of this series of microcontrollers is that not only do they work as a general purpose microcontroller, but also come with an integrated sub GHz radio supporting LoRa WAN which makes it a great choice for smart home applications, utilities, logistics, Industrial IOT etc.   Ultra-Low Power – STM32 ultra-low-power microcontrollers offer designers of energy-efficient embedded systems and applications a balance between performance, power, security and cost effectiveness. The portfolio includes the STM8L (8-bit proprietary core), the STM32L4 (Arm® Cortex®-M4), the STM32L0 (Arm® Cortex®-M0+) and the STM32L1 (Arm® Cortex®-M3). The STM32L5 MCU (Arm® Cortex®-M33) with its enhancedsecurity features is the latest addition to this rich portfolio. The new STM32U5 series combines the latest and most efficient Arm Cortex-M33 core with an innovative 40 nm platform that reduces energy consumption to the bone, while increasing performance. The series also adds the state-of-the-art features which are required in today’s applications,including advanced cyber security with hardware-based protection, and graphics accelerators for rich graphical user interfaces. The STM32L5 series enhanced security features leverage Arm® Cortex®-M33 and its TrustZone® for Armv8-M. Thanks to this new core and a new ST ART Accelerator™ (also supporting external memory), the STM32L5 reaches an 443 CoreMark. The STM32L4 series offers the excellence of ST’s ultra-low power platform with an additional performance dimension by providing 100 DMIPS with DSP instructions and Floating-Point Unit (FPU), more memory (up to 1 Mbyte of Flash memory) and innovative features. The STM32L4+ series extends STM32L4 technology by offering higher performance (120 MHz/409 CoreMark executing from internal Flash memory), larger embedded memories (up to 2 Mbytes of Flash memory and 640 Kbytes of SRAM), and rendering advanced graphics without compromising ultra-low-power consumption. The STM32L0 series offers a genuine energy-saving solution for entry-level applications. Available in tiny packages down to 14 pins and with a wide range of Flash memory densities from 8 to 192 Kbytes, the STM32L0 features ultra-low power consumption in a competitive portfolio. Mainstream– The mainstream microcontrollers are more general-purpose boards

Read More »

NXP S32K344 Automotive MCU

Table of Contents About NXP semiconductors NXP Semiconductors is a global semiconductor company that designs and produces a broad range of semiconductor solutions providing hardware and software for them. It is a fabless company that designs the Microcontrollers, Microprocessor, ASICS’s and other discrete electronic components, targeting the industries like but not only limited to automotive, industrial automation, communication infrastructure, IoT, Robotics, Medical, Consumer Electronics, drones and Security Chips. Apart from these chips and IC’s, NXP semiconductors also provide software to run these chips and demonstrating different applications where its products can be used. NXP semiconductor also works in AI/ML and edge computing.Making it not only giant hardware-based semiconductor company but software company too. History of NXP Semiconductors roots back to 1950’s. It was founded in 1953 as part of the electronics giant Philips, at that time the company name is Philips Semiconductors. In 2006 it become independent company and its name changed to present name: NXP Semiconductors. After that it had acquired several companies and had done partnership deals with companies like Silicon labs, STMicroelectronics, Trident Microsystems, Conexant Systems and to expand its product lines and business revenue. In March 2015, merger agreement was announced through which NXP semiconductors acquired The Freescale semiconductors. After this merger NXP semiconductor become one of the largest semiconductor companies in the world. Both NXP and Freescale had deep roots stretching back to when they were part of Philips (NXP), and Motorola (Freescale). NXP primarily focusing on near field communication (NFC) and high-performance mixed signal (HPMS) hardware, and Freescale focusing on its microprocessor and microcontroller businesses. NXP semiconductor chips are used in almost every electronic gadget we see around us. To know more about NXP semiconductors, one can navigate to NXP website and Wiki page. In this blog we are going to start with Automotive Industry. NXP semiconductors has a S32 Platform of microcontrollers and microprocessors specially targeting the Automotive industry. S32 platform has all the kind of MCU’s and MPU’s targeting different Domains of Automotive. There are broadly five domains in automotive:  Power Train, Chassis, Body & Comfort, HMI, andTelematics. These 5 domains cover all the devices embedded in Automotive. NXP S32 Platform S32 is a series of Microcontroller which are designed and manufactures by NXP Semiconductors, specially targeting for automotive applications but not only limited to them. Then with in S32, there are further categories as S32K, S32G, S32R, S32V. Each of the category, can be used in one of the domains of automotive. Like say in powertrain one can use S32K for motor control applications and for telematics one can use S32R for radar processing, to get information about nearby objects. This is what something ADAS system is. Like when some car come nearby to a car, some sensor senses that distance (That is telematics zone) and after a certain threshold car speed automatically changes by controlling its motor RPM (that is part of powertrain). NXP S32 Platform is being designed, providing end to end solutions for Automotive Industry. Not only MCU’s and MPU’s but NXP also provides large number of tools and software to build the applications for Automotive from S32 Platform. Dedicated IDE, Debugging tools and drivers, Real-time Drivers (RTD), RTOSes(FreeRTOS, Zephyr, NXP RTOS) and Autosar complaint SDK. Almost every MCU of S32 family has support of Automotive protocols: CAN, LIN, FlexIO and etc. Ranging from ARM CortexM3(single core) to ARM Cortex A9(triple core). Having advance features to support automotive needs like : Hardware security modules(HSM) for safety and security, High Bandwidth Ethernet and USB support for connectivity and etc. We are going to start with S32K312 MCU, which is part of S32K family. It is based on ARM Cortex M7 and has dedicated HSM-H that too upto EVITA specs and also ASIB/D complaint. Making it a perfect MCU to start for building general purpose Automotive applications. Would get to know more about S32 platform along with tutorial series on S32K312 MCU.  About S32K312 MCU Processor/Core Clocks Memory of MCU Basic peripherals Advance Peripherals Connectivity peripherals Packages and Pins Power ratings and power saving modes Debug and programming interface Processor/Core S32K312 is a Microcontroller which is based on ARM Cortex M7 Processor. ARM Core is based on Armv7 and Thumb-ISA (Instruction Set Architecture). S32K312 is a single core microcontroller, i.e., only one ARM Cortex M7 processor is based. Cortex M7 present in S32K312 provides the following features/Subsystems: Cortex M7 in S32K312 can run upto 120 Mhz.  It has Integrated Digital signal Processing (DSP) subsystem, for doing complex signal processing operations.  Configurable Nested Vector Interrupt Controller (NVIC) for handling the interrupts. Single precision Floating Point Unit (FPU) for doing complex Mathematical calculation.  It has standrad ARM Debug Port that supports JTAG and SWD interfaces. It has subsystem, CoresSight Architecture for debugging and programming. Support of SWD and SWO for debugging and programming Microcontroller, in addition to that it has numerous trace units like FPB (Flash Patch and Breakpoints) ITM (instrumentation Trace Macrocell), HTM (Coresight AHB Trace Macrocell), ECT (Embedded Cross Trigger) and DWT (Debug Watchpoint and Trace). All of these subunits are part of coresight Architecture, which is subsystem in the Arm CortexM7. It is optimized for real time with Cortex M7 local memories ITCM and DTCM cacheable memories for faster execution and better efficiency. ARM Cortex M7 has AXIM, AHBP and PPB buses for its interconnects and interfaces. It follows AMBA protocol for communicating with various subsystems using these busses. Apart from ARM Cortex M7, it has dedicated ARM Cortex M0 also which is used in its Hardware Security engine (HSE) for performing cryptographic operations. Clocks S32K312 MCU has 5 clock interfaces in it. Fast External Oscillator (FXOSC): External oscillator of upto 8-40 MHZ can be connected to S32K312 MCU, at pins EXTAL and EXTAL which can be used as a source for PLL to configure system clock and also for peripherals like FlexCAN, QuadSPI and etc. 48 MHz Fast Internal RC oscillator (FIRC): This is the default system Clock. FIRC also has dividers 1,2,16 to configure its clock, 32 kHz Low Power Oscillator 32

Read More »

CONFIGURING THE OLED WITH STM32 MCU

In previous blog we covered a brief overview of how the OLED display works in microscopic level and also understood various types of OLED displays available in the market . In this blog we’ll be discussing  how to configure the SSD1306  display with the microcontroller and we’ll be  making the embedded driver as well.128×64 display is a dot matrix display , hence 128×64 =8192 total pixels . It is by turning on/off these pixels we display graphical image of any shape . It is the current provided to each pixel that varies the brightness. HARDWARE DESCRIPTION OLED Display chosen is driven by SSD1306 Driver IC although they are other ICs such as SSD1331 which can be used to drive the display . These ICs  are CMOS OLED Driver controller for dot-matrix system . OLED has 256 brightness steps .Besides 128×64 , 128×32 display resolution is also available. Specification of ssd1306 128×64 OLED Display Type: OLED (Organic Light Emitting Diode) Display Size: 128×64 pixels Display Driver: ssd1306 Display Colors: Monochrome (White), Yellow, and Blue Operating Voltage: 3.3V to 5V Interface: I2C Operating Current: ~20mA Display Structure OLED DISPLAY is mapped using GDDRAM page structure  OF SSD1306 GDDRAM or graphic display ram is a bit mapped static RAM . It holds the bit pattern to be displayed. The GDDRAM having size 128×64 is divided into 8 pages from PAGE 0 TO PAGE 7 which is used for monochrome matrix display . When data bit D0 – D7 is sent the row0 gets filled with D0 and D7 is written into the bottom row.  Display has 64 rows , 128 columns divided into 8 pages . Each page has 128 columns and 8 rows. Display 128 columns known as segments For displaying the graphical data in the first location , page address and column address both are set to 0 with the end address of page and column also being selected End of column and End of the page is 7FH and 07H respectively SSD1306 BLOCK DIAGRAM PIN ARANGEMENT SSD1306 FUNCTIONAL BLOCK DIAGRAM SSD1306 BLOCK DIAGRAM PIN ARANGEMENT SSD1306 FUNCTIONAL BLOCK DIAGRAM ADDRESSING MODE 1. PAGE ADDRESSING MODE 2.Horizontal Addressing Mode 3.Vertical Addressing Mode 1. PAGE ADDRESSING MODE In page addressing mode, after the display RAM is read/written, the column address pointer is increased automatically by 1.                                                                If the column address pointer reaches column end address, the column address pointer is reset to column start address but page address pointer not points to next page. Hence, we need to set the new page and column addresses in order to access the next page RAM content. We need to set lower two bits to ‘1’ and ‘0’ for Page Addressing Mode. In page addressing mode, the following steps are required to define the starting RAM access pointer location: Set the page start address of the target display location by command B0h to B7h. Set the lower start column address of pointer by command 00h~0Fh. Set the upper start column address of pointer by command 10h~1Fh 2.Horizontal Addressing Mode In horizontal addressing mode, after the display RAM is read/written, the column address pointer is increased automatically by 1. If the column address pointer reaches column end address, the column address pointer is reset to column start address and page address pointer is increased by 1. When both column and page address pointers reach the end address, the pointers are reset to column start address and page start address We need to set last two digits to ‘0’ and ’0’ for horizontal addressing mode. 3.Vertical Addressing Mode In vertical addressing mode, after the display RAM is read/written, the page address pointer is increased automatically by 1. If the page address pointer reaches the page end address, the page address pointer is reset to page start address and column address pointer is increased by 1. When both column and page address pointers reach the end address, the pointers are reset to column start address and page start address. We need to set last two digits to ‘0’ and ’1’ for vertical addressing mode. In normal display data RAM read or write and horizontal/vertical addressing mode, the following steps are required to define the RAM access pointer location: Set the column start and end address of the target display location by command 21h. Set the page start and end address of the target display location by command 22h. Hardware Pinout SDAThis pin is used to send data between master and slave with the acknowledgement of the master SCLThis is a clock signal that helps keeps the process in synchronization VCCA power supply of 3.3 V is required . More than 3.3V may damage the module GNDThis ground pin is connected to the ground supply ALGORITHM Select the I2C slave address and specify the operation that will be performed i.e Read 0x79 or Write 0x78. #define SSD1306_I2C_ADDR 0x78 Set the clock divide ratio and oscillator frequency . Bit 3-0 sets the clock divide ratio , Bit 7-4 sets the oscillator frequency SSD1306_WRITECOMMAND(0xD5); //–set display clock divide ratio/oscillator frequency SSD1306_WRITECOMMAND(0xF0); //–set divide ratio Set the multiplex ratio switching to any value ranging from 16-63 SSD1306_WRITECOMMAND(0xA8); //–set multiplex ratio(1 to 64) Display start line addressing in which the starting address of the display ram is determined . In our case this is set to zero and RAM row 0 is mapped to col 0 SSD1306_WRITECOMMAND(0x40); //–set start line address Set memory addressing mode using page addressing mode, horizontal addressing mode, vertical addressing mode. SSD1306_WRITECOMMAND(0x10); //00,Horizontal Addressing Mode;01,Vertical Addressing Mode;10,Page Addressing Mode (RESET);11,Invalid SSD1306_WRITECOMMAND(0xB0); //Set Page Start Address for Page Addressing Mode,0-7 Set column address using a triple byte first specifies the column setting , second column start and third column  end . Do the same for the page SSD1306_WRITECOMMAND(0x00); //—set low column address SSD1306_WRITECOMMAND(0x10); //—set high column address Set pre-charge period and VCOMH deselect level SSD1306_WRITECOMMAND(0xDB); //–set vcomh Entire display is on using A4H and A5H command SSD1306_WRITECOMMAND(0xA4); //0xa4,Output follows RAM content;0xa5,Output ignores RAM content The normal functionality of the

Read More »
Kunal Gupta
Author: Kunal Gupta

Author

Kunal Gupta

Leave a comment

Stay Updated With Us

Error: Contact form not found.

      Blog