ataradov / usb-sniffer-lite

A simple USB sniffer based on Raspberry Pi RP2040
BSD 3-Clause "New" or "Revised" License
519 stars 45 forks source link

UART problem #9

Open noobestguy opened 3 months ago

noobestguy commented 3 months ago

I'm not quite sure how to use UART0 in your "baremetal development environment." I don't want to make a mess, haha. Could you send me a small proof of concept to print some debug information at 115200 on the USB sniffer?

this dont works

static void uart_init(uint32_t baud)
{
  RESETS_CLR->RESET = RESETS_RESET_uart0_Msk;
  while (0 == RESETS->RESET_DONE_b.uart0);

  baud = (F_PER * 4) / baud;

  UART0->UARTIBRD = baud / 64;
  UART0->UARTFBRD = baud % 64;

  UART0->UARTLCR_H = (3/*8 bits*/ << UART0_UARTLCR_H_WLEN_Pos) | UART0_UARTLCR_H_FEN_Msk;
  UART0->UARTCR = UART0_UARTCR_UARTEN_Msk | UART0_UARTCR_RXE_Msk | UART0_UARTCR_TXE_Msk;

  HAL_GPIO_UART_TX_init();
  HAL_GPIO_UART_RX_init();
}

awesome project @ataradov

I need to use UART+ VCP

ataradov commented 3 months ago

How exactly it does not work? How it was tested? This code is directly from the starter project that works.

Build the original starter project and check that it works on your hardware.

noobestguy commented 3 months ago

I've done exactly that, and it doesn't work! I'm receiving garbage

static void uart_init(uint32_t baud)
{
  RESETS_CLR->RESET = RESETS_RESET_uart0_Msk;
  while (0 == RESETS->RESET_DONE_b.uart0);

  baud = (F_PER * 4) / baud;

  UART0->UARTIBRD = baud / 64;
  UART0->UARTFBRD = baud % 64;

  UART0->UARTLCR_H = (3/*8 bits*/ << UART0_UARTLCR_H_WLEN_Pos) | UART0_UARTLCR_H_FEN_Msk;
  UART0->UARTCR = UART0_UARTCR_UARTEN_Msk | UART0_UARTCR_RXE_Msk | UART0_UARTCR_TXE_Msk;

  HAL_GPIO_UART_TX_init();
  HAL_GPIO_UART_RX_init();
}

//-----------------------------------------------------------------------------
int main(void)
{
  sys_init();
  timer_init();
  ...
  uart_init(115200);
  uart_puts("\r\nHello, world!\r\n");

PI PICO + debug probe (for PI PICO), recv data:

00 43 21 fffd 16 fffd fffd fffd 09 fffd 271 fffd fffd 35
29 fffd 
ataradov commented 3 months ago

Oh what hardware and with what device used as a receiver?

noobestguy commented 3 months ago

PI PICO + debug probe (for PI PICO), recv data:

00 43 21 fffd 16 fffd fffd fffd 09 fffd 271 fffd fffd 35
29 fffd 
noobestguy commented 3 months ago

image

noobestguy commented 3 months ago

@ataradov Does this code work for you?

ataradov commented 3 months ago

I can see junk being received by the Pico debug probe. It works fine with other adapters.

It also works fine after the initial Hello World string. If you type characters, it will echo them back correctly.

I assume this is some bug in the pico probe. It may not like initial level transitions when device is reset or something like this.

Try with a different USB to UART adapter.

noobestguy commented 3 months ago

oh, right!

sleeping a little.. and it works:

  uart_init(115200);
  for (uint32_t a = 0; a < 1000000; a++) {b++;};

  uart_puts("\r\nHello, world!\r\n");
  uart_puts("\r\nHello, world!\r\n");
ataradov commented 3 months ago

Yes, I was just going to suggest that. There is a chance that clock is not entirely stable at that point, but given that it works with a number of other adapters, but not with Pico Probe, I would assume this is some issue with the probe. I don't have time to investigate that right now.

noobestguy commented 3 months ago

Thank you very much for the help. How can I implement a sleep function for X milliseconds in your baremetal environment?

ataradov commented 3 months ago

The same way as on any other MCU:

static void delay_ms(int ms)
{
  uint32_t cycles = ms * F_CPU / 3 / 1000;

  asm volatile (
    "1: sub %[cycles], %[cycles], #1 \n"
    "   bne 1b \n"
    : [cycles] "+r"(cycles)
  );
}

Normally you would want this to be located in the RAM through the section attribute, but in this case it already runs from the RAM.

noobestguy commented 3 months ago

Great! Is there a way to generate the ELF file to debug it? I think there might be something wrong with the checksums or something. The U2F with your tool works great, but I'd like to use OpenOCD for debugging and a I need a valid ELF (the generated ELF with Makefile dont works)

ataradov commented 3 months ago

You can get the CRC value generated by the tool and place it in the linker script instead of a dummy value LONG(0xcccccccc).

The value would be different for different compilers, but you can get it by running

bin2uf2 -s -i UsbSnifferLite.bin -o UsbSnifferLite.bin 

It will show you the correct CRC. In my case it is 0x76b0d78e. So you would replace LONG(0xcccccccc) with LONG(0x76b0d78e).