void * UART_Get(void) { uart_tx_task(); // Rx Task static uint8_t rx_pos = 0; static uint8_t rx_len = 0; static MQ_t * pRx_buf; static uint16_t rx_wd = 0; while(hal_uart_datardy(UART_PHY_PORT)) { uint8_t data = hal_uart_get(UART_PHY_PORT); rx_wd = (HAL_get_ms() & 0xFFFF); if(rx_len == 0) { if((data >= 2) && (data < sizeof(MQTTSN_MESSAGE_t))) { pRx_buf = mqAlloc(sizeof(MQ_t)); rx_len = data; rx_pos = 0; } } else { if(rx_pos < sizeof(MQTTSN_MESSAGE_t)) pRx_buf->m.raw[rx_pos++] = data; if(rx_pos == rx_len) { memcpy(pRx_buf->a.phy1addr, (const void *)&uart_addr, sizeof(UART_ADDR_t)); pRx_buf->Length = rx_len; rx_len = 0; #ifdef LED_On LED_On(); #endif // LED_On return pRx_buf; } } } if((rx_len != 0) && (((HAL_get_ms() & 0xFFFF) - rx_wd) > 100)) { rx_len = 0; mqFree(pRx_buf); } return NULL; }
void * UART_Get(void) { uart_tx_task(); // Rx Task static uint8_t rx_pos = 0; static uint8_t rx_len = 0; static MQ_t * pRx_buf; static uint32_t rx_wd = 0; while(hal_uart_datardy(UART_PHY_PORT)) { uint8_t data = hal_uart_get(UART_PHY_PORT); rx_wd = hal_get_ms() + 50; if(rx_len == 0) { if(data >= 2) { pRx_buf = mqAlloc(sizeof(MQ_t)); if(pRx_buf != NULL) rx_len = data; rx_pos = 0; } } else { if(rx_pos < sizeof(MQTTSN_MESSAGE_t)) pRx_buf->raw[rx_pos++] = data; if(rx_pos == rx_len) { memcpy(pRx_buf->phy1addr, (const void *)&uart_addr, sizeof(UART_ADDR_t)); pRx_buf->Length = rx_len; rx_len = 0; Activity(UART_PHY); return pRx_buf; } } } if((rx_len != 0) && (rx_wd < hal_get_ms())) rx_len = 0; return NULL; }
void serProc(void) { uint8_t port; for(port = 0; port < EXTSER_USED; port++) { if(extSerV[port] != NULL) { if(extSerV[port]->flags & EXTSER_FLAG_TXEN) { if((extSerV[port]->pTxBuf != NULL) && (hal_uart_free(port))) { mqFree(extSerV[port]->pTxBuf); extSerV[port]->pTxBuf = NULL; } } if(extSerV[port]->flags & EXTSER_FLAG_RXEN) { if(hal_uart_datardy(port)) { uint8_t tmphead = extSerV[port]->RxHead + 1; if(tmphead >= sizeof(MQ_t)) tmphead = 0; if(tmphead != extSerV[port]->RxTail) { extSerV[port]->pRxBuf[extSerV[port]->RxHead] = hal_uart_get(port); extSerV[port]->RxHead = tmphead; uint8_t size; if(tmphead > extSerV[port]->RxTail) size = tmphead - extSerV[port]->RxTail; else size = sizeof(MQ_t) - extSerV[port]->RxTail + tmphead; if(size < (sizeof(MQ_t)/2)) extSerV[port]->flags &= ~EXTSER_FLAG_RXRDY; else extSerV[port]->flags |= EXTSER_FLAG_RXRDY; } } } } } }