#define CXPI_QUEUE_TASK_STK_SIZE 256
#define MSG_QUEUE_MAXSIZE 50
#define BUFF_HEAD 0
#define BUFF_LEN 15
static uint8 TxQueue_a[MSG_QUEUE_MAXSIZE][BUFF_LEN];
static uint8 TxQueueFront;
static uint8 TxQueueRear;
static uint8 RxQueue_a[MSG_QUEUE_MAXSIZE][BUFF_LEN];
static uint8 RxQueueFront;
static uint8 RxQueueRear;
static uint32 SEM_ID_TXQUE = 0;
static uint32 SEM_ID_RXQUE = 0;
int8 IsRxQueueEmpty()
{
int8 ret;
if(RxQueueFront == RxQueueRear)
{
ret = true;
}
else
{
ret = false;
}
return ret;
}
int8 IsTxQueueEmpty()
{
int8 ret;
if(TxQueueFront == TxQueueRear)
{
ret = true;
}
else
{
ret = false;
}
return ret;
}
int8 IsRxQueueFull()
{
int8 ret;
uint8 RxQueueNext = 0;
RxQueueNext = (RxQueueRear + 1) % MSG_QUEUE_MAXSIZE;
if(RxQueueNext == RxQueueFront)
{
ret = true;
}
else
{
ret = false;
}
return ret;
}
int8 IsTxQueueFull()
{
int8 ret;
uint8 TxQueueNext = 0;
TxQueueNext = (TxQueueRear + 1) % MSG_QUEUE_MAXSIZE;
if(TxQueueNext == TxQueueFront)
{
ret = true;
}
else
{
ret = false;
}
return ret;
}
int8 RxEnQueue(uint8* pdata)
{
int8 ret;
ret = OSAL_SemaObtain(SEM_ID_RXQUE, 100, OSAL_API_OPT_BLOCKING);
if (ret == OSAL_ERR_NONE) {
ret = IsRxQueueFull();
if (ret) {
DEBUG("Rx queue is full, enqueue faild\n");
}
else {
RxQueueRear = (RxQueueRear + 1) % MSG_QUEUE_MAXSIZE;
OSAL_MemCopy(&RxQueue_a[RxQueueRear][0], pdata, BUFF_LEN);
}
ret = OSAL_SemaRelease(SEM_ID_RXQUE);
if (ret != OSAL_ERR_NONE) {
ERROR("RX Enqueue sema release faild\n");
}
}
else {
ERROR("RX Enqueue sema obtain faild\n");
}
return ret;
}
int8 TxEnQueue(uint8* pdata)
{
int8 ret;
ret = OSAL_SemaObtain(SEM_ID_TXQUE, 100, OSAL_API_OPT_BLOCKING);
if (ret == OSAL_ERR_NONE) {
ret = IsTxQueueFull();
if (ret) {
DEBUG("Tx queue is full, enqueue faild\n");
}
else {
TxQueueRear = (TxQueueRear + 1) % MSG_QUEUE_MAXSIZE;
OSAL_MemCopy(&TxQueue_a[TxQueueRear][0], pdata, BUFF_LEN);
}
ret = OSAL_SemaRelease(SEM_ID_TXQUE);
if (ret != OSAL_ERR_NONE) {
ERROR("TX Enqueue sema release faild\n");
}
}
else {
ERROR("TX Enqueue sema obtain faild\n");
}
return ret;
}
int8 RxDeQueue(uint8* pdata)
{
int8 ret = OSAL_ERR_NONE;
ret = OSAL_SemaObtain(SEM_ID_RXQUE, 100, OSAL_API_OPT_BLOCKING);
if (ret == OSAL_ERR_NONE) {
ret = IsRxQueueEmpty();
if (ret) {
DEBUG("Rx Queue is empty, dequeue faild\n");
OSAL_SemaRelease(SEM_ID_RXQUE);
return ret;
}
else {
RxQueueFront = (RxQueueFront + 1) % MSG_QUEUE_MAXSIZE;
OSAL_MemCopy(pdata, &RxQueue_a[RxQueueFront][0], BUFF_LEN);
OSAL_MemSet(&RxQueue_a[RxQueueFront][0], 0x00, BUFF_LEN);
}
ret = OSAL_SemaRelease(SEM_ID_RXQUE);
if (ret != OSAL_ERR_NONE) {
ERROR("Rx Dequeue sema release faild\n");
}
}
else {
ERROR("Rx Dequeue sema obtain faild\n");
}
return ret;
}
int8 TxDeQueue(uint8* pdata)
{
int8 ret;
ret = OSAL_SemaObtain(SEM_ID_TXQUE, 100, OSAL_API_OPT_BLOCKING);
if (ret == OSAL_ERR_NONE) {
ret = IsTxQueueEmpty();
if (ret) {
DEBUG("Tx Queue is empty, dequeue faild\n");
OSAL_SemaRelease(SEM_ID_TXQUE);
return ret;
}
else {
TxQueueFront = (TxQueueFront + 1) % MSG_QUEUE_MAXSIZE;
OSAL_MemCopy(pdata, &TxQueue_a[TxQueueFront][0], BUFF_LEN);
OSAL_MemSet(&TxQueue_a[TxQueueFront][0], 0x00, BUFF_LEN);
}
ret = OSAL_SemaRelease(SEM_ID_TXQUE);
if (ret != OSAL_ERR_NONE) {
ERROR("Tx Dequeue sema release faild\n");
}
}
else {
ERROR("Tx Dequeue sema obtain faild\n");
}
return ret;
}
void InitRxQueue(void)
{
RxQueueFront = -1;
RxQueueRear = -1;
OSAL_MemSet(&RxQueue_a[BUFF_HEAD][BUFF_HEAD], 0x00, BUFF_LEN * MSG_QUEUE_MAXSIZE);
OSAL_SemaCreate(&SEM_ID_RXQUE, (const uint8 *)"rx queue sema", 1, 0);
}
void InitTxQueue(void)
{
TxQueueFront = -1;
TxQueueRear = -1;
OSAL_MemSet(&TxQueue_a[BUFF_HEAD][BUFF_HEAD], 0x00, BUFF_LEN * MSG_QUEUE_MAXSIZE);
OSAL_SemaCreate(&SEM_ID_TXQUE, (const uint8 *)"tx queue sema", 1, 0);
}
06-05
613
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
07-29