Skip to content

Commit

Permalink
Improve code snippets. Re-work linker stuff. Add new 8-bit target.
Browse files Browse the repository at this point in the history
  • Loading branch information
ckormanyos committed Feb 1, 2018
1 parent dda0db3 commit 6cfd3f1
Show file tree
Hide file tree
Showing 43 changed files with 2,078 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <cstdint>
#include <iostream>

// Both template parameters have default types.
template<typename x_type = std::uint8_t, // OK.
typename y_type = std::uint16_t> // OK
class point
Expand Down
2 changes: 2 additions & 0 deletions code_snippets/chapter05/chapter05_05-003_template_point.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include <cstdint>
#include <iostream>

// Only the second template parameter
// has a default types.
template<typename x_type,
typename y_type = std::uint16_t> // OK
class point
Expand Down
3 changes: 3 additions & 0 deletions code_snippets/chapter05/chapter05_06-002_template_point.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ class point
my_y(y) { }
};

// A partial specialization of the point
// class with x-axis having type std::uint8_t.

template<typename y_type>
class point<std::uint8_t, y_type>
{
Expand Down
6 changes: 3 additions & 3 deletions examples/chapter02_02/crt0.s
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ isr_vectors:
__my_startup:

eor r1, r1
out 0x3f, r1 ; SREG
out 0x3f, r1 ; sreg
ldi r28,lo8(__initial_stack_pointer)
ldi r29,hi8(__initial_stack_pointer)
out 0x3E, r29 ; SPH
out 0x3D, r28 ; SPL
out 0x3e, r29 ; sph
out 0x3d, r28 ; spl

; Initialize the rom-to-ram data
call __do_copy_data
Expand Down
1 change: 1 addition & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Supported Targets in the Reference Application
The reference application supports the following targets:
* Win32 in both Release and Debug modes
* ATMEL(R) AVR(R) Atmega328P
* ATMEL(R) AVR(R) Atmega2560
* ST Microelectronics(R) STM32F100 ARM(R) Cortex(TM)-M3
* ST Microelectronics(R) STM32L152 ARM(R) Cortex(TM)-M3
* ST Microelectronics(R) STM32F407 ARM(R) Cortex(TM)-M4
Expand Down
11 changes: 9 additions & 2 deletions ref_app/ref_app.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2013
VisualStudioVersion = 12.0.31101.0
# Visual Studio 15
VisualStudioVersion = 15.0.27130.2026
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ref_app", "ref_app.vcxproj", "{C8B59726-9319-45C3-8F11-F9F388FB6A2C}"
EndProject
Expand All @@ -12,6 +12,7 @@ Global
Debug|Win32 = Debug|Win32
Release|Win32 = Release|Win32
target am335x|Win32 = target am335x|Win32
target atmega2560|Win32 = target atmega2560|Win32
target avr|Win32 = target avr|Win32
target bcm2835_raspi_b|Win32 = target bcm2835_raspi_b|Win32
target pic32mx795fx|Win32 = target pic32mx795fx|Win32
Expand All @@ -28,6 +29,7 @@ Global
{C8B59726-9319-45C3-8F11-F9F388FB6A2C}.Release|Win32.ActiveCfg = Release|Win32
{C8B59726-9319-45C3-8F11-F9F388FB6A2C}.Release|Win32.Build.0 = Release|Win32
{C8B59726-9319-45C3-8F11-F9F388FB6A2C}.target am335x|Win32.ActiveCfg = Release|Win32
{C8B59726-9319-45C3-8F11-F9F388FB6A2C}.target atmega2560|Win32.ActiveCfg = target atmega2560|Win32
{C8B59726-9319-45C3-8F11-F9F388FB6A2C}.target avr|Win32.ActiveCfg = Release|Win32
{C8B59726-9319-45C3-8F11-F9F388FB6A2C}.target bcm2835_raspi_b|Win32.ActiveCfg = Release|Win32
{C8B59726-9319-45C3-8F11-F9F388FB6A2C}.target pic32mx795fx|Win32.ActiveCfg = Release|Win32
Expand All @@ -41,6 +43,8 @@ Global
{30CE370B-40F3-4BCD-8986-64AAFF8971BD}.Release|Win32.ActiveCfg = target avr|Win32
{30CE370B-40F3-4BCD-8986-64AAFF8971BD}.target am335x|Win32.ActiveCfg = target am335x|Win32
{30CE370B-40F3-4BCD-8986-64AAFF8971BD}.target am335x|Win32.Build.0 = target am335x|Win32
{30CE370B-40F3-4BCD-8986-64AAFF8971BD}.target atmega2560|Win32.ActiveCfg = target atmega2560|Win32
{30CE370B-40F3-4BCD-8986-64AAFF8971BD}.target atmega2560|Win32.Build.0 = target atmega2560|Win32
{30CE370B-40F3-4BCD-8986-64AAFF8971BD}.target avr|Win32.ActiveCfg = target avr|Win32
{30CE370B-40F3-4BCD-8986-64AAFF8971BD}.target avr|Win32.Build.0 = target avr|Win32
{30CE370B-40F3-4BCD-8986-64AAFF8971BD}.target bcm2835_raspi_b|Win32.ActiveCfg = target bcm2835_raspi_b|Win32
Expand All @@ -63,4 +67,7 @@ Global
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {B366D59E-C623-4FD4-BF1E-302144448A9E}
EndGlobalSection
EndGlobal
417 changes: 414 additions & 3 deletions ref_app/ref_app.vcxproj

Large diffs are not rendered by default.

69 changes: 69 additions & 0 deletions ref_app/ref_app.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@
<Filter Include="src\math\functions">
<UniqueIdentifier>{62976de0-9569-4a72-af6f-e6c489a1dfb0}</UniqueIdentifier>
</Filter>
<Filter Include="src\mcal\atmega2560">
<UniqueIdentifier>{8fef6100-47d4-49d0-9a34-3e060e206eea}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="src\app\led\app_led.cpp">
Expand Down Expand Up @@ -493,6 +496,33 @@
<ClCompile Include="src\mcal\stm32l152\mcal_wdg.cpp">
<Filter>src\mcal\stm32l152</Filter>
</ClCompile>
<ClCompile Include="src\mcal\atmega2560\mcal_cpu.cpp">
<Filter>src\mcal\atmega2560</Filter>
</ClCompile>
<ClCompile Include="src\mcal\atmega2560\mcal_gpt.cpp">
<Filter>src\mcal\atmega2560</Filter>
</ClCompile>
<ClCompile Include="src\mcal\atmega2560\mcal_irq.cpp">
<Filter>src\mcal\atmega2560</Filter>
</ClCompile>
<ClCompile Include="src\mcal\atmega2560\mcal_led.cpp">
<Filter>src\mcal\atmega2560</Filter>
</ClCompile>
<ClCompile Include="src\mcal\atmega2560\mcal_osc.cpp">
<Filter>src\mcal\atmega2560</Filter>
</ClCompile>
<ClCompile Include="src\mcal\atmega2560\mcal_port.cpp">
<Filter>src\mcal\atmega2560</Filter>
</ClCompile>
<ClCompile Include="src\mcal\atmega2560\mcal_pwm.cpp">
<Filter>src\mcal\atmega2560</Filter>
</ClCompile>
<ClCompile Include="src\mcal\atmega2560\mcal_spi.cpp">
<Filter>src\mcal\atmega2560</Filter>
</ClCompile>
<ClCompile Include="src\mcal\atmega2560\mcal_wdg.cpp">
<Filter>src\mcal\atmega2560</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="src\math\calculus\derivative.h">
Expand Down Expand Up @@ -1302,6 +1332,45 @@
<ClInclude Include="src\math\functions\math_functions_bessel.h">
<Filter>src\math\functions</Filter>
</ClInclude>
<ClInclude Include="src\mcal\atmega2560\mcal_benchmark.h">
<Filter>src\mcal\atmega2560</Filter>
</ClInclude>
<ClInclude Include="src\mcal\atmega2560\mcal_cpu.h">
<Filter>src\mcal\atmega2560</Filter>
</ClInclude>
<ClInclude Include="src\mcal\atmega2560\mcal_gpt.h">
<Filter>src\mcal\atmega2560</Filter>
</ClInclude>
<ClInclude Include="src\mcal\atmega2560\mcal_irq.h">
<Filter>src\mcal\atmega2560</Filter>
</ClInclude>
<ClInclude Include="src\mcal\atmega2560\mcal_led.h">
<Filter>src\mcal\atmega2560</Filter>
</ClInclude>
<ClInclude Include="src\mcal\atmega2560\mcal_osc.h">
<Filter>src\mcal\atmega2560</Filter>
</ClInclude>
<ClInclude Include="src\mcal\atmega2560\mcal_port.h">
<Filter>src\mcal\atmega2560</Filter>
</ClInclude>
<ClInclude Include="src\mcal\atmega2560\mcal_pwm.h">
<Filter>src\mcal\atmega2560</Filter>
</ClInclude>
<ClInclude Include="src\mcal\atmega2560\mcal_reg.h">
<Filter>src\mcal\atmega2560</Filter>
</ClInclude>
<ClInclude Include="src\mcal\atmega2560\mcal_reg_access.h">
<Filter>src\mcal\atmega2560</Filter>
</ClInclude>
<ClInclude Include="src\mcal\atmega2560\mcal_ser.h">
<Filter>src\mcal\atmega2560</Filter>
</ClInclude>
<ClInclude Include="src\mcal\atmega2560\mcal_spi.h">
<Filter>src\mcal\atmega2560</Filter>
</ClInclude>
<ClInclude Include="src\mcal\atmega2560\mcal_wdg.h">
<Filter>src\mcal\atmega2560</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="src\util\STL\algorithm">
Expand Down
26 changes: 26 additions & 0 deletions ref_app/src/mcal/atmega2560/mcal_benchmark.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
///////////////////////////////////////////////////////////////////////////////
// Copyright Christopher Kormanyos 2014.
// Distributed under the Boost Software License,
// Version 1.0. (See accompanying file LICENSE_1_0.txt
// or copy at http://www.boost.org/LICENSE_1_0.txt)
//

#ifndef MCAL_BENCHMARK_2014_04_16_H_
#define MCAL_BENCHMARK_2014_04_16_H_

#include <cstdint>
#include <mcal_port.h>
#include <mcal_reg.h>

namespace mcal
{
namespace benchmark
{
typedef mcal::port::port_pin<std::uint8_t,
std::uint8_t,
mcal::reg::portd,
UINT8_C(3)> benchmark_port_type;
}
}

#endif // MCAL_BENCHMARK_2014_04_16_H_
18 changes: 18 additions & 0 deletions ref_app/src/mcal/atmega2560/mcal_cpu.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
///////////////////////////////////////////////////////////////////////////////
// Copyright Christopher Kormanyos 2007 - 2018.
// Distributed under the Boost Software License,
// Version 1.0. (See accompanying file LICENSE_1_0.txt
// or copy at http://www.boost.org/LICENSE_1_0.txt)
//

#include <mcal_cpu.h>
#include <mcal_osc.h>
#include <mcal_port.h>
#include <mcal_wdg.h>

void mcal::cpu::init()
{
mcal::wdg::init(nullptr);
mcal::port::init(nullptr);
mcal::osc::init(nullptr);
}
81 changes: 81 additions & 0 deletions ref_app/src/mcal/atmega2560/mcal_cpu.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
///////////////////////////////////////////////////////////////////////////////
// Copyright Christopher Kormanyos 2007 - 2018.
// Distributed under the Boost Software License,
// Version 1.0. (See accompanying file LICENSE_1_0.txt
// or copy at http://www.boost.org/LICENSE_1_0.txt)
//

#ifndef MCAL_CPU_2009_02_14_H_
#define MCAL_CPU_2009_02_14_H_

#if defined(__cplusplus)
#include <cstdint>
#else
#include <stdint.h>
#endif

#include <avr/pgmspace.h>

#if defined(__cplusplus)
namespace mcal
{
namespace cpu
{
void init();

inline void post_init() { }

inline void nop() { asm volatile("nop"); }
}
}
#endif

#if defined(__cplusplus)
extern "C"
{
#endif

inline uint8_t mcal_cpu_read_program_memory_byte(const uint8_t* pointer_to_program_memory)
{
const uint16_t memory_address = (uint16_t) pointer_to_program_memory;

return pgm_read_byte(memory_address);
}

inline uint16_t mcal_cpu_read_program_memory_word (const uint8_t* pointer_to_program_memory)
{
const uint16_t memory_address = (uint16_t) pointer_to_program_memory;

return pgm_read_word(memory_address);
}

inline uint32_t mcal_cpu_read_program_memory_dword(const uint8_t* pointer_to_program_memory)
{
const uint16_t memory_address = (uint16_t) pointer_to_program_memory;

return pgm_read_dword(memory_address);
}

inline uint64_t mcal_cpu_read_program_memory_qword(const uint8_t* pointer_to_program_memory)
{
const uint16_t memory_address = (uint16_t) pointer_to_program_memory;

uint64_t result_of_read;

*(((uint8_t*) &result_of_read) + 0U) = pgm_read_byte(memory_address + 0U);
*(((uint8_t*) &result_of_read) + 1U) = pgm_read_byte(memory_address + 1U);
*(((uint8_t*) &result_of_read) + 2U) = pgm_read_byte(memory_address + 2U);
*(((uint8_t*) &result_of_read) + 3U) = pgm_read_byte(memory_address + 3U);
*(((uint8_t*) &result_of_read) + 4U) = pgm_read_byte(memory_address + 4U);
*(((uint8_t*) &result_of_read) + 5U) = pgm_read_byte(memory_address + 5U);
*(((uint8_t*) &result_of_read) + 6U) = pgm_read_byte(memory_address + 6U);
*(((uint8_t*) &result_of_read) + 7U) = pgm_read_byte(memory_address + 7U);

return result_of_read;
}

#if defined(__cplusplus)
}
#endif

#endif // MCAL_CPU_2009_02_14_H_
80 changes: 80 additions & 0 deletions ref_app/src/mcal/atmega2560/mcal_gpt.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
///////////////////////////////////////////////////////////////////////////////
// Copyright Christopher Kormanyos 2007 - 2015.
// Distributed under the Boost Software License,
// Version 1.0. (See accompanying file LICENSE_1_0.txt
// or copy at http://www.boost.org/LICENSE_1_0.txt)
//

#include <mcal_gpt.h>
#include <mcal_reg_access.h>

namespace
{
// The one (and only one) system tick.
volatile mcal::gpt::value_type system_tick;

bool& gpt_is_initialized() __attribute__((used, noinline));

bool& gpt_is_initialized()
{
static bool is_init = bool();

return is_init;
}
}

extern "C"
void __vector_23() __attribute__((section(".isr_handlers"),signal, used, externally_visible));

void __vector_23()
{
// Increment the 32-bit system tick with 0x80, representing 128 microseconds.
system_tick += static_cast<std::uint8_t>(0x80U);
}

void mcal::gpt::init(const config_type*)
{
if(gpt_is_initialized() == false)
{
// Clear the timer0 overflow flag.
mcal::reg::access<std::uint8_t, std::uint8_t, mcal::reg::tifr0, 0x01U>::reg_set();

// Enable the timer0 overflow interrupt.
mcal::reg::access<std::uint8_t, std::uint8_t, mcal::reg::timsk0, 0x01U>::reg_set();

// Set the timer0 clock source to f_osc/8 = 2MHz and begin counting.
mcal::reg::access<std::uint8_t, std::uint8_t, mcal::reg::tccr0b, 0x02U>::reg_set();

// Set the is-initialized indication flag.
gpt_is_initialized() = true;
}
}

mcal::gpt::value_type mcal::gpt::secure::get_time_elapsed()
{
if(gpt_is_initialized())
{
// Return the system tick using a multiple read to ensure data consistency.

typedef std::uint8_t timer_address_type;
typedef std::uint8_t timer_register_type;

// Do the first read of the timer0 counter and the system tick.
const timer_register_type tim0_cnt_1 = mcal::reg::access<timer_address_type, timer_register_type, mcal::reg::tcnt0>::reg_get();
const mcal::gpt::value_type sys_tick_1 = system_tick;

// Do the second read of the timer0 counter.
const timer_register_type tim0_cnt_2 = mcal::reg::access<timer_address_type, timer_register_type, mcal::reg::tcnt0>::reg_get();

// Perform the consistency check.
const mcal::gpt::value_type consistent_microsecond_tick
= ((tim0_cnt_2 >= tim0_cnt_1) ? mcal::gpt::value_type(sys_tick_1 | std::uint8_t(tim0_cnt_1 >> 1U))
: mcal::gpt::value_type(system_tick | std::uint8_t(tim0_cnt_2 >> 1U)));

return consistent_microsecond_tick;
}
else
{
return mcal::gpt::value_type(0U);
}
}
Loading

0 comments on commit 6cfd3f1

Please sign in to comment.