TS-E+ базовая библиотека.
libTSE+
 Указатель Классы Функции Переменные Определения типов
system.h
1 /*
2  * board.h
3  *
4  * Created on: Apr 12, 2015
5  * Author: nick
6  */
7 
8 #ifndef BOARD_H_
9 #define BOARD_H_
10 
11 #include <stdint.h>
12 #include <stdlib.h>
13 #include <avr/io.h>
14 #include <avr/pgmspace.h>
15 #include "net.h"
16 
20 typedef struct {
21  uint32_t t32Mhz,t2Mhz;
23 
24 
28 class OUT {
29 private:
30  PORT_t *_port;
31  uint8_t _pin;
32  bool _on;
33 public:
34  OUT(PORT_t *port, const uint8_t pin): _port(port),_pin(pin) {
35  _port->OUTCLR = _pin;
36  _port->DIRSET = _pin;
37  _on = false;
38  }
39  void on() { _on = (_port->OUTSET = _pin); }
40  void off() { _on = !(_port->OUTCLR = _pin); }
41  void toggle() { _on ? off() : on(); }
42  bool isOn() { return _on; }
43 };
44 
48 class IN {
49 private:
50  PORT_t *_port;
51  uint8_t _pin;
52  bool _gc;
53 public:
59  IN(PORT_t *port, const uint8_t pin, bool gc = true);
60  bool isOn() {
61  return _gc ? !(_port->IN & _pin) : (_port->IN & _pin);
62  }
63 };
64 
68 #define GREEN 1
69 #define YELLOW 0
70 
74 #define nCS1(value) { \
75  if(value) PORTD.OUTCLR = PIN3_bm; else PORTD.OUTSET = PIN3_bm; \
76 }
77 
80 #define nCS2(value) { \
81  if(value) PORTD.OUTCLR = PIN2_bm; else PORTD.OUTSET = PIN2_bm; \
82 }
83 
84 
85 class SPI;
86 
90 class USARTHandler {
91 public:
92  virtual void onCharReceived(const char ) = 0;
93  virtual ~USARTHandler() { }
94 };
98 class USART {
99 private:
100  USARTHandler *_handler;
101 public:
102  USART();
103  static const char * CLRSCR;
104  static const char * CRLF;
108  void setHandler(USARTHandler * handler) { _handler = handler; }
112  USART& speed(const uint32_t);
113  USART& operator << (const char);
114  USART& operator << (const char *);
115  USART& operator >> (char &);
119  void ISRHandler();
120 };
121 
125 class FlashRAM {
126 public:
127  FlashRAM() {};
128  static const size_t BLOCK_SIZE;
129 
132  virtual uint32_t size() = 0;
136  virtual void init() = 0;
137  virtual size_t read(const uint32_t address, void * buffer, size_t size) =0;
138  virtual size_t write(const uint32_t address, const void * buffer, size_t size) = 0;
142  virtual bool erase() = 0;
146  virtual uint16_t id() = 0;
150  virtual bool eraseBlock(const uint32_t address) = 0;
151  virtual ~FlashRAM() { };
155  virtual void shutdown() = 0;
156 
157 };
158 
159 
163 class SPI {
164 private:
165  SPI_t *_port;
166 public:
167  SPI();
168  SPI &operator >> (uint8_t &);
169  SPI &operator << (const uint8_t);
170  const uint8_t get() {
171  uint8_t b;
172  *this >> b;
173  return b;
174  }
175  SPI &read(void *buffer,size_t size) {
176  uint8_t *p = (uint8_t *)buffer;
177  for(size_t i=0;i<size;i++)
178  *this >> p[i];
179  return *this;
180  }
181  SPI &write(const void *buffer, size_t size) {
182  uint8_t *p = (uint8_t *)buffer;
183  for(size_t i=0;i<size;i++)
184  *this << p[i];
185  return *this;
186  }
187 };
188 
189 typedef enum {
190  p10ms = 0,
191  p100ms = 1,
192  p1s = 2,
193  p10s = 3,
194  p1m = 4,
195  p1h = 5
196 } TaskPeriod;
200 typedef
201  void (*PeriodicTask)(const uint64_t ticks);
202 
206 class System {
207 private:
208  OUT _yellow, _green;
209  bool _perfomance;
210  IN _button;
211 public:
212  System();
217  OUT *led(const uint8_t which);
221  const uint64_t getTicksCount();
226  void setHiPerfomance(bool is32);
231  return _button.isOn();
232  }
236  size_t getFreeMem();
240  void reboot();
246  void addPeriodicTask(const TaskPeriod period, PeriodicTask task);
250  void removePeriodicTask(PeriodicTask task);
251  void onIdle();
252 };
253 
254 extern class USART *USART;
255 extern class SPI *SPI;
256 extern FlashRAM *FLASH;
257 extern class System *System;
258 extern PerfomanceTime perfomanceStats;
259 
260 #endif /* BOARD_H_ */
void reboot()
Definition: system.h:20
const uint64_t getTicksCount()
virtual void shutdown()=0
Definition: system.h:125
Definition: system.h:28
virtual bool erase()=0
void ISRHandler()
size_t getFreeMem()
OUT * led(const uint8_t which)
virtual uint32_t size()=0
void setHiPerfomance(bool is32)
Definition: system.h:163
virtual void init()=0
void addPeriodicTask(const TaskPeriod period, PeriodicTask task)
Definition: system.h:90
void setHandler(USARTHandler *handler)
Definition: system.h:108
Definition: system.h:98
IN(PORT_t *port, const uint8_t pin, bool gc=true)
USART & speed(const uint32_t)
Definition: system.h:206
Definition: system.h:48
virtual uint16_t id()=0
bool isButtonPressed()
Definition: system.h:230
static const size_t BLOCK_SIZE
размер минимально стираемого блока
Definition: system.h:127
virtual bool eraseBlock(const uint32_t address)=0
void removePeriodicTask(PeriodicTask task)