/*
 * Copyright (c) 2006-2019, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2021-05-24                  the first version
 */

#include <rthw.h>
#include <rtthread.h>
#include "Board.h"

#define _SCB_BASE       (0xE000E010UL)
#define _SYSTICK_CTRL   (*(rt_uint32_t *)(_SCB_BASE + 0x0))
#define _SYSTICK_LOAD   (*(rt_uint32_t *)(_SCB_BASE + 0x4))
#define _SYSTICK_VAL    (*(rt_uint32_t *)(_SCB_BASE + 0x8))
#define _SYSTICK_CALIB  (*(rt_uint32_t *)(_SCB_BASE + 0xC))
#define _SYSTICK_PRI    (*(rt_uint8_t  *)(0xE000ED23UL))

// Holds the system core clock, which is the system clock
// frequency supplied to the SysTick timer and the processor
// core clock.
extern uint32_t SystemCoreClock;

static rt_uint32_t _SysTick_Config(rt_uint32_t ticks)
{
    if ((ticks - 1) > 0xFFFFFF)
    {
        return 1;
    }

    _SYSTICK_LOAD = ticks - 1;
    _SYSTICK_PRI = 0xFF;
    _SYSTICK_VAL  = 0;
    _SYSTICK_CTRL = 0x07;

    return 0;
}

#if defined(RT_USING_USER_MAIN) && defined(RT_USING_HEAP)

/* Internal SRAM memory size[Kbytes] */
#define APM32_SRAM_SIZE      4
#define APM32_SRAM_END       (0x20000000 + APM32_SRAM_SIZE * 1024)

#if defined(__ARMCC_VERSION)
    extern int Image$$RW_IRAM1$$ZI$$Limit;
    #define HEAP_BEGIN      ((void *)&Image$$RW_IRAM1$$ZI$$Limit)
#elif __ICCARM__
    #pragma section="CSTACK"
    #define HEAP_BEGIN      (__segment_end("CSTACK"))
#else
    extern int __bss_end;
    #define HEAP_BEGIN      ((void *)&__bss_end)
#endif

#define HEAP_END        APM32_SRAM_END

RT_WEAK void* rt_heap_begin_get(void)
{
    return HEAP_BEGIN;
}

RT_WEAK void* rt_heap_end_get(void)
{
    return (void*)HEAP_END;
}
#endif

void rt_os_tick_callback(void)
{
    rt_interrupt_enter();

    rt_tick_increase();

    rt_interrupt_leave();
}

/**
 * This function will initial your board.
 */
void rt_hw_board_init(void)
{
//#error "TODO 1: OS Tick Configuration."
    /*
     * TODO 1: OS Tick Configuration
     * Enable the hardware timer and call the rt_os_tick_callback function
     * periodically with the frequency RT_TICK_PER_SECOND.
     */
    _SysTick_Config(SystemCoreClock / RT_TICK_PER_SECOND);

    /* Call components board initial (use INIT_BOARD_EXPORT()) */
#ifdef RT_USING_COMPONENTS_INIT
    rt_components_board_init();
#endif

#if defined(RT_USING_USER_MAIN) && defined(RT_USING_HEAP)
    rt_system_heap_init(rt_heap_begin_get(), rt_heap_end_get());
#endif
}

void SysTick_Handler(void)
{
    rt_os_tick_callback();
}

int uart_init(void)
{
    USART_Config_T UartHandle;
    GPIO_Config_T GPIO_ConfigStruct;

    /* Configure USART Tx and Rx */
    GPIO_ConfigStruct.mode = GPIO_MODE_OUT_PP;
    GPIO_ConfigStruct.speed = GPIO_SPEED_10MHz;
    GPIO_ConfigStruct.intEn = GPIO_EINT_DISABLE;
    GPIO_ConfigStruct.pin = GPIO_PIN_5;
    GPIO_Config(GPIOD, &GPIO_ConfigStruct);

    GPIO_ConfigStruct.mode = GPIO_MODE_IN_FLOATING;
    GPIO_ConfigStruct.pin = GPIO_PIN_6;
    GPIO_Config(GPIOD, &GPIO_ConfigStruct);

    RCM_EnableAPBPeriphClock(RCM_PERIPH_USART1);

    UartHandle.baudRate = 115200;
    UartHandle.mode = USART_MODE_TX_RX;
    UartHandle.interrupt = USART_INT_NONE;
    UartHandle.parity = USART_PARITY_NONE;
    UartHandle.stopBits = USART_STOP_BIT_1;
    UartHandle.wordLength = USART_WORD_LEN_8B;

    USART_Config(USART1, &UartHandle);
    USART_Enable(USART1);
    return 0;
}

INIT_BOARD_EXPORT(uart_init);

void rt_hw_console_output(const char* str)
{

    rt_enter_critical();

    while (*str != '\0')
    {
        if (*str == '\n')
        {
            while (USART_ReadStatusFlag(USART1, USART_FLAG_TXBE) == RESET);

            USART_TxData8(USART1, '\r');
        }

        while (USART_ReadStatusFlag(USART1, USART_FLAG_TXBE) == RESET);

        USART_TxData8(USART1, *str++);
    }

    rt_exit_critical();
}

char rt_hw_console_getchar(void)
{
    int ch = -1;

    if (USART_ReadStatusFlag(USART1, USART_FLAG_RXBNE) != RESET)
    {
        ch = (int)USART_RxData8(USART1);
        USART_ClearStatusFlag(USART1, USART_FLAG_RXBNE);
    }
    else
    {
        if (USART_ReadStatusFlag(USART1, USART_FLAG_OVREE) != RESET)
        {
            USART_ClearStatusFlag(USART1, USART_FLAG_OVREE);
        }

        rt_thread_mdelay(10);
    }

    return ch;
}
