uart2.c 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. rbuf_ini(uart2_ibuf, sizeof(uart2_ibuf) - 4);
  31. UCSR2A |= (1 << U2X2); // baudrate multiplier
  32. UBRR2L = UART_BAUD_SELECT(UART2_BAUD, F_CPU); // select baudrate
  33. UCSR2B = (1 << RXEN2) | (1 << TXEN2); // enable receiver and transmitter
  34. UCSR2B |= (1 << RXCIE2); // enable rx interrupt
  35. fdev_setup_stream(uart2io, uart2_putchar, uart2_getchar, _FDEV_SETUP_WRITE | _FDEV_SETUP_READ); //setup uart2 i/o stream
  36. }
  37. //returns 1 if chars in input buffer match to str
  38. //returns -1 if chars does not match and 0 for empty buffer
  39. int8_t uart2_rx_str_P(const char* str)
  40. {
  41. uint8_t r = rbuf_r(uart2_ibuf); //get read index
  42. uint8_t w = rbuf_w(uart2_ibuf); //get write index
  43. // printf_P(PSTR("uart2_rx_str_P r=%d w=%d\n"), r, w);
  44. uint8_t e = rbuf_l(uart2_ibuf) - 1; //get end index
  45. uint8_t len = strlen_P(str); //get string length
  46. str += len; //last char will be compared first
  47. // printf_P(PSTR(" len=%d\n"), len);
  48. while (len--) //loop over all chars
  49. {
  50. if (w == r) return 0; //empty buffer - return 0
  51. if ((--w) == 255) w = e; //decrement index
  52. char c0 = pgm_read_byte(--str); //read char from str
  53. char c1 = uart2_ibuf[4 + w]; //read char from input buffer
  54. // printf_P(PSTR(" uart2_rx_str_P w=%d l=%d c0=%02x c1=%02x\n"), w, len, c0, c1);
  55. if (c0 == c1) continue; //if match, continue with next char
  56. if ((c0 == '\r') && (c1 == '\n')) //match cr as lf
  57. continue;
  58. if ((c0 == '\n') && (c1 == '\r')) //match lf as cr
  59. continue;
  60. return -1; //no match - return -1
  61. }
  62. return 1; //all characters match - return 1
  63. }
  64. ISR(USART2_RX_vect)
  65. {
  66. //printf_P(PSTR("USART2_RX_vect \n") );
  67. if (rbuf_put(uart2_ibuf, UDR2) < 0) // put received byte to buffer
  68. { //rx buffer full
  69. //uart2_rx_clr(); //for sure, clear input buffer
  70. printf_P(PSTR("USART2 rx Full!!!\n"));
  71. }
  72. }