33 *
44 * The MIT License (MIT)
55 *
6- * Copyright (c) 2016 Scott Shawcroft
6+ * Copyright (c) 2020 Scott Shawcroft for Adafruit Industries LLC
77 *
88 * Permission is hereby granted, free of charge, to any person obtaining a copy
99 * of this software and associated documentation files (the "Software"), to deal
3030
3131#include "driver/i2c.h"
3232
33- #include "esp_log.h"
34-
3533#include "shared-bindings/microcontroller/__init__.h"
3634#include "supervisor/shared/translate.h"
3735
38- // Number of times to try to send packet if failed.
39- #define ATTEMPTS 2
40-
41- static const char * TAG = "CircuitPython I2C" ;
42-
4336typedef enum {
4437 STATUS_FREE = 0 ,
4538 STATUS_IN_USE ,
@@ -63,42 +56,43 @@ void i2c_reset(void) {
6356
6457void common_hal_busio_i2c_construct (busio_i2c_obj_t * self ,
6558 const mcu_pin_obj_t * scl , const mcu_pin_obj_t * sda , uint32_t frequency , uint32_t timeout ) {
66-
67- // Make sure scl and sda aren't input only
59+ // Pins 45 and 46 are "strapping" pins that impact start up behavior. They usually need to
60+ // be pulled-down so pulling them up for I2C is a bad idea. To make this hard, we don't
61+ // support I2C on these pins.
62+ //
63+ // 46 is also input-only so it'll never work.
64+ if (scl -> number == 45 || scl -> number == 46 || sda -> number == 45 || sda -> number == 46 ) {
65+ mp_raise_ValueError (translate ("Invalid pins" ));
66+ }
6867
6968#if CIRCUITPY_REQUIRE_I2C_PULLUPS
70- // // Test that the pins are in a high state. (Hopefully indicating they are pulled up.)
71- // gpio_set_pin_function(sda->number, GPIO_PIN_FUNCTION_OFF);
72- // gpio_set_pin_function(scl->number, GPIO_PIN_FUNCTION_OFF);
73- // gpio_set_pin_direction(sda->number, GPIO_DIRECTION_IN);
74- // gpio_set_pin_direction(scl->number, GPIO_DIRECTION_IN);
69+ // Test that the pins are in a high state. (Hopefully indicating they are pulled up.)
70+ gpio_set_direction (sda -> number , GPIO_MODE_DEF_INPUT );
71+ gpio_set_direction (scl -> number , GPIO_MODE_DEF_INPUT );
7572
76- // gpio_set_pin_pull_mode (sda->number, GPIO_PULL_DOWN );
77- // gpio_set_pin_pull_mode (scl->number, GPIO_PULL_DOWN );
73+ gpio_pulldown_en (sda -> number );
74+ gpio_pulldown_en (scl -> number );
7875
79- // common_hal_mcu_delay_us(10);
76+ common_hal_mcu_delay_us (10 );
8077
81- // gpio_set_pin_pull_mode (sda->number, GPIO_PULL_OFF );
82- // gpio_set_pin_pull_mode (scl->number, GPIO_PULL_OFF );
78+ gpio_pulldown_dis (sda -> number );
79+ gpio_pulldown_dis (scl -> number );
8380
84- // // We must pull up within 3us to achieve 400khz.
85- // common_hal_mcu_delay_us(3);
81+ // We must pull up within 3us to achieve 400khz.
82+ common_hal_mcu_delay_us (3 );
8683
87- // if (!gpio_get_pin_level (sda->number) || !gpio_get_pin_level (scl->number)) {
88- // reset_pin_number(sda->number);
89- // reset_pin_number(scl->number);
90- // mp_raise_RuntimeError(translate("SDA or SCL needs a pull up"));
91- // }
84+ if (gpio_get_level (sda -> number ) == 0 || gpio_get_level (scl -> number ) == 0 ) {
85+ reset_pin_number (sda -> number );
86+ reset_pin_number (scl -> number );
87+ mp_raise_RuntimeError (translate ("SDA or SCL needs a pull up" ));
88+ }
9289#endif
93- ESP_EARLY_LOGW (TAG , "create I2C" );
9490
9591
9692 self -> semaphore_handle = xSemaphoreCreateBinaryStatic (& self -> semaphore );
9793 xSemaphoreGive (self -> semaphore_handle );
98- ESP_EARLY_LOGW (TAG , "new I2C lock %x" , self -> semaphore_handle );
9994 self -> sda_pin = sda ;
10095 self -> scl_pin = scl ;
101- ESP_EARLY_LOGW (TAG , "scl %d sda %d" , self -> sda_pin -> number , self -> scl_pin -> number );
10296 self -> i2c_num = I2C_NUM_MAX ;
10397 for (i2c_port_t num = 0 ; num < I2C_NUM_MAX ; num ++ ) {
10498 if (i2c_status [num ] == STATUS_FREE ) {
@@ -120,15 +114,10 @@ void common_hal_busio_i2c_construct(busio_i2c_obj_t *self,
120114 .clk_speed = frequency ,
121115 }
122116 };
123- // ESP_EARLY_LOGW(TAG, "param config %p %p %d %d", &i2c_conf, &(i2c_conf.mode), i2c_conf.mode, I2C_MODE_MAX);
124- ESP_EARLY_LOGW (TAG , "param config %p %d %d" , & i2c_conf , i2c_conf .mode , I2C_MODE_MAX );
125117 esp_err_t result = i2c_param_config (self -> i2c_num , & i2c_conf );
126118 if (result != ESP_OK ) {
127- ESP_EARLY_LOGW (TAG , "error %d %p %d %d" , result , & i2c_conf , (& i2c_conf )-> mode , I2C_MODE_MAX );
128- vTaskDelay (0 );
129119 mp_raise_ValueError (translate ("Invalid pins" ));
130120 }
131- ESP_EARLY_LOGW (TAG , "param config I2C done" );
132121 result = i2c_driver_install (self -> i2c_num ,
133122 I2C_MODE_MASTER ,
134123 0 ,
@@ -140,7 +129,6 @@ void common_hal_busio_i2c_construct(busio_i2c_obj_t *self,
140129
141130 claim_pin (sda );
142131 claim_pin (scl );
143- ESP_EARLY_LOGW (TAG , "create I2C done" );
144132}
145133
146134bool common_hal_busio_i2c_deinited (busio_i2c_obj_t * self ) {
@@ -166,19 +154,13 @@ bool common_hal_busio_i2c_probe(busio_i2c_obj_t *self, uint8_t addr) {
166154 i2c_master_start (cmd );
167155 i2c_master_write_byte (cmd , addr << 1 , true);
168156 i2c_master_stop (cmd );
169- esp_err_t result = i2c_master_cmd_begin (self -> i2c_num , cmd , 100 );
157+ esp_err_t result = i2c_master_cmd_begin (self -> i2c_num , cmd , 10 );
170158 i2c_cmd_link_delete (cmd );
171159 return result == ESP_OK ;
172160}
173161
174162bool common_hal_busio_i2c_try_lock (busio_i2c_obj_t * self ) {
175- ESP_EARLY_LOGW (TAG , "locking I2C %x" , self -> semaphore_handle );
176163 self -> has_lock = xSemaphoreTake (self -> semaphore_handle , 0 ) == pdTRUE ;
177- if (self -> has_lock ) {
178- ESP_EARLY_LOGW (TAG , "lock grabbed" );
179- } else {
180- ESP_EARLY_LOGW (TAG , "unable to grab lock" );
181- }
182164 return self -> has_lock ;
183165}
184166
@@ -187,7 +169,6 @@ bool common_hal_busio_i2c_has_lock(busio_i2c_obj_t *self) {
187169}
188170
189171void common_hal_busio_i2c_unlock (busio_i2c_obj_t * self ) {
190- ESP_EARLY_LOGW (TAG , "unlocking I2C" );
191172 xSemaphoreGive (self -> semaphore_handle );
192173 self -> has_lock = false;
193174}
@@ -213,8 +194,7 @@ uint8_t common_hal_busio_i2c_write(busio_i2c_obj_t *self, uint16_t addr,
213194}
214195
215196uint8_t common_hal_busio_i2c_read (busio_i2c_obj_t * self , uint16_t addr ,
216- uint8_t * data , size_t len ) {
217-
197+ uint8_t * data , size_t len ) {
218198 i2c_cmd_handle_t cmd = i2c_cmd_link_create ();
219199 i2c_master_start (cmd );
220200 i2c_master_write_byte (cmd , addr << 1 | 1 , true); // | 1 to indicate read
0 commit comments