Exemple #1
0
void UART_Initialize()
{
    /* Enable clocks for GPIO port containing _USART and USART */
    rcc_peripheral_enable_clock(&_USART_RCC_APB_ENR_IOP,   _USART_RCC_APB_ENR_IOP_EN);
    rcc_peripheral_enable_clock(&_USART_RCC_APB_ENR_USART, _USART_RCC_APB_ENR_USART_EN);

    /* Enable DMA clock */
    // TODO ENABLED ALREADY FOR ADC?
    rcc_peripheral_enable_clock(&RCC_AHBENR, _RCC_AHBENR_DMAEN);

    /* Setup GPIO pin GPIO_USARTX_TX on USART GPIO port for transmit. */
    gpio_set_mode(_USART_GPIO, GPIO_MODE_OUTPUT_50_MHZ,
                    GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, _USART_GPIO_USART_TX);
    //gpio_set_mode(GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT,
                  //GPIO_USART1_RX);

    /* Setup UART parameters. */
    usart_set_baudrate(_USART, 115200);
    usart_set_databits(_USART, 8);
    usart_set_stopbits(_USART, USART_STOPBITS_1);
    usart_set_mode(_USART, USART_MODE_TX);
    usart_set_parity(_USART, USART_PARITY_NONE);
    usart_set_flow_control(_USART, USART_FLOWCONTROL_NONE);
    //usart_set_mode(USART1, USART_MODE_TX_RX);

    /* Finally enable the USART. */
    usart_enable(_USART);

    nvic_set_priority(_USART_NVIC_DMA_CHANNEL_IRQ, 3);
    nvic_enable_irq(_USART_NVIC_DMA_CHANNEL_IRQ);

#if HAS_AUDIO_UART5
    /* Enable clocks for GPIO port C (for GPIO_UART5_TX) and UART5. */
    rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPCEN);
    rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_UART5EN);

    /* Setup GPIO pins to use UART5 */
    gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ,
                    GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_UART5_TX);

    /* Setup UART5 parameters. */
    usart_set_baudrate(UART5, 9600);
    usart_set_databits(UART5, 8);
    usart_set_stopbits(UART5, USART_STOPBITS_1);
    usart_set_parity(UART5, USART_PARITY_NONE);
    usart_set_mode(UART5, USART_MODE_TX);

    /* Finally enable the UART5. */
    usart_enable(UART5);
#endif
}
Exemple #2
0
/**
 * Initializes the clocks, the UART peripheral, and the RX and TX buffer
 * structures.
 */
static void comm_init(void)
{
	os_char_buffer_init(&bsp_rx_buffer,
	                    rx_buffer_mem,
	                    BSP_RX_BUFFER_SIZE,
	                    NULL);

	os_char_buffer_init(&bsp_tx_buffer,
	                    tx_buffer_mem,
	                    BSP_TX_BUFFER_SIZE,
	                    enable_usart1_tx_interrupt);

	rcc_periph_clock_enable(RCC_GPIOA);

	rcc_periph_clock_enable(RCC_USART1);

	gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO9 | GPIO10);
	gpio_set_af(GPIOA, GPIO_AF1, GPIO9 | GPIO10);

	usart_set_baudrate(USART1, 115200);
	usart_set_databits(USART1, 8);
	usart_set_flow_control(USART1, USART_FLOWCONTROL_NONE);
	usart_set_mode(USART1, USART_MODE_TX_RX);
	usart_set_parity(USART1, USART_PARITY_NONE);
	usart_set_stopbits(USART1, USART_CR2_STOP_1_0BIT);

	usart_enable_rx_interrupt(USART1);

	usart_enable(USART1);

	nvic_set_priority(NVIC_USART1_IRQ, 0);
	nvic_enable_irq(NVIC_USART1_IRQ);
}
Exemple #3
0
static void usart_setup(void)
{
	/* Enable the USART2 interrupt. */
	nvic_enable_irq(NVIC_USART2_IRQ);

	/* Setup GPIO pins for USART2 transmit. */
	gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO2);

	/* Setup GPIO pins for USART2 receive. */
	gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO3);
	gpio_set_output_options(GPIOA, GPIO_OTYPE_OD, GPIO_OSPEED_25MHZ, GPIO3);

	/* Setup USART2 TX and RX pin as alternate function. */
	gpio_set_af(GPIOA, GPIO_AF7, GPIO2);
	gpio_set_af(GPIOA, GPIO_AF7, GPIO3);

	/* Setup USART2 parameters. */
	usart_set_baudrate(USART2, 38400);
	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);

	/* Enable USART2 Receive interrupt. */
	usart_enable_rx_interrupt(USART2);

	/* Finally enable the USART. */
	usart_enable(USART2);
}
/* Set up all the peripherals */
void setup(void)
{
	rcc_clock_setup_in_hsi_out_48mhz();

	rcc_peripheral_enable_clock(&RCC_APB2ENR, 
			RCC_APB2ENR_IOPAEN |
			RCC_APB2ENR_IOPBEN |
			RCC_APB2ENR_AFIOEN | 
			RCC_APB2ENR_USART1EN);

	/* GPIO pin for the LED */
	gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_2_MHZ,
			GPIO_CNF_OUTPUT_PUSHPULL, GPIO2);

	/* GPIO pin for USART TX */
	gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ,
			GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO9);

	/* Setup USART parameters. */
	usart_set_baudrate(USART1, 19200);
	usart_set_databits(USART1, 8);
	usart_set_stopbits(USART1, USART_STOPBITS_1);
	usart_set_parity(USART1, USART_PARITY_NONE);
	usart_set_flow_control(USART1, USART_FLOWCONTROL_NONE);
	usart_set_mode(USART1, USART_MODE_TX);

	/* Finally enable the USART. */
	usart_enable(USART1);
}
static void usart_setup(void)
{
	/* Initialize output ring buffer. */
	ring_init(&output_ring, output_ring_buffer, BUFFER_SIZE);

	/* Enable the USART2 interrupt. */
	nvic_enable_irq(NVIC_USART2_IRQ);

	/* Setup GPIO pin GPIO_USART2_TX on GPIO port A for transmit. */
	gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ,
		      GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART2_TX);

	/* Setup GPIO pin GPIO_USART2_RX on GPIO port A for receive. */
	gpio_set_mode(GPIOA, GPIO_MODE_INPUT,
		      GPIO_CNF_INPUT_FLOAT, GPIO_USART2_RX);

	/* Setup UART parameters. */
	usart_set_baudrate(USART2, 230400);
	usart_set_databits(USART2, 8);
	usart_set_stopbits(USART2, USART_STOPBITS_1);
	usart_set_parity(USART2, USART_PARITY_NONE);
	usart_set_flow_control(USART2, USART_FLOWCONTROL_NONE);
	usart_set_mode(USART2, USART_MODE_TX_RX);

	/* Enable USART2 Receive interrupt. */
	USART_CR1(USART2) |= USART_CR1_RXNEIE;

	/* Finally enable the USART. */
	usart_enable(USART2);
}
Exemple #6
0
/** Initialize the USART peripheral.
 *
 * @param obj_s The serial object
 */
static void usart_init(struct serial_s *obj_s)
{
    if (obj_s->index >= USART_NUM) {
        return;
    }

    /* USART configuration */
    usart_deinit(obj_s->uart);
    usart_word_length_set(obj_s->uart, obj_s->databits);
    usart_baudrate_set(obj_s->uart, obj_s->baudrate);
    usart_stop_bit_set(obj_s->uart, obj_s->stopbits);
    usart_parity_config(obj_s->uart, obj_s->parity);
#if DEVICE_SERIAL_FC
    if (obj_s->hw_flow_ctl == USART_HWCONTROL_NONE) {
        usart_hardware_flow_cts_config(obj_s->uart, USART_CTS_DISABLE);
        usart_hardware_flow_rts_config(obj_s->uart, USART_RTS_DISABLE);
    } else if (obj_s->hw_flow_ctl == USART_HWCONTROL_RTS) {
        usart_hardware_flow_cts_config(obj_s->uart, USART_CTS_DISABLE);
        usart_hardware_flow_rts_config(obj_s->uart, USART_RTS_ENABLE);
    } else if (obj_s->hw_flow_ctl == USART_HWCONTROL_CTS) {
        usart_hardware_flow_cts_config(obj_s->uart, USART_CTS_ENABLE);
        usart_hardware_flow_rts_config(obj_s->uart, USART_RTS_DISABLE);
    } else if (obj_s->hw_flow_ctl == USART_HWCONTROL_RTS_CTS) {
        usart_hardware_flow_cts_config(obj_s->uart, USART_CTS_ENABLE);
        usart_hardware_flow_rts_config(obj_s->uart, USART_RTS_ENABLE);
    }
#endif /* DEVICE_SERIAL_FC */
    usart_receive_config(obj_s->uart, USART_RECEIVE_ENABLE);
    usart_transmit_config(obj_s->uart, USART_TRANSMIT_ENABLE);
    usart_enable(obj_s->uart);
}
Exemple #7
0
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
}
static void usart_setup(void)
{
	/* Enable the USART1 interrupt. */
	nvic_enable_irq(NVIC_USART1_IRQ);

	/* Enable USART1 pin software remapping. */
	AFIO_MAPR |= AFIO_MAPR_USART1_REMAP;

	/* Setup GPIO pin GPIO_USART1_RE_TX on GPIO port B for transmit. */
	gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ,
		      GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART1_RE_TX);

	/* Setup GPIO pin GPIO_USART1_RE_RX on GPIO port B for receive. */
	gpio_set_mode(GPIOB, GPIO_MODE_INPUT,
		      GPIO_CNF_INPUT_FLOAT, GPIO_USART1_RE_RX);

	/* Setup UART parameters. */
	usart_set_baudrate(USART1, 230400);
	usart_set_databits(USART1, 8);
	usart_set_stopbits(USART1, USART_STOPBITS_1);
	usart_set_parity(USART1, USART_PARITY_NONE);
	usart_set_flow_control(USART1, USART_FLOWCONTROL_NONE);
	usart_set_mode(USART1, USART_MODE_TX_RX);

	/* Enable USART1 Receive interrupt. */
	USART_CR1(USART1) |= USART_CR1_RXNEIE;

	/* Finally enable the USART. */
	usart_enable(USART1);
}
Exemple #9
0
void usart_init( int baudrate )
{
	gpio_set_mode(GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO_USART1_RX);
	gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART1_TX);

#ifdef BUFFERED


	buffer_reset (&u1rx);
	buffer_reset (&u1tx);

	nvic_enable_irq(NVIC_USART1_IRQ);
	usart_enable_rx_interrupt(USART1);
	usart_disable_tx_interrupt(USART1);

#endif

	// usart peripheral confguration
	usart_set_baudrate(USART1, baudrate);
	usart_set_databits(USART1, 8);
	usart_set_parity(USART1, USART_PARITY_NONE);
	usart_set_mode(USART1, USART_MODE_TX_RX);
	usart_set_stopbits(USART1, USART_STOPBITS_1);
	usart_set_flow_control(USART1, USART_FLOWCONTROL_NONE);

	usart_enable(USART1);

}
void HardwareSerial::begin(uint32 baud) {
    ASSERT(baud <= usart_device->max_baud);

    if (baud > usart_device->max_baud) {
        return;
    }

    const stm32_pin_info *txi = &PIN_MAP[tx_pin];
    const stm32_pin_info *rxi = &PIN_MAP[rx_pin];
#ifdef STM32F2
	// int af = 7<<8;
    gpio_set_af_mode(txi->gpio_device, txi->gpio_bit, 7);
    gpio_set_af_mode(rxi->gpio_device, rxi->gpio_bit, 7);
    gpio_set_mode(txi->gpio_device, txi->gpio_bit, (gpio_pin_mode)(GPIO_AF_OUTPUT_PP | GPIO_PUPD_INPUT_PU | 0x700));
    gpio_set_mode(rxi->gpio_device, rxi->gpio_bit, (gpio_pin_mode)(GPIO_MODE_AF      | GPIO_PUPD_INPUT_PU | 0x700));
    //gpio_set_mode(txi->gpio_device, txi->gpio_bit, (gpio_pin_mode)(GPIO_PUPD_INPUT_PU));
    //gpio_set_mode(rxi->gpio_device, rxi->gpio_bit, (gpio_pin_mode)(GPIO_PUPD_INPUT_PU));
#else
	gpio_set_mode(txi->gpio_device, txi->gpio_bit, GPIO_AF_OUTPUT_PP);
    gpio_set_mode(rxi->gpio_device, rxi->gpio_bit, GPIO_INPUT_FLOATING);
#endif
#if 0
    if (txi->timer_device != NULL) {
        /* Turn off any PWM if there's a conflict on this GPIO bit. */
        timer_set_mode(txi->timer_device, txi->timer_channel, TIMER_DISABLED);
    }
#endif

    usart_init(usart_device);
    usart_set_baud_rate(usart_device, baud);
    usart_enable(usart_device);
}
Exemple #11
0
//Sets USART to the EDBG virtual COM port of SAMD21 Xpro
void configure_usart(void){
	
	struct usart_config config_usart;
	usart_get_config_defaults(&config_usart);
	
	config_usart.baudrate    = 9600; //Need to find the SERCOM/pins connected to EDBG on the SAMW25
	/*
	config_usart.mux_setting = EDBG_CDC_SERCOM_MUX_SETTING; //USART_RX_1_TX_0_XCK_1
	config_usart.pinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0; //PINMUX_PA22C_SERCOM3_PAD0
	config_usart.pinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1; //PINMUX_PA23C_SERCOM3_PAD1
	config_usart.pinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2; //PINMUX_UNUSED
	config_usart.pinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3; //PINMUX_UNUSED
	
	while (usart_init(&usart_instance,
	EDBG_CDC_MODULE, &config_usart) != STATUS_OK) {}
	*/
	
	config_usart.mux_setting = USART_RX_1_TX_0_XCK_1; //USART_RX_1_TX_2_XCK_3
	config_usart.pinmux_pad0 = PINMUX_PA12C_SERCOM2_PAD0;
	config_usart.pinmux_pad1 = PINMUX_PA13C_SERCOM2_PAD1;
	config_usart.pinmux_pad2 = PINMUX_UNUSED;
	config_usart.pinmux_pad3 = PINMUX_UNUSED;
	
	while(usart_init(&usart_instance,SERCOM2, &config_usart) != STATUS_OK) {}
	
	usart_enable(&usart_instance);
}
Exemple #12
0
void usart_setup(void) {
  /* Enable clocks for GPIO port B (for GPIO_USART1_TX) and USART1. */
  rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPBEN);
  rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_AFIOEN);
  rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_USART1EN);

  gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ,
                GPIO_CNF_OUTPUT_PUSHPULL, GPIO5);
  gpio_clear(GPIOB, GPIO5);

  AFIO_MAPR |= AFIO_MAPR_USART1_REMAP;
  gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ,
                GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART1_RE_TX);
  gpio_set_mode(GPIOB, GPIO_MODE_INPUT,
                GPIO_CNF_INPUT_FLOAT, GPIO_USART1_RE_RX);


  /* Setup UART parameters. */
  usart_set_baudrate(USART1, 115200);
  usart_set_databits(USART1, 8);
  usart_set_stopbits(USART1, USART_STOPBITS_1);
  usart_set_mode(USART1, USART_MODE_TX_RX);
  usart_set_parity(USART1, USART_PARITY_NONE);
  usart_set_flow_control(USART1, USART_FLOWCONTROL_NONE);

  /* Finally enable the USART. */
  usart_enable(USART1);
}
Exemple #13
0
/**
 * \brief configure and enable SERCOM - USART
 */
void configure_usart(void)
{
	/* USART set up configuration */
	struct usart_config config_usart;
	
	/* USART base address */
	SercomUsart *const usart_hw	= SERCOM2;
	
	/* Get USART default configuration */
	usart_get_config_defaults(&config_usart);

	/* Configure USART baud rate and pad */
	config_usart.baudrate    = 460800;
	config_usart.mux_setting = EDBG_CDC_SERCOM_MUX_SETTING;
	config_usart.pinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0;
	config_usart.pinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1;
	config_usart.pinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2;
	config_usart.pinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3;

	/* Initialize USART */
	while (usart_init(&usart_instance,
	EDBG_CDC_MODULE, &config_usart) != STATUS_OK) {
	}

	/* Enable USART */
	usart_enable(&usart_instance);
	
	/* Enable USART transfer complete interrupt */
	usart_hw->INTENSET.reg	= SERCOM_USART_INTFLAG_TXC;

}
Exemple #14
0
void mew_bluetooth_init(void) {
    gpio_mode_setup(MEW_BLUETOOTH_POWER_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, MEW_BLUETOOTH_POWER_PIN);
    gpio_set_output_options(MEW_BLUETOOTH_POWER_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_100MHZ, MEW_BLUETOOTH_POWER_PIN);
    gpio_clear(MEW_BLUETOOTH_POWER_PORT, MEW_BLUETOOTH_POWER_PIN);
    
    gpio_mode_setup(MEW_BLUETOOTH_RESET_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, MEW_BLUETOOTH_RESET_PIN);
    gpio_set_output_options(MEW_BLUETOOTH_RESET_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_100MHZ, MEW_BLUETOOTH_RESET_PIN);
    gpio_set(MEW_BLUETOOTH_RESET_PORT, MEW_BLUETOOTH_RESET_PIN);
    
    gpio_mode_setup(MEW_BLUETOOTH_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, MEW_BLUETOOTH_PIN);
    gpio_set_af(MEW_BLUETOOTH_PORT, MEW_BLUETOOTH_PORT_AF, MEW_BLUETOOTH_PIN);
    
    usart_disable(MEW_BLUETOOTH_USART);
    usart_set_baudrate(MEW_BLUETOOTH_USART, MEW_BLUETOOTH_SPEED);
    usart_set_databits(MEW_BLUETOOTH_USART, 8);
    usart_set_stopbits(MEW_BLUETOOTH_USART, USART_STOPBITS_1);
    usart_set_mode(MEW_BLUETOOTH_USART, USART_MODE_TX_RX);
    usart_set_parity(MEW_BLUETOOTH_USART, USART_PARITY_NONE);
    usart_set_flow_control(MEW_BLUETOOTH_USART, USART_FLOWCONTROL_NONE);
    usart_enable_rx_interrupt(MEW_BLUETOOTH_USART);
    usart_disable_tx_interrupt(MEW_BLUETOOTH_USART);
    usart_enable_tx_dma(MEW_BLUETOOTH_USART);
    usart_enable(MEW_BLUETOOTH_USART);
    
    nvic_enable_irq(MEW_BLUETOOTH_IRQ);
    nvic_enable_irq(MEW_BLUETOOTH_DMA_NVIC_TX);
    
    memset(_mew_bt_buffer, 0, MEW_BT_RECEIVE_BUFFER_SIZE);
}
Exemple #15
0
int rc100Initialize(uint32 baudrate )
{
/*
 *  Opening device
 *  baudrate: Real baudrate (ex> 115200, 57600, 38400...)
 *  Return: 0(Failed), 1(Succeed)
 *
 * */
	gpio_set_mode(GPIOA, 2, GPIO_AF_OUTPUT_PP);
	gpio_set_mode(GPIOA, 3, GPIO_INPUT_FLOATING);
	usart_init(USART2);
	//TxDStringC("USART clock = ");TxDHex32C(STM32_PCLK2);TxDStringC("\r\n");
	usart_set_baud_rate(USART2, STM32_PCLK1, baudrate);
	usart_attach_interrupt(USART2,rc100Interrupt);
	usart_enable(USART2);

	gbRcvFlag = 0;
	gwRcvData = 0;
	gbRcvPacketNum = 0;
	/*Clear rx tx uart2 buffer */
	gbPacketWritePointer =0;
	gbPacketReadPointer=0;

	return 1;
}
Exemple #16
0
//! [setup]
void configure_usart(void)
{
//! [setup_config]
	struct usart_config config_usart;
//! [setup_config]
//! [setup_config_defaults]
	usart_get_config_defaults(&config_usart);
//! [setup_config_defaults]

//! [setup_change_config]
	config_usart.baudrate    = 9600;
	config_usart.mux_setting = EDBG_CDC_SERCOM_MUX_SETTING;
	config_usart.pinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0;
	config_usart.pinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1;
	config_usart.pinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2;
	config_usart.pinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3;
//! [setup_change_config]

//! [setup_set_config]
	while (usart_init(&usart_instance,
			EDBG_CDC_MODULE, &config_usart) != STATUS_OK) {
	}
//! [setup_set_config]

//! [setup_enable]
	usart_enable(&usart_instance);
//! [setup_enable]
}
Exemple #17
0
/******************************************************************************
Initialize the hardware to receive (USART based) CAN messages and start the
timer for the CANopen stack.
INPUT	bitrate		bitrate in kilobit (fixed at 115200)
OUTPUT	1 if successful	
******************************************************************************/
unsigned char canInit(unsigned int bitrate)
{
	msg_recv_status = 0;
	msg_received = 0;
/* Enable clocks for GPIO port A (for GPIO_USART1_TX) and USART1. */
	rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN |
				    RCC_APB2ENR_AFIOEN | RCC_APB2ENR_USART1EN);
/* Enable the USART1 interrupt. */
	nvic_enable_irq(NVIC_USART1_IRQ);
/* Setup GPIO pin GPIO_USART1_RE_TX on GPIO port A for transmit. */
	gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ,
		      GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART1_TX);
/* Setup GPIO pin GPIO_USART1_RE_RX on GPIO port A for receive. */
	gpio_set_mode(GPIOA, GPIO_MODE_INPUT,
		      GPIO_CNF_INPUT_FLOAT, GPIO_USART1_RX);
/* Setup USART parameters. */
	usart_set_baudrate(USART1, 115200);
	usart_set_databits(USART1, 8);
	usart_set_stopbits(USART1, USART_STOPBITS_1);
	usart_set_parity(USART1, USART_PARITY_NONE);
	usart_set_flow_control(USART1, USART_FLOWCONTROL_NONE);
	usart_set_mode(USART1, USART_MODE_TX_RX);
/* Enable USART1 receive interrupts. */
	usart_enable_rx_interrupt(USART1);
	usart_disable_tx_interrupt(USART1);
/* Finally enable the USART. */
	usart_enable(USART1);

/* Initialise the send and receive buffers */
	buffer_init(send_buffer,BUFFER_SIZE);
	buffer_init(receive_buffer,BUFFER_SIZE);

 	return 1;
}
Exemple #18
0
void usart_setup(void)
{
    /* Setup clock */
    /* Enable GPIOA clock for USART. */
    rcc_peripheral_enable_clock(&RCC_AHB1ENR, RCC_AHB1ENR_IOPAEN);
    
    /* Enable clocks for USART2. */
    rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_USART2EN);
    
    /* Setup GPIO pins for USART2 transmit. */
    gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO2);
    
    /* Setup USART2 TX pin as alternate function. */
    gpio_set_af(GPIOA, GPIO_AF7, GPIO2);
    
    /* Setup USART2 parameters. */
    usart_set_baudrate(USART2, 115200);
    usart_set_databits(USART2, 8);
    usart_set_stopbits(USART2, USART_STOPBITS_1);
    usart_set_mode(USART2, USART_MODE_TX);
    usart_set_parity(USART2, USART_PARITY_NONE);
    usart_set_flow_control(USART2, USART_FLOWCONTROL_NONE);
    
    /* Finally enable the USART. */
    usart_enable(USART2);
}
Exemple #19
0
void usart_setup(void)
{
	/* Enable all required USART modules */
	rcc_periph_clock_enable(USART_RCC_PORT);
	rcc_periph_clock_enable(USART_RCC_ID);

	/* Setup GPIO pins for USART transmit. */
	gpio_mode_setup(USART_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, USART_RX);
	gpio_mode_setup(USART_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, USART_TX);
//	gpio_set_output_options(USART_PORT, GPIO_OTYPE_OD, GPIO_OSPEED_25MHZ, USART_TX);

	/* Setup USART TX pin as alternate function. */
	gpio_set_af(USART_PORT, GPIO_AF7, USART_RX);
	gpio_set_af(USART_PORT, GPIO_AF7, USART_TX);

	/* Setup USART parameters. */
	usart_set_baudrate(USART_ID, USART_BAUD_RATE);
	usart_set_databits(USART_ID, 8);
	usart_set_stopbits(USART_ID, USART_STOPBITS_1);
	usart_set_mode(USART_ID, USART_MODE_TX_RX);
	usart_set_parity(USART_ID, USART_PARITY_NONE);
	usart_set_flow_control(USART_ID, USART_FLOWCONTROL_NONE);

	/* Finally enable the USART. */
	usart_enable(USART_ID);
}
/* === IMPLEMENTATION ====================================================== */
static inline void usart_configure_flowcontrol(void)
{
	struct usart_config config_usart;
#if UART_FLOWCONTROL_6WIRE_MODE == true
	usart_disable(&usart_instance);
	usart_reset(&usart_instance);
#endif
	usart_get_config_defaults(&config_usart);

	config_usart.baudrate = CONF_FLCR_BLE_BAUDRATE;
	config_usart.generator_source = CONF_FLCR_BLE_UART_CLOCK;
	config_usart.mux_setting = CONF_FLCR_BLE_MUX_SETTING;
	config_usart.pinmux_pad0 = CONF_FLCR_BLE_PINMUX_PAD0;
	config_usart.pinmux_pad1 = CONF_FLCR_BLE_PINMUX_PAD1;
	config_usart.pinmux_pad2 = CONF_FLCR_BLE_PINMUX_PAD2;
	config_usart.pinmux_pad3 = CONF_FLCR_BLE_PINMUX_PAD3;

	while (usart_init(&usart_instance, CONF_FLCR_BLE_USART_MODULE, &config_usart) != STATUS_OK);

	usart_enable(&usart_instance);
	
	/* register and enable usart callbacks */
	usart_register_callback(&usart_instance,
	serial_drv_read_cb, USART_CALLBACK_BUFFER_RECEIVED);
	usart_register_callback(&usart_instance,
	serial_drv_write_cb, USART_CALLBACK_BUFFER_TRANSMITTED);
	usart_enable_callback(&usart_instance, USART_CALLBACK_BUFFER_RECEIVED);
	usart_enable_callback(&usart_instance, USART_CALLBACK_BUFFER_TRANSMITTED);
	serial_read_byte(&rx_data);
}
Exemple #21
0
/** Configure the format. Set the number of bits, parity and the number of stop bits
 *
 * @param obj       The serial object
 * @param data_bits The number of data bits
 * @param parity    The parity
 * @param stop_bits The number of stop bits
 */
void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits)
{
    uint16_t uen_flag = 0U;
    struct serial_s *p_obj = GET_SERIAL_S(obj);

    /* store the UEN flag */
    uen_flag = USART_CTL0(p_obj->uart) & USART_CTL0_UEN;

    /* disable the UART clock first */
    usart_disable(p_obj->uart);

    /* configurate the UART parity */
    switch (parity) {
        case ParityOdd:
            p_obj->parity = USART_PM_ODD;
            usart_parity_config(p_obj->uart, USART_PM_ODD);
            break;

        case ParityEven:
            p_obj->parity = USART_PM_EVEN;
            usart_parity_config(p_obj->uart, USART_PM_EVEN);
            break;

        case ParityForced0:
        case ParityForced1:
        default:
            p_obj->parity = USART_PM_NONE;
            usart_parity_config(p_obj->uart, USART_PM_NONE);
            break;
    }

    if (p_obj->parity == USART_PM_NONE) {
        if (data_bits == 9) {
            usart_word_length_set(p_obj->uart, USART_WL_9BIT);
        } else if (data_bits == 8) {
            usart_word_length_set(p_obj->uart, USART_WL_8BIT);
        } else if (data_bits == 7) {
            return;
        }
    } else {
        if (data_bits == 9) {
            return;
        } else if (data_bits == 8) {
            usart_word_length_set(p_obj->uart, USART_WL_9BIT);
        } else if (data_bits == 7) {
            usart_word_length_set(p_obj->uart, USART_WL_8BIT);
        }
    }

    if (stop_bits == 2) {
        usart_stop_bit_set(p_obj->uart, USART_STB_2BIT);
    } else {
        usart_stop_bit_set(p_obj->uart, USART_STB_1BIT);
    }

    /* restore the UEN flag */
    if (RESET != uen_flag) {
        usart_enable(p_obj->uart);
    }
}
uint8_t configure_serial_drv(void)
{
	#if UART_FLOWCONTROL_4WIRE_MODE == true
		usart_configure_flowcontrol();
		#warning "This mode works only if Flow Control Permanently Enabled in the BTLC1000"
	#else
	struct usart_config config_usart;
	usart_get_config_defaults(&config_usart);
	config_usart.baudrate = CONF_BLE_BAUDRATE;
	config_usart.generator_source = CONF_BLE_UART_CLOCK;
	config_usart.mux_setting = CONF_BLE_MUX_SETTING;
	config_usart.pinmux_pad0 = CONF_BLE_PINMUX_PAD0;
	config_usart.pinmux_pad1 = CONF_BLE_PINMUX_PAD1;
	config_usart.pinmux_pad2 = CONF_BLE_PINMUX_PAD2;
	config_usart.pinmux_pad3 = CONF_BLE_PINMUX_PAD3;

	while (usart_init(&usart_instance, CONF_BLE_USART_MODULE, &config_usart) != STATUS_OK);

	usart_enable(&usart_instance);
	
	/* register and enable usart callbacks */
	usart_register_callback(&usart_instance,
		serial_drv_read_cb, USART_CALLBACK_BUFFER_RECEIVED);
	usart_register_callback(&usart_instance,
		serial_drv_write_cb, USART_CALLBACK_BUFFER_TRANSMITTED);
	usart_enable_callback(&usart_instance, USART_CALLBACK_BUFFER_RECEIVED);
	usart_enable_callback(&usart_instance, USART_CALLBACK_BUFFER_TRANSMITTED);
	serial_read_byte(&rx_data);
	#endif
	
	return STATUS_OK;
}
Exemple #23
0
void usart_setup(void)
{
/* Enable clocks for GPIO port A (for GPIO_USART1_TX) and USART1. */
    rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN |
                    RCC_APB2ENR_AFIOEN | RCC_APB2ENR_USART1EN);
/* Enable the USART1 interrupt. */
    nvic_enable_irq(NVIC_USART1_IRQ);
/* Setup GPIO pin GPIO_USART1_RE_TX on GPIO port A for transmit. */
    gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ,
              GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART1_TX);
/* Setup GPIO pin GPIO_USART1_RE_RX on GPIO port A for receive. */
    gpio_set_mode(GPIOA, GPIO_MODE_INPUT,
              GPIO_CNF_INPUT_FLOAT, GPIO_USART1_RX);
/* Setup UART parameters. */
    usart_set_baudrate(USART1, 115200);
    usart_set_databits(USART1, 8);
    usart_set_stopbits(USART1, USART_STOPBITS_1);
    usart_set_parity(USART1, USART_PARITY_NONE);
    usart_set_flow_control(USART1, USART_FLOWCONTROL_NONE);
    usart_set_mode(USART1, USART_MODE_TX_RX);
/* Enable USART1 receive interrupts. */
    usart_enable_rx_interrupt(USART1);
    usart_disable_tx_interrupt(USART1);
/* Finally enable the USART. */
    usart_enable(USART1);
}
Exemple #24
0
int stm32_test_usart(int id)
{
	int ret = -1;
	struct usart_data usart_data;
	uint8_t data[] = {'A', 'B', 'C', 'D', 'E', 'F' };

	usart_data.data = &data[0];
	usart_data.size = sizeof(data);

	/* set USART parameters */
	usart_set_word_length(id, 8);
	usart_set_baud_rate(id, 57600);
	usart_set_stop_bit(id, 1);

	/* enable USART2 */
	usart_enable(id);

	usart_start_tx(id, &usart_data);

	/* Disable USART2 */
	usart_disable(id);

	ret = 0;
	return ret;
}
Exemple #25
0
otError otPlatUartEnable(void)
{
    struct usart_config configUsart;

    usart_get_config_defaults(&configUsart);

    configUsart.baudrate    = 115200;
    configUsart.mux_setting = UART_SERCOM_MUX_SETTING;
    configUsart.pinmux_pad0 = UART_SERCOM_PINMUX_PAD0;
    configUsart.pinmux_pad1 = UART_SERCOM_PINMUX_PAD1;
    configUsart.pinmux_pad2 = UART_SERCOM_PINMUX_PAD2;
    configUsart.pinmux_pad3 = UART_SERCOM_PINMUX_PAD3;

    while (usart_init(&sUsartInstance, UART_SERCOM_MODULE, &configUsart) != STATUS_OK)
        ;

    usart_enable(&sUsartInstance);

    sReceive.mHead = 0;
    sReceive.mTail = 0;

    usart_register_callback(&sUsartInstance, usartWriteCallback, USART_CALLBACK_BUFFER_TRANSMITTED);
    usart_register_callback(&sUsartInstance, usartReadCallback, USART_CALLBACK_BUFFER_RECEIVED);

    usart_enable_callback(&sUsartInstance, USART_CALLBACK_BUFFER_TRANSMITTED);
    usart_enable_callback(&sUsartInstance, USART_CALLBACK_BUFFER_RECEIVED);

    usart_read_job(&sUsartInstance, (uint16_t *)&sReceive.mBuffer[sReceive.mTail]);

    return OT_ERROR_NONE;
}
Exemple #26
0
void bluetooth_init(void)
{
	rcc_periph_clock_enable(RCC_B_WAKE_SW_PORT);
	rcc_periph_clock_enable(RCC_B_WAKE_HW_PORT);
	rcc_periph_clock_enable(RCC_B_CMD_PORT);


	gpio_mode_setup(B_WAKE_SW_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, B_WAKE_SW_PIN);
	gpio_mode_setup(B_WAKE_HW_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, B_WAKE_HW_PIN);
	gpio_mode_setup(B_CMD_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, B_CMD_PIN);

	//uart

	rcc_periph_clock_enable(RCC_B_USART);
	gpio_mode_setup(B_USART_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, B_USART_TX_PIN|B_USART_RX_PIN);
	gpio_set_af(B_USART_PORT, GPIO_AF1, B_USART_TX_PIN|B_USART_RX_PIN);
	usart_set_baudrate(B_USART, 115200);//9600 );
	usart_set_databits(B_USART, 8);
	usart_set_stopbits(B_USART, USART_CR2_STOP_1_0BIT);
	usart_set_mode(B_USART, USART_MODE_TX_RX);
	usart_set_parity(B_USART, USART_PARITY_NONE);
	usart_set_flow_control(B_USART, USART_FLOWCONTROL_NONE);
	usart_enable(B_USART);

}
Exemple #27
0
/*---------------------------------------------------------------------------*/
static int
open(int32_t baudrate, uart_rx_char_callback char_cb, uart_rx_frame_callback frame_cb)
{
  struct usart_config config_usart;
  struct port_config pin_conf;

  usart_get_config_defaults(&config_usart);

  config_usart.baudrate    = baudrate;
  config_usart.mux_setting = RS485_SERCOM_MUX_SETTING;
  config_usart.pinmux_pad0 = RS485_SERCOM_PINMUX_PAD0;
  config_usart.pinmux_pad1 = RS485_SERCOM_PINMUX_PAD1;
  config_usart.pinmux_pad2 = RS485_SERCOM_PINMUX_PAD2;
  config_usart.pinmux_pad3 = RS485_SERCOM_PINMUX_PAD3;

  while (usart_init(&usart_instance, RS485_MODULE, &config_usart) != STATUS_OK) {}

  usart_enable(&usart_instance);

  port_get_config_defaults(&pin_conf);
  pin_conf.direction = PORT_PIN_DIR_OUTPUT;
  port_pin_set_config(RS485_TXE, &pin_conf);
  port_pin_set_output_level(RS485_TXE, false);

  char_callback = char_cb;

  usart_register_callback(&usart_instance,
                          usart_read_callback, USART_CALLBACK_BUFFER_RECEIVED);

  usart_enable_callback(&usart_instance, USART_CALLBACK_BUFFER_RECEIVED);

  usart_read_job(&usart_instance, &rx_char);

  return 1;
}
Exemple #28
0
void dbg_serial_init(void)
{
    dbg_fifo_flush( &usart_rx_buf );
    dbg_fifo_flush( &usart_tx_buf );

    rcc_periph_clock_enable(RCC_AFIO);
    rcc_periph_clock_enable(DBG_USART_PERIPH);
    rcc_periph_clock_enable(DBG_USART_TX_PERIPH);
#if (DBG_USART_RX_PERIPH != DBG_USART_TX_PERIPH)
    rcc_periph_clock_enable(DBG_USART_RX_PERIPH);
#endif
    /* Enable the DBG_USART interrupt. */
    nvic_enable_irq(DBG_USART_VECTOR);

    /* Setup GPIO pins for USART transmit. */
    gpio_set_mode(DBG_USART_TX_PORT, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, DBG_USART_TX_PIN);

    /* Setup GPIO pins for USART receive. */
    gpio_set_mode(DBG_USART_RX_PORT, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, DBG_USART_RX_PIN);
    gpio_set(DBG_USART_RX_PORT, DBG_USART_RX_PIN);

    /* Setup USART parameters. */
    usart_set_baudrate    (DBG_USART, 57600);
    usart_set_databits    (DBG_USART, 8);
    usart_set_stopbits    (DBG_USART, USART_STOPBITS_1);
    usart_set_mode        (DBG_USART, USART_MODE_TX_RX);
    usart_set_parity      (DBG_USART, USART_PARITY_NONE);
    usart_set_flow_control(DBG_USART, USART_FLOWCONTROL_NONE);
    /* Enable USART Receive interrupt. */
    usart_enable_rx_interrupt(DBG_USART);

    /* Finally enable the USART. */
    usart_enable(DBG_USART);
}
Exemple #29
0
void usart_init(void) {
    msg_targets = 0;
    msg_state_reset(&msg_state_soc);
    msg_state_reset(&msg_state_display);
    usart_enable(USART_DISPLAY|USART_SOC);
    usart_lowlevel_init();
}
Exemple #30
0
/**
 * \brief Configure UART console.
 */
static void configure_console(void)
{
#if SAMD21
	struct usart_config usart_conf;

	usart_get_config_defaults(&usart_conf);
	usart_conf.mux_setting = CONF_STDIO_MUX_SETTING;
	usart_conf.pinmux_pad0 = CONF_STDIO_PINMUX_PAD0;
	usart_conf.pinmux_pad1 = CONF_STDIO_PINMUX_PAD1;
	usart_conf.pinmux_pad2 = CONF_STDIO_PINMUX_PAD2;
	usart_conf.pinmux_pad3 = CONF_STDIO_PINMUX_PAD3;
	usart_conf.baudrate    = CONF_STDIO_BAUDRATE;

	stdio_serial_init(&cdc_uart_module, CONF_STDIO_USART_MODULE, &usart_conf);
	usart_enable(&cdc_uart_module);
#elif SAME70
	const usart_serial_options_t uart_serial_options = {
		.baudrate = CONF_UART_BAUDRATE,
#ifdef CONF_UART_CHAR_LENGTH
		.charlength = CONF_UART_CHAR_LENGTH,
#endif
		.paritytype = CONF_UART_PARITY,
#ifdef CONF_UART_STOP_BITS
		.stopbits = CONF_UART_STOP_BITS,
#endif
	};

	/* Configure console UART. */
	sysclk_enable_peripheral_clock(CONSOLE_UART_ID);
	stdio_serial_init(CONF_UART, &uart_serial_options);
#endif
}