STM32 blinky example does not work

1

Here is a "simplified blinky" example from MikroC meant to toggle the user LED at a rate that is visible to the naked eye:

void main()
{
    GPIO_Clk_Enable(&GPIOA_BASE);
    GPIO_Digital_Output(&GPIOA_BASE, _GPIO_PINMASK_5);
    GPIOA_ODR |= (1<<_GPIO_PIN_5);
    while(1)
    {
      GPIOA_ODR ^= (1<<_GPIO_PIN_5);
      Delay_ms(300);
    }
}

The expected result of this code is that the port A5 pin (LED) will toggle at a 300ms rate. The microcontroller part is an STM32L476RG on a NUCLEO board.

The actual result is still -- nothing at all. No blinking.

I have tested the board by loading the hex file from an identical board onto it, restoring the "factory" blinking demo that responds to presses of the user button. It works. So the device is fine and the programming process is working.

I have stepped through the code in the debugger and watched GPIOA_ODR register toggle just as intended.

My Scheme, created by using the "Default" button, running the core at 4 MHZ from from the MSI internal oscillator, is as follows:

<?xml version="1.0"?>
<MCU_DEVICE_FLAGS>
  <DEVICE>
    <DEVICE_NAME>STM32L476RG</DEVICE_NAME>
    <SETTINGS>
      <COUNT>32</COUNT>
      <SETTING0>
        <NAME>MSI clock enable</NAME>
        <DESCRIPTION>MSI oscillator ON</DESCRIPTION>
      </SETTING0>
      <SETTING1>
        <NAME>MSI clock PLL enable</NAME>
        <DESCRIPTION>MSI PLL OFF</DESCRIPTION>
      </SETTING1>
      <SETTING2>
        <NAME>MSI clock range selection</NAME>
        <DESCRIPTION>MSI Range is provided in RCC_CSR register</DESCRIPTION>
      </SETTING2>
      <SETTING3>
        <NAME>MSI clock ranges in RCC_CR register</NAME>
        <DESCRIPTION>range 6 around 4MHz</DESCRIPTION>
      </SETTING3>
      <SETTING4>
        <NAME>HSI clock enable</NAME>
        <DESCRIPTION>HSI16 oscillator OFF</DESCRIPTION>
      </SETTING4>
      <SETTING5>
        <NAME>HSI16 always enable for peripheral kernels</NAME>
        <DESCRIPTION>No effect on HSI16 oscillator</DESCRIPTION>
      </SETTING5>
      <SETTING6>
        <NAME>HSI16 automatic start from Stop</NAME>
        <DESCRIPTION>HSI16 oscillator is not enabled by hardware when exiting Stop mode with MSI as wakeup clock</DESCRIPTION>
      </SETTING6>
      <SETTING7>
        <NAME>HSE clock enable</NAME>
        <DESCRIPTION>HSE oscillator OFF</DESCRIPTION>
      </SETTING7>
      <SETTING8>
        <NAME>HSE crystal oscillator bypass</NAME>
        <DESCRIPTION>HSE crystal oscillator not bypassed</DESCRIPTION>
      </SETTING8>
      <SETTING9>
        <NAME>Clock security system enable</NAME>
        <DESCRIPTION>Clock security system OFF</DESCRIPTION>
      </SETTING9>
      <SETTING10>
        <NAME>Main PLL enable</NAME>
        <DESCRIPTION>PLL OFF</DESCRIPTION>
      </SETTING10>
      <SETTING11>
        <NAME>SAI1 PLL enable</NAME>
        <DESCRIPTION>PLLSAI1 OFF</DESCRIPTION>
      </SETTING11>
      <SETTING12>
        <NAME>SAI2 PLL enable</NAME>
        <DESCRIPTION>PLLSAI2 OFF</DESCRIPTION>
      </SETTING12>
      <SETTING13>
        <NAME>System clock switch</NAME>
        <DESCRIPTION>MSI selected as system clock</DESCRIPTION>
      </SETTING13>
      <SETTING14>
        <NAME>Set and cleared by software to control the division factor of the AHB clock</NAME>
        <DESCRIPTION>SYSCLK not divided</DESCRIPTION>
      </SETTING14>
      <SETTING15>
        <NAME>APB low-speed prescaler (APB1)</NAME>
        <DESCRIPTION>HCLK not divided </DESCRIPTION>
      </SETTING15>
      <SETTING16>
        <NAME>APB high-speed prescaler (APB2)</NAME>
        <DESCRIPTION>HCLK not divided </DESCRIPTION>
      </SETTING16>
      <SETTING17>
        <NAME>Wakeup from Stop and CSS backup clock selection</NAME>
        <DESCRIPTION>MSI oscillator selected as wakeup from stop clock and CSS backup clock</DESCRIPTION>
      </SETTING17>
      <SETTING18>
        <NAME>Microcontroller clock output</NAME>
        <DESCRIPTION>MCO output disabled, no clock on MCO</DESCRIPTION>
      </SETTING18>
      <SETTING19>
        <NAME>Microcontroller clock output prescaler</NAME>
        <DESCRIPTION>MCO is divided by 1</DESCRIPTION>
      </SETTING19>
      <SETTING20>
        <NAME>Main PLL, PLLSAI1 and PLLSAI2 entry clock source</NAME>
        <DESCRIPTION>No clock sent to PLL, PLLSAI1 and PLLSAI2</DESCRIPTION>
      </SETTING20>
      <SETTING21>
        <NAME>Division factor for the main PLL and audio PLL (PLLSAI1 and PLLSAI2) input clock</NAME>
        <DESCRIPTION>PLLM = 1</DESCRIPTION>
      </SETTING21>
      <SETTING22>
        <NAME>Main PLL multiplication factor for VCO</NAME>
        <DESCRIPTION>16</DESCRIPTION>
      </SETTING22>
      <SETTING23>
        <NAME>Main PLL PLLSAI3CLK output enable</NAME>
        <DESCRIPTION>PLLSAI3CLK output disable</DESCRIPTION>
      </SETTING23>
      <SETTING24>
        <NAME>Main PLL division factor for PLLSAI3CLK (SAI1 and SAI2 clock)</NAME>
        <DESCRIPTION>PLLP = 7</DESCRIPTION>
      </SETTING24>
      <SETTING25>
        <NAME>Main PLL PLL48M1CLK output enable</NAME>
        <DESCRIPTION>PLL48M1CLK output disable</DESCRIPTION>
      </SETTING25>
      <SETTING26>
        <NAME>Main PLL division factor for PLL48M1CLK (48 MHz clock)</NAME>
        <DESCRIPTION>PLLQ = 2</DESCRIPTION>
      </SETTING26>
      <SETTING27>
        <NAME>Main PLL PLLCLK output enable</NAME>
        <DESCRIPTION>PLLCLK output disable</DESCRIPTION>
      </SETTING27>
      <SETTING28>
        <NAME>Main PLL division factor for PLLCLK (system clock)</NAME>
        <DESCRIPTION>PLLR = 2</DESCRIPTION>
      </SETTING28>
      <SETTING29>
        <NAME>LSI oscillator enable</NAME>
        <DESCRIPTION>LSI oscillator OFF</DESCRIPTION>
      </SETTING29>
      <SETTING30>
        <NAME>MSI range after Standby mode (RCC_CSR)</NAME>
        <DESCRIPTION>Range 6 around 4 MHz (reset value)</DESCRIPTION>
      </SETTING30>
      <SETTING31>
        <NAME>Core Voltage</NAME>
        <DESCRIPTION>VCORE = 1.2 V</DESCRIPTION>
      </SETTING31>
    </SETTINGS>
  </DEVICE>
</MCU_DEVICE_FLAGS>

Question: What is wrong with this default scheme or this simple code that prevents this from working?

N.B. I have chosen the MikroC tag even though this is an STM32 part. The definition for the MikroC tag is incorrectly specific to PIC devices.

When I contacted the vendor about this problem, they gave me a scheme file to load. I attempted to load it but it wouldn't even load. It had some spelling errors. After correcting them based on the default scheme above, the example still didn't work. They had chosen many non-default settings. My rationale is that if I'm using the default (MSI) clock and default scheme, I should at least be able to get the part to run at 4MHz and have a successful starting point for further improvements.

Finally, the configuration register values from the project configurator are as follows:

RCC_CR      : $40021000 : 0x00000061
RCC_CFGR    : $40021008 : 0x00000000
RCC_PLLCFGR : $4002100C : 0x00001000
RCC_CSR     : $40021094 : 0x00000600
PWR_CR1     : $40007000 : 0x00000200
embedded
stm32
mikroc
asked on Stack Overflow Jan 12, 2018 by TomServo • edited Jan 14, 2018 by TomServo

1 Answer

3

GPIO_Digital_Output() configures the pin as a output but it may not enable the clock to the GPIO peripheral. Try adding a call to GPIO_Clk_Enable(&GPIOC_BASE).

answered on Stack Overflow Jan 12, 2018 by kkrambo

User contributions licensed under CC BY-SA 3.0