NetBurner 3.5.6
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
90const uint32_t CONF_MCAN_RX_FIFO_0_NUM = 32; //16
91
93const uint32_t CONF_MCAN_RX_FIFO_1_NUM = 1; //16
94
96const uint32_t CONF_MCAN_RX_BUFFER_NUM = 1; //16
97
99const uint32_t CONF_MCAN_TX_BUFFER_NUM = 8; // 4
100
102const uint32_t CONF_MCAN_TX_FIFO_QUEUE_NUM = 1; // 4
103
105const uint32_t CONF_MCAN_TX_EVENT_FIFO = 8; // 8
106
108const uint32_t CONF_MCAN_RX_STANDARD_ID_FILTER_NUM = 32; //32
109
111const uint32_t CONF_MCAN_RX_EXTENDED_ID_FILTER_NUM = 32; //16
112
113/****************WARNING **********************/
114/* This is added to
115CONF_MCAN_RX_STANDARD_ID_FILTER_NUM
116and
117CONF_MCAN_RX_EXTENDED_ID_FILTER_NUMc
118and
119CONF_MCAN_TX_BUFFER_NUM
120
121and the limits above must be ok with the added values.
122*/
123const uint32_t CONF_MCAN_RTR_CHANNELS = 8;
124
136{
137 private:
138 Mcan *hw;
139 uint32_t TxBufferIndex;
140 OS_SEM TxSem;
141 volatile uint32_t standard_receive_index = 0;
142 volatile uint32_t extended_receive_index = 0;
143
144 __attribute__((__aligned__(0x0800)))
145 struct mcan_rx_element_buffer mcan_rx_buffer[CONF_MCAN_RX_BUFFER_NUM];
146 struct mcan_rx_element_buffer mcan_rx_fifo_0[CONF_MCAN_RX_FIFO_0_NUM];
147 struct mcan_rx_element_buffer mcan_rx_fifo_1[CONF_MCAN_RX_FIFO_1_NUM];
148 struct mcan_tx_element mcan_tx_buffer[CONF_MCAN_TX_BUFFER_NUM + CONF_MCAN_TX_FIFO_QUEUE_NUM+CONF_MCAN_RTR_CHANNELS];
149 struct mcan_tx_event_element mcan_tx_event_fifo[CONF_MCAN_TX_EVENT_FIFO+CONF_MCAN_RTR_CHANNELS];
150 struct mcan_standard_message_filter_element mcan_rx_standard_filter[CONF_MCAN_RX_STANDARD_ID_FILTER_NUM+CONF_MCAN_RTR_CHANNELS];
151 struct mcan_extended_message_filter_element mcan_rx_extended_filter[CONF_MCAN_RX_EXTENDED_ID_FILTER_NUM+CONF_MCAN_RTR_CHANNELS];
152
153 tx_record mcan_tx_record[CONF_MCAN_TX_BUFFER_NUM];
154
155 OS_FIFO * Extended_Fifos[CONF_MCAN_RX_EXTENDED_ID_FILTER_NUM];
156 OS_FIFO * Standard_Fifos[CONF_MCAN_RX_STANDARD_ID_FILTER_NUM];
157
158 void enable_peripheral_clock();
159 void message_memory_init();
160 void set_configuration(struct mcan_config *config);
161 void clean_up_pending_tx();
162
163 void process_isr(void);
164
165 void PushToFifo(mcan_rx_element_buffer * pBuffer, OS_FIFO * pFifo);
166 void ack_tx(uint32_t index);
167 void process_rx(mcan_rx_element_buffer * pBuffer);
168
169 static mcan_module * this_Ref[2];
170 static inline void dispatch_isr(int n) {if(this_Ref[n]) this_Ref[n]->process_isr(); };
171 friend void MCAN0_Handler( void );
172 friend void MCAN1_Handler( void );
173
174 void set_baudrate(uint32_t baudrate);
175 void fd_set_baudrate(uint32_t baudrate);
176 void start();
177 void stop();
178 void enable_fd_mode();
179 void disable_fd_mode();
180 void enable_restricted_operation_mode();
181 void disable_restricted_operation_mode();
182 void enable_bus_monitor_mode();
183 void disable_bus_monitor_mode();
184 void enable_sleep_mode();
185 void disable_sleep_mode();
186 void enable_test_mode();
187 void disable_test_mode();
188 enum status_code set_rx_standard_filter(struct mcan_standard_message_filter_element *sd_filter, uint32_t index);
189 enum status_code set_rx_extended_filter(struct mcan_extended_message_filter_element *et_filter, uint32_t index);
190 enum status_code get_rx_buffer_element (struct mcan_rx_element_buffer *rx_element, uint32_t index);
191 enum status_code get_rx_fifo_0_element (struct mcan_rx_element_buffer *rx_element, uint32_t index);
192 enum status_code get_rx_fifo_1_element (struct mcan_rx_element_buffer *rx_element, uint32_t index);
193 enum status_code set_tx_buffer_element (struct mcan_tx_element *tx_element, uint32_t index);
194 enum status_code get_tx_event_fifo_element(struct mcan_tx_event_element *tx_event_element, uint32_t index);
195
196 /*
197 * \brief Can read timestamp count value.
198 *
199 * \param[in] module_inst Pointer to the MCAN software instance struct
200 *
201 * \return Timestamp count value.
202 */
203 inline uint16_t read_timestamp_count_value(){return hw->MCAN_TSCV;};
204
205 /*
206 * \brief Can read timeout count value.
207 *
208 * \param[in] module_inst Pointer to the MCAN software instance struct
209 *
210 * \return Timeout count value.
211 */
212 inline uint16_t read_timeout_count_value(){return hw->MCAN_TOCV;};
213
214 /*
215 * \brief Can read error count.
216 *
217 * \param[in] module_inst Pointer to the MCAN software instance struct
218 *
219 * \return Error count value.
220 */
221 inline uint32_t read_error_count(){return hw->MCAN_ECR;};
222
223 /*
224 * \brief Can read protocol status.
225 *
226 * \param[in] module_inst Pointer to the MCAN software instance struct
227 *
228 * \return protocol status value.
229 */
230 inline uint32_t read_protocal_status(){ return hw->MCAN_PSR;};
231
232 /*
233 * \brief Read high priority message status.
234 *
235 * \param[in] module_inst Pointer to the MCAN software instance struct
236 *
237 * \return High priority message status value.
238 */
239 inline uint32_t read_high_priority_message_status(){return hw->MCAN_HPMS;}
240
241 /*
242 * \brief Get Rx buffer status.
243 *
244 * \param[in] module_inst Pointer to the MCAN software instance struct
245 * \param[in] index Index offset in Rx buffer
246 *
247 * \return Rx buffer status value.
248 *
249 * \retval true Rx Buffer updated from new message.
250 * \retval false Rx Buffer not updated.
251 */
252 inline bool rx_get_buffer_status(uint32_t index)
253 {
254 if (index < 32)
255 {
256 if (hw->MCAN_NDAT1 & (1 << index))
257 {
258 return true;
259 } else
260 {
261 return false;
262 }
263 } else
264 {
265 index -= 32;
266 if (hw->MCAN_NDAT2 & (1 << index))
267 {
268 return true;
269 } else
270 {
271 return false;
272 }
273 }
274 };
275
276 /*
277 * \brief Clear Rx buffer status.
278 *
279 * \param[in] module_inst Pointer to the MCAN software instance struct
280 * \param[in] index Index offset in Rx buffer
281 */
282 inline void rx_clear_buffer_status(uint32_t index)
283 {
284 if (index < 32)
285 {
286 hw->MCAN_NDAT1 = (1 << index);
287 } else
288 {
289 index -= 32;
290 hw->MCAN_NDAT2 = (1 << index);
291 }
292 }
293
294 /*
295 * \brief Get Rx FIFO status.
296 *
297 * \param[in] module_inst Pointer to the MCAN software instance struct
298 * \param[in] fifo_number Rx FIFO 0 or 1
299 *
300 * \return Rx FIFO status value.
301 */
302 inline uint32_t rx_get_fifo_status(bool fifo_number)
303 {
304 if (!fifo_number)
305 {
306 return hw->MCAN_RXF0S;
307 } else
308 {
309 return hw->MCAN_RXF1S;
310 }
311 }
312
313 /*
314 * \brief Set Rx acknowledge.
315 *
316 * \param[in] module_inst Pointer to the MCAN software instance struct
317 * \param[in] fifo_number Rx FIFO 0 or 1
318 * \param[in] index Index offset in FIFO
319 */
320 inline void rx_fifo_acknowledge(bool fifo_number, uint32_t index)
321 {
322 if (!fifo_number)
323 {
324 hw->MCAN_RXF0A = MCAN_RXF0A_F0AI(index);
325 } else
326 {
327 hw->MCAN_RXF1A = MCAN_RXF1A_F1AI(index);
328 }
329 }
330
331 /*
332 * \brief Get Tx FIFO/Queue status.
333 *
334 * \param[in] module_inst Pointer to the MCAN software instance struct
335 *
336 * \return Tx FIFO/Queue status value.
337 */
338 inline uint32_t tx_get_fifo_queue_status()
339 {
340 return hw->MCAN_TXFQS;
341 }
342
343 /*
344 * \brief Get Tx buffer request pending status.
345 *
346 * \param[in] module_inst Pointer to the MCAN software instance struct
347 *
348 * \return Bit mask of Tx buffer request pending status value.
349 */
350 inline uint32_t tx_get_pending_status()
351 {
352 return hw->MCAN_TXBRP;
353 }
354
355 /*
356 * \brief Tx buffer add transfer request.
357 *
358 * \param[in] module_inst Pointer to the MCAN software instance struct
359 * \param[in] trig_mask The mask value to trigger transfer buffer
360 *
361 * \return Status of the result.
362 *
363 * \retval STATUS_OK Set the transfer request.
364 * \retval STATUS_ERR_BUSY The module is in configuration.
365 */
366 inline enum status_code tx_transfer_request( uint32_t trig_mask)
367 {
368 if (hw->MCAN_CCCR & MCAN_CCCR_CCE)
369 {
370 return ERR_BUSY;
371 }
372 hw->MCAN_TXBAR = trig_mask;
373 return STATUS_OK;
374 }
375
376 /*
377 * \brief Set Tx Queue operation.
378 *
379 * \param[in] module_inst Pointer to the MCAN software instance struct
380 * \param[in] trig_mask The mask value to cancel transfer buffer
381 *
382 * \return Status of the result.
383 *
384 * \retval STATUS_OK Set the transfer request.
385 * \retval STATUS_BUSY The module is in configuration.
386 */
387 inline enum status_code tx_cancel_request( uint32_t trig_mask)
388 {
389 if (hw->MCAN_CCCR & MCAN_CCCR_CCE)
390 {
391 return STATUS_ERR_BUSY;
392 }
393 hw->MCAN_TXBCR = trig_mask;
394 return STATUS_OK;
395 }
396
397 /*
398 * \brief Get Tx transmission status.
399 *
400 * \param[in] module_inst Pointer to the MCAN software instance struct
401 *
402 * \return Bit mask of Tx transmission status value.
403 */
404 inline uint32_t tx_get_transmission_status()
405 {
406 return hw->MCAN_TXBTO;
407 }
408
409 /*
410 * \brief Get Tx cancellation status.
411 *
412 * \param[in] module_inst Pointer to the MCAN software instance struct
413 *
414 * \return Bit mask of Tx cancellation status value.
415 */
416 inline uint32_t tx_get_cancellation_status()
417 {
418 return hw->MCAN_TXBCF;
419 }
420
421 /*
422 * \brief Get Tx event FIFO status.
423 *
424 * \param[in] module_inst Pointer to the MCAN software instance struct
425 *
426 * \return Tx event FIFO status value.
427 */
428 inline uint32_t tx_get_event_fifo_status()
429 {
430 return hw->MCAN_TXEFS;
431 }
432
433 /*
434 * \brief Set Tx Queue operation.
435 *
436 * \param[in] module_inst Pointer to the MCAN software instance struct
437 * \param[in] index Index for the transfer FIFO
438 */
439 inline void tx_event_fifo_acknowledge( uint32_t index)
440 {
441 hw->MCAN_TXEFA = MCAN_TXEFA_EFAI(index);
442 }
443
444 /*
445 * \brief Enable MCAN interrupt.
446 *
447 * \param[in] module_inst Pointer to the MCAN software instance struct
448 * \param[in] source Interrupt source type
449 */
450 inline void enable_interrupt(const enum mcan_interrupt_source source)
451 {
452 hw->MCAN_IE |= source;
453 }
454
455 /*
456 * \brief Disable MCAN interrupt.
457 *
458 * \param[in] module_inst Pointer to the MCAN software instance struct
459 * \param[in] source Interrupt source type
460 */
461 inline void disable_interrupt(const enum mcan_interrupt_source source)
462 {
463 hw->MCAN_IE &= ~source;
464 }
465
466 /*
467 * \brief Get MCAN interrupt status.
468 *
469 * \param[in] module_inst Pointer to the MCAN software instance struct
470 */
471 inline uint32_t read_interrupt_status()
472 {
473 return hw->MCAN_IR;
474 }
475
476 /*
477 * \brief Clear MCAN interrupt status.
478 *
479 * \param[in] module_inst Pointer to the MCAN software instance struct
480 * \param[in] source Interrupt source type
481 *
482 * \return Bit mask of interrupt status value.
483 */
484 inline void clear_interrupt_status(const enum mcan_interrupt_source source)
485 {
486 hw->MCAN_IR = source;
487 }
488
489
490 public:
498 mcan_module(Mcan *phw, uint32_t baud);
499
500 mcan_module(){};
501
511 void init(Mcan *phw, struct mcan_config *config, uint32_t baud);
512
522 void send_message(uint32_t id_value, uint8_t *data,uint32_t data_length, OS_SEM * pSem=0);
523
533 bool blocking_send_message(uint32_t id_value, uint8_t *data,uint32_t data_length,uint32_t TimeOut);
534
535
545 int RegisterRxFifo(uint32_t composite_id, OS_FIFO *pFifo, int channel = -1 );
546
558 int RegisterRxFifoMask(uint32_t composite_id, uint32_t mask, OS_FIFO *pFifo, int channel = -1 );
559
570 int RegisterRxFifoRange(uint32_t composite_id_low,uint32_t composite_id_hi, OS_FIFO *pFifo, int channel = -1 );
571
579 int UnRegisterFifo(int channel );
580
596 int MultiCanSetRTRMessage(uint32_t id, uint8_t *data, uint8_t len, int channel = -1 );
597
605 int MultiCanReplaceRTRMessage(int channel, uint8_t *data, uint8_t len );
606
616 int MultiCanStopRTRMessage(int channel );
617
618 // Toggle the TX line hi/low and print the result of the RX line.
619 void IOTest();
620
621}; // end class mcan_module
622
623
632#define CAN_EXTENDED_ID_BIT (0x20000000)
633
637#define ExtToNbId(id ) (id | CAN_EXTENDED_ID_BIT)
638
642#define NormToNbId(id) (id & 0x7ff)
643
649#define IsNBIdExt(id) ( (id & CAN_EXTENDED_ID_BIT)!=0)
650
654#define NbToExtId(id) (id & 0x1FFFFFFF)
655
663#define NbToNormId(id) (id & 0x7FF)
664
665
677{
678 public:
731
798
799}; // end mcan_config class
800
801
802
807#define CAN_DATA_STORE_SIZE (512)
811struct PrivateCanData;
812
817{
818 private:
819 PrivateCanData *pData;
820 /* Private constructor used for received frames */
821 CanRxMessage( PrivateCanData *pData );
822
823 public:
829 uint8_t GetLength();
830
839 uint8_t CopyData( uint8_t *buffer, uint8_t max_len );
840
847 const uint8_t * GetData();
848
854 uint32_t GetId();
855
876 uint16_t GetTimeStamp();
877
885 BOOL IsValid();
886
887 /* Constructors */
888
899 CanRxMessage( OS_FIFO *pFifo, uint32_t timeout = WAIT_FOREVER );
900
906
917 BOOL GetNewMessage( OS_FIFO *pFifo, uint32_t timeout = WAIT_FOREVER );
918};
919
920
923#ifdef DOXYGEN_STUFF
924} // namespace
925#endif
926
927
928#endif /* MCAN_H_INCLUDED */
929
930
931
MCAN extended message ID filter element structure.
Definition mcan_internal.h:414
Class to hold received CAN messages.
Definition mcan.h:817
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:677
uint8_t rx_fifo_1_watermark
Definition mcan.h:726
uint8_t rx_fifo_0_watermark
Definition mcan.h:722
bool tx_queue_mode
Definition mcan.h:728
bool run_in_standby
Definition mcan.h:680
bool protocol_exception_handling
Definition mcan.h:688
bool transmit_pause
Definition mcan.h:684
enum mcan_timeout_mode timeout_mode
Definition mcan.h:700
uint8_t watchdog_configuration
Definition mcan.h:682
uint8_t tx_event_fifo_watermark
Definition mcan.h:730
bool remote_frames_standard_reject
Definition mcan.h:714
void set_config_defaults()
Initializes an MCAN configuration structure to defaults.
Definition mcan.h:767
bool remote_frames_extended_reject
Definition mcan.h:716
bool clock_stop_acknowledge
Definition mcan.h:694
bool clock_stop_request
Definition mcan.h:692
bool automatic_retransmission
Definition mcan.h:690
uint8_t delay_compensation_offset
Definition mcan.h:706
bool rx_fifo_1_overwrite
Definition mcan.h:724
bool timeout_enable
Definition mcan.h:702
uint8_t timestamp_prescaler
Definition mcan.h:696
uint32_t extended_id_mask
Definition mcan.h:718
uint8_t delay_compensation_filter_window_length
Definition mcan.h:708
bool tdc_enable
Definition mcan.h:704
enum mcan_nonmatching_frames_action nonmatching_frames_action_extended
Definition mcan.h:712
uint16_t timeout_period
Definition mcan.h:698
enum mcan_nonmatching_frames_action nonmatching_frames_action_standard
Definition mcan.h:710
bool rx_fifo_0_overwrite
Definition mcan.h:720
bool edge_filtering
Definition mcan.h:686
MCAN Module Class.
Definition mcan.h:136
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:111
const uint32_t CONF_MCAN_TX_EVENT_FIFO
Range: 1..32.
Definition mcan.h:105
const uint32_t CONF_MCAN_RX_FIFO_1_NUM
Range: 1..64.
Definition mcan.h:93
const uint32_t CONF_MCAN_RX_STANDARD_ID_FILTER_NUM
Range: 1..128.
Definition mcan.h:108
const uint32_t CONF_MCAN_TX_FIFO_QUEUE_NUM
Range: 1..16.
Definition mcan.h:102
const uint32_t CONF_MCAN_TX_BUFFER_NUM
Range: 1..16.
Definition mcan.h:99
const uint32_t CONF_MCAN_RX_BUFFER_NUM
Range: 1..64.
Definition mcan.h:96
const uint32_t CONF_MCAN_RX_FIFO_0_NUM
Range: 1..64.
Definition mcan.h:90
#define STATUS_OK
OK, no errors.
Definition mailto.h:28
mcanMODM7AE70 namespace
Definition mcan.h:46
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