我正在尝试使用STM32L4R5ZI Nucleo板从ADE7913 Eval-Z板读取IWV寄存器。由于某些我无法理解的原因,我无法完成相同的操作。我正在使用HAL_SPI_Transmit()传输命令字节以开始读取寄存器IWV(地址:0x00)。我要传输的值为0x04,但HAL_SPI_Transmit返回HAL_ERROR。
我逐行调试,发现问题出在哪里。它位于SPI_EndRxTxTranction()中,该函数用于检查交易结束。
我已经附上了代码文件。请有人检查并告诉我我要去哪里错了。我已经初始化了SPI参数以及SPI外围设备和CS(3个I / O引脚)的GPIO参数。并且还遵循了从ADE7913读取内容的逻辑结构。
请让我知道我在哪里出错。
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/** @addtogroup STM32L4xx_HAL_Examples
* @{
*/
/** @addtogroup Templates
* @{
*/
/* Private typedef -----------------------------------------------------------*/
SPI_HandleTypeDef SPI_handler;
GPIO_InitTypeDef GPIO_handler;
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
uint8_t write_instruction[1]={0x04};
uint8_t data[1];
/* Private function prototypes -----------------------------------------------*/
static void SystemClock_Config(void);
void Error_Handler(void);
void HAL_SPI_TxCpltCallback(SPI_HandleTypeDef *hspi);
void HAL_SPI_RxCpltCallback(SPI_HandleTypeDef *hspi);
/* Private functions ---------------------------------------------------------*/
/**
* @brief Main program
* @param None
* @retval None
*/
int main(void)
{
/* STM32L4xx HAL library initialization:
- Configure the Flash prefetch, Flash preread and Buffer caches
- Systick timer is configured by default as source of time base, but user
can eventually implement his proper time base source (a general purpose
timer for example or other time source), keeping in mind that Time base
duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and
handled in milliseconds basis.
- Low Level Initialization
*/
HAL_Init();
/* Configure the System clock to have a frequency of 120 MHz */
SystemClock_Config();
BSP_LED_Init(LED1);
BSP_LED_Init(LED2);
BSP_LED_Init(LED3);
BSP_PB_Init(BUTTON_USER,BUTTON_MODE_GPIO);
/* Add your application code here
*/
SPI_handler.Instance = SPI1;
SPI_handler.Init.BaudRatePrescaler=SPI_BAUDRATEPRESCALER_128;
SPI_handler.Init.CLKPhase= SPI_PHASE_2EDGE;
SPI_handler.Init.CLKPolarity= SPI_POLARITY_HIGH;
SPI_handler.Init.DataSize= SPI_DATASIZE_8BIT;
SPI_handler.Init.Direction= SPI_DIRECTION_2LINES;
SPI_handler.Init.FirstBit= SPI_FIRSTBIT_MSB;
SPI_handler.Init.Mode= SPI_MODE_MASTER;
//SPI_handler.Init.NSS=
SPI_handler.Init.CRCCalculation= SPI_CRCCALCULATION_DISABLE;
SPI_handler.Init.NSSPMode=SPI_NSS_PULSE_DISABLE;
SPI_handler.Init.TIMode= SPI_TIMODE_DISABLE;
if(HAL_SPI_Init(&SPI_handler)!=HAL_OK)
{
Error_Handler();
}
/*Initialize GPIOs for CS of different ADEs*/
/*for CSB_A of ADE7913A*/
__HAL_RCC_GPIOF_CLK_ENABLE();
GPIO_handler.Pin= GPIO_PIN_3;
GPIO_handler.Mode= GPIO_MODE_OUTPUT_PP;
GPIO_handler.Pull= GPIO_NOPULL;
GPIO_handler.Speed= GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOF,&GPIO_handler);
HAL_GPIO_WritePin(GPIOF,GPIO_PIN_3,GPIO_PIN_SET);
/*for CSB_B of ADE7913B*/
__HAL_RCC_GPIOF_CLK_ENABLE();
GPIO_handler.Pin= GPIO_PIN_5;
GPIO_handler.Mode= GPIO_MODE_OUTPUT_PP;
GPIO_handler.Pull= GPIO_NOPULL;
GPIO_handler.Speed= GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOF,&GPIO_handler);
HAL_GPIO_WritePin(GPIOF,GPIO_PIN_5,GPIO_PIN_SET);
/*for CSB_C of ADE7913C*/
__HAL_RCC_GPIOG_CLK_ENABLE();
GPIO_handler.Pin= GPIO_PIN_1;
GPIO_handler.Mode= GPIO_MODE_OUTPUT_PP;
GPIO_handler.Pull= GPIO_NOPULL;
GPIO_handler.Speed= GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOG,&GPIO_handler);
HAL_GPIO_WritePin(GPIOG,GPIO_PIN_1,GPIO_PIN_SET);
/* Infinite loop */
while (1)
{
while(BSP_PB_GetState(BUTTON_USER)!=GPIO_PIN_SET)
{
BSP_LED_Toggle(LED2);
HAL_Delay(100);
}
BSP_LED_Off(LED2);
HAL_GPIO_WritePin(GPIOF,GPIO_PIN_5,GPIO_PIN_RESET); //Pull the CSb_B of ADE7913B low to start communication
/*
if(HAL_SPI_TransmitReceive(&SPI_handler,(uint8_t*)write_instruction,(uint8_t*)data,sizeof(write_instruction),1000)!=HAL_OK)
{
Error_Handler();
}
*/
if(HAL_SPI_Transmit(&SPI_handler,(uint8_t*)write_instruction,sizeof(write_instruction),10)!=HAL_OK)//Sends the 8 bit command byte to read the register at address 0x00
{
Error_Handler();
}
while(HAL_SPI_GetState(&SPI_handler)!=HAL_SPI_STATE_READY)
{
}
if(HAL_SPI_Receive(&SPI_handler,(uint8_t*)data,sizeof(write_instruction),10)!=HAL_OK) //Receive the data on the MISO line from the selected ADE7913
{
Error_Handler();
}
HAL_GPIO_WritePin(GPIOF,GPIO_PIN_5,GPIO_PIN_SET); //Pull the CSb_B of ADE7913B high to end communication
}
}
SPI的硬件初始化
/* Includes ------------------------------------------------------------------*/
#include "stm32l4xx_hal.h"
/** @addtogroup STM32L4xx_HAL_Driver
* @{
*/
/** @defgroup HAL_MSP
* @brief HAL MSP module.
* @{
*/
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/** @defgroup HAL_MSP_Private_Functions
* @{
*/
/**
* @brief Initializes the SPI MSP.
* @param None
* @retval None
*/
void HAL_SPI_MspInit(SPI_HandleTypeDef *hspi)
{
GPIO_InitTypeDef GPIO_Handler;
__HAL_RCC_SPI1_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
//for SPI_SCK
GPIO_Handler.Pin= GPIO_PIN_5;
GPIO_Handler.Pull= GPIO_PULLDOWN;
GPIO_Handler.Mode= GPIO_MODE_AF_PP;
GPIO_Handler.Alternate= GPIO_AF5_SPI1;
GPIO_Handler.Speed= GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOA,&GPIO_Handler);
//for SPI_MISO
GPIO_Handler.Pin= GPIO_PIN_6;
GPIO_Handler.Alternate= GPIO_AF5_SPI1;
HAL_GPIO_Init(GPIOA,&GPIO_Handler);
//for SPI_MOSI
GPIO_Handler.Pin= GPIO_PIN_7;
GPIO_Handler.Alternate= GPIO_AF5_SPI1;
HAL_GPIO_Init(GPIOA,&GPIO_Handler);
HAL_NVIC_SetPriority(SPI1_IRQn,1,0);
HAL_NVIC_EnableIRQ(SPI1_IRQn);
}
/**
* @brief DeInitializes the Global MSP.
* @param None
* @retval None
*/
void HAL_SPI_MspDeInit(SPI_HandleTypeDef *hspi)
{
__HAL_RCC_SPI1_FORCE_RESET();
__HAL_RCC_SPI1_RELEASE_RESET();
HAL_GPIO_DeInit(GPIOA,GPIO_PIN_5);
HAL_GPIO_DeInit(GPIOA,GPIO_PIN_6);
HAL_GPIO_DeInit(GPIOA,GPIO_PIN_7);
}