вот рабочий пример.
#include "agg240064_interface.h" #include "pin.h" #include "stm32f4xx_spi.h" #define GRAPH_IRQ_PRIORITY 15 static Pin_T pinSDA = { .port = GPIOB, .pin = GPIO_Pin_5 }; static Pin_T pinSCK = { .port = GPIOB, .pin = GPIO_Pin_3 }; static Pin_T pinCD = { .port = GPIOB, .pin = GPIO_Pin_4 }; static Pin_T pinCS = { .port = GPIOC, .pin = GPIO_Pin_12 }; static Pin_T pinRST = { .port = GPIOD, .pin = GPIO_Pin_2 }; #define s_SPI SPI1 #define s_SPI_CLK RCC_APB2Periph_SPI1 #define s_SPI_CLK_INIT RCC_APB2PeriphClockCmd #define s_SPI_SCK_PIN GPIO_Pin_3 #define s_SPI_SCK_GPIO_PORT GPIOB #define s_SPI_SCK_GPIO_CLK RCC_AHB1Periph_GPIOB #define s_SPI_SCK_SOURCE GPIO_PinSource3 #define s_SPI_SCK_AF GPIO_AF_SPI1 #define s_SPI_MOSI_PIN GPIO_Pin_5 #define s_SPI_MOSI_GPIO_PORT GPIOB #define s_SPI_MOSI_GPIO_CLK RCC_AHB1Periph_GPIOB #define s_SPI_MOSI_SOURCE GPIO_PinSource5 #define s_SPI_MOSI_AF GPIO_AF_SPI1 #define s_CS_PIN GPIO_Pin_12 #define s_CS_GPIO_PORT GPIOB #define s_CS_GPIO_CLK RCC_AHB1Periph_GPIOB #define SPI_PORT_TX_DMA_CHANNEL DMA_Channel_3 #define SPI_PORT_TX_DMA_STREAM DMA2_Stream3 #define SPI_PORT_DMA DMA2 #define SPI_PORT_DMAx_CLK RCC_AHB1Periph_DMA2 #define SPI_PORT_DMA_TX_IRQHandler DMA2_Stream3_IRQHandler #define SPI_PORT_DMA_TX_IRQn DMA2_Stream3_IRQn static void spi_init(void) { SPI_InitTypeDef SPI_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; DMA_InitTypeDef DMA_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; s_SPI_CLK_INIT(s_SPI_CLK, ENABLE); RCC_AHB1PeriphClockCmd(SPI_PORT_DMAx_CLK, ENABLE); RCC_AHB1PeriphClockCmd(s_SPI_SCK_GPIO_CLK | s_SPI_MOSI_GPIO_CLK | s_CS_GPIO_CLK, ENABLE); /*!< SPI pins configuration *************************************************/ /*!< Connect SPI pins to AF5 */ GPIO_PinAFConfig(s_SPI_SCK_GPIO_PORT, s_SPI_SCK_SOURCE, s_SPI_SCK_AF); //GPIO_PinAFConfig(s_SPI_MISO_GPIO_PORT, s_SPI_MISO_SOURCE, s_SPI_MISO_AF); GPIO_PinAFConfig(s_SPI_MOSI_GPIO_PORT, s_SPI_MOSI_SOURCE, s_SPI_MOSI_AF); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN; GPIO_InitStructure.GPIO_Pin = s_SPI_SCK_PIN; GPIO_Init(s_SPI_SCK_GPIO_PORT, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = s_SPI_MOSI_PIN; GPIO_Init(s_SPI_MOSI_GPIO_PORT, &GPIO_InitStructure); /*!< Configure Card CS pin in output pushpull mode ********************/ GPIO_InitStructure.GPIO_Pin = s_CS_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(s_CS_GPIO_PORT, &GPIO_InitStructure); SPI_I2S_DeInit (s_SPI); SPI_StructInit (&SPI_InitStructure); SPI_InitStructure.SPI_Mode = SPI_Mode_Master; SPI_InitStructure.SPI_Direction = SPI_Direction_1Line_Tx; SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; SPI_Init(s_SPI, &SPI_InitStructure); SPI_Cmd(s_SPI, ENABLE); DMA_DeInit(SPI_PORT_TX_DMA_STREAM); while (DMA_GetCmdStatus (SPI_PORT_TX_DMA_STREAM) != DISABLE); DMA_StructInit(&DMA_InitStructure); DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t) & (s_SPI->DR); DMA_InitStructure.DMA_Channel = SPI_PORT_TX_DMA_CHANNEL; DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_Memory0BaseAddr = 0; DMA_InitStructure.DMA_BufferSize =1 ; DMA_Init(SPI_PORT_TX_DMA_STREAM, &DMA_InitStructure); DMA_ITConfig(SPI_PORT_TX_DMA_STREAM, DMA_IT_TC, ENABLE); NVIC_InitStructure.NVIC_IRQChannel = SPI_PORT_DMA_TX_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = GRAPH_IRQ_PRIORITY; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init (&NVIC_InitStructure); // Enable dma tx request. SPI_I2S_DMACmd (s_SPI, SPI_I2S_DMAReq_Tx, ENABLE); } void SPI_PORT_DMA_TX_IRQHandler() { if (DMA_GetITStatus(SPI_PORT_TX_DMA_STREAM, DMA_IT_TCIF3)) { DMA_ClearITPendingBit(SPI_PORT_TX_DMA_STREAM, DMA_IT_TCIF3); } } void AGG240064_interaface_init() { pin_config(&pinCD, GPIO_Mode_OUT, 0, GPIO_OType_PP, GPIO_Speed_50MHz); pin_config(&pinRST, GPIO_Mode_OUT, 0, GPIO_OType_PP, GPIO_Speed_50MHz); spi_init(); pin_write(&pinRST, 1); } void AGG240064_interaface_reset(uint8_t value) { pin_write(&pinRST, value); } void AGG240064_interaface_write_buffer(uint8_t *buf, uint16_t len) { pin_write(&pinCD, 1); pin_write(&pinCS, 1); while (SPI_I2S_GetFlagStatus(s_SPI, SPI_I2S_FLAG_TXE) == RESET) { }; while (SPI_I2S_GetFlagStatus(s_SPI, SPI_I2S_FLAG_BSY) == SET) { }; SPI_PORT_TX_DMA_STREAM->NDTR = (uint32_t) len; SPI_PORT_TX_DMA_STREAM->M0AR = (uint32_t) buf; DMA_Cmd(SPI_PORT_TX_DMA_STREAM, ENABLE); }