quickref.rst 5.86 KB
Newer Older
1
.. _quickref_:
2
3
4
5
6
7
8
9

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

10
11
12
General board control (including sleep modes)
---------------------------------------------

13
See the :mod:`machine` module::
14

15
    import machine
16

17
18
19
    help(machine) # display all members from the machine module
    machine.freq() # get the CPU frequency
    machine.unique_id() # return the 6-byte unique id of the board (the WiPy's MAC address)
20

21
    machine.idle()        # average current decreases to (~12mA), any interrupts wake it up
22
23
24
    machine.sleep()       # everything except for WLAN is powered down (~950uA avg. current)
                          # wakes from Pin, RTC or WLAN
    machine.deepsleep()   # deepest sleep mode, MCU starts from reset. Wakes from Pin and RTC.
25
26
27
28

Pins and GPIO
-------------

29
See :ref:`machine.Pin <machine.Pin>`. ::
30

31
    from machine import Pin
32

33
    # initialize GP2 in gpio mode (alt=0) and make it an output
34
    p_out = Pin('GP2', mode=Pin.OUT)
35
36
    p_out.value(1)
    p_out.value(0)
37
    p_out.toggle()
38
    p_out(True)
39

40
    # make GP1 an input with the pull-up enabled
41
    p_in = Pin('GP1', mode=Pin.IN, pull=Pin.PULL_UP)
42
    p_in() # get value, 0 or 1
43
44
45
46

Timers
------

47
See :ref:`machine.Timer <machine.Timer>` and :ref:`machine.Pin <machine.Pin>`. ::
48

49
50
    from machine import Timer
    from machine import Pin
51

52
    tim = Timer(0, mode=Timer.PERIODIC)
53
    tim_a = tim.channel(Timer.A, freq=1000)
54
    tim_a.freq(5) # 5 Hz
55
    
56
    p_out = Pin('GP2', mode=Pin.OUT)
juhasch's avatar
juhasch committed
57
    tim_a.irq(trigger=Timer.TIMEOUT, handler=lambda t: p_out.toggle())
58
59
60
61

PWM (pulse width modulation)
----------------------------

62
See :ref:`machine.Pin <machine.Pin>` and :ref:`machine.Timer <machine.Timer>`. ::
63

64
    from machine import Timer
65

66
67
    # timer 1 in PWM mode and width must be 16 buts
    tim = Timer(1, mode=Timer.PWM, width=16)
68
    
69
70
    # enable channel A @1KHz with a 50.55% duty cycle
    tim_a = tim.channel(Timer.A, freq=1000, duty_cycle=5055)
71
72
73
74

ADC (analog to digital conversion)
----------------------------------

75
See :ref:`machine.ADC <machine.ADC>`. ::
76

77
    from machine import ADC
78

79
80
81
    adc = ADC()
    apin = adc.channel(pin='GP3')
    apin() # read value, 0-4095
82
83
84
85

UART (serial bus)
-----------------

86
See :ref:`machine.UART <machine.UART>`. ::
87

88
    from machine import UART
89
    uart = UART(0, baudrate=9600)
90
91
92
93
94
95
    uart.write('hello')
    uart.read(5) # read up to 5 bytes

SPI bus
-------

96
See :ref:`machine.SPI <machine.SPI>`. ::
97

98
    from machine import SPI
99
100

    # configure the SPI master @ 2MHz
101
102
103
104
105
    spi = SPI(0, SPI.MASTER, baudrate=200000, polarity=0, phase=0)
    spi.write('hello')
    spi.read(5) # receive 5 bytes on the bus
    rbuf = bytearray(5)
    spi.write_readinto('hello', rbuf) # send a receive 5 bytes
106
107
108
109

I2C bus
-------

110
See :ref:`machine.I2C <machine.I2C>`. ::
111

112
    from machine import I2C
113
    # configure the I2C bus
114
    i2c = I2C(0, I2C.MASTER, baudrate=100000)
115
    i2c.scan() # returns list of slave addresses
116
117
118
119
    i2c.writeto(0x42, 'hello') # send 5 bytes to slave with address 0x42
    i2c.readfrom(0x42, 5) # receive 5 bytes from slave
    i2c.readfrom_mem(0x42, 0x10, 2) # read 2 bytes from slave 0x42, slave memory 0x10
    i2c.writeto_mem(0x42, 0x10, 'xy') # write 2 bytes to slave 0x42, slave memory 0x10
120
121
122
123

Watchdog timer (WDT)
--------------------

124
See :ref:`machine.WDT <machine.WDT>`. ::
125

126
    from machine import WDT
127
128

    # enable the WDT with a timeout of 5s (1s is the minimum)
129
130
131
    wdt = WDT(timeout=5000)
    wdt.feed()

132
133
134
Real time clock (RTC)
---------------------

135
See :ref:`machine.RTC <machine.RTC>` ::
136

137
    from machine import RTC
138

juhasch's avatar
juhasch committed
139
    rtc = RTC() # init with default time and date
140
141
    rtc = RTC(datetime=(2015, 8, 29, 9, 0, 0, 0, None)) # init with a specific time and date
    print(rtc.now())
142

143
144
145
146
147
    def alarm_handler (rtc_o):
        pass
        # do some non blocking operations
        # warning printing on an irq via telnet is not
        # possible, only via UART 
148

149
150
151
152
153
    # create a RTC alarm that expires after 5 seconds
    rtc.alarm(time=5000, repeat=False)

    # enable RTC interrupts
    rtc_i = rtc.irq(trigger=RTC.ALARM0, handler=alarm_handler, wake=machine.SLEEP)
154
155

    # go into suspended mode waiting for the RTC alarm to expire and wake us up
156
    machine.sleep()
157
158
159
160

SD card
-------

161
See :ref:`machine.SD <machine.SD>`. ::
162

163
    from machine import SD
164
    import os
165

166
167
    # clock pin, cmd pin, data0 pin
    sd = SD(pins=('GP10', 'GP11', 'GP15'))
168
169
    # or use default ones for the expansion board
    sd = SD()
170
    os.mount(sd, '/sd')
171
172
173
174

WLAN (WiFi) 
-----------

175
See :ref:`network.WLAN <network.WLAN>` and :mod:`machine`. ::
176

177
    import machine
178
179
180
    from network import WLAN

    # configure the WLAN subsystem in station mode (the default is AP)
181
    wlan = WLAN(mode=WLAN.STA)
182
    # go for fixed IP settings
183
184
185
186
    wlan.ifconfig(config=('192.168.0.107', '255.255.255.0', '192.168.0.1', '8.8.8.8'))
    wlan.scan()     # scan for available networks
    wlan.connect(ssid='mynetwork', auth=(WLAN.WPA2, 'mynetworkkey'))
    while not wlan.isconnected():
187
        pass
188
    print(wlan.ifconfig())
189
    # enable wake on WLAN
190
    wlan.irq(trigger=WLAN.ANY_EVENT, wake=machine.SLEEP)
191
    # go to sleep
192
    machine.sleep()
193
194
    # now, connect to the FTP or the Telnet server and the WiPy will wake-up

195
196
197
Telnet and FTP server
---------------------

198
See :ref:`network.Server <network.Server>` ::
199

200
    from network import Server
201

202
    # init with new user, password and seconds timeout
203
    server = Server(login=('user', 'password'), timeout=60)
204
205
    server.timeout(300) # change the timeout
    server.timeout() # get the timeout
Mike Causer's avatar
Mike Causer committed
206
    server.isrunning() # check whether the server is running or not
207

208
209
Heart beat LED
--------------
210

211
See :mod:`wipy`. ::
212

213
    import wipy
214

215
216
217
    wipy.heartbeat(False)  # disable the heartbeat LED
    wipy.heartbeat(True)   # enable the heartbeat LED
    wipy.heartbeat()       # get the heartbeat state