From c12cfe2f64d8515a4936bfefb34e8495712177e0 Mon Sep 17 00:00:00 2001 From: Sander Speetjens Date: Mon, 24 Feb 2025 08:17:30 +0100 Subject: [PATCH] Add pin control and interrupt --- mcp2515.c | 42 ++++++++++++++++++++++++++++++++---------- mcp2515.h | 12 ++++++++---- mcp2515_consts.h | 1 + 3 files changed, 41 insertions(+), 14 deletions(-) diff --git a/mcp2515.c b/mcp2515.c index 1e4f160..7d3086a 100644 --- a/mcp2515.c +++ b/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) { diff --git a/mcp2515.h b/mcp2515.h index 3f605af..976b130 100644 --- a/mcp2515.h +++ b/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); diff --git a/mcp2515_consts.h b/mcp2515_consts.h index 4d06fc4..2be2920 100644 --- a/mcp2515_consts.h +++ b/mcp2515_consts.h @@ -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,