RISCOS I2C Access


2 posts
by Shawty » Fri Jun 09, 2017 10:05 pm
For those that are interested (Iv'e seen a few Q's asking) Iv'e just been mucking about with R15 of RiscOS on my Pi 2.

I have an MCP23017 16 Port I/O extender board and an ADXL345 Acceleromter, both of which are I2C devices.

Been a while, but I broke out BBC Basic, and started mucking about with SWI IIC_Control, and hey presto, I can now read/write both devices straight from Basic 5.

If anyone is interested Iv'e attached 2 text files to this post, with the BBC Basic code in.
Scratch that, apparently the TXT file extension is not safe to upload to this forum???? so I'm pasting the txt inline in my message instead:

BBC Basic code for accellerometer


REM IIC Basic test

ON ERROR:REPORT:PRINT " at line ";ERL:END

SYS "OS_ReadSysInfo", 14 TO numIICs%
PRINT "There are ";numIICs%;" IIC Buses on this device"

device% = &53

PROCinitAccelerometer(device%)

WHILE 1
PROCreadAccelerometer(device%)
ENDWHILE


END

DEF PROCinitAccelerometer(dev%)

PROCwriteRegByte(dev%, &31, &0B)
PROCwriteRegByte(dev%, &2D, &08)
REM PROCwriteRegByte(dev%, &2E, &00)
PROCwriteRegByte(dev%, &1E, &00)
PROCwriteRegByte(dev%, &1F, &00)
PROCwriteRegByte(dev%, &20, &00)

PROCwriteRegByte(dev%, &21, &00)
PROCwriteRegByte(dev%, &22, &00)
PROCwriteRegByte(dev%, &23, &00)

PROCwriteRegByte(dev%, &24, &01)
PROCwriteRegByte(dev%, &25, &0F)
PROCwriteRegByte(dev%, &26, &2B)
PROCwriteRegByte(dev%, &27, &00)

PROCwriteRegByte(dev%, &28, &09)
PROCwriteRegByte(dev%, &29, &FF)
PROCwriteRegByte(dev%, &2A, &80)
PROCwriteRegByte(dev%, &2C, &0A)
PROCwriteRegByte(dev%, &2F, &00)
PROCwriteRegByte(dev%, &38, &9F)

ENDPROC

DEF PROCreadAccelerometer(dev%)

x0% = &FF - FNreadRegByte(dev%, &32)
x1% = &FF - FNreadRegByte(dev%, &33)

y0% = &FF - FNreadRegByte(dev%, &34)
y1% = &FF - FNreadRegByte(dev%, &35)

z0% = &FF - FNreadRegByte(dev%, &36)
z1% = &FF - FNreadRegByte(dev%, &37)

X% = (x1% << 8) + x0%
Y% = (y1% << 8) + y0%
Z% = (z1% << 8) + z0%

PRINT "X=";X%;" Y=";Y%;" Z=";Z%

ENDPROC

DEF PROCwriteRegByte(dev%, reg%, val%)

DIM blk% 5
blk%?0 = reg%
blk%?1 = val%
SYS "IIC_Control", (dev% << 1) + 0, blk%, 2

ENDPROC

DEF FNreadRegByte(dev%, reg%)

DIM blk% 5
blk%?0 = reg%
SYS "IIC_Control", (dev% << 1) + 0, blk%, 1
SYS "IIC_Control", (dev% << 1) + 1, blk%, 1

=?blk%


If you wire the accelerometer up the same way as is shown in this article:
https://www.sunfounder.com/learn/Super_ ... rrypi.html

Then it will appear on the I2C bus as device number &53

BBC Basic code for IO extender


REM IIC Basic test

ON ERROR:REPORT:PRINT " at line ";ERL:END

SYS "OS_ReadSysInfo", 14 TO numIICs%
PRINT "There are ";numIICs%;" IIC Buses on this device"

REM device number is shifted left by 1 as bottom bit in dev number is r/w control
device% = &27 << 1
DIM block% 10

block%?0 = &00 :REM IODIRA (Port A Direction Reg bit = 0 - output, bit = 1 - input)
block%?1 = &00 :REM Set all lines as output
SYS "IIC_Control", device% + 0, block%, 2

block%?0 = &14 :REM OLATA (Port A Output latch)
block%?1 = &01 :REM Turn on IO Line 1, Port A
SYS "IIC_Control", device% + 0, block%, 2

INPUT a$

block%?0 = &14 : REM OLATA (Port A Output Latch)
block%?1 = &00 : REM Turn off IO Line 1, Port A
SYS "IIC_Control", device% + 0, block%, 2

INPUT a$

block%?0 = &01 : REM IODIRB (Port B Direction reg)
block%?1 = &01 : REM Set port B line 1 as an input
SYS "IIC_Control", device% + 0, block%, 2

block%?0 = &13 : REM GPIOB (PORT B Realtime I/O register)
REM Set register to read (dev + 0 means write)
SYS "IIC_Control", device% + 0, block%, 1

REM read the selected register (dev + 1 means read)
SYS "IIC_Control", device% + 1, block%, 1
PRINT ?block%


The device address for my extender is &27, as it has solder pads on the back of the board, and that's what I soldered them to be (It was a build it yourself kit from maplin [Made by cisero or something similar])

If there's enough interest, I have the full Acorn C development kit and assembler, I could easily write a relocatable module with SWI's in to allow other to easily use these devices.

Hope this is usefull to someone.

Shawty
still crazy (Even since the days of my BBC Model B) BEST and only way to be ;-)

IM: @shawty_ds on twitter
if you remember the Acorn and BBC days then I was "!Shawty! of DSPD" (Author of the BBC B Sound Tracker suite, and the Dreamscape demo)
User avatar
Posts: 57
Joined: Fri Nov 16, 2012 1:22 am
Location: North East UK
by Steve Drain » Sat Jun 10, 2017 6:27 pm
Shawty wrote:I have an MCP23017 16 Port I/O extender board and an ADXL345 Acceleromter, both of which are I2C devices.

Been a while, but I broke out BBC Basic, and started mucking about with SWI IIC_Control, and hey presto, I can now read/write both devices straight from Basic 5.


You might want to look at SWI OS_IICOp, which encompasses IIC_Control, and makes the use of IIC more straightforward.

I have programmed the MCP23017 using it, both from BASIC and in a module:

http://www.kappa.me.uk/Modules/swMCPIO020.zip

However, that may not be sufficiently up to date for PI3.
Posts: 87
Joined: Tue Oct 30, 2012 2:08 pm
Location: Exeter UK