Skip to content

Commit 2433c95

Browse files
committed
Add full pin mux info and use it for I2C
All I2C peripherals should be usable now. This also adds pin in-use tracking and resetting. Part of #5629
1 parent b83e098 commit 2433c95

File tree

7 files changed

+111
-154
lines changed

7 files changed

+111
-154
lines changed

ports/broadcom/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ SRC_C += bindings/videocore/__init__.c \
6363
lib/tinyusb/src/portable/synopsys/dwc2/dcd_dwc2.c \
6464
peripherals/broadcom/caches.c \
6565
peripherals/broadcom/gen/interrupt_handlers.c \
66+
peripherals/broadcom/gen/pins.c \
6667
peripherals/broadcom/gpio.c \
6768
peripherals/broadcom/interrupts.c \
6869
peripherals/broadcom/mmu.c \
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2021 Scott Shawcroft for Adafruit Industries
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#pragma once
28+
29+
#include "py/obj.h"
30+
31+
extern const mp_obj_type_t mcu_pin_type;
32+
33+
#define PIN_PREFIX_VALUES { &mcu_pin_type },
34+
#define PIN_PREFIX_FIELDS mp_obj_base_t base;

ports/broadcom/common-hal/busio/I2C.c

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,35 +35,63 @@
3535
#include "peripherals/broadcom/cpu.h"
3636
#include "peripherals/broadcom/vcmailbox.h"
3737

38-
#define NUM_I2C (2)
38+
#if BCM_VERSION == 2711
39+
#define NUM_I2C (8)
40+
STATIC BSC0_Type *i2c[NUM_I2C] = {BSC0, BSC1, NULL, BSC3, BSC4, BSC5, BSC6, NULL};
41+
#else
42+
#define NUM_I2C (3)
43+
STATIC BSC0_Type *i2c[NUM_I2C] = {BSC0, BSC1, NULL};
44+
#endif
3945

4046
STATIC bool never_reset_i2c[NUM_I2C];
41-
STATIC bool in_use_i2c[NUM_I2C];
42-
STATIC BSC0_Type *i2c[2] = {BSC0, BSC1};
47+
STATIC bool i2c_in_use[NUM_I2C];
4348

4449
void reset_i2c(void) {
50+
// BSC2 is dedicated to the first HDMI output.
51+
never_reset_i2c[2] = true;
52+
i2c_in_use[2] = true;
53+
#if BCM_VERSION == 2711
54+
// BSC7 is dedicated to the second HDMI output.
55+
never_reset_i2c[7] = true;
56+
i2c_in_use[7] = true;
57+
#endif
4558
for (size_t i = 0; i < 2; i++) {
4659
if (never_reset_i2c[i]) {
4760
continue;
4861
}
49-
in_use_i2c[i] = false;
62+
i2c_in_use[i] = false;
63+
i2c[i]->C_b.I2CEN = false;
64+
COMPLETE_MEMORY_READS;
5065
}
5166
}
5267

5368
void common_hal_busio_i2c_construct(busio_i2c_obj_t *self,
5469
const mcu_pin_obj_t *scl, const mcu_pin_obj_t *sda, uint32_t frequency, uint32_t timeout) {
5570
size_t instance_index = NUM_I2C;
56-
if ((scl == &pin_GPIO1 || scl == &pin_GPIO29 || scl == &pin_GPIO45) &&
57-
(sda == &pin_GPIO0 || sda == &pin_GPIO28 || sda == &pin_GPIO44)) {
58-
instance_index = 0;
59-
} else if ((scl == &pin_GPIO44 || scl == &pin_GPIO3) &&
60-
(sda == &pin_GPIO43 || sda == &pin_GPIO2)) {
61-
instance_index = 1;
71+
uint8_t scl_alt = 0;
72+
uint8_t sda_alt = 0;
73+
for (scl_alt = 0; scl_alt < 6; scl_alt++) {
74+
if (scl->functions[scl_alt].type != PIN_FUNCTION_I2C ||
75+
i2c_in_use[scl->functions[scl_alt].index]) {
76+
continue;
77+
}
78+
for (sda_alt = 0; sda_alt < 6; sda_alt++) {
79+
if (sda->functions[sda_alt].type != PIN_FUNCTION_I2C ||
80+
scl->functions[scl_alt].index != sda->functions[sda_alt].index ||
81+
sda->functions[sda_alt].function != I2C_FUNCTION_SDA) {
82+
continue;
83+
}
84+
instance_index = scl->functions[scl_alt].index;
85+
break;
86+
}
87+
if (instance_index != NUM_I2C) {
88+
break;
89+
}
6290
}
6391
if (instance_index == NUM_I2C) {
6492
mp_raise_ValueError(translate("Invalid pins"));
6593
}
66-
in_use_i2c[instance_index] = true;
94+
i2c_in_use[instance_index] = true;
6795
self->index = instance_index;
6896
self->peripheral = i2c[self->index];
6997
self->sda_pin = sda;
@@ -73,8 +101,8 @@ void common_hal_busio_i2c_construct(busio_i2c_obj_t *self,
73101
uint16_t clock_divider = source_clock / frequency;
74102
self->peripheral->DIV_b.CDIV = clock_divider;
75103

76-
gpio_set_function(sda->number, GPIO_GPFSEL0_FSEL2_SDA1);
77-
gpio_set_function(scl->number, GPIO_GPFSEL0_FSEL3_SCL1);
104+
gpio_set_function(sda->number, FSEL_VALUES[sda_alt]);
105+
gpio_set_function(scl->number, FSEL_VALUES[scl_alt]);
78106
}
79107

80108
bool common_hal_busio_i2c_deinited(busio_i2c_obj_t *self) {
@@ -86,6 +114,7 @@ void common_hal_busio_i2c_deinit(busio_i2c_obj_t *self) {
86114
return;
87115
}
88116
never_reset_i2c[self->index] = false;
117+
i2c_in_use[self->index] = false;
89118

90119
common_hal_reset_pin(self->sda_pin);
91120
common_hal_reset_pin(self->scl_pin);
@@ -95,7 +124,6 @@ void common_hal_busio_i2c_deinit(busio_i2c_obj_t *self) {
95124

96125
bool common_hal_busio_i2c_probe(busio_i2c_obj_t *self, uint8_t addr) {
97126
uint8_t result = common_hal_busio_i2c_write(self, addr, NULL, 0, true);
98-
// mp_printf(&mp_plat_print, "result %d %d\n", addr, result);
99127
return result == 0;
100128
}
101129

ports/broadcom/common-hal/microcontroller/Pin.c

Lines changed: 30 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,41 @@
2828

2929
#include "peripherals/broadcom/gpio.h"
3030

31+
STATIC bool pin_in_use[BCM_PIN_COUNT];
32+
STATIC bool never_reset_pin[BCM_PIN_COUNT];
33+
3134
void reset_all_pins(void) {
35+
for (size_t i = 0; i < BCM_PIN_COUNT; i++) {
36+
if (never_reset_pin[i]) {
37+
continue;
38+
}
39+
reset_pin_number(i);
40+
}
3241
}
3342

3443
void never_reset_pin_number(uint8_t pin_number) {
44+
never_reset_pin[pin_number] = true;
3545
}
3646

3747
void reset_pin_number(uint8_t pin_number) {
38-
gpio_set_function(pin_number, 0);
48+
gpio_set_function(pin_number, GPIO_FUNCTION_INPUT);
49+
pin_in_use[pin_number] = false;
50+
never_reset_pin[pin_number] = false;
51+
// Set the pull to match the datasheet.
52+
BP_PULL_Enum pull = BP_PULL_NONE;
53+
if (pin_number < 9 ||
54+
(33 < pin_number && pin_number < 37) ||
55+
pin_number > 45) {
56+
pull = BP_PULL_UP;
57+
} else if (pin_number != 28 &&
58+
pin_number != 29 &&
59+
pin_number != 44 &&
60+
pin_number != 45) {
61+
// Most pins are pulled low so we only exclude the four pins that aren't
62+
// pulled at all.
63+
pull = BP_PULL_DOWN;
64+
}
65+
gpio_set_pull(pin_number, pull);
3966
}
4067

4168
void common_hal_never_reset_pin(const mcu_pin_obj_t *pin) {
@@ -47,11 +74,11 @@ void common_hal_reset_pin(const mcu_pin_obj_t *pin) {
4774
}
4875

4976
void claim_pin(const mcu_pin_obj_t *pin) {
50-
// Nothing to do because all changes will set the GPIO settings.
77+
pin_in_use[pin->number] = true;
5178
}
5279

5380
bool pin_number_is_free(uint8_t pin_number) {
54-
return true;
81+
return !pin_in_use[pin_number];
5582
}
5683

5784
bool common_hal_mcu_pin_is_free(const mcu_pin_obj_t *pin) {
@@ -69,71 +96,3 @@ void common_hal_mcu_pin_claim(const mcu_pin_obj_t *pin) {
6996
void common_hal_mcu_pin_reset_number(uint8_t pin_no) {
7097
reset_pin_number(pin_no);
7198
}
72-
73-
#define PIN(num) \
74-
const mcu_pin_obj_t pin_GPIO##num = { \
75-
{ &mcu_pin_type }, \
76-
.number = num \
77-
};
78-
79-
PIN(0)
80-
PIN(1)
81-
PIN(2)
82-
PIN(3)
83-
PIN(4)
84-
PIN(5)
85-
PIN(6)
86-
PIN(7)
87-
PIN(8)
88-
PIN(9)
89-
PIN(10)
90-
PIN(11)
91-
PIN(12)
92-
PIN(13)
93-
PIN(14)
94-
PIN(15)
95-
PIN(16)
96-
PIN(17)
97-
PIN(18)
98-
PIN(19)
99-
PIN(20)
100-
PIN(21)
101-
PIN(22)
102-
PIN(23)
103-
PIN(24)
104-
PIN(25)
105-
PIN(26)
106-
PIN(27)
107-
PIN(28)
108-
PIN(29)
109-
110-
PIN(30)
111-
PIN(31)
112-
PIN(32)
113-
PIN(33)
114-
PIN(34)
115-
PIN(35)
116-
PIN(36)
117-
PIN(37)
118-
PIN(38)
119-
PIN(39)
120-
121-
PIN(40)
122-
PIN(41)
123-
PIN(42)
124-
PIN(43)
125-
PIN(44)
126-
PIN(45)
127-
PIN(46)
128-
PIN(47)
129-
PIN(48)
130-
PIN(49)
131-
132-
PIN(50)
133-
PIN(51)
134-
PIN(52)
135-
PIN(53)
136-
PIN(54)
137-
PIN(55)
138-
PIN(56)
139-
PIN(57)

ports/broadcom/common-hal/microcontroller/Pin.h

Lines changed: 1 addition & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -32,74 +32,7 @@
3232

3333
#include <py/obj.h>
3434

35-
typedef struct {
36-
mp_obj_base_t base;
37-
uint8_t number;
38-
} mcu_pin_obj_t;
39-
40-
extern const mcu_pin_obj_t pin_GPIO0;
41-
extern const mcu_pin_obj_t pin_GPIO1;
42-
extern const mcu_pin_obj_t pin_GPIO2;
43-
extern const mcu_pin_obj_t pin_GPIO3;
44-
extern const mcu_pin_obj_t pin_GPIO4;
45-
extern const mcu_pin_obj_t pin_GPIO5;
46-
extern const mcu_pin_obj_t pin_GPIO6;
47-
extern const mcu_pin_obj_t pin_GPIO7;
48-
extern const mcu_pin_obj_t pin_GPIO8;
49-
extern const mcu_pin_obj_t pin_GPIO9;
50-
51-
extern const mcu_pin_obj_t pin_GPIO10;
52-
extern const mcu_pin_obj_t pin_GPIO11;
53-
extern const mcu_pin_obj_t pin_GPIO12;
54-
extern const mcu_pin_obj_t pin_GPIO13;
55-
extern const mcu_pin_obj_t pin_GPIO14;
56-
extern const mcu_pin_obj_t pin_GPIO15;
57-
extern const mcu_pin_obj_t pin_GPIO16;
58-
extern const mcu_pin_obj_t pin_GPIO17;
59-
extern const mcu_pin_obj_t pin_GPIO18;
60-
extern const mcu_pin_obj_t pin_GPIO19;
61-
62-
extern const mcu_pin_obj_t pin_GPIO20;
63-
extern const mcu_pin_obj_t pin_GPIO21;
64-
extern const mcu_pin_obj_t pin_GPIO22;
65-
extern const mcu_pin_obj_t pin_GPIO23;
66-
extern const mcu_pin_obj_t pin_GPIO24;
67-
extern const mcu_pin_obj_t pin_GPIO25;
68-
extern const mcu_pin_obj_t pin_GPIO26;
69-
extern const mcu_pin_obj_t pin_GPIO27;
70-
extern const mcu_pin_obj_t pin_GPIO28;
71-
extern const mcu_pin_obj_t pin_GPIO29;
72-
73-
extern const mcu_pin_obj_t pin_GPIO30;
74-
extern const mcu_pin_obj_t pin_GPIO31;
75-
extern const mcu_pin_obj_t pin_GPIO32;
76-
extern const mcu_pin_obj_t pin_GPIO33;
77-
extern const mcu_pin_obj_t pin_GPIO34;
78-
extern const mcu_pin_obj_t pin_GPIO35;
79-
extern const mcu_pin_obj_t pin_GPIO36;
80-
extern const mcu_pin_obj_t pin_GPIO37;
81-
extern const mcu_pin_obj_t pin_GPIO38;
82-
extern const mcu_pin_obj_t pin_GPIO39;
83-
84-
extern const mcu_pin_obj_t pin_GPIO40;
85-
extern const mcu_pin_obj_t pin_GPIO41;
86-
extern const mcu_pin_obj_t pin_GPIO42;
87-
extern const mcu_pin_obj_t pin_GPIO43;
88-
extern const mcu_pin_obj_t pin_GPIO44;
89-
extern const mcu_pin_obj_t pin_GPIO45;
90-
extern const mcu_pin_obj_t pin_GPIO46;
91-
extern const mcu_pin_obj_t pin_GPIO47;
92-
extern const mcu_pin_obj_t pin_GPIO48;
93-
extern const mcu_pin_obj_t pin_GPIO49;
94-
95-
extern const mcu_pin_obj_t pin_GPIO50;
96-
extern const mcu_pin_obj_t pin_GPIO51;
97-
extern const mcu_pin_obj_t pin_GPIO52;
98-
extern const mcu_pin_obj_t pin_GPIO53;
99-
extern const mcu_pin_obj_t pin_GPIO54;
100-
extern const mcu_pin_obj_t pin_GPIO55;
101-
extern const mcu_pin_obj_t pin_GPIO56;
102-
extern const mcu_pin_obj_t pin_GPIO57;
35+
#include "peripherals/broadcom/pins.h"
10336

10437
void reset_all_pins(void);
10538
// reset_pin_number takes the pin number instead of the pointer so that objects don't

ports/broadcom/common-hal/microcontroller/__init__.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,11 @@ const mp_rom_map_elem_t mcu_pin_global_dict_table[TOTAL_GPIO_COUNT] = {
187187
PIN(51)
188188
PIN(52)
189189
PIN(53)
190+
#if BCM_VERSION == 2711
190191
PIN(54)
191192
PIN(55)
192193
PIN(56)
193194
PIN(57)
195+
#endif
194196
};
195197
MP_DEFINE_CONST_DICT(mcu_pin_globals, mcu_pin_global_dict_table);

0 commit comments

Comments
 (0)