annotate flash.c @ 25:a9cc07caa801

Add stream read/write commands.
author Daniel O'Connor <darius@dons.net.au>
date Sat, 17 Nov 2012 12:18:24 +1030
parents bd8e2cf04034
children 74efdb21ae5d
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>
25
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
5 #include <assert.h>
8
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
6
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
7 #include "stm32f10x.h"
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
8 #include "spi.h"
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
9 #include "flash.h"
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
10
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
11 #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
12 #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
13
21
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
14 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
15 "BUSY",
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
16 "WEL",
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
17 "BP0",
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
18 "BP1",
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
19 "BP2",
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
20 "BP3",
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
21 "AAI",
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
22 "BPL"
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
23 };
25
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
24
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
25 #define RW_IDLE 0
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
26 #define RW_RUNNING 1
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
27 #define RW_IDLE 0
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
28
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
29 static int writestate = RW_IDLE;
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
30 static int readstate = RW_IDLE;
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
31
21
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
32 void
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
33 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
34 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
35
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
36 if (argc == 0) {
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
37 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
38 return;
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
39 }
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 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
42 status = flashreadstatus();
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
43 fputs("Status = ", stdout);
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
44 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
45 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
46 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
47 fputs(" ", stdout);
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
48 }
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
49 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
50 } 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
51 if (argc != 2) {
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
52 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
53 return;
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
54 }
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
55 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
56 flashwritestatus(tmp);
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
57 status = flashreadstatus();
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
58 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
59 } 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
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 flash4kerase(tmp);
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
66 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
67 } 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
68 if (argc != 2) {
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
69 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
70 return;
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
71 }
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
72 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
73
25
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
74 flashstartread(tmp);
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
75
21
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
76 for (int i = 0; i < 16; i++)
25
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
77 printf("Read 0x%02x from 0x%06x\r\n", flashreadbyte(), tmp + i);
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
78 flashstopread();
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
79
21
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
80 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
81 } 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
82 if (argc != 2) {
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
83 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
84 return;
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
85 }
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
86
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
87 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
88
25
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
89 for (int i = 0; i < 16; i += 2) {
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
90 uint16_t data;
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
91 data = ((i + 1) << 8) | i;
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
92 printf("Writing 0x%04x to 0x%06x\r\n", data, tmp + i);
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
93
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
94 if (i == 0)
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
95 flashstartwrite(tmp, data);
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
96 else
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
97 flashwriteword(data);
21
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
98 }
25
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
99 flashstopwrite();
21
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
100 } 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
101 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
102 } else {
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
103 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
104 return;
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
105 }
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
106 }
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 void
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
109 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
110 flashenablewrite(); /* Enable writing */
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
111
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
112 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
113
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
114 SPI_WriteByte(FL_4KERASE); /* Send command */
25
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
115 SPI_WriteByte(addr >> 16); /* Send address */
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
116 SPI_WriteByte(addr >> 8);
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
117 SPI_WriteByte(addr);
21
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
118
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
119 FL_DESELECT();
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
120
25
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
121 flashwait();
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
122 }
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
123
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
124 void
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
125 flashwait(void) {
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
126 uint8_t cnt;
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
127
21
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
128 /* 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
129 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
130 ;
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
131
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
132 //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
133 }
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
134
8
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
135 uint16_t
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
136 flashreadid(void) {
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
137 uint8_t fac, dev;
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
138
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
139 FL_SELECT(); /* Select device */
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
140
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
141 SPI_WriteByte(FL_RDID); /* Send command */
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
142 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
143 SPI_WriteByte(0x00);
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
144 SPI_WriteByte(0x00);
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
145 fac = SPI_WriteByte(0x00); /* Read ID */
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
146 dev = SPI_WriteByte(0x00);
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
147
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
148 FL_DESELECT(); /* De-select device */
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
149
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
150 return fac << 8 | dev;
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
151 }
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
152
21
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
153 void
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
154 flashenablewrite(void) {
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
155 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
156
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
157 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
158
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
159 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
160 }
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
161
8
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
162 uint8_t
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
163 flashreadstatus(void) {
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
164 uint8_t status;
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
165
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
166 FL_SELECT(); /* 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 SPI_WriteByte(FL_RDSR); /* Send command */
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
169 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
170 status = SPI_WriteByte(0x00); /* Read status */
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
171
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
172 FL_DESELECT(); /* De-select device */
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
173
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
174 return status;
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
175 }
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
176
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
177 void
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
178 flashwritestatus(uint8_t status) {
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
179 /* Enable status write */
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
180 FL_SELECT(); /* Select device */
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
181 SPI_WriteByte(FL_EWSR); /* Send command */
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
182 SPI_WriteByte(0x00); /* Send data byte */
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
183 FL_DESELECT();
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
184
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
185 /* Actually write status */
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
186 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
187 SPI_WriteByte(FL_WRSR); /* Send command */
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
188 SPI_WriteByte(status); /* Send data byte */
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
189 FL_DESELECT(); /* De-select device */
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
190 }
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
191
21
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
192 uint8_t
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
193 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
194 uint8_t data;
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
195
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
196 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
197
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
198 SPI_WriteByte(FL_READ); /* Send command */
25
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
199 SPI_WriteByte(addr >> 16); /* Send address */
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
200 SPI_WriteByte(addr >> 8);
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
201 SPI_WriteByte(addr);
21
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
202 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
203
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
204 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
205
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
206 return data;
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
207 }
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
208
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
209 void
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
210 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
211 flashenablewrite(); /* Enable writes */
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
212
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
213 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
214
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
215 SPI_WriteByte(FL_BYTEPROG); /* Send command */
25
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
216 SPI_WriteByte(addr >> 16); /* Send address */
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
217 SPI_WriteByte(addr >> 8);
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
218 SPI_WriteByte(addr);
21
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
219 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
220
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
221 FL_DESELECT(); /* De-select device */
25
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
222
21
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
223 }
25
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
224
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
225 /*
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
226 * fStream reading looks like so
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
227 *
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
228 */
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
229
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
230 void
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
231 flashstartread(uint32_t addr) {
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
232 assert(readstate == RW_IDLE);
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
233
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
234 FL_SELECT(); /* Select device */
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
235
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
236 SPI_WriteByte(FL_READ); /* Send command */
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
237 SPI_WriteByte(addr >> 16); /* Send address */
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
238 SPI_WriteByte(addr >> 8);
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
239 SPI_WriteByte(addr);
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
240
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
241 readstate = RW_RUNNING;
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
242 }
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
243
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
244 uint8_t
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
245 flashreadbyte(void) {
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
246 assert(readstate == RW_RUNNING);
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
247 return SPI_WriteByte(0x00); /* Read data */
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
248 }
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
249
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
250 void
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
251 flashstopread(void) {
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
252 assert(readstate == RW_RUNNING);
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
253
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
254 FL_DESELECT();
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
255
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
256 readstate = RW_IDLE;
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
257 }
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
258
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
259 /*
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
260 * Auto increment writing looks like so
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
261 *
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
262 * Enable writing CS, WREN, nCS
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
263 * Send start address & first data word CS, AAI + addr + data, nCS
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
264 * Send subsequent words wait for nBUSY, CS, AAI + data, nCS
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
265 * ...
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
266 * Disable writing CS, WRDI, nCS
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
267 *
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
268 * XXX: EBSY command links SO to flash busy state, I don't think the
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
269 * STM32 could sample it without switching out of SPI mode.
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
270 */
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
271 void
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
272 flashstartwrite(uint32_t addr, uint16_t data) {
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
273 assert(writestate == RW_IDLE);
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
274
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
275 flashenablewrite(); /* Enable writes */
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
276
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
277 FL_SELECT(); /* Select device */
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
278
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
279 SPI_WriteByte(FL_AAIWP); /* Send command */
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
280 SPI_WriteByte(addr >> 16);
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
281 SPI_WriteByte(addr >> 8);
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
282 SPI_WriteByte(addr & 0xff); /* Send address */
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
283
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
284 SPI_WriteByte(data & 0xff); /* Write LSB */
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
285 SPI_WriteByte(data >> 8); /* Write MSB */
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
286
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
287 FL_DESELECT();
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
288
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
289 writestate = RW_RUNNING;
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
290 }
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
291
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
292 void
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
293 flashwriteword(uint16_t data) {
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
294 assert(writestate == RW_RUNNING);
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
295
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
296 flashwait(); /* Wait until not busy */
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
297
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
298 FL_SELECT(); /* Select device */
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
299
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
300 SPI_WriteByte(FL_AAIWP); /* Send command */
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
301 SPI_WriteByte(data & 0xff); /* Write LSB */
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
302 SPI_WriteByte(data >> 8); /* Write MSB */
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
303
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
304 FL_DESELECT(); /* De-select device */
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
305 }
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
306
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
307 void
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
308 flashstopwrite(void) {
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
309 assert(writestate == RW_RUNNING);
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
310
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
311 flashwait(); /* Wait until not busy */
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
312
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
313 FL_SELECT(); /* Select device */
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
314
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
315 SPI_WriteByte(FL_WRDI); /* Send command */
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
316
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
317 FL_DESELECT(); /* Deselect device */
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
318
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
319 flashwait(); /* Wait until not busy */
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
320
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
321 writestate = RW_IDLE;
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
322 }
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
323