«

»

Août 26

Robotics 101 : Basic GPIOs

First Steps

Now that we are fully able to use chibiOS programs on the board let’s understand them. Let’s start by the electronic ‘hello world’ which is: blinking a LED. To do so we are going to use General Purpose Input Output (GPIOs)

First let’s get all the components ready :

Then let’s connect everything :
You will need to connect the component as in the picture (be careful for the LED ! it has a precise orientation). Doing so you are creating a ground row (all the pins in the row are at GND), then you are connecting the + pin (cathode) of the LED to PC1, the resistor to the – pin (anode) of the LED and the resistor to GND.
circuit1
You can find which pin of the LED is the cathode by checking which one is the longer, the longer one is the cathode, the shorter one the anode. (LEDs only allow current to flow in one direction).
circuit2

Here is the source code for the project:

Code overview :

(I assume you are already a bit familiar with the C language) :

#include “hal.h”
#include “ch.h”

In these two lines we include the Hardware Abstraction Layer and the ChibiOS core libraries. The first one manages all the drivers (let’s say it allows to control the hardware of the board, that’s one of ChibiOS specificity), the second one manages the ChibiOS kernel (multithreading and others).

int main(void)
creates the “main” function, it will be launched when you start the board. It’s where you are supposed to write your code.

halInit(); // initialize hardware abstraction layer
chSysInit(); // initialize ChibiOS kernel

Initializes ChibiOS’s kernel and Hardware Abstraction Layer (HAL).

palSetPadMode(GPIOC, 1, PAL_MODE_OUTPUT_PUSHPULL);
It tells the chip what is the pin (pad) mode (how it is gonna be used). Here it is used as an output doing pushpull.
You have to do this because a pin can either be used in many different ways, so you have to specify at the initialization how you intend to use it.

palSetPadMode takes three inputs, the gpio port, the pin number and the pal mode. For the simple PUSHPULL use, the first parameter is simply GPIO followed by the channel (A,B,C …) of the pin, the second one is the number of the pin as you can see here :
The last one is simply PAL_MODE_OUTPUT_PUSHPULL or PAL_MODE_INTPUT_PUSHPULL depending on whether you want your pad to be an input or an output.

Here the PUSHPULL mode means that it can either supply current to, or absorb current from, a connected load (it is the most basic mode, in facts it means that you can either put the output at VCC or GND), and OUTPUT obviously means that it is an output.
Pal in palSetPadMode, is a prefix which means Port Abstraction Layout, it’s what chibiOS uses to control the ports use. SetPadMode means that we are going to set the Pad (=pin) mode (=use/function).
Eventually PAL_MODE_OUTPUT_PUSHPULL is a constant defined in the PAL. (you can recognize constants as they are written in uppercase).

Now that the initialization is done let’s see the core of the program :

while(1){
palSetPad(GPIOC, 1); // set GPIOC 1 high
chThdSleepMilliseconds(500); // sleep for 500ms
palClearPad(GPIOC, 1); // set GPIOC 1 low
chThdSleepMilliseconds(500);
}

The first line is simply an endless loop, so that the led will keep on blinking, if you don’t know what a loop is here is the wikipedia article.

There are three new functions :

  • palSetPad(port, pad); It sets the output of the pin (of the port) at the logical high level (the pin stays this way). It simply takes the port and pad of the target as parameters.
  • palClearPad(port, pad); It sets the output of the pin (of the port) at the logical low level (the pin stays this way). It simply takes the port and pad of the target as parameters.
  • chThdSleepMilliseconds(duration); puts the program to sleep (= pauses it) for duration*millisecond. (that’s not technically true, we will see the correct explanation in the thread tutorial)

So this code simply sets the pad high, waits 500ms, sets it back to low, waits 500ms over and over again.

Hardware review :

The components are the following:

  • A breadboard, also known as a solderless board, which allows you to prototype easily without having to waste your components. The electrical diagram of the breadboard is the following :
    Breadboard
  • A LED, it is a piece of silicon which lights up when enough current passes through its pins. The higher the current, the brighter the LED is. In order to limit the current passing through the LED (if there is too much current passing through the LED it will burn) we simply add a current limiting resistor : thanks to Ohm’s law it will limit the current passing through the LED.

That’s about it ! Now you know how to create a basic program and how to use basic GPIO.

Next part here.

Felix.

Once again feel free to use the comment section.