NetBurner 3.5.8
PDF Version
multican.h
1/*NB_REVISION*/
2
3/*NB_COPYRIGHT*/
4
10
11#ifndef _MULTICAN_H
12#define _MULTICAN_H
13
14#ifdef DOXYGEN_STUFF
18namespace canMCF5441x
19{
20#endif
21
22
34
35#if (defined MOD5441X)
36#define DEFAULT_CAN_MOD (0)
37#elif (defined NANO54415)
38#define DEFAULT_CAN_MOD (1)
39#elif (defined SB800EX)
40#define DEFAULT_CAN_MOD (0)
41#endif
42
43#define CAN_OK (0)
44#define CAN_RATE_FAIL (-1)
45#define CAN_ALREAD_OPEN (-2)
46#define CAN_CHANNEL_USED (-3)
47#define CAN_CHANNEL_NOT_USED (-4)
48#define CAN_TIMEOUT (-5)
49
50#define DONT_WAIT (0xFFFF) // Non-blocking value for CanRxMessage and GetNewMessage timeout parameter
51
52struct PrivateCanData;
53
57class CanRxMessage
58{
59 private:
60 PrivateCanData *pData;
61 /* Private constructor used for received frames */
62 CanRxMessage(PrivateCanData *pData);
63
64 /* Helper method for the RTR constructors */
65 void MultiCanSendRTR(int moduleNum, uint32_t id, uint16_t timeout, uint8_t length);
66
67 inline void CanSendRTR(uint32_t id, uint16_t timeout, uint8_t length) { return MultiCanSendRTR(DEFAULT_CAN_MOD, id, timeout, length); }
68
69 public:
73 uint8_t GetLength();
74
83 uint8_t GetData(uint8_t *buffer, uint8_t max_len);
84
88 uint32_t GetId();
89
93 uint16_t GetTimeStamp();
94
101 BOOL IsValid();
102
103 /* Constructors */
104
115 CanRxMessage(OS_FIFO *pFifo, uint16_t timeout);
116
130 CanRxMessage(int moduleNum, uint32_t id, uint16_t timeout);
131
132 CanRxMessage(int moduleNum, uint32_t id, uint16_t timeout, uint8_t length);
133
138
148 BOOL GetNewMessage(OS_FIFO *pFifo, uint16_t timeout);
149};
150
151/**********************************************************************************
152
153 A special note about CAN identifiers
154
155**********************************************************************************
156 Can identifiers come in two flavors, Normal (11 bits ) and Extended. (29 bits)
157 In this software API we always refer to can identifiers as 32 bit uint32_tS.
158 A 32 bit uint32_t is bigger than either Identifier.
159
160 A Normal identifier will always have bits 0 to 17 as zero
161
162 An extended identifier can have bits 0 to 17 low. So extended identifiers
163 that are received will have bit 29 set to 1. Any ID input into the
164 system will be treated as extended if bit 29 is set or if bits
165 0 to 17 are not zero.
166
167 The following MACROS can help work with this ID scheme.
168 */
169
170/* The single bit used by this API to indicate an extended ID */
171#define CAN_EXTENDED_ID_BIT (0x20000000)
172
173/* This macro takes and id and returs an ID that is guarenteed
174to be treated as an extended ID by the system
175Make an id that is extended from either extended or normal */
176#define ExtToNbId(id) (id | CAN_EXTENDED_ID_BIT)
177
178/*Make Normal Id make an ID set from a normal id in the range 0 to 2048 */
179#define NormToNbId(id) ((id & 0x7ff) << 18)
180
181/* Is the ID extended ? returns non zero if the id passed in was an extended ID */
182#define IsNBIdExt(id) ((id & (CAN_EXTENDED_ID_BIT | 0x3FFFF)) != 0)
183
184/* Strip the extra flag remove the API extended flage from the ID*/
185#define NbToExtId(id) (id & 0x1FFFFFFF)
186
187/* Shift a Normal ID so it hase value 0 to 1023
188Some CAN systems will treat normal ID's as an integer from 0 to 2048
189Other systems may treat normal ID's as 28 bit values wheer the bottom 17 bits are zero
190This MACRO will convert our Normal ID format into the 0 to 2048 format.*/
191#define NbToNormId(id) ((id >> 18) & 0x7FF)
192
193/* Initialize the CAN system
194parameters:
195
196 bit_rate is the bitrate to run the CAN system at.
197 The system will get as close as possible, but 1000000, 500000, 250000 and 125000 are
198 the only values that are known to work.
199
200 Global_Mask is the mask used to mask received ID's a bit =0 is don't care, 1= care.
201
202 irq_level The interrupt level you want the CAN system to operate at.
203
204Returns
205 CAN_OK on success.
206 CAN_RATE_FAIL if the bit rate could not be set withing 1.5%
207 CAN_ALREADOPEN if the CAN system is already running. Youmust call CanShutDown first.
208*/
209int MultiCanInit(int moduleNum, uint32_t bit_rate, uint32_t Global_Mask, uint8_t irq_level = 4);
210
211/* Shut down the CAN system */
212void MultiCanShutDown(int moduleNum);
213
214/* Change the global receive mask after the CAN system is started. */
215void MultiCanChangeGlobalMask(int moduleNum, uint32_t Global_Mask);
216
217/* The CAN system has 16 availible channels this tells how many are currently in use */
218int MultiCanFreeCanChannels(int moduleNum);
219
220/* This tells if a specific channel is currently free */
221BOOL MultiCanIsChannelFree(int moduleNum, int channel);
222
223/* RegisterCanRxFifo and RegisterCanSpecialRxFifo
224Tell the CAN system to start listening for a specific CAN ID.
225Any incoming CAN frames that match the id as set by the apropriate mask will
226be placed into the fifo.
227
228Parameters:
229 uint32_t id The identifer to match on received frames. This is modified by
230 the global mask, or in the case of the RegisterCanSpecial
231 the passed in mask.
232
233 uint32_t spl_mask Only used by RegisterCanSpecialRxFifo. There are only
234 two channels availible for use with the speical mask
235 so use this call sparinging and only if really needed.
236
237 OSFifo * pFifo The Fifo used to communicate between the CAN subsystem and
238 the CanRxMessageClass. This FIFO must be initialized before use.
239 The same fifo can be passed to multiple receive regestration functions.
240
241 int channel The Channel to place the receive request. A value of -1 allows the system to
242 select an unused channel.
243
244
245Returns
246 Any value >=0 the channel this request is assigned to. This value must be stored to
247 later call UnRegisterCanFifo.
248 CAN_CHANNEL_USED If the channel is used or there are no free channels.
249
250*/
251int RegisterMultiCanRxFifo(int moduleNum, uint32_t id, OS_FIFO *pFifo, int channel = -1);
252int RegisterMultiCanSpecialRxFifo(int moduleNum, uint32_t id, uint32_t spl_mask, OS_FIFO *pFifo, int channel = -1);
253
254/* disconnect a receiver channel form a Fifo
255Parameters:
256 int channel The channel to remove.
257
258returns
259 CAN_OK if succesful.
260 CAN_CHANNEL_NOT_USED if the channel was not currently in use
261*/
262int UnRegisterMultiCanFifo(int moduleNum, int channel);
263
264/* Send a message
265Parameters:
266 uint32_t id The Identifier to send. See the earlier discussion of identifiers.
267 and the identifier macros.
268
269 uint8_t * data A pointer to the data to send
270
271 uint8_t len The length of the Data must be <=8.
272
273 uint16_t timeout How lont to wait for confirmation it sent.
274 0 = wait forevver.
275 0xFFFF = don't wait at all.
276
277 int channel The channel to use. A value of -1
278 will allow the system to pick and unused channel.
279
280Returns
281 CAN_OK If the message was sent
282 CAN_CHANNEL_USED Can't send because the channel was already in use or no channels availible.
283 CAN_TIMEOUT Did not send in the time alotted
284*/
285int MultiCanSendMessage(int moduleNum, uint32_t id, uint8_t *data, uint8_t len, uint16_t timeout, int channel = -1);
286
287/*RTR messages
288Can can be configured to send messages in response to memessage requests.
289The next three functions handle that functionality */
290
291/* Set RTRMessage
292This sets up a message to be sent in response to an incoming message that matches the
293RTR message id. The system will continue to respond with this message forever.
294
295Parameters:
296 uint32_t id. The id to respond to and the id responded with.
297 uint8_t * data The data to respond with
298 uint8_t len The length of the responding data.
299 int channel The channel to use, or -1 to let the system pic one.
300
301Returns:
302 value >0 the channel assigned to this tsk or the channel requested.
303 CAN_CHANNEL_USED if the channel is already used or no free channels are availible.
304*/
305int MultiCanSetRTRMessage(int moduleNum, uint32_t id, uint8_t *data, uint8_t len, int channel = -1);
306
307/*ReplaceRTRMessage
308Replace the outgoing message in an RTR channel already set up
309with a call to SetRTRMessage. Use of this function can
310cause a brief instant (a few uSec.) when no message response would be sent
311to an incoming request. If this is unacceptible register two identical RTR
312messages and change them in sequence.
313
314
315Parameters:
316 int channel The channel value returned from the SetRTRMessage call
317 uint8_t * data the data to send.
318 uint8_t len the length of the data.
319
320
321Returns
322 CAN_OK if the replacement succeded.
323 CAN_CHANNEL_NOT_USED if the passed in channed was not set up for RTR messages
324
325*/
326int MultiCanReplaceRTRMessage(int moduleNum, int channel, uint8_t *data, uint8_t len);
327
328/* Stop an RTR message
329Parameters:
330 int channel The channel value returned from the SetRTRMessage call
331
332Returns
333 CAN_OK if the replacement succeded.
334 CAN_CHANNEL_NOT_USED if the passed in channed was not set up for RTR messages
335
336*/
337int MultiCanStopRTRMessage(int moduleNum, int channel);
338
339// Should a user include functions used in the canif.h file, these inline functions
340// will call the MultiCan module function associated with that function.
341
342inline int CanInit(uint32_t bit_rate, uint32_t Global_Mask, uint8_t irq_level = 4)
343{
344 return MultiCanInit(DEFAULT_CAN_MOD, bit_rate, Global_Mask, irq_level);
345}
346
347inline void CanShutDown(void)
348{
349 return MultiCanShutDown(DEFAULT_CAN_MOD);
350}
351
352inline void ChangeGlobalMask(uint32_t Global_Mask)
353{
354 return MultiCanChangeGlobalMask(DEFAULT_CAN_MOD, Global_Mask);
355}
356
357inline int RegisterCanRxFifo(uint32_t id, OS_FIFO *pFifo, int channel = -1)
358{
359 return RegisterMultiCanRxFifo(DEFAULT_CAN_MOD, id, pFifo, channel);
360}
361
362inline int RegisterCanSpecialRxFifo(uint32_t id, uint32_t spl_mask, OS_FIFO *pFifo, int channel = -1)
363{
364 return RegisterMultiCanSpecialRxFifo(DEFAULT_CAN_MOD, id, spl_mask, pFifo, channel);
365}
366
367inline int UnRegisterCanFifo(int channel)
368{
369 return UnRegisterMultiCanFifo(DEFAULT_CAN_MOD, channel);
370}
371
372inline int SendMessage(uint32_t id, uint8_t *data, uint8_t len, uint16_t timeout, int channel = -1)
373{
374 return MultiCanSendMessage(DEFAULT_CAN_MOD, id, data, len, timeout, channel);
375}
376
377inline int SetRTRMessage(uint32_t id, uint8_t *data, uint8_t len, int channel = -1)
378{
379 return MultiCanSetRTRMessage(DEFAULT_CAN_MOD, id, data, len, channel);
380}
381
382inline int ReplaceRTRMessage(int channel, uint8_t *data, uint8_t len)
383{
384 return MultiCanReplaceRTRMessage(DEFAULT_CAN_MOD, channel, data, len);
385}
386
387inline int StopRTRMessage(int channel)
388{
389 return MultiCanStopRTRMessage(DEFAULT_CAN_MOD, channel);
390}
391
392/* The CAN system has 16 availible channels this tells how many are currently in use */
393inline int FreeCanChannels()
394{
395 return MultiCanFreeCanChannels(DEFAULT_CAN_MOD);
396}
397
398/* This tells if a specific channel is currently free */
399inline BOOL IsChannelFree(int channel)
400{
401 return MultiCanIsChannelFree(DEFAULT_CAN_MOD, channel);
402}
403
404/*******************************************************************************
405 Example #1
406
407 To set up to receive frames:
408
409
410OS_FIFO fifo;
411OSFifoInit(&fifo);
412
413//REgister to listen for CAN data
414//We will listed for frames with a normal is of 123
415int chan1=RegisterCanRxFifo( MakeIdFromNormal(123),& fifoifo);
416
417//We will also listen for the extended frame id of 0x1234
418int chan2=RegisterCanRxFifo( ox1234,& fifoifo);
419
420if ((chan1 >0) || (chan2> 0)_
421 {//We succeded in registering the fifo
422 while(1)
423 {
424 CanRxMessage can_msg(&fifo,30*TICKS_PER_SECOND); //Wait up to 20 seconds
425 if (can_msg.IsValid())
426 {uint8_t buffer[8];
427 uint8_t len;
428 len=can_msg.GetData(buffer,8);
429
430 iprintf("got a can message of %d bytes from ID %08X \r\n",len ,can_msg.GetId());
431 iprintf("Data = [");
432 for (int i=0; i<len; i++)
433 iprintf("%02X ",buffer[i]);
434 iprintf("]\r\n");
435 }
436 else
437 iprintf("CAN received timed out ");
438 }
439 }
440
441**************************************************************************************
442End of example #1
443**************************************************************************************/
444
445/*******************************************************************************
446 Example #2
447 Setup an automatic response buffer (an RTR buffer)
448
449// we want to setup an automatic response message (RTR
450//We will update it every 5 seconds
451uint16_t value=GetValue();
452int RTRChan=SetRTRMessage(0x5678,(puint8_t),&value,sizeof(value));
453
454
455while (1)
456{
457//Every 5 seconds update the value stored in the message
458OSTimeDly(5 * TICKS_PER_SECOND);
459
460//Get the new value
461value=GetValue();
462//Store it in the automatic response channel
463ReplaceRTRMessage(RTRChan,(puint8_t),&value,sizeof(value) );
464}
465
466
467**************************************************************************************
468End of example #2
469**************************************************************************************/
470
472
473#ifdef DOXYGEN_STUFF
474} // namespace
475#endif
476
477#endif
BOOL IsValid()
Check to verify the CanRxMessage is a valid message.
uint8_t GetData(uint8_t *buffer, uint8_t max_len)
Copy the data in the message up to max_len.
CanRxMessage(OS_FIFO *pFifo, uint16_t timeout)
Build a CanRxMessage from a FIFO.
BOOL GetNewMessage(OS_FIFO *pFifo, uint16_t timeout)
Get a new message from the FIFO. If no message is available, wait up to the timeout for one to be rec...
CanRxMessage(int moduleNum, uint32_t id, uint16_t timeout)
Constructor that sends out a RTR request to ID and waits for a response.
uint32_t GetId()
Returns the ID of the message.
uint16_t GetTimeStamp()
Returns the time stamp of the message.
uint8_t GetLength()
Returns the amount of data stored in the message.
canMCF5441x namespace
Definition multican.h:19
Definition nbrtos.h:928