188 lines
5.6 KiB
C
188 lines
5.6 KiB
C
/**
|
||
******************************************************************
|
||
* @file main.c
|
||
* @author fire
|
||
* @version V1.0
|
||
* @date 2018-xx-xx
|
||
* @brief QSPI FLASH基本读写例程
|
||
******************************************************************
|
||
* @attention
|
||
*
|
||
* 实验平台:野火 STM32H743开发板
|
||
* 论坛 :http://www.firebbs.cn
|
||
* 淘宝 :http://firestm32.taobao.com
|
||
*
|
||
******************************************************************
|
||
*/
|
||
#include "stm32h7xx.h"
|
||
#include "main.h"
|
||
#include "./led/bsp_led.h"
|
||
#include "./usart/bsp_debug_usart.h"
|
||
#include "./flash/bsp_qspi_flash.h"
|
||
#include "./delay/core_delay.h"
|
||
#include "usb_device.h"
|
||
|
||
//读取的ID存储位置
|
||
__IO uint32_t DeviceID = 0;
|
||
__IO uint32_t FlashID = 0;
|
||
|
||
// 函数原型声明
|
||
void Delay(__IO uint32_t nCount);
|
||
|
||
uint8_t state = QSPI_ERROR;
|
||
/**
|
||
* @brief 主函数
|
||
* @param 无
|
||
* @retval 无
|
||
*/
|
||
int main(void)
|
||
{
|
||
/* 使能指令缓存 */
|
||
SCB_EnableICache();
|
||
/* 使能数据缓存 */
|
||
SCB_EnableDCache();
|
||
/* 系统时钟初始化成480MHz */
|
||
SystemClock_Config();
|
||
|
||
/* 开启USB的GPIO引脚时钟 */
|
||
__HAL_RCC_GPIOH_CLK_ENABLE();
|
||
__HAL_RCC_GPIOB_CLK_ENABLE();
|
||
|
||
/* LED 端口初始化 */
|
||
LED_GPIO_Config();
|
||
LED_BLUE;
|
||
|
||
/* 配置串口1为:115200 8-N-1 */
|
||
DEBUG_USART_Config();
|
||
|
||
printf("\r\n这是一个32M串行flash(W25Q256)间接模式读写模拟U盘的实验(QSPI驱动) \r\n");
|
||
|
||
/* 32M串行flash W25Q256初始化 */
|
||
QSPI_FLASH_Init();
|
||
|
||
/* 获取 Flash Device ID */
|
||
DeviceID = QSPI_FLASH_ReadDeviceID();
|
||
|
||
Delay( 200 );
|
||
/* 获取 SPI Flash ID */
|
||
FlashID = QSPI_FLASH_ReadID();
|
||
QSPI_Set_WP_High();
|
||
// /*写状态寄存器*/
|
||
QSPI_FLASH_WriteStatusReg(1,0X00);
|
||
QSPI_FLASH_WriteStatusReg(2,0X00);
|
||
QSPI_FLASH_WriteStatusReg(3,0X61);
|
||
printf("\r\nFlashID is 0x%X, Manufacturer Device ID is 0x%X\r\n", FlashID, DeviceID);
|
||
printf("\r\nFlash Status Reg1 is 0x%02X,\r\n", QSPI_FLASH_ReadStatusReg(1));
|
||
printf("\r\nFlash Status Reg2 is 0x%02X,\r\n", QSPI_FLASH_ReadStatusReg(2));
|
||
printf("\r\nFlash Status Reg3 is 0x%02X,\r\n", QSPI_FLASH_ReadStatusReg(3));
|
||
QSPI_Set_WP_TO_QSPI_IO();
|
||
/* 检验 SPI Flash ID */
|
||
|
||
MX_USB_DEVICE_Init();
|
||
while(1);
|
||
}
|
||
|
||
void Delay(__IO uint32_t nCount)
|
||
{
|
||
for(; nCount != 0; nCount--);
|
||
}
|
||
|
||
/**
|
||
* @brief System Clock 配置
|
||
* system Clock 配置如下:
|
||
* System Clock source = PLL (HSE)
|
||
* SYSCLK(Hz) = 480000000 (CPU Clock)
|
||
* HCLK(Hz) = 240000000 (AXI and AHBs Clock)
|
||
* AHB Prescaler = 2
|
||
* D1 APB3 Prescaler = 2 (APB3 Clock 120MHz)
|
||
* D2 APB1 Prescaler = 2 (APB1 Clock 120MHz)
|
||
* D2 APB2 Prescaler = 2 (APB2 Clock 120MHz)
|
||
* D3 APB4 Prescaler = 2 (APB4 Clock 120MHz)
|
||
* HSE Frequency(Hz) = 25000000
|
||
* PLL_M = 5
|
||
* PLL_N = 192
|
||
* PLL_P = 2
|
||
* PLL_Q = 4
|
||
* PLL_R = 2
|
||
* VDD(V) = 3.3
|
||
* Flash Latency(WS) = 4
|
||
* @param None
|
||
* @retval None
|
||
*/
|
||
/**
|
||
* @brief System Clock Configuration
|
||
* @retval None
|
||
*/
|
||
void SystemClock_Config(void)
|
||
{
|
||
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
|
||
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
|
||
RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
|
||
/** 启用电源配置更新
|
||
*/
|
||
HAL_PWREx_ConfigSupply(PWR_LDO_SUPPLY);
|
||
/** 配置主内稳压器输出电压
|
||
*/
|
||
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE0);
|
||
|
||
while(!__HAL_PWR_GET_FLAG(PWR_FLAG_VOSRDY)) {}
|
||
|
||
__HAL_RCC_PLL_PLLSOURCE_CONFIG(RCC_PLLSOURCE_HSE);
|
||
/** 初始化CPU、AHB和APB总线时钟
|
||
*/
|
||
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 = 5;
|
||
RCC_OscInitStruct.PLL.PLLN = 192;
|
||
RCC_OscInitStruct.PLL.PLLP = 2;
|
||
RCC_OscInitStruct.PLL.PLLQ = 4;
|
||
RCC_OscInitStruct.PLL.PLLR = 2;
|
||
RCC_OscInitStruct.PLL.PLLRGE = RCC_PLL1VCIRANGE_2;
|
||
RCC_OscInitStruct.PLL.PLLVCOSEL = RCC_PLL1VCOWIDE;
|
||
RCC_OscInitStruct.PLL.PLLFRACN = 0;
|
||
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
|
||
{
|
||
while(1);
|
||
}
|
||
/** 初始化CPU、AHB和APB总线时钟
|
||
*/
|
||
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|
||
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2
|
||
|RCC_CLOCKTYPE_D3PCLK1|RCC_CLOCKTYPE_D1PCLK1;
|
||
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
|
||
RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1;
|
||
RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV2;
|
||
RCC_ClkInitStruct.APB3CLKDivider = RCC_APB3_DIV2;
|
||
RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV2;
|
||
RCC_ClkInitStruct.APB2CLKDivider = RCC_APB2_DIV2;
|
||
RCC_ClkInitStruct.APB4CLKDivider = RCC_APB4_DIV2;
|
||
|
||
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
|
||
{
|
||
while(1);
|
||
}
|
||
|
||
/* 初始化串口、USB外设时钟 */
|
||
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_USART1|RCC_PERIPHCLK_USB;
|
||
PeriphClkInitStruct.PLL3.PLL3M = 25;
|
||
PeriphClkInitStruct.PLL3.PLL3N = 384;
|
||
PeriphClkInitStruct.PLL3.PLL3P = 2;
|
||
PeriphClkInitStruct.PLL3.PLL3Q = 8;
|
||
PeriphClkInitStruct.PLL3.PLL3R = 2;
|
||
PeriphClkInitStruct.PLL3.PLL3RGE = RCC_PLL3VCIRANGE_0;
|
||
PeriphClkInitStruct.PLL3.PLL3VCOSEL = RCC_PLL3VCOWIDE;
|
||
PeriphClkInitStruct.PLL3.PLL3FRACN = 0;
|
||
PeriphClkInitStruct.Usart16ClockSelection = RCC_USART16CLKSOURCE_PLL3;
|
||
PeriphClkInitStruct.UsbClockSelection = RCC_USBCLKSOURCE_PLL3;
|
||
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
|
||
{
|
||
LED_RED;
|
||
}
|
||
/** Enable USB Voltage detector
|
||
*/
|
||
HAL_PWREx_EnableUSBVoltageDetector();
|
||
}
|
||
/****************************END OF FILE***************************/
|