NetBurner 3.5.8
PDF Version
mcan.h
1
30
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
79
80
81#define SAME70B 1
82
83#define DONT_WAIT (0xFFFFFFFF) // Non-blocking value for CanRxMessage and GetNewMessage timeout parameter
84
89
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
127
128
129
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 enable_fd_mode();
178 void disable_fd_mode();
179 void enable_restricted_operation_mode();
180 void disable_restricted_operation_mode();
181 void enable_bus_monitor_mode();
182 void disable_bus_monitor_mode();
183 void enable_sleep_mode();
184 void disable_sleep_mode();
185 void enable_test_mode();
186 void disable_test_mode();
187 enum status_code set_rx_standard_filter(struct mcan_standard_message_filter_element *sd_filter, uint32_t index);
188 enum status_code set_rx_extended_filter(struct mcan_extended_message_filter_element *et_filter, uint32_t index);
189 enum status_code get_rx_buffer_element (struct mcan_rx_element_buffer *rx_element, uint32_t index);
190 enum status_code get_rx_fifo_0_element (struct mcan_rx_element_buffer *rx_element, uint32_t index);
191 enum status_code get_rx_fifo_1_element (struct mcan_rx_element_buffer *rx_element, uint32_t index);
192 enum status_code set_tx_buffer_element (struct mcan_tx_element *tx_element, uint32_t index);
193 enum status_code get_tx_event_fifo_element(struct mcan_tx_event_element *tx_event_element, uint32_t index);
194
195 /*
196 * \brief Can read timestamp count value.
197 *
198 * \param[in] module_inst Pointer to the MCAN software instance struct
199 *
200 * \return Timestamp count value.
201 */
202 inline uint16_t read_timestamp_count_value(){return hw->MCAN_TSCV;};
203
204 /*
205 * \brief Can read timeout count value.
206 *
207 * \param[in] module_inst Pointer to the MCAN software instance struct
208 *
209 * \return Timeout count value.
210 */
211 inline uint16_t read_timeout_count_value(){return hw->MCAN_TOCV;};
212
213 /*
214 * \brief Read high priority message status.
215 *
216 * \param[in] module_inst Pointer to the MCAN software instance struct
217 *
218 * \return High priority message status value.
219 */
220 inline uint32_t read_high_priority_message_status(){return hw->MCAN_HPMS;}
221
222 /*
223 * \brief Get Rx buffer status.
224 *
225 * \param[in] module_inst Pointer to the MCAN software instance struct
226 * \param[in] index Index offset in Rx buffer
227 *
228 * \return Rx buffer status value.
229 *
230 * \retval true Rx Buffer updated from new message.
231 * \retval false Rx Buffer not updated.
232 */
233 inline bool rx_get_buffer_status(uint32_t index)
234 {
235 if (index < 32)
236 {
237 if (hw->MCAN_NDAT1 & (1 << index))
238 {
239 return true;
240 } else
241 {
242 return false;
243 }
244 } else
245 {
246 index -= 32;
247 if (hw->MCAN_NDAT2 & (1 << index))
248 {
249 return true;
250 } else
251 {
252 return false;
253 }
254 }
255 };
256
257 /*
258 * \brief Clear Rx buffer status.
259 *
260 * \param[in] module_inst Pointer to the MCAN software instance struct
261 * \param[in] index Index offset in Rx buffer
262 */
263 inline void rx_clear_buffer_status(uint32_t index)
264 {
265 if (index < 32)
266 {
267 hw->MCAN_NDAT1 = (1 << index);
268 } else
269 {
270 index -= 32;
271 hw->MCAN_NDAT2 = (1 << index);
272 }
273 }
274
275 /*
276 * \brief Get Rx FIFO status.
277 *
278 * \param[in] module_inst Pointer to the MCAN software instance struct
279 * \param[in] fifo_number Rx FIFO 0 or 1
280 *
281 * \return Rx FIFO status value.
282 */
283 inline uint32_t rx_get_fifo_status(bool fifo_number)
284 {
285 if (!fifo_number)
286 {
287 return hw->MCAN_RXF0S;
288 } else
289 {
290 return hw->MCAN_RXF1S;
291 }
292 }
293
294 /*
295 * \brief Set Rx acknowledge.
296 *
297 * \param[in] module_inst Pointer to the MCAN software instance struct
298 * \param[in] fifo_number Rx FIFO 0 or 1
299 * \param[in] index Index offset in FIFO
300 */
301 inline void rx_fifo_acknowledge(bool fifo_number, uint32_t index)
302 {
303 if (!fifo_number)
304 {
305 hw->MCAN_RXF0A = MCAN_RXF0A_F0AI(index);
306 } else
307 {
308 hw->MCAN_RXF1A = MCAN_RXF1A_F1AI(index);
309 }
310 }
311
312 /*
313 * \brief Get Tx FIFO/Queue status.
314 *
315 * \param[in] module_inst Pointer to the MCAN software instance struct
316 *
317 * \return Tx FIFO/Queue status value.
318 */
319 inline uint32_t tx_get_fifo_queue_status()
320 {
321 return hw->MCAN_TXFQS;
322 }
323
324 /*
325 * \brief Get Tx buffer request pending status.
326 *
327 * \param[in] module_inst Pointer to the MCAN software instance struct
328 *
329 * \return Bit mask of Tx buffer request pending status value.
330 */
331 inline uint32_t tx_get_pending_status()
332 {
333 return hw->MCAN_TXBRP;
334 }
335
336 /*
337 * \brief Tx buffer add transfer request.
338 *
339 * \param[in] module_inst Pointer to the MCAN software instance struct
340 * \param[in] trig_mask The mask value to trigger transfer buffer
341 *
342 * \return Status of the result.
343 *
344 * \retval STATUS_OK Set the transfer request.
345 * \retval STATUS_ERR_BUSY The module is in configuration.
346 */
347 inline enum status_code tx_transfer_request( uint32_t trig_mask)
348 {
349 if (hw->MCAN_CCCR & MCAN_CCCR_CCE)
350 {
351 return ERR_BUSY;
352 }
353 hw->MCAN_TXBAR = trig_mask;
354 return STATUS_OK;
355 }
356
357 /*
358 * \brief Set Tx Queue operation.
359 *
360 * \param[in] module_inst Pointer to the MCAN software instance struct
361 * \param[in] trig_mask The mask value to cancel transfer buffer
362 *
363 * \return Status of the result.
364 *
365 * \retval STATUS_OK Set the transfer request.
366 * \retval STATUS_BUSY The module is in configuration.
367 */
368 inline enum status_code tx_cancel_request( uint32_t trig_mask)
369 {
370 if (hw->MCAN_CCCR & MCAN_CCCR_CCE)
371 {
372 return STATUS_ERR_BUSY;
373 }
374 hw->MCAN_TXBCR = trig_mask;
375 return STATUS_OK;
376 }
377
378 /*
379 * \brief Get Tx transmission status.
380 *
381 * \param[in] module_inst Pointer to the MCAN software instance struct
382 *
383 * \return Bit mask of Tx transmission status value.
384 */
385 inline uint32_t tx_get_transmission_status()
386 {
387 return hw->MCAN_TXBTO;
388 }
389
390 /*
391 * \brief Get Tx cancellation status.
392 *
393 * \param[in] module_inst Pointer to the MCAN software instance struct
394 *
395 * \return Bit mask of Tx cancellation status value.
396 */
397 inline uint32_t tx_get_cancellation_status()
398 {
399 return hw->MCAN_TXBCF;
400 }
401
402 /*
403 * \brief Get Tx event FIFO status.
404 *
405 * \param[in] module_inst Pointer to the MCAN software instance struct
406 *
407 * \return Tx event FIFO status value.
408 */
409 inline uint32_t tx_get_event_fifo_status()
410 {
411 return hw->MCAN_TXEFS;
412 }
413
414 /*
415 * \brief Set Tx Queue operation.
416 *
417 * \param[in] module_inst Pointer to the MCAN software instance struct
418 * \param[in] index Index for the transfer FIFO
419 */
420 inline void tx_event_fifo_acknowledge( uint32_t index)
421 {
422 hw->MCAN_TXEFA = MCAN_TXEFA_EFAI(index);
423 }
424
425 /*
426 * \brief Enable MCAN interrupt.
427 *
428 * \param[in] module_inst Pointer to the MCAN software instance struct
429 * \param[in] source Interrupt source type
430 */
431 inline void enable_interrupt(const enum mcan_interrupt_source source)
432 {
433 hw->MCAN_IE |= source;
434 }
435
436 /*
437 * \brief Disable MCAN interrupt.
438 *
439 * \param[in] module_inst Pointer to the MCAN software instance struct
440 * \param[in] source Interrupt source type
441 */
442 inline void disable_interrupt(const enum mcan_interrupt_source source)
443 {
444 hw->MCAN_IE &= ~source;
445 }
446
447 /*
448 * \brief Get MCAN interrupt status.
449 *
450 * \param[in] module_inst Pointer to the MCAN software instance struct
451 */
452 inline uint32_t read_interrupt_status()
453 {
454 return hw->MCAN_IR;
455 }
456
457 /*
458 * \brief Clear MCAN interrupt status.
459 *
460 * \param[in] module_inst Pointer to the MCAN software instance struct
461 * \param[in] source Interrupt source type
462 *
463 * \return Bit mask of interrupt status value.
464 */
465 inline void clear_interrupt_status(const enum mcan_interrupt_source source)
466 {
467 hw->MCAN_IR = source;
468 }
469
470
471 public:
479 mcan_module(Mcan *phw, uint32_t baud);
480
481 mcan_module(){};
482
492 void init(Mcan *phw, struct mcan_config *config, uint32_t baud);
493
504 int send_message(uint32_t id_value, uint8_t *data,uint32_t data_length, OS_SEM * pSem=0);
505
515 bool blocking_send_message(uint32_t id_value, uint8_t *data,uint32_t data_length,uint32_t TimeOut);
516
517
527 int RegisterRxFifo(uint32_t composite_id, OS_FIFO *pFifo, int channel = -1 );
528
540 int RegisterRxFifoMask(uint32_t composite_id, uint32_t mask, OS_FIFO *pFifo, int channel = -1 );
541
552 int RegisterRxFifoRange(uint32_t composite_id_low,uint32_t composite_id_hi, OS_FIFO *pFifo, int channel = -1 );
553
561 int UnRegisterFifo(int channel );
562
578 int MultiCanSetRTRMessage(uint32_t id, uint8_t *data, uint8_t len, int channel = -1 );
579
587 int MultiCanReplaceRTRMessage(int channel, uint8_t *data, uint8_t len );
588
598 int MultiCanStopRTRMessage(int channel );
599
617 inline uint32_t read_error_count() { return hw->MCAN_ECR; }
618
632 inline uint32_t read_protocol_status() { return hw->MCAN_PSR; }
633
643 void start();
644
654 void stop();
655
656 // Toggle the TX line hi/low and print the result of the RX line.
657 void IOTest();
658
659}; // end class mcan_module
660
661
666
670#define CAN_EXTENDED_ID_BIT (0x20000000)
671
675#define ExtToNbId(id ) (id | CAN_EXTENDED_ID_BIT)
676
680#define NormToNbId(id) (id & 0x7ff)
681
687#define IsNBIdExt(id) ( (id & CAN_EXTENDED_ID_BIT)!=0)
688
692#define NbToExtId(id) (id & 0x1FFFFFFF)
693
701#define NbToNormId(id) (id & 0x7FF)
702
703
705
706
715{
716 public:
769
836
837}; // end mcan_config class
838
839
840
845#define CAN_DATA_STORE_SIZE (512)
847
848
849struct PrivateCanData;
850
854class CanRxMessage
855{
856 private:
857 PrivateCanData *pData;
858 /* Private constructor used for received frames */
859 CanRxMessage( PrivateCanData *pData );
860
861 public:
867 uint8_t GetLength();
868
877 uint8_t CopyData( uint8_t *buffer, uint8_t max_len );
878
885 const uint8_t * GetData();
886
892 uint32_t GetId();
893
914 uint16_t GetTimeStamp();
915
923 BOOL IsValid();
924
925 /* Constructors */
926
938 CanRxMessage( OS_FIFO *pFifo, uint32_t timeout = WAIT_FOREVER );
939
945
957 BOOL GetNewMessage( OS_FIFO *pFifo, uint32_t timeout = WAIT_FOREVER );
958};
959
960
962
963#ifdef DOXYGEN_STUFF
964} // namespace
965#endif
966
967
968#endif /* MCAN_H_INCLUDED */
969
970
971
MCAN extended message ID filter element structure.
Definition mcan_internal.h:414
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:715
uint8_t rx_fifo_1_watermark
Definition mcan.h:764
uint8_t rx_fifo_0_watermark
Definition mcan.h:760
bool tx_queue_mode
Definition mcan.h:766
bool run_in_standby
Definition mcan.h:718
bool protocol_exception_handling
Definition mcan.h:726
bool transmit_pause
Definition mcan.h:722
enum mcan_timeout_mode timeout_mode
Definition mcan.h:738
uint8_t watchdog_configuration
Definition mcan.h:720
uint8_t tx_event_fifo_watermark
Definition mcan.h:768
bool remote_frames_standard_reject
Definition mcan.h:752
void set_config_defaults()
Initializes an MCAN configuration structure to defaults.
Definition mcan.h:805
bool remote_frames_extended_reject
Definition mcan.h:754
bool clock_stop_acknowledge
Definition mcan.h:732
bool clock_stop_request
Definition mcan.h:730
bool automatic_retransmission
Definition mcan.h:728
uint8_t delay_compensation_offset
Definition mcan.h:744
bool rx_fifo_1_overwrite
Definition mcan.h:762
bool timeout_enable
Definition mcan.h:740
uint8_t timestamp_prescaler
Definition mcan.h:734
uint32_t extended_id_mask
Definition mcan.h:756
uint8_t delay_compensation_filter_window_length
Definition mcan.h:746
bool tdc_enable
Definition mcan.h:742
enum mcan_nonmatching_frames_action nonmatching_frames_action_extended
Definition mcan.h:750
uint16_t timeout_period
Definition mcan.h:736
enum mcan_nonmatching_frames_action nonmatching_frames_action_standard
Definition mcan.h:748
bool rx_fifo_0_overwrite
Definition mcan.h:758
bool edge_filtering
Definition mcan.h:724
void init(Mcan *phw, struct mcan_config *config, uint32_t baud)
Initialize a MCAN module.
uint32_t read_protocol_status()
Read the raw value of the MCAN Protocol Status Register (MCAN_PSR).
Definition mcan.h:632
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.
int MultiCanReplaceRTRMessage(int channel, uint8_t *data, uint8_t len)
Modifies/updates the response for the RTR message registered wotj MultiCanSetRTRMessage().
void start()
Take the MCAN controller out of initialization mode and place it on the bus.
void stop()
Halt the MCAN controller and place it in initialization mode.
int send_message(uint32_t id_value, uint8_t *data, uint32_t data_length, OS_SEM *pSem=0)
Send a MCAN message.
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.
uint32_t read_error_count()
Read the raw value of the MCAN Error Count Register (MCAN_ECR).
Definition mcan.h:617
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