Skip to content

Commit 0f50a8f

Browse files
bors[bot]dylad
andauthoredJul 13, 2023
Merge #19798
19798: cpu/nrf53: add I2C and SPI support r=benpicco a=dylad ### Contribution description This PR provides support for nRF53 SPI and I2C. It also moves common structs from each nRF CPU folder to `cpu/nrf5x_common` to avoid duplication. Moreover, since nRF9160 and nRF5340 have shared IRQ for UART/SPI/I2C. Both this families now use a common file to register and manage these interrupts. Note that nRF9160 have different name for its interrupts than nRF5340 but they have the same purpose. ### Testing procedure Since some structs were moved around, I think this PR should be carefully tested against nRF52, nRF53 and nRF9160 to avoid any issues. On nRF5340DK-APP, SPI can be tested with its onboard SPI flash. ### Issues/PRs references Co-authored-by: Dylan Laduranty <[email protected]>
2 parents 7dd7d1e + 883d138 commit 0f50a8f

33 files changed

+602
-456
lines changed
 

‎boards/nrf5340dk-app/Kconfig

+4
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,15 @@ config BOARD_NRF5340DK_APP
1111
bool
1212
default y
1313
select CPU_MODEL_NRF5340_APP
14+
select HAS_PERIPH_I2C
15+
select HAS_PERIPH_SPI
16+
select HAS_PERIPH_SPI_GPIO_MODE
1417
select HAS_PERIPH_PWM
1518
select HAS_PERIPH_RTT
1619
select HAS_PERIPH_TIMER
1720
select HAS_PERIPH_UART
1821
select HAS_PERIPH_UART_HW_FC
1922
select HAS_PERIPH_USBDEV
23+
select HAVE_MTD_SPI_NOR
2024

2125
# Put other features for this board (in alphabetical order)

‎boards/nrf5340dk-app/Makefile.dep

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
ifneq (,$(filter mtd,$(USEMODULE)))
2+
USEMODULE += mtd_spi_nor
3+
endif
4+
5+
# default to using littlefs2 on the external flash
6+
ifneq (,$(filter vfs_default,$(USEMODULE)))
7+
USEPKG += littlefs2
8+
USEMODULE += mtd
9+
endif

‎boards/nrf5340dk-app/Makefile.features

+2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ CPU_MODEL = nrf5340_app
22
CPU = nrf53
33

44
# Put defined MCU peripherals here (in alphabetical order)
5+
FEATURES_PROVIDED += periph_i2c
56
FEATURES_PROVIDED += periph_pwm
67
FEATURES_PROVIDED += periph_rtt
8+
FEATURES_PROVIDED += periph_spi
79
FEATURES_PROVIDED += periph_timer
810
FEATURES_PROVIDED += periph_uart
911
FEATURES_PROVIDED += periph_uart_hw_fc

‎boards/nrf5340dk-app/board.c

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright (C) 2023 Mesotic SAS
3+
*
4+
* This file is subject to the terms and conditions of the GNU Lesser
5+
* General Public License v2.1. See the file LICENSE in the top level
6+
* directory for more details.
7+
*/
8+
9+
/**
10+
* @ingroup boards_nrf5340dk-app
11+
* @{
12+
*
13+
* @file
14+
* @brief Board specific implementations for the Nordic nRF5340DK board
15+
*
16+
* @author Dylan Laduranty <dylan.laduranty@mesotic.com>
17+
* @}
18+
*/
19+
20+
#include "board.h"
21+
#include "periph/gpio.h"
22+
#include "timex.h"
23+
#ifdef MODULE_VFS_DEFAULT
24+
#include "vfs_default.h"
25+
#endif
26+
27+
#ifdef MODULE_MTD_SPI_NOR
28+
#include "mtd_spi_nor.h"
29+
/* MX25R64 */
30+
static const mtd_spi_nor_params_t _nrf5340_nor_params = {
31+
.opcode = &mtd_spi_nor_opcode_default,
32+
.wait_chip_erase = 240 * US_PER_SEC,
33+
.wait_64k_erase = 800 * US_PER_MS,
34+
.wait_sector_erase = 240 * US_PER_MS,
35+
.wait_chip_wake_up = 1 * US_PER_MS,
36+
.clk = MHZ(54),
37+
.flag = SPI_NOR_F_SECT_4K | SPI_NOR_F_SECT_64K,
38+
.spi = SPI_DEV(0),
39+
.mode = SPI_MODE_0,
40+
.cs = BOARD_QSPI_PIN_CS,
41+
.wp = BOARD_QSPI_PIN_WP,
42+
.hold = BOARD_QSPI_PIN_HOLD,
43+
};
44+
45+
static mtd_spi_nor_t nrf5340_nor_dev = {
46+
.base = {
47+
.driver = &mtd_spi_nor_driver,
48+
.page_size = 256,
49+
.pages_per_sector = 16,
50+
},
51+
.params = &_nrf5340_nor_params,
52+
};
53+
mtd_dev_t *mtd0 = (mtd_dev_t *)&nrf5340_nor_dev;
54+
55+
#ifdef MODULE_VFS_DEFAULT
56+
VFS_AUTO_MOUNT(littlefs2, VFS_MTD(nrf5340_nor_dev), VFS_DEFAULT_NVM(0), 0);
57+
#endif
58+
#endif /* MODULE_MTD_SPI_NOR */

‎boards/nrf5340dk-app/include/board.h

+14
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#define BOARD_H
2121

2222
#include "cpu.h"
23+
#include "mtd.h"
2324

2425
#ifdef __cplusplus
2526
extern "C" {
@@ -95,6 +96,19 @@ extern "C" {
9596
#define BTN3_MODE GPIO_IN_PU /**< BTN3 default mode */
9697
/** @} */
9798

99+
/**
100+
* @name MTD configuration
101+
* @{
102+
*/
103+
extern mtd_dev_t *mtd0;
104+
#define MTD_0 mtd0
105+
#define MTD_NUMOF 1
106+
107+
#define BOARD_QSPI_PIN_CS GPIO_PIN(0, 18) /**< SPI Flash Chip Select */
108+
#define BOARD_QSPI_PIN_WP GPIO_PIN(0, 15) /**< SPI Flash Write Protect */
109+
#define BOARD_QSPI_PIN_HOLD GPIO_PIN(0, 16) /**< SPI Flash Hold pin */
110+
/** @} */
111+
98112
#ifdef __cplusplus
99113
}
100114
#endif

‎boards/nrf5340dk-app/include/periph_conf.h

+31-2
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,6 @@ static const uart_conf_t uart_config[] = {
7070
},
7171
};
7272

73-
#define UART_0_ISR (isr_serial0) /**< SERIAL0_IRQn */
74-
7573
#define UART_NUMOF ARRAY_SIZE(uart_config) /**< UART configuration NUMOF */
7674
/** @} */
7775

@@ -112,6 +110,37 @@ static const pwm_conf_t pwm_config[] = {
112110
#define PWM_NUMOF ARRAY_SIZE(pwm_config)
113111
/** @} */
114112

113+
/**
114+
* @name SPI configuration
115+
* @{
116+
*/
117+
static const spi_conf_t spi_config[] = {
118+
{
119+
.dev = NRF_SPIM1_S,
120+
.sclk = GPIO_PIN(0, 17),
121+
.mosi = GPIO_PIN(0, 13),
122+
.miso = GPIO_PIN(0, 14),
123+
}
124+
};
125+
126+
#define SPI_NUMOF ARRAY_SIZE(spi_config)
127+
/** @} */
128+
129+
/**
130+
* * @name I2C configuration
131+
* * @{
132+
* */
133+
static const i2c_conf_t i2c_config[] = {
134+
{
135+
.dev = NRF_TWIM2_S,
136+
.scl = GPIO_PIN(1, 3),
137+
.sda = GPIO_PIN(1, 2),
138+
.speed = I2C_SPEED_NORMAL
139+
}
140+
};
141+
#define I2C_NUMOF ARRAY_SIZE(i2c_config)
142+
/** @} */
143+
115144
#ifdef __cplusplus
116145
}
117146
#endif

‎boards/nrf9160dk/include/periph_conf.h

-3
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,6 @@ static const uart_conf_t uart_config[] = {
113113
},
114114
};
115115

116-
#define UART_0_ISR (isr_uarte0_spim0_spis0_twim0_twis0) /**< UART0_IRQ */
117-
#define UART_1_ISR (isr_uarte1_spim1_spis1_twim1_twis1) /**< UART1_IRQ */
118-
119116
#define UART_NUMOF ARRAY_SIZE(uart_config) /**< UART configuration NUMOF */
120117
/** @} */
121118

‎cpu/nrf52/include/periph_cpu.h

-85
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,6 @@
2727
extern "C" {
2828
#endif
2929

30-
/**
31-
* @brief Enable the workaround for the SPI single byte transmit errata (No.
32-
* 58 on the nrf52832)
33-
*/
34-
#ifdef CPU_MODEL_NRF52832XXAA
35-
#define ERRATA_SPI_SINGLE_BYTE_WORKAROUND (1)
36-
#endif
37-
3830
/**
3931
* @brief System core clock speed, fixed to 64MHz for all NRF52x CPUs
4032
*/
@@ -45,18 +37,6 @@ extern "C" {
4537
*/
4638
#define PERIPH_CLOCK (16000000U)
4739

48-
/**
49-
* @brief Redefine some peripheral names to unify them between nRF51 and 52
50-
* @{
51-
*/
52-
#define SPI_SCKSEL (dev(bus)->PSEL.SCK)
53-
#define SPI_MOSISEL (dev(bus)->PSEL.MOSI)
54-
#define SPI_MISOSEL (dev(bus)->PSEL.MISO)
55-
#ifdef CPU_MODEL_NRF52832XXAA
56-
#define UART_IRQN (UARTE0_UART0_IRQn)
57-
#endif
58-
/** @} */
59-
6040
/**
6141
* @brief The nRF52 family of CPUs provides a fixed number of 9 ADC lines
6242
*/
@@ -66,14 +46,6 @@ extern "C" {
6646
#define ADC_NUMOF (9U)
6747
#endif
6848

69-
/**
70-
* @brief SPI temporary buffer size for storing const data in RAM before
71-
* initiating DMA transfer
72-
*/
73-
#ifndef CONFIG_SPI_MBUF_SIZE
74-
#define CONFIG_SPI_MBUF_SIZE 64
75-
#endif
76-
7749
/**
7850
* @brief nRF52 specific naming of ADC lines (for convenience)
7951
*/
@@ -109,70 +81,13 @@ typedef enum {
10981
/** @} */
11082
#endif /* ndef DOXYGEN */
11183

112-
#ifndef DOXYGEN
113-
/**
114-
* @brief Override I2C speed settings
115-
* @{
116-
*/
117-
#define HAVE_I2C_SPEED_T
118-
typedef enum {
119-
I2C_SPEED_LOW = 0xff, /**< not supported */
120-
I2C_SPEED_NORMAL = TWIM_FREQUENCY_FREQUENCY_K100, /**< 100kbit/s */
121-
I2C_SPEED_FAST = TWIM_FREQUENCY_FREQUENCY_K400, /**< 400kbit/s */
122-
I2C_SPEED_FAST_PLUS = 0xfe, /**< not supported */
123-
I2C_SPEED_HIGH = 0xfd, /**< not supported */
124-
} i2c_speed_t;
125-
/** @} */
126-
#endif /* ndef DOXYGEN */
127-
128-
/**
129-
* @brief I2C (TWI) configuration options
130-
* @{
131-
*/
132-
typedef struct {
133-
NRF_TWIM_Type *dev; /**< TWIM hardware device */
134-
gpio_t scl; /**< SCL pin */
135-
gpio_t sda; /**< SDA pin */
136-
i2c_speed_t speed; /**< Bus speed */
137-
} i2c_conf_t;
138-
/** @} */
139-
140-
/**
141-
* @name Use shared I2C functions
142-
* @{
143-
*/
144-
#define PERIPH_I2C_NEED_READ_REG
145-
#define PERIPH_I2C_NEED_WRITE_REG
146-
/** @} */
147-
148-
/**
149-
* @name Define macros for sda and scl pin to be able to reinitialize them
150-
* @{
151-
*/
152-
#define i2c_pin_sda(dev) i2c_config[dev].sda
153-
#define i2c_pin_scl(dev) i2c_config[dev].scl
154-
/** @} */
155-
15684
/**
15785
* @brief Size of the UART TX buffer for non-blocking mode.
15886
*/
15987
#ifndef UART_TXBUF_SIZE
16088
#define UART_TXBUF_SIZE (64)
16189
#endif
16290

163-
/**
164-
* @brief SPI configuration values
165-
*/
166-
typedef struct {
167-
NRF_SPIM_Type *dev; /**< SPI device used */
168-
gpio_t sclk; /**< CLK pin */
169-
gpio_t mosi; /**< MOSI pin */
170-
gpio_t miso; /**< MISO pin */
171-
#if ERRATA_SPI_SINGLE_BYTE_WORKAROUND
172-
uint8_t ppi; /**< PPI channel */
173-
#endif
174-
} spi_conf_t;
175-
17691
/**
17792
* @brief Common SPI/I2C interrupt callback
17893
*

‎cpu/nrf52/periph/Kconfig

-10
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,6 @@
77

88
if TEST_KCONFIG
99

10-
config MODULE_PERIPH_UART_NONBLOCKING
11-
depends on HAS_PERIPH_UART_NONBLOCKING
12-
depends on MODULE_PERIPH_UART
13-
select MODULE_TSRB
14-
15-
config MODULE_PERIPH_SPI
16-
depends on HAS_PERIPH_SPI
17-
select MODULE_PERIPH_GPIO_IRQ if CPU_MODEL_NRF52832XXAA && HAS_PERIPH_GPIO_IRQ
18-
select MODULE_PERIPH_SPI_GPIO_MODE if MODULE_PERIPH_SPI && HAS_PERIPH_SPI_GPIO_MODE
19-
2010
config MODULE_SAUL_NRF_VDDH
2111
bool "Internal Voltage Sensor"
2212
depends on HAS_PERIPH_ADC

‎cpu/nrf52/spi_twi_irq.c

+7-7
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
#define SPIM_COUNT 2
5858
#endif
5959

60-
static spi_twi_irq_cb_t _irq[SPIM_COUNT];
60+
static shared_irq_cb_t _irq[SPIM_COUNT];
6161
static void *_irq_arg[SPIM_COUNT];
6262

6363
static mutex_t _locks[SPIM_COUNT];
@@ -123,8 +123,8 @@ static const IRQn_Type _isr[] = {
123123
#endif /* CPU_MODEL_NRF52840XXAA */
124124
};
125125

126-
void spi_twi_irq_register_spi(NRF_SPIM_Type *bus,
127-
spi_twi_irq_cb_t cb, void *arg)
126+
void shared_irq_register_spi(NRF_SPIM_Type *bus,
127+
shared_irq_cb_t cb, void *arg)
128128
{
129129
size_t num = _spi_dev2num(bus);
130130

@@ -133,8 +133,8 @@ void spi_twi_irq_register_spi(NRF_SPIM_Type *bus,
133133
NVIC_EnableIRQ(_isr[num]);
134134
}
135135

136-
void spi_twi_irq_register_i2c(NRF_TWIM_Type *bus,
137-
spi_twi_irq_cb_t cb, void *arg)
136+
void shared_irq_register_i2c(NRF_TWIM_Type *bus,
137+
shared_irq_cb_t cb, void *arg)
138138
{
139139
size_t num = _i2c_dev2num(bus);
140140

@@ -145,7 +145,7 @@ void spi_twi_irq_register_i2c(NRF_TWIM_Type *bus,
145145
}
146146

147147
void nrf5x_i2c_acquire(NRF_TWIM_Type *bus,
148-
spi_twi_irq_cb_t cb, void *arg)
148+
shared_irq_cb_t cb, void *arg)
149149
{
150150
size_t num = _i2c_dev2num(bus);
151151
mutex_lock(&_locks[num]);
@@ -154,7 +154,7 @@ void nrf5x_i2c_acquire(NRF_TWIM_Type *bus,
154154
}
155155

156156
void nrf5x_spi_acquire(NRF_SPIM_Type *bus,
157-
spi_twi_irq_cb_t cb, void *arg)
157+
shared_irq_cb_t cb, void *arg)
158158
{
159159
size_t num = _spi_dev2num(bus);
160160
mutex_lock(&_locks[num]);

‎cpu/nrf53/Kconfig

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ config CPU_FAM_NRF53
1919
select HAS_PERIPH_UART_MODECFG
2020
select HAS_PERIPH_WDT
2121
select HAS_PERIPH_WDT_CB
22+
select MODULE_NRF_SHARED_SERIAL_IRQ
2223

2324
## CPU Models
2425
config CPU_MODEL_NRF5340_APP

‎cpu/nrf53/Makefile.dep

+5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
USEMODULE += nrf53_vectors
2+
USEMODULE += nrf_shared_serial_irq
3+
4+
ifneq (,$(filter periph_spi,$(USEMODULE)))
5+
USEMODULE += periph_spi_gpio_mode
6+
endif
27

38
include $(RIOTCPU)/nrf5x_common/Makefile.dep
49
include $(RIOTCPU)/cortexm_common/Makefile.dep

‎cpu/nrf53/Makefile.features

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
CPU_CORE = cortex-m33
22
CPU_FAM = nrf53
33

4+
FEATURES_PROVIDED += periph_spi_gpio_mode
5+
46
include $(RIOTCPU)/nrf5x_common/Makefile.features

0 commit comments

Comments
 (0)
Please sign in to comment.