A demo application for HXC900 LoRaWAN module using Nucleo-L053R8.

Dependencies:   mbed

Committer:
fahadmirza
Date:
Thu Jul 12 00:50:48 2018 +0000
Revision:
0:a0c5877bd360
Porting complete!

Who changed what in which revision?

UserRevisionLine numberNew contents of line
fahadmirza 0:a0c5877bd360 1 /******************************************************************************
fahadmirza 0:a0c5877bd360 2 * @file debug.c
fahadmirza 0:a0c5877bd360 3 * @author MCD Application Team
fahadmirza 0:a0c5877bd360 4 * @version V1.1.4
fahadmirza 0:a0c5877bd360 5 * @date 08-January-2018
fahadmirza 0:a0c5877bd360 6 * @brief debug API
fahadmirza 0:a0c5877bd360 7 ******************************************************************************
fahadmirza 0:a0c5877bd360 8 * @attention
fahadmirza 0:a0c5877bd360 9 *
fahadmirza 0:a0c5877bd360 10 * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics International N.V.
fahadmirza 0:a0c5877bd360 11 * All rights reserved.</center></h2>
fahadmirza 0:a0c5877bd360 12 *
fahadmirza 0:a0c5877bd360 13 * Redistribution and use in source and binary forms, with or without
fahadmirza 0:a0c5877bd360 14 * modification, are permitted, provided that the following conditions are met:
fahadmirza 0:a0c5877bd360 15 *
fahadmirza 0:a0c5877bd360 16 * 1. Redistribution of source code must retain the above copyright notice,
fahadmirza 0:a0c5877bd360 17 * this list of conditions and the following disclaimer.
fahadmirza 0:a0c5877bd360 18 * 2. Redistributions in binary form must reproduce the above copyright notice,
fahadmirza 0:a0c5877bd360 19 * this list of conditions and the following disclaimer in the documentation
fahadmirza 0:a0c5877bd360 20 * and/or other materials provided with the distribution.
fahadmirza 0:a0c5877bd360 21 * 3. Neither the name of STMicroelectronics nor the names of other
fahadmirza 0:a0c5877bd360 22 * contributors to this software may be used to endorse or promote products
fahadmirza 0:a0c5877bd360 23 * derived from this software without specific written permission.
fahadmirza 0:a0c5877bd360 24 * 4. This software, including modifications and/or derivative works of this
fahadmirza 0:a0c5877bd360 25 * software, must execute solely and exclusively on microcontroller or
fahadmirza 0:a0c5877bd360 26 * microprocessor devices manufactured by or for STMicroelectronics.
fahadmirza 0:a0c5877bd360 27 * 5. Redistribution and use of this software other than as permitted under
fahadmirza 0:a0c5877bd360 28 * this license is void and will automatically terminate your rights under
fahadmirza 0:a0c5877bd360 29 * this license.
fahadmirza 0:a0c5877bd360 30 *
fahadmirza 0:a0c5877bd360 31 * THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS"
fahadmirza 0:a0c5877bd360 32 * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT
fahadmirza 0:a0c5877bd360 33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
fahadmirza 0:a0c5877bd360 34 * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY
fahadmirza 0:a0c5877bd360 35 * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT
fahadmirza 0:a0c5877bd360 36 * SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
fahadmirza 0:a0c5877bd360 37 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
fahadmirza 0:a0c5877bd360 38 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
fahadmirza 0:a0c5877bd360 39 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
fahadmirza 0:a0c5877bd360 40 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
fahadmirza 0:a0c5877bd360 41 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
fahadmirza 0:a0c5877bd360 42 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
fahadmirza 0:a0c5877bd360 43 *
fahadmirza 0:a0c5877bd360 44 ******************************************************************************
fahadmirza 0:a0c5877bd360 45 */
fahadmirza 0:a0c5877bd360 46
fahadmirza 0:a0c5877bd360 47 /* Includes ------------------------------------------------------------------*/
fahadmirza 0:a0c5877bd360 48 #include <stdarg.h>
fahadmirza 0:a0c5877bd360 49 #include "hw.h"
fahadmirza 0:a0c5877bd360 50 #include "tiny_vsnprintf.h"
fahadmirza 0:a0c5877bd360 51
fahadmirza 0:a0c5877bd360 52 static UART_HandleTypeDef debugUart;
fahadmirza 0:a0c5877bd360 53 static char buffTx[256];
fahadmirza 0:a0c5877bd360 54
fahadmirza 0:a0c5877bd360 55 //static bool Debug_UART_Init(void);
fahadmirza 0:a0c5877bd360 56
fahadmirza 0:a0c5877bd360 57 /**
fahadmirza 0:a0c5877bd360 58 * @brief Initializes the debug
fahadmirza 0:a0c5877bd360 59 * @param None
fahadmirza 0:a0c5877bd360 60 * @retval None
fahadmirza 0:a0c5877bd360 61 */
fahadmirza 0:a0c5877bd360 62 void DBG_Init( void )
fahadmirza 0:a0c5877bd360 63 {
fahadmirza 0:a0c5877bd360 64 #ifdef DEBUG
fahadmirza 0:a0c5877bd360 65 GPIO_InitTypeDef gpioinitstruct = {0};
fahadmirza 0:a0c5877bd360 66
fahadmirza 0:a0c5877bd360 67 /* Enable the GPIO_B Clock */
fahadmirza 0:a0c5877bd360 68 __HAL_RCC_GPIOB_CLK_ENABLE();
fahadmirza 0:a0c5877bd360 69
fahadmirza 0:a0c5877bd360 70 /* Configure the GPIO pin */
fahadmirza 0:a0c5877bd360 71 gpioinitstruct.Mode = GPIO_MODE_OUTPUT_PP;
fahadmirza 0:a0c5877bd360 72 gpioinitstruct.Pull = GPIO_PULLUP;
fahadmirza 0:a0c5877bd360 73 gpioinitstruct.Speed = GPIO_SPEED_HIGH;
fahadmirza 0:a0c5877bd360 74
fahadmirza 0:a0c5877bd360 75 gpioinitstruct.Pin = (GPIO_PIN_12 | GPIO_PIN_13| GPIO_PIN_14 | GPIO_PIN_15);
fahadmirza 0:a0c5877bd360 76 HAL_GPIO_Init(GPIOB, &gpioinitstruct);
fahadmirza 0:a0c5877bd360 77
fahadmirza 0:a0c5877bd360 78 /* Reset debug Pins */
fahadmirza 0:a0c5877bd360 79 HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_RESET);
fahadmirza 0:a0c5877bd360 80 HAL_GPIO_WritePin(GPIOB, GPIO_PIN_13, GPIO_PIN_RESET);
fahadmirza 0:a0c5877bd360 81 HAL_GPIO_WritePin(GPIOB, GPIO_PIN_14, GPIO_PIN_RESET);
fahadmirza 0:a0c5877bd360 82 HAL_GPIO_WritePin(GPIOB, GPIO_PIN_15, GPIO_PIN_RESET);
fahadmirza 0:a0c5877bd360 83 #if 0
fahadmirza 0:a0c5877bd360 84 HAL_RCC_MCOConfig(RCC_MCO1, RCC_MCO1SOURCE_SYSCLK, RCC_MCODIV_1);
fahadmirza 0:a0c5877bd360 85 #endif
fahadmirza 0:a0c5877bd360 86
fahadmirza 0:a0c5877bd360 87 __HAL_RCC_DBGMCU_CLK_ENABLE( );
fahadmirza 0:a0c5877bd360 88
fahadmirza 0:a0c5877bd360 89 HAL_DBGMCU_EnableDBGSleepMode( );
fahadmirza 0:a0c5877bd360 90 HAL_DBGMCU_EnableDBGStopMode( );
fahadmirza 0:a0c5877bd360 91 HAL_DBGMCU_EnableDBGStandbyMode( );
fahadmirza 0:a0c5877bd360 92
fahadmirza 0:a0c5877bd360 93 #else /* DEBUG */
fahadmirza 0:a0c5877bd360 94 /* sw interface off*/
fahadmirza 0:a0c5877bd360 95 GPIO_InitTypeDef GPIO_InitStructure = {0};
fahadmirza 0:a0c5877bd360 96
fahadmirza 0:a0c5877bd360 97 GPIO_InitStructure.Mode = GPIO_MODE_ANALOG;
fahadmirza 0:a0c5877bd360 98 GPIO_InitStructure.Pull = GPIO_NOPULL;
fahadmirza 0:a0c5877bd360 99 GPIO_InitStructure.Pin = (GPIO_PIN_13 | GPIO_PIN_14);
fahadmirza 0:a0c5877bd360 100 __GPIOA_CLK_ENABLE();
fahadmirza 0:a0c5877bd360 101 HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
fahadmirza 0:a0c5877bd360 102 __GPIOA_CLK_DISABLE();
fahadmirza 0:a0c5877bd360 103
fahadmirza 0:a0c5877bd360 104 // ToDo : Code below does something and code wont work. Need to investigate more.
fahadmirza 0:a0c5877bd360 105 //__HAL_RCC_DBGMCU_CLK_ENABLE( );
fahadmirza 0:a0c5877bd360 106 //HAL_DBGMCU_DisableDBGSleepMode( );
fahadmirza 0:a0c5877bd360 107 //HAL_DBGMCU_DisableDBGStopMode( );
fahadmirza 0:a0c5877bd360 108 //HAL_DBGMCU_DisableDBGStandbyMode( );
fahadmirza 0:a0c5877bd360 109 //__HAL_RCC_DBGMCU_CLK_DISABLE( );
fahadmirza 0:a0c5877bd360 110 #endif
fahadmirza 0:a0c5877bd360 111
fahadmirza 0:a0c5877bd360 112 Debug_UART_Init();
fahadmirza 0:a0c5877bd360 113 }
fahadmirza 0:a0c5877bd360 114
fahadmirza 0:a0c5877bd360 115 void Debug_UART_Init(void)
fahadmirza 0:a0c5877bd360 116 {
fahadmirza 0:a0c5877bd360 117 debugUart.Instance = DBG_UARTX;
fahadmirza 0:a0c5877bd360 118 debugUart.Init.BaudRate = 9600;
fahadmirza 0:a0c5877bd360 119 debugUart.Init.WordLength = UART_WORDLENGTH_8B;
fahadmirza 0:a0c5877bd360 120 debugUart.Init.StopBits = UART_STOPBITS_1;
fahadmirza 0:a0c5877bd360 121 debugUart.Init.Parity = UART_PARITY_NONE;
fahadmirza 0:a0c5877bd360 122 debugUart.Init.Mode = UART_MODE_TX_RX;
fahadmirza 0:a0c5877bd360 123 debugUart.Init.HwFlowCtl = UART_HWCONTROL_NONE;
fahadmirza 0:a0c5877bd360 124 debugUart.Init.OverSampling = UART_OVERSAMPLING_16;
fahadmirza 0:a0c5877bd360 125 debugUart.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
fahadmirza 0:a0c5877bd360 126 debugUart.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
fahadmirza 0:a0c5877bd360 127
fahadmirza 0:a0c5877bd360 128 HAL_UART_Init(&debugUart);
fahadmirza 0:a0c5877bd360 129 }
fahadmirza 0:a0c5877bd360 130
fahadmirza 0:a0c5877bd360 131 void Debug_UART_SendBytes(const char *format, ...)
fahadmirza 0:a0c5877bd360 132 {
fahadmirza 0:a0c5877bd360 133 va_list args;
fahadmirza 0:a0c5877bd360 134 static __IO uint16_t len = 0;
fahadmirza 0:a0c5877bd360 135 uint16_t current_len;
fahadmirza 0:a0c5877bd360 136
fahadmirza 0:a0c5877bd360 137 va_start(args, format);
fahadmirza 0:a0c5877bd360 138
fahadmirza 0:a0c5877bd360 139 BACKUP_PRIMASK();
fahadmirza 0:a0c5877bd360 140 DISABLE_IRQ();
fahadmirza 0:a0c5877bd360 141 if (len != 0)
fahadmirza 0:a0c5877bd360 142 {
fahadmirza 0:a0c5877bd360 143 if (len != sizeof(buffTx))
fahadmirza 0:a0c5877bd360 144 {
fahadmirza 0:a0c5877bd360 145 current_len = len; /* use current_len instead of volatile len in below computation */
fahadmirza 0:a0c5877bd360 146 len = current_len + tiny_vsnprintf_like(buffTx + current_len, sizeof(buffTx) - current_len, format, args);
fahadmirza 0:a0c5877bd360 147 }
fahadmirza 0:a0c5877bd360 148 RESTORE_PRIMASK();
fahadmirza 0:a0c5877bd360 149 va_end(args);
fahadmirza 0:a0c5877bd360 150 return;
fahadmirza 0:a0c5877bd360 151 }
fahadmirza 0:a0c5877bd360 152 else
fahadmirza 0:a0c5877bd360 153 {
fahadmirza 0:a0c5877bd360 154 len = tiny_vsnprintf_like(buffTx, sizeof(buffTx), format, args);
fahadmirza 0:a0c5877bd360 155 }
fahadmirza 0:a0c5877bd360 156
fahadmirza 0:a0c5877bd360 157 current_len = len;
fahadmirza 0:a0c5877bd360 158 RESTORE_PRIMASK();
fahadmirza 0:a0c5877bd360 159 HAL_UART_Transmit(&debugUart, (uint8_t *)buffTx, current_len, 5000);
fahadmirza 0:a0c5877bd360 160 len = 0; // ToDo
fahadmirza 0:a0c5877bd360 161 va_end(args);
fahadmirza 0:a0c5877bd360 162 }
fahadmirza 0:a0c5877bd360 163
fahadmirza 0:a0c5877bd360 164 /**
fahadmirza 0:a0c5877bd360 165 * @brief Error_Handler
fahadmirza 0:a0c5877bd360 166 * @param None
fahadmirza 0:a0c5877bd360 167 * @retval None
fahadmirza 0:a0c5877bd360 168 */
fahadmirza 0:a0c5877bd360 169 void Error_Handler( void )
fahadmirza 0:a0c5877bd360 170 {
fahadmirza 0:a0c5877bd360 171 DBG_PRINTF("Error_Handler\n\r");
fahadmirza 0:a0c5877bd360 172 while(1);
fahadmirza 0:a0c5877bd360 173 }
fahadmirza 0:a0c5877bd360 174
fahadmirza 0:a0c5877bd360 175 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
fahadmirza 0:a0c5877bd360 176
fahadmirza 0:a0c5877bd360 177
fahadmirza 0:a0c5877bd360 178