[活动专区] 【AT-START-F425测评】+ SPI接口Flash读写测试

[复制链接]
1312|0
 楼主| WoodData 发表于 2022-3-14 16:04 | 显示全部楼层 |阅读模式
ar, pi, AD, ADXL, CD, RS
    找到以前做的一块扩展外设功能板,利用AT32F425先测试一下SPI接口的FLASH。紫色那个是自己以前做的扩展板。
板上带有SPI FLASH,TF卡座;
SPI接口的2.4寸tftlcd,带电阻触摸XT2046;
I2C接口的加速度传感器ADXL345.光照传感器rpr-0521rs。
外加几个按键和轴了。

2.png
3.png
下面先测试下FLASH,型号是W25Q128。
扩展板SPI flash接口如下,
4.png
下面找到AT-START-F425开发板对应的引脚如下。
5.png
对应:

FLASH CS = PB3
FLASH SCK = PA5
FLASH MISO = PA6
FLASH MOSI = PA7

在AT-START-F425开发板的资料例程找到了一个flash例程。修改了一下cs的管教。基本就可以用的。
主要代码:
  1. #include "at32f425_board.h"
  2. #include "at32f425_clock.h"
  3. #include "at32f425_int.h"
  4. #include "spi_flash.h"

  5. /** @addtogroup AT32F425_periph_examples
  6.   * @{
  7.   */
  8. void _delay_ms(uint32_t ms)
  9. {
  10.     uint32_t g_ms_tick;
  11.     g_ms_tick = hal_get_tick();
  12.     while(hal_get_tick() - g_ms_tick < ms);
  13. }

  14. /**
  15.   * [url=home.php?mod=space&uid=247401]@brief[/url]  systick interrupt handler.
  16.   * @param  none
  17.   * @retval none
  18.   */
  19. void systick_handler(void)
  20. {

  21. }

  22. #define FLASH_TEST_ADDR                  0x1000
  23. #define BUF_SIZE                         0x500

  24. uint8_t tx_buffer[BUF_SIZE];
  25. uint8_t rx_buffer[BUF_SIZE];
  26. volatile error_status transfer_status = ERROR;

  27. void tx_data_fill(void);
  28. error_status buffer_compare(uint8_t* pbuffer1, uint8_t* pbuffer2, uint16_t buffer_length);

  29. /**
  30.   * @brief  transfer data fill.
  31.   * @param  none
  32.   * @retval none
  33.   */
  34. void tx_data_fill(void)
  35. {
  36.     uint32_t data_index = 0;
  37.     for(data_index = 0; data_index < BUF_SIZE; data_index++)
  38.     {
  39.         tx_buffer[data_index] = data_index+0X55;
  40.     }
  41. }

  42. /**
  43.   * @brief  buffer compare function.
  44.   * @param  pbuffer1, pbuffer2: buffers to be compared.
  45.   * @param  buffer_length: buffer's length
  46.   * @retval the result of compare
  47.   */
  48. error_status buffer_compare(uint8_t* pbuffer1, uint8_t* pbuffer2, uint16_t buffer_length)
  49. {
  50.     while(buffer_length--)
  51.     {
  52.         if(*pbuffer1 != *pbuffer2)
  53.         {
  54.             return ERROR;
  55.         }

  56.         pbuffer1++;
  57.         pbuffer2++;
  58.     }
  59.     return SUCCESS;
  60. }

  61. /**
  62.   * @brief  main function.
  63.   * @param  none
  64.   * @retval none
  65.   */
  66. int main(void)
  67. {
  68.     __IO uint32_t index = 0;
  69.     __IO uint32_t flash_id_index = 0;

  70.     system_clock_config();
  71.     /* config systick clock source */
  72.     systick_clock_source_config(SYSTICK_CLOCK_SOURCE_AHBCLK_NODIV);
  73.     /* config systick reload value and enable interrupt */
  74.     SysTick_Config(system_core_clock / 1000);
  75.     /* configure led */
  76.     at32_led_init(LED2);
  77.     at32_led_init(LED3);
  78.     uart_print_init(115200);
  79.     printf("AT32F425 Test\r\n");
  80.    

  81.     tx_data_fill();
  82.     spiflash_init();
  83.     flash_id_index = spiflash_read_id();
  84.     if(flash_id_index != W25Q128)
  85.     {
  86.         printf("flash id check error!\r\n");
  87.         for(index = 0; index < 50; index++)
  88.         {
  89.             at32_led_toggle(LED2);
  90.             at32_led_toggle(LED3);
  91.             _delay_ms(200);
  92.         }
  93.         return 1;
  94.     }
  95.     else
  96.     {
  97.         printf("flash id check success! id: %X\r\n", flash_id_index);
  98.     }

  99.     /* erase sector */
  100.     spiflash_sector_erase(FLASH_TEST_ADDR / SPIF_SECTOR_SIZE);

  101.     /* write data */
  102.     spiflash_write(tx_buffer, FLASH_TEST_ADDR, BUF_SIZE);

  103.     /* read data */
  104.     spiflash_read(rx_buffer, FLASH_TEST_ADDR, BUF_SIZE);

  105.     /* printf read data */
  106.     printf("Read Data: ");
  107.     for(index = 0; index < BUF_SIZE; index++)
  108.     {
  109.         printf("%02X ", rx_buffer[index]);
  110.     }

  111.     /* test result:the data check */
  112.     transfer_status = buffer_compare(rx_buffer, tx_buffer, BUF_SIZE);

  113.     /* test result indicate:if SUCCESS ,led2 lights */
  114.     if(transfer_status == SUCCESS)
  115.     {
  116.         printf("\r\nflash data read write success!\r\n");
  117.         at32_led_on(LED2);
  118.     }
  119.     else
  120.     {
  121.         printf("\r\nflash data read write ERROR!\r\n");
  122.         at32_led_off(LED2);
  123.     }
  124.    
  125.     while(1)
  126.     {
  127.     }
  128. }
通过串口输出信息:
1.png
测试读写成功,型号标志EF17.对应W25Q128.



您需要登录后才可以回帖 登录 | 注册

本版积分规则

127

主题

4784

帖子

28

粉丝
快速回复 在线客服 返回列表 返回顶部