
1.概述
本篇文章主要介紹如何使用e2studio對瑞薩進行spi配置,同時移植stm32上的W25Q128到瑞薩上,同時通過對該FLASH進行讀寫操作,驗證是否正確。
2.硬件準備
首先需要準備一個開發板,這里我準備的是芯片型號 R7FA2L1AB2DFL 的開發板。

3.新建工程

4.工程模板

5.保存工程路徑

6.芯片配置
本文中使用R7FA2L1AB2DFL來進行演示。

7
7.工程模板選擇

8.SPI配置
點擊Stacks->New Stack->Driver->Connectivity->SPI Driver on r_spi。

9.SPI屬性配置

10.片選CS管腳設置
設置P103管腳為輸出管腳,作為CS片選。

11.設置E2STUDIO堆棧

12.e2studio的重定向printf設置

C++ 構建->設置->GNU ARM Cross C Linker->Miscellaneous去掉Other linker flags中的 “--specs=rdimon.specs”

13.printf輸出重定向到串口
打印最常用的方法是printf,所以要解決的問題是將printf的輸出重定向到串口,然后通過串口將數據發送出去。
注意一定要加上頭文件#include
#ifdef __GNUC__ //串口重定向
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif
PUTCHAR_PROTOTYPE
{
err = R_SCI_UART_Write(&g_uart0_ctrl, (uint8_t *)&ch, 1);
if(FSP_SUCCESS != err) __BKPT();
while(uart_send_complete_flag == false){}
uart_send_complete_flag = false;
return ch;
}
int _write(int fd,char *pBuffer,int size)
{
for(int i=0;i;i++)>
14.stm32移植瑞薩說明
在STM32的W25Qx.h中,有個片選定義,代碼如下。
#define W25Qx_Enable() HAL_GPIO_WritePin(CS_GPIO_Port, CS_Pin, GPIO_PIN_RESET)
#define W25Qx_Disable() HAL_GPIO_WritePin(CS_GPIO_Port, CS_Pin, GPIO_PIN_SET)
修改后如下所示。
#define W25Qx_Enable() R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_01_PIN_03, BSP_IO_LEVEL_LOW);
#define W25Qx_Disable() R_IOPORT_PinWrite(&g_ioport_ctrl, BSP_IO_PORT_01_PIN_03, BSP_IO_LEVEL_HIGH);
在STM32的W25Qx.c中,有對數據進行發送和接受,代碼如下。
/* Send the read status command */
HAL_SPI_Transmit(&hspi1, cmd, 1, W25Qx_TIMEOUT_VALUE);
/* Reception of the data */
HAL_SPI_Receive(&hspi1,&status, 1, W25Qx_TIMEOUT_VALUE);
修改后如下所示。
/* Send the read status command */
g_transfer_complete = false;
err = R_SPI_Write(&g_spi0_ctrl, cmd, 1, SPI_BIT_WIDTH_8_BITS);
assert(FSP_SUCCESS == err);
/* Wait for SPI_EVENT_TRANSFER_COMPLETE callback event. */
while ( g_transfer_complete==false)
{
;
}
/* Reception of the data */
g_transfer_complete = false;
err = R_SPI_Read(&g_spi0_ctrl, &status, 1, SPI_BIT_WIDTH_8_BITS);
assert(FSP_SUCCESS == err);
/* Wait for SPI_EVENT_TRANSFER_COMPLETE callback event. */
while ( g_transfer_complete==false)
{
;
}
15.W25Q128說明
W25Q128將16M的容量分為256個塊(Block),每個塊大小為64K字節,每個塊又分為16個扇區(Sector),每個扇區4K個字節。W25Q128的最小擦除單位為一個扇區,也就是每次必須擦除4K個字節。芯片ID如下所示。
0XEF13,表示芯片型號為W25Q80
0XEF14,表示芯片型號為W25Q16
0XEF15,表示芯片型號為W25Q32
0XEF16,表示芯片型號為W25Q64
0XEF17,表示芯片型號為W25Q128
16.演示效果
開機會打印W25Q128的ID,ID為0XEF17,實際如下所示。
并且之前保存的數據也正確讀取出來了。

定義數組DataBuff,其中DataBuff[0]表示寫入扇區, DataBuff[1]表示寫入位置,剩下的為寫入數據,同時以0xff結尾。
分別輸入數據 01 02 01 02 03 04 ff與02 20 aa bb cc dd ff


17.主程序代碼
#include "hal_data.h"
#include
#include "W25Qx.h"
FSP_CPP_HEADER
void R_BSP_WarmStart(bsp_warm_start_event_t event);
FSP_CPP_FOOTER
void uart1_data(void);
#define BUFFERSIZE 255 //可以接收的最大字符個數
uint8_t ReceiveBuff[BUFFERSIZE]; //接收緩沖區
uint8_t recv_end_flag = 0,Rx_len=0;//接收完成中斷標志,接收到字符長度
uint8_t wData1[0x200];
uint8_t wData2[0x200];
uint8_t wData3[0x200];
uint8_t rData1[0x200];
uint8_t rData2[0x200];
uint8_t rData3[0x200];
uint8_t ID[4];
uint32_t i;
uint8_t flag[1] ;
int i_flag = 0;
fsp_err_t err = FSP_SUCCESS;
volatile bool uart_send_complete_flag = false;
uint8_t RxBuff[1]; //進入中斷接收數據的數組
uint8_t DataBuff[5000]; //保存接收到的數據的數組
int RxLine=0; //接收到的數據長度
int Rx_flag=0; //接受到數據標志
int Rx_flag_finish=0; //接受完成或者時間溢出
void user_uart_callback (uart_callback_args_t * p_args)
{
if(p_args->event == UART_EVENT_TX_COMPLETE)
{
uart_send_complete_flag = true;
}
if(p_args->event == UART_EVENT_RX_CHAR)
{
RxBuff[0] = p_args->data;
RxLine++; //每接收到一個數據,進入回調數據長度加1
DataBuff[RxLine-1]=RxBuff[0]; //把每次接收到的數據保存到緩存數組
Rx_flag=1;
Rx_len++;
if(RxBuff[0]==0xff) //接收結束標志位,這個數據可以自定義,根據實際需求,這里只做示例使用,不一定是0xff
{
Rx_flag_finish=1;
Rx_len--;
}
RxBuff[0]=0;
}
}
#ifdef __GNUC__ //串口重定向
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif
PUTCHAR_PROTOTYPE
{
err = R_SCI_UART_Write(&g_uart1_ctrl, (uint8_t *)&ch, 1);
if(FSP_SUCCESS != err) __BKPT();
while(uart_send_complete_flag == false){}
uart_send_complete_flag = false;
return ch;
}
int _write(int fd,char *pBuffer,int size)
{
for(int i=0;ievent)
{
g_transfer_complete = true;
}
}
/*******************************************************************************************************************//**
* main() is generated by the RA Configuration editor and is used to generate threads if an RTOS is used. This function
* is called by main() when no RTOS is used.
**********************************************************************************************************************/
void hal_entry(void)
{
/* TODO: add your own code here */
err = R_SCI_UART_Open(&g_uart1_ctrl, &g_uart1_cfg);
assert(FSP_SUCCESS == err);
err = R_SPI_Open(&g_spi0_ctrl, &g_spi0_cfg);
assert(FSP_SUCCESS == err);
printf("\r\n SPI-W25Q128 open\n");
/*##-1- Read the device ID ########################*/
BSP_W25Qx_Init();//初始化W25Q128
BSP_W25Qx_Read_ID(ID);//讀取ID
if((ID[0] != 0xEF) | (ID[1] != 0x17))
{
printf("SPI-W25Q128 error");
}
else//ID正確,打印ID
{
printf("W25Q128 ID : ");
for(i=0;i<2;i++)
{
printf("0x%02X ",ID[i]);
}
printf("\r\n\r\n");
}
/**************************讀取第1扇區數據**************************************************************/
/*##-3- Read the flash ########################*/
/*讀取數據,rData讀取數據的指針,起始地址0x00,讀取數據長度0x200*/
if(BSP_W25Qx_Read(rData1,0x0,0x200)== W25Qx_OK)
printf("The first sector success\n");
else
printf("The first sector error\n");
/*打印數據*/
printf("The first sector data: \r\n");
for(i =0;i<0x200;i++)
{
if(i%20==0)
printf("\nThe first sector data[%d]--data[%d]: \r\n",i,i+19);
printf("0x%02X ",rData1[i]);
}
printf("\n");
/**************************讀取第2扇區數據**************************************************************/
/*##-3- Read the flash ########################*/
/*讀取數據,rData讀取數據的指針,起始地址0x1000,讀取數據長度0x200*/
if(BSP_W25Qx_Read(rData2,0x1000,0x200)== W25Qx_OK)
printf("The second sector success\n");
else
printf("The second sector error\n");
/*打印數據*/
printf("The second sector data: \r\n");
for(i =0;i<0x200;i++)
{
if(i%20==0)
printf("\nThe second sector data[%d]--data[%d]: \r\n",i,i+19);
printf("0x%02X ",rData2[i]);
}
printf("\n");
/**************************讀取第3扇區數據**************************************************************/
/*##-3- Read the flash ########################*/
/*讀取數據,rData讀取數據的指針,起始地址0x2000,讀取數據長度0x200*/
if(BSP_W25Qx_Read(rData3,0x2000,0x200)== W25Qx_OK)
printf("The third sector success\n");
else
printf("The third sector error\n");
/*打印數據*/
printf("The third sector data: \r\n");
for(i =0;i<0x200;i++)
{
if(i%20==0)
printf("\nThe third sector data[%d]--data[%d]: \r\n",i,i+19);
printf("0x%02X ",rData3[i]);
}
printf("\n");
/**************************清除第1扇區數據為0**************************************************************/
/*##-1- Erase Block ##################################*/
if(BSP_W25Qx_Erase_Block(0) == W25Qx_OK)
printf(" QSPI Erase Block ok\r\n");
else
printf("error\r\n");
/*##-1- Written to the flash ########################*/
/* fill buffer */
printf(" Clear the first sector data[0]--data[0x200]\r\n");
for(i =0;i<0x200;i ++)
{
wData1[i] = 0;
rData1[i] = 0;
}
/*寫入數據,wData寫入數據的指針,起始地址0x00,寫入數據長度0x200*/
if(BSP_W25Qx_Write(wData1,0x00,0x200)== W25Qx_OK)
printf("Clear success\r\n");
else
printf("Clear error\r\n");
/*##-1- Read the flash ########################*/
/*讀取數據,rData讀取數據的指針,起始地址0x00,讀取數據長度0x200*/
if(BSP_W25Qx_Read(rData1,0x00,0x200)== W25Qx_OK)
printf("Read the first sector data[0]--data[0x200]\r\n\r\n");
else
printf("Read error\r\n\r\n");
/*打印數據*/
printf("the first sector data[0]--data[0x200]: \r\n");
for(i =0;i<0x200;i++)
{
if(i%20==0)
printf("\ndata[%d]--data[%d]:\r\n",i,i+19);
printf("0x%02X ",rData1[i]);
}
printf("\n");
/**************************清除第2扇區數據為0**************************************************************/
/*##-2- Erase Block ##################################*/
if(BSP_W25Qx_Erase_Block(0x1000) == W25Qx_OK)
printf(" QSPI Erase Block ok\r\n");
else
printf("error\r\n");
/*##-2- Written to the flash ########################*/
/* fill buffer */
printf(" Clear the second sector data[0]--data[0x200]\r\n");
for(i =0;i<0x200;i ++)
{
wData2[i] = 0;
rData2[i] = 0;
}
/*寫入數據,wData寫入數據的指針,起始地址0x1000,寫入數據長度0x200*/
if(BSP_W25Qx_Write(wData2,0x1000,0x200)== W25Qx_OK)
printf("Clear success\r\n");
else
printf("Clear error\r\n");
/*##-2- Read the flash ########################*/
/*讀取數據,rData讀取數據的指針,起始地址0x00,讀取數據長度0x200*/
if(BSP_W25Qx_Read(rData2,0x1000,0x200)== W25Qx_OK)
printf("Read the second sector data[0]--data[0x200]\r\n\r\n");
else
printf("Read error\r\n\r\n");
/*打印數據*/
printf("the first sector data[0]--data[0x200]: \r\n");
for(i =0;i<0x200;i++)
{
if(i%20==0)
printf("\ndata[%d]--data[%d]:\r\n",i,i+19);
printf("0x%02X ",rData2[i]);
}
printf("\n");
/**************************清除第3扇區數據為0**************************************************************/
/*##-3- Erase Block ##################################*/
if(BSP_W25Qx_Erase_Block(0x2000) == W25Qx_OK)
printf(" QSPI Erase Block ok\r\n");
else
printf("error\r\n");
/*##-3- Written to the flash ########################*/
/* fill buffer */
printf(" Clear the third sector data[0]--data[0x200]\r\n");
for(i =0;i<0x200;i ++)
{
wData3[i] = 0;
rData3[i] = 0;
}
/*寫入數據,wData寫入數據的指針,起始地址0x2000,寫入數據長度0x200*/
if(BSP_W25Qx_Write(wData3,0x2000,0x200)== W25Qx_OK)
printf("Clear success\r\n");
else
printf("Clear error\r\n");
/*##-3- Read the flash ########################*/
/*讀取數據,rData讀取數據的指針,起始地址0x00,讀取數據長度0x200*/
if(BSP_W25Qx_Read(rData3,0x2000,0x200)== W25Qx_OK)
printf("Read the third sector data[0]--data[0x200]\r\n\r\n");
else
printf("Read error\r\n\r\n");
/*打印數據*/
printf("the first third data[0]--data[0x200]: \r\n");
for(i =0;i<0x200;i++)
{
if(i%20==0)
printf("\ndata[%d]--data[%d]:\r\n",i,i+19);
printf("0x%02X ",rData3[i]);
}
printf("\n");
while(1)
{
uart1_data();
R_BSP_SoftwareDelay(100, BSP_DELAY_UNITS_MILLISECONDS); // NOLINT100->160
}
#if BSP_TZ_SECURE_BUILD
/* Enter non-secure code */
R_BSP_NonSecureEnter();
#endif
}
void uart1_data(void)
{
if(Rx_flag_finish ==1)//接收完成標志
{
if(DataBuff[0]==0x01)
{
printf("LENGTH:%d\n",Rx_len-2);
for(int i =0;i;i++)>;i++)>
聲明:本文內容及配圖由入駐作者撰寫或者入駐合作網站授權轉載。文章觀點僅代表作者本人,不代表電子發燒友網立場。文章及其配圖僅供工程師學習之用,如有內容侵權或者其他違規問題,請聯系本站處理。
舉報投訴
-
mcu
+關注
關注
146文章
17831瀏覽量
360307 -
ARM
+關注
關注
134文章
9305瀏覽量
374964 -
嵌入式
+關注
關注
5141文章
19526瀏覽量
314861 -
開發板
+關注
關注
25文章
5503瀏覽量
102202
發布評論請先 登錄
相關推薦
熱點推薦
【RA-Eco-RA4M2開發板評測】初學瑞薩-使用瑞薩flash programmer燒錄程序
本人剛入坑不久,對單片機的熱情很高,于是也加入了瑞薩的板子申請隊伍,很榮幸也成為了試用者之一,此前對于瑞薩的板子從未接觸過,包括對于使用e2stud
發表于 04-29 17:28
【RA-Eco-RA4M2開發板評測】試用瑞薩flash programmer燒錄
本人剛入坑不久,對單片機的熱情很高,于是也加入了瑞薩的板子申請隊伍,很榮幸也成為了試用者之一,此前對于瑞薩的板子從未接觸過,包括對于使用e2stud
發表于 04-28 19:12
【瑞薩RA2L1入門學習】+e2_studio軟件安裝及使用
一、e2_studio軟件安裝及使用
注冊e2 studio | Renesas 瑞薩電子
下載軟件
安裝
4.創建工程
更改工程位置
新
發表于 03-27 13:25
瑞薩e2studio(1)----瑞薩芯片之搭建FSP環境
視頻教學
樣品申請
請勿添加外鏈
e2studio軟件
e2studio是瑞薩的集成開發環境,FSP 提供了眾多可提高效率的工具,用于開發針對瑞
發表于 09-30 15:28
STM32CUBEMX(13)--SPI,W25Q128外部Flash移植
上節省空間,提供方便,正是出于這種簡單易用的特性,越來越多的芯片集成了這種通信協議,比如 EEPROM,FLASH,實時時鐘,AD轉換器。 W25Q128 是一款SPI接口的Flash
發表于 09-30 14:41
物聯網行業中Nor Flash的軟件設計分享_W25Q128的軟件設計方案
一 概述 W25Q128是一種NOR Flash芯片,掉電后數據不丟失的特點。 W25Q128FV陣列被組織成65,536個可編程頁面,每個頁面256字節。每次最多可編程256字節。可

瑞薩e2studio----SPI速率解析
在嵌入式系統的設計中,串行外設接口(SPI)的通信速率是一個關鍵參數,它直接影響到系統的性能和穩定性。瑞薩電子的RA4M2微控制器為開發者提供了靈活而強大的

ESP32外部flash與spi外設沖突怎么解決?
硬件: ESP32 ,W25Q128 SPI顯示器
庫:IDF4.0.1
使用hspi掛載了外部16MB的W25Q128,并同時掛載了SPI
發表于 06-25 06:19
【GD32H757Z海棠派開發板使用手冊】第十一講 SPI-SPI NOR FLASH讀寫實驗
通過本實驗主要學習以下內容:
?SPI簡介
?GD32H7 SPI簡介
?SPI NOR FLASH——GD

評論