Exemplo n.º 1
0
Arquivo: main.c Projeto: 12019/openpcd
int main(void)
{
	/* initialize LED and debug unit */
	led_init();
	sysirq_init();
	AT91F_DBGU_Init();

	AT91F_PIOA_CfgPMC();
	wdt_init();
	pit_init();
	blinkcode_init();

	/* initialize USB */
	req_ctx_init();
	usbcmd_gen_init();
	udp_open();

	/* call application specific init function */
	_init_func();

	// Enable User Reset and set its minimal assertion to 960 us
	AT91C_BASE_RSTC->RSTC_RMR =
	    AT91C_RSTC_URSTEN | (0x4 << 8) | (unsigned int)(0xA5 << 24);

#ifdef DEBUG_CLOCK_PA6
	AT91F_PMC_EnablePCK(AT91C_BASE_PMC, 0, AT91C_PMC_CSS_PLL_CLK);
	AT91F_PIO_CfgPeriph(AT91C_BASE_PIOA, 0, AT91C_PA6_PCK0);
#endif

	/* switch on first led */
	led_switch(2, 1);

	DEBUGPCRF("entering main (idle) loop");
	while (1) {
		/* Call application specific main idle function */
		_main_func();
		dbgu_rb_flush();
		
		/* restart watchdog timer */
		wdt_restart();
#ifdef CONFIG_IDLE
		//cpu_idle();
#endif
	}
}
Exemplo n.º 2
0
//*--------------------------------------------------------------------------------------
//* Function Name       : main
//* Object              :
//*--------------------------------------------------------------------------------------
int main ( void )
{
//    char button = 0;
//    int x = 0, y = 0, j = 0;
//    int adc;
//    int i = 0;
    int x,y,z,i;

    button_flag = 0;

    //Init trace DBGU
    //AT91F_DBGU_Init();
    //AT91F_DBGU_Printk("\n\r-I- BasicUSB 1.1 (USB_DP_PUP) \n\r0) Set Pull-UP 1) Clear Pull UP\n\r");

    //Enable RESET
    m_pRSTC->RSTC_RCR = 0xA5000008;
    m_pRSTC->RSTC_RMR = 0xA5000001;
    Delay(1000);

    // Init USB device
    AT91F_USB_Open();

    // Configure the RTT:
    *AT91C_RTTC_RTMR = BUTTON_SAMPLING;

    // Set in PIO mode and Configure in Input
    AT91F_PIOA_CfgPMC();
    // AT91F_PIO_CfgInput(AT91C_BASE_PIOA, (SW1|SW2));

   // Wait for the end of enumeration
   // while (!HID.IsConfigured(&HID));

    // InitMAM
    InitMMA();

    // InitADC
    ADCInit();

    // UART0 Init
    InitUSART0();

    // Stat led
    s_pPio->PIO_PER   = BIT31;
    // Configure PA31 as output
    s_pPio->PIO_OER   = BIT31;
    // Set PA31 to HIGH
    s_pPio->PIO_SODR  = BIT31;


    // Test flash
    if(TestFlash()) {

      for(i=0; i<20; i++) {
        s_pPio->PIO_CODR  = BIT31; Delay(150000);
        s_pPio->PIO_SODR  = BIT31; Delay(150000);
      }
    }

    // Button
    // Configure P20 as input
    s_pPio->PIO_ODR   = BIT20;
    // Enable
    s_pPio->PIO_PER   = BIT20;

    Delay(1000);

    // CALIBRATE WHEN BUTT IS PRESS
    // while((s_pPio->PIO_PDSR&BIT20)==BIT20);

    Delay(1000);

    // Get coordinates
    GetCoordinates();

    base_y = Coordinates[0]/NSAMPLE;
    base_x = Coordinates[1]/NSAMPLE;
    base_z = Coordinates[2]/NSAMPLE;


    // Start waiting some cmd
    while (1) {

      // Check enumeration
      if (HID.IsConfigured(&HID)) {

        // check button status
        if((s_pPio->PIO_PDSR&BIT20)==0) {

          // Set PA31 to LOW
          s_pPio->PIO_CODR  = BIT31;

          // set button flag
          button_flag = 0x01;

          Delay(1000);

        }
        else {

          // Set PA31 to HIGH
          // s_pPio->PIO_SODR  = BIT31;

          // clear button flag
          button_flag = 0x00;

        }


        GetCoordinates();

        Delay(10000);

//*        if((j++)==100) {
//*
//*         print_view = PRINT_COORDINATES;
//*         write_report_USART0();
//*
//*         // print_view = PRINT_DIVERSION;
//*         // write_report_USART0();
//*
//*         j=0;
//*        }

        y = 478 - Coordinates[0]/NSAMPLE;
        // y = base_y - Coordinates[0]/NSAMPLE;

        if(y>40) {
          if(y<50)
            HID.SendReport(&HID, button_flag, 0, -1);
          else if (y<100)
            HID.SendReport(&HID, button_flag, 0, -2);
          else if (y<150)
            HID.SendReport(&HID, button_flag, 0, -3);
          else if (y<200)
            HID.SendReport(&HID, button_flag, 0, -6);
          else
            HID.SendReport(&HID, button_flag, 0, -16);
        }
        else if(y<-40){
        if(y>-50)
            HID.SendReport(&HID, button_flag, 0, 1);
          else if (y>-100)
            HID.SendReport(&HID, button_flag, 0, 2);
          else if (y>-150)
            HID.SendReport(&HID, button_flag, 0, 3);
          else if (y>-200)
            HID.SendReport(&HID, button_flag, 0, 6);
          else
            HID.SendReport(&HID, button_flag, 0, 16);

        }

        x = 565 - Coordinates[1]/NSAMPLE;
        // x = base_x - Coordinates[1]/NSAMPLE;

        if(x>40) {
          if(x<50)
            HID.SendReport(&HID, button_flag, -1, 0);
          else if (x<100)
            HID.SendReport(&HID, button_flag, -2, 0);
          else if (x<150)
            HID.SendReport(&HID, button_flag, -3, 0);
          else if (x<200)
            HID.SendReport(&HID, button_flag, -6, 0);
          else
            HID.SendReport(&HID, button_flag, -16, 0);
        }
        else if(x<-40){
          if(x>-50)
            HID.SendReport(&HID, button_flag, 1, 0);
          else if (x>-100)
            HID.SendReport(&HID, button_flag, 2, 0);
          else if (x>-150)
            HID.SendReport(&HID, button_flag, 3, 0);
          else if (x>-200)
            HID.SendReport(&HID, button_flag, 6, 0);
          else
            HID.SendReport(&HID, button_flag, 16, 0);

        }

        // just test Z coordinate
        z = Coordinates[2]/NSAMPLE;

        if(z<300) {
          s_pPio->PIO_CODR  = BIT31;
        }
        else {
          s_pPio->PIO_SODR  = BIT31;
        }

        Delay(20);



//*      x = ((150) - GetADC4())/20;
//*      HID.SendReport(&HID, button, x, 0);
//*      Delay(50);
//*      HID.SendReport(&HID, button, ((base_y) - GetADC5())/50, 0);
//*      Delay(50);
//*      HID.SendReport(&HID, button, ((base_z) - GetADC6())/50, 0);
//*      Delay(50);
//*
//*       button = 0;
//*
//*       for(j=0; j<100; j++) {
//*           HID.SendReport(&HID, button, 1, 0);
//*           Delay(50);
//*       }
//*
//*       for(j=0; j<100; j++) {
//*           HID.SendReport(&HID, button, 0, 1);
//*           Delay(50);
//*       }
//*
//*       for(j=0; j<100; j++) {
//*         HID.SendReport(&HID, button, -1, 0);
//*         Delay(50);
//*       }
//*
//*       for(j=0; j<100; j++) {
//*         HID.SendReport(&HID, button, 0, -1);
//*         Delay(50);
//*       }

      }
   }
}
Exemplo n.º 3
0
unsigned char canInit(unsigned int bitrate)
/******************************************************************************
Initialize the hardware to receive CAN messages and start the timer for the
CANopen stack.
INPUT	
OUTPUT	
******************************************************************************/
{
  unsigned char i;
  AT91S_CAN_MB *mb_ptr = AT91C_BASE_CAN_MB0;

  // Enable CAN PIOs
  AT91F_CAN_CfgPIO();
  // Enable CAN Clock
  AT91F_CAN_CfgPMC();

  // Enable CAN Transceiver
  AT91F_PIOA_CfgPMC();

  // Init CAN Interrupt Source Level
  AT91F_AIC_ConfigureIt(AT91C_BASE_AIC,				// CAN base address
                        AT91C_ID_CAN,				// CAN ID
                        AT91C_AIC_PRIOR_HIGHEST,		// Max priority
                        AT91C_AIC_SRCTYPE_INT_HIGH_LEVEL,	// Level sensitive
                        can_irq_handler);			// C Handler

  AT91F_AIC_EnableIt(AT91C_BASE_AIC, AT91C_ID_CAN);

  if (bitrate <= 500)
  {
    // CANopen 10..500 kbit with 16 tq, sample point is at 14 tq
    // all values are added to 1 by hardware
    // Resynchronisation jump width (SJW)	= 1 tq
    // Propagation Time Segment (PRS)		= 5 tq
    // Phase Segment 1 (PHS1)			= 8 tq
    // Phase Segment 2 (PHS2)			= 2 tq
    // Total					= 16 tq
    AT91F_CAN_CfgBaudrateReg(AT91C_BASE_CAN,
                             (AT91C_MASTER_CLOCK/16/1000/bitrate - 1) << 16 | 0x0471);
  }
  else
    return 0;

  // Enable CAN and Wait for WakeUp Interrupt
//  AT91F_CAN_EnableIt(AT91C_BASE_CAN, AT91C_CAN_WAKEUP);
  AT91F_CAN_CfgModeReg(AT91C_BASE_CAN, AT91C_CAN_CANEN);

  // Reset all mailsboxes (MBs), filters are zero (accept all) by clear all MB
  // Set the lower MBs as rx buffer
  for (i = 0; i < NB_RX_MB; i++, mb_ptr++)
  // Configure receive MBs as receive buffer, last as receive overwrite
    AT91F_InitMailboxRegisters(mb_ptr,
				((i < (NB_RX_MB - 1)) ? AT91C_CAN_MOT_RX : AT91C_CAN_MOT_RXOVERWRITE)
				| AT91C_CAN_PRIOR,	// Mailbox Mode Reg
				0x00000000,		// Mailbox Acceptance Mask Reg
				0x00000000,		// Mailbox ID Reg
				0x00000000,		// Mailbox Data Low Reg
				0x00000000,		// Mailbox Data High Reg
				0x00000000);		// Mailbox Control Reg
  for (   ; i < NB_MB; i++, mb_ptr++)
  // Configure transmit MBs
    AT91F_InitMailboxRegisters(mb_ptr,
				AT91C_CAN_MOT_TX
				| AT91C_CAN_PRIOR,	// Mailbox Mode Reg
				0x00000000,		// Mailbox Acceptance Mask Reg
				0x00000000,		// Mailbox ID Reg
				0x00000000,		// Mailbox Data Low Reg
				0x00000000,		// Mailbox Data High Reg
				0x00000000);		// Mailbox Control Reg
  // Enable Reception on all receive Mailboxes
  AT91F_CAN_InitTransferRequest(AT91C_BASE_CAN, RX_INT_MSK);
  // Enable all receive interrupts
  AT91F_CAN_EnableIt(AT91C_BASE_CAN, RX_INT_MSK);
  return 1;
}