I would appreciate it if someone could help me.
Both slave and master code are come from example.
slave send:
I (552736) [no-router, 235]: send:a
I (553237) [no-router, 235]: send:a
I (553736) [no-router, 235]: send:a
I (554240) [no-router, 235]: send:a
I (554740) [no-router, 235]: send:a
I (555242) [no-router, 235]: send:a
I (555738) [no-router, 235]: send:a
I (556238) [no-router, 235]: send:a
I (556742) [no-router, 235]: send:a
I (557238) [no-router, 235]: send:a
I (557743) [no-router, 235]: send:a
I (558240) [no-router, 235]: send:a
master receive
I (523864) [no-router, 183]: Recieved data 0
I (524365) [no-router, 183]: Recieved data 0
I (524866) [no-router, 183]: Recieved data @
I (525366) [no-router, 183]: Recieved data @
I (525866) [no-router, 183]: Recieved data @
I (526366) [no-router, 183]: Recieved data 0
I (526866) [no-router, 183]: Recieved data @
I (527366) [no-router, 183]: Recieved data @
I (527866) [no-router, 183]: Recieved data 0
I (528366) [no-router, 183]: Recieved data @
I (528866) [no-router, 183]: Recieved data @
I (529366) [no-router, 183]: Recieved data 0
I (529867) [no-router, 183]: Recieved data 0
I (530368) [no-router, 183]: Recieved data @
slave code:
Code: Select all
static mdf_err_t spi_initialize()
{
esp_err_t ret;
//Configuration for the SPI bus
spi_bus_config_t buscfg={
.mosi_io_num=GPIO_MOSI,
.miso_io_num=GPIO_MISO,
.sclk_io_num=GPIO_SCLK,
.quadwp_io_num = -1,
.quadhd_io_num = -1,
};
//Configuration for the SPI slave interface
spi_slave_interface_config_t slvcfg={
.mode=0,
.spics_io_num=GPIO_CS,
.queue_size=3,
.flags=0,
.post_setup_cb=my_post_setup_cb,
.post_trans_cb=my_post_trans_cb
};
//Configuration for the SPI device on the other side of the bus
spi_device_interface_config_t devcfg={
.command_bits=0,
.address_bits=0,
.dummy_bits=0,
.clock_speed_hz=5000000,
.duty_cycle_pos=128, //50% duty cycle
.mode=0,
.spics_io_num=GPIO_CS,
.cs_ena_posttrans=3, //Keep the CS low 3 cycles after transaction, to stop slave from missing the last bit when CS has less propagation delay than CLK
.queue_size=3
};
//Configuration for the handshake line
gpio_config_t io_conf={
.intr_type=GPIO_INTR_DISABLE,
.mode=GPIO_MODE_OUTPUT,
.pin_bit_mask=(1<<GPIO_HANDSHAKE)
};
//Configure handshake line as output
gpio_config(&io_conf);
//Enable pull-ups on SPI lines so we don't detect rogue pulses when no master is connected.
gpio_set_pull_mode(GPIO_MOSI, GPIO_PULLUP_ONLY);
gpio_set_pull_mode(GPIO_SCLK, GPIO_PULLUP_ONLY);
gpio_set_pull_mode(GPIO_CS, GPIO_PULLUP_ONLY);
//Initialize SPI slave interface
ret=spi_slave_initialize(RCV_HOST, &buscfg, &slvcfg, DMA_CHAN);
assert(ret==ESP_OK);
return MDF_OK;
}
static void spi_handle_task(void *arg)
{
int recv_length = 0;
mdf_err_t ret = MDF_OK;
cJSON *json_root = NULL;
cJSON *json_addr = NULL;
cJSON *json_group = NULL;
cJSON *json_data = NULL;
cJSON *json_dest_addr = NULL;
// Configure a temporary buffer for the incoming data
uint8_t *data = (uint8_t *) MDF_MALLOC(BUF_SIZE);
size_t size = MWIFI_PAYLOAD_LEN;
char *jsonstring = NULL;
uint8_t dest_addr[MWIFI_ADDR_LEN] = {0};
mwifi_data_type_t data_type = {0};
uint8_t sta_mac[MWIFI_ADDR_LEN] = {0};
MDF_LOGI("Uart handle task is running");
esp_wifi_get_mac(ESP_IF_WIFI_STA, sta_mac);
/* spi initialization */
MDF_ERROR_ASSERT(spi_initialize());
int n = 0;
WORD_ALIGNED_ATTR char sendbuf[129]="";
WORD_ALIGNED_ATTR char recvbuf[129]="";
memset(sendbuf, 0, 33);
spi_slave_transaction_t t;
memset(&t, 0, sizeof(t));
sprintf(sendbuf, "a");
memset(recvbuf, '\0', 129);
while (1) {
t.length=128*8;
t.tx_buffer=sendbuf;
t.rx_buffer=recvbuf;
//xSemaphoreTake(rdySem, portMAX_DELAY);
MDF_LOGI("send:%s",sendbuf);
ret=spi_slave_transmit(RCV_HOST, &t, portMAX_DELAY);
}
}
Code: Select all
spi_bus_handle_t bus_handle = NULL;
spi_bus_device_handle_t device_handle = NULL;
static mdf_err_t spi_initialize()
{
spi_config_t bus_conf = {
.miso_io_num = PIN_NUM_MISO,
.mosi_io_num = PIN_NUM_MOSI,
.sclk_io_num = PIN_NUM_CLK,
}; // spi_bus configurations
spi_device_config_t device_conf = {
.cs_io_num = PIN_NUM_CS,
.mode = 0,
.clock_speed_hz = 20 * 1000 * 1000,
}; // spi_device configurations
gpio_pad_select_gpio(PIN_NUM_CS); // 选择一个GPIO
gpio_set_direction(PIN_NUM_CS, GPIO_MODE_OUTPUT);// 把这个GPIO作为输出
gpio_pad_select_gpio(GPIO_HANDSHAKE); // 选择一个GPIO
gpio_set_direction(GPIO_HANDSHAKE, GPIO_MODE_OUTPUT);// 把这个GPIO作为输出
bus_handle = spi_bus_create(SPI2_HOST, &bus_conf); // create spi bus
device_handle = spi_bus_device_create(bus_handle, &device_conf); // create spi device
return MDF_OK;
}
static void spi_handle_task(void *arg)
{
int recv_length = 0;
mdf_err_t ret = MDF_OK;
cJSON *json_root = NULL;
cJSON *json_addr = NULL;
cJSON *json_group = NULL;
cJSON *json_data = NULL;
cJSON *json_dest_addr = NULL;
// Configure a temporary buffer for the incoming data
uint8_t *data = (uint8_t *) MDF_MALLOC(BUF_SIZE);
size_t size = MWIFI_PAYLOAD_LEN;
char *jsonstring = NULL;
uint8_t dest_addr[MWIFI_ADDR_LEN] = {0};
mwifi_data_type_t data_type = {0};
uint8_t sta_mac[MWIFI_ADDR_LEN] = {0};
MDF_LOGI("Uart handle task is running");
esp_wifi_get_mac(ESP_IF_WIFI_STA, sta_mac);
/* spi initialization */
MDF_ERROR_ASSERT(spi_initialize());
while (1) {
memset(data, 0, BUF_SIZE);
char* stl = "{\"dest_addr\":\"78:e3:6d:cd:17:6c\",\"data\":\"contentbb\"}";
gpio_set_level(PIN_NUM_CS, 0);
int ret = spi_bus_transfer_bytes(device_handle, (uint8_t *)stl, data, BUF_SIZE); // BUF_SIZE only read 1 byte with spi device
gpio_set_level(PIN_NUM_CS, 1);
MDF_ERROR_CONTINUE(ret != ESP_OK, "SPI transfer bytes fail, data: %s", data);
MDF_LOGI("Recieved data %s", data);
vTaskDelay(500);
}
}