-
Notifications
You must be signed in to change notification settings - Fork 0
/
mtssp_interface.cpp
112 lines (87 loc) · 2.71 KB
/
mtssp_interface.cpp
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <Wire.h>
#include "mtssp_interface.h"
#include "xbus.h"
MtsspInterface::MtsspInterface(MtsspDriver* driver)
: m_driver(driver) {
}
void MtsspInterface::readProtocolInfo(uint8_t& version, uint8_t& dataReadyConfig) {
uint8_t buffer[2];
m_driver->read(XBUS_PROTOCOL_INFO, buffer, 2);
version = buffer[0];
dataReadyConfig = buffer[1];
}
void MtsspInterface::configureProtocol(uint8_t dataReadyConfig) {
m_driver->write(XBUS_CONFIGURE_PROTOCOL, &dataReadyConfig, sizeof(dataReadyConfig));
}
void MtsspInterface::readPipeStatus(uint16_t& notificationMessageSize, uint16_t& measurementMessageSize) {
uint8_t status[4];
m_driver->read(XBUS_PIPE_STATUS, status, sizeof(status));
notificationMessageSize = status[0] | (status[1] << 8);
measurementMessageSize = status[2] | (status[3] << 8);
}
void MtsspInterface::readFromPipe(uint8_t* buffer, uint16_t size, uint8_t pipe) {
if (!(pipe == XBUS_NOTIFICATION_PIPE || pipe == XBUS_MEASUREMENT_PIPE)) {
Serial.println("mtssp_interface.cpp, pipe is not XBUS_NOTIFICATION_PIPE or XBUS_MEASUREMENT_PIPE");
return;
}
m_driver->read(pipe, buffer, size);
}
size_t Xbus_createRawMessage(uint8_t* dest, uint8_t const* message, enum XbusBusFormat format) {
int n;
uint8_t checksum;
uint16_t length;
uint8_t* dptr = dest;
length = Xbus_getPayloadLength(message);
if (dest == 0) {
switch (format) {
case XBF_I2c:
return (length < 255) ? length + 4 : length + 6;
case XBF_Spi:
return (length < 255) ? length + 7 : length + 9;
case XBF_Uart:
return (length < 255) ? length + 5 : length + 7;
}
}
switch (format) {
case XBF_I2c:
*dptr++ = XBUS_CONTROL_PIPE;
break;
case XBF_Spi:
*dptr++ = XBUS_CONTROL_PIPE;
// Fill bytes required to allow MT to process data
*dptr++ = 0;
*dptr++ = 0;
*dptr++ = 0;
break;
case XBF_Uart:
*dptr++ = XBUS_PREAMBLE;
*dptr++ = XBUS_MASTERDEVICE;
break;
}
checksum = 0;
checksum -= XBUS_MASTERDEVICE;
*dptr = Xbus_getMessageId(message);
checksum -= *dptr++;
if (length < XBUS_EXTENDED_LENGTH) {
*dptr = length;
checksum -= *dptr++;
} else {
*dptr = XBUS_EXTENDED_LENGTH;
checksum -= *dptr++;
*dptr = length >> 8;
checksum -= *dptr++;
*dptr = length & 0xFF;
checksum -= *dptr++;
}
for (n = 0; n < length; n++) {
*dptr = Xbus_getConstPointerToPayload(message)[n];
checksum -= *dptr++;
}
*dptr++ = checksum;
return dptr - dest;
}
void MtsspInterface::sendXbusMessage(uint8_t const* xbusMessage) {
uint8_t buffer[128];
size_t rawLength = Xbus_createRawMessage(buffer, xbusMessage, m_driver->busFormat());
m_driver->writeRaw(buffer, rawLength);
}