id: p4mG8UQd3ozMCs4NEqsgLjME8hL7jN40mgr6PyIS
createdBy: 15sg55Ga16CT
dateCreated: 1689064662293
name: STM Programming mbed
meta:
logo: >-
https://global.discourse-cdn.com/business7/uploads/ros/original/2X/2/26a69f2033d48e6099e82d162917b2463ed65fce.jpeg
description: Implementation of a HelloWorld example in C and C++ (STM HAL, mbed)
selfAssign: true
members:
teacher: []
student: []
modules:
- url: https://edrys-labs.github.io/module-markdown-it/index.html
config:
content: >-
# STM Programming mbed
This task is designed to familiarize you with the microcontroller.
To sharpen your understanding of its structure and to review procedural
and object-oriented programming concepts, you will implement a `Hello
World` example that involves the alternating blinking of two LEDs on
different hardware abstraction levels:
+ in C using the STM Hardware Abstraction Layer provided by the
manufacturer STM
+ in C++ with the manufacturer-independent mbedOS Framework.
Version 6.16 of mbedOS will be used for this task.
The microcontroller used is the STM32 L475E-OTO01A1 board.
You can find the manual for this specific board under User Manuals .
studentConfig:
content: ''
teacherConfig:
content: >-
## Welcome for Teachers
> This can be used as a base laboratory to create further and more
elaborate labs with [edrys-Lite](https://edrys-labs.github.io).
> You are currently in the Lobby. If there is a station available you can
switch to it and try out the terminal.
> Otherwise, if you are in teacher-mode you can share a lab by clicking
onto `setting` >> `station` and then by clicking the presented link,
> which is the same as the current link, but only with the word `station`
instead of `classroom`.
stationConfig:
content: ''
showInCustom: lobby
width: full
height: tall
- url: https://edrys-labs.github.io/module-markdown-it/index.html
config:
content: >-
## Task
This example illustrates the use of the DigitalOut class to control a single pin.
1. Experiment with the ability to evaluate whether the electrical
connection (`.is_connected()`) exists.
Determine which types of errors this method can identify.
2. Extend the implementation to LED2 and make both LEDs blink alternately.
Realize the implementation with the more generic classes `DigitalInOut` and `PortInOut`.
What are the differences in relation to the `DigitalOut` class?
In particular, work out an overview of the modes that can be configured for inputs and outputs.
3. mbed is based on the HAL C implementation for STM controllers.
Replace the object-oriented HelloWorld example with a C implementation.
Use the GPIO_InitTypeDef for this.
Take into account that the clock of the port must also be activated!
## Notes
1. LED1 is connected to pin A5 and LED2 to pin B14.
2. Avoid overwriting the entire register when setting and clearing
individual bits! Rather, use the C bit operations.
```c
PORTB |= (1 << Bit); // Setzen
PORTB &=~(1 << Bit); // Löschen
```
## Inital Code
```cpp
#include "mbed.h"
#define BLINKING_RATE 500 //ms
int main()
{
DigitalOut led(LED1);
printf("Programming ARM Controler with mbed is cool\r\n");
while (true) {
led = !led;
thread_sleep_for(BLINKING_RATE);
}
}
```
studentConfig:
content: ''
teacherConfig:
content: |-
## Solution
```cpp
#include "mbed.h"
#define BLINK_DELAY 1000
#define LEDgn_Pin_1 GPIO_PIN_5
#define LEDgn_GPIO_Port_1 GPIOA
#define LEDgn_Pin_2 GPIO_PIN_14
#define LEDgn_GPIO_Port_2 GPIOB
#define GREEN_LED2_MASK 0b100000000000000
int main()
{
printf("Programming ARM GPIO outputs\r\n");
// HAL basierte Implementierung
__HAL_RCC_GPIOA_CLK_ENABLE();
GPIO_InitTypeDef GPIO_InitStruct_1;
GPIO_InitStruct_1.Pin = LEDgn_Pin_1;
GPIO_InitStruct_1.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct_1.Pull = GPIO_NOPULL;
GPIO_InitStruct_1.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(LEDgn_GPIO_Port_1, &GPIO_InitStruct_1);
PortOut greenLED2(PortB, GREEN_LED2_MASK);
//__HAL_RCC_GPIOB_CLK_ENABLE();
//GPIO_InitTypeDef GPIO_InitStruct_2;
//GPIO_InitStruct_2.Pin = LEDgn_Pin_2;
//GPIO_InitStruct_2.Mode = GPIO_MODE_OUTPUT_PP;
//GPIO_InitStruct_2.Pull = GPIO_NOPULL;
//GPIO_InitStruct_2.Speed = GPIO_SPEED_FREQ_LOW;
//HAL_GPIO_Init(LEDgn_GPIO_Port_2, &GPIO_InitStruct_2);
while(true)
{
//HAL_GPIO_WritePin(LEDgn_GPIO_Port_2, LEDgn_Pin_2, GPIO_PIN_SET);
greenLED2 = greenLED2 | GPIO_PIN_14;
thread_sleep_for(BLINK_DELAY);
//HAL_GPIO_WritePin(LEDgn_GPIO_Port_2, LEDgn_Pin_2, GPIO_PIN_RESET);
greenLED2 = greenLED2 &~ GPIO_PIN_14;
HAL_GPIO_WritePin(LEDgn_GPIO_Port_1, LEDgn_Pin_1, GPIO_PIN_SET);
thread_sleep_for(BLINK_DELAY);
HAL_GPIO_WritePin(LEDgn_GPIO_Port_1, LEDgn_Pin_1, GPIO_PIN_RESET);
}
}
```
stationConfig:
content: >-
## Instructions for Station-Sharing
You are currently responsible for sharing a station of this lab.
You have multiple options to share a or your terminal.
Therefor we use the pyxtermjs - terminal server from:
https://github.com/edrys-labs/module-pyxtermjs
### Using Docker
If you haven't done it so far, install
[docker](https://docs.docker.com/engine/install/).
Or, follow one of the instruction-videos for your system:
Install Docker on LinuxInstall Docker on WindowsInstall Docker on MacOS
Then the only thing that is required is to run the following command:
```bash
docker run -it --privileged \
--name edrys_arm_development \
-p 5000:5000 \
-e PYXTERM_CMD=zsh \
--device /dev/ttyACM0 \
-v /media/$USER/DIS_L4IOT:/media/appuser/DIS_L4IOT \
-v /dev/disk/by-id:/dev/disk/by-id \
-v /dev/serial/by-id:/dev/serial/by-id \
-v /run/udev:/run/udev:ro \
crosslab/edrys_pyxtermjs_stm:latest
```
This will download the pyxtermjs terminal-server from docker-hub and run
it in a secure environment.
### Using Python
You can also share your terminal directly via Python, visit the following
project
https://github.com/edrys-labs/module-pyxtermjs
... the easiest way is to perform the following steps:
``` bash
# 1. clone the repository or download the folder manually
git clone https://github.com/edrys-labs/module-pyxtermjs
# 2. install all required sources
pip3 install -r requirements.txt
# 3. run the terminal-server
python3 -m pyxtermjs --cors True --command bash --port 5000
```
showInCustom: station
width: half
height: tall
- url: https://edrys-labs.github.io/module-editor/index.html
config:
runCommand: execute
language: python
theme: light
editorText: |-
#include "mbed.h"
#define BLINKING_RATE 500 //ms
int main()
{
DigitalOut led(LED1);
printf("Programming ARM Controler with mbed is cool\r\n");
while (true) {
led = !led;
thread_sleep_for(BLINKING_RATE);
}
}
showInCustom: station
width: full
height: medium
- url: https://edrys-labs.github.io/module-pyxtermjs/index.html
config: ''
studentConfig: ''
teacherConfig: ''
stationConfig:
server: http://localhost:5000/pty
execute: execute
script: |
echo $CODE | base64 --decode > main.cpp
PROJECT="mbedOS_project"
if [ ! -d "$PROJECT" ]; then
mbed-tools new $PROJECT
fi
mv main.cpp $PROJECT/main.cpp
cd $PROJECT
mbed-tools compile -m DISCO_L475VG_IOT01A -t GCC_ARM -f
cd ..
mbed-tools sterm
enable:
teacher: true
student: true
showInCustom: station
width: full
height: tall
- url: https://edrys-labs.github.io/module-station-stream/index.html
stationConfig:
video: true
audio: false
showInCustom: station
width: half
height: medium