Using the SPI-bus

eliabieri
Posts: 3
Joined: Thu Nov 26, 2015 8:45 pm

Using the SPI-bus

Postby eliabieri » Fri Mar 17, 2017 8:35 am

hello everyone,
I wanted to use the max6675 chip to read out a k-type temperature sensor.
You find the datasheet here https://cdn-shop.adafruit.com/datasheets/MAX6675.pdf
If you tie CS low and apply a CLK signal at the clk pin, the max6675 spits out one 16bit package, which looks like that:
https://www.dropbox.com/s/yytkafnku97o5 ... 2.png?dl=0
I use the following code t read out the package, but it doesn't work till now and the data variable always stays empty.
I studied the ESP-IDF SPI documentation but couldn't find out, if I use the apis properly.
Maybe some of you guys and girls have an idea on how to fix this.

Code: Select all

//define pins for MAX6675's SPI bus
#define PIN_NUM_MISO 25
#define PIN_NUM_CLK  19
#define PIN_NUM_CS   22

void read_temp_task(void *pvParameter) {
        max6675_event_group = xEventGroupCreate();
        spi_device_handle_t spi;
        spi_bus_config_t buscfg={
                .miso_io_num=PIN_NUM_MISO,
                .mosi_io_num=-1,
                .sclk_io_num=PIN_NUM_CLK,
                .quadwp_io_num=-1,
                .quadhd_io_num=-1
        };
        spi_device_interface_config_t devcfg={
                .clock_speed_hz=10000000, //Clock out at 10 MHz
                .mode=1,                  //Falling edge
                .spics_io_num=PIN_NUM_CS, //CS pin
                .queue_size=1,            //We want to be able to queue 7 transactions at a time

        };
        //Initialize the SPI bus
        ESP_ERROR_CHECK(spi_bus_initialize(HSPI_HOST, &buscfg, 1)); //
        //Attach the MAX6675 to the SPI bus
        ESP_ERROR_CHECK(spi_bus_add_device(HSPI_HOST, &devcfg, &spi));
        uint16_t data = NULL; //16 bit
        spi_transaction_t trans_desc={
                .address=0,
                .command=0,
                .flags=0,
                .length=16,
                .rxlength=16,
                .tx_buffer=0,
                .rx_buffer=data,

        };
        while (1) {
                ESP_ERROR_CHECK(spi_device_transmit(spi, &trans_desc));
                if(data&0x4) //check if thermocouple is not attached
                {
                        ESP_LOGI(TAG, "Thermocouple not attached \n");
                        xEventGroupSetBits(max6675_event_group,THERMOCOUPLE_NOT_ATTACHED_BIT);
                }
                uint16_t temp;  //extract temp from payload by applying a bit mask
                temp = data & 0b0111111111111000;
                max6675_temp = temp;
                xEventGroupSetBits(max6675_event_group,GOT_TEMP);
                printf("temp: %d\n",max6675_temp );
                printf("raw data: %d\n",data );



                vTaskDelay(1000/portTICK_RATE_MS);
        }



}
Last edited by eliabieri on Tue Apr 25, 2017 2:25 pm, edited 1 time in total.

ESP_Sprite
Posts: 9759
Joined: Thu Nov 26, 2015 4:08 am

Re: Using the SPI-bus

Postby ESP_Sprite » Mon Mar 20, 2017 4:59 am

You should stash a *pointer* to data into the transaction struct. At the moment you're putting the *value* of data into it.

eliabieri
Posts: 3
Joined: Thu Nov 26, 2015 8:45 pm

Re: Using the SPI-bus

Postby eliabieri » Sun Apr 02, 2017 12:48 pm

Okey this is the modified version. Sadly it still doesn't work.

Code: Select all

void read_temp_task(void *pvParameter) {
        max6675_event_group = xEventGroupCreate(); //event group for max6675

        spi_bus_config_t bus_config; //conf struct for bus
        bus_config.sclk_io_num = PIN_NUM_CLK; // CLK
        bus_config.mosi_io_num = -1; // MOSI
        bus_config.miso_io_num = PIN_NUM_MISO; // MISO
        bus_config.quadwp_io_num = -1; // Not used
        bus_config.quadhd_io_num = -1; // Not used
        ESP_LOGI(TAG, "... Initializing bus.");
        ESP_ERROR_CHECK(spi_bus_initialize(HSPI_HOST, &bus_config, 1));

        spi_device_handle_t spi = NULL;  //create slave instance
        spi_device_interface_config_t dev_config; //conf struct for device
        dev_config.address_bits = 0;
        dev_config.command_bits = 0;
        dev_config.dummy_bits = 0;
        dev_config.mode = 1;  //KEEP AN EYE ON, MAY HAS TO BE CHANGED
        dev_config.duty_cycle_pos = 0;
        dev_config.cs_ena_posttrans = 0;
        dev_config.cs_ena_pretrans = 0;
        dev_config.clock_speed_hz = 1000000; //1 MHZ
        dev_config.spics_io_num = PIN_NUM_CS;
        dev_config.flags = 0;
        dev_config.queue_size = 1;
        dev_config.pre_cb = NULL;
        dev_config.post_cb = NULL;
        ESP_LOGI(TAG, "... Adding device bus.");
        ESP_ERROR_CHECK(spi_bus_add_device(HSPI_HOST, &dev_config, &spi));

        char data[2];
        data[0] = 0;
        data[1] = 0;
        spi_transaction_t trans_desc;
        trans_desc.address   = 0;
        trans_desc.command   = 0;
        trans_desc.flags     = 0;
        trans_desc.length    = 0;
        trans_desc.rxlength  = 2*8;
        trans_desc.tx_buffer = NULL;
        trans_desc.rx_buffer = &data;


        while (1) {
                ESP_ERROR_CHECK(spi_device_transmit(spi, &trans_desc));
                uint16_t _data = atoi(data);
                if(_data&0x4) //check if thermocouple is not attached
                {
                        ESP_LOGI(TAG, "Thermocouple not attached \n");
                        xEventGroupSetBits(max6675_event_group,THERMOCOUPLE_NOT_ATTACHED_BIT);
                }
                uint16_t temp;  //extract temp from payload by applying a bit mask
                temp = _data & 0b0111111111111000;
                max6675_temp = temp;
                xEventGroupSetBits(max6675_event_group,GOT_TEMP);
                printf("temp: %d\n",max6675_temp );
                printf("raw data: %d\n",_data );



                vTaskDelay(1000/portTICK_RATE_MS);
        }



}

ESP_Sprite
Posts: 9759
Joined: Thu Nov 26, 2015 4:08 am

Re: Using the SPI-bus

Postby ESP_Sprite » Sun Apr 02, 2017 2:14 pm

You've managed to go from too few to too many dereferences... you now have data, a pointer to two bytes, and store its address in rx_buffer. Drop the & before data and you should be good.

Furthermore, you use 'atoi' to go back to an int16_t. Unless the sensor spits out the temperature in ASCII, which I highly doubt, this is not what you want. Not sure about endianness, but something like uint16_t _data = (data[0]<<8)+data[1]; will probably work better.

Ritesh
Posts: 1383
Joined: Tue Sep 06, 2016 9:37 am
Location: India
Contact:

Re: Using the SPI-bus

Postby Ritesh » Mon Jul 22, 2019 2:23 pm

eliabieri wrote:
Sun Apr 02, 2017 12:48 pm
Okey this is the modified version. Sadly it still doesn't work.

Code: Select all

void read_temp_task(void *pvParameter) {
        max6675_event_group = xEventGroupCreate(); //event group for max6675

        spi_bus_config_t bus_config; //conf struct for bus
        bus_config.sclk_io_num = PIN_NUM_CLK; // CLK
        bus_config.mosi_io_num = -1; // MOSI
        bus_config.miso_io_num = PIN_NUM_MISO; // MISO
        bus_config.quadwp_io_num = -1; // Not used
        bus_config.quadhd_io_num = -1; // Not used
        ESP_LOGI(TAG, "... Initializing bus.");
        ESP_ERROR_CHECK(spi_bus_initialize(HSPI_HOST, &bus_config, 1));

        spi_device_handle_t spi = NULL;  //create slave instance
        spi_device_interface_config_t dev_config; //conf struct for device
        dev_config.address_bits = 0;
        dev_config.command_bits = 0;
        dev_config.dummy_bits = 0;
        dev_config.mode = 1;  //KEEP AN EYE ON, MAY HAS TO BE CHANGED
        dev_config.duty_cycle_pos = 0;
        dev_config.cs_ena_posttrans = 0;
        dev_config.cs_ena_pretrans = 0;
        dev_config.clock_speed_hz = 1000000; //1 MHZ
        dev_config.spics_io_num = PIN_NUM_CS;
        dev_config.flags = 0;
        dev_config.queue_size = 1;
        dev_config.pre_cb = NULL;
        dev_config.post_cb = NULL;
        ESP_LOGI(TAG, "... Adding device bus.");
        ESP_ERROR_CHECK(spi_bus_add_device(HSPI_HOST, &dev_config, &spi));

        char data[2];
        data[0] = 0;
        data[1] = 0;
        spi_transaction_t trans_desc;
        trans_desc.address   = 0;
        trans_desc.command   = 0;
        trans_desc.flags     = 0;
        trans_desc.length    = 0;
        trans_desc.rxlength  = 2*8;
        trans_desc.tx_buffer = NULL;
        trans_desc.rx_buffer = &data;


        while (1) {
                ESP_ERROR_CHECK(spi_device_transmit(spi, &trans_desc));
                uint16_t _data = atoi(data);
                if(_data&0x4) //check if thermocouple is not attached
                {
                        ESP_LOGI(TAG, "Thermocouple not attached \n");
                        xEventGroupSetBits(max6675_event_group,THERMOCOUPLE_NOT_ATTACHED_BIT);
                }
                uint16_t temp;  //extract temp from payload by applying a bit mask
                temp = _data & 0b0111111111111000;
                max6675_temp = temp;
                xEventGroupSetBits(max6675_event_group,GOT_TEMP);
                printf("temp: %d\n",max6675_temp );
                printf("raw data: %d\n",_data );



                vTaskDelay(1000/portTICK_RATE_MS);
        }



}
Hello,

your code is working now? would you please share complete code as sample?
Regards,
Ritesh Prajapati

Who is online

Users browsing this forum: Bing [Bot] and 93 guests