B15F Board 15 Famulus Edition

b15f.h
1 #ifndef B15F_H
2 #define B15F_H
3
4 #include <iostream>
5 #include <bits/stdc++.h>
6 #include <string>
7 #include <fstream>
8 #include <cstdlib>
9 #include <chrono>
10 #include <cstdint>
11 #include <vector>
12
13 #include <unistd.h>
14 #include <fcntl.h>
15 #include <sys/ioctl.h>
16 #include <termios.h>
17
18 #include "commit_hash.h"
19 #include "requests.h"
20 #include "usart.h"
21 #include "driverexception.h"
22 #include "timeoutexception.h"
23
24 // wichtig für die Register-Zugriffe
25 #define _AVR_IO_H_ 1 // Erzwinge die Inklusion
26 #include "/usr/lib/avr/include/avr/sfr_defs.h"
27 #include "/usr/lib/avr/include/avr/iom1284p.h"
28
29 typedef std::function<void(std::exception&)> errorhandler_t;
30
31 // Wrapper für Codeposition-Ersetzung
32 #define assertCode(code, expectation) assertCodeFunc(code, expectation, &__FUNCTION__[0], &__FILE__[0], __LINE__)
33 #define assertRequestLength(rq, rq_num) assertRequestLengthFunc(rq, rq_num,& __FUNCTION__[0], &__FILE__[0], __LINE__)
34
35
38 class B15F
39 {
40 public:
41
42 /*************************************
43 * Grundfunktionen des B15F Treibers *
44 *************************************/
45
50 static B15F& getInstance(void);
51
56 void reconnect(void);
57
62 void discard(void);
63
68 void testConnection(void);
69
74 void testIntConv(void);
75
80 std::vector<std::string> getBoardInfo(void);
81
86 void delay_ms(uint16_t ms);
87
92 void delay_us(uint16_t us);
93
99 void reverse(uint8_t& b);
100
105 static std::string exec(std::string cmd);
106
111 static void abort(std::string msg);
112
117 static void abort(std::exception& ex);
118
123 static void setAbortHandler(errorhandler_t func);
124
125 /*************************************/
126
127
128
129 /*************************
130 * Steuerbefehle für B15 *
131 *************************/
132
138 void activateSelfTestMode(void);
139
145 void digitalWrite0(uint8_t);
146
152 void digitalWrite1(uint8_t);
153
159 uint8_t digitalRead0(void);
160
166 uint8_t digitalRead1(void);
167
173 uint8_t readDipSwitch(void);
174
180 void analogWrite0(uint16_t port);
181
187 void analogWrite1(uint16_t port);
188
194 uint16_t analogRead(uint8_t channel);
195
211 void analogSequence(uint8_t channel_a, uint16_t* buffer_a, uint32_t offset_a, uint8_t channel_b, uint16_t* buffer_b, uint32_t offset_b, uint16_t start, int16_t delta, uint16_t count);
212
222 uint8_t pwmSetFrequency(uint32_t freq);
223
229 void pwmSetValue(uint8_t value);
230
239 void setMem8(volatile uint8_t* adr, uint8_t val);
240
247 uint8_t getMem8(volatile uint8_t* adr);
248
257 void setMem16(volatile uint16_t* adr, uint16_t val);
258
265 uint16_t getMem16(volatile uint16_t* adr);
266
274 void setRegister(volatile uint8_t* adr, uint8_t val);
275
281 uint8_t getRegister(volatile uint8_t* adr);
282
287 uint16_t* getInterruptCounterOffset(void);
288
293 void setServoEnabled(void);
294
299 void setServoDisabled(void);
300
306 void setServoPosition(uint16_t pos);
307
308 /*************************/
309
310
311 // CONSTANTS
312 const std::string PRE = "[B15F] ";
313 constexpr static uint8_t MSG_OK = 0xFF;
314 constexpr static uint8_t MSG_FAIL = 0xFE;
315 constexpr static uint16_t RECONNECT_TIMEOUT = 64;
316 constexpr static uint16_t WDT_TIMEOUT = 15;
317 constexpr static uint8_t RECONNECT_TRIES = 3;
318 constexpr static uint32_t BAUDRATE = 57600;
319
320 private:
321
325 B15F(void);
326
331 void init(void);
332
338 template<typename CodeType, typename ExpectationType>
339 void assertCodeFunc(CodeType& code, ExpectationType expectation, const char* func, const char* file, int line) const
340 {
341 if(code != static_cast<CodeType>(expectation))
342 throw DriverException("Ungültige Antwort erhalten: " + std::to_string((int) code) + " (erwartet: " + std::to_string((int) expectation) + ") in " + std::string(func) + ": " + std::string(file) + "#" + std::to_string(line));
343 }
344
350 template<size_t RequestLength>
351 void assertRequestLengthFunc(uint8_t (&)[RequestLength], uint8_t rq_num, const char* func, const char* file, int line)
352 {
353 if(RequestLength != rq_len[rq_num])
354 throw DriverException("Ungültige Request Länge: " + std::to_string(RequestLength) + " (erwartet: " + std::to_string(rq_len[rq_num]) + ") in " + std::string(func) + ": " + std::string(file) + "#" + std::to_string(line));
355 }
356
357 USART usart;
358 static B15F* instance;
359 static errorhandler_t errorhandler;
360 };
361
362 #endif // B15F_H
B15F::testIntConv
void testIntConv(void)
Definition: b15f.cpp:85
B15F::pwmSetValue
void pwmSetValue(uint8_t value)
Definition: b15f.cpp:437
B15F::exec
static std::string exec(std::string cmd)
Definition: b15f.cpp:159
B15F::MSG_FAIL
constexpr static uint8_t MSG_FAIL
Value to reject a received command.
Definition: b15f.h:312
B15F::analogWrite0
void analogWrite0(uint16_t port)
Definition: b15f.cpp:302
B15F::setServoPosition
void setServoPosition(uint16_t pos)
Definition: b15f.cpp:595
B15F::analogWrite1
void analogWrite1(uint16_t port)
Definition: b15f.cpp:319
B15F::getMem16
uint16_t getMem16(volatile uint16_t *adr)
Definition: b15f.cpp:515
B15F::delay_us
void delay_us(uint16_t us)
Definition: b15f.cpp:146
B15F::digitalRead0
uint8_t digitalRead0(void)
Definition: b15f.cpp:251
B15F::pwmSetFrequency
uint8_t pwmSetFrequency(uint32_t freq)
Definition: b15f.cpp:416
B15F::digitalWrite0
void digitalWrite0(uint8_t)
Definition: b15f.cpp:219
B15F::analogSequence
void analogSequence(uint8_t channel_a, uint16_t *buffer_a, uint32_t offset_a, uint8_t channel_b, uint16_t *buffer_b, uint32_t offset_b, uint16_t start, int16_t delta, uint16_t count)
Definition: b15f.cpp:359
B15F::readDipSwitch
uint8_t readDipSwitch(void)
Definition: b15f.cpp:283
B15F::getRegister
uint8_t getRegister(volatile uint8_t *adr)
Definition: b15f.cpp:539
B15F::getMem8
uint8_t getMem8(volatile uint8_t *adr)
Definition: b15f.cpp:475
B15F::delay_ms
void delay_ms(uint16_t ms)
Definition: b15f.cpp:141
B15F::testConnection
void testConnection(void)
Definition: b15f.cpp:63
B15F::getInstance
static B15F & getInstance(void)
Definition: b15f.cpp:10
B15F
Definition: b15f.h:38
B15F::abort
static void abort(std::string msg)
Definition: b15f.cpp:175
B15F::getInterruptCounterOffset
uint16_t * getInterruptCounterOffset(void)
Definition: b15f.cpp:544
USART
Definition: usart.h:16
B15F::setMem16
void setMem16(volatile uint16_t *adr, uint16_t val)
Definition: b15f.cpp:494
B15F::analogRead
uint16_t analogRead(uint8_t channel)
Definition: b15f.cpp:336
B15F::activateSelfTestMode
void activateSelfTestMode(void)
Definition: b15f.cpp:204
B15F::PRE
const std::string PRE
B15F stdout prefix.
Definition: b15f.h:310
B15F::reverse
void reverse(uint8_t &b)
Definition: b15f.cpp:151
B15F::getBoardInfo
std::vector< std::string > getBoardInfo(void)
Definition: b15f.cpp:107
B15F::RECONNECT_TIMEOUT
constexpr static uint16_t RECONNECT_TIMEOUT
Time in ms after which a reconnect attempt aborts.
Definition: b15f.h:313
B15F::discard
void discard(void)
Definition: b15f.cpp:40
B15F::setRegister
void setRegister(volatile uint8_t *adr, uint8_t val)
Definition: b15f.cpp:534
B15F::MSG_OK
constexpr static uint8_t MSG_OK
Value to acknowledge a received command.
Definition: b15f.h:311
B15F::digitalRead1
uint8_t digitalRead1(void)
Definition: b15f.cpp:267
B15F::WDT_TIMEOUT
constexpr static uint16_t WDT_TIMEOUT
Time in ms after which the watch dog timer resets the MCU.
Definition: b15f.h:314
B15F::reconnect
void reconnect(void)
Definition: b15f.cpp:18
B15F::BAUDRATE
constexpr static uint32_t BAUDRATE
USART baudrate for communication with the MCU.
Definition: b15f.h:316
B15F::setAbortHandler
static void setAbortHandler(errorhandler_t func)
Definition: b15f.cpp:192
B15F::digitalWrite1
void digitalWrite1(uint8_t)
Definition: b15f.cpp:235
B15F::setServoEnabled
void setServoEnabled(void)
Definition: b15f.cpp:561
B15F::RECONNECT_TRIES
constexpr static uint8_t RECONNECT_TRIES
Maximum count of reconnect attempts after which the driver stops.
Definition: b15f.h:315
B15F::setMem8
void setMem8(volatile uint8_t *adr, uint8_t val)
Definition: b15f.cpp:455
B15F::setServoDisabled
void setServoDisabled(void)
Definition: b15f.cpp:578
DriverException
Definition: driverexception.h:10