#pragma once
#include <stdint.h> 

#define MCP2521_BFPCTRL 0x0C
#define MCP2521_BFPCTRL_B1BFS (1 << 5)
#define MCP2521_BFPCTRL_B0BFS (1 << 4)
#define MCP2521_BFPCTRL_B1BFE (1 << 3)
#define MCP2521_BFPCTRL_B0BFE (1 << 2)
#define MCP2521_BFPCTRL_B1BFM (1 << 1)
#define MCP2521_BFPCTRL_B0BFM (1 << 0)

#define MCP2521_TXRTSCTRL 0x0D
#define MCP2521_TXRTSCTRL_B2RTS (1 << 5)
#define MCP2521_TXRTSCTRL_B1RTS (1 << 4)
#define MCP2521_TXRTSCTRL_B0RTS (1 << 3)
#define MCP2521_TXRTSCTRL_B2RTSM (1 << 2)
#define MCP2521_TXRTSCTRL_B1RTSM (1 << 1)
#define MCP2521_TXRTSCTRL_B0RTSM (1 << 0)

#define MCP2521_CANSTAT 0x0E
#define MCP2521_CANSTAT_OPMOD2 (1 << 7)
#define MCP2521_CANSTAT_OPMOD1 (1 << 6)
#define MCP2521_CANSTAT_OPMOD0 (1 << 5)
#define MCP2521_CANSTAT_ICOD2 (1 << 2)
#define MCP2521_CANSTAT_ICOD1 (1 << 1)
#define MCP2521_CANSTAT_ICOD0 (1 << 0)

struct CANSTAT_Register {
    uint8_t ICOD : 3;
    uint8_t : 1; // Unused bit
    uint8_t OPMOD : 3;
    uint8_t : 1; // Unused bit
};

#define MCP2521_CANCTRL 0x0F
#define MCP2521_CANCTRL_REQOP2 (1 << 7)
#define MCP2521_CANCTRL_REQOP1 (1 << 6)
#define MCP2521_CANCTRL_REQOP0 (1 << 5)
#define MCP2521_CANCTRL_ABAT (1 << 4)
#define MCP2521_CANCTRL_OSM (1 << 3)
#define MCP2521_CANCTRL_CLKEN (1 << 2)
#define MCP2521_CANCTRL_CLKPRE1 (1 << 1)
#define MCP2521_CANCTRL_CLKPRE0 (1 << 0)

#define MCP2521_TEC 0x1C

#define MCP2521_REC 0x1D

#define MCP2521_CNF3 0x28
#define MCP2521_CNF3_SOF (1 << 7)
#define MCP2521_CNF3_WAKFIL (1 << 6)
#define MCP2521_CNF3_PHSEG22 (1 << 2)
#define MCP2521_CNF3_PHSEG21 (1 << 1)
#define MCP2521_CNF3_PHSEG20 (1 << 0)

#define MCP2521_CNF2 0x29
#define MCP2521_CNF2_BTLMODE (1 << 7)
#define MCP2521_CNF2_SAM (1 << 6)
#define MCP2521_CNF2_PHSEG12 (1 << 5)
#define MCP2521_CNF2_PHSEG11 (1 << 4)
#define MCP2521_CNF2_PHSEG10 (1 << 3)
#define MCP2521_CNF2_PRSEG2 (1 << 2)
#define MCP2521_CNF2_PRSEG1 (1 << 1)
#define MCP2521_CNF2_PRSEG0 (1 << 0)

#define MCP2521_CNF1 0x2A
#define MCP2521_CNF1_SJW1 (1 << 7)
#define MCP2521_CNF1_SJW0 (1 << 6)
#define MCP2521_CNF1_BRP5 (1 << 5)
#define MCP2521_CNF1_BRP4 (1 << 4)
#define MCP2521_CNF1_BRP3 (1 << 3)
#define MCP2521_CNF1_BRP2 (1 << 2)
#define MCP2521_CNF1_BRP1 (1 << 1)
#define MCP2521_CNF1_BRP0 (1 << 0)

#define MCP2521_CANINTE 0x2B
#define MCP2521_CANINTE_MERRE (1 << 7)
#define MCP2521_CANINTE_WAKIE (1 << 6)
#define MCP2521_CANINTE_ERRIE (1 << 5)
#define MCP2521_CANINTE_TX2IE (1 << 4)
#define MCP2521_CANINTE_TX1IE (1 << 3)
#define MCP2521_CANINTE_TX0IE (1 << 2)
#define MCP2521_CANINTE_RX1IE (1 << 1)
#define MCP2521_CANINTE_RX0IE (1 << 0)

#define MCP2521_CANINTF 0x2C
#define MCP2521_CANINTF_MERRF (1 << 7)
#define MCP2521_CANINTF_WAKIF (1 << 6)
#define MCP2521_CANINTF_ERRIF (1 << 5)
#define MCP2521_CANINTF_TX2IF (1 << 4)
#define MCP2521_CANINTF_TX1IF (1 << 3)
#define MCP2521_CANINTF_TX0IF (1 << 2)
#define MCP2521_CANINTF_RX1IF (1 << 1)
#define MCP2521_CANINTF_RX0IF (1 << 0)

#define MCP2521_EFLG 0x2D
#define MCP2521_EFLG_RX1OVR (1 << 7)
#define MCP2521_EFLG_RX0OVR (1 << 6)
#define MCP2521_EFLG_TXBO (1 << 5)
#define MCP2521_EFLG_TXEP (1 << 4)
#define MCP2521_EFLG_RXEP (1 << 3)
#define MCP2521_EFLG_TXWAR (1 << 2)
#define MCP2521_EFLG_RXWAR (1 << 1)
#define MCP2521_EFLG_EWARN (1 << 0)

#define MCP2521_TXB0CTRL 0x30
#define MCP2521_TXB0CTRL_ABTF (1 << 6)
#define MCP2521_TXB0CTRL_MLOA (1 << 5)
#define MCP2521_TXB0CTRL_TXERR (1 << 4)
#define MCP2521_TXB0CTRL_TXREQ (1 << 3)
#define MCP2521_TXB0CTRL_TXP1 (1 << 1)
#define MCP2521_TXB0CTRL_TXP0 (1 << 0)

#define MCP2521_TXB0DLC 0x35
#define MCP2521_TXB1DLC 0x45
#define MCP2521_TXB2DLC 0x55

#define MCP2521_TXB1CTRL 0x40
#define MCP2521_TXB1CTRL_ABTF (1 << 6)
#define MCP2521_TXB1CTRL_MLOA (1 << 5)
#define MCP2521_TXB1CTRL_TXERR (1 << 4)
#define MCP2521_TXB1CTRL_TXREQ (1 << 3)
#define MCP2521_TXB1CTRL_TXP1 (1 << 1)
#define MCP2521_TXB1CTRL_TXP0 (1 << 0)

#define MCP2521_TXB2CTRL 0x50
#define MCP2521_TXB2CTRL_ABTF (1 << 6)
#define MCP2521_TXB2CTRL_MLOA (1 << 5)
#define MCP2521_TXB2CTRL_TXERR (1 << 4)
#define MCP2521_TXB2CTRL_TXREQ (1 << 3)
#define MCP2521_TXB2CTRL_TXP1 (1 << 1)
#define MCP2521_TXB2CTRL_TXP0 (1 << 0)

struct TXBnCTRL_Register {
    uint8_t TXP : 2;
    uint8_t : 1; // Unused bit
    uint8_t TXREQ : 1;
    uint8_t TXERR : 1;
    uint8_t MLOA : 1;
    uint8_t ABTF : 1;
    uint8_t : 1; // Unused bit
};


#define MCP2521_RXB0CTRL 0x60
#define MCP2521_RXB0CTRL_RXM1 (1 << 6)
#define MCP2521_RXB0CTRL_RXM0 (1 << 5)
#define MCP2521_RXB0CTRL_RXRTR (1 << 3)
#define MCP2521_RXB0CTRL_BUKT (1 << 2)
#define MCP2521_RXB0CTRL_BUKT1 (1 << 1)
#define MCP2521_RXB0CTRL_FILHIT0 (1 << 0)

#define MCP2521_RXB1CTRL 0x70
#define MCP2521_RXB1CTRL_RXM1 (1 << 6)
#define MCP2521_RXB1CTRL_RXM0 (1 << 5)
#define MCP2521_RXB1CTRL_RXRTR (1 << 3)
#define MCP2521_RXB1CTRL_FILHIT2 (1 << 2)
#define MCP2521_RXB1CTRL_FILHIT1 (1 << 1)
#define MCP2521_RXB1CTRL_FILHIT0 (1 << 0)

#define MCP2521_OP_RESET            0b11000000
#define MCP2521_OP_READ             0b00000011
#define MCP2521_OP_READ_RX_BUFFER   0b10010000
#define MCP2521_OP_WRITE            0b00000010
#define MCP2521_OP_LOAD_TX_BUFFER   0b01000000
#define MCP2521_OP_RTS              0b10000000
#define MCP2521_OP_READ_STATUS      0b10100000
#define MCP2521_OP_RX_STATUS        0b10110000
#define MCP2521_OP_BIT_MODIFY       0b00000101

#define MCP2521_TXB0SIDH 0x31
#define MCP2521_TXB1SIDH 0x41
#define MCP2521_TXB2SIDH 0x51

#define MCP2521_TXB0SIDL 0x32
#define MCP2521_TXB1SIDL 0x42
#define MCP2521_TXB2SIDL 0x52

#define MCP2521_RXB0SIDH 0x61
#define MCP2521_RXB1SIDH 0x71

#define MCP2521_RXB0SIDL 0x62
#define MCP2521_RXB1SIDL 0x72

#define MCP2521_RXB0EID8 0x63
#define MCP2521_RXB1EID8 0x73

#define MCP2521_RXB0EID0 0x64
#define MCP2521_RXB1EID0 0x74

#define MCP2521_RXB0DLC 0x65
#define MCP2521_RXB1DLC 0x75