Hello,
I have been browsing through many of the CC2530 or CC2531 writing to flash and reading from flash problem.
I am trying to do the same things as the others. Many questions asked here, However I could not find an answer.
I have the feeling that many of us here have tried the same code. But didnt include the right .h header files or .c file.
I guess many of us here did not write the code including the Z-Stack.
That is why many of us here have no success and many questions asked in the forum are without answers for this problem.
What I have for header files in my code is only the <ioCC2530.h>.
1) Is this correct just using this header file <ioCC2530.h> ?
2) I am using IAR for coding CC2530. How do i check if i successfully got the data written in Flash?
3) I provided my code below. A lot of people in the forum stuck in the same place.
while (!(DMAIRQ & 0x01)); //Wait Until Write Complete
DMAIRQ &= 0xFE; //Clear Any DMA IRQ on Channel 0 - Bit 0
Many of us stuck in the while loop in writeflashDMA.
The DMAIRQ just never activated, I guess the DMA transfer never complete the job therefore DMAIRQ never activate?
Could you please help for this matter? this could solve the problems for many of us here.
Highly appreciated!
/-------------------------------------------------------------------------
#include <ioCC2530.h>
//=================DMA傳輸=====================
#pragma bitfields=reversed
typedef struct {
unsigned int SRCADDRH; //Byte 0
unsigned int SRCADDRL; //Byte 1
unsigned int DESTADDRH; //Byte 2
unsigned int DESTADDRL; //Byte 3
unsigned int LENH:5; //Byte 4 - Bit 4:0
unsigned int VLEN:3; //Byte 4 - Bit 7:5
unsigned int LENL; //Byte 5
unsigned int TRIG:5; //Byte 6 - Bit 4:0
unsigned int TMODE:2; //Byte 6 - Bit 6:5
unsigned int WORDSIZE:1; //Byte 6 - Bit 7
unsigned int PRIORITY:2; //Byte 7 - Bit 1:0
unsigned int M8:1; //Byte 7 - Bit 2
unsigned int IRQMASK:1; //Byte 7 - Bit 3
unsigned int DESTINC:2; //Byte 7 - Bit 5:4
unsigned int SRCINC:2; //Byte 7 - Bit 7:6
} DMA_DESC;
#pragma bitfields=default
//==============主程式=================
void main( void )
{
buf[0]= 0;
buf[1]= 1;
buf[2]= 2;
buf[3]= 5;
buf[4]= 3;
buf[5]= 4;
buf[6]= 6;
buf[7]= 7;
Flash_PageErase(3);
WriteFlashDMA(buf,8, 0X1900);
Read_Flash_To_Buf(0X9900, test, 8);
while(1)
{
if(buf==0x00)
{
P1_0=0;
}
else
{
P1_0=~P1_0;
}
}
//WriteFlashDMA("01234567",8,0x1900);
} // main()
void WriteFlashDMA(uchar data[], int length, int flashadr) //Write Flash, DMA Method
//Length Must be Divisible by 4 or last bytes fail to write
{
DMA_DESC dmaConfig0;
//MEMCTR|=0X07;//
dmaConfig0.SRCADDRH = ((int)data >> 8) & 0x00FF; //
dmaConfig0.SRCADDRL = (int)data & 0x00FF;//;
dmaConfig0.DESTADDRH = (((int)&FWDATA) >> 8) & 0x00FF;
dmaConfig0.DESTADDRL = ((int)&FWDATA) & 0x00FF;//
dmaConfig0.VLEN = 0; //
dmaConfig0.LENH = (length>>8) & 0x00FF; //
dmaConfig0.LENL = length & 0x00FF;//
dmaConfig0.WORDSIZE = 0; //Size of Each Transfer - 0=8 Bit
dmaConfig0.TMODE = 0; //Transfer Mode - 1=Block, 0=Single
dmaConfig0.TRIG = 18; //DMA Trigger - 0=Manual Via DMAREQ, 18=Flash;
dmaConfig0.SRCINC = 1; //Source Address Increment - 1=1 Byte//
dmaConfig0.DESTINC = 0; //Destination Address Increment - 0=0 Bytes (Always Write to FWDATA, No Need to Increment)
dmaConfig0.IRQMASK = 0; //Interrupt Mask - 0=Disable Interrupts//
dmaConfig0.M8 = 0; //8th Bit Mode - 0=Use All 8 Bits
dmaConfig0.PRIORITY = 2; //Priority - 10(2)=High Priority
//DMA
while (FCTL & 0x80); //Wait Until DMA Controller is Available - Busy Bit 7//
/********* flash***********************************************/
FADDRH =(flashadr >> 10) & 0x00FF; // page size: 2048; select the flash page via FADDRH[7:1] bits
FADDRL =(flashadr >> 2) & 0x00FF; //
DMA0CFGH = (((int)&dmaConfig0) >> 8) & 0x00FF; //Pass DmaConfig0
DMA0CFGL = ((int)&dmaConfig0) & 0x00FF;
DMAARM |= 0x01; //Arm the DMA Channel//
FCTL |= 0x02; //Start Write
while (!(DMAIRQ & 0x01)); //Wait Until Write Complete
DMAIRQ &= 0xFE; //Clear Any DMA IRQ on Channel 0 - Bit 0
while (FCTL & (0x80)); //Wait Until Flash Controller is Not Busy - Busy Bit 7//
}
//flash
void Flash_PageErase(uchar byPage)
{
EA=0;
while(FCTL & 0x80);//
FADDRH = byPage << 1;//
FCTL |= 0x01;//
while(FCTL & 0x80);//
EA=1;
}
void Read_Flash_To_Buf(int read_adr, uchar *buf, uchar num_bytes)//read_adr=flashadr+0x8000
{
unsigned char i;
for( i=0; i<num_bytes; i++)
{
buf[i]=*(volatile char *)(read_adr + i);
}
}