140 lines
4.2 KiB
C
140 lines
4.2 KiB
C
/*****************************************************************************
|
|
* Copyright (c) 2019, Nations Technologies Inc.
|
|
*
|
|
* All rights reserved.
|
|
* ****************************************************************************
|
|
*
|
|
* Redistribution and use in source and binary forms, with or without
|
|
* modification, are permitted provided that the following conditions are met:
|
|
*
|
|
* - Redistributions of source code must retain the above copyright notice,
|
|
* this list of conditions and the disclaimer below.
|
|
*
|
|
* Nations' name may not be used to endorse or promote products derived from
|
|
* this software without specific prior written permission.
|
|
*
|
|
* DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
|
|
* DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
|
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
|
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
* ****************************************************************************/
|
|
|
|
/**
|
|
* @file log.c
|
|
* @author Nations Solution Team
|
|
* @version v1.0.0
|
|
*
|
|
* @copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
|
|
*/
|
|
#include "log.h"
|
|
|
|
#if LOG_ENABLE
|
|
|
|
#include "n32g45x.h"
|
|
#include "n32g45x_gpio.h"
|
|
#include "n32g45x_usart.h"
|
|
#include "n32g45x_rcc.h"
|
|
|
|
#define LOG_USARTx USART2
|
|
#define LOG_PERIPH RCC_APB1_PERIPH_USART2
|
|
#define LOG_GPIO GPIOB
|
|
#define LOG_PERIPH_GPIO RCC_APB2_PERIPH_GPIOB
|
|
#define LOG_REMAP GPIO_RMP3_USART2
|
|
#define LOG_TX_PIN GPIO_PIN_4
|
|
#define LOG_RX_PIN GPIO_PIN_5
|
|
|
|
void log_init(void)
|
|
{
|
|
GPIO_InitType GPIO_InitStructure;
|
|
USART_InitType USART_InitStructure;
|
|
|
|
// close JTAG
|
|
|
|
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_AFIO | LOG_PERIPH_GPIO, ENABLE);
|
|
if (LOG_REMAP)
|
|
{
|
|
if (LOG_REMAP == GPIO_RMP3_USART2)
|
|
{
|
|
// release PB4
|
|
GPIO_ConfigPinRemap(GPIO_RMP_SW_JTAG_NO_NJTRST, ENABLE);
|
|
}
|
|
GPIO_ConfigPinRemap(LOG_REMAP, ENABLE);
|
|
}
|
|
|
|
RCC_EnableAPB1PeriphClk(LOG_PERIPH, ENABLE);
|
|
|
|
|
|
GPIO_InitStructure.Pin = LOG_TX_PIN;
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
|
GPIO_InitPeripheral(LOG_GPIO, &GPIO_InitStructure);
|
|
|
|
//GPIO_InitStructure.Pin = LOG_RX_PIN;
|
|
//GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
|
|
//GPIO_InitPeripheral(LOG_GPIO, &GPIO_InitStructure);
|
|
|
|
USART_InitStructure.BaudRate = 115200;
|
|
USART_InitStructure.WordLength = USART_WL_8B;
|
|
USART_InitStructure.StopBits = USART_STPB_1;
|
|
USART_InitStructure.Parity = USART_PE_NO;
|
|
USART_InitStructure.HardwareFlowControl = USART_HFCTRL_NONE;
|
|
USART_InitStructure.Mode = USART_MODE_TX;
|
|
|
|
// init uart
|
|
USART_Init(USART2, &USART_InitStructure);
|
|
|
|
// enable uart
|
|
USART_Enable(USART2, ENABLE);
|
|
}
|
|
|
|
static int is_lr_sent = 0;
|
|
|
|
int fputc(int ch, FILE* f)
|
|
{
|
|
if (ch == '\r')
|
|
{
|
|
is_lr_sent = 1;
|
|
}
|
|
else if (ch == '\n')
|
|
{
|
|
if (!is_lr_sent)
|
|
{
|
|
USART_SendData(LOG_USARTx, (uint8_t)'\r');
|
|
/* Loop until the end of transmission */
|
|
while (USART_GetFlagStatus(LOG_USARTx, USART_FLAG_TXC) == RESET)
|
|
{
|
|
}
|
|
}
|
|
is_lr_sent = 0;
|
|
}
|
|
else
|
|
{
|
|
is_lr_sent = 0;
|
|
}
|
|
USART_SendData(LOG_USARTx, (uint8_t)ch);
|
|
/* Loop until the end of transmission */
|
|
while (USART_GetFlagStatus(LOG_USARTx, USART_FLAG_TXC) == RESET)
|
|
{
|
|
}
|
|
return ch;
|
|
}
|
|
|
|
#ifdef USE_FULL_ASSERT
|
|
|
|
__WEAK void assert_failed(const uint8_t* expr, const uint8_t* file, uint32_t line)
|
|
{
|
|
log_error("assertion failed: `%s` at %s:%d", expr, file, line);
|
|
while (1)
|
|
{
|
|
}
|
|
}
|
|
#endif // USE_FULL_ASSERT
|
|
|
|
#endif // LOG_ENABLE
|