Giter Site home page Giter Site logo

onion_omega2_samples's Introduction

There is a so far nice little IoT platform to compete with the raspberry
pi I guess.  Way overkill for IoT as it is a linux capable/designed
platform.  But I have little interest in yet another linux computer
so this is about bare metal (no operating system).  So far there is some
good documentation for Linux users, there is a manual with some holes
in it for the chip but better than most for platforms like this.

I am an ARM fan but it is nice to see something non-arm from time to
time, this platform is based around a MIPS core.  Most baremetal work
is not related to ARM nor MIPS anyway.

Unlike the pi but as with most Linux/OS capable systems this has a
bootloader, u-boot being a popular choice and that is the case here.
Unfortunately like some u-boot ports they didnt bother to keep some
of the freebies like s-record loading.  So far as I can tell the sd
card is not supported by u-boot.  USB is supported so my recommended
minimum starting point is the omega2 without the extra couple of bucks
worth of sd card holder and the expansion board that gives us USB
support.

https://www.sparkfun.com/products/14432
https://www.sparkfun.com/products/14439

And a FAT32 formatted thumb drive.

There is a lot of good documentation, but not much of it helps with
baremetal

https://docs.onion.io/omega2-docs/

There is info on reloading the linux/os image from the bootloader.

The expansion board has a silabs usb to uart interface for us, that
is why uart 0 pins are not on the header.

[ 9966.623072] usb 1-3.4.1: new full-speed USB device number 35 using ehci-pci
[ 9966.762055] usb 1-3.4.1: New USB device found, idVendor=10c4, idProduct=ea60
[ 9966.762058] usb 1-3.4.1: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[ 9966.762059] usb 1-3.4.1: Product: CP2102 USB to UART Bridge Controller
[ 9966.762061] usb 1-3.4.1: Manufacturer: Silicon Labs
[ 9966.762062] usb 1-3.4.1: SerialNumber: 0001
[ 9966.770086] cp210x 1-3.4.1:1.0: cp210x converter detected
[ 9966.770290] usb 1-3.4.1: cp210x converter now attached to ttyUSB1

Connecting at 115200 8N1 you can see the bootloader and serial console.



   ____       _             ____
  / __ \___  (_)__  ___    / __ \__ _  ___ ___ ____ _
 / /_/ / _ \/ / _ \/ _ \  / /_/ /  ' \/ -_) _ `/ _ `/
 \____/_//_/_/\___/_//_/  \____/_/_/_/\__/\_, /\_,_/
 W H A T  W I L L  Y O U  I N V E N T ? /___/"

Board: Onion Omega2 APSoC DRAM:  128 MB
relocate_code Pointer at: 87f60000
flash manufacture id: c2, device id 20 19
find flash: MX25L25635E
*** Warning - bad CRC, using default environment

============================================
Onion Omega2 UBoot Version: 4.3.0.3
--------------------------------------------
ASIC 7628_MP (Port5<->None)
DRAM component: 1024 Mbits DDR, width 16
DRAM bus: 16 bit
Total memory: 128 MBytes
Flash component: SPI Flash
Date:Oct 18 2016  Time:17:29:05
============================================
icache: sets:512, ways:4, linesz:32 ,total:65536
dcache: sets:256, ways:4, linesz:32 ,total:32768
CPU freq = 575 MHZ
Estimated memory size = 128 Mbytes
Resetting MT7628 PHY.
Initializing MT7688 GPIO system.


**************************************
* Hold Reset button for more options *
**************************************


You have 40 seconds left to select a menu option...


Please select option:
   [ Enter ]: Boot Omega2.
   [ 0 ]: Start Web recovery mode.
   [ 1 ]: Start command line mode.
   [ 2 ]: Flash firmware from USB storage.


The expansion board as a switch which is a power switch and the angled
corner has a so called reset button.  Hold the "reset" button and
flip the switch off then on and you will see the above choices

We want 1 for now  start in command line mode.


Option [1] selected.

1: System Enter Boot Command Line Interface.

U-Boot 1.1.3 (Oct 18 2016 - 17:29:05)
Omega2 #

There are instructions for building a cross-compile environment for
this platform which includes some libraries.  You can use that you
can google around for a mips gcc pre-built or I have my own repo
that contains a script to build a gcc based mips toolchain from
sources
https://github.com/dwelch67/build_gcc
the build_mips script.

Yes all my development work is on (yet another) Linux (computer).  It
is easy to get a thumb drive bootable linux to play with or put one on
a virtual machine like VirtualBox or vmware or others.  This can of
course all be done on windows or macos with some small tweaking here
and there.

On the docs.onion.io page under hardware overview they have a link
to the processor documentation we will rely on heavily, as of this
writing this is the link to version 1.4:

https://labs.mediatek.com/fileMedia/download/9ef51e98-49b1-489a-b27e-391bac9f7bf3

The omega2 docs tell us that the led is on GPIO44 and that appears
to be true you can from this command line interface use nm to write
0x1000 to address 0x10000634

Omega2 # nm 0x10000634
10000634: ffffffff ? 0x1000
10000634: ffffffff ?
Omega2 #

I dont know if that is the proper way to exit from the nm command but
I just happened to figure out entering a question mark bumps us back
to the u-boot command line.  When you enter the 1000 above you can
see that the (amber) led turns off on the board.  To turn it back on

Omega2 # nm 0x10000644
10000644: ffffffff ? 0x1000
10000644: ffffffff ? ?
Omega2 #

So ideally we would want to write/run a simple program

00000000:   3c021000    lui $2,0x1000
00000004:   24031000    li  $3,4096
00000008:   ac430634    sw  $3,1588($2)
0000000c:   1000ffff    b   1000000c <_start+0xc>
00000010:   00000000    nop

I spent a fair amount of time trying to use 0x80000000 as the ram
address space, and it is, but it appears to be data cached.  For
example you can use mm to write to some of it

Omega2 # mm 0x00000000
00000000: 7ebb0da4 ? 3c021000
00000004: 76fab4ff ? 24031000
00000008: c6ba1d8f ? ac430634
0000000c: d443f80b ? 1000ffff
00000010: 868fb33b ? 00000000
00000014: fe7f7c47 ?
Omega2 # md 0x00000000
00000000: 3c021000 24031000 ac430634 1000ffff    ...<...$4.C.....
00000010: 00000000 fe7f7c47 c6cdbf69 6afdfdcf    ....G|..i......j
 ---
Omega2 # md 0x80000000
80000000: 3c021000 24031000 ac430634 1000ffff    ...<...$4.C.....
80000010: 00000000 fe7f7c47 c6cdbf69 6afdfdcf    ....G|..i......j
 ---
Omega2 #

unlike nm if you just hit enter with no added data it takes you
back to the u-boot command line.

So from a clean boot we see the above, but if you mm to 0x80000000
(note u-boot doesnt seem to need the 0x in front of hex numbers it
assumes all of these numbers are hex)


Omega2 # mm 0x80000000
80000000: 3c021000 ? 1
80000004: 24031000 ? 2
80000008: ac430634 ? 3
8000000c: 1000ffff ? 4
80000010: 00000000 ? 5
80000014: fe7f7c47 ?
Omega2 # md 0x00000000
00000000: 3c021000 24031000 ac430634 1000ffff    ...<...$4.C.....
00000010: 00000000 fe7f7c47 c6cdbf69 6afdfdcf    ....G|..i......j
 ----
Omega2 # md 80000000
80000000: 00000001 00000002 00000003 00000004    ................
80000010: 00000005 fe7f7c47 c6cdbf69 6afdfdcf    ....G|..i......j
 ---
Omega2 #

so 0x80000000 values changed but not 0x00000000 making 0x80000000
appear to be a cached view.

You can now mm 0x00000000 using different values than you wrote to
0x80000000 and now you wont see the changes at 0x80000000 because they
are cached.

So go back to this:

Omega2 # mm 0x00000000
00000000: 7ebb0da4 ? 3c021000
00000004: 76fab4ff ? 24031000
00000008: c6ba1d8f ? ac430634
0000000c: d443f80b ? 1000ffff
00000010: 868fb33b ? 00000000
00000014: fe7f7c47 ?
Omega2 #

Note the second column of numbers is the before value doesnt matter
if your before values dont match mine, or due to experiments above

and now use the go command


Omega2 # go 0x00000000
## Starting application at 0x00000000 ...

and now the (amber) led is off.

but we left it in an infinite loop so get back to u-boot (hold reset
when powering on, select 1)

And try this program


   0:   3c021000    lui $2,0x1000
   4:   24031000    li  $3,4096
   8:   ac430634    sw  $3,1588($2)
   c:   03e00008    jr  $31
  10:   00000000    nop


U-Boot 1.1.3 (Oct 18 2016 - 17:29:05)
Omega2 # mm 0x00000000
00000000: 7ebb5db4 ? 3c021000
00000004: fefffcfb ? 24031000
00000008: e6fb5fff ? ac430634
0000000c: d443fddb ? 03e00008
00000010: e69fbbff ? 00000000
00000014: fefffce7 ?
Omega2 # go 0x00000000
## Starting application at 0x00000000 ...
## Application terminated, rc = 0x10000000
Omega2 #

Performing a return rather than an infinite loop we end up back at
the u-boot prompt to continue to play.

Now entering a long program like this can be pain.

They have provided a way to write your application to flash using
a usb stick.  This is documented on the docs.onion.io website.  The
fat32 formatted thumb drive needs the file omega2.bin.  When you
power up with the reset button pressed choose option 2 it does all
the rest of the work for you.

So try this with the blinker01 binary.





You have 40 seconds left to select a menu option...


Please select option:
   [ Enter ]: Boot Omega2.
   [ 0 ]: Start Web recovery mode.
   [ 1 ]: Start command line mode.
   [ 2 ]: Flash firmware from USB storage.



enter 2 and it prints a bunch of stuff out then boots into our program


   ____       _             ____
  / __ \___  (_)__  ___    / __ \__ _  ___ ___ ____ _
 / /_/ / _ \/ / _ \/ _ \  / /_/ /  ' \/ -_) _ `/ _ `/
 \____/_//_/_/\___/_//_/  \____/_/_/_/\__/\_, /\_,_/
 W H A T  W I L L  Y O U  I N V E N T ? /___/"

Board: Onion Omega2 APSoC DRAM:  128 MB
relocate_code Pointer at: 87f60000
******************************
Software System Reset Occurred
******************************
flash manufacture id: c2, device id 20 19
find flash: MX25L25635E
*** Warning - bad CRC, using default environment

============================================
Onion Omega2 UBoot Version: 4.3.0.3
--------------------------------------------
ASIC 7628_MP (Port5<->None)
DRAM component: 1024 Mbits DDR, width 16
DRAM bus: 16 bit
Total memory: 128 MBytes
Flash component: SPI Flash
Date:Oct 18 2016  Time:17:29:05
============================================
icache: sets:512, ways:4, linesz:32 ,total:65536
dcache: sets:256, ways:4, linesz:32 ,total:32768
CPU freq = 575 MHZ
Estimated memory size = 128 Mbytes
Resetting MT7628 PHY.
Initializing MT7688 GPIO system.


**************************************
* Hold Reset button for more options *
**************************************



Boot Linux from Flash NO RESET PRESSED.
## Booting image at bc050000 ...
   Image Name:   blinker01
   Image Type:   MIPS Linux Standalone Program (uncompressed)
   Data Size:    216 Bytes =  0.2 kB
   Load Address: 00000000
   Entry Point:  00000000
   Verifying Checksum ... OK
OK


if you look the led is now blinking.  So another angle on the cache
theory is that through some ugly experiments I was able to run
code from 0x80000000 and my initial blinker program was posted.
using the 1 option you can do something like

usb reset
fatload usb 0:1 0x80000000 omega2.bin
bootm 0x80000000

The ugly trick is this, the mkimage header is 0x40 bytes (you apt-get
mkimage for Ubuntu/Mint and other in that flavor, other linuxes should
be as easy, windows/macos no doubt tools are there, dont use it so
you have to figure it out on your own).  So the "data" which is
our program starts at 0x40 bytes after the header, so if for example
I were to change memmap so that .text is at 0x80000040, change
the mkimage command so that the load address is 0x80000000 and
the entry point is 0x80000040, then if you do the above it will
load and run from 0x8xxxxxxx address space but much faster as if it
is running from cache not dram.  So I changed the hand tuned numbers
in blinker01 for the 0x00000000 address space, you can adjust those
numbers re-build, re-flash and see the blink rate change.

We now have the ability to load a program into flash, such that when
we power up normally without the reset button pressed our program
loads from flash to ram and runs there.

u-boot has already configured uart0 for its own use, the side effect
of this is we dont have to configure it we can just use it.  The
documentation is a bit lacking here, noticing that the chip docs
are showing peripherals in address order 0x10000400, 0x10000500, etc
we can assume that uart0 is 0x10000c00 based and uart1 0x10000d00 based
and so on.  And that works out

Omega2 # nm 0x10000c00
10000c00: 0000000d ? 55
U10000c00: 0000000d ? ?
Omega2 #

The 0x00 register (0x10000c00) when written goes to the transmitter
holding register and/or fifo.  So by writing a 0x55 (the letter 'U')
we can see that we inserted it into the output.

The bootloader01 directory contains a bootloader I have used many
times it is s-record based

https://en.wikipedia.org/wiki/SREC_(file_format)

Specifically it looks for the S3 format.

    $(GNUMIPS)-objcopy --srec-forceS3 blinker01.elf -O srec blinker01.srec

So my compiler produced this for blinker01 (as of this writing)

S0110000626C696E6B657230312E73726563CB
S315000000001000000C00811D3C00000000FFFF0010E6
S3150000001000000000000000000800E003000085ACBE
S31500000020000000000000828C0800E00300000000D1
S31500000030000000000800E0030000000000000000CF
S31500000040D8FFBD271C00B2AF1800B1AF0010123C9C
S315000000500600113C2000B3AF2400BFAF3406532680
S315000000601400B0AF801A312644065226001005242B
S315000000700600000C252060022580000025200002D5
S315000000800D00000C01001026FDFF111625200002B0
S31500000090001005240600000C2520400225800000E3
S315000000A0252000020D00000C01001026FDFF111690
S311000000B025200002EEFF001000100524C1
S315000000C00000010001010001000000000000000026
S30D000000D0000000000000000022
S70500000000FA

If you do the omega2.bin thing above and put bootloader01 in flash
Then you can cut and paste the above into the terminal.  Then
hit the g key to go or run the program

                if((ra=='g')||(ra=='G'))
                {
                    hexstring(entry);
                    uart_flush();
                    BRANCHTO(entry);
                }


Boot Linux from Flash NO RESET PRESSED.
## Booting image at bc050000 ...
   Image Name:   notmain
   Image Type:   MIPS Linux Standalone Program (uncompressed)
   Data Size:    1376 Bytes =  1.3 kB
   Load Address: 01000000
   Entry Point:  01000000
   Verifying Checksum ... OK
OK
12345678

SREC
00000000
00000000

And the led will blink.

I have left an omega2.bin in this base directory for you to use in case
you are struggling to get your toolchain to work.  At least you can
see the program load into flash and run on boot.

Note the documentation for this chip says it is a MIPS24KEc but if I
use -march=24kec on the gcc command line then bootloader01 does not
build a working binary.  So I am going ultra concervative and using
-mips1.  The bootloader works now.

Note if using minicom you can use ctrl-a then s then select ascii
to download the srec file.  It spawns a separate program to do this
so there is some data loss as it returns back to minicom so you wont
see the first entry printout, but should see it when you enter g to go.
I have not determined if the uart_flush() is correct yet.

Once this is in place you dont need to do the thumb drive dance anymore
you can simply reset or power cycle (does the reset button even work
as a reset?) to run the bootloader from on board flash.  Makes life
much easier.

----------------------------------------------

The processor id register reads as

00019655

0x96 family  which is the 24KE from what I can tell

00019655 PRId
80040483 Config0
BEE3519E Config1
80000000 Config2

-----------------------------------------

As I learn more about MIPS.  The 32 bit address space is broken up
into mirrored sections.  The first half starting at 0x00000000 is
user memory space, through a tlb for user access.  Dont want that.
Starting at 0xA0000000 we have uncached kernel/priveledged access
(to the the first 0x20000000 bytes at 0x00000000).  Want that.
Basically address 0x00000000 is mapped to a number of places depending
on what address you use to access can/may determine cachability
or if you are behind the mmu.  Initially dont want any of that want
clean priveleged access.  So these examples are setup to have the
stack come down from 0xA0FFFFFC and the program start at 0xA0000000.
The bootloader is setup to use 0xA1000000 to 0xA1FFFFFC.  It is not
all of the memory for the omega2 not plus, but enough for these
projects.


onion_omega2_samples's People

Contributors

dwelch67 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.