board.c 2.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
/*
 * Copyright (c)
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date        Author    Email                    Notes
 * 2019-07-16  Kevin.Liu kevin.liu.mchp@gmail.com First Release
 */

#include <string.h>

#include <atmel_start.h>
#include "peripheral_clk_config.h"

#include <rtthread.h>
#include "board.h"

#ifdef RT_USING_SERIAL
extern int rt_hw_uart_init(void);
#endif

static struct io_descriptor* g_stdio;

void rt_hw_console_output(const char *str)
{
    io_write(g_stdio, (uint8_t *)str, strlen(str));
    while (TARGET_IO.stat != 0);
}
RTM_EXPORT(rt_hw_console_output);

static inline void hw_board_init_usart(void)
{
    usart_async_get_io_descriptor(&TARGET_IO, &g_stdio);
    usart_async_enable(&TARGET_IO);
}

/**
 * This is the timer interrupt service routine.
 *
 */
void SysTick_Handler(void)
{
    /* enter interrupt */
    rt_interrupt_enter();

    rt_tick_increase();

    /* leave interrupt */
    rt_interrupt_leave();
}

/**
 * This function will initial SAMC21 board.
 */
void rt_hw_board_init(void)
{
    /* Initializes MCU, drivers and middleware */
    atmel_start_init();

    /* enable USART stdout module */
    hw_board_init_usart();

    /* UART driver initialization is open by default */
#ifdef RT_USING_SERIAL
    rt_hw_uart_init();
#endif

    /* init systick */
    SysTick_Config(CONF_CPU_FREQUENCY / RT_TICK_PER_SECOND);

    /* set pend exception priority */
    NVIC_SetPriority(PendSV_IRQn, (1 << __NVIC_PRIO_BITS) - 1);

#ifdef RT_USING_HEAP
76
    #if defined(__ARMCC_VERSION)
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
        rt_system_heap_init((void*)&Image$$RW_IRAM1$$ZI$$Limit, (void*)HEAP_END);
    #elif __ICCARM__
        rt_system_heap_init((void*)HEAP_BEGIN, (void*)HEAP_END);
    #else
        /* init memory system */
        rt_system_heap_init((void*)&__bss_end, (void*)HEAP_END);
    #endif
#endif

    /* Set the shell console output device */
#if defined(RT_USING_CONSOLE) && defined(RT_USING_DEVICE)
    rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
#endif

#ifdef RT_USING_COMPONENTS_INIT
    rt_components_board_init();
#endif
}

/*@}*/