This repository has been archived by the owner on Jan 9, 2024. It is now read-only.
/
main.c
195 lines (175 loc) · 5.17 KB
/
main.c
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/* Simple example for Teensy USB Development Board
* http://www.pjrc.com/teensy/
* Copyright (c) 2008 PJRC.COM, LLC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <stdint.h>
#include <util/delay.h>
#include "usb_serial.h"
#define LED_CONFIG (DDRD |= (1<<6))
#define LED_ON (PORTD |= (1<<6))
#define LED_OFF (PORTD &= ~(1<<6))
#define CPU_PRESCALE(n) (CLKPR = 0x80, CLKPR = (n))
void send_str(const char *s);
uint8_t recv_str(char *buf, uint8_t size);
void parse_and_execute_command(const char *buf, uint8_t num);
#if 0
// Very simple character echo test
int main(void)
{
CPU_PRESCALE(0);
usb_init();
while (1) {
int n = usb_serial_getchar();
if (n >= 0) usb_serial_putchar(n);
}
}
#else
// Basic command interpreter for controlling port pins
int main(void)
{
char buf[32];
uint8_t n;
// set for 16 MHz clock, and turn on the LED
CPU_PRESCALE(0);
LED_CONFIG;
LED_ON;
// initialize the USB, and then wait for the host
// to set configuration. If the Teensy is powered
// without a PC connected to the USB port, this
// will wait forever.
usb_init();
while (!usb_configured()) /* wait */ ;
_delay_ms(1000);
while (1) {
// wait for the user to run their terminal emulator program
// which sets DTR to indicate it is ready to receive.
while (!(usb_serial_get_control() & USB_SERIAL_DTR)) /* wait */ ;
// discard anything that was received prior. Sometimes the
// operating system or other software will send a modem
// "AT command", which can still be buffered.
usb_serial_flush_input();
// and then listen for commands and process them
while (1) {
n = recv_str(buf, sizeof(buf));
if (n == 255) break;
parse_and_execute_command(buf, n);
}
}
}
#endif
// Send a string to the USB serial port. The string must be in
// flash memory, using PSTR
//
void send_str(const char *s)
{
char c;
while (1) {
c = pgm_read_byte(s++);
if (!c) break;
usb_serial_putchar(c);
}
}
// Receive a string from the USB serial port. The string is stored
// in the buffer and this function will not exceed the buffer size.
// A carriage return or newline completes the string, and is not
// stored into the buffer.
// The return value is the number of characters received, or 255 if
// the virtual serial connection was closed while waiting.
//
uint8_t recv_str(char *buf, uint8_t size)
{
int16_t r;
uint8_t count=0;
while (count < size) {
r = usb_serial_getchar();
if (r != -1) {
if (r == '\r' || r == '\n') return count;
if (r >= ' ' && r <= '~') {
*buf++ = r;
usb_serial_putchar(r);
count++;
}
} else {
if (!usb_configured() ||
!(usb_serial_get_control() & USB_SERIAL_DTR)) {
// user no longer connected
return 255;
}
// just a normal timeout, keep waiting
}
}
return count;
}
// parse a user command and execute it, or print an error message
//
void parse_and_execute_command(const char *buf, uint8_t num)
{
uint8_t port, pin, val;
if (num < 3) {
return;
}
// first character is the port letter
if (buf[0] >= 'A' && buf[0] <= 'F') {
port = buf[0] - 'A';
} else if (buf[0] >= 'a' && buf[0] <= 'f') {
port = buf[0] - 'a';
} else {
return;
}
// second character is the pin number
if (buf[1] >= '0' && buf[1] <= '7') {
pin = buf[1] - '0';
} else {
return;
}
// if the third character is a question mark, read the pin
if (buf[2] == '?') {
// make the pin an input
*(uint8_t *)(0x21 + port * 3) &= ~(1 << pin);
// drive it high for high impedance
*(uint8_t *)(0x22 + port * 3) |= (1 << pin);
// read the pin
val = *(uint8_t *)(0x20 + port * 3) & (1 << pin);
usb_serial_putchar(val ? '1' : '0');
send_str(PSTR("\r\n"));
return;
}
// if the third character is an equals sign, write the pin
if (num >= 4 && buf[2] == '=') {
if (buf[3] == '0') {
// make the pin an output
*(uint8_t *)(0x21 + port * 3) |= (1 << pin);
// drive it low
*(uint8_t *)(0x22 + port * 3) &= ~(1 << pin);
return;
} else if (buf[3] == '1') {
// make the pin an output
*(uint8_t *)(0x21 + port * 3) |= (1 << pin);
// drive it high
*(uint8_t *)(0x22 + port * 3) |= (1 << pin);
return;
} else {
return;
}
}
}