/*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 }
static void gprs_send_event_handler(void) //APP新插入数据直接发送 { if (NULL != gprs_task_timer) { hal_timer_cancel(&gprs_task_timer); DBG_ASSERT(gprs_task_timer == NULL __DBG_LINE); } if(gprs_mode == GPRS_CONNECTED) //已经连接就直接发送 { osel_post(GPRS_SEND_CMD_EVENT, (uint8_t *)(GPRS_CMD_SEND), OSEL_EVENT_PRIO_LOW); } else if(gprs_mode == GPRS_CLOSE) { gprs_open_handler(); } else //其他状态查询连接状态 { osel_post(GPRS_SEND_CMD_EVENT, (uint8_t *)(GPRS_CMD_STATUS), OSEL_EVENT_PRIO_LOW); } }
static void gprs_task_timer_cb(void *p) { if (NULL != gprs_task_timer) { hal_timer_cancel(&gprs_task_timer); DBG_ASSERT(gprs_task_timer == NULL __DBG_LINE); } if(GPRS_QUEUE_HAVE_DATA) //发送缓冲不为空 { if(gprs_mode == GPRS_CONNECTED) //已经连接就直接发送 { osel_post(GPRS_SEND_CMD_EVENT, (uint8_t *)(GPRS_CMD_SEND), OSEL_EVENT_PRIO_LOW); } else if(gprs_mode == GPRS_CLOSE) { gprs_open_handler(); } else { osel_post(GPRS_SEND_CMD_EVENT, (uint8_t *)(GPRS_CMD_STATUS), OSEL_EVENT_PRIO_LOW); } } else //没有要发送的数据 { osel_post(GPRS_POLL_EVENT, NULL, OSEL_EVENT_PRIO_LOW); } }
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); }
/*call back for sending Successfully*/ static void tx_ok_cb(uint16_t int_time) { #if M_TRAN_DGB_EN > 0 m_tran_tracker.txok_cb++; if (tran_state.can_send) { DBG_ASSERT(FALSE __DBG_LINE); } #endif #if TXOK_INT_SIMU_EN > 0 //DBG_ASSERT(txok_timer != NULL __DBG_LINE); if (txok_timer != NULL) // 确保该timer未被free { hal_timer_cancel(&txok_timer); tran_cb_send_msg(M_TRAN_TXOK_EVENT, MSG_LOW_PRIO); } #else tran_cb_send_msg(M_TRAN_TXOK_EVENT, MSG_LOW_PRIO); #endif #if M_TRAN_DGB_EN > 0 m_tran_recv_tracker.txok_int_cb_tick = hal_timer_now().w; #endif }
/*call back for timeout of waiting tx ok*/ static void tx_ok_error_timer(void *arg) { txok_timer = NULL; if (ack_timer != NULL) { hal_timer_cancel(&ack_timer); } tran_cb_send_msg(M_TRAN_TX_ERROR_EVENT, MSG_LOW_PRIO); }
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); } }
static void gprs_response_cb(void *p) //定时器时间到,读缓冲区里数据 { if(gprs_response_timer != NULL) { hal_timer_cancel(&gprs_response_timer); DBG_ASSERT(gprs_response_timer == NULL __DBG_LINE); } osel_post(GPRS_RESPONSE_EVENT, NULL, OSEL_EVENT_PRIO_LOW); }
static void tx_und_cb(uint16_t int_time) { #if M_TRAN_DGB_EN > 0 m_tran_tracker.tx_und_cb++; #endif #if TXOK_INT_SIMU_EN > 0 if (txok_timer != NULL) // 确保该timer未被free { hal_timer_cancel(&txok_timer); } #endif if (ack_timer != NULL) { hal_timer_cancel(&ack_timer); } tran_cb_send_msg(M_TRAN_TXUND_EVENT, MSG_LOW_PRIO); }
static void gprs_open_timer_cb(void *p) { if(NULL != gprs_open_timer) { hal_timer_cancel(&gprs_open_timer); DBG_ASSERT(gprs_open_timer == NULL __DBG_LINE); } osel_post(GPRS_SEND_CMD_EVENT, (uint8_t *)(GPRS_CMD_AT), OSEL_EVENT_PRIO_LOW); }
static void gprs_poll_event_handler(void) { if (NULL != gprs_task_timer) { hal_timer_cancel(&gprs_task_timer); DBG_ASSERT(gprs_task_timer == NULL __DBG_LINE); } HAL_TIMER_SET_REL(MS_TO_TICK(GPRS_POLL_TIME), //2s gprs_task_timer_cb, NULL, gprs_task_timer); DBG_ASSERT(gprs_task_timer != NULL __DBG_LINE); }
/*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); } }
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); } }