|
| 1 | +#ifndef __HardwareSerial_h__ |
| 2 | +#define __HardwareSerial_h__ |
| 3 | + |
| 4 | +#include <zpuino.h> |
| 5 | +#include <zpuino-types.h> |
| 6 | +#include "Stream.h" |
| 7 | +#include "Print.h" |
| 8 | + |
| 9 | +#ifdef ZPU15 |
| 10 | +#include "BaseDevice.h" |
| 11 | + |
| 12 | +#define VENDOR_ZPUINO 0x08 |
| 13 | +#define PRODUCT_ZPUINO_UART 0x11 |
| 14 | + |
| 15 | +namespace ZPUino { |
| 16 | +}; |
| 17 | + |
| 18 | +class HardwareSerial: public ZPUino::BaseDevice, public Stream |
| 19 | +{ |
| 20 | +private: |
| 21 | +public: |
| 22 | + HardwareSerial(uint8_t instance=0xff): BaseDevice(instance) {} |
| 23 | + |
| 24 | + __attribute__((always_inline)) inline void begin(const unsigned int baudrate) { |
| 25 | + if (deviceBegin(VENDOR_ZPUINO, PRODUCT_ZPUINO_UART)==0) { |
| 26 | + if (__builtin_constant_p(baudrate)) { |
| 27 | + REG(1) = BAUDRATEGEN(baudrate) | BIT(UARTEN); |
| 28 | + } else { |
| 29 | + begin_slow(baudrate); |
| 30 | + } |
| 31 | + } |
| 32 | + } |
| 33 | + void begin_slow(const unsigned int baudrate); |
| 34 | + |
| 35 | + int available(void) { |
| 36 | + return (REG(1) & 1); |
| 37 | + } |
| 38 | + |
| 39 | + virtual int read(void) { |
| 40 | + return REG(0); |
| 41 | + } |
| 42 | + |
| 43 | + virtual void flush(void); |
| 44 | + |
| 45 | + size_t write(uint8_t c); |
| 46 | + |
| 47 | + virtual int peek() { return -1; } |
| 48 | + |
| 49 | + using Print::write; // pull in write(str) and write(buf, size) from Print |
| 50 | +private: |
| 51 | + unsigned int ioslot; |
| 52 | +}; |
| 53 | + |
| 54 | +extern void serialEventRun(void) __attribute__((weak)); |
| 55 | + |
| 56 | +extern HardwareSerial Serial; /* 1st slot */ |
| 57 | +extern HardwareSerial Serial1; /* 1st slot */ |
| 58 | + |
| 59 | +#else |
| 60 | +class HardwareSerial: public Stream |
| 61 | +{ |
| 62 | +private: |
| 63 | +public: |
| 64 | + HardwareSerial(unsigned int b): ioslot(IO_SLOT(b)) {} |
| 65 | + |
| 66 | + __attribute__((always_inline)) inline void begin(const unsigned int baudrate) { |
| 67 | + if (__builtin_constant_p(baudrate)) { |
| 68 | + REGISTER(ioslot,1) = BAUDRATEGEN(baudrate) | BIT(UARTEN); |
| 69 | + } else { |
| 70 | + begin_slow(baudrate); |
| 71 | + } |
| 72 | + } |
| 73 | + void begin_slow(const unsigned int baudrate); |
| 74 | + |
| 75 | + int available(void) { |
| 76 | + return (REGISTER(ioslot,1) & 1); |
| 77 | + } |
| 78 | + |
| 79 | + virtual int read(void) { |
| 80 | + return REGISTER(ioslot,0); |
| 81 | + } |
| 82 | + |
| 83 | + virtual void flush(void) {}; |
| 84 | + |
| 85 | + size_t write(uint8_t c); |
| 86 | + |
| 87 | + virtual int peek() { return -1; } |
| 88 | + |
| 89 | + using Print::write; // pull in write(str) and write(buf, size) from Print |
| 90 | +private: |
| 91 | + unsigned int ioslot; |
| 92 | +}; |
| 93 | + |
| 94 | +extern void serialEventRun(void) __attribute__((weak)); |
| 95 | + |
| 96 | +extern HardwareSerial Serial; /* 1st slot */ |
| 97 | +#endif |
| 98 | + |
| 99 | +#endif |
0 commit comments