esp32 spi communication
Posted: Sun Jun 02, 2019 6:27 pm
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.
--------------------------------------------
E D I T
I find solution in meanwhile. It just need to be:
and everything works nice!
--------------------------------------------
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 all
pin_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 all
halfduplex = false
and everything works nice!
--------------------------------------------