Skip to content

Conversation

@jaguilar
Copy link
Contributor

btstack may send us buffers longer than 256 bytes to pass to the uart.

It should be relatively low-cost to increase the width of the length
argument of the read and write buffers to accommodate this.

I haven't really tested this much at all. I doubt we'll see anything with current
use cases since presumably all callsites are currently using uint8_t
as the length argument. When the btstack uart block for ev3 is done,
we may be able to exercise this a little better.

I did add some gcc pragmas in a working copy to look for conversion mistakes
in the uart implementation files. I didn't find anything in the new code.

@coveralls
Copy link

coveralls commented Oct 21, 2025

Coverage Status

coverage: 59.78%. remained the same
when pulling f039d01 on jaguilar:uart-buffer-length
into 8e71552 on pybricks:master.

@laurensvalk
Copy link
Member

Would it make sense to save this one for when we're closer to the end goal here? We can keep it open for now.

@jaguilar
Copy link
Contributor Author

I'm not opposed to saving it until we're closer. That being said, the very next PR (the btstack uart block impl for ev3) uses this change.

Now, I am not sure on what timing basis y'all want to receive the PRs. Should I wait until I have something demonstrably working on an end-to-end basis before I send the pull requests for prerequisites?

@dlech
Copy link
Member

dlech commented Oct 21, 2025

I don't see a reason to delay the change. Also, I would suggest trying it with uin32_t instead to see if it reduces the firmware size (at the expense of a few bytes of RAM). On ARM, using anything smaller than 32-bit requires an extra instruction every time you read or write the field, so there is a tradeoff between instructions (firmware size) and RAM usage.

@jaguilar
Copy link
Contributor Author

Okay, I'll try that. I thought about it but I wasn't sure if stack space or code space was more precious. uint32_t should also be slightly faster as it avoids any extension and truncation steps on the way to and from the ALU.

@jaguilar
Copy link
Contributor Author

Adjusting the types to uint32 increased the firmware size by eight bytes. Perhaps this causes more conversions somewhere else. I decided not to change it unless there is an explicit ask to do so regardless of the firmware size. :)

@dlech
Copy link
Member

dlech commented Oct 23, 2025

I'm showing on movehub (the one that matters the most) that it actually saves 20 bytes in .text at the expense of 24 bytes in .bss. This is kind of withing the "noise" level of random small changes though, so not really compelling.

Before (uint16_t)

section             size        addr
.text             105360   134238208
.magic               260   536870912
.data                224   536871172
.bss                5408   536871400
.noinit             7176   536876808
.stack              3072   536883984
.name                 16   134343792
.user                  4   134343808
.checksum              4   134343812
.ARM.attributes       45           0
.comment              70           0
.debug_line_str      210           0
.debug_frame         116           0
Total             121965

after (uint32_t):

section             size        addr
.text             105340   134238208
.magic               260   536870912
.data                224   536871172
.bss                5432   536871400
.noinit             7176   536876832
.stack              3072   536884008
.name                 16   134343772
.user                  4   134343788
.checksum              4   134343792
.ARM.attributes       45           0
.comment              70           0
.debug_line_str      210           0
.debug_frame         116           0
Total             121969

So not really enough to say that one is better than the other. But uint32_t seems more natural to me if there isn't a strong reason to do something else.

@jaguilar
Copy link
Contributor Author

jaguilar commented Oct 23, 2025 via email

@jaguilar jaguilar force-pushed the uart-buffer-length branch 2 times, most recently from 996da6b to 960a806 Compare October 25, 2025 16:58
@jaguilar jaguilar changed the title pbdrv/uart: Use uint16_t for read/write length pbdrv/uart: Use uint32_t for read/write length Oct 25, 2025
btstack may send us buffers longer than 256 bytes
to pass to the uart. It should be relatively low-cost
to increase the length of the read and write buffers
to accomodate this.
@dlech dlech merged commit cff1933 into pybricks:master Oct 25, 2025
17 checks passed
@dlech
Copy link
Member

dlech commented Oct 25, 2025

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants