Exemple #1
0
/**
  * @brief  LwIP_DHCP_Process_Handle
  * @param  None
  * @retval None
  */
void LwIP_DHCP_Process_Handle()
{
  struct ip_addr ipaddr;
  struct ip_addr netmask;
  struct ip_addr gw;
  uint8_t iptab[4];
  uint8_t iptxt[20];

  switch (DHCP_state)
  {
    case DHCP_START:
    {
      dhcp_start(&netif);
      IPaddress = 0;
      DHCP_state = DHCP_WAIT_ADDRESS;
#ifdef USE_LCD
      LCD_DisplayStringLine(Line4, (uint8_t*)"     Looking for    ");
      LCD_DisplayStringLine(Line5, (uint8_t*)"     DHCP server    ");
      LCD_DisplayStringLine(Line6, (uint8_t*)"     please wait... ");
#endif
    }
    break;

    case DHCP_WAIT_ADDRESS:
    {
      /* Read the new IP address */
      IPaddress = netif.ip_addr.addr;

      if (IPaddress!=0) 
      {
        DHCP_state = DHCP_ADDRESS_ASSIGNED;	

        /* Stop DHCP */
        dhcp_stop(&netif);

#ifdef USE_LCD      
        iptab[0] = (uint8_t)(IPaddress >> 24);
        iptab[1] = (uint8_t)(IPaddress >> 16);
        iptab[2] = (uint8_t)(IPaddress >> 8);
        iptab[3] = (uint8_t)(IPaddress);

        sprintf((char*)iptxt, " %d.%d.%d.%d", iptab[3], iptab[2], iptab[1], iptab[0]);       

        LCD_ClearLine(Line4);
        LCD_ClearLine(Line5);
        LCD_ClearLine(Line6);

        /* Display the IP address */
        LCD_DisplayStringLine(Line7, (uint8_t*)"IP address assigned ");
        LCD_DisplayStringLine(Line8, (uint8_t*)"  by a DHCP server  ");
        LCD_DisplayStringLine(Line9, iptxt);
#endif
        STM_EVAL_LEDOn(LED1);
      }
      else
      {
        /* DHCP timeout */
        if (netif.dhcp->tries > MAX_DHCP_TRIES)
        {
          DHCP_state = DHCP_TIMEOUT;

          /* Stop DHCP */
          dhcp_stop(&netif);

          /* Static address used */
          IP4_ADDR(&ipaddr, IP_ADDR0 ,IP_ADDR1 , IP_ADDR2 , IP_ADDR3 );
          IP4_ADDR(&netmask, NETMASK_ADDR0, NETMASK_ADDR1, NETMASK_ADDR2, NETMASK_ADDR3);
          IP4_ADDR(&gw, GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_ADDR3);
          netif_set_addr(&netif, &ipaddr , &netmask, &gw);

#ifdef USE_LCD   
          LCD_DisplayStringLine(Line7, (uint8_t*)"    DHCP timeout    ");

          iptab[0] = IP_ADDR3;
          iptab[1] = IP_ADDR2;
          iptab[2] = IP_ADDR1;
          iptab[3] = IP_ADDR0;

          sprintf((char*)iptxt, "  %d.%d.%d.%d", iptab[3], iptab[2], iptab[1], iptab[0]); 

          LCD_ClearLine(Line4);
          LCD_ClearLine(Line5);
          LCD_ClearLine(Line6);

          LCD_DisplayStringLine(Line8, (uint8_t*)"  Static IP address   ");
          LCD_DisplayStringLine(Line9, iptxt);         
#endif    
          STM_EVAL_LEDOn(LED1);
        }
      }
    }
Exemple #2
0
/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{
  /*!< At this stage the microcontroller clock setting is already configured, 
       this is done through SystemInit() function which is called from startup
       files (startup_stm32f40_41xxx.s/startup_stm32f427_437xx.s/startup_stm32f429_439xx.s)
       before to branch to application main.
     */     

  /* SysTick end of count event each 10ms */
  RCC_GetClocksFreq(&RCC_Clocks);
  SysTick_Config(RCC_Clocks.HCLK_Frequency / 100);
         
  /* Initialize LEDs mounted on EVAL board */
  STM_EVAL_LEDInit(LED1);
  STM_EVAL_LEDInit(LED2);
  STM_EVAL_LEDInit(LED3);
  STM_EVAL_LEDInit(LED4);

  /* Select the Button test mode (polling or interrupt) BUTTON_MODE in main.h */
  STM_EVAL_PBInit(BUTTON_WAKEUP, BUTTON_MODE);
  STM_EVAL_PBInit(BUTTON_TAMPER, BUTTON_MODE);

  /* Initialize the LCD */
  LCD_Init();

  /* Initialize the LCD Layers */
  LCD_LayerInit();  
  
  /* Enable LCD display */
  LCD_DisplayOn();
  
  /* Set foreground layer */
  LCD_SetLayer(LCD_FOREGROUND_LAYER);
  
  /* Clear the LCD */ 
  LCD_Clear(White);
  
  /* Set the LCD Back Color */
  LCD_SetBackColor(White);
  
  /* Set the LCD Text Color */
  LCD_SetTextColor(Blue);    
 
  LCD_DisplayStringLine(LCD_LINE_0, (uint8_t *)"        STM324x9I-EVAL       ");
  LCD_DisplayStringLine(LCD_LINE_1, (uint8_t *)"      Example on how to      ");
  LCD_DisplayStringLine(LCD_LINE_2, (uint8_t *)"     use the IO Expander     ");
  
  /* Configure the IO Expander */
  if (IOE_Config() == IOE_OK && IOE16_Config() == IOE16_OK)
  {
    LCD_DisplayStringLine(LCD_LINE_3, (uint8_t *)"      IO Expander OK       ");
  }
  else
  {
    LCD_DisplayStringLine(LCD_LINE_4, (uint8_t *)"IO Expander FAILED    ");
    LCD_DisplayStringLine(LCD_LINE_5, (uint8_t *)" Please Reset the     ");
    LCD_DisplayStringLine(LCD_LINE_6, (uint8_t *)"   board and start    ");
    LCD_DisplayStringLine(LCD_LINE_7, (uint8_t *)"    again             ");
    while(1);
  }

  /* LEDs Control blocks */
  LCD_SetTextColor(Blue);
  LCD_DrawRect(310, 180,  40, 60);
  LCD_SetTextColor(Red);
  LCD_DrawRect(230, 180, 40, 60);
  LCD_SetTextColor(Yellow);
  LCD_DrawRect(150, 180, 40, 60);
  LCD_SetTextColor(Green);
  LCD_DrawRect(70, 180, 40, 60);

#ifdef IOE_INTERRUPT_MODE
  /* Configure motherboard interrupt source IO_EXP4 */ 
  IOE16_IOPinConfig(IOE16_TS_IT,Direction_IN);
  IOE16_ITConfig(IOE16_TS_IT);
  
  /* Enable joystick interrupt */
  IOE16_ITConfig(JOY_IO16_PINS);
  
  /* Enable the Touch Screen interrupt */
  IOE_TSITConfig(); 
  
  /* Read IOs state to let IO interrupt occur */
  IOE16_I2C_ReadDeviceRegister(IOE16_REG_GPMR_LSB);
  IOE16_I2C_ReadDeviceRegister(IOE16_REG_GPMR_MSB);
#endif /* IOE_INTERRUPT_MODE */
  
  
  while(1)
  {
#ifdef IOE_POLLING_MODE
    static JOY_State_TypeDef JoyState = JOY_NONE;
    static TS_STATE* TS_State;
    
    /* Get the Joystick State */
    JoyState = IOE16_JoyStickGetState();
    
    /* Set the LCD Text Color */
    LCD_SetTextColor(Blue); 
   
    switch (JoyState)
    {
      case JOY_NONE:
        LCD_DisplayStringLine(LCD_LINE_5, (uint8_t *)"JOY:     ----        ");
        break;
      case JOY_UP:
        LCD_DisplayStringLine(LCD_LINE_5, (uint8_t *)"JOY:     UP         ");
        break;     
      case JOY_DOWN:
        LCD_DisplayStringLine(LCD_LINE_5, (uint8_t *)"JOY:    DOWN        ");
        break;          
      case JOY_LEFT:
        LCD_DisplayStringLine(LCD_LINE_5, (uint8_t *)"JOY:    LEFT        ");
        break;         
      case JOY_RIGHT:
        LCD_DisplayStringLine(LCD_LINE_5, (uint8_t *)"JOY:    RIGHT        ");
        break;                 
      case JOY_CENTER:
        LCD_DisplayStringLine(LCD_LINE_5, (uint8_t *)"JOY:   CENTER       ");
        break; 
      default:
        LCD_DisplayStringLine(LCD_LINE_5, (uint8_t *)"JOY:   ERROR      ");
        break;         
    }

    /* Update the structure with the current position */
    TS_State = IOE_TS_GetState();  
    
    if ((TS_State->TouchDetected) && (TS_State->Y < 92) && (TS_State->Y > 52))
    {
      if ((TS_State->X > 60) && (TS_State->X < 120))
      {
        LCD_SetTextColor(LCD_COLOR_GREEN);   
        LCD_DisplayStringLine(LCD_LINE_10, (uint8_t *)"     LD1                ");
        STM_EVAL_LEDOn(LED1);
      }
      else if ((TS_State->X > 140) && (TS_State->X < 200))
      {
        LCD_SetTextColor(LCD_COLOR_YELLOW); 
        LCD_DisplayStringLine(LCD_LINE_10, (uint8_t *)"          LD2           ");
        STM_EVAL_LEDOn(LED2);
      }
      else if ((TS_State->X > 220) && (TS_State->X < 280))
      {
        LCD_SetTextColor(LCD_COLOR_RED); 
        LCD_DisplayStringLine(LCD_LINE_10, (uint8_t *)"               LD3      ");
        STM_EVAL_LEDOn(LED3);
      }     
      else if ((TS_State->X > 300) && (TS_State->X < 360))
      {
        LCD_SetTextColor(LCD_COLOR_BLUE); 
        LCD_DisplayStringLine(LCD_LINE_10, (uint8_t *)"                    LD4 ");
        STM_EVAL_LEDOn(LED4);
      }
    }
    else
    {
      STM_EVAL_LEDOff(LED1);
      STM_EVAL_LEDOff(LED2);
      STM_EVAL_LEDOff(LED3);
      STM_EVAL_LEDOff(LED4);
    }
#endif /* IOE_POLLING_MODE */  
    
#ifdef BUTTON_POLLING_MODE
    /* Insert 10 ms delay */
    Delay(1);
    
    /* Set the LCD Text Color */
    LCD_SetTextColor(Blue); 

    if (STM_EVAL_PBGetState(BUTTON_TAMPER) == 0)
    {
      /* Toggle LD2 */
      STM_EVAL_LEDToggle(LED2);

      LCD_DisplayStringLine(LCD_LINE_4, (uint8_t *)"Pol: TAMPER/KEY Pressed  ");
    }

    if (STM_EVAL_PBGetState(BUTTON_WAKEUP) != 0)
    {
      /* Toggle LD3 */
      STM_EVAL_LEDToggle(LED3);
      LCD_DisplayStringLine(LCD_LINE_4, (uint8_t *)"Pol: WAKEUP Pressed      ");
    }
#endif
  }
}
Exemple #3
0
/**
  * @brief   Main program
  * @param  None
  * @retval None
  */
int main(void)
{
  /*!< At this stage the microcontroller clock setting is already configured,
  this is done through SystemInit() function which is called from startup
  file (startup_stm32l1xx_xx.s) before to branch to application main.
  To reconfigure the default setting of SystemInit() function, refer to
  system_stm32l1xx.c file
  */
  
/* I2C configuration ---------------------------------------------------------*/
  I2C_Config();
  
  /* Initialize LEDs mounted on STM32L152-EVAL board */
  STM_EVAL_LEDInit(LED1);
  STM_EVAL_LEDInit(LED2);
  STM_EVAL_LEDInit(LED3);
  STM_EVAL_LEDInit(LED4);
  
/* SysTick configuration -----------------------------------------------------*/
  SysTickConfig();
  
/*************************************Master Code******************************/
#if defined (I2C_MASTER)
  /* I2C De-initialize */
  I2C_DeInit(I2Cx);
  
  /*!< I2C Struct Initialize */
  I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
  I2C_InitStructure.I2C_DutyCycle = I2C_DUTYCYCLE;
  I2C_InitStructure.I2C_OwnAddress1 = 0xA0;
  I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
  I2C_InitStructure.I2C_ClockSpeed = I2C_SPEED;
  
#ifndef I2C_10BITS_ADDRESS
  I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
#else
  I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_10bit;
#endif /* I2C_10BITS_ADDRESS */
  
  /*!< I2C Initialize */
  I2C_Init(I2Cx, &I2C_InitStructure);
  
  /* Enable Error Interrupt */
  I2C_ITConfig(I2Cx, I2C_IT_ERR , ENABLE);
  
  /* I2C ENABLE */
  I2C_Cmd(I2Cx, ENABLE);
  
/* Master Transmitter---------------------------------------------------------*/   
  NumberOfByteToTransmit = TXBUFFERSIZE;
  MasterMode = MASTER_MODE_TRANSMITTER;
  Tx_Idx = 0x00;
  /* Enable Error and Buffer Interrupts */
  I2C_ITConfig(I2Cx, (I2C_IT_EVT | I2C_IT_BUF), ENABLE);
  /* Generate the Start condition */
  I2C_GenerateSTART(I2Cx, ENABLE);
  /* Data transfer is performed in the I2C interrupt routine */
  /* Wait until end of data transfer or time out */
  TimeOut = USER_TIMEOUT;
  while ((Tx_Idx < TXBUFFERSIZE)&&(TimeOut != 0x00))
  {}
  if(TimeOut == 0)
  {
    TimeOut_UserCallback();
  }
  
  TimeOut = USER_TIMEOUT;
  while ((I2C_GetFlagStatus(I2Cx, I2C_FLAG_BUSY))&&(TimeOut != 0x00))
  {}
  if(TimeOut == 0)
  {
    TimeOut_UserCallback();
  }
/* Master Receiver------------------------------------------------------------*/ 
  NumberOfByteToReceive = RXBUFFERSIZE;
  MasterMode = MASTER_MODE_RECEIVER;
  Rx_Idx = 0x00;
  /* Clear the RxBuffer */
  Fill_Buffer(RxBuffer, RXBUFFERSIZE);
  
  /* Enable Event Interrupts */
  I2C_ITConfig(I2Cx, I2C_IT_EVT , ENABLE);
  /* Enable Acknowledge */
  I2C_AcknowledgeConfig(I2Cx, ENABLE);
  
  /* Generate the Start condition */
  I2C_GenerateSTART(I2Cx, ENABLE);
  /* Data transfer is performed in the I2C interrupt routine */
  /* Wait until end of data transfer */
  while (Rx_Idx < RXBUFFERSIZE)
  {}
  if (Buffercmp(TxBuffer, RxBuffer, RXBUFFERSIZE) == PASSED)
  {
    /* LED2, LED3 and LED4 Toggle */
    STM_EVAL_LEDOn(LED2);
    STM_EVAL_LEDOn(LED3);
    STM_EVAL_LEDOn(LED4);
  }
  else 
  {   /* ED2, LED3 and LED4 On */
    STM_EVAL_LEDOff(LED2);
    STM_EVAL_LEDOff(LED3);
    STM_EVAL_LEDOff(LED4);
  }
  
#endif /* I2C_MASTER */
  
/**********************************Slave Code**********************************/
#if defined (I2C_SLAVE)
  
   I2C_DeInit(I2Cx);
  
  /* Initialize I2C peripheral */
  /*!< I2C Init */
  I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
  I2C_InitStructure.I2C_DutyCycle = I2C_DUTYCYCLE;
  I2C_InitStructure.I2C_OwnAddress1 = SLAVE_ADDRESS;
  I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
  I2C_InitStructure.I2C_ClockSpeed = I2C_SPEED;
  
#ifndef I2C_10BITS_ADDRESS
  I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
#else
  I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_10bit;
#endif /* I2C_10BITS_ADDRESS */
  
  I2C_Init(I2Cx, &I2C_InitStructure);
  
  /* Enable Error Interrupt */
  I2C_ITConfig(I2Cx, (I2C_IT_ERR | I2C_IT_EVT | I2C_IT_BUF), ENABLE);
  
  /* I2C ENABLE */
  I2C_Cmd(I2Cx, ENABLE);
  
/* Slave Receiver-------------------------------------------------------------*/
  Rx_Idx = 0x00; 
  Tx_Idx = 0x00;
  
  /* Clear the RxBuffer */
  Fill_Buffer(RxBuffer, RXBUFFERSIZE);
  
  /* Wait until end of data transfer */
  while (Rx_Idx < RXBUFFERSIZE)
  {} 
  
  TimeOut = USER_TIMEOUT;
  while ((I2C_GetFlagStatus(I2Cx, I2C_FLAG_BUSY))&&(TimeOut != 0x00))
  {}
  if(TimeOut == 0)
  {
    TimeOut_UserCallback();
  }
  
/* Slave Transmitter----------------------------------------------------------*/   
  
  /* Wait until end of data transfer */
  while (Tx_Idx < TXBUFFERSIZE)
  {} 
  
  if (Buffercmp(TxBuffer, RxBuffer, RXBUFFERSIZE) == PASSED)
  {
    /* LED2, LED3 and LED4 Toggle */
    STM_EVAL_LEDOn(LED2);
    STM_EVAL_LEDOn(LED3);
    STM_EVAL_LEDOn(LED4);
  }
  else 
  {   /* ED2, LED3 and LED4 On */
    STM_EVAL_LEDOff(LED2);
    STM_EVAL_LEDOff(LED3);
    STM_EVAL_LEDOff(LED4);
  } 
  
#endif /* I2C_SLAVE */
  while(1)
  {}
}
Exemple #4
0
/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{
    /*!< At this stage the microcontroller clock setting is already configured,
         this is done through SystemInit() function which is called from startup
         files (startup_stm32f40_41xxx.s/startup_stm32f42_43xxx.s) before to branch
         to application main.
         To reconfigure the default setting of SystemInit() function, refer to
         system_stm32f4xx.c file
       */

    /* Initialize LEDs, Key Button, LCD available on EVAL board *****************/
    STM_EVAL_LEDInit(LED1);
    STM_EVAL_LEDInit(LED2);
    STM_EVAL_LEDInit(LED3);
    STM_EVAL_LEDInit(LED4);

    /* Initialize the Push buttons */
    /* Key button used for Pause/Resume */
    STM_EVAL_PBInit(BUTTON_KEY, BUTTON_MODE_GPIO);
    /* Key button used for Volume High */
    STM_EVAL_PBInit(BUTTON_WAKEUP, BUTTON_MODE_GPIO);
    /* Key button used for Volume Low */
    STM_EVAL_PBInit(BUTTON_TAMPER, BUTTON_MODE_GPIO);

    /* Initialize the LCD */
    LCD_Init();

    /* Display message on EVAL LCD **********************************************/
    /* Clear the LCD */
    LCD_Clear(LCD_COLOR_BLUE);

    /* Set the LCD Back Color */
    LCD_SetBackColor(Blue);
    /* Set the LCD Text Color */
    LCD_SetTextColor(White);
    LCD_DisplayStringLine(Line0, (uint8_t *)MESSAGE1);
    LCD_DisplayStringLine(Line1, (uint8_t *)MESSAGE2);
    LCD_DisplayStringLine(Line2, (uint8_t *)MESSAGE3);

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

    /* SysTick end of count event each 10ms */
    RCC_GetClocksFreq(&RCC_Clocks);
    SysTick_Config(RCC_Clocks.HCLK_Frequency / 100);

    /* Initialize the Audio codec and all related peripherals (I2S, I2C, IOExpander, IOs...) */
    if (EVAL_AUDIO_Init(OUTPUT_DEVICE_AUTO, uwVolume, I2S_AudioFreq_48k) == 0)
    {
        LCD_DisplayStringLine(Line3, (uint8_t *)"====================");
        LCD_DisplayStringLine(Line4, (uint8_t *)"Key   : Play/Pause ");
        LCD_DisplayStringLine(Line5, (uint8_t *)"Tamper: Vol+/Headph");
        LCD_DisplayStringLine(Line6, (uint8_t *)"Wakeup: Vol-/Spkr  ");
        LCD_DisplayStringLine(Line7, (uint8_t *)"====================");
        LCD_DisplayStringLine(Line8, (uint8_t *)"  AUDIO CODEC   OK  ");
    }
    else
    {
        LCD_DisplayStringLine(Line4, (uint8_t *)"  AUDIO CODEC  FAIL ");
        LCD_DisplayStringLine(Line5, (uint8_t *)" Try to reset board ");
    }

    /*
    Normal mode description:
        Start playing the audio file (using DMA stream) .
        Using this mode, the application can run other tasks in parallel since
        the DMA is handling the Audio Transfer instead of the CPU.
        The only task remaining for the CPU will be the management of the DMA
        Transfer Complete interrupt or the Half Transfer Complete interrupt in
        order to load again the buffer and to calculate the remaining data.
    Circular mode description:
       Start playing the file from a circular buffer, once the DMA is enabled it
       always run. User has to fill periodically the buffer with the audio data
       using Transfer complete and/or half transfer complete interrupts callbacks
       (EVAL_AUDIO_TransferComplete_CallBack() or EVAL_AUDIO_HalfTransfer_CallBack()...
       In this case the audio data file is smaller than the DMA max buffer
       size 65535 so there is no need to load buffer continuously or manage the
       transfer complete or Half transfer interrupts callbacks. */
    EVAL_AUDIO_Play((uint16_t*)(AUDIO_SAMPLE + AUDIO_START_ADDRESS), (AUDIO_FILE_SZE - AUDIO_START_ADDRESS));

    /* Display the state on the screen */
    LCD_DisplayStringLine(Line8, (uint8_t *)"       PLAYING      ");

    /* Infinite loop */
    while (1)
    {
        /* Check on the Pause/Resume button */
        if (STM_EVAL_PBGetState(BUTTON_KEY) != Bit_SET)
        {
            /* Wait to avoid rebound */
            while (STM_EVAL_PBGetState(BUTTON_KEY) != Bit_SET);

            EVAL_AUDIO_PauseResume(uwCommand);
            if (uwCommand == AUDIO_PAUSE)
            {
                /* Display the current state of the player */
                LCD_DisplayStringLine(Line8, (uint8_t *)"       PAUSED       ");

                /* Next time Resume command should be processed */
                uwCommand = AUDIO_RESUME;

                /* Push buttons will be used to switch between Speaker and Headphone modes */
                uwSpHpSwitch = 1;
            }
            else
            {
                /* Display the current state of the player */
                LCD_DisplayStringLine(Line8, (uint8_t *)"       PLAYING      ");

                /* Next time Pause command should be processed */
                uwCommand = AUDIO_PAUSE;

                /* Push buttons will be used to control volume level */
                uwSpHpSwitch = 0;
            }
        }

        /* Check on the Volume high button */
        if (STM_EVAL_PBGetState(BUTTON_WAKEUP) == Bit_SET)
        {
            /* Check if the current state is paused (push buttons are used for volume control or for
               speaker/headphone mode switching) */
            if (uwSpHpSwitch)
            {
                /* Set output to Speaker */
                Codec_SwitchOutput(OUTPUT_DEVICE_SPEAKER);

                /* Display the current state of the player */
                LCD_DisplayStringLine(Line9, (uint8_t *)"       SPEAKER      ");
            }
            else
            {
                /* wait to avoid rebound */
                while (STM_EVAL_PBGetState(BUTTON_WAKEUP) == Bit_SET);

                /* Decrease volume by 5% */
                if (uwVolume > 5)
                    uwVolume -= 5;
                else
                    uwVolume = 0;

                /* Apply the new volume to the codec */
                EVAL_AUDIO_VolumeCtl(uwVolume);
                LCD_DisplayStringLine(Line9, (uint8_t *)"       VOL:   -     ");
            }
        }

        /* Check on the Volume high button */
        if (STM_EVAL_PBGetState(BUTTON_TAMPER) != Bit_SET)
        {
            /* Check if the current state is paused (push buttons are used for volume control or for
               speaker/headphone mode switching) */
            if (uwSpHpSwitch)
            {
                /* Set output to Headphone */
                Codec_SwitchOutput(OUTPUT_DEVICE_HEADPHONE);

                /* Display the current state of the player */
                LCD_DisplayStringLine(Line9, (uint8_t *)"      HEADPHONE     ");
            }
            else
            {
                /* Wait to avoid rebound */
                while (STM_EVAL_PBGetState(BUTTON_TAMPER) != Bit_SET);

                /* Increase volume by 5% */
                if (uwVolume < 95)
                    uwVolume += 5;
                else
                    uwVolume = 100;

                /* Apply the new volume to the codec */
                EVAL_AUDIO_VolumeCtl(uwVolume);
                LCD_DisplayStringLine(Line9, (uint8_t *)"       VOL:   +     ");
            }
        }

        /* Toggle LED4 */
        STM_EVAL_LEDToggle(LED3);

        /* Insert 50 ms delay */
        Delay(5);

        /* Toggle LED2 */
        STM_EVAL_LEDToggle(LED2);

        /* Insert 50 ms delay */
        Delay(5);
    }
}
/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
void main(void)
{
  /* Initialize LEDs mounted on STM8L152X-EVAL board */
  STM_EVAL_LEDInit(LED2);
  STM_EVAL_LEDInit(LED3);

  /* Initialize key and Joystick down buttons mounted on STM8L152X-EVAL board */
  STM_EVAL_PBInit(BUTTON_KEY, BUTTON_MODE_EXTI);
  STM_EVAL_PBInit(BUTTON_DOWN, BUTTON_MODE_EXTI);

#ifdef USE_STM8L1526_EVAL

  /* Set PC1 sensitivity to falling edge and low level */
  EXTI_SetPinSensitivity(EXTI_Pin_1, EXTI_Trigger_Falling_Low);
  /* Set PD4 sensitivity to falling edge and low level */
  EXTI_SetPinSensitivity(EXTI_Pin_4, EXTI_Trigger_Falling_Low);

  /*
    Change the software priority between key and joystick down buttons:
    - By default the Pin_1 (key) and Pin_4 have the same priority level: level3
    - We configure the Pin_4 (joystick_down) to have a higher sw priority than Pin_1 (key)
  */
  /* Set the EXTI1 IRQ (which is connected to key button) level to 1 */
  ITC_SetSoftwarePriority(EXTI1_IRQn, ITC_PriorityLevel_1);

  /* Set the EXTI4 IRQ (which is connected to joystick down button) software level to 2 */
  /* Thus joystick down button IRQ software priority is higher than key one */
  /* Pressing joystick down button interrupts the execution of the EXTI1 Interrupt Service Routine */
  ITC_SetSoftwarePriority(EXTI4_IRQn, ITC_PriorityLevel_2);

#elif defined USE_STM8L1528_EVAL

  /* Set PH6 sensitivity to falling edge and low level */
  EXTI_SetPinSensitivity(EXTI_Pin_6, EXTI_Trigger_Falling_Low);
  /* Set PG1 sensitivity to falling edge and low level */
  EXTI_SetPinSensitivity(EXTI_Pin_1, EXTI_Trigger_Falling_Low);

  /*
  Change the software priority between key and joystick down buttons:
  - By default the Pin_6 (key) and Pin_1 have the same priority level: level3
  - We configure the Pin_1 (joystick_down) to have a higher sw priority than Pin_6 (key)
  */
  /* Set the EXTI6 IRQ (which is connected to key button) level to 1 */
  ITC_SetSoftwarePriority(EXTI6_IRQn, ITC_PriorityLevel_1);

  /* Set the EXTI1 IRQ (which is connected to joystick down button) software level to 2 */
  /* Thus joystick down button IRQ software priority is higher than key one */
  /* Pressing joystick down button interrupts the execution of the EXTI6 Interrupt Service Routine */
  ITC_SetSoftwarePriority(EXTI1_IRQn, ITC_PriorityLevel_2);

#endif /* USE_STM8L152X_EVAL */

  /* enable interrupts by setting level 0 */
  enableInterrupts();

  /* LD2 and LD3 are on */
  STM_EVAL_LEDOn(LED2);
  STM_EVAL_LEDOn(LED3);

  while (1);
}
Exemple #6
0
/**
  * @brief   Main program.
  * @param  None
  * @retval None
  */
int main(void)
{
  /*!< At this stage the microcontroller clock setting is already configured, 
       this is 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
     */     
       
  /* NVIC configuration */
  NVIC_Configuration();
    
  /* Initialize Leds mounted on STM3210X-EVAL board */
  STM_EVAL_LEDInit(LED1);
  STM_EVAL_LEDInit(LED2);
  STM_EVAL_LEDInit(LED3);
  STM_EVAL_LEDInit(LED4);

  /* Enable PWR and BKP clock */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);

  /* Enable write access to Backup domain */
  PWR_BackupAccessCmd(ENABLE);

  /* Disable Tamper pin */
  BKP_TamperPinCmd(DISABLE);
  
  /* Disable Tamper interrupt */
  BKP_ITConfig(DISABLE);

  /* Tamper pin active on low level */
  BKP_TamperPinLevelConfig(BKP_TamperPinLevel_Low);

  /* Clear Tamper pin Event(TE) pending flag */
  BKP_ClearFlag();

  /* Enable Tamper interrupt */
  BKP_ITConfig(ENABLE);

  /* Enable Tamper pin */
  BKP_TamperPinCmd(ENABLE);
   
  /* Write data to Backup DRx registers */
  WriteToBackupReg(0xA53C);
  
  /* Check if the written data are correct */
  if(CheckBackupReg(0xA53C) == 0x00)
  {
    /* Turn on LED1 */
    STM_EVAL_LEDOn(LED1);
  }
  else
  {
    /* Turn on LED2 */
    STM_EVAL_LEDOn(LED2);
  }
        
  while (1)
  {    
  }
}
Exemple #7
0
/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{
  /*!< At this stage the microcontroller clock setting is already configured,
       this is done through SystemInit() function which is called from startup
       file (startup_stm32f2xx.s) before to branch to application main.
       To reconfigure the default setting of SystemInit() function, refer to
       system_stm32f2xx.c file
     */

  /* TIM Configuration */
  TIM_Config();

  TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);

  TIM_OCStructInit(&TIM_OCInitStructure);


  /* ---------------------------------------------------------------------------
    TIM3 Configuration: Output Compare Active Mode:
    In this example TIM3 input clock (TIM3CLK) is set to 2 * APB1 clock (PCLK1),
    since APB1 prescaler is different from 1.
      TIM3CLK = 2 * PCLK1
      PCLK1 = HCLK / 4
      => TIM3CLK = HCLK / 2 = SystemCoreClock /2

    To get TIM3 counter clock at 1 KHz, the prescaler is computed as follows:
       Prescaler = (TIM3CLK / TIM3 counter clock) - 1
       Prescaler = ((SystemCoreClock /2) /1 KHz) - 1

    Generate 4 signals with 4 different delays:
    TIM3_CH1 delay = CCR1_Val/TIM3 counter clock = 1000 ms
    TIM3_CH2 delay = CCR2_Val/TIM3 counter clock = 500 ms
    TIM3_CH3 delay = CCR3_Val/TIM3 counter clock = 250 ms
    TIM3_CH4 delay = CCR4_Val/TIM3 counter clock = 125 ms

    Note:
     SystemCoreClock variable holds HCLK frequency and is defined in system_stm32f2xx.c file.
     Each time the core clock (HCLK) changes, user had to call SystemCoreClockUpdate()
     function to update SystemCoreClock variable value. Otherwise, any configuration
     based on this variable will be incorrect.

  --------------------------------------------------------------------------- */

  /*Compute the prescaler value */
  PrescalerValue = (uint16_t) ((SystemCoreClock / 2) / 1000) - 1;

  /* Time base configuration */
  TIM_TimeBaseStructure.TIM_Period = 65535;
  TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;
  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

  TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);

  /* Output Compare Active Mode configuration: Channel1 */
  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Active;
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_Pulse = CCR1_Val;
  TIM_OC1Init(TIM3, &TIM_OCInitStructure);

  TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Disable);
  TIM_ARRPreloadConfig(TIM3, DISABLE);
  /* Output Compare Active Mode configuration: Channel2 */
  TIM_OCInitStructure.TIM_Pulse = CCR2_Val;
  TIM_OC2Init(TIM3, &TIM_OCInitStructure);

  TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Disable);

  /* Output Compare Active Mode configuration: Channel3 */
  TIM_OCInitStructure.TIM_Pulse = CCR3_Val;
  TIM_OC3Init(TIM3, &TIM_OCInitStructure);

  TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Disable);

  /* Output Compare Active Mode configuration: Channel4 */
  TIM_OCInitStructure.TIM_Pulse = CCR4_Val;
  TIM_OC4Init(TIM3, &TIM_OCInitStructure);

  TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Disable);

  /* TIM3 enable counter */
  TIM_Cmd(TIM3, ENABLE);

  TIM_GenerateEvent(TIM3, TIM_EventSource_Update);

  /* Turn on LED1 */
  STM_EVAL_LEDOn(LED1);

  while (1)
  {}
}
Exemple #8
0
/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
int main(void)
{
  /*!< At this stage the microcontroller clock setting is already configured, 
  this is done through SystemInit() function which is called from startup
  file (startup_stm32f0xx.s) before to branch to application main.
  To reconfigure the default setting of SystemInit() function, refer to
  system_stm32f0xx.c file
  */ 
  
  /* SPI configuration ------------------------------------------------------*/
  SPI_Config();
  
  /* SysTick configuration ---------------------------------------------------*/
  SysTickConfig();
  
  /* Initialize LEDs mounted on STM320518-EVAL board */
  STM_EVAL_LEDInit(LED1);
  STM_EVAL_LEDInit(LED2);
  STM_EVAL_LEDInit(LED3);
  STM_EVAL_LEDInit(LED4);
  
  /* Master board configuration ------------------------------------------------*/
#ifdef SPI_MASTER
  /* Initialize push-buttons mounted on STM320518-EVAL board */
  STM_EVAL_PBInit(BUTTON_RIGHT, BUTTON_MODE_GPIO);
  STM_EVAL_PBInit(BUTTON_LEFT, BUTTON_MODE_GPIO);
  STM_EVAL_PBInit(BUTTON_UP, BUTTON_MODE_GPIO);
  STM_EVAL_PBInit(BUTTON_DOWN, BUTTON_MODE_GPIO);
  STM_EVAL_PBInit(BUTTON_SEL, BUTTON_MODE_GPIO);
  
  /* Initializes the SPI communication */
  SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
  SPI_Init(SPIx, &SPI_InitStructure);
  
  /* Initialize the FIFO threshold */
  SPI_RxFIFOThresholdConfig(SPIx, SPI_RxFIFOThreshold_QF);
  
  /* Enable the Rx buffer not empty interrupt */
  SPI_I2S_ITConfig(SPIx, SPI_I2S_IT_RXNE, ENABLE);
  /* Enable the SPI Error interrupt */
  SPI_I2S_ITConfig(SPIx, SPI_I2S_IT_ERR, ENABLE);
  /* Data transfer is performed in the SPI interrupt routine */
  
  /* Enable the SPI peripheral */
  SPI_Cmd(SPIx, ENABLE);
  
  while (1)
  {   
    CmdTransmitted = 0x00;
    CmdReceived = 0x00;
    CmdStatus = 0x00;
    Tx_Idx = 0x00;
    Rx_Idx = 0x00;
    
    /* Clear the RxBuffer */
    Fill_Buffer(RxBuffer, TXBUFFERSIZE);
    PressedButton = Read_Joystick();
    
    while (PressedButton == JOY_NONE)
    {
      PressedButton = Read_Joystick();
    }    
    
    switch (PressedButton)
    {
      /* JOY_RIGHT button pressed */
    case JOY_RIGHT:
      CmdTransmitted = CMD_RIGHT;
      break;
      /* JOY_LEFT button pressed */ 
    case JOY_LEFT:
      CmdTransmitted = CMD_LEFT;
      break;
      /* JOY_UP button pressed */
    case JOY_UP:
      CmdTransmitted = CMD_UP;
      break;
      /* JOY_DOWN button pressed */
    case JOY_DOWN:
      CmdTransmitted = CMD_DOWN;
      break;
      /* JOY_SEL button pressed */
    case JOY_SEL:
      CmdTransmitted = CMD_SEL;
      break;
    default:
      break;
    }
    
    if (CmdTransmitted != 0x00)
    {    
      /* Enable the Tx buffer empty interrupt */
      SPI_I2S_ITConfig(SPIx, SPI_I2S_IT_TXE, ENABLE);
      
      /* Wait until end of data transfer or time out*/
      TimeOut = USER_TIMEOUT;
      while ((Rx_Idx < DATA_SIZE)&&(TimeOut != 0x00))
      {}
      if(TimeOut == 0)
      {
        TimeOut_UserCallback();
      } 
    }
    
    /* Waiting until TX FIFO is empty */
    while (SPI_GetTransmissionFIFOStatus(SPIx) != SPI_TransmissionFIFOStatus_Empty)
    {}
    
    /* Wait busy flag */
    while(SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_BSY) == SET)
    {}
    
    /* Waiting until RX FIFO is empty */
    while (SPI_GetReceptionFIFOStatus(SPIx) != SPI_ReceptionFIFOStatus_Empty)
    {}
    
    switch (CmdTransmitted)
    {
      /* Right button pressed */
    case CMD_RIGHT:
      if ((Buffercmp(TxBuffer, RxBuffer, DATA_SIZE, SPI_DATAMASK) == PASSED) && (CmdReceived == CMD_ACK))
      {
        /* Turn ON LED2 and LED3 */
        STM_EVAL_LEDOn(LED2);
        STM_EVAL_LEDOn(LED3);
        /* Turn all other LEDs off */
        STM_EVAL_LEDOff(LED4);
      }
      break;
      /* Left button pressed*/
    case CMD_LEFT:
      if ((Buffercmp(TxBuffer, RxBuffer, DATA_SIZE, SPI_DATAMASK) == PASSED) && (CmdReceived == CMD_ACK))
      {
        /* Turn ON LED4 */
        STM_EVAL_LEDOn(LED4);
        /* Turn all other LEDs off */
        STM_EVAL_LEDOff(LED2);
        STM_EVAL_LEDOff(LED3);
      }
      break;
      /* Up button pressed */
    case CMD_UP:
      if ((Buffercmp(TxBuffer, RxBuffer, DATA_SIZE, SPI_DATAMASK) == PASSED) && (CmdReceived == CMD_ACK))
      {
        /* Turn ON LED2 */
        STM_EVAL_LEDOn(LED2);
        /* Turn all other LEDs off */
        STM_EVAL_LEDOff(LED3);
        STM_EVAL_LEDOff(LED4);
      }
      break;
      /* Down button pressed */
    case CMD_DOWN:
      if ((Buffercmp(TxBuffer, RxBuffer, DATA_SIZE, SPI_DATAMASK) == PASSED) && (CmdReceived == CMD_ACK))
      {
        /* Turn ON LED3 */
        STM_EVAL_LEDOn(LED3);
        /* Turn all other LEDs off */
        STM_EVAL_LEDOff(LED2);
        STM_EVAL_LEDOff(LED4);
      }
      break;
      /* Sel button pressed */
    case CMD_SEL:
      if ((Buffercmp(TxBuffer, RxBuffer, DATA_SIZE, SPI_DATAMASK) == PASSED) && (CmdReceived == CMD_ACK))
      {
        /* Turn ON all LEDs */
        STM_EVAL_LEDOn(LED2);
        STM_EVAL_LEDOn(LED3);
        STM_EVAL_LEDOn(LED4);
      }
      break;
    default:
      break;
    }        
  }
#endif /* SPI_MASTER */
  
  /* Slave board configuration ----------------------------------------------*/
#ifdef SPI_SLAVE
  /* Initializes the SPI communication */
  SPI_I2S_DeInit(SPIx);
  SPI_InitStructure.SPI_Mode = SPI_Mode_Slave;
  SPI_Init(SPIx, &SPI_InitStructure);
  
  /* Initialize the FIFO threshold */
  SPI_RxFIFOThresholdConfig(SPIx, SPI_RxFIFOThreshold_QF);
  
  /* Enable the Rx buffer not empty interrupt */
  SPI_I2S_ITConfig(SPIx, SPI_I2S_IT_RXNE, ENABLE);
  
  /* Enable the SPI Error interrupt */
  SPI_I2S_ITConfig(SPIx, SPI_I2S_IT_ERR, ENABLE);
  
  /* Enable the SPI peripheral */
  SPI_Cmd(SPIx, ENABLE);
  
  /* Infinite Loop */
  while (1)
  {     
    CmdStatus = 0x00;
    CmdReceived = 0x00;
    Rx_Idx = 0x00;
    Tx_Idx = 0x00;
    
    /* Write the first data in SPI shift register before enabling the interrupt
    this data will be transmitted when the Slave receive the generated clock
    by the Master */    
    /* Enable the Tx buffer empty interrupt */
    SPI_SendData8(SPIx, CMD_ACK);
    SPI_I2S_ITConfig(SPIx, SPI_I2S_IT_TXE, ENABLE);
    
    /* Waiting Transaction code Byte */
    while (CmdStatus == 0x00)
    {}
    
    TimeOut = USER_TIMEOUT;
    while ((Rx_Idx < DATA_SIZE)&&(TimeOut != 0x00))
    {}
    if(TimeOut == 0)
    {
      TimeOut_UserCallback();
    }    
    
    switch (CmdReceived)
    {
      /* CMD_RIGHT command received or time out*/
    case CMD_RIGHT:
      if (Buffercmp(TxBuffer, RxBuffer, DATA_SIZE, SPI_DATAMASK) != FAILED) 
      {
        /* Turn ON LED2 and LED3 */
        STM_EVAL_LEDOn(LED2);
        STM_EVAL_LEDOn(LED3);
        /* Turn OFF LED4 */
        STM_EVAL_LEDOff(LED4);
      }
      break;
      /* CMD_LEFT command received or time out */
    case CMD_LEFT:
      if (Buffercmp(TxBuffer, RxBuffer, DATA_SIZE, SPI_DATAMASK) != FAILED)
      {
        /* Turn ON LED4 */
        STM_EVAL_LEDOn(LED4);
        /* Turn OFF LED2 and LED3 */
        STM_EVAL_LEDOff(LED2);
        STM_EVAL_LEDOff(LED3);
      }
      break;
      /* CMD_UP command received or time out*/
    case CMD_UP:
      if (Buffercmp(TxBuffer, RxBuffer, DATA_SIZE, SPI_DATAMASK) != FAILED)
      {
        /* Turn ON LED2 */
        STM_EVAL_LEDOn(LED2);
        /* Turn OFF LED3 and LED4 */
        STM_EVAL_LEDOff(LED3);
        STM_EVAL_LEDOff(LED4);
      }
      break;
      /* CMD_DOWN command received or time out */
    case CMD_DOWN:
      if (Buffercmp(TxBuffer, RxBuffer, DATA_SIZE, SPI_DATAMASK) != FAILED)
      {
        /* Turn ON LED3 */
        STM_EVAL_LEDOn(LED3);
        /* Turn OFF LED2 and LED4 */
        STM_EVAL_LEDOff(LED2);
        STM_EVAL_LEDOff(LED4);
      }
      break;
      /* CMD_SEL command received or time out */
    case CMD_SEL:
      if (Buffercmp(TxBuffer, RxBuffer, DATA_SIZE, SPI_DATAMASK) != FAILED)
      {
        /* Turn ON LED2, LED3 and LED4 */
        STM_EVAL_LEDOn(LED2);
        STM_EVAL_LEDOn(LED3);
        STM_EVAL_LEDOn(LED4);
      }
      break;
    default:
      break;   
    }
    
    /* Clear the RxBuffer */
    Fill_Buffer(RxBuffer, TXBUFFERSIZE);
    
    /* Waiting until TX FIFO is empty */
    while (SPI_GetTransmissionFIFOStatus(SPIx) != SPI_TransmissionFIFOStatus_Empty)
    {}    
    
    /* Wait busy flag */
    while(SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_BSY) == SET)
    {}
    
    /* Disable the Tx buffer empty interrupt */
    SPI_I2S_ITConfig(SPIx, SPI_I2S_IT_TXE, DISABLE);
    
    /* Waiting until RX FIFO is empty */
    while (SPI_GetReceptionFIFOStatus(SPIx) != SPI_ReceptionFIFOStatus_Empty)
    {} 
    
  }
#endif /* SPI_SLAVE */

}
Exemple #9
0
/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{
  /*!< At this stage the microcontroller clock setting is already configured, 
       this is done through SystemInit() function which is called from startup
       file (startup_stm32f40xx.s/startup_stm32f427x.s/startup_stm32f429x.s)  
       before to branch to application main.
       To reconfigure the default setting of SystemInit() function, refer to
       system_stm32f4xx.c file
     */     

  /* Initialize LEDs mounted on EVAL board */
  STM_EVAL_LEDInit(LED1);
  STM_EVAL_LEDInit(LED2);
  STM_EVAL_LEDInit(LED3);
  STM_EVAL_LEDInit(LED4);

  /* Initialize Key Button mounted on EVAL board */
  STM_EVAL_PBInit(BUTTON_KEY, BUTTON_MODE_GPIO);

  /* Test if Key push-button on EVAL board is pressed */
  if (STM_EVAL_PBGetState(BUTTON_KEY) == 0x00)
  {
    /* Get FLASH_WRP_SECTORS write protection status */
    SectorsWRPStatus = FLASH_OB_GetWRP() & FLASH_WRP_SECTORS;

    if (SectorsWRPStatus == 0x00)
    {
     /* If FLASH_WRP_SECTORS are write protected, disable the write protection */

      /* Enable the Flash option control register access */
      FLASH_OB_Unlock();

      /* Disable FLASH_WRP_SECTORS write protection */
      FLASH_OB_WRPConfig(FLASH_WRP_SECTORS, DISABLE); 

      /* Start the Option Bytes programming process */  
      if (FLASH_OB_Launch() != FLASH_COMPLETE)
      {
        /* User can add here some code to deal with this error */
        while (1)
        {
        }
      }
      /* Disable the Flash option control register access (recommended to protect 
         the option Bytes against possible unwanted operations) */
      FLASH_OB_Lock();

      /* Get FLASH_WRP_SECTORS write protection status */
      SectorsWRPStatus = FLASH_OB_GetWRP() & FLASH_WRP_SECTORS;
  
      /* Check if FLASH_WRP_SECTORS write protection is disabled */
      if (SectorsWRPStatus == FLASH_WRP_SECTORS)
      {
        /* OK, turn ON LED1 */
        STM_EVAL_LEDOn(LED1); 
      }
      else
      {
        /* KO, turn ON LED3 */
        STM_EVAL_LEDOn(LED3); 
      }
    }
    else
    { /* If FLASH_WRP_SECTORS are not write protected, enable the write protection */

      /* Enable the Flash option control register access */
      FLASH_OB_Unlock();

      /* Enable FLASH_WRP_SECTORS write protection */
      FLASH_OB_WRPConfig(FLASH_WRP_SECTORS, ENABLE); 

      /* Start the Option Bytes programming process */  
      if (FLASH_OB_Launch() != FLASH_COMPLETE)
      {
        /* User can add here some code to deal with this error */
        while (1)
        {
        }
      }

      /* Disable the Flash option control register access (recommended to protect 
         the option Bytes against possible unwanted operations) */
      FLASH_OB_Lock();

      /* Get FLASH_WRP_SECTORS write protection status */
      SectorsWRPStatus = FLASH_OB_GetWRP() & FLASH_WRP_SECTORS;

      /* Check if FLASH_WRP_SECTORS are write protected */
      if (SectorsWRPStatus == 0x00)
      {
        /* OK, turn ON LED4 */
        STM_EVAL_LEDOn(LED4); 
      }
      else
      {
        /* KO, turn ON LED3 */
        STM_EVAL_LEDOn(LED3); 
      }
    }
  }

  /* Turn ON LED2 */
  STM_EVAL_LEDOn(LED2); 

  while (1)
  {
  }
}
/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{
  /*!< At this stage the microcontroller clock setting is already configured, 
       this is done through SystemInit() function which is called from startup
       file (startup_stm32f401xx.s) before to branch to application main.
       To reconfigure the default setting of SystemInit() function, refer to
       system_stm32f40x.c file
     */     
       
  /* Initialize LEDs and User Button mounted on STM324F401C-DISCO board */       
  STM_EVAL_LEDInit(LED3);
  STM_EVAL_LEDInit(LED4);  
  STM_EVAL_LEDInit(LED5);
  STM_EVAL_PBInit(BUTTON_USER, BUTTON_MODE_EXTI);

  /* Setup SysTick Timer for 1 msec interrupts  */
  if (SysTick_Config(SystemCoreClock / 1000))
  { 
    /* Capture error */ 
    while (1);
  }

  /* Check if the system has resumed from IWDG reset */
  if (RCC_GetFlagStatus(RCC_FLAG_IWDGRST) != RESET)
  {
    /* IWDGRST flag set */
    /* Turn on LED5 */
    STM_EVAL_LEDOn(LED5);

    /* Clear reset flags */
    RCC_ClearFlag();
  }
  else
  {
    /* IWDGRST flag is not set */
    /* Turn off LED5 */
    STM_EVAL_LEDOff(LED5);
  }
 
  /* Get the LSI frequency:  TIM5 is used to measure the LSI frequency */
  LsiFreq = GetLSIFrequency();

  /* IWDG timeout equal to 250 ms (the timeout may varies due to LSI frequency
     dispersion) */
  /* Enable write access to IWDG_PR and IWDG_RLR registers */
  IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);

  /* IWDG counter clock: LSI/32 */
  IWDG_SetPrescaler(IWDG_Prescaler_32);

  /* Set counter reload value to obtain 250ms IWDG TimeOut.
     Counter Reload Value = 250ms/IWDG counter clock period
                          = 250ms / (LSI/32)
                          = 0.25s / (LsiFreq/32)
                          = LsiFreq/(32 * 4)
                          = LsiFreq/128
   */
  IWDG_SetReload(LsiFreq/128);

  /* Reload IWDG counter */
  IWDG_ReloadCounter();

  /* Enable IWDG (the LSI oscillator will be enabled by hardware) */
  IWDG_Enable();

  while (1)
  {
    /* Toggle LED4 */
    STM_EVAL_LEDToggle(LED4);

    /* Insert 240 ms delay */
    Delay(240);

    /* Reload IWDG counter */
    IWDG_ReloadCounter();  
  }
}
/**
  * @brief  This function handles External lines 9 to 5 interrupt request.
  * @param  None
  * @retval None
  */
void EXTI9_5_IRQHandler(void)
{
  if(EXTI_GetITStatus(IOE16_IT_EXTI_LINE) != RESET)
  {  
#ifdef IOE_INTERRUPT_MODE 
    __IO uint16_t tmpsr = 0;  
    static JOY_State_TypeDef JoyState = JOY_NONE;
    static TS_STATE* TS_State;
    
    /* Get the interrupt status register */
    tmpsr = IOE16_GetITStatus();
    
    /* Check Touch screen interrupt event occurred */
    if((tmpsr & IOE16_TS_IT) != 0)
    {
      /* Update the structure with the current position */
      TS_State = IOE_TS_GetState();  
    
      if ((TS_State->TouchDetected) && (TS_State->Y < 92) && (TS_State->Y > 52))
      {
        if ((TS_State->X > 60) && (TS_State->X < 120))
        {
          LCD_SetTextColor(LCD_COLOR_GREEN);   
          LCD_DisplayStringLine(LCD_LINE_10, (uint8_t *)"     LED1               ");
          STM_EVAL_LEDOn(LED1);
        }
        else if ((TS_State->X > 140) && (TS_State->X < 200))
        {
          LCD_SetTextColor(LCD_COLOR_YELLOW); 
          LCD_DisplayStringLine(LCD_LINE_10, (uint8_t *)"          LED2          ");
          STM_EVAL_LEDOn(LED2);
        }
        else if ((TS_State->X > 220) && (TS_State->X < 280))
        {
          LCD_SetTextColor(LCD_COLOR_RED); 
          LCD_DisplayStringLine(LCD_LINE_10, (uint8_t *)"               LED3     ");
          STM_EVAL_LEDOn(LED3);
        }     
        else if ((TS_State->X > 300) && (TS_State->X < 360))
        {
          LCD_SetTextColor(LCD_COLOR_BLUE); 
          LCD_DisplayStringLine(LCD_LINE_10, (uint8_t *)"                    LED4");
          STM_EVAL_LEDOn(LED4);
        }
      }
      else
      {
        STM_EVAL_LEDOff(LED1);
        STM_EVAL_LEDOff(LED2);
        STM_EVAL_LEDOff(LED3);
        STM_EVAL_LEDOff(LED4);
      }    
      
      /* Clear the interrupt pending bits */    
      IOE_ClearGITPending(IOE16_TS_IT);     
    }
    
    /* Check joystick interrupt event occurred */
    if((tmpsr & IOE16_JOY_IT) != 0 )
    {
      /* Get the joystick State */
      JoyState = IOE16_JoyStickGetState();
      
      /* Set the LCD Text Color */
      LCD_SetTextColor(Blue); 
  
      switch (JoyState)
      {
        case JOY_NONE:
          LCD_DisplayStringLine(LCD_LINE_5, (uint8_t *)"  JOY:     ----          ");
          break;
        case JOY_UP:
          LCD_DisplayStringLine(LCD_LINE_5, (uint8_t *)"  JOY:     UP            ");
          break;     
        case JOY_DOWN:
          LCD_DisplayStringLine(LCD_LINE_5, (uint8_t *)"  JOY:    DOWN           ");
          break;          
        case JOY_LEFT:
          LCD_DisplayStringLine(LCD_LINE_5, (uint8_t *)"  JOY:    LEFT           ");
          break;         
        case JOY_RIGHT:
          LCD_DisplayStringLine(LCD_LINE_5, (uint8_t *)"  JOY:    RIGHT          ");
          break;                 
        case JOY_CENTER:
          LCD_DisplayStringLine(LCD_LINE_5, (uint8_t *)"  JOY:   CENTER          ");
          break; 
        default:
          LCD_DisplayStringLine(LCD_LINE_5, (uint8_t *)"  JOY:   ERROR           ");
          break;         
      } 
    }
#endif /* IOE_INTERRUPT_MODE */

    /* Clear all pending bits */
    EXTI_ClearITPendingBit(IOE16_IT_EXTI_LINE);
    IOE_ClearGITPending(ALL_GIT);
    IOE16_GetITStatus();  
  }  
}