uart2.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. //uart2.c
  2. #include "uart2.h"
  3. #include <avr/io.h>
  4. #include <avr/interrupt.h>
  5. #include <avr/pgmspace.h>
  6. #include "rbuf.h"
  7. #define UART2_BAUD 115200
  8. #define UART_BAUD_SELECT(baudRate,xtalCpu) (((float)(xtalCpu))/(((float)(baudRate))*8.0)-1.0+0.5)
  9. #define uart2_rxcomplete (UCSR2A & (1 << RXC2))
  10. #define uart2_txcomplete (UCSR2A & (1 << TXC2))
  11. #define uart2_txready (UCSR2A & (1 << UDRE2))
  12. uint8_t uart2_ibuf[14] = {0, 0};
  13. FILE _uart2io = {0};
  14. int uart2_putchar(char c, FILE *stream __attribute__((unused)))
  15. {
  16. while (!uart2_txready);
  17. UDR2 = c; // transmit byte
  18. // while (!uart2_txcomplete); // wait until byte sent
  19. // UCSR2A |= (1 << TXC2); // delete TXCflag
  20. return 0;
  21. }
  22. int uart2_getchar(FILE *stream __attribute__((unused)))
  23. {
  24. if (rbuf_empty(uart2_ibuf)) return -1;
  25. return rbuf_get(uart2_ibuf);
  26. }
  27. //uart init (io + FILE stream)
  28. void uart2_init(void)
  29. {
  30. DDRH &= ~0x01;
  31. PORTH |= 0x01;
  32. rbuf_ini(uart2_ibuf, sizeof(uart2_ibuf) - 4);
  33. UCSR2A |= (1 << U2X2); // baudrate multiplier
  34. UBRR2L = UART_BAUD_SELECT(UART2_BAUD, F_CPU); // select baudrate
  35. UCSR2B = (1 << RXEN2) | (1 << TXEN2); // enable receiver and transmitter
  36. UCSR2B |= (1 << RXCIE2); // enable rx interrupt
  37. fdev_setup_stream(uart2io, uart2_putchar, uart2_getchar, _FDEV_SETUP_WRITE | _FDEV_SETUP_READ); //setup uart2 i/o stream
  38. }
  39. //returns 1 if chars in input buffer match to str
  40. //returns -1 if chars does not match and 0 for empty buffer
  41. int8_t uart2_rx_str_P(const char* str)
  42. {
  43. uint8_t r = rbuf_r(uart2_ibuf); //get read index
  44. uint8_t w = rbuf_w(uart2_ibuf); //get write index
  45. // printf_P(PSTR("uart2_rx_str_P r=%d w=%d\n"), r, w);
  46. uint8_t e = rbuf_l(uart2_ibuf) - 1; //get end index
  47. uint8_t len = strlen_P(str); //get string length
  48. str += len; //last char will be compared first
  49. // printf_P(PSTR(" len=%d\n"), len);
  50. while (len--) //loop over all chars
  51. {
  52. if (w == r) return 0; //empty buffer - return 0
  53. if ((--w) == 255) w = e; //decrement index
  54. char c0 = pgm_read_byte(--str); //read char from str
  55. char c1 = uart2_ibuf[4 + w]; //read char from input buffer
  56. // printf_P(PSTR(" uart2_rx_str_P w=%d l=%d c0=%02x c1=%02x\n"), w, len, c0, c1);
  57. if (c0 == c1) continue; //if match, continue with next char
  58. if ((c0 == '\r') && (c1 == '\n')) //match cr as lf
  59. continue;
  60. if ((c0 == '\n') && (c1 == '\r')) //match lf as cr
  61. continue;
  62. return -1; //no match - return -1
  63. }
  64. return 1; //all characters match - return 1
  65. }
  66. ISR(USART2_RX_vect)
  67. {
  68. //printf_P(PSTR("USART2_RX_vect \n") );
  69. if (rbuf_put(uart2_ibuf, UDR2) < 0) // put received byte to buffer
  70. { //rx buffer full
  71. //uart2_rx_clr(); //for sure, clear input buffer
  72. printf_P(PSTR("USART2 rx Full!!!\n"));
  73. }
  74. }