Skip to content
Snippets Groups Projects
Select Git revision
  • f872c735c849b88874ee1386f13875c96212c85c
  • master default protected
  • blink_multi_rocket
  • blink_rocket
  • msgctl/gfx_rle
  • msgctl/faultscreen
  • msgctl/textbuffer_api
  • schneider/bonding
  • schneider/bootloader-update-9a0d158
  • schneider/bsec
  • rahix/bma
  • rahix/bhi
  • schleicher-test
  • schneider/schleicher-test
  • freertos-btle
  • ch3/api-speed-eval2
  • schneider/mp-for-old-bl
  • ch3/leds-api
  • ch3/genapi-refactor
  • ch3/dual-core
  • dualcore
  • v0.0
22 results

os.c

Blame
  • Forked from card10 / firmware
    Source project has a limited visibility.
    machine_i2c.c 6.37 KiB
    /*
     * This file is part of the MicroPython project, http://micropython.org/
     *
     * The MIT License (MIT)
     *
     * Copyright (c) 2019 Damien P. George
     *
     * Permission is hereby granted, free of charge, to any person obtaining a copy
     * of this software and associated documentation files (the "Software"), to deal
     * in the Software without restriction, including without limitation the rights
     * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     * copies of the Software, and to permit persons to whom the Software is
     * furnished to do so, subject to the following conditions:
     *
     * The above copyright notice and this permission notice shall be included in
     * all copies or substantial portions of the Software.
     *
     * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
     * THE SOFTWARE.
     */
    
    #include "py/runtime.h"
    #include "py/mphal.h"
    #include "py/mperrno.h"
    #include "extmod/machine_i2c.h"
    
    #include "driver/i2c.h"
    
    #define I2C_0_DEFAULT_SCL (GPIO_NUM_18)
    #define I2C_0_DEFAULT_SDA (GPIO_NUM_19)
    #define I2C_1_DEFAULT_SCL (GPIO_NUM_25)
    #define I2C_1_DEFAULT_SDA (GPIO_NUM_26)
    
    #define I2C_DEFAULT_TIMEOUT_US (10000) // 10ms
    
    typedef struct _machine_hw_i2c_obj_t {
        mp_obj_base_t base;
        i2c_port_t port : 8;
        gpio_num_t scl : 8;
        gpio_num_t sda : 8;
    } machine_hw_i2c_obj_t;
    
    STATIC const mp_obj_type_t machine_hw_i2c_type;
    STATIC machine_hw_i2c_obj_t machine_hw_i2c_obj[I2C_NUM_MAX];
    
    STATIC void machine_hw_i2c_init(machine_hw_i2c_obj_t *self, uint32_t freq, uint32_t timeout_us, bool first_init) {
        if (!first_init) {
            i2c_driver_delete(self->port);
        }
        i2c_config_t conf = {
            .mode = I2C_MODE_MASTER,
            .sda_io_num = self->sda,
            .sda_pullup_en = GPIO_PULLUP_ENABLE,
            .scl_io_num = self->scl,
            .scl_pullup_en = GPIO_PULLUP_ENABLE,
            .master.clk_speed = freq,
        };
        i2c_param_config(self->port, &conf);
        i2c_set_timeout(self->port, I2C_APB_CLK_FREQ / 1000000 * timeout_us);
        i2c_driver_install(self->port, I2C_MODE_MASTER, 0, 0, 0);
    }
    
    int machine_hw_i2c_transfer(mp_obj_base_t *self_in, uint16_t addr, size_t n, mp_machine_i2c_buf_t *bufs, unsigned int flags) {
        machine_hw_i2c_obj_t *self = MP_OBJ_TO_PTR(self_in);