[EN] Maix Class

This article compiles a list of the classes that fall under the Maix class MaixPy, the MicroPython of the Sipeed M1W dock suit board (Figure 1) previously mentioned.

(Figure. 1 Sipeed M1W doc suit)

Reading board information

First, let’s check the features of Sipeed M1W dock suit board with the following code which will produce the result as shown in Figure 2.

import sys
import gc
import os
import sys
import time
from Maix import freq

def show_hw_info():
    uname = os.uname()
    mem_total = gc.mem_alloc()+gc.mem_free()
    free_percent = "("+str((gc.mem_free())/mem_total*100.0)+"%)"
    alloc_percent = "("+str((gc.mem_alloc())/mem_total*100.0)+"%)"
    stat = os.statvfs('/flash')
    block_size = stat[0]
    total_blocks = stat[2]
    free_blocks  = stat[3]
    rom_total = (total_blocks * block_size)/1024
    rom_free = (free_blocks * block_size)/1024
    rom_usage = (rom_total-rom_free)
    rfree_percent = "("+str(rom_free/rom_total*100.0)+"%)"
    rusage_percent = "("+str(rom_usage/rom_total*100.0)+"%)"
    print("implementation ....:", sys.implementation)
    print("path ..............:", sys.path)
    print("Python ............:", sys.version)
    print("Platform ..........:",sys.platform)
    print("Memory")
    print("   total ..........:",mem_total/1024,"KB")
    print("   usage ..........:",gc.mem_alloc()/1024,"KB",alloc_percent)
    print("   free ...........:",gc.mem_free()/1024,"KB",free_percent)
    print("ROM")
    print("   total ..........:", rom_total,"KB" )
    print("   usage ..........:", rom_usage,"KB",rfree_percent )
    print("   Free ...........:", rom_free,"KB",rusage_percent )
    print("system name .......:",uname.sysname)
    print("node name .........:",uname.nodename)
    print("release ...........:",uname.release)
    print("version ...........:",uname.version)
    print("machine ...........:",uname.machine)
    cpu_freq, kpu_freq = freq.get()
    print("CPU/KPU freq ......: {}/{}MHz".format(cpu_freq, kpu_freq))

gc.enable()
gc.collect()
show_hw_info()
(Figure. 2 Result from reading data from Sipeed M1W Doc Suit)

Now let’s try and find out how to find primes from numbers in the range 2000 with the following code and get an example of the result shown in Figure 3. It will take 569msec.

# Untitled - By: cid - Thu Sep 9 2021

import gc
import os
import sys
import machine as mc
import time as tm
import image, time, lcd

lcd.init(freq=15000000)
gc.enable()
gc.collect()

clock = time.clock()
#mc.freq(400000000)

def isPrime(x):
    i = 2
    while (i < x):
        if x%i == 0:
            return False
        i = i+1
    if (i == x):
        return True
    return False

def testPrimeNumber(maxN):
    counter = 0
    t0 = tm.ticks_ms()
    for n in range(2, maxN):
        if isPrime(n):
            counter+=1
    t1 = tm.ticks_ms()
    print("Found {} in {} msecs.".format(counter,abs(t1-t0)))

testPrimeNumber(2000)

(Figure. 3 Result of testPrime)

Maix Class

Maix is a class for Sipeed M1W boards, which inherits from MicroPython‘s machine class. The Maix class contains the following items:

object <module 'machine'> is of type module
  __name__ -- machine
  FPIOA -- <class 'FPIOA'>
  GPIO -- <class 'Pin'>
  I2S -- <class 'I2S'>
  Audio -- <class 'Audio'>
  FFT -- <class 'FFT'>
  MIC_ARRAY -- <class 'MIC_ARRAY'>
  freq -- <class 'freq'>
  utils -- <class 'utils'>
  config -- <class 'config'>

FPIOA

The FPIOA class is the I/O pin numbering class of the Sipeed M1W suit board, allowing us to use pin names instead of remembering numbers. The list within this class is as follows.

object <class 'FPIOA'> is of type type
  set_function -- <function>
  help -- <function>
  get_Pin_num -- <function>
  JTAG_TCLK -- 0
  JTAG_TDI -- 1
  JTAG_TMS -- 2
  JTAG_TDO -- 3
  SPI0_D0 -- 4
  SPI0_D1 -- 5
  SPI0_D2 -- 6
  SPI0_D3 -- 7
  SPI0_D4 -- 8
  SPI0_D5 -- 9
  SPI0_D6 -- 10
  SPI0_D7 -- 11
  SPI0_SS0 -- 12
  SPI0_SS1 -- 13
  SPI0_SS2 -- 14
  SPI0_SS3 -- 15
  SPI0_ARB -- 16
  SPI0_SCLK -- 17
  UARTHS_RX -- 18
  UARTHS_TX -- 19
  RESV6 -- 20
  RESV7 -- 21
  CLK_SPI1 -- 22
  CLK_I2C1 -- 23
  GPIOHS0 -- 24
  GPIOHS1 -- 25
  GPIOHS2 -- 26
  GPIOHS3 -- 27
  GPIOHS4 -- 28
  GPIOHS5 -- 29
  GPIOHS6 -- 30
  GPIOHS7 -- 31
  GPIOHS8 -- 32
  GPIOHS9 -- 33
  GPIOHS10 -- 34
  GPIOHS11 -- 35
  GPIOHS12 -- 36
  GPIOHS13 -- 37
  GPIOHS14 -- 38
  GPIOHS15 -- 39
  GPIOHS16 -- 40
  GPIOHS17 -- 41
  GPIOHS18 -- 42
  GPIOHS19 -- 43
  GPIOHS20 -- 44
  GPIOHS21 -- 45
  GPIOHS22 -- 46
  GPIOHS23 -- 47
  GPIOHS24 -- 48
  GPIOHS25 -- 49
  GPIOHS26 -- 50
  GPIOHS27 -- 51
  GPIOHS28 -- 52
  GPIOHS29 -- 53
  GPIOHS30 -- 54
  GPIOHS31 -- 55
  GPIO0 -- 56
  GPIO1 -- 57
  GPIO2 -- 58
  GPIO3 -- 59
  GPIO4 -- 60
  GPIO5 -- 61
  GPIO6 -- 62
  GPIO7 -- 63
  UART1_RX -- 64
  UART1_TX -- 65
  UART2_RX -- 66
  UART2_TX -- 67
  UART3_RX -- 68
  UART3_TX -- 69
  SPI1_D0 -- 70
  SPI1_D1 -- 71
  SPI1_D2 -- 72
  SPI1_D3 -- 73
  SPI1_D4 -- 74
  SPI1_D5 -- 75
  SPI1_D6 -- 76
  SPI1_D7 -- 77
  SPI1_SS0 -- 78
  SPI1_SS1 -- 79
  SPI1_SS2 -- 80
  SPI1_SS3 -- 81  SPI1_ARB -- 82
  SPI1_SCLK -- 83
  SPI_SLAVE_D0 -- 84
  SPI_SLAVE_SS -- 85
  SPI_SLAVE_SCLK -- 86
  I2S0_MCLK -- 87
  I2S0_SCLK -- 88
  I2S0_WS -- 89
  I2S0_IN_D0 -- 90
  I2S0_IN_D1 -- 91
  I2S0_IN_D2 -- 92
  I2S0_IN_D3 -- 93
  I2S0_OUT_D0 -- 94
  I2S0_OUT_D1 -- 95
  I2S0_OUT_D2 -- 96
  I2S0_OUT_D3 -- 97
  I2S1_MCLK -- 98
  I2S1_SCLK -- 99
  I2S1_WS -- 100
  I2S1_IN_D0 -- 101
  I2S1_IN_D1 -- 102
  I2S1_IN_D2 -- 103
  I2S1_IN_D3 -- 104
  I2S1_OUT_D0 -- 105
  I2S1_OUT_D1 -- 106
  I2S1_OUT_D2 -- 107
  I2S1_OUT_D3 -- 108
  I2S2_MCLK -- 109
  I2S2_SCLK -- 110
  I2S2_WS -- 111
  I2S2_IN_D0 -- 112
  I2S2_IN_D1 -- 113
  I2S2_IN_D2 -- 114
  I2S2_IN_D3 -- 115
  I2S2_OUT_D0 -- 116
  I2S2_OUT_D1 -- 117
  I2S2_OUT_D2 -- 118
  I2S2_OUT_D3 -- 119
  RESV0 -- 120
  RESV1 -- 121
  RESV2 -- 122
  RESV3 -- 123
  RESV4 -- 124
  RESV5 -- 125
  I2C0_SCLK -- 126
  I2C0_SDA -- 127
  I2C1_SCLK -- 128
  I2C1_SDA -- 129
  I2C2_SCLK -- 130
  I2C2_SDA -- 131
  CMOS_XCLK -- 132
  CMOS_RST -- 133
  CMOS_PWDN -- 134
  CMOS_VSYNC -- 135
  CMOS_HREF -- 136
  CMOS_PCLK -- 137
  CMOS_D0 -- 138
  CMOS_D1 -- 139
  CMOS_D2 -- 140
  CMOS_D3 -- 141
  CMOS_D4 -- 142
  CMOS_D5 -- 143
  CMOS_D6 -- 144
  CMOS_D7 -- 145
  SCCB_SCLK -- 146
  SCCB_SDA -- 147
  UART1_CTS -- 148
  UART1_DSR -- 149
  UART1_DCD -- 150
  UART1_RI -- 151
  UART1_SIR_IN -- 152
  UART1_DTR -- 153
  UART1_RTS -- 154
  UART1_OUT2 -- 155
  UART1_OUT1 -- 156
  UART1_SIR_OUT -- 157
  UART1_BAUD -- 158
  UART1_RE -- 159
  UART1_DE -- 160
  UART1_RS485_EN -- 161
  UART2_CTS -- 162
  UART2_DSR -- 163
  UART2_DCD -- 164
  UART2_RI -- 165
  UART2_SIR_IN -- 166
  UART2_DTR -- 167
  UART2_RTS -- 168
  UART2_OUT2 -- 169
  UART2_OUT1 -- 170
  UART2_SIR_OUT -- 171
  UART2_BAUD -- 172
  UART2_RE -- 173
  UART2_DE -- 174
  UART2_RS485_EN -- 175
  UART3_CTS -- 176
  UART3_DSR -- 177
  UART3_DCD -- 178
  UART3_RI -- 179
  UART3_SIR_IN -- 180
  UART3_DTR -- 181
  UART3_RTS -- 182
  UART3_OUT2 -- 183
  UART3_OUT1 -- 184
  UART3_SIR_OUT -- 185
  UART3_BAUD -- 186
  UART3_RE -- 187
  UART3_DE -- 188
  UART3_RS485_EN -- 189
  TIMER0_TOGGLE1 -- 190
  TIMER0_TOGGLE2 -- 191
  TIMER0_TOGGLE3 -- 192
  TIMER0_TOGGLE4 -- 193
  TIMER1_TOGGLE1 -- 194
  TIMER1_TOGGLE2 -- 195
  TIMER1_TOGGLE3 -- 196
  TIMER1_TOGGLE4 -- 197
  TIMER2_TOGGLE1 -- 198
  TIMER2_TOGGLE2 -- 199
  TIMER2_TOGGLE3 -- 200
  TIMER2_TOGGLE4 -- 201
  CLK_SPI2 -- 202
  CLK_I2C2 -- 203

GPIO

The GPIO class is a class that inherits from the Pin class. It is used to define the functions of the pin and set/read the status of the pin which consists of the following items:

bject <class 'Pin'> is of type type
  init -- <function>
  value -- <function>
  irq -- <function>
  disirq -- <function>
  mode -- <function>
  IN -- 0
  OUT -- 3
  PULL_NONE -- -1
  PULL_UP -- 2
  PULL_DOWN -- 1
  IRQ_NONE -- 0
  IRQ_RISING -- 2
  IRQ_FALLING -- 1
  IRQ_BOTH -- 3
  GPIOHS0 -- 0
  GPIOHS1 -- 1
  GPIOHS2 -- 2
  GPIOHS3 -- 3
  GPIOHS4 -- 4
  GPIOHS5 -- 5
  GPIOHS6 -- 6
  GPIOHS7 -- 7
  GPIOHS8 -- 8
  GPIOHS9 -- 9
  GPIOHS10 -- 10
  GPIOHS11 -- 11
  GPIOHS12 -- 12
  GPIOHS13 -- 13
  GPIOHS14 -- 14
  GPIOHS15 -- 15
  GPIOHS16 -- 16
  GPIOHS17 -- 17
  GPIOHS18 -- 18
  GPIOHS19 -- 19
  GPIOHS20 -- 20
  GPIOHS21 -- 21
  GPIOHS22 -- 22
  GPIOHS23 -- 23
  GPIOHS24 -- 24
  GPIOHS25 -- 25
  GPIOHS26 -- 26
  GPIOHS27 -- 27
  GPIOHS28 -- 28
  GPIOHS29 -- 29
  GPIOHS30 -- 30
  GPIOHS31 -- 31
  GPIO0 -- 32
  GPIO1 -- 33
  GPIO2 -- 34
  GPIO3 -- 35
  GPIO4 -- 36
  GPIO5 -- 37
  GPIO6 -- 38
  GPIO7 -- 39
  WAKEUP_NOT_SUPPORT -- 0

I2S

The I2S class is a class for connecting to an audio module via the I2S bus. The details within the class are:

object <class 'I2S'> is of type type
  __deinit__ -- <function>
  init -- <function>
  channel_config -- <function>
  set_sample_rate -- <function>
  record -- <function>
  wait_record -- <function>
  play -- <function>
  DEVICE_0 -- 0
  DEVICE_1 -- 1
  DEVICE_2 -- 2
  CHANNEL_0 -- 0
  CHANNEL_1 -- 1
  CHANNEL_2 -- 2
  CHANNEL_3 -- 3
  IGNORE_WORD_LENGTH -- 0
  RESOLUTION_12_BIT -- 1
  RESOLUTION_16_BIT -- 2
  RESOLUTION_20_BIT -- 3
  RESOLUTION_24_BIT -- 4
  RESOLUTION_32_BIT -- 5
  SCLK_CYCLES_16 -- 0
  SCLK_CYCLES_24 -- 1
  SCLK_CYCLES_32 -- 2
  TRANSMITTER -- 0
  RECEIVER -- 1
  STANDARD_MODE -- 1
  RIGHT_JUSTIFYING_MODE -- 2
  LEFT_JUSTIFYING_MODE -- 4

Audio

The Audio class is a class that performs music playback, volume adjustment, and recording. The list of commands within the Audio class is as follows:

object <class 'Audio'> is of type type
  init -- <function>
  __deinit__ -- <function>
  to_bytes -- <function>
  play_process -- <function>
  volume -- <function>
  play -- <function>
  record_process -- <function>
  record -- <function>
  finish -- <function>

FFT

The FFT class is used to perform a Fast Fourier Transform. The class commands are:

object <class 'FFT'> is of type type
  run -- <function>
  freq -- <function>
  amplitude -- <function>

MIC_ARRAY

The MIC_ARRAY class is used to support Sipeed’s Microphone Array module are

object <class 'MIC_ARRAY'> is of type type
  init -- <function>
  deinit -- <function>
  get_dir -- <function>
  set_led -- <function>
  get_map -- <function>

freq

The freq class is used to read CPU and KPU clock frequencies.

object <class 'freq'> is of type type
  __name__ -- freq
  set -- <function>
  get -- <function>
  get_kpu -- <function>
  get_cpu -- <function>

utils

The utils class is a collection of instructions for quantifying memory and read data from flash ROM memory as follows

object <class 'utils'> is of type type
  __name__ -- utils
  gc_heap_size -- <function>
  heap_free -- <function>
  flash_read -- <function>

config

The config class is used to read board configuration values:

object <class 'config'> is of type type
  __name__ -- config
  __init__ -- <function>
  get_value -- <function>

Conclusion

From this article, Sipeed M1W‘s Maix class consists of internal sub-markets related to board I/O, I2S processing, audio, FFT, and MIC Array. This enables convenience and high availability because the board itself has a TFT display interface, a microSD reader, and a camera mount as mentioned in the previous article. The details of the usage of each class will be discussed in the following articles. Finally, have fun with programming.

If you want to talk with us, feel free to leave comments below!!

(C) 2020-2021, By Jarut Busarathid and Danai Jedsadathitikul
Updated 2021-12-21