Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small footprint flash #1

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
app_*/.build_*/
app_*/bin/
doc/.doxygen/
doc/.linked_dirs/
doc/.sources/
doc/Doxyfile-e
doc/_build/
doc/index.pdf
module_flash/.build_flash_dev/
module_flash/lib/
9 changes: 9 additions & 0 deletions app_example_flash_small/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
TARGET = XK-1
APP_NAME =
USED_MODULES = module_flash_small
XCC_FLAGS_Debug =
XCC_FLAGS_Release = -g
VERBOSE = 0

XMOS_MAKE_PATH ?= ../..
-include $(XMOS_MAKE_PATH)/xcommon/module_xcommon/build/Makefile.common
109 changes: 109 additions & 0 deletions app_example_flash_small/src/main.xc
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
#include <flash.h>
#include <stdio.h>


//::program that tests erase sector

testBytes(int start, int end, int value) {
char data[1];
printf("Testing that bytes %d..%d are 0x%02x\n", start, end, value);
for(int i = start; i < end; i++) {
spiFlashRead(i, data, 1);
if (data[0] != value) {
printf("Byte %d is 0x%02x rather than 0x%02x- sector size/erase command are not consistent\n", i, data[0] & 0xff, value);
}
}
}

testErase() {
char data[256];

spiInit();
printf("Erasing %d bytes (2 sectors)\n", SPI_SECTOR_SIZE*2);
spiFlashErase(0, SPI_SECTOR_SIZE*2);
testBytes(0, SPI_SECTOR_SIZE*2, 0xff);
for(int i = 0; i < 256; i++) {
data[i] = 0;
}
printf("Setting 2 sectors to 0x00\n");
for(int i = 0; i < SPI_SECTOR_SIZE*2; i+=256) {
spiFlashWriteSmall(i, data, 256);
}
testBytes(0, SPI_SECTOR_SIZE*2, 0x00);
printf("Erasing %d bytes (1 sectorx)\n", SPI_SECTOR_SIZE);
spiFlashErase(0, SPI_SECTOR_SIZE);
testBytes(0, SPI_SECTOR_SIZE, 0xff);
testBytes(SPI_SECTOR_SIZE, SPI_SECTOR_SIZE*2, 0x00);
}
//::

#define spiInit() ;

//::spi program
#include <spi.h>

useSPI() {
spiInit();
printf("ID4: %08x\n",spiCommandStatus(SPI_CMD_READID, 4));
}
//::

//::flash program
#include <flash.h>

useFlash() {
char data[256];
int addr = 0x18800;
spiInit();

spiFlashRead(addr, data, 32);
for(int i = 0; i < 32; i++) {
printf(" %02x", data[i]);
}
printf("\n");
spiFlashErase(addr, 32);
spiFlashRead(addr, data, 32);
for(int i = 0; i < 32; i++) {
printf(" %02x", data[i]);
}
printf("\n");
for(int i = 0; i < 32; i++) {
data[i] = i;
}
printf("\n");
spiFlashWriteSmall(addr, data, 32);
spiFlashRead(addr, data, 32);
for(int i = 0; i < 32; i++) {
printf(" %02x", data[i]);
}
printf("\n");
}
//::

//::persistence program
#include <flash.h>

char state[15];

usePersistence() {
int valid;
spiInit();

valid = spiFlashPersistentStateRead(state);
if (!valid) {
// fill state[] with factory default
}

//...
// update state
spiFlashPersistentStateWrite(state);
//...
}
//::

main() {
testErase();
useSPI();
useFlash();
usePersistence();
}
9 changes: 9 additions & 0 deletions app_test_flash_small/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
TARGET = XK-1
APP_NAME =
USED_MODULES = module_flash_small
XCC_FLAGS_Debug =
XCC_FLAGS_Release = -g -O2
VERBOSE = 0

XMOS_MAKE_PATH ?= ../..
-include $(XMOS_MAKE_PATH)/xcommon/module_xcommon/build/Makefile.common
43 changes: 43 additions & 0 deletions app_test_flash_small/src/main.xc
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include <flash.h>
#include <stdio.h>

int errors = 0;

void testBytes(int start, int end, int value) {
char data[1];
// printf("Testing that bytes %d..%d are 0x%02x\n", start, end, value);
for(int i = start; i < end; i++) {
spiFlashRead(i, data, 1);
if (data[0] != value) {
printf("Byte %d is 0x%02x rather than 0x%02x- sector size/erase command are not consistent\n", i, data[0] & 0xff, value);
errors++;
}
}
}

int main(void) {
char data[256];

spiInit();
// printf("Erasing %d bytes (2 sectors)\n", SPI_SECTOR_SIZE*2);
spiFlashErase(0, SPI_SECTOR_SIZE*2);
testBytes(0, SPI_SECTOR_SIZE*2, 0xff);
for(int i = 0; i < 256; i++) {
data[i] = 0;
}
// printf("Setting 2 sectors to 0x00\n");
for(int i = 0; i < SPI_SECTOR_SIZE*2; i+=256) {
spiFlashWriteSmall(i, data, 256);
}
testBytes(0, SPI_SECTOR_SIZE*2, 0x00);
// printf("Erasing %d bytes (1 sectorx)\n", SPI_SECTOR_SIZE);
spiFlashErase(0, SPI_SECTOR_SIZE);
testBytes(0, SPI_SECTOR_SIZE, 0xff);
testBytes(SPI_SECTOR_SIZE, SPI_SECTOR_SIZE*2, 0x00);
printf("Erase code 0x%02x and sector size %d ", SPI_CMD_ERASE, SPI_SECTOR_SIZE);
if (errors == 0) {
printf("appear to work for this device\n");
} else {
printf("appear to be broken for this device\n");
}
}
3 changes: 3 additions & 0 deletions app_test_flash_small/src/spi_conf.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#define SPI_CLK_MHZ 25
#define SPI_CMD_ERASE 0xD8
#define SPI_SECTOR_SIZE 32768
9 changes: 9 additions & 0 deletions doc/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
REPO=../../sc_flash
VERSION=2v0
DOXYGEN_DIRS=$(REPO)/module_flash_small
SOURCE_INCLUDE_DIRS=$(REPO)
XDOC_DIR ?= ../../xdoc
include $(XDOC_DIR)/Makefile.inc

all: html pdf
@if [ ! -d $(REPO)_gh_pages ] ; then echo '**** no gh_pages checked out ****'; exit 0; else cp -r _build/html/* $(REPO)_gh_pages/; cp -r _build/html/.doctrees $(REPO)_gh_pages/; echo 'HTML files copied to $(REPO)_gh_pages'; echo 'Now go to $(REPO)_gh_pages, add, commit, and push to publish the documentation'; fi
Loading