STM32 L1 Tutorial #1: Hello, World! (LED Blink)

stm32_icon_bigToday I’m about to show you how to make any use of my version of STM32L Standard Peripheral Library (post, .zip). First order of business for everyone that starts using MCU that he never used before is to get into controlling states of MCUs General Purpose Input/Output (GPIO) pins which translates to setting the voltage high or low on those pins. This can (and will) be used to lit up a LED directly connected to leads of our STM32L. I am assuming that reader has some basic knowledge about C Programming language, because I’m not planning to describe every line in every file, but to focus on what’s important. Here’s the code that does all that magic:

/* system entry point */
int main(void)
	/* gpio init struct */
	gpio_init_t gpio;

	/* reset rcc */

	/* enable clock to GPIOC */

	/* initialize gpio structure */
	/* use pin 13 */
	gpio.pins = GPIO_P13;
	/* mode: output */
	gpio.mode = GPIO_OUTPUT;
	/* output type: push-pull */
	gpio.otype = GPIO_OT_PP;
	/* apply configuration */
	GPIO_Init(GPIOC, &gpio);

	/* main program loop */
	for (;;) {
		/* set led on */
		GPIO_SetPins(GPIOC, GPIO_P13);
		/* delay */
		/* clear led */
		GPIO_ClearPins(GPIOC, GPIO_P13);
		/* delay */

	/* never reached */
	return 0;

Preparing the MCU

First step when you are dealing with STM32L will be resetting the RCC, which is done in line #8. RCC is that little MCU fragment that is responsible for enabling/disabling clock to all other modules, such as GPIO, as well as controlling MCU clock speed. After a reset we need to bring it to a known state and that’s why we call RCC_Deinit() routine.

Configuring GPIO

In order to be able to use GPIO port (MCU pins are grouped in ports, every port can have up to 16 pins) we need to enable it’s clock. It is a common pitfall to forget about enabling particular peripheral’s clock, so keep in mind that this should be the first step before using any of peripheral’s registers.

Since we’ve got our GPIO up and running, we need to configure the operation of pin that will drive our LED. This is accomplished with the use of gpio_init_t structure. First thing that should be done here is to reset it’s contents by calling GPIO_StructInit() which will set all fields to their default values. Why bother using this function? Well, since our struct is stored on stack (it is defined inside main() function, not as a global variable) it will contain rubbish values, because stack variables aren’t initialized just as global variables are. Several fields of that data structure have been used to define pin’s behavior:

  • gpio.pins – selects which pin we are about to configure. In this case it’s pin 13.
  • gpio.mode – chooses pin mode of operation, which can be input, output, alternate function (pin driven by some other peripheral) or analog (pin used by analog to digital converter), in this case, since we want to *drive* LED we need to go for GPIO_OUTPUT.
  • gpio.otype – output pins can operate in two modes: push-pull (MCU can set pin high and low) or open-drain (MCU can set pin low and high-impedance). We choose push-pull mode to be able to set pin high.

After setting all the fields we need to apply this configuration to given port. My LED is connected to pin PC13, I used GPIO_Init() with the first argument set to GPIOC.

Using GPIO pin

Now the eye-candy. I have written a simple loop (lines 25 – 34) that will turn-on and turn-off the LED just to demonstrate that this tutorial actually does something. As you can see the simplest way of altering pin output states is to use GPIO_SetPin() and GPIO_ClearPin() respectively. I have also inserted some wait-sates just so the LED doesn’t blink too fast.


  • Source Code – all that you need to run this example, just compile, upload to your mcu and observe the magic!

STM32 L1 Standard Peripheral Lib Revisited

stm32_icon_bigHello everyone! Today I thought that I might actually share my version of STM32L Standard Peripheral Library that I am using for all my projects. You would probably ask if it is any different from what ST Microelectronics already gave you. Well there are few differences which are presented below. You may also ask why bother switching to my library instead of using ST’s. The main reason for that is, that all of my projects are/will be based on this piece of code, so if you are interested in those you may want to get familiar with my lib. I plan to upload some simple examples (“Hello, World!” like, you know, for basic interfaces, GPIO, SPI, I2C, DMA…), so stay tuned and watch for updates.


  • I’ve got rid of all that CMSIS related sources – since I’m using CodeSourcery toolchain I have no need to have those. To maintain support for all ARM related things I have created separate files like nvic.c, systick.c and so on, which are no different (in terms of usage, from programmer’s point of view, of course) from any other peripheral.
  • I’ve improved code formatting, because I hate it when lines of code have more than 80 characters. That gray, vertical line (a.k.a. “margin”) in your editor exists there for a reason which is called “usability”. Whenever you try to open two source code files in, let’s say, Eclipse IDE then long lines of code can really ruin your day, because not only that you have to scroll up and down but, in addition you need to go from left to right. Bummer!
  • Most of functions have the same name as in ST’s version, but some of those were altered to be more descriptive about what they really do. Nevertheless you should’n have any hard time with that.
  • I made some use of bit-banding feautre in some of those functions, just to improve overall performance.
  • I removed all #define #if and all that macro magic used in *.c files by ST. I find it pretty hard to debug such things.
  • I removed all “asserts“, wasn’t using those anyway…

That’s about it. I’m about to post all my source code in downloads section of this page, so you all can have a better access to it.

Mighty Little FM Transmitter (part 1)

fmtrx_icon_bigWell there is one thing that’s most annoying about laptops: their speakers. People often choose to listen to their music with headphones, but still, sitting with headphones on for a number of hours is not too comfortable nor healthy. Many of us use speaker systems but that often leads to quite a mess with all those cables and power cords.

Since I’m in possession of Phillips Music Center with integrated FM receiver I wanted to make some use out of it. In order to avoid a long piece of audio cable going on my floor from my laptop to music center itself, I thought of a device that would allow me to send my audio signal in a wireless manner. Here is the whole story how this simple idea materialized.

Step One: Concept.

To make this device functional it had to satisfy following requirements:

  • Ability to transmit high quality audio signal in FM Broadcast Band (stereo, high bandwidth, etc.)
  • Implement USB Audio Sound Card (driver-less solution) to get all the audio from my laptop, so that additional stereo jack connection wouldn’t be necessary.
  • Power the whole system from USB port (again – no additional cables)
  • Optional: Implement FM receiver, so that I could send audio from one PC to another
  • Support as many different architectures/OSes/etc. as possible
  • Implement an easy way to modify parameters (such as transmit frequency) over USB (single interface to rule it all)

Step Two: Choosing the right components for the job.

… a.k.a choosing the obvious. Implementing whole FM circuitry all by myself would be pointless and painful (to say the least). The overall outcome in terms of performance would be doubtful, so I decided to choose a single IC solution that integrates all that I needed: Si4721, from Silicon Labs. Just a single glance at it’s datasheet and you know you’ve scored a home-run.

What about MCU? Well, considering my stock, again – I chose the obvious candidate for the job: STM32L151, from ST Microelectronics. I have couple of those as leftovers from my previous projects. It embeds all that is needed to make a perfect ‘heart’ of entire system and I really like working with ST MCUs :)

I also decided to design and implement a simple RF Power Amplifier just to pump up the output antenna swing a little bit, mainly because I’m about to use short monopole antenna, known as ‘the wire’ Using full size monopole antenna for FM Band would be a project-killer IMHO (75 cm of wire sticking out of my USB dongle? no thanks, maybe another time..)

Additional parts included: some LDO that will be capable of powering all ICs and PA as well, LEDs just to notify the user of current mode of operation, USB state, etc.., passive components, blah blah…

Stay tuned for more !