Book Image

ARM® Cortex® M4 Cookbook

By : Mark Fisher, Dr. Mark Fisher
Book Image

ARM® Cortex® M4 Cookbook

By: Mark Fisher, Dr. Mark Fisher

Overview of this book

Embedded microcontrollers are at the core of many everyday electronic devices. Electronic automotive systems rely on these devices for engine management, anti-lock brakes, in car entertainment, automatic transmission, active suspension, satellite navigation, etc. The so-called internet of things drives the market for such technology, so much so that embedded cores now represent 90% of all processor’s sold. The ARM Cortex-M4 is one of the most powerful microcontrollers on the market and includes a floating point unit (FPU) which enables it to address applications. The ARM Cortex-M4 Microcontroller Cookbook provides a practical introduction to programming an embedded microcontroller architecture. This book attempts to address this through a series of recipes that develop embedded applications targeting the ARM-Cortex M4 device family. The recipes in this book have all been tested using the Keil MCBSTM32F400 board. This board includes a small graphic LCD touchscreen (320x240 pixels) that can be used to create a variety of 2D gaming applications. These motivate a younger audience and are used throughout the book to illustrate particular hardware peripherals and software concepts. C language is used predominantly throughout but one chapter is devoted to recipes involving assembly language. Programs are mostly written using ARM’s free microcontroller development kit (MDK) but for those looking for open source development environments the book also shows how to configure the ARM-GNU toolchain. Some of the recipes described in the book are the basis for laboratories and assignments undertaken by undergraduates.
Table of Contents (16 chapters)
ARM Cortex M4 Cookbook
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface
Index

Understanding the simple use of GPIO


Making an LED blink involves connecting it to a signal that alternately switches ON and OFF. General purpose input/output (GPIO) is the name of a microcontroller peripheral that provides functionality to source many signals at once (that is, in parallel). GPIO peripherals are designed to be very flexible, so configuring them can be rather confusing but using the RTE manager makes this process much simpler. We will modify our helloBlinky_c1v0 recipe to simultaneously make all the LEDs blink rather than just one. Each LED on the evaluation board is connected to a pin on the microcontroller, so to illuminate an LED the microcontroller needs to provide a voltage and current similar to the that of a torch battery. To source this current, the corresponding GPIO port bit connected to the pin must be configured as an output that is switched ON and OFF by statements in our program that write to the port output data register.

How to do it…

To configure the GPIO follow the steps outlined:

  1. Make a copy of the helloBlinky_c1v0 folder from the previous recipe (and its contents) and rename this copy as helloBlinky_c1v1. Open the folder and open the helloBlinky project (double-click on the file). Then edit the main function defined in the helloBlinky.c file search for the following statement:

    LED_On (num);
  2. Replace this statement with the following one:

    LED_SetOut (On_Code);
  3. Also, search for the following statement:

    LED_Off (num);
  4. Replace this statement with the following one:

    LED_SetOut (Off_Code);
  5. The variables, On_code and Off_Code, are declared, as follows:

    const unsigned intOff_Code = 0x0000;
    const unsigned intOn_Code = 0x00FF;
  6. A complete listing of the main function is as follows:

    /*--------------------------------------------------
     * Recipe:  helloBlinky_c1v1
     * Name:    helloBlinky.c
     * Purpose: Simultaneous MCBSTM32F400 LED Flasher 
     *--------------------------------------------------
     * Modification History
     * 16.01.14 Created
     * 03.12.15 Updated 
     * (uVision5v5.17+STM32F4xx_DFP2.6.0)
     *
     * Dr Mark Fisher, CMP, UEA, Norwich, UK
     *--------------------------------------------------*/
    #include "stm32F4xx_hal.h"
    #include "Board_LED.h"
    
    int main (void) {
    const unsigned intOff_Code = 0x0000;
    const unsigned intOn_Code = 0x00FF;
      unsigned inti;
    
    LED_Initialize();                      /* LED Init */
    
      for (;;) {                         /* Loop forever */
    LED_SetOut (On_Code);            /* Turn LEDs on */	
        for (i = 0; i< 1000000; i++)
    /* empty statement */ ;                /* Wait */
    LED_SetOut (Off_Code);          /* Turn LEDs off */
        for (i = 0; i< 1000000; i++)
    	/* empty statement */ ;                   /* Wait */
      } /* end for */
    }
  7. Build, download, and run the application in exactly the same way as we did in the previous version.

How it works…

The GPIO interface is a particularly important feature in microcontrollers because it is designed to be easily integrated within user systems to drive light emitting diodes, read the state of switches, or connect to other peripheral interface circuits. Early I/O ports were prewired to provide either output or input interfaces, but soon they evolved into general purpose interfaces that could be programmed to provide either output or input connections. Later devices included more programmable features. As GPIO is so important for microcontroller applications, designers are keen to specify as many I/O pins as possible on their devices. However, increasing the device pin-out adds cost because the device becomes physically larger to accommodate the pins. This motivates manufacturers to develop devices that have pins that are configured by software. As you can imagine, configuring such a device is quite a challenge, so we're lucky that Keil's developers have provided library functions that make this task more manageable. As GPIO represents the interface between hardware and software, the evaluation board's schematic (http://www.keil.com/mcbstm32f400/mcbstm32f400-schematics.pdf) is essential to understanding the I/O.

The STM microcontroller used by the evaluation board provides eight GPIO ports, named A-I. Port pins PG6,7,8; PH2,3,6,7; PI10 are connected to LEDs. Those who have never encountered an LED may imagine it as a filament lamp, but an LED is a semiconductor device and behaves slightly differently. However, sticking with our initial lamp analogy (for the time being), we'll first consider a battery-operated torch comprising a battery, switch, and lamp. These components are connected by a copper wire that is often hidden within the body of the torch. We'll assume that the torch uses two AA batteries providing a voltage of about 3 Volts. We can depict the circuit as a diagram with symbols representing each of the components, as shown in the following diagram:

When we close the switch, the battery voltage (denoted V) is applied directly to the lamp, a current flows (denoted I), heating the lamp filament, and this in turn, gives out light.

The electrical resistance (denoted R) of the filament determines the amount of current that flows according to Ohm's Law that is as follows:

Lamp filaments used in torches usually have a resistance of about 10 Ohms (10 Ω), so the amount of current flowing is about 0.3 A or 300 mA.

Imagine that a fault develops, which produces a short across the lamp. The current flowing is now only limited by the resistance of the copper wire and the internal resistance of the battery; these are both very small (a fraction of an Ohm). A high current will circulate which might, if the battery stored enough energy, cause the copper wire to heat up and melt the plastic case of the torch. However, AA batteries are unable to store sufficient energy for this to be a serious problem and in most cases the battery will discharge within a few seconds.

In modern torches, the lamp is replaced by an LED, which is a semiconductor device (its electrical properties lie between those of conductors, such as copper, and insulators, such as glass). An LED is a two terminal device with special properties. One of the terminals is known as the anode and the other as the cathode. If we replace the lamp in our torch with an LED, then current will only flow and the LED will illuminate when the anode is connected to the positive-battery terminal and the cathode to the negative-battery terminal, as depicted in the following diagram:

If we connect the device the other way round as depicted in the right side of the preceding diagram, then no current will flow; so, make sure that the batteries in your LED torch are fitted the right way round! When the anode is connected to the positive-battery terminal, the diode resistance is very low and the diode is said to be forward biased. When the cathode is connected to the positive-battery terminal the diode exhibits an extremely high resistance (negligible current flow) and the diode is said to be reversed biased. When forward biased, the LED exhibits an extremely low resistance, so an additional resistor must be placed in the circuit to limit the current flowing.

GPIO can also be used to read the state of switches that are connected to microcontroller pins. For this operation, each port bit must be configured as an input. When configured for input (that is, output is disabled), each bit of the parallel port's input data register is connected to a pin on the integrated circuit (on which the embedded processor is fabricated). Let's assume that we wish to connect a simple push-button switch to an input bit such that when the switch is operated, a voltage is applied to the port (pin), otherwise, no voltage is applied. The circuit a) shown as follows will achieve this. A complementary circuit that produces a voltage when the switch is open, and no voltage when the switch is operated (closed) is shown in b):

To eliminate the need for an additional resistor, the GPIO port input circuit includes one that can be configured by software as pull-up, pull-down, or disconnected. Obviously, when the port is configured as an output, both resistors are disconnected.

There's more…

Section 7 of STMicroelectronics Reference manual RM0090 (www.st.com) for microcontrollers featuring the Cortex-M4 provides comprehensive programming details for the GPIO port. As well as producing logic signals (for example, making LEDs blink) and reading logic levels (for example, from switches), GPIO ports also provide an I/O path for other peripheral functions, such as Times and Digital-to-Analogue converters. We'll take a closer look at GPIO later on in this cookbook when we write programs that include more functionality.