This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

MSP430G2553从RAM里擦除FLASH是什么意思啊,怎么操作的???

今天看了一天的FLASH,觉得有点奇怪,为什么直接从Flash中擦除不用检测忙标志,而从RAM中要判断,写好像也不要,但块写要判断,这之间有什么差别呢?

  • shuai lee,

    建议参考user guide 中7.3.2 Erasing Flash Memory部分,其中对从FLASH和RAM擦除FLASH进行了详细的讲解。下列是参考x2xx user guide

    1. 从FLASH擦除不需检测BUSY位是因为:

    When a flash segment erase operation is initiated from within flash memory, all timing is controlled by the flash controller, and the CPU is held while the erase cycle completes.

    2. 从RAM要判断是因为:

    Any erase cycle may be initiated from RAM. In this case, the CPU is not held and can continue to execute
    code from RAM. The BUSY bit must be polled to determine the end of the erase cycle before the CPU
    can access any flash address again. If a flash access occurs while BUSY = 1, it is an access violation,
    ACCVIFG will be set, and the erase results will be unpredictable.

    3. 从FLASH Byte/Word写不需要判断,但是从RAM中的Byte/Word写也是需要判断的:

    When a byte/word write is executed from RAM, the CPU continues to execute code from RAM. The BUSY
    bit must be zero before the CPU accesses flash again, otherwise an access violation occurs, ACCVIFG is
    set, and the write result is unpredictable

    4. 块写只能从RAM发起,而不能从FLASH发起

    A block write cannot be initiated from within flash memory. The block write must be initiated from RAM
    only. The BUSY bit remains set throughout the duration of the block write. The WAIT bit must be checked
    between writing each byte or word in the block. When WAIT is set the next byte or word of the block can
    be written.

    希望对你有帮助!O(∩_∩)O~

  •  Lina Lian

    其实这些我都有看到过,不过英文不太好,看英文文档比较费劲,很多都没看太明白就跳过去了。谢谢你帮我的归纳

    可是我还是分不开什么时候是FLASH 写或擦除,什么时候是从RAM开始写或擦除。。看它的例程,好像从RAM和从FLASH是一样的

    下面是我 昨天写的一个例程,是参照别人的写的,工作是可以工作,不过我还是分不清细节方面的东西,可以再麻烦你给我讲下

    他们的差别吗,或者发个例程给我参考下 谢谢

    #include "flash.h"

    //=====================================================================
    //flash initialization
    //=====================================================================
    void FlashInit(void)
    {
    //set SMCLK as 1Mhz
    if(0xff == CALDCO_1MHZ || 0xff == CALBC1_1MHZ)
    {
    while(1);//if flash is repeated erased, don't run, trap CPU
    }
    DCOCTL = CALDCO_1MHZ;
    BCSCTL1 = CALBC1_1MHZ;

    FCTL2 = FWKEY + FSSEL_2 + FN1;//clock = SMCLK / 3
    }

    //=====================================================================
    //check wether flash is busy
    //=====================================================================
    uchar FlashCheckBusy()
    {
    if (FCTL3 & BUSY)
    return 1;
    else
    return 0;
    }

    //=====================================================================
    //flash segment erasement
    //=====================================================================
    void FlashErase(int SegX)
    {
    _DINT();//disable global interrupt
    while ( FlashCheckBusy() );
    FCTL3 = FWKEY;//clear LOCK bit
    FCTL1 = FWKEY + ERASE;
    *((int *)SegX) = 0x00;
    while ( FlashCheckBusy() );

    FCTL3 = FWKEY | LOCK;
    return;
    }

    //=====================================================================
    //flash write byte
    //=====================================================================
    void FlashWriteChar(uint addr, char wdata)
    {
    _DINT();//disable global interrupt
    while( FlashCheckBusy() );

    FCTL3 = FWKEY;// clear LOCK bit
    FCTL1 = FWKEY + WRT;
    *((uchar *)addr) = wdata;//put write data 2 flsh memory
    FCTL1 = FWKEY;//clear WRT bit
    FCTL3 = FWKEY + LOCK;//LOCK flash
    return;
    }

    //====================================================================
    //flash read byte
    //====================================================================
    char FlashReadChar(uint addr)
    {
    char rdata;
    rdata = *((char *)addr);//go to the refered address read
    return rdata;
    }

    //====================================================================
    //Flash write word
    //====================================================================
    void FlashWriteWord(uint addr, uint wdata)
    {
    _DINT();
    while ( FlashCheckBusy() );
    FCTL3 = FWKEY;// clear lock
    FCTL1 = FWKEY + WRT;//Enable write
    *((uint *)addr) = wdata;
    FCTL1 = FWKEY ;//Clear write
    FCTL3 = FWKEY + LOCK;//LOCK FALSH module
    return;
    }

    //====================================================================
    //Flash Read Word
    //====================================================================
    uint FlashReadWord(uint addr)
    {
    uint rdata;
    rdata = *((uint*)addr);//get value from refered address
    return rdata;
    }

    //===================================================================
    //Flash modify byte
    //===================================================================
    void FlashModifyChar(uint SegX, char AddrNum, char wdata)
    {
    char i, TempArray[SegSize];
    for(i = 0; i < SegSize; i++)
    {
    TempArray[i] = FlashReadChar(SegX + i);
    }

    TempArray[AddrNum] = wdata;

    FlashErase(SegX);

    FCTL3 = FWKEY;
    FCTL1 = FWKEY + WRT;

    for(i = 0; i < 64; i ++)
    {
    *((char *)(SegX + i)) = TempArray[i];
    }

    FCTL1 = FWKEY;// Clear WRITE bit
    FCTL3 = FWKEY + LOCK;//lock flash

    return;
    }

    //===================================================================
    //flash block write
    //===================================================================
    void FlashBlockWrite(int SegX, char *pStr)
    {
    int i;
    FlashErase(SegX);//block erasement
    FCTL3 = FWKEY;// Clear LOCK bit
    FCTL1 = FWKEY + WRT;
    for(i = 0; i < 2 * sizeof(pStr); i++)
    {
    *(uchar *)(SegX + i) = *(pStr + i);
    }
    FCTL1 = FWKEY;
    FCTL3 = FWKEY + LOCK;
    }


  • shuai lee,

    其实你就是想知道从RAM和FLASH进行FLASH操作的区别是什么,其实这个就是进行FLASH操作的这段代码是运行在哪里,如果是运行在RAM里,那就叫从RAM发起的FLASH操作,如果运行在FLASH里,那就叫从FLASH发起的FLASH操作。其实从RAM发起,就是指把程序复制到RAM区,然后对FLASH操作。

    若是从FLASH发起的,那么就不能对代码所在的FLASH区域进行操作,而若是从RAM发起的,那就可以对任何FLASH区域进行操作。即从 RAM 发起的 Flash 擦除或写操作,可以对整个 Flash 区域进行擦除或写操作,而从 Flash 区域发起的 Flash 擦除或写操作,就不能对当前程序所处的段进行擦除或写操作。

    另外,你可以看看数据手册,上面也写到代码在RAM里执行和从FLASH中执行,在ACtive状态时,功耗其实是不一样的。那里的从RAM执行,就是将代码复制到RAM区,然后执行的。

    希望对你有帮助!O(∩_∩)O~

  • 嗯,谢谢了!就是这个搞不明白,现在有点明白了,我想我们应该一般用的都是从FLASH开始的吧,

    那个把代码复制放到RAM应该一般都不会这样去做 。

  • 你好,我也有类似的问题,我想知道单片机内部硬件是如何区分擦除是从Flash还是Ram发起的,是通过判断假写指令所在地址或程序指针之类的方法么?  知道这些有助于我编写一个尽可能小的,在Flash擦除期间响应中断的功能函数。具体点说是不是可以这样:

    FCTL3 = FWKEY;                 // 清LOCK位    代码在Flsah
    FCTL1 = FWKEY | ERASE;  // 设置擦除位   代码在Flsah

    *EraseAddr = 0xff;                 // 擦除              代码在Ram

     while( FCTL3 | BUSY )         //等待               代码在Ram

    { 检查中断标志,并响应 };       //响应中断       代码在Ram

    FCTL3 = FWKEY | LOCK;    //                       代码在Flsah

    请问以上的代码示意,是否可以算是从Ram发起的?

x 出现错误。请重试或与管理员联系。