2015-06-11 09:53:31 -04:00
|
|
|
.. _quickref_:
|
2015-06-10 17:29:56 -04:00
|
|
|
|
|
|
|
Quick reference for the WiPy
|
|
|
|
============================
|
|
|
|
|
|
|
|
.. image:: https://raw.githubusercontent.com/wipy/wipy/master/docs/PinOUT.png
|
|
|
|
:alt: WiPy pinout and alternate functions table
|
|
|
|
:width: 800px
|
|
|
|
|
|
|
|
General board control
|
|
|
|
---------------------
|
|
|
|
|
|
|
|
See :mod:`pyb`. ::
|
|
|
|
|
|
|
|
import pyb
|
|
|
|
|
|
|
|
help(pyb) # display all members from the pyb module
|
|
|
|
pyb.delay(50) # wait 50 milliseconds
|
|
|
|
pyb.millis() # number of milliseconds since boot-up
|
|
|
|
pyb.freq() # get the CPU frequency
|
2015-06-11 09:53:31 -04:00
|
|
|
pyb.unique_id() # return the 6-byte unique id of the board (the WiPy's MAC address)
|
2015-06-10 17:29:56 -04:00
|
|
|
|
|
|
|
Pins and GPIO
|
|
|
|
-------------
|
|
|
|
|
|
|
|
See :ref:`pyb.Pin <pyb.Pin>`. ::
|
|
|
|
|
|
|
|
from pyb import Pin
|
|
|
|
|
2015-07-28 17:03:53 -04:00
|
|
|
# initialize GP2 in gpio mode (af=0) and make it an output
|
2015-09-03 05:25:44 -04:00
|
|
|
p_out = Pin('GP2', mode=Pin.OUT)
|
2015-06-10 17:29:56 -04:00
|
|
|
p_out.high()
|
|
|
|
p_out.low()
|
|
|
|
p_out.toggle()
|
2015-09-03 05:25:44 -04:00
|
|
|
p_out(True)
|
2015-06-10 17:29:56 -04:00
|
|
|
|
2015-07-28 17:03:53 -04:00
|
|
|
# make GP1 an input with the pull-up enabled
|
2015-09-03 05:25:44 -04:00
|
|
|
p_in = Pin('GP1', mode=Pin.IN, pull = Pin.PULL_UP)
|
|
|
|
p_in() # get value, 0 or 1
|
2015-06-10 17:29:56 -04:00
|
|
|
|
|
|
|
Timers
|
|
|
|
------
|
|
|
|
|
|
|
|
See :ref:`pyb.Timer <pyb.Timer>` and :ref:`pyb.Pin <pyb.Pin>`. ::
|
|
|
|
|
|
|
|
from pyb import Timer
|
|
|
|
from pyb import Pin
|
|
|
|
|
|
|
|
tim = Timer(1, mode=Timer.PERIODIC)
|
|
|
|
tim_a = tim.channel(Timer.A, freq=1000)
|
|
|
|
tim_a.time() # get the value in microseconds
|
|
|
|
tim_a.freq(1) # 1 Hz
|
|
|
|
|
2015-07-28 17:03:53 -04:00
|
|
|
p_out = Pin('GP2', af=0, mode=Pin.OUT)
|
2015-06-10 17:29:56 -04:00
|
|
|
tim_a.callback(handler=lambda t: p_out.toggle())
|
|
|
|
|
|
|
|
PWM (pulse width modulation)
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
See :ref:`pyb.Pin <pyb.Pin>` and :ref:`pyb.Timer <pyb.Timer>`. ::
|
|
|
|
|
|
|
|
from pyb import Timer
|
|
|
|
from pyb import Pin
|
|
|
|
|
2015-07-28 17:03:53 -04:00
|
|
|
# assign GP25 to alternate function 5 (PWM)
|
|
|
|
p_out = Pin('GP25', af=9, type=Pin.STD)
|
2015-06-10 17:29:56 -04:00
|
|
|
|
|
|
|
# timer 2 in PWM mode and width must be 16 buts
|
|
|
|
tim = Timer(2, mode=Timer.PWM, width=16)
|
|
|
|
|
|
|
|
# enable channel A @1KHz with a 50% duty cycle
|
|
|
|
tim_a = tim.channel(Timer.A, freq=1000, duty_cycle=50)
|
|
|
|
|
|
|
|
ADC (analog to digital conversion)
|
|
|
|
----------------------------------
|
|
|
|
|
|
|
|
See :ref:`pyb.ADC <pyb.ADC>`. ::
|
|
|
|
|
|
|
|
from pyb import ADC
|
|
|
|
|
|
|
|
adc = ADC(1)
|
|
|
|
adc.read() # read value, 0-4095
|
|
|
|
|
|
|
|
UART (serial bus)
|
|
|
|
-----------------
|
|
|
|
|
|
|
|
See :ref:`pyb.Pin <pyb.Pin>` and :ref:`pyb.UART <pyb.UART>`. ::
|
|
|
|
|
|
|
|
from pyb import Pin, UART
|
|
|
|
|
|
|
|
# first assign TX and RX to the correct pins
|
2015-07-28 17:03:53 -04:00
|
|
|
Pin('GP1', af=3, mode=Pin.STD_PU) # TX
|
|
|
|
Pin('GP2', af=3, mode=Pin.STD_PU) # RX
|
2015-06-10 17:29:56 -04:00
|
|
|
|
|
|
|
uart = UART(1, 9600)
|
|
|
|
uart.write('hello')
|
|
|
|
uart.read(5) # read up to 5 bytes
|
|
|
|
|
|
|
|
SPI bus
|
|
|
|
-------
|
|
|
|
|
|
|
|
See :ref:`pyb.Pin <pyb.Pin>` and :ref:`pyb.SPI <pyb.SPI>`. ::
|
|
|
|
|
|
|
|
from pyb import Pin, SPI
|
|
|
|
|
|
|
|
# first assign CLK, MISO, MOSI, CS to the correct pins
|
2015-07-28 17:03:53 -04:00
|
|
|
Pin('GP14', af=7, mode=Pin.STD) # CLK
|
|
|
|
Pin('GP15', af=7, mode=Pin.STD) # MISO
|
|
|
|
Pin('GP16', af=7, mode=Pin.STD) # MOSI
|
|
|
|
Pin('GP17', af=7, mode=Pin.STD) # NSS/CS
|
2015-06-10 17:29:56 -04:00
|
|
|
|
|
|
|
# configure the SPI master @ 2MHz
|
|
|
|
spi = SPI(1, SPI.MASTER, baudrate=200000, polarity=0, phase=0)
|
|
|
|
spi.send('hello')
|
|
|
|
spi.recv(5) # receive 5 bytes on the bus
|
|
|
|
spi.send_recv('hello') # send a receive 5 bytes
|
|
|
|
|
|
|
|
I2C bus
|
|
|
|
-------
|
|
|
|
|
|
|
|
See :ref:`pyb.Pin <pyb.Pin>` and :ref:`pyb.I2C <pyb.I2C>`. ::
|
|
|
|
|
|
|
|
from pyb import Pin, I2C
|
|
|
|
|
|
|
|
# first assign SCL and SDA to the correct pins
|
2015-07-28 17:03:53 -04:00
|
|
|
Pin('GP23', af=9, mode=Pin.STD_PU) # SCL
|
|
|
|
Pin('GP24', af=9, mode=Pin.STD_PU) # SDA
|
2015-06-10 17:29:56 -04:00
|
|
|
|
|
|
|
# configure the I2C bus
|
|
|
|
i2c = I2C(1, I2C.MASTER, baudrate=100000)
|
|
|
|
i2c.scan() # returns list of slave addresses
|
|
|
|
i2c.send('hello', 0x42) # send 5 bytes to slave with address 0x42
|
|
|
|
i2c.recv(5, 0x42) # receive 5 bytes from slave
|
|
|
|
i2c.mem_read(2, 0x42, 0x10) # read 2 bytes from slave 0x42, slave memory 0x10
|
|
|
|
i2c.mem_write('xy', 0x42, 0x10) # write 2 bytes to slave 0x42, slave memory 0x10
|
|
|
|
|
|
|
|
Watchdog timer (WDT)
|
|
|
|
--------------------
|
|
|
|
|
|
|
|
See :ref:`pyb.WDT <pyb.WDT>`. ::
|
|
|
|
|
|
|
|
from pyb import WDT
|
|
|
|
|
|
|
|
# enable the WDT with a timeout of 5s (1s is the minimum)
|
|
|
|
wdt = WDT(5000)
|
|
|
|
wdt.kick()
|
|
|
|
|
|
|
|
Real time clock (RTC)
|
|
|
|
---------------------
|
|
|
|
|
|
|
|
See :ref:`pyb.RTC <pyb.RTC>` and ``pyb.Sleep``. ::
|
|
|
|
|
|
|
|
from pyb import RTC, Sleep
|
|
|
|
|
|
|
|
rtc = pyb.RTC()
|
|
|
|
rtc.datetime((2014, 5, 1, 4, 13, 0, 0, 0))
|
|
|
|
print(rtc.datetime())
|
|
|
|
|
|
|
|
def some_handler (rtc_obj):
|
|
|
|
# trigger the callback again in 30s
|
|
|
|
rtc_obj.callback(value=30000, handler=some_handler)
|
|
|
|
|
|
|
|
# create a RTC alarm that expires in 30s
|
2015-08-11 05:18:02 -04:00
|
|
|
rtc.callback(value=30000, handler=some_handler, wake_from=Sleep.SUSPENDED)
|
2015-06-10 17:29:56 -04:00
|
|
|
|
|
|
|
# go into suspended mode waiting for the RTC alarm to expire and wake us up
|
|
|
|
Sleep.suspend()
|
|
|
|
|
|
|
|
SD card
|
|
|
|
-------
|
|
|
|
|
|
|
|
See :ref:`pyb.SD <pyb.SD>`. ::
|
|
|
|
|
|
|
|
from pyb import SD
|
|
|
|
|
2015-08-11 10:06:43 -04:00
|
|
|
# SD card pins need special configuration so we pass them to the constructor
|
2015-06-10 17:29:56 -04:00
|
|
|
# data pin, data af, clock pin, clock af, cmd pin, cmd af
|
2015-08-11 10:06:43 -04:00
|
|
|
sd = pyb.SD(('GP15', 8, 'GP10', 6, 'GP11', 6))
|
|
|
|
sd.mount()
|
2015-06-10 17:29:56 -04:00
|
|
|
|
|
|
|
WLAN (WiFi)
|
|
|
|
-----------
|
|
|
|
|
|
|
|
See :ref:`network.WLAN <network.WLAN>` and ``pyb.Sleep``. ::
|
|
|
|
|
|
|
|
from network import WLAN
|
|
|
|
from pyb import Sleep
|
|
|
|
|
|
|
|
# configure the WLAN subsystem in station mode (the default is AP)
|
|
|
|
wifi = WLAN(WLAN.STA)
|
|
|
|
# go for fixed IP settings
|
2015-07-28 17:08:15 -04:00
|
|
|
wifi.ifconfig(('192.168.0.107', '255.255.255.0', '192.168.0.1', '8.8.8.8'))
|
2015-06-10 17:29:56 -04:00
|
|
|
wifi.scan() # scan for available netrworks
|
|
|
|
wifi.connect(ssid='mynetwork', security=2, key='mynetworkkey')
|
|
|
|
while not wifi.isconnected():
|
|
|
|
pass
|
|
|
|
print(wifi.ifconfig())
|
|
|
|
# enable wake on WLAN
|
2015-08-11 05:18:02 -04:00
|
|
|
wifi.callback(wake_from=Sleep.SUSPENDED)
|
2015-06-10 17:29:56 -04:00
|
|
|
# go to sleep
|
|
|
|
Sleep.suspend()
|
|
|
|
# now, connect to the FTP or the Telnet server and the WiPy will wake-up
|
|
|
|
|
|
|
|
Sleep and power modes control
|
|
|
|
-----------------------------
|
|
|
|
|
|
|
|
See ``pyb.Sleep``. ::
|
|
|
|
|
|
|
|
from pyb import Sleep
|
|
|
|
|
|
|
|
Sleep.idle() # lowest sleep mode (~12mA), any interrupts wakes it up
|
|
|
|
Sleep.suspend() # everything except for WLAN is powered down (~950uA)
|
|
|
|
# wakes from Pin, RTC or WLAN
|
|
|
|
|
2015-06-11 09:53:31 -04:00
|
|
|
Sleep.hibernate() # deepest sleep mode, MCU starts from reset. Wakes from Pin and RTC.
|
2015-06-10 17:29:56 -04:00
|
|
|
|
|
|
|
Heart beat LED
|
2015-06-11 09:53:31 -04:00
|
|
|
--------------
|
2015-06-10 17:29:56 -04:00
|
|
|
|
|
|
|
See :ref:`pyb.HeartBeat <pyb.HeartBeat>`. ::
|
|
|
|
|
|
|
|
from pyb import HeartBeat
|
|
|
|
|
2015-07-28 17:03:53 -04:00
|
|
|
# disable the heart beat indication (you are free to use this LED connected to GP25)
|
2015-06-11 09:53:31 -04:00
|
|
|
HeartBeat().disable()
|
2015-06-10 17:29:56 -04:00
|
|
|
# enable the heart beat again
|
2015-06-11 09:53:31 -04:00
|
|
|
HeartBeat().enable()
|