HXC Client Shield Repository.

Dependencies:   mbed

Committer:
kashish_mbed
Date:
Mon Mar 29 15:37:08 2021 +0000
Revision:
0:bacc6e701fb4
Child:
2:52bb5ee0a72d
Child:
3:5e1a54378107
Initial Commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kashish_mbed 0:bacc6e701fb4 1 /*
kashish_mbed 0:bacc6e701fb4 2 _ _ _____ _______
kashish_mbed 0:bacc6e701fb4 3 | | | | |_ _| |__ __|
kashish_mbed 0:bacc6e701fb4 4 | |__| | __ ___ __ | | ___ | |
kashish_mbed 0:bacc6e701fb4 5 | __ |/ _` \ \/ / | | / _ \| |
kashish_mbed 0:bacc6e701fb4 6 | | | | (_| |> < _| || (_) | |
kashish_mbed 0:bacc6e701fb4 7 |_| |_|\__,_/_/\_\_____\___/|_|
kashish_mbed 0:bacc6e701fb4 8 (C)2017 HaxIoT
kashish_mbed 0:bacc6e701fb4 9 */
kashish_mbed 0:bacc6e701fb4 10 /*******************************************************************************
kashish_mbed 0:bacc6e701fb4 11 * @File : main.cpp
kashish_mbed 0:bacc6e701fb4 12 * @Author : Fahad Mirza (Haxiot)
kashish_mbed 0:bacc6e701fb4 13 * @Version : V1.0.0
kashish_mbed 0:bacc6e701fb4 14 * @Modified: 18 October, 2018
kashish_mbed 0:bacc6e701fb4 15 * @Brief : Main file
kashish_mbed 0:bacc6e701fb4 16 ******************************************************************************
kashish_mbed 0:bacc6e701fb4 17 * @attention
kashish_mbed 0:bacc6e701fb4 18 *
kashish_mbed 0:bacc6e701fb4 19 * <h2><center>&copy; COPYRIGHT(c) 2017 Haxiot</center></h2>
kashish_mbed 0:bacc6e701fb4 20 *
kashish_mbed 0:bacc6e701fb4 21 * Redistribution and use in source and binary forms, with or without modification,
kashish_mbed 0:bacc6e701fb4 22 * are permitted provided that the following conditions are met:
kashish_mbed 0:bacc6e701fb4 23 * 1. Redistributions of source code must retain the above copyright notice,
kashish_mbed 0:bacc6e701fb4 24 * this list of conditions and the following disclaimer.
kashish_mbed 0:bacc6e701fb4 25 * 2. Redistributions in binary form must reproduce the above copyright notice,
kashish_mbed 0:bacc6e701fb4 26 * this list of conditions and the following disclaimer in the documentation
kashish_mbed 0:bacc6e701fb4 27 * and/or other materials provided with the distribution.
kashish_mbed 0:bacc6e701fb4 28 * 3. Neither the name of Haxiot nor the names of its contributors
kashish_mbed 0:bacc6e701fb4 29 * may be used to endorse or promote products derived from this software
kashish_mbed 0:bacc6e701fb4 30 * without specific prior written permission.
kashish_mbed 0:bacc6e701fb4 31 *
kashish_mbed 0:bacc6e701fb4 32 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
kashish_mbed 0:bacc6e701fb4 33 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
kashish_mbed 0:bacc6e701fb4 34 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
kashish_mbed 0:bacc6e701fb4 35 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
kashish_mbed 0:bacc6e701fb4 36 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
kashish_mbed 0:bacc6e701fb4 37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
kashish_mbed 0:bacc6e701fb4 38 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
kashish_mbed 0:bacc6e701fb4 39 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
kashish_mbed 0:bacc6e701fb4 40 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
kashish_mbed 0:bacc6e701fb4 41 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
kashish_mbed 0:bacc6e701fb4 42 *
kashish_mbed 0:bacc6e701fb4 43 ******************************************************************************
kashish_mbed 0:bacc6e701fb4 44 */
kashish_mbed 0:bacc6e701fb4 45 #include "mbed.h"
kashish_mbed 0:bacc6e701fb4 46 #include "hw.h"
kashish_mbed 0:bacc6e701fb4 47 #include "hxcclient_bsp.h"
kashish_mbed 0:bacc6e701fb4 48 #include "lora_conf.h"
kashish_mbed 0:bacc6e701fb4 49
kashish_mbed 0:bacc6e701fb4 50 /* Variables -----------------------------------------------------------------*/
kashish_mbed 0:bacc6e701fb4 51 //Flag to indicate if the MCU is Initialized
kashish_mbed 0:bacc6e701fb4 52 static bool McuInitialized = false;
kashish_mbed 0:bacc6e701fb4 53
kashish_mbed 0:bacc6e701fb4 54 /* Function Declarations -----------------------------------------------------*/
kashish_mbed 0:bacc6e701fb4 55 void SystemClock_Config(void);
kashish_mbed 0:bacc6e701fb4 56 void HW_Init(void);
kashish_mbed 0:bacc6e701fb4 57
kashish_mbed 0:bacc6e701fb4 58 int main()
kashish_mbed 0:bacc6e701fb4 59 {
kashish_mbed 0:bacc6e701fb4 60 HW_Init();
kashish_mbed 0:bacc6e701fb4 61
kashish_mbed 0:bacc6e701fb4 62 DBG_PRINTF("HXC900-NucleoL053R8 Demo Application\r\n");
kashish_mbed 0:bacc6e701fb4 63 Lora_init(&LoraConfigParam, &LoraDriverParam);
kashish_mbed 0:bacc6e701fb4 64
kashish_mbed 0:bacc6e701fb4 65 while(1)
kashish_mbed 0:bacc6e701fb4 66 {
kashish_mbed 0:bacc6e701fb4 67 Lora_fsm();
kashish_mbed 0:bacc6e701fb4 68 }
kashish_mbed 0:bacc6e701fb4 69 }
kashish_mbed 0:bacc6e701fb4 70
kashish_mbed 0:bacc6e701fb4 71 /******************************************************************************
kashish_mbed 0:bacc6e701fb4 72 * @Brief : This function initializes the hardware
kashish_mbed 0:bacc6e701fb4 73 * @Param : None
kashish_mbed 0:bacc6e701fb4 74 * @Return: None
kashish_mbed 0:bacc6e701fb4 75 ******************************************************************************/
kashish_mbed 0:bacc6e701fb4 76 void HW_Init(void)
kashish_mbed 0:bacc6e701fb4 77 {
kashish_mbed 0:bacc6e701fb4 78 if(McuInitialized == false)
kashish_mbed 0:bacc6e701fb4 79 {
kashish_mbed 0:bacc6e701fb4 80 // Reset of all peripherals, Initializes the Flash interface and the Systick.
kashish_mbed 0:bacc6e701fb4 81 HAL_Init();
kashish_mbed 0:bacc6e701fb4 82 SystemClock_Config();
kashish_mbed 0:bacc6e701fb4 83 Debug_UART_Init();
kashish_mbed 0:bacc6e701fb4 84 HW_RTC_Init();
kashish_mbed 0:bacc6e701fb4 85 BSP_LED_Init(LED_GREEN);// LED on Nucleo board
kashish_mbed 0:bacc6e701fb4 86 HXC_BSP_Init();
kashish_mbed 0:bacc6e701fb4 87 McuInitialized = true;
kashish_mbed 0:bacc6e701fb4 88 }
kashish_mbed 0:bacc6e701fb4 89 }
kashish_mbed 0:bacc6e701fb4 90
kashish_mbed 0:bacc6e701fb4 91 /******************************************************************************
kashish_mbed 0:bacc6e701fb4 92 * @Brief : System Clock Configuration
kashish_mbed 0:bacc6e701fb4 93 * The system Clock is configured as follow :
kashish_mbed 0:bacc6e701fb4 94 * System Clock source = PLL (HSI)
kashish_mbed 0:bacc6e701fb4 95 * SYSCLK(Hz) = 32000000
kashish_mbed 0:bacc6e701fb4 96 * HCLK(Hz) = 32000000
kashish_mbed 0:bacc6e701fb4 97 * AHB Prescaler = 1
kashish_mbed 0:bacc6e701fb4 98 * APB1 Prescaler = 1
kashish_mbed 0:bacc6e701fb4 99 * APB2 Prescaler = 1
kashish_mbed 0:bacc6e701fb4 100 * HSI Frequency(Hz) = 16000000
kashish_mbed 0:bacc6e701fb4 101 * PLLMUL = 6
kashish_mbed 0:bacc6e701fb4 102 * PLLDIV = 3
kashish_mbed 0:bacc6e701fb4 103 * Flash Latency(WS) = 1
kashish_mbed 0:bacc6e701fb4 104 * @Return: None
kashish_mbed 0:bacc6e701fb4 105 * @Note : This function enables all the clock necessary for the demo
kashish_mbed 0:bacc6e701fb4 106 * including UARTs
kashish_mbed 0:bacc6e701fb4 107 ******************************************************************************/
kashish_mbed 0:bacc6e701fb4 108 void SystemClock_Config(void)
kashish_mbed 0:bacc6e701fb4 109 {
kashish_mbed 0:bacc6e701fb4 110 RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
kashish_mbed 0:bacc6e701fb4 111 RCC_OscInitTypeDef RCC_OscInitStruct = {0};
kashish_mbed 0:bacc6e701fb4 112
kashish_mbed 0:bacc6e701fb4 113 // Enable HSI48 Oscillator for RNG analog part
kashish_mbed 0:bacc6e701fb4 114 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI48;
kashish_mbed 0:bacc6e701fb4 115 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
kashish_mbed 0:bacc6e701fb4 116 RCC_OscInitStruct.HSI48State = RCC_HSI48_ON;
kashish_mbed 0:bacc6e701fb4 117 if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
kashish_mbed 0:bacc6e701fb4 118 {
kashish_mbed 0:bacc6e701fb4 119 // Initialization Error
kashish_mbed 0:bacc6e701fb4 120 Error_Handler();
kashish_mbed 0:bacc6e701fb4 121 }
kashish_mbed 0:bacc6e701fb4 122
kashish_mbed 0:bacc6e701fb4 123 // Set Voltage scale1 as MCU will run at 32MHz
kashish_mbed 0:bacc6e701fb4 124 __HAL_RCC_PWR_CLK_ENABLE();
kashish_mbed 0:bacc6e701fb4 125 __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
kashish_mbed 0:bacc6e701fb4 126
kashish_mbed 0:bacc6e701fb4 127 // Poll VOSF bit of in PWR_CSR. Wait until it is reset to 0
kashish_mbed 0:bacc6e701fb4 128 while (__HAL_PWR_GET_FLAG(PWR_FLAG_VOS) != RESET) {};
kashish_mbed 0:bacc6e701fb4 129
kashish_mbed 0:bacc6e701fb4 130 // Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 clocks dividers
kashish_mbed 0:bacc6e701fb4 131 RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
kashish_mbed 0:bacc6e701fb4 132 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
kashish_mbed 0:bacc6e701fb4 133 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
kashish_mbed 0:bacc6e701fb4 134 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
kashish_mbed 0:bacc6e701fb4 135 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
kashish_mbed 0:bacc6e701fb4 136 if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
kashish_mbed 0:bacc6e701fb4 137 {
kashish_mbed 0:bacc6e701fb4 138 Error_Handler();
kashish_mbed 0:bacc6e701fb4 139 }
kashish_mbed 0:bacc6e701fb4 140
kashish_mbed 0:bacc6e701fb4 141 HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
kashish_mbed 0:bacc6e701fb4 142 HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
kashish_mbed 0:bacc6e701fb4 143
kashish_mbed 0:bacc6e701fb4 144 // SysTick_IRQn interrupt configuration
kashish_mbed 0:bacc6e701fb4 145 HAL_NVIC_SetPriority(SysTick_IRQn, 1, 0);
kashish_mbed 0:bacc6e701fb4 146 }
kashish_mbed 0:bacc6e701fb4 147
kashish_mbed 0:bacc6e701fb4 148 /******************************************************************************
kashish_mbed 0:bacc6e701fb4 149 * @Brief : Initializes the MSP.
kashish_mbed 0:bacc6e701fb4 150 * @Param : None
kashish_mbed 0:bacc6e701fb4 151 * @Return: None
kashish_mbed 0:bacc6e701fb4 152 ******************************************************************************/
kashish_mbed 0:bacc6e701fb4 153 void HAL_MspInit(void)
kashish_mbed 0:bacc6e701fb4 154 {
kashish_mbed 0:bacc6e701fb4 155 __HAL_RCC_PWR_CLK_ENABLE();
kashish_mbed 0:bacc6e701fb4 156
kashish_mbed 0:bacc6e701fb4 157 // Disable the Power Voltage Detector
kashish_mbed 0:bacc6e701fb4 158 HAL_PWR_DisablePVD();
kashish_mbed 0:bacc6e701fb4 159
kashish_mbed 0:bacc6e701fb4 160 // Enables the Ultra Low Power mode
kashish_mbed 0:bacc6e701fb4 161 HAL_PWREx_EnableUltraLowPower();
kashish_mbed 0:bacc6e701fb4 162
kashish_mbed 0:bacc6e701fb4 163 __HAL_FLASH_SLEEP_POWERDOWN_ENABLE();
kashish_mbed 0:bacc6e701fb4 164
kashish_mbed 0:bacc6e701fb4 165 /* In debug mode, e.g. when DBGMCU is activated, Arm core has always clocks
kashish_mbed 0:bacc6e701fb4 166 * And will not wait that the FLACH is ready to be read. It can miss in this
kashish_mbed 0:bacc6e701fb4 167 * case the first instruction. To overcome this issue, the flash remain clocked during sleep mode
kashish_mbed 0:bacc6e701fb4 168 */
kashish_mbed 0:bacc6e701fb4 169 DBG( __HAL_FLASH_SLEEP_POWERDOWN_DISABLE(); );
kashish_mbed 0:bacc6e701fb4 170 /*Enable fast wakeUp*/
kashish_mbed 0:bacc6e701fb4 171 HAL_PWREx_EnableFastWakeUp( );
kashish_mbed 0:bacc6e701fb4 172 }
kashish_mbed 0:bacc6e701fb4 173
kashish_mbed 0:bacc6e701fb4 174 #ifdef USE_FULL_ASSERT
kashish_mbed 0:bacc6e701fb4 175
kashish_mbed 0:bacc6e701fb4 176 /******************************************************************************
kashish_mbed 0:bacc6e701fb4 177 * @Brief : Reports the name of the source file and the source line number
kashish_mbed 0:bacc6e701fb4 178 * where the assert_param error has occurred.
kashish_mbed 0:bacc6e701fb4 179 * @Param : file: pointer to the source file name
kashish_mbed 0:bacc6e701fb4 180 * line: assert_param error line source number
kashish_mbed 0:bacc6e701fb4 181 * @Return: None
kashish_mbed 0:bacc6e701fb4 182 ******************************************************************************/
kashish_mbed 0:bacc6e701fb4 183 void assert_failed(uint8_t* file, uint32_t line)
kashish_mbed 0:bacc6e701fb4 184 {
kashish_mbed 0:bacc6e701fb4 185 /* User can add his own implementation to report the file name and line number,
kashish_mbed 0:bacc6e701fb4 186 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
kashish_mbed 0:bacc6e701fb4 187 }
kashish_mbed 0:bacc6e701fb4 188
kashish_mbed 0:bacc6e701fb4 189 #endif
kashish_mbed 0:bacc6e701fb4 190
kashish_mbed 0:bacc6e701fb4 191 /************************ (C) COPYRIGHT Haxiot ***** END OF FILE ****/
kashish_mbed 0:bacc6e701fb4 192