wm8753.c 13 KB

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