Skip to content

Commit d86529d

Browse files
committed
Use standard mcal::gpt timer
1 parent 55dc991 commit d86529d

File tree

15 files changed

+101
-1028
lines changed

15 files changed

+101
-1028
lines changed

ref_app/src/mcal/r7fa4m1ab/mcal_gpt.cpp

Lines changed: 12 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -6,103 +6,26 @@
66
//
77

88
#include <mcal_gpt.h>
9-
#include <mcal_reg.h>
109

11-
#include <util/utility/util_attribute.h>
10+
extern "C" void mcal_gpt_init(void);
1211

13-
namespace
12+
extern "C" void mcal_gpt_init(void)
1413
{
15-
// The one (and only one) system tick.
16-
volatile auto mcal_gpt_system_tick = mcal::gpt::value_type { };
17-
18-
auto gpt_is_initialized() -> bool& ATTRIBUTE(used, noinline);
19-
20-
auto gpt_is_initialized() -> bool&
21-
{
22-
static auto is_init = bool { };
23-
24-
return is_init;
25-
}
14+
mcal::gpt::init(nullptr);
2615
}
2716

2817
extern "C"
29-
auto __vector_16() -> void ATTRIBUTE(signal, used, externally_visible);
30-
31-
auto __vector_16() -> void
18+
auto __sys_tick_handler() -> void
3219
{
33-
// Increment the 64-bit system tick with 0x100, representing 256 [(1/2) us].
34-
// This is basically the roll-over of the 8-bit timer0 at 2MHz each 128us.
20+
using local_arm_sys_tick_type = ::mcal::gpt::arm_sys_tick_type;
21+
using local_value_type = typename local_arm_sys_tick_type::value_type;
22+
23+
// Increment the system tick (having 64-bits) with 0x01000000.
3524

36-
const auto new_tick =
37-
static_cast<mcal::gpt::value_type>
25+
local_arm_sys_tick_type::my_sys_tick_value =
26+
static_cast<local_value_type>
3827
(
39-
mcal_gpt_system_tick + static_cast<std::uint16_t>(UINT16_C(0x100))
28+
local_arm_sys_tick_type::my_sys_tick_value
29+
+ static_cast<std::uint32_t>(UINT32_C(0x01000000))
4030
);
41-
42-
mcal_gpt_system_tick = new_tick;
43-
}
44-
45-
auto mcal::gpt::init(const config_type*) -> void
46-
{
47-
if(!gpt_is_initialized())
48-
{
49-
// Clear the timer0 overflow flag.
50-
mcal::reg::reg_access_static<std::uint8_t, std::uint8_t, mcal::reg::tifr0, static_cast<std::uint8_t>(UINT8_C(0x01))>::reg_set();
51-
52-
// Enable the timer0 overflow interrupt.
53-
mcal::reg::reg_access_static<std::uint8_t, std::uint8_t, mcal::reg::timsk0, static_cast<std::uint8_t>(UINT8_C(0x01))>::reg_set();
54-
55-
// Set the timer0 clock source to f_osc/8 = 2MHz and begin counting.
56-
mcal::reg::reg_access_static<std::uint8_t, std::uint8_t, mcal::reg::tccr0b, static_cast<std::uint8_t>(UINT8_C(0x02))>::reg_set();
57-
58-
// Set the is-initialized indication flag.
59-
gpt_is_initialized() = true;
60-
}
61-
}
62-
63-
auto mcal::gpt::secure::get_time_elapsed() -> mcal::gpt::value_type
64-
{
65-
if(gpt_is_initialized())
66-
{
67-
// Return the system tick using a multiple read to ensure data consistency.
68-
69-
using timer_address_type = std::uint8_t;
70-
using timer_register_type = std::uint8_t;
71-
72-
// Do the first read of the timer0 counter and the system tick.
73-
const auto t0_cnt_1 = mcal::reg::reg_access_static<timer_address_type, timer_register_type, mcal::reg::tcnt0>::reg_get();
74-
const auto sys_tick_1 = mcal_gpt_system_tick;
75-
76-
// Do the second read of the timer0 counter.
77-
const auto t0_cnt_2 = mcal::reg::reg_access_static<timer_address_type, timer_register_type, mcal::reg::tcnt0>::reg_get();
78-
79-
const auto t0_tick_is_consistent = (t0_cnt_2 >= t0_cnt_1);
80-
81-
// Perform the consistency check.
82-
const auto consistent_half_microsecond_tick =
83-
static_cast<value_type>
84-
(
85-
t0_tick_is_consistent ? static_cast<value_type>(sys_tick_1 | t0_cnt_1)
86-
: static_cast<value_type>(mcal_gpt_system_tick | t0_cnt_2)
87-
);
88-
89-
// Scale the timer0 tick to 1MHz and perform a rounding correction.
90-
return
91-
static_cast<value_type>
92-
(
93-
static_cast<std::uint64_t>
94-
(
95-
static_cast<std::uint64_t>
96-
(
97-
static_cast<std::uint64_t>(consistent_half_microsecond_tick)
98-
+ static_cast<std::uint8_t>(UINT8_C(1))
99-
)
100-
/ static_cast<std::uint8_t>(UINT8_C(2))
101-
)
102-
);
103-
}
104-
else
105-
{
106-
return static_cast<value_type>(UINT8_C(0));
107-
}
10831
}

ref_app/src/mcal/r7fa4m1ab/mcal_gpt.h

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
///////////////////////////////////////////////////////////////////////////////
1+
///////////////////////////////////////////////////////////////////////////////
22
// Copyright Christopher Kormanyos 2007 - 2026.
33
// Distributed under the Boost Software License,
44
// Version 1.0. (See accompanying file LICENSE_1_0.txt
@@ -10,18 +10,34 @@
1010

1111
#include <cstdint>
1212

13+
#include <mcal/mcal_gpt_arm_sys_tick.h>
14+
1315
namespace mcal
1416
{
1517
namespace gpt
1618
{
19+
using arm_sys_tick_type = arm_sys_tick<static_cast<std::uint32_t>(UINT16_C(48))>;
20+
1721
using config_type = void;
18-
using value_type = std::uint64_t;
22+
using value_type = typename arm_sys_tick_type::value_type;
23+
24+
inline auto init(const config_type*) -> void;
1925

20-
auto init(const config_type*) -> void;
26+
inline auto init(const config_type*) -> void
27+
{
28+
using local_arm_sys_tick_type = arm_sys_tick_type;
29+
30+
local_arm_sys_tick_type::init();
31+
}
2132

2233
struct secure final
2334
{
24-
static auto get_time_elapsed() -> value_type;
35+
static auto get_time_elapsed() -> value_type
36+
{
37+
using local_arm_sys_tick_type = arm_sys_tick_type;
38+
39+
return static_cast<value_type>(local_arm_sys_tick_type::get_time_elapsed());
40+
}
2541
};
2642
}
2743
}

ref_app/target.vcxproj

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1175,12 +1175,8 @@
11751175
<ClCompile Include="target\micros\nxp_imxrt1062\startup\crt0_init_ram.cpp" />
11761176
<ClCompile Include="target\micros\nxp_imxrt1062\startup\crt1.cpp" />
11771177
<ClCompile Include="target\micros\nxp_imxrt1062\startup\int_vect.cpp" />
1178-
<ClCompile Include="target\micros\r7fa4m1ab\startup\Code\Appli\main.c" />
1178+
<ClCompile Include="target\micros\r7fa4m1ab\startup\Code\Appli\main.cpp" />
11791179
<ClCompile Include="target\micros\r7fa4m1ab\startup\Code\Mcal\clock.cpp" />
1180-
<ClCompile Include="target\micros\r7fa4m1ab\startup\Code\Mcal\SysTickTimer.c" />
1181-
<ClCompile Include="target\micros\r7fa4m1ab\startup\Code\Startup\intvect.c" />
1182-
<ClCompile Include="target\micros\r7fa4m1ab\startup\Code\Startup\Startup.c" />
1183-
<ClCompile Include="target\micros\r7fa4m1ab\startup\Code\StdLib\StdLib.c" />
11841180
<ClCompile Include="target\micros\r7fa4m1ab\startup\crt0.cpp" />
11851181
<ClCompile Include="target\micros\r7fa4m1ab\startup\crt0_init_ram.cpp" />
11861182
<ClCompile Include="target\micros\r7fa4m1ab\startup\crt1.cpp" />
@@ -1275,7 +1271,6 @@
12751271
<ClInclude Include="target\micros\am6254_soc\Code\Startup\Core\a53\core_macros.h" />
12761272
<ClInclude Include="target\micros\am6254_soc\Code\Startup\Core\a53\gic-500.h" />
12771273
<ClInclude Include="target\micros\r7fa4m1ab\startup\Code\Mcal\R7FA4M1AB.h" />
1278-
<ClInclude Include="target\micros\r7fa4m1ab\startup\Code\Mcal\SysTickTimer.h" />
12791274
<ClInclude Include="target\micros\xtensa_esp32_s3\startup\Std\core-isa.h" />
12801275
</ItemGroup>
12811276
<ItemGroup>

ref_app/target.vcxproj.filters

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -349,12 +349,6 @@
349349
<Filter Include="micros\r7fa4m1ab\startup\Code\Mcal">
350350
<UniqueIdentifier>{e9f5404a-7f3f-4aff-bf36-630e2a2822c6}</UniqueIdentifier>
351351
</Filter>
352-
<Filter Include="micros\r7fa4m1ab\startup\Code\Startup">
353-
<UniqueIdentifier>{4dfeed6a-2f54-40ad-a68f-9760ef18d457}</UniqueIdentifier>
354-
</Filter>
355-
<Filter Include="micros\r7fa4m1ab\startup\Code\StdLib">
356-
<UniqueIdentifier>{e5fcad2a-8f5e-4543-bd53-8ab67cf89154}</UniqueIdentifier>
357-
</Filter>
358352
</ItemGroup>
359353
<ItemGroup>
360354
<None Include="target\app\make\app_files.gmk">
@@ -1148,24 +1142,12 @@
11481142
<ClCompile Include="target\micros\r7fa4m1ab\startup\int_vect.cpp">
11491143
<Filter>micros\r7fa4m1ab\startup</Filter>
11501144
</ClCompile>
1151-
<ClCompile Include="target\micros\r7fa4m1ab\startup\Code\Appli\main.c">
1145+
<ClCompile Include="target\micros\r7fa4m1ab\startup\Code\Appli\main.cpp">
11521146
<Filter>micros\r7fa4m1ab\startup\Code\Appli</Filter>
11531147
</ClCompile>
1154-
<ClCompile Include="target\micros\r7fa4m1ab\startup\Code\Mcal\SysTickTimer.c">
1155-
<Filter>micros\r7fa4m1ab\startup\Code\Mcal</Filter>
1156-
</ClCompile>
11571148
<ClCompile Include="target\micros\r7fa4m1ab\startup\Code\Mcal\clock.cpp">
11581149
<Filter>micros\r7fa4m1ab\startup\Code\Mcal</Filter>
11591150
</ClCompile>
1160-
<ClCompile Include="target\micros\r7fa4m1ab\startup\Code\Startup\intvect.c">
1161-
<Filter>micros\r7fa4m1ab\startup\Code\Startup</Filter>
1162-
</ClCompile>
1163-
<ClCompile Include="target\micros\r7fa4m1ab\startup\Code\Startup\Startup.c">
1164-
<Filter>micros\r7fa4m1ab\startup\Code\Startup</Filter>
1165-
</ClCompile>
1166-
<ClCompile Include="target\micros\r7fa4m1ab\startup\Code\StdLib\StdLib.c">
1167-
<Filter>micros\r7fa4m1ab\startup\Code\StdLib</Filter>
1168-
</ClCompile>
11691151
</ItemGroup>
11701152
<ItemGroup>
11711153
<Text Include="target\micros\bcm2835_raspi_b\startup\SD_CARD\PiZero\config.txt">
@@ -1188,9 +1170,6 @@
11881170
<ClInclude Include="target\micros\r7fa4m1ab\startup\Code\Mcal\R7FA4M1AB.h">
11891171
<Filter>micros\r7fa4m1ab\startup\Code\Mcal</Filter>
11901172
</ClInclude>
1191-
<ClInclude Include="target\micros\r7fa4m1ab\startup\Code\Mcal\SysTickTimer.h">
1192-
<Filter>micros\r7fa4m1ab\startup\Code\Mcal</Filter>
1193-
</ClInclude>
11941173
</ItemGroup>
11951174
<ItemGroup>
11961175
<Library Include="target\micros\xtensa_esp32_s3\startup\Std\lib_call0_abi\libc_call0_abi.a">

ref_app/target/micros/r7fa4m1ab/make/r7fa4m1ab_files.gmk

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ FILES_TGT = $(PATH_APP)/mcal/mcal_gcc_cxx_completion \
1717
$(PATH_TGT)/startup/crt1 \
1818
$(PATH_TGT)/startup/int_vect \
1919
$(PATH_APP)/mcal/$(TGT)/mcal_cpu \
20+
$(PATH_APP)/mcal/$(TGT)/mcal_gpt \
2021
$(PATH_APP)/mcal/$(TGT)/mcal_osc \
2122
$(PATH_APP)/mcal/$(TGT)/mcal_port \
2223
$(PATH_APP)/mcal/$(TGT)/mcal_wdg \
2324
$(PATH_TGT)/startup/Code/Appli/main \
24-
$(PATH_TGT)/startup/Code/Mcal/clock \
25-
$(PATH_TGT)/startup/Code/Mcal/SysTickTimer
25+
$(PATH_TGT)/startup/Code/Mcal/clock

ref_app/target/micros/r7fa4m1ab/make/r7fa4m1ab_flags.gmk

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@ SRC_DIR := $(PATH_TGT)/startup/Code
4040
TGT_INCLUDES = -I$(SRC_DIR) \
4141
-I$(SRC_DIR)/Appli \
4242
-I$(SRC_DIR)/Mcal \
43-
-I$(SRC_DIR)/Startup \
44-
-I$(SRC_DIR)/StdLib \
4543
$(INC_PREFIX)$(PATH_APP)/util/STL
4644

4745
TGT_AFLAGS =

ref_app/target/micros/r7fa4m1ab/startup/Code/Appli/main.c

Lines changed: 0 additions & 97 deletions
This file was deleted.

0 commit comments

Comments
 (0)