Sharing data between programs

Coding and general discussion relating to the compiler

Moderators: David Barker, Jerry Messina

Post Reply
SHughes_Fusion
Posts: 219
Joined: Wed Sep 11, 2013 1:27 pm
Location: Chesterfield

Sharing data between programs

Post by SHughes_Fusion » Mon Jan 26, 2015 10:11 am

Is there any way to 'tell' Swordfish where in memory to put a particular variable, or is there any other neat way to share data between programs?

I'm working on a few projects that use my Bluetooth bootloader. Previously I've never bothered checking the RCON register but I'm now finding I want to implement a battery powered system so I need to be able to detect brown-outs and watchdog wake from sleep etc.

As my bootloader will be the first code to run after a reset event I'd like it to store the relevant register contents somewhere that the main code can access to save the risk of them getting corrupted in the bootloader. However, I can see no way to access a specific memory location - and more importantly, to reserve it so no other use is made of it.

Does anyone have any suggestions?

Also, is there any generic way to detect a watchdog wake from sleep? Presumably the core will start executing commands from the one after sleep when the watchdog wakes the device, so do you just check for the appropriate bits in RCON and carry on executing code from there?

Jerry Messina
Swordfish Developer
Posts: 1473
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Re: Sharing data between programs

Post by Jerry Messina » Mon Jan 26, 2015 11:27 am

You can use the 'absolute' modifier in the dim statement.

For multiple variables I use something like this:

Code: Select all

// declare a struct that contains the persistent variables you want to keep
structure reg_block_t
    r1 as byte
    r2 as byte
    w1 as word
    w2 as word
end structure

// define the struct and locate it at the end of memory
const REG_BLOCK_SIZE = sizeof(reg_block_t)
const REG_BLOCK_ADDR as word = _maxram - REG_BLOCK_SIZE
dim regblk as reg_block_t absolute REG_BLOCK_ADDR

regblk.r1 = 1
regblk.r2 = 2
regblk.w1 = $55AA
The only thing to watch out for is that objects located 'absolute' aren't accounted for/tracked by the compiler.
To keep it from using that space you need to change the '#variable _maxram' setting, but unfortunately you have to do that by hand

Code: Select all

// reserve a fixed block of 30 bytes from the current end of ram
#variable _maxram = _maxram - 30

// these produce errors
#variable _maxram = _maxram - sizeof(reg_block_t)
#variable _maxram = _maxram - REG_BLOCK_SIZE
That's because the preprocessor can't use the 'sizeof' or 'const' language elements... it doesn't know about them.

Post Reply