BEI ZHANG / Mbed 2 deprecated RM3100_SPI_SampleCode

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers RM3100.h Source File

RM3100.h

Go to the documentation of this file.
00001 /**
00002 * @file         RM3100.h
00003 *
00004 * @brief        Sample interface for RM3100.
00005 *
00006 * @authors      Betty Zhang
00007 * @date         05/21/2018
00008 * @copyright    (C) 2018 PNI Corp, Protonex LLC
00009 *
00010 * @copyright    Disclosure to third parties or reproduction in any form
00011 *               whatsoever, without prior written consent, is strictly forbidden
00012 *
00013 */
00014 #include "mbed.h"
00015 
00016 
00017 // Global typedef
00018 typedef signed char     s8;
00019 typedef char            u8;    
00020 typedef short           s16;   
00021 typedef unsigned short  u16;   
00022 typedef int             s32;   
00023 typedef unsigned int    u32;   
00024 typedef unsigned long   u64;
00025 
00026 //RM3100 Registers 
00027 #define RM3100_POLL_REG     0x00 /**< RW; Poll for a single measurement*/
00028 #define RM3100_CMM_REG      0x01 /**< RW; Initiate continuous measurement mode*/
00029 #define RM3100_CCXLSB_REG   0x04 /**< RW; Cycle count X-axis */
00030 #define RM3100_CCXMSB_REG   0x05
00031 #define RM3100_CCYLSB_REG   0x06 /**< RW; Cycle count Y-axis */
00032 #define RM3100_CCYMSB_REG   0x07
00033 #define RM3100_CCZLSB_REG   0x08 /**< RW; Cycle count Z-axis */
00034 #define RM3100_CCZMSB_REG   0x09
00035 #define RM3100_TMRC_REG     0x0B /**< RW; Set data rate in continuous measurement mode*/
00036 #define RM3100_MX_REG       0x24 /**< RW; Measurement Result X-axis, Signed 24-bit */
00037 #define RM3100_BIST_REG     0x33 /**< RW; Built-in self test */
00038 #define RM3100_STATUS_REG   0x34 /**< R; Status of DRDY */
00039 #define RM3100_REVID_REG    0x36 /**< R; Revision ID, default 0x22 */
00040 
00041 //RM3100 Default values
00042 #define DEFAULTCCOUNT 200 //200
00043 #define DEFAULTGAIN 75    //200 Cycle Count, Gain 75
00044 
00045 //Other Settings
00046 #define UPPERCYCLECOUNT 400
00047 #define LOWERCYCLECOUNT 30
00048 
00049 
00050  /**
00051  * \brief Host interface control
00052  */
00053 #pragma pack(push, 1)
00054 typedef union RegCMM
00055 {
00056     /**
00057     * \brief Direct access to the complete 8bit register
00058     */
00059     u8 reg;
00060     /**
00061     * \brief Access to individual bits in the register.
00062     */
00063     struct
00064     {
00065         u8 Start : 1;             /**< bit 0: Initiate Continuous Measurement Mode */
00066         u8 Alarm : 1;             /**< bit 1: Go high if measurment outside a prefefined range */
00067         u8 Drdm :  2;             /**< bit 2: DRDM bits establish required condition to trigger DRDY */
00068                                   /**< 0 = drdy high when Alarm = 1 and complete of full measurement set by CMX CMY CMZ */
00069                                   /**< 1 = drdy high after complete of measurement on any axis */
00070                                   /**< 2 = drdy high after complete of full measurement set by CMX CMY CMZ */
00071                                   /**< 3 = drdy high when Alarm = 1 */
00072         u8 CMX : 1;               /**< bit 4: 1 means measurement on this Axis */
00073         u8 CMY : 1;               /**< bit 5: 1 means measurement on this Axis */
00074         u8 CMZ : 1;               /**< bit 6: 1 means measurement on this Axis */
00075         u8 LDM : 1;               /**< bit 7: 0 absolute alarm mode, 1 relative alarm mode */
00076     }
00077     bits;
00078 }
00079 RegCMM;                          /**< typedef for storing CMM register values */
00080 #pragma pack(pop)
00081 
00082 #pragma pack(push, 1)
00083 typedef union RegTMRC
00084 {
00085     /**
00086     * \brief Direct access to the complete 8bit register
00087     */
00088     u8 reg;
00089     /**
00090     * \brief Access to individual bits in the register.
00091     */
00092     struct
00093     {
00094         u8 TMRC : 4;             /**< bit 0 - 3: Time between measurement in CCM moden 1-axis */
00095                                  /**< value 0b0010 to 0b1111, 0x2 to 0xF, bigger value longer interval */
00096         u8 Resv : 4;             /**< bit 4 - 7: Fixed as 0b1001, 0x9 */
00097     }
00098     bits;
00099 }
00100 RegTMRC;                          /**< typedef for storing TMRC register values */
00101 #pragma pack(pop)
00102 
00103 #pragma pack(push, 1)
00104 typedef union RegBIST
00105 {
00106     /**
00107     * \brief Direct access to the complete 8bit register
00108     */
00109     u8 reg;
00110     /**
00111     * \brief Access to individual bits in the register.
00112     */
00113     struct
00114     {
00115         u8 BP : 2;             /**< bit 0-1: Define number of LR periods for measurement */
00116                                /**< 0 = unused */
00117                                /**< 1 = 1 LR Period */
00118                                /**< 2 = 2 LR Periods */
00119                                /**< 3 = 4 LR Periods */
00120         u8 BW : 2;             /**< bit 2-3: Define timeout period for LR oscillator perriod */
00121                                /**< 0 = unused */
00122                                /**< 1 = 1 sleep oscillation cycle,  30us */
00123                                /**< 2 = 2 sleep oscillation cycles,  60us */
00124                                /**< 3 = 4 sleep oscillation cycles,  120us */
00125         u8 XYZOK : 3;          /**< bit 4-6: Read only */
00126                                /**< bit 4: 1 X ok, 0 not ok */
00127                                /**< bit 5: 1 Y ok, 0 not ok */
00128                                /**< bit 6: 1 Z ok, 0 not ok */
00129         u8 STE : 1;            /**< bit 7: write 1 to enable the self test */
00130     }
00131     bits;
00132 }
00133 RegBIST;                          /**< typedef for storing TMRC register values */
00134 #pragma pack(pop)
00135 
00136 #pragma pack(push, 1)
00137 typedef union RegPOLL
00138 {
00139     /**
00140     * \brief Direct access to the complete 8bit register
00141     */
00142     u8 reg;
00143     /**
00144     * \brief Access to individual bits in the register.
00145     */
00146     struct
00147     {
00148         u8 LowNibble : 4;      /**< bit 0-3: Reserved */
00149         u8 PMX : 1;            /**< bit 4: Measure X axis */
00150         u8 PMY : 1;            /**< bit 5: Measure Y axis */
00151         u8 PMZ : 1;            /**< bit 6: Measure Z axis */
00152         u8 MSB : 1;            /**< bit 7: Reserved */
00153     }
00154     bits;
00155 }
00156 RegPOLL;                          /**< typedef for storing TMRC register values */
00157 #pragma pack(pop)
00158 
00159 
00160 //RM3100 class
00161 class RM3100
00162 {
00163 
00164 public:
00165     void ClearDrdyInt(void);
00166     
00167     void RunCMM(int flag);
00168     
00169     void ChangeSampleRate(int flag);
00170     
00171     void SetSampleRateReg(int rate); 
00172     
00173     int GetSampleRate(int * reg_val); 
00174     
00175     void ChangeCycleCount(int flag);
00176     
00177     void SetCycleCountReg();
00178 
00179     void ReadRM3100(); 
00180     
00181     void SetDrdyIntFlag(u8 flag);
00182     
00183     u8 GetDrdyIntFlag(void);
00184     
00185     void DrdyCallBack(void);
00186     
00187     void ProcessDrdyInt(void);
00188     
00189     void DisplayCycleCount();
00190     
00191     void DisplayREVIDReg();
00192         
00193     void SelfTest();
00194     
00195     //constructor
00196     RM3100();
00197     
00198 private:
00199     u8 rm3100_service_flag;
00200     int current_gain[3];
00201     int current_ccount[3];
00202     int sample_rate;
00203     int prev_rate;
00204     
00205     DigitalOut cs; //ssel
00206     InterruptIn drdy; //Drdy pin D9
00207     
00208     SPI spi;
00209 
00210 };