Esempio n. 1
0
/*============================
 *  post a mutex to thread
 *     信号量的释放
 *===========================*/
acoral_u32 acoral_mutex_post(acoral_evt_t *evt)
{
    acoral_sr             cpu_sr;
    acoral_u8             ownerPrio;
    acoral_u8             highPrio;
    acoral_u32            index;
    acoral_thread_t      *thread;
    acoral_list_t        *head;
    acoral_thread_t      *cur;

    HAL_ENTER_CRITICAL();
    acoral_spin_lock(&evt->spin_lock);

    if ( NULL == evt )
    {
        acoral_printerr("mutex NULL\n");
        acoral_spin_unlock(&evt->spin_lock);
        HAL_EXIT_CRITICAL();
        return MUTEX_ERR_NULL;   /*error*/
    }

    highPrio  = (acoral_u8)(evt->count >> 8);
    ownerPrio = (acoral_u8)(evt->count & MUTEX_L_MASK);
    cur=acoral_cur_thread;
    if (highPrio!=0&&cur->prio != highPrio && cur->prio != ownerPrio )
    {
        acoral_printerr("mutex prio err\n");
        acoral_spin_unlock(&evt->spin_lock);
        HAL_EXIT_CRITICAL();
        return MUTEX_ERR_UNDEF;
    }
    cur->evt=NULL;
    if (cur->prio != ownerPrio)
    {
        /* 提升过优先级,进行优先级复原*/
        acoral_change_prio_self(ownerPrio);
    }

    thread =acoral_evt_high_thread(evt);
    if (thread==NULL) {
        evt->count |= MUTEX_AVAI;
        evt->data = NULL;
        acoral_spin_unlock(&evt->spin_lock);
        HAL_EXIT_CRITICAL();
        return MUTEX_SUCCED;
    }
    timeout_queue_del(thread);
    acoral_evt_queue_del(thread);
#ifdef CFG_TEST
    acoral_print("%d post %d\n",cur->prio,thread->prio);
#endif
    evt->count &= MUTEX_U_MASK;
    evt->count |= thread->prio;
    evt->data = thread;
    acoral_rdy_thread(thread);
    acoral_spin_unlock(&evt->spin_lock);
    HAL_EXIT_CRITICAL();
    acoral_sched();
    return MUTEX_SUCCED;
}
Esempio n. 2
0
/*======================================
 *   try receive  a mail to mailbox
 *       接收邮件从邮箱
 *======================================*/
void* acoral_mbox_tryrecv(acoral_evt_t * event)
{
	void            *msg;
	acoral_sr         cpu_sr;
	acoral_thread_t  *cur;
	
	if (acoral_intr_nesting > 0)
		return NULL;

	if(event->type!=ACORAL_EVENT_MBOX)
		return NULL;
	
	HAL_ENTER_CRITICAL();
	acoral_spin_lock(&event->spin_lock);
	if( event->data == NULL)
	{
		acoral_spin_unlock(&event->spin_lock);
		HAL_EXIT_CRITICAL();
		return NULL;
	}
	
	msg         = event->data;
	event->data = NULL;
	acoral_spin_unlock(&event->spin_lock);
	HAL_EXIT_CRITICAL();

	return msg;
}
Esempio n. 3
0
/*===================================
 * 
 *   消息发送
 *===================================*/
acoral_u32 acoral_msg_send(acoral_msgctr_t* msgctr, acoral_msg_t* msg)
{
	acoral_sr     cpu_sr;
	
/*	if (acoral_intr_nesting > 0)
		return MST_ERR_INTR;
*/
	HAL_ENTER_CRITICAL();
	acoral_spin_lock(&msgctr->spin_lock);

	if (NULL == msgctr)
	{
		acoral_spin_unlock(&msgctr->spin_lock);
		HAL_EXIT_CRITICAL();
		return MST_ERR_NULL;
	}

	if (NULL == msg)
	{
		acoral_spin_unlock(&msgctr->spin_lock);
		HAL_EXIT_CRITICAL();
		return MSG_ERR_NULL;
	}

	/*----------------*/
	/*   消息数限制*/
	/*----------------*/
	if (ACORAL_MESSAGE_MAX_COUNT <= msgctr->count)
	{
		acoral_spin_unlock(&msgctr->spin_lock);
		HAL_EXIT_CRITICAL();
		return MSG_ERR_COUNT;
	}

	/*----------------*/
	/*   增加消息*/
	/*----------------*/
	msgctr->count++;
	msg->ttl += acoral_get_ticks();
	acoral_list_add2_tail(&msg->msglist, &msgctr->msglist);

	/*----------------*/
	/*   唤醒等待*/
	/*----------------*/
	if (msgctr->wait_thread_num > 0)
	{
		/* 此处将最高优先级唤醒*/
		wake_up_thread(&msgctr->waiting);
		msgctr->wait_thread_num--;
	}
	acoral_spin_unlock(&msgctr->spin_lock);
	HAL_EXIT_CRITICAL();
	acoral_sched();
	return MSGCTR_SUCCED;
}
Esempio n. 4
0
/*======================================
 *    receive  a mail to mailbox
 *       接收邮件从邮箱
 *======================================*/
void* acoral_mbox_recv(acoral_evt_t * event, acoral_time timeout)
{
	void            *msg;
	acoral_sr         cpu_sr;
	acoral_thread_t  *cur;
	
	if (acoral_intr_nesting > 0)
		return NULL;

	if(event->type!=ACORAL_EVENT_MBOX)
		return NULL;
	
	HAL_ENTER_CRITICAL();
	acoral_spin_lock(&event->spin_lock);
	if( event->data == NULL)
	{
		cur = acoral_cur_thread;
		if (timeout > 0)
		{
			cur->delay = TIME_TO_TICKS(timeout);
			timeout_queue_add(cur);
		}
		acoral_unrdy_thread(cur);
		acoral_evt_queue_add(event, cur);
		acoral_spin_unlock(&event->spin_lock);
		HAL_EXIT_CRITICAL();
		acoral_sched();
		HAL_ENTER_CRITICAL();
		acoral_spin_lock(&event->spin_lock);

		if (timeout > 0 && cur->delay <= 0)
		{
			acoral_evt_queue_del(cur);
			acoral_spin_unlock(&event->spin_lock);
			HAL_EXIT_CRITICAL();
			return NULL;
		}

		msg        = event->data;
		event->data = NULL;
		acoral_spin_unlock(&event->spin_lock);
		HAL_EXIT_CRITICAL();
		return msg;
	}
	
	msg         = event->data;
	event->data = NULL;
	acoral_spin_unlock(&event->spin_lock);
	HAL_EXIT_CRITICAL();

	return msg;
}
Esempio n. 5
0
/*send data about considering ACK and late send completely*/
void m_tran_send(sbuf_t *const sbuf, tran_tx_finish_cb_t tx_finish, uint8_t resend_times)
{
    DBG_ASSERT(sbuf != NULL __DBG_LINE);
    DBG_ASSERT(sbuf->primargs.pbuf != NULL __DBG_LINE);
    DBG_ASSERT(tx_finish != NULL __DBG_LINE);

    pbuf_t *pbuf = NULL;

    /* 如果当前正在等待ACK或重发则不接受新数据 */
    if (tran_state.can_send == FALSE)
    {
        asm("NOP");
        tx_finish(sbuf, FALSE);
        return;
    }

    m_tran_stop();

    if (tx_finish != NULL)
    {
        tran_state.cb.tx_finish = tx_finish;
    }
    else
    {
        return;
    }

    if (sbuf != NULL)
    {
        pbuf = sbuf->primargs.pbuf;
        if (pbuf != NULL)
        {
            tran_state.sbuf = sbuf;
            tran_state.ack_received   = FALSE;
            tran_state.need_resend_times  = resend_times;
            already_send_times = 0;

#if M_TRAN_DGB_EN > 0
            osel_memset((uint8_t *)&m_tran_tracker, 0, sizeof(m_tran_tracker));
            m_tran_tracker.data_send_start++;
#endif
            tran_send_intra();
        }
        else
        {
            hal_int_state_t s;
            HAL_ENTER_CRITICAL(s);

            tran_state.ack_needed   = FALSE;
            tran_state.can_send     = TRUE;
            sbuf_t *sbuf_tmp        = tran_state.sbuf;
            tran_state.sbuf         = NULL;
            HAL_EXIT_CRITICAL(s);
            phy_set_state(PHY_SLEEP_STATE);

            DBG_ASSERT(tran_state.cb.tx_finish != NULL __DBG_LINE);
            tran_state.cb.tx_finish(sbuf_tmp, FAILE);
        }
    }
}
Esempio n. 6
0
/*recevice data */
void m_tran_recv(void)
{
    /* 如果当前正在发送帧 */
    if (phy_get_state() == PHY_TX_STATE)
    {
        phy_set_state(PHY_IDLE_STATE);
        if (tran_state.sbuf != NULL)
        {
            hal_int_state_t s;
            HAL_ENTER_CRITICAL(s);
            if (ack_timer != NULL)
            {
                hal_timer_cancel(&ack_timer);
            }

            tran_state.can_send = TRUE;
            tran_state.ack_needed = FALSE;
            sbuf_t *sbuf_tmp = tran_state.sbuf;
            tran_state.sbuf = NULL;
            HAL_EXIT_CRITICAL(s);

            DBG_ASSERT(tran_state.cb.tx_finish != NULL __DBG_LINE);
            tran_state.cb.tx_finish(sbuf_tmp, FAILE);

            sbuf_tmp = NULL;
        }
    }
    
    tran_rx_sfd_set(FALSE);
    phy_set_state(PHY_RX_STATE);

#if M_TRAN_DGB_EN > 0
    osel_memset((void *)&m_tran_recv_tracker, 0, sizeof(m_tran_recv_tracker));
#endif
}
Esempio n. 7
0
void tran_rx_sfd_set(bool_t res)
{
    hal_int_state_t s;
    HAL_ENTER_CRITICAL(s);
    rx_sfd_happed = res;
    HAL_EXIT_CRITICAL(s);
}
Esempio n. 8
0
void m_tran_sleep(void)
{
    /* 如果当前正在发送帧 */
    if (phy_get_state() == PHY_TX_STATE)
    {
        phy_set_state(PHY_IDLE_STATE);
        if (tran_state.sbuf != NULL)
        {
            hal_int_state_t s;
            HAL_ENTER_CRITICAL(s);
            if (ack_timer != NULL)
            {
                hal_timer_cancel(&ack_timer);
            }

            tran_state.can_send = TRUE;
            tran_state.ack_needed = FALSE;
            sbuf_t *sbuf_tmp = tran_state.sbuf;
            tran_state.sbuf = NULL;
            HAL_EXIT_CRITICAL(s);

            DBG_ASSERT(tran_state.cb.tx_finish != NULL __DBG_LINE);
            tran_state.cb.tx_finish(sbuf_tmp, FAILE);

            sbuf_tmp = NULL;
        }
    }
    
    phy_set_state(PHY_SLEEP_STATE);
}
Esempio n. 9
0
/*function uses to deal tx event when the interrupt of tx ok */
static void tran_deal_tx_error_event(void)
{
    simu_int_cnt++;
    if (tran_state.can_send == FALSE)  /* txok event haven't been dealed */
    {
        hal_int_state_t s;
        HAL_ENTER_CRITICAL(s);

        sbuf_t *sbuf_tmp = tran_state.sbuf;
        tran_state.sbuf = NULL;
        tran_state.can_send = TRUE;

        HAL_EXIT_CRITICAL(s);

        extern void rf_enter_sleep_force(void);
        rf_enter_sleep_force();

        DBG_ASSERT(tran_state.cb.tx_finish != NULL __DBG_LINE);
        tran_state.cb.tx_finish(sbuf_tmp, FAILE);
    }
    else
    {
        DBG_ASSERT(FALSE __DBG_LINE);
    }
}
Esempio n. 10
0
static void tran_deal_tx_und_event(void)
{
#if M_TRAN_DGB_EN > 0
    m_tran_tracker.tx_und_msg_deal++;
#endif

    if (tran_state.can_send == FALSE)  /* txok event haven't been dealed */
    {
        hal_int_state_t s;
        HAL_ENTER_CRITICAL(s);
        tran_state.tx_und_happend = TRUE;

        sbuf_t *sbuf_tmp = tran_state.sbuf;
        tran_state.sbuf = NULL;
        tran_state.can_send = TRUE;

        HAL_EXIT_CRITICAL(s);

        phy_set_state(PHY_SLEEP_STATE);

        DBG_ASSERT(tran_state.cb.tx_finish != NULL __DBG_LINE);
        tran_state.cb.tx_finish(sbuf_tmp, FAILE);
    }
    else
    {
#if TXOK_INT_SIMU_EN > 0
        ; //模拟TXOK中断已经处理过下溢
#else
        DBG_ASSERT(FALSE __DBG_LINE);
#endif
    }
}
Esempio n. 11
0
acoral_u32 hal_susp_enter(){
	acoral_u32 regs[16];
	acoral_sr cpu_sr;
	HAL_ENTER_CRITICAL();
	regs_save_ptr=regs;
	rGSTATUS3=(acoral_u32)HAL_REAL_RESUME;
	hal_reg_array_save(core_save, ARRAY_SIZE(core_save));
	hal_reg_array_save(gpio_save, ARRAY_SIZE(gpio_save));
	hal_reg_array_save(irq_save, ARRAY_SIZE(irq_save));
	hal_reg_array_save(uart_save, ARRAY_SIZE(uart_save));
	rGPGCON=0x22;//key1,key2允许唤醒
	rINTMSK=0xffffffffL;
	rEINTMSK=0xffffffffL;
	rEINTPND=rEINTPND;
	acoral_prints("Enter Sleep\n");
	rCLKCON=0x0;
	flush_cache();//将cache内容写回mem,防止掉电的时候及恢复的时候丢失
	HAL_REAL_SLEEP(regs);
	int i=rEINTPND;
	hal_reg_array_restore(uart_save, ARRAY_SIZE(uart_save));
	hal_reg_array_restore(core_save, ARRAY_SIZE(core_save));
	hal_reg_array_restore(irq_save, ARRAY_SIZE(irq_save));
	hal_reg_array_restore(gpio_save, ARRAY_SIZE(gpio_save));
	acoral_prints("Wake up from Sleep\n");
	acoral_print("0x%x\n",rINTPND);
	acoral_print("0x%x\n",i);
	acoral_print("0x%x\n",rINTOFFSET);
	HAL_EXIT_CRITICAL();
}
Esempio n. 12
0
/*function uses to deal tx event when the interrupt of tx ok */
static void tran_deal_txok_event(void)
{
#if M_TRAN_DGB_EN > 0
    m_tran_tracker.txok_msg_deal++;
#endif

    /* 当无法在驱动中断中给出下溢处理判断时,在txok消息处理中判断*/
#if RF_INT_DEAL_FLOW == 0u
    //@todo  add code
//    if (hal_rf_txfifo_underflow())
//    {
//        tx_und_cb(0);
//        return;
//    }
#endif

    if (!tran_state.tx_und_happend)
    {
        if (tran_state.ack_needed == FALSE)
        {
            if (tran_state.can_send == FALSE)  /* txok event haven't been dealed */
            {
                hal_int_state_t s;
                HAL_ENTER_CRITICAL(s);
                sbuf_t *sbuf_tmp = tran_state.sbuf;
                tran_state.sbuf = NULL;
                tran_state.can_send = TRUE;
                HAL_EXIT_CRITICAL(s);

                phy_set_state(PHY_SLEEP_STATE);
                DBG_ASSERT(tran_state.cb.tx_finish != NULL __DBG_LINE);
                tran_state.cb.tx_finish(sbuf_tmp, TRUE);
#if M_TRAN_DGB_EN > 0
                m_tran_tracker.tx_success_without_ack++;
#endif
            }
            else
            {
                // ACK定时器超时-超时消息响应之前收到ACK,ACK等待处理,先进入重传流程
                // 重传数据发送成功后,发送txok消息,再处理之前的ACK消息,变量复位
                // 接着处理重传的txok消息,变量异步,会进入这里。已在RXOK事件中处理。
                DBG_ASSERT(FALSE __DBG_LINE);
            }
        }
        else
        {
            tran_wait_ack();
        }
    }
    else
    {
        tx_und_cnt++;
        return;//下溢中断已经发生并处理过,不操作
    }

#if M_TRAN_DGB_EN > 0
    m_tran_recv_tracker.txok_msg_deal_tick = hal_timer_now().w;
#endif
}
Esempio n. 13
0
static void tran_send_intra(void)
{
    pbuf_t *pbuf = tran_state.sbuf->primargs.pbuf;
    uint32_t tran_resend_times = 0;

    if (++already_send_times <= tran_state.need_resend_times)
    {
        tran_state.can_send = FALSE;                    // 标志进入发送数据阶段
        /* 重传退避,如果重传时间大于0,就退避,否则直接进入CSMA */
        tran_resend_times = TRAN_RESEND_TIMES * (already_send_times - 1) * (already_send_times - 1);
        if (tran_resend_times > 0)
        {
            if (resend_timer == NULL)
            {
                HAL_TIMER_SET_REL(MS_TO_TICK(tran_resend_times),
                                  resend_timeout_cb,
                                  pbuf,
                                  resend_timer);
                DBG_ASSERT(resend_timer != NULL __DBG_LINE);
            }
        }
        else
        {
            if (pbuf->attri.send_mode == CSMA_SEND_MODE)
            {
                tran_csma_send(pbuf);
            }
            else
            {
                tran_send(pbuf);
            }
        }
    }
    else
    {
        hal_int_state_t s;
        HAL_ENTER_CRITICAL(s);
        if (ack_timer != NULL)
        {
            hal_timer_cancel(&ack_timer);
        }

        tran_state.can_send = TRUE;
        tran_state.ack_needed = FALSE;
        sbuf_t *sbuf_tmp = tran_state.sbuf;
        tran_state.sbuf = NULL;
        HAL_EXIT_CRITICAL(s);
        phy_set_state(PHY_SLEEP_STATE);

#if M_TRAN_DGB_EN > 0
        m_tran_tracker.tx_finish_fail++;
#endif
        DBG_ASSERT(tran_state.cb.tx_finish != NULL __DBG_LINE);
        tran_state.cb.tx_finish(sbuf_tmp, FAILE);
    }
}
Esempio n. 14
0
void 
HeapFree(
	PPrivateHeap pHeap,
	void* pData
)
{
	acoral_sr cpu_sr;
	HAL_ENTER_CRITICAL();
	acoral_release_res((acoral_res_t *)pData);
	HAL_EXIT_CRITICAL();
}
Esempio n. 15
0
/*======================================
 *     send a mail to mailbox
 *       发送邮件至邮箱
 *======================================*/
acoral_u32 acoral_mbox_send(acoral_evt_t * event, void *msg)
{
	acoral_sr            cpu_sr;
	acoral_thread_t     *thread;

	/*if (acoral_intr_nesting > 0)
		return MBOX_ERR_INTR;
	*/
	if (NULL == event)
		return MBOX_ERR_NULL;

	if(event->type != ACORAL_EVENT_MBOX)
		return MBOX_ERR_TYPE;

	HAL_ENTER_CRITICAL();
	acoral_spin_lock(&event->spin_lock);
	if(event->data != NULL)
	{
		acoral_spin_unlock(&event->spin_lock);
		HAL_EXIT_CRITICAL();
		return MBOX_ERR_MES_EXIST;
	}
	event->data = msg;
	thread =acoral_evt_high_thread(event);
	/* 释放等待进程*/
	if (thread==NULL)
	{
		/* 没有等待队列*/
		acoral_spin_unlock(&event->spin_lock);
		HAL_EXIT_CRITICAL();
		return  MBOX_SUCCED;
	}
	/*释放等待任务*/
	timeout_queue_del(thread);
	acoral_evt_queue_del(thread);
	acoral_rdy_thread(thread);
	acoral_spin_unlock(&event->spin_lock);
	HAL_EXIT_CRITICAL();
	acoral_sched();
	return MBOX_SUCCED;
}
Esempio n. 16
0
/*=============================
 *  The deletion of the mutex
 *      信号号删除函数
 *============================= */
acoral_u32 acoral_mutex_del(acoral_evt_t *evt, acoral_u32 opt)
{
    acoral_sr            cpu_sr;
    acoral_queue_t      *queue;
    acoral_list_t       *head;
    acoral_prio_array_t *array;
    acoral_u32            index;

    /* 参数检测 */
    if (NULL == evt)
    {
        return MUTEX_ERR_NULL; /*error*/
    }

    if ( ACORAL_EVENT_MUTEX != evt->type )
    {
        return MUTEX_ERR_TYPE; /*error*/
    }

    /* 是否有任务等待*/
    HAL_ENTER_CRITICAL();
    acoral_spin_lock(&evt->spin_lock);
    if (acoral_evt_queue_empty(evt))
    {
        /*无等待任务删除*/
        acoral_spin_unlock(&evt->spin_lock);
        HAL_EXIT_CRITICAL();
        return MUTEX_SUCCED;
    }
    else
    {
        /*有等待任务*/
        acoral_spin_unlock(&evt->spin_lock);
        HAL_EXIT_CRITICAL();
        return MUTEX_ERR_TASK_EXIST;
    }
}
Esempio n. 17
0
/*=============================
 *  the appliation for mutex
 *      信号号申请操作
 *  优先级继承的优先级反转解决
 *=============================*/
acoral_u32 acoral_mutex_trypend(acoral_evt_t *evt)
{
    acoral_sr        cpu_sr;
    acoral_u8        highPrio;
    acoral_u8        ownerPrio;
    acoral_thread_t *thread;
    acoral_thread_t *cur;

    if(acoral_intr_nesting>0)
        return MUTEX_ERR_INTR;

    cur=acoral_cur_thread;

    HAL_ENTER_CRITICAL();
    acoral_spin_lock(&evt->spin_lock);
    if (NULL== evt)
    {
        acoral_spin_unlock(&evt->spin_lock);
        HAL_EXIT_CRITICAL();
        return MUTEX_ERR_NULL;
    }

    if ((acoral_u8)(evt->count & MUTEX_L_MASK) == MUTEX_AVAI)
    {
        /* 申请成功*/
        evt->count &= MUTEX_U_MASK;
        evt->count |= cur->prio;
        evt->data = (void*)cur;
        acoral_spin_unlock(&evt->spin_lock);
        HAL_EXIT_CRITICAL();
        return MUTEX_SUCCED;
    }

    acoral_spin_unlock(&evt->spin_lock);
    HAL_EXIT_CRITICAL();
    return MUTEX_ERR_TIMEOUT;
}
Esempio n. 18
0
//tran_send失败
static void tran_send_failed(void)
{
    hal_int_state_t s;
    HAL_ENTER_CRITICAL(s);
    tran_state.ack_needed = FALSE;
    tran_state.can_send = TRUE;
    sbuf_t *sbuf_tmp = tran_state.sbuf;
    tran_state.sbuf = NULL;
    HAL_EXIT_CRITICAL(s);

    phy_set_state(PHY_SLEEP_STATE);

    DBG_ASSERT(tran_state.cb.tx_finish != NULL __DBG_LINE);
    tran_state.cb.tx_finish(sbuf_tmp, FAILE);
}
Esempio n. 19
0
/*===================================
 *
 *  消息接收
 *===================================*/
void* acoral_msg_recv (acoral_msgctr_t* msgctr,
						acoral_u32  id,
						acoral_time  timeout,
						acoral_u32  *err)
{
	void             *dat;
	acoral_sr        cpu_sr;
	acoral_list_t    *p, *q;
	acoral_msg_t     *pmsg;
	acoral_thread_t  *cur;

	if (acoral_intr_nesting > 0)
	{
		*err = MST_ERR_INTR;
		return NULL;
	}
	if (NULL == msgctr)
	{
		*err = MST_ERR_NULL;
		return NULL;
	}

	cur = acoral_cur_thread;

	HAL_ENTER_CRITICAL();
	acoral_spin_lock(&msgctr->spin_lock);
	if(timeout>0){
		cur->delay = TIME_TO_TICKS(timeout);
		timeout_queue_add( cur);
	}
	while(1)
	{
		p = &msgctr->msglist;
		q = p->next;
		for( ;p != q; q = q->next)
		{
			pmsg = list_entry( q, acoral_msg_t, msglist);
			if ( (pmsg->id == id) && (pmsg->n > 0))
			{
				/*-----------------*/
				/* 有接收消息*/
				/*-----------------*/
				pmsg->n--;
				/*-----------------*/
				/* 延时列表删除*/
				/*-----------------*/
				timeout_queue_del(cur);
				dat = pmsg->data;
				acoral_list_del (q);
				acoral_release_res ((acoral_res_t *)pmsg);		
				msgctr->count--;
				acoral_spin_unlock(&msgctr->spin_lock);
				HAL_EXIT_CRITICAL();
				return dat;
			}
		}
	
		/*-----------------*/
		/*  没有接收消息*/
		/*-----------------*/
		msgctr->wait_thread_num++;
		acoral_msgctr_queue_add(msgctr, cur);
		acoral_unrdy_thread(cur);
		acoral_spin_unlock(&msgctr->spin_lock);
		HAL_EXIT_CRITICAL();
		acoral_sched();
		/*-----------------*/
		/*  看有没有超时*/
		/*-----------------*/
		HAL_ENTER_CRITICAL();
		acoral_spin_lock(&msgctr->spin_lock);
	
		if (timeout>0&&(acoral_32)cur->delay <=0 )
			break;

	}

	/*---------------*/
	/*  超时退出*/
	/*---------------*/
//	timeout_queue_del(cur);
	if(msgctr->wait_thread_num>0)
		msgctr->wait_thread_num--;
	acoral_list_del(&cur->waiting);
	acoral_spin_unlock(&msgctr->spin_lock);
	HAL_EXIT_CRITICAL();
	*err = MST_ERR_TIMEOUT;
	return NULL;

}
Esempio n. 20
0
/*function uses to deal event when RX interrupt come*/
static void tran_deal_rxok_event(void)
{
    sbuf_t *sbuf_tmp = NULL;
    bool_t is_valid_frm = FALSE;

    pbuf_t *pbuf = tran_state.cb.frm_get();

#if M_TRAN_DGB_EN > 0
    m_tran_recv_tracker.rxok_msg_deal_tick = hal_timer_now().w;
#endif

    if ((pbuf == NULL) || (pbuf->data_len == 0))
    {
        if (pbuf != NULL)
        {
            pbuf_free(&pbuf __PLINE2);
        }
        return;
    }

    is_valid_frm = tran_state.cb.frm_head_parse(pbuf);

    if (is_valid_frm == TRUE)
    {
        /* 等待ACK时收到其他有效帧,释放该帧,继续等待 */
        if ((tran_state.ack_needed == TRUE) && (pbuf->attri.is_ack == FALSE))
        {
            pbuf_free(&pbuf __PLINE2);
            return;
        }

#if M_TRAN_DGB_EN > 0
        m_tran_tracker.recv_data++;
#endif

#if M_SYNC_EN > 0
        m_sync_rxfilter(pbuf);
#endif

        if (pbuf->attri.need_ack == TRUE)
        {
            tran_state.cb.send_ack(pbuf->attri.seq);//发送结束为IDLE状态
        }
        else if (pbuf->attri.is_ack == TRUE)
        {
#if M_TRAN_DGB_EN > 0
            m_tran_tracker.recv_ack++;
#endif
            if (tran_state.ack_needed == TRUE)              //状态为正在等待ACK
            {
                if (tran_state.sbuf->primargs.pbuf->attri.seq == pbuf->attri.seq)
                {
                    hal_int_state_t s;
                    HAL_ENTER_CRITICAL(s);

                    if (ack_timer != NULL)
                    {
                        hal_timer_cancel(&ack_timer);
                    }
                    else // 在处理ack期间等待ACK定时器已然触发,超时消息已发出
                    {
                        if (tran_state.ack_timeout_happened)
                        {
                            //重传数据已经发出,TXOK消息已经发出,交给后面的TXOK消息处理
                            tran_state.ack_timeout_happened = FALSE;
                            ack_timeout_cnt++;
                            HAL_EXIT_CRITICAL(s);
                            return;
                        }
                        else
                        {
                            ; // 超时消息处理在排队,本流程继续处理
                        }
                    }

                    tran_state.ack_needed = FALSE;
                    tran_state.can_send = TRUE;
                    tran_state.ack_received = TRUE;
                    sbuf_tmp = tran_state.sbuf;
                    tran_state.sbuf = NULL;
                    HAL_EXIT_CRITICAL(s);

#if M_TRAN_DGB_EN > 0
                    m_tran_tracker.tx_success_ack++;
#endif
                    DBG_ASSERT(tran_state.cb.tx_finish != NULL __DBG_LINE);
                    tran_state.cb.tx_finish(sbuf_tmp, TRUE);
                }
                else
                {
                    ;   //收到错误的ACK
                }
            }
            else
            {
                ;//不处理不在等待ACK期间收到的ACK
            }
        }

        tran_state.cb.frm_payload_parse(pbuf);
    }
    else
    {// is valid == FALSE
        if(pbuf->used == TRUE)
        {
            pbuf_free(&pbuf __PLINE2);
        }
        phy_set_state(PHY_RX_STATE);
    }
}
Esempio n. 21
0
/*=============================
 *  the appliation for mutex
 *      信号号申请操作
 *  优先级继承的优先级反转解决
 *=============================*/
acoral_u32 acoral_mutex_pend(acoral_evt_t *evt, acoral_time timeout)
{
    acoral_sr        cpu_sr;
    acoral_u8        highPrio;
    acoral_u8        ownerPrio;
    acoral_thread_t *thread;
    acoral_thread_t *cur;

    if(acoral_intr_nesting>0)
        return MUTEX_ERR_INTR;

    cur=acoral_cur_thread;

    HAL_ENTER_CRITICAL();
    acoral_spin_lock(&evt->spin_lock);
    if (NULL== evt)
    {
        acoral_spin_unlock(&evt->spin_lock);
        HAL_EXIT_CRITICAL();
        return MUTEX_ERR_NULL;
    }

    if ((acoral_u8)(evt->count & MUTEX_L_MASK) == MUTEX_AVAI)
    {
        /* 申请成功*/
        evt->count &= MUTEX_U_MASK;
        evt->count |= cur->prio;
        evt->data = (void*)cur;
        acoral_spin_unlock(&evt->spin_lock);
        HAL_EXIT_CRITICAL();
        return MUTEX_SUCCED;
    }

    /* 互斥量已被占有*/
    /* 这里要看进程是否一个核上的*/
    /* 是一个核上的任务才有可能发生优先级反转*/
    highPrio = (acoral_u8)(evt->count >> 8);
    ownerPrio = (acoral_u8)(evt->count & MUTEX_L_MASK);
    thread = (acoral_thread_t*)evt->data;

    if (thread->prio != highPrio&&ownerPrio>cur->prio)
    {
        /*有可能优先级反转,提升拥有者优先级*/
        if(highPrio==0)
            highPrio=cur->prio;
        acoral_thread_change_prio(thread,highPrio);
    }
    /*不需要或不能提高优先级*/
    acoral_unrdy_thread(cur);
    acoral_evt_queue_add(evt,cur);
    if (timeout > 0)
    {
        /*加载到超时队列*/
        cur->delay = TIME_TO_TICKS(timeout);
        timeout_queue_add(cur);
    }
    acoral_spin_unlock(&evt->spin_lock);
    HAL_EXIT_CRITICAL();
    acoral_sched();
    HAL_ENTER_CRITICAL();
    acoral_spin_lock(&evt->spin_lock);
    if(evt->data!=cur&&timeout>0&&cur->delay<=0) {
        acoral_printk("Time Out Return\n");
        acoral_evt_queue_del(cur);
        acoral_spin_unlock(&evt->spin_lock);
        HAL_EXIT_CRITICAL();
        return MUTEX_ERR_TIMEOUT;
    }

    //---------------
    // modify by pegasus 0804: timeout_queue_del [+]
    timeout_queue_del(cur);

    if(evt->data!=cur) {
        acoral_printk("Err Ready Return\n");
        acoral_evt_queue_del(cur);
        acoral_spin_unlock(&evt->spin_lock);
        HAL_EXIT_CRITICAL();
        return MUTEX_ERR_RDY;
    }

    return MUTEX_SUCCED;
}
Esempio n. 22
0
static void tran_send(pbuf_t *pbuf)
{
    DBG_ASSERT(pbuf != NULL __DBG_LINE);

#if M_SLOT_EN > 0
    uint32_t send_need_time = LEN_TO_US(pbuf->data_len);
    uint32_t slot_remain_time = TICK_TO_US(m_slot_get_remain_time());
#else
    uint32_t send_need_time = 0;
    uint32_t slot_remain_time = 0xFFFFFFFF;
#endif
    if ((pbuf != NULL) && (slot_remain_time > send_need_time))
    {
        uint8_t stamp_size = 0;
#if M_SYNC_EN > 0
        stamp_size = m_sync_txfilter(pbuf);
#endif
        if (!phy_write_buf(pbuf, stamp_size))
        {
#if M_TRAN_DGB_EN > 0
            static uint32_t tx_write_fifo_fail_cnt = 0;
            tx_write_fifo_fail_cnt++;
#endif
            tran_send_failed();

            return;
        }

        tran_state.tx_und_happend = FALSE;

        if (!phy_send_data())
        {
            tran_send_failed();
            return;
        }

#if M_TRAN_DGB_EN > 0
        m_tran_tracker.data_send_real++;
#endif

#if TXOK_INT_SIMU_EN > 0
        HAL_TIMER_SET_REL(US_TO_TICK(LEN_TO_US(pbuf->data_len) + 10000),
                          tx_ok_error_timer,
                          NULL,
                          txok_timer);
        DBG_ASSERT(txok_timer != NULL __DBG_LINE);
#endif

        if (pbuf->attri.need_ack == TRUE)
        {
            tran_state.ack_needed = TRUE;
        }
    }
    else
    {
        hal_int_state_t s;
        HAL_ENTER_CRITICAL(s);
        if (ack_timer != NULL)
        {
            hal_timer_cancel(&ack_timer);
        }
        tran_send_failed();
        HAL_EXIT_CRITICAL(s);
    }
}