Tag Archives: Advanced Embedded-Systems


STM32 External Interrupt

In my earlier post on STM32 GPIOs I showed how to flash a LED with variable delay times. That example was based on polling method where the code continuously monitored the logic state of a GPIO input pin attached to a push button to determine the delay amount. Obviously that won’t be an efficient technique when a program will be of a considerable size and complexity. This is simply so because the CPU will have to check the GPIO’s logic state every time the super-loop (while (1) loop in the main function) repeats and the push button will also not be responsive during the software delay function calls. Thus the overall performance is poor and not real-time. To get rid of these issues, we’ll need to use external interrupts – a vital feature in every common microcontroller.

STM32F1xx series are ARM Cortex M3 based MCUs. The Cortex M3 based MCUs have a sophisticated and yet easy to use interrupt system called the Nested Vectored Interrupt Controller (NVIC). It ensures low latency and high performance. There are several features of the NVIC and these are handled by the compiler. Our job is simply to enjoy the lightning fast interrupt responses owing to the NVIC. In many MCUs’ interrupt system, interrupt priority can be set and Reset has the highest interrupt priority over anything else. The same things go for STM32s too. However at present I’m not going to go that deep as that’s not needed for now. In some upcoming post may be I’ll discuss the NVIC in details. As per STM32’s reference manuals for more information on exceptions and NVIC programming read Chapter 5 Exceptions and Chapter 8 Nested Vectored Interrupt Controller of the ARM Cortex-M3 Technical Reference Manual. There are other interrupts that are related to RTC, timer, etc. We won’t also look into them in this post. We will learn about them when we learn about the related hardware with them.

EXTI internal

Read more

Porting ST Standard Peripheral Library (SPL) with MikroC PRO for ARM

What is this Standard Peripheral Library (SPL)?

It’s just a collection of hardware libraries that provide an easy approach to any STM32 ARM programmer. It has support for every peripheral a STM32 micro has like CAN, USB, ADC, Timers, etc. In short it’s a hardware abstraction layer fully covering the STM32.

Why use the ST SPL?

  1. Reduce coding time by spending less time figuring out 32 bit register values and going through a near thousand page reference manual.
  2. Take the advantages of the built-in code library of MikroC which other compilers don’t offer.
  3. SPL is used by most STM32 ARM programmers and so it is a widely used tool with a large community.
  4. Most books and documents on STM32 ARMs are based on SPL.
  5. A compiler like MikroC doesn’t give full focus towards STM32. It has other ARM concerns like TI’s TIVA, NXP, etc and so MikroC’s built-in libraries are not 100% compatible with all ST hardware.
  6. Following the previous point MikroC (at least at the time of writing this article) doesn’t give library support for hardware like the DMA and so there’s incompleteness to some extent. It is also worth noting that depending on MikroC’s library also has the disadvantage of code size and efficiency. It is in general true for every compiler. However MikroC’s IDE is cool enough to do things with ease and that’s why I like it. Unless you are a Maple or Arduino fanboy, you’ll get my point.

Software Needed

  1. Modified ST Standard Peripheral Library (SPL) as supplied here: STM32 Standard Peripheral Library for MikroC. It works for STM32F10x micros only.
  2. MikroC PRO for ARM compiler.
  3. Flash Loader Demo or ST-Link Programmer Software.

Hardware Needed

  1. A STM32F10x Development Board.
  2. A USB-Serial Converter or ST-Link Programmer.
  3. Some basic components like a mini bread board, connecting wires/jumpers, a LED, a 220Ω 0.25W resistor and a push button.
  4. Optional debugger hardware.

 

How to use the Standard Peripheral Library?

First we need to prepare our MikroC PRO for ARM compiler for linking with SPL. I’m assuming that the compiler is preinstalled. First go to the compiler’s installation folder and look for the include folder. In my case, it is:
C:\Users\Public\Documents\Mikroelektronika\mikroC PRO for ARM\Include.

In this folder there’s a file named stdint.h. It’s basically a definition of variable types and other related stuffs. Rename it to stdint (backup).h and copy the new stdint.h file from the supplied folder to this location. We are done here.

stdint modification

Read more

STM32 – Prior to Start

STM32 ARM-based micros from STMicroelectronics pack high density resources than any other conventional microcontroller. They are also high speed devices, operating typically at 72MHz and beyond. Despite several advanced features and heavy resources, they turn out to be misfortunes for beginners who wish to play with them. Available in market are several cool STM32 boards but most of them are not well documented. The aim of this document is to address some common FAQs.

Typically most people ask the following question:

  • How to program the STM32 micro embedded in my development board?
  • What tools do I need to get started?
  • What compiler support do I have for STM32?
  • What resources are available for STM32 on the internet?

In this doc, I’ll be addressing these FAQs along with some basics as I had similar questions in my early expeditions with STM32 micros. When I started with STM32 these questions were there as they are now. Literally there was nothing to address them till now. Most experts take it for granted that those who are beginning with ARMs must have heavy knowledge on the topic and thus skip answering these simple or silly questions.

Well I decided to break this stalemate for any starter no matter what’s his/her experience. Firstly most people nowadays believe ARM is only for mobile phones, PDAs, tablets, etc. and can’t be used like other micros like AVRs and PICs. This is completely wrong. Firstly because no one has set such a rule and secondly because there’s no limit to requirements in an embedded design. However it would be wasteful to make simple LED blinking stuffs with such chips. When it comes to size and resources ARM varies in all aspects. You can have an ARM chip with similar resources available in a typical 8 bit or 16 bit micro and yet you can also have an ARM chip with high resources. For instance if you think of flash memory, an ARM chip can have a few kilobytes to several megabytes of flash memory. Thus not just for real-time operating systems, phones, DSP, graphical processing and so on but also for other kinds of embedded systems the door for ARM is always open. It’ll completely depend on its user. STM32 portfolio has wide ranges of 32 bit ARM micros just as I described. A quick look on ST’s page will give you a brief idea: http://www.st.com/web/en/catalog/mmc/FM141/SC1169 .

Answering from the last, let’s see what resources are available for STM32s on the internet. Going back to ST’s site one can clearly see that there are several app notes for STM32s. At the time of writing this doc there 83 app notes on ST’s page. Obviously then there are reference manuals, datasheet and other docs too. Of these the important ones are the datasheet of a particular micro and its family reference manual. The rest of the stuffs are various software and tools but here confusion arises. We’ll see what other things are needed later but for now let’s see the purpose of reference manuals and datasheet. The reference manual is needed for understanding the internal hardware of a particular STM32 family, the registers associated with these hardware, programming techniques and what “not”s and what “are”s. These make it the core document of all. Datasheet basically give overviews of the devices. Books available for STM32-based ARMs are hard to find but still found a book called Discovering STM32 on Google search’s first page.

Regarding compiler selection, there are several available for STM32 micros. The most common ones are Keil, CooCox and MikroC PRO for ARM. My personal favourite is MikroC (http://www.mikroe.com/mikroc/arm/) as I’m already familiar with MikroC for AVR and 8051. MikroC comes with lot of easy-to-use built-in and examples and so it’s a good compiler to work with. Anyone familiar with MikroC will not have any trouble using it for ARM. With MikroC one can go to raw levels and he/she can also go for easy library-based coding.

Now let’s see what tools we’ll need for STM32 micros. The first tool we’ll need is called Flash Loader Demonstrator (http://www.st.com/web/en/catalog/tools/PF257525).  Basically it’s a programmer GUI based on factory loaded bootloader that allows a user to load, erase, verify, read or set configuration bits for his/her STM32 micro. The second tool is called Microxplorer (http://www.st.com/web/en/catalog/tools/PF251717). This tool allows a user to graphically set configurations for IO ports, internal hardware and many more. Basically it’ll help you reduce your time debugging registers. Both of these software are free and easy to use.  The last but not the least tool is called Timer Calculator (http://www.libstock.com/projects/view/398/timer-calculator). It’s a handy tool for those who’ll use MikroC for ARM. It can be used to generate codes for timer interrupts and timer-based stuffs. There are other tools too.

The final topic is about loading code to a STM32 micro. There are two ways either by a JTAG programmer like ST-Link or by bootloader and UART. The former is the best choice as it is a programmer and a debugger but in terms of expense this is unnecessary as the chips come with preprogramed bootloader. Thus the latter method of using bootloader and UART is simplest and also the most popular solution. This is also my personal preference as I didn’t want to spend for another programmer just for the purpose studying. This is the method I’ll describe here. Let’s see the board:

Untitled

Read more

Recent Entries »