Advertisement
Guest User

esp32 uart interrupt

a guest
Oct 17th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.49 KB | None | 0 0
  1. void uartSetup(void)
  2. {
  3.   /* Configure parameters of an UART driver,
  4.     * communication pins and install the driver */
  5.     uart_config_t uart_config = {
  6.         .baud_rate = 19200,
  7.         .data_bits = UART_DATA_8_BITS,
  8.         .parity = UART_PARITY_DISABLE,
  9.         .stop_bits = UART_STOP_BITS_1,
  10.         .flow_ctrl = UART_HW_FLOWCTRL_DISABLE
  11.     };
  12.  
  13.     ESP_ERROR_CHECK(uart_param_config(EX_UART_NUM, &uart_config));
  14.     //Set UART pins
  15.     ESP_ERROR_CHECK(uart_set_pin(EX_UART_NUM, GPIO_NUM_5, GPIO_NUM_17, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE));
  16.  
  17.     //Install UART driver, and get the queue.
  18.     ESP_ERROR_CHECK(uart_driver_install(EX_UART_NUM, BUF_SIZE * 2, 0, 0, NULL, 0));
  19.  
  20.     // release the pre registered UART handler/subroutine
  21.     ESP_ERROR_CHECK(uart_isr_free(EX_UART_NUM));
  22.  
  23.     // register new UART subroutine
  24.     ESP_ERROR_CHECK(uart_isr_register(EX_UART_NUM,uart_intr_handle, NULL, ESP_INTR_FLAG_IRAM, &handle_console));
  25.  
  26.     // enable RX interrupt
  27.     ESP_ERROR_CHECK(uart_enable_rx_intr(EX_UART_NUM));
  28.     log_d("interrupt enabled");
  29. }
  30.  
  31.  
  32. /*
  33.  * Define UART interrupt subroutine to ackowledge interrupt
  34.  */
  35. static void IRAM_ATTR uart_intr_handle(void *arg)
  36. {
  37.   uint16_t rx_fifo_len, status;
  38.   uint8_t i=0;
  39.  
  40.   status = UART2.int_st.val; // read UART interrupt Status
  41.   rx_fifo_len = UART2.status.rxfifo_cnt; // read number of bytes in UART buffer
  42.  
  43.   while(rx_fifo_len){
  44.    rxbuf[i] = UART2.fifo.rw_byte; // read all bytes
  45.    rx_fifo_len--;
  46.    ESP_ERROR_CHECK(uart_disable_rx_intr(EX_UART_NUM));
  47.    Serial.print(rxbuf[i++]);
  48.   }
  49.   // after reading bytes from buffer clear UART interrupt status
  50.   uart_clear_intr_status(EX_UART_NUM, UART_RXFIFO_FULL_INT_CLR|UART_RXFIFO_TOUT_INT_CLR);
  51.   //Serial.write(rxbuf,77);
  52.   for (int i = 33; i <= 48; i++)
  53.   {
  54.     pacemakerID[i-33] = rxbuf[i];
  55.   }
  56.   byte byteID[7] ={pacemakerID[2],pacemakerID[3],pacemakerID[6],pacemakerID[7],pacemakerID[13],pacemakerID[14],pacemakerID[15]};
  57.  
  58.   String tmpPacemakerID = String((char*)byteID);
  59.   uint32_t deviceID = tmpPacemakerID.toInt();
  60.   DeviceInfo = true;
  61.   //Serial.write(pacemakerID,16);
  62.   //Serial.println("");
  63.   //Serial.write(devID,4);
  64.   devID[3] = (deviceID >> 24) & 0xFF;
  65.     devID[2] = (deviceID >> 16) & 0xFF;
  66.     devID[1] = (deviceID >> 8) & 0xFF;
  67.     devID[0] = deviceID & 0xFF;
  68.   //Serial.write(devID,4);
  69.   //Serial.println(base64::encode(&pacemakerID[0],16));
  70.   // a test code or debug code to indicate UART receives successfully,
  71.   log_d("rx done!");
  72.   Serial.write(rxbuf,rx_fifo_len);
  73.   ESP_ERROR_CHECK(uart_disable_rx_intr(EX_UART_NUM));
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement