xor eax, eax




Ø main()
Ø fireworkx
Ø amp
Ø gallery
Ø feedbacks
Ø about




ronybc.com
45.113.122.52



flag counter

Buffered Flash memory access routines for PIC microcontrollers


This is an intermediate code layer for fast and flat; non block boundary limited access to Flash memory. Written for the CCS C compiler and libraries for Microchip PIC microcontrollers. This lib resides right on top of CCS C built-in functions read_program_memory() and write_program_memory(), fulfilling their limitations and making per byte read/write/erase operations to Flash memory possible like that of EEPROM.

do.. Flattr micro-donation

This code is originally written for PIC18F452. So the parameters RAM_ZEROZ, BLOCK_SIZE, and FLASH_DATA_END needs to be changed accordingly when implementing to a different PIC.

#byte RAM_ZEROZ = 0x0600

Is set so because, PIC18F452 have 1536 bytes on-chip RAM that ends at address 0x05ff and the vast unused address space after that is read as zero as per the datasheet.


The function flash_erase() erases destination bytes by writing zeros to it; sourced from this imaginary area, the '/dev/zero' of PIC.



// ==========================================================
// PIC18F452 buffered Flash routines
// written by Rony B Chandran ( http://www.ronybc.com )
// ==========================================================
// publik functions:
//	int1 flash_erase(flash_t addr, int count);
//	int1 flash_read(flash_t addr, byte *data, int count);
//	int1 flash_write(flash_t addr, byte *data, int count);
//	int1 flash_read_byte(flash_t addr, byte *data);
//	int1 flash_write_byte(flash_t addr, byte *data);
//	void flash_flush_buf();
//
// NOTE: max data size (read/write) = 256
// NOTE: adjust 'RAM_ZEROZ' with the PIC in use (pointing to the null area)
// NOTE: use 'flash_t' (int16/int32) wrt the flash address width

#byte RAM_ZEROZ = 0x0600 // < /dev/zero

#define BLOCK_SIZE 64
#define FLASH_DATA_START 0x5000
#define FLASH_DATA_END 0x7fff

#define flash_t int16
#define flash_eraze(A,N) flash_rw(A,&RAM_ZEROZ,N,1)
#define flash_read(A,D,N) flash_rw(A,D,N,0)
#define flash_write(A,D,N) flash_rw(A,D,N,1)
#define flash_read_byte(A,D) flash_rw(A,D,1,0)
#define flash_write_byte(A,D) flash_rw(A,D,1,1)
#define flash_flush_buf() buffer_flash_block(0)

byte flash_buf[BLOCK_SIZE];
int flash_auto_flush;
int1 flash_buf_taint;
flash_t flash_buf_block;

void init_flash()
{
	flash_buf_taint = 0;
	flash_buf_block = 0;
}

void buffer_flash_block(flash_t block)
{
	if(block == flash_buf_block)
	{
		return;
	}
	if(flash_buf_taint)
	{
		write_program_memory(flash_buf_block, flash_buf, BLOCK_SIZE);
	}
	flash_buf_taint = 0;
	if(block == 0)
	{
		return;
	}
	flash_buf_block = block;
	read_program_memory(block, flash_buf, BLOCK_SIZE);
}

int1 flash_rw(flash_t addr, byte *data, int count, int1 write)
{
	int offset, n;
	addr += FLASH_DATA_START;
	if(count == 0 || (addr + count) > FLASH_DATA_END)
	{
		return(0);
	}
	flash_auto_flush = 0;
	offset = addr & 0x3f;
	addr &= 0xffffffc0;
	buffer_flash_block(addr);
	if(count < (BLOCK_SIZE - offset))
	{
		n = count;
	}
	else
	{
		n = BLOCK_SIZE - offset;
	}
	if(write)
	{
		memcpy(flash_buf + offset, data, n);
		flash_buf_taint = 1;
	}
	else
	{
		memcpy(data, flash_buf + offset, n);
	}
	count -= n;
	data += n;
	addr += BLOCK_SIZE;
	while(count != 0)
	{
		if(count < BLOCK_SIZE)
		{
			n = count;
		}
		else
		{
			n = BLOCK_SIZE;
		}
		buffer_flash_block(addr);
		if(write)
		{
			memcpy(flash_buf, data, n);
			flash_buf_taint = 1;
		}
		else
		{
			memcpy(data, flash_buf, n);
		}
		count -= n;
		data += n;
		addr += BLOCK_SIZE;
	}
	return(1);
}


/*____________________________________________*/
/*               t h e  e n d                 */



Sample application video:




It runs a lighting script stored in PIC 18F452 microcontroller Flash by an interpreter program.

NAVIGATION PANEL:

Ø Parallel Resistance Calculator
Ø Voltage Divider Calculator
Ø LM317 Calculator
Ø Volume Control Pot with Parallel Resistor Graph tool
Ø Power Dissipation Across Transistor/MOSFET
Ø Display PPI Calculator
Ø Blogandrum - The Complete Conundrum
Ø The Eight Queens Puzzle
Ø Fireworkx : Linux version
Ø Kunthrantum - very low distortion audio power amplifier
Ø Creative AudioPCI soundcard mods (Ensoniq ES1370, AK4531)
Ø Buffered Flash memory access routines for PIC microcontrollers
Ø Fireworks : Windows version, coded using Win32 ASM
Ø SparcZ - tiny telnet server with remote desktop administration powers