8000 Merge pull request #678 from tannewt/m0_no_timer_fix · godlygeek/circuitpython@f173d45 · GitHub 8000
[go: up one dir, main page]

Skip to content

Commit f173d45

Browse files
authored
Merge pull request adafruit#678 from tannewt/m0_no_timer_fix
Correct NO_TIMER index value for SAMD21.
2 parents 07dd26d + 88aa0e2 commit f173d45

File tree

9 files changed

+45
-42
lines changed

9 files changed

+45
-42
lines changed

ports/atmel-samd/common-hal/busio/I2C.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,21 @@ void common_hal_busio_i2c_construct(busio_i2c_obj_t *self,
4646
uint32_t sda_pinmux = 0;
4747
uint32_t scl_pinmux = 0;
4848
for (int i = 0; i < NUM_SERCOMS_PER_PIN; i++) {
49-
Sercom* potential_sercom = sda->sercom[i].sercom;
50-
if (potential_sercom == NULL ||
51-
potential_sercom->I2CM.CTRLA.bit.ENABLE != 0 ||
49+
sercom_index = sda->sercom[i].index;
50+
if (sercom_index >= SERCOM_INST_NUM) {
51+
continue;
52+
}
53+
Sercom* potential_sercom = sercom_insts[sercom_index];
54+
if (potential_sercom->I2CM.CTRLA.bit.ENABLE != 0 ||
5255
sda->sercom[i].pad != 0) {
5356
continue;
5457
}
5558
sda_pinmux = PINMUX(sda->pin, (i == 0) ? MUX_C : MUX_D);
5659
for (int j = 0; j < NUM_SERCOMS_PER_PIN; j++) {
57-
if (potential_sercom == scl->sercom[j].sercom &&
60+
if (sercom_index == scl->sercom[j].index &&
5861
scl->sercom[j].pad == 1) {
5962
scl_pinmux = PINMUX(scl->pin, (j == 0) ? MUX_C : MUX_D);
6063
sercom = potential_sercom;
61-
sercom_index = scl->sercom[j].index; // 2 for SERCOM2, etc.
6264
break;
6365
}
6466
}
@@ -77,15 +79,15 @@ void common_hal_busio_i2c_construct(busio_i2c_obj_t *self,
7779
if (i2c_m_sync_init(&self->i2c_desc, sercom) != ER 8000 R_NONE) {
7880
mp_raise_OSError(MP_EIO);
7981
}
80-
82+
8183
gpio_set_pin_pull_mode(sda->pin, GPIO_PULL_OFF);
8284
gpio_set_pin_function(sda->pin, sda_pinmux);
8385

8486
gpio_set_pin_pull_mode(scl->pin, GPIO_PULL_OFF);
8587
gpio_set_pin_function(scl->pin, scl_pinmux);
8688

8789
// clkrate is always 0. baud_rate is in kHz.
88-
90+
8991
// Frequency must be set before the I2C device is enabled.
9092
if (i2c_m_sync_set_baudrate(&self->i2c_desc, 0, frequency / 1000) != ERR_NONE) {
9193
mp_raise_ValueError("Unsupported baudrate");
@@ -113,7 +115,7 @@ void common_hal_busio_i2c_deinit(busio_i2c_obj_t *self) {
113115

114116
i2c_m_sync_disable(&self->i2c_desc);
115117
i2c_m_sync_deinit(&self->i2c_desc);
116-
118+
117119
reset_pin(self->sda_pin);
118120
reset_pin(self->scl_pin);
119121
self->sda_pin = NO_PIN;

ports/atmel-samd/common-hal/busio/SPI.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,12 @@ void common_hal_busio_spi_construct(busio_spi_obj_t *self,
5555
uint8_t miso_pad = 0;
5656
uint8_t dopo = 255;
5757
for (int i = 0; i < NUM_SERCOMS_PER_PIN; i++) {
58-
Sercom* potential_sercom = clock->sercom[i].sercom;
5958
sercom_index = clock->sercom[i].index; // 2 for SERCOM2, etc.
60-
if (potential_sercom == NULL ||
59+
if (sercom_index >= SERCOM_INST_NUM) {
60+
continue;
61+
}
62+
Sercom* potential_sercom = sercom_insts[sercom_index];
63+
if (
6164
#if defined(MICROPY_HW_APA102_SCK) && defined(MICROPY_HW_APA102_MOSI) && !defined(CIRCUITPY_BITBANG_APA102)
6265
(potential_sercom->SPI.CTRLA.bit.ENABLE != 0 &&
6366
potential_sercom != status_apa102.spi_desc.dev.prvt &&
@@ -74,7 +77,7 @@ void common_hal_busio_spi_construct(busio_spi_obj_t *self,
7477
}
7578
for (int j = 0; j < NUM_SERCOMS_PER_PIN; j++) {
7679
if (!mosi_none) {
77-
if(potential_sercom == mosi->sercom[j].sercom) {
80+
if (sercom_index == mosi->sercom[j].index) {
7881
mosi_pinmux = PINMUX(mosi->pin, (j == 0) ? MUX_C : MUX_D);
7982
mosi_pad = mosi->sercom[j].pad;
8083
dopo = samd_peripherals_get_spi_dopo(clock_pad, mosi_pad);
@@ -91,7 +94,7 @@ void common_hal_busio_spi_construct(busio_spi_obj_t *self,
9194
}
9295
if (!miso_none) {
9396
for (int k = 0; k < NUM_SERCOMS_PER_PIN; k++) {
94-
if (potential_sercom == miso->sercom[k].sercom) {
97+
if (sercom_index == miso->sercom[k].index) {
9598
miso_pinmux = PINMUX(miso->pin, (k == 0) ? MUX_C : MUX_D);
9699
miso_pad = miso->sercom[k].pad;
97100
sercom = potential_sercom;

ports/atmel-samd/common-hal/busio/UART.c

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
7171
if (!have_tx && !have_rx) {
7272
mp_raise_ValueError("tx and rx cannot both be None");
7373
}
74-
74+
7575
self->baudrate = baudrate;
7676
self->character_bits = bits;
7777
self->timeout_ms = timeout;
@@ -82,10 +82,12 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
8282
for (int i = 0; i < NUM_SERCOMS_PER_PIN; i++) {
8383
Sercom* potential_sercom = NULL;
8484
if (have_tx) {
85-
potential_sercom = tx->sercom[i].sercom;
8685
sercom_index = tx->sercom[i].index;
87-
if (potential_sercom == NULL ||
88-
potential_sercom->USART.CTRLA.bit.ENABLE != 0 ||
86+
if (sercom_index >= SERCOM_INST_NUM) {
87+
continue;
88+
}
89+
potential_sercom = sercom_insts[sercom_index];
90+
if (potential_sercom->USART.CTRLA.bit.ENABLE != 0 ||
8991
!(tx->sercom[i].pad == 0 ||
9092
tx->sercom[i].pad == 2)) {
9193
continue;
@@ -98,12 +100,13 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
98100
}
99101
}
100102
for (int j = 0; j < NUM_SERCOMS_PER_PIN; j++) {
101-
if (((!have_tx && rx->sercom[j].sercom->USART.CTRLA.bit.ENABLE == 0) ||
102-
potential_sercom == rx->sercom[j].sercom) &&
103+
if (((!have_tx && rx->sercom[j].index < SERCOM_INST_NUM &&
104+
sercom_insts[rx->sercom[j].index]->USART.CTRLA.bit.ENABLE == 0) ||
105+
sercom_index == rx->sercom[j].index) &&
103106
rx->sercom[j].pad != tx_pad) {
104107
rx_pinmux = PINMUX(rx->pin, (j == 0) ? MUX_C : MUX_D);
105108
rx_pad = rx->sercom[j].pad;
106-
sercom = rx->sercom[j].sercom;
109+
sercom = sercom_insts[rx->sercom[j].index];
107110
sercom_index = rx->sercom[j].index;
108111
break;
109112
}
@@ -147,7 +150,7 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
147150
// usart_async_init() sets a number of defaults based on a prototypical SERCOM
148151
// which don't necessarily match what we need. After calling it, set the values
149152
// specific to this instantiation of UART.
150-
153+
151154
// Set pads computed for this SERCOM.
152155
// TXPO:
153156
// 0x0: TX pad 0; no RTS/CTS
@@ -182,7 +185,7 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
182185
// http://start.atmel.com/static/help/index.html?GUID-79201A5A-226F-4FBB-B0B8-AB0BE0554836
183186
// Look at the ASFv4 code example for async USART.
184187
usart_async_register_callback(usart_desc_p, USART_ASYNC_RXC_CB, usart_async_rxc_callback);
185-
188+
186189

187190
if (have_tx) {
188191
gpio_set_pin_direction(tx->pin, GPIO_DIRECTION_OUT);
@@ -193,7 +196,7 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
193196
} else {
194197
self->tx_pin = NO_PIN;
195198
}
196-
199+
197200
if (have_rx) {
198201
gpio_set_pin_direction(rx->pin, GPIO_DIRECTION_IN);
199202
gpio_set_pin_pull_mode(rx->pin, GPIO_PULL_OFF);
@@ -238,7 +241,7 @@ size_t common_hal_busio_uart_read(busio_uart_obj_t *self, uint8_t *data, size_t
238241
// Nothing to read.
239242
return 0;
240243
}
241-
244+
242245
struct io_descriptor *io;
243246
usart_async_get_io_descriptor(usart_desc_p, &io);
244247

@@ -266,7 +269,7 @@ size_t common_hal_busio_uart_read(busio_uart_obj_t *self, uint8_t *data, size_t
266269
MICROPY_VM_HOOK_LOOP
267270
#endif
268271
}
269-
272+
270273
return total_read;
271274
}
272275

@@ -305,7 +308,7 @@ size_t common_hal_busio_uart_write(busio_uart_obj_t *self, const uint8_t *data,
305308
*errcode = MP_EAGAIN;
306309
return MP_STREAM_ERROR;
307310
}
308-
311+
309312
struct usart_async_status async_status;
310313
// Could return ERR_BUSY, but if that's true there's already a problem.
311314
usart_async_get_status(usart_desc_p, &async_status);

ports/atmel-samd/common-hal/microcontroller/Pin.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,8 @@
3434
#include "include/component/sercom.h"
3535

3636
typedef struct {
37-
Sercom *const sercom; // SERCOM0, SERCOM1, etc.
38-
uint8_t index; // 0, 1, etc. corresponding to SERCOM<n>.
39-
uint8_t pad; // which of the four SERCOM pads to use
37+
uint8_t index:6; // 0, 1, etc. corresponding to SERCOM<n>.
38+
uint8_t pad:2; // which of the four SERCOM pads to use
4039
} pin_sercom_t;
4140

4241
typedef struct {

ports/atmel-samd/peripherals.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
uint8_t samd_peripherals_spi_baudrate_to_baud_reg_value(const uint32_t baudrate);
3636
uint32_t samd_peripherals_spi_baud_reg_value_to_baudrate(const uint8_t baud_reg_value);
3737

38+
Sercom* sercom_insts[SERCOM_INST_NUM];
39+
3840
#ifdef SAMD21
3941
#include "samd21_peripherals.h"
4042
#endif

ports/atmel-samd/samd21_peripherals.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ static const uint8_t SERCOMx_GCLK_ID_SLOW[] = {
5656
#endif
5757
};
5858

59-
59+
Sercom* sercom_insts[SERCOM_INST_NUM] = SERCOM_INSTS;
60+
6061
// Clock initialization as done in Atmel START.
6162
void samd_peripherals_sercom_clock_init(Sercom* sercom, uint8_t sercom_index) {
6263
_pm_enable_bus_clock(PM_BUS_APBC, sercom);

ports/atmel-samd/samd21_pins.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,13 @@
3030

3131
#define SERCOM(sercom_index, p_pad) \
3232
{ \
33-
.sercom = SERCOM## sercom_index, \
3433
.index = sercom_index, \
3534
.pad = p_pad \
3635
}
3736

3837
#define NO_SERCOM \
3938
{ \
40-
.sercom = 0, \
41-
.index = 0, \
39+
.index = 0x3f, \
4240
.pad = 0 \
4341
}
4442

@@ -57,7 +55,7 @@
5755
.wave_output = p_wave_output \
5856
}
5957

60-
#define NO_TIMER TCC(0, 0)
58+
#define NO_TIMER TCC(0xff, 0)
6159

6260
#define TOUCH(y_line) \
6361
.has_touch = true, \
@@ -92,8 +90,6 @@ const mcu_pin_obj_t pin_## p_name = { \
9290
.sercom = {p_primary_sercom, p_secondary_sercom}, \
9391
}
9492

95-
#define NO_ADC_INPUT (0)
96-
9793
// Pins in datasheet order.
9894
// NOTE(tannewt): TC wave out 0 is commented out because the first channel is
9995
// used to vary the 16 bit timer's frequency.

ports/atmel-samd/samd51_peripherals.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ static const uint8_t SERCOMx_GCLK_ID_SLOW[] = {
6060
#endif
6161
};
6262

63-
63+
Sercom* sercom_insts[SERCOM_INST_NUM] = SERCOM_INSTS;
64+
6465
// Clock initialization as done in Atmel START.
6566
void samd_peripherals_sercom_clock_init(Sercom* sercom, uint8_t sercom_index) {
6667
hri_gclk_write_PCHCTRL_reg(GCLK,

ports/atmel-samd/samd51_pins.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,13 @@
3030

3131
#define SERCOM(sercom_index, p_pad) \
3232
{ \
33-
.sercom = SERCOM## sercom_index, \
3433
.index = sercom_index, \
3534
.pad = p_pad \
3635
}
3736

3837
#define NO_SERCOM \
3938
{ \
40-
.sercom = 0, \
41-
.index = 0, \
39+
.index = 0x3f, \
4240
.pad = 0 \
4341
}
4442

@@ -91,8 +89,6 @@ const mcu_pin_obj_t pin_## p_name = { \
9189
.sercom = {p_primary_sercom, p_secondary_sercom}, \
9290
}
9391

94-
#define NO_ADC_INPUT (0)
95-
9692
// Pins in datasheet order.
9793
// NOTE(tannewt): TC wave out 0 is commented out because the first channel is
9894
// used to vary the 16 bit timer's frequency.
@@ -1139,7 +1135,7 @@ PIN(PA31, EXTINT_CHANNEL(15), NO_ADC, NO_ADC, NO_TOUCH,
11391135
#else
11401136
NO_SERCOM,
11411137
#endif
1142-
SERCOM(1, 23),
1138+
SERCOM(1, 3),
11431139
#ifdef TC6
11441140
TC(6, 1),
11451141
#else

0 commit comments

Comments
 (0)
0