Skip to content

Commit 78477a3

Browse files
committed
Initial SPI commit
1 parent e7da852 commit 78477a3

File tree

5 files changed

+304
-0
lines changed

5 files changed

+304
-0
lines changed

py/circuitpy_defns.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,7 @@ SRC_SHARED_MODULE_ALL = \
437437
board/__init__.c \
438438
busdevice/__init__.c \
439439
busdevice/I2CDevice.c \
440+
busdevice/SPIDevice.c \
440441
busio/OneWire.c \
441442
displayio/Bitmap.c \
442443
displayio/ColorConverter.c \

shared-bindings/busdevice/SPIDevice.c

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2020 Mark Komus
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
// This file contains all of the Python API definitions for the
28+
// busio.SPI class.
29+
30+
#include "shared-bindings/microcontroller/Pin.h"
31+
#include "shared-bindings/busdevice/SPIDevice.h"
32+
#include "shared-bindings/util.h"
33+
#include "shared-module/busdevice/SPIDevice.h"
34+
#include "common-hal/digitalio/DigitalInOut.h"
35+
#include "shared-bindings/digitalio/DigitalInOut.h"
36+
37+
38+
#include "lib/utils/buffer_helper.h"
39+
#include "lib/utils/context_manager_helpers.h"
40+
#include "py/runtime.h"
41+
#include "supervisor/shared/translate.h"
42+
43+
44+
//| class SPIDevice:
45+
//| """
46+
//| Represents a single SPI device and manages locking the bus and the device
47+
//| address.
48+
//| :param ~busio.SPI spi: The SPI bus the device is on
49+
//| :param int device_address: The 7 bit device address
50+
//| :param bool probe: Probe for the device upon object creation, default is true
51+
//| .. note:: This class is **NOT** built into CircuitPython. See
52+
//| :ref:`here for install instructions <bus_device_installation>`.
53+
//| Example:
54+
//| .. code-block:: python
55+
//| import busio
56+
//| from board import *
57+
//| from adafruit_bus_device.spi_device import SPIDevice
58+
//| with busio.SPI(SCL, SDA) as spi:
59+
//| device = SPIDevice(spi, 0x70)
60+
//| bytes_read = bytearray(4)
61+
//| with device:
62+
//| device.readinto(bytes_read)
63+
//| # A second transaction
64+
//| with device:
65+
//| device.write(bytes_read)"""
66+
//| ...
67+
//|
68+
STATIC mp_obj_t busdevice_spidevice_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
69+
busdevice_spidevice_obj_t *self = m_new_obj(busdevice_spidevice_obj_t);
70+
self->base.type = &busdevice_spidevice_type;
71+
enum { ARG_spi, ARG_chip_select, ARG_baudrate, ARG_polarity, ARG_phase, ARG_extra_clocks };
72+
static const mp_arg_t allowed_args[] = {
73+
{ MP_QSTR_spi, MP_ARG_REQUIRED | MP_ARG_OBJ },
74+
{ MP_QSTR_chip_select, MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
75+
{ MP_QSTR_baudrate, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 100000} },
76+
{ MP_QSTR_polarity, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
77+
{ MP_QSTR_phase, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
78+
{ MP_QSTR_extra_clocks, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
79+
};
80+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
81+
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
82+
83+
busio_spi_obj_t* spi = args[ARG_spi].u_obj;
84+
85+
common_hal_busdevice_spidevice_construct(MP_OBJ_TO_PTR(self), spi, args[ARG_chip_select].u_obj, args[ARG_baudrate].u_int, args[ARG_polarity].u_int,
86+
args[ARG_phase].u_int, args[ARG_extra_clocks].u_int);
87+
88+
if (args[ARG_chip_select].u_obj != MP_OBJ_NULL) {
89+
digitalinout_result_t result = common_hal_digitalio_digitalinout_switch_to_output(MP_OBJ_TO_PTR(args[ARG_chip_select].u_obj),
90+
true, DRIVE_MODE_PUSH_PULL);
91+
if (result == DIGITALINOUT_INPUT_ONLY) {
92+
mp_raise_NotImplementedError(translate("Pin is input only"));
93+
}
94+
}
95+
96+
return (mp_obj_t)self;
97+
}
98+
99+
STATIC mp_obj_t busdevice_spidevice_obj___enter__(mp_obj_t self_in) {
100+
busdevice_spidevice_obj_t *self = MP_OBJ_TO_PTR(self_in);
101+
common_hal_busdevice_spidevice_enter(self);
102+
return self;
103+
}
104+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(busdevice_spidevice___enter___obj, busdevice_spidevice_obj___enter__);
105+
106+
STATIC mp_obj_t busdevice_spidevice_obj___exit__(size_t n_args, const mp_obj_t *args) {
107+
common_hal_busdevice_spidevice_exit(MP_OBJ_TO_PTR(args[0]));
108+
return mp_const_none;
109+
}
110+
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(busdevice_spidevice___exit___obj, 4, 4, busdevice_spidevice_obj___exit__);
111+
112+
STATIC const mp_rom_map_elem_t busdevice_spidevice_locals_dict_table[] = {
113+
{ MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&busdevice_spidevice___enter___obj) },
114+
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&busdevice_spidevice___exit___obj) },
115+
};
116+
117+
STATIC MP_DEFINE_CONST_DICT(busdevice_spidevice_locals_dict, busdevice_spidevice_locals_dict_table);
118+
119+
const mp_obj_type_t busdevice_spidevice_type = {
120+
{ &mp_type_type },
121+
.name = MP_QSTR_SPIDevice,
122+
.make_new = busdevice_spidevice_make_new,
123+
.locals_dict = (mp_obj_dict_t*)&busdevice_spidevice_locals_dict,
124+
};

shared-bindings/busdevice/SPIDevice.h

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2020 Mark Komus
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
// Machine is the HAL for low-level, hardware accelerated functions. It is not
28+
// meant to simplify APIs, its only meant to unify them so that other modules
29+
// do not require port specific logic.
30+
//
31+
// This file includes externs for all functions a port should implement to
32+
// support the machine module.
33+
34+
#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_BUSDEVICE_SPIDEVICE_H
35+
#define MICROPY_INCLUDED_SHARED_BINDINGS_BUSDEVICE_SPIDEVICE_H
36+
37+
#include "py/obj.h"
38+
39+
#include "shared-module/busdevice/SPIDevice.h"
40+
41+
// Type object used in Python. Should be shared between ports.
42+
extern const mp_obj_type_t busdevice_spidevice_type;
43+
44+
// Initializes the hardware peripheral.
45+
extern void common_hal_busdevice_spidevice_construct(busdevice_spidevice_obj_t *self, busio_spi_obj_t *spi, digitalio_digitalinout_obj_t *cs,
46+
uint32_t baudrate, uint8_t polarity, uint8_t phase, uint8_t extra_clocks);
47+
extern void common_hal_busdevice_spidevice_enter(busdevice_spidevice_obj_t *self);
48+
extern void common_hal_busdevice_spidevice_exit(busdevice_spidevice_obj_t *self);
49+
50+
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_BUSDEVICE_SPIDEVICE_H

shared-module/busdevice/SPIDevice.c

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2020 Mark Komus
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include "shared-bindings/busdevice/SPIDevice.h"
28+
#include "shared-bindings/busio/SPI.h"
29+
#include "shared-bindings/digitalio/DigitalInOut.h"
30+
#include "py/mperrno.h"
31+
#include "py/nlr.h"
32+
#include "py/runtime.h"
33+
34+
void common_hal_busdevice_spidevice_construct(busdevice_spidevice_obj_t *self, busio_spi_obj_t *spi, digitalio_digitalinout_obj_t *cs,
35+
uint32_t baudrate, uint8_t polarity, uint8_t phase, uint8_t extra_clocks) {
36+
self->spi = spi;
37+
self->baudrate = baudrate;
38+
self->polarity = polarity;
39+
self->phase = phase;
40+
self->extra_clocks = extra_clocks;
41+
self->chip_select = cs;
42+
}
43+
44+
void common_hal_busdevice_spidevice_enter(busdevice_spidevice_obj_t *self) {
45+
bool success = false;
46+
while (!success) {
47+
success = common_hal_busio_spi_try_lock(self->spi);
48+
RUN_BACKGROUND_TASKS;
49+
mp_handle_pending();
50+
}
51+
52+
common_hal_busio_spi_configure(self->spi, self->baudrate, self->polarity, self->phase, 8);
53+
54+
if (self->chip_select != MP_OBJ_NULL) {
55+
common_hal_digitalio_digitalinout_set_value(MP_OBJ_TO_PTR(self->chip_select), false);
56+
}
57+
}
58+
59+
void common_hal_busdevice_spidevice_exit(busdevice_spidevice_obj_t *self) {
60+
if (self->chip_select != MP_OBJ_NULL) {
61+
common_hal_digitalio_digitalinout_set_value(MP_OBJ_TO_PTR(self->chip_select), true);
62+
}
63+
64+
if (self->extra_clocks > 0) {
65+
66+
mp_buffer_info_t bufinfo;
67+
mp_obj_t buffer = mp_obj_new_bytearray_of_zeros(1);
68+
69+
mp_get_buffer_raise(buffer, &bufinfo, MP_BUFFER_READ);
70+
((uint8_t*)bufinfo.buf)[0] = 0xFF;
71+
72+
uint8_t clocks = self->extra_clocks / 8;
73+
if ((self->extra_clocks % 8) != 0)
74+
clocks += 1;
75+
76+
while (clocks > 0) {
77+
if (!common_hal_busio_spi_write(self->spi, ((uint8_t*)bufinfo.buf), 1)) {
78+
mp_raise_OSError(MP_EIO);
79+
}
80+
clocks--;
81+
}
82+
}
83+
84+
common_hal_busio_spi_unlock(self->spi);
85+
}

shared-module/busdevice/SPIDevice.h

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#ifndef MICROPY_INCLUDED_ATMEL_SAMD_SHARED_MODULE_BUSDEVICE_SPIDEVICE_H
28+
#define MICROPY_INCLUDED_ATMEL_SAMD_SHARED_MODULE_BUSDEVICE_SPIDEVICE_H
29+
30+
#include "py/obj.h"
31+
#include "common-hal/busio/SPI.h"
32+
#include "common-hal/digitalio/DigitalInOut.h"
33+
34+
typedef struct {
35+
mp_obj_base_t base;
36+
busio_spi_obj_t *spi;
37+
uint32_t baudrate;
38+
uint8_t polarity;
39+
uint8_t phase;
40+
uint8_t extra_clocks;
41+
digitalio_digitalinout_obj_t *chip_select;
42+
} busdevice_spidevice_obj_t;
43+
44+
#endif // MICROPY_INCLUDED_ATMEL_SAMD_SHARED_MODULE_BUSDEVICE_SPIDEVICE_H

0 commit comments

Comments
 (0)