Author Topic: help with stm32f103 led code  (Read 1224 times)

0 Members and 1 Guest are viewing this topic.

Offline jeet55Topic starter

  • Contributor
  • Posts: 19
help with stm32f103 led code
« on: July 09, 2020, 07:38:47 am »
Code: [Select]
int main()
{
RCC->APB2ENR |= RCC_APB2ENR_IOPCEN;
GPIOC->CRH |= GPIO_CRH_MODE13_1;
GPIOC->CRH &= ~GPIO_CRH_MODE13_0;
GPIOC->CRH &= ~(GPIO_CRH_CNF13_0 | GPIO_CRH_CNF13_1);
GPIOC->ODR |= GPIO_ODR_ODR13;
//GPIOC->BSRR |= GPIO_BSRR_BS13;

while(1)
{

}
return 0;
}
this is the code for turning on pc13 led on stm32f103 but it doesn't work any guess why?
 

Offline ozcar

  • Frequent Contributor
  • **
  • Posts: 322
  • Country: au
Re: help with stm32f103 led code
« Reply #1 on: July 09, 2020, 08:47:38 am »
What is the other end of the LED connected to? Maybe you have to drive PC13 low to turn it on.
 

Offline jeet55Topic starter

  • Contributor
  • Posts: 19
Re: help with stm32f103 led code
« Reply #2 on: July 09, 2020, 08:54:39 am »
tried driving it low doesn't work
edit: sorry you wr correct led needs to be driven low thank you
« Last Edit: July 09, 2020, 10:04:10 am by jeet55 »
 

Online Doctorandus_P

  • Super Contributor
  • ***
  • Posts: 3367
  • Country: nl
Re: help with stm32f103 led code
« Reply #3 on: July 12, 2020, 12:02:19 am »
I recommend to start with a working example project.

I'm also foolin' around with the Blue Pills & STM32, and have gotten some projects to work, but it's not an easy task.

There are different clones of the STM32F103 around, there are different toolchains, different flash programs and bootloaders, different frameworks (CMSIS, Mbed, arduino, opencm3, etc)

The uC also has to initialize a bunch of "stuff" before main() is even called, and if you make some error in the startup code, there is no hope of your thing ever working properly.

Also:
Instead of just setting or resetting the LED pin, try toggling it. That works with either polarity, and you can also see the uC is "doing something".

The blinking led project is always one of the most difficult. There is a lot that can go wrong, and few hints of what is going wrong.

What is the output from your programmer? Can you verify the chip can at least be programmed?
 

Online Doctorandus_P

  • Super Contributor
  • ***
  • Posts: 3367
  • Country: nl
Re: help with stm32f103 led code
« Reply #4 on: July 12, 2020, 09:14:08 am »
Today I've had success again.
I had some very weird problems (as usual).

One of the problems was that my latest batch of "Blue Pills" had some "id code" 0x2ba01477
For older Pills, the ID code was: 0x1ba01477
This had some problem with the toolchain I used "some time ago".

Yesterday & today I got a blinking LED working again on my new batch of Blue Pills.
I could not install stlink-tools with apt (Still have to figure out why that does not work), So I compiled from source (github.com/texane)

I had a look at the first tutorial from Matej Bagsic:


His blinking led project (github) does not "blink" though, and is dependent on an I/O pin, and it's also for an STM32F40x.

So, after a quick search:
https://www.startpage.com/row/search?q="blue+pill"+blinking+led+example+program

I found Satoshim (again):
https://github.com/satoshinm/pill_blink

I compiled his "bare-metal/" project with the provided makefile.

There is no programmer specified in that makefile, so I added a target for st-flash:

flash: pill_blink.bin
   st-flash write pill_blink.bin 0x8000000

After that I have changed the blink rate multiple times, and verified the change in the blink rate on the Blue Pill after flashing.

There were 2 other weird problems I had to overcome:
I have another Blue Pill, which I flashed with a GRBL variant some time ago (years), and this was still attached to an USB port and somehow this showed up as an STM32F40x and gave an incromprehensible error with libusb, but after detaching the plug the error went away.

Some other things to keep track of:
The first time you flash with st-flash, you overwrite the bootloader (which also has a blinking led). And then the led stopped blinking, it just went off. After a power cycle it started flashing at the new flashrate, and consecutive programming cycles did not need a power cycle, nor reset button or BOOT jumper changes. Both BOOT jumpers are "0", that is, on the side of the USB connector.

In the old programmer software I used I had to "unlock" the flash of each new blue pill before it could be written. With st-flash this is apparently not necessary anymore.


Satoshinm also has blink projects for CubeMX and LibOpenCM3, so pick your poison.
« Last Edit: July 12, 2020, 09:41:43 am by Doctorandus_P »
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf