我可以使用一些帮助进行故障排除。我正试图在Arduino Due上使用SPI从W25Q128FV串行闪存读取制造商和设备ID。
闪存的数据表上写着:
“通过将/ CS引脚驱动为低电平并移位指令代码”90h“然后移位24位地址(A23-A0)为000000h来启动该指令。之后,Winbond的制造商ID(EFh)和器件ID在CLK的下降沿移出,最高有效位(MSB)优先,如图39所示。“
我的代码是: 我得到的响应总是255,255或0,0 主要:
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_SPI1_Init();
manualId = deviceId = 0;
read_id = 0x90;
zero1 = 0x00;
zero2 = 0x00;
zero3 = 0x00;
//Enable SPI
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_5, GPIO_PIN_RESET);
//Send code
masterToSlave = HAL_SPI_Transmit(&hspi1, &read_id, 1, 1000);
masterToSlave = HAL_SPI_Transmit(&hspi1, &zero1, 1, 1000);
masterToSlave = HAL_SPI_Transmit(&hspi1, &zero2, 1, 1000);
masterToSlave = HAL_SPI_Transmit(&hspi1, &zero3, 1, 1000);
//Recieve ID
slaveToMaster = HAL_SPI_Receive(&hspi1, &manualId, 1, 1000);
slaveToMaster = HAL_SPI_Receive(&hspi1, &deviceId, 1, 1000);
//Disable SPI
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_5, GPIO_PIN_SET);
}
完整代码:
#include "main.h"
#include "stm32f4xx_hal.h"
//FLASH CHIP
//--------------------------------
// 1-----------|------------PC 5
// 2-----------|------------PA 6
// 3-----------|------------
// 4-----------|------------GND
// 5-----------|------------PA 7
// 6-----------|------------PA 5
// 7-----------|------------
// 8-----------|------------VCC
SPI_HandleTypeDef hspi1;
void SystemClock_Config(void);
void Error_Handler(void);
static void MX_GPIO_Init(void);
static void MX_SPI1_Init(void);
#define spi_enable HAL_GPIO_WritePin(GPIOC, GPIO_PIN_5, GPIO_PIN_RESET)
#define spi_disable HAL_GPIO_WritePin(GPIOC, GPIO_PIN_5, GPIO_PIN_SET)
HAL_StatusTypeDef masterToSlave, slaveToMaster;
uint8_t receive_data=0;
uint8_t read_id, zero1, zero2, zero3;
uint8_t manualId, deviceId;
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_SPI1_Init();
manualId = deviceId = 0;
read_id = 0x90;
zero1 = 0x00;
zero2 = 0x00;
zero3 = 0x00;
//Enable SPI
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_5, GPIO_PIN_RESET);
//Send code
masterToSlave = HAL_SPI_Transmit(&hspi1, &read_id, 1, 1000);
masterToSlave = HAL_SPI_Transmit(&hspi1, &zero1, 1, 1000);
masterToSlave = HAL_SPI_Transmit(&hspi1, &zero2, 1, 1000);
masterToSlave = HAL_SPI_Transmit(&hspi1, &zero3, 1, 1000);
//Recieve ID
slaveToMaster = HAL_SPI_Receive(&hspi1, &manualId, 1, 1000);
slaveToMaster = HAL_SPI_Receive(&hspi1, &deviceId, 1, 1000);
//Disable SPI
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_5, GPIO_PIN_SET);
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_ClkInitTypeDef RCC_ClkInitStruct;
/**Configure the main internal regulator output voltage
*/
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
/**Initializes the CPU, AHB and APB busses clocks
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 8;
RCC_OscInitStruct.PLL.PLLN = 336;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 7;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/**Initializes the CPU, AHB and APB busses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV8;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV4;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
{
Error_Handler();
}
/**Configure the Systick interrupt time
*/
HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
/**Configure the Systick
*/
HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
/* SysTick_IRQn interrupt configuration */
HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
}
static void MX_SPI1_Init(void)
{
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;//SPI_POLARITY_HIGH;//
hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
hspi1.Init.NSS = SPI_NSS_SOFT;
hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;
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();
}
}
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOH_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOC_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_5, GPIO_PIN_SET);
/*Configure GPIO pin : PC5 */
GPIO_InitStruct.Pin = GPIO_PIN_5;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
}
void Error_Handler(void)
{
while(1)
{
}
}
#ifdef USE_FULL_ASSERT
void assert_failed(uint8_t* file, uint32_t line)
{
}
#endif
答案 0 :(得分:0)
这里还没有启动SPI Clock。如果没有启动SPI时钟,SPI将无法初始化,因此无法进行通信。