Hello Nordic Devzone,
I have problems with twi/i2c init code on a Micro:bit, nrf51822. The accelerometer is a Freescale MMA8652FC. I use Segger Embedded Studio for ARM V3_34 64-bit.
The accelerometer works fine with codes from makecode.microbit.org or mbed.com I also checked the P19:SCL and P20:SDA pins on the edge connector with a logic analyser and they work fine.
The thumb assembly code is here. I cut the code out from a longer file.
The full code is here in a ZIP file. drive.google.com/open
@----------- code starts
gpio_regs= 0x50000000
twi0_base= 0x40003000
RXDREADY= 0x108
TXDSENT= 0x11c
PSELSCL= 0x508
PSELSDA= 0x50c
FREQENCY= 0x524
K100=0x01980000
K250=0x04000000
K400=0x06680000
pin_i2c_scl= 0 @ pin 19 on edge connector
pin_i2c_sda= 30 @ pin 20 on edge connector
.macro out32 rreg,vval
ldr r1,=\rreg
ldr r0,=\vval
str r0,[r1]
.endm
.macro in32 rreg
ldr r1,=\rreg
ldr r0,[r1]
.endm
/* Set GPIO pins to
input, connect input buffer,
pull up on bit, standard '0' - disconnect '1'
sense disabled */
tmp1=0 | 0<<1 | 3<<2 | 6<<8 | 0<<16
out32 gpio_regs|0x700|pin_i2c_scl<<2,tmp1
out32 gpio_regs|0x700|pin_i2c_sda<<2,tmp1
out32 twi0_base|RXDREADY,0
out32 twi0_base|TXDSENT,0
out32 twi0_base|PSELSCL,1<<pin_i2c_scl
out32 twi0_base|PSELSDA,1<<pin_i2c_sda
out32 twi0_base|FREQENCY,K400
@----------code ends
The problem is that I can not select GPIO:30 to TWI0.PSELSDA.
out32 twi0_base|PSELSDA,1<<pin_i2c_sda
I always get "0" when I read back TWI0.PSELSDA.
in32 twi0_base|PSELSDA
I did read the NRF51 docs and the C code for twi_init is the same.
My other codes compile and run fine so settings and defines are okay.
Thank you in advance.
Edit: Something is not clear to me. What should be written to the PSELSDA register? The GPIO pin number or the GPIO bit mask?
out32 twi0_base|PSELSCL,pin_i2c_scl
or
out32 twi0_base|PSELSCL,1<<pin_i2c_scl
When I write the GPIO mask (1<<0 and 1<<30) then the SCL line works, SDA line doesn't. If I write the pin # (0, 30) then nothing works.