Sensirion / embedded-uart-sps

Embedded UART Driver for Sensirion Particulate Matter Sensors - Download the Zip Package from the Release Page
https://github.com/Sensirion/embedded-uart-sps/releases
BSD 3-Clause "New" or "Revised" License
44 stars 24 forks source link

sensor probing failed #84

Closed Anzal3c3 closed 1 year ago

Anzal3c3 commented 1 year ago

HI i am using stm32l4XX microcontroller to read the data from the sensor. It outputs sensor probing failed what could it possibly mean. Is it because of anything wrong with the data being sent or being received?

{7E}{00}{7D}{31}{43}{00}{AB}{7E}{7E}{00}{7D}{31}{43}{00}{AB}{7E} this one is the mosi out that i get from the sensor when is send wakeup instruction

Anzal3c3 commented 1 year ago

{98}{FF}{09}{20}{36}{02}{00}{00}{7E}{00}{00}{02} this is the response i am getting from the sensor when its connected to the mcu when i send start measurement command . when i send the command the fan is turning on but the response is different than in the data sheet

Anzal3c3 commented 1 year ago

/*

include "../SPS30_DRIVER/sensirion_arch_config.h"

include "../SPS30_DRIVER/sensirion_uart.h"

include "stm32l4xx_hal.h"

include "stdio.h"

int i=0; /*

/**

uint8_t datas[100];

int16_t sensirion_uart_select_port(uint8_t port) { return 0; }

/**

/**

/**

/**

}

/**

psachs commented 1 year ago

Hi @Anzal3c3

For STM we currently don't have a UART sample implementation or a working setup. I therefore try to explain how the protocol works so you better understand the results you get.

The UART interface implements a Sensirion protocol (SHDLC) on top of uart which introduces frames for a more reliable communication.

Please note that UART will not necessary read all the data at once. This depends on the implementation of the underlying library.

A very simplified explanation of the protocol: Each frame has at most 255 bytes of data and is encapsulated in the start/stop byte (0x7E)

to avoid conflicts, there are the following byte stuffings

0x7E => 0x7D, 0x5E
0x7D => 0x7D, 0x5D
0x11 => 0x7D, 0x31
0x13 => 0x7D, 0x33

A message (MOSI) has the following parts

Address (1 byte) | Command (1 byte) | Lengh (1 byte) | Data (Lenght bytes) | Checksum (1 byte)

The response (MISO)

Address (1 byte) | Command (1 byte) | State (1byte) | Lengh (1 byte) | Data (Lenght bytes) | Checksum (1 byte)

You should also be able to find this information in the SPS30 datasheet.

If the answer you receive from the module is not starting with 0x7E and stopping with 0x7E you most probably have not read all the data.

Your examples first example with the wake-up

Note Wake-up command is 0x11 (which is encoded to 0x7D 0x31

The sensor answers with (It looks like you duplicated the same message, or read it twice): {7E}{00}{7D}{31}{43}{00}{AB}{7E}

which is

frame start =>  0x7E
Address => 0x00
Wake_up => 0x7D 0x31 => 0x11
State => 0x43
Length => 0x00
Checksum => 0xAB
frame end => 0x7E

so for the wake-up command the response makes sense. Btw. the state 0x43 means "Command not allowed in current state" because the sensor is already awake. This error you can ignore.

Your examples first example start measurement

Since the fan turns on, it looks like the transmission of the start command was successful. The answer you get however doesn't look complete.

{98}{FF}{09}{20}{36}{02}{00}{00}{7E}{00}{00}{02}

The MOSI data sent to the sensor should look as follows

frame start => 0x7E
Address => 0x00
Start measurement => 0x00
length => 0x2  (2 sub-command values are sent: 0x01 and 0x03)
data0 => 0x1
data1 => 0x3
Checksum => 0xF9
frame end => 0x7E

The expected answer is an empty frame, that looks as follows

frame start =>  0x7E
Address => 0x00
Start measurement => 0x00
State => 0x00
Length => 0x00
Checksum => 0xFF
frame end => 0x7E

The data you read, looks like it contains the MOSI frame at the end. So something is wrong there. I suspect, the problem is that after each send and read you need to flush the UART buffer to make sure you get the recent data. Also make sure that you read all the data from the buffer within a certain timeout if you still receive incomplete messages.

Let me know if my answer was helpful

Anzal3c3 commented 1 year ago

@psachs hey thanks for reaching out. it worked for me