C functions for file operation

本文总结了标准输入输出相关的函数,包括字符级别的读写如getchar、putchar,字符串级别的读写如fgets、fputs,以及文件级别的读写如fread、fwrite等,并介绍了它们的功能区别。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

getchar

_gettchar

Read a character from standard input.

putchar

_puttchar

Write a character to stdout.

getc

_gettc

Read a character from a stream.

fgetc

_fgettc

fgetc is equivalent to getc, but is implemented only as a function, rather than as a function and a macro.

putc

_puttc

Write a character to a stream

fputc

_fputtc

Equivalent to putc, but implemented only as a function, rather than as a function and a macro.

 

 

 

gets

_getts

Get a line from the stdin stream. gets then replaces the newline character with a null character ('/0') before returning the line.

fgets

_fgetts

Get a string from a stream. the fgets function retains the newline character.

puts

_putts

Write a string to stdout. replacing the string's terminating null character ('/0') with a newline character ('/n') in the output stream.

fputs

_fputfs

Write a string to a stream.

 

 

 

fread

 

Reads data from a stream.

fwrite

 

Writes data to a stream.

 

 

 

_read

 

Reads data from a file.

_write

 

Writes data to a file.

_open

 

Opens a file.

_create

 

Creates a new file.

_close

 

Closes a file.

<think>我们正在处理STM32F4系列微控制器的Flash操作,特别是使用标准外设库(非HAL)的情况。用户询问的是Flash固件库函数原型,因此我们需要从标准外设库中提取与Flash操作相关的函数原型。根据引用[1]和[3]的内容,我们知道STM32F4的标准外设库包含在CMSIS的ST目录下,其中头文件`stm32f4xx.h`包含了外设寄存器的定义,而`stm32f4xx_flash.h`则包含了Flash操作的函数原型。因此,我们将列出`stm32f4xx_flash.h`中与Flash擦除和编程相关的主要函数原型。注意,这些函数原型通常位于标准外设库的`Libraries/STM32F4xx_StdPeriph_Driver/inc/`目录下。以下是一些关键函数的原型:###Flash操作函数原型(来自stm32f4xx_flash.h) 1. **解锁和锁定Flash**```cvoid FLASH_Unlock(void);voidFLASH_Lock(void);```2.**擦除函数**```cFLASH_StatusFLASH_EraseSector(uint32_tSector,uint8_t VoltageRange);FLASH_Status FLASH_EraseAllSectors(uint8_tVoltageRange);FLASH_StatusFLASH_EraseAllBank1Sectors(uint8_t VoltageRange);//仅适用于大容量产品``` 3. **编程函数**```cFLASH_StatusFLASH_ProgramDoubleWord(uint32_tAddress,uint64_t Data);FLASH_StatusFLASH_ProgramWord(uint32_tAddress,uint32_t Data);FLASH_StatusFLASH_ProgramHalfWord(uint32_t Address, uint16_tData);FLASH_Status FLASH_ProgramByte(uint32_t Address, uint8_tData);```4.**选项字节操作(包括写保护)**```cvoidFLASH_OB_Unlock(void);void FLASH_OB_Lock(void);FLASH_StatusFLASH_OB_Erase(void);FLASH_StatusFLASH_OB_WriteUser(uint8_tUserConfig);FLASH_StatusFLASH_OB_RDPConfig(uint8_tRDP);FLASH_Status FLASH_OB_UserConfig(uint8_t OB_IWDG,uint8_t OB_STOP, uint8_tOB_STDBY);FLASH_StatusFLASH_OB_BORConfig(uint8_tBORLevel);//更多选项字节相关函数...```5.**状态和标志管理**```cFLASH_StatusFLASH_GetStatus(void);FLASH_StatusFLASH_WaitForLastOperation(void);voidFLASH_ClearFlag(uint32_tFLASH_FLAG);FlagStatusFLASH_GetFlagStatus(uint32_t FLASH_FLAG);```6.**中断相关**```cvoidFLASH_ITConfig(uint32_tFLASH_IT, FunctionalState NewState);```###函数参数说明- `FLASH_Status`:枚举类型,表示Flash操作的状态(如`FLASH_BUSY`, `FLASH_ERROR_PROGRAM`,`FLASH_COMPLETE`等)。 -`Sector`:要擦除的扇区号,对于STM32F407VGT6,扇区范围是0到11(具体参考芯片手册)。 -`VoltageRange`:电压范围,用于确定擦除和编程的参数,取值为:```c#define FLASH_VOLTAGE_RANGE_1((uint8_t)0x00)//2.7Vto3.6V#defineFLASH_VOLTAGE_RANGE_2((uint8_t)0x01)//2.4V to2.7V#define FLASH_VOLTAGE_RANGE_3((uint8_t)0x02)//2.1Vto2.4V#defineFLASH_VOLTAGE_RANGE_4((uint8_t)0x03)//1.8V to2.1V```###使用示例以下是一个使用标准外设库进行扇区擦除的示例代码(与之前给出的类似,但更详细):```c#include"stm32f4xx_flash.h" #defineFLASH_SECTOR_TO_ERASEFLASH_Sector_5voidFlash_Erase(void){FLASH_Statusstatus =FLASH_COMPLETE;//解锁FlashFLASH_Unlock();//清除所有错误标志FLASH_ClearFlag(FLASH_FLAG_EOP |FLASH_FLAG_OPERR |FLASH_FLAG_WRPERR|FLASH_FLAG_PGAERR| FLASH_FLAG_PGPERR |FLASH_FLAG_PGSERR);//擦除扇区5(128KB)status =FLASH_EraseSector(FLASH_SECTOR_TO_ERASE, FLASH_VOLTAGE_RANGE_3);if(status!= FLASH_COMPLETE) {//处理错误while(1);}//锁定FlashFLASH_Lock();} ```###注意事项-在进行Flash操作(擦除或编程)时,必须确保代码在RAM中运行,因为Flash操作会暂停对Flash的访问。-操作前需要禁用中断,以避免在操作过程中发生中断导致错误。 -每次操作后应检查状态标志,确保操作成功。###相关引用以上函数原型和定义可以在标准外设库的`stm32f4xx_flash.h`头文件中找到[^1][^3]。 §§相关问题§§1. STM32F4标准外设库中Flash编程函数的具体用法是什么?2.如何配置选项字节(Option Bytes)来设置读保护(RDP)? 3. Flash操作过程中如何避免中断干扰? 4. STM32F4的Flash擦除和编程时间是多少?</think>### STM32F4xx Flash固件库函数原型(标准外设库) 以下是STM32F4xx标准外设库(StdPeriph Lib)中Flash操作的核心函数原型,这些函数定义在`stm32f4xx_flash.h`头文件中: #### 1. Flash控制函数 ```c /* Flash解锁/锁定 */ void FLASH_Unlock(void); void FLASH_Lock(void); /* 选项字节解锁/锁定 */ void FLASH_OB_Unlock(void); void FLASH_OB_Lock(void); ``` #### 2. 擦除操作函数 ```c /* 扇区擦除 */ FLASH_Status FLASH_EraseSector(uint32_t Sector, uint8_t VoltageRange); /* 批量擦除 */ FLASH_Status FLASH_EraseAllSectors(uint8_t VoltageRange); FLASH_Status FLASH_EraseAllBank1Sectors(uint8_t VoltageRange); ``` #### 3. 编程操作函数 ```c /* 不同数据宽度的编程 */ FLASH_Status FLASH_ProgramDoubleWord(uint32_t Address, uint64_t Data); FLASH_Status FLASH_ProgramWord(uint32_t Address, uint32_t Data); FLASH_Status FLASH_ProgramHalfWord(uint32_t Address, uint16_t Data); FLASH_Status FLASH_ProgramByte(uint32_t Address, uint8_t Data); ``` #### 4. 状态管理函数 ```c /* 获取操作状态 */ FLASH_Status FLASH_GetStatus(void); /* 标志位管理 */ void FLASH_ClearFlag(uint32_t FLASH_FLAG); FlagStatus FLASH_GetFlagStatus(uint32_t FLASH_FLAG); ``` #### 5. 选项字节配置函数 ```c /* 写保护配置 */ FLASH_Status FLASH_OB_EnableWRP(uint32_t OB_WRP); /* 读保护配置 */ FLASH_Status FLASH_OB_RDPConfig(uint8_t OB_RDP); /* 用户配置 */ FLASH_Status FLASH_OB_UserConfig(uint8_t OB_IWDG, uint8_t OB_STOP, uint8_t OB_STDBY); ``` #### 6. 类型定义 ```c /* 操作状态枚举 */ typedef enum { FLASH_BUSY = 1, FLASH_ERROR_RD, FLASH_ERROR_PGS, FLASH_ERROR_PGP, FLASH_ERROR_PGA, FLASH_ERROR_WRP, FLASH_ERROR_PROGRAM, FLASH_ERROR_OPERATION, FLASH_COMPLETE } FLASH_Status; ``` #### 关键宏定义 ```c /* 扇区选择 */ #define FLASH_Sector_0 ((uint16_t)0x0000) #define FLASH_Sector_1 ((uint16_t)0x0008) /* ... 其他扇区定义 ... */ /* 电压范围 */ #define FLASH_VOLTAGE_RANGE_1 ((uint8_t)0x00) // 2.7-3.6V #define FLASH_VOLTAGE_RANGE_2 ((uint8_t)0x01) // 2.4-2.7V #define FLASH_VOLTAGE_RANGE_3 ((uint8_t)0x02) // 2.1-2.4V #define FLASH_VOLTAGE_RANGE_4 ((uint8_t)0x03) // 1.8-2.1V ``` ### 使用示例 ```c #include "stm32f4xx_flash.h" void Flash_Example(void) { FLASH_Unlock(); // 解锁Flash // 擦除扇区5 FLASH_Status status = FLASH_EraseSector(FLASH_Sector_5, FLASH_VOLTAGE_RANGE_3); if(status == FLASH_COMPLETE) { // 编程双字数据 FLASH_ProgramDoubleWord(0x08020000, 0x0123456789ABCDEF); } FLASH_Lock(); // 锁定Flash } ``` ### 文件位置参考 这些函数原型在STM32标准外设库中的位置: ``` │ ├── Libraries │ │ ├── CMSIS │ │ │ └── Device │ │ │ └── ST │ │ │ └── STM32F4xx │ │ │ ├── Include │ │ │ │ ├── stm32f4xx.h // 主设备头文件 │ │ │ │ └── system_stm32f4xx.h │ │ │ └── Source │ │ │ └── Templates │ │ ├── STM32F4xx_StdPeriph_Driver │ │ │ ├── inc │ │ │ │ └── stm32f4xx_flash.h // Flash操作函数原型[^1][^3] │ │ │ └── src │ │ │ └── stm32f4xx_flash.c // 函数实现 ``` > **注意**:使用前需在工程中包含`stm32f4xx_flash.h`头文件,并链接`stm32f4xx_flash.c`源文件[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值