Adafruit Industries, Essential service and business: NYC – Executive Order 202.6 - Read more.
0

How to enable @micropython.viper
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

How to enable @micropython.viper

by qbbenub on Sun Mar 22, 2020 9:36 pm

Hello, first post here! I'm using a Feather nRF52840 express to develop a (non-usb) MIDI controller, and have run into an issue with setting the baud rate with busio.UART. It turns out that the MIDI standard baud rate is 31250, which is not supported. The only workaround, as far as I can tell, is to write the baud rate register directly. I have done this in C, and it works, so the question is, how to do this in circuitpython? Googling around, it appears that I should be able to use the @micropython.viper decorator to define (and later call) a function using the ptr32 type to write the baud rate to the desired location, as in:
Code: Select all | TOGGLE FULL SIZE
@micropython.viper
def set_midi_baud_rate():
    ptr32(0x40002524)[0] = 0x00800000

The problem is, the @micropython.viper decorator does not seem to be supported in the the 5.0.0 build I'm using, at least not for my board. Or maybe I'm missing something basic? I'm a circuitpython noob so that's probably the case. Any help here is appreciated. I'm not averse to building the circuitpython code base, but I'm not sure where to dig in.

Thanks,
Bill
qbbenub
 
Posts: 3
Joined: Sat Nov 30, 2013 4:40 pm

Re: How to enable @micropython.viper

by tannewt on Mon Mar 23, 2020 4:17 pm

Hi Bill,
Boards can enable Viper as described here: https://github.com/adafruit/circuitpyth ... -551950497

The guide for building CircuitPython is here: https://learn.adafruit.com/building-circuitpython/

I'd rather not enable Viper and other decorators by default. However, it'd be great to support that baudrate. The exist code for baudrates is here: https://github.com/adafruit/circuitpyth ... UART.c#L66
Thanks,
Scott

tannewt
 
Posts: 1536
Joined: Thu Oct 06, 2016 8:48 pm

Re: How to enable @micropython.viper

by qbbenub on Mon Mar 23, 2020 4:42 pm

Thanks Scott, I'm in the middle of building the code right now, so your response is timely :)
I'll be going the decorator route; Unfortunately, there's no simple way to add the 31250 baud rate by updating UART.c. Just adding it to the map won't work -- it's a hack specific the nRF52 boards
- Bill
qbbenub
 
Posts: 3
Joined: Sat Nov 30, 2013 4:40 pm

Re: How to enable @micropython.viper

by tannewt on Mon Mar 23, 2020 4:59 pm

The code I linked to is for nRF52840 only at the moment so feel free to hack it in there. :-)

tannewt
 
Posts: 1536
Joined: Thu Oct 06, 2016 8:48 pm

Re: How to enable @micropython.viper

by qbbenub on Mon Mar 23, 2020 9:02 pm

I was wrong! It was super easy to add the 31250 baudrate option. I just added the line
Code: Select all | TOGGLE FULL SIZE
{ 31250, NRF_UARTE_BAUDRATE_31250 },


to the baudrate_map initialization at L65 in circuitpython/ports/nrf/common-hal/busio/UART.c. It turns out that the actual 32-bit word that the nRF52 boards use for this baud rate is defined already, along with all the others, in the board-specific nrfx files, eg:
Code: Select all | TOGGLE FULL SIZE
nrfx/mdk/nrf52840_bitfields.h:#define UARTE_BAUDRATE_BAUDRATE_Baud31250 (0x00800000UL) /*!< 31250 baud */


Do you want me to do a pull request for this mod, or do you want to do it yourselves?

Also, just for posterity, enabling viper was super easy as well, since all you need is to define a compile-time constant. Just cd to the build dir for your board, and add the constant to the build -- no need to edit any files. In my case it looks like this:
Code: Select all | TOGGLE FULL SIZE
 cd circuitpython/ports/nrf
  make V=1 BOARD=feather_nrf52840_express CIRCUITPY_ENABLE_MPY_NATIVE=1


Cheers,
Bill
qbbenub
 
Posts: 3
Joined: Sat Nov 30, 2013 4:40 pm

Re: How to enable @micropython.viper

by tannewt on Mon Mar 23, 2020 10:31 pm

A PR would be great! Please include any other baud rates we missed. Thanks!

tannewt
 
Posts: 1536
Joined: Thu Oct 06, 2016 8:48 pm

Please be positive and constructive with your questions and comments.


cron