keith-packard / snek

Snek programming language for tiny systems
GNU General Public License v3.0
292 stars 30 forks source link

ports/lilypad328: LilyPad Arduino 328 target #46

Closed douglasfraser closed 2 years ago

douglasfraser commented 2 years ago

Adding LilyPad Arduino 328 as additional "port" based on Uno.

I'm running LilyPad off 3V and 8MHz so I built bootloader and Snek to talk at only 38400 bps.

Creating PR as draft so a. You can give feedback on using Uno approach etc. b. I can continue to do some testing, including serial at 115.2k.

keith-packard commented 2 years ago

This looks great! I've been using 115200 baud for everything so that mu-editor and snekde can both avoid model-specific baud rate settings. As we can't reliably detect the device model, that's a nice thing to avoid. Have you tried either of these IDEs to see if the new synchronous serial protocol (that uses ENQ/ACK for flow control)?

douglasfraser commented 2 years ago

Thanks for the fast feedback you gave on this originally. I've not had the time to complete testing yet. Closing to keep things tidy. Hope to return with new PR and correct story on 115200 on LilyPad at some point in the future.

Many thanks for your awesome work!

keith-packard commented 2 years ago

I got a lilypad and gave this a try -- because the lilypad runs on 3.3V, it uses an 8MHz clock instead of the 16MHz clock found on 5V arduino devices. Getting snek built for that was easy enough, but I discovered that the slower clock rate doesn't allow for a serial data rate close enough to the 115200 target to work reliably -- you get to pick between 111k and 125k. In contrast, the 16MHz clock allows you to pick a 117k rate, which seems to be close enough to work. I think this means we either provide a way to select baud rates in all of the UIs, or move all of the devices running over a serial link to 57600 baud.

keith-packard commented 2 years ago

I think the best thing we can do is just plan on supporting both 115200 and 57600 baud in the IDEs, defaulting to 115200 and letting the user select 57600 if necessary. I've done that in snekde and pushed the 'lilypad' branch with those changes and the port files for the lilypad, but none of the documentation changes. Do you think this should be the lilypad328 port or just the plain lilypad port? I know there's a 32u4 lilypad as well; that seems to be called the lilypad USB though?

keith-packard commented 2 years ago

I decided to try auto-detecting the baud rate and that seems to work very well. When snek receives a ^T, it sends a ^T back, so the IDE can see if it gets ^T back from sending ^T for each baud rate. snekde and mu-editor both work with lilypad this way.

douglasfraser commented 2 years ago

Interesting development. I have to admit I was doing all my testing with GNU screen and suspect I simply made an error with my last build that left the baud set at 115200.

Coming back to this I think the build config for these small devices becomes rather opinionated and at the same time inflexible. I originally choose 38400 baud as documentation suggested it would be the most reliable option and accepted adapting my setup to accommodate. You have made a slightly different set of choices, for example also supporting buzzer which I don't have. When you take into account the "big" build without bootloader space it seems like reducing to a simple "port" name no longer works to convey what is going on.

I'm probably not directly your target audience when it comes to the port build options because I'm fairly okay poking around to the get the config I want.

When I get some time to play around again I was going see if I could get my kid using https://github.com/BIPES/BIPES rather than me being intermediate Snek developer. Current use case is Lilypad as controller for LEDs on clothing. Perhaps this hints to small device builds becoming role based like motor controller, sound maker or LED controllers? Allowing appropriate feature subset to be packed in.

Regardless I think it's awesome that I can basically write python interactively on a device the size of the ATMega328.

keith-packard commented 2 years ago

I hadn't realized that bipes added snek as a possible backend; that's pretty cool. I'll have to give that a try at some point. And, yes, because of the small size of the ROM, we end up needing to pick-and-choose which pieces to include in these smaller devices. Makes me wonder if we need some kind of tool to generate a custom snek image that contains the necessary drivers for any particular project. That seems rather more work than it's probably worth, given that you can get 'arduino' scale devices with significantly larger processors that could simply include code for all of the drivers.

For LilyPad, I thought it would be useful to support as many of the associated components as possible, which is why I added support for the beeper. As for the baud rate, I picked 57600 because that's the rate used by the boot loader, and as it's half 115200, I figured it would be reliable enough as that ends up using the same divider as 115200 does on 16MHz devices.

In any case, I think the lilypad 'port' is probably useful enough to include; it can definitely control lights and tones in a wearable project, and that seems to be its design space.

keith-packard commented 2 years ago

I've merged a LilyPad port to main.