Getting Started with STM32 Board

Thread Starter

champ1

Joined Jun 4, 2018
136
Here is a condensed version of the above code:
Thank you millions time MrChips, I was facing a little bit of trouble initially. I was getting error initializing ST-link device. It solved by reinstalled Atollic TrueSTUDIO

I learned to program STM32F4_Discovery Board. I tested both programs on the board. Both program's are working fine. I connected external led with current limit resistor to see it work's or not, There is no problem it's also working.

I'll try to write a few simple programs and check with board.
 

MrChips

Joined Oct 2, 2009
34,880
Thank you millions time MrChips, I was facing a little bit of trouble initially. I was getting error initializing ST-link device. It solved by reinstalled Atollic TrueSTUDIO

I learned to program STM32F4_Discovery Board. I tested both programs on the board. Both program's are working fine. I connected external led with current limit resistor to see it work's or not, There is no problem it's also working.

I'll try to write a few simple programs and check with board.
Glad to hear you got it working.
 

Thread Starter

champ1

Joined Jun 4, 2018
136
Glad to hear you got it working.
Hello MrChips

I am little bit struggling with pin configuration. We were configuring pin as output for Led.

I want to configure pin as input for switch button
Code:
GPIO_Pin : Select the pin
GPIO_Mode: Select the mode
GPIO_OType :Select the type of output
GPIO_PuPd:-Select resistor
GPIO_Speed:- Select GPIO pin speed
Every setting have options

1. GPIO_Pin :
My selection :
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0

2. GPIO_Mode:-Type of mode of operation
Code:
GPIO_Mode_IN: Set pin as input
GPIO_Mode_OUT: Set pin as output
GPIO_Mode_AF: Set pin as alternating function ( like when used as SPI, USART, etc)
GPIO_Mode_AN: Set pin as analog (ADC or DAC)
My selection : GPIO_InitStructure.GPIO_Mode = GPIO_Mode_INPUT;

3. GPIO_OType:- Type of Mode of output pins
Code:
GPIO_OType_PP: Output type is push-pull
GPIO_OType_OD: Output type is open drain
I don't understand which option would be good ?

4. GPIO_PuPd:- Select type of Pull-up/Pull-down
Code:
GPIO_PuPd_UP: Enable pull up resistor
GPIO_PuPd_DOWN: Enable pull down resistor
GPIO_PuPd_NOPULL: Disable pull resistor
I don't understand which option would be good ?

5. GPIO_Speed:- Select GPIO speed
Code:
GPIO_Speed_100MHz
GPIO_Speed_50MHz
GPIO_Speed_25MHz
GPIO_Speed_2MHz
My selection : GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz
 

MrChips

Joined Oct 2, 2009
34,880
Usually, one would connect a push-button or switch across the input pin and GND.
Use the MCU internal pull-up then you do not need an external pull-up resistor.
Since this is an input function, you don't need to specify the OTYPE.
Code:
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
 

Thread Starter

champ1

Joined Jun 4, 2018
136
Usually, one would connect a push-button or switch across the input pin and GND.
Use the MCU internal pull-up then you do not need an external pull-up resistor.
Since this is an input function, you don't need to specify the OTYPE.
I have to configure the input and output pin in program.

How to configure input and output in one one program

Can we write like this

C:
void Init(void)
{
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);

  /* Configure pin_0 as input for switch */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;

  /* Configure pin_5 as output for Led */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;

  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_Init(GPIOD, &GPIO_InitStructure);

}
 

MrChips

Joined Oct 2, 2009
34,880
No. You do them separately.
C:
void Init(void)
{
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);

  /* Configure pin_0 as input for switch */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_Init(GPIOD, &GPIO_InitStructure);

  /* Configure pin_5 as output for Led */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_Init(GPIOD, &GPIO_InitStructure);
}
BTW, if you use CubeMX it will generate the code for you. You can learn a lot by looking at the code that CubeMX generates for you.
 

Thread Starter

champ1

Joined Jun 4, 2018
136
No. You do them separately.

BTW, if you use CubeMX it will generate the code for you. You can learn a lot by looking at the code that CubeMX generates for you.
Might be I am following wrong link https://www.badprog.com/electronics...ch-on-the-led6-on-the-stm32f3-discovery-board

But I tried to make complete program.

Again stuck to set gpio high / low

C:
#include "stm32f4_discovery.h"

GPIO_InitTypeDef  GPIO_InitStructure;

void Init(void)
{
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);

   /* Configure pin_0 as input for switch */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_Init(GPIOD, &GPIO_InitStructure);

   /* Configure pin_5 as output for Led */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_Init(GPIOD, &GPIO_InitStructure);
}

void main(void)
{
  Init();

   while (1) // infinite loop

     {                   
        if (GPIO_ReadPin(GPIOD, GPIO_PIN_0))  // checks if switch is set
           {               
               GPIO_WritePin(GPIOD, GPIO_PIN_5, GPIO_PIN_SET);      // switch on LED
           }

        else
           {
               GPIO_WritePin(GPIOD, GPIO_PIN_5, GPIO_PIN_RESET);    // switch off LED
           }
     }
}
 
Last edited:

Thread Starter

champ1

Joined Jun 4, 2018
136
Why are you using HAL_ library function?

Which pin is connected to the LED, PD5 or PD15?
My mistake, I am using PD6

I want to convert this logic into program

Code:
if ( switch == closed )
{
   switch on LED
}
else
{
   switch off LED
}
 

MrChips

Joined Oct 2, 2009
34,880
Note that this is set to output on PD6.
How is the switch connected?
C:
#include "stm32f4_discovery.h"

void Init(void)
{
  GPIO_InitTypeDef  GPIO_InitStructure;
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);

   /* Configure pin_0 as input for switch */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_Init(GPIOD, &GPIO_InitStructure);

   /* Configure pin_6 as output for LED */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_Init(GPIOD, &GPIO_InitStructure);
}

void main(void)
{
  Init();

   while (1) // infinite loop
     {                  
        if (GPIO_ReadPin(GPIOD, GPIO_PIN_0))    // checks if switch is set
           {              
               GPIO_WritePin(GPIOD, GPIO_PIN_6, GPIO_PIN_RESET);      // switch off LED
           }
        else
           {
               GPIO_WritePin(GPIOD, GPIO_PIN_6, GPIO_PIN_SET);    // switch onLED
           }
     }
}
 

Thread Starter

champ1

Joined Jun 4, 2018
136
Note that this is set to output on PD6.
How is the switch connected?
C:
  /* Configure pin_0 as input for switch */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_Init(GPIOD, &GPIO_InitStructure);

}
I think you have used an internal pull resistor in the program so I can connect one pin of push button with Pin_0 and other connects to ground.

If you did not set up the internal pull resitor then I would have to connect the switch to the external pullup resistor
 

Thread Starter

champ1

Joined Jun 4, 2018
136
Note that this is set to output on PD6.
How is the switch connected?
C:
#include "stm32f4_discovery.h"

void Init(void)
{
  GPIO_InitTypeDef  GPIO_InitStructure;
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);

   /* Configure pin_0 as input for switch */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_Init(GPIOD, &GPIO_InitStructure);

   /* Configure pin_6 as output for LED */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_Init(GPIOD, &GPIO_InitStructure);
}

void main(void)
{
  Init();

   while (1) // infinite loop
     {                
        if (GPIO_ReadPin(GPIOD, GPIO_PIN_0))    // checks if switch is set
           {            
               GPIO_WritePin(GPIOD, GPIO_PIN_6, GPIO_PIN_RESET);      // switch off LED
           }
        else
           {
               GPIO_WritePin(GPIOD, GPIO_PIN_6, GPIO_PIN_SET);    // switch onLED
           }
     }
}
I compiled this program, it gives four error's
I am trying to remove these errors
Code:
Description    Resource    Path    Location    Type
'GPIO_PIN_0' undeclared (first use in this function)    main.c    /STM32F4_Discovery_Demo/src    line 29    C/C++ Problem
'GPIO_PIN_6' undeclared (first use in this function)    main.c    /STM32F4_Discovery_Demo/src    line 31    C/C++ Problem
'GPIO_PIN_RESET' undeclared (first use in this function)    main.c    /STM32F4_Discovery_Demo/src    line 31    C/C++ Problem
'GPIO_PIN_SET' undeclared (first use in this function)    main.c    /STM32F4_Discovery_Demo/src    line 35    C/C++ Problem
'return' with no value, in function returning non-void    STM32F4-Discovery_callback.c    /STM32F4_Discovery_IO_Toggle/src    line 42    C/C++ Problem
implicit declaration of function 'GPIO_ReadPin' [-Wimplicit-function-declaration]    main.c    /STM32F4_Discovery_Demo/src    line 29    C/C++ Problem
implicit declaration of function 'GPIO_WritePin' [-Wimplicit-function-declaration]    main.c    /STM32F4_Discovery_Demo/src    line 31    C/C++ Problem
pointer targets in passing argument 1 of 'USBD_GetString' differ in signedness [-Wpointer-sign]    usbd_desc.c    /STM32F4_Discovery_Demo/src    line 61    C/C++ Problem
pointer targets in passing argument 1 of 'USBD_GetString' differ in signedness [-Wpointer-sign]    usbd_desc.c    /STM32F4_Discovery_Demo/src    line 63    C/C++ Problem
pointer targets in passing argument 1 of 'USBD_GetString' differ in signedness [-Wpointer-sign]    usbd_desc.c    /STM32F4_Discovery_Demo/src    line 64    C/C++ Problem
pointer targets in passing argument 1 of 'USBD_GetString' differ in signedness [-Wpointer-sign]    usbd_desc.c    /STM32F4_Discovery_Demo/src    line 66    C/C++ Problem
pointer targets in passing argument 1 of 'USBD_GetString' differ in signedness [-Wpointer-sign]    usbd_desc.c    /STM32F4_Discovery_Demo/src    line 67    C/C++ Problem
pointer targets in passing argument 1 of 'USBD_GetString' differ in signedness [-Wpointer-sign]    usbd_desc.c    /STM32F4_Discovery_Demo/src    line 69    C/C++ Problem
pointer targets in passing argument 1 of 'USBD_GetString' differ in signedness [-Wpointer-sign]    usbd_desc.c    /STM32F4_Discovery_Demo/src    line 70    C/C++ Problem
pointer targets in passing argument 1 of 'USBD_GetString' differ in signedness [-Wpointer-sign]    usbd_desc.c    /STM32F4_Discovery_Demo/src    line 72    C/C++ Problem
pointer targets in passing argument 1 of 'USBD_GetString' differ in signedness [-Wpointer-sign]    usbd_desc.c    /STM32F4_Discovery_Demo/src    line 73    C/C++ Problem
return type of 'main' is not 'int' [-Wmain]    main.c    /STM32F4_Discovery_Demo/src    line 23    C/C++ Problem
return type of 'main' is not 'int' [-Wmain]    main.c    /STM32F4_Discovery_IO_Toggle/src    line 27    C/C++ Problem
this 'while' clause does not guard... [-Wmisleading-indentation]    system_stm32f4xx.c    /STM32F4_Discovery_IO_Toggle/src    line 405    C/C++ Problem
this 'while' clause does not guard... [-Wmisleading-indentation]    system_stm32f4xx.c    /project1/src    line 399    C/C++ Problem
variable 'tmp_psize' set but not used [-Wunused-but-set-variable]    stm32f4xx_flash.c    /STM32F4_Discovery_Demo/Libraries/STM32F4xx_StdPeriph_Driver/src    line 434    C/C++ Problem
...this statement, but the latter is misleadingly indented as if it is guarded by the 'while'    system_stm32f4xx.c    /STM32F4_Discovery_IO_Toggle/src    line 406    C/C++ Problem
...this statement, but the latter is misleadingly indented as if it is guarded by the 'while'    system_stm32f4xx.c    /project1/src    line 400    C/C++ Problem
declared here    STM32F4-Discovery_callback.c    /STM32F4_Discovery_IO_Toggle/src    line 39    C/C++ Problem
each undeclared identifier is reported only once for each function it appears in    main.c    /STM32F4_Discovery_Demo/src    line 29    C/C++ Problem
expected 'uint8_t * {aka unsigned char *}' but argument is of type 'char *'    usbd_req.h    /STM32F4_Discovery_Demo/Libraries/STM32_USB_Device_Library/Core/inc    line 92    C/C++ Problem
in expansion of macro 'USBD_CONFIGURATION_FS_STRING'    usbd_desc.c    /STM32F4_Discovery_Demo/src    line 278    C/C++ Problem
in expansion of macro 'USBD_CONFIGURATION_HS_STRING'    usbd_desc.c    /STM32F4_Discovery_Demo/src    line 274    C/C++ Problem
in expansion of macro 'USBD_INTERFACE_FS_STRING'    usbd_desc.c    /STM32F4_Discovery_Demo/src    line 299    C/C++ Problem
in expansion of macro 'USBD_INTERFACE_HS_STRING'    usbd_desc.c    /STM32F4_Discovery_Demo/src    line 295    C/C++ Problem
in expansion of macro 'USBD_MANUFACTURER_STRING'    usbd_desc.c    /STM32F4_Discovery_Demo/src    line 239    C/C++ Problem
in expansion of macro 'USBD_PRODUCT_FS_STRING'    usbd_desc.c    /STM32F4_Discovery_Demo/src    line 225    C/C++ Problem
in expansion of macro 'USBD_PRODUCT_HS_STRING'    usbd_desc.c    /STM32F4_Discovery_Demo/src    line 221    C/C++ Problem
in expansion of macro 'USBD_SERIALNUMBER_FS_STRING'    usbd_desc.c    /STM32F4_Discovery_Demo/src    line 258    C/C++ Problem
in expansion of macro 'USBD_SERIALNUMBER_HS_STRING'    usbd_desc.c    /STM32F4_Discovery_Demo/src    line 254    C/C++ Problem
 

MrChips

Joined Oct 2, 2009
34,880
Did the previous program to toggle an I/O port work?
Use that code as a starting template.
C:
#include "stm32f4_discovery.h"

#define LED_PORT GPIOD
#define LED GPIO_Pin_6
#define SX_PORT GPIOD
#define SX GPIO_Pin_0

void Init(void)
{
  GPIO_InitTypeDef  GPIO_InitStructure;
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);

   /* Configure pin_0 as input for switch */
  GPIO_InitStructure.GPIO_Pin = SX;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_Init(SX_PORT, &GPIO_InitStructure);

   /* Configure pin_6 as output for LED */
  GPIO_InitStructure.GPIO_Pin = LED;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_Init(LED_PORT, &GPIO_InitStructure);
}

void main(void)
{
  Init();

   while (1) // infinite loop
     {                
        if (GPIO_ReadInputDataBit(SX_PORT, SX))    // checks if switch is set
           {            
               GPIO_ResetBits(LED_PORT, LED);      // switch off LED
           }
        else
           {
               GPIO_SetBits(LED_PORT, LED);    // switch on LED
           }
     }
}
 

MrChips

Joined Oct 2, 2009
34,880
Look at the errors. The error messages are saying that certain names are not declared. This means that your project is not set up correctly and you have not loaded the required libraries into the project.

Start off with a working example. This will have the required libraries.
I have tested the previous I/O toggle program on Atollic TrueSTUDIO. Not much has changed in the latest code I have shown. I can test this for you later today.
 

MrChips

Joined Oct 2, 2009
34,880
The program compiled with no errors. I don't have a DISCO board with me now but I can test the code next week.
Open the project tree and check under Libraries that you have the STM32F4xx_StdPeriph_Driver installed.
 

Thread Starter

champ1

Joined Jun 4, 2018
136
The program compiled with no errors. I don't have a DISCO board with me now but I can test the code next week..
In my case it was giving error, I have solved it.
When i use int main void compiler doesn't give error

I have compiled this program with no errors but when I press button led doesn't turn on

I have connected led to pin PD6 and Button to PD0

Code:
#include "stm32f4_discovery.h"

#define LED_PORT GPIOD
#define LED GPIO_Pin_6
#define SX_PORT GPIOD
#define SX GPIO_Pin_0

void Init(void)
{
  GPIO_InitTypeDef  GPIO_InitStructure;
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);

   /* Configure pin_0 as input for switch */
  GPIO_InitStructure.GPIO_Pin = SX;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_Init(SX_PORT, &GPIO_InitStructure);

   /* Configure pin_6 as output for LED */
  GPIO_InitStructure.GPIO_Pin = LED;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_Init(LED_PORT, &GPIO_InitStructure);
}

int main(void)
{
  Init();

   while (1) // infinite loop
     {
        if (GPIO_ReadInputDataBit(SX_PORT, SX))    // checks if switch is set
           {
               GPIO_ResetBits(LED_PORT, LED);      // switch off LED
           }
        else
           {
               GPIO_SetBits(LED_PORT, LED);    // switch on LED
           }
     }
   return 0;
}
Did you debug the program ?
 
Top