annotate flash.c @ 24:1e2fa7396f98

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