Page 1 of 1

RISCOS I2C Access

Posted: Fri Jun 09, 2017 10:05 pm
by Shawty
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

Code: Select all

[i]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%
[/i]
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

Code: Select all

[i]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%[/i]
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

Re: RISCOS I2C Access

Posted: Sat Jun 10, 2017 6:27 pm
by Steve Drain
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.

Re: RISCOS I2C Access

Posted: Thu Sep 21, 2017 1:38 pm
by Ronaldlees
Steve Drain wrote:
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.
Thanks. Been looking for something like this.

Re: RISCOS I2C Access

Posted: Thu Sep 21, 2017 5:39 pm
by mahjongg
added code tags to original post.

Re: RISCOS I2C Access

Posted: Wed Nov 29, 2017 4:33 am
by Norman Lawrence
A belated thanks for posting your code. It helped me to understand how to address equipment using the IIC bus. I have an Expander PI so your code for the 16 channel digital Input / output was exactly what I needed. Next task is to learn how to address the ADC and DAC part of the Expander Pi board using the SPI bus.
Many thanks :)

Re: RISCOS I2C Access

Posted: Mon Jul 30, 2018 8:57 pm
by alexgibsondotnet
Hi Shawty
Thanks for sharing this info (last year!) and I wonder as you have plenty of history with Acorns whether you could point me in the right direction?

- Does the code you shared work equally for older Acorn machines with I2C bus on the podule backplanes????

For reference, I'm looking at Page 21, Table 2 of this:
https://www.chiark.greenend.org.uk/~the ... xpspec.pdf

I would like to interface my old RiscPC with an Arduino and a Raspberry Pi, and this seems like the most promising method to me... am I barking up the wrong tree or onto a winner?

Cheers
Alex

Re: RISCOS I2C Access

Posted: Wed May 01, 2019 10:01 pm
by Shawty
Hi alex,

First let me say a big sorry.

I posted this then kind of forgot it existed, and purely by chance stumbled across it again this evening.

I don't have an answer for you I'm afraid, but as far as I can remember, the iic as I used to allow access
To any device on the iic bus in my a5000 (which currently is sadly dead) and I could get at things like the cmos ram chip, so my guess would be, yes, as long as you know the device id and it appears on the bus.