circuitpython/ports/raspberrypi/common-hal/busio/I2C.c

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

244 lines
8.0 KiB
C
Raw Normal View History

/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2021 Scott Shawcroft for Adafruit Industries
*
* 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.
*/
2021-03-02 15:16:55 -05:00
#include "py/mperrno.h"
#include "py/mphal.h"
#include "shared-bindings/busio/I2C.h"
#include "py/runtime.h"
#include "shared-bindings/microcontroller/__init__.h"
#include "shared-bindings/microcontroller/Pin.h"
#include "shared-bindings/bitbangio/I2C.h"
#include "src/rp2_common/hardware_gpio/include/hardware/gpio.h"
// Synopsys DW_apb_i2c (v2.01) IP
#define NO_PIN 0xff
// One second
#define BUS_TIMEOUT_US 1000000
STATIC bool never_reset_i2c[2];
2021-03-15 09:57:36 -04:00
STATIC i2c_inst_t *i2c[2] = {i2c0, i2c1};
void reset_i2c(void) {
for (size_t i = 0; i < 2; i++) {
if (never_reset_i2c[i]) {
continue;
}
i2c_deinit(i2c[i]);
}
}
void common_hal_busio_i2c_construct(busio_i2c_obj_t *self,
2021-03-15 09:57:36 -04:00
const mcu_pin_obj_t *scl, const mcu_pin_obj_t *sda, uint32_t frequency, uint32_t timeout) {
self->peripheral = NULL;
// I2C pins have a regular pattern. SCL is always odd and SDA is even. They match up in pairs
// so we can divide by two to get the instance. This pattern repeats.
2021-08-18 11:55:52 -04:00
size_t scl_instance = (scl->number / 2) % 2;
size_t sda_instance = (sda->number / 2) % 2;
if (scl->number % 2 == 1 && sda->number % 2 == 0 && scl_instance == sda_instance) {
self->peripheral = i2c[sda_instance];
}
if (self->peripheral == NULL) {
raise_ValueError_invalid_pins();
}
if ((i2c_get_hw(self->peripheral)->enable & I2C_IC_ENABLE_ENABLE_BITS) != 0) {
mp_raise_ValueError(translate("I2C peripheral in use"));
}
mp_arg_validate_int_max(frequency, 1000000, MP_QSTR_frequency);
2021-03-15 09:57:36 -04:00
#if CIRCUITPY_REQUIRE_I2C_PULLUPS
// Test that the pins are in a high state. (Hopefully indicating they are pulled up.)
gpio_set_function(sda->number, GPIO_FUNC_SIO);
gpio_set_function(scl->number, GPIO_FUNC_SIO);
gpio_set_dir(sda->number, GPIO_IN);
gpio_set_dir(scl->number, GPIO_IN);
gpio_set_pulls(sda->number, false, true);
gpio_set_pulls(scl->number, false, true);
common_hal_mcu_delay_us(10);
gpio_set_pulls(sda->number, false, false);
gpio_set_pulls(scl->number, false, false);
// We must pull up within 3us to achieve 400khz.
common_hal_mcu_delay_us(3);
if (!gpio_get(sda->number) || !gpio_get(scl->number)) {
reset_pin_number(sda->number);
reset_pin_number(scl->number);
mp_raise_RuntimeError(translate("No pull up found on SDA or SCL; check your wiring"));
}
2021-03-15 09:57:36 -04:00
#endif
// Create a bitbangio.I2C object to do 0 byte writes.
//
// These are used to non-invasively detect I2C devices by sending
// the address and confirming an ACK.
// They are not supported by the RP2040 hardware.
//
2021-03-02 15:16:55 -05:00
// Must be done before setting up the I2C pins, since they will be
// set up as GPIO by the bitbangio.I2C object.
2021-03-02 23:18:01 -05:00
//
// Sets pins to open drain, high, and input.
//
// Do not use the default supplied clock stretching timeout here.
// It is too short for some devices. Use the busio timeout instead.
2021-03-02 15:16:55 -05:00
shared_module_bitbangio_i2c_construct(&self->bitbangio_i2c, scl, sda,
frequency, BUS_TIMEOUT_US);
2021-03-02 15:16:55 -05:00
self->baudrate = i2c_init(self->peripheral, frequency);
self->scl_pin = scl->number;
2021-03-02 23:18:01 -05:00
self->sda_pin = sda->number;
claim_pin(scl);
2021-03-02 23:18:01 -05:00
claim_pin(sda);
gpio_set_function(self->scl_pin, GPIO_FUNC_I2C);
gpio_set_function(self->sda_pin, GPIO_FUNC_I2C);
}
bool common_hal_busio_i2c_deinited(busio_i2c_obj_t *self) {
return self->sda_pin == NO_PIN;
}
void common_hal_busio_i2c_deinit(busio_i2c_obj_t *self) {
if (common_hal_busio_i2c_deinited(self)) {
return;
}
never_reset_i2c[i2c_hw_index(self->peripheral)] = false;
i2c_deinit(self->peripheral);
reset_pin_number(self->sda_pin);
reset_pin_number(self->scl_pin);
self->sda_pin = NO_PIN;
self->scl_pin = NO_PIN;
}
bool common_hal_busio_i2c_probe(busio_i2c_obj_t *self, uint8_t addr) {
2022-01-31 22:03:30 -05:00
return common_hal_busio_i2c_write(self, addr, NULL, 0) == 0;
}
bool common_hal_busio_i2c_try_lock(busio_i2c_obj_t *self) {
bool grabbed_lock = false;
if (!self->has_lock) {
grabbed_lock = true;
self->has_lock = true;
}
return grabbed_lock;
}
bool common_hal_busio_i2c_has_lock(busio_i2c_obj_t *self) {
return self->has_lock;
}
void common_hal_busio_i2c_unlock(busio_i2c_obj_t *self) {
self->has_lock = false;
}
2022-01-31 22:03:30 -05:00
STATIC uint8_t _common_hal_busio_i2c_write(busio_i2c_obj_t *self, uint16_t addr,
codeformat: Fix filename matching In #4683, tannewt noticed that uncrustify was not running on some file in common-hal. I investigated and found that it was not being run on a bunch of paths. Rather than make incremental changes, I rewrote list_files to work bsaed on regular expressions; these regular expressions are created from the same git-style glob patterns. I spot-checked some specific filenames after this change, and all looks good: ``` $ python3 tools/codeformat.py -v --dry-run tests/basics/int_small.py ports/raspberrypi/common-hal/pulseio/PulseIn.c extmod/virtpin.c tests/thread/thread_exit1.py ports/raspberrypi/background.h extmod/re1.5/recursiveloop.c tools/codeformat.py -v --dry-run tests/basics/int_small.py ports/raspberrypi/common-hal/pulseio/PulseIn.c extmod/virtpin.c tests/thread/thread_exit1.py ports/raspberrypi/background.h extmod/re1.5/recursiveloop.c uncrustify -c /home/jepler/src/circuitpython/tools/uncrustify.cfg -lC --no-backup extmod/virtpin.c ports/raspberrypi/background.h ports/raspberrypi/common-hal/pulseio/PulseIn.c black --fast --line-length=99 -v tests/thread/thread_exit1.py ``` recursiveloop and int_small are excluded, while PulseIn, virtpin, and background are included. Testing running from a subdirectory (not _specifically_ supported though): ``` (cd ports && python3 ../tools/codeformat.py -v --dry-run raspberrypi/common-hal/pulseio/PulseIn.c ../extmod/virtpin.c) ../tools/codeformat.py -v --dry-run raspberrypi/common-hal/pulseio/PulseIn.c ../extmod/virtpin.c uncrustify -c /home/jepler/src/circuitpython/tools/uncrustify.cfg -lC --no-backup ../extmod/virtpin.c raspberrypi/common-hal/pulseio/PulseIn. ``` As a side-effect, a bunch more files are re-formatted now. :-P
2021-04-30 11:47:37 -04:00
const uint8_t *data, size_t len, bool transmit_stop_bit) {
if (len == 0) {
// The RP2040 I2C peripheral will not perform 0 byte writes.
2021-03-02 23:18:01 -05:00
// So use bitbangio.I2C to do the write.
2021-03-02 15:16:55 -05:00
gpio_set_function(self->scl_pin, GPIO_FUNC_SIO);
2021-03-02 23:18:01 -05:00
gpio_set_function(self->sda_pin, GPIO_FUNC_SIO);
2021-03-02 15:16:55 -05:00
gpio_set_dir(self->scl_pin, GPIO_IN);
2021-03-02 23:18:01 -05:00
gpio_set_dir(self->sda_pin, GPIO_IN);
gpio_put(self->scl_pin, false);
gpio_put(self->sda_pin, false);
2021-03-02 15:16:55 -05:00
uint8_t status = shared_module_bitbangio_i2c_write(&self->bitbangio_i2c,
2021-03-15 09:57:36 -04:00
addr, data, len, transmit_stop_bit);
2021-03-02 23:18:01 -05:00
// The pins must be set back to GPIO_FUNC_I2C in the order given here,
// SCL first, otherwise reads will hang.
gpio_set_function(self->scl_pin, GPIO_FUNC_I2C);
2021-03-02 23:18:01 -05:00
gpio_set_function(self->sda_pin, GPIO_FUNC_I2C);
return status;
}
int result = i2c_write_timeout_us(self->peripheral, addr, data, len, !transmit_stop_bit, BUS_TIMEOUT_US);
if (result == len) {
return 0;
}
switch (result) {
case PICO_ERROR_GENERIC:
return MP_ENODEV;
case PICO_ERROR_TIMEOUT:
return MP_ETIMEDOUT;
default:
return MP_EIO;
}
}
2022-01-31 22:03:30 -05:00
uint8_t common_hal_busio_i2c_write(busio_i2c_obj_t *self, uint16_t addr,
const uint8_t *data, size_t len) {
return _common_hal_busio_i2c_write(self, addr, data, len, true);
}
uint8_t common_hal_busio_i2c_read(busio_i2c_obj_t *self, uint16_t addr,
2021-03-15 09:57:36 -04:00
uint8_t *data, size_t len) {
int result = i2c_read_timeout_us(self->peripheral, addr, data, len, false, BUS_TIMEOUT_US);
if (result == len) {
return 0;
}
switch (result) {
case PICO_ERROR_GENERIC:
return MP_ENODEV;
case PICO_ERROR_TIMEOUT:
return MP_ETIMEDOUT;
default:
return MP_EIO;
}
}
2022-01-31 22:03:30 -05:00
uint8_t common_hal_busio_i2c_write_read(busio_i2c_obj_t *self, uint16_t addr,
uint8_t *out_data, size_t out_len, uint8_t *in_data, size_t in_len) {
uint8_t result = _common_hal_busio_i2c_write(self, addr, out_data, out_len, false);
if (result != 0) {
return result;
}
return common_hal_busio_i2c_read(self, addr, in_data, in_len);
}
void common_hal_busio_i2c_never_reset(busio_i2c_obj_t *self) {
never_reset_i2c[i2c_hw_index(self->peripheral)] = true;
never_reset_pin_number(self->scl_pin);
never_reset_pin_number(self->sda_pin);
}