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