|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2018 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 | +#include "shared-bindings/busio/I2C.h" |
| 28 | +#include "shared-bindings/busio/SPI.h" |
| 29 | +#include "shared-bindings/busio/UART.h" |
| 30 | + |
| 31 | +#include "shared-bindings/microcontroller/Pin.h" |
| 32 | +#include "supervisor/shared/translate.h" |
| 33 | +#include "mpconfigboard.h" |
| 34 | +#include "nrf/pins.h" |
| 35 | +#include "py/runtime.h" |
| 36 | + |
| 37 | +#if !defined(DEFAULT_I2C_BUS_SDA) || !defined(DEFAULT_I2C_BUS_SCL) |
| 38 | + STATIC mp_obj_t board_i2c(void) { |
| 39 | + mp_raise_NotImplementedError(translate("No default I2C bus")); |
| 40 | + return NULL; |
| 41 | + } |
| 42 | +#else |
| 43 | + STATIC mp_obj_t i2c_singleton = NULL; |
| 44 | + |
| 45 | + STATIC mp_obj_t board_i2c(void) { |
| 46 | + |
| 47 | + if (i2c_singleton == NULL) { |
| 48 | + busio_i2c_obj_t *self = m_new_obj(busio_i2c_obj_t); |
| 49 | + self->base.type = &busio_i2c_type; |
| 50 | + |
| 51 | + assert_pin_free(DEFAULT_I2C_BUS_SDA); |
| 52 | + assert_pin_free(DEFAULT_I2C_BUS_SCL); |
| 53 | + common_hal_busio_i2c_construct(self, DEFAULT_I2C_BUS_SCL, DEFAULT_I2C_BUS_SDA, 400000, 0); |
| 54 | + i2c_singleton = (mp_obj_t)self; |
| 55 | + } |
| 56 | + return i2c_singleton; |
| 57 | + |
| 58 | + } |
| 59 | +#endif |
| 60 | +MP_DEFINE_CONST_FUN_OBJ_0(board_i2c_obj, board_i2c); |
| 61 | + |
| 62 | +#if !defined(DEFAULT_SPI_BUS_SCK) || !defined(DEFAULT_SPI_BUS_MISO) || !defined(DEFAULT_SPI_BUS_MOSI) |
| 63 | + STATIC mp_obj_t board_spi(void) { |
| 64 | + mp_raise_NotImplementedError(translate("No default SPI bus")); |
| 65 | + return NULL; |
| 66 | + } |
| 67 | +#else |
| 68 | + STATIC mp_obj_t spi_singleton = NULL; |
| 69 | + |
| 70 | + STATIC mp_obj_t board_spi(void) { |
| 71 | + |
| 72 | + if (spi_singleton == NULL) { |
| 73 | + busio_spi_obj_t *self = m_new_obj(busio_spi_obj_t); |
| 74 | + self->base.type = &busio_spi_type; |
| 75 | + assert_pin_free(DEFAULT_SPI_BUS_SCK); |
| 76 | + assert_pin_free(DEFAULT_SPI_BUS_MOSI); |
| 77 | + assert_pin_free(DEFAULT_SPI_BUS_MISO); |
| 78 | + const mcu_pin_obj_t* clock = MP_OBJ_TO_PTR(DEFAULT_SPI_BUS_SCK); |
| 79 | + const mcu_pin_obj_t* mosi = MP_OBJ_TO_PTR(DEFAULT_SPI_BUS_MOSI); |
| 80 | + const mcu_pin_obj_t* miso = MP_OBJ_TO_PTR(DEFAULT_SPI_BUS_MISO); |
| 81 | + common_hal_busio_spi_construct(self, clock, mosi, miso); |
| 82 | + spi_singleton = (mp_obj_t)self; |
| 83 | + } |
| 84 | + return spi_singleton; |
| 85 | + } |
| 86 | +#endif |
| 87 | +MP_DEFINE_CONST_FUN_OBJ_0(board_spi_obj, board_spi); |
| 88 | + |
| 89 | +#if !defined(DEFAULT_UART_BUS_RX) || !defined(DEFAULT_UART_BUS_TX) |
| 90 | + STATIC mp_obj_t board_uart(void) { |
| 91 | + mp_raise_NotImplementedError(translate("No default UART bus")); |
| 92 | + return NULL; |
| 93 | + } |
| 94 | +#else |
| 95 | + STATIC mp_obj_t uart_singleton = NULL; |
| 96 | + |
| 97 | + STATIC mp_obj_t board_uart(void) { |
| 98 | + if (uart_singleton == NULL) { |
| 99 | + busio_uart_obj_t *self = m_new_obj(busio_uart_obj_t); |
| 100 | + self->base.type = &busio_uart_type; |
| 101 | + |
| 102 | + assert_pin_free(DEFAULT_UART_BUS_RX); |
| 103 | + assert_pin_free(DEFAULT_UART_BUS_TX); |
| 104 | + |
| 105 | + const mcu_pin_obj_t* rx = MP_OBJ_TO_PTR(DEFAULT_UART_BUS_RX); |
| 106 | + const mcu_pin_obj_t* tx = MP_OBJ_TO_PTR(DEFAULT_UART_BUS_TX); |
| 107 | + |
| 108 | + common_hal_busio_uart_construct(self, tx, rx, 9600, 8, PARITY_NONE, 1, 1000, 64); |
| 109 | + uart_singleton = (mp_obj_t)self; |
| 110 | + } |
| 111 | + return uart_singleton; |
| 112 | + } |
| 113 | +#endif |
| 114 | +MP_DEFINE_CONST_FUN_OBJ_0(board_uart_obj, board_uart); |
0 commit comments