@geastack/targets 0.1.108 → 0.1.109

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/build-config.json CHANGED
@@ -96,7 +96,9 @@
96
96
  "compiler.translationUnits",
97
97
  "compiler.storeRelowering",
98
98
  "compiler.moduleGraph",
99
- "compiler.allowAny"
99
+ "compiler.allowAny",
100
+ "display.width",
101
+ "display.height"
100
102
  ],
101
103
  "defaults": {
102
104
  "compiler": {
@@ -251,6 +253,30 @@
251
253
  "inference": {
252
254
  "canvasOnly": true
253
255
  }
256
+ },
257
+ "tvos": {
258
+ "supported": [
259
+ "compiler.staticCssRules",
260
+ "compiler.staticCssTape",
261
+ "compiler.staticCssTapeMinChunk",
262
+ "compiler.translationUnits",
263
+ "compiler.storeRelowering",
264
+ "compiler.moduleGraph",
265
+ "compiler.allowAny",
266
+ "display.width",
267
+ "display.height"
268
+ ],
269
+ "defaults": {
270
+ "compiler": {
271
+ "staticCssRules": true,
272
+ "staticCssTape": true,
273
+ "staticCssTapeMinChunk": 5,
274
+ "translationUnits": "default",
275
+ "storeRelowering": true,
276
+ "moduleGraph": "auto",
277
+ "allowAny": false
278
+ }
279
+ }
254
280
  }
255
281
  },
256
282
  "boards": {
@@ -732,6 +758,26 @@
732
758
  "inference": {
733
759
  "canvasOnly": true
734
760
  }
761
+ },
762
+ "esp32-s3-elecrow-crowpanel-advance-7.0": {
763
+ "supported": [
764
+ "compiler.generatedCodeOptimization",
765
+ "services.wifi",
766
+ "renderer.fuseReplayFlush"
767
+ ],
768
+ "defaults": {
769
+ "compiler": {
770
+ "generatedCodeOptimization": "O2",
771
+ "numberPrecision": "float32"
772
+ },
773
+ "services": {
774
+ "wifi": "auto"
775
+ },
776
+ "renderer": {
777
+ "fuseReplayFlush": false
778
+ }
779
+ },
780
+ "cssDevicePixelRatio": 1.5
735
781
  }
736
782
  }
737
783
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@geastack/targets",
3
- "version": "0.1.108",
3
+ "version": "0.1.109",
4
4
  "description": "Native embedded build targets (ESP32-IDF, RP2350) for geastack apps: board sources, target catalog, and CMake glue driven by the gea CLI.",
5
5
  "license": "GPL-3.0-only",
6
6
  "repository": {
@@ -38,12 +38,12 @@
38
38
  "dependencies": {
39
39
  "@geastack/chips": "^0.1.7",
40
40
  "@geastack/compiler": "^1.0.41",
41
- "@geastack/core": "0.1.49",
41
+ "@geastack/core": "0.1.52",
42
42
  "@geastack/elements": "^0.1.6",
43
- "@geastack/engine": "^0.1.20",
43
+ "@geastack/engine": "^0.1.23",
44
44
  "@geastack/geaos": "^0.1.3",
45
- "@geastack/geatsc-plugin-gea": "0.1.26",
46
- "@geastack/host": "0.1.22"
45
+ "@geastack/geatsc-plugin-gea": "0.1.28",
46
+ "@geastack/host": "0.1.23"
47
47
  },
48
48
  "scripts": {
49
49
  "test": "for f in $(git ls-files | grep -E '(^|/)(test[-_][^/]*|[^/]*\\.test)\\.mjs$'); do echo \"==> $f\"; node \"$f\" || exit 1; done"
@@ -0,0 +1,326 @@
1
+ // NS4168 I2S class-D amplifier, plus an optional separate I2S MEMS microphone
2
+ // (INMP441-style: 24-bit left-justified samples in a 32-bit left slot).
3
+ //
4
+ // There is no codec to configure: the S3 is the I2S master and streams PCM to
5
+ // the amplifier, which decodes it itself. So the speaker path is one TX
6
+ // channel, the volume is applied to the samples in software, and the only
7
+ // control lines are the amplifier's shutdown and mute, which a board may route
8
+ // through its I/O expander (board::audio.expanderShutdown/expanderMute: high
9
+ // shuts down / mutes, as on the Elecrow CrowPanel Advance). The microphone,
10
+ // when the board has one, runs on the other I2S controller and its own pins, so
11
+ // capture and playback never contend for a clock. It is a standard-I2S MEMS
12
+ // part or a PDM one (board::audio.micFormat); the S3 receives PDM only on I2S0,
13
+ // so the speaker always takes I2S1. Format 2 asks the board controller, for
14
+ // boards whose revisions differ only in the microphone (CrowPanel V1.0 has an
15
+ // INMP441, V1.3+ a PDM part).
16
+
17
+ #define GEA_AUDIO_DRIVER_INTERNAL 1
18
+ #include "audio.h"
19
+ #include "board.h"
20
+ #if GEA_BOARD_HAS_EXPANDER
21
+ #include "chip_bindings/expanders/io_expander.h"
22
+ #endif
23
+
24
+ #include <algorithm>
25
+ #include <array>
26
+ #include <cstddef>
27
+ #include <cstdint>
28
+ #include <mutex>
29
+ #include <string_view>
30
+ #include <vector>
31
+
32
+ #include "driver/i2s_pdm.h"
33
+ #include "driver/i2s_std.h"
34
+ #include "esp_heap_caps.h"
35
+ #include "esp_log.h"
36
+ #include "freertos/FreeRTOS.h"
37
+ #include "freertos/task.h"
38
+
39
+ #include "host/media.h"
40
+
41
+ #ifndef GEA_AUDIO_DMA_DESCRIPTORS
42
+ #define GEA_AUDIO_DMA_DESCRIPTORS 6
43
+ #endif
44
+ #ifndef GEA_AUDIO_DMA_FRAMES
45
+ #define GEA_AUDIO_DMA_FRAMES 240
46
+ #endif
47
+ static_assert(GEA_AUDIO_DMA_DESCRIPTORS >= 2);
48
+ static_assert(GEA_AUDIO_DMA_FRAMES >= 8 && GEA_AUDIO_DMA_FRAMES <= 511);
49
+
50
+ namespace gea::platform::esp32::chip_bindings::ns4168 {
51
+ namespace {
52
+
53
+ constexpr char kTag[] = "ns4168";
54
+ constexpr auto &kAudio = gea::platform::board::audio;
55
+ constexpr bool kHasMicrophone = kAudio.micBclk != GPIO_NUM_NC && kAudio.micDin != GPIO_NUM_NC;
56
+ constexpr i2s_port_t kSpeakerPort = I2S_NUM_1;
57
+ constexpr i2s_port_t kMicPort = I2S_NUM_0;
58
+ // The INMP441's 24 significant bits sit at the top of the 32-bit slot; keeping
59
+ // bits 29..14 gives ~12 dB of headroom over the raw top 16 bits, which is where
60
+ // speech from arm's length lands for this part.
61
+ constexpr int kMicShift = 14;
62
+ constexpr std::size_t kMicFrames = 256;
63
+
64
+ bool micIsPdm() {
65
+ if (kAudio.micFormat == 1) return true;
66
+ if (kAudio.micFormat == 0) return false;
67
+ #if GEA_BOARD_HAS_EXPANDER
68
+ auto &expander = gea::platform::esp32::chip_bindings::expanders::ioExpander();
69
+ expander.init();
70
+ return std::string_view(gea::platform::esp32::chip_bindings::expanders::ioExpanderDriverName()) == "elecrow-stc8h";
71
+ #else
72
+ return false;
73
+ #endif
74
+ }
75
+
76
+ bool setExpanderLine(int pin, bool high) {
77
+ #if GEA_BOARD_HAS_EXPANDER
78
+ if (pin < 0) return true;
79
+ return gea::platform::esp32::chip_bindings::expanders::ioExpander().writePin(pin, high);
80
+ #else
81
+ (void)pin;
82
+ (void)high;
83
+ return true;
84
+ #endif
85
+ }
86
+
87
+ class AudioDriver {
88
+ public:
89
+ static AudioDriver &instance() {
90
+ // Heap-allocated on first use, so an app that never plays sound pays nothing.
91
+ static AudioDriver *const driver = new AudioDriver();
92
+ return *driver;
93
+ }
94
+
95
+ bool open(int sampleRate, int channels, int bitsPerSample) {
96
+ std::lock_guard<std::mutex> lock(writeMutex_);
97
+ if (bitsPerSample != 16 || (channels != 1 && channels != 2) || sampleRate < 8000 || sampleRate > 48000) return false;
98
+ if (tx_ && sampleRate == sampleRate_ && channels == channels_) return true;
99
+ closeLocked();
100
+
101
+ i2s_chan_config_t channelConfig = I2S_CHANNEL_DEFAULT_CONFIG(kSpeakerPort, I2S_ROLE_MASTER);
102
+ channelConfig.auto_clear = true; // an underrun plays silence, not the last buffer again
103
+ channelConfig.dma_desc_num = GEA_AUDIO_DMA_DESCRIPTORS;
104
+ channelConfig.dma_frame_num = GEA_AUDIO_DMA_FRAMES;
105
+ esp_err_t err = i2s_new_channel(&channelConfig, &tx_, nullptr);
106
+ if (err != ESP_OK) {
107
+ ESP_LOGE(kTag, "I2S TX channel: %s", esp_err_to_name(err));
108
+ tx_ = nullptr;
109
+ return false;
110
+ }
111
+ // Mono PCM goes out on both slots, so the amplifier plays it whichever
112
+ // channel its CTRL strap selects.
113
+ i2s_std_config_t config{};
114
+ config.clk_cfg = I2S_STD_CLK_DEFAULT_CONFIG(static_cast<std::uint32_t>(sampleRate));
115
+ config.slot_cfg = I2S_STD_PHILIPS_SLOT_DEFAULT_CONFIG(I2S_DATA_BIT_WIDTH_16BIT,
116
+ channels == 1 ? I2S_SLOT_MODE_MONO : I2S_SLOT_MODE_STEREO);
117
+ config.gpio_cfg.mclk = GPIO_NUM_NC;
118
+ config.gpio_cfg.bclk = kAudio.bclk;
119
+ config.gpio_cfg.ws = kAudio.ws;
120
+ config.gpio_cfg.dout = kAudio.dout;
121
+ config.gpio_cfg.din = GPIO_NUM_NC;
122
+ err = i2s_channel_init_std_mode(tx_, &config);
123
+ if (err == ESP_OK) err = i2s_channel_enable(tx_);
124
+ if (err != ESP_OK) {
125
+ ESP_LOGE(kTag, "I2S TX init: %s", esp_err_to_name(err));
126
+ i2s_del_channel(tx_);
127
+ tx_ = nullptr;
128
+ return false;
129
+ }
130
+ setExpanderLine(kAudio.expanderMute, false);
131
+ setExpanderLine(kAudio.expanderShutdown, false);
132
+ sampleRate_ = sampleRate;
133
+ channels_ = channels;
134
+ ESP_LOGI(kTag, "speaker ready sample_rate=%d channels=%d volume=%d", sampleRate, channels, volume_);
135
+ return true;
136
+ }
137
+
138
+ bool write(const std::int16_t *pcm, std::size_t sampleCount, int timeoutMs) {
139
+ if (!pcm || sampleCount == 0) return false;
140
+ std::lock_guard<std::mutex> lock(writeMutex_);
141
+ if (!tx_) return false;
142
+ const TickType_t timeout = timeoutMs < 0 ? portMAX_DELAY : pdMS_TO_TICKS(timeoutMs);
143
+ const int gain = gainQ15();
144
+ while (sampleCount > 0) {
145
+ const std::size_t count = std::min(sampleCount, scaled_.size());
146
+ for (std::size_t i = 0; i < count; ++i) scaled_[i] = static_cast<std::int16_t>((pcm[i] * gain) >> 15);
147
+ std::size_t written = 0;
148
+ if (i2s_channel_write(tx_, scaled_.data(), count * sizeof(std::int16_t), &written, timeout) != ESP_OK) return false;
149
+ const std::size_t samples = written / sizeof(std::int16_t);
150
+ if (samples == 0) return false;
151
+ pcm += samples;
152
+ sampleCount -= samples;
153
+ }
154
+ return true;
155
+ }
156
+
157
+ void close() {
158
+ std::lock_guard<std::mutex> lock(writeMutex_);
159
+ closeLocked();
160
+ }
161
+
162
+ bool flush() {
163
+ std::lock_guard<std::mutex> lock(writeMutex_);
164
+ if (!tx_) return true;
165
+ // Disabling drops what is queued; re-enabling starts from silence
166
+ // (auto_clear), which is what a seek or stop wants.
167
+ if (i2s_channel_disable(tx_) != ESP_OK) return false;
168
+ return i2s_channel_enable(tx_) == ESP_OK;
169
+ }
170
+
171
+ int volume() const { return volume_; }
172
+ void setVolume(int percent) { volume_ = std::clamp(percent, 0, 100); }
173
+
174
+ void attachTrack(gea::host::NativeMediaTrackHandle handle) {
175
+ if constexpr (!kHasMicrophone) {
176
+ (void)handle;
177
+ return;
178
+ }
179
+ std::lock_guard<std::mutex> lock(trackMutex_);
180
+ tracks_.push_back(handle);
181
+ if (captureTask_) return;
182
+ if (xTaskCreatePinnedToCoreWithCaps(captureEntry, "gea_mic", 6144, this, 5, &captureTask_, 1,
183
+ MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT) != pdPASS) {
184
+ captureTask_ = nullptr;
185
+ tracks_.pop_back();
186
+ ESP_LOGE(kTag, "microphone task allocation failed");
187
+ }
188
+ }
189
+
190
+ void detachTrack(gea::host::NativeMediaTrackHandle handle) {
191
+ std::lock_guard<std::mutex> lock(trackMutex_);
192
+ tracks_.erase(std::remove(tracks_.begin(), tracks_.end(), handle), tracks_.end());
193
+ }
194
+
195
+ private:
196
+ AudioDriver() = default;
197
+
198
+ // Perceptual volume: 50% is about -12 dB, 0% is silent.
199
+ int gainQ15() const {
200
+ const int v = volume_;
201
+ return (v * v * 32767) / 10000;
202
+ }
203
+
204
+ void closeLocked() {
205
+ if (!tx_) return;
206
+ setExpanderLine(kAudio.expanderShutdown, true);
207
+ i2s_channel_disable(tx_);
208
+ i2s_del_channel(tx_);
209
+ tx_ = nullptr;
210
+ sampleRate_ = 0;
211
+ channels_ = 0;
212
+ }
213
+
214
+ static void captureEntry(void *arg) { static_cast<AudioDriver *>(arg)->runCapture(); }
215
+
216
+ void runCapture() {
217
+ i2s_chan_handle_t rx = nullptr;
218
+ i2s_chan_config_t channelConfig = I2S_CHANNEL_DEFAULT_CONFIG(kMicPort, I2S_ROLE_MASTER);
219
+ channelConfig.dma_desc_num = GEA_AUDIO_DMA_DESCRIPTORS;
220
+ channelConfig.dma_frame_num = GEA_AUDIO_DMA_FRAMES;
221
+ const bool pdm = micIsPdm();
222
+ esp_err_t err = i2s_new_channel(&channelConfig, nullptr, &rx);
223
+ if (err == ESP_OK && pdm) {
224
+ // PDM: the S3 converts to 16-bit PCM in hardware.
225
+ i2s_pdm_rx_config_t config{};
226
+ config.clk_cfg = I2S_PDM_RX_CLK_DEFAULT_CONFIG(static_cast<std::uint32_t>(gea::platform::audio::deviceSampleRate));
227
+ config.slot_cfg = I2S_PDM_RX_SLOT_DEFAULT_CONFIG(I2S_DATA_BIT_WIDTH_16BIT, I2S_SLOT_MODE_MONO);
228
+ config.gpio_cfg.clk = kAudio.micBclk;
229
+ config.gpio_cfg.din = kAudio.micDin;
230
+ err = i2s_channel_init_pdm_rx_mode(rx, &config);
231
+ if (err == ESP_OK) err = i2s_channel_enable(rx);
232
+ } else if (err == ESP_OK) {
233
+ i2s_std_config_t config{};
234
+ config.clk_cfg = I2S_STD_CLK_DEFAULT_CONFIG(static_cast<std::uint32_t>(gea::platform::audio::deviceSampleRate));
235
+ config.slot_cfg = I2S_STD_PHILIPS_SLOT_DEFAULT_CONFIG(I2S_DATA_BIT_WIDTH_32BIT, I2S_SLOT_MODE_MONO);
236
+ config.slot_cfg.slot_mask = I2S_STD_SLOT_LEFT; // L/R strapped low
237
+ config.gpio_cfg.mclk = GPIO_NUM_NC;
238
+ config.gpio_cfg.bclk = kAudio.micBclk;
239
+ config.gpio_cfg.ws = kAudio.micWs;
240
+ config.gpio_cfg.dout = GPIO_NUM_NC;
241
+ config.gpio_cfg.din = kAudio.micDin;
242
+ err = i2s_channel_init_std_mode(rx, &config);
243
+ if (err == ESP_OK) err = i2s_channel_enable(rx);
244
+ }
245
+ if (err != ESP_OK) ESP_LOGE(kTag, "microphone I2S init: %s", esp_err_to_name(err));
246
+ else ESP_LOGI(kTag, "microphone ready (%s) sample_rate=%d", pdm ? "PDM" : "I2S", gea::platform::audio::deviceSampleRate);
247
+
248
+ std::array<std::int32_t, kMicFrames> raw{};
249
+ std::array<std::int16_t, kMicFrames> pcm{};
250
+ std::vector<gea::host::NativeMediaTrackHandle> snapshot;
251
+ while (err == ESP_OK) {
252
+ {
253
+ std::lock_guard<std::mutex> lock(trackMutex_);
254
+ if (tracks_.empty()) break;
255
+ snapshot = tracks_;
256
+ }
257
+ std::size_t bytes = 0;
258
+ std::size_t frames = 0;
259
+ if (pdm) {
260
+ if (i2s_channel_read(rx, pcm.data(), sizeof(pcm), &bytes, pdMS_TO_TICKS(200)) != ESP_OK) continue;
261
+ frames = bytes / sizeof(std::int16_t);
262
+ } else {
263
+ if (i2s_channel_read(rx, raw.data(), sizeof(raw), &bytes, pdMS_TO_TICKS(200)) != ESP_OK) continue;
264
+ frames = bytes / sizeof(std::int32_t);
265
+ for (std::size_t i = 0; i < frames; ++i) {
266
+ const std::int32_t sample = raw[i] >> kMicShift;
267
+ pcm[i] = static_cast<std::int16_t>(std::clamp<std::int32_t>(sample, -32768, 32767));
268
+ }
269
+ }
270
+ for (auto handle : snapshot) gea::host::media::track_inject_pcm(handle, pcm.data(), frames);
271
+ }
272
+ if (rx) {
273
+ i2s_channel_disable(rx);
274
+ i2s_del_channel(rx);
275
+ }
276
+ {
277
+ std::lock_guard<std::mutex> lock(trackMutex_);
278
+ captureTask_ = nullptr;
279
+ // A track attached after the loop saw none starts its own task.
280
+ if (!tracks_.empty()) attachTrackRestart();
281
+ }
282
+ vTaskDeleteWithCaps(nullptr);
283
+ }
284
+
285
+ void attachTrackRestart() {
286
+ if (xTaskCreatePinnedToCoreWithCaps(captureEntry, "gea_mic", 6144, this, 5, &captureTask_, 1,
287
+ MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT) != pdPASS) {
288
+ captureTask_ = nullptr;
289
+ }
290
+ }
291
+
292
+ std::mutex writeMutex_;
293
+ i2s_chan_handle_t tx_ = nullptr;
294
+ int sampleRate_ = 0;
295
+ int channels_ = 0;
296
+ int volume_ = 70;
297
+ std::array<std::int16_t, 512> scaled_{};
298
+
299
+ std::mutex trackMutex_;
300
+ std::vector<gea::host::NativeMediaTrackHandle> tracks_;
301
+ TaskHandle_t captureTask_ = nullptr;
302
+ };
303
+
304
+ } // namespace
305
+ } // namespace gea::platform::esp32::chip_bindings::ns4168
306
+
307
+ using gea::platform::esp32::chip_bindings::ns4168::AudioDriver;
308
+
309
+ double gea::platform::audio::OutputDriver::outputLatency(int sampleRate) {
310
+ return sampleRate > 0 ? double(GEA_AUDIO_DMA_DESCRIPTORS * GEA_AUDIO_DMA_FRAMES) / sampleRate : 0;
311
+ }
312
+ bool gea::platform::audio::OutputDriver::open(int sampleRate, int channels, int bitsPerSample) {
313
+ return AudioDriver::instance().open(sampleRate, channels, bitsPerSample);
314
+ }
315
+ bool gea::platform::audio::OutputDriver::write(const std::int16_t *pcm, std::size_t sampleCount, int timeoutMs) {
316
+ return AudioDriver::instance().write(pcm, sampleCount, timeoutMs);
317
+ }
318
+ void gea::platform::audio::OutputDriver::close() { AudioDriver::instance().close(); }
319
+ bool gea::platform::audio::OutputDriver::flush() { return AudioDriver::instance().flush(); }
320
+ int gea::platform::audio::OutputDriver::volume() { return AudioDriver::instance().volume(); }
321
+ void gea::platform::audio::OutputDriver::setVolume(int volumePercent) { AudioDriver::instance().setVolume(volumePercent); }
322
+
323
+ namespace gea::host::media {
324
+ void platform_attach_track(NativeMediaTrackHandle handle) { AudioDriver::instance().attachTrack(handle); }
325
+ void platform_detach_track(NativeMediaTrackHandle handle) { AudioDriver::instance().detachTrack(handle); }
326
+ } // namespace gea::host::media