35#ifndef MCAN_H_INCLUDED
36#define MCAN_H_INCLUDED
39#include <mcan_internal.h>
83#define DONT_WAIT (0xFFFFFFFF)
124const uint32_t CONF_MCAN_RTR_CHANNELS = 8;
140 uint32_t TxBufferIndex;
142 volatile uint32_t standard_receive_index = 0;
143 volatile uint32_t extended_receive_index = 0;
145 __attribute__((__aligned__(0x0800)))
159 void enable_peripheral_clock();
160 void message_memory_init();
161 void set_configuration(
struct mcan_config *config);
162 void clean_up_pending_tx();
164 void process_isr(
void);
167 void ack_tx(uint32_t index);
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 );
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);
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);
202 inline uint16_t read_timestamp_count_value(){
return hw->MCAN_TSCV;};
211 inline uint16_t read_timeout_count_value(){
return hw->MCAN_TOCV;};
220 inline uint32_t read_high_priority_message_status(){
return hw->MCAN_HPMS;}
233 inline bool rx_get_buffer_status(uint32_t index)
237 if (hw->MCAN_NDAT1 & (1 << index))
247 if (hw->MCAN_NDAT2 & (1 << index))
263 inline void rx_clear_buffer_status(uint32_t index)
267 hw->MCAN_NDAT1 = (1 << index);
271 hw->MCAN_NDAT2 = (1 << index);
283 inline uint32_t rx_get_fifo_status(
bool fifo_number)
287 return hw->MCAN_RXF0S;
290 return hw->MCAN_RXF1S;
301 inline void rx_fifo_acknowledge(
bool fifo_number, uint32_t index)
305 hw->MCAN_RXF0A = MCAN_RXF0A_F0AI(index);
308 hw->MCAN_RXF1A = MCAN_RXF1A_F1AI(index);
319 inline uint32_t tx_get_fifo_queue_status()
321 return hw->MCAN_TXFQS;
331 inline uint32_t tx_get_pending_status()
333 return hw->MCAN_TXBRP;
347 inline enum status_code tx_transfer_request( uint32_t trig_mask)
349 if (hw->MCAN_CCCR & MCAN_CCCR_CCE)
353 hw->MCAN_TXBAR = trig_mask;
368 inline enum status_code tx_cancel_request( uint32_t trig_mask)
370 if (hw->MCAN_CCCR & MCAN_CCCR_CCE)
372 return STATUS_ERR_BUSY;
374 hw->MCAN_TXBCR = trig_mask;
385 inline uint32_t tx_get_transmission_status()
387 return hw->MCAN_TXBTO;
397 inline uint32_t tx_get_cancellation_status()
399 return hw->MCAN_TXBCF;
409 inline uint32_t tx_get_event_fifo_status()
411 return hw->MCAN_TXEFS;
420 inline void tx_event_fifo_acknowledge( uint32_t index)
422 hw->MCAN_TXEFA = MCAN_TXEFA_EFAI(index);
433 hw->MCAN_IE |= source;
444 hw->MCAN_IE &= ~source;
452 inline uint32_t read_interrupt_status()
467 hw->MCAN_IR = source;
670#define CAN_EXTENDED_ID_BIT (0x20000000)
675#define ExtToNbId(id ) (id | CAN_EXTENDED_ID_BIT)
680#define NormToNbId(id) (id & 0x7ff)
687#define IsNBIdExt(id) ( (id & CAN_EXTENDED_ID_BIT)!=0)
692#define NbToExtId(id) (id & 0x1FFFFFFF)
701#define NbToNormId(id) (id & 0x7FF)
845#define CAN_DATA_STORE_SIZE (512)
849struct PrivateCanData;
857 PrivateCanData *pData;
859 CanRxMessage( PrivateCanData *pData );
877 uint8_t
CopyData( uint8_t *buffer, uint8_t max_len );
MCAN extended message ID filter element structure.
Definition mcan_internal.h:414
BOOL IsValid()
Check to verify the CanRxMessage is a valid message.
~CanRxMessage()
Destructor.
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
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