mirror of
https://github.com/Next-Flip/Momentum-Firmware.git
synced 2026-04-24 03:29:57 -07:00
Merge unleashed
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
App(
|
||||
appid="camerasuite",
|
||||
appid="camera_suite",
|
||||
apptype=FlipperAppType.EXTERNAL,
|
||||
cdefines=["APP_CAMERA_SUITE"],
|
||||
entry_point="camera_suite_app",
|
||||
fap_author="Cody Tolene",
|
||||
fap_author="@CodyTolene @Z4urce @leedave",
|
||||
fap_category="GPIO",
|
||||
fap_description="A camera suite application for the Flipper Zero ESP32-CAM module.",
|
||||
fap_icon="icons/camera_suite.png",
|
||||
|
||||
@@ -13,7 +13,7 @@ void camera_suite_tick_event_callback(void* context) {
|
||||
scene_manager_handle_tick_event(app->scene_manager);
|
||||
}
|
||||
|
||||
//leave app if back button pressed
|
||||
// Leave app if back button pressed.
|
||||
bool camera_suite_navigation_event_callback(void* context) {
|
||||
furi_assert(context);
|
||||
CameraSuite* app = context;
|
||||
@@ -25,10 +25,10 @@ CameraSuite* camera_suite_app_alloc() {
|
||||
app->gui = furi_record_open(RECORD_GUI);
|
||||
app->notification = furi_record_open(RECORD_NOTIFICATION);
|
||||
|
||||
//Turn backlight on, believe me this makes testing your app easier
|
||||
// Turn backlight on.
|
||||
notification_message(app->notification, &sequence_display_backlight_on);
|
||||
|
||||
//Scene additions
|
||||
// Scene additions
|
||||
app->view_dispatcher = view_dispatcher_alloc();
|
||||
view_dispatcher_enable_queue(app->view_dispatcher);
|
||||
|
||||
@@ -60,17 +60,11 @@ CameraSuite* camera_suite_app_alloc() {
|
||||
CameraSuiteViewIdStartscreen,
|
||||
camera_suite_view_start_get_view(app->camera_suite_view_start));
|
||||
|
||||
app->camera_suite_view_style_1 = camera_suite_view_style_1_alloc();
|
||||
app->camera_suite_view_camera = camera_suite_view_camera_alloc();
|
||||
view_dispatcher_add_view(
|
||||
app->view_dispatcher,
|
||||
CameraSuiteViewIdScene1,
|
||||
camera_suite_view_style_1_get_view(app->camera_suite_view_style_1));
|
||||
|
||||
app->camera_suite_view_style_2 = camera_suite_view_style_2_alloc();
|
||||
view_dispatcher_add_view(
|
||||
app->view_dispatcher,
|
||||
CameraSuiteViewIdScene2,
|
||||
camera_suite_view_style_2_get_view(app->camera_suite_view_style_2));
|
||||
CameraSuiteViewIdCamera,
|
||||
camera_suite_view_camera_get_view(app->camera_suite_view_camera));
|
||||
|
||||
app->camera_suite_view_guide = camera_suite_view_guide_alloc();
|
||||
view_dispatcher_add_view(
|
||||
@@ -98,9 +92,9 @@ void camera_suite_app_free(CameraSuite* app) {
|
||||
scene_manager_free(app->scene_manager);
|
||||
|
||||
// View Dispatcher
|
||||
view_dispatcher_remove_view(app->view_dispatcher, CameraSuiteViewIdStartscreen);
|
||||
view_dispatcher_remove_view(app->view_dispatcher, CameraSuiteViewIdMenu);
|
||||
view_dispatcher_remove_view(app->view_dispatcher, CameraSuiteViewIdScene1);
|
||||
view_dispatcher_remove_view(app->view_dispatcher, CameraSuiteViewIdScene2);
|
||||
view_dispatcher_remove_view(app->view_dispatcher, CameraSuiteViewIdCamera);
|
||||
view_dispatcher_remove_view(app->view_dispatcher, CameraSuiteViewIdGuide);
|
||||
view_dispatcher_remove_view(app->view_dispatcher, CameraSuiteViewIdSettings);
|
||||
submenu_free(app->submenu);
|
||||
@@ -110,8 +104,7 @@ void camera_suite_app_free(CameraSuite* app) {
|
||||
|
||||
// Free remaining resources
|
||||
camera_suite_view_start_free(app->camera_suite_view_start);
|
||||
camera_suite_view_style_1_free(app->camera_suite_view_style_1);
|
||||
camera_suite_view_style_2_free(app->camera_suite_view_style_2);
|
||||
camera_suite_view_camera_free(app->camera_suite_view_camera);
|
||||
camera_suite_view_guide_free(app->camera_suite_view_guide);
|
||||
button_menu_free(app->button_menu);
|
||||
variable_item_list_free(app->variable_item_list);
|
||||
|
||||
@@ -4,9 +4,7 @@
|
||||
#include "scenes/camera_suite_scene.h"
|
||||
#include "views/camera_suite_view_guide.h"
|
||||
#include "views/camera_suite_view_start.h"
|
||||
#include "views/camera_suite_view_style_1.h"
|
||||
#include "views/camera_suite_view_style_2.h"
|
||||
#include <assets_icons.h>
|
||||
#include "views/camera_suite_view_camera.h"
|
||||
#include <furi.h>
|
||||
#include <furi_hal.h>
|
||||
#include <gui/gui.h>
|
||||
@@ -29,8 +27,7 @@ typedef struct {
|
||||
SceneManager* scene_manager;
|
||||
VariableItemList* variable_item_list;
|
||||
CameraSuiteViewStart* camera_suite_view_start;
|
||||
CameraSuiteViewStyle1* camera_suite_view_style_1;
|
||||
CameraSuiteViewStyle2* camera_suite_view_style_2;
|
||||
CameraSuiteViewCamera* camera_suite_view_camera;
|
||||
CameraSuiteViewGuide* camera_suite_view_guide;
|
||||
uint32_t orientation;
|
||||
uint32_t haptic;
|
||||
@@ -42,8 +39,7 @@ typedef struct {
|
||||
typedef enum {
|
||||
CameraSuiteViewIdStartscreen,
|
||||
CameraSuiteViewIdMenu,
|
||||
CameraSuiteViewIdScene1,
|
||||
CameraSuiteViewIdScene2,
|
||||
CameraSuiteViewIdCamera,
|
||||
CameraSuiteViewIdGuide,
|
||||
CameraSuiteViewIdSettings,
|
||||
} CameraSuiteViewId;
|
||||
|
||||
@@ -8,20 +8,13 @@ typedef enum {
|
||||
CameraSuiteCustomEventStartRight,
|
||||
CameraSuiteCustomEventStartOk,
|
||||
CameraSuiteCustomEventStartBack,
|
||||
// Scene events: Camera style 1
|
||||
CameraSuiteCustomEventSceneStyle1Up,
|
||||
CameraSuiteCustomEventSceneStyle1Down,
|
||||
CameraSuiteCustomEventSceneStyle1Left,
|
||||
CameraSuiteCustomEventSceneStyle1Right,
|
||||
CameraSuiteCustomEventSceneStyle1Ok,
|
||||
CameraSuiteCustomEventSceneStyle1Back,
|
||||
// Scene events: Camera style 2
|
||||
CameraSuiteCustomEventSceneStyle2Up,
|
||||
CameraSuiteCustomEventSceneStyle2Down,
|
||||
CameraSuiteCustomEventSceneStyle2Left,
|
||||
CameraSuiteCustomEventSceneStyle2Right,
|
||||
CameraSuiteCustomEventSceneStyle2Ok,
|
||||
CameraSuiteCustomEventSceneStyle2Back,
|
||||
// Scene events: Camera
|
||||
CameraSuiteCustomEventSceneCameraUp,
|
||||
CameraSuiteCustomEventSceneCameraDown,
|
||||
CameraSuiteCustomEventSceneCameraLeft,
|
||||
CameraSuiteCustomEventSceneCameraRight,
|
||||
CameraSuiteCustomEventSceneCameraOk,
|
||||
CameraSuiteCustomEventSceneCameraBack,
|
||||
// Scene events: Guide
|
||||
CameraSuiteCustomEventSceneGuideUp,
|
||||
CameraSuiteCustomEventSceneGuideDown,
|
||||
|
||||
@@ -1,35 +1,35 @@
|
||||
#include "../camera_suite.h"
|
||||
#include "../helpers/camera_suite_custom_event.h"
|
||||
#include "../views/camera_suite_view_style_1.h"
|
||||
#include "../views/camera_suite_view_camera.h"
|
||||
|
||||
static void camera_suite_view_style_1_callback(CameraSuiteCustomEvent event, void* context) {
|
||||
void camera_suite_view_camera_callback(CameraSuiteCustomEvent event, void* context) {
|
||||
furi_assert(context);
|
||||
CameraSuite* app = context;
|
||||
view_dispatcher_send_custom_event(app->view_dispatcher, event);
|
||||
}
|
||||
|
||||
void camera_suite_scene_style_1_on_enter(void* context) {
|
||||
void camera_suite_scene_camera_on_enter(void* context) {
|
||||
furi_assert(context);
|
||||
CameraSuite* app = context;
|
||||
camera_suite_view_style_1_set_callback(
|
||||
app->camera_suite_view_style_1, camera_suite_view_style_1_callback, app);
|
||||
view_dispatcher_switch_to_view(app->view_dispatcher, CameraSuiteViewIdScene1);
|
||||
camera_suite_view_camera_set_callback(
|
||||
app->camera_suite_view_camera, camera_suite_view_camera_callback, app);
|
||||
view_dispatcher_switch_to_view(app->view_dispatcher, CameraSuiteViewIdCamera);
|
||||
}
|
||||
|
||||
bool camera_suite_scene_style_1_on_event(void* context, SceneManagerEvent event) {
|
||||
bool camera_suite_scene_camera_on_event(void* context, SceneManagerEvent event) {
|
||||
CameraSuite* app = context;
|
||||
bool consumed = false;
|
||||
|
||||
if(event.type == SceneManagerEventTypeCustom) {
|
||||
switch(event.event) {
|
||||
case CameraSuiteCustomEventSceneStyle1Left:
|
||||
case CameraSuiteCustomEventSceneStyle1Right:
|
||||
case CameraSuiteCustomEventSceneStyle1Up:
|
||||
case CameraSuiteCustomEventSceneStyle1Down:
|
||||
case CameraSuiteCustomEventSceneStyle1Ok:
|
||||
case CameraSuiteCustomEventSceneCameraLeft:
|
||||
case CameraSuiteCustomEventSceneCameraRight:
|
||||
case CameraSuiteCustomEventSceneCameraUp:
|
||||
case CameraSuiteCustomEventSceneCameraDown:
|
||||
case CameraSuiteCustomEventSceneCameraOk:
|
||||
// Do nothing.
|
||||
break;
|
||||
case CameraSuiteCustomEventSceneStyle1Back:
|
||||
case CameraSuiteCustomEventSceneCameraBack:
|
||||
notification_message(app->notification, &sequence_reset_red);
|
||||
notification_message(app->notification, &sequence_reset_green);
|
||||
notification_message(app->notification, &sequence_reset_blue);
|
||||
@@ -46,7 +46,7 @@ bool camera_suite_scene_style_1_on_event(void* context, SceneManagerEvent event)
|
||||
return consumed;
|
||||
}
|
||||
|
||||
void camera_suite_scene_style_1_on_exit(void* context) {
|
||||
void camera_suite_scene_camera_on_exit(void* context) {
|
||||
CameraSuite* app = context;
|
||||
UNUSED(app);
|
||||
}
|
||||
@@ -1,6 +1,5 @@
|
||||
ADD_SCENE(camera_suite, start, Start)
|
||||
ADD_SCENE(camera_suite, menu, Menu)
|
||||
ADD_SCENE(camera_suite, style_1, Style_1)
|
||||
ADD_SCENE(camera_suite, style_2, Style_2)
|
||||
ADD_SCENE(camera_suite, camera, Camera)
|
||||
ADD_SCENE(camera_suite, guide, Guide)
|
||||
ADD_SCENE(camera_suite, settings, Settings)
|
||||
@@ -1,10 +1,8 @@
|
||||
#include "../camera_suite.h"
|
||||
|
||||
enum SubmenuIndex {
|
||||
/** Atkinson Dithering Algorithm. */
|
||||
SubmenuIndexSceneStyle1 = 10,
|
||||
/** Floyd-Steinberg Dithering Algorithm. */
|
||||
SubmenuIndexSceneStyle2,
|
||||
/** Camera. */
|
||||
SubmenuIndexSceneCamera = 10,
|
||||
/** Guide/how-to. */
|
||||
SubmenuIndexGuide,
|
||||
/** Settings menu. */
|
||||
@@ -22,16 +20,9 @@ void camera_suite_scene_menu_on_enter(void* context) {
|
||||
submenu_add_item(
|
||||
app->submenu,
|
||||
"Open Camera",
|
||||
SubmenuIndexSceneStyle1,
|
||||
SubmenuIndexSceneCamera,
|
||||
camera_suite_scene_menu_submenu_callback,
|
||||
app);
|
||||
// Staged view for the future.
|
||||
// submenu_add_item(
|
||||
// app->submenu,
|
||||
// "Test",
|
||||
// SubmenuIndexSceneStyle2,
|
||||
// camera_suite_scene_menu_submenu_callback,
|
||||
// app);
|
||||
submenu_add_item(
|
||||
app->submenu, "Guide", SubmenuIndexGuide, camera_suite_scene_menu_submenu_callback, app);
|
||||
submenu_add_item(
|
||||
@@ -56,15 +47,10 @@ bool camera_suite_scene_menu_on_event(void* context, SceneManagerEvent event) {
|
||||
view_dispatcher_stop(app->view_dispatcher);
|
||||
return true;
|
||||
} else if(event.type == SceneManagerEventTypeCustom) {
|
||||
if(event.event == SubmenuIndexSceneStyle1) {
|
||||
if(event.event == SubmenuIndexSceneCamera) {
|
||||
scene_manager_set_scene_state(
|
||||
app->scene_manager, CameraSuiteSceneMenu, SubmenuIndexSceneStyle1);
|
||||
scene_manager_next_scene(app->scene_manager, CameraSuiteSceneStyle_1);
|
||||
return true;
|
||||
} else if(event.event == SubmenuIndexSceneStyle2) {
|
||||
scene_manager_set_scene_state(
|
||||
app->scene_manager, CameraSuiteSceneMenu, SubmenuIndexSceneStyle2);
|
||||
scene_manager_next_scene(app->scene_manager, CameraSuiteSceneStyle_2);
|
||||
app->scene_manager, CameraSuiteSceneMenu, SubmenuIndexSceneCamera);
|
||||
scene_manager_next_scene(app->scene_manager, CameraSuiteSceneCamera);
|
||||
return true;
|
||||
} else if(event.event == SubmenuIndexGuide) {
|
||||
scene_manager_set_scene_state(
|
||||
|
||||
@@ -24,9 +24,9 @@ bool camera_suite_scene_start_on_event(void* context, SceneManagerEvent event) {
|
||||
switch(event.event) {
|
||||
case CameraSuiteCustomEventStartLeft:
|
||||
case CameraSuiteCustomEventStartRight:
|
||||
break;
|
||||
case CameraSuiteCustomEventStartUp:
|
||||
case CameraSuiteCustomEventStartDown:
|
||||
// Do nothing.
|
||||
break;
|
||||
case CameraSuiteCustomEventStartOk:
|
||||
scene_manager_next_scene(app->scene_manager, CameraSuiteSceneMenu);
|
||||
|
||||
@@ -1,54 +0,0 @@
|
||||
#include "../camera_suite.h"
|
||||
#include "../helpers/camera_suite_custom_event.h"
|
||||
#include "../helpers/camera_suite_haptic.h"
|
||||
#include "../helpers/camera_suite_led.h"
|
||||
#include "../views/camera_suite_view_style_2.h"
|
||||
|
||||
void camera_suite_view_style_2_callback(CameraSuiteCustomEvent event, void* context) {
|
||||
furi_assert(context);
|
||||
CameraSuite* app = context;
|
||||
view_dispatcher_send_custom_event(app->view_dispatcher, event);
|
||||
}
|
||||
|
||||
void camera_suite_scene_style_2_on_enter(void* context) {
|
||||
furi_assert(context);
|
||||
CameraSuite* app = context;
|
||||
camera_suite_view_style_2_set_callback(
|
||||
app->camera_suite_view_style_2, camera_suite_view_style_2_callback, app);
|
||||
view_dispatcher_switch_to_view(app->view_dispatcher, CameraSuiteViewIdScene2);
|
||||
}
|
||||
|
||||
bool camera_suite_scene_style_2_on_event(void* context, SceneManagerEvent event) {
|
||||
CameraSuite* app = context;
|
||||
bool consumed = false;
|
||||
|
||||
if(event.type == SceneManagerEventTypeCustom) {
|
||||
switch(event.event) {
|
||||
case CameraSuiteCustomEventSceneStyle2Left:
|
||||
case CameraSuiteCustomEventSceneStyle2Right:
|
||||
case CameraSuiteCustomEventSceneStyle2Up:
|
||||
case CameraSuiteCustomEventSceneStyle2Down:
|
||||
case CameraSuiteCustomEventSceneStyle2Ok:
|
||||
// Do nothing.
|
||||
break;
|
||||
case CameraSuiteCustomEventSceneStyle2Back:
|
||||
notification_message(app->notification, &sequence_reset_red);
|
||||
notification_message(app->notification, &sequence_reset_green);
|
||||
notification_message(app->notification, &sequence_reset_blue);
|
||||
if(!scene_manager_search_and_switch_to_previous_scene(
|
||||
app->scene_manager, CameraSuiteSceneMenu)) {
|
||||
scene_manager_stop(app->scene_manager);
|
||||
view_dispatcher_stop(app->view_dispatcher);
|
||||
}
|
||||
consumed = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return consumed;
|
||||
}
|
||||
|
||||
void camera_suite_scene_style_2_on_exit(void* context) {
|
||||
CameraSuite* app = context;
|
||||
UNUSED(app);
|
||||
}
|
||||
@@ -8,23 +8,19 @@
|
||||
#include "../helpers/camera_suite_speaker.h"
|
||||
#include "../helpers/camera_suite_led.h"
|
||||
|
||||
static CameraSuiteViewStyle1* current_instance = NULL;
|
||||
// Dithering type:
|
||||
// 0 = Floyd Steinberg (default)
|
||||
// 1 = Atkinson
|
||||
static int current_dithering = 0;
|
||||
static CameraSuiteViewCamera* current_instance = NULL;
|
||||
|
||||
struct CameraSuiteViewStyle1 {
|
||||
CameraSuiteViewStyle1Callback callback;
|
||||
struct CameraSuiteViewCamera {
|
||||
CameraSuiteViewCameraCallback callback;
|
||||
FuriStreamBuffer* rx_stream;
|
||||
FuriThread* worker_thread;
|
||||
View* view;
|
||||
void* context;
|
||||
};
|
||||
|
||||
void camera_suite_view_style_1_set_callback(
|
||||
CameraSuiteViewStyle1* instance,
|
||||
CameraSuiteViewStyle1Callback callback,
|
||||
void camera_suite_view_camera_set_callback(
|
||||
CameraSuiteViewCamera* instance,
|
||||
CameraSuiteViewCameraCallback callback,
|
||||
void* context) {
|
||||
furi_assert(instance);
|
||||
furi_assert(callback);
|
||||
@@ -32,7 +28,29 @@ void camera_suite_view_style_1_set_callback(
|
||||
instance->context = context;
|
||||
}
|
||||
|
||||
static void camera_suite_view_style_1_draw(Canvas* canvas, UartDumpModel* model) {
|
||||
// Function to draw pixels on the canvas based on camera orientation
|
||||
static void draw_pixel_by_orientation(Canvas* canvas, uint8_t x, uint8_t y, uint8_t orientation) {
|
||||
switch(orientation) {
|
||||
case 0: // Camera rotated 0 degrees (right side up, default)
|
||||
canvas_draw_dot(canvas, x, y);
|
||||
break;
|
||||
case 1: // Camera rotated 90 degrees
|
||||
canvas_draw_dot(canvas, y, FRAME_WIDTH - 1 - x);
|
||||
break;
|
||||
case 2: // Camera rotated 180 degrees (upside down)
|
||||
canvas_draw_dot(canvas, FRAME_WIDTH - 1 - x, FRAME_HEIGHT - 1 - y);
|
||||
break;
|
||||
case 3: // Camera rotated 270 degrees
|
||||
canvas_draw_dot(canvas, FRAME_HEIGHT - 1 - y, x);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static void camera_suite_view_camera_draw(Canvas* canvas, void* _model) {
|
||||
UartDumpModel* model = _model;
|
||||
|
||||
// Clear the screen.
|
||||
canvas_set_color(canvas, ColorBlack);
|
||||
|
||||
@@ -41,60 +59,17 @@ static void camera_suite_view_style_1_draw(Canvas* canvas, UartDumpModel* model)
|
||||
|
||||
CameraSuite* app = current_instance->context;
|
||||
|
||||
// Draw the pixels with rotation.
|
||||
for(size_t p = 0; p < FRAME_BUFFER_LENGTH; ++p) {
|
||||
uint8_t x = p % ROW_BUFFER_LENGTH; // 0 .. 15
|
||||
uint8_t y = p / ROW_BUFFER_LENGTH; // 0 .. 63
|
||||
|
||||
// Apply rotation
|
||||
int16_t rotated_x, rotated_y;
|
||||
switch(app->orientation) {
|
||||
case 1: // 90 degrees
|
||||
rotated_x = y;
|
||||
rotated_y = FRAME_WIDTH - 1 - x;
|
||||
break;
|
||||
case 2: // 180 degrees
|
||||
rotated_x = FRAME_WIDTH - 1 - x;
|
||||
rotated_y = FRAME_HEIGHT - 1 - y;
|
||||
break;
|
||||
case 3: // 270 degrees
|
||||
rotated_x = FRAME_HEIGHT - 1 - y;
|
||||
rotated_y = x;
|
||||
break;
|
||||
case 0: // 0 degrees
|
||||
default:
|
||||
rotated_x = x;
|
||||
rotated_y = y;
|
||||
break;
|
||||
}
|
||||
|
||||
for(uint8_t i = 0; i < 8; ++i) {
|
||||
if((model->pixels[p] & (1 << i)) != 0) {
|
||||
// Adjust the coordinates based on the new screen dimensions
|
||||
uint16_t screen_x, screen_y;
|
||||
switch(app->orientation) {
|
||||
case 1: // 90 degrees
|
||||
screen_x = rotated_x;
|
||||
screen_y = FRAME_HEIGHT - 8 + (rotated_y * 8) + i;
|
||||
break;
|
||||
case 2: // 180 degrees
|
||||
screen_x = FRAME_WIDTH - 8 + (rotated_x * 8) + i;
|
||||
screen_y = FRAME_HEIGHT - 1 - rotated_y;
|
||||
break;
|
||||
case 3: // 270 degrees
|
||||
screen_x = FRAME_WIDTH - 1 - rotated_x;
|
||||
screen_y = rotated_y * 8 + i;
|
||||
break;
|
||||
case 0: // 0 degrees
|
||||
default:
|
||||
screen_x = rotated_x * 8 + i;
|
||||
screen_y = rotated_y;
|
||||
break;
|
||||
}
|
||||
canvas_draw_dot(canvas, screen_x, screen_y);
|
||||
if((model->pixels[p] & (1 << (7 - i))) != 0) {
|
||||
draw_pixel_by_orientation(canvas, (x * 8) + i, y, app->orientation);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Draw the guide if the camera is not initialized.
|
||||
if(!model->initialized) {
|
||||
canvas_draw_icon(canvas, 74, 16, &I_DolphinCommon_56x48);
|
||||
@@ -107,15 +82,15 @@ static void camera_suite_view_style_1_draw(Canvas* canvas, UartDumpModel* model)
|
||||
}
|
||||
}
|
||||
|
||||
static void camera_suite_view_style_1_model_init(UartDumpModel* const model) {
|
||||
static void camera_suite_view_camera_model_init(UartDumpModel* const model) {
|
||||
for(size_t i = 0; i < FRAME_BUFFER_LENGTH; i++) {
|
||||
model->pixels[i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
static bool camera_suite_view_style_1_input(InputEvent* event, void* context) {
|
||||
static bool camera_suite_view_camera_input(InputEvent* event, void* context) {
|
||||
furi_assert(context);
|
||||
CameraSuiteViewStyle1* instance = context;
|
||||
CameraSuiteViewCamera* instance = context;
|
||||
if(event->type == InputTypeRelease) {
|
||||
switch(event->key) {
|
||||
default: // Stop all sounds, reset the LED.
|
||||
@@ -144,7 +119,7 @@ static bool camera_suite_view_style_1_input(InputEvent* event, void* context) {
|
||||
UartDumpModel * model,
|
||||
{
|
||||
UNUSED(model);
|
||||
instance->callback(CameraSuiteCustomEventSceneStyle1Back, instance->context);
|
||||
instance->callback(CameraSuiteCustomEventSceneCameraBack, instance->context);
|
||||
},
|
||||
true);
|
||||
break;
|
||||
@@ -159,7 +134,7 @@ static bool camera_suite_view_style_1_input(InputEvent* event, void* context) {
|
||||
camera_suite_play_happy_bump(instance->context);
|
||||
camera_suite_play_input_sound(instance->context);
|
||||
camera_suite_led_set_rgb(instance->context, 0, 0, 255);
|
||||
instance->callback(CameraSuiteCustomEventSceneStyle1Left, instance->context);
|
||||
instance->callback(CameraSuiteCustomEventSceneCameraLeft, instance->context);
|
||||
},
|
||||
true);
|
||||
break;
|
||||
@@ -174,7 +149,7 @@ static bool camera_suite_view_style_1_input(InputEvent* event, void* context) {
|
||||
camera_suite_play_happy_bump(instance->context);
|
||||
camera_suite_play_input_sound(instance->context);
|
||||
camera_suite_led_set_rgb(instance->context, 0, 0, 255);
|
||||
instance->callback(CameraSuiteCustomEventSceneStyle1Right, instance->context);
|
||||
instance->callback(CameraSuiteCustomEventSceneCameraRight, instance->context);
|
||||
},
|
||||
true);
|
||||
break;
|
||||
@@ -189,7 +164,7 @@ static bool camera_suite_view_style_1_input(InputEvent* event, void* context) {
|
||||
camera_suite_play_happy_bump(instance->context);
|
||||
camera_suite_play_input_sound(instance->context);
|
||||
camera_suite_led_set_rgb(instance->context, 0, 0, 255);
|
||||
instance->callback(CameraSuiteCustomEventSceneStyle1Up, instance->context);
|
||||
instance->callback(CameraSuiteCustomEventSceneCameraUp, instance->context);
|
||||
},
|
||||
true);
|
||||
break;
|
||||
@@ -204,18 +179,13 @@ static bool camera_suite_view_style_1_input(InputEvent* event, void* context) {
|
||||
camera_suite_play_happy_bump(instance->context);
|
||||
camera_suite_play_input_sound(instance->context);
|
||||
camera_suite_led_set_rgb(instance->context, 0, 0, 255);
|
||||
instance->callback(CameraSuiteCustomEventSceneStyle1Down, instance->context);
|
||||
instance->callback(CameraSuiteCustomEventSceneCameraDown, instance->context);
|
||||
},
|
||||
true);
|
||||
break;
|
||||
case InputKeyOk:
|
||||
if(current_dithering == 0) {
|
||||
data[0] = 'd'; // Update to Floyd Steinberg dithering.
|
||||
current_dithering = 1;
|
||||
} else {
|
||||
data[0] = 'D'; // Update to Atkinson dithering.
|
||||
current_dithering = 0;
|
||||
}
|
||||
// Switch dithering types.
|
||||
data[0] = 'D';
|
||||
with_view_model(
|
||||
instance->view,
|
||||
UartDumpModel * model,
|
||||
@@ -224,7 +194,7 @@ static bool camera_suite_view_style_1_input(InputEvent* event, void* context) {
|
||||
camera_suite_play_happy_bump(instance->context);
|
||||
camera_suite_play_input_sound(instance->context);
|
||||
camera_suite_led_set_rgb(instance->context, 0, 0, 255);
|
||||
instance->callback(CameraSuiteCustomEventSceneStyle1Ok, instance->context);
|
||||
instance->callback(CameraSuiteCustomEventSceneCameraOk, instance->context);
|
||||
},
|
||||
true);
|
||||
break;
|
||||
@@ -237,16 +207,16 @@ static bool camera_suite_view_style_1_input(InputEvent* event, void* context) {
|
||||
return true;
|
||||
}
|
||||
|
||||
static void camera_suite_view_style_1_exit(void* context) {
|
||||
static void camera_suite_view_camera_exit(void* context) {
|
||||
furi_assert(context);
|
||||
}
|
||||
|
||||
static void camera_suite_view_style_1_enter(void* context) {
|
||||
static void camera_suite_view_camera_enter(void* context) {
|
||||
// Check `context` for null. If it is null, abort program, else continue.
|
||||
furi_assert(context);
|
||||
|
||||
// Cast `context` to `CameraSuiteViewStyle1*` and store it in `instance`.
|
||||
CameraSuiteViewStyle1* instance = (CameraSuiteViewStyle1*)context;
|
||||
// Cast `context` to `CameraSuiteViewCamera*` and store it in `instance`.
|
||||
CameraSuiteViewCamera* instance = (CameraSuiteViewCamera*)context;
|
||||
|
||||
// Assign the current instance to the global variable
|
||||
current_instance = instance;
|
||||
@@ -259,7 +229,7 @@ static void camera_suite_view_style_1_enter(void* context) {
|
||||
with_view_model(
|
||||
instance->view,
|
||||
UartDumpModel * model,
|
||||
{ camera_suite_view_style_1_model_init(model); },
|
||||
{ camera_suite_view_camera_model_init(model); },
|
||||
true);
|
||||
}
|
||||
|
||||
@@ -267,8 +237,8 @@ static void camera_on_irq_cb(UartIrqEvent uartIrqEvent, uint8_t data, void* cont
|
||||
// Check `context` for null. If it is null, abort program, else continue.
|
||||
furi_assert(context);
|
||||
|
||||
// Cast `context` to `CameraSuiteViewStyle1*` and store it in `instance`.
|
||||
CameraSuiteViewStyle1* instance = context;
|
||||
// Cast `context` to `CameraSuiteViewCamera*` and store it in `instance`.
|
||||
CameraSuiteViewCamera* instance = context;
|
||||
|
||||
// If `uartIrqEvent` is `UartIrqEventRXNE`, send the data to the
|
||||
// `rx_stream` and set the `WorkerEventRx` flag.
|
||||
@@ -319,7 +289,7 @@ static void process_ringbuffer(UartDumpModel* model, uint8_t byte) {
|
||||
|
||||
static int32_t camera_worker(void* context) {
|
||||
furi_assert(context);
|
||||
CameraSuiteViewStyle1* instance = context;
|
||||
CameraSuiteViewCamera* instance = context;
|
||||
|
||||
while(1) {
|
||||
uint32_t events =
|
||||
@@ -348,14 +318,17 @@ static int32_t camera_worker(void* context) {
|
||||
false);
|
||||
}
|
||||
} while(length > 0);
|
||||
|
||||
with_view_model(
|
||||
instance->view, UartDumpModel * model, { UNUSED(model); }, true);
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
CameraSuiteViewStyle1* camera_suite_view_style_1_alloc() {
|
||||
CameraSuiteViewStyle1* instance = malloc(sizeof(CameraSuiteViewStyle1));
|
||||
CameraSuiteViewCamera* camera_suite_view_camera_alloc() {
|
||||
CameraSuiteViewCamera* instance = malloc(sizeof(CameraSuiteViewCamera));
|
||||
|
||||
instance->view = view_alloc();
|
||||
|
||||
@@ -364,15 +337,15 @@ CameraSuiteViewStyle1* camera_suite_view_style_1_alloc() {
|
||||
// Set up views
|
||||
view_allocate_model(instance->view, ViewModelTypeLocking, sizeof(UartDumpModel));
|
||||
view_set_context(instance->view, instance); // furi_assert crashes in events without this
|
||||
view_set_draw_callback(instance->view, (ViewDrawCallback)camera_suite_view_style_1_draw);
|
||||
view_set_input_callback(instance->view, camera_suite_view_style_1_input);
|
||||
view_set_enter_callback(instance->view, camera_suite_view_style_1_enter);
|
||||
view_set_exit_callback(instance->view, camera_suite_view_style_1_exit);
|
||||
view_set_draw_callback(instance->view, (ViewDrawCallback)camera_suite_view_camera_draw);
|
||||
view_set_input_callback(instance->view, camera_suite_view_camera_input);
|
||||
view_set_enter_callback(instance->view, camera_suite_view_camera_enter);
|
||||
view_set_exit_callback(instance->view, camera_suite_view_camera_exit);
|
||||
|
||||
with_view_model(
|
||||
instance->view,
|
||||
UartDumpModel * model,
|
||||
{ camera_suite_view_style_1_model_init(model); },
|
||||
{ camera_suite_view_camera_model_init(model); },
|
||||
true);
|
||||
|
||||
instance->worker_thread = furi_thread_alloc_ex("UsbUartWorker", 2048, camera_worker, instance);
|
||||
@@ -390,7 +363,7 @@ CameraSuiteViewStyle1* camera_suite_view_style_1_alloc() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
void camera_suite_view_style_1_free(CameraSuiteViewStyle1* instance) {
|
||||
void camera_suite_view_camera_free(CameraSuiteViewCamera* instance) {
|
||||
furi_assert(instance);
|
||||
|
||||
with_view_model(
|
||||
@@ -407,7 +380,7 @@ void camera_suite_view_style_1_free(CameraSuiteViewStyle1* instance) {
|
||||
}
|
||||
}
|
||||
|
||||
View* camera_suite_view_style_1_get_view(CameraSuiteViewStyle1* instance) {
|
||||
View* camera_suite_view_camera_get_view(CameraSuiteViewCamera* instance) {
|
||||
furi_assert(instance);
|
||||
return instance->view;
|
||||
}
|
||||
}
|
||||
@@ -25,11 +25,12 @@
|
||||
#define FRAME_WIDTH 128
|
||||
#define FRAME_HEIGHT 64
|
||||
#define FRAME_BIT_DEPTH 1
|
||||
#define FRAME_BUFFER_LENGTH \
|
||||
(FRAME_WIDTH * FRAME_HEIGHT * FRAME_BIT_DEPTH / 8) // 128*64*1 / 8 = 1024
|
||||
#define ROW_BUFFER_LENGTH (FRAME_WIDTH / 8) // 128/8 = 16
|
||||
#define RING_BUFFER_LENGTH (ROW_BUFFER_LENGTH + 3) // ROW_BUFFER_LENGTH + Header => 16 + 3 = 19
|
||||
#define LAST_ROW_INDEX (FRAME_BUFFER_LENGTH - ROW_BUFFER_LENGTH) // 1024 - 16 = 1008
|
||||
#define FRAME_BUFFER_LENGTH 1024
|
||||
#define ROW_BUFFER_LENGTH 16
|
||||
#define RING_BUFFER_LENGTH 19
|
||||
#define LAST_ROW_INDEX 1008
|
||||
|
||||
extern const Icon I_DolphinCommon_56x48;
|
||||
|
||||
typedef struct UartDumpModel UartDumpModel;
|
||||
|
||||
@@ -41,20 +42,20 @@ struct UartDumpModel {
|
||||
uint8_t row_ringbuffer[RING_BUFFER_LENGTH];
|
||||
};
|
||||
|
||||
typedef struct CameraSuiteViewStyle1 CameraSuiteViewStyle1;
|
||||
typedef struct CameraSuiteViewCamera CameraSuiteViewCamera;
|
||||
|
||||
typedef void (*CameraSuiteViewStyle1Callback)(CameraSuiteCustomEvent event, void* context);
|
||||
typedef void (*CameraSuiteViewCameraCallback)(CameraSuiteCustomEvent event, void* context);
|
||||
|
||||
void camera_suite_view_style_1_set_callback(
|
||||
CameraSuiteViewStyle1* camera_suite_view_style_1,
|
||||
CameraSuiteViewStyle1Callback callback,
|
||||
void camera_suite_view_camera_set_callback(
|
||||
CameraSuiteViewCamera* camera_suite_view_camera,
|
||||
CameraSuiteViewCameraCallback callback,
|
||||
void* context);
|
||||
|
||||
CameraSuiteViewStyle1* camera_suite_view_style_1_alloc();
|
||||
CameraSuiteViewCamera* camera_suite_view_camera_alloc();
|
||||
|
||||
void camera_suite_view_style_1_free(CameraSuiteViewStyle1* camera_suite_static);
|
||||
void camera_suite_view_camera_free(CameraSuiteViewCamera* camera_suite_static);
|
||||
|
||||
View* camera_suite_view_style_1_get_view(CameraSuiteViewStyle1* camera_suite_static);
|
||||
View* camera_suite_view_camera_get_view(CameraSuiteViewCamera* camera_suite_static);
|
||||
|
||||
typedef enum {
|
||||
// Reserved for StreamBuffer internal event
|
||||
@@ -1,249 +0,0 @@
|
||||
#include "../camera_suite.h"
|
||||
#include <furi.h>
|
||||
#include <furi_hal.h>
|
||||
#include <input/input.h>
|
||||
#include <gui/elements.h>
|
||||
#include <dolphin/dolphin.h>
|
||||
#include "../helpers/camera_suite_haptic.h"
|
||||
#include "../helpers/camera_suite_speaker.h"
|
||||
#include "../helpers/camera_suite_led.h"
|
||||
|
||||
struct CameraSuiteViewStyle2 {
|
||||
View* view;
|
||||
CameraSuiteViewStyle2Callback callback;
|
||||
void* context;
|
||||
};
|
||||
|
||||
typedef struct {
|
||||
int screen_text;
|
||||
} CameraSuiteViewStyle2Model;
|
||||
|
||||
char buttonText[11][14] = {
|
||||
"",
|
||||
"Press Up",
|
||||
"Press Down",
|
||||
"Press Left",
|
||||
"Press Right",
|
||||
"Press Ok",
|
||||
"Release Up",
|
||||
"Release Down",
|
||||
"Release Left",
|
||||
"Release Right",
|
||||
"Release Ok",
|
||||
};
|
||||
|
||||
void camera_suite_view_style_2_set_callback(
|
||||
CameraSuiteViewStyle2* instance,
|
||||
CameraSuiteViewStyle2Callback callback,
|
||||
void* context) {
|
||||
furi_assert(instance);
|
||||
furi_assert(callback);
|
||||
instance->callback = callback;
|
||||
instance->context = context;
|
||||
}
|
||||
|
||||
void camera_suite_view_style_2_draw(Canvas* canvas, CameraSuiteViewStyle2Model* model) {
|
||||
canvas_clear(canvas);
|
||||
canvas_set_color(canvas, ColorBlack);
|
||||
canvas_set_font(canvas, FontPrimary);
|
||||
canvas_draw_str_aligned(canvas, 0, 10, AlignLeft, AlignTop, "Scene 2: Input Examples");
|
||||
canvas_set_font(canvas, FontSecondary);
|
||||
char* strInput = malloc(15);
|
||||
strcpy(strInput, buttonText[model->screen_text]);
|
||||
canvas_draw_str_aligned(canvas, 0, 22, AlignLeft, AlignTop, strInput);
|
||||
free(strInput);
|
||||
}
|
||||
|
||||
static void camera_suite_view_style_2_model_init(CameraSuiteViewStyle2Model* const model) {
|
||||
model->screen_text = 0;
|
||||
}
|
||||
|
||||
bool camera_suite_view_style_2_input(InputEvent* event, void* context) {
|
||||
furi_assert(context);
|
||||
CameraSuiteViewStyle2* instance = context;
|
||||
if(event->type == InputTypeRelease) {
|
||||
switch(event->key) {
|
||||
case InputKeyBack:
|
||||
with_view_model(
|
||||
instance->view,
|
||||
CameraSuiteViewStyle2Model * model,
|
||||
{
|
||||
UNUSED(model);
|
||||
camera_suite_stop_all_sound(instance->context);
|
||||
instance->callback(CameraSuiteCustomEventSceneStyle2Back, instance->context);
|
||||
camera_suite_play_long_bump(instance->context);
|
||||
},
|
||||
true);
|
||||
break;
|
||||
case InputKeyUp:
|
||||
with_view_model(
|
||||
instance->view,
|
||||
CameraSuiteViewStyle2Model * model,
|
||||
{
|
||||
model->screen_text = 6;
|
||||
camera_suite_play_bad_bump(instance->context);
|
||||
camera_suite_stop_all_sound(instance->context);
|
||||
camera_suite_led_set_rgb(instance->context, 255, 0, 255);
|
||||
},
|
||||
true);
|
||||
break;
|
||||
case InputKeyDown:
|
||||
with_view_model(
|
||||
instance->view,
|
||||
CameraSuiteViewStyle2Model * model,
|
||||
{
|
||||
model->screen_text = 7;
|
||||
camera_suite_play_bad_bump(instance->context);
|
||||
camera_suite_stop_all_sound(instance->context);
|
||||
camera_suite_led_set_rgb(instance->context, 255, 255, 0);
|
||||
},
|
||||
true);
|
||||
break;
|
||||
case InputKeyLeft:
|
||||
with_view_model(
|
||||
instance->view,
|
||||
CameraSuiteViewStyle2Model * model,
|
||||
{
|
||||
model->screen_text = 8;
|
||||
camera_suite_play_bad_bump(instance->context);
|
||||
camera_suite_stop_all_sound(instance->context);
|
||||
camera_suite_led_set_rgb(instance->context, 0, 255, 255);
|
||||
},
|
||||
true);
|
||||
break;
|
||||
case InputKeyRight:
|
||||
with_view_model(
|
||||
instance->view,
|
||||
CameraSuiteViewStyle2Model * model,
|
||||
{
|
||||
model->screen_text = 9;
|
||||
camera_suite_play_bad_bump(instance->context);
|
||||
camera_suite_stop_all_sound(instance->context);
|
||||
camera_suite_led_set_rgb(instance->context, 255, 0, 0);
|
||||
},
|
||||
true);
|
||||
break;
|
||||
case InputKeyOk:
|
||||
with_view_model(
|
||||
instance->view,
|
||||
CameraSuiteViewStyle2Model * model,
|
||||
{
|
||||
model->screen_text = 10;
|
||||
camera_suite_play_bad_bump(instance->context);
|
||||
camera_suite_stop_all_sound(instance->context);
|
||||
camera_suite_led_set_rgb(instance->context, 255, 255, 255);
|
||||
},
|
||||
true);
|
||||
break;
|
||||
case InputKeyMAX:
|
||||
break;
|
||||
}
|
||||
} else if(event->type == InputTypePress) {
|
||||
switch(event->key) {
|
||||
case InputKeyUp:
|
||||
with_view_model(
|
||||
instance->view,
|
||||
CameraSuiteViewStyle2Model * model,
|
||||
{
|
||||
model->screen_text = 1;
|
||||
camera_suite_play_happy_bump(instance->context);
|
||||
camera_suite_play_input_sound(instance->context);
|
||||
},
|
||||
true);
|
||||
break;
|
||||
case InputKeyDown:
|
||||
with_view_model(
|
||||
instance->view,
|
||||
CameraSuiteViewStyle2Model * model,
|
||||
{
|
||||
model->screen_text = 2;
|
||||
camera_suite_play_happy_bump(instance->context);
|
||||
camera_suite_play_input_sound(instance->context);
|
||||
},
|
||||
true);
|
||||
break;
|
||||
case InputKeyLeft:
|
||||
with_view_model(
|
||||
instance->view,
|
||||
CameraSuiteViewStyle2Model * model,
|
||||
{
|
||||
model->screen_text = 3;
|
||||
camera_suite_play_happy_bump(instance->context);
|
||||
camera_suite_play_input_sound(instance->context);
|
||||
},
|
||||
true);
|
||||
break;
|
||||
case InputKeyRight:
|
||||
with_view_model(
|
||||
instance->view,
|
||||
CameraSuiteViewStyle2Model * model,
|
||||
{
|
||||
model->screen_text = 4;
|
||||
camera_suite_play_happy_bump(instance->context);
|
||||
camera_suite_play_input_sound(instance->context);
|
||||
},
|
||||
true);
|
||||
break;
|
||||
case InputKeyOk:
|
||||
with_view_model(
|
||||
instance->view,
|
||||
CameraSuiteViewStyle2Model * model,
|
||||
{
|
||||
model->screen_text = 5;
|
||||
camera_suite_play_happy_bump(instance->context);
|
||||
camera_suite_play_input_sound(instance->context);
|
||||
},
|
||||
true);
|
||||
break;
|
||||
case InputKeyBack:
|
||||
case InputKeyMAX:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void camera_suite_view_style_2_exit(void* context) {
|
||||
furi_assert(context);
|
||||
CameraSuite* app = context;
|
||||
camera_suite_stop_all_sound(app);
|
||||
//camera_suite_led_reset(app);
|
||||
}
|
||||
|
||||
void camera_suite_view_style_2_enter(void* context) {
|
||||
furi_assert(context);
|
||||
dolphin_deed(DolphinDeedPluginStart);
|
||||
}
|
||||
|
||||
CameraSuiteViewStyle2* camera_suite_view_style_2_alloc() {
|
||||
CameraSuiteViewStyle2* instance = malloc(sizeof(CameraSuiteViewStyle2));
|
||||
instance->view = view_alloc();
|
||||
view_allocate_model(instance->view, ViewModelTypeLocking, sizeof(CameraSuiteViewStyle2Model));
|
||||
view_set_context(instance->view, instance);
|
||||
view_set_draw_callback(instance->view, (ViewDrawCallback)camera_suite_view_style_2_draw);
|
||||
view_set_input_callback(instance->view, camera_suite_view_style_2_input);
|
||||
//view_set_enter_callback(instance->view, camera_suite_view_style_2_enter);
|
||||
view_set_exit_callback(instance->view, camera_suite_view_style_2_exit);
|
||||
|
||||
with_view_model(
|
||||
instance->view,
|
||||
CameraSuiteViewStyle2Model * model,
|
||||
{ camera_suite_view_style_2_model_init(model); },
|
||||
true);
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
void camera_suite_view_style_2_free(CameraSuiteViewStyle2* instance) {
|
||||
furi_assert(instance);
|
||||
|
||||
view_free(instance->view);
|
||||
free(instance);
|
||||
}
|
||||
|
||||
View* camera_suite_view_style_2_get_view(CameraSuiteViewStyle2* instance) {
|
||||
furi_assert(instance);
|
||||
|
||||
return instance->view;
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <gui/view.h>
|
||||
#include "../helpers/camera_suite_custom_event.h"
|
||||
|
||||
typedef struct CameraSuiteViewStyle2 CameraSuiteViewStyle2;
|
||||
|
||||
typedef void (*CameraSuiteViewStyle2Callback)(CameraSuiteCustomEvent event, void* context);
|
||||
|
||||
void camera_suite_view_style_2_set_callback(
|
||||
CameraSuiteViewStyle2* instance,
|
||||
CameraSuiteViewStyle2Callback callback,
|
||||
void* context);
|
||||
|
||||
CameraSuiteViewStyle2* camera_suite_view_style_2_alloc();
|
||||
|
||||
void camera_suite_view_style_2_free(CameraSuiteViewStyle2* camera_suite_static);
|
||||
|
||||
View* camera_suite_view_style_2_get_view(CameraSuiteViewStyle2* boilerpate_static);
|
||||
@@ -1,64 +0,0 @@
|
||||
#include "wifi_marauder_app_i.h"
|
||||
#include "wifi_marauder_pcap.h"
|
||||
|
||||
void wifi_marauder_get_prefix_from_sniff_cmd(char* dest, const char* command) {
|
||||
int start, end, delta;
|
||||
start = strlen("sniff");
|
||||
end = strcspn(command, " ");
|
||||
delta = end - start;
|
||||
strncpy(dest, command + start, end - start);
|
||||
dest[delta] = '\0';
|
||||
}
|
||||
|
||||
void wifi_marauder_get_prefix_from_cmd(char* dest, const char* command) {
|
||||
int end;
|
||||
end = strcspn(command, " ");
|
||||
strncpy(dest, command, end);
|
||||
dest[end] = '\0';
|
||||
}
|
||||
|
||||
void wifi_marauder_create_pcap_file(WifiMarauderApp* app) {
|
||||
char prefix[10];
|
||||
char capture_file_path[100];
|
||||
wifi_marauder_get_prefix_from_sniff_cmd(prefix, app->selected_tx_string);
|
||||
|
||||
int i = 0;
|
||||
do {
|
||||
snprintf(
|
||||
capture_file_path,
|
||||
sizeof(capture_file_path),
|
||||
"%s/%s_%d.pcap",
|
||||
MARAUDER_APP_FOLDER_PCAPS,
|
||||
prefix,
|
||||
i);
|
||||
i++;
|
||||
} while(storage_file_exists(app->storage, capture_file_path));
|
||||
|
||||
if(!storage_file_open(app->capture_file, capture_file_path, FSAM_WRITE, FSOM_CREATE_ALWAYS)) {
|
||||
dialog_message_show_storage_error(app->dialogs, "Cannot open pcap file");
|
||||
}
|
||||
}
|
||||
|
||||
void wifi_marauder_create_log_file(WifiMarauderApp* app) {
|
||||
char prefix[10];
|
||||
char log_file_path[100];
|
||||
wifi_marauder_get_prefix_from_cmd(prefix, app->selected_tx_string);
|
||||
|
||||
int i = 0;
|
||||
do {
|
||||
snprintf(
|
||||
log_file_path,
|
||||
sizeof(log_file_path),
|
||||
"%s/%s_%d.log",
|
||||
MARAUDER_APP_FOLDER_LOGS,
|
||||
prefix,
|
||||
i);
|
||||
i++;
|
||||
} while(storage_file_exists(app->storage, log_file_path));
|
||||
|
||||
if(!storage_file_open(app->log_file, log_file_path, FSAM_WRITE, FSOM_CREATE_ALWAYS)) {
|
||||
dialog_message_show_storage_error(app->dialogs, "Cannot open log file");
|
||||
} else {
|
||||
strcpy(app->log_file_path, log_file_path);
|
||||
}
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "furi_hal.h"
|
||||
|
||||
/**
|
||||
* Creates a PCAP file to store incoming packets.
|
||||
* The file name will have a prefix according to the type of scan being performed by the application (Eg: raw_0.pcap)
|
||||
*
|
||||
* @param app Application context
|
||||
*/
|
||||
void wifi_marauder_create_pcap_file(WifiMarauderApp* app);
|
||||
|
||||
/**
|
||||
* Creates a log file to store text from console output.
|
||||
* The file name will have a prefix according to the command being performed by the application (Eg: scanap_0.log)
|
||||
*
|
||||
* @param app Application context
|
||||
*/
|
||||
// same as wifi_marauder_create_pcap_file, but for log files (to save console text output)
|
||||
void wifi_marauder_create_log_file(WifiMarauderApp* app);
|
||||
@@ -1,15 +0,0 @@
|
||||
## v1.2
|
||||
|
||||
* Lux only screen now has statistics
|
||||
* Settings are now stored on SD card
|
||||
* You can choose the resolution (BH1750 only) and address for sensor
|
||||
|
||||
(thanks to @danielskowronski for contributing to this update)
|
||||
|
||||
## v1.1
|
||||
|
||||
Added support for MAX44009 sensor (thanks to @wosk)
|
||||
|
||||
## v1.0
|
||||
|
||||
Initial release for Flipper Application Catalog
|
||||
@@ -1,6 +1,7 @@
|
||||
App(
|
||||
appid="esp32_wifi_marauder",
|
||||
name="[ESP32] WiFi Marauder",
|
||||
fap_version=(6,0),
|
||||
apptype=FlipperAppType.EXTERNAL,
|
||||
entry_point="wifi_marauder_app",
|
||||
requires=["gui"],
|
||||
@@ -8,22 +9,5 @@ App(
|
||||
order=90,
|
||||
fap_icon="wifi_10px.png",
|
||||
fap_category="WiFi",
|
||||
fap_private_libs=[
|
||||
Lib(
|
||||
name="esp-serial-flasher",
|
||||
fap_include_paths=["include"],
|
||||
sources=[
|
||||
"src/esp_loader.c",
|
||||
"src/esp_targets.c",
|
||||
"src/md5_hash.c",
|
||||
"src/protocol_common.c",
|
||||
"src/protocol_uart.c",
|
||||
"src/slip.c",
|
||||
],
|
||||
cincludes=["lib/esp-serial-flasher/private_include"],
|
||||
cdefines=["SERIAL_FLASHER_INTERFACE_UART=1", "MD5_ENABLED=1"],
|
||||
),
|
||||
],
|
||||
cdefines=["SERIAL_FLASHER_INTERFACE_UART=1"],
|
||||
fap_icon_assets="assets",
|
||||
)
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
## v0.4.0
|
||||
|
||||
Added Signal Monitor (thanks @justcallmekoko!) to support new sigmon command in Marauder v0.10.5: https://github.com/justcallmekoko/ESP32Marauder/releases
|
||||
|
||||
Added keyboard and +5V support from unleashed (thanks @xMasterX!)
|
||||
@@ -1,202 +0,0 @@
|
||||
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
@@ -1,154 +0,0 @@
|
||||
# esp-serial-flasher
|
||||
|
||||
`esp-serial-flasher` is a portable C library for flashing or loading apps to RAM of Espressif SoCs from other host microcontrollers.
|
||||
|
||||
## Using the library
|
||||
Espressif SoCs are normally programmed via serial interface (UART). The port layer for the given host microcontroller has to be implemented if not available. Details can be found in section below.
|
||||
|
||||
Supported **host** microcontrollers:
|
||||
|
||||
- STM32
|
||||
- Raspberry Pi SBC
|
||||
- ESP32
|
||||
- Any MCU running Zephyr OS
|
||||
|
||||
Supported **target** microcontrollers:
|
||||
|
||||
- ESP32
|
||||
- ESP8266
|
||||
- ESP32-S2
|
||||
- ESP32-S3
|
||||
- ESP32-C3
|
||||
- ESP32-C2
|
||||
- ESP32-H2
|
||||
|
||||
Supported hardware interfaces:
|
||||
- UART
|
||||
- SPI (only for RAM download, experimental)
|
||||
|
||||
For example usage check the `examples` directory.
|
||||
|
||||
## Supporting a new host target
|
||||
|
||||
In order to support a new target, following functions have to be implemented by user:
|
||||
|
||||
- `loader_port_read()`
|
||||
- `loader_port_write()`
|
||||
- `loader_port_enter_bootloader()`
|
||||
- `loader_port_delay_ms()`
|
||||
- `loader_port_start_timer()`
|
||||
- `loader_port_remaining_time()`
|
||||
|
||||
For the SPI interface ports
|
||||
- `loader_port_spi_set_cs()`
|
||||
needs to be implemented as well.
|
||||
|
||||
The following functions are part of the [io.h](include/io.h) header for convenience, however, the user does not have to strictly follow function signatures, as there are not called directly from library.
|
||||
|
||||
- `loader_port_change_transmission_rate()`
|
||||
- `loader_port_reset_target()`
|
||||
- `loader_port_debug_print()`
|
||||
|
||||
Prototypes of all functions mentioned above can be found in [io.h](include/io.h).
|
||||
|
||||
## Configuration
|
||||
|
||||
These are the configuration toggles available to the user:
|
||||
|
||||
* `SERIAL_FLASHER_INTERFACE_UART/SERIAL_FLASHER_INTERFACE_SPI`
|
||||
|
||||
This defines the hardware interface to use. SPI interface only supports RAM download mode and is in experimental stage and can undergo changes.
|
||||
|
||||
Default: SERIAL_FLASHER_INTERFACE_UART
|
||||
|
||||
* `MD5_ENABLED`
|
||||
|
||||
If enabled, `esp-serial-flasher` is capable of verifying flash integrity after writing to flash.
|
||||
|
||||
Default: Enabled
|
||||
> Warning: As ROM bootloader of the ESP8266 does not support MD5_CHECK, this option has to be disabled!
|
||||
|
||||
* `SERIAL_FLASHER_RESET_HOLD_TIME_MS`
|
||||
|
||||
This is the time for which the reset pin is asserted when doing a hard reset in milliseconds.
|
||||
|
||||
Default: 100
|
||||
|
||||
* `SERIAL_FLASHER_BOOT_HOLD_TIME_MS`
|
||||
|
||||
This is the time for which the boot pin is asserted when doing a hard reset in milliseconds.
|
||||
|
||||
Default: 50
|
||||
|
||||
Configuration can be passed to `cmake` via command line:
|
||||
|
||||
```
|
||||
cmake -DMD5_ENABLED=1 .. && cmake --build .
|
||||
```
|
||||
|
||||
### STM32 support
|
||||
|
||||
The STM32 port makes use of STM32 HAL libraries, and these do not come with CMake support. In order to compile the project, `stm32-cmake` (a `CMake` support package) has to be pulled as submodule.
|
||||
|
||||
```
|
||||
git clone --recursive https://github.com/espressif/esp-serial-flasher.git
|
||||
```
|
||||
|
||||
If you have cloned this repository without the `--recursive` flag, you can initialize the submodule using the following command:
|
||||
|
||||
```
|
||||
git submodule update --init
|
||||
```
|
||||
|
||||
In addition to configuration parameters mentioned above, following definitions has to be set:
|
||||
|
||||
- TOOLCHAIN_PREFIX: path to arm toolchain (i.e /home/user/gcc-arm-none-eabi-9-2019-q4-major)
|
||||
- STM32Cube_DIR: path to STM32 Cube libraries (i.e /home/user/STM32Cube/Repository/STM32Cube_FW_F4_V1.25.0)
|
||||
- STM32_CHIP: name of STM32 for which project should be compiled (i.e STM32F407VG)
|
||||
- PORT: STM32
|
||||
|
||||
This can be achieved by passing definitions to the command line, such as:
|
||||
|
||||
```
|
||||
cmake -DTOOLCHAIN_PREFIX="/path_to_toolchain" -DSTM32Cube_DIR="path_to_stm32Cube" -DSTM32_CHIP="STM32F407VG" -DPORT="STM32" .. && cmake --build .
|
||||
```
|
||||
|
||||
Alternatively, those variables can be set in the top level `cmake` directory:
|
||||
|
||||
```
|
||||
set(TOOLCHAIN_PREFIX path_to_toolchain)
|
||||
set(STM32Cube_DIR path_to_stm32_HAL)
|
||||
set(STM32_CHIP STM32F407VG)
|
||||
set(PORT STM32)
|
||||
```
|
||||
|
||||
### Zephyr support
|
||||
|
||||
The Zephyr port is ready to be integrated into Zephyr apps as a Zephyr module. In the manifest file (west.yml), add:
|
||||
|
||||
```
|
||||
- name: esp-flasher
|
||||
url: https://github.com/espressif/esp-serial-flasher
|
||||
revision: master
|
||||
path: modules/lib/esp_flasher
|
||||
```
|
||||
|
||||
And add
|
||||
|
||||
```
|
||||
CONFIG_ESP_SERIAL_FLASHER=y
|
||||
CONFIG_CONSOLE_GETCHAR=y
|
||||
CONFIG_SERIAL_FLASHER_MD5_ENABLED=y
|
||||
```
|
||||
|
||||
to the project configuration `prj.conf`.
|
||||
|
||||
For the C/C++ source code, the example code provided in `examples/zephyr_example` can be used as a starting point.
|
||||
|
||||
## Licence
|
||||
|
||||
Code is distributed under Apache 2.0 license.
|
||||
|
||||
## Known limitations
|
||||
|
||||
Size of new binary image has to be known before flashing.
|
||||
@@ -1,313 +0,0 @@
|
||||
/* Copyright 2020-2023 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Used for backwards compatibility with the previous API */
|
||||
#define esp_loader_change_baudrate esp_loader_change_transmission_rate
|
||||
|
||||
/**
|
||||
* Macro which can be used to check the error code,
|
||||
* and return in case the code is not ESP_LOADER_SUCCESS.
|
||||
*/
|
||||
#define RETURN_ON_ERROR(x) do { \
|
||||
esp_loader_error_t _err_ = (x); \
|
||||
if (_err_ != ESP_LOADER_SUCCESS) { \
|
||||
return _err_; \
|
||||
} \
|
||||
} while(0)
|
||||
|
||||
/**
|
||||
* @brief Error codes
|
||||
*/
|
||||
typedef enum {
|
||||
ESP_LOADER_SUCCESS, /*!< Success */
|
||||
ESP_LOADER_ERROR_FAIL, /*!< Unspecified error */
|
||||
ESP_LOADER_ERROR_TIMEOUT, /*!< Timeout elapsed */
|
||||
ESP_LOADER_ERROR_IMAGE_SIZE, /*!< Image size to flash is larger than flash size */
|
||||
ESP_LOADER_ERROR_INVALID_MD5, /*!< Computed and received MD5 does not match */
|
||||
ESP_LOADER_ERROR_INVALID_PARAM, /*!< Invalid parameter passed to function */
|
||||
ESP_LOADER_ERROR_INVALID_TARGET, /*!< Connected target is invalid */
|
||||
ESP_LOADER_ERROR_UNSUPPORTED_CHIP, /*!< Attached chip is not supported */
|
||||
ESP_LOADER_ERROR_UNSUPPORTED_FUNC, /*!< Function is not supported on attached target */
|
||||
ESP_LOADER_ERROR_INVALID_RESPONSE /*!< Internal error */
|
||||
} esp_loader_error_t;
|
||||
|
||||
/**
|
||||
* @brief Supported targets
|
||||
*/
|
||||
typedef enum {
|
||||
ESP8266_CHIP = 0,
|
||||
ESP32_CHIP = 1,
|
||||
ESP32S2_CHIP = 2,
|
||||
ESP32C3_CHIP = 3,
|
||||
ESP32S3_CHIP = 4,
|
||||
ESP32C2_CHIP = 5,
|
||||
ESP32H4_CHIP = 6,
|
||||
ESP32H2_CHIP = 7,
|
||||
ESP_MAX_CHIP = 8,
|
||||
ESP_UNKNOWN_CHIP = 8
|
||||
} target_chip_t;
|
||||
|
||||
/**
|
||||
* @brief Application binary header
|
||||
*/
|
||||
typedef struct {
|
||||
uint8_t magic;
|
||||
uint8_t segments;
|
||||
uint8_t flash_mode;
|
||||
uint8_t flash_size_freq;
|
||||
uint32_t entrypoint;
|
||||
} esp_loader_bin_header_t;
|
||||
|
||||
/**
|
||||
* @brief Segment binary header
|
||||
*/
|
||||
typedef struct {
|
||||
uint32_t addr;
|
||||
uint32_t size;
|
||||
uint8_t *data;
|
||||
} esp_loader_bin_segment_t;
|
||||
|
||||
/**
|
||||
* @brief SPI pin configuration arguments
|
||||
*/
|
||||
typedef union {
|
||||
struct {
|
||||
uint32_t pin_clk: 6;
|
||||
uint32_t pin_q: 6;
|
||||
uint32_t pin_d: 6;
|
||||
uint32_t pin_cs: 6;
|
||||
uint32_t pin_hd: 6;
|
||||
uint32_t zero: 2;
|
||||
};
|
||||
uint32_t val;
|
||||
} esp_loader_spi_config_t;
|
||||
|
||||
/**
|
||||
* @brief Connection arguments
|
||||
*/
|
||||
typedef struct {
|
||||
uint32_t sync_timeout; /*!< Maximum time to wait for response from serial interface. */
|
||||
int32_t trials; /*!< Number of trials to connect to target. If greater than 1,
|
||||
100 millisecond delay is inserted after each try. */
|
||||
} esp_loader_connect_args_t;
|
||||
|
||||
#define ESP_LOADER_CONNECT_DEFAULT() { \
|
||||
.sync_timeout = 100, \
|
||||
.trials = 10, \
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Connects to the target
|
||||
*
|
||||
* @param connect_args[in] Timing parameters to be used for connecting to target.
|
||||
*
|
||||
* @return
|
||||
* - ESP_LOADER_SUCCESS Success
|
||||
* - ESP_LOADER_ERROR_TIMEOUT Timeout
|
||||
* - ESP_LOADER_ERROR_INVALID_RESPONSE Internal error
|
||||
*/
|
||||
esp_loader_error_t esp_loader_connect(esp_loader_connect_args_t *connect_args);
|
||||
|
||||
/**
|
||||
* @brief Returns attached target chip.
|
||||
*
|
||||
* @warning This function can only be called after connection with target
|
||||
* has been successfully established by calling esp_loader_connect().
|
||||
*
|
||||
* @return One of target_chip_t
|
||||
*/
|
||||
target_chip_t esp_loader_get_target(void);
|
||||
|
||||
|
||||
#ifdef SERIAL_FLASHER_INTERFACE_UART
|
||||
/**
|
||||
* @brief Initiates flash operation
|
||||
*
|
||||
* @param offset[in] Address from which flash operation will be performed.
|
||||
* @param image_size[in] Size of the whole binary to be loaded into flash.
|
||||
* @param block_size[in] Size of buffer used in subsequent calls to esp_loader_flash_write.
|
||||
*
|
||||
* @note image_size is size of the whole image, whereas, block_size is chunk of data sent
|
||||
* to the target, each time esp_loader_flash_write function is called.
|
||||
*
|
||||
* @return
|
||||
* - ESP_LOADER_SUCCESS Success
|
||||
* - ESP_LOADER_ERROR_TIMEOUT Timeout
|
||||
* - ESP_LOADER_ERROR_INVALID_RESPONSE Internal error
|
||||
*/
|
||||
esp_loader_error_t esp_loader_flash_start(uint32_t offset, uint32_t image_size, uint32_t block_size);
|
||||
|
||||
/**
|
||||
* @brief Writes supplied data to target's flash memory.
|
||||
*
|
||||
* @param payload[in] Data to be flashed into target's memory.
|
||||
* @param size[in] Size of payload in bytes.
|
||||
*
|
||||
* @note size must not be greater that block_size supplied to previously called
|
||||
* esp_loader_flash_start function. If size is less than block_size,
|
||||
* remaining bytes of payload buffer will be padded with 0xff.
|
||||
* Therefore, size of payload buffer has to be equal or greater than block_size.
|
||||
*
|
||||
* @return
|
||||
* - ESP_LOADER_SUCCESS Success
|
||||
* - ESP_LOADER_ERROR_TIMEOUT Timeout
|
||||
* - ESP_LOADER_ERROR_INVALID_RESPONSE Internal error
|
||||
*/
|
||||
esp_loader_error_t esp_loader_flash_write(void *payload, uint32_t size);
|
||||
|
||||
/**
|
||||
* @brief Ends flash operation.
|
||||
*
|
||||
* @param reboot[in] reboot the target if true.
|
||||
*
|
||||
* @return
|
||||
* - ESP_LOADER_SUCCESS Success
|
||||
* - ESP_LOADER_ERROR_TIMEOUT Timeout
|
||||
* - ESP_LOADER_ERROR_INVALID_RESPONSE Internal error
|
||||
*/
|
||||
esp_loader_error_t esp_loader_flash_finish(bool reboot);
|
||||
#endif /* SERIAL_FLASHER_INTERFACE_UART */
|
||||
|
||||
|
||||
/**
|
||||
* @brief Initiates mem operation, initiates loading for program into target RAM
|
||||
*
|
||||
* @param offset[in] Address from which mem operation will be performed.
|
||||
* @param size[in] Size of the whole binary to be loaded into mem.
|
||||
* @param block_size[in] Size of buffer used in subsequent calls to esp_loader_mem_write.
|
||||
*
|
||||
* @note image_size is size of the whole image, whereas, block_size is chunk of data sent
|
||||
* to the target, each time esp_mem_flash_write function is called.
|
||||
*
|
||||
* @return
|
||||
* - ESP_LOADER_SUCCESS Success
|
||||
* - ESP_LOADER_ERROR_TIMEOUT Timeout
|
||||
* - ESP_LOADER_ERROR_INVALID_RESPONSE Internal error
|
||||
*/
|
||||
esp_loader_error_t esp_loader_mem_start(uint32_t offset, uint32_t size, uint32_t block_size);
|
||||
|
||||
|
||||
/**
|
||||
* @brief Writes supplied data to target's mem memory.
|
||||
*
|
||||
* @param payload[in] Data to be loaded into target's memory.
|
||||
* @param size[in] Size of data in bytes.
|
||||
*
|
||||
* @note size must not be greater that block_size supplied to previously called
|
||||
* esp_loader_mem_start function.
|
||||
* Therefore, size of data buffer has to be equal or greater than block_size.
|
||||
*
|
||||
* @return
|
||||
* - ESP_LOADER_SUCCESS Success
|
||||
* - ESP_LOADER_ERROR_TIMEOUT Timeout
|
||||
* - ESP_LOADER_ERROR_INVALID_RESPONSE Internal error
|
||||
*/
|
||||
esp_loader_error_t esp_loader_mem_write(const void *payload, uint32_t size);
|
||||
|
||||
|
||||
/**
|
||||
* @brief Ends mem operation, finish loading for program into target RAM
|
||||
* and send the entrypoint of ram_loadable app
|
||||
*
|
||||
* @param entrypoint[in] entrypoint of ram program.
|
||||
*
|
||||
* @return
|
||||
* - ESP_LOADER_SUCCESS Success
|
||||
* - ESP_LOADER_ERROR_TIMEOUT Timeout
|
||||
* - ESP_LOADER_ERROR_INVALID_RESPONSE Internal error
|
||||
*/
|
||||
esp_loader_error_t esp_loader_mem_finish(uint32_t entrypoint);
|
||||
|
||||
|
||||
/**
|
||||
* @brief Writes register.
|
||||
*
|
||||
* @param address[in] Address of register.
|
||||
* @param reg_value[in] New register value.
|
||||
*
|
||||
* @return
|
||||
* - ESP_LOADER_SUCCESS Success
|
||||
* - ESP_LOADER_ERROR_TIMEOUT Timeout
|
||||
* - ESP_LOADER_ERROR_INVALID_RESPONSE Internal error
|
||||
*/
|
||||
esp_loader_error_t esp_loader_write_register(uint32_t address, uint32_t reg_value);
|
||||
|
||||
/**
|
||||
* @brief Reads register.
|
||||
*
|
||||
* @param address[in] Address of register.
|
||||
* @param reg_value[out] Register value.
|
||||
*
|
||||
* @return
|
||||
* - ESP_LOADER_SUCCESS Success
|
||||
* - ESP_LOADER_ERROR_TIMEOUT Timeout
|
||||
* - ESP_LOADER_ERROR_INVALID_RESPONSE Internal error
|
||||
*/
|
||||
esp_loader_error_t esp_loader_read_register(uint32_t address, uint32_t *reg_value);
|
||||
|
||||
/**
|
||||
* @brief Change baud rate.
|
||||
*
|
||||
* @note Baud rate has to be also adjusted accordingly on host MCU, as
|
||||
* target's baud rate is changed upon return from this function.
|
||||
*
|
||||
* @param transmission_rate[in] new baud rate to be set.
|
||||
*
|
||||
* @return
|
||||
* - ESP_LOADER_SUCCESS Success
|
||||
* - ESP_LOADER_ERROR_TIMEOUT Timeout
|
||||
* - ESP_LOADER_ERROR_INVALID_RESPONSE Internal error
|
||||
* - ESP_LOADER_ERROR_UNSUPPORTED_FUNC Unsupported on the target
|
||||
*/
|
||||
esp_loader_error_t esp_loader_change_transmission_rate(uint32_t transmission_rate);
|
||||
|
||||
/**
|
||||
* @brief Verify target's flash integrity by checking MD5.
|
||||
* MD5 checksum is computed from data pushed to target's memory by calling
|
||||
* esp_loader_flash_write() function and compared against target's MD5.
|
||||
* Target computes checksum based on offset and image_size passed to
|
||||
* esp_loader_flash_start() function.
|
||||
*
|
||||
* @note This function is only available if MD5_ENABLED is set.
|
||||
*
|
||||
* @return
|
||||
* - ESP_LOADER_SUCCESS Success
|
||||
* - ESP_LOADER_ERROR_INVALID_MD5 MD5 does not match
|
||||
* - ESP_LOADER_ERROR_TIMEOUT Timeout
|
||||
* - ESP_LOADER_ERROR_INVALID_RESPONSE Internal error
|
||||
* - ESP_LOADER_ERROR_UNSUPPORTED_FUNC Unsupported on the target
|
||||
*/
|
||||
#if MD5_ENABLED
|
||||
esp_loader_error_t esp_loader_flash_verify(void);
|
||||
#endif
|
||||
/**
|
||||
* @brief Toggles reset pin.
|
||||
*/
|
||||
void esp_loader_reset_target(void);
|
||||
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -1,112 +0,0 @@
|
||||
/* Copyright 2020-2023 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include "esp_loader.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Changes the transmission rate of the used peripheral.
|
||||
*/
|
||||
esp_loader_error_t loader_port_change_transmission_rate(uint32_t transmission_rate);
|
||||
|
||||
/**
|
||||
* @brief Writes data over the io interface.
|
||||
*
|
||||
* @param data[in] Buffer with data to be written.
|
||||
* @param size[in] Size of data in bytes.
|
||||
* @param timeout[in] Timeout in milliseconds.
|
||||
*
|
||||
* @return
|
||||
* - ESP_LOADER_SUCCESS Success
|
||||
* - ESP_LOADER_ERROR_TIMEOUT Timeout elapsed
|
||||
*/
|
||||
esp_loader_error_t loader_port_write(const uint8_t *data, uint16_t size, uint32_t timeout);
|
||||
|
||||
/**
|
||||
* @brief Reads data from the io interface.
|
||||
*
|
||||
* @param data[out] Buffer into which received data will be written.
|
||||
* @param size[in] Number of bytes to read.
|
||||
* @param timeout[in] Timeout in milliseconds.
|
||||
*
|
||||
* @return
|
||||
* - ESP_LOADER_SUCCESS Success
|
||||
* - ESP_LOADER_ERROR_TIMEOUT Timeout elapsed
|
||||
*/
|
||||
esp_loader_error_t loader_port_read(uint8_t *data, uint16_t size, uint32_t timeout);
|
||||
|
||||
/**
|
||||
* @brief Delay in milliseconds.
|
||||
*
|
||||
* @param ms[in] Number of milliseconds.
|
||||
*
|
||||
*/
|
||||
void loader_port_delay_ms(uint32_t ms);
|
||||
|
||||
/**
|
||||
* @brief Starts timeout timer.
|
||||
*
|
||||
* @param ms[in] Number of milliseconds.
|
||||
*
|
||||
*/
|
||||
void loader_port_start_timer(uint32_t ms);
|
||||
|
||||
/**
|
||||
* @brief Returns remaining time since timer was started by calling esp_loader_start_timer.
|
||||
* 0 if timer has elapsed.
|
||||
*
|
||||
* @return Number of milliseconds.
|
||||
*
|
||||
*/
|
||||
uint32_t loader_port_remaining_time(void);
|
||||
|
||||
/**
|
||||
* @brief Asserts bootstrap pins to enter boot mode and toggles reset pin.
|
||||
*
|
||||
* @note Reset pin should stay asserted for at least 20 milliseconds.
|
||||
*/
|
||||
void loader_port_enter_bootloader(void);
|
||||
|
||||
/**
|
||||
* @brief Toggles reset pin.
|
||||
*
|
||||
* @note Reset pin should stay asserted for at least 20 milliseconds.
|
||||
*/
|
||||
void loader_port_reset_target(void);
|
||||
|
||||
/**
|
||||
* @brief Function can be defined by user to print debug message.
|
||||
*
|
||||
* @note Empty weak function is used, otherwise.
|
||||
*
|
||||
*/
|
||||
void loader_port_debug_print(const char *str);
|
||||
|
||||
#ifdef SERIAL_FLASHER_INTERFACE_SPI
|
||||
/**
|
||||
* @brief Sets the chip select to a defined level
|
||||
*/
|
||||
void loader_port_spi_set_cs(uint32_t level);
|
||||
#endif /* SERIAL_FLASHER_INTERFACE_SPI */
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -1,24 +0,0 @@
|
||||
/* Copyright 2020-2023 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#warning Please replace serial_io.h with esp_loader_io.h and change the function names \
|
||||
to match the new API
|
||||
|
||||
/* Defines used to avoid breaking existing ports */
|
||||
#define loader_port_change_baudrate loader_port_change_transmission_rate
|
||||
#define loader_port_serial_write loader_port_write
|
||||
#define loader_port_serial_read loader_port_read
|
||||
|
||||
#include "esp_loader_io.h"
|
||||
@@ -1,181 +0,0 @@
|
||||
/* Copyright 2020-2023 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "esp32_port.h"
|
||||
#include "driver/uart.h"
|
||||
#include "driver/gpio.h"
|
||||
#include "esp_timer.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_idf_version.h"
|
||||
#include <unistd.h>
|
||||
|
||||
#ifdef SERIAL_FLASHER_DEBUG_TRACE
|
||||
static void transfer_debug_print(const uint8_t *data, uint16_t size, bool write)
|
||||
{
|
||||
static bool write_prev = false;
|
||||
|
||||
if (write_prev != write) {
|
||||
write_prev = write;
|
||||
printf("\n--- %s ---\n", write ? "WRITE" : "READ");
|
||||
}
|
||||
|
||||
for (uint32_t i = 0; i < size; i++) {
|
||||
printf("%02x ", data[i]);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
static int64_t s_time_end;
|
||||
static int32_t s_uart_port;
|
||||
static int32_t s_reset_trigger_pin;
|
||||
static int32_t s_gpio0_trigger_pin;
|
||||
|
||||
esp_loader_error_t loader_port_esp32_init(const loader_esp32_config_t *config)
|
||||
{
|
||||
s_uart_port = config->uart_port;
|
||||
s_reset_trigger_pin = config->reset_trigger_pin;
|
||||
s_gpio0_trigger_pin = config->gpio0_trigger_pin;
|
||||
|
||||
// Initialize UART
|
||||
uart_config_t uart_config = {
|
||||
.baud_rate = config->baud_rate,
|
||||
.data_bits = UART_DATA_8_BITS,
|
||||
.parity = UART_PARITY_DISABLE,
|
||||
.stop_bits = UART_STOP_BITS_1,
|
||||
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
|
||||
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
|
||||
.source_clk = UART_SCLK_DEFAULT,
|
||||
#endif
|
||||
};
|
||||
|
||||
int rx_buffer_size = config->rx_buffer_size ? config->rx_buffer_size : 400;
|
||||
int tx_buffer_size = config->tx_buffer_size ? config->tx_buffer_size : 400;
|
||||
QueueHandle_t *uart_queue = config->uart_queue ? config->uart_queue : NULL;
|
||||
int queue_size = config->queue_size ? config->queue_size : 0;
|
||||
|
||||
if ( uart_param_config(s_uart_port, &uart_config) != ESP_OK ) {
|
||||
return ESP_LOADER_ERROR_FAIL;
|
||||
}
|
||||
if ( uart_set_pin(s_uart_port, config->uart_tx_pin, config->uart_rx_pin, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE) != ESP_OK ) {
|
||||
return ESP_LOADER_ERROR_FAIL;
|
||||
}
|
||||
if ( uart_driver_install(s_uart_port, rx_buffer_size, tx_buffer_size, queue_size, uart_queue, 0) != ESP_OK ) {
|
||||
return ESP_LOADER_ERROR_FAIL;
|
||||
}
|
||||
|
||||
// Initialize boot pin selection pins
|
||||
gpio_reset_pin(s_reset_trigger_pin);
|
||||
gpio_set_pull_mode(s_reset_trigger_pin, GPIO_PULLUP_ONLY);
|
||||
gpio_set_direction(s_reset_trigger_pin, GPIO_MODE_OUTPUT);
|
||||
|
||||
gpio_reset_pin(s_gpio0_trigger_pin);
|
||||
gpio_set_pull_mode(s_gpio0_trigger_pin, GPIO_PULLUP_ONLY);
|
||||
gpio_set_direction(s_gpio0_trigger_pin, GPIO_MODE_OUTPUT);
|
||||
|
||||
return ESP_LOADER_SUCCESS;
|
||||
}
|
||||
|
||||
void loader_port_esp32_deinit(void)
|
||||
{
|
||||
uart_driver_delete(s_uart_port);
|
||||
}
|
||||
|
||||
|
||||
esp_loader_error_t loader_port_write(const uint8_t *data, uint16_t size, uint32_t timeout)
|
||||
{
|
||||
uart_write_bytes(s_uart_port, (const char *)data, size);
|
||||
esp_err_t err = uart_wait_tx_done(s_uart_port, pdMS_TO_TICKS(timeout));
|
||||
|
||||
if (err == ESP_OK) {
|
||||
#ifdef SERIAL_FLASHER_DEBUG_TRACE
|
||||
transfer_debug_print(data, size, true);
|
||||
#endif
|
||||
return ESP_LOADER_SUCCESS;
|
||||
} else if (err == ESP_ERR_TIMEOUT) {
|
||||
return ESP_LOADER_ERROR_TIMEOUT;
|
||||
} else {
|
||||
return ESP_LOADER_ERROR_FAIL;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
esp_loader_error_t loader_port_read(uint8_t *data, uint16_t size, uint32_t timeout)
|
||||
{
|
||||
int read = uart_read_bytes(s_uart_port, data, size, pdMS_TO_TICKS(timeout));
|
||||
|
||||
if (read < 0) {
|
||||
return ESP_LOADER_ERROR_FAIL;
|
||||
} else if (read < size) {
|
||||
#ifdef SERIAL_FLASHER_DEBUG_TRACE
|
||||
transfer_debug_print(data, read, false);
|
||||
#endif
|
||||
return ESP_LOADER_ERROR_TIMEOUT;
|
||||
} else {
|
||||
#ifdef SERIAL_FLASHER_DEBUG_TRACE
|
||||
transfer_debug_print(data, read, false);
|
||||
#endif
|
||||
return ESP_LOADER_SUCCESS;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Set GPIO0 LOW, then
|
||||
// assert reset pin for 50 milliseconds.
|
||||
void loader_port_enter_bootloader(void)
|
||||
{
|
||||
gpio_set_level(s_gpio0_trigger_pin, 0);
|
||||
loader_port_reset_target();
|
||||
loader_port_delay_ms(SERIAL_FLASHER_BOOT_HOLD_TIME_MS);
|
||||
gpio_set_level(s_gpio0_trigger_pin, 1);
|
||||
}
|
||||
|
||||
|
||||
void loader_port_reset_target(void)
|
||||
{
|
||||
gpio_set_level(s_reset_trigger_pin, 0);
|
||||
loader_port_delay_ms(SERIAL_FLASHER_RESET_HOLD_TIME_MS);
|
||||
gpio_set_level(s_reset_trigger_pin, 1);
|
||||
}
|
||||
|
||||
|
||||
void loader_port_delay_ms(uint32_t ms)
|
||||
{
|
||||
usleep(ms * 1000);
|
||||
}
|
||||
|
||||
|
||||
void loader_port_start_timer(uint32_t ms)
|
||||
{
|
||||
s_time_end = esp_timer_get_time() + ms * 1000;
|
||||
}
|
||||
|
||||
|
||||
uint32_t loader_port_remaining_time(void)
|
||||
{
|
||||
int64_t remaining = (s_time_end - esp_timer_get_time()) / 1000;
|
||||
return (remaining > 0) ? (uint32_t)remaining : 0;
|
||||
}
|
||||
|
||||
|
||||
void loader_port_debug_print(const char *str)
|
||||
{
|
||||
printf("DEBUG: %s\n", str);
|
||||
}
|
||||
|
||||
esp_loader_error_t loader_port_change_transmission_rate(uint32_t baudrate)
|
||||
{
|
||||
esp_err_t err = uart_set_baudrate(s_uart_port, baudrate);
|
||||
return (err == ESP_OK) ? ESP_LOADER_SUCCESS : ESP_LOADER_ERROR_FAIL;
|
||||
}
|
||||
@@ -1,59 +0,0 @@
|
||||
/* Copyright 2020-2023 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "esp_loader_io.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/queue.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint32_t baud_rate; /*!< Initial baud rate, can be changed later */
|
||||
uint32_t uart_port; /*!< UART port */
|
||||
uint32_t uart_rx_pin; /*!< This pin will be configured as UART Rx pin */
|
||||
uint32_t uart_tx_pin; /*!< This pin will be configured as UART Tx pin */
|
||||
uint32_t reset_trigger_pin; /*!< This pin will be used to reset target chip */
|
||||
uint32_t gpio0_trigger_pin; /*!< This pin will be used to toggle set IO0 of target chip */
|
||||
uint32_t rx_buffer_size; /*!< Set to zero for default RX buffer size */
|
||||
uint32_t tx_buffer_size; /*!< Set to zero for default TX buffer size */
|
||||
uint32_t queue_size; /*!< Set to zero for default UART queue size */
|
||||
QueueHandle_t *uart_queue; /*!< Set to NULL, if UART queue handle is not
|
||||
necessary. Otherwise, it will be assigned here */
|
||||
} loader_esp32_config_t;
|
||||
|
||||
/**
|
||||
* @brief Initializes serial interface.
|
||||
*
|
||||
* @param baud_rate[in] Communication speed.
|
||||
*
|
||||
* @return
|
||||
* - ESP_LOADER_SUCCESS Success
|
||||
* - ESP_LOADER_ERROR_FAIL Initialization failure
|
||||
*/
|
||||
esp_loader_error_t loader_port_esp32_init(const loader_esp32_config_t *config);
|
||||
|
||||
/**
|
||||
* @brief Deinitialize serial interface.
|
||||
*/
|
||||
void loader_port_esp32_deinit(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -1,298 +0,0 @@
|
||||
/* Copyright 2020-2023 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "esp32_spi_port.h"
|
||||
#include "esp_log.h"
|
||||
#include "driver/gpio.h"
|
||||
#include "esp_timer.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_idf_version.h"
|
||||
#include <unistd.h>
|
||||
|
||||
// #define SERIAL_DEBUG_ENABLE
|
||||
|
||||
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 3, 0)
|
||||
#define DMA_CHAN SPI_DMA_CH_AUTO
|
||||
#else
|
||||
#define DMA_CHAN 1
|
||||
#endif
|
||||
|
||||
#define WORD_ALIGNED(ptr) ((size_t)ptr % sizeof(size_t) == 0)
|
||||
|
||||
#ifdef SERIAL_DEBUG_ENABLE
|
||||
|
||||
static void dec_to_hex_str(const uint8_t dec, uint8_t hex_str[3])
|
||||
{
|
||||
static const uint8_t dec_to_hex[] = {
|
||||
'0', '1', '2', '3', '4', '5', '6', '7',
|
||||
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
|
||||
};
|
||||
|
||||
hex_str[0] = dec_to_hex[dec >> 4];
|
||||
hex_str[1] = dec_to_hex[dec & 0xF];
|
||||
hex_str[2] = '\0';
|
||||
}
|
||||
|
||||
static void serial_debug_print(const uint8_t *data, uint16_t size, bool write)
|
||||
{
|
||||
static bool write_prev = false;
|
||||
uint8_t hex_str[3];
|
||||
|
||||
if(write_prev != write) {
|
||||
write_prev = write;
|
||||
printf("\n--- %s ---\n", write ? "WRITE" : "READ");
|
||||
}
|
||||
|
||||
for(uint32_t i = 0; i < size; i++) {
|
||||
dec_to_hex_str(data[i], hex_str);
|
||||
printf("%s ", hex_str);
|
||||
}
|
||||
}
|
||||
|
||||
#else
|
||||
static void serial_debug_print(const uint8_t *data, uint16_t size, bool write) { }
|
||||
#endif
|
||||
|
||||
static spi_host_device_t s_spi_bus;
|
||||
static spi_bus_config_t s_spi_config;
|
||||
static spi_device_handle_t s_device_h;
|
||||
static spi_device_interface_config_t s_device_config;
|
||||
static int64_t s_time_end;
|
||||
static uint32_t s_reset_trigger_pin;
|
||||
static uint32_t s_strap_bit0_pin;
|
||||
static uint32_t s_strap_bit1_pin;
|
||||
static uint32_t s_strap_bit2_pin;
|
||||
static uint32_t s_strap_bit3_pin;
|
||||
static uint32_t s_spi_cs_pin;
|
||||
|
||||
esp_loader_error_t loader_port_esp32_spi_init(const loader_esp32_spi_config_t *config)
|
||||
{
|
||||
/* Initialize the global static variables*/
|
||||
s_spi_bus = config->spi_bus;
|
||||
s_reset_trigger_pin = config->reset_trigger_pin;
|
||||
s_strap_bit0_pin = config->strap_bit0_pin;
|
||||
s_strap_bit1_pin = config->strap_bit1_pin;
|
||||
s_strap_bit2_pin = config->strap_bit2_pin;
|
||||
s_strap_bit3_pin = config->strap_bit3_pin;
|
||||
s_spi_cs_pin = config->spi_cs_pin;
|
||||
|
||||
/* Configure and initialize the SPI bus*/
|
||||
s_spi_config.mosi_io_num = config->spi_mosi_pin;
|
||||
s_spi_config.miso_io_num = config->spi_miso_pin;
|
||||
s_spi_config.sclk_io_num = config->spi_clk_pin;
|
||||
s_spi_config.quadwp_io_num = config->spi_quadwp_pin;
|
||||
s_spi_config.quadhd_io_num = config->spi_quadhd_pin;
|
||||
s_spi_config.max_transfer_sz = 4096 * 4;
|
||||
|
||||
if (spi_bus_initialize(s_spi_bus, &s_spi_config, DMA_CHAN) != ESP_OK) {
|
||||
return ESP_LOADER_ERROR_FAIL;
|
||||
}
|
||||
|
||||
/* Configure and add the device */
|
||||
s_device_config.clock_speed_hz = config->frequency;
|
||||
s_device_config.spics_io_num = -1; /* We're using the chip select pin as GPIO as we need to
|
||||
chain multiple transactions with CS pulled down */
|
||||
s_device_config.flags = SPI_DEVICE_HALFDUPLEX;
|
||||
s_device_config.queue_size = 16;
|
||||
|
||||
if (spi_bus_add_device(s_spi_bus, &s_device_config, &s_device_h) != ESP_OK) {
|
||||
return ESP_LOADER_ERROR_FAIL;
|
||||
}
|
||||
|
||||
/* Initialize the pins except for the strapping ones */
|
||||
gpio_reset_pin(s_reset_trigger_pin);
|
||||
gpio_set_pull_mode(s_reset_trigger_pin, GPIO_PULLUP_ONLY);
|
||||
gpio_set_direction(s_reset_trigger_pin, GPIO_MODE_OUTPUT);
|
||||
gpio_set_level(s_reset_trigger_pin, 1);
|
||||
|
||||
gpio_reset_pin(s_spi_cs_pin);
|
||||
gpio_set_pull_mode(s_spi_cs_pin, GPIO_PULLUP_ONLY);
|
||||
gpio_set_direction(s_spi_cs_pin, GPIO_MODE_OUTPUT);
|
||||
gpio_set_level(s_spi_cs_pin, 1);
|
||||
|
||||
return ESP_LOADER_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
void loader_port_esp32_spi_deinit(void)
|
||||
{
|
||||
gpio_reset_pin(s_reset_trigger_pin);
|
||||
gpio_reset_pin(s_spi_cs_pin);
|
||||
spi_bus_remove_device(s_device_h);
|
||||
spi_bus_free(s_spi_bus);
|
||||
}
|
||||
|
||||
|
||||
void loader_port_spi_set_cs(const uint32_t level) {
|
||||
gpio_set_level(s_spi_cs_pin, level);
|
||||
}
|
||||
|
||||
|
||||
esp_loader_error_t loader_port_write(const uint8_t *data, const uint16_t size, const uint32_t timeout)
|
||||
{
|
||||
/* Due to the fact that the SPI driver uses DMA for larger transfers,
|
||||
and the DMA requirements, the buffer must be word aligned */
|
||||
if (data == NULL || !WORD_ALIGNED(data)) {
|
||||
return ESP_LOADER_ERROR_INVALID_PARAM;
|
||||
}
|
||||
|
||||
serial_debug_print(data, size, true);
|
||||
|
||||
spi_transaction_t transaction = {
|
||||
.tx_buffer = data,
|
||||
.rx_buffer = NULL,
|
||||
.length = size * 8U,
|
||||
.rxlength = 0,
|
||||
};
|
||||
|
||||
esp_err_t err = spi_device_transmit(s_device_h, &transaction);
|
||||
|
||||
if (err == ESP_OK) {
|
||||
serial_debug_print(data, size, false);
|
||||
return ESP_LOADER_SUCCESS;
|
||||
} else if (err == ESP_ERR_TIMEOUT) {
|
||||
return ESP_LOADER_ERROR_TIMEOUT;
|
||||
} else {
|
||||
return ESP_LOADER_ERROR_FAIL;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
esp_loader_error_t loader_port_read(uint8_t *data, const uint16_t size, const uint32_t timeout)
|
||||
{
|
||||
/* Due to the fact that the SPI driver uses DMA for larger transfers,
|
||||
and the DMA requirements, the buffer must be word aligned */
|
||||
if (data == NULL || !WORD_ALIGNED(data)) {
|
||||
return ESP_LOADER_ERROR_INVALID_PARAM;
|
||||
}
|
||||
|
||||
serial_debug_print(data, size, true);
|
||||
|
||||
spi_transaction_t transaction = {
|
||||
.tx_buffer = NULL,
|
||||
.rx_buffer = data,
|
||||
.rxlength = size * 8,
|
||||
};
|
||||
|
||||
esp_err_t err = spi_device_transmit(s_device_h, &transaction);
|
||||
|
||||
if (err == ESP_OK) {
|
||||
serial_debug_print(data, size, false);
|
||||
return ESP_LOADER_SUCCESS;
|
||||
} else if (err == ESP_ERR_TIMEOUT) {
|
||||
return ESP_LOADER_ERROR_TIMEOUT;
|
||||
} else {
|
||||
return ESP_LOADER_ERROR_FAIL;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void loader_port_enter_bootloader(void)
|
||||
{
|
||||
/*
|
||||
We have to initialize the GPIO pins for the target strapping pins here,
|
||||
as they may overlap with target SPI pins.
|
||||
For instance in the case of ESP32C3 MISO and strapping bit 0 pins overlap.
|
||||
*/
|
||||
spi_bus_remove_device(s_device_h);
|
||||
spi_bus_free(s_spi_bus);
|
||||
|
||||
gpio_reset_pin(s_strap_bit0_pin);
|
||||
gpio_set_pull_mode(s_strap_bit0_pin, GPIO_PULLUP_ONLY);
|
||||
gpio_set_direction(s_strap_bit0_pin, GPIO_MODE_OUTPUT);
|
||||
|
||||
gpio_reset_pin(s_strap_bit1_pin);
|
||||
gpio_set_pull_mode(s_strap_bit1_pin, GPIO_PULLUP_ONLY);
|
||||
gpio_set_direction(s_strap_bit1_pin, GPIO_MODE_OUTPUT);
|
||||
|
||||
gpio_reset_pin(s_strap_bit2_pin);
|
||||
gpio_set_pull_mode(s_strap_bit2_pin, GPIO_PULLUP_ONLY);
|
||||
gpio_set_direction(s_strap_bit2_pin, GPIO_MODE_OUTPUT);
|
||||
|
||||
gpio_reset_pin(s_strap_bit3_pin);
|
||||
gpio_set_pull_mode(s_strap_bit3_pin, GPIO_PULLUP_ONLY);
|
||||
gpio_set_direction(s_strap_bit3_pin, GPIO_MODE_OUTPUT);
|
||||
|
||||
/* Set the strapping pins and perform the reset sequence */
|
||||
gpio_set_level(s_strap_bit0_pin, 1);
|
||||
gpio_set_level(s_strap_bit1_pin, 0);
|
||||
gpio_set_level(s_strap_bit2_pin, 0);
|
||||
gpio_set_level(s_strap_bit3_pin, 0);
|
||||
loader_port_reset_target();
|
||||
loader_port_delay_ms(SERIAL_FLASHER_BOOT_HOLD_TIME_MS);
|
||||
gpio_set_level(s_strap_bit3_pin, 1);
|
||||
gpio_set_level(s_strap_bit0_pin, 0);
|
||||
|
||||
/* Disable the strapping pins so they can be used by the slave later */
|
||||
gpio_reset_pin(s_strap_bit0_pin);
|
||||
gpio_reset_pin(s_strap_bit1_pin);
|
||||
gpio_reset_pin(s_strap_bit2_pin);
|
||||
gpio_reset_pin(s_strap_bit3_pin);
|
||||
|
||||
/* Restore the SPI bus pins */
|
||||
spi_bus_initialize(s_spi_bus, &s_spi_config, DMA_CHAN);
|
||||
spi_bus_add_device(s_spi_bus, &s_device_config, &s_device_h);
|
||||
}
|
||||
|
||||
|
||||
void loader_port_reset_target(void)
|
||||
{
|
||||
gpio_set_level(s_reset_trigger_pin, 0);
|
||||
loader_port_delay_ms(SERIAL_FLASHER_RESET_HOLD_TIME_MS);
|
||||
gpio_set_level(s_reset_trigger_pin, 1);
|
||||
}
|
||||
|
||||
|
||||
void loader_port_delay_ms(const uint32_t ms)
|
||||
{
|
||||
usleep(ms * 1000);
|
||||
}
|
||||
|
||||
|
||||
void loader_port_start_timer(const uint32_t ms)
|
||||
{
|
||||
s_time_end = esp_timer_get_time() + ms * 1000;
|
||||
}
|
||||
|
||||
|
||||
uint32_t loader_port_remaining_time(void)
|
||||
{
|
||||
int64_t remaining = (s_time_end - esp_timer_get_time()) / 1000;
|
||||
return (remaining > 0) ? (uint32_t)remaining : 0;
|
||||
}
|
||||
|
||||
|
||||
void loader_port_debug_print(const char *str)
|
||||
{
|
||||
printf("DEBUG: %s\n", str);
|
||||
}
|
||||
|
||||
|
||||
esp_loader_error_t loader_port_change_transmission_rate(const uint32_t frequency)
|
||||
{
|
||||
if (spi_bus_remove_device(s_device_h) != ESP_OK) {
|
||||
return ESP_LOADER_ERROR_FAIL;
|
||||
}
|
||||
|
||||
uint32_t old_frequency = s_device_config.clock_speed_hz;
|
||||
s_device_config.clock_speed_hz = frequency;
|
||||
|
||||
if (spi_bus_add_device(s_spi_bus, &s_device_config, &s_device_h) != ESP_OK) {
|
||||
s_device_config.clock_speed_hz = old_frequency;
|
||||
return ESP_LOADER_ERROR_FAIL;
|
||||
}
|
||||
|
||||
return ESP_LOADER_SUCCESS;
|
||||
}
|
||||
@@ -1,60 +0,0 @@
|
||||
/* Copyright 2020-2023 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "esp_loader_io.h"
|
||||
#include "driver/spi_master.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct
|
||||
{
|
||||
spi_host_device_t spi_bus;
|
||||
uint32_t frequency;
|
||||
uint32_t spi_clk_pin;
|
||||
uint32_t spi_miso_pin;
|
||||
uint32_t spi_mosi_pin;
|
||||
uint32_t spi_cs_pin;
|
||||
uint32_t spi_quadwp_pin;
|
||||
uint32_t spi_quadhd_pin;
|
||||
uint32_t reset_trigger_pin;
|
||||
uint32_t strap_bit0_pin;
|
||||
uint32_t strap_bit1_pin;
|
||||
uint32_t strap_bit2_pin;
|
||||
uint32_t strap_bit3_pin;
|
||||
} loader_esp32_spi_config_t;
|
||||
|
||||
/**
|
||||
* @brief Initializes the SPI interface.
|
||||
*
|
||||
* @param config[in] Configuration structure
|
||||
*
|
||||
* @return
|
||||
* - ESP_LOADER_SUCCESS Success
|
||||
* - ESP_LOADER_ERROR_FAIL Initialization failure
|
||||
*/
|
||||
esp_loader_error_t loader_port_esp32_spi_init(const loader_esp32_spi_config_t *config);
|
||||
|
||||
/**
|
||||
* @brief Deinitializes the SPI interface.
|
||||
*/
|
||||
void loader_port_esp32_spi_deinit(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -1,302 +0,0 @@
|
||||
/* Copyright 2020-2023 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "esp_loader_io.h"
|
||||
#include "protocol.h"
|
||||
#include <pigpio.h>
|
||||
#include "raspberry_port.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
#include <termios.h>
|
||||
#include <time.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <stdarg.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/param.h>
|
||||
|
||||
#ifdef SERIAL_FLASHER_DEBUG_TRACE
|
||||
static void transfer_debug_print(const uint8_t *data, uint16_t size, bool write)
|
||||
{
|
||||
static bool write_prev = false;
|
||||
|
||||
if (write_prev != write) {
|
||||
write_prev = write;
|
||||
printf("\n--- %s ---\n", write ? "WRITE" : "READ");
|
||||
}
|
||||
|
||||
for (uint32_t i = 0; i < size; i++) {
|
||||
printf("%02x ", data[i]);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
static int serial;
|
||||
static int64_t s_time_end;
|
||||
static int32_t s_reset_trigger_pin;
|
||||
static int32_t s_gpio0_trigger_pin;
|
||||
|
||||
|
||||
static speed_t convert_baudrate(int baud)
|
||||
{
|
||||
switch (baud) {
|
||||
case 50: return B50;
|
||||
case 75: return B75;
|
||||
case 110: return B110;
|
||||
case 134: return B134;
|
||||
case 150: return B150;
|
||||
case 200: return B200;
|
||||
case 300: return B300;
|
||||
case 600: return B600;
|
||||
case 1200: return B1200;
|
||||
case 1800: return B1800;
|
||||
case 2400: return B2400;
|
||||
case 4800: return B4800;
|
||||
case 9600: return B9600;
|
||||
case 19200: return B19200;
|
||||
case 38400: return B38400;
|
||||
case 57600: return B57600;
|
||||
case 115200: return B115200;
|
||||
case 230400: return B230400;
|
||||
case 460800: return B460800;
|
||||
case 500000: return B500000;
|
||||
case 576000: return B576000;
|
||||
case 921600: return B921600;
|
||||
case 1000000: return B1000000;
|
||||
case 1152000: return B1152000;
|
||||
case 1500000: return B1500000;
|
||||
case 2000000: return B2000000;
|
||||
case 2500000: return B2500000;
|
||||
case 3000000: return B3000000;
|
||||
case 3500000: return B3500000;
|
||||
case 4000000: return B4000000;
|
||||
default: return -1;
|
||||
}
|
||||
}
|
||||
|
||||
static int serialOpen (const char *device, uint32_t baudrate)
|
||||
{
|
||||
struct termios options;
|
||||
int status, fd;
|
||||
|
||||
if ((fd = open (device, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK)) == -1) {
|
||||
printf("Error occured while opening serial port !\n");
|
||||
return -1 ;
|
||||
}
|
||||
|
||||
fcntl (fd, F_SETFL, O_RDWR) ;
|
||||
|
||||
// Get and modify current options:
|
||||
|
||||
tcgetattr (fd, &options);
|
||||
speed_t baud = convert_baudrate(baudrate);
|
||||
|
||||
if(baud < 0) {
|
||||
printf("Invalid baudrate!\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
cfmakeraw (&options) ;
|
||||
cfsetispeed (&options, baud) ;
|
||||
cfsetospeed (&options, baud) ;
|
||||
|
||||
options.c_cflag |= (CLOCAL | CREAD) ;
|
||||
options.c_cflag &= ~(PARENB | CSTOPB | CSIZE) ;
|
||||
options.c_cflag |= CS8 ;
|
||||
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG) ;
|
||||
options.c_oflag &= ~OPOST ;
|
||||
options.c_iflag &= ~(IXON | IXOFF | IXANY); // Turn off s/w flow ctrl
|
||||
options.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL); // Disable any special handling of received bytes
|
||||
|
||||
options.c_cc [VMIN] = 0 ;
|
||||
options.c_cc [VTIME] = 10 ; // 1 Second
|
||||
|
||||
tcsetattr (fd, TCSANOW, &options) ;
|
||||
|
||||
ioctl (fd, TIOCMGET, &status);
|
||||
|
||||
status |= TIOCM_DTR ;
|
||||
status |= TIOCM_RTS ;
|
||||
|
||||
ioctl (fd, TIOCMSET, &status);
|
||||
|
||||
usleep (10000) ; // 10mS
|
||||
|
||||
return fd ;
|
||||
}
|
||||
|
||||
static esp_loader_error_t change_baudrate(int file_desc, int baudrate)
|
||||
{
|
||||
struct termios options;
|
||||
speed_t baud = convert_baudrate(baudrate);
|
||||
|
||||
if(baud < 0) {
|
||||
return ESP_LOADER_ERROR_INVALID_PARAM;
|
||||
}
|
||||
|
||||
tcgetattr (file_desc, &options);
|
||||
|
||||
cfmakeraw (&options) ;
|
||||
cfsetispeed (&options, baud);
|
||||
cfsetospeed (&options, baud);
|
||||
|
||||
tcsetattr (file_desc, TCSANOW, &options);
|
||||
|
||||
return ESP_LOADER_SUCCESS;
|
||||
}
|
||||
|
||||
static void set_timeout(uint32_t timeout)
|
||||
{
|
||||
struct termios options;
|
||||
|
||||
timeout /= 100;
|
||||
timeout = MAX(timeout, 1);
|
||||
|
||||
tcgetattr(serial, &options);
|
||||
options.c_cc[VTIME] = timeout;
|
||||
tcsetattr(serial, TCSANOW, &options);
|
||||
}
|
||||
|
||||
static esp_loader_error_t read_char(char *c, uint32_t timeout)
|
||||
{
|
||||
set_timeout(timeout);
|
||||
int read_bytes = read(serial, c, 1);
|
||||
|
||||
if (read_bytes == 1) {
|
||||
return ESP_LOADER_SUCCESS;
|
||||
} else if (read_bytes == 0) {
|
||||
return ESP_LOADER_ERROR_TIMEOUT;
|
||||
} else {
|
||||
return ESP_LOADER_ERROR_FAIL;
|
||||
}
|
||||
}
|
||||
|
||||
static esp_loader_error_t read_data(char *buffer, uint32_t size)
|
||||
{
|
||||
for (int i = 0; i < size; i++) {
|
||||
uint32_t remaining_time = loader_port_remaining_time();
|
||||
RETURN_ON_ERROR( read_char(&buffer[i], remaining_time) );
|
||||
}
|
||||
|
||||
return ESP_LOADER_SUCCESS;
|
||||
}
|
||||
|
||||
esp_loader_error_t loader_port_raspberry_init(const loader_raspberry_config_t *config)
|
||||
{
|
||||
s_reset_trigger_pin = config->reset_trigger_pin;
|
||||
s_gpio0_trigger_pin = config->gpio0_trigger_pin;
|
||||
|
||||
serial = serialOpen(config->device, config->baudrate);
|
||||
if (serial < 0) {
|
||||
printf("Serial port could not be opened!\n");
|
||||
return ESP_LOADER_ERROR_FAIL;
|
||||
}
|
||||
|
||||
if (gpioInitialise() < 0) {
|
||||
printf("pigpio initialisation failed\n");
|
||||
return ESP_LOADER_ERROR_FAIL;
|
||||
}
|
||||
|
||||
gpioSetMode(config->reset_trigger_pin, PI_OUTPUT);
|
||||
gpioSetMode(config->gpio0_trigger_pin, PI_OUTPUT);
|
||||
|
||||
return ESP_LOADER_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
esp_loader_error_t loader_port_write(const uint8_t *data, uint16_t size, uint32_t timeout)
|
||||
{
|
||||
int written = write(serial, data, size);
|
||||
|
||||
if (written < 0) {
|
||||
return ESP_LOADER_ERROR_FAIL;
|
||||
} else if (written < size) {
|
||||
#ifdef SERIAL_FLASHER_DEBUG_TRACE
|
||||
transfer_debug_print(data, written, true);
|
||||
#endif
|
||||
return ESP_LOADER_ERROR_TIMEOUT;
|
||||
} else {
|
||||
#ifdef SERIAL_FLASHER_DEBUG_TRACE
|
||||
transfer_debug_print(data, written, true);
|
||||
#endif
|
||||
return ESP_LOADER_SUCCESS;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
esp_loader_error_t loader_port_read(uint8_t *data, uint16_t size, uint32_t timeout)
|
||||
{
|
||||
RETURN_ON_ERROR( read_data(data, size) );
|
||||
|
||||
#ifdef SERIAL_FLASHER_DEBUG_TRACE
|
||||
transfer_debug_print(data, size, false);
|
||||
#endif
|
||||
|
||||
return ESP_LOADER_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
// Set GPIO0 LOW, then assert reset pin for 50 milliseconds.
|
||||
void loader_port_enter_bootloader(void)
|
||||
{
|
||||
gpioWrite(s_gpio0_trigger_pin, 0);
|
||||
loader_port_reset_target();
|
||||
loader_port_delay_ms(SERIAL_FLASHER_BOOT_HOLD_TIME_MS);
|
||||
gpioWrite(s_gpio0_trigger_pin, 1);
|
||||
}
|
||||
|
||||
|
||||
void loader_port_reset_target(void)
|
||||
{
|
||||
gpioWrite(s_reset_trigger_pin, 0);
|
||||
loader_port_delay_ms(SERIAL_FLASHER_RESET_HOLD_TIME_MS);
|
||||
gpioWrite(s_reset_trigger_pin, 1);
|
||||
}
|
||||
|
||||
|
||||
void loader_port_delay_ms(uint32_t ms)
|
||||
{
|
||||
usleep(ms * 1000);
|
||||
}
|
||||
|
||||
|
||||
void loader_port_start_timer(uint32_t ms)
|
||||
{
|
||||
s_time_end = clock() + (ms * (CLOCKS_PER_SEC / 1000));
|
||||
}
|
||||
|
||||
|
||||
uint32_t loader_port_remaining_time(void)
|
||||
{
|
||||
int64_t remaining = (s_time_end - clock()) / 1000;
|
||||
return (remaining > 0) ? (uint32_t)remaining : 0;
|
||||
}
|
||||
|
||||
|
||||
void loader_port_debug_print(const char *str)
|
||||
{
|
||||
printf("DEBUG: %s\n", str);
|
||||
}
|
||||
|
||||
esp_loader_error_t loader_port_change_transmission_rate(uint32_t baudrate)
|
||||
{
|
||||
return change_baudrate(serial, baudrate);
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
/* Copyright 2020-2023 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include "esp_loader_io.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
const char *device;
|
||||
uint32_t baudrate;
|
||||
uint32_t reset_trigger_pin;
|
||||
uint32_t gpio0_trigger_pin;
|
||||
} loader_raspberry_config_t;
|
||||
|
||||
esp_loader_error_t loader_port_raspberry_init(const loader_raspberry_config_t *config);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -1,140 +0,0 @@
|
||||
/* Copyright 2020-2023 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <stdint.h>
|
||||
#include <sys/param.h>
|
||||
#include <stdio.h>
|
||||
#include "stm32_port.h"
|
||||
|
||||
static UART_HandleTypeDef *uart;
|
||||
static GPIO_TypeDef* gpio_port_io0, *gpio_port_rst;
|
||||
static uint16_t gpio_num_io0, gpio_num_rst;
|
||||
|
||||
#ifdef SERIAL_FLASHER_DEBUG_TRACE
|
||||
static void transfer_debug_print(const uint8_t *data, uint16_t size, bool write)
|
||||
{
|
||||
static bool write_prev = false;
|
||||
|
||||
if (write_prev != write) {
|
||||
write_prev = write;
|
||||
printf("\n--- %s ---\n", write ? "WRITE" : "READ");
|
||||
}
|
||||
|
||||
for (uint32_t i = 0; i < size; i++) {
|
||||
printf("%02x ", data[i]);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
static uint32_t s_time_end;
|
||||
|
||||
esp_loader_error_t loader_port_write(const uint8_t *data, uint16_t size, uint32_t timeout)
|
||||
{
|
||||
HAL_StatusTypeDef err = HAL_UART_Transmit(uart, (uint8_t *)data, size, timeout);
|
||||
|
||||
if (err == HAL_OK) {
|
||||
#ifdef SERIAL_FLASHER_DEBUG_TRACE
|
||||
transfer_debug_print(data, size, true);
|
||||
#endif
|
||||
return ESP_LOADER_SUCCESS;
|
||||
} else if (err == HAL_TIMEOUT) {
|
||||
return ESP_LOADER_ERROR_TIMEOUT;
|
||||
} else {
|
||||
return ESP_LOADER_ERROR_FAIL;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
esp_loader_error_t loader_port_read(uint8_t *data, uint16_t size, uint32_t timeout)
|
||||
{
|
||||
HAL_StatusTypeDef err = HAL_UART_Receive(uart, data, size, timeout);
|
||||
|
||||
if (err == HAL_OK) {
|
||||
#ifdef SERIAL_FLASHER_DEBUG_TRACE
|
||||
transfer_debug_print(data, size, false);
|
||||
#endif
|
||||
return ESP_LOADER_SUCCESS;
|
||||
} else if (err == HAL_TIMEOUT) {
|
||||
return ESP_LOADER_ERROR_TIMEOUT;
|
||||
} else {
|
||||
return ESP_LOADER_ERROR_FAIL;
|
||||
}
|
||||
}
|
||||
|
||||
void loader_port_stm32_init(loader_stm32_config_t *config)
|
||||
|
||||
{
|
||||
uart = config->huart;
|
||||
gpio_port_io0 = config->port_io0;
|
||||
gpio_port_rst = config->port_rst;
|
||||
gpio_num_io0 = config->pin_num_io0;
|
||||
gpio_num_rst = config->pin_num_rst;
|
||||
}
|
||||
|
||||
// Set GPIO0 LOW, then
|
||||
// assert reset pin for 100 milliseconds.
|
||||
void loader_port_enter_bootloader(void)
|
||||
{
|
||||
HAL_GPIO_WritePin(gpio_port_io0, gpio_num_io0, GPIO_PIN_RESET);
|
||||
loader_port_reset_target();
|
||||
HAL_Delay(SERIAL_FLASHER_BOOT_HOLD_TIME_MS);
|
||||
HAL_GPIO_WritePin(gpio_port_io0, gpio_num_io0, GPIO_PIN_SET);
|
||||
}
|
||||
|
||||
|
||||
void loader_port_reset_target(void)
|
||||
{
|
||||
HAL_GPIO_WritePin(gpio_port_rst, gpio_num_rst, GPIO_PIN_RESET);
|
||||
HAL_Delay(SERIAL_FLASHER_RESET_HOLD_TIME_MS);
|
||||
HAL_GPIO_WritePin(gpio_port_rst, gpio_num_rst, GPIO_PIN_SET);
|
||||
}
|
||||
|
||||
|
||||
void loader_port_delay_ms(uint32_t ms)
|
||||
{
|
||||
HAL_Delay(ms);
|
||||
}
|
||||
|
||||
|
||||
void loader_port_start_timer(uint32_t ms)
|
||||
{
|
||||
s_time_end = HAL_GetTick() + ms;
|
||||
}
|
||||
|
||||
|
||||
uint32_t loader_port_remaining_time(void)
|
||||
{
|
||||
int32_t remaining = s_time_end - HAL_GetTick();
|
||||
return (remaining > 0) ? (uint32_t)remaining : 0;
|
||||
}
|
||||
|
||||
|
||||
void loader_port_debug_print(const char *str)
|
||||
{
|
||||
printf("DEBUG: %s", str);
|
||||
}
|
||||
|
||||
esp_loader_error_t loader_port_change_transmission_rate(uint32_t baudrate)
|
||||
{
|
||||
uart->Init.BaudRate = baudrate;
|
||||
|
||||
if( HAL_UART_Init(uart) != HAL_OK ) {
|
||||
return ESP_LOADER_ERROR_FAIL;
|
||||
}
|
||||
|
||||
return ESP_LOADER_SUCCESS;
|
||||
}
|
||||
@@ -1,38 +0,0 @@
|
||||
/* Copyright 2020-2023 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include "esp_loader_io.h"
|
||||
#include "stm32f4xx_hal.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
UART_HandleTypeDef *huart;
|
||||
GPIO_TypeDef *port_io0;
|
||||
uint16_t pin_num_io0;
|
||||
GPIO_TypeDef *port_rst;
|
||||
uint16_t pin_num_rst;
|
||||
} loader_stm32_config_t;
|
||||
|
||||
void loader_port_stm32_init(loader_stm32_config_t *config);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -1,170 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2022 KT-Elektronik, Klaucke und Partner GmbH
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "zephyr_port.h"
|
||||
#include <zephyr/drivers/uart.h>
|
||||
#include <zephyr/console/tty.h>
|
||||
|
||||
static const struct device *uart_dev;
|
||||
static struct gpio_dt_spec enable_spec;
|
||||
static struct gpio_dt_spec boot_spec;
|
||||
|
||||
static struct tty_serial tty;
|
||||
static char tty_rx_buf[CONFIG_ESP_SERIAL_FLASHER_UART_BUFSIZE];
|
||||
static char tty_tx_buf[CONFIG_ESP_SERIAL_FLASHER_UART_BUFSIZE];
|
||||
|
||||
#ifdef SERIAL_FLASHER_DEBUG_TRACE
|
||||
static void transfer_debug_print(const uint8_t *data, uint16_t size, bool write)
|
||||
{
|
||||
static bool write_prev = false;
|
||||
|
||||
if (write_prev != write) {
|
||||
write_prev = write;
|
||||
printk("\n--- %s ---\n", write ? "WRITE" : "READ");
|
||||
}
|
||||
|
||||
for (uint32_t i = 0; i < size; i++) {
|
||||
printk("%02x ", data[i]);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
esp_loader_error_t configure_tty()
|
||||
{
|
||||
if (tty_init(&tty, uart_dev) < 0 ||
|
||||
tty_set_rx_buf(&tty, tty_rx_buf, sizeof(tty_rx_buf)) < 0 ||
|
||||
tty_set_tx_buf(&tty, tty_tx_buf, sizeof(tty_tx_buf)) < 0) {
|
||||
return ESP_LOADER_ERROR_FAIL;
|
||||
}
|
||||
|
||||
return ESP_LOADER_SUCCESS;
|
||||
}
|
||||
|
||||
esp_loader_error_t loader_port_read(uint8_t *data, const uint16_t size, const uint32_t timeout)
|
||||
{
|
||||
if (!device_is_ready(uart_dev) || data == NULL || size == 0) {
|
||||
return ESP_LOADER_ERROR_FAIL;
|
||||
}
|
||||
|
||||
ssize_t total_read = 0;
|
||||
ssize_t remaining = size;
|
||||
|
||||
tty_set_rx_timeout(&tty, timeout);
|
||||
while (remaining > 0) {
|
||||
const uint16_t chunk_size = remaining < CONFIG_ESP_SERIAL_FLASHER_UART_BUFSIZE ?
|
||||
remaining : CONFIG_ESP_SERIAL_FLASHER_UART_BUFSIZE;
|
||||
ssize_t read = tty_read(&tty, &data[total_read], chunk_size);
|
||||
if (read < 0) {
|
||||
return ESP_LOADER_ERROR_TIMEOUT;
|
||||
}
|
||||
#ifdef SERIAL_FLASHER_DEBUG_TRACE
|
||||
transfer_debug_print(data, read, false);
|
||||
#endif
|
||||
total_read += read;
|
||||
remaining -= read;
|
||||
}
|
||||
|
||||
return ESP_LOADER_SUCCESS;
|
||||
}
|
||||
|
||||
esp_loader_error_t loader_port_write(const uint8_t *data, const uint16_t size, const uint32_t timeout)
|
||||
{
|
||||
if (!device_is_ready(uart_dev) || data == NULL || size == 0) {
|
||||
return ESP_LOADER_ERROR_FAIL;
|
||||
}
|
||||
|
||||
ssize_t total_written = 0;
|
||||
ssize_t remaining = size;
|
||||
|
||||
tty_set_tx_timeout(&tty, timeout);
|
||||
while (remaining > 0) {
|
||||
const uint16_t chunk_size = remaining < CONFIG_ESP_SERIAL_FLASHER_UART_BUFSIZE ?
|
||||
remaining : CONFIG_ESP_SERIAL_FLASHER_UART_BUFSIZE;
|
||||
ssize_t written = tty_write(&tty, &data[total_written], chunk_size);
|
||||
if (written < 0) {
|
||||
return ESP_LOADER_ERROR_TIMEOUT;
|
||||
}
|
||||
#ifdef SERIAL_FLASHER_DEBUG_TRACE
|
||||
transfer_debug_print(data, written, true);
|
||||
#endif
|
||||
total_written += written;
|
||||
remaining -= written;
|
||||
}
|
||||
|
||||
return ESP_LOADER_SUCCESS;
|
||||
}
|
||||
|
||||
esp_loader_error_t loader_port_zephyr_init(const loader_zephyr_config_t *config)
|
||||
{
|
||||
uart_dev = config->uart_dev;
|
||||
enable_spec = config->enable_spec;
|
||||
boot_spec = config->boot_spec;
|
||||
return configure_tty();
|
||||
}
|
||||
|
||||
void loader_port_reset_target(void)
|
||||
{
|
||||
gpio_pin_set_dt(&enable_spec, false);
|
||||
loader_port_delay_ms(CONFIG_SERIAL_FLASHER_RESET_HOLD_TIME_MS);
|
||||
gpio_pin_set_dt(&enable_spec, true);
|
||||
}
|
||||
|
||||
void loader_port_enter_bootloader(void)
|
||||
{
|
||||
gpio_pin_set_dt(&boot_spec, false);
|
||||
loader_port_reset_target();
|
||||
loader_port_delay_ms(CONFIG_SERIAL_FLASHER_BOOT_HOLD_TIME_MS);
|
||||
gpio_pin_set_dt(&boot_spec, true);
|
||||
}
|
||||
|
||||
void loader_port_delay_ms(uint32_t ms)
|
||||
{
|
||||
k_msleep(ms);
|
||||
}
|
||||
|
||||
static uint64_t s_time_end;
|
||||
|
||||
void loader_port_start_timer(uint32_t ms)
|
||||
{
|
||||
s_time_end = sys_clock_timeout_end_calc(Z_TIMEOUT_MS(ms));
|
||||
}
|
||||
|
||||
uint32_t loader_port_remaining_time(void)
|
||||
{
|
||||
int64_t remaining = k_ticks_to_ms_floor64(s_time_end - k_uptime_ticks());
|
||||
return (remaining > 0) ? (uint32_t)remaining : 0;
|
||||
}
|
||||
|
||||
esp_loader_error_t loader_port_change_transmission_rate(uint32_t baudrate)
|
||||
{
|
||||
struct uart_config uart_config;
|
||||
|
||||
if (!device_is_ready(uart_dev)) {
|
||||
return ESP_LOADER_ERROR_FAIL;
|
||||
}
|
||||
|
||||
if (uart_config_get(uart_dev, &uart_config) != 0) {
|
||||
return ESP_LOADER_ERROR_FAIL;
|
||||
}
|
||||
uart_config.baudrate = baudrate;
|
||||
|
||||
if (uart_configure(uart_dev, &uart_config) != 0) {
|
||||
return ESP_LOADER_ERROR_FAIL;
|
||||
}
|
||||
|
||||
/* bitrate-change can require tty re-configuration */
|
||||
return configure_tty();
|
||||
}
|
||||
@@ -1,39 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2022 KT-Elektronik, Klaucke und Partner GmbH
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "esp_loader_io.h"
|
||||
#include <zephyr/kernel.h>
|
||||
#include <zephyr/drivers/gpio.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
const struct device *uart_dev;
|
||||
const struct gpio_dt_spec enable_spec;
|
||||
const struct gpio_dt_spec boot_spec;
|
||||
} loader_zephyr_config_t;
|
||||
|
||||
esp_loader_error_t loader_port_zephyr_init(const loader_zephyr_config_t *config);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -1,33 +0,0 @@
|
||||
/* Copyright 2020 Espressif Systems (Shanghai) PTE LTD
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include "esp_loader.h"
|
||||
|
||||
typedef struct {
|
||||
uint32_t cmd;
|
||||
uint32_t usr;
|
||||
uint32_t usr1;
|
||||
uint32_t usr2;
|
||||
uint32_t w0;
|
||||
uint32_t mosi_dlen;
|
||||
uint32_t miso_dlen;
|
||||
} target_registers_t;
|
||||
|
||||
esp_loader_error_t loader_detect_chip(target_chip_t *target, const target_registers_t **regs);
|
||||
esp_loader_error_t loader_read_spi_config(target_chip_t target_chip, uint32_t *spi_config);
|
||||
bool encryption_in_begin_flash_cmd(target_chip_t target);
|
||||
@@ -1,28 +0,0 @@
|
||||
/*
|
||||
* MD5 hash implementation and interface functions
|
||||
* Copyright (c) 2003-2005, Jouni Malinen <j@w1.fi>
|
||||
*
|
||||
* This software may be distributed under the terms of the BSD license.
|
||||
* See README for more details.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
struct MD5Context {
|
||||
uint32_t buf[4];
|
||||
uint32_t bits[2];
|
||||
uint8_t in[64];
|
||||
};
|
||||
|
||||
void MD5Init(struct MD5Context *context);
|
||||
void MD5Update(struct MD5Context *context, unsigned char const *buf, unsigned len);
|
||||
void MD5Final(unsigned char digest[16], struct MD5Context *context);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -1,230 +0,0 @@
|
||||
/* Copyright 2020-2023 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include "esp_loader.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define STATUS_FAILURE 1
|
||||
#define STATUS_SUCCESS 0
|
||||
|
||||
#define READ_DIRECTION 1
|
||||
#define WRITE_DIRECTION 0
|
||||
|
||||
#define MD5_SIZE 32
|
||||
|
||||
typedef enum __attribute__((packed))
|
||||
{
|
||||
FLASH_BEGIN = 0x02,
|
||||
FLASH_DATA = 0x03,
|
||||
FLASH_END = 0x04,
|
||||
MEM_BEGIN = 0x05,
|
||||
MEM_END = 0x06,
|
||||
MEM_DATA = 0x07,
|
||||
SYNC = 0x08,
|
||||
WRITE_REG = 0x09,
|
||||
READ_REG = 0x0a,
|
||||
|
||||
SPI_SET_PARAMS = 0x0b,
|
||||
SPI_ATTACH = 0x0d,
|
||||
CHANGE_BAUDRATE = 0x0f,
|
||||
FLASH_DEFL_BEGIN = 0x10,
|
||||
FLASH_DEFL_DATA = 0x11,
|
||||
FLASH_DEFL_END = 0x12,
|
||||
SPI_FLASH_MD5 = 0x13,
|
||||
} command_t;
|
||||
|
||||
typedef enum __attribute__((packed))
|
||||
{
|
||||
RESPONSE_OK = 0x00,
|
||||
INVALID_COMMAND = 0x05, // parameters or length field is invalid
|
||||
COMMAND_FAILED = 0x06, // Failed to act on received message
|
||||
INVALID_CRC = 0x07, // Invalid CRC in message
|
||||
FLASH_WRITE_ERR = 0x08, // After writing a block of data to flash, the ROM loader reads the value back and the 8-bit CRC is compared to the data read from flash. If they don't match, this error is returned.
|
||||
FLASH_READ_ERR = 0x09, // SPI read failed
|
||||
READ_LENGTH_ERR = 0x0a, // SPI read request length is too long
|
||||
DEFLATE_ERROR = 0x0b, // ESP32 compressed uploads only
|
||||
} error_code_t;
|
||||
|
||||
typedef struct __attribute__((packed))
|
||||
{
|
||||
uint8_t direction;
|
||||
uint8_t command; // One of command_t
|
||||
uint16_t size;
|
||||
uint32_t checksum;
|
||||
} command_common_t;
|
||||
|
||||
typedef struct __attribute__((packed))
|
||||
{
|
||||
command_common_t common;
|
||||
uint32_t erase_size;
|
||||
uint32_t packet_count;
|
||||
uint32_t packet_size;
|
||||
uint32_t offset;
|
||||
uint32_t encrypted;
|
||||
} flash_begin_command_t;
|
||||
|
||||
typedef struct __attribute__((packed))
|
||||
{
|
||||
command_common_t common;
|
||||
uint32_t data_size;
|
||||
uint32_t sequence_number;
|
||||
uint32_t zero_0;
|
||||
uint32_t zero_1;
|
||||
} data_command_t;
|
||||
|
||||
typedef struct __attribute__((packed))
|
||||
{
|
||||
command_common_t common;
|
||||
uint32_t stay_in_loader;
|
||||
} flash_end_command_t;
|
||||
|
||||
typedef struct __attribute__((packed))
|
||||
{
|
||||
command_common_t common;
|
||||
uint32_t total_size;
|
||||
uint32_t blocks;
|
||||
uint32_t block_size;
|
||||
uint32_t offset;
|
||||
} mem_begin_command_t;
|
||||
|
||||
typedef struct __attribute__((packed))
|
||||
{
|
||||
command_common_t common;
|
||||
uint32_t stay_in_loader;
|
||||
uint32_t entry_point_address;
|
||||
} mem_end_command_t;
|
||||
|
||||
typedef struct __attribute__((packed))
|
||||
{
|
||||
command_common_t common;
|
||||
uint8_t sync_sequence[36];
|
||||
} sync_command_t;
|
||||
|
||||
typedef struct __attribute__((packed))
|
||||
{
|
||||
command_common_t common;
|
||||
uint32_t address;
|
||||
uint32_t value;
|
||||
uint32_t mask;
|
||||
uint32_t delay_us;
|
||||
} write_reg_command_t;
|
||||
|
||||
typedef struct __attribute__((packed))
|
||||
{
|
||||
command_common_t common;
|
||||
uint32_t address;
|
||||
} read_reg_command_t;
|
||||
|
||||
typedef struct __attribute__((packed))
|
||||
{
|
||||
command_common_t common;
|
||||
uint32_t configuration;
|
||||
uint32_t zero; // ESP32 ROM only
|
||||
} spi_attach_command_t;
|
||||
|
||||
typedef struct __attribute__((packed))
|
||||
{
|
||||
command_common_t common;
|
||||
uint32_t new_baudrate;
|
||||
uint32_t old_baudrate;
|
||||
} change_baudrate_command_t;
|
||||
|
||||
typedef struct __attribute__((packed))
|
||||
{
|
||||
command_common_t common;
|
||||
uint32_t address;
|
||||
uint32_t size;
|
||||
uint32_t reserved_0;
|
||||
uint32_t reserved_1;
|
||||
} spi_flash_md5_command_t;
|
||||
|
||||
typedef struct __attribute__((packed))
|
||||
{
|
||||
uint8_t direction;
|
||||
uint8_t command; // One of command_t
|
||||
uint16_t size;
|
||||
uint32_t value;
|
||||
} common_response_t;
|
||||
|
||||
typedef struct __attribute__((packed))
|
||||
{
|
||||
uint8_t failed;
|
||||
uint8_t error;
|
||||
} response_status_t;
|
||||
|
||||
typedef struct __attribute__((packed))
|
||||
{
|
||||
common_response_t common;
|
||||
response_status_t status;
|
||||
} response_t;
|
||||
|
||||
typedef struct __attribute__((packed))
|
||||
{
|
||||
common_response_t common;
|
||||
uint8_t md5[MD5_SIZE]; // ROM only
|
||||
response_status_t status;
|
||||
} rom_md5_response_t;
|
||||
|
||||
typedef struct __attribute__((packed))
|
||||
{
|
||||
command_common_t common;
|
||||
uint32_t id;
|
||||
uint32_t total_size;
|
||||
uint32_t block_size;
|
||||
uint32_t sector_size;
|
||||
uint32_t page_size;
|
||||
uint32_t status_mask;
|
||||
} write_spi_command_t;
|
||||
|
||||
esp_loader_error_t loader_initialize_conn(esp_loader_connect_args_t *connect_args);
|
||||
|
||||
#ifdef SERIAL_FLASHER_INTERFACE_UART
|
||||
esp_loader_error_t loader_flash_begin_cmd(uint32_t offset, uint32_t erase_size, uint32_t block_size, uint32_t blocks_to_write, bool encryption);
|
||||
|
||||
esp_loader_error_t loader_flash_data_cmd(const uint8_t *data, uint32_t size);
|
||||
|
||||
esp_loader_error_t loader_flash_end_cmd(bool stay_in_loader);
|
||||
|
||||
esp_loader_error_t loader_sync_cmd(void);
|
||||
|
||||
esp_loader_error_t loader_spi_attach_cmd(uint32_t config);
|
||||
|
||||
esp_loader_error_t loader_md5_cmd(uint32_t address, uint32_t size, uint8_t *md5_out);
|
||||
|
||||
esp_loader_error_t loader_spi_parameters(uint32_t total_size);
|
||||
#endif /* SERIAL_FLASHER_INTERFACE_UART */
|
||||
|
||||
esp_loader_error_t loader_mem_begin_cmd(uint32_t offset, uint32_t size, uint32_t blocks_to_write, uint32_t block_size);
|
||||
|
||||
esp_loader_error_t loader_mem_data_cmd(const uint8_t *data, uint32_t size);
|
||||
|
||||
esp_loader_error_t loader_mem_end_cmd(uint32_t entrypoint);
|
||||
|
||||
esp_loader_error_t loader_write_reg_cmd(uint32_t address, uint32_t value, uint32_t mask, uint32_t delay_us);
|
||||
|
||||
esp_loader_error_t loader_read_reg_cmd(uint32_t address, uint32_t *reg);
|
||||
|
||||
esp_loader_error_t loader_change_baudrate_cmd(uint32_t baudrate);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -1,31 +0,0 @@
|
||||
/* Copyright 2020-2023 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include "esp_loader.h"
|
||||
#include "protocol.h"
|
||||
|
||||
void log_loader_internal_error(error_code_t error);
|
||||
|
||||
esp_loader_error_t send_cmd(const void *cmd_data, uint32_t size, uint32_t *reg_value);
|
||||
|
||||
esp_loader_error_t send_cmd_with_data(const void *cmd_data, size_t cmd_size,
|
||||
const void *data, size_t data_size);
|
||||
|
||||
esp_loader_error_t send_cmd_md5(const void *cmd_data, size_t cmd_size, uint8_t md5_out[MD5_SIZE]);
|
||||
@@ -1,28 +0,0 @@
|
||||
/* Copyright 2020-2023 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "esp_loader.h"
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
esp_loader_error_t SLIP_receive_data(uint8_t *buff, size_t size);
|
||||
|
||||
esp_loader_error_t SLIP_receive_packet(uint8_t *buff, size_t size);
|
||||
|
||||
esp_loader_error_t SLIP_send(const uint8_t *data, size_t size);
|
||||
|
||||
esp_loader_error_t SLIP_send_delimiter(void);
|
||||
@@ -1,415 +0,0 @@
|
||||
/* Copyright 2020-2023 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "protocol.h"
|
||||
#include "esp_loader_io.h"
|
||||
#include "esp_loader.h"
|
||||
#include "esp_targets.h"
|
||||
#include "md5_hash.h"
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
|
||||
#ifndef MAX
|
||||
#define MAX(a, b) ((a) > (b)) ? (a) : (b)
|
||||
#endif
|
||||
|
||||
#ifndef MIN
|
||||
#define MIN(a, b) ((a) < (b)) ? (a) : (b)
|
||||
#endif
|
||||
|
||||
#ifndef ROUNDUP
|
||||
#define ROUNDUP(a, b) (((int)a + (int)b - 1) / (int)b)
|
||||
#endif
|
||||
|
||||
static const uint32_t DEFAULT_TIMEOUT = 1000;
|
||||
static const uint32_t DEFAULT_FLASH_TIMEOUT = 3000; // timeout for most flash operations
|
||||
static const uint32_t LOAD_RAM_TIMEOUT_PER_MB = 2000000; // timeout (per megabyte) for erasing a region
|
||||
|
||||
typedef enum {
|
||||
SPI_FLASH_READ_ID = 0x9F
|
||||
} spi_flash_cmd_t;
|
||||
|
||||
static const target_registers_t *s_reg = NULL;
|
||||
static target_chip_t s_target = ESP_UNKNOWN_CHIP;
|
||||
|
||||
#if MD5_ENABLED
|
||||
|
||||
static const uint32_t MD5_TIMEOUT_PER_MB = 800;
|
||||
static struct MD5Context s_md5_context;
|
||||
static uint32_t s_start_address;
|
||||
static uint32_t s_image_size;
|
||||
|
||||
static inline void init_md5(uint32_t address, uint32_t size)
|
||||
{
|
||||
s_start_address = address;
|
||||
s_image_size = size;
|
||||
MD5Init(&s_md5_context);
|
||||
}
|
||||
|
||||
static inline void md5_update(const uint8_t *data, uint32_t size)
|
||||
{
|
||||
MD5Update(&s_md5_context, data, size);
|
||||
}
|
||||
|
||||
static inline void md5_final(uint8_t digets[16])
|
||||
{
|
||||
MD5Final(digets, &s_md5_context);
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
static inline void init_md5(uint32_t address, uint32_t size) { }
|
||||
static inline void md5_update(const uint8_t *data, uint32_t size) { }
|
||||
static inline void md5_final(uint8_t digets[16]) { }
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
static uint32_t timeout_per_mb(uint32_t size_bytes, uint32_t time_per_mb)
|
||||
{
|
||||
uint32_t timeout = time_per_mb * (size_bytes / 1e6);
|
||||
return MAX(timeout, DEFAULT_FLASH_TIMEOUT);
|
||||
}
|
||||
|
||||
esp_loader_error_t esp_loader_connect(esp_loader_connect_args_t *connect_args)
|
||||
{
|
||||
loader_port_enter_bootloader();
|
||||
|
||||
RETURN_ON_ERROR(loader_initialize_conn(connect_args));
|
||||
|
||||
RETURN_ON_ERROR(loader_detect_chip(&s_target, &s_reg));
|
||||
|
||||
#ifdef SERIAL_FLASHER_INTERFACE_UART
|
||||
esp_loader_error_t err;
|
||||
uint32_t spi_config;
|
||||
if (s_target == ESP8266_CHIP) {
|
||||
err = loader_flash_begin_cmd(0, 0, 0, 0, s_target);
|
||||
} else {
|
||||
RETURN_ON_ERROR( loader_read_spi_config(s_target, &spi_config) );
|
||||
loader_port_start_timer(DEFAULT_TIMEOUT);
|
||||
err = loader_spi_attach_cmd(spi_config);
|
||||
}
|
||||
return err;
|
||||
#endif /* SERIAL_FLASHER_INTERFACE_UART */
|
||||
return ESP_LOADER_SUCCESS;
|
||||
}
|
||||
|
||||
target_chip_t esp_loader_get_target(void)
|
||||
{
|
||||
return s_target;
|
||||
}
|
||||
|
||||
#ifdef SERIAL_FLASHER_INTERFACE_UART
|
||||
static uint32_t s_flash_write_size = 0;
|
||||
|
||||
static esp_loader_error_t spi_set_data_lengths(size_t mosi_bits, size_t miso_bits)
|
||||
{
|
||||
if (mosi_bits > 0) {
|
||||
RETURN_ON_ERROR( esp_loader_write_register(s_reg->mosi_dlen, mosi_bits - 1) );
|
||||
}
|
||||
if (miso_bits > 0) {
|
||||
RETURN_ON_ERROR( esp_loader_write_register(s_reg->miso_dlen, miso_bits - 1) );
|
||||
}
|
||||
|
||||
return ESP_LOADER_SUCCESS;
|
||||
}
|
||||
|
||||
static esp_loader_error_t spi_set_data_lengths_8266(size_t mosi_bits, size_t miso_bits)
|
||||
{
|
||||
uint32_t mosi_mask = (mosi_bits == 0) ? 0 : mosi_bits - 1;
|
||||
uint32_t miso_mask = (miso_bits == 0) ? 0 : miso_bits - 1;
|
||||
return esp_loader_write_register(s_reg->usr1, (miso_mask << 8) | (mosi_mask << 17));
|
||||
}
|
||||
|
||||
static esp_loader_error_t spi_flash_command(spi_flash_cmd_t cmd, void *data_tx, size_t tx_size, void *data_rx, size_t rx_size)
|
||||
{
|
||||
assert(rx_size <= 32); // Reading more than 32 bits back from a SPI flash operation is unsupported
|
||||
assert(tx_size <= 64); // Writing more than 64 bytes of data with one SPI command is unsupported
|
||||
|
||||
uint32_t SPI_USR_CMD = (1 << 31);
|
||||
uint32_t SPI_USR_MISO = (1 << 28);
|
||||
uint32_t SPI_USR_MOSI = (1 << 27);
|
||||
uint32_t SPI_CMD_USR = (1 << 18);
|
||||
uint32_t CMD_LEN_SHIFT = 28;
|
||||
|
||||
// Save SPI configuration
|
||||
uint32_t old_spi_usr;
|
||||
uint32_t old_spi_usr2;
|
||||
RETURN_ON_ERROR( esp_loader_read_register(s_reg->usr, &old_spi_usr) );
|
||||
RETURN_ON_ERROR( esp_loader_read_register(s_reg->usr2, &old_spi_usr2) );
|
||||
|
||||
if (s_target == ESP8266_CHIP) {
|
||||
RETURN_ON_ERROR( spi_set_data_lengths_8266(tx_size, rx_size) );
|
||||
} else {
|
||||
RETURN_ON_ERROR( spi_set_data_lengths(tx_size, rx_size) );
|
||||
}
|
||||
|
||||
uint32_t usr_reg_2 = (7 << CMD_LEN_SHIFT) | cmd;
|
||||
uint32_t usr_reg = SPI_USR_CMD;
|
||||
if (rx_size > 0) {
|
||||
usr_reg |= SPI_USR_MISO;
|
||||
}
|
||||
if (tx_size > 0) {
|
||||
usr_reg |= SPI_USR_MOSI;
|
||||
}
|
||||
|
||||
RETURN_ON_ERROR( esp_loader_write_register(s_reg->usr, usr_reg) );
|
||||
RETURN_ON_ERROR( esp_loader_write_register(s_reg->usr2, usr_reg_2 ) );
|
||||
|
||||
if (tx_size == 0) {
|
||||
// clear data register before we read it
|
||||
RETURN_ON_ERROR( esp_loader_write_register(s_reg->w0, 0) );
|
||||
} else {
|
||||
uint32_t *data = (uint32_t *)data_tx;
|
||||
uint32_t words_to_write = (tx_size + 31) / (8 * 4);
|
||||
uint32_t data_reg_addr = s_reg->w0;
|
||||
|
||||
while (words_to_write--) {
|
||||
uint32_t word = *data++;
|
||||
RETURN_ON_ERROR( esp_loader_write_register(data_reg_addr, word) );
|
||||
data_reg_addr += 4;
|
||||
}
|
||||
}
|
||||
|
||||
RETURN_ON_ERROR( esp_loader_write_register(s_reg->cmd, SPI_CMD_USR) );
|
||||
|
||||
uint32_t trials = 10;
|
||||
while (trials--) {
|
||||
uint32_t cmd_reg;
|
||||
RETURN_ON_ERROR( esp_loader_read_register(s_reg->cmd, &cmd_reg) );
|
||||
if ((cmd_reg & SPI_CMD_USR) == 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (trials == 0) {
|
||||
return ESP_LOADER_ERROR_TIMEOUT;
|
||||
}
|
||||
|
||||
RETURN_ON_ERROR( esp_loader_read_register(s_reg->w0, data_rx) );
|
||||
|
||||
// Restore SPI configuration
|
||||
RETURN_ON_ERROR( esp_loader_write_register(s_reg->usr, old_spi_usr) );
|
||||
RETURN_ON_ERROR( esp_loader_write_register(s_reg->usr2, old_spi_usr2) );
|
||||
|
||||
return ESP_LOADER_SUCCESS;
|
||||
}
|
||||
|
||||
static esp_loader_error_t detect_flash_size(size_t *flash_size)
|
||||
{
|
||||
uint32_t flash_id = 0;
|
||||
|
||||
RETURN_ON_ERROR( spi_flash_command(SPI_FLASH_READ_ID, NULL, 0, &flash_id, 24) );
|
||||
uint32_t size_id = flash_id >> 16;
|
||||
|
||||
if (size_id < 0x12 || size_id > 0x18) {
|
||||
return ESP_LOADER_ERROR_UNSUPPORTED_CHIP;
|
||||
}
|
||||
|
||||
*flash_size = 1 << size_id;
|
||||
|
||||
return ESP_LOADER_SUCCESS;
|
||||
}
|
||||
|
||||
static uint32_t calc_erase_size(const target_chip_t target, const uint32_t offset,
|
||||
const uint32_t image_size)
|
||||
{
|
||||
if (target != ESP8266_CHIP) {
|
||||
return image_size;
|
||||
} else {
|
||||
/* Needed to fix a bug in the ESP8266 ROM */
|
||||
const uint32_t sectors_per_block = 16U;
|
||||
const uint32_t sector_size = 4096U;
|
||||
|
||||
const uint32_t num_sectors = (image_size + sector_size - 1) / sector_size;
|
||||
const uint32_t start_sector = offset / sector_size;
|
||||
|
||||
uint32_t head_sectors = sectors_per_block - (start_sector % sectors_per_block);
|
||||
|
||||
/* The ROM bug deletes extra num_sectors if we don't cross the block boundary
|
||||
and extra head_sectors if we do */
|
||||
if (num_sectors <= head_sectors) {
|
||||
return ((num_sectors + 1) / 2) * sector_size;
|
||||
} else {
|
||||
return (num_sectors - head_sectors) * sector_size;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
esp_loader_error_t esp_loader_flash_start(uint32_t offset, uint32_t image_size, uint32_t block_size)
|
||||
{
|
||||
s_flash_write_size = block_size;
|
||||
|
||||
size_t flash_size = 0;
|
||||
if (detect_flash_size(&flash_size) == ESP_LOADER_SUCCESS) {
|
||||
if (image_size > flash_size) {
|
||||
return ESP_LOADER_ERROR_IMAGE_SIZE;
|
||||
}
|
||||
loader_port_start_timer(DEFAULT_TIMEOUT);
|
||||
RETURN_ON_ERROR( loader_spi_parameters(flash_size) );
|
||||
} else {
|
||||
loader_port_debug_print("Flash size detection failed, falling back to default");
|
||||
}
|
||||
|
||||
init_md5(offset, image_size);
|
||||
|
||||
bool encryption_in_cmd = encryption_in_begin_flash_cmd(s_target);
|
||||
const uint32_t erase_size = calc_erase_size(esp_loader_get_target(), offset, image_size);
|
||||
const uint32_t blocks_to_write = (image_size + block_size - 1) / block_size;
|
||||
|
||||
const uint32_t erase_region_timeout_per_mb = 10000;
|
||||
loader_port_start_timer(timeout_per_mb(erase_size, erase_region_timeout_per_mb));
|
||||
return loader_flash_begin_cmd(offset, erase_size, block_size, blocks_to_write, encryption_in_cmd);
|
||||
}
|
||||
|
||||
|
||||
esp_loader_error_t esp_loader_flash_write(void *payload, uint32_t size)
|
||||
{
|
||||
uint32_t padding_bytes = s_flash_write_size - size;
|
||||
uint8_t *data = (uint8_t *)payload;
|
||||
uint32_t padding_index = size;
|
||||
|
||||
if (size > s_flash_write_size) {
|
||||
return ESP_LOADER_ERROR_INVALID_PARAM;
|
||||
}
|
||||
|
||||
const uint8_t padding_pattern = 0xFF;
|
||||
while (padding_bytes--) {
|
||||
data[padding_index++] = padding_pattern;
|
||||
}
|
||||
|
||||
md5_update(payload, (size + 3) & ~3);
|
||||
|
||||
loader_port_start_timer(DEFAULT_TIMEOUT);
|
||||
|
||||
return loader_flash_data_cmd(data, s_flash_write_size);
|
||||
}
|
||||
|
||||
|
||||
esp_loader_error_t esp_loader_flash_finish(bool reboot)
|
||||
{
|
||||
loader_port_start_timer(DEFAULT_TIMEOUT);
|
||||
|
||||
return loader_flash_end_cmd(!reboot);
|
||||
}
|
||||
#endif /* SERIAL_FLASHER_INTERFACE_UART */
|
||||
|
||||
esp_loader_error_t esp_loader_mem_start(uint32_t offset, uint32_t size, uint32_t block_size)
|
||||
{
|
||||
uint32_t blocks_to_write = ROUNDUP(size, block_size);
|
||||
loader_port_start_timer(timeout_per_mb(size, LOAD_RAM_TIMEOUT_PER_MB));
|
||||
return loader_mem_begin_cmd(offset, size, blocks_to_write, block_size);
|
||||
}
|
||||
|
||||
|
||||
esp_loader_error_t esp_loader_mem_write(const void *payload, uint32_t size)
|
||||
{
|
||||
const uint8_t *data = (const uint8_t *)payload;
|
||||
loader_port_start_timer(timeout_per_mb(size, LOAD_RAM_TIMEOUT_PER_MB));
|
||||
return loader_mem_data_cmd(data, size);
|
||||
}
|
||||
|
||||
|
||||
esp_loader_error_t esp_loader_mem_finish(uint32_t entrypoint)
|
||||
{
|
||||
loader_port_start_timer(DEFAULT_TIMEOUT);
|
||||
return loader_mem_end_cmd(entrypoint);
|
||||
}
|
||||
|
||||
|
||||
esp_loader_error_t esp_loader_read_register(uint32_t address, uint32_t *reg_value)
|
||||
{
|
||||
loader_port_start_timer(DEFAULT_TIMEOUT);
|
||||
|
||||
return loader_read_reg_cmd(address, reg_value);
|
||||
}
|
||||
|
||||
|
||||
esp_loader_error_t esp_loader_write_register(uint32_t address, uint32_t reg_value)
|
||||
{
|
||||
loader_port_start_timer(DEFAULT_TIMEOUT);
|
||||
|
||||
return loader_write_reg_cmd(address, reg_value, 0xFFFFFFFF, 0);
|
||||
}
|
||||
|
||||
esp_loader_error_t esp_loader_change_transmission_rate(uint32_t transmission_rate)
|
||||
{
|
||||
if (s_target == ESP8266_CHIP) {
|
||||
return ESP_LOADER_ERROR_UNSUPPORTED_FUNC;
|
||||
}
|
||||
|
||||
loader_port_start_timer(DEFAULT_TIMEOUT);
|
||||
|
||||
return loader_change_baudrate_cmd(transmission_rate);
|
||||
}
|
||||
|
||||
#if MD5_ENABLED
|
||||
|
||||
static void hexify(const uint8_t raw_md5[16], uint8_t hex_md5_out[32])
|
||||
{
|
||||
static const uint8_t dec_to_hex[] = {
|
||||
'0', '1', '2', '3', '4', '5', '6', '7',
|
||||
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
|
||||
};
|
||||
for (int i = 0; i < 16; i++) {
|
||||
*hex_md5_out++ = dec_to_hex[raw_md5[i] >> 4];
|
||||
*hex_md5_out++ = dec_to_hex[raw_md5[i] & 0xF];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
esp_loader_error_t esp_loader_flash_verify(void)
|
||||
{
|
||||
if (s_target == ESP8266_CHIP) {
|
||||
return ESP_LOADER_ERROR_UNSUPPORTED_FUNC;
|
||||
}
|
||||
|
||||
uint8_t raw_md5[16] = {0};
|
||||
|
||||
/* Zero termination and new line character require 2 bytes */
|
||||
uint8_t hex_md5[MD5_SIZE + 2] = {0};
|
||||
uint8_t received_md5[MD5_SIZE + 2] = {0};
|
||||
|
||||
md5_final(raw_md5);
|
||||
hexify(raw_md5, hex_md5);
|
||||
|
||||
loader_port_start_timer(timeout_per_mb(s_image_size, MD5_TIMEOUT_PER_MB));
|
||||
|
||||
RETURN_ON_ERROR( loader_md5_cmd(s_start_address, s_image_size, received_md5) );
|
||||
|
||||
bool md5_match = memcmp(hex_md5, received_md5, MD5_SIZE) == 0;
|
||||
|
||||
if (!md5_match) {
|
||||
hex_md5[MD5_SIZE] = '\n';
|
||||
received_md5[MD5_SIZE] = '\n';
|
||||
|
||||
loader_port_debug_print("Error: MD5 checksum does not match:\n");
|
||||
loader_port_debug_print("Expected:\n");
|
||||
loader_port_debug_print((char *)received_md5);
|
||||
loader_port_debug_print("Actual:\n");
|
||||
loader_port_debug_print((char *)hex_md5);
|
||||
|
||||
return ESP_LOADER_ERROR_INVALID_MD5;
|
||||
}
|
||||
|
||||
return ESP_LOADER_SUCCESS;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
void esp_loader_reset_target(void)
|
||||
{
|
||||
loader_port_reset_target();
|
||||
}
|
||||
@@ -1,263 +0,0 @@
|
||||
/* Copyright 2020 Espressif Systems (Shanghai) PTE LTD
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "esp_targets.h"
|
||||
#include <stddef.h>
|
||||
|
||||
#define MAX_MAGIC_VALUES 2
|
||||
|
||||
typedef esp_loader_error_t (*read_spi_config_t)(uint32_t efuse_base, uint32_t *spi_config);
|
||||
|
||||
typedef struct {
|
||||
target_registers_t regs;
|
||||
uint32_t efuse_base;
|
||||
uint32_t chip_magic_value[MAX_MAGIC_VALUES];
|
||||
read_spi_config_t read_spi_config;
|
||||
bool encryption_in_begin_flash_cmd;
|
||||
} esp_target_t;
|
||||
|
||||
// This ROM address has a different value on each chip model
|
||||
#define CHIP_DETECT_MAGIC_REG_ADDR 0x40001000
|
||||
|
||||
#define ESP8266_SPI_REG_BASE 0x60000200
|
||||
#define ESP32S2_SPI_REG_BASE 0x3f402000
|
||||
#define ESP32xx_SPI_REG_BASE 0x60002000
|
||||
#define ESP32_SPI_REG_BASE 0x3ff42000
|
||||
|
||||
static esp_loader_error_t spi_config_esp32(uint32_t efuse_base, uint32_t *spi_config);
|
||||
static esp_loader_error_t spi_config_esp32xx(uint32_t efuse_base, uint32_t *spi_config);
|
||||
|
||||
static const esp_target_t esp_target[ESP_MAX_CHIP] = {
|
||||
|
||||
// ESP8266
|
||||
{
|
||||
.regs = {
|
||||
.cmd = ESP8266_SPI_REG_BASE + 0x00,
|
||||
.usr = ESP8266_SPI_REG_BASE + 0x1c,
|
||||
.usr1 = ESP8266_SPI_REG_BASE + 0x20,
|
||||
.usr2 = ESP8266_SPI_REG_BASE + 0x24,
|
||||
.w0 = ESP8266_SPI_REG_BASE + 0x40,
|
||||
.mosi_dlen = 0,
|
||||
.miso_dlen = 0,
|
||||
},
|
||||
.efuse_base = 0, // Not used
|
||||
.chip_magic_value = { 0xfff0c101, 0 },
|
||||
.read_spi_config = NULL, // Not used
|
||||
},
|
||||
|
||||
// ESP32
|
||||
{
|
||||
.regs = {
|
||||
.cmd = ESP32_SPI_REG_BASE + 0x00,
|
||||
.usr = ESP32_SPI_REG_BASE + 0x1c,
|
||||
.usr1 = ESP32_SPI_REG_BASE + 0x20,
|
||||
.usr2 = ESP32_SPI_REG_BASE + 0x24,
|
||||
.w0 = ESP32_SPI_REG_BASE + 0x80,
|
||||
.mosi_dlen = ESP32_SPI_REG_BASE + 0x28,
|
||||
.miso_dlen = ESP32_SPI_REG_BASE + 0x2c,
|
||||
},
|
||||
.efuse_base = 0x3ff5A000,
|
||||
.chip_magic_value = { 0x00f01d83, 0 },
|
||||
.read_spi_config = spi_config_esp32,
|
||||
},
|
||||
|
||||
// ESP32S2
|
||||
{
|
||||
.regs = {
|
||||
.cmd = ESP32S2_SPI_REG_BASE + 0x00,
|
||||
.usr = ESP32S2_SPI_REG_BASE + 0x18,
|
||||
.usr1 = ESP32S2_SPI_REG_BASE + 0x1c,
|
||||
.usr2 = ESP32S2_SPI_REG_BASE + 0x20,
|
||||
.w0 = ESP32S2_SPI_REG_BASE + 0x58,
|
||||
.mosi_dlen = ESP32S2_SPI_REG_BASE + 0x24,
|
||||
.miso_dlen = ESP32S2_SPI_REG_BASE + 0x28,
|
||||
},
|
||||
.efuse_base = 0x3f41A000,
|
||||
.chip_magic_value = { 0x000007c6, 0 },
|
||||
.read_spi_config = spi_config_esp32xx,
|
||||
},
|
||||
|
||||
// ESP32C3
|
||||
{
|
||||
.regs = {
|
||||
.cmd = ESP32xx_SPI_REG_BASE + 0x00,
|
||||
.usr = ESP32xx_SPI_REG_BASE + 0x18,
|
||||
.usr1 = ESP32xx_SPI_REG_BASE + 0x1c,
|
||||
.usr2 = ESP32xx_SPI_REG_BASE + 0x20,
|
||||
.w0 = ESP32xx_SPI_REG_BASE + 0x58,
|
||||
.mosi_dlen = ESP32xx_SPI_REG_BASE + 0x24,
|
||||
.miso_dlen = ESP32xx_SPI_REG_BASE + 0x28,
|
||||
},
|
||||
.efuse_base = 0x60008800,
|
||||
.chip_magic_value = { 0x6921506f, 0x1b31506f },
|
||||
.read_spi_config = spi_config_esp32xx,
|
||||
},
|
||||
|
||||
// ESP32S3
|
||||
{
|
||||
.regs = {
|
||||
.cmd = ESP32xx_SPI_REG_BASE + 0x00,
|
||||
.usr = ESP32xx_SPI_REG_BASE + 0x18,
|
||||
.usr1 = ESP32xx_SPI_REG_BASE + 0x1c,
|
||||
.usr2 = ESP32xx_SPI_REG_BASE + 0x20,
|
||||
.w0 = ESP32xx_SPI_REG_BASE + 0x58,
|
||||
.mosi_dlen = ESP32xx_SPI_REG_BASE + 0x24,
|
||||
.miso_dlen = ESP32xx_SPI_REG_BASE + 0x28,
|
||||
},
|
||||
.efuse_base = 0x60007000,
|
||||
.chip_magic_value = { 0x00000009, 0 },
|
||||
.read_spi_config = spi_config_esp32xx,
|
||||
},
|
||||
|
||||
// ESP32C2
|
||||
{
|
||||
.regs = {
|
||||
.cmd = ESP32xx_SPI_REG_BASE + 0x00,
|
||||
.usr = ESP32xx_SPI_REG_BASE + 0x18,
|
||||
.usr1 = ESP32xx_SPI_REG_BASE + 0x1c,
|
||||
.usr2 = ESP32xx_SPI_REG_BASE + 0x20,
|
||||
.w0 = ESP32xx_SPI_REG_BASE + 0x58,
|
||||
.mosi_dlen = ESP32xx_SPI_REG_BASE + 0x24,
|
||||
.miso_dlen = ESP32xx_SPI_REG_BASE + 0x28,
|
||||
},
|
||||
.efuse_base = 0x60008800,
|
||||
.chip_magic_value = { 0x6f51306f, 0x7c41a06f },
|
||||
.read_spi_config = spi_config_esp32xx,
|
||||
},
|
||||
// ESP32H4
|
||||
{
|
||||
.regs = {
|
||||
.cmd = ESP32xx_SPI_REG_BASE + 0x00,
|
||||
.usr = ESP32xx_SPI_REG_BASE + 0x18,
|
||||
.usr1 = ESP32xx_SPI_REG_BASE + 0x1c,
|
||||
.usr2 = ESP32xx_SPI_REG_BASE + 0x20,
|
||||
.w0 = ESP32xx_SPI_REG_BASE + 0x58,
|
||||
.mosi_dlen = ESP32xx_SPI_REG_BASE + 0x24,
|
||||
.miso_dlen = ESP32xx_SPI_REG_BASE + 0x28,
|
||||
},
|
||||
.efuse_base = 0x6001A000,
|
||||
.chip_magic_value = {0xca26cc22, 0x6881b06f}, // ESP32H4-BETA1, ESP32H4-BETA2
|
||||
.read_spi_config = spi_config_esp32xx,
|
||||
},
|
||||
// ESP32H2
|
||||
{
|
||||
.regs = {
|
||||
.cmd = ESP32xx_SPI_REG_BASE + 0x00,
|
||||
.usr = ESP32xx_SPI_REG_BASE + 0x18,
|
||||
.usr1 = ESP32xx_SPI_REG_BASE + 0x1c,
|
||||
.usr2 = ESP32xx_SPI_REG_BASE + 0x20,
|
||||
.w0 = ESP32xx_SPI_REG_BASE + 0x58,
|
||||
.mosi_dlen = ESP32xx_SPI_REG_BASE + 0x24,
|
||||
.miso_dlen = ESP32xx_SPI_REG_BASE + 0x28,
|
||||
},
|
||||
.efuse_base = 0x6001A000,
|
||||
.chip_magic_value = {0xd7b73e80, 0},
|
||||
.read_spi_config = spi_config_esp32xx,
|
||||
},
|
||||
};
|
||||
|
||||
const target_registers_t *get_esp_target_data(target_chip_t chip)
|
||||
{
|
||||
return (const target_registers_t *)&esp_target[chip];
|
||||
}
|
||||
|
||||
esp_loader_error_t loader_detect_chip(target_chip_t *target_chip, const target_registers_t **target_data)
|
||||
{
|
||||
uint32_t magic_value;
|
||||
RETURN_ON_ERROR( esp_loader_read_register(CHIP_DETECT_MAGIC_REG_ADDR, &magic_value) );
|
||||
|
||||
for (int chip = 0; chip < ESP_MAX_CHIP; chip++) {
|
||||
for(int index = 0; index < MAX_MAGIC_VALUES; index++) {
|
||||
if (magic_value == esp_target[chip].chip_magic_value[index]) {
|
||||
*target_chip = (target_chip_t)chip;
|
||||
*target_data = (target_registers_t *)&esp_target[chip];
|
||||
return ESP_LOADER_SUCCESS;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ESP_LOADER_ERROR_INVALID_TARGET;
|
||||
}
|
||||
|
||||
esp_loader_error_t loader_read_spi_config(target_chip_t target_chip, uint32_t *spi_config)
|
||||
{
|
||||
const esp_target_t *target = &esp_target[target_chip];
|
||||
return target->read_spi_config(target->efuse_base, spi_config);
|
||||
}
|
||||
|
||||
static inline uint32_t efuse_word_addr(uint32_t efuse_base, uint32_t n)
|
||||
{
|
||||
return efuse_base + (n * 4);
|
||||
}
|
||||
|
||||
// 30->GPIO32 | 31->GPIO33
|
||||
static inline uint8_t adjust_pin_number(uint8_t num)
|
||||
{
|
||||
return (num >= 30) ? num + 2 : num;
|
||||
}
|
||||
|
||||
|
||||
static esp_loader_error_t spi_config_esp32(uint32_t efuse_base, uint32_t *spi_config)
|
||||
{
|
||||
*spi_config = 0;
|
||||
|
||||
uint32_t reg5, reg3;
|
||||
RETURN_ON_ERROR( esp_loader_read_register(efuse_word_addr(efuse_base, 5), ®5) );
|
||||
RETURN_ON_ERROR( esp_loader_read_register(efuse_word_addr(efuse_base, 3), ®3) );
|
||||
|
||||
uint32_t pins = reg5 & 0xfffff;
|
||||
|
||||
if (pins == 0 || pins == 0xfffff) {
|
||||
return ESP_LOADER_SUCCESS;
|
||||
}
|
||||
|
||||
uint8_t clk = adjust_pin_number( (pins >> 0) & 0x1f );
|
||||
uint8_t q = adjust_pin_number( (pins >> 5) & 0x1f );
|
||||
uint8_t d = adjust_pin_number( (pins >> 10) & 0x1f );
|
||||
uint8_t cs = adjust_pin_number( (pins >> 15) & 0x1f );
|
||||
uint8_t hd = adjust_pin_number( (reg3 >> 4) & 0x1f );
|
||||
|
||||
if (clk == cs || clk == d || clk == q || q == cs || q == d || q == d) {
|
||||
return ESP_LOADER_SUCCESS;
|
||||
}
|
||||
|
||||
*spi_config = (hd << 24) | (cs << 18) | (d << 12) | (q << 6) | clk;
|
||||
|
||||
return ESP_LOADER_SUCCESS;
|
||||
}
|
||||
|
||||
// Applies for esp32s2, esp32c3 and esp32c3
|
||||
static esp_loader_error_t spi_config_esp32xx(uint32_t efuse_base, uint32_t *spi_config)
|
||||
{
|
||||
*spi_config = 0;
|
||||
|
||||
uint32_t reg1, reg2;
|
||||
RETURN_ON_ERROR( esp_loader_read_register(efuse_word_addr(efuse_base, 18), ®1) );
|
||||
RETURN_ON_ERROR( esp_loader_read_register(efuse_word_addr(efuse_base, 19), ®2) );
|
||||
|
||||
uint32_t pins = ((reg1 >> 16) | ((reg2 & 0xfffff) << 16)) & 0x3fffffff;
|
||||
|
||||
if (pins == 0 || pins == 0xffffffff) {
|
||||
return ESP_LOADER_SUCCESS;
|
||||
}
|
||||
|
||||
*spi_config = pins;
|
||||
return ESP_LOADER_SUCCESS;
|
||||
}
|
||||
|
||||
bool encryption_in_begin_flash_cmd(target_chip_t target)
|
||||
{
|
||||
return target == ESP32_CHIP || target == ESP8266_CHIP;
|
||||
}
|
||||
@@ -1,262 +0,0 @@
|
||||
/*
|
||||
* MD5 hash implementation and interface functions
|
||||
* Copyright (c) 2003-2005, Jouni Malinen <j@w1.fi>
|
||||
*
|
||||
* This software may be distributed under the terms of the BSD license.
|
||||
* See README for more details.
|
||||
*/
|
||||
|
||||
|
||||
#include "md5_hash.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
static void MD5Transform(uint32_t buf[4], uint32_t const in[16]);
|
||||
|
||||
|
||||
/* ===== start - public domain MD5 implementation ===== */
|
||||
/*
|
||||
* This code implements the MD5 message-digest algorithm.
|
||||
* The algorithm is due to Ron Rivest. This code was
|
||||
* written by Colin Plumb in 1993, no copyright is claimed.
|
||||
* This code is in the public domain; do with it what you wish.
|
||||
*
|
||||
* Equivalent code is available from RSA Data Security, Inc.
|
||||
* This code has been tested against that, and is equivalent,
|
||||
* except that you don't need to include two pages of legalese
|
||||
* with every copy.
|
||||
*
|
||||
* To compute the message digest of a chunk of bytes, declare an
|
||||
* MD5Context structure, pass it to MD5Init, call MD5Update as
|
||||
* needed on buffers full of bytes, and then call MD5Final, which
|
||||
* will fill a supplied 16-byte array with the digest.
|
||||
*/
|
||||
|
||||
#ifndef WORDS_BIGENDIAN
|
||||
#define byteReverse(buf, len) /* Nothing */
|
||||
#else
|
||||
/*
|
||||
* Note: this code is harmless on little-endian machines.
|
||||
*/
|
||||
static void byteReverse(unsigned char *buf, unsigned longs)
|
||||
{
|
||||
uint32_t t;
|
||||
do {
|
||||
t = (uint32_t) ((unsigned) buf[3] << 8 | buf[2]) << 16 |
|
||||
((unsigned) buf[1] << 8 | buf[0]);
|
||||
*(uint32_t *) buf = t;
|
||||
buf += 4;
|
||||
} while (--longs);
|
||||
}
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
|
||||
* initialization constants.
|
||||
*/
|
||||
void MD5Init(struct MD5Context *ctx)
|
||||
{
|
||||
ctx->buf[0] = 0x67452301;
|
||||
ctx->buf[1] = 0xefcdab89;
|
||||
ctx->buf[2] = 0x98badcfe;
|
||||
ctx->buf[3] = 0x10325476;
|
||||
|
||||
ctx->bits[0] = 0;
|
||||
ctx->bits[1] = 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Update context to reflect the concatenation of another buffer full
|
||||
* of bytes.
|
||||
*/
|
||||
void MD5Update(struct MD5Context *ctx, unsigned char const *buf, unsigned len)
|
||||
{
|
||||
uint32_t t;
|
||||
|
||||
/* Update bitcount */
|
||||
|
||||
t = ctx->bits[0];
|
||||
if ((ctx->bits[0] = t + ((uint32_t) len << 3)) < t) {
|
||||
ctx->bits[1]++; /* Carry from low to high */
|
||||
}
|
||||
ctx->bits[1] += len >> 29;
|
||||
|
||||
t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */
|
||||
|
||||
/* Handle any leading odd-sized chunks */
|
||||
|
||||
if (t) {
|
||||
unsigned char *p = (unsigned char *) ctx->in + t;
|
||||
|
||||
t = 64 - t;
|
||||
if (len < t) {
|
||||
memcpy(p, buf, len);
|
||||
return;
|
||||
}
|
||||
memcpy(p, buf, t);
|
||||
byteReverse(ctx->in, 16);
|
||||
MD5Transform((uint32_t *)ctx->buf, (uint32_t *) ctx->in);
|
||||
buf += t;
|
||||
len -= t;
|
||||
}
|
||||
/* Process data in 64-byte chunks */
|
||||
|
||||
while (len >= 64) {
|
||||
memcpy(ctx->in, buf, 64);
|
||||
byteReverse(ctx->in, 16);
|
||||
MD5Transform((uint32_t *)ctx->buf, (uint32_t *) ctx->in);
|
||||
buf += 64;
|
||||
len -= 64;
|
||||
}
|
||||
|
||||
/* Handle any remaining bytes of data. */
|
||||
|
||||
memcpy(ctx->in, buf, len);
|
||||
}
|
||||
|
||||
/*
|
||||
* Final wrapup - pad to 64-byte boundary with the bit pattern
|
||||
* 1 0* (64-bit count of bits processed, MSB-first)
|
||||
*/
|
||||
void MD5Final(unsigned char digest[16], struct MD5Context *ctx)
|
||||
{
|
||||
unsigned count;
|
||||
unsigned char *p;
|
||||
|
||||
/* Compute number of bytes mod 64 */
|
||||
count = (ctx->bits[0] >> 3) & 0x3F;
|
||||
|
||||
/* Set the first char of padding to 0x80. This is safe since there is
|
||||
always at least one byte free */
|
||||
p = ctx->in + count;
|
||||
*p++ = 0x80;
|
||||
|
||||
/* Bytes of padding needed to make 64 bytes */
|
||||
count = 64 - 1 - count;
|
||||
|
||||
/* Pad out to 56 mod 64 */
|
||||
if (count < 8) {
|
||||
/* Two lots of padding: Pad the first block to 64 bytes */
|
||||
memset(p, 0, count);
|
||||
byteReverse(ctx->in, 16);
|
||||
MD5Transform((uint32_t *)ctx->buf, (uint32_t *) ctx->in);
|
||||
|
||||
/* Now fill the next block with 56 bytes */
|
||||
memset(ctx->in, 0, 56);
|
||||
} else {
|
||||
/* Pad block to 56 bytes */
|
||||
memset(p, 0, count - 8);
|
||||
}
|
||||
byteReverse(ctx->in, 14);
|
||||
|
||||
/* Append length in bits and transform */
|
||||
((uint32_t *) ctx->in)[14] = ctx->bits[0];
|
||||
((uint32_t *) ctx->in)[15] = ctx->bits[1];
|
||||
|
||||
MD5Transform((uint32_t *)ctx->buf, (uint32_t *) ctx->in);
|
||||
byteReverse((unsigned char *) ctx->buf, 4);
|
||||
memcpy(digest, ctx->buf, 16);
|
||||
memset(ctx, 0, sizeof(struct MD5Context)); /* In case it's sensitive */
|
||||
}
|
||||
|
||||
/* The four core functions - F1 is optimized somewhat */
|
||||
|
||||
/* #define F1(x, y, z) (x & y | ~x & z) */
|
||||
#define F1(x, y, z) (z ^ (x & (y ^ z)))
|
||||
#define F2(x, y, z) F1(z, x, y)
|
||||
#define F3(x, y, z) (x ^ y ^ z)
|
||||
#define F4(x, y, z) (y ^ (x | ~z))
|
||||
|
||||
/* This is the central step in the MD5 algorithm. */
|
||||
#define MD5STEP(f, w, x, y, z, data, s) \
|
||||
( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
|
||||
|
||||
/*
|
||||
* The core of the MD5 algorithm, this alters an existing MD5 hash to
|
||||
* reflect the addition of 16 longwords of new data. MD5Update blocks
|
||||
* the data and converts bytes into longwords for this routine.
|
||||
*/
|
||||
static void MD5Transform(uint32_t buf[4], uint32_t const in[16])
|
||||
{
|
||||
register uint32_t a, b, c, d;
|
||||
|
||||
a = buf[0];
|
||||
b = buf[1];
|
||||
c = buf[2];
|
||||
d = buf[3];
|
||||
|
||||
MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
|
||||
MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
|
||||
MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
|
||||
MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
|
||||
MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
|
||||
MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
|
||||
MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
|
||||
MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
|
||||
MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
|
||||
MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
|
||||
MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
|
||||
MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
|
||||
MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
|
||||
MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
|
||||
MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
|
||||
MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
|
||||
|
||||
MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
|
||||
MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
|
||||
MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
|
||||
MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
|
||||
MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
|
||||
MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
|
||||
MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
|
||||
MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
|
||||
MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
|
||||
MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
|
||||
MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
|
||||
MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
|
||||
MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
|
||||
MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
|
||||
MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
|
||||
MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
|
||||
|
||||
MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
|
||||
MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
|
||||
MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
|
||||
MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
|
||||
MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
|
||||
MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
|
||||
MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
|
||||
MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
|
||||
MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
|
||||
MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
|
||||
MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
|
||||
MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
|
||||
MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
|
||||
MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
|
||||
MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
|
||||
MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
|
||||
|
||||
MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
|
||||
MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
|
||||
MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
|
||||
MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
|
||||
MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
|
||||
MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
|
||||
MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
|
||||
MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
|
||||
MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
|
||||
MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
|
||||
MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
|
||||
MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
|
||||
MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
|
||||
MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
|
||||
MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
|
||||
MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
|
||||
|
||||
buf[0] += a;
|
||||
buf[1] += b;
|
||||
buf[2] += c;
|
||||
buf[3] += d;
|
||||
}
|
||||
/* ===== end - public domain MD5 implementation ===== */
|
||||
@@ -1,301 +0,0 @@
|
||||
/* Copyright 2020-2023 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "protocol.h"
|
||||
#include "protocol_prv.h"
|
||||
#include "esp_loader_io.h"
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
|
||||
#define CMD_SIZE(cmd) ( sizeof(cmd) - sizeof(command_common_t) )
|
||||
|
||||
static uint32_t s_sequence_number = 0;
|
||||
|
||||
static uint8_t compute_checksum(const uint8_t *data, uint32_t size)
|
||||
{
|
||||
uint8_t checksum = 0xEF;
|
||||
|
||||
while (size--) {
|
||||
checksum ^= *data++;
|
||||
}
|
||||
|
||||
return checksum;
|
||||
}
|
||||
|
||||
void log_loader_internal_error(error_code_t error)
|
||||
{
|
||||
loader_port_debug_print("Error: ");
|
||||
|
||||
switch (error) {
|
||||
case INVALID_CRC: loader_port_debug_print("INVALID_CRC"); break;
|
||||
case INVALID_COMMAND: loader_port_debug_print("INVALID_COMMAND"); break;
|
||||
case COMMAND_FAILED: loader_port_debug_print("COMMAND_FAILED"); break;
|
||||
case FLASH_WRITE_ERR: loader_port_debug_print("FLASH_WRITE_ERR"); break;
|
||||
case FLASH_READ_ERR: loader_port_debug_print("FLASH_READ_ERR"); break;
|
||||
case READ_LENGTH_ERR: loader_port_debug_print("READ_LENGTH_ERR"); break;
|
||||
case DEFLATE_ERROR: loader_port_debug_print("DEFLATE_ERROR"); break;
|
||||
default: loader_port_debug_print("UNKNOWN ERROR"); break;
|
||||
}
|
||||
|
||||
loader_port_debug_print("\n");
|
||||
}
|
||||
|
||||
|
||||
esp_loader_error_t loader_flash_begin_cmd(uint32_t offset,
|
||||
uint32_t erase_size,
|
||||
uint32_t block_size,
|
||||
uint32_t blocks_to_write,
|
||||
bool encryption)
|
||||
{
|
||||
uint32_t encryption_size = encryption ? sizeof(uint32_t) : 0;
|
||||
|
||||
flash_begin_command_t flash_begin_cmd = {
|
||||
.common = {
|
||||
.direction = WRITE_DIRECTION,
|
||||
.command = FLASH_BEGIN,
|
||||
.size = CMD_SIZE(flash_begin_cmd) - encryption_size,
|
||||
.checksum = 0
|
||||
},
|
||||
.erase_size = erase_size,
|
||||
.packet_count = blocks_to_write,
|
||||
.packet_size = block_size,
|
||||
.offset = offset,
|
||||
.encrypted = 0
|
||||
};
|
||||
|
||||
s_sequence_number = 0;
|
||||
|
||||
return send_cmd(&flash_begin_cmd, sizeof(flash_begin_cmd) - encryption_size, NULL);
|
||||
}
|
||||
|
||||
|
||||
esp_loader_error_t loader_flash_data_cmd(const uint8_t *data, uint32_t size)
|
||||
{
|
||||
data_command_t data_cmd = {
|
||||
.common = {
|
||||
.direction = WRITE_DIRECTION,
|
||||
.command = FLASH_DATA,
|
||||
.size = CMD_SIZE(data_cmd) + size,
|
||||
.checksum = compute_checksum(data, size)
|
||||
},
|
||||
.data_size = size,
|
||||
.sequence_number = s_sequence_number++,
|
||||
};
|
||||
|
||||
return send_cmd_with_data(&data_cmd, sizeof(data_cmd), data, size);
|
||||
}
|
||||
|
||||
|
||||
esp_loader_error_t loader_flash_end_cmd(bool stay_in_loader)
|
||||
{
|
||||
flash_end_command_t end_cmd = {
|
||||
.common = {
|
||||
.direction = WRITE_DIRECTION,
|
||||
.command = FLASH_END,
|
||||
.size = CMD_SIZE(end_cmd),
|
||||
.checksum = 0
|
||||
},
|
||||
.stay_in_loader = stay_in_loader
|
||||
};
|
||||
|
||||
return send_cmd(&end_cmd, sizeof(end_cmd), NULL);
|
||||
}
|
||||
|
||||
|
||||
esp_loader_error_t loader_mem_begin_cmd(uint32_t offset, uint32_t size, uint32_t blocks_to_write, uint32_t block_size)
|
||||
{
|
||||
|
||||
mem_begin_command_t mem_begin_cmd = {
|
||||
.common = {
|
||||
.direction = WRITE_DIRECTION,
|
||||
.command = MEM_BEGIN,
|
||||
.size = CMD_SIZE(mem_begin_cmd),
|
||||
.checksum = 0
|
||||
},
|
||||
.total_size = size,
|
||||
.blocks = blocks_to_write,
|
||||
.block_size = block_size,
|
||||
.offset = offset
|
||||
};
|
||||
|
||||
s_sequence_number = 0;
|
||||
|
||||
return send_cmd(&mem_begin_cmd, sizeof(mem_begin_cmd), NULL);
|
||||
}
|
||||
|
||||
|
||||
esp_loader_error_t loader_mem_data_cmd(const uint8_t *data, uint32_t size)
|
||||
{
|
||||
data_command_t data_cmd = {
|
||||
.common = {
|
||||
.direction = WRITE_DIRECTION,
|
||||
.command = MEM_DATA,
|
||||
.size = CMD_SIZE(data_cmd) + size,
|
||||
.checksum = compute_checksum(data, size)
|
||||
},
|
||||
.data_size = size,
|
||||
.sequence_number = s_sequence_number++,
|
||||
};
|
||||
return send_cmd_with_data(&data_cmd, sizeof(data_cmd), data, size);
|
||||
}
|
||||
|
||||
esp_loader_error_t loader_mem_end_cmd(uint32_t entrypoint)
|
||||
{
|
||||
mem_end_command_t end_cmd = {
|
||||
.common = {
|
||||
.direction = WRITE_DIRECTION,
|
||||
.command = MEM_END,
|
||||
.size = CMD_SIZE(end_cmd),
|
||||
},
|
||||
.stay_in_loader = (entrypoint == 0),
|
||||
.entry_point_address = entrypoint
|
||||
};
|
||||
|
||||
return send_cmd(&end_cmd, sizeof(end_cmd), NULL);
|
||||
}
|
||||
|
||||
|
||||
esp_loader_error_t loader_sync_cmd(void)
|
||||
{
|
||||
sync_command_t sync_cmd = {
|
||||
.common = {
|
||||
.direction = WRITE_DIRECTION,
|
||||
.command = SYNC,
|
||||
.size = CMD_SIZE(sync_cmd),
|
||||
.checksum = 0
|
||||
},
|
||||
.sync_sequence = {
|
||||
0x07, 0x07, 0x12, 0x20,
|
||||
0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
|
||||
0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
|
||||
0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
|
||||
0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
|
||||
}
|
||||
};
|
||||
|
||||
return send_cmd(&sync_cmd, sizeof(sync_cmd), NULL);
|
||||
}
|
||||
|
||||
|
||||
esp_loader_error_t loader_write_reg_cmd(uint32_t address, uint32_t value,
|
||||
uint32_t mask, uint32_t delay_us)
|
||||
{
|
||||
write_reg_command_t write_cmd = {
|
||||
.common = {
|
||||
.direction = WRITE_DIRECTION,
|
||||
.command = WRITE_REG,
|
||||
.size = CMD_SIZE(write_cmd),
|
||||
.checksum = 0
|
||||
},
|
||||
.address = address,
|
||||
.value = value,
|
||||
.mask = mask,
|
||||
.delay_us = delay_us
|
||||
};
|
||||
|
||||
return send_cmd(&write_cmd, sizeof(write_cmd), NULL);
|
||||
}
|
||||
|
||||
|
||||
esp_loader_error_t loader_read_reg_cmd(uint32_t address, uint32_t *reg)
|
||||
{
|
||||
read_reg_command_t read_cmd = {
|
||||
.common = {
|
||||
.direction = WRITE_DIRECTION,
|
||||
.command = READ_REG,
|
||||
.size = CMD_SIZE(read_cmd),
|
||||
.checksum = 0
|
||||
},
|
||||
.address = address,
|
||||
};
|
||||
|
||||
return send_cmd(&read_cmd, sizeof(read_cmd), reg);
|
||||
}
|
||||
|
||||
|
||||
esp_loader_error_t loader_spi_attach_cmd(uint32_t config)
|
||||
{
|
||||
spi_attach_command_t attach_cmd = {
|
||||
.common = {
|
||||
.direction = WRITE_DIRECTION,
|
||||
.command = SPI_ATTACH,
|
||||
.size = CMD_SIZE(attach_cmd),
|
||||
.checksum = 0
|
||||
},
|
||||
.configuration = config,
|
||||
.zero = 0
|
||||
};
|
||||
|
||||
return send_cmd(&attach_cmd, sizeof(attach_cmd), NULL);
|
||||
}
|
||||
|
||||
esp_loader_error_t loader_change_baudrate_cmd(uint32_t baudrate)
|
||||
{
|
||||
change_baudrate_command_t baudrate_cmd = {
|
||||
.common = {
|
||||
.direction = WRITE_DIRECTION,
|
||||
.command = CHANGE_BAUDRATE,
|
||||
.size = CMD_SIZE(baudrate_cmd),
|
||||
.checksum = 0
|
||||
},
|
||||
.new_baudrate = baudrate,
|
||||
.old_baudrate = 0 // ESP32 ROM only
|
||||
};
|
||||
|
||||
return send_cmd(&baudrate_cmd, sizeof(baudrate_cmd), NULL);
|
||||
}
|
||||
|
||||
esp_loader_error_t loader_md5_cmd(uint32_t address, uint32_t size, uint8_t *md5_out)
|
||||
{
|
||||
spi_flash_md5_command_t md5_cmd = {
|
||||
.common = {
|
||||
.direction = WRITE_DIRECTION,
|
||||
.command = SPI_FLASH_MD5,
|
||||
.size = CMD_SIZE(md5_cmd),
|
||||
.checksum = 0
|
||||
},
|
||||
.address = address,
|
||||
.size = size,
|
||||
.reserved_0 = 0,
|
||||
.reserved_1 = 0
|
||||
};
|
||||
|
||||
return send_cmd_md5(&md5_cmd, sizeof(md5_cmd), md5_out);
|
||||
}
|
||||
|
||||
esp_loader_error_t loader_spi_parameters(uint32_t total_size)
|
||||
{
|
||||
write_spi_command_t spi_cmd = {
|
||||
.common = {
|
||||
.direction = WRITE_DIRECTION,
|
||||
.command = SPI_SET_PARAMS,
|
||||
.size = 24,
|
||||
.checksum = 0
|
||||
},
|
||||
.id = 0,
|
||||
.total_size = total_size,
|
||||
.block_size = 64 * 1024,
|
||||
.sector_size = 4 * 1024,
|
||||
.page_size = 0x100,
|
||||
.status_mask = 0xFFFF,
|
||||
};
|
||||
|
||||
return send_cmd(&spi_cmd, sizeof(spi_cmd), NULL);
|
||||
}
|
||||
|
||||
__attribute__ ((weak)) void loader_port_debug_print(const char *str)
|
||||
{
|
||||
(void)(str);
|
||||
}
|
||||
@@ -1,312 +0,0 @@
|
||||
/* Copyright 2020-2023 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "protocol.h"
|
||||
#include "protocol_prv.h"
|
||||
#include "esp_loader_io.h"
|
||||
#include <stddef.h>
|
||||
#include <assert.h>
|
||||
|
||||
typedef struct __attribute__((packed)) {
|
||||
uint8_t cmd;
|
||||
uint8_t addr;
|
||||
uint8_t dummy;
|
||||
} transaction_preamble_t;
|
||||
|
||||
typedef enum {
|
||||
TRANS_CMD_WRBUF = 0x01,
|
||||
TRANS_CMD_RDBUF = 0x02,
|
||||
TRANS_CMD_WRDMA = 0x03,
|
||||
TRANS_CMD_RDDMA = 0x04,
|
||||
TRANS_CMD_SEG_DONE = 0x05,
|
||||
TRANS_CMD_ENQPI = 0x06,
|
||||
TRANS_CMD_WR_DONE = 0x07,
|
||||
TRANS_CMD_CMD8 = 0x08,
|
||||
TRANS_CMD_CMD9 = 0x09,
|
||||
TRANS_CMD_CMDA = 0x0A,
|
||||
TRANS_CMD_EXQPI = 0xDD,
|
||||
} transaction_cmd_t;
|
||||
|
||||
/* Slave protocol registers */
|
||||
typedef enum {
|
||||
SLAVE_REGISTER_VER = 0,
|
||||
SLAVE_REGISTER_RXSTA = 4,
|
||||
SLAVE_REGISTER_TXSTA = 8,
|
||||
SLAVE_REGISTER_CMD = 12,
|
||||
} slave_register_addr_t;
|
||||
|
||||
#define SLAVE_STA_TOGGLE_BIT (0x01U << 0)
|
||||
#define SLAVE_STA_INIT_BIT (0x01U << 1)
|
||||
#define SLAVE_STA_BUF_LENGTH_POS 2U
|
||||
|
||||
typedef enum {
|
||||
SLAVE_STATE_INIT = SLAVE_STA_TOGGLE_BIT | SLAVE_STA_INIT_BIT,
|
||||
SLAVE_STATE_FIRST_PACKET = SLAVE_STA_INIT_BIT,
|
||||
} slave_state_t;
|
||||
|
||||
typedef enum {
|
||||
/* Target to host */
|
||||
SLAVE_CMD_IDLE = 0xAA,
|
||||
SLAVE_CMD_READY = 0xA5,
|
||||
/* Host to target */
|
||||
SLAVE_CMD_REBOOT = 0xFE,
|
||||
SLAVE_CMD_COMM_REINIT = 0x5A,
|
||||
SLAVE_CMD_DONE = 0x55,
|
||||
} slave_cmd_t;
|
||||
|
||||
static uint8_t s_slave_seq_tx;
|
||||
static uint8_t s_slave_seq_rx;
|
||||
|
||||
static esp_loader_error_t write_slave_reg(const uint8_t *data, const uint32_t addr,
|
||||
const uint8_t size);
|
||||
static esp_loader_error_t read_slave_reg(uint8_t *out_data, const uint32_t addr,
|
||||
const uint8_t size);
|
||||
static esp_loader_error_t handle_slave_state(const uint32_t status_reg_addr, uint8_t *seq_state,
|
||||
bool *slave_ready, uint32_t *buf_size);
|
||||
static esp_loader_error_t check_response(command_t cmd, uint32_t *reg_value);
|
||||
|
||||
esp_loader_error_t loader_initialize_conn(esp_loader_connect_args_t *connect_args)
|
||||
{
|
||||
for (uint8_t trial = 0; trial < connect_args->trials; trial++) {
|
||||
uint8_t slave_ready_flag;
|
||||
RETURN_ON_ERROR(read_slave_reg(&slave_ready_flag, SLAVE_REGISTER_CMD,
|
||||
sizeof(slave_ready_flag)));
|
||||
|
||||
if (slave_ready_flag != SLAVE_CMD_IDLE) {
|
||||
loader_port_debug_print("Waiting for Slave to be idle...\n");
|
||||
loader_port_delay_ms(100);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
const uint8_t reg_val = SLAVE_CMD_READY;
|
||||
RETURN_ON_ERROR(write_slave_reg(®_val, SLAVE_REGISTER_CMD, sizeof(reg_val)));
|
||||
|
||||
for (uint8_t trial = 0; trial < connect_args->trials; trial++) {
|
||||
uint8_t slave_ready_flag;
|
||||
RETURN_ON_ERROR(read_slave_reg(&slave_ready_flag, SLAVE_REGISTER_CMD,
|
||||
sizeof(slave_ready_flag)));
|
||||
|
||||
if (slave_ready_flag != SLAVE_CMD_READY) {
|
||||
loader_port_debug_print("Waiting for Slave to be ready...\n");
|
||||
loader_port_delay_ms(100);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return ESP_LOADER_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
esp_loader_error_t send_cmd(const void *cmd_data, uint32_t size, uint32_t *reg_value)
|
||||
{
|
||||
command_t command = ((const command_common_t *)cmd_data)->command;
|
||||
|
||||
uint32_t buf_size;
|
||||
bool slave_ready = false;
|
||||
while (!slave_ready) {
|
||||
RETURN_ON_ERROR(handle_slave_state(SLAVE_REGISTER_RXSTA, &s_slave_seq_rx, &slave_ready,
|
||||
&buf_size));
|
||||
}
|
||||
|
||||
if (size > buf_size) {
|
||||
return ESP_LOADER_ERROR_INVALID_PARAM;
|
||||
}
|
||||
|
||||
/* Start and write the command */
|
||||
transaction_preamble_t preamble = {.cmd = TRANS_CMD_WRDMA};
|
||||
|
||||
loader_port_spi_set_cs(0);
|
||||
RETURN_ON_ERROR(loader_port_write((const uint8_t *)&preamble, sizeof(preamble),
|
||||
loader_port_remaining_time()));
|
||||
RETURN_ON_ERROR(loader_port_write((const uint8_t *)cmd_data, size,
|
||||
loader_port_remaining_time()));
|
||||
loader_port_spi_set_cs(1);
|
||||
|
||||
/* Terminate the write */
|
||||
loader_port_spi_set_cs(0);
|
||||
preamble.cmd = TRANS_CMD_WR_DONE;
|
||||
RETURN_ON_ERROR(loader_port_write((const uint8_t *)&preamble, sizeof(preamble),
|
||||
loader_port_remaining_time()));
|
||||
loader_port_spi_set_cs(1);
|
||||
|
||||
return check_response(command, reg_value);
|
||||
}
|
||||
|
||||
|
||||
esp_loader_error_t send_cmd_with_data(const void *cmd_data, size_t cmd_size,
|
||||
const void *data, size_t data_size)
|
||||
{
|
||||
uint32_t buf_size;
|
||||
bool slave_ready = false;
|
||||
while (!slave_ready) {
|
||||
RETURN_ON_ERROR(handle_slave_state(SLAVE_REGISTER_RXSTA, &s_slave_seq_rx, &slave_ready,
|
||||
&buf_size));
|
||||
}
|
||||
|
||||
if (cmd_size + data_size > buf_size) {
|
||||
return ESP_LOADER_ERROR_INVALID_PARAM;
|
||||
}
|
||||
|
||||
/* Start and write the command and the data */
|
||||
transaction_preamble_t preamble = {.cmd = TRANS_CMD_WRDMA};
|
||||
|
||||
loader_port_spi_set_cs(0);
|
||||
RETURN_ON_ERROR(loader_port_write((const uint8_t *)&preamble, sizeof(preamble),
|
||||
loader_port_remaining_time()));
|
||||
RETURN_ON_ERROR(loader_port_write((const uint8_t *)cmd_data, cmd_size,
|
||||
loader_port_remaining_time()));
|
||||
RETURN_ON_ERROR(loader_port_write((const uint8_t *)data, data_size,
|
||||
loader_port_remaining_time()));
|
||||
loader_port_spi_set_cs(1);
|
||||
|
||||
/* Terminate the write */
|
||||
loader_port_spi_set_cs(0);
|
||||
preamble.cmd = TRANS_CMD_WR_DONE;
|
||||
RETURN_ON_ERROR(loader_port_write((const uint8_t *)&preamble, sizeof(preamble),
|
||||
loader_port_remaining_time()));
|
||||
loader_port_spi_set_cs(1);
|
||||
|
||||
command_t command = ((const command_common_t *)cmd_data)->command;
|
||||
return check_response(command, NULL);
|
||||
}
|
||||
|
||||
|
||||
static esp_loader_error_t read_slave_reg(uint8_t *out_data, const uint32_t addr,
|
||||
const uint8_t size)
|
||||
{
|
||||
transaction_preamble_t preamble = {
|
||||
.cmd = TRANS_CMD_RDBUF,
|
||||
.addr = addr,
|
||||
};
|
||||
|
||||
loader_port_spi_set_cs(0);
|
||||
RETURN_ON_ERROR(loader_port_write((const uint8_t *)&preamble, sizeof(preamble),
|
||||
loader_port_remaining_time()));
|
||||
RETURN_ON_ERROR(loader_port_read(out_data, size, loader_port_remaining_time()));
|
||||
loader_port_spi_set_cs(1);
|
||||
|
||||
return ESP_LOADER_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
static esp_loader_error_t write_slave_reg(const uint8_t *data, const uint32_t addr,
|
||||
const uint8_t size)
|
||||
{
|
||||
transaction_preamble_t preamble = {
|
||||
.cmd = TRANS_CMD_WRBUF,
|
||||
.addr = addr,
|
||||
};
|
||||
|
||||
loader_port_spi_set_cs(0);
|
||||
RETURN_ON_ERROR(loader_port_write((const uint8_t *)&preamble, sizeof(preamble),
|
||||
loader_port_remaining_time()));
|
||||
RETURN_ON_ERROR(loader_port_write(data, size, loader_port_remaining_time()));
|
||||
loader_port_spi_set_cs(1);
|
||||
|
||||
return ESP_LOADER_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
static esp_loader_error_t handle_slave_state(const uint32_t status_reg_addr, uint8_t *seq_state,
|
||||
bool *slave_ready, uint32_t *buf_size)
|
||||
{
|
||||
uint32_t status_reg;
|
||||
RETURN_ON_ERROR(read_slave_reg((uint8_t *)&status_reg, status_reg_addr,
|
||||
sizeof(status_reg)));
|
||||
const slave_state_t state = status_reg & (SLAVE_STA_TOGGLE_BIT | SLAVE_STA_INIT_BIT);
|
||||
|
||||
switch(state) {
|
||||
case SLAVE_STATE_INIT: {
|
||||
const uint32_t initial = 0U;
|
||||
RETURN_ON_ERROR(write_slave_reg((uint8_t *)&initial, status_reg_addr, sizeof(initial)));
|
||||
break;
|
||||
}
|
||||
|
||||
case SLAVE_STATE_FIRST_PACKET: {
|
||||
*seq_state = state & SLAVE_STA_TOGGLE_BIT;
|
||||
*buf_size = status_reg >> SLAVE_STA_BUF_LENGTH_POS;
|
||||
*slave_ready = true;
|
||||
break;
|
||||
}
|
||||
|
||||
default: {
|
||||
const uint8_t new_seq = state & SLAVE_STA_TOGGLE_BIT;
|
||||
if (new_seq != *seq_state) {
|
||||
*seq_state = new_seq;
|
||||
*buf_size = status_reg >> SLAVE_STA_BUF_LENGTH_POS;
|
||||
*slave_ready = true;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return ESP_LOADER_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
static esp_loader_error_t check_response(command_t cmd, uint32_t *reg_value)
|
||||
{
|
||||
response_t resp;
|
||||
|
||||
uint32_t buf_size;
|
||||
bool slave_ready = false;
|
||||
while (!slave_ready) {
|
||||
RETURN_ON_ERROR(handle_slave_state(SLAVE_REGISTER_TXSTA, &s_slave_seq_tx, &slave_ready,
|
||||
&buf_size));
|
||||
}
|
||||
|
||||
if (sizeof(resp) > buf_size) {
|
||||
return ESP_LOADER_ERROR_INVALID_PARAM;
|
||||
}
|
||||
|
||||
transaction_preamble_t preamble = {
|
||||
.cmd = TRANS_CMD_RDDMA,
|
||||
};
|
||||
|
||||
loader_port_spi_set_cs(0);
|
||||
RETURN_ON_ERROR(loader_port_write((const uint8_t *)&preamble, sizeof(preamble),
|
||||
loader_port_remaining_time()));
|
||||
RETURN_ON_ERROR(loader_port_read((uint8_t *)&resp, sizeof(resp),
|
||||
loader_port_remaining_time()));
|
||||
loader_port_spi_set_cs(1);
|
||||
|
||||
/* Terminate the read */
|
||||
loader_port_spi_set_cs(0);
|
||||
preamble.cmd = TRANS_CMD_CMD8;
|
||||
RETURN_ON_ERROR(loader_port_write((const uint8_t *)&preamble, sizeof(preamble),
|
||||
loader_port_remaining_time()));
|
||||
loader_port_spi_set_cs(1);
|
||||
|
||||
common_response_t *common = (common_response_t *)&resp;
|
||||
if ((common->direction != READ_DIRECTION) || (common->command != cmd)) {
|
||||
return ESP_LOADER_ERROR_INVALID_RESPONSE;
|
||||
}
|
||||
|
||||
response_status_t *status =
|
||||
(response_status_t *)((uint8_t *)&resp + sizeof(resp) - sizeof(response_status_t));
|
||||
if (status->failed) {
|
||||
log_loader_internal_error(status->error);
|
||||
return ESP_LOADER_ERROR_INVALID_RESPONSE;
|
||||
}
|
||||
|
||||
if (reg_value != NULL) {
|
||||
*reg_value = common->value;
|
||||
}
|
||||
|
||||
return ESP_LOADER_SUCCESS;
|
||||
}
|
||||
@@ -1,114 +0,0 @@
|
||||
/* Copyright 2020-2023 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "protocol.h"
|
||||
#include "protocol_prv.h"
|
||||
#include "esp_loader_io.h"
|
||||
#include "slip.h"
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
|
||||
static esp_loader_error_t check_response(command_t cmd, uint32_t *reg_value, void* resp, uint32_t resp_size);
|
||||
|
||||
esp_loader_error_t loader_initialize_conn(esp_loader_connect_args_t *connect_args) {
|
||||
esp_loader_error_t err;
|
||||
int32_t trials = connect_args->trials;
|
||||
|
||||
do {
|
||||
loader_port_start_timer(connect_args->sync_timeout);
|
||||
err = loader_sync_cmd();
|
||||
if (err == ESP_LOADER_ERROR_TIMEOUT) {
|
||||
if (--trials == 0) {
|
||||
return ESP_LOADER_ERROR_TIMEOUT;
|
||||
}
|
||||
loader_port_delay_ms(100);
|
||||
} else if (err != ESP_LOADER_SUCCESS) {
|
||||
return err;
|
||||
}
|
||||
} while (err != ESP_LOADER_SUCCESS);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
esp_loader_error_t send_cmd(const void *cmd_data, uint32_t size, uint32_t *reg_value)
|
||||
{
|
||||
response_t response;
|
||||
command_t command = ((const command_common_t *)cmd_data)->command;
|
||||
|
||||
RETURN_ON_ERROR( SLIP_send_delimiter() );
|
||||
RETURN_ON_ERROR( SLIP_send((const uint8_t *)cmd_data, size) );
|
||||
RETURN_ON_ERROR( SLIP_send_delimiter() );
|
||||
|
||||
return check_response(command, reg_value, &response, sizeof(response));
|
||||
}
|
||||
|
||||
|
||||
esp_loader_error_t send_cmd_with_data(const void *cmd_data, size_t cmd_size,
|
||||
const void *data, size_t data_size)
|
||||
{
|
||||
response_t response;
|
||||
command_t command = ((const command_common_t *)cmd_data)->command;
|
||||
|
||||
RETURN_ON_ERROR( SLIP_send_delimiter() );
|
||||
RETURN_ON_ERROR( SLIP_send((const uint8_t *)cmd_data, cmd_size) );
|
||||
RETURN_ON_ERROR( SLIP_send(data, data_size) );
|
||||
RETURN_ON_ERROR( SLIP_send_delimiter() );
|
||||
|
||||
return check_response(command, NULL, &response, sizeof(response));
|
||||
}
|
||||
|
||||
|
||||
esp_loader_error_t send_cmd_md5(const void *cmd_data, size_t cmd_size, uint8_t md5_out[MD5_SIZE])
|
||||
{
|
||||
rom_md5_response_t response;
|
||||
command_t command = ((const command_common_t *)cmd_data)->command;
|
||||
|
||||
RETURN_ON_ERROR( SLIP_send_delimiter() );
|
||||
RETURN_ON_ERROR( SLIP_send((const uint8_t *)cmd_data, cmd_size) );
|
||||
RETURN_ON_ERROR( SLIP_send_delimiter() );
|
||||
|
||||
RETURN_ON_ERROR( check_response(command, NULL, &response, sizeof(response)) );
|
||||
|
||||
memcpy(md5_out, response.md5, MD5_SIZE);
|
||||
|
||||
return ESP_LOADER_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
static esp_loader_error_t check_response(command_t cmd, uint32_t *reg_value, void* resp, uint32_t resp_size)
|
||||
{
|
||||
esp_loader_error_t err;
|
||||
common_response_t *response = (common_response_t *)resp;
|
||||
|
||||
do {
|
||||
err = SLIP_receive_packet(resp, resp_size);
|
||||
if (err != ESP_LOADER_SUCCESS) {
|
||||
return err;
|
||||
}
|
||||
} while ((response->direction != READ_DIRECTION) || (response->command != cmd));
|
||||
|
||||
response_status_t *status = (response_status_t *)((uint8_t *)resp + resp_size - sizeof(response_status_t));
|
||||
|
||||
if (status->failed) {
|
||||
log_loader_internal_error(status->error);
|
||||
return ESP_LOADER_ERROR_INVALID_RESPONSE;
|
||||
}
|
||||
|
||||
if (reg_value != NULL) {
|
||||
*reg_value = response->value;
|
||||
}
|
||||
|
||||
return ESP_LOADER_SUCCESS;
|
||||
}
|
||||
@@ -1,125 +0,0 @@
|
||||
/* Copyright 2020-2023 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "slip.h"
|
||||
#include "esp_loader_io.h"
|
||||
|
||||
static const uint8_t DELIMITER = 0xC0;
|
||||
static const uint8_t C0_REPLACEMENT[2] = {0xDB, 0xDC};
|
||||
static const uint8_t DB_REPLACEMENT[2] = {0xDB, 0xDD};
|
||||
|
||||
static inline esp_loader_error_t peripheral_read(uint8_t *buff, const size_t size)
|
||||
{
|
||||
return loader_port_read(buff, size, loader_port_remaining_time());
|
||||
}
|
||||
|
||||
static inline esp_loader_error_t peripheral_write(const uint8_t *buff, const size_t size)
|
||||
{
|
||||
return loader_port_write(buff, size, loader_port_remaining_time());
|
||||
}
|
||||
|
||||
esp_loader_error_t SLIP_receive_data(uint8_t *buff, const size_t size)
|
||||
{
|
||||
uint8_t ch;
|
||||
|
||||
for (uint32_t i = 0; i < size; i++) {
|
||||
RETURN_ON_ERROR( peripheral_read(&ch, 1) );
|
||||
|
||||
if (ch == 0xDB) {
|
||||
RETURN_ON_ERROR( peripheral_read(&ch, 1) );
|
||||
if (ch == 0xDC) {
|
||||
buff[i] = 0xC0;
|
||||
} else if (ch == 0xDD) {
|
||||
buff[i] = 0xDB;
|
||||
} else {
|
||||
return ESP_LOADER_ERROR_INVALID_RESPONSE;
|
||||
}
|
||||
} else {
|
||||
buff[i] = ch;
|
||||
}
|
||||
}
|
||||
|
||||
return ESP_LOADER_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
esp_loader_error_t SLIP_receive_packet(uint8_t *buff, const size_t size)
|
||||
{
|
||||
uint8_t ch;
|
||||
|
||||
// Wait for delimiter
|
||||
do {
|
||||
RETURN_ON_ERROR( peripheral_read(&ch, 1) );
|
||||
} while (ch != DELIMITER);
|
||||
|
||||
// Workaround: bootloader sends two dummy(0xC0) bytes after response when baud rate is changed.
|
||||
do {
|
||||
RETURN_ON_ERROR( peripheral_read(&ch, 1) );
|
||||
} while (ch == DELIMITER);
|
||||
|
||||
buff[0] = ch;
|
||||
|
||||
RETURN_ON_ERROR( SLIP_receive_data(&buff[1], size - 1) );
|
||||
|
||||
// Wait for delimiter
|
||||
do {
|
||||
RETURN_ON_ERROR( peripheral_read(&ch, 1) );
|
||||
} while (ch != DELIMITER);
|
||||
|
||||
return ESP_LOADER_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
esp_loader_error_t SLIP_send(const uint8_t *data, const size_t size)
|
||||
{
|
||||
uint32_t to_write = 0; // Bytes ready to write as they are
|
||||
uint32_t written = 0; // Bytes already written
|
||||
|
||||
for (uint32_t i = 0; i < size; i++) {
|
||||
if (data[i] != 0xC0 && data[i] != 0xDB) {
|
||||
to_write++; // Queue this byte for writing
|
||||
continue;
|
||||
}
|
||||
|
||||
// We have a byte that needs encoding, write the queue first
|
||||
if (to_write > 0) {
|
||||
RETURN_ON_ERROR( peripheral_write(&data[written], to_write) );
|
||||
}
|
||||
|
||||
// Write the encoded byte
|
||||
if (data[i] == 0xC0) {
|
||||
RETURN_ON_ERROR( peripheral_write(C0_REPLACEMENT, 2) );
|
||||
} else {
|
||||
RETURN_ON_ERROR( peripheral_write(DB_REPLACEMENT, 2) );
|
||||
}
|
||||
|
||||
// Update to start again after the encoded byte
|
||||
written = i + 1;
|
||||
to_write = 0;
|
||||
}
|
||||
|
||||
// Write the rest of the bytes that didn't need encoding
|
||||
if (to_write > 0) {
|
||||
RETURN_ON_ERROR( peripheral_write(&data[written], to_write) );
|
||||
}
|
||||
|
||||
return ESP_LOADER_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
esp_loader_error_t SLIP_send_delimiter(void)
|
||||
{
|
||||
return peripheral_write(&DELIMITER, 1);
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
cmake_minimum_required(VERSION 3.5)
|
||||
|
||||
if (CONFIG_ESP_SERIAL_FLASHER)
|
||||
zephyr_include_directories(
|
||||
"${ZEPHYR_CURRENT_MODULE_DIR}/include"
|
||||
"${ZEPHYR_CURRENT_MODULE_DIR}/port"
|
||||
"${ZEPHYR_CURRENT_MODULE_DIR}/private_include"
|
||||
)
|
||||
|
||||
zephyr_interface_library_named(esp_flasher)
|
||||
|
||||
zephyr_library()
|
||||
|
||||
zephyr_library_sources(${ZEPHYR_CURRENT_MODULE_DIR}/src/esp_loader.c
|
||||
${ZEPHYR_CURRENT_MODULE_DIR}/src/esp_targets.c
|
||||
${ZEPHYR_CURRENT_MODULE_DIR}/src/protocol_common.c
|
||||
${ZEPHYR_CURRENT_MODULE_DIR}/src/protocol_uart.c
|
||||
${ZEPHYR_CURRENT_MODULE_DIR}/src/slip.c
|
||||
${ZEPHYR_CURRENT_MODULE_DIR}/src/md5_hash.c
|
||||
${ZEPHYR_CURRENT_MODULE_DIR}/port/zephyr_port.c
|
||||
)
|
||||
|
||||
target_compile_definitions(esp_flasher INTERFACE SERIAL_FLASHER_INTERFACE_UART)
|
||||
|
||||
zephyr_library_link_libraries(esp_flasher)
|
||||
|
||||
if(DEFINED MD5_ENABLED OR CONFIG_SERIAL_FLASHER_MD5_ENABLED)
|
||||
target_compile_definitions(esp_flasher INTERFACE -DMD5_ENABLED=1)
|
||||
endif()
|
||||
endif()
|
||||
@@ -1,16 +0,0 @@
|
||||
config ESP_SERIAL_FLASHER
|
||||
bool "Enable ESP serial flasher library"
|
||||
default y
|
||||
select CONSOLE_SUBSYS
|
||||
help
|
||||
Select this option to enable the ESP serial flasher library.
|
||||
|
||||
config ESP_SERIAL_FLASHER_UART_BUFSIZE
|
||||
int "ESP Serial Flasher UART buffer size"
|
||||
default 512
|
||||
help
|
||||
Buffer size for UART TX and RX packets
|
||||
|
||||
if ESP_SERIAL_FLASHER
|
||||
rsource "../Kconfig"
|
||||
endif
|
||||
@@ -1,5 +0,0 @@
|
||||
name: esp-flasher
|
||||
|
||||
build:
|
||||
cmake: zephyr
|
||||
kconfig: zephyr/Kconfig
|
||||
@@ -13,4 +13,3 @@ ADD_SCENE(wifi_marauder, script_stage_edit, ScriptStageEdit)
|
||||
ADD_SCENE(wifi_marauder, script_stage_add, ScriptStageAdd)
|
||||
ADD_SCENE(wifi_marauder, script_stage_edit_list, ScriptStageEditList)
|
||||
ADD_SCENE(wifi_marauder, sniffpmkid_options, SniffPmkidOptions)
|
||||
ADD_SCENE(wifi_marauder, flasher, Flasher)
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
#include "../wifi_marauder_app_i.h"
|
||||
|
||||
#include "../wifi_marauder_flasher.h"
|
||||
|
||||
char* _wifi_marauder_get_prefix_from_cmd(const char* command) {
|
||||
int end = strcspn(command, " ");
|
||||
char* prefix = (char*)malloc(sizeof(char) * (end + 1));
|
||||
@@ -103,20 +101,12 @@ void wifi_marauder_scene_console_output_on_enter(void* context) {
|
||||
view_dispatcher_switch_to_view(app->view_dispatcher, WifiMarauderAppViewConsoleOutput);
|
||||
|
||||
// Register callbacks to receive data
|
||||
// setup callback for general log rx thread
|
||||
if(app->flash_mode) {
|
||||
wifi_marauder_uart_set_handle_rx_data_cb(
|
||||
app->uart,
|
||||
wifi_marauder_flash_handle_rx_data_cb); // setup callback for general log rx thread
|
||||
} else {
|
||||
wifi_marauder_uart_set_handle_rx_data_cb(
|
||||
app->uart,
|
||||
wifi_marauder_console_output_handle_rx_data_cb); // setup callback for general log rx thread
|
||||
}
|
||||
|
||||
if(app->flash_mode) {
|
||||
wifi_marauder_flash_start_thread(app);
|
||||
}
|
||||
wifi_marauder_uart_set_handle_rx_data_cb(
|
||||
app->uart,
|
||||
wifi_marauder_console_output_handle_rx_data_cb); // setup callback for general log rx thread
|
||||
wifi_marauder_uart_set_handle_rx_data_cb(
|
||||
app->lp_uart,
|
||||
wifi_marauder_console_output_handle_rx_packets_cb); // setup callback for packets rx thread
|
||||
|
||||
// Get ready to send command
|
||||
if((app->is_command && app->selected_tx_string) || app->script) {
|
||||
@@ -179,11 +169,6 @@ bool wifi_marauder_scene_console_output_on_event(void* context, SceneManagerEven
|
||||
consumed = true;
|
||||
} else if(event.type == SceneManagerEventTypeTick) {
|
||||
consumed = true;
|
||||
} else {
|
||||
if(app->flash_worker_busy) {
|
||||
// ignore button presses while flashing
|
||||
consumed = true;
|
||||
}
|
||||
}
|
||||
|
||||
return consumed;
|
||||
@@ -198,10 +183,6 @@ void wifi_marauder_scene_console_output_on_exit(void* context) {
|
||||
furi_delay_ms(50);
|
||||
}
|
||||
|
||||
if(app->flash_mode) {
|
||||
wifi_marauder_flash_stop_thread(app);
|
||||
}
|
||||
|
||||
// Unregister rx callback
|
||||
wifi_marauder_uart_set_handle_rx_data_cb(app->uart, NULL);
|
||||
|
||||
|
||||
@@ -1,266 +0,0 @@
|
||||
#include "../wifi_marauder_app_i.h"
|
||||
#include "../wifi_marauder_flasher.h"
|
||||
|
||||
enum SubmenuIndex {
|
||||
SubmenuIndexS3Mode,
|
||||
SubmenuIndexBoot,
|
||||
SubmenuIndexPart,
|
||||
SubmenuIndexNvs,
|
||||
SubmenuIndexBootApp0,
|
||||
SubmenuIndexApp,
|
||||
SubmenuIndexCustom,
|
||||
SubmenuIndexFlash,
|
||||
};
|
||||
|
||||
static void wifi_marauder_scene_flasher_callback(void* context, uint32_t index) {
|
||||
WifiMarauderApp* app = context;
|
||||
|
||||
scene_manager_set_scene_state(app->scene_manager, WifiMarauderSceneFlasher, index);
|
||||
|
||||
// browse for files
|
||||
FuriString* predefined_filepath = furi_string_alloc_set_str(MARAUDER_APP_FOLDER);
|
||||
FuriString* selected_filepath = furi_string_alloc();
|
||||
DialogsFileBrowserOptions browser_options;
|
||||
dialog_file_browser_set_basic_options(&browser_options, ".bin", &I_Text_10x10);
|
||||
|
||||
// TODO refactor
|
||||
switch(index) {
|
||||
case SubmenuIndexS3Mode:
|
||||
// toggle S3 mode
|
||||
app->selected_flash_options[SelectedFlashS3Mode] =
|
||||
!app->selected_flash_options[SelectedFlashS3Mode];
|
||||
view_dispatcher_send_custom_event(app->view_dispatcher, WifiMarauderEventRefreshSubmenu);
|
||||
break;
|
||||
case SubmenuIndexBoot:
|
||||
app->selected_flash_options[SelectedFlashBoot] =
|
||||
!app->selected_flash_options[SelectedFlashBoot];
|
||||
if(app->selected_flash_options[SelectedFlashBoot]) {
|
||||
if(dialog_file_browser_show(
|
||||
app->dialogs, selected_filepath, predefined_filepath, &browser_options)) {
|
||||
strncpy(
|
||||
app->bin_file_path_boot,
|
||||
furi_string_get_cstr(selected_filepath),
|
||||
sizeof(app->bin_file_path_boot));
|
||||
}
|
||||
}
|
||||
if(app->bin_file_path_boot[0] == '\0') {
|
||||
// if user didn't select a file, leave unselected
|
||||
app->selected_flash_options[SelectedFlashBoot] = false;
|
||||
}
|
||||
view_dispatcher_send_custom_event(app->view_dispatcher, WifiMarauderEventRefreshSubmenu);
|
||||
break;
|
||||
case SubmenuIndexPart:
|
||||
app->selected_flash_options[SelectedFlashPart] =
|
||||
!app->selected_flash_options[SelectedFlashPart];
|
||||
if(dialog_file_browser_show(
|
||||
app->dialogs, selected_filepath, predefined_filepath, &browser_options)) {
|
||||
strncpy(
|
||||
app->bin_file_path_part,
|
||||
furi_string_get_cstr(selected_filepath),
|
||||
sizeof(app->bin_file_path_part));
|
||||
}
|
||||
if(app->bin_file_path_part[0] == '\0') {
|
||||
// if user didn't select a file, leave unselected
|
||||
app->selected_flash_options[SelectedFlashPart] = false;
|
||||
}
|
||||
view_dispatcher_send_custom_event(app->view_dispatcher, WifiMarauderEventRefreshSubmenu);
|
||||
break;
|
||||
case SubmenuIndexNvs:
|
||||
app->selected_flash_options[SelectedFlashNvs] =
|
||||
!app->selected_flash_options[SelectedFlashNvs];
|
||||
if(dialog_file_browser_show(
|
||||
app->dialogs, selected_filepath, predefined_filepath, &browser_options)) {
|
||||
strncpy(
|
||||
app->bin_file_path_nvs,
|
||||
furi_string_get_cstr(selected_filepath),
|
||||
sizeof(app->bin_file_path_nvs));
|
||||
}
|
||||
if(app->bin_file_path_nvs[0] == '\0') {
|
||||
// if user didn't select a file, leave unselected
|
||||
app->selected_flash_options[SelectedFlashNvs] = false;
|
||||
}
|
||||
view_dispatcher_send_custom_event(app->view_dispatcher, WifiMarauderEventRefreshSubmenu);
|
||||
break;
|
||||
case SubmenuIndexBootApp0:
|
||||
app->selected_flash_options[SelectedFlashBootApp0] =
|
||||
!app->selected_flash_options[SelectedFlashBootApp0];
|
||||
if(dialog_file_browser_show(
|
||||
app->dialogs, selected_filepath, predefined_filepath, &browser_options)) {
|
||||
strncpy(
|
||||
app->bin_file_path_boot_app0,
|
||||
furi_string_get_cstr(selected_filepath),
|
||||
sizeof(app->bin_file_path_boot_app0));
|
||||
}
|
||||
if(app->bin_file_path_boot_app0[0] == '\0') {
|
||||
// if user didn't select a file, leave unselected
|
||||
app->selected_flash_options[SelectedFlashBootApp0] = false;
|
||||
}
|
||||
view_dispatcher_send_custom_event(app->view_dispatcher, WifiMarauderEventRefreshSubmenu);
|
||||
break;
|
||||
case SubmenuIndexApp:
|
||||
app->selected_flash_options[SelectedFlashApp] =
|
||||
!app->selected_flash_options[SelectedFlashApp];
|
||||
if(dialog_file_browser_show(
|
||||
app->dialogs, selected_filepath, predefined_filepath, &browser_options)) {
|
||||
strncpy(
|
||||
app->bin_file_path_app,
|
||||
furi_string_get_cstr(selected_filepath),
|
||||
sizeof(app->bin_file_path_app));
|
||||
}
|
||||
if(app->bin_file_path_app[0] == '\0') {
|
||||
// if user didn't select a file, leave unselected
|
||||
app->selected_flash_options[SelectedFlashApp] = false;
|
||||
}
|
||||
view_dispatcher_send_custom_event(app->view_dispatcher, WifiMarauderEventRefreshSubmenu);
|
||||
break;
|
||||
case SubmenuIndexCustom:
|
||||
app->selected_flash_options[SelectedFlashCustom] =
|
||||
!app->selected_flash_options[SelectedFlashCustom];
|
||||
if(dialog_file_browser_show(
|
||||
app->dialogs, selected_filepath, predefined_filepath, &browser_options)) {
|
||||
strncpy(
|
||||
app->bin_file_path_custom,
|
||||
furi_string_get_cstr(selected_filepath),
|
||||
sizeof(app->bin_file_path_custom));
|
||||
}
|
||||
if(app->bin_file_path_custom[0] == '\0') {
|
||||
// if user didn't select a file, leave unselected
|
||||
app->selected_flash_options[SelectedFlashCustom] = false;
|
||||
}
|
||||
view_dispatcher_send_custom_event(app->view_dispatcher, WifiMarauderEventRefreshSubmenu);
|
||||
break;
|
||||
case SubmenuIndexFlash:
|
||||
// count how many options are selected
|
||||
app->num_selected_flash_options = 0;
|
||||
for(bool* option = &app->selected_flash_options[SelectedFlashBoot];
|
||||
option < &app->selected_flash_options[NUM_FLASH_OPTIONS];
|
||||
++option) {
|
||||
if(*option) {
|
||||
++app->num_selected_flash_options;
|
||||
}
|
||||
}
|
||||
if(app->num_selected_flash_options) {
|
||||
// only start next scene if at least one option is selected
|
||||
scene_manager_next_scene(app->scene_manager, WifiMarauderSceneConsoleOutput);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
furi_string_free(selected_filepath);
|
||||
furi_string_free(predefined_filepath);
|
||||
}
|
||||
|
||||
#define STR_SELECT "[x]"
|
||||
#define STR_UNSELECT "[ ]"
|
||||
#define STR_BOOT "Bootloader (" TOSTRING(ESP_ADDR_BOOT) ")"
|
||||
#define STR_BOOT_S3 "Bootloader (" TOSTRING(ESP_ADDR_BOOT_S3) ")"
|
||||
#define STR_PART "Part Table (" TOSTRING(ESP_ADDR_PART) ")"
|
||||
#define STR_NVS "NVS (" TOSTRING(ESP_ADDR_NVS) ")"
|
||||
#define STR_BOOT_APP0 "boot_app0 (" TOSTRING(ESP_ADDR_BOOT_APP0) ")"
|
||||
#define STR_APP "Firmware (" TOSTRING(ESP_ADDR_APP) ")"
|
||||
#define STR_CUSTOM "Custom"
|
||||
#define STR_FLASH_S3 "[>] FLASH (ESP32-S3)"
|
||||
#define STR_FLASH "[>] FLASH"
|
||||
static void _refresh_submenu(WifiMarauderApp* app) {
|
||||
Submenu* submenu = app->submenu;
|
||||
|
||||
submenu_reset(app->submenu);
|
||||
|
||||
submenu_set_header(submenu, "Browse for files to flash");
|
||||
submenu_add_item(
|
||||
submenu,
|
||||
app->selected_flash_options[SelectedFlashS3Mode] ? "[x] Using ESP32-S3" :
|
||||
"[ ] Check if using S3",
|
||||
SubmenuIndexS3Mode,
|
||||
wifi_marauder_scene_flasher_callback,
|
||||
app);
|
||||
const char* strSelectBootloader = STR_UNSELECT " " STR_BOOT;
|
||||
if(app->selected_flash_options[SelectedFlashS3Mode]) {
|
||||
if(app->selected_flash_options[SelectedFlashBoot]) {
|
||||
strSelectBootloader = STR_SELECT " " STR_BOOT_S3;
|
||||
} else {
|
||||
strSelectBootloader = STR_UNSELECT " " STR_BOOT_S3;
|
||||
}
|
||||
} else {
|
||||
if(app->selected_flash_options[SelectedFlashBoot]) {
|
||||
strSelectBootloader = STR_SELECT " " STR_BOOT;
|
||||
} else {
|
||||
strSelectBootloader = STR_UNSELECT " " STR_BOOT;
|
||||
}
|
||||
}
|
||||
submenu_add_item(
|
||||
submenu, strSelectBootloader, SubmenuIndexBoot, wifi_marauder_scene_flasher_callback, app);
|
||||
submenu_add_item(
|
||||
submenu,
|
||||
app->selected_flash_options[SelectedFlashPart] ? STR_SELECT " " STR_PART :
|
||||
STR_UNSELECT " " STR_PART,
|
||||
SubmenuIndexPart,
|
||||
wifi_marauder_scene_flasher_callback,
|
||||
app);
|
||||
submenu_add_item(
|
||||
submenu,
|
||||
app->selected_flash_options[SelectedFlashNvs] ? STR_SELECT " " STR_NVS :
|
||||
STR_UNSELECT " " STR_NVS,
|
||||
SubmenuIndexNvs,
|
||||
wifi_marauder_scene_flasher_callback,
|
||||
app);
|
||||
submenu_add_item(
|
||||
submenu,
|
||||
app->selected_flash_options[SelectedFlashBootApp0] ? STR_SELECT " " STR_BOOT_APP0 :
|
||||
STR_UNSELECT " " STR_BOOT_APP0,
|
||||
SubmenuIndexBootApp0,
|
||||
wifi_marauder_scene_flasher_callback,
|
||||
app);
|
||||
submenu_add_item(
|
||||
submenu,
|
||||
app->selected_flash_options[SelectedFlashApp] ? STR_SELECT " " STR_APP :
|
||||
STR_UNSELECT " " STR_APP,
|
||||
SubmenuIndexApp,
|
||||
wifi_marauder_scene_flasher_callback,
|
||||
app);
|
||||
// TODO: custom addr
|
||||
//submenu_add_item(
|
||||
// submenu, app->selected_flash_options[SelectedFlashCustom] ? STR_SELECT " " STR_CUSTOM : STR_UNSELECT " " STR_CUSTOM, SubmenuIndexCustom, wifi_marauder_scene_flasher_callback, app);
|
||||
submenu_add_item(
|
||||
submenu,
|
||||
app->selected_flash_options[SelectedFlashS3Mode] ? STR_FLASH_S3 : STR_FLASH,
|
||||
SubmenuIndexFlash,
|
||||
wifi_marauder_scene_flasher_callback,
|
||||
app);
|
||||
|
||||
submenu_set_selected_item(
|
||||
submenu, scene_manager_get_scene_state(app->scene_manager, WifiMarauderSceneFlasher));
|
||||
view_dispatcher_switch_to_view(app->view_dispatcher, WifiMarauderAppViewSubmenu);
|
||||
}
|
||||
|
||||
void wifi_marauder_scene_flasher_on_enter(void* context) {
|
||||
WifiMarauderApp* app = context;
|
||||
|
||||
memset(app->selected_flash_options, 0, sizeof(app->selected_flash_options));
|
||||
app->bin_file_path_boot[0] = '\0';
|
||||
app->bin_file_path_part[0] = '\0';
|
||||
app->bin_file_path_nvs[0] = '\0';
|
||||
app->bin_file_path_boot_app0[0] = '\0';
|
||||
app->bin_file_path_app[0] = '\0';
|
||||
app->bin_file_path_custom[0] = '\0';
|
||||
|
||||
_refresh_submenu(app);
|
||||
}
|
||||
|
||||
bool wifi_marauder_scene_flasher_on_event(void* context, SceneManagerEvent event) {
|
||||
WifiMarauderApp* app = context;
|
||||
bool consumed = false;
|
||||
if(event.type == SceneManagerEventTypeCustom) {
|
||||
if(event.event == WifiMarauderEventRefreshSubmenu) {
|
||||
_refresh_submenu(app);
|
||||
consumed = true;
|
||||
}
|
||||
}
|
||||
|
||||
return consumed;
|
||||
}
|
||||
|
||||
void wifi_marauder_scene_flasher_on_exit(void* context) {
|
||||
WifiMarauderApp* app = context;
|
||||
submenu_reset(app->submenu);
|
||||
}
|
||||
@@ -68,6 +68,13 @@ const WifiMarauderItem items[NUM_MENU_ITEMS] = {
|
||||
NO_ARGS,
|
||||
FOCUS_CONSOLE_END,
|
||||
SHOW_STOPSCAN_TIP},
|
||||
{"Evil Portal",
|
||||
{"start"},
|
||||
1,
|
||||
{"evilportal -c start"},
|
||||
NO_ARGS,
|
||||
FOCUS_CONSOLE_END,
|
||||
SHOW_STOPSCAN_TIP},
|
||||
{"Targeted Deauth",
|
||||
{"station", "manual"},
|
||||
2,
|
||||
@@ -83,11 +90,10 @@ const WifiMarauderItem items[NUM_MENU_ITEMS] = {
|
||||
FOCUS_CONSOLE_END,
|
||||
SHOW_STOPSCAN_TIP},
|
||||
{"Sniff",
|
||||
{"beacon", "deauth", "esp", "pmkid", "probe", "pwn", "raw", "bt", "skim"},
|
||||
9,
|
||||
{"beacon", "deauth", "pmkid", "probe", "pwn", "raw", "bt", "skim"},
|
||||
8,
|
||||
{"sniffbeacon",
|
||||
"sniffdeauth",
|
||||
"sniffesp",
|
||||
"sniffpmkid",
|
||||
"sniffprobe",
|
||||
"sniffpwn",
|
||||
@@ -105,6 +111,7 @@ const WifiMarauderItem items[NUM_MENU_ITEMS] = {
|
||||
TOGGLE_ARGS,
|
||||
FOCUS_CONSOLE_END,
|
||||
NO_TIP},
|
||||
{"LED", {"hex", "pattern"}, 2, {"led -s", "led -p"}, INPUT_ARGS, FOCUS_CONSOLE_END, NO_TIP},
|
||||
{"Settings",
|
||||
{"display", "restore", "ForcePMKID", "ForceProbe", "SavePCAP", "EnableLED", "other"},
|
||||
7,
|
||||
@@ -118,10 +125,9 @@ const WifiMarauderItem items[NUM_MENU_ITEMS] = {
|
||||
TOGGLE_ARGS,
|
||||
FOCUS_CONSOLE_START,
|
||||
NO_TIP},
|
||||
{"Update", {"ota", "sd"}, 2, {"update -w", "update -s"}, NO_ARGS, FOCUS_CONSOLE_END, NO_TIP},
|
||||
{"Update", {"sd"}, 1, {"update -s"}, NO_ARGS, FOCUS_CONSOLE_END, NO_TIP},
|
||||
{"Reboot", {""}, 1, {"reboot"}, NO_ARGS, FOCUS_CONSOLE_END, NO_TIP},
|
||||
{"Help", {""}, 1, {"help"}, NO_ARGS, FOCUS_CONSOLE_START, SHOW_STOPSCAN_TIP},
|
||||
{"Reflash ESP32 (WIP)", {""}, 1, {""}, NO_ARGS, FOCUS_CONSOLE_END, NO_TIP},
|
||||
{"Scripts", {""}, 1, {""}, NO_ARGS, FOCUS_CONSOLE_END, NO_TIP},
|
||||
{"Save to flipper sdcard", // keep as last entry or change logic in callback below
|
||||
{""},
|
||||
@@ -150,17 +156,6 @@ static void wifi_marauder_scene_start_var_list_enter_callback(void* context, uin
|
||||
item->focus_console;
|
||||
app->show_stopscan_tip = item->show_stopscan_tip;
|
||||
|
||||
// TODO cleanup
|
||||
if(index == NUM_MENU_ITEMS - 3) {
|
||||
// flasher
|
||||
app->is_command = false;
|
||||
app->flash_mode = true;
|
||||
view_dispatcher_send_custom_event(app->view_dispatcher, WifiMarauderEventStartFlasher);
|
||||
return;
|
||||
}
|
||||
|
||||
app->flash_mode = false;
|
||||
|
||||
if(!app->is_command && selected_option_index == 0) {
|
||||
// View Log from start
|
||||
view_dispatcher_send_custom_event(app->view_dispatcher, WifiMarauderEventStartLogViewer);
|
||||
@@ -243,6 +238,7 @@ void wifi_marauder_scene_start_on_enter(void* context) {
|
||||
}
|
||||
|
||||
bool wifi_marauder_scene_start_on_event(void* context, SceneManagerEvent event) {
|
||||
UNUSED(context);
|
||||
WifiMarauderApp* app = context;
|
||||
bool consumed = false;
|
||||
|
||||
@@ -271,10 +267,6 @@ bool wifi_marauder_scene_start_on_event(void* context, SceneManagerEvent event)
|
||||
scene_manager_set_scene_state(
|
||||
app->scene_manager, WifiMarauderSceneStart, app->selected_menu_index);
|
||||
scene_manager_next_scene(app->scene_manager, WifiMarauderSceneSniffPmkidOptions);
|
||||
} else if(event.event == WifiMarauderEventStartFlasher) {
|
||||
scene_manager_set_scene_state(
|
||||
app->scene_manager, WifiMarauderSceneStart, app->selected_menu_index);
|
||||
scene_manager_next_scene(app->scene_manager, WifiMarauderSceneFlasher);
|
||||
}
|
||||
consumed = true;
|
||||
} else if(event.type == SceneManagerEventTypeTick) {
|
||||
|
||||
@@ -46,9 +46,7 @@ void wifi_marauder_scene_text_input_on_enter(void* context) {
|
||||
// Setup view
|
||||
TextInput* text_input = app->text_input;
|
||||
// Add help message to header
|
||||
if(app->flash_mode) {
|
||||
text_input_set_header_text(text_input, "Enter destination address");
|
||||
} else if(app->special_case_input_step == 1) {
|
||||
if(app->special_case_input_step == 1) {
|
||||
text_input_set_header_text(text_input, "Enter source MAC");
|
||||
} else if(0 == strncmp("ssid -a -g", app->selected_tx_string, strlen("ssid -a -g"))) {
|
||||
text_input_set_header_text(text_input, "Enter # SSIDs to generate");
|
||||
|
||||
@@ -86,9 +86,6 @@ WifiMarauderApp* wifi_marauder_app_alloc() {
|
||||
view_dispatcher_add_view(
|
||||
app->view_dispatcher, WifiMarauderAppViewSubmenu, submenu_get_view(app->submenu));
|
||||
|
||||
app->flash_mode = false;
|
||||
app->flash_worker_busy = false;
|
||||
|
||||
scene_manager_next_scene(app->scene_manager, WifiMarauderSceneStart);
|
||||
|
||||
return app;
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define WIFI_MARAUDER_APP_VERSION "v0.5.1"
|
||||
#define WIFI_MARAUDER_APP_VERSION "v0.6.0"
|
||||
|
||||
typedef struct WifiMarauderApp WifiMarauderApp;
|
||||
|
||||
|
||||
@@ -32,7 +32,7 @@
|
||||
(XTREME_SETTINGS()->uart_esp_channel == UARTDefault ? FuriHalUartIdUSART1 : \
|
||||
FuriHalUartIdLPUART1)
|
||||
|
||||
#define NUM_MENU_ITEMS (19)
|
||||
#define NUM_MENU_ITEMS (20)
|
||||
|
||||
#define WIFI_MARAUDER_TEXT_BOX_STORE_SIZE (4096)
|
||||
#define WIFI_MARAUDER_TEXT_INPUT_STORE_SIZE (512)
|
||||
@@ -54,17 +54,6 @@ typedef enum WifiMarauderUserInputType {
|
||||
WifiMarauderUserInputTypeFileName
|
||||
} WifiMarauderUserInputType;
|
||||
|
||||
typedef enum SelectedFlashOptions {
|
||||
SelectedFlashS3Mode,
|
||||
SelectedFlashBoot,
|
||||
SelectedFlashPart,
|
||||
SelectedFlashNvs,
|
||||
SelectedFlashBootApp0,
|
||||
SelectedFlashApp,
|
||||
SelectedFlashCustom,
|
||||
NUM_FLASH_OPTIONS
|
||||
} SelectedFlashOptions;
|
||||
|
||||
struct WifiMarauderApp {
|
||||
Gui* gui;
|
||||
ViewDispatcher* view_dispatcher;
|
||||
@@ -129,19 +118,6 @@ struct WifiMarauderApp {
|
||||
int special_case_input_step;
|
||||
char special_case_input_src_addr[20];
|
||||
char special_case_input_dst_addr[20];
|
||||
|
||||
// For flashing - TODO: put into its own struct?
|
||||
bool selected_flash_options[NUM_FLASH_OPTIONS];
|
||||
int num_selected_flash_options;
|
||||
char bin_file_path_boot[100];
|
||||
char bin_file_path_part[100];
|
||||
char bin_file_path_nvs[100];
|
||||
char bin_file_path_boot_app0[100];
|
||||
char bin_file_path_app[100];
|
||||
char bin_file_path_custom[100];
|
||||
FuriThread* flash_worker;
|
||||
bool flash_worker_busy;
|
||||
bool flash_mode;
|
||||
};
|
||||
|
||||
// Supported commands:
|
||||
|
||||
@@ -9,7 +9,5 @@ typedef enum {
|
||||
WifiMarauderEventStartSettingsInit,
|
||||
WifiMarauderEventStartLogViewer,
|
||||
WifiMarauderEventStartScriptSelect,
|
||||
WifiMarauderEventStartSniffPmkidOptions,
|
||||
WifiMarauderEventStartFlasher,
|
||||
WifiMarauderEventRefreshSubmenu
|
||||
WifiMarauderEventStartSniffPmkidOptions
|
||||
} WifiMarauderCustomEvent;
|
||||
|
||||
@@ -1,251 +0,0 @@
|
||||
#include "wifi_marauder_flasher.h"
|
||||
|
||||
FuriStreamBuffer* flash_rx_stream; // TODO make safe
|
||||
WifiMarauderApp* global_app; // TODO make safe
|
||||
FuriTimer* timer; // TODO make
|
||||
|
||||
static uint32_t _remaining_time = 0;
|
||||
static void _timer_callback(void* context) {
|
||||
UNUSED(context);
|
||||
if(_remaining_time > 0) {
|
||||
_remaining_time--;
|
||||
}
|
||||
}
|
||||
|
||||
static esp_loader_error_t _flash_file(WifiMarauderApp* app, char* filepath, uint32_t addr) {
|
||||
// TODO cleanup
|
||||
esp_loader_error_t err;
|
||||
static uint8_t payload[1024];
|
||||
File* bin_file = storage_file_alloc(app->storage);
|
||||
|
||||
char user_msg[256];
|
||||
|
||||
// open file
|
||||
if(!storage_file_open(bin_file, filepath, FSAM_READ, FSOM_OPEN_EXISTING)) {
|
||||
storage_file_close(bin_file);
|
||||
storage_file_free(bin_file);
|
||||
dialog_message_show_storage_error(app->dialogs, "Cannot open file");
|
||||
return ESP_LOADER_ERROR_FAIL;
|
||||
}
|
||||
|
||||
uint64_t size = storage_file_size(bin_file);
|
||||
|
||||
loader_port_debug_print("Erasing flash...this may take a while\n");
|
||||
err = esp_loader_flash_start(addr, size, sizeof(payload));
|
||||
if(err != ESP_LOADER_SUCCESS) {
|
||||
storage_file_close(bin_file);
|
||||
storage_file_free(bin_file);
|
||||
snprintf(user_msg, sizeof(user_msg), "Erasing flash failed with error %d\n", err);
|
||||
loader_port_debug_print(user_msg);
|
||||
return err;
|
||||
}
|
||||
|
||||
loader_port_debug_print("Start programming\n");
|
||||
uint64_t last_updated = size;
|
||||
while(size > 0) {
|
||||
if((last_updated - size) > 50000) {
|
||||
// inform user every 50k bytes
|
||||
// TODO: draw a progress bar next update
|
||||
snprintf(user_msg, sizeof(user_msg), "%llu bytes left.\n", size);
|
||||
loader_port_debug_print(user_msg);
|
||||
last_updated = size;
|
||||
}
|
||||
size_t to_read = MIN(size, sizeof(payload));
|
||||
uint16_t num_bytes = storage_file_read(bin_file, payload, to_read);
|
||||
err = esp_loader_flash_write(payload, num_bytes);
|
||||
if(err != ESP_LOADER_SUCCESS) {
|
||||
snprintf(user_msg, sizeof(user_msg), "Packet could not be written! Error: %u\n", err);
|
||||
storage_file_close(bin_file);
|
||||
storage_file_free(bin_file);
|
||||
loader_port_debug_print(user_msg);
|
||||
return err;
|
||||
}
|
||||
|
||||
size -= num_bytes;
|
||||
}
|
||||
|
||||
loader_port_debug_print("Finished programming\n");
|
||||
|
||||
// TODO verify
|
||||
|
||||
storage_file_close(bin_file);
|
||||
storage_file_free(bin_file);
|
||||
|
||||
return ESP_LOADER_SUCCESS;
|
||||
}
|
||||
|
||||
typedef struct {
|
||||
SelectedFlashOptions selected;
|
||||
const char* description;
|
||||
char* path;
|
||||
uint32_t addr;
|
||||
} FlashItem;
|
||||
|
||||
static void _flash_all_files(WifiMarauderApp* app) {
|
||||
esp_loader_error_t err;
|
||||
const int num_steps = app->num_selected_flash_options;
|
||||
|
||||
#define NUM_FLASH_ITEMS 6
|
||||
FlashItem items[NUM_FLASH_ITEMS] = {
|
||||
{SelectedFlashBoot,
|
||||
"bootloader",
|
||||
app->bin_file_path_boot,
|
||||
app->selected_flash_options[SelectedFlashS3Mode] ? ESP_ADDR_BOOT_S3 : ESP_ADDR_BOOT},
|
||||
{SelectedFlashPart, "partition table", app->bin_file_path_part, ESP_ADDR_PART},
|
||||
{SelectedFlashNvs, "NVS", app->bin_file_path_nvs, ESP_ADDR_NVS},
|
||||
{SelectedFlashBootApp0, "boot_app0", app->bin_file_path_boot_app0, ESP_ADDR_BOOT_APP0},
|
||||
{SelectedFlashApp, "firmware", app->bin_file_path_app, ESP_ADDR_APP},
|
||||
{SelectedFlashCustom, "custom data", app->bin_file_path_custom, 0x0},
|
||||
/* if you add more entries, update NUM_FLASH_ITEMS above! */
|
||||
};
|
||||
|
||||
char user_msg[256];
|
||||
|
||||
int current_step = 1;
|
||||
for(FlashItem* item = &items[0]; item < &items[NUM_FLASH_ITEMS]; ++item) {
|
||||
if(app->selected_flash_options[item->selected]) {
|
||||
snprintf(
|
||||
user_msg,
|
||||
sizeof(user_msg),
|
||||
"Flashing %s (%d/%d) to address 0x%lx\n",
|
||||
item->description,
|
||||
current_step++,
|
||||
num_steps,
|
||||
item->addr);
|
||||
loader_port_debug_print(user_msg);
|
||||
err = _flash_file(app, item->path, item->addr);
|
||||
if(err) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static int32_t wifi_marauder_flash_bin(void* context) {
|
||||
WifiMarauderApp* app = (void*)context;
|
||||
esp_loader_error_t err;
|
||||
|
||||
app->flash_worker_busy = true;
|
||||
|
||||
// alloc global objects
|
||||
flash_rx_stream = furi_stream_buffer_alloc(RX_BUF_SIZE, 1);
|
||||
timer = furi_timer_alloc(_timer_callback, FuriTimerTypePeriodic, app);
|
||||
|
||||
loader_port_debug_print("Connecting\n");
|
||||
esp_loader_connect_args_t connect_config = ESP_LOADER_CONNECT_DEFAULT();
|
||||
err = esp_loader_connect(&connect_config);
|
||||
if(err != ESP_LOADER_SUCCESS) {
|
||||
char err_msg[256];
|
||||
snprintf(err_msg, sizeof(err_msg), "Cannot connect to target. Error: %u\n", err);
|
||||
loader_port_debug_print(err_msg);
|
||||
}
|
||||
|
||||
#if 0 // still getting packet drops with this
|
||||
// higher BR
|
||||
if(!err) {
|
||||
loader_port_debug_print("Increasing speed for faster flash\n");
|
||||
err = esp_loader_change_transmission_rate(230400);
|
||||
if (err != ESP_LOADER_SUCCESS) {
|
||||
char err_msg[256];
|
||||
snprintf(
|
||||
err_msg,
|
||||
sizeof(err_msg),
|
||||
"Cannot change transmission rate. Error: %u\n",
|
||||
err);
|
||||
loader_port_debug_print(err_msg);
|
||||
}
|
||||
furi_hal_uart_set_br(FuriHalUartIdUSART1, 230400);
|
||||
}
|
||||
#endif
|
||||
|
||||
if(!err) {
|
||||
loader_port_debug_print("Connected\n");
|
||||
_flash_all_files(app);
|
||||
#if 0
|
||||
loader_port_debug_print("Restoring transmission rate\n");
|
||||
furi_hal_uart_set_br(FuriHalUartIdUSART1, 115200);
|
||||
#endif
|
||||
loader_port_debug_print("Done flashing. Please reset the board manually.\n");
|
||||
}
|
||||
|
||||
// done
|
||||
app->flash_worker_busy = false;
|
||||
|
||||
// cleanup
|
||||
furi_stream_buffer_free(flash_rx_stream);
|
||||
flash_rx_stream = NULL;
|
||||
furi_timer_free(timer);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void wifi_marauder_flash_start_thread(WifiMarauderApp* app) {
|
||||
global_app = app;
|
||||
|
||||
app->flash_worker = furi_thread_alloc();
|
||||
furi_thread_set_name(app->flash_worker, "WifiMarauderFlashWorker");
|
||||
furi_thread_set_stack_size(app->flash_worker, 2048);
|
||||
furi_thread_set_context(app->flash_worker, app);
|
||||
furi_thread_set_callback(app->flash_worker, wifi_marauder_flash_bin);
|
||||
furi_thread_start(app->flash_worker);
|
||||
}
|
||||
|
||||
void wifi_marauder_flash_stop_thread(WifiMarauderApp* app) {
|
||||
furi_thread_join(app->flash_worker);
|
||||
furi_thread_free(app->flash_worker);
|
||||
}
|
||||
|
||||
esp_loader_error_t loader_port_read(uint8_t* data, uint16_t size, uint32_t timeout) {
|
||||
size_t read = furi_stream_buffer_receive(flash_rx_stream, data, size, pdMS_TO_TICKS(timeout));
|
||||
if(read < size) {
|
||||
return ESP_LOADER_ERROR_TIMEOUT;
|
||||
} else {
|
||||
return ESP_LOADER_SUCCESS;
|
||||
}
|
||||
}
|
||||
|
||||
esp_loader_error_t loader_port_write(const uint8_t* data, uint16_t size, uint32_t timeout) {
|
||||
UNUSED(timeout);
|
||||
wifi_marauder_uart_tx((uint8_t*)data, size);
|
||||
return ESP_LOADER_SUCCESS;
|
||||
}
|
||||
|
||||
void loader_port_enter_bootloader(void) {
|
||||
// unimplemented
|
||||
}
|
||||
|
||||
void loader_port_delay_ms(uint32_t ms) {
|
||||
furi_delay_ms(ms);
|
||||
}
|
||||
|
||||
void loader_port_start_timer(uint32_t ms) {
|
||||
_remaining_time = ms;
|
||||
furi_timer_start(timer, pdMS_TO_TICKS(1));
|
||||
}
|
||||
|
||||
uint32_t loader_port_remaining_time(void) {
|
||||
return _remaining_time;
|
||||
}
|
||||
|
||||
extern void wifi_marauder_console_output_handle_rx_data_cb(
|
||||
uint8_t* buf,
|
||||
size_t len,
|
||||
void* context); // TODO cleanup
|
||||
void loader_port_debug_print(const char* str) {
|
||||
if(global_app)
|
||||
wifi_marauder_console_output_handle_rx_data_cb((uint8_t*)str, strlen(str), global_app);
|
||||
}
|
||||
|
||||
void loader_port_spi_set_cs(uint32_t level) {
|
||||
UNUSED(level);
|
||||
// unimplemented
|
||||
}
|
||||
|
||||
void wifi_marauder_flash_handle_rx_data_cb(uint8_t* buf, size_t len, void* context) {
|
||||
UNUSED(context);
|
||||
if(flash_rx_stream) {
|
||||
furi_stream_buffer_send(flash_rx_stream, buf, len, 0);
|
||||
} else {
|
||||
// done flashing
|
||||
if(global_app) wifi_marauder_console_output_handle_rx_data_cb(buf, len, global_app);
|
||||
}
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "wifi_marauder_app_i.h"
|
||||
#include "wifi_marauder_uart.h"
|
||||
#define SERIAL_FLASHER_INTERFACE_UART /* TODO why is application.fam not passing this via cdefines */
|
||||
#include "esp_loader_io.h"
|
||||
|
||||
#define ESP_ADDR_BOOT_S3 0x0
|
||||
#define ESP_ADDR_BOOT 0x1000
|
||||
#define ESP_ADDR_PART 0x8000
|
||||
#define ESP_ADDR_NVS 0x9000
|
||||
#define ESP_ADDR_BOOT_APP0 0xE000
|
||||
#define ESP_ADDR_APP 0x10000
|
||||
|
||||
void wifi_marauder_flash_start_thread(WifiMarauderApp* app);
|
||||
void wifi_marauder_flash_stop_thread(WifiMarauderApp* app);
|
||||
void wifi_marauder_flash_handle_rx_data_cb(uint8_t* buf, size_t len, void* context);
|
||||
@@ -119,14 +119,15 @@ static void subghz_scene_reciever_config_set_ext_mod_power_amp_text(VariableItem
|
||||
|
||||
if(index == 1) {
|
||||
furi_hal_gpio_init_simple(&gpio_ext_pc3, GpioModeOutputPushPull);
|
||||
furi_hal_gpio_write(&gpio_ext_pc3, 0);
|
||||
} else {
|
||||
furi_hal_gpio_init_simple(&gpio_ext_pc3, GpioModeAnalog);
|
||||
}
|
||||
|
||||
subghz->last_settings->external_module_power_amp = index == 1;
|
||||
|
||||
// Set globally in furi hal
|
||||
furi_hal_subghz_set_ext_power_amp(subghz->last_settings->external_module_power_amp);
|
||||
|
||||
subghz_last_settings_save(subghz->last_settings);
|
||||
}
|
||||
|
||||
@@ -164,7 +165,7 @@ void subghz_scene_radio_settings_on_enter(void* context) {
|
||||
|
||||
item = variable_item_list_add(
|
||||
variable_item_list,
|
||||
"Ext high power",
|
||||
"Ext Power Amp",
|
||||
EXT_MOD_POWER_AMP_COUNT,
|
||||
subghz_scene_reciever_config_set_ext_mod_power_amp_text,
|
||||
subghz);
|
||||
@@ -174,7 +175,7 @@ void subghz_scene_radio_settings_on_enter(void* context) {
|
||||
|
||||
item = variable_item_list_add(
|
||||
variable_item_list,
|
||||
"Time in names",
|
||||
"Time In Names",
|
||||
TIMESTAMP_NAMES_COUNT,
|
||||
subghz_scene_receiver_config_set_timestamp_file_names,
|
||||
subghz);
|
||||
@@ -185,7 +186,7 @@ void subghz_scene_radio_settings_on_enter(void* context) {
|
||||
if(furi_hal_rtc_is_flag_set(FuriHalRtcFlagDebug)) {
|
||||
item = variable_item_list_add(
|
||||
variable_item_list,
|
||||
"Counter incr.",
|
||||
"Counter Incr.",
|
||||
DEBUG_COUNTER_COUNT,
|
||||
subghz_scene_receiver_config_set_debug_counter,
|
||||
subghz);
|
||||
@@ -214,7 +215,7 @@ void subghz_scene_radio_settings_on_enter(void* context) {
|
||||
} else {
|
||||
item = variable_item_list_add(
|
||||
variable_item_list,
|
||||
"Counter incr.",
|
||||
"Counter Incr.",
|
||||
3,
|
||||
subghz_scene_receiver_config_set_debug_counter,
|
||||
subghz);
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#include <furi.h>
|
||||
#include <furi_hal.h>
|
||||
#include <furi_hal_subghz_i.h>
|
||||
#include <subghz/subghz_last_settings.h>
|
||||
#include <flipper_format/flipper_format_i.h>
|
||||
|
||||
void subghz_extended_freq() {
|
||||
@@ -16,4 +17,14 @@ void subghz_extended_freq() {
|
||||
|
||||
flipper_format_free(file);
|
||||
furi_record_close(RECORD_STORAGE);
|
||||
|
||||
// Load external module power amp setting (TODO: move to other place)
|
||||
// TODO: Disable this when external module is not CC1101 E07
|
||||
SubGhzLastSettings* last_settings = subghz_last_settings_alloc();
|
||||
subghz_last_settings_load(last_settings, 0);
|
||||
|
||||
// Set globally in furi hal
|
||||
furi_hal_subghz_set_ext_power_amp(last_settings->external_module_power_amp);
|
||||
|
||||
subghz_last_settings_free(last_settings);
|
||||
}
|
||||
|
||||
@@ -19,8 +19,8 @@
|
||||
#define SUBGHZ_LAST_SETTING_FIELD_FREQUENCY_ANALYZER_TRIGGER "FATrigger"
|
||||
#define SUBGHZ_LAST_SETTING_FIELD_EXTERNAL_MODULE_ENABLED "External"
|
||||
#define SUBGHZ_LAST_SETTING_FIELD_EXTERNAL_MODULE_POWER "ExtPower"
|
||||
#define SUBGHZ_LAST_SETTING_FIELD_EXTERNAL_MODULE_POWER_AMP "ExtPowerAmp"
|
||||
#define SUBGHZ_LAST_SETTING_FIELD_TIMESTAMP_FILE_NAMES "TimestampNames"
|
||||
#define SUBGHZ_LAST_SETTING_FIELD_EXTERNAL_MODULE_POWER_AMP "ExtPowerAmp"
|
||||
|
||||
SubGhzLastSettings* subghz_last_settings_alloc(void) {
|
||||
SubGhzLastSettings* instance = malloc(sizeof(SubGhzLastSettings));
|
||||
@@ -82,13 +82,13 @@ void subghz_last_settings_load(SubGhzLastSettings* instance, size_t preset_count
|
||||
1);
|
||||
flipper_format_read_bool(
|
||||
fff_data_file,
|
||||
SUBGHZ_LAST_SETTING_FIELD_EXTERNAL_MODULE_POWER_AMP,
|
||||
(bool*)&temp_external_module_power_amp,
|
||||
SUBGHZ_LAST_SETTING_FIELD_TIMESTAMP_FILE_NAMES,
|
||||
(bool*)&temp_timestamp_file_names,
|
||||
1);
|
||||
flipper_format_read_bool(
|
||||
fff_data_file,
|
||||
SUBGHZ_LAST_SETTING_FIELD_TIMESTAMP_FILE_NAMES,
|
||||
(bool*)&temp_timestamp_file_names,
|
||||
SUBGHZ_LAST_SETTING_FIELD_EXTERNAL_MODULE_POWER_AMP,
|
||||
(bool*)&temp_external_module_power_amp,
|
||||
1);
|
||||
|
||||
} else {
|
||||
@@ -103,8 +103,8 @@ void subghz_last_settings_load(SubGhzLastSettings* instance, size_t preset_count
|
||||
SUBGHZ_LAST_SETTING_FREQUENCY_ANALYZER_FEEDBACK_LEVEL;
|
||||
instance->frequency_analyzer_trigger = SUBGHZ_LAST_SETTING_FREQUENCY_ANALYZER_TRIGGER;
|
||||
instance->external_module_enabled = false;
|
||||
instance->external_module_power_amp = false;
|
||||
instance->timestamp_file_names = false;
|
||||
instance->external_module_power_amp = false;
|
||||
|
||||
} else {
|
||||
instance->frequency = temp_frequency;
|
||||
@@ -127,8 +127,12 @@ void subghz_last_settings_load(SubGhzLastSettings* instance, size_t preset_count
|
||||
|
||||
instance->timestamp_file_names = temp_timestamp_file_names;
|
||||
|
||||
// External power amp CC1101
|
||||
instance->external_module_power_amp = temp_external_module_power_amp;
|
||||
|
||||
// Set globally in furi hal
|
||||
furi_hal_subghz_set_ext_power_amp(instance->external_module_power_amp);
|
||||
|
||||
/*/} else {
|
||||
instance->preset = temp_preset;
|
||||
}*/
|
||||
@@ -201,15 +205,15 @@ bool subghz_last_settings_save(SubGhzLastSettings* instance) {
|
||||
}
|
||||
if(!flipper_format_insert_or_update_bool(
|
||||
file,
|
||||
SUBGHZ_LAST_SETTING_FIELD_EXTERNAL_MODULE_POWER_AMP,
|
||||
&instance->external_module_power_amp,
|
||||
SUBGHZ_LAST_SETTING_FIELD_TIMESTAMP_FILE_NAMES,
|
||||
&instance->timestamp_file_names,
|
||||
1)) {
|
||||
break;
|
||||
}
|
||||
if(!flipper_format_insert_or_update_bool(
|
||||
file,
|
||||
SUBGHZ_LAST_SETTING_FIELD_TIMESTAMP_FILE_NAMES,
|
||||
&instance->timestamp_file_names,
|
||||
SUBGHZ_LAST_SETTING_FIELD_EXTERNAL_MODULE_POWER_AMP,
|
||||
&instance->external_module_power_amp,
|
||||
1)) {
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -53,9 +53,8 @@ typedef struct {
|
||||
const GpioPin* async_mirror_pin;
|
||||
|
||||
uint8_t rolling_counter_mult;
|
||||
bool timestamp_file_names : 1;
|
||||
bool ext_power_amp : 1;
|
||||
bool extended_frequency_i : 1;
|
||||
bool external_module_power_amp : 1;
|
||||
} FuriHalSubGhz;
|
||||
|
||||
volatile FuriHalSubGhz furi_hal_subghz = {
|
||||
@@ -63,8 +62,8 @@ volatile FuriHalSubGhz furi_hal_subghz = {
|
||||
.regulation = SubGhzRegulationTxRx,
|
||||
.async_mirror_pin = NULL,
|
||||
.rolling_counter_mult = 1,
|
||||
.ext_power_amp = false,
|
||||
.extended_frequency_i = false,
|
||||
.external_module_power_amp = false,
|
||||
};
|
||||
|
||||
uint8_t furi_hal_subghz_get_rolling_counter_mult(void) {
|
||||
@@ -79,12 +78,12 @@ void furi_hal_subghz_set_extended_frequency(bool state_i) {
|
||||
furi_hal_subghz.extended_frequency_i = state_i;
|
||||
}
|
||||
|
||||
bool furi_hal_subghz_get_ext_power_amp() {
|
||||
return furi_hal_subghz.external_module_power_amp;
|
||||
void furi_hal_subghz_set_ext_power_amp(bool enabled) {
|
||||
furi_hal_subghz.ext_power_amp = enabled;
|
||||
}
|
||||
|
||||
void furi_hal_subghz_set_ext_power_amp(bool enabled) {
|
||||
furi_hal_subghz.external_module_power_amp = enabled;
|
||||
bool furi_hal_subghz_get_ext_power_amp() {
|
||||
return furi_hal_subghz.ext_power_amp;
|
||||
}
|
||||
|
||||
void furi_hal_subghz_set_async_mirror_pin(const GpioPin* pin) {
|
||||
|
||||
@@ -238,16 +238,6 @@ bool furi_hal_subghz_is_async_tx_complete();
|
||||
*/
|
||||
void furi_hal_subghz_stop_async_tx();
|
||||
|
||||
/** Get external amplifier power state
|
||||
* @return true if amplifier is enabled
|
||||
*/
|
||||
bool furi_hal_subghz_get_ext_power_amp();
|
||||
|
||||
/** Set external amplifier power state
|
||||
* @param enabled true to enable amplifier state, false to disable state
|
||||
*/
|
||||
void furi_hal_subghz_set_ext_power_amp(bool enabled);
|
||||
|
||||
// /** Initialize and switch to power save mode Used by internal API-HAL
|
||||
// * initialization routine Can be used to reinitialize device to safe state and
|
||||
// * send it to sleep
|
||||
@@ -292,6 +282,11 @@ void furi_hal_subghz_set_ext_power_amp(bool enabled);
|
||||
// */
|
||||
// void furi_hal_subghz_select_radio_type(SubGhzRadioType state);
|
||||
|
||||
// External CC1101 Ebytes power amplifier control
|
||||
void furi_hal_subghz_set_ext_power_amp(bool enabled);
|
||||
|
||||
bool furi_hal_subghz_get_ext_power_amp();
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -46,11 +46,14 @@ bool subghz_devices_begin(const SubGhzDevice* device) {
|
||||
bool ret = false;
|
||||
furi_assert(device);
|
||||
if(device->interconnect->begin) {
|
||||
ret = device->interconnect->begin();
|
||||
|
||||
if(furi_hal_subghz_get_ext_power_amp()) {
|
||||
furi_hal_gpio_write(&gpio_ext_pc3, 0);
|
||||
// TODO: Remake this check and move this code
|
||||
if(strcmp("cc1101_ext", device->name) == 0) {
|
||||
if(furi_hal_subghz_get_ext_power_amp()) {
|
||||
furi_hal_gpio_init_simple(&gpio_ext_pc3, GpioModeOutputPushPull);
|
||||
}
|
||||
}
|
||||
|
||||
ret = device->interconnect->begin();
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
@@ -58,6 +61,12 @@ bool subghz_devices_begin(const SubGhzDevice* device) {
|
||||
void subghz_devices_end(const SubGhzDevice* device) {
|
||||
furi_assert(device);
|
||||
if(device->interconnect->end) {
|
||||
// TODO: Remake this check and move this code
|
||||
if(strcmp("cc1101_ext", device->name) == 0) {
|
||||
if(furi_hal_subghz_get_ext_power_amp()) {
|
||||
furi_hal_gpio_init_simple(&gpio_ext_pc3, GpioModeAnalog);
|
||||
}
|
||||
}
|
||||
device->interconnect->end();
|
||||
}
|
||||
}
|
||||
@@ -89,8 +98,11 @@ void subghz_devices_idle(const SubGhzDevice* device) {
|
||||
furi_assert(device);
|
||||
if(device->interconnect->idle) {
|
||||
device->interconnect->idle();
|
||||
if(furi_hal_subghz_get_ext_power_amp()) {
|
||||
furi_hal_gpio_write(&gpio_ext_pc3, 0);
|
||||
// TODO: Remake this check and move this code
|
||||
if(strcmp("cc1101_ext", device->name) == 0) {
|
||||
if(furi_hal_subghz_get_ext_power_amp()) {
|
||||
furi_hal_gpio_write(&gpio_ext_pc3, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -145,8 +157,11 @@ bool subghz_devices_set_tx(const SubGhzDevice* device) {
|
||||
if(device->interconnect->set_tx) {
|
||||
ret = device->interconnect->set_tx();
|
||||
|
||||
if(furi_hal_subghz_get_ext_power_amp()) {
|
||||
furi_hal_gpio_write(&gpio_ext_pc3, 1);
|
||||
// TODO: Remake this check and move this code
|
||||
if(strcmp("cc1101_ext", device->name) == 0) {
|
||||
if(furi_hal_subghz_get_ext_power_amp()) {
|
||||
furi_hal_gpio_write(&gpio_ext_pc3, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
@@ -189,8 +204,11 @@ void subghz_devices_set_rx(const SubGhzDevice* device) {
|
||||
if(device->interconnect->set_rx) {
|
||||
device->interconnect->set_rx();
|
||||
|
||||
if(furi_hal_subghz_get_ext_power_amp()) {
|
||||
furi_hal_gpio_write(&gpio_ext_pc3, 0);
|
||||
// TODO: Remake this check and move this code
|
||||
if(strcmp("cc1101_ext", device->name) == 0) {
|
||||
if(furi_hal_subghz_get_ext_power_amp()) {
|
||||
furi_hal_gpio_write(&gpio_ext_pc3, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,11 +38,11 @@ fbtenv_wget()
|
||||
fbtenv_restore_env()
|
||||
{
|
||||
TOOLCHAIN_ARCH_DIR_SED="$(echo "$TOOLCHAIN_ARCH_DIR" | sed 's/\//\\\//g')"
|
||||
PATH="$(echo "$PATH" | /usr/bin/sed "s/$TOOLCHAIN_ARCH_DIR_SED\/python\/bin://g")";
|
||||
PATH="$(echo "$PATH" | /usr/bin/sed "s/$TOOLCHAIN_ARCH_DIR_SED\/bin://g")";
|
||||
PATH="$(echo "$PATH" | /usr/bin/sed "s/$TOOLCHAIN_ARCH_DIR_SED\/protobuf\/bin://g")";
|
||||
PATH="$(echo "$PATH" | /usr/bin/sed "s/$TOOLCHAIN_ARCH_DIR_SED\/openocd\/bin://g")";
|
||||
PATH="$(echo "$PATH" | /usr/bin/sed "s/$TOOLCHAIN_ARCH_DIR_SED\/openssl\/bin://g")";
|
||||
PATH="$(echo "$PATH" | sed "s/$TOOLCHAIN_ARCH_DIR_SED\/python\/bin://g")";
|
||||
PATH="$(echo "$PATH" | sed "s/$TOOLCHAIN_ARCH_DIR_SED\/bin://g")";
|
||||
PATH="$(echo "$PATH" | sed "s/$TOOLCHAIN_ARCH_DIR_SED\/protobuf\/bin://g")";
|
||||
PATH="$(echo "$PATH" | sed "s/$TOOLCHAIN_ARCH_DIR_SED\/openocd\/bin://g")";
|
||||
PATH="$(echo "$PATH" | sed "s/$TOOLCHAIN_ARCH_DIR_SED\/openssl\/bin://g")";
|
||||
if [ -n "${PS1:-""}" ]; then
|
||||
PS1="$(echo "$PS1" | sed 's/\[fbt\]//g')";
|
||||
elif [ -n "${PROMPT:-""}" ]; then
|
||||
@@ -104,8 +104,6 @@ fbtenv_check_if_sourced_multiple_times()
|
||||
return 0;
|
||||
fi
|
||||
fi
|
||||
echo "Warning! FBT environment script was sourced more than once!";
|
||||
echo "You might be doing things wrong, please open a new shell!";
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -170,7 +168,7 @@ fbtenv_get_kernel_type()
|
||||
fbtenv_check_rosetta()
|
||||
{
|
||||
if [ "$ARCH_TYPE" = "arm64" ]; then
|
||||
if ! /usr/bin/pgrep -q oahd; then
|
||||
if ! pgrep -q oahd; then
|
||||
echo "Flipper Zero Toolchain needs Rosetta2 to run under Apple Silicon";
|
||||
echo "Please instal it by typing 'softwareupdate --install-rosetta --agree-to-license'";
|
||||
return 1;
|
||||
@@ -322,7 +320,9 @@ fbtenv_main()
|
||||
fbtenv_restore_env;
|
||||
return 0;
|
||||
fi
|
||||
fbtenv_check_if_sourced_multiple_times;
|
||||
if ! fbtenv_check_if_sourced_multiple_times; then
|
||||
return 0;
|
||||
fi;
|
||||
fbtenv_check_env_vars || return 1;
|
||||
fbtenv_check_download_toolchain || return 1;
|
||||
fbtenv_set_shell_prompt;
|
||||
|
||||
Reference in New Issue
Block a user