machine constants

C programming, build, interpreter/VM.
Target audience: MicroPython Developers.
Post Reply
hlovatt
Posts: 68
Joined: Thu Aug 15, 2019 3:52 am
Location: Sydney

machine constants

Post by hlovatt » Sat Aug 08, 2020 6:53 am

Hi,

When you:

Code: Select all

>>> dir(machine)
['__class__', '__name__', 'RTC', 'ADC', 'DEEPSLEEP_RESET', 'HARD_RESET', 'I2C', 'PWRON_RESET', 'Pin', 'SOFT_RESET', 'SPI', 'Signal', 'Timer', 'UART', 'WDT', 'WDT_RESET', 'bootloader', 'deepsleep', 'disable_irq', 'enable_irq', 'freq', 'idle', 'info', 'lightsleep', 'mem16', 'mem32', 'mem8', 'reset', 'reset_cause', 'rng', 'sleep', 'soft_reset', 'time_pulse_us', 'unique_id']
But the documentation mentions more constants:

Code: Select all

Constants
---------

.. data:: machine.IDLE
          machine.SLEEP
          machine.DEEPSLEEP

    IRQ wake values.

.. data:: machine.PWRON_RESET
          machine.HARD_RESET
          machine.WDT_RESET
          machine.DEEPSLEEP_RESET
          machine.SOFT_RESET

    Reset causes.

.. data:: machine.WLAN_WAKE
          machine.PIN_WAKE
          machine.RTC_WAKE

    Wake-up reasons.
Only the `XXX_RESET` constants are present in `dir`, which is correct `dir` or docs?

Thanks,

-- Howard.

hlovatt
Posts: 68
Joined: Thu Aug 15, 2019 3:52 am
Location: Sydney

Re: machine constants

Post by hlovatt » Sun Aug 09, 2020 4:08 am

Similar issue with `machine.Pin`:

Code: Select all

>>> dir(machine.Pin)
['__class__', '__name__', 'dict', 'value', '__bases__', 'AF1_TIM1', 'AF1_TIM2', 'AF2_TIM3', 'AF2_TIM4', 'AF2_TIM5', 'AF3_TIM10', 'AF3_TIM11', 'AF3_TIM8', 'AF3_TIM9', 'AF4_I2C1', 'AF4_I2C2', 'AF5_SPI1', 'AF5_SPI2', 'AF7_USART1', 'AF7_USART2', 'AF7_USART3', 'AF8_UART4', 'AF8_USART6', 'AF9_CAN1', 'AF9_CAN2', 'AF9_TIM12', 'AF9_TIM13', 'AF9_TIM14', 'AF_OD', 'AF_PP', 'ALT', 'ALT_OPEN_DRAIN', 'ANALOG', 'IN', 'IRQ_FALLING', 'IRQ_RISING', 'OPEN_DRAIN', 'OUT', 'OUT_OD', 'OUT_PP', 'PULL_DOWN', 'PULL_NONE', 'PULL_UP', 'af', 'af_list', 'board', 'cpu', 'debug', 'gpio', 'high', 'init', 'irq', 'low', 'mapper', 'mode', 'name', 'names', 'off', 'on', 'pin', 'port', 'pull']
Docs:

Code: Select all

Constants
---------

The following constants are used to configure the pin objects.  Note that
not all constants are available on all ports.

.. data:: Pin.IN
          Pin.OUT
          Pin.OPEN_DRAIN
          Pin.ALT
          Pin.ALT_OPEN_DRAIN

   Selects the pin mode.

.. data:: Pin.PULL_UP
          Pin.PULL_DOWN
          Pin.PULL_HOLD

   Selects whether there is a pull up/down resistor.  Use the value
   ``None`` for no pull.

.. data:: Pin.LOW_POWER
          Pin.MED_POWER
          Pin.HIGH_POWER

   Selects the pin drive strength.

.. data:: Pin.IRQ_FALLING
          Pin.IRQ_RISING
          Pin.IRQ_LOW_LEVEL
          Pin.IRQ_HIGH_LEVEL

   Selects the IRQ trigger type.
 
Note missing "pin drive strength"s.

Which is correct `dir` or docs?

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

Re: machine constants

Post by pythoncoder » Sun Aug 09, 2020 4:15 pm

I think you'll find these are hardware dependent. For example STM does not have drive strength adjustment.
Peter Hinch
Index to my micropython libraries.

hlovatt
Posts: 68
Joined: Thu Aug 15, 2019 3:52 am
Location: Sydney

Re: machine constants

Post by hlovatt » Mon Aug 10, 2020 5:57 am

@pythoncoder,

Thanks.

I thought machine was meant to be device independent?

Have I misunderstood?

User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

Re: machine constants

Post by deshipu » Tue Aug 11, 2020 1:46 pm

Sadly, nothing in MicroPython is device-independent.

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

Re: machine constants

Post by pythoncoder » Thu Aug 13, 2020 8:53 am

It's hard to see how it could be, unless machine was limited to a lowest common denominator supported by every single MicroPython platform. Given that Unix doesn't even have Pin instances, it would end up being entirely useless.

Take the issue raised, of pin drive strength. Some hardware doesn't physically support it. Providing the appearance of support without any underlying capability would merely confuse.

I think it is acceptable that physical differences are respected. My confusion arises where language features are only supported selectively. This makes it difficult to write pure Python which is guaranteed to be portable. See this RFC which I raised.
Peter Hinch
Index to my micropython libraries.

Post Reply