aboutsummaryrefslogtreecommitdiff
path: root/lib/stm32/f1/can.c
blob: fc7e0e756c20d853f29b17362698b4d8f4ae4adb (plain)
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*
 * This file is part of the libopencm3 project.
 *
 * Copyright (C) 2010 Piotr Esden-Tempski <piotr@esden.net>
 *
 * This library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <libopencm3/stm32/can.h>
#include <libopencm3/stm32/f1/rcc.h>

void can_reset(u32 canport)
{
	if (canport == CAN1) {
		rcc_peripheral_reset(&RCC_APB1RSTR, RCC_APB1RSTR_CAN1RST);
		rcc_peripheral_clear_reset(&RCC_APB1RSTR, RCC_APB1RSTR_CAN1RST);
	} else {
		rcc_peripheral_reset(&RCC_APB1RSTR, RCC_APB1RSTR_CAN2RST);
		rcc_peripheral_clear_reset(&RCC_APB1RSTR, RCC_APB1RSTR_CAN2RST);
	}
}

int can_init(u32 canport, bool ttcm, bool abom, bool awum, bool nart,
	     bool rflm, bool txfp, u32 sjw, u32 ts1, u32 ts2, u32 brp)
{
	u32 wait_ack = 0x00000000;
	u32 can_msr_inak_timeout = 0x0000FFFF;
	int ret = 0;

	/* Exit from sleep mode. */
	CAN_MCR(canport) &= ~CAN_MCR_SLEEP;

	/* Request initialization "enter". */
	CAN_MCR(canport) |= CAN_MCR_INRQ;

	/* Wait for acknowledge. */
	while ((wait_ack != can_msr_inak_timeout) &&
	       ((CAN_MSR(canport) & CAN_MSR_INAK) != CAN_MSR_INAK)) {
		wait_ack++;
	}

	/* Check the acknowledge. */
	if ((CAN_MSR(canport) & CAN_MSR_INAK) != CAN_MSR_INAK)
		return 1;

	/* Set the automatic bus-off management. */
	if (ttcm)
		CAN_MCR(canport) |= CAN_MCR_TTCM;
	else
		CAN_MCR(canport) &= ~CAN_MCR_TTCM;

	if (abom)
		CAN_MCR(canport) |= CAN_MCR_ABOM;
	else
		CAN_MCR(canport) &= ~CAN_MCR_ABOM;

	if (awum)
		CAN_MCR(canport) |= CAN_MCR_AWUM;
	else
		CAN_MCR(canport) &= ~CAN_MCR_AWUM;

	if (nart)
		CAN_MCR(canport) |= CAN_MCR_NART;
	else
		CAN_MCR(canport) &= ~CAN_MCR_NART;

	if (rflm)
		CAN_MCR(canport) |= CAN_MCR_RFLM;
	else
		CAN_MCR(canport) &= ~CAN_MCR_RFLM;

	if (txfp)
		CAN_MCR(canport) |= CAN_MCR_TXFP;
	else
		CAN_MCR(canport) &= ~CAN_MCR_TXFP;

	/* Set bit timings. */
	CAN_BTR(canport) = sjw | ts2 | ts1 |
		           (u32)(CAN_BTR_BRP_MASK & (brp - 1));

	/* Request initialization "leave". */
	CAN_MCR(canport) &= ~CAN_MCR_INRQ;

	/* Wait for acknowledge. */
	wait_ack = 0x00000000;
	while ((wait_ack != can_msr_inak_timeout) &&
	       ((CAN_MSR(canport) & CAN_MSR_INAK) == CAN_MSR_INAK)) {
		wait_ack++;
	}

	if ((CAN_MSR(canport) & CAN_MSR_INAK) == CAN_MSR_INAK)
		ret = 1;

	return ret;
}

void can_filter_init(u32 canport, u32 nr, bool scale_32bit, bool id_list_mode,
		     u32 fr1, u32 fr2, u32 fifo, bool enable)
{
	u32 filter_select_bit = 0x00000001 << nr;

	/* Request initialization "enter". */
	CAN_FMR(canport) |= CAN_FMR_FINIT;

	/* Deactivate the filter. */
	CAN_FA1R(canport) &= ~filter_select_bit;

	if (scale_32bit) {
		/* Set 32-bit scale for the filter. */
		CAN_FS1R(canport) |= filter_select_bit;
	} else {
		/* Set 16-bit scale for the filter. */
		CAN_FS1R(canport) &= ~filter_select_bit;
	}

	if (id_list_mode) {
		/* Set filter mode to ID list mode. */
		CAN_FM1R(canport) |= filter_select_bit;
	} else {
		/* Set filter mode to id/mask mode. */
		CAN_FM1R(canport) &= ~filter_select_bit;
	}

	/* Set the first filter register. */
	CAN_FiR1(canport, nr) = fr1;

	/* Set the second filter register. */
	CAN_FiR2(canport, nr) = fr2;

	/* Select FIFO0 or FIFO1 as filter assignement. */
	if (fifo)
		CAN_FFA1R(canport) |= filter_select_bit;  /* FIFO1 */
	else
		CAN_FFA1R(canport) &= ~filter_select_bit; /* FIFO0 */

	if (enable)
		CAN_FA1R(canport) |= filter_select_bit; /* Activate filter. */

	/* Request initialization "leave". */
	CAN_FMR(canport) &= ~CAN_FMR_FINIT;
}

void can_filter_id_mask_16bit_init(u32 canport, u32 nr, u16 id1, u16 mask1,
				   u16 id2, u16 mask2, u32 fifo, bool enable)
{
	can_filter_init(canport, nr, false, false,
			((u32)id1 << 16) | (u32)mask1,
			((u32)id2 << 16) | (u32)mask2, fifo, enable);
}

void can_filter_id_mask_32bit_init(u32 canport, u32 nr, u32 id, u32 mask,
				   u32 fifo, bool enable)
{
	can_filter_init(canport, nr, true, false, id, mask, fifo, enable);
}

void can_filter_id_list_16bit_init(u32 canport, u32 nr, u16 id1, u16 id2,
				   u16 id3, u16 id4, u32 fifo, bool enable)
{
	can_filter_init(canport, nr, false, true,
			((u32)id1 << 16) | (u32)id2,
			((u32)id3 << 16) | (u32)id4, fifo, enable);
}

void can_filter_id_list_32bit_init(u32 canport, u32 nr, u32 id1, u32 id2,
				   u32 fifo, bool enable)
{
	can_filter_init(canport, nr, true, true, id1, id2, fifo, enable);
}

void can_enable_irq(u32 canport, u32 irq)
{
	CAN_IER(canport) |= irq;
}

void can_disable_irq(u32 canport, u32 irq)
{
	CAN_IER(canport) &= ~irq;
}

int can_transmit(u32 canport, u32 id, bool ext, bool rtr, u8 length, u8 *data)
{
	int ret = 0, i;
	u32 mailbox = 0;

	if ((CAN_TSR(canport) & CAN_TSR_TME0) == CAN_TSR_TME0) {
		ret = 0;
		mailbox = CAN_MBOX0;
	} else if ((CAN_TSR(canport) & CAN_TSR_TME1) == CAN_TSR_TME1) {
		ret = 1;
		mailbox = CAN_MBOX1;
	} else if ((CAN_TSR(canport) & CAN_TSR_TME2) == CAN_TSR_TME2) {
		ret = 2;
		mailbox = CAN_MBOX2;
	} else {
		ret = -1;
	}

	/* Check if we have an empty mailbox. */
	if (ret == -1)
		return ret;

	/* Clear stale register bits */
	CAN_TIxR(canport, mailbox) = 0;
	if (ext) {
		/* Set extended ID. */
		CAN_TIxR(canport, mailbox) |= id << CAN_TIxR_EXID_SHIFT;
		/* Set extended ID indicator bit. */
		CAN_TIxR(canport, mailbox) |= CAN_TIxR_IDE;
	} else {
		/* Set standard ID. */
		CAN_TIxR(canport, mailbox) |= id << CAN_TIxR_STID_SHIFT;
	}

	/* Set/clear remote transmission request bit. */
	if (rtr)
		CAN_TIxR(canport, mailbox) |= CAN_TIxR_RTR; /* Set */

	/* Set the DLC. */
	CAN_TDTxR(canport, mailbox) &= 0xFFFFFFF0;
	CAN_TDTxR(canport, mailbox) |= length & CAN_TDTxR_DLC_MASK;

	/* Set the data. */
	CAN_TDLxR(canport, mailbox) = 0;
	CAN_TDHxR(canport, mailbox) = 0;
	for (i = 0; (i < 4) && (i < length); i++)
		CAN_TDLxR(canport, mailbox) |= (u32)data[i] << (8 * i);
	for (i = 4; (i < 8) && (i < length); i++)
		CAN_TDHxR(canport, mailbox) |= (u32)data[i] << (8 * (i - 4));

	/* Request transmission. */
	CAN_TIxR(canport, mailbox) |= CAN_TIxR_TXRQ;

	return ret;
}

void can_fifo_release(u32 canport, u8 fifo)
{
	if (fifo == 0)
		CAN_RF0R(canport) |= CAN_RF1R_RFOM1;
	else
		CAN_RF1R(canport) |= CAN_RF1R_RFOM1;
}

void can_receive(u32 canport, u8 fifo, bool release, u32 *id, bool *ext,
		 bool *rtr, u32 *fmi, u8 *length, u8 *data)
{
	u32 fifo_id = 0;
	int i;

	if (fifo == 0)
		fifo_id = CAN_FIFO0;
	else
		fifo_id = CAN_FIFO1;

	/* Get type of CAN ID and CAN ID. */
	if (CAN_RIxR(canport, fifo_id) & CAN_RIxR_IDE) {
		*ext = true;
		/* Get extended CAN ID. */
		*id = ((CAN_RIxR(canport, fifo_id) & CAN_RIxR_EXID_MASK) >>
			CAN_RIxR_EXID_SHIFT);
	} else {
		*ext = false;
		/* Get standard CAN ID. */
		*id = ((CAN_RIxR(canport, fifo_id) & CAN_RIxR_STID_MASK) >>
			CAN_RIxR_STID_SHIFT);
	}

	/* Get request transmit flag. */
	if (CAN_RIxR(canport, fifo_id) & CAN_RIxR_RTR)
		*rtr = true;
	else
		*rtr = false;

	/* Get filter match ID. */
	*fmi = ((CAN_RDTxR(canport, fifo_id) & CAN_RDTxR_FMI_MASK) >
		CAN_RDTxR_FMI_SHIFT);

	/* Get data length. */
	*length = CAN_RDTxR(canport, fifo_id) & CAN_RDTxR_DLC_MASK;

	/* Get data. */
	for (i = 0; (i < 4) && (i < *length); i++)
		data[i] = (CAN_RDLxR(canport, fifo_id) >> (8 * i)) & 0xFF;

	for (i = 4; (i < 8) && (i < *length); i++)
		data[i] = (CAN_RDHxR(canport, fifo_id) >> (8 * (i - 4))) & 0xFF;

	/* Release the FIFO. */
	if (release)
		can_fifo_release(CAN1, 0);
}