# R-01 RISC-V Baremetal with RT-Thread. LCD work, USB in progress

**URL:** <https://forum.clockworkpi.com/t/r-01-risc-v-baremetal-with-rt-thread-lcd-work-usb-in-progress/14683>\
**Category:** uConsole\
**Tags:** community, hardware, devterm, r-01, uconsole\
**Created:** [October 17, 2024, 11:20am UTC](https://forum.clockworkpi.com/t/r-01-risc-v-baremetal-with-rt-thread-lcd-work-usb-in-progress/14683 "2024-10-17T11:20:02Z")\
**Posts on this page:** 6\
**Page:** 2

<div class="post-metadata">

**Author:** ![ylyamin](https://yyz1.discourse-cdn.com/flex029/user_avatar/forum.clockworkpi.com/ylyamin/32/8418_2.png) [@ylyamin](https://forum.clockworkpi.com/u/ylyamin)\
**Post date:** [October 25, 2024, 4:30pm UTC](https://forum.clockworkpi.com/t/r-01-risc-v-baremetal-with-rt-thread-lcd-work-usb-in-progress/14683/21 "2024-10-25T16:30:51Z")

</div>

Hi @zoenggit,

Thank you very much, is very interesting about T113. I need to deep dive into this variant.

---

<div class="post-metadata">

**Author:** ![ylyamin](https://yyz1.discourse-cdn.com/flex029/user_avatar/forum.clockworkpi.com/ylyamin/32/8418_2.png) [@ylyamin](https://forum.clockworkpi.com/u/ylyamin)\
**Post date:** [November 4, 2024, 10:37am UTC](https://forum.clockworkpi.com/t/r-01-risc-v-baremetal-with-rt-thread-lcd-work-usb-in-progress/14683/22 "2024-11-04T10:37:30Z")

</div>

Hi @zoenggit

According this discussion [issue](https://github.com/hathach/tinyusb/discussions/1599) in TinyUSB. User @robots created repo for [Allwinner T113](https://github.com/robots/allwinner_t113) chip. Is ARM core but peripherals looks absolutely the same as D1H RISC-V.

Is repo is bare metal experiments with T113 and FreeRTOS also contains Doom. So author decide to deactivate USB EHCI and use only OHCI. Also for USB descriptors used memory section with direct mapping.

**I try to do the same:**

1. Deactivate EHCI:

rt-thread/bsp/allwinner/d1s\_d1h/packages/TinyUSB/rt-thread/bsp/sunxi\_D1/drv\_tinyusb.c

```c
	volatile uint32_t *portsc = (uint32_t * ) (EHCI1_BASE + 0x054); //E_PORTSC
	*portsc |= BV(13);

```

1. Add few places to clear/invalidate caches, in rt-thread/bsp/allwinner/d1s\_d1h/packages/TinyUSB/src/portable/ohci/ohci.c:

```c
static void gtd_init(ohci_gtd_t* p_td, uint8_t* data_ptr, uint16_t total_bytes)
{
...
rt_hw_cpu_dcache_clean_and_invalidate_local(data_ptr, total_bytes);
}

```

1. Define DMA memory section:  
rt-thread/bsp/allwinner/d1s\_d1h/link.lds

```conf
MEMORY
{
    SRAM : ORIGIN = 0x40300000, LENGTH = 4M
    DMA : ORIGIN = 0x40700000, LENGTH = 16M
    HEAP : ORIGIN = 0x41700000, LENGTH = 16M
}

SECTIONS
{
    .usb :
    {
        . = ALIGN(4);   
    } > DMA

```

Init MMU with different sections in rt-thread/bsp/allwinner/d1s\_d1h/board/board.c:

```c
struct mem_desc platform_mem_desc[] = {
    {KERNEL_VADDR_START, 0x40700000 - 1, (rt_size_t)ARCH_MAP_FAILED, NORMAL_MEM}, //KERNEL
    {0x1000, 0x3ffff000 - 1, (rt_size_t)ARCH_MAP_FAILED, DEVICE_MEM}, //IO
    {0x40700000, 0x40700000 + 0x1000000 - 1, (rt_size_t)ARCH_MAP_FAILED, DEVICE_MEM}, //DMA
    {0x41700000, 0x40700000 + 0x4000000 - 1, (rt_size_t)ARCH_MAP_FAILED, NORMAL_MEM}, //HEAP + HW PAGE
};

```

rt-thread/bsp/allwinner/d1s\_d1h/packages/TinyUSB/src/portable/ohci/ohci.c

```c
CFG_TUSB_MEM_SECTION TU_ATTR_ALIGNED(256) static ohci_data_t ohci_data;

```

**Run board.**  
Looks like USB device start to do something new, but all data transfers did not produce any results, all of it STALLED or FAULT:

```sh
[0:0] Open EP0 with Size = 8
Get 8 byte of Device Descriptor
[0:0] Get Descriptor: 80 06 00 01 00 00 08 00
on EP 00 with 8 bytes
on EP 80 with 8 bytes
[0:0] Control STALLED, xferred_bytes = 8

[0:0] Get Descriptor: 80 06 00 03 D0 03 08 00 
on EP 00 with 8 bytes
on EP 80 with 8 bytes
[0:0] Control STALLED, xferred_bytes = 8

[0:0] Get Descriptor: 80 06 00 02 18 08 08 00 
on EP 00 with 8 bytes
on EP 80 with 8 bytes
[0:0] Control STALLED, xferred_bytes = 8

[0:0] Get Descriptor: 80 06 00 02 58 0C 08 00 
on EP 00 with 8 bytes
on EP 00 with 4294964828 bytes

Set Address = 1
[0:0] Set Address: 00 05 01 00 00 00 00 00
on EP 00 with 0 bytes
[0:0] Control STALLED, xferred_bytes = 0

[0:0] Set Address: 00 05 01 02 00 00 00 00 
on EP 00 with 0 bytes
on EP 00 with 2088 bytes

[0:0] Get Descriptor: 80 06 00 01 00 00 12 00 
on EP 00 with 8 bytes
[0:1] Control FAILED, xferred_bytes = 8

```

**So did not lead to success with USB again.**  
Need to dive deeper into EHCI/OHCI, maybe some one can advice please ?

- DevTerm R01 image [image/sd\_image\_devterm.img](https://github.com/ylyamin/RT-Thread-on-Allwinner-D1H/blob/master/image/sd_image_devterm.img)
- uConsole R01 image [image/sd\_image\_uconsole.img](https://github.com/ylyamin/RT-Thread-on-Allwinner-D1H/blob/master/image/sd_image_uconsole.img)
- Sipeed Lichee image [image/sd\_image\_lichee.img](https://github.com/ylyamin/RT-Thread-on-Allwinner-D1H/blob/master/image/sd_image_lichee.img)

---

<div class="post-metadata">

**Author:** ![ylyamin](https://yyz1.discourse-cdn.com/flex029/user_avatar/forum.clockworkpi.com/ylyamin/32/8418_2.png) [@ylyamin](https://forum.clockworkpi.com/u/ylyamin)\
**Post date:** [November 15, 2024, 7:55pm UTC](https://forum.clockworkpi.com/t/r-01-risc-v-baremetal-with-rt-thread-lcd-work-usb-in-progress/14683/23 "2024-11-15T19:55:03Z")

</div>

@zoenggit, @jusentari

Finnaly !

When press the keys “Hello !” on the USB keyboard, I see it In RTT console:

```shell
HID device address = 2, instance = 0 is mounted
HID Interface Protocol = None
HID has 4 reports
msh />
H
e
l
l
o
!

```

How did I manage to do this ? Surprisingly, but with the help of:

 ![IMG_20230528_151045_219](https://canada1.discourse-cdn.com/flex029/uploads/clockworkpi/original/2X/2/231ead4fae5134c84084725e60b4a0cff881602e.jpeg)

Is hand-made ham radio transceivers from one guru guy  
Thanks @zoenggit for the tip about T113. Based on this I found it.

This project supports many chips: ARM, RISC-V, ATMEGA, and many peripheries. And is support Allwinner F133 aka D1s. On this radio Allwinner USB works perfectly. Why ? Because it just has a proper implementation of EHCI/OHCI stack and that is.

Ham radio uses [hftrx\_tinyusb](https://github.com/ua1arn/hftrx_tinyusb) fork of TinyUSB with several changes from guru. I just little changed it for support RT-Thread. Figure out that it is necessary to configure MMU and linker sections for USB to work from SD card.

The next step is routing the keyboard to the console pipe and LCD frame-buffer and we will have a fully portable RT-Thread device !

1. Images in repo @jusentari could I kindly ask you to try it in uConsole if you have time please ?

2. Also @jusentari about lcd\_dclk\_freq change from 62 to 67 to uConsole. I still worry about LCD affects.  
I sent email to display supplier Shenzhen tianxianwei Technologyit about it is safe to change 62 to 67, and they answered:  

```auto
lcd_dclk_freq（像素时钟）=67Mhz，对应的刷新率为65Hz，
大批量做货可能会掉数据出现黑屏、花屏、闪屏；
建议按我们的刷新率=60Hz来调试。

```

Believe it means:

```auto
lcd_dclk_freq (pixel clock) = 67Mhz, the corresponding refresh rate is 65Hz. 
When producing large quantities of devices, data may be dropped, black screens, blurry screens, and flickering screens appear; 
it is recommended to debug according to our refresh rate = 60Hz.

```

Looks like they did not say LCD could be damaged but recommended not changing the frequency.  
Also, they sent the recommended parameters:

```auto
Resolution:720x1280
External system porch setting: VS=2 ,VBP=16 ,VFP=8 , HS=20 ,HBP=20 ,HFP=30
Frame rate:60.10HZ
Pixel Clk:61.90Mhz
MIPI CLK:372Mbps(MIPI通道速率)
dsi.PLL_CLOCK:186Mbps(MTK平台速率)
Power:VCI=2.8V, IOVCC=1.8V RESET=1.8V

```

In Shenzhen answer HBP and VBP are quite different from our ClockworkPi driver:

```yaml
	lcd_hspw = <20>;
	lcd_hbp = <40>;
	lcd_ht = <790>;

	lcd_vspw = <2>;
	lcd_vbp = <18>;
	lcd_vt = <1306>;

```

Can’t test it as I didn’t have uConsole.  
If you have time maybe you could try these parameters and lcd\_dclk\_freq = 62Mhz on uConsole please?

1. @jusentari Sory mentioned you so many times, I hope you are not offended.

Should say I created a true bare-metal library for D1 on ClockworkPi.

No OS, no hidden libs - only minimal low-level stuff. I know you are interested about it.  
Is already working with USB.  
I need some time to clean up this and figure out about the display and I can present.

Thanks

---

<div class="post-metadata">

**Author:** ![jusentari](https://yyz1.discourse-cdn.com/flex029/user_avatar/forum.clockworkpi.com/jusentari/32/10390_2.png) [@jusentari](https://forum.clockworkpi.com/u/jusentari)\
**Post date:** [November 16, 2024, 7:18pm UTC](https://forum.clockworkpi.com/t/r-01-risc-v-baremetal-with-rt-thread-lcd-work-usb-in-progress/14683/24 "2024-11-16T19:18:01Z")

</div>

@ylyamin,  
Nice job!! Very cool that ham radio of all things would have the answer here.

I can confirm that the USB driver is working on my uConsole as well! I was able to type with the keyboard and have it show up in the RTT console.

However, I couldn’t get the LCD screen parameters to work. I changed lcd\_hbp to 20 & lcd\_vbp to 16 in cwu50\_config.c and the screen didn’t respond. Interestingly, the console stopped taking input as well (although it still outputted data over UART) unless I built with lcd\_hbp = 40.

When lcd\_hbp = 40, lcd\_vbp = 16, and lcd\_dclk\_freq = 62, there was a faded outline when using lcd\_draw\_point, but it definitely didn’t look right.

I’ve been tweaking the settings a bit and I’ve gotten something on the screen at lcd\_hbp = 30, lcd\_vbp = 8, freq = 62, but I’ll need to mess around with the settings some more to get it right.

 ![weird_lcd](https://canada1.discourse-cdn.com/flex029/uploads/clockworkpi/original/2X/6/6c809ea1a626886cc4f0d9d6481a7c0d28b5fad3.jpeg)

I’m thinking I’ll need to dig through the Clockwork code to find the values for the screen back porch to maybe tweak the ht/vt values according to this diagram:

 ![image](https://canada1.discourse-cdn.com/flex029/uploads/clockworkpi/original/2X/b/b8f982df7230c2063499b560399fe063bdd0cdd4.png)  
([source](http://bhuvanchandra.github.io/blog/understanding-linux-lcd-display-timings/))  
The values from the email do match up with the ht/vt values already in the cwu50\_config file though:  
vt = 1280 + 2 + 16 + 8 = 1306  
ht = 720 + 20 + 20 + 30 = 790

Great job with the USB!!

---

<div class="post-metadata">

**Author:** ![ylyamin](https://yyz1.discourse-cdn.com/flex029/user_avatar/forum.clockworkpi.com/ylyamin/32/8418_2.png) [@ylyamin](https://forum.clockworkpi.com/u/ylyamin)\
**Post date:** [November 18, 2024, 7:08am UTC](https://forum.clockworkpi.com/t/r-01-risc-v-baremetal-with-rt-thread-lcd-work-usb-in-progress/14683/25 "2024-11-18T07:08:24Z")

</div>

HI, @jusentari

Thanks for testing the USB in uConsole.  
Oh, this LCD work is very mysterious to me. I appreciate your support.

---

<div class="post-metadata">

**Author:** ![ylyamin](https://yyz1.discourse-cdn.com/flex029/user_avatar/forum.clockworkpi.com/ylyamin/32/8418_2.png) [@ylyamin](https://forum.clockworkpi.com/u/ylyamin)\
**Post date:** [February 3, 2025, 12:32pm UTC](https://forum.clockworkpi.com/t/r-01-risc-v-baremetal-with-rt-thread-lcd-work-usb-in-progress/14683/26 "2025-02-03T12:32:31Z")

</div>

Hi Guys, @jusentari @zoenggit

Just in case you still interesting. I stopped with RTT and continue with bare-metal library for R-01, USB work but LCD is not as usual. [R-01 library for small OS](https://forum.clockworkpi.com/t/r-01-library-for-small-os/15602)

[Previous page](https://forum.clockworkpi.com/t/r-01-risc-v-baremetal-with-rt-thread-lcd-work-usb-in-progress/14683.md?page=1)
