File: serial.c

package info (click to toggle)
simulavr 1.0.0%2Bgit20160221.e53413b-1
  • links: PTS
  • area: main
  • in suites: buster
  • size: 5,748 kB
  • sloc: cpp: 35,491; python: 6,991; ansic: 3,567; makefile: 1,072; sh: 653; asm: 414; tcl: 320
file content (75 lines) | stat: -rw-r--r-- 1,795 bytes parent folder | download | duplicates (2)
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
71
72
73
74
75
/*
 *  $Id$
 */

#include <avr/io.h>
#include <stdlib.h>
//include <sig-avr.h>
#include <avr/interrupt.h>

#include "serial.h"
#include "StdDefs.h"


// 02-Sep-2008 K. Schwichtenberg Clearing of UDRIE corrected

#define ESC 0x1b
#define BUFF_SIZE 64

unsigned char CLR[] = {ESC, '[','H', ESC, '[', '2', 'J',0};

unsigned char UART_buffer[BUFF_SIZE];
unsigned char *inptr, *outptr;
unsigned char buff_cnt;


void init_uart(void)
{
    inptr =  UART_buffer;
    outptr = UART_buffer;
    buff_cnt = 0;
}

void clr(void)
{
    //putstr(CLR);                                // Send a 'clear screen' to a VT100 terminal
}


int putchar(int c)
{
    if (buff_cnt<BUFF_SIZE)
    {
        *inptr = c;                             // Put character into buffer
        inptr++;                                // Increment pointer

        buff_cnt++;

        if (inptr >= UART_buffer + BUFF_SIZE)   // Pointer wrapping
            inptr = UART_buffer;

        UART_CONTROL_REG = 0x38;                // Enable UART Data register
                                                // empty interrupt

        return 1;
    } else {
        return 0;                               // Buffer is full
    }

}

// Interrupt driven transmitter

SIGNAL(UART_REG_EMPTY_INT_VECTOR)
{
    UART_DATA_REG = *outptr;                    // Send next byte
    outptr++;                                   // Increment pointer

    if (outptr >= UART_buffer + BUFF_SIZE)      // Pointer wrapping
        outptr = UART_buffer;

    if(--buff_cnt == 0)                         // If buffer is empty:
//        UART_CONTROL_REG = UART_CONTROL_REG && (1<<UDRIE); // disable interrupt This was Atmels original
        UART_CONTROL_REG = UART_CONTROL_REG & (~(1<<UDRIE)); // disable interrupt This is what they meant K.Schwi
}