You are on page 1of 28

LABORATORIO DI ARCHITETTURE E

PROGRAMMAZIONE DEI SISTEMI


ELETTRONICI INDUSTRIALI
Laboratory Lesson 2:
- General Purpose I/O
- SysTick

Prof. Luca Benini <luca.benini@unibo.it> Simone Benatti <simone.benatti@unibo.it>


Bojan Milosevic <bojan.milosevic@unibo.it>
Info & Communications
Course Organization
• Check website for announcements, course material:
http://courses.eees.dei.unibo.it/LABARCH/
• Send homework via mail, mandatory subject with group number and
lesson number (with spaces, for easy indexing)
• LABARCH2017 Group 22 Lab 2 (Lab 2 is the lesson number, NOT Lab schedule)
• Please name correctly your files
• NO Attachments with same name or code sent in .txt: (main.c, main.c,
main.c), main1.c.txt
• OK Attachments: main-lab2-es1.c, main-lab2-es2.c, …
• Send replies to questions via mail, always report question and response
(formats: doc, txt, pdf)
• Always contact both tutors!!!
• Simone Benatti simone.benatti@unibo.it
• Bojan Milosevic bojan.milosevic@unibo.it
STM32F4Discovery Kit
Read the Manual(s)!!!
(all available on ST website and on course site)

UM1472 User Manual


User manual for the STM32F4Discovert Kit
(information on board, its components,
connectors, schematics, …)

RM0090 Reference Manual


Reference manual for MCU STM32F407
(detailed information on all peripherals)

PM0241 Programming Manual


Programming manuals for STM32F4
(detailed information on CortexM4 core)
For Help
• At the beginning of every lab session you are provided with all the documents
needed to understand what it is going on in the lab and to successfully do your
homework
• if you don’t know something related to the hardware:
• If it is a component on the board (e.g. a LED) look at the Discovery User Manual
• If it is related to a peripheral of the MCU (e.g. UART, Timers) look at the MCU
Reference manual
• If it is related to the ARM core look in the MCU Programming manual
• If you do not know how to use a peripheral:
• Look at the MCU Reference Manual for the pheripheral description
• Look at the StdPeriph Library and its code examples (download the full version of
the StdPeriph Lib for STM32F4).
Recap
Lab1: IDE & Debug
• Intro to embedded programming
• Crate/compile/debug Eclipse projects
General Purpose I/O (GPIO)
GPIO: Intuition
Take a bit from the physical world and put it in software (or vice versa).
• Input
• Buttons

Somewhere in a Voltage logic level


register of our MCU to be read

• Output
• LEDs

Somewhere in a Voltage logic level


register of our MCU to be set
GPIO: Diagram
Each MCU pin can be used as a General Purpose digital input or
output.

Input: read binary value of


specified pin, (e.g. button
state)

Output: set binary value of


specified pin (e.g. LED, simple
signal trigger)

• GPIO Pins can be configured to trigger an interrupt


• GPIO Pins can not source significant current (typ. 4mA max!)
GPIO in STM32
• The STM32 is well served with general purpose IO pins, having up to
81 bidirectional IO pins with interrupt capability.
• The IO pins are arranged as five ports each having 16 IO lines.

PA [15:0] GPIO port A

PB [15:0] GPIO port B

PC [15:0] GPIO port C

APB2
PD [15:0] GPIO port D

PE [15:0] GPIO port E


GPIO: Block Diagram
GPIO: Turn on a LED
Turning on a LED  one of the most common “hello world” applications for all
MCUs:
• LED driving circuitry is very simple : it consists in a voltage source connected to
a diode in series to a limiting resistor
• Our GPIO acts as a voltage source (0..3.3V) (it can provide a small current too!)

GPIO PIN
OUTPUT CURRENT

MCU
…and let there be light!
GPIO: What
I want to use a GPIO. What do I need to know?
• Which bus the GPIOs are connected to?
• GPIO ports are on the AHB1 bus
• Which Port/PIN the GPIO is on?
• Depends on the application/board
I want to use a LED on the Discovery board. What do I need to know?
• Which PIN the LED is connected to?
• look at your board schematic!
• Four user LEDs: LD3 (orange), LD4 (green), LD5 (red) and LD6 (blue):
• User LD3 orange: connected to the I/O PD13
• User LD4 green: connected to the I/O PD12
• User LD5 red: connected to the I/O PD14
• User LD6 blue: connected to the I/O PD15
• What do I need to do with this GPIO? (input, output, ...)
• I need to write (output)
GPIO: Where

I want to use a GPIO. Where can I gather these information?


• Look at the Discovery User Manual
• Board schematic and information on how the LEDs are connected
• Look at the MCU Reference Manual
• Info on GPIO peripheral, its clock, its BUS, …
• To check electrical characteristics look in the MCU Data Sheet
• Info on electrical specifications and supported current/voltages
GPIO: How
I want to use a GPIO. How can I use this information to actually turn on a LED?
1) Enable the High Speed APB (APB2) peripheral by enabling its clock:
void RCC_AHB1PeriphClockCmd(uint32_t RCC_AHB1Periph_GPIOD, FunctionalState NewState);

• Look at: stm32f4xx_rcc.c


2) Configure the GPIO Port, by declaring and populating:
GPIO_InitTypeDef myConfig
• Look at: stm32f4xx_gpio.h
3) Init the GPIO Port
void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct);
• Look at: stm32f4xx_gpio.c
4) Turn ON the LED
void GPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
• Look at: stm32f4xx_gpio.c
GPIO: Code example
#include "stm32f4xx.h"

int main(void) {
GPIO_InitTypeDef GPIO_InitStructure; /* Enable the GPIO_LED Clock */

/* Configure the GPIO_LED pin and enable AHB1 BUS Port D */


RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);

/*Configure for Pin 12 Port D as output*/


GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD, &GPIO_InitStructure);

GPIO_SetBits(GPIOD,GPIO_Pin_12); /*And light was made*/

while(1);
}
GPIO: LED blink
int main(void) {

. . .

while(1) {
myDelay(1000000);
GPIO_SetBits(GPIOD,GPIO_Pin_15);
myDelay(1000000);
GPIO_ResetBits(GPIOD,GPIO_Pin_15); Check the code example:
} main-lab2-GPIO.c
}

void myDelay(int delay)


{
while (delay > 0)
delay--;

return;
}
GPIO: Use library
#include "stm32f4xx.h"
#include "stm32f4_discovery.h"

int main(void) {
STM_EVAL_LEDInit(LED6)

while(1) {
myDelay(1000000); Check the code example:
STM_EVAL_LEDToggle(LED6); main-lab2-LED.c
}
}

void myDelay(int delay)


{
Tip: to see the code of a library
while (delay > 0) function right-click on it and select
delay--; Open Declaration (or hit F3)

return;
}
SysTick
SysTick: Intro
What if… I need to repeat a periodic task or put a delay in my code?
• Very common problem in all embedded applications!
• Example: blink a LED every second

• Bad engineer solution: wait in a loop for(i = 0; i< 10000;i++ ) {


dummy_counter++;
• Decrement a variable to “waste” time }

• Smart embedded engineer: use timers and interrupt!


• SysTick timer!
SysTick: What
• SystTick  It is part of the ARM Cortex M CORE.
• System timer that can generate periodic interrupts

• Widely used in embedded applications to have an exact timing


• SysTick is used to schedule periodic events (usually configured to fire in the
range of 1 - 100ms)

• When enabled, it periodically generates an interrupt


• Current program state is saved
• The IRQ handler is exectured: SysTick_Handler
SysTick: Setup
I want to schedule a periodic event. How can I use SysTick?

1) We need to setup the SysTick


static __INLINE uint32_t SysTick_Config(uint32_t ticks)
• ticks is the number of ticks between two interrupts
• SystemCoreClock is the number of ticks in 1 sec
• Returns 0 if ok, 1 if something wrong

2) We need to implement the ISR (Interrupt Service Routine)


void SysTick_Handler(void)
• The ISR is always defined in stm32f4xx_it.h (but you con re-define it)
• The name of the ISR for SysTick is SysTick_Handler(void)
• Here is the code executed every ticks ticks
SysTick: Code example
int main(void) {

if (SysTick_Config(SystemCoreClock / 1000)) {
/* Capture error */ ISR executed every 1 ms
while(1);
}
}

void SysTick_Handler(void) {
static u16 counter = 0; static variable
counter++;

if (counter >= 500) { Every 500 increments:


STM_EVAL_LEDToggle(LED6); 500ms period
counter = 0;
}
} Check the code example:
main-lab2-SysTick.c
Weekly assignments
GPIO: Assignments
1.1) Write a code to use all the LEDs on the board. All LEDs should be used together (same
state or blink for all LEDS).
• Do not use STM_EVAL_xxx functions, write the code using only GPIO functions.
• Use Delay function, not SysTick

1.2) Write a code to use all LEDs and use them to compose a blinking pattern. The pattern
can be anything but not all LEDs together (e.g. circular pattern, alternate 2 and 2, different
frequencies for each LED, …).
• Do not use STM_EVAL_xxx functions, write the code using only GPIO functions.
• Use Delay function, not SysTick
GPIO: Questions
Look at the Reference manual, stm32f10x_gpio.h/stm32f4xx_gpio.c (and
google).
The answers can be in Italian or in English

1.a) To use a LED, why we do not configure GPIO_Mode = GPIO_Mode_AN?

1.b) Describe all the available GPIO configuration modalities (GPIO_Mode).

1.c) What is the purpose of the configurations “Input pull-up” and “Input pull-
down”?
SysTick: Assignments

1.3) Write a code to blink two or more LEDs with different patterns/blinking frequency.
• You can use STM_EVAL_xxx functions or write your functions using GPIO functions.
• Use only the SysTick, no Delay functions

1.4) Write a code to blink more than one LED, with different patterns/blinking frequency. All
the blinking frequencies should be below 1Hz.
• You can use STM_EVAL_xxx functions or write your functions using GPIO functions.
• Use only the SysTick, no Delay functions
SysTick: Questions
1.d) What is SystemCoreClock? Where is it defined and what is its value?

1.e) What is SysTick_Handler and what is its purpose?

You might also like