Add initial AVR PROGMEM abstraction in hal::progmem

pull/41/head
Yuri D'Elia 2021-07-03 19:43:27 +02:00 committed by DRracer
parent 9efb127acb
commit a9937b94e2
4 changed files with 50 additions and 0 deletions

26
src/hal/progmem.h Normal file
View File

@ -0,0 +1,26 @@
#pragma once
#include <stdint.h>
#ifdef __AVR__
#include <avr/progmem.h>
#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

View File

@ -1 +1,2 @@
add_subdirectory(circular_buffer)
add_subdirectory(progmem)

View File

@ -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)

View File

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