<commit_before>/*
 * This file is part of the MicroPython project, http://micropython.org/
 *
 * The MIT License (MIT)
 *
 * Copyright (c) 2016 <NAME>
 *
 * 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 "shared-bindings/busio/I2C.h"
#include "py/mperrno.h"
#include "py/runtime.h"

#include "hal/include/hal_gpio.h"
#include "hal/include/hal_i2c_m_sync.h"
#include "hal/include/hpl_i2c_m_sync.h"

#include "peripherals.h"
#include "pins.h"


// Number of times to try to send packet if failed.
#define ATTEMPTS 2

void common_hal_busio_i2c_construct(busio_i2c_obj_t *self,
        const mcu_pin_obj_t* scl, const mcu_pin_obj_t* sda, uint32_t frequency) {
    Sercom* sercom = NULL;
    uint8_t sercom_index;
    uint32_t sda_pinmux = 0;
    uint32_t scl_pinmux = 0;
    for (int i = 0; i < NUM_SERCOMS_PER_PIN; i++) {
        sercom_index = sda->sercom[i].index;
        if (sercom_index >= SERCOM_INST_NUM) {
            continue;
        }
        Sercom* potential_sercom = sercom_insts[sercom_index];
        if (potential_sercom->I2CM.CTRLA.bit.ENABLE != 0 ||
            sda->sercom[i].pad != 0) {
            continue;
        }
        sda_pinmux = PINMUX(sda->pin, (i == 0) ? MUX_C : MUX_D);
        for (int j = 0; j < NUM_SERCOMS_PER_PIN; j++) {
            if (sercom_index == scl->sercom[j].index &&
                scl->sercom[j].pad == 1) {
                scl_pinmux = PINMUX(scl->pin, (j == 0) ? MUX_C : MUX_D);
                sercom = potential_sercom;
                break;
            }
        }
        if (sercom != NULL) {
            break;
        }
    }
    if (sercom == NULL) {
        mp_raise_ValueError("Invalid pins");
    }

    // Test that the pins are in a high state. (Hopefully indicating they are pulled up.)
    gpio_set_pin_function(sda->pin, GPIO_PIN_FUNCTION_OFF);
    gpio_set_pin_function(scl->pin, GPIO_PIN_FUNCTION_OFF);
    gpio_set_pin_direction(sda->pin, GPIO_DIRECTION_IN);
    gpio_set_pin_direction(scl->pin, GPIO_DIRECTION_IN);
    gpio_set_pin_pull_mode(sda->pin, GPIO_PULL_OFF);
    gpio_set_pin_pull_mode(scl->pin, GPIO_PULL_OFF);

    if (!gpio_get_pin_level(sda->pin) || !gpio_get_pin_level(scl->pin)) {
        mp_raise_RuntimeError("SDA or SCL needs a pull up");
    }
    gpio_set_pin_function(sda->pin, sda_pinmux);
    gpio_set_pin_function(scl->pin, scl_pinmux);

    // Set up I2C clocks on sercom.
    samd_peripherals_sercom_clock_init(sercom, sercom_index);

    if (i2c_m_sync_init(&self->i2c_desc, sercom) != ERR_NONE) {
            mp_raise_OSError(MP_EIO);
    }

    // clkrate is always 0. baud_rate is in kHz.

    // Frequency must be set before the I2C device is enabled.
    if (i2c_m_sync_set_baudrate(&self->i2c_desc, 0, frequency / 1000) != ERR_NONE) {
        mp_raise_ValueError("Unsupported baudrate");
    }

    self->sda_pin = sda->pin;
    self->scl_pin = scl->pin;
    claim_pin(sda);
    claim_pin(scl);

    if (i2c_m_sync_enable(&self->i2c_desc) != ERR_NONE) {
        common_hal_busio_i2c_deinit(self);
        mp_raise_OSError(MP_EIO);
    }
}

bool common_hal_busio_i2c_deinited(busio_i2c_obj_t *self) {
    return self->sda_pin == NO_PIN;<commit_msg>Check for floating pins by pulling them low briefly before testing their values.<commit_after>/*
 * This file is part of the MicroPython project, http://micropython.org/
 *
 * The MIT License (MIT)
 *
 * Copyright (c) 2016 <NAME>
 *
 * 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 "shared-bindings/busio/I2C.h"
#include "py/mperrno.h"
#include "py/runtime.h"

#include "hal/include/hal_gpio.h"
#include "hal/include/hal_i2c_m_sync.h"
#include "hal/include/hpl_i2c_m_sync.h"

#include "peripherals.h"
#include "pins.h"
#include "shared-bindings/microcontroller/__init__.h"


// Number of times to try to send packet if failed.
#define ATTEMPTS 2

void common_hal_busio_i2c_construct(busio_i2c_obj_t *self,
        const mcu_pin_obj_t* scl, const mcu_pin_obj_t* sda, uint32_t frequency) {
    Sercom* sercom = NULL;
    uint8_t sercom_index;
    uint32_t sda_pinmux = 0;
    uint32_t scl_pinmux = 0;
    for (int i = 0; i < NUM_SERCOMS_PER_PIN; i++) {
        sercom_index = sda->sercom[i].index;
        if (sercom_index >= SERCOM_INST_NUM) {
            continue;
        }
        Sercom* potential_sercom = sercom_insts[sercom_index];
        if (potential_sercom->I2CM.CTRLA.bit.ENABLE != 0 ||
            sda->sercom[i].pad != 0) {
            continue;
        }
        sda_pinmux = PINMUX(sda->pin, (i == 0) ? MUX_C : MUX_D);
        for (int j = 0; j < NUM_SERCOMS_PER_PIN; j++) {
            if (sercom_index == scl->sercom[j].index &&
                scl->sercom[j].pad == 1) {
                scl_pinmux = PINMUX(scl->pin, (j == 0) ? MUX_C : MUX_D);
                sercom = potential_sercom;
                break;
            }
        }
        if (sercom != NULL) {
            break;
        }
    }
    if (sercom == NULL) {
        mp_raise_ValueError("Invalid pins");
    }

    // Test that the pins are in a high state. (Hopefully indicating they are pulled up.)
    gpio_set_pin_function(sda->pin, GPIO_PIN_FUNCTION_OFF);
    gpio_set_pin_function(scl->pin, GPIO_PIN_FUNCTION_OFF);
    gpio_set_pin_direction(sda->pin, GPIO_DIRECTION_IN);
    gpio_set_pin_direction(scl->pin, GPIO_DIRECTION_IN);

    gpio_set_pin_pull_mode(sda->pin, GPIO_PULL_DOWN);
    gpio_set_pin_pull_mode(scl->pin, GPIO_PULL_DOWN);

    common_hal_mcu_delay_us(10);

    gpio_set_pin_pull_mode(sda->pin, GPIO_PULL_OFF);
    gpio_set_pin_pull_mode(scl->pin, GPIO_PULL_OFF);

    // We must pull up within 3us to achieve 400khz.
    common_hal_mcu_delay_us(3);

    if (!gpio_get_pin_level(sda->pin) || !gpio_get_pin_level(scl->pin)) {
        reset_pin(sda->pin);
        reset_pin(scl->pin);
        mp_raise_RuntimeError("SDA or SCL needs a pull up");
    }
    gpio_set_pin_function(sda->pin, sda_pinmux);
    gpio_set_pin_function(scl->pin, scl_pinmux);

    // Set up I2C clocks on sercom.
    samd_peripherals_sercom_clock_init(sercom, sercom_index);

    if (i2c_m_sync_init(&self->i2c_desc, sercom) != ERR_NONE) {
        reset_pin(sda->pin);
        reset_pin(scl->pin);
        mp_raise_OSError(MP_EIO);
    }

    // clkrate is always 0. baud_rate is in kHz.

    // Frequency must be set before the I2C device is enabled.
    if (i2c_m_sync_set_baudrate(&self->i2c_desc, 0, frequency / 1000) != ERR_NONE) {
        reset_pin(sda->pin);
        reset_pin(scl->pin);
        mp_raise_ValueError("Unsupported baudrate");
    }

    self->sda_pin = sda->pin;
    self->scl_pin = scl->pin;
    claim_pin(sda);
    claim_pin(scl);

    if (i2c_m_sync_enable(&self->i2c_desc) != ERR_NONE) {
        common_hal_busio_i2c_deinit(self);
        mp_raise_OSError(MP_EIO);
    }
}

bool common_hal_busio_i2c_deinited(busio_i2c_obj_t *self) {
    return self->sda_pin == NO_PIN;
{'fraction_non_alphanumeric': 0.10419242441272339, 'fraction_numerical': 0.010677756547150726, 'mean_word_length': 3.1482517482517482, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 65, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '809013', 'n_tokens_mistral': 3257, 'n_tokens_neox': 3096, 'n_words': 806}