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
14 #error Unique 32bit number CANLOAD_ID must be defined for each compiled target board.
16 extern uint32_t canload_id
;
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
38 can_msg_t received_msg
;
40 void rx_msg(uint32_t id
) {
42 while (!can_msg_received
);
44 } while (can_rx_msg
.id
!= id
);
45 memcpy(&received_msg
, (void*)&can_rx_msg
, sizeof(can_msg_t
));
49 can_msg_t msg
= {.flags
= 0, .dlc
= 5, .id
= CANLOAD_ID
& 0x7ff};
54 canload_id
= CANLOAD_ID
;
55 /* peripheral clock = CPU clock (10MHz) */
57 /** map exception handling to ROM **/
59 /* init Vector Interrupt Controller */
60 VICIntEnClr
= 0xFFFFFFFF;
61 VICIntSelect
= 0x00000000;
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
;
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
;
81 LED_IOCLR
= LED_IOBIT1
;
83 while (can_tx_msg(&msg
));
85 LED_IOSET
= LED_IOBIT1
;
87 /* command processing loop */
89 /* wait for loader HEAD message */
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
)))
97 LED_IOCLR
= LED_IOBIT1
;
99 /* receive loader command message */
100 rx_msg(CANLOAD_BODY
);
101 switch (cmd
= received_msg
.data
[0]) {
102 case CANLOAD_CMD_NOP
:
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 */
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);
115 memcpy(&user_code
, (void*)received_msg
.data
+ 1, 4);
120 /* unrecognized command -- do not acknowledge */
124 LED_IOSET
= LED_IOBIT1
;
126 /* command executed -- acknowledge */
128 ((uint32_t*)msg
.data
)[0] = canload_id
;
129 while (can_tx_msg(&msg
));