static void uart_irq(UARTName uart_name) { int8_t id = get_uart_index(uart_name); if (id >= 0) { UART_HandleTypeDef *huart = &uart_handlers[id]; if (serial_irq_ids[id] != 0) { if (__HAL_UART_GET_FLAG(huart, UART_FLAG_TXE) != RESET) { if (__HAL_UART_GET_IT(huart, UART_IT_TXE) != RESET) { irq_handler(serial_irq_ids[id], TxIrq); } } if (__HAL_UART_GET_FLAG(huart, UART_FLAG_RXNE) != RESET) { if (__HAL_UART_GET_IT(huart, UART_IT_RXNE) != RESET) { irq_handler(serial_irq_ids[id], RxIrq); /* Flag has been cleared when reading the content */ } } if (__HAL_UART_GET_FLAG(huart, UART_FLAG_ORE) != RESET) { if (__HAL_UART_GET_IT(huart, UART_IT_ORE) != RESET) { __HAL_UART_CLEAR_IT(huart, UART_CLEAR_OREF); } } } } }
static void uart_irq(UARTName uart_name) { int8_t id = get_uart_index(uart_name); if (id >= 0) { UART_HandleTypeDef * huart = &uart_handlers[id]; if (serial_irq_ids[id] != 0) { if (__HAL_UART_GET_FLAG(huart, UART_FLAG_TXE) != RESET) { if (__HAL_UART_GET_IT_SOURCE(huart, UART_IT_TXE) != RESET) { irq_handler(serial_irq_ids[id], TxIrq); } } if (__HAL_UART_GET_FLAG(huart, UART_FLAG_RXNE) != RESET) { if (__HAL_UART_GET_IT_SOURCE(huart, UART_IT_RXNE) != RESET) { irq_handler(serial_irq_ids[id], RxIrq); /* Flag has been cleared when reading the content */ } } if (__HAL_UART_GET_FLAG(huart, UART_FLAG_ORE) != RESET) { if (__HAL_UART_GET_IT_SOURCE(huart, USART_IT_ERR) != RESET) { volatile uint32_t tmpval __attribute__((unused)) = huart->Instance->DR; // Clear ORE flag } } } } }
void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart) { if (__HAL_UART_GET_FLAG(huart, UART_FLAG_PE) != RESET) { volatile uint32_t tmpval = huart->Instance->DR; // Clear PE flag } else if (__HAL_UART_GET_FLAG(huart, UART_FLAG_FE) != RESET) { volatile uint32_t tmpval = huart->Instance->DR; // Clear FE flag } else if (__HAL_UART_GET_FLAG(huart, UART_FLAG_NE) != RESET) { volatile uint32_t tmpval = huart->Instance->DR; // Clear NE flag } else if (__HAL_UART_GET_FLAG(huart, UART_FLAG_ORE) != RESET) { volatile uint32_t tmpval = huart->Instance->DR; // Clear ORE flag } }
static void uart_irq(UARTName name, int id) { UartHandle.Instance = (USART_TypeDef *)name; if (serial_irq_ids[id] != 0) { if (__HAL_UART_GET_FLAG(&UartHandle, UART_FLAG_TC) != RESET) { irq_handler(serial_irq_ids[id], TxIrq); __HAL_UART_CLEAR_IT(&UartHandle, UART_FLAG_TC); } if (__HAL_UART_GET_FLAG(&UartHandle, UART_FLAG_RXNE) != RESET) { irq_handler(serial_irq_ids[id], RxIrq); volatile uint32_t tmpval = UartHandle.Instance->RDR; // Clear RXNE bit } } }
static void uart_irq(UARTName name, int id) { UartHandle.Instance = (USART_TypeDef *)name; if (serial_irq_ids[id] != 0) { if (__HAL_UART_GET_FLAG(&UartHandle, UART_FLAG_TC) != RESET) { irq_handler(serial_irq_ids[id], TxIrq); __HAL_UART_CLEAR_FLAG(&UartHandle, UART_FLAG_TC); } if (__HAL_UART_GET_FLAG(&UartHandle, UART_FLAG_RXNE) != RESET) { irq_handler(serial_irq_ids[id], RxIrq); __HAL_UART_CLEAR_FLAG(&UartHandle, UART_FLAG_RXNE); } } }
void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart) { if (__HAL_UART_GET_FLAG(huart, UART_FLAG_PE) != RESET) { __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_PEF); } if (__HAL_UART_GET_FLAG(huart, UART_FLAG_FE) != RESET) { __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_FEF); } if (__HAL_UART_GET_FLAG(huart, UART_FLAG_NE) != RESET) { __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_NEF); } if (__HAL_UART_GET_FLAG(huart, UART_FLAG_ORE) != RESET) { __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF); } }
U32 End_uart_send(UCHAR end_id, UCHAR* txbuf, USHORT txnum ) { UART_HandleTypeDef *UARTx; P_UART_CCB p_uc; UCHAR send_byte=0; if( txnum < 1 ) { return FALSE; } switch(end_id) { case PC_COM_PORT: UARTx = &PC_UART; break; case RS485_COM_PORT: UARTx= &RS485_UART; break; case PLC_COM_PORT: UARTx = &PLC_UART; break; default: return FALSE; } p_uc = &g_uart_ccb[end_id]; p_uc->gpUartTxAddress = txbuf; p_uc->gUartTxCnt = txnum; send_byte = *(p_uc->gpUartTxAddress); p_uc->gpUartTxAddress++; p_uc->gUartTxCnt--; while(RESET == __HAL_UART_GET_FLAG(UARTx, UART_FLAG_TXE)); //关键 UARTx->Instance->DR = (send_byte & (uint8_t)0xFF); while(RESET == __HAL_UART_GET_FLAG(UARTx, UART_FLAG_TC)); //关键 if(p_uc->gUartTxCnt) __HAL_UART_ENABLE_IT(UARTx, UART_IT_TXE); return TRUE; };
static void uart_irq(uint8_t id) { UART_HandleTypeDef *handle = &UartHandle[id]; if (serial_irq_ids[id] != 0) { if (__HAL_UART_GET_FLAG(handle, UART_FLAG_TC) != RESET) { irq_handlers[id](serial_irq_ids[id], TxIrq); __HAL_UART_CLEAR_FLAG(handle, UART_FLAG_TC); } if (__HAL_UART_GET_FLAG(handle, UART_FLAG_RXNE) != RESET) { irq_handlers[id](serial_irq_ids[id], RxIrq); __HAL_UART_CLEAR_FLAG(handle, UART_FLAG_RXNE); } } }
// this IRQ handler is set up to handle RXNE interrupts only void uart_irq_handler(mp_uint_t uart_id) { // get the uart object pyb_uart_obj_t *self = MP_STATE_PORT(pyb_uart_obj_all)[uart_id - 1]; if (self == NULL) { // UART object has not been set, so we can't do anything, not // even disable the IRQ. This should never happen. return; } if (__HAL_UART_GET_FLAG(&self->uart, UART_FLAG_RXNE) != RESET) { if (self->read_buf_len != 0) { uint16_t next_head = (self->read_buf_head + 1) % self->read_buf_len; if (next_head != self->read_buf_tail) { // only read data if room in buf #if defined(STM32F7) || defined(STM32L4) || defined(STM32H7) int data = self->uart.Instance->RDR; // clears UART_FLAG_RXNE #else int data = self->uart.Instance->DR; // clears UART_FLAG_RXNE #endif data &= self->char_mask; if (self->char_width == CHAR_WIDTH_9BIT) { ((uint16_t*)self->read_buf)[self->read_buf_head] = data; } else { self->read_buf[self->read_buf_head] = data; } self->read_buf_head = next_head; } else { // No room: leave char in buf, disable interrupt __HAL_UART_DISABLE_IT(&self->uart, UART_IT_RXNE); } } } }
mp_uint_t uart_rx_any(pyb_uart_obj_t *uart_obj) { #if 0 return __HAL_UART_GET_FLAG(&uart_obj->uart, UART_FLAG_RXNE); #else return 0; #endif }
bool uart_rx_any(pyb_uart_obj_t *uart_obj) { #if 0 return __HAL_UART_GET_FLAG(&uart_obj->uart, UART_FLAG_RXNE); #else return false; #endif }
static int uart_stm32_fifo_read(struct device *dev, uint8_t *rx_data, const int size) { struct uart_stm32_data *data = DEV_DATA(dev); UART_HandleTypeDef *UartHandle = &data->huart; uint8_t num_rx = 0; while ((size - num_rx > 0) && __HAL_UART_GET_FLAG(UartHandle, UART_FLAG_RXNE)) { /* Clear the interrupt */ __HAL_UART_CLEAR_FLAG(UartHandle, UART_FLAG_RXNE); /* Receive a character (8bit , parity none) */ #if defined(CONFIG_SOC_SERIES_STM32F1X) || defined(CONFIG_SOC_SERIES_STM32F4X) /* Use direct access for F1, F4 until Low Level API is available * Once it is we can remove the if/else */ rx_data[num_rx++] = (uint8_t)(UartHandle->Instance->DR & (uint8_t)0x00FF); #else rx_data[num_rx++] = LL_USART_ReceiveData8(UartHandle->Instance); #endif } return num_rx; }
// this IRQ handler is set up to handle RXNE interrupts only void uart_irq_handler(mp_uint_t uart_id) { // get the uart object pyb_uart_obj_t *self = MP_STATE_PORT(pyb_uart_obj_all)[uart_id - 1]; if (self == NULL) { // UART object has not been set, so we can't do anything, not // even disable the IRQ. This should never happen. return; } if (__HAL_UART_GET_FLAG(&self->uart, UART_FLAG_RXNE) != RESET) { #if defined(MCU_SERIES_F7) int data = self->uart.Instance->RDR; // clears UART_FLAG_RXNE #else int data = self->uart.Instance->DR; // clears UART_FLAG_RXNE #endif data &= self->char_mask; if (self->read_buf_len != 0) { uint16_t next_head = (self->read_buf_head + 1) % self->read_buf_len; if (next_head != self->read_buf_tail) { // only store data if room in buf if (self->char_width == CHAR_WIDTH_9BIT) { ((uint16_t*)self->read_buf)[self->read_buf_head] = data; } else { self->read_buf[self->read_buf_head] = data; } self->read_buf_head = next_head; } } else { // TODO set flag for buffer overflow } } }
static int uart_stm32_fifo_fill(struct device *dev, const uint8_t *tx_data, int size) { struct uart_stm32_data *data = DEV_DATA(dev); UART_HandleTypeDef *UartHandle = &data->huart; uint8_t num_tx = 0; while ((size - num_tx > 0) && __HAL_UART_GET_FLAG(UartHandle, UART_FLAG_TXE)) { /* TXE flag will be cleared with byte write to DR register */ /* Send a character (8bit , parity none) */ #if defined(CONFIG_SOC_SERIES_STM32F1X) || defined(CONFIG_SOC_SERIES_STM32F4X) /* Use direct access for F1, F4 until Low Level API is available * Once it is we can remove the if/else */ UartHandle->Instance->DR = (tx_data[num_tx++] & (uint8_t)0x00FF); #else LL_USART_TransmitData8(UartHandle->Instance, tx_data[num_tx++]); #endif } return num_tx; }
int serial_writable(serial_t *obj) { int status; UartHandle.Instance = (USART_TypeDef *)(obj->uart); // Check if data is transmitted status = ((__HAL_UART_GET_FLAG(&UartHandle, UART_FLAG_TXE) != RESET) ? 1 : 0); return status; }
static int uart_stm32_irq_is_pending(struct device *dev) { struct uart_stm32_data *data = DEV_DATA(dev); UART_HandleTypeDef *UartHandle = &data->huart; return __HAL_UART_GET_FLAG(UartHandle, UART_FLAG_TXE | UART_FLAG_RXNE); }
int serial_readable(serial_t *obj) { int status; UartHandle.Instance = (USART_TypeDef *)(obj->uart); // Check if data is received status = ((__HAL_UART_GET_FLAG(&UartHandle, UART_FLAG_RXNE) != RESET) ? 1 : 0); return status; }
int serial_readable(serial_t *obj) { struct serial_s *obj_s = SERIAL_S(obj); UART_HandleTypeDef *huart = &uart_handlers[obj_s->index]; // Check if data is received return (__HAL_UART_GET_FLAG(huart, UART_FLAG_RXNE) != RESET) ? 1 : 0; }
int uart_putc(int port, char c) { while (__HAL_UART_GET_FLAG(&handle, UART_FLAG_TXE) == RESET) ; handle.Instance->TDR = (c & (uint8_t)0xFF); return 1; }
int serial_readable(serial_t *obj) { int status; UART_HandleTypeDef *handle = &UartHandle[SERIAL_OBJ(index)]; // Check if data is received status = ((__HAL_UART_GET_FLAG(handle, UART_FLAG_RXNE) != RESET) ? 1 : 0); return status; }
int serial_writable(serial_t *obj) { int status; UART_HandleTypeDef *handle = &UartHandle[SERIAL_OBJ(index)]; // Check if data is transmitted status = ((__HAL_UART_GET_FLAG(handle, UART_FLAG_TXE) != RESET) ? 1 : 0); return status; }
int serial_writable(serial_t *obj) { struct serial_s *obj_s = SERIAL_S(obj); UART_HandleTypeDef *huart = &uart_handlers[obj_s->index]; // Check if data is transmitted return (__HAL_UART_GET_FLAG(huart, UART_FLAG_TXE) != RESET) ? 1 : 0; }
mp_uint_t uart_rx_any(pyb_uart_obj_t *self) { int buffer_bytes = self->read_buf_head - self->read_buf_tail; if (buffer_bytes < 0) { return buffer_bytes + self->read_buf_len; } else if (buffer_bytes > 0) { return buffer_bytes; } else { return __HAL_UART_GET_FLAG(&self->uart, UART_FLAG_RXNE) != RESET; } }
void UART_Send_Data(uint8_t* Buf, uint16_t Len) { uint16_t i; for (i=0;i<Len;i++) { huart3.Instance->DR = *(Buf + i); while (__HAL_UART_GET_FLAG(&huart3, UART_FLAG_TXE) == RESET){}//TX empty } }
static int drv_putc(struct rt_serial_device *serial, char c) { struct drv_uart *uart; RT_ASSERT(serial != RT_NULL); uart = (struct drv_uart *)serial->parent.user_data; while((__HAL_UART_GET_FLAG(&uart->UartHandle, UART_FLAG_TXE) == RESET)); uart->UartHandle.Instance->DR = c; return 1; }
/** * @brief This function handles USART1 global interrupt. */ void USART1_IRQHandler(void) { uint8_t tmp1,tmp2; tmp1 = __HAL_UART_GET_FLAG(&huart1, UART_FLAG_RXNE); tmp2 = __HAL_UART_GET_IT_SOURCE(&huart1, UART_IT_RXNE); if((tmp1 != RESET) && (tmp2 != RESET)) { RxBuffer[RxBufferHead] = UART_ReceiveData(&huart1); RxBufferHead = (RxBufferHead + 1) % RX_BUFFER_SIZE; } }
// Waits at most timeout milliseconds for at least 1 char to become ready for // reading (from buf or for direct reading). // Returns true if something available, false if not. STATIC bool uart_rx_wait(pyb_uart_obj_t *self, uint32_t timeout) { uint32_t start = HAL_GetTick(); for (;;) { if (self->read_buf_tail != self->read_buf_head || __HAL_UART_GET_FLAG(&self->uart, UART_FLAG_RXNE) != RESET) { return true; // have at least 1 char ready for reading } if (HAL_GetTick() - start >= timeout) { return false; // timeout } __WFI(); } }
/** * @brief This function handles UART interrupt request. * @param huart: pointer to a UART_HandleTypeDef structure that contains * the configuration information for the specified UART module. * @retval None */ void vUARTIRQHandler(UART_HandleTypeDef *pxUARTHandle) { uint32_t tmp1 = 0, tmp2 = 0; tmp1 = __HAL_UART_GET_FLAG(pxUARTHandle, UART_FLAG_RXNE); tmp2 = __HAL_UART_GET_IT_SOURCE(pxUARTHandle, UART_IT_RXNE); /* UART in mode Receiver ---------------------------------------------------*/ if((tmp1 != RESET) && (tmp2 != RESET)) { vUARTReceive(pxUARTHandle); } }
/*====================================================================================================*/ int8_t UART_RecvByteWTO( UART_HandleTypeDef *huart, uint8_t *recvData, uint32_t timeout ) { while(!__HAL_UART_GET_FLAG(huart, UART_FLAG_RXNE)) { if(timeout-- > 0) HAL_Delay(1); else return ERROR; // timeout } *recvData = (uint16_t)(huart->Instance->DR & (uint16_t)0x01FF); return SUCCESS; }
// Waits at most timeout milliseconds for TX register to become empty. // Returns true if can write, false if can't. STATIC bool uart_tx_wait(pyb_uart_obj_t *self, uint32_t timeout) { uint32_t start = HAL_GetTick(); for (;;) { if (__HAL_UART_GET_FLAG(&self->uart, UART_FLAG_TXE)) { return true; // tx register is empty } if (HAL_GetTick() - start >= timeout) { return false; // timeout } __WFI(); } }