|
| 1 | +/* spi_drv.h |
| 2 | + * |
| 3 | + * Driver for the SPI back-end of the SPI_FLASH module. |
| 4 | + * |
| 5 | + * Example implementation for stm32F4, using SPI1. |
| 6 | + * |
| 7 | + * Pinout: see spi_drv_stm32f4.h |
| 8 | + * |
| 9 | + * Copyright (C) 2018 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_stm32f4.h" |
| 30 | + |
| 31 | +void spi_cs_off(void) |
| 32 | +{ |
| 33 | + int i; |
| 34 | + GPIOE_BSRR |= (1 << SPI_FLASH_PIN); |
| 35 | + while(!(GPIOE_ODR & (1 << SPI_FLASH_PIN))) |
| 36 | + ; |
| 37 | + for(i = 0; i < 168000; i++) |
| 38 | + ; |
| 39 | +} |
| 40 | + |
| 41 | +void spi_cs_on(void) |
| 42 | +{ |
| 43 | + GPIOE_BSRR |= (1 << (SPI_FLASH_PIN + 16)); |
| 44 | + while(GPIOE_ODR & (1 << SPI_FLASH_PIN)) |
| 45 | + ; |
| 46 | +} |
| 47 | + |
| 48 | + |
| 49 | +static void spi_flash_pin_setup(void) |
| 50 | +{ |
| 51 | + uint32_t reg; |
| 52 | + AHB1_CLOCK_ER |= GPIOE_AHB1_CLOCK_ER; |
| 53 | + reg = GPIOE_MODE & ~ (0x03 << (SPI_FLASH_PIN * 2)); |
| 54 | + GPIOE_MODE = reg | (1 << (SPI_FLASH_PIN * 2)); |
| 55 | + |
| 56 | + reg = GPIOE_PUPD & ~(0x03 << (SPI_FLASH_PIN * 2)); |
| 57 | + GPIOE_PUPD = reg | (0x01 << (SPI_FLASH_PIN * 2)); |
| 58 | + |
| 59 | + reg = GPIOE_OSPD & ~(0x03 << (SPI_FLASH_PIN * 2)); |
| 60 | + GPIOE_OSPD |= (0x03 << (SPI_FLASH_PIN * 2)); |
| 61 | + |
| 62 | +} |
| 63 | + |
| 64 | +static void spi1_pins_setup(void) |
| 65 | +{ |
| 66 | + uint32_t reg; |
| 67 | + AHB1_CLOCK_ER |= GPIOB_AHB1_CLOCK_ER; |
| 68 | + /* Set mode = AF */ |
| 69 | + reg = GPIOB_MODE & ~ (0x03 << (SPI1_CLOCK_PIN * 2)); |
| 70 | + GPIOB_MODE = reg | (2 << (SPI1_CLOCK_PIN * 2)); |
| 71 | + reg = GPIOB_MODE & ~ (0x03 << (SPI1_MOSI_PIN * 2)); |
| 72 | + GPIOB_MODE = reg | (2 << (SPI1_MOSI_PIN * 2)); |
| 73 | + reg = GPIOB_MODE & ~ (0x03 << (SPI1_MISO_PIN * 2)); |
| 74 | + GPIOB_MODE = reg | (2 << (SPI1_MISO_PIN * 2)); |
| 75 | + |
| 76 | + /* Alternate function: use low pins (5,6,7) */ |
| 77 | + reg = GPIOB_AFL & ~(0xf << ((SPI1_CLOCK_PIN) * 4)); |
| 78 | + GPIOB_AFL = reg | (SPI1_PIN_AF << ((SPI1_CLOCK_PIN) * 4)); |
| 79 | + reg = GPIOB_AFL & ~(0xf << ((SPI1_MOSI_PIN) * 4)); |
| 80 | + GPIOB_AFL = reg | (SPI1_PIN_AF << ((SPI1_MOSI_PIN) * 4)); |
| 81 | + reg = GPIOB_AFL & ~(0xf << ((SPI1_MISO_PIN) * 4)); |
| 82 | + GPIOB_AFL = reg | (SPI1_PIN_AF << ((SPI1_MISO_PIN) * 4)); |
| 83 | +} |
| 84 | + |
| 85 | +static void spi_pins_release(void) |
| 86 | +{ |
| 87 | + uint32_t reg; |
| 88 | + /* Set mode = 0 */ |
| 89 | + GPIOB_MODE &= ~ (0x03 << (SPI1_CLOCK_PIN * 2)); |
| 90 | + GPIOB_MODE &= ~ (0x03 << (SPI1_MOSI_PIN * 2)); |
| 91 | + GPIOB_MODE &= ~ (0x03 << (SPI1_MISO_PIN * 2)); |
| 92 | + |
| 93 | + /* Alternate function clear */ |
| 94 | + GPIOB_AFL &= ~(0xf << ((SPI1_CLOCK_PIN) * 4)); |
| 95 | + GPIOB_AFL &= ~(0xf << ((SPI1_MOSI_PIN) * 4)); |
| 96 | + GPIOB_AFL &= ~(0xf << ((SPI1_MISO_PIN) * 4)); |
| 97 | + |
| 98 | + /* Floating */ |
| 99 | + GPIOB_PUPD &= ~ (0x03 << (SPI1_CLOCK_PIN * 2)); |
| 100 | + GPIOB_PUPD &= ~ (0x03 << (SPI1_MOSI_PIN * 2)); |
| 101 | + GPIOB_PUPD &= ~ (0x03 << (SPI1_MISO_PIN * 2)); |
| 102 | + |
| 103 | + /* Release CS */ |
| 104 | + GPIOE_MODE &= ~ (0x03 << (SPI_FLASH_PIN * 2)); |
| 105 | + GPIOE_PUPD &= ~ (0x03 << (SPI_FLASH_PIN * 2)); |
| 106 | + |
| 107 | + /* Disable GPIOB+GPIOE clock */ |
| 108 | + AHB1_CLOCK_ER &= ~(GPIOB_AHB1_CLOCK_ER | GPIOE_AHB1_CLOCK_ER); |
| 109 | +} |
| 110 | + |
| 111 | +static void spi1_reset(void) |
| 112 | +{ |
| 113 | + APB2_CLOCK_RST |= SPI1_APB2_CLOCK_ER_VAL; |
| 114 | + APB2_CLOCK_RST &= ~SPI1_APB2_CLOCK_ER_VAL; |
| 115 | +} |
| 116 | + |
| 117 | +uint8_t spi_read(void) |
| 118 | +{ |
| 119 | + volatile uint32_t reg; |
| 120 | + do { |
| 121 | + reg = SPI1_SR; |
| 122 | + } while(!(reg & SPI_SR_RX_NOTEMPTY)); |
| 123 | + return (uint8_t)SPI1_DR; |
| 124 | +} |
| 125 | + |
| 126 | +void spi_write(const char byte) |
| 127 | +{ |
| 128 | + int i; |
| 129 | + volatile uint32_t reg; |
| 130 | + do { |
| 131 | + reg = SPI1_SR; |
| 132 | + } while ((reg & SPI_SR_TX_EMPTY) == 0); |
| 133 | + SPI1_DR = byte; |
| 134 | + do { |
| 135 | + reg = SPI1_SR; |
| 136 | + } while ((reg & SPI_SR_TX_EMPTY) == 0); |
| 137 | +} |
| 138 | + |
| 139 | + |
| 140 | +void spi_init(int polarity, int phase) |
| 141 | +{ |
| 142 | + spi1_pins_setup(); |
| 143 | + spi_flash_pin_setup(); |
| 144 | + APB2_CLOCK_ER |= SPI1_APB2_CLOCK_ER_VAL; |
| 145 | + spi1_reset(); |
| 146 | + SPI1_CR1 = SPI_CR1_MASTER | (5 << 3) | (polarity << 1) | (phase << 0); |
| 147 | + SPI1_CR2 |= SPI_CR2_SSOE; |
| 148 | + SPI1_CR1 |= SPI_CR1_SPI_EN; |
| 149 | +} |
| 150 | + |
| 151 | +void spi_release(void) |
| 152 | +{ |
| 153 | + spi1_reset(); |
| 154 | + SPI1_CR2 &= ~SPI_CR2_SSOE; |
| 155 | + SPI1_CR1 = 0; |
| 156 | + spi_pins_release(); |
| 157 | +} |
| 158 | + |
0 commit comments