📄 artx_can.lst
字号:
ARM COMPILER V2.32a, ARTX_CAN 15/03/07 08:58:24 PAGE 1
ARM COMPILER V2.32a, COMPILATION OF MODULE ARTX_CAN
OBJECT MODULE PLACED IN .\obj\ARTX_CAN.obj
COMPILER INVOKED BY: C:\Keil\ARM\BIN\CA.exe ARTX_CAN.c THUMB OPTIMIZE(7,SPEED) DEBUG CODE PRINT(.\LST\ARTX_CAN.LST) TABS
-(3) OBJECT(.\obj\ARTX_CAN.obj)
stmt level source
1 /*----------------------------------------------------------------------------
2 * A R T X - C A N D r i v e r - G e n e r i c L a y e r
3 *----------------------------------------------------------------------------
4 * Name: ARTX_CAN.c
5 * Purpose: CAN Generic Driver
6 * Rev.: V0.20 / october-14-2005
7 *----------------------------------------------------------------------------
8 * This code is part of the ARTX-ARM kernel package of Keil Software.
9 * Copyright (c) 2004-2005 Keil Software. All rights reserved.
10 *---------------------------------------------------------------------------*/
11
12 #include <ARTX.h> /* ARTX kernel functions & defines */
13 #include "CAN_Cfg.h" /* CAN Configuration */
14 #include "ARTX_CAN.h" /* CAN Generic functions & defines */
15 #include "CAN_Hw.h" /* CAN hw specific functions & defines */
16
17
18 /* Declare memory pool for CAN messages, both transmit and receive */
19 CAN_msgpool_declare(CAN_mpool,CAN_CTRL_MAX_NUM*(CAN_No_SendObjects+CAN_No_ReceiveObjects));
20
21 /* Declare mailbox, for CAN transmit messages */
22 mbx_arr_declare(MBX_tx_ctrl,CAN_CTRL_MAX_NUM,CAN_No_SendObjects);
23
24 /* Declare mailbox, for CAN receive messages */
25 mbx_arr_declare(MBX_rx_ctrl,CAN_CTRL_MAX_NUM,CAN_No_ReceiveObjects);
26
27 /* Flags signaling writing to CAN hardware */
28 U8 wr_to_CAN_hw[CAN_CTRL_MAX_NUM];
29
30
31 /*----------------------------------------------------------------------------
32 * CAN ARTX Generic Driver Functions
33 *----------------------------------------------------------------------------
34 * Functions implemented in this module:
35 * CAN_ERROR CAN_mem_init (void);
36 * CAN_ERROR CAN_setup (void)
37 * CAN_ERROR CAN_init (U32 ctrl, U32 baudrate)
38 * CAN_ERROR CAN_start (U32 ctrl)
39 * static CAN_ERROR CAN_push (U32 ctrl, CAN_msg *msg, U16 timeout)
40 * CAN_ERROR CAN_send (U32 ctrl, CAN_msg *msg, U16 timeout)
41 * CAN_ERROR CAN_request (U32 ctrl, CAN_msg *msg, U16 timeout)
42 * CAN_ERROR CAN_set (U32 ctrl, CAN_msg *msg, U16 timeout)
43 * static CAN_ERROR CAN_pull (U32 ctrl, CAN_msg *msg, U16 timeout)
44 * CAN_ERROR CAN_receive (U32 ctrl, CAN_msg *msg, U16 timeout)
45 * CAN_ERROR CAN_rx_object (U32 ctrl, U32 ch, U32 id, CAN_FORMAT format)
46 * CAN_ERROR CAN_tx_object (U32 ctrl, U32 ch, CAN_FORMAT format)
47 *---------------------------------------------------------------------------*/
48
49
50 /*--------------------------- CAN_init --------------------------------------
51 *
52 * The first time this function is called initialize the memory pool for
53 * CAN messages and setup CAN controllers hardware
54 *
55 * Initialize mailboxes for CAN messages and initialize CAN controller
56 *
57 * Parameter: ctrl: Index of the hardware CAN controller (1 .. x)
58 * baudrate: Baudrate
ARM COMPILER V2.32a, ARTX_CAN 15/03/07 08:58:24 PAGE 2
59 *
60 * Return: CAN_ERROR: Error code
61 *---------------------------------------------------------------------------*/
62
63 CAN_ERROR CAN_init (U32 ctrl, U32 baudrate) {
64 1 static U8 first_run_flag = 0;
65 1 CAN_ERROR error_code;
66 1 U32 ctrl0 = ctrl-1; /* Controller index 0 .. x-1 */
67 1
68 1 /* When function is called for the first time it will initialize and setup
69 1 all of the resources that are common to CAN functionality */
70 1 if (first_run_flag == 0) {
71 2 first_run_flag = 1;
72 2 if (_init_box (CAN_mpool, sizeof(CAN_mpool), sizeof(CAN_msg)) == 1)
73 2 return CAN_MEM_POOL_INIT_ERROR;
74 2 }
75 1
76 1 os_mbx_init (MBX_tx_ctrl[ctrl0], sizeof(MBX_tx_ctrl[ctrl0]));
77 1 os_mbx_init (MBX_rx_ctrl[ctrl0], sizeof(MBX_rx_ctrl[ctrl0]));
78 1
79 1 error_code = CAN_hw_setup ();
80 1 if (error_code != CAN_OK)
81 1 return error_code;
82 1
83 1 return (CAN_hw_init (ctrl, baudrate));
84 1 }
85
86
87 /*--------------------------- CAN_start -------------------------------------
88 *
89 * Start CAN controller (enable it to participate on CAN network)
90 *
91 * Parameter: ctrl: Index of the hardware CAN controller (1 .. x)
92 *
93 * Return: CAN_ERROR: Error code
94 *---------------------------------------------------------------------------*/
95
96 CAN_ERROR CAN_start (U32 ctrl) {
97 1 return (CAN_hw_start (ctrl));
98 1 }
99
100
101 /*--------------------------- CAN_push --------------------------------------
102 *
103 * Send CAN_msg if hardware is free for sending, otherwise push message to
104 * message queue to be sent when hardware becomes free
105 *
106 * Parameter: ctrl: Index of the hardware CAN controller (1 .. x)
107 * msg: Pointer to CAN message to be sent
108 * timeout: Timeout value for message sending
109 *
110 * Return: CAN_ERROR: Error code
111 *---------------------------------------------------------------------------*/
112
113 static CAN_ERROR CAN_push (U32 ctrl, CAN_msg *msg, U16 timeout) {
114 1 CAN_msg *ptrmsg;
115 1 U32 ctrl0 = ctrl-1; /* Controller index 0 .. x-1 */
116 1
117 1 if (CAN_hw_tx_empty (ctrl) == 0) { /* Transmit hardware free for sending */
118 2 wr_to_CAN_hw[ctrl0] = 1; /* Writing to transmit hardware */
119 2 CAN_hw_wr (ctrl, msg); /* Send message */
120 2 wr_to_CAN_hw[ctrl0] = 0; /* Writing to transmit hardware finished */
121 2 }
122 1 else { /* If hardware for sending is busy */
123 2 /* Write the message to send mailbox if there is room for it */
124 2 ptrmsg = _alloc_box (CAN_mpool);
ARM COMPILER V2.32a, ARTX_CAN 15/03/07 08:58:24 PAGE 3
125 2 if (ptrmsg != NULL)
126 2 *ptrmsg = *msg;
127 2 /* If message hasn't been received but timeout expired, deallocate memory*/
128 2 if (os_mbx_send (MBX_tx_ctrl[ctrl0], ptrmsg, timeout) == OS_R_TMO) {
129 3 if (_free_box (CAN_mpool, ptrmsg) == 1)
130 3 return CAN_DEALLOC_MEM_ERROR;
131 3
132 3 return CAN_TIMEOUT_ERROR;
133 3 }
134 2 else
135 2 return CAN_ALLOC_MEM_ERROR;
136 2 }
137 1 return CAN_OK;
138 1 }
139
140
141 /*--------------------------- CAN_send --------------------------------------
142 *
143 * Send DATA FRAME message, see CAN_push function comment
144 *
145 * Parameter: ctrl: Index of the hardware CAN controller (1 .. x)
146 * msg: Pointer to CAN message to be sent
147 * timeout: Timeout value for message sending
148 *
149 * Return: CAN_ERROR: Error code
150 *---------------------------------------------------------------------------*/
151
152 CAN_ERROR CAN_send (U32 ctrl, CAN_msg *msg, U16 timeout) {
153 1 msg->type = DATA_FRAME;
154 1
155 1 return (CAN_push (ctrl, msg, timeout));
156 1 }
157
158
159 /*--------------------------- CAN_request -----------------------------------
160 *
161 * Send REMOTE FRAME message, see CAN_push function comment
162 *
163 * Parameter: ctrl: Index of the hardware CAN controller (1 .. x)
164 * msg: Pointer to CAN message to be sent
165 * timeout: Timeout value for message sending
166 *
167 * Return: CAN_ERROR: Error code
168 *---------------------------------------------------------------------------*/
169
170 CAN_ERROR CAN_request (U32 ctrl, CAN_msg *msg, U16 timeout) {
171 1 msg->type = REMOTE_FRAME;
172 1
173 1 return (CAN_push (ctrl, msg, timeout));
174 1 }
175
176
177 /*--------------------------- CAN_set ---------------------------------------
178 *
179 * Set a message that will automatically be sent as an answer to REMOTE
180 * FRAME message
181 *
182 * Parameter: ctrl: Index of the hardware CAN controller (1 .. x)
183 * msg: Pointer to CAN message to be set
184 * timeout: Timeout value for message to be set
185 *
186 * Return: CAN_ERROR: Error code
187 *---------------------------------------------------------------------------*/
188
189 CAN_ERROR CAN_set (U32 ctrl, CAN_msg *msg, U16 timeout) {
190 1 S32 i = timeout;
ARM COMPILER V2.32a, ARTX_CAN 15/03/07 08:58:24 PAGE 4
191 1 U32 result = 0;
192 1 U32 ctrl0 = ctrl-1; /* Controller index 0 .. x-1 */
193 1
194 1 do {
195 2 if (CAN_hw_tx_empty (ctrl) == CAN_OK) { /* Transmit hardware free */
196 3 wr_to_CAN_hw[ctrl0] = 1; /* Writing to transmit hardware */
197 3 result = CAN_hw_set (ctrl, msg); /* Set message */
198 3 wr_to_CAN_hw[ctrl0] = 0; /* Writing to tx hardware finished */
199 3 return CAN_OK;
200 3 }
201 2 if (timeout == 0xffff) /* Indefinite wait */
202 2 i++;
203 2 i--;
204 2 os_dly_wait (1); /* Wait 1 timer tick */
205 2 } while (i >= 0);
206 1
207 1 return CAN_TIMEOUT_ERROR; /* CAN message not set */
208 1 }
209
210
211 /*--------------------------- CAN_pull --------------------------------------
212 *
213 * Pull first received and unread CAN_msg from receiving message queue
214 *
215 * Parameter: ctrl: Index of the hardware CAN controller (1 .. x)
216 * msg: Pointer where CAN message will be read
217 * timeout: Timeout value for message receiving
218 *
219 * Return: CAN_ERROR: Error code
220 *---------------------------------------------------------------------------*/
221
222
223 static CAN_ERROR CAN_pull (U32 ctrl, CAN_msg *msg, U16 timeout) {
224 1 CAN_msg *ptrmsg;
225 1 U32 ctrl0 = ctrl-1; /* Controller index 0 .. x-1 */
226 1
227 1 /* Wait for received message in mailbox */
228 1 if (os_mbx_wait (MBX_rx_ctrl[ctrl0], &ptrmsg, timeout) == OS_R_TMO)
229 1 return CAN_TIMEOUT_ERROR;
230 1
231 1 /* Copy received message from mailbox to address given in function parameter msg */
232 1 *msg = *ptrmsg;
233 1
234 1 /* Free box where message was kept */
235 1 if (_free_box (CAN_mpool, ptrmsg) == 1)
236 1 return CAN_DEALLOC_MEM_ERROR;
237 1
238 1 return CAN_OK;
239 1 }
240
241
242 /*--------------------------- CAN_receive -----------------------------------
243 *
244 * Read received message, see CAN_pull function comment
245 *
246 * Parameter: ctrl: Index of the hardware CAN controller (1 .. x)
247 * msg: Pointer where CAN message will be read
248 * timeout: Timeout value for message receiving
249 *
250 * Return: CAN_ERROR: Error code
251 *---------------------------------------------------------------------------*/
252
253 CAN_ERROR CAN_receive (U32 ctrl, CAN_msg *msg, U16 timeout) {
254 1 return (CAN_pull (ctrl, msg, timeout));
255 1 }
256
ARM COMPILER V2.32a, ARTX_CAN 15/03/07 08:58:24 PAGE 5
257
258 /*--------------------------- CAN_rx_object ---------------------------------
259 *
260 * Enable reception of messages on specified controller and channel with
261 * specified identifier
262 *
263 * Parameter: ctrl: Index of the hardware CAN controller (1 .. x)
264 * ch: Channel for the message transmission
265 * id: CAN message identifier
266 * CAN_FORMAT: Format of CAN identifier (standard or extended)
267 *
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -