2022-01-29 09:37 PM
I am using a NUCLEO-L476RG development board, I am learning to write GPIO drivers for STM32 family I am implementing a simple logic in which I need to turn on an LED when a push button is pressed.
I have a strange issue: The Bread board LED turns ON when the line temp=10 line is commented, it doesn't turn ON when the line temp=10 is uncommented.
What could be the issue?
The issue doesn't solve after changing the location of the line
The connection to the bread board looks like the image below , I have not configured any clock source in my code, meaning it would take the default clock(MSI at 4MHz) for its operations. I have powered it using the Mini USB
#include <stdint.h>
#include "stm32l476xx.h"
#include "stm32l476xx_gpoi_driver.h"
#if !defined(__SOFT_FP__) && defined(__ARM_FP)
#warning "FPU is not initialized, but the project is compiling for an FPU. Please initialize the FPU before use."
#endif
#define delay() {;}
//#define delay() for(uint32_t i=0; i<=50000; i++);
int main(void)
{
GPIO_Handle_t NucleoUserLED,NucleoUserPB,BreadBoardLED,BreadBoardPB;
volatile uint8_t inputVal,BBinpVal;
uint32_t temp;
//User green led in the nucleo board connected to PA5
NucleoUserLED.pGPIO = GPIOA;
NucleoUserLED.GPIO_Pin_Cfg.GPIO_PinNumber = GPIO_PIN_5;
NucleoUserLED.GPIO_Pin_Cfg.GPIO_PinMode = GPIO_MODE_OP;
NucleoUserLED.GPIO_Pin_Cfg.GPIO_PinPuPdControl = GPIO_IP_NO_PUPD;
NucleoUserLED.GPIO_Pin_Cfg.GPIO_PinOpType = GPIO_OP_TYPE_PP;
//User blue button in the nucleo connected to PC13
NucleoUserPB.pGPIO = GPIOC;
NucleoUserPB.GPIO_Pin_Cfg.GPIO_PinNumber = GPIO_PIN_13;
NucleoUserPB.GPIO_Pin_Cfg.GPIO_PinMode = GPIO_MODE_IP;
NucleoUserPB.GPIO_Pin_Cfg.GPIO_PinPuPdControl = GPIO_IP_NO_PUPD;
//User led in the bread board connected to PC8
BreadBoardLED.pGPIO = GPIOC;
BreadBoardLED.GPIO_Pin_Cfg.GPIO_PinNumber = GPIO_PIN_8;
BreadBoardLED.GPIO_Pin_Cfg.GPIO_PinMode = GPIO_MODE_OP;
BreadBoardLED.GPIO_Pin_Cfg.GPIO_PinPuPdControl = GPIO_IP_NO_PUPD;
BreadBoardLED.GPIO_Pin_Cfg.GPIO_PinOpType = GPIO_OP_TYPE_PP;
//User DPDT connected in the breadboard connected to PC6
BreadBoardPB.pGPIO = GPIOC;
BreadBoardPB.GPIO_Pin_Cfg.GPIO_PinNumber = GPIO_PIN_6;
BreadBoardPB.GPIO_Pin_Cfg.GPIO_PinMode = GPIO_MODE_IP;
BreadBoardPB.GPIO_Pin_Cfg.GPIO_PinPuPdControl = GPIO_IP_PU;
GPIO_PeriClkCtrl(GPIOA, ENABLE);
GPIO_PeriClkCtrl(GPIOC, ENABLE);
GPIO_Init(&NucleoUserLED);
GPIO_Init(&NucleoUserPB);
GPIO_Init(&BreadBoardLED);
GPIO_Init(&BreadBoardPB);
while(1)
{
/*****************************************************************
* Controlling the IO present in the nucleo board *
*****************************************************************/
inputVal = GPIO_ReadInputPin(NucleoUserPB.pGPIO, NucleoUserPB.GPIO_Pin_Cfg.GPIO_PinNumber);
BBinpVal = GPIO_ReadInputPin(BreadBoardPB.pGPIO, BreadBoardPB.GPIO_Pin_Cfg.GPIO_PinNumber);
if(inputVal == 0)
{
GPIO_ToggleOutputPin(NucleoUserLED.pGPIO, NucleoUserLED.GPIO_Pin_Cfg.GPIO_PinNumber);
}
/*****************************************************************
* Controlling the IO present in the bread board *
*****************************************************************/
temp = 10;
if (BBinpVal == 0 )
{
GPIO_WriteOutputPin(BreadBoardLED.pGPIO, BreadBoardLED.GPIO_Pin_Cfg.GPIO_PinNumber, 1);
}
else
{
GPIO_WriteOutputPin(BreadBoardLED.pGPIO, BreadBoardLED.GPIO_Pin_Cfg.GPIO_PinNumber, 0);
}
}
return 0;
}
Solved! Go to Solution.
2022-01-30 01:36 AM
Do you have a debugger?
Does the Nucleo LED keep toggling?
What happens if you make temp global, i.e. define it outside main()?
JW
2022-01-30 01:36 AM
Do you have a debugger?
Does the Nucleo LED keep toggling?
What happens if you make temp global, i.e. define it outside main()?
JW
2022-01-30 03:05 AM
Hi @Community member
Declaring the variable as Global variable solved my issue..
Can you tell me what difference it makes?
2022-01-30 03:21 AM
> Declaring the variable as Global variable solved my issue
Hmm. It just brings more questions.
Local variables are placed on stack, usually on top of the memory as stack grows downwards; whereas globals are placed on bottom of the memory.
It may be that the stack is somehow corrupted, but that does not sound much pleasible.
But it also may be that "fixing" the problem is simply consequence of changing the order how variables are declared.
GPIO_Handle_t, GPIO_WriteOutputPin() etc. - are these from some "known good" library such as ST's SPL, or did you write them yourself?
Do you compile it as C or C++?
JW
2022-01-30 03:40 AM
GPIO_Handle_t, GPIO_WriteOutputPin() etc. - are these from some "known good" library such as ST's SPL, or did you write them yourself?
I am written a simple driver myself by enrolling myself in a course.
I have only implemented the API for the following:
I am using STMCubeIDE and compiling as C
2022-01-30 03:50 AM
> I am written a simple driver myself
Maybe there's some error in it. You can post it, if you want to discuss it further.
JW
2022-01-30 04:37 AM
Hi @Community member
You can find it here in this link
https://drive.google.com/drive/folders/1SdOgrAFT2uZsfo47o8QLG_Jewg9C2S4-?usp=sharing
2022-01-30 05:43 AM
I don't see anything which would explain the problem, sorry.
You may want to restore the problematic code and in debugger try to find out, where does the code go.
JW
2022-01-30 06:23 AM
Hi
Thanks for the support,
The issue was I had to reset the unused configuration register such as the OTYPE etc to their default values for the GPIO configured as input.
Since it was not set the driver had set wrong values in the OTYPE register
The issue is solved now..