Boot Linux faster!

Check our new training course

Boot Linux faster!

Check our new training course
and Creative Commons CC-BY-SA
lecture and lab materials

Bootlin logo

Elixir Cross Referencer

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
/*
 * Copyright (c) 2017 Intel Corp.
 *
 * SPDX-License-Identifier: Apache-2.0
 */

/*
 * #define CONFIG_ASSERT
 * #define __ASSERT_ON 1
 */

#include <errno.h>
#include <spi.h>
#include <soc.h>
#include <nrf.h>
#include <misc/util.h>
#include <gpio.h>

#define SYS_LOG_DOMAIN "spim"
#define SYS_LOG_LEVEL CONFIG_SYS_LOG_SPI_LEVEL
#include <logging/sys_log.h>

/* @todo
 *
 * Add support for 52840 spim2.
 */

struct spim_nrf52_config {
	volatile NRF_SPIM_Type *base;
	void (*irq_config_func)(struct device *dev);
	struct spi_config default_cfg;
	struct {
		u8_t sck;
		u8_t mosi;
		u8_t miso;
#define SS_UNUSED              255
		/* Pin number of up to 4 slave devices */
		u8_t ss[4];
	} psel;
	u8_t orc;
};

struct spim_nrf52_data {
	struct k_sem sem;
	struct device *gpio_port;
	u8_t slave;
	u8_t stopped:1;
	u8_t txd:1;
	u8_t rxd:1;
#if (SYS_LOG_LEVEL > SYS_LOG_LEVEL_INFO)
	u32_t tx_cnt;
	u32_t rx_cnt;
#endif
};

#define NRF52_SPIM_INT_END SPIM_INTENSET_END_Msk
#define NRF52_SPIM_INT_ENDRX SPIM_INTENSET_ENDRX_Msk
#define NRF52_SPIM_INT_ENDTX SPIM_INTENSET_ENDTX_Msk
#define NRF52_SPIM_ENABLE SPIM_ENABLE_ENABLE_Enabled
#define NRF52_SPIM_DISABLE SPIM_ENABLE_ENABLE_Disabled

static void spim_nrf52_print_cfg_registers(struct device *dev)
{
	const struct spim_nrf52_config *config = dev->config->config_info;
	volatile NRF_SPIM_Type *spim = config->base;

	SYS_LOG_DBG("\n"
		"SHORTS=0x%x INT=0x%x FREQUENCY=0x%x CONFIG=0x%x\n"
		"ENABLE=0x%x SCKPIN=%d MISOPIN=%d MOSIPIN=%d\n"
		"RXD.(PTR=0x%x MAXCNT=0x%x AMOUNT=0x%x)\n"
		"TXD.(PTR=0x%x MAXCNT=0x%x AMOUNT=0x%x)\n",
		spim->SHORTS, spim->INTENSET, spim->FREQUENCY, spim->CONFIG,
		spim->ENABLE, spim->PSEL.SCK, spim->PSEL.MISO, spim->PSEL.MOSI,
		spim->RXD.PTR, spim->RXD.MAXCNT, spim->RXD.AMOUNT,
		spim->TXD.PTR, spim->TXD.MAXCNT, spim->TXD.AMOUNT);

	/* maybe unused */
	(void)spim;
}

static int spim_nrf52_configure(struct device *dev,
				struct spi_config *spi_config)
{
	const struct spim_nrf52_config *config = dev->config->config_info;
	struct spim_nrf52_data *data = dev->driver_data;
	volatile NRF_SPIM_Type *spim = config->base;
	u32_t flags;

	SYS_LOG_DBG("config=0x%x max_sys_freq=%d", spi_config->config,
		    spi_config->max_sys_freq);

	/* make sure SPIM block is off */
	spim->ENABLE = NRF52_SPIM_DISABLE;

	spim->INTENCLR = 0xffffffffUL;

	spim->SHORTS = 0;

	spim->ORC = config->orc;

	spim->TXD.LIST = 0;
	spim->RXD.LIST = 0;

	spim->TXD.MAXCNT = 0;
	spim->RXD.MAXCNT = 0;

	spim->EVENTS_END = 0;
	spim->EVENTS_ENDTX = 0;
	spim->EVENTS_ENDRX = 0;
	spim->EVENTS_STOPPED = 0;
	spim->EVENTS_STARTED = 0;

	data->stopped = 1;
	data->txd = 0;
	data->rxd = 0;
#if (SYS_LOG_LEVEL > SYS_LOG_LEVEL_INFO)
	data->tx_cnt = 0;
	data->rx_cnt = 0;
#endif

	switch (spi_config->max_sys_freq) {
	case 125000:
		spim->FREQUENCY = SPIM_FREQUENCY_FREQUENCY_K125;
		break;
	case 250000:
		spim->FREQUENCY = SPIM_FREQUENCY_FREQUENCY_K250;
		break;
	case 500000:
		spim->FREQUENCY = SPIM_FREQUENCY_FREQUENCY_K500;
		break;
	case 1000000:
		spim->FREQUENCY = SPIM_FREQUENCY_FREQUENCY_M1;
		break;
	case 2000000:
		spim->FREQUENCY = SPIM_FREQUENCY_FREQUENCY_M2;
		break;
	case 4000000:
		spim->FREQUENCY = SPIM_FREQUENCY_FREQUENCY_M4;
		break;
	case 8000000:
		spim->FREQUENCY = SPIM_FREQUENCY_FREQUENCY_M8;
		break;
	default:
		SYS_LOG_ERR("unsupported frequency sck=%d\n",
			    spi_config->max_sys_freq);
		return -EINVAL;
	}

	flags = spi_config->config;

	/* nrf5 supports only 8 bit word size */
	if (SPI_WORD_SIZE_GET(flags) != 8) {
		SYS_LOG_ERR("unsupported word size\n");
		return -EINVAL;
	}

	if (flags & SPI_MODE_LOOP) {
		SYS_LOG_ERR("loopback unsupported\n");
		return -EINVAL;
	}

	spim->CONFIG = (flags & SPI_TRANSFER_LSB) ?
		       (SPIM_CONFIG_ORDER_LsbFirst << SPIM_CONFIG_ORDER_Pos) :
		       (SPIM_CONFIG_ORDER_MsbFirst << SPIM_CONFIG_ORDER_Pos);
	spim->CONFIG |= (flags & SPI_MODE_CPOL) ?
			(SPIM_CONFIG_CPOL_ActiveLow << SPIM_CONFIG_CPOL_Pos) :
			(SPIM_CONFIG_CPOL_ActiveHigh << SPIM_CONFIG_CPOL_Pos);
	spim->CONFIG |= (flags & SPI_MODE_CPHA) ?
			(SPIM_CONFIG_CPHA_Trailing << SPIM_CONFIG_CPHA_Pos) :
			(SPIM_CONFIG_CPHA_Leading << SPIM_CONFIG_CPHA_Pos);

	spim->INTENSET = NRF52_SPIM_INT_END;

	spim_nrf52_print_cfg_registers(dev);

	return 0;
}

static int spim_nrf52_slave_select(struct device *dev, u32_t slave)
{
	struct spim_nrf52_data *data = dev->driver_data;
	const struct spim_nrf52_config *config = dev->config->config_info;

	__ASSERT((slave > 0) && (slave <= 4), "slave=%d", slave);

	slave--;

	if (config->psel.ss[slave] == SS_UNUSED) {
		SYS_LOG_ERR("Slave %d is not configured\n", slave);
		return -EINVAL;
	}

	data->slave = slave;

	return 0;
}

static inline void spim_nrf52_csn(struct device *gpio_port, u32_t pin,
				  bool select)
{
	int status;

	status = gpio_pin_write(gpio_port, pin, select ? 0x0 : 0x1);
	__ASSERT_NO_MSG(status == 0);
}

static int spim_nrf52_transceive(struct device *dev, const void *tx_buf,
				 u32_t tx_buf_len, void *rx_buf,
				 u32_t rx_buf_len)
{
	const struct spim_nrf52_config *config = dev->config->config_info;
	struct spim_nrf52_data *data = dev->driver_data;
	volatile NRF_SPIM_Type *spim = config->base;

	SYS_LOG_DBG("transceive tx_buf=0x%p rx_buf=0x%p tx_len=0x%x "
		    "rx_len=0x%x\n", tx_buf, rx_buf, tx_buf_len, rx_buf_len);

	if (spim->ENABLE) {
		return -EALREADY;
	}
	spim->ENABLE = NRF52_SPIM_ENABLE;

	__ASSERT_NO_MSG(data->stopped);
	data->stopped = 0;

	if (tx_buf_len) {
		__ASSERT_NO_MSG(tx_buf);
		spim->TXD.MAXCNT = tx_buf_len;
		spim->TXD.PTR = (u32_t)tx_buf;
		data->txd = 0;
#if (SYS_LOG_LEVEL > SYS_LOG_LEVEL_INFO)
		data->tx_cnt = 0;
#endif
	} else {
		spim->TXD.MAXCNT = 0;
	}

	if (rx_buf_len) {
		__ASSERT_NO_MSG(rx_buf);
		spim->RXD.MAXCNT = rx_buf_len;
		spim->RXD.PTR = (u32_t)rx_buf;
		data->rxd = 0;
#if (SYS_LOG_LEVEL > SYS_LOG_LEVEL_INFO)
		data->rx_cnt = 0;
#endif
	} else {
		spim->RXD.MAXCNT = 0;
	}

	if (data->slave != SS_UNUSED) {
		spim_nrf52_csn(data->gpio_port, config->psel.ss[data->slave],
			       true);
	}

	spim->INTENSET = NRF52_SPIM_INT_END;

	SYS_LOG_DBG("spi_xfer %s/%s CS%d\n", rx_buf_len ? "R" : "-",
		    tx_buf_len ? "W" : "-", data->slave);

	/* start SPI transfer transaction */
	spim->TASKS_START = 1;

	/* Wait for the transfer to complete */
	k_sem_take(&data->sem, K_FOREVER);

	if (data->slave != SS_UNUSED) {
		spim_nrf52_csn(data->gpio_port, config->psel.ss[data->slave],
			       false);
	}

	/* Disable SPIM block for power saving */
	spim->INTENCLR = 0xffffffffUL;
	spim->ENABLE = NRF52_SPIM_DISABLE;

#if (SYS_LOG_LEVEL > SYS_LOG_LEVEL_INFO)
	SYS_LOG_DBG("xfer complete rx_cnt=0x%x tx_cnt=0x%x rxd=%d txd=%d "
		    "stopped=%d\n", data->rx_cnt, data->tx_cnt, data->rxd,
		    data->txd, data->stopped);
#endif

	return 0;
}

static void spim_nrf52_isr(void *arg)
{
	struct device *dev = arg;
	const struct spim_nrf52_config *config = dev->config->config_info;
	struct spim_nrf52_data *data = dev->driver_data;
	volatile NRF_SPIM_Type *spim = config->base;

	if (spim->EVENTS_END) {
		data->rxd = 1;
		data->txd = 1;

		/* assume spi transaction has stopped */
		data->stopped = 1;

		/* Cortex M4 specific EVENTS register clearing requires 4 cycles
		 * delayto avoid re-triggering of interrupt. Call to
		 * k_sem_give() will ensure this limit.
		 */
		spim->EVENTS_END = 0;

#if (SYS_LOG_LEVEL > SYS_LOG_LEVEL_INFO)
		data->rx_cnt = spim->RXD.AMOUNT;
		data->tx_cnt = spim->TXD.AMOUNT;
		SYS_LOG_DBG("endrxtx rx_cnt=%d tx_cnt=%d", data->rx_cnt,
			    data->tx_cnt);
#endif
		k_sem_give(&data->sem);
	}
}

static int spim_nrf52_init(struct device *dev)
{
	const struct spim_nrf52_config *config = dev->config->config_info;
	struct spim_nrf52_data *data = dev->driver_data;
	volatile NRF_SPIM_Type *spim = config->base;
	int status;
	int i;

	SYS_LOG_DBG("%s", dev->config->name);

	data->gpio_port = device_get_binding(CONFIG_GPIO_NRF5_P0_DEV_NAME);

	k_sem_init(&data->sem, 0, UINT_MAX);

	for (i = 0; i < sizeof(config->psel.ss); i++) {
		if (config->psel.ss[i] != SS_UNUSED) {
			status = gpio_pin_configure(data->gpio_port,
						    config->psel.ss[i],
						    GPIO_DIR_OUT | GPIO_PUD_PULL_UP);
			__ASSERT_NO_MSG(status == 0);

			spim_nrf52_csn(data->gpio_port, config->psel.ss[i],
				       false);
			SYS_LOG_DBG("CS%d=%d\n", i, config->psel.ss[i]);
		}
	}

	data->slave = SS_UNUSED;

	status = gpio_pin_configure(data->gpio_port, config->psel.sck,
				    GPIO_DIR_OUT);
	__ASSERT_NO_MSG(status == 0);

	status = gpio_pin_configure(data->gpio_port, config->psel.mosi,
				    GPIO_DIR_OUT);
	__ASSERT_NO_MSG(status == 0);

	status = gpio_pin_configure(data->gpio_port, config->psel.miso,
				    GPIO_DIR_IN);
	__ASSERT_NO_MSG(status == 0);

	spim->PSEL.SCK = config->psel.sck;
	spim->PSEL.MOSI = config->psel.mosi;
	spim->PSEL.MISO = config->psel.miso;

	status = spim_nrf52_configure(dev, (void *)&config->default_cfg);
	if (status) {
		return status;
	}

	config->irq_config_func(dev);

	return 0;
}

static const struct spi_driver_api spim_nrf52_driver_api = {
	.configure = spim_nrf52_configure,
	.slave_select = spim_nrf52_slave_select,
	.transceive = spim_nrf52_transceive,
};

/* i2c & spi (SPIM, SPIS, SPI) instance with the same id (e.g. I2C_0 and SPI_0)
 * can NOT be used at the same time on nRF5x chip family.
 */
#if defined(CONFIG_SPIM0_NRF52) && !defined(CONFIG_I2C_0)
static void spim_nrf52_config_func_0(struct device *dev);

static const struct spim_nrf52_config spim_nrf52_config_0 = {
	.base = NRF_SPIM0,
	.irq_config_func = spim_nrf52_config_func_0,
	.default_cfg = {
		.config = CONFIG_SPI_0_DEFAULT_CFG,
		.max_sys_freq = CONFIG_SPI_0_DEFAULT_BAUD_RATE,
	},
	.psel = {
		.sck = CONFIG_SPIM0_NRF52_GPIO_SCK_PIN,
		.mosi = CONFIG_SPIM0_NRF52_GPIO_MOSI_PIN,
		.miso = CONFIG_SPIM0_NRF52_GPIO_MISO_PIN,
		.ss = { CONFIG_SPIM0_NRF52_GPIO_SS_PIN_0,
			CONFIG_SPIM0_NRF52_GPIO_SS_PIN_1,
			CONFIG_SPIM0_NRF52_GPIO_SS_PIN_2,
			CONFIG_SPIM0_NRF52_GPIO_SS_PIN_3 },
	},
	.orc = CONFIG_SPIM0_NRF52_ORC,
};

static struct spim_nrf52_data spim_nrf52_data_0;

DEVICE_AND_API_INIT(spim_nrf52_0, CONFIG_SPI_0_NAME, spim_nrf52_init,
		    &spim_nrf52_data_0, &spim_nrf52_config_0,
		    POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEVICE,
		    &spim_nrf52_driver_api);

static void spim_nrf52_config_func_0(struct device *dev)
{
	IRQ_CONNECT(NRF5_IRQ_SPI0_TWI0_IRQn, CONFIG_SPI_0_IRQ_PRI,
		    spim_nrf52_isr, DEVICE_GET(spim_nrf52_0), 0);

	irq_enable(NRF5_IRQ_SPI0_TWI0_IRQn);
}
#endif /* CONFIG_SPIM0_NRF52 && !CONFIG_I2C_0 */

#if defined(CONFIG_SPIM1_NRF52) && !defined(CONFIG_I2C_1)
static void spim_nrf52_config_func_1(struct device *dev);

static const struct spim_nrf52_config spim_nrf52_config_1 = {
	.base = NRF_SPIM1,
	.irq_config_func = spim_nrf52_config_func_1,
	.default_cfg = {
		.config = CONFIG_SPI_1_DEFAULT_CFG,
		.max_sys_freq = CONFIG_SPI_1_DEFAULT_BAUD_RATE,
	},
	.psel = {
		.sck = CONFIG_SPIM1_NRF52_GPIO_SCK_PIN,
		.mosi = CONFIG_SPIM1_NRF52_GPIO_MOSI_PIN,
		.miso = CONFIG_SPIM1_NRF52_GPIO_MISO_PIN,
		.ss = { CONFIG_SPIM1_NRF52_GPIO_SS_PIN_0,
			CONFIG_SPIM1_NRF52_GPIO_SS_PIN_1,
			CONFIG_SPIM1_NRF52_GPIO_SS_PIN_2,
			CONFIG_SPIM1_NRF52_GPIO_SS_PIN_3 },
	},
	.orc = CONFIG_SPIM1_NRF52_ORC,
};

static struct spim_nrf52_data spim_nrf52_data_1;

DEVICE_AND_API_INIT(spim_nrf52_1, CONFIG_SPI_1_NAME, spim_nrf52_init,
		    &spim_nrf52_data_1, &spim_nrf52_config_1,
		    POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEVICE,
		    &spim_nrf52_driver_api);

static void spim_nrf52_config_func_1(struct device *dev)
{
	IRQ_CONNECT(NRF5_IRQ_SPI1_TWI1_IRQn, CONFIG_SPI_1_IRQ_PRI,
		    spim_nrf52_isr, DEVICE_GET(spim_nrf52_1), 0);

	irq_enable(NRF5_IRQ_SPI1_TWI1_IRQn);
}
#endif /* CONFIG_SPIM1_NRF52 && !CONFIG_I2C_1 */