forked from otto-tom/can_axi4lite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
181 lines (162 loc) · 6.05 KB
/
main.c
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
/* ******************************************************************************\
* /------------------------------------------------------------------------------/
* |-- Title : ISCA CAN applicatio example
* |-- Project : CAN-bus Controller
* |#----------------------------------------------------------------------------#
* |-- File : main.c
* |-- Author : Othon Tomoutzoglou <[email protected]>
* |-- Company : Hellenic Mediterranean University, department of
* |-- Electrical & Computer Engineering, ISCA-lab
* |-- URL : http://isca.hmu.gr/
* |-- Created : 2018-10-22
* |-- Last update: 2019-05-10
* |-- License :
* |-- This program is free software: you can redistribute it and/or modify
* |-- it under the terms of the GNU General Public License as published by
* |-- the Free Software Foundation, either version 3 of the License, or
* |-- (at your option) any later version.
* |--
* |-- This program 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 General Public License for more details.
* |--
* |-- You should have received a copy of the GNU General Public License
* |-- along with this program. If not, see <https://www.gnu.org/licenses/>.
* |--
* |-- Platform :
* |-- Standard :
* |#----------------------------------------------------------------------------#
* |-- Description: Before using define the in file CAN0_ROLE, CAN0_BASEADDR,
* | RX_QUEUE_SIZE, alongside APPRISE_MODE and CAN_MODE in
* | ISCA_CAN_CFG.h. The later can optionally be defined as
* | compiler options, e.g., $gcc ... -DCAN_MODE=CAN_2A.
* | Available Options List:
* | - APPRISE_MODE: APP_POLL(0) / APP_IRQ(1), poll or irq mode
* | - CAN_MODE: CAN_2A(0) / CAN_2B(1), basic or extended mode
* | - RX_QUEUE_SIZE: Integer, SW RX CAN frames queue size
* | attached to the controller instance
* | - CAN0_BASEADDR: Integer, CAN controller's physical address
* | - CAN0_ROLE: TX(0) / RX(1), main TXes or RXes a CAN frame
* |#----------------------------------------------------------------------------#
* |-- Copyright (c) 2018
* |#----------------------------------------------------------------------------#
* |-- Revisions :
* |-- Date Version Author Description
* |-- 2018-10-22 1.0 OtTo Created
* \#-----------------------------------------------------------------------------\
* *******************************************************************************/
/** \mainpage CAN controller documentation
*
* Use the menus or the search field to navigate and get informed about the provided
* software.
*
* @file main.c
*
* @brief Provides tools to initialize and use a preallocated
* circular queue pointers
*
* @author Othon Tomoutzoglou
*
* Contact: <[email protected]>
*
* @version 1.0
*
*/
/******
* HEADERS
******/
#include "ISCA_CAN_API.h"
/******
* DEFINITIONS
******/
#define CAN_TX (0U)
#define CAN_RX (1U)
#define CAN0_ROLE CAN_TX // 0:TX, 1:RX
#define CAN0_BASEADDR (0xA0000000U)
#define RX_QUEUE_SIZE (15U)
/******
* VARIABLES
******/
static can_ctrl_s can_ctrl;
static can_frame_s can_frame;
/******
* FUNCTIONS DECLARATION
******/
int config_can(can_ctrl_s *CanInstancePtr);
void can_example(can_ctrl_s *CanInstancePtr, can_frame_s *CanFrm);
/******
* MAIN
******/
int main(void) {
int status;
/*
* configure CAN controller
*/
status = config_can(&can_ctrl);
if ( status != ISCA_CAN_OK) {
return ISCA_CAN_ERROR;
}
/*
* Run application
*/
can_example(&can_ctrl, &can_frame);
return 0;
}
/******
* FUNCTIONS DEFINITION
******/
int config_can(can_ctrl_s *CanInstancePtr) {
/**
* Initialize can structure
* - Operate at 1000kbp (if input clock 100MHz)
* - Drop any frame if header MSB is equal to '0'
* - Enable interrupts
* - Enable extended frame format (29-bit header) if CAN_MODE=CAN_2B
**/
CanInstancePtr->addr = CAN0_BASEADDR;
CanInstancePtr->brp = 9U;
CanInstancePtr->tseg1 = 1U;
CanInstancePtr->tseg2 = 1U;
CanInstancePtr->sjw = 1U;
CanInstancePtr->irqs_en.rx = CAN_IRQ_ON; /// Enable RX interrupt
CanInstancePtr->irqs_en.tx = CAN_IRQ_OFF; /// Disable TX interrupt
#if !(CAN_MODE == CAN_2B)
CanInstancePtr->mask = 0x3FF;
CanInstancePtr->code = 0x400;
CanInstancePtr->irqs_en.err0 = IRQ_ON; /// Enable RX FIFO overrun interrupt
CanInstancePtr->irqs_en.err1 = IRQ_ON; /// Enable Error interrupt
#else
CanInstancePtr->frm_md = CAN_FRAME_EXT;
CanInstancePtr->mask = 0x0FFFFFFF; // check only ID[28]
CanInstancePtr->code = 0x10000000; // check if ID[28]='1'
CanInstancePtr->irqs_en.err0 = CAN_IRQ_ON; /// Enable Error warning interrupt
CanInstancePtr->irqs_en.err1 = CAN_IRQ_ON; /// Enable RX FIFO overrun interrupt
CanInstancePtr->irqs_en.err2 = CAN_IRQ_ON; /// Enable Error passive IRQ interrupt
CanInstancePtr->irqs_en.err3 = CAN_IRQ_ON; /// Enable Arbitration lost interrupt
CanInstancePtr->irqs_en.err4 = CAN_IRQ_ON; /// Enable Bus error interrupt
#endif // !(CAN_MODE == CAN_2B)
#if !(APPRISE_MODE==APP_IRQ)
CanInstancePtr->irq = CAN_IRQ_OFF;
#else
CanInstancePtr->irq = CAN_IRQ_ON;
#endif // !(APPRISE_MODE==APP_IRQ)
/*Initialize the controller and exit*/
return lbr_isca_can_init(CanInstancePtr, RX_QUEUE_SIZE);
}
void can_example(can_ctrl_s *CanInstancePtr, can_frame_s *CanFrm) {
#if !(CAN0_ROLE==CAN_RX)
/*TX*/
CanFrm->ID = 0x10AB0003;
#if (CAN_MODE == CAN_2B)
CanFrm->IDE = CAN_FRAME_EXT;
#endif // (CAN_MODE == CAN_2B)
CanFrm->DLC = 2U;
CanFrm->DATA[0] = 0xAA;
CanFrm->DATA[1] = 0xBB;
lbr_isca_can_transmit_pkt(CanInstancePtr, CanFrm);
#else
/*RX*/
lbr_isca_can_receive_pkt(CanInstancePtr, CanFrm);
#endif // !(CAN0_ROLE==RX)
}