From b2d74902d76e35fdd420e8b9739335ff2c392e1d Mon Sep 17 00:00:00 2001 From: Bernhard Tittelbach Date: Wed, 15 Feb 2012 01:09:11 +0000 Subject: cmx589a test inital base git-svn-id: https://svn.spreadspace.org/mur.sat@246 7de4ea59-55d0-425e-a1af-a3118ea81d4c --- hardware/cmx589a_teensy_test/cmx589a.c | 280 +++++++++++++++++++++++++++++++++ 1 file changed, 280 insertions(+) create mode 100644 hardware/cmx589a_teensy_test/cmx589a.c (limited to 'hardware/cmx589a_teensy_test/cmx589a.c') diff --git a/hardware/cmx589a_teensy_test/cmx589a.c b/hardware/cmx589a_teensy_test/cmx589a.c new file mode 100644 index 0000000..c444abb --- /dev/null +++ b/hardware/cmx589a_teensy_test/cmx589a.c @@ -0,0 +1,280 @@ +/* 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 +#include +#include +#include +#include "usb_serial.h" +#include "uart.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)) + +#define INT1_SET_INPUT (DDRD |= (1<<1)) +#define PB3_SET_OUTPUT (DDRB &= ~ (1<<3)) +#define PB7_SET_OUTPUT (DDRB &= ~(1<<7)) + +#define PB3_TX_ENABLE (PORTB |= (1<<3)) +#define PB3_TX_DISABLE (PORTB &= ~(1<<3)) + +#define ExtInt1On EIMSK |= 1 << INT1 +#define ExtInt1Config EICRA |= (1 << ISC10 ) | ( 1 << ISC11) + +//#define INIT_COUNTER_0 TCNT0 = 0 +//#define SET_CTC_MODE WGM &= !7; WGM |= 2 +//#define SET_EXT_FREQ_5_3_MHZ OCR0A = 3 + + +unsigned int snr_rsii_counter = 0; +ISR(INT1_vect) +{ + snr_rsii_counter++; +} + + + +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; + + INT1_SET_INPUT; + ExtInt1On; + ExtInt1Config; + + PB3_SET_OUTPUT; + PB3_TX_DISABLE; + + // 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(); + + // print a nice welcome message + send_str(PSTR("\r\nTeensy USB Serial Example, " + "Simple Pin Control Shell\r\n\r\n" + "Example Commands\r\n" + " B0? Read Port B, pin 0\r\n" + " C2=0 Write Port C, pin 1 LOW\r\n" + " D6=1 Write Port D, pin 6 HIGH (D6 is LED pin)\r\n\r\n")); + + // and then listen for commands and process them + while (1) { + send_str(PSTR("> ")); + n = recv_str(buf, sizeof(buf)); + if (n == 255) break; + send_str(PSTR("\r\n")); + 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) { + send_str(PSTR("unrecognized format, 3 chars min req'd\r\n")); + 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 { + send_str(PSTR("Unknown port \"")); + usb_serial_putchar(buf[0]); + send_str(PSTR("\", must be A - F\r\n")); + return; + } + // second character is the pin number + if (buf[1] >= '0' && buf[1] <= '7') { + pin = buf[1] - '0'; + } else { + send_str(PSTR("Unknown pin \"")); + usb_serial_putchar(buf[0]); + send_str(PSTR("\", must be 0 to 7\r\n")); + 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); + // 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 { + send_str(PSTR("Unknown value \"")); + usb_serial_putchar(buf[3]); + send_str(PSTR("\", must be 0 or 1\r\n")); + return; + } + } + // otherwise, error message + send_str(PSTR("Unknown command \"")); + usb_serial_putchar(buf[0]); + send_str(PSTR("\", must be ? or =\r\n")); +} + + +#define BAUD_RATE 9600 + +//~ // write a string to the uart +//~ #define uart_print(s) uart_print_P(PSTR(s)) +//~ void uart_print_P(const char *str) +//~ { + //~ char c; + //~ while (1) { + //~ c = pgm_read_byte(str++); + //~ if (!c) break; + //~ uart_putchar(c); + //~ } +//~ } + +//~ // A very basic example... +//~ // when the user types a character, print it back +//~ int main(void) +//~ { + //~ uint8_t c; + + //~ CPU_PRESCALE(0); // run at 16 MHz + //~ uart_init(BAUD_RATE); + //~ uart_print("UART Example\r\n"); + //~ while (1) { + //~ if (uart_available()) { + //~ c = uart_getchar(); + //~ uart_print("Byte: "); + //~ uart_putchar(c); + //~ uart_putchar('\r'); + //~ uart_putchar('\n'); + //~ } + //~ } +//~ } + -- cgit v1.2.3