OB1203 basic mbed driver

Dependents:   OB1203_IDT

Committer:
laserdad
Date:
Thu May 17 21:52:52 2018 +0000
Revision:
8:6c19c052b053
Parent:
6:337ee64ca7ab
Child:
9:0c3d2eea1c79
added oscillator trimming

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 8:6c19c052b053 52 #define REG_OSC_TRIM 0x3E
laserdad 0:2da1f69b38fd 53
laserdad 0:2da1f69b38fd 54 //Define settings
laserdad 0:2da1f69b38fd 55 //STATUS_0
laserdad 4:f25ca600a0e6 56 #define POR_STATUS 0x80
laserdad 4:f25ca600a0e6 57 #define LS_INT_STATUS 0x02
laserdad 0:2da1f69b38fd 58 #define LS_NEW_DATA 0x01
laserdad 0:2da1f69b38fd 59 //STATUS_1
laserdad 0:2da1f69b38fd 60 #define LED_DRIVER_STATUS 0x40
laserdad 0:2da1f69b38fd 61 #define FIFO_AFULL_STATUS 0x20
laserdad 0:2da1f69b38fd 62 #define PPG_DATA_STATUS 0x10
laserdad 0:2da1f69b38fd 63 #define PS_LOGIC_STATUS 0x04
laserdad 0:2da1f69b38fd 64 #define PS_INT_STATUS 0x02
laserdad 0:2da1f69b38fd 65 #define PS_NEW_DATA 0x01
laserdad 0:2da1f69b38fd 66 //MAIN_CTRL_0
laserdad 0:2da1f69b38fd 67 #define SW_RESET 0x01<<7
laserdad 0:2da1f69b38fd 68 #define LS_SAI_ON 0x01<<3
laserdad 0:2da1f69b38fd 69 #define LS_SAI_OFF 0x00
laserdad 0:2da1f69b38fd 70 #define ALS_MODE 0x00
laserdad 0:2da1f69b38fd 71 #define RGB_MODE 0x01<<1
laserdad 0:2da1f69b38fd 72 #define LS_OFF 0x00
laserdad 0:2da1f69b38fd 73 #define LS_ON 0x01
laserdad 0:2da1f69b38fd 74 //MAIN_CTRL_1
laserdad 0:2da1f69b38fd 75 #define PS_SAI_ON 0x01<<3
laserdad 0:2da1f69b38fd 76 #define PS_SAI_OFF 0x00
laserdad 0:2da1f69b38fd 77 #define PS_MODE 0x00
laserdad 0:2da1f69b38fd 78 #define HR_MODE 0x01<<1
laserdad 0:2da1f69b38fd 79 #define SPO2_MODE 0x02<<1
laserdad 0:2da1f69b38fd 80 #define PPG_PS_ON 0x01
laserdad 0:2da1f69b38fd 81 #define PPG_PS_OFF 0x00
laserdad 4:f25ca600a0e6 82 #define TEMP_ON 0x01<<7
laserdad 4:f25ca600a0e6 83 #define TEMP_OFF 0x00
laserdad 0:2da1f69b38fd 84 //PS_CAN_PULSES
laserdad 0:2da1f69b38fd 85 #define PS_CAN_ANA_0 0x00 //off
laserdad 0:2da1f69b38fd 86 #define PS_CAN_ANA_1 0x01<<6 //50% of FS
laserdad 0:2da1f69b38fd 87 #define PS_CAN_ANA_2 0x02<<6 //100% of FS
laserdad 0:2da1f69b38fd 88 #define PS_CAN_ANA_3 0x03<<6 //150% of FS
laserdad 1:99ca9c464503 89 #define PS_PULSES(x) (( (x) & (0x07) )<<3) //where x = 0..5 and num pulses = 2^x
laserdad 0:2da1f69b38fd 90 //PS_PWIDTH_RATE
laserdad 0:2da1f69b38fd 91 #define PS_PWIDTH(x) (x & 0x03)<<4 //where x = 0..3
laserdad 0:2da1f69b38fd 92 #define PS_RATE(x) (x & 0x07) //where x = 0..7
laserdad 0:2da1f69b38fd 93 //PS_MOV_AVG_HYS
laserdad 0:2da1f69b38fd 94 #define PS_AVG_ON 1<<7
laserdad 0:2da1f69b38fd 95 #define PS_AVG_OFF 0
laserdad 0:2da1f69b38fd 96 #define PS_HYS_LEVEL(x) x>>1 //where x=0..256
laserdad 0:2da1f69b38fd 97 //LS_RES_RATE
laserdad 0:2da1f69b38fd 98 #define LS_RES(x) (x & 0x07)<<4 //where x=0..7
laserdad 0:2da1f69b38fd 99 #define LS_RATE(x) (x & 0x07) //where x=0..7
laserdad 0:2da1f69b38fd 100 //LS_GAIN
laserdad 0:2da1f69b38fd 101 #define LS_GAIN(x) (x & 0x03)
laserdad 0:2da1f69b38fd 102 //LS_THRES_VAR
laserdad 0:2da1f69b38fd 103 #define LS_THRES_VAR(x) (x & 0x07)
laserdad 0:2da1f69b38fd 104 //INT_CFG_0
laserdad 0:2da1f69b38fd 105 #define LS_INT_SEL_W 0
laserdad 0:2da1f69b38fd 106 #define LS_INT_SEL_G 1<<4
laserdad 0:2da1f69b38fd 107 #define LS_INT_SEL_R 2<<4
laserdad 0:2da1f69b38fd 108 #define LS_INT_SEL_B 3<<4
laserdad 0:2da1f69b38fd 109 #define LS_THRES_INT_MODE 0
laserdad 0:2da1f69b38fd 110 #define LS_VAR_INT_MODE 1<<1
laserdad 0:2da1f69b38fd 111 #define LS_INT_ON 1
laserdad 0:2da1f69b38fd 112 #define LS_INT_OFF 0
laserdad 0:2da1f69b38fd 113 //INT_CFG_1
laserdad 0:2da1f69b38fd 114 #define AFULL_INT_ON 1<<5
laserdad 0:2da1f69b38fd 115 #define AFULL_INT_OFF 0
laserdad 0:2da1f69b38fd 116 #define PPG_INT_ON 1<<4
laserdad 0:2da1f69b38fd 117 #define PPG_INT_OFF 0
laserdad 2:3cba0fe6db60 118 #define PS_INT_READ_CLEARS 0<<1
laserdad 2:3cba0fe6db60 119 #define PS_INT_NO_CLEAR 1
laserdad 0:2da1f69b38fd 120 #define PS_INT_ON 1
laserdad 0:2da1f69b38fd 121 #define PS_INT_OFF 0
laserdad 0:2da1f69b38fd 122 //INT_PST
laserdad 0:2da1f69b38fd 123 #define LS_PERSIST(x) (x & 0x0F)<<4
laserdad 0:2da1f69b38fd 124 #define PS_PERSIST(x) (x & 0x0F)
laserdad 0:2da1f69b38fd 125 //PPG_PS_GAIN
laserdad 0:2da1f69b38fd 126 #define PPG_PS_GAIN_1 0
laserdad 0:2da1f69b38fd 127 #define PPG_PS_GAIN_1P5 1<<4
laserdad 0:2da1f69b38fd 128 #define PPG_PS_GAIN_2 2<<4
laserdad 0:2da1f69b38fd 129 #define PPG_PS_GAIN_4 3<<4
laserdad 8:6c19c052b053 130 #define PPG_LED_SETTLING(x) (x & 0x03)<<2 // 0=0us, 1 = 5us, 2 = 10us (Default), 3=20us
laserdad 8:6c19c052b053 131 #define PPG_ALC_TRACK(x) (x & 0x03) // 0 = 10us, 1 (20us) DEFAULT ,2 = 30us, 3 = 60us
laserdad 0:2da1f69b38fd 132 //PPG_PS_CFG
laserdad 0:2da1f69b38fd 133 #define PPG_POW_SAVE_ON 1<<6
laserdad 0:2da1f69b38fd 134 #define PPG_POW_SAVE_OFF 0
laserdad 0:2da1f69b38fd 135 #define LED_FLIP_ON 1<<3
laserdad 0:2da1f69b38fd 136 #define LED_FLIP_OFF 0
laserdad 0:2da1f69b38fd 137 //PPG_CAN_ANA
laserdad 0:2da1f69b38fd 138 #define PPG_CH1_CAN(x) (x & 0x03)<<2
laserdad 0:2da1f69b38fd 139 #define PPG_CH2_CAN(x) (x & 0x03)
laserdad 0:2da1f69b38fd 140 //PPG_AVG
laserdad 0:2da1f69b38fd 141 #define PPG_AVG(x) (x & 0x07)<<4
laserdad 0:2da1f69b38fd 142 //PPG_PWIDTH_RATE
laserdad 0:2da1f69b38fd 143 #define PPG_PWIDTH(x) (x & 0x07)<<4
laserdad 0:2da1f69b38fd 144 #define PPG_FREQ_60HZ 0
laserdad 0:2da1f69b38fd 145 #define PPG_FREQ_50HZ 1<<3
laserdad 0:2da1f69b38fd 146 #define PPG_RATE(x) (x & 0x07)
laserdad 0:2da1f69b38fd 147 //FIFO_CFG
laserdad 0:2da1f69b38fd 148 #define FIFO_ROLL_ON 1<<4
laserdad 0:2da1f69b38fd 149 #define FIFO_ROLL_OFF 0
laserdad 0:2da1f69b38fd 150 #define AFULL_SAMPLES_LEFT(x) (x & 0x0F)
laserdad 0:2da1f69b38fd 151
laserdad 0:2da1f69b38fd 152
laserdad 0:2da1f69b38fd 153
laserdad 0:2da1f69b38fd 154
laserdad 0:2da1f69b38fd 155
laserdad 0:2da1f69b38fd 156 #define POR_TIME_MS 5 //a guess
laserdad 0:2da1f69b38fd 157
laserdad 0:2da1f69b38fd 158
laserdad 0:2da1f69b38fd 159
laserdad 0:2da1f69b38fd 160 class OB1203
laserdad 0:2da1f69b38fd 161 {
laserdad 0:2da1f69b38fd 162 I2C *i2c;
laserdad 4:f25ca600a0e6 163 // SoftI2C *i2c;
laserdad 0:2da1f69b38fd 164
laserdad 4:f25ca600a0e6 165 public:
laserdad 8:6c19c052b053 166 char osc_trim;
laserdad 4:f25ca600a0e6 167 char ls_res;
laserdad 4:f25ca600a0e6 168 char ls_rate;
laserdad 4:f25ca600a0e6 169 char ls_gain;
laserdad 4:f25ca600a0e6 170 uint32_t ls_thres_hi;
laserdad 4:f25ca600a0e6 171 uint32_t ls_thres_lo;
laserdad 4:f25ca600a0e6 172 char ls_sai;
laserdad 4:f25ca600a0e6 173 char ls_mode;
laserdad 4:f25ca600a0e6 174 char ls_en;
laserdad 4:f25ca600a0e6 175 char ls_int_sel;
laserdad 4:f25ca600a0e6 176 char ls_var_mode;
laserdad 4:f25ca600a0e6 177 char ps_sai_en;
laserdad 4:f25ca600a0e6 178 char temp_en;
laserdad 4:f25ca600a0e6 179 char ppg_ps_mode;
laserdad 4:f25ca600a0e6 180 char ppg_ps_en;
laserdad 4:f25ca600a0e6 181 char ps_can_ana;
laserdad 4:f25ca600a0e6 182 char afull_int_en;
laserdad 4:f25ca600a0e6 183 char ppg_int_en;
laserdad 4:f25ca600a0e6 184 char ps_logic_mode;
laserdad 4:f25ca600a0e6 185 uint16_t ps_digital_can;
laserdad 4:f25ca600a0e6 186 char ps_int_en;
laserdad 4:f25ca600a0e6 187 char ls_persist;
laserdad 4:f25ca600a0e6 188 char ps_persist;
laserdad 4:f25ca600a0e6 189 uint16_t ps_thres_hi;
laserdad 4:f25ca600a0e6 190 uint16_t ps_thres_lo;
laserdad 4:f25ca600a0e6 191 uint16_t ps_current;
laserdad 4:f25ca600a0e6 192 uint16_t ir_current;
laserdad 4:f25ca600a0e6 193 uint16_t r_current;
laserdad 4:f25ca600a0e6 194 char ppg_ps_gain;
laserdad 4:f25ca600a0e6 195 char ppg_pow_save;
laserdad 4:f25ca600a0e6 196 char led_flip;
laserdad 4:f25ca600a0e6 197 char ch1_can_ana;
laserdad 4:f25ca600a0e6 198 char ch2_can_ana;
laserdad 4:f25ca600a0e6 199 char ppg_avg;
laserdad 4:f25ca600a0e6 200 char ppg_pwidth;
laserdad 4:f25ca600a0e6 201 char ppg_freq;
laserdad 4:f25ca600a0e6 202 char ppg_rate;
laserdad 8:6c19c052b053 203 char ppg_LED_settling;
laserdad 8:6c19c052b053 204 char ppg_ALC_track;
laserdad 4:f25ca600a0e6 205 char ps_pulses;
laserdad 4:f25ca600a0e6 206 char ps_pwidth;
laserdad 4:f25ca600a0e6 207 char ps_rate;
laserdad 4:f25ca600a0e6 208 char ps_avg_en;
laserdad 4:f25ca600a0e6 209 char ps_hys_level;
laserdad 4:f25ca600a0e6 210 char ls_int_en;
laserdad 4:f25ca600a0e6 211 char fifo_rollover_en;
laserdad 4:f25ca600a0e6 212 char fifo_afull_samples_left;
laserdad 4:f25ca600a0e6 213 char writePointer;
laserdad 4:f25ca600a0e6 214 char readPointer;
laserdad 4:f25ca600a0e6 215 char fifoOverflow;
laserdad 4:f25ca600a0e6 216 OB1203 (I2C *);
laserdad 4:f25ca600a0e6 217 // OB1203(SoftI2C *);
laserdad 0:2da1f69b38fd 218
laserdad 4:f25ca600a0e6 219 // Low-level operations
laserdad 4:f25ca600a0e6 220 void reset();
laserdad 4:f25ca600a0e6 221 uint16_t get_status();
laserdad 4:f25ca600a0e6 222 void writeRegister(int, char, char);
laserdad 4:f25ca600a0e6 223 void writeBlock(int, char, char *, char);
laserdad 4:f25ca600a0e6 224 void readBlock(int, char, char *, int);
laserdad 4:f25ca600a0e6 225 uint32_t bytes2uint32(char *, int);
laserdad 6:337ee64ca7ab 226 uint32_t twoandhalfBytes2uint32(char *, int);
laserdad 4:f25ca600a0e6 227
laserdad 4:f25ca600a0e6 228 // High-level operations
laserdad 8:6c19c052b053 229 void setOscTrim();
laserdad 4:f25ca600a0e6 230 bool dataIsNew();
laserdad 6:337ee64ca7ab 231 bool lsIsNew();
laserdad 6:337ee64ca7ab 232 bool psIsNew();
laserdad 6:337ee64ca7ab 233 bool tempIsNew();
laserdad 5:527556d05aa8 234 bool bioIsNew();
laserdad 4:f25ca600a0e6 235 void setMainConfig();
laserdad 4:f25ca600a0e6 236 void setIntConfig();
laserdad 4:f25ca600a0e6 237 void setLSthresh();
laserdad 4:f25ca600a0e6 238 void setPSthresh();
laserdad 4:f25ca600a0e6 239 void setPScurrent();
laserdad 4:f25ca600a0e6 240 void setPPGcurrent();
laserdad 4:f25ca600a0e6 241 void setPPG_PSgain_cfg();
laserdad 4:f25ca600a0e6 242 void setPPGana_can();
laserdad 4:f25ca600a0e6 243 void setDigitalCan();
laserdad 4:f25ca600a0e6 244 void setPPGavg_and_rate();
laserdad 4:f25ca600a0e6 245 void setFifoConfig();
laserdad 4:f25ca600a0e6 246 char get_ps_data(uint32_t *);
laserdad 4:f25ca600a0e6 247 char get_ls_data(uint32_t *);
laserdad 4:f25ca600a0e6 248 char get_ps_ls_data(uint32_t *);
laserdad 4:f25ca600a0e6 249 void init_ps();
laserdad 4:f25ca600a0e6 250 void init_rgb();
laserdad 4:f25ca600a0e6 251 void init_ps_rgb();
laserdad 4:f25ca600a0e6 252 void init_hr();
laserdad 4:f25ca600a0e6 253 void init_spo2();
laserdad 4:f25ca600a0e6 254 void getFifoInfo(char *fifo_info);
laserdad 4:f25ca600a0e6 255 uint8_t getNumFifoSamplesAvailable();
laserdad 4:f25ca600a0e6 256 void getFifoSamples(uint8_t, char *);
laserdad 4:f25ca600a0e6 257 void parseFifoSamples(char, char *, uint32_t *);
laserdad 4:f25ca600a0e6 258 char get_part_ID(char *);
laserdad 4:f25ca600a0e6 259
laserdad 4:f25ca600a0e6 260 //variables
laserdad 4:f25ca600a0e6 261 uint16_t rate;
laserdad 4:f25ca600a0e6 262 char res;
laserdad 4:f25ca600a0e6 263 char gain;
laserdad 4:f25ca600a0e6 264 uint32_t data_max;
laserdad 4:f25ca600a0e6 265 uint32_t reg_max;
laserdad 0:2da1f69b38fd 266
laserdad 0:2da1f69b38fd 267 };
laserdad 0:2da1f69b38fd 268
laserdad 0:2da1f69b38fd 269
laserdad 0:2da1f69b38fd 270 #endif