Skip to content

Commit

Permalink
test: Read CANINTF register to detect message failures
Browse files Browse the repository at this point in the history
  • Loading branch information
AdonaiDiazEsparza committed Dec 10, 2024
1 parent 9346419 commit 4db4ef6
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 4 deletions.
Binary file modified Canbus_app/dist/canbus_app.fap
Binary file not shown.
Binary file modified Canbus_app/dist/debug/canbus_app_d.elf
Binary file not shown.
42 changes: 39 additions & 3 deletions Canbus_app/libraries/mcp_can_2515.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ static bool read_register(FuriHalSpiBusHandle* spi, uint8_t address, uint8_t* da
bool ret = true;
uint8_t instruction[] = {INSTRUCTION_READ, address};
furi_hal_spi_acquire(spi);
ret =
(furi_hal_spi_bus_tx(spi, instruction, sizeof(instruction), TIMEOUT_SPI) &&
furi_hal_spi_bus_rx(spi, data, sizeof(data), TIMEOUT_SPI));
furi_hal_spi_bus_tx(spi, instruction, sizeof(instruction), TIMEOUT_SPI);
furi_hal_spi_bus_rx(spi, data, sizeof(data), TIMEOUT_SPI);

furi_hal_spi_release(spi);
return ret;
Expand Down Expand Up @@ -682,6 +681,43 @@ ERROR_CAN send_can_frame(MCP2515* mcp_can, CANFRAME* frame) {
return send_can_message(spi, frame, free_buffer);
}

// Function to detect the baudrate
bool detect_baudrate(MCP2515* mcp_can, MCP_BITRATE bitrate) {
FuriHalSpiBusHandle* spi = mcp_can->spi;
bool ret = true;

set_config_mode(mcp_can);

mcp_set_bitrate(spi, bitrate, mcp_can->clck);

set_listen_only_mode(mcp_can);

uint8_t data_canintf = 0;
uint8_t data_caninte = 0;

uint8_t instruction[] = {INSTRUCTION_READ, MCP_CANINTF};
furi_hal_spi_acquire(spi);
furi_hal_spi_bus_tx(spi, instruction, sizeof(instruction), TIMEOUT_SPI);
furi_hal_spi_bus_rx(spi, &data_canintf, 1, TIMEOUT_SPI);
furi_hal_spi_release(spi);

instruction[1] = MCP_CANINTE;
furi_hal_spi_acquire(spi);
furi_hal_spi_bus_tx(spi, instruction, sizeof(instruction), TIMEOUT_SPI);
furi_hal_spi_bus_rx(spi, &data_caninte, 1, TIMEOUT_SPI);
furi_hal_spi_release(spi);

set_register(spi, MCP_CANINTF, 0);

// data_canintf &= 0x80;

log_info("CANTINTF: %x", data_canintf);
log_info("CANTINTE: %x", data_caninte);

// if(data_canintf == 0x80) ret = false;
return ret;
}

// This function works to alloc the struct
MCP2515* mcp_alloc(MCP_MODE mode, MCP_CLOCK clck, MCP_BITRATE bitrate) {
MCP2515* mcp_can = malloc(sizeof(MCP2515));
Expand Down
2 changes: 2 additions & 0 deletions Canbus_app/libraries/mcp_can_2515.h
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,8 @@ ERROR_CAN check_receive(MCP2515* mcp_can);
ERROR_CAN read_can_message(MCP2515* mcp_can,
CANFRAME* frame); // Read a CAN BUS message

bool detect_baudrate(MCP2515* mcp_can, MCP_BITRATE bitrate);

ERROR_CAN send_can_frame(MCP2515* mcp_can,
CANFRAME* frame); // Send a CANBUS Frame

Expand Down
69 changes: 68 additions & 1 deletion Canbus_app/scenes/DetectorSpeedOption.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
#include "../app_user.h"

bool interr = false;

const char* bitrates_names[] = {
"125KBPS",
"250KBPS",
"500KBPS",
"1000KBPS",
};

static int32_t thread_to_detect_speed(void* context);

void app_scene_speed_detector_on_enter(void* context) {
Expand Down Expand Up @@ -28,8 +37,66 @@ void app_scene_speed_detector_on_exit(void* context) {
widget_reset(app->widget);
}

/*
bool get_real_bitrate(MCP2515* mcp_can, MCP_BITRATE bitrate) {
for(uint8_t j = 0; j < 10; j++) {
if(detect_baudrate(mcp_can, bitrate)) {
return true;
}
furi_delay_ms(1);
}
}
*/

static int32_t thread_to_detect_speed(void* context) {
App* app = context;
UNUSED(app);

MCP2515* mcp_can = app->mcp_can;
mcp_can->mode = MCP_LISTENONLY;

bool debug = (mcp2515_init(mcp_can) == ERROR_OK) ? true : false;

if(!debug) draw_device_no_connected(app);

uint8_t baud_detected = 0;

MCP_BITRATE bitrates[] = {
MCP_125KBPS,
MCP_250KBPS,
MCP_500KBPS,
MCP_1000KBPS,
};

UNUSED(bitrates);

furi_delay_ms(100);

// uint8_t count = 0;

uint32_t time = furi_get_tick();

while(furi_hal_gpio_read(&gpio_button_back)) {
/*if(detect_baudrate(mcp_can, count)) {
log_info("The bitrate %s is %u", bitrates_names[count], count);
}*/

if((furi_get_tick() - time) > 1000) {
log_info(
"Con bitrate de %s value of: %u ---------------------",
bitrates_names[mcp_can->bitRate],
mcp_can->bitRate);
detect_baudrate(mcp_can, mcp_can->bitRate);
// count++;
time = furi_get_tick();
}

// if(count > 3) count = 0;
}

log_info("BAUD DETECTED %u", baud_detected);

free_mcp2515(mcp_can);

return 0;
}

0 comments on commit 4db4ef6

Please sign in to comment.