main.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. /**
  2. * Copyright (c) 2017 - 2020, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #include <stdint.h>
  41. #include <stdbool.h>
  42. #include <stddef.h>
  43. #include "nrf.h"
  44. #include "app_util.h"
  45. #include "nrf_drv_usbd.h"
  46. #include "nrf_drv_clock.h"
  47. #include "nrf_gpio.h"
  48. #include "nrf_delay.h"
  49. #include "nrf_drv_power.h"
  50. #include "app_usbd.h"
  51. #include "app_usbd_core.h"
  52. #include "app_usbd_string_desc.h"
  53. #include "app_usbd_audio.h"
  54. #include "app_error.h"
  55. #include "boards.h"
  56. #include "nrf_log.h"
  57. #include "nrf_log_ctrl.h"
  58. #include "nrf_log_default_backends.h"
  59. /**@file
  60. * @defgroup usbd_aduio_example main.c
  61. * @{
  62. * @ingroup usbd_audio_example
  63. * @brief USBD Audio class example
  64. *
  65. */
  66. #define LED_USB_RESUME (BSP_BOARD_LED_0)
  67. #define LED_USB_START (BSP_BOARD_LED_1)
  68. #define LED_AUDIO_RX (BSP_BOARD_LED_2)
  69. #define LED_AUDIO_TX (BSP_BOARD_LED_3)
  70. /**
  71. * @brief USB audio samples size
  72. */
  73. #define BUFFER_SIZE (48)
  74. /**
  75. * @brief Enable power USB detection
  76. *
  77. * Configure if example supports USB port connection
  78. */
  79. #ifndef USBD_POWER_DETECTION
  80. #define USBD_POWER_DETECTION true
  81. #endif
  82. /**
  83. * @brief Audio class user event handler
  84. */
  85. static void hp_audio_user_ev_handler(app_usbd_class_inst_t const * p_inst,
  86. app_usbd_audio_user_event_t event);
  87. static void mic_audio_user_ev_handler(app_usbd_class_inst_t const * p_inst,
  88. app_usbd_audio_user_event_t event);
  89. /* Channels and feature controls configuration */
  90. /**
  91. * @brief Input terminal channel configuration
  92. */
  93. #define HP_TERMINAL_CH_CONFIG() \
  94. (APP_USBD_AUDIO_IN_TERM_CH_CONFIG_LEFT_FRONT | APP_USBD_AUDIO_IN_TERM_CH_CONFIG_RIGHT_FRONT)
  95. /**
  96. * @brief Feature controls
  97. *
  98. * general
  99. * channel 0
  100. * channel 1
  101. */
  102. #define HP_FEATURE_CONTROLS() \
  103. APP_USBD_U16_TO_RAW_DSC(APP_USBD_AUDIO_FEATURE_UNIT_CONTROL_MUTE), \
  104. APP_USBD_U16_TO_RAW_DSC(APP_USBD_AUDIO_FEATURE_UNIT_CONTROL_MUTE), \
  105. APP_USBD_U16_TO_RAW_DSC(APP_USBD_AUDIO_FEATURE_UNIT_CONTROL_MUTE)
  106. /**
  107. * @brief Input terminal channel configuration
  108. */
  109. #define MIC_TERMINAL_CH_CONFIG() \
  110. (APP_USBD_AUDIO_IN_TERM_CH_CONFIG_LEFT_FRONT | APP_USBD_AUDIO_IN_TERM_CH_CONFIG_RIGHT_FRONT)
  111. /**
  112. * @brief Feature controls
  113. *
  114. * general
  115. * channel 0
  116. * channel 1
  117. */
  118. #define MIC_FEATURE_CONTROLS() \
  119. APP_USBD_U16_TO_RAW_DSC(APP_USBD_AUDIO_FEATURE_UNIT_CONTROL_MUTE), \
  120. APP_USBD_U16_TO_RAW_DSC(APP_USBD_AUDIO_FEATURE_UNIT_CONTROL_MUTE), \
  121. APP_USBD_U16_TO_RAW_DSC(APP_USBD_AUDIO_FEATURE_UNIT_CONTROL_MUTE)
  122. /* Microphone descriptors */
  123. /**
  124. * @brief Audio class specific format descriptor
  125. */
  126. APP_USBD_AUDIO_FORMAT_DESCRIPTOR(m_mic_form_desc,
  127. APP_USBD_AUDIO_AS_FORMAT_I_DSC( /* Format type 1 descriptor */
  128. 2, /* Number of channels */
  129. 2, /* Subframe size */
  130. 16, /* Bit resolution */
  131. 1, /* Frequency type */
  132. APP_USBD_U24_TO_RAW_DSC(48000)) /* Frequency */
  133. );
  134. /**
  135. * @brief Audio class input terminal descriptor
  136. */
  137. APP_USBD_AUDIO_INPUT_DESCRIPTOR(m_mic_inp_desc,
  138. APP_USBD_AUDIO_INPUT_TERMINAL_DSC(
  139. 1, /* Terminal ID */
  140. APP_USBD_AUDIO_TERMINAL_IN_MICROPHONE, /* Terminal type */
  141. 2, /* Number of channels */
  142. MIC_TERMINAL_CH_CONFIG()) /* Channels config */
  143. );
  144. /**
  145. * @brief Audio class output terminal descriptor
  146. */
  147. APP_USBD_AUDIO_OUTPUT_DESCRIPTOR(m_mic_out_desc,
  148. APP_USBD_AUDIO_OUTPUT_TERMINAL_DSC(
  149. 3, /* Terminal ID */
  150. APP_USBD_AUDIO_TERMINAL_USB_STREAMING, /* Terminal type */
  151. 2) /* Source ID */
  152. );
  153. /**
  154. * @brief Audio class feature unit descriptor
  155. */
  156. APP_USBD_AUDIO_FEATURE_DESCRIPTOR(m_mic_fea_desc,
  157. APP_USBD_AUDIO_FEATURE_UNIT_DSC(
  158. 2, /* Unit ID */
  159. 1, /* Source ID */
  160. MIC_FEATURE_CONTROLS()) /* List of controls */
  161. );
  162. /* Headphones descriptors */
  163. /**
  164. * @brief Audio class specific format III descriptor
  165. */
  166. APP_USBD_AUDIO_FORMAT_DESCRIPTOR(m_hp_form_desc,
  167. APP_USBD_AUDIO_AS_FORMAT_III_DSC( /* Format type 3 descriptor */
  168. 2, /* Number of channels */
  169. 2, /* Subframe size */
  170. 16, /* Bit resolution */
  171. 1, /* Frequency type */
  172. APP_USBD_U24_TO_RAW_DSC(48000)) /* Frequency */
  173. );
  174. /**
  175. * @brief Audio class input terminal descriptor
  176. */
  177. APP_USBD_AUDIO_INPUT_DESCRIPTOR(m_hp_inp_desc,
  178. APP_USBD_AUDIO_INPUT_TERMINAL_DSC(
  179. 1, /* Terminal ID */
  180. APP_USBD_AUDIO_TERMINAL_USB_STREAMING, /* Terminal type */
  181. 2, /* Number of channels */
  182. HP_TERMINAL_CH_CONFIG()) /* Channels config */
  183. );
  184. /**
  185. * @brief Audio class output terminal descriptor
  186. */
  187. APP_USBD_AUDIO_OUTPUT_DESCRIPTOR(m_hp_out_desc,
  188. APP_USBD_AUDIO_OUTPUT_TERMINAL_DSC(
  189. 3, /* Terminal ID */
  190. APP_USBD_AUDIO_TERMINAL_OUT_HEADPHONES, /* Terminal type */
  191. 2) /* Source ID */
  192. );
  193. /**
  194. * @brief Audio class feature unit descriptor
  195. */
  196. APP_USBD_AUDIO_FEATURE_DESCRIPTOR(m_hp_fea_desc,
  197. APP_USBD_AUDIO_FEATURE_UNIT_DSC(
  198. 2, /* Unit ID */
  199. 1, /* Source ID */
  200. HP_FEATURE_CONTROLS()) /* List of controls */
  201. );
  202. /* Interfaces lists */
  203. /**
  204. * @brief Interfaces list passed to @ref APP_USBD_AUDIO_GLOBAL_DEF
  205. */
  206. #define HP_INTERFACES_CONFIG() APP_USBD_AUDIO_CONFIG_OUT(0, 1)
  207. /**
  208. * @brief Interfaces list passed to @ref APP_USBD_AUDIO_GLOBAL_DEF
  209. */
  210. #define MIC_INTERFACES_CONFIG() APP_USBD_AUDIO_CONFIG_IN(2, 3)
  211. /*lint -save -e26 -e64 -e123 -e505 -e651*/
  212. /**
  213. * @brief Headphone Audio class instance
  214. */
  215. APP_USBD_AUDIO_GLOBAL_DEF(m_app_audio_headphone,
  216. HP_INTERFACES_CONFIG(),
  217. hp_audio_user_ev_handler,
  218. &m_hp_form_desc,
  219. &m_hp_inp_desc,
  220. &m_hp_out_desc,
  221. &m_hp_fea_desc,
  222. 0,
  223. APP_USBD_AUDIO_AS_IFACE_FORMAT_PCM,
  224. 192,
  225. APP_USBD_AUDIO_SUBCLASS_AUDIOSTREAMING,
  226. 1
  227. );
  228. /**
  229. * @brief Microphone Audio class instance
  230. */
  231. APP_USBD_AUDIO_GLOBAL_DEF(m_app_audio_microphone,
  232. MIC_INTERFACES_CONFIG(),
  233. mic_audio_user_ev_handler,
  234. &m_mic_form_desc,
  235. &m_mic_inp_desc,
  236. &m_mic_out_desc,
  237. &m_mic_fea_desc,
  238. 0,
  239. APP_USBD_AUDIO_AS_IFACE_FORMAT_PCM,
  240. 192,
  241. APP_USBD_AUDIO_SUBCLASS_AUDIOSTREAMING,
  242. 3
  243. );
  244. /*lint -restore*/
  245. /**
  246. * @brief Internal audio temporary buffer
  247. */
  248. static int16_t m_temp_buffer[2 * BUFFER_SIZE];
  249. /**
  250. * @brief The size of last received block from the microphone
  251. */
  252. static size_t m_temp_buffer_size;
  253. /**
  254. * @brief Actual headphones mute
  255. */
  256. static uint8_t m_mute_hp;
  257. /**
  258. * @brief Actual sampling frequency
  259. */
  260. static uint32_t m_freq_hp;
  261. /**
  262. * @brief Actual microphone mute state
  263. */
  264. static uint8_t m_mute_mic;
  265. /**
  266. * @brief Actual microphone sampling frequency
  267. */
  268. static uint32_t m_freq_mic;
  269. /**
  270. * @brief Audio class specific request handle (headphones)
  271. */
  272. static void hp_audio_user_class_req(app_usbd_class_inst_t const * p_inst)
  273. {
  274. app_usbd_audio_t const * p_audio = app_usbd_audio_class_get(p_inst);
  275. app_usbd_audio_req_t * p_req = app_usbd_audio_class_request_get(p_audio);
  276. UNUSED_VARIABLE(m_mute_hp);
  277. UNUSED_VARIABLE(m_freq_hp);
  278. switch (p_req->req_target)
  279. {
  280. case APP_USBD_AUDIO_CLASS_REQ_IN:
  281. if (p_req->req_type == APP_USBD_AUDIO_REQ_SET_CUR)
  282. {
  283. //Only mute control is defined
  284. p_req->payload[0] = m_mute_hp;
  285. }
  286. break;
  287. case APP_USBD_AUDIO_CLASS_REQ_OUT:
  288. if (p_req->req_type == APP_USBD_AUDIO_REQ_SET_CUR)
  289. {
  290. //Only mute control is defined
  291. m_mute_hp = p_req->payload[0];
  292. }
  293. break;
  294. case APP_USBD_AUDIO_EP_REQ_IN:
  295. break;
  296. case APP_USBD_AUDIO_EP_REQ_OUT:
  297. if (p_req->req_type == APP_USBD_AUDIO_REQ_SET_CUR)
  298. {
  299. //Only set frequency is supported
  300. m_freq_hp = uint24_decode(p_req->payload);
  301. }
  302. break;
  303. default:
  304. break;
  305. }
  306. }
  307. /**
  308. * @brief Audio class specific request handle (microphone)
  309. */
  310. static void mic_audio_user_class_req(app_usbd_class_inst_t const * p_inst)
  311. {
  312. app_usbd_audio_t const * p_audio = app_usbd_audio_class_get(p_inst);
  313. app_usbd_audio_req_t * p_req = app_usbd_audio_class_request_get(p_audio);
  314. UNUSED_VARIABLE(m_mute_mic);
  315. UNUSED_VARIABLE(m_freq_mic);
  316. switch (p_req->req_target)
  317. {
  318. case APP_USBD_AUDIO_CLASS_REQ_IN:
  319. if (p_req->req_type == APP_USBD_AUDIO_REQ_SET_CUR)
  320. {
  321. //Only mute control is defined
  322. p_req->payload[0] = m_mute_mic;
  323. }
  324. break;
  325. case APP_USBD_AUDIO_CLASS_REQ_OUT:
  326. if (p_req->req_type == APP_USBD_AUDIO_REQ_SET_CUR)
  327. {
  328. //Only mute control is defined
  329. m_mute_mic = p_req->payload[0];
  330. }
  331. break;
  332. case APP_USBD_AUDIO_EP_REQ_IN:
  333. break;
  334. case APP_USBD_AUDIO_EP_REQ_OUT:
  335. if (p_req->req_type == APP_USBD_AUDIO_REQ_SET_CUR)
  336. {
  337. //Only set frequency is supported
  338. m_freq_mic = uint24_decode(p_req->payload);
  339. }
  340. break;
  341. default:
  342. break;
  343. }
  344. }
  345. /**
  346. * @brief User event handler @ref app_usbd_audio_user_ev_handler_t (headphones)
  347. */
  348. static void hp_audio_user_ev_handler(app_usbd_class_inst_t const * p_inst,
  349. app_usbd_audio_user_event_t event)
  350. {
  351. app_usbd_audio_t const * p_audio = app_usbd_audio_class_get(p_inst);
  352. UNUSED_VARIABLE(p_audio);
  353. switch (event)
  354. {
  355. case APP_USBD_AUDIO_USER_EVT_CLASS_REQ:
  356. hp_audio_user_class_req(p_inst);
  357. break;
  358. case APP_USBD_AUDIO_USER_EVT_RX_DONE:
  359. {
  360. ret_code_t ret;
  361. /* Block from headphones copied into buffer, send it into microphone input */
  362. ret = app_usbd_audio_class_tx_start(&m_app_audio_microphone.base, m_temp_buffer, m_temp_buffer_size);
  363. if (NRF_SUCCESS == ret)
  364. {
  365. bsp_board_led_invert(LED_AUDIO_RX);
  366. }
  367. break;
  368. }
  369. default:
  370. break;
  371. }
  372. }
  373. /**
  374. * @brief User event handler @ref app_usbd_audio_user_ev_handler_t (microphone)
  375. */
  376. static void mic_audio_user_ev_handler(app_usbd_class_inst_t const * p_inst,
  377. app_usbd_audio_user_event_t event)
  378. {
  379. app_usbd_audio_t const * p_audio = app_usbd_audio_class_get(p_inst);
  380. UNUSED_VARIABLE(p_audio);
  381. switch (event)
  382. {
  383. case APP_USBD_AUDIO_USER_EVT_CLASS_REQ:
  384. mic_audio_user_class_req(p_inst);
  385. break;
  386. case APP_USBD_AUDIO_USER_EVT_TX_DONE:
  387. {
  388. bsp_board_led_invert(LED_AUDIO_TX);
  389. break;
  390. }
  391. default:
  392. break;
  393. }
  394. }
  395. static void hp_sof_ev_handler(uint16_t framecnt)
  396. {
  397. UNUSED_VARIABLE(framecnt);
  398. if (APP_USBD_STATE_Configured != app_usbd_core_state_get())
  399. {
  400. return;
  401. }
  402. size_t rx_size = app_usbd_audio_class_rx_size_get(&m_app_audio_headphone.base);
  403. m_temp_buffer_size = rx_size;
  404. if (rx_size > 0)
  405. {
  406. ASSERT(rx_size <= sizeof(m_temp_buffer));
  407. ret_code_t ret;
  408. ret = app_usbd_audio_class_rx_start(&m_app_audio_headphone.base, m_temp_buffer, rx_size);
  409. if (NRF_SUCCESS != ret)
  410. {
  411. NRF_LOG_ERROR("Cannot start RX transfer from headphone\r\n");
  412. }
  413. }
  414. }
  415. /**
  416. * @brief USBD library specific event handler.
  417. *
  418. * @param event USBD library event.
  419. */
  420. static void usbd_user_ev_handler(app_usbd_event_type_t event)
  421. {
  422. switch (event)
  423. {
  424. case APP_USBD_EVT_DRV_SOF:
  425. break;
  426. case APP_USBD_EVT_DRV_SUSPEND:
  427. bsp_board_leds_off();
  428. break;
  429. case APP_USBD_EVT_DRV_RESUME:
  430. bsp_board_led_on(LED_USB_RESUME);
  431. break;
  432. case APP_USBD_EVT_STARTED:
  433. bsp_board_led_on(LED_USB_START);
  434. break;
  435. case APP_USBD_EVT_STOPPED:
  436. app_usbd_disable();
  437. bsp_board_leds_off();
  438. break;
  439. case APP_USBD_EVT_POWER_DETECTED:
  440. NRF_LOG_INFO("USB power detected");
  441. if (!nrf_drv_usbd_is_enabled())
  442. {
  443. app_usbd_enable();
  444. }
  445. break;
  446. case APP_USBD_EVT_POWER_REMOVED:
  447. NRF_LOG_INFO("USB power removed");
  448. app_usbd_stop();
  449. break;
  450. case APP_USBD_EVT_POWER_READY:
  451. NRF_LOG_INFO("USB ready");
  452. app_usbd_start();
  453. break;
  454. default:
  455. break;
  456. }
  457. }
  458. int main(void)
  459. {
  460. ret_code_t ret;
  461. static const app_usbd_config_t usbd_config = {
  462. .ev_state_proc = usbd_user_ev_handler,
  463. .enable_sof = true
  464. };
  465. ret = NRF_LOG_INIT(NULL);
  466. APP_ERROR_CHECK(ret);
  467. NRF_LOG_DEFAULT_BACKENDS_INIT();
  468. ret = nrf_drv_clock_init();
  469. APP_ERROR_CHECK(ret);
  470. NRF_LOG_INFO("USBD audio example started.");
  471. // Initialize LEDs and buttons
  472. bsp_board_init(BSP_INIT_LEDS | BSP_INIT_BUTTONS);
  473. ret = app_usbd_init(&usbd_config);
  474. APP_ERROR_CHECK(ret);
  475. app_usbd_class_inst_t const * class_inst_hp =
  476. app_usbd_audio_class_inst_get(&m_app_audio_headphone);
  477. ret = app_usbd_audio_sof_interrupt_register(class_inst_hp, hp_sof_ev_handler);
  478. APP_ERROR_CHECK(ret);
  479. ret = app_usbd_class_append(class_inst_hp);
  480. APP_ERROR_CHECK(ret);
  481. app_usbd_class_inst_t const * class_inst_mic =
  482. app_usbd_audio_class_inst_get(&m_app_audio_microphone);
  483. ret = app_usbd_class_append(class_inst_mic);
  484. APP_ERROR_CHECK(ret);
  485. if (USBD_POWER_DETECTION)
  486. {
  487. ret = app_usbd_power_events_enable();
  488. APP_ERROR_CHECK(ret);
  489. }
  490. else
  491. {
  492. NRF_LOG_INFO("No USB power detection enabled\r\nStarting USB now");
  493. app_usbd_enable();
  494. app_usbd_start();
  495. }
  496. while (true)
  497. {
  498. while (app_usbd_event_queue_process())
  499. {
  500. /* Nothing to do */
  501. }
  502. UNUSED_RETURN_VALUE(NRF_LOG_PROCESS());
  503. /* Sleep CPU only if there was no interrupt since last loop processing */
  504. __WFE();
  505. }
  506. }
  507. /** @} */