view flash.c @ 89:fc21fb5b171b default tip

Make error message more useful
author Daniel O'Connor <darius@dons.net.au>
date Fri, 13 Mar 2015 11:39:59 +1030
parents 05ba84c7da97
children
line wrap: on
line source

#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "spiflash.h"

void
flashcmd(int argc, char **argv) {
    uint8_t status, tmp, len;
    uint32_t addr;

    if (argc == 0) {
	fputs("No command specified\n", stdout);
	return;
    }

    if (!strcmp(argv[0], "str")) {
	status = spiflashreadstatus();
	fputs("Status = ", stdout);
	spiflashprintstatus(status, stdout);
	printf("(0x%02x)\n", status);
    } else if (!strcmp(argv[0], "stw")) {
	if (argc != 2) {
	    fputs("Incorrect number of arguments\n", stdout);
	    return;
	}
	tmp = atoi(argv[1]);
	spiflashwritestatus(tmp);
	status = spiflashreadstatus();
	printf("Wrote 0x%02x to status, now 0x%02x\n", tmp, status);
    } else if (!strcmp(argv[0], "er")) {
	if (argc != 2) {
	    fputs("Incorrect number of arguments\n", stdout);
	    return;
	}
	addr = atoi(argv[1]);
	spiflash4kerase(addr);
	printf("Erased 0x%x\n", (unsigned int)addr);
    } else if (!strcmp(argv[0], "rd")) {
	if (argc < 2) {
	    fputs("Incorrect number of arguments\n", stdout);
	    return;
	}

	addr = atoi(argv[1]);

	if (argc > 2)
	    len = atoi(argv[2]);
	else
	    len = 16;

	spiflashstartread(addr);

	for (uint32_t i = 0; i < len; i++)
	    printf("Read 0x%02x from 0x%06x\n", spiflashreadbyte(), (unsigned int)(addr + i));
	spiflashstopread();

	fputs("\n", stdout);
    } else if (!strcmp(argv[0], "wr")) {
	if (argc < 2) {
	    fputs("Incorrect number of arguments\n", stdout);
	    return;
	}

	addr = atoi(argv[1]);

	if (argc > 2)
	    len = atoi(argv[2]);
	else
	    len = 16;

	for (int i = 0; i < 16; i += 2) {
	    uint16_t data;
	    data = ((i + 1) << 8) | i;
	    printf("Writing 0x%04x to 0x%06x\n", data, (unsigned int)(addr + i));

	    if (i == 0)
		spiflashstartwrite(addr, data);
	    else
		spiflashwriteword(data);
	}
	spiflashstopwrite();
    } else if (!strcmp(argv[0], "id")) {
	printf("Flash ID = 0x%04hx (expect 0xbf41)\n", spiflashreadid());
    } else {
	fputs("Unknown sub command\n", stdout);
	return;
    }
}
int
flashreadblock(uint32_t addr, uint32_t len, void *_data) {
    return spiflashreadblock(addr, len, _data);
}


int
flashwriteblock(uint32_t addr, uint32_t len, void *_data) {
    return spiflashwriteblock(addr, len, _data);
}