I2C driver by PRU

I want to manage the I2C1 module by the PRU, in order to interface some I/O expanders (MCP23017 by Microchip).
I use may own “cape” without the plug-n’ play eeprom (one of the next steps will be adding management for DCAN0 and DCAN1 so i’ll need these pins too…).
So, at present, there are just 2 MCP23017 connected to the P9.17 and P9.18.

I load the cape-universal into slots and then I use configure-pin command to set P9.17 and P9.18 as i2c

I’ve started from an example into the am335x_pru_package-master and wrote my own C PRU loader.
Very simple, it just:

loads the PRU code
init the data exhanged with the PRU
start the PRU
wait for ESC key press
signal to the PRU to stop
wait for the PRU stop
exit.

Also the assembly PRU code is simple:

init I2C1 module (by writing registers PSC, SCLL, SCLH, CON)
init 1st I/O expander as 16 inputs (even if at power on it’s already set as input)
init 2nd I/O expander as 16 outputs
cicle reading status of inputs from 1st expander and echoing to the outputs of 2nd expander
exit cycle and halt when receive stop flag from the loader

for send and receive I2C messages I use register SA, CNT, DATA and CON.

That’s very simple and linear… pity, it doesn’t work.

I didn’t see any activity at all in P9.17 and P9.18.

The PRU code is surely running, as I add a cycle counter and show it in the loader while it’s waiting for ESC keypress, and also the PRU code correctly stops at the loader command.

I was expecting that the PRU code stalls if I2C bus doesn’t work, as there are waiting cycles both for STOP condition or for CNT reaching zero (depending on the write or read message sending).

But it seems running, and running very fast also: the cycle counter is incremented to a very fast rate (over 550 kcycles/s) that’s not compatible with the correct executing of I2C sequences (I’ve setted the module for 400Kbps rate… so the PRU cycle it’s even faster than a single I2C bit time!).

I’m surely doing something wrong, but I cant fugure what.

Any idea?

Suggestions?

Inserisci qui il codice...
.origin 0
.entrypoint START

#include “iic_ioexp.hp”

//costanti per l’accesso al modulo I2C1
#define I2C1_BASE C2 //base registri I2C1 nella tabella costanti
#define I2C_SYSC 0x10 //offset del registro I2C_SYSC
#define I2C_STAT_RAW 0x24 //offset del registro I2C_STATUS_RAW
#define I2C_SYSS 0x90 //offset del registro I2C_SYSS
#define I2C_CNT 0x98 //offset del registro I2C_CNT
#define I2C_DATA 0x9C //offset del registro I2C_DATA
#define I2C_CON 0xA4 //offset del registro I2C_CON
#define I2C_SA 0xAC //offset del registro I2C_SA
#define I2C_PSC 0xB0 //offset del registro I2C_PSC
#define I2C_SCLL 0xB4 //offset del registro I2C_SCLL
#define I2C_SCLH 0xB8 //offset del registro I2C_SCLH

#define I2C_CMD_ENABLE 0x8400 //modulo I2C abilitato come master
#define I2C_CMD_TX 0x0200 //modulo I2C in trasmissione
#define I2C_CMD_RX 0x0000 //modulo I2C in ricezione
#define I2C_CMD_START 0x0001 //modulo I2C richiesta generazione sequenza START
#define I2C_CMD_STOP 0x0002 //modulo I2C richiesta generazione sequenza STOP

//costanti per l’accesso all’I/O expander MCP23017
#define IO_EXP0 0x20 //7bit I2C address dell’I/O expander 0 (ingressi)
#define IO_EXP1 0x21 //7bit I2C address dell’I/O expander 1 (uscite)

#define IO_EXP_IODIRA 0x00 //indirizzo registro IODIRA dell’I/O expander
#define IO_EXP_GPIOA 0x12 //indirizzo registro GPIOA dell’I/O expander

//======================================================================

//macro che attende fine sequenza verificando generazione seqeunza di STOP
.macro I2C_WAIT_BY_STOP
_CHECK:
LBCO r1.w0, I2C1_BASE, I2C_CON, 2
QBBS _CHECK, r1.t1
.endm

//macro che attende fine sequenza verificando generazione seqeunza di STOP
.macro I2C_WAIT_BY_COUNT
_CHECK:
LBCO r1.w0, I2C1_BASE, I2C_CNT, 2
QBNE _CHECK, r1.w0, 0
.endm

//======================================================================

START:
// clear that bit
LBCO r0, C4, 4, 4
CLR r0, r0, 4
SBCO r0, C4, 4, 4

//------------------------------------------------------------------
//configurazione modulo I2C1

//reset del modulo I2C1
MOV r1.w0, 0x0002
SBCO r1.w0, I2C1_BASE, I2C_SYSC, 2

MOV r1.w0, 0x0000
SBCO r1.w0, I2C1_BASE, I2C_SYSC, 2

// //attesa fine reset modulo
//_WAIT_RDONE:
// LBCO r1, I2C1_BASE, I2C_SYSS, 4
// QBBC _WAIT_RDONE, r1.t0

//configura prescaler e durata SCL H/L per avere 400kHz
MOV r1.b0, 4 //prescaler=4+1 → ICLK=SCLK/prescaler=100/5=20MHz (nel reference manual raccomandano circa 24Mhz)
SBCO r1.b0, I2C1_BASE, I2C_PSC, 1

MOV r1.b0, 18 //durata SCL L=18+7 → tLOW=1/ICLK*(SCLL+7)=1/20E6*(18+7)=1.25us
SBCO r1.b0, I2C1_BASE, I2C_SCLL, 1

MOV r1.b0, 20 //durata SCL H=20+5 → tHIGH=1/ICLK*(SCLH+5)=1/20E6*(20+5)=1.25us
SBCO r1.b0, I2C1_BASE, I2C_SCLH, 1

//abilita modulo
MOV r1.w0, I2C_CMD_ENABLE
SBCO r1.w0, I2C1_BASE, I2C_CON, 2

//------------------------------------------------------------------
//inizializzazione IOEXP 0

//indirizzo slave
MOV r1.w0, IO_EXP0
SBCO r1.w0, I2C1_BASE, I2C_SA, 2

//n. byte da spedire
MOV r1.w0, 3
SBCO r1.w0, I2C1_BASE, I2C_CNT, 2

//riempie FIFO
MOV r1, IO_EXP_IODIRA | 0x00FFFF00 //tutti i pin come ingressi (in realta’ e’ gia’ cosi’ dal POR)
SBCO r1.b0, I2C1_BASE, I2C_DATA, 1
SBCO r1.b1, I2C1_BASE, I2C_DATA, 1
SBCO r1.b2, I2C1_BASE, I2C_DATA, 1

//attesa bus free
_WAIT_BB:
LBCO r1, I2C1_BASE, I2C_STAT_RAW, 4
QBBS _WAIT_BB, r1.t12

//comando scrittura
MOV r1.w0, I2C_CMD_ENABLE | I2C_CMD_TX | I2C_CMD_START | I2C_CMD_STOP
SBCO r1.w0, I2C1_BASE, I2C_CON, 2

I2C_WAIT_BY_STOP

//------------------------------------------------------------------
//inizializzazione IOEXP 1

//indirizzo slave
MOV r1.w0, IO_EXP1
SBCO r1.w0, I2C1_BASE, I2C_SA, 2

//n. byte da spedire gia’ impostato

//riempie FIFO
MOV r1, IO_EXP_IODIRA | 0x00000000 //tutti pin come uscita
SBCO r1.b0, I2C1_BASE, I2C_DATA, 1
SBCO r1.b1, I2C1_BASE, I2C_DATA, 1
SBCO r1.b2, I2C1_BASE, I2C_DATA, 1

//comando scrittura
MOV r1.w0, I2C_CMD_ENABLE | I2C_CMD_TX | I2C_CMD_START | I2C_CMD_STOP
SBCO r1.w0, I2C1_BASE, I2C_CON, 2

I2C_WAIT_BY_STOP

//------------------------------------------------------------------
//ciclo rinfresco I/O

_LOOP:

//------------------------------------------------------------------
//legge ingressi

//indirizzo slave
MOV r1.w0, IO_EXP0
SBCO r1.w0, I2C1_BASE, I2C_SA, 2

//n. byte da spedire
MOV r1.w0, 1
SBCO r1.w0, I2C1_BASE, I2C_CNT, 2

//riempie FIFO
MOV r1, IO_EXP_GPIOA
SBCO r1.b0, I2C1_BASE, I2C_DATA, 1

//comando scrittura
MOV r1.w0, I2C_CMD_ENABLE | I2C_CMD_TX | I2C_CMD_START
SBCO r1.w0, I2C1_BASE, I2C_CON, 2

I2C_WAIT_BY_COUNT

//n. byte da ricevere
MOV r1.w0, 2
SBCO r1.w0, I2C1_BASE, I2C_CNT, 2

//comando lettura
MOV r1.w0, I2C_CMD_ENABLE | I2C_CMD_RX | I2C_CMD_START | I2C_CMD_STOP
SBCO r1.w0, I2C1_BASE, I2C_CON, 2

I2C_WAIT_BY_STOP

//legge i byte ricevuti
LBCO r2.b1, I2C1_BASE, I2C_DATA, 1
LBCO r2.b2, I2C1_BASE, I2C_DATA, 1

//------------------------------------------------------------------
//scrive uscite

//indirizzo slave
MOV r1.w0, IO_EXP1
SBCO r1.w0, I2C1_BASE, I2C_SA, 2

//n. byte da spedire
MOV r1.w0, 3
SBCO r1.w0, I2C1_BASE, I2C_CNT, 2

//riempie FIFO
MOV r2.b0, IO_EXP_GPIOA
SBCO r2.b0, I2C1_BASE, I2C_DATA, 1
SBCO r2.b0, I2C1_BASE, I2C_DATA, 1
SBCO r2.b0, I2C1_BASE, I2C_DATA, 1

//comando scrittura
MOV r1.w0, I2C_CMD_ENABLE | I2C_CMD_TX | I2C_CMD_START | I2C_CMD_STOP
SBCO r1.w0, I2C1_BASE, I2C_CON, 2

I2C_WAIT_BY_STOP

//------------------------------------------------------------------
LBCO r2, CONST_PRUDRAM, 0, 4
ADD r2, r2, 1
SBCO r2, CONST_PRUDRAM, 0, 4

//------------------------------------------------------------------
//esce dal ciclo e se il flag di uscita nella ram condivisa e’ azzerato
LBCO r2, CONST_PRUDRAM, 4, 1
QBNE _LOOP, r2.b0, 0

_EXIT:
//invia all’host la notifica di programma completato
MOV R31.b0, PRU0_ARM_INTERRUPT+16

HALT

I finally installed CCS6 and use USB100v2 JTAG interface (after soldering the header into BBB pads).

It looks like as I2C1 module never goes out of reset status: RDONE flag into I2C_SYSS register is always 0 even after writing I2C_EN=1 into I2C_CON register (and also, SRST=0 into register I2C_SYSC).
The very odd thing is that if I read register I2C_CON it results 0, as if the SBCO instruction didn’t write it effectively.

Is there some write-protect lock to deal with?

Just for completeness, I2C1 module seems correctly active and working:

root@beaglebone:~# i2cdetect -l
i2c-0 i2c OMAP I2C adapter I2C adapter
i2c-1 i2c OMAP I2C adapter I2C adapter
i2c-2 i2c OMAP I2C adapter I2C adapter

I2C1 module was mapped on i2c-2 (nothing new here) as we can see from the base address 4802a000:

root@beaglebone:~# ls /sys/class/i2c-dev/ -l
total 0
lrwxrwxrwx 1 root root 0 Jan 1 2000 i2c-0 → …/…/devices/ocp.3/44e0b000.i2c/i2c-0/i2c-dev/i2c-0
lrwxrwxrwx 1 root root 0 Jan 1 2000 i2c-1 → …/…/devices/ocp.3/4819c000.i2c/i2c-1/i2c-dev/i2c-1
lrwxrwxrwx 1 root root 0 Jul 19 09:57 i2c-2 → …/…/devices/ocp.3/4802a000.i2c/i2c-2/i2c-dev/i2c-2

And the devices are correctly attached to the bus (2x MCP23017):

root@beaglebone:~# i2cdetect -r 2
WARNING! This program can confuse your I2C bus, cause data loss and worse!
I will probe file /dev/i2c-2 using read byte commands.
I will probe address range 0x03-0x77.
Continue? [Y/n]
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: – -- – -- – -- – -- – -- – -- –
10: – -- – -- – -- – -- – -- – -- – -- – --
20: 20 21 – -- – -- – -- – -- – -- – -- – --
30: – -- – -- – -- – -- – -- – -- – -- – --
40: – -- – -- – -- – -- – -- – -- – -- – --
50: – -- – -- – -- – -- – -- – -- – -- – --
60: – -- – -- – -- – -- – -- – -- – -- – --
70: – -- – -- – -- – --

0x20 and 0x21 are the address of the 2 I/O expanders, as expected.
I also look at the SDA and SCL pins (with a scope) during the bus scan and both clock and data signals seems good (I notice 100kHz clock is used).

All is working well… so why I still can’t correctly access the I2C1 module by PRU?

FIXED ! :slight_smile:

It happens that the I2C1 module was not activated into the PRCM (Power, Reset and Clock Management): the module clocks was not supplied (both interface and functional) as it was hold in idle status.
So module registers was not really accessible (and of course the module function was also off).

I managed to activate the module writing MODULEMODE field into register CM_PER_I2C1_CLKCTRL register (and also I wait for IDLEST field to confirm that module is ready).

After that, all works as expected!
I can drive the I2C devices attached to the bus by the PRU, off-loading the main processor and (mainly) keeping tight timings.