summaryrefslogtreecommitdiff
path: root/bsp/nxp/imx/imxrt/imxrt1180-nxp-evk/cm7/applications/multicore_thread.c
blob: 5f253b0e9ff4d80a86566746098f0ec64f8e3515 (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
 * Copyright 2021-2025 NXP
 * All rights reserved.
 *
 * Change Logs:
 * Date           Author       Notes
 * 2026-06-02     Ran Chen     multicore communication thread
 */

#include <rtthread.h>
#include <rtdevice.h>

#ifdef BSP_USE_MULTICORE

#ifdef BSP_USE_MULTICORE_RPMSG_LITE
#include "rpmsg_lite.h"
#include "rpmsg_queue.h"
#include "rpmsg_ns.h"
#endif

#ifdef BSP_USE_MULTICORE_KICK_OFF
#include "mcmgr.h"
#include "board.h"
#endif

/*******************************************************************************
 * Definitions
 ******************************************************************************/
#define MULTICORE_THREAD_STACK_SIZE 2048
#define MULTICORE_THREAD_PRIORITY   10
#define MULTICORE_THREAD_TIMESLICE  5
#ifdef BSP_USE_MULTICORE_RPMSG_LITE
#ifndef RPMSG_LITE_LINK_ID
#define RPMSG_LITE_LINK_ID (RL_PLATFORM_IMXRT1180_M33_M7_LINK_ID)
#endif

#ifndef RPMSG_NS_ANNOUNCE_STRING
#define RPMSG_LITE_NS_ANNOUNCE_STRING "rpmsg-openamp-demo-channel"
#else
#define RPMSG_LITE_NS_ANNOUNCE_STRING RPMSG_NS_ANNOUNCE_STRING
#endif

#ifndef RPMSG_LOCAL_EPT_ADDR
#define LOCAL_EPT_ADDR (30U)
#else
#define LOCAL_EPT_ADDR RPMSG_LOCAL_EPT_ADDR
#endif

#define APP_RPMSG_READY_EVENT_DATA (1U)

typedef struct the_message
{
    uint32_t DATA;
} THE_MESSAGE, *THE_MESSAGE_PTR;

static volatile THE_MESSAGE msg = { 0 };
static struct rpmsg_lite_instance * volatile my_rpmsg = NULL;
static struct rpmsg_lite_endpoint * volatile my_ept = NULL;
static volatile rpmsg_queue_handle my_queue = NULL;
static bool error_occurred = false;
#endif /* BSP_USE_MULTICORE_RPMSG_LITE */

/*******************************************************************************
 * Private Functions
 ******************************************************************************/
#ifdef BSP_USE_MULTICORE_KICK_OFF
static rt_err_t multicore_kickoff_init(void)
{
    mcmgr_status_t status;

    rt_kprintf("[Multicore] Initializing MCMGR ...\r\n");
    status = MCMGR_Init();
    if (status != kStatus_MCMGR_Success)
    {
        rt_kprintf("[Multicore] MCMGR initialization failed!\r\n");
        return -RT_ERROR;
    }
    rt_kprintf("[Multicore] MCMGR initialized successfully\r\n");

    return RT_EOK;
}
#endif /* BSP_USE_MULTICORE_KICK_OFF */
#ifdef BSP_USE_MULTICORE_RPMSG_LITE
static void app_nameservice_isr_cb(uint32_t new_ept, const char *new_ept_name,
                                   uint32_t flags, void *user_data)
{
    /* Nameservice callback - can be extended as needed */
}

static rt_err_t rpmsg_lite_init(void)
{
    uint32_t startupData;
    mcmgr_status_t status;

    rt_kprintf("[RPMsg] Initializing RPMsg-Lite (CM7 as remote)...\r\n");

    /* Get the startup data from CM33 master core */
    do
    {
        status = MCMGR_GetStartupData(kMCMGR_Core0, &startupData);
    } while (status != kStatus_MCMGR_Success);

    /* Initialize RPMsg-Lite as remote (CM7 side) */
    my_rpmsg = rpmsg_lite_remote_init((void *)(char *)(platform_patova(startupData)),
                                      RPMSG_LITE_LINK_ID, RL_NO_FLAGS);
    if (my_rpmsg == NULL)
    {
        rt_kprintf("[RPMsg] Failed to initialize rpmsg!\r\n");
        error_occurred = true;
        return -RT_ERROR;
    }

    /* Signal the CM33 master core we are ready */
    (void)MCMGR_TriggerEvent(kMCMGR_Core0, kMCMGR_RemoteApplicationEvent,
                             APP_RPMSG_READY_EVENT_DATA);

    /* Wait for link up with CM33 master */
    rpmsg_lite_wait_for_link_up(my_rpmsg, RL_BLOCK);
    rt_kprintf("[RPMsg] Link is up with CM33 master!\r\n");

    /* Create queue */
    my_queue = rpmsg_queue_create(my_rpmsg);
    if (my_queue == NULL)
    {
        rt_kprintf("[RPMsg] Failed to create queue!\r\n");
        error_occurred = true;
        return -RT_ERROR;
    }

    /* Create endpoint */
    my_ept = rpmsg_lite_create_ept(my_rpmsg, LOCAL_EPT_ADDR, rpmsg_queue_rx_cb, my_queue);
    if (my_ept == NULL)
    {
        rt_kprintf("[RPMsg] Failed to create endpoint!\r\n");
        error_occurred = true;
        return -RT_ERROR;
    }

    /* Bind nameservice */
    rpmsg_ns_handle ns_handle = rpmsg_ns_bind(my_rpmsg, app_nameservice_isr_cb, NULL);
    if (ns_handle == NULL)
    {
        rt_kprintf("[RPMsg] Failed to bind name service!\r\n");
        error_occurred = true;
        return -RT_ERROR;
    }

    /* Announce service */
//    SDK_DelayAtLeastUs(1000000U, SDK_DEVICE_MAXIMUM_CPU_CLOCK_FREQUENCY);
    (void)rpmsg_ns_announce(my_rpmsg, my_ept, RPMSG_LITE_NS_ANNOUNCE_STRING,
                            (uint32_t)RL_NS_CREATE);
    rt_kprintf("[RPMsg] Nameservice announced\r\n");

    return RT_EOK;
}

static void rpmsg_lite_communication(void)
{
    volatile uint32_t remote_addr = 0U;

    rt_kprintf("[RPMsg] Starting ping-pong communication...\r\n");

    while (msg.DATA <= 100U)
    {
        rt_kprintf("[RPMsg] Waiting for ping...\r\n");

        (void)rpmsg_queue_recv(my_rpmsg, my_queue, (uint32_t *)&remote_addr,
                               (char *)&msg, sizeof(THE_MESSAGE), NULL, RL_BLOCK);

        msg.DATA++;

        rt_kprintf("[RPMsg] Received: %u, Sending pong: %u\r\n", msg.DATA - 1, msg.DATA);

        (void)rpmsg_lite_send(my_rpmsg, my_ept, remote_addr, (char *)&msg,
                              sizeof(THE_MESSAGE), RL_BLOCK);
    }

    rt_kprintf("[RPMsg] Ping-pong communication completed!\r\n");
}

static void rpmsg_lite_cleanup(void)
{
    rt_kprintf("[RPMsg] Cleaning up resources...\r\n");

    if (my_ept)
    {
        (void)rpmsg_lite_destroy_ept(my_rpmsg, my_ept);
        my_ept = NULL;
    }

    if (my_queue)
    {
        (void)rpmsg_queue_destroy(my_rpmsg, my_queue);
        my_queue = NULL;
    }

    if (my_rpmsg)
    {
        (void)rpmsg_lite_deinit(my_rpmsg);
        my_rpmsg = NULL;
    }

    msg.DATA = 0U;
}
#endif /* BSP_USE_MULTICORE_RPMSG_LITE */

/*******************************************************************************
 * Thread Entry
 ******************************************************************************/
static void multicore_thread_entry(void *parameter)
{
#ifndef BSP_USE_MULTICORE_RPMSG_LITE
    uint32_t startupData;
    mcmgr_status_t status;
#endif

    rt_kprintf("\r\n=== Multicore Communication Thread Started ===\r\n");

#ifdef BSP_USE_MULTICORE_KICK_OFF
    /* Initialize and kickoff CM33 core */
    if (multicore_kickoff_init() != RT_EOK)
    {
        rt_kprintf("[Multicore] Kickoff initialization failed!\r\n");
        return;
    }
#endif

#ifdef BSP_USE_MULTICORE_RPMSG_LITE
    /* Initialize RPMsg-Lite */
    if (rpmsg_lite_init() != RT_EOK)
    {
        rt_kprintf("[Multicore] RPMsg-Lite initialization failed!\r\n");
        goto cleanup;
    }

    /* Start communication */
    rpmsg_lite_communication();

cleanup:
    /* Cleanup resources */
    rpmsg_lite_cleanup();

    if (error_occurred)
    {
        rt_kprintf("[Multicore] Error occurred during communication!\r\n");
    }
    else
    {
        rt_kprintf("[Multicore] Communication completed successfully!\r\n");
    }
#else
         /* Get the startup data */
    do
    {
        status = MCMGR_GetStartupData(kMCMGR_Core0, &startupData);
    } while (status != kStatus_MCMGR_Success);
#endif

    rt_kprintf("=== Multicore Communication Thread Ended ===\r\n");
}

/*******************************************************************************
 * Public Functions
 ******************************************************************************/
int multicore_init(void)
{
    rt_thread_t tid;

    tid = rt_thread_create("multicore",
                           multicore_thread_entry,
                           RT_NULL,
                           MULTICORE_THREAD_STACK_SIZE,
                           MULTICORE_THREAD_PRIORITY,
                           MULTICORE_THREAD_TIMESLICE);

    if (tid != RT_NULL)
    {
        rt_thread_startup(tid);
//        rt_kprintf("[Multicore] Communication thread created successfully\r\n");
        return RT_EOK;
    }
    else
    {
        rt_kprintf("[Multicore] Failed to create communication thread!\r\n");
        return -RT_ERROR;
    }
}
INIT_APP_EXPORT(multicore_init);

#endif /* BSP_USE_MULTICORE */