Skip to content

Commit

Permalink
Merge pull request #15 from ElectronicCats/dev
Browse files Browse the repository at this point in the history
Add Speed Detector and Basic Services for UDS protocol
  • Loading branch information
sabas1080 authored Dec 12, 2024
2 parents 9faeab7 + 3b2a1af commit b61d578
Show file tree
Hide file tree
Showing 28 changed files with 2,933 additions and 500 deletions.
4 changes: 3 additions & 1 deletion Canbus_app/.gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
dist/*
.vscode
.clang-format
.clangd
Expand All @@ -8,3 +7,6 @@ dist/*

# Ignore the makefile
Makefile

#ignore folder
/dist
7 changes: 6 additions & 1 deletion Canbus_app/app_user.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,17 @@ static App* app_alloc() {

app->mcp_can = mcp_alloc(MCP_NORMAL, MCP_16MHZ, MCP_500KBPS);

app->frameArray = (CANFRAME*)malloc(100 * sizeof(CANFRAME));
app->frameArray = (CANFRAME*)calloc(100, sizeof(CANFRAME));

app->log_file_path = (char*)malloc(100 * sizeof(char));

app->frame_to_send = malloc(sizeof(CANFRAME));

app->obdii.bitrate = app->mcp_can->bitRate;

app->uds_received_id = UDS_RESPONSE_ID_DEFAULT;
app->uds_send_id = UDS_REQUEST_ID_DEFAULT;

makePaths(app);

return app;
Expand Down Expand Up @@ -118,6 +121,8 @@ static void app_free(App* app) {
free(app->log_file_path);
free(app->frameArray);

free_mcp2515(app->mcp_can);

free(app);
}

Expand Down
16 changes: 16 additions & 0 deletions Canbus_app/app_user.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@

#include "libraries/mcp_can_2515.h"
#include "libraries/pid_library.h"
#include "libraries/uds_library.h"

#include "canbus_app_icons.h"

#define PROGRAM_VERSION "v1.1.4.0"

#define PATHAPP "apps_data/canbus"
#define PATHAPPEXT EXT_PATH(PATHAPP)
#define PATHLOGS PATHAPPEXT "/logs"
Expand All @@ -30,6 +33,11 @@

#define MESSAGE_ERROR 0xF0

#define UDS_REQUEST_ID_DEFAULT 0x7e1
#define UDS_RESPONSE_ID_DEFAULT 0x7e9

#define START_TIME 1500

typedef enum {
WorkerflagStop = (1 << 0),
WorkerflagReceived = (1 << 1),
Expand All @@ -50,6 +58,7 @@ typedef struct {
uint32_t current_time[100];

FuriThread* thread;
FuriMutex* mutex;
SceneManager* scene_manager;
ViewDispatcher* view_dispatcher;
Widget* widget;
Expand All @@ -73,6 +82,9 @@ typedef struct {
uint32_t sniffer_index;
uint32_t sniffer_index_aux;

uint32_t uds_received_id;
uint32_t uds_send_id;

uint8_t config_timing_index;

uint8_t num_of_devices;
Expand All @@ -90,8 +102,10 @@ typedef struct {
// This is for the menu Options
typedef enum {
SniffingTestOption,
SpeedDetectorOption,
SenderOption,
ObdiiOption,
UDSOption,
ReadLOGOption,
PlayLOGOption,
SettingsOption,
Expand All @@ -101,9 +115,11 @@ typedef enum {
// These are the events on the main menu
typedef enum {
SniffingOptionEvent,
SpeedDetectorEvent,
SenderOptionEvent,
SettingsOptionEvent,
ObdiiOptionEvent,
UDSOptionEvent,
ReadLOGOptionEvent,
PlayLOGOptionEvent,
AboutUsEvent,
Expand Down
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.
163 changes: 124 additions & 39 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 @@ -184,9 +183,67 @@ bool set_sleep_mode(MCP2515* mcp_can) {
return ret;
}

// To set Loop Back Mode
bool set_loop_back_mode(MCP2515* mcp_can) {
bool ret = true;
ret = set_new_mode(mcp_can, MCP_LOOPBACK);
return ret;
}

// 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);

furi_hal_spi_bus_tx(spi, bufData, 4, TIMEOUT_SPI);
furi_hal_spi_release(spi);
}

// Init can buffers
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;

write_mf(spi, MCP_RXM0SIDH, ext, ulMask);

write_mf(spi, MCP_RXM1SIDH, ext, ulMask);

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;
Expand All @@ -199,9 +256,6 @@ void init_can_buffer(FuriHalSpiBusHandle* spi) {
a2++;
a3++;
}

set_register(spi, MCP_RXB0CTRL, 0);
set_register(spi, MCP_RXB1CTRL, 0);
}

// This function works to set Registers to initialize the MCP2515
Expand All @@ -211,6 +265,15 @@ void set_registers_init(FuriHalSpiBusHandle* spi) {
set_register(spi, MCP_BFPCTRL, MCP_BxBFS_MASK | MCP_BxBFE_MASK);

set_register(spi, MCP_TXRTSCTRL, 0x00);

set_register(spi, MCP_RXB0CTRL, MCP_RXB_BUKT_MASK);
set_register(spi, MCP_RXB1CTRL, 0);

// Part added
/*modify_register(
spi, MCP_RXB0CTRL, MCP_RXB_RX_MASK | MCP_RXB_BUKT_MASK, MCP_RXB_RX_ANY | MCP_RXB_BUKT_MASK);
modify_register(spi, MCP_RXB1CTRL, MCP_RXB_RX_MASK, MCP_RXB_RX_ANY);*/
}

// This function Works to set the Clock and Bitrate of the MCP2515
Expand Down Expand Up @@ -298,35 +361,6 @@ void mcp_set_bitrate(FuriHalSpiBusHandle* spi, MCP_BITRATE bitrate, MCP_CLOCK cl
set_register(spi, MCP_CNF3, cfg3);
}

// 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);

furi_hal_spi_bus_tx(spi, bufData, 4, 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;
Expand Down Expand Up @@ -446,12 +480,13 @@ ERROR_CAN read_can_message(MCP2515* mcp_can, CANFRAME* frame) {
ERROR_CAN ret = ERROR_OK;
FuriHalSpiBusHandle* spi = mcp_can->spi;

uint8_t status = read_rx_tx_status(spi);
uint8_t status = 0;

mcp_get_status(spi, &status);

if(status & MCP_RX0IF) {
read_frame(spi, frame, INSTRUCTION_READ_RX0);
modify_register(spi, MCP_CANINTF, MCP_RX0IF, 0);

} else if(status & MCP_RX1IF) {
read_frame(spi, frame, INSTRUCTION_READ_RX1);
modify_register(spi, MCP_CANINTF, MCP_RX1IF, 0);
Expand Down Expand Up @@ -646,6 +681,48 @@ ERROR_CAN send_can_frame(MCP2515* mcp_can, CANFRAME* frame) {
return send_can_message(spi, frame, free_buffer);
}

uint8_t read_detection_baudrate(FuriHalSpiBusHandle* spi) {
uint8_t data_canintf = 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);

return (data_canintf & 0xf0);
}

// Function to detect the baudrate
ERROR_CAN is_this_bitrate(MCP2515* mcp_can, MCP_BITRATE bitrate) {
FuriHalSpiBusHandle* spi = mcp_can->spi;
ERROR_CAN ret = ERROR_OK;

set_config_mode(mcp_can);

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

set_listen_only_mode(mcp_can);

if(check_receive(mcp_can) == ERROR_NOMSG) return ERROR_NOMSG;

uint8_t data_canintf = 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);

data_canintf &= 0x80;

if(data_canintf == 0x80) ret = ERROR_FAIL;

set_register(spi, MCP_CANINTF, 0);

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 All @@ -656,12 +733,18 @@ MCP2515* mcp_alloc(MCP_MODE mode, MCP_CLOCK clck, MCP_BITRATE bitrate) {
return mcp_can;
}

// To free
void free_mcp2515(MCP2515* mcp_can) {
// To deinit
void deinit_mcp2515(MCP2515* mcp_can) {
mcp_reset(mcp_can->spi);
furi_hal_spi_bus_handle_deinit(mcp_can->spi);
}

// free instance
void free_mcp2515(MCP2515* mcp_can) {
free(mcp_can->spi);
free(mcp_can);
}

// This function starts the SPI communication and set the MCP2515 device
ERROR_CAN mcp2515_start(MCP2515* mcp_can) {
furi_hal_spi_bus_handle_init(mcp_can->spi);
Expand All @@ -670,6 +753,8 @@ ERROR_CAN mcp2515_start(MCP2515* mcp_can) {

mcp_reset(mcp_can->spi);

set_new_mode(mcp_can, MODE_CONFIG);

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

init_can_buffer(mcp_can->spi);
Expand Down
6 changes: 6 additions & 0 deletions Canbus_app/libraries/mcp_can_2515.h
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ typedef enum {
ERROR_NOMSG = 5,
ERROR_GET_TXB_FTIMEOUT = 6,
ERROR_SEND_MSG_TIMEOUT = 7,
ERROR_WRONG_BITRATE = 8,
} ERROR_CAN;

// MCP2515 BITRATES VALUES
Expand Down Expand Up @@ -355,6 +356,9 @@ MCP2515* mcp_alloc(MCP_MODE mode, MCP_CLOCK clck, MCP_BITRATE bitrate);
ERROR_CAN mcp2515_init(MCP2515* mcp_can);

// To close the MCP2515
void deinit_mcp2515(MCP2515* mcp_can);

// free instance
void free_mcp2515(MCP2515* mcp_can);

// This is to get the status
Expand Down Expand Up @@ -386,6 +390,8 @@ ERROR_CAN check_receive(MCP2515* mcp_can);
ERROR_CAN read_can_message(MCP2515* mcp_can,
CANFRAME* frame); // Read a CAN BUS message

ERROR_CAN is_this_bitrate(MCP2515* mcp_can, MCP_BITRATE bitrate);

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

Expand Down
4 changes: 1 addition & 3 deletions Canbus_app/libraries/pid_library.c
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,6 @@ void separate_codes(CANFRAME* frames, uint16_t* save_codes, uint8_t length) {
void get_dtc(uint16_t numerical_code, char* dtc_code) {
char* first;

UNUSED(dtc_code);

FuriString* text = furi_string_alloc();

uint8_t identifier = numerical_code >> 12; // For the second
Expand Down Expand Up @@ -456,7 +454,7 @@ bool get_ECU_name(OBDII* obdii, FuriString* ecu_name) {

// It works to free
void pid_deinit(OBDII* obdii) {
free_mcp2515(obdii->CAN);
deinit_mcp2515(obdii->CAN);
free(obdii->codes);
}

Expand Down
Loading

0 comments on commit b61d578

Please sign in to comment.