0% found this document useful (0 votes)
38 views41 pages

Chap04 Poo

la POO en stm32

Uploaded by

Chemlali Rayen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views41 pages

Chap04 Poo

la POO en stm32

Uploaded by

Chemlali Rayen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

Object Oriented Programming

For EMBEDDED SYSTEMS

Emir DAMERGI
INSAT - GL3 - 2023

Emir DAMERGI – INSAT - 2023


OOP For EMBEDDED SYSTEMS

Advantages of OOP:

1 - Transparent Programming through Encapsulation

2 - Flexibility through polymorphism

3- Reuse of code through inheritance

Emir DAMERGI – INSAT - 2023 2


OOP For EMBEDDED SYSTEMS
Case Study: Configuring GPIO Pins as Inputs
Using SOC Firmware
(ST FWL)

Class Object Parameter(s)


Using OOP

PinAsInput button (PA_5, Floating); //or PA5


if (button == 0) {

Emir DAMERGI – INSAT - 2023 3


OOP For EMBEDDED SYSTEMS
Case Study: Configuring GPIO Pins as Inputs

PinAsInput button (PA_5, Floating);


OOP if (button == 0) {

Wrapper
layer Wrapper Functions

HAL
(ST
FWL)

HARDWARE
Emir DAMERGI – INSAT - 2023 4
OOP For EMBEDDED SYSTEMS

Case Study:
Configuring
GPIO Pins as
Inputs

Emir DAMERGI – INSAT - 2023 5


OOP For EMBEDDED SYSTEMS

Case Study:
Configuring
GPIO Pins as
Inputs

Emir DAMERGI – INSAT - 2023 6


OOP For EMBEDDED SYSTEMS Case Study: Configuring GPIO Pins as Inputs

PinModeNames.h
File containig OO Pins/Modes
definitions (Platform Independent)
Pin Represention:

0xYZ

with

Y for the GPIO


0 : GPIOA
1 : GPIOB
2: GPIOC
3: ….

Z for the Pin


0 : Pin 0
1 : Pin 1
…..

F : Pin 15
Emir DAMERGI – INSAT - 2023 7
OOP For EMBEDDED SYSTEMS Case Study: Configuring GPIO Pins as Inputs

PinAsInput.h (Class definition)

 Default Constructor : without mode


specification (Floating)
In most cases, the Flotaing mode is choosen
for the pins configured as inputs.

Constructor with pin and mode

parameters

Read Pin State (IDR)


Emir DAMERGI – INSAT - 2023 8
OOP For EMBEDDED SYSTEMS

Case Study:
Configuring
GPIO Pins as
Inputs

Emir DAMERGI – INSAT - 2023 9


OOP For EMBEDDED SYSTEMS Case Study: Configuring GPIO Pins as Inputs

STM32_GPIO_Wrapper.c -1-

Emir DAMERGI – INSAT - 2023 10


OOP For EMBEDDED SYSTEMS Case Study: Configuring GPIO Pins as Inputs

STM32_GPIO_Wrapper.c -2-

gpio_init_in ( pin, Floating )

Static (function scope limited


to its object file)
}

Function to translate
PinName (OO) to GPIOX (Firmware)
Emir DAMERGI – INSAT - 2023 11
OOP For EMBEDDED SYSTEMS Case Study: Configuring GPIO Pins as Inputs

STM32_GPIO_Wrapper.c -3- Adapt OO definitions to ST HAL –


PinName to GPIO_PIN

PinName pin

int gpioPIN = 1<<(pin & 0x0F);

Emir DAMERGI – INSAT - 2023 12


OOP For EMBEDDED SYSTEMS Case Study: Configuring GPIO Pins as Inputs

STM32_GPIO_Wrapper.c -4- Adapt OO definitions to ST HAL –


PinName to GPIOx

Emir DAMERGI – INSAT - 2023 13


OOP For EMBEDDED SYSTEMS Case Study: Configuring GPIO Pins as Inputs

PinNames.h
STM32_GPIO_Wrapper.c -5- Adapt OO definitions to ST Library
Modes – PinName to GPIOx

Emir DAMERGI – INSAT - 2023 14


OOP For EMBEDDED SYSTEMS Case Study: Configuring GPIO Pins as Inputs

STM32_GPIO_Wrapper.c -6-

Emir DAMERGI – INSAT - 2023 15


OOP For EMBEDDED SYSTEMS Case Study: Configuring GPIO Pins as Inputs

STM32_GPIO_Wrapper.c -7-

APB2ENR

. 4 3 2 1 0
. C B A

Emir DAMERGI – INSAT - 2023 16


OOP For EMBEDDED SYSTEMS Case Study: Configuring GPIO Pins as Inputs

ShortHand notation

To read the pin state (from IDR):


int state = ObjectName.read()
if (state = = ..)

A shorthand for read:

If (ObjectName = = ..)

Each Object Invocation -> call to read


Emir DAMERGI – INSAT - 2023 17
OOP For EMBEDDED SYSTEMS Case Study: Configuring GPIO Pins as Inputs

PinAsInput.h

Constructors

Methods

Operator (=)
ShortHand
Emir DAMERGI – INSAT - 2023 18
OOP For EMBEDDED SYSTEMS Case Study: Configuring GPIO Pins as Inputs

Main.cpp

Toggle the Led (PA5) each time the

Pushbutton (PC13) is pressed.

- Inputs are programmed using

OO (PinAsInput Class)

- Outputs are programmed using

Low Level (Register) Access.

Emir DAMERGI – INSAT - 2023 19


OOP For EMBEDDED SYSTEMS Case Study: Configuring GPIO Pins as Inputs

Main.cpp

With PinAsOutput Class for


Pins in output mode

2
Emir DAMERGI – INSAT - 2023
0
THE MBED ECOSYSTEM

Emir DAMERGI – INSAT - 2023 21


The MBED ECOSYSTEM for ARM processors based SOCs

mbed OS is an operating system for IoT devices : energy constraints, connectivity,


security, device management.

Emir DAMERGI – INSAT - 2023 22


The MBED ECOSYSTEM for ARM processors based SOCs

Choosing a Hardware Platform


(hundreds available from many
SOC manufacturers).

For the developper, the HW


details are transparent.
The code portability is ensured
without changes or minor
changes.

Emir DAMERGI – INSAT - 2023 23


The MBED ECOSYSTEM for ARM processors based SOCs
Each SOC manufacturer (ST Microelectronics, Texas Instruments, FreeScale, etc.
provides his own HAL library and the corresponding Wrapper Layer.

User Code : Objets (classes instances)

Object Oriented Library (ARM)


MBED

Wrapper Wrapper Wrapper


Layer ST Layer TI Layer ..

HAL ST HAL TI HAL …

HW ST HW TI HW …
Emir DAMERGI – INSAT - 2023 24
The MBED ECOSYSTEM for ARM processors based SOCs

Example1: Toggle the Led connected to Pin PC9 when Button PA0 pressed.

#include "mbed.h"
Classes defining
Pins configured DigitalIn mybutton (USER_BUTTON); //USER_BUTTON = PA0
in digital input
resp outupt DigitalOut myled (LED1); //LED1 = PC9

int main() {
Objects with pin
number as while(1) {
parameter if (mybutton == 0) // Button is pressed
{
myled = !myled; // Toggle the LED state
wait(0.2); // 200 ms
}
}
}
Emir DAMERGI – INSAT - 2023 25
The MBED ECOSYSTEM for ARM processors based SOCs

Example2: Serial Communication.

Class defining #include "mbed.h"


Serial
Serial My_USART (PinTX, PinRX); // PinTX et PinRX are
communiction
functions // defined pins
int main() {
Object Char C;
My_USART.Baud (115200); //Baudrate property
My_USART.printf ("Hello World!\n"); // Send data with Serial Interface
while(1) {
C = My_USART.getc() ; // Receive one caracter
}
}
Emir DAMERGI – INSAT - 2023 26
THE ARDUINO ECOSYSTEM

Toute cette partie (Arduino) ne fait partie du cours


“Architectures matérielles et programmation bas niveau

Emir DAMERGI – INSAT - 2023 27


A l’origine, une marque qui couvre

des cartes matériellement libres sur lesquelles

se trouve un microcontrôleur

d'architecture Atmel AVR comme

l'Atmega328p

Emir DAMERGI – INSAT - 2023 28


L’IDE Arduino permettant de programmer les

cartes Arduino :

• Simple à programmer

• Un nombre important de librairies

• Langage C++

Emir DAMERGI – INSAT - 2023 29


D’autres constructeurs de SOCs (Microcontrôleurs) ont développé des extensions permettant de
rendre leurs produits compatibles avec Arduino IDE :

• STM32 de ST Microelectronics : à base de processeurs ARM Cortex-M)

• ESP8266 de Espressif : à base de processseurs Tensilica Xtensa LX106,


80 MHz, 64 à 96 Ko de RAM, 512K à 4M de Flash,
Wifi integré, jusqu’à 16 pins GPIO)

• ESP32 de Espressif: à base de processseurs Tensilica Xtensa LX6,


160 à 240 Mhz, 512K RAM, jusqu’à 8 M Flash,
Wifi et BlueTooth intégrés.

Emir DAMERGI – INSAT - 2023 30


Pour ajouter ces SOCs à l’ IDE Arduino (1):

http://arduino.esp8266.com/stable/package_esp8266com_index.json

https://dl.espressif.com/dl/package_esp32_index.json

https://raw.githubusercontent.com/stm32duino/BoardManagerFiles/master/STM32/package_stm_index.json

Emir DAMERGI – INSAT - 2023 31


Pour ajouter ces SOCs à l’ IDE Arduino (2):

Emir DAMERGI – INSAT - 2023 32


Arduino
Digital Input /output

33
Digital I/O : GPIO Pins & Ardunio Numbering

Arduino Connector
When programming SoCs Input/output operations, the
used pin(s) must be identified.
For Arduino IDE, the connector pinout identifiers can be

GPIO4
used D0..D15 (Digital) and A0..A5 (Analog)

#define GreenLed D12

For Pins that are not tied to the Arduino Connector, the
ardunio pin numbers must be known. For each installed
Example for Arduino Pin
board, a pins_arduino.h file contains the pin mapping.
numbering (ESP8266)

#define PushButton 4

It is possible to use Arduino pin Numbers for connector pins.

Emir DAMERGI – INSAT - 2023 34


Digital I/O: Arduino Programming

Pin Configuration Write a Value to Pin Read Pin Value

Arduino Connector
Example:
An application that continously :
Tests if the PushButton is pressed (Low level).
 If yes : The Led is Turned ON
GPIO4
 No: the Led is Turend OFF

Emir DAMERGI – INSAT - 2023 35


Digital I/O: Arduino Programming
Arduino Connector

GPIO4

Example:
An application that continously :
Tests if the PushButton is pressed
(Low level).
 If yes : The Led is Turned ON
 No: the Led is Turend OFF

Emir DAMERGI – INSAT - 2023 36


Arduino
UART Communications

3
7
USART:

Synchronous

Asynchronous

STM32F4: USART1, USART2, USART3, UART4, UART5,Emir


USART6
DAMERGI – INSAT - 2023 38
UART Communications : Serial Object
Serial Object Creation and Communication parameters configuration:

If config is not specified, the default value is used :


SERIAL_8N1: 8 data bits, No Parity bits and 1 Stop bit

Setup() { Generally, the UART Pins are specified and


pre-configured.
Serial.begin (115200); //initialize Serial Port with 115200 bits/sec

while ( !Serial) {//wait for Serial port Initialization The default Serial object names in arduino are:
} Serial (the UART connected to the USB/Serial

} Converter)
Serial1 for the second UART, Serial2, etc…

Emir DAMERGI – INSAT - 2023 39


UART Communications: Sending Data

The Arduino Default Total


Buffer size is 64 bytes.
If more bytes are sent, the
write operation can be blocked. Examples:
Serial.print (" HELLO !!") ;
int X=17; Example:
Serial.print (X);  17 Char Msg[ ] = " HELLO !!";
Serial.print (X,BIN);  10001 Serial.print (Msg , 7) ;
Serial.print(X,HEX);  11

Float Accel = 35,26548


Serial.print (Accel);  35,26
Serial.print (Accel,4);  35,2654

4
Emir DAMERGI – INSAT - 2023
0
UART Communications : Receiving Data

const byte numChars = 32;


char receivedChars [numChars];
Byte nbr;

while (Serial.available() > 0 && nbr < numChars) {

char rc;
rc = Serial.read();
receivedChars[nbr] = rc;
nbr++;
}

Emir DAMERGI – INSAT - 2023 41

You might also like