Skip to content
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

STM32: UART support #2211

Merged
merged 27 commits into from Nov 12, 2019
Merged

STM32: UART support #2211

merged 27 commits into from Nov 12, 2019

Conversation

hierophect
Copy link
Collaborator

@hierophect hierophect commented Oct 11, 2019

This PR adds UART support to the STM32 series of development boards. As required by boards such as the GPS Featherwing, the current implementation allows for unexpected reception of data via a ring buffer. Transmits, however, are blocking. Since the UART HAL does not allow for passing context pointers, UART objects are tracked in global state.

This iteration is currently a bit hacked together. I'd probably be a good idea to revisit the module in the future to remove unnecessary HAL features and streamline the code for improved speed/maintainability.

@dhalbert
Copy link
Collaborator

Maybe look at the STM32 Arduino core for inspiration? https://github.com/stm32duino/Arduino_Core_STM32/blob/master/cores/arduino/stm32/uart.c et al

@dhalbert
Copy link
Collaborator

Or the MicroPython STM32 port?

@hierophect
Copy link
Collaborator Author

@dhalbert I'll see what I can dig out of micropython. It has a very different interface but maybe I can glean their strategy on the interrupts.

@hierophect hierophect marked this pull request as ready for review October 16, 2019 16:00
Copy link
Member

@tannewt tannewt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here are some initial comments. I'm headed out now for the day but will look at the rest tonight.

ports/stm32f4/common-hal/busio/UART.c Outdated Show resolved Hide resolved
ports/stm32f4/common-hal/busio/UART.c Show resolved Hide resolved
ports/stm32f4/common-hal/busio/UART.c Outdated Show resolved Hide resolved
ports/stm32f4/common-hal/busio/UART.c Outdated Show resolved Hide resolved
ports/stm32f4/mpconfigport.h Outdated Show resolved Hide resolved
@hierophect
Copy link
Collaborator Author

firmware.bin.zip
bin for @ladyada

Copy link
Member

@tannewt tannewt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please double check that this PR doesn't change the version of TinyUSB. The Spresense build is failing because tinyusb is an older version that what is checked in.

I don't know why it's crashing after soft reset. The backtrace of the crash should provide info to help hunt it down. The busio_uart_obj_t data will be left alone until the next vm run where it may be allocated for something else and cleared. I'd suggest clearing the pointer array in uart_reset as well to make sure other memory isn't accidentally referenced on a subsequent run.

ports/stm32f4/common-hal/busio/UART.c Show resolved Hide resolved
ports/stm32f4/mpconfigport.h Outdated Show resolved Hide resolved
@@ -40,6 +40,7 @@
#include "py/circuitpy_mpconfig.h"

#define MICROPY_PORT_ROOT_POINTERS \
void *cpy_uart_obj_all[6]; \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Want to use MAX_UART here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above. I could just have this definition in every mpconfigboard.h file if that's better, though.

@hierophect
Copy link
Collaborator Author

@tannewt on the subject of tinyusb, I often find I have a hard time keeping it synced when merging in upstream/master. It only every happens with tinyusb, too. There are no messages or conflicts or anything to signify that the wrong version is taking priority during the merge.

Do you have any idea what that might be?

@tannewt
Copy link
Member

tannewt commented Oct 17, 2019

@hierophect Merging and changing branches doesn't automatically update submodules. After merging and before committing I recommend running git status to make sure everything is as you expect. After merging you'll want to git submodule update --init --recursive . to make sure you have the correct submodule.

@tannewt
Copy link
Member

tannewt commented Oct 17, 2019

To get TinyUSB back to the checked in version I recommend looking on github for the hash and then locally change to lib/tinyusb and check it out. The commit change should show on git status and then you can commit it and push.

@dhalbert
Copy link
Collaborator

To get TinyUSB back to the checked in version I recommend looking on github for the hash and then locally change to lib/tinyusb and check it out. The commit change should show on git status and then you can commit it and push.

Yes, I usually do:

$ cd lib/tinyusb
$ git checkout master
$ git pull
$ git log
# check that the commit matches the commit in adafruit/circuitpython
# and checkout the wanted commit or just leave as is if it's already correct.

@hierophect
Copy link
Collaborator Author

@ladyada Do you think you could give this latest binary a run? I'm trying to get a better handle on what's wrong with it, which is proving tricky without better python experience. It's structurally very similar to stm32duino, but it just doesn't seem to be delivering on the basics these libraries need from it.

@hierophect
Copy link
Collaborator Author

bin if git CI is still down
firmware.bin.zip

@dhalbert dhalbert added this to the 5.0.0 milestone Oct 31, 2019
@hierophect
Copy link
Collaborator Author

@ladyada New bin, should have fixed all crashes and buffer errors.
firmware.bin.zip

@ladyada
Copy link
Member

ladyada commented Nov 1, 2019

yay verified this works a lot better, this is ready to merge/release to folks. open an issue to add dma/non-blocking writes and we can get feedback ask well

ladyada
ladyada previously approved these changes Nov 1, 2019
Copy link
Member

@ladyada ladyada left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tested functionality on STM32F405

Copy link
Member

@tannewt tannewt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good job getting this going using the HAL. I think it'd be simpler to not use the HAL for the RX interrupt but don't want to block if others disagree. The main thing will be to make sure the interrupt bits are cleared after you handle it. (The peripheral interrupt bits get merged into the interrupt line to the CPU.)

ports/stm32f4/common-hal/busio/UART.c Outdated Show resolved Hide resolved
ports/stm32f4/common-hal/busio/UART.c Show resolved Hide resolved
@hierophect
Copy link
Collaborator Author

hierophect commented Nov 2, 2019

Hey @tannewt, my current approach is inspired by stm32duino, which is completely HAL based. I opted to follow the way they did things as I felt it'd make stuff more readable to contributors who are familiar with that system. It's also a little closer to NRF, and avoids exposing all the parity and wordlength management in UART_Receive_IT in the uart.c file. But I can revisit a custom register approach if you feel strongly that it's more maintainable/appropriate.

Copy link
Collaborator

@dhalbert dhalbert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some q's and a few things to fix.

ports/stm32f4/common-hal/busio/UART.c Show resolved Hide resolved
ports/stm32f4/common-hal/busio/UART.c Outdated Show resolved Hide resolved
ports/stm32f4/common-hal/busio/UART.c Outdated Show resolved Hide resolved
ports/stm32f4/common-hal/busio/UART.c Outdated Show resolved Hide resolved
ports/stm32f4/common-hal/busio/UART.c Show resolved Hide resolved
ports/stm32f4/common-hal/busio/UART.c Outdated Show resolved Hide resolved
ports/stm32f4/common-hal/busio/UART.c Outdated Show resolved Hide resolved
ports/stm32f4/supervisor/port.c Outdated Show resolved Hide resolved
ports/stm32f4/common-hal/busio/UART.c Show resolved Hide resolved
ports/stm32f4/tick.c Outdated Show resolved Hide resolved
Copy link
Member

@tannewt tannewt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have one non-blocking comment. Will change review to comment so it can be merged after approval from @dhalbert

Copy link
Collaborator

@dhalbert dhalbert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, let's go with this now! I agree with @tannewt that there is a chance you may lose characters due to disabling/enabling, but we can revisit that after some user testing.

@dhalbert dhalbert merged commit 356aa2e into adafruit:master Nov 12, 2019
@hierophect hierophect added the stm label Nov 13, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants