Skip to content

Commit 247782a

Browse files
nzmichaelhgalak
authored andcommitted
sam0: move the UART and SPI configuration into pinmux.
Also pull out the SERCOM pads configuration to defines. Note that the SAM0 has a two level configuration - a signal (like TX) is mapped to a pad, and then a pad is mapped to a function on a pin. Signed-off-by: Michael Hope <[email protected]>
1 parent cf36cb2 commit 247782a

File tree

8 files changed

+92
-122
lines changed

8 files changed

+92
-122
lines changed

arch/arm/soc/atmel_sam0/samd/soc.c

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,6 @@
1616
#include <kernel.h>
1717
#include <soc.h>
1818

19-
void soc_gpio_configure(const struct soc_gpio_pin *pin)
20-
{
21-
PortGroup *group = &PORT->Group[pin->group];
22-
23-
if ((pin->pin & 1) == 0) {
24-
group->PMUX[pin->pin / 2].bit.PMUXE = pin->mux;
25-
} else {
26-
group->PMUX[pin->pin / 2].bit.PMUXO = pin->mux;
27-
}
28-
group->PINCFG[pin->pin].bit.PMUXEN = 1;
29-
}
30-
3119
static void flash_waitstates_init(void)
3220
{
3321
/* One wait state at 48 MHz. */

arch/arm/soc/atmel_sam0/samd/soc.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,6 @@
4545
#error Library does not support the specified device.
4646
#endif
4747

48-
#include "soc_pinmap.h"
49-
50-
struct soc_gpio_pin {
51-
u8_t group;
52-
u8_t pin;
53-
u8_t mux;
54-
};
55-
56-
void soc_gpio_configure(const struct soc_gpio_pin *pin);
57-
5848
#endif /* _ASMLANGUAGE */
5949

6050
/** Processor Clock (HCLK) Frequency */

arch/arm/soc/atmel_sam0/samd/soc_pinmap.h

Lines changed: 0 additions & 66 deletions
This file was deleted.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
if(CONFIG_PINMUX_SAM0)
2+
zephyr_library()
3+
zephyr_library_sources(pinmux.c)
4+
endif()

boards/arm/arduino_zero/board.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,12 @@
2121
#define LED2_GPIO_PORT CONFIG_GPIO_SAM0_PORTB_LABEL
2222
#define LED2_GPIO_PIN 3
2323

24+
#define CONFIG_UART_SAM0_SERCOM0_PADS \
25+
(SERCOM_USART_CTRLA_RXPO(3) | SERCOM_USART_CTRLA_TXPO(1))
26+
#define CONFIG_UART_SAM0_SERCOM5_PADS \
27+
(SERCOM_USART_CTRLA_RXPO(3) | SERCOM_USART_CTRLA_TXPO(1))
28+
29+
#define CONFIG_SPI_SAM0_SERCOM4_PADS \
30+
(SERCOM_SPI_CTRLA_DIPO(0) | SERCOM_SPI_CTRLA_DOPO(1))
31+
2432
#endif /* __INC_BOARD_H */

boards/arm/arduino_zero/pinmux.c

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright (c) 2018 Google LLC.
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <init.h>
8+
#include <pinmux.h>
9+
10+
static int board_pinmux_init(struct device *dev)
11+
{
12+
struct device *muxa = device_get_binding(CONFIG_PINMUX_SAM0_A_LABEL);
13+
struct device *muxb = device_get_binding(CONFIG_PINMUX_SAM0_B_LABEL);
14+
15+
ARG_UNUSED(dev);
16+
17+
#if CONFIG_UART_SAM0_SERCOM0_BASE_ADDRESS
18+
/* SERCOM0 on RX=PA11, TX=PA10 */
19+
pinmux_pin_set(muxa, 11, PINMUX_FUNC_C);
20+
pinmux_pin_set(muxa, 10, PINMUX_FUNC_C);
21+
#endif
22+
23+
#if CONFIG_UART_SAM0_SERCOM5_BASE_ADDRESS
24+
/* SERCOM5 on RX=PB23, TX=PB22 */
25+
pinmux_pin_set(muxb, 23, PINMUX_FUNC_D);
26+
pinmux_pin_set(muxb, 22, PINMUX_FUNC_D);
27+
#endif
28+
29+
#if CONFIG_UART_SAM0_SERCOM1_BASE_ADDRESS
30+
#error Pin mapping is not configured
31+
#endif
32+
#if CONFIG_UART_SAM0_SERCOM2_BASE_ADDRESS
33+
#error Pin mapping is not configured
34+
#endif
35+
#if CONFIG_UART_SAM0_SERCOM3_BASE_ADDRESS
36+
#error Pin mapping is not configured
37+
#endif
38+
#if CONFIG_UART_SAM0_SERCOM4_BASE_ADDRESS
39+
#error Pin mapping is not configured
40+
#endif
41+
42+
#if CONFIG_SPI_SAM0_SERCOM4_BASE_ADDRESS
43+
/* SPI SERCOM4 on MISO=PB12/pad 0, MOSI=PB10/pad 2, SCK=PB11/pad 3 */
44+
pinmux_pin_set(muxb, 12, PINMUX_FUNC_C);
45+
pinmux_pin_set(muxb, 10, PINMUX_FUNC_D);
46+
pinmux_pin_set(muxb, 11, PINMUX_FUNC_D);
47+
#endif
48+
49+
#if CONFIG_SPI_SAM0_SERCOM0_BASE_ADDRESS
50+
#error Pin mapping is not configured
51+
#endif
52+
#if CONFIG_SPI_SAM0_SERCOM1_BASE_ADDRESS
53+
#error Pin mapping is not configured
54+
#endif
55+
#if CONFIG_SPI_SAM0_SERCOM2_BASE_ADDRESS
56+
#error Pin mapping is not configured
57+
#endif
58+
#if CONFIG_SPI_SAM0_SERCOM3_BASE_ADDRESS
59+
#error Pin mapping is not configured
60+
#endif
61+
#if CONFIG_SPI_SAM0_SERCOM5_BASE_ADDRESS
62+
#error Pin mapping is not configured
63+
#endif
64+
65+
return 0;
66+
}
67+
68+
SYS_INIT(board_pinmux_init, PRE_KERNEL_1, CONFIG_PINMUX_INIT_PRIORITY);

drivers/serial/uart_sam0.c

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,15 @@
1010
#include <misc/__assert.h>
1111
#include <soc.h>
1212
#include <uart.h>
13+
#include <board.h>
1314

1415
/* Device constant configuration parameters */
1516
struct uart_sam0_dev_cfg {
1617
SercomUsart *regs;
1718
u32_t baudrate;
18-
u32_t ctrla;
19+
u32_t pads;
1920
u32_t pm_apbcmask;
2021
u16_t gclk_clkctrl_id;
21-
struct soc_gpio_pin pin_rx;
22-
struct soc_gpio_pin pin_tx;
2322
#if CONFIG_UART_INTERRUPT_DRIVEN
2423
void (*irq_config_func)(struct device *dev);
2524
#endif
@@ -76,17 +75,13 @@ static int uart_sam0_init(struct device *dev)
7675
/* Enable SERCOM clock in PM */
7776
PM->APBCMASK.reg |= cfg->pm_apbcmask;
7877

79-
/* Connect pins to the peripheral */
80-
soc_gpio_configure(&cfg->pin_rx);
81-
soc_gpio_configure(&cfg->pin_tx);
82-
8378
/* Disable all USART interrupts */
8479
usart->INTENCLR.reg = SERCOM_USART_INTENCLR_MASK;
8580
wait_synchronization(usart);
8681

8782
/* 8 bits of data, no parity, 1 stop bit in normal mode */
8883
usart->CTRLA.reg =
89-
cfg->ctrla |
84+
cfg->pads |
9085
/* Internal clock */
9186
SERCOM_USART_CTRLA_MODE_USART_INT_CLK
9287
/* 16x oversampling with arithmetic baud rate generation */
@@ -285,9 +280,7 @@ static const struct uart_sam0_dev_cfg uart_sam0_config_##n = { \
285280
.baudrate = CONFIG_UART_SAM0_SERCOM##n##_CURRENT_SPEED, \
286281
.pm_apbcmask = PM_APBCMASK_SERCOM##n, \
287282
.gclk_clkctrl_id = GCLK_CLKCTRL_ID_SERCOM##n##_CORE, \
288-
.ctrla = SERCOM_USART_CTRLA_RXPO(3) | SERCOM_USART_CTRLA_TXPO(1), \
289-
.pin_rx = PIN_UART_SAM0_SERCOM##n##_RX, \
290-
.pin_tx = PIN_UART_SAM0_SERCOM##n##_TX, \
283+
.pads = CONFIG_UART_SAM0_SERCOM##n##_PADS, \
291284
UART_SAM0_IRQ_HANDLER_FUNC(n) \
292285
}
293286

drivers/spi/spi_sam0.c

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,11 @@
88
#include <logging/sys_log.h>
99

1010
#include "spi_context.h"
11-
#include <device.h>
1211
#include <errno.h>
13-
#include <init.h>
14-
#include <misc/__assert.h>
15-
#include <soc.h>
12+
#include <device.h>
1613
#include <spi.h>
14+
#include <soc.h>
15+
#include <board.h>
1716

1817
#if defined(CONFIG_SPI_LEGACY_API)
1918
#error "This driver does not support the SPI legacy API."
@@ -22,12 +21,9 @@
2221
/* Device constant configuration parameters */
2322
struct spi_sam0_config {
2423
SercomSpi *regs;
25-
u32_t ctrla;
24+
u32_t pads;
2625
u32_t pm_apbcmask;
2726
u16_t gclk_clkctrl_id;
28-
struct soc_gpio_pin pin_miso;
29-
struct soc_gpio_pin pin_mosi;
30-
struct soc_gpio_pin pin_sck;
3127
};
3228

3329
/* Device run time data */
@@ -77,13 +73,11 @@ static int spi_sam0_configure(struct spi_config *config)
7773
ctrla.bit.CPHA = 1;
7874
}
7975

80-
/* MOSI on PAD2, SCK on PAD3 */
81-
ctrla.bit.DOPO = 1;
76+
ctrla.reg |= cfg->pads;
8277

8378
if ((config->operation & SPI_MODE_LOOP) != 0) {
84-
/* Put MISO on the same pin as MOSI */
85-
ctrla.bit.DIPO = 2;
86-
} else {
79+
/* Put MISO and MOSI on the same pad */
80+
ctrla.bit.DOPO = 0;
8781
ctrla.bit.DIPO = 0;
8882
}
8983

@@ -419,11 +413,6 @@ static int spi_sam0_init(struct device *dev)
419413
/* Enable SERCOM clock in PM */
420414
PM->APBCMASK.reg |= cfg->pm_apbcmask;
421415

422-
/* Connect pins to the peripheral */
423-
soc_gpio_configure(&cfg->pin_mosi);
424-
soc_gpio_configure(&cfg->pin_miso);
425-
soc_gpio_configure(&cfg->pin_sck);
426-
427416
/* Disable all SPI interrupts */
428417
regs->INTENCLR.reg = SERCOM_SPI_INTENCLR_MASK;
429418
wait_synchronization(regs);
@@ -450,11 +439,7 @@ static const struct spi_driver_api spi_sam0_driver_api = {
450439
.regs = &SERCOM##n->SPI, \
451440
.pm_apbcmask = PM_APBCMASK_SERCOM##n, \
452441
.gclk_clkctrl_id = GCLK_CLKCTRL_ID_SERCOM##n##_CORE, \
453-
.ctrla = SERCOM_USART_CTRLA_RXPO(3) | \
454-
SERCOM_USART_CTRLA_TXPO(1), \
455-
.pin_miso = PIN_SPI_SAM0_SERCOM##n##_MISO, \
456-
.pin_mosi = PIN_SPI_SAM0_SERCOM##n##_MOSI, \
457-
.pin_sck = PIN_SPI_SAM0_SERCOM##n##_SCK, \
442+
.pads = CONFIG_SPI_SAM0_SERCOM##n##_PADS \
458443
}
459444

460445
#define SPI_SAM0_DEVICE_INIT(n) \

0 commit comments

Comments
 (0)