annotate flash.c @ 27:5c9d2e3d6591

Add flashread/writeblock commands which read/write a block of data to flash with a CRC.
author Daniel O'Connor <darius@dons.net.au>
date Tue, 20 Nov 2012 21:54:06 +1030
parents 74efdb21ae5d
children 03592ca4d37e
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
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
28 /* Holds all the settings needed */
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
29 typedef struct {
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
30 uint8_t fermenter_ROM[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
31 uint8_t fridge_ROM[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
32 uint8_t ambient_ROM[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
33 int16_t target_temp;
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
34 uint16_t hysteresis;
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
35 /* How much to under/overshoot on heating/cooling */
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
36 int16_t minheatovershoot;
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
37 int16_t mincoolovershoot;
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
38
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
39 /* Minimum time the cooler can be on/off */
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
40 int16_t mincoolontime;
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
41 int16_t mincoolofftime;
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
42
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
43 /* Minimum time the heater can be on/off */
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
44 int16_t minheatontime;
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
45 int16_t minheatofftime;
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
46
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
47 #define TC_MODE_AUTO 'a' /* Automatic control */
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
48 #define TC_MODE_HEAT 'h' /* Force heating */
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
49 #define TC_MODE_COOL 'c' /* Force cooling */
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
50 #define TC_MODE_IDLE 'i' /* Force idle */
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
51 #define TC_MODE_NOTHING 'n' /* Do nothing (like idle but log nothing) */
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
52 char mode;
25
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
53
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
54 /* Bit patterns for various modes */
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
55 uint8_t coolbits;
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
56 uint8_t heatbits;
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
57 uint8_t idlebits;
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
58
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
59 /* Check/stale times */
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
60 int16_t check_interval;
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
61 int16_t stale_factor;
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
62
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
63 /* Beep if stale */
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
64 int8_t dobeep;
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
65
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
66 /* Pad to 4 bytes */
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
67 uint8_t pad[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
68
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 } __attribute__((packed, aligned(4))) settings_t;
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
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 const settings_t default_settings = {
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 .fermenter_ROM = { 0x10, 0x4c, 0x7d, 0x53, 0x01, 0x08, 0x00, 0xff },
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 .fridge_ROM = { 0x10, 0x6f, 0x40, 0x53, 0x01, 0x08, 0x00, 0x16 },
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 .ambient_ROM = { 0x10, 0x76, 0x05, 0x53, 0x01, 0x08, 0x00, 0x8c },
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 .target_temp = 1000,
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 .hysteresis = 100,
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 .minheatovershoot = 50,
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 .mincoolovershoot = -50,
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 .mincoolontime = 300,
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 .mincoolofftime = 600,
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 .minheatontime = 60,
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 .minheatofftime = 60,
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 .mode = TC_MODE_AUTO,
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 .coolbits = 1 << 6,
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 .heatbits = 1<< 7,
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 .idlebits = 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
87 .check_interval = 10,
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 .stale_factor = 3,
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
89 .dobeep = 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
90 };
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
91
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
92 /* RAM copy of setting */
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
93 static settings_t ram_settings;
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
94
25
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
95 static int writestate = RW_IDLE;
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
96 static int readstate = RW_IDLE;
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
97
21
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
98 void
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
99 flashcmd(char **argv, int argc) {
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
100 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
101 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
102
21
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
103 if (argc == 0) {
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
104 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
105 return;
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 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
109 status = flashreadstatus();
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
110 fputs("Status = ", stdout);
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
111 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
112 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
113 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
114 fputs(" ", stdout);
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
115 }
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
116 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
117 } 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
118 if (argc != 2) {
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
119 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
120 return;
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
121 }
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
122 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
123 flashwritestatus(tmp);
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
124 status = flashreadstatus();
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
125 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
126 } 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
127 if (argc != 2) {
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
128 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
129 return;
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
130 }
26
74efdb21ae5d Use a 32 bit var to hold address from atoi().
Daniel O'Connor <darius@dons.net.au>
parents: 25
diff changeset
131 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
132 flash4kerase(addr);
74efdb21ae5d Use a 32 bit var to hold address from atoi().
Daniel O'Connor <darius@dons.net.au>
parents: 25
diff changeset
133 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
134 } 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
135 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
136 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
137 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
138 }
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
139
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
140 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
141
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
142 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
143 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
144 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
145 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
146
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
147 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
148
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
149 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
150 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
151 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
152
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
153 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
154 } 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
155 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
156 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
157 return;
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
26
74efdb21ae5d Use a 32 bit var to hold address from atoi().
Daniel O'Connor <darius@dons.net.au>
parents: 25
diff changeset
160 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
161
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
162 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
163 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
164 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
165 len = 16;
21
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
166
25
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
167 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
168 uint16_t data;
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
169 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
170 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
171
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
172 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
173 flashstartwrite(addr, data);
25
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
174 else
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
175 flashwriteword(data);
21
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
176 }
25
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
177 flashstopwrite();
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
178 } else if (!strcmp(argv[0], "tw")) {
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
179 /* Copy default to RAM */
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
180 bcopy(&default_settings, &ram_settings, sizeof(default_settings));
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
181
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
182 /* Write RAM copy into flash */
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
183 flashwriteblock(0, sizeof(ram_settings), &ram_settings);
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
184 } else if (!strcmp(argv[0], "tr")) {
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
185 int crcok;
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
186
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
187 /* Read flash copy to RAM */
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
188 crcok = flashreadblock(0, sizeof(ram_settings), &ram_settings);
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
189
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
190 printf("CRC is %s\r\n", crcok ? "OK" : "bad");
21
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
191 } 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
192 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
193 } else {
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
194 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
195 return;
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
196 }
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
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
199 void
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
200 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
201 flashenablewrite(); /* Enable writing */
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
202
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
203 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
204
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
205 SPI_WriteByte(FL_4KERASE); /* Send command */
25
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
206 SPI_WriteByte(addr >> 16); /* Send address */
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
207 SPI_WriteByte(addr >> 8);
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
208 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
209
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
210 FL_DESELECT();
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
211
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
212 //fputs("4k erase ", stdout);
25
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
213 flashwait();
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
214 }
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
215
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
216 void
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
217 flashwait(void) {
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
218 uint8_t cnt;
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
219
21
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
220 /* 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
221 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
222 ;
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
223
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
224 //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
225 }
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
226
8
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
227 uint16_t
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
228 flashreadid(void) {
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
229 uint8_t fac, dev;
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
230
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
231 FL_SELECT(); /* Select device */
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
232
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
233 SPI_WriteByte(FL_RDID); /* Send command */
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
234 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
235 SPI_WriteByte(0x00);
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
236 SPI_WriteByte(0x00);
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
237 fac = SPI_WriteByte(0x00); /* Read ID */
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
238 dev = SPI_WriteByte(0x00);
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
239
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
240 FL_DESELECT(); /* De-select device */
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
241
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
242 return fac << 8 | dev;
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
243 }
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
244
21
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
245 void
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
246 flashenablewrite(void) {
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
247 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
248
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
249 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
250
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
251 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
252 }
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
253
8
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
254 uint8_t
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
255 flashreadstatus(void) {
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
256 uint8_t status;
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
257
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
258 FL_SELECT(); /* Select device */
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
259
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
260 SPI_WriteByte(FL_RDSR); /* Send command */
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
261 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
262 status = SPI_WriteByte(0x00); /* Read status */
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
263
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
264 FL_DESELECT(); /* De-select device */
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
265
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
266 return status;
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
267 }
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
268
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
269 void
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
270 flashwritestatus(uint8_t status) {
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
271 /* Enable status write */
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
272 FL_SELECT(); /* Select device */
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
273 SPI_WriteByte(FL_EWSR); /* Send command */
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
274 SPI_WriteByte(0x00); /* Send data byte */
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
275 FL_DESELECT();
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
276
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
277 /* Actually write status */
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
278 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
279 SPI_WriteByte(FL_WRSR); /* Send command */
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
280 SPI_WriteByte(status); /* Send data byte */
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
281 FL_DESELECT(); /* De-select device */
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
282 }
58d76cf522ff Split out code into separate files.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
283
21
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
284 uint8_t
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
285 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
286 uint8_t data;
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
287
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
288 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
289
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
290 SPI_WriteByte(FL_READ); /* Send command */
25
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
291 SPI_WriteByte(addr >> 16); /* Send address */
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
292 SPI_WriteByte(addr >> 8);
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
293 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
294 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
295
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
296 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
297
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
298 return data;
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
299 }
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
300
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
301 void
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
302 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
303 flashwait();
21
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
304 flashenablewrite(); /* Enable writes */
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
305
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
306 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
307
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
308 SPI_WriteByte(FL_BYTEPROG); /* Send command */
25
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
309 SPI_WriteByte(addr >> 16); /* Send address */
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
310 SPI_WriteByte(addr >> 8);
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
311 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
312 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
313
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
314 FL_DESELECT(); /* De-select device */
25
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
315
21
bd8e2cf04034 - Add flash erase, write & read commands (needs more work).
Daniel O'Connor <darius@dons.net.au>
parents: 8
diff changeset
316 }
25
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 /*
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
319 * fStream reading looks like so
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 */
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 void
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
324 flashstartread(uint32_t addr) {
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
325 assert(readstate == RW_IDLE);
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
326
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
327 FL_SELECT(); /* Select device */
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
328
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
329 SPI_WriteByte(FL_READ); /* Send command */
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
330 SPI_WriteByte(addr >> 16); /* Send address */
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
331 SPI_WriteByte(addr >> 8);
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
332 SPI_WriteByte(addr);
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
333
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
334 readstate = RW_RUNNING;
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
335 }
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 uint8_t
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
338 flashreadbyte(void) {
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
339 assert(readstate == RW_RUNNING);
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
340 return SPI_WriteByte(0x00); /* Read data */
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
341 }
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
342
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
343 void
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
344 flashstopread(void) {
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
345 assert(readstate == RW_RUNNING);
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
346
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
347 FL_DESELECT();
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
348
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
349 readstate = RW_IDLE;
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
350 }
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
351
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
352 /*
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
353 * Auto increment writing looks like so
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
354 *
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
355 * Enable writing CS, WREN, nCS
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
356 * 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
357 * 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
358 * ...
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
359 * Disable writing CS, WRDI, nCS
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
360 *
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
361 * 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
362 * 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
363 */
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
364 void
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
365 flashstartwrite(uint32_t addr, uint16_t data) {
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
366 assert(writestate == RW_IDLE);
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
367
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
368 flashenablewrite(); /* Enable writes */
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
369
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
370 FL_SELECT(); /* Select device */
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
371
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
372 SPI_WriteByte(FL_AAIWP); /* Send command */
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
373 SPI_WriteByte(addr >> 16);
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
374 SPI_WriteByte(addr >> 8);
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
375 SPI_WriteByte(addr & 0xff); /* Send address */
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
376
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
377 SPI_WriteByte(data & 0xff); /* Write LSB */
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
378 SPI_WriteByte(data >> 8); /* Write MSB */
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
379
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
380 FL_DESELECT();
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
381
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
382 writestate = RW_RUNNING;
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
383 }
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
384
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
385 void
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
386 flashwriteword(uint16_t data) {
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
387 assert(writestate == RW_RUNNING);
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
388
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
389 //fputs("write word ", stdout);
25
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
390 flashwait(); /* Wait until not busy */
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
391
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
392 FL_SELECT(); /* Select device */
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
393
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
394 SPI_WriteByte(FL_AAIWP); /* Send command */
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
395 SPI_WriteByte(data & 0xff); /* Write LSB */
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
396 SPI_WriteByte(data >> 8); /* Write MSB */
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
397
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
398 FL_DESELECT(); /* De-select device */
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
399 }
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
400
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
401 void
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
402 flashstopwrite(void) {
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
403 assert(writestate == RW_RUNNING);
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
404
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
405 //fputs("flash stop write start ", stdout);
25
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
406 flashwait(); /* Wait until not busy */
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
407
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
408 FL_SELECT(); /* Select device */
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
409
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
410 SPI_WriteByte(FL_WRDI); /* Send command */
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
411
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
412 FL_DESELECT(); /* Deselect device */
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
413
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
414 //fputs("flash stop write end ", stdout);
25
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
415 flashwait(); /* Wait until not busy */
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
416
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
417 writestate = RW_IDLE;
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
418 }
a9cc07caa801 Add stream read/write commands.
Daniel O'Connor <darius@dons.net.au>
parents: 21
diff changeset
419
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
420 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
421 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
422 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
423 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
424
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
425 /* 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
426 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
427
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
428 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
429 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
430 *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
431 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
432 }
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
433
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
434 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
435 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
436 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
437 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
438
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
439 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
440
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
441 /* 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
442 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
443 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
444
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
445 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
446
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
447 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
448 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
449 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
450 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
451 }
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
452
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
453 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
454 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
455 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
456 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
457
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
458 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
459
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
460 /* 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
461 * - 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
462 * - 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
463 * - 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
464 */
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
465 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
466 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
467 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
468
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
469 /* 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
470 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
471
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
472 /* 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
473 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
474
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
475 /* 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
476 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
477 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
478 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
479 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
480 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
481 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
482 }
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
483
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
484 /* 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
485 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
486 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
487
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
488 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
489
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
490 /* 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
491 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
492 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
493
5c9d2e3d6591 Add flashread/writeblock commands which read/write a block of data to
Daniel O'Connor <darius@dons.net.au>
parents: 26
diff changeset
494 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
495 }