Page 1 of 1

Where is actual library source?

Posted: Wed Feb 17, 2021 10:21 pm
by dlynch13
I am new to MicroPython and been looking through docs, books and the forum for days to the answer to these questions.

1. Is the pyb library only to be used for the Pyboard? What if I have a custom STM32 project as I am planning? Should I then be using the machine library instead? My pinout will likely not be like the Pyboard.
2. Where is the source code for the machine library, for instance, the Pin class, or the Adc class? I see that there are different/more functions for the ADC in the pyb library (like filtering) than there are in the machine.ADC library. Perhaps I can add those missing functions to the machine library, but how? I would like to be able to examine the source files for these functions to see how they differ. I see machine_spi.c and etc. in the extmod library, but no such files for machine.Pin or machine.ADC classes. What am I missing? In general, how do I find the source behind any MicroPython class? How can I be sure that I have located the appropriate library code?
3. The pinout for the Pyboard shows a slave select pin for SPI but the library doesn't seem to have a hardware-controlled option. Why would it designate a slave select in the diagram if you could just use any pin and control it via software? I think that the STM32 should support a hardware-controlled slave select. Can it be enabled in MicroPython?

Thanks.

Re: Where is actual library source?

Posted: Thu Feb 18, 2021 1:09 am
by jimmo
dlynch13 wrote:
Wed Feb 17, 2021 10:21 pm
1. Is the pyb library only to be used for the Pyboard? What if I have a custom STM32 project as I am planning? Should I then be using the machine library instead? My pinout will likely not be like the Pyboard.
The pyb module pre-dates the machine module, back when the Pyboard was the only supported board. It's confusing these days but "pyboard" as a term is used to refer to both "the pyboard(s)" but also "a board running micropython". (e.g. pyboard.py http://docs.micropython.org/en/latest/r ... rd.py.html works on all boards and ports, not just pyboard/stm32).

However, the pyb module works on all STM32 boards.

In general though, most core functionality has been moved across to the machine module (e.g. Pin, I2C, SPI, etc), and in most cases the machine.X class is actually the exact same class as pyb.X.

FWIW, there's also an "stm" module, and probably the end goal should be that common stuff ends up in "machine", and stm32 stuff ends up in "stm" and "pyb" goes away.
dlynch13 wrote:
Wed Feb 17, 2021 10:21 pm
2. Where is the source code for the machine library, for instance, the Pin class, or the Adc class? I see that there are different/more functions for the ADC in the pyb library (like filtering) than there are in the machine.ADC library. Perhaps I can add those missing functions to the machine library, but how? I would like to be able to examine the source files for these functions to see how they differ. I see machine_spi.c and etc. in the extmod library, but no such files for machine.Pin or machine.ADC classes. What am I missing? In general, how do I find the source behind any MicroPython class? How can I be sure that I have located the appropriate library code?
This is a bit confusing because of the way that the pyb.X classes alias the machine.X classes.

In general, the approach is:
- A port-specific module "foo" will exist as "ports/name/modfoo.c". So ports/stm32/modmachine.c
- This will contain all the "globals" for that module. So for machine.ADC:

Code: Select all

{ MP_ROM_QSTR(MP_QSTR_ADC),                 MP_ROM_PTR(&machine_adc_type) },
- So now you need to find the definition of machine_adc_type. In this case it's in ports/stm32/machine_adc.c but some types work differently (e.g. the extmod ones like I2C and SPI which share their implementation across ports... this is because the software/bitbanging implementation is the same).
dlynch13 wrote:
Wed Feb 17, 2021 10:21 pm
3. The pinout for the Pyboard shows a slave select pin for SPI but the library doesn't seem to have a hardware-controlled option. Why would it designate a slave select in the diagram if you could just use any pin and control it via software? I think that the STM32 should support a hardware-controlled slave select. Can it be enabled in MicroPython?
Yeah, the pinout is just highlighting that the board is routed this way, so that it is in the right place on the header to use the hardware-controlled feature, however we do not provide an API for it at this stage. This is something that comes up moderately often, I think it would be a useful feature to add to machine.SPI.

I think I have seen people enable the hardware SS via directly setting the registers from Python. i.e. machine.mem32[addr] |= ...
(You can access the register addresses in the stm module.. e.g. machine.mem32[stm.SPI1+stm.SPI_CR2] to acess CR2 on the SPI1 peripheral...I'm not sure which register the SS control is in of the top of my head though)..