OB1203 basic mbed driver

Dependents:   OB1203_IDT

Committer:
laserdad
Date:
Mon May 14 19:31:41 2018 +0000
Revision:
6:337ee64ca7ab
Parent:
5:527556d05aa8
Child:
8:6c19c052b053
fixed temp readout (byte swap)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
laserdad 0:2da1f69b38fd 1 #ifndef __OB1203_H__
laserdad 0:2da1f69b38fd 2 #define __OB1203_H__
laserdad 0:2da1f69b38fd 3
laserdad 0:2da1f69b38fd 4 #include "mbed.h"
laserdad 4:f25ca600a0e6 5 //#include "SoftI2C.h"
laserdad 0:2da1f69b38fd 6
laserdad 1:99ca9c464503 7 //#define DEBUG
laserdad 0:2da1f69b38fd 8
laserdad 0:2da1f69b38fd 9 #define OB1203_ADDR 0xA6
laserdad 0:2da1f69b38fd 10
laserdad 0:2da1f69b38fd 11 //Define registers
laserdad 0:2da1f69b38fd 12 #define REG_STATUS_0 0x00
laserdad 0:2da1f69b38fd 13 #define REG_STATUS_1 0x01
laserdad 0:2da1f69b38fd 14 #define REG_PS_DATA 0x02
laserdad 0:2da1f69b38fd 15 #define REG_LS_W_DATA 0x04
laserdad 0:2da1f69b38fd 16 #define REG_LS_G_DATA 0x07
laserdad 0:2da1f69b38fd 17 #define REG_LS_B_DATA 0x0A
laserdad 0:2da1f69b38fd 18 #define REG_LS_R_DATA 0x0D
laserdad 0:2da1f69b38fd 19 #define REG_LS_C_DATA 0x10
laserdad 4:f25ca600a0e6 20 #define REG_TEMP_DATA 0x13
laserdad 0:2da1f69b38fd 21 #define REG_MAIN_CTRL_0 0x15
laserdad 0:2da1f69b38fd 22 #define REG_MAIN_CTRL_1 0x16
laserdad 0:2da1f69b38fd 23 #define REG_PS_LED_CURR 0x17
laserdad 0:2da1f69b38fd 24 #define REG_PS_CAN_PULSES 0x19
laserdad 0:2da1f69b38fd 25 #define REG_PS_PWIDTH_RATE 0x1A
laserdad 0:2da1f69b38fd 26 #define REG_PS_CAN_DIG 0x1B
laserdad 0:2da1f69b38fd 27 #define REG_PS_MOV_AVG_HYS 0x1D
laserdad 0:2da1f69b38fd 28 #define REG_PS_THRES_HI 0x1E
laserdad 0:2da1f69b38fd 29 #define REG_PS_THRES_LO 0x20
laserdad 0:2da1f69b38fd 30 #define REG_LS_RES_RATE 0x22
laserdad 0:2da1f69b38fd 31 #define REG_LS_GAIN 0x23
laserdad 0:2da1f69b38fd 32 #define REG_LS_THRES_HI 0x24
laserdad 0:2da1f69b38fd 33 #define REG_LS_THRES_LO 0x27
laserdad 0:2da1f69b38fd 34 #define REG_LS_THRES_VAR 0x2A
laserdad 0:2da1f69b38fd 35 #define REG_INT_CFG_0 0x2B
laserdad 0:2da1f69b38fd 36 #define REG_PS_INT_CFG_1 0x2C
laserdad 0:2da1f69b38fd 37 #define REG_INT_PST 0x2D
laserdad 0:2da1f69b38fd 38 #define REG_PPG_PS_GAIN 0x2E
laserdad 0:2da1f69b38fd 39 #define REG_PPG_PS_CFG 0x2F
laserdad 0:2da1f69b38fd 40 #define REG_PPG_IRLED_CURR 0x30
laserdad 0:2da1f69b38fd 41 #define REG_PPG_RLED_CURR 0x32
laserdad 0:2da1f69b38fd 42 #define REG_PPG_CAN_ANA 0x34
laserdad 0:2da1f69b38fd 43 #define REG_PPG_AVG 0x35
laserdad 0:2da1f69b38fd 44 #define REG_PPG_PWIDTH_RATE 0x36
laserdad 0:2da1f69b38fd 45 #define REG_FIFO_CFG 0x37
laserdad 0:2da1f69b38fd 46 #define REG_FIFO_WR_PTR 0x38
laserdad 0:2da1f69b38fd 47 #define REG_FIFO_RD_PTR 0x39
laserdad 0:2da1f69b38fd 48 #define REG_FIFO_OVF_CNT 0x3A
laserdad 0:2da1f69b38fd 49 #define REG_FIFO_DATA 0x3B
laserdad 0:2da1f69b38fd 50 #define REG_PART_ID 0x3D
laserdad 0:2da1f69b38fd 51 #define REG_DEVICE_CFG 0x4D
laserdad 0:2da1f69b38fd 52
laserdad 0:2da1f69b38fd 53 //Define settings
laserdad 0:2da1f69b38fd 54 //STATUS_0
laserdad 4:f25ca600a0e6 55 #define POR_STATUS 0x80
laserdad 4:f25ca600a0e6 56 #define LS_INT_STATUS 0x02
laserdad 0:2da1f69b38fd 57 #define LS_NEW_DATA 0x01
laserdad 0:2da1f69b38fd 58 //STATUS_1
laserdad 0:2da1f69b38fd 59 #define LED_DRIVER_STATUS 0x40
laserdad 0:2da1f69b38fd 60 #define FIFO_AFULL_STATUS 0x20
laserdad 0:2da1f69b38fd 61 #define PPG_DATA_STATUS 0x10
laserdad 0:2da1f69b38fd 62 #define PS_LOGIC_STATUS 0x04
laserdad 0:2da1f69b38fd 63 #define PS_INT_STATUS 0x02
laserdad 0:2da1f69b38fd 64 #define PS_NEW_DATA 0x01
laserdad 0:2da1f69b38fd 65 //MAIN_CTRL_0
laserdad 0:2da1f69b38fd 66 #define SW_RESET 0x01<<7
laserdad 0:2da1f69b38fd 67 #define LS_SAI_ON 0x01<<3
laserdad 0:2da1f69b38fd 68 #define LS_SAI_OFF 0x00
laserdad 0:2da1f69b38fd 69 #define ALS_MODE 0x00
laserdad 0:2da1f69b38fd 70 #define RGB_MODE 0x01<<1
laserdad 0:2da1f69b38fd 71 #define LS_OFF 0x00
laserdad 0:2da1f69b38fd 72 #define LS_ON 0x01
laserdad 0:2da1f69b38fd 73 //MAIN_CTRL_1
laserdad 0:2da1f69b38fd 74 #define PS_SAI_ON 0x01<<3
laserdad 0:2da1f69b38fd 75 #define PS_SAI_OFF 0x00
laserdad 0:2da1f69b38fd 76 #define PS_MODE 0x00
laserdad 0:2da1f69b38fd 77 #define HR_MODE 0x01<<1
laserdad 0:2da1f69b38fd 78 #define SPO2_MODE 0x02<<1
laserdad 0:2da1f69b38fd 79 #define PPG_PS_ON 0x01
laserdad 0:2da1f69b38fd 80 #define PPG_PS_OFF 0x00
laserdad 4:f25ca600a0e6 81 #define TEMP_ON 0x01<<7
laserdad 4:f25ca600a0e6 82 #define TEMP_OFF 0x00
laserdad 0:2da1f69b38fd 83 //PS_CAN_PULSES
laserdad 0:2da1f69b38fd 84 #define PS_CAN_ANA_0 0x00 //off
laserdad 0:2da1f69b38fd 85 #define PS_CAN_ANA_1 0x01<<6 //50% of FS
laserdad 0:2da1f69b38fd 86 #define PS_CAN_ANA_2 0x02<<6 //100% of FS
laserdad 0:2da1f69b38fd 87 #define PS_CAN_ANA_3 0x03<<6 //150% of FS
laserdad 1:99ca9c464503 88 #define PS_PULSES(x) (( (x) & (0x07) )<<3) //where x = 0..5 and num pulses = 2^x
laserdad 0:2da1f69b38fd 89 //PS_PWIDTH_RATE
laserdad 0:2da1f69b38fd 90 #define PS_PWIDTH(x) (x & 0x03)<<4 //where x = 0..3
laserdad 0:2da1f69b38fd 91 #define PS_RATE(x) (x & 0x07) //where x = 0..7
laserdad 0:2da1f69b38fd 92 //PS_MOV_AVG_HYS
laserdad 0:2da1f69b38fd 93 #define PS_AVG_ON 1<<7
laserdad 0:2da1f69b38fd 94 #define PS_AVG_OFF 0
laserdad 0:2da1f69b38fd 95 #define PS_HYS_LEVEL(x) x>>1 //where x=0..256
laserdad 0:2da1f69b38fd 96 //LS_RES_RATE
laserdad 0:2da1f69b38fd 97 #define LS_RES(x) (x & 0x07)<<4 //where x=0..7
laserdad 0:2da1f69b38fd 98 #define LS_RATE(x) (x & 0x07) //where x=0..7
laserdad 0:2da1f69b38fd 99 //LS_GAIN
laserdad 0:2da1f69b38fd 100 #define LS_GAIN(x) (x & 0x03)
laserdad 0:2da1f69b38fd 101 //LS_THRES_VAR
laserdad 0:2da1f69b38fd 102 #define LS_THRES_VAR(x) (x & 0x07)
laserdad 0:2da1f69b38fd 103 //INT_CFG_0
laserdad 0:2da1f69b38fd 104 #define LS_INT_SEL_W 0
laserdad 0:2da1f69b38fd 105 #define LS_INT_SEL_G 1<<4
laserdad 0:2da1f69b38fd 106 #define LS_INT_SEL_R 2<<4
laserdad 0:2da1f69b38fd 107 #define LS_INT_SEL_B 3<<4
laserdad 0:2da1f69b38fd 108 #define LS_THRES_INT_MODE 0
laserdad 0:2da1f69b38fd 109 #define LS_VAR_INT_MODE 1<<1
laserdad 0:2da1f69b38fd 110 #define LS_INT_ON 1
laserdad 0:2da1f69b38fd 111 #define LS_INT_OFF 0
laserdad 0:2da1f69b38fd 112 //INT_CFG_1
laserdad 0:2da1f69b38fd 113 #define AFULL_INT_ON 1<<5
laserdad 0:2da1f69b38fd 114 #define AFULL_INT_OFF 0
laserdad 0:2da1f69b38fd 115 #define PPG_INT_ON 1<<4
laserdad 0:2da1f69b38fd 116 #define PPG_INT_OFF 0
laserdad 2:3cba0fe6db60 117 #define PS_INT_READ_CLEARS 0<<1
laserdad 2:3cba0fe6db60 118 #define PS_INT_NO_CLEAR 1
laserdad 0:2da1f69b38fd 119 #define PS_INT_ON 1
laserdad 0:2da1f69b38fd 120 #define PS_INT_OFF 0
laserdad 0:2da1f69b38fd 121 //INT_PST
laserdad 0:2da1f69b38fd 122 #define LS_PERSIST(x) (x & 0x0F)<<4
laserdad 0:2da1f69b38fd 123 #define PS_PERSIST(x) (x & 0x0F)
laserdad 0:2da1f69b38fd 124 //PPG_PS_GAIN
laserdad 0:2da1f69b38fd 125 #define PPG_PS_GAIN_1 0
laserdad 0:2da1f69b38fd 126 #define PPG_PS_GAIN_1P5 1<<4
laserdad 0:2da1f69b38fd 127 #define PPG_PS_GAIN_2 2<<4
laserdad 0:2da1f69b38fd 128 #define PPG_PS_GAIN_4 3<<4
laserdad 0:2da1f69b38fd 129 //PPG_PS_CFG
laserdad 0:2da1f69b38fd 130 #define PPG_POW_SAVE_ON 1<<6
laserdad 0:2da1f69b38fd 131 #define PPG_POW_SAVE_OFF 0
laserdad 0:2da1f69b38fd 132 #define LED_FLIP_ON 1<<3
laserdad 0:2da1f69b38fd 133 #define LED_FLIP_OFF 0
laserdad 0:2da1f69b38fd 134 //PPG_CAN_ANA
laserdad 0:2da1f69b38fd 135 #define PPG_CH1_CAN(x) (x & 0x03)<<2
laserdad 0:2da1f69b38fd 136 #define PPG_CH2_CAN(x) (x & 0x03)
laserdad 0:2da1f69b38fd 137 //PPG_AVG
laserdad 0:2da1f69b38fd 138 #define PPG_AVG(x) (x & 0x07)<<4
laserdad 0:2da1f69b38fd 139 //PPG_PWIDTH_RATE
laserdad 0:2da1f69b38fd 140 #define PPG_PWIDTH(x) (x & 0x07)<<4
laserdad 0:2da1f69b38fd 141 #define PPG_FREQ_60HZ 0
laserdad 0:2da1f69b38fd 142 #define PPG_FREQ_50HZ 1<<3
laserdad 0:2da1f69b38fd 143 #define PPG_RATE(x) (x & 0x07)
laserdad 0:2da1f69b38fd 144 //FIFO_CFG
laserdad 0:2da1f69b38fd 145 #define FIFO_ROLL_ON 1<<4
laserdad 0:2da1f69b38fd 146 #define FIFO_ROLL_OFF 0
laserdad 0:2da1f69b38fd 147 #define AFULL_SAMPLES_LEFT(x) (x & 0x0F)
laserdad 0:2da1f69b38fd 148
laserdad 0:2da1f69b38fd 149
laserdad 0:2da1f69b38fd 150
laserdad 0:2da1f69b38fd 151
laserdad 0:2da1f69b38fd 152
laserdad 0:2da1f69b38fd 153 #define POR_TIME_MS 5 //a guess
laserdad 0:2da1f69b38fd 154
laserdad 0:2da1f69b38fd 155
laserdad 0:2da1f69b38fd 156
laserdad 0:2da1f69b38fd 157 class OB1203
laserdad 0:2da1f69b38fd 158 {
laserdad 0:2da1f69b38fd 159 I2C *i2c;
laserdad 4:f25ca600a0e6 160 // SoftI2C *i2c;
laserdad 0:2da1f69b38fd 161
laserdad 4:f25ca600a0e6 162 public:
laserdad 4:f25ca600a0e6 163 char ls_res;
laserdad 4:f25ca600a0e6 164 char ls_rate;
laserdad 4:f25ca600a0e6 165 char ls_gain;
laserdad 4:f25ca600a0e6 166 uint32_t ls_thres_hi;
laserdad 4:f25ca600a0e6 167 uint32_t ls_thres_lo;
laserdad 4:f25ca600a0e6 168 char ls_sai;
laserdad 4:f25ca600a0e6 169 char ls_mode;
laserdad 4:f25ca600a0e6 170 char ls_en;
laserdad 4:f25ca600a0e6 171 char ls_int_sel;
laserdad 4:f25ca600a0e6 172 char ls_var_mode;
laserdad 4:f25ca600a0e6 173 char ps_sai_en;
laserdad 4:f25ca600a0e6 174 char temp_en;
laserdad 4:f25ca600a0e6 175 char ppg_ps_mode;
laserdad 4:f25ca600a0e6 176 char ppg_ps_en;
laserdad 4:f25ca600a0e6 177 char ps_can_ana;
laserdad 4:f25ca600a0e6 178 char afull_int_en;
laserdad 4:f25ca600a0e6 179 char ppg_int_en;
laserdad 4:f25ca600a0e6 180 char ps_logic_mode;
laserdad 4:f25ca600a0e6 181 uint16_t ps_digital_can;
laserdad 4:f25ca600a0e6 182 char ps_int_en;
laserdad 4:f25ca600a0e6 183 char ls_persist;
laserdad 4:f25ca600a0e6 184 char ps_persist;
laserdad 4:f25ca600a0e6 185 uint16_t ps_thres_hi;
laserdad 4:f25ca600a0e6 186 uint16_t ps_thres_lo;
laserdad 4:f25ca600a0e6 187 uint16_t ps_current;
laserdad 4:f25ca600a0e6 188 uint16_t ir_current;
laserdad 4:f25ca600a0e6 189 uint16_t r_current;
laserdad 4:f25ca600a0e6 190 char ppg_ps_gain;
laserdad 4:f25ca600a0e6 191 char ppg_pow_save;
laserdad 4:f25ca600a0e6 192 char led_flip;
laserdad 4:f25ca600a0e6 193 char ch1_can_ana;
laserdad 4:f25ca600a0e6 194 char ch2_can_ana;
laserdad 4:f25ca600a0e6 195 char ppg_avg;
laserdad 4:f25ca600a0e6 196 char ppg_pwidth;
laserdad 4:f25ca600a0e6 197 char ppg_freq;
laserdad 4:f25ca600a0e6 198 char ppg_rate;
laserdad 4:f25ca600a0e6 199 char ps_pulses;
laserdad 4:f25ca600a0e6 200 char ps_pwidth;
laserdad 4:f25ca600a0e6 201 char ps_rate;
laserdad 4:f25ca600a0e6 202 char ps_avg_en;
laserdad 4:f25ca600a0e6 203 char ps_hys_level;
laserdad 4:f25ca600a0e6 204 char ls_int_en;
laserdad 4:f25ca600a0e6 205 char fifo_rollover_en;
laserdad 4:f25ca600a0e6 206 char fifo_afull_samples_left;
laserdad 4:f25ca600a0e6 207 char writePointer;
laserdad 4:f25ca600a0e6 208 char readPointer;
laserdad 4:f25ca600a0e6 209 char fifoOverflow;
laserdad 4:f25ca600a0e6 210 OB1203 (I2C *);
laserdad 4:f25ca600a0e6 211 // OB1203(SoftI2C *);
laserdad 0:2da1f69b38fd 212
laserdad 4:f25ca600a0e6 213 // Low-level operations
laserdad 4:f25ca600a0e6 214 void reset();
laserdad 4:f25ca600a0e6 215 uint16_t get_status();
laserdad 4:f25ca600a0e6 216 void writeRegister(int, char, char);
laserdad 4:f25ca600a0e6 217 void writeBlock(int, char, char *, char);
laserdad 4:f25ca600a0e6 218 void readBlock(int, char, char *, int);
laserdad 4:f25ca600a0e6 219 uint32_t bytes2uint32(char *, int);
laserdad 6:337ee64ca7ab 220 uint32_t twoandhalfBytes2uint32(char *, int);
laserdad 4:f25ca600a0e6 221
laserdad 4:f25ca600a0e6 222 // High-level operations
laserdad 4:f25ca600a0e6 223 bool dataIsNew();
laserdad 6:337ee64ca7ab 224 bool lsIsNew();
laserdad 6:337ee64ca7ab 225 bool psIsNew();
laserdad 6:337ee64ca7ab 226 bool tempIsNew();
laserdad 5:527556d05aa8 227 bool bioIsNew();
laserdad 4:f25ca600a0e6 228 void setMainConfig();
laserdad 4:f25ca600a0e6 229 void setIntConfig();
laserdad 4:f25ca600a0e6 230 void setLSthresh();
laserdad 4:f25ca600a0e6 231 void setPSthresh();
laserdad 4:f25ca600a0e6 232 void setPScurrent();
laserdad 4:f25ca600a0e6 233 void setPPGcurrent();
laserdad 4:f25ca600a0e6 234 void setPPG_PSgain_cfg();
laserdad 4:f25ca600a0e6 235 void setPPGana_can();
laserdad 4:f25ca600a0e6 236 void setDigitalCan();
laserdad 4:f25ca600a0e6 237 void setPPGavg_and_rate();
laserdad 4:f25ca600a0e6 238 void setFifoConfig();
laserdad 4:f25ca600a0e6 239 char get_ps_data(uint32_t *);
laserdad 4:f25ca600a0e6 240 char get_ls_data(uint32_t *);
laserdad 4:f25ca600a0e6 241 char get_ps_ls_data(uint32_t *);
laserdad 4:f25ca600a0e6 242 void init_ps();
laserdad 4:f25ca600a0e6 243 void init_rgb();
laserdad 4:f25ca600a0e6 244 void init_ps_rgb();
laserdad 4:f25ca600a0e6 245 void init_hr();
laserdad 4:f25ca600a0e6 246 void init_spo2();
laserdad 4:f25ca600a0e6 247 void getFifoInfo(char *fifo_info);
laserdad 4:f25ca600a0e6 248 uint8_t getNumFifoSamplesAvailable();
laserdad 4:f25ca600a0e6 249 void getFifoSamples(uint8_t, char *);
laserdad 4:f25ca600a0e6 250 void parseFifoSamples(char, char *, uint32_t *);
laserdad 4:f25ca600a0e6 251 char get_part_ID(char *);
laserdad 4:f25ca600a0e6 252
laserdad 4:f25ca600a0e6 253 //variables
laserdad 4:f25ca600a0e6 254 uint16_t rate;
laserdad 4:f25ca600a0e6 255 char res;
laserdad 4:f25ca600a0e6 256 char gain;
laserdad 4:f25ca600a0e6 257 uint32_t data_max;
laserdad 4:f25ca600a0e6 258 uint32_t reg_max;
laserdad 0:2da1f69b38fd 259
laserdad 0:2da1f69b38fd 260 };
laserdad 0:2da1f69b38fd 261
laserdad 0:2da1f69b38fd 262
laserdad 0:2da1f69b38fd 263 #endif