i2s.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  1. /* Copyright 2018 Canaan Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. #ifndef _DRIVER_I2S_H
  16. #define _DRIVER_I2S_H
  17. #include <stddef.h>
  18. #include <stdint.h>
  19. #include "bsp.h"
  20. #include "dmac.h"
  21. #include "io.h"
  22. #include "platform.h"
  23. #ifdef __cplusplus
  24. extern "C" {
  25. #endif
  26. #define I2S0_IN_D0 90
  27. #define I2S0_SCLK 88
  28. #define I2S0_WS 89
  29. typedef enum _i2s_device_number
  30. {
  31. I2S_DEVICE_0 = 0,
  32. I2S_DEVICE_1 = 1,
  33. I2S_DEVICE_2 = 2,
  34. I2S_DEVICE_MAX
  35. } i2s_device_number_t;
  36. typedef enum _i2s_channel_num
  37. {
  38. I2S_CHANNEL_0 = 0,
  39. I2S_CHANNEL_1 = 1,
  40. I2S_CHANNEL_2 = 2,
  41. I2S_CHANNEL_3 = 3
  42. } i2s_channel_num_t;
  43. typedef enum _i2s_transmit
  44. {
  45. I2S_TRANSMITTER = 0,
  46. I2S_RECEIVER = 1
  47. } i2s_transmit_t;
  48. typedef enum _i2s_work_mode
  49. {
  50. STANDARD_MODE = 1,
  51. RIGHT_JUSTIFYING_MODE = 2,
  52. LEFT_JUSTIFYING_MODE = 4
  53. } i2s_work_mode_t;
  54. typedef enum _sclk_gating_cycles
  55. {
  56. /* Clock gating is diable */
  57. NO_CLOCK_GATING = 0x0,
  58. /* Gating after 12 sclk cycles */
  59. CLOCK_CYCLES_12 = 0x1,
  60. /* Gating after 16 sclk cycles */
  61. CLOCK_CYCLES_16 = 0x2,
  62. /* Gating after 20 sclk cycles */
  63. CLOCK_CYCLES_20 = 0x3,
  64. /* Gating after 24 sclk cycles */
  65. CLOCK_CYCLES_24 = 0x4
  66. } i2s_sclk_gating_cycles_t;
  67. typedef enum _word_select_cycles
  68. {
  69. /* 16 sclk cycles */
  70. SCLK_CYCLES_16 = 0x0,
  71. /* 24 sclk cycles */
  72. SCLK_CYCLES_24 = 0x1,
  73. /* 32 sclk cycles */
  74. SCLK_CYCLES_32 = 0x2
  75. } i2s_word_select_cycles_t;
  76. typedef enum _word_length
  77. {
  78. /* Ignore the word length */
  79. IGNORE_WORD_LENGTH = 0x0,
  80. /* 12-bit data resolution of the receiver */
  81. RESOLUTION_12_BIT = 0x1,
  82. /* 16-bit data resolution of the receiver */
  83. RESOLUTION_16_BIT = 0x2,
  84. /* 20-bit data resolution of the receiver */
  85. RESOLUTION_20_BIT = 0x3,
  86. /* 24-bit data resolution of the receiver */
  87. RESOLUTION_24_BIT = 0x4,
  88. /* 32-bit data resolution of the receiver */
  89. RESOLUTION_32_BIT = 0x5
  90. } i2s_word_length_t;
  91. typedef enum _fifo_threshold
  92. {
  93. /* Interrupt trigger when FIFO level is 1 */
  94. TRIGGER_LEVEL_1 = 0x0,
  95. /* Interrupt trigger when FIFO level is 2 */
  96. TRIGGER_LEVEL_2 = 0x1,
  97. /* Interrupt trigger when FIFO level is 3 */
  98. TRIGGER_LEVEL_3 = 0x2,
  99. /* Interrupt trigger when FIFO level is 4 */
  100. TRIGGER_LEVEL_4 = 0x3,
  101. /* Interrupt trigger when FIFO level is 5 */
  102. TRIGGER_LEVEL_5 = 0x4,
  103. /* Interrupt trigger when FIFO level is 6 */
  104. TRIGGER_LEVEL_6 = 0x5,
  105. /* Interrupt trigger when FIFO level is 7 */
  106. TRIGGER_LEVEL_7 = 0x6,
  107. /* Interrupt trigger when FIFO level is 8 */
  108. TRIGGER_LEVEL_8 = 0x7,
  109. /* Interrupt trigger when FIFO level is 9 */
  110. TRIGGER_LEVEL_9 = 0x8,
  111. /* Interrupt trigger when FIFO level is 10 */
  112. TRIGGER_LEVEL_10 = 0x9,
  113. /* Interrupt trigger when FIFO level is 11 */
  114. TRIGGER_LEVEL_11 = 0xa,
  115. /* Interrupt trigger when FIFO level is 12 */
  116. TRIGGER_LEVEL_12 = 0xb,
  117. /* Interrupt trigger when FIFO level is 13 */
  118. TRIGGER_LEVEL_13 = 0xc,
  119. /* Interrupt trigger when FIFO level is 14 */
  120. TRIGGER_LEVEL_14 = 0xd,
  121. /* Interrupt trigger when FIFO level is 15 */
  122. TRIGGER_LEVEL_15 = 0xe,
  123. /* Interrupt trigger when FIFO level is 16 */
  124. TRIGGER_LEVEL_16 = 0xf
  125. } i2s_fifo_threshold_t;
  126. typedef struct _i2s_ier
  127. {
  128. /* Bit 0 is ien, 0 for disable i2s and 1 for enable i2s */
  129. uint32_t ien : 1;
  130. /* Bits [31:1] is reserved */
  131. uint32_t resv : 31;
  132. } __attribute__((packed, aligned(4))) i2s_ier_t;
  133. typedef union _ier_u
  134. {
  135. i2s_ier_t ier;
  136. uint32_t reg_data;
  137. } ier_t;
  138. typedef struct _i2s_irer
  139. {
  140. /* Bit 0 is receiver block enable,
  141. * 0 for receiver disable
  142. * 1 for receiver enable
  143. */
  144. uint32_t rxen : 1;
  145. /* Bits [31:1] is reserved */
  146. uint32_t resv : 31;
  147. } __attribute__((packed, aligned(4))) i2s_irer_t;
  148. typedef union _irer_u
  149. {
  150. i2s_irer_t irer;
  151. uint32_t reg_data;
  152. } irer_t;
  153. typedef struct _i2s_iter
  154. {
  155. uint32_t txen : 1;
  156. /* Bit 0 is transmitter block enable,
  157. * 0 for transmitter disable
  158. * 1 for transmitter enable
  159. */
  160. uint32_t resv : 31;
  161. /* Bits [31:1] is reserved */
  162. } __attribute__((packed, aligned(4))) i2s_iter_t;
  163. typedef union _iter_u
  164. {
  165. i2s_iter_t iter;
  166. uint32_t reg_data;
  167. } iter_t;
  168. typedef struct _i2s_cer
  169. {
  170. uint32_t clken : 1;
  171. /* Bit 0 is clock generation enable/disable,
  172. * 0 for clock generation disable,
  173. * 1 for clock generation enable
  174. */
  175. uint32_t resv : 31;
  176. /* Bits [31:1] is reserved */
  177. } __attribute__((packed, aligned(4))) i2s_cer_t;
  178. typedef union _cer_u
  179. {
  180. i2s_cer_t cer;
  181. uint32_t reg_data;
  182. } cer_t;
  183. typedef struct _i2s_ccr
  184. {
  185. /* Bits [2:0] is used to program the gating of sclk,
  186. * 0x0 for clock gating is diable,
  187. * 0x1 for gating after 12 sclk cycles
  188. * 0x2 for gating after 16 sclk cycles
  189. * 0x3 for gating after 20 sclk cycles
  190. * 0x4 for gating after 24 sclk cycles
  191. */
  192. uint32_t clk_gate : 3;
  193. /* Bits [4:3] used program the number of sclk cycles for which the
  194. * word select line stayd in the left aligned or right aligned mode.
  195. * 0x0 for 16sclk cycles, 0x1 for 24 sclk cycles 0x2 for 32 sclk
  196. * cycles
  197. */
  198. uint32_t clk_word_size : 2;
  199. /* Bit[5:7] is alignment mode setting.
  200. * 0x1 for standard i2s format
  201. * 0x2 for right aligned format
  202. * 0x4 for left aligned format
  203. */
  204. uint32_t align_mode : 3;
  205. /* Bit[8] is DMA transmit enable control */
  206. uint32_t dma_tx_en : 1;
  207. /* Bit[9] is DMA receive enable control */
  208. uint32_t dma_rx_en : 1;
  209. uint32_t dma_divide_16 : 1;
  210. /* Bit[10] split 32bit data to two 16 bit data and filled in left
  211. * and right channel. Used with dma_tx_en or dma_rx_en
  212. */
  213. uint32_t sign_expand_en : 1;
  214. uint32_t resv : 20;
  215. /* Bits [31:11] is reseved */
  216. } __attribute__((packed, aligned(4))) i2s_ccr_t;
  217. typedef union _ccr_u
  218. {
  219. i2s_ccr_t ccr;
  220. uint32_t reg_data;
  221. } ccr_t;
  222. typedef struct _i2s_rxffr
  223. {
  224. uint32_t rxffr : 1;
  225. /* Bit 0 is receiver FIFO reset,
  226. * 0 for does not flush RX FIFO, 1 for flush RX FIFO
  227. */
  228. uint32_t resv : 31;
  229. /* Bits [31:1] is reserved */
  230. } __attribute__((packed, aligned(4))) i2s_rxffr_t;
  231. typedef union _rxffr_u
  232. {
  233. i2s_rxffr_t rxffr;
  234. uint32_t reg_data;
  235. } rxffr_t;
  236. typedef struct _i2s_lrbrthr
  237. {
  238. uint32_t fifo : 16;
  239. /* Bits [15:0] if used data receive or transmit */
  240. uint32_t resv : 16;
  241. } i2s_lrbrthr_t;
  242. typedef union _lrbthr_u
  243. {
  244. i2s_lrbrthr_t buffer;
  245. uint32_t reg_data;
  246. } lrbthr_t;
  247. typedef struct _i2s_rthr
  248. {
  249. /* Bits [15:0] is right stereo data transmitted serially
  250. * from transmit channel input
  251. */
  252. uint32_t rthrx : 16;
  253. /* Bits [31:16] is reserved */
  254. uint32_t resv : 16;
  255. } __attribute__((packed, aligned(4))) i2s_rthr_t;
  256. typedef union _rthr_u
  257. {
  258. i2s_rthr_t rthr;
  259. uint32_t reg_data;
  260. } rthr_t;
  261. typedef struct _i2s_rer
  262. {
  263. /* Bit 0 is receive channel enable/disable, 0 for receive channel disable,
  264. *1 for receive channel enable
  265. */
  266. uint32_t rxchenx : 1;
  267. /* Bits [31:1] is reseved */
  268. uint32_t resv : 31;
  269. } __attribute__((packed, aligned(4))) i2s_rer_t;
  270. typedef union _rer_u
  271. {
  272. i2s_rer_t rer;
  273. uint32_t reg_data;
  274. } rer_t;
  275. typedef struct _i2s_ter
  276. {
  277. /* Bit 0 is transmit channel enable/disable, 0 for transmit channel disable,
  278. * 1 for transmit channel enable
  279. */
  280. uint32_t txchenx : 1;
  281. /* Bits [31:1] is reseved */
  282. uint32_t resv : 31;
  283. } __attribute__((packed, aligned(4))) i2s_ter_t;
  284. typedef union _ter_u
  285. {
  286. i2s_ter_t ter;
  287. uint32_t reg_data;
  288. } ter_t;
  289. typedef struct _i2s_rcr_tcr
  290. {
  291. /* Bits [2:0] is used to program desired data resolution of
  292. * receiver/transmitter,
  293. * 0x0 for ignore the word length
  294. * 0x1 for 12-bit data resolution of the receiver/transmitter,
  295. * 0x2 for 16-bit data resolution of the receiver/transmitter,
  296. * 0x3 for 20-bit data resolution of the receiver/transmitter,
  297. * 0x4 for 24-bit data resolution of the receiver/transmitter,
  298. * 0x5 for 32-bit data resolution of the receiver/transmitter
  299. */
  300. uint32_t wlen : 3;
  301. /* Bits [31:3] is reseved */
  302. uint32_t resv : 29;
  303. } __attribute__((packed, aligned(4))) i2s_rcr_tcr_t;
  304. typedef union _rcr_tcr_u
  305. {
  306. i2s_rcr_tcr_t rcr_tcr;
  307. uint32_t reg_data;
  308. } rcr_tcr_t;
  309. typedef struct _i2s_isr
  310. {
  311. /* Bit 0 is status of receiver data avaliable interrupt
  312. * 0x0 for RX FIFO trigger level not reached
  313. * 0x1 for RX FIFO trigger level is reached
  314. */
  315. uint32_t rxda : 1;
  316. /* Bit 1 is status of data overrun interrupt for rx channel
  317. * 0x0 for RX FIFO write valid
  318. * 0x1 for RX FIFO write overrun
  319. */
  320. uint32_t rxfo : 1;
  321. /* Bits [3:2] is reserved */
  322. uint32_t resv1 : 2;
  323. /* Bit 4 is status of transmit empty triger interrupt
  324. * 0x0 for TX FIFO triiger level is reach
  325. * 0x1 for TX FIFO trigger level is not reached
  326. */
  327. uint32_t txfe : 1;
  328. /* BIt 5 is status of data overrun interrupt for the TX channel
  329. * 0x0 for TX FIFO write valid
  330. * 0x1 for TX FIFO write overrun
  331. */
  332. uint32_t txfo : 1;
  333. /* BIts [31:6] is reserved */
  334. uint32_t resv2 : 26;
  335. } __attribute__((packed, aligned(4))) i2s_isr_t;
  336. typedef union _isr_u
  337. {
  338. i2s_isr_t isr;
  339. uint32_t reg_data;
  340. } isr_t;
  341. typedef struct _i2s_imr
  342. {
  343. /* Bit 0 is mask RX FIFO data available interrupt
  344. * 0x0 for unmask RX FIFO data available interrupt
  345. * 0x1 for mask RX FIFO data available interrupt
  346. */
  347. uint32_t rxdam : 1;
  348. /* Bit 1 is mask RX FIFO overrun interrupt
  349. * 0x0 for unmask RX FIFO overrun interrupt
  350. * 0x1 for mask RX FIFO overrun interrupt
  351. */
  352. uint32_t rxfom : 1;
  353. /* Bits [3:2] is reserved */
  354. uint32_t resv1 : 2;
  355. /* Bit 4 is mask TX FIFO empty interrupt,
  356. * 0x0 for unmask TX FIFO empty interrupt,
  357. * 0x1 for mask TX FIFO empty interrupt
  358. */
  359. uint32_t txfem : 1;
  360. /* BIt 5 is mask TX FIFO overrun interrupt
  361. * 0x0 for mask TX FIFO overrun interrupt
  362. * 0x1 for unmash TX FIFO overrun interrupt
  363. */
  364. uint32_t txfom : 1;
  365. /* Bits [31:6] is reserved */
  366. uint32_t resv2 : 26;
  367. } __attribute__((packed, aligned(4))) i2s_imr_t;
  368. typedef union _imr_u
  369. {
  370. i2s_imr_t imr;
  371. uint32_t reg_data;
  372. } imr_t;
  373. typedef struct _i2s_ror
  374. {
  375. /* Bit 0 is read this bit to clear RX FIFO data overrun interrupt
  376. * 0x0 for RX FIFO write valid,
  377. *0x1 for RX FIFO write overrun
  378. */
  379. uint32_t rxcho : 1;
  380. /* Bits [31:1] is reserved */
  381. uint32_t resv : 31;
  382. } __attribute__((packed, aligned(4))) i2s_ror_t;
  383. typedef union _ror_u
  384. {
  385. i2s_ror_t ror;
  386. uint32_t reg_data;
  387. } ror_t;
  388. typedef struct _i2s_tor
  389. {
  390. /* Bit 0 is read this bit to clear TX FIFO data overrun interrupt
  391. * 0x0 for TX FIFO write valid,
  392. *0x1 for TX FIFO write overrun
  393. */
  394. uint32_t txcho : 1;
  395. /* Bits [31:1] is reserved */
  396. uint32_t resv : 31;
  397. } __attribute__((packed, aligned(4))) i2s_tor_t;
  398. typedef union _tor_u
  399. {
  400. i2s_tor_t tor;
  401. uint32_t reg_data;
  402. } tor_t;
  403. typedef struct _i2s_rfcr
  404. {
  405. /* Bits [3:0] is used program the trigger level in the RX FIFO at
  406. * which the receiver data available interrupt generate,
  407. * 0x0 for interrupt trigger when FIFO level is 1,
  408. * 0x2 for interrupt trigger when FIFO level is 2,
  409. * 0x3 for interrupt trigger when FIFO level is 4,
  410. * 0x4 for interrupt trigger when FIFO level is 5,
  411. * 0x5 for interrupt trigger when FIFO level is 6,
  412. * 0x6 for interrupt trigger when FIFO level is 7,
  413. * 0x7 for interrupt trigger when FIFO level is 8,
  414. * 0x8 for interrupt trigger when FIFO level is 9,
  415. * 0x9 for interrupt trigger when FIFO level is 10,
  416. * 0xa for interrupt trigger when FIFO level is 11,
  417. * 0xb for interrupt trigger when FIFO level is 12,
  418. * 0xc for interrupt trigger when FIFO level is 13,
  419. * 0xd for interrupt trigger when FIFO level is 14,
  420. * 0xe for interrupt trigger when FIFO level is 15,
  421. * 0xf for interrupt trigger when FIFO level is 16
  422. */
  423. uint32_t rxchdt : 4;
  424. /* Bits [31:4] is reserved */
  425. uint32_t rsvd_rfcrx : 28;
  426. } __attribute__((packed, aligned(4))) i2s_rfcr_t;
  427. typedef union _rfcr_u
  428. {
  429. i2s_rfcr_t rfcr;
  430. uint32_t reg_data;
  431. } rfcr_t;
  432. typedef struct _i2s_tfcr
  433. {
  434. /* Bits [3:0] is used program the trigger level in the TX FIFO at
  435. * which the receiver data available interrupt generate,
  436. * 0x0 for interrupt trigger when FIFO level is 1,
  437. * 0x2 for interrupt trigger when FIFO level is 2,
  438. * 0x3 for interrupt trigger when FIFO level is 4,
  439. * 0x4 for interrupt trigger when FIFO level is 5,
  440. * 0x5 for interrupt trigger when FIFO level is 6,
  441. * 0x6 for interrupt trigger when FIFO level is 7,
  442. * 0x7 for interrupt trigger when FIFO level is 8,
  443. * 0x8 for interrupt trigger when FIFO level is 9,
  444. * 0x9 for interrupt trigger when FIFO level is 10,
  445. * 0xa for interrupt trigger when FIFO level is 11,
  446. * 0xb for interrupt trigger when FIFO level is 12,
  447. * 0xc for interrupt trigger when FIFO level is 13,
  448. * 0xd for interrupt trigger when FIFO level is 14,
  449. * 0xe for interrupt trigger when FIFO level is 15,
  450. * 0xf for interrupt trigger when FIFO level is 16
  451. */
  452. uint32_t txchet : 4;
  453. /* Bits [31:4] is reserved */
  454. uint32_t rsvd_tfcrx : 28;
  455. } __attribute__((packed, aligned(4))) i2s_tfcr_t;
  456. typedef union _tfcr_u
  457. {
  458. i2s_tfcr_t tfcr;
  459. uint32_t reg_data;
  460. } tfcr_t;
  461. typedef struct _i2s_rff
  462. {
  463. /* Bit 0 is receiver channel FIFO reset,
  464. * 0x0 for does not flush an individual RX FIFO,
  465. * 0x1 for flush an indiviadual RX FIFO
  466. */
  467. uint32_t rxchfr : 1;
  468. /*< Bits [31:1] is reserved ,write only */
  469. uint32_t rsvd_rffx : 31;
  470. } __attribute__((packed, aligned(4))) i2s_rff_t;
  471. typedef union _rff_u
  472. {
  473. i2s_rff_t rff;
  474. uint32_t reg_data;
  475. } rff_t;
  476. typedef struct _i2s_tff
  477. {
  478. /* Bit 0 is transmit channel FIFO reset,
  479. * 0x0 for does not flush an individual TX FIFO,
  480. * 0x1 for flush an indiviadual TX FIFO
  481. */
  482. uint32_t rtxchfr : 1;
  483. /*< Bits [31:1] is reserved ,write only */
  484. uint32_t rsvd_rffx : 31;
  485. } __attribute__((packed, aligned(4))) i2s_tff_t;
  486. typedef union tff_u
  487. {
  488. i2s_tff_t tff;
  489. uint32_t reg_data;
  490. } tff_t;
  491. typedef struct _i2s_channel
  492. {
  493. /* Left Receive or Left Transmit Register (0x20) */
  494. volatile uint32_t left_rxtx;
  495. /* Right Receive or Right Transmit Register (0x24) */
  496. volatile uint32_t right_rxtx;
  497. /* Receive Enable Register (0x28) */
  498. volatile uint32_t rer;
  499. /* Transmit Enable Register (0x2c) */
  500. volatile uint32_t ter;
  501. /* Receive Configuration Register (0x30) */
  502. volatile uint32_t rcr;
  503. /* Transmit Configuration Register (0x34) */
  504. volatile uint32_t tcr;
  505. /* Interrupt Status Register (0x38) */
  506. volatile uint32_t isr;
  507. /* Interrupt Mask Register (0x3c) */
  508. volatile uint32_t imr;
  509. /* Receive Overrun Register (0x40) */
  510. volatile uint32_t ror;
  511. /* Transmit Overrun Register (0x44) */
  512. volatile uint32_t tor;
  513. /* Receive FIFO Configuration Register (0x48) */
  514. volatile uint32_t rfcr;
  515. /* Transmit FIFO Configuration Register (0x4c) */
  516. volatile uint32_t tfcr;
  517. /* Receive FIFO Flush Register (0x50) */
  518. volatile uint32_t rff;
  519. /* Transmit FIFO Flush Register (0x54) */
  520. volatile uint32_t tff;
  521. /* reserved (0x58-0x5c) */
  522. volatile uint32_t reserved1[2];
  523. } __attribute__((packed, aligned(4))) i2s_channel_t;
  524. /****is* i2s.api/dw_i2s_portmap
  525. * NAME
  526. * i2s_t
  527. * DESCRIPTION
  528. * This is the structure used for accessing the i2s register
  529. * portmap.
  530. * EXAMPLE
  531. * struct i2s_t *portmap;
  532. * portmap = (struct dw_i2s_portmap *) DW_APB_I2S_BASE;
  533. * SOURCE
  534. */
  535. typedef struct _i2s
  536. {
  537. /* I2S Enable Register (0x00) */
  538. volatile uint32_t ier;
  539. /* I2S Receiver Block Enable Register (0x04) */
  540. volatile uint32_t irer;
  541. /* I2S Transmitter Block Enable Register (0x08) */
  542. volatile uint32_t iter;
  543. /* Clock Enable Register (0x0c) */
  544. volatile uint32_t cer;
  545. /* Clock Configuration Register (0x10) */
  546. volatile uint32_t ccr;
  547. /* Receiver Block FIFO Reset Register (0x04) */
  548. volatile uint32_t rxffr;
  549. /* Transmitter Block FIFO Reset Register (0x18) */
  550. volatile uint32_t txffr;
  551. /* reserved (0x1c) */
  552. volatile uint32_t reserved1;
  553. volatile i2s_channel_t channel[4];
  554. /* reserved (0x118-0x1bc) */
  555. volatile uint32_t reserved2[40];
  556. /* Receiver Block DMA Register (0x1c0) */
  557. volatile uint32_t rxdma;
  558. /* Reset Receiver Block DMA Register (0x1c4) */
  559. volatile uint32_t rrxdma;
  560. /* Transmitter Block DMA Register (0x1c8) */
  561. volatile uint32_t txdma;
  562. /* Reset Transmitter Block DMA Register (0x1cc) */
  563. volatile uint32_t rtxdma;
  564. /* reserved (0x1d0-0x1ec) */
  565. volatile uint32_t reserved3[8];
  566. /* Component Parameter Register 2 (0x1f0) */
  567. volatile uint32_t i2s_comp_param_2;
  568. /* Component Parameter Register 1 (0x1f4) */
  569. volatile uint32_t i2s_comp_param_1;
  570. /* I2S Component Version Register (0x1f8) */
  571. volatile uint32_t i2s_comp_version_1;
  572. /* I2S Component Type Register (0x1fc) */
  573. volatile uint32_t i2s_comp_type;
  574. } __attribute__((packed, aligned(4))) i2s_t;
  575. typedef enum _i2s_transfer_mode
  576. {
  577. I2S_SEND,
  578. I2S_RECEIVE,
  579. } i2s_transfer_mode_t;
  580. typedef struct _i2s_data_t
  581. {
  582. dmac_channel_number_t tx_channel;
  583. dmac_channel_number_t rx_channel;
  584. uint32_t *tx_buf;
  585. size_t tx_len;
  586. uint32_t *rx_buf;
  587. size_t rx_len;
  588. i2s_transfer_mode_t transfer_mode;
  589. bool nowait_dma_idle;
  590. bool wait_dma_done;
  591. } i2s_data_t;
  592. /**
  593. * @brief I2S object instance
  594. */
  595. extern volatile i2s_t *const i2s[3];
  596. /**
  597. * @brief I2s init
  598. *
  599. * @param[in] device_num The device number
  600. * @param[in] rxtx_mode I2s work mode
  601. * @param[in] channel_mask Channel mask to which channel work
  602. *
  603. */
  604. void i2s_init(i2s_device_number_t device_num, i2s_transmit_t rxtx_mode, uint32_t channel_mask);
  605. /**
  606. * @brief Read pcm data from dma
  607. *
  608. * @param[in] device_num which of device
  609. * @param[in] buf save read data
  610. * @param[in] buf_len the length to read form i2s
  611. * @param[in] channel_num The dma channel number
  612. *
  613. * @return result
  614. * - 0 Success
  615. * - Other Fail
  616. */
  617. void i2s_receive_data_dma(i2s_device_number_t device_num, uint32_t *buf, size_t buf_len,
  618. dmac_channel_number_t channel_num);
  619. /**
  620. * @brief Write pcm data to channel_num channel by dma, first wait dmac done
  621. *
  622. * @param[in] device_num which of device
  623. * @param[in] pcm Send data
  624. * @param[in] buf_len Send data length
  625. * @param[in] channel_num dmac channel
  626. *
  627. */
  628. void i2s_send_data_dma(i2s_device_number_t device_num, const void *buf, size_t buf_len, dmac_channel_number_t channel_num);
  629. /**
  630. * @brief I2S receive channel configure
  631. *
  632. * @param[in] device_num The device number
  633. * @param[in] channel_num The channel number
  634. * @param[in] word_length The word length
  635. * @param[in] word_select_size The word select size
  636. * @param[in] trigger_level The trigger level
  637. */
  638. void i2s_rx_channel_config(i2s_device_number_t device_num,
  639. i2s_channel_num_t channel_num,
  640. i2s_word_length_t word_length,
  641. i2s_word_select_cycles_t word_select_size,
  642. i2s_fifo_threshold_t trigger_level,
  643. i2s_work_mode_t word_mode);
  644. /**
  645. * @brief I2S transmit channel enable
  646. *
  647. * @param[in] device_num The device number
  648. * @param[in] channel_num The channel number
  649. * @param[in] word_length The word length
  650. * @param[in] word_select_size The word select size
  651. * @param[in] trigger_level The trigger level
  652. */
  653. void i2s_tx_channel_config(i2s_device_number_t device_num,
  654. i2s_channel_num_t channel_num,
  655. i2s_word_length_t word_length,
  656. i2s_word_select_cycles_t word_select_size,
  657. i2s_fifo_threshold_t trigger_level,
  658. i2s_work_mode_t word_mode);
  659. /**
  660. * @brief Play PCM format audio
  661. *
  662. * @param[in] device_num The device number
  663. * @param[in] channel_num The channel number
  664. * @param[in] buf PCM data
  665. * @param[in] buf_len PCM data length
  666. * @param[in] frame Transmit amount once
  667. * @param[in] bits_per_sample Sample bit length
  668. * @param[in] track_num Track amount
  669. */
  670. void i2s_play(i2s_device_number_t device_num, dmac_channel_number_t channel_num,
  671. const uint8_t *buf, size_t buf_len, size_t frame, size_t bits_per_sample, uint8_t track_num);
  672. /**
  673. * @brief Play PCM format audio
  674. *
  675. * @param[in] device_num The device number
  676. * @param[in] sample_rate The Sample rate
  677. *
  678. *
  679. * @return The real sample rate
  680. */
  681. uint32_t i2s_set_sample_rate(i2s_device_number_t device_num, uint32_t sample_rate);
  682. /**
  683. * @brief Set dma_divide_16 split 32bit data to two 16 bit data and filled in left
  684. * and right channel. Used with dma_tx_en or dma_rx_en
  685. *
  686. * @param[in] device_num The device number
  687. * @param[in] enable The value of dma_divide_16 0:disable 1:enable
  688. *
  689. * @return result
  690. * - 0 Success
  691. * - Other Fail
  692. */
  693. int i2s_set_dma_divide_16(i2s_device_number_t device_num, uint32_t enable);
  694. /**
  695. * @brief Get dma_divide_16.
  696. *
  697. * @param[in] device_num The device number
  698. *
  699. * @return result
  700. * - <0 Fail
  701. * - other value of dma_divide_16
  702. */
  703. int i2s_get_dma_divide_16(i2s_device_number_t device_num);
  704. /**
  705. * @brief I2s handle transfer data operations
  706. *
  707. * @param[in] device_num I2s device number
  708. * @param[in] data I2s data information
  709. * @param[in] cb I2s dma callback
  710. *
  711. */
  712. void i2s_handle_data_dma(i2s_device_number_t device_num, i2s_data_t data, plic_interrupt_t *cb);
  713. #ifdef __cplusplus
  714. }
  715. #endif
  716. #endif