Peek and poke in uLisp


#1

I assume there currently is no way to peek and poke in the special function registers of the arduino due (or whatever board) ?
Can this easily be done , or could it be added in later versions of ulisp ?

Kind regards

Ronny Suy


#2

What functions would you suggest?

You could add them yourself; see:

Adding your own functions


#3

I was thinking of :

(peek address) -> returns the value of the given address

(poke address data) -> store the data on address

Both examples for 32 bit SAM arduino boards

I will try to write something ,following your link , and keep you posted of my progression .

Thanks Ronny


#4

I’m a bit stuck on this:

// Insert your own function definitions here

object *fn_peek (object *args, object *env) {
(void) env;
object *arg1 = first(args);
//code for reading the arg1 as an address and reading the value of it -> but how ???
return (value);
}

whatever I try I mostly get errors like this :

/home/ronny/Arduino/due_2.3/due_2.3.ino: In function ‘object* fn_peek(object*, object*)’:
due_2.3:2974: error: invalid conversion from ‘int’ to ‘object* {aka sobject*}’ [-fpermissive]
return (*val);

Anybody any idea’s ??


#5

Can you give some examples of what you want to use peek and poke for?


#6

Hi

My intention is to be able to read from , and write to , every memory location of an arm processor.
memory ranges from 0x00000000 to 0xffffffff.
This enables me to change special function registers directly in lisp .
For example , change the twi speed , spi speed and so on.

Regards ,
Ronny


#7

OK, here’s how to do it:

Define the functions:

// Insert your own function definitions here

object *fn_peek (object *args, object *env) {
  (void) env;
  int addr = integer(first(args));
  return number(*(int *)addr);
}

object *fn_poke (object *args, object *env) {
  (void) env;
  int addr = integer(first(args));
  object *val = second(args);
  *(int *)addr = integer(val);
  return val;
}

Note that poke returns the value you gave it - it doesn’t read back the contents of the address.

Add their names on the end of the existing list:

const char string174[] PROGMEM = "peek";
const char string175[] PROGMEM = "poke";

Add their entry points onto the end of the existing table:

  { string174, fn_peek, 1, 1 },
  { string175, fn_poke, 2, 2 },

Add their symbols onto the end of the enum function list:

MILLIS, SLEEP, NOTE, EDIT, PPRINT, PPRINTALL, PEEK, POKE, ENDFUNCTIONS };

Trying it out:

3071> (peek #x20000160)
0

3071> (poke #x20000160 123)
123

3071> (peek #x20000160)
123

Note that I had taken care to check that the memory location #x20000160 was safe to write in!


MMIO read/write access
#8

Hi Johnson

First of all , thanks very much for your effort !

I must tell you however it doesn’t seem to work on my arduino due

Peeking looks ok , but if i want to change an address , the value reed back with poke is always the same.

On the number 3071 in your example before the prompt i assume you tested it on an arduino zero , is that correct

So why shouldn’t this work on arduino due ??


#9

correction , I meant reding back with peek :-)


#10

Hi Johnson

My stupidity , I made a typo in the lookup table :-(
Its working perfectly now , sorry for this

Kind regards
Ronny Suy


#11

Great! Glad it’s working.