⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dac.cpp

📁 EM8511s中使用的mp4播放器
💻 CPP
字号:
#include "dac.h"#if 0#include <stdio.h>static void debug_break (void){}#define ASSERT(exp)					((void)((exp)?1:(printf ("ASSERT failed: line %d, file %s\n", __LINE__,__FILE__), debug_break(), 0)))#define DEBUGMSG(cond,printf_exp)	((void)((cond)?(printf printf_exp),1:0))#else#define ASSERT(exp)#define DEBUGMSG(cond,printf_exp)#endif#define MAX(X,Y) (((X)>=(Y))?(X):(Y))	// to find the max between two number.#define MIN(X,Y) (((X)<=(Y))?(X):(Y))	// to find the min between two number.static int mute = 0;static int volume = 50;static int balance = 50;typedef unsigned char RMuint8;typedef unsigned long RMuint32;#define I2C_MASTER_BASE_ADDRESS		(0x500800)#define I2C_MASTER_CONFIG			(I2C_MASTER_BASE_ADDRESS + 0x00)#define I2C_MASTER_CLK_DIV			(I2C_MASTER_BASE_ADDRESS + 0x04)#define I2C_MASTER_DEV_ADDR			(I2C_MASTER_BASE_ADDRESS + 0x08)#define I2C_MASTER_ADR				(I2C_MASTER_BASE_ADDRESS + 0x0c)#define I2C_MASTER_DATAOUT			(I2C_MASTER_BASE_ADDRESS + 0x10)#define I2C_MASTER_DATAIN			(I2C_MASTER_BASE_ADDRESS + 0x14)#define I2C_MASTER_STATUS			(I2C_MASTER_BASE_ADDRESS + 0x18)#define I2C_MASTER_STARTXFER		(I2C_MASTER_BASE_ADDRESS + 0x1c)#define I2C_MASTER_BYTE_CNT			(I2C_MASTER_BASE_ADDRESS + 0x20)#define I2C_MASTER_INTEN			(I2C_MASTER_BASE_ADDRESS + 0x24)#define I2C_MASTER_INT				(I2C_MASTER_BASE_ADDRESS + 0x28)#define ADRR_DEVICE					0x34	//adresse of the device (0x36)#define ADRR_REG_VOL_LEFT			4	//adresse of the register for the volume headphone left#define ADRR_REG_VOL_RIGHT			6	//adresse of the register for the volume headphone right#define ADRR_REG_RESET				0x1e //0	//adresse of the register for the reset of board#define ADRR_REG_ACTIVATION			18	//adresse of the register for the actvation of sound#define ADRR_REG_DAC_SELECTED		8	//adresse of the register for the selected dac#define ADRR_REG_MUTE				10	//adresse of the register for manage mute#define ADRR_REG_POWER				12	//adresse of the register for manage power#define DAC_SELECTED_ENABLE			0x10	//to selected the dac#define RESET_SOUND					0x00	//to reset all function of sound#define ACTIVATION_ENABLE			0x01	//to activate the sound#define MUTE_DISABLE				0x00	//to disable the function mute#define MUTE_ENABLE					0x08	//to enable the function mute#define ENABLE_POWER				0x66	//to manage the function power down (DAC and LINEOUT power on) [01100110]static void EM85xx_WriteReg (RMuint32 Reg, RMuint32 Data){	*((volatile RMuint32 *)Reg) = Data;}static RMuint32 EM85xx_ReadReg (RMuint32 Reg){	return *((volatile RMuint32 *)(Reg));}static int EM85xxI2C__Write (RMuint8 devaddr, RMuint8 adr, RMuint8* pData, int n){	int i;	EM85xx_WriteReg (I2C_MASTER_CONFIG, 0xf8);	EM85xx_WriteReg (I2C_MASTER_CLK_DIV, 375);	// assume 150mhz clock (375=750/2)	EM85xx_WriteReg (I2C_MASTER_DEV_ADDR, devaddr >> 1);	// ready for a command	// XXX bug: we really should have a timeout here	while ((EM85xx_ReadReg (I2C_MASTER_STATUS) & 1) == 0);	// write	for (i=0; i<n; i++)	{ 		EM85xx_WriteReg (I2C_MASTER_ADR, adr++);		EM85xx_WriteReg (I2C_MASTER_BYTE_CNT, 0);		EM85xx_WriteReg (I2C_MASTER_DATAOUT, *pData++);		EM85xx_WriteReg (I2C_MASTER_STARTXFER, 0);		// XXX bug: we really should have a timeout here		while ((EM85xx_ReadReg (I2C_MASTER_STATUS) & 2) == 0);	}		return 1;}static int EM85xxI2C__Read (RMuint8 devaddr, RMuint8 adr, RMuint8* pData, int n){	int i;		EM85xx_WriteReg (I2C_MASTER_CLK_DIV, 375);	// assume 150mhz clock (375=750/2)		// ready for a command	// XXX bug: we really should have a timeout here	while ((EM85xx_ReadReg (I2C_MASTER_STATUS) & 1) == 0);	for (i=0; i<n; i++)	{		EM85xx_WriteReg (I2C_MASTER_CONFIG, 0xfa);		EM85xx_WriteReg (I2C_MASTER_BYTE_CNT, 0);		EM85xx_WriteReg (I2C_MASTER_DEV_ADDR, devaddr >> 1);		EM85xx_WriteReg	(I2C_MASTER_DATAOUT, adr++);				EM85xx_WriteReg	(I2C_MASTER_STARTXFER, 0 | 0x4);		// XXX bug: we really should have a timeout here		while ((EM85xx_ReadReg (I2C_MASTER_STATUS) & 2) == 0);		while ((EM85xx_ReadReg (I2C_MASTER_STATUS) & 1) == 0);		EM85xx_WriteReg (I2C_MASTER_CONFIG, 0x00FA);		EM85xx_WriteReg (I2C_MASTER_BYTE_CNT, 0);				EM85xx_WriteReg (I2C_MASTER_DEV_ADDR, devaddr >> 1);			EM85xx_WriteReg (I2C_MASTER_STARTXFER, 1);		// XXX bug: we really should have a timeout here		while ((EM85xx_ReadReg (I2C_MASTER_STATUS) & 4) == 0);		while ((EM85xx_ReadReg (I2C_MASTER_STATUS) & 1) == 0);		*pData++ = (RMuint8)EM85xx_ReadReg (I2C_MASTER_DATAIN);	}	return 1;}int dac_init (void){	RMuint8 data;	// reset the dac	data = (RMuint8)RESET_SOUND;	EM85xxI2C__Write ((RMuint8)ADRR_DEVICE, (RMuint8)ADRR_REG_RESET, &data, 1);	DEBUGMSG (1, ("%02x: wrote 0x%02x to register %d\n", (RMuint8)ADRR_DEVICE, data, (RMuint8)ADRR_REG_RESET));    // activate dac	data = (RMuint8)ACTIVATION_ENABLE;	EM85xxI2C__Write ((RMuint8)ADRR_DEVICE, (RMuint8)ADRR_REG_ACTIVATION, &data, 1);	DEBUGMSG (1, ("%02x: wrote 0x%02x to register %d\n", (RMuint8)ADRR_DEVICE, data, (RMuint8)ADRR_REG_ACTIVATION));	// select dac	data = (RMuint8)DAC_SELECTED_ENABLE;	EM85xxI2C__Write ((RMuint8)ADRR_DEVICE, (RMuint8)ADRR_REG_DAC_SELECTED, &data, 1);	DEBUGMSG (1, ("%02x: wrote 0x%02x to register %d\n", (RMuint8)ADRR_DEVICE, data, (RMuint8)ADRR_REG_DAC_SELECTED));	// disable mute	data = (RMuint8)MUTE_DISABLE;	EM85xxI2C__Write ((RMuint8)ADRR_DEVICE, (RMuint8)ADRR_REG_MUTE, &data, 1);	DEBUGMSG (1, ("%02x: wrote 0x%02x to register %d\n", (RMuint8)ADRR_DEVICE, data, (RMuint8) ADRR_REG_MUTE));	// disable power down	data = (RMuint8)ENABLE_POWER;	EM85xxI2C__Write ((RMuint8)ADRR_DEVICE, (RMuint8)ADRR_REG_POWER, &data, 1);		DEBUGMSG (1, ("%02x: wrote 0x%02x to register %d\n", (RMuint8)ADRR_DEVICE, data, (RMuint8)ADRR_REG_POWER));	// put the volume at 50%	int volLeft;	int volRight;	if (balance > 50)	{		volLeft = (balance*volume) / 100;		volRight = volume;		}	else if (balance < 50)	{		volLeft = volume;		volRight = ((100-balance)*volume)/100;		}	else if (balance == 50)	{		volLeft = volume;		volRight = volume;		}	DEBUGMSG (1, ("balance : %d volume is :%d and [volLeft : %d volRight : %d ]\n", balance, volume, volLeft, volRight));	// to pump up the volume	volLeft = (volLeft*79/100) + 48;	volRight = (volRight*79/100) + 48;	DEBUGMSG (1, ("balance : %d volume is :%d and [volLeft : %d volRight : %d ]\n",balance, volume, volLeft, volRight));		EM85xxI2C__Write ((RMuint8)ADRR_DEVICE, (RMuint8)ADRR_REG_VOL_LEFT, (RMuint8 *)&volLeft, 1);	EM85xxI2C__Write ((RMuint8)ADRR_DEVICE, (RMuint8)ADRR_REG_VOL_RIGHT, (RMuint8 *)&volRight, 1);	return 0;}int dac_getvolume (void){	return volume;}int dac_getbalance (void){	return balance;}int dac_setvolume (int vol) {	int volLeft;	int volRight;	volume = MIN (100, vol);	volume = MAX (0, vol);	if (balance > 50)	{		volLeft = ((100-balance)*2*volume)/100;		volRight = volume;		}	else if (balance < 50)	{		volLeft = volume;		volRight = (balance*2*volume)/100;		}	else if (balance == 50)	{		volLeft=volume;		volRight=volume;		}	DEBUGMSG (1, ("balance : %d volume is :%d and [volLeft : %d volRight : %d ]\n", balance, volume, volLeft, volRight));	// to pump up the volume	volLeft = (volLeft*79/100) + 48;	volRight = (volRight*79/100) + 48;	EM85xxI2C__Write ((RMuint8)ADRR_DEVICE, (RMuint8)ADRR_REG_VOL_LEFT, (RMuint8 *)&volLeft, 1);	EM85xxI2C__Write ((RMuint8)ADRR_DEVICE,( RMuint8)ADRR_REG_VOL_RIGHT, (RMuint8 *)&volRight, 1);	return volume;}int dac_setbalance (const int bal) {	int volLeft;	int volRight;	balance = MIN (100, bal);	balance = MAX (0, bal);	if (balance > 50)	{		volLeft = ((100-balance)*2*volume)/100;		volRight = volume;		}	else if(balance < 50)	{		volLeft = volume;		volRight = (balance*2*volume)/100;		}	else if(balance == 50)	{		volLeft = volume;		volRight = volume;		}	DEBUGMSG (1, ("\nbalance : %d volume is :%d and [volLeft : %d volRight : %d ]\n", balance, volume, volLeft, volRight));	// to pump up the volume	volLeft = (volLeft*79/100)+48;	volRight = (volRight*79/100)+48;		EM85xxI2C__Write ((RMuint8)ADRR_DEVICE, (RMuint8)ADRR_REG_VOL_LEFT, (RMuint8 *)&volLeft, 1);	EM85xxI2C__Write ((RMuint8)ADRR_DEVICE, (RMuint8)ADRR_REG_VOL_RIGHT, (RMuint8 *)&volRight, 1);	return balance;}int dac_getmute (void){	return mute;}int dac_volumeup (void){	int volLeft;	int volRight;	volume = MIN (100, volume+5);	if (balance > 50)	{		volLeft = ((100-balance)*2*volume)/100;		volRight = volume;		}	else if (balance < 50)	{		volLeft = volume;		volRight = (balance*2*volume)/100;		}	else if (balance == 50)	{		volLeft=volume;		volRight=volume;		}	DEBUGMSG (1, ("balance : %d volume is :%d and [volLeft : %d volRight : %d ]\n", balance, volume, volLeft, volRight));	// to pump up the volume	volLeft = (volLeft*79/100) + 48;	volRight = (volRight*79/100) + 48;	EM85xxI2C__Write ((RMuint8)ADRR_DEVICE, (RMuint8)ADRR_REG_VOL_LEFT, (RMuint8 *)&volLeft, 1);	EM85xxI2C__Write ((RMuint8)ADRR_DEVICE,( RMuint8)ADRR_REG_VOL_RIGHT, (RMuint8 *)&volRight, 1);	return volume;}int dac_volumedown (void){		int volLeft;	int volRight;	volume = MAX (0, volume-5);	if (balance > 50)	{		volLeft = ((100-balance)*2*volume)/100;		volRight = volume;		}	else if (balance < 50)	{		volLeft = volume;		volRight = (balance*2*volume)/100;		}	else if (balance == 50)	{		volLeft = volume;		volRight = volume;		}	DEBUGMSG (1, ("balance : %d volume is :%d and [volLeft : %d volRight : %d ]\n", balance, volume, volLeft, volRight));	// to pump up the volume	volLeft = (volLeft*79/100)+48;	volRight = (volRight*79/100)+48;	EM85xxI2C__Write ((RMuint8)ADRR_DEVICE, (RMuint8)ADRR_REG_VOL_LEFT,(RMuint8 *)&volLeft, 1);	EM85xxI2C__Write ((RMuint8)ADRR_DEVICE, (RMuint8)ADRR_REG_VOL_RIGHT,(RMuint8 *)&volRight, 1);	return volume;}int dac_balanceright (void){	int volLeft;	int volRight;	balance = MIN (100, balance+5);	if (balance > 50)	{		volLeft = ((100-balance)*2*volume)/100;		volRight = volume;		}	else if(balance < 50)	{		volLeft = volume;		volRight = (balance*2*volume)/100;		}	else if(balance == 50)	{		volLeft = volume;		volRight = volume;		}	DEBUGMSG (1, ("\nbalance : %d volume is :%d and [volLeft : %d volRight : %d ]\n", balance, volume, volLeft, volRight));	// to pump up the volume	volLeft = (volLeft*79/100)+48;	volRight = (volRight*79/100)+48;		EM85xxI2C__Write ((RMuint8)ADRR_DEVICE, (RMuint8)ADRR_REG_VOL_LEFT, (RMuint8 *)&volLeft, 1);	EM85xxI2C__Write ((RMuint8)ADRR_DEVICE, (RMuint8)ADRR_REG_VOL_RIGHT, (RMuint8 *)&volRight, 1);	return balance;}int dac_balanceleft (void){	int volLeft;	int volRight;	balance = MAX (0, balance-5);	if (balance>50)	{		volLeft = ((100-balance)*2*volume)/100;		volRight = volume;		}	else if (balance < 50)	{		volLeft = volume;		volRight = (balance*2*volume)/100;		}	else if (balance == 50)	{		volLeft = volume;		volRight = volume;		}	DEBUGMSG (1, ("balance : %d volume is :%d and [volLeft : %d volRight : %d ]\n", balance, volume, volLeft, volRight));	// to pump up the volume	volLeft = (volLeft*79/100)+48;	volRight = (volRight*79/100)+48;		EM85xxI2C__Write ((RMuint8)ADRR_DEVICE, (RMuint8)ADRR_REG_VOL_LEFT,(RMuint8 *)&volLeft, 1);	EM85xxI2C__Write ((RMuint8)ADRR_DEVICE, (RMuint8)ADRR_REG_VOL_RIGHT,(RMuint8 *)&volRight, 1);	return balance;}int dac_mute (int mute_enable){	int data;	mute = mute_enable;	if (mute)		data = MUTE_ENABLE;	else		data = MUTE_DISABLE;	EM85xxI2C__Write ((RMuint8)ADRR_DEVICE, (RMuint8)ADRR_REG_MUTE, (RMuint8 *)&data, 1);	DEBUGMSG (1, ("mute= : %d \n", mute));	return mute;}int dac_write (int addr, int data){	RMuint8 b = (RMuint8)data;	EM85xxI2C__Write ((RMuint8)ADRR_DEVICE, (RMuint8)addr, &b, 1);	return data;}int dac_read (int addr){	RMuint8 b = 0xff;	EM85xxI2C__Read ((RMuint8)ADRR_DEVICE, (RMuint8)addr, &b, 1); 	return b;}void dac_powerdown_input(const bool down) {	const int def = ENABLE_POWER;	if(down)		//111 (left to right: adc, mic input + bias, line input)		dac_write(ADRR_REG_POWER, def | 0x7);	else		dac_write(ADRR_REG_POWER, def & 0xFFF8);}void dac_powerdown_output(const bool down) {	const int def = ENABLE_POWER;	if(down)		//11000 (left to right: dac, line output)		dac_write(ADRR_REG_POWER, def | 0x18);	else		dac_write(ADRR_REG_POWER, def & 0xFFE7);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -