This is probably a dumb question but I need some help : I'm trying to simply blink an LED on my STM32L073 board. The LED is connected to the PB6 pin here's my code :
#include <zephyr.h>
#include <device.h>
#include <devicetree.h>
#include <drivers/gpio.h>
void main(void)
{
const struct device *dev;
dev = device_get_binding("GPIOB");
gpio_pin_configure(dev,6,GPIO_OUTPUT);
while(1){
gpio_pin_set(dev,6,1);
k_msleep(1000);
gpio_pin_set(dev,6,0);
k_msleep(1000);
}
}
I'm using platformio with Zephyr RTOS framework
In prj.conf I do have :
CONFIG_GPIO=y
In zephyr.dts, for gpiob the label is well GPIOB so I don't understand what is wrong here, so if anyone can help me it would be great :)
Related
experts.
I am new to STM32.
So I downloaded some sample code from github.
https://github.com/yohanes-erwin/stm32f103-keil
And I download the simple led blinking code of it to the STM32F103C6 developing board but it doesn't work at all.
The original program embedded on the chip had been erased successfully ( originally the led was always on and LCD screen shows some text, but now after I download the code, the led is off and screen off. So I think the downloading is successful.) but the code does not work.
When I download the original code to the chip, it works again. So I think the chip isn't broken.
I think it's because of compatibility of sample code and my chip. The sample code was written for STM32F103C8. But it was a very simple code. What is the reason?
Here is the code.
#include "stm32f10x.h"
#include "stm32f10x_rcc.h"
#include "stm32f10x_gpio.h"
void delay(unsigned int nCount);
GPIO_InitTypeDef GPIO_InitStruct;
int main (void)
{
// Enable clock for GPIOA
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
// Configure PA4 as push-pull output
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, &GPIO_InitStruct);
while (1)
{
/* Toggle LED on PA0 */
// Reset bit will turn on LED (because the logic is interved)
GPIO_ResetBits(GPIOA, GPIO_Pin_4);
delay(1000);
// Set bit will turn off LED (because the logic is interved)
GPIO_SetBits(GPIOA, GPIO_Pin_4);
delay(1000);
}
}
// Delay function
void delay(unsigned int nCount)
{
unsigned int i, j;
for (i = 0; i < nCount; i++)
for (j = 0; j < 0x2AFF; j++);
}
My code does not set the GPIOs as inputs on my Nucleo-G071RB. The MODER register ist completly set (0xffffffff) and the GPIOs work as outputs.
What did I wrong?
Code:
#include <libopencm3/stm32/gpio.h>
void setupGpio(void);
void setupGpio(void) {
// set input
gpio_mode_setup(GPIOB, GPIO_MODE_INPUT, GPIO_PUPD_PULLDOWN, GPIO_ALL);
}
int main(void){
setupGpio();
while (1)
{
// Loop with pin read
}
}
You need to enable peripheral clock first. Not only GPIO but almost all peripherals need this.
Modify your function as below:
void setupGpio(void) {
rcc_periph_clock_enable(RCC_GPIOB); // Enable GPIOB clock
gpio_mode_setup(GPIOB, GPIO_MODE_INPUT, GPIO_PUPD_PULLDOWN, GPIO_ALL);
}
So, I am trying to toggle a LED based on an interrupt from a button.
Ideally the when the button is pressed the LED should toggle i.e. switch on if its off and vice versa. But when I execute this code it toggles and returns to its original state.
Expected Result:
LED OFF » Button pressed » LED ON
Practical Result:
LED OFF » Button pressed » LED ON » LED OFF
I have added a delay for debouncing so bouncing is out of the picture. Also the GPIO's ODR is set in the ISR when the button is pressed so how is it getting cleared while exiting the ISR?
I would really appreciate your help! Thank you.
#include <libopencm3/stm32/rcc.h>
#include <libopencm3/stm32/gpio.h>
#include <libopencm3/stm32/exti.h>
#include <libopencm3/cm3/nvic.h>
#define LEDPIN (GPIO13)
static void exti_setup(void)
{
/* Enable GPIOA and AFIO clock. */
rcc_periph_clock_enable(RCC_GPIOB);
rcc_periph_clock_enable(RCC_AFIO);
/* Enable EXTI0 interrupt. */
nvic_enable_irq(NVIC_EXTI15_10_IRQ);
/* Set GPIO12 (in GPIO port B) to input */
gpio_set_mode(GPIOB, GPIO_MODE_INPUT,GPIO_CNF_INPUT_FLOAT, GPIO12);
/* Configure the EXTI subsystem. */
exti_select_source(EXTI12,GPIOB);
exti_set_trigger(EXTI12, EXTI_TRIGGER_BOTH);
exti_enable_request(EXTI12);
}
static void gpio_setup(void)
{
/* Enable clock for GPIO port C */
rcc_periph_clock_enable(RCC_GPIOC);
/* Set LEDPIN (in GPIO port C) as opendrain output */
gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, LEDPIN);
}
void delay(){
int i;
for (i = 0; i < 1000000; i++)
{
__asm__("nop");
}
}
void handler(){
delay();
gpio_toggle(GPIOC, GPIO13);
}
int main(void)
{
gpio_setup();
exti_setup();
while (1) {
__asm__("nop");
}
return 0;
}
void exti15_10_isr()
{
exti_reset_request(EXTI12);
handler();
}
Not open drain but push-pull
Butttons should not use EXTI as it makes debouncing more complicated, often floods the uC with interrupts, Use timer interrupt instead to read the key and debounce.
As #dev_eng rightly pointed out the issue was the interrupt being configured as both RISING/FALLING edge.
Configuring it with single EDGE that is either RISING or FALLING solved my problem.
I developing RC switch for my RC glider.
I have microkontroller Microchip PIC10F202 and I have problem with ON/OFF LED Lights.
I want triggering input PPM signal from RC receiver, I setted GP2 as input (by T0CKI and count for every rise edge - from low to high), but LED is still ON, not reacting on input signal from RC receiver.
I post my source code in C language, I can not ASM language.
// this is header file with some macros and function prototypes
#ifndef XC_HEADER_TEMPLATE_H
#define XC_HEADER_TEMPLATE_H
#include <xc.h>
#define _XTAL_FREQ 4000000
#define KONST_ON 50
#define KONST_OFF 1000
#define STROBO_LED GP0
#define NAVI_LED GP1
#define RC_SIGNAL GP2
#define STAV_ON 1
#define STAV_OFF 0
#ifdef __cplusplus
extern "C" {
#endif
#ifdef __cplusplus
}
#endif
void setup ();
void flashing ();
#endif
// In this function is setup for microkontroller
#include <xc.h>
#include "prototypy.h"
void setup ()
{
OSCCAL = 0b0111111;
//STATUS = 0b00111000;
OPTION = 0b11100111;
TRISGPIO = 0b0000;
};
// This is function for LED flashing
#include <xc.h>
#include "prototypy.h"
void flashing ()
{
__delay_ms (KONST_OFF);
STROBO_LED = STAV_OFF;
__delay_ms (KONST_ON);
STROBO_LED = STAV_ON;
};
// in this main function I do logical product, where I have logical state from pin GP2 and number 1
#include <xc.h>
#include "prototypy.h"
// Code protection bit
#pragma config CP = OFF
// Watchdog timer
#pragma config WDTE = OFF
// Pin function
#pragma config MCLRE = OFF
void main ()
{
setup ();
while (1)
{
if (RC_SIGNAL & 1)
{
flashing ();
}
}
}
Please, can me someone help and found error in my source code?
I'am not really familar with that devive, but I guess you Pin RC_SIGNAL is not configured as an Input. Please try
TRISGPIO = 0b0100;
I'm new with ARM processors, i recently bought STM32F4 Discovery Kit. Do you suggest me to begin proggraming with CooCoz or another IDE?
Sorry for English, Good work.
Yes, it is easy to use.
Download ST-LINK utility, CoIDE and gcc-arm-embedded toolchain.
Install it and configure CooCox. In "Project -> Select Toolchain Path" select directory with toolchain, by default "C:\Program Files\GNU Tools ARM Embedded\4.8 2013q4\bin"
Create new project, select stm32f407vg chip, then from repository select M4 CMSIS Core, CMSIS BOOT, RCC, GPIO. All needed files will be added to project tree.
Add code to main.c file
//basic headers
#include "stm32f4xx.h"
#include "stm32f4xx_gpio.h"
#include "stm32f4xx_rcc.h"
// delay function
void Soft_Delay(volatile uint32_t number)
{
while(number--);
}
int main(void)
{
//Peripherial clock setup
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
//Port setup
GPIO_InitTypeDef ledinit;
ledinit.GPIO_Mode = GPIO_Mode_OUT;
ledinit.GPIO_OType = GPIO_OType_PP;
ledinit.GPIO_PuPd = GPIO_PuPd_NOPULL;
ledinit.GPIO_Speed = GPIO_Speed_2MHz;
ledinit.GPIO_Pin = GPIO_Pin_15; //
GPIO_Init(GPIOD, &ledinit);
while(1)
{
// Led On
GPIO_SetBits(GPIOD, GPIO_Pin_15);
// Pause
Soft_Delay(0x000FFFFF);
// Led Off
GPIO_ResetBits(GPIOD, GPIO_Pin_15);
// PAuse
Soft_Delay(0x000FFFFF);
}
}
Select "Project -> Rebuild" and "Flash -> Program Download", blue led will start blink.