Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GD32VF103 port #5449

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open

Add GD32VF103 port #5449

wants to merge 17 commits into from

Conversation

esmil
Copy link
Contributor

@esmil esmil commented Dec 22, 2019

Hi, I'm making badges for a danish hacker camp and this year we're planning on using this RISC-V chip from GigaDevice. It has a bit more flash compared to the HappyGecko we used to use, so I thought it'd be fun to see if I could make MicroPython run on it.

For now it passes all tests but micropython/extreme_exc.py, but I haven't yet implemented any "drivers" for peripherals and such. I thought I'd send an RFC in early and see if you'd be interesting in merging this upstream at some point.

Unfortunately riscv toolchains for bare-metal aren't yet available in most distributions, but cross-compilers for Linux seem to be. So I've made it so that this port can actually be compiled with such a toolchain without any standard library. MicroPython already comes with implementations of memset, memcpy, printf etc., which is cool, so it just needs a few little extra patches for this to work that I hope you're fine with.

I've ordered the commits such that the ones up to "travis: Compile test gd32vf103" are needed to make the port compile in travis. The rest are mostly just fixes for warnings that I get because I enabled a little more compiler warnings (-Wall -Wextra -Wno-unused-parameter).

In the future it would be nice to also add support for SiFive's HiFive1 board which is also emulated by Qemu, such that the tests can be automatically run on a RISC-V platform by travis.

/Emil

py/pystack.h Outdated Show resolved Hide resolved
py/nlr.h Show resolved Hide resolved
@stinos
Copy link
Contributor

stinos commented Dec 25, 2019

Can't comment on the addition of the port itself, but some of these commits would be good in any case. Note te convention is to have the commit header end with a dot, and form a gramaticaly correct sentence normally starting with a verb in the imperative tense so you might want to fix that. And maybe merge the 2 commits for -Wimplicit-fallthrough ?

@esmil
Copy link
Contributor Author

esmil commented Dec 27, 2019

Cool. I'll fix the headers and merge the implicit fallthrough commits

@esmil esmil force-pushed the gd32vf103 branch 4 times, most recently from ad4ad4c to 086dcab Compare December 29, 2019 15:42
@imheresamir
Copy link

Hi, I'm interested in this. Is this ready to be merged?

@esmil
Copy link
Contributor Author

esmil commented Sep 30, 2020

Hi Samir,

Thank you for your interest. Unfortunately our whole badge plan was thwarted by the pandemic, so I haven't looked at this since. But I've now updated my branch to the latest master, except for the travis parts. I'll play with those in a separate branch.

I'll remove the [RFC] and see if we can get some more attention. There's still missing actual "drivers" for gpio and other peripherals though so you can't do much from python yet. Don't know if that is a requirement for upstreaming this..

@esmil esmil changed the title [RFC] Add GD32VF103 port Add GD32VF103 port Sep 30, 2020
@esmil
Copy link
Contributor Author

esmil commented Sep 30, 2020

Ok, now only the size test fails which I guess is because of the signed comparisons. That is IMHO a correctness fix.

@esmil
Copy link
Contributor Author

esmil commented Oct 3, 2020

Rebased on #6510

@esmil
Copy link
Contributor Author

esmil commented Oct 6, 2020

Now with a machine.Pin module, so you can actually do something from python :)

The function scope_find_or_add_id used to take a scope_kind_t enum and
save it in an uint8_t. Saving an enum in a uint8_t is fine, but
everywhere this function is called it is not actually given a
scope_kind_t but an anonymous enum instead. Let's give this enum a name
and use that as the argument type.

This doesn't change the generated code, but is a C type mismatch that
unfortunately doesn't show up unless you enable -Wenum-conversion.
mp_emergency_exception_buf_size is signed, so let's make sure we compare
it as such.
On x86 chars are signed, but we're comparing a char to '0' + unsigned int,
which is promoted to an unsigned int. Let's promote the char to unsigned
before doing the comparison to avoid weird corner cases.
When compiling with -Wextra which includes -Wmissing-field-initializers
GCC will warn that the defval field of mp_arg_val_t is not initialized.
This is just a warning as it is defined to be zero initialized, but since
it is a union it makes sense to be explicit about which member we're
going to use, so add the explicit initializers and get rid of the
warning.
Like Clang, GCC warns about this file, but only with -Woverride-init
which is enabled by -Wextra. Disable the warnings for this file just
like we do for Clang to make -Wextra happy.
Newer GCC versions are able to warn about switch cases that fall
through.  This is usually a sign of a forgotten break statement, but in
the few cases where a fall through is intended we annotate it with this
macro to avoid the warning.
MicroPython already includes its own implementation of memcpy, memmove,
strlen, printf and other functions required to build without a C
library.

So use stddef.h for size_t since it is provided by the compiler, and
leave out stdlib.h where it isn't needed and thus make it easier to
compile MicroPython without a C library.
That is include assert.h in files where assert() is used, and don't
include assert.h in files where it is not used.
We need to include limits.h for PATH_MAX among other things.
Use the defines from our stream.h rather than SEEK_CUR and SEEK_SET from
the C library. This makes it possible to compile even when a C library
is not available.  We already rely on our own versions being equal to
the C library versions in other places, so this shouldn't break any
users.
This is useful to enable travis on your own github fork without having
to create a coveralls.io account.
@dpgeorge
Copy link
Member

Sorry but this really went under my radar... looks interesting and will get to it after #6510

@dpgeorge
Copy link
Member

Thanks a lot for raising this PR. It's very interesting to have RISC-V support.

It's not possible to yet say whether this would be merged or not, but some general points regarding a new port are (see eg #4557):

  1. are the chips widely available to the public and will they be for the foreseeable future?
  2. are development boards easily available?
  3. is the toolchain open source (or at least free to obtain)?
  4. are there plans to support multiple MCUs/SoCs?
  5. will there be someone around for the long term to maintain this port?
  6. the license (and copyright) must be appropriate.
  7. support for a new device must be sustainable in the long term (that's a general remark).
  8. a new port should ideally not touch any files in any directory except the new port directory (and any new sumbodules), so fixes/updates to the core (py/, extmod/, etc) should be in separate PR's
  9. the new port name should be generic enough in scope to encompass new MCUs from the same family in the future

@nsathya123
Copy link

Hi
We will be interested to have this. We hava hackathon happening in India.
The home grown processors are risc-v based. This port will be huge. Since, python compiler for risc-v is missing.

@nsathya123
Copy link

are the chips widely available to the public and will they be for the foreseeable future?

Yes. there are many chips based on risc-v. And SHAKTI is an open source project developing risc-v based soft cores to run on fpga. There are many risc-v groups all around.

are development boards easily available?

SiFive development boards available. And shakti offers fpga based solution. There is a big competetion happnieng in india based on shakti processors.
is the toolchain open source (or at least free to obtain)?
Yes. https://github.com/riscv/riscv-gnu-toolchain
are there plans to support multiple MCUs/SoCs?
Yes. SHAKTi team will be happy to get there MCUs or SoCs.

@esmil
Copy link
Contributor Author

esmil commented Oct 30, 2020

Ok, so just to answer for this particular port and not RISC-V in general:

1. are the chips widely available to the public and will they be for the foreseeable future?

No, unfortunately the GD32VF103 chips themselves are a bit hard to get outside of China. GigaDevice also produces GD32F103 ARM-based chips which are pin-compatible and API compatible with STM's STM32F103 chips, but not register compatible as far as I understand. My understanding is that GD32VF103 is basically the same, but with the ARM core replaced by a Chinese RISC-V implementation. So my guess is that international distributors are wary of selling these chips as they might get into trouble with STM.

2. are development boards easily available?

Yes! seeedstudio.org sells many different boards by Sipeed with this chip. The cheapest is the Longan Nano for $4.90 that even comes with a small screen. You can even find it cheaper without the screen at other places that ships outside of China as well as other boards not designed Sipeed. However if you want to run this port be sure to get a board with the largest version of the chip with 128kB flash and 32kB sram. They're called GD32VF103xB for x in R, T, V or C depending on the packaging.

3. is the toolchain open source (or at least free to obtain)?

Yes! It is upstream in GCC. The README of my https://github.com/esmil/gd32vf103inator/ has some instruction on how to get it on various OSs.

4. are there plans to support multiple MCUs/SoCs?

I don't know what GigaDevice's plans are. Maybe someone who speaks Mandarin will be able to pick up news about this better than me.

5. will there be someone around for the long term to maintain this port?

Yeah, this is the most sticky point. I don't actually know how much I'll use this chip in the future and be around to maintain this port.

6. the license (and copyright) must be appropriate.

Everything in gd32vf104inator is BSD 3-clause, but the code in the port should be the same license as MicroPython. The gd32vf103inator code is based on headers from GigaDevice, so not something I can easily change.

7. support for a new device must be sustainable in the long term (that's a general remark).

8. a new port should ideally not touch any files in any directory except the new port directory (and any new sumbodules), so fixes/updates to the core (py/, extmod/, etc) should be in separate PR's

I can definitely submit the preparatory patches to make it easier to compile MicroPython without newlib as a separate pull request. The reason I haven't done so yet, is just that nothing in the CI system checks that this continues to work.

9. the new port name should be generic enough in scope to encompass new MCUs from the same family in the future

Yeah if GigaDevice creates new versions of this chip maybe something like gd32v would be better.

@benner
Copy link

benner commented Feb 2, 2022

Is there any progress on this? I have couple Longan Nano and looking forward to have MicroPython on it.

@dpgeorge
Copy link
Member

dpgeorge commented Feb 2, 2022

There has been no progress on this from my side.

@projectgus
Copy link
Contributor

This is an automated heads-up that we've just merged a Pull Request
that removes the STATIC macro from MicroPython's C API.

See #13763

A search suggests this PR might apply the STATIC macro to some C code. If it
does, then next time you rebase the PR (or merge from master) then you should
please replace all the STATIC keywords with static.

Although this is an automated message, feel free to @-reply to me directly if
you have any questions about this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants