From a9937b94e23ad1555ecfad4d57394623d4fb4407 Mon Sep 17 00:00:00 2001 From: Yuri D'Elia Date: Sat, 3 Jul 2021 19:43:27 +0200 Subject: [PATCH] Add initial AVR PROGMEM abstraction in hal::progmem --- src/hal/progmem.h | 26 +++++++++++++++++++++++++ tests/unit/hal/CMakeLists.txt | 1 + tests/unit/hal/progmem/CMakeLists.txt | 8 ++++++++ tests/unit/hal/progmem/test_progmem.cpp | 15 ++++++++++++++ 4 files changed, 50 insertions(+) create mode 100644 src/hal/progmem.h create mode 100644 tests/unit/hal/progmem/CMakeLists.txt create mode 100644 tests/unit/hal/progmem/test_progmem.cpp diff --git a/src/hal/progmem.h b/src/hal/progmem.h new file mode 100644 index 0000000..c67106e --- /dev/null +++ b/src/hal/progmem.h @@ -0,0 +1,26 @@ +#pragma once +#include + +#ifdef __AVR__ +#include +#else +#define PROGMEM // ignored +#endif + +namespace hal { + +/// AVR PROGMEM handling +namespace progmem { + +/// read a 16bit word from PROGMEM +static inline uint16_t pgm_read_word(const uint16_t* addr) +{ +#ifndef __AVR__ + return *addr; +#else + return (uint16_t)::pgm_read_word(addr); +#endif +} + +} // namespace progmem +} // namespace hal diff --git a/tests/unit/hal/CMakeLists.txt b/tests/unit/hal/CMakeLists.txt index ce3c840..eb7d50c 100644 --- a/tests/unit/hal/CMakeLists.txt +++ b/tests/unit/hal/CMakeLists.txt @@ -1 +1,2 @@ add_subdirectory(circular_buffer) +add_subdirectory(progmem) diff --git a/tests/unit/hal/progmem/CMakeLists.txt b/tests/unit/hal/progmem/CMakeLists.txt new file mode 100644 index 0000000..8567655 --- /dev/null +++ b/tests/unit/hal/progmem/CMakeLists.txt @@ -0,0 +1,8 @@ +# define the test executable +add_executable(progmem_tests test_progmem.cpp) + +# define required search paths +target_include_directories(progmem_tests PUBLIC ${CMAKE_SOURCE_DIR}/src/hal) + +# tell build system about the test case +add_catch_test(progmem_tests) diff --git a/tests/unit/hal/progmem/test_progmem.cpp b/tests/unit/hal/progmem/test_progmem.cpp new file mode 100644 index 0000000..abd54f7 --- /dev/null +++ b/tests/unit/hal/progmem/test_progmem.cpp @@ -0,0 +1,15 @@ +#include "catch2/catch.hpp" +#include "progmem.h" + +using Catch::Matchers::Equals; +using hal::progmem::pgm_read_word; + +TEST_CASE("progmem::basic", "[progmem]") { + + // create a PROGMEM array + const uint16_t arr[2] PROGMEM = {0, 1}; + + // ensure it can be read correctly + REQUIRE(0 == pgm_read_word(&arr[0])); + REQUIRE(1 == pgm_read_word(&arr[1])); +}