/**
  ******************************************************************************
  * @file    Common/utilities.c
  * @author  MCD Application Team
  * @version V1.0.0
  * @date    23-March-2012
  * @brief   This code has been developped for STM32F051 Discovery Kit.
*                      Functions for general purpose.
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT 2012 STMicroelectronics</center></h2>
  *
  * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  * You may not use this file except in compliance with the License.
  * You may obtain a copy of the License at:
  *
  *        http://www.st.com/software_license_agreement_liberty_v2
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  *
  ******************************************************************************
  */
/* Includes ------------------------------------------------------------------*/
#include "utilities.h"
#include "stm32f0xx_conf.h"


/* Private define ------------------------------------------------------------*/
#define  MS_DELAY_HSI (1000)
#define  SECOND_DELAY (1000*MS_DELAY_HSI)



/* Private consts ------------------------------------------------------------*/

/* Global variables ----------------------------------------------------------*/
uint32_t G_tickValue=0;
uint32_t G_tick=0;
void (*delay)(uint32_t) = delay_ms; //Delay function


/**
  * @brief  delay_ms  delay for some time in ms unit(accurate)
  * @param  n_ms is how many ms of time to delay
  * @retval None
  */
void delay_ms(uint32_t n_ms)
{
    // SysTick interrupt each 1000 Hz with HCLK equal to 32MHz
    // - 30 to compensate the overhead of this sub routine
    if(!n_ms)
    {
        return;
    }
    SysTick_Config(8000*PLL_MUL_X - 30);
    // Enable the SysTick Counter

    G_tickValue = n_ms;
    while(G_tickValue == n_ms);

    // SysTick interrupt each 1000 Hz with HCLK equal to 32MHz
    SysTick_Config(8000*PLL_MUL_X);
    while(G_tickValue != 0);
}

/**
  * @brief  delay_10us delay for some time in 10us unit(accurate)
  * @param  n_10us is how many 10us of time to delay
  * @retval None
  */
void delay_10us(uint32_t n_10us)
{
    // Enable the SysTick Counter
    if(!n_10us)
    {
        return;
    }
    G_tickValue = n_10us;
    // SysTick interrupt each 10us with HCLK equal to 24MHz
    // - 30 to compensate the overhead of this sub routine
    SysTick_Config(80*PLL_MUL_X - 30);
    while(G_tickValue == n_10us);

    // SysTick interrupt each 10us with HCLK equal to 24MHz
    SysTick_Config(80 * PLL_MUL_X);
    while(G_tickValue != 0);
}

/* Private functions ---------------------------------------------------------*/
/**
  * @brief  Init_LED LEDs initialization and User button
  * @param  None
  * @retval None
  */
void Init_LED(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;

    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);

    GPIO_InitStructure.GPIO_Pin = GPIO_LED_ERROR | GPIO_NRSTPD;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
}

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
