Chào mấy anh/em,
Mình đang làm cái project nhỏ vể máy mp3, mới viết tới driver cho VS1011 audio decoder thì bị đứng hình không biết sai chổ nào, mò 2-3 bữa nay rồi. Mong mọi người giúp mình với.
Mục tiêu: driver cho VS1011 và test âm thanh.
Phần cứng:
VS1011 -> LPV1769
SI -> P0.18
SO -> P0.17
SCLK ->P0.15
DREQ ->P0.21
xDCS ->P0.19
xCS ->P0.16
xRST ->P0.20
Firmware: (mình dùng IAR)
Driver:
Main:
Mình không tài nào nghe dc âm thanh sinewave khi chay đoạn code này! không biết giao thức SPI mình config và giải thuật có đúng ko.
Cảm ơn anh/em rất nhìu.
Mình đang làm cái project nhỏ vể máy mp3, mới viết tới driver cho VS1011 audio decoder thì bị đứng hình không biết sai chổ nào, mò 2-3 bữa nay rồi. Mong mọi người giúp mình với.
Mục tiêu: driver cho VS1011 và test âm thanh.
Phần cứng:
VS1011 -> LPV1769
SI -> P0.18
SO -> P0.17
SCLK ->P0.15
DREQ ->P0.21
xDCS ->P0.19
xCS ->P0.16
xRST ->P0.20
Firmware: (mình dùng IAR)
Driver:
Code:
/********************************************************************** * $Id$ vs1011.c 2012-07-02 * * @file vs1011.c * @brief VS1011 audio decoder driver * @MCU NXP ARM Cortex-M3 LPC1769 * @version 1.0 * @date 02. July. 2012 * @Company * @Website * @author Sang Mai * * @References: THORSTEN PAULY 2007,Luca Pascarella 2009 - www.lucasproject.it * * All rights reserved. * ***********************************************************************/ #include "lpc17xx_spi.h" #include "lpc17xx_libcfg.h" #include "lpc17xx_pinsel.h" #include "lpc17xx_gpio.h" #include "vs1011.h" /************************************************* * SPI Define * **************************************************/ #define SPI_Function PINSEL_FUNC_3 #define SPI_Port PINSEL_PORT_0 #define SPI_SCK PINSEL_PIN_15 #define SPI_MISO PINSEL_PIN_17 #define SPI_MOSI PINSEL_PIN_18 /************************************************* * VS1011 Define * **************************************************/ #define VS1011_Port PINSEL_PORT_0 #define VS1011_Function PINSEL_FUNC_0 #define VS1011_xCS PINSEL_PIN_16 #define VS1011_xDCS PINSEL_PIN_19 #define VS1011_xRST PINSEL_PIN_20 #define VS1011_DREQ PINSEL_PIN_21 /************************************************* * *General Define * **************************************************/ #define Output 1 #define Input 0 /************************************************* * * Local Variable * **************************************************/ static unsigned long DREQ_Port; // SPI Configuration structure variable SPI_CFG_Type SPI_ConfigStruct; // SPI Data Setup structure variable SPI_DATA_SETUP_Type xferConfig; /************************************************************************ * @brief SPI and VS1011 Configuration * @param[in] None * @return None ***********************************************************************/ void SPI_Config(void) { PINSEL_CFG_Type PinCfg; //Pin config /* * Initialize SPI pin connect * P0.15 - SCK; * P0.16 - CS/xCS - used as GPIO * P0.17 - MISO * P0.18 - MOSI * P0.19 - xDCS/BSYNC -GPIO * P0.20 - RESET/xRST - GPIO * P0.21 - DREQ - GPIO */ //SPI PinCfg.Funcnum = SPI_Function; PinCfg.OpenDrain = PINSEL_PINMODE_NORMAL; PinCfg.Pinmode = PINSEL_PINMODE_PULLUP; PinCfg.Portnum = SPI_Port; PinCfg.Pinnum = SPI_SCK; PINSEL_ConfigPin(&PinCfg); PinCfg.Pinnum = SPI_MISO; PINSEL_ConfigPin(&PinCfg); PinCfg.Pinnum = SPI_MOSI; PINSEL_ConfigPin(&PinCfg); // initialize SPI configuration structure to default SPI_ConfigStruct.CPHA = SPI_CPHA_FIRST; SPI_ConfigStruct.CPOL = SPI_CPOL_LO; SPI_ConfigStruct.ClockRate = 1000000; SPI_ConfigStruct.DataOrder = SPI_DATA_MSB_FIRST; SPI_ConfigStruct.Databit = SPI_DATABIT_8; SPI_ConfigStruct.Mode = SPI_MASTER_MODE; // Initialize SPI peripheral with parameter given in structure above SPI_Init(LPC_SPI, &SPI_ConfigStruct); //GPIO PinCfg.Funcnum = VS1011_Function; //GPIO PinCfg.Pinnum = (VS1011_xCS); PINSEL_ConfigPin(&PinCfg); PinCfg.Pinnum = (VS1011_xDCS); PINSEL_ConfigPin(&PinCfg); PinCfg.Pinnum = (VS1011_xRST); PINSEL_ConfigPin(&PinCfg); PinCfg.Pinnum = (VS1011_DREQ); PINSEL_ConfigPin(&PinCfg); //GPIO Direction GPIO_SetDir(VS1011_Port, (1<<VS1011_xCS), Output);//output GPIO_SetDir(VS1011_Port, (1<<VS1011_xDCS), Output);//output GPIO_SetDir(VS1011_Port, (1<<VS1011_xRST), Output);//output GPIO_SetDir(VS1011_Port, (1<<VS1011_DREQ), Input);//input //SPI config } /************************************************************************ * @brief Initialize VS1011 - * @param[in] None * @return None ***********************************************************************/ void VS1011_Init(void) { xCS_DeSelect(); xDCS_DeSelect(); xRST_Enable(); //delay delay(); xRST_Disable(); // enable sinewave test VS1011_WriteReg(VS1011_MODE,SineTestEnable); delay(); while (!ReadDREQ()); // set clock for VS1011 VS1011_WriteReg(VS1011_CLOCKF, 0x9800); delay(); while (!ReadDREQ()); } /************************************************************************ * @brief Drive xCS output. * @param[in] None * @return None ***********************************************************************/ void xCS_DeSelect(void) { GPIO_SetValue(VS1011_Port, (1<<VS1011_xCS)); } void xCS_Select(void) { GPIO_ClearValue(VS1011_Port, (1<<VS1011_xCS)); } /************************************************************************ * @brief Drive xDCS output. * @param[in] None * @return None ***********************************************************************/ void xDCS_DeSelect(void) { GPIO_SetValue(VS1011_Port, (1<<VS1011_xDCS)); } void xDCS_Select(void) { GPIO_ClearValue(VS1011_Port, (1<<VS1011_xDCS)); } /************************************************************************ * @brief Drive xRESET output. * @param[in] None * @return None ***********************************************************************/ void xRST_Disable(void) { GPIO_SetValue(VS1011_Port, (1<<VS1011_xRST)); } void xRST_Enable(void) { GPIO_ClearValue(VS1011_Port, (1<<VS1011_xRST)); } /************************************************************************ * @brief Read DREQ Value from VS1011 * @param[in] None * @return 1 or 0 ***********************************************************************/ uint8_t ReadDREQ() { DREQ_Port = GPIO_ReadValue(VS1011_Port); if (( DREQ_Port & VS1011_DREQ) == 0) return 0; if (( DREQ_Port & VS1011_DREQ) == 1) return 1; } /************************************************************************ * @brief Reading data info in SCI data register * @param[in] Address * @return the value that input address ***********************************************************************/ uint16_t VS1011_ReadReg( uint8_t vAddress) { uint16_t wValue; xCS_Select(); //Control register is selected SPI_SendData(LPC_SPI, VS1011_SCIRead_Command); SPI_SendData(LPC_SPI,vAddress); wValue = SPI_ReceiveData(LPC_SPI); //((uint8_t*)&wValue)[0] = SPI_ReceiveData(LPC_SPI); xCS_DeSelect(); return wValue; //test } /************************************************************************ * @brief Writing data info in SCI data register * @param[in] Address, writing value * @return None ***********************************************************************/ void VS1011_WriteReg( uint8_t vAddress, uint16_t wValue) { unsigned char Data_Buf[4]; xCS_Select(); delay(); SPI_SendData(LPC_SPI, VS1011_SCIWrite_Command); SPI_SendData(LPC_SPI,vAddress); //SPI_SendData(LPC_SPI, wValue); SPI_SendData(LPC_SPI, ((uint8_t*)&wValue)[1]); SPI_SendData(LPC_SPI, ((uint8_t*)&wValue)[0]); delay(); /* Data_Buf[0] = VS1011_SCIWrite_Command; Data_Buf[1] = vAddress; Data_Buf[2] = ((uint8_t*)&wValue)[1]; Data_Buf[3] = ((uint8_t*)&wValue)[0]; xferConfig.tx_data = Data_Buf; xferConfig.rx_data = 0; xferConfig.length = 4; SPI_ReadWrite(LPC_SPI, &xferConfig, SPI_TRANSFER_POLLING); */ xCS_DeSelect(); } /************************************************************************ * @brief Enable VS1011 Sine wave test * @param[in] None * @return None ***********************************************************************/ void VS1011_EnableSineTest() { unsigned char Data_Buf[8]; Data_Buf[0] = 0x53; Data_Buf[1] = 0xEF; Data_Buf[2] = 0x6E; Data_Buf[3] = 126; //5168 Hz Data_Buf[4] = 0x00; Data_Buf[5] = 0x00; Data_Buf[6] = 0x00; Data_Buf[7] = 0x00; VS1011_WriteData(Data_Buf, 8); } /************************************************************************ * @brief Disable VS1011 Sine wave test * @param[in] None * @return None ***********************************************************************/ void VS1011_DisableSineTest() { unsigned char Data_Buf[8]; Data_Buf[0] = 0x45; Data_Buf[1] = 0x78; Data_Buf[2] = 0x69; Data_Buf[3] = 0x74; //5168 Hz Data_Buf[4] = 0x00; Data_Buf[5] = 0x00; Data_Buf[6] = 0x00; Data_Buf[7] = 0x00; VS1011_WriteData(Data_Buf, 8); VS1011_WriteReg(VS1011_MODE,SineTestDisable); //enable sine test condition } /************************************************************************ * @brief VS1011 Soft reset * @param[in] None * @return None ***********************************************************************/ void VS1011_SoftReset() { VS1011_WriteReg(VS1011_MODE, 0x0806); //delay delay(); VS1011_WriteReg(VS1011_CLOCKF, 0x9800); while (ReadDREQ() == 0); } /************************************************************************ * @brief Writing data to VS1011 data buffer in order to decode it * @param[in] * @return ***********************************************************************/ void VS1011_WriteData(uint8_t *Data, int len) { int i; //uint8_t temp, ret; xDCS_Select(); //delay delay(); for( i= 0; i < len ; i++) { SPI_SendData(LPC_SPI,Data[i]); } delay(); xDCS_DeSelect(); } /************************************************************************ * @brief Delay Function * @param[in] * @return ***********************************************************************/ void delay(void) { unsigned int count; for(count = 0; count < 10000; count++) { } }
Code:
/********************************************************************** * $Id$ main.c 2012-07-02 *//** * @file main.c * @brief The purpose of this project is to test the interface btw MCU * and VS1011 * @MCU NXP ARM Cortex-M3 LPC1769 * @version 1.0 * @date 02. July. 2012 * @Company * @Website * @author Sang Mai * * @References: * * All rights reserved. * ***********************************************************************/ #include "lpc17xx_spi.h" #include "lpc17xx_libcfg.h" #include "lpc17xx_pinsel.h" #include "lpc17xx_gpio.h" #include "vs1011.h" void main(void) { SPI_Config(); /* * LED Config */ GPIO_SetDir(0, (1<<PINSEL_PIN_22), 1); //GPIO_SetValue(0, (1<<PINSEL_PIN_22)); GPIO_ClearValue(0, (1<<PINSEL_PIN_22)); VS1011_Init(); VS1011_EnableSineTest(); //xCS_DeSelect(); //xDCS_DeSelect(); //xRST_Disable(); while (1) { } }
Cảm ơn anh/em rất nhìu.
Comment