|
| 1 | +#ifndef SPI_DRV_STM32_H_INCLUDED |
| 2 | +#define SPI_DRV_STM32_H_INCLUDED |
| 3 | +#include <stdint.h> |
| 4 | +/** SPI settings **/ |
| 5 | + |
| 6 | +#define SPI1 (0x40013000)/* SPI1 base address */ |
| 7 | +#define SPI1_APB2_CLOCK_ER_VAL (1 << 12) |
| 8 | + |
| 9 | +#define CEN_GPIOA (1 << 0) |
| 10 | +#define CEN_GPIOB (1 << 1) |
| 11 | +#define CEN_GPIOC (1 << 2) |
| 12 | +#define CEN_GPIOD (1 << 3) |
| 13 | +#define CEN_GPIOE (1 << 4) |
| 14 | + |
| 15 | +#ifdef PLATFORM_stm32f4 |
| 16 | +#define APB2_CLOCK_ER (*(volatile uint32_t *)(0x40023844)) |
| 17 | +#define APB2_CLOCK_RST (*(volatile uint32_t *)(0x40023824)) |
| 18 | +#define CLOCK_SPEED (168000000) |
| 19 | +#define RCC_GPIO_CLOCK_ER (*(volatile uint32_t *)(0x40023830)) |
| 20 | +#define GPIOA_BASE (0x40020000) |
| 21 | +#define GPIOB_BASE (0x40020400) |
| 22 | +#define GPIOC_BASE (0x40020800) |
| 23 | +#define GPIOD_BASE (0x40020C00) |
| 24 | +#define GPIOE_BASE (0x40021000) |
| 25 | +#define SPI_GPIO GPIOB_BASE |
| 26 | +#define SPI_CS_GPIO GPIOB_BASE |
| 27 | +#define SPI_PIO_BASE GPIOB_BASE |
| 28 | +#define SPI_CS_PIO_BASE GPIOE_BASE |
| 29 | +#define SPI_CS_FLASH 1 /* Flash CS connected to GPIOE1 */ |
| 30 | +#define SPI_CS_TPM 0 /* TPM CS connected to GPIOE0 */ |
| 31 | +#define SPI1_PIN_AF 5 /* Alternate function for SPI pins */ |
| 32 | +#define SPI1_CLOCK_PIN 3 /* SPI_SCK: PB3 */ |
| 33 | +#define SPI1_MISO_PIN 4 /* SPI_MISO PB4 */ |
| 34 | +#define SPI1_MOSI_PIN 5 /* SPI_MOSI PB5 */ |
| 35 | +#endif |
| 36 | + |
| 37 | +#ifdef PLATFORM_stm32wb |
| 38 | +#define APB2_CLOCK_ER (*(volatile uint32_t *)(0x58000060)) |
| 39 | +#define APB2_CLOCK_RST (*(volatile uint32_t *)(0x58000040)) |
| 40 | +#define SPI_GPIO GPIOA_BASE |
| 41 | +#define RCC_GPIO_CLOCK_ER (*(volatile uint32_t *)(0x5800004C)) |
| 42 | +#define GPIOA_BASE (0x48000000) |
| 43 | +#define GPIOB_BASE (0x48000400) |
| 44 | +#define GPIOC_BASE (0x48000800) |
| 45 | +#define GPIOD_BASE (0x48000C00) |
| 46 | + |
| 47 | +#define SPI_CS_GPIO GPIOA_BASE |
| 48 | +#define SPI_CS_FLASH 4 /* Flash CS connected to GPIOA4 */ |
| 49 | +#define SPI_CS_TPM 0 /* TPM CS connected to GPIOA0 */ |
| 50 | +#define SPI1_PIN_AF 5 /* Alternate function for SPI pins */ |
| 51 | +#define SPI1_CLOCK_PIN 5 /* SPI_SCK: PA5 */ |
| 52 | +#define SPI1_MISO_PIN 6 /* SPI_MISO PA6 */ |
| 53 | +#define SPI1_MOSI_PIN 7 /* SPI_MOSI PA7 */ |
| 54 | +#endif |
| 55 | + |
| 56 | +#define SPI_PIO_BASE SPI_GPIO |
| 57 | +#define SPI_CS_PIO_BASE SPI_CS_GPIO |
| 58 | + |
| 59 | +#if (SPI_GPIO == GPIOA_BASE) |
| 60 | +# define SPI_PIO_CEN CEN_GPIOA |
| 61 | +#elif (SPI_GPIO == GPIOB_BASE) |
| 62 | +# define SPI_PIO_CEN CEN_GPIOB |
| 63 | +#elif (SPI_GPIO == GPIOC_BASE) |
| 64 | +# define SPI_PIO_CEN CEN_GPIOC |
| 65 | +#elif (SPI_GPIO == GPIOD_BASE) |
| 66 | +# define SPI_PIO_CEN CEN_GPIOD |
| 67 | +#elif (SPI_GPIO == GPIOE_BASE) |
| 68 | +# define SPI_PIO_CEN CEN_GPIOE |
| 69 | +#endif |
| 70 | + |
| 71 | +#if (SPI_CS_GPIO == GPIOA_BASE) |
| 72 | +# define SPI_PIO_CS_CEN CEN_GPIOA |
| 73 | +#elif (SPI_CS_GPIO == GPIOB_BASE) |
| 74 | +# define SPI_PIO_CS_CEN CEN_GPIOB |
| 75 | +#elif (SPI_CS_GPIO == GPIOC_BASE) |
| 76 | +# define SPI_PIO_CS_CEN CEN_GPIOC |
| 77 | +#elif (SPI_CS_GPIO == GPIOD_BASE) |
| 78 | +# define SPI_PIO_CS_CEN CEN_GPIOD |
| 79 | +#elif (SPI_CS_GPIO == GPIOE_BASE) |
| 80 | +# define SPI_PIO_CS_CEN CEN_GPIOE |
| 81 | +#endif |
| 82 | + |
| 83 | + |
| 84 | +#define SPI1_CR1 (*(volatile uint32_t *)(SPI1)) |
| 85 | +#define SPI1_CR2 (*(volatile uint32_t *)(SPI1 + 0x04)) |
| 86 | +#define SPI1_SR (*(volatile uint32_t *)(SPI1 + 0x08)) |
| 87 | +#define SPI1_DR (*(volatile uint32_t *)(SPI1 + 0x0c)) |
| 88 | + |
| 89 | +#define SPI_CR1_CLOCK_PHASE (1 << 0) |
| 90 | +#define SPI_CR1_CLOCK_POLARITY (1 << 1) |
| 91 | +#define SPI_CR1_MASTER (1 << 2) |
| 92 | +#define SPI_CR1_BAUDRATE (0x07 << 3) |
| 93 | +#define SPI_CR1_SPI_EN (1 << 6) |
| 94 | +#define SPI_CR1_LSBFIRST (1 << 7) |
| 95 | +#define SPI_CR1_SSI (1 << 8) |
| 96 | +#define SPI_CR1_SSM (1 << 9) |
| 97 | +#define SPI_CR1_16BIT_FORMAT (1 << 11) |
| 98 | +#define SPI_CR1_TX_CRC_NEXT (1 << 12) |
| 99 | +#define SPI_CR1_HW_CRC_EN (1 << 13) |
| 100 | +#define SPI_CR1_BIDIOE (1 << 14) |
| 101 | +#define SPI_CR2_SSOE (1 << 2) |
| 102 | + |
| 103 | + |
| 104 | +#define SPI_SR_RX_NOTEMPTY (1 << 0) |
| 105 | +#define SPI_SR_TX_EMPTY (1 << 1) |
| 106 | +#define SPI_SR_BUSY (1 << 7) |
| 107 | + |
| 108 | + |
| 109 | + |
| 110 | + |
| 111 | + |
| 112 | + |
| 113 | +#define SPI_PIO_MODE (*(volatile uint32_t *)(SPI_PIO_BASE + 0x00)) |
| 114 | +#define SPI_PIO_AFL (*(volatile uint32_t *)(SPI_PIO_BASE + 0x20)) |
| 115 | +#define SPI_PIO_AFH (*(volatile uint32_t *)(SPI_PIO_BASE + 0x24)) |
| 116 | +#define SPI_PIO_OSPD (*(volatile uint32_t *)(SPI_PIO_BASE + 0x08)) |
| 117 | +#define SPI_PIO_PUPD (*(volatile uint32_t *)(SPI_PIO_BASE + 0x0c)) |
| 118 | +#define SPI_PIO_BSRR (*(volatile uint32_t *)(SPI_PIO_BASE + 0x18)) |
| 119 | +#define SPI_PIO_CS_MODE (*(volatile uint32_t *)(SPI_CS_PIO_BASE + 0x00)) |
| 120 | +#define SPI_PIO_CS_AFL (*(volatile uint32_t *)(SPI_CS_PIO_BASE + 0x20)) |
| 121 | +#define SPI_PIO_CS_AFH (*(volatile uint32_t *)(SPI_CS_PIO_BASE + 0x24)) |
| 122 | +#define SPI_PIO_CS_OSPD (*(volatile uint32_t *)(SPI_CS_PIO_BASE + 0x08)) |
| 123 | +#define SPI_PIO_CS_PUPD (*(volatile uint32_t *)(SPI_CS_PIO_BASE + 0x0c)) |
| 124 | +#define SPI_PIO_CS_BSRR (*(volatile uint32_t *)(SPI_CS_PIO_BASE + 0x18)) |
| 125 | +#define SPI_PIO_CS_ODR (*(volatile uint32_t *)(SPI_CS_PIO_BASE + 0x14)) |
| 126 | +#define GPIO_MODE_AF (2) |
| 127 | + |
| 128 | + |
| 129 | +#endif |
0 commit comments