I use the SPI master examples to modify my code,and Compiled success.But when I write a byte to the RC522'register,Then,I read it,I found that the value I wrote is inconsistent with the value I read ,I think my spi communicate must be many problem,here are my code ,please help me to make it work.
SPI Connectors
Code: Select all
#define PIN_NUM_MISO GPIO_NUM_25
#define PIN_NUM_MOSI GPIO_NUM_23
#define PIN_NUM_CLK GPIO_NUM_19
#define PIN_NUM_CS GPIO_NUM_22
#define PIN_NUM_RST GPIO_NUM_18
Code: Select all
GPIO_input_init(PIN_NUM_MISO);
GPIO_output_init(PIN_NUM_MOSI);
GPIO_output_init(PIN_NUM_CLK);
GPIO_output_init(PIN_NUM_CS);
GPIO_output_init(PIN_NUM_RST);
gpio_set_direction(PIN_NUM_MISO, GPIO_MODE_INPUT);
gpio_set_pull_mode(PIN_NUM_MISO, GPIO_PULLUP_ONLY);
Code: Select all
spi_device_handle_t spi;
spi_bus_config_t buscfg={
.miso_io_num=PIN_NUM_MISO,
.mosi_io_num=PIN_NUM_MOSI,
.sclk_io_num=PIN_NUM_CLK,
.quadwp_io_num=-1,
.quadhd_io_num=-1,
// .max_transfer_sz =2048,
};
spi_device_interface_config_t devcfg={
.command_bits =0,
.address_bits =0,
.dummy_bits =0,
.mode =0, //SPI mode 0
.duty_cycle_pos =0,
.cs_ena_pretrans =0,
.cs_ena_posttrans =0,
.clock_speed_hz =10*1000*1000, //Clock out at 10 MHz
.spics_io_num =PIN_NUM_CS, //CS pin
.flags =0,
.queue_size =1, //We want to be able to queue 7 transactions at a time
.pre_cb =NULL, //Specify pre-transfer callback to handle D/C line
.post_cb =NULL,
};
//Initialize the SPI bus
ret=spi_bus_initialize(HSPI_HOST, &buscfg, 1);
ESP_ERROR_CHECK(ret);
ret=spi_bus_add_device(HSPI_HOST, &devcfg, &spi);
ESP_ERROR_CHECK(ret);
RC522 CMD
This code for write same one cmd to RC522
Code: Select all
void RC522_cmd(spi_device_handle_t spi, uint8_t cmd)
{
esp_err_t ret;
spi_transaction_t t;
memset(&t, 0, sizeof(t)); //Zero out the transaction
t.addr =0;
t.cmd =0;
t.flags =0;
t.length =8; //Command is 8 bits
t.rxlength =8;
t.tx_buffer =&cmd; //The data is the cmd itself
// t.user=(void*)0; //D/C needs to be set to 0
ret=spi_device_transmit(spi, &t); //Transmit!
assert(ret==ESP_OK); //Should have had no issues.
}
Code: Select all
void RC522_write_byte(spi_device_handle_t spi,uint8_t value)
{
esp_err_t ret;
spi_transaction_t t;
memset(&t, 0, sizeof(t)); //Zero out the transaction
t.addr =0;
t.cmd =0;
t.length =8; //Command is 8 bits
t.rxlength =8;
t.tx_buffer =&value; //The data is the cmd itself
t.flags = SPI_TRANS_USE_TXDATA;
// t.user=(void*)0; //D/C needs to be set to 0
ret=spi_device_transmit(spi, &t); //Transmit!
ESP_ERROR_CHECK(ret); //Should have had no issues.
}
RC522_write_addr_value(spi,TxAutoReg,0x40);
write 0x40 to the addr of "TxAutoReg"
Code: Select all
void RC522_write_addr_value(spi_device_handle_t spi, uint8_t Address, uint8_t Value)
{
// uint8_t ucAddr=0;
Address = ((Address<<1)&0x7E);
RC522_cmd(spi,Address);
RC522_write_byte(spi,Value);
}
Code: Select all
uint8_t RC522_read_data(spi_device_handle_t spi, uint8_t Address)
{
// uint8_t rxdata[];
Address = ((Address<<1)&0x7E)|0x80;
spi_transaction_t t;
memset(&t, 0, sizeof(t));
RC522_cmd(spi,Address);
t.addr =0;
t.cmd =0;
t.length =8; //Command is 8 bits
t.rxlength =8;
t.flags = SPI_TRANS_USE_RXDATA;
// t.user = (void*)0;
esp_err_t ret = spi_device_transmit(spi, &t);
assert( ret == ESP_OK );
return *(uint8_t*) t.rx_data;
}
Code: Select all
RC522_write_addr_value(spi,TxAutoReg,0x40);
printf("read TxAutoReg 0 value = %08x \n",RC522_read_data(spi,TxAutoReg));
hear are some info for RC522
https://wenku.baidu.com/view/6c881266d ... afb4.html
I have no ways to solve this problems ,please help me ,thank you very much !!