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
|
#include <xc.h>
#include <stdbool.h>
#include <stdint.h>
#include "system.h"
#include "types.h"
#include "table.h"
#include "spi.h"
#include "eeprom.h"
#include "dac.h"
#include "can.h"
// Parameters:
// Each parameter has an associated CAN ID and calibration table in the EEPROM.
enum {
TACH = 0,
SPEED = 1,
AN1 = 2,
AN2 = 3,
AN3 = 4,
AN4 = 5,
} Parameter;
// CAN ID for writing/reading calibration tables from a host PC.
// LSB of ID, 'X', indicates table and row number: X[7:5]{table #}, X[4:0]{row #}.
static const CanId tblCanId = {
.type = CAN_ID_EXT,
.eid = {0x00, 0x2F, 0x27, 0x01}, // 1272F0Xh
};
// CAN ID for writing/reading the CAN ID associated with each parameter
// from a host PC.
// LSB of ID, 'X', indicates a particular Parameter enum.
static const CanId paramCanId = {
.type = CAN_ID_EXT,
.eid = {0x10, 0x2F, 0x27, 0x01}, // 1272F1Xh
};
// Receive buffer 0 mask.
// RXB0 is used for control messages for reading/writing configuration tables and CAN IDs for each parameter, which are stored in the EEPROM.
// Mask all but the LSB of the Table and Parameter IDs.
static const CanId rxb0Mask = {
.type = CAN_ID_EXT,
.eid = {0x10, 0x2F, 0x27, 0x01}, // 1272F10h
};
// Calibration tables in EEPROM:
static Table tachTbl;
static Table speedTbl;
static Table an1Tbl;
static Table an2Tbl;
static Table an3Tbl;
// Filter ID addresses in EEPROM:
// Each one of these addresses holds a CanId struct.
static U16 id1Addr;
static U16 id2Addr;
static U16 id3Addr;
static U16 id4Addr;
static U16 id5Addr;
void
main(void) {
U16 offset;
// Initialize calibration table EEPROM addresses
offset = (U16){0u, 0u};
tachTbl = (Table){offset};
offset = addU16U8(offset, TAB_SIZE);
speedTbl = (Table){offset};
offset = addU16U8(offset, TAB_SIZE);
an1Tbl = (Table){offset};
offset = addU16U8(offset, TAB_SIZE);
an2Tbl = (Table){offset};
offset = addU16U8(offset, TAB_SIZE);
an3Tbl = (Table){offset};
offset = addU16U8(offset, TAB_SIZE);
// Initialize filter ID EEPROM addresses
id1Addr = offset;
offset = addU16U8(offset, sizeof(CanId));
id2Addr = offset;
offset = addU16U8(offset, sizeof(CanId));
id3Addr = offset;
offset = addU16U8(offset, sizeof(CanId));
id4Addr = offset;
offset = addU16U8(offset, sizeof(CanId));
id5Addr = offset;
offset = addU16U8(offset, sizeof(CanId));
sysInit();
spiInit();
eepromInit();
dacInit();
canInit();
for (;;) {
}
}
void
__interrupt() isr(void) {
}
|