Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 00:29 12 Nov 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : MMbasic : callbacks, register access

Author Message
vincenthimpe
Regular Member

Joined: 14/10/2018
Location: United States
Posts: 86
Posted: 07:53pm 14 Oct 2018
Copy link to clipboard 
Print this post

Is it possible to do direct hardware access in MMbasic. For example: alter one of the CPU registers or an I/O register using a basic function (like peek and poke).

for example: i want more granular control over the I2C bus and want access to the registers of the I2C controller so i can implement Restart and clock stretching.
For that i need access to the registers of the Io block. the built in functions don't suit me. I want low level functions like I2C_Start , I2C_Stop , i2C_restart , Sendbyte, receivebyte, give_ack , get_ack , and control the SDA and SCL pins ( like set SCL hard low to block the bus ) If i have register access i can do this in basic.

IS it possible to build callback functions ?

for example : i have a hardware interrupt pin. Depending on what happens the interrupt handler needs to call user provided functions. if no user provided funciton is given the interrupt handler simply exits.

pseudo code :

ui.condition1 = addressof(something)
ui.condition2 = addressof(somethingelse)

sub something
end sub

sub somethingelse
end sub

sub Ui_handler (handles int falling_edge on pin(122))
static condition1 = null
static condition2 = null
static condition3 = null
if <condition> and (condition1<>null) then call_by_address(condition1)
end sub
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 10572
Posted: 07:41am 15 Oct 2018
Copy link to clipboard 
Print this post

  Quote  Is it possible to do direct hardware access in MMbasic. For example: alter one of the CPU registers or an I/O register using a basic function (like peek and poke).


Yes, PEEK and POKE to registers are supported
For more complicated stuff you need CFunctions. See the details in Geoff's Micromite release download which includes links to 4 tutorials I posted.
 
vincenthimpe
Regular Member

Joined: 14/10/2018
Location: United States
Posts: 86
Posted: 04:26pm 15 Oct 2018
Copy link to clipboard 
Print this post

perfect !. thanks.
I studied the MZ datasheet a bit and there are actually 6 I2c controllers. all identical , just with offsets. The current i2c code is pretty convoluted ( i have the C sourcecode) and i'm thinking of writing a basic library that is portable between the channels. you just provide the channel and the code uses different offsets.

I am trying to port an existing hardware project from Teensy to the Extreme version.
I use a capacitive touch screen (as opposed to resistive which looks dull and reduces luminosity of the panel) and i use one i2c bus for internal household : buttons, led's , navipad (rotary encoder with 5 way tactile pad). battery backed RTC , battery gauge / charger and more stuff.

The hardware is open source and posted on Circuitmaker hub.
I wanted callbacks because my architecture is so that there is one system interrupt that needs servicing. This interrupt will generate events like
key<x>_[down/up/click] where key is [up/down/left/right/a/b/c/standby/select]
rotary.[increment/decrement/lowlimit/highlimit/change]

The rotary encoder uses a table so i can dynamically switch what variable is controlled by it without user interaction.

const value1 1
const value2 2

' Setup this variable so that data lies between lower and upper bound in steps of 1. whenever the encoder is manipulated we get an interrupt ( if the encoder is currently'bound' to the variable.)

rotary(value1).lowerbound = 4
rotary(value1).upperbound = 9
rotary(value1).interrupt = always
rotary(value1).step = 1

' Setup this variable so that data lies between lower and upper bound in steps of 0.1. We only get an interrupt if data has changed AND the encoder is clicked (center button) or we hit zero. other possible interrupt scenarios : on_increment, on_decrement, on_max,on_min,on_zero. this is a bitfield so it can be or-ed together.

rotary(value2).lowerbound = -2
rotary(value2).upperbound = +2
rotary(value2).step = 0.1
rotary(value2).interrupt = on_confirm | on_zero
rotary(value2).handler = addressof (value2_handler)

rotary.select(value2) ' binds the encoder to value2. whenever the user turns the data updates and an event fires

sub value2_handler(eventmask) .....
if (eventmask and on_zero) then print "zero !"
if (eventmask and on_confirm) then print "new value : ", rotary.currentvalue
end sub






 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025