Fahad Mirza / Mbed 2 deprecated Nucleo_HXC900

Dependencies:   mbed

main.h

Committer:
fahadmirza
Date:
2018-07-12
Revision:
0:a0c5877bd360
Child:
4:511677d804ac

File content as of revision 0:a0c5877bd360:

/*
_    _            _____   _______
 | |  | |          |_   _| |__   __|
 | |__| | __ ___  __ | |  ___ | |
 |  __  |/ _` \ \/ / | | / _ \| |
 | |  | | (_| |>  < _| || (_) | |
 |_|  |_|\__,_/_/\_\_____\___/|_|
    (C)2017 HaxIoT

Description: contains hardware configuration Macros and Constants
License: Revised BSD License, see LICENSE.TXT file include in the project
*/
/*******************************************************************************
  * @File Name    : main.h
  * @Author       : Fahad Mirza
  * @Created      : 22 January, 2018
  * @Modified     : 04 April, 2018
  * @Description  : Header file for main.c
********************************************************************************/

#ifndef MAIN_H_
#define MAIN_H_

#include "hw.h"
#include "debug.h"
#include "delay.h"
#include "low_power_manager.h"

/* Macros --------------------------------------------------------------------*/
// Uplink packet acknowledgment configuration
#define UNCONFIRMED 0
#define CONFIRMED   1

#define ENABLE_FAST_WAKEUP

/* Variables -----------------------------------------------------------------*/
//Flag to indicate if the MCU is Initialized
static bool McuInitialized = false;



/******************************************************************************
  * @brief :  System Clock Configuration
  * The system Clock is configured as follow :
  *      System Clock source            = PLL (HSI)
  *      SYSCLK(Hz)                     = 32000000
  *      HCLK(Hz)                       = 32000000
  *      AHB Prescaler                  = 1
  *      APB1 Prescaler                 = 1
  *      APB2 Prescaler                 = 1
  *      HSI Frequency(Hz)              = 16000000
  *      PLLMUL                         = 6
  *      PLLDIV                         = 3
  *      Flash Latency(WS)              = 1
  * @retval: None
  * @note  : This function enables all the clock necessary for the demo
  *          including UARTs
******************************************************************************/
void SystemClock_Config(void)
{
	RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
    RCC_OscInitTypeDef RCC_OscInitStruct = {0};
    RCC_PeriphCLKInitTypeDef PeriphClkInit;

    // Enable HSI48 Oscillator for RNG analog part
    RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI48;
    RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
    RCC_OscInitStruct.HSI48State = RCC_HSI48_ON;
    if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
    {
      // Initialization Error
      Error_Handler();
    }

//    // Enable HSI Oscillator and Activate PLL with HSI as source
//    RCC_OscInitStruct.OscillatorType      = RCC_OSCILLATORTYPE_HSI;
//    RCC_OscInitStruct.HSEState            = RCC_HSE_OFF;
//    RCC_OscInitStruct.HSIState            = RCC_HSI_ON;
//    RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
//    RCC_OscInitStruct.PLL.PLLState        = RCC_PLL_ON;
//    RCC_OscInitStruct.PLL.PLLSource       = RCC_PLLSOURCE_HSI;
//    RCC_OscInitStruct.PLL.PLLMUL          = RCC_PLLMUL_6;
//    RCC_OscInitStruct.PLL.PLLDIV          = RCC_PLLDIV_3;
//
//    if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
//    {
//    	Error_Handler();
//    }

    // Set Voltage scale1 as MCU will run at 32MHz
    __HAL_RCC_PWR_CLK_ENABLE();
    __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

    // Poll VOSF bit of in PWR_CSR. Wait until it is reset to 0
    while (__HAL_PWR_GET_FLAG(PWR_FLAG_VOS) != RESET) {};

    // Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 clocks dividers
    RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
    RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
    RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
    RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
    RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
    if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
    {
    	Error_Handler();
    }
    

//    // Select clock for both UART1 and UART2
//    PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART2;
//    PeriphClkInit.Usart2ClockSelection = RCC_USART2CLKSOURCE_HSI;
//    HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit);
//
//    PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1;
//    PeriphClkInit.Usart2ClockSelection = RCC_USART1CLKSOURCE_HSI;
//    HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit);

    HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
    HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

    // SysTick_IRQn interrupt configuration
    HAL_NVIC_SetPriority(SysTick_IRQn, 1, 0);
}

/******************************************************************************
 * @Brief : Set all pin as analog
 * @Param : none
 * @return: none
******************************************************************************/
void HW_GpioInit(void)
{
	GPIO_InitTypeDef GPIO_InitStruct;

    // STM32L0 Gpios are all configured as analog input at Reset
    __HAL_RCC_GPIOA_CLK_ENABLE();
    __HAL_RCC_GPIOB_CLK_ENABLE();
    __HAL_RCC_GPIOC_CLK_ENABLE();

    /*GPIOC*/
    GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3
                          |GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7
                          |GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_12;
    GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

    /*GPIOA*/
    GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_4|GPIO_PIN_6
                          |GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10
                          |GPIO_PIN_11|GPIO_PIN_12|GPIO_PIN_15;

    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    /*GPIOB*/
    GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_10
                          |GPIO_PIN_11|GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14
                          |GPIO_PIN_15|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5
                          |GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9;

    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

    __HAL_RCC_GPIOA_CLK_DISABLE();
    __HAL_RCC_GPIOB_CLK_DISABLE();
    __HAL_RCC_GPIOC_CLK_DISABLE();
}

/******************************************************************************
  * @Brief : This function initializes the hardware
  * @Param : None
  * @Retval: None
******************************************************************************/
void HW_Init(void)
{
  if(McuInitialized == false)
  {
  	// Reset of all peripherals, Initializes the Flash interface and the Systick.
    HAL_Init();
    SystemClock_Config();
    HW_GpioInit();
    Debug_UART_Init();
    
	#if defined( USE_BOOTLOADER )
		/* Set the Vector Table base location at 0x3000 */
		NVIC_SetVectorTable( NVIC_VectTab_FLASH, 0x3000 );
	#endif

    HW_RTC_Init();

    // LED on Nucleo board
    BSP_LED_Init(LED_GREEN);
    // Todo: Add UART initialization here
    McuInitialized = true;
  }
}

/******************************************************************************
  * @Brief : Initializes the MSP.
  * @Param : None
  * @retval: None
******************************************************************************/
void HAL_MspInit(void)
{
	__HAL_RCC_PWR_CLK_ENABLE();

	// Disable the Power Voltage Detector
	HAL_PWR_DisablePVD();

	// Enables the Ultra Low Power mode
	HAL_PWREx_EnableUltraLowPower();

	__HAL_FLASH_SLEEP_POWERDOWN_ENABLE();

	/* In debug mode, e.g. when DBGMCU is activated, Arm core has always clocks
	 * And will not wait that the FLACH is ready to be read. It can miss in this
	 * case the first instruction. To overcome this issue, the flash remain clocked during sleep mode
	 */
	DBG( __HAL_FLASH_SLEEP_POWERDOWN_DISABLE(); );

	#ifdef ENABLE_FAST_WAKEUP
		/*Enable fast wakeUp*/
		HAL_PWREx_EnableFastWakeUp( );
	#else
		HAL_PWREx_DisableFastWakeUp( );
	#endif
}


#ifdef USE_FULL_ASSERT

/******************************************************************************
   * @Brief : Reports the name of the source file and the source line number
   *          where the assert_param error has occurred.
   * @Param : file: pointer to the source file name
   *          line: assert_param error line source number
   * @Retval: None
******************************************************************************/
void assert_failed(uint8_t* file, uint32_t line)
{
  /* User can add his own implementation to report the file name and line number,
    ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
}

#endif

#endif /* MAIN_H_ */