static rt_size_t rt_uart_write(rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size) { volatile uint8_t *p; struct uart_device * uart_dev; _lock((struct uart_device *)dev); uart_dev = (struct uart_device*)dev; p = (uint8_t*)buffer; while(size--) { /* * to be polite with serial console add a line feed * to the carriage return character */ if (*p == '\n' && (dev->open_flag & RT_DEVICE_FLAG_STREAM)) { UART_WriteByte(uart_dev->hw_instance, '\r'); } UART_WriteByte(uart_dev->hw_instance, *p++); } _unlock((struct uart_device *)dev); return size; }
/* read data.*/ while (size--) { while(UART_ReadByte(UART_DebugInstance, &ch)); *buffer++ = (char)ch & 0xFF; ++nChars; } return nChars; } #endif /* comiler support */ #else /* DO NOT USE STDIO */ static void UART_putstr(uint32_t instance, const char *str) { while(*str != '\0') { UART_WriteByte(instance, *str++); } }
int UART_printf(const char *fmt, ...) { char c; unsigned int *adx = (unsigned int*)(void*)&fmt + 1; _loop: while((c = *fmt++) != '%') { if (c == '\0') return 0; UART_WriteByte(UART_DebugInstance, c); } c = *fmt++; if (c == 'd' || c == 'l') { printn(*adx, 10); } if (c == 'o' || c == 'x') { printn(*adx, c=='o'? 8:16 ); } if (c == 's') { UART_putstr(UART_DebugInstance, (char*)*adx); } adx++; goto _loop; return 0; }
int main(void) { uint32_t instance; /*存放 UART 的模块号 */ DelayInit(); DelayMs(10); GPIO_QuickInit(HW_GPIOE, 6, kGPIO_Mode_OPP); /* 初始化UART 使用快速初始化方式 波特率 115200 其他配置默认 返回初始化后 UART的模块号 */ instance = UART_QuickInit(UART0_RX_PD06_TX_PD07, 115200); /* 当使用串口初始化后 printf 被默认连接到第一个被初始化的串口上*/ printf("UART%d OK! Hello Kinetis\r\n", instance); while(1) { /* 串口 按字节发送 数据 注意 HW_UART0必须是已经初始化过的模块 否则 将产生错误*/ UART_WriteByte(instance, 'h'); UART_WriteByte(instance, 'e'); UART_WriteByte(instance, 'l'); UART_WriteByte(instance, 'l'); UART_WriteByte(instance, 'o'); UART_WriteByte(instance, '\r'); UART_WriteByte(instance, '\n'); /* 闪烁小灯 */ GPIO_ToggleBit(HW_GPIOE, 6); DelayMs(500); } }
int main() { uint8_t i,Track_Midline_value,A; DelayInit(); GPIO_QuickInit(HW_GPIOE,0,kGPIO_Mode_OPP); GPIO_WriteBit(HW_GPIOE,0,0); GPIO_QuickInit(HW_GPIOB,2,kGPIO_Mode_OPP); GPIO_QuickInit(HW_GPIOB,4,kGPIO_Mode_OPP); CCD_SI(0); CCD_CLK(0); UART_QuickInit(UART0_RX_PA15_TX_PA14, 115200); ADC_QuickInit(ADC0_SE8_PB0,kADC_SingleDiff8or9); CCD_Restet(); while(1) { //CCD(); CCD_gather(); CCD_Filtering(); Data_binarization(averaging()); for(i=0;i<128;i++) { if(CCD_filtering_data[127]==0xff) { CCD_filtering_data[127]=0xfe; } UART_WriteByte(HW_UART0,CCD_filtering_data[i]); } UART_WriteByte(HW_UART0,0xff); // Track_Midline_value = Track_Midline(); // A = Track_Midline_value; // printf("Track_Midline_value = %d\n",A); } }
static uint32_t send(uint8_t *buf, uint32_t len) { volatile int i; for(i=0; i<len; i++) { UART_WriteByte(HW_UART0, *buf++); } return len; }
static unsigned char uart_mcux_poll_out(struct device *dev, unsigned char c) { const struct uart_mcux_config *config = dev->config->config_info; while (!(UART_GetStatusFlags(config->base) & kUART_TxDataRegEmptyFlag)) ; UART_WriteByte(config->base, c); return c; }
static void printn(unsigned int n, unsigned int b) { static char *ntab = "0123456789ABCDEF"; unsigned int a, m; if (n / b) { a = n / b; printn(a, b); } m = n % b; UART_WriteByte(UART_DebugInstance, ntab[m]); }
static int uart_mcux_fifo_fill(struct device *dev, const u8_t *tx_data, int len) { const struct uart_mcux_config *config = dev->config->config_info; u8_t num_tx = 0; while ((len - num_tx > 0) && (UART_GetStatusFlags(config->base) & kUART_TxDataRegEmptyFlag)) { UART_WriteByte(config->base, tx_data[num_tx++]); } return num_tx; }
int main(void) { uint32_t i; uint32_t len; DelayInit(); GPIO_QuickInit(HW_GPIOE, 6, kGPIO_Mode_OPP); UART_QuickInit(UART0_RX_PD06_TX_PD07, 115200); UART_CallbackRxInstall(HW_UART0, UART_ISR); UART_ITDMAConfig(HW_UART0, kUART_IT_Rx, true); printf("NRF24L01 test\r\n"); /* 初始化 NRF2401模块 的SPI接口及片选 */ PORT_PinMuxConfig(HW_GPIOE, 1, kPinAlt2); PORT_PinMuxConfig(HW_GPIOE, 2, kPinAlt2); PORT_PinMuxConfig(HW_GPIOE, 3, kPinAlt2); PORT_PinMuxConfig(HW_GPIOE, 4, kPinAlt2); /* 初始化2401所需的CE引脚 */ GPIO_QuickInit(HW_GPIOE, 0 , kGPIO_Mode_OPP); /* 初始化2401模块*/ SPI_QuickInit(SPI1_SCK_PE02_SOUT_PE01_SIN_PE03, kSPI_CPOL0_CPHA0, 1*1000*1000); nrf24l01_init(HW_SPI1, 0); //检测是否存在无线设备,并配置接收和发送地址 if(nrf24l01_probe()) { printf("no nrf24l01 device found!\r\n"); } /* 进入Rx模式 */ nrf24l01_set_rx_mode(); while(1) { /* 如果收到串口数据则发送 */ if(gpRevChar != NULL) { nrf24l01_set_tx_mode(); nrf24l01_write_packet(gpRevChar, 1); nrf24l01_set_rx_mode(); gpRevChar = NULL; } /* 如果收到2401 的数据 则传输到串口 */ if(!nrf24l01_read_packet(NRF2401RXBuffer, &len)) { i = 0; while(len--) { UART_WriteByte(HW_UART0, NRF2401RXBuffer[i++]); } } } }
void hal_uart_start_tx(int port) { struct hal_uart *u; int data = -1; int rc; if (port >= FSL_FEATURE_SOC_UART_COUNT) { return; } u = &uarts[port]; if (!u->u_configured || !u->u_open) { return; } /* main loop */ while (true) { /* add data to TX ring buffer */ if (u->u_tx_started == 0) { rc = hal_uart_tx_fill_buf(u); if (rc > 0) { u->u_tx_started = 1; } } /* Send data only when UART TX register is empty and TX ring buffer has data to send out. */ while (!ur_is_empty(&u->ur_tx) && (kUART_TxDataRegEmptyFlag & UART_GetStatusFlags(u->u_base))) { data = ur_read(&u->ur_tx); UART_WriteByte(u->u_base, data); ur_bump(&u->ur_tx); } if (ur_is_empty(&u->ur_tx)) { if (u->u_tx_done) { u->u_tx_done(u->u_func_arg); } u->u_tx_started = 0; break; } } }
/*! * @brief Main function */ int main(void) { uart_config_t config; BOARD_InitPins(); BOARD_BootClockRUN(); /* * config.baudRate_Bps = 115200U; * config.parityMode = kUART_ParityDisabled; * config.stopBitCount = kUART_OneStopBit; * config.txFifoWatermark = 0; * config.rxFifoWatermark = 1; * config.enableTx = false; * config.enableRx = false; */ UART_GetDefaultConfig(&config); config.baudRate_Bps = BOARD_DEBUG_UART_BAUDRATE; config.enableTx = true; config.enableRx = true; UART_Init(DEMO_UART, &config, CLOCK_GetFreq(DEMO_UART_CLKSRC)); /* Send g_tipString out. */ UART_WriteBlocking(DEMO_UART, g_tipString, sizeof(g_tipString) / sizeof(g_tipString[0])); /* Enable RX interrupt. */ UART_EnableInterrupts(DEMO_UART, kUART_RxDataRegFullInterruptEnable | kUART_RxOverrunInterruptEnable); EnableIRQ(DEMO_UART_IRQn); while (1) { /* Send data only when UART TX register is empty and ring buffer has data to send out. */ while ((kUART_TxDataRegEmptyFlag & UART_GetStatusFlags(DEMO_UART)) && (rxIndex != txIndex)) { UART_WriteByte(DEMO_UART, demoRingBuffer[txIndex]); txIndex++; txIndex %= DEMO_RING_BUFFER_SIZE; } } }
static void UART_RX_ISR(uint16_t byteReceived) { /* 将接收到的数据发送回去 */ #if 0 UART_WriteByte(HW_UART4, byteReceived); #else static int index = -1; if (byteReceived == 0x53) {// S Start str_sign = 1; index++; } else if (byteReceived == 0x45) {// E End str_sign = 0; parameter(rec, index); index = -1; } else if (str_sign = 1 && index > -1) { rec[index++] = byteReceived; } #endif }
void DataScope(uint32_t instance, float *scope_buf) { unsigned char i, Send_Count; //通道显示数据 DataScope_Get_Channel_Data( scope_buf[0] , 1 ); //将数据 1.0 写入通道 1 DataScope_Get_Channel_Data( scope_buf[1] , 2 ); //将数据 2.0 写入通道 2 DataScope_Get_Channel_Data( scope_buf[2] , 3 ); //将数据 3.0 写入通道 3 DataScope_Get_Channel_Data( scope_buf[3] , 4 ); //将数据 4.0 写入通道 4 DataScope_Get_Channel_Data( scope_buf[4] , 5 ); //将数据 5.0 写入通道 5 DataScope_Get_Channel_Data( scope_buf[5] , 6 ); //将数据 6.0 写入通道 6 DataScope_Get_Channel_Data( scope_buf[6] , 7 ); //将数据 7.0 写入通道 7 DataScope_Get_Channel_Data( scope_buf[7] , 8 ); //将数据 8.0 写入通道 8 DataScope_Get_Channel_Data( scope_buf[8] , 9 ); //将数据 9.0 写入通道 9 DataScope_Get_Channel_Data( scope_buf[9] , 10); //将数据 10.0 写入通道 10 Send_Count = DataScope_Data_Generate(10); //生成10个通道的 for (i = 0; i < Send_Count; ++i) { UART_WriteByte(instance, DataScope_OutPut_Buffer[i]); } DelayMs(10); }
/* FILE is typedef’ d in stdio.h. */ FILE __stdout; FILE __stdin; int fputc(int ch,FILE *f) { UART_WriteByte(UART_DebugInstance, ch); return ch; } int fgetc(FILE *f) { uint16_t ch; while(UART_ReadByte(UART_DebugInstance, &ch)); return (ch & 0xFF); } #elif __ICCARM__ /* IAR support */ size_t __write(int handle, const unsigned char * buffer, size_t size) { size_t nChars = 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_STDOUT) && (handle != _LLIO_STDERR)) { return _LLIO_ERROR; } /* Send data.*/ while (size--) { UART_WriteByte(UART_DebugInstance, *buffer++); ++nChars; } return nChars; }
void serial_putc(serial_t *obj, int c) { while (!serial_writable(obj)); UART_WriteByte(uart_addrs[obj->index], (uint8_t)c); }
/* 串口接收中断回调函数 在函数中写中断想要做的事情 */ static void UART_RX_ISR(uint16_t byteReceived) { /* 将接收到的数据发送回去 */ UART_WriteByte(HW_UART0, byteReceived); }
int fputc(int ch,FILE *f) { UART_WriteByte(UART_DebugInstance, ch); return ch; }