Example #1
0
File: SPI.c Project: thonv54/IR-PIR
void TestFlash(void) {
	unsigned char RdData[64] = { 0 };
	unsigned char data[64] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
			15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
			0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
			19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31 };
	flash_sector_erase(0);
	flash_page_program(0, data, 64);
	flash_read(0, RdData, 64);
	__delay_ms(100);
}
//TODO: works with mavlink_log, should test it for standards log.
int flash_write(uint32_t addr, const void *buf, size_t len)
{
    int ret = 0;

    const uint8_t *p_data = (const uint8_t *) buf;

    size_t pos = 0;
    size_t align = FLASH_PAGE_SIZE - (addr % FLASH_PAGE_SIZE);
    align = align < len ? align : len;

    while (pos < len && ret == 0) {
    	//write new data
    	if (ret == 0) {
    		ret = flash_page_program(addr + pos, &(p_data[pos]), align);
    	}

    	//update pointers and counters
    	pos += align;
		align = len - pos < FLASH_PAGE_SIZE ? len - pos : FLASH_PAGE_SIZE;
    }
    return ret;
}