wm8753.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. #include <rthw.h>
  2. #include <rtthread.h>
  3. #include "stm32f10x.h"
  4. /*
  5. * WM8753 Driver
  6. */
  7. /* WM8753 register definitions */
  8. #define WM8753_DAC 0x01
  9. #define WM8753_ADC 0x02
  10. #define WM8753_PCM 0x03
  11. #define WM8753_HIFI 0x04
  12. #define WM8753_IOCTL 0x05
  13. #define WM8753_SRATE1 0x06
  14. #define WM8753_SRATE2 0x07
  15. #define WM8753_LDAC 0x08
  16. #define WM8753_RDAC 0x09
  17. #define WM8753_BASS 0x0a
  18. #define WM8753_TREBLE 0x0b
  19. #define WM8753_ALC1 0x0c
  20. #define WM8753_ALC2 0x0d
  21. #define WM8753_ALC3 0x0e
  22. #define WM8753_NGATE 0x0f
  23. #define WM8753_LADC 0x10
  24. #define WM8753_RADC 0x11
  25. #define WM8753_ADCTL1 0x12
  26. #define WM8753_3D 0x13
  27. #define WM8753_PWR1 0x14
  28. #define WM8753_PWR2 0x15
  29. #define WM8753_PWR3 0x16
  30. #define WM8753_PWR4 0x17
  31. #define WM8753_ID 0x18
  32. #define WM8753_INTPOL 0x19
  33. #define WM8753_INTEN 0x1a
  34. #define WM8753_GPIO1 0x1b
  35. #define WM8753_GPIO2 0x1c
  36. #define WM8753_RESET 0x1f
  37. #define WM8753_RECMIX1 0x20
  38. #define WM8753_RECMIX2 0x21
  39. #define WM8753_LOUTM1 0x22
  40. #define WM8753_LOUTM2 0x23
  41. #define WM8753_ROUTM1 0x24
  42. #define WM8753_ROUTM2 0x25
  43. #define WM8753_MOUTM1 0x26
  44. #define WM8753_MOUTM2 0x27
  45. #define WM8753_LOUT1V 0x28
  46. #define WM8753_ROUT1V 0x29
  47. #define WM8753_LOUT2V 0x2a
  48. #define WM8753_ROUT2V 0x2b
  49. #define WM8753_MOUTV 0x2c
  50. #define WM8753_OUTCTL 0x2d
  51. #define WM8753_ADCIN 0x2e
  52. #define WM8753_INCTL1 0x2f
  53. #define WM8753_INCTL2 0x30
  54. #define WM8753_LINVOL 0x31
  55. #define WM8753_RINVOL 0x32
  56. #define WM8753_MICBIAS 0x33
  57. #define WM8753_CLOCK 0x34
  58. #define WM8753_PLL1CTL1 0x35
  59. #define WM8753_PLL1CTL2 0x36
  60. #define WM8753_PLL1CTL3 0x37
  61. #define WM8753_PLL1CTL4 0x38
  62. #define WM8753_PLL2CTL1 0x39
  63. #define WM8753_PLL2CTL2 0x3a
  64. #define WM8753_PLL2CTL3 0x3b
  65. #define WM8753_PLL2CTL4 0x3c
  66. #define WM8753_BIASCTL 0x3d
  67. #define WM8753_ADCTL2 0x3f
  68. /*
  69. SCLK PA5 SPI1_SCK
  70. SDIN PA7 SPI1_MOSI
  71. CSB PA4 SPI1_NSS
  72. */
  73. #define wm_sclk_0 GPIO_ResetBits(GPIOA,GPIO_Pin_5)
  74. #define wm_sclk_1 GPIO_SetBits(GPIOA,GPIO_Pin_5)
  75. #define wm_sdin_0 GPIO_ResetBits(GPIOA,GPIO_Pin_7)
  76. #define wm_sdin_1 GPIO_SetBits(GPIOA,GPIO_Pin_7)
  77. #define wm_csb_0 GPIO_ResetBits(GPIOA,GPIO_Pin_4)
  78. #define wm_csb_1 GPIO_SetBits(GPIOA,GPIO_Pin_4)
  79. #define DATA_NODE_MAX 5
  80. /* data node for Tx Mode */
  81. struct wm8753_data_node
  82. {
  83. rt_uint16_t *data_ptr;
  84. rt_size_t data_size;
  85. };
  86. struct wm8753_device
  87. {
  88. /* inherit from rt_device */
  89. struct rt_device parent;
  90. /* pcm data list */
  91. struct wm8753_data_node data_list[DATA_NODE_MAX];
  92. rt_uint16_t read_index, put_index;
  93. /* transmitted offset of current data node */
  94. rt_size_t offset;
  95. };
  96. struct wm8753_device wm8753;
  97. static void NVIC_Configuration(void)
  98. {
  99. NVIC_InitTypeDef NVIC_InitStructure;
  100. /* SPI2 IRQ Channel configuration */
  101. NVIC_InitStructure.NVIC_IRQChannel = SPI2_IRQn;
  102. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  103. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  104. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  105. NVIC_Init(&NVIC_InitStructure);
  106. /* DMA1 IRQ Channel configuration */
  107. NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel5_IRQn;
  108. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  109. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  110. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  111. NVIC_Init(&NVIC_InitStructure);
  112. }
  113. static void GPIO_Configuration(void)
  114. {
  115. GPIO_InitTypeDef GPIO_InitStructure;
  116. /* Disable the JTAG interface and enable the SWJ interface */
  117. GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);
  118. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
  119. /* Configure GPIOA 2, 3, 7 */
  120. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_7;
  121. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  122. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
  123. GPIO_Init(GPIOA,&GPIO_InitStructure);
  124. /* Configure SPI2 pins: CK, WS and SD */
  125. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_15;
  126. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  127. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  128. GPIO_Init(GPIOB, &GPIO_InitStructure);
  129. /* MCO configure */
  130. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
  131. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  132. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  133. GPIO_Init(GPIOA,&GPIO_InitStructure);
  134. RCC_MCOConfig(RCC_MCO_HSE);
  135. }
  136. #define SPI2_DR_Address 0x4000380C
  137. static void DMA_Configuration(rt_uint32_t addr, rt_size_t size)
  138. {
  139. DMA_InitTypeDef DMA_InitStructure;
  140. /* DMA1 Channel2 configuration ----------------------------------------------*/
  141. DMA_Cmd(DMA1_Channel5, DISABLE);
  142. DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)SPI2_DR_Address;
  143. DMA_InitStructure.DMA_MemoryBaseAddr = (u32)addr;
  144. DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
  145. DMA_InitStructure.DMA_BufferSize = size;
  146. DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  147. DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
  148. DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
  149. DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
  150. DMA_InitStructure.DMA_Priority = DMA_Priority_Low;
  151. DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
  152. DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
  153. DMA_Init(DMA1_Channel5, &DMA_InitStructure);
  154. /* Enable SPI2 DMA Tx request */
  155. SPI_I2S_DMACmd(SPI2, SPI_I2S_DMAReq_Tx, ENABLE);
  156. DMA_ITConfig(DMA1_Channel5, DMA_IT_TC, ENABLE);
  157. DMA_Cmd(DMA1_Channel5, ENABLE);
  158. }
  159. static void I2S_Configuration(void)
  160. {
  161. I2S_InitTypeDef I2S_InitStructure;
  162. /* I2S peripheral configuration */
  163. I2S_InitStructure.I2S_Standard = I2S_Standard_Phillips;
  164. I2S_InitStructure.I2S_DataFormat = I2S_DataFormat_16b;
  165. I2S_InitStructure.I2S_MCLKOutput = I2S_MCLKOutput_Disable;
  166. I2S_InitStructure.I2S_AudioFreq = I2S_AudioFreq_44k;
  167. I2S_InitStructure.I2S_CPOL = I2S_CPOL_High;// I2S_CPOL_Low
  168. /* I2S2 Master Transmitter to I2S3 Slave Receiver communication -----------*/
  169. /* I2S2 configuration */
  170. I2S_InitStructure.I2S_Mode = I2S_Mode_MasterTx;//I2S_Mode_MasterTx I2S_Mode_SlaveTx
  171. I2S_Init(SPI2, &I2S_InitStructure);
  172. }
  173. void wm8753_send(rt_uint16_t s_data)
  174. {
  175. u8 i;
  176. wm_sclk_0;
  177. for (i=0;i<16;i++)
  178. {
  179. if (s_data & 0x8000)
  180. {
  181. wm_sdin_1;
  182. }
  183. else
  184. {
  185. wm_sdin_0;
  186. }
  187. wm_sclk_1;
  188. s_data <<= 1;
  189. wm_sclk_0;
  190. }
  191. wm_csb_0;
  192. wm_csb_1;
  193. }
  194. static rt_err_t wm8753_init (rt_device_t dev)
  195. {
  196. wm8753_send(31<<9 | 0); // reset
  197. wm8753_send(20<<9 | (1<<7) | 1<<6 | 1<<3 | 1<<2 ); // 打开电源 处理部分
  198. //wm8753_send(21<<9 | 0x1FF);
  199. wm8753_send(22<<9 | 1<<3 | 1<<7 | 1<<8 | 1<<5 | 1<<6 ); // 电源管理 功率输出部分
  200. wm8753_send(23<<9 | 1<<1 | 1 ); // 打开左右调音台电源
  201. /* 设置时钟及PLL  */
  202. #define MCLK1DIV2 0
  203. #define pll1_N 11
  204. #define pll1_K 0x1288CE
  205. #if pll1_K > 0x3FFFFF
  206. #warning MAX bit(21:0)
  207. #endif
  208. wm8753_send(53<<9 | 1<<5 | MCLK1DIV2<<3 | 1<<2 | 1<<1 | 1 );
  209. wm8753_send(54<<9 | pll1_N<<5 | (pll1_K>>18) );
  210. wm8753_send(55<<9 | ( (pll1_K>>9)&0x1FF ) );
  211. wm8753_send(56<<9 | ( (pll1_K)&0x1FF ) );
  212. wm8753_send(52<<9 | 1<<4 | 0<<1 | 0 ); // 打开CLK输出 测试用 可以不设置
  213. /* 设置时钟及PLL  */
  214. /* 设置IIS及DAC */
  215. // wm8753_send(6<<9 | 0<<1 | 0 ); // 48K
  216. wm8753_send(7<<9 | 3<<3 ); // BCLK = MCLK / 8 0:0 1:2 2:4 3:8 4:16
  217. wm8753_send(6<<9 | 16<<1 | 0 ); // 44.1K
  218. wm8753_send(5<<9 | 0x01<<4 | 0x01<<5 | 0x02<<2 | 0x02<<2 | 0x01<<1 | 1); //
  219. wm8753_send(4<<9 | 0<<6 | 2 ); // 6.master IIS
  220. wm8753_send(1<<9 | 0 ); // 关闭DAC静音
  221. /* 设置IIS及DAC */
  222. /* 设置模拟通道及功放输出部分 */
  223. wm8753_send(34<<9 | 1<<8 | 1<<7 | 4<<4 ); // DAC LINE
  224. wm8753_send(36<<9 | 1<<8 | 1<<7 | 4<<4 ); // DAC LINE
  225. wm8753_send(40<<9 | 0<<8 | 1<<7 | 100); // 耳机音量
  226. wm8753_send(41<<9 | 1<<8 | 1<<7 | 100); // 耳机音量
  227. wm8753_send(45<<9 | 1<<2); // 设置ROUT反向
  228. wm8753_send(42<<9 | 1<<8 | 1<<7 | 105 ); //喇叭音量
  229. wm8753_send(43<<9 | 1<<8 | 1<<7 | 105 ); //喇叭音量
  230. /* 设置IIS及DAC */
  231. return RT_EOK;
  232. }
  233. #include <finsh.h>
  234. void vol(int v)
  235. {
  236. wm8753_send(40<<9 | 0<<8 | 1<<7 | v); // 耳机音量
  237. wm8753_send(41<<9 | 1<<8 | 1<<7 | v); // 耳机音量
  238. wm8753_send(42<<9 | 0<<8 | 1<<7 | v); // 耳机音量
  239. wm8753_send(43<<9 | 1<<8 | 1<<7 | v); // 耳机音量
  240. }
  241. FINSH_FUNCTION_EXPORT(vol, set volume)
  242. static rt_err_t wm8753_open(rt_device_t dev, rt_uint16_t oflag)
  243. {
  244. /* enable I2S */
  245. I2S_Cmd(SPI2, ENABLE);
  246. return RT_EOK;
  247. }
  248. static rt_err_t wm8753_close(rt_device_t dev)
  249. {
  250. /* interrupt mode */
  251. if (dev->flag & RT_DEVICE_FLAG_INT_TX)
  252. {
  253. /* Disable the I2S2 */
  254. I2S_Cmd(SPI2, DISABLE);
  255. }
  256. /* remove all data node */
  257. return RT_EOK;
  258. }
  259. static rt_err_t wm8753_control(rt_device_t dev, rt_uint8_t cmd, void *args)
  260. {
  261. /* rate control */
  262. return RT_EOK;
  263. }
  264. static rt_size_t wm8753_write (rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size)
  265. {
  266. struct wm8753_device* device;
  267. struct wm8753_data_node* node;
  268. rt_uint32_t level;
  269. rt_uint16_t next_index;
  270. device = (struct wm8753_device*)dev;
  271. RT_ASSERT(device != RT_NULL);
  272. next_index = device->put_index + 1;
  273. if (next_index >= DATA_NODE_MAX) next_index = 0;
  274. /* check data_list full */
  275. if (next_index == device->read_index)
  276. {
  277. rt_set_errno(-RT_EFULL);
  278. return 0;
  279. }
  280. level = rt_hw_interrupt_disable();
  281. node = &device->data_list[device->put_index];
  282. device->put_index = next_index;
  283. // rt_kprintf("+\n");
  284. /* set node attribute */
  285. node->data_ptr = (rt_uint16_t*)buffer;
  286. node->data_size = size >> 1; /* size is byte unit, convert to half word unit */
  287. next_index = device->read_index + 1;
  288. if (next_index >= DATA_NODE_MAX) next_index = 0;
  289. /* check data list whether is empty */
  290. if (next_index == device->put_index)
  291. {
  292. if (dev->flag & RT_DEVICE_FLAG_INT_TX)
  293. {
  294. device->offset = 0;
  295. /* enable I2S interrupt */
  296. SPI_I2S_ITConfig(SPI2, SPI_I2S_IT_TXE, ENABLE);
  297. }
  298. else if (dev->flag & RT_DEVICE_FLAG_DMA_TX)
  299. {
  300. DMA_Configuration((rt_uint32_t)node->data_ptr, node->data_size);
  301. }
  302. }
  303. rt_hw_interrupt_enable(level);
  304. return size;
  305. }
  306. rt_err_t wm8753_hw_init(void)
  307. {
  308. rt_device_t dev;
  309. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);
  310. RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
  311. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
  312. NVIC_Configuration();
  313. GPIO_Configuration();
  314. I2S_Configuration();
  315. dev = (rt_device_t)&wm8753;
  316. dev->type = RT_Device_Class_Unknown;
  317. dev->rx_indicate = RT_NULL;
  318. dev->tx_complete = RT_NULL;
  319. dev->init = wm8753_init;
  320. dev->open = wm8753_open;
  321. dev->close = wm8753_close;
  322. dev->read = RT_NULL;
  323. dev->write = wm8753_write;
  324. dev->control = wm8753_control;
  325. dev->private = RT_NULL;
  326. /* set read_index and put index to 0 */
  327. wm8753.read_index = 0;
  328. wm8753.put_index = 0;
  329. wm_sclk_0;
  330. wm_sclk_1;
  331. wm_sclk_0;
  332. wm_sdin_0;
  333. wm_sdin_1;
  334. wm_sdin_0;
  335. wm_csb_0;
  336. wm_csb_1;
  337. /* register the device */
  338. return rt_device_register(&wm8753.parent, "snd",
  339. RT_DEVICE_FLAG_WRONLY | RT_DEVICE_FLAG_DMA_TX);
  340. }
  341. void wm8753_isr()
  342. {
  343. struct wm8753_data_node* node;
  344. node = &wm8753.data_list[wm8753.read_index]; /* get current data node */
  345. if (SPI_I2S_GetITStatus(SPI2, SPI_I2S_IT_TXE) == SET)
  346. {
  347. SPI_I2S_SendData(SPI2, node->data_ptr[wm8753.offset++]);
  348. }
  349. if (wm8753.offset == node->data_size)
  350. {
  351. /* move to next node */
  352. rt_uint16_t next_index;
  353. next_index = wm8753.read_index + 1;
  354. if (next_index >= DATA_NODE_MAX) next_index = 0;
  355. /* notify transmitted complete. */
  356. if (wm8753.parent.tx_complete != RT_NULL)
  357. {
  358. wm8753.parent.tx_complete (&wm8753.parent, wm8753.data_list[wm8753.read_index].data_ptr);
  359. rt_kprintf("-\n");
  360. }
  361. wm8753.offset = 0;
  362. wm8753.read_index = next_index;
  363. if (next_index == wm8753.put_index)
  364. {
  365. /* no data on the list, disable I2S interrupt */
  366. SPI_I2S_ITConfig(SPI2, SPI_I2S_IT_TXE, DISABLE);
  367. rt_kprintf("*\n");
  368. }
  369. }
  370. }
  371. void wm8753_dma_isr()
  372. {
  373. /* switch to next buffer */
  374. rt_uint16_t next_index;
  375. void* data_ptr;
  376. next_index = wm8753.read_index + 1;
  377. if (next_index >= DATA_NODE_MAX) next_index = 0;
  378. /* save current data pointer */
  379. data_ptr = wm8753.data_list[wm8753.read_index].data_ptr;
  380. wm8753.read_index = next_index;
  381. if (next_index != wm8753.put_index)
  382. {
  383. /* enable next dma request */
  384. DMA_Configuration((rt_uint32_t)wm8753.data_list[wm8753.read_index].data_ptr,
  385. wm8753.data_list[wm8753.read_index].data_size);
  386. }
  387. else
  388. {
  389. rt_kprintf("*\n");
  390. }
  391. /* notify transmitted complete. */
  392. if (wm8753.parent.tx_complete != RT_NULL)
  393. {
  394. wm8753.parent.tx_complete (&wm8753.parent, data_ptr);
  395. // rt_kprintf("<-0x%08x\n", data_ptr);
  396. }
  397. }