使用CubeMx V6.3 FW_F1 V1.8.4 SPI1配置成DMA模式接收发送。
使用 HAL_SPI_TransmitReceive(&hspi1, (uint8_t *)&dataTx, (uint8_t *)&dataRx, 6, 10000);可以正常操作,
使用HAL_SPI_TransmitReceive_DMA(&hspi1, (uint8_t *)&dataTx, (uint8_t *)&dataRx, 6);就检测不到时钟,数据。
但是会进入DMA发送中断。
void DMA1_Channel3_IRQHandler(void)
{
/* USER CODE BEGIN DMA1_Channel3_IRQn 0 */
/* USER CODE END DMA1_Channel3_IRQn 0 */
HAL_DMA_IRQHandler(&hdma_spi1_tx);
/* USER CODE BEGIN DMA1_Channel3_IRQn 1 */
setCS(HIGH);
/* USER CODE END DMA1_Channel3_IRQn 1 */
}
- static void MX_DMA_Init(void)
- {
- /* DMA controller clock enable */
- __HAL_RCC_DMA1_CLK_ENABLE();
- /* DMA interrupt init */
- /* DMA1_Channel2_IRQn interrupt configuration */
- HAL_NVIC_SetPriority(DMA1_Channel2_IRQn, 2, 0);
- HAL_NVIC_EnableIRQ(DMA1_Channel2_IRQn);
- /* DMA1_Channel3_IRQn interrupt configuration */
- HAL_NVIC_SetPriority(DMA1_Channel3_IRQn, 1, 0);
- HAL_NVIC_EnableIRQ(DMA1_Channel3_IRQn);
- }
- static void MX_SPI1_Init(void)
- {
- /* USER CODE BEGIN SPI1_Init 0 */
- /* USER CODE END SPI1_Init 0 */
- /* USER CODE BEGIN SPI1_Init 1 */
- /* USER CODE END SPI1_Init 1 */
- /* SPI1 parameter configuration*/
- hspi1.Instance = SPI1;
- hspi1.Init.Mode = SPI_MODE_MASTER;
- hspi1.Init.Direction = SPI_DIRECTION_2LINES;
- hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
- hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
- hspi1.Init.CLKPhase = SPI_PHASE_2EDGE;
- hspi1.Init.NSS = SPI_NSS_SOFT;
- hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_4;
- hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
- hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
- hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
- hspi1.Init.CRCPolynomial = 10;
- if (HAL_SPI_Init(&hspi1) != HAL_OK)
- {
- Error_Handler();
- }
- /* USER CODE BEGIN SPI1_Init 2 */
- /* USER CODE END SPI1_Init 2 */
- }
- void HAL_SPI_MspInit(SPI_HandleTypeDef* hspi)
- {
- GPIO_InitTypeDef GPIO_InitStruct = {0};
- if(hspi->Instance==SPI1)
- {
- /* USER CODE BEGIN SPI1_MspInit 0 */
- /* USER CODE END SPI1_MspInit 0 */
- /* Peripheral clock enable */
- __HAL_RCC_SPI1_CLK_ENABLE();
- __HAL_RCC_GPIOA_CLK_ENABLE();
- /**SPI1 GPIO Configuration
- PA5 ------> SPI1_SCK
- PA6 ------> SPI1_MISO
- PA7 ------> SPI1_MOSI
- */
- GPIO_InitStruct.Pin = GPIO_PIN_5|GPIO_PIN_7;
- GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
- HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
- GPIO_InitStruct.Pin = GPIO_PIN_6;
- GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
- /* SPI1 DMA Init */
- /* SPI1_TX Init */
- hdma_spi1_tx.Instance = DMA1_Channel3;
- hdma_spi1_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
- hdma_spi1_tx.Init.PeriphInc = DMA_PINC_DISABLE;
- hdma_spi1_tx.Init.MemInc = DMA_MINC_ENABLE;
- hdma_spi1_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
- hdma_spi1_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
- hdma_spi1_tx.Init.Mode = DMA_NORMAL;
- hdma_spi1_tx.Init.Priority = DMA_PRIORITY_LOW;
- if (HAL_DMA_Init(&hdma_spi1_tx) != HAL_OK)
- {
- Error_Handler();
- }
- __HAL_LINKDMA(hspi,hdmatx,hdma_spi1_tx);
- /* SPI1_RX Init */
- hdma_spi1_rx.Instance = DMA1_Channel2;
- hdma_spi1_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
- hdma_spi1_rx.Init.PeriphInc = DMA_PINC_DISABLE;
- hdma_spi1_rx.Init.MemInc = DMA_MINC_ENABLE;
- hdma_spi1_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
- hdma_spi1_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
- hdma_spi1_rx.Init.Mode = DMA_NORMAL;
- hdma_spi1_rx.Init.Priority = DMA_PRIORITY_LOW;
- if (HAL_DMA_Init(&hdma_spi1_rx) != HAL_OK)
- {
- Error_Handler();
- }
- __HAL_LINKDMA(hspi,hdmarx,hdma_spi1_rx);
- /* SPI1 interrupt Init */
- HAL_NVIC_SetPriority(SPI1_IRQn, 0, 0);
- HAL_NVIC_EnableIRQ(SPI1_IRQn);
- /* USER CODE BEGIN SPI1_MspInit 1 */
- /* USER CODE END SPI1_MspInit 1 */
- }
- else if(hspi->Instance==SPI2)
- {
- /* USER CODE BEGIN SPI2_MspInit 0 */
- /* USER CODE END SPI2_MspInit 0 */
- /* Peripheral clock enable */
- __HAL_RCC_SPI2_CLK_ENABLE();
- __HAL_RCC_GPIOB_CLK_ENABLE();
- /**SPI2 GPIO Configuration
- PB13 ------> SPI2_SCK
- PB14 ------> SPI2_MISO
- PB15 ------> SPI2_MOSI
- */
- GPIO_InitStruct.Pin = GPIO_PIN_13|GPIO_PIN_15;
- GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
- HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
- GPIO_InitStruct.Pin = GPIO_PIN_14;
- GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
- /* USER CODE BEGIN SPI2_MspInit 1 */
- /* USER CODE END SPI2_MspInit 1 */
- }
- }
- int main(void)
- {
- /* USER CODE BEGIN 1 */
- /* USER CODE END 1 */
- /* MCU Configuration--------------------------------------------------------*/
- /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
- HAL_Init();
- /* USER CODE BEGIN Init */
- /* USER CODE END Init */
- /* Configure the system clock */
- SystemClock_Config();
- /* USER CODE BEGIN SysInit */
- /* USER CODE END SysInit */
- /* Initialize all configured peripherals */
- MX_GPIO_Init();
- MX_SPI1_Init();
- MX_SPI2_Init();
- MX_USART1_UART_Init();
- MX_DMA_Init();
- MX_USART2_UART_Init();
- /* USER CODE BEGIN 2 */
-
- WriteData(REG_SOFTWARE_RESET,0,0);//DAC¸´Î»
- WriteData(REG_WRITE_UPDATE_DAC_CHANNEL,DAC_A,16384);//µ÷ÊÔÉèÒ»¸öDAC
- WriteData(REG_WRITE_UPDATE_DAC_CHANNEL,DAC_B,16384);//µ÷ÊÔÉèÒ»¸öDAC
-
- HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0|GPIO_PIN_1, GPIO_PIN_RESET);
- HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, GPIO_PIN_RESET);
- HAL_GPIO_WritePin(GPIOB, GPIO_PIN_9, GPIO_PIN_SET);
- setCS(LOW);
- //HAL_SPI_TransmitReceive(&hspi1, (uint8_t *)&dataTx, (uint8_t *)&dataRx, 6, 10000);
-
- HAL_SPI_TransmitReceive_DMA(&hspi1, (uint8_t *)&dataTx, (uint8_t *)&dataRx, 6);
- // hspi1->Instance->CR3 |= USART_CR3_DMAT;//ʹÄÜ´®¿ÚDMA·¢ËÍ
- //setCS(HIGH);
- // adcStartup();
- // adc_channel_data ADCtemp;
- /* USER CODE END 2 */
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
- while (1)
复制代码
|
STM32Cube_FW_F1_V1.8.0\Projects\STM32F103RB-Nucleo\Examples\SPI\SPI_FullDuplex_ComDMA
发现在初始化的时候竟然出错, hdma->Instance->CCR = tmp;没有赋值成功,不知道是什么原因造成的
发现在DMA初始化的并未成功,hdma->Instance->CCR = tmp;这句赋值出错 ,不知道是什么原因
MX_GPIO_Init();
MX_SPI1_Init();
MX_SPI2_Init();
MX_USART1_UART_Init();
MX_DMA_Init();
MX_USART2_UART_Init();
这段程序先配置SPI1的DMA,再打开DMA的时钟。实际上DMA未开时钟,写操作是无效的。
这个跟初始化顺序有点关系,这里有个相关话题。一个跟初始化顺序有关的异常话题 (qq.com)
文章里讲的是:用户在整理代码过程中无意调整了二者的初始化顺序。可是我并没有调整顺序,而且每次产生的的代码都是开DMA外部时钟在后面