Author Topic: Using the uTasker Serial Loader with Teensy 3.1 Arduino/Teensyduino applications  (Read 11045 times)

Offline mark

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3234
    • View Profile
    • uTasker
Hi All

The following is a guide for Teensy 3.1 Arduino/Teensyduino users to allow their application to be used together with the uTasker Serial Loader.

1. Determine the application address that the serial loader uses. This is 0x8080 in the pre-built version but can be changed when building with the uTasker Serial Loader project (this is because the serial loader includes SREC and USB-MSD loading and stores a file object at 0x8080 so that the name, date, time of the loaded application can be displayed and the address can be lower when less serial loader features are enabled).

2. Link your project to this address by modifying the linker script setting (in mk20dx256.ld) from
FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 256K
to
FLASH (rx) : ORIGIN = 0x00008080, LENGTH = 256K

3. Note that the uTasker serial loader will either disable or enable the watchdog (1s timeout) depending on the state of the input pin 21 [A6]. Left unconnected it will enable it and holding it to ground at reset will cause it to be disabled.
The Teensyduino code will also do something similar but this will fail due to the fact that its unlock sequence code is too slow when the processor has already been initialised. A reset would fire "during" the unlock sequence and so this code can't be used.
Therefore, since the watchdog has already been enabled/disabled in the serial loader, remove the following code from mk20dx128.c. It is also recommeneded to generally use an active watchdog to allow system recovery in case or errors during opertion and so the watchdog should be periodically retriggered by the application code.

   WDOG_UNLOCK = WDOG_UNLOCK_SEQ1;
   WDOG_UNLOCK = WDOG_UNLOCK_SEQ2;
   asm volatile ("nop");
   asm volatile ("nop");

   // programs using the watchdog timer or needing to initialize hardware as
   // early as possible can implement startup_early_hook()
   startup_early_hook();


4. Depending on which version of the Teensy project you are using its interrupt vectors will either be in Flash or in RAM (from Teensyduino 1.20 they should be in RAM). When in RAM there is nothing more to be done but when in Flash there are two choices, depending on the link address being used:

a. If the link address is divisible by 512 (eg. 0x2800, 0x8000 etc.) simply set the Cortex M4's vector table offset to match. That is, modify the line of code in mk20dx128.c from
SCB_VTOR = 0;
to
SCB_VTOR = 0x00008000; // this must match the link address of the application when interrupts are fixed in Flash

b. In the case of 0x8080 or other addresses that are not divisble by 512 (this is a Cortex M4 NVIC restriction) it is not possible to work with fixed vectors in Flash and so these must be moved to RAM, which is simply done by using these lines of code instead:
   SCB_VTOR = 0x1fff8000; // set the location of interrupt vectors to the start of SRAM
   memcpy((void *)0x1fff8000, 0x00008080, 0x200); // copy the fixed vectors to SRAM


Plus by editing another linker script file (mk20dx256.ld) entry from
RAM  (rwx) : ORIGIN = 0x1FFF8000, LENGTH = 64K
to
RAM  (rwx) : ORIGIN = 0x1FFF8200, LENGTH = 64K-0x200
in order to keep this area free for the usage.

Note that from Teensyduino 1.20 the vectors should already be in RAM and so this step is not required.



5. The Teensy 3.1 Arduino/Teensyduino build creates an Intel Hex output file. The uTasker Serial Loader generally works with SREC or Binary files and the standard output can be converted to one of these by using GCC's objcopy - it is simplest to create a bat file with the commands that can be executed after each project build. Assuming that the output from the build is a file called software.hex the following commands will create both SREC and binary versions form it. In addition, using the uTasker utility "uTaskerCombine" [ http://www.utasker.com/forum/index.php?topic=1445.0 ] a combined serial loader/aplication binary can be created that can then be loaded as a single file (loader plus application), which is useful for a production environment in order to be able to load one complete software rather than first the loader and then the first application.

objcopy --input-target=ihex --output-target=binary software.hex application.bin
objcopy --input-target=ihex --output-target=srec software.hex application.srec
uTaskerCombine uTaskerSerialLoader.bin application.bin 0x8080 file3.bin combinedLoaderApp.bin

Note that the application's start address must also match when combining the binary files

6. When working with SD card loading (rarer case with the Teensy) the software file loaded to the SD card needs an additional header for authentication (and can be optionally encrypted). To do this the uTasker utility uTaskerConvert is used (see the utilities link above). The command for the reference serial loader without encryption is
uTaskerConvert.exe application.bin software.bin -0x1234 -a748b6531124
For full details of adding authentication and encryption see chapter 8 of http://www.utasker.com/docs/uTasker/uTasker_BM_Loader.pdf


After making these adjustments you should find that the Teensy 3.1 Arduino/Teensyduino application in question can work at the offset link address and so operate in conjunction with the uTasker serial loader.

Regards

Mark

« Last Edit: January 03, 2015, 04:55:18 PM by mark »