Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Roll your own... hc12

2 views
Skip to first unread message

David Bourgoyne

unread,
Oct 25, 2002, 8:09:18 AM10/25/02
to

A friend of mine is looking for a solution to a problem/code he
inherited. This all runs on a hc12. His application is event driven.
Basically main() is just a big loop waiting on messages. When a
message comes in, he goes to a table and calls the function associated
with a message. The function runs to completion. Here's the twist...

Inside of the function, at some point, he would like to modify the
message association table and then return (this return is not at the
end of the function). Then when another message comes in (we're back
looping in main() at this point), execution begins in the same
function right below where he returned. Thus, he would sort of emulate
a blocking read in a mult-tasking OS.

I don't think he needs to build a multi-tasking OS. For example, he
does not have to remember stack information, that is, do a real
context switch. I just thinks he has to store the instruction pointer
in that message association table and jump to the address upon receipt
of a message.

Does anyone have a slick way of doing this? Anyone have a snippet of
hc12 code that does something like this?

Thanks,

David

--
A wise man once said: If it didn't come with source, it's firmware not software.

David W. Bourgoyne
david.b...@bigfoot.com

CBFalconer

unread,
Oct 25, 2002, 10:30:35 AM10/25/02
to
David Bourgoyne wrote:
>
> A friend of mine is looking for a solution to a problem/code he
> inherited. This all runs on a hc12. His application is event driven.
> Basically main() is just a big loop waiting on messages. When a
> message comes in, he goes to a table and calls the function associated
> with a message. The function runs to completion. Here's the twist...
>
> Inside of the function, at some point, he would like to modify the
> message association table and then return (this return is not at the
> end of the function). Then when another message comes in (we're back
> looping in main() at this point), execution begins in the same
> function right below where he returned. Thus, he would sort of emulate
> a blocking read in a mult-tasking OS.

Avoid it like the plague. Using such self-modifying code leads to
madness.

Instead, make the called routine a state machine. It needs only a
single state variable, which it sets on exit, and uses on entry.
In C, something like:

static enum {STATE0, STATE1, ... LASTSTATE} state;

void routine(...) {

switch state {
case STATE0 :
state = dostate0; break;
case STATE1 :
state = dostate1; break;
....
}
return;
}

--
Chuck F (cbfal...@yahoo.com) (cbfal...@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!


Steve Fairhead

unread,
Oct 25, 2002, 11:59:39 AM10/25/02
to
"CBFalconer" <cbfal...@yahoo.com> wrote in message
news:3DB94E0E...@yahoo.com...

> Avoid it like the plague. Using such self-modifying code leads to
> madness.

Absolutely agreed. There's always a better way.

> Instead, make the called routine a state machine. It needs only a
> single state variable, which it sets on exit, and uses on entry.

... And I totally agree that a state machine is the better way :).

Steve
--
http://www.sfdesign.co.uk - SFD: Solutions by Design
http://www.fivetrees.com - ecommerce for independent musicians


Nuno Alves

unread,
Oct 26, 2002, 4:45:55 PM10/26/02
to
well... you could always put the table on a predifined place in memory and
change it using asm routines. Ive done things like that in the past... but
its usually a big headache to debug it.

--

Nuno Alves
nuno...@nunoalves.com

Cell Phone: 617-529-7597
Homepage: http://www.nunoalves.com
AOL Instant Messenger: nunoalves7
MS Windows Messenger: nuno...@nunoalves.com


"CBFalconer" <cbfal...@yahoo.com> wrote in message
news:3DB94E0E...@yahoo.com...

Paul Keinanen

unread,
Oct 26, 2002, 7:07:41 PM10/26/02
to
On Fri, 25 Oct 2002 12:09:18 GMT, David Bourgoyne
<david.b...@bigfoot.com> wrote:

>
>A friend of mine is looking for a solution to a problem/code he
>inherited. This all runs on a hc12. His application is event driven.
>Basically main() is just a big loop waiting on messages. When a
>message comes in, he goes to a table and calls the function associated
>with a message. The function runs to completion. Here's the twist...
>
>Inside of the function, at some point, he would like to modify the
>message association table and then return (this return is not at the
>end of the function). Then when another message comes in (we're back
>looping in main() at this point), execution begins in the same
>function right below where he returned. Thus, he would sort of emulate
>a blocking read in a mult-tasking OS.

If you only need to switch between two functions, then a coroutine
structure could be used. In this an indirect subroutine call using
(and popping) the return address from the top of the stack will put
your own return address on top of the stack. To switch back, the other
function issues an identical indirect call.

I don't know if this can be done in the hc12.

It should be noted that the old return address must be first popped
from the stack before the indirect call is executed, effectively
replacing the old return address with your own. This can also be done
by pushing a copy of the original return address on the stack, replace
the original reply address with an address following the RTS
instruction and executing the RTS instruction. This will pop off the
copy of the original return address, leaving your own return address
on top of the stack.

Paul

Ralph Mason

unread,
Oct 27, 2002, 4:59:16 AM10/27/02
to
If you interpreted his posting the way I did he has a function table in
ram - so that isn't self modifying code and changing the entries in that
table *is* a state machine.

although if that was his structure he probably wouldn't have asked the
question.

Ralph

"Steve Fairhead" <st...@deleteme-fivetrees.com> wrote in message
news:apbpoh$2dq$1$8302...@news.demon.co.uk...

0 new messages