STM32F4(Nucleo)SPI线路不传输

时间:2017-07-12 15:52:20

标签: arm stm coocox nucleo

我似乎无法让我的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备用功能。真的很感激任何帮助

1 个答案:

答案 0 :(得分:0)

使用当前代码,您似乎实际上并未设置SPI实例。您没有设置SPI_InitTypeDef的{​​{1}}。将您的SPI_HandleTypeDef替换为SPItest

SPIinit.Init