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