-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcan-c.s
198 lines (168 loc) · 4.79 KB
/
can-c.s
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
Controller Area Network (CAN) Controller.
*/
.thumb
.text
.equ CAN_MCR_OFFSET, 0x00 /* Master Control Register */
.equ CAN_MSR_OFFSET, 0x04 /* Master Status Register */
.equ CAN_TSR_OFFSET, 0x08 /* Transmit Status Register */
.equ CAN_ESR_OFFSET, 0x18 /* Error Status Register */
.equ CAN_BTR_OFFSET, 0x1C /* Bit Timing Register */
.equ CAN_ESR_OFFSET, 0x18 /* Error Status Register */
.equ CAN_TI0R_OFFSET, 0x180 /* Transmit Mailbox Identifier 0 Register */
.equ CAN_TDT0R_OFFSET, 0x184 /* Transmit Mailbox Data Time 0 Register */
.equ CAN_TDL0R_OFFSET, 0x188 /* Transmit Mailbox Data Low 0 Register */
.equ CAN_TDH0R_OFFSET, 0x18C /* Transmit Mailbox Data High 0 Register */
.equ CAN_MCR, CAN_BASE + CAN_MCR_OFFSET
.equ CAN_MSR, CAN_BASE + CAN_MSR_OFFSET
.equ CAN_TSR, CAN_BASE + CAN_TSR_OFFSET
.equ CAN_ESR, CAN_BASE + CAN_ESR_OFFSET
.equ CAN_BTR, CAN_BASE + CAN_BTR_OFFSET
.equ CAN_TI0R, CAN_BASE + CAN_TI0R_OFFSET
.equ CAN_TDT0R, CAN_BASE + CAN_TDT0R_OFFSET
.equ CAN_TDL0R, CAN_BASE + CAN_TDL0R_OFFSET
.equ CAN_MCR_INRQ, 1 << 0 /* Initialization mode request */
.equ CAN_MCR_SLEEP, 1 << 1 /* Sleep mode */
.equ CAN_MCR_NART, 1 << 4 /* Autmatic retransmit */
.equ CAN_MSR_INAK, 1 << 0 /* Initialization acknowledgement */
.equ CAN_BTR_LBKM, 0 << 30 /* Loopback mode */
.equ CAN_BTR_SJW, 0x1 << 24 /* ReSynchronization Jump Width */
.equ CAN_BTR_TS1, 13 << 16 /* Time Segment 1 (prop seg+phase 1 seg) */
.equ CAN_BTR_TS2, 2 << 20 /* Time Segment 2 (phase 2 segment) */
.equ CAN_BTR_BRP, 6 << 0 /* Baud Rate Prescalar */
.equ CAN_TSR_TME0, 1 << 26 /* Transmit Mailbox 0 Empty */
.equ CAN_TI0R_IDE, 0 << 2 /* Identifier extension */
.equ CAN_TI0R_TXRQ, 1 << 0 /* Transmit request */
.equ CAN_TDT0R_DCL, 1 << 0 /* Data Code Length */
.equ CAN_TI0R_ID, 7 << 21 /* Identifier */
.equ CAN_TI0R_RTR, 1 << 1 /* Data Frame */
.global start
Vector_Table:
.word 0x20002000
.word start + 1
start:
bl led_init
bl can_init
bl can_controller_init
send_loop:
bl turn_led_on
bl can_send
bl delay
bl turn_led_off
b .
can_controller_init:
ldr r1, =CAN_MCR
ldr r2, =CAN_MCR_INRQ
ldr r0, [r1]
orr r0, r0, r2
str r0, [r1]
ldr r1, =CAN_MSR
ldr r2, =CAN_MSR_INAK
wait_inak_set:
ldr r0, [r1]
and r0, r0, r2
cmp r0, r2
bne wait_inak_set
/* Exit sleep mode */
ldr r1, =CAN_MCR
ldr r2, =CAN_MCR_SLEEP
ldr r0, [r1]
bic r0, r0, r2
str r0, [r1]
/* Set No Automatic Retransmission */
/*
ldr r1, =CAN_MCR
ldr r2, =CAN_MCR_NART
ldr r0, [r1]
orr r0, r0, r2
str r0, [r1]
*/
ldr r1, =CAN_BTR
ldr r2, =#0x0000
str r2, [r1]
/* Set loopback mode */
ldr r1, =CAN_BTR
ldr r2, =CAN_BTR_LBKM
ldr r0, [r1]
orr r0, r0, r2
str r0, [r1]
ldr r1, =CAN_BTR
ldr r2, =CAN_BTR_SJW
ldr r0, [r1]
orr r0, r0, r2
str r0, [r1]
ldr r1, =CAN_BTR
ldr r2, =(CAN_BTR_TS1 + CAN_BTR_TS2 + CAN_BTR_BRP)
ldr r0, [r1]
orr r0, r0, r2
str r0, [r1]
/* Exit init mode */
ldr r1, =CAN_MCR
ldr r2, =CAN_MCR_INRQ
ldr r0, [r1]
bic r0, r0, r2
str r0, [r1]
ldr r1, =CAN_MSR
ldr r2, =CAN_MSR_INAK
ldr r7, =CAN_ESR
wait_inak:
ldr r0, [r1]
and r0, r0, r2
cmp r0, r2
beq wait_inak
bx lr
can_send:
ldr r6, =CAN_TSR
ldr r7, =CAN_ESR
/* Check that the Transmit 0 mailbox is empty */
ldr r1, =CAN_TSR
ldr r2, =CAN_TSR_TME0
ldr r0, [r1]
and r0, r0, r2
cmp r0, r2
bne mailbox_0_not_empty
ldr r1, =CAN_TI0R
ldr r2, =0x0000
str r2, [r1]
/* Set standard message type */
ldr r1, =CAN_TI0R
ldr r2, =CAN_TI0R_IDE
ldr r0, [r1]
orr r0, r0, r2
str r0, [r1]
/* Set the message length */
ldr r1, =CAN_TDT0R
ldr r2, =CAN_TDT0R_DCL
ldr r0, [r1]
orr r0, r0, r2
str r0, [r1]
/* Set the message */
ldr r1, =CAN_TDL0R
ldr r2, ='A'
str r2, [r1]
/* Set the identifier */
ldr r1, =CAN_TI0R
ldr r2, =CAN_TI0R_ID
ldr r0, [r1]
orr r0, r0, r2
str r0, [r1]
/* Set Remote Transmission Request to be Data Frame */
ldr r1, =CAN_TI0R
ldr r2, =CAN_TI0R_RTR
ldr r0, [r1]
bic r0, r0, r2
str r0, [r1]
/* Request transmission of the mailbox. This bit is cleared by hardware
when the mailbox becomes empty */
ldr r6, =CAN_TSR
ldr r1, =CAN_TI0R
ldr r2, =CAN_TI0R_TXRQ
ldr r0, [r1]
orr r0, r0, r2
str r0, [r1]
ldr r4, =CAN_MSR
ldr r5, =CAN_TDL0R
ldr r7, =CAN_ESR
bx lr
mailbox_0_not_empty:
b .