@typecad/board-arduino-uno 0.1.0-alpha.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 +21 -0
- package/README.md +103 -0
- package/dist/analog.d.ts +6 -0
- package/dist/analog.js +9 -0
- package/dist/board.d.ts +62 -0
- package/dist/board.js +26 -0
- package/dist/index.d.ts +48 -0
- package/dist/index.js +94 -0
- package/dist/pins.d.ts +24 -0
- package/dist/pins.js +47 -0
- package/package.json +60 -0
- package/src/USAGE.md +485 -0
- package/src/analog.ts +10 -0
- package/src/board.ts +41 -0
- package/src/index.ts +115 -0
- package/src/pins.ts +63 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 typecad0
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
# @typecad/board-arduino-uno
|
|
2
|
+
|
|
3
|
+
Arduino Uno board definition package for TypeCAD.
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
`@typecad/board-arduino-uno` provides a fully-typed TypeScript SDK for the Arduino Uno (ATmega328P). It exports pins, peripherals, timing utilities, and board metadata so TypeCAD can perform board-aware transpilation and catch hardware mistakes early.
|
|
8
|
+
|
|
9
|
+
## Quick start
|
|
10
|
+
|
|
11
|
+
Add or reference this board in `cuttlefish.config.ts`:
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import type { CuttlefishConfig } from '@typecad/cuttlefish/api';
|
|
15
|
+
|
|
16
|
+
const config: CuttlefishConfig = {
|
|
17
|
+
entry: './src/main.ts',
|
|
18
|
+
target: 'avr',
|
|
19
|
+
mcu: '@typecad/mcu-atmega328p',
|
|
20
|
+
board: '@typecad/board-arduino-uno',
|
|
21
|
+
framework: '@typecad/framework-arduino',
|
|
22
|
+
output: { framework: 'arduino', optimize: 'size' },
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export default config;
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
Transpile and compile:
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
npx cuttlefish src/main.ts --compile --upload --port COM3
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## How to use
|
|
35
|
+
|
|
36
|
+
### Import styles
|
|
37
|
+
|
|
38
|
+
There are several import styles:
|
|
39
|
+
|
|
40
|
+
#### Virtual `@typecad/board` import (recommended)
|
|
41
|
+
|
|
42
|
+
```ts
|
|
43
|
+
import { D13, A0, LED, UART0, delay, millis, I2C0, SPI0, Board } from '@typecad/board';
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
The `@typecad/board` import is a virtual module that the TypeCAD transpiler resolves to whichever board package you configured in `cuttlefish.config.ts`. This is the recommended style.
|
|
47
|
+
|
|
48
|
+
#### Barrel import
|
|
49
|
+
|
|
50
|
+
```ts
|
|
51
|
+
import {
|
|
52
|
+
D13, A0, LED, UART0, delay, millis,
|
|
53
|
+
I2C0, SPI0, Board,
|
|
54
|
+
} from '@typecad/board-arduino-uno';
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
#### Board namespace
|
|
58
|
+
|
|
59
|
+
```ts
|
|
60
|
+
import { Board } from '@typecad/board-arduino-uno';
|
|
61
|
+
|
|
62
|
+
Board.D13.high();
|
|
63
|
+
Board.UART0.println('Hello');
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
### Pin and peripheral exports
|
|
67
|
+
|
|
68
|
+
The package exports full Uno pin definitions plus convenience aliases:
|
|
69
|
+
|
|
70
|
+
- `D0`..`D13`
|
|
71
|
+
- `A0`..`A5`
|
|
72
|
+
- `LED`, `SDA`, `SCL`, `MOSI`, `MISO`, `SCK`, `SS`, `TX`, `RX`
|
|
73
|
+
- `I2C0`, `SPI0`, `UART0`
|
|
74
|
+
- timing utilities: `delay`, `millis`, `micros`, `delayMicroseconds`, `map`, `constrain`
|
|
75
|
+
- numeric helpers: `abs`, `min`, `max`, `clamp`, `inRange`, `toPercent`, `toByte`, `Num`
|
|
76
|
+
- pulse helpers: `pulseIn`, `pulseInLong`, `Pulse`
|
|
77
|
+
- shift utilities: `shiftIn`, `shiftOut`, `Shift`, `ShiftBitOrder`
|
|
78
|
+
- random utilities: `randomSeed`, `random`, `Random`
|
|
79
|
+
- analog helpers: `AnalogReference`, `analogReference`
|
|
80
|
+
- interrupt helpers: `noInterrupts`, `interrupts`, `attachInterrupt`, `detachInterrupt`
|
|
81
|
+
|
|
82
|
+
### Pin safety
|
|
83
|
+
|
|
84
|
+
The Arduino Uno pin package exposes peripheral pin mappings and capability information that TypeCAD uses to warn about unsafe pin usage. Key reserved pin groups include:
|
|
85
|
+
|
|
86
|
+
- `D0` / `D1` — UART0 RX/TX
|
|
87
|
+
- `A4` / `A5` — I2C0 SDA/SCL
|
|
88
|
+
- `D11` / `D12` / `D13` — SPI0 MOSI/MISO/SCK
|
|
89
|
+
|
|
90
|
+
### Example
|
|
91
|
+
|
|
92
|
+
```ts
|
|
93
|
+
import { LED, delay, HIGH } from '@typecad/board';
|
|
94
|
+
|
|
95
|
+
LED.output(HIGH);
|
|
96
|
+
|
|
97
|
+
while (true) {
|
|
98
|
+
LED.toggle();
|
|
99
|
+
delay(1000);
|
|
100
|
+
}
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
This package is intended to be used through the virtual `@typecad/board` import resolver in TypeCAD, but direct imports from `@typecad/board-arduino-uno` are also supported when you want explicit board package references.
|
package/dist/analog.d.ts
ADDED
package/dist/analog.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// ---------------------------------------------------------------------------
|
|
2
|
+
// @typecad/board-arduino-uno — Analog constants
|
|
3
|
+
// ---------------------------------------------------------------------------
|
|
4
|
+
/** Default reference (VCC). */
|
|
5
|
+
export const DEFAULT = 0;
|
|
6
|
+
/** Internal 1.1V reference. */
|
|
7
|
+
export const INTERNAL = 3;
|
|
8
|
+
/** External AREF pin. */
|
|
9
|
+
export const EXTERNAL = 1;
|
package/dist/board.d.ts
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import type { BoardDefinition } from '@typecad/cuttlefish/api/schema';
|
|
2
|
+
import * as boardIndex from './index.js';
|
|
3
|
+
export declare const Board: {
|
|
4
|
+
readonly definition: BoardDefinition;
|
|
5
|
+
D0: boardIndex.Pin;
|
|
6
|
+
D1: boardIndex.Pin;
|
|
7
|
+
D2: boardIndex.Pin;
|
|
8
|
+
D3: boardIndex.Pin;
|
|
9
|
+
D4: boardIndex.Pin;
|
|
10
|
+
D5: boardIndex.Pin;
|
|
11
|
+
D6: boardIndex.Pin;
|
|
12
|
+
D7: boardIndex.Pin;
|
|
13
|
+
D8: boardIndex.Pin;
|
|
14
|
+
D9: boardIndex.Pin;
|
|
15
|
+
D10: boardIndex.Pin;
|
|
16
|
+
D11: boardIndex.Pin;
|
|
17
|
+
D12: boardIndex.Pin;
|
|
18
|
+
D13: boardIndex.Pin;
|
|
19
|
+
A0: boardIndex.Pin;
|
|
20
|
+
A1: boardIndex.Pin;
|
|
21
|
+
A2: boardIndex.Pin;
|
|
22
|
+
A3: boardIndex.Pin;
|
|
23
|
+
A4: boardIndex.Pin;
|
|
24
|
+
A5: boardIndex.Pin;
|
|
25
|
+
LED: boardIndex.Pin;
|
|
26
|
+
SDA: boardIndex.Pin;
|
|
27
|
+
SCL: boardIndex.Pin;
|
|
28
|
+
MOSI: boardIndex.Pin;
|
|
29
|
+
MISO: boardIndex.Pin;
|
|
30
|
+
SCK: boardIndex.Pin;
|
|
31
|
+
SS: boardIndex.Pin;
|
|
32
|
+
TX: boardIndex.Pin;
|
|
33
|
+
RX: boardIndex.Pin;
|
|
34
|
+
I2C0: boardIndex.I2CBus;
|
|
35
|
+
SPI0: boardIndex.SPIBus;
|
|
36
|
+
UART0: boardIndex.SerialPort;
|
|
37
|
+
digital: {
|
|
38
|
+
D0: boardIndex.Pin;
|
|
39
|
+
D1: boardIndex.Pin;
|
|
40
|
+
D2: boardIndex.Pin;
|
|
41
|
+
D3: boardIndex.Pin;
|
|
42
|
+
D4: boardIndex.Pin;
|
|
43
|
+
D5: boardIndex.Pin;
|
|
44
|
+
D6: boardIndex.Pin;
|
|
45
|
+
D7: boardIndex.Pin;
|
|
46
|
+
D8: boardIndex.Pin;
|
|
47
|
+
D9: boardIndex.Pin;
|
|
48
|
+
D10: boardIndex.Pin;
|
|
49
|
+
D11: boardIndex.Pin;
|
|
50
|
+
D12: boardIndex.Pin;
|
|
51
|
+
D13: boardIndex.Pin;
|
|
52
|
+
};
|
|
53
|
+
analog: {
|
|
54
|
+
A0: boardIndex.Pin;
|
|
55
|
+
A1: boardIndex.Pin;
|
|
56
|
+
A2: boardIndex.Pin;
|
|
57
|
+
A3: boardIndex.Pin;
|
|
58
|
+
A4: boardIndex.Pin;
|
|
59
|
+
A5: boardIndex.Pin;
|
|
60
|
+
};
|
|
61
|
+
};
|
|
62
|
+
export default Board;
|
package/dist/board.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// ---------------------------------------------------------------------------
|
|
2
|
+
// @typecad/board-arduino-uno — Board namespace
|
|
3
|
+
//
|
|
4
|
+
// Convenience namespace that exposes every board feature under one object.
|
|
5
|
+
// For method calls on pins/peripherals, prefer direct imports:
|
|
6
|
+
//
|
|
7
|
+
// import { D13, UART0 } from '@typecad/board-arduino-uno';
|
|
8
|
+
// D13.high();
|
|
9
|
+
// UART0.begin(115200);
|
|
10
|
+
//
|
|
11
|
+
// The Board object is useful for pin iteration and metadata access.
|
|
12
|
+
// ---------------------------------------------------------------------------
|
|
13
|
+
import { D0, D1, D2, D3, D4, D5, D6, D7, D8, D9, D10, D11, D12, D13, A0, A1, A2, A3, A4, A5, LED, SDA, SCL, MOSI, MISO, SCK, SS, TX, RX, } from './pins.js';
|
|
14
|
+
import { I2C0, SPI0, UART0 } from '@typecad/mcu-atmega328p';
|
|
15
|
+
import * as boardIndex from './index.js';
|
|
16
|
+
export const Board = {
|
|
17
|
+
get definition() { return boardIndex.ArduinoUno; },
|
|
18
|
+
D0, D1, D2, D3, D4, D5, D6, D7,
|
|
19
|
+
D8, D9, D10, D11, D12, D13,
|
|
20
|
+
A0, A1, A2, A3, A4, A5,
|
|
21
|
+
LED, SDA, SCL, MOSI, MISO, SCK, SS, TX, RX,
|
|
22
|
+
I2C0, SPI0, UART0,
|
|
23
|
+
digital: { D0, D1, D2, D3, D4, D5, D6, D7, D8, D9, D10, D11, D12, D13 },
|
|
24
|
+
analog: { A0, A1, A2, A3, A4, A5 },
|
|
25
|
+
};
|
|
26
|
+
export default Board;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { BoardDefinition } from '@typecad/cuttlefish/api/schema';
|
|
2
|
+
export declare const ArduinoUno: BoardDefinition;
|
|
3
|
+
export default ArduinoUno;
|
|
4
|
+
export * from '@typecad/mcu-atmega328p';
|
|
5
|
+
export * from '@typecad/hal';
|
|
6
|
+
/**
|
|
7
|
+
* Pin collections for runtime capability discovery.
|
|
8
|
+
*/
|
|
9
|
+
export declare const pins: {
|
|
10
|
+
/** PWM-capable pins: PD3, PD5, PD6, PB1, PB2, PB3 */
|
|
11
|
+
readonly pwm: readonly [import("@typecad/hal").Pin, import("@typecad/hal").Pin, import("@typecad/hal").Pin, import("@typecad/hal").Pin, import("@typecad/hal").Pin, import("@typecad/hal").Pin];
|
|
12
|
+
/** Analog input pins: PC0, PC1, PC2, PC3, PC4, PC5 */
|
|
13
|
+
readonly analog: readonly [import("@typecad/hal").Pin, import("@typecad/hal").Pin, import("@typecad/hal").Pin, import("@typecad/hal").Pin, import("@typecad/hal").Pin, import("@typecad/hal").Pin];
|
|
14
|
+
/** External interrupt-capable pins: PD0, PD1, PD2, PD3 */
|
|
15
|
+
readonly interrupt: readonly [import("@typecad/hal").Pin, import("@typecad/hal").Pin, import("@typecad/hal").Pin, import("@typecad/hal").Pin];
|
|
16
|
+
/** All digital I/O pins */
|
|
17
|
+
readonly digital: readonly [import("@typecad/hal").Pin, import("@typecad/hal").Pin, import("@typecad/hal").Pin, import("@typecad/hal").Pin, import("@typecad/hal").Pin, import("@typecad/hal").Pin, import("@typecad/hal").Pin, import("@typecad/hal").Pin, import("@typecad/hal").Pin, import("@typecad/hal").Pin, import("@typecad/hal").Pin, import("@typecad/hal").Pin, import("@typecad/hal").Pin, import("@typecad/hal").Pin, import("@typecad/hal").Pin, import("@typecad/hal").Pin, import("@typecad/hal").Pin, import("@typecad/hal").Pin, import("@typecad/hal").Pin, import("@typecad/hal").Pin];
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Peripheral-to-pin mapping for the Arduino Uno.
|
|
21
|
+
*/
|
|
22
|
+
export declare const PeripheralPins: {
|
|
23
|
+
/** I2C bus 0 — requires PC4 (SDA) and PC5 (SCL). */
|
|
24
|
+
readonly I2C0: {
|
|
25
|
+
readonly SDA: "PC4";
|
|
26
|
+
readonly SCL: "PC5";
|
|
27
|
+
};
|
|
28
|
+
/** SPI bus 0 — requires PB3 (MOSI), PB4 (MISO), PB5 (SCK). PB2 is default CS. */
|
|
29
|
+
readonly SPI0: {
|
|
30
|
+
readonly MOSI: "PB3";
|
|
31
|
+
readonly MISO: "PB4";
|
|
32
|
+
readonly SCK: "PB5";
|
|
33
|
+
readonly CS: "PB2";
|
|
34
|
+
};
|
|
35
|
+
/** UART/Serial 0 — requires PD1 (TX) and PD0 (RX). */
|
|
36
|
+
readonly UART0: {
|
|
37
|
+
readonly TX: "PD1";
|
|
38
|
+
readonly RX: "PD0";
|
|
39
|
+
};
|
|
40
|
+
/** UART/Serial 1 — requires PD2 (TX) and PD3 (RX). */
|
|
41
|
+
readonly UART1: {
|
|
42
|
+
readonly TX: "PD2";
|
|
43
|
+
readonly RX: "PD3";
|
|
44
|
+
};
|
|
45
|
+
};
|
|
46
|
+
export * from './pins.js';
|
|
47
|
+
export * from './analog.js';
|
|
48
|
+
export { Board } from './board.js';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { ATmega328P } from '@typecad/mcu-atmega328p';
|
|
2
|
+
// ---------------------------------------------------------------------------
|
|
3
|
+
// Board definition
|
|
4
|
+
// ---------------------------------------------------------------------------
|
|
5
|
+
export const ArduinoUno = {
|
|
6
|
+
id: 'arduino-uno',
|
|
7
|
+
name: 'Arduino Uno',
|
|
8
|
+
vendor: 'Arduino',
|
|
9
|
+
description: 'Arduino Uno Rev3 — ATmega328P',
|
|
10
|
+
mcu: ATmega328P,
|
|
11
|
+
clockSpeed: 16_000_000, // 16 MHz
|
|
12
|
+
// ----- Pins --------------------------------------------------------------
|
|
13
|
+
pins: {
|
|
14
|
+
...ATmega328P.pins,
|
|
15
|
+
all: ATmega328P.pins.all.map(p => {
|
|
16
|
+
// Add board-specific metadata to PB5 (LED)
|
|
17
|
+
if (p.name === 'PB5') {
|
|
18
|
+
return {
|
|
19
|
+
...p,
|
|
20
|
+
aliases: [...(p.aliases || []), 'LED'],
|
|
21
|
+
onboardLed: true,
|
|
22
|
+
alternateFunctions: [...(p.alternateFunctions || []), 'On-board LED'],
|
|
23
|
+
warnings: [...(p.warnings || []), 'PB5 is also the on-board LED — using as GPIO conflicts with SPI0 SCK']
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
return p;
|
|
27
|
+
}),
|
|
28
|
+
led: 'PB5',
|
|
29
|
+
},
|
|
30
|
+
// ----- Peripherals -------------------------------------------------------
|
|
31
|
+
peripherals: {
|
|
32
|
+
...ATmega328P.peripherals,
|
|
33
|
+
aliases: {
|
|
34
|
+
UART0: 'Serial',
|
|
35
|
+
I2C0: 'Wire',
|
|
36
|
+
SPI0: 'SPI',
|
|
37
|
+
},
|
|
38
|
+
},
|
|
39
|
+
// ----- Build config ------------------------------------------------------
|
|
40
|
+
build: {
|
|
41
|
+
frameworks: {
|
|
42
|
+
platformio: 'uno',
|
|
43
|
+
arduino: 'arduino:avr:uno',
|
|
44
|
+
},
|
|
45
|
+
defines: {
|
|
46
|
+
F_CPU: '16000000UL',
|
|
47
|
+
ARDUINO_AVR_UNO: '1',
|
|
48
|
+
},
|
|
49
|
+
},
|
|
50
|
+
};
|
|
51
|
+
export default ArduinoUno;
|
|
52
|
+
// ---------------------------------------------------------------------------
|
|
53
|
+
// Re-exports — convenience barrel
|
|
54
|
+
// ---------------------------------------------------------------------------
|
|
55
|
+
// Silicon-level re-exports from MCU package (pure silicon)
|
|
56
|
+
export * from '@typecad/mcu-atmega328p';
|
|
57
|
+
// Generic HAL re-exports from @typecad/hal. Full re-export so this board package
|
|
58
|
+
// is a superset of @typecad/hal — `import { ... } from '@typecad/hal'` resolves
|
|
59
|
+
// here at transpile time and exposes every HAL symbol plus board-specific pins.
|
|
60
|
+
export * from '@typecad/hal';
|
|
61
|
+
// Board-level pin Discovery API (using silicon pins from MCU)
|
|
62
|
+
import { PD0, PD1, PD2, PD3, PD4, PD5, PD6, PD7, PB0, PB1, PB2, PB3, PB4, PB5, PC0, PC1, PC2, PC3, PC4, PC5, } from '@typecad/mcu-atmega328p';
|
|
63
|
+
/**
|
|
64
|
+
* Pin collections for runtime capability discovery.
|
|
65
|
+
*/
|
|
66
|
+
export const pins = {
|
|
67
|
+
/** PWM-capable pins: PD3, PD5, PD6, PB1, PB2, PB3 */
|
|
68
|
+
pwm: [PD3, PD5, PD6, PB1, PB2, PB3],
|
|
69
|
+
/** Analog input pins: PC0, PC1, PC2, PC3, PC4, PC5 */
|
|
70
|
+
analog: [PC0, PC1, PC2, PC3, PC4, PC5],
|
|
71
|
+
/** External interrupt-capable pins: PD0, PD1, PD2, PD3 */
|
|
72
|
+
interrupt: [PD0, PD1, PD2, PD3],
|
|
73
|
+
/** All digital I/O pins */
|
|
74
|
+
digital: [PD0, PD1, PD2, PD3, PD4, PD5, PD6, PD7, PB0, PB1, PB2, PB3, PB4, PB5, PC0, PC1, PC2, PC3, PC4, PC5],
|
|
75
|
+
};
|
|
76
|
+
/**
|
|
77
|
+
* Peripheral-to-pin mapping for the Arduino Uno.
|
|
78
|
+
*/
|
|
79
|
+
export const PeripheralPins = {
|
|
80
|
+
/** I2C bus 0 — requires PC4 (SDA) and PC5 (SCL). */
|
|
81
|
+
I2C0: { SDA: 'PC4', SCL: 'PC5' },
|
|
82
|
+
/** SPI bus 0 — requires PB3 (MOSI), PB4 (MISO), PB5 (SCK). PB2 is default CS. */
|
|
83
|
+
SPI0: { MOSI: 'PB3', MISO: 'PB4', SCK: 'PB5', CS: 'PB2' },
|
|
84
|
+
/** UART/Serial 0 — requires PD1 (TX) and PD0 (RX). */
|
|
85
|
+
UART0: { TX: 'PD1', RX: 'PD0' },
|
|
86
|
+
/** UART/Serial 1 — requires PD2 (TX) and PD3 (RX). */
|
|
87
|
+
UART1: { TX: 'PD2', RX: 'PD3' },
|
|
88
|
+
};
|
|
89
|
+
// Board-level typed pins (including Arduino-style aliases D0, A0, etc.)
|
|
90
|
+
export * from './pins.js';
|
|
91
|
+
// Board-specific analog constants
|
|
92
|
+
export * from './analog.js';
|
|
93
|
+
// Board namespace (single-import convenience)
|
|
94
|
+
export { Board } from './board.js';
|
package/dist/pins.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export { PD0, PD1, PD2, PD3, PD4, PD5, PD6, PD7, PB0, PB1, PB2, PB3, PB4, PB5, PC0, PC1, PC2, PC3, PC4, PC5, } from '@typecad/mcu-atmega328p';
|
|
2
|
+
export declare const D0: import("@typecad/hal").Pin;
|
|
3
|
+
export declare const D1: import("@typecad/hal").Pin;
|
|
4
|
+
export declare const D2: import("@typecad/hal").Pin;
|
|
5
|
+
export declare const D3: import("@typecad/hal").Pin;
|
|
6
|
+
export declare const D4: import("@typecad/hal").Pin;
|
|
7
|
+
export declare const D5: import("@typecad/hal").Pin;
|
|
8
|
+
export declare const D6: import("@typecad/hal").Pin;
|
|
9
|
+
export declare const D7: import("@typecad/hal").Pin;
|
|
10
|
+
export declare const D8: import("@typecad/hal").Pin;
|
|
11
|
+
export declare const D9: import("@typecad/hal").Pin;
|
|
12
|
+
export declare const D10: import("@typecad/hal").Pin;
|
|
13
|
+
export declare const D11: import("@typecad/hal").Pin;
|
|
14
|
+
export declare const D12: import("@typecad/hal").Pin;
|
|
15
|
+
export declare const D13: import("@typecad/hal").Pin;
|
|
16
|
+
export declare const A0: import("@typecad/hal").Pin;
|
|
17
|
+
export declare const A1: import("@typecad/hal").Pin;
|
|
18
|
+
export declare const A2: import("@typecad/hal").Pin;
|
|
19
|
+
export declare const A3: import("@typecad/hal").Pin;
|
|
20
|
+
export declare const A4: import("@typecad/hal").Pin;
|
|
21
|
+
export declare const A5: import("@typecad/hal").Pin;
|
|
22
|
+
/** On-board LED (PB5). */
|
|
23
|
+
export declare const LED: import("@typecad/hal").Pin;
|
|
24
|
+
export { SDA, SCL, MOSI, MISO, SCK, SS, TX, RX } from '@typecad/mcu-atmega328p';
|
package/dist/pins.js
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
// ---------------------------------------------------------------------------
|
|
2
|
+
// @typecad/board-arduino-uno — Pin exports
|
|
3
|
+
//
|
|
4
|
+
// Each pin is a Pin instance from @typecad/hal. The transpiler inlines
|
|
5
|
+
// method calls (high(), low(), write(), etc.) as direct Arduino C++.
|
|
6
|
+
// Pin names use MCU port identifiers (PD0, PB5, PC0) matching the
|
|
7
|
+
// ATmega328P datasheet and KiCad symbol library.
|
|
8
|
+
//
|
|
9
|
+
// Port-name pins are imported from the MCU package and re-exported here
|
|
10
|
+
// for backward compatibility. The Pin.fromPort() factory stores both the
|
|
11
|
+
// port name and the resolved Arduino pin number.
|
|
12
|
+
// ---------------------------------------------------------------------------
|
|
13
|
+
// Import datasheet pins from MCU package — these use Pin.fromPort() internally
|
|
14
|
+
// so each Pin carries both its port name (PB5) and Arduino pin number (13).
|
|
15
|
+
export { PD0, PD1, PD2, PD3, PD4, PD5, PD6, PD7, PB0, PB1, PB2, PB3, PB4, PB5, PC0, PC1, PC2, PC3, PC4, PC5, } from '@typecad/mcu-atmega328p';
|
|
16
|
+
// Re-import for local aliasing
|
|
17
|
+
import { PD0, PD1, PD2, PD3, PD4, PD5, PD6, PD7, PB0, PB1, PB2, PB3, PB4, PB5, PC0, PC1, PC2, PC3, PC4, PC5, } from '@typecad/mcu-atmega328p';
|
|
18
|
+
// ---------------------------------------------------------------------------
|
|
19
|
+
// Arduino-style pin aliases (D0–D13, A0–A5)
|
|
20
|
+
// ---------------------------------------------------------------------------
|
|
21
|
+
export const D0 = PD0;
|
|
22
|
+
export const D1 = PD1;
|
|
23
|
+
export const D2 = PD2;
|
|
24
|
+
export const D3 = PD3;
|
|
25
|
+
export const D4 = PD4;
|
|
26
|
+
export const D5 = PD5;
|
|
27
|
+
export const D6 = PD6;
|
|
28
|
+
export const D7 = PD7;
|
|
29
|
+
export const D8 = PB0;
|
|
30
|
+
export const D9 = PB1;
|
|
31
|
+
export const D10 = PB2;
|
|
32
|
+
export const D11 = PB3;
|
|
33
|
+
export const D12 = PB4;
|
|
34
|
+
export const D13 = PB5;
|
|
35
|
+
export const A0 = PC0;
|
|
36
|
+
export const A1 = PC1;
|
|
37
|
+
export const A2 = PC2;
|
|
38
|
+
export const A3 = PC3;
|
|
39
|
+
export const A4 = PC4;
|
|
40
|
+
export const A5 = PC5;
|
|
41
|
+
// ---------------------------------------------------------------------------
|
|
42
|
+
// Convenience aliases
|
|
43
|
+
// ---------------------------------------------------------------------------
|
|
44
|
+
/** On-board LED (PB5). */
|
|
45
|
+
export const LED = PB5;
|
|
46
|
+
// Re-export silicon-level aliases (SDA, MOSI, etc.) from MCU package
|
|
47
|
+
export { SDA, SCL, MOSI, MISO, SCK, SS, TX, RX } from '@typecad/mcu-atmega328p';
|
package/package.json
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@typecad/board-arduino-uno",
|
|
3
|
+
"version": "0.1.0-alpha.1",
|
|
4
|
+
"description": "TypeCAD Arduino Uno board definition with typed pins and peripherals",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"files": [
|
|
9
|
+
"dist",
|
|
10
|
+
"src"
|
|
11
|
+
],
|
|
12
|
+
"scripts": {
|
|
13
|
+
"build": "tsc",
|
|
14
|
+
"prepublishOnly": "npm run build"
|
|
15
|
+
},
|
|
16
|
+
"dependencies": {
|
|
17
|
+
"@typecad/cuttlefish": "0.1.0-alpha.1",
|
|
18
|
+
"@typecad/hal": "0.1.0-alpha.1",
|
|
19
|
+
"@typecad/mcu-atmega328p": "0.1.0-alpha.1"
|
|
20
|
+
},
|
|
21
|
+
"license": "MIT",
|
|
22
|
+
"publishConfig": {
|
|
23
|
+
"access": "public"
|
|
24
|
+
},
|
|
25
|
+
"repository": {
|
|
26
|
+
"type": "git",
|
|
27
|
+
"url": "git+https://github.com/justind000/typecode.git",
|
|
28
|
+
"directory": "packages/board-arduino-uno"
|
|
29
|
+
},
|
|
30
|
+
"homepage": "https://github.com/justind000/typecode/tree/main/packages/board-arduino-uno",
|
|
31
|
+
"bugs": {
|
|
32
|
+
"url": "https://github.com/justind000/typecode/issues"
|
|
33
|
+
},
|
|
34
|
+
"keywords": [
|
|
35
|
+
"arduino",
|
|
36
|
+
"arduino-uno",
|
|
37
|
+
"board",
|
|
38
|
+
"cpp",
|
|
39
|
+
"cuttlefish",
|
|
40
|
+
"devkit",
|
|
41
|
+
"embedded",
|
|
42
|
+
"firmware",
|
|
43
|
+
"microcontroller",
|
|
44
|
+
"pins",
|
|
45
|
+
"typecad",
|
|
46
|
+
"typescript"
|
|
47
|
+
],
|
|
48
|
+
"engines": {
|
|
49
|
+
"node": ">=18"
|
|
50
|
+
},
|
|
51
|
+
"author": "typecad0",
|
|
52
|
+
"sideEffects": false,
|
|
53
|
+
"exports": {
|
|
54
|
+
".": {
|
|
55
|
+
"types": "./dist/index.d.ts",
|
|
56
|
+
"default": "./dist/index.js"
|
|
57
|
+
},
|
|
58
|
+
"./package.json": "./package.json"
|
|
59
|
+
}
|
|
60
|
+
}
|
package/src/USAGE.md
ADDED
|
@@ -0,0 +1,485 @@
|
|
|
1
|
+
# Arduino Uno TypeCAD SDK — Usage Guide
|
|
2
|
+
|
|
3
|
+
The `@typecad/board-arduino-uno` package provides a fully-typed TypeScript
|
|
4
|
+
SDK for the Arduino Uno (ATmega328P). Every pin, peripheral, and timing
|
|
5
|
+
function carries rich type information so that TypeScript catches hardware
|
|
6
|
+
errors **at compile time** — before you flash anything.
|
|
7
|
+
|
|
8
|
+
---
|
|
9
|
+
|
|
10
|
+
## Quick Start
|
|
11
|
+
|
|
12
|
+
```typescript
|
|
13
|
+
// cuttlefish.config.ts
|
|
14
|
+
import type { CuttlefishConfig } from '@typecad/cuttlefish/api';
|
|
15
|
+
|
|
16
|
+
const config: CuttlefishConfig = {
|
|
17
|
+
entry: './src/main.ts',
|
|
18
|
+
target: 'avr',
|
|
19
|
+
mcu: '@typecad/mcu-atmega328p',
|
|
20
|
+
board: '@typecad/board-arduino-uno',
|
|
21
|
+
framework: '@typecad/framework-arduino',
|
|
22
|
+
output: { framework: 'arduino', optimize: 'size' },
|
|
23
|
+
};
|
|
24
|
+
export default config;
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Then create your firmware in TypeScript and transpile:
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
npx tsc -p tsconfig.json # build the transpiler
|
|
31
|
+
node dist/cli.js transpile main.ts \
|
|
32
|
+
--target arduino --fqbn arduino:avr:uno # TS → C++
|
|
33
|
+
arduino-cli compile --fqbn arduino:avr:uno main # compile to hex
|
|
34
|
+
arduino-cli upload --fqbn arduino:avr:uno -p COM3 main # flash
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
---
|
|
38
|
+
|
|
39
|
+
## Importing
|
|
40
|
+
|
|
41
|
+
There are two import styles — pick whichever you prefer.
|
|
42
|
+
|
|
43
|
+
### Style 1: Individual imports (tree-shakeable)
|
|
44
|
+
|
|
45
|
+
```typescript
|
|
46
|
+
import { D13, A0 } from '@typecad/board-arduino-uno/pins';
|
|
47
|
+
import { UART0 } from '@typecad/board-arduino-uno/peripherals';
|
|
48
|
+
import { delay, millis } from '@typecad/board-arduino-uno/timing';
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
### Style 2: Unified `Board` namespace
|
|
52
|
+
|
|
53
|
+
```typescript
|
|
54
|
+
import { Board } from '@typecad/board-arduino-uno/board';
|
|
55
|
+
|
|
56
|
+
Board.D13.high();
|
|
57
|
+
Board.UART0.println("Hello");
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### Style 3: Barrel import (everything)
|
|
61
|
+
|
|
62
|
+
```typescript
|
|
63
|
+
import {
|
|
64
|
+
D13, A0, LED, UART0, delay, millis,
|
|
65
|
+
I2C0, SPI0, Board,
|
|
66
|
+
} from '@typecad/board-arduino-uno';
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
### Style 4: Virtual `@typecad/board` import (recommended)
|
|
70
|
+
|
|
71
|
+
```typescript
|
|
72
|
+
import { D13, A0, LED, UART0, delay, millis, I2C0, SPI0, Board } from '@typecad/board';
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
---
|
|
76
|
+
|
|
77
|
+
## Pin System
|
|
78
|
+
|
|
79
|
+
### Pin types at a glance
|
|
80
|
+
|
|
81
|
+
| Export | TypeScript Type | Capabilities |
|
|
82
|
+
|--------|----------------|--------------|
|
|
83
|
+
| `D0`, `D1` | `IDigitalPin & IInterruptPin` | Digital I/O + external interrupt |
|
|
84
|
+
| `D2` | `IDigitalPin & IInterruptPin` | Digital I/O + INT0 |
|
|
85
|
+
| `D3` | `IPWMPin` | Digital I/O + PWM (Timer2B) + INT1 |
|
|
86
|
+
| `D4`, `D7`, `D8`, `D12` | `IDigitalPin` | Digital I/O only |
|
|
87
|
+
| `D5`, `D6` | `IPWMPin` | Digital I/O + PWM (Timer0) |
|
|
88
|
+
| `D9`, `D10`, `D11` | `IPWMPin` | Digital I/O + PWM (Timer1/2) |
|
|
89
|
+
| `D13` | `IDigitalPin` | Digital I/O (+ onboard LED) |
|
|
90
|
+
| `A0`–`A5` | `IAnalogInput` | 10-bit ADC input |
|
|
91
|
+
|
|
92
|
+
### Convenience Aliases
|
|
93
|
+
|
|
94
|
+
| Alias | Points to | Function |
|
|
95
|
+
|-------|-----------|----------|
|
|
96
|
+
| `LED` | `D13` | Onboard LED |
|
|
97
|
+
| `SDA` | `A4` | I2C data |
|
|
98
|
+
| `SCL` | `A5` | I2C clock |
|
|
99
|
+
| `MOSI` | `D11` | SPI master out |
|
|
100
|
+
| `MISO` | `D12` | SPI master in |
|
|
101
|
+
| `SCK` | `D13` | SPI clock |
|
|
102
|
+
| `SS` | `D10` | SPI slave select |
|
|
103
|
+
| `TX` | `D1` | UART transmit |
|
|
104
|
+
| `RX` | `D0` | UART receive |
|
|
105
|
+
|
|
106
|
+
### Alias And Timer Notes
|
|
107
|
+
|
|
108
|
+
- `LED` and `D13` are the same physical pin. Use one name consistently.
|
|
109
|
+
- `SDA/A4`, `SCL/A5`, `TX/D1`, and `RX/D0` follow the same rule.
|
|
110
|
+
- PWM pins are grouped by timer on Uno: `D5/D6` share `timer0`, `D9/D10` share `timer1`, and `D3/D11` share `timer2`.
|
|
111
|
+
|
|
112
|
+
TypeCAD surfaces both conditions as diagnostics so the schematic does not stay hidden until runtime.
|
|
113
|
+
|
|
114
|
+
### Type Safety in Action
|
|
115
|
+
|
|
116
|
+
```typescript
|
|
117
|
+
import { D4, A0 } from '@typecad/board-arduino-uno/pins';
|
|
118
|
+
|
|
119
|
+
// ✅ OK — D4 is IDigitalPin, supports .high()
|
|
120
|
+
D4.high();
|
|
121
|
+
|
|
122
|
+
// ✅ OK — A0 is IAnalogInput, supports .readAnalog()
|
|
123
|
+
const value = A0.readAnalog();
|
|
124
|
+
|
|
125
|
+
// ❌ COMPILE ERROR — IAnalogInput has no .high() method
|
|
126
|
+
A0.high(); // Property 'high' does not exist on type 'IAnalogInput'
|
|
127
|
+
|
|
128
|
+
// ❌ COMPILE ERROR — IDigitalPin has no .pwm()
|
|
129
|
+
D4.pwm(50); // Property 'pwm' does not exist on type 'IDigitalPin'
|
|
130
|
+
|
|
131
|
+
// ✅ OK — D5 is IPWMPin, supports .pwm()
|
|
132
|
+
import { D5 } from '@typecad/board-arduino-uno/pins';
|
|
133
|
+
D5.pwm(50);
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
---
|
|
137
|
+
|
|
138
|
+
## Examples
|
|
139
|
+
|
|
140
|
+
Each example below is a complete, self-contained `.ts` file that the
|
|
141
|
+
transpiler converts to a valid `.ino` sketch.
|
|
142
|
+
|
|
143
|
+
### 1. Blink (Hello World)
|
|
144
|
+
|
|
145
|
+
```typescript
|
|
146
|
+
// examples/01-blink.ts
|
|
147
|
+
import { LED, delay, HIGH } from '@typecad/board';
|
|
148
|
+
|
|
149
|
+
LED.output(HIGH);
|
|
150
|
+
|
|
151
|
+
while (true) {
|
|
152
|
+
LED.toggle();
|
|
153
|
+
delay(1000);
|
|
154
|
+
}
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
**Generated C++:**
|
|
158
|
+
```cpp
|
|
159
|
+
void setup() {
|
|
160
|
+
pinMode(13, OUTPUT);
|
|
161
|
+
}
|
|
162
|
+
void loop() {
|
|
163
|
+
digitalWrite(13, !digitalRead(13));
|
|
164
|
+
delay(1000);
|
|
165
|
+
}
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
---
|
|
169
|
+
|
|
170
|
+
### 2. Analog Read → Serial
|
|
171
|
+
|
|
172
|
+
```typescript
|
|
173
|
+
// examples/02-analog-serial.ts
|
|
174
|
+
import { A0, UART0, delay } from '@typecad/board';
|
|
175
|
+
|
|
176
|
+
UART0.begin(9600);
|
|
177
|
+
|
|
178
|
+
while (true) {
|
|
179
|
+
const value = A0.readAnalog();
|
|
180
|
+
UART0.println(value.toString());
|
|
181
|
+
delay(500);
|
|
182
|
+
}
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
---
|
|
186
|
+
|
|
187
|
+
### 3. PWM Fade
|
|
188
|
+
|
|
189
|
+
```typescript
|
|
190
|
+
// examples/03-pwm-fade.ts
|
|
191
|
+
import { D9, delay, LOW } from '@typecad/board';
|
|
192
|
+
|
|
193
|
+
D9.output(LOW);
|
|
194
|
+
|
|
195
|
+
let brightness = 0;
|
|
196
|
+
let step = 5;
|
|
197
|
+
|
|
198
|
+
while (true) {
|
|
199
|
+
D9.pwm(brightness / 2.55);
|
|
200
|
+
brightness += step;
|
|
201
|
+
if (brightness <= 0 || brightness >= 255) {
|
|
202
|
+
step = -step;
|
|
203
|
+
}
|
|
204
|
+
delay(30);
|
|
205
|
+
}
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
---
|
|
209
|
+
|
|
210
|
+
### 4. External Interrupt (Button)
|
|
211
|
+
|
|
212
|
+
```typescript
|
|
213
|
+
// examples/04-interrupt.ts
|
|
214
|
+
import { D2, LED, LOW } from '@typecad/board';
|
|
215
|
+
|
|
216
|
+
LED.output(LOW);
|
|
217
|
+
D2.inputPullUp();
|
|
218
|
+
|
|
219
|
+
let ledState = false;
|
|
220
|
+
|
|
221
|
+
D2.onFalling(() => {
|
|
222
|
+
ledState = !ledState;
|
|
223
|
+
if (ledState) {
|
|
224
|
+
LED.high();
|
|
225
|
+
} else {
|
|
226
|
+
LED.low();
|
|
227
|
+
}
|
|
228
|
+
});
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
---
|
|
232
|
+
|
|
233
|
+
### 5. I2C — Read From a Sensor
|
|
234
|
+
|
|
235
|
+
```typescript
|
|
236
|
+
// examples/05-i2c-sensor.ts
|
|
237
|
+
import { I2C0, UART0, delay } from '@typecad/board';
|
|
238
|
+
|
|
239
|
+
UART0.begin(9600);
|
|
240
|
+
I2C0.begin(); // Wire.begin() - master mode
|
|
241
|
+
|
|
242
|
+
const BME280_ADDR = 0x76;
|
|
243
|
+
|
|
244
|
+
while (true) {
|
|
245
|
+
// Read 2 bytes from register 0xFA (temperature data)
|
|
246
|
+
const tempData = I2C0.device(BME280_ADDR).readBytes(0xFA, 2);
|
|
247
|
+
const msb = tempData[0];
|
|
248
|
+
const lsb = tempData[1];
|
|
249
|
+
const tempRaw = (msb << 8) | lsb;
|
|
250
|
+
const temperature = tempRaw / 100.0;
|
|
251
|
+
|
|
252
|
+
UART0.println(temperature.toString());
|
|
253
|
+
delay(1000);
|
|
254
|
+
}
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
---
|
|
258
|
+
|
|
259
|
+
### 6. SPI — Write to a Shift Register
|
|
260
|
+
|
|
261
|
+
```typescript
|
|
262
|
+
// examples/06-spi-shift-register.ts
|
|
263
|
+
import { SPI0, SS, delay, LOW } from '@typecad/board';
|
|
264
|
+
|
|
265
|
+
SPI0.begin();
|
|
266
|
+
SPI0.setFrequency(1_000_000);
|
|
267
|
+
SS.output(LOW);
|
|
268
|
+
|
|
269
|
+
let pattern = 0b00000001;
|
|
270
|
+
|
|
271
|
+
while (true) {
|
|
272
|
+
SPI0.device(SS).write(pattern);
|
|
273
|
+
|
|
274
|
+
// rotate left
|
|
275
|
+
pattern = ((pattern << 1) | (pattern >> 7)) & 0xFF;
|
|
276
|
+
delay(200);
|
|
277
|
+
}
|
|
278
|
+
```
|
|
279
|
+
|
|
280
|
+
---
|
|
281
|
+
|
|
282
|
+
### 7. Board Namespace (All-In-One)
|
|
283
|
+
|
|
284
|
+
```typescript
|
|
285
|
+
// examples/07-board-namespace.ts
|
|
286
|
+
import { Board, LOW } from '@typecad/board';
|
|
287
|
+
|
|
288
|
+
Board.UART0.begin(115200);
|
|
289
|
+
Board.LED.output(LOW);
|
|
290
|
+
|
|
291
|
+
Board.UART0.println("Arduino Uno booted");
|
|
292
|
+
Board.UART0.println("MCU: " + Board.definition.mcu);
|
|
293
|
+
Board.UART0.println("Flash: " + Board.definition.memory.flash + " bytes");
|
|
294
|
+
|
|
295
|
+
while (true) {
|
|
296
|
+
const sensor = Board.A0.readAnalog();
|
|
297
|
+
Board.UART0.println(sensor.toString());
|
|
298
|
+
Board.LED.toggle();
|
|
299
|
+
}
|
|
300
|
+
```
|
|
301
|
+
|
|
302
|
+
---
|
|
303
|
+
|
|
304
|
+
## Peripheral Reference
|
|
305
|
+
|
|
306
|
+
### Serial (UART 0)
|
|
307
|
+
|
|
308
|
+
```typescript
|
|
309
|
+
import { UART0 } from '@typecad/board';
|
|
310
|
+
|
|
311
|
+
UART0.begin(9600);
|
|
312
|
+
UART0.println("Hello, World!");
|
|
313
|
+
UART0.print("Value: ");
|
|
314
|
+
UART0.println(42);
|
|
315
|
+
UART0.flush(); // wait for transmit buffer to empty
|
|
316
|
+
```
|
|
317
|
+
|
|
318
|
+
### I2C0 (Wire)
|
|
319
|
+
|
|
320
|
+
```typescript
|
|
321
|
+
import { I2C0, UART0 } from '@typecad/board';
|
|
322
|
+
|
|
323
|
+
// Initialize as master
|
|
324
|
+
I2C0.begin(); // 100 kHz (default)
|
|
325
|
+
I2C0.setClock(400000); // optionally switch to 400 kHz fast mode
|
|
326
|
+
|
|
327
|
+
const DEVICE_ADDR = 0x76;
|
|
328
|
+
|
|
329
|
+
// Read bytes from a register
|
|
330
|
+
const data = I2C0.device(DEVICE_ADDR).readBytes(0xFA, 2);
|
|
331
|
+
UART0.println(`Received: ${data[0]}, ${data[1]}`);
|
|
332
|
+
|
|
333
|
+
// Write bytes to a register
|
|
334
|
+
I2C0.device(DEVICE_ADDR).writeByte(0xF4, 0x27);
|
|
335
|
+
```
|
|
336
|
+
|
|
337
|
+
#### Wire-Compatible API
|
|
338
|
+
|
|
339
|
+
This API directly maps to Arduino's Wire library:
|
|
340
|
+
|
|
341
|
+
```typescript
|
|
342
|
+
import { I2C0 } from '@typecad/board';
|
|
343
|
+
import { I2CStatus } from '@typecad/hal';
|
|
344
|
+
|
|
345
|
+
// Initialize as master
|
|
346
|
+
I2C0.begin();
|
|
347
|
+
|
|
348
|
+
// Set clock speed (optional, default 100kHz)
|
|
349
|
+
I2C0.setClock(400000); // 400 kHz fast mode
|
|
350
|
+
|
|
351
|
+
// Write to a device with error checking
|
|
352
|
+
I2C0.beginTransmission(0x68); // MPU-6050 address
|
|
353
|
+
I2C0.write(0x6B); // PWR_MGMT_1 register
|
|
354
|
+
I2C0.write(0x00); // wake up
|
|
355
|
+
const status = I2C0.endTransmission();
|
|
356
|
+
|
|
357
|
+
// Check status (I2CStatus enum)
|
|
358
|
+
if (status === I2CStatus.SUCCESS) {
|
|
359
|
+
// Write succeeded
|
|
360
|
+
} else if (status === I2CStatus.NACK_ON_ADDRESS) {
|
|
361
|
+
// Device not responding
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
// Read from a device
|
|
365
|
+
I2C0.beginTransmission(0x68);
|
|
366
|
+
I2C0.write(0x75); // WHO_AM_I register
|
|
367
|
+
I2C0.endTransmission();
|
|
368
|
+
|
|
369
|
+
const bytesReceived = I2C0.requestFrom(0x68, 1);
|
|
370
|
+
if (bytesReceived > 0) {
|
|
371
|
+
const whoAmI = I2C0.read();
|
|
372
|
+
}
|
|
373
|
+
```
|
|
374
|
+
|
|
375
|
+
#### Error Codes (I2CStatus)
|
|
376
|
+
|
|
377
|
+
| Status | Value | Description |
|
|
378
|
+
|--------|-------|-------------|
|
|
379
|
+
| `SUCCESS` | 0 | Operation completed successfully |
|
|
380
|
+
| `DATA_TOO_LONG` | 1 | Transmit buffer overflow |
|
|
381
|
+
| `NACK_ON_ADDRESS` | 2 | NACK received on address (device not found) |
|
|
382
|
+
| `NACK_ON_DATA` | 3 | NACK received on data byte |
|
|
383
|
+
| `OTHER_ERROR` | 4 | Other error |
|
|
384
|
+
| `PARTIAL_READ` | 5 | Fewer bytes read than requested |
|
|
385
|
+
|
|
386
|
+
### SPI0
|
|
387
|
+
|
|
388
|
+
```typescript
|
|
389
|
+
import { SPI0, SS, D10 } from '@typecad/board';
|
|
390
|
+
|
|
391
|
+
// Initialize and configure
|
|
392
|
+
SPI0.begin();
|
|
393
|
+
SPI0.setFrequency(4_000_000);
|
|
394
|
+
SPI0.setMode(0);
|
|
395
|
+
SPI0.setBitOrder('msb');
|
|
396
|
+
|
|
397
|
+
// Chip select pin
|
|
398
|
+
const CS = D10;
|
|
399
|
+
CS.output(HIGH); // HIGH = deselected
|
|
400
|
+
|
|
401
|
+
// Single byte transfer with fluent device API
|
|
402
|
+
const response = SPI0.device(CS).transfer(0x55);
|
|
403
|
+
|
|
404
|
+
// Write data to device
|
|
405
|
+
SPI0.device(CS).write(0xFF);
|
|
406
|
+
|
|
407
|
+
// Multi-byte transfer
|
|
408
|
+
const rxData = SPI0.device(CS).transfer(new Uint8Array([0x80, 0x00, 0xFF]));
|
|
409
|
+
```
|
|
410
|
+
|
|
411
|
+
---
|
|
412
|
+
|
|
413
|
+
## Timing Functions
|
|
414
|
+
|
|
415
|
+
```typescript
|
|
416
|
+
import { delay, millis, micros, delayMicroseconds } from '@typecad/board';
|
|
417
|
+
|
|
418
|
+
delay(1000); // block 1 second
|
|
419
|
+
delayMicroseconds(10); // block 10 µs
|
|
420
|
+
|
|
421
|
+
const t = millis(); // ms since reset
|
|
422
|
+
const us = micros(); // µs since reset
|
|
423
|
+
```
|
|
424
|
+
|
|
425
|
+
---
|
|
426
|
+
|
|
427
|
+
## Utility Functions
|
|
428
|
+
|
|
429
|
+
```typescript
|
|
430
|
+
import { map, constrain } from '@typecad/board';
|
|
431
|
+
|
|
432
|
+
// Re-map a 10-bit ADC reading (0–1023) to an 8-bit PWM range (0–255)
|
|
433
|
+
const pwmValue = map(sensorReading, 0, 1023, 0, 255);
|
|
434
|
+
|
|
435
|
+
// Clamp a value to the valid PWM range
|
|
436
|
+
const clamped = constrain(pwmValue, 0, 255);
|
|
437
|
+
```
|
|
438
|
+
|
|
439
|
+
---
|
|
440
|
+
|
|
441
|
+
## Board Definition Metadata
|
|
442
|
+
|
|
443
|
+
The `ArduinoUno` constant (or `Board.definition`) exposes the full hardware
|
|
444
|
+
manifest at design time.
|
|
445
|
+
|
|
446
|
+
```typescript
|
|
447
|
+
import { ArduinoUno, Board } from '@typecad/board';
|
|
448
|
+
|
|
449
|
+
// Access via ArduinoUno export
|
|
450
|
+
console.log(ArduinoUno.name); // "Arduino Uno"
|
|
451
|
+
console.log(ArduinoUno.mcu); // "ATmega328P"
|
|
452
|
+
console.log(ArduinoUno.clockSpeed); // 16000000
|
|
453
|
+
console.log(ArduinoUno.memory.flash); // 32768
|
|
454
|
+
console.log(ArduinoUno.memory.sram); // 2048
|
|
455
|
+
console.log(ArduinoUno.memory.eeprom); // 1024
|
|
456
|
+
console.log(ArduinoUno.pins.pwm); // ["D3","D5","D6","D9","D10","D11"]
|
|
457
|
+
console.log(ArduinoUno.features.watchdog);// true
|
|
458
|
+
console.log(ArduinoUno.build.frameworks.arduino); // "arduino:avr:uno"
|
|
459
|
+
|
|
460
|
+
// Or via Board namespace
|
|
461
|
+
console.log(Board.definition.name); // "Arduino Uno"
|
|
462
|
+
```
|
|
463
|
+
|
|
464
|
+
---
|
|
465
|
+
|
|
466
|
+
## Project Structure (Recommended)
|
|
467
|
+
|
|
468
|
+
```
|
|
469
|
+
my-project/
|
|
470
|
+
├── cuttlefish.config.ts ← board + target selection
|
|
471
|
+
├── main.ts ← your firmware
|
|
472
|
+
├── lib/
|
|
473
|
+
│ └── bme280.ts ← reusable driver (uses core interfaces)
|
|
474
|
+
├── code/
|
|
475
|
+
│ ├── core/ ← @typecad/cuttlefish type system
|
|
476
|
+
│ └── board-arduino-uno/ ← board SDK
|
|
477
|
+
│ ├── index.ts ← barrel export + ArduinoUno manifest
|
|
478
|
+
│ ├── board.ts ← Board namespace (single-import)
|
|
479
|
+
│ ├── pins.ts ← Typed pin exports (D0-D13, A0-A5)
|
|
480
|
+
│ ├── peripherals.ts ← I2C0, SPI0, Serial stubs
|
|
481
|
+
│ ├── timing.ts ← delay, millis, micros
|
|
482
|
+
│ ├── analog.ts ← analogReference
|
|
483
|
+
│ └── interrupts.ts ← noInterrupts, attachInterrupt
|
|
484
|
+
└── out/ ← generated .ino files
|
|
485
|
+
```
|
package/src/analog.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
// ---------------------------------------------------------------------------
|
|
2
|
+
// @typecad/board-arduino-uno — Analog constants
|
|
3
|
+
// ---------------------------------------------------------------------------
|
|
4
|
+
|
|
5
|
+
/** Default reference (VCC). */
|
|
6
|
+
export const DEFAULT = 0;
|
|
7
|
+
/** Internal 1.1V reference. */
|
|
8
|
+
export const INTERNAL = 3;
|
|
9
|
+
/** External AREF pin. */
|
|
10
|
+
export const EXTERNAL = 1;
|
package/src/board.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
// ---------------------------------------------------------------------------
|
|
2
|
+
// @typecad/board-arduino-uno — Board namespace
|
|
3
|
+
//
|
|
4
|
+
// Convenience namespace that exposes every board feature under one object.
|
|
5
|
+
// For method calls on pins/peripherals, prefer direct imports:
|
|
6
|
+
//
|
|
7
|
+
// import { D13, UART0 } from '@typecad/board-arduino-uno';
|
|
8
|
+
// D13.high();
|
|
9
|
+
// UART0.begin(115200);
|
|
10
|
+
//
|
|
11
|
+
// The Board object is useful for pin iteration and metadata access.
|
|
12
|
+
// ---------------------------------------------------------------------------
|
|
13
|
+
|
|
14
|
+
import type { BoardDefinition } from '@typecad/cuttlefish/api/schema';
|
|
15
|
+
|
|
16
|
+
import {
|
|
17
|
+
D0, D1, D2, D3, D4, D5, D6, D7,
|
|
18
|
+
D8, D9, D10, D11, D12, D13,
|
|
19
|
+
A0, A1, A2, A3, A4, A5,
|
|
20
|
+
LED, SDA, SCL, MOSI, MISO, SCK, SS, TX, RX,
|
|
21
|
+
} from './pins.js';
|
|
22
|
+
|
|
23
|
+
import { I2C0, SPI0, UART0 } from '@typecad/mcu-atmega328p';
|
|
24
|
+
import * as boardIndex from './index.js';
|
|
25
|
+
|
|
26
|
+
export const Board = {
|
|
27
|
+
get definition() { return boardIndex.ArduinoUno; },
|
|
28
|
+
|
|
29
|
+
D0, D1, D2, D3, D4, D5, D6, D7,
|
|
30
|
+
D8, D9, D10, D11, D12, D13,
|
|
31
|
+
A0, A1, A2, A3, A4, A5,
|
|
32
|
+
|
|
33
|
+
LED, SDA, SCL, MOSI, MISO, SCK, SS, TX, RX,
|
|
34
|
+
|
|
35
|
+
I2C0, SPI0, UART0,
|
|
36
|
+
|
|
37
|
+
digital: { D0, D1, D2, D3, D4, D5, D6, D7, D8, D9, D10, D11, D12, D13 },
|
|
38
|
+
analog: { A0, A1, A2, A3, A4, A5 },
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
export default Board;
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import type { BoardDefinition } from '@typecad/cuttlefish/api/schema';
|
|
2
|
+
import { ATmega328P } from '@typecad/mcu-atmega328p';
|
|
3
|
+
|
|
4
|
+
// ---------------------------------------------------------------------------
|
|
5
|
+
// Board definition
|
|
6
|
+
// ---------------------------------------------------------------------------
|
|
7
|
+
|
|
8
|
+
export const ArduinoUno: BoardDefinition = {
|
|
9
|
+
id: 'arduino-uno',
|
|
10
|
+
name: 'Arduino Uno',
|
|
11
|
+
vendor: 'Arduino',
|
|
12
|
+
description: 'Arduino Uno Rev3 — ATmega328P',
|
|
13
|
+
|
|
14
|
+
mcu: ATmega328P,
|
|
15
|
+
clockSpeed: 16_000_000, // 16 MHz
|
|
16
|
+
|
|
17
|
+
// ----- Pins --------------------------------------------------------------
|
|
18
|
+
pins: {
|
|
19
|
+
...ATmega328P.pins,
|
|
20
|
+
all: ATmega328P.pins.all.map(p => {
|
|
21
|
+
// Add board-specific metadata to PB5 (LED)
|
|
22
|
+
if (p.name === 'PB5') {
|
|
23
|
+
return {
|
|
24
|
+
...p,
|
|
25
|
+
aliases: [...(p.aliases || []), 'LED'],
|
|
26
|
+
onboardLed: true,
|
|
27
|
+
alternateFunctions: [...(p.alternateFunctions || []), 'On-board LED'],
|
|
28
|
+
warnings: [...(p.warnings || []), 'PB5 is also the on-board LED — using as GPIO conflicts with SPI0 SCK']
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
return p;
|
|
32
|
+
}),
|
|
33
|
+
led: 'PB5',
|
|
34
|
+
},
|
|
35
|
+
|
|
36
|
+
// ----- Peripherals -------------------------------------------------------
|
|
37
|
+
peripherals: {
|
|
38
|
+
...ATmega328P.peripherals,
|
|
39
|
+
aliases: {
|
|
40
|
+
UART0: 'Serial',
|
|
41
|
+
I2C0: 'Wire',
|
|
42
|
+
SPI0: 'SPI',
|
|
43
|
+
},
|
|
44
|
+
},
|
|
45
|
+
|
|
46
|
+
// ----- Build config ------------------------------------------------------
|
|
47
|
+
build: {
|
|
48
|
+
frameworks: {
|
|
49
|
+
platformio: 'uno',
|
|
50
|
+
arduino: 'arduino:avr:uno',
|
|
51
|
+
},
|
|
52
|
+
defines: {
|
|
53
|
+
F_CPU: '16000000UL',
|
|
54
|
+
ARDUINO_AVR_UNO: '1',
|
|
55
|
+
},
|
|
56
|
+
},
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
export default ArduinoUno;
|
|
60
|
+
|
|
61
|
+
// ---------------------------------------------------------------------------
|
|
62
|
+
// Re-exports — convenience barrel
|
|
63
|
+
// ---------------------------------------------------------------------------
|
|
64
|
+
|
|
65
|
+
// Silicon-level re-exports from MCU package (pure silicon)
|
|
66
|
+
export * from '@typecad/mcu-atmega328p';
|
|
67
|
+
|
|
68
|
+
// Generic HAL re-exports from @typecad/hal. Full re-export so this board package
|
|
69
|
+
// is a superset of @typecad/hal — `import { ... } from '@typecad/hal'` resolves
|
|
70
|
+
// here at transpile time and exposes every HAL symbol plus board-specific pins.
|
|
71
|
+
export * from '@typecad/hal';
|
|
72
|
+
|
|
73
|
+
// Board-level pin Discovery API (using silicon pins from MCU)
|
|
74
|
+
import {
|
|
75
|
+
PD0, PD1, PD2, PD3, PD4, PD5, PD6, PD7,
|
|
76
|
+
PB0, PB1, PB2, PB3, PB4, PB5,
|
|
77
|
+
PC0, PC1, PC2, PC3, PC4, PC5,
|
|
78
|
+
} from '@typecad/mcu-atmega328p';
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Pin collections for runtime capability discovery.
|
|
82
|
+
*/
|
|
83
|
+
export const pins = {
|
|
84
|
+
/** PWM-capable pins: PD3, PD5, PD6, PB1, PB2, PB3 */
|
|
85
|
+
pwm: [PD3, PD5, PD6, PB1, PB2, PB3] as const,
|
|
86
|
+
/** Analog input pins: PC0, PC1, PC2, PC3, PC4, PC5 */
|
|
87
|
+
analog: [PC0, PC1, PC2, PC3, PC4, PC5] as const,
|
|
88
|
+
/** External interrupt-capable pins: PD0, PD1, PD2, PD3 */
|
|
89
|
+
interrupt: [PD0, PD1, PD2, PD3] as const,
|
|
90
|
+
/** All digital I/O pins */
|
|
91
|
+
digital: [PD0, PD1, PD2, PD3, PD4, PD5, PD6, PD7, PB0, PB1, PB2, PB3, PB4, PB5, PC0, PC1, PC2, PC3, PC4, PC5] as const,
|
|
92
|
+
} as const;
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Peripheral-to-pin mapping for the Arduino Uno.
|
|
96
|
+
*/
|
|
97
|
+
export const PeripheralPins = {
|
|
98
|
+
/** I2C bus 0 — requires PC4 (SDA) and PC5 (SCL). */
|
|
99
|
+
I2C0: { SDA: 'PC4', SCL: 'PC5' } as const,
|
|
100
|
+
/** SPI bus 0 — requires PB3 (MOSI), PB4 (MISO), PB5 (SCK). PB2 is default CS. */
|
|
101
|
+
SPI0: { MOSI: 'PB3', MISO: 'PB4', SCK: 'PB5', CS: 'PB2' } as const,
|
|
102
|
+
/** UART/Serial 0 — requires PD1 (TX) and PD0 (RX). */
|
|
103
|
+
UART0: { TX: 'PD1', RX: 'PD0' } as const,
|
|
104
|
+
/** UART/Serial 1 — requires PD2 (TX) and PD3 (RX). */
|
|
105
|
+
UART1: { TX: 'PD2', RX: 'PD3' } as const,
|
|
106
|
+
} as const;
|
|
107
|
+
|
|
108
|
+
// Board-level typed pins (including Arduino-style aliases D0, A0, etc.)
|
|
109
|
+
export * from './pins.js';
|
|
110
|
+
|
|
111
|
+
// Board-specific analog constants
|
|
112
|
+
export * from './analog.js';
|
|
113
|
+
|
|
114
|
+
// Board namespace (single-import convenience)
|
|
115
|
+
export { Board } from './board.js';
|
package/src/pins.ts
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
// ---------------------------------------------------------------------------
|
|
2
|
+
// @typecad/board-arduino-uno — Pin exports
|
|
3
|
+
//
|
|
4
|
+
// Each pin is a Pin instance from @typecad/hal. The transpiler inlines
|
|
5
|
+
// method calls (high(), low(), write(), etc.) as direct Arduino C++.
|
|
6
|
+
// Pin names use MCU port identifiers (PD0, PB5, PC0) matching the
|
|
7
|
+
// ATmega328P datasheet and KiCad symbol library.
|
|
8
|
+
//
|
|
9
|
+
// Port-name pins are imported from the MCU package and re-exported here
|
|
10
|
+
// for backward compatibility. The Pin.fromPort() factory stores both the
|
|
11
|
+
// port name and the resolved Arduino pin number.
|
|
12
|
+
// ---------------------------------------------------------------------------
|
|
13
|
+
|
|
14
|
+
// Import datasheet pins from MCU package — these use Pin.fromPort() internally
|
|
15
|
+
// so each Pin carries both its port name (PB5) and Arduino pin number (13).
|
|
16
|
+
export {
|
|
17
|
+
PD0, PD1, PD2, PD3, PD4, PD5, PD6, PD7,
|
|
18
|
+
PB0, PB1, PB2, PB3, PB4, PB5,
|
|
19
|
+
PC0, PC1, PC2, PC3, PC4, PC5,
|
|
20
|
+
} from '@typecad/mcu-atmega328p';
|
|
21
|
+
|
|
22
|
+
// Re-import for local aliasing
|
|
23
|
+
import {
|
|
24
|
+
PD0, PD1, PD2, PD3, PD4, PD5, PD6, PD7,
|
|
25
|
+
PB0, PB1, PB2, PB3, PB4, PB5,
|
|
26
|
+
PC0, PC1, PC2, PC3, PC4, PC5,
|
|
27
|
+
} from '@typecad/mcu-atmega328p';
|
|
28
|
+
|
|
29
|
+
// ---------------------------------------------------------------------------
|
|
30
|
+
// Arduino-style pin aliases (D0–D13, A0–A5)
|
|
31
|
+
// ---------------------------------------------------------------------------
|
|
32
|
+
|
|
33
|
+
export const D0 = PD0;
|
|
34
|
+
export const D1 = PD1;
|
|
35
|
+
export const D2 = PD2;
|
|
36
|
+
export const D3 = PD3;
|
|
37
|
+
export const D4 = PD4;
|
|
38
|
+
export const D5 = PD5;
|
|
39
|
+
export const D6 = PD6;
|
|
40
|
+
export const D7 = PD7;
|
|
41
|
+
export const D8 = PB0;
|
|
42
|
+
export const D9 = PB1;
|
|
43
|
+
export const D10 = PB2;
|
|
44
|
+
export const D11 = PB3;
|
|
45
|
+
export const D12 = PB4;
|
|
46
|
+
export const D13 = PB5;
|
|
47
|
+
|
|
48
|
+
export const A0 = PC0;
|
|
49
|
+
export const A1 = PC1;
|
|
50
|
+
export const A2 = PC2;
|
|
51
|
+
export const A3 = PC3;
|
|
52
|
+
export const A4 = PC4;
|
|
53
|
+
export const A5 = PC5;
|
|
54
|
+
|
|
55
|
+
// ---------------------------------------------------------------------------
|
|
56
|
+
// Convenience aliases
|
|
57
|
+
// ---------------------------------------------------------------------------
|
|
58
|
+
|
|
59
|
+
/** On-board LED (PB5). */
|
|
60
|
+
export const LED = PB5;
|
|
61
|
+
|
|
62
|
+
// Re-export silicon-level aliases (SDA, MOSI, etc.) from MCU package
|
|
63
|
+
export { SDA, SCL, MOSI, MISO, SCK, SS, TX, RX } from '@typecad/mcu-atmega328p';
|