0

Basically, I am trying to blink an LED on an STM32F103C8T6. Besides the LED on the PCB, there is a label reads "PC13". I did some searching and realized that it probably means the LED is on the 13th pin on port C, so all I need to do is to drive it high or low, but how?

I have tried Googling, and it leads me to this example. However, the author doesn't give enough explanation so I am not sure of the purpose of writing the magic numbers to magic addresses (I guess they are memory-mapped registers?). The example spent some time configuring a peripheral clock, but we don't do this when using the Arduino framework, so why do I need to configure another clock when the goal is as simple as driving a pin? More importantly, it doesn't tell me how to figure out the steps myself: how can I know it is necessary to configure the clock? While ST has provided detailed documents including a Programming manual and Reference manual, I cannot effectively navigate these one-thousand-page manuals. Section 9 General-purpose and alternate-function I/Os (GPIOs and AFIOs) looks relevant, but I just cannot make sense of it.

To reiterate, I would like to know how can I learn how to blink an LED on an embed system with bare-metal assembly. For example, which part of which document do I need to read, in order to find which information? I specify the trivial task of blinking an LED to narrow the scope, and I believe I have shown enough research effort, so I hope this question is on topic.

glts
  • 21,808
  • 12
  • 73
  • 94
nalzok
  • 14,965
  • 21
  • 72
  • 139
  • you look at the schematics and see how the led is wired up, is one end tied high or one end tied low. If tied high then you need to gnd the gpio end, if low then you need to drive the gpio end high to turn it on. or you perform an experiment. – old_timer Feb 08 '20 at 22:44
  • the ST documentation describes how each of the registers work, it documents the addresses, it documents that you need to enable the clocks to the peripheral(s) used. 99% of bare metal is reading, the 1% is writing code of which most of that is throw away experiments. – old_timer Feb 08 '20 at 22:46
  • now yes to be fair the blue pills have been cloned and who knows what schematic if any is real. so for that specific board you need to do experiments. – old_timer Feb 08 '20 at 22:47
  • how are you loading firmware into the flash? and/or running programs? if using openocd then you can use mdw and mww from a telnet session into openocd to read and write those registers in the documentation and figure out how to make it work before having to get a program do it. if using the uart bootloader then just write programs like the one below. If using something else like a usb based bootloader then dont run the program in the duplicate answer as is, learn where the correct address is and how the entry point works and change the linker script and bootstrap to match. – old_timer Feb 08 '20 at 22:51
  • @old_timer Ah thanks for the reply! I’m using an ST-link with OpenOCD, and the mdw/mww would definitely be helpful. I’m going to read the other question, but firstly, where am I supposed to find those header files? OP just presented them in pastebin links without saying where did he get them from – nalzok Feb 08 '20 at 23:05
  • @old_timer ...and can you point me to a few relevant sections in the document? In fact, I don’t think the GPIO section mentions the clock part (or maybe I misread). The problem is I cannot tell which parts do I need to read in detail to blink an LED, and it’s not possible to read all of them – nalzok Feb 08 '20 at 23:14
  • just look at the example in the other question. the rcc registers in the rcc chapter covers clocks and there are a number of clock enables. search for iopc in the reference manual, some docs you can just search gpioc others you cant. work backward from the addresses in the example. first few chips you learn you may just have to read the 1000 pages. it takes practice to learn the different manual styles and avoiding having to read the whole thing, at the same time you fall into traps that you didnt read, by skipping and have to deal with that. – old_timer Feb 08 '20 at 23:28
  • this is all part of this game if you want to play it, most folks use the sandboxes (libraries from the vendor or arduino like environments) so they dont have to deal with this. If you want to just build a hobby project, like some robot or something, use the libraries. if the hobby project is reading manuals and figuring out how things work, and no desire to make a robot or anything useful. then gotta dig through those docs and do countless throwaway experiments trying to understand what they are saying. – old_timer Feb 08 '20 at 23:34
  • it is very possible to read this whole document, if/as you work through each peripheral learning how they work. for an led on a gpio pin, chapter 3 memory bus architecture to get addresses, chapter 8 clocks and resets, chapter 9 gpio. plus information from the armv7-m architectural reference manual with respect to the vector table and how it works, then the gnu gcc and binutils documentation or man pages to laern how to use those tools to make a working binary image. then whatever tool you are using to load the flash – old_timer Feb 08 '20 at 23:37
  • before asking a question like this do a search at this site. STM32F103C8T6 produced some hits and an led blinking example which should be easy to work backward to the docs, saving time but still requiring the necessary work to understand. – old_timer Feb 08 '20 at 23:39
  • @old_timer OK, I guess I need to do some reading now! Besides, I just did some more googling, and it turns out that the header file `stm32f10x.h` used in the other question seems to come from a library named “xdk-asf”. I mean, if a library file is included, then we are not doing bare-metal programming. Should this question still be considered a duplicate? – nalzok Feb 08 '20 at 23:56
  • see my answer, not the question, contains a complete blink the led on the blue pill example. baremetal in this context means no operating system, so libraries and such are still within baremetal. there isnt necessarily an adjective that defines with or without vendor libraries or someone elses libraries or making your own libraries, for a real, full sized, project wouldnt you make libraries for yourself? and header files? – old_timer Feb 09 '20 at 00:17
  • @old_timer Yeah that makes sense. One more question: is there any prerequisites I need to learn before diving into the documents? I come from a software background and am not super familiar with hardware concepts – nalzok Feb 09 '20 at 00:34
  • just take it one step at a time, you can certainly figure out the difference between input and output. and have a rough idea what a clock enable means. you want push pull for the gpio not open drain. timers you can somewhat figure out, etc... – old_timer Feb 09 '20 at 01:56

0 Answers0