Пример #1
0
void gprs_open_handler(void)
{
#if GPRS_DEBUG
    serial_write(HAL_UART_2, GPRS_OPENING, sizeof(GPRS_OPENING)-1); 
#endif    
    
    gprs_switch();
    
    if(gprs_open_timer == NULL)
    {
        HAL_TIMER_SET_REL(MS_TO_TICK(GPRS_RESPONSE_TIME_1S),   
                          gprs_open_timer_cb,
                          NULL,
                          gprs_open_timer);
        DBG_ASSERT(gprs_open_timer != NULL __DBG_LINE); 
    }
    
    gprs_mode = GPRS_RUN;
    
    gprs_restart_cnt    = 0;
    gprs_cmd_send_cnt   = 0;
    gprs_send_data_cnt  = 0;
    gprs_cmd_send_cpin  = 0; 
    gprs_cmd_send_cipstart = 0;
    gprs_cmd_send_cgatt = 0;  
    
    gprs_flush_receive_buf();
    hal_uart_recv_enable(HAL_UART_4);
}
Пример #2
0
void gprs_restart_handler(void)
{
#if GPRS_DEBUG
    serial_write(HAL_UART_2, GPRS_RESTARTING, sizeof(GPRS_RESTARTING)-1); 
#endif
    
    if(gprs_restart_cnt > 3)
    {
        if(GET_DATA_FROM_QUEUE)        //提取发送缓冲区第一位信息并丢弃
        {
            gprs_restart_cnt = 0;
            (* gprs_cb)(GS_ERROR, n_item.gprs_tag);
        }
        gprs_close_handler();
    }
    else
    {
        gprs_restart_cnt++;
        gprs_mode = GPRS_RUN;
        gprs_restart();
        if(gprs_open_timer == NULL)
        {
            HAL_TIMER_SET_REL(MS_TO_TICK(GPRS_RESPONSE_TIME_1S),   
                              gprs_open_timer_cb,
                              NULL,
                              gprs_open_timer);
            DBG_ASSERT(gprs_open_timer != NULL __DBG_LINE); 
        }
    }
}
Пример #3
0
/*set a random time for backing off of csma*/
static void csma_backoff_tick(uint32_t time_us)
{
    if (csma_timer == NULL)
    {
        HAL_TIMER_SET_REL(US_TO_TICK(time_us - 80), csma_tick_cb, NULL, csma_timer);
        DBG_ASSERT(csma_timer != NULL __DBG_LINE);
    }
}
Пример #4
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);
    }
}
Пример #5
0
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); 
   
}
Пример #6
0
/*the founction uses to wait ack*/
static void tran_wait_ack(void)
{
    tran_state.ack_received = FALSE;
    tran_state.ack_timeout_happened = FALSE;

    if (ack_timer == NULL)
    {
        m_tran_recv();

        HAL_TIMER_SET_REL(MS_TO_TICK(MS_FOR_ACK), ack_timeout_cb, NULL, ack_timer);
        DBG_ASSERT(ack_timer != NULL __DBG_LINE);

#if M_TRAN_DGB_EN > 0
        m_tran_tracker.waitting_ack++;
#endif
    }
    else
    {
        /* a very strange state*/
        DBG_ASSERT(FALSE __DBG_LINE);
    }
}
Пример #7
0
static void gprs_send_cmd_event_handler(uint8_t type)
{
    switch (type)
    {
    case GPRS_CMD_AT:
        gprs_cmd_type = GPRS_CMD_AT;
        
        if(gprs_response_timer == NULL)
        {
            HAL_TIMER_SET_REL(MS_TO_TICK(GPRS_RESPONSE_TIME_1S),   
                              gprs_response_cb,
                              NULL,
                              gprs_response_timer);
            DBG_ASSERT(gprs_response_timer != NULL __DBG_LINE); 
        }
        
        gprs_send_cmd(GPRS_AT, sizeof(GPRS_AT) - 1);
        break;
        
    case GPRS_CMD_ATE0:
        gprs_cmd_type = GPRS_CMD_ATE0;
        
        if(gprs_response_timer == NULL)
        {
            //因为发AT是GPRS刚刚启动要等待时间长点
            HAL_TIMER_SET_REL(MS_TO_TICK(GPRS_RESPONSE_TIME_1S),   
                              gprs_response_cb,
                              NULL,
                              gprs_response_timer);
            DBG_ASSERT(gprs_response_timer != NULL __DBG_LINE); 
        }
        
        gprs_send_cmd(GPRS_ATE0, sizeof(GPRS_ATE0) - 1);
        break;  
        
    case GPRS_CMD_CGATT:
        gprs_cmd_type = GPRS_CMD_CGATT;
        
        if(gprs_response_timer == NULL)
        {
            HAL_TIMER_SET_REL(MS_TO_TICK(GPRS_RESPONSE_TIME_2S),   
                              gprs_response_cb,
                              NULL,
                              gprs_response_timer);
            DBG_ASSERT(gprs_response_timer != NULL __DBG_LINE); 
        }
        
        gprs_send_cmd(GPRS_CGATT, sizeof(GPRS_CGATT) - 1); 
        break;
        
    case GPRS_CMD_CIPSTART:
        gprs_cmd_type = GPRS_CMD_CIPSTART;
        
        if(gprs_response_timer == NULL)
        {
            HAL_TIMER_SET_REL(MS_TO_TICK(GPRS_RESPONSE_TIME_5S),   
                              gprs_response_cb,
                              NULL,
                              gprs_response_timer);
            DBG_ASSERT(gprs_response_timer != NULL __DBG_LINE); 
        }
        
        gprs_cmd_cipstart();
        break;
        
    case GPRS_CMD_SEND:
        gprs_cmd_type = GPRS_CMD_SEND;
        
        if(gprs_response_timer == NULL)
        {
            HAL_TIMER_SET_REL(MS_TO_TICK(GPRS_RESPONSE_TIME_500MS),   
                              gprs_response_cb,
                              NULL,
                              gprs_response_timer);
            DBG_ASSERT(gprs_response_timer != NULL __DBG_LINE); 
        }  
        
        gprs_presend_cmd();       //"AT+CIPSEND=len\r"
        break;
        
    case GPRS_SEND_DATA:
        gprs_cmd_type = GPRS_SEND_DATA;
        
        if(gprs_response_timer == NULL)
        {
            HAL_TIMER_SET_REL(MS_TO_TICK(GPRS_RESPONSE_TIME_1S),   
                              gprs_response_cb,
                              NULL,
                              gprs_response_timer);
            DBG_ASSERT(gprs_response_timer != NULL __DBG_LINE); 
        } 
        
        gprs_send_data();        //发送数据
        break;
        
    case GPRS_CMD_STATUS:
        gprs_cmd_type = GPRS_CMD_STATUS;
        
        if(gprs_response_timer == NULL)
        {
            HAL_TIMER_SET_REL(MS_TO_TICK(GPRS_RESPONSE_TIME_1S),   
                              gprs_response_cb,
                              NULL,
                              gprs_response_timer);
            DBG_ASSERT(gprs_response_timer != NULL __DBG_LINE); 
        }        
        
        gprs_send_cmd(GPRS_CIPSTATUS, 0x0D);
        break;
        
    case GPRS_CMD_CPIN:
        gprs_cmd_type = GPRS_CMD_CPIN;
        
        if(gprs_response_timer == NULL)
        {
            HAL_TIMER_SET_REL(MS_TO_TICK(GPRS_RESPONSE_TIME_2S),   
                              gprs_response_cb,
                              NULL,
                              gprs_response_timer);
            DBG_ASSERT(gprs_response_timer != NULL __DBG_LINE); 
        } 
        
        gprs_send_cmd(GPRS_CPIN, sizeof(GPRS_CPIN) - 1);
        break;
        
    case GPRS_CMD_CIPSHUT:
        gprs_cmd_type = GPRS_CMD_CIPSHUT;
        
        if(gprs_response_timer == NULL)
        {
            HAL_TIMER_SET_REL(MS_TO_TICK(GPRS_RESPONSE_TIME_2S),  
                              gprs_response_cb,
                              NULL,
                              gprs_response_timer);
            DBG_ASSERT(gprs_response_timer != NULL __DBG_LINE); 
        } 
        
        gprs_send_cmd(GPRS_CIPSHUT, sizeof(GPRS_CIPSHUT) - 1);
        break;
        
    case GPRS_CMD_CIPCLOSE:
        gprs_cmd_type = GPRS_CMD_CIPCLOSE;
        
        if(gprs_response_timer == NULL)
        {
            HAL_TIMER_SET_REL(MS_TO_TICK(GPRS_RESPONSE_TIME_2S),   
                              gprs_response_cb,
                              NULL,
                              gprs_response_timer);
            DBG_ASSERT(gprs_response_timer != NULL __DBG_LINE); 
        } 
        
        gprs_send_cmd(GPRS_CIPCLOSE, sizeof(GPRS_CIPCLOSE) - 1);
        break;
        
    default:
        break;          
    }
}
Пример #8
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);
    }
}