libs
platform_specific_components
avr
libcanard
avr-can-lib
src
sja1000.c
Go to the documentation of this file.
1
// ----------------------------------------------------------------------------
2
/*
3
* Copyright (c) 2007 Fabian Greif, Roboterclub Aachen e.V.
4
* All rights reserved.
5
*
6
* Redistribution and use in source and binary forms, with or without
7
* modification, are permitted provided that the following conditions
8
* are met:
9
* 1. Redistributions of source code must retain the above copyright
10
* notice, this list of conditions and the following disclaimer.
11
* 2. Redistributions in binary form must reproduce the above copyright
12
* notice, this list of conditions and the following disclaimer in the
13
* documentation and/or other materials provided with the distribution.
14
*
15
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
19
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25
* SUCH DAMAGE.
26
*
27
* $Id$
28
*/
29
// ----------------------------------------------------------------------------
30
31
#include "
sja1000_private.h
"
32
#ifdef SUPPORT_FOR_SJA1000__
33
34
#if !SJA1000_MEMORY_MAPPED
35
#define _NOP() do { __asm__ __volatile__ ("nop"); } while (0)
36
37
void
sja1000_write(
uint8_t
address,
uint8_t
data)
38
{
39
// set address
40
SET
(SJA1000_ALE);
41
PORT
(SJA1000_DATA) = address;
42
_NOP();
43
RESET
(SJA1000_ALE);
44
45
// write data
46
PORT
(SJA1000_DATA) = data;
47
RESET
(SJA1000_WR);
48
_NOP();
49
SET
(SJA1000_WR);
50
}
51
52
uint8_t
sja1000_read(
uint8_t
address)
53
{
54
uint8_t
data;
55
56
// set address
57
SET
(SJA1000_ALE);
58
PORT
(SJA1000_DATA) = address;
59
_NOP();
60
RESET
(SJA1000_ALE);
61
DDR
(SJA1000_DATA) = 0;
62
63
// read data
64
RESET
(SJA1000_RD);
65
_NOP();
66
data =
PIN
(SJA1000_DATA);
67
SET
(SJA1000_RD);
68
DDR
(SJA1000_DATA) = 0xff;
69
70
return
data;
71
}
72
#endif
73
74
// ----------------------------------------------------------------------------
75
// useable can-bitrates (for calculation see http://www.kvaser.com/index.htm)
76
77
const
uint8_t
_sja1000_cnf[8][2]
PROGMEM
= {
78
// 10 kbps
79
{ 0xe7,
80
0x4d
81
},
82
// 20 kbps
83
{ 0xd3,
84
0x4d
85
},
86
// 50 kbps
87
{ 0xc7,
88
0x4d
89
},
90
// 100 kbps
91
{ 0xc3,
92
0x4d
93
},
94
// 125 kbps
95
{ (1<<
_SJW0
)|(1<<
_BRP1
)|(1<<
_BRP0
),
96
(1<<
TSEG13
)|(1<<
TSEG12
)|(1<<
TSEG20
)
97
},
98
// 250 kbps
99
{ (1<<
_SJW0
)|(1<<
_BRP0
),
100
(1<<
TSEG13
)|(1<<
TSEG12
)|(1<<
TSEG20
)
101
},
102
// 500 kbps
103
{ (1<<
_SJW0
),
104
(1<<
TSEG13
)|(1<<
TSEG12
)|(1<<
TSEG20
)
105
},
106
// 1 Mbps
107
{ (1<<
_SJW0
),
108
(1<<
TSEG12
)|(1<<
TSEG20
)
109
}
110
};
111
112
// ----------------------------------------------------------------------------
113
// init sja1000-interface
114
115
bool
sja1000_init(
uint8_t
bitrate)
116
{
117
if
(bitrate >= 8)
118
return
false
;
119
120
#if !SJA1000_MEMORY_MAPPED
121
SET
(SJA1000_WR);
122
SET
(SJA1000_RD);
123
RESET
(SJA1000_ALE);
124
RESET
(SJA1000_CS);
125
126
SET_OUTPUT
(SJA1000_WR);
127
SET_OUTPUT
(SJA1000_RD);
128
SET_OUTPUT
(SJA1000_ALE);
129
130
SET_OUTPUT
(SJA1000_CS);
131
DDR
(SJA1000_DATA) = 0xff;
132
#endif
133
134
// enter reset mode
135
sja1000_write(
MOD
, (1<<
RM
)|(1<<
AFM
));
136
137
// choose PeliCAN-Mode
138
sja1000_write(
CDR
, (1<<
CANMODE
) | SJA1000_CLOCK_REGISTER);
139
140
// select the bitrate configuration
141
sja1000_write(
BTR0
, pgm_read_byte(&_sja1000_cnf[bitrate][0]));
142
sja1000_write(
BTR1
, pgm_read_byte(&_sja1000_cnf[bitrate][1]));
143
144
// filter are not practical useable, so we disable them
145
sja1000_write(
AMR0
, 0xff);
146
sja1000_write(
AMR1
, 0xff);
147
sja1000_write(
AMR2
, 0xff);
148
sja1000_write(
AMR3
, 0xff);
149
150
// set output driver configuration
151
sja1000_write(
OCR
, (1<<
OCTP0
)|(1<<
OCTN0
)|(1<<
OCMODE1
));
152
153
// enable receive interrupt
154
sja1000_write(
IER
, (1<<
RIE
));
155
156
// leave reset-mode
157
sja1000_write(
MOD
, (1<<
AFM
));
158
159
return
true
;
160
}
161
162
#endif // SUPPORT_FOR_SJA1000__
OCTN0
#define OCTN0
Definition:
sja1000_defs.h:189
AMR1
#define AMR1
Definition:
sja1000_defs.h:69
AMR0
#define AMR0
Definition:
sja1000_defs.h:68
IER
#define IER
Definition:
sja1000_defs.h:53
PIN
#define PIN(x)
Definition:
utils.h:224
CDR
#define CDR
Definition:
sja1000_defs.h:51
RM
#define RM
Definition:
sja1000_defs.h:111
DDR
#define DDR(x)
Definition:
utils.h:223
AMR2
#define AMR2
Definition:
sja1000_defs.h:70
AFM
#define AFM
Definition:
sja1000_defs.h:108
_SJW0
#define _SJW0
Definition:
sja1000_defs.h:162
SET
@ SET
Definition:
lpc_types.h:62
RIE
#define RIE
Definition:
sja1000_defs.h:156
_BRP1
#define _BRP1
Definition:
sja1000_defs.h:167
OCR
#define OCR
Definition:
sja1000_defs.h:50
uavcan::uint8_t
std::uint8_t uint8_t
Definition:
std.hpp:24
TSEG13
#define TSEG13
Definition:
sja1000_defs.h:177
BTR0
#define BTR0
Definition:
sja1000_defs.h:48
MOD
#define MOD
Definition:
sja1000_defs.h:41
OCMODE1
#define OCMODE1
Definition:
sja1000_defs.h:191
SET_OUTPUT
#define SET_OUTPUT(x)
Definition:
utils.h:231
CANMODE
#define CANMODE
Bitdefinition von CDR.
Definition:
sja1000_defs.h:230
TSEG20
#define TSEG20
Definition:
sja1000_defs.h:176
TSEG12
#define TSEG12
Definition:
sja1000_defs.h:178
BTR1
#define BTR1
Definition:
sja1000_defs.h:49
PORT
#define PORT(x)
Definition:
utils.h:222
AMR3
#define AMR3
Definition:
sja1000_defs.h:71
OCTP0
#define OCTP0
Definition:
sja1000_defs.h:188
RESET
@ RESET
Definition:
lpc_types.h:62
PROGMEM
const uint8_t can_filter[] PROGMEM
Definition:
main.c:60
sja1000_private.h
SJA1000 Interface.
_BRP0
#define _BRP0
Definition:
sja1000_defs.h:168
uavcan_communicator
Author(s):
autogenerated on Fri Dec 13 2024 03:10:03