⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 flash.c

📁 CC1110 CC2510 片上flash操作工程实例 可能过串口终端完成片上flash的擦除或写入动作。
💻 C
字号:

#include "includes.h"

//Key define
#define ENTER      		0x0D
#define BACK_SPACE 		0x08
#define ESC        		0x1B

#define STRING_LENGTH 	35 						// 35 bytes in XDATA RAM to which the flash write procedure
#define PAGE_ADDRESS 	0x7000
#define PAGE_NUMBER 	(PAGE_ADDRESS >> 10)	// 等价于PAGE_ADDRESS / 1024 转换为页地址 
                                                // (each page is Controller, the flash memory is word-1024 bytes) depending on the total flash size

__root __code const unsigned char IEEE_ADDRESS[8] @ PAGE_ADDRESS = {
 
  'i','e','e','e','b','u','f',0
};


volatile    DMA_DESC dmaChannel;

/******************************************************************************
* @fn  writeFlashUsingDMA
*
* @brief
*      Writes data to flash using DMA. Erases the page in advance if told to.
*
* Parameters:
*
* @param  unsigned char * pSrcAddr
*         The start of the data to be written to flash.
*
*         int length
*         The number of bytes to be written to flash.
*
*         unsigned int flashAddress
*         The address in flash the data is to be written to.
*
*         bool erase
*         Indicating whether the flash is to be erased or not.
*
* @return void
*
******************************************************************************/
void writeFlashUsingDMA(unsigned char * pSrcAddr, int length, unsigned int flashAddress, bool erase)
{
	unsigned char buffer[10];

   	INT_GLOBAL_ENABLE(INT_OFF);

   	// Setting up the flash address,
   	// erasing the page if required.
   	SET_WORD(FADDRH, FADDRL, (int)(flashAddress >> 1));
   	if(erase == TRUE)
   	{
		halFlashErasePage(buffer, PAGE_NUMBER);
   	}

   	halWait(255);

   	// Making sure a multiplum of 4 bytes is transferred.
   	while(length & 0x0003){
		length++;
   	}

   	SET_WORD(dmaChannel.SRCADDRH, dmaChannel.SRCADDRL,   pSrcAddr);   // The start address of the segment
   	SET_WORD(dmaChannel.DESTADDRH, dmaChannel.DESTADDRL, &X_FWDATA);  // Input of the AES module
   	SET_WORD(dmaChannel.LENH, dmaChannel.LENL, length);               // Setting the length of the transfer (bytes)
   	dmaChannel.VLEN      = VLEN_USE_LEN;      // Using the length field
   	dmaChannel.M8        = M8_USE_8_BITS;     // Transferring all 8 bits in each byte.
   	dmaChannel.IRQMASK   = FALSE;             // The DMA complete interrupt flag is set at completion.
   	dmaChannel.DESTINC   = DESTINC_0;         // The destination address is constant
   	dmaChannel.SRCINC    = SRCINC_1;          // The address for data fetch is inremented by 1 byte
   	dmaChannel.TRIG      = DMATRIG_FLASH;     // Setting the FLASH module to generate the DMA trigger
   	dmaChannel.TMODE     = TMODE_SINGLE;      // A single byte is transferred each time.
   	dmaChannel.WORDSIZE  = WORDSIZE_BYTE;     // Set to count bytes.

   	// Setting up the DMA.
   	// Clearing all DMA complete flags and arming the channel.
   	DMA_SET_ADDR_DESC0(&dmaChannel);
   	DMA_ABORT_CHANNEL(0);
   	DMAIRQ &= ~DMA_CHANNEL_0;
   	DMA_ARM_CHANNEL(0);


   	// Starting to write
   	FLASH_CONFIG(WRITE);

   	// Waiting for the DMA to finish.
   	while(!(DMAIRQ & DMA_CHANNEL_0));
   	DMAIRQ &= ~DMA_CHANNEL_0;
    
   INT_GLOBAL_ENABLE(INT_ON);
        
   	return;
}

/******************************************************************************
* @fn  flash_main
*
* @brief
*      Main function.
*
* Parameters:
*
* @param  void
*
* @return void
*
******************************************************************************/
void main(void){

   	unsigned char buffer[30];
   	char inputBuffer[STRING_LENGTH];
   	char pointer = 0;
   	char c;
    
	SET_MAIN_CLOCK_SOURCE(CRYSTAL);

   	// Clearing buffers
   	memset(buffer,0,sizeof(buffer));
   	memset(inputBuffer,0,sizeof(inputBuffer));
   
	initUART();
  
    printf((char*)"\n");
    printf((char*)"|===========================================|\n");
    printf((char*)"|                                           |\n");
	printf((char*)"|          Flash Programming V1.0           |\n");
    printf((char*)"|         Written: Cuiqingwei [gary]        |\n");
    printf((char*)"|                                           |\n");
    printf((char*)"|===========================================|\n");
    
   	printf((char*)"Type data to be written\n");
   	printf((char*)"ENTER: store in flash; ESC: read from flash\n");
    
   	memset(inputBuffer,0,STRING_LENGTH);

   	while(1)
   	{
      	c = getUART();
		
      	switch (c){
      		case ENTER:
         		inputBuffer[pointer] = 0;
                
         		printf((char*)"\nTo write: %s\nENTER if OK.\n",inputBuffer);
                halFlashWritePage((unsigned char*) &inputBuffer, buffer,PAGE_NUMBER);
                //writeFlashUsingDMA((unsigned char*) &inputBuffer, STRING_LENGTH, PAGE_ADDRESS, TRUE);
                pointer = 0;
                
         	break;
      		case BACK_SPACE:
         		// Erasing the last typed data.
         		if (pointer > 0)
         		{
            		pointer--;
            		inputBuffer[pointer] = ' ';
         		}
         	break;
      		case ESC:
         		// Abort Flash write.
                printf("\f");
                printf((char*)"Updated:");
      			printf((char*)" %s\n",(char __code*) PAGE_ADDRESS);
         	break;
      		default:
         		// Add typed data to buffer.
         		if (pointer < STRING_LENGTH-1)
         		{
            		inputBuffer[pointer] = c;
            		pointer++;
         		}
         	break;
      	}
   	}
}

/*------------------------------------------------------------------------------
										  0ooo
								ooo0     (   )
								(   )     ) /
								 \ (     (_/
	    				          \_)        By:cuiqingwei [gary]
------------------------------------------------------------------------------*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -