Skip to content

Commit 2b1c027

Browse files
committed
Pi calc works for 100 and 1,000 digits
1 parent d8f9ab8 commit 2b1c027

7 files changed

Lines changed: 69 additions & 64 deletions

File tree

examples/chapter11_07a/readme.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Example Chapter11_07a
2+
## Preemptive Multitasking
3+
4+
This example makes advanced use
5+
of preemptive multitasking scheduling with a highly compley pi-spigot calculation
6+
combined with a blinky-style application. The pi calculation
7+
is performed perpetually and cyclically (and also numerically verified)
8+
in a low-priority background task.
9+
10+
## Application Description
11+
12+
TODO

examples/chapter11_07a/src/app/led/app_led.cpp

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,12 @@ namespace
1919

2020
app_led_timer_type app_led_timer_background;
2121
app_led_timer_type app_led_timer_toggle_led0;
22+
23+
bool result_pi_calc_is_ok { true };
2224
}
2325

24-
extern auto manip_container() -> bool;
26+
extern "C"
27+
auto pi_main() -> int;
2528

2629
extern "C"
2730
void app_led_task_background(void*)
@@ -33,20 +36,21 @@ void app_led_task_background(void*)
3336

3437
for(;;)
3538
{
36-
while((!app_led_timer_background.timeout()))
37-
{
38-
mcal::cpu::nop();
39-
}
39+
const int next_pi_result { pi_main() };
4040

41-
if(!manip_container())
41+
result_pi_calc_is_ok = ((next_pi_result == int { INT8_C(0) }) && result_pi_calc_is_ok);
42+
43+
if(!result_pi_calc_is_ok)
4244
{
43-
for(;;) { ; }
45+
// If the pi calculation is wrong, exercise a hard, visible error
46+
// that stops the perpetual calculation loop.
47+
for(;;)
48+
{
49+
mcal::cpu::nop();
50+
51+
mcal::wdg::secure::trigger();
52+
}
4453
}
45-
app_led_timer_background.start_interval(app_led_timer_type::milliseconds(app_led_tick_type { UINT8_C(50) }));
46-
47-
// Toggle led1 every 50ms.
48-
49-
mcal::led::led1().toggle();
5054
}
5155
}
5256

examples/chapter11_07a/src/app/pi_calc_cfg.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,17 @@
99
#define PI_CALC_CFG_2023_05_09_H
1010

1111
//#define PI_CRUNCH_METAL_STANDALONE_MAIN
12-
//#define PI_CRUNCH_METAL_DISABLE_IOSTREAM
12+
#if !defined(PI_CRUNCH_METAL_DISABLE_IOSTREAM)
13+
#define PI_CRUNCH_METAL_DISABLE_IOSTREAM
14+
#endif
1315

1416
#define PI_CRUNCH_METAL_PI_SPIGOT_USE_100_DIGITS 100
1517
#define PI_CRUNCH_METAL_PI_SPIGOT_USE_1K_DIGITS 1000
1618
#define PI_CRUNCH_METAL_PI_SPIGOT_USE_10K_DIGITS 10000
1719
#define PI_CRUNCH_METAL_PI_SPIGOT_USE_100K_DIGITS 100000
1820

1921
#if !defined(PI_CRUNCH_METAL_PI_SPIGOT_DIGITS)
20-
#define PI_CRUNCH_METAL_PI_SPIGOT_DIGITS PI_CRUNCH_METAL_PI_SPIGOT_USE_10K_DIGITS
22+
#define PI_CRUNCH_METAL_PI_SPIGOT_DIGITS PI_CRUNCH_METAL_PI_SPIGOT_USE_1K_DIGITS
2123
#endif
2224

2325
#endif // PI_CALC_CFG_2023_05_09_H

examples/chapter11_07a/src/app/pi_spigot/pi_spigot.cpp

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828

2929
#include <math/checksums/hash/hash_sha1.h>
3030
#include <math/pi_spigot/pi_spigot.h>
31-
#if !defined(PI_CRUNCH_METAL_STANDALONE_MAIN)
3231
#include <mcal_benchmark.h>
32+
#if !defined(PI_CRUNCH_METAL_STANDALONE_MAIN)
3333
#include <mcal_memory/mcal_memory_sram_array.h>
3434
#endif
3535
#include <util/utility/util_baselexical_cast.h>
@@ -70,9 +70,7 @@ namespace local
7070

7171
auto pi_output_digits10 = static_cast<std::uint32_t>(UINT8_C(0));
7272

73-
#if !defined(PI_CRUNCH_METAL_STANDALONE_MAIN)
7473
using benchmark_port_type = ::mcal::benchmark::benchmark_port_type;
75-
#endif
7674

7775
#if defined(PI_CRUNCH_METAL_STANDALONE_MAIN)
7876
using mcal_sram_uintptr_t = std::uintptr_t;
@@ -94,27 +92,15 @@ namespace local
9492

9593
extern "C"
9694
{
97-
auto mcal_led_toggle() -> void;
98-
9995
auto pi_main() -> int;
100-
101-
auto pi_led_toggle() -> void;
102-
}
103-
104-
extern "C"
105-
auto pi_led_toggle() -> void
106-
{
107-
::mcal_led_toggle();
10896
}
10997

11098
extern auto pi_lcd_progress(const std::uint32_t pi_output_digits10) -> void;
11199
extern auto pi_count_of_calculations() -> std::uint32_t&;
112100

113101
auto pi_main() -> int
114102
{
115-
#if !defined(PI_CRUNCH_METAL_STANDALONE_MAIN)
116103
local::benchmark_port_type::toggle_pin();
117-
#endif
118104

119105
local::pi_spigot_instance.calculate(local::pi_spigot_input.data(), pi_lcd_progress, &local::pi_spigot_hash);
120106

examples/chapter11_07a/src/mcal/avr/mcal_benchmark.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
{
2020
using benchmark_port_type = mcal::port::port_pin<std::uint8_t,
2121
std::uint8_t,
22-
mcal::reg::portd,
23-
UINT8_C(3)>;
22+
mcal::reg::portb,
23+
UINT8_C(2)>;
2424
}
2525
}
2626

examples/chapter11_07a/src/sys/start/sys_start.cpp

Lines changed: 7 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
//
77

88
#include <app/led/app_led.h>
9+
#include <mcal_benchmark.h>
910
#include <mcal/mcal.h>
1011
#include <os/os_task.h>
1112

@@ -15,51 +16,24 @@
1516

1617
ATTRIBUTE(used,noinline) auto main() -> int;
1718

18-
auto manip_container() -> bool;
19-
20-
constexpr auto sram_start_address = static_cast<mcal_sram_uintptr_t>(UINT8_C(0));
21-
22-
using sram_container_type =
23-
mcal::memory::sram::array<std::uint32_t,
24-
std::uint32_t { UINT32_C(1024) * UINT32_C(1024) },
25-
sram_start_address>;
26-
27-
sram_container_type container { };
28-
2919
auto main() -> int
3020
{
3121
// Initialize the microcontroller abstraction layer.
3222
mcal::init();
3323

24+
using benchmark_port_type = ::mcal::benchmark::benchmark_port_type;
25+
26+
benchmark_port_type::set_direction_output();
27+
3428
mcal::spi::sram::mcal_spi_sram_type::init();
3529

3630
// Configure and create the OS tasks. These macros
3731
// also setup the task static resources including the
3832
// task control block structures and task stacks.
3933

40-
OS_TASK_CREATE(app_led_task_background, nullptr, 1U, 64U);
41-
OS_TASK_CREATE(app_led_task_toggle_led0, nullptr, 3U, 64U);
34+
OS_TASK_CREATE(app_led_task_background, nullptr, 1U, 512U);
35+
OS_TASK_CREATE(app_led_task_toggle_led0, nullptr, 3U, 32U);
4236

4337
// Start the OS scheduler (and never return).
4438
OS_TASK_START_SCHEDULER();
4539
}
46-
47-
auto manip_container() -> bool
48-
{
49-
static sram_container_type::pointer my_sram_ptr { container.data() };
50-
static std::uint32_t my_sram_value { };
51-
52-
*my_sram_ptr = my_sram_value;
53-
54-
const bool result_sram_is_ok { (*my_sram_ptr == my_sram_value) };
55-
56-
++my_sram_ptr;
57-
++my_sram_value;
58-
59-
if(my_sram_ptr == container.data() + container.size())
60-
{
61-
my_sram_ptr = container.data();
62-
}
63-
64-
return result_sram_is_ok;
65-
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
rem ///////////////////////////////////////////////////////////////////////////////
2+
rem // Copyright Christopher Kormanyos 2007 - 2025.
3+
rem // Distributed under the Boost Software License,
4+
rem // Version 1.0. (See accompanying file LICENSE_1_0.txt
5+
rem // or copy at http://www.boost.org/LICENSE_1_0.txt)
6+
rem //
7+
8+
echo off
9+
10+
set AVRDUDE=.\avrdude.exe
11+
12+
set HEX=../../../../../examples/chapter09_08/bin/chapter09_08.hex
13+
14+
rem Erase the chip.
15+
echo "Erase the chip."
16+
%AVRDUDE% -c avrisp2 -p m328p -P usb -e
17+
echo.
18+
19+
rem Flash the HEX-file.
20+
echo "Flash the HEX-file."
21+
%AVRDUDE% -c avrisp2 -p m328p -P usb -U flash:w:%HEX%:i
22+
echo.
23+
24+
rem Verify the flash.
25+
echo "Verify the flash."
26+
%AVRDUDE% -c avrisp2 -p m328p -P usb -U flash:v:%HEX%:i
27+
echo.

0 commit comments

Comments
 (0)