quickref.rst 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. .. _quickref_:
  2. Quick reference for the WiPy
  3. ============================
  4. .. image:: https://raw.githubusercontent.com/wipy/wipy/master/docs/PinOUT.png
  5. :alt: WiPy pinout and alternate functions table
  6. :width: 800px
  7. General board control (including sleep modes)
  8. ---------------------------------------------
  9. See the :mod:`machine` module::
  10. import machine
  11. help(machine) # display all members from the machine module
  12. machine.freq() # get the CPU frequency
  13. machine.unique_id() # return the 6-byte unique id of the board (the WiPy's MAC address)
  14. machine.idle() # average current decreases to (~12mA), any interrupts wake it up
  15. machine.sleep() # everything except for WLAN is powered down (~950uA avg. current)
  16. # wakes from Pin, RTC or WLAN
  17. machine.deepsleep() # deepest sleep mode, MCU starts from reset. Wakes from Pin and RTC.
  18. Pins and GPIO
  19. -------------
  20. See :ref:`machine.Pin <machine.Pin>`. ::
  21. from machine import Pin
  22. # initialize GP2 in gpio mode (alt=0) and make it an output
  23. p_out = Pin('GP2', mode=Pin.OUT)
  24. p_out.value(1)
  25. p_out.value(0)
  26. p_out.toggle()
  27. p_out(True)
  28. # make GP1 an input with the pull-up enabled
  29. p_in = Pin('GP1', mode=Pin.IN, pull=Pin.PULL_UP)
  30. p_in() # get value, 0 or 1
  31. Timers
  32. ------
  33. See :ref:`machine.Timer <machine.Timer>` and :ref:`machine.Pin <machine.Pin>`.
  34. Timer ``id``'s take values from 0 to 3.::
  35. from machine import Timer
  36. from machine import Pin
  37. tim = Timer(0, mode=Timer.PERIODIC)
  38. tim_a = tim.channel(Timer.A, freq=1000)
  39. tim_a.freq(5) # 5 Hz
  40. p_out = Pin('GP2', mode=Pin.OUT)
  41. tim_a.irq(trigger=Timer.TIMEOUT, handler=lambda t: p_out.toggle())
  42. PWM (pulse width modulation)
  43. ----------------------------
  44. See :ref:`machine.Pin <machine.Pin>` and :ref:`machine.Timer <machine.Timer>`. ::
  45. from machine import Timer
  46. # timer 1 in PWM mode and width must be 16 buts
  47. tim = Timer(1, mode=Timer.PWM, width=16)
  48. # enable channel A @1KHz with a 50.55% duty cycle
  49. tim_a = tim.channel(Timer.A, freq=1000, duty_cycle=5055)
  50. ADC (analog to digital conversion)
  51. ----------------------------------
  52. See :ref:`machine.ADC <machine.ADC>`. ::
  53. from machine import ADC
  54. adc = ADC()
  55. apin = adc.channel(pin='GP3')
  56. apin() # read value, 0-4095
  57. UART (serial bus)
  58. -----------------
  59. See :ref:`machine.UART <machine.UART>`. ::
  60. from machine import UART
  61. uart = UART(0, baudrate=9600)
  62. uart.write('hello')
  63. uart.read(5) # read up to 5 bytes
  64. SPI bus
  65. -------
  66. See :ref:`machine.SPI <machine.SPI>`. ::
  67. from machine import SPI
  68. # configure the SPI master @ 2MHz
  69. spi = SPI(0, SPI.MASTER, baudrate=200000, polarity=0, phase=0)
  70. spi.write('hello')
  71. spi.read(5) # receive 5 bytes on the bus
  72. rbuf = bytearray(5)
  73. spi.write_readinto('hello', rbuf) # send and receive 5 bytes
  74. I2C bus
  75. -------
  76. See :ref:`machine.I2C <machine.I2C>`. ::
  77. from machine import I2C
  78. # configure the I2C bus
  79. i2c = I2C(baudrate=100000)
  80. i2c.scan() # returns list of slave addresses
  81. i2c.writeto(0x42, 'hello') # send 5 bytes to slave with address 0x42
  82. i2c.readfrom(0x42, 5) # receive 5 bytes from slave
  83. i2c.readfrom_mem(0x42, 0x10, 2) # read 2 bytes from slave 0x42, slave memory 0x10
  84. i2c.writeto_mem(0x42, 0x10, 'xy') # write 2 bytes to slave 0x42, slave memory 0x10
  85. Watchdog timer (WDT)
  86. --------------------
  87. See :ref:`machine.WDT <machine.WDT>`. ::
  88. from machine import WDT
  89. # enable the WDT with a timeout of 5s (1s is the minimum)
  90. wdt = WDT(timeout=5000)
  91. wdt.feed()
  92. Real time clock (RTC)
  93. ---------------------
  94. See :ref:`machine.RTC <machine.RTC>` ::
  95. from machine import RTC
  96. rtc = RTC() # init with default time and date
  97. rtc = RTC(datetime=(2015, 8, 29, 9, 0, 0, 0, None)) # init with a specific time and date
  98. print(rtc.now())
  99. def alarm_handler (rtc_o):
  100. pass
  101. # do some non blocking operations
  102. # warning printing on an irq via telnet is not
  103. # possible, only via UART
  104. # create a RTC alarm that expires after 5 seconds
  105. rtc.alarm(time=5000, repeat=False)
  106. # enable RTC interrupts
  107. rtc_i = rtc.irq(trigger=RTC.ALARM0, handler=alarm_handler, wake=machine.SLEEP)
  108. # go into suspended mode waiting for the RTC alarm to expire and wake us up
  109. machine.sleep()
  110. SD card
  111. -------
  112. See :ref:`machine.SD <machine.SD>`. ::
  113. from machine import SD
  114. import os
  115. # clock pin, cmd pin, data0 pin
  116. sd = SD(pins=('GP10', 'GP11', 'GP15'))
  117. # or use default ones for the expansion board
  118. sd = SD()
  119. os.mount(sd, '/sd')
  120. WLAN (WiFi)
  121. -----------
  122. See :ref:`network.WLAN <network.WLAN>` and :mod:`machine`. ::
  123. import machine
  124. from network import WLAN
  125. # configure the WLAN subsystem in station mode (the default is AP)
  126. wlan = WLAN(mode=WLAN.STA)
  127. # go for fixed IP settings
  128. wlan.ifconfig(config=('192.168.0.107', '255.255.255.0', '192.168.0.1', '8.8.8.8'))
  129. wlan.scan() # scan for available networks
  130. wlan.connect(ssid='mynetwork', auth=(WLAN.WPA2, 'mynetworkkey'))
  131. while not wlan.isconnected():
  132. pass
  133. print(wlan.ifconfig())
  134. # enable wake on WLAN
  135. wlan.irq(trigger=WLAN.ANY_EVENT, wake=machine.SLEEP)
  136. # go to sleep
  137. machine.sleep()
  138. # now, connect to the FTP or the Telnet server and the WiPy will wake-up
  139. Telnet and FTP server
  140. ---------------------
  141. See :ref:`network.Server <network.Server>` ::
  142. from network import Server
  143. # init with new user, password and seconds timeout
  144. server = Server(login=('user', 'password'), timeout=60)
  145. server.timeout(300) # change the timeout
  146. server.timeout() # get the timeout
  147. server.isrunning() # check whether the server is running or not
  148. Heart beat LED
  149. --------------
  150. See :mod:`wipy`. ::
  151. import wipy
  152. wipy.heartbeat(False) # disable the heartbeat LED
  153. wipy.heartbeat(True) # enable the heartbeat LED
  154. wipy.heartbeat() # get the heartbeat state