26
26
27
27
#include "shared-bindings/microcontroller/Pin.h"
28
28
29
+ #include "asf/sam0/drivers/port/port.h"
30
+
29
31
#include "rgb_led_status.h"
30
32
#include "samd21_pins.h"
31
33
@@ -36,6 +38,47 @@ bool neopixel_in_use;
36
38
bool apa102_sck_in_use ;
37
39
bool apa102_mosi_in_use ;
38
40
#endif
41
+ #ifdef SPEAKER_ENABLE_PIN
42
+ bool speaker_enable_in_use ;
43
+ #endif
44
+
45
+ void reset_all_pins (void ) {
46
+ struct system_pinmux_config config ;
47
+ system_pinmux_get_config_defaults (& config );
48
+ config .powersave = true;
49
+
50
+ uint32_t pin_mask [2 ] = PORT_OUT_IMPLEMENTED ;
51
+
52
+ // Do not full reset USB or SWD lines.
53
+ pin_mask [0 ] &= ~(PIN_PA24 | PIN_PA25 | PIN_PA30 | PIN_PA31 );
54
+
55
+ system_pinmux_group_set_config (& (PORT -> Group [0 ]), pin_mask [0 ] & ~MICROPY_PORT_A , & config );
56
+ system_pinmux_group_set_config (& (PORT -> Group [1 ]), pin_mask [1 ] & ~MICROPY_PORT_B , & config );
57
+
58
+ // Configure SWD
59
+ system_pinmux_get_config_defaults (& config );
60
+ config .mux_position = 0x6 ;
61
+ system_pinmux_group_set_config (& (PORT -> Group [0 ]), PIN_PA30 | PIN_PA31 , & config );
62
+
63
+ #ifdef MICROPY_HW_NEOPIXEL
64
+ neopixel_in_use = false;
65
+ #endif
66
+ #ifdef MICROPY_HW_APA102_MOSI
67
+ apa102_sck_in_use = false;
68
+ apa102_mosi_in_use = false;
69
+ #endif
70
+
71
+ // After configuring SWD because it may be shared.
72
+ #ifdef SPEAKER_ENABLE_PIN
73
+ speaker_enable_in_use = false;
74
+ struct port_config pin_conf ;
75
+ port_get_config_defaults (& pin_conf );
76
+
77
+ pin_conf .direction = PORT_PIN_DIR_OUTPUT ;
78
+ port_pin_set_config (SPEAKER_ENABLE_PIN -> pin , & pin_conf );
79
+ port_pin_set_output_level (SPEAKER_ENABLE_PIN -> pin , false);
80
+ #endif
81
+ }
39
82
40
83
void reset_pin (uint8_t pin ) {
41
84
if (pin >= PORT_BITS ) {
@@ -63,8 +106,24 @@ void reset_pin(uint8_t pin) {
63
106
64
107
struct system_pinmux_config config ;
65
108
system_pinmux_get_config_defaults (& config );
66
- config .powersave = true;
109
+ if (pin == PIN_PA30 || pin == PIN_PA31 ) {
110
+ config .mux_position = 0x6 ;
111
+ } else {
112
+ config .powersave = true;
113
+ }
67
114
system_pinmux_pin_set_config (pin , & config );
115
+
116
+ #ifdef SPEAKER_ENABLE_PIN
117
+ if (pin == SPEAKER_ENABLE_PIN -> pin ) {
118
+ speaker_enable_in_use = false;
119
+ struct port_config pin_conf ;
120
+ port_get_config_defaults (& pin_conf );
121
+
122
+ pin_conf .direction = PORT_PIN_DIR_OUTPUT ;
123
+ port_pin_set_config (SPEAKER_ENABLE_PIN -> pin , & pin_conf );
124
+ port_pin_set_output_level (SPEAKER_ENABLE_PIN -> pin , false);
125
+ }
126
+ #endif
68
127
}
69
128
70
129
void claim_pin (const mcu_pin_obj_t * pin ) {
@@ -81,6 +140,12 @@ void claim_pin(const mcu_pin_obj_t* pin) {
81
140
apa102_sck_in_use = true;
82
141
}
83
142
#endif
143
+
144
+ #ifdef SPEAKER_ENABLE_PIN
145
+ if (pin == SPEAKER_ENABLE_PIN ) {
146
+ speaker_enable_in_use = true;
147
+ }
148
+ #endif
84
149
}
85
150
86
151
bool common_hal_mcu_pin_is_free (const mcu_pin_obj_t * pin ) {
@@ -98,10 +163,21 @@ bool common_hal_mcu_pin_is_free(const mcu_pin_obj_t* pin) {
98
163
}
99
164
#endif
100
165
166
+ #ifdef SPEAKER_ENABLE_PIN
167
+ if (pin == SPEAKER_ENABLE_PIN ) {
168
+ return !speaker_enable_in_use ;
169
+ }
170
+ #endif
171
+
101
172
PortGroup * const port = system_pinmux_get_group_from_gpio_pin (pin -> pin );
102
173
uint32_t pin_index = (pin -> pin );
103
- PORT_PINCFG_Type state = port -> PINCFG [pin_index ];
174
+ volatile PORT_PINCFG_Type * state = & port -> PINCFG [pin_index ];
175
+ volatile PORT_PMUX_Type * pmux = & port -> PMUX [pin_index / 2 ];
176
+
177
+ if (pin -> pin == PIN_PA30 || pin -> pin == PIN_PA31 ) {
178
+ return state -> bit .PMUXEN == 1 && ((pmux -> reg >> (4 * pin_index % 2 )) & 0xf ) == 0x6 ;
179
+ }
104
180
105
- return state . bit .PMUXEN == 0 && state . bit .INEN == 0 &&
106
- state . bit .PULLEN == 0 && (port -> DIR .reg & (1 << pin_index )) == 0 ;
181
+ return state -> bit .PMUXEN == 0 && state -> bit .INEN == 0 &&
182
+ state -> bit .PULLEN == 0 && (port -> DIR .reg & (1 << pin_index )) == 0 ;
107
183
}
0 commit comments