Skip to content

Commit

Permalink
feat: send and receive multiple frames with a function
Browse files Browse the repository at this point in the history
  • Loading branch information
AdonaiDiazEsparza committed Nov 26, 2024
1 parent 8e4d6ca commit b9c2749
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 11 deletions.
3 changes: 3 additions & 0 deletions Canbus_app/app_user.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@

#define MESSAGE_ERROR 0xF0

#define UDS_REQUEST_ID_DEFAULT 0x7e0
#define UDS_RESPONSE_ID_DEFAULT 0x7e8

typedef enum {
WorkerflagStop = (1 << 0),
WorkerflagReceived = (1 << 1),
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.
95 changes: 88 additions & 7 deletions Canbus_app/libraries/uds_library.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ bool read_frames_uds(MCP2515* CAN, uint32_t id, CANFRAME* frame) {

} while((time_delay < 6000));

log_exception("Error");
// log_exception("Error");

return false;
}
Expand Down Expand Up @@ -144,8 +144,8 @@ bool uds_multi_frame_request(
uint8_t* data,
uint8_t length,
CANFRAME* canframes_to_send,
uint8_t count_of_frames_to_received,
CANFRAME* canframes_to_received) {
UNUSED(canframes_to_received);
uint8_t size_frames_to_send = 1;

// Condition to know the count of frames to send if the data need more frames
Expand All @@ -159,6 +159,8 @@ bool uds_multi_frame_request(

// log_info("count of frames to send: %u", size_frames_to_send);

// log_info("Start"); // 0

// Set the frames if the data need more than one can frame
if(size_frames_to_send > 1) {
canframes_to_send[0].buffer[0] = 0x10; // Set the byte to indicate the first frame
Expand All @@ -184,8 +186,8 @@ bool uds_multi_frame_request(
}
}
}

}

// If the data only needs one frame
else {
canframes_to_send[0].canId = uds->id_to_send;
Expand All @@ -196,13 +198,92 @@ bool uds_multi_frame_request(
}
}

/* Just for debbug
canframes_to_send[size_frames_to_send].canId = uds->id_to_send;
canframes_to_send[size_frames_to_send].data_lenght = 3;
canframes_to_send[size_frames_to_send].buffer[0] = 0x30;

log_info("Messages already set, count of frames: %u", size_frames_to_send); // 1

// Just for debbug
FuriString* text = furi_string_alloc();

log_info("------------To send--------------------------");

for(uint8_t j = 0; j < size_frames_to_send; j++) {
log_info("--------------Frame %u-----------------------", j);
furi_string_reset(text);
furi_string_cat_printf(text, "%lx\t", canframes_to_send[j].canId);
for(uint8_t i = 0; i < canframes_to_send[j].data_lenght; i++) {
log_info("data[%u]: %x ", i, canframes_to_send[j].buffer[i]);
furi_string_cat_printf(text, "%x ", canframes_to_send[j].buffer[i]);
}
}*/

log_info(furi_string_get_cstr(text));
}

furi_string_free(text);

// From here is the work to send de canbus data

// Send the first frame
if(send_can_frame(uds->CAN, &canframes_to_send[0]) != ERROR_OK) {
log_exception("First message wasnt send");
return false;
}

// Wait message of the response
if(!read_frames_uds(uds->CAN, uds->id_to_received, &canframes_to_received[0])) {
log_exception("message wasnt received");
return false;
}

// To received multiple frames
if(canframes_to_received[0].buffer[0] == 0x10 && count_of_frames_to_received > 1) {
// If there no more data will pass this code
send_can_frame(uds->CAN, &canframes_to_send[size_frames_to_send]);

for(uint8_t i = 1; i < count_of_frames_to_received; i++) {
if(!read_frames_uds(uds->CAN, uds->id_to_received, &canframes_to_received[i])) break;
}
}

// To know if it is only one frame to send
if(size_frames_to_send == 1) {
canframes_to_send[size_frames_to_send].canId = 0;
return true;
}

/*
Here is the end for only one frame received.
The next code describe how to send multple frames
*/

// If the flow control is not ok
if(canframes_to_received[0].buffer[0] != 0x30) {
log_exception("Message to flow_control was a error");
return false;
}

// Send the rest of the data
for(uint8_t i = 1; i < size_frames_to_send; i++) {
send_can_frame(uds->CAN, &canframes_to_send[i]);
}

// Read the first ECU's response
if(!read_frames_uds(uds->CAN, uds->id_to_received, &canframes_to_received[0])) {
log_exception("Error to read the frame");
return false;
}

// To received multiple frames
if(canframes_to_received[0].buffer[0] == 0x10 && count_of_frames_to_received > 1) {
// If there no more data will pass this code
send_can_frame(uds->CAN, &canframes_to_send[size_frames_to_send]);

for(uint8_t i = 1; i < count_of_frames_to_received; i++) {
if(!read_frames_uds(uds->CAN, uds->id_to_received, &canframes_to_received[i])) break;
}
}

canframes_to_send[size_frames_to_send].canId = 0;

return true;
}
1 change: 1 addition & 0 deletions Canbus_app/libraries/uds_library.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ bool uds_multi_frame_request(
uint8_t* data,
uint8_t length,
CANFRAME* canframes_to_send,
uint8_t count_of_frames_to_received,
CANFRAME* canframes_to_received);

// Get VIN Number
Expand Down
42 changes: 38 additions & 4 deletions Canbus_app/scenes/UDSOptions/UDSManualRequest.c
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ void app_scene_uds_single_frame_request_response_on_enter(void* context) {
text_box_reset(app->textBox);
text_box_set_focus(app->textBox, TextBoxFocusEnd);

app->thread = furi_thread_alloc_ex("ManualUDS", 1024 * 3, uds_multiframe_request_thread, app);
app->thread = furi_thread_alloc_ex("ManualUDS", 10 * 1024, uds_multiframe_request_thread, app);
furi_thread_start(app->thread);

view_dispatcher_switch_to_view(app->view_dispatcher, TextBoxView);
Expand Down Expand Up @@ -361,7 +361,6 @@ static int32_t uds_multiframe_request_thread(void* context) {
App* app = context;
FuriString* text = app->text;
furi_string_reset(text);
furi_string_cat_printf(text, "On Development");
text_box_set_text(app->textBox, furi_string_get_cstr(text));

MCP2515* CAN = app->mcp_can;
Expand All @@ -370,6 +369,11 @@ static int32_t uds_multiframe_request_thread(void* context) {
uds_service_alloc(id_request, id_response, CAN->mode, CAN->clck, CAN->bitRate);

bool run = uds_init(uds_service);

furi_delay_ms(500);

log_info("Here");

if(run) {
CANFRAME canframes_to_received[count_of_frames];

Expand All @@ -379,8 +383,38 @@ static int32_t uds_multiframe_request_thread(void* context) {

memset(canframes_to_send, 0, sizeof(canframes_to_send));

uds_multi_frame_request(
uds_service, data_to_send, count_of_bytes, canframes_to_send, canframes_to_received);
if(uds_multi_frame_request(
uds_service,
data_to_send,
count_of_bytes,
canframes_to_send,
count_of_frames,
canframes_to_received)) {
for(uint8_t i = 0; i < 15; i++) {
if(canframes_to_send[i].canId != uds_service->id_to_send) break;

furi_string_cat_printf(text, "->%lx ", canframes_to_send[i].canId);
for(uint8_t j = 0; j < canframes_to_send[i].data_lenght; j++) {
furi_string_cat_printf(text, "%x ", canframes_to_send[i].buffer[j]);
}

furi_string_cat_printf(text, "\n");
}

for(uint8_t i = 0; i < count_of_frames; i++) {
if(canframes_to_received[i].canId != uds_service->id_to_received) break;
furi_string_cat_printf(text, "<-%lx ", canframes_to_received[i].canId);
for(uint8_t j = 0; j < canframes_to_received[i].data_lenght; j++) {
furi_string_cat_printf(text, "%x ", canframes_to_received[i].buffer[j]);
}

furi_string_cat_printf(text, "\n");
}

text_box_set_text(app->textBox, furi_string_get_cstr(text));
} else {
text_box_set_text(app->textBox, "Transmition Failure");
}

} else {
}
Expand Down

0 comments on commit b9c2749

Please sign in to comment.