-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
SoftUART module fixes and code simplification #3104
Conversation
Please rebase on the new |
- Simplify code by using lua_L* functions and using userdata properly - Fix some edge-cases - Add more examples to documentation
dcc30bb
to
56bac4e
Compare
@nwf I rebased my branch - now it should be fine. It's still rough draft because I'm sure that at least Lua API part of code could be simplified even more. |
@galjonsfigur, @vsky279 , I apologize for only getting to test this now. The feedback is as follows: The previous test method detailed in #2996 was used:
Test observations:
|
@NicolSpies Thanks for extensive tests! // Wait till start bit is half over so we can sample the next one in the center
if (elapsed_time < s->bit_time / 2) {
uint16_t wait_time = s->bit_time / 2 - elapsed_time;
while ((uint32_t)(asm_ccount() - start_time) < wait_time);
start_time += wait_time;
} to // Wait till start bit is half over so we can sample the next one in the center
uint16_t wait_time = (s->bit_time / 2) - elapsed_time;
while ((uint32_t)(asm_ccount() - start_time) < wait_time);
start_time += wait_time;
and report results? I will try to rewrite this ISR because now it has problems both in very low and very high baud rates and work on it in the near future but for now I don't have equipment at hand. |
@galjonsfigur, the swapped code fragment displays the same problem as before where the frame start bit is taken as the LSB of the data received in every second frame received at a baud rate of 115200. Like before at slower baud rates it does not occur. The insertion of the empty frame before the received data still happens as described before at a baud rate of 57600. At slower baud rates the effect is bigger with 2 or 3 empty frames inserted in front of the received data. |
@NicolSpies Thanks for the feedback! |
I can't get consistent testing environment for the RX module at the moment, but I thought I can be a good idea to make ISR a little smaller by moving part of the code to another function that gets scheduled by ISR with medium priority instead of low. Also now |
@galjonsfigur, 👍 , I will test tomorrow. |
@galjonsfigur , the latest change breaks the data received completely. The data is still transmitted correctly. I propose rolling back to the code before the latest changes but including the de-registration functionality. That would would enable me to run automated tests to verify the issues observed over many tests. |
I made a rollback of the ISR code - it should be just as before. It's really tricky to get this right - I will be looking for a better way to get correct timings and to get the ISR as small as possible. |
@galjonsfigur , I have tested and have feedback maybe we need to do the feedback offline not to clutter the list. My email is in my github profile. |
@galjonsfigur , the rollback performance is as described before with some improvement allowing the module to be also usable when The blocking problem is a memory leak. The softuart instance is not de-registering preventing the GC to free the instance memory. |
@NicolSpies 👍 Thanks for checking!
What do you mean exactly? Now because |
@galjonsfigur , By using
I have identified an extra function in registry on every iteration of my function using the softuart module. |
app/modules/softuart.c
Outdated
} | ||
// Register callback | ||
softuart_rx_cb_ref[softuart->pin_rx] = luaL_ref(L, LUA_REGISTRYINDEX); |
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 if() { unref } ref
dance can be simplified to @TerryE's new luaL_reref
function:
luaL_reref(L, LUA_REGISTRYINDEX, &softuart_rx_cb_ref[softuart->pin_rx])
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.
@nwf 👍 Thanks for looking into it - I added this change and unref2
macro in the newest commit.
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.
@galjonsfigur , I apologize, no leak in the module but in my code, a local timer for some reason does not de-register, a simple tmr=nil
did the trick. Thanks for helping.
@galjonsfigur, update regarding testing of the empty frame issue reported. The problem is not present, no empty frames, when changing only the ESP8266 module and keeping everything else consistent. |
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.
Some very minor nits, but I'm not in a position to test this.
volatile softuart_buffer_t buffer; | ||
uint16_t bit_time; | ||
uint16_t need_len; // Buffer length needed to run callback function | ||
char end_char; // Used to run callback if last char in buffer will be the same | ||
uint8_t armed; | ||
uint8_t pin_rx; | ||
uint8_t pin_tx; |
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.
Why were these moved? Does it reduce padding within the structure?
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 was the intent - I'm not sure if this would help, but having the buffer as a first element of the struct should make access to it a bit easier and maybe instruction or two faster - but I haven't tested it.
@NicolSpies This is very interesting - this would mean that some ESP modules have greater interrupt latency than the others. I'm not really sure what I could do about it - I will look into some other ISR tricks that other authors have been using to make bit-banged transmission more reliable, but I didn't expect that it would differ so much depending on the hardware. Will look into that - but maybe not in this PR. |
@galjonsfigur, since posting I have confirmed it on 3 units, from the same production batch. Another possibility is that the original unit used for testing is suspect. |
As this has gone through many iterations and heavy testing it should now be ready for prime -> merging |
* SoftUART fixes: - Simplify code by using lua_L* functions and using userdata properly - Fix some edge-cases - Add more examples to documentation * Don't de-register interrupt hook if there is more RX instances * More bug fixes and registering simplification with luaL_reref and unref2 * Correct documentation of SoftUART module
dev
branch rather than formaster
.docs/*
.This is a response to @TerryE comments about C Module coding standards(#1028). Just by using
luaL_argcheck
code became easier to read. It's still not the best possible implementation of Lua interface as I'm still trying to wrap my head around Lua C API and all available functions and macros.Changes include: fixing multiple SoftUART rx instances, checking if interrupt was attached successfully and a bit more examples and fixes in documentation. The rest is just code simplification with use of @TerryE tips.
It's still a draft, as I would want to simplify it even more. Any feedback welcome.