The first step is to create the COG assembly language program to be loaded into our COG. For this example I used the Parallax Spin IDE to create the COG program. The COG program is created the same as any normal COG assembly object. The one difference is we will not be using any Spin language code at the start of the object. Since the Spin IDE requires at least one spin method before the DAT section containing the COG assembly language code we will include a stub function with no actual spin code. The following code snippet shows the start of the real time clock spin object source code.
CON
_clkmode = xtal1 + pll16x
_xinfreq = 5_000_000
PUB Start
''does nothing but must be there for spin IDE to compile
DAT
org 0
Notice in the code snippet that the "Start" public method has no code to execute it is only there to satisfy the Parallax IDE.
For this example the source for the COG object can be found in the "Forth\Timer COG\timer.spin" file included with the MV4th demo version 0.91 and higher source files. To compile the COG object into a binary file load the "timer.spin" file in the parallax IDE and select the "Run/Compile Current/View Info" menu selection or press the F8 function key. If your object compiles correctly the IDE will display the Object Info Dialog shown below. If the hex display is not showing in the Object Info Dialog, press the "Show Hex" button.
Press the "Save Binary" button and save the file to your computer. Make sure the saved file has the ".binary" file extension. We will need this file later so remember where you put it.
In order for MV4th to start the COG, MV4th will require access to a copy of the binary image to load into the COG. The only persistent storage MV4th has access to is the EEPROM based block device. Each MV4th block device block is 1k bytes in size. For this example we will store the binary image into device block index 31. Our binary COG test program uses less then 1k of space so it will fit in a single block. If your object binary is larger then 1k bytes (256 longs) it will require 2 forth device blocks.
The ForthTerm program available in the MV4th16 dowloads will be used to upload the binary image to the forth block device. See the article Getting Started With MV4th16 for more information on using ForthTerm.
With ForthTerm connected to the MV4th system reset the propeller board and wait for the MV4th prompt to appear. In the ForthTerm program select the "Upload Block File" command under the Communications main menu. In the displayed file select dialog select the "Binary File" option in the file extension selection,then slect the "timer.binary" file we created in the first step. ForthTerm will now display a dialog that the Forth block load words are not loaded and offer to upload the code to the MV4th system, select "OK" to continue.
Once the code has been transfered to the MV4th system the ForthTerm program will display the "Transfer Blocks" dialog. Set the "First Block" setting to 31 then press the "Upload" button.
If no error is reported by the ForthTerm program you will have placed a copy of the COG binary image into block 31 of the MV4th block device.
The Forth source code for this exampleis provided in the text file "Forth\Timer COG\timer.forth". This source code has also been saved to the forth block file "Forth\Timer COG\timer.forth.blk" using the ForthTerm program. The timer Forth source code is comprised of two main parts; the first part of the code is used to set and read the real time clock, and the second part of the code is used to initialize and start the COG running the timer.
The ForthTerm program can be used to upload the block file "Forth\Timer COG\timer.forth.blk" file in the same maner we loaded the COG binary image file above. When loading this example save the block file to block index 30 on the MV4th system. After the block file has been uploaded you can run the forth real time clock COG. To run the real time clock COG type "30 LOAD" without the quotes and press enter. A prompt should indicate the COG index of the timer COG.
To set the time for the clock, type the following at the MV4th prompt; "$11223344. set-time" without the quotes and press enter. This will set the time to 11:22:33.44 (of course you would enter the correct time in 24 hour format). Do not forget the period at the end of the constant to indicate a double precision value.
To set the time to 0 used the forth word clr-timer.
To display the current time type "timer." without the quotes and press enter.