avr-something

Something something AVR side…
git clone git://henryandlizzy.uk/avr-something
Log | Files | Refs | README

commit 0ae92ac6f34c8bbc1d059809f4a144c5d3445803
parent 16cdc9385b1c3ff1516b54186ba29d70d38c2dec
Author: Henry Wilson <m3henry@googlemail.com>
Date:   Tue, 13 Feb 2018 20:29:24 +0000

Put load from flash memory in a class

Diffstat:
Mlcd.cpp | 16++++++----------
Mmakefile | 2+-
Mutils.h | 45+++++++++++++++++++++++++++++++++------------
3 files changed, 40 insertions(+), 23 deletions(-)

diff --git a/lcd.cpp b/lcd.cpp @@ -161,13 +161,12 @@ struct auto g = drawSequence(); - uint8_t const* bytePtr = fonts::mash[c - ' ']; + auto bytePtr = flashPtr(fonts::mash[c - ' ']); uint8_t x = 8; do { - //auto byte = pgm_read_byte(bytePtr++); - auto byte = loadPROGMEM(bytePtr); + auto byte = bytePtr.load_post_inc(); send(byte & bit(7) ? foreground : background); send(byte & bit(6) ? foreground : background); send(byte & bit(5) ? foreground : background); @@ -182,15 +181,12 @@ struct void newline() { y += 8; + x = 0; } void advance(uint8_t count = 1) { - if (x >= 320 - 8) - { - newline(); - x -= 320; - } - x += count * 8; + if (x >= 320 - 8) newline(); + else x += count * 8; } void puts(char const* str) { @@ -205,7 +201,7 @@ struct void testFont() { - lout.puts("Hello, World! It is a very nice day today, what are you up to today?"); + lout.puts("Hello, World!\nIt is a very nice day today, what are you up to today?"); } } diff --git a/makefile b/makefile @@ -25,7 +25,7 @@ program.elf: $(OBJECTS) # The install target install: program.hex - @ avrdude -c usbasp -p m644p -U $< + @ avrdude -c usbasp -p m644p -D -U $< # Clean up everything that we generate clean: diff --git a/utils.h b/utils.h @@ -36,17 +36,38 @@ inline constexpr uint8_t mix(const uint8_t a, const uint8_t b, const uint8_t mas return (a & ~mask) | (b & mask); } +struct flashPtr +{ + inline flashPtr(uint8_t const* const p) : ptr(p) {} + inline uint8_t load() const + { + uint8_t result; + asm volatile + ( + "lpm %0, Z" + + : "=r" (result) + : "z" (ptr) + ); + return result; + } + inline uint8_t load_post_inc() + { + uint8_t result; + asm volatile + ( + "lpm %0, Z+" + + : "=r" (result), "=z" (ptr) + : "1" (ptr) + ); + return result; + } + + static_assert(sizeof(uint16_t) == sizeof(uint8_t const *), "Flash ptr should be a word"); + +private: + uint8_t const* ptr; +}; -inline uint8_t loadPROGMEM(uint8_t const* &addr) -{ - uint8_t result; - asm volatile - ( - "lpm %0, Z+" - - : "=r" (result), "=z" (addr) - : "1" (addr) - ); - return result; -}