STM32F042-Drivers-Pub/can.h
Francesco Gritti fe64f5342d initial commit
2024-05-02 12:18:04 +02:00

134 lines
2.6 KiB
C

#ifndef CAN_h
#define CAN_h
#include "ftypes.h"
#include "mcu.h"
// ========================= CAN Configuration ======================= //
#define CAN_PRESCALER 2 // f_osc = 24MHz, f_periph = 12MHz
#define CAN_SEG1 6
#define CAN_SEG2 5
#define CAN_SJW 3
#define CAN_INIT_ERROR -1
#define CAN_INIT_OK 0
#define CAN_TX_ERROR -1
#define CAN_TX_OK 0
#define N_FILTERS 14
typedef enum {
canSTD_ID,
canEXTENDED_ID
}canIdExtension;
typedef enum {
canDATA_FRAME,
canREMOTE_FRAME
}canDataRemote;
typedef enum {
canFIFO_0,
canFIFO_1
}canFIFO;
typedef struct {
canIdExtension std_ext_id;
canDataRemote data_remote_frame;
u32 id;
u8 data [8];
u8 data_len;
}canMessage_t;
typedef struct {
u8 TEC;
u8 REC;
u8 LEC: 3;
u8 error_warning:1;
u8 error_passive:1;
u8 bus_off:1;
}canNetworkStatus_t;
/* This enum is used for specifying the behavior of the software
* during transmission, when all the mailboxes are full. Either
* the message is ignored, the mailbox with lowest priority is
* overwritten or if a mailbox has same id is overwritten so
* that the most recent data is transmitted
* */
typedef enum {
CAN_DO_NOT_OVERRIDE = 0,
CAN_OVERRIDE_LOW_PRIO,
CAN_OVERRIDE_IF_HIGHER_PRIO,
CAN_OVERRIDE_ID
}canTxOverrideBehaviorE;
i8 can_transmit_f32 (u16 ID, f32 data, canIdExtension idType, canDataRemote dataRemote, canTxOverrideBehaviorE override);
i8 can_transmit_u32 (u16 ID, u32 data, canIdExtension idType, canDataRemote dataRemote, canTxOverrideBehaviorE override);
f32 can_extract_f32 (u8 *payload);
u32 can_extract_u32 (u8 *payload);
i8 can_init (void);
void can_init_minimal (void);
i8 can_transmit (u16 ID, u8 * data, u8 len, canIdExtension idType, canDataRemote dataRemote, canTxOverrideBehaviorE override);
void can_enable_retransmission (void);
void can_disable_retransmission (void);
// with 16bit registers only STD IDs can be mapped
u8 can_config_filter_mask_mode_16bit (u8 filter_id, canFIFO assigned_fifo, canDataRemote dataRemote, u16 id1, u16 id2, u16 mask1, u16 mask2);
u8 can_config_filter_mask_mode_32bit (u8 filter_id, canFIFO assigned_fifo, canIdExtension extendedId, canDataRemote dataRemote, u32 id, u32 mask);
u8 can_config_filter_list_mode_16bit (u8 filter_id, canFIFO assigned_fifo, canDataRemote dataRemote, u16 id1, u16 id2, u16 id3, u16 id4);
u8 can_config_filter_list_mode_32bit (u8 filter_id, canFIFO assigned_fifo, canIdExtension extendedId, canDataRemote dataRemote, u32 id1, u32 id2);
void can_enable_filter (u8 filter_id);
void can_disable_filter (u8 filter_id);
canNetworkStatus_t can_get_network_status ();
#endif