Author Topic: CH32v003 gpio speed  (Read 3559 times)

0 Members and 1 Guest are viewing this topic.

Offline NorthGuy

  • Super Contributor
  • ***
  • Posts: 3303
  • Country: ca
Re: CH32v003 gpio speed
« Reply #25 on: December 17, 2024, 10:59:03 pm »
All this shifting and masking and extracting 3 bit fields that span bytes and reassembling doesn't look like much less work than just sampling a bit and then toggling a GPIO twice with a few NOPs in between (which an 8 MHz AVR can do no problem).

Assuming you want to do other things too, you would need to prepare a buffer, fill it with data, let it sail and free the CPU.

For every bit you want to send, you need to

- with PWM - select one or the other duty cycle and store the result
- with UART - if the bit is 0 then do "or" and store after every 3 bits

If you want to save processing time, generating PWM buffers will be somewhat faster (unless there are too many bus wait cycles) than UART.

If you want to prefill more buffers, UART will let you store 3 times more data (6 times if PWM duty cycle is 16-bit long).
 

Online mikeselectricstuff

  • Super Contributor
  • ***
  • Posts: 14126
  • Country: gb
    • Mike's Electric Stuff
Re: CH32v003 gpio speed
« Reply #26 on: December 18, 2024, 12:03:51 am »
All this shifting and masking and extracting 3 bit fields that span bytes and reassembling doesn't look like much less work than just sampling a bit and then toggling a GPIO twice with a few NOPs in between (which an 8 MHz AVR can do no problem).

Assuming you want to do other things too, you would need to prepare a buffer, fill it with data, let it sail and free the CPU.

For every bit you want to send, you need to

- with PWM - select one or the other duty cycle and store the result
- with UART - if the bit is 0 then do "or" and store after every 3 bits

If you want to save processing time, generating PWM buffers will be somewhat faster (unless there are too many bus wait cycles) than UART.

If you want to prefill more buffers, UART will let you store 3 times more data (6 times if PWM duty cycle is 16-bit long).
Can the CH32V003 do DMA to a PIO port ? if so, creating a bit pattern in memory and DMAing it could be another option, maybe 1 byte at a time if your ws2812's are OK with inter-byte gaps.
Youtube channel:Taking wierd stuff apart. Very apart.
Mike's Electric Stuff: High voltage, vintage electronics etc.
Day Job: Mostly LEDs
 

Offline HeindalTopic starter

  • Contributor
  • Posts: 16
  • Country: us
Re: CH32v003 gpio speed
« Reply #27 on: December 18, 2024, 01:20:37 pm »
In my case uart and spi cannot be used because they already serve a function. Uart is needed to receive instructions over serial port while spi is connected to external chip. The spi code to read flash takes up space already if i were to bitbang spi then it would take up alot more space. My end goal is to make a flash programmer . The neopixel is just for status purposes. Like one color would represent a erase or read or write etc.

if i use adafruit library i would have to stop uart then update neopixel then start uart again to display or send instructions. But then i would be limited by ch32v003 flash again because of uart + spi + adafruit lib. And assuming i can trim down the required flash size..id prolly lose instructions i sent over  uart when neopixel is being updated.

So id either have to use one of my more powerful mcus like ch32v203 , rp2040 or esp series. But that would be overkill and not much of a learning experience. This is my first project with ch32v003 so kinda want to see it through.

Thats why I am looking for a better way to use neopixel. I think pwm is still available but not sure if it is possible using that.

lol attached is the circuit i have now. The external flash will be removed later so i can use any 3.3v flash and some form of mounting will be made. There is a usb to serial chip under the pcb ( ch340c - 16 pin). The pcb has the contacts only on one side so  wires had to be used.
« Last Edit: December 18, 2024, 01:52:19 pm by Heindal »
 

Offline NorthGuy

  • Super Contributor
  • ***
  • Posts: 3303
  • Country: ca
Re: CH32v003 gpio speed
« Reply #28 on: December 18, 2024, 03:01:49 pm »
So id either have to use one of my more powerful mcus like ch32v203 , rp2040 or esp series. But that would be overkill and not much of a learning experience. This is my first project with ch32v003 so kinda want to see it through.

When choosing MCU, look at periphery first, assign the tasks to the periphery, then decide on the MCU. An MCU with a suitable set of periphery goes a long way.

If it's only one WS2812 for status, simply bit-bang it.

It may be beneficial to use bit-banging for the flash too (using CPU or perhaps DMA). This way you can use QSPI mode which is much faster.
 

Online mikeselectricstuff

  • Super Contributor
  • ***
  • Posts: 14126
  • Country: gb
    • Mike's Electric Stuff
Re: CH32v003 gpio speed
« Reply #29 on: December 18, 2024, 03:13:03 pm »

If it's only one WS2812 for status, simply bit-bang it.

or use a vanilla RGB LED
Youtube channel:Taking wierd stuff apart. Very apart.
Mike's Electric Stuff: High voltage, vintage electronics etc.
Day Job: Mostly LEDs
 

Offline rhodges

  • Frequent Contributor
  • **
  • Posts: 355
  • Country: us
  • Available for embedded projects.
    • My public libraries, code samples, and projects for STM8.
Re: CH32v003 gpio speed
« Reply #30 on: December 19, 2024, 06:52:36 pm »
For what it's worth, I use SPI and DMA for the WS2812B. Each LED uses 9 bytes, encoded as 24 color bits and 3 SPI bits each. I set the CPU to 48mhz and divide by 5, and SPI divisor of 4 to get the SPI 2.4mhz.
Here is my code to create the SPI bit table, if anyone is interested.
Currently developing embedded RISC-V. Recently STM32 and STM8. All are excellent choices. Past includes 6809, Z80, 8086, PIC, MIPS, PNX1302, and some 8748 and 6805. Check out my public code on github. https://github.com/unfrozen
 

Offline westfw

  • Super Contributor
  • ***
  • Posts: 4361
  • Country: us
Re: CH32v003 gpio speed
« Reply #31 on: December 20, 2024, 01:46:42 am »
Hmm.
CH32v003 datasheet: 2KB volatile data storage area SRAM

SPI-based code:
Code: [Select]
    uint8_t    pixtab[256][3];
Of course, with the 32v003 prices, you might be best off just using an entire chip just for controlling the LEDs
 

Offline ocelot

  • Contributor
  • Posts: 12
  • Country: gb
Re: CH32v003 gpio speed
« Reply #32 on: December 22, 2024, 08:35:04 am »
On an STM32 project with the WS2812, I used SPI with DMA and because I could not get the clock rate on the SPI precisely correct for the WS2812, I oversampled the data, by a factor of about 2.5.  The WS2812 can accept some jitter in the width of the narrow and wide pulses .
So the process was to build up the message in RAM, encoding the pattern as repeated 1 and 0 bits, then point the DMA controller at it, with the SPI data register as the destination. 
 

Offline pastaclub

  • Contributor
  • Posts: 45
  • Country: th
Re: CH32v003 gpio speed
« Reply #33 on: January 12, 2025, 06:42:18 am »
Timing is tight to distinguish a 0 from a 1, but between the data bit you have up to 5us of time. So you can do all the data fetching, shifting and branching between the transmission of the bits, and then you have short blocks of assembly with fixed known execution times for transmitting a 0 and for transmitting a 1. No DMA, no peripheral, no worries about which RAM is used, just bit banging.

People have done this with 8MHz MCUs, so for sure it can‘t be impossible on a 48Mhz MCU.

For timing, check this:
https://wp.josh.com/2014/05/13/ws2812-neopixels-are-not-so-finicky-once-you-get-to-know-them/
 

Offline DavidAlfa

  • Super Contributor
  • ***
  • Posts: 6433
  • Country: es
Re: CH32v003 gpio speed
« Reply #34 on: January 12, 2025, 07:03:55 am »
Commands normally use very low bandwith, so why not make a software uart for them? 9600baud should be easy.
Then use the hw uart for the neopixel.

Or, can't you remap the spi pin?
Change it on the fly before and after the neopixel transfer.
The flash won't care about anything with CS inactive.
Create a busy flag so nothing else uses the spi.

And use DMA!

Also check these fast rgb expander routines for spi/uart:
https://www.eevblog.com/forum/microcontrollers/more-fun-with-less-4k-ram-100-x-ws2812bs/msg4525124/#msg4525124
« Last Edit: January 12, 2025, 07:25:21 am by DavidAlfa »
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf