-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
executable file
·105 lines (100 loc) · 3.1 KB
/
Copy pathmain.cpp
File metadata and controls
executable file
·105 lines (100 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include "bitfilled.hpp"
#include "stm32f4xx_ll_gpio.h"
struct gpio
{
#if USE_BIT_BAND
using mmr_ops = bitfilled::bitband<PERIPH_BASE>;
#else
using mmr_ops = bitfilled::base;
#endif
enum class mode
{
input = 0,
output = 1,
alternate = 2,
analog = 3
};
struct moder : BF_MMREG(std::uint32_t, rw, mmr_ops)
{
BF_COPY_SUPERCLASS(moder)
BF_MMREGBITSET(mode, rw, 2, 16, 0) MODE;
} MODER;
struct otyper : BF_MMREG(std::uint32_t, rw, mmr_ops)
{
BF_COPY_SUPERCLASS(otyper)
BF_MMREGBITSET(std::uint32_t, rw, 1, 16, 0) OTYPE;
} OTYPER;
struct ospeedr : BF_MMREG(std::uint32_t, rw, mmr_ops)
{
BF_COPY_SUPERCLASS(ospeedr)
BF_MMREGBITSET(std::uint32_t, rw, 2, 16, 0) OSPEED;
} OSPEEDR;
struct pupdr : BF_MMREG(std::uint32_t, rw, mmr_ops)
{
BF_COPY_SUPERCLASS(pupdr)
BF_MMREGBITSET(std::uint32_t, rw, 2, 16, 0) PUPD;
} PUPDR;
struct idr : BF_MMREG(std::uint32_t, r, mmr_ops)
{
BF_COPY_SUPERCLASS(idr)
BF_MMREGBITSET(std::uint32_t, r, 1, 16, 0) ID;
} IDR;
struct odr : BF_MMREG(std::uint32_t, rw, mmr_ops)
{
BF_COPY_SUPERCLASS(odr)
BF_MMREGBITSET(std::uint32_t, rw, 1, 16, 0) OD;
} ODR;
struct bsrr : BF_MMREG(std::uint32_t, w, mmr_ops)
{
BF_COPY_SUPERCLASS(bsrr)
BF_MMREGBITSET(std::uint32_t, rw, 1, 16, 0) BS;
BF_MMREGBITSET(std::uint32_t, rw, 1, 16, 16) BR;
} BSRR;
struct lckr : BF_MMREG(std::uint32_t, rw, mmr_ops)
{
BF_COPY_SUPERCLASS(lckr)
BF_MMREGBITSET(std::uint32_t, rw, 1, 16, 0) LCK;
BF_MMREGBITS(std::uint32_t, rw, 16, 16) LCKK;
} LCKR;
struct afr : BF_MMREG(std::uint32_t, rw, mmr_ops)
{
BF_COPY_SUPERCLASS(afr)
BF_MMREGBITSET(std::uint32_t, rw, 4, 8, 0) AFSEL;
} AFR[2];
};
int main()
{
RCC->AHB1ENR = RCC->AHB1ENR | RCC_AHB1ENR_GPIOAEN | RCC_AHB1ENR_GPIODEN;
__DSB();
#if BITFILLED_SOLUTION
auto& GPIO_A = *reinterpret_cast<volatile gpio*>(GPIOA_BASE);
auto& GPIO_D = *reinterpret_cast<volatile gpio*>(GPIOD_BASE);
GPIO_A.MODER.MODE[0] = gpio::mode::input;
GPIO_D.MODER.MODE[12] = gpio::mode::output;
while (true)
{
// only use read-modify-write ODR register if it's done atomically via bitband CPU operation
if constexpr (std::is_same_v<gpio::mmr_ops, bitfilled::bitband<PERIPH_BASE>>)
{
GPIO_D.ODR.OD[12] = GPIO_A.IDR.ID[0];
}
else
{
if (GPIO_A.IDR.ID[0] == 0)
GPIO_D.BSRR.BR[12] = 1;
else
GPIO_D.BSRR.BS[12] = 1;
}
}
#else
LL_GPIO_SetPinMode(GPIOA, LL_GPIO_PIN_0, LL_GPIO_MODE_INPUT);
LL_GPIO_SetPinMode(GPIOD, LL_GPIO_PIN_12, LL_GPIO_MODE_OUTPUT);
while (true)
{
if (LL_GPIO_IsInputPinSet(GPIOA, LL_GPIO_PIN_0))
LL_GPIO_SetOutputPin(GPIOD, LL_GPIO_PIN_12);
else
LL_GPIO_ResetOutputPin(GPIOD, LL_GPIO_PIN_12);
}
#endif
}