/********************************************************************* * @fn dataHandler * * @brief Callback from UART indicating a data coming * * @param port - data port. * * @param events - type of data. * * @return none */ static void dataHandler( uint8 port, uint8 events ) { if((events & HAL_UART_RX_TIMEOUT) == HAL_UART_RX_TIMEOUT) { osal_stop_timerEx( biscuit_TaskID, SBP_RX_TIME_OUT_EVT); uint8 len = NPI_RxBufLen(); uint8 buf[128]; NPI_ReadTransport( buf, len ); uint8 copy; if(len > (MAX_RX_LEN-rxLen)) { copy = MAX_RX_LEN - rxLen; rxLen = MAX_RX_LEN; } else { rxLen += len; copy = len; } for(uint8 i=0; i<copy; i++) { RXBuf[rxHead] = buf[i]; rxHead++; if(rxHead == MAX_RX_LEN) { rxHead = 0; } } osal_start_timerEx( biscuit_TaskID, SBP_RX_TIME_OUT_EVT, SBP_RX_TIME_OUT); } return; }
/* 1, 思路: 当串口收到数据后,就会马上调用以下回调函数,在实际测试中发现,此回调 函数调用频繁, 如果你不执行NPI_ReadTransport函数进行读取, 那么这个回调函数就会 频繁地被执行,但是,你通过串口发送一段数据, 你本意是想处理这一完整一段的数据,所以, 我们在下面引入了时间的处理方法, 也即接收的数据够多或者超时,就读取一次数据, 然后根据当前的状态决定执行,如果没有连接上,就把所有数据当做AT命令处理, 如果连接 上了,就把数据送到对端。 ---------------amomcu 2014.08.17 */ void NpiSerialCallback( uint8 port, uint8 events ) { (void)port;//加个 (void),是未了避免编译告警,明确告诉缓冲区不用理会这个变量 if (events & (HAL_UART_RX_TIMEOUT | HAL_UART_RX_FULL)) //串口有数据 { uint8 numBytes = 0; numBytes = NPI_RxBufLen(); //读出串口缓冲区有多少字节 if(numBytes == 0) { return; } else { //申请缓冲区buffer uint8 *buffer = osal_mem_alloc(numBytes); if(buffer) { //读取读取串口缓冲区数据,释放串口数据 NPI_ReadTransport(buffer,numBytes); //把收到的数据发送到串口-实现回环 NPI_WriteTransport(buffer, numBytes); //释放申请的缓冲区 osal_mem_free(buffer); } } } }
// 串口回调函数 static void simpleBLE_NpiSerialCallback( uint8 port, uint8 events ) { (void)port; if (events & (HAL_UART_RX_TIMEOUT | HAL_UART_RX_FULL)) //串口有数据 { uint8 numBytes = 0; numBytes = NPI_RxBufLen(); //读出串口缓冲区有多少字节 if(numBytes > 0) { uint8 *buffer = osal_mem_alloc(numBytes); if(buffer) { // 读出串口数据 NPI_ReadTransport(buffer,numBytes); #if 1 // 作为测试, 把读到的数据也通过串口返回, 这只是一个test功能, 你可以把去掉 NPI_WriteTransport(buffer,numBytes); #endif osal_mem_free(buffer); } } } }
// 串口回调函数 static void simpleBLE_NpiSerialCallback( uint8 port, uint8 events ) { (void)port; if (events & (HAL_UART_RX_TIMEOUT | HAL_UART_RX_FULL)) //串口有数据 { uint8 numBytes = 0; numBytes = NPI_RxBufLen(); //读出串口缓冲区有多少字节 if(numBytes > 0) { uint8 *buffer = osal_mem_alloc(numBytes); if(buffer) { NPI_ReadTransport(buffer,numBytes); NPI_WriteTransport(buffer,numBytes); osal_mem_free(buffer); } } } }
/*************************************************************************************************** * @fn uart_NpiSerialCallback * * @brief uart callback function * * @param port: uart port; events: events * * @return None ***************************************************************************************************/ void uart_NpiSerialCallback( uint8 port, uint8 events ) { (void)port;//加个 (void),是未了避免编译告警,明确告诉缓冲区不用理会这个变量 static uint32 old_time; //老时间 static uint32 old_time_data_len = 0; //老时间是的数据长度 uint32 new_time; //新时间 uint8 readMaxBytes = 32; if (events & (HAL_UART_RX_TIMEOUT | HAL_UART_RX_FULL)) //串口有数据 { uint8 numBytes = 0; numBytes = NPI_RxBufLen(); //读出串口缓冲区有多少字节 if(numBytes == 0) { old_time_data_len = 0; return; } if(old_time_data_len == 0) { old_time = osal_GetSystemClock(); //有数据来时, 记录一下 old_time_data_len = numBytes; } else { new_time = osal_GetSystemClock(); //当前时间 if( (numBytes >= readMaxBytes) || ( (new_time - old_time) > 50/*ms*/)) { uint8 sendBytes = 0; //申请缓冲区buffer uint8 *buffer = osal_mem_alloc(numBytes); if(numBytes > readMaxBytes) { sendBytes = readMaxBytes; } else { sendBytes = numBytes; } if(buffer) { //读取读取串口缓冲区数据,释放串口数据 NPI_ReadTransport(buffer,numBytes); //NPI_WriteTransport(buffer,numBytes); //NPI_WriteTransport("\r\n",2); //sprintf(buffer,"numBytes=%d\r\n",numBytes); //NPI_WriteTransport(buffer,strlen(buffer)); //NPI_WriteTransport(tmpbuffer,strlen(tmpbuffer)); // 命令处理函数 Uart_Msg_Handle(buffer,numBytes); //Uart_Msg_Handle(tmpbuffer,strlen(tmpbuffer)); //释放申请的缓冲区 osal_mem_free(buffer); } old_time = new_time; old_time_data_len = numBytes - sendBytes; } } } }
void cSerialPacketParser( uint8 port, uint8 events ) { //unused input parameters (void)port; (void)events; uint8 done = FALSE; uint8 numBytes; // get the number of available bytes to process numBytes = NPI_RxBufLen(); // check if there's any serial port data to process while ( (numBytes > 0) && (!done) ) { switch (serialRxState) { case SERIAL_STATE_LEN: //read the length (void)NPI_ReadTransport((uint8 *)&packet_length, 1); // decrement the number of available bytes numBytes -= 1; // next state serialRxState = SERIAL_STATE_DATA; //DROP THROUGH case SERIAL_STATE_DATA: //check if we've dma'd the entire packet if (numBytes >= packet_length) { // alloc temporary buffer uint8 *temp_buf; temp_buf = osal_mem_alloc( packet_length ); //store dma buffer into temp buffer (void)NPI_ReadTransport(temp_buf, packet_length); // copy data from temp buffer into rx circular buffer for (uint8 i = 0; i < packet_length; i++) { //copy one byte to data buffer serialBuffer[serialBufferOffset] = temp_buf[i]; //update offset serialBufferOffset = circular_add(serialBufferOffset,1); } //free temp buffer osal_mem_free(temp_buf); //decrement number of available bytes numBytes -= packet_length; //reset state machine serialRxState = SERIAL_STATE_LEN; } else { //not enough data to progress, so leave it in driver buffer done = TRUE; } break; } } }