IZU2020 / SPS30

Dependents:   IZU2021_SPS30 Hybrid_IZU2021_MISSION_v2 Hybrid_IZU2021_MISSION

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers sps30.h Source File

sps30.h

00001 /*******************************************************************************
00002  * Copyright (c) 2018-2019, Sensors and IoT Capability Centre (SIOT) at GovTech.
00003  *
00004  * Contributor(s):
00005  *    Yap Zi Qi     yap_zi_qi@tech.gov.sg
00006  *******************************************************************************/
00007 
00008 #ifndef SPS30_H
00009 #define SPS30_H
00010 
00011 #define SPS30_I2C_ADDR                  0xd2    //  left shifted by 1 bit from 0x69
00012 
00013 #define SPS30_CMMD_STRT_MEAS            0x0010
00014 #define SPS30_CMMD_STOP_MEAS            0x0104
00015 #define SPS30_CMMD_GET_READY_STAT       0x0202
00016 #define SPS30_CMMD_READ_MEAS            0x0300
00017 
00018 #define SPS30_CMMD_AUTO_CLEAN_INTV      0x8004
00019 #define SPS30_CMMD_START_FAN_CLEAN      0x5607
00020 
00021 #define SPS30_CMMD_SOFT_RESET           0xD304
00022 
00023 #define SPS30_CMMD_READ_SERIALNBR       0xD033
00024 #define SPS30_CMMD_READ_ARTICLECODE     0xD025
00025 
00026 #define SPS30_STRT_MEAS_WRITE_DATA      0x0300
00027     
00028 #define SPS30_POLYNOMIAL                0x31    // P(x) = x^8 + x^5 + x^4 + 1 = 100110p01
00029 #define SPS30_CRC_INIT                  0xff
00030 
00031 #define SPS30_SN_SIZE                   33      // size of the s/n ascii string + CRC values
00032 
00033 #define MASS_MAX            1000.00f
00034 #define MASS_MIN            0.00f
00035 #define NUM_MAX             3000.00f
00036 #define NUM_MIN             0.00f
00037 
00038 #define I2C_FREQUENCY_STD   100000              // SPS30 uses 100MHz for I2C communication
00039 
00040 /** Create SPS30 controller class
00041  *
00042  * @param sps30 class
00043  *
00044  */
00045 class Sps30 {
00046 
00047 public:
00048     enum SPSStatus {
00049         DISCONNECTED,
00050         CONNECTED,
00051         DATANOTREADY,
00052         DATAREADY,
00053         DATAERROR,
00054         DATAOOR,
00055         DATAOK,
00056     } SPSStatus;
00057     
00058     uint16_t sps_ready;            /**< 1 = ready, 0 = busy */
00059     uint32_t clean_interval_i;     /** 32 unsigned bit in seconds */
00060         
00061     float mass_1p0_f;       /**< float of Mass Conc of PM1.0 */
00062     float mass_2p5_f;       /**< float of Mass Conc of PM2.5 */
00063     float mass_4p0_f;       /**< float of Mass Conc of PM4.0 */
00064     float mass_10p0_f;      /**< float of Mass Conc of PM10 */
00065 
00066     float num_0p5_f;        /**< float of Number Conc of PM0.5 */
00067     float num_1p0_f;        /**< float of Number Conc of PM1.0 */
00068     float num_2p5_f;        /**< float of Number Conc of PM2.5 */
00069     float num_4p0_f;        /**< float of Number Conc of PM4.0 */
00070     float num_10p0_f;       /**< float of Number Conc of PM10 */
00071 
00072     float typ_pm_size_f;    /**< float of Typical Particle Size */
00073         
00074     uint8_t sn[33];         /**< ASCII Serial Number */
00075 
00076     
00077     
00078     /** Create a SPS30 object using the specified I2C object
00079      * @param sda - mbed I2C interface pin
00080      * @param scl - mbed I2C interface pin
00081      * @param I2C Frequency (in Hz)
00082      *
00083      * @return none
00084      */
00085      Sps30(PinName sda, PinName scl, int i2c_frequency);
00086      
00087     /** Destructor
00088      *
00089      * @param --none--
00090      *
00091      * @return none
00092      */
00093     ~Sps30();
00094     
00095     
00096     /** Initialise SPS30
00097      *
00098      * @param --none--
00099      *
00100      * @return enum SPSStatus
00101      */
00102     uint8_t InitSPS30();
00103     
00104     /** Poll SPS30 Data
00105      *
00106      * @param --none--
00107      *
00108      * @return enum SPSStatus
00109      */
00110     uint8_t PollSPS30();
00111     
00112  
00113 private:
00114 
00115     enum SPSError {
00116         SPSNOERROR,         //all ok
00117         SPSISREADY,         //ready status register
00118         SPSNOACKERROR,      //no I2C ACK error
00119         SPSCRCERROR,        //CRC error, any
00120     };
00121     
00122     char i2cbuff[60];
00123     
00124     uint16_t clean_interval_m;    /**< High order 16 bit word of Auto Clean Interval */
00125     uint16_t clean_interval_l;    /**< High order 16 bit word of Auto Clean Interval */
00126 
00127     uint16_t mass_1p0_m;          /**< High order 16 bit word of Mass Conc of PM1.0 */
00128     uint16_t mass_1p0_l;          /**< Low order 16 bit word of Mass Conc of PM1.0 */
00129     uint16_t mass_2p5_m;          /**< High order 16 bit word of Mass Conc of PM2.5 */
00130     uint16_t mass_2p5_l;          /**< Low order 16 bit word of Mass Conc of PM2.5 */
00131     uint16_t mass_4p0_m;          /**< High order 16 bit word of Mass Conc of PM4.0 */
00132     uint16_t mass_4p0_l;          /**< Low order 16 bit word of Mass Conc of PM4.0 */
00133     uint16_t mass_10p0_m;         /**< High order 16 bit word of Mass Conc of PM10 */
00134     uint16_t mass_10p0_l;         /**< Low order 16 bit word of Mass Conc of PM10 */
00135 
00136     uint16_t num_0p5_m;           /**< High order 16 bit word of Number Conc of PM0.5 */
00137     uint16_t num_0p5_l;           /**< Low order 16 bit word of Number Conc of PM0.5 */
00138     uint16_t num_1p0_m;           /**< High order 16 bit word of Number Conc of PM1.0 */
00139     uint16_t num_1p0_l;           /**< Low order 16 bit word of Number Conc of PM1.0 */
00140     uint16_t num_2p5_m;           /**< High order 16 bit word of Number Conc of PM2.5 */
00141     uint16_t num_2p5_l;           /**< Low order 16 bit word of Number Conc of PM2.5 */
00142     uint16_t num_4p0_m;           /**< High order 16 bit word of Number Conc of PM4.0 */
00143     uint16_t num_4p0_l;           /**< Low order 16 bit word of Number Conc of PM4.0 */
00144     uint16_t num_10p0_m;          /**< High order 16 bit word of Number Conc of PM10 */
00145     uint16_t num_10p0_l;          /**< Low order 16 bit word of Number Conc of PM10 */
00146 
00147     uint16_t typ_pm_size_m;       /**< High order 16 bit word of Typical Particle Size */
00148     uint16_t typ_pm_size_l;       /**< Low order 16 bit word of Typical Particle Size */
00149 
00150     uint32_t mass_1p0_i;          /**< 32 bit int of Mass Conc of PM1.0 */
00151     uint32_t mass_2p5_i;          /**< 32 bit int of Mass Conc of PM2.5 */
00152     uint32_t mass_4p0_i;          /**< 32 bit int of Mass Conc of PM4.0 */
00153     uint32_t mass_10p0_i;         /**< 32 bit int of Mass Conc of PM10 */
00154 
00155     uint32_t num_0p5_i;           /**< 32 bit int of Number Conc of PM0.5 */
00156     uint32_t num_1p0_i;           /**< 32 bit int of Number Conc of PM1.0 */
00157     uint32_t num_2p5_i;           /**< 32 bit int of Number Conc of PM2.5 */
00158     uint32_t num_4p0_i;           /**< 32 bit int of Number Conc of PM4.0 */
00159     uint32_t num_10p0_i;          /**< 32 bit int of Number Conc of PM10 */
00160 
00161     uint32_t typ_pm_size_i;       /**< 32 bit int of Typical Particle Size */
00162     
00163     /** Start Auto-Measurement 
00164      *
00165      * @param --none--
00166      *
00167      * @return enum SPSerror
00168      */
00169     uint8_t StartMeasurement();
00170     
00171     /** Stop Auto-Measurement 
00172      *
00173      * @param --none--
00174      *
00175      * @return enum SPSerror
00176      */
00177     uint8_t StopMeasurement();
00178     
00179     /** Get Ready Status register 
00180      *
00181      * @param --none--
00182      * @see Ready Status result
00183      *
00184      * @return enum SPSerror
00185      */
00186     uint8_t GetReadyStatus();
00187     
00188     /** Get all particulate matter parameters
00189      *
00190      * @param --none-
00191      * @see Results in Public member variables
00192      *
00193      * @return enum SPSerror
00194      */
00195     uint8_t ReadMeasurement();
00196     
00197     /** Calculate the SPS30 CRC value
00198      *
00199      * @param 16 bit value to perform a CRC check on
00200      *
00201      * @return 8 bit CRC value
00202      */
00203     uint8_t CalcCrc2b(uint16_t seed);
00204     
00205     /** Compare received CRC value with calculated CRC value
00206      *
00207      * @param 16 bit value to perform a CRC check on
00208      * @param 8 bit value to compare CRC values
00209      *
00210      * @return enum SPSerror
00211      */
00212     uint8_t CheckCrc2b(uint16_t seed, uint8_t crc_in);
00213     
00214     /** Get Serial Number
00215      *
00216      * @param --none--
00217      * @see ASCII Serial Number as sn[33]
00218      *
00219      * @return enum SPSerror
00220      */
00221     uint8_t GetSerialNumber();
00222     
00223     /** Read Auto Cleaning Interval on the SPS30
00224      *
00225      * @param --none--
00226      *
00227      * @return enum SPSerror
00228      */
00229     uint8_t ReadAutoCleanInterval();
00230     
00231     /** Set Auto Cleaning Interval on the SPS30
00232      *
00233      * @param Auto Cleaning Interval in seconds 
00234      * default is 604800s = 1 week, 0 to disable auto clean
00235      *
00236      * @return enum SPSerror
00237      */
00238     uint8_t SetAutoCleanInterval(uint32_t set_interval = 604800);
00239     
00240 public:
00241     
00242     /** Perform manual Fan Cleaning
00243      *
00244      * @param --none--
00245      *
00246      * @return enum SPSerror
00247      */
00248     uint8_t StartFanClean();
00249     
00250 private:
00251 
00252     /** Perform a soft reset
00253      *
00254      * @param --none--
00255      *
00256      * @return enum SPSerror
00257      */
00258     uint8_t SoftReset();
00259  
00260 protected:
00261     I2C     _i2c;    
00262 
00263 };    
00264 #endif
00265