///*
// * Copyright (c) 2006-2021, RT-Thread Development Team
// *
// * SPDX-License-Identifier: Apache-2.0
// *
// * Change Logs:
// * Date           Author       Notes
// * 2024-05-10     LJ       the first version
// */
//#include <rtthread.h>
//#include <rtdevice.h>
//#include <drv_common.h>
//#include "spi_flash.h"
//#include "spi_flash_sfud.h"
//#include "board.h"
//#include "drv_spi.h"
//
//#define W25Q_SPI_DEVICE_NAME "spi30"
//struct rt_spi_device *spi_dev;
//
//
//int w25q_spi_device_init(void)
//{
//
//    struct rt_spi_configuration cfg;
//    __HAL_RCC_GPIOA_CLK_ENABLE();
//
//    rt_hw_spi_device_attach("spi3", W25Q_SPI_DEVICE_NAME, GPIOA, GPIO_PIN_15);
//    spi_dev = (struct rt_spi_device *) rt_device_find(W25Q_SPI_DEVICE_NAME);
//
//    cfg.data_width=8;
//    cfg.mode=RT_SPI_MASTER|RT_SPI_MODE_0|RT_SPI_MSB;
//    cfg.max_hz= 20 * 1000 *1000; /* 20M */
//
//    rt_spi_configure(spi_dev, &cfg);
//
//}
//
//void spi_w25q_ID(void)
//{
//    rt_uint8_t w25x_read_id = 0x9f;
//    rt_uint8_t id[5] = {0};
//
//    rt_spi_send_then_recv(spi_dev, &w25x_read_id, 1, id, 5);
//
//    rt_kprintf("use rt_spi_send_then_recv() read w25q ID is:%x%x%x%x%x\n", id[0], id[1], id[2], id[3], id[4]);
//
////            struct rt_spi_message msg1, msg2;
////
////            msg1.send_buf   = &w25x_read_id;
////            msg1.recv_buf   = RT_NULL;
////            msg1.length     = 1;
////            msg1.cs_take    = 1;
////            msg1.cs_release = 0;
////            msg1.next       = &msg2;
////
////            msg2.send_buf   = RT_NULL;
////            msg2.recv_buf   = id;
////            msg2.length     = 5;
////            msg2.cs_take    = 0;
////            msg2.cs_release = 1;
////            msg2.next       = RT_NULL;
////
////            rt_spi_transfer_message(spi_dev, &msg1);
////            rt_kprintf("use rt_spi_transfer_message() read w25q ID is:%x%x\n", id[3], id[4]);
//
//}

//#define W25Q_SPI_DEVICE_NAME     "spi30"
////extern SPI_HandleTypeDef hspi3;
//static int rt_hw_spi_flash_init(void)
//{
//    struct rt_spi_device *spi_device = RT_NULL;
//
//    spi_device = (struct rt_spi_device *)rt_malloc(sizeof(struct rt_spi_device));
//    if(RT_NULL == spi_device)
//    {
//        rt_kprintf("Failed to malloc the spi device.");
//        return -RT_ENOMEM;
//    }
//    if (RT_EOK != rt_spi_bus_attach_device_cspin(spi_device, "spi30", "spi3",GET_PIN(A, 15), RT_NULL))
//    {
//        rt_kprintf("Failed to attach the spi device.");
//        return -RT_ERROR;
//    }
////    if (RT_NULL == rt_sfud_flash_probe("W25Q64", "spi30"))
////    {
////        rt_kprintf("Failed to probe the W25Q64.");
//        return -RT_ERROR;
////    };
//
//    return RT_EOK;
//}
//
//INIT_COMPONENT_EXPORT(rt_hw_spi_flash_init);
//
//static void spi_w25q_sample(int argc, char *argv[])
//{
//    struct rt_spi_device *spi_dev_w25q;
//    char name[RT_NAME_MAX];
//    rt_uint8_t w25x_read_id = 0x9F;
//    rt_uint8_t id[5] = {0};
////
//////    if (argc == 2)
//////    {
//////        rt_strncpy(name, argv[1], RT_NAME_MAX);
//////    }
//////    else
//////    {
//////        rt_strncpy(name, W25Q_SPI_DEVICE_NAME, RT_NAME_MAX);
//////    }
////
//    spi_dev_w25q = (struct rt_spi_device *)rt_device_find("spi30");
//    if (!spi_dev_w25q)
//    {
//        rt_kprintf("spi sample run failed! can't find %s device!\n", name);
//    }
//    else
//    {
//        rt_spi_send_then_recv(spi_dev_w25q, &w25x_read_id, 1, id, 5);
//        rt_kprintf("use rt_spi_send_then_recv() read w25q ID is:%x%x\n", id[3], id[4]);
//
////        struct rt_spi_message msg1, msg2;
////
////        msg1.send_buf   = &w25x_read_id;
////        msg1.recv_buf   = RT_NULL;
////        msg1.length     = 1;
////        msg1.cs_take    = 1;
////        msg1.cs_release = 0;
////        msg1.next       = &msg2;
////
////        msg2.send_buf   = RT_NULL;
////        msg2.recv_buf   = id;
////        msg2.length     = 5;
////        msg2.cs_take    = 0;
////        msg2.cs_release = 1;
////        msg2.next       = RT_NULL;
////
////        rt_spi_transfer_message(spi_dev_w25q, &msg1);
////        rt_kprintf("use rt_spi_transfer_message() read w25q ID is:%x%x\n", id[3], id[4]);
//
//
////    rt_uint8_t cmd = 0x9F;  // 读取JEDEC ID的命令
////    rt_uint8_t id[3];       // 存储ID的数组
////    rt_uint32_t jedecID;
////    char msg[50];
////    /* USER CODE END 2 */
////    rt_pin_mode(GET_PIN(A, 15), PIN_MODE_OUTPUT);
////      // 默认为高电平
////      rt_pin_write(GET_PIN(A, 15), PIN_HIGH);
////    /* Infinite loop */
////    /* USER CODE BEGIN WHILE */
////    while (1)
////    {
////
////
////        rt_pin_write(GET_PIN(A, 15), PIN_LOW);
////          HAL_SPI_Transmit(&hspi3, &cmd, 1, HAL_MAX_DELAY);      // 发送读ID命令
////          HAL_SPI_Receive(&hspi3, id, 3, HAL_MAX_DELAY);         // 读取3字节的ID
////          rt_pin_write(GET_PIN(A, 15), PIN_HIGH);
////
////
////          jedecID = (id[0] << 16) | (id[1] << 8) | id[2];  // 组合ID
////
////
////          rt_kprintf("JEDEC ID: %lX\r\n", jedecID);
////
////          rt_thread_mdelay(3000);
//
//    }
//}
//MSH_CMD_EXPORT(spi_w25q_sample, spi w25q sample);
