node-ch347 0.0.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/LICENSE +24 -0
- package/README.md +234 -0
- package/dist/config.d.ts +20 -0
- package/dist/config.js +110 -0
- package/dist/constants.d.ts +83 -0
- package/dist/constants.js +107 -0
- package/dist/flash.d.ts +134 -0
- package/dist/flash.js +597 -0
- package/dist/gpio.d.ts +67 -0
- package/dist/gpio.js +196 -0
- package/dist/index.d.ts +149 -0
- package/dist/index.js +222 -0
- package/dist/spi.d.ts +66 -0
- package/dist/spi.js +227 -0
- package/dist/types.d.ts +74 -0
- package/dist/types.js +45 -0
- package/dist/uart.d.ts +81 -0
- package/dist/uart.js +343 -0
- package/dist/usb.d.ts +80 -0
- package/dist/usb.js +422 -0
- package/package.json +49 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
This is free and unencumbered software released into the public domain.
|
|
2
|
+
|
|
3
|
+
Anyone is free to copy, modify, publish, use, compile, sell, or
|
|
4
|
+
distribute this software, either in source code form or as a compiled
|
|
5
|
+
binary, for any purpose, commercial or non-commercial, and by any
|
|
6
|
+
means.
|
|
7
|
+
|
|
8
|
+
In jurisdictions that recognize copyright laws, the author or authors
|
|
9
|
+
of this software dedicate any and all copyright interest in the
|
|
10
|
+
software to the public domain. We make this dedication for the benefit
|
|
11
|
+
of the public at large and to the detriment of our heirs and
|
|
12
|
+
successors. We intend this dedication to be an overt act of
|
|
13
|
+
relinquishment in perpetuity of all present and future rights to this
|
|
14
|
+
software under copyright law.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
19
|
+
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
|
20
|
+
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
|
21
|
+
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
22
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
|
23
|
+
|
|
24
|
+
For more information, please refer to <https://unlicense.org>
|
package/README.md
ADDED
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
# node-ch347
|
|
2
|
+
|
|
3
|
+
A Node.js library for interfacing with WCH CH347 USB devices. Supports GPIO control and SPI flash programming.
|
|
4
|
+
|
|
5
|
+
> **Note:** This is an early release (v0.0.1). The API may change in future versions.
|
|
6
|
+
|
|
7
|
+
## Features
|
|
8
|
+
|
|
9
|
+
- **GPIO Control**: 8 GPIO channels with input/output support
|
|
10
|
+
- **SPI Flash Programming**: Read, write, erase, and verify SPI flash chips
|
|
11
|
+
- Supports common chips: W25Qxx, GD25Qxx, MX25Lxx, MT25Qxx, etc.
|
|
12
|
+
- JEDEC ID detection
|
|
13
|
+
- Sector (4KB), block (32KB/64KB), and chip erase
|
|
14
|
+
- **UART Path Discovery**: Get the serial port path for use with external libraries
|
|
15
|
+
- **Cross-Platform**: Works on Linux and macOS
|
|
16
|
+
|
|
17
|
+
## Installation
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
npm install node-ch347
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
### Prerequisites
|
|
24
|
+
|
|
25
|
+
**Linux:**
|
|
26
|
+
```bash
|
|
27
|
+
# Install libusb
|
|
28
|
+
sudo apt-get install libusb-1.0-0-dev
|
|
29
|
+
|
|
30
|
+
# Add udev rules for non-root access
|
|
31
|
+
sudo tee /etc/udev/rules.d/99-ch347.rules << EOF
|
|
32
|
+
SUBSYSTEM=="usb", ATTRS{idVendor}=="1a86", ATTRS{idProduct}=="55db", MODE="0666"
|
|
33
|
+
EOF
|
|
34
|
+
sudo udevadm control --reload-rules
|
|
35
|
+
sudo udevadm trigger
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
**macOS:**
|
|
39
|
+
```bash
|
|
40
|
+
# Install libusb via Homebrew
|
|
41
|
+
brew install libusb
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## Quick Start
|
|
45
|
+
|
|
46
|
+
```typescript
|
|
47
|
+
import CH347Device, { SPISpeed } from 'node-ch347';
|
|
48
|
+
|
|
49
|
+
async function main() {
|
|
50
|
+
// Create device instance
|
|
51
|
+
const ch347 = new CH347Device({
|
|
52
|
+
spi: { speed: SPISpeed.CLK_15M }
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
// Open connection
|
|
56
|
+
await ch347.open();
|
|
57
|
+
|
|
58
|
+
// GPIO: Set pin 3 high
|
|
59
|
+
await ch347.gpioWrite(3, true);
|
|
60
|
+
|
|
61
|
+
// SPI Flash: Read chip ID
|
|
62
|
+
const flashInfo = await ch347.flashReadId();
|
|
63
|
+
console.log(`Flash: ${flashInfo.name}, ${flashInfo.size / 1024 / 1024} MB`);
|
|
64
|
+
|
|
65
|
+
// SPI Flash: Read data
|
|
66
|
+
const data = await ch347.flashRead(0, 4096);
|
|
67
|
+
|
|
68
|
+
// Close connection
|
|
69
|
+
ch347.close();
|
|
70
|
+
}
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## API Reference
|
|
74
|
+
|
|
75
|
+
### CH347Device
|
|
76
|
+
|
|
77
|
+
Main class that provides unified access to all functionality.
|
|
78
|
+
|
|
79
|
+
```typescript
|
|
80
|
+
const device = new CH347Device(options?: {
|
|
81
|
+
spi?: Partial<SPIConfig>;
|
|
82
|
+
});
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
#### Static Methods
|
|
86
|
+
|
|
87
|
+
- `CH347Device.listDevices()`: List all connected CH347 devices
|
|
88
|
+
|
|
89
|
+
#### Instance Methods
|
|
90
|
+
|
|
91
|
+
**Connection:**
|
|
92
|
+
- `open(deviceIndex?: number)`: Open device connection
|
|
93
|
+
- `close()`: Close connection
|
|
94
|
+
- `isConnected()`: Check if connected
|
|
95
|
+
- `getUARTPath()`: Get the tty path for this device (e.g., `/dev/ttyACM0`)
|
|
96
|
+
|
|
97
|
+
**GPIO:**
|
|
98
|
+
- `gpioReadAll()`: Read all GPIO pin states
|
|
99
|
+
- `gpioRead(pin)`: Read single GPIO pin
|
|
100
|
+
- `gpioWrite(pin, value)`: Set GPIO output
|
|
101
|
+
- `gpioPulse(pin, duration, activeHigh)`: Pulse GPIO pin
|
|
102
|
+
|
|
103
|
+
**SPI Flash:**
|
|
104
|
+
- `spiInit(config?)`: Initialize SPI interface
|
|
105
|
+
- `flashReadId()`: Read JEDEC ID
|
|
106
|
+
- `flashRead(address, length, onProgress?)`: Read data
|
|
107
|
+
- `flashWrite(address, data, onProgress?)`: Write data
|
|
108
|
+
- `flashEraseSector(address)`: Erase 4KB sector
|
|
109
|
+
- `flashEraseChip(onProgress?)`: Erase entire chip
|
|
110
|
+
- `flashProgram(address, data, options?)`: Erase + write + verify
|
|
111
|
+
- `flashProgramFile(filePath, address?, options?)`: Program flash from binary file (if address omitted, file size must match flash size)
|
|
112
|
+
- `flashReadToFile(filePath, address?, length?, onProgress?)`: Read flash to binary file
|
|
113
|
+
|
|
114
|
+
### SPI Configuration
|
|
115
|
+
|
|
116
|
+
```typescript
|
|
117
|
+
interface SPIConfig {
|
|
118
|
+
speed: SPISpeed; // Clock speed (default: CLK_15M)
|
|
119
|
+
mode: SPIMode; // SPI mode 0-3 (default: MODE_0)
|
|
120
|
+
chipSelect: 0 | 1; // CS line (default: 0)
|
|
121
|
+
bitOrder: 'MSB' | 'LSB'; // Bit order (default: 'MSB')
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
enum SPISpeed {
|
|
125
|
+
CLK_60M = 0,
|
|
126
|
+
CLK_30M = 1,
|
|
127
|
+
CLK_15M = 2,
|
|
128
|
+
CLK_7_5M = 3,
|
|
129
|
+
CLK_3_75M = 4,
|
|
130
|
+
CLK_1_875M = 5,
|
|
131
|
+
CLK_937_5K = 6,
|
|
132
|
+
CLK_468_75K = 7,
|
|
133
|
+
}
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
## Examples
|
|
137
|
+
|
|
138
|
+
### GPIO Control
|
|
139
|
+
|
|
140
|
+
```typescript
|
|
141
|
+
import CH347Device from 'node-ch347';
|
|
142
|
+
|
|
143
|
+
const ch347 = new CH347Device();
|
|
144
|
+
await ch347.open();
|
|
145
|
+
|
|
146
|
+
// Read all GPIO states
|
|
147
|
+
const states = await ch347.gpioReadAll();
|
|
148
|
+
states.forEach(s => console.log(`GPIO${s.pin}: ${s.value}`));
|
|
149
|
+
|
|
150
|
+
// Set output on pin 3
|
|
151
|
+
await ch347.gpioWrite(3, true);
|
|
152
|
+
|
|
153
|
+
// Pulse pin 0 (for button press simulation)
|
|
154
|
+
await ch347.gpioPulse(0, 100);
|
|
155
|
+
|
|
156
|
+
ch347.close();
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
### Flash Programming
|
|
160
|
+
|
|
161
|
+
```typescript
|
|
162
|
+
import CH347Device, { SPISpeed } from 'node-ch347';
|
|
163
|
+
|
|
164
|
+
const ch347 = new CH347Device({ spi: { speed: SPISpeed.CLK_30M } });
|
|
165
|
+
await ch347.open();
|
|
166
|
+
|
|
167
|
+
// Detect flash
|
|
168
|
+
const info = await ch347.flashReadId();
|
|
169
|
+
console.log(`Detected: ${info.name}`);
|
|
170
|
+
|
|
171
|
+
// Backup flash to file
|
|
172
|
+
await ch347.flashReadToFile('backup.bin', 0, info.size, (p) => {
|
|
173
|
+
console.log(`Reading: ${p.percentage}%`);
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
// Program flash from file (erase + write + verify)
|
|
177
|
+
await ch347.flashProgramFile('new_firmware.bin', 0, {
|
|
178
|
+
erase: true,
|
|
179
|
+
verify: true,
|
|
180
|
+
onProgress: (p) => console.log(`${p.operation}: ${p.percentage}%`)
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
ch347.close();
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
### UART Path Discovery
|
|
187
|
+
|
|
188
|
+
The library provides UART path discovery. Use an external serial library like `serialport` for actual communication:
|
|
189
|
+
|
|
190
|
+
```typescript
|
|
191
|
+
import CH347Device from 'node-ch347';
|
|
192
|
+
import { SerialPort } from 'serialport'; // npm install serialport
|
|
193
|
+
|
|
194
|
+
const ch347 = new CH347Device();
|
|
195
|
+
await ch347.open();
|
|
196
|
+
|
|
197
|
+
// Get UART path for this device
|
|
198
|
+
const uartPath = ch347.getUARTPath();
|
|
199
|
+
console.log('UART path:', uartPath); // e.g., '/dev/ttyACM0'
|
|
200
|
+
|
|
201
|
+
// Use serialport library for UART communication
|
|
202
|
+
if (uartPath) {
|
|
203
|
+
const port = new SerialPort({ path: uartPath, baudRate: 115200 });
|
|
204
|
+
port.write('AT\r\n');
|
|
205
|
+
// ... handle data with port.on('data', ...)
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
ch347.close();
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
## CH347 Hardware
|
|
212
|
+
|
|
213
|
+
The CH347 is a USB 2.0 high-speed (480 Mbps) bus converter chip by WCH (Nanjing Qinheng Microelectronics).
|
|
214
|
+
|
|
215
|
+
**Supported mode:**
|
|
216
|
+
- Mode 1 (PID 0x55DB): UART + SPI + I2C + GPIO
|
|
217
|
+
|
|
218
|
+
**Pin assignments for GPIO (Mode 1):**
|
|
219
|
+
- GPIO0: CTS0/SCK/TCK
|
|
220
|
+
- GPIO1: RTS0/MISO/TDO
|
|
221
|
+
- GPIO2: DSR0/SCS0/TMS
|
|
222
|
+
- GPIO3: SCL
|
|
223
|
+
- GPIO4: ACT
|
|
224
|
+
- GPIO5: DTR0/TNOW0/SCS1/TRST
|
|
225
|
+
- GPIO6: CTS1
|
|
226
|
+
- GPIO7: RTS1
|
|
227
|
+
|
|
228
|
+
## Disclaimer
|
|
229
|
+
|
|
230
|
+
This project was developed with AI-assisted code generation. Not all functions have been fully tested. Use at your own risk.
|
|
231
|
+
|
|
232
|
+
## License
|
|
233
|
+
|
|
234
|
+
This project is released into the public domain under [The Unlicense](LICENSE).
|
package/dist/config.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CH347 Device Configuration
|
|
3
|
+
*
|
|
4
|
+
* Functions to list CH347 devices with their serial numbers.
|
|
5
|
+
*/
|
|
6
|
+
export interface CH347DeviceWithSerial {
|
|
7
|
+
index: number;
|
|
8
|
+
vendorId: number;
|
|
9
|
+
productId: number;
|
|
10
|
+
busNumber: number;
|
|
11
|
+
deviceAddress: number;
|
|
12
|
+
serialNumber: string | null;
|
|
13
|
+
manufacturer: string | null;
|
|
14
|
+
product: string | null;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* List all connected CH347 devices with their serial numbers
|
|
18
|
+
*/
|
|
19
|
+
export declare function listDevicesWithSerial(): Promise<CH347DeviceWithSerial[]>;
|
|
20
|
+
//# sourceMappingURL=config.d.ts.map
|
package/dist/config.js
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* CH347 Device Configuration
|
|
4
|
+
*
|
|
5
|
+
* Functions to list CH347 devices with their serial numbers.
|
|
6
|
+
*/
|
|
7
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
8
|
+
if (k2 === undefined) k2 = k;
|
|
9
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
10
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
11
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
12
|
+
}
|
|
13
|
+
Object.defineProperty(o, k2, desc);
|
|
14
|
+
}) : (function(o, m, k, k2) {
|
|
15
|
+
if (k2 === undefined) k2 = k;
|
|
16
|
+
o[k2] = m[k];
|
|
17
|
+
}));
|
|
18
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
19
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
20
|
+
}) : function(o, v) {
|
|
21
|
+
o["default"] = v;
|
|
22
|
+
});
|
|
23
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
24
|
+
var ownKeys = function(o) {
|
|
25
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
26
|
+
var ar = [];
|
|
27
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
28
|
+
return ar;
|
|
29
|
+
};
|
|
30
|
+
return ownKeys(o);
|
|
31
|
+
};
|
|
32
|
+
return function (mod) {
|
|
33
|
+
if (mod && mod.__esModule) return mod;
|
|
34
|
+
var result = {};
|
|
35
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
36
|
+
__setModuleDefault(result, mod);
|
|
37
|
+
return result;
|
|
38
|
+
};
|
|
39
|
+
})();
|
|
40
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
41
|
+
exports.listDevicesWithSerial = listDevicesWithSerial;
|
|
42
|
+
const usb = __importStar(require("usb"));
|
|
43
|
+
const constants_1 = require("./constants");
|
|
44
|
+
/**
|
|
45
|
+
* List all connected CH347 devices with their serial numbers
|
|
46
|
+
*/
|
|
47
|
+
async function listDevicesWithSerial() {
|
|
48
|
+
const devices = [];
|
|
49
|
+
const allDevices = usb.getDeviceList();
|
|
50
|
+
let index = 0;
|
|
51
|
+
for (const device of allDevices) {
|
|
52
|
+
const desc = device.deviceDescriptor;
|
|
53
|
+
if (desc.idVendor === constants_1.CH347_VID &&
|
|
54
|
+
(desc.idProduct === constants_1.CH347_PID_SPI_I2C_UART ||
|
|
55
|
+
desc.idProduct === constants_1.CH347_PID_JTAG_I2C_UART)) {
|
|
56
|
+
const info = {
|
|
57
|
+
index,
|
|
58
|
+
vendorId: desc.idVendor,
|
|
59
|
+
productId: desc.idProduct,
|
|
60
|
+
busNumber: device.busNumber,
|
|
61
|
+
deviceAddress: device.deviceAddress,
|
|
62
|
+
serialNumber: null,
|
|
63
|
+
manufacturer: null,
|
|
64
|
+
product: null,
|
|
65
|
+
};
|
|
66
|
+
// Try to read string descriptors
|
|
67
|
+
try {
|
|
68
|
+
device.open();
|
|
69
|
+
if (desc.iSerialNumber) {
|
|
70
|
+
info.serialNumber = await getStringDescriptor(device, desc.iSerialNumber);
|
|
71
|
+
}
|
|
72
|
+
if (desc.iManufacturer) {
|
|
73
|
+
info.manufacturer = await getStringDescriptor(device, desc.iManufacturer);
|
|
74
|
+
}
|
|
75
|
+
if (desc.iProduct) {
|
|
76
|
+
info.product = await getStringDescriptor(device, desc.iProduct);
|
|
77
|
+
}
|
|
78
|
+
device.close();
|
|
79
|
+
}
|
|
80
|
+
catch {
|
|
81
|
+
// Device may be in use or require permissions
|
|
82
|
+
try {
|
|
83
|
+
device.close();
|
|
84
|
+
}
|
|
85
|
+
catch {
|
|
86
|
+
// Ignore close errors
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
devices.push(info);
|
|
90
|
+
index++;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return devices;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Get USB string descriptor
|
|
97
|
+
*/
|
|
98
|
+
function getStringDescriptor(device, index) {
|
|
99
|
+
return new Promise((resolve, reject) => {
|
|
100
|
+
device.getStringDescriptor(index, (err, data) => {
|
|
101
|
+
if (err) {
|
|
102
|
+
reject(err);
|
|
103
|
+
}
|
|
104
|
+
else {
|
|
105
|
+
resolve(data ?? '');
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
//# sourceMappingURL=config.js.map
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CH347 USB Constants and Protocol Definitions
|
|
3
|
+
*/
|
|
4
|
+
export declare const CH347_VID = 6790;
|
|
5
|
+
export declare const CH347_PID_SPI_I2C_UART = 21979;
|
|
6
|
+
export declare const CH347_PID_JTAG_I2C_UART = 21981;
|
|
7
|
+
export declare const CH347_EP_OUT = 6;
|
|
8
|
+
export declare const CH347_EP_IN = 134;
|
|
9
|
+
export declare const CH347_IFACE_SPI_I2C_GPIO = 2;
|
|
10
|
+
export declare const CH347_IFACE_UART = 0;
|
|
11
|
+
export declare const CH347_PACKET_SIZE = 512;
|
|
12
|
+
export declare const CH347_MAX_PAYLOAD = 507;
|
|
13
|
+
export declare const CH347_TIMEOUT_MS = 1000;
|
|
14
|
+
export declare const CH347_CMD_GPIO = 204;
|
|
15
|
+
export declare const CH347_GPIO_COUNT = 8;
|
|
16
|
+
export declare const GPIO_PIN_ENABLE = 192;
|
|
17
|
+
export declare const GPIO_PIN_DIR_OUT = 48;
|
|
18
|
+
export declare const GPIO_PIN_VALUE_HIGH = 8;
|
|
19
|
+
export declare const GPIO_PIN_IS_OUTPUT = 128;
|
|
20
|
+
export declare const GPIO_PIN_VALUE = 64;
|
|
21
|
+
export declare const CH347_CMD_I2C_STREAM = 170;
|
|
22
|
+
export declare const CH347_CMD_I2C_STM_END = 0;
|
|
23
|
+
export declare const CH347_CMD_I2C_STM_STA = 116;
|
|
24
|
+
export declare const CH347_CMD_I2C_STM_STO = 117;
|
|
25
|
+
export declare const CH347_CMD_I2C_STM_OUT = 128;
|
|
26
|
+
export declare const CH347_CMD_I2C_STM_IN = 192;
|
|
27
|
+
export declare const CH347_CMD_I2C_STM_SET = 96;
|
|
28
|
+
export declare enum I2CSpeed {
|
|
29
|
+
LOW = 0,// 20kHz
|
|
30
|
+
STANDARD = 1,// 100kHz
|
|
31
|
+
FAST = 2,// 400kHz
|
|
32
|
+
HIGH = 3
|
|
33
|
+
}
|
|
34
|
+
export declare const CH347_CMD_SPI_SET_CFG = 192;
|
|
35
|
+
export declare const CH347_CMD_SPI_CS_CTRL = 193;
|
|
36
|
+
export declare const CH347_CMD_SPI_OUT_IN = 194;
|
|
37
|
+
export declare const CH347_CMD_SPI_IN = 195;
|
|
38
|
+
export declare const CH347_CMD_SPI_OUT = 196;
|
|
39
|
+
export declare const CH347_CMD_SPI_GET_CFG = 202;
|
|
40
|
+
export declare const CH347_CS_ASSERT = 0;
|
|
41
|
+
export declare const CH347_CS_DEASSERT = 64;
|
|
42
|
+
export declare const CH347_CS_CHANGE = 128;
|
|
43
|
+
export declare const CH347_CS_IGNORE = 0;
|
|
44
|
+
export declare enum SPISpeed {
|
|
45
|
+
CLK_60M = 0,
|
|
46
|
+
CLK_30M = 1,
|
|
47
|
+
CLK_15M = 2,
|
|
48
|
+
CLK_7_5M = 3,
|
|
49
|
+
CLK_3_75M = 4,
|
|
50
|
+
CLK_1_875M = 5,
|
|
51
|
+
CLK_937_5K = 6,
|
|
52
|
+
CLK_468_75K = 7
|
|
53
|
+
}
|
|
54
|
+
export declare enum SPIMode {
|
|
55
|
+
MODE_0 = 0,// CPOL=0, CPHA=0
|
|
56
|
+
MODE_1 = 1,// CPOL=0, CPHA=1
|
|
57
|
+
MODE_2 = 2,// CPOL=1, CPHA=0
|
|
58
|
+
MODE_3 = 3
|
|
59
|
+
}
|
|
60
|
+
export declare const FLASH_CMD_WRITE_ENABLE = 6;
|
|
61
|
+
export declare const FLASH_CMD_WRITE_DISABLE = 4;
|
|
62
|
+
export declare const FLASH_CMD_READ_STATUS = 5;
|
|
63
|
+
export declare const FLASH_CMD_WRITE_STATUS = 1;
|
|
64
|
+
export declare const FLASH_CMD_READ_DATA = 3;
|
|
65
|
+
export declare const FLASH_CMD_FAST_READ = 11;
|
|
66
|
+
export declare const FLASH_CMD_PAGE_PROGRAM = 2;
|
|
67
|
+
export declare const FLASH_CMD_SECTOR_ERASE = 32;
|
|
68
|
+
export declare const FLASH_CMD_BLOCK_ERASE_32K = 82;
|
|
69
|
+
export declare const FLASH_CMD_BLOCK_ERASE_64K = 216;
|
|
70
|
+
export declare const FLASH_CMD_CHIP_ERASE = 199;
|
|
71
|
+
export declare const FLASH_CMD_READ_ID = 159;
|
|
72
|
+
export declare const FLASH_CMD_READ_JEDEC_ID = 159;
|
|
73
|
+
export declare const FLASH_CMD_POWER_DOWN = 185;
|
|
74
|
+
export declare const FLASH_CMD_RELEASE_POWER_DOWN = 171;
|
|
75
|
+
export declare const FLASH_CMD_READ_SFDP = 90;
|
|
76
|
+
export declare const FLASH_STATUS_WIP = 1;
|
|
77
|
+
export declare const FLASH_STATUS_WEL = 2;
|
|
78
|
+
export declare const FLASH_PAGE_SIZE = 256;
|
|
79
|
+
export declare const FLASH_SECTOR_SIZE = 4096;
|
|
80
|
+
export declare const FLASH_BLOCK_SIZE_32K = 32768;
|
|
81
|
+
export declare const FLASH_BLOCK_SIZE_64K = 65536;
|
|
82
|
+
export declare const CH347_UART_DEFAULT_BAUD = 115200;
|
|
83
|
+
//# sourceMappingURL=constants.d.ts.map
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* CH347 USB Constants and Protocol Definitions
|
|
4
|
+
*/
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.FLASH_CMD_READ_JEDEC_ID = exports.FLASH_CMD_READ_ID = exports.FLASH_CMD_CHIP_ERASE = exports.FLASH_CMD_BLOCK_ERASE_64K = exports.FLASH_CMD_BLOCK_ERASE_32K = exports.FLASH_CMD_SECTOR_ERASE = exports.FLASH_CMD_PAGE_PROGRAM = exports.FLASH_CMD_FAST_READ = exports.FLASH_CMD_READ_DATA = exports.FLASH_CMD_WRITE_STATUS = exports.FLASH_CMD_READ_STATUS = exports.FLASH_CMD_WRITE_DISABLE = exports.FLASH_CMD_WRITE_ENABLE = exports.SPIMode = exports.SPISpeed = exports.CH347_CS_IGNORE = exports.CH347_CS_CHANGE = exports.CH347_CS_DEASSERT = exports.CH347_CS_ASSERT = exports.CH347_CMD_SPI_GET_CFG = exports.CH347_CMD_SPI_OUT = exports.CH347_CMD_SPI_IN = exports.CH347_CMD_SPI_OUT_IN = exports.CH347_CMD_SPI_CS_CTRL = exports.CH347_CMD_SPI_SET_CFG = exports.I2CSpeed = exports.CH347_CMD_I2C_STM_SET = exports.CH347_CMD_I2C_STM_IN = exports.CH347_CMD_I2C_STM_OUT = exports.CH347_CMD_I2C_STM_STO = exports.CH347_CMD_I2C_STM_STA = exports.CH347_CMD_I2C_STM_END = exports.CH347_CMD_I2C_STREAM = exports.GPIO_PIN_VALUE = exports.GPIO_PIN_IS_OUTPUT = exports.GPIO_PIN_VALUE_HIGH = exports.GPIO_PIN_DIR_OUT = exports.GPIO_PIN_ENABLE = exports.CH347_GPIO_COUNT = exports.CH347_CMD_GPIO = exports.CH347_TIMEOUT_MS = exports.CH347_MAX_PAYLOAD = exports.CH347_PACKET_SIZE = exports.CH347_IFACE_UART = exports.CH347_IFACE_SPI_I2C_GPIO = exports.CH347_EP_IN = exports.CH347_EP_OUT = exports.CH347_PID_JTAG_I2C_UART = exports.CH347_PID_SPI_I2C_UART = exports.CH347_VID = void 0;
|
|
7
|
+
exports.CH347_UART_DEFAULT_BAUD = exports.FLASH_BLOCK_SIZE_64K = exports.FLASH_BLOCK_SIZE_32K = exports.FLASH_SECTOR_SIZE = exports.FLASH_PAGE_SIZE = exports.FLASH_STATUS_WEL = exports.FLASH_STATUS_WIP = exports.FLASH_CMD_READ_SFDP = exports.FLASH_CMD_RELEASE_POWER_DOWN = exports.FLASH_CMD_POWER_DOWN = void 0;
|
|
8
|
+
// USB Vendor and Product IDs
|
|
9
|
+
exports.CH347_VID = 0x1a86;
|
|
10
|
+
exports.CH347_PID_SPI_I2C_UART = 0x55db; // Mode 1: SPI + I2C + UART
|
|
11
|
+
exports.CH347_PID_JTAG_I2C_UART = 0x55dd; // Mode 3: JTAG + I2C + UART
|
|
12
|
+
// USB Endpoints (Mode 1 - SPI/I2C/GPIO interface is on interface 2)
|
|
13
|
+
exports.CH347_EP_OUT = 0x06;
|
|
14
|
+
exports.CH347_EP_IN = 0x86;
|
|
15
|
+
exports.CH347_IFACE_SPI_I2C_GPIO = 2;
|
|
16
|
+
exports.CH347_IFACE_UART = 0; // UART is typically on interface 0
|
|
17
|
+
// USB Transfer Constants
|
|
18
|
+
exports.CH347_PACKET_SIZE = 512;
|
|
19
|
+
exports.CH347_MAX_PAYLOAD = 507; // 512 - 3 (cmd + length) - 2 (padding)
|
|
20
|
+
exports.CH347_TIMEOUT_MS = 1000;
|
|
21
|
+
// GPIO Commands
|
|
22
|
+
exports.CH347_CMD_GPIO = 0xcc;
|
|
23
|
+
exports.CH347_GPIO_COUNT = 8;
|
|
24
|
+
// GPIO Pin Bit Masks (for gpio_obuf[3 + pin])
|
|
25
|
+
exports.GPIO_PIN_ENABLE = 0xc0; // Enable pin change
|
|
26
|
+
exports.GPIO_PIN_DIR_OUT = 0x30; // Set direction to output
|
|
27
|
+
exports.GPIO_PIN_VALUE_HIGH = 0x08; // Set value high
|
|
28
|
+
// GPIO Response Bit Masks (for gpio_ibuf[3 + pin])
|
|
29
|
+
exports.GPIO_PIN_IS_OUTPUT = 0x80; // Direction is output
|
|
30
|
+
exports.GPIO_PIN_VALUE = 0x40; // Current value
|
|
31
|
+
// I2C Commands
|
|
32
|
+
exports.CH347_CMD_I2C_STREAM = 0xaa;
|
|
33
|
+
exports.CH347_CMD_I2C_STM_END = 0x00;
|
|
34
|
+
exports.CH347_CMD_I2C_STM_STA = 0x74; // Start condition
|
|
35
|
+
exports.CH347_CMD_I2C_STM_STO = 0x75; // Stop condition
|
|
36
|
+
exports.CH347_CMD_I2C_STM_OUT = 0x80; // Output data (| length)
|
|
37
|
+
exports.CH347_CMD_I2C_STM_IN = 0xc0; // Input data (| length)
|
|
38
|
+
exports.CH347_CMD_I2C_STM_SET = 0x60; // Set speed (| speed)
|
|
39
|
+
// I2C Speed modes
|
|
40
|
+
var I2CSpeed;
|
|
41
|
+
(function (I2CSpeed) {
|
|
42
|
+
I2CSpeed[I2CSpeed["LOW"] = 0] = "LOW";
|
|
43
|
+
I2CSpeed[I2CSpeed["STANDARD"] = 1] = "STANDARD";
|
|
44
|
+
I2CSpeed[I2CSpeed["FAST"] = 2] = "FAST";
|
|
45
|
+
I2CSpeed[I2CSpeed["HIGH"] = 3] = "HIGH";
|
|
46
|
+
})(I2CSpeed || (exports.I2CSpeed = I2CSpeed = {}));
|
|
47
|
+
// SPI Commands (from flashrom ch347_spi.c)
|
|
48
|
+
exports.CH347_CMD_SPI_SET_CFG = 0xc0;
|
|
49
|
+
exports.CH347_CMD_SPI_CS_CTRL = 0xc1;
|
|
50
|
+
exports.CH347_CMD_SPI_OUT_IN = 0xc2;
|
|
51
|
+
exports.CH347_CMD_SPI_IN = 0xc3;
|
|
52
|
+
exports.CH347_CMD_SPI_OUT = 0xc4;
|
|
53
|
+
exports.CH347_CMD_SPI_GET_CFG = 0xca;
|
|
54
|
+
// SPI Chip Select Control
|
|
55
|
+
exports.CH347_CS_ASSERT = 0x00;
|
|
56
|
+
exports.CH347_CS_DEASSERT = 0x40;
|
|
57
|
+
exports.CH347_CS_CHANGE = 0x80;
|
|
58
|
+
exports.CH347_CS_IGNORE = 0x00;
|
|
59
|
+
// SPI Clock Speeds (divisor values)
|
|
60
|
+
var SPISpeed;
|
|
61
|
+
(function (SPISpeed) {
|
|
62
|
+
SPISpeed[SPISpeed["CLK_60M"] = 0] = "CLK_60M";
|
|
63
|
+
SPISpeed[SPISpeed["CLK_30M"] = 1] = "CLK_30M";
|
|
64
|
+
SPISpeed[SPISpeed["CLK_15M"] = 2] = "CLK_15M";
|
|
65
|
+
SPISpeed[SPISpeed["CLK_7_5M"] = 3] = "CLK_7_5M";
|
|
66
|
+
SPISpeed[SPISpeed["CLK_3_75M"] = 4] = "CLK_3_75M";
|
|
67
|
+
SPISpeed[SPISpeed["CLK_1_875M"] = 5] = "CLK_1_875M";
|
|
68
|
+
SPISpeed[SPISpeed["CLK_937_5K"] = 6] = "CLK_937_5K";
|
|
69
|
+
SPISpeed[SPISpeed["CLK_468_75K"] = 7] = "CLK_468_75K";
|
|
70
|
+
})(SPISpeed || (exports.SPISpeed = SPISpeed = {}));
|
|
71
|
+
// SPI Modes
|
|
72
|
+
var SPIMode;
|
|
73
|
+
(function (SPIMode) {
|
|
74
|
+
SPIMode[SPIMode["MODE_0"] = 0] = "MODE_0";
|
|
75
|
+
SPIMode[SPIMode["MODE_1"] = 1] = "MODE_1";
|
|
76
|
+
SPIMode[SPIMode["MODE_2"] = 2] = "MODE_2";
|
|
77
|
+
SPIMode[SPIMode["MODE_3"] = 3] = "MODE_3";
|
|
78
|
+
})(SPIMode || (exports.SPIMode = SPIMode = {}));
|
|
79
|
+
// SPI Flash Commands (common JEDEC commands)
|
|
80
|
+
exports.FLASH_CMD_WRITE_ENABLE = 0x06;
|
|
81
|
+
exports.FLASH_CMD_WRITE_DISABLE = 0x04;
|
|
82
|
+
exports.FLASH_CMD_READ_STATUS = 0x05;
|
|
83
|
+
exports.FLASH_CMD_WRITE_STATUS = 0x01;
|
|
84
|
+
exports.FLASH_CMD_READ_DATA = 0x03;
|
|
85
|
+
exports.FLASH_CMD_FAST_READ = 0x0b;
|
|
86
|
+
exports.FLASH_CMD_PAGE_PROGRAM = 0x02;
|
|
87
|
+
exports.FLASH_CMD_SECTOR_ERASE = 0x20; // 4KB
|
|
88
|
+
exports.FLASH_CMD_BLOCK_ERASE_32K = 0x52; // 32KB
|
|
89
|
+
exports.FLASH_CMD_BLOCK_ERASE_64K = 0xd8; // 64KB
|
|
90
|
+
exports.FLASH_CMD_CHIP_ERASE = 0xc7;
|
|
91
|
+
exports.FLASH_CMD_READ_ID = 0x9f;
|
|
92
|
+
exports.FLASH_CMD_READ_JEDEC_ID = 0x9f;
|
|
93
|
+
exports.FLASH_CMD_POWER_DOWN = 0xb9;
|
|
94
|
+
exports.FLASH_CMD_RELEASE_POWER_DOWN = 0xab;
|
|
95
|
+
exports.FLASH_CMD_READ_SFDP = 0x5a; // Read SFDP (Serial Flash Discoverable Parameters)
|
|
96
|
+
// Flash Status Register Bits
|
|
97
|
+
exports.FLASH_STATUS_WIP = 0x01; // Write In Progress
|
|
98
|
+
exports.FLASH_STATUS_WEL = 0x02; // Write Enable Latch
|
|
99
|
+
// Flash Constants
|
|
100
|
+
exports.FLASH_PAGE_SIZE = 256;
|
|
101
|
+
exports.FLASH_SECTOR_SIZE = 4096;
|
|
102
|
+
exports.FLASH_BLOCK_SIZE_32K = 32768;
|
|
103
|
+
exports.FLASH_BLOCK_SIZE_64K = 65536;
|
|
104
|
+
// UART interface - uses CDC ACM protocol via serial port
|
|
105
|
+
// The CH347 UART appears as a virtual COM port
|
|
106
|
+
exports.CH347_UART_DEFAULT_BAUD = 115200;
|
|
107
|
+
//# sourceMappingURL=constants.js.map
|