0

我似乎无法让我的 SPI 线路工作。尽管它确实在 sck 引脚上生成了时钟信号,但它根本不想传输。

时钟信号的频率为 62 kHz,MOSI 和 MISO 引脚无限期地保持高电平。我已经根据库定义它应该使用的方式构建了代码,尽管我不确定我的传输函数是否不正确。我还进行了错误和状态检查,错误检查返回说没有错误,状态表示 SPI 总线已准备好。这是我的代码:

//*****************************************************************************
//
//! \file main.c
//! \brief main application
//! \version 1.0.0.0
//! \date $Creat_time$
//! \author $Creat_author$
//! \copy
//!
//! Copyright (c) 2014 CooCox.  All rights reserved.
//
//! \addtogroup project
//! @{
//! \addtogroup main
//! @{
//*****************************************************************************
#include "stm32f4xx.h"
#include "stm32f4xx_hal.h"

int main(void)
{
//Initialize Variables
//*****************************************************************************
uint32_t x;
uint32_t x1;
//Variables to check the state of the SPI bus.
HAL_SPI_StateTypeDef t;
HAL_SPI_StateTypeDef t1;
HAL_SPI_StateTypeDef t2;
HAL_SPI_StateTypeDef t3;
HAL_SPI_StateTypeDef t4;
HAL_SPI_StateTypeDef t5;
HAL_SPI_StateTypeDef t6;
HAL_SPI_StateTypeDef t7;
HAL_SPI_StateTypeDef t8;
HAL_SPI_StateTypeDef t9;
uint8_t message = 0xA4;
//*****************************************************************************


//(1)Declare a SPI_HandleTypeDef handle structure, for example: SPI_HandleTypeDef  hspi;
//*****************************************************************************
SPI_HandleTypeDef  SPIinit;
SPIinit.Instance = SPI1;
t1=  HAL_SPI_GetState(&SPIinit);
//*****************************************************************************



//(2)Initialize the SPI low level resources by implementing the HAL_SPI_MspInit ()API:
//*****************************************************************************
HAL_SPI_MspInit(&SPIinit);
t2=  HAL_SPI_GetState(&SPIinit);
//*****************************************************************************




// (3) Enable the SPIx interface clock
//*****************************************************************************
__HAL_RCC_SPI1_CLK_ENABLE();
t3 =  HAL_SPI_GetState(&SPIinit);
//*****************************************************************************


// (4) SPI pins configuration
//(4.a) Enable the clock for the SPI GPIOs
//*****************************************************************************
__HAL_RCC_GPIOB_CLK_ENABLE();
//*****************************************************************************


//(4.b) Configure these SPI pins as alternate function push-pull
//*****************************************************************************

//Configure the SPI SCK,MISO & MOSI pins

GPIO_InitTypeDef NSS;

NSS.Pin       = GPIO_PIN_4;
NSS.Mode      = GPIO_MODE_AF_PP;
NSS.Pull      = GPIO_PULLUP;
NSS.Speed     = GPIO_SPEED_LOW;
NSS.Alternate = GPIO_AF5_SPI1;


//HAL_GPIO_Init(GPIOA, &NSS);


GPIO_InitTypeDef SCK;

SCK.Pin       = GPIO_PIN_3;
SCK.Mode      = GPIO_MODE_AF_PP;
SCK.Pull      = GPIO_PULLUP;
SCK.Speed     = GPIO_SPEED_LOW;
SCK.Alternate = GPIO_AF5_SPI1;

HAL_GPIO_Init(GPIOB, &SCK);


GPIO_InitTypeDef MISO;

MISO.Pin       = GPIO_PIN_4;
MISO.Mode      = GPIO_MODE_AF_PP;
MISO.Pull      = GPIO_PULLUP;
MISO.Speed     = GPIO_SPEED_LOW;
MISO.Alternate = GPIO_AF5_SPI1;

HAL_GPIO_Init(GPIOB, &MISO);


GPIO_InitTypeDef MOSI;

MOSI.Pin       = GPIO_PIN_5;
MOSI.Mode      = GPIO_MODE_AF_PP;
MOSI.Pull      = GPIO_PULLUP;
MOSI.Speed     = GPIO_SPEED_LOW;
MOSI.Alternate = GPIO_AF5_SPI1;

HAL_GPIO_Init(GPIOB, &MOSI);

//Configure the SPI NSS pin

//*****************************************************************************


//(5) Program the Mode, Direction , Data size, Baudrate Prescaler, NSS management, Clock polarity and phase, FirstBit and CRC configuration in the hspi Init structure
//*****************************************************************************

SPI_InitTypeDef SPItest;

SPItest.Mode = SPI_MODE_MASTER;
SPItest.Direction = SPI_DIRECTION_1LINE;
SPItest.DataSize =  SPI_DATASIZE_8BIT;
SPItest.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;
SPItest.CLKPhase = SPI_PHASE_1EDGE;
SPItest.CLKPolarity = SPI_POLARITY_LOW;
SPItest.FirstBit = SPI_FIRSTBIT_LSB;
SPItest.TIMode = SPI_TIMODE_DISABLE ;
SPItest.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
SPItest.NSS = SPI_NSS_SOFT;

t4=  HAL_SPI_GetState(&SPIinit);
//*****************************************************************************

//(6) Initialize the SPI registers by calling the HAL_SPI_Init() API:
//*****************************************************************************
HAL_SPI_Init(&SPIinit);
t5=  HAL_SPI_GetState(&SPIinit);
//*****************************************************************************

__HAL_SPI_ENABLE(&SPIinit);


while(1)

 {

    HAL_GPIO_WritePin(GPIOA,GPIO_PIN_4,GPIO_PIN_RESET);
    //t6=  HAL_SPI_GetState(&SPIinit);
    //x1 = HAL_SPI_GetError(&SPIinit);

    HAL_SPI_Transmit(&SPIinit, &message, 7, 0x01);

//  t8=  HAL_SPI_GetState(&SPIinit);

//  x = HAL_SPI_GetError(&SPIinit);
    HAL_GPIO_WritePin(GPIOA,GPIO_PIN_4,GPIO_PIN_SET);
 }
}

    //  SPI STATE CHECK
// HAL_SPI_STATE_RESET      = 0x00,  /*!< SPI not yet initialized or disabled                */
// HAL_SPI_STATE_READY      = 0x01,  /*!< SPI initialized and ready for use                  */
// HAL_SPI_STATE_BUSY       = 0x02,  /*!< SPI process is ongoing                             */
// HAL_SPI_STATE_BUSY_TX    = 0x12,  /*!< Data Transmission process is ongoing               */
// HAL_SPI_STATE_BUSY_RX    = 0x22,  /*!< Data Reception process is ongoing                  */
// HAL_SPI_STATE_BUSY_TX_RX = 0x32,  /*!< Data Transmission and Reception process is ongoing */
// HAL_SPI_STATE_ERROR      = 0x03   /*!< SPI error state                                    */

//  SPI ERROR CHECK
//#define HAL_SPI_ERROR_NONE         ((uint32_t)0x00000000)   /*!< No error             */
//#define HAL_SPI_ERROR_MODF         ((uint32_t)0x00000001)   /*!< MODF error           */
//#define HAL_SPI_ERROR_CRC          ((uint32_t)0x00000002)   /*!< CRC error            */
//#define HAL_SPI_ERROR_OVR          ((uint32_t)0x00000004)   /*!< OVR error            */
//#define HAL_SPI_ERROR_FRE          ((uint32_t)0x00000008)   /*!< FRE error            */
//#define HAL_SPI_ERROR_DMA          ((uint32_t)0x00000010)   /*!< DMA transfer error   */
//#define HAL_SPI_ERROR_FLAG         ((uint32_t)0x00000010)   /*!< Flag: RXNE,TXE, BSY  */

我已经尝试过 GPIOA 和 GPIOB 备用功能..任何帮助将不胜感激

4

1 回答 1

0

使用您当前的代码,您似乎并没有真正设置您的 SPI 实例。你没有设置SPI_InitTypeDef你的SPI_HandleTypeDef. 替换你SPItestSPIinit.Init

//SPI_InitTypeDef SPItest;

SPIinit.Init.Mode = SPI_MODE_MASTER;
SPIinit.Init.Direction = SPI_DIRECTION_1LINE;
SPIinit.Init.DataSize =  SPI_DATASIZE_8BIT;
SPIinit.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;
SPIinit.Init.CLKPhase = SPI_PHASE_1EDGE;
SPIinit.Init.CLKPolarity = SPI_POLARITY_LOW;
SPIinit.Init.FirstBit = SPI_FIRSTBIT_LSB;
SPIinit.Init.TIMode = SPI_TIMODE_DISABLE ;
SPIinit.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
SPIinit.Init.NSS = SPI_NSS_SOFT;

HAL_SPI_Init(&SPIinit);
于 2017-07-14T13:04:49.713 回答