Hi,
I am using an Metro M0 with circuitPython loaded onto it.
in Arduino IDE I can perform direct port manupulation as illustrated on this thread:
viewtopic.php?f=57&t=102695&p=514098&hi ... on#p514098
Question:
Is it possible to perform same direct port manipulation using circuitPython Script?
Blink for Metro M0 using direct register manipulation
Moderators: adafruit_support_bill, adafruit
Please be positive and constructive with your questions and comments.
- MetroM0
- Posts: 8
- Joined: Fri Jul 29, 2022 2:41 am
- neradoc
- Posts: 542
- Joined: Wed Apr 27, 2016 2:38 pm
Re: Blink for Metro M0 using direct register manipulation
No, there is no direct port manipulation API. That seems really outside of the scope of Circuitpython.
Is there a specific use case you are thinking of that can't be achieve with CP ?
We are always interested in learning about those.
Is there a specific use case you are thinking of that can't be achieve with CP ?
We are always interested in learning about those.
- MetroM0
- Posts: 8
- Joined: Fri Jul 29, 2022 2:41 am
Re: Blink for Metro M0 using direct register manipulation
That's a pity...neradoc wrote:No, there is no direct port manipulation API. That seems really outside of the scope of Circuitpython.
Is there a specific use case you are thinking of that can't be achieve with CP ?
We are always interested in learning about those.
I fail to understand how this is 'out of scope' since this can be achived within arduino IDE. I have used direct port manipulation in the past for parallel read/writes and I'm truly surprised that this feature is not available if I were to work with CircuitPython (still beginner with it btw!)
no particular project in mind at the moment but comparing the Metro with the Metro express, it strikes me that 2 pins are not used, namely pins 48 and 22 (used as part for the SPI flash connections for Metro express)
I checked the pins defined for Metro using dir(board) and the above mentioned pins are not listed. so I was wondering if thru direct port manipulation, these could be accessed. Also would have been nice if for the metro the 2nd SPI but define! Extra IOs are always handy and soldering is not and issue for me! ;)
- MetroM0
- Posts: 8
- Joined: Fri Jul 29, 2022 2:41 am
Re: Blink for Metro M0 using direct register manipulation
Is there any way for me to modify the METRO .uf2 file to allow me to use SP1 and/or SCK pin is #38, MISO is #36, MOSI is #37, and CS is #39 as digital IO pins?
if yes, could you kindly please describe to me how to go about doing that?
if yes, could you kindly please describe to me how to go about doing that?
- neradoc
- Posts: 542
- Joined: Wed Apr 27, 2016 2:38 pm
Re: Blink for Metro M0 using direct register manipulation
I am very confused. You say Metro M0, but there is no Metro M0 that is not Express that I know of.
What board are you using exactly ?
What build of Circuitpython are you using ?
Do you mean the Arduino Zero ?
Can you show on the board schematic/pinouts the pins that are not available in Circuitpython ?
If they are missing and usable they can be added to the board definition.
What board are you using exactly ?
What build of Circuitpython are you using ?
Do you mean the Arduino Zero ?
Can you show on the board schematic/pinouts the pins that are not available in Circuitpython ?
If they are missing and usable they can be added to the board definition.
- MetroM0
- Posts: 8
- Joined: Fri Jul 29, 2022 2:41 am
Re: Blink for Metro M0 using direct register manipulation
Yes, I am using an arduino M0 pro, Circuitpython build is adafruit-circuitpython-arduino_zero-en_GB-7.3.1.uf2neradoc wrote:I am very confused. You say Metro M0, but there is no Metro M0 that is not Express that I know of.
What board are you using exactly ?
What build of Circuitpython are you using ?
Do you mean the Arduino Zero ?
Can you show on the board schematic/pinouts the pins that are not available in Circuitpython ?
If they are missing and usable they can be added to the board definition.
As previously mention when I run the "dir(board)" command I cannot see the SPI1 definitions ie SCK1 pin#38, MISO1 #36, MOSI1 #37, and CS1 #39
This is the results I get:
['__name__', 'A0', 'A1', 'A2', 'A3', 'A4', 'A5', 'D0', 'D1', 'D10', 'D11', 'D12', 'D13', 'D2', 'D3', 'D4', 'D5', 'D6', 'D7', 'D8', 'D9', 'I2C', 'LED', 'MISO', 'MOSI', 'RX', 'SCK', 'SCL', 'SDA', 'SPI', 'TX', 'UART', 'board_id']
hence my query as to whether this pins can be made accessible within circuitPython. the hadware aspect is not an issue for me.
- Attachments
-
- schem_arduino-M0-pro.pdf
- (106.53 KiB) Downloaded 3 times
- neradoc
- Posts: 542
- Joined: Wed Apr 27, 2016 2:38 pm
Re: Blink for Metro M0 using direct register manipulation
The pins might be available through the microcontroller module.
(I don't have the board, I tested by installing the Arduino Zero build on another M0 board).
Are there missing pins from that list ? I don't actually know exactly where the microcontroller module pins are managed, I can look into it.
You can add pin names for the ones that don't have them to the board definition, if they are the same on the zero and the M0 Pro, or we can make a new board definition for the M0 Pro.
https://github.com/adafruit/circuitpyth ... ero/pins.c
The flash pins on the Metro are the following:
(I don't have the board, I tested by installing the Arduino Zero build on another M0 board).
Code: Select all
>>> import microcontroller
>>>help(microcontroller.pin)
object <module ''> is of type module
PA02 -- board.A0
PA03 -- microcontroller.pin.PA03
PA04 -- board.A3
PA05 -- board.A4
PA06 -- board.D8
PA07 -- board.D9
PA08 -- board.D4
PA09 -- board.D3
PA10 -- board.D1
PA11 -- board.D0
PA12 -- board.MISO
PA13 -- microcontroller.pin.PA13
PA14 -- board.D2
PA15 -- board.D5
PA16 -- board.D11
PA17 -- board.D13
PA18 -- board.D10
PA19 -- board.D12
PA20 -- board.D6
PA21 -- board.D7
PA22 -- board.SDA
PA23 -- board.SCL
PA27 -- microcontroller.pin.PA27
PA28 -- microcontroller.pin.PA28
PB02 -- board.A5
PB03 -- microcontroller.pin.PB03
PB08 -- board.A1
PB09 -- board.A2
PB10 -- board.MOSI
PB11 -- board.SCK
PB22 -- microcontroller.pin.PB22
PB23 -- microcontroller.pin.PB23
You can add pin names for the ones that don't have them to the board definition, if they are the same on the zero and the M0 Pro, or we can make a new board definition for the M0 Pro.
https://github.com/adafruit/circuitpyth ... ero/pins.c
The flash pins on the Metro are the following:
Code: Select all
#define SPI_FLASH_MOSI_PIN &pin_PB22
#define SPI_FLASH_MISO_PIN &pin_PB03
#define SPI_FLASH_SCK_PIN &pin_PB23
#define SPI_FLASH_CS_PIN &pin_PA13
- MetroM0
- Posts: 8
- Joined: Fri Jul 29, 2022 2:41 am
Re: Blink for Metro M0 using direct register manipulation
from the list your shared, it does appear that the pins I'm after are defined here...neradoc wrote: Are there missing pins from that list ? I don't actually know exactly where the microcontroller module pins are managed, I can look into it.
I sure can add them to the board definition file but just to confirm, within CircuitPython, could you kindly please lay out the steps I would need to take in order to confirm I have done so correctly (as mentioned before I am a noob when it comes to using circuitpython! ;P)neradoc wrote: You can add pin names for the ones that don't have them to the board definition, if they are the same on the zero and the M0 Pro, or we can make a new board definition for the M0 Pro
- neradoc
- Posts: 542
- Joined: Wed Apr 27, 2016 2:38 pm
Re: Blink for Metro M0 using direct register manipulation
Yeah, so you can use them like you can use board.* pins:MetroM0 wrote:from the list your shared, it does appear that the pins I'm after are defined here...
Code: Select all
import microcontroller
import digitalio
import time
blink = digitalio.DigitalInOut(microcontroller.pin.PB03)
blink.switch_to_output(True)
while True:
blink.value = not blink.value
time.sleep(1)
- PB03 is wired to the LED_RX
- PB22, PB23 and PA13 are wired to the on-board programming chip like they are on the zero
- LED_TX is wired to PA27
So you should be able to use PB03 and PA27 from microcontroller to blink those LEDs. PA17 is board.LED (and D13).
Are there other pins that are not in the board module ?
If so, can you show them on the pinout or schematics ?
- MetroM0
- Posts: 8
- Joined: Fri Jul 29, 2022 2:41 am
Re: Blink for Metro M0 using direct register manipulation
Thank you for the sample code. I tried it out but is comes up with the following error:
Any ideas as to why?
The script you shared allows me to use those pins a Digital IO but if I wanted to make use of SPI1 (ie SPI_Flash on Metro express), is there any library I would be able to use to allow me to do that as well?
PS: I am very well pleased with the support I have been receiving here so far! :)
Code: Select all
import microcontroller
>>> import digitalio
>>> led = digitalio.DigitalInOut(microcontroller.pin.PB03)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: PB03 in use
that accounts for all the pins that are not directly accessible of the M0 Pro as far as I am aware, thank you.neradoc wrote: Looking closer at the schematics though:
- PB03 is wired to the LED_RX
- PB22, PB23 and PA13 are wired to the on-board programming chip like they are on the zero
- LED_TX is wired to PA27
Are there other pins that are not in the board module ?
If so, can you show them on the pinout or schematics ?
The script you shared allows me to use those pins a Digital IO but if I wanted to make use of SPI1 (ie SPI_Flash on Metro express), is there any library I would be able to use to allow me to do that as well?
PS: I am very well pleased with the support I have been receiving here so far! :)
- neradoc
- Posts: 542
- Joined: Wed Apr 27, 2016 2:38 pm
Re: Blink for Metro M0 using direct register manipulation
Oh ok, so the LEDs are always in use to automatically report the serial traffic.MetroM0 wrote:Any ideas as to why?
Sorry I missed that, I'm not familiar with that feature and I don't have the board to try stuff.
That wouldn't help you use SPI anyway.
So the conclusion is that there are no free pins on the board that are not already defined it seems.
You can use any valid pins for SPI.MetroM0 wrote:The script you shared allows me to use those pins a Digital IO but if I wanted to make use of SPI1 (ie SPI_Flash on Metro express), is there any library I would be able to use to allow me to do that as well?
Here is a script that should list all trio of pins valid for SPI, excluding the default SPI pins (D11-D13).
- MetroM0
- Posts: 8
- Joined: Fri Jul 29, 2022 2:41 am
Re: Blink for Metro M0 using direct register manipulation
Too bad this is the case :(neradoc wrote:So the conclusion is that there are no free pins on the board that are not already defined it seems.MetroM0 wrote:Any ideas as to why?
But can live with that! it was a long shot after all! ;)
Thank your for your kind support in understanding my board better.
I consider this topic not closed! :)
- MetroM0
- Posts: 8
- Joined: Fri Jul 29, 2022 2:41 am
Re: Blink for Metro M0 using direct register manipulation
@neradoc ,neradoc wrote: So the conclusion is that there are no free pins on the board that are not already defined it seems.
I have been playing around a bit more with my board and turns out that what it is possible to use the 'extra' pins though not all of them!!
Code: Select all
PA02 -- board.A0
PA03 -- microcontroller.pin.PA03 -- AREF on board can be used a digital IO
PA04 -- board.A3
PA05 -- board.A4
PA06 -- board.D8
PA07 -- board.D9
PA08 -- board.D4
PA09 -- board.D3
PA10 -- board.D1
PA11 -- board.D0
PA12 -- board.MISO
PA13 -- microcontroller.pin.PA13 -- Available to use
PA14 -- board.D2
PA15 -- board.D5
PA16 -- board.D11
PA17 -- board.D13
PA18 -- board.D10
PA19 -- board.D12
PA20 -- board.D6
PA21 -- board.D7
PA22 -- board.SDA
PA23 -- board.SCL
PA27 -- microcontroller.pin.PA27 -- NOT available
PA28 -- microcontroller.pin.PA28 -- Available to use (controls 5V supply from USB)
PB02 -- board.A5
PB03 -- microcontroller.pin.PB03 -- NOT available
PB08 -- board.A1
PB09 -- board.A2
PB10 -- board.MOSI
PB11 -- board.SCK
PB22 -- microcontroller.pin.PB22 -- Available to use
PB23 -- microcontroller.pin.PB23 -- Available to use
1. I am able to use PA28 as digital IO but looking at the board schematic I'm unclear of its acutal purpose. is it really OK to use this pin as digital IO?
2. PB22/PB23 has Serial UART capability (wired up as such based on schematic). while I was able to use them as digital IO, if I wanted to use them as UART within circuitPython, would it be possible?
Would it also be possible to share a script to demostrate the use of the board hardware serial port UART (ie board pins 0 and 1) please?
Please be positive and constructive with your questions and comments.