Add pin control and interrupt
This commit is contained in:
42
mcp2515.c
42
mcp2515.c
@@ -251,20 +251,42 @@ CAN_Error MCP2515_Reset(MCP2515_HandleTypeDef* hcan) {
|
||||
return ERROR_OK;
|
||||
}
|
||||
|
||||
CAN_Error MCP2515_Set_Listen_Only_Mode(MCP2515_HandleTypeDef* hcan) {
|
||||
return MCP2515_Set_Mode(hcan, CANCTRL_REQOP_LISTENONLY);
|
||||
CAN_Error MCP2515_Set_Mode(MCP2515_HandleTypeDef* hcan, MCP2515_Mode_t mode) {
|
||||
switch (mode) {
|
||||
case MODE_LISTEN_ONLY:
|
||||
return MCP2515_Set_Mode(hcan, CANCTRL_REQOP_LISTENONLY);
|
||||
break;
|
||||
case MODE_LOOPBACK:
|
||||
return MCP2515_Set_Mode(hcan, CANCTRL_REQOP_LOOPBACK);
|
||||
break;
|
||||
case MODE_SLEEP:
|
||||
return MCP2515_Set_Mode(hcan, CANCTRL_REQOP_SLEEP);
|
||||
break;
|
||||
case MODE_NORMAL:
|
||||
return MCP2515_Set_Mode(hcan, CANCTRL_REQOP_NORMAL);
|
||||
break;
|
||||
default:
|
||||
return MCP2515_Set_Mode(hcan, CANCTRL_REQOP_NORMAL);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
CAN_Error MCP2515_Set_Sleep_Mode(MCP2515_HandleTypeDef* hcan) {
|
||||
return MCP2515_Set_Mode(hcan, CANCTRL_REQOP_SLEEP);
|
||||
CAN_Error MCP2515_Set_Pin_Control(MCP2515_HandleTypeDef* hcan, bool B1BFS, bool B0BFS, bool B1BFE, bool B0BFE, bool B1BFM, bool B0BFM) {
|
||||
CAN_Error error = MCP2515_Set_Config_Mode(hcan);
|
||||
if (error != ERROR_OK) {
|
||||
return error;
|
||||
}
|
||||
MCP2515_Set_Register(hcan, MCP_BFPCTRL, (B1BFS << 5) | (B0BFS << 4) | (B1BFE << 3) | (B0BFE << 2) | (B1BFM << 1) | B0BFM);
|
||||
return ERROR_OK;
|
||||
}
|
||||
|
||||
CAN_Error MCP2515_Set_Loopback_Mode(MCP2515_HandleTypeDef* hcan) {
|
||||
return MCP2515_Set_Mode(hcan, CANCTRL_REQOP_LOOPBACK);
|
||||
}
|
||||
|
||||
CAN_Error MCP2515_Set_Normal_Mode(MCP2515_HandleTypeDef* hcan) {
|
||||
return MCP2515_Set_Mode(hcan, CANCTRL_REQOP_NORMAL);
|
||||
CAN_Error MCP2515_Set_Interrupt(MCP2515_HandleTypeDef* hcan, bool MERRIE, bool WAKIE, bool ERRIE, bool TX2IE, bool TX1IE, bool TX0IE, bool RX1IE, bool RX0IE) {
|
||||
CAN_Error error = MCP2515_Set_Config_Mode(hcan);
|
||||
if (error != ERROR_OK) {
|
||||
return error;
|
||||
}
|
||||
MCP2515_Set_Register(hcan, MCP_CANINTE, (MERRIE << 7) | (WAKIE << 6) | (ERRIE << 5) | (TX2IE << 4) | (TX1IE << 3) | (TX0IE << 2) | (RX1IE << 1) | RX0IE);
|
||||
return ERROR_OK;
|
||||
}
|
||||
|
||||
CAN_Error MCP2515_Set_Bitrate_Clock(MCP2515_HandleTypeDef* hcan, CAN_SPEED canSpeed, CAN_CLOCK canClock) {
|
||||
|
||||
12
mcp2515.h
12
mcp2515.h
@@ -50,6 +50,13 @@ typedef enum { TXB0 = 0, TXB1 = 1, TXB2 = 2 } TXBn;
|
||||
|
||||
typedef enum { RXB0 = 0, RXB1 = 1 } RXBn;
|
||||
|
||||
typedef enum {
|
||||
MODE_LISTEN_ONLY,
|
||||
MODE_SLEEP,
|
||||
MODE_LOOPBACK,
|
||||
MODE_NORMAL
|
||||
} MCP2515_Mode_t;
|
||||
|
||||
typedef struct {
|
||||
SPI_HandleTypeDef* hspi;
|
||||
GPIO_TypeDef* cs_port;
|
||||
@@ -66,10 +73,7 @@ void MCP2515_Init(MCP2515_HandleTypeDef* hcan, SPI_HandleTypeDef* hspi, GPIO_Typ
|
||||
CAN_Error MCP2515_Reset(MCP2515_HandleTypeDef* hcan);
|
||||
uint8_t MCP2515_Get_Status(MCP2515_HandleTypeDef* hcan);
|
||||
|
||||
CAN_Error MCP2515_Set_Listen_Only_Mode(MCP2515_HandleTypeDef* hcan);
|
||||
CAN_Error MCP2515_Set_Sleep_Mode(MCP2515_HandleTypeDef* hcan);
|
||||
CAN_Error MCP2515_Set_Loopback_Mode(MCP2515_HandleTypeDef* hcan);
|
||||
CAN_Error MCP2515_Set_Normal_Mode(MCP2515_HandleTypeDef* hcan);
|
||||
CAN_Error MCP2515_Set_Mode(MCP2515_HandleTypeDef* hcan, MCP2515_Mode_t mode);
|
||||
|
||||
CAN_Error MCP2515_Set_Bitrate_Clock(MCP2515_HandleTypeDef* hcan, CAN_SPEED canSpeed, CAN_CLOCK canClock);
|
||||
|
||||
|
||||
@@ -209,6 +209,7 @@ typedef enum {
|
||||
MCP_RXF2SIDL = 0x09,
|
||||
MCP_RXF2EID8 = 0x0A,
|
||||
MCP_RXF2EID0 = 0x0B,
|
||||
MCP_BFPCTRL = 0x0C,
|
||||
MCP_CANSTAT = 0x0E,
|
||||
MCP_CANCTRL = 0x0F,
|
||||
MCP_RXF3SIDH = 0x10,
|
||||
|
||||
Reference in New Issue
Block a user