示例#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;
	}
}
示例#2
0
文件: debug_usart.c 项目: marekr/asf
/**
 * 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);
}
示例#3
0
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;
}
示例#4
0
/**
 * \brief Disable receiver and Enable transmitter.
 */
static void func_transmitter(void)
{
	/* Configure the TXD pin as peripheral. */
	pio_configure_pin(PIN_USART_TXD_IDX, PIN_USART_TXD_FLAGS);

	/* Disable Receiver. */
	usart_disable_rx(BOARD_USART);

	/* Enable transmitter. */
	usart_enable_tx(BOARD_USART);
}
示例#5
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;
}
示例#6
0
static void XPROGTarget_SetTxMode(void)
{
	/* Wait for a full cycle of the clock */
	while(gpio_pin_is_high(PIN_PDIC_GPIO) && TimeoutTicksRemaining);
	while(gpio_pin_is_low(PIN_PDIC_GPIO) && TimeoutTicksRemaining);
	while(gpio_pin_is_high(PIN_PDIC_GPIO) && TimeoutTicksRemaining);
		
	usart_disable_rx(USART_PDI);
	usart_enable_tx(USART_PDI);
	gpio_configure_pin(PIN_PDIDTX_GPIO, PIN_PDIDTX_USART_FLAGS);

	IsSending = true;
}
示例#7
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;
}
示例#8
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;
}
示例#9
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;
}
示例#10
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);
}
示例#11
0
文件: usart_spi.c 项目: kerichsen/asf
/*! \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);
}
示例#12
0
/**
 *  \brief usart_rs485 Application entry point.
 *
 *  Configure USART in RS485 mode. If the application starts earlier, it acts
 *  as a receiver. Otherwise, it should be a transmitter.
 *
 *  \return Unused (ANSI-C compatibility).
 */
int main(void)
{
	static uint8_t uc_sync = SYNC_CHAR;
	uint32_t time_elapsed = 0;
	uint32_t ul_i;

	/* Initialize the SAM system. */
	sysclk_init();
	board_init();

	/* Configure UART for debug message output. */
	configure_console();

	/* Output example information. */
	puts(STRING_HEADER);

	/* Configure USART. */
	configure_usart();

	/* Get board USART PDC base address and enable receiver and transmitter. */
	g_p_pdc = usart_get_pdc_base(BOARD_USART);
	pdc_enable_transfer(g_p_pdc, PERIPH_PTCR_RXTEN | PERIPH_PTCR_TXTEN);

	/* 1ms tick. */
	configure_systick();

	/* Initialize receiving buffer to distinguish with the sent frame. */
	memset(g_uc_receive_buffer, 0x0, BUFFER_SIZE);

	/*
	 * Enable transmitter here, and disable receiver first, to avoid receiving
	 * characters sent by itself. It's necessary for half duplex RS485.
	 */
	usart_enable_tx(BOARD_USART);
	usart_disable_rx(BOARD_USART);

	/* Send a sync character XON (0x11). */
	g_st_packet.ul_addr = (uint32_t)&uc_sync;
	g_st_packet.ul_size = 1;
	pdc_tx_init(g_p_pdc, &g_st_packet, NULL);

	/* Delay until the line is cleared, an estimated time used. */
	wait(50);

	/* Read the acknowledgement. */
	g_st_packet.ul_addr = (uint32_t)&uc_sync;
	g_st_packet.ul_size = 1;
	pdc_rx_init(g_p_pdc, &g_st_packet, NULL);

	/* Then enable receiver. */
	usart_enable_rx(BOARD_USART);

	/* Wait until time out or acknowledgement is received. */
	time_elapsed = get_tick_count();
	while (!usart_is_rx_buf_end(BOARD_USART)) {
		if (get_tick_count() - time_elapsed > TIMEOUT) {
			break;
		}
	}

	/* If acknowledgement received in a short time. */
	if (usart_is_rx_buf_end(BOARD_USART)) {
		/* Acknowledgement. */
		if (uc_sync == ACK_CHAR) {
			/* Act as transmitter, start transmitting. */
			g_state = TRANSMITTING;
			puts("-I- Start transmitting!\r");
			g_st_packet.ul_addr = (uint32_t)g_uc_transmit_buffer;
			g_st_packet.ul_size = PDC_BUF_SIZE;
			pdc_tx_init(g_p_pdc, &g_st_packet, NULL);

			/* Enable transmitting interrupt. */
			usart_enable_interrupt(BOARD_USART, US_IER_ENDTX);
		}
	} else {
		/* Start receiving, act as receiver. */
		g_st_packet.ul_addr = (uint32_t)&uc_sync;
		g_st_packet.ul_size = 1;
		pdc_rx_init(g_p_pdc, &g_st_packet, NULL);
		puts("-I- Receiving sync character.\r");
		while (!usart_is_rx_buf_end(BOARD_USART)) {
		}

		/* Sync character is received. */
		if (uc_sync == SYNC_CHAR) {
			/* SEND XOff as acknowledgement. */
			uc_sync = ACK_CHAR;

			/*
			 * Delay to prevent the character from being discarded by
			 * transmitter due to responding too soon.
			 */
			wait(100);
			pio_set_pin_high(PIN_RE_IDX);
			g_st_packet.ul_addr = (uint32_t)&uc_sync;
			g_st_packet.ul_size = 1;
			pdc_tx_init(g_p_pdc, &g_st_packet, NULL);

			g_state = RECEIVING;
			puts("-I- Start receiving!\r");
			g_st_packet.ul_addr = (uint32_t)g_uc_receive_buffer;
			g_st_packet.ul_size = PDC_BUF_SIZE;
			pdc_rx_init(g_p_pdc, &g_st_packet, NULL);
			pio_set_pin_low(PIN_RE_IDX);
			/* Enable receiving interrupt. */
			usart_enable_interrupt(BOARD_USART, US_IER_ENDRX);
		}
	}
	while (g_state != RECEIVED) {
	}

	ul_i = 0;
	/* Print received frame out. */
	while ((ul_i < BUFFER_SIZE) && (g_uc_receive_buffer[ul_i] != '\0')) {
		if (g_uc_transmit_buffer[ul_i] != g_uc_receive_buffer[ul_i]) {
			puts("-E- Error occurred while receiving!\r");
			/* Infinite loop here. */
			while (1) {
			}
		}
		ul_i++;
	}
	puts("-I- Received successfully!\r");

	while (1) {
	}
}
示例#13
0
/**
 *  \brief usart_rs485 Application entry point.
 *
 *  Configure USART in RS485 mode. If the application starts earlier, it acts
 *  as a receiver. Otherwise, it should be a transmitter.
 *
 *  \return Unused (ANSI-C compatibility).
 */
int main(void)
{
	uint8_t uc_receive, uc_send = SYNC_CHAR;
	uint32_t time_elapsed = 0;
	uint32_t i;

	/* Initialize the SAM system. */
	sysclk_init();
	board_init();

	/* Configure UART for debug message output. */
	configure_console();

	/* Output example information. */
	puts(STRING_HEADER);

	/* 1ms tick. */
	configure_systick();

	/* Configure USART. */
	configure_usart();

	/* Initialize receiving buffer to distinguish with the sent frame. */
	memset(g_uc_receive_buffer, 0x0, BUFFER_SIZE);

	/*
	 * Enable transmitter here, and disable receiver first, to avoid receiving
	 * characters sent by itself. It's necessary for half duplex RS485.
	 */
	usart_enable_tx(BOARD_USART);
	usart_disable_rx(BOARD_USART);

	/* Enable PDCA module clock */
	pdca_enable(PDCA);

	/* Init PDCA channel with the pdca_options.*/
	pdca_channel_set_config(PDCA_RX_CHANNEL, &PDCA_RX_OPTIONS);
	pdca_channel_set_config(PDCA_TX_CHANNEL, &PDCA_TX_OPTIONS);

	/* Send a sync character XON (0x11). */
	pdca_channel_write_load(PDCA_TX_CHANNEL, &uc_send, 1);
	/* Enable transfer PDCA channel */
	pdca_channel_enable(PDCA_TX_CHANNEL);

	/* Delay until the line is cleared, an estimated time used. */
	wait(50);

	/* Then enable receiver. */
	usart_enable_rx(BOARD_USART);

	/* Read the acknowledgement. */
	pdca_channel_write_load(PDCA_RX_CHANNEL, &uc_receive, 1);
	/* Enable PDCA channel */
	pdca_channel_enable(PDCA_RX_CHANNEL);


	/* Wait until time out or acknowledgement is received. */
	time_elapsed = get_tick_count();
	while (pdca_get_channel_status(PDCA_RX_CHANNEL) !=
			PDCA_CH_TRANSFER_COMPLETED) {
		if (get_tick_count() - time_elapsed > TIMEOUT) {
			break;
		}
	}

	/* If acknowledgement received in a short time. */
	if (pdca_get_channel_status(PDCA_RX_CHANNEL) ==
			PDCA_CH_TRANSFER_COMPLETED) {
		/* Acknowledgement. */
		if (uc_receive == ACK_CHAR) {
			/* Act as transmitter, start transmitting. */
			puts("-I- Act as transmitter.\r");

			g_state = TRANSMITTING;
			puts("-I- Start transmitting!\r");
			pdca_channel_write_load(PDCA_TX_CHANNEL, g_uc_transmit_buffer,
					BUFFER_SIZE);

			/* Enable PDCA interrupt */
			pdca_channel_set_callback(PDCA_TX_CHANNEL, PDCA_TX_Handler,
					PDCA_1_IRQn, 1, PDCA_IER_TRC);

			while (g_state != TRANSMITTED) {
			}
			puts("-I- Transmit done!\r");

			while (1) {
			}
		}
	} else {
		/* Start receiving, act as receiver. */
		puts("-I- Act as receiver.\r");
		puts("-I- Receiving sync character.\r");

		while (pdca_get_channel_status(PDCA_RX_CHANNEL) !=
			PDCA_CH_TRANSFER_COMPLETED) {
		}

		/* Sync character is received. */
		if (uc_receive == SYNC_CHAR) {
			puts("-I- Received sync character.\r");
			/* SEND XOff as acknowledgement. */
			uc_send = ACK_CHAR;

			/*
			 * Delay to prevent the character from being discarded by
			 * transmitter due to responding too soon.
			 */
			wait(100);
			ioport_set_pin_level(RS485_USART_CTS_PIN, 1);
			pdca_channel_write_load(PDCA_TX_CHANNEL, &uc_send, 1);

			g_state = RECEIVING;
			puts("-I- Start receiving buffer!\r");

			pdca_channel_write_load(PDCA_RX_CHANNEL, g_uc_receive_buffer,
					BUFFER_SIZE);

			/* Enable PDCA interrupt */
			pdca_channel_set_callback(PDCA_RX_CHANNEL, PDCA_RX_Handler,
					PDCA_0_IRQn, 1, PDCA_IER_TRC);

			ioport_set_pin_level(RS485_USART_CTS_PIN, 0);

			while (g_state != RECEIVED) {
			}
		}
	}

	i = 0;
	/* Check received frame. */
	while (i < BUFFER_SIZE) {
		if (g_uc_transmit_buffer[i] != g_uc_receive_buffer[i]) {
			puts("-E- Error occurred while receiving!\r");
			/* Infinite loop here. */
			while (1) {
			}
		}

		i++;
	}
	puts("-I- Received buffer successfully!\r");

	while (1) {
	}
}
示例#14
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));
    }
}
示例#15
0
/**
 * \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;
}