-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTMCSerial.hpp
More file actions
77 lines (60 loc) · 2.91 KB
/
TMCSerial.hpp
File metadata and controls
77 lines (60 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
* TMCSerial.h
*
* Created on: 09.05.2021
* Author: AB
*/
#ifndef TMCSERIAL_H_
#define TMCSERIAL_H_
#include<Arduino.h>
#include<stdint.h>
#include<stdbool.h>
#include"TMCField.hpp"
#define TMCSERIAL_READ_ERROR -1 // Invalid checksum in read
#define TMCSERIAL_READ_TIMEOUT -2 // Timed out waiting for whole frame
#define TMCSERIAL_WRITE_ERROR -3 // Write transaction didn't increment IFCNT
#define TMCSERIAL_RW_SUCCESS 0
class TMCSerial
{
public:
/* Create instance, set up attributes. */
TMCSerial(HardwareSerial& serialPort, uint32_t baudrate, uint8_t chipAddress);
/* Initialize Serial Port. Takes no arguments. */
void begin();
/* Safe register update, calls on read/write functions, and forwards error code. */
void writeField(TMCField field, uint32_t value, int8_t &tmcError);
uint32_t readField(TMCField field, int8_t &tmcError);
/* Overloaded register functions, not using error code for simpler syntax. */
void writeField(TMCField field, uint32_t value);
uint32_t readField(TMCField field);
/* Enable/Disable checking for success of write operations. Disabled by default. */
void enableVerifyWrite(bool enable);
/* Enable/Disable Checksum calculation on receive data. Disabled by default. */
void enableReadChecksum(bool enable);
/* Sets delay for bus switching both for master side, and inside TMC Chip in slaveConf register.
* Lower 3 bits of bitTimes will be ignored which makes valid times :
* 8*1, 8*3, 8*5, 8*7, 8*9, 8*11, 8*13, 8*15, values over 120 are ignored.
* This is for use with RS485 transceiver that might need delay for switching between transmitting and receiving. */
void setMasterSlaveDelay(uint32_t bitTimes);
/* Read and Write raw registers using its address, returns error code. */
int8_t writeRegister(uint8_t address, uint32_t ®isterValue);
int8_t readRegister(uint8_t address, uint32_t ®istervalue);
private:
/* Chip communication parameters */
HardwareSerial& _serialPort;
uint32_t _baudrate;
uint8_t _chipAddress;
/* Calculate CRC for outgoing and incoming data */
bool _enableReadChecksum;
uint8_t calcCRC(uint8_t data[], uint8_t dataLength);
/* Check counter for successful write feature */
bool _enableVerifyWrite;
uint32_t _writeCounter;
bool isCounterIncreased();
/* Sets delay to switch while bus switches from master to slave, and vice-versa.
* Input is the number of bits times to wait, _masterSlaveDelay is a value in microseconds.
* If sendToChip is True, function will attempt to write to the chip SLAVECONF register. */
uint32_t _masterSlaveDelay;
void updateMasterSlaveDelay(uint32_t bitTimes, bool sendToChip);
};
#endif /* TMCSERIAL_H_ */