void usbuart_set_line_coding(struct usb_cdc_line_coding *coding) { uart_set_baudrate(USBUART, coding->dwDTERate); uart_set_databits(USBUART, coding->bDataBits); switch(coding->bCharFormat) { case 0: case 1: uart_set_stopbits(USBUART, 1); break; case 2: uart_set_stopbits(USBUART, 2); break; } switch(coding->bParityType) { case 0: uart_set_parity(USBUART, UART_PARITY_NONE); break; case 1: uart_set_parity(USBUART, UART_PARITY_ODD); break; case 2: uart_set_parity(USBUART, UART_PARITY_EVEN); break; } }
void usbuart_init(void) { UART_PIN_SETUP(); periph_clock_enable(USBUART_CLK); __asm__("nop"); __asm__("nop"); __asm__("nop"); uart_disable(USBUART); /* Setup UART parameters. */ uart_clock_from_sysclk(USBUART); uart_set_baudrate(USBUART, 38400); uart_set_databits(USBUART, 8); uart_set_stopbits(USBUART, 1); uart_set_parity(USBUART, UART_PARITY_NONE); // Enable FIFO uart_enable_fifo(USBUART); // Set FIFO interrupt trigger levels to 1/8 full for RX buffer and // 7/8 empty (1/8 full) for TX buffer uart_set_fifo_trigger_levels(USBUART, UART_FIFO_RX_TRIG_1_8, UART_FIFO_TX_TRIG_7_8); uart_clear_interrupt_flag(USBUART, UART_INT_RX | UART_INT_RT); /* Enable interrupts */ uart_enable_interrupts(UART0, UART_INT_RX| UART_INT_RT); /* Finally enable the USART. */ uart_enable(USBUART); //nvic_set_priority(USBUSART_IRQ, IRQ_PRI_USBUSART); nvic_enable_irq(USBUART_IRQ); }
static void uart_setup(void) { u32 pins; /* Enable GPIOA in run mode. */ periph_clock_enable(RCC_GPIOA); /* Configure PA0 and PA1 as alternate function pins */ pins = GPIO0 | GPIO1; GPIO_AFSEL(GPIOA) |= pins; GPIO_DEN(GPIOA) |= pins; /* PA0 and PA1 are muxed to UART0 during power on, by default */ /* Enable the UART clock */ periph_clock_enable(RCC_UART0); /* We need a brief delay before we can access UART config registers */ __asm__("nop"); /* Disable the UART while we mess with its setings */ uart_disable(UART0); /* Configure the UART clock source as precision internal oscillator */ uart_clock_from_piosc(UART0); /* Set communication parameters */ uart_set_baudrate(UART0, 921600); uart_set_databits(UART0, 8); uart_set_parity(UART0, UART_PARITY_NONE); uart_set_stopbits(UART0, 1); /* Now that we're done messing with the settings, enable the UART */ uart_enable(UART0); }
int Camera_SendCmd(byte *cmd, int len) { int fd; int err = 0; byte cmd_stop[7] = {0xff, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01}; pthread_mutex_lock(&rs485_mutex); av_rs485_used = 1; fd = uart_open_dev(UART_PORT_RS485); if (fd == -1) { logcat("serial port open error: %s\n", strerror(errno)); pthread_mutex_unlock(&rs485_mutex); return -1; } uart_set_speed(fd, UART_RS485_SPEDD); if(uart_set_parity(fd, 8, 1, 'N') == -1) { logcat ("Set Parity Error\n"); pthread_mutex_unlock(&rs485_mutex); return -1; } system("echo 1 >/sys/devices/platform/gpio-power.0/rs485_direction"); cmd[len - 1] = checksum((cmd + 1), 5); err = io_writen(fd, cmd, len); if (err > 0) logcat("RS485: Send Command Sucess.\n"); else { logcat("write error, ret = %d\n", err); pthread_mutex_unlock(&rs485_mutex); return -1; } usleep(ACTION_INTERVAL); /* Stop Cmd */ cmd_stop[6] = checksum((cmd_stop + 1), 5); err = io_writen(fd, cmd_stop, 7); if (err == 7) logcat("RS485: Send Stop Command Sucess.\n"); else { logcat("write error, ret = %d\n", err); pthread_mutex_unlock(&rs485_mutex); return -1; } #ifdef _DEBUG print_message(cmd, len); #endif pthread_mutex_unlock(&rs485_mutex); return 0; }
int glue_set_line_coding_cb(uint32_t baud, uint8_t databits, enum usb_cdc_line_coding_bParityType cdc_parity, enum usb_cdc_line_coding_bCharFormat cdc_stopbits) { enum uart_parity parity; uint8_t uart_stopbits; if (databits < 5 || databits > 8) return 0; switch (cdc_parity) { case USB_CDC_NO_PARITY: parity = UART_PARITY_NONE; break; case USB_CDC_ODD_PARITY: parity = UART_PARITY_ODD; break; case USB_CDC_EVEN_PARITY: parity = UART_PARITY_EVEN; break; default: return 0; } switch (cdc_stopbits) { case USB_CDC_1_STOP_BITS: uart_stopbits = 1; break; case USB_CDC_2_STOP_BITS: uart_stopbits = 2; break; default: return 0; } /* Disable the UART while we mess with its settings */ uart_disable(UART1); /* Set communication parameters */ uart_set_baudrate(UART1, baud); uart_set_databits(UART1, databits); uart_set_parity(UART1, parity); uart_set_stopbits(UART1, uart_stopbits); /* Back to work. */ uart_enable(UART1); return 1; }
int Zigbee_Get_Device(int speed) { int fd; fd = uart_open_dev(ZIGBEE_UART_NAME); if (fd == -1) { logcat("serial port open error: %s\n", strerror(errno)); return -1; } uart_set_speed(fd, speed); if(uart_set_parity(fd, 8, 1, 'N') == -1) { logcat ("Set Parity Error\n"); return -1; } return fd; }
void traceswo_init(void) { periph_clock_enable(RCC_GPIOD); periph_clock_enable(TRACEUART_CLK); __asm__("nop"); __asm__("nop"); __asm__("nop"); gpio_mode_setup(SWO_PORT, GPIO_MODE_INPUT, GPIO_PUPD_NONE, SWO_PIN); gpio_set_af(SWO_PORT, 1, SWO_PIN); /* U2RX */ uart_disable(TRACEUART); /* Setup UART parameters. */ uart_clock_from_sysclk(TRACEUART); uart_set_baudrate(TRACEUART, 800000); uart_set_databits(TRACEUART, 8); uart_set_stopbits(TRACEUART, 1); uart_set_parity(TRACEUART, UART_PARITY_NONE); // Enable FIFO uart_enable_fifo(TRACEUART); // Set FIFO interrupt trigger levels to 4/8 full for RX buffer and // 7/8 empty (1/8 full) for TX buffer uart_set_fifo_trigger_levels(TRACEUART, UART_FIFO_RX_TRIG_1_2, UART_FIFO_TX_TRIG_7_8); uart_clear_interrupt_flag(TRACEUART, UART_INT_RX | UART_INT_RT); /* Enable interrupts */ uart_enable_interrupts(TRACEUART, UART_INT_RX | UART_INT_RT); /* Finally enable the USART. */ uart_enable(TRACEUART); nvic_set_priority(TRACEUART_IRQ, 0); nvic_enable_irq(TRACEUART_IRQ); /* Un-stall USB endpoint */ usbd_ep_stall_set(usbdev, 0x85, 0); gpio_mode_setup(GPIOD, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO3); }
uint8_t USART_Init(struct USART_configuration config) { // Add your code here. Don't forget that this function is supposed // to return an error code if something goes wrong! /* Create Buffers */ rx_buf = rb_create(RX_BUFFLEN); tx_buf = rb_create(TX_BUFFLEN); /* Enable UART receiver and transmitter */ uart_enable_rx(); uart_enable_tx(); /* Enable UART RX Complete Interrupt */ uart_enable_rx_complete_interrupt(); /* set baudrate */ if (uart_set_baudrate(config.baudrate)) goto error; /* Parity */ if (uart_set_parity(config.parity)) goto error; /* Stop bit */ if (uart_set_stopbit(config.stopbits)) goto error; /* Number of databits*/ if (uart_set_databits(config.databits)) goto error; /* No errors in configuration */ return OK; error: /* Reset; configuration to 8N1, 9600b */ uart_default_conf(); return ERROR; }
static void uart_setup(void) { /* Enable GPIOA in run mode. */ periph_clock_enable(RCC_GPIOA); /* Mux PA0 and PA1 to UART0 (alternate function 1) */ gpio_set_af(GPIOA, 1, GPIO0 | GPIO1); /* Enable the UART clock */ periph_clock_enable(RCC_UART0); /* We need a brief delay before we can access UART config registers */ __asm__("nop"); /* Disable the UART while we mess with its setings */ uart_disable(UART0); /* Configure the UART clock source as precision internal oscillator */ uart_clock_from_piosc(UART0); /* Set communication parameters */ uart_set_baudrate(UART0, 921600); uart_set_databits(UART0, 8); uart_set_parity(UART0, UART_PARITY_NONE); uart_set_stopbits(UART0, 1); /* Now that we're done messing with the settings, enable the UART */ uart_enable(UART0); }
static void cli_command_uart0(char *args) { char *token; int value; cli_strip_spaces(&args); token = args; if(cli_strip_word(&args)) { uart_printf("1 Incorrect format, uart0 [speed [baudrate]] [parity [even|odd|none]] [bits [7|8]]\n"); return; } if(strcmp(token, "speed") == 0) { ++args; cli_strip_spaces(&args); token = args; if(cli_strip_decimal_number(&args)) { uart_printf("2 Incorrect format, uart0 [speed [baudrate]] [parity [even|odd|none]] [bits [7|8]]\n"); return; } value = atoi(token); uart_set_baudrate(value); ++args; cli_strip_spaces(&args); token = args; cli_strip_word(&args); } if(strcmp(token, "parity") == 0) { ++args; cli_strip_spaces(&args); token = args; if(cli_strip_word(&args)) { uart_printf("3 Incorrect format, uart0 [speed [baudrate]] [parity [even|odd|none]] [bits [7|8]]\n"); return; } uart_set_parity(token); ++args; cli_strip_spaces(&args); token = args; cli_strip_word(&args); } if(strcmp(token, "bits") == 0) { ++args; cli_strip_spaces(&args); token = args; if(cli_strip_decimal_number(&args)) { uart_printf("4 Incorrect format, uart0 [speed [baudrate]] [parity [even|odd|none]] [bits [7|8]]\n"); return; } uart_set_bits(token); } }