NetBurner 3.5.7
PDF Version
mcan.h
1
31/*
32 * Support and FAQ: visit <a href="https://www.microchip.com/support/">Microchip Support</a>
33 */
34
35#ifndef MCAN_H_INCLUDED
36#define MCAN_H_INCLUDED
37
38#include <conf_mcan.h>
39#include <mcan_internal.h>
40
41#ifdef DOXYGEN_STUFF
46{
47#endif
48
81#define SAME70B 1
82
83#define DONT_WAIT (0xFFFFFFFF) // Non-blocking value for CanRxMessage and GetNewMessage timeout parameter
84
91const uint32_t CONF_MCAN_RX_FIFO_0_NUM = 32; //16
92
94const uint32_t CONF_MCAN_RX_FIFO_1_NUM = 1; //16
95
97const uint32_t CONF_MCAN_RX_BUFFER_NUM = 1; //16
98
100const uint32_t CONF_MCAN_TX_BUFFER_NUM = 8; // 4
101
103const uint32_t CONF_MCAN_TX_FIFO_QUEUE_NUM = 1; // 4
104
106const uint32_t CONF_MCAN_TX_EVENT_FIFO = 8; // 8
107
109const uint32_t CONF_MCAN_RX_STANDARD_ID_FILTER_NUM = 32; //32
110
112const uint32_t CONF_MCAN_RX_EXTENDED_ID_FILTER_NUM = 32; //16
113
114/****************WARNING **********************/
115/* This is added to
116CONF_MCAN_RX_STANDARD_ID_FILTER_NUM
117and
118CONF_MCAN_RX_EXTENDED_ID_FILTER_NUMc
119and
120CONF_MCAN_TX_BUFFER_NUM
121
122and the limits above must be ok with the added values.
123*/
124const uint32_t CONF_MCAN_RTR_CHANNELS = 8;
125
137{
138 private:
139 Mcan *hw;
140 uint32_t TxBufferIndex;
141 OS_SEM TxSem;
142 volatile uint32_t standard_receive_index = 0;
143 volatile uint32_t extended_receive_index = 0;
144
145 __attribute__((__aligned__(0x0800)))
146 struct mcan_rx_element_buffer mcan_rx_buffer[CONF_MCAN_RX_BUFFER_NUM];
147 struct mcan_rx_element_buffer mcan_rx_fifo_0[CONF_MCAN_RX_FIFO_0_NUM];
148 struct mcan_rx_element_buffer mcan_rx_fifo_1[CONF_MCAN_RX_FIFO_1_NUM];
149 struct mcan_tx_element mcan_tx_buffer[CONF_MCAN_TX_BUFFER_NUM + CONF_MCAN_TX_FIFO_QUEUE_NUM+CONF_MCAN_RTR_CHANNELS];
150 struct mcan_tx_event_element mcan_tx_event_fifo[CONF_MCAN_TX_EVENT_FIFO+CONF_MCAN_RTR_CHANNELS];
151 struct mcan_standard_message_filter_element mcan_rx_standard_filter[CONF_MCAN_RX_STANDARD_ID_FILTER_NUM+CONF_MCAN_RTR_CHANNELS];
152 struct mcan_extended_message_filter_element mcan_rx_extended_filter[CONF_MCAN_RX_EXTENDED_ID_FILTER_NUM+CONF_MCAN_RTR_CHANNELS];
153
154 tx_record mcan_tx_record[CONF_MCAN_TX_BUFFER_NUM];
155
158
159 void enable_peripheral_clock();
160 void message_memory_init();
161 void set_configuration(struct mcan_config *config);
162 void clean_up_pending_tx();
163
164 void process_isr(void);
165
166 void PushToFifo(mcan_rx_element_buffer * pBuffer, OS_FIFO * pFifo);
167 void ack_tx(uint32_t index);
168 void process_rx(mcan_rx_element_buffer * pBuffer);
169
170 static mcan_module * this_Ref[2];
171 static inline void dispatch_isr(int n) {if(this_Ref[n]) this_Ref[n]->process_isr(); };
172 friend void MCAN0_Handler( void );
173 friend void MCAN1_Handler( void );
174
175 void set_baudrate(uint32_t baudrate);
176 void fd_set_baudrate(uint32_t baudrate);
177 void start();
178 void stop();
179 void enable_fd_mode();
180 void disable_fd_mode();
181 void enable_restricted_operation_mode();
182 void disable_restricted_operation_mode();
183 void enable_bus_monitor_mode();
184 void disable_bus_monitor_mode();
185 void enable_sleep_mode();
186 void disable_sleep_mode();
187 void enable_test_mode();
188 void disable_test_mode();
189 enum status_code set_rx_standard_filter(struct mcan_standard_message_filter_element *sd_filter, uint32_t index);
190 enum status_code set_rx_extended_filter(struct mcan_extended_message_filter_element *et_filter, uint32_t index);
191 enum status_code get_rx_buffer_element (struct mcan_rx_element_buffer *rx_element, uint32_t index);
192 enum status_code get_rx_fifo_0_element (struct mcan_rx_element_buffer *rx_element, uint32_t index);
193 enum status_code get_rx_fifo_1_element (struct mcan_rx_element_buffer *rx_element, uint32_t index);
194 enum status_code set_tx_buffer_element (struct mcan_tx_element *tx_element, uint32_t index);
195 enum status_code get_tx_event_fifo_element(struct mcan_tx_event_element *tx_event_element, uint32_t index);
196
197 /*
198 * \brief Can read timestamp count value.
199 *
200 * \param[in] module_inst Pointer to the MCAN software instance struct
201 *
202 * \return Timestamp count value.
203 */
204 inline uint16_t read_timestamp_count_value(){return hw->MCAN_TSCV;};
205
206 /*
207 * \brief Can read timeout count value.
208 *
209 * \param[in] module_inst Pointer to the MCAN software instance struct
210 *
211 * \return Timeout count value.
212 */
213 inline uint16_t read_timeout_count_value(){return hw->MCAN_TOCV;};
214
215 /*
216 * \brief Can read error count.
217 *
218 * \param[in] module_inst Pointer to the MCAN software instance struct
219 *
220 * \return Error count value.
221 */
222 inline uint32_t read_error_count(){return hw->MCAN_ECR;};
223
224 /*
225 * \brief Can read protocol status.
226 *
227 * \param[in] module_inst Pointer to the MCAN software instance struct
228 *
229 * \return protocol status value.
230 */
231 inline uint32_t read_protocal_status(){ return hw->MCAN_PSR;};
232
233 /*
234 * \brief Read high priority message status.
235 *
236 * \param[in] module_inst Pointer to the MCAN software instance struct
237 *
238 * \return High priority message status value.
239 */
240 inline uint32_t read_high_priority_message_status(){return hw->MCAN_HPMS;}
241
242 /*
243 * \brief Get Rx buffer status.
244 *
245 * \param[in] module_inst Pointer to the MCAN software instance struct
246 * \param[in] index Index offset in Rx buffer
247 *
248 * \return Rx buffer status value.
249 *
250 * \retval true Rx Buffer updated from new message.
251 * \retval false Rx Buffer not updated.
252 */
253 inline bool rx_get_buffer_status(uint32_t index)
254 {
255 if (index < 32)
256 {
257 if (hw->MCAN_NDAT1 & (1 << index))
258 {
259 return true;
260 } else
261 {
262 return false;
263 }
264 } else
265 {
266 index -= 32;
267 if (hw->MCAN_NDAT2 & (1 << index))
268 {
269 return true;
270 } else
271 {
272 return false;
273 }
274 }
275 };
276
277 /*
278 * \brief Clear Rx buffer status.
279 *
280 * \param[in] module_inst Pointer to the MCAN software instance struct
281 * \param[in] index Index offset in Rx buffer
282 */
283 inline void rx_clear_buffer_status(uint32_t index)
284 {
285 if (index < 32)
286 {
287 hw->MCAN_NDAT1 = (1 << index);
288 } else
289 {
290 index -= 32;
291 hw->MCAN_NDAT2 = (1 << index);
292 }
293 }
294
295 /*
296 * \brief Get Rx FIFO status.
297 *
298 * \param[in] module_inst Pointer to the MCAN software instance struct
299 * \param[in] fifo_number Rx FIFO 0 or 1
300 *
301 * \return Rx FIFO status value.
302 */
303 inline uint32_t rx_get_fifo_status(bool fifo_number)
304 {
305 if (!fifo_number)
306 {
307 return hw->MCAN_RXF0S;
308 } else
309 {
310 return hw->MCAN_RXF1S;
311 }
312 }
313
314 /*
315 * \brief Set Rx acknowledge.
316 *
317 * \param[in] module_inst Pointer to the MCAN software instance struct
318 * \param[in] fifo_number Rx FIFO 0 or 1
319 * \param[in] index Index offset in FIFO
320 */
321 inline void rx_fifo_acknowledge(bool fifo_number, uint32_t index)
322 {
323 if (!fifo_number)
324 {
325 hw->MCAN_RXF0A = MCAN_RXF0A_F0AI(index);
326 } else
327 {
328 hw->MCAN_RXF1A = MCAN_RXF1A_F1AI(index);
329 }
330 }
331
332 /*
333 * \brief Get Tx FIFO/Queue status.
334 *
335 * \param[in] module_inst Pointer to the MCAN software instance struct
336 *
337 * \return Tx FIFO/Queue status value.
338 */
339 inline uint32_t tx_get_fifo_queue_status()
340 {
341 return hw->MCAN_TXFQS;
342 }
343
344 /*
345 * \brief Get Tx buffer request pending status.
346 *
347 * \param[in] module_inst Pointer to the MCAN software instance struct
348 *
349 * \return Bit mask of Tx buffer request pending status value.
350 */
351 inline uint32_t tx_get_pending_status()
352 {
353 return hw->MCAN_TXBRP;
354 }
355
356 /*
357 * \brief Tx buffer add transfer request.
358 *
359 * \param[in] module_inst Pointer to the MCAN software instance struct
360 * \param[in] trig_mask The mask value to trigger transfer buffer
361 *
362 * \return Status of the result.
363 *
364 * \retval STATUS_OK Set the transfer request.
365 * \retval STATUS_ERR_BUSY The module is in configuration.
366 */
367 inline enum status_code tx_transfer_request( uint32_t trig_mask)
368 {
369 if (hw->MCAN_CCCR & MCAN_CCCR_CCE)
370 {
371 return ERR_BUSY;
372 }
373 hw->MCAN_TXBAR = trig_mask;
374 return STATUS_OK;
375 }
376
377 /*
378 * \brief Set Tx Queue operation.
379 *
380 * \param[in] module_inst Pointer to the MCAN software instance struct
381 * \param[in] trig_mask The mask value to cancel transfer buffer
382 *
383 * \return Status of the result.
384 *
385 * \retval STATUS_OK Set the transfer request.
386 * \retval STATUS_BUSY The module is in configuration.
387 */
388 inline enum status_code tx_cancel_request( uint32_t trig_mask)
389 {
390 if (hw->MCAN_CCCR & MCAN_CCCR_CCE)
391 {
392 return STATUS_ERR_BUSY;
393 }
394 hw->MCAN_TXBCR = trig_mask;
395 return STATUS_OK;
396 }
397
398 /*
399 * \brief Get Tx transmission status.
400 *
401 * \param[in] module_inst Pointer to the MCAN software instance struct
402 *
403 * \return Bit mask of Tx transmission status value.
404 */
405 inline uint32_t tx_get_transmission_status()
406 {
407 return hw->MCAN_TXBTO;
408 }
409
410 /*
411 * \brief Get Tx cancellation status.
412 *
413 * \param[in] module_inst Pointer to the MCAN software instance struct
414 *
415 * \return Bit mask of Tx cancellation status value.
416 */
417 inline uint32_t tx_get_cancellation_status()
418 {
419 return hw->MCAN_TXBCF;
420 }
421
422 /*
423 * \brief Get Tx event FIFO status.
424 *
425 * \param[in] module_inst Pointer to the MCAN software instance struct
426 *
427 * \return Tx event FIFO status value.
428 */
429 inline uint32_t tx_get_event_fifo_status()
430 {
431 return hw->MCAN_TXEFS;
432 }
433
434 /*
435 * \brief Set Tx Queue operation.
436 *
437 * \param[in] module_inst Pointer to the MCAN software instance struct
438 * \param[in] index Index for the transfer FIFO
439 */
440 inline void tx_event_fifo_acknowledge( uint32_t index)
441 {
442 hw->MCAN_TXEFA = MCAN_TXEFA_EFAI(index);
443 }
444
445 /*
446 * \brief Enable MCAN interrupt.
447 *
448 * \param[in] module_inst Pointer to the MCAN software instance struct
449 * \param[in] source Interrupt source type
450 */
451 inline void enable_interrupt(const enum mcan_interrupt_source source)
452 {
453 hw->MCAN_IE |= source;
454 }
455
456 /*
457 * \brief Disable MCAN interrupt.
458 *
459 * \param[in] module_inst Pointer to the MCAN software instance struct
460 * \param[in] source Interrupt source type
461 */
462 inline void disable_interrupt(const enum mcan_interrupt_source source)
463 {
464 hw->MCAN_IE &= ~source;
465 }
466
467 /*
468 * \brief Get MCAN interrupt status.
469 *
470 * \param[in] module_inst Pointer to the MCAN software instance struct
471 */
472 inline uint32_t read_interrupt_status()
473 {
474 return hw->MCAN_IR;
475 }
476
477 /*
478 * \brief Clear MCAN interrupt status.
479 *
480 * \param[in] module_inst Pointer to the MCAN software instance struct
481 * \param[in] source Interrupt source type
482 *
483 * \return Bit mask of interrupt status value.
484 */
485 inline void clear_interrupt_status(const enum mcan_interrupt_source source)
486 {
487 hw->MCAN_IR = source;
488 }
489
490
491 public:
499 mcan_module(Mcan *phw, uint32_t baud);
500
501 mcan_module(){};
502
512 void init(Mcan *phw, struct mcan_config *config, uint32_t baud);
513
523 void send_message(uint32_t id_value, uint8_t *data,uint32_t data_length, OS_SEM * pSem=0);
524
534 bool blocking_send_message(uint32_t id_value, uint8_t *data,uint32_t data_length,uint32_t TimeOut);
535
536
546 int RegisterRxFifo(uint32_t composite_id, OS_FIFO *pFifo, int channel = -1 );
547
559 int RegisterRxFifoMask(uint32_t composite_id, uint32_t mask, OS_FIFO *pFifo, int channel = -1 );
560
571 int RegisterRxFifoRange(uint32_t composite_id_low,uint32_t composite_id_hi, OS_FIFO *pFifo, int channel = -1 );
572
580 int UnRegisterFifo(int channel );
581
597 int MultiCanSetRTRMessage(uint32_t id, uint8_t *data, uint8_t len, int channel = -1 );
598
606 int MultiCanReplaceRTRMessage(int channel, uint8_t *data, uint8_t len );
607
617 int MultiCanStopRTRMessage(int channel );
618
619 // Toggle the TX line hi/low and print the result of the RX line.
620 void IOTest();
621
622}; // end class mcan_module
623
624
633#define CAN_EXTENDED_ID_BIT (0x20000000)
634
638#define ExtToNbId(id ) (id | CAN_EXTENDED_ID_BIT)
639
643#define NormToNbId(id) (id & 0x7ff)
644
650#define IsNBIdExt(id) ( (id & CAN_EXTENDED_ID_BIT)!=0)
651
655#define NbToExtId(id) (id & 0x1FFFFFFF)
656
664#define NbToNormId(id) (id & 0x7FF)
665
666
678{
679 public:
732
799
800}; // end mcan_config class
801
802
803
808#define CAN_DATA_STORE_SIZE (512)
812struct PrivateCanData;
813
818{
819 private:
820 PrivateCanData *pData;
821 /* Private constructor used for received frames */
822 CanRxMessage( PrivateCanData *pData );
823
824 public:
830 uint8_t GetLength();
831
840 uint8_t CopyData( uint8_t *buffer, uint8_t max_len );
841
848 const uint8_t * GetData();
849
855 uint32_t GetId();
856
877 uint16_t GetTimeStamp();
878
886 BOOL IsValid();
887
888 /* Constructors */
889
901 CanRxMessage( OS_FIFO *pFifo, uint32_t timeout = WAIT_FOREVER );
902
908
920 BOOL GetNewMessage( OS_FIFO *pFifo, uint32_t timeout = WAIT_FOREVER );
921};
922
923
926#ifdef DOXYGEN_STUFF
927} // namespace
928#endif
929
930
931#endif /* MCAN_H_INCLUDED */
932
933
934
MCAN extended message ID filter element structure.
Definition mcan_internal.h:414
Class to hold received CAN messages.
Definition mcan.h:818
BOOL IsValid()
Check to verify the CanRxMessage is a valid message.
uint32_t GetId()
Returns the ID of the message.
uint8_t CopyData(uint8_t *buffer, uint8_t max_len)
Copy the data in the message up to max_len.
const uint8_t * GetData()
Returns a pointer to the message data.
BOOL GetNewMessage(OS_FIFO *pFifo, uint32_t timeout=WAIT_FOREVER)
Get a new message from the FIFO. If no message is available, wait up to the timeout for one to be rec...
uint8_t GetLength()
Returns the amount of data stored in the message.
uint16_t GetTimeStamp()
Returns the time stamp of the message.
CanRxMessage(OS_FIFO *pFifo, uint32_t timeout=WAIT_FOREVER)
Build a CanRxMessage from a FIFO.
MCAN configuration structure.
Definition mcan.h:678
uint8_t rx_fifo_1_watermark
Definition mcan.h:727
uint8_t rx_fifo_0_watermark
Definition mcan.h:723
bool tx_queue_mode
Definition mcan.h:729
bool run_in_standby
Definition mcan.h:681
bool protocol_exception_handling
Definition mcan.h:689
bool transmit_pause
Definition mcan.h:685
enum mcan_timeout_mode timeout_mode
Definition mcan.h:701
uint8_t watchdog_configuration
Definition mcan.h:683
uint8_t tx_event_fifo_watermark
Definition mcan.h:731
bool remote_frames_standard_reject
Definition mcan.h:715
void set_config_defaults()
Initializes an MCAN configuration structure to defaults.
Definition mcan.h:768
bool remote_frames_extended_reject
Definition mcan.h:717
bool clock_stop_acknowledge
Definition mcan.h:695
bool clock_stop_request
Definition mcan.h:693
bool automatic_retransmission
Definition mcan.h:691
uint8_t delay_compensation_offset
Definition mcan.h:707
bool rx_fifo_1_overwrite
Definition mcan.h:725
bool timeout_enable
Definition mcan.h:703
uint8_t timestamp_prescaler
Definition mcan.h:697
uint32_t extended_id_mask
Definition mcan.h:719
uint8_t delay_compensation_filter_window_length
Definition mcan.h:709
bool tdc_enable
Definition mcan.h:705
enum mcan_nonmatching_frames_action nonmatching_frames_action_extended
Definition mcan.h:713
uint16_t timeout_period
Definition mcan.h:699
enum mcan_nonmatching_frames_action nonmatching_frames_action_standard
Definition mcan.h:711
bool rx_fifo_0_overwrite
Definition mcan.h:721
bool edge_filtering
Definition mcan.h:687
MCAN Module Class.
Definition mcan.h:137
void init(Mcan *phw, struct mcan_config *config, uint32_t baud)
Initialize a MCAN module.
int RegisterRxFifoRange(uint32_t composite_id_low, uint32_t composite_id_hi, OS_FIFO *pFifo, int channel=-1)
Register a FIFO to receive messages in the specified numeric range of addresses from low to high.
void send_message(uint32_t id_value, uint8_t *data, uint32_t data_length, OS_SEM *pSem=0)
Send a MCAN message.
int MultiCanReplaceRTRMessage(int channel, uint8_t *data, uint8_t len)
Modifies/updates the response for the RTR message registered wotj MultiCanSetRTRMessage().
int MultiCanStopRTRMessage(int channel)
Unregister the RTR message for the specified channel.
int RegisterRxFifoMask(uint32_t composite_id, uint32_t mask, OS_FIFO *pFifo, int channel=-1)
Register a FIFO to receive messages as specified by the address bit mask. A value of 1 = match,...
mcan_module(Mcan *phw, uint32_t baud)
Create a MCAN module instance.
int MultiCanSetRTRMessage(uint32_t id, uint8_t *data, uint8_t len, int channel=-1)
Enable an auto-reply to a CAN RTR message for the specified CAN ID. The reply message is specified by...
int UnRegisterFifo(int channel)
Unregister the FIFO for the specified CAN channel.
int RegisterRxFifo(uint32_t composite_id, OS_FIFO *pFifo, int channel=-1)
Register a FIFO to receive CAN messages.
bool blocking_send_message(uint32_t id_value, uint8_t *data, uint32_t data_length, uint32_t TimeOut)
Send a MCAN message and block until sent.
mcan_timeout_mode
Can time out modes.
Definition mcan_internal.h:546
mcan_nonmatching_frames_action
Can non-matching frames action.
Definition mcan_internal.h:562
mcan_interrupt_source
Can module interrupt source.
Definition mcan_internal.h:579
@ MCAN_TIMEOUT_CONTINUES
Definition mcan_internal.h:548
@ MCAN_NONMATCHING_FRAMES_REJECT
Definition mcan_internal.h:568
const uint32_t CONF_MCAN_RX_EXTENDED_ID_FILTER_NUM
Range: 1..64.
Definition mcan.h:112
const uint32_t CONF_MCAN_TX_EVENT_FIFO
Range: 1..32.
Definition mcan.h:106
const uint32_t CONF_MCAN_RX_FIFO_1_NUM
Range: 1..64.
Definition mcan.h:94
const uint32_t CONF_MCAN_RX_STANDARD_ID_FILTER_NUM
Range: 1..128.
Definition mcan.h:109
const uint32_t CONF_MCAN_TX_FIFO_QUEUE_NUM
Range: 1..16.
Definition mcan.h:103
const uint32_t CONF_MCAN_TX_BUFFER_NUM
Range: 1..16.
Definition mcan.h:100
const uint32_t CONF_MCAN_RX_BUFFER_NUM
Range: 1..64.
Definition mcan.h:97
const uint32_t CONF_MCAN_RX_FIFO_0_NUM
Range: 1..64.
Definition mcan.h:91
#define WAIT_FOREVER
Parameter macro used for timeout parameters that have a 0 value and wait forever.
Definition nbrtos.h:81
#define STATUS_OK
OK, no errors.
Definition mailto.h:28
mcanMODM7AE70 namespace
Definition mcan.h:46
Definition nbrtos.h:928
Semaphores are used to control access to shared resources or or to communicate between tasks in a mul...
Definition nbrtos.h:407
MCAN receive element structure for buffer.
Definition mcan_internal.h:79
MCAN transfer element structure.
Definition mcan_internal.h:191
MCAN transfer event FIFO element structure.
Definition mcan_internal.h:270