Skip to content

Commit b61d578

Browse files
authored
Merge pull request #15 from ElectronicCats/dev
Add Speed Detector and Basic Services for UDS protocol
2 parents 9faeab7 + 3b2a1af commit b61d578

28 files changed

+2933
-500
lines changed

Canbus_app/.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
dist/*
21
.vscode
32
.clang-format
43
.clangd
@@ -8,3 +7,6 @@ dist/*
87

98
# Ignore the makefile
109
Makefile
10+
11+
#ignore folder
12+
/dist

Canbus_app/app_user.c

+6-1
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,17 @@ static App* app_alloc() {
7171

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

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

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

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

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

82+
app->uds_received_id = UDS_RESPONSE_ID_DEFAULT;
83+
app->uds_send_id = UDS_REQUEST_ID_DEFAULT;
84+
8285
makePaths(app);
8386

8487
return app;
@@ -118,6 +121,8 @@ static void app_free(App* app) {
118121
free(app->log_file_path);
119122
free(app->frameArray);
120123

124+
free_mcp2515(app->mcp_can);
125+
121126
free(app);
122127
}
123128

Canbus_app/app_user.h

+16
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,12 @@
1919

2020
#include "libraries/mcp_can_2515.h"
2121
#include "libraries/pid_library.h"
22+
#include "libraries/uds_library.h"
2223

2324
#include "canbus_app_icons.h"
2425

26+
#define PROGRAM_VERSION "v1.1.4.0"
27+
2528
#define PATHAPP "apps_data/canbus"
2629
#define PATHAPPEXT EXT_PATH(PATHAPP)
2730
#define PATHLOGS PATHAPPEXT "/logs"
@@ -30,6 +33,11 @@
3033

3134
#define MESSAGE_ERROR 0xF0
3235

36+
#define UDS_REQUEST_ID_DEFAULT 0x7e1
37+
#define UDS_RESPONSE_ID_DEFAULT 0x7e9
38+
39+
#define START_TIME 1500
40+
3341
typedef enum {
3442
WorkerflagStop = (1 << 0),
3543
WorkerflagReceived = (1 << 1),
@@ -50,6 +58,7 @@ typedef struct {
5058
uint32_t current_time[100];
5159

5260
FuriThread* thread;
61+
FuriMutex* mutex;
5362
SceneManager* scene_manager;
5463
ViewDispatcher* view_dispatcher;
5564
Widget* widget;
@@ -73,6 +82,9 @@ typedef struct {
7382
uint32_t sniffer_index;
7483
uint32_t sniffer_index_aux;
7584

85+
uint32_t uds_received_id;
86+
uint32_t uds_send_id;
87+
7688
uint8_t config_timing_index;
7789

7890
uint8_t num_of_devices;
@@ -90,8 +102,10 @@ typedef struct {
90102
// This is for the menu Options
91103
typedef enum {
92104
SniffingTestOption,
105+
SpeedDetectorOption,
93106
SenderOption,
94107
ObdiiOption,
108+
UDSOption,
95109
ReadLOGOption,
96110
PlayLOGOption,
97111
SettingsOption,
@@ -101,9 +115,11 @@ typedef enum {
101115
// These are the events on the main menu
102116
typedef enum {
103117
SniffingOptionEvent,
118+
SpeedDetectorEvent,
104119
SenderOptionEvent,
105120
SettingsOptionEvent,
106121
ObdiiOptionEvent,
122+
UDSOptionEvent,
107123
ReadLOGOptionEvent,
108124
PlayLOGOptionEvent,
109125
AboutUsEvent,

Canbus_app/dist/canbus_app.fap

34.7 KB
Binary file not shown.
229 KB
Binary file not shown.

Canbus_app/libraries/mcp_can_2515.c

+124-39
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@ static bool read_register(FuriHalSpiBusHandle* spi, uint8_t address, uint8_t* da
55
bool ret = true;
66
uint8_t instruction[] = {INSTRUCTION_READ, address};
77
furi_hal_spi_acquire(spi);
8-
ret =
9-
(furi_hal_spi_bus_tx(spi, instruction, sizeof(instruction), TIMEOUT_SPI) &&
10-
furi_hal_spi_bus_rx(spi, data, sizeof(data), TIMEOUT_SPI));
8+
furi_hal_spi_bus_tx(spi, instruction, sizeof(instruction), TIMEOUT_SPI);
9+
furi_hal_spi_bus_rx(spi, data, sizeof(data), TIMEOUT_SPI);
1110

1211
furi_hal_spi_release(spi);
1312
return ret;
@@ -184,9 +183,67 @@ bool set_sleep_mode(MCP2515* mcp_can) {
184183
return ret;
185184
}
186185

186+
// To set Loop Back Mode
187+
bool set_loop_back_mode(MCP2515* mcp_can) {
188+
bool ret = true;
189+
ret = set_new_mode(mcp_can, MCP_LOOPBACK);
190+
return ret;
191+
}
192+
193+
// To write the mask-filters for the chip
194+
void write_mf(FuriHalSpiBusHandle* spi, uint8_t address, uint8_t ext, uint32_t id) {
195+
uint16_t canId = (uint16_t)(id & 0x0FFFF);
196+
uint8_t bufData[4];
197+
198+
if(ext) {
199+
bufData[MCP_EID0] = (uint8_t)(canId & 0xFF);
200+
bufData[MCP_EID8] = (uint8_t)(canId >> 8);
201+
canId = (uint16_t)(id >> 16);
202+
bufData[MCP_SIDL] = (uint8_t)(canId & 0x03);
203+
bufData[MCP_SIDL] += (uint8_t)((canId & 0x1C) << 3);
204+
bufData[MCP_SIDL] |= MCP_TXB_EXIDE_M;
205+
bufData[MCP_SIDH] = (uint8_t)(canId >> 5);
206+
} else {
207+
bufData[MCP_SIDL] = (uint8_t)((canId & 0x07) << 5);
208+
bufData[MCP_SIDH] = (uint8_t)(canId >> 3);
209+
bufData[MCP_EID0] = 0;
210+
bufData[MCP_EID8] = 0;
211+
}
212+
213+
uint8_t instruction[] = {INSTRUCTION_WRITE, address};
214+
215+
furi_hal_spi_acquire(spi);
216+
furi_hal_spi_bus_tx(spi, instruction, sizeof(instruction), TIMEOUT_SPI);
217+
218+
furi_hal_spi_bus_tx(spi, bufData, 4, TIMEOUT_SPI);
219+
furi_hal_spi_release(spi);
220+
}
221+
222+
// Init can buffers
187223
void init_can_buffer(FuriHalSpiBusHandle* spi) {
188224
uint8_t a1 = 0, a2 = 0, a3 = 0;
189225

226+
uint8_t std = 0;
227+
uint8_t ext = 1;
228+
229+
uint32_t ulMask = 0x00, ulFilt = 0x00;
230+
231+
write_mf(spi, MCP_RXM0SIDH, ext, ulMask);
232+
233+
write_mf(spi, MCP_RXM1SIDH, ext, ulMask);
234+
235+
write_mf(spi, MCP_RXF0SIDH, ext, ulFilt);
236+
237+
write_mf(spi, MCP_RXF1SIDH, std, ulFilt);
238+
239+
write_mf(spi, MCP_RXF2SIDH, ext, ulFilt);
240+
241+
write_mf(spi, MCP_RXF3SIDH, std, ulFilt);
242+
243+
write_mf(spi, MCP_RXF4SIDH, ext, ulFilt);
244+
245+
write_mf(spi, MCP_RXF5SIDH, std, ulFilt);
246+
190247
a1 = MCP_TXB0CTRL;
191248
a2 = MCP_TXB1CTRL;
192249
a3 = MCP_TXB2CTRL;
@@ -199,9 +256,6 @@ void init_can_buffer(FuriHalSpiBusHandle* spi) {
199256
a2++;
200257
a3++;
201258
}
202-
203-
set_register(spi, MCP_RXB0CTRL, 0);
204-
set_register(spi, MCP_RXB1CTRL, 0);
205259
}
206260

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

213267
set_register(spi, MCP_TXRTSCTRL, 0x00);
268+
269+
set_register(spi, MCP_RXB0CTRL, MCP_RXB_BUKT_MASK);
270+
set_register(spi, MCP_RXB1CTRL, 0);
271+
272+
// Part added
273+
/*modify_register(
274+
spi, MCP_RXB0CTRL, MCP_RXB_RX_MASK | MCP_RXB_BUKT_MASK, MCP_RXB_RX_ANY | MCP_RXB_BUKT_MASK);
275+
276+
modify_register(spi, MCP_RXB1CTRL, MCP_RXB_RX_MASK, MCP_RXB_RX_ANY);*/
214277
}
215278

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

301-
// To write the mask-filters for the chip
302-
void write_mf(FuriHalSpiBusHandle* spi, uint8_t address, uint8_t ext, uint32_t id) {
303-
uint16_t canId = (uint16_t)(id & 0x0FFFF);
304-
uint8_t bufData[4];
305-
306-
if(ext) {
307-
bufData[MCP_EID0] = (uint8_t)(canId & 0xFF);
308-
bufData[MCP_EID8] = (uint8_t)(canId >> 8);
309-
canId = (uint16_t)(id >> 16);
310-
bufData[MCP_SIDL] = (uint8_t)(canId & 0x03);
311-
bufData[MCP_SIDL] += (uint8_t)((canId & 0x1C) << 3);
312-
bufData[MCP_SIDL] |= MCP_TXB_EXIDE_M;
313-
bufData[MCP_SIDH] = (uint8_t)(canId >> 5);
314-
} else {
315-
bufData[MCP_SIDL] = (uint8_t)((canId & 0x07) << 5);
316-
bufData[MCP_SIDH] = (uint8_t)(canId >> 3);
317-
bufData[MCP_EID0] = 0;
318-
bufData[MCP_EID8] = 0;
319-
}
320-
321-
uint8_t instruction[] = {INSTRUCTION_WRITE, address};
322-
323-
furi_hal_spi_acquire(spi);
324-
furi_hal_spi_bus_tx(spi, instruction, sizeof(instruction), TIMEOUT_SPI);
325-
326-
furi_hal_spi_bus_tx(spi, bufData, 4, TIMEOUT_SPI);
327-
furi_hal_spi_release(spi);
328-
}
329-
330364
// To set a Mask
331365
void init_mask(MCP2515* mcp_can, uint8_t num_mask, uint32_t mask) {
332366
FuriHalSpiBusHandle* spi = mcp_can->spi;
@@ -446,12 +480,13 @@ ERROR_CAN read_can_message(MCP2515* mcp_can, CANFRAME* frame) {
446480
ERROR_CAN ret = ERROR_OK;
447481
FuriHalSpiBusHandle* spi = mcp_can->spi;
448482

449-
uint8_t status = read_rx_tx_status(spi);
483+
uint8_t status = 0;
484+
485+
mcp_get_status(spi, &status);
450486

451487
if(status & MCP_RX0IF) {
452488
read_frame(spi, frame, INSTRUCTION_READ_RX0);
453489
modify_register(spi, MCP_CANINTF, MCP_RX0IF, 0);
454-
455490
} else if(status & MCP_RX1IF) {
456491
read_frame(spi, frame, INSTRUCTION_READ_RX1);
457492
modify_register(spi, MCP_CANINTF, MCP_RX1IF, 0);
@@ -646,6 +681,48 @@ ERROR_CAN send_can_frame(MCP2515* mcp_can, CANFRAME* frame) {
646681
return send_can_message(spi, frame, free_buffer);
647682
}
648683

684+
uint8_t read_detection_baudrate(FuriHalSpiBusHandle* spi) {
685+
uint8_t data_canintf = 0;
686+
687+
uint8_t instruction[] = {INSTRUCTION_READ, MCP_CANINTF};
688+
furi_hal_spi_acquire(spi);
689+
furi_hal_spi_bus_tx(spi, instruction, sizeof(instruction), TIMEOUT_SPI);
690+
furi_hal_spi_bus_rx(spi, &data_canintf, 1, TIMEOUT_SPI);
691+
furi_hal_spi_release(spi);
692+
693+
return (data_canintf & 0xf0);
694+
}
695+
696+
// Function to detect the baudrate
697+
ERROR_CAN is_this_bitrate(MCP2515* mcp_can, MCP_BITRATE bitrate) {
698+
FuriHalSpiBusHandle* spi = mcp_can->spi;
699+
ERROR_CAN ret = ERROR_OK;
700+
701+
set_config_mode(mcp_can);
702+
703+
mcp_set_bitrate(spi, bitrate, mcp_can->clck);
704+
705+
set_listen_only_mode(mcp_can);
706+
707+
if(check_receive(mcp_can) == ERROR_NOMSG) return ERROR_NOMSG;
708+
709+
uint8_t data_canintf = 0;
710+
711+
uint8_t instruction[] = {INSTRUCTION_READ, MCP_CANINTF};
712+
furi_hal_spi_acquire(spi);
713+
furi_hal_spi_bus_tx(spi, instruction, sizeof(instruction), TIMEOUT_SPI);
714+
furi_hal_spi_bus_rx(spi, &data_canintf, 1, TIMEOUT_SPI);
715+
furi_hal_spi_release(spi);
716+
717+
data_canintf &= 0x80;
718+
719+
if(data_canintf == 0x80) ret = ERROR_FAIL;
720+
721+
set_register(spi, MCP_CANINTF, 0);
722+
723+
return ret;
724+
}
725+
649726
// This function works to alloc the struct
650727
MCP2515* mcp_alloc(MCP_MODE mode, MCP_CLOCK clck, MCP_BITRATE bitrate) {
651728
MCP2515* mcp_can = malloc(sizeof(MCP2515));
@@ -656,12 +733,18 @@ MCP2515* mcp_alloc(MCP_MODE mode, MCP_CLOCK clck, MCP_BITRATE bitrate) {
656733
return mcp_can;
657734
}
658735

659-
// To free
660-
void free_mcp2515(MCP2515* mcp_can) {
736+
// To deinit
737+
void deinit_mcp2515(MCP2515* mcp_can) {
661738
mcp_reset(mcp_can->spi);
662739
furi_hal_spi_bus_handle_deinit(mcp_can->spi);
663740
}
664741

742+
// free instance
743+
void free_mcp2515(MCP2515* mcp_can) {
744+
free(mcp_can->spi);
745+
free(mcp_can);
746+
}
747+
665748
// This function starts the SPI communication and set the MCP2515 device
666749
ERROR_CAN mcp2515_start(MCP2515* mcp_can) {
667750
furi_hal_spi_bus_handle_init(mcp_can->spi);
@@ -670,6 +753,8 @@ ERROR_CAN mcp2515_start(MCP2515* mcp_can) {
670753

671754
mcp_reset(mcp_can->spi);
672755

756+
set_new_mode(mcp_can, MODE_CONFIG);
757+
673758
mcp_set_bitrate(mcp_can->spi, mcp_can->bitRate, mcp_can->clck);
674759

675760
init_can_buffer(mcp_can->spi);

Canbus_app/libraries/mcp_can_2515.h

+6
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@ typedef enum {
309309
ERROR_NOMSG = 5,
310310
ERROR_GET_TXB_FTIMEOUT = 6,
311311
ERROR_SEND_MSG_TIMEOUT = 7,
312+
ERROR_WRONG_BITRATE = 8,
312313
} ERROR_CAN;
313314

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

357358
// To close the MCP2515
359+
void deinit_mcp2515(MCP2515* mcp_can);
360+
361+
// free instance
358362
void free_mcp2515(MCP2515* mcp_can);
359363

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

393+
ERROR_CAN is_this_bitrate(MCP2515* mcp_can, MCP_BITRATE bitrate);
394+
389395
ERROR_CAN send_can_frame(MCP2515* mcp_can,
390396
CANFRAME* frame); // Send a CANBUS Frame
391397

Canbus_app/libraries/pid_library.c

+1-3
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,6 @@ void separate_codes(CANFRAME* frames, uint16_t* save_codes, uint8_t length) {
241241
void get_dtc(uint16_t numerical_code, char* dtc_code) {
242242
char* first;
243243

244-
UNUSED(dtc_code);
245-
246244
FuriString* text = furi_string_alloc();
247245

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

457455
// It works to free
458456
void pid_deinit(OBDII* obdii) {
459-
free_mcp2515(obdii->CAN);
457+
deinit_mcp2515(obdii->CAN);
460458
free(obdii->codes);
461459
}
462460

0 commit comments

Comments
 (0)