int main(void)
{
	//code tested on Xmega32A4U
	PORTA.DIR = 0xFF; //port A as output for indication in a led on pin 1
	//Configures interrupt 0 input in pin 2 of the port B in falling edge
	InterruptConfig(&PORTB,pin,PIN2_bm,PORT_ISC_FALLING_gc,0);
    while(1);
}
Ejemplo n.º 2
0
void Board_main(void)
{   
  
  
    uint8_t i;
    uint16_t flash_flag = 0;
    uint16_t copy_file_flag = 0;
    
    /* RCC system reset(for debug purpose) */
    RCC_DeInit();
    
    /* Enable HSE */
    RCC_HSEConfig(RCC_HSE_ON);
    
    /* Wait till HSE is ready */
    HSEStartUpStatus = RCC_WaitForHSEStartUp();
    
    if(HSEStartUpStatus == SUCCESS)
    {
    #if 0
        /* Enable Prefetch Buffer */
        FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
	FLASH_HalfCycleAccessCmd(FLASH_HalfCycleAccess_Disable);
        /* Flash 2 wait state */
        FLASH_SetLatency(FLASH_Latency_2);

	#else
     FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
	FLASH_HalfCycleAccessCmd(FLASH_HalfCycleAccess_Enable);
	        /* Flash 2 wait state */
        /*FLASH_SetLatency(FLASH_Latency_2);*/
	FLASH_SetLatency(FLASH_Latency_0);	/*abin@ */
	#endif
        

        
        /* HCLK = SYSCLK */
        RCC_HCLKConfig(RCC_SYSCLK_Div1); 
        
        /* PCLK2 = HCLK */
        RCC_PCLK2Config(RCC_HCLK_Div1); 
        
        /* PCLK1 = HCLK/2 */
        RCC_PCLK1Config(RCC_HCLK_Div2);


	#if 0
        /* PLLCLK = 8MHz * 9 = 72 MHz */
	/*abin:note*/
        RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
//        RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_8);
        
        /* Enable PLL */ 
        RCC_PLLCmd(ENABLE);
      
        /* Wait till PLL is ready */
        while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
        {
        }
        
        /* Select PLL as system clock source */
        RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
	        /* Wait till PLL is used as system clock source */
        while(RCC_GetSYSCLKSource() != 0x08)
        {
        }	
	#else
 RCC_PLLCmd(DISABLE);
	        /* Select HSE as system clock source */
        RCC_SYSCLKConfig(RCC_SYSCLKSource_HSE);
	        /* Wait till PLL is used as system clock source */
        while(RCC_GetSYSCLKSource() != 0x04)
        {
        }	
         #endif
		  

    }
    
    /* Enable GPIOA, GPIOB, and AFIO clocks */
    RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA |RCC_APB2Periph_GPIOB| RCC_APB2Periph_AFIO, ENABLE);
    
        /* Enable DMA1 ,DMA2 clock */
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1|RCC_AHBPeriph_DMA2, ENABLE);
    
    /* Enable ADC1 ADC2,and GPIOC clock */
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 |RCC_APB2Periph_ADC2| RCC_APB2Periph_GPIOC, ENABLE);


    /* Enable PWR and BKP clock */
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
  
    /* Enable write access to Backup domain */
    PWR_BackupAccessCmd(ENABLE);
    
    /* Clear Tamper pin Event(TE) pending flag */
    BKP_ClearFlag();

     #if ABIN_DEBUG
  USART_InitStructure.USART_BaudRate = 115200;
  USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  USART_InitStructure.USART_StopBits = USART_StopBits_1;
  USART_InitStructure.USART_Parity = USART_Parity_No;
  USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

  STM_EVAL_COMInit(COM1, &USART_InitStructure);

  /* Output a message on Hyperterminal using printf function */
  //sys_printf("\n\r abin 8M is ok moify flash latency :USART Init end \n\r");    
    
#endif 
  
    /*------------------- Resources Initialization -----------------------------*/
    /* GPIO Configuration */
    GPIO_Config();
  //sys_printf("\n\r abin mul2 :GPIO_Config \n\r");    

#if ABIN_CPU_UPGRADE2HIGHSPEED_OPTION
   USB_Plugin_State = GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_14);
   if(USB_Plugin_State == 1)
   {
       //sys_printf("\n\r abin switch to high speed !!\n\r"); 
      UpgradeToHighSpeed(); 
     
   }
#endif

    /* Interrupt Configuration */
    InterruptConfig();
    
    /* Configure the systick */    
    SysTick_Configuration();
    
    /*------------------- Drivers Initialization -------------------------------*/
    
    /* Initialize the Low Power application */
    LowPower_Init();
    
    
    /* Enable WKUP pin */
    PWR_WakeUpPinCmd(ENABLE);
    
    /* Allow access to BKP Domain */
    PWR_BackupAccessCmd(ENABLE);
    
    
    RTC_Init();  
    
    
    /* If HSE is not detected at program startup */
    if(HSEStartUpStatus == ERROR)
    {
        /* Generate NMI exception */
        SCB->ICSR |= SCB_ICSR_NMIPENDSET;
    }  
    

    USB_Disconnect_Config();       
    GPIO_SetBits(USB_DISCONNECT, USB_DISCONNECT_PIN);/*???abin@20100714*/
    
    WakupPin_Init();
    
          //sys_printf("\n\r abin before CheckPowerOnReason\n\r"); 
    CheckPowerOnReason();


      //sys_printf("\n\r abin after CheckPowerOnReason\n\r");    
    
      
    Board_ADC_Init();        

        
    /*init the flag*/  
    flash_flag = *(uint16_t *)FLASH_READY_ADDRESS;
    if( flash_flag != FLAG_FLASH_READY)
    {
    	     //sys_printf("\n\r abin ready to erase flash \n\r");  
        FLASH_Unlock();
        FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR);	        
        //Erase the 32 page 32K
        for(i = 0;i< DATA_PAGES;i++)
        {
        	 //sys_printf("\n\r abin ready to erase aa"); 
            FLASH_ErasePage(DATA_LOGGER_ADDRESS_START + i * 1024);/*数据在Page1...page32存放*/
        }
        FLASH_ErasePage(FLASH_READY_ADDRESS); /*abin@20100715 没添加保护*/
        FLASH_ProgramHalfWord(FLASH_READY_ADDRESS , FLAG_FLASH_READY);

        FLASH_ErasePage(REOCRD_COUNT_ADDRESS);        
        FLASH_ProgramHalfWord(REOCRD_COUNT_ADDRESS , 0x0000);
        
        FLASH_Lock();
         //sys_printf("\n\r abin mul2 :erase flash end!!!\n\r"); 
    }
    
     //sys_printf("\n\r abin ready to erase flash end \n\r");  
    USB_Plugin_State = GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_14);
    
    if(USB_Plugin_State == 0)
    {
        
      
        record_count =   *(uint16_t *)REOCRD_COUNT_ADDRESS;
        
        if(record_count >= 15428)
        {
            //Write the full flag
            //Do nothing....          
          while(1)
          {
           //sys_printf("\n\r sample end !!!!!!!!!!!!!!!!!!!!!\n\r"); 
            Led_One_By_One(4);
			/*应该进入standy 模式 abin@20100715*/
          }
        }
        else
        {
        //sys_printf("\n\r abin ready to add sample count \n\r");  
            FLASH_Unlock();
            FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR);	        
            FLASH_ProgramHalfWord(DATA_LOGGER_ADDRESS_START + record_count * 2, GetTemperature());
            //Erase first
            FLASH_ErasePage(REOCRD_COUNT_ADDRESS);
            //Update the count
            record_count = record_count + 1;
            
            FLASH_ProgramHalfWord(REOCRD_COUNT_ADDRESS , record_count);
            FLASH_Lock();
            
            //sys_printf("\n\r abin add sample count :end\n\r"); 
        }
        

      	     //sys_printf("\n\r %000\n\r");
      GPIO_SetBits(GPIOA, GPIO_Pin_1);
	  #if 0
      Delay(25);
	  #else
      Delay(5);
	  
	  #endif
	  	     //sys_printf("\n\r %111\n\r");

    }
    else
    {
      
        Enable_SDcard();
        /*
        if there is usb connect, copy the data to sdcard. and start the mass storage
        */
        
        copy_file_flag =   *(uint16_t *)COPY_FILE_ADDRESS;

        if(copy_file_flag == FLAG_FILE_COPYED)
        {

              Write_Copy_File_Flag(FLAG_FILE_NO_COPYED);                    

              USB_Disconnect_Config();       
              GPIO_SetBits(USB_DISCONNECT, USB_DISCONNECT_PIN);
              Delay(10);
              GPIO_ResetBits(USB_DISCONNECT, USB_DISCONNECT_PIN);
              
               /* Enable and GPIOD clock */
               Mass_Storage_Start ();                  
               while( bDeviceState != CONFIGURED)
               {
                  Led_Both(1);
               }
               
               USB_Plugin_State = GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_14);
               while(    USB_Plugin_State == 1)/* U-DISK success ,then CPU Loop in here*/
              {
                        USB_Plugin_State = GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_14);
                        Led_One_By_One(1);
                         //sys_printf("\n\r abin :mul2  u disk !!!\n\r"); 
              }
              PowerOff();    
              Write_Copy_File_Flag(FLAG_FILE_NO_COPYED);                    
               
        }
        else
        {
              Write_Copy_File_Flag(FLAG_FILE_COPYED);
              Led_Green_Flink(3);
              NAND_FAT();  
              CreateDataLoggerFile();                          
              Write_Copy_File_Flag(FLAG_FILE_COPYED);
              //sys_printf("\n\r abin :mul2  NAND_FAT!!!\n\r"); 
        }
         //sys_printf("\n\r abin :mul2  Disable_SDcard!!!\n\r"); 
        Disable_SDcard();        
        BKP_WriteBackupRegister(BKP_POWER_ON, FLAG_POWER_OFF); 
        PWR_EnterSTANDBYMode();    

        /* Generate a system reset */  
        //NVIC_SystemReset();    
    }
    
     //sys_printf("\n\r @111 \n\r"); 
    /* Set the RTC Alarm after 60s */
    RTC_SetAlarm(RTC_GetCounter()+ 3);
	     //sys_printf("\n\r @222:RTC_GetCounter()=0x%x \n\r",RTC_GetCounter()); 

    /* Wait until last write operation on RTC registers has finished */
    RTC_WaitForLastTask();
    	     //sys_printf("\n\r @333\n\r");
    PWR_EnterSTANDBYMode();    
    
    
}
Ejemplo n.º 3
0
/*******************************************************************************
* Function Name  : Demo_Init
* Description    : Initializes the demonstration application.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void Demo_Init(void)
{
  /* RCC system reset(for debug purpose) */
  RCC_DeInit();

  /* Enable HSE */
  RCC_HSEConfig(RCC_HSE_ON);

  /* Wait till HSE is ready */
  HSEStartUpStatus = RCC_WaitForHSEStartUp();

  if(HSEStartUpStatus == SUCCESS)
  {
    /* Enable Prefetch Buffer */
    FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

    /* Flash 2 wait state */
    FLASH_SetLatency(FLASH_Latency_2);
 	
    /* HCLK = SYSCLK */
    RCC_HCLKConfig(RCC_SYSCLK_Div1); 
  
    /* PCLK2 = HCLK */
    RCC_PCLK2Config(RCC_HCLK_Div1); 

    /* PCLK1 = HCLK/2 */
    RCC_PCLK1Config(RCC_HCLK_Div2);

    /* PLLCLK = 8MHz * 9 = 72 MHz */
    RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);

    /* Enable PLL */ 
    RCC_PLLCmd(ENABLE);

    /* Wait till PLL is ready */
    while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
    {
    }

    /* Select PLL as system clock source */
    RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

    /* Wait till PLL is used as system clock source */
    while(RCC_GetSYSCLKSource() != 0x08)
    {
    }
  }

  /* Enable GPIOA, GPIOB, GPIOC, GPIOD, GPIOE, GPIOF, GPIOG and AFIO clocks */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |RCC_APB2Periph_GPIOC 
         | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE | RCC_APB2Periph_GPIOF | RCC_APB2Periph_GPIOG 
         | RCC_APB2Periph_AFIO, ENABLE);
  
  /* TIM1 Periph clock enable */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
 
/*------------------- Resources Initialization -----------------------------*/
  /* GPIO Configuration */
  GPIO_Config();

  /* Interrupt Configuration */
  InterruptConfig();

  /* Configure the systick */    
  SysTick_Configuration();

/*------------------- Drivers Initialization -------------------------------*/
  /* Initialize the LEDs toogling */
  LedShow_Init();

  /* Initialize the Low Power application */
  LowPower_Init();

  /* Initialize the LCD */
  STM3210E_LCD_Init();

  /* Clear the LCD */ 
  LCD_Clear(White);
  
  /* If HSE is not detected at program startup */
  if(HSEStartUpStatus == ERROR)
  {
    /* Generate NMI exception */
    SCB->ICSR |= SCB_ICSR_NMIPENDSET;
  }  
   
  /* Checks the availability of the bitmap files */
  CheckBitmapFilesStatus();
  
  /* Display the STM32 introduction */
  STM32Intro();

  /* Clear the LCD */ 
  LCD_Clear(White);

  /* Initialize the Calendar */
  Calendar_Init();

  /* Enable Leds toggling */
  LedShow(ENABLE);
  
  /* Initialize the Low Power application*/ 
  LowPower_Init();

  /* Set the LCD Back Color */
  LCD_SetBackColor(Blue);

  /* Set the LCD Text Color */
  LCD_SetTextColor(White);
  
  /* Initialize the Menu */
  Menu_Init();

  /* Display the main menu icons */
  ShowMenuIcons();
}