Пример #1
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;
}
Пример #2
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);
}
Пример #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);
}
Пример #4
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);
}
Пример #5
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);
}
Пример #6
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);
}
Пример #7
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);

}
Пример #8
0
/* ----------------------- Enable USART interrupts -----------------------------*/
void
vMBPortSerialEnable( BOOL xRxEnable, BOOL xTxEnable )
{
    /* If xRXEnable enable serial receive interrupts. If xTxENable enable
     * transmitter empty interrupts.
     */
    if (xRxEnable)
    {
        txen = false;
        usart_enable_rx_interrupt(MB_USART);
    }
    else
    {
        usart_disable_rx_interrupt(MB_USART);
    }

    if (xTxEnable)
    {
        txen = true;
        gpio_set(MB_USART_TXEN_PORT, MB_USART_TXEN_PIN);

        usart_enable_tx_interrupt(MB_USART);
    }
    else
    {
        txen = false;
        usart_disable_tx_interrupt(MB_USART);
    }
}
Пример #9
0
/* Configure USART2 as a 38400, 8N1 serial port that has an interrupt
 * driven receive function (transmit is blocking)
 */
void uart_setup(int baud)
{
    /* Setup GPIO pins for USART2 transmit. */
    gpio_mode_setup(GPIOC, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO6);
    gpio_mode_setup(GPIOC, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO7);

    gpio_set_output_options(GPIOC, GPIO_OTYPE_OD, GPIO_OSPEED_25MHZ, GPIO7);

    gpio_set_af(GPIOC, GPIO_AF8, GPIO6);
    gpio_set_af(GPIOC, GPIO_AF8, GPIO7);

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

    /* Allow for receive interrupts */
    buf_ndx = 0;
    read_ndx = 0;
    nvic_enable_irq(NVIC_USART6_IRQ);

    /* Finally enable the USART. */
    usart_enable(USART6);
    usart_enable_rx_interrupt(USART6);
    /* 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);

    /* Allow for receive interrupts */
    buf_ndx = 0;
    read_ndx = 0;
    nvic_enable_irq(NVIC_USART2_IRQ);

    /* Finally enable the USART. */
    usart_enable(USART2);
    usart_enable_rx_interrupt(USART2);
}
Пример #10
0
void serial_initialize(u32 baud)
{
	usart_set_baudrate(USART, baud);
	usart_set_databits(USART, 8);
	usart_set_stopbits(USART, USART_STOPBITS_1);
	usart_set_parity(USART, USART_PARITY_NONE);
	usart_set_flow_control(USART, USART_FLOWCONTROL_NONE);
	usart_enable(USART);
	usart_enable_rx_interrupt(USART);
	//usart_enable_tx_interrupt(USART);
}
Пример #11
0
/* Enable serial data reception by providing a callback function
   to accept received character and status. Disable by calling with argument NULL.
   Callback executes in interrupt context and must be short.
*/
void UART_StartReceive(usart_callback_t *isr_callback)
{
    rx_callback = isr_callback;

    if (isr_callback) {
        nvic_enable_irq(get_nvic_irq(UART_CFG.uart));
        usart_enable_rx_interrupt(UART_CFG.uart);
    } else {
        usart_disable_rx_interrupt(UART_CFG.uart);
        nvic_disable_irq(get_nvic_irq(UART_CFG.uart));
    }
}
Пример #12
0
void usart_init(uint32_t arg_usart, uint32_t baudrate)
{
	usart_set_baudrate(arg_usart, baudrate);
	usart_set_databits(arg_usart, 8);
	usart_set_flow_control(arg_usart, USART_FLOWCONTROL_NONE);
	usart_set_mode(arg_usart, USART_MODE_TX | USART_MODE_RX);
	usart_set_parity(arg_usart, USART_PARITY_NONE);
	usart_set_stopbits(arg_usart, USART_STOPBITS_1);

	usart_enable_rx_interrupt(arg_usart);
	usart_enable(arg_usart);
	usart = arg_usart;
}
Пример #13
0
void usart_init(void)
{
  gpio_set_af(GPIOA, GPIO_AF7, GPIO9 | GPIO10);
  gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO9 | GPIO10);
  rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_USART1EN);

  usart_enable(USART1);
  usart_set_databits(USART1, 8);
  usart_set_stopbits(USART1, USART_STOPBITS_1);
  usart_set_parity(USART1, USART_PARITY_NONE);
  usart_set_mode(USART1, USART_MODE_TX_RX);
  usart_set_baudrate(USART1, 9600);
  usart_enable_rx_interrupt(USART1);
  nvic_enable_irq(NVIC_USART1_IRQ);
}
Пример #14
0
void
Board_FY20AP::com_init(unsigned speed)
{
	/* configure UART */
	usart_set_baudrate(USART1, speed);
	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 receive interrupt */
	usart_enable_rx_interrupt(USART1);

	/* and enable the UART */
	usart_enable(USART1);
}
Пример #15
0
/*
 * Set up the GPIO subsystem with an "Alternate Function"
 * on some of the pins, in this case connected to a
 * USART.
 */
void console_setup(void)
{

	/* MUST enable the GPIO clock in ADDITION to the USART clock */
	rcc_periph_clock_enable(RCC_GPIOA);

	/* This example uses PD9 and PD10 for Tx and Rx respectively
	 * but other pins are available for this role on USART1 (our chosen
	 * USART) as well, we are using them because they are connected over
	 * jumpers to the programmer.
	 */
	gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO9 | GPIO10);

	/* Actual Alternate function number (in this case 7) is part
	 * depenedent, check the data sheet for the right number to
	 * use.
	 */
	gpio_set_af(GPIOA, GPIO_AF7, GPIO9 | GPIO10);


	/* This then enables the clock to the USART1 peripheral which is
	 * attached inside the chip to the APB1 bus. Different peripherals
	 * attach to different buses, and even some UARTS are attached to
	 * APB1 and some to APB2, again the data sheet is useful here.
	 * We are using the rcc_periph_clock_enable function that knows which
	 * peripheral is on which clock bus and sets things up accordingly.
	 */
	rcc_periph_clock_enable(RCC_USART1);

	/* Set up USART/UART parameters using the libopencm3 helper functions */
	usart_set_baudrate(CONSOLE_UART, 115200);
	usart_set_databits(CONSOLE_UART, 8);
	usart_set_stopbits(CONSOLE_UART, USART_STOPBITS_1);
	usart_set_mode(CONSOLE_UART, USART_MODE_TX_RX);
	usart_set_parity(CONSOLE_UART, USART_PARITY_NONE);
	usart_set_flow_control(CONSOLE_UART, USART_FLOWCONTROL_NONE);
	usart_enable(CONSOLE_UART);

	/* Enable interrupts from the USART */
	nvic_enable_irq(NVIC_USART1_IRQ);

	/* Specifically enable recieve interrupts */
	usart_enable_rx_interrupt(CONSOLE_UART);
}
Пример #16
0
/*
 * console_setup(int baudrate)
 *
 * Set the pins and clocks to create a console that we can
 * use for serial messages and getting text from the user.
 */
void console_setup(int baud)
{
	/* MUST enable the GPIO clock in ADDITION to the USART clock */
	rcc_periph_clock_enable(RCC_GPIOA);

	/* This example uses PD5 and PD6 for Tx and Rx respectively
	 * but other pins are available for this role on USART1 (our chosen
	 * USART) as well, such as PA2 and PA3. You can also split them
	 * so PA2 for Tx, PD6 for Rx but you would have to enable both
	 * the GPIOA and GPIOD clocks in that case
	 */
	gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO9 | GPIO10);

	/* Actual Alternate function number (in this case 7) is part
	 * depenedent, CHECK THE DATA SHEET for the right number to
	 * use.
	 */
	gpio_set_af(GPIOA, GPIO_AF7, GPIO9 | GPIO10);


	/* This then enables the clock to the USART1 peripheral which is
	 * attached inside the chip to the APB2 bus. Different peripherals
	 * attach to different buses, and even some UARTS are attached to
	 * APB1 and some to APB2, again the data sheet is useful here.
	 */
	rcc_periph_clock_enable(RCC_USART1);

	/* Set up USART/UART parameters using the libopencm3 helper functions */
	usart_set_baudrate(CONSOLE_UART, baud);
	usart_set_databits(CONSOLE_UART, 8);
	usart_set_stopbits(CONSOLE_UART, USART_STOPBITS_1);
	usart_set_mode(CONSOLE_UART, USART_MODE_TX_RX);
	usart_set_parity(CONSOLE_UART, USART_PARITY_NONE);
	usart_set_flow_control(CONSOLE_UART, USART_FLOWCONTROL_NONE);
	usart_enable(CONSOLE_UART);

	/* Enable interrupts from the USART */
	nvic_enable_irq(NVIC_USART1_IRQ);

	/* Specifically enable receive interrupts */
	usart_enable_rx_interrupt(CONSOLE_UART);
}
Пример #17
0
void
usart_init(int usart, int irq, int baudrate, int over8) {
  /* Setup USART parameters. */
  nvic_disable_irq(irq);
  usart_disable_rx_interrupt(usart);
  usart_disable_tx_interrupt(usart);
  usart_disable(usart);
  USART_CR1(usart) |= over8;  /* This doubles the listed baudrate. */
  usart_set_baudrate(usart, baudrate);
  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);
  /* Finally enable the USART. */
  usart_enable(usart);
  usart_enable_rx_interrupt(usart);
  usart_enable_tx_interrupt(usart);
  nvic_enable_irq(irq);
}
Пример #18
0
void uart_init(int baud)
{
	uart_init_gpio();

	nvic_enable_irq(NVIC_USART1_IRQ);
	nvic_set_priority(NVIC_USART1_IRQ, 2);

	usart_set_baudrate(USART1, baud);
	usart_set_databits(USART1, 8);
	usart_set_parity(USART1, USART_PARITY_NONE);
	usart_set_flow_control(USART1, USART_FLOWCONTROL_NONE);
	usart_set_stopbits(USART1, USART_CR2_STOPBITS_1);
	usart_set_mode(USART1, USART_MODE_TX_RX);
	usart_enable_rx_interrupt(USART1);
	usart_enable(USART1);

	/* This ensures stdio doesn't use its own buffers */
	setvbuf(stdin, NULL, _IONBF, 0);
	setvbuf(stdout, NULL, _IONBF, 0);
}
Пример #19
0
void BT_Initialize()
{
    /* Enable clocks for GPIO port A (for GPIO_USART1_TX) and USART1. */
    rcc_periph_clock_enable(RCC_GPIOA);
#if DISCOVERY
    rcc_periph_clock_enable(RCC_USART3);
    rcc_periph_clock_enable(RCC_GPIOC);
    nvic_enable_irq(NVIC_USART3_4_IRQ);
#else
    rcc_periph_clock_enable(RCC_USART2);
    nvic_enable_irq(NVIC_USART2_IRQ);
#endif

    PORT_mode_setup(BT_STATE, GPIO_MODE_INPUT, GPIO_PUPD_PULLDOWN);
    PORT_mode_setup(BT_KEY, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE);
    PORT_pin_clear(BT_KEY);

    /* Setup GPIO pin for Tx/Rx */
    PORT_mode_setup(BT_TX, GPIO_MODE_AF, GPIO_PUPD_NONE);
    PORT_mode_setup(BT_RX, GPIO_MODE_AF, GPIO_PUPD_NONE);
    gpio_set_output_options(BT_RX.port, GPIO_OTYPE_OD, GPIO_OSPEED_25MHZ, BT_RX.pin);
    gpio_set_af(BT_TX.port, GPIO_AF1, BT_TX.pin | BT_RX.pin);

    /* Setup UART parameters. */
    usart_set_baudrate(BTUART, 9600);
    usart_set_databits(BTUART, 8);
    usart_set_stopbits(BTUART, USART_CR2_STOP_1_0BIT);
    usart_set_mode(BTUART, USART_MODE_TX_RX);
    usart_set_parity(BTUART, USART_PARITY_NONE);
    usart_set_flow_control(BTUART, USART_FLOWCONTROL_NONE);
    usart_enable_rx_interrupt(BTUART);

    BT_ResetPtr();
    state = 0;
    /* Finally enable the USART. */
    usart_enable(BTUART);
}
Пример #20
0
void usart_setup(void)
{
	usart_clock_setup();
	usart_gpio_setup();

	nvic_enable_irq(NVIC_USART2_IRQ);

	/* 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_MODE_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);

    /* Disable stupid output buffers. */
	setbuf(stdout, NULL);
}
Пример #21
0
/**
 *  \brief getting-started Application entry point.
 *
 *  \return Unused (ANSI-C compatibility).
*/
int main(void)
{
    struct port_config pin;
    unsigned char c;

    system_init();

    //Configure UART console.
    configure_console();

    configure_usart_callbacks();
    usart_enable_rx_interrupt(&cdc_uart_module,&c);

    usart_enable(&cdc_uart_module);
    //Configures  TC driver
    configure_tc();

    //Configures TC callback
    configure_tc_callbacks();

    //Initialize the delay driver
    delay_init();

    //Enable system interrupt
    system_interrupt_enable_global();

    //Configures PORT for LED0
    port_get_config_defaults(&pin);
    pin.direction = PORT_PIN_DIR_OUTPUT;
    port_pin_set_config(LED0_PIN, &pin);

    port_pin_set_output_level(LED0_PIN, LED0_INACTIVE);
    port_pin_set_output_level(LED0_PIN, LED0_INACTIVE);

    /*main loop*/
    while(1)
    {
        if (is_running)
        {
            //Handle user's input
            //		if (uart_getc(&c))
            //		{
            switch (c)
            {
            case 'w':
            case ' ':
                //ROTATE
                tetris_rotate();
                break;
            case 's':
                //DOWN
                tetris_gravity();
                break;
            case 'd':
                //RIGHT
                tetris_move_right();
                break;
            case 'a':
                //LEFT
                tetris_move_left();
                break;
            default:
                break;
            }
            c=0;
            //		}
            // was here if(!iterate_game)
            if(iterate_game)
            {
                //Update game
                iterate_game = false;
                tetris_gravity();
                tetris_check_lines();
                terminal_cursor_home();
                tetris_print();
                if (tetris_is_game_over())
                {
                    is_running = false;
                }
            }
        }
        else
        {
            //	if (uart_getc(&c))
            //	{
            if (c == 'n')
            {
                c=0;
                //Seed random function so we do not get same start condition
                //for each new game. In essence we will not start a new game
                //exactly at the same time.
                srand(tick);

                //New Game
                is_running = true;
                terminal_cursor_off();
                terminal_clear();
                tetris_init();
                tetris_new_block();
                terminal_cursor_home();
                tetris_print();
            }
            //	}
        }
    }
}
USART_ext::USART_ext(USART_Struct usart, USART_Settings settings, RoundBuffer rb_in_size, RoundBuffer rb_out_size)
{
	rb_in = new RoundBuffer(rb_in_size);
	rb_out = new RoundBuffer(rb_out_size);

	if (usart.rx.pin)
	{
		GPIO_ext rx(usart.rx);
		rx.mode_setup(GPIO_CPP_Extension::Mode::ALTERNATE_FUNCTION, GPIO_CPP_Extension::PullMode::NO_PULL);
		rx.set_output_options(GPIO_CPP_Extension::OutputType::PUSH_PULL, GPIO_CPP_Extension::Speed::MEDIUM_25MHz);

		if ((usart.number >= 1) && (usart.number <= 3)) {
			rx.set_af(GPIO_CPP_Extension::AF_Number::AF7);
		}
		if ((usart.number >= 4) && (usart.number <= 6)) {
			rx.set_af(GPIO_CPP_Extension::AF_Number::AF8);
		}
	}

	if (usart.tx.pin)
	{
		GPIO_ext tx(usart.tx);
		tx.mode_setup(GPIO_CPP_Extension::Mode::ALTERNATE_FUNCTION, GPIO_CPP_Extension::PullMode::NO_PULL);
		tx.set_output_options(GPIO_CPP_Extension::OutputType::PUSH_PULL, GPIO_CPP_Extension::Speed::MEDIUM_25MHz);
		if ((usart.number >= 1) && (usart.number <= 3)) {
			tx.set_af(GPIO_CPP_Extension::AF_Number::AF7);
		}
		if ((usart.number >= 4) && (usart.number <= 6)) {
			tx.set_af(GPIO_CPP_Extension::AF_Number::AF8);
		}
	}

	switch (usart.number)
	{
		case 1:
		    _usart = USART1;
			_usart_nvic = NVIC_USART1_IRQ;
        break;
		case 2:
		    _usart = USART2;
		    _usart_nvic = NVIC_USART2_IRQ;
        break;
		case 3:
		    _usart = USART3;
		    _usart_nvic = NVIC_USART3_IRQ;
        break;
		case 4:
		    _usart = UART4;
		    _usart_nvic = NVIC_UART4_IRQ;
        break;
		case 5:
		    _usart = UART5;
		    _usart_nvic = NVIC_UART5_IRQ;
        break;
		case 6:
		    _usart = USART6;
		    _usart_nvic = NVIC_USART6_IRQ;
        break;
	}

	// USART config
	usart_set_baudrate(_usart, settings.baud_rate);
	usart_set_databits(_usart, settings.word_length);
	usart_set_stopbits(_usart, settings.stop_bits);
	usart_set_mode(_usart, settings.mode);
	usart_set_parity(_usart, settings.parity);
	usart_set_flow_control(_usart, settings.flow_control);

	if (settings.mode & USART_MODE_RX)
		usart_enable_rx_interrupt(_usart);

	usart_enable(_usart);

    // NVIC config
    nvic_set_priority(_usart_nvic, settings.nvic_priority);
    nvic_enable_irq(_usart_nvic);
}
Пример #23
0
bool Serial_stm32::init(void)
{
    // Enable the USART interrupt
    switch (config_.device)
    {
        case SERIAL_STM32_1:
            rcc_periph_clock_enable(RCC_USART1);
            nvic_enable_irq(NVIC_USART1_IRQ);
            handlers_[0] = this;
            break;

        case SERIAL_STM32_2:
            rcc_periph_clock_enable(RCC_USART2);
            nvic_enable_irq(NVIC_USART2_IRQ);
            handlers_[1] = this;
            break;

        case SERIAL_STM32_3:
            rcc_periph_clock_enable(RCC_USART3);
            nvic_enable_irq(NVIC_USART3_IRQ);
            handlers_[2] = this;
            break;

        case SERIAL_STM32_4:
            rcc_periph_clock_enable(RCC_UART4);
            nvic_enable_irq(NVIC_UART4_IRQ);
            handlers_[3] = this;
            break;

        case SERIAL_STM32_5:
            rcc_periph_clock_enable(RCC_UART5);
            nvic_enable_irq(NVIC_UART5_IRQ);
            handlers_[4] = this;
            break;

        case SERIAL_STM32_6:
            rcc_periph_clock_enable(RCC_USART6);
            nvic_enable_irq(NVIC_USART6_IRQ);
            handlers_[5] = this;
            break;

        case SERIAL_STM32_7:
            rcc_periph_clock_enable(RCC_UART7);
            nvic_enable_irq(NVIC_UART7_IRQ);
            handlers_[6] = this;
            break;

        case SERIAL_STM32_8:
            rcc_periph_clock_enable(RCC_UART8);
            nvic_enable_irq(NVIC_UART8_IRQ);
            handlers_[7] = this;
            break;

        default:
            break;
    }

    // Setup GPIO pins for TX
    gpio_mode_setup(config_.tx_port,
                    GPIO_MODE_AF,
                    GPIO_PUPD_NONE,
                    config_.tx_pin);

    // Setup GPIO pins for RX
    gpio_mode_setup(config_.rx_port,
                    GPIO_MODE_AF,
                    GPIO_PUPD_NONE,
                    config_.rx_pin);

    // Setup TX and RX pins alternate function
    gpio_set_af(config_.tx_port, config_.tx_af, config_.tx_pin);
    gpio_set_af(config_.rx_port, config_.rx_af, config_.rx_pin);

    // Setup USART parameters
    usart_set_baudrate(config_.device, config_.baudrate);
    usart_set_databits(config_.device, config_.databits);
    usart_set_stopbits(config_.device, config_.stopbits);
    usart_set_mode(config_.device, config_.mode);
    usart_set_parity(config_.device, config_.parity);
    usart_set_flow_control(config_.device, config_.flow_control);

    // Enable RX interrupt
    usart_enable_rx_interrupt(config_.device);

    // Finally enable the USART
    usart_enable(config_.device);

    return true;
}
Пример #24
0
/*
 * uart_init(tx pin, rx pin, baudrate);
 *
 * Initialize a UART that will talk on the tx/rx pin pair at a given baudrate
 * Curently only 8n1 format, no-hw flow control, only.
 *
 * Returns channel unumber (0 - MAX_UART_CHANNELS) or -1 on error
 */
int
uart_init(enum GPIO_PORT_PIN tx, enum GPIO_PORT_PIN rx, int baudrate) {
    uint32_t my_uart = uart_pin_map(tx, USART);
    int i;

    if (uart_pin_map(rx, USART) != my_uart) {
        /* Both pins are not connected to same serial port */
        return -1;
    }
    if (nxt_channel >= MAX_UART_CHANNELS) {
        /* Need more channel configured */
        return -2;
    }
    if (my_uart == 0) {
        /* neither pin connects to a USART? */
        return -3;
    }

    /* Enable Clock for the USART/UART involved */
    rcc_peripheral_enable_clock((uint32_t *)uart_pin_map(tx, APB_REG),
                                uart_pin_map(tx, APB_ENA));

    /* Enable Clock for the GPIOs we are using */
    gpio_enable_clock(tx);
    gpio_enable_clock(rx);

    /* GPIO pins */

    /* Both AF Mode */
    gpio_mode_setup(gpio_base(tx), GPIO_MODE_AF, GPIO_PUPD_NONE, gpio_bit(tx));
    gpio_mode_setup(gpio_base(rx), GPIO_MODE_AF, GPIO_PUPD_NONE, gpio_bit(rx));
    gpio_set_af(gpio_base(tx), uart_pin_map(tx, AF), gpio_bit(tx));
    gpio_set_af(gpio_base(rx), uart_pin_map(rx, AF), gpio_bit(rx));

    /* Transmit pin set to an output */
    gpio_set_output_options(gpio_base(tx), GPIO_OTYPE_PP,
        GPIO_OSPEED_25MHZ, gpio_bit(tx));


    /* Set up UART parameters */
    usart_set_baudrate(my_uart, baudrate);
    usart_set_databits(my_uart, 8);
    usart_set_stopbits(my_uart, USART_STOPBITS_1);
    usart_set_mode(my_uart, USART_MODE_TX_RX);
    usart_set_parity(my_uart, USART_PARITY_NONE);
    usart_set_flow_control(my_uart, USART_FLOWCONTROL_NONE);
    usart_enable(my_uart);

    nxt_recv_ndx[nxt_channel] = cur_recv_ndx[nxt_channel] = 0;
    nxt_xmit_ndx[nxt_channel] = cur_xmit_ndx[nxt_channel] = 0;
    /*
     * This was done to try to get it to run under GDB with the Black
     * Magic debug probe but it didn't have any effect (interrupts
     * are still masked)
     */
    nvic_set_priority(uart_pin_map(tx, IRQ), 0); // highest priority
    nvic_enable_irq(uart_pin_map(tx, IRQ));
    USART_DR(my_uart) = 0;
    usart_enable_rx_interrupt(my_uart);

    /* Now create two mappings, channel => usart, and usart => channel */
    channel_to_uart_map[nxt_channel] = my_uart;
    for (i = 0; i < NUARTS; i++) {
        if (UART_MAP[i] == my_uart) {
            uart_to_channel_map[i] = nxt_channel;
            break;
        }
    }
    nxt_channel++;
    return (nxt_channel - 1);
}
Пример #25
0
static void usart_setup(void)
{
// ----BEGIN XBEE SETUP
    // Enable the USART3 interrupt.
    nvic_enable_irq(NVIC_USART3_IRQ);

    // Setup GPIO pins for USART3 transmit. 
    gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART3_TX);

    // Setup GPIO pins for USART3 receive.
    gpio_set_mode(GPIOB, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO_USART3_RX);

    // Setup USART3 RTS and CTS
    gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART3_RTS);
    gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_INPUT_FLOAT, GPIO_USART3_CTS);

    // Setup USART3 parameters.
    usart_set_baudrate(USART3, 115200);
    usart_set_databits(USART3, 8);
    usart_set_stopbits(USART3, USART_STOPBITS_1);
    usart_set_mode(USART3, USART_MODE_TX_RX);
    usart_set_parity(USART3, USART_PARITY_NONE);
    usart_set_flow_control(USART3, USART_FLOWCONTROL_RTS_CTS);

    // Enable USART3 Receive interrupt. 
    usart_enable_rx_interrupt(USART3);

    // Finally enable the USART.
    usart_enable(USART3);
// ----END XBEE SETUP

// ----BEGIN GPS SETUP
    // Enable the USART2 interrupt.
    nvic_enable_irq(NVIC_USART2_IRQ);

    // Setup GPIO pins for USART2 transmit.
    gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART2_TX);

    // Setup GPIO pins for USART2 receive.
    gpio_set_mode(GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO_USART2_RX);

    // 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_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);

// ----END GPS SETUP

// ----BEGIN DEBUG SETUP

    // Setup GPIO pins for USART2 transmit.
    gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART1_TX);

    // Setup GPIO pins for USART1 receive.
    gpio_set_mode(GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO_USART1_RX);

    // Setup USART1 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);

// ----END DEBUG SETUP

}
Пример #26
0
void usart_rx_reset() {
  usart_rx_dest = config.console.rxbuffer;
  usart_rx_end = 0;
  usart_enable_rx_interrupt(USART1);
}