/************************************************************************************************* * @fn UART Rx/Tx ISR * * @brief Called when a serial byte is ready to read and/or write. * NOTE: Assumes that uartRecord.configured is TRUE if this interrupt is enabled. * * @param void * * @return void **************************************************************************************************/ void HalUartISR(void) { UARTIntClear(HAL_UART_PORT, (UART_INT_RX | UART_INT_RT)); procRx(); UARTIntClear(HAL_UART_PORT, (UART_INT_TX | UART_INT_CTS)); procTx(); }
/************************************************************************************************* * @fn HalUARTWriteIsr() * * @brief Write a buffer to the UART * * @param port - UART port (not used.) * pBuffer - pointer to the buffer that will be written * length - length of * * @return length of the buffer that was sent *************************************************************************************************/ uint16 HalUARTWriteIsr(uint8 port, uint8 *pBuffer, uint16 length) { (void)port; if (!uartRecord.configured) { return 0; } uint16 idx = uartRecord.tx.bufferHead; uint16 cnt = uartRecord.tx.bufferTail; if (cnt == idx) { cnt = uartRecord.tx.maxBufSize; } else if (cnt > idx) { cnt = uartRecord.tx.maxBufSize - cnt + idx; } else /* (cnt < idx) */ { cnt = idx - cnt; } /* Accept "all-or-none" on write request. */ if (cnt < length) { return 0; } txMT = false; idx = uartRecord.tx.bufferTail; for (cnt = 0; cnt < length; cnt++) { uartRecord.tx.pBuffer[idx++] = pBuffer[cnt]; if (idx >= uartRecord.tx.maxBufSize) { idx = 0; } } halIntState_t intState; HAL_ENTER_CRITICAL_SECTION(intState); uartRecord.tx.bufferTail = idx; procTx(); HAL_EXIT_CRITICAL_SECTION(intState); /* Return the number of bytes actually put into the buffer. */ return length; }
/************************************************************************************************* * @fn Hal_UARTPollIsr * * @brief This routine simulate polling and has to be called by the main loop * * @param void * * @return void *************************************************************************************************/ void HalUARTPollIsr(void) { uint16 head = uartRecord.tx.bufferHead; uint16 tail = uartRecord.tx.bufferTail; /* If port is not configured, no point to poll it. */ if (!uartRecord.configured) { return; } halIntState_t intState; HAL_ENTER_CRITICAL_SECTION(intState); procRx(); procTx(); HAL_EXIT_CRITICAL_SECTION(intState); uint8 evts = 0; /* Report if Rx Buffer is full. */ if ((Hal_UART_RxBufLen(0) + 1) >= uartRecord.rx.maxBufSize) { evts = HAL_UART_RX_FULL; } /* Report if Rx Buffer is idled. */ if ((uartRecord.rxChRvdTime != 0) && ((osal_GetSystemClock() - uartRecord.rxChRvdTime) > uartRecord.idleTimeout)) { uartRecord.rxChRvdTime = 0; evts |= HAL_UART_RX_TIMEOUT; } if (Hal_UART_RxBufLen(0) >= uartRecord.rx.maxBufSize - uartRecord.flowControlThreshold) { evts |= HAL_UART_RX_ABOUT_FULL; } if (!txMT && (head == tail)) { txMT = true; evts |= HAL_UART_TX_EMPTY; } if (evts && uartRecord.callBackFunc) { (uartRecord.callBackFunc)(0, evts); } }
/************************************************************************************************* * @fn sbUartWrite() * * @brief Write a buffer to the UART. * * @param pBuf - pointer to the buffer that will be written. * len - length of buffer to write. * * @return length of the buffer that was sent. */ uint32 sbUartWrite(uint8 *pBuf, uint32 len) { uint32 idx = txHead; uint32 cnt = txTail; if (cnt == idx) { cnt = SB_BUF_SIZE; } else if (cnt > idx) { cnt = SB_BUF_SIZE - cnt + idx; } else // (cnt < idx) { cnt = idx - cnt; } // Accept "all-or-none" on write request. if (cnt < len) { return 0; } idx = txTail; for (cnt = 0; cnt < len; cnt++) { txBuf[idx++] = pBuf[cnt]; if (idx >= SB_BUF_SIZE) { // txBuf is a circular buffer. When reaching the end - go back to the start. idx = 0; } } txTail = idx; procTx(); return len; // Return the number of bytes actually put into the buffer. }
/************************************************************************************************* * @fn sbUartPoll * * @brief This routine simulate polling and has to be called by the main loop. * * @param void * * @return void */ void sbUartPoll(void) { procTx(); }