38
38
//| =================================================
39
39
//|
40
40
//| .. module:: machine
41
- //| :synopsis: functions related to the board
41
+ //| :synopsis: functions related to the board
42
+ //| :platform: SAMD21
42
43
//|
43
44
//| The ``machine`` module contains specific functions related to the board.
44
45
//|
45
- //| This is soon to be renamed to distinguish it from upstream's machine!
46
+ //| This is soon to be renamed to distinguish it from upstream's ` machine` !
46
47
//|
47
- //| .. currentmodule:: machine
48
+ //| :class:`I2C` --- Two wire serial protocol
49
+ //| ------------------------------------------
48
50
//|
49
- //| class I2C -- a two-wire serial protocol
50
- //| =======================================
51
+ //| .. class:: I2C(scl, sda, \*, freq=400000)
51
52
//|
52
- //| I2C is a two-wire protocol for communicating between devices. At the
53
- //| physical level it consists of 2 wires: SCL and SDA, the clock and data lines
54
- //| respectively.
53
+ //| I2C is a two-wire protocol for communicating between devices. At the
54
+ //| physical level it consists of 2 wires: SCL and SDA, the clock and data lines
55
+ //| respectively.
55
56
//|
56
- //| I2C objects are created attached to a specific bus. They can be initialised
57
- //| when created, or initialised later on.
57
+ //| I2C objects are created attached to a specific bus. They can be initialised
58
+ //| when created, or initialised later on.
58
59
//|
59
- //| Constructors
60
- //| ------------
61
- //| .. class:: I2C(scl, sda, \*, freq=400000)
60
+ //| :param str scl: The clock pin
61
+ //| :param str sda: The data pin
62
+ //| :param int freq: The clock frequency
62
63
//|
63
- //| Construct and return a new I2C object.
64
- //| See the init method below for a description of the arguments.
65
64
STATIC mp_obj_t machine_i2c_make_new (const mp_obj_type_t * type , size_t n_args , size_t n_kw , const mp_obj_t * pos_args ) {
66
65
mp_arg_check_num (n_args , n_kw , 0 , MP_OBJ_FUN_ARGS_MAX , true);
67
66
machine_i2c_obj_t * self = m_new_obj (machine_i2c_obj_t );
@@ -83,15 +82,22 @@ STATIC mp_obj_t machine_i2c_make_new(const mp_obj_type_t *type, size_t n_args, s
83
82
return (mp_obj_t )self ;
84
83
}
85
84
86
- //| .. method:: I2C.init()
85
+ //| .. method:: I2C.init()
86
+ //|
87
+ //| Initializes control of the underlying hardware so other classes cannot
88
+ //| use it.
89
+ //|
87
90
STATIC mp_obj_t machine_i2c_obj_init (mp_obj_t self_in ) {
88
91
machine_i2c_obj_t * self = MP_OBJ_TO_PTR (self_in );
89
92
mp_hal_i2c_init (self );
90
93
return self_in ;
91
94
}
92
95
MP_DEFINE_CONST_FUN_OBJ_1 (machine_i2c_init_obj , machine_i2c_obj_init );
93
96
94
- //| .. method:: I2C.deinit()
97
+ //| .. method:: I2C.deinit()
98
+ //|
99
+ //| Releases control of the underlying hardware so other classes can use it.
100
+ //|
95
101
STATIC mp_obj_t machine_i2c_obj_deinit (mp_obj_t self_in ) {
96
102
machine_i2c_obj_t * self = MP_OBJ_TO_PTR (self_in );
97
103
mp_hal_i2c_deinit (self );
@@ -106,11 +112,11 @@ STATIC mp_obj_t machine_i2c_obj___exit__(size_t n_args, const mp_obj_t *args) {
106
112
}
107
113
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (machine_i2c_obj___exit___obj , 4 , 4 , machine_i2c_obj___exit__ );
108
114
109
- //| .. method:: I2C.scan()
115
+ //| .. method:: I2C.scan()
110
116
//|
111
- //| Scan all I2C addresses between 0x08 and 0x77 inclusive and return a list of
112
- //| those that respond. A device responds if it pulls the SDA line low after
113
- //| its address (including a read bit) is sent on the bus.
117
+ //| Scan all I2C addresses between 0x08 and 0x77 inclusive and return a list of
118
+ //| those that respond. A device responds if it pulls the SDA line low after
119
+ //| its address (including a read bit) is sent on the bus.
114
120
//|
115
121
STATIC mp_obj_t machine_i2c_scan (mp_obj_t self_in ) {
116
122
machine_i2c_obj_t * self = MP_OBJ_TO_PTR (self_in );
@@ -126,16 +132,14 @@ STATIC mp_obj_t machine_i2c_scan(mp_obj_t self_in) {
126
132
}
127
133
MP_DEFINE_CONST_FUN_OBJ_1 (machine_i2c_scan_obj , machine_i2c_scan );
128
134
129
- //| Standard bus operations
130
- //| -----------------------
135
+ //| .. method:: I2C.readfrom(addr, nbytes)
131
136
//|
132
- //| The following methods implement the standard I2C master read and write
133
- //| operations that target a given slave device.
137
+ //| Read `nbytes` from the slave specified by `addr`.
134
138
//|
135
- //| .. method:: I2C.readfrom( addr, nbytes)
136
- //|
137
- //| Read `nbytes` from the slave specified by `addr`.
138
- //| Returns a ` bytes` object with the data read.
139
+ //| :param int addr: The 7 bit address of the device
140
+ //| :param int nbytes: The number of bytes to read
141
+ //| :return: the data read
142
+ //| :rtype: bytes
139
143
//|
140
144
STATIC mp_obj_t machine_i2c_readfrom (mp_obj_t self_in , mp_obj_t addr_in , mp_obj_t nbytes_in ) {
141
145
machine_i2c_obj_t * self = MP_OBJ_TO_PTR (self_in );
@@ -146,13 +150,10 @@ STATIC mp_obj_t machine_i2c_readfrom(mp_obj_t self_in, mp_obj_t addr_in, mp_obj_
146
150
}
147
151
MP_DEFINE_CONST_FUN_OBJ_3 (machine_i2c_readfrom_obj , machine_i2c_readfrom );
148
152
149
- //| .. method:: I2C.readfrom_into(addr, buf)
150
- //|
151
- //| Read into `buf` from the slave specified by `addr`.
152
- //| The number of bytes read will be the length of `buf`.
153
+ //| .. method:: I2C.readfrom_into(addr, buf)
153
154
//|
154
- //| On WiPy the return value is the number of bytes read. Otherwise the
155
- //| return value is `None `.
155
+ //| Read into `buf` from the slave specified by `addr`.
156
+ //| The number of bytes read will be the length of `buf `.
156
157
//|
157
158
STATIC mp_obj_t machine_i2c_readfrom_into (mp_obj_t self_in , mp_obj_t addr_in , mp_obj_t buf_in ) {
158
159
machine_i2c_obj_t * self = MP_OBJ_TO_PTR (self_in );
@@ -163,9 +164,9 @@ STATIC mp_obj_t machine_i2c_readfrom_into(mp_obj_t self_in, mp_obj_t addr_in, mp
163
164
}
164
165
MP_DEFINE_CONST_FUN_OBJ_3 (machine_i2c_readfrom_into_obj , machine_i2c_readfrom_into );
165
166
166
- //| .. method:: I2C.writeto(addr, buf)
167
+ //| .. method:: I2C.writeto(addr, buf)
167
168
//|
168
- //| Write the bytes from `buf` to the slave specified by `addr`.
169
+ //| Write the bytes from `buf` to the slave specified by `addr`.
169
170
//|
170
171
STATIC mp_obj_t machine_i2c_writeto (mp_obj_t self_in , mp_obj_t addr_in , mp_obj_t buf_in ) {
171
172
machine_i2c_obj_t * self = MP_OBJ_TO_PTR (self_in );
@@ -176,22 +177,13 @@ STATIC mp_obj_t machine_i2c_writeto(mp_obj_t self_in, mp_obj_t addr_in, mp_obj_t
176
177
}
177
178
STATIC MP_DEFINE_CONST_FUN_OBJ_3 (machine_i2c_writeto_obj , machine_i2c_writeto );
178
179
179
- //| Memory operations
180
- //| -----------------
180
+ //| .. method:: I2C.readfrom_mem(addr, memaddr, nbytes, \*, addrsize=8)
181
181
//|
182
- //| Some I2C devices act as a memory device (or set of registers) that can be
183
- //| read from and written to. In this case there are two addresses associated
184
- //| with an I2C transaction: the slave address and the memory address. The following
185
- //| following methods are convenience functions to communicate with such
186
- //| devices.
187
- //|
188
- //| .. method:: I2C.readfrom_mem(addr, memaddr, nbytes, \*, addrsize=8)
189
- //|
190
- //| Read `nbytes` from the slave specified by `addr` starting from the memory
191
- //| address specified by `memaddr`.
192
- //| The argument `addrsize` specifies the address size in bits (on ESP8266
193
- //| this argument is not recognised and the address size is always 8 bits).
194
- //| Returns a `bytes` object with the data read.
182
+ //| Read `nbytes` from the slave specified by `addr` starting from the memory
183
+ //| address specified by `memaddr`.
184
+ //| The argument `addrsize` specifies the address size in bits (on ESP8266
185
+ //| this argument is not recognised and the address size is always 8 bits).
186
+ //| Returns a `bytes` object with the data read.
195
187
//|
196
188
STATIC mp_obj_t machine_i2c_readfrom_mem (size_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
197
189
enum { ARG_addr , ARG_memaddr , ARG_n , ARG_addrsize };
@@ -216,16 +208,13 @@ STATIC mp_obj_t machine_i2c_readfrom_mem(size_t n_args, const mp_obj_t *pos_args
216
208
MP_DEFINE_CONST_FUN_OBJ_KW (machine_i2c_readfrom_mem_obj , 1 , machine_i2c_readfrom_mem );
217
209
218
210
219
- //| .. method:: I2C.readfrom_mem_into(addr, memaddr, buf, \*, addrsize=8)
211
+ //| .. method:: I2C.readfrom_mem_into(addr, memaddr, buf, \*, addrsize=8)
220
212
//|
221
- //| Read into `buf` from the slave specified by `addr` starting from the
222
- //| memory address specified by `memaddr`. The number of bytes read is the
223
- //| length of `buf`.
224
- //| The argument `addrsize` specifies the address size in bits (on ESP8266
225
- //| this argument is not recognised and the address size is always 8 bits).
226
- //|
227
- //| On WiPy the return value is the number of bytes read. Otherwise the
228
- //| return value is `None`.
213
+ //| Read into `buf` from the slave specified by `addr` starting from the
214
+ //| memory address specified by `memaddr`. The number of bytes read is the
215
+ //| length of `buf`.
216
+ //| The argument `addrsize` specifies the address size in bits (on ESP8266
217
+ //| this argument is not recognised and the address size is always 8 bits).
229
218
//|
230
219
STATIC mp_obj_t machine_i2c_readfrom_mem_into (size_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
231
220
enum { ARG_addr , ARG_memaddr , ARG_buf , ARG_addrsize };
@@ -249,15 +238,12 @@ STATIC mp_obj_t machine_i2c_readfrom_mem_into(size_t n_args, const mp_obj_t *pos
249
238
}
250
239
MP_DEFINE_CONST_FUN_OBJ_KW (machine_i2c_readfrom_mem_into_obj , 1 , machine_i2c_readfrom_mem_into );
251
240
252
- //| .. method:: I2C.writeto_mem(addr, memaddr, buf, \*, addrsize=8)
253
- //|
254
- //| Write `buf` to the slave specified by `addr` starting from the
255
- //| memory address specified by `memaddr`.
256
- //| The argument `addrsize` specifies the address size in bits (on ESP8266
257
- //| this argument is not recognised and the address size is always 8 bits).
241
+ //| .. method:: I2C.writeto_mem(addr, memaddr, buf, \*, addrsize=8)
258
242
//|
259
- //| On WiPy the return value is the number of bytes written. Otherwise the
260
- //| return value is `None`.
243
+ //| Write `buf` to the slave specified by `addr` starting from the
244
+ //| memory address specified by `memaddr`.
245
+ //| The argument `addrsize` specifies the address size in bits (on ESP8266
246
+ //| this argument is not recognised and the address size is always 8 bits).
261
247
//|
262
248
STATIC mp_obj_t machine_i2c_writeto_mem (size_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
263
249
enum { ARG_addr , ARG_memaddr , ARG_buf , ARG_addrsize };
@@ -309,31 +295,29 @@ const mp_obj_type_t machine_i2c_type = {
309
295
.locals_dict = (mp_obj_dict_t * )& machine_i2c_locals_dict ,
310
296
};
311
297
312
- //| class SPI -- a master-driven serial protocol
313
- //| ============================================
298
+ //| : class:` SPI` -- a 3-4 wire serial protocol
299
+ //| -----------------------------------------------
314
300
//|
315
- //| SPI is a serial protocol that is driven by a master. This class only
316
- //| manages three of the four SPI lines: SCK, MOSI, MISO. Its up to the client
317
- //| to manage the appropriate slave select line.
301
+ //| SPI is a serial protocol that has exlusive pins for data in and out of the
302
+ //| master. It is typically faster than `I2C` because a separate pin is used to
303
+ //| control the active slave rather than a transitted address. This class only
304
+ //| manages three of the four SPI lines: `clock`, `MOSI`, `MISO`. Its up to the
305
+ //| client to manage the appropriate slave select line. (This is common because
306
+ //| multiple slaves can share the `clock`, `MOSI` and `MISO` lines and therefore
307
+ //| the hardware.)
318
308
//|
319
- //| Constructors
320
- //| ------------
309
+ //| .. class:: SPI(clock, MOSI, MISO, baudrate=1000000)
321
310
//|
322
- //| .. class:: SPI(clock, MOSI, MISO, baudrate=1000000)
311
+ //| Construct an SPI object on the given bus. ``id`` can be only 0.
312
+ //| With no additional parameters, the SPI object is created but not
313
+ //| initialised (it has the settings from the last initialisation of
314
+ //| the bus, if any). If extra arguments are given, the bus is initialised.
315
+ //| See ``init`` for parameters of initialisation.
323
316
//|
324
- //| Construct an SPI object on the given bus. ``id`` can be only 0.
325
- //| With no additional parameters, the SPI object is created but not
326
- //| initialised (it has the settings from the last initialisation of
327
- //| the bus, if any). If extra arguments are given, the bus is initialised.
328
- //| See ``init`` for parameters of initialisation.
329
- //|
330
- //| - ``clock`` is the pin to use for the clock.
331
- //| - ``MOSI`` is the Master Out Slave In pin.
332
- //| - ``MISO`` is the Master In Slave Out pin.
333
- //| - ``baudrate`` is the SCK clock rate.
334
- //|
335
- //| Methods
336
- //| -------
317
+ //| - ``clock`` is the pin to use for the clock.
318
+ //| - ``MOSI`` is the Master Out Slave In pin.
319
+ //| - ``MISO`` is the Master In Slave Out pin.
320
+ //| - ``baudrate`` is the SCK clock rate.
337
321
//|
338
322
339
323
// TODO(tannewt): Support LSB SPI.
@@ -364,9 +348,9 @@ STATIC mp_obj_t machine_spi_make_new(const mp_obj_type_t *type, size_t n_args, s
364
348
return (mp_obj_t )self ;
365
349
}
366
350
367
- //| .. method:: SPI.init()
351
+ //| .. method:: SPI.init()
368
352
//|
369
- //| Initialises the bus.
353
+ //| Initialises the bus.
370
354
//|
371
355
STATIC mp_obj_t machine_spi_obj_init (mp_obj_t self_in ) {
372
356
machine_spi_obj_t * self = MP_OBJ_TO_PTR (self_in );
@@ -375,9 +359,9 @@ STATIC mp_obj_t machine_spi_obj_init(mp_obj_t self_in) {
375
359
}
376
360
MP_DEFINE_CONST_FUN_OBJ_1 (machine_spi_init_obj , machine_spi_obj_init );
377
361
378
- //| .. method:: SPI.deinit()
362
+ //| .. method:: SPI.deinit()
379
363
//|
380
- //| Turn off the SPI bus.
364
+ //| Turn off the SPI bus.
381
365
//|
382
366
STATIC mp_obj_t machine_spi_obj_deinit (mp_obj_t self_in ) {
383
367
machine_spi_obj_t * self = MP_OBJ_TO_PTR (self_in );
@@ -393,11 +377,11 @@ STATIC mp_obj_t machine_spi_obj___exit__(size_t n_args, const mp_obj_t *args) {
393
377
}
394
378
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (machine_spi_obj___exit___obj , 4 , 4 , machine_spi_obj___exit__ );
395
379
396
- //| .. method:: SPI.write_readinto(write_buf, read_buf)
380
+ //| .. method:: SPI.write_readinto(write_buf, read_buf)
397
381
//|
398
- //| Write from ``write_buf`` and read into ``read_buf``. Both buffers must have the
399
- //| same length. This is the same as a SPI transfer function on other platforms.
400
- //| Returns the number of bytes written
382
+ //| Write from ``write_buf`` and read into ``read_buf``. Both buffers must have the
383
+ //| same length. This is the same as a SPI transfer function on other platforms.
384
+ //| Returns the number of bytes written
401
385
//|
402
386
STATIC mp_obj_t mp_machine_spi_write_readinto (mp_obj_t self_in , mp_obj_t wr_buf , mp_obj_t rd_buf ) {
403
387
mp_buffer_info_t src ;
@@ -413,15 +397,10 @@ STATIC mp_obj_t mp_machine_spi_write_readinto(mp_obj_t self_in, mp_obj_t wr_buf,
413
397
}
414
398
MP_DEFINE_CONST_FUN_OBJ_3 (mp_machine_spi_write_readinto_obj , mp_machine_spi_write_readinto );
415
399
416
- //| Helper operations
417
- //| -----------------
418
- //| The below operations are finer grained operations based upon ``SPI.write_readinto``.
419
- //| They may be moved out of the core module later.
420
- //|
421
- //| .. method:: SPI.write(buf)
400
+ //| .. method:: SPI.write(buf)
422
401
//|
423
- //| Write the data contained in ``buf``.
424
- //| Returns the number of bytes written.
402
+ //| Write the data contained in ``buf``.
403
+ //| Returns the number of bytes written.
425
404
//|
426
405
STATIC mp_obj_t mp_machine_spi_write (mp_obj_t self_in , mp_obj_t wr_buf ) {
427
406
mp_buffer_info_t src ;
@@ -432,10 +411,10 @@ STATIC mp_obj_t mp_machine_spi_write(mp_obj_t self_in, mp_obj_t wr_buf) {
432
411
}
433
412
MP_DEFINE_CONST_FUN_OBJ_2 (mp_machine_spi_write_obj , mp_machine_spi_write );
434
413
435
- //| .. method:: SPI.read(nbytes, *, write=0x00)
414
+ //| .. method:: SPI.read(nbytes, *, write=0x00)
436
415
//|
437
- //| Read the ``nbytes`` while writing the data specified by ``write``.
438
- //| Return the number of bytes read.
416
+ //| Read the ``nbytes`` while writing the data specified by ``write``.
417
+ //| Return the number of bytes read.
439
418
//|
440
419
STATIC mp_obj_t mp_machine_spi_read (size_t n_args , const mp_obj_t * args ) {
441
420
vstr_t vstr ;
@@ -446,11 +425,11 @@ STATIC mp_obj_t mp_machine_spi_read(size_t n_args, const mp_obj_t *args) {
446
425
}
447
426
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (mp_machine_spi_read_obj , 2 , 3 , mp_machine_spi_read );
448
427
449
- //| .. method:: SPI.readinto(buf, *, write=0x00)
428
+ //| .. method:: SPI.readinto(buf, *, write=0x00)
450
429
//|
451
- //| Read into the buffer specified by ``buf`` while writing the data specified by
452
- //| ``write``.
453
- //| Return the number of bytes read.
430
+ //| Read into the buffer specified by ``buf`` while writing the data
431
+ //| specified by ``write``.
432
+ //| Return the number of bytes read.
454
433
//|
455
434
STATIC mp_obj_t mp_machine_spi_readinto (size_t n_args , const mp_obj_t * args ) {
456
435
mp_buffer_info_t bufinfo ;
0 commit comments