annotate flash.c @ 44:f1cc171b06b5

Remove useless delay test. Add GPIO PE2 changes to make testing arbitary delays possible.
author Daniel O'Connor <darius@dons.net.au>
date Tue, 02 Apr 2013 10:54:20 +1030
parents 03592ca4d37e
children dcac5f08f87a
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
27
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
27
25
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
28 static int writestate = RW_IDLE;
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
29 static int readstate = RW_IDLE;
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
30
21
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
31 void
31
03592ca4d37e Port tempctrl.c from AVR. I removed the beep code as I don't have a
Daniel O'Connor <darius@dons.net.au>
parents: 27
diff changeset
32 flashcmd(int argc, char **argv) {
27
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
33 uint8_t status, tmp, len;
26
74efdb21ae5d Use a 32 bit var to hold address from atoi().
Daniel O'Connor <darius@dons.net.au>
parents: 25
diff changeset
34 uint32_t addr;
74efdb21ae5d Use a 32 bit var to hold address from atoi().
Daniel O'Connor <darius@dons.net.au>
parents: 25
diff changeset
35
21
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 }
26
74efdb21ae5d Use a 32 bit var to hold address from atoi().
Daniel O'Connor <darius@dons.net.au>
parents: 25
diff changeset
64 addr = atoi(argv[1]);
74efdb21ae5d Use a 32 bit var to hold address from atoi().
Daniel O'Connor <darius@dons.net.au>
parents: 25
diff changeset
65 flash4kerase(addr);
74efdb21ae5d Use a 32 bit var to hold address from atoi().
Daniel O'Connor <darius@dons.net.au>
parents: 25
diff changeset
66 printf("Erased 0x%x\r\n", (unsigned int)addr);
21
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")) {
27
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
68 if (argc < 2) {
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
69 fputs("Incorrect number of arguments\r\n", stdout);
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
70 return;
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
71 }
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
72
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
73 addr = atoi(argv[1]);
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
74
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
75 if (argc > 2)
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
76 len = atoi(argv[2]);
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
77 else
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
78 len = 16;
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
79
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
80 flashstartread(addr);
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
81
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
82 for (uint32_t i = 0; i < len; i++)
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
83 printf("Read 0x%02x from 0x%06x\r\n", flashreadbyte(), (unsigned int)(addr + i));
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
84 flashstopread();
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
85
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
86 fputs("\r\n", stdout);
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
87 } else if (!strcmp(argv[0], "wr")) {
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
88 if (argc < 2) {
21
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
89 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
90 return;
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
91 }
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
92
26
74efdb21ae5d Use a 32 bit var to hold address from atoi().
Daniel O'Connor <darius@dons.net.au>
parents: 25
diff changeset
93 addr = atoi(argv[1]);
74efdb21ae5d Use a 32 bit var to hold address from atoi().
Daniel O'Connor <darius@dons.net.au>
parents: 25
diff changeset
94
27
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
95 if (argc > 2)
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
96 len = atoi(argv[2]);
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
97 else
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
98 len = 16;
21
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
99
25
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
100 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
101 uint16_t data;
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
102 data = ((i + 1) << 8) | i;
26
74efdb21ae5d Use a 32 bit var to hold address from atoi().
Daniel O'Connor <darius@dons.net.au>
parents: 25
diff changeset
103 printf("Writing 0x%04x to 0x%06x\r\n", data, (unsigned int)(addr + i));
25
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
104
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
105 if (i == 0)
26
74efdb21ae5d Use a 32 bit var to hold address from atoi().
Daniel O'Connor <darius@dons.net.au>
parents: 25
diff changeset
106 flashstartwrite(addr, data);
25
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
107 else
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
108 flashwriteword(data);
21
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
109 }
25
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
110 flashstopwrite();
21
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
111 } 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
112 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
113 } else {
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
114 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
115 return;
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
116 }
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
117 }
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 void
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
120 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
121 flashenablewrite(); /* Enable writing */
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
122
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
123 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
124
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
125 SPI_WriteByte(FL_4KERASE); /* Send command */
25
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
126 SPI_WriteByte(addr >> 16); /* Send address */
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
127 SPI_WriteByte(addr >> 8);
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
128 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
129
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
130 FL_DESELECT();
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
131
27
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
132 //fputs("4k erase ", stdout);
25
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
133 flashwait();
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
134 }
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
135
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
136 void
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
137 flashwait(void) {
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
138 uint8_t cnt;
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
139
21
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
140 /* 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
141 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
142 ;
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
143
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
144 //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
145 }
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
146
8
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
147 uint16_t
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
148 flashreadid(void) {
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
149 uint8_t fac, dev;
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 FL_SELECT(); /* Select device */
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 SPI_WriteByte(FL_RDID); /* Send command */
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
154 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
155 SPI_WriteByte(0x00);
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
156 SPI_WriteByte(0x00);
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
157 fac = SPI_WriteByte(0x00); /* Read ID */
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
158 dev = SPI_WriteByte(0x00);
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
159
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
160 FL_DESELECT(); /* De-select device */
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 return fac << 8 | dev;
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
163 }
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
164
21
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
165 void
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
166 flashenablewrite(void) {
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
167 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
168
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
169 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
170
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
171 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
172 }
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
173
8
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
174 uint8_t
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
175 flashreadstatus(void) {
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
176 uint8_t status;
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
177
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
178 FL_SELECT(); /* Select device */
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
179
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
180 SPI_WriteByte(FL_RDSR); /* Send command */
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
181 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
182 status = SPI_WriteByte(0x00); /* Read status */
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
183
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
184 FL_DESELECT(); /* De-select device */
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
185
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
186 return status;
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
187 }
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
188
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
189 void
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
190 flashwritestatus(uint8_t status) {
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
191 /* Enable status write */
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
192 FL_SELECT(); /* Select device */
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
193 SPI_WriteByte(FL_EWSR); /* Send command */
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
194 SPI_WriteByte(0x00); /* Send data byte */
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
195 FL_DESELECT();
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
196
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
197 /* Actually write status */
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
198 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
199 SPI_WriteByte(FL_WRSR); /* Send command */
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
200 SPI_WriteByte(status); /* Send data byte */
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
201 FL_DESELECT(); /* De-select device */
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
202 }
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
203
21
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
204 uint8_t
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
205 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
206 uint8_t 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 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
209
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
210 SPI_WriteByte(FL_READ); /* Send command */
25
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
211 SPI_WriteByte(addr >> 16); /* Send address */
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
212 SPI_WriteByte(addr >> 8);
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
213 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
214 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
215
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
216 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
217
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
218 return data;
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
219 }
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 void
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
222 flashwrite(uint32_t addr, uint8_t data) {
27
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
223 flashwait();
21
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
224 flashenablewrite(); /* Enable writes */
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
225
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
226 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
227
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
228 SPI_WriteByte(FL_BYTEPROG); /* Send command */
25
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
229 SPI_WriteByte(addr >> 16); /* Send address */
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
230 SPI_WriteByte(addr >> 8);
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
231 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
232 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
233
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
234 FL_DESELECT(); /* De-select device */
25
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
235
21
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
236 }
25
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
237
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
238 /*
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
239 * fStream reading looks like so
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 */
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 void
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
244 flashstartread(uint32_t addr) {
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
245 assert(readstate == RW_IDLE);
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
246
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
247 FL_SELECT(); /* Select device */
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 SPI_WriteByte(FL_READ); /* Send command */
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
250 SPI_WriteByte(addr >> 16); /* Send address */
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
251 SPI_WriteByte(addr >> 8);
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
252 SPI_WriteByte(addr);
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 readstate = RW_RUNNING;
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
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
257 uint8_t
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
258 flashreadbyte(void) {
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
259 assert(readstate == RW_RUNNING);
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
260 return SPI_WriteByte(0x00); /* Read data */
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
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
263 void
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
264 flashstopread(void) {
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
265 assert(readstate == RW_RUNNING);
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
266
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
267 FL_DESELECT();
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
268
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
269 readstate = RW_IDLE;
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
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
272 /*
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
273 * Auto increment writing looks like so
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 * Enable writing CS, WREN, nCS
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
276 * 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
277 * 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
278 * ...
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
279 * Disable writing CS, WRDI, nCS
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
280 *
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
281 * 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
282 * 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
283 */
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
284 void
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
285 flashstartwrite(uint32_t addr, uint16_t data) {
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
286 assert(writestate == RW_IDLE);
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
287
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
288 flashenablewrite(); /* Enable writes */
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
289
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
290 FL_SELECT(); /* Select device */
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 SPI_WriteByte(FL_AAIWP); /* Send command */
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
293 SPI_WriteByte(addr >> 16);
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
294 SPI_WriteByte(addr >> 8);
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
295 SPI_WriteByte(addr & 0xff); /* Send address */
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
296
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
297 SPI_WriteByte(data & 0xff); /* Write LSB */
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
298 SPI_WriteByte(data >> 8); /* Write MSB */
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 FL_DESELECT();
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
301
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
302 writestate = RW_RUNNING;
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
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
305 void
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
306 flashwriteword(uint16_t data) {
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
307 assert(writestate == RW_RUNNING);
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
308
27
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
309 //fputs("write word ", stdout);
25
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
310 flashwait(); /* Wait until not busy */
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
311
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
312 FL_SELECT(); /* Select device */
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
313
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
314 SPI_WriteByte(FL_AAIWP); /* Send command */
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
315 SPI_WriteByte(data & 0xff); /* Write LSB */
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
316 SPI_WriteByte(data >> 8); /* Write MSB */
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
317
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
318 FL_DESELECT(); /* De-select device */
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
319 }
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 void
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
322 flashstopwrite(void) {
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
323 assert(writestate == RW_RUNNING);
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
324
27
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
325 //fputs("flash stop write start ", stdout);
25
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
326 flashwait(); /* Wait until not busy */
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
327
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
328 FL_SELECT(); /* Select device */
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
329
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
330 SPI_WriteByte(FL_WRDI); /* Send command */
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
331
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
332 FL_DESELECT(); /* Deselect device */
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
333
27
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
334 //fputs("flash stop write end ", stdout);
25
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
335 flashwait(); /* Wait until not busy */
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
336
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
337 writestate = RW_IDLE;
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
338 }
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
339
27
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
340 int
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
341 flashreadblock(uint32_t addr, uint32_t len, void *_data) {
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
342 uint8_t *data = _data;
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
343 uint32_t flashcrc, ramcrc;
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
344
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
345 /* Must be a multiple of 4 due to CRC check */
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
346 assert(len % 4 == 0);
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
347
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
348 flashstartread(addr);
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
349 for (int i = len; i > 0; i--) {
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
350 *data = flashreadbyte();
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
351 data++;
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
352 }
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
353
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
354 flashcrc = flashreadbyte();
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
355 flashcrc |= flashreadbyte() << 8;
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
356 flashcrc |= flashreadbyte() << 16;
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
357 flashcrc |= flashreadbyte() << 24;
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
358
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
359 flashstopread();
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
360
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
361 /* Calculate CRC */
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
362 CRC_ResetDR();
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
363 ramcrc = CRC_CalcBlockCRC((uint32_t *)_data, len / 4);
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
364
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
365 printf("RAM CRC 0x%08x Flash CRC 0x%08x\r\n", (uint)ramcrc, (uint)flashcrc);
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
366
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
367 if (ramcrc == flashcrc)
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
368 return 1;
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
369 else
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
370 return 0;
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
371 }
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
372
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
373 void
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
374 flashwriteblock(uint32_t addr, uint32_t len, void *_data) {
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
375 uint16_t *data = _data;
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
376 uint32_t crc;
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
377
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
378 printf("Writing %u bytes to 0x%06x\r\n", (uint)len, (uint)addr);
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
379
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
380 /* Ensure data is
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
381 * - 16 bit aligned
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
382 * - a multiple of 32 bits in length (for CRCs, the flash only need 16 bits)
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
383 * - not longer than a sector
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
384 */
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
385 assert(addr % 2 == 0);
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
386 assert(len % 4 == 0);
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
387 assert(len <= 4096);
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
388
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
389 /* Disable write protect */
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
390 flashwritestatus(0x00);
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
391
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
392 /* Erase sector */
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
393 flash4kerase(addr);
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
394
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
395 /* Write data */
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
396 for (uint i = 0; i < len / 2; i++) {
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
397 if (i == 0)
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
398 flashstartwrite(addr, *data);
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
399 else
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
400 flashwriteword(*data);
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
401 data++;
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
402 }
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
403
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
404 /* Calculate CRC */
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
405 CRC_ResetDR();
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
406 crc = CRC_CalcBlockCRC((uint32_t *)_data, len / 4);
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
407
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
408 printf("CRC is 0x%08x\r\n", (uint)crc);
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
409
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
410 /* Write CRC */
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
411 flashwriteword(crc);
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
412 flashwriteword(crc >> 16);
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
413
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
414 flashstopwrite();
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
415 }