Esempio n. 1
0
/**
  * @brief  Setup STM32 system (clocks, Ethernet, GPIO, NVIC) and STM3210C-EVAL resources.
  * @param  None
  * @retval None
  */
void System_Setup(void)
{
  RCC_ClocksTypeDef RCC_Clocks;

	
  /* Setup STM32 clock, PLL and Flash configuration) */
  SystemInit();

  /* Enable USART2 clock */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);


  /* Enable ETHERNET clock  */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_ETH_MAC | RCC_AHBPeriph_ETH_MAC_Tx |
                        RCC_AHBPeriph_ETH_MAC_Rx, ENABLE);

  /* Enable GPIOs and ADC1 clocks */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC |
                         RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE | RCC_APB2Periph_AFIO |
						 RCC_APB2Periph_ADC1, ENABLE);
  
  /* NVIC configuration */
  NVIC_Configuration();  

  /* ADC configuration */
  ADC_Configuration();

  /* Configure the GPIO ports */
  GPIO_Configuration();

  /* Initialize the STM3210C-EVAL's LCD */
  STM3210C_LCD_Init();

  /* Initialize STM3210C-EVAL's LEDs */
  STM_EVAL_LEDInit(LED1);
  STM_EVAL_LEDInit(LED2);
  STM_EVAL_LEDInit(LED3);
  STM_EVAL_LEDInit(LED4);

  /* Turn on leds available on STM3210X-EVAL */
  STM_EVAL_LEDOn(LED1);
  STM_EVAL_LEDOn(LED2);
  STM_EVAL_LEDOn(LED3);
  STM_EVAL_LEDOn(LED4);

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

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

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

  /* Display message on the LCD*/
  LCD_DisplayStringLine(Line0, MESSAGE1);
  LCD_DisplayStringLine(Line1, MESSAGE2);
  LCD_DisplayStringLine(Line2, MESSAGE3);
  LCD_DisplayStringLine(Line3, MESSAGE4);

  /* Configure the Ethernet peripheral */
  Ethernet_Configuration();

  /* SystTick configuration: an interrupt every 10ms */
  RCC_GetClocksFreq(&RCC_Clocks);
  SysTick_Config(RCC_Clocks.SYSCLK_Frequency / 100);

  /* Update the SysTick IRQ priority should be higher than the Ethernet IRQ */
  /* The Localtime should be updated during the Ethernet packets processing */
  NVIC_SetPriority (SysTick_IRQn, 1);  
  
  /* Configure the Key button */ 
  STM_EVAL_PBInit(Button_KEY, Mode_GPIO);
	

}
Esempio n. 2
0
/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
int main(void)
{
  FlagStatus status;
  uint16_t bak_dr10;
  //uint16_t bak_dr9;
  
  //RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
  //PWR_BackupAccessCmd(ENABLE);  
  
  //bak_dr10 = BKP_ReadBackupRegister(BKP_DR10);
  //bak_dr9 = BKP_ReadBackupRegister(BKP_DR9);
  
  //BKP_WriteBackupRegister(BKP_DR10, 0x0707);
  //BKP_WriteBackupRegister(BKP_DR9, 0x0001);
  
  
  /* Test if Key push-button on STM3210C-EVAL Board is not pressed */
  //if((bak_dr10 == 0x0707) /*&& (bak_dr9 != 0x0000)*/)
  if(1)
  {
    /* Setup STM32 system (clocks, Ethernet, GPIO, NVIC) and STM3210C-EVAL resources */
    System_Setup();
    
    Delay(1000);
    //可添加延时, 用以确保DM9000启动时电压满足芯片要求
    //"nRST must not go high until after the VDDIO and VDD_CORE supplies are stable"  手册P51
    GPIO_WriteBit(ETH_RESET,  Bit_SET);   //拉高DM9000 nRST, 复位启动    
    /* Configure the Ethernet peripheral */
    Ethernet_Configuration();
    
    /*
    //判断设备是否是初次上电, 如果'是', 重启一次, 确保以太网初始化正常
    status = RCC_GetFlagStatus(RCC_FLAG_SFTRST);
    RCC_ClearFlag(); 
    if(!status)
    {   
      Delay(10);
      
      NVIC_SystemReset();
    }
    */
 
    /* Initilaize the LwIP stack */
    LwIP_Init();
    
#ifdef USE_IAP_HTTP
    /* Initilaize the webserver module */
    IAP_httpd_init();
#endif
    
#ifdef USE_IAP_TFTP    
    /* Initialize the TFTP server */
    IAP_tftpd_init();
#endif    
    
    /* Infinite loop */
    while (1)
    {
      /* check if any packet received */
      if (ETH_GetRxPktSize()!=0)
      { 
        /* process received eth packet */
        LwIP_Pkt_Handle();
      }
      /* Periodic tasks */
      System_Periodic_Handle();
    }    
  }
  /* enter in IAP mode */
  else
  {
    /* Key push-button not pressed: jump to user application */
    
    /* Check if valid stack address (RAM address) then jump to user application */
    if (((*(__IO uint32_t*)USER_FLASH_FIRST_PAGE_ADDRESS) & 0x2FFE0000 ) == 0x20000000)
    {
      /* Jump to user application */
      JumpAddress = *(__IO uint32_t*) (USER_FLASH_FIRST_PAGE_ADDRESS + 4);
      Jump_To_Application = (pFunction) JumpAddress;
      /* Initialize user application's Stack Pointer */
      __set_MSP(*(__IO uint32_t*) USER_FLASH_FIRST_PAGE_ADDRESS);
      Jump_To_Application();
    }
    else
    {/* Otherwise, do nothing */
      /* LED3 (RED) ON to indicate bad software (when not valid stack address) */
      //STM_EVAL_LEDInit(LED3);
      //STM_EVAL_LEDOn(LED3);
      /* do nothing */
      while(1);
    }    
  }
  
  return 0;
}
Esempio n. 3
0
int main(void)
{
  /*!< At this stage the microcontroller clock setting is already configured, 
  this i
  s done through SystemInit() function which is called from startup
  file (startup_stm32f10x_xx.s) before to branch to application main.
  To reconfigure the default setting of SystemInit() function, refer to
  system_stm32f10x.c file
  */     
  //GPIO_InitTypeDef GPIO_InitStructure;
  //uint32_t irq; //test IRQ
  
  
  /* System clocks configuration ---------------------------------------------*/
  SystemInit();
  RCC_Configuration();
  
  /* GPIO configuration ------------------------------------------------------*/
  GPIO_Configuration();
  GPIO_WriteBit(CANTX_LED,  Bit_SET);
  GPIO_WriteBit(CANRX_LED,  Bit_SET);
  GPIO_WriteBit(COMTX_LED,  Bit_SET);
  GPIO_WriteBit(COMRX_LED,  Bit_SET);
  GPIO_WriteBit(PWR_LED,  Bit_RESET);
  GPIO_WriteBit(ALARM_LED,  Bit_SET);
  GPIO_WriteBit(RUNSTAT_LED,  Bit_SET);

  GPIO_WriteBit(ETH_RESET,  Bit_RESET);//拉低DM9000 nRST, 延时复位启动
  
  DataBase_Init(DevIPAddressTab);
  
#ifndef TEST  
  /* com1 configuration ------------------------------------------------------*/
  COM1_Configuration();
#ifdef TEST_PARTS  
  GPIO_WriteBit(CANTX_LED,  Bit_RESET);
  GPIO_WriteBit(CANRX_LED,  Bit_SET);
  GPIO_WriteBit(COMTX_LED,  Bit_SET);
  GPIO_WriteBit(COMRX_LED,  Bit_SET);
#endif
  printf(" <<<<<<< COM1 config complete <<<<<<<\r\n\r\n");  
  
  
  /* com2 configuration ------------------------------------------------------*/
  //COM2_Configuration();
#endif 
  
  /* i2c configuration ------------------------------------------------------*/
  printf(" >>>>>>> I2C config begin >>>>>>>\r\n");
  I2C_Configuration();
#ifdef TEST_PARTS  
  GPIO_WriteBit(CANTX_LED,  Bit_SET);
  GPIO_WriteBit(CANRX_LED,  Bit_RESET);
  GPIO_WriteBit(COMTX_LED,  Bit_SET);
  GPIO_WriteBit(COMRX_LED,  Bit_SET); 
#endif 
  printf(" <<<<<<< I2C config complete <<<<<<<\r\n\r\n");
  
#ifndef TEST  
  /* RTC configuration--------------------------------------------------------*/
  printf(" >>>>>>> RTC config begin >>>>>>>\r\n");
  RTC_Configuration();
#ifdef TEST_PARTS  
  GPIO_WriteBit(CANTX_LED,  Bit_RESET);
  GPIO_WriteBit(CANRX_LED,  Bit_RESET);
  GPIO_WriteBit(COMTX_LED,  Bit_SET);
  GPIO_WriteBit(COMRX_LED,  Bit_SET);
#endif  
  printf(" <<<<<<< RTC config complete <<<<<<<\r\n\r\n");
#endif
  
#ifdef CAN_APP  
  /* can configuration ------------------------------------------------------*/
  printf(" >>>>>>> CAN config begin >>>>>>>\r\n");
  CAN_Configuration();
#ifdef TEST_PARTS  
  GPIO_WriteBit(CANTX_LED,  Bit_SET);
  GPIO_WriteBit(CANRX_LED,  Bit_SET);
  GPIO_WriteBit(COMTX_LED,  Bit_RESET);
  GPIO_WriteBit(COMRX_LED,  Bit_SET);
#endif  
  printf(" <<<<<<< CAN config complete <<<<<<<\r\n\r\n");  
#endif
  
#ifndef TEST  
  /*temperature configuration------------------------------------------------------*/
  printf(" >>>>>>> TEMPMEA config begin >>>>>>>\r\n");
  TEMPMEA_Confitguration();
#ifdef TEST_PARTS  
  GPIO_WriteBit(CANTX_LED,  Bit_SET);
  GPIO_WriteBit(CANRX_LED,  Bit_RESET);
  GPIO_WriteBit(COMTX_LED,  Bit_RESET);
  GPIO_WriteBit(COMRX_LED,  Bit_SET);
#endif 
  printf(" <<<<<<< TEMPMEA config complete <<<<<<<\r\n\r\n");
#endif
  
  /* SysTick configuration ------------------------------------------------------*/
  printf(" >>>>>>> SysTick config begin >>>>>>>\r\n");
  SysTick_Configuration();
#ifdef TEST_PARTS  
  GPIO_WriteBit(CANTX_LED,  Bit_RESET);
  GPIO_WriteBit(CANRX_LED,  Bit_RESET);
  GPIO_WriteBit(COMTX_LED,  Bit_RESET);
  GPIO_WriteBit(COMRX_LED,  Bit_SET);
#endif  
  printf(" <<<<<<< SysTick config complete <<<<<<<\r\n\r\n");

  /* NVIC configuration ------------------------------------------------------*/
  printf(" >>>>>>> NVIC config begin >>>>>>>\r\n");
  NVIC_Configuration();
#ifdef TEST_PARTS  
  GPIO_WriteBit(CANTX_LED,  Bit_SET);
  GPIO_WriteBit(CANRX_LED,  Bit_SET);
  GPIO_WriteBit(COMTX_LED,  Bit_SET);
  GPIO_WriteBit(COMRX_LED,  Bit_RESET);
#endif  
  printf(" <<<<<<< NVIC config complete <<<<<<<\r\n\r\n");
  
  /* Update the SysTick IRQ priority should be higher than the Ethernet IRQ */
  /* The Localtime should be updated during the Ethernet packets processing */
  NVIC_SetPriority (SysTick_IRQn, 1); 
  
  /* test IRQ*/
//  irq = NVIC_GetPriority(SysTick_IRQn);
//  irq = NVIC_GetPriority(ETH_IRQn);
//  irq = NVIC_GetPriority(I2C1_ER_IRQn);
//  irq = NVIC_GetPriority(I2C1_EV_IRQn);
  
  
  /* ethernet configuration ------------------------------------------------------*/
  //可添加延时, 用以确保DM9000启动时电压满足芯片要求
  //"nRST must not go high until after the VDDIO and VDD_CORE supplies are stable"  手册P51
  GPIO_WriteBit(ETH_RESET,  Bit_SET);   //拉高DM9000 nRST, 复位启动
  
  printf(" >>>>>>> ETH config begin >>>>>>>\r\n");
  Ethernet_Configuration();
#ifdef TEST_PARTS  
  GPIO_WriteBit(CANTX_LED,  Bit_RESET);
  GPIO_WriteBit(CANRX_LED,  Bit_SET);
  GPIO_WriteBit(COMTX_LED,  Bit_RESET);
  GPIO_WriteBit(COMRX_LED,  Bit_SET);
#endif  
  printf(" <<<<<<< ETH config complete <<<<<<<\r\n\r\n");  

    
#ifdef WATCHDOG
  /* WATCHDOG configuration ------------------------------------------------------*/
  IWDG_Configuration();
#endif
  
  //判断是否有以太网链接
  if(EthInitState)
  {
    CommunicationInit();
  }
  
  printf(" ******* 输入'$'将触发串口控制台!*******\r\n\r\n");
  GPIO_WriteBit(CANTX_LED,  Bit_SET);
  GPIO_WriteBit(CANRX_LED,  Bit_SET);
  GPIO_WriteBit(COMTX_LED,  Bit_SET);
  GPIO_WriteBit(COMRX_LED,  Bit_SET); 
  
  
  //启动完成, 进入常规流程
#ifdef _TEST  
  uint8_t test = 0;
#endif
  
  while (1)
  {
    Di_PostWork();
    LwIP_Periodic_Handle(LocalTime);
    Task_Periodic_Handle(LocalTime);
    

    //判断以太网状态, 决定是否要复位以太网
    EthStateCheck();
    
    reset_flag = Get_eth_reset_flag();
    if(reset_flag == Ethernet_SWRST_FLAG)
      Ethernet_SWRST();
    else if(reset_flag == Ethernet_HWRST_FLAG)
      Ethernet_HWRST();

#ifdef _TEST
    //tyh:20130407 eth reset test
    if((DiStatus_DI[1].Value != test)&&(DiStatus_DI[1].Value == 1))
    {
      //eth_reg = ETH_ReadPHYRegister(0x1F, 17);
      Ethernet_HWRST();
    }
    test = DiStatus_DI[1].Value;
#endif
    
//    else
//    {
//      if( EthLinkCheck() )
//      {
//        //tyh:20130403 send udp test_message
//        if((DiStatus_DI[1].Value != test)&&(DiStatus_DI[1].Value == 1))
//        {
//          Udp_timing_test();
//        }
//        
//        test = DiStatus_DI[1].Value;
//      }
//    }
    
#ifdef WATCHDOG 
    WDGFeeding();
#endif
    
  }
}
struct netif * LwIP_Init()
{
  uint8_t macaddress[6] = ETH_MAC_ADDR;
  struct ip_addr ipaddr;
  struct ip_addr netmask;
  struct ip_addr gw;

  /* Create isr for ethernet interrupt */
  TaskType tid;
  tid = Os_Arc_CreateIsr(Eth_Isr,3/*prio*/,"Eth"); \
  Irq_AttachIsr2(tid,NULL,ETH_IRQn); \

  /* Configure ethernet */
   Ethernet_Configuration();

#if NO_SYS
#if (MEM_LIBC_MALLOC==0)  
  mem_init();
#endif
#if (MEMP_MEM_MALLOC==0)  
  memp_init();
#endif
#else
  pre_sys_init();
  tcpip_init(tcpip_init_done, NULL);
  uint32 lockcnt = 0;
  while(tcpip_initialized == FALSE){
  	 lockcnt++;
  	 SLEEP(0);
  };
#endif

#if LWIP_DHCP
  ipaddr.addr = 0;
  netmask.addr = 0;
  gw.addr = 0;
#else
  GET_BOOT_IPADDR;
  GET_BOOT_NETMASK;
  GET_BOOT_GW;
#endif

  Set_MAC_Address(macaddress);

  /* Add network interface to the netif_list */
  netif_add(&netif, &ipaddr, &netmask, &gw, NULL, &ethernetif_init, &tcpip_input);

  /*  Registers the default network interface.*/
  netif_set_default(&netif);

#if LWIP_DHCP
  /* start dhcp search */
  dhcp_start(&netif);
#else
  netif_set_addr(&netif, &ipaddr , &netmask, &gw);
#endif

  /* netif is configured */
  netif_set_up(&netif);

  EnableEthDMAIrq();

  netbios_init();

  return &netif;
}
Esempio n. 5
0
/**
  * @brief  Ethernet Initialize function
  * @param  None
  * @retval None
  */
static void BSP_EthernetInit(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
		
		/* Enable ETHERNET clock  */
		RCC_AHBPeriphClockCmd(RCC_AHBPeriph_ETH_MAC | RCC_AHBPeriph_ETH_MAC_Tx |
                        RCC_AHBPeriph_ETH_MAC_Rx, ENABLE);

  	/* Enable GPIOs clocks */
  	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC |
                         RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE | RCC_APB2Periph_AFIO , ENABLE);
  	/* ETHERNET pins configuration */
  	/* AF Output Push Pull:
  	- ETH_MII_MDIO / ETH_RMII_MDIO: PA2
  	- ETH_MII_MDC / ETH_RMII_MDC: PC1
  	- ETH_MII_TXD2: PC2
  	- ETH_MII_TX_EN / ETH_RMII_TX_EN: PB11
  	- ETH_MII_TXD0 / ETH_RMII_TXD0: PB12
  	- ETH_MII_TXD1 / ETH_RMII_TXD1: PB13
  	- ETH_MII_PPS_OUT / ETH_RMII_PPS_OUT: PB5
  	- ETH_MII_TXD3: PB8 */

  //配置PA2  复用推免输出
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

  //配置PC1  复用推免输出
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_Init(GPIOC, &GPIO_InitStructure);

  //配置PB11 PB12 PB13 复用推免输出
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_8 | GPIO_Pin_11 |
                                GPIO_Pin_12 | GPIO_Pin_13;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_Init(GPIOB, &GPIO_InitStructure);


  //改变RX_DV RXD0和RXD1的映射 
  //GPIO_PinRemapConfig(GPIO_Remap_ETH, ENABLE);

  //配置PA0 PA1 浮空输入
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_3;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

  //配置PB10 浮空输入
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  GPIO_Init(GPIOB, &GPIO_InitStructure);
	
  /* Configure PC3 as input */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  GPIO_Init(GPIOC, &GPIO_InitStructure);


  //配置PD8 PD9 PD10 浮空输入
  GPIO_InitStructure.GPIO_Pin = /*GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | */GPIO_Pin_11 | GPIO_Pin_12;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  GPIO_Init(GPIOD, &GPIO_InitStructure); 

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4| GPIO_Pin_5;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  GPIO_Init(GPIOC, &GPIO_InitStructure);
	
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
	
  //配置PA8 复用推免输出
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
  Ethernet_Configuration();

}