wm8753.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. #include <rthw.h>
  2. #include <rtthread.h>
  3. #include "stm32f10x_lib.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 PA3 SPI1_SCK
  70. SDIN PA7 SPI1_MOSI
  71. CSB PA2 SPI1_NSS
  72. */
  73. #define wm_sclk_0 GPIO_ResetBits(GPIOA,GPIO_Pin_3)
  74. #define wm_sclk_1 GPIO_SetBits(GPIOA,GPIO_Pin_3)
  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_2)
  78. #define wm_csb_1 GPIO_SetBits(GPIOA,GPIO_Pin_2)
  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. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
  101. /* SPI2 IRQ Channel configuration */
  102. NVIC_InitStructure.NVIC_IRQChannel = SPI2_IRQChannel;
  103. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  104. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  105. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  106. NVIC_Init(&NVIC_InitStructure);
  107. /* DMA1 IRQ Channel configuration */
  108. NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel5_IRQChannel;
  109. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  110. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  111. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  112. NVIC_Init(&NVIC_InitStructure);
  113. }
  114. static void GPIO_Configuration(void)
  115. {
  116. GPIO_InitTypeDef GPIO_InitStructure;
  117. /* Disable the JTAG interface and enable the SWJ interface */
  118. GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);
  119. /* Configure GPIOA 2, 3, 7 */
  120. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3 | 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. }
  130. #define SPI2_DR_Address 0x4000380C
  131. static void DMA_Configuration(rt_uint32_t addr, rt_size_t size)
  132. {
  133. DMA_InitTypeDef DMA_InitStructure;
  134. /* DMA1 Channel2 configuration ----------------------------------------------*/
  135. DMA_Cmd(DMA1_Channel5, DISABLE);
  136. DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)SPI2_DR_Address;
  137. DMA_InitStructure.DMA_MemoryBaseAddr = (u32)addr;
  138. DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
  139. DMA_InitStructure.DMA_BufferSize = size;
  140. DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  141. DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
  142. DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
  143. DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
  144. DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;
  145. DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
  146. DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
  147. DMA_Init(DMA1_Channel5, &DMA_InitStructure);
  148. /* Enable SPI2 DMA Tx request */
  149. SPI_I2S_DMACmd(SPI2, SPI_I2S_DMAReq_Tx, ENABLE);
  150. DMA_ITConfig(DMA1_Channel5, DMA_IT_TC, ENABLE);
  151. DMA_Cmd(DMA1_Channel5, ENABLE);
  152. }
  153. static void I2S_Configuration(void)
  154. {
  155. I2S_InitTypeDef I2S_InitStructure;
  156. /* I2S peripheral configuration */
  157. I2S_InitStructure.I2S_Standard = I2S_Standard_Phillips;
  158. I2S_InitStructure.I2S_DataFormat = I2S_DataFormat_16b;
  159. I2S_InitStructure.I2S_MCLKOutput = I2S_MCLKOutput_Disable;
  160. I2S_InitStructure.I2S_AudioFreq = I2S_AudioFreq_44k;
  161. I2S_InitStructure.I2S_CPOL = I2S_CPOL_High;// I2S_CPOL_Low
  162. /* I2S2 Master Transmitter to I2S3 Slave Receiver communication -----------*/
  163. /* I2S2 configuration */
  164. I2S_InitStructure.I2S_Mode = I2S_Mode_MasterTx;//I2S_Mode_MasterTx
  165. I2S_Init(SPI2, &I2S_InitStructure);
  166. }
  167. void wm8753_send(rt_uint16_t s_data)
  168. {
  169. u8 i;
  170. wm_sclk_0;
  171. for(i=0;i<16;i++)
  172. {
  173. if(s_data & 0x8000)
  174. {
  175. wm_sdin_1;
  176. }
  177. else
  178. {
  179. wm_sdin_0;
  180. }
  181. //wde();
  182. wm_sclk_1;
  183. //wde();
  184. s_data <<= 1;
  185. wm_sclk_0;
  186. //wde();
  187. }
  188. wm_csb_0;
  189. //wde();
  190. wm_csb_1;
  191. }
  192. void wm8753_hw_write(rt_uint16_t reg, rt_uint16_t value)
  193. {
  194. rt_uint32_t index;
  195. value = (reg << 9) | value;
  196. wm_sclk_0;
  197. for(index = 0; index < 16; index++)
  198. {
  199. if(value & 0x8000) wm_sdin_1;
  200. else wm_sdin_0;
  201. wm_sclk_1;
  202. value <<= 1;
  203. wm_sclk_0;
  204. }
  205. wm_csb_0;
  206. wm_csb_1;
  207. }
  208. static rt_err_t wm8753_init (rt_device_t dev)
  209. {
  210. wm8753_send(31<<9 | 0); // reset
  211. wm8753_send(20<<9 | (1<<7) | 1<<6 | 1<<3 | 1<<2 ); // 打开电源 处理部分
  212. //wm8753_send(21<<9 | 0x1FF);
  213. wm8753_send(22<<9 | 1<<3 | 1<<7 | 1<<8 | 1<<5 | 1<<6 ); // 电源管理 功率输出部分
  214. wm8753_send(23<<9 | 1<<1 | 1 ); // 打开左右调音台电源
  215. /* 设置时钟及PLL  */
  216. #define MCLK1DIV2 0
  217. #define pll1_N 11 // 12
  218. #define pll1_K 0x1288CE // 0x126E97 //0x126E97
  219. #if pll1_K > 0x3FFFFF
  220. #warning MAX bit(21:0)
  221. #endif
  222. wm8753_send(53<<9 | 1<<5 | MCLK1DIV2<<3 | 1<<2 | 1<<1 | 1 );
  223. wm8753_send(54<<9 | pll1_N<<5 | (pll1_K>>18) );
  224. wm8753_send(55<<9 | ( (pll1_K>>9)&0x1FF ) );
  225. wm8753_send(56<<9 | ( (pll1_K)&0x1FF ) );
  226. wm8753_send(52<<9 | 1<<1 | 1 ); // 打开CLK输出 测试用 可以不设置
  227. /* 设置时钟及PLL  */
  228. /* 设置IIS及DAC */
  229. // wm8753_send(6<<9 | 0<<1 | 0 ); // 48K
  230. wm8753_send(6<<9 | 1<<5 | 0 ); // 44.1K
  231. wm8753_send(4<<9 | 0<<6 | 2 ); // 6.master IIS
  232. wm8753_send(1<<9 | 0 ); // 关闭DAC静音
  233. /* 设置IIS及DAC */
  234. /* 设置模拟通道及功放输出部分 */
  235. wm8753_send(34<<9 | 1<<8 | 1<<7 | 4<<4 ); // DAC LINE
  236. wm8753_send(36<<9 | 1<<8 | 1<<7 | 4<<4 ); // DAC LINE
  237. wm8753_send(40<<9 | 1<<8 | 1<<7 | 110); // 耳机音量
  238. wm8753_send(41<<9 | 1<<8 | 1<<7 | 110); // 耳机音量
  239. wm8753_send(45<<9 | 1<<2); // 设置ROUT反向
  240. wm8753_send(43<<9 | 1<<8 | 1<<7 | 70 ); //喇叭音量
  241. return RT_EOK;
  242. }
  243. #include <finsh.h>
  244. void vol(int v)
  245. {
  246. wm8753_send(40<<9 | 1<<8 | 1<<7 | v); // 耳机音量
  247. wm8753_send(41<<9 | 1<<8 | 1<<7 | v); // 耳机音量
  248. }
  249. FINSH_FUNCTION_EXPORT(vol, set volume)
  250. static rt_err_t wm8753_open(rt_device_t dev, rt_uint16_t oflag)
  251. {
  252. /* enable I2S */
  253. I2S_Cmd(SPI2, ENABLE);
  254. return RT_EOK;
  255. }
  256. static rt_err_t wm8753_close(rt_device_t dev)
  257. {
  258. /* interrupt mode */
  259. if (dev->flag & RT_DEVICE_FLAG_INT_TX)
  260. {
  261. /* Disable the I2S2 */
  262. I2S_Cmd(SPI2, DISABLE);
  263. }
  264. /* remove all data node */
  265. return RT_EOK;
  266. }
  267. static rt_err_t wm8753_control(rt_device_t dev, rt_uint8_t cmd, void *args)
  268. {
  269. /* rate control */
  270. return RT_EOK;
  271. }
  272. static rt_size_t wm8753_write (rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size)
  273. {
  274. struct wm8753_device* device;
  275. struct wm8753_data_node* node;
  276. rt_uint32_t level;
  277. rt_uint16_t next_index;
  278. device = (struct wm8753_device*)dev;
  279. RT_ASSERT(device != RT_NULL);
  280. next_index = device->put_index + 1;
  281. if (next_index >= DATA_NODE_MAX) next_index = 0;
  282. /* check data_list full */
  283. if (next_index == device->read_index)
  284. {
  285. rt_set_errno(-RT_EFULL);
  286. return 0;
  287. }
  288. level = rt_hw_interrupt_disable();
  289. node = &device->data_list[device->put_index];
  290. device->put_index = next_index;
  291. // rt_kprintf("+\n");
  292. /* set node attribute */
  293. node->data_ptr = (rt_uint16_t*)buffer;
  294. node->data_size = size >> 1; /* size is byte unit, convert to half word unit */
  295. #if 0
  296. {
  297. /* sound patch */
  298. rt_uint32_t index;
  299. for (index = 0; index < node->data_size; index ++)
  300. {
  301. ((rt_int16_t*)(node->data_ptr))[index] = (rt_int16_t)(node->data_ptr[index] + 0x8000);
  302. }
  303. }
  304. #endif
  305. next_index = device->read_index + 1;
  306. if (next_index >= DATA_NODE_MAX) next_index = 0;
  307. /* check data list whether is empty */
  308. if (next_index == device->put_index)
  309. {
  310. if (dev->flag & RT_DEVICE_FLAG_INT_TX)
  311. {
  312. device->offset = 0;
  313. /* enable I2S interrupt */
  314. SPI_I2S_ITConfig(SPI2, SPI_I2S_IT_TXE, ENABLE);
  315. }
  316. else if (dev->flag & RT_DEVICE_FLAG_DMA_TX)
  317. {
  318. DMA_Configuration((rt_uint32_t)node->data_ptr, node->data_size);
  319. }
  320. }
  321. rt_hw_interrupt_enable(level);
  322. return size;
  323. }
  324. rt_err_t wm8753_hw_init(void)
  325. {
  326. rt_device_t dev;
  327. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);
  328. RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
  329. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
  330. NVIC_Configuration();
  331. GPIO_Configuration();
  332. I2S_Configuration();
  333. dev = (rt_device_t)&wm8753;
  334. dev->type = RT_Device_Class_Unknown;
  335. dev->rx_indicate = RT_NULL;
  336. dev->tx_complete = RT_NULL;
  337. dev->init = wm8753_init;
  338. dev->open = wm8753_open;
  339. dev->close = wm8753_close;
  340. dev->read = RT_NULL;
  341. dev->write = wm8753_write;
  342. dev->control = wm8753_control;
  343. dev->private = RT_NULL;
  344. /* set read_index and put index to 0 */
  345. wm8753.read_index = 0;
  346. wm8753.put_index = 0;
  347. wm_sclk_0;
  348. wm_sclk_1;
  349. wm_sclk_0;
  350. wm_sdin_0;
  351. wm_sdin_1;
  352. wm_sdin_0;
  353. wm_csb_0;
  354. wm_csb_1;
  355. /* register the device */
  356. return rt_device_register(&wm8753.parent, "snd",
  357. RT_DEVICE_FLAG_WRONLY | RT_DEVICE_FLAG_DMA_TX);
  358. }
  359. void wm8753_isr()
  360. {
  361. struct wm8753_data_node* node;
  362. node = &wm8753.data_list[wm8753.read_index]; /* get current data node */
  363. if (SPI_I2S_GetITStatus(SPI2, SPI_I2S_IT_TXE) == SET)
  364. {
  365. SPI_I2S_SendData(SPI2, node->data_ptr[wm8753.offset++]);
  366. }
  367. if (wm8753.offset == node->data_size)
  368. {
  369. /* move to next node */
  370. rt_uint16_t next_index;
  371. next_index = wm8753.read_index + 1;
  372. if (next_index >= DATA_NODE_MAX) next_index = 0;
  373. /* notify transmitted complete. */
  374. if (wm8753.parent.tx_complete != RT_NULL)
  375. {
  376. wm8753.parent.tx_complete (&wm8753.parent, wm8753.data_list[wm8753.read_index].data_ptr);
  377. rt_kprintf("-\n");
  378. }
  379. wm8753.offset = 0;
  380. wm8753.read_index = next_index;
  381. if (next_index == wm8753.put_index)
  382. {
  383. /* no data on the list, disable I2S interrupt */
  384. SPI_I2S_ITConfig(SPI2, SPI_I2S_IT_TXE, DISABLE);
  385. rt_kprintf("*\n");
  386. }
  387. }
  388. }
  389. void wm8753_dma_isr()
  390. {
  391. /* switch to next buffer */
  392. rt_uint16_t next_index;
  393. void* data_ptr;
  394. next_index = wm8753.read_index + 1;
  395. if (next_index >= DATA_NODE_MAX) next_index = 0;
  396. /* save current data pointer */
  397. data_ptr = wm8753.data_list[wm8753.read_index].data_ptr;
  398. wm8753.read_index = next_index;
  399. if (next_index != wm8753.put_index)
  400. {
  401. /* enable next dma request */
  402. DMA_Configuration((rt_uint32_t)wm8753.data_list[wm8753.read_index].data_ptr,
  403. wm8753.data_list[wm8753.read_index].data_size);
  404. }
  405. else
  406. {
  407. rt_kprintf("*\n");
  408. }
  409. /* notify transmitted complete. */
  410. if (wm8753.parent.tx_complete != RT_NULL)
  411. {
  412. wm8753.parent.tx_complete (&wm8753.parent, data_ptr);
  413. // rt_kprintf("-\n");
  414. }
  415. }