view flash.c @ 85:18b154c447bb

Add note about 1-wire wiring
author Daniel O'Connor <darius@dons.net.au>
date Thu, 12 Mar 2015 23:22:11 +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);
}