示例#1
0
void lcd_write_byte(uint8_t ch)
{
    // Note RS must be select outside
    LCD_CTRL_PORT &= ~(1 << LCD_CTRL_RD);
	_nop();
	LCD_CTRL_PORT |= (1 << LCD_CTRL_EN);
	_nop();
    LCD_DATA_PORT = ch;
	_nop();
    LCD_CTRL_PORT &= ~(1 << LCD_CTRL_EN);
	_nop();
}
示例#2
0
__interrupt void PORT3_ISR (void)
{
	if(P3IFG & BIT5){
		//P9OUT ^= BIT6;
		P3IFG &= ~BIT5;
		//P3IES ^= BIT5;     // 检测双边沿
		
#ifdef MAN_MASTER
		_nop();
#else
		if(WAITING_S == g_ucRXState){
			MAN_RX_DINT;                   // 关闭接收端口的中断使能
			g_ucRXState = RECEIVING_DATA;  // 可能收到数据,下一步确认是否为开始位
			g_ucRXIndex = 1;               // 收到的数据位数清零,从1开始
			g_ucParity = 0;                // 奇偶校验清零
			for(i=0; i<SAMP_DELAY; ++i){
				__no_operation();
			}
			START_TB;                      // 打开定时器,定时一个周期去获取一次数据
			ManGetData(g_ucRXIndex);       // 采集第一位数据
		}
		
#endif
	}
}
/*****************************************************************************
 函 数 名  : OLED_Init
 功能描述  : OOLED初始化
 输入参数  : void
 输出参数  : NONE
 返 回 值  : NONE
*****************************************************************************/
void OLED_Init(void)
{
    uchar8 i;

    OLED |=OLED_SCL;
    OLED &=~OLED_RST;

    for(i = 0; i < 100; i++)
    {
        _nop();            //从上电到下面开始初始化要有足够的时间,即等待RC复位完毕
    }

    OLED |=OLED_RST;

    SetDisplayOnOff(0x00);     // Display Off (0x00/0x01)
    SetDisplayClock(0x80);     // Set Clock as 100 Frames/Sec
    SetMultiplexRatio(0x3F);   // 1/64 Duty (0x0F~0x3F)
    SetDisplayOffset(0x00);    // Shift Mapping RAM Counter (0x00~0x3F)
    SetStartLine(0x00);        // Set Mapping RAM Display Start Line (0x00~0x3F)
    SetChargePump(0x04);       // Enable Embedded DC/DC Converter (0x00/0x04)
    SetAddressingMode(0x02);   // Set Page Addressing Mode (0x00/0x01/0x02)
    SetSegmentRemap(0x01);     // Set SEG/Column Mapping     0x00左右反置 0x01正常
    SetCommonRemap(0x08);      // Set COM/Row Scan Direction 0x00上下反置 0x08正常
    SetCommonConfig(0x10);     // Set Sequential Configuration (0x00/0x10)
    SetContrastControl(0xCF);  // Set SEG Output Current
    SetPrechargePeriod(0xF1);  // Set Pre-Charge as 15 Clocks & Discharge as 1 Clock
    SetVCOMH(0x40);            // Set VCOM Deselect Level
    SetEntireDisplay(0x00);    // Disable Entire Display On (0x00/0x01)
    SetInverseDisplay(0x00);   // Disable Inverse Display On (0x00/0x01)
    SetDisplayOnOff(0x01);     // Display On (0x00/0x01)
    OLED_Fill(0x00);            // 初始清屏
    OLED_SetPos(0,0);

    return;
}
示例#4
0
void lcd_write_data(uint8_t data)
{
    LCD_CTRL_PORT |= (1 << LCD_CTRL_RS);
    _nop();
	lcd_write_byte(data);
    _delay_us(40);
}
示例#5
0
void lcd_write_cmd(uint8_t cmd)
{
    LCD_CTRL_PORT &= ~(1 << LCD_CTRL_RS);
	_nop();
    lcd_write_byte(cmd);
    _delay_ms(2);
}
示例#6
0
/*----------------------------------------------------------------------------
*  Module:
*   SchedulerISR
*
*     This function is automatically invoked by the C167 when Timer
*     1 has expired (interrupt service routine).
*
----------------------------------------------------------------------------*/
interrupt (CAPCOM_TIMER_1) using (SCHEDULE_RB) void SchedulerISR (void)
{
	BOOLEAN taskMVBAlive = FALSE;

    /* Service Vdrive Watchdog */
    ServiceVDriveWatchdog();

	/* Monitor the MVB 8 ms interrupt. Allow 5 consecutive dead interrupts
	   before declaring interrupt dead and forcing a CPU reset */
	if (GetStartupSuccessful() == TRUE)
	{
		taskMVBAlive = MonitorMVBInterrupt (5);
		if (!taskMVBAlive)
		{
			SetCPUDisable (TASK_MVB_DEAD);
		}
	}

	/* Service CPU watchdog. Note: Logic exists in this function to disable
		servicing watchdog if above check fails */
	ServiceCPUWatchdog();

    /* -------------------------------------------------------------------------
    Service the timers. CLOCK_TICK should be updated in "timer.h"
    to reflect the periodicity of this interrupt.
    ------------------------------------------------------------------------- */
    TM_Service();

	 _nop();

}
_reentrant void Philips3WireClockDelay(void)
{
    int i;
   for(i=0;i<PHILIPS_3WIRE_DELAY;i++)
   {
       _nop(); // TODO: For portability: convert this to macro defined in a single file like types.h
   };
}
示例#8
0
文件: main.c 项目: FarmerLjx/MCU_Libs
void main(void)
{
   	SysInit();
    AD9954Init();
   	IrInit();
   	ADCInit();
   	UIInit();
	UIRefresh();
   	//InitManchester();

    //Set the timer A1
    TA1CCTL0 = CCIE;                          // CCR0 interrupt enabled
    TA1CCR0 = 3277;
    TA1CTL = TASSEL_1 + MC_1 + TACLR;         // SMCLK, up mode, clear TAR


    //AD9954_OSK_H;                           // OSK UP
    SingleTone();                             // 1MHz for default
	g_ulFrequence = 5000000;                  // 必须和SetFreqToHz绑定使用
    SetFreqToHz(g_ulFrequence);               // n Hz
	
	_EINT();

    while(1){
    	//ADSend(0xff);
    	_nop();_nop();_nop();_nop();_nop();
    	delay_ms(100);

    	if(g_ucGetNumFlag){
    		//g_ulFrequence = g_ulGetNum;
			//SetFreqToHz(g_ulFrequence);
			g_dC = GetCValueByVRate(g_ulGetNum / 1000.0);
			UIRefresh();
    		//AD9954SetReg8(ASF, 1000*g_ulGetNum);
    		//g_ulGetNum = 16415;
    		g_ucGetNumFlag = 0;      // Clear
    		g_ulGetNum = 0;          // Reset
			
			//g_dQ = GetCValueByVRate(g_fVRate);
    	}
		
		// 进入低功耗模式
		__bis_SR_register(LPM0_bits + GIE);     // Enter LPM0, Enable interrupts
		__no_operation();                       // For debugger
    }
}
示例#9
0
文件: 1602.c 项目: timqi/watch
/*****写控制字符****attrib控制是否检测忙信号***/
void lcd_write_command(uchar cmd, uchar attrib)
{
	if(attrib)	lcd_wait_for_enable();	//检测是否繁忙

	RS=0; RW=0; _nop_(); E=1;
	DATAPORT = cmd;  _nop_();

	E=0; _nop_(); _nop(); E=1;
}
示例#10
0
void main(void) {

	WDTCTL = WDTPW + WDTHOLD;

	_disable_interrupts();

	clkInit(DCO_FREQ);
	CHG_CONFIG();
	//LED_RED_CONFIG();
	//LED_GREEN_CONFIG();
	//SW1_CONFIG();
	_enable_interrupts();

#ifdef CDC_CONT
	//register ADC with default codeword for continuous sampling
	myCDC = registerCDC(CDC_CHEN1 | CDC_CHEN2 | CDC_SAMPLEC);

//	resetCDC();
	__delay_cycles(250000);

	setCDC(myCDC);

	writeRegCDC(CDC_POS, CDC_DACPEN | 0x01); //setup the CAPDAC (vertical offset capacitance compensation)
	writeRegCDC(CDC_SETUP1, CDC_SE_25pF); //set the full scale range

	startCDC();

	while (1) {
		_nop();
//		writeRegCDC(testReg, testVal);
		_nop();
		readRegCDC(CDC_STATUS, 16);
	}

#else
	myADC = registerADS(ADS_01, ADS_DEFAULT1); //register ADc with default codeword for 1 shot sampling
	setADS(myADC);
	while(1) {
		a = singleShotADS();
	}

#endif
}
示例#11
0
void main(void) {

	WDTCTL = WDTPW + WDTHOLD;

	_disable_interrupts();

	clkInit(DCO_FREQ);
	CHG_CONFIG();
	//LED_RED_CONFIG();
	//LED_GREEN_CONFIG();
	//SW1_CONFIG();
	_enable_interrupts();

#ifdef TMP_CONT
	//register ADC with default codeword for continuous sampling
	myTMP = registerTMP(TMP100_01, TMP_DEFAULTC);

	setTMP(myTMP);
	startTMP();

	writeLongTMP(TMP_HI_THRESH, TMP_REF_100);//demo write register

	while (1) {
		a = readLongTMP(TMP_LO_THRESH);
		_nop();
		a = readLongTMP(TMP_HI_THRESH);
		_nop();
		a = readConfigTMP();
		_nop();
		a = readLongTMP(TMP_TEMP);
		_nop();

	}
#else
	myTMP = registerTMP(TMP100_01, TMP_DEFAULT1); //register ADc with default codeword for 1 shot sampling
	setTMP(myTMP);
	while(1){
	a = singleShotTMP();
	}

#endif
}
示例#12
0
static inline void lcd_write_byte(char data)
{
    // Ensure ENABLE is off
    LCD_EN_CLR();

    PORT_WR(LCD_DATA_P, data << LCD_DATA_OFFSET);

    // Flash ENABLE to issue command. Requires at least 450ns, which is a bit less than this will take
    LCD_EN_SET();
    //task_delay(LCD_VERY_SHORT_DELAY);
    _nop();
    LCD_EN_CLR();
}
示例#13
0
unsigned int captouch_sense(Button *button)
{
    int mask = button->pin;

    //TRISACLR = BIT_12;
    //PORTASET = BIT_12;

    SetChanADC10(ADC_CH0_POS_SAMPLEA_AN10);

    // sample, but cancel it - this is only to charge the capacitor
    AD1CON1SET = 0x0002;
    _nop();
    _nop();
    _nop();
    AD1CON1CLR = 0x0002;
    AD1CON1CLR = 0x8000;
    AD1CON1SET = 0x8000;

    //TRISBCLR = 0x038;
    //PORTBCLR = 0x038;
    //TRISBSET = 0x038;

    TRISBCLR = mask;
    PORTBCLR = mask;
    TRISBSET = mask;

    SetChanADC10(button->channel);

    AD1CON1SET = 0x2;
    while (!(AD1CON1 & 0x1));
    AD1CON1CLR = 0x2;

    TRISBCLR = 0x038;
    PORTBCLR = 0x038;

    return ADC1BUF0;
}
/*
«ü¥O:wlcmd_4('¸ê®Æ/«ü¥O¼Ò¦¡','¸ê®Æ¦r¥À/©R¥O¼Æ­È')
¥Øªº:¨Ï¥Îc»y¨¥¼ÒÀÀ²Õ»y
Example:
²M°£«ü¥O=>wlcmd_4('c',0x01);
S¡B0¡B1¦r¤¸=>wlcmd_4('d','S');wlcmd_4('d','0');wlcmd_4('d','1');
*/
void wlcmd_4(char lcm_command,char InputChar) 
{
	char dc_flag;
if (lcm_command=='d') 
	{
	dc_flag=1;
	InputChar=chartolcm(InputChar);
	}
else 
	{dc_flag=0;}
lcm_dataportc=lcm_dataportc|0b11110000;
lcm_rw=1;
lcm_rs=0;
_nop();
lcm_en=1;
_nop();
while((lcm_dataport&0b10000000)==1);
lcm_en=0;
lcm_dataport=lcm_dataport&0b00001111;
lcm_dataportc=lcm_dataportc&0b00001111;
lcm_dataport=(InputChar&0b11110000)|lcm_dataport;
lcm_rw=0;
lcm_rs=0;
if (dc_flag==1) lcm_rs=1;
_nop();	//<-¼ÒÀÀNOP«ü¥O
lcm_en=1;
_nop();
lcm_en=0;
lcm_dataport=lcm_dataport&0b00001111;
InputChar=((InputChar&0b11110000)>>4)+((InputChar&0b00001111)<<4);
lcm_dataport=(InputChar&0b11110000)|lcm_dataport;
lcm_en=1;
_nop();
lcm_en=0;
return;
}
示例#15
0
/*----------------------------------------------------------------------------
  Module:
    ISR to service Vehicle related task including the PTU

     This function is automatically invoked by the C167 when Timer
     7 has expired (interrupt service routine).

--------------------------------------------------------------------------  */
interrupt (CAPCOM_TIMER_7) using (VEHICLECONTROLISR_RB) void VehicleControlISR (void)
{
	// ONLY USE FOR CPU LOAD VERIFICATION --- HIDAC_FAIL_LED_ON();
	if (GetBurninConfig () == TRUE)
	{
		BurnInISR ();
	}
    else if (GetStartupSuccessful() == TRUE)
    {
        Task8ms();
    }
	// ONLY USE FOR CPU LOAD VERIFICATION --- HIDAC_FAIL_LED_OFF();
	_nop();

}
示例#16
0
文件: main.c 项目: linkinlzm/MCU
int main(void)
{
    WDTCTL = WDTPW + WDTHOLD;             // Stop watchdog timer

    initializeClocks();
    InitializeButton();
    InitializeLeds();

    //This will reset the nRF8001. ACI Device Started Event is generated by the nRF8001 device
    //as soon as the reset is complete
    hal_aci_tl_init();

    // Reset nRF8001
    resetDevice();
    _BIS_SR(GIE);

    begin_BLE(&aci_state);

    // Main application loop
    for (;;)
    {
        //_BIS_SR(LPM0_bits + GIE); // Enter LPM0 w/interrupt
        _nop();                   // For debugger
        //Not entirely sure if any of this if statement needs to be here...
        if(rdynFlag == 1)
        {
            rdynFlag = 0;

            m_rdy_line_handle();
        }
        pollACI(&aci_state, &aci_data, &aci_cmd);
        state = getState();
        if ((getState() == ACI_EVT_CONNECTED) && (!ctr) && lib_aci_is_pipe_available(&aci_state, PIPE_UART_OVER_BTLE_UART_TX_TX)) {
            //make sure pipe is available
//#define UART
#ifdef UART
            write("80 98 37 998", 12, &aci_state, &aci_data, &aci_cmd);
#else
            write(data, 6, &aci_state, &aci_data, &aci_cmd);
#endif
            //lib_aci_get_battery_level();
        }
        ctr++;
    }
}
void divide(float z)
{
	int i=0,j=0;
	lcm_num[0]=0;
	lcm_num[1]=0;
	lcm_num[2]=0;
	lcm_num[3]=0;
	lcm_num[4]=0;
	lcm_num[5]=0;
	lcm_num[6]=0;
	lcm_num[6]=(int)(z/10000);
	z=z%10000;
	lcm_num[5]=(int)(z/1000);
	z=z%1000;
	lcm_num[4]=(int)(z/100);
	z=z%100;
	lcm_num[3]=(int)(z/10);
	z=z%10;
	lcm_num[2]=(int)(z/1);
	z=z%1;
	lcm_num[1]=(int)(z*10);
	z=z%0.1;
	lcm_num[0]=(int)(z*100);
	
	j=2;
	for(i=6;i>=2;i--)
	{
		if (lcm_num[i]!=0)
		{
			j=i;
			break;
		}
	}
	
	for(i=j;i>=0;i--)
	{
	wlcmd_4('d',lcm_num[i]);
	if(i==2) wlcmd_4('d','.');
	}
	_nop();
	return;
}
示例#18
0
int main(void) {
	WDTCTL = WDTPW | WDTHOLD;		// Stop watchdog timer

	// LED setup
//	P1OUT = 0;
//	P1DIR |= BIT0;

	// output SMCLK on P3.4
	P3SEL1 |= BIT4;
	P3DIR |= BIT4;

	// Set PJ for HF Crystal use
	PJSEL0 |= BIT6;
	PJSEL1 &= ~BIT6;

	// Configure CS module
	CSCTL0_H = 0xA5; 							// Write password to unlock clock registers
	CSCTL1 = DCOFSEL_0; 						// Set DCO to 1MHz
	CSCTL1 &= ~DCORSEL;
	CSCTL2 = SELS__HFXTCLK; 	// set SMCLK and MCLK to HFXTCLK 12MHz crystal oscillator
	CSCTL3 = DIVS__1; 				// set SMCLK divisor to 1, SMCLK = 12MHz
	CSCTL4 |= HFFREQ_1 + HFXTDRIVE_3;			// set HFX frequency to 8-16MHz, HFXTOFF = 0, turn it on
	//CSCTL4 &= ~HFXTOFF;
	CSCTL5 |= ENSTFCNT2;						// enable HF counter

	do {
		CSCTL5 &= ~HFXTOFFG;   					// Clear XT1 fault flag
		SFRIFG1 &= ~OFIFG;
	} while (SFRIFG1 & OFIFG);					// Test oscillator fault flag

	CSCTL0_H = 0;
	SFRIE1 |= OFIE; // enable oscillator fault interrupt. If there is a fault.... then what?

//	P1OUT |= BIT0;
	while(1);
	_nop();

	return 0;
}
void MtpSuspendService(BOOL bSetup, BYTE btDirection, BYTE _USB_MEM * pbtBuffer, WORD wLength,WORD wPrivateData)
{

    USHORT uStatus;
    BOOL   bLowPowerUsbStickyBitReadResult;

    if( usb_get_current_limit() > 100 )
    {
        usb_device_get_status(USB_STATUS,&uStatus);
        if(uStatus == USB_STATE_ADDRESSED)
        {   //If we're being suspended in the addressed state, that means we've likely asked for more current than allowed by the host
            //we're going to set a sticky bit (which we'll use when we reset to determine if we've been shutdown for too much
            //current requested), and then reset the part.
            // 8/19/04 - add one bit so that we have a counter - we want to try 500mA twice before failing.
            if( ReadStickyBit((volatile _X WORD *) &HW_RTC_PERSISTENT1, HW_RTC_PERSISTANT1_LOW_POWER2_USB_BITPOS,
                              &bLowPowerUsbStickyBitReadResult) != SUCCESS )
            {
                DebugBuildAssert(FALSE); // halts only in DEBUG builds.
            }
            // if 2nd try flag is set, we've failed the 2nd time, now drop down into 100mA mode.
            if (bLowPowerUsbStickyBitReadResult)
            {
                // Clear 2nd try flag
                if ( ClearStickyBit((volatile _X WORD *)&HW_RTC_PERSISTENT1,
                                    HW_RTC_PERSISTANT1_LOW_POWER2_USB_BITPOS)  != SUCCESS)
                {
                    DebugBuildAssert(FALSE); // halts only in DEBUG build, vanishes in RETAIL build.
                }
                // Set the 100mA flag.
                if( SetStickyBit((volatile _X WORD *)&HW_RTC_PERSISTENT1, HW_RTC_PERSISTANT1_LOW_POWER_USB_BITPOS) != SUCCESS )
                {
                    DebugBuildAssert(0); // halts only if DEBUG build.
                }
            } else
            {
                //set the 2nd try sticky bit.
                if( SetStickyBit((volatile _X WORD *)&HW_RTC_PERSISTENT1, HW_RTC_PERSISTANT1_LOW_POWER2_USB_BITPOS) != SUCCESS )
                {
                    DebugBuildAssert(0); // halts only if DEBUG build.
                }
            }

            usb_device_shutdown();
            for(uStatus =0; uStatus<10000; uStatus++)
                _nop();
            //reset the part
            SystemReset();
        }
        else
        {   //DebugBuildAssert(FALSE);   //!
        }
    }

    //Turn off LRADC
    HW_BATT_CTRL.B.PWD = 1;

#ifdef DCDC_POWER_TRANSFER
    //If using hand-off code, leave the DCDC circuitry on.
    HW_VDD5V_PWR_CHARGE.B.DCANA_LP = FALSE;
    HW_DCDC_TBR.B.DCDC2_STOPCLK = FALSE;
    HW_DCDC_TBR.B.DCDC1_STOPCLK = FALSE;
#else
    //If not using hand-off code, turn off circuitry.
    HW_VDD5V_PWR_CHARGE.B.DCANA_LP = TRUE;  //Turn off some unused circuitry in the dcdc converter
    HW_DCDC_TBR.B.DCDC2_STOPCLK = TRUE;  //Turn off DCDC#2
    HW_DCDC_TBR.B.DCDC1_STOPCLK = TRUE;  //Turn off DCDC#1
#endif

    HW_DCDC_TBR.B.DCDC_ANA_BGR_BIAS = TRUE;  //Switch over to the Vbg bias voltage
    HW_REF_CTRL.B.PWRDWNS = TRUE;       // Turn down the self bias circuit
    HW_REF_CTRL.B.BIASC=1;              // drops bias currents
    HW_REF_CTRL.B.LOW_PWR=1;            // and some more.

    HW_FLCR2.B.CLKOFF = 1;                // Turns Off clock to flash module
    HW_GPFLASH_CSR0R.B.CLK_DISABLE = 1;  // Turns OFF GPFLASH

    HW_DCDC_VDDD.B.BROWNOUT_ENABLE = 0;       // Disable the brownout
    HW_DCDC_VDDD.B.VOLTAGE_LEVEL=0x0a;  //Set core to 1.34 volts

#ifdef BATTERY_CHARGE
    BatteryChargeDisableCharging(FALSE);
#endif
}
示例#20
0
int32_t xPortStartScheduler( void )
{
extern void vTrapInstallHandlers( void );
uint32_t ulMFCR = 0UL;
uint32_t *pulUpperCSA = NULL;
uint32_t *pulLowerCSA = NULL;

	/* Interrupts at or below configMAX_SYSCALL_INTERRUPT_PRIORITY are disable
	when this function is called. */

	/* Set-up the timer interrupt. */
	prvSetupTimerInterrupt();

	/* Install the Trap Handlers. */
	vTrapInstallHandlers();

	/* Install the Syscall Handler for yield calls. */
	if( 0 == _install_trap_handler( portSYSCALL_TRAP, prvTrapYield ) )
	{
		/* Failed to install the yield handler, force an assert. */
		configASSERT( ( ( volatile void * ) NULL ) );
	}

	/* Enable then install the priority 1 interrupt for pending context
	switches from an ISR.  See mod_SRC in the TriCore manual. */
	CPU_SRC0.reg = 	( portENABLE_CPU_INTERRUPT ) | ( configKERNEL_YIELD_PRIORITY );
	if( 0 == _install_int_handler( configKERNEL_YIELD_PRIORITY, prvInterruptYield, 0 ) )
	{
		/* Failed to install the yield handler, force an assert. */
		configASSERT( ( ( volatile void * ) NULL ) );
	}

	_disable();

	/* Load the initial SYSCON. */
	_mtcr( $SYSCON, portINITIAL_SYSCON );
	_isync();

	/* ENDINIT has already been applied in the 'cstart.c' code. */

	/* Clear the PSW.CDC to enable the use of an RFE without it generating an
	exception because this code is not genuinely in an exception. */
	ulMFCR = _mfcr( $PSW );
	ulMFCR &= portRESTORE_PSW_MASK;
	_dsync();
	_mtcr( $PSW, ulMFCR );
	_isync();

	/* Finally, perform the equivalent of a portRESTORE_CONTEXT() */
	pulLowerCSA = portCSA_TO_ADDRESS( ( *pxCurrentTCB ) );
	pulUpperCSA = portCSA_TO_ADDRESS( pulLowerCSA[0] );
	_dsync();
	_mtcr( $PCXI, *pxCurrentTCB );
	_isync();
	_nop();
	_rslcx();
	_nop();

	/* Return to the first task selected to execute. */
	__asm volatile( "rfe" );

	/* Will not get here. */
	return 0;
}
示例#21
0
文件: main.c 项目: iOperator/FRAMpen
void main(void) {

	init_system();

	volatile enum states state;
	state = IDLE;
	volatile enum button_states button_state;
	button_state = NO_PUSH;

	__enable_interrupt();

	while(1) {

		/* Check push button */
		if (button_flag == 1) {
			button_flag = 0;

			// Check push button
			unsigned int push_cnt = 0;
			button_state = NO_PUSH;
			__delay_cycles(200000);  // Wait 25 msec to debounce
			__delay_cycles(200000);  // Wait 25 msec to debounce
			while (!(P4IN & BIT0)) {  // Button pushed
				__delay_cycles(8000);  // Wait 1 msec
				push_cnt++;

				if (push_cnt > TIME_SHORT_PUSH) {  // Push button was pushed a short time
					button_state = SHORT_PUSH;
					LED1_on();
				}
				if (push_cnt > TIME_LONG_PUSH) {  // Push button was pushed a long time
					button_state = LONG_PUSH;
					LED2_on();
				}
			}
			LED1_off();
			LED2_off();
			__delay_cycles(400000);  // Wait 50 msec to debounce
			__delay_cycles(400000);  // Wait 50 msec to debounce
		}


		/* Check states */
		switch (state) {
			case IDLE:
//				toggle_led(LED1_PIN, TIME_3SEC);
				switch (button_state) {
					case LONG_PUSH:
						state = TRANSMIT;
						wdt_disable();
						LED1_off();
						toggle_led(LED2_PIN, TIME_05SEC);
						transmit_data();
						break;
					case SHORT_PUSH:
						state = RECORD;
						wdt_disable();
						LED2_off();
						toggle_led(LED1_PIN, TIME_1SEC);
						record_data();
						break;
					default:
						break;
				}
				break;

			case RECORD:
				switch (button_state) {
					case LONG_PUSH:  // Delete data
						delete_data();
						state = IDLE;
						break;
					case SHORT_PUSH:  // Stop recording
						// Recording stop from inside recorde()
						state = IDLE;
						break;
					default:
						break;
				}
//				state = IDLE;
				break;
			case TRANSMIT:
				switch (button_state) {
					case LONG_PUSH:
						break;
					case SHORT_PUSH:
						break;
					default:
						break;
				}
				state = IDLE;
				break;
			default:
				break;
		}

		//button_state = NO_PUSH;
		PAIE |= BIT0;  // Enable interrupt for push button

		if (state == IDLE) {
			toggle_led(LED1_PIN, TIME_3SEC);
			wdt_enable();  // Activate auto-deep-sleep-watchdog
			LPM3;
			_nop();
		}

	}

}
示例#22
0
int main(void)
{
	uint16_t crc, data;
	uint8_t index, lo, hi;
	uint8_t tx_count;
	uint8_t flags;
	uint8_t *TxDataBuffer;
	const uint8_t *RxDataBuffer;
	uint8_t len;

	/* enable pull-up on RA3 (for boot mode detection) */
	OPTION_REGbits.nWPUEN = 0;
	WPUA = (1 << 3);

	/* magic numbers from Microchip */
	OSCTUNE = 0;
	OSCCON = 0xFC; /* 16MHz HFINTOSC with 3x PLL enabled (48MHz operation) */
	ACTCON = 0x90; /* Enable active clock tuning with USB */

	/*
	pull-up on RA3 needs a moment to do its thing
	so, now is a good time to spend time computing a CRC of the user-programmable area
	*/

	flags = 0;

	/* initialize CRC */
	crc = 0;

	/* use local copy of PMADRH:PMADRL to prevent XC8 compilation issues */
	lo = 0x00;
	hi = 0x10;

	/*
	There are two possible CRC locations, and either if valid will boot user code:
	0x1FFF: the very end of the user code
	0x1F7F: the last address of the normal endurance memory
	*/

	for (;;)
	{
		PMADRH = hi;
		PMADRL = lo;

		/* clear CFGS (we're reading Program, not Configuration, Memory) */
		PMCON1bits.CFGS = 0;
		/* set RD (initiate read) */
		PMCON1bits.RD = 1;
		/* mandatory two nops */
		_nop(); _nop();

		/* retrieve result from program memory and put it into a WORD */
		data = PMDATH;
		data <<= 8;
		data += PMDATL;

		/* increment program memory address, and bail if we've just read the last word */
		lo++;
		if (0 == lo)
		{
			hi++;
		}

		if (
		( (0x80 == lo) && (0x1F == hi) ) ||
		( (0x00 == lo) && (0x20 == hi) )
		)
		{
			if (data == crc)
			{
				/* we've reached one of the two valid CRC locations *and* the CRC passes, so we note this */
				/* we choose NOT to exit the loop here in order to have a consistent amount of time for the pull-up on RA3 */
				flags |= FLAG_PASSED_CRC;
			}
			if (0x20 == hi) /* check if we've reached the final address */
			{
				/* we've reached the last word, so we must bail */
				break;
			}
		}
        
		/* update CRC over the 14 bits of program memory data */
		for (index = 0; index < 14; index++)
		{
			if ((crc & 0x0001) ^ (data & 0x0001))
				crc = (crc >> 1) ^ 0x23B1;
			else
				crc >>= 1;
			data >>= 1;
		}
	}
示例#23
0
文件: main.c 项目: hozmaster/octaneos
//
// Start Kernel
asmlinkage void start_kernel(void) {

  char buf[255];
  int _i = 0;
  int _diff = 0;
  _video_draw_char('O', 79, 10);
  _video_draw_char('c', 79, 11);
  _video_draw_char('t', 79, 12);
  _video_draw_char('a', 79, 13);
  _video_draw_char('n', 79, 14);
  _video_draw_char('e', 79, 15);
  _video_draw_char(' ', 79, 16);
  _video_draw_char(' ', 79, 17);

  // Draw a banner line of the right side of the screen
  for (_i = 0; _i < 10; _i++)  { _video_draw_char(' ', 79,_i); }
  for (_i = 18; _i < 22; _i++) { _video_draw_char(' ', 79,_i); }  
   
  load_misc_kprint();  
  __sprintf(buf, "=========== Octane ===========\n"); __puts(buf);
  __sprintf(buf, "@INFO: Build: %ld; since (10/15/2007) %s\n", 
		  	main_get_build_count, main_get_version); __puts(buf);

  //memory_end = (1<<20) + (EXT_MEM_K<<10);
  //  memory_end &= PAGE_MASK;
  memory_end = 16*1024*1024;
  memory_start = 1024*1024;
  low_memory_start = (unsigned long) &_end;

  // now lets load the IDT
  load_exception_table();

  init_interrupts();
  sched_init();
  load_keyboard_driver();
  
  //memory_start = blk_dev_init(memory_start, memory_end);
  sti();
  //calibrate_delay();
  
  //memory_start = inode_init(memory_start, memory_end);
  //memory_start = file_table_init(memory_start, memory_end);  
  //mem_init(low_memory_start, memory_start, memory_end);
  //buffer_init();
  time_init();
 
  // Print the current time.
  printk("INFO: current time: %d\n", xtime.tv_sec);
  printk("INFO: %d/%d/%d %d:%d\n", cur_simpletime.mon,
		 cur_simpletime.day, cur_simpletime.year,
		 cur_simpletime.hour, cur_simpletime.min);

  floppy_init();
  sti();

  //__debug_floppy_open();
  //__debug_floppy_release();
  test_time_1();
  test_block_1();

  tests();
  for(;;) {  
	  _nop();
  }
}
示例#24
0
文件: aram.c 项目: comex/libogc
static void __ARCheckSize()
{
	u32 i,arsize,arszflag;
	static u32 test_data[8] ATTRIBUTE_ALIGN(32);
	static u32 dummy_data[8] ATTRIBUTE_ALIGN(32);
	static u32 buffer[8] ATTRIBUTE_ALIGN(32);

#ifdef _AR_DEBUG
	printf("__ARCheckSize()\n");
#endif

	while(!(_dspReg[11]&0x0001));

	__ARSize = __ARInternalSize = arsize  = 0x1000000;
	_dspReg[9] = (_dspReg[9]&~0x3f)|0x23;

	for(i=0;i<8;i++) {
		test_data[i] = 0xBAD1BAD0;
		dummy_data[i] = 0xDEADBEEF;
	}
	DCFlushRange(test_data,32);
	DCFlushRange(dummy_data,32);
	
	__ARExpansionSize = 0;
	__ARWriteDMA((u32)test_data,0x1000000,32);
	__ARWriteDMA((u32)test_data,0x1200000,32);
	__ARWriteDMA((u32)test_data,0x2000000,32);
	__ARWriteDMA((u32)test_data,0x1000200,32);
	__ARWriteDMA((u32)test_data,0x1400000,32);

	memset(buffer,0,32);
	DCFlushRange(buffer,32);

	__ARWriteDMA((u32)dummy_data,0x1000000,32);

	DCInvalidateRange(buffer,32);
	__ARReadDMA((u32)buffer,0x1000000,32);
	_nop();

	arszflag = 0x03;
	if(buffer[0]==dummy_data[0]) {
		memset(buffer,0,32);
		DCFlushRange(buffer,32);
		__ARReadDMA((u32)buffer,0x1200000,32);
		_nop();
		if(buffer[0]==dummy_data[0]) {
			__ARExpansionSize = 0x200000;
			arsize +=  0x200000;
			goto end_check;				//not nice but fast
		}
		
		memset(buffer,0,32);
		DCFlushRange(buffer,32);
		__ARReadDMA((u32)buffer,0x2000000,32);
		_nop();
		if(buffer[0]==dummy_data[0]) {
			__ARExpansionSize = 0x400000;
			arsize +=  0x400000;
			arszflag |= 0x08;
			goto end_check;				//not nice but fast
		}
		
		memset(buffer,0,32);
		DCFlushRange(buffer,32);
		__ARReadDMA((u32)buffer,0x1400000,32);
		_nop();
		if(buffer[0]==dummy_data[0]) {
			__ARExpansionSize = 0x1000000;
			arsize +=  0x1000000;
			arszflag |= 0x18;
		} else {
			__ARExpansionSize = 0x2000000;
			arsize +=  0x2000000;
			arszflag |= 0x20;
		}
end_check:
		_dspReg[9] = (_dspReg[9]&~0x3f)|arszflag;
	}

#ifdef _AR_DEBUG
	printf("__ARCheckSize(%d)\n",arsize);
#endif
	*(u32*)0x800000d0 = arsize;
	__ARSize = arsize;
}
_reentrant INT STFM1000I2CReset(WORD mode, WORD ClockDiv)
{
    int Speed;
    int cnt;
    int iI2CWord3;
    int RtnCode = 0;
    
    HW_GP0ENR.B.B16 = 0;        // enable output
    HW_GP0ENR.B.B17 = 0;        // enable output

    HW_GP0PWR.B.B16 = 1;        // power on
    HW_GP0PWR.B.B17 = 1;        // power on
     
#if 1
    for(cnt = 0; (0 != HW_I2CCSR.B.BUSY); cnt++)    // Delay time 1 cnt
    {    
        if (TIMEOUT_COUNT <= cnt)
        {
            HW_I2CCSR.I = 0;	// Reset I2C device
            HW_I2CCSR.B.TREQ = 1;
            HW_I2CCSR.B.TREQ = 0;
            HW_I2CCSR.B.I2C_EN = 1;
            HW_I2CCSR.B.ACKF = 0;         
            break;
        }
    } 
#else
    for(cnt = 0; (0 != HW_I2CCSR.B.BUSY); cnt++)    // Delay time 1 cnt
    {    
        if (TIMEOUT_COUNT <= cnt) RtnCode = -3;	//return(TIMEOUT_ERROR);
    } 
#endif
    
    for(cnt = 0; cnt < 100; cnt++) { _nop(); }  // Short delay  
 
    HW_I2CCSR.I = 0;	// Reset I2C device
  	
    iI2CWord3 = HW_I2CCSR.I;	// verify that reset worked
    
    if ( 0 != (RESET_HW_I2CCSR_MASK & iI2CWord3) )
    {
      	 RtnCode = -1;
    }       
 
    HW_I2CCSR.B.I2C_EN = 1;	 	// Enable I2C device
    
    if (0 == ClockDiv)         // lookup Clock Div based on System Clock Speed
    {
        Speed = SysGetSpeed();
        switch(Speed)
        {
            case SPEED_IDLE: ClockDiv = I2C_IDLE_SPEED;          	break;  
            case SPEED_FM:   ClockDiv = I2C_FM_SPEED;               break; 
            case SPEED_FM_EQ:   ClockDiv = I2C_FM_EQ_SPEED;         break;         
            case SPEED_MP3: ClockDiv = I2C_MP3_SPEED;           	break;
            case SPEED_ADPCM: ClockDiv = I2C_ADPCM_SPEED;         	break;
            case SPEED_MIXER: ClockDiv = I2C_MIXER_SPEED;         	break;
            case SPEED_ADCBASE: ClockDiv = I2C_ADCBASE_SPEED;       break;
            case SPEED_MAX: ClockDiv = I2C_MAX_SPEED;           	break;
            case SPEED_WMA: ClockDiv = I2C_WMA_SPEED;           	break;
            case SPEED_MP3ENCODE: ClockDiv = I2C_MP3ENCODE_SPEED;   break;
            case SPEED_PEAK: 
            default: ClockDiv = I2C_PEAK_SPEED;         			break;  
         } 
    }  
           
    //HW_I2CDIV.I = (ClockDiv<<1) & 0x0001FE;   // Clock Divider Register (e.g. 0x78) Write Only 
    HW_I2CDIV.B.FACT = ClockDiv;                // Cannot be read, we need to set bit[8:1]
                                                
    HW_I2CCSR.B.MODE = mode;    // Operating Mode Bit  1=Fast

    if (1 == HW_I2CCSR.B.ROFL) 	// Clear Receiver Overflow
    {
    	cnt = HW_I2CDAT.U;
    	HW_I2CCSR.B.ROFLCL = 1;
    	HW_I2CCSR.B.ROFLCL = 0;
    }
    
    if (1 == HW_I2CCSR.B.TUFL) 	// Clear Transmitter Undererflow
    {
    	HW_I2CDAT.U = 0;
    	HW_I2CCSR.B.TUFLCL = 1;
    	HW_I2CCSR.B.TUFLCL = 0;
    }
  
    return(RtnCode);
}
示例#26
0
文件: editing.c 项目: dbarnett/wiimu
s32 extractChannelContents(u64 titleID, char* location)
{
	u32 TitleIDH=TITLE_UPPER(titleID);
	u32 TitleIDL=TITLE_LOWER(titleID);
	if(ES_SetUID(titleID)<0)
		return ERR_UID;

	u32 tmd_size ATTRIBUTE_ALIGN(32);


	if(ES_GetStoredTMDSize(titleID, &tmd_size)<0)
		return ERR_TMDSIZE;

	signed_blob *TMD = (signed_blob *)memalign( 32, tmd_size );
	memset(TMD, 0, tmd_size);

	if(ES_GetStoredTMD(titleID, TMD, tmd_size)<0)
	{
		free(TMD);
		return ERR_TMD;
	}

	u32 cnt ATTRIBUTE_ALIGN(32);

	if(ES_GetNumTicketViews(titleID, &cnt)<0)
	{
		free(TMD);
		return ERR_TIKCOUNT;
	}

	if( cnt <= 0 )
	{
		free(TMD);
		return ERR_TIKCOUNT;
	}

	tikview *views = (tikview *)memalign( 32, sizeof(tikview)*cnt );
	if(ES_GetTicketViews(titleID, views, cnt)<0)
	{
		free(views);
		free(TMD);
		return ERR_TIK;
	}
	printf("Allocated and filled views.\n");
	sleep(3);

	Identify_SU();

	int z;
	tmd_content *TMDc = TMD_CONTENTS(((tmd*)(SIGNATURE_PAYLOAD(TMD))));		// OH GOD CREDIAR, WTF WAS THAT MESS!
	// List format is "XXXXXXXX = YYYYYYYY" where X is index, and Y is cid.
	char *lookup_list=calloc(21,((tmd*)(SIGNATURE_PAYLOAD(TMD)))->num_contents);
	ClearScreen();
	printf("\nNumber of contents: %d\n",((tmd*)(SIGNATURE_PAYLOAD(TMD)))->num_contents);
	sleep(1);
	for(z=0; z < ((tmd*)(SIGNATURE_PAYLOAD(TMD)))->num_contents; z++)
	{
		/* Get Content */
		char nameDirectory[80];
		sprintf(nameDirectory,"/title/%08x/%08x/content/%08x.app",TitleIDH,TitleIDL,TMDc[z].cid);
		s32 contentFd=ISFS_Open(nameDirectory,ISFS_OPEN_READ);
		u8 *data=calloc(TMDc[z].size,1);
		if(contentFd<0)
		{
			switch(contentFd)
			{
				case ISFS_EINVAL:
					printf("FAILED! (Invalid Argument %s)\n\tQuitting...\n",nameDirectory);
					sleep(5);
					Finish(1);
					break;
			
				case ISFS_ENOMEM:
					printf("FAILED! (Out of memory %s)\n\tQuitting...\n",nameDirectory);
					sleep(5);
					Finish(1);
					break;
			
				default:
					goto skip;
//					Finish(1);
					break;
			}
		}
		int isUnaligned = ((int)data)%32 | TMDc[z].size%32;
		int alignedLen = (TMDc[z].size+31)&0xffffffe0;
		unsigned char* alignedBuf;
		if(isUnaligned) alignedBuf = memalign(32, alignedLen);
		else alignedBuf = data;
		ISFS_Seek(contentFd,0,0);
		ISFS_Read(contentFd, alignedBuf, alignedLen);
		// If it was unaligned, you have to copy it and clean up
		if(isUnaligned){
			memcpy(data, alignedBuf, TMDc[z].size);
			free(alignedBuf);
		}
		ISFS_Close(contentFd);

		// Do copying here.
		// data is the actual content data (use fwrite with it :P).
		// Copy the file with it's index as it's filename
		char* destination=calloc(sizeof(location)+14, 1);
		char lookup_entry[21];
		sprintf(destination, "%s/%08x.app",location,TMDc[z].index);
		sprintf(lookup_entry, "%08x = %08x\n",TMDc[z].index,TMDc[z].cid);
		strcat(lookup_list, lookup_entry);
		printf("Got destination as: %s\n", destination);
		sleep(3);
		FILE *dfd=fopen(destination,"wb+");
		printf("Opened %s\n", destination);
		sleep(3);
//		free(destination);
		fwrite(data, TMDc[z].size, 1, dfd);
		printf("Wrote data to %s\n", destination);
		sleep(3);
		fclose(dfd);
		printf("Closed %s\n", destination);
		sleep(2);
skip:
		_nop();
	}
	// Make a file containing the lookups called files.txt
	char* lookup_file=calloc(sizeof(location)+14, 1);
	sprintf(lookup_file, "%s/files.txt",location);
	printf("Got destination as: %s\n", lookup_file);
	sleep(3);
	FILE* lfd=fopen(lookup_file,"wb+");
	printf("Opened %s\n", lookup_file);
	sleep(3);
//	free(lookup_file);
	fwrite(lookup_list, 21, ((tmd*)SIGNATURE_PAYLOAD(TMD))->num_contents, lfd);
	printf("Wrote lookups to %s\n", lookup_file);
	sleep(3);
	fclose(lfd);
	printf("Closed %s\n", lookup_file);
	sleep(2);
	printf("Freed TMD and views");
	sleep(1);