The SPI transaction should send 1 byte as an op code and receive one byte containing the status.
The first byte of MOSI is the command’s opcode
0x9C and the second byte of MISO shows the latched
status information.
iC-MCW Specs:
CPOL = 0, CPHA = 0
CPOL = 1, CPHA = 1
SPI Min Clock 10Mhz
This seems straight forward enough but I am not getting a response. The output repeats in a loop and is always 0. Am I missing something? I suspect the CS line may be the culprit, its held high by the MCW dev board when unplugged at 5V. It high 3.6 when connected to pin 5.
Code: Select all
Tx Byte 0: 0x9C
Tx Byte 1: 0x0
Rx Byte 0: 0x0
Rx Byte 1: 0x0
- # define PIN_NUM_MISO 19
- # define PIN_NUM_MOSI 23
- # define PIN_NUM_CLK 18
- # define PIN_NUM_CS 5
- void mcw_get_status(spi_device_handle_t spi){
- esp_err_t ret;
- spi_transaction_t t;
- uint8_t cmd_buff[2];
- cmd_buff[0] = 0x9C;
- cmd_buff[1] = 0x00;
- for (int i = 0; i < 2; i++){
- printf("Tx Byte %d: 0x%X\n", i, cmd_buff[i]);
- }
- memset(&t, 0, sizeof(t));
- t.length = 8 * 2;
- t.tx_buffer = &cmd_buff;
- t.flags = SPI_TRANS_USE_RXDATA;
- ret = spi_device_polling_transmit(spi, &t);
- assert(ret == ESP_OK);
- for (int i = 0; i < t.rxlength / 8; i++){
- printf("Rx Byte %d: 0x%X\n", i, t.rx_data[i]);
- }
- };
- void mcw_init(spi_device_handle_t spi){
- mcw_get_status(spi);
- };
- void app_main(void)
- {
- 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 = 2,
- };
- spi_device_interface_config_t devcfg = {
- .clock_speed_hz = 10 * 1000 * 1000,
- .mode = 0,
- .spics_io_num = PIN_NUM_CS,
- .queue_size = 10,
- };
- esp_err_t ret = spi_bus_initialize(SPI2_HOST, &buscfg, SPI_DMA_CH_AUTO);
- ESP_ERROR_CHECK(ret);
- ret = spi_bus_add_device(SPI2_HOST, &devcfg, &spi);
- ESP_ERROR_CHECK(ret);
- while(true) {
- mcw_init(spi);
- sleep(2);
- }
- }