uart2.c 2.8 KB

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