audio_driver_shell.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993
  1. /*
  2. * Copyright (c) 2020 Actions Semiconductor Co., Ltd
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /**
  7. * @file
  8. * @brief Audio Driver Shell implementation
  9. */
  10. #include <shell/shell.h>
  11. #include <string.h>
  12. #include <stdlib.h>
  13. #include <sys/byteorder.h>
  14. #include <fs/fs.h>
  15. #include "../phy_audio_common.h"
  16. #include <drivers/audio/audio_out.h>
  17. #include <drivers/audio/audio_in.h>
  18. #include <board_cfg.h>
  19. #include <sys/ring_buffer.h>
  20. #include <logging/log.h>
  21. LOG_MODULE_REGISTER(ashell, LOG_LEVEL_INF);
  22. #ifdef CONFIG_AUDIO_DRIVER_STREAM_SHELL
  23. #define SHELL_ADC_USE_DIFFERENT_BUFFER
  24. //#define DAC_PCMBUF_DEBUG
  25. #define SHELL_ADC_USE_STATIC_BUFFER
  26. #define SHELL_DAC_TRIGGER_CONTROL_DEFAULT (BIT(3) | BIT(1) | BIT(0))
  27. #define SHELL_CMD_CHL_INDEX_KEY "chl_id="
  28. #define SHELL_CMD_FIFO_SRC_KEY "fifosrc_type="
  29. #define SHELL_CMD_CHL_TYPE_KEY "chl_type="
  30. #define SHELL_CMD_CHL_WIDTH_KEY "chl_width="
  31. #define SHELL_CMD_SAMPLE_RATE_KEY "sr="
  32. #define SHELL_CMD_FIFO_TYPE_KEY "fifo_type="
  33. #define SHELL_CMD_LEFT_VOL_KEY "left_vol="
  34. #define SHELL_CMD_RIGHT_VOL_KEY "right_vol="
  35. #define SHELL_CMD_RELOAD_KEY "reload_en"
  36. #define SHELL_CMD_CH0_GAIN_KEY "ch0_gain="
  37. #define SHELL_CMD_CH1_GAIN_KEY "ch1_gain="
  38. #define SHELL_CMD_CH2_GAIN_KEY "ch2_gain="
  39. #define SHELL_CMD_CH3_GAIN_KEY "ch3_gain="
  40. #define SHELL_CMD_INPUT_DEV_KEY "input_dev="
  41. #define SHELL_CMD_DUMP_LEN_KEY "dump_len="
  42. #define SHELL_CMD_IOCTL_PARAM_KEY "param="
  43. #define SHELL_CMD_IOCTL_CMD_KEY "cmd="
  44. #define SHELL_CMD_IOCTL_CMD_SPDIF_CSL_KEY "csl="
  45. #define SHELL_CMD_IOCTL_CMD_SPDIF_CSH_KEY "csh="
  46. #define SHELL_CMD_IOCTL_CMD_BIND_ID_KEY "bind_id="
  47. #define SHELL_CMD_RECORD_FILE "rec_file"
  48. #define SHELL_CMD_ADDA_KEY "adda_en"
  49. #define SHELL_CMD_ZERO_KEY "zero_mute"
  50. #define SHELL_CMD_PLAY_MONO_KEY "mono_en"
  51. #define SHELL_CMD_PLAY_PERF_DETAIL_KEY "perf_detail"
  52. #define SHELL_CMD_HW_TRIGGER_SRC_KEY "trigger_src="
  53. #define SHELL_CMD_DMA_INTERLEAVED_EN "interleaved_en"
  54. #define REC_FIEE_PATH "/NOR:/rec.pcm"
  55. struct fs_file_t record_file;
  56. struct cap_device_info {
  57. struct k_work work;
  58. char name[16];
  59. uint8_t *buf;
  60. int buf_size;
  61. } cap_device;
  62. #if 1
  63. static const uint16_t L1khz_R2khz_Dat_Sine[] = {
  64. 0x0000, 0x0000,
  65. 0x10B5, 0x2120,
  66. 0x2120, 0x3FFF,
  67. 0x30FB, 0x5A82,
  68. 0x3FFF, 0x6ED9,
  69. 0x4DEB, 0x7BA3,
  70. 0x5A82, 0x7FFF,
  71. 0x658C, 0x7BA3,
  72. 0x6ED9, 0x6ED9,
  73. 0x7641, 0x5A82,
  74. 0x7BA3, 0x3FFF,
  75. 0x7EE7, 0x2120,
  76. 0x7FFF, 0x0000,
  77. 0x7EE7, 0xDEDF,
  78. 0x7BA3, 0xC000,
  79. 0x7641, 0xA57D,
  80. 0x6ED9, 0x9126,
  81. 0x658C, 0x845C,
  82. 0x5A82, 0x8000,
  83. 0x4DEB, 0x845C,
  84. 0x3FFF, 0x9126,
  85. 0x30FB, 0xA57D,
  86. 0x2120, 0xC000,
  87. 0x10B5, 0xDEDF,
  88. 0x0000, 0x0000,
  89. 0xEF4A, 0x2120,
  90. 0xDEDF, 0x3FFF,
  91. 0xCF04, 0x5A82,
  92. 0xC000, 0x6ED9,
  93. 0xB214, 0x7BA3,
  94. 0xA57D, 0x7FFF,
  95. 0x9A73, 0x7BA3,
  96. 0x9126, 0x6ED9,
  97. 0x89BE, 0x5A82,
  98. 0x845C, 0x3FFF,
  99. 0x8118, 0x2120,
  100. 0x8000, 0x0000,
  101. 0x8118, 0xDEDF,
  102. 0x845C, 0xC000,
  103. 0x89BE, 0xA57D,
  104. 0x9126, 0x9126,
  105. 0x9A73, 0x845C,
  106. 0xA57D, 0x8000,
  107. 0xB214, 0x845C,
  108. 0xC000, 0x9126,
  109. 0xCF04, 0xA57D,
  110. 0xDEDF, 0xC000,
  111. 0xEF4A, 0xDEDF
  112. };
  113. #else
  114. static const uint16_t L1khz_R2khz_Dat_Sine[] = {
  115. 0x1234, 0x5678,
  116. 0x1234, 0x5678,
  117. 0x1234, 0x5678,
  118. 0x1234, 0x5678,
  119. 0x1234, 0x5678,
  120. 0x1234, 0x5678,
  121. 0x1234, 0x5678,
  122. 0x1234, 0x5678,
  123. 0x1234, 0x5678,
  124. 0x1234, 0x5678,
  125. 0x1234, 0x5678,
  126. 0x1234, 0x5678,
  127. 0x1234, 0x5678,
  128. 0x1234, 0x5678,
  129. 0x1234, 0x5678,
  130. 0x1234, 0x5678,
  131. 0x1234, 0x5678,
  132. 0x1234, 0x5678,
  133. 0x1234, 0x5678,
  134. 0x1234, 0x5678,
  135. 0x1234, 0x5678,
  136. 0x1234, 0x5678,
  137. 0x1234, 0x5678,
  138. 0x1234, 0x5678,
  139. 0x1234, 0x5678,
  140. 0x1234, 0x5678,
  141. 0x1234, 0x5678,
  142. 0x1234, 0x5678,
  143. 0x1234, 0x5678,
  144. 0x1234, 0x5678,
  145. 0x1234, 0x5678,
  146. 0x1234, 0x5678,
  147. 0x1234, 0x5678,
  148. 0x1234, 0x5678,
  149. 0x1234, 0x5678,
  150. 0x1234, 0x5678,
  151. 0x1234, 0x5678,
  152. 0x1234, 0x5678,
  153. 0x1234, 0x5678,
  154. 0x1234, 0x5678,
  155. 0x1234, 0x5678,
  156. 0x1234, 0x5678,
  157. 0x1234, 0x5678,
  158. 0x1234, 0x5678,
  159. 0x1234, 0x5678,
  160. 0x1234, 0x5678,
  161. 0x1234, 0x5678,
  162. 0x1234, 0x5678
  163. };
  164. #endif
  165. #if 1
  166. static const uint32_t L1khz_R2khz_Dat_Sine_24bits[]= {
  167. 0x00000000,0x00000000,
  168. 0x10B51400,0x2120FB00,
  169. 0x2120FB00,0x3FFFFF00,
  170. 0x30FBC400,0x5A827800,
  171. 0x3FFFFF00,0x6ED9EA00,
  172. 0x4DEBE400,0x7BA37400,
  173. 0x5A827800,0x7FFFFF00,
  174. 0x658C9900,0x7BA37400,
  175. 0x6ED9EA00,0x6ED9EA00,
  176. 0x7641AE00,0x5A827900,
  177. 0x7BA37400,0x3FFFFF00,
  178. 0x7EE7A900,0x2120FB00,
  179. 0x7FFFFF00,0x00000000,
  180. 0x7EE7A900,0xDEDF0600,
  181. 0x7BA37400,0xC0000100,
  182. 0x7641AE00,0xA57D8800,
  183. 0x6ED9EA00,0x91261600,
  184. 0x658C9900,0x845C8D00,
  185. 0x5A827900,0x80000200,
  186. 0x4DEBE400,0x845C8C00,
  187. 0x3FFFFF00,0x91261500,
  188. 0x30FBC500,0xA57D8700,
  189. 0x2120FB00,0xC0000000,
  190. 0x10B51500,0xDEDF0400,
  191. 0x00000000,0x00000000,
  192. 0xEF4AEC00,0x2120FB00,
  193. 0xDEDF0600,0x3FFFFF00,
  194. 0xCF043C00,0x5A827800,
  195. 0xC0000100,0x6ED9EA00,
  196. 0xB2141D00,0x7BA37400,
  197. 0xA57D8800,0x7FFFFF00,
  198. 0x9A736700,0x7BA37400,
  199. 0x91261600,0x6ED9EA00,
  200. 0x89BE5200,0x5A827900,
  201. 0x845C8D00,0x3FFFFF00,
  202. 0x81185700,0x2120FB00,
  203. 0x80000200,0x00000000,
  204. 0x81185700,0xDEDF0600,
  205. 0x845C8C00,0xC0000100,
  206. 0x89BE5200,0xA57D8800,
  207. 0x91261500,0x91261600,
  208. 0x9A736700,0x845C8D00,
  209. 0xA57D8700,0x80000200,
  210. 0xB2141B00,0x845C8C00,
  211. 0xC0000000,0x91261500,
  212. 0xCF043B00,0xA57D8700,
  213. 0xDEDF0400,0xC0000000,
  214. 0xEF4AEB00,0xDEDF0400
  215. };
  216. #else
  217. static const uint32_t L1khz_R2khz_Dat_Sine_24bits[]= {
  218. 0x12345678,0x33335555,
  219. 0x12345678,0x33335555,
  220. 0x12345678,0x33335555,
  221. 0x12345678,0x33335555,
  222. 0x12345678,0x33335555,
  223. 0x12345678,0x33335555,
  224. 0x12345678,0x33335555,
  225. 0x12345678,0x33335555,
  226. 0x12345678,0x33335555,
  227. 0x12345678,0x33335555,
  228. 0x12345678,0x33335555,
  229. 0x12345678,0x33335555,
  230. 0x12345678,0x33335555,
  231. 0x12345678,0x33335555,
  232. 0x12345678,0x33335555,
  233. 0x12345678,0x33335555,
  234. 0x12345678,0x33335555,
  235. 0x12345678,0x33335555,
  236. 0x12345678,0x33335555,
  237. 0x12345678,0x33335555,
  238. 0x12345678,0x33335555,
  239. 0x12345678,0x33335555,
  240. 0x12345678,0x33335555,
  241. 0x12345678,0x33335555,
  242. 0x12345678,0x33335555,
  243. 0x12345678,0x33335555,
  244. 0x12345678,0x33335555,
  245. 0x12345678,0x33335555,
  246. 0x12345678,0x33335555,
  247. 0x12345678,0x33335555,
  248. 0x12345678,0x33335555,
  249. 0x12345678,0x33335555,
  250. 0x12345678,0x33335555,
  251. 0x12345678,0x33335555,
  252. 0x12345678,0x33335555,
  253. 0x12345678,0x33335555,
  254. 0x12345678,0x33335555,
  255. 0x12345678,0x33335555,
  256. 0x12345678,0x33335555,
  257. 0x12345678,0x33335555,
  258. 0x12345678,0x33335555,
  259. 0x12345678,0x33335555,
  260. 0x12345678,0x33335555,
  261. 0x12345678,0x33335555,
  262. 0x12345678,0x33335555,
  263. 0x12345678,0x33335555,
  264. 0x12345678,0x33335555,
  265. 0x12345678,0x33335555
  266. };
  267. #endif
  268. /**
  269. * struct audio_play_attr
  270. * @brief Audio play attribute.
  271. */
  272. struct audio_play_attr {
  273. uint8_t chl_type; /* channel type */
  274. uint8_t chl_width; /* channel width */
  275. uint8_t sr; /* sample rate */
  276. uint8_t fifo_type; /* fifo type */
  277. int32_t left_vol; /* left volume */
  278. int32_t right_vol; /* right volume */
  279. uint8_t id; /* channel index */
  280. uint8_t hw_trigger_src; /* hw irq sources to trigger DAC start */
  281. uint8_t reload_en : 1; /* reload enable flag */
  282. uint8_t adda_en : 1; /* ADC => DAC enable */
  283. uint8_t mono_en : 1; /* mono mode enable */
  284. uint8_t zero_mute : 1; /*zero mute */
  285. uint8_t perf_detail_en : 1; /* performance detail information enable */
  286. uint8_t dma_separated_en : 1; /* DMA interleaved enable */
  287. uint8_t reserved : 3;
  288. };
  289. /**
  290. * struct audio_play_objects
  291. * @brief The resource that belongs to audio player.
  292. */
  293. struct audio_play_object {
  294. #define AUDIO_PLAY_START_FLAG (1 << 0)
  295. #define AUDIO_PLAY_SAMPLES_CNT_EN_FLAG (1 << 1)
  296. #define AUDIO_PLAY_SDM_CNT_EN_FLAG (1 << 2)
  297. struct device *aout_dev;
  298. uint8_t *audio_buffer;
  299. uint32_t audio_buffer_size;
  300. void *aout_handle;
  301. uint32_t play_total_size;
  302. uint32_t flags;
  303. #ifdef DAC_PCMBUF_DEBUG
  304. uint32_t pcmbuf_avail_origin;
  305. uint32_t pcmbuf_avail;
  306. #endif
  307. struct audio_play_attr attribute;
  308. };
  309. /* @brief audio play handler */
  310. typedef struct audio_play_object *audio_play_handle;
  311. /* audio player instance */
  312. #define AUDIO_PLAY_MAX_HANLDER (2)
  313. static audio_play_handle audio_player[AUDIO_PLAY_MAX_HANLDER];
  314. /* statistics audio play total size */
  315. static uint64_t audio_play_total_size;
  316. /**
  317. * struct audio_record_attr
  318. * @brief Audio record attribute.
  319. */
  320. struct audio_record_attr {
  321. uint8_t chl_type; /* channel type */
  322. uint8_t chl_width; /* channel width */
  323. uint8_t sr; /* sample rate */
  324. uint8_t dump_len; /* the length of data that per-second dump */
  325. uint8_t id; /* channel id */
  326. int16_t ch_gain[ADC_CH_NUM_MAX]; /* ADC left gain */
  327. uint16_t input_dev; /* input audio device */
  328. uint8_t hw_trigger_src; /* hw irq sources to trigger ADC start */
  329. uint8_t adda_en : 1; /* ADC => DAC enable */
  330. uint8_t rec_file : 1; /* record to file */
  331. uint8_t dma_separated_en : 1; /* DMA interleaved enable */
  332. };
  333. /**
  334. * struct audio_record_object
  335. * @brief The resource that belongs to audio recorder.
  336. */
  337. struct audio_record_object {
  338. struct device *ain_dev;
  339. uint8_t *audio_buffer;
  340. uint32_t audio_buffer_size;
  341. void *ain_handle;
  342. struct audio_record_attr attribute;
  343. };
  344. /* @brief audio record handler */
  345. typedef struct audio_record_object *audio_record_handle;
  346. /* audio recorder instance */
  347. #define AUDIO_RECORD_MAX_HANLDER (2)
  348. static audio_record_handle audio_recorder[AUDIO_RECORD_MAX_HANLDER];
  349. #define AUDIO_RECORD_SAVE_BUFF_CNT (0x1)
  350. #define SHELL_AUDIO_BUFFER_SIZE (1024)
  351. #define SHELL_AUDIO_ADDA_BUFFER_SIZE (SHELL_AUDIO_BUFFER_SIZE*AUDIO_RECORD_SAVE_BUFF_CNT)
  352. #define SHELL_AUDIO_REC_BUFFER_CNT (128)
  353. #define SHELL_AUDIO_RECORD_SAVE_BUFFER_SIZE (SHELL_AUDIO_BUFFER_SIZE*AUDIO_RECORD_SAVE_BUFF_CNT)
  354. #ifdef SHELL_ADC_USE_STATIC_BUFFER
  355. static int16_t shell_audio_buffer[SHELL_AUDIO_BUFFER_SIZE/2];
  356. #else
  357. extern char __share_ram_start[];
  358. #define SHARE_RAM_START_OFFSET (0x2000)
  359. static uint16_t *shell_audio_buffer = __share_ram_start + SHARE_RAM_START_OFFSET;
  360. #endif
  361. #ifdef SHELL_ADC_USE_DIFFERENT_BUFFER
  362. #ifdef SHELL_ADC_USE_STATIC_BUFFER
  363. static int16_t shell_audio_buffer1[SHELL_AUDIO_BUFFER_SIZE/2];
  364. static int16_t shell_audio_adda_buffer[SHELL_AUDIO_ADDA_BUFFER_SIZE/2];
  365. static int16_t shell_audio_record_buffer[SHELL_AUDIO_RECORD_SAVE_BUFFER_SIZE/2];
  366. #else
  367. static uint16_t *shell_audio_buffer1 = __share_ram_start + SHELL_AUDIO_BUFFER_SIZE \
  368. + SHARE_RAM_START_OFFSET;
  369. static uint16_t *shell_audio_adda_buffer = __share_ram_start + SHELL_AUDIO_BUFFER_SIZE \
  370. + SHELL_AUDIO_BUFFER_SIZE + SHARE_RAM_START_OFFSET;
  371. #endif
  372. #endif
  373. static struct ring_buf shell_audio_ringbuf;
  374. int rec_buff_cnt;
  375. static void rec_to_file(struct k_work *item)
  376. {
  377. uint32_t delta;
  378. static uint32_t save_cap_timestamp = 0;
  379. struct cap_device_info *the_device =
  380. CONTAINER_OF(item, struct cap_device_info, work);
  381. delta = (uint32_t)k_cyc_to_ns_floor64(k_cycle_get_32() - save_cap_timestamp);
  382. rec_buff_cnt++;
  383. if (delta > 1000000000UL)
  384. {
  385. printk("%s,buf:%p->size:%d",
  386. the_device->name, the_device->buf, the_device->buf_size);
  387. AUDIO_DUMP_MEM(the_device->buf,16);
  388. save_cap_timestamp = k_cycle_get_32();
  389. }
  390. if (rec_buff_cnt < SHELL_AUDIO_REC_BUFFER_CNT)
  391. fs_write(&record_file, the_device->buf, the_device->buf_size);
  392. else
  393. fs_close(&record_file);
  394. return;
  395. }
  396. static int rec_fs_init(void)
  397. {
  398. int err;
  399. off_t offset = -1;
  400. fs_file_t_init(&record_file);
  401. err = fs_open(&record_file, REC_FIEE_PATH, FS_O_CREATE | FS_O_RDWR);
  402. if (err) {
  403. LOG_ERR("Failed to open %s (%d)", REC_FIEE_PATH, err);
  404. }
  405. if (offset < 0) {
  406. err = fs_seek(&record_file, 0, FS_SEEK_END);
  407. } else {
  408. err = fs_seek(&record_file, offset, FS_SEEK_SET);
  409. }
  410. if (err) {
  411. LOG_ERR("Failed to seek %s (%d)", REC_FIEE_PATH, err);
  412. fs_close(&record_file);
  413. }
  414. return 0 ;
  415. }
  416. /* @brief find the value according to the key */
  417. static int cmd_search_value_by_key(size_t argc, char **argv,
  418. const char *key, uint32_t *value)
  419. {
  420. int i;
  421. char *p;
  422. for (i = 1; i < argc; i++) {
  423. if (strstr(argv[i], key)) {
  424. LOG_DBG("found string:%s", argv[i]);
  425. /* found key */
  426. p = strchr(argv[i], '=');
  427. if (!p) {
  428. *value = 1;
  429. } else {
  430. LOG_DBG("found key value:%s", p);
  431. *value = strtoul(++p, NULL, 0);
  432. }
  433. break;
  434. }
  435. }
  436. if (i == argc)
  437. return -ENXIO;
  438. return 0;
  439. }
  440. /* @brief prepare audio PCM data */
  441. static void audio_prepare_data(audio_play_handle hdl, uint8_t *buf, uint32_t len, uint8_t width)
  442. {
  443. uint32_t fill_len;
  444. uint16_t count;
  445. uint16_t i, j;
  446. if (hdl)
  447. hdl->audio_buffer = buf;
  448. if (width == CHANNEL_WIDTH_16BITS) {
  449. fill_len = len / sizeof(L1khz_R2khz_Dat_Sine) * sizeof(L1khz_R2khz_Dat_Sine);
  450. if (hdl)
  451. hdl->audio_buffer_size = fill_len;
  452. count = fill_len / sizeof(L1khz_R2khz_Dat_Sine);
  453. const uint16_t *src = L1khz_R2khz_Dat_Sine;
  454. uint16_t *dst = (uint16_t *)buf;
  455. for (i = 0; i < count; i++) {
  456. for (j = 0; j < sizeof(L1khz_R2khz_Dat_Sine)/2; j++)
  457. *dst++ = src[j];
  458. }
  459. } else {
  460. /* 32bits width */
  461. fill_len = len / sizeof(L1khz_R2khz_Dat_Sine_24bits) * sizeof(L1khz_R2khz_Dat_Sine_24bits);
  462. if (hdl)
  463. hdl->audio_buffer_size = fill_len;
  464. count = fill_len / sizeof(L1khz_R2khz_Dat_Sine_24bits);
  465. const uint32_t *src = L1khz_R2khz_Dat_Sine_24bits;
  466. uint32_t *dst = (uint32_t *)buf;
  467. for (i = 0; i < count; i++) {
  468. for (j = 0; j < sizeof(L1khz_R2khz_Dat_Sine_24bits)/4; j++) {
  469. *dst++ = src[j];
  470. }
  471. }
  472. }
  473. }
  474. /* @brief audio i2stx SRD callback */
  475. static int audio_i2stx_srd_cb(void *cb_data, uint32_t cmd, void *param)
  476. {
  477. switch (cmd) {
  478. case I2STX_SRD_FS_CHANGE:
  479. LOG_INF("New sample rate %d", *(audio_sr_sel_e *)param);
  480. break;
  481. case I2STX_SRD_WL_CHANGE:
  482. LOG_INF("New width length %d", *(audio_i2s_srd_wl_e *)param);
  483. break;
  484. case I2STX_SRD_TIMEOUT:
  485. LOG_INF("SRD timeout");
  486. break;
  487. default:
  488. LOG_ERR("Error command %d", cmd);
  489. return -EFAULT;
  490. }
  491. return 0;
  492. }
  493. /* @brief dump the detail informatioin of audio play */
  494. static void audio_play_perf_detail(void)
  495. {
  496. uint8_t i;
  497. int ret;
  498. uint32_t count = 0, sdm_count = 0, sdm_stable_count = 0;
  499. for (i = 0; i < AUDIO_PLAY_MAX_HANLDER; i++) {
  500. if (audio_player[i]->flags & AUDIO_PLAY_START_FLAG) {
  501. LOG_INF("** play[%d] performance %dB/s **",
  502. i, audio_player[i]->play_total_size);
  503. audio_play_total_size += audio_player[i]->play_total_size;
  504. audio_player[i]->play_total_size = 0;
  505. if (audio_player[i]->flags & AUDIO_PLAY_SAMPLES_CNT_EN_FLAG) {
  506. ret = audio_out_control(audio_player[i]->aout_dev,
  507. audio_player[i]->aout_handle, AOUT_CMD_GET_SAMPLE_CNT, &count);
  508. }
  509. if (audio_player[i]->flags & AUDIO_PLAY_SDM_CNT_EN_FLAG) {
  510. ret = audio_out_control(audio_player[i]->aout_dev,
  511. audio_player[i]->aout_handle, AOUT_CMD_GET_DAC_SDM_SAMPLE_CNT, &sdm_count);
  512. ret = audio_out_control(audio_player[i]->aout_dev,
  513. audio_player[i]->aout_handle, AOUT_CMD_GET_DAC_SDM_STABLE_SAMPLE_CNT, &sdm_stable_count);
  514. }
  515. if ((audio_player[i]->flags & AUDIO_PLAY_SAMPLES_CNT_EN_FLAG)
  516. || audio_player[i]->flags & AUDIO_PLAY_SDM_CNT_EN_FLAG)
  517. LOG_INF(" play[%d] counter samples:%d sdm:%d sdm lock:%d ", i, count, sdm_count, sdm_stable_count);
  518. #ifdef DAC_PCMBUF_DEBUG
  519. LOG_INF(" play[%d] pcmbuf avail origin 0x%x ", i, audio_player[i]->pcmbuf_avail_origin);
  520. LOG_INF(" play[%d] pcmbuf avail 0x%x ", i, audio_player[i]->pcmbuf_avail);
  521. #endif
  522. }
  523. }
  524. LOG_INF(" play total size %lld ", audio_play_total_size);
  525. }
  526. /* @brief The callback from the low level audio play device for the request to write more data */
  527. static int audio_play_write_data_cb(void *handle, uint32_t reason)
  528. {
  529. audio_play_handle hdl = (audio_play_handle)handle;
  530. uint32_t len, delta;
  531. uint8_t *buf = NULL;
  532. int ret;
  533. static uint32_t play_timestamp = 0;
  534. LOG_DBG("handle %p, cb reason %d", handle, reason);
  535. if (hdl->attribute.reload_en) {
  536. len = hdl->audio_buffer_size / 2;
  537. if (AOUT_DMA_IRQ_HF == reason) {
  538. buf = hdl->audio_buffer;
  539. LOG_DBG("DMA IRQ HF");
  540. } else if (AOUT_DMA_IRQ_TC == reason) {
  541. buf = hdl->audio_buffer + len;
  542. LOG_DBG("DMA IRQ TC");
  543. } else {
  544. LOG_ERR("invalid reason %d", reason);
  545. return -1;
  546. }
  547. } else {
  548. buf = hdl->audio_buffer;
  549. len = hdl->audio_buffer_size;
  550. }
  551. #ifdef CONFIG_AUDIO_OUT_DAC_PCMBUF_SUPPORT
  552. if ((AOUT_FIFO_DAC0 == hdl->attribute.fifo_type)
  553. || (AOUT_FIFO_DAC1 == hdl->attribute.fifo_type))
  554. if (AOUT_DMA_IRQ_TC == reason) {
  555. LOG_ERR("*");
  556. }
  557. #endif
  558. delta = (uint32_t)k_cyc_to_ns_floor64(k_cycle_get_32() - play_timestamp);
  559. if (hdl->flags & AUDIO_PLAY_START_FLAG)
  560. hdl->play_total_size += len;
  561. if (!hdl->attribute.reload_en) {
  562. #ifdef DAC_PCMBUF_DEBUG
  563. audio_out_control(hdl->aout_dev,
  564. hdl->aout_handle, AOUT_CMD_GET_FIFO_AVAILABLE_LEN,
  565. &hdl->pcmbuf_avail_origin);
  566. #endif
  567. if (hdl->attribute.adda_en && shell_audio_ringbuf.buf.buf32) {
  568. len = ring_buf_get(&shell_audio_ringbuf, buf, len);
  569. if (!len) {
  570. memset(buf, 0, hdl->audio_buffer_size / 2);
  571. len = hdl->audio_buffer_size / 2;
  572. }
  573. }
  574. ret = audio_out_write(hdl->aout_dev, hdl->aout_handle, buf, len);
  575. if (ret) {
  576. LOG_ERR("write data error:%d", ret);
  577. return ret;
  578. }
  579. #ifdef DAC_PCMBUF_DEBUG
  580. audio_out_control(hdl->aout_dev,
  581. hdl->aout_handle, AOUT_CMD_GET_FIFO_AVAILABLE_LEN,
  582. &hdl->pcmbuf_avail);
  583. #endif
  584. }
  585. if (delta > 1000000000UL) {
  586. play_timestamp = k_cycle_get_32();
  587. audio_play_perf_detail();
  588. }
  589. return 0;
  590. }
  591. /* @brief Configure the extral infomation to the low level audio play device */
  592. static void audio_play_ext_config(audio_play_handle handle)
  593. {
  594. int ret = 0;
  595. uint32_t count;
  596. if (!handle) {
  597. LOG_ERR("null handle");
  598. return;
  599. }
  600. LOG_DBG("perf_detail_en %d", handle->attribute.perf_detail_en);
  601. if (handle->attribute.hw_trigger_src) {
  602. --handle->attribute.hw_trigger_src;
  603. ret = audio_out_control(handle->aout_dev,
  604. handle->aout_handle, AOUT_CMD_SET_DAC_TRIGGER_SRC,
  605. &handle->attribute.hw_trigger_src);
  606. if (!ret) {
  607. LOG_INF("set DAC external IRQ trigger source %d ok",
  608. handle->attribute.hw_trigger_src);
  609. }
  610. dac_ext_trigger_ctl_t trigger_ctl = {0};
  611. trigger_ctl.trigger_ctl = (uint8_t)SHELL_DAC_TRIGGER_CONTROL_DEFAULT;
  612. ret = audio_out_control(handle->aout_dev, handle->aout_handle,
  613. AOUT_CMD_DAC_TRIGGER_CONTROL, &trigger_ctl);
  614. if (!ret) {
  615. LOG_INF("set DAC external IRQ trigger control %d ok",
  616. (uint32_t)SHELL_DAC_TRIGGER_CONTROL_DEFAULT);
  617. }
  618. }
  619. if (handle->attribute.perf_detail_en) {
  620. ret = audio_out_control(handle->aout_dev,
  621. handle->aout_handle, AOUT_CMD_RESET_SAMPLE_CNT, NULL);
  622. if (!ret) {
  623. handle->flags |= AUDIO_PLAY_SAMPLES_CNT_EN_FLAG;
  624. LOG_INF("enable samples counter function");
  625. }
  626. ret = audio_out_control(handle->aout_dev,
  627. handle->aout_handle, AOUT_CMD_GET_SAMPLE_CNT, &count);
  628. if (!ret) {
  629. LOG_INF("orign samples counter:%d", count);
  630. }
  631. if ((handle->attribute.fifo_type == AOUT_FIFO_DAC0)
  632. && (handle->attribute.chl_type == AUDIO_CHANNEL_DAC)) {
  633. ret = audio_out_control(handle->aout_dev,
  634. handle->aout_handle, AOUT_CMD_RESET_DAC_SDM_SAMPLE_CNT, NULL);
  635. if (!ret) {
  636. handle->flags |= AUDIO_PLAY_SDM_CNT_EN_FLAG;
  637. LOG_INF("enable sdm counter function");
  638. }
  639. ret = audio_out_control(handle->aout_dev,
  640. handle->aout_handle, AOUT_CMD_GET_DAC_SDM_SAMPLE_CNT, &count);
  641. if (!ret) {
  642. LOG_INF("origin sdm samples counter:%d", count);
  643. }
  644. }
  645. }
  646. if (handle->attribute.dma_separated_en) {
  647. ret = audio_out_control(handle->aout_dev,
  648. handle->aout_handle, AOUT_CMD_SET_SEPARATED_MODE, NULL);
  649. if (ret) {
  650. LOG_INF("set DMA interleaved mode error:%d", ret);
  651. }
  652. }
  653. }
  654. /* @brief Configure the low level audio play device by specified handler with necessary attribute */
  655. static int audio_play_config(audio_play_handle handle)
  656. {
  657. aout_param_t aout_param = {0};
  658. dac_setting_t dac_setting = {0};
  659. i2stx_setting_t i2stx_setting = {0};
  660. spdiftx_setting_t spdiftx_setting = {0};
  661. audio_reload_t reload_setting = {0};
  662. /* open audio power gating*/
  663. sys_write32(sys_read32(PWRGATE_DIG) | (0x1 << 26), PWRGATE_DIG);
  664. if (!handle) {
  665. LOG_ERR("null handle");
  666. return -EINVAL;
  667. }
  668. aout_param.sample_rate = handle->attribute.sr;
  669. aout_param.channel_type = handle->attribute.chl_type;
  670. aout_param.outfifo_type = handle->attribute.fifo_type;
  671. aout_param.channel_width = handle->attribute.chl_width;
  672. LOG_INF("sr:%d channel type:0x%x fifo type:%d width:%d",
  673. aout_param.sample_rate, aout_param.channel_type,
  674. aout_param.outfifo_type, aout_param.channel_width);
  675. aout_param.callback = audio_play_write_data_cb;
  676. aout_param.cb_data = handle;
  677. /* DMA reload mode */
  678. if (handle->attribute.reload_en) {
  679. reload_setting.reload_addr = handle->audio_buffer;
  680. reload_setting.reload_len = handle->audio_buffer_size;
  681. aout_param.reload_setting = &reload_setting;
  682. }
  683. /* DAC interface setting */
  684. if (handle->attribute.chl_type & AUDIO_CHANNEL_DAC) {
  685. dac_setting.volume.left_volume = handle->attribute.left_vol;
  686. dac_setting.volume.right_volume = handle->attribute.right_vol;
  687. dac_setting.channel_mode = handle->attribute.mono_en ? MONO_MODE : STEREO_MODE;
  688. aout_param.dac_setting = &dac_setting;
  689. }
  690. /* I2S interface setting */
  691. if (handle->attribute.chl_type & AUDIO_CHANNEL_I2STX) {
  692. /* SRD function is only used in slave mode */
  693. i2stx_setting.srd_callback = audio_i2stx_srd_cb;
  694. i2stx_setting.cb_data = handle;
  695. aout_param.i2stx_setting = &i2stx_setting;
  696. if (AOUT_FIFO_DAC0 == aout_param.outfifo_type) {
  697. aout_param.dac_setting = &dac_setting;
  698. dac_setting.volume.left_volume = handle->attribute.left_vol;
  699. dac_setting.volume.right_volume = handle->attribute.right_vol;
  700. dac_setting.channel_mode = handle->attribute.mono_en ? MONO_MODE : STEREO_MODE;
  701. }
  702. }
  703. /* SPDIF interface setting */
  704. if (handle->attribute.chl_type & AUDIO_CHANNEL_SPDIFTX) {
  705. if (AOUT_FIFO_DAC0 == aout_param.outfifo_type) {
  706. aout_param.dac_setting = &dac_setting;
  707. dac_setting.volume.left_volume = handle->attribute.left_vol;
  708. dac_setting.volume.right_volume = handle->attribute.right_vol;
  709. }
  710. aout_param.spdiftx_setting = &spdiftx_setting;
  711. }
  712. handle->aout_handle = audio_out_open(handle->aout_dev, (void *)&aout_param);
  713. if (!handle->aout_handle)
  714. return -EFAULT;
  715. audio_play_ext_config(handle);
  716. return 0;
  717. }
  718. /* @brief create an audio play instance and return the play handler */
  719. static audio_play_handle audio_play_create(struct audio_play_attr *attr)
  720. {
  721. struct device *dev;
  722. audio_play_handle hdl = NULL;
  723. static struct audio_play_object play_object[AUDIO_PLAY_MAX_HANLDER];
  724. if (!attr) {
  725. LOG_ERR("Invalid attr");
  726. return NULL;
  727. }
  728. dev = (struct device *)device_get_binding(CONFIG_AUDIO_OUT_ACTS_DEV_NAME);
  729. if (!dev) {
  730. LOG_ERR("find to bind dev %s", CONFIG_AUDIO_OUT_ACTS_DEV_NAME);
  731. return NULL;
  732. }
  733. hdl = &play_object[attr->id];
  734. memset(hdl, 0, sizeof(struct audio_play_object));
  735. #ifdef SHELL_ADC_USE_DIFFERENT_BUFFER
  736. if (!attr->id)
  737. hdl->audio_buffer =(uint8_t *) shell_audio_buffer;
  738. else
  739. hdl->audio_buffer = (uint8_t *)shell_audio_buffer1;
  740. #else
  741. hdl->audio_buffer = (uint8_t *)shell_audio_buffer;
  742. #endif
  743. hdl->audio_buffer_size = SHELL_AUDIO_BUFFER_SIZE;
  744. if (!attr->zero_mute && !attr->adda_en) {
  745. audio_prepare_data(hdl, hdl->audio_buffer,
  746. SHELL_AUDIO_BUFFER_SIZE, attr->chl_width);
  747. } else {
  748. memset(hdl->audio_buffer, 0, SHELL_AUDIO_BUFFER_SIZE);
  749. }
  750. hdl->aout_dev = (struct device *)dev;
  751. memcpy(&hdl->attribute, attr, sizeof(struct audio_play_attr));
  752. return hdl;
  753. }
  754. /* @brief start to play stream */
  755. static int audio_play_start(audio_play_handle handle)
  756. {
  757. int ret;
  758. if (handle->attribute.reload_en) {
  759. ret = audio_out_start(handle->aout_dev, handle->aout_handle);
  760. } else {
  761. ret = audio_out_write(handle->aout_dev, handle->aout_handle,
  762. handle->audio_buffer, handle->audio_buffer_size);
  763. audio_play_total_size += handle->audio_buffer_size;
  764. }
  765. if (ret) {
  766. LOG_ERR("player[%d] start error:%d", handle->attribute.id, ret);
  767. } else {
  768. handle->flags |= AUDIO_PLAY_START_FLAG;
  769. LOG_INF("player[%d] start successfully", handle->attribute.id);
  770. }
  771. return ret;
  772. }
  773. /* @brief stop playing stream */
  774. static void audio_play_stop(audio_play_handle handle)
  775. {
  776. uint8_t i;
  777. bool all_stop_flag = true;
  778. if (handle && handle->aout_handle) {
  779. audio_out_stop(handle->aout_dev, handle->aout_handle);
  780. audio_out_close(handle->aout_dev, handle->aout_handle);
  781. audio_player[handle->attribute.id & (AUDIO_PLAY_MAX_HANLDER - 1)] = NULL;
  782. handle->flags = 0;
  783. }
  784. for (i = 0; i < AUDIO_PLAY_MAX_HANLDER; i++) {
  785. if (audio_player[i]) {
  786. all_stop_flag = false;
  787. break;
  788. }
  789. }
  790. if (all_stop_flag)
  791. audio_play_total_size = 0;
  792. }
  793. #endif
  794. #ifdef TIME_SRC_TRIGGER
  795. /*RMU MRCR1*/
  796. #define ASSHLL_RMU_MRCR1 0x40000004
  797. #define ASHLL_RMU_MRCR1_TIME0_RET BIT(10)
  798. #define ASHLL_RMU_MRCR1_TIME1_RET BIT(11)
  799. #define ASHLL_RMU_MRCR1_TIME2_RET BIT(12)
  800. #define ASHLL_RMU_MRCR1_TIME3_RET BIT(13)
  801. #define ASHLL_RMU_MRCR1_TIME4_RET BIT(14)
  802. #define ASHLL_RMU_MRCR1_TIME5_RET BIT(15)
  803. /*CMUD DEV1*/
  804. #define ASSHLL_CMUD_DEV1 0x40001008
  805. #define ASHLL_CUMD_TIME0_EN BIT(10)
  806. #define ASHLL_CUMD_TIME1_EN BIT(11)
  807. #define ASHLL_CUMD_TIME2_EN BIT(12)
  808. #define ASHLL_CUMD_TIME3_EN BIT(13)
  809. #define ASHLL_CUMD_TIME4_EN BIT(14)
  810. #define ASHLL_CUMD_TIME5_EN BIT(15)
  811. /*TIME BASE REG*/
  812. #define ASSHLL_TIME0_BASE_REG 0x4000C100
  813. #define ASSHLL_TIME1_BASE_REG 0x4000C120
  814. #define ASSHLL_TIME2_BASE_REG 0x4000C140
  815. #define ASSHLL_TIME3_BASE_REG 0x4000C160
  816. #define ASSHLL_TIME4_BASE_REG 0x4000C180
  817. #define ASSHLL_TIME5_BASE_REG 0x4000C1A0
  818. /*CMUD TIME SRC SEL*/
  819. #define ASSHLL_TIME_SEL_SRC_SHIFT (0)
  820. #define ASSHLL_TIME_SEL_SRC_MASK ((0x3)<<ASSHLL_TIME_SEL_SRC_SHIFT)
  821. #define ASSHLL_TIME_SEL_SRC(x) ((x)<<ASSHLL_TIME_SEL_SRC_SHIFT)
  822. #define BIT(n) (1UL << (n))
  823. /*T0_CTL*/
  824. #define TX_CTL_BT_TWS_EN BIT(17)
  825. #define TX_CTL_BT_TWS_SEL BIT(16)
  826. #define TX_CTL_DIR BIT(11)
  827. #define TX_CTL_MODE_SEL_SHIFT (9)
  828. #define TX_CTL_MODE_SEL_MASK ((0x7)<<TX_CTL_MODE_SEL_SHIFT)
  829. #define TX_CTL_MODE_SEL(x) ((x)<<TX_CTL_MODE_SEL_SHIFT)
  830. #define TX_CTL_EN BIT(5)
  831. #define TX_CTL_PAUSH BIT(3)
  832. #define TX_CTL_RELO BIT(2)
  833. #define TX_CTL_ZIEN BIT(1)
  834. #define TX_CTL_ZIPD BIT(0)
  835. /*T0_VAL*/
  836. #define TX_VAL_VAL_SHIFT (0)
  837. #define TX_VAL_VAL_MASK ((0xFFFFFFFF)<<TX_VAL_VAL_SHIFT)
  838. #define TX_VAL_VAL(x) ((x)<<TX_VAL_VAL_SHIFT)
  839. /*T0_CNT*/
  840. #define TX_CNT_CNT_SHIFT (0)
  841. #define TX_CNT_CNT_MASK (0xFFFFFFFF<<TX_CNT_CNT_SHIFT)
  842. #define TX_CNT_CNT(x) (x<<TX_CNT_CNT_SHIFT)
  843. /*T0_CNT_TMP*/
  844. #define TX_CNT_TMP_CNT_SHIFT (0)
  845. #define TX_CNT_TMP_CNT_MASK (0xFFFFFFFF<<TX_CNT_TMP_CNT_SHIFT)
  846. #define TX_CNT_TMP_CNT(x) (x<<TX_CNT_TMP_CNT_SHIFT)
  847. struct acts_time{
  848. volatile uint32_t tx_ctl;
  849. volatile uint32_t tx_val;
  850. volatile uint32_t tx_cnt;
  851. volatile uint32_t tx_ctl_tmp;
  852. };
  853. // static void ashell_timer_isr0(const void *arg)
  854. // {
  855. // struct acts_time *acts_time_reg = (struct acts_time *)ASSHLL_TIME0_BASE_REG;
  856. // if(acts_time_reg->tx_ctl & TX_CTL_ZIPD)
  857. // {
  858. // acts_time_reg->tx_ctl &= TX_CTL_ZIPD;
  859. // printk("%s:%d\n",__func__,__LINE__);
  860. // }
  861. // return;
  862. // }
  863. // static void ashell_timer_isr1(const void *arg)
  864. // {
  865. // struct acts_time *acts_time_reg = (struct acts_time *)ASSHLL_TIME1_BASE_REG;
  866. // if(acts_time_reg->tx_ctl & TX_CTL_ZIPD)
  867. // {
  868. // acts_time_reg->tx_ctl &= TX_CTL_ZIPD;
  869. // printk("%s:%d\n",__func__,__LINE__);
  870. // }
  871. // return;
  872. // }
  873. static void ashell_timer_isr2(const void *arg)
  874. {
  875. struct acts_time *acts_time_reg = (struct acts_time *)ASSHLL_TIME2_BASE_REG;
  876. if(acts_time_reg->tx_ctl & TX_CTL_ZIPD)
  877. {
  878. acts_time_reg->tx_ctl &= TX_CTL_ZIPD;
  879. printk("%s:%d\n",__func__,__LINE__);
  880. }
  881. return;
  882. }
  883. static void ashell_timer_isr3(const void *arg)
  884. {
  885. struct acts_time *acts_time_reg = (struct acts_time *)ASSHLL_TIME3_BASE_REG;
  886. if(acts_time_reg->tx_ctl & TX_CTL_ZIPD)
  887. {
  888. acts_time_reg->tx_ctl &= TX_CTL_ZIPD;
  889. printk("%s:%d\n",__func__,__LINE__);
  890. }
  891. return;
  892. }
  893. static void ashell_timer_isr4(const void *arg)
  894. {
  895. struct acts_time *acts_time_reg = (struct acts_time *)ASSHLL_TIME4_BASE_REG;
  896. if(acts_time_reg->tx_ctl & TX_CTL_ZIPD)
  897. {
  898. acts_time_reg->tx_ctl &= TX_CTL_ZIPD;
  899. printk("%s:%d\n",__func__,__LINE__);
  900. }
  901. return;
  902. }
  903. static void ashell_timer_isr5(const void *arg)
  904. {
  905. struct acts_time *acts_time_reg = (struct acts_time *)ASSHLL_TIME5_BASE_REG;
  906. if(acts_time_reg->tx_ctl & TX_CTL_ZIPD)
  907. {
  908. acts_time_reg->tx_ctl &= TX_CTL_ZIPD;
  909. printk("%s:%d\n",__func__,__LINE__);
  910. }
  911. return;
  912. }
  913. /* @brief command to time src test */
  914. static int cmd_time_src(const struct shell *shell,
  915. size_t argc, char **argv)
  916. {
  917. int time_t;
  918. int time_handle;
  919. time_handle = atoi(argv[1]);
  920. time_t = atoi(argv[2]);
  921. printk("argc:%d,time_handle:%d,time_s:%d\n",argc,time_handle,time_t);
  922. struct acts_time *acts_time_reg = NULL;
  923. acts_reset_peripheral(RESET_ID_TIMER2);
  924. acts_reset_peripheral(RESET_ID_TIMER3);
  925. acts_reset_peripheral(RESET_ID_TIMER4);
  926. acts_reset_peripheral(RESET_ID_TIMER5);
  927. switch (time_handle)
  928. {
  929. // case 0:
  930. // acts_time_reg = (struct acts_time *)ASSHLL_TIME0_BASE_REG;
  931. // sys_write32(sys_read32(ASSHLL_CMUD_DEV1) | ASHLL_CUMD_TIME0_EN,ASSHLL_CMUD_DEV1);
  932. // IRQ_CONNECT(IRQ_ID_TIMER0, 1, ashell_timer_isr0,0, 0);
  933. // sys_write32(sys_read32(ASSHLL_TIME0_BASE_REG) | TX_CTL_EN | TX_CTL_ZIEN,
  934. // ASSHLL_TIME0_BASE_REG);
  935. // sys_write32(sys_read32(ASSHLL_TIME0_BASE_REG+0x04) | TX_VAL_VAL(time_t*1000),
  936. // ASSHLL_TIME0_BASE_REG+0x04);
  937. // irq_enable(IRQ_ID_TIMER0);
  938. // break;
  939. // case 1:
  940. // acts_time_reg = (struct acts_time *)ASSHLL_TIME1_BASE_REG;
  941. // sys_write32(sys_read32(ASSHLL_CMUD_DEV1) | ASHLL_CUMD_TIME1_EN,ASSHLL_CMUD_DEV1);
  942. // IRQ_CONNECT(IRQ_ID_TIMER1, 1, ashell_timer_isr1,0, 0);
  943. // sys_write32(sys_read32(ASSHLL_TIME1_BASE_REG) | TX_CTL_EN | TX_CTL_ZIEN,
  944. // ASSHLL_TIME1_BASE_REG);
  945. // sys_write32(sys_read32(ASSHLL_TIME1_BASE_REG+0x04) | TX_VAL_VAL(time_t*1000),
  946. // ASSHLL_TIME1_BASE_REG+0x04);
  947. // irq_enable(IRQ_ID_TIMER1);
  948. // break;
  949. case 2:
  950. acts_time_reg = (struct acts_time *)ASSHLL_TIME2_BASE_REG;
  951. sys_write32(sys_read32(ASSHLL_CMUD_DEV1) | ASHLL_CUMD_TIME2_EN,ASSHLL_CMUD_DEV1);
  952. IRQ_CONNECT(IRQ_ID_TIMER2, 1, ashell_timer_isr2,0, 0);
  953. sys_write32(sys_read32(ASSHLL_TIME2_BASE_REG) | TX_CTL_EN | TX_CTL_ZIEN,
  954. ASSHLL_TIME2_BASE_REG);
  955. sys_write32(sys_read32(ASSHLL_TIME2_BASE_REG+0x04) | TX_VAL_VAL(time_t*1000),
  956. ASSHLL_TIME2_BASE_REG+0x04);
  957. irq_enable(IRQ_ID_TIMER2);
  958. break;
  959. case 3:
  960. acts_time_reg = (struct acts_time *)ASSHLL_TIME3_BASE_REG;
  961. sys_write32(sys_read32(ASSHLL_CMUD_DEV1) | ASHLL_CUMD_TIME3_EN,ASSHLL_CMUD_DEV1);
  962. sys_write32(sys_read32(ASSHLL_TIME3_BASE_REG) | TX_CTL_EN | TX_CTL_ZIEN,
  963. ASSHLL_TIME3_BASE_REG);
  964. sys_write32(sys_read32(ASSHLL_TIME3_BASE_REG+0x04) | TX_VAL_VAL(time_t*1000),
  965. ASSHLL_TIME3_BASE_REG+0x04);
  966. IRQ_CONNECT(IRQ_ID_TIMER3, 1, ashell_timer_isr3,0, 0);
  967. irq_enable(IRQ_ID_TIMER3);
  968. break;
  969. case 4:
  970. acts_time_reg = (struct acts_time *)ASSHLL_TIME4_BASE_REG;
  971. sys_write32(sys_read32(ASSHLL_CMUD_DEV1) | ASHLL_CUMD_TIME4_EN,ASSHLL_CMUD_DEV1);
  972. sys_write32(sys_read32(ASSHLL_TIME4_BASE_REG) | TX_CTL_EN | TX_CTL_ZIEN,
  973. ASSHLL_TIME4_BASE_REG);
  974. sys_write32(sys_read32(ASSHLL_TIME4_BASE_REG+0x04) | TX_VAL_VAL(time_t*1000),
  975. ASSHLL_TIME4_BASE_REG+0x04);
  976. IRQ_CONNECT(IRQ_ID_TIMER4, 1, ashell_timer_isr4,0, 0);
  977. irq_enable(IRQ_ID_TIMER4);
  978. break;
  979. case 5:
  980. acts_time_reg = (struct acts_time *)ASSHLL_TIME5_BASE_REG;
  981. sys_write32(sys_read32(ASSHLL_CMUD_DEV1) | ASHLL_CUMD_TIME5_EN,ASSHLL_CMUD_DEV1);
  982. IRQ_CONNECT(IRQ_ID_TIMER5, 1, ashell_timer_isr5,0, 0);
  983. irq_enable(IRQ_ID_TIMER5);
  984. break;
  985. default:
  986. break;
  987. }
  988. // /*enable timer and irq*/
  989. // acts_time_reg->tx_ctl |= TX_CTL_EN | TX_CTL_ZIEN;
  990. // /*set time second*/
  991. // acts_time_reg->tx_val |= TX_VAL_VAL(time_t*1000);
  992. printk("%08x,t%d_ctl:%08x\n",(uint32_t)acts_time_reg,time_handle,acts_time_reg->tx_ctl);
  993. printk("t%d_val:%08x\n",time_handle,acts_time_reg->tx_val);
  994. printk("t%d_cnt:%08x\n",time_handle,acts_time_reg->tx_cnt);
  995. printk("t%d_ctl_tmp:%08x\n",time_handle,acts_time_reg->tx_ctl_tmp);
  996. // k_sleep(K_MSEC(10000));
  997. return 0;
  998. }
  999. #endif
  1000. /* @brief command to dump the audio controller registers */
  1001. static int cmd_dump_register(const struct shell *shell,
  1002. size_t argc, char **argv)
  1003. {
  1004. struct device *dev = NULL;
  1005. if (argc != 2) {
  1006. shell_error(shell, "Invalid parameter");
  1007. shell_fprintf(shell, SHELL_NORMAL,
  1008. "Usage: dump_reg [dac|i2stx|spdiftx|adc|i2srx|spdifrx|anc]\n");
  1009. return -1;
  1010. }
  1011. if (!strcmp(argv[1], "dac")) {
  1012. #ifdef CONFIG_AUDIO_OUT_DAC_SUPPORT
  1013. dev = (struct device *)device_get_binding(CONFIG_AUDIO_DAC_0_NAME);
  1014. #endif
  1015. } else if (!strcmp(argv[1], "i2stx")) {
  1016. #ifdef CONFIG_AUDIO_OUT_I2STX_SUPPORT
  1017. dev = (struct device *)device_get_binding(CONFIG_AUDIO_I2STX_0_NAME);
  1018. #endif
  1019. } else if (!strcmp(argv[1], "spdiftx")) {
  1020. #ifdef CONFIG_AUDIO_OUT_SPDIFTX_SUPPORT
  1021. dev = (struct device *)device_get_binding(CONFIG_AUDIO_SPDIFTX_0_NAME);
  1022. #endif
  1023. } else if (!strcmp(argv[1], "adc")) {
  1024. #ifdef CONFIG_AUDIO_IN_ADC_SUPPORT
  1025. dev = (struct device *)device_get_binding(CONFIG_AUDIO_ADC_0_NAME);
  1026. #endif
  1027. } else if (!strcmp(argv[1], "i2srx")) {
  1028. #ifdef CONFIG_AUDIO_IN_I2SRX_SUPPORT
  1029. dev = (struct device *)device_get_binding(CONFIG_AUDIO_I2SRX_0_NAME);
  1030. #endif
  1031. } else if (!strcmp(argv[1], "spdifrx")) {
  1032. #ifdef CONFIG_AUDIO_IN_SPDIFRX_SUPPORT
  1033. dev = (struct device *)device_get_binding(CONFIG_AUDIO_SPDIFRX_0_NAME);
  1034. #endif
  1035. } else if (!strcmp(argv[1], "anc")) {
  1036. #ifdef CONFIG_AUDIO_IN_ANC_SUPPORT
  1037. dev = (struct device *)device_get_binding(PHY_ANC_DEV_NAME);
  1038. #endif
  1039. } else if (!strcmp(argv[1], "pdmtx")) {
  1040. #ifdef CONFIG_AUDIO_OUT_PDMTX_SUPPORT
  1041. dev = (struct device *)device_get_binding(CONFIG_AUDIO_PDMTX_0_NAME);
  1042. #endif
  1043. } else
  1044. shell_error(shell, "invalid %s", argv[1]);
  1045. if (dev)
  1046. phy_audio_control(dev, PHY_CMD_DUMP_REGS, NULL);
  1047. return 0;
  1048. }
  1049. #ifdef CONFIG_AUDIO_DRIVER_STREAM_SHELL
  1050. /* @brief command to start playing */
  1051. static int cmd_play_start(const struct shell *shell,
  1052. size_t argc, char **argv)
  1053. {
  1054. struct audio_play_attr play_attr = {0};
  1055. uint32_t val;
  1056. audio_play_handle handle;
  1057. int ret;
  1058. if (!cmd_search_value_by_key(argc, argv, SHELL_CMD_CHL_TYPE_KEY, &val))
  1059. play_attr.chl_type = val;
  1060. else
  1061. play_attr.chl_type = AUDIO_CHANNEL_DAC;
  1062. if (!cmd_search_value_by_key(argc, argv, SHELL_CMD_CHL_WIDTH_KEY, &val))
  1063. play_attr.chl_width = val;
  1064. else
  1065. play_attr.chl_width = CHANNEL_WIDTH_16BITS;
  1066. if (!cmd_search_value_by_key(argc, argv, SHELL_CMD_SAMPLE_RATE_KEY, &val))
  1067. play_attr.sr = val;
  1068. else
  1069. play_attr.sr = SAMPLE_RATE_48KHZ;
  1070. if (!cmd_search_value_by_key(argc, argv, SHELL_CMD_FIFO_TYPE_KEY, &val))
  1071. play_attr.fifo_type = val;
  1072. else
  1073. play_attr.fifo_type = AOUT_FIFO_DAC0;
  1074. if (!cmd_search_value_by_key(argc, argv, SHELL_CMD_LEFT_VOL_KEY, &val))
  1075. play_attr.left_vol = (int32_t)val;
  1076. else
  1077. play_attr.left_vol = 0; /* 0dB */
  1078. if (!cmd_search_value_by_key(argc, argv, SHELL_CMD_RIGHT_VOL_KEY, &val))
  1079. play_attr.right_vol = (int32_t)val;
  1080. else
  1081. play_attr.right_vol = 0; /* 0dB */
  1082. if (!cmd_search_value_by_key(argc, argv, SHELL_CMD_RELOAD_KEY, &val))
  1083. play_attr.reload_en = val;
  1084. else
  1085. play_attr.reload_en = 0;
  1086. if (!cmd_search_value_by_key(argc, argv, SHELL_CMD_CHL_INDEX_KEY, &val))
  1087. play_attr.id = val;
  1088. else
  1089. play_attr.id = 0;
  1090. if (!cmd_search_value_by_key(argc, argv, SHELL_CMD_ADDA_KEY, &val))
  1091. play_attr.adda_en = val;
  1092. else
  1093. play_attr.adda_en = 0;
  1094. if (!cmd_search_value_by_key(argc, argv, SHELL_CMD_ZERO_KEY, &val))
  1095. play_attr.zero_mute = val;
  1096. else
  1097. play_attr.zero_mute = 0;
  1098. printk("play_attr.zero_mute %d \n",play_attr.zero_mute);
  1099. if (!cmd_search_value_by_key(argc, argv, SHELL_CMD_PLAY_MONO_KEY, &val))
  1100. play_attr.mono_en = val;
  1101. else
  1102. play_attr.mono_en = 0;
  1103. if (!cmd_search_value_by_key(argc, argv, SHELL_CMD_PLAY_PERF_DETAIL_KEY, &val))
  1104. play_attr.perf_detail_en = val;
  1105. else
  1106. play_attr.perf_detail_en = 0;
  1107. if (play_attr.id >= AUDIO_PLAY_MAX_HANLDER) {
  1108. LOG_ERR("invalid channel id %d", play_attr.id);
  1109. return -EINVAL;
  1110. }
  1111. if (!cmd_search_value_by_key(argc, argv, SHELL_CMD_HW_TRIGGER_SRC_KEY, &val))
  1112. play_attr.hw_trigger_src = val + 1;
  1113. else
  1114. play_attr.hw_trigger_src = 0;
  1115. if (!cmd_search_value_by_key(argc, argv, SHELL_CMD_DMA_INTERLEAVED_EN, &val))
  1116. play_attr.dma_separated_en = val;
  1117. else
  1118. play_attr.dma_separated_en = 0;
  1119. if (audio_player[play_attr.id])
  1120. audio_play_stop(audio_player[play_attr.id]);
  1121. handle = audio_play_create(&play_attr);
  1122. if (!handle)
  1123. return -EFAULT;
  1124. audio_player[play_attr.id] = handle;
  1125. ret = audio_play_config(handle);
  1126. if (ret)
  1127. return ret;
  1128. ret = audio_play_start(handle);
  1129. if (ret)
  1130. return ret;
  1131. return ret;
  1132. }
  1133. /* @brief command to stop playing */
  1134. static int cmd_play_stop(const struct shell *shell,
  1135. size_t argc, char **argv)
  1136. {
  1137. uint32_t val;
  1138. uint8_t id;
  1139. if (!cmd_search_value_by_key(argc, argv, SHELL_CMD_CHL_INDEX_KEY, &val))
  1140. id = val;
  1141. else
  1142. id = 0;
  1143. if (id >= AUDIO_PLAY_MAX_HANLDER) {
  1144. LOG_ERR("invalid channel id %d", id);
  1145. return -EINVAL;
  1146. }
  1147. if (audio_player[id])
  1148. audio_play_stop(audio_player[id]);
  1149. return 0;
  1150. }
  1151. /* @brief command to control playing dynamically */
  1152. static int cmd_play_ioctl(const struct shell *shell,
  1153. size_t argc, char **argv)
  1154. {
  1155. uint32_t val, param = 0;
  1156. uint16_t cmd, id = 0;
  1157. int ret = 0;
  1158. bool is_in = false;
  1159. uint8_t fifo_type;
  1160. uint8_t fifo_from_dsp;
  1161. if (!cmd_search_value_by_key(argc, argv, SHELL_CMD_IOCTL_CMD_KEY, &val)) {
  1162. cmd = val;
  1163. } else {
  1164. LOG_ERR("invalid cmd");
  1165. return -EINVAL;
  1166. }
  1167. if (!cmd_search_value_by_key(argc, argv, SHELL_CMD_IOCTL_PARAM_KEY, &val)) {
  1168. param = val;
  1169. is_in = true;
  1170. }
  1171. if (!cmd_search_value_by_key(argc, argv, SHELL_CMD_CHL_INDEX_KEY, &val))
  1172. id = (val < AUDIO_PLAY_MAX_HANLDER) ? val : 0;
  1173. if (!cmd_search_value_by_key(argc, argv, SHELL_CMD_FIFO_TYPE_KEY, &val))
  1174. fifo_type = val;
  1175. else
  1176. fifo_type = AOUT_FIFO_DAC0;
  1177. if (!cmd_search_value_by_key(argc, argv, SHELL_CMD_FIFO_SRC_KEY, &val))
  1178. fifo_from_dsp = val;
  1179. else
  1180. fifo_from_dsp = 0;
  1181. if (!audio_player[id]) {
  1182. LOG_WRN("audio player[%d] does not start yet", id);
  1183. struct audio_play_attr attr = {0};
  1184. audio_player[id] = audio_play_create(&attr);
  1185. }
  1186. LOG_INF("play command:%d param:%d", cmd, param);
  1187. if (cmd == AOUT_CMD_GET_VOLUME) {
  1188. volume_setting_t volume = {0};
  1189. ret = audio_out_control(audio_player[id]->aout_dev,
  1190. audio_player[id]->aout_handle, cmd, (void *)&volume);
  1191. if (!ret) {
  1192. LOG_INF("Left volume: %d", volume.left_volume);
  1193. LOG_INF("Right_volume: %d", volume.right_volume);
  1194. } else {
  1195. LOG_ERR("Get volume error:%d", ret);
  1196. }
  1197. } else if (cmd == AOUT_CMD_SET_VOLUME) {
  1198. volume_setting_t volume = {0};
  1199. if (!cmd_search_value_by_key(argc, argv, SHELL_CMD_LEFT_VOL_KEY, &val))
  1200. volume.left_volume = val;
  1201. if (!cmd_search_value_by_key(argc, argv, SHELL_CMD_RIGHT_VOL_KEY, &val))
  1202. volume.right_volume = val;
  1203. ret = audio_out_control(audio_player[id]->aout_dev,
  1204. audio_player[id]->aout_handle, cmd, (void *)&volume);
  1205. if (ret) {
  1206. LOG_ERR("Set volume error:%d", ret);
  1207. }
  1208. } else if (cmd == AOUT_CMD_SPDIF_SET_CHANNEL_STATUS) {
  1209. audio_spdif_ch_status_t ch_status = {0};
  1210. if (!cmd_search_value_by_key(argc, argv, SHELL_CMD_IOCTL_CMD_SPDIF_CSL_KEY, &val))
  1211. ch_status.csl = val;
  1212. if (!cmd_search_value_by_key(argc, argv, SHELL_CMD_IOCTL_CMD_SPDIF_CSH_KEY, &val))
  1213. ch_status.csh = val;
  1214. ret = audio_out_control(audio_player[id]->aout_dev,
  1215. audio_player[id]->aout_handle, cmd, (void *)&ch_status);
  1216. if (ret) {
  1217. LOG_ERR("Set SPDIF status error:%d", ret);
  1218. }
  1219. } else if (cmd == AOUT_CMD_SPDIF_GET_CHANNEL_STATUS) {
  1220. audio_spdif_ch_status_t ch_status = {0};
  1221. ret = audio_out_control(audio_player[id]->aout_dev,
  1222. audio_player[id]->aout_handle, cmd, (void *)&ch_status);
  1223. if (ret) {
  1224. LOG_ERR("Get SPDIF status error:%d", ret);
  1225. } else {
  1226. LOG_INF("SPDIF CSL: 0x%x", ch_status.csl);
  1227. LOG_INF("SPDIF CSH: 0x%x", ch_status.csh);
  1228. }
  1229. } else if (cmd == AOUT_CMD_SET_FIFO_SRC) {
  1230. dac_fifosrc_setting_t dac_fifosrc_setting = {0};
  1231. if(AOUT_FIFO_DAC0 == fifo_type)
  1232. dac_fifosrc_setting.fifo_idx = AOUT_FIFO_DAC0;
  1233. else
  1234. dac_fifosrc_setting.fifo_idx = AOUT_FIFO_DAC1;
  1235. dac_fifosrc_setting.fifo_from_dsp = fifo_from_dsp;
  1236. ret = audio_out_control(audio_player[id]->aout_dev,
  1237. audio_player[id]->aout_handle, cmd, (void *)&dac_fifosrc_setting);
  1238. if (!ret) {
  1239. LOG_ERR("setting fifi_from mcu fail\n");
  1240. }
  1241. } else {
  1242. ret = audio_out_control(audio_player[id]->aout_dev,
  1243. audio_player[id]->aout_handle, cmd, (void *)&param);
  1244. if (ret) {
  1245. LOG_ERR("execute cmd:%d error:%d", cmd, ret);
  1246. } else {
  1247. if (!is_in) {
  1248. LOG_INF("cmd:%d result:%d", cmd, param);
  1249. }
  1250. }
  1251. }
  1252. return ret;
  1253. }
  1254. /* @brief audio i2srx SRD(sample rate detection) callback */
  1255. static int audio_i2srx_srd_cb(void *cb_data, uint32_t cmd, void *param)
  1256. {
  1257. switch (cmd) {
  1258. case I2SRX_SRD_FS_CHANGE:
  1259. LOG_INF("New sample rate %d", *(audio_sr_sel_e *)param);
  1260. break;
  1261. case I2SRX_SRD_WL_CHANGE:
  1262. LOG_INF("New width length %d", *(audio_i2s_srd_wl_e *)param);
  1263. break;
  1264. case I2SRX_SRD_TIMEOUT:
  1265. LOG_INF("SRD timeout");
  1266. break;
  1267. default:
  1268. LOG_ERR("Error command %d", cmd);
  1269. return -EFAULT;
  1270. }
  1271. return 0;
  1272. }
  1273. /* @brief audio spdifrx SRD(sample rate detection) callback */
  1274. static int audio_spdifrx_srd_cb(void *cb_data, uint32_t cmd, void *param)
  1275. {
  1276. switch (cmd) {
  1277. case SPDIFRX_SRD_FS_CHANGE:
  1278. LOG_INF("New sample rate %d", *(audio_sr_sel_e *)param);
  1279. break;
  1280. case SPDIFRX_SRD_TIMEOUT:
  1281. LOG_INF("SRD timeout");
  1282. break;
  1283. default:
  1284. LOG_ERR("Error command %d", cmd);
  1285. return -EFAULT;
  1286. }
  1287. return 0;
  1288. }
  1289. /* @brief read data callback from the low level audio record device */
  1290. static int audio_rec_read_data_cb(void *callback_data, uint32_t reason)
  1291. {
  1292. static uint32_t rec_timestamp = 0;
  1293. static uint32_t rec_total_size = 0; /* per-second total size */
  1294. audio_record_handle handle = (audio_record_handle)callback_data;
  1295. uint32_t delta, len = handle->audio_buffer_size / 2;
  1296. uint8_t *buf = NULL;
  1297. if (AIN_DMA_IRQ_HF == reason) {
  1298. buf = handle->audio_buffer;
  1299. } else if (AIN_DMA_IRQ_TC == reason) {
  1300. buf = handle->audio_buffer + len;
  1301. } else {
  1302. LOG_ERR("invalid reason:%d", reason);
  1303. return 0;
  1304. }
  1305. delta = (uint32_t)k_cyc_to_ns_floor64(k_cycle_get_32() - rec_timestamp);
  1306. rec_total_size += len;
  1307. if (handle->attribute.adda_en)
  1308. ring_buf_put(&shell_audio_ringbuf, buf, len);
  1309. if (handle->attribute.rec_file)
  1310. {
  1311. cap_device.buf = buf;
  1312. k_work_submit(&cap_device.work);
  1313. }
  1314. if (delta > 1000000000UL) {
  1315. if (handle->attribute.dump_len)
  1316. AUDIO_DUMP_MEM(buf, handle->attribute.dump_len);
  1317. rec_timestamp = k_cycle_get_32();
  1318. printk("** record[%d] performance %dB/s ** \n",
  1319. handle->attribute.id, rec_total_size);
  1320. rec_total_size = 0;
  1321. }
  1322. return 0;
  1323. }
  1324. /* @brief Configure the extral infomation to the low level audio record device */
  1325. static void audio_record_ext_config(audio_record_handle handle)
  1326. {
  1327. int ret = 0;
  1328. if (!handle) {
  1329. LOG_ERR("null handle");
  1330. return;
  1331. }
  1332. if (handle->attribute.hw_trigger_src) {
  1333. --handle->attribute.hw_trigger_src;
  1334. ret = audio_in_control(handle->ain_dev,
  1335. handle->ain_handle, AIN_CMD_SET_ADC_TRIGGER_SRC,
  1336. &handle->attribute.hw_trigger_src);
  1337. if (!ret) {
  1338. LOG_INF("set ADC external IRQ trigger source %d ok",
  1339. handle->attribute.hw_trigger_src);
  1340. }
  1341. }
  1342. if (handle->attribute.dma_separated_en) {
  1343. uint8_t mode = LEFT_MUTE_RIGHT_MONO_MODE;
  1344. ret = audio_in_control(handle->ain_dev,
  1345. handle->ain_handle, AIN_CMD_SET_SEPARATED_MODE, &mode);
  1346. if (ret) {
  1347. LOG_INF("set DMA interleaved mode error:%d", ret);
  1348. }
  1349. }
  1350. }
  1351. /* @brief Configure the low level audio device to record */
  1352. static int audio_record_config(audio_record_handle handle)
  1353. {
  1354. ain_param_t ain_param = {0};
  1355. adc_setting_t adc_setting = {0};
  1356. i2srx_setting_t i2srx_setting = {0};
  1357. spdifrx_setting_t spdifrx_setting = {0};
  1358. /* initialize name info for a device */
  1359. if (handle->attribute.rec_file)
  1360. {
  1361. strcpy(cap_device.name, "CAP_dev");
  1362. cap_device.buf = handle->audio_buffer;
  1363. cap_device.buf_size = handle->audio_buffer_size /2;
  1364. k_work_init(&cap_device.work,rec_to_file);
  1365. rec_fs_init();
  1366. }
  1367. /* initialize work item for printing device's error messages */
  1368. /* open audio power gating*/
  1369. sys_write32(sys_read32(PWRGATE_DIG) | (0x1 << 26), PWRGATE_DIG);
  1370. if (!handle)
  1371. return -EINVAL;
  1372. memset(&ain_param, 0, sizeof(ain_param_t));
  1373. ain_param.sample_rate = handle->attribute.sr;
  1374. ain_param.callback = audio_rec_read_data_cb;
  1375. ain_param.cb_data = (void *)handle;
  1376. ain_param.reload_setting.reload_addr = handle->audio_buffer;
  1377. ain_param.reload_setting.reload_len = handle->audio_buffer_size;
  1378. ain_param.channel_type = handle->attribute.chl_type;
  1379. ain_param.channel_width = handle->attribute.chl_width;
  1380. switch (handle->attribute.chl_type) {
  1381. case AUDIO_CHANNEL_ADC:
  1382. adc_setting.device = handle->attribute.input_dev;
  1383. uint8_t i;
  1384. for (i = 0; i < ADC_CH_NUM_MAX; i++) {
  1385. adc_setting.gain.ch_gain[i] = handle->attribute.ch_gain[i];
  1386. }
  1387. ain_param.adc_setting = &adc_setting;
  1388. break;
  1389. case AUDIO_CHANNEL_I2SRX:
  1390. /* SRD is only used in i2s slave mode */
  1391. i2srx_setting.srd_callback = audio_i2srx_srd_cb;
  1392. i2srx_setting.cb_data = handle;
  1393. ain_param.i2srx_setting = &i2srx_setting;
  1394. break;
  1395. case AUDIO_CHANNEL_SPDIFRX:
  1396. spdifrx_setting.srd_callback = audio_spdifrx_srd_cb;
  1397. spdifrx_setting.cb_data = handle;
  1398. ain_param.spdifrx_setting = &spdifrx_setting;
  1399. break;
  1400. default:
  1401. LOG_ERR("Unsupport channel type: %d", handle->attribute.chl_type);
  1402. return -ENOTSUP;
  1403. }
  1404. handle->ain_handle = audio_in_open(handle->ain_dev, &ain_param);
  1405. if (!handle->ain_handle)
  1406. return -EFAULT;
  1407. audio_record_ext_config(handle);
  1408. return 0;
  1409. }
  1410. /* @brief create an audio record instance */
  1411. static audio_record_handle audio_record_create(struct audio_record_attr *attr)
  1412. {
  1413. struct device *dev;
  1414. audio_record_handle hdl = NULL;
  1415. static struct audio_record_object record_object[AUDIO_RECORD_MAX_HANLDER];
  1416. dev = (struct device *)device_get_binding(CONFIG_AUDIO_IN_ACTS_DEV_NAME);
  1417. if (!dev) {
  1418. LOG_ERR("Failed to get the audio in device %s", CONFIG_AUDIO_IN_ACTS_DEV_NAME);
  1419. return NULL;
  1420. }
  1421. hdl = &record_object[attr->id];
  1422. memset(hdl, 0, sizeof(struct audio_record_object));
  1423. hdl->audio_buffer_size = SHELL_AUDIO_BUFFER_SIZE;
  1424. #ifdef SHELL_ADC_USE_DIFFERENT_BUFFER
  1425. if (attr->id)
  1426. hdl->audio_buffer = (uint8_t *)shell_audio_buffer;
  1427. else
  1428. hdl->audio_buffer = (uint8_t *)shell_audio_buffer1;
  1429. #else
  1430. hdl->audio_buffer = (uint8_t *)shell_audio_buffer;
  1431. #endif
  1432. if (attr->adda_en) {
  1433. LOG_INF("enable ADDA ring buffer");
  1434. ring_buf_init(&shell_audio_ringbuf,
  1435. SHELL_AUDIO_ADDA_BUFFER_SIZE, shell_audio_adda_buffer);
  1436. }
  1437. memcpy(&hdl->attribute, attr, sizeof(struct audio_record_attr));
  1438. hdl->ain_dev = dev;
  1439. return hdl;
  1440. }
  1441. /* @brief start record */
  1442. static int audio_record_start(audio_record_handle handle)
  1443. {
  1444. return audio_in_start(handle->ain_dev, handle->ain_handle);
  1445. }
  1446. /* @brief stop record */
  1447. static void audio_record_stop(audio_record_handle handle)
  1448. {
  1449. if (handle && handle->ain_handle) {
  1450. audio_in_stop(handle->ain_dev, handle->ain_handle);
  1451. audio_in_close(handle->ain_dev, handle->ain_handle);
  1452. audio_recorder[handle->attribute.id & (AUDIO_RECORD_MAX_HANLDER - 1)] = NULL;
  1453. }
  1454. fs_close(&record_file);
  1455. }
  1456. /* @brief command to creat a recorder channel instance */
  1457. static int cmd_record_create(const struct shell *shell,
  1458. size_t argc, char **argv)
  1459. {
  1460. struct audio_record_attr record_attr = {0};
  1461. uint32_t val;
  1462. audio_record_handle handle;
  1463. int ret;
  1464. if (!cmd_search_value_by_key(argc, argv, SHELL_CMD_CHL_TYPE_KEY, &val))
  1465. record_attr.chl_type = val;
  1466. else
  1467. record_attr.chl_type = AUDIO_CHANNEL_ADC;
  1468. if (!cmd_search_value_by_key(argc, argv, SHELL_CMD_CHL_WIDTH_KEY, &val))
  1469. record_attr.chl_width = val;
  1470. else
  1471. record_attr.chl_width = CHANNEL_WIDTH_16BITS;
  1472. if (!cmd_search_value_by_key(argc, argv, SHELL_CMD_SAMPLE_RATE_KEY, &val))
  1473. record_attr.sr = val;
  1474. else
  1475. record_attr.sr = SAMPLE_RATE_48KHZ;
  1476. if (!cmd_search_value_by_key(argc, argv, SHELL_CMD_INPUT_DEV_KEY, &val))
  1477. record_attr.input_dev = val;
  1478. else
  1479. #ifdef CONFIG_SOC_SERIES_PEARLRIVER
  1480. record_attr.input_dev = AUDIO_ANALOG_MIC0;
  1481. #else
  1482. record_attr.input_dev = AUDIO_ANALOG_MIC0;
  1483. #endif
  1484. if (!cmd_search_value_by_key(argc, argv, SHELL_CMD_CH0_GAIN_KEY, &val))
  1485. record_attr.ch_gain[0] = (int16_t)val;
  1486. else
  1487. record_attr.ch_gain[0] = 0; /* 0dB */
  1488. #if (ADC_CH_NUM_MAX >= 2)
  1489. if (!cmd_search_value_by_key(argc, argv, SHELL_CMD_CH1_GAIN_KEY, &val))
  1490. record_attr.ch_gain[1] = (int16_t)val;
  1491. else
  1492. record_attr.ch_gain[1] = 0; /* 0dB */
  1493. #endif
  1494. #if (ADC_CH_NUM_MAX == 4)
  1495. if (!cmd_search_value_by_key(argc, argv, SHELL_CMD_CH2_GAIN_KEY, &val))
  1496. record_attr.ch_gain[2] = (int16_t)val;
  1497. else
  1498. record_attr.ch_gain[2] = 0; /* 0dB */
  1499. if (!cmd_search_value_by_key(argc, argv, SHELL_CMD_CH3_GAIN_KEY, &val))
  1500. record_attr.ch_gain[3] = (int16_t)val;
  1501. else
  1502. record_attr.ch_gain[3] = 0; /* 0dB */
  1503. #endif
  1504. if (!cmd_search_value_by_key(argc, argv, SHELL_CMD_DUMP_LEN_KEY, &val)) {
  1505. record_attr.dump_len = val;
  1506. LOG_INF("Dump length: %d", record_attr.dump_len);
  1507. } else {
  1508. record_attr.dump_len = 16;
  1509. }
  1510. if (!cmd_search_value_by_key(argc, argv, SHELL_CMD_CHL_INDEX_KEY, &val))
  1511. record_attr.id = val;
  1512. else
  1513. record_attr.id = 0;
  1514. if (record_attr.id >= AUDIO_RECORD_MAX_HANLDER) {
  1515. LOG_ERR("invalid channel id %d", record_attr.id);
  1516. return -EINVAL;
  1517. }
  1518. if (!cmd_search_value_by_key(argc, argv, SHELL_CMD_HW_TRIGGER_SRC_KEY, &val))
  1519. record_attr.hw_trigger_src = val + 1;
  1520. else
  1521. record_attr.hw_trigger_src = 0;
  1522. if (!cmd_search_value_by_key(argc, argv, SHELL_CMD_ADDA_KEY, &val))
  1523. record_attr.adda_en = val;
  1524. else
  1525. record_attr.adda_en = 0;
  1526. if (!cmd_search_value_by_key(argc, argv, SHELL_CMD_RECORD_FILE, &val))
  1527. record_attr.rec_file = val;
  1528. else
  1529. record_attr.rec_file = 0;
  1530. if (!cmd_search_value_by_key(argc, argv, SHELL_CMD_DMA_INTERLEAVED_EN, &val))
  1531. record_attr.dma_separated_en = val;
  1532. else
  1533. record_attr.dma_separated_en = 0;
  1534. if (audio_recorder[record_attr.id])
  1535. audio_record_stop(audio_recorder[record_attr.id]);
  1536. handle = audio_record_create(&record_attr);
  1537. if (!handle)
  1538. return -EFAULT;
  1539. ret = audio_record_config(handle);
  1540. if (ret)
  1541. return ret;
  1542. audio_recorder[record_attr.id] = handle;
  1543. return ret;
  1544. }
  1545. /* @brief command to start recording */
  1546. static int cmd_record_start(const struct shell *shell,
  1547. size_t argc, char **argv)
  1548. {
  1549. uint32_t val;
  1550. uint8_t id;
  1551. int ret;
  1552. if (!cmd_search_value_by_key(argc, argv, SHELL_CMD_CHL_INDEX_KEY, &val))
  1553. id = val;
  1554. else
  1555. id = 0;
  1556. if (!audio_recorder[id]) {
  1557. LOG_ERR("audio recorder[%d] does not create yet", id);
  1558. return -ENXIO;
  1559. }
  1560. ret = audio_record_start(audio_recorder[id]);
  1561. if (ret) {
  1562. LOG_ERR("recorder[id:%d] start error:%d", id, ret);
  1563. } else {
  1564. LOG_INF("recorder[id:%d] start successfully", id);
  1565. }
  1566. return ret;
  1567. }
  1568. /* @brief command to stop recording */
  1569. static int cmd_record_stop(const struct shell *shell,
  1570. size_t argc, char **argv)
  1571. {
  1572. uint32_t val;
  1573. uint8_t id;
  1574. if (!cmd_search_value_by_key(argc, argv, SHELL_CMD_CHL_INDEX_KEY, &val))
  1575. id = val;
  1576. else
  1577. id = 0;
  1578. if (id >= AUDIO_RECORD_MAX_HANLDER) {
  1579. LOG_ERR("invalid channel id %d", id);
  1580. return -EINVAL;
  1581. }
  1582. if (audio_recorder[id])
  1583. audio_record_stop(audio_recorder[id]);
  1584. return 0;
  1585. }
  1586. /* @brief command to cat pcm energy average value */
  1587. static int cmd_record_cat_pcm_energy(const struct shell *shell,
  1588. size_t argc, char **argv)
  1589. {
  1590. int len;
  1591. int recor_data_sum=0;
  1592. int16_t temp_value = 0;
  1593. memset(shell_audio_record_buffer, 0, SHELL_AUDIO_RECORD_SAVE_BUFFER_SIZE);
  1594. len=ring_buf_get(&shell_audio_ringbuf, (uint8_t *)shell_audio_record_buffer,
  1595. ring_buf_size_get(&shell_audio_ringbuf));
  1596. for (size_t i = 0; i <= len/2; i++)
  1597. {
  1598. if(shell_audio_record_buffer[i] < 0)
  1599. temp_value= 0 -shell_audio_record_buffer[i];
  1600. else
  1601. temp_value = shell_audio_record_buffer[i];
  1602. recor_data_sum+=(int)temp_value;
  1603. }
  1604. printk("=len:%d record_data_ave:%d\n",len,recor_data_sum/(len/2));
  1605. if(ring_buf_size_get(&shell_audio_ringbuf) > 0)
  1606. printk("==ring_buf :%d",ring_buf_size_get(&shell_audio_ringbuf));
  1607. return 0;
  1608. }
  1609. /* @brief command to control recording dynamically */
  1610. static int cmd_record_ioctl(const struct shell *shell,
  1611. size_t argc, char **argv)
  1612. {
  1613. uint32_t val, param = 0;
  1614. uint8_t cmd, id = 0;
  1615. int ret = 0;
  1616. bool is_in = false;
  1617. if (!cmd_search_value_by_key(argc, argv, SHELL_CMD_IOCTL_CMD_KEY, &val)) {
  1618. cmd = val;
  1619. } else {
  1620. LOG_ERR("invalid cmd");
  1621. return -EINVAL;
  1622. }
  1623. if (!cmd_search_value_by_key(argc, argv, SHELL_CMD_IOCTL_PARAM_KEY, &val)) {
  1624. param = val;
  1625. is_in = true;
  1626. }
  1627. if (!cmd_search_value_by_key(argc, argv, SHELL_CMD_CHL_INDEX_KEY, &val))
  1628. id = (val < AUDIO_RECORD_MAX_HANLDER) ? val : 0;
  1629. if (!audio_recorder[id]) {
  1630. LOG_ERR("audio recorder[%d] does not create yet", id);
  1631. return -ENXIO;
  1632. }
  1633. if (cmd == AIN_CMD_SET_ADC_GAIN) {
  1634. adc_gain gain = {0};
  1635. if (!cmd_search_value_by_key(argc, argv, SHELL_CMD_CH0_GAIN_KEY, &val))
  1636. gain.ch_gain[0] = val;
  1637. #if (ADC_CH_NUM_MAX == 2)
  1638. if (!cmd_search_value_by_key(argc, argv, SHELL_CMD_CH1_GAIN_KEY, &val))
  1639. gain.ch_gain[1] = val;
  1640. #endif
  1641. #if (ADC_CH_NUM_MAX == 4)
  1642. if (!cmd_search_value_by_key(argc, argv, SHELL_CMD_CH2_GAIN_KEY, &val))
  1643. gain.ch_gain[2] = val;
  1644. if (!cmd_search_value_by_key(argc, argv, SHELL_CMD_CH2_GAIN_KEY, &val))
  1645. gain.ch_gain[3] = val;
  1646. #endif
  1647. ret = audio_in_control(audio_recorder[id]->ain_dev,
  1648. audio_recorder[id]->ain_handle, cmd, (void *)&gain);
  1649. if (ret) {
  1650. LOG_ERR("Set gain error:%d", ret);
  1651. }
  1652. } else if ((cmd == AIN_CMD_GET_ADC_LEFT_GAIN_RANGE)
  1653. || (cmd == AIN_CMD_GET_ADC_RIGHT_GAIN_RANGE)) {
  1654. adc_gain_range range = {0};
  1655. ret = audio_in_control(audio_recorder[id]->ain_dev,
  1656. audio_recorder[id]->ain_handle, cmd, (void *)&range);
  1657. if (ret) {
  1658. LOG_ERR("Get gain range error:%d", ret);
  1659. } else {
  1660. LOG_INF("gain range [%d..%d]", range.min, range.max);
  1661. }
  1662. } else if (cmd == AIN_CMD_SPDIF_GET_CHANNEL_STATUS) {
  1663. audio_spdif_ch_status_t ch_status = {0};
  1664. ret = audio_in_control(audio_recorder[id]->ain_dev,
  1665. audio_recorder[id]->ain_handle, cmd, (void *)&ch_status);
  1666. if (ret) {
  1667. LOG_ERR("Get SPDIF status error:%d", ret);
  1668. } else {
  1669. LOG_INF("SPDIF CSL: 0x%x", ch_status.csl);
  1670. LOG_INF("SPDIF CSH: 0x%x", ch_status.csh);
  1671. }
  1672. } else if (cmd == AIN_CMD_BIND_CHANNEL) {
  1673. uint8_t bind_id;
  1674. if (!cmd_search_value_by_key(argc, argv, SHELL_CMD_IOCTL_CMD_BIND_ID_KEY, &val)) {
  1675. bind_id = val;
  1676. if (bind_id > AUDIO_RECORD_MAX_HANLDER) {
  1677. LOG_ERR("invalid bind id %d", bind_id);
  1678. return -EINVAL;
  1679. }
  1680. } else {
  1681. LOG_ERR("no bind id");
  1682. return -ENOTSUP;
  1683. }
  1684. LOG_INF("bind session id %d", bind_id);
  1685. param = (uint32_t)audio_recorder[bind_id]->ain_handle;
  1686. ret = audio_in_control(audio_recorder[id]->ain_dev,
  1687. audio_recorder[id]->ain_handle, cmd, (void *)param);
  1688. } else {
  1689. ret = audio_in_control(audio_recorder[id]->ain_dev,
  1690. audio_recorder[id]->ain_handle, cmd, (void *)&param);
  1691. if (ret) {
  1692. LOG_ERR("execute cmd:%d error:%d", cmd, ret);
  1693. } else {
  1694. if (!is_in) {
  1695. LOG_INF("cmd:%d result:%d", cmd, param);
  1696. }
  1697. }
  1698. }
  1699. return ret;
  1700. }
  1701. #endif
  1702. #ifdef CONFIG_AUDIO_DEBUG_TRACE
  1703. /* @brief command to show the audio debug trace information */
  1704. static int cmd_audio_debug_trace(const struct shell *shell,
  1705. size_t argc, char **argv)
  1706. {
  1707. if ((argc > 1) && !strcmp(argv[1], "clear"))
  1708. audio_debug_trace_clear();
  1709. else
  1710. audio_debug_trace_info();
  1711. return 0;
  1712. }
  1713. #endif
  1714. #ifdef CONFIG_AUDIO_DRIVER_STREAM_SHELL
  1715. SHELL_STATIC_SUBCMD_SET_CREATE(sub_audio_play,
  1716. SHELL_CMD(start, NULL, "Play start.", cmd_play_start),
  1717. SHELL_CMD(stop, NULL, "Play stop.", cmd_play_stop),
  1718. SHELL_CMD(ioctl, NULL, "Play io control and comands.", cmd_play_ioctl),
  1719. SHELL_SUBCMD_SET_END /* Array terminated. */
  1720. );
  1721. SHELL_STATIC_SUBCMD_SET_CREATE(sub_audio_record,
  1722. SHELL_CMD(create, NULL, "Record create an instance.", cmd_record_create),
  1723. SHELL_CMD(start, NULL, "Record start.", cmd_record_start),
  1724. SHELL_CMD(stop, NULL, "Record stop.", cmd_record_stop),
  1725. SHELL_CMD(cat_pcm_energy, NULL, "cat record data.", cmd_record_cat_pcm_energy),
  1726. SHELL_CMD(ioctl, NULL, "Play io control and comands.", cmd_record_ioctl),
  1727. SHELL_SUBCMD_SET_END /* Array terminated. */
  1728. );
  1729. #endif
  1730. SHELL_STATIC_SUBCMD_SET_CREATE(sub_acts_audio,
  1731. #ifdef TIME_SRC_TRIGGER
  1732. SHELL_CMD(time_src, NULL, "time_src test.", cmd_time_src),
  1733. #endif
  1734. SHELL_CMD(dump_reg, NULL, "Dump register.", cmd_dump_register),
  1735. #ifdef CONFIG_AUDIO_DRIVER_STREAM_SHELL
  1736. SHELL_CMD(play, &sub_audio_play, "Audio play.", NULL),
  1737. SHELL_CMD(record, &sub_audio_record, "Audio record.", NULL),
  1738. #endif
  1739. #ifdef CONFIG_AUDIO_DEBUG_TRACE
  1740. SHELL_CMD(debug, NULL, "Audio debug trace.", cmd_audio_debug_trace),
  1741. #endif
  1742. SHELL_SUBCMD_SET_END /* Array terminated. */
  1743. );
  1744. SHELL_CMD_REGISTER(audio, &sub_acts_audio, "Actions audio commands", NULL);