/*!
    \file    gd32f450i_eval.c
    \brief   firmware functions to manage leds, keys, COM ports
    
    \version 2016-08-15, V1.0.0, firmware for GD32F4xx
    \version 2018-12-12, V2.0.0, firmware for GD32F4xx
    \version 2020-09-30, V2.1.0, firmware for GD32F4xx
*/

/*
    Copyright (c) 2020, GigaDevice Semiconductor Inc.

    Redistribution and use in source and binary forms, with or without modification, 
are permitted provided that the following conditions are met:

    1. Redistributions of source code must retain the above copyright notice, this 
       list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright notice, 
       this list of conditions and the following disclaimer in the documentation 
       and/or other materials provided with the distribution.
    3. Neither the name of the copyright holder nor the names of its contributors 
       may be used to endorse or promote products derived from this software without 
       specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
*/

#include "gd32f450i_eval.h"
#include "lprintf.h"
#include "dev_pins.h"
#include "lprintf_config.h"

#define DEBUG_PORT_UART_NUM LPRINTF_UART_NUM

#define DEBUG_UART                     DEBUG_UART_NUM(DEBUG_PORT_UART_NUM)
#define DEBUG_UART_NUM(tnum)           _DEBUG_UART_NUM(tnum)
#if DEBUG_PORT_UART_NUM == 0 || DEBUG_PORT_UART_NUM == 5 || DEBUG_PORT_UART_NUM == 1 || DEBUG_PORT_UART_NUM == 2
    #define _DEBUG_UART_NUM(tnum)          USART##tnum
#else
    #define _DEBUG_UART_NUM(tnum)          USART##tnum
#endif

#define DEBUG_UART_RCU              DEBUG_UART_RCU_NUM(DEBUG_PORT_UART_NUM)
#define DEBUG_UART_RCU_NUM(tnum)    _DEBUG_UART_RCU_NUM(tnum)
#if DEBUG_PORT_UART_NUM == 0 || DEBUG_PORT_UART_NUM == 5 || DEBUG_PORT_UART_NUM == 1 || DEBUG_PORT_UART_NUM == 2
    #define _DEBUG_UART_RCU_NUM(tnum)   RCU_USART##tnum
#else
    #define _DEBUG_UART_RCU_NUM(tnum)   RCU_UART##tnum
#endif

int lprintf_uart_init(unsigned baud, unsigned databits, t_lprintf_uart_parity parity) {
    (void)(databits); // реализовано только 8 бит
    
    CHIP_PIN_CONFIG_AF(PIN_UART_DEBUG_RX);
    CHIP_PIN_CONFIG_AF(PIN_UART_DEBUG_TX);
    
    
    /* enable USART clock */
    rcu_periph_clock_enable(DEBUG_UART_RCU);

    /* USART configure */
    usart_deinit(DEBUG_UART);
    usart_baudrate_set(DEBUG_UART, baud);
    
    uint32_t paritycfg = USART_PM_NONE;
    if (parity==LPRINTF_UART_PARITY_ODD)
        paritycfg = USART_PM_ODD;
    else if (parity==LPRINTF_UART_PARITY_EVEN)
        paritycfg = USART_PM_EVEN;

    usart_parity_config(DEBUG_UART, paritycfg);
    usart_receive_config(DEBUG_UART, USART_RECEIVE_ENABLE);
    usart_transmit_config(DEBUG_UART, USART_TRANSMIT_ENABLE);
    usart_enable(DEBUG_UART);
    return 0;
}

/* TODO: возможно нужно реализовать для выхода в бутлоадер*/
void lprintf_close(void) {
}

/* TODO: возможно нужно реализовать для выхода в бутлоадер*/
void lclock_disable(void) {
}

void lprintf_putchar(char c) {
    usart_data_transmit(DEBUG_UART, (uint8_t)c);
    while(RESET == usart_flag_get(DEBUG_UART, USART_FLAG_TBE));
}

/*********************** Stubs */
/* TODO: */
void indicate_fatal_error(void) {
}

void usb_init(void) {
}
