Page 1 of 1

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

Posted: Thu Oct 04, 2018 4:07 am
by WhiteHare
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.

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

Posted: Thu Oct 04, 2018 4:54 am
by pythoncoder
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.

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

Posted: Thu Oct 04, 2018 3:07 pm
by jickster
You can write assembly code in Python to manipulate registers.


Sent from my iPhone using Tapatalk Pro

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

Posted: Thu Oct 04, 2018 4:58 pm
by WhiteHare
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.)

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

Posted: Sat Oct 06, 2018 3:30 am
by dhylands
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.

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

Posted: Fri Oct 12, 2018 6:28 pm
by WhiteHare
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