RISC-V/V203F6P6/programmer/helpers.h
2025-02-04 16:32:08 +01:00

61 lines
1.8 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 používá 24-bitovou adresu počátku bloku, zde máme k dispozici
* jen 16-bitový offset. Takže to jen natáhneme 256 krát. Software
* tomu musí odpovídat.
* */
flash.EraseSector(blok << 8);
}
};
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