|
@@ -1,37 +1,55 @@
|
|
|
#include <rthw.h>
|
|
|
#include <rtthread.h>
|
|
|
#include "stm32f10x.h"
|
|
|
-
|
|
|
+#include "codec.h"
|
|
|
|
|
|
/*
|
|
|
SCLK PA5 SPI1_SCK
|
|
|
SDIN PA7 SPI1_MOSI
|
|
|
CSB PC5
|
|
|
*/
|
|
|
-#define wm_csb_0 GPIO_ResetBits(GPIOC,GPIO_Pin_5)
|
|
|
-#define wm_csb_1 GPIO_SetBits(GPIOC,GPIO_Pin_5)
|
|
|
+#define CODEC_CSB_PORT GPIOC
|
|
|
+#define CODEC_CSB_PIN GPIO_Pin_5
|
|
|
+#define codec_set_csb() do { CODEC_CSB_PORT->BSRR = CODEC_CSB_PIN; } while (0)
|
|
|
+#define codec_reset_csb() do { CODEC_CSB_PORT->BRR = CODEC_CSB_PIN; } while (0)
|
|
|
+
|
|
|
+void vol(uint16_t v);
|
|
|
|
|
|
#define DATA_NODE_MAX 5
|
|
|
/* data node for Tx Mode */
|
|
|
-struct wm8753_data_node
|
|
|
+struct codec_data_node
|
|
|
{
|
|
|
rt_uint16_t *data_ptr;
|
|
|
rt_size_t data_size;
|
|
|
};
|
|
|
|
|
|
-struct wm8753_device
|
|
|
+struct codec_device
|
|
|
{
|
|
|
/* inherit from rt_device */
|
|
|
struct rt_device parent;
|
|
|
|
|
|
/* pcm data list */
|
|
|
- struct wm8753_data_node data_list[DATA_NODE_MAX];
|
|
|
+ struct codec_data_node data_list[DATA_NODE_MAX];
|
|
|
rt_uint16_t read_index, put_index;
|
|
|
|
|
|
/* transmitted offset of current data node */
|
|
|
rt_size_t offset;
|
|
|
};
|
|
|
-struct wm8753_device wm8753;
|
|
|
+struct codec_device codec;
|
|
|
+
|
|
|
+struct pll_ratio
|
|
|
+{
|
|
|
+ uint8_t n;
|
|
|
+ uint8_t k1;
|
|
|
+ uint16_t k2;
|
|
|
+ uint16_t k3;
|
|
|
+};
|
|
|
+
|
|
|
+static void delay_ms(unsigned int dt)
|
|
|
+{
|
|
|
+ volatile unsigned int u;
|
|
|
+ for (u = 0; u < dt * 30; u++);
|
|
|
+}
|
|
|
|
|
|
static void NVIC_Configuration(void)
|
|
|
{
|
|
@@ -56,332 +74,434 @@ static void GPIO_Configuration(void)
|
|
|
{
|
|
|
GPIO_InitTypeDef GPIO_InitStructure;
|
|
|
|
|
|
- /* Disable the JTAG interface and enable the SWJ interface */
|
|
|
- GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);
|
|
|
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC, ENABLE);
|
|
|
-
|
|
|
- /* PC5 CODEC CS */
|
|
|
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
|
|
|
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
|
|
|
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
|
|
- GPIO_Init(GPIOC,&GPIO_InitStructure);
|
|
|
-
|
|
|
- /* Configure SPI2 pins: CK, WS and SD */
|
|
|
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_15;
|
|
|
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
|
|
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
|
|
|
- GPIO_Init(GPIOB, &GPIO_InitStructure);
|
|
|
-
|
|
|
-#if 0
|
|
|
- /* MCO configure */
|
|
|
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
|
|
|
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
|
|
|
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
|
|
- GPIO_Init(GPIOA,&GPIO_InitStructure);
|
|
|
-
|
|
|
- RCC_MCOConfig(RCC_MCO_HSE);
|
|
|
+ /* Disable the JTAG interface and enable the SWJ interface */
|
|
|
+ GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);
|
|
|
+
|
|
|
+ /* PC5 CODEC CS */
|
|
|
+ GPIO_InitStructure.GPIO_Pin = CODEC_CSB_PIN;
|
|
|
+ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
|
|
|
+ GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
|
|
|
+ GPIO_Init(CODEC_CSB_PORT, &GPIO_InitStructure);
|
|
|
+
|
|
|
+ /* Configure SPI2 pins: CK, WS and SD */
|
|
|
+ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_15;
|
|
|
+ GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
|
|
|
+ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
|
|
|
+ GPIO_Init(GPIOB, &GPIO_InitStructure);
|
|
|
+
|
|
|
+#ifdef CODEC_USE_MCO
|
|
|
+ /* MCO configure */
|
|
|
+ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
|
|
|
+ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
|
|
|
+ GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
|
|
+ GPIO_Init(GPIOA,&GPIO_InitStructure);
|
|
|
+
|
|
|
+ RCC_MCOConfig(RCC_MCO_HSE);
|
|
|
#endif
|
|
|
}
|
|
|
|
|
|
-#define SPI2_DR_Address 0x4000380C
|
|
|
static void DMA_Configuration(rt_uint32_t addr, rt_size_t size)
|
|
|
{
|
|
|
- DMA_InitTypeDef DMA_InitStructure;
|
|
|
-
|
|
|
- /* DMA1 Channel2 configuration ----------------------------------------------*/
|
|
|
- DMA_Cmd(DMA1_Channel5, DISABLE);
|
|
|
- DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)SPI2_DR_Address;
|
|
|
- DMA_InitStructure.DMA_MemoryBaseAddr = (u32)addr;
|
|
|
- DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
|
|
|
- DMA_InitStructure.DMA_BufferSize = size;
|
|
|
- DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
|
|
|
- DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
|
|
|
- DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
|
|
|
- DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
|
|
|
- DMA_InitStructure.DMA_Priority = DMA_Priority_Low;
|
|
|
- DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
|
|
|
- DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
|
|
|
- DMA_Init(DMA1_Channel5, &DMA_InitStructure);
|
|
|
-
|
|
|
- /* Enable SPI2 DMA Tx request */
|
|
|
- SPI_I2S_DMACmd(SPI2, SPI_I2S_DMAReq_Tx, ENABLE);
|
|
|
-
|
|
|
- DMA_ITConfig(DMA1_Channel5, DMA_IT_TC, ENABLE);
|
|
|
- DMA_Cmd(DMA1_Channel5, ENABLE);
|
|
|
+ DMA_InitTypeDef DMA_InitStructure;
|
|
|
+
|
|
|
+ /* DMA1 Channel2 configuration ----------------------------------------------*/
|
|
|
+ DMA_Cmd(DMA1_Channel5, DISABLE);
|
|
|
+ DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)(&(SPI2->DR));
|
|
|
+ DMA_InitStructure.DMA_MemoryBaseAddr = (u32) addr;
|
|
|
+ DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
|
|
|
+ DMA_InitStructure.DMA_BufferSize = size;
|
|
|
+ DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
|
|
|
+ DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
|
|
|
+ DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
|
|
|
+ DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
|
|
|
+ DMA_InitStructure.DMA_Priority = DMA_Priority_High;
|
|
|
+ DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
|
|
|
+ DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
|
|
|
+ DMA_Init(DMA1_Channel5, &DMA_InitStructure);
|
|
|
+
|
|
|
+ /* Enable SPI2 DMA Tx request */
|
|
|
+ SPI_I2S_DMACmd(SPI2, SPI_I2S_DMAReq_Tx, ENABLE);
|
|
|
+
|
|
|
+ DMA_ITConfig(DMA1_Channel5, DMA_IT_TC, ENABLE);
|
|
|
+ DMA_Cmd(DMA1_Channel5, ENABLE);
|
|
|
}
|
|
|
|
|
|
static void I2S_Configuration(void)
|
|
|
{
|
|
|
- I2S_InitTypeDef I2S_InitStructure;
|
|
|
-
|
|
|
- /* I2S peripheral configuration */
|
|
|
- I2S_InitStructure.I2S_Standard = I2S_Standard_Phillips;
|
|
|
- I2S_InitStructure.I2S_DataFormat = I2S_DataFormat_16b;
|
|
|
- I2S_InitStructure.I2S_MCLKOutput = I2S_MCLKOutput_Disable;
|
|
|
- I2S_InitStructure.I2S_AudioFreq = I2S_AudioFreq_44k;
|
|
|
- I2S_InitStructure.I2S_CPOL = I2S_CPOL_High;// I2S_CPOL_Low
|
|
|
-
|
|
|
- /* I2S2 Master Transmitter to I2S3 Slave Receiver communication -----------*/
|
|
|
- /* I2S2 configuration */
|
|
|
- I2S_InitStructure.I2S_Mode = I2S_Mode_MasterTx;//I2S_Mode_MasterTx I2S_Mode_SlaveTx
|
|
|
- I2S_Init(SPI2, &I2S_InitStructure);
|
|
|
+ I2S_InitTypeDef I2S_InitStructure;
|
|
|
+
|
|
|
+ /* I2S peripheral configuration */
|
|
|
+ I2S_InitStructure.I2S_Standard = I2S_Standard_Phillips;
|
|
|
+ I2S_InitStructure.I2S_DataFormat = I2S_DataFormat_16b;
|
|
|
+ I2S_InitStructure.I2S_MCLKOutput = I2S_MCLKOutput_Disable;
|
|
|
+ I2S_InitStructure.I2S_AudioFreq = I2S_AudioFreq_44k;
|
|
|
+ I2S_InitStructure.I2S_CPOL = I2S_CPOL_High; // I2S_CPOL_Low
|
|
|
+
|
|
|
+ /* I2S2 Master Transmitter to I2S3 Slave Receiver communication -----------*/
|
|
|
+ /* I2S2 configuration */
|
|
|
+ I2S_InitStructure.I2S_Mode = I2S_Mode_MasterTx; //I2S_Mode_MasterTx I2S_Mode_SlaveTx
|
|
|
+ I2S_Init(SPI2, &I2S_InitStructure);
|
|
|
}
|
|
|
|
|
|
-unsigned char SPI_WriteByte(unsigned char data)
|
|
|
+uint8_t SPI_WriteByte(unsigned char data)
|
|
|
{
|
|
|
- unsigned char Data = 0;
|
|
|
+ //Wait until the transmit buffer is empty
|
|
|
+ while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
|
|
|
+ // Send the byte
|
|
|
+ SPI_I2S_SendData(SPI1, data);
|
|
|
+
|
|
|
+ //Wait until a data is received
|
|
|
+ while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
|
|
|
+ // Get the received data
|
|
|
+ data = SPI_I2S_ReceiveData(SPI1);
|
|
|
+
|
|
|
+ // Return the shifted data
|
|
|
+ return data;
|
|
|
+}
|
|
|
|
|
|
- //Wait until the transmit buffer is empty
|
|
|
- while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_TXE)==RESET);
|
|
|
- // Send the byte
|
|
|
- SPI_I2S_SendData(SPI1,data);
|
|
|
+static void codec_send(rt_uint16_t s_data)
|
|
|
+{
|
|
|
+ codec_reset_csb();
|
|
|
+ SPI_WriteByte((s_data >> 8) & 0xFF);
|
|
|
+ SPI_WriteByte(s_data & 0xFF);
|
|
|
+ codec_set_csb();
|
|
|
+}
|
|
|
|
|
|
- //Wait until a data is received
|
|
|
- while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_RXNE)==RESET);
|
|
|
- // Get the received data
|
|
|
- Data = SPI_I2S_ReceiveData(SPI1);
|
|
|
+static rt_err_t codec_init(rt_device_t dev)
|
|
|
+{
|
|
|
+ codec_send(REG_SOFTWARE_RESET);
|
|
|
+
|
|
|
+ // 1.5x boost power up sequence.
|
|
|
+ // Mute all outputs.
|
|
|
+ codec_send(REG_LOUT1_VOL | LOUT1MUTE);
|
|
|
+ codec_send(REG_ROUT1_VOL | ROUT1MUTE);
|
|
|
+ codec_send(REG_LOUT2_VOL | LOUT2MUTE);
|
|
|
+ codec_send(REG_ROUT2_VOL | ROUT2MUTE);
|
|
|
+ // Enable unused output chosen from L/ROUT2, OUT3 or OUT4.
|
|
|
+ codec_send(REG_POWER_MANAGEMENT3 | OUT4EN);
|
|
|
+ // Set BUFDCOPEN=1 and BUFIOEN=1 in register R1
|
|
|
+ codec_send(REG_POWER_MANAGEMENT1 | BUFDCOPEN | BUFIOEN);
|
|
|
+ // Set SPKBOOST=1 in register R49.
|
|
|
+ codec_send(REG_OUTPUT | SPKBOOST);
|
|
|
+ // Set VMIDSEL[1:0] to required value in register R1.
|
|
|
+ codec_send(REG_POWER_MANAGEMENT1 | BUFDCOPEN | BUFIOEN | VMIDSEL_75K);
|
|
|
+ // Wait for VMID supply to settle.
|
|
|
+ delay_ms(750);
|
|
|
+ // Set L/RMIXEN=1 and DACENL/R=1 in register R3.
|
|
|
+ codec_send(REG_POWER_MANAGEMENT3 | LMIXEN | RMIXEN | DACENL | DACENR);
|
|
|
+ // Set BIASEN=1 in register R1.
|
|
|
+ codec_send(REG_POWER_MANAGEMENT1 | BUFDCOPEN | BUFIOEN | VMIDSEL_75K | BIASEN);
|
|
|
+ // Set L/ROUT2EN=1 in register R3.
|
|
|
+ codec_send(REG_POWER_MANAGEMENT3 | LMIXEN | RMIXEN | DACENL | DACENR | LOUT2EN | ROUT2EN);
|
|
|
+ // Enable other mixers as required.
|
|
|
+ // Enable other outputs as required.
|
|
|
+ codec_send(REG_POWER_MANAGEMENT2 | LOUT1EN | ROUT1EN | BOOSTENL | BOOSTENR | INPPGAENL | INPPGAENR);
|
|
|
+
|
|
|
+ // Digital inferface setup.
|
|
|
+ codec_send(REG_AUDIO_INTERFACE | BCP_NORMAL | LRP_NORMAL | WL_16BITS | FMT_I2S);
|
|
|
+
|
|
|
+ // PLL setup.
|
|
|
+ // fs = 44.1KHz / 256fs = 11.2896MHz
|
|
|
+ // F_PLL = 11.2896MHz * 4 * 2 = 90.3168MHz
|
|
|
+ // R = 90.3168MHz / 12.288MHz = 7.35
|
|
|
+ // PLL_N = 7
|
|
|
+ // PLL_K = 5872026
|
|
|
+ codec_send(REG_PLL_N | 7);
|
|
|
+ codec_send(REG_PLL_K1 | 0x16);
|
|
|
+ codec_send(REG_PLL_K2 | 0xCC);
|
|
|
+ codec_send(REG_PLL_K3 | 0x19A);
|
|
|
+ codec_send(REG_POWER_MANAGEMENT1 | BUFDCOPEN | BUFIOEN | VMIDSEL_75K | BIASEN | PLLEN);
|
|
|
+ codec_send(REG_CLOCK_GEN | CLKSEL_PLL | MCLK_DIV2);
|
|
|
+
|
|
|
+ // Enable DAC 128x oversampling.
|
|
|
+ codec_send(REG_DAC | DACOSR128);
|
|
|
+
|
|
|
+ // Set LOUT2/ROUT2 in BTL operation.
|
|
|
+ codec_send(REG_BEEP | INVROUT2);
|
|
|
+
|
|
|
+ // Set output volume to -22dB.
|
|
|
+ vol(35);
|
|
|
+
|
|
|
+ return RT_EOK;
|
|
|
+}
|
|
|
+
|
|
|
+// Exported functions
|
|
|
+#include <finsh.h>
|
|
|
|
|
|
- // Return the shifted data
|
|
|
- return Data;
|
|
|
+void vol(uint16_t v)
|
|
|
+{
|
|
|
+ v = (v & VOL_MASK) << VOL_POS;
|
|
|
+ codec_send(REG_LOUT1_VOL | v);
|
|
|
+ codec_send(REG_ROUT1_VOL | HPVU | v);
|
|
|
+ codec_send(REG_LOUT2_VOL | v);
|
|
|
+ codec_send(REG_ROUT2_VOL | SPKVU | v);
|
|
|
}
|
|
|
|
|
|
-void wm8753_send(rt_uint16_t s_data)
|
|
|
+void eq1(uint8_t freq, uint8_t gain, uint8_t mode)
|
|
|
{
|
|
|
- wm_csb_0;
|
|
|
- SPI_WriteByte( (s_data>>8)&0xFF );
|
|
|
- SPI_WriteByte( s_data&0xFF );
|
|
|
- wm_csb_1;
|
|
|
+ codec_send(REG_EQ1 | ((freq & EQC_MASK) << EQC_POS) | ((gain & EQG_MASK) << EQG_POS) | (mode ? EQ3DMODE_DAC : EQ3DMODE_ADC));
|
|
|
}
|
|
|
|
|
|
-static rt_err_t wm8753_init (rt_device_t dev)
|
|
|
+void eq2(uint8_t freq, uint8_t gain, uint8_t bw)
|
|
|
{
|
|
|
- wm8753_send(0<<9 | 0xFF); // reset
|
|
|
-
|
|
|
- /* POWER manager */
|
|
|
- wm8753_send(1<<9 | (1<<8) | (0<<7) | (0<<6) | (0<<5) | (1<<4) | (1<<3) | (1<<2) | 2 );//电源设置
|
|
|
- wm8753_send(2<<9 | (1<<8) | (1<<7) | (1<<5) | (1<<4) | (1<<3) | (1<<2) ); // 打开电源 耳机输出
|
|
|
- wm8753_send(3<<9 | (0<<8) | (0<<7) | (1<<6) | (1<<5) | (1<<3) | (1<<2) | (1<<1) | 1 ); // 喇叭输出和DAC
|
|
|
-
|
|
|
- /* IIS DAC test */
|
|
|
- wm8753_send(4<<9 | (0<<7) | (2<<3) );//IIS 16BIT
|
|
|
- // 12.288/3/8=512K
|
|
|
- wm8753_send(6<<9 | (0<<5) | (3<<2)| 0);//0: slave 1: master | (3<<5) | (3<<2)
|
|
|
- wm8753_send(43<<9 | (1<<4) );//INVROUT2
|
|
|
-
|
|
|
- /* 设置初始化音量 */
|
|
|
- wm8753_send(52<<9 | (1<<8) | (1<<7) | 35 ); // LOUT1 0-57-63
|
|
|
- wm8753_send(53<<9 | (1<<8) | (1<<7) | 35 ); // ROUT1 0-57-63
|
|
|
- wm8753_send(54<<9 | (1<<8) | (1<<7) | 35 ); // LOUT2 0-57-63
|
|
|
- wm8753_send(55<<9 | (1<<8) | (1<<7) | 35 ); // ROUT2 0-57-63
|
|
|
-
|
|
|
-#if 1
|
|
|
- /* LINE IN test */
|
|
|
- wm8753_send(47<<9 | (1<<8) | (1<<4) ); //L LINE_IN VOL (6:4)输入增益: 0-关 1-12DB 2-9DB 5-0db 7+6DB
|
|
|
- wm8753_send(48<<9 | (1<<8) | (1<<4) ); //R LINE_IN VOL (6:4)输入增益: 0-关 1-12DB 2-9DB 5-0db 7+6DB
|
|
|
- wm8753_send(50<<9 | (5<<2) | (1<<1) | (1<<0) );//打开左监听 (4:2)增益 0-关 1-12DB 2-9DB 5-0db 7+6DB
|
|
|
- wm8753_send(51<<9 | (5<<2) | (1<<1) | (1<<0) );//打开右监听 (4:2)增益 0-关 1-12DB 2-9DB 5-0db 7+6DB
|
|
|
-
|
|
|
- /* MIC test */
|
|
|
- wm8753_send(44<<9 | (1<<8) | (1<<5) | (1<<4) | (0<<2) | (1<<1) | (1<<0) );//MIC输入选择
|
|
|
- wm8753_send(45<<9 | 50);//16-0 63-35
|
|
|
- wm8753_send(46<<9 | 50);//16-0 63-35
|
|
|
-#endif
|
|
|
+ codec_send(REG_EQ2 | ((freq & EQC_MASK) << EQC_POS) | ((gain & EQG_MASK) << EQG_POS) | (bw ? EQ2BW_WIDE : EQ2BW_NARROW));
|
|
|
+}
|
|
|
|
|
|
- return RT_EOK;
|
|
|
+void eq3(uint8_t freq, uint8_t gain, uint8_t bw)
|
|
|
+{
|
|
|
+ codec_send(REG_EQ3 | ((freq & EQC_MASK) << EQC_POS) | ((gain & EQG_MASK) << EQG_POS) | (bw ? EQ3BW_WIDE : EQ3BW_NARROW));
|
|
|
}
|
|
|
|
|
|
-#include <finsh.h>
|
|
|
-//0~57~63
|
|
|
-void vol(int v)
|
|
|
+void eq4(uint8_t freq, uint8_t gain, uint8_t bw)
|
|
|
{
|
|
|
- wm8753_send(52<<9 | (0<<8) | (1<<7) | v ); // LOUT1 0-57-63
|
|
|
- wm8753_send(53<<9 | (1<<8) | (1<<7) | v ); // ROUT1 0-57-63
|
|
|
- wm8753_send(54<<9 | (0<<8) | (1<<7) | v ); // LOUT2 0-57-63
|
|
|
- wm8753_send(55<<9 | (1<<8) | (1<<7) | v ); // ROUT2 0-57-63
|
|
|
+ codec_send(REG_EQ4 | ((freq & EQC_MASK) << EQC_POS) | ((gain & EQG_MASK) << EQG_POS) | (bw ? EQ4BW_WIDE : EQ4BW_NARROW));
|
|
|
}
|
|
|
-FINSH_FUNCTION_EXPORT(vol, set volume)
|
|
|
|
|
|
-static rt_err_t wm8753_open(rt_device_t dev, rt_uint16_t oflag)
|
|
|
+void eq5(uint8_t freq, uint8_t gain)
|
|
|
{
|
|
|
- /* enable I2S */
|
|
|
- I2S_Cmd(SPI2, ENABLE);
|
|
|
+ codec_send(REG_EQ2 | ((freq & EQC_MASK) << EQC_POS) | ((gain & EQG_MASK) << EQG_POS));
|
|
|
+}
|
|
|
|
|
|
- return RT_EOK;
|
|
|
+void eq3d(uint8_t depth)
|
|
|
+{
|
|
|
+ codec_send(REG_3D | ((depth & DEPTH3D_MASK) << DEPTH3D_POS));
|
|
|
}
|
|
|
|
|
|
-static rt_err_t wm8753_close(rt_device_t dev)
|
|
|
+void sample_rate(uint8_t sr)
|
|
|
{
|
|
|
- /* interrupt mode */
|
|
|
- if (dev->flag & RT_DEVICE_FLAG_INT_TX)
|
|
|
- {
|
|
|
- /* Disable the I2S2 */
|
|
|
- I2S_Cmd(SPI2, DISABLE);
|
|
|
- }
|
|
|
+ if (sr == 44)
|
|
|
+ {
|
|
|
+ codec_send(REG_ADDITIONAL | SR_48KHZ);
|
|
|
+ codec_send(REG_CLOCK_GEN | CLKSEL_PLL | MCLK_DIV2);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ switch (sr)
|
|
|
+ {
|
|
|
+ case 8:
|
|
|
+ codec_send(REG_ADDITIONAL | SR_8KHZ);
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 12:
|
|
|
+ codec_send(REG_ADDITIONAL | SR_12KHZ);
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 16:
|
|
|
+ codec_send(REG_ADDITIONAL | SR_16KHZ);
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 24:
|
|
|
+ codec_send(REG_ADDITIONAL | SR_24KHZ);
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 32:
|
|
|
+ codec_send(REG_ADDITIONAL | SR_32KHZ);
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 48:
|
|
|
+ codec_send(REG_ADDITIONAL | SR_48KHZ);
|
|
|
+ break;
|
|
|
+
|
|
|
+ default:
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ codec_send(REG_CLOCK_GEN | CLKSEL_MCLK | MCLK_DIV1);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+FINSH_FUNCTION_EXPORT(vol, Set volume);
|
|
|
+FINSH_FUNCTION_EXPORT(eq1, Set EQ1(Cut-off, Gain, Mode));
|
|
|
+FINSH_FUNCTION_EXPORT(eq2, Set EQ2(Center, Gain, Bandwidth));
|
|
|
+FINSH_FUNCTION_EXPORT(eq3, Set EQ3(Center, Gain, Bandwidth));
|
|
|
+FINSH_FUNCTION_EXPORT(eq4, Set EQ4(Center, Gain, Bandwidth));
|
|
|
+FINSH_FUNCTION_EXPORT(eq5, Set EQ5(Cut-off, Gain));
|
|
|
+FINSH_FUNCTION_EXPORT(eq3d, Set 3D(Depth));
|
|
|
+FINSH_FUNCTION_EXPORT(sample_rate, Set sample rate);
|
|
|
|
|
|
- /* remove all data node */
|
|
|
+static rt_err_t codec_open(rt_device_t dev, rt_uint16_t oflag)
|
|
|
+{
|
|
|
+ /* enable I2S */
|
|
|
+ I2S_Cmd(SPI2, ENABLE);
|
|
|
|
|
|
- return RT_EOK;
|
|
|
+ return RT_EOK;
|
|
|
}
|
|
|
|
|
|
-static rt_err_t wm8753_control(rt_device_t dev, rt_uint8_t cmd, void *args)
|
|
|
+static rt_err_t codec_close(rt_device_t dev)
|
|
|
{
|
|
|
- /* rate control */
|
|
|
- return RT_EOK;
|
|
|
+ /* interrupt mode */
|
|
|
+ if (dev->flag & RT_DEVICE_FLAG_INT_TX)
|
|
|
+ {
|
|
|
+ /* Disable the I2S2 */
|
|
|
+ I2S_Cmd(SPI2, DISABLE);
|
|
|
+ }
|
|
|
+
|
|
|
+ /* remove all data node */
|
|
|
+
|
|
|
+ return RT_EOK;
|
|
|
}
|
|
|
|
|
|
-static rt_size_t wm8753_write (rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size)
|
|
|
+static rt_err_t codec_control(rt_device_t dev, rt_uint8_t cmd, void *args)
|
|
|
{
|
|
|
- struct wm8753_device* device;
|
|
|
- struct wm8753_data_node* node;
|
|
|
- rt_uint32_t level;
|
|
|
- rt_uint16_t next_index;
|
|
|
-
|
|
|
- device = (struct wm8753_device*)dev;
|
|
|
- RT_ASSERT(device != RT_NULL);
|
|
|
-
|
|
|
- next_index = device->put_index + 1;
|
|
|
- if (next_index >= DATA_NODE_MAX) next_index = 0;
|
|
|
-
|
|
|
- /* check data_list full */
|
|
|
- if (next_index == device->read_index)
|
|
|
- {
|
|
|
- rt_set_errno(-RT_EFULL);
|
|
|
- return 0;
|
|
|
- }
|
|
|
-
|
|
|
- level = rt_hw_interrupt_disable();
|
|
|
- node = &device->data_list[device->put_index];
|
|
|
- device->put_index = next_index;
|
|
|
-
|
|
|
- // rt_kprintf("+\n");
|
|
|
- /* set node attribute */
|
|
|
- node->data_ptr = (rt_uint16_t*)buffer;
|
|
|
- node->data_size = size >> 1; /* size is byte unit, convert to half word unit */
|
|
|
-
|
|
|
- next_index = device->read_index + 1;
|
|
|
- if (next_index >= DATA_NODE_MAX) next_index = 0;
|
|
|
-
|
|
|
- /* check data list whether is empty */
|
|
|
- if (next_index == device->put_index)
|
|
|
- {
|
|
|
- if (dev->flag & RT_DEVICE_FLAG_INT_TX)
|
|
|
- {
|
|
|
- device->offset = 0;
|
|
|
- /* enable I2S interrupt */
|
|
|
- SPI_I2S_ITConfig(SPI2, SPI_I2S_IT_TXE, ENABLE);
|
|
|
- }
|
|
|
- else if (dev->flag & RT_DEVICE_FLAG_DMA_TX)
|
|
|
- {
|
|
|
- DMA_Configuration((rt_uint32_t)node->data_ptr, node->data_size);
|
|
|
- }
|
|
|
- }
|
|
|
- rt_hw_interrupt_enable(level);
|
|
|
-
|
|
|
- return size;
|
|
|
+ /* rate control */
|
|
|
+ return RT_EOK;
|
|
|
}
|
|
|
|
|
|
-rt_err_t wm8753_hw_init(void)
|
|
|
+static rt_size_t codec_write(rt_device_t dev, rt_off_t pos,
|
|
|
+ const void* buffer, rt_size_t size)
|
|
|
{
|
|
|
- rt_device_t dev;
|
|
|
-
|
|
|
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);
|
|
|
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
|
|
|
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
|
|
|
-
|
|
|
- NVIC_Configuration();
|
|
|
- GPIO_Configuration();
|
|
|
- I2S_Configuration();
|
|
|
-
|
|
|
- dev = (rt_device_t)&wm8753;
|
|
|
- dev->type = RT_Device_Class_Unknown;
|
|
|
- dev->rx_indicate = RT_NULL;
|
|
|
- dev->tx_complete = RT_NULL;
|
|
|
- dev->init = wm8753_init;
|
|
|
- dev->open = wm8753_open;
|
|
|
- dev->close = wm8753_close;
|
|
|
- dev->read = RT_NULL;
|
|
|
- dev->write = wm8753_write;
|
|
|
- dev->control = wm8753_control;
|
|
|
- dev->private = RT_NULL;
|
|
|
-
|
|
|
- /* set read_index and put index to 0 */
|
|
|
- wm8753.read_index = 0;
|
|
|
- wm8753.put_index = 0;
|
|
|
-
|
|
|
- /* unselect */
|
|
|
- wm_csb_1;
|
|
|
-
|
|
|
- /* register the device */
|
|
|
- return rt_device_register(&wm8753.parent, "snd",
|
|
|
- RT_DEVICE_FLAG_WRONLY | RT_DEVICE_FLAG_DMA_TX);
|
|
|
+ struct codec_device* device;
|
|
|
+ struct codec_data_node* node;
|
|
|
+ rt_uint32_t level;
|
|
|
+ rt_uint16_t next_index;
|
|
|
+
|
|
|
+ device = (struct codec_device*) dev;
|
|
|
+ RT_ASSERT(device != RT_NULL);
|
|
|
+
|
|
|
+ next_index = device->put_index + 1;
|
|
|
+ if (next_index >= DATA_NODE_MAX)
|
|
|
+ next_index = 0;
|
|
|
+
|
|
|
+ /* check data_list full */
|
|
|
+ if (next_index == device->read_index)
|
|
|
+ {
|
|
|
+ rt_set_errno(-RT_EFULL);
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ level = rt_hw_interrupt_disable();
|
|
|
+ node = &device->data_list[device->put_index];
|
|
|
+ device->put_index = next_index;
|
|
|
+
|
|
|
+ // rt_kprintf("+\n");
|
|
|
+ /* set node attribute */
|
|
|
+ node->data_ptr = (rt_uint16_t*) buffer;
|
|
|
+ node->data_size = size >> 1; /* size is byte unit, convert to half word unit */
|
|
|
+
|
|
|
+ next_index = device->read_index + 1;
|
|
|
+ if (next_index >= DATA_NODE_MAX)
|
|
|
+ next_index = 0;
|
|
|
+
|
|
|
+ /* check data list whether is empty */
|
|
|
+ if (next_index == device->put_index)
|
|
|
+ {
|
|
|
+ if (dev->flag & RT_DEVICE_FLAG_INT_TX)
|
|
|
+ {
|
|
|
+ device->offset = 0;
|
|
|
+ /* enable I2S interrupt */
|
|
|
+ SPI_I2S_ITConfig(SPI2, SPI_I2S_IT_TXE, ENABLE);
|
|
|
+ }
|
|
|
+ else if (dev->flag & RT_DEVICE_FLAG_DMA_TX)
|
|
|
+ {
|
|
|
+ DMA_Configuration((rt_uint32_t) node->data_ptr, node->data_size);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ rt_hw_interrupt_enable(level);
|
|
|
+
|
|
|
+ return size;
|
|
|
}
|
|
|
|
|
|
-void wm8753_isr()
|
|
|
+rt_err_t codec_hw_init(void)
|
|
|
{
|
|
|
- struct wm8753_data_node* node;
|
|
|
- node = &wm8753.data_list[wm8753.read_index]; /* get current data node */
|
|
|
-
|
|
|
- if (SPI_I2S_GetITStatus(SPI2, SPI_I2S_IT_TXE) == SET)
|
|
|
- {
|
|
|
- SPI_I2S_SendData(SPI2, node->data_ptr[wm8753.offset++]);
|
|
|
- }
|
|
|
-
|
|
|
- if (wm8753.offset == node->data_size)
|
|
|
- {
|
|
|
- /* move to next node */
|
|
|
- rt_uint16_t next_index;
|
|
|
-
|
|
|
- next_index = wm8753.read_index + 1;
|
|
|
- if (next_index >= DATA_NODE_MAX) next_index = 0;
|
|
|
-
|
|
|
- /* notify transmitted complete. */
|
|
|
- if (wm8753.parent.tx_complete != RT_NULL)
|
|
|
- {
|
|
|
- wm8753.parent.tx_complete (&wm8753.parent, wm8753.data_list[wm8753.read_index].data_ptr);
|
|
|
- rt_kprintf("-\n");
|
|
|
- }
|
|
|
-
|
|
|
- wm8753.offset = 0;
|
|
|
- wm8753.read_index = next_index;
|
|
|
- if (next_index == wm8753.put_index)
|
|
|
- {
|
|
|
- /* no data on the list, disable I2S interrupt */
|
|
|
- SPI_I2S_ITConfig(SPI2, SPI_I2S_IT_TXE, DISABLE);
|
|
|
-
|
|
|
- rt_kprintf("*\n");
|
|
|
- }
|
|
|
- }
|
|
|
+ rt_device_t dev;
|
|
|
+
|
|
|
+ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC, ENABLE);
|
|
|
+ RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
|
|
|
+ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
|
|
|
+
|
|
|
+ NVIC_Configuration();
|
|
|
+ GPIO_Configuration();
|
|
|
+ I2S_Configuration();
|
|
|
+
|
|
|
+ dev = (rt_device_t) &codec;
|
|
|
+ dev->type = RT_Device_Class_Unknown;
|
|
|
+ dev->rx_indicate = RT_NULL;
|
|
|
+ dev->tx_complete = RT_NULL;
|
|
|
+ dev->init = codec_init;
|
|
|
+ dev->open = codec_open;
|
|
|
+ dev->close = codec_close;
|
|
|
+ dev->read = RT_NULL;
|
|
|
+ dev->write = codec_write;
|
|
|
+ dev->control = codec_control;
|
|
|
+ dev->private = RT_NULL;
|
|
|
+
|
|
|
+ /* set read_index and put index to 0 */
|
|
|
+ codec.read_index = 0;
|
|
|
+ codec.put_index = 0;
|
|
|
+
|
|
|
+ /* unselect */
|
|
|
+ codec_set_csb();
|
|
|
+
|
|
|
+ /* register the device */
|
|
|
+ return rt_device_register(&codec.parent, "snd", RT_DEVICE_FLAG_WRONLY | RT_DEVICE_FLAG_DMA_TX);
|
|
|
}
|
|
|
|
|
|
-void wm8753_dma_isr()
|
|
|
+void codec_isr()
|
|
|
{
|
|
|
- /* switch to next buffer */
|
|
|
- rt_uint16_t next_index;
|
|
|
- void* data_ptr;
|
|
|
-
|
|
|
- next_index = wm8753.read_index + 1;
|
|
|
- if (next_index >= DATA_NODE_MAX) next_index = 0;
|
|
|
-
|
|
|
- /* save current data pointer */
|
|
|
- data_ptr = wm8753.data_list[wm8753.read_index].data_ptr;
|
|
|
-
|
|
|
- wm8753.read_index = next_index;
|
|
|
- if (next_index != wm8753.put_index)
|
|
|
- {
|
|
|
- /* enable next dma request */
|
|
|
- DMA_Configuration((rt_uint32_t)wm8753.data_list[wm8753.read_index].data_ptr,
|
|
|
- wm8753.data_list[wm8753.read_index].data_size);
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- rt_kprintf("*\n");
|
|
|
- }
|
|
|
-
|
|
|
- /* notify transmitted complete. */
|
|
|
- if (wm8753.parent.tx_complete != RT_NULL)
|
|
|
- {
|
|
|
- wm8753.parent.tx_complete (&wm8753.parent, data_ptr);
|
|
|
- // rt_kprintf("-\n");
|
|
|
- }
|
|
|
+ struct codec_data_node* node;
|
|
|
+ node = &codec.data_list[codec.read_index]; /* get current data node */
|
|
|
+
|
|
|
+ if (SPI_I2S_GetITStatus(SPI2, SPI_I2S_IT_TXE) == SET)
|
|
|
+ {
|
|
|
+ SPI_I2S_SendData(SPI2, node->data_ptr[codec.offset++]);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (codec.offset == node->data_size)
|
|
|
+ {
|
|
|
+ /* move to next node */
|
|
|
+ rt_uint16_t next_index;
|
|
|
+
|
|
|
+ next_index = codec.read_index + 1;
|
|
|
+ if (next_index >= DATA_NODE_MAX)
|
|
|
+ next_index = 0;
|
|
|
+
|
|
|
+ /* notify transmitted complete. */
|
|
|
+ if (codec.parent.tx_complete != RT_NULL)
|
|
|
+ {
|
|
|
+ codec.parent.tx_complete(&codec.parent,
|
|
|
+ codec.data_list[codec.read_index].data_ptr);
|
|
|
+ rt_kprintf("-\n");
|
|
|
+ }
|
|
|
+
|
|
|
+ codec.offset = 0;
|
|
|
+ codec.read_index = next_index;
|
|
|
+ if (next_index == codec.put_index)
|
|
|
+ {
|
|
|
+ /* no data on the list, disable I2S interrupt */
|
|
|
+ SPI_I2S_ITConfig(SPI2, SPI_I2S_IT_TXE, DISABLE);
|
|
|
+
|
|
|
+ rt_kprintf("*\n");
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
+void codec_dma_isr()
|
|
|
+{
|
|
|
+ /* switch to next buffer */
|
|
|
+ rt_uint16_t next_index;
|
|
|
+ void* data_ptr;
|
|
|
+
|
|
|
+ next_index = codec.read_index + 1;
|
|
|
+ if (next_index >= DATA_NODE_MAX)
|
|
|
+ next_index = 0;
|
|
|
+
|
|
|
+ /* save current data pointer */
|
|
|
+ data_ptr = codec.data_list[codec.read_index].data_ptr;
|
|
|
+
|
|
|
+ codec.read_index = next_index;
|
|
|
+ if (next_index != codec.put_index)
|
|
|
+ {
|
|
|
+ /* enable next dma request */
|
|
|
+ DMA_Configuration((rt_uint32_t) codec.data_list[codec.read_index].data_ptr, codec.data_list[codec.read_index].data_size);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ rt_kprintf("*\n");
|
|
|
+ }
|
|
|
+
|
|
|
+ /* notify transmitted complete. */
|
|
|
+ if (codec.parent.tx_complete != RT_NULL)
|
|
|
+ {
|
|
|
+ codec.parent.tx_complete(&codec.parent, data_ptr);
|
|
|
+ // rt_kprintf("-\n");
|
|
|
+ }
|
|
|
+}
|