打印
[应用相关]

STM32F103C8T6 内部 FLASH读写

[复制链接]
821|5
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
drer|  楼主 | 2021-7-5 16:29 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

u16        people_id[50] = { 1, 2, 3 };    /* 假设公司最多50人 */
u16        people_clock_t[50];             /* 每个人签到次数 */
u16        people_num = 2;                 /* 人数 */


#define DEBUG
#define FLASH_SIZE 64                   /* 所选MCU的FLASH容量大小(单位为K) */
#if FLASH_SIZE < 256
#define SECTOR_SIZE 1024                /* 字节 */
#else
#define SECTOR_SIZE 2048                /* 字节 */
#endif
FLASH_Status Debug;
/* Flash 写函数 以8 bit 方式写入 */
void FLASH_WriteData( uint32_t startAddress, uint16_t *writeData, uint16_t countToWrite )
{
        uint16_t        i;
        uint32_t        offsetAddress;  /* 偏移地址 */
        uint32_t        sectorPosition; /* 扇区位置 */
        uint32_t        sectorStartAddress;
        if ( startAddress < FLASH_BASE || ( (startAddress + countToWrite) >= (FLASH_BASE + 1024 * FLASH_SIZE) ) )
        {
                return;                 /* 非法地址 */
        }
        /* 解锁写保护 */
        FLASH_Unlock();

        /* 计算去掉0X08000000后的实际偏移地址 */
        offsetAddress = startAddress - FLASH_BASE;
        /* 计算扇区地址 */
        sectorPosition = offsetAddress / SECTOR_SIZE;
        /* 对应扇区的首地址 */
        sectorStartAddress = sectorPosition * SECTOR_SIZE + FLASH_BASE;

        FLASH_ClearFlag( FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR );

        /* 擦除这个扇区 */
        Debug = FLASH_ErasePage( sectorStartAddress );
#ifdef DEBUG
        if ( Debug != FLASH_COMPLETE )
                printf( "扇区擦除失败\n\n" );
#endif
        for ( i = 0; i < countToWrite; i++ )
        {
                Debug = FLASH_ProgramHalfWord( startAddress, writeData[i] );
#ifdef DEBUG
                if ( Debug != FLASH_COMPLETE )
                        printf( "扇区写失败\n\n" );
#endif
                startAddress = startAddress + 2;
        }
        FLASH_Lock(); /*上锁写保护 */
}


使用特权

评论回复
沙发
drer|  楼主 | 2021-7-5 16:30 | 只看该作者
u16 STMFLASH_ReadHalfWord( u32 faddr )
{
        return(*(vu16 *) faddr);
}




/* 16位数据读取 */
void FLASH_ReadMoreData( u32 ReadAddr, u16 *pBuffer, u16 NumToRead )
{
        u16 i;
        for ( i = 0; i < NumToRead; i++ )
        {
                pBuffer[i]        = STMFLASH_ReadHalfWord( ReadAddr );    /* 读取2个字节. */
                ReadAddr        += 2;                                   /* 偏移2个字节. */
        }
}


使用特权

评论回复
板凳
drer|  楼主 | 2021-7-5 16:31 | 只看该作者
void write_to_flash( void )
{
        u16        shuju[102];
        u16        c = 0;
        memset( shuju, 0, sizeof(shuju) );
        for ( c = 0; c < 50; c++ )
        {
                shuju[c] = people_id[c];                /*复制 */
        }
        for ( c = 0; c < 50; c++ )
        {
                shuju[50 + c] = people_clock_t[c];      /*复制 */
        }
        shuju[100] = people_num;


        FLASH_WriteData( 0x08004000, shuju, 101 );      /* 写入 */
}


使用特权

评论回复
地板
drer|  楼主 | 2021-7-5 16:32 | 只看该作者
void read_from_flash( void )
{
        u16        shuju[102];
        u16        c = 0;

        memset( shuju, 0, sizeof(shuju) );
        FLASH_ReadMoreData( 0x08004000, shuju, 101 );


        for ( c = 0; c < 50; c++ )
        {
                people_id[c] = shuju[c];                /* 读取 */
        }
        for ( c = 0; c < 50; c++ )
        {
                people_clock_t[c] = shuju[50 + c];      /*复制 */
        }
        people_num = shuju[100];
}


使用特权

评论回复
5
drer|  楼主 | 2021-7-5 16:32 | 只看该作者
void main(void)
{
     write_to_flash();
        read_from_flash();
    while(1)
    {

    }
}



使用特权

评论回复
6
guijial511| | 2021-7-5 21:13 | 只看该作者
搞个外部flash吧

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

60

主题

3309

帖子

4

粉丝