Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 201 lines (180 sloc) 5.149 kb
2bdbb11 @mhenstell adding teensy firmware and dependencies
mhenstell authored
1 /* Simple example for Teensy USB Development Board
2 * http://www.pjrc.com/teensy/
3 * Copyright (c) 2008 PJRC.COM, LLC
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 * THE SOFTWARE.
22 */
23
24 #include <avr/io.h>
25 #include <avr/pgmspace.h>
26 #include <avr/interrupt.h>
27 #include <stdint.h>
28 #include <util/delay.h>
29 #include "usb_serial.h"
30
31 #define LED_CONFIG (DDRD |= (1<<6))
32 #define LED_ON (PORTD |= (1<<6))
33 #define LED_OFF (PORTD &= ~(1<<6))
34 #define CPU_PRESCALE(n) (CLKPR = 0x80, CLKPR = (n))
35
36 void send_str(const char *s);
37 uint8_t recv_str(char *buf, uint8_t size);
38 void parse_and_execute_command(const char *buf, uint8_t num);
39
40 // Serial to parallel SPI driver
41 static
42 void send_parallel_byte(
43 const uint8_t c
44 )
45 {
9f08284 @mhenstell cleaned up firmware
mhenstell authored
46 PORTD = 0x00;
2bdbb11 @mhenstell adding teensy firmware and dependencies
mhenstell authored
47
48 PORTB = c;
49
9f08284 @mhenstell cleaned up firmware
mhenstell authored
50 PORTD = 0xFF;
2bdbb11 @mhenstell adding teensy firmware and dependencies
mhenstell authored
51 }
52
53 int main(void)
54 {
55 CPU_PRESCALE(0);
56 usb_init();
57
58 DDRB = 0xFF;
9f08284 @mhenstell cleaned up firmware
mhenstell authored
59 DDRD = 0xFF;
2bdbb11 @mhenstell adding teensy firmware and dependencies
mhenstell authored
60
61 while (1)
62 {
63 const int8_t n = usb_serial_available();
64 if (n <= 0)
65 continue;
66
67 const uint8_t irq_flags = SREG;
68 cli();
69
70 int8_t i;
71 for (i = 0 ; i < n ; i++)
72 {
73 #define CDC_RX_ENDPOINT 3
74 UENUM = CDC_RX_ENDPOINT;
75 const uint8_t c = UEDATX;
76 send_parallel_byte(c);
77 }
78
79 // Release the USB buffer
80 UEINTX = 0x6B;
81
82 // Re-enabled interrupts
83 SREG = irq_flags;
84 }
85 }
86
87 // Send a string to the USB serial port. The string must be in
88 // flash memory, using PSTR
89 //
90 void send_str(const char *s)
91 {
92 char c;
93 while (1) {
94 c = pgm_read_byte(s++);
95 if (!c) break;
96 usb_serial_putchar(c);
97 }
98 }
99
100 // Receive a string from the USB serial port. The string is stored
101 // in the buffer and this function will not exceed the buffer size.
102 // A carriage return or newline completes the string, and is not
103 // stored into the buffer.
104 // The return value is the number of characters received, or 255 if
105 // the virtual serial connection was closed while waiting.
106 //
107 uint8_t recv_str(char *buf, uint8_t size)
108 {
109 int16_t r;
110 uint8_t count=0;
111
112 while (count < size) {
113 r = usb_serial_getchar();
114 if (r != -1) {
115 if (r == '\r' || r == '\n') return count;
116 if (r >= ' ' && r <= '~') {
117 *buf++ = r;
118 usb_serial_putchar(r);
119 count++;
120 }
121 } else {
122 if (!usb_configured() ||
123 !(usb_serial_get_control() & USB_SERIAL_DTR)) {
124 // user no longer connected
125 return 255;
126 }
127 // just a normal timeout, keep waiting
128 }
129 }
130 return count;
131 }
132
133 // parse a user command and execute it, or print an error message
134 //
135 void parse_and_execute_command(const char *buf, uint8_t num)
136 {
137 uint8_t port, pin, val;
138
139 if (num < 3) {
140 send_str(PSTR("unrecognized format, 3 chars min req'd\r\n"));
141 return;
142 }
143 // first character is the port letter
144 if (buf[0] >= 'A' && buf[0] <= 'F') {
145 port = buf[0] - 'A';
146 } else if (buf[0] >= 'a' && buf[0] <= 'f') {
147 port = buf[0] - 'a';
148 } else {
149 send_str(PSTR("Unknown port \""));
150 usb_serial_putchar(buf[0]);
151 send_str(PSTR("\", must be A - F\r\n"));
152 return;
153 }
154 // second character is the pin number
155 if (buf[1] >= '0' && buf[1] <= '7') {
156 pin = buf[1] - '0';
157 } else {
158 send_str(PSTR("Unknown pin \""));
159 usb_serial_putchar(buf[0]);
160 send_str(PSTR("\", must be 0 to 7\r\n"));
161 return;
162 }
163 // if the third character is a question mark, read the pin
164 if (buf[2] == '?') {
165 // make the pin an input
166 *(uint8_t *)(0x21 + port * 3) &= ~(1 << pin);
167 // read the pin
168 val = *(uint8_t *)(0x20 + port * 3) & (1 << pin);
169 usb_serial_putchar(val ? '1' : '0');
170 send_str(PSTR("\r\n"));
171 return;
172 }
173 // if the third character is an equals sign, write the pin
174 if (num >= 4 && buf[2] == '=') {
175 if (buf[3] == '0') {
176 // make the pin an output
177 *(uint8_t *)(0x21 + port * 3) |= (1 << pin);
178 // drive it low
179 *(uint8_t *)(0x22 + port * 3) &= ~(1 << pin);
180 return;
181 } else if (buf[3] == '1') {
182 // make the pin an output
183 *(uint8_t *)(0x21 + port * 3) |= (1 << pin);
184 // drive it high
185 *(uint8_t *)(0x22 + port * 3) |= (1 << pin);
186 return;
187 } else {
188 send_str(PSTR("Unknown value \""));
189 usb_serial_putchar(buf[3]);
190 send_str(PSTR("\", must be 0 or 1\r\n"));
191 return;
192 }
193 }
194 // otherwise, error message
195 send_str(PSTR("Unknown command \""));
196 usb_serial_putchar(buf[0]);
197 send_str(PSTR("\", must be ? or =\r\n"));
198 }
199
200
Something went wrong with that request. Please try again.