Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 88fc1a8

Browse files
committed
Added Classes vs Struct example
1 parent c7985d3 commit 88fc1a8

File tree

3 files changed

+159
-1
lines changed

3 files changed

+159
-1
lines changed

Baremetal/Cpp/Minimal_Cpp/CMakeLists.txt

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,20 @@ set(CMAKE_TOOLCHAIN_FILE "toolchain.cmake")
88
project(${PROJECT_NAME})
99

1010
set(USER_PROJECT_TARGET "${PROJECT_NAME}.elf")
11-
add_executable(${USER_PROJECT_TARGET} src/main.cpp src/isr_vector.c)
11+
12+
# ~~~
13+
# Functions vs Namespaces
14+
# add_executable(
15+
# ${USER_PROJECT_TARGET}
16+
# # src/main.c
17+
# src/main.cpp src/isr_vector.c)
18+
# ~~~
19+
20+
# Classes vs Structs
21+
add_executable(
22+
${USER_PROJECT_TARGET}
23+
# src/class/main.c
24+
src/class/main.cpp src/isr_vector.c)
1225

1326
# TODO, Add more files here
1427

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#include "stm32l475xx.h"
2+
#include <stdint.h>
3+
4+
typedef struct {
5+
GPIO_TypeDef *port;
6+
uint8_t pin;
7+
} Blink;
8+
9+
void init(Blink *blink);
10+
void set(Blink *blink);
11+
void reset(Blink *blink);
12+
void spin_delay(Blink *blink, uint32_t delay);
13+
14+
int main(void) {
15+
Blink blink = {GPIOA, 5};
16+
17+
init(&blink);
18+
set(&blink);
19+
spin_delay(&blink, 1000 * 1000);
20+
21+
while (1) {
22+
reset(&blink);
23+
spin_delay(&blink, 1000 * 1000);
24+
25+
set(&blink);
26+
spin_delay(&blink, 1000 * 1000);
27+
}
28+
29+
return 0;
30+
}
31+
32+
void init(Blink *blink) {
33+
RCC->AHB2ENR |= RCC_AHB2ENR_GPIOAEN;
34+
blink->port->BRR |= (1 << blink->pin); // Reset the pin here
35+
36+
// Set the mode
37+
blink->port->MODER &= ~(3 << (blink->pin * 2));
38+
blink->port->MODER |= (1 << (blink->pin * 2)); // 01 00 00 00 00 00
39+
40+
// Check these registers
41+
blink->port->OTYPER &= ~(1 << blink->pin); // set to 0
42+
blink->port->OSPEEDR &= ~(3 << (blink->pin * 2));
43+
blink->port->PUPDR &= ~(3 << (blink->pin * 2));
44+
}
45+
46+
void set(Blink *blink) {
47+
// Set the pin here
48+
blink->port->BSRR |= (1 << blink->pin);
49+
}
50+
51+
void reset(Blink *blink) {
52+
blink->port->BRR = (1 << blink->pin); // Reset
53+
}
54+
55+
void spin_delay(Blink *blink, uint32_t delay) {
56+
(void)blink;
57+
58+
while (delay) {
59+
__NOP();
60+
--delay;
61+
}
62+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/**
2+
*
3+
* Having a constructor (and destructor) increases the binary size by 4 bytes
4+
* over its C counter part
5+
* However code generation for both C and C++ strictly results in the same
6+
* binary output if (constructor and destructors arent used)
7+
*
8+
* C with Struct without args = 1152
9+
* C++ with Classes without args (without constructor) = 1152
10+
* C++ with Classes without args (with constructor) = 1156
11+
*
12+
* C with Struct with args = 1188
13+
* C++ with Classes with args (with constructor) = 1188
14+
*
15+
*/
16+
#include "stm32l475xx.h"
17+
#include <cstdint>
18+
19+
class Blink {
20+
public:
21+
Blink(GPIO_TypeDef *port, std::uint8_t pin);
22+
// ~Blink();
23+
24+
void init();
25+
void set();
26+
void reset();
27+
void spin_delay(std::uint32_t delay);
28+
29+
private:
30+
GPIO_TypeDef *port;
31+
std::uint8_t pin;
32+
};
33+
34+
Blink::Blink(GPIO_TypeDef *port, std::uint8_t pin) : port(port), pin(pin) {}
35+
// Blink::~Blink() {}
36+
37+
void Blink::init() {
38+
RCC->AHB2ENR |= RCC_AHB2ENR_GPIOAEN;
39+
this->port->BRR |= (1 << this->pin); // Reset the pin here
40+
41+
// Set the mode
42+
this->port->MODER &= ~(3 << (this->pin * 2));
43+
this->port->MODER |= (1 << (this->pin * 2)); // 01 00 00 00 00 00
44+
45+
// Check these registers
46+
this->port->OTYPER &= ~(1 << this->pin); // set to 0
47+
this->port->OSPEEDR &= ~(3 << (this->pin * 2));
48+
this->port->PUPDR &= ~(3 << (this->pin * 2));
49+
}
50+
51+
void Blink::set() {
52+
// Set the pin here
53+
this->port->BSRR |= (1 << this->pin);
54+
}
55+
56+
void Blink::reset() {
57+
this->port->BRR = (1 << this->pin); // Reset
58+
}
59+
60+
void Blink::spin_delay(std::uint32_t delay) {
61+
while (delay) {
62+
__NOP();
63+
--delay;
64+
}
65+
}
66+
67+
int main() {
68+
Blink blink{GPIOA, 5};
69+
70+
blink.init();
71+
blink.set();
72+
blink.spin_delay(1000 * 1000);
73+
74+
while (1) {
75+
blink.reset();
76+
blink.spin_delay(1000 * 1000);
77+
78+
blink.set();
79+
blink.spin_delay(1000 * 1000);
80+
}
81+
82+
return 0;
83+
}

0 commit comments

Comments
 (0)