riscv-rust / k210-example

Example project for K210 SoC to show usage of k210-hal project
30 stars 9 forks source link

SiSpeed MAIX BIT (with mic) #3

Open chutchinson opened 4 years ago

chutchinson commented 4 years ago

I'm trying to write firmware for the Sispeed MAix BiT (with microphone); for a thermostat controller, using these crates, with an interest in providing examples or possibly contributing to cargo-flash in order to help automate the flashing process. That's just a bit out of my league at the moment, but those are my aspirations.

I can compile the blinky example and flash it to the device; however, no blinks. A similar program written in the Arduino IDE (C, of course) and flashed to the device (FLASH, instead of SRAM), works fine. Do I need to make an adjustment to the memory map? Are there any considerations for this specific device? Is anyone patient enough to point me to the section of the datasheet that has the answers? I'm willing to put the work in I just don't know where to start.

This is the datasheet referenced by the product SKU:

https://s3.cn-north-1.amazonaws.com.cn/dl.kendryte.com/documents/kendryte_datasheet_20181011163248_en.pdf

https://dl.sipeed.com/MAIX/HDK/Sipeed-Maix-Bit/Maix-Bit%20V2.0(with%20MEMS%20microphone)

chutchinson commented 4 years ago

Okay, so it turns out that the ELF binary produced by Rust is based at address 0xffffffff80000000 instead of 0x80000000. I was unable to get OpenOCD to work, so I was relying on kflash. I ended up patching kflash to mask away the upper bits when flashing dataframes:

# kflash.py 1041
# self.flash_dataframe(segment.data(), segment['p_vaddr'])
self.flash_dataframe(segment.data(), segment['p_vaddr'] & 0xffffffff)
diogoviannaaraujo commented 4 years ago

@chutchinson Patched kflash.py as you described but still no Blinky blink, have you done any other modifications?

Im not in the default submodules commits, I've checkout master on examples and also on Hal and pac and rt, so it would compile. In what commits are your submodules at?

chutchinson commented 4 years ago

@diogoviannaaraujo I am using master of hal/pac/rt; unfortunately there's quite a lot to get right for the build process, and it looks like these K210 crates are still very experimental. I am looking into providing support for the on-board FT2232 in cargo-flash, but until then...

Make sure you have the appropriate linker scripts for the K210 memory layout, and wired into the build process (.cargo/config, build.rs). In the repository these are memory.x and memory-k210.x linker scripts. Other than that, I am using cargo-binutils (objcopy) in conjunction with kflash to flash. My MAIX BIT appears to be a newer variant and is identified by kflash as a Maixduino.

Supposedly newer versions of kflash can directly flash an ELF binary, and nightly versions of rustc can compile RISC-V binaries with the medium code model (-mcmodel=medium) which can generate code with an 0x80000000 section base address. I will be looking into that soon.

# install with "cargo install cargo-binutils"
# build program in release mode and transform ELF binary into raw binary
cargo objcopy --release -- -O binary main.bin
# flash to SRAM (may need to specify dan/bit here)
./kflash.py -s -p /dev/ttyS13 -B maixduino "./main.bin"
chutchinson commented 4 years ago

Okay, so using stable Rust 1.43 I can confirm that compiling with a static relocation model and medium code model results in a functional executable. An unpatched kflash is able to flash the resulting executable; if you patched the linker scripts to use higher memory addresses you can instead go back to the datasheet values.

My cargo configuration looks like this:

[target.riscv64gc-unknown-none-elf]
rustflags = [
  "-C", "link-arg=-Tmemory.x",
  "-C", "link-arg=-Tlink.x",
  "-C", "code-model=medium",
  "-C", "relocation-model=static",
]

[build]
target = "riscv64gc-unknown-none-elf"

And the k210 memory linker script (memory-k210.x):

_max_hart_id = 1;

MEMORY
{
    SRAM : ORIGIN = 0x80000000, LENGTH = 6M
    AI_SRAM : ORIGIN = 0x80600000, LENGTH = 2M
    SRAM_NOCACHE : ORIGIN = 0x40000000, LENGTH = 6M
    AI_SRAM_NOCACHE : ORIGIN = 0x40600000, LENGTH = 2M
}
diogoviannaaraujo commented 4 years ago

Nice, will give a try later. I was able to compile and convert to .bin with riscv-utils from brew. Flashing seemed to happen ok but no Blinky... Hope this new information can make a change on that.

Thanks <3

chutchinson commented 4 years ago

@diogoviannaaraujo any progress on flashing your device?

diogoviannaaraujo commented 4 years ago

No, tried as described above but had no luck. The software flashes but nothing happens.

Bought a st-link v2 to try to use openocd instead of kflash, must be arriving today.

chutchinson commented 4 years ago

@diogoviannaaraujo perhaps try modifying the sample to use a different IO pin? The Maixduino doesn't appear to have a dedicated RGB LED like the MAIX BIT, for example. You could try changing the IO pin to 12/13/14, or if you're testing on a Maixduino, IO pin 1:

https://github.com/sipeed/Maixduino/search?q=LED_BUILTIN&unscoped_q=LED_BUILTIN

chutchinson commented 4 years ago

I added a repository with a blink example that works both on the Maixduino and MAIX BIT. I'm not sure which device you have, but it appears the example in this repository is not designed for a Maixduino. That device does not appear to have an RGB LED, and instead has LEDs for monitoring the RX/TX pins.

If you look at the datasheet, you can see that TX is on IO5:

https://www.waveshare.com/w/upload/8/89/Sipeed_Maixduino_Datasheet_V1.0.pdf

Here is my repository:

https://github.com/chutchinson/k210-examples

diogoviannaaraujo commented 4 years ago

Hi, I have a MAix bit, I’ve checked the PIN number on the code before running. I’m doing in my free time, that’s my I’m not giving that attention on work days, but I will get into it again this weekend. Also got a cheap st-link so I can try the OpenOcd way.

Sent from my iPhone

On Sep 4, 2020, at 6:45 PM, Chris Hutchinson notifications@github.com wrote:

 I added a repository with a blink example that works both on the Maixduino and MAIX BIT. I'm not sure which device you have, but it appears the example in this repository is not designed for a Maixduino. That device does not appear to have an RGB LED, and instead has LEDs for monitoring the RX/TX pins.

If you look at the datasheet, you can see that TX is on IO5:

https://www.waveshare.com/w/upload/8/89/Sipeed_Maixduino_Datasheet_V1.0.pdf

Here is my repository:

https://github.com/chutchinson/k210-examples

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.