-
Notifications
You must be signed in to change notification settings - Fork 0
Feat/add uart support #25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
👋 Hello Dzarda7, we appreciate your contribution to this project! Click to see more instructions ...
Review and merge process you can expect ...
|
c082f6b to
e6fdc45
Compare
|
@dobairoland @radimkarnis @jedi7 This is working implementation for UART. I do not expect many changes in the API on esp-stub-lib side, but it waits for merging as some things needs to be decided. But this can be used for testing. Also review can be done I believe. |
| if ((intr_flags & UART_INTR_RXFIFO_FULL) || (intr_flags & UART_INTR_RXFIFO_TOUT)) { | ||
| uint32_t count = stub_target_uart_get_rxfifo_count(UART_NUM_0); | ||
|
|
||
| for (uint32_t i = 0; i < count; i++) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: ++i
|
|
||
| for (uint32_t i = 0; i < count; i++) { | ||
| uint8_t byte = stub_target_uart_read_rxfifo_byte(UART_NUM_0); | ||
| stub_lib_uart_tx_one_char(byte); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is little confusing, we are doing echo? or what is the purpose?
| slip_recv_byte(byte); | ||
|
|
||
| if (slip_is_frame_complete() || slip_is_frame_error()) { | ||
| break; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it will be nice to have a comment like:
"cannot receive next frame till the current frame is processed"
also there should be mechanism which will prevent receiving next frame (blocking interrupt?) or allow to cache next frame.
| } | ||
|
|
||
| if (slip_is_frame_error()) { | ||
| slip_recv_reset(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we send an error to user?
|
|
||
| // If frame is already complete or has error, ignore new bytes until reset | ||
| if (s_recv_ctx.frame_complete || s_recv_ctx.frame_error) { | ||
| return; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this can lead to frame errors at higher speeds. we should buffer that.
| // Add escaped byte to buffer | ||
| if (s_recv_ctx.frame_length < s_recv_ctx.buffer_size) { | ||
| s_recv_ctx.buffer[s_recv_ctx.frame_length] = byte; | ||
| s_recv_ctx.frame_length++; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: ++s_recv_ctx.frame_length
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR implements UART reception and SLIP protocol command processing for ESP stub firmware, enabling the stub to receive and decode commands from the host. Key changes include implementing the SLIP decoder state machine with buffer management, adding UART interrupt handling, and fixing BSS initialization and checksum handling issues.
Key changes:
- Implements complete SLIP frame reception with state machine, error handling, and buffer management
- Adds UART interrupt handler to receive bytes and process them through SLIP decoder
- Fixes command handler to account for checksum field in SLIP header and replaces inline register functions with macros
Reviewed Changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| src/slip.h | Adds function declarations for frame reception status checking and data retrieval |
| src/slip.c | Implements SLIP decoder state machine with buffering, error detection, and frame completion tracking |
| src/main.c | Adds BSS initialization, UART setup with interrupt handler, and main loop for processing received frames |
| src/command_handler.c | Replaces inline register access functions with macros and accounts for checksum in SLIP header parsing |
| src/CMakeLists.txt | Adds command_handler.c to build sources |
| esp-stub-lib | Updates submodule to version with UART support |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
|
||
| for (uint32_t i = 0; i < count; i++) { | ||
| uint8_t byte = stub_target_uart_read_rxfifo_byte(UART_NUM_0); | ||
| stub_lib_uart_tx_one_char(byte); |
Copilot
AI
Nov 19, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line echoes every received byte back to UART, which appears to be debug code left in by mistake. In production, the stub should not echo received bytes as this will interfere with the SLIP protocol communication between the host and stub.
| stub_lib_uart_tx_one_char(byte); |
| s_recv_ctx.frame_error = true; | ||
| state = STATE_NO_FRAME; | ||
| } | ||
| break; |
Copilot
AI
Nov 19, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The STATE_ESCAPING case has duplicate buffer overflow handling logic that's identical to the STATE_IN_FRAME case (lines 117-124). Consider extracting this into a helper function to reduce duplication and improve maintainability.
|
|
||
| void slip_recv_byte(uint8_t byte) | ||
| { | ||
| static slip_state_t state = STATE_NO_FRAME; |
Copilot
AI
Nov 19, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The static state variable is not reset when slip_recv_reset() is called, which could leave the decoder in an inconsistent state. If a frame error occurs mid-escape sequence (STATE_ESCAPING), the state will remain in that state even after reset, causing the next frame to be decoded incorrectly. Consider resetting state to STATE_NO_FRAME in slip_recv_reset().
|
LGTM in general. Thanks! |
radimkarnis
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice job, this LGTM, thank you!
e6fdc45 to
1ef9839
Compare
This commit also starts using read and write reg macros from esp-stub-lib
1ef9839 to
aea8a71
Compare
Description
This PR adds support for UART reception and processing of SLIP command. It was tested and works on the following targets:
esp32 v3.1
esp32c2 v2.0 (v1.0 does not work!)
esp32c3 v1.0
esp32c6 v0.1
esp32h2 v0.1
esp32s3 v0.2
esp32s2 v1.0
esp32c5 v1.0 and v1.1
esp32p4 v1.2 (need new target for >=v3.0)
Along the way, other stuff was fixed and resolved to enable this like not linking standard libraries which caused build with warnings for newer targets. Erase of bss section and account for checksum in SLIP header.
Related
Related esp-stub-lib PR, not merged yet, needs some modifications: espressif/esp-stub-lib#30
Testing
Tested on mentioned targets using
esptool flash-idcommand. Works, some targets fail because flash chip is not attached, but stub responds correctly.Checklist
Before submitting a Pull Request, please ensure the following: