Mercurial > ~darius > hgwebdir.cgi > stm32temp
diff flash.c @ 83:05ba84c7da97
Add a flash layer for compatibility (in future).
Fix SPI flash block writing.
White space fixes.
author | Daniel O'Connor <darius@dons.net.au> |
---|---|
date | Mon, 02 Mar 2015 14:32:08 +1030 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/flash.c Mon Mar 02 14:32:08 2015 +1030 @@ -0,0 +1,100 @@ +#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); +} +