|
| 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/microcontroller/Pin.h" |
| 29 | +#include "mpconfigboard.h" |
| 30 | +#include "pins.h" |
| 31 | +#include "py/runtime.h" |
| 32 | + |
| 33 | + |
| 34 | + |
| 35 | +#if !defined(DEFAULT_I2C_BUS_SDA) || !defined(DEFAULT_I2C_BUS_SCL) |
| 36 | + |
| 37 | + STATIC mp_obj_t board_i2c(void) { |
| 38 | + //board_i2c_obj = NULL; |
| 39 | + mp_raise_NotImplementedError("No default I2C bus"); |
| 40 | + return NULL; |
| 41 | + } |
| 42 | + |
| 43 | +#else |
| 44 | + STATIC mp_obj_t i2c_singleton = NULL; |
| 45 | + |
| 46 | + STATIC mp_obj_t board_i2c(void) { |
| 47 | + |
| 48 | + if (i2c_singleton == NULL) { |
| 49 | + busio_i2c_obj_t *self = m_new_obj(busio_i2c_obj_t); |
| 50 | + self->base.type = &busio_i2c_type; |
| 51 | + |
| 52 | + assert_pin_free(DEFAULT_I2C_BUS_SDA); |
| 53 | + assert_pin_free(DEFAULT_I2C_BUS_SCL); |
| 54 | + common_hal_busio_i2c_construct(self, DEFAULT_I2C_BUS_SCL, DEFAULT_I2C_BUS_SDA, 400000, 0); |
| 55 | + i2c_singleton = (mp_obj_t)self; |
| 56 | + } |
| 57 | + return i2c_singleton; |
| 58 | + |
| 59 | + } |
| 60 | +#endif |
| 61 | + |
| 62 | +MP_DEFINE_CONST_FUN_OBJ_0(board_i2c_obj, board_i2c); |
0 commit comments