58 lines
1.5 KiB
C
58 lines
1.5 KiB
C
![]() |
#ifndef HELPERS_H
|
||
|
#define HELPERS_H
|
||
|
#include <stdint.h>
|
||
|
#include "norflash.h"
|
||
|
#include "fifo.h"
|
||
|
#include "baselayer.h"
|
||
|
|
||
|
class MemoryBase {
|
||
|
NorFlash flash;
|
||
|
public:
|
||
|
explicit MemoryBase () noexcept : flash() {}
|
||
|
uint32_t Write (const uint32_t addr, const void * ptr, const uint32_t len) {
|
||
|
return flash.WriteBlock(addr, reinterpret_cast<const uint8_t*>(ptr), len);
|
||
|
}
|
||
|
uint32_t Read (const uint32_t addr, void * ptr, const uint32_t len) {
|
||
|
return flash.ReadBlock(addr, reinterpret_cast<uint8_t*> (ptr), len);
|
||
|
}
|
||
|
void Erase (const uint32_t blok) {
|
||
|
flash.EraseSector(blok);
|
||
|
}
|
||
|
};
|
||
|
|
||
|
class CdcCmd : public BaseLayer {
|
||
|
static constexpr int maxlen = 0x80;
|
||
|
FIFO<char, maxlen> ring;
|
||
|
char line [maxlen];
|
||
|
unsigned index;
|
||
|
public:
|
||
|
explicit CdcCmd () noexcept : BaseLayer(), ring(), index(0u) {}
|
||
|
uint32_t Up(const char * data, const uint32_t len) override {
|
||
|
for (unsigned n=0u; n<len; n++) ring.Write (data [n]);
|
||
|
return len;
|
||
|
}
|
||
|
void SendString (const char * ptr, const uint32_t len) {
|
||
|
unsigned ofs = 0u, res = len;
|
||
|
while (res) {
|
||
|
const unsigned chunk = res > 0x20u ? 0x20u : res;
|
||
|
const unsigned n = Down (ptr + ofs, chunk);
|
||
|
ofs += n;
|
||
|
res -= n;
|
||
|
}
|
||
|
}
|
||
|
char * GetLine (unsigned & len) {
|
||
|
char c;
|
||
|
while (ring.Read(c)) {
|
||
|
line [index++] = c;
|
||
|
if (c == '\n') {
|
||
|
len = index;
|
||
|
index = 0u;
|
||
|
return line;
|
||
|
}
|
||
|
}
|
||
|
len = 0u;
|
||
|
return line;
|
||
|
}
|
||
|
};
|
||
|
#endif // HELPERS_H
|