Skip to content

machine

Boris Lovosevic edited this page Jul 26, 2019 · 4 revisions

machine module

machine module contains various methods and classes related to K210 hardware.

Methods

machine.freq([cpu_freq [,pll0_freq]])

Get or set the current K210 CPU clock in MHz
Executed without an argument returns the current CPU clock and PLL0 frequency in Hz.

When setting the frequency, cpu_freq argument must be entered in MHz.
Only frequencies 100, 200 and 400 MHz are alloved.
Optional argument pll0_freq can be given to set the PLL0 frequency. Alloved values are 800 and 1000 (MHz).
Acctual CPU frequency depends on PLL0 frequency (default: 1000MHz).
When the PLL frequency is set to 1000MHz, the actual CPU frequency will be 125% of the requested frequency (123.5MHz, 247MHz, 494MHz).

Returns tuple of current CPU frequency and current PLL0 frequency in Hz.

machine.reset()

Reset the K210

machine.reset_reason()

Returns the K210 reset reason as 2-item tuple (code, description)
0 - Unknown
1 - Soft reset
2 - WDT0 reset
3 - WDT1 (NLR) reset
4 - External pin reset
5 - Unknown
6 - Unknown
7 - Unknown

machine.random(limit [,upper_limit])

Returns random number between 0 and limit
If the optional upper_limit argument is given, returns random number between limit and upper_limit


machine.pinstat()

Reports currently used pins

>>> machine.pinstat()
 Pin  GpioHS     Used by      as  Fpioa
---------------------------------------
   4       -    ISP_UART      Rx     18
   5       -    ISP_UART      Tx     19
  12       2         Pin  output     26
  36       -     Display      cs     15
  37       1     Display     rst     25
  38       0     Display     dcx     24
  39       -     Display     clk     17
---------------------------------------
>>> 

machine.repl_baudrate([baudrate])

Set or get the baudrate used by the REPL UART.
Most of the standard baudrates from 115200 to 4000000 bd can be used.
After the command is executed, you must change the baudrate of your terminal emulator too.

>>> machine.repl_baudrate()                                                                                                                 
115208                                                                                                                                      
>>>                                                                                                                                         
>>> machine.repl_baudrate(1000000)                                                                                                          
###  <--- Change baud rate of the terminal emulator                                                                                                                                         
>>> machine.repl_baudrate()                                                                                                                 
1000000                                                                                                                                     
>>> 

The baudrate can be set in boot.py.

If using MPyTerm as terminal emulator, both REPL baudrate and terminal baudrate can be set at once using MPyTerm's command mode command set_baudrate

machine.loglevel(loglevel)

Set the system log level.
Some system level log messages can be printed during firmware run, depending on the log level set. For loglevel use one of the constants: LOG_NONE, LOG_ERROR, LOG_WARN, LOG_INFO, LOG_DEBUG, LOG_VERBOSE. The dafault log level after boot is LOG_WARN.

machine.fsdebug(True|False)

Enable or disable logs from file system related functions.

>>> machine.loglevel(machine.LOG_DEBUG)
>>> machine.fsdebug(True)
>>> import os
>>> os.listdir('/flash')
D (2087743442) [LITTLEFS]: LOCAL_PATH [/]->[], currdir=[]
D (2087748136) [LITTLEFS]: LISTDIR []
D (2087751875) [LITTLEFS]: [READ] bkl=0, off=0, sz=512, adr=0x400000
D (2087758332) [LITTLEFS]: [READ] bkl=1, off=0, sz=512, adr=0x400200
D (2087764797) [LITTLEFS]: Read dir: [.], 0
D (2087768969) [LITTLEFS]: Read dir: [..], 0
D (2087773321) [LITTLEFS]: Read dir: [boot.py], 82
D (2087778247) [LITTLEFS]: Read dir: [examples], 0
D (2087783103) [LITTLEFS]: Read dir: [fonts], 0
D (2087787702) [LITTLEFS]: Read dir: [lib], 0
D (2087792127) [LITTLEFS]: Read dir: [www], 0
['boot.py', 'examples', 'fonts', 'lib', 'www']
>>> 

machine.crc16(buffer)

Return crc16 value for the provided buffer object.

machine.crc32(buffer)

Return crc32 value for the provided buffer object.

>>> machine.crc16("This is the test string of which we want the CRC value")
31151
>>> machine.crc32("This is the test string of which we want the CRC value")
908668415
>>> 

machine.base64enc(buffer)

Return base64 encoded string for the provided buffer object.

>>> machine.base64enc("This is the test string (\x00\x8a\x07) of which we want base64 encoded")
'VGhpcyBpcyB0aGUgdGVzdCBzdHJpbmcgKADCigcpIG9mIHdoaWNoIHdlIHdhbnQgYmFzZTY0IGVuY29kZWQ='
>>> 

machine.wdt(timeout)

Set the hardware Watchdog timeout in seconds.
Allowed values: 0 ~ 120
Setting it to 0 disables the watchdog.

machine.wdt_reset()

Reset hardware Watchdog. May be needed in some long running functions.

machine.fs_spicheck(True|False)

Enables or disables (default) some additional check when reading and writing from/to Flash file system.
Usually not needed.


machine.mpy_config([args])

Get or set MicroPython configuration.
If the configuration was changed, it will be saved to flash and applied after reset.
All argument are optional, if provided the must be entered as KW arguments (key=value)

Argument Description
two tasks_enable Enable or disable running two MicroPython instances
pystack_enable Enable or disable using of separate PyStack
heap MicroPython heap size in bytes
pystack_size PyStack size if used
main_stack_size Main MicroPython FreeRTOS task's stack size
cpu_freq Default CPU frequency after reset
repl_baudrate Default REPL baudrate after reset
bootmenu_pin Pin (GPIO) used to enter the Boot menu
log_level Default LOG level after reset
log_color Enable or disable using ansi colors
vm_divisor MicroPython virtual machine divisor
Determines after how many bytecodes the threads are switched
print if set to False does nor print the info, only returnes te result tuple
>>> machine.mpy_config()

MicroPython configuration:
--------------------------
   MPy version code: 011106
  Two MPy instances: False
       PyStack used: True
Available heap size: 2852 KB
      MPy heap size: 2852 KB
       PyStack size: 4096 B
     MPy stack size: 32768 B
      CPU frequency: 400 MHz
      REPL baudrate: 115200 bd
      Boot menu pin: 17
  Default log level: 2 (LOG_WARN)
     Use log colors: 1 (True)
         VM divisor: 32 bytecodes
(False, True, 2920448, 2920448, 0, 4096, 32768, 400000000, 115200, 17, 2, True, 32)
>>> 

machine.rambuf()

Returns the address and size of the special RAM buffer which may be used with machine.mem_XXX functions.
RAM buffer's content is preserved after reset.

machine.mem8

machine.mem16

machine.mem32

machine.mem64

machine.memstr

machine.membytes(size)

Access RAM memory directly.
All K210 RAM can be read, only the memory in the SRAM buffer can be written.

>>> import machine
>>> ram=machine.rambuf()
>>> ramaddr=ram[0]
>>> machine.mem8[ramaddr] = 210
>>> machine.mem16[ramaddr+32] = 10000
>>> machine.mem64[ramaddr+128] = 123456789012345678
>>> machine.memstr[ramaddr+256] = "MicroPython machine module"
>>> mb=machine.membytes(64)
>>> mb
<64 bytes memory>
>>> mb[ramaddr+512] = b'\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c'
>>> 
>>> machine.mem8[ramaddr]
210
>>> machine.mem16[ramaddr+32]
10000
>>> machine.mem64[ramaddr+128]
123456789012345678
>>> machine.memstr[ramaddr+256]
'MicroPython machine module'
>>> mb[ramaddr+512]
'\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\x7f/\x00\x07\x04D\x12\n\x01_\x00=)\x00\x03_w\x08\x05\x07d\x14\t\x00;?]\u03f9\x0f\x030\x10D\x10h;~\x15\u037e\x00_'
Clone this wiki locally