你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

STM32用户手册哪里下载 哪里有分类的

[复制链接]
七颗咖啡豆${PWD} 提问时间:2024-12-25 15:04 / 已解决

STM32L476

收藏 评论5 发布时间:2024-12-25 15:04

举报

5个回答
七颗咖啡豆${PWD} 最优答案 回答时间:前天 15:28

image.png

生成的代码没有初始化 State,导致HAL_SPI_MspInit函数没有被调用啊。。。。。

BUG这么多么

七颗咖啡豆${PWD} 回答时间:前天 15:08

具体问题就是使用NUCLEO-L476RG 板子 SPI有问题。

SPI1 回环测试,把MOSI---MISO 短接 然后发数据,每次都报超时错误。

仿真去看是 SPI_FLAG_TXE 这个没有set.

void spi_send(const uint8_t pData, uint16_t Size) { //HAL_StatusTypeDef ret = HAL_SPI_Transmit_DMA(&hspi1,pData,Size); uint16_t a = (uint16_t*)pData; LL_GPIO_ResetOutputPin(GPIOB, LL_GPIO_PIN_6); //HAL_StatusTypeDef ret = HAL_SPI_TransmitReceive(&hspi1,pData,rx,Size,1000); HAL_StatusTypeDef ret = HAL_SPI_Transmit(&hspi1,pData,Size,1000); LL_GPIO_SetOutputPin(GPIOB, LL_GPIO_PIN_6); printf("HAL_SPI_Transmit [%d] err=%d\n",a,ret); }

星海灿烂 回答时间:前天 15:29

七颗咖啡豆${PWD} 发表于 2024-12-25 15:28
![image.png](data/attachment/forum/202412/25/152713z5cl9jl5t95c5zzc.png?imageMogr2/auto-orient/s ...

[md]STM32CubeMX是有一些BUG的,之前用STM32L151 生成定时器ETR的功能,结果复用功能没有开启起来

七颗咖啡豆${PWD} 回答时间:前天 18:26

分享一下最终版本 三种方式都可成功发送

/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file    spi.c
  * @brief   This file provides code for the configuration
  *          of the SPI instances.
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2024 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "spi.h"

/* USER CODE BEGIN 0 */
#include "string.h"
/* USER CODE END 0 */

SPI_HandleTypeDef hspi1;
DMA_HandleTypeDef hdma_spi1_rx;
DMA_HandleTypeDef hdma_spi1_tx;

/* SPI1 init function */
void MX_SPI1_Init(void)
{

  /* USER CODE BEGIN SPI1_Init 0 */

  /* USER CODE END SPI1_Init 0 */

  /* USER CODE BEGIN SPI1_Init 1 */
    memset(&hspi1,0,sizeof(hspi1));
  /* USER CODE END SPI1_Init 1 */
  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_128;
  hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
  hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
  hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  hspi1.Init.CRCPolynomial = 7;
  hspi1.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;
  hspi1.Init.NSSPMode = SPI_NSS_PULSE_DISABLE;
  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* spiHandle)
{

  GPIO_InitTypeDef GPIO_InitStruct = {0};
  if(spiHandle->Instance==SPI1)
  {
  /* USER CODE BEGIN SPI1_MspInit 0 */

  /* USER CODE END SPI1_MspInit 0 */
    /* SPI1 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_6|GPIO_PIN_7;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF5_SPI1;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    /* SPI1 DMA Init */
    /* SPI1_RX Init */
    hdma_spi1_rx.Instance = DMA1_Channel2;
    hdma_spi1_rx.Init.Request = DMA_REQUEST_1;
    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(spiHandle,hdmarx,hdma_spi1_rx);

    /* SPI1_TX Init */
    hdma_spi1_tx.Instance = DMA1_Channel3;
    hdma_spi1_tx.Init.Request = DMA_REQUEST_1;
    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(spiHandle,hdmatx,hdma_spi1_tx);

    /* 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 */
  }
}

void HAL_SPI_MspDeInit(SPI_HandleTypeDef* spiHandle)
{

  if(spiHandle->Instance==SPI1)
  {
  /* USER CODE BEGIN SPI1_MspDeInit 0 */

  /* USER CODE END SPI1_MspDeInit 0 */
    /* Peripheral clock disable */
    __HAL_RCC_SPI1_CLK_DISABLE();

    /**SPI1 GPIO Configuration
    PA5     ------> SPI1_SCK
    PA6     ------> SPI1_MISO
    PA7     ------> SPI1_MOSI
    */
    HAL_GPIO_DeInit(GPIOA, GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7);

    /* SPI1 DMA DeInit */
    HAL_DMA_DeInit(spiHandle->hdmarx);
    HAL_DMA_DeInit(spiHandle->hdmatx);

    /* SPI1 interrupt Deinit */
    HAL_NVIC_DisableIRQ(SPI1_IRQn);
  /* USER CODE BEGIN SPI1_MspDeInit 1 */

  /* USER CODE END SPI1_MspDeInit 1 */
  }
}

/* USER CODE BEGIN 1 */
//发送完毕拉高CS
void HAL_SPI_TxCpltCallback(SPI_HandleTypeDef *hspi)
{
  /* Prevent unused argument(s) compilation warning */
  UNUSED(hspi);
    LL_GPIO_SetOutputPin(GPIOB, LL_GPIO_PIN_6);
  /* NOTE : This function should not be modified, when the callback is needed,
            the HAL_SPI_TxCpltCallback should be implemented in the user file
   */
}

uint8_t rx[4096];
/*
中断或者DMA方式发送
*/
void spi_send(const uint8_t *pData, uint16_t Size)
{

        //uint16_t a = *(uint16_t*)pData;
        LL_GPIO_ResetOutputPin(GPIOB, LL_GPIO_PIN_6);
        HAL_StatusTypeDef ret = HAL_SPI_Transmit_DMA(&hspi1,pData,Size);
        //HAL_StatusTypeDef ret = HAL_SPI_Transmit_IT(&hspi1,pData,Size);

        //LL_GPIO_SetOutputPin(GPIOB, LL_GPIO_PIN_6);

        //printf("HAL_SPI_Transmit [%d] err=%d\n",a,ret);
}
/*
阻塞式发送
*/
void spi_send2(const uint8_t *pData, uint16_t Size)
{

        //uint16_t a = *(uint16_t*)pData;
        LL_GPIO_ResetOutputPin(GPIOB, LL_GPIO_PIN_6);
        //HAL_StatusTypeDef ret = HAL_SPI_Transmit_DMA(&hspi1,pData,Size);
        HAL_StatusTypeDef ret = HAL_SPI_Transmit(&hspi1,pData,Size,1000);
        //HAL_StatusTypeDef ret = HAL_SPI_Transmit_IT(&hspi1,pData,Size);
        LL_GPIO_SetOutputPin(GPIOB, LL_GPIO_PIN_6);

        //printf("HAL_SPI_Transmit [%d] err=%d\n",a,ret);
}
/* USER CODE END 1 */

所属标签

相似问题

官网相关资源

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32Cube扩展软件包
意法半导体边缘AI套件
ST - 理想汽车豪华SUV案例
ST意法半导体智能家居案例
STM32 ARM Cortex 32位微控制器
关注我们
st-img 微信公众号
st-img 手机版