Need help with SPI

lesept
Posts: 38
Joined: Wed Jun 27, 2018 10:30 pm

Need help with SPI

Postby lesept » Fri Oct 05, 2018 11:57 am

Hi
I'm trying to run a simple test with the SPI bus, but the program gets stuck at the transmit instruction. I guess I haven't understood al the subtleties of the protocol.
Here is my code :

Code: Select all

/* 

*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "driver/spi_master.h"
#include "soc/gpio_struct.h"
#include "driver/gpio.h"

// GPIO definition
#define PIN_NUM_MISO 25
#define PIN_NUM_MOSI 23
#define PIN_NUM_CLK  19
#define PIN_NUM_CS   22
#define PIN_NUM_DC   21

#define NUMBYTES     20

//Place data into DRAM. Constant data gets placed into DROM by default, which is not accessible by DMA.
//DRAM_ATTR static uint8_t data[];
uint8_t data[NUMBYTES];


/* Send data. Uses spi_device_polling_transmit, which waits until the
 * transfer is complete.
 *
 * Since data transactions are usually small, they are handled in polling
 * mode for higher speed. The overhead of interrupt transactions is more than
 * just waiting for the transaction to complete.
 */
void spi_send(spi_device_handle_t spi, const uint8_t *data)
{
    esp_err_t ret;
    spi_transaction_t t;
    memset(&t, 0, sizeof(t));       //Zero out the transaction
    t.length=NUMBYTES*8;            //Transaction length in bits.
    t.tx_buffer=data;               //Data
    t.user=(void*)1;                //D/C needs to be set to 1
	printf ("Transmitting...\n");
//    ret=spi_device_polling_transmit(spi, &t);  //Transmit!
    ret=spi_device_transmit(spi, &t);  //Transmit!
    assert(ret==ESP_OK);            //Should have had no issues.
}

void app_main()
{
    esp_err_t ret;
    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=NUMBYTES
    };
    spi_device_interface_config_t devcfg={
        .command_bits=0,
        .address_bits=0,
        .dummy_bits=0,
        .mode=0,                       //SPI mode 0
        .duty_cycle_pos=128,
        .clock_speed_hz=8*1000*1000,   //Clock out frequency (Hz)
        .queue_size=1,
        .spics_io_num=PIN_NUM_CS      //CS pin
    };
	// Prepare GPIO
	gpio_set_direction(PIN_NUM_DC, GPIO_MODE_OUTPUT);
	gpio_set_direction(GPIO_NUM_9, GPIO_MODE_OUTPUT);
	gpio_set_direction(GPIO_NUM_22, GPIO_MODE_OUTPUT); // Builtin LED
	printf ("GPIO OK\n");
	// Prepare data
	srand(0);
	for (int i = 0; i<NUMBYTES/2; i++) data[i] = 0xAA; // rand()&0xff;
	for (int i = NUMBYTES/2; i<NUMBYTES; i++) data[i] = 0xCC;
 	printf ("Data OK. Initializing SPI\n");
   //Initialize the SPI bus
    ret=spi_bus_initialize(HSPI_HOST, &buscfg, 1); // 1 pour DMA
    ESP_ERROR_CHECK(ret);
 	printf ("-------------------------------------\n\n");
 	printf ("Adding device\n");
    ret=spi_bus_add_device(HSPI_HOST, &devcfg, &spi);
    ESP_ERROR_CHECK(ret);
 	printf ("-------------------------------------\n\n");
    // Set pin 9 to HIGH
	gpio_set_level(GPIO_NUM_9, 1);
	printf ("LED ON\n");
	gpio_set_level(GPIO_NUM_22, 1); // LED ON
	// Send data
	printf ("Sending\n");
    spi_send(spi,data);
	// Set pin 9 to LOW
	gpio_set_level(GPIO_NUM_9, 0);
	printf("Finished... CTRL + ] to end.");
	vTaskDelay(1000 / portTICK_PERIOD_MS);      // Wait 1 sec
	gpio_set_level(GPIO_NUM_22, 0); // LED OFF
}
And here is the answer from 'make monitor':

Code: Select all

MONITOR
--- idf_monitor on COM7 115200 ---
--- Quit: Ctrl+] | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H ---
ets Jun  8 2016 00:22:57

rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0018,len:4
load:0x3fff001c,len:5736
load:0x40078000,len:9020
load:0x40080000,len:6064
0x40080000: _iram_start at C:/msys32/home/fa125436/esp/esp-idf/components/freertos/xtensa_vectors.S:1685

entry 0x40080330
0x40080330: _KernelExceptionVector at ??:?

I (29) boot: ESP-IDF v3.1 2nd stage bootloader
I (29) boot: compile time 16:33:07
I (29) boot: Enabling RNG early entropy source...
I (33) boot: SPI Speed      : 40MHz
I (37) boot: SPI Mode       : DIO
I (41) boot: SPI Flash Size : 4MB
I (45) boot: Partition Table:
I (49) boot: ## Label            Usage          Type ST Offset   Length
I (56) boot:  0 nvs              WiFi data        01 02 00009000 00006000
I (63) boot:  1 phy_init         RF data          01 01 0000f000 00001000
I (71) boot:  2 factory          factory app      00 00 00010000 00100000
I (78) boot: End of partition table
I (83) esp_image: segment 0: paddr=0x00010020 vaddr=0x3f400020 size=0x09a44 ( 39492) map
I (105) esp_image: segment 1: paddr=0x00019a6c vaddr=0x3ffb0000 size=0x022c8 (  8904) load
I (109) esp_image: segment 2: paddr=0x0001bd3c vaddr=0x3ffb22c8 size=0x00000 (     0) load
I (112) esp_image: segment 3: paddr=0x0001bd44 vaddr=0x40080000 size=0x00400 (  1024) load
0x40080000: _iram_start at C:/msys32/home/fa125436/esp/esp-idf/components/freertos/xtensa_vectors.S:1685

I (122) esp_image: segment 4: paddr=0x0001c14c vaddr=0x40080400 size=0x03ec4 ( 16068) load
I (137) esp_image: segment 5: paddr=0x00020018 vaddr=0x400d0018 size=0x12db4 ( 77236) map
0x400d0018: _flash_cache_start at ??:?

I (166) esp_image: segment 6: paddr=0x00032dd4 vaddr=0x400842c4 size=0x05354 ( 21332) load
0x400842c4: spi_intr at C:/msys32/home/fa125436/esp/esp-idf/components/driver/spi_master.c:433

I (175) esp_image: segment 7: paddr=0x00038130 vaddr=0x400c0000 size=0x00000 (     0) load
I (175) esp_image: segment 8: paddr=0x00038138 vaddr=0x50000000 size=0x00000 (     0) load
I (190) boot: Loaded app from partition at offset 0x10000
I (190) boot: Disabling RNG early entropy source...
I (195) cpu_start: Pro cpu up.
I (199) cpu_start: Starting app cpu, entry point is 0x40080ea0
0x40080ea0: call_start_cpu1 at C:/msys32/home/fa125436/esp/esp-idf/components/esp32/cpu_start.c:225

I (0) cpu_start: App cpu up.
I (210) heap_init: Initializing. RAM available for dynamic allocation:
I (216) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM
I (222) heap_init: At 3FFB3350 len 0002CCB0 (179 KiB): DRAM
I (229) heap_init: At 3FFE0440 len 00003BC0 (14 KiB): D/IRAM
I (235) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
I (241) heap_init: At 40089618 len 000169E8 (90 KiB): IRAM
I (248) cpu_start: Pro cpu start user code
I (266) cpu_start: Starting scheduler on PRO CPU.
I (0) cpu_start: Starting scheduler on APP CPU.
GPIO OK
Data OK. Initializing SPI
-------------------------------------

Adding device
-------------------------------------

LED ON
Sending
Transmitting...

Can anyone help me or explain why it stays stuck at the transmit instruction? Thank you.

Who is online

Users browsing this forum: No registered users and 85 guests