Posted on Leave a comment

How to Blink LED on Mini STM32 v3.0 Discovery Board Using STM32CubeIDE

Step 2: Configure GPIO Pins

#define LED1_Pin GPIO_PIN_2
#define LED1_GPIO_Port GPIOA
#define LED2_Pin GPIO_PIN_3
#define LED2_GPIO_Port GPIOA

In the Project Explorer pane, expand the “Src” folder and open the “main.c” file. Scroll down to the main() function and add the following code to configure the GPIO pins:

/* Configure GPIO pins */
__HAL_RCC_GPIOA_CLK_ENABLE();      // Enable GPIOA clock
GPIO_InitTypeDef GPIO_InitStruct; // GPIO configuration structure
GPIO_InitStruct.Pin = GPIO_PIN_2; // Use pin 2 on GPIOA
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; // Push-Pull output mode
GPIO_InitStruct.Pull = GPIO_NOPULL; // No pull-up or pull-down resistors
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; // Low speed
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); // Initialize GPIOA with settings

This code configures pin 2 on GPIOA as a push-pull output pin with no pull-up or pull-down resistors and low output speed.

Blink the LED using HAL_GPIO_WritePin

Add the following code inside the while(1) loop to blink the LED:

/* Blink LED */
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_2, GPIO_PIN_SET); // Turn LED on
HAL_Delay(1000); // Wait for 1 second
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_2, GPIO_PIN_RESET); // Turn LED off
HAL_Delay(1000); // Wait for 1 second

This code turns the LED on by setting the state of pin 2 on GPIOA to high, waits for 1 second, turns the LED off by setting the state of pin 2 on GPIOA to low, and then waits for 1 second again. This creates a blinking effect.

Blink the LED using HAL_GPIO_TogglePin

/* Blink LED */
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_2); // Toggle the state of the LED
HAL_Delay(1000); // Wait for 1 second

This code toggles the state of pin 2 on GPIOA (which is connected to an LED on the Mini STM32 board) and then waits for 1 second before toggling it again. This creates a blinking effect.

HAL_GPIO_TogglePin(LED1_GPIO_Port, LED1_Pin);
HAL_Delay(1000);
HAL_GPIO_TogglePin(LED2_GPIO_Port, LED2_Pin);

Code to Blink both LED’s

HAL_GPIO_TogglePin(LED1_GPIO_Port, LED1_Pin);
HAL_Delay(1000);
HAL_GPIO_TogglePin(LED2_GPIO_Port, LED2_Pin);

The first line toggles the state of an LED connected to the LED1_GPIO_Port and LED1_Pin. The second line waits for 1000 milliseconds (1 second) using the HAL_Delay() function. The third line toggles the state of another LED connected to the LED2_GPIO_Port and LED2_Pin.

Posted on Leave a comment

Up Counting Timer using STM32L476

I have created this Upcounting timer using the RTC of STM32L476vgt. In this timer, time will keep on incrementing and it will be displayed using the onboard LCD.

How to Read RTC of STM32L476G-DISCO

Here is the code that I have used to make this.

I have used STM32 CUBE IDE for programming and debugging purposes.

  MX_RTC_Init();				// RTC initalization and configuration created using integrated cube mx
  /* USER CODE BEGIN 2 */
  BSP_LCD_GLASS_Init();
  BSP_LCD_GLASS_Clear();
  BSP_LCD_GLASS_DisplayString((uint8_t *)"HALLO");
  HAL_Delay(1000);
  BSP_LCD_GLASS_DisplayString((uint8_t *)"EXASUB");
  HAL_Delay(2000);


  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */

  RTC_TimeTypeDef readTime;	// RTC Time structure
  RTC_DateTypeDef readDate;	// RTC Date structure
  while (1)
  {
    /* USER CODE END WHILE */
    MX_USB_HOST_Process();

    /* USER CODE BEGIN 3 */

    char bufSec[2];
    char bufMin[2];
    char bufHou[2];
    HAL_RTC_GetTime(&hrtc, &readTime, RTC_FORMAT_BIN);	// function to read time from RTC shadow register
    HAL_RTC_GetDate(&hrtc, &readDate, RTC_FORMAT_BIN);	// function to read date from RTC shadow register
   itoa(readTime.Seconds,bufSec,10);
   itoa(readTime.Minutes,bufMin,10);
   itoa(readTime.Hours,bufHou,10);
   if(readTime.Seconds == 0){
	   BSP_LCD_GLASS_Clear();
   }
   if(readTime.Minutes == 0){
	   BSP_LCD_GLASS_Clear();
   }
   /*
    if(readTime.Hours == 0){
   	   BSP_LCD_GLASS_Clear();
      }
   */
   BSP_LCD_GLASS_DisplayChar((uint8_t *)&bufHou[0], POINT_OFF, DOUBLEPOINT_OFF, 0);
   BSP_LCD_GLASS_DisplayChar((uint8_t *)&bufHou[1], POINT_OFF, DOUBLEPOINT_ON, 1);
   BSP_LCD_GLASS_DisplayChar((uint8_t *)&bufMin[0], POINT_OFF, DOUBLEPOINT_OFF, 2);
   BSP_LCD_GLASS_DisplayChar((uint8_t *)&bufMin[1], POINT_OFF, DOUBLEPOINT_ON, 3);
   BSP_LCD_GLASS_DisplayChar((uint8_t *)&bufSec[0], POINT_OFF, DOUBLEPOINT_OFF, 4);
   BSP_LCD_GLASS_DisplayChar((uint8_t *)&bufSec[1], POINT_OFF, DOUBLEPOINT_OFF, 5);

    HAL_Delay(1000);									// HAL Delay of 1000 millisecond
  }
  /* USER CODE END 3 */
}

Posted on 1 Comment

How to Read RTC of STM32L476G-DISCO

Create a Project in STM32 CUBE IDE for the STM32L476G-DISCO board.

Select LSE as Clock Source for RTC

The default option is LSI which uses an internal RC circuit.

LSE is the external 32KHz crystal that is provided on the board for the RTC.

Activate the Clock source and Calendar

This will enable the RTC clock source and will also enable the calendar for date and timekeeping.

If you want to set some default time and date you can set it in the RTC configuration menu. You can always change them later through your code.

After doing the above-said steps you can now generate code. This will give you an initialization code.

To read the value from the RTC registers. You need to remember the following thing.

From Section 38.3.8 of RM0351 Rev 1 Reference Manual

It says that reading the sub-second register or Time register alone will lock the value. It will remain locked until the Date register is read.

To prevent the lockup from happening. It is suggested to read time and date simultaneously one after the another.

Here I am writing working code

 MX_RTC_Init();				// RTC initalization and configuration created using integrated cube mx 
  /* USER CODE BEGIN 2 */

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */

  RTC_TimeTypeDef readTime;	// RTC Time structure 
  RTC_DateTypeDef readDate;	// RTC Date structure
  while (1)
  {
    /* USER CODE END WHILE */
    

    /* USER CODE BEGIN 3 */
/*  function to read time from RTC shadow register */
    HAL_RTC_GetTime(&hrtc, &readTime, RTC_FORMAT_BIN);

/* function to read date from RTC shadow register	*/
    HAL_RTC_GetDate(&hrtc, &readDate, RTC_FORMAT_BIN);

    printf("Minute: %d\t",readTime.Minutes);
    printf("Seconds: %d\n",readTime.Seconds);

    HAL_Delay(1000);	// HAL Delay of 1000 millisecond
  }
  /* USER CODE END 3 */
}

I am using print statements for debugging using stm32cube ide.
How to use printf using serial wire debug on STM32L476 Discovery

Posted on Leave a comment

How to add a new .h header file in STM32 Cube IDE

  1. Click on [arrow] besides Core in your project explorer
  2. Right Click on “Inc” folder and point your mouse at the “NEW” menu item.
  3. Select “header file” option
  4. A new Header file dialog box will appear
  5. Fill in the name of the header file in “Header file” with a dot h extension
  6. Click on finish
  7. Now open your main.c file and add your custom header file after USER CODE BEGIN Incudes
Posted on Leave a comment

How to add CMSIS DSP Library to STM32 Cube IDE Project for stm32l476vg

To add CMSIS DSP library in you stm32cube project.

You can follow the steps written on ST’s website

Configuring DSP libraries on STM32CubeIDE

I have also made a video.

In this video. I have shown steps to add the arm_math.h header file. You need to configure the stm32 cube ide.

Posted on Leave a comment

How to use LCD Driver of STM32L476VGT on STM32L476 Discovery

To use the internal LCD driver of stm32l476; the best way is to use the LCD driver provided in the BSP. It is located inside the STM32Cube_FW_L4_V1.16.0 repository.

  • Go to the
    ..\STM32Cube\Repository\STM32Cube_FW_L4_V1.16.0\Drivers\BSP\STM32L476G-Discovery
  • Copy the stm32l476g_discovery.c, stm32l476g_discovery.h, stm32l476g_discovery_glass_lcd.c and stm32l476g_discovery_glass_lcd.h
  • Paste the files in your project.
  • Include the header file in your project.
    #include “stm32l476g_discovery_glass_lcd.h”

After this you need to

  1. Initialize the LCD segment
    BSP_LCD_GLASS_Init();
  2. Clear the LCD RAM
    BSP_LCD_GLASS_Clear();
  3. Write Data in LCD RAM
    BSP_LCD_GLASS_DisplayChar((uint8_t *)”A”, POINT_ON, DOUBLEPOINT_OFF, 1);
Posted on Leave a comment

How to use printf using serial wire debug on STM32L476 Discovery

We all use printf statements for debugging purposes at some point in time.

To use print statement. You need to do fuse one solder bridge

Solder Bridge information from the STM32L476 Discovery Kit User Manual UM1789

Fuse the solder bridge number SB14.

Always be careful since the solder bridges are very small.

After you have done fused the solder bridge. You can configure your project in STM32 Cube IDE.

Watch the video below for complete instructions.

A few things must be taken care of before you start debugging:

  • The Core Clock in your debug configuration must be equal to the system core clock.
  • Press the record button in SWV ITM Data Console before you start debugging.
Posted on 1 Comment

How to start a project for STM32L476 Discovery in STM32 Cube IDE

Download the STM32 Cube IDE from https://www.st.com/en/development-tools/stm32cubeide.html

Watch this video tutorial.

Write a program using STM32L476G-DISCO

Step1: Install STM32 Cube IDE

Step2: Create a New Workspace
A workspace is a directory which will contain all your code and library related to your project.

Step 3: click on “Start new STM32 Project” button

Step 4: Select your board “stm32l476G-DISCO”

Step 5: Give your project a name
Do not change any options

Step 6: Select “YES” when prompted for “Initialze all peripheral with their default mode”
It is very important that you select YES other wise it will remove all the associated peripherals and you have to manually add the desired peripherals one by one; which is very difficult for beginners.

After these steps your project is created and it will open a STM32 CUBEMX inside your IDE.

Please watch the video. As there are a lots of steps and instruction which are difficult to explain by writing alone. Video will show you a step by step procedure and give you a basic explainition.

You can now change the option or you can proceed forward and click on generate code. This will create all the required code changes.

Now you can open you “main.c” file inside your IDE and write code.

Posted on Leave a comment

IDE Supporting STM32L476-Discovery

An IDE combines a range of different tools which are essential for the development of software. Different IDE from different vendors is available.

Here are the IDE that I have used.

  1. STM CUBE IDE
    website: https://www.st.com/en/development-tools/stm32cubeide.html
    It is distribuited freely by ST. You can use this and it has a good device support and newer devices are quickly added to it. ST uses the eclipse IDE as the base and then they have customized it heavily for the microcontroller development. It also comes with their ST cube MX integrated in it. Which makes development quicker. From the information i have gathered till now it uses gcc as their compiler. Support is available at the forum hosted by ST on thier website.
    They offer support for freeRTOS out of the box.
  2. Keil MDK
    website: https://www2.keil.com/mdk5
    They have a wide support for ARM based devices. You can download the IDE from thier website. It has two version one is the evaluation which limits your code size to 32 Kilo Bytes; but it is good for begineers. The second is the paid option. In both the version all the tools works fine. It is just the code size that puts a limit.
    Keil also host a huge knowledge base on their website. It also has some sample which can be usefull in getting started.
  3. IAR Embedded Workbench for Arm (EWARM)
    website : https://www.iar.com/products/architectures/arm/iar-embedded-workbench-for-arm/
    They are also used by the electronic industry. They also has their own compiler which is written for the speed. They have a standard layout and option. They are user friendly. From the information gathered by me they have tested functions only. So it is very standardised set of tool. They also have put a limit on their evualtion IDE of 32 Kilo Byte Code size.