Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 12 of 12

Thread: Teensy 4.0 Execute Program from SD Card/RAM

  1. #1
    Junior Member
    Join Date
    Feb 2020
    Posts
    5

    Teensy 4.0 Execute Program from SD Card/RAM

    Hello,

    I recently bought a Teensy 4.0 and have been having a lot of fun with it. I've played with arduinos and other micro-controllers before but have mostly done basic things like using GPIO, PWMs and talking to SPI/I2C devices.

    I've been trying to take a deeper dive and from reading the forums have so far learned how to use a makefile to compile outside of the arduino environment as well as calling a function from a simple assembly file into the blinky LED program.

    A couple days ago I came across a thread that really piqued my interest: https://forum.pjrc.com/threads/26995...-off-a-SD-card

    While the project in itself seemed pretty cool (running games off an SD card), a lot of the topics involved to get it working are something that I know very little about but would like to learn how to do.

    From Paul's Comments:
    • You'd need to edit the linker file, so the compiler creates the code for the RAM address, rather than flash.
    • Your code will presumably need both static and stack based variables.
    • You could create your linker script to just put your static variables right after your code, in the same block of RAM.
    • You could even simplify the startup routines at the beginning of your code, since it wouldn't need to copy static initialization from flash to ram. Presumably, you'd use the already-established stack, set up by the code running from flash.
    • You'll probably need to edit the normal linker script too, to make a big chunk of the RAM with a fixed starting address available.
    • Then you could have the flash-based code simply copy the binary data from a file on the SD card to the big RAM buffer.
    • Achieving any sort of integration between the RAM-based code and flash based code will make the arcane syntax of linker scripts and C function pointer type casting look like a walk in the park!
    • Using interrupts from the RAM-based code will be the first huge challenge. Probably the simplest way would be to just create a new vector table in the RAM image and use it.
    • But if you want to actually use any of the code in flash, you're going to have to figure out some way for your RAM-based program to know where the flash-based code allocated stuff in the _other_ RAM that's not dedicated to your program.


    I've read through a few other threads that seemed to ask similar things and it seems the moral of the story is: this very very difficult and I have a high chance of ending up with a bricked board.
    https://forum.pjrc.com/threads/31673...cation-loading
    https://forum.pjrc.com/threads/38559...m-3-2-from-can
    https://forum.pjrc.com/threads/29772...h-from-SD-card
    https://forum.pjrc.com/threads/30723-SD-Bootloader
    https://forum.pjrc.com/threads/24302...RAM-(Teensy-3)


    Since this topic seems to come up frequently I figured there might be someone that already ran through these hurdles and has code available that I could try to learn from but so far I have been unable to find it.

    I have tried to get started by reading up on a lot of the vocabulary and googling the topics but I have been having a lot of trouble figuring out just how to implement it on the Teensy 4.0.

    I understand that a lot of these topics are very complex but I figured I gotta start somewhere and I really like this project. What I'm looking for is any documentation, code examples or guidance that could help me get to the goal of running a simple game off removable memory (I was thinking tic tac toe to begin with). Thanks!

  2. #2
    Senior Member+ Frank B's Avatar
    Join Date
    Apr 2014
    Location
    Germany NRW
    Posts
    6,243
    To load from SD:
    On T4,I'd say you just need to remove the startup code, and write a short new one which relocates the vectors and sets the stack pointer.
    On exit, take back the vector-table relocation and restore the stackpointer to the old location (or just do a reset)

    You'll need to modify the linkerscript and set a new adresses for the programs that will be loaded.

    You will have to fiddle with the iTCM and DTCM blocks. Maybe reserve some RAM in ITCM by using a large dummy array in the "loader"
    Last edited by Frank B; 02-07-2020 at 08:35 AM.

  3. #3
    Senior Member+ Frank B's Avatar
    Join Date
    Apr 2014
    Location
    Germany NRW
    Posts
    6,243
    What happens with USB is another question. I'm not sure about that.
    Might be needed to set it to "no USB" for the loaded program. Or Paul knows a way to reset the USB stack.
    Last edited by Frank B; 02-07-2020 at 07:48 AM.

  4. #4
    Senior Member+ Frank B's Avatar
    Join Date
    Apr 2014
    Location
    Germany NRW
    Posts
    6,243
    ..but the easiest is just to make use of the large flash and to put your games in functions
    void game_1().. game_n() and call them from your menu.
    Done.

  5. #5
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    10,641
    Perhaps even a single common entry point in FLASH where a single game might be loaded to that FLASH address from the SD card ?

  6. #6
    Junior Member
    Join Date
    Feb 2020
    Posts
    5
    Quote Originally Posted by Frank B View Post
    To load from SD:
    On T4,I'd say you just need to remove the startup code, and write a short new one which relocates the vectors and sets the stack pointer.
    On exit, take back the vector-table relocation and restore the stackpointer to the old location (or just do a reset)

    You'll need to modify the linkerscript and set a new adresses for the programs that will be loaded.

    You will have to fiddle with the iTCM and DTCM blocks. Maybe reserve some RAM in ITCM by using a large dummy array in the "loader"
    Just to be sure, the startup script and linker file are named startup.c and imxrt1062.ld in the Arduino install folder right?
    C:\Program Files (x86)\Arduino\hardware\teensy\avr\cores\teensy4

    I'll take a look through them and see if I can understand how they work so that I can edit them, but I might end up coming back with more questions.

  7. #7
    Junior Member
    Join Date
    Feb 2020
    Posts
    5
    Quote Originally Posted by Frank B View Post
    ..but the easiest is just to make use of the large flash and to put your games in functions
    void game_1().. game_n() and call them from your menu.
    Done.
    Quote Originally Posted by defragster View Post
    Perhaps even a single common entry point in FLASH where a single game might be loaded to that FLASH address from the SD card ?
    Well, I know it would take a lot of writes, but I was worried constantly loading different games might end up wearing out the flash faster?

  8. #8
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    21,365
    This really depends on the meaning of "constantly loading" and whether that behavior really is sustained over a long period of time.

    Teensy 4.0's flash chip is rated for an endurance of 100K cycles. So if you reprogram it 20 times each day, you'll reach the rated endurance after 13.7 years of consistently reprogramming almost every hour.

    But if you write to it every minute, 24 hour a day (and without any wear leveling, as the EEROM emulation does) then you'll reach the endurance spec after only 2 months.

    100,000 lasts a very long time for human activity which requires any significant amount of effort. But for machine-driven actions which can happen much more rapidly and are likely to be consistently performed over long timer, 100K isn't such a huge number.

  9. #9
    Senior Member+ Frank B's Avatar
    Join Date
    Apr 2014
    Location
    Germany NRW
    Posts
    6,243
    Quote Originally Posted by rohan2 View Post
    Well, I know it would take a lot of writes, but I was worried constantly loading different games might end up wearing out the flash faster?
    My idea was to put them all in one program. You'll have not a single additional write
    Requires no fiddling, and works out of the box.

    Your idea with loading into RAM or even flash different programs is interesting, and my fingers are itching to work on it - but the result would not justify the effort in my opinion. The flash is so big. So I can control myself and will not work on it :-)

  10. #10
    Junior Member
    Join Date
    Feb 2020
    Posts
    5
    Quote Originally Posted by PaulStoffregen View Post
    This really depends on the meaning of "constantly loading" and whether that behavior really is sustained over a long period of time.

    Teensy 4.0's flash chip is rated for an endurance of 100K cycles. So if you reprogram it 20 times each day, you'll reach the rated endurance after 13.7 years of consistently reprogramming almost every hour.

    But if you write to it every minute, 24 hour a day (and without any wear leveling, as the EEROM emulation does) then you'll reach the endurance spec after only 2 months.

    100,000 lasts a very long time for human activity which requires any significant amount of effort. But for machine-driven actions which can happen much more rapidly and are likely to be consistently performed over long timer, 100K isn't such a huge number.
    Quote Originally Posted by Frank B View Post
    My idea was to put them all in one program. You'll have not a single additional write
    Requires no fiddling, and works out of the box.

    Your idea with loading into RAM or even flash different programs is interesting, and my fingers are itching to work on it - but the result would not justify the effort in my opinion. The flash is so big. So I can control myself and will not work on it :-)
    Thanks guys, appreciate the responses! I think I'll give it a go. I'm thinking about starting out with RAM and if it gets too difficult i'll switch over to flash.

  11. #11
    Senior Member+ MichaelMeissner's Avatar
    Join Date
    Nov 2012
    Location
    Ayer Massachussetts
    Posts
    3,490
    Note, if you need flexibility but don't need the speed, perhaps the best solution is to work with the Circuit Python folks on the Teensy 4 fork. Then you should be able to install new python scripts from the SD card.

  12. #12
    Junior Member
    Join Date
    Feb 2020
    Posts
    5
    Yeah, python would make this a whole lot easier, but I feel like the extra work for the speed boost might be worth it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •