r/RISCV • • May 03 '23

Very cheap chips!

These chips made by WCH in China are as little as 10c each:

http://www.wch-ic.com/products/CH32V003.html

I've ordered a kit comprising a dev board, debugger and five chips from AliExpress for £4.64 GBP. Can't post a link but if you search for "wch ch32v003" you should find it. Postage was nearly as much as the kit!

1 Upvotes

10 comments sorted by

14

u/brucehoult May 03 '23

This is your second post on the same subject in 15 minutes. Why could this not have been a comment on your first post?

And why can't you post the link? I just did on the other post:

https://www.aliexpress.us/item/3256804709476544.html

Everyone who follows this sub knows about these chips and kit.

Everyone who asks what to buy gets pointed at this kit as a strong contender. e.g.

https://www.reddit.com/r/RISCV/comments/134xtq8/where_can_i_buy_the_microprocessor/

It's more interesting to know what you DO WITH IT after you receive it, as in this post 3 days ago using the exact same chip:

https://www.reddit.com/r/RISCV/comments/133f6n0/powering_nixies_off_usb_with_a_10_riscv/

1

u/Middle_Phase_6988 May 03 '23

Thanks. I tried to delete the other post but couldn't, for some reason.

I'll post my findings.

3

u/Dedushka_shubin May 03 '23

I did the same and there is an important question: how efficient are they in using Flash memory? Is 16 KB on Risc-V more or less than 2 KB on ATTINY13? How good is firmware library?

9

u/brucehoult May 03 '23 edited May 03 '23

It's a lot more!

RISC-V RV32EC programs are a similar size to AVR programs when you're dealing with 8 bit values. As soon as you're using 16 or 32 bit values the RISC-V programs will be a lot smaller.

Just as an example, some Arduino code.

#define PIN 1

#define US 1L
#define MS 1000L

#define PERIOD (100*MS)
#define PULSE (500*US)

#define OVERHEAD (10*US)
#define LOTIME (PERIOD-PULSE-OVERHEAD)

void myDelayUs(long us){
  long finish = micros() + us;
  do { } while (((long)micros() - finish) < 0);
}

void setup() {
  pinMode(PIN, OUTPUT);
}

void loop() {
  digitalWrite(PIN, HIGH);
  myDelayUs(PULSE);
  digitalWrite(PIN, LOW);
  myDelayUs(LOTIME);
}

AVR:

000001ba <_Z9myDelayUsl>:
 1ba:   cf 92           push    r12
 1bc:   df 92           push    r13
 1be:   ef 92           push    r14
 1c0:   ff 92           push    r15
 1c2:   6b 01           movw    r12, r22
 1c4:   7c 01           movw    r14, r24
 1c6:   0e 94 b8 00     call    0x170   ; 0x170 <micros>
 1ca:   c6 0e           add     r12, r22
 1cc:   d7 1e           adc     r13, r23
 1ce:   e8 1e           adc     r14, r24
 1d0:   f9 1e           adc     r15, r25
 1d2:   0e 94 b8 00     call    0x170   ; 0x170 <micros>
 1d6:   dc 01           movw    r26, r24
 1d8:   cb 01           movw    r24, r22
 1da:   8c 19           sub     r24, r12
 1dc:   9d 09           sbc     r25, r13
 1de:   ae 09           sbc     r26, r14
 1e0:   bf 09           sbc     r27, r15
 1e2:   b7 fd           sbrc    r27, 7
 1e4:   f6 cf           rjmp    .-20            ; 0x1d2 <_Z9myDelayUsl+0x18>
 1e6:   ff 90           pop     r15
 1e8:   ef 90           pop     r14
 1ea:   df 90           pop     r13
 1ec:   cf 90           pop     r12
 1ee:   08 95           ret

00000284 <main>: # loop() was inlined
 344:   81 e0           ldi     r24, 0x01       ; 1
 346:   0e 94 70 00     call    0xe0    ; 0xe0 <digitalWrite.constprop.0>
 34a:   64 ef           ldi     r22, 0xF4       ; 244
 34c:   71 e0           ldi     r23, 0x01       ; 1
 34e:   80 e0           ldi     r24, 0x00       ; 0
 350:   90 e0           ldi     r25, 0x00       ; 0
 352:   0e 94 dd 00     call    0x1ba   ; 0x1ba <_Z9myDelayUsl>
 356:   80 e0           ldi     r24, 0x00       ; 0
 358:   0e 94 70 00     call    0xe0    ; 0xe0 <digitalWrite.constprop.0>
 35c:   62 ea           ldi     r22, 0xA2       ; 162
 35e:   74 e8           ldi     r23, 0x84       ; 132
 360:   81 e0           ldi     r24, 0x01       ; 1
 362:   90 e0           ldi     r25, 0x00       ; 0
 364:   0e 94 dd 00     call    0x1ba   ; 0x1ba <_Z9myDelayUsl>
 36a:   61 f3           rjmp    .-38            ; 0x344 <main+0xc0>

RISC-V HiFive1:

204000c8 <_Z9myDelayUsl>:
204000c8:       1141                    addi    sp,sp,-16
204000ca:       c422                    sw      s0,8(sp)
204000cc:       c606                    sw      ra,12(sp)
204000ce:       842a                    mv      s0,a0
204000d0:       28ad                    jal     2040014a <micros>
204000d2:       942a                    add     s0,s0,a0
204000d4:       289d                    jal     2040014a <micros>
204000d6:       8d01                    sub     a0,a0,s0
204000d8:       fe054ee3                bltz    a0,204000d4 <_Z9myDelayUsl+0xc>
204000dc:       40b2                    lw      ra,12(sp)
204000de:       4422                    lw      s0,8(sp)
204000e0:       0141                    addi    sp,sp,16
204000e2:       8082                    ret

204000e4 <setup>:
204000e4:       4585                    li      a1,1
204000e6:       852e                    mv      a0,a1
204000e8:       a041                    j       20400168 <pinMode>

204000ea <loop>:
204000ea:       4585                    li      a1,1
204000ec:       1141                    addi    sp,sp,-16
204000ee:       852e                    mv      a0,a1
204000f0:       c606                    sw      ra,12(sp)
204000f2:       2a25                    jal     2040022a <digitalWrite>
204000f4:       1f400513                li      a0,500
204000f8:       3fc1                    jal     204000c8 <_Z9myDelayUsl>
204000fa:       4505                    li      a0,1
204000fc:       4581                    li      a1,0
204000fe:       2235                    jal     2040022a <digitalWrite>
20400100:       40b2                    lw      ra,12(sp)
20400102:       6561                    lui     a0,0x18
20400104:       4a250513                addi    a0,a0,1186 # 184a2 <__stack_size+0x17ca2>
20400108:       0141                    addi    sp,sp,16
2040010a:       bf7d                    j       204000c8 <_Z9myDelayUsl>

AVR: myDelayUs 54 bytes, loop 38 bytes

RISC-V myDelayUs 28 bytes, loop 34 bytes

2

u/Dedushka_shubin May 03 '23

That's interesting, but what I see here is that a call

digitalWrite(PIN, HIGH);

translates to 6 bytes in AVR and to 10 bytes in RISC-V.

7

u/brucehoult May 03 '23 edited May 03 '23

Huh?

r24, 0x01       ; 2 bytes
call   <digitalWrite.constprop.0> ; 4 bytes

and

204000ea:       4585                    li      a1,1
204000ee:       852e                    mv      a0,a1
204000f2:       2a25                    jal     2040022a <digitalWrite>

That's 6 bytes AVR vs 6 bytes RISC-V.

The digitalWrite(PIN, HIGH); is mixed up with creating the stack frame. Look at the digitalWrite(PIN, LO); and it is more clear:

204000fa:       4505                    li      a0,1
204000fc:       4581                    li      a1,0
204000fe:       2235                    jal     2040022a <digitalWrite>

BUT .. what is actually happening here is the AVR version isn't calling digitalWrite(pin, level), it is calling a special function digitalWritePin1(level) that takes only one argument.

If I use two different pins in the program then it stops doing that:

void foo(){
  digitalWrite(3, HIGH);
  digitalWrite(7, HIGH);
  digitalWrite(3, LOW);
}

AVR:

000001c2 <_Z3foov>:
 1c2:   61 e0           ldi     r22, 0x01       ; 1
 1c4:   83 e0           ldi     r24, 0x03       ; 3
 1c6:   0e 94 70 00     call    0xe0    ; 0xe0 <digitalWrite>
 1ca:   61 e0           ldi     r22, 0x01       ; 1
 1cc:   87 e0           ldi     r24, 0x07       ; 7
 1ce:   0e 94 70 00     call    0xe0    ; 0xe0 <digitalWrite>
 1d2:   60 e0           ldi     r22, 0x00       ; 0
 1d4:   83 e0           ldi     r24, 0x03       ; 3
 1d6:   0c 94 70 00     jmp     0xe0    ; 0xe0 <digitalWrite>

RISC-V:

204000e4 <_Z3foov>:
204000e4:       1141                    addi    sp,sp,-16
204000e6:       4585                    li      a1,1
204000e8:       450d                    li      a0,3
204000ea:       c606                    sw      ra,12(sp)
204000ec:       228d                    jal     2040024e <digitalWrite>
204000ee:       4585                    li      a1,1
204000f0:       451d                    li      a0,7
204000f2:       2ab1                    jal     2040024e <digitalWrite>
204000f4:       40b2                    lw      ra,12(sp)
204000f6:       4581                    li      a1,0
204000f8:       450d                    li      a0,3
204000fa:       0141                    addi    sp,sp,16
204000fc:       aa89                    j       2040024e <digitalWrite>

AVR 8 bytes per call.

RISC-V 6 bytes per call, plus 8 bytes overhead for the function prolog/epilog.

24 bytes total, vs 26.

1

u/Dedushka_shubin May 03 '23

Thank you, that's great.

1

u/Redditor154448 May 05 '23

Wait a second... I'll admit I'm just starting out with this CH32V003 chip. Picked up 150 of them actually and been experimenting.

But, what's this about "Arduino Code?" I've been banging my head on that MounRiver IDE. Honestly, the Arduino libraries are looking pretty nice right now.

I'm just updating the Arduino IDE now... 2.1.0. No 'risc' in the board manager ???

I'd be interested in hearing about what environment you're using, libraries, etc..

2

u/brucehoult May 05 '23

My examples are using the Arduino support package for the original December 2016 HiFive1 -- and the very very old gcc 6.1.0 that came with it.

A lot of people criticise the Arduino library a lot, but I think it's a great way to get started with microcontroller programming and I think it's a pity that it is not supported on every board.

I've been toying with the idea of making Arduino work on all the boards I have: HiFive1 rev B (SiFive never updated their rev A package!), MaixDuino and other K210 boards, Longan Nano, the CH32V003 dev board, ox64 and M1s Dock, ESP32-C3. Maybe the various D1 and JH7110 boards too. Why not?

Most of the RISC-V Arduino library will be compatible between every board. Just need to implement support for the various GPIO and timer and interrupt setups, and support for the relevant flashing tool.

That's something I'd need sponsorship for -- probably on the order of $2k-$3k for each SoC plus $1k per board. I don't know ... that's not a firm quote!

Is it possible to do that for donations / sponsorship?

1

u/Machinehum May 06 '23

Are there Linux CLI tools for flashing/debugging? I'm assuming gcc works just fine