Run another program slot.
The 28X2/40X2 parts have four completely separate internal program slots. By default program 0 runs whenever the part is reset. The 20X2 only supports slot 0.
A new program is downloaded into any slot via the #slot directive, which is added as a line to the program. It is only possible to download one program to one slot at a time. The other programs are not affected by the download.
To run the second program (after downloading with a #slot 1 directive) use the command 'run 1'. This command stops the current program and starts the second program running immediately. Variables and pin conditions are not reset, so can be shared between the programs. However all other system functions, such as the gosub/return stack, are reset when the second program starts. Therefore slot 1 program can only be considered as a a goto from the slot 0 program, not a gosub.
When in program 1 you can also use 'run 0' to restart the first program. If you wish to also reset the variables you must use a 'reset' command instead to restart program 0. This is equivalent to 'run 0' + variable reset.
Note that when carrying out a new program download the download is into the first program slot by default. If you wish to download into the second program slot you must use the '#slot 1' directive within the program.
All X2 parts also support running programs from external i2c EEPROM chips. These are known as program slots 4 to 7 (on an EEPROM with address 000). As up to 8 possible external EEPROM addresses may be used, that gives a theoretical total of 32 (8x4) external programs. When using an EEPROM not at address 000, bits 7-5 of the slot number are used as the EEPROM address, e.g. for an EEPROM with address pins A2 low, A1 high and A0 high, running slot 5 would be:
run %011xx101 (where x = 0 or 1, don't care)
When running a program from an external EEPROM chip certain restrictions apply:
1) The i2c SDA and SCL pins are reserved, and so the i2c bus cannot be used for other commands
2) Program operation will be marginally slower, as retrieving data from an external EEPROM is slower than retrieving data from the internal program memory.
Also see the 'booti2c' command, which may be preferable to using slots 4-7.
Additional Information - Understanding Program Slots
The X2 range have up to 4 internal program slots, numbered 0 to 3. Each slot is completely independent of the other slots. When the microcontroller is reset the program in slot 0 automatically starts running. The other programs can then be started by using a 'run' command.
A new program download is, by default, into slot 0. To download into another program slot the #slot directive must be used in the program, .e.g. '#slot 1' will download the program into slot 1 instead of slot 0. All other slots are unaffected.
Note that when the download is complete the program will always start running from slot 0, not the slot just downloaded. If you wish to instantly test, for instance, a program downloaded into slot 1, the command 'run 1' must have been previously downloaded into slot 0.
As the microcontroller only has one internal EEPROM data area (used by the EEPROM, read and write commands) any download into any internal memory slot will always update the same EEPROM memory. To disable this update it is possible to use a #no_data directive in the downloaded program. This prevents the EEPROM data area being updated (i.e. any EEPROM command data is ignored).
The usual way to make use of the program slots is to test an input (e.g. jumper link) upon reset, and then run the different program according to the input condition.
However program slots can be combined into one 'long program' as long as the following points are noted:
1) No gosubs (including the interrupt) can be shared between program slots.
2) The gosub/return stack is reset when moving from one slot to another.
3) Outputs and variables/scratchpad are not reset.
4) The 'run X' command should be regarded as 'goto to the start of program X'
Note that 'run 0' is not the same as the 'reset' command, as the reset command will also reset all variables and convert all pins back to inputs.
External Program Slots
As well as the internal memory slots, 4 additional slots can be used by connecting an external i2c EEPROM chip (part 24LC128). As up to 8 different 24LC128 chips could be used on the same I2C bus, this gives a theoretical 32 (8x4) additional program slots.
For an 24LC128 at address 0 (ie pins A0, A1, A2 all connected to 0V) the i2c program slots are simply numbered 4 to 7. For other 24LC128 addresses the run (and #slot) number must be calculated as follow:
||24LC128 address pin A2
||24LC128 address pin A1
||24LC128 address pin A0
||reserved for future use, use 0
||reserved for future use, use 0
||1 = I2C
||0 = internal
||4 possible slot numbers
Running a program from external i2c has some restrictions
1) The i2c bus is reserved exclusively for the program reading.
2) The i2c pins cannot be used for any other purpose.
3) Any hardware i2c/spi commands are completely ignored.
4) Program execution speed is reduced, due to the relatively slow speed of reading data from the external 24LC12.
The external 24LC128 only stores the program memory space. Any download data memory information (ie from the EEPROM command) is not stored externally. Read and write commands continue to act on the internal PICAXE EEPROM data memory space.