|
| 1 | +/* spi_drv_nrf52.c |
| 2 | + * |
| 3 | + * Driver for the SPI back-end of the SPI_FLASH module. |
| 4 | + * |
| 5 | + * Example implementation for nrf52F4. |
| 6 | + * |
| 7 | + * Pinout: see spi_drv_nrf52.h |
| 8 | + * |
| 9 | + * Copyright (C) 2020 wolfSSL Inc. |
| 10 | + * |
| 11 | + * This file is part of wolfBoot. |
| 12 | + * |
| 13 | + * wolfBoot is free software; you can redistribute it and/or modify |
| 14 | + * it under the terms of the GNU General Public License as published by |
| 15 | + * the Free Software Foundation; either version 2 of the License, or |
| 16 | + * (at your option) any later version. |
| 17 | + * |
| 18 | + * wolfBoot is distributed in the hope that it will be useful, |
| 19 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | + * GNU General Public License for more details. |
| 22 | + * |
| 23 | + * You should have received a copy of the GNU General Public License |
| 24 | + * along with this program; if not, write to the Free Software |
| 25 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA |
| 26 | + */ |
| 27 | +#include <stdint.h> |
| 28 | +#include "spi_drv.h" |
| 29 | +#include "spi_drv_nrf52.h" |
| 30 | + |
| 31 | +#define SPI0 (0x40003000) |
| 32 | +#define SPI1 (0x40004000) |
| 33 | +#define SPI2 (0x40023000) |
| 34 | + |
| 35 | +#define SPI SPI0 |
| 36 | +#define SPI_TASKS_START *((volatile uint32_t *)(SPI + 0x10)) |
| 37 | +#define SPI_TASKS_STOP *((volatile uint32_t *)(SPI + 0x14)) |
| 38 | +#define SPI_EVENTS_ENDRX *((volatile uint32_t *)(SPI + 0x110)) |
| 39 | +#define SPI_EVENTS_END *((volatile uint32_t *)(SPI + 0x118)) |
| 40 | +#define SPI_EVENTS_ENDTX *((volatile uint32_t *)(SPI + 0x120)) |
| 41 | +#define SPI_EV_RDY *((volatile uint32_t *)(SPI + 0x108)) |
| 42 | +#define SPI_INTENSET *((volatile uint32_t *)(SPI + 0x304)) |
| 43 | +#define SPI_INTENCLR *((volatile uint32_t *)(SPI + 0x308)) |
| 44 | +#define SPI_ENABLE *((volatile uint32_t *)(SPI + 0x500)) |
| 45 | +#define SPI_PSEL_SCK *((volatile uint32_t *)(SPI + 0x508)) |
| 46 | +#define SPI_PSEL_MOSI *((volatile uint32_t *)(SPI + 0x50C)) |
| 47 | +#define SPI_PSEL_MISO *((volatile uint32_t *)(SPI + 0x510)) |
| 48 | +#define SPI_RXDATA *((volatile uint32_t *)(SPI + 0x518)) |
| 49 | +#define SPI_TXDATA *((volatile uint32_t *)(SPI + 0x51C)) |
| 50 | +#define SPI_FREQUENCY *((volatile uint32_t *)(SPI + 0x524)) |
| 51 | +#define SPI_CONFIG *((volatile uint32_t *)(SPI + 0x554)) |
| 52 | + |
| 53 | +#define K125 0x02000000 |
| 54 | +#define K250 0x04000000 |
| 55 | +#define K500 0x08000000 |
| 56 | +#define M1 0x10000000 |
| 57 | +#define M2 0x20000000 |
| 58 | +#define M4 0x40000000 |
| 59 | +#define M8 0x80000000 |
| 60 | + |
| 61 | +void spi_cs_off(int pin) |
| 62 | +{ |
| 63 | + GPIO_OUTSET = (1 << pin); |
| 64 | + while ((GPIO_OUT & (1 << pin)) == 0) |
| 65 | + ; |
| 66 | +} |
| 67 | + |
| 68 | +void spi_cs_on(int pin) |
| 69 | +{ |
| 70 | + GPIO_OUTCLR = (1 << pin); |
| 71 | + while ((GPIO_OUT & (1 << pin)) != 0) |
| 72 | + ; |
| 73 | + |
| 74 | +} |
| 75 | + |
| 76 | +uint8_t spi_read(void) |
| 77 | +{ |
| 78 | + volatile uint32_t reg = SPI_EV_RDY; |
| 79 | + while (!reg) |
| 80 | + reg = SPI_EV_RDY; |
| 81 | + reg = SPI_RXDATA; |
| 82 | + SPI_EV_RDY = 0; |
| 83 | + return reg; |
| 84 | +} |
| 85 | + |
| 86 | +void spi_write(const char byte) |
| 87 | +{ |
| 88 | + uint32_t reg; |
| 89 | + SPI_EV_RDY = 0; |
| 90 | + SPI_TXDATA = (uint32_t)byte; |
| 91 | + reg = SPI_EV_RDY; |
| 92 | + while (!reg) |
| 93 | + reg = SPI_EV_RDY; |
| 94 | +} |
| 95 | + |
| 96 | + |
| 97 | +void spi_init(int polarity, int phase) |
| 98 | +{ |
| 99 | + static int initialized = 0; |
| 100 | + if (!initialized) { |
| 101 | + initialized++; |
| 102 | + GPIO_PIN_CNF[SPI_CS_PIN] = GPIO_CNF_OUT; |
| 103 | + GPIO_PIN_CNF[SPI_SCLK_PIN] = GPIO_CNF_OUT; |
| 104 | + GPIO_PIN_CNF[SPI_MOSI_PIN] = GPIO_CNF_OUT; |
| 105 | + GPIO_PIN_CNF[SPI_MISO_PIN] = GPIO_CNF_IN; |
| 106 | + //GPIO_DIRSET = ((1 << SPI_CS_PIN) | (1 << SPI_SCLK_PIN) | (1 << SPI_MOSI_PIN)); |
| 107 | + GPIO_OUTSET = (1 << SPI_CS_PIN); |
| 108 | + GPIO_OUTCLR = (1 << SPI_MOSI_PIN) | (1 << SPI_SCLK_PIN); |
| 109 | + |
| 110 | + SPI_PSEL_MISO = SPI_MISO_PIN; |
| 111 | + SPI_PSEL_MOSI = SPI_MOSI_PIN; |
| 112 | + SPI_PSEL_SCK = SPI_SCLK_PIN; |
| 113 | + |
| 114 | + SPI_FREQUENCY = M1; |
| 115 | + SPI_CONFIG = 0; /* mode 0,0 default */ |
| 116 | + SPI_ENABLE = 1; |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +void spi_release(void) |
| 121 | +{ |
| 122 | + |
| 123 | +} |
0 commit comments