xx_jj_ww 发表于 2010-10-29 16:03:24

C++ 调用JlinkARM.dll编程读写STM32F103芯片,能读但不能写,跪求原因

想写个软件用JlinkARM的动态库直接刷写flash程序,但出现了以下问题:
使用JLINKARM_WriteMem和JLINKARM_WriteU32或者JLINKARM_WriteU16 或者JLINKARM_WriteU8这几个函数都不能往flash(芯片内自身flash)中写数据。
但是使用JLINKARM_ReadMemU8或者JLINKARM_ReadMemU32又可以读取flash中的内容。
觉得非常怪异!

         CString        m_sFileName;
        FILE *fbin;
        U8 data;
        U32 readdata;
        U8readbyte;
        U8 * pdata;
        U32 dataLenth;
        U32 flashAddress;
        U8 pStatus;
        //JLINKARM_Reset();
        //JLINKARM_Halt();
        JLINKARM_Open();
        //flashAddress = JLINKARM_GetId();
        //JLINKARM_Go();
        JLINKARM_Halt();


        CString sFilter="HEX文件(*.bin)|*.bin||";
        CFileDialog dlgFileLoad(TRUE,"bin","Project",OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,sFilter,NULL);
        if(dlgFileLoad.DoModal()==IDOK)                //读取文件
        {
                m_sFileName=dlgFileLoad.GetPathName();
                UpdateData(FALSE);
        }
        UpdateData(FALSE);

        fbin = fopen(m_sFileName, "rb");
        if (0==fbin)
        {
                MessageBox("file open error!" , NULL , MB_OK);
                return ;
        }
        fseek(fbin,0,SEEK_END);                //文件指针到文件尾
    dataLenth = ftell(fbin);               //读取文件长度

    pdata = data;      
    //读取文件到分配的内存
    fseek(fbin,0,SEEK_SET);            //文件指针到文件头
    fread(pdata,1,dataLenth,fbin);      //读取文件到申请的内存中
        fclose(fbin);
        pdata = data;
       

        flashAddress = 0x08000000;
        U32 temp;
        U32 tempData;
       
        for(int i= 0 ;i< 8; i++)
        {
                JLINKARM_WriteMem(flashAddress,1,&data);
                JLINKARM_ReadMemU8(flashAddress,1,&readbyte,&pStatus);
                fbin ++;
                flashAddress ++;
        }

shangdawei 发表于 2011-7-5 10:33:32

flash 写入是 Flash program and erase controller (FPEC) 控制的.
不能直接通过 JLINKARM_WriteMem(flashAddress,1,&data); 写入

1. Unlocking the Flash memory

2. The main Flash memory programming sequence in standard mode is as follows:
Check that no main Flash memory operation is ongoing by checking the BSY bit in the FLASH_SR register.
Set the PG bit in the FLASH_CR register.
Perform the data write (half-word) at the desired address.
Wait for the BSY bit to be reset.
Read the programmed value and verify.

写入之前可能需要先擦除

The Flash memory can be erased page by page or completely (Mass Erase).

Page Erase
A page of the Flash memory can be erased using the Page Erase feature of the FPEC. To
erase a page, the procedure below should be followed:
Check that no Flash memory operation is ongoing by checking the BSY bit in the FLASH_CR register
Set the PER bit in the FLASH_CR register
Program the FLASH_AR register to select a page to erase
Set the STRT bit in the FLASH_CR register
Wait for the BSY bit to be reset
Read the erased page and verify

Mass Erase
The Mass Erase command can be used to completely erase the user pages of the Flash
memory. The information block is unaffected by this procedure. The following sequence is
recommended:
Check that no Flash memory operation is ongoing by checking the BSY bit in the FLASH_SR register
Set the MER bit in the FLASH_CR register
Set the STRT bit in the FLASH_CR register
Wait for the BSY bit to be reset
Read all the pages and verify

详情参考

PM0042
Programming manual
STM32F10xxx Flash programming
页: [1]
查看完整版本: C++ 调用JlinkARM.dll编程读写STM32F103芯片,能读但不能写,跪求原因