aboutsummaryrefslogtreecommitdiffhomepage
path: root/pyecsca/codegen/hal/xmega/uart.c
blob: 692a019795e0ed07fc63ff0b99a0de49be018bc9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "uart.h"

/*! Define that selects the Usart used in example. */
#define USART USARTC0

#define TIMEOUT 0
#define BYTE_REC 1

void init_uart0(void)
    {
    /* This PORT setting is only valid to USARTC0 if other USARTs is used a
     * different PORT and/or pins is used. */
    /* PIN3 (TXD0) as output. */
    PORTC.DIRSET = PIN3_bm;

    /* PC2 (RXD0) as input. */
    PORTC.DIRCLR = PIN2_bm;

    /* USARTC0, 8 Data bits, No Parity, 1 Stop bit. */
    USART_Format_Set(&USART, USART_CHSIZE_8BIT_gc, USART_PMODE_DISABLED_gc, false);

    /* Set Baudrate to 9600 bps:
     * Use the default I/O clock fequency that is 2 MHz.
     * Do not use the baudrate scale factor
     *
     * Baudrate select = (1/(16*(((I/O clock frequency)/Baudrate)-1)
     *                 = 12
     */
    USART_Baudrate_Set(&USART, 11, 0);

    /* Enable both RX and TX. */
    USART_Rx_Enable(&USART);
    USART_Tx_Enable(&USART);
    }

unsigned char input_ch_w_timeout_0(char * data, volatile unsigned int timeout)
    {
    unsigned int				timeout_counter = 0;


    //check if a byte has been recieved or if the timeout has been excedded
    while (timeout_counter != timeout)
        {
        if (USART_IsRXComplete(&USART))
            {
            *data = USART_GetChar(&USART);
            return BYTE_REC;
            }
        timeout_counter++;
        }

    return TIMEOUT;
    }

char input_ch_0(void)
    {
    //check if a byte has been recieved or if the timeout has been excedded
    while (!USART_IsRXComplete(&USART))
        {
        continue;
        }
    return USART_GetChar(&USART);;
    }
	
void output_ch_0(char data)
    {
    while(!USART_IsTXDataRegisterEmpty(&USART));
    USART_PutChar(&USART, data);
    return;
    }