Can't manage to get UART DMA working

aerkiaga
Posts: 5
Joined: Sun Oct 30, 2022 3:13 pm

Can't manage to get UART DMA working

Postby aerkiaga » Sun Oct 30, 2022 3:23 pm

So, I've started using my ESP32 board with the Arduino IDE. I read the programming manual to learn how to configure and use the hardware, then wrote a C header file with all register and bit mask definitions (which I'll omit for brevity). This is my code:

Code: Select all

char alignas(uint32_t) out_buffer[16] = "Hello world!";

struct esp32_dma_linked_list dma_linked_list {
  .dw0 =
    ESP32_DMA_LINKED_LIST_DW0_OWNER |
    ESP32_DMA_LINKED_LIST_DW0_EOF |
    ESP32_DMA_LINKED_LIST_DW0_LENGTH(12) |
    ESP32_DMA_LINKED_LIST_DW0_SIZE(16)
  ,
  .dw1 = out_buffer,
  .dw2 = nullptr
};

void setup_uart() {
  *ESP32_UART0_FIFO_REG = ESP32_UART_RXFIFO_RD_BYTE('\0');
  *ESP32_UART0_CONF0_REG =
    ESP32_UART_TICK_REF_ALWAYS_ON |
    ESP32_UART_TXFIFO_RST |
    ESP32_UART_TXD_BRK |
    ESP32_UART_STOP_BIT_NUM(1) |
    ESP32_UART_BIT_NUM(3) |
    ESP32_UART_PARITY_EN |
    ESP32_UART_PARITY
  ;
  *ESP32_UART0_CONF1_REG =
    ESP32_UART_TXFIFO_EMPTY_THRHD(96) |
    ESP32_UART_RXFIFO_FULL_THRHD(96)
  ;
  set_baud_rate(9600);
  *ESP32_UHCI0_CONF0_REG =
    ESP32_UHCI_UART0_CE
  ;
  *ESP32_UHCI0_DMA_OUT_LINK_REG =
    ESP32_UHCI_OUTLINK_START |
    ESP32_UHCI_OUTLINK_ADDR(((uintptr_t) &dma_linked_list) & 0xFFFFF)
  ;
}

void setup() {
  setup_uart();
}

void loop() {
}
Unfortunately, nothing comes out. I can easily print bytes by writing directly to ESP32_UART0_FIFO_REG, but that approach works terribly. I realize that the Arduino ESP32 library initializes the hardware to some values before my code runs, and I believe I'm probably doing the initialization wrong myself. Any ideas what might be wrong in this code? I've read the manual several times now and I can't find an issue.

aerkiaga
Posts: 5
Joined: Sun Oct 30, 2022 3:13 pm

Re: Can't manage to get UART DMA working

Postby aerkiaga » Mon Oct 31, 2022 6:42 pm

Partially fixed the issue. I added this code before the current UART setup code:

Code: Select all

  // enable UART-related clocks
  *ESP32_DPORT_PERIP_CLK_EN_REG |=
    ESP32_DPORT_UART_MEM_CLK_EN |
    ESP32_DPORT_UHCI0_CLK_EN |
    ESP32_DPORT_UART_CLK_EN
  ;
  // release UART-related modules from reset
  *ESP32_DPORT_PERIP_RST_EN_REG &= !(
    ESP32_DPORT_UART_MEM_RST |
    ESP32_DPORT_UHCI0_RST |
    ESP32_DPORT_UART_RST
  );
Now UDMA is working, but the characters come out with some bits (always the same) flipped. Weird... It reads like this:

@el�o Gor�d!

aerkiaga
Posts: 5
Joined: Sun Oct 30, 2022 3:13 pm

Re: Can't manage to get UART DMA working

Postby aerkiaga » Mon Oct 31, 2022 7:46 pm

Now I think the problem is can't manage to get UHCI interrupts working. I've tried using esp_intr_alloc with ETS_UHCI0_INTR_SOURCE as source, as well as doing the process manually:

Code: Select all

// enable level 1 interrupts
*ESP32_PIDCTRL_INTERRUPT_ENABLE_REG = ESP32_PIDCTRL_INTERRUPT_ENABLE(0x1);
// set handler
*ESP32_PIDCTRL_INTERRUPT_ADDR_1_REG = (uintptr_t) master_interrupt_handler;
// enable interrupt for UDMA
*ESP32_DPORT_APP_UHCI0_INTR_MAP_REG = 0;
*ESP32_UHCI0_INT_ENA_REG |= ESP32_UHCI_OUT_DONE_INT_ENA;
In either case, the handler isn't even called when the first blocks have been copied over by DMA.

ESP_Sprite
Posts: 9052
Joined: Thu Nov 26, 2015 4:08 am

Re: Can't manage to get UART DMA working

Postby ESP_Sprite » Tue Nov 01, 2022 12:18 am

esp_intr_alloc should work, but (and apologies if this was already obvious) you also need to enable the interrupt on the UHCI itself; have you set anything in UHCI_INT_ENA_REG?

aerkiaga
Posts: 5
Joined: Sun Oct 30, 2022 3:13 pm

Re: Can't manage to get UART DMA working

Postby aerkiaga » Tue Nov 01, 2022 1:10 pm

Yes, thanks for pointing it out! I had tried setting the ESP32_UHCI_OUT_DONE_INT_ENA bit in ESP32_UHCI0_INT_ENA_REG, but incidentally I had left behind a line wherein I also set ESP32_DPORT_APP_UHCI0_INTR_MAP_REG. Deleting it affords the interrupt.

aerkiaga
Posts: 5
Joined: Sun Oct 30, 2022 3:13 pm

Re: Can't manage to get UART DMA working

Postby aerkiaga » Tue Nov 01, 2022 2:39 pm

Unfortunately it's still flipping some bits in the first and fourth bytes of each block... Will investigate more.

Who is online

Users browsing this forum: No registered users and 149 guests