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

📄 sadc.c.svn-base

📁 君正早期ucos系统(只有早期的才不没有打包成库),MPLAYER,文件系统,图片解码,浏览,电子书,录音,想学ucos,识货的人就下吧 russblock fmradio explore set
💻 SVN-BASE
字号:
/********************** BEGIN LICENSE BLOCK ************************************
 *
 * JZ4740  mobile_tv  Project  V1.0.0
 * INGENIC CONFIDENTIAL--NOT FOR DISTRIBUTION IN SOURCE CODE FORM
 * Copyright (c) Ingenic Semiconductor Co. Ltd 2005. All rights reserved.
 * 
 * This file, and the files included with this file, is distributed and made 
 * available on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 
 * EXPRESS OR IMPLIED, AND REALNETWORKS HEREBY DISCLAIMS ALL SUCH WARRANTIES, 
 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS 
 * FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 
 * 
 * http://www.ingenic.cn 
 *
 ********************** END LICENSE BLOCK **************************************
 *
 *  Author:  <dsqiu@ingenic.cn>  <jgao@ingenic.cn> 
 *
 *  Create:   2008-06-26, by dsqiu
 *            
 *  Maintain: 2008-06-26, by jgao
 *            
 *
 *******************************************************************************
 */

#include <jz4740.h>
#include <ucos_ii.h>
#include <jztouch.h>
#include <function.h>

#ifdef USE_MIDWARE
#include "midware.h"
#endif

#define TS_AD_COUNT             5
#define M_SADC_CFG_SNUM_5	    ((TS_AD_COUNT - 1) << SADC_CFG_SNUM_BIT)

#define SADC_CFG_INIT (   \
                             (2 << SADC_CFG_CLKOUT_NUM_BIT) |  \
						                SADC_CFG_XYZ1Z2		   		   |  \
									    SADC_CFG_SNUM_5                |  \
									    (1 << SADC_CFG_CLKDIV_BIT)    |  \
						                SADC_CFG_PBAT_HIGH             |  \
									    SADC_CFG_CMD_INT_PEN )
									    
static unsigned char run_flag = 0;
static u16 ts_TimeOut = 0;


#define TASK_STK_SIZE	1024
static OS_STK SADCTaskStack[TASK_STK_SIZE];
static OS_EVENT *sadcEvent;
static OS_EVENT *batteryEvent;
#define TOUCH_TASK_PRIO	5

static PFN_SADC touchFunction = 0;
static PFN_SADC sadcFunction = 0;
static PFN_SADC batFunction = 0;
static unsigned int batCountperSec = 0;
static unsigned int sadcCountperSec = 0;
static unsigned char dCount = 0;
unsigned short tsxData = -1,tsyData = -1;
static unsigned pendown_flag = 0;

static void read_ts_data()
{
	unsigned int dat;
	unsigned short xData,yData;
	short tsz1Data,tsz2Data;
    
	
	dat = REG_SADC_TSDAT;
	
	xData = (dat >>  0) & 0xfff;
	yData = (dat >> 16) & 0xfff;
	
    //printf("%d -> %d %d\r\n",dCount,xData,yData);		
	dat = REG_SADC_TSDAT;
    tsz1Data = (dat >>  0) & 0xfff;
	tsz2Data = (dat >> 16) & 0xfff;
	//printf("%d -> %d %d\r\n",dCount,tsz1Data,tsz2Data);		
	tsz1Data = tsz2Data - tsz1Data;
	if(pendown_flag != 1)
		return ;
	
	
	if((tsz1Data > 15) || (tsz1Data < -15))
    {
		if(tsxData == (unsigned short)-1)
			tsxData = xData;
		else
			tsxData = (tsxData + xData) / 2;
		
		if(tsyData == (unsigned short)-1)
			tsyData = yData;
		else
			tsyData = (tsyData + yData) / 2;
		
    }

//	printf("0x%08x \r\n",dat);	
    dCount ++;
	
	if(dCount > TS_AD_COUNT - 1)
	{
		if(tsxData != (unsigned short) -1)
		{
			
		    if(touchFunction)
			{
				dat = tsxData + (tsyData << 16);
				touchFunction((unsigned short *)&dat);
			}
			//printf("x: 0x%d y: 0x%d\r\n",tsxData,tsyData);	
			tsxData = -1;
			tsyData = -1;
		}
		
		dCount = 0;
	}
	
}
static void touch_task_entry(void *arg)
{
		unsigned char state;
		unsigned char sadcstat;
		unsigned char err;
		
		while(run_flag)
		{
			OSSemPend(sadcEvent,ts_TimeOut,&err);
			ts_TimeOut = 0;
			sadcstat = REG_SADC_STATE;
			state = REG_SADC_STATE & (~REG_SADC_CTRL);
			REG_SADC_STATE &= sadcstat;
					
			if(state & SADC_CTRL_PENDM)
			{
				  //printf("pen down irq \r\n");
				  REG_SADC_CTRL &= (~(SADC_CTRL_PENUM |  SADC_CTRL_TSRDYM));
				  REG_SADC_CTRL |= (SADC_CTRL_PENDM);// | SADC_CTRL_TSRDYM);
				  pendown_flag = 1;
			}
			if(state & SADC_CTRL_PENUM)
			{
				//printf("pen up irq \r\n");
				REG_SADC_CTRL &= (~SADC_CTRL_PENDM );
				REG_SADC_CTRL |= SADC_CTRL_PENUM;
				pendown_flag = 0;
				tsxData = -1;
				tsyData = -1;
				if(touchFunction)
				{
					unsigned int dat;
					
					dat = tsxData + (tsyData << 16);
					touchFunction((unsigned short *)&dat);
				}
			    //printf("x: 0x%d y: 0x%d\r\n",tsxData,tsyData);	
			}
			if(state & SADC_CTRL_TSRDYM)
			{
				//printf("touch ad irq 0x%x \r\n",sadcstat);
					read_ts_data();
					
			}
			if(state & SADC_CTRL_PBATRDYM)
			{
				printf("battery ad irq\r\n");
			}
			if(state & SADC_CTRL_SRDYM)
			{
				printf("sad ad irq\r\n");
				
			}
			
			__intc_unmask_irq(IRQ_SADC);
		}
		
}


static void handler(unsigned int arg)
{
	__intc_mask_irq(IRQ_SADC);
//	printf("IRQ_SADC");
	
	OSSemPost(sadcEvent);
}

static int sadc_init()
{
	REG_SADC_ENA = 0;
	REG_SADC_STATE &= (~REG_SADC_STATE);
	REG_SADC_CTRL = 0x1f;
	
	sadcEvent = OSSemCreate(0);
	//__cpm_start_sadc();
	REG_SADC_CFG =  SADC_CFG_INIT;

	run_flag = 1;
	request_irq(IRQ_SADC, handler, 0);
	
	OSTaskCreate(touch_task_entry, (void *)0,
		     (void *)&SADCTaskStack[TASK_STK_SIZE - 1],
		     TOUCH_TASK_PRIO);
	REG_SADC_SAMETIME = 1;
	REG_SADC_WAITTIME = 1000; //per 100 HZ
	ts_TimeOut = 0;
	REG_SADC_STATE &= (~REG_SADC_STATE);
	REG_SADC_CTRL &= (~(SADC_CTRL_PENDM | SADC_CTRL_TSRDYM)); // 
	REG_SADC_ENA = SADC_ENA_TSEN; // | REG_SADC_ENA;//SADC_ENA_TSEN | SADC_ENA_PBATEN | SADC_ENA_SADCINEN;
	
}

static int sadc_deinit()
{
	REG_SADC_ENA = 0;
	__intc_mask_irq(IRQ_SADC);
	OSTimeDly(20);
	run_flag = 0;
	__cpm_stop_sadc();
}

int ts_init(void)
{
	return sadc_init();
}

/********* battery start ************************************************/

#ifdef USE_MIDWARE
u16 pbat= MAX_BAT_VAL; //battery voltage
#else
u16 pbat= 2280; //battery voltage
#endif


/*
 *  battery voltage int handle
 */
static void handler_battery(unsigned int arg)
{
	unsigned char state;

	__intc_mask_irq(IRQ_SADC);
	state = REG_SADC_STATE & (~REG_SADC_CTRL);
	if(state & SADC_CTRL_PBATRDYM)
	{
		pbat = REG_SADC_BATDAT;
//		OSSemPost(batteryEvent);
		REG_SADC_STATE = SADC_STATE_PBATRDY;
	}
	__intc_unmask_irq(IRQ_SADC);
}

/*
 * Read the battery voltage
 */
static void start_pbat_adc(void);

static unsigned int jz4740_read_battery(void)
{
	if(!(REG_SADC_STATE & SADC_STATE_PBATRDY) ==1)
		start_pbat_adc();
}

/* 
 * set adc clock to 12MHz/div. A/D works at freq between 500KHz to 6MHz.
 */
static void sadc_init_clock(int div)
{
	if (div < 2) div = 2;
	if (div > 23) div = 23;
	
	REG_SADC_CFG &= ~SADC_CFG_CLKDIV_MASK;
	REG_SADC_CFG |= (div - 1) << SADC_CFG_CLKDIV_BIT;
//	REG_SADC_CTRL = 0x1d;  //touch screen
//	REG_SADC_ENA = SADC_ENA_TSEN;
}

static void start_pbat_adc(void)
{
	REG_SADC_CFG |= SADC_CFG_PBAT_HIGH ;   /* full baterry voltage >= 2.5V */
//  	REG_SADC_CFG |= SADC_CFG_PBAT_LOW;    /* full baterry voltage < 2.5V */
  	REG_SADC_ENA |= SADC_ENA_PBATEN;      /* Enable pbat adc */

	
}

  
int battery_init()
{
	batteryEvent = OSSemCreate(0);
	sadc_init_clock(3);
	request_irq(IRQ_SADC,handler_battery, 0);

}

int read_battery(void)
{
	unsigned char err;

	jz4740_read_battery();
//	OSSemPend(batteryEvent,100,&err);
//	printf("read battery delay!---err:%d\n",err);
	
	return pbat;
}

/************ battery end ************************************************/

⌨️ 快捷键说明

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