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
337 template<typename CodeType, typename ExpectationType>
338 void assertCodeFunc(CodeType& code, ExpectationType expectation, const char* func, const char* file, int line) const
339 {
340 if(code != static_cast<CodeType>(expectation))
341 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));
342 }
343
348 template<size_t RequestLength>
349 void assertRequestLengthFunc(uint8_t (&)[RequestLength], uint8_t rq_num, const char* func, const char* file, int line)
350 {
351 if(RequestLength != rq_len[rq_num])
352 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));
353 }
354
355 USART usart;
356 static B15F* instance;
357 static errorhandler_t errorhandler;
358 };
359
360 #endif // B15F_H
B15F::testIntConv
void testIntConv(void)
Definition: b15f.cpp:83
B15F::pwmSetValue
void pwmSetValue(uint8_t value)
Definition: b15f.cpp:431
B15F::exec
static std::string exec(std::string cmd)
Definition: b15f.cpp:153
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:296
B15F::setServoPosition
void setServoPosition(uint16_t pos)
Definition: b15f.cpp:589
B15F::analogWrite1
void analogWrite1(uint16_t port)
Definition: b15f.cpp:313
B15F::getMem16
uint16_t getMem16(volatile uint16_t *adr)
Definition: b15f.cpp:509
B15F::delay_us
void delay_us(uint16_t us)
Definition: b15f.cpp:140
B15F::digitalRead0
uint8_t digitalRead0(void)
Definition: b15f.cpp:245
B15F::pwmSetFrequency
uint8_t pwmSetFrequency(uint32_t freq)
Definition: b15f.cpp:410
B15F::digitalWrite0
void digitalWrite0(uint8_t)
Definition: b15f.cpp:213
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:353
B15F::readDipSwitch
uint8_t readDipSwitch(void)
Definition: b15f.cpp:277
B15F::getRegister
uint8_t getRegister(volatile uint8_t *adr)
Definition: b15f.cpp:533
B15F::getMem8
uint8_t getMem8(volatile uint8_t *adr)
Definition: b15f.cpp:469
B15F::delay_ms
void delay_ms(uint16_t ms)
Definition: b15f.cpp:135
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:169
B15F::getInterruptCounterOffset
uint16_t * getInterruptCounterOffset(void)
Definition: b15f.cpp:538
USART
Definition: usart.h:16
B15F::setMem16
void setMem16(volatile uint16_t *adr, uint16_t val)
Definition: b15f.cpp:488
B15F::analogRead
uint16_t analogRead(uint8_t channel)
Definition: b15f.cpp:330
B15F::activateSelfTestMode
void activateSelfTestMode(void)
Definition: b15f.cpp:198
B15F::PRE
const std::string PRE
B15F stdout prefix.
Definition: b15f.h:310
B15F::reverse
void reverse(uint8_t &b)
Definition: b15f.cpp:145
B15F::getBoardInfo
std::vector< std::string > getBoardInfo(void)
Definition: b15f.cpp:103
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:528
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:261
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:186
B15F::digitalWrite1
void digitalWrite1(uint8_t)
Definition: b15f.cpp:229
B15F::setServoEnabled
void setServoEnabled(void)
Definition: b15f.cpp:555
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:449
B15F::setServoDisabled
void setServoDisabled(void)
Definition: b15f.cpp:572
DriverException
Definition: driverexception.h:10