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

📄 flashtest.c

📁 This a simple bootloader for AT91SAM7{S,X}{64,128,256} processors. It permits you to download new c
💻 C
字号:
// Flash memory store/recall for the Olimex SAM7-P256//// Public domain software by Adam Pierce  http://www.doctort.org/adam/// You may freely copy this and use it for whatever purpose you like.// 15-Apr-2007//// This is example code which accompanies the article at http://www.doctort.org/adam/nerd-notes/arm-flash-memory.html//// Operate this by using a serial terminal connected to the first serial port on the ARM dev board.//#include "AT91SAM7S256.h" // Definitions of the ARM chip and on-chip peripherals.#include "Board.h"        // Definitions of peripherals on the Olimex dev board.#include <stdio.h>#include <string.h>// Macro to activate a peripheral on the ARM7.#define ARM_ACTIVATE_PERIPHERAL(id) AT91C_BASE_PMC->PMC_PCER = 1 << (id)// Switches the given PIO pins from PIO function to a different peripheral.#define ARM_ROUTE_PERIPHERAL_A(pPio,mask) { pPio->PIO_PDR = mask; pPio->PIO_ASR = mask; }// We will store our data in the last page of Flash.#define OUR_FLASH_ADDR (AT91C_IFLASH + AT91C_IFLASH_SIZE - AT91C_IFLASH_PAGE_SIZE)#define COMM_SPEED 9600#define AT91C_MC_WRITE_KEY  ((unsigned)0x5A << 24) // Value to enable flash commands.// ***************************************************************// ** flashInit - Sets up the flash writing peripheral. Call this// **             before you do any flash writes.// **void flashInit(){// Get base location of memory controller peripheral.	volatile AT91PS_MC mc = AT91C_BASE_MC;// Enable the auto-erase feature and set up flash write timing.	mc->MC_FMR = AT91C_MC_FWS_1FWS | (1 + (((MCK * 15) / 10000000)) << 16);}// ***************************************************************// ** flashWrite - Write the given data to the given address in// **              Flash memory. Erases the 256 byte page before// **              writing.// **              address must be DWORD-aligned.// **              data must not cross a 256-byte boundary.// **              Returns nonzero on successful write.// **int flashWrite(unsigned address, const void *data, unsigned size){	unsigned i;	unsigned page;	unsigned *src  = (unsigned *)data;	unsigned *dest = (unsigned *)address;// Get base location of memory controller peripheral.	volatile AT91PS_MC mc = AT91C_BASE_MC;// Calculate the number of DWORDs to be written.	size = (size + 3) / 4;// Copy the data into the flash write buffer (this must be done 32 bits at a time).	for(i = 0; i < size; i++)		dest[i] = src[i];// Write the page to Flash memory.	page = ((char *)dest - AT91C_IFLASH) / AT91C_IFLASH_PAGE_SIZE;	mc->MC_FCR = AT91C_MC_WRITE_KEY | AT91C_MC_FCMD_START_PROG | (page << 8);// Check for errors.	if(0 != (mc->MC_FSR & AT91C_MC_PROGE))		return 0;// Wait for write cycle to complete.	while(0 == (mc->MC_FSR & AT91C_MC_FRDY))		;	return 1;}int main(){	volatile AT91PS_PIO    pPioA  = AT91C_BASE_PIOA; // PIOA	volatile AT91PS_USART  pPortA = AT91C_BASE_US0;  // Serial port A	char buf[96];	int  size;// Set up PIOA device.	ARM_ACTIVATE_PERIPHERAL(AT91C_ID_PIOA);// Set up US0 serial port.	ARM_ACTIVATE_PERIPHERAL(AT91C_ID_US0);  // Enable serial port 0.	ARM_ROUTE_PERIPHERAL_A(pPioA,US0_MASK); // Route to external pins.	pPortA->US_CR = AT91C_US_RSTRX  // Reset Receiver.
	              | AT91C_US_RSTTX  // Reset Transmitter.	              | AT91C_US_RXDIS  // Receiver Disable.	              | AT91C_US_TXDIS; // Transmitter Disable.

	pPortA->US_MR = AT91C_US_USMODE_NORMAL   // Normal Mode.	              | AT91C_US_CLKS_CLOCK      // Clock = MCK.	              | AT91C_US_CHRL_8_BITS     // 8-bit Data.	              | AT91C_US_PAR_NONE        // No Parity.	              | AT91C_US_NBSTOP_1_BIT;   // 1 Stop Bit.

	pPortA->US_BRGR = (MCK/16/COMM_SPEED);   // Compute baud rate divisor

	pPortA->US_CR = AT91C_US_RXEN   // Receiver Enable	              | AT91C_US_TXEN;  // Transmitter Enable
// Initialize flash write peripheral.	flashInit();	printf("Flash memory test by Adam Pierce - http://www.doctort.org/adam/\n\n");// Retrieve string from Flash memory.	if(*OUR_FLASH_ADDR != 0xFF)		printf("Current data: %-80s\n", OUR_FLASH_ADDR);// Ask the user to enter some text.	printf("Enter some text (up to 80 characters) > ");	scanf("%[^\r]", buf);// Write that text to the flash memory.	size = strlen(buf) + 1;	flashWrite(OUR_FLASH_ADDR, buf, size);	printf("\nWrote %d bytes to flash at address 0x%08X.\n\n", (3 + size) & ~3, OUR_FLASH_ADDR);// Exit back to SAM-BA.	return 0;}

⌨️ 快捷键说明

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