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

📄 flash.c

📁 基于cc1010的设计实例
💻 C
字号:
/*****************************************************************************
 *                                                                           *
 *        **********                                                         *
 *       ************                                                        *
 *      ***        ***                                                       *
 *      ***   +++   ***                                                      *
 *      ***   + +   ***                                                      *
 *      ***   +                    CHIPCON CC1010 EXAMPLE PROGRAM            *
 *      ***   + +   ***                         FLASH                        *
 *      ***   +++   ***                                                      *
 *      ***       ***                                                        *
 *       ***********                                                         *
 *        *********                                                          *
 *                                                                           *
 *****************************************************************************
 * This program demonstrates the use of the halFlashWritePage() and          *
 * halCopy2Flash() functions for writing data to flash.                      *
 *                                                                           *
 * halFlashWritePage() requires a properly page-aligned XDATA buffer and     *
 * writes an entire page of the Flash with its content, much like what the   *
 * underlying hardware requires.                                             *
 *                                                                           *
 * halCopy2Flash() disguises the underlying hardware requirements of the     *
 * Flash Write DMA and allows the user to copy data of any length from any   *
 * address space to anywhere in the CODE (Flash) address space. A 128 byte   *
 * RAM buffer in XDATA is still required but it needs not be page-aligned    *
 * and thus not allocated to an absolute address.                            *
 *                                                                           *
 * The function checkBuffers has been defined to compare the content of the  *
 * written Flash buffer with the XRAM content. The function memcmp can also  *
 * be used, but will push the program length above the 2K evaluation version *
 * limit.                                                                    *
 *****************************************************************************
 * Author:              ARR                                                  *
 *****************************************************************************
 * Revision history:                                                         *
 * 1.0  2002/08/29      First public release                                 *
 *                                                                           *
 * $Log: flash.c,v $
 * Revision 1.2  2002/11/18 10:47:10  kht
 * Added startup macros
 *
 * Revision 1.1  2002/10/11 14:58:13  tos
 * Initial version in CVS.
 *
 *                                                                           *
 ****************************************************************************/

#include <chipcon/hal.h>
#include <chipcon/cc1010eb.h>
#include <stdlib.h>
#include <string.h>

// Define length constants
#define RND_LENGTH          300
#define SHORT_LENGTH        3
#define LONG_LENGTH         300

// Define the absolute addresses to use in conjunction with the 
// halFlashWritePage() function.
#define RAMBUF_ADDRESS      0x0000
#define FLASHPAGE_ADDRESS   0x6000

// function prototypes
bool checkBuffers(byte code *buffer1, byte xdata *buffer2, byte length);


// Set up properly aligned RAM-buffer and Flash page for halFlashWritePage()
byte xdata ramBuf[128] _at_ RAMBUF_ADDRESS;
byte code flashPage[128] _at_ FLASHPAGE_ADDRESS;

// Array which will be initialized with random bytes
byte xdata rndData[300];

// RAM-buffer for use with halCopy2Flash(). Not page-aligned...
byte xdata ramBufNonAligned[128];

// Non-page-aligned arrays (short + long) in Flash.
byte code shortFlashArray[SHORT_LENGTH];
byte code longFlashArray[LONG_LENGTH];



int main() {

    word i;

    // Turn off watchdog timer
    WDT_ENABLE(FALSE);

    // Startup macros for speed and low power consumption
    MEM_NO_WAIT_STATES();
    FLASH_SET_POWER_MODE(FLASH_STANDBY_BETWEEN_READS);
    
    // All I/Os are inputs after reset. Make I/Os connected to LEDs outputs
    RLED=YLED=GLED=BLED=LED_OFF;
    RLED_OE(TRUE); YLED_OE(TRUE); GLED_OE(TRUE); BLED_OE(TRUE);

    // Fill rndData with random contents.
    srand(0xABCD);
    for (i=0; i<RND_LENGTH; i++)
        rndData[i]=rand()%0xFF;
    
    // Copy 128 bytes (one page) of random data to Flash using the 
    // halFlashWritePage() function. Then check to see if it succeeded. If so
    // light up Yellow LED, otherwise light up Red LED.
    memcpy(ramBuf, rndData, 128);
    halFlashWritePage(flashPage, ramBuf, CC1010EB_CLKFREQ);
    // if not eval version, use: if (memcmp(flashPage, rndData, 128))
    if (!checkBuffers(flashPage, rndData, 128))
        RLED=LED_ON;                    // Verify failed
    else
        YLED=LED_ON;                    // Verify OK
    
    // Copy just a few bytes into an array somewhere in Flash using
    // halCopy2Flash().
    halCopy2Flash(
        shortFlashArray, &rndData[100], SHORT_LENGTH, 
        ramBufNonAligned, CC1010EB_CLKFREQ
    ); 
    // if not eval version, use: if (memcmp(shortFlashArray, &rndData[100], SHORT_LENGTH))
    if (!checkBuffers(shortFlashArray, &rndData[100], SHORT_LENGTH))
        RLED=LED_ON;                    // Verify failed
    else
        GLED=LED_ON;                    // Verify OK

    // Copy many bytes (more than one page)into an array somewhere in Flash 
    // using halCopy2Flash().
    halCopy2Flash(
        longFlashArray, rndData, LONG_LENGTH, 
        ramBufNonAligned, CC1010EB_CLKFREQ
    ); 
    // if not eval version, use: if (memcmp(longFlashArray, rndData, LONG_LENGTH))
    if (!checkBuffers(longFlashArray, rndData, LONG_LENGTH))
        RLED=LED_ON;                    // Verify failed
    else
        BLED=LED_ON;                    // Verify OK

    // Infinite loop
    while (1);
}


// Function checks a Flash and an XRAM buffer for equivalence
// Returns TRUE if equivalent
bool checkBuffers(byte code *buffer1, byte xdata *buffer2, byte length) {

    int i;

    for (i=0; i<length; i++) {
        if (*buffer1 != *buffer2)
            return 0x00;
        else {
            buffer1++;
            buffer2++;
        }   
    }
    return 0x01;

}

// Flash interrupt handler (do nothing)
// We need to handle the interrupt even though we do not do anything.
// If not, the program will not run correctly except under the debugger,
// which has its own Flash interrupt handler

void FlashIntrHandler(void) interrupt INUM_FLASH {
    
    INT_SETFLAG(INUM_FLASH, INT_CLR);    
    return;
}

⌨️ 快捷键说明

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