wm8978.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. #include <rthw.h>
  2. #include <rtthread.h>
  3. #include "stm32f10x.h"
  4. /*
  5. * WM8978 Driver
  6. */
  7. /* WM8978 register definitions */
  8. #define WM8978_DAC 0x01
  9. #define WM8978_ADC 0x02
  10. #define WM8978_PCM 0x03
  11. #define WM8978_HIFI 0x04
  12. #define WM8978_IOCTL 0x05
  13. #define WM8978_SRATE1 0x06
  14. #define WM8978_SRATE2 0x07
  15. #define WM8978_LDAC 0x08
  16. #define WM8978_RDAC 0x09
  17. #define WM8978_BASS 0x0a
  18. #define WM8978_TREBLE 0x0b
  19. #define WM8978_ALC1 0x0c
  20. #define WM8978_ALC2 0x0d
  21. #define WM8978_ALC3 0x0e
  22. #define WM8978_NGATE 0x0f
  23. #define WM8978_LADC 0x10
  24. #define WM8978_RADC 0x11
  25. #define WM8978_ADCTL1 0x12
  26. #define WM8978_3D 0x13
  27. #define WM8978_PWR1 0x14
  28. #define WM8978_PWR2 0x15
  29. #define WM8978_PWR3 0x16
  30. #define WM8978_PWR4 0x17
  31. #define WM8978_ID 0x18
  32. #define WM8978_INTPOL 0x19
  33. #define WM8978_INTEN 0x1a
  34. #define WM8978_GPIO1 0x1b
  35. #define WM8978_GPIO2 0x1c
  36. #define WM8978_RESET 0x1f
  37. #define WM8978_RECMIX1 0x20
  38. #define WM8978_RECMIX2 0x21
  39. #define WM8978_LOUTM1 0x22
  40. #define WM8978_LOUTM2 0x23
  41. #define WM8978_ROUTM1 0x24
  42. #define WM8978_ROUTM2 0x25
  43. #define WM8978_MOUTM1 0x26
  44. #define WM8978_MOUTM2 0x27
  45. #define WM8978_LOUT1V 0x28
  46. #define WM8978_ROUT1V 0x29
  47. #define WM8978_LOUT2V 0x2a
  48. #define WM8978_ROUT2V 0x2b
  49. #define WM8978_MOUTV 0x2c
  50. #define WM8978_OUTCTL 0x2d
  51. #define WM8978_ADCIN 0x2e
  52. #define WM8978_INCTL1 0x2f
  53. #define WM8978_INCTL2 0x30
  54. #define WM8978_LINVOL 0x31
  55. #define WM8978_RINVOL 0x32
  56. #define WM8978_MICBIAS 0x33
  57. #define WM8978_CLOCK 0x34
  58. #define WM8978_PLL1CTL1 0x35
  59. #define WM8978_PLL1CTL2 0x36
  60. #define WM8978_PLL1CTL3 0x37
  61. #define WM8978_PLL1CTL4 0x38
  62. #define WM8978_PLL2CTL1 0x39
  63. #define WM8978_PLL2CTL2 0x3a
  64. #define WM8978_PLL2CTL3 0x3b
  65. #define WM8978_PLL2CTL4 0x3c
  66. #define WM8978_BIASCTL 0x3d
  67. #define WM8978_ADCTL2 0x3f
  68. /*
  69. SCLK PB7
  70. SDIN PB8
  71. CSB PB9
  72. */
  73. #define wm_sclk_0 GPIO_ResetBits(GPIOB,GPIO_Pin_7)
  74. #define wm_sclk_1 GPIO_SetBits(GPIOB,GPIO_Pin_7)
  75. #define wm_sdin_0 GPIO_ResetBits(GPIOB,GPIO_Pin_8)
  76. #define wm_sdin_1 GPIO_SetBits(GPIOB,GPIO_Pin_8)
  77. #define wm_csb_0 GPIO_ResetBits(GPIOB,GPIO_Pin_9)
  78. #define wm_csb_1 GPIO_SetBits(GPIOB,GPIO_Pin_9)
  79. #define DATA_NODE_MAX 5
  80. /* data node for Tx Mode */
  81. struct wm8978_data_node
  82. {
  83. rt_uint16_t *data_ptr;
  84. rt_size_t data_size;
  85. };
  86. struct wm8978_device
  87. {
  88. /* inherit from rt_device */
  89. struct rt_device parent;
  90. /* pcm data list */
  91. struct wm8978_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 wm8978_device wm8978;
  97. static void NVIC_Configuration(void)
  98. {
  99. NVIC_InitTypeDef NVIC_InitStructure;
  100. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
  101. /* SPI3 IRQ Channel configuration */
  102. NVIC_InitStructure.NVIC_IRQChannel = SPI3_IRQn;
  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 = DMA2_Channel2_IRQn;
  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. /* configure GPIO B 7, 8, 9 */
  118. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9;
  119. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  120. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  121. GPIO_Init(GPIOB,&GPIO_InitStructure);
  122. /* configure GPIO B 3, 5 */
  123. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_5;
  124. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  125. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  126. GPIO_Init(GPIOB, &GPIO_InitStructure);
  127. /* configure GPIO A15 */
  128. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
  129. GPIO_Init(GPIOA, &GPIO_InitStructure);
  130. /* Disable the JTAG interface and enable the SWJ interface */
  131. GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);
  132. }
  133. #define SPI3_DR_Address 0x40003C0C
  134. static void DMA_Configuration(rt_uint32_t addr, rt_size_t size)
  135. {
  136. DMA_InitTypeDef DMA_InitStructure;
  137. /* DMA2 Channel2 configuration ----------------------------------------------*/
  138. DMA_Cmd(DMA2_Channel2, DISABLE);
  139. DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)SPI3_DR_Address;
  140. DMA_InitStructure.DMA_MemoryBaseAddr = (u32)addr;
  141. DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
  142. DMA_InitStructure.DMA_BufferSize = size;
  143. DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  144. DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
  145. DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
  146. DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
  147. DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;
  148. DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
  149. DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
  150. DMA_Init(DMA2_Channel2, &DMA_InitStructure);
  151. /* Enable SPI3 DMA Tx request */
  152. SPI_I2S_DMACmd(SPI3, SPI_I2S_DMAReq_Tx, ENABLE);
  153. DMA_ITConfig(DMA2_Channel2, DMA_IT_TC, ENABLE);
  154. DMA_Cmd(DMA2_Channel2, ENABLE);
  155. }
  156. static void I2S_Configuration(void)
  157. {
  158. I2S_InitTypeDef I2S_InitStructure;
  159. /* I2S peripheral configuration */
  160. I2S_InitStructure.I2S_Standard = I2S_Standard_Phillips;
  161. I2S_InitStructure.I2S_DataFormat = I2S_DataFormat_16b;
  162. I2S_InitStructure.I2S_MCLKOutput = I2S_MCLKOutput_Disable;
  163. I2S_InitStructure.I2S_AudioFreq = I2S_AudioFreq_44k;
  164. I2S_InitStructure.I2S_CPOL = I2S_CPOL_High;// I2S_CPOL_Low
  165. /* I2S2 Master Transmitter to I2S3 Slave Receiver communication -----------*/
  166. /* I2S2 configuration */
  167. I2S_InitStructure.I2S_Mode = I2S_Mode_SlaveTx;
  168. I2S_Init(SPI3, &I2S_InitStructure);
  169. }
  170. void wm_send(rt_uint16_t s_data)
  171. {
  172. u8 i;
  173. wm_csb_0;
  174. wm_sclk_0;
  175. for (i=0;i<16;i++)
  176. {
  177. if (s_data & 0x8000)
  178. {
  179. wm_sdin_1;
  180. }
  181. else
  182. {
  183. wm_sdin_0;
  184. }
  185. wm_sclk_1;
  186. s_data <<= 1;
  187. wm_sclk_0;
  188. }
  189. wm_csb_1;
  190. }
  191. static rt_err_t wm8978_init (rt_device_t dev)
  192. {
  193. wm_send(0<<9 | 0xFF); // reset
  194. /* POWER manager */
  195. wm_send(1<<9 | (0<<8) | (0<<7) | (0<<6) | (0<<5) | (1<<4) | (1<<3) | (1<<2) | 3 );
  196. wm_send(2<<9 | (1<<8) | (1<<7) | (1<<5) | (1<<4) );
  197. wm_send(3<<9 | (0<<8) | (0<<7) | (1<<6) | (1<<5) | (1<<3) | (1<<2) | (1<<1) | 1 );
  198. /* IIS DAC test */
  199. wm_send(4<<9 | 0 | (2<<3) );//IIS 16BIT
  200. #if 0
  201. wm_send(6<<9 | (3<<5) | (3<<2)| 1);//0: slave 1: master | (3<<5) | (3<<2)
  202. wm_send(43<<9 | (1<<4) );//INVROUT2
  203. #else
  204. // 12.288/3 = 4.096M 4.096M / 8 = 512K == (16K *2??)*16?
  205. wm_send(6<<9 | (3<<5) | (3<<2)| 1); //16K MCLK/3/8
  206. wm_send(6<<9 | (0<<5) | (3<<2)| 1); //48K MCLK/1/8
  207. #endif
  208. return RT_EOK;
  209. }
  210. #include <finsh.h>
  211. #if 0
  212. void vol(int v)
  213. {
  214. wm_send(40<<9 | 0<<8 | 1<<7 | v);
  215. wm_send(41<<9 | 1<<8 | 1<<7 | v);
  216. wm_send(42<<9 | 0<<8 | 1<<7 | v);
  217. wm_send(43<<9 | 1<<8 | 1<<7 | v);
  218. }
  219. FINSH_FUNCTION_EXPORT(vol, set volume)
  220. #endif
  221. static rt_err_t wm8978_open(rt_device_t dev, rt_uint16_t oflag)
  222. {
  223. /* enable I2S */
  224. I2S_Cmd(SPI3, ENABLE);
  225. return RT_EOK;
  226. }
  227. static rt_err_t wm8978_close(rt_device_t dev)
  228. {
  229. /* interrupt mode */
  230. if (dev->flag & RT_DEVICE_FLAG_INT_TX)
  231. {
  232. /* Disable the I2S2 */
  233. I2S_Cmd(SPI3, DISABLE);
  234. }
  235. /* remove all data node */
  236. return RT_EOK;
  237. }
  238. static rt_err_t wm8978_control(rt_device_t dev, rt_uint8_t cmd, void *args)
  239. {
  240. /* rate control */
  241. return RT_EOK;
  242. }
  243. static rt_size_t wm8978_write (rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size)
  244. {
  245. struct wm8978_device* device;
  246. struct wm8978_data_node* node;
  247. rt_uint32_t level;
  248. rt_uint16_t next_index;
  249. device = (struct wm8978_device*)dev;
  250. RT_ASSERT(device != RT_NULL);
  251. next_index = device->put_index + 1;
  252. if (next_index >= DATA_NODE_MAX) next_index = 0;
  253. /* check data_list full */
  254. if (next_index == device->read_index)
  255. {
  256. rt_set_errno(-RT_EFULL);
  257. return 0;
  258. }
  259. level = rt_hw_interrupt_disable();
  260. node = &device->data_list[device->put_index];
  261. device->put_index = next_index;
  262. // rt_kprintf("+\n");
  263. /* set node attribute */
  264. node->data_ptr = (rt_uint16_t*)buffer;
  265. node->data_size = size >> 1; /* size is byte unit, convert to half word unit */
  266. next_index = device->read_index + 1;
  267. if (next_index >= DATA_NODE_MAX) next_index = 0;
  268. /* check data list whether is empty */
  269. if (next_index == device->put_index)
  270. {
  271. if (dev->flag & RT_DEVICE_FLAG_INT_TX)
  272. {
  273. device->offset = 0;
  274. /* enable I2S interrupt */
  275. SPI_I2S_ITConfig(SPI3, SPI_I2S_IT_TXE, ENABLE);
  276. }
  277. else if (dev->flag & RT_DEVICE_FLAG_DMA_TX)
  278. {
  279. DMA_Configuration((rt_uint32_t)node->data_ptr, node->data_size);
  280. }
  281. }
  282. rt_hw_interrupt_enable(level);
  283. return size;
  284. }
  285. rt_err_t wm8978_hw_init(void)
  286. {
  287. rt_device_t dev;
  288. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);
  289. RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI3, ENABLE);
  290. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA2, ENABLE);
  291. NVIC_Configuration();
  292. GPIO_Configuration();
  293. I2S_Configuration();
  294. dev = (rt_device_t)&wm8978;
  295. dev->type = RT_Device_Class_Unknown;
  296. dev->rx_indicate = RT_NULL;
  297. dev->tx_complete = RT_NULL;
  298. dev->init = wm8978_init;
  299. dev->open = wm8978_open;
  300. dev->close = wm8978_close;
  301. dev->read = RT_NULL;
  302. dev->write = wm8978_write;
  303. dev->control = wm8978_control;
  304. dev->private = RT_NULL;
  305. /* set read_index and put index to 0 */
  306. wm8978.read_index = 0;
  307. wm8978.put_index = 0;
  308. wm_sclk_0;
  309. wm_sclk_1;
  310. wm_sclk_0;
  311. wm_sdin_0;
  312. wm_sdin_1;
  313. wm_sdin_0;
  314. wm_csb_1;
  315. wm_csb_0;
  316. wm_csb_1;
  317. /* register the device */
  318. return rt_device_register(&wm8978.parent, "snd",
  319. RT_DEVICE_FLAG_WRONLY | RT_DEVICE_FLAG_DMA_TX);
  320. }
  321. void wm8978_isr()
  322. {
  323. struct wm8978_data_node* node;
  324. node = &wm8978.data_list[wm8978.read_index]; /* get current data node */
  325. if (SPI_I2S_GetITStatus(SPI3, SPI_I2S_IT_TXE) == SET)
  326. {
  327. SPI_I2S_SendData(SPI3, node->data_ptr[wm8978.offset++]);
  328. }
  329. if (wm8978.offset == node->data_size)
  330. {
  331. /* move to next node */
  332. rt_uint16_t next_index;
  333. next_index = wm8978.read_index + 1;
  334. if (next_index >= DATA_NODE_MAX) next_index = 0;
  335. /* notify transmitted complete. */
  336. if (wm8978.parent.tx_complete != RT_NULL)
  337. {
  338. wm8978.parent.tx_complete (&wm8978.parent, wm8978.data_list[wm8978.read_index].data_ptr);
  339. rt_kprintf("-\n");
  340. }
  341. wm8978.offset = 0;
  342. wm8978.read_index = next_index;
  343. if (next_index == wm8978.put_index)
  344. {
  345. /* no data on the list, disable I2S interrupt */
  346. SPI_I2S_ITConfig(SPI3, SPI_I2S_IT_TXE, DISABLE);
  347. rt_kprintf("*\n");
  348. }
  349. }
  350. }
  351. void wm8978_dma_isr()
  352. {
  353. /* switch to next buffer */
  354. rt_uint16_t next_index;
  355. void* data_ptr;
  356. next_index = wm8978.read_index + 1;
  357. if (next_index >= DATA_NODE_MAX) next_index = 0;
  358. /* save current data pointer */
  359. data_ptr = wm8978.data_list[wm8978.read_index].data_ptr;
  360. wm8978.read_index = next_index;
  361. if (next_index != wm8978.put_index)
  362. {
  363. /* enable next dma request */
  364. DMA_Configuration((rt_uint32_t)wm8978.data_list[wm8978.read_index].data_ptr,
  365. wm8978.data_list[wm8978.read_index].data_size);
  366. }
  367. else
  368. {
  369. rt_kprintf("*\n");
  370. }
  371. /* notify transmitted complete. */
  372. if (wm8978.parent.tx_complete != RT_NULL)
  373. {
  374. wm8978.parent.tx_complete (&wm8978.parent, data_ptr);
  375. // rt_kprintf("-\n");
  376. }
  377. }