forked from sonydevworld/spresense
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add TEST-Code for Multicore in aps_cxx_audio_detect_main.cxx
- Loading branch information
1 parent
0621b84
commit b27ac54
Showing
5 changed files
with
484 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
160 changes: 160 additions & 0 deletions
160
_aps_samples/aps00_sandbox/aps_cxx_audio_detect/worker_ctrl.cxx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
#include "worker_ctrl.h" | ||
|
||
/** WorkerCtrl (constructor) | ||
* - arg = program on sub-core | ||
* - geneate task, and assign at a CPU. | ||
*/ | ||
WorkerCtrl::WorkerCtrl(const char *setFileName) | ||
{ | ||
int ret; | ||
|
||
this->retcode = -1; | ||
this->status = TASK_EMPTY; | ||
this->filename = setFileName; | ||
ret = mptask_init(&this->mptask, this->filename); | ||
if (ret != 0) { | ||
return; | ||
} | ||
ret = mptask_assign(&this->mptask); | ||
if (ret != 0) { | ||
return; | ||
} | ||
this->status = TASK_READY; | ||
return; | ||
} | ||
|
||
/** WorkerCtrl (destructor) | ||
* - delete shm, mutex, mq | ||
*/ | ||
WorkerCtrl::~WorkerCtrl(void) | ||
{ | ||
/* Finalize all of MP objects */ | ||
mpshm_detach(&this->shm); | ||
mpshm_destroy(&this->shm); | ||
mpmutex_destroy(&this->mutex); | ||
mpmq_destroy(&this->mq); | ||
} | ||
|
||
/** getReady | ||
* - check already init all task-resources | ||
*/ | ||
bool WorkerCtrl::getReady(void) | ||
{ | ||
return (this->status == TASK_READY)? true : false; | ||
} | ||
|
||
/** initMutex | ||
* - init mutex in TaskResource as ID | ||
*/ | ||
int WorkerCtrl::initMutex(int id) | ||
{ | ||
int ret; | ||
ret = mpmutex_init(&this->mutex, id); | ||
if (ret < 0) { | ||
return ret; | ||
} | ||
ret = mptask_bindobj(&this->mptask, &this->mutex); | ||
if (ret < 0) { | ||
return ret; | ||
} | ||
return 0; | ||
} | ||
|
||
/** initMq | ||
* - init Message-Queue in TaskResource as ID | ||
*/ | ||
int WorkerCtrl::initMq(int id) | ||
{ | ||
int ret; | ||
ret = mpmq_init(&this->mq, id, mptask_getcpuid(&this->mptask)); | ||
if (ret < 0) { | ||
return ret; | ||
} | ||
ret = mptask_bindobj(&this->mptask, &this->mq); | ||
if (ret < 0) { | ||
return ret; | ||
} | ||
return 0; | ||
} | ||
|
||
/** initShm | ||
* - init Shared-memory in TaskResource as ID | ||
*/ | ||
void *WorkerCtrl::initShm(int id, ssize_t size) | ||
{ | ||
int ret; | ||
ret = mpshm_init(&this->shm, id, size); | ||
if (ret < 0) { | ||
return NULL; | ||
} | ||
ret = mptask_bindobj(&this->mptask, &this->shm); | ||
if (ret < 0) { | ||
return NULL; | ||
} | ||
/* Map shared memory to virtual space */ | ||
this->buf = mpshm_attach(&this->shm, size); | ||
if (!this->buf) { | ||
return NULL; | ||
} | ||
return buf; | ||
} | ||
|
||
/** execTask | ||
* - start Task on sub-core | ||
*/ | ||
int WorkerCtrl::execTask(void) | ||
{ | ||
return mptask_exec(&this->mptask); | ||
} | ||
|
||
/** destroyTask | ||
* - force Exit and Destroy Task on sub-core | ||
*/ | ||
int WorkerCtrl::destroyTask(void) | ||
{ | ||
mptask_destroy(&this->mptask, false, &this->retcode); | ||
return this->retcode; | ||
} | ||
|
||
/** lock | ||
* - Entering Critical-Section with mutex | ||
*/ | ||
void WorkerCtrl::lock(void) | ||
{ | ||
mpmutex_lock(&this->mutex); | ||
} | ||
|
||
/** unlock | ||
* - Exit Critical-Section with mutex | ||
*/ | ||
void WorkerCtrl::unlock(void) | ||
{ | ||
mpmutex_unlock(&this->mutex); | ||
} | ||
|
||
/** send | ||
* - send message with (id,value) via MQ | ||
*/ | ||
int WorkerCtrl::send(int8_t msgid, uint32_t value) | ||
{ | ||
return mpmq_send(&this->mq, msgid, value); | ||
} | ||
|
||
/** receive | ||
* - receive message (id,value) via MQ | ||
* - it can be used for nonblocking receive. | ||
*/ | ||
int WorkerCtrl::receive(uint32_t *pvalue, bool nonblocking) | ||
{ | ||
int ret; | ||
uint32_t gabage; | ||
if (pvalue == NULL) { | ||
pvalue = &gabage; | ||
} | ||
if (nonblocking) { | ||
ret = mpmq_timedreceive(&this->mq, pvalue, MPMQ_NONBLOCK); | ||
} else { | ||
ret = mpmq_receive(&this->mq, pvalue); | ||
} | ||
return ret; | ||
} |
47 changes: 47 additions & 0 deletions
47
_aps_samples/aps00_sandbox/aps_cxx_audio_detect/worker_ctrl.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#ifndef __WORKER_CTRL_H__ | ||
#define __WORKER_CTRL_H__ | ||
|
||
#include <sdk/config.h> | ||
#include <stdio.h> | ||
#include <string.h> | ||
|
||
#include <asmp/asmp.h> | ||
#include <asmp/mptask.h> | ||
#include <asmp/mpshm.h> | ||
#include <asmp/mpmq.h> | ||
#include <asmp/mpmutex.h> | ||
|
||
class WorkerCtrl { | ||
public: | ||
typedef enum e_task_status { | ||
TASK_EMPTY, | ||
TASK_READY, | ||
} TASK_STAT; | ||
private: | ||
const char *filename; | ||
TASK_STAT status; | ||
mptask_t mptask; | ||
mpmq_t mq; | ||
mpmutex_t mutex; | ||
mpshm_t shm; | ||
void *buf; | ||
int retcode; | ||
|
||
private: | ||
WorkerCtrl(); | ||
public: | ||
WorkerCtrl(const char *filename); | ||
~WorkerCtrl(void); | ||
bool getReady(void); | ||
int initMutex(int id); | ||
int initMq(int id); | ||
void *initShm(int id, ssize_t size); | ||
int execTask(void); | ||
int destroyTask(void); | ||
void lock(void); | ||
void unlock(void); | ||
int send(int8_t msgid, uint32_t value); | ||
int receive(uint32_t *pvalue, bool nonblocking = false); | ||
}; | ||
|
||
#endif /* __WORKER_CTRL_H__ */ |
Oops, something went wrong.