summaryrefslogtreecommitdiff
path: root/bsp/nxp/lpc/lpc43xx/M4/applications/application.c
blob: c02c3adf9e78708965c898d9cde06996be6a2a57 (plain)
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2014-07-13     xiaonong     port for lpc43xx
 */

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

#ifdef RT_USING_FINSH
#include <finsh.h>
#include <shell.h>
#endif

static const unsigned char _M0_CODE[] rt_section("M0_CODE") = {
// #include "M0_CODE.h"
};

static void _boot_M0(void)
{
    volatile uint32_t u32REG,  u32Val;

    LPC_CREG->M0APPMEMMAP = (uint32_t)&_M0_CODE[0];

    // Release Slave from reset,  first read status
    u32REG = LPC_RGU->RESET_ACTIVE_STATUS1;

    // If the M0 is being held in reset,  release it...
    // 1 = no reset,  0 = reset
    while(!(u32REG & (1u << 24)))
    {
        u32Val = (~(u32REG) & (~(1 << 24)));
        LPC_RGU->RESET_CTRL1 = u32Val;
        u32REG = LPC_RGU->RESET_ACTIVE_STATUS1;
    }

    rt_kprintf("M0 boot to %p\n", &_M0_CODE[0]);
}

/* thread phase init */
void rt_init_thread_entry(void *parameter)
{
    /*
     *register unsigned int _msp __asm("msp");
     *register unsigned int _psp __asm("psp");
     *rt_kprintf("msp@ %p, psp@ %p\n", _msp, _psp);
     */

#ifdef RT_USING_FINSH
    /* initialize finsh */
    finsh_system_init();
#if !defined(RT_USING_POSIX_STDIO) && defined(RT_USING_DEVICE)
    finsh_set_device(RT_CONSOLE_DEVICE_NAME);
#endif
#endif

    _boot_M0();
}

/*the led thread*/
rt_align(RT_ALIGN_SIZE)
static rt_uint8_t led_stack[ 512 ];
static struct rt_thread led_thread;
static void led_thread_entry(void *parameter)
{
    rt_uint8_t led_value;
    rt_device_t led_dev;

    rt_led_hw_init();

    led_dev = rt_device_find("led");
    if (led_dev == RT_NULL)
    {
        rt_kprintf("can not find the led device\n");
        return;
    }

    led_value = 0;
    while (1)
    {
        led_dev->write(led_dev, 0, &led_value, sizeof(led_value));
        led_value = !led_value;
        rt_thread_delay(1000);
    }
}

int rt_application_init(void)
{
    rt_thread_t tid;
    rt_err_t result;
    tid = rt_thread_create("init",
                           rt_init_thread_entry, RT_NULL,
                           2048, 3, 20);
    if (tid != RT_NULL)
        rt_thread_startup(tid);

    /* init led thread */
    result = rt_thread_init(&led_thread, "led",
                            led_thread_entry, RT_NULL,
                            (rt_uint8_t *)&led_stack[0], sizeof(led_stack),
                            20, 5);
    if (result == RT_EOK)
    {
        rt_thread_startup(&led_thread);
    }
    return 0;
}