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