size_t __read(int handle, unsigned char * buffer, size_t size) { size_t nChars = 0; uint16_t ch = 0; if (buffer == 0) { /* This means that we should flush internal buffers. Since we*/ /* don't we just return. (Remember, "handle" == -1 means that all*/ /* handles should be flushed.)*/ return 0; } /* This function only writes to "standard out" and "standard err",*/ /* for all other file handles it returns failure.*/ if ((handle != _LLIO_STDIN) && (handle != _LLIO_STDERR)) { return _LLIO_ERROR; } /* read data.*/ while (size--) { while(UART_ReadByte(UART_DebugInstance, &ch)); *buffer++ = (char)ch & 0xFF; ++nChars; } return nChars; }
static void uart_irq_handler(int port) { struct hal_uart *u; uint32_t status; uint8_t data; u = &uarts[port]; if (u->u_configured && u->u_open) { status = UART_GetStatusFlags(u->u_base); /* Check for RX data */ if (status & (kUART_RxDataRegFullFlag | kUART_RxOverrunFlag)) { data = UART_ReadByte(u->u_base); if (u->u_rx_stall || u->u_rx_func(u->u_func_arg, data) < 0) { /* * RX queue full. */ u->u_rx_stall = 1; ur_queue(&u->ur_rx, data); } } /* Check for TX complete */ if (kUART_TxDataRegEmptyFlag & UART_GetStatusFlags(u->u_base)) { if (u->u_tx_started) { u->u_tx_started = 0; if (u->u_tx_done) u->u_tx_done(u->u_func_arg); } } } }
/* 实验名称:WDOG窗口看门狗 实验平台:渡鸦开发板 板载芯片:MK60DN512ZVQ10 实验效果:开启看门狗的窗口模式,必须在规定的时间范围内喂狗,否则芯片复位 */ int main(void) { DelayInit(); GPIO_QuickInit(HW_GPIOE, 6, kGPIO_Mode_OPP); /* LED */ UART_QuickInit(UART0_RX_PD06_TX_PD07, 115200); /* 初始化看门狗 */ WDOG_InitTypeDef WDOG_InitStruct1 = {0}; WDOG_InitStruct1.mode = kWDOG_Mode_Window; //设置看门狗为窗口模式 WDOG_InitStruct1.windowInMs = 1000; /* 开窗时间 设置为窗体模式后 喂狗必须在 看门狗开始计时后 1000 - 2000 MS内完成 多了少了都复位 比普通看门狗严格*/ WDOG_InitStruct1.timeOutInMs = 2000; /* 时限 2000MS : 2000MS 内没有喂狗则复位 */ WDOG_Init(&WDOG_InitStruct1); printf("\r\nSYSTEM RESET!!!!!!!%d\r\n", WDOG_GetResetCounter()); printf("press any character to feed dog feed, must be in windows time\r\n"); static uint32_t i; uint16_t ch; while(1) { if(UART_ReadByte(HW_UART0, &ch) == 0) { printf("wdog feed succ!\r\n"); WDOG_Refresh(); //喂狗 i = 0; } printf("cnt:i:%d\r", i++); DelayMs(100); GPIO_ToggleBit(HW_GPIOE, 6); } }
int serial_getc(serial_t *obj) { while (!serial_readable(obj)); uint8_t data; data = UART_ReadByte(uart_addrs[obj->index]); return data; }
static int uart_mcux_poll_in(struct device *dev, unsigned char *c) { const struct uart_mcux_config *config = dev->config->config_info; u32_t flags = UART_GetStatusFlags(config->base); int ret = -1; if (flags & kUART_RxDataRegFullFlag) { *c = UART_ReadByte(config->base); ret = 0; } return ret; }
static int uart_mcux_fifo_read(struct device *dev, u8_t *rx_data, const int len) { const struct uart_mcux_config *config = dev->config->config_info; u8_t num_rx = 0; while ((len - num_rx > 0) && (UART_GetStatusFlags(config->base) & kUART_RxDataRegFullFlag)) { rx_data[num_rx++] = UART_ReadByte(config->base); } return num_rx; }
void DEMO_UART_IRQHandler(void) { uint8_t data; /* If new data arrived. */ if ((kUART_RxDataRegFullFlag | kUART_RxOverrunFlag) & UART_GetStatusFlags(DEMO_UART)) { data = UART_ReadByte(DEMO_UART); /* If ring buffer is not full, add data to ring buffer. */ if (((rxIndex + 1) % DEMO_RING_BUFFER_SIZE) != txIndex) { demoRingBuffer[rxIndex] = data; rxIndex++; rxIndex %= DEMO_RING_BUFFER_SIZE; } } }
int fgetc(FILE *f) { uint16_t ch; while(UART_ReadByte(UART_DebugInstance, &ch)); return (ch & 0xFF); }