Exemple #1
0
/**
 * \brief This function closes and disables communication in the specified
 * USART.
 *
 * \param chn  Communication channel [0, 1]
 *
 * \retval true on success.
 * \retval false on failure.
 */
int8_t busart_if_close(uint8_t chn)
{
	if (!busart_chn_open[chn]) {
		return false;
	}

	switch (chn) {
#ifdef CONF_BOARD_USART0_RXD
	case 0:
	{
		usart_disable_tx(USART0);
		usart_disable_rx(USART0);

		usart_disable_interrupt(USART0, US_IDR_RXRDY);
		usart_disable_interrupt(USART0, US_IER_ENDRX);

		/* Stop TC */
		if (!busart_chn_open[1]) {
			tc_stop(TC_USART, TC_USART_CHN);
		}

		return true;
	}
	break;
#endif

#ifdef CONF_BOARD_USART1_RXD
	case 1:
	{
		usart_disable_tx(USART1);
		usart_disable_rx(USART1);

		usart_disable_interrupt(USART1, US_IDR_RXRDY);
		usart_disable_interrupt(USART1, US_IER_ENDRX);

		/* Stop TC */
		if (!busart_chn_open[0]) {
			tc_stop(TC_USART, TC_USART_CHN);
		}

		return true;
	}
	break;
#endif
	default:
		return false;
	}
}
Exemple #2
0
/**
 * Close USART serial debug communication port
 */
void dbg_usart_cleanup(void)
{
	usart_disable_tx(DBG_USART_BASE);
	usart_disable_rx(DBG_USART_BASE);
	usart_reset(DBG_USART_BASE);
	pmc_disable_periph_clk(DBG_USART_ID);
}
platform_result_t platform_uart_deinit( platform_uart_driver_t* driver )
{

    usart_disable_interrupt( driver->peripheral->peripheral, 0xffffffff );

    NVIC_DisableIRQ( platform_uarts_irq_numbers[driver->peripheral->uart_id] );

    pdc_disable_transfer( usart_get_pdc_base( driver->peripheral->peripheral ), PERIPH_PTCR_TXTDIS | PERIPH_PTCR_RXTDIS );

    usart_disable_tx( driver->peripheral->peripheral );
    usart_disable_rx( driver->peripheral->peripheral );

    sysclk_disable_peripheral_clock( driver->peripheral->peripheral_id );

    platform_gpio_deinit( driver->peripheral->tx_pin );
    platform_gpio_deinit( driver->peripheral->rx_pin );

    if ( driver->peripheral->cts_pin != NULL )
    {
        platform_gpio_deinit( driver->peripheral->cts_pin );
    }

    if ( driver->peripheral->rts_pin != NULL )
    {
        platform_gpio_deinit( driver->peripheral->rts_pin );
    }

    host_rtos_deinit_semaphore( &driver->tx_dma_complete );
    host_rtos_deinit_semaphore( &driver->rx_dma_complete );

    driver->peripheral = NULL;
    memset( driver, 0, sizeof(platform_uart_driver_t) );

    return WICED_SUCCESS;
}
Exemple #4
0
void bm_print_clear(void)
{
	usart_enable_tx(BM_USART_USART);
	usart_putchar(BM_USART_USART, BM_MSG_START_PATTERN);
	usart_putchar(BM_USART_USART, 3 /* length */);
	usart_putchar(BM_USART_USART, BM_PRINT_CLEAR);
	usart_putchar(BM_USART_USART, BM_MSG_STOP_PATTERN);
	while (!usart_is_tx_empty(BM_USART_USART));
	usart_disable_tx(BM_USART_USART);
}
Exemple #5
0
void bm_mouse_pointer_ctrl(bool state)
{
	usart_enable_tx(BM_USART_USART);
	usart_putchar(BM_USART_USART, BM_MSG_START_PATTERN);
	usart_putchar(BM_USART_USART, 4 /* length */);
	usart_putchar(BM_USART_USART, BM_POINTER_CTRL);
	usart_putchar(BM_USART_USART, state);
	usart_putchar(BM_USART_USART, BM_MSG_STOP_PATTERN);
	while (!usart_is_tx_empty(BM_USART_USART));
	usart_disable_tx(BM_USART_USART);
}
Exemple #6
0
void bm_led_tgl(uint32_t led)
{
	usart_enable_tx(BM_USART_USART);
	usart_putchar(BM_USART_USART, BM_MSG_START_PATTERN);
	usart_putchar(BM_USART_USART, 4 /* length */);
	usart_putchar(BM_USART_USART, BM_LED_TGL);
	usart_putchar(BM_USART_USART, led);
	usart_putchar(BM_USART_USART, BM_MSG_STOP_PATTERN);
	while (!usart_is_tx_empty(BM_USART_USART));
	usart_disable_tx(BM_USART_USART);
}
Exemple #7
0
void bm_pullup_twi(bool state)
{
	usart_enable_tx(BM_USART_USART);
	usart_putchar(BM_USART_USART, BM_MSG_START_PATTERN);
	usart_putchar(BM_USART_USART, 4 /* length */);
	usart_putchar(BM_USART_USART, BM_PULLUP_TWI);
	usart_putchar(BM_USART_USART, state);
	usart_putchar(BM_USART_USART, BM_MSG_STOP_PATTERN);
	while (!usart_is_tx_empty(BM_USART_USART));
	usart_disable_tx(BM_USART_USART);
}
Exemple #8
0
static void XPROGTarget_SetRxMode(void)
{
	while(usart_is_tx_empty(USART_PDI) == 0);

	usart_disable_tx(USART_PDI);
	usart_enable_rx(USART_PDI);
	
	gpio_configure_pin(PIN_PDIDTX_GPIO, PIN_PDIDTX_IN_FLAGS);
	
	IsSending = false;
}
Exemple #9
0
void bm_mouse_pointer_move(uint32_t x, uint32_t y)
{
	usart_enable_tx(BM_USART_USART);
	usart_putchar(BM_USART_USART, BM_MSG_START_PATTERN);
	usart_putchar(BM_USART_USART, 5 /* length */);
	usart_putchar(BM_USART_USART, BM_POINTER_MOVE);
	usart_putchar(BM_USART_USART, x);
	usart_putchar(BM_USART_USART, y);
	usart_putchar(BM_USART_USART, BM_MSG_STOP_PATTERN);
	while (!usart_is_tx_empty(BM_USART_USART));
	usart_disable_tx(BM_USART_USART);
}
Exemple #10
0
bool bm_get_mcu_current(uint32_t* sleep_mode, float* current)
{
	uint32_t current_d;
        uint32_t c;
	usart_enable_tx(BM_USART_USART);
	usart_putchar(BM_USART_USART, BM_MSG_START_PATTERN);
	usart_putchar(BM_USART_USART, 4 /* length */);
	usart_putchar(BM_USART_USART, BM_MCU_GET_CURRENT);
	usart_putchar(BM_USART_USART, *sleep_mode);
	usart_putchar(BM_USART_USART, BM_MSG_STOP_PATTERN);
	while (!usart_is_tx_empty(BM_USART_USART));
	usart_disable_tx(BM_USART_USART);
	usart_enable_rx(BM_USART_USART);
	// Check first caracter is start pattern
	usart_getchar(BM_USART_USART, &c);
	if (c == BM_MSG_STOP_PATTERN) {
		usart_getchar(BM_USART_USART, &c);
	}
	if (c != BM_MSG_START_PATTERN) {
		return false;
	}
	// Check second caracter is length
	usart_getchar(BM_USART_USART, &c);
	if (c != 8) {
		return false;
	}
	// Check third caracter is Current Command
	usart_getchar(BM_USART_USART, &c);
	if (c != BM_MCU_RET_CURRENT) {
		return false;
	}
	// Check third caracter is sleepmode
	usart_getchar(BM_USART_USART, &c);
	*sleep_mode = c;
	// Then read current
	usart_getchar(BM_USART_USART, &c);
	current_d = c<<24;
	usart_getchar(BM_USART_USART, &c);
	current_d |= c<<16;
	usart_getchar(BM_USART_USART, &c);
	current_d |= c<<8;
	usart_getchar(BM_USART_USART, &c);
	current_d |= c;
	*current = *(float*)& current_d;
	// Check last caracter is stop pattern
	usart_getchar(BM_USART_USART, &c);
	if (c != BM_MSG_STOP_PATTERN) {
		return false;
	}
	usart_disable_rx(BM_USART_USART);
	return true;
}
Exemple #11
0
bool bm_get_firmware_version(uint8_t* fw_minor_version, uint8_t* fw_major_version)
{
        uint32_t start, length, stop, cmd_id, c;

	// Wait for some microseconds in order to avoid fifo overrun
	//
	delay_ms(20);

	usart_enable_tx(BM_USART_USART);
	usart_putchar(BM_USART_USART, BM_MSG_START_PATTERN);
	usart_putchar(BM_USART_USART, 3 /* length */);
	usart_putchar(BM_USART_USART, BM_GET_FIRMWARE_VERSION);
	usart_putchar(BM_USART_USART, BM_MSG_STOP_PATTERN);

	while (!usart_is_tx_empty(BM_USART_USART));
	usart_disable_tx(BM_USART_USART);
	usart_enable_rx(BM_USART_USART);
	
	// Check first character is start pattern
	usart_getchar(BM_USART_USART, &start);
	if (start == BM_MSG_STOP_PATTERN) {
		usart_getchar(BM_USART_USART, &start);
	}
	if (start != BM_MSG_START_PATTERN) {
		return false;
	}
	// Check second character is length
	usart_getchar(BM_USART_USART, &length);
	if (length != 5) {
		return false;
	}
	// Check third character is Current Command
	usart_getchar(BM_USART_USART, &cmd_id);
	if (cmd_id != BM_RET_FIRMWARE_VERSION) {
		return false;
	}

	// Get Fifo free size
	usart_getchar(BM_USART_USART, &c);
	*fw_major_version = c;
	usart_getchar(BM_USART_USART, &c);
	*fw_minor_version = c;

	// Check last character is stop pattern
	usart_getchar(BM_USART_USART, &stop);
	if (stop != BM_MSG_STOP_PATTERN) {
		return false;
	}
	usart_disable_rx(BM_USART_USART);
	return true;
}
Exemple #12
0
bool bm_get_fifo_free_size(uint16_t* free_size)
{
        uint32_t start, length, stop, cmd_id, c;

	// Wait for some microseconds in order to avoid fifo overrun
	//
	delay_ms(20);

	usart_enable_tx(BM_USART_USART);
	usart_putchar(BM_USART_USART, BM_MSG_START_PATTERN);
	usart_putchar(BM_USART_USART, 3 /* length */);
	usart_putchar(BM_USART_USART, BM_MCU_GET_FIFO_FREE_SIZE);
	usart_putchar(BM_USART_USART, BM_MSG_STOP_PATTERN);

	while (!usart_is_tx_empty(BM_USART_USART));
	usart_disable_tx(BM_USART_USART);
	usart_enable_rx(BM_USART_USART);
	
	// Check first character is start pattern
	usart_getchar(BM_USART_USART, &start);
	if (start == BM_MSG_STOP_PATTERN) {
		usart_getchar(BM_USART_USART, &start);
	}
	if (start != BM_MSG_START_PATTERN) {
		return false;
	}
	// Check second character is length
	usart_getchar(BM_USART_USART, &length);
	if (length != 5) {
		return false;
	}
	// Check third character is Current Command
	usart_getchar(BM_USART_USART, &cmd_id);
	if (cmd_id != BM_MCU_RET_FIFO_FREE_SIZE) {
		return false;
	}

	// Get Fifo free size
	usart_getchar(BM_USART_USART, &c);
	*free_size = c << 8;
	usart_getchar(BM_USART_USART, &c);
	*free_size |= c;

	// Check last character is stop pattern
	usart_getchar(BM_USART_USART, &stop);
	if (stop != BM_MSG_STOP_PATTERN) {
		return false;
	}
	usart_disable_rx(BM_USART_USART);
	return true;
}
Exemple #13
0
void bm_tgl_button(uint32_t timeout_ms)
{
	usart_enable_tx(BM_USART_USART);
	usart_putchar(BM_USART_USART, BM_MSG_START_PATTERN);
	usart_putchar(BM_USART_USART, 7 /* length */);
	usart_putchar(BM_USART_USART, BM_TGL_BUTTON);
	usart_putchar(BM_USART_USART, timeout_ms & 0xff);
	usart_putchar(BM_USART_USART, (timeout_ms >> 8 ) & 0xff);
	usart_putchar(BM_USART_USART, (timeout_ms >> 16) & 0xff);
	usart_putchar(BM_USART_USART, timeout_ms >> 24);
	usart_putchar(BM_USART_USART, BM_MSG_STOP_PATTERN);
	while (!usart_is_tx_empty(BM_USART_USART));
	usart_disable_tx(BM_USART_USART);
}
Exemple #14
0
void bm_send_picouart_frame(uint8_t frame, uint32_t timeout_ms)
{
	usart_enable_tx(BM_USART_USART);
	usart_putchar(BM_USART_USART, BM_MSG_START_PATTERN);
	usart_putchar(BM_USART_USART, 8 /* length */);
	usart_putchar(BM_USART_USART, BM_PICOUART_SEND);
	usart_putchar(BM_USART_USART, frame);
	usart_putchar(BM_USART_USART, timeout_ms & 0xff);
	usart_putchar(BM_USART_USART, (timeout_ms >> 8 ) & 0xff);
	usart_putchar(BM_USART_USART, (timeout_ms >> 16) & 0xff);
	usart_putchar(BM_USART_USART, timeout_ms >> 24);
	usart_putchar(BM_USART_USART, BM_MSG_STOP_PATTERN);
	while (!usart_is_tx_empty(BM_USART_USART));
	usart_disable_tx(BM_USART_USART);
}
Exemple #15
0
OSStatus platform_uart_deinit( platform_uart_driver_t* driver )
{
    OSStatus          err = kNoErr;

    platform_mcu_powersave_disable();
    require_action_quiet( ( driver != NULL ), exit, err = kParamErr);

    usart_disable_interrupt( driver->peripheral->port, 0xffffffff );

    NVIC_DisableIRQ( platform_flexcom_irq_numbers[driver->peripheral->uart_id] );

    pdc_disable_transfer( usart_get_pdc_base( driver->peripheral->port ), PERIPH_PTCR_TXTDIS | PERIPH_PTCR_RXTDIS );

    usart_disable_tx( driver->peripheral->port );
    usart_disable_rx( driver->peripheral->port );

    if( pmc_is_periph_clk_enabled( driver->peripheral->peripheral_id ) == 1  ) {
        flexcom_disable( driver->peripheral->flexcom_base );
    }

    platform_gpio_deinit( driver->peripheral->tx_pin );
    platform_gpio_deinit( driver->peripheral->rx_pin );

    if ( driver->peripheral->cts_pin != NULL )
    {
        platform_gpio_deinit( driver->peripheral->cts_pin );
    }

    if ( driver->peripheral->rts_pin != NULL )
    {
        platform_gpio_deinit( driver->peripheral->rts_pin );
    }

#ifndef NO_MICO_RTOS
    mico_rtos_deinit_semaphore(&driver->rx_complete);
    mico_rtos_deinit_semaphore(&driver->tx_complete);
#endif

    driver->peripheral = NULL;
    memset( driver, 0, sizeof(platform_uart_driver_t) );

exit:
    platform_mcu_powersave_enable();
    return err;
}
Exemple #16
0
void bm_send_mcu_status(uint32_t power_scaling, uint32_t sleep_mode,
		uint32_t cpu_freq, uint32_t cpu_src)
{
	usart_enable_tx(BM_USART_USART);
	usart_putchar(BM_USART_USART, BM_MSG_START_PATTERN);
	usart_putchar(BM_USART_USART, 10 /* length */);
	usart_putchar(BM_USART_USART, BM_MCU_STATUS);
	usart_putchar(BM_USART_USART, power_scaling);
	usart_putchar(BM_USART_USART, sleep_mode);
	usart_putchar(BM_USART_USART, cpu_freq >> 24);
	usart_putchar(BM_USART_USART, (cpu_freq >> 16) & 0xff);
	usart_putchar(BM_USART_USART, (cpu_freq >> 8 ) & 0xff);
	usart_putchar(BM_USART_USART, cpu_freq & 0xff);
	usart_putchar(BM_USART_USART, cpu_src);
	usart_putchar(BM_USART_USART, BM_MSG_STOP_PATTERN);
	while (!usart_is_tx_empty(BM_USART_USART));
	usart_disable_tx(BM_USART_USART);
}
Exemple #17
0
/** Disables the target's PDI interface, exits programming mode and starts the target's application. */
void XPROGTarget_DisableTargetPDI(void)
{
	/* Switch to Rx mode to ensure that all pending transmissions are complete */
	if (IsSending)
	  XPROGTarget_SetRxMode();

	/* Turn off receiver and transmitter of the USART, clear settings */
	usart_disable_rx(USART_PDI);
	usart_disable_tx(USART_PDI);

	/* Tristate all pins */
	gpio_configure_pin(PIN_PDIC_GPIO, PIN_PDIC_IN_FLAGS);
	gpio_configure_pin(PIN_PDIDRX_GPIO, PIN_PDIDRX_FLAGS);
	gpio_configure_pin(PIN_PDIDTX_GPIO, PIN_PDIDTX_IN_FLAGS);
	
	/* Turn off USART */
	sysclk_disable_peripheral_clock(USART_PDI_ID);
}
Exemple #18
0
static void usart_handler(uint8_t port)
{
	Usart* usart = get_usart(port);
	uint32_t sr = usart_get_status(usart);
	if (sr & US_CSR_RXRDY) {
		// Data received
		ui_com_tx_start();
		uint32_t value;
		bool b_error = usart_read(usart, &value) ||
			(sr & (US_CSR_FRAME | US_CSR_TIMEOUT | US_CSR_PARE));
		if (b_error) {
			usart_reset_rx(usart);
			usart_enable_rx(usart);
			udi_cdc_multi_signal_framing_error(port);
			ui_com_error();
		}
		// Transfer UART RX fifo to CDC TX
		if (!udi_cdc_multi_is_tx_ready(port)) {
			// Fifo full
			udi_cdc_multi_signal_overrun(port);
			ui_com_overflow();
		} else {
			udi_cdc_multi_putc(port, value);
		}
		ui_com_tx_stop();
		return;
	}

	if (sr & US_CSR_TXRDY) {
		// Data send
		if (udi_cdc_multi_is_rx_ready(port)) {
			// Transmit next data
			ui_com_rx_start();
			int c = udi_cdc_multi_getc(port);
			usart_write(usart, c);
			
		} else {
			// Fifo empty then Stop UART transmission
			usart_disable_tx(usart);
			usart_disable_interrupt(usart, US_IDR_TXRDY);
			ui_com_rx_stop();
		}
	}
}
Exemple #19
0
/**
 * \brief Disable transmitter and Enable receiver.
 */
static void func_receiver(void)
{
	uint32_t ul_temp;

	/* Disable Transmitter. */
	usart_disable_tx(BOARD_USART);

	/* Configure the TXD pin as PIO. */
	pio_configure_pin(PIN_USART_TXD_IDX, PIN_USART_TXD_IO_FLAGS);
	pio_set_pin_low(PIN_USART_TXD_IDX);

	/* Enable receiver. */
	usart_enable_rx(BOARD_USART);

	/* Read dummy to make sure that there are no characters in US_THR! */
	if (usart_is_rx_ready(BOARD_USART)) {
		usart_read(BOARD_USART, &ul_temp);
		/* avoid Cppcheck Warning */
		UNUSED(ul_temp);
	}
}
Exemple #20
0
void bm_print_txt(uint8_t* str, uint8_t str_length)
{
#define BM_PRINT_TEXT_SIZE                  21
#define BM_PRINT_TEXT_DEEP                  6
	usart_enable_tx(BM_USART_USART);
	usart_putchar(BM_USART_USART, BM_MSG_START_PATTERN);
	usart_putchar(BM_USART_USART, BM_PRINT_TEXT_SIZE + 3 /* length */);
	usart_putchar(BM_USART_USART, BM_PRINT_TEXT);
	if (str_length < BM_PRINT_TEXT_SIZE) {
		for (uint8_t i=0;i<str_length;i++){
			usart_putchar(BM_USART_USART, str[i]);
		}
		for (uint8_t i=0;i < BM_PRINT_TEXT_SIZE - str_length;i++){
			usart_putchar(BM_USART_USART, '\0');
		}
	} else {
		for (uint8_t i=0;i<BM_PRINT_TEXT_SIZE;i++){
			usart_putchar(BM_USART_USART, str[i]);
		}
	}
	usart_putchar(BM_USART_USART, BM_MSG_STOP_PATTERN);
	while (!usart_is_tx_empty(BM_USART_USART));
	usart_disable_tx(BM_USART_USART);
}
Exemple #21
0
/*! \brief Disable the USART for the specified USART in SPI mode.
 *
 * Ensure that nothing is transferred while setting up buffers.
 *
 * \param p_usart Base address of the USART instance.
 *
 */
void usart_spi_disable(Usart *p_usart)
{
	usart_disable_tx(p_usart);
	usart_disable_rx(p_usart);
}
/**
 * \ingroup freertos_usart_peripheral_control_group
 * \brief Initializes the FreeRTOS ASF USART driver for the specified USART
 * port.
 *
 * freertos_usart_serial_init() is an ASF specific FreeRTOS driver function.  It
 * must be called before any other ASF specific FreeRTOS driver functions
 * attempt to access the same USART port.
 *
 * If freertos_driver_parameters->operation_mode equals USART_RS232 then
 * freertos_usart_serial_init() will configure the USART port for standard RS232
 * operation.  If freertos_driver_parameters->operation_mode equals any other
 * value then freertos_usart_serial_init() will not take any action.
 *
 * Other ASF USART functions can be called after freertos_usart_serial_init()
 * has completed successfully.
 *
 * The FreeRTOS ASF driver both installs and handles the USART PDC interrupts.
 * Users do not need to concern themselves with interrupt handling, and must
 * not install their own interrupt handler.
 *
 * This driver is provided with an application note, and an example project that
 * demonstrates the use of this function.
 *
 * \param p_usart    The USART peripheral being initialized.
 * \param uart_parameters    Structure that defines the USART bus and transfer
 *     parameters, such the baud rate and the number of data bits.
 *     sam_usart_opt_t is a standard ASF type (it is not FreeRTOS specific).
 * \param freertos_driver_parameters    Defines the driver behavior.  See the
 *    freertos_peripheral_options_t documentation, and the application note that
 *    accompanies the ASF specific FreeRTOS functions.
 *
 * \return If the initialization completes successfully then a handle that can
 *     be used with FreeRTOS USART read and write functions is returned.  If
 *     the initialisation fails then NULL is returned.
 */
freertos_usart_if freertos_usart_serial_init(Usart *p_usart,
		const sam_usart_opt_t *const uart_parameters,
		const freertos_peripheral_options_t *const freertos_driver_parameters)
{
	portBASE_TYPE usart_index;
	bool is_valid_operating_mode;
	freertos_usart_if return_value;
	const enum peripheral_operation_mode valid_operating_modes[] = {USART_RS232};

	/* Find the index into the all_usart_definitions array that holds details of
	the p_usart peripheral. */
	usart_index = get_pdc_peripheral_details(all_usart_definitions,
			MAX_USARTS,
			(void *) p_usart);

	/* Check the requested operating mode is valid for the peripheral. */
	is_valid_operating_mode = check_requested_operating_mode(
			freertos_driver_parameters->operation_mode,
			valid_operating_modes,
			sizeof(valid_operating_modes) /
			sizeof(enum peripheral_operation_mode));

	/* Don't do anything unless a valid p_usart pointer was used, and a valid
	operating mode was requested. */
	if ((usart_index < MAX_USARTS) && (is_valid_operating_mode == true)) {
		/* This function must be called exactly once per supported USART.  Check it
		has not been called	before. */
		configASSERT(rx_buffer_definitions[usart_index].next_byte_to_read == NULL);

		/* Disable everything before enabling the clock. */
		usart_disable_tx(p_usart);
		usart_disable_rx(p_usart);
		pdc_disable_transfer(all_usart_definitions[usart_index].pdc_base_address,
				(PERIPH_PTCR_RXTDIS | PERIPH_PTCR_TXTDIS));

#if (SAMG55)
		/* Enable the peripheral and set USART mode. */
		uint32_t temp = (uint32_t)all_usart_definitions[usart_index].peripheral_base_address - 0x200;
		Flexcom *p_flexcom = (Flexcom *)temp;
		flexcom_enable(p_flexcom);
		flexcom_set_opmode(p_flexcom, FLEXCOM_USART);
#else
		/* Enable the peripheral clock in the PMC. */
		pmc_enable_periph_clk(
				all_usart_definitions[usart_index].peripheral_id);
#endif

		switch (freertos_driver_parameters->operation_mode) {
		case USART_RS232:
			/* Call the standard ASF init function. */
			usart_init_rs232(p_usart, uart_parameters,
					sysclk_get_cpu_hz());
			break;

		default:
			/* Other modes are not currently supported. */
			break;
		}

		/* Disable all the interrupts. */
		usart_disable_interrupt(p_usart, MASK_ALL_INTERRUPTS);

		/* Create any required peripheral access mutexes and transaction complete
		semaphores.  This peripheral is full duplex so only the Tx semaphores
		are created in the following function.  The the Rx semaphores are
		created	separately. */
		create_peripheral_control_semaphores(
				freertos_driver_parameters->options_flags,
				&(tx_dma_control[usart_index]),
				NULL /* The rx structures are not created in this function. */);

		/* Is the driver also going to receive? */
		if (freertos_driver_parameters->receive_buffer != NULL) {
			/* rx_event_semaphore is used to signal the arival of new data.  It
			must be a counting semaphore for the following reason:  If the Rx
			DMA places data at the end of its circular buffer it will give the
			semaphore to indicate the presence of unread data.  If it then
			receives more data, it will write this to the start of the circular
			buffer, then give the semaphore again.  Now, if a task reads data
			out of the same circular buffer, and requests less data than is
			available, but more than is available between the next read pointer
			and the end of the buffer, the actual amount returned will be
			capped to that available up to the end of the buffer only.  If this
			semaphore was a binary semaphore, it would then be 'taken' even
			though, unknown to the reading task, unread and therefore available
			data remained at the beginning of the buffer. */
			rx_buffer_definitions[usart_index].rx_event_semaphore =
					xSemaphoreCreateCounting(portMAX_DELAY, 0);
			configASSERT(rx_buffer_definitions[usart_index].rx_event_semaphore);

			/* Set the timeout to 5ms, then start waiting for a character (the
			timeout is not started until characters have started to	be
			received). */
			usart_set_rx_timeout(p_usart,
					(uart_parameters->baudrate / BITS_PER_5_MS));
			usart_start_rx_timeout(p_usart);

			/* The receive buffer is currently empty, so the DMA has control
			over the entire buffer. */
			rx_buffer_definitions[usart_index].rx_pdc_parameters.ul_addr =
					(uint32_t)freertos_driver_parameters->receive_buffer;
			rx_buffer_definitions[usart_index].rx_pdc_parameters.ul_size =
					freertos_driver_parameters->receive_buffer_size;
			pdc_rx_init(
					all_usart_definitions[usart_index].pdc_base_address,
					&(rx_buffer_definitions[usart_index].rx_pdc_parameters),
					NULL);

			/* Set the next byte to read to the start of the buffer as no data
			has yet been read. */
			rx_buffer_definitions[usart_index].next_byte_to_read =
					freertos_driver_parameters->receive_buffer;

			/* Remember the limits of entire buffer. */
			rx_buffer_definitions[usart_index].rx_buffer_start_address =
					rx_buffer_definitions[usart_index].rx_pdc_parameters.ul_addr;
			rx_buffer_definitions[usart_index].past_rx_buffer_end_address =
					rx_buffer_definitions[usart_index].rx_buffer_start_address +
					freertos_driver_parameters->receive_buffer_size;

			/* If the rx driver is to be thread aware, create an access control
			mutex. */
			if ((freertos_driver_parameters->options_flags &
					USE_RX_ACCESS_MUTEX) != 0) {
				rx_buffer_definitions[usart_index].rx_access_mutex =
					xSemaphoreCreateMutex();
				configASSERT(rx_buffer_definitions[usart_index].rx_access_mutex);
			}

			/* Catch the DMA running out of Rx space, and gaps in the
			reception.  These events are both used to signal that there is
			data available in the Rx buffer. */
			usart_enable_interrupt(p_usart, US_IER_ENDRX | US_IER_TIMEOUT);

			/* The Rx DMA is running all the time, so enable it now. */
			pdc_enable_transfer(
					all_usart_definitions[usart_index].pdc_base_address,
					PERIPH_PTCR_RXTEN);
		} else {
			/* next_byte_to_read is used to check to see if this function
			has been called before, so it must be set to something, even if
			it is not going to be used.  The value it is set to is not
			important, provided it is not zero (NULL). */
			rx_buffer_definitions[usart_index].next_byte_to_read = RX_NOT_USED;
		}

		/* Configure and enable the USART interrupt in the interrupt controller. */
		configure_interrupt_controller(all_usart_definitions[usart_index].peripheral_irq,
				freertos_driver_parameters->interrupt_priority);

		/* Error interrupts are always enabled. */
		usart_enable_interrupt(
				all_usart_definitions[usart_index].peripheral_base_address,
				IER_ERROR_INTERRUPTS);

		/* Finally, enable the receiver and transmitter. */
		usart_enable_tx(p_usart);
		usart_enable_rx(p_usart);

		return_value = (freertos_usart_if) p_usart;
	} else {
		return_value = NULL;
	}

	return return_value;
}
Exemple #23
0
void serial_init(serial_t *obj, PinName tx, PinName rx)
{
    /* Sanity check arguments */
    MBED_ASSERT(obj);
    int clockid = NC, flexcom = NC;

    /*To determine the uart peripheral associated with pins*/
    UARTName uart_tx = (UARTName)pinmap_peripheral(tx, PinMap_UART_TX);
    UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX);
    UARTName uart = (UARTName)pinmap_merge(uart_tx, uart_rx);

    MBED_ASSERT(uart != (UARTName)NC);

    if (g_sys_init == 0) {
        sysclk_init();
        system_board_init();
        g_sys_init = 1;
    }
    pUSART_S(obj) = uart;
    pSERIAL_S(obj)->uart_serial_options.baudrate = (9600UL);
    pSERIAL_S(obj)->uart_serial_options.charlength = US_MR_CHRL_8_BIT;
    pSERIAL_S(obj)->uart_serial_options.paritytype = US_MR_PAR_NO;
    pSERIAL_S(obj)->uart_serial_options.stopbits = US_MR_NBSTOP_1_BIT;
    pSERIAL_S(obj)->actrec = false;
    pSERIAL_S(obj)->acttra = false;

    /* Configure UART pins */
    if(tx != NC) {
        pin_function(tx, pinmap_find_function(tx, PinMap_UART_TX));
        ioport_disable_pin(tx);
    }
    if(rx != NC) {
        pin_function(rx, pinmap_find_function(rx, PinMap_UART_RX));
        ioport_disable_pin(rx);
    }
    clockid = get_usart_clock_id(uart);
    if (clockid != NC) {
        sysclk_enable_peripheral_clock(clockid);
    }

    flexcom = (int)get_flexcom_id(uart);
#if (!SAM4L)
#if (SAMG55)
    /* Configure flexcom for usart */
    flexcom_enable((Flexcom* )flexcom);
    flexcom_set_opmode((Flexcom* )flexcom, FLEXCOM_USART);
#else
    sysclk_enable_peripheral_clock(clockid);
#endif
    /* Configure USART */
    usart_init_rs232((Usart*)uart, (sam_usart_opt_t*)&(pSERIAL_S(obj)->uart_serial_options),
                     sysclk_get_peripheral_hz());
#endif
#if (SAM4L)
    sysclk_enable_peripheral_clock(clockid);
    /* Configure USART */
    usart_init_rs232((Usart*)uart,  (sam_usart_opt_t*)&(pSERIAL_S(obj)->uart_serial_options, sysclk_get_peripheral_bus_hz((Usart*)uart));
#endif
                     /* Disable rx and tx in case 1 line only required to be configured for usart */
                     usart_disable_tx((Usart*)uart);
                     usart_disable_rx((Usart*)uart);
                     /* Enable the receiver and transmitter. */
    if(tx != NC) {
    usart_enable_tx((Usart*)uart);
    }
    if(rx != NC) {
    usart_enable_rx((Usart*)uart);
    }

    if(uart == STDIO_UART) {
    stdio_uart_inited = 1;
    memcpy(&stdio_uart, obj, sizeof(serial_t));
    }
}