|
5 | 5 | // or copy at http://www.boost.org/LICENSE_1_0.txt) |
6 | 6 | // |
7 | 7 |
|
| 8 | +// Parts of this file originate from the file Mcal/Gpio/led.h in: |
| 9 | +// https://github.com/Chalandi/Baremetal_TI_AM6254_multicore_nosdk |
| 10 | + |
8 | 11 | #ifndef MCAL_LED_AM6254_SOC_2025_08_04_H |
9 | 12 | #define MCAL_LED_AM6254_SOC_2025_08_04_H |
10 | 13 |
|
|
13 | 16 | #include <util/utility/util_time.h> |
14 | 17 |
|
15 | 18 | #include <cstdint> |
16 | | - |
17 | | - #define PADCFG_CTRL0_CFG0_PADCONFIG3 UINT32_C(0x000F400C) |
18 | | - #define PADCFG_CTRL0_CFG0_PADCONFIG4 UINT32_C(0x000F4010) |
19 | | - #define PADCFG_CTRL0_CFG0_PADCONFIG5 UINT32_C(0x000F4014) |
20 | | - #define PADCFG_CTRL0_CFG0_PADCONFIG6 UINT32_C(0x000F4018) |
21 | | - |
22 | | - #define GPIO_DIR01 UINT32_C(0x00600010) |
23 | | - #define GPIO_OUT_DATA01 UINT32_C(0x00600014) |
24 | | - #define GPIO_SET_DATA01 UINT32_C(0x00600018) |
25 | | - #define GPIO_CLR_DATA01 UINT32_C(0x0060001C) |
26 | | - |
27 | | - #define LED_1 6 |
28 | | - #define LED_2 5 |
29 | | - #define LED_3 4 |
30 | | - #define LED_4 3 |
31 | | - |
32 | | - #define LED_INIT() do{ \ |
33 | | - *(volatile uint32_t*)(PADCFG_CTRL0_CFG0_PADCONFIG3) &= ~((uint32_t)1ul <<21); \ |
34 | | - *(volatile uint32_t*)(PADCFG_CTRL0_CFG0_PADCONFIG4) &= ~((uint32_t)1ul <<21); \ |
35 | | - *(volatile uint32_t*)(PADCFG_CTRL0_CFG0_PADCONFIG5) &= ~((uint32_t)1ul <<21); \ |
36 | | - *(volatile uint32_t*)(PADCFG_CTRL0_CFG0_PADCONFIG6) &= ~((uint32_t)1ul <<21); \ |
37 | | - *(volatile uint32_t*)(GPIO_DIR01) &= ~((uint32_t)0x78ul); \ |
38 | | - *(volatile uint32_t*)(GPIO_CLR_DATA01) |= 0x78; \ |
39 | | - *(volatile uint32_t*)(GPIO_OUT_DATA01) &= ~((uint32_t)0x78ul); \ |
40 | | - }while(0) |
41 | | - |
42 | | - #define LED_ON(x) do{*(volatile uint32_t*)(GPIO_SET_DATA01) |= (1ul << x); *(volatile uint32_t*)(GPIO_OUT_DATA01) |= (1ul << x);}while(0) |
43 | | - #define LED_OFF(x) do{*(volatile uint32_t*)(GPIO_CLR_DATA01) |= (1ul << x); *(volatile uint32_t*)(GPIO_OUT_DATA01) &= (uint32_t)(~(uint32_t)(1ul << x));}while(0) |
44 | | - |
45 | | - #define LED_1_ON() LED_ON(LED_1) |
46 | | - #define LED_2_ON() LED_ON(LED_2) |
47 | | - #define LED_3_ON() LED_ON(LED_3) |
48 | | - #define LED_4_ON() LED_ON(LED_4) |
49 | | - |
50 | | - #define LED_1_OFF() LED_OFF(LED_1) |
51 | | - #define LED_2_OFF() LED_OFF(LED_2) |
52 | | - #define LED_3_OFF() LED_OFF(LED_3) |
53 | | - #define LED_4_OFF() LED_OFF(LED_4) |
| 19 | + #include <type_Traits> |
54 | 20 |
|
55 | 21 | namespace mcal |
56 | 22 | { |
57 | 23 | namespace led |
58 | 24 | { |
| 25 | + constexpr unsigned LED_1 { UINT8_C(6) }; |
| 26 | + constexpr unsigned LED_2 { UINT8_C(5) }; |
| 27 | + constexpr unsigned LED_3 { UINT8_C(4) }; |
| 28 | + constexpr unsigned LED_4 { UINT8_C(3) }; |
| 29 | + |
59 | 30 | template<typename VoidClass> |
60 | 31 | class led_am6254_soc_base : public mcal::led::led_boolean_state_base |
61 | 32 | { |
|
72 | 43 | base_class_type::toggle(); |
73 | 44 | } |
74 | 45 |
|
| 46 | + static constexpr std::uint32_t PADCFG_CTRL0_CFG0_PADCONFIG3 { UINT32_C(0x000F400C) }; |
| 47 | + static constexpr std::uint32_t PADCFG_CTRL0_CFG0_PADCONFIG4 { UINT32_C(0x000F4010) }; |
| 48 | + static constexpr std::uint32_t PADCFG_CTRL0_CFG0_PADCONFIG5 { UINT32_C(0x000F4014) }; |
| 49 | + static constexpr std::uint32_t PADCFG_CTRL0_CFG0_PADCONFIG6 { UINT32_C(0x000F4018) }; |
| 50 | + |
| 51 | + static constexpr std::uint32_t GPIO_DIR01 { UINT32_C(0x00600010) }; |
| 52 | + static constexpr std::uint32_t GPIO_OUT_DATA01 { UINT32_C(0x00600014) }; |
| 53 | + static constexpr std::uint32_t GPIO_SET_DATA01 { UINT32_C(0x00600018) }; |
| 54 | + static constexpr std::uint32_t GPIO_CLR_DATA01 { UINT32_C(0x0060001C) }; |
| 55 | + |
75 | 56 | private: |
76 | | - static const bool is_init; |
| 57 | + static const volatile bool is_init; |
| 58 | + |
| 59 | + using local_void_class = VoidClass; |
| 60 | + |
| 61 | + static_assert(std::is_same<local_void_class, void>::value, "Error: The template parameter must be of type void."); |
| 62 | + |
| 63 | + #if defined(__GNUC__) |
| 64 | + __attribute__((noinline,used)) |
| 65 | + #endif |
| 66 | + static auto LED_INIT() -> bool |
| 67 | + { |
| 68 | + *reinterpret_cast<volatile std::uint32_t*>(PADCFG_CTRL0_CFG0_PADCONFIG3) &= static_cast<std::uint32_t>(~(UINT32_C(1) << 21U)); |
| 69 | + *reinterpret_cast<volatile std::uint32_t*>(PADCFG_CTRL0_CFG0_PADCONFIG4) &= static_cast<std::uint32_t>(~(UINT32_C(1) << 21U)); |
| 70 | + *reinterpret_cast<volatile std::uint32_t*>(PADCFG_CTRL0_CFG0_PADCONFIG5) &= static_cast<std::uint32_t>(~(UINT32_C(1) << 21U)); |
| 71 | + *reinterpret_cast<volatile std::uint32_t*>(PADCFG_CTRL0_CFG0_PADCONFIG6) &= static_cast<std::uint32_t>(~(UINT32_C(1) << 21U)); |
| 72 | + *reinterpret_cast<volatile std::uint32_t*>(GPIO_CLR_DATA01) |= UINT32_C(0x78); |
| 73 | + *reinterpret_cast<volatile std::uint32_t*>(GPIO_OUT_DATA01) &= static_cast<std::uint32_t>(~(UINT32_C(0x78))); |
| 74 | + *reinterpret_cast<volatile std::uint32_t*>(GPIO_DIR01) &= static_cast<std::uint32_t>(~(UINT32_C(0x78))); |
| 75 | + |
| 76 | + return true; |
| 77 | + } |
77 | 78 | }; |
78 | 79 |
|
79 | 80 | template<typename VoidClass> |
80 | | - const bool led_am6254_soc_base<VoidClass>::is_init = []() { LED_INIT(); return true; }(); |
| 81 | + const volatile bool led_am6254_soc_base<VoidClass>::is_init { LED_INIT() }; |
81 | 82 |
|
82 | | - template<const int LED_ID> |
| 83 | + template<const unsigned LED_ID> |
83 | 84 | class led_am6254_soc final : public mcal::led::led_am6254_soc_base<void> |
84 | 85 | { |
| 86 | + private: |
| 87 | + using base_class_type = led_am6254_soc_base<void>; |
| 88 | + |
| 89 | + static constexpr auto local_led_id() noexcept -> unsigned { return LED_ID; } |
| 90 | + |
85 | 91 | public: |
86 | 92 | led_am6254_soc() noexcept = default; |
87 | 93 |
|
88 | 94 | ~led_am6254_soc() override = default; |
89 | 95 |
|
90 | 96 | auto toggle() -> void override |
91 | 97 | { |
92 | | - using base_class_type = led_am6254_soc_base<void>; |
93 | | - |
94 | | - if(base_class_type::state_is_on()) |
95 | | - { |
96 | | - LED_OFF(LED_ID); |
97 | | - } |
98 | | - else |
99 | | - { |
100 | | - LED_ON(LED_ID); |
101 | | - } |
| 98 | + ((base_class_type::state_is_on()) ? LED_OFF() : LED_ON()); |
102 | 99 |
|
103 | 100 | base_class_type::toggle(); |
104 | 101 | } |
105 | | - }; |
106 | 102 |
|
107 | | - template<const int LED_ID> |
108 | | - static void main_core_worker(void) |
109 | | - { |
110 | | - using timer_type = util::timer<std::uint64_t>; |
111 | | - |
112 | | - using led_type = led_am6254_soc<LED_ID>; |
| 103 | + static auto main_core_worker() -> void |
| 104 | + { |
| 105 | + using local_timer_type = util::timer<std::uint64_t>; |
| 106 | + using local_tick_type = typename local_timer_type::tick_type; |
| 107 | + using local_led_type = led_am6254_soc<local_led_id()>; |
113 | 108 |
|
114 | | - led_type my_led; |
| 109 | + local_led_type my_led { }; |
115 | 110 |
|
116 | | - timer_type led_timer(timer_type::seconds(1U)); |
| 111 | + local_timer_type led_timer(local_timer_type::seconds(local_tick_type { UINT8_C(1) })); |
117 | 112 |
|
118 | | - my_led.toggle(); |
| 113 | + my_led.toggle(); |
119 | 114 |
|
120 | | - for(;;) |
121 | | - { |
122 | | - while(!led_timer.timeout()) { asm volatile("nop"); } |
| 115 | + for(;;) |
| 116 | + { |
| 117 | + while(!led_timer.timeout()) { asm volatile("nop"); } |
123 | 118 |
|
124 | | - my_led.toggle(); |
| 119 | + my_led.toggle(); |
125 | 120 |
|
126 | | - led_timer.start_interval(timer_type::seconds(1U)); |
| 121 | + led_timer.start_interval(local_timer_type::seconds(local_tick_type { UINT8_C(1) })); |
| 122 | + } |
127 | 123 | } |
128 | | - } |
| 124 | + |
| 125 | + private: |
| 126 | + auto LED_ON () noexcept -> void { *reinterpret_cast<volatile std::uint32_t*>(base_class_type::GPIO_SET_DATA01) |= static_cast<std::uint32_t>(UINT32_C(1) << local_led_id()); *reinterpret_cast<volatile std::uint32_t*>(base_class_type::GPIO_OUT_DATA01) |= static_cast<std::uint32_t>(UINT32_C(1) << local_led_id()); } |
| 127 | + auto LED_OFF() noexcept -> void { *reinterpret_cast<volatile std::uint32_t*>(base_class_type::GPIO_CLR_DATA01) |= static_cast<std::uint32_t>(UINT32_C(1) << local_led_id()); *reinterpret_cast<volatile std::uint32_t*>(base_class_type::GPIO_OUT_DATA01) &= static_cast<std::uint32_t>(~static_cast<std::uint32_t>(UINT32_C(1) << local_led_id())); } |
| 128 | + }; |
129 | 129 | } // namespace led |
130 | 130 | } // namespace mcal |
131 | 131 |
|
|
0 commit comments