Advertisement
Guest User

Untitled

a guest
Jan 24th, 2020
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.38 KB | None | 0 0
  1. [Forwarded from mlat 🏳️‍🌈]
  2. ISR(USART_RXC_vect) {
  3.     char r = UDR;
  4.    
  5.     switch (uart.state) {
  6.         case UART_IDLE: //payload start
  7.             while (UCSRA & (1 << UDRE)) {}
  8.             if (r < BUFSIZE && r > 0) {
  9.                 uart.len = r; //set payload length
  10.                 uart.state = UART_PAYLOAD;
  11.                 UDR = 0x00;
  12.             } else if (r == 0) {
  13.                 UDR = 0xf0;
  14.             } else if (r >= BUFSIZE) {
  15.                 UDR = 0xf9;
  16.             }
  17.             while (UCSRA & (1 << UDRE)) {}
  18.             break;
  19.         case UART_PAYLOAD:
  20.             uart.buffer[uart.idx] = r;
  21.             uart.crc = _crc8_ccitt_update(uart.crc, r);
  22.             uart.idx++;
  23.             if (uart.len == uart.idx) {
  24.                 uart.state = UART_CRC;
  25.             }
  26.             break;
  27.         case UART_CRC:
  28.             while (UCSRA & (1 << UDRE)) {}
  29.             if (_crc8_ccitt_update(uart.crc, r)) {
  30.                 UDR = 0;
  31.             } else {
  32.                 UDR = 0xff;
  33.             }
  34.             while (UCSRA & (1 << UDRE)) {}
  35.             uart.state = UART_IDLE;
  36.             break;
  37.         case UART_CONFIRM:
  38.             uart.conf_ret = r;
  39.             uart.state = UART_IDLE;
  40.         case UART_LOCK:
  41.             while (UCSRA & (1 << UDRE)) {}
  42.             UDR = 0xff;
  43.             while (UCSRA & (1 << UDRE)) {}
  44.             break;
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement