diff --git a/Canbus_app/libraries/mcp_can_2515.c b/Canbus_app/libraries/mcp_can_2515.c index a4de92a..d2755d2 100644 --- a/Canbus_app/libraries/mcp_can_2515.c +++ b/Canbus_app/libraries/mcp_can_2515.c @@ -27,24 +27,6 @@ static bool set_register(FuriHalSpiBusHandle* spi, return ret; } -// To write multiple registers -static bool set_registerS(FuriHalSpiBusHandle* spi, - uint8_t address, - uint8_t* data) { - bool ret = true; - uint8_t instruction[] = {INSTRUCTION_WRITE, address}; - uint8_t count = sizeof(data); - - for (int i = 0; i < count; i++) { - instruction[i + 2] = data[i]; - } - - furi_hal_spi_acquire(spi); - ret = furi_hal_spi_bus_tx(spi, instruction, sizeof(instruction), TIMEOUT_SPI); - furi_hal_spi_release(spi); - return ret; -} - // To modify the value of one bit from a register static bool modify_register(FuriHalSpiBusHandle* spi, uint8_t address, @@ -79,33 +61,6 @@ bool mcp_get_status(FuriHalSpiBusHandle* spi, uint8_t* data) { return ret; } -// To init the buffer -void write_mf(FuriHalSpiBusHandle* spi, - uint8_t adress, - uint8_t ext, - uint8_t id) { - uint16_t canId = (uint16_t) (id & 0x0FFFF); - uint8_t bufData[4]; - - if (ext) { - bufData[MCP_EID0] = (uint8_t) (canId & 0xFF); - bufData[MCP_EID8] = (uint8_t) (canId >> 8); - canId = (uint16_t) (id >> 16); - bufData[MCP_SIDL] = (uint8_t) (canId & 0x03); - bufData[MCP_SIDL] += (uint8_t) ((canId & 0x1C) << 3); - bufData[MCP_SIDL] |= MCP_TXB_EXIDE_M; - bufData[MCP_SIDH] = (uint8_t) (canId >> 5); - } else { - bufData[MCP_EID0] = (uint8_t) (canId & 0xFF); - bufData[MCP_EID8] = (uint8_t) (canId >> 8); - canId = (uint16_t) (id >> 16); - bufData[MCP_SIDL] = (uint8_t) ((canId & 0x07) << 5); - bufData[MCP_SIDH] = (uint8_t) (canId >> 3); - } - - set_registerS(spi, adress, bufData); -} - // This function works to get the Can Id from the buffer void read_Id(FuriHalSpiBusHandle* spi, uint8_t addr, @@ -127,11 +82,54 @@ void read_Id(FuriHalSpiBusHandle* spi, } } -// To set the Mode -bool set_mode(FuriHalSpiBusHandle* spi, MCP_MODE new_mode) { - bool ret = false; +// get actual mode of the MCP2515 +uint8_t get_mode(FuriHalSpiBusHandle* spi) { + uint8_t data = 0; + read_register(spi, MCP_CANSTAT, &data); + return data & CANSTAT_OPM; +} + +// compare if the chip is the same mode +bool is_mode(MCP2515* mcp_can, MCP_MODE mode) { + uint16_t data = get_mode(mcp_can->spi); + if (data == mode) + return true; + return false; +} + +// To set a new mode +bool set_new_mode(MCP2515* mcp_can, MCP_MODE new_mode) { + FuriHalSpiBusHandle* spi = mcp_can->spi; + uint8_t read_status = 0; - uint8_t time_out = 0; + bool ret = false; + + if (get_mode(spi) == new_mode) + return false; + + if ((get_mode(spi) == MCP_SLEEP) && new_mode != MCP_SLEEP) { + uint8_t wake_up_enabled = 0; + read_register(spi, MCP_CANINTE, &wake_up_enabled); + wake_up_enabled &= MCP_WAKIF; + + if (!wake_up_enabled) { + modify_register(spi, MCP_CANINTE, MCP_WAKIF, MCP_WAKIF); + } + + if (get_mode(spi) != MCP_LISTENONLY) { + return false; + } + + if (!wake_up_enabled) { + modify_register(spi, MCP_CANINTE, MCP_WAKIF, 1); + } + + modify_register(spi, MCP_CANINTF, MCP_WAKIF, 0); + } + + uint32_t time_out = furi_get_tick(); + + time_out = furi_get_tick(); do { modify_register(spi, MCP_CANCTRL, CANCTRL_REQOP, MODE_CONFIG); @@ -140,15 +138,10 @@ bool set_mode(FuriHalSpiBusHandle* spi, MCP_MODE new_mode) { read_status &= CANSTAT_OPM; if (read_status == MODE_CONFIG) ret = true; - furi_delay_ms(1); - time_out++; - } while ((ret != true) && (time_out < 50)); - if ((ret != true) || (time_out >= 50)) { - return false; - } + } while ((ret != true) && ((furi_get_tick() - time_out) < 50)); - time_out = 0; + time_out = furi_get_tick(); do { modify_register(spi, MCP_CANCTRL, CANCTRL_REQOP, new_mode); @@ -157,81 +150,58 @@ bool set_mode(FuriHalSpiBusHandle* spi, MCP_MODE new_mode) { read_status &= CANSTAT_OPM; if (read_status == new_mode) return true; - furi_delay_ms(1); - time_out++; - } while ((ret != true) && (time_out < 50)); + } while ((furi_get_tick() - time_out) < 50); - return ret; + return false; } // To set Config mode bool set_config_mode(MCP2515* mcp_can) { - FuriHalSpiBusHandle* spi = mcp_can->spi; bool ret = true; - ret = set_mode(spi, MODE_CONFIG); + ret = set_new_mode(mcp_can, MODE_CONFIG); + + if (ret) + mcp_can->mode = MODE_CONFIG; + return ret; } // To set Normal Mode bool set_normal_mode(MCP2515* mcp_can) { - FuriHalSpiBusHandle* spi = mcp_can->spi; bool ret = true; - ret = set_mode(spi, MCP_NORMAL); + ret = set_new_mode(mcp_can, MCP_NORMAL); + + if (ret) + mcp_can->mode = MCP_NORMAL; + return ret; } // To set ListenOnly Mode bool set_listen_only_mode(MCP2515* mcp_can) { - FuriHalSpiBusHandle* spi = mcp_can->spi; bool ret = true; - ret = set_mode(spi, MCP_LISTENONLY); + ret = set_new_mode(mcp_can, MCP_LISTENONLY); + + if (ret) + mcp_can->mode = MCP_LISTENONLY; + return ret; } // To set Sleep Mode bool set_sleep_mode(MCP2515* mcp_can) { - FuriHalSpiBusHandle* spi = mcp_can->spi; bool ret = true; - ret = set_mode(spi, MCP_SLEEP); - return ret; -} + ret = set_new_mode(mcp_can, MCP_SLEEP); + + if (ret) + mcp_can->mode = MCP_SLEEP; -// To set LoopBackMode -bool set_loop_back_mode(MCP2515* mcp_can) { - FuriHalSpiBusHandle* spi = mcp_can->spi; - bool ret = true; - ret = set_mode(spi, MCP_LOOPBACK); return ret; } void init_can_buffer(FuriHalSpiBusHandle* spi) { uint8_t a1 = 0, a2 = 0, a3 = 0; - uint8_t std = 0; - uint8_t ext = 1; - - uint32_t ulMask = 0x00, ulFilt = 0x00; - - // ulMask --------------------------- - - write_mf(spi, MCP_RXM0SIDH, ext, ulMask); - - write_mf(spi, MCP_RXM1SIDH, ext, ulMask); - - // ulFilt -------------------------- - - write_mf(spi, MCP_RXF0SIDH, ext, ulFilt); - - write_mf(spi, MCP_RXF1SIDH, std, ulFilt); - - write_mf(spi, MCP_RXF2SIDH, ext, ulFilt); - - write_mf(spi, MCP_RXF3SIDH, std, ulFilt); - - write_mf(spi, MCP_RXF4SIDH, ext, ulFilt); - - write_mf(spi, MCP_RXF5SIDH, std, ulFilt); - a1 = MCP_TXB0CTRL; a2 = MCP_TXB1CTRL; a3 = MCP_TXB2CTRL; @@ -267,7 +237,7 @@ void mcp_set_bitrate(FuriHalSpiBusHandle* spi, switch (clk) { case MCP_8MHZ: switch (bitrate) { - case MCP_125KPS: + case MCP_125KBPS: cfg1 = MCP_8MHz_125kBPS_CFG1; cfg2 = MCP_8MHz_125kBPS_CFG2; cfg3 = MCP_8MHz_125kBPS_CFG3; @@ -291,7 +261,7 @@ void mcp_set_bitrate(FuriHalSpiBusHandle* spi, break; case MCP_16MHZ: switch (bitrate) { - case MCP_125KPS: + case MCP_125KBPS: cfg1 = MCP_16MHz_125kBPS_CFG1; cfg2 = MCP_16MHz_125kBPS_CFG2; cfg3 = MCP_16MHz_125kBPS_CFG3; @@ -316,7 +286,7 @@ void mcp_set_bitrate(FuriHalSpiBusHandle* spi, case MCP_20MHZ: switch (bitrate) { - case MCP_125KPS: + case MCP_125KBPS: cfg1 = MCP_20MHz_125kBPS_CFG1; cfg2 = MCP_20MHz_125kBPS_CFG2; cfg3 = MCP_20MHz_125kBPS_CFG3; @@ -345,54 +315,183 @@ void mcp_set_bitrate(FuriHalSpiBusHandle* spi, set_register(spi, MCP_CNF3, cfg3); } -// This function Works to read the can frame from the buffer -void read_canframe(FuriHalSpiBusHandle* spi, - const uint8_t addr, - CANFRAME* frame) { - uint8_t ctrl = 0, len = 0; - static uint8_t data = 0; +// To write the mask-filters for the chip +void write_mf(FuriHalSpiBusHandle* spi, + uint8_t address, + uint8_t ext, + uint32_t id) { + uint16_t canId = (uint16_t) (id & 0x0FFFF); + uint8_t bufData[4]; + + if (ext) { + bufData[MCP_EID0] = (uint8_t) (canId & 0xFF); + bufData[MCP_EID8] = (uint8_t) (canId >> 8); + canId = (uint16_t) (id >> 16); + bufData[MCP_SIDL] = (uint8_t) (canId & 0x03); + bufData[MCP_SIDL] += (uint8_t) ((canId & 0x1C) << 3); + bufData[MCP_SIDL] |= MCP_TXB_EXIDE_M; + bufData[MCP_SIDH] = (uint8_t) (canId >> 5); + } else { + bufData[MCP_SIDL] = (uint8_t) ((canId & 0x07) << 5); + bufData[MCP_SIDH] = (uint8_t) (canId >> 3); + bufData[MCP_EID0] = 0; + bufData[MCP_EID8] = 0; + } + + uint8_t instruction[] = {INSTRUCTION_WRITE, address}; + + furi_hal_spi_acquire(spi); + furi_hal_spi_bus_tx(spi, instruction, sizeof(instruction), TIMEOUT_SPI); + + for (uint8_t i = 0; i < 4; i++) { + furi_hal_spi_bus_tx(spi, &bufData[i], 1, TIMEOUT_SPI); + } + + furi_hal_spi_release(spi); +} + +// To set a Mask +void init_mask(MCP2515* mcp_can, uint8_t num_mask, uint32_t mask) { + FuriHalSpiBusHandle* spi = mcp_can->spi; + + uint8_t ext = 0; + + MCP_MODE last_mode = mcp_can->mode; + + set_config_mode(mcp_can); + + if (num_mask > 1) + return; + + if (mask > 0x7FF) + ext = 1; + + if (num_mask == 0) { + write_mf(spi, MCP_RXM0SIDH, ext, mask); + } + + if (num_mask == 1) { + write_mf(spi, MCP_RXM1SIDH, ext, mask); + } + mcp_can->mode = last_mode; + set_new_mode(mcp_can, last_mode); +} + +// To set a Filter +void init_filter(MCP2515* mcp_can, uint8_t num_filter, uint32_t filter) { + FuriHalSpiBusHandle* spi = mcp_can->spi; + + uint8_t ext = 0; + + MCP_MODE last_mode = mcp_can->mode; + + set_config_mode(mcp_can); + + if (num_filter > 6) + return; + + if (filter > 0x7FF) + ext = 1; + + switch (num_filter) { + case 0: + write_mf(spi, MCP_RXF0SIDH, ext, filter); + break; + case 1: + write_mf(spi, MCP_RXF1SIDH, ext, filter); + break; + + case 2: + write_mf(spi, MCP_RXF2SIDH, ext, filter); + break; + + case 3: + write_mf(spi, MCP_RXF3SIDH, ext, filter); + break; + + case 4: + write_mf(spi, MCP_RXF4SIDH, ext, filter); + break; + + case 5: + write_mf(spi, MCP_RXF5SIDH, ext, filter); + break; + + default: + break; + } + + mcp_can->mode = last_mode; + set_new_mode(mcp_can, last_mode); +} + +// This function works to know if there is any message waiting +uint8_t read_rx_tx_status(FuriHalSpiBusHandle* spi) { + uint8_t ret = 0; - read_Id(spi, addr, &frame->canId, &frame->ext); + mcp_get_status(spi, &ret); - read_register(spi, addr - 1, &ctrl); + ret &= (MCP_STAT_TXIF_MASK | MCP_STAT_RXIF_MASK); - read_register(spi, addr + 4, &(len)); + ret = (ret & MCP_STAT_TX0IF ? MCP_TX0IF : 0) | + (ret & MCP_STAT_TX0IF ? MCP_TX1IF : 0) | + (ret & MCP_STAT_TX0IF ? MCP_TX2IF : 0) | (ret & MCP_STAT_RXIF_MASK); - if (ctrl & 0x08) - frame->req = 1; - else - frame->req = 0; + return ret; +} - len &= MCP_DLC_MASK; +// The function to read the message +void read_frame(FuriHalSpiBusHandle* spi, + CANFRAME* frame, + uint8_t read_instruction) { + uint8_t data[4]; + uint8_t data_ctrl = 0; - frame->data_lenght = len; + furi_hal_spi_acquire(spi); + furi_hal_spi_bus_tx(spi, &read_instruction, 1, TIMEOUT_SPI); + furi_hal_spi_bus_rx(spi, data, sizeof(data), TIMEOUT); - for (uint8_t i = 0; i < len; i++) { - read_register(spi, addr + 5 + i, &data); - frame->buffer[i] = data; + uint32_t id = (data[MCP_SIDH] << 3) + (data[MCP_SIDL] >> 5); + uint8_t ext = 0; + + if ((data[MCP_SIDL] & MCP_TXB_EXIDE_M) == MCP_TXB_EXIDE_M) { + id = (id << 2) + (data[MCP_SIDL] & 0x03); + id = (id << 8) + data[MCP_EID8]; + id = (id << 8) + data[MCP_EID0]; + ext = 1; } + + frame->canId = id; + frame->ext = ext; + + furi_hal_spi_bus_rx(spi, &data_ctrl, 1, TIMEOUT); + + frame->data_lenght = data_ctrl & MCP_DLC_MASK; + frame->req = (data_ctrl & MCP_RTR_MASK) ? 1 : 0; + + for (uint8_t i = 0; i < frame->data_lenght; i++) { + furi_hal_spi_bus_rx(spi, &frame->buffer[i], 1, TIMEOUT); + } + + furi_hal_spi_release(spi); } // This function Works to get the Can message ERROR_CAN read_can_message(MCP2515* mcp_can, CANFRAME* frame) { - FuriHalSpiBusHandle* spi = mcp_can->spi; ERROR_CAN ret = ERROR_OK; - static uint8_t status = 0; + FuriHalSpiBusHandle* spi = mcp_can->spi; - mcp_get_status(spi, &status); + uint8_t status = read_rx_tx_status(spi); - if (status & MCP_STAT_RX0IF) { - read_canframe(spi, MCP_RXB0SIDH, frame); + if (status & MCP_RX0IF) { + read_frame(spi, frame, INSTRUCTION_READ_RX0); modify_register(spi, MCP_CANINTF, MCP_RX0IF, 0); - } - else if (status & MCP_STAT_RX1IF) { - read_canframe(spi, MCP_RXB1SIDH, frame); + } else if (status & MCP_RX1IF) { + read_frame(spi, frame, INSTRUCTION_READ_RX1); modify_register(spi, MCP_CANINTF, MCP_RX1IF, 0); - } else { + } else ret = ERROR_NOMSG; - } - return ret; } @@ -424,33 +523,20 @@ ERROR_CAN check_error(MCP2515* mcp_can) { // This function works to get ERROR_CAN check_receive(MCP2515* mcp_can) { FuriHalSpiBusHandle* spi = mcp_can->spi; - static uint8_t status = 0; - mcp_get_status(spi, &status); - if (status & MCP_STAT_RXIF_MASK) - return ERROR_OK; - else - return ERROR_NOMSG; -} - -// To get the next buffer -ERROR_CAN get_next_buffer_free(FuriHalSpiBusHandle* spi, - uint8_t* buffer_address) { - static uint8_t number_of_buffers[3] = {MCP_TXB0CTRL, MCP_TXB1CTRL, - MCP_TXB2CTRL}; - - uint8_t buffer_control_value = 0; + uint8_t status = read_rx_tx_status(spi); - for (uint8_t i = 0; i < 3; i++) { - read_register(spi, number_of_buffers[i], &buffer_control_value); - if (buffer_control_value == 0) { - *buffer_address = number_of_buffers[i] + 1; - return ERROR_OK; - } + if (status & MCP_RX0IF) { + return ERROR_OK; } - return ERROR_ALLTXBUSY; + if (status & MCP_RX1IF) { + return ERROR_OK; + } + + return ERROR_NOMSG; } +// write the id in the tx register void write_id(FuriHalSpiBusHandle* spi, uint8_t address, CANFRAME* frame) { uint32_t can_id = frame->canId; @@ -478,11 +564,19 @@ void write_id(FuriHalSpiBusHandle* spi, uint8_t address, CANFRAME* frame) { tbufdata[MCP_EID8] = 0; } + uint8_t instruction[] = {INSTRUCTION_WRITE, address}; + + furi_hal_spi_acquire(spi); + furi_hal_spi_bus_tx(spi, instruction, sizeof(instruction), TIMEOUT_SPI); + for (uint8_t i = 0; i < 4; i++) { - set_register(spi, address + i, tbufdata[i]); + furi_hal_spi_bus_tx(spi, &tbufdata[i], 1, TIMEOUT_SPI); } + + furi_hal_spi_release(spi); } +// write the data lenght in it respective register void write_dlc_register(FuriHalSpiBusHandle* spi, uint8_t address, CANFRAME* frame) { @@ -494,17 +588,51 @@ void write_dlc_register(FuriHalSpiBusHandle* spi, set_register(spi, address + 4, data_lenght); } +// write data in the registers void write_buffer(FuriHalSpiBusHandle* spi, uint8_t address, CANFRAME* frame) { uint8_t data_lenght = frame->data_lenght; address = address + 5; + uint8_t instruction[] = {INSTRUCTION_WRITE, address}; + + furi_hal_spi_acquire(spi); + furi_hal_spi_bus_tx(spi, instruction, sizeof(instruction), TIMEOUT_SPI); + for (uint8_t i = 0; i < data_lenght; i++) { - set_register(spi, address + i, frame->buffer[i]); + furi_hal_spi_bus_tx(spi, &frame->buffer[i], 1, TIMEOUT_SPI); } + + furi_hal_spi_release(spi); +} + +uint8_t get_free_buffer(FuriHalSpiBusHandle* spi) { + static uint8_t status = 0; + uint8_t instruction = INSTRUCTION_READ_STATUS; + + furi_hal_spi_acquire(spi); + furi_hal_spi_bus_tx(spi, &instruction, 1, TIMEOUT_SPI); + furi_hal_spi_bus_rx(spi, &status, 1, TIMEOUT_SPI); + furi_hal_spi_release(spi); + + uint8_t status_TX0 = status & MCP_STAT_TX0_PENDING; + uint8_t status_TX1 = status & MCP_STAT_TX1_PENDING; + uint8_t status_TX2 = status & MCP_STAT_TX2_PENDING; + + if (!status_TX0) + return MCP_TXB0CTRL; + if (!status_TX1) + return MCP_TXB1CTRL; + if (!status_TX2) + return MCP_TXB2CTRL; + + return 0xFF; } -ERROR_CAN send_can_message(FuriHalSpiBusHandle* spi, CANFRAME* frame) { +// send can message +ERROR_CAN send_can_message(FuriHalSpiBusHandle* spi, + CANFRAME* frame, + uint8_t tx_buffer) { static CANFRAME auxiliar_frame; memset(&auxiliar_frame, 0, sizeof(CANFRAME)); auxiliar_frame.canId = frame->canId; @@ -516,19 +644,10 @@ ERROR_CAN send_can_message(FuriHalSpiBusHandle* spi, CANFRAME* frame) { auxiliar_frame.buffer[i] = frame->buffer[i]; } - ERROR_CAN res; + ERROR_CAN res = ERROR_FAILTX; uint8_t is_send_it = 0; - uint8_t free_buffer = 0; - uint16_t time_waiting = 0; - - do { - res = get_next_buffer_free(spi, &free_buffer); - furi_delay_us(1); - time_waiting++; - } while ((res == ERROR_ALLTXBUSY) && (time_waiting < 1000)); - - if (res != ERROR_OK) - return res; + uint8_t free_buffer = tx_buffer + 1; + uint32_t time_waiting = furi_get_tick(); write_id(spi, free_buffer, &auxiliar_frame); @@ -536,23 +655,29 @@ ERROR_CAN send_can_message(FuriHalSpiBusHandle* spi, CANFRAME* frame) { write_buffer(spi, free_buffer, &auxiliar_frame); - modify_register(spi, free_buffer - 1, MCP_TXB_TXREQ_M, MCP_TXB_TXREQ_M); + modify_register(spi, tx_buffer, MCP_TXB_TXREQ_M, MCP_TXB_TXREQ_M); + + time_waiting = furi_get_tick(); - time_waiting = 0; - res = ERROR_ALLTXBUSY; + uint8_t instruction[] = {INSTRUCTION_READ, free_buffer - 1}; do { + furi_hal_spi_acquire(spi); + + furi_hal_spi_bus_tx(spi, instruction, sizeof(instruction), TIMEOUT_SPI); + furi_hal_spi_bus_rx(spi, &is_send_it, 1, TIMEOUT_SPI); + + furi_hal_spi_release(spi); + read_register(spi, free_buffer - 1, &is_send_it); if (is_send_it == 0) res = ERROR_OK; - furi_delay_us(1); - time_waiting++; - } while ((res != ERROR_OK) && (time_waiting < TIMEOUT)); - read_register(spi, free_buffer - 1, &is_send_it); + furi_delay_us(1); + } while ((res != ERROR_OK) && ((furi_get_tick() - time_waiting) < 1)); - if (time_waiting == TIMEOUT) - return ERROR_FAILTX; + if (is_send_it) + return res; return ERROR_OK; } @@ -560,7 +685,15 @@ ERROR_CAN send_can_message(FuriHalSpiBusHandle* spi, CANFRAME* frame) { // This function is to sent a can message ERROR_CAN send_can_frame(MCP2515* mcp_can, CANFRAME* frame) { FuriHalSpiBusHandle* spi = mcp_can->spi; - return send_can_message(spi, frame); + + uint8_t free_buffer = get_free_buffer(spi); + + if (free_buffer == 0xFF) + return ERROR_ALLTXBUSY; + + return send_can_message(spi, frame, free_buffer); + + return ERROR_OK; } // This function works to alloc the struct @@ -593,7 +726,7 @@ ERROR_CAN mcp2515_start(MCP2515* mcp_can) { set_registers_init(mcp_can->spi); - ret = set_mode(mcp_can->spi, mcp_can->mode); + ret = set_new_mode(mcp_can, mcp_can->mode); if (!ret) return ERROR_FAILINIT; diff --git a/Canbus_app/libraries/mcp_can_2515.h b/Canbus_app/libraries/mcp_can_2515.h index 407eec1..ff058b7 100644 --- a/Canbus_app/libraries/mcp_can_2515.h +++ b/Canbus_app/libraries/mcp_can_2515.h @@ -142,6 +142,16 @@ typedef enum { MCP_TXB_RTR_M = 0x40, /* In TXBnDLC */ MCP_RXB_IDE_M = 0x08, /* In RXBnSIDL */ MCP_RXB_RTR_M = 0x40, /* In RXBnDLC */ + // + MCP_STAT_TXIF_MASK = 0xA8, + MCP_STAT_TX0IF = 0x08, + MCP_STAT_TX1IF = 0x20, + MCP_STAT_TX2IF = 0x80, + MCP_STAT_TX_PENDING_MASK = 0x54, + MCP_STAT_TX0_PENDING = 0x04, + MCP_STAT_TX1_PENDING = 0x10, + MCP_STAT_TX2_PENDING = 0x40, + // MCP_STAT_RXIF_MASK = (0x03), MCP_STAT_RX0IF = (1 << 0), @@ -185,10 +195,6 @@ typedef enum { MCP_LOOPBACK = 0x40, MCP_LISTENONLY = 0x60, MODE_CONFIG = 0x80, - MODE_POWERUP = 0xE0, - MODE_MASK = 0xE0, - MODE_ONESHOT = 0x08, - } MCP_MODE; // MCP2515 registers @@ -307,7 +313,7 @@ typedef enum { // MCP2515 BITRATES VALUES typedef enum { - MCP_125KPS, + MCP_125KBPS, MCP_250KBPS, MCP_500KBPS, MCP_1000KBPS, @@ -354,6 +360,12 @@ void free_mcp2515(MCP2515* mcp_can); // This is to get the status bool mcp_get_status(FuriHalSpiBusHandle* spi, uint8_t* data); +// To set a new mode +bool set_new_mode(MCP2515* mcp_can, MCP_MODE new_mode); + +// The function works to compare if the chip is in a specific mode +bool is_mode(MCP2515* mcp_can, MCP_MODE mode); + // The modes we want to work bool set_config_mode(MCP2515* mcp_can); bool set_normal_mode(MCP2515* mcp_can); @@ -361,6 +373,12 @@ bool set_listen_only_mode(MCP2515* mcp_can); bool set_sleep_mode(MCP2515* mcp_can); bool set_loop_back_mode(MCP2515* mcp_can); +// To set the mask +void init_mask(MCP2515* mcp_can, uint8_t num_mask, uint32_t mask); + +// To set the filters +void init_filter(MCP2515* mcp_can, uint8_t num_filter, uint32_t filter); + // To read and write a message uint8_t get_error(MCP2515* mcp_can); ERROR_CAN check_error(MCP2515* mcp_can); @@ -370,4 +388,5 @@ ERROR_CAN read_can_message(MCP2515* mcp_can, ERROR_CAN send_can_frame(MCP2515* mcp_can, CANFRAME* frame); // Send a CANBUS Frame + #endif