u8 rdata[1];
u8 rec;
void testWrite(u8 * filepath,u8 * datas,u32 length);
void testSeek(u8 * filepath,u32 index,u8 *datas,u32 length);
UART_HandleTypeDef usart1_handler; //UART句柄
void uart1_init()
{
usart1_handler.Instance=USART1; //USART1
usart1_handler.Init.BaudRate=115200; //波特率
usart1_handler.Init.WordLength=UART_WORDLENGTH_8B; //字长为8位数据格式
usart1_handler.Init.StopBits=UART_STOPBITS_1; //一个停止位
usart1_handler.Init.Parity=UART_PARITY_NONE; //无奇偶校验位
usart1_handler.Init.HwFlowCtl=UART_HWCONTROL_NONE; //无硬件流控
usart1_handler.Init.Mode=UART_MODE_TX_RX; //收发模式
HAL_UART_Init(&usart1_handler); //HAL_UART_Init()会使能UART1
}
void HAL_UART_MspInit(UART_HandleTypeDef *huart)
{
GPIO_InitTypeDef GPIO_Initure;
if(huart->Instance==USART1)//如果是串口1,进行串口1 MSP初始化
{
__HAL_RCC_GPIOA_CLK_ENABLE(); //使能GPIOA时钟
__HAL_RCC_USART1_CLK_ENABLE(); //使能USART1时钟
GPIO_Initure.Pin=GPIO_PIN_9; //PA9
GPIO_Initure.Mode=GPIO_MODE_AF_PP; //复用推挽输出
GPIO_Initure.Pull=GPIO_PULLUP; //上拉
GPIO_Initure.Speed=GPIO_SPEED_FAST; //高速
GPIO_Initure.Alternate=GPIO_AF7_USART1; //复用为USART1
HAL_GPIO_Init(GPIOA,&GPIO_Initure); //初始化PA9
GPIO_Initure.Pin=GPIO_PIN_10; //PA10
HAL_GPIO_Init(GPIOA,&GPIO_Initure); //初始化PA10
HAL_NVIC_EnableIRQ(USART1_IRQn); //使能USART1中断通道
HAL_NVIC_SetPriority(USART1_IRQn,3,3); //抢占优先级3,子优先级3
}
}
void USART1_IRQHandler(void)
{
HAL_UART_IRQHandler(&usart1_handler);
while (HAL_UART_GetState(&usart1_handler) != HAL_UART_STATE_READY);//等待就绪
while(HAL_UART_Receive_IT(&usart1_handler, (u8 *)rdata, 1) != HAL_OK);
}
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
u8 length=0;
u8 rec;
if(huart->Instance==USART1)//如果是串口1
{
rec=*(--(huart->pRxBuffPtr));
HAL_UART_Transmit(&usart1_handler,&rec,1,1000);
testWrite("0:/test.txt",&rec,1);
}
// HAL_UART_Receive_IT(&usart1_handler, (u8 *)rdata, 1);
// testSeek("0:/test.txt",length,&rec,1);
// length++;
}
/**********************************************/
FATFS fatsd;
FATFS fatflash;
FATFS fatnand;
FIL fileobj;
FRESULT fr;
UINT brs;
DIR dirobj;
FILINFO fileinfoobj;
void printffile(u8 * filepath)//读文件内容
{
u8 fdatas[255];
u32 size=0;
fr=f_open(&fileobj,(const TCHAR*)filepath,FA_READ|FA_WRITE);
if(fr==FR_OK)
{
size=f_size(&fileobj);
f_read(&fileobj,fdatas,size,&brs);
}
f_close(&fileobj);
printf("------------------------
");
printf("File Path : %s
",filepath);
printf("File Content : %s
",fdatas);
printf("------------------------
");
}
void testWrite(u8 * filepath,u8 * datas,u32 length)//写文件、数据、数据长度,从头开始写
{
fr=f_open(&fileobj,(const TCHAR*)filepath,FA_READ|FA_WRITE);
if(fr==FR_OK)
{
fr=f_write(&fileobj,datas,length,&brs);
printf("ok
");
}
f_close(&fileobj);
printffile(filepath);
}
void testSeek(u8 * filepath,u32 index,u8 *datas,u32 length)
{
fr=f_open(&fileobj,(const TCHAR*)filepath,FA_READ|FA_WRITE);
f_lseek(&fileobj, index);
fr=f_write(&fileobj,datas,length,&brs);
f_close(&fileobj);
printffile(filepath);
}
/***********************************************************/
int main(void)
{
u32 total,free;
u8 t=0;
u8 res=0;
u8 datas[]="1";
Cache_Enable(); //打开L1-Cache
MPU_Memory_Protection(); //保护相关存储区域
HAL_Init(); //初始化HAL库
Stm32_Clock_Init(432,25,2,9); //设置时钟,216Mhz
delay_init(216); //延时初始化
uart_init(115200); //串口初始化
uart1_init();//初始化串口参数
usmart_dev.init(108); //初始化USMART
LED_Init(); //初始化LED
KEY_Init(); //初始化按键
SDRAM_Init(); //初始化SDRAM
LCD_Init(); //初始化LCD
W25QXX_Init(); //初始化W25Q256
my_mem_init(SRAMIN); //初始化内部内存池
my_mem_init(SRAMEX); //初始化外部内存池
my_mem_init(SRAMDTCM); //初始化CCM内存池
POINT_COLOR=RED;
LCD_ShowString(30,50,200,16,16,"Apollo STM32F4/F7");
LCD_ShowString(30,70,200,16,16,"FATFS TEST");
LCD_ShowString(30,90,200,16,16,"ATOM@ALIENTEK");
LCD_ShowString(30,110,200,16,16,"2016/7/15");
LCD_ShowString(30,130,200,16,16,"Use USMART for test");
while(SD_Init())//检测不到SD卡
{
LCD_ShowString(30,150,200,16,16,"SD Card Error!");
delay_ms(500);
LCD_ShowString(30,150,200,16,16,"Please Check! ");
delay_ms(500);
LED0_Toggle;//DS0闪烁
}
FTL_Init();
exfuns_init(); //为fatfs相关变量申请内存
f_mount(&fatsd,"0:",1); //挂载SD卡
res=f_mount(&fatflash,"1:",1); //挂载FLASH.
res=f_mount(&fatnand,"2:",1); //挂载NAND FLASH.
HAL_UART_Receive_IT(&usart1_handler, (u8 *)rdata, 1);//该函数会开启接收中断:标志位UART_IT_RXNE,并且设置接收缓冲以及接收缓冲接收最大数据量
// printffile("0:/test.txt");
// testWrite("0:/test.txt",datas,1);//444456789
while(1);
}
原test.txt内容:abcdefg hijklmn
一周热门 更多>