|
msp430在操作flash时一般都需要先屏蔽中断,确保flash的写入过程不被打断,现在俺有个不明白的问题是,如果将flash操作的函数放到某个中断服务函数里,是不是就不需要做这个处理了,俺的理解是msp430在进入某个中断服务函数后会自动屏蔽其他的中断,这样就相当于保证了flash写入过程的持续性。不知道俺理解的对不对。比如放到下面的中断函数中,是不是FlashErase和FlashModifyInt函数中就不需要增加_DINT()操作了。
- #pragma vector=PORT1_VECTOR
- __interrupt void Port1()
- {
- //以下为参考处理程序,不使用的端口应当删除其对于中断源的判断。
- if((P1IFG&BIT5) ==BIT5)
- {
- //处理P1IN.5中断
- P1IFG &= ~BIT5; //清除中断标志
- //以下填充用户代码
- if(FactoryModeFlag==0)
- {
- CorrectIndex = 0;
- FlashErase(SegD); //when entry factory mode,erase SegC;
- LEDBlink(50);
- LEDBlink(50);
- FactoryModeFlag = 1; //entry factory mode
- P1IES &= ~BIT5; //change Pin1.5 interrupt trigger mode to rising edge
- }
- else
- {
- FlashModifyInt(SegD,0x02,CorrectIndex);
- LEDBlink(5);
- LEDBlink(5);
- LEDBlink(5);
- LEDBlink(5);
- FactoryModeFlag = 0; //exit factory mode
- P1IES |= BIT5; //change Pin1.5 interrupt trigger mode to falling edge
- }
- }
- LPM3_EXIT; //退出中断后退出低功耗模式。若退出中断后要保留低功耗模式,将本句屏蔽
- }
复制代码
下面是俺参照资料连抄带改搞了个flash操作的函数:
- /*
- * FlashOperation.h
- *
- * Created on: 2013-9-16
- * Author: zhanghaibo1
- */
- #ifndef FLASHOPERATION_H_
- #define FLASHOPERATION_H_
- /*************************************
- * msp430G2553 has 4 data segments, there are
- * 64 bytes in every data segment. there are
- * 256 byte in all
- * D:0x1000--------0x003F
- * C:0x1040--------0x107F
- * B:0x1080--------0x10BF
- * A:0x10C0--------0x10FF
- ****************************************/
- #define SegA 0x010C0
- #define SegB 0x01080
- #define SegC 0x01040
- #define SegD 0x01000
- #define SegSize 64
- void FlashInit(void) //flash初始化
- {
- FCTL2 = FWKEY + FSSEL_2 + FN1; //选择SMCLK作为时钟源,二分频
- }
- void FlashCheckBusy(void) //flash检测忙
- {
- while(BUSY == (FCTL3 & BUSY)); //检测是否忙
- }
- void FlashErase(int SegX) //flash段擦除
- {
- _DINT();
- FlashCheckBusy(); //check flash is busy or not
- FCTL3 = FWKEY; //lock=0 means unlock
- FCTL1 = FWKEY + ERASE; //enable segment erase
- *((int *)SegX) = 0x00; //erase segment--write null
- FlashCheckBusy(); //flash is busy?
- FCTL3 = FWKEY | LOCK; //Lock
- return;
- }
- void FlashWriteChar(unsigned int addr,char wdata) //falsh写字节
- {
- _DINT(); //disable sum interrupt
- FlashCheckBusy(); //check if flash is busy
- FCTL3 = FWKEY; //Unlock
- FCTL1 = FWKEY + WRT; //Enable write
- *((unsigned char *)addr) = wdata; //write wdata to address of addr
- FCTL1 = FWKEY; //disable write
- FCTL3 = FWKEY + LOCK; //lock
- return;
- }
- char FlashReadChar(unsigned int addr) //flash读字节
- {
- char rdata;
- rdata = *(char*)addr; //read content of address addr
- return rdata;
- }
- void FlashWriteWord(unsigned addr,unsigned int wdata) //flash写字
- {
- _DINT(); //disable sum interrupt
- FlashCheckBusy(); //if flash is busy, then wait
- FCTL3 = FWKEY; //unlock
- FCTL1 = FWKEY + WRT; //enable write
- *((unsigned int *)addr) = wdata; //write wdata to addr
- FCTL1 = FWKEY; //disable write
- FCTL3 = FWKEY + LOCK; //lock
- }
- void FlashWriteInt(unsigned addr,int wdata) //flash write signed int
- {
- _DINT(); //disable sum interrupt
- FlashCheckBusy(); //if flash is busy, then wait
- FCTL3 = FWKEY; //unlock
- FCTL1 = FWKEY + WRT; //enable write
- *((unsigned int *)addr) = wdata; //write wdata to addr
- FCTL1 = FWKEY; //disable write
- FCTL3 = FWKEY + LOCK; //lock
- }
- unsigned int FlashReadWord(unsigned addr) //flash读字
- {
- unsigned int rdata;
- rdata = *(unsigned int *)addr; //read content of addr
- return rdata;
- }
- int FlashReadInt(unsigned addr) //flash read signed int
- {
- int rdata;
- rdata = *(int *)addr;
- return rdata;
- }
- void FlashModifyChar(unsigned int SegX,char AddrNum,char wdata) //flash修改字节
- {
- char i, TempArray[SegSize];
- for(i=0;i<SegSize;i++)
- {
- TempArray[i] = *(unsigned int *)(SegX + i);
- }
- TempArray[AddrNum] = wdata; //write wdata to one item in the array
- FlashErase(SegX); //erase segment
- FCTL3 = FWKEY; //unlock
- FCTL1 = FWKEY + WRT; //prepare write
- for(i=0;i<SegSize;i++)
- {
- *(unsigned int *)(SegX + i) = TempArray[i];
- }
- FCTL1 = FWKEY; //disable write
- FCTL3 = FWKEY + LOCK; //lock
- }
- //read address = AddrNum*2 + SegX; eg:if AddrNum=0x02 then real address = 1004
- void FlashModifyInt(unsigned int SegX,char AddrNum,int wdata) //flash修改int,zhb add this function 2013-10-09
- {
- char i;
- int TempArray[SegSize/2];
- for(i=0;i<(SegSize/2);i++)
- {
- TempArray[i] = *(unsigned int *)(SegX + i*2);
- }
- TempArray[AddrNum] = wdata; //write wdata to one item in the array
- FlashErase(SegX); //erase segment
- FCTL3 = FWKEY; //unlock
- FCTL1 = FWKEY + WRT; //prepare write
- for(i=0;i<SegSize/2;i++)
- {
- *(unsigned int *)(SegX + i*2) = TempArray[i];
- }
- FCTL1 = FWKEY; //disable write
- FCTL3 = FWKEY + LOCK; //lock
- _EINT(); //Enable interrupt
- }
- void FlashBurstWrite(unsigned int SegX,int *pStr) //flash批量写
- {
- int i;
- FlashErase(SegX); //erase segment
- FCTL3 = FWKEY; //unlock
- FCTL1 = FWKEY + WRT; //enable write
- for(i=0;i<2*sizeof(pStr);i++)
- {
- *(unsigned char *)(SegX + i) = *(pStr + i);
- }
- FCTL1 = FWKEY; //disable write
- FCTL3 = FWKEY + LOCK; //lock
- }
- #endif /* FLASHOPERATION_H_ */
复制代码
先谢谢各位了。 |
阿莫论坛20周年了!感谢大家的支持与爱护!!
月入3000的是反美的。收入3万是亲美的。收入30万是移民美国的。收入300万是取得绿卡后回国,教唆那些3000来反美的!
|