-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstartupavr.ino
139 lines (111 loc) · 3.38 KB
/
startupavr.ino
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/* CAN interface structure... MessageID->Messagestructure
NBT power comand
ID = 0x12F
Data = 0x37, 0x7C, 0x8A, 0xDD, 0xD4, 0x05, 0x33, 0x6B
Ignition off message for NBT
ID = 0x12F
Data = 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
CIC power command:
ID = 0x4F8
Data = 0x00, 0x42, 0xFE, 0x01, 0xFF, 0xFF, 0xFF, 0xFF
Tagged as toLowerCase?
CIC
ID = 0x273
Data = 0x1D, 0xE1, 0x00, 0xF0, 0xFF, 0x7F, 0xDE, 0x04
NBT
ID = 0x563
Data = 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x63
intended flow:
Gunna skip using the CAS/Ignition listening setup and go a little more old school
will wire a switch to close with accessory 12v power to kick over action of NBT/CIC on/off
Key in ignition->Aux 12v power on hardware listen -> NBT/CIC poweron -> Pulse command (if needed) -> NBT poweroff when 12v accessory switch is off
SOURCED FROM https://github.com/kmalinich/node-bmw-client specifically his NBT module... i basically ported the concept to AVR to work on BMWs with CAN connections
*/
// #define DEBUG //uncomment for serial debug
// #define CIC // uncomment for CIC
#define NBT // uncomment for NBT
#include <mcp_can.h>
#include <SPI.h>
int powerswitch = 0;
int sendpower = 0;
const int powerpin = A5;
byte len;
int wakeswitch = 0;
#ifdef NBT
byte NBTpower[] = {0x12F};
byte NBTcase[] = {0x563};
#endif
#ifdef CIC
byte CICcase[] = {0x273};
byte CICpower[] = {0x4F8};
#endif
#ifdef NBT
byte NBTlowercaseBuf[] = {0x00,0x00,0x00,0x00,0xFF,0xFF,0x00,0x63};
#endif
#ifdef CIC
byte CIClowercaseBuf[] = {0x1D,0xE1,0x00,0xF0,0xFF,0x7F,0xDE,0x04};
#endif
//I think this command is to be sent to keep the device awake when powered on
byte PowerOFFBuf[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
#ifdef NBT
byte NBTpowerBuf[] = {0x37,0x7C,0x8A,0xDD,0xD4,0x05,0x33,0x6B};
#endif
#ifdef CIC
byte CICpowerbuf[] = {0x00,0x42,0xFE,0x01,0xFF,0xFF,0xFF,0xFF};
#endif
MCP_CAN CAN0(10); //CAN0 is on pin 10 for CS
MCP_CAN CAN1(9); // CAN1 is on pin 9 for CS
void setup(){
// init CAN0 bus, baudrate: 250k@16MHz
if(CAN0.begin(MCP_EXT, CAN_250KBPS, MCP_16MHZ) == CAN_OK){
#ifdef DEBUG
Serial.print("CAN0: Init OK!\r\n");
#endif
CAN0.setMode(MCP_NORMAL);
}
#ifdef DEBUG
else Serial.print("CAN0: Init Fail!!!\r\n");
#endif
// init CAN1 bus, baudrate: 250k@16MHz
if(CAN1.begin(MCP_EXT, CAN_250KBPS, MCP_16MHZ) == CAN_OK){
#ifdef DEBUG
Serial.print("CAN1: Init OK!\r\n");
#endif
CAN1.setMode(MCP_NORMAL);
}
#ifdef DEBUG
else Serial.print("CAN1: Init Fail!!!\r\n");
#endif
SPI.setClockDivider(SPI_CLOCK_DIV2); // Set SPI to run at 8MHz (16MHz / 2 = 8 MHz)
}
void loop() {
//todo add actual listen action for the hardware switch that is 12v switched power.
sendpower = analogRead(powerpin); //can probably substitute this for some kind of CAN.readMsgBuf watchdog with the statusing of the CAS / IKE module
// CAS 0x00=off 0x40=first key touch 0x41=Accessory 0x45=run 0x55 = start
if(sendpower >= 0) {
powerswitch = 1;
}
else if(sendpower <= 1) {
powerswitch = 0;
}
switch (powerswitch) {
case '1' : {
#ifdef NBT
CAN0.sendMsgBuf(NBTpower,len,NBTpowerBuf);
if (wakeswitch==0)
{ wakeswitch = 1;
}
#endif
break;
}
case '0' : {
#ifdef NBT
CAN0.sendMsgBuf(NBTpower,len,PowerOFFBuf);
#endif
}
}
switch (wakeswitch) {
case '0' : break;
case '1' : CAN0.sendMsgBuf(NBTcase,len,NBTlowercaseBuf);
}
}