Guest User

HelloEcho Firmware

a guest
Mar 5th, 2019
577
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //
  3. // hello.ftdi.44.echo.c
  4. //
  5. // 115200 baud FTDI character echo, with flash string
  6. //
  7. // set lfuse to 0x5E for 20 MHz xtal
  8. //
  9. // Neil Gershenfeld
  10. // 12/8/10
  11. //
  12. // (c) Massachusetts Institute of Technology 2010
  13. // This work may be reproduced, modified, distributed,
  14. // performed, and displayed for any purpose. Copyright is
  15. // retained and must be preserved. The work is provided
  16. // as is; no warranty is provided, and users accept all
  17. // liability.
  18. //
  19.  
  20. #include <avr/io.h>
  21. #include <util/delay.h>
  22. #include <avr/pgmspace.h>
  23.  
  24. #define output(directions,pin) (directions |= pin) // set port direction for output
  25. #define set(port,pin) (port |= pin) // set port pin
  26. #define clear(port,pin) (port &= (~pin)) // clear port pin
  27. #define pin_test(pins,pin) (pins & pin) // test for port pin
  28. #define bit_test(byte,bit) (byte & (1 << bit)) // test for bit set
  29. #define bit_delay_time 8.5 // bit delay for 115200 with overhead
  30. #define bit_delay() _delay_us(bit_delay_time) // RS232 bit delay
  31. #define half_bit_delay() _delay_us(bit_delay_time/2) // RS232 half bit delay
  32. #define char_delay() _delay_ms(10) // char delay
  33.  
  34. #define serial_port PORTA
  35. #define serial_direction DDRA
  36. #define serial_pins PINA
  37. #define serial_pin_in (1 << PA0)
  38. #define serial_pin_out (1 << PA1)
  39.  
  40. #define max_buffer 25
  41.  
  42. void get_char(volatile unsigned char *pins, unsigned char pin, char *rxbyte) {
  43.    //
  44.    // read character into rxbyte on pins pin
  45.    //    assumes line driver (inverts bits)
  46.    //
  47.    *rxbyte = 0;
  48.    while (pin_test(*pins,pin))
  49.       //
  50.       // wait for start bit
  51.       //
  52.       ;
  53.    //
  54.    // delay to middle of first data bit
  55.    //
  56.    half_bit_delay();
  57.    bit_delay();
  58.    //
  59.    // unrolled loop to read data bits
  60.    //
  61.    if pin_test(*pins,pin)
  62.       *rxbyte |= (1 << 0);
  63.    else
  64.       *rxbyte |= (0 << 0);
  65.    bit_delay();
  66.    if pin_test(*pins,pin)
  67.       *rxbyte |= (1 << 1);
  68.    else
  69.       *rxbyte |= (0 << 1);
  70.    bit_delay();
  71.    if pin_test(*pins,pin)
  72.       *rxbyte |= (1 << 2);
  73.    else
  74.       *rxbyte |= (0 << 2);
  75.    bit_delay();
  76.    if pin_test(*pins,pin)
  77.       *rxbyte |= (1 << 3);
  78.    else
  79.       *rxbyte |= (0 << 3);
  80.    bit_delay();
  81.    if pin_test(*pins,pin)
  82.       *rxbyte |= (1 << 4);
  83.    else
  84.       *rxbyte |= (0 << 4);
  85.    bit_delay();
  86.    if pin_test(*pins,pin)
  87.       *rxbyte |= (1 << 5);
  88.    else
  89.       *rxbyte |= (0 << 5);
  90.    bit_delay();
  91.    if pin_test(*pins,pin)
  92.       *rxbyte |= (1 << 6);
  93.    else
  94.       *rxbyte |= (0 << 6);
  95.    bit_delay();
  96.    if pin_test(*pins,pin)
  97.       *rxbyte |= (1 << 7);
  98.    else
  99.       *rxbyte |= (0 << 7);
  100.    //
  101.    // wait for stop bit
  102.    //
  103.    bit_delay();
  104.    half_bit_delay();
  105.    }
  106.  
  107. void put_char(volatile unsigned char *port, unsigned char pin, char txchar) {
  108.    //
  109.    // send character in txchar on port pin
  110.    //    assumes line driver (inverts bits)
  111.    //
  112.    // start bit
  113.    //
  114.    clear(*port,pin);
  115.    bit_delay();
  116.    //
  117.    // unrolled loop to write data bits
  118.    //
  119.    if bit_test(txchar,0)
  120.       set(*port,pin);
  121.    else
  122.       clear(*port,pin);
  123.    bit_delay();
  124.    if bit_test(txchar,1)
  125.       set(*port,pin);
  126.    else
  127.       clear(*port,pin);
  128.    bit_delay();
  129.    if bit_test(txchar,2)
  130.       set(*port,pin);
  131.    else
  132.       clear(*port,pin);
  133.    bit_delay();
  134.    if bit_test(txchar,3)
  135.       set(*port,pin);
  136.    else
  137.       clear(*port,pin);
  138.    bit_delay();
  139.    if bit_test(txchar,4)
  140.       set(*port,pin);
  141.    else
  142.       clear(*port,pin);
  143.    bit_delay();
  144.    if bit_test(txchar,5)
  145.       set(*port,pin);
  146.    else
  147.       clear(*port,pin);
  148.    bit_delay();
  149.    if bit_test(txchar,6)
  150.       set(*port,pin);
  151.    else
  152.       clear(*port,pin);
  153.    bit_delay();
  154.    if bit_test(txchar,7)
  155.       set(*port,pin);
  156.    else
  157.       clear(*port,pin);
  158.    bit_delay();
  159.    //
  160.    // stop bit
  161.    //
  162.    set(*port,pin);
  163.    bit_delay();
  164.    //
  165.    // char delay
  166.    //
  167.    bit_delay();
  168.    }
  169.  
  170. void put_string(volatile unsigned char *port, unsigned char pin, char *str) {
  171.    //
  172.    // print a null-terminated string
  173.    //
  174.    static int index;
  175.    index = 0;
  176.    do {
  177.       put_char(port, pin, str[index]);
  178.       ++index;
  179.       } while (str[index] != 0);
  180.    }
  181.  
  182. int main(void) {
  183.    //
  184.    // main
  185.    //
  186.    static char chr;
  187.    static char buffer[max_buffer] = {0};
  188.    static int index;
  189.    //
  190.    // set clock divider to /1
  191.    //
  192.    CLKPR = (1 << CLKPCE);
  193.    CLKPR = (0 << CLKPS3) | (0 << CLKPS2) | (0 << CLKPS1) | (0 << CLKPS0);
  194.    //
  195.    // initialize output pins
  196.    //
  197.    set(serial_port, serial_pin_out);
  198.    output(serial_direction, serial_pin_out);
  199.    //
  200.    // main loop
  201.    //
  202.    index = 0;
  203.    while (1) {
  204.       get_char(&serial_pins, serial_pin_in, &chr);
  205.       put_string(&serial_port, serial_pin_out, "hello.ftdi.44.echo.c: you typed \"");
  206.       buffer[index++] = chr;
  207.       if (index == (max_buffer-1))
  208.          index = 0;
  209.       put_string(&serial_port, serial_pin_out, buffer);
  210.       put_char(&serial_port, serial_pin_out, '\"');
  211.       put_char(&serial_port, serial_pin_out, 10); // new line
  212.       }
  213.    }
Add Comment
Please, Sign In to add comment