int main(void)
{
	system_init();

//! [setup_init]
	configure_usart();
	configure_usart_callbacks();
//! [setup_init]

//! [main]
//! [enable_global_interrupts]
	system_interrupt_enable_global();
//! [enable_global_interrupts]

//! [main_send_string]
	uint8_t string[] = "Hello World!\r\n";
	usart_write_buffer_job(&usart_instance, string, sizeof(string));
//! [main_send_string]

//! [main_loop]
	while (true) {
//! [main_loop]
//! [main_read]
		usart_read_buffer_job(&usart_instance,
				(uint8_t *)rx_buffer, MAX_RX_BUFFER_LENGTH);
//! [main_read]
	}
//! [main]
}
예제 #2
0
int main(void)
{
	system_init();
	configure_usart();
	configure_usart_callbacks();
	
	struct port_config pin_conf;
	port_get_config_defaults(&pin_conf);

	/* Configure LEDs as outputs, turn them off */
	pin_conf.direction  = PORT_PIN_DIR_OUTPUT;
	port_pin_set_config(LED_1_PIN, &pin_conf);
	port_pin_set_output_level(LED_1_PIN, LED_1_INACTIVE);

	/* Set buttons as inputs */
	pin_conf.direction  = PORT_PIN_DIR_INPUT;
	pin_conf.input_pull = PORT_PIN_PULL_UP;
	port_pin_set_config(BUTTON_1_PIN, &pin_conf);


#if USE_EIC == true
	configure_extint();
#endif

#if USE_INTERRUPTS == true
#  if USE_EIC == false
	configure_systick_handler();
#  else
	configure_eic_callback();
#  endif

	system_interrupt_enable_global();

	uint16_t temp;
	while (true) {
		/* Do nothing - use interrupts */
		//if (usart_read_wait(&usart_instance, &temp) == STATUS_OK)
		//{
			//while (usart_write_wait(&usart_instance, temp) != STATUS_OK);
		//}
		usart_read_buffer_job(&usart_instance, (uint8_t *)rx_buffer, 1);
		//sleepmgr_sleep(SLEEPMGR_STANDBY);
	}
#else
#  if USE_EIC == false
	while (true) {
		update_led_state();
	}
#  else
	while (true) {
		if (extint_chan_is_detected(BUTTON_1_EIC_LINE)) {
			extint_chan_clear_detected(BUTTON_1_EIC_LINE);

			update_led_state();
		}
	}
#  endif
#endif
}
예제 #3
0
static void configure_usart_callbacks(void)
{
    usart_register_callback(&usart_debug, usart_read_callback, USART_CALLBACK_BUFFER_RECEIVED);
    usart_enable_callback(&usart_debug, USART_CALLBACK_BUFFER_RECEIVED);

    // set up first callback 'job'
    usart_read_buffer_job(&usart_debug, (uint8_t *)rx_buffer, MAX_RX_BUFFER_LENGTH);
}
예제 #4
0
static void usart_read_callback(struct usart_module *const usart_module)
{
    /* Write byte to circular buffer */
    if (circularBufferWrite(&rxcb, rx_buffer[0]) != 0) {
        printf("RX error\r\n");
    }

    /* Set for usart callback */
    usart_read_buffer_job(&usart_debug, (uint8_t *)rx_buffer, MAX_RX_BUFFER_LENGTH);
}
/**
 * \internal
 * \brief USART interrupt callback function
 *
 * Called by USART driver when receiving is complete.
 *
 * * \param module USART module causing the interrupt (not used)
 */
static void usart_rx_callback(struct usart_module *const module)
{
	/* Data received */
	ui_com_tx_start();

	/* Transfer UART RX fifo to CDC TX */
	if (!udi_cdc_is_tx_ready()) {
		/* Fifo full */
		udi_cdc_signal_overrun();
		ui_com_overflow();
	} else {
		udi_cdc_putc(rx_data);
	}

	ui_com_tx_stop();

	usart_read_buffer_job(&usart_module_edbg, &rx_data, 1);

	return;
}
static void prvUARTCommandConsoleTask( void *pvParameters )
{
uint8_t ucRxedChar, ucInputIndex = 0, *pucOutputString;
static int8_t cInputString[ cmdMAX_INPUT_SIZE ], cLastInputString[ cmdMAX_INPUT_SIZE ];
portBASE_TYPE xReturned;
static struct usart_module xCDCUsart; /* Static so it doesn't take up too much stack. */

	( void ) pvParameters;

	/* A UART is used for printf() output and CLI input and output.  Note there
	is no mutual exclusion on the UART, but the demo as it stands does not
	require mutual exclusion. */
	prvConfigureUART( &xCDCUsart );

	/* Obtain the address of the output buffer.  Note there is no mutual
	exclusion on this buffer as it is assumed only one command console
	interface will be used at any one time. */
	pucOutputString = ( uint8_t * ) FreeRTOS_CLIGetOutputBuffer();

	/* Send the welcome message. */
	prvSendBuffer( &xCDCUsart, pcWelcomeMessage, strlen( ( char * ) pcWelcomeMessage ) );

	for( ;; )
	{
		/* Wait for the next character to arrive.  A semaphore is used to
		ensure no CPU time is used until data has arrived. */
		usart_read_buffer_job( &xCDCUsart, &ucRxedChar, sizeof( ucRxedChar ) );		
		if( xSemaphoreTake( xRxCompleteSemaphore, portMAX_DELAY ) == pdPASS )
		{
			/* Echo the character back. */
			prvSendBuffer( &xCDCUsart, ( uint8_t * ) &ucRxedChar, sizeof( ucRxedChar ) );

			/* Was it the end of the line? */
			if( ucRxedChar == '\n' || ucRxedChar == '\r' )
			{
				/* Just to space the output from the input. */
				prvSendBuffer( &xCDCUsart, ( uint8_t * ) pcNewLine, strlen( ( char * ) pcNewLine ) );

				/* See if the command is empty, indicating that the last command is
				to be executed again. */
				if( ucInputIndex == 0 )
				{
					/* Copy the last command back into the input string. */
					strcpy( ( char * ) cInputString, ( char * ) cLastInputString );
				}

				/* Pass the received command to the command interpreter.  The
				command interpreter is called repeatedly until it returns pdFALSE
				(indicating there is no more output) as it might generate more than
				one string. */
				do
				{
					/* Get the next output string from the command interpreter. */
					xReturned = FreeRTOS_CLIProcessCommand( cInputString, ( int8_t * ) pucOutputString, configCOMMAND_INT_MAX_OUTPUT_SIZE );

					/* Write the generated string to the UART. */
					prvSendBuffer( &xCDCUsart, ( uint8_t * ) pucOutputString, strlen( ( char * ) pucOutputString ) );

				} while( xReturned != pdFALSE );

				/* All the strings generated by the input command have been sent.
				Clear the input	string ready to receive the next command.  Remember
				the command that was just processed first in case it is to be
				processed again. */
				strcpy( ( char * ) cLastInputString, ( char * ) cInputString );
				ucInputIndex = 0;
				memset( cInputString, 0x00, cmdMAX_INPUT_SIZE );

				prvSendBuffer( &xCDCUsart, ( uint8_t * ) pcEndOfOutputMessage, strlen( ( char * ) pcEndOfOutputMessage ) );
			}
			else
			{
				if( ucRxedChar == '\r' )
				{
					/* Ignore the character. */
				}
				else if( ( ucRxedChar == '\b' ) || ( ucRxedChar == cmdASCII_DEL ) )
				{
					/* Backspace was pressed.  Erase the last character in the
					string - if any. */
					if( ucInputIndex > 0 )
					{
						ucInputIndex--;
						cInputString[ ucInputIndex ] = '\0';
					}
				}
				else
				{
					/* A character was entered.  Add it to the string
					entered so far.  When a \n is entered the complete
					string will be passed to the command interpreter. */
					if( ( ucRxedChar >= ' ' ) && ( ucRxedChar <= '~' ) )
					{
						if( ucInputIndex < cmdMAX_INPUT_SIZE )
						{
							cInputString[ ucInputIndex ] = ucRxedChar;
							ucInputIndex++;
						}
					}
				}
			}
		}
	}
}
uint8_t serial_read_data(uint8_t* data, uint16_t max_len)
{
 return usart_read_buffer_job(&usart_instance, data, max_len);
}
void uart_config(uint8_t port,usb_cdc_line_coding_t *cfg)
{
	UNUSED(port);
	/* Configure USART for unit test output */
	usart_get_config_defaults(&usart_conf);

	switch (cfg->bCharFormat) {
	case CDC_STOP_BITS_2:
		usart_conf.stopbits = USART_STOPBITS_2;
		break;

	case CDC_STOP_BITS_1_5:
		usart_conf.stopbits = USART_STOPBITS_1;
		break;

	case CDC_STOP_BITS_1:
	default:
		/* Default stop bit = 1 stop bit */
		usart_conf.stopbits = USART_STOPBITS_1;
		break;
	}

	switch (cfg->bParityType) {
	case CDC_PAR_EVEN:
		usart_conf.parity = USART_PARITY_EVEN;
		break;

	case CDC_PAR_ODD:
		usart_conf.parity = USART_PARITY_ODD;
		break;

	case CDC_PAR_MARK:
		usart_conf.parity = USART_PARITY_NONE;
		break;

	case CDC_PAR_SPACE:
		usart_conf.parity = USART_PARITY_NONE;
		break;

	case CDC_PAR_NONE:
	default:
		usart_conf.parity = USART_PARITY_NONE;
		break;
	}

	switch(cfg->bDataBits) {
	case 5:
		usart_conf.character_size = USART_CHARACTER_SIZE_5BIT;
		break;
	case 6:
		usart_conf.character_size = USART_CHARACTER_SIZE_6BIT;
		break;
	case 7:
		usart_conf.character_size = USART_CHARACTER_SIZE_7BIT;
		break;
	case 8:
	default:
		usart_conf.character_size = USART_CHARACTER_SIZE_8BIT;
		break;
	}

	/* Options for USART. */
	usart_conf.baudrate = LE32_TO_CPU(cfg->dwDTERate);
	usart_conf.mux_setting = CONF_USART_MUX_SETTING;
	usart_conf.pinmux_pad0 = CONF_USART_PINMUX_PAD0;
	usart_conf.pinmux_pad1 = CONF_USART_PINMUX_PAD1;
	usart_conf.pinmux_pad2 = CONF_USART_PINMUX_PAD2;
	usart_conf.pinmux_pad3 = CONF_USART_PINMUX_PAD3;
	usart_disable(&usart_module_edbg);
	usart_init(&usart_module_edbg, CONF_USART_BASE, &usart_conf);
	usart_enable(&usart_module_edbg);

	/* Enable interrupts */
	usart_register_callback(&usart_module_edbg, usart_tx_callback,
			USART_CALLBACK_BUFFER_TRANSMITTED);
	usart_enable_callback(&usart_module_edbg, USART_CALLBACK_BUFFER_TRANSMITTED);
	usart_register_callback(&usart_module_edbg, usart_rx_callback,
			USART_CALLBACK_BUFFER_RECEIVED);
	usart_enable_callback(&usart_module_edbg, USART_CALLBACK_BUFFER_RECEIVED);
	usart_read_buffer_job(&usart_module_edbg, &rx_data, 1);
}