-->
Page 1 of 1

esp32 spi communication

PostPosted: Sun Jun 02, 2019 6:27 pm
by abobija
Hi everyone, I'm trying to establish connection between ESP32 and RC522 rfid card reader over the SPI protocol but unfortunately I failed.

Here is my code, please tell me where I make mistakes?
I try to write value 0x25 into register 0x24, and then read that value from that register and "print" it.

Code: Select allpin_sda  = 15
pin_clk  = 19
pin_miso = 25
pin_mosi = 23

_master = spi.master(spi.HSPI, {
    sclk = pin_clk,
    mosi = pin_mosi,
    miso = pin_miso
}, 0)

_device = _master:device({
    cs = pin_sda,
    mode = 0,
    freq = 5000000,
    halfduplex = true
})

function write(address, value)
    local _addr = bit.band(bit.lshift(address,1), 0x7E)

    _device:transfer(string.char(_addr))
    _device:transfer(string.char(value))
end

function read(address)
    local _addr = bit.bor(bit.band(bit.lshift(address, 1), 0x7E), 0x80)

    _device:transfer(string.char(_addr))

    return _device:transfer(string.char(0xFF)):byte(1)
end

write(0x24, 0x25)

print( read(0x24) )


--------------------------------------------
E D I T

I find solution in meanwhile. It just need to be:
Code: Select allhalfduplex = false

and everything works nice!
--------------------------------------------