Developer World
Spresense Arduino Library v3.2.0-77d75a4
MP.h
Go to the documentation of this file.
1/*
2 * MP.h - Spresense Arduino Multi-Processer Communication library
3 * Copyright 2019,2021 Sony Semiconductor Solutions Corporation
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20#ifndef _MP_H_
21#define _MP_H_
22
37/****************************************************************************
38 * Included Files
39 ****************************************************************************/
40
41#include <Arduino.h>
42#include <sdk/config.h>
43#include <stdio.h>
44#include <string.h>
45#include <errno.h>
46
47#include <asmp/asmp.h>
48#include <asmp/mptask.h>
49#include <asmp/mpshm.h>
50#include <asmp/mpmq.h>
51#include <asmp/mpmutex.h>
52
53#include <multi_print.h>
54
55/****************************************************************************
56 * Pre-processor Definitions
57 ****************************************************************************/
58
59#ifdef BRD_DEBUG
60#define MPDBG(format, ...) printf("DEBUG: " format, ##__VA_ARGS__)
61#else
62#define MPDBG(format, ...)
63#endif
64#define MPERR(format, ...) printf("ERROR: " format, ##__VA_ARGS__)
65
66#define KEY_SHM 1
67#define KEY_MQ 2
68
69#define MP_RECV_BLOCKING (0)
70#define MP_RECV_POLLING (MPMQ_NONBLOCK)
71
72#define MP_GET_CPUID() (*(volatile int *)0x4e002040)
73
74#define MP_MAX_SUBID 6
75
76/* MP Log utility */
77#if (SUBCORE == 1)
78#define MPLOG_PREFIX "[Sub1] "
79#elif (SUBCORE == 2)
80#define MPLOG_PREFIX "[Sub2] "
81#elif (SUBCORE == 3)
82#define MPLOG_PREFIX "[Sub3] "
83#elif (SUBCORE == 4)
84#define MPLOG_PREFIX "[Sub4] "
85#elif (SUBCORE == 5)
86#define MPLOG_PREFIX "[Sub5] "
87#else
88#define MPLOG_PREFIX "[Main] "
89#endif
90
91#define MPLog(fmt, ...) do { \
92 irqstate_t flags; \
93 flags = printlock(); \
94 sync_printf(MPLOG_PREFIX fmt, ##__VA_ARGS__); \
95 printunlock(flags); \
96} while (0)
97
98/****************************************************************************
99 * class declaration
100 ****************************************************************************/
101
108{
109public:
110 MPClass();
111
124#ifdef SUBCORE
125 int begin();
126#else
127 int begin(int subid);
128#endif
129
139#ifdef SUBCORE
140 int end();
141#else
142 int end(int subid);
143#endif
144
156#ifdef SUBCORE
157 int Send(int8_t msgid, uint32_t msgdata, int subid = 0);
158#else
159 int Send(int8_t msgid, uint32_t msgdata, int subid);
160#endif
161
173#ifdef SUBCORE
174 int Recv(int8_t *msgid, uint32_t *msgdata, int subid = 0);
175#else
176 int Recv(int8_t *msgid, uint32_t *msgdata, int subid);
177#endif
178
190#ifdef SUBCORE
191 int Send(int8_t msgid, void *msgaddr, int subid = 0);
192#else
193 int Send(int8_t msgid, void *msgaddr, int subid);
194#endif
195
207#ifdef SUBCORE
208 int Recv(int8_t *msgid, void *msgaddr, int subid = 0);
209#else
210 int Recv(int8_t *msgid, void *msgaddr, int subid);
211#endif
212
223#ifdef SUBCORE
224 template <typename T> int SendObject(T &t, int subid = 0);
225#else
226 template <typename T> int SendObject(T &t, int subid);
227#endif
228
240#ifdef SUBCORE
241 template <typename T> int RecvObject(T &t, int subid = 0);
242#else
243 template <typename T> int RecvObject(T &t, int subid);
244#endif
245
256#ifdef SUBCORE
257 int SendWaitComplete(int subid = 0);
258#else
259 int SendWaitComplete(int subid);
260#endif
261
269 void RecvTimeout(uint32_t timeout);
270
275 uint32_t GetRecvTimeout();
276
282 uint32_t Virt2Phys(void *virt);
283
290 void GetMemoryInfo(int &usedMem, int &freeMem, int &largestFreeMem);
291
297
303
304#ifndef SUBCORE
311 void *AllocSharedMemory(size_t size);
312
317 void FreeSharedMemory(void *addr);
318#endif
319
320private:
321 uint32_t _recvTimeout;
322 mpmq_t _mq[MP_MAX_SUBID];
323 struct ResourceManagement {
324 uint32_t magic;
325 uint32_t cpu_assign;
326 uint32_t reserved[2];
327 uint32_t resource[4];
328 } *_rmng;
329
330 int checkid(int subid);
331#ifndef SUBCORE
332 mptask_t _mptask[MP_MAX_SUBID];
333 int load(int subid);
334 int unload(int subid);
335 sq_queue_t _shmlist;
336 struct shm_entry {
337 sq_entry_t entry;
338 mpshm_t shm;
339 uint32_t addr;
340 };
341#endif
342};
343
344/****************************************************************************
345 * template functions
346 ****************************************************************************/
347
348template <typename T> int MPClass::SendObject(T &t, int subid)
349{
350 int ret;
351
352 ret = checkid(subid);
353 if (ret) {
354 return ret;
355 }
356
357 size_t msgsz = sizeof(T);
358 if (msgsz > 127) {
359 return -EINVAL;
360 }
361
362 ret = Send((int8_t)msgsz, Virt2Phys(&t), subid);
363 if (ret < 0) {
364 MPDBG("Send(&object) failure. %d\n", ret);
365 return ret;
366 }
367
368 return ret;
369}
370
371template <typename T> int MPClass::RecvObject(T &t, int subid)
372{
373 int ret;
374 void *vp;
375 int8_t rsz;
376
377 ret = checkid(subid);
378 if (ret) {
379 return ret;
380 }
381
382 size_t msgsz = sizeof(T);
383 if (msgsz > 127) {
384 return -EINVAL;
385 }
386
387 ret = Recv(&rsz, (uint32_t*)&vp, subid);
388
389 if ((ret <= 0) || (msgsz != ret)) {
390 MPDBG("Recv(&object) failure. %d\n", ret);
391 mpmq_send(&_mq[subid], -1, 0); // error
392 return ret;
393 }
394
395 memcpy(&t, vp, msgsz);
396
397 ret = mpmq_send(&_mq[subid], 0, 0); // success
398 if (ret < 0) {
399 MPDBG("mpmq_send() failure. %d\n", ret);
400 return ret;
401 }
402
403 return ret;
404}
405
406/****************************************************************************
407 * extern declaration
408 ****************************************************************************/
409
410extern MPClass MP;
411
414#endif /* _MP_H_ */
This is the interface for MP (Multi-Processor).
Definition: MP.h:108
int Recv(int8_t *msgid, void *msgaddr, int subid)
Receive the address of any message from the other processor.
int Send(int8_t msgid, void *msgaddr, int subid)
Send the address of any message to the other processor.
void * AllocSharedMemory(size_t size)
Allocate memory from shared memory.
uint32_t Virt2Phys(void *virt)
Convert virtual address to physical address.
int SendWaitComplete(int subid)
Wait for the object to be sent.
int begin(int subid)
Start communication with the other processor.
void DisableConsole()
Disable console.
int Recv(int8_t *msgid, uint32_t *msgdata, int subid)
Receive any 32bit-data from the other processor.
int Send(int8_t msgid, uint32_t msgdata, int subid)
Send any 32bit-data to the other processor.
uint32_t GetRecvTimeout()
Get timeout of receiver.
void RecvTimeout(uint32_t timeout)
Set timeout of receiver.
void EnableConsole()
Enable console.
void FreeSharedMemory(void *addr)
Free memory from shared memory.
void GetMemoryInfo(int &usedMem, int &freeMem, int &largestFreeMem)
Get memory information.
int end(int subid)
End communication with the other processor.
int SendObject(T &t, int subid)
Send any object to the other processor.
Definition: MP.h:348
int RecvObject(T &t, int subid)
Receive any object from the other processor.
Definition: MP.h:371