aboutsummaryrefslogtreecommitdiffstats
path: root/firmware/octoclock/lib/serial.c
blob: 298cdff8db17d3a995f4e1da8cebb652fc64609f (plain)
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
 * Copyright 2014 Ettus Research LLC
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdbool.h>

#include <octoclock.h>
#include <serial.h>

#include <avr/pgmspace.h>
#include <util/delay.h>
#include <avr/io.h>
#include <avr/interrupt.h>

void serial_init(volatile uint8_t* port, uint8_t index){
    *port |= _BV(index);
}

static void _serial_tx_send(uint8_t* buffer, volatile uint8_t* port, uint8_t index){
    const uint8_t delay = BAUD_115200_DELAY;
    uint8_t countdown;

    for(uint8_t i = 0; i < 10; ++i){
        if(buffer[i]) *port |= _BV(index);
        else *port &= ~_BV(index);

        countdown = delay;
        while(--countdown) asm("nop");
    }
}

static void _serial_tx_char(char c, volatile uint8_t* port, uint8_t index){
	uint8_t buffer[10];
	uint8_t i = 0;

	buffer[i++] = 0;	// START
	for (int idx = 0; idx < 8; ++idx)
	buffer[i++] = (((uint8_t)(c) & ((uint8_t)1<<((idx)))) ? 0x01 : 0x00);	// Endianness: 7-
	buffer[i++] = 1;	// STOP

	_serial_tx_send(buffer, port, index);
}

void serial_tx_P(const char* message, volatile uint8_t* port, uint8_t index, bool newline){
    char c = pgm_read_byte(message);
    if(c == '\0') return;

    do{
        _serial_tx_char(c, port, index);
        c = pgm_read_byte(++message);
    } while(c != '\0');

    if(newline){
        _serial_tx_char('\r', port, index);
        _serial_tx_char('\n', port, index);
    }

    *port |= _BV(index);
}

void serial_tx(const char* message, volatile uint8_t* port, uint8_t index, bool newline){
    if (message[0] == '\0')
    return;

    do
    {
        _serial_tx_char(*message, port, index);
    } while (*(++message) != '\0');

    if (newline){
        _serial_tx_char('\r', port, index);
        _serial_tx_char('\n', port, index);
    }

    *port |= _BV(index);
}

void serial_tx_byte(uint8_t byte, volatile uint8_t* port, uint8_t index, bool newline){
    char ch[4];
    ch[0] = '0' + (byte / 100);
    ch[1] = '0' + ((byte % 100) / 10);
    ch[2] = '0' + (byte % 10);
    ch[3] = '\0';
    serial_tx(ch, port, index, newline);
}

void serial_tx_hex(uint8_t byte, volatile uint8_t* port, uint8_t index, bool newline){
    char ch[3];
    uint8_t _byte = byte >> 4;
    if (_byte < 10)
        ch[0] = '0' + _byte;
    else
        ch[0] = 'A' + (_byte - 10);
    byte &= 0x0F;
    if (byte < 10)
        ch[1] = '0' + byte;
    else
        ch[1] = 'A' + (byte - 10);
    ch[2] = '\0';
    serial_tx(ch, port, index, newline);
}

char serial_rx_char(volatile uint8_t* port, uint8_t index){
    char c = 0;
    const uint8_t delay = BAUD_115200_DELAY;
    uint8_t countdown;

    //Wait for character to appear, 0 will act as start marker
    while(*port & _BV(index));

    //With start marker there, wait for next bit
    countdown = delay;
    while(--countdown) asm("nop");

    for(uint8_t i = 0; i < 8; ++i){
        if(*port & _BV(index)) c &= (uint8_t)(1 << i);

        countdown = delay;
        while(--countdown) asm("nop");
    }

    return c;
}

//Assume ready (probably risky)
char serial_rx_char_nowait(volatile uint8_t* port, uint8_t index){
    char c = 0;
    const uint8_t delay = BAUD_115200_DELAY;
    uint8_t countdown;

    //Wait for start marker to pass
    countdown = delay;
    while(--countdown) asm("nop");

    for(uint8_t i = 0; i < 8; ++i){
        if(*port & _BV(index)) c &= (uint8_t)(1 << i);

        countdown = delay;
        while(--countdown) asm("nop");
    }

    return c;
}