STM32BareMetalMiniCookbookV1 5
STM32BareMetalMiniCookbookV1 5
Bare-Metal
Embedded
C Drivers
Mini
Cookbook
TABLE OF CONTENT
Introduction(Must Read) O1
Introduction(Must do) 03
SYSTICK-Introduction 07
SYSTICK-Documentation 09
TIMERS-Uses 11
TIM-Registers 15
Analog-To-Digital
21
Converter(ADC)
UART-Registers 26
Our Courses 31
INTRODUCTION : MUST READ
microcontrollers.
https://www.st.com/resource/en/reference_manual/
dm00119316-stm32f411xc-e-advanced-arm-based32-
bit-mcus-stmicroelectronics.pdf
https://developer.arm.com/documentation/dui0553/
latest/
01
This mini-cookbook is brought to you by
https://study.embeddedexpert.io/
02
INTRODUCTION : MUST DO
Watch this lesson collection for setup and live coding of this
particular task. Link : Access Mini-Cook book Companion
Video Lessons
03
TASK: WRITE A BARE-METAL DRIVER TO TOGGLE GPIOA
PIN 5
#include "stm32f4xx.h"
int main(void) {
Reference manual
RCC->AHB1ENR |=(1U<<0);
while (1) {
Reference manual
GPIOA->ODR |=(1U<<5);
Reference manual
GPIOA->ODR &=~(1U<<5);
04
TASK: WRITE A BARE-METAL DRIVER TO READ INPUT
FROM PC13 TO TOGGLE OUTPUT AT PA5
#include "stm32f4xx.h"
int main(void) {
of AHB1ENR*/
RCC->AHB1ENR |=(1U<<0);
Reference manual
RCC->AHB1ENR |=(1U<<2);
Reference manual
GPIOA->MODER |=(1U<<10);
GPIOC->MODER |=(0U<<26);
Reference manual
GPIOC->MODER |=(0U<<27);
while (1) {
Reference manual
if(GPIOA->IDR & (1U<<13)) {
05
/*6. Turn off PA5 by writing 0 to bit5 of ODR*/
GPIOA->ODR &=~(1U<<5);
}
else {
06
SYSTICK-Introduction
1 Found in all ARM Cortex-Microcontrollers, regardless of silicon
manufacturer.
SYSTICK-Counting
1 Counts from initial value down to zero
0xFFFFFF
SYSTICK-Registers
1 Systick Current Value Register (STCVR)
07
SYSTICK-Count value computations
Compute the delay achieved by loading 11 in the Systick Reload Value
Written as :
Systick->LOAD = 10
*written in the CMSIS standardt
*we write 10 although we want 11 because
the counter starts counting from 0
Solution
System clock = 16MHz = 16 000 000 cycles/second.
1
= 62.5us = 62.5 x10-9 s
16000000
08
SYSTICK-Delay computation
Compute N value for achieving a 1ms delay given SYSCLK as 16MHz
Solution
1ms = 0.001s
N
Delay =
SYSCLK
N
0.001 =
16 000 000
N = 0.001 x 16 000 000
N = 16000
SYSTICK-Documentation
Because SYSTICK is a core Cortex-M peripheral its references
Link Here
09
TASK: WRITE BARE-DRIVER FOR THE SYSTICK TIMER TO
#include "stm32f4xx.h"
int main(void) {
Reference manual
RCC->AHB1ENR |=(1U<<0);
Reference manual
GPIOA->MODER |=(1U<<10);
while (1) {
/* 6. Wait for flag to be set if COUNT flag is See Page 4-33 Cortex-
10
/*7. Toggle green LED */
GPIOA->ODR &=~(1U<<5);
TIMER-Uses
1 Counting Events
2 Creating Delays
11
TIMER-STM32 Timers
TIMER-Registers
12
TIMER-Clock Pre-scaling
SYSCLK
AHB Prescaler
Divide by : 1,2,4,8...512
TIMx_PSC TIMx_PSC
to TIM_CNT to TIM_CNT
13
TIMER-Clock Pre-scaling
1 Update Event
2 Period
3 Up counter
4 Down counter
14
TIMER- Computing Update Event
Timer
clock
Update Event =
(Prescaler+1)(Period+1)
Example
Let
48 000 000
Update Event = = 2Hz = 21 s = 0.5s
(47999+1)(499+1)
15
TIM-Registers
16
TIM-Registers
1 Prescaler (PSC)
-Prescaler value is put here
Example
Example
Example
Example
17
5 Capture/Compare Register (CCR1, CCR2, CCR4, CCR4)
-One capture/compare register for each of the 4 channels.
Example
timestamp =TIM2->CCR1; // read captured value
Example
TIM2->CCMR1 = 0x41; // set CH1 to capture at every edge
Example
TIM2->CCER = 1; // Enable channel 1
18
TASK: WRITE A BARE-METAL TIMER DRIVER TO TOGGLE
PA5 AT A 1HZ RATE.
#include "stm32f4xx.h"
int main(void) {
Reference manual
RCC->AHB1ENR |=(1U<<0);
Reference manual
GPIOA->MODER |=(1U<<10);
TIM2->CNT = 0;
Reference manual
19
/*7. Enable TIM2*/
See Page-351 of RM0383
TIM2->CR1 = 1;
Reference manual
while (1) {
} Reference manual
20
ANALOG-TO-DIGTAL
CONVERTER (ADC)
21
ADC Independent Modes
22
ADC-Registers
1 Control Register 1 (CR1)
-For setting the ADC resolution.
Example
ADC1->CR2 = 1 ; // Enable channel 1
Example
ADC1->SQR1 = 0 ; // set sequence length to 1
23
7 Sequence Register 2 (SQR2)
-Setting conversions sequence rank.
#include "stm32f4xx.h"
int main(void) {
24
/*5. Set start of conversion sequence to ch
See Page-235 of RM0383
1*/
Reference manual
ADC1->SQR3 = 1;
25
UART-Registers
1 Baud Rate Register (BRR)
- For setting uart baud rate.
Example
USART2->BRR = 0x0683; //9600 baud operating at 16 MHZ
Example
USART2->CR1 = 0x0008; // 8-bit data, enable TX
USART2->CR1 |= 0x2000; //enable uart2
Example
USART2->CR2 = 0x0000; // 1 stop bit
Example
USART2->CR3 = 0x0000; // no flow control
26
5 Status Register (SR)
-Checking the state of rx and tx buffers.
Example
USART2->SR & 0x0080; // check if tx buffer
void uart2_init(void);
int uart2_write(int c);
int uart2_read(void);
int main(void) {
int number;
char sentence[100];
uart2_init();
printf("Hello from STM32!! \n\r");
while (1) {
printf("Please enter a number: ");
scanf("%d", &number);
printf("the number entered is: %d\r\n", number);
printf("please type a character string: ");
gets(sentence);
printf("the character string entered is: ");
puts(sentence);
printf("\r\n");
}
}
27
/* Initialize USART2 to transmit at
9600 Baud */
void uart2_init (void) {
/*4. Enable alternate function for PA2, PA3 */ See Page-156 of RM0383
GPIOA->MODER |= 0x00A0; Reference manual
28
/*9. Enable USART2 */
See Page-551 of RM0383
USART2->CR1 |= 0x2000; Reference manual
}
29
int fgetc(FILE *f) {
int c;
/*1. read the character from console */
/*2. If '\r', after it is echoed, a '\n' is appended*/
if (c == '\r') {
uart2_write(c); /* echo */
c = '\n';
}
/*3. Echoe*/
uart2_write(c);
30
Our Courses
31
ARM Assembly Bare-Metal C/C++ Learning
Programming Mastery Pack Path
1. Modern Bare-Metal Embedded-C
Covering ARM Systems Design,
From Ground Up (STM32F4) : Old and
Architecture and Practical Assembly
New Edition
Source Included
32
Embedded Systems IoT Embedded Wifi Bare-
Learning Path Metal Development From
3 Courses | 36+ Courses | Complete
Ground Up™
Source Included
Welcome to the Embedded WIFI
1. Bluetooth Low Energy (BLE) from Bare-Metal Development From
Ground Up .... Ground Up™ course..
CubeIDE
1. Embedded Build ..
2. Embedded Systems..
33
Realtime Operating Systems STM32F3 Bare-Metal
(RTOS) Learning Path Peripheral Drivers
4 Courses | 47+ hours | Complete Development
Source Included
Welcome to the STM32F3 Bare-Metal
1. FreeRTOS from Ground Up
Peripheral Drivers Programming
2. Arm Assembly Programming.. course ..
Welcome to the ARM GNU Assembly This course is the beginner course
Programming From Ground Up™ of a 3 course learning path
course .. teaching you how ..
34
Embedded Systems Bare- Embedded Systems
Metal Programming Ground STM32 HAL APIs Driver
Up™ (STM32F4) Development
The goal of this course is to teach you Welcome to the Embedded Systems
how to navigate the microcontroller STM32 Hardware Abstraction Layer
reference manual .. (HAL) ..
35
Mastering STM32CubeMX 5 {C++}Build Your Own
and CubeIDE - Embedded Realtime OS (RTOS) From
Systems Ground Up™ on ARM
Hello Welcome to the Mastering Welcome to the {C++} Build Your Own
STM32CubeMX 5 and CubeIDE course
RTOS From Ground Up™ course.
36
Embedded Systems Cellular Modern Bare-Metal
Firmware Development(GSM)
37
Deep Learning on ARM Build Your Own RealTime
Processors - From Ground Up™
38
Embedded Systems Object- Practical Low Cost Bare-
Oriented Programming in C Metal Bluetooth
Welcome to the Embedded Systems Development
Object-Oriented Programming course.
Hello, welcome to the “Practical Low
This course is for anyone seeking to Cost Bare-Metal Bluetooth
improve their .. Development” course. ..
39
Firmware Version Control with USB Host Development
Git from Ground Up™ Essential Training with
We shall delve into the world of Version CubeMX
Control Systems (VCS). We start by This course complements our USB
introducing .. Device Development Essential
Training, offering a holistic ..
40
Flash Memory and EEPROM Advanced Digital Signal
Drivers: A Hands-On Guide for Processing on ARM
Embedded Engineers Processors
Are you an Embedded Engineer looking Welcome to the “Advanced Digital
to master the fundamentals of memory Signal Processing on ARM
storage and .. Processors” course. Whether ..
41
Embedded Local Database Embedded Azure Cloud <>
Storage: MySQL Python Gateway
Communication
Enter the world of embedded database
Step into the fascinating world of
storage in our new course, "Embedded
Microsoft Azure with this practical
Local Database Storage: MySQL".
course designed to empower you to
42
Custom Cloud <> Python Embedded Audio Solutions:
Gateway Communication
Developing an Audio Media
Are you ready to redefine the future
Player
with IoT without the complexity of Welcome to the "Embedded Audio
wireless radios? Media Player" course, your quickest
way to developing a complete
Up™
you an embedded systems enthusiast Get ready to dive into the exciting
or a professional engineer looking to world of bootloader development
level up your skills and with this beginner level course of our
43
HAPPY
CODING