void recv_USART_bytes(unsigned char* out, int n) { int i; for(i = 0; i < n; i++) { out[i] = usart_recv_blocking(USART2); } }
void uart_cinit(void *config) { usart = (uint32_t)config; /* board is expected to do pin and clock setup */ /* do usart setup */ //USART_CR1(usart) |= (1 << 15); /* because libopencm3 doesn't know the OVER8 bit */ usart_set_baudrate(usart, 115200); usart_set_databits(usart, 8); usart_set_stopbits(usart, USART_STOPBITS_1); usart_set_mode(usart, USART_MODE_TX_RX); usart_set_parity(usart, USART_PARITY_NONE); usart_set_flow_control(usart, USART_FLOWCONTROL_NONE); /* and enable */ usart_enable(usart); #if 0 usart_send_blocking(usart, 'B'); usart_send_blocking(usart, 'B'); usart_send_blocking(usart, 'B'); usart_send_blocking(usart, 'B'); while (true) { int c; c = usart_recv_blocking(usart); usart_send_blocking(usart, c); } #endif }
int main() { rcc_clock_setup_in_hse_8mhz_out_72mhz(); usart_setup(); UARTSink sink; arm_bootloader::Handler handler(sink, arm_bootloader::get_unique_dest(), 1024); while(true) { handler.handleByte(usart_recv_blocking(USART1)); } }
int _read(int file, char *buf, int len) { if (STDIN_FILENO == file) { buf[0] = usart_recv_blocking(USART_ID); #ifdef USART_AUTO_ECHO putchar(buf[0]); fflush(stdout); #endif return 1; } else { errno = EBADF; return -1; } }
/* * A buffered line editing function. */ void get_buffered_line(void) { char c; if (start_ndx != end_ndx) { return; } while (1) { c = usart_recv_blocking(USART2); if (c == '\r') { buf[end_ndx] = '\n'; end_ndx = inc_ndx(end_ndx); buf[end_ndx] = '\0'; usart_send_blocking(USART2, '\r'); usart_send_blocking(USART2, '\n'); return; } /* ^H or DEL erase a character */ if ((c == '\010') || (c == '\177')) { if (buf_len == 0) { usart_send_blocking(USART2, '\a'); } else { back_up(); } /* ^W erases a word */ } else if (c == 0x17) { while ((buf_len > 0) && (!(isspace((int) buf[end_ndx])))) { back_up(); } /* ^U erases the line */ } else if (c == 0x15) { while (buf_len > 0) { back_up(); } /* Non-editing character so insert it */ } else { if (buf_len == (BUFLEN - 1)) { usart_send_blocking(USART2, '\a'); } else { buf[end_ndx] = c; end_ndx = inc_ndx(end_ndx); usart_send_blocking(USART2, c); } } } }
u8 tty_get_noblock(void) { #ifdef BUFFERED u8 ch; //check if buffer is empty if (buffer_empty(&u1rx) == SUCCESS) { return 0; } else { buffer_get(&u1rx, &ch); return ch; } #else return usart_recv_blocking(USART1); #endif }
int main(void) { int32_t i; rcc_periph_clock_enable(RCC_GPIOA); gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO5); flash_set_ws(FLASH_ACR_LATENCY_2WS); rcc_set_adcpre(RCC_CFGR_ADCPRE_PCLK2_DIV4); /* 14MHz */ rcc_set_hpre(RCC_CFGR_HPRE_SYSCLK_NODIV); /* 56MHz */ rcc_set_ppre1(RCC_CFGR_PPRE1_HCLK_DIV2); /* 28MHz */ rcc_set_ppre2(RCC_CFGR_PPRE2_HCLK_NODIV); /* 56MHz */ rcc_set_pll_multiplication_factor(RCC_CFGR_PLLMUL_PLL_CLK_MUL14); /* 8MHz/2 x14 = 56MHz */ rcc_set_pll_source(RCC_CFGR_PLLSRC_HSI_CLK_DIV2); rcc_osc_on(PLL); rcc_wait_for_osc_ready(PLL); rcc_set_sysclk_source(RCC_CFGR_SW_SYSCLKSEL_PLLCLK); rcc_periph_clock_enable(RCC_USART2); gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART2_TX); gpio_set_mode(GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO_USART2_RX); uint32_t baud = 57600; uint32_t clock = 28000000; USART2_BRR = ((2 * clock) + baud) / (2 * baud); usart_set_databits(USART2, 8); usart_set_stopbits(USART2, USART_STOPBITS_1); usart_set_mode(USART2, USART_MODE_TX_RX); usart_set_parity(USART2, USART_PARITY_NONE); usart_set_flow_control(USART2, USART_FLOWCONTROL_NONE); usart_enable(USART2); usart_puts(USART2, "ready to receive.\r\n"); gpio_set(GPIOA, GPIO5); while(1) { uint16_t c; c = usart_recv_blocking(USART2); usart_send_blocking(USART2, toupper(c)); gpio_toggle(GPIOA, GPIO5); } }
static void usart_get_string(u32 usart, u8 *out_string, u16 str_max_size) { u8 sign = 0; u16 iter = 0; while(iter < str_max_size) { sign = usart_recv_blocking(usart); #if USART_ECHO_EN == 1 usart_send_blocking(usart, sign); #endif if(sign != '\n' && sign != '\r') out_string[iter++] = sign; else { out_string[iter] = 0; usart_send_string(USART1, (u8*)"\r\n", 3); break; } } }
/** * _read and _write for STM32 * * _read and _write are used by newlib's I/O routines (think printf, etc.) * If you want to use this code in your binary, you will have to initialise * the UART in your board's setup code and define STD_CON to your UART's * name in your board's Makefile */ int _read(int fd, void *buf, size_t count) { int rcvd; char *ptr; if(fd <= 2){ ptr = (char *) buf; rcvd = 0; while(count > 0){ *ptr = usart_recv_blocking(STD_CON); if(*ptr == '\r'){ *ptr = '\n'; } ++rcvd; --count; } }else{ rcvd = -1; errno = EIO; } return rcvd; }
int main (void) { clock_setup(); //setup the main clock for 168MHz usart_setup(); //setup usart1 for debug messages debug_send("\n\n\n*************************************\n"); debug_send("* Lynx test starting up *\n"); debug_send("* Waveforms *\n"); debug_send("* Joe Roberts *\n"); debug_send("* UROP - Summer 2013 *\n"); debug_send("*************************************\n\n\n"); debug_send("ledpins_setup()\n"); ledpins_setup(); //setup the status led's gpio's debug_send("dac_setup()\n"); dac_setup(); //setup the dac gpio's debug_send("transmit_timer_setup(1)\n"); transmit_timer_setup(1); //setup the transmit timer and its interrupt debug_send("Starting the transmission timer\n"); timer_enable_counter(TIM2); while(1) { usart_wait_recv_ready(USART2); timer_disable_counter(TIM2); timer_set_counter(TIM2, 0); char message = usart_recv_blocking(USART2); debug_send("\nRecieved "); usart_send_blocking(USART2, message); bool done = false; char param; while(!done) { debug_send(": Send your parameter [0-9]\n"); usart_wait_recv_ready(USART2); param = usart_recv_blocking(USART2); if((param>47)&&(param<58)) { done = true; param = param-48; //ASCII to number } else debug_send("\nParameter must be [0-9] - try again\n"); } if(message=='n') n = 1000*param; if(message=='t') { transmit_timer_setup(10*param); timer_enable_counter(TIM2); } if(message=='w') set_waveform(param); } debug_send("We somehow escaped the for ever loop\n"); debug_send("..This is a little sad, there's not much you can do to fix this\n\n\n\n"); debug_send("goodbye, cruel world"); while(1); return 1; }
int main(void) { uint16_t menu_choice = 0; int percentage = 0, kill = 0, shutdown_delay = 0, tmp; char str[20]; clock_setup(); gpio_setup(); usart_setup(); adc_setup(); while(1) { menu_choice = usart_recv_blocking(USART2); switch(menu_choice) { case 's': { percentage = get_percentage(); kill = get_kill_status(); sprintf(str, "%i %i\n\r", percentage, kill); for(int i = 0; i < 20; ++i) { if(str[i] == 0) break; usart_send_blocking(USART2, (uint16_t)str[i]); } break; } case 'k': { tmp = usart_recv_blocking(USART2); for(int i = 0; tmp != '^';) { shutdown_delay *= 10; shutdown_delay += (tmp - 48); tmp = usart_recv_blocking(USART2); } sprintf(str, "Kill in %ims\n\r", shutdown_delay); for(int i = 0; i < 20; ++i) { if(str[i] == 0) break; usart_send_blocking(USART2, (uint16_t)str[i]); } delay_ms(shutdown_delay); sprintf(str, "Killing.\n\r"); for(int i = 0; i < 20; ++i) { if(str[i] == 0) break; usart_send_blocking(USART2, (uint16_t)str[i]); } shutdown_delay = 0; break; } default: break; } //sprintf(str, "%i\n\r", adc_dat); //for(int i = 0; i < 20; ++i) { // if(str[i] == 0) break; // usart_send_blocking(USART2, (uint16_t)str[i]); //} //delay_ms(10); } // while(1) return 0; }
unsigned char serial_recv_blocking() { return usart_recv_blocking(USART1); }
u8 gps_read_byte(void) { u16 b = usart_recv_blocking(GPS_USART); return (u8)(b & 0xFF); }