forked from Candas1/Split_Hoverboard_SimpleFOC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PilotI2c.cpp
125 lines (102 loc) · 2.86 KB
/
PilotI2c.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
113
114
115
116
117
118
119
120
121
122
123
#include "PilotI2c.h"
#include <Defines.h>
#include <Wire.h>
//CRC-8 - based on the CRC8 formulas by Dallas/Maxim
//code released under the therms of the GNU GPL 3.0 license
byte CRC8(const byte *data, byte len) {
byte crc = 0x00;
while (len--) {
byte extract = *data++;
for (byte tempI = 8; tempI; tempI--) {
byte sum = (crc ^ extract) & 0x01;
crc >>= 1;
if (sum) {
crc ^= 0x8C;
}
extract >>= 1;
}
}
return crc;
}
template <typename O,typename T> unsigned int SerialWrite (O& oSerial, const T& o) // for uart and i2c :-)
{
oSerial.write((byte *) &o, sizeof (o));
byte iCRC8 = CRC8((byte *) &o,sizeof (o));
oSerial.write(iCRC8);
return sizeof (o);
}
template <typename C,typename S, typename I> bool SerialRead(C& oSerial,S& o, I iAvailable) // for uart and i2c :-)
{
if (iAvailable < sizeof(o)+1 )
return false;
byte buff[sizeof(o)];
byte* p = buff;
for (unsigned int i=0; i < sizeof o; i++)
*p++ = oSerial.read();
byte iMust = CRC8(buff,sizeof(o) );
byte iCRC8 = oSerial.read();
if (iMust != iCRC8)
return false;
memcpy(&o,buff,sizeof(o));
return true;
}
PilotI2c* pPilotI2c = NULL;
void RequestEventPilotI2c()
{
if (pPilotI2c) pPilotI2c->RequestEvent();
}
void ReceiveEventPilotI2c(int iAvail)
{
if (pPilotI2c) pPilotI2c->ReceiveEvent(iAvail);
}
PilotI2c::PilotI2c(Hoverboard& r) : Pilot(r)
{
}
// function that executes whenever data is requested by master
void PilotI2c::RequestEvent()
{
iRequests++;
oSlave2Master.iStatus = oHoverboard.IsReady() * HOVERSTATUS_Ready;
oSlave2Master.fRpm1 = oHoverboard.GetRpmPerVolt();
oSlave2Master.fCurrent1 = oHoverboard.GetDcCurrent();
SerialWrite(Wire,oSlave2Master); // to send struct with crc: from openPanasonicBike
}
void PilotI2c::ReceiveEvent(int iAvail)
{
iReceived++;
switch(iAvail-1) // CRC takes one bytes
{
case sizeof(Server2Hover1) :
if (SerialRead(Wire,oMaster2Slave1,iAvail))
{
oSlave2Master.iCmdLast = oMaster2Slave1.iCmd;
switch (oMaster2Slave1.iCmd)
{
case CMD1_Speed:
oHoverboard.MoveLP(oMaster2Slave1.fValue);
break;
}
return;
}
break;
case sizeof(Server2Hover2) :
if (SerialRead(Wire,oMaster2Slave2,iAvail))
{
oSlave2Master.iCmdLast = oMaster2Slave2.iCmd;
return;
}
break;
}
OUT2("\nCRC fail. avail: ",iAvail);
}
void PilotI2c::init(byte iI2cAdress)
{
pPilotI2c = this;
Wire.begin(iI2cAdress); // join i2c bus with address #8
// ArduinoCore: nvic_irq_enable(I2C0_EV_IRQn, 1, 3);
// nvic_irq_enable(I2C0_ER_IRQn, 1, 2);
nvic_irq_enable(I2C0_EV_IRQn, 3, 3); // to not block IrqMotor nvic_irq_enable(XY_IRQn, 1, 2);
nvic_irq_enable(I2C0_ER_IRQn, 3, 3);
Wire.onReceive(ReceiveEventPilotI2c); // receiveEvent called when data from master received
Wire.onRequest(RequestEventPilotI2c); // called when master requests data from this slave
}