📄 flash.c
字号:
{ /* ** Compile time error? */ TRACE1 ("%C: No flash devices?\n"); } }#endif}/*** Name: flash_deinitialise**** Purpose: Release any resources used by the FLASH sub-system.**** Arguments:** <None>**** Result:** <Nothing>***/void flash_deinitialise (void){#if !defined (BOOT_ROM) || !defined (NO_CONFIGINFO_FLASH) /* ** The only resources used are malloced memory for a sector buffer: ** no requirement to set the initialised status to false. */ if (NULL != flashSectorCopy) { free (flashSectorCopy); flashSectorCopy = NULL; }#endif return;}/*** Name: flash_checksum**** Purpose: Perform checksum calculation on specified area of FLASH memory**** Calculates the checksum according to the algorithm used by the boot ROMs** and returns the value. The function is used to check FLASH contents on** FLASHFS initialisation and during a console 'fsck' and also when updating** the FLASH to calculate the correct new checksum value.**** Arguments:** start Source address (FLASH address)** len Length in bytes**** Result:** sum 32 bit checksum***/BITS flash_checksum(U32 start, U32 len){ BITS b, c0, c1; U32 addr, sub_len; addr = start; /* check we not going to go anything silly ? */ assert(addr <= TOTAL_FLASH_SIZE); assert((addr + len) <= TOTAL_FLASH_SIZE); assert(initialised); /* loop until all done, inner loop is on a per-device basis */ for (c0 = c1 = 0; len; len -= sub_len, addr += sub_len) { U32 device = FLASH_DEVICE(addr); U32 offset = addr - flashDevices [device].logicalBase; U32 endchip; SELECT_FLASH_DEVICE (device); /* sub length is remaining length in this device ... */ sub_len = flashDevices [device].size - offset; /* _OR_ the 'real' length if this is less */ sub_len = len < sub_len ? len : sub_len; /* calc 'final' end of device offset from length on this pass. */ endchip = offset + sub_len; if (flashDevices [device].Is16) { /* loop round adding 1 byte at a time from the current device. */ for(; offset < endchip; offset += 2) { U16 temph; BYTE tempb; temph = FLASH_READ_RAW16(offset, device); tempb = (BYTE)temph; c0 += tempb; c1 += c0; c0 += temph >> 8; c1 += c0; } /* do a chips worth */ } else { /* loop round adding only 1 byte at a time from the current device. */ for (offset *= STRIDE, endchip *= STRIDE; offset < endchip; offset += STRIDE) { c0 += (BITS) FLASH_READ_RAW(offset, device); c1 += c0; } /* do a chips worth */ } } /* for total length */ b = (c0 & 65535) + ((c0 >> 16) & 65535); c0 = ((b >> 16) + b) & 65535; b = (c1 & 65535) + ((c1 >> 16) & 65535); c1 = ((b >> 16) + b) & 65535; return((c1 << 16) | c0);} /* flash_checksum *//* the address is a logical flash one, *not* a flash filing system address. */inline static BYTE flash_read_byte(U32 addr){ U32 device = FLASH_DEVICE(addr); U32 offset = addr - flashDevices [device].logicalBase; assert(device < NUM_FLASH_DEVICES); addr = offset * STRIDE; SELECT_FLASH_DEVICE (device); return FLASH_READ_RAW(addr, device);} inline static BYTE flash_read_byte16(U32 addr){ U32 device = FLASH_DEVICE(addr); U32 offset = addr - flashDevices [device].logicalBase; U16 data; assert(device < NUM_FLASH_DEVICES); assert (flashDevices [device].Is16); addr = offset * STRIDE; SELECT_FLASH_DEVICE (device); data = FLASH_READ_RAW16(addr, device); if (addr & 1) data >>= 8; return data;}/* the address is a logical flash one, *not* a flash filing system address. */inline static U16 flash_read_16(U32 addr){ U32 device = FLASH_DEVICE(addr); U32 offset = addr - flashDevices [device].logicalBase; assert(device < NUM_FLASH_DEVICES); addr = offset * STRIDE; SELECT_FLASH_DEVICE (device); return FLASH_READ_RAW16(addr, device);} /* the address is a logical flash one, *not* a flash filing system address. */inline static BITS flash_read_word(U32 addr){ U32 device = FLASH_DEVICE(addr); U32 offset = addr - flashDevices [device].logicalBase; assert(device < NUM_FLASH_DEVICES); assert(0 == (addr & 3)); /* Must be word aligned */ addr = offset * STRIDE; SELECT_FLASH_DEVICE (device);#if (STRIDE != 1) || (defined (SIMULATOR)) { BITS a; a = (BITS) FLASH_READ_RAW (addr, device); a |= (BITS)(FLASH_READ_RAW((addr + STRIDE), device) << 8); a |= (BITS)(FLASH_READ_RAW((addr + STRIDE * 2), device) << 16); a |= (BITS)(FLASH_READ_RAW((addr + STRIDE * 3), device) << 24); return a; }#else { BITS *a = (BITS *)&FLASH_READ_RAW(addr, device); return *a; }#endif}/*** Name: flash_raw_read_word**** Purpose: Read word from FLASH **** Arguments:** U32 logical address (0 ... (TOTAL_FLASH_SIZE - 1))**** Result:** BITS read from FLASH***/BITS flash_raw_read_word(U32 addr){ assert(initialised); return flash_read_word (addr);}/*** Name: flash_read_data**** Purpose: Read data from flash device(s)**** This function is logically equivalent to 'fread'. It hides all the detail** involved in reading the FLASH devices from the calling function.**** Arguments:** s Source address (FLASH address)** pd Pointer to desination** n Number of bytes to read**** Result:** <None>***/void flash_read_data(U32 s, BYTE *d, U32 n){ assert(s <= TOTAL_FLASH_SIZE); assert(s+n <= TOTAL_FLASH_SIZE); assert(initialised); while (n > 0) { U32 device = FLASH_DEVICE (s); U32 offset = s - flashDevices [device].logicalBase; U32 blockLength, bl; if (n > (flashDevices [device].size - offset)) { blockLength = flashDevices [device].size - offset; } else { blockLength = n; } bl = blockLength; if (flashDevices [device].Is16) { while (bl > 0) { *d++ = flash_read_byte16(s++); --bl; } } else { /* Check for any parameter that is not a word (i.e. modulo 4) */ if ((((BITS)s) | ((BITS)d) | ((BITS)bl)) % sizeof (BITS)) { while (bl > 0) { *d++ = flash_read_byte(s++); --bl; } } else { /* special case, read words rather than bytes */ BITS *dest = (BITS *)d; bl /= sizeof (BITS); while (bl > 0) { *dest++ = flash_read_word(s); s += sizeof (U32); --bl; } /* ** Don't forget to update the destination too. */ d = (BYTE) dest; } } n -= blockLength; }}/*** Name: flash_total_size**** Purpose: Return size of flash for specified number of chips.**** This counts the size from the first flash chip upwards.**** Arguments:** U32 Number of chips to be considered.**** Result:** U32 Size of flash for number of chips or 0 if error.***/U32 flash_total_size (U32 numberChips){ U32 size = 0; if (numberChips <= NUM_FLASH_DEVICES) { while (numberChips-- != 0) { size += flashDevices [numberChips].size; } } return size;}/*** Name: flash_config**** Purpose: Display flash configuration information**** Arguments:** <None>**** Result:** <Nothing>***/void flash_config (void){ U32 i; /* ** Basic configuration. */ printf ("Flash configuration: %d chips", NUM_FLASH_DEVICES);#ifdef PAGED_FLASH printf (", paged");#endif#ifdef WINDOWED_FLASH printf (", windowed (0x%08x window)", (1 << FLASH_WINDOW_BITS));#endif printf ("\n"); /* ** Compiled parameters and actual devices present */ for (i = 0; i < NUM_FLASH_DEVICES; i++) { printf ("Chip %d size %u @ 0x%08x", i, flashDevices [i].size, flashDevices [i].physicalBaseRead); if (flashDevices [i].flashPage != FLASH_NOPAGE) { printf (", page %u", flashDevices [i].flashPage); } printf ("\n");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -