-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy pathspi_drv_nrf52.c
118 lines (103 loc) · 2.91 KB
/
spi_drv_nrf52.c
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
/* spi_drv_nrf52.c
*
* Driver for the SPI back-end of the SPI_FLASH module.
*
* Example implementation for nrf52F4.
*
* Pinout: see spi_drv_nrf52.h
*
* Copyright (C) 2021 wolfSSL Inc.
*
* This file is part of wolfBoot.
*
* wolfBoot is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* wolfBoot is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/
#include <stdint.h>
#include "spi_drv.h"
#ifdef TARGET_nrf52
#if defined(SPI_FLASH) || defined(WOLFBOOT_TPM)
#include "hal/nrf52.h"
void RAMFUNCTION spi_cs_off(uint32_t base, int pin)
{
GPIO_OUTSET = (1 << pin);
while ((GPIO_OUT & (1 << pin)) == 0)
;
(void)base;
}
void RAMFUNCTION spi_cs_on(uint32_t base, int pin)
{
GPIO_OUTCLR = (1 << pin);
while ((GPIO_OUT & (1 << pin)) != 0)
;
(void)base;
}
uint8_t RAMFUNCTION spi_read(void)
{
volatile uint32_t reg = SPI_EV_RDY;
while (!reg)
reg = SPI_EV_RDY;
reg = SPI_RXDATA;
SPI_EV_RDY = 0;
return reg;
}
void RAMFUNCTION spi_write(const char byte)
{
uint32_t reg;
SPI_EV_RDY = 0;
SPI_TXDATA = (uint32_t)byte;
reg = SPI_EV_RDY;
while (!reg)
reg = SPI_EV_RDY;
}
void spi_init(int polarity, int phase)
{
static int initialized = 0;
if (!initialized) {
initialized++;
GPIO_PIN_CNF[SPI_CS_PIN] = GPIO_CNF_OUT;
GPIO_PIN_CNF[SPI_SCLK_PIN] = GPIO_CNF_OUT;
GPIO_PIN_CNF[SPI_MOSI_PIN] = GPIO_CNF_OUT;
GPIO_PIN_CNF[SPI_MISO_PIN] = GPIO_CNF_IN;
//GPIO_DIRSET = ((1 << SPI_CS_PIN) | (1 << SPI_SCLK_PIN) | (1 << SPI_MOSI_PIN));
GPIO_OUTSET = (1 << SPI_CS_PIN);
GPIO_OUTCLR = (1 << SPI_MOSI_PIN) | (1 << SPI_SCLK_PIN);
SPI_PSEL_MISO = SPI_MISO_PIN;
SPI_PSEL_MOSI = SPI_MOSI_PIN;
SPI_PSEL_SCK = SPI_SCLK_PIN;
SPI_FREQUENCY = M1;
SPI_CONFIG = 0; /* mode 0,0 default */
SPI_ENABLE = 1;
}
}
void spi_release(void)
{
}
#ifdef WOLFBOOT_TPM
int spi_xfer(int cs, const uint8_t* tx, uint8_t* rx, uint32_t sz, int flags)
{
uint32_t i;
spi_cs_on(SPI_CS_TPM_PIO_BASE, cs);
for (i = 0; i < sz; i++) {
spi_write((const char)tx[i]);
rx[i] = spi_read();
}
if (!(flags & SPI_XFER_FLAG_CONTINUE)) {
spi_cs_off(SPI_CS_TPM_PIO_BASE, cs);
}
return 0;
}
#endif /* WOLFBOOT_TPM */
#endif /* SPI_FLASH || WOLFBOOT_TPM */
#endif /* TARGET_ */