Is there any example code for manipulating the registers on the nRF52?

Discussion and questions about boards that can run MicroPython but don't have a dedicated forum.
Target audience: Everyone interested in running MicroPython on other hardware.
Post Reply
User avatar
WhiteHare
Posts: 129
Joined: Thu Oct 04, 2018 4:00 am

Is there any example code for manipulating the registers on the nRF52?

Post by WhiteHare » Thu Oct 04, 2018 4:07 am

Simply put, I'd like to use Nordic's so-called "proprietary" radio modes (i.e. not bluetooth per se), and unless there's already a micropython library which supports that (?), then I will need to manipulate the registers directly to send and receive packets. I've done this successfully using C, but I'm completely new to micropython and therefore unsure whether micropython will allow me access the nRF52 hardware directly, and, if so, how to go about it.

I posted this question on github, and one person there suggested I post it here.

Please advise.

User avatar
pythoncoder
Posts: 3126
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Is there any example code for manipulating the registers on the nRF52?

Post by pythoncoder » Thu Oct 04, 2018 4:54 am

I can only answer generally as I have no experience of the Nordic chips. On STM micros (e.g. the Pyboard) registers are memory mapped. There is an stm module which provides a set of constants providing convenient names for the relevant addresses. So you can write code like

Code: Select all

stm.mem32[stm.RCC + stm.RCC_APB1ENR] |= 0x10000000
return stm.mem32[self.BKPSRAM + idx * 4]
You can evidently write your own set of mnemonics for the registers you want to use. But the question is whether your port has an equivalent to stm.mem32, and I don't know the answer to that.

There are other ways of accessing memory directly: see uctypes.addressof(), uctypes.bytearray_at() and so on. If the chip supports the Arm Thumb instruction set your port may even provide access to the inline assembler.
Peter Hinch

jickster
Posts: 546
Joined: Thu Sep 07, 2017 8:57 pm

Re: Is there any example code for manipulating the registers on the nRF52?

Post by jickster » Thu Oct 04, 2018 3:07 pm

You can write assembly code in Python to manipulate registers.


Sent from my iPhone using Tapatalk Pro

User avatar
WhiteHare
Posts: 129
Joined: Thu Oct 04, 2018 4:00 am

Re: Is there any example code for manipulating the registers on the nRF52?

Post by WhiteHare » Thu Oct 04, 2018 4:58 pm

I think pythoncoder is probably right. I'll give it a try if I can ever get the .hex file created (I'm having make problems with nrfjprog. I posted a question about it, but I'm having to wait for moderation.)

User avatar
dhylands
Posts: 2908
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Is there any example code for manipulating the registers on the nRF52?

Post by dhylands » Sat Oct 06, 2018 3:30 am

If you look at modstm.c: https://github.com/micropython/micropyt ... 2/modstm.c you'll see that the actual machinery for accessing the memory is generic and is implemented in this file:
https://github.com/micropython/micropyt ... hine_mem.c

The things that are specific to the stm32 are all of the actual constants associated with the register names.

User avatar
WhiteHare
Posts: 129
Joined: Thu Oct 04, 2018 4:00 am

Re: Is there any example code for manipulating the registers on the nRF52?

Post by WhiteHare » Fri Oct 12, 2018 6:28 pm

Just to close the loop for anyone reading this in the future: the solution is to use machine.mem32, as described here: viewtopic.php?f=12&t=5377

:D

Post Reply