node-red-contrib-periph-memory 1.2.1 → 1.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -16,8 +16,14 @@ npm install node-red-contrib-periph-memory
16
16
 
17
17
  | Node | Kind | Description |
18
18
  |------|------|-------------|
19
+ | `23x1024-device` | config | Bus configuration for a 23X1024 |
20
+ | `23x1024` | input | Reads, writes and fills a 23LC1024 / 23A1024 1-Mbit (128 KiB) SPI serial SRAM, and selects its operating mode. |
19
21
  | `24aa02uid-device` | config | I²C bus and address for a 24AA02UID |
20
22
  | `24aa02uid` | input | Reads the factory-programmed 32-bit unique serial number from a 24AA02UID EEPROM, or reads / writes user EEPROM bytes, over I²C. |
23
+ | `mx25l12845e-device` | config | Bus configuration for a MX25L12845E |
24
+ | `mx25l12845e` | input | Reads, writes and erases the 16 MiB Macronix MX25L12845E SPI NOR flash by byte address. Writes are erase-aware: the node erases a 4 KiB sector itself when bits must go from 0 to 1, and keeps the other bytes of that sector. There is no filesystem. |
25
+ | `sd-spi-device` | config | Bus configuration for a SD-SPI |
26
+ | `sd-spi` | input | Reads and writes raw 512-byte blocks of an SD / SDHC / SDXC / microSD card in SPI mode. There is no filesystem: blocks are addressed by number. |
21
27
 
22
28
  ## Links
23
29
 
@@ -0,0 +1,130 @@
1
+ [
2
+ {
3
+ "id": "tab-23x1024-demo",
4
+ "type": "tab",
5
+ "label": "23X1024 scratch buffer",
6
+ "disabled": false,
7
+ "info": "Demonstrates the 23X1024 serial SRAM as a fast scratch buffer. Every 2 seconds a function node writes a 256-byte ramp pattern at address 0, then reads it back and compares it, and a debug node shows whether the round trip matched. The data lives in volatile SRAM, so it does not survive a power cycle."
8
+ },
9
+ {
10
+ "id": "cfg-23x1024",
11
+ "type": "23x1024-device",
12
+ "name": "23X1024",
13
+ "bus": "0",
14
+ "device": "0",
15
+ "speed": "1000000"
16
+ },
17
+ {
18
+ "id": "inj-tick",
19
+ "type": "inject",
20
+ "name": "every 2 s",
21
+ "repeat": "2",
22
+ "crontab": "",
23
+ "once": true,
24
+ "onceDelay": 0.1,
25
+ "topic": "",
26
+ "payload": "",
27
+ "payloadType": "date",
28
+ "z": "tab-23x1024-demo",
29
+ "x": 130,
30
+ "y": 120,
31
+ "wires": [
32
+ [
33
+ "make-write"
34
+ ]
35
+ ]
36
+ },
37
+ {
38
+ "id": "make-write",
39
+ "type": "function",
40
+ "name": "Write ramp",
41
+ "func": "msg.op = 'write';\nmsg.address = 0;\nmsg.data = Array.from({ length: 256 }, (_, i) => i);\nreturn msg;",
42
+ "outputs": 1,
43
+ "noerr": 0,
44
+ "z": "tab-23x1024-demo",
45
+ "x": 300,
46
+ "y": 120,
47
+ "wires": [
48
+ [
49
+ "sram-write"
50
+ ]
51
+ ]
52
+ },
53
+ {
54
+ "id": "sram-write",
55
+ "type": "23x1024",
56
+ "name": "Write",
57
+ "device": "cfg-23x1024",
58
+ "z": "tab-23x1024-demo",
59
+ "x": 470,
60
+ "y": 120,
61
+ "wires": [
62
+ [
63
+ "make-read"
64
+ ]
65
+ ]
66
+ },
67
+ {
68
+ "id": "make-read",
69
+ "type": "function",
70
+ "name": "Read back",
71
+ "func": "msg.op = 'read';\nmsg.address = 0;\nmsg.length = 256;\nreturn msg;",
72
+ "outputs": 1,
73
+ "noerr": 0,
74
+ "z": "tab-23x1024-demo",
75
+ "x": 630,
76
+ "y": 120,
77
+ "wires": [
78
+ [
79
+ "sram-read"
80
+ ]
81
+ ]
82
+ },
83
+ {
84
+ "id": "sram-read",
85
+ "type": "23x1024",
86
+ "name": "Read",
87
+ "device": "cfg-23x1024",
88
+ "z": "tab-23x1024-demo",
89
+ "x": 790,
90
+ "y": 120,
91
+ "wires": [
92
+ [
93
+ "compare"
94
+ ]
95
+ ]
96
+ },
97
+ {
98
+ "id": "compare",
99
+ "type": "function",
100
+ "name": "Compare",
101
+ "func": "const d = msg.payload.data;\nmsg.payload = { match: d.length === 256 && d.every((v, i) => v === i) };\nreturn msg;",
102
+ "outputs": 1,
103
+ "noerr": 0,
104
+ "z": "tab-23x1024-demo",
105
+ "x": 940,
106
+ "y": 120,
107
+ "wires": [
108
+ [
109
+ "dbg-match"
110
+ ]
111
+ ]
112
+ },
113
+ {
114
+ "id": "dbg-match",
115
+ "type": "debug",
116
+ "name": "Round trip",
117
+ "active": true,
118
+ "tosidebar": true,
119
+ "console": false,
120
+ "tostatus": true,
121
+ "complete": "payload",
122
+ "targetType": "msg",
123
+ "statusVal": "payload.match",
124
+ "statusType": "auto",
125
+ "z": "tab-23x1024-demo",
126
+ "x": 1110,
127
+ "y": 120,
128
+ "wires": []
129
+ }
130
+ ]
@@ -0,0 +1,144 @@
1
+ [
2
+ {
3
+ "id": "tab-mx25-demo",
4
+ "type": "tab",
5
+ "label": "MX25L12845E flash reader",
6
+ "disabled": false,
7
+ "info": "Reads the JEDEC ID and the first 16 bytes of an MX25L12845E every 5 seconds. Read-only: nothing is written to the chip."
8
+ },
9
+ {
10
+ "id": "cfg-mx25",
11
+ "type": "mx25l12845e-device",
12
+ "name": "MX25L12845E",
13
+ "bus": "0",
14
+ "device": "0",
15
+ "speed": "8000000"
16
+ },
17
+ {
18
+ "id": "inj-tick",
19
+ "type": "inject",
20
+ "name": "every 5 s",
21
+ "repeat": "5",
22
+ "crontab": "",
23
+ "once": true,
24
+ "onceDelay": 0.1,
25
+ "topic": "",
26
+ "payload": "",
27
+ "payloadType": "date",
28
+ "z": "tab-mx25-demo",
29
+ "x": 130,
30
+ "y": 120,
31
+ "wires": [
32
+ [
33
+ "make-id"
34
+ ]
35
+ ]
36
+ },
37
+ {
38
+ "id": "make-id",
39
+ "type": "function",
40
+ "name": "Read ID",
41
+ "func": "msg.op = 'id';\nreturn msg;",
42
+ "outputs": 1,
43
+ "noerr": 0,
44
+ "z": "tab-mx25-demo",
45
+ "x": 290,
46
+ "y": 120,
47
+ "wires": [
48
+ [
49
+ "mx-id"
50
+ ]
51
+ ]
52
+ },
53
+ {
54
+ "id": "mx-id",
55
+ "type": "mx25l12845e",
56
+ "name": "ID",
57
+ "device": "cfg-mx25",
58
+ "z": "tab-mx25-demo",
59
+ "x": 430,
60
+ "y": 120,
61
+ "wires": [
62
+ [
63
+ "dbg-id",
64
+ "make-read"
65
+ ]
66
+ ]
67
+ },
68
+ {
69
+ "id": "dbg-id",
70
+ "type": "debug",
71
+ "name": "JEDEC ID",
72
+ "active": true,
73
+ "tosidebar": true,
74
+ "console": false,
75
+ "tostatus": false,
76
+ "complete": "payload",
77
+ "targetType": "msg",
78
+ "z": "tab-mx25-demo",
79
+ "x": 620,
80
+ "y": 80,
81
+ "wires": []
82
+ },
83
+ {
84
+ "id": "make-read",
85
+ "type": "function",
86
+ "name": "Read 16 bytes",
87
+ "func": "msg.op = 'read';\nmsg.address = 0;\nmsg.length = 16;\nreturn msg;",
88
+ "outputs": 1,
89
+ "noerr": 0,
90
+ "z": "tab-mx25-demo",
91
+ "x": 420,
92
+ "y": 180,
93
+ "wires": [
94
+ [
95
+ "mx-read"
96
+ ]
97
+ ]
98
+ },
99
+ {
100
+ "id": "mx-read",
101
+ "type": "mx25l12845e",
102
+ "name": "Read",
103
+ "device": "cfg-mx25",
104
+ "z": "tab-mx25-demo",
105
+ "x": 580,
106
+ "y": 180,
107
+ "wires": [
108
+ [
109
+ "hex"
110
+ ]
111
+ ]
112
+ },
113
+ {
114
+ "id": "hex",
115
+ "type": "function",
116
+ "name": "As hex",
117
+ "func": "msg.payload = msg.payload.toString('hex');\nreturn msg;",
118
+ "outputs": 1,
119
+ "noerr": 0,
120
+ "z": "tab-mx25-demo",
121
+ "x": 730,
122
+ "y": 180,
123
+ "wires": [
124
+ [
125
+ "dbg-read"
126
+ ]
127
+ ]
128
+ },
129
+ {
130
+ "id": "dbg-read",
131
+ "type": "debug",
132
+ "name": "first 16 bytes",
133
+ "active": true,
134
+ "tosidebar": true,
135
+ "console": false,
136
+ "tostatus": false,
137
+ "complete": "payload",
138
+ "targetType": "msg",
139
+ "z": "tab-mx25-demo",
140
+ "x": 900,
141
+ "y": 180,
142
+ "wires": []
143
+ }
144
+ ]
@@ -0,0 +1,100 @@
1
+ [
2
+ {
3
+ "id": "tab-sd-spi-demo",
4
+ "type": "tab",
5
+ "label": "SD card block reader",
6
+ "disabled": false,
7
+ "info": "Reads block 0 of an SD card every 5 seconds and shows the boot signature (0x55AA on a partitioned card) and the card info. Read-only: nothing is written to the card."
8
+ },
9
+ {
10
+ "id": "cfg-sd-spi",
11
+ "type": "sd-spi-device",
12
+ "name": "SD card",
13
+ "bus": "0",
14
+ "device": "0",
15
+ "gpiochip": "/dev/gpiochip0",
16
+ "csLine": "25",
17
+ "speed": "12000000"
18
+ },
19
+ {
20
+ "id": "inj-tick",
21
+ "type": "inject",
22
+ "name": "every 5 s",
23
+ "repeat": "5",
24
+ "crontab": "",
25
+ "once": true,
26
+ "onceDelay": 0.1,
27
+ "topic": "",
28
+ "payload": "",
29
+ "payloadType": "date",
30
+ "z": "tab-sd-spi-demo",
31
+ "x": 130,
32
+ "y": 120,
33
+ "wires": [
34
+ [
35
+ "make-read"
36
+ ]
37
+ ]
38
+ },
39
+ {
40
+ "id": "make-read",
41
+ "type": "function",
42
+ "name": "Read block 0",
43
+ "func": "msg.op = 'read';\nmsg.block = 0;\nmsg.count = 1;\nreturn msg;",
44
+ "outputs": 1,
45
+ "noerr": 0,
46
+ "z": "tab-sd-spi-demo",
47
+ "x": 300,
48
+ "y": 120,
49
+ "wires": [
50
+ [
51
+ "sd-read"
52
+ ]
53
+ ]
54
+ },
55
+ {
56
+ "id": "sd-read",
57
+ "type": "sd-spi",
58
+ "name": "Read",
59
+ "device": "cfg-sd-spi",
60
+ "z": "tab-sd-spi-demo",
61
+ "x": 460,
62
+ "y": 120,
63
+ "wires": [
64
+ [
65
+ "summarise"
66
+ ]
67
+ ]
68
+ },
69
+ {
70
+ "id": "summarise",
71
+ "type": "function",
72
+ "name": "Boot signature",
73
+ "func": "const b = msg.payload;\nmsg.payload = {\n bootSignature: b[510].toString(16).padStart(2, '0') + b[511].toString(16).padStart(2, '0'),\n first16: Array.from(b.subarray(0, 16))\n};\nreturn msg;",
74
+ "outputs": 1,
75
+ "noerr": 0,
76
+ "z": "tab-sd-spi-demo",
77
+ "x": 640,
78
+ "y": 120,
79
+ "wires": [
80
+ [
81
+ "dbg"
82
+ ]
83
+ ]
84
+ },
85
+ {
86
+ "id": "dbg",
87
+ "type": "debug",
88
+ "name": "block 0",
89
+ "active": true,
90
+ "tosidebar": true,
91
+ "console": false,
92
+ "tostatus": false,
93
+ "complete": "payload",
94
+ "targetType": "msg",
95
+ "z": "tab-sd-spi-demo",
96
+ "x": 820,
97
+ "y": 120,
98
+ "wires": []
99
+ }
100
+ ]
@@ -0,0 +1,103 @@
1
+ <!-- Config node: one instance per physical device -->
2
+ <script type="text/javascript">
3
+ RED.nodes.registerType('23x1024-device', {
4
+ category: 'config',
5
+ defaults: {
6
+ name: { value: '' },
7
+ bus: { value: '0', required: true, validate: RED.validators.number() },
8
+ device: { value: '0', required: true, validate: RED.validators.number() },
9
+ speed: { value: '1000000', required: true, validate: RED.validators.number() }
10
+ },
11
+ label: function() {
12
+ return this.name || ('23X1024 @ spidev' + this.bus + '.' + this.device);
13
+ }
14
+ });
15
+ </script>
16
+
17
+ <script type="text/html" data-template-name="23x1024-device">
18
+ <div class="form-row">
19
+ <label for="node-config-input-name"><i class="fa fa-tag"></i> Name</label>
20
+ <input type="text" id="node-config-input-name" placeholder="23X1024">
21
+ </div>
22
+ <div class="form-row">
23
+ <label for="node-config-input-bus"><i class="fa fa-microchip"></i> SPI bus</label>
24
+ <input type="number" id="node-config-input-bus" min="0" style="width:80px"> &nbsp;(Linux /dev/spidevB.D)
25
+ </div>
26
+ <div class="form-row">
27
+ <label for="node-config-input-device">Chip select</label>
28
+ <input type="number" id="node-config-input-device" min="0" style="width:80px"> &nbsp;device number D
29
+ </div>
30
+ <div class="form-row">
31
+ <label for="node-config-input-speed">Clock (Hz)</label>
32
+ <input type="number" id="node-config-input-speed" min="1000" max="20000000" style="width:120px"> &nbsp;default: 1000000 (max 20 MHz)
33
+ </div>
34
+ </script>
35
+
36
+ <!-- Operation node: trigger → read / write / fill / mode -->
37
+ <script type="text/javascript">
38
+ RED.nodes.registerType('23x1024', {
39
+ category: 'Peripheral',
40
+ color: '#C084FC',
41
+ defaults: {
42
+ name: { value: '' },
43
+ device: { value: '', type: '23x1024-device', required: true }
44
+ },
45
+ inputs: 1,
46
+ outputs: 1,
47
+ icon: 'font-awesome/fa-database',
48
+ label: function() { return this.name || '23X1024'; },
49
+ paletteLabel: '23X1024'
50
+ });
51
+ </script>
52
+
53
+ <script type="text/html" data-template-name="23x1024">
54
+ <div class="form-row">
55
+ <label for="node-input-device"><i class="fa fa-microchip"></i> Device</label>
56
+ <input type="text" id="node-input-device">
57
+ </div>
58
+ <div class="form-row">
59
+ <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
60
+ <input type="text" id="node-input-name" placeholder="23X1024">
61
+ </div>
62
+ </script>
63
+
64
+ <script type="text/html" data-help-name="23x1024">
65
+ <p>Reads, writes and fills a 23LC1024 / 23A1024 1-Mbit (128 KiB) SPI serial SRAM,
66
+ and selects its operating mode.</p>
67
+ <p>Any input message triggers an operation. The device is configured via a
68
+ <b>23x1024-device</b> config node. The SRAM is volatile: its contents are lost
69
+ when power is removed.</p>
70
+
71
+ <h3>Inputs</h3>
72
+ <dl class="message-properties">
73
+ <dt>op <span class="property-type">string</span></dt>
74
+ <dd>Operation: <code>read</code> (default), <code>write</code>, <code>fill</code> or
75
+ <code>mode</code>. May be passed in <code>msg.op</code>.</dd>
76
+ <dt>address <span class="property-type">number</span></dt>
77
+ <dd>Start address 0–131071 for read / write / fill.</dd>
78
+ <dt>length <span class="property-type">number</span></dt>
79
+ <dd>Number of bytes (<code>read</code>, <code>fill</code>).</dd>
80
+ <dt>data <span class="property-type">array of numbers</span></dt>
81
+ <dd>Bytes to write (<code>write</code> only).</dd>
82
+ <dt>value <span class="property-type">number</span></dt>
83
+ <dd>Fill byte, default 0 (<code>fill</code> only).</dd>
84
+ <dt>mode <span class="property-type">string</span></dt>
85
+ <dd><code>byte</code>, <code>page</code> or <code>sequential</code> (<code>mode</code> only).
86
+ Omit it to just report the current mode.</dd>
87
+ </dl>
88
+
89
+ <h3>Outputs</h3>
90
+ <dl class="message-properties">
91
+ <dt>payload.data <span class="property-type">array of numbers</span></dt>
92
+ <dd>Bytes read from the chip. <code>read</code> only.</dd>
93
+ <dt>payload.ok <span class="property-type">boolean</span></dt>
94
+ <dd>True on a successful write or fill.</dd>
95
+ <dt>payload.mode <span class="property-type">string</span></dt>
96
+ <dd>Current operating mode. <code>mode</code> only.</dd>
97
+ </dl>
98
+
99
+ <h3>Configuration</h3>
100
+ <p>Set the SPI bus number, the chip-select (device) number and the clock in the
101
+ device config node. The SRAM runs in SPI Mode 0 at up to 20 MHz. Tie the chip's
102
+ HOLD pin high and do not leave SIO2 floating.</p>
103
+ </script>
@@ -0,0 +1,84 @@
1
+ "use strict";
2
+
3
+ module.exports = function(RED) {
4
+ const { SPIConnection } = require('periph/src/connection/spi');
5
+ const { SRAM23X1024Full } = require('periph/src/chips/memory/_23x1024');
6
+
7
+ const MODES = { byte: SRAM23X1024Full.MODE_BYTE, page: SRAM23X1024Full.MODE_PAGE, sequential: SRAM23X1024Full.MODE_SEQUENTIAL };
8
+ const MODE_NAMES = { [SRAM23X1024Full.MODE_BYTE]: 'byte', [SRAM23X1024Full.MODE_PAGE]: 'page', [SRAM23X1024Full.MODE_SEQUENTIAL]: 'sequential' };
9
+
10
+ function Sram23X1024DeviceNode(config) {
11
+ RED.nodes.createNode(this, config);
12
+ const node = this;
13
+ node.ready = null;
14
+ try {
15
+ const connection = new SPIConnection(parseInt(config.bus), parseInt(config.device), {
16
+ mode: 0,
17
+ maxSpeedHz: parseInt(config.speed) || 1000000,
18
+ });
19
+ node.driver = new SRAM23X1024Full(connection);
20
+ node.connection = connection;
21
+ node.ready = node.driver.init().catch(function(e) {
22
+ node.error('23X1024 init failed: ' + e.message);
23
+ node.driver = null;
24
+ });
25
+ } catch (e) {
26
+ node.error('23X1024 init failed: ' + e.message);
27
+ }
28
+ node.on('close', function() {
29
+ if (node.connection) node.connection.close();
30
+ });
31
+ }
32
+ RED.nodes.registerType('23x1024-device', Sram23X1024DeviceNode);
33
+
34
+ function Sram23X1024Node(config) {
35
+ RED.nodes.createNode(this, config);
36
+ const node = this;
37
+ node.device = RED.nodes.getNode(config.device);
38
+
39
+ node.on('input', async function(msg, send, done) {
40
+ if (node.device && node.device.ready) await node.device.ready;
41
+ if (!node.device || !node.device.driver) {
42
+ node.error('No 23X1024 device configured', msg);
43
+ done();
44
+ return;
45
+ }
46
+ try {
47
+ const d = node.device.driver;
48
+ const op = (msg.op || config.op || 'read');
49
+ if (op === 'read') {
50
+ const address = (msg.address != null) ? msg.address : 0;
51
+ const length = (msg.length != null) ? msg.length : 4;
52
+ const data = await d.read(address, length);
53
+ msg.payload = { data: Array.from(data) };
54
+ } else if (op === 'write') {
55
+ const address = (msg.address != null) ? msg.address : 0;
56
+ const data = (msg.data != null) ? msg.data : [];
57
+ await d.write(address, Buffer.from(data));
58
+ msg.payload = { ok: true };
59
+ } else if (op === 'fill') {
60
+ const address = (msg.address != null) ? msg.address : 0;
61
+ const length = (msg.length != null) ? msg.length : 0;
62
+ const value = (msg.value != null) ? msg.value : 0;
63
+ await d.fill(address, length, value);
64
+ msg.payload = { ok: true };
65
+ } else if (op === 'mode') {
66
+ if (msg.mode != null) {
67
+ if (!(msg.mode in MODES)) throw new Error('Unknown mode: ' + msg.mode);
68
+ await d.setMode(MODES[msg.mode]);
69
+ }
70
+ msg.payload = { mode: MODE_NAMES[await d.getMode()] };
71
+ } else {
72
+ node.error('Unknown op: ' + op, msg);
73
+ done();
74
+ return;
75
+ }
76
+ send(msg);
77
+ done();
78
+ } catch (e) {
79
+ done(e);
80
+ }
81
+ });
82
+ }
83
+ RED.nodes.registerType('23x1024', Sram23X1024Node);
84
+ };
@@ -0,0 +1,92 @@
1
+ <!-- Config node: one instance per physical chip -->
2
+ <script type="text/javascript">
3
+ RED.nodes.registerType('mx25l12845e-device', {
4
+ category: 'config',
5
+ defaults: {
6
+ name: { value: '' },
7
+ bus: { value: '0', required: true, validate: RED.validators.number() },
8
+ device: { value: '0', required: true, validate: RED.validators.number() },
9
+ speed: { value: '8000000', required: true, validate: RED.validators.number() }
10
+ },
11
+ label: function() {
12
+ return this.name || ('MX25L12845E @ spidev' + this.bus + '.' + this.device);
13
+ }
14
+ });
15
+ </script>
16
+
17
+ <script type="text/html" data-template-name="mx25l12845e-device">
18
+ <div class="form-row">
19
+ <label for="node-config-input-name"><i class="fa fa-tag"></i> Name</label>
20
+ <input type="text" id="node-config-input-name" placeholder="MX25L12845E">
21
+ </div>
22
+ <div class="form-row">
23
+ <label for="node-config-input-bus"><i class="fa fa-microchip"></i> SPI bus</label>
24
+ <input type="number" id="node-config-input-bus" min="0" style="width:80px"> &nbsp;(Linux /dev/spidevB.D)
25
+ </div>
26
+ <div class="form-row">
27
+ <label for="node-config-input-device">Device</label>
28
+ <input type="number" id="node-config-input-device" min="0" style="width:80px"> &nbsp;chip-select number D
29
+ </div>
30
+ <div class="form-row">
31
+ <label for="node-config-input-speed">Clock (Hz)</label>
32
+ <input type="number" id="node-config-input-speed" min="100000" max="50000000" style="width:120px"> &nbsp;default 8000000
33
+ </div>
34
+ </script>
35
+
36
+ <!-- Operation node: trigger → id / status / info / read / write / erase -->
37
+ <script type="text/javascript">
38
+ RED.nodes.registerType('mx25l12845e', {
39
+ category: 'Peripheral',
40
+ color: '#C084FC',
41
+ defaults: {
42
+ name: { value: '' },
43
+ device: { value: '', type: 'mx25l12845e-device', required: true }
44
+ },
45
+ inputs: 1,
46
+ outputs: 1,
47
+ icon: 'font-awesome/fa-hdd-o',
48
+ label: function() { return this.name || 'MX25L12845E'; },
49
+ paletteLabel: 'MX25L12845E (NOR flash)'
50
+ });
51
+ </script>
52
+
53
+ <script type="text/html" data-template-name="mx25l12845e">
54
+ <div class="form-row">
55
+ <label for="node-input-device"><i class="fa fa-microchip"></i> Device</label>
56
+ <input type="text" id="node-input-device">
57
+ </div>
58
+ <div class="form-row">
59
+ <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
60
+ <input type="text" id="node-input-name" placeholder="MX25L12845E">
61
+ </div>
62
+ </script>
63
+
64
+ <script type="text/html" data-help-name="mx25l12845e">
65
+ <p>Reads, writes and erases the 16 MiB Macronix MX25L12845E SPI NOR flash by byte address.
66
+ Writes are erase-aware: the node erases a 4 KiB sector itself when bits must go from 0 to 1,
67
+ and keeps the other bytes of that sector. There is no filesystem.</p>
68
+ <p>Any input message triggers an operation. The chip is configured via an
69
+ <b>mx25l12845e-device</b> config node. Flash wears out after about 100 000 erase cycles per sector.</p>
70
+
71
+ <h3>Inputs</h3>
72
+ <dl class="message-properties">
73
+ <dt>op <span class="property-type">string</span></dt>
74
+ <dd>Operation: <code>read</code> (default), <code>write</code>, <code>erase</code> (the
75
+ 4 KiB sector containing <code>address</code>), <code>id</code>, <code>status</code> or
76
+ <code>info</code>. May be passed in <code>msg.op</code>.</dd>
77
+ <dt>address <span class="property-type">number</span></dt>
78
+ <dd>Byte address 0–16777215 (default 0).</dd>
79
+ <dt>length <span class="property-type">number</span></dt>
80
+ <dd>Number of bytes to read (<code>read</code> only; default 16).</dd>
81
+ <dt>payload <span class="property-type">Buffer</span></dt>
82
+ <dd>Data to write (<code>write</code> only; alternatively <code>msg.data</code> as an array of numbers).</dd>
83
+ </dl>
84
+
85
+ <h3>Outputs</h3>
86
+ <dl class="message-properties">
87
+ <dt>payload <span class="property-type">Buffer | object</span></dt>
88
+ <dd><code>read</code>: the bytes as a Buffer. <code>id</code>: JEDEC ID and serial-number area
89
+ (hex). <code>status</code>: status register, block protection, security register, busy flag.
90
+ <code>info</code>: block geometry. Other operations: <code>{ ok: true }</code>.</dd>
91
+ </dl>
92
+ </script>
@@ -0,0 +1,75 @@
1
+ "use strict";
2
+
3
+ module.exports = function(RED) {
4
+ const { SPIConnection } = require('periph/src/connection/spi');
5
+ const { MX25L12845EFull } = require('periph/src/chips/memory/mx25l12845e');
6
+
7
+ function Mx25l12845eDeviceNode(config) {
8
+ RED.nodes.createNode(this, config);
9
+ const node = this;
10
+ node.ready = null;
11
+ try {
12
+ const connection = new SPIConnection(parseInt(config.bus), parseInt(config.device), {
13
+ mode: 0,
14
+ maxSpeedHz: parseInt(config.speed) || 8000000,
15
+ });
16
+ node.driver = new MX25L12845EFull(connection, { autoErase: true });
17
+ node.connection = connection;
18
+ node.ready = node.driver.init().catch(function(e) {
19
+ node.error('MX25L12845E init failed: ' + e.message);
20
+ node.driver = null;
21
+ });
22
+ } catch (e) {
23
+ node.error('MX25L12845E init failed: ' + e.message);
24
+ }
25
+ node.on('close', function() {
26
+ if (node.connection) node.connection.close();
27
+ });
28
+ }
29
+ RED.nodes.registerType('mx25l12845e-device', Mx25l12845eDeviceNode);
30
+
31
+ function Mx25l12845eNode(config) {
32
+ RED.nodes.createNode(this, config);
33
+ const node = this;
34
+ node.device = RED.nodes.getNode(config.device);
35
+
36
+ node.on('input', async function(msg, send, done) {
37
+ if (node.device && node.device.ready) await node.device.ready;
38
+ if (!node.device || !node.device.driver) {
39
+ node.error('No MX25L12845E device configured', msg);
40
+ done();
41
+ return;
42
+ }
43
+ try {
44
+ const d = node.device.driver;
45
+ const op = (msg.op || config.op || 'read');
46
+ const address = (msg.address != null) ? msg.address : 0;
47
+ if (op === 'read') {
48
+ msg.payload = await d.readBytes(address, (msg.length != null) ? msg.length : 16);
49
+ } else if (op === 'write') {
50
+ const data = Buffer.isBuffer(msg.payload) ? msg.payload : Buffer.from(msg.data || []);
51
+ await d.writeBytes(address, data);
52
+ msg.payload = { ok: true };
53
+ } else if (op === 'erase') {
54
+ await d.eraseSector(address);
55
+ msg.payload = { ok: true };
56
+ } else if (op === 'id') {
57
+ msg.payload = { jedecId: (await d.jedecId()).toString('hex'), serial: (await d.serialNumber()).toString('hex') };
58
+ } else if (op === 'status') {
59
+ msg.payload = { status: await d.readStatus(), protection: await d.protection(), security: await d.readSecurity(), busy: await d.isBusy() };
60
+ } else if (op === 'info') {
61
+ msg.payload = d.info();
62
+ } else {
63
+ node.error('Unknown op: ' + op, msg);
64
+ done();
65
+ return;
66
+ }
67
+ send(msg);
68
+ done();
69
+ } catch (e) {
70
+ done(e);
71
+ }
72
+ });
73
+ }
74
+ RED.nodes.registerType('mx25l12845e', Mx25l12845eNode);
75
+ };
@@ -0,0 +1,104 @@
1
+ <!-- Config node: one instance per physical card -->
2
+ <script type="text/javascript">
3
+ RED.nodes.registerType('sd-spi-device', {
4
+ category: 'config',
5
+ defaults: {
6
+ name: { value: '' },
7
+ bus: { value: '0', required: true, validate: RED.validators.number() },
8
+ device: { value: '0', required: true, validate: RED.validators.number() },
9
+ gpiochip: { value: '/dev/gpiochip0', required: true },
10
+ csLine: { value: '25', required: true, validate: RED.validators.number() },
11
+ speed: { value: '12000000', required: true, validate: RED.validators.number() }
12
+ },
13
+ label: function() {
14
+ return this.name || ('SD card @ spidev' + this.bus + '.' + this.device);
15
+ }
16
+ });
17
+ </script>
18
+
19
+ <script type="text/html" data-template-name="sd-spi-device">
20
+ <div class="form-row">
21
+ <label for="node-config-input-name"><i class="fa fa-tag"></i> Name</label>
22
+ <input type="text" id="node-config-input-name" placeholder="SD card">
23
+ </div>
24
+ <div class="form-row">
25
+ <label for="node-config-input-bus"><i class="fa fa-microchip"></i> SPI bus</label>
26
+ <input type="number" id="node-config-input-bus" min="0" style="width:80px"> &nbsp;(Linux /dev/spidevB.D)
27
+ </div>
28
+ <div class="form-row">
29
+ <label for="node-config-input-device">Device</label>
30
+ <input type="number" id="node-config-input-device" min="0" style="width:80px"> &nbsp;device number D (its kernel chip select is disabled)
31
+ </div>
32
+ <div class="form-row">
33
+ <label for="node-config-input-gpiochip">GPIO chip</label>
34
+ <input type="text" id="node-config-input-gpiochip" style="width:200px">
35
+ </div>
36
+ <div class="form-row">
37
+ <label for="node-config-input-csLine">CS line</label>
38
+ <input type="number" id="node-config-input-csLine" min="0" style="width:80px"> &nbsp;GPIO wired to the card's CS (driven by the connection)
39
+ </div>
40
+ <div class="form-row">
41
+ <label for="node-config-input-speed">Clock (Hz)</label>
42
+ <input type="number" id="node-config-input-speed" min="100000" max="25000000" style="width:120px"> &nbsp;after init (init runs at 400 kHz); default 12000000
43
+ </div>
44
+ </script>
45
+
46
+ <!-- Operation node: trigger → info / read / write / erase / sync -->
47
+ <script type="text/javascript">
48
+ RED.nodes.registerType('sd-spi', {
49
+ category: 'Peripheral',
50
+ color: '#C084FC',
51
+ defaults: {
52
+ name: { value: '' },
53
+ device: { value: '', type: 'sd-spi-device', required: true }
54
+ },
55
+ inputs: 1,
56
+ outputs: 1,
57
+ icon: 'font-awesome/fa-hdd-o',
58
+ label: function() { return this.name || 'SD card'; },
59
+ paletteLabel: 'SD card (SPI)'
60
+ });
61
+ </script>
62
+
63
+ <script type="text/html" data-template-name="sd-spi">
64
+ <div class="form-row">
65
+ <label for="node-input-device"><i class="fa fa-microchip"></i> Device</label>
66
+ <input type="text" id="node-input-device">
67
+ </div>
68
+ <div class="form-row">
69
+ <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
70
+ <input type="text" id="node-input-name" placeholder="SD card">
71
+ </div>
72
+ </script>
73
+
74
+ <script type="text/html" data-help-name="sd-spi">
75
+ <p>Reads and writes raw 512-byte blocks of an SD / SDHC / SDXC / microSD card in SPI mode.
76
+ There is no filesystem: blocks are addressed by number.</p>
77
+ <p>Any input message triggers an operation. The card is configured via an
78
+ <b>sd-spi-device</b> config node. Writing a block that holds a filesystem destroys it.</p>
79
+
80
+ <h3>Inputs</h3>
81
+ <dl class="message-properties">
82
+ <dt>op <span class="property-type">string</span></dt>
83
+ <dd>Operation: <code>read</code> (default), <code>write</code>, <code>erase</code>,
84
+ <code>sync</code> or <code>info</code>. May be passed in <code>msg.op</code>.</dd>
85
+ <dt>block <span class="property-type">number</span></dt>
86
+ <dd>First block index (default 0).</dd>
87
+ <dt>count <span class="property-type">number</span></dt>
88
+ <dd>Number of blocks (<code>read</code>, <code>erase</code>; default 1).</dd>
89
+ <dt>payload <span class="property-type">Buffer</span></dt>
90
+ <dd>Data to write, a multiple of 512 bytes (<code>write</code> only; alternatively
91
+ <code>msg.data</code> as an array of numbers).</dd>
92
+ </dl>
93
+
94
+ <h3>Outputs</h3>
95
+ <dl class="message-properties">
96
+ <dt>payload <span class="property-type">Buffer | object</span></dt>
97
+ <dd><code>read</code>: the blocks as a Buffer. <code>info</code>: block size, count, name.
98
+ Other operations: <code>{ ok: true }</code>.</dd>
99
+ </dl>
100
+
101
+ <h3>Configuration</h3>
102
+ <p>Set the SPI bus and device number, the GPIO line that drives the card's chip select and
103
+ the clock in the device config node. Cards are 3.3 V only.</p>
104
+ </script>
@@ -0,0 +1,81 @@
1
+ "use strict";
2
+
3
+ module.exports = function(RED) {
4
+ const { SPIConnection } = require('periph/src/connection/spi');
5
+ const { GpioOutputPin } = require('periph/src/connection/output_pin');
6
+ const { SDSPIFull } = require('periph/src/chips/memory/sd_spi');
7
+
8
+ function SdSpiDeviceNode(config) {
9
+ RED.nodes.createNode(this, config);
10
+ const node = this;
11
+ node.ready = null;
12
+ try {
13
+ // The kernel cannot hold chip select across calls, so CS is a GPIO (SPI_NO_CS).
14
+ const { Default } = require('opengpio');
15
+ const csPin = new GpioOutputPin(Default.output({ chip: config.gpiochip || '/dev/gpiochip0', line: parseInt(config.csLine) }));
16
+ const connection = new SPIConnection(parseInt(config.bus), parseInt(config.device), {
17
+ mode: 0,
18
+ maxSpeedHz: 400000,
19
+ csPin,
20
+ });
21
+ node.driver = new SDSPIFull(connection, { speedHz: parseInt(config.speed) || 12000000 });
22
+ node.connection = connection;
23
+ node.ready = node.driver.init().catch(function(e) {
24
+ node.error('SD card init failed: ' + e.message);
25
+ node.driver = null;
26
+ });
27
+ } catch (e) {
28
+ node.error('SD card init failed: ' + e.message);
29
+ }
30
+ node.on('close', function() {
31
+ if (node.connection) node.connection.close();
32
+ });
33
+ }
34
+ RED.nodes.registerType('sd-spi-device', SdSpiDeviceNode);
35
+
36
+ function SdSpiNode(config) {
37
+ RED.nodes.createNode(this, config);
38
+ const node = this;
39
+ node.device = RED.nodes.getNode(config.device);
40
+
41
+ node.on('input', async function(msg, send, done) {
42
+ if (node.device && node.device.ready) await node.device.ready;
43
+ if (!node.device || !node.device.driver) {
44
+ node.error('No SD card device configured', msg);
45
+ done();
46
+ return;
47
+ }
48
+ try {
49
+ const d = node.device.driver;
50
+ const op = (msg.op || config.op || 'read');
51
+ if (op === 'info') {
52
+ msg.payload = d.info();
53
+ } else if (op === 'read') {
54
+ const block = (msg.block != null) ? msg.block : 0;
55
+ const count = (msg.count != null) ? msg.count : 1;
56
+ msg.payload = await d.read(block, count);
57
+ } else if (op === 'write') {
58
+ const block = (msg.block != null) ? msg.block : 0;
59
+ const data = Buffer.isBuffer(msg.payload) ? msg.payload : Buffer.from(msg.data || []);
60
+ await d.write(block, data);
61
+ msg.payload = { ok: true };
62
+ } else if (op === 'erase') {
63
+ await d.erase((msg.block != null) ? msg.block : 0, (msg.count != null) ? msg.count : 1);
64
+ msg.payload = { ok: true };
65
+ } else if (op === 'sync') {
66
+ await d.sync();
67
+ msg.payload = { ok: true };
68
+ } else {
69
+ node.error('Unknown op: ' + op, msg);
70
+ done();
71
+ return;
72
+ }
73
+ send(msg);
74
+ done();
75
+ } catch (e) {
76
+ done(e);
77
+ }
78
+ });
79
+ }
80
+ RED.nodes.registerType('sd-spi', SdSpiNode);
81
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-red-contrib-periph-memory",
3
- "version": "1.2.1",
3
+ "version": "1.3.1",
4
4
  "description": "Node-RED nodes for memory chips",
5
5
  "keywords": [
6
6
  "node-red",
@@ -9,11 +9,16 @@
9
9
  "flash",
10
10
  "i2c",
11
11
  "spi",
12
- "peripheral"
12
+ "peripheral",
13
+ "sd",
14
+ "storage"
13
15
  ],
14
16
  "node-red": {
15
17
  "nodes": {
16
- "24aa02uid": "nodes/24aa02uid/24aa02uid.js"
18
+ "23x1024": "nodes/23x1024/23x1024.js",
19
+ "24aa02uid": "nodes/24aa02uid/24aa02uid.js",
20
+ "sd_spi": "nodes/sd_spi/sd_spi.js",
21
+ "mx25l12845e": "nodes/mx25l12845e/mx25l12845e.js"
17
22
  }
18
23
  },
19
24
  "dependencies": {