int main(void)
{
	//char *msg = " There be dragons ";
	char *msg = ".";
	
	struct usart_dev *ser0 = usart_init(USART_0,
					    9600, 
					    USART_BITS_8,
					    USART_STOPBITS_1,
					    USART_PARITY_NONE,
					    0);

	struct usart_dev *ser1 = usart_init(USART_1,
					    19200, 
					    USART_BITS_8,
					    USART_STOPBITS_1,
					    USART_PARITY_NONE,
					    0);
	for (;;) {
		char r[5];
		usart_send_byte(ser0, '0');
		usart_send_byte(ser1, '1');
/*
		r[0] = *ser0->ucsra;
		r[1] = *ser0->ucsrb;
		r[2] = *ser0->ucsrc;
		usart_send_bytes(ser0, r, 3);
		r[0] = *ser1->ubrrh;
		r[1] = *ser1->ubrrl;
		usart_send_bytes(ser1, r, 2);

*/

		if (usart_data_available(ser0)) {
			char b = usart_read(ser0);
			usart_send_byte(ser0, '+');
			usart_send_byte(ser0, b);
		}

		if (usart_data_available(ser1)) {
			char b = usart_read(ser1);
			usart_send_byte(ser1, '-');
			usart_send_byte(ser1, b);
		}

		_delay_ms(10);
	}
}
/**
 * \brief Test the usart sleepwalking in active mode.
 */
static void usart_sleepwalking_test_active(void)
{
	uint32_t temp;

	puts("Test in active mode, press 's' to continue.\r");

	/* Wait for the puts operation to finish. */
	delay_ms(50);

	/* Enable UART IRQ */
	usart_enable_interrupt(CONSOLE_UART, US_IER_CMP);

	/* Enable UART interrupt */
	NVIC_EnableIRQ(CONSOLE_UART_IRQn);

	/* Set the match condition */
	usart_set_sleepwalking(CONSOLE_UART, 's', true, true, 's');

	/* Enable the sleepwalking in PMC */
	pmc_enable_sleepwalking(CONSOLE_UART_ID);

	/* Wait for the match interrupt */
	while (!cmp_flag) {
	}

	usart_read(CONSOLE_UART, &temp);
	puts("'s' character is received.\r\n\r");

}
uint8_t usart_read_buffer(uint8_t *buffer, uint8_t maxlen)
{
  uint8_t len=0;
  while (usart_rx_buffercounter>0 && len<maxlen)
  {
    *buffer++ = usart_read();
    len++;
  }
  return len;
}
Beispiel #4
0
/**
 * \brief Get voltage from user input, the input range is:
 * (1/6)*ADVREF~(5/6)*ADVREF (mv)
 */
static int16_t get_input_voltage(void)
{
	uint32_t i = 0, uc_key;
	int16_t us_value = 0;
	int8_t c_length = 0;
	int8_t ac_str_temp[5] = { 0 };

	while (1) {
		while (usart_read(CONSOLE_UART, &uc_key)) {
		}
		if (uc_key == '\n' || uc_key == '\r') {
			puts("\r");
			break;
		}

		if ('0' <= uc_key && '9' >= uc_key) {
			printf("%c", uc_key);
			ac_str_temp[i++] = uc_key;

			if (i >= 4)
				break;
		}
	}

	ac_str_temp[i] = '\0';
	/* Input string length */
	c_length = i;
	us_value = 0;

	/* Convert string to integer */
	for (i = 0; i < 4; i++) {
		if (ac_str_temp[i] != '0') {
			switch (c_length - i - 1) {
			case 0:
				us_value += (ac_str_temp[i] - '0');
				break;
			case 1:
				us_value += (ac_str_temp[i] - '0') * 10;
				break;
			case 2:
				us_value += (ac_str_temp[i] - '0') * 100;
				break;
			case 3:
				us_value += (ac_str_temp[i] - '0') * 1000;
				break;
			}
		}
	}

	if (us_value > (5 * VOLT_REF / 6) || us_value < (1 * VOLT_REF / 6)) {
		return -1;
	}

	return us_value;
}
Beispiel #5
0
/** Read character.  */
int
usart_getc (usart_t usart)
{
    int ret;
    char ch;
    
    ret = usart_read (usart, &ch, 1);
    if (ret == 1)
        return ch;
    return ret;
}
Beispiel #6
0
/**
 * \brief Application entry point for adcife example.
 *
 * \return Unused (ANSI-C compatibility).
 */
int main(void)
{
	uint32_t uc_key = 0;

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

	/* Initialize the UART console */
	configure_console();

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

	/* Set default ADCIFE test mode. */
	g_adc_test_mode.uc_trigger_mode = TRIGGER_MODE_SOFTWARE;
	g_adc_test_mode.uc_pdc_en = 1;
	g_adc_test_mode.uc_gain_en = 0;

	display_menu();

	start_dac();

	start_adc();

	while (1) {
		/* ADCIFE software trigger per 1s */
		if (g_adc_test_mode.uc_trigger_mode == TRIGGER_MODE_SOFTWARE) {
			adc_start_software_conversion(&g_adc_inst);
		}
		if (!usart_read(CONF_UART, &uc_key)) {
			adc_disable_interrupt(&g_adc_inst, ADC_SEQ_SEOC);
			display_menu();
			set_adc_test_mode();
			start_adc();
			puts("Press any key to display configuration menu.\r");
		}
		delay_ms(1000);
		if (g_uc_condone_flag == 1) {
			if(g_adc_test_mode.uc_pdc_en == 0) {
				printf("Internal DAC Voltage = %4d mv  \r\n",
						(int)(g_adc_sample_data[0] * VOLT_REF / MAX_DIGITAL));
			} else {
				printf("Internal DAC Voltage = %4d mv  \r\n",
						(int)(g_adc_sample_data[0] * VOLT_REF /
							MAX_DIGITAL));
				printf("Scaled VCC Voltage = %4d mv  \r\n",
						(int)(g_adc_sample_data[1] * VOLT_REF /
							MAX_DIGITAL));
			}
			g_uc_condone_flag = 0;
		}
	}
}
command_t usart_get_command()
{
	command_t cmd;
	cmd.code = usart_getword();
	cmd.length = usart_getword();
	if (cmd.length > 0)
	{
		cmd.data = (char *)malloc(cmd.length);
		usart_read(cmd.data, cmd.length);
	}
	return cmd;
}
Beispiel #8
0
/**
 * \brief
 *
 * \param
 *
 * \return void
 */
void USART1_Handler(void)
{
	uint32_t ul_status;

	/* Read USART Status. */
	ul_status = usart_get_status( BOARD_USART1 );

	/* Receive buffer is full. */
	if (ul_status & US_CSR_RXRDY)
	{
		usart_read( BOARD_USART1, (uint32_t *)&gs_ul_read_buffer[0] );
	}
}
Beispiel #9
0
/** Receives a byte via the hardware USART, blocking until data is received or timeout expired.
 *
 *  \return Received byte from the USART
 */
uint8_t XPROGTarget_ReceiveByte(void)
{
	uint32_t dummy_read;
		
	/* Switch to Rx mode if currently in Tx mode */
	if (IsSending)
	  XPROGTarget_SetRxMode();

	/* Wait until a byte has been received before reading */
	//usart_getchar(USART_PDI, &dummy_read);
	while((usart_read(USART_PDI, &dummy_read) == 1) && (TimeoutTicksRemaining));

	return dummy_read;
}
int main(void) {
    int i = 10;

    GPIO_InitTypeDef gpio_init;
    USART_InitTypeDef usart_init;
    USART_ClockInitTypeDef usart_clk_init;

    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);

    // PA9 = Tx, PA10 = Rx
    gpio_init.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
    gpio_init.GPIO_Mode = GPIO_Mode_AF;
    gpio_init.GPIO_Speed = GPIO_Speed_40MHz;
    gpio_init.GPIO_OType = GPIO_OType_PP;
    gpio_init.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_Init(GPIOA, &gpio_init);

    GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);

    USART_ClockStructInit(&usart_clk_init);
    USART_ClockInit(USART1, &usart_clk_init);

    usart_init.USART_BaudRate =            9600;
    usart_init.USART_WordLength =          USART_WordLength_8b;
    usart_init.USART_StopBits =            USART_StopBits_1;
    usart_init.USART_Parity =              USART_Parity_No ;
    usart_init.USART_Mode =                USART_Mode_Rx | USART_Mode_Tx;
    usart_init.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    USART_Init(USART1, &usart_init);
    USART_Cmd(USART1,ENABLE);

    while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET) {}

    while (1) {

        if ( usart_available() ) // data available
        {
            usart_print( "Data Available: " );
            uint8_t ch = usart_read();
            usart_write(ch);
            usart_print( "\r\n" );
        }

    }


    return 0;
}
Beispiel #11
0
int Ax12Class::readPosition(unsigned char ID)
{	
    TChecksum = (ID + AX_POS_LENGTH  + AX_READ_DATA + AX_PRESENT_POSITION_L + AX_BYTE_READ_POS);
    while ( TChecksum >= 255){
		TChecksum -= 255;     
    }
    Checksum = 255 - TChecksum;
    
    digitalWrite(Direction_Pin,HIGH); 
    usart_write(AX_START);
    usart_write(AX_START);
    usart_write(ID);
    usart_write(AX_POS_LENGTH);
    usart_write(AX_READ_DATA);
    usart_write(AX_PRESENT_POSITION_L);
    usart_write(AX_BYTE_READ_POS);
    usart_write(Checksum);
    delayMicroseconds(TX_DELAY_TIME);
    digitalWrite(Direction_Pin,LOW);     // Set Rx Mode
	
    Position_Long_Byte = 0;
	Time_Counter = 0;
    while(usart_available() < 7 & Time_Counter < TIME_OUT){
		Time_Counter++;
		delay(1);
		if( usart_peek() != 255 ){
			usart_read();
		}   
    }
	
    while (usart_available() > 0){
		Incoming_Byte = usart_read();
		if ( Incoming_Byte == 255 & usart_peek() == 255 ){
			usart_read();                            // Start Bytes
			usart_read();                            // Ax-12 ID
			usart_read();                            // Length
			if( (Error_Byte = usart_read()) != 0 )   // Error
				return (Error_Byte*(-1));
    
			Position_Low_Byte = usart_read();            // Position Bytes
			Position_High_Byte = usart_read();
			Position_Long_Byte = Position_High_Byte << 8; 
			Position_Long_Byte = Position_Long_Byte + Position_Low_Byte;
		}
    }
	return (Position_Long_Byte);     // Returns the read position
}
void main_task(void *args) {
    uint8_t buf = 0;

    usart_write(DEBUG_USART, (uint8_t *)"Enter some text:\r\n", 18);

    for(;;) {
        usart_read(DEBUG_USART, &buf, 1);
        usart_write(DEBUG_USART, &buf, 1);

        if (buf == '\r') {
            buf = '\n';
            usart_write(DEBUG_USART, &buf, 1);
        }
    }
}
Beispiel #13
0
int main()
{
    char str[1024] = {0};
    int len = 0;

	SysTick_Config(720000);

    usart_init();
    usart_write("Hello, rnrOS!\r\n", 15);
    while (1){
        len = usart_read(str, 1024);
        usart_write(str, len);
        len = 0;
    }
}
Beispiel #14
0
/**
 * \brief Cold reset.
 */
void iso7816_cold_reset(void)
{
	uint32_t i;
	uint32_t ul_data;

	/* tb: wait 400 cycles */
	for (i = 0; i < (RST_WAIT_TIME * (g_ul_clk / 1000000)); i++) {
	}

	usart_read(ISO7816_USART, &ul_data);
	usart_reset_status(ISO7816_USART);
	usart_reset_iterations(ISO7816_USART);
	usart_reset_nack(ISO7816_USART);

	iso7816_icc_power_on();
}
Beispiel #15
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();
		}
	}
}
Beispiel #16
0
/*!
 * \brief main function : perform several read/write accesses to the flash then
 * lock and unlock a page in the flash.
 */
int main(void)
{
	uint32_t key;

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

	/* Initialize the console uart */
	configure_console();

	/* Output example information */
	printf("-- FLASHCALW Example --\r\n");
	printf("-- %s\n\r", BOARD_NAME);
	printf("-- Compiled: %s %s --\n\r", __DATE__, __TIME__);

	/* Apply the example to the flash array. */
	flash_rw_example(
		"\x0C=== Using a piece of the flash array as NVRAM ===\r\n",
		(nvram_data_t *)NVRAM_PAGE_ADDRESS);

	/* Apply the example to the user page. */
	flash_rw_example(
		"\r\n\r\n=== Using a piece of the user page as NVRAM ===\r\n",
		(nvram_data_t *)USER_PAGE_ADDRESS);

	/* Flash lock example */
	flash_protect_example();

	printf("-I- Good job!\n\r"
		"-I- Now set the security bit \n\r"
		"-I- Press any key to continue to see what happened...\n\r");
	while (0 != usart_read(CONF_UART, &key));

	/* Set security bit */
	printf("-I- Setting security bit \n\r");
	flashcalw_set_security_bit();

	printf("-I- All tests done\n\r");

	while (true) {
	}
}
Beispiel #17
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);
	}
}
Beispiel #18
0
int Ax12Class::readVoltage(unsigned char ID)
{    
    TChecksum = (ID + AX_VOLT_LENGTH  + AX_READ_DATA + AX_PRESENT_VOLTAGE + AX_BYTE_READ);
    while ( TChecksum >= 255){
		TChecksum -= 255;     
    }
    Checksum = 255 - TChecksum;
    
    digitalWrite(Direction_Pin,HIGH); 
    usart_write(AX_START);
    usart_write(AX_START);
    usart_write(ID);
    usart_write(AX_VOLT_LENGTH);
    usart_write(AX_READ_DATA);
    usart_write(AX_PRESENT_VOLTAGE);
    usart_write(AX_BYTE_READ);
    usart_write(Checksum);
	delayMicroseconds(TX_DELAY_TIME);
    digitalWrite(Direction_Pin,LOW);     // Set Rx Mode 
	
    Voltage_Byte = 0;
	Time_Counter = 0;
    while(usart_available() < 6 & Time_Counter < TIME_OUT){
		Time_Counter++;
		delay(1);
		if( usart_peek() != 255 ){
			usart_read();
		}   
    }
	
    while (usart_available() > 0){
		Incoming_Byte = usart_read();
		if ( Incoming_Byte == 255 & usart_peek() == 255 ){
			usart_read();                            // Start Bytes
			usart_read();                            // Ax-12 ID
			usart_read();                            // Length
			if( (Error_Byte = usart_read()) != 0 )   // Error
				return (Error_Byte*(-1));
			Voltage_Byte = usart_read();             // Voltage
		}
    }
	return (Voltage_Byte);               // Returns the read Voltage
}
Beispiel #19
0
/**
 * \brief Get a character from ISO7816.
 *
 * \param p_char_received Pointer for store the received char.
 *
 * \return 0: if timeout else status of US_CSR.
 */
static uint32_t iso7816_get_char(uint8_t *p_char_received)
{
	uint32_t ul_data;
	uint32_t ul_status;
	uint32_t ul_timeout = 0;

	if (gs_uc_state == USART_SEND) {
		while ((usart_get_status(ISO7816_USART) & US_CSR_TXEMPTY) == 0) {
		}
		usart_reset_status(ISO7816_USART);
		usart_reset_iterations(ISO7816_USART);
		usart_reset_nack(ISO7816_USART);
		gs_uc_state = USART_RCV;
	}

	/* Wait USART ready for reception. */
	while (((usart_get_status(ISO7816_USART) & US_CSR_RXRDY) == 0)) {
		if (ul_timeout++ > RX_TIMEOUT * (g_ul_clk / 1000000)) {
			return (0);
		}
	}

	/* At least one complete character has been received and US_RHR has not yet been read. */
	usart_read(ISO7816_USART, &ul_data);
	/* ISO7816 only has 8 bits data. */
	*p_char_received = 0xFF & ul_data;

	ul_status = usart_get_status(ISO7816_USART) & (US_CSR_OVRE |
			US_CSR_FRAME | US_CSR_PARE | US_CSR_TIMEOUT |
			US_CSR_NACK | US_CSR_ITER);

	if (ul_status != 0) {
		usart_reset_status(ISO7816_USART);
	}

	/* Return status. */
	return (ul_status);
}
/**
 *  \brief Handler for USART interrupt.
 *
 */
void USART_Handler(void)
{
	uint32_t ul_status;
	uint8_t uc_char;

	/* Read USART status. */
	ul_status = usart_get_status(BOARD_USART);

	/*transmit interrupt rises*/
	if(ul_status & (US_IER_TXRDY | US_IER_TXEMPTY)) {
		usart_disable_interrupt(BOARD_USART, (US_IER_TXRDY | US_IER_TXEMPTY));
	}

	/*receive interrupt rise, store character to receiver buffer*/
	if((g_state == RECEIVING) && (usart_read(BOARD_USART, (uint32_t *)&uc_char) == 0)) {
		*p_revdata++ = uc_char;
		g_ulcount++;
		if(g_ulcount >= BUFFER_SIZE) {
			g_state = RECEIVED;
			usart_disable_interrupt(BOARD_USART, US_IER_RXRDY);
		}
	}
}
/**
 * \brief USART IRQ handler.
 *
 * Interrupt handler for USART. After reception is done, set g_ul_recv_done to true,
 * and if transmission is done, set g_ul_sent_done to true.
 *
 */
void USART0_Handler(void)
{
	uint32_t ul_status;
	uint8_t uc_char;

	/* Read USART Status. */
	ul_status = usart_get_status(BOARD_USART);

	if(ul_status & (US_IER_TXRDY | US_IER_TXEMPTY)) {
		usart_disable_interrupt(BOARD_USART, (US_IER_TXRDY | US_IER_TXEMPTY));
	}

	/* Receive register is full. */
	if((g_uc_state == STATE_READ) && (usart_read(BOARD_USART, (uint32_t *)&uc_char) == 0)) {
		*p_revdata++ = uc_char;
		g_ulcount++;
		if(g_ulcount >= BUFFER_SIZE) {
			usart_disable_interrupt(BOARD_USART, US_IER_RXRDY);
			g_ul_recv_done = true;
		}
	}

}
Beispiel #22
0
// Interrupt vector
void interrupt() {
    // If there is an external interrupt
    if (INTCON.INTF) {
        delay_ms(25); // Debounce delay

        // Determine the sound to be played (debugging purposes)
        rx_data = 0xFF;
        if (PORTA.F0 && PORTA.F1) {
            rx_data = ITS_MARIO;
        } else if (!PORTA.F0 && PORTA.F1) {
            rx_data = OUTTA_TIME;
        } else if (PORTA.F0 && !PORTA.F1) {
            rx_data = DOWN_PIPE;
        }

        INTCON.INTF = 0; // Clear interrupt flag
    }

    // If there is an unread byte
    if (PIR1.RCIF) {
        rx_data = usart_read();
        wave_scan = 0x80000000; // Stop on-going sounds
    }
}
Beispiel #23
0
int Ax12Class::read_error(void)
{
	Time_Counter = 0;
	while(usart_available() < 5 & Time_Counter < TIME_OUT){  // Wait for Data
		Time_Counter++;
		delay(1);
		if( usart_peek() != 255 ){
		usart_read();
		}
	}
	
	while (usart_available() > 0){
		Incoming_Byte = usart_read();
		if ( Incoming_Byte == 255 & usart_peek() == 255 ){
			usart_read();                                    // Start Bytes
			usart_read();                                    // Ax-12 ID
			usart_read();                                    // Length
			Error_Byte = usart_read();                       // Error
				return (Error_Byte);
		}
	}
	return (-1);											 // No Ax Response
}
Beispiel #24
0
static void handle_data(void){
    #ifdef USE_STTERM
    unsigned char tmp_c;
    #endif

    uint32_t tmp=0;
    char crc=0;

    #ifdef USE_STTERM
    if(stlinky_rx_ready()){
        scanf("%c", &tmp_c);
        INPUT[k] = tmp_c;
        gpio_toggle(GPIOD, GPIO15);
    #else
    if(usart_has_data()){
        INPUT[k] = usart_read();
    #endif
    k++;
    if(k==1 && INPUT[0]!=0x53){ // 'S'
        #ifdef DEBUG
        printf("ERROR AT START-BYTE %d\r\n",INPUT[0]);
        #endif
        k=0;
    }
    if(k==10 && INPUT[9]!=0x45){ // 'E'
        #ifdef DEBUG
        printf("ERROR AT END-BYTE %d\r\n",INPUT[8]);
        #endif
        k=0;
    }
    if(k==11){
        #ifdef DEBUG
        printf("Package: ");
        for(tmp=0;tmp<10;tmp++) printf("%d ",INPUT[tmp]);
        printf("\n");
        #endif
        for(tmp=0;tmp<8;tmp++) KEYS[tmp] = INPUT[tmp+1];
        crc = getCRC(KEYS,8);
        if(crc==INPUT[10]){
            #ifdef DEBUG
            printf("INPUT OK!\r\n");
                #endif
                if(usb_ready == 3) usb_send_packet(KEYS, 8);
            }else{
                #ifdef DEBUG
                printf("ERROR AT CRC-BYTE %d vs %d\r\n",INPUT[10],crc);
                #endif
            }
            k=0;
        }
    }
}
void tim3_isr(void){
    if(timer_get_flag(TIM3, TIM_SR_UIF)){
        handle_data();
        timer_clear_flag(TIM3, TIM_SR_UIF);
    }
}

void tim4_isr(void){
    if(timer_get_flag(TIM4, TIM_SR_UIF)){
        if(usb_ready==3){
            gpio_toggle(GPIOD,GPIO14);
            KEYS[0]=0;
            KEYS[1]=0;
            KEYS[3]=0;
            KEYS[4]=0;
            KEYS[5]=0;
            KEYS[6]=0;
            KEYS[7]=0;

            KEYS[2]=0x04;
            usb_send_packet(KEYS, 8);
            KEYS[2]=0x00;
            usb_send_packet(KEYS, 8);
            KEYS[2]=0x05;
            usb_send_packet(KEYS, 8);
            KEYS[2]=0x00;
            usb_send_packet(KEYS, 8);
        }
        timer_clear_flag(TIM4, TIM_SR_UIF);
    }
}

void tim5_isr(void){
    if(timer_get_flag(TIM5, TIM_SR_UIF)){
        gpio_toggle(GPIOD, GPIO15);
        timer_clear_flag(TIM5, TIM_SR_UIF);
    }
}

int main(void) {
    iwdg_reset();
    iwdg_set_period_ms(5);
    iwdg_start();

    rcc_clock_setup_hse_3v3(&hse_8mhz_3v3[CLOCK_3V3_168MHZ]);
    rcc_periph_clock_enable(RCC_GPIOD);
    systick_setup();

    gpio_mode_setup(GPIOD, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO12 | GPIO13 | GPIO14 | GPIO15);
    gpio_clear(GPIOD, GPIO12 | GPIO13 | GPIO14 | GPIO15);

    iwdg_set_period_ms(500);
    iwdg_reset();
    msleep(400);
    iwdg_set_period_ms(5);
    iwdg_reset();

    gpio_set(GPIOD, GPIO15);

//    msleep(100000); /* SLEEEEEEEEEEEEEEEEEEEEEEP */

    #ifdef USE_STTERM
    stlinky_init();
    #else
    usart_setup();
    #endif

    timer2_setup(100);

    usb_setup();

    //timer4_setup(1);
    timer3_setup(100);
    timer5_setup(2);

//    gpio_set(GPIOD, GPIO12 | GPIO13 | GPIO14 | GPIO15);

    while(1) {
        iwdg_reset();
    }

    return 0;
}
Beispiel #25
0
int main(void) {
	uint16_t bits;
	uint32_t intval = 40;
	uint32_t tnow;
	char tmp[92];
	RCC_ClocksTypeDef RCC_Clocks;
	uint16_t i;
	
	TIM2_timer_start();

	usart_begin(&USerial3, USART3, PC11, PC10, 19200);

	usart_print(&USerial3, 
			"Happy are those who know they are spiritually poor; \n"
			"The kingdom of heaven belongs to them!\n");
	usart_flush(&USerial3);
	
	RCC_GetClocksFreq(&RCC_Clocks);

	sprintf(tmp, "SYSCLK = %ul\n", RCC_Clocks.SYSCLK_Frequency);
	usart_print(&USerial3, tmp);
	sprintf(tmp, "PCLK1 = %ul\n", RCC_Clocks.PCLK1_Frequency);
	usart_flush(&USerial3);

	GPIOMode(PinPort(PD12),
			(PinBit(PD12) | PinBit(PD13) | PinBit(PD14) | PinBit(PD15)), OUTPUT,
			FASTSPEED, PUSHPULL, NOPULL);
			/*
	spi_begin(SPI2, PB13, PB14, PB15, PB12);
	digitalWrite(PB12, HIGH);
*/
	I2C1_Init();
/*
	i2c_begin(&Wire1, PB9, PB8, 100000);
	lcd.init(&Wire1);
	lcd.begin();
	lcd.setContrast(46);
	lcd.print("Yappee!");       // Classic Hello World!
*/
	bits = GPIO_ReadOutputData(GPIOD );
	GPIOWrite(GPIOD, PinBit(PD13) | (bits & 0x0fff));
	delay_ms(intval);
	tnow = millis() / 1000;
	while (tnow == millis() / 1000)
		;
	tnow = millis() / 1000;

	while (1) {
		bits = GPIO_ReadOutputData(GPIOD );

		GPIOWrite(GPIOD, PinBit(PD13) | (bits & 0x0fff));
		delay_ms(intval);
		GPIOWrite(GPIOD, PinBit(PD14) | (bits & 0x0fff));
		delay_ms(intval);
		GPIOWrite(GPIOD, PinBit(PD15) | (bits & 0x0fff));
		delay_ms(intval);
		GPIOWrite(GPIOD, PinBit(PD12) | (bits & 0x0fff));
		delay_ms(intval);
		//
		bits &= 0x0fff;
		switch ((tnow % 60) / 15) {
		case 3:
			bits |= PinBit(PD12);
		case 2:
			bits |= PinBit(PD15);
		case 1:
			bits |= PinBit(PD14);
		case 0:
		default:
			bits |= PinBit(PD13);
			break;
		}
		GPIOWrite(GPIOD, bits);

		while (tnow == millis() / 1000);
		tnow = millis() / 1000;

		//Serial3.print(tmp);
		sprintf(tmp, "%04ld\n", millis());
		usart_print(&USerial3, tmp);

		/*
		digitalWrite(PB12, LOW);
		spi_transfer(SPI2, (uint8_t *) tmp, 8);
		digitalWrite(PB12, HIGH);
*/
		i = 0;
		if (usart_available(&USerial3) > 0) {
			while (usart_available(&USerial3) > 0 && i < 92) {
				tmp[i++] = (char) usart_read(&USerial3);
			}
			tmp[i] = 0;
			usart_print(&USerial3, "> ");
			usart_print(&USerial3, tmp);
			usart_print(&USerial3, "\n");
		}

	}
	return 0;
}
Beispiel #26
0
/**
 *  \brief ACC example application entry point.
 *
 *  \return Unused (ANSI-C compatibility).
 */
int main(void)
{
	uint32_t uc_key;
	int16_t s_volt = 0;
	uint32_t ul_value = 0;
	volatile uint32_t ul_status = 0x0;
	int32_t l_volt_dac0 = 0;

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

	/* Initialize debug console */
	configure_console();

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

	/* Initialize DACC */
	/* Enable clock for DACC */
	pmc_enable_periph_clk(ID_DACC);
	/* Reset DACC registers */
	dacc_reset(DACC);
	/* External trigger mode disabled. DACC in free running mode. */
	dacc_disable_trigger(DACC, DACC_CHANNEL_0);
	/* Half word transfer mode */
	dacc_set_transfer_mode(DACC, 0);
#if (SAM3S) || (SAM3XA)
	/* Power save:
	 * sleep mode  - 0 (disabled)
	 * fast wakeup - 0 (disabled)
	 */
	dacc_set_power_save(DACC, 0, 0);
#endif

	/* Enable output channel DACC_CHANNEL */
	dacc_enable_channel(DACC, DACC_CHANNEL_0);
	/* Setup analog current */
	dacc_set_analog_control(DACC, DACC_ANALOG_CONTROL);

	/* Set DAC0 output at ADVREF/2. The DAC formula is:
	 *
	 * (5/6 * VOLT_REF) - (1/6 * VOLT_REF)     volt - (1/6 * VOLT_REF)
	 * ----------------------------------- = --------------------------
	 *              MAX_DIGITAL                       digit
	 *
	 * Here, digit = MAX_DIGITAL/2
	 */
	dacc_write_conversion_data(DACC, MAX_DIGITAL / 2, DACC_CHANNEL_0);
	l_volt_dac0 = (MAX_DIGITAL / 2) * (2 * VOLT_REF / 3) / MAX_DIGITAL +
			VOLT_REF / 6;

	/* Enable clock for AFEC */
	afec_enable(AFEC0);

	struct afec_config afec_cfg;

	afec_get_config_defaults(&afec_cfg);
	/* Initialize AFEC */
	afec_init(AFEC0, &afec_cfg);

	struct afec_ch_config afec_ch_cfg;
	afec_ch_get_config_defaults(&afec_ch_cfg);
	afec_ch_cfg.gain = AFEC_GAINVALUE_0;
	afec_ch_set_config(AFEC0, AFEC_CHANNEL_POTENTIOMETER, &afec_ch_cfg);
	/*
	 * Because the internal ADC offset is 0x200, it should cancel it and shift
	 * down to 0.
	 */
	afec_channel_set_analog_offset(AFEC0, AFEC_CHANNEL_POTENTIOMETER, 0x200);

	afec_set_trigger(AFEC0, AFEC_TRIG_SW);

	/* Enable channel for potentiometer. */
	afec_channel_enable(AFEC0, AFEC_CHANNEL_POTENTIOMETER);

	/* Enable clock for ACC */
	pmc_enable_periph_clk(ID_ACC);
	
	/* Initialize ACC */
	acc_init(ACC, ACC_MR_SELPLUS_AFE0_AD0, ACC_MR_SELMINUS_DAC0,
			ACC_MR_EDGETYP_ANY, ACC_MR_INV_DIS);

	/* Enable ACC interrupt */
	NVIC_EnableIRQ(ACC_IRQn);

	/* Enable */
	acc_enable_interrupt(ACC);

	dsplay_menu();

	while (1) {
		while (usart_read(CONSOLE_UART, &uc_key)) {
		}

		printf("input: %c\r\n", uc_key);

		switch (uc_key) {
		case 's':
		case 'S':
			printf("Input DAC0 output voltage (%d~%d mv): ",
					(VOLT_REF / 6), (VOLT_REF * 5 / 6));
			s_volt = get_input_voltage();
			puts("\r");

			if (s_volt > 0) {
				l_volt_dac0 = s_volt;
				/* The DAC formula is:
				 *
				 * (5/6 * VOLT_REF) - (1/6 * VOLT_REF)     volt - (1/6 * VOLT_REF)
				 * ----------------------------------- = --------------------------
				 *              MAX_DIGITAL                       digit
				 *
				 */
				ul_value = ((s_volt - (VOLT_REF / 6))
					* (MAX_DIGITAL * 6) / 4) / VOLT_REF;
				dacc_write_conversion_data(DACC, ul_value, DACC_CHANNEL_0);
				puts("-I- Set ok\r");
			} else {
				puts("-I- Input voltage is invalid\r");
			}
			break;
		case 'v':
		case 'V':
			/* Start conversion */
			afec_start_software_conversion(AFEC0);
			ul_status = afec_get_interrupt_status(AFEC0);
			while ((ul_status & AFEC_ISR_EOC0) != AFEC_ISR_EOC0) {
				ul_status = afec_get_interrupt_status(AFEC0);
			}
			/* Conversion is done */
			ul_value = afec_channel_get_value(AFEC0, AFEC_CHANNEL_POTENTIOMETER);

			/*
			 * Convert AFEC sample data to voltage value:
			 * voltage value = (sample data / max. resolution) * reference voltage
			 */
			s_volt = (ul_value * VOLT_REF) / MAX_DIGITAL;
			printf("-I- Voltage on potentiometer(AD0) is %d mv\n\r", s_volt);
			printf("-I- Voltage on DAC0 is %ld mv \n\r", (long)l_volt_dac0);
			break;
			
		case 'm':
		case 'M':
			dsplay_menu();
			break;
		}
	}
}
Beispiel #27
0
/**
 * \brief
 *
 * \param
 *
 * \return void
 */
void uart_config(void)
{
	const sam_uart_opt_t uart_settings = {
		.ul_mck = sysclk_get_peripheral_hz(),
		.ul_baudrate = 19200,
		.ul_mode = UART_MR_PAR_NO
	};

	sysclk_enable_peripheral_clock(ID_UART);
	uart_init( BOARD_UART, &uart_settings );

}
/**
 * \brief
 *
 * \param
 *
 * \return void
 */
void usart0_config(void)
{

	const sam_usart_opt_t usart_0_settings = {
		.baudrate = 19200,
		.char_length = US_MR_CHRL_8_BIT,
		.parity_type = US_MR_PAR_NO,
		.stop_bits = US_MR_NBSTOP_1_BIT,
		.channel_mode = US_MR_CHMODE_NORMAL,
		/* This field is only used in IrDA mode. */
		.irda_filter = 0
	};
	sysclk_enable_peripheral_clock(BOART_ID_USART0);
	usart_init_rs232(BOARD_USART0, &usart_0_settings, sysclk_get_peripheral_hz());

	/* Disable all the interrupts. */
	usart_disable_interrupt(BOARD_USART0, 0xffffffff);

	/* Enable the receiver and transmitter. */
	usart_enable_tx(BOARD_USART0);
	usart_enable_rx(BOARD_USART0);

	/* Configure and enable interrupt of USART. */
	NVIC_EnableIRQ(USART0_IRQn);

	usart_enable_interrupt(BOARD_USART0, US_IER_RXRDY);
}

/**
 * \brief
 *
 * \param
 *
 * \return void
 */
void usart1_config(void)
{
	const sam_usart_opt_t usart_1_settings = {
		.baudrate = 115200,
		.char_length = US_MR_CHRL_8_BIT,
		.parity_type = US_MR_PAR_NO,
		.stop_bits = US_MR_NBSTOP_1_BIT,
		.channel_mode = US_MR_CHMODE_NORMAL,
		/* This field is only used in IrDA mode. */
		.irda_filter = 0
	};

	sysclk_enable_peripheral_clock( BOART_ID_USART1 );
	usart_init_rs232( BOARD_USART1, &usart_1_settings, sysclk_get_peripheral_hz() );

	/* Disable all the interrupts. */
	usart_disable_interrupt(BOARD_USART1, 0xffffffff);

	/* Enable the receiver and transmitter. */
	usart_enable_tx(BOARD_USART1);
	usart_enable_rx(BOARD_USART1);

	/* Configure and enable interrupt of USART. */
	NVIC_EnableIRQ( USART1_IRQn );

	usart_enable_interrupt( BOARD_USART1, US_IER_RXRDY);
}

/**
 * \brief
 *
 * \param uxPriority
 *
 * \return void
 */
void vStartUartTaskLauncher( unsigned portBASE_TYPE uxPriority )
{
	/* Spawn the Sentinel task. */
	xTaskCreate( vUartTask, (const signed portCHAR *)"SPILAUNCH",
				TASK_RADIO_STACK_SIZE, NULL, uxPriority,
				(xTaskHandle *)NULL );
}

/**
 * \brief UART handle task..
 *
 * \param
 * \param
 *
 * \return None
 */
portTASK_FUNCTION_PROTO( vUartTask, pvParameters )
{
	uint8_t	i, sms_text[] = "AT\r";

	(void)pvParameters;

	/* Initialize UART model.*/
	uart_config();
	usart0_config();
	usart1_config();

	gpio_set_pin_low( PIO_PA17_IDX );

	for (;;)
	{
		vTaskDelay(1000);

		uart_write( BOARD_UART, '1' );
		usart_serial_write_packet( BOARD_USART1, sms_text, sizeof(sms_text) );
	}
}

/**
 * \brief
 *
 * \param
 *
 * \return void
 */
void USART0_Handler(void)
{
	uint32_t ul_status;

	/* Read USART Status. */
	ul_status = usart_get_status( BOARD_USART0 );

	/* Receive buffer is full. */
	if (ul_status & US_CSR_RXRDY)
	{
		usart_read( BOARD_USART0, (uint32_t *)&gs_ul_read_buffer[0] );
	}
}
Beispiel #28
0
/**
 * \brief Application entry point for PARC example.
 *
 * \return Unused (ANSI-C compatibility).
 */
int main(void)
{
	uint32_t uc_key;

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

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

	//! [parc_variables]	
	struct parc_module module_inst;
	struct parc_config config;
	//! [parc_variables]	

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

	/* Configure TC. */
	configure_tc();
	/* Start timer. */
	tc_start(TC0, 0);

	//! [parc_get_defaults]	
	// Get default configuration
	parc_get_config_defaults(&config);
	//! [parc_get_defaults]	
	printf("Press y to sample the data when both data enable pins are enabled.\r\n");
	printf("Press n to sample the data, don't care the status of the data enable pins.\r\n");
	uc_key = 0;
	while ((uc_key != 'y') && (uc_key != 'n')) {
		usart_read(CONF_UART, &uc_key);
	}
	if (uc_key == 'y') {
		/* Sample the data when both data enable pins are enabled. */
		config.smode = PARC_SMODE_PCEN1_AND_PCEN2_H;
		ioport_set_pin_level(PIN_PCEN1_INPUT, IOPORT_PIN_LEVEL_HIGH);
		ioport_set_pin_level(PIN_PCEN2_INPUT, IOPORT_PIN_LEVEL_HIGH);
		printf("Receive data when both data enable pins are enabled.\r\n");
	} else {
		/* Sample the data, don't care the status of the data enable pins. */
		config.smode = PARC_SMODE_ALWAYS;
		printf("Receive data, don't care the status of the data enable pins.\r\n");
	}

	printf("Press y to sample all the data.\r\n");
	printf("Press n to sample the data only one out of two.\r\n");
	uc_key = 0;
	while ((uc_key != 'y') && (uc_key != 'n')) {
		usart_read(CONF_UART, &uc_key);
	}
	if (uc_key == 'y') {
			/* Sample all the data. */
		config.capture_mode = PARC_BOTH_CAPTURE;
		printf("All data are sampled.\r\n");
	} else {
		/* Sample the data only one out of two. */
		config.capture_mode = PARC_EVEN_CAPTURE;
		printf("Only one out of two data is sampled, with an even index.\r\n");
	}

	//! [parc_init_enable_and_start]
	//! [parc_init_enable_and_start_1]
	// Initialize PARC.
	parc_init(&module_inst, PARC, &config);
	//! [parc_init_enable_and_start_1]
	
	//! [parc_init_enable_and_start_2]
	// Enable the PARC
	parc_enable(&module_inst);
	
	// Start capture.
	parc_start_capture(&module_inst);
	//! [parc_init_enable_and_start_2]
	//! [parc_init_enable_and_start]

	/* Enable PDCA module clock */
	pdca_enable(PDCA);
	/* Init PDCA channel with the pdca_options.*/
	pdca_channel_set_config(PDCA_PARC_CHANNEL, &PDCA_PARC_OPTIONS);

	/* Set callback for PDCA interrupt. */
	pdca_channel_set_callback(PDCA_PARC_CHANNEL,
			pdca_parc_callback,PDCA_0_IRQn,1,PDCA_IER_RCZ);

	/* Enable PDCA channel, start receiving data. */
	pdca_channel_enable(PDCA_PARC_CHANNEL);
	/* Start read PARC data capture via PDCA. */
	pdca_channel_write_load(PDCA_PARC_CHANNEL,
			(void *)gs_puc_buffer, BUFFER_SIZE);
	/* Main loop. */
	while(1) {
	}
}
char usart_read_wait(struct usart_dev *dev)
{
	while (!(usart_data_available(dev)))
		;
	return usart_read(dev);
}