board.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. /**
  2. * @file board.c
  3. * @brief SqueezeAMP board implementation
  4. *
  5. * Initializes the TAS57xx DAC and handles RTSP events to control DAC power.
  6. *
  7. * Features
  8. * - DAC control via TAS57xx
  9. * - Speaker fault auto-mute and recovery
  10. * - Headphone jack detection to disable speakers
  11. *
  12. * LED handling is done by the led.c module.
  13. *
  14. * SqueezeAmp GPIO Pin Assignment
  15. * ┌─────────────────────────────────────────────────────────────┐
  16. * │ Function │ GPIO │ Direction │ Notes │
  17. * ├─────────────────────────────────────────────────────────────┤
  18. * │ I2S Bit Clock (BCK) │ 33 │ Output │ Audio │
  19. * │ I2S Word Select (WS) │ 25 │ Output │ Audio │
  20. * │ I2S Data Out (DO) │ 32 │ Output │ Audio │
  21. * ├─────────────────────────────────────────────────────────────┤
  22. * │ SPDIF Data Out │ 15 │ Output │ Optical │
  23. * ├─────────────────────────────────────────────────────────────┤
  24. * │ DAC I2C SDA │ 27 │ Bidir │ TAS57xx │
  25. * │ DAC I2C SCL │ 26 │ Output │ TAS57xx │
  26. * │ Mute Control │ 14 │ Output │ Active Low │
  27. * ├─────────────────────────────────────────────────────────────┤
  28. * │ Speaker Fault │ 2 │ Input │ Protection │
  29. * │ Jack Detection │ 34 │ Input │ Headphone │
  30. * ├─────────────────────────────────────────────────────────────┤
  31. * │ Status LED │ 12 │ Output │ Green │
  32. * │ Error LED │ 13 │ Output │ Red │
  33. * ├─────────────────────────────────────────────────────────────┤
  34. * │ Battery Monitor │ 7 │ ADC Input │ Voltage │
  35. * └─────────────────────────────────────────────────────────────┘
  36. */
  37. #include "iot_board.h"
  38. #include "dac.h"
  39. #include "dac_tas57xx.h"
  40. #include "settings.h"
  41. #include "driver/gpio.h"
  42. #include "driver/i2c_master.h"
  43. #include "esp_check.h"
  44. #include "esp_log.h"
  45. #include "esp_attr.h"
  46. #include "freertos/FreeRTOS.h"
  47. #include "freertos/task.h"
  48. #include "rtsp_events.h"
  49. #include "led.h"
  50. #include "soc/uart_pins.h"
  51. #include "soc/gpio_struct.h"
  52. #include "esp_rom_gpio.h"
  53. #define ISR_HANDLER_TASK_STACK_SIZE 4096
  54. #define ISR_HANDLER_TASK_PRIORITY 5
  55. #define JACK_DEBOUNCE_MS 200
  56. // Notification bits for speaker fault task
  57. #define SPKFAULT_NOTIFY_FAULT (1 << 0)
  58. #define SPKFAULT_NOTIFY_CLEAR (1 << 1)
  59. #define JACK_NOTIFY_CHANGED (1 << 2)
  60. static const char TAG[] = "SqueezeAMP";
  61. static bool s_board_initialized = false;
  62. static TaskHandle_t gpio_task_handle = NULL;
  63. static volatile bool speaker_fault_active = false;
  64. static volatile bool headphone_inserted = false;
  65. static i2c_master_bus_handle_t s_i2c_dac_bus_handle = NULL;
  66. static i2c_master_bus_handle_t s_i2c_disp_bus_handle = NULL;
  67. static esp_err_t init_mute_gpio(void);
  68. static esp_err_t init_spkfault_gpio(void);
  69. static esp_err_t init_jack_gpio(void);
  70. static esp_err_t init_gpio_isr_task(void);
  71. static void on_rtsp_event(rtsp_event_t event, const rtsp_event_data_t *data,
  72. void *user_data);
  73. // Speaker fault ISR - just notifies the task, no I2C calls
  74. static void IRAM_ATTR spkfault_isr_handler(void *arg) {
  75. (void)arg;
  76. BaseType_t xHigherPriorityTaskWoken = pdFALSE;
  77. // Check current GPIO level to determine fault or clear
  78. int level = gpio_get_level(BOARD_SPKFAULT_GPIO);
  79. uint32_t notify_bit =
  80. (level == 0) ? SPKFAULT_NOTIFY_FAULT : SPKFAULT_NOTIFY_CLEAR;
  81. if (gpio_task_handle != NULL) {
  82. xTaskNotifyFromISR(gpio_task_handle, notify_bit, eSetBits,
  83. &xHigherPriorityTaskWoken);
  84. }
  85. portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
  86. }
  87. // Headphone jack ISR - notifies the task (debounced in task)
  88. static void IRAM_ATTR jack_isr_handler(void *arg) {
  89. (void)arg;
  90. BaseType_t xHigherPriorityTaskWoken = pdFALSE;
  91. if (gpio_task_handle != NULL) {
  92. xTaskNotifyFromISR(gpio_task_handle, JACK_NOTIFY_CHANGED, eSetBits,
  93. &xHigherPriorityTaskWoken);
  94. }
  95. portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
  96. }
  97. // Task to handle speaker fault and jack events (runs I2C-safe operations)
  98. static void spkfault_task(void *arg) {
  99. (void)arg;
  100. uint32_t notification;
  101. ESP_LOGI(TAG, "GPIO events task started");
  102. while (true) {
  103. if (xTaskNotifyWait(0, UINT32_MAX, &notification, portMAX_DELAY) ==
  104. pdTRUE) {
  105. // Handle speaker fault
  106. if (notification & SPKFAULT_NOTIFY_FAULT) {
  107. if (!speaker_fault_active) {
  108. speaker_fault_active = true;
  109. ESP_LOGW(TAG, "Speaker fault detected");
  110. dac_enable_speaker(false);
  111. led_set_error(true);
  112. }
  113. }
  114. if (notification & SPKFAULT_NOTIFY_CLEAR) {
  115. if (speaker_fault_active) {
  116. speaker_fault_active = false;
  117. ESP_LOGI(TAG, "Speaker fault cleared");
  118. // Only re-enable speaker if no headphone inserted
  119. if (!headphone_inserted) {
  120. dac_enable_speaker(true);
  121. }
  122. led_set_error(false);
  123. }
  124. }
  125. // Handle headphone jack with debounce
  126. if (notification & JACK_NOTIFY_CHANGED) {
  127. // Wait for debounce period
  128. vTaskDelay(pdMS_TO_TICKS(JACK_DEBOUNCE_MS));
  129. // Read stable state after debounce
  130. bool jack_inserted = (gpio_get_level(BOARD_JACK_GPIO) == 0);
  131. if (jack_inserted && !headphone_inserted) {
  132. headphone_inserted = true;
  133. dac_enable_speaker(false);
  134. } else if (!jack_inserted && headphone_inserted) {
  135. headphone_inserted = false;
  136. // Only re-enable speaker if no fault active
  137. if (!speaker_fault_active) {
  138. dac_enable_speaker(true);
  139. }
  140. }
  141. }
  142. }
  143. }
  144. }
  145. static void on_rtsp_event(rtsp_event_t event, const rtsp_event_data_t *data,
  146. void *user_data) {
  147. (void)data;
  148. (void)user_data;
  149. switch (event) {
  150. case RTSP_EVENT_CLIENT_CONNECTED:
  151. case RTSP_EVENT_PAUSED:
  152. dac_set_power_mode(DAC_POWER_STANDBY);
  153. break;
  154. case RTSP_EVENT_PLAYING:
  155. dac_set_power_mode(DAC_POWER_ON);
  156. break;
  157. case RTSP_EVENT_DISCONNECTED:
  158. dac_set_power_mode(DAC_POWER_OFF);
  159. break;
  160. case RTSP_EVENT_METADATA:
  161. break;
  162. }
  163. }
  164. const char *iot_board_get_info(void) {
  165. return BOARD_NAME;
  166. }
  167. bool iot_board_is_init(void) {
  168. return s_board_initialized;
  169. }
  170. board_res_handle_t iot_board_get_handle(int id) {
  171. switch (id) {
  172. case BOARD_I2C_DAC_ID:
  173. return (board_res_handle_t)s_i2c_dac_bus_handle;
  174. case BOARD_I2C_DISP_ID:
  175. return (board_res_handle_t)s_i2c_disp_bus_handle;
  176. default:
  177. return NULL;
  178. }
  179. }
  180. esp_err_t iot_board_init(void) {
  181. esp_err_t err = ESP_OK;
  182. if (s_board_initialized) {
  183. ESP_LOGW(TAG, "Board already initialized");
  184. return ESP_OK;
  185. }
  186. // Initialize DAC I2C bus
  187. i2c_master_bus_config_t i2c_cfg = {
  188. .i2c_port = BOARD_I2C_PORT,
  189. .sda_io_num = BOARD_I2C_SDA_GPIO,
  190. .scl_io_num = BOARD_I2C_SCL_GPIO,
  191. .clk_source = I2C_CLK_SRC_DEFAULT,
  192. .glitch_ignore_cnt = 7,
  193. .flags.enable_internal_pullup = true,
  194. };
  195. err = i2c_new_master_bus(&i2c_cfg, &s_i2c_dac_bus_handle);
  196. if (err != ESP_OK) {
  197. ESP_LOGE(TAG, "Failed to initialize DAC I2C bus: %s", esp_err_to_name(err));
  198. return err;
  199. }
  200. ESP_LOGI(TAG, "DAC I2C bus %d initialized: sda=%d, scl=%d", BOARD_I2C_PORT,
  201. BOARD_I2C_SDA_GPIO, BOARD_I2C_SCL_GPIO);
  202. #if defined(CONFIG_DISPLAY_ENABLED) && defined(CONFIG_DISPLAY_BUS_I2C)
  203. // Initialize display I2C bus — share with DAC bus if pins are identical,
  204. // otherwise bring up a second controller on BOARD_I2C_DISP_PORT.
  205. if (CONFIG_DISPLAY_I2C_SDA == BOARD_I2C_SDA_GPIO &&
  206. CONFIG_DISPLAY_I2C_SCL == BOARD_I2C_SCL_GPIO) {
  207. s_i2c_disp_bus_handle = s_i2c_dac_bus_handle;
  208. ESP_LOGI(TAG, "Display sharing DAC I2C bus");
  209. } else {
  210. // UART0 owns GPIO1 (TX) and GPIO3 (RX) by default via the GPIO matrix.
  211. // If the display I2C pins overlap, detach them from UART0 first so the
  212. // I2C driver can claim them. This ends serial console output on those
  213. // pins.
  214. if (CONFIG_DISPLAY_I2C_SDA == U0TXD_GPIO_NUM ||
  215. CONFIG_DISPLAY_I2C_SDA == U0RXD_GPIO_NUM ||
  216. CONFIG_DISPLAY_I2C_SCL == U0TXD_GPIO_NUM ||
  217. CONFIG_DISPLAY_I2C_SCL == U0RXD_GPIO_NUM) {
  218. ESP_LOGW(TAG,
  219. "Display I2C pins (sda=%d, scl=%d) conflict with UART0 "
  220. "— detaching serial console",
  221. CONFIG_DISPLAY_I2C_SDA, CONFIG_DISPLAY_I2C_SCL);
  222. // gpio_reset_pin only resets the IO_MUX; the UART0 signal remains
  223. // routed through the GPIO matrix. esp_rom_gpio_pad_select_gpio clears
  224. // both the IO_MUX and the GPIO matrix routing.
  225. esp_rom_gpio_pad_select_gpio(CONFIG_DISPLAY_I2C_SDA);
  226. esp_rom_gpio_pad_select_gpio(CONFIG_DISPLAY_I2C_SCL);
  227. }
  228. i2c_master_bus_config_t disp_i2c_cfg = {
  229. .i2c_port = -1,
  230. .sda_io_num = CONFIG_DISPLAY_I2C_SDA,
  231. .scl_io_num = CONFIG_DISPLAY_I2C_SCL,
  232. .clk_source = I2C_CLK_SRC_DEFAULT,
  233. .glitch_ignore_cnt = 7,
  234. .flags.enable_internal_pullup = true,
  235. };
  236. err = i2c_new_master_bus(&disp_i2c_cfg, &s_i2c_disp_bus_handle);
  237. if (err != ESP_OK) {
  238. ESP_LOGW(TAG,
  239. "Failed to initialize display I2C bus: %s — display will be "
  240. "unavailable",
  241. esp_err_to_name(err));
  242. s_i2c_disp_bus_handle = NULL;
  243. } else {
  244. ESP_LOGI(TAG, "Display I2C bus %d initialized: sda=%d, scl=%d",
  245. BOARD_I2C_DISP_PORT, CONFIG_DISPLAY_I2C_SDA,
  246. CONFIG_DISPLAY_I2C_SCL);
  247. // Allow lines to settle then check idle levels.
  248. // Both should read HIGH; LOW means pull-ups are too weak or something
  249. // is actively driving the line (UART residual, unpowered device, etc).
  250. vTaskDelay(pdMS_TO_TICKS(10));
  251. int sda_lvl = gpio_get_level(CONFIG_DISPLAY_I2C_SDA);
  252. int scl_lvl = gpio_get_level(CONFIG_DISPLAY_I2C_SCL);
  253. if (sda_lvl && scl_lvl) {
  254. ESP_LOGI(TAG, "Display I2C lines idle-high (SDA=%d SCL=%d) — OK",
  255. sda_lvl, scl_lvl);
  256. } else {
  257. ESP_LOGW(TAG,
  258. "Display I2C lines not idle-high (SDA=%d SCL=%d) — "
  259. "check pull-ups and that the display is powered",
  260. sda_lvl, scl_lvl);
  261. }
  262. }
  263. }
  264. #endif
  265. // Register and initialize DAC
  266. dac_register(&dac_tas57xx_ops);
  267. err = dac_init(s_i2c_dac_bus_handle);
  268. if (err != ESP_OK) {
  269. ESP_LOGE(TAG, "Failed to initialize DAC: %s", esp_err_to_name(err));
  270. return err;
  271. }
  272. // Restore saved volume
  273. float vol_db;
  274. if (ESP_OK == settings_get_volume(&vol_db)) {
  275. dac_set_volume(vol_db);
  276. }
  277. // Configure mute GPIO
  278. err = init_mute_gpio();
  279. if (err != ESP_OK) {
  280. ESP_LOGE(TAG, "Failed to initialize mute GPIO: %s", esp_err_to_name(err));
  281. return err;
  282. }
  283. // Create GPIO ISR service and event handler task
  284. err = init_gpio_isr_task();
  285. if (err != ESP_OK) {
  286. ESP_LOGE(TAG, "Failed to initialize GPIO ISR task: %s",
  287. esp_err_to_name(err));
  288. return err;
  289. }
  290. // Configure speaker fault detection
  291. err = init_spkfault_gpio();
  292. if (err != ESP_OK) {
  293. ESP_LOGE(TAG, "Failed to initialize speaker fault GPIO: %s",
  294. esp_err_to_name(err));
  295. return err;
  296. }
  297. // Configure headphone jack detection
  298. err = init_jack_gpio();
  299. if (err != ESP_OK) {
  300. ESP_LOGE(TAG, "Failed to initialize jack GPIO: %s", esp_err_to_name(err));
  301. return err;
  302. }
  303. // Register for RTSP events to control DAC power
  304. rtsp_events_register(on_rtsp_event, NULL);
  305. // Start in standby
  306. dac_set_power_mode(DAC_POWER_OFF);
  307. s_board_initialized = true;
  308. ESP_LOGI(TAG, "SqueezeAMP initialized");
  309. return ESP_OK;
  310. }
  311. esp_err_t iot_board_deinit(void) {
  312. if (!s_board_initialized) {
  313. return ESP_OK;
  314. }
  315. gpio_isr_handler_remove(BOARD_JACK_GPIO);
  316. gpio_isr_handler_remove(BOARD_SPKFAULT_GPIO);
  317. if (gpio_task_handle != NULL) {
  318. vTaskDelete(gpio_task_handle);
  319. gpio_task_handle = NULL;
  320. }
  321. rtsp_events_unregister(on_rtsp_event);
  322. // Ensure mute GPIO is active (muted) during shutdown for safety
  323. gpio_set_level(BOARD_MUTE_GPIO, BOARD_MUTE_GPIO_LEVEL);
  324. dac_enable_speaker(false);
  325. dac_set_power_mode(DAC_POWER_OFF);
  326. dac_deinit();
  327. // Tear down I2C buses (after DAC is deinitialized)
  328. // Free display bus first — only if it is not shared with the DAC bus
  329. if (s_i2c_disp_bus_handle != NULL &&
  330. s_i2c_disp_bus_handle != s_i2c_dac_bus_handle) {
  331. i2c_del_master_bus(s_i2c_disp_bus_handle);
  332. }
  333. s_i2c_disp_bus_handle = NULL;
  334. if (s_i2c_dac_bus_handle != NULL) {
  335. i2c_del_master_bus(s_i2c_dac_bus_handle);
  336. s_i2c_dac_bus_handle = NULL;
  337. }
  338. s_board_initialized = false;
  339. return ESP_OK;
  340. }
  341. static esp_err_t init_mute_gpio(void) {
  342. gpio_config_t mute_gpio_cfg = {
  343. .pin_bit_mask = (1ULL << BOARD_MUTE_GPIO),
  344. .mode = GPIO_MODE_OUTPUT,
  345. .pull_up_en = GPIO_PULLUP_DISABLE,
  346. .pull_down_en = GPIO_PULLDOWN_DISABLE,
  347. .intr_type = GPIO_INTR_DISABLE,
  348. };
  349. esp_err_t err = gpio_config(&mute_gpio_cfg);
  350. ESP_RETURN_ON_ERROR(err, TAG, "Failed to configure mute GPIO");
  351. // Initialize to unmuted state (active low, so set high to unmute)
  352. gpio_set_level(BOARD_MUTE_GPIO, !BOARD_MUTE_GPIO_LEVEL);
  353. ESP_LOGI(TAG, "Mute GPIO initialized on GPIO %d (active %s)", BOARD_MUTE_GPIO,
  354. BOARD_MUTE_GPIO_LEVEL ? "high" : "low");
  355. return ESP_OK;
  356. }
  357. static esp_err_t init_gpio_isr_task(void) {
  358. esp_err_t err = board_gpio_isr_init();
  359. if (err != ESP_OK) {
  360. return err;
  361. }
  362. BaseType_t ret =
  363. xTaskCreate(spkfault_task, "gpio_events", ISR_HANDLER_TASK_STACK_SIZE,
  364. NULL, ISR_HANDLER_TASK_PRIORITY, &gpio_task_handle);
  365. if (ret != pdPASS) {
  366. ESP_LOGE(TAG, "Failed to create GPIO events task");
  367. return ESP_ERR_NO_MEM;
  368. }
  369. return ESP_OK;
  370. }
  371. static esp_err_t init_spkfault_gpio(void) {
  372. gpio_config_t spkfault_cfg = {
  373. .pin_bit_mask = (1ULL << BOARD_SPKFAULT_GPIO),
  374. .mode = GPIO_MODE_INPUT,
  375. .pull_up_en = GPIO_PULLUP_ENABLE,
  376. .pull_down_en = GPIO_PULLDOWN_DISABLE,
  377. .intr_type = GPIO_INTR_ANYEDGE, // Trigger on both fault and recovery
  378. };
  379. esp_err_t err = gpio_config(&spkfault_cfg);
  380. ESP_RETURN_ON_ERROR(err, TAG, "Failed to configure speaker fault GPIO");
  381. err = gpio_isr_handler_add(BOARD_SPKFAULT_GPIO, spkfault_isr_handler, NULL);
  382. ESP_RETURN_ON_ERROR(err, TAG, "Failed to add speaker fault ISR handler");
  383. // Check initial state
  384. int level = gpio_get_level(BOARD_SPKFAULT_GPIO);
  385. if (level == 0) {
  386. ESP_LOGW(TAG, "Speaker fault already active at startup");
  387. xTaskNotify(gpio_task_handle, SPKFAULT_NOTIFY_FAULT, eSetBits);
  388. }
  389. ESP_LOGI(TAG, "Speaker fault detection enabled on GPIO %d",
  390. BOARD_SPKFAULT_GPIO);
  391. return ESP_OK;
  392. }
  393. static esp_err_t init_jack_gpio(void) {
  394. // Note: GPIO 34-39 on ESP32 are input-only and have no internal pull-up.
  395. // An external pull-up resistor is required on the jack detect pin.
  396. gpio_config_t jack_cfg = {
  397. .pin_bit_mask = (1ULL << BOARD_JACK_GPIO),
  398. .mode = GPIO_MODE_INPUT,
  399. .pull_up_en = GPIO_PULLUP_DISABLE, // GPIO 34 has no internal pull-up
  400. .pull_down_en = GPIO_PULLDOWN_DISABLE,
  401. .intr_type = GPIO_INTR_ANYEDGE, // Trigger on both insert and remove
  402. };
  403. esp_err_t err = gpio_config(&jack_cfg);
  404. ESP_RETURN_ON_ERROR(err, TAG, "Failed to configure jack GPIO");
  405. err = gpio_isr_handler_add(BOARD_JACK_GPIO, jack_isr_handler, NULL);
  406. ESP_RETURN_ON_ERROR(err, TAG, "Failed to add jack ISR handler");
  407. // Check initial state in case headphone is already inserted
  408. if (gpio_get_level(BOARD_JACK_GPIO) == 0) {
  409. xTaskNotify(gpio_task_handle, JACK_NOTIFY_CHANGED, eSetBits);
  410. }
  411. ESP_LOGI(TAG, "Headphone jack detection enabled on GPIO %d", BOARD_JACK_GPIO);
  412. return ESP_OK;
  413. }
  414. // Override the abort() function to mute GPIO during system panics
  415. // This is called by ESP-IDF during panic/abort situations via -Wl,--wrap=abort
  416. void IRAM_ATTR __wrap_abort(void) {
  417. // Immediately mute the amplifier using direct register access
  418. // This must be fast and not rely on any complex systems
  419. if (BOARD_MUTE_GPIO_LEVEL) {
  420. // Active high - set bit
  421. GPIO.out_w1ts = (1ULL << BOARD_MUTE_GPIO);
  422. } else {
  423. // Active low - clear bit
  424. GPIO.out_w1tc = (1ULL << BOARD_MUTE_GPIO);
  425. }
  426. // Call the original abort function
  427. extern void __real_abort(void) __attribute__((noreturn));
  428. __real_abort();
  429. }