comparison flash.c @ 21:bd8e2cf04034

- Add flash erase, write & read commands (needs more work). - Split the buffer into argv/argc to make sub commands simpler.
author Daniel O'Connor <darius@dons.net.au>
date Thu, 15 Nov 2012 23:40:51 +1030
parents 58d76cf522ff
children a9cc07caa801
comparison
equal deleted inserted replaced
20:35cf31794a42 21:bd8e2cf04034
1 #include <stdio.h>
1 #include <stdint.h> 2 #include <stdint.h>
3 #include <string.h>
4 #include <stdlib.h>
2 5
3 #include "stm32f10x.h" 6 #include "stm32f10x.h"
4 #include "spi.h" 7 #include "spi.h"
5 #include "flash.h" 8 #include "flash.h"
6 9
7 #define FL_SELECT() GPIO_ResetBits(GPIOA, GPIO_Pin_4) 10 #define FL_SELECT() GPIO_ResetBits(GPIOA, GPIO_Pin_4)
8 #define FL_DESELECT() GPIO_SetBits(GPIOA, GPIO_Pin_4) 11 #define FL_DESELECT() GPIO_SetBits(GPIOA, GPIO_Pin_4)
12
13 static const char *flstattbl[] = {
14 "BUSY",
15 "WEL",
16 "BP0",
17 "BP1",
18 "BP2",
19 "BP3",
20 "AAI",
21 "BPL"
22 };
23
24 void
25 flashcmd(char **argv, int argc) {
26 uint8_t status, tmp;
27
28 if (argc == 0) {
29 fputs("No command specified\r\n", stdout);
30 return;
31 }
32
33 if (!strcmp(argv[0], "str")) {
34 status = flashreadstatus();
35 fputs("Status = ", stdout);
36 for (unsigned int i = 0; i < sizeof(flstattbl) / sizeof(flstattbl[0]); i++)
37 if (status & 1 << i) {
38 fputs(flstattbl[i], stdout);
39 fputs(" ", stdout);
40 }
41 printf("(0x%02x)\r\n", status);
42 } else if (!strcmp(argv[0], "stw")) {
43 if (argc != 2) {
44 fputs("Incorrect number of arguments\r\n", stdout);
45 return;
46 }
47 tmp = atoi(argv[1]);
48 flashwritestatus(tmp);
49 status = flashreadstatus();
50 printf("Wrote 0x%02x to status, now 0x%02x\r\n", tmp, status);
51 } else if (!strcmp(argv[0], "er")) {
52 if (argc != 2) {
53 fputs("Incorrect number of arguments\r\n", stdout);
54 return;
55 }
56 tmp = atoi(argv[1]);
57 flash4kerase(tmp);
58 printf("Erased 0x%x\r\n", tmp);
59 } else if (!strcmp(argv[0], "rd")) {
60 if (argc != 2) {
61 fputs("Incorrect number of arguments\r\n", stdout);
62 return;
63 }
64 tmp = atoi(argv[1]);
65
66 for (int i = 0; i < 16; i++)
67 printf("Read 0x%02x from 0x%06x\r\n", flashread(tmp + i), tmp + i);
68 fputs("\r\n", stdout);
69 } else if (!strcmp(argv[0], "wr")) {
70 if (argc != 2) {
71 fputs("Incorrect number of arguments\r\n", stdout);
72 return;
73 }
74
75 tmp = atoi(argv[1]);
76
77 for (int i = 0; i < 16; i++) {
78 printf("Writing 0x%02x to 0x%06x\r\n", tmp + i, i);
79 flashwrite(tmp + i, i);
80 }
81
82 } else if (!strcmp(argv[0], "id")) {
83 printf("Flash ID = 0x%04hx (expect 0xbf41)\r\n", flashreadid());
84 } else {
85 fputs("Unknown sub command\r\n", stdout);
86 return;
87 }
88 }
89
90 void
91 flash4kerase(uint32_t addr) {
92 uint8_t cnt;
93
94 flashenablewrite(); /* Enable writing */
95
96 FL_SELECT(); /* Select device */
97
98 SPI_WriteByte(FL_4KERASE); /* Send command */
99 SPI_WriteByte((addr & 0x00ff0000) >> 16);
100 SPI_WriteByte((addr & 0x0000ff00) >> 8);
101 SPI_WriteByte((addr & 0x000000ff)); /* Send address */
102
103 FL_DESELECT();
104
105 /* Wait for not BUSY */
106 for (cnt = 0; (flashreadstatus() & FL_BUSY) != 0; cnt++)
107 ;
108
109 //printf("cnt = %d\r\n", cnt);
110 }
9 111
10 uint16_t 112 uint16_t
11 flashreadid(void) { 113 flashreadid(void) {
12 uint8_t fac, dev; 114 uint8_t fac, dev;
13 115
21 dev = SPI_WriteByte(0x00); 123 dev = SPI_WriteByte(0x00);
22 124
23 FL_DESELECT(); /* De-select device */ 125 FL_DESELECT(); /* De-select device */
24 126
25 return fac << 8 | dev; 127 return fac << 8 | dev;
128 }
129
130 void
131 flashenablewrite(void) {
132 FL_SELECT(); /* Select device */
133
134 SPI_WriteByte(FL_WREN); /* Send command */
135
136 FL_DESELECT(); /* De-select device */
26 } 137 }
27 138
28 uint8_t 139 uint8_t
29 flashreadstatus(void) { 140 flashreadstatus(void) {
30 uint8_t status; 141 uint8_t status;
53 SPI_WriteByte(FL_WRSR); /* Send command */ 164 SPI_WriteByte(FL_WRSR); /* Send command */
54 SPI_WriteByte(status); /* Send data byte */ 165 SPI_WriteByte(status); /* Send data byte */
55 FL_DESELECT(); /* De-select device */ 166 FL_DESELECT(); /* De-select device */
56 } 167 }
57 168
169 uint8_t
170 flashread(uint32_t addr) {
171 uint8_t data;
172
173 FL_SELECT(); /* Select device */
174
175 SPI_WriteByte(FL_READ); /* Send command */
176 SPI_WriteByte((addr & 0x00ff0000) >> 16);
177 SPI_WriteByte((addr & 0x0000ff00) >> 8);
178 SPI_WriteByte((addr & 0x000000ff)); /* Send address */
179 data = SPI_WriteByte(0x00); /* Read data */
180
181 FL_DESELECT(); /* De-select device */
182
183 return data;
184 }
185
186 void
187 flashwrite(uint32_t addr, uint8_t data) {
188 flashenablewrite(); /* Enable writes */
189
190 FL_SELECT(); /* Select device */
191
192 SPI_WriteByte(FL_BYTEPROG); /* Send command */
193 SPI_WriteByte((addr & 0x00ff0000) >> 16);
194 SPI_WriteByte((addr & 0x0000ff00) >> 8);
195 SPI_WriteByte((addr & 0x000000ff)); /* Send address */
196 SPI_WriteByte(data); /* Write data */
197
198 FL_DESELECT(); /* De-select device */
199 }