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])); +}