3
3
*
4
4
* The MIT License (MIT)
5
5
*
6
- * Copyright (c) 2016 Scott Shawcroft
6
+ * Copyright (c) 2020 Scott Shawcroft for Adafruit Industries LLC
7
7
*
8
8
* Permission is hereby granted, free of charge, to any person obtaining a copy
9
9
* of this software and associated documentation files (the "Software"), to deal
30
30
31
31
#include "driver/i2c.h"
32
32
33
- #include "esp_log.h"
34
-
35
33
#include "shared-bindings/microcontroller/__init__.h"
36
34
#include "supervisor/shared/translate.h"
37
35
38
- // Number of times to try to send packet if failed.
39
- #define ATTEMPTS 2
40
-
41
- static const char * TAG = "CircuitPython I2C" ;
42
-
43
36
typedef enum {
44
37
STATUS_FREE = 0 ,
45
38
STATUS_IN_USE ,
@@ -63,42 +56,43 @@ void i2c_reset(void) {
63
56
64
57
void common_hal_busio_i2c_construct (busio_i2c_obj_t * self ,
65
58
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
+ }
68
67
69
68
#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 );
75
72
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 );
78
75
79
- // common_hal_mcu_delay_us(10);
76
+ common_hal_mcu_delay_us (10 );
80
77
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 );
83
80
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 );
86
83
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
+ }
92
89
#endif
93
- ESP_EARLY_LOGW (TAG , "create I2C" );
94
90
95
91
96
92
self -> semaphore_handle = xSemaphoreCreateBinaryStatic (& self -> semaphore );
97
93
xSemaphoreGive (self -> semaphore_handle );
98
- ESP_EARLY_LOGW (TAG , "new I2C lock %x" , self -> semaphore_handle );
99
94
self -> sda_pin = sda ;
100
95
self -> scl_pin = scl ;
101
- ESP_EARLY_LOGW (TAG , "scl %d sda %d" , self -> sda_pin -> number , self -> scl_pin -> number );
102
96
self -> i2c_num = I2C_NUM_MAX ;
103
97
for (i2c_port_t num = 0 ; num < I2C_NUM_MAX ; num ++ ) {
104
98
if (i2c_status [num ] == STATUS_FREE ) {
@@ -120,15 +114,10 @@ void common_hal_busio_i2c_construct(busio_i2c_obj_t *self,
120
114
.clk_speed = frequency ,
121
115
}
122
116
};
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 );
125
117
esp_err_t result = i2c_param_config (self -> i2c_num , & i2c_conf );
126
118
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 );
129
119
mp_raise_ValueError (translate ("Invalid pins" ));
130
120
}
131
- ESP_EARLY_LOGW (TAG , "param config I2C done" );
132
121
result = i2c_driver_install (self -> i2c_num ,
133
122
I2C_MODE_MASTER ,
134
123
0 ,
@@ -140,7 +129,6 @@ void common_hal_busio_i2c_construct(busio_i2c_obj_t *self,
140
129
141
130
claim_pin (sda );
142
131
claim_pin (scl );
143
- ESP_EARLY_LOGW (TAG , "create I2C done" );
144
132
}
145
133
146
134
bool 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) {
166
154
i2c_master_start (cmd );
167
155
i2c_master_write_byte (cmd , addr << 1 , true);
168
156
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 );
170
158
i2c_cmd_link_delete (cmd );
171
159
return result == ESP_OK ;
172
160
}
173
161
174
162
bool common_hal_busio_i2c_try_lock (busio_i2c_obj_t * self ) {
175
- ESP_EARLY_LOGW (TAG , "locking I2C %x" , self -> semaphore_handle );
176
163
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
- }
182
164
return self -> has_lock ;
183
165
}
184
166
@@ -187,7 +169,6 @@ bool common_hal_busio_i2c_has_lock(busio_i2c_obj_t *self) {
187
169
}
188
170
189
171
void common_hal_busio_i2c_unlock (busio_i2c_obj_t * self ) {
190
- ESP_EARLY_LOGW (TAG , "unlocking I2C" );
191
172
xSemaphoreGive (self -> semaphore_handle );
192
173
self -> has_lock = false;
193
174
}
@@ -213,8 +194,7 @@ uint8_t common_hal_busio_i2c_write(busio_i2c_obj_t *self, uint16_t addr,
213
194
}
214
195
215
196
uint8_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 ) {
218
198
i2c_cmd_handle_t cmd = i2c_cmd_link_create ();
219
199
i2c_master_start (cmd );
220
200
i2c_master_write_byte (cmd , addr << 1 | 1 , true); // | 1 to indicate read
0 commit comments