Applications moved from app/arm direcotry to app.
[LPC2xxx_and_RobotSpejbl.git] / app / canldtg / canld_tg.c
blob601b99684631e3c2b02f08f9797b9b1ef5bfab07
1 /* CAN loader */
2 #include <string.h>
3 #include <periph/can.h>
5 #define CANLOAD_HEAD 0x7fe
6 #define CANLOAD_BODY 0x7ff
8 #define CANLOAD_CMD_NOP 0x00
9 #define CANLOAD_CMD_LOAD 0x01
10 #define CANLOAD_CMD_GO 0x02
11 #define CANLOAD_CMD_ON 0xfa
13 #ifndef CANLOAD_ID
14 #error Unique 32bit number CANLOAD_ID must be defined for each compiled target board.
15 #endif
16 extern uint32_t canload_id;
18 #define STATUS_LED
19 #ifdef STATUS_LED
20 /* define i/o port addr&values for lighting status LED */
21 #define LED_PINSEL2 PINSEL2
22 #define LED_PINSEL_VAL2 0xfffffff3
23 #define LED_IODIR2 IODIR1
24 #define LED_IOSET2 IOSET1
25 #define LED_IOCLR2 IOCLR1
26 #define LED_IOBIT3 0x00080000
28 #define LED_PINSEL PINSEL1
29 #define LED_PINSEL_VAL 0xfffc0fff;
30 #define LED_IODIR IODIR0
31 #define LED_IOSET IOSET0
32 #define LED_IOCLR IOCLR0
33 #define LED_IOBIT0 0x00400000
34 #define LED_IOBIT1 0x00800000
35 #define LED_IOBIT2 0x01000000
36 #endif
38 can_msg_t received_msg;
40 void rx_msg(uint32_t id) {
41 do {
42 while (!can_msg_received);
43 can_msg_received = 0;
44 } while (can_rx_msg.id != id);
45 memcpy(&received_msg, (void*)&can_rx_msg, sizeof(can_msg_t));
48 int main() {
49 can_msg_t msg = {.flags = 0, .dlc = 5, .id = CANLOAD_ID & 0x7ff};
50 uint8_t cmd;
51 void *address;
52 void (*user_code)();
54 canload_id = CANLOAD_ID;
55 /* peripheral clock = CPU clock (10MHz) */
56 VPBDIV = 1;
57 /** map exception handling to ROM **/
58 MEMMAP = 0x1;
59 /* init Vector Interrupt Controller */
60 VICIntEnClr = 0xFFFFFFFF;
61 VICIntSelect = 0x00000000;
62 #ifdef STATUS_LED
63 /* turn LED on */
64 LED_PINSEL &= LED_PINSEL_VAL;
65 LED_PINSEL2 &= LED_PINSEL_VAL2;
66 LED_IODIR |= LED_IOBIT0;
67 LED_IODIR |= LED_IOBIT1;
68 LED_IODIR |= LED_IOBIT2;
69 LED_IODIR2 |= LED_IOBIT3;
70 LED_IOCLR = LED_IOBIT0;
71 LED_IOSET = LED_IOBIT1;
72 LED_IOCLR = LED_IOBIT2;
73 LED_IOCLR2 = LED_IOBIT3;
74 #endif
75 /* 10MHz CPU, 10MHz VPB, 50kb/s CAN */
76 can_init(0x250000, 14, NULL);
77 /* announce ourself to the world */
78 msg.data[4] = CANLOAD_CMD_ON;
79 ((uint32_t*)msg.data)[0] = canload_id;
80 #ifdef STATUS_LED
81 LED_IOCLR = LED_IOBIT1;
82 #endif
83 while (can_tx_msg(&msg));
84 #ifdef STATUS_LED
85 LED_IOSET = LED_IOBIT1;
86 #endif
87 /* command processing loop */
88 for (;;) {
89 /* wait for loader HEAD message */
90 rx_msg(CANLOAD_HEAD);
91 /* check target ID and mask */
92 if ((received_msg.dlc != 8) ||
93 (((uint32_t*)received_msg.data)[0] !=
94 (((uint32_t*)received_msg.data)[1] & canload_id)))
95 continue;
96 #ifdef STATUS_LED
97 LED_IOCLR = LED_IOBIT1;
98 #endif
99 /* receive loader command message */
100 rx_msg(CANLOAD_BODY);
101 switch (cmd = received_msg.data[0]) {
102 case CANLOAD_CMD_NOP:
103 break;
104 case CANLOAD_CMD_LOAD:
105 /* set starting load address */
106 memcpy(&address, (void*)received_msg.data + 1, 4);
107 /* receive data, until last packet (~shorter msg) arrive */
108 do {
109 rx_msg(CANLOAD_BODY);
110 memcpy(address, (void*)received_msg.data, received_msg.dlc);
111 address += received_msg.dlc;
112 } while (received_msg.dlc == 8);
113 break;
114 case CANLOAD_CMD_GO:
115 memcpy(&user_code, (void*)received_msg.data + 1, 4);
116 /* GO! */
117 user_code();
118 break;
119 default:
120 /* unrecognized command -- do not acknowledge */
121 continue;
123 #ifdef STATUS_LED
124 LED_IOSET = LED_IOBIT1;
125 #endif
126 /* command executed -- acknowledge */
127 msg.data[4] = cmd;
128 ((uint32_t*)msg.data)[0] = canload_id;
129 while (can_tx_msg(&msg));
133 /* KOHE||, */