Example source code for MAXREFDES101.
Dependencies: max32630fthr Adafruit_FeatherOLED USBDevice
Revision 2:58c2d53dea38, committed 2019-04-10
- Comitter:
- gmehmet
- Date:
- Wed Apr 10 16:58:06 2019 +0300
- Parent:
- 1:f60eafbf009a
- Child:
- 3:2fe2ff1ca0dc
- Commit message:
- removed 8614x direc driver
Changed in this revision
--- a/Drivers/MAX8614X/MAX8614X.cpp Wed Apr 10 14:56:25 2019 +0300
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,631 +0,0 @@
-/*******************************************************************************
-* Copyright (C) 2017 Maxim Integrated Products, Inc., All Rights Reserved.
-*
-* Permission is hereby granted, free of charge, to any person obtaining a
-* copy of this software and associated documentation files (the "Software"),
-* to deal in the Software without restriction, including without limitation
-* the rights to use, copy, modify, merge, publish, distribute, sublicense,
-* and/or sell copies of the Software, and to permit persons to whom the
-* Software is furnished to do so, subject to the following conditions:
-*
-* The above copyright notice and this permission notice shall be included
-* in all copies or substantial portions of the Software.
-*
-* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
-* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
-* IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
-* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
-* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
-* OTHER DEALINGS IN THE SOFTWARE.
-*
-* Except as contained in this notice, the name of Maxim Integrated
-* Products, Inc. shall not be used except as stated in the Maxim Integrated
-* Products, Inc. Branding Policy.
-*
-* The mere transfer of this software does not imply any licenses
-* of trade secrets, proprietary technology, copyrights, patents,
-* trademarks, maskwork rights, or any other form of intellectual
-* property whatsoever. Maxim Integrated Products, Inc. retains all
-* ownership rights.
-*******************************************************************************
-*/
-
-#include "MAX8614X.h"
-#include "PpgComm.h"
-#include "Peripherals.h"
-
-#define ARRAY_SIZE(array) (sizeof(array)/sizeof(array[0]))
-
-#define MAX8614X_AGC_DEFAULT_LED_OUT_RANGE 50 //AGC Range parameter
-#define MAX8614X_AGC_INCREMENT_MID 15 //AGC Range parameter MID
-#define MAX8614X_AGC_INCREMENT_TOP 15 //AGC Range parameter TOP
-
-MAX8614X::MAX8614X(SPI &spiBus, DigitalOut &cs, PinName pin)
-:m_ir(pin), m_spiBus(spiBus), m_cs(cs)
-{
- m_cs = 1;
- kMax8614xDefaultLedOutRange = MAX8614X_AGC_DEFAULT_LED_OUT_RANGE;
-
-}
-
-int MAX8614X::readRegister(uint8_t reg, uint8_t *data, int len)
-{
- int i = 0;
-
- m_cs = 0;
-
- m_spiBus.write(reg);
- m_spiBus.write(0x80);
- for(; i < len; i++) { /*TODO: make len unsigned*/
- data[i] = m_spiBus.write(0x00);
- }
-
- m_cs = 1;
-
- return 0; /*TODO: handle error cases*/
-}
-
-int MAX8614X::writeRegister(uint8_t reg, const uint8_t data)
-{
- m_cs = 0;
-
- m_spiBus.write(reg);
- m_spiBus.write(0x00);
- m_spiBus.write(data);
-
- m_cs = 1;
-
- return 0; /*TODO: handle error cases*/
-}
-
-int MAX8614X::writeBlock(const RegisterMap reg_block[], unsigned int size)
-{
- unsigned int i;
- int ret = 0;
-
- for (i = 0; i < size; i++) {
- ret = writeRegister((Registers) reg_block[i].addr,
- reg_block[i].val);
- if (ret < 0) {
- pr_err("writeRegister failed. ret: %d", ret);
- return ret;
- }
- }
-
- return ret;
-}
-
-int MAX8614X::get_part_info(uint8_t *part_id, uint8_t *rev_id)
-{
- uint32_t i;
- int ret;
- uint8_t buf[2];
- const uint8_t max8614x_part_ids[] = {
- MAX86140_PART_ID_VAL,
- MAX86141_PART_ID_VAL,
- MAX86142_PART_ID_VAL,
- MAX86143_PART_ID_VAL,
- };
-
- ret = readRegister(MAX8614X_REV_ID_REG, buf, 2);
- if (ret < 0) {
- pr_err("readRegister failed. ret: %d", ret);
- return ret;
- }
-
- for (i = 0; i < ARRAY_SIZE(max8614x_part_ids); i++) {
- if (buf[1] == max8614x_part_ids[i]) {
- *rev_id = buf[0];
- *part_id = buf[1];
- return 0;
- }
- }
-
- // Unsupported part
- return -1;
-}
-
-int MAX8614X::get_num_samples_in_fifo()
-{
- int fifo_ovf_cnt;
- uint8_t fifo_data[4];
- uint32_t num_samples;
- int ret;
-
- ret = readRegister(MAX8614X_OVF_CNT_REG, fifo_data, 2);
- if (ret < 0) {
- pr_err("readRegister failed. ret: %d", ret);
- return ret;
- }
-
- fifo_ovf_cnt = fifo_data[0] & MAX8614X_OVF_CNT_MASK;
- num_samples = fifo_data[1];
-
- if (num_samples >= MAX8614X_MAX_FIFO_DEPTH) {
- pr_err("# FIFO is Full. OVF: %d num_samples: %lu",
- fifo_ovf_cnt, num_samples);
- }
-
- return num_samples;
-}
-
-int MAX8614X::read_fifo_data(uint8_t *fifo_data, int num_samples)
-{
- uint16_t num_bytes = num_samples * MAX8614X_DATA_WORD_SIZE;
- int ret = 0;
-
-// const struct RegisterMap test_clk_enable[] = {
-// {0xFF, 0x54},
-// {0xFF, 0x4D},
-// {0x81, 0x20},
-// {0xFF, 0x00}
-// };
-//
-// const struct RegisterMap test_clk_disable[] = {
-// {0xFF, 0x54},
-// {0xFF, 0x4D},
-// {0x81, 0x00},
-// {0xFF, 0x00}
-// };
-
-// ret = writeBlock(test_clk_enable, ARRAY_SIZE(test_clk_enable));
-// if (ret < 0) {
-// pr_err("writeBlock failed. ret: %d", ret);
-// return ret;
-// }
-
- fifo_data[0] = MAX8614X_FIFO_DATA_REG;
- ret = readRegister(MAX8614X_FIFO_DATA_REG, fifo_data, num_bytes);
- if (ret < 0) {
- pr_err("readRegister failed. ret: %d", ret);
- return ret;
- }
-
-// ret = writeBlock(test_clk_disable, ARRAY_SIZE(test_clk_disable));
-// if (ret < 0) {
-// pr_err("writeBlock failed. ret: %d", ret);
-// return ret;
-// }
-
- return ret;
-}
-
-void MAX8614X::irq()
-{
- irq_triggered = true;
-}
-
-int MAX8614X::irq_handler()
-{
- int ret;
- int_status_t status;
- irq_triggered = false;
-
- ret = readRegister(MAX8614X_INT_STATUS1_REG, status.val, 2);
- if (ret < 0) {
- pr_err("readRegister failed. ret: %d", ret);
- return -1;
- }
- pr_debug("Status reg: %X %X", status.val[0], status.val[1]);
-
- if (status.a_full || status.data_rdy) {
- fifo_irq_handler();
- }
-
- if (status.die_temp_rdy) {
- pr_debug("Pwr_rdy interrupt was triggered.");
- }
-
- if (status.pwr_rdy) {
- pr_info("Pwr_rdy interrupt was triggered.");
- }
-
- if (status.die_temp_rdy) {
- read_die_temp();
- }
-
- if (status.vdd_oor) {
- vdd_oor_cnt++;
- pr_info("VDD Out of range cnt: %d", vdd_oor_cnt);
- }
- return 0;
-}
-
-int MAX8614X::fifo_irq_handler()
-{
- uint8_t fifo_buf[MAX8614X_MAX_FIFO_DEPTH * MAX8614X_DATA_WORD_SIZE] = {0};
- int ret;
- int num_samples = 0;
- static int last_samples[DATA_TYPE_PPG2_LEDC4];
- fifo_data_t fifo_data;
- uint16_t idx;
- int i;
- static ppg_data_t ppg_data;
- static uint32_t ppg_data_ack = 0;
- const uint32_t ir_green_red_ppg_type = ((1 << DATA_TYPE_PPG1_LEDC1) |
- (1 << DATA_TYPE_PPG1_LEDC2) |
- (1 << DATA_TYPE_PPG1_LEDC3));
-
- num_samples = get_num_samples_in_fifo();
- if (num_samples <= 0) {
- pr_err("get_num_samples_in_fifo failed. err: %d", num_samples);
- return -1;
- }
-
- pr_debug("num_samples: %d", num_samples);
-
- ret = read_fifo_data(fifo_buf, num_samples);
- if (ret < 0) {
- pr_err("read_fifo_data failed. ret: %d", ret);
- return -1;
- }
-
- for (i = 0; i < num_samples; i++) {
- idx = MAX8614X_DATA_WORD_SIZE * i;
- fifo_data.raw = fifo_buf[idx + 0] << 16
- | fifo_buf[idx + 1] << 8
- | fifo_buf[idx + 2];
- if (fifo_data.type == DATA_TYPE_INVALID_DATA || fifo_data.type == 0) {
- pr_err("Received invalid data. data: %X, i: %d",
- (unsigned int)fifo_data.raw, i);
-
- continue;
- }
- pr_debug("\ttype: %lu, val: %lu (%lX)",
- fifo_data.type, fifo_data.val, fifo_data.val);
-
- if (fifo_data.type > DATA_TYPE_PPG2_LEDC3) {
- pr_err("Wrong Data Type: -> Type: %lu, val:%lu, Raw:%lu",
- fifo_data.type, fifo_data.val, fifo_data.raw);
- continue;
- }
-
- if (fifo_data.type > 0 && fifo_data.type < DATA_TYPE_PPG2_LEDC3) {
- last_samples[fifo_data.type] = fifo_data.val;
- }
-
- if (fifo_data.type == DATA_TYPE_PPG1_LEDC1) {
- max8614x_agc_handler(&led_ctrl, last_samples);
- }
-
- if (fifo_data.type == DATA_TYPE_PPG1_LEDC1) {
- ppg_data.ir = fifo_data.val;
- } else if (fifo_data.type == DATA_TYPE_PPG1_LEDC2) {
- ppg_data.red = fifo_data.val;
- } else if (fifo_data.type == DATA_TYPE_PPG1_LEDC3) {
- ppg_data.green = fifo_data.val;
- }
-
- ppg_data_ack |= 1 << fifo_data.type;
-
- if ((ppg_data_ack & ir_green_red_ppg_type) != ir_green_red_ppg_type ){
- continue;
- }
- ppg_data_ack = 0;
-
- if (ppg_queue.size() >= MAX8614X_COMMON_QUEUE_SIZE) {
- pr_err("ppg_queue is full.");
- } else {
- ppg_queue.push(ppg_data);
- }
- }
- return 0;
-}
-
-int MAX8614X::get_sensor_report(ppg_sensor_report &sensor_report)
-{
- int32_t ret;
- uint32_t irleddata, redleddata, greenleddata;
- //TODO: Implement
- ret = dequeue_from_fifo_queue(&irleddata, &redleddata, &greenleddata);
- if(ret == RTN_NO_ERROR){
- sensor_report.grn = irleddata;
- sensor_report.grn2 = redleddata;
- }else{
- return -1;
- }
- return 0;
-}
-
-int MAX8614X::enable_die_temp()
-{
- int ret = 0;
-
- ret = writeRegister(MAX8614X_DIE_TEMP_CFG_REG, MAX8614X_DIE_TEMP_EN);
- if (ret < 0) {
- pr_err("SPI Communication error");
- }
-
- return ret;
-}
-
-int MAX8614X::read_die_temp()
-{
- int ret = 0;
- uint8_t buf[2];
-
- ret = readRegister(MAX8614X_DIE_TEMP_INT_REG, buf, 2);
- if (ret < 0) {
- pr_err("Unable to read die_temp. ret: %d", ret);
- return ret;
- }
-
- die_temp.frac = (uint8_t)buf[1] & MAX8614X_DIE_TEMP_FRAC_MASK;
- die_temp.tint = (uint8_t)buf[0];
-
- pr_debug("Die temp: %d - %d, %d", die_temp.val, buf[0], buf[1]);
- return enable_die_temp();
-}
-
-int MAX8614X::reset()
-{
- int ret = 0;
-
- ret = writeRegister(MAX8614X_SYSTEM_CTRL_REG,
- MAX8614X_SYSTEM_RESET_MASK);
- if (ret < 0) {
- pr_err("writeRegister failed. ret: %d", ret);
- return ret;
- }
-
- /* Reset ppg_queue */
- std::queue<ppg_data_t> empty_queue;
- std::swap(ppg_queue, empty_queue);
-
- led_control_reset(&led_ctrl);
-
- return ret;
-}
-
-int MAX8614X::poweroff()
-{
- int ret = 0;
-
- ret = writeRegister(MAX8614X_SYSTEM_CTRL_REG,
- MAX8614X_SYSTEM_SHDN_MASK);
- if (ret < 0) {
- pr_err("writeRegister failed. ret: %d", ret);
- return ret;
- }
-
- return ret;
-}
-
-int MAX8614X::init()
-{
- int ret = RTN_NO_ERROR;
- uint8_t part_id, rev_id;
-
- ret = get_part_info(&part_id, &rev_id);
- if (ret < 0) {
- pr_err("MAX8614X is not detected. Part_id: 0x%X, Rev_Id: 0x%X",
- part_id, rev_id);
- ret = RTN_ERR_NOT_8614x;
- goto fail;
- }
- pr_info("MAX8614X detected. Part_id: 0x%X, Rev_Id: 0x%X", part_id, rev_id);
-
- ret = writeRegister(MAX8614X_SYSTEM_CTRL_REG,
- MAX8614X_SYSTEM_RESET_MASK);
- if (ret < 0) {
- pr_err("writeRegister failed. ret: %d", ret);
- goto fail;
- }
-
- die_temp.frac = 0;
- die_temp.tint = 0;
-
- led_control_init(&led_ctrl); /*TODO: after porting agc, test */
-
- irq_triggered = false;
- m_ir.fall(Callback<void()>(this, &MAX8614X::irq));
-
- return ret;
-fail:
- pr_err("Init failed. ret: %d", ret);
- return ret;
-}
-
-int MAX8614X::sensor_enable(int enable)
-{
- int ret = RTN_NO_ERROR;
- uint8_t led_seq[3];
-
- RegisterMap_t ppg_init_cfg[] = {
- { MAX8614X_SYSTEM_CTRL_REG, MAX8614X_SYSTEM_RESET_MASK},
- { MAX8614X_PPG_CFG1_REG, MAX8614X_PPG_LED_PW_115_2_US_MASK // PPG_LED_PW = 3 (115.2us)
- | MAX8614X_PPG1_ADC_RGE_32768_MASK // PPG1_ADC_RGE = 3(32768nA)
- | MAX8614X_PPG2_ADC_RGE_32768_MASK }, // PPG2_ADC_RGE = 3(32768nA)
- { MAX8614X_PPG_CFG2_REG, MAX8614X_PPG_SR_25_SPS},
- { MAX8614X_LED_SEQ1_REG, 0x24},
- { MAX8614X_LED_SEQ2_REG, 0x03},
- { MAX8614X_LED1_PA_REG, 0x80},
- { MAX8614X_LED2_PA_REG, 0x80},
- { MAX8614X_LED3_PA_REG, 0x00},
- { MAX8614X_LED_RANGE1_REG, MAX8614X_LED1_RGE_25mA_MASK
- | MAX8614X_LED2_RGE_25mA_MASK
- | MAX8614X_LED3_RGE_25mA_MASK},
- { MAX8614X_FIFO_CFG1_REG, MAX8614X_INT1_EN_DATA_RDY_MASK},
- // 24 (ir+r+g) samples in fifo, size is 128/3=42
- { MAX8614X_FIFO_CFG2_REG, MAX8614X_FLUSH_FIFO_MASK
- | MAX8614X_FIFO_STAT_CLR_MASK
- | MAX8614X_A_FULL_TYPE_MASK // Try 0
- | MAX8614X_FIFO_RO_MASK
- | MAX8614X_FIFO_EN_MASK},
- { MAX8614X_INT_ENABLE1_REG, MAX8614X_INT1_EN_A_FULL_MASK
- | MAX8614X_INT1_EN_DIE_TEMP_MASK
- | MAX8614X_INT1_EN_VDD_OOR_MASK},
- };
-
- if (enable) {
- /* Read current sequence settings, and check what modes are open */
- led_seq[0] = MAX8614X_LED_SEQ1_REG;
- ret |= readRegister(MAX8614X_LED_SEQ1_REG, led_seq, 3);
- if (ret < 0) {
- pr_err("readRegister failed. ret: %d", ret);
- return ret;
- }
-
- pr_debug("0-Sequence registers: %X %X %X", led_seq[0], led_seq[1], led_seq[2]);
- ret |= writeBlock(ppg_init_cfg, ARRAY_SIZE(ppg_init_cfg));
- led_seq[0] = MAX8614X_INT_STATUS1_REG;
- ret |= readRegister(MAX8614X_INT_STATUS1_REG, led_seq, 3); // Mert: the len was 3 ?!
- pr_debug("1-Sequence registers: %X %X %X", led_seq[0], led_seq[1], led_seq[2]);
- if (ret < 0) {
- pr_err("readRegister failed. ret: %d", ret);
- return ret;
- }
- agc_enable(1);
- pr_debug("Wrote register settings. ret: %d", ret);
- } else {
- ret = reset();
- if (ret < 0) {
- pr_err("reset failed. ret: %d", ret);
- return ret;
- }
-
- ret = poweroff();
- if (ret < 0) {
- pr_err("poweroff failed. ret: %d", ret);
- return ret;
- }
- }
-
- return RTN_NO_ERROR;
-}
-
-int MAX8614X::agc_enable(int agc_enable)
-{
- int ret = RTN_NO_ERROR;
- led_ctrl.agc_is_enabled = !!agc_enable;
- led_ctrl.lpm_is_enabled = !!agc_enable;
- ret = led_prox_init(&led_ctrl, led_ctrl.lpm_is_enabled);
- return ret;
-}
-
-int MAX8614X::dequeue_from_fifo_queue(uint32_t *ir, uint32_t *red, uint32_t *green)
-{
- ppg_data_t ppg_data;
-
- if (irq_triggered) {
- irq_handler();
- }
-
- if (ppg_queue.size() <= 0) {
- return RTN_ERR_QUEUE_EMPTY;
- }
- ppg_data = ppg_queue.front();
- ppg_queue.pop();
-
- pr_debug("%lu %lu %lu", ppg_data.ir, ppg_data.red, ppg_data.green);
-
- *ir = ppg_data.ir;
- *red = ppg_data.red;
- *green = ppg_data.green;
-
- return RTN_NO_ERROR;
-}
-// total number of registers to print are 57
-int MAX8614X::dump_registers(addr_val_pair *reg_vals)
-{
- int i, j = 0;
- int ret = 0;
- uint8_t val;
- // read 23 registers
- for (i = 0x00; i <= 0x16; i++, j++) {
- reg_vals[j].addr = i;
- ret |= readRegister(i, &val, 1);
- reg_vals[j].val = val;
- }
- // read 24 registers
- for (i = 0x20; i <= 0x37; i++, j++) {
- reg_vals[j].addr = i;
- ret |= readRegister(i, &val, 1);
- reg_vals[j].val = val;
- }
- // read 3 registers
- for (i = 0x40; i <= 0x42; i++, j++) {
- reg_vals[j].addr = i;
- ret |= readRegister(i, &val, 1);
- reg_vals[j].val = val;
- }
- // read 5 registers
- for (i = 0xF0; i <= 0xF4; i++, j++) {
- reg_vals[j].addr = i;
- ret |= readRegister(i, &val, 1);
- reg_vals[j].val = val;
- }
- // read 2 registers
- for (i = 0xFE; i <= 0xFF; i++, j++) {
- reg_vals[j].addr = i;
- ret |= readRegister(i, &val, 1);
- reg_vals[j].val = val;
- }
- return ret;
-}
-
-const char * MAX8614X::get_sensor_part_name()
-{
-#if 1
- /* For sensor studio */
- return "max86141";
-#else
- uint8_t part_id, rev_id;
-
- get_part_info(&part_id, &rev_id);
-
- switch (part_id) {
- case MAX86140_PART_ID_VAL:
- return "max86140";
- case MAX86141_PART_ID_VAL:
- return "max86141";
- case MAX86142_PART_ID_VAL:
- return "max86142";
- case MAX86143_PART_ID_VAL:
- return "max86143";
- }
- return "unknown";
-#endif
-}
-
-const char * MAX8614X::get_sensor_name()
-{
- return "ppg";
-}
-
-const char * MAX8614X::get_sensor_algo_ver()
-{
- return "dummy_algo_ver";
-}
-
-
-//@brief Get status of the sensor: if PROX mode return true
-unsigned char MAX8614X::is_sensor_in_daq_mode(){
- return (led_ctrl.state == LED_DATA_ACQ);
-}
-
-//@brief set agc range
-void MAX8614X::set_agc_range(enum Max8614x_Agc_Range RangeLevel){
-
- if(RangeLevel == kRangeHigh)
- kMax8614xDefaultLedOutRange = MAX8614X_AGC_DEFAULT_LED_OUT_RANGE + MAX8614X_AGC_INCREMENT_TOP;
- else if(RangeLevel == kRangeMid)
- kMax8614xDefaultLedOutRange = MAX8614X_AGC_DEFAULT_LED_OUT_RANGE + MAX8614X_AGC_INCREMENT_MID;
- else
- kMax8614xDefaultLedOutRange= MAX8614X_AGC_DEFAULT_LED_OUT_RANGE;
-
-// printf("Set agc range as: %d\r\n", kMax8614xDefaultLedOutRange);
-}
-
-
-/**
-* @brief Get sensor ID.
-*
-* @returns Sensor ID number.
-*/
-unsigned char MAX8614X::get_sensor_id() {
-
- return( SENSOR_ID_MAX30205 );
-
-}
--- a/Drivers/MAX8614X/MAX8614X.h Wed Apr 10 14:56:25 2019 +0300
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,587 +0,0 @@
-/*******************************************************************************
-* Copyright (C) 2017 Maxim Integrated Products, Inc., All Rights Reserved.
-*
-* Permission is hereby granted, free of charge, to any person obtaining a
-* copy of this software and associated documentation files (the "Software"),
-* to deal in the Software without restriction, including without limitation
-* the rights to use, copy, modify, merge, publish, distribute, sublicense,
-* and/or sell copies of the Software, and to permit persons to whom the
-* Software is furnished to do so, subject to the following conditions:
-*
-* The above copyright notice and this permission notice shall be included
-* in all copies or substantial portions of the Software.
-*
-* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
-* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
-* IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
-* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
-* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
-* OTHER DEALINGS IN THE SOFTWARE.
-*
-* Except as contained in this notice, the name of Maxim Integrated
-* Products, Inc. shall not be used except as stated in the Maxim Integrated
-* Products, Inc. Branding Policy.
-*
-* The mere transfer of this software does not imply any licenses
-* of trade secrets, proprietary technology, copyrights, patents,
-* trademarks, maskwork rights, or any other form of intellectual
-* property whatsoever. Maxim Integrated Products, Inc. retains all
-* ownership rights.
-*******************************************************************************
-*/
-
-#ifndef _MAX8614X_H_
-#define _MAX8614X_H_
-
-#include "mbed.h"
-#include <queue>
-#include "MaximSensor.h"
-#include "PpgComm.h"
-
-#define SENSOR_ID_MAX8614X 0x02
-
-/**
- * @brief MAX8614X - supports MAX8614X object with SPI interface
- * @details The MAX8614X is a complete integrated optical data acquistion
- * system, ideal for optical pulse oximetry and heart rate detection
- * applications. High resolution optical readout signal processing channels
- * with ambient light cancellation and high current LED driver DACS form a
- * complete optical readout signal chain. Features are ultra-low power heart
- * rate monitoring, SpO2 monitoring, SmO2 & StO2 detection.
- * Typical applications are for wearable devices, fitness devices, clinical
- * monitoring, partical detection.
- */
-class MAX8614X: public MaximSensor
-{
-public:
- /* PUBLIC CONST VARIABLES */
-
- ///Return value on success.
- static const int8_t RTN_NO_ERROR = 0;
- ///Return value on unsupported MAX8614x.
- static const int8_t RTN_ERR_UNSOPPORTED_PART = -1;
- ///Return value on reading wrong-type data from sensor FIFO
- static const int8_t RTN_ERR_WRONG_DATA_TYPE = -2;
- ///Return value on no plugged 8614x.
- static const int8_t RTN_ERR_NOT_8614x = -3;
- ///Return value on memory allocation failure
- static const int8_t RTN_ERR_MEM_ALLOC_FAIL = -4;
- ///Return value on internal queue init
- static const int8_t RTN_ERR_QUEUE_INIT = -5;
- ///Return value on the case of no available data on queue
- static const int8_t RTN_ERR_QUEUE_EMPTY = -6;
-
- // AGC Range Enums:
- enum Max8614x_Agc_Range{
- kRangeLow,
- kRangeMid,
- kRangeHigh,
- };
-
- /// This is MAX8614X Registers
- enum Registers
- {
- MAX8614X_INT_STATUS1_REG = 0x00,///< INT status REG is cleared when it is read
- MAX8614X_INT_STATUS2_REG, ///< INT status set to 1 when the Authentication Algorithm completes
- MAX8614X_INT_ENABLE1_REG, ///< Enables INT for FIFO Almost Full or DATA_RDY_EN or ALC_OVF ...
- MAX8614X_INT_ENABLE2_REG, ///< Enables SHA_DONE interrupt
- MAX8614X_FIFO_WR_PTR_REG, ///< Points to the location where the next sample will be written
- MAX8614X_FIFO_RD_PTR_REG, ///< Points the location from where the processor gets the next sample from the FIFO
- MAX8614X_OVF_CNT_REG, ///< FIFO Overflow Counter
- MAX8614X_FIFO_DATA_CNT_REG, ///< Holds the number of items available in the FIFO for the host to read
- MAX8614X_FIFO_DATA_REG, ///< FIFO_DATA
- MAX8614X_FIFO_CFG1_REG, ///< Sets how many new samples can be written to the FIFO before the interrupt
- MAX8614X_FIFO_CFG2_REG, ///< //0x0A
- MAX8614X_SYSTEM_CTRL_REG = 0x0D, ///< System Control REG
- MAX8614X_PPG_SYNC_CTRL_REG = 0x10, ///< Aborts current sample and starts a new sample
- MAX8614X_PPG_CFG1_REG, ///< PPG config for adc range control, led_pw etc.
- MAX8614X_PPG_CFG2_REG, ///< PPG config for sampling rate
- MAX8614X_PPG_CFG3_REG, ///< PPG config for burst rate
- MAX8614X_PROX_INT_THRES_REG, ///< Proximity Mode Interrupt Threshold
- MAX8614X_PHOTO_DIODE_BIAS_REG, ///<
- MAX8614X_FICKET_FENCE_REG, //0x16
- MAX8614X_LED_SEQ1_REG = 0x20, ///< LED Sequence Register 1
- MAX8614X_LED_SEQ2_REG,///< LED Sequence Register 2
- MAX8614X_LED_SEQ3_REG, ///< LED Sequence Register 3
- MAX8614X_LED1_PA_REG, ///< LED 1 Drive Current
- MAX8614X_LED2_PA_REG, ///< LED 2 Drive Current
- MAX8614X_LED3_PA_REG, ///< LED 3 Drive Current
- MAX8614X_LED4_PA_REG, ///< LED 4 Drive Current
- MAX8614X_LED5_PA_REG, ///< LED 5 Drive Current
- MAX8614X_LED6_PA_REG, ///< LED 6 Drive Current
- MAX8614X_LED_PILOT_PA_REG, ///< Proximity Mode LED Pulse Amplitude
- MAX8614X_LED_RANGE1_REG, ///< LED range 1
- MAX8614X_LED_RANGE2_REG, ///< LEd range 2 //0x2B
- MAX8614X_DIE_TEMP_CFG_REG = 0x40,///< Enables one temperature measurement
- MAX8614X_DIE_TEMP_INT_REG, ///< Stores the integer temperature data
- MAX8614X_DIE_TEMP_FRAC_REG,///<
- MAX8614X_SHA_CMD_REG = 0xF0, ///<
- MAX8614X_SHA_CFG_REG, ///<
- MAX8614X_MEMORY_CONTROL_REG, ///<
- MAX8614X_MEMORY_INDEX_REG, ///<
- MAX8614X_MEMORY_DATA_REG, ///<
- MAX8614X_REV_ID_REG = 0xFE, ///< Chip Rev. ID Code
- MAX8614X_PART_ID_REG, ///< Chip Part ID Code
-
- };
-
- ///System Control
- static const uint8_t MAX8614X_SYSTEM_RESET_MASK = (0x01 << 0);
- static const uint8_t MAX8614X_SYSTEM_SHDN_MASK = (0x01 << 1);
- static const uint8_t MAX8614X_SYSTEM_LP_BOOST = (0x01 << 2);
-
- /// Die Temperature
- static const uint8_t MAX8614X_DIE_TEMP_EN = (0x1 << 0);
- static const uint8_t MAX8614X_DIE_TEMP_FRAC_MASK = (0xF << 0);
-
- ///Sample rates
- static const uint8_t MAX8614X_PPG_SR_25_SPS = (0x00 << 3);
- static const uint8_t MAX8614X_PPG_SR_50_SPS = (0x01 << 3);
- static const uint8_t MAX8614X_PPG_SR_84_SPS = (0x02 << 3);
- static const uint8_t MAX8614X_PPG_SR_100_SPS = (0x03 << 3);
- static const uint8_t MAX8614X_PPG_SR_200_SPS = (0x04 << 3);
- static const uint8_t MAX8614X_PPG_SR_400_SPS = (0x05 << 3);
- static const uint8_t MAX8614X_PPG_SR_512_SPS = (0x10 << 3);
- static const uint8_t MAX8614X_PPG_SR_1024_SPS = (0x11 << 3);
- static const uint8_t MAX8614X_PPG_SR_2048_SPS = (0x12 << 3);
- static const uint8_t MAX8614X_PPG_SR_4096_SPS = (0x13 << 3);
-
- /// LED Pulse Amplitude
- static const uint8_t MAX8614X_LED1_RGE_MASK = (0x3 << 0);
- static const uint8_t MAX8614X_LED2_RGE_MASK = (0x3 << 2);
- static const uint8_t MAX8614X_LED3_RGE_MASK = (0x3 << 4);
-
- static const uint8_t MAX8614X_LED4_RGE_MASK = (0x3 << 0);
- static const uint8_t MAX8614X_LED5_RGE_MASK = (0x3 << 2);
- static const uint8_t MAX8614X_LED6_RGE_MASK = (0x3 << 4);
-
- static const uint8_t MAX8614X_LED1_RGE_25mA_MASK = (0x0 << 0);
- static const uint8_t MAX8614X_LED1_RGE_50mA_MASK = (0x1 << 0);
- static const uint8_t MAX8614X_LED1_RGE_75mA_MASK = (0x2 << 0);
- static const uint8_t MAX8614X_LED1_RGE_100mA_MASK = (0x3 << 0);
-
- static const uint8_t MAX8614X_LED2_RGE_25mA_MASK = (0x0 << 2);
- static const uint8_t MAX8614X_LED2_RGE_50mA_MASK = (0x1 << 2);
- static const uint8_t MAX8614X_LED2_RGE_75mA_MASK = (0x2 << 2);
- static const uint8_t MAX8614X_LED2_RGE_100mA_MASK = (0x3 << 2);
-
- static const uint8_t MAX8614X_LED3_RGE_25mA_MASK = (0x0 << 4);
- static const uint8_t MAX8614X_LED3_RGE_50mA_MASK = (0x1 << 4);
- static const uint8_t MAX8614X_LED3_RGE_75mA_MASK = (0x2 << 4);
- static const uint8_t MAX8614X_LED3_RGE_100mA_MASK = (0x3 << 4);
-
- /// PPG PW
- static const uint8_t MAX8614X_PPG_LED_PW_14_4_US_MASK = (0 << 0);
- static const uint8_t MAX8614X_PPG_LED_PW_28_8_US_MASK = (1 << 0);
- static const uint8_t MAX8614X_PPG_LED_PW_57_6_US_MASK = (2 << 0);
- static const uint8_t MAX8614X_PPG_LED_PW_115_2_US_MASK = (3 << 0);
-
- /// PPG Range
- static const uint8_t MAX8614X_PPG1_ADC_RGE_4096_MASK = (0 << 2);
- static const uint8_t MAX8614X_PPG1_ADC_RGE_8192_MASK = (1 << 2);
- static const uint8_t MAX8614X_PPG1_ADC_RGE_16384_MASK = (2 << 2);
- static const uint8_t MAX8614X_PPG1_ADC_RGE_32768_MASK = (3 << 2);
-
- static const uint8_t MAX8614X_PPG2_ADC_RGE_4096_MASK = (0 << 4);
- static const uint8_t MAX8614X_PPG2_ADC_RGE_8192_MASK = (1 << 4);
- static const uint8_t MAX8614X_PPG2_ADC_RGE_16384_MASK = (2 << 4);
- static const uint8_t MAX8614X_PPG2_ADC_RGE_32768_MASK = (3 << 4);
-
- ///FIFO
- static const uint8_t MAX8614X_FIFO_WR_PTR_MASK = (0x7F << 0);
- static const uint8_t MAX8614X_FIFO_RD_PTR_MASK = (0x7F << 0);
- static const uint8_t MAX8614X_OVF_CNT_MASK = (0x7F << 0);
- static const uint8_t MAX8614X_FIFO_A_FULL_MASK = (0x7F << 0);
- static const uint8_t MAX8614X_FIFO_EN_MASK = (0x01 << 0);
- static const uint8_t MAX8614X_FIFO_RO_MASK = (0x01 << 1);
- static const uint8_t MAX8614X_A_FULL_TYPE_MASK = (0x01 << 2);
- static const uint8_t MAX8614X_A_FULL_RPT = (0x00 << 2);
- static const uint8_t MAX8614X_A_FULL_ONCE = (0x01 << 2);
- static const uint8_t MAX8614X_FIFO_STAT_CLR_MASK = (0x01 << 3);
- static const uint8_t MAX8614X_FLUSH_FIFO_MASK = (0x01 << 4);
-
- /// Status
- static const uint8_t MAX8614X_INT1_EN_A_FULL_MASK = (0x1 << 7);
- static const uint8_t MAX8614X_INT1_EN_DATA_RDY_MASK = (0x1 << 6);
- static const uint8_t MAX8614X_INT1_EN_DIE_TEMP_MASK = (0x1 << 2);
- static const uint8_t MAX8614X_INT1_EN_VDD_OOR_MASK = (0x1 << 1);
-
- /* PUBLIC TYPE DEFINITIONS */
-
- /// RegisterMap entry to keep a register addr and its value
- typedef struct RegisterMap {
- uint8_t addr; ///< Register addr
- uint8_t val; ///< Value that will be written to this register
- } RegisterMap_t;
-
- /* PUBLIC FUNCTION DECLARATIONS */
- ///@brief MAX8614X Constructor.\n
- ///
- ///On Entry:
- ///@param[in] spiBus - reference to SPI bus for this device
- ///@param[in] cs - reference to chip select of SPI bus for this device
- ///@param[in] intPin - pin name to data ready INT for this device
- ///On Exit:
- ///
- ///@returns none
- MAX8614X(SPI &spiBus, DigitalOut &cs, PinName intPin);
-
- ///@brief Reads from register.\n
- ///
- ///On Entry:
- ///@param[in] data - pointer to memory for storing read data
- ///@param[in] len - len to read data
- ///
- ///On Exit:
- ///@param[out] data - holds contents of read register on success
- ///
- ///@returns 0 on success, non 0 on failure
- virtual int readRegister(uint8_t reg, uint8_t *data, int len);
-
- ///@brief Writes a single register.\n
- ///
- ///On Entry:
- ///@param[in] data - data to write to register
- ///
- ///@returns 0 on success, non 0 on failure
- virtual int writeRegister(uint8_t reg, const uint8_t data);
-
- ///@brief Writes a block of registers\n
- ///On Entry:
- ///@param[in] reg_block - block of registers and values written to them
- ///@param[in] data - pointer to data to write to registers
- ///
- ///On Exit:
- ///@param[out] none
- ///
- ///@returns 0 on success, non 0 on failure
- virtual int writeBlock(const RegisterMap reg_block[], unsigned int size);
-
- ///@brief Dequeue ppg sample from the queue\n
- ///
- ///On Entry:
- ///@param[in] ir - ref to int value
- ///@param[in] red - ref to int value
- ///@param[in] green - ref to int value
- ///
- ///On Exit:
- ///@param[out] ir - ref to int value
- ///@param[out] red - ref to int value
- ///@param[out] green - ref to int value
- ///
- ///@returns 0 on success, non 0 on no available sample on queue
- virtual int dequeue_from_fifo_queue(uint32_t *ir, uint32_t *red, uint32_t *green);
-
- ///@brief Get Sensor Part ID and Rev ID\n
- ///On Entry:
- ///@param[in] part_id - ref to pre-allocated str
- ///@param[in] rev_id - ref to pre-allocated str
- ///
- ///On Exit:
- ///@param[out] part_id - ref to part ID
- ///@param[out] rev_id - ref to rev. ID
- ///
- ///@returns 0 on success, non 0 on failure
- virtual int get_part_info(uint8_t *part_id, uint8_t *rev_id);
-
- ///@brief Initialize internal queue, counters etc.
- ///@returns 0 on success, non 0 on failure
- int init();
-
- ///@brief Get hard-coded part name\n
- ///@returns max86141
- virtual const char *get_sensor_part_name();
-
- ///@brief Get hard-coded sensor type\n
- ///@returns ppg
- virtual const char *get_sensor_name();
-
-
- ///@brief Get hard-coded sensor algo_ver\n
- ///@returns algo_version
- virtual const char *get_sensor_algo_ver();
-
- ///@brief Enable/disable sensor to generate PPG samples \n
- ///@returns 0 on success, non 0 on failure
- virtual int sensor_enable(int enable);
-
- ///@brief Enable/disable Automatic Gain Control \n
- ///
- ///On Entry:
- ///@param[in] enable - if it is 1, enable; otherwise disable
- ///
- ///On Exit:
- ///@returns 0 on success, non 0 on failure
- virtual int agc_enable(int agc_enable);
-
- ///@brief Create list of register address/pair values
- ///@returns 0 on success, non 0 on failure
- virtual int dump_registers(addr_val_pair *reg_vals);
-
- ///@brief Fill out ppg_sensor_report struct
- ///@param[in] sensor_report report to fill out
- ///@returns - on success, non 0 on failure
- virtual int get_sensor_report(ppg_sensor_report &data_report);
-
- //@brief Get status of the sensor: if PROX mode return true
- unsigned char is_sensor_in_daq_mode();
-
- //@brief set agc range
- void set_agc_range(enum Max8614x_Agc_Range RangeLevel);
-
- /**
- * @brief Get sensor ID.
- *
- * @returns Sensor ID number.
- */
- unsigned char get_sensor_id();
-
-
- /* PUBLIC VARIABLES */
- /// Interrupt object to handle DATA_RDY raise-up\n
- InterruptIn m_ir;
-
- /*! Sensor output data to report */
- typedef struct {
- uint32_t sample_cnt;
- uint32_t ir;
- uint32_t red;
- uint32_t green;
- float x;
- float y;
- float z;
- float hr;
- float spo2;
- float resp_rate;
- float hrv;
- } sensor_report;
-
-private:
- /* PRIVATE CONST VARIABLES */
- static const uint32_t MAX8614X_DATA_WORD_SIZE = 3;
- static const uint32_t MAX8614X_MAX_FIFO_DEPTH = 128;
- static const uint32_t MAX8614X_DATA_TYPE_MASK = (0x1F << 19);
- static const uint32_t MAX8614X_COMMON_QUEUE_SIZE = 200;
-
- /* PRIVATE TYPE DEFINITIONS */
- enum LedSequence
- {
- DATA_SEQ_NONE,
- DATA_SEQ_LED1,
- DATA_SEQ_LED2,
- DATA_SEQ_LED3,
- DATA_SEQ_LED1_LED2,
- DATA_SEQ_LED1_LED3,
- DATA_SEQ_LED2_LED3,
- DATA_SEQ_LED1_LED2_LED3,
- DATA_SEQ_PILOT_LED1,
- DATA_SEQ_AMBIENT,
- DATA_SEQ_LED4_EXT_MUX,
- DATA_SEQ_LED5_EXT_MUX,
- DATA_SEQ_LED6_EXT_MUX,
- };
-
- enum DataTypes
- {
- DATA_TYPE_PPG1_LEDC1 = 0x01,
- DATA_TYPE_PPG1_LEDC2,
- DATA_TYPE_PPG1_LEDC3,
- DATA_TYPE_PPG1_LEDC4,
- DATA_TYPE_PPG1_LEDC5,
- DATA_TYPE_PPG1_LEDC6,
- DATA_TYPE_PPG2_LEDC1,
- DATA_TYPE_PPG2_LEDC2,
- DATA_TYPE_PPG2_LEDC3,
- DATA_TYPE_PPG2_LEDC4,
- DATA_TYPE_PPG2_LEDC5,
- DATA_TYPE_PPG2_LEDC6,
- DATA_TYPE_PPF1_LEDC1,
- DATA_TYPE_PPF1_LEDC2,
- DATA_TYPE_PPF1_LEDC3,
- DATA_TYPE_PPF2_LEDC1 = 0x13,
- DATA_TYPE_PPF3_LEDC2,
- DATA_TYPE_PPF3_LEDC3,
- DATA_TYPE_PROX1 = 0x19,
- DATA_TYPE_PROX2,
- DATA_TYPE_INVALID_DATA = 0x1E,
- DATA_TYPE_TIME_STAMP,
- };
-
- enum PartIDs
- {
- MAX86140_PART_ID_VAL = 0x24,
- MAX86141_PART_ID_VAL,
- MAX86142_PART_ID_VAL,
- MAX86143_PART_ID_VAL,
- };
-
- enum LedRanges
- {
- LED_RANGE_0_50,
- LED_RANGE_50_100,
- LED_RANGE_100_150,
- LED_RANGE_150_200,
- };
-
- enum LedRangeSteps
- {
- LED_RANGE_STEP_25uA = 120,
- LED_RANGE_STEP_50uA = 240,
- LED_RANGE_STEP_75uA = 360,
- LED_RANGE_STEP_100uA = 480,
- };
-
- typedef enum {
- LED_1 = 0,
- LED_2,
- LED_3,
- LED_4,
- LED_5,
- LED_6,
- NUM_OF_LED,
- } max8614x_led_t;
-
- enum LED_CTRL_SM {
- LED_PROX = 1,
- LED_DATA_ACQ,
- };
-
- // AGC Range Parameter
- uint8_t kMax8614xDefaultLedOutRange;
-
- typedef union {
- struct {
- uint32_t val:19;
- uint32_t type:5;
- uint32_t:8;
- };
- uint32_t raw;
- } fifo_data_t;
-
- typedef union {
- uint16_t val;
- struct {
- uint8_t tint;
- uint8_t frac:4;
- uint8_t:4;
- };
- } die_temp_t;
-
- typedef union {
- struct {
- struct {
- unsigned char pwr_rdy:1;
- unsigned char vdd_oor:1;
- unsigned char die_temp_rdy:1;
- unsigned char led_compb:1;
- unsigned char prox_int:1;
- unsigned char alc_ovf:1;
- unsigned char data_rdy:1;
- unsigned char a_full:1;
- };
- struct {
- unsigned char:7;
- unsigned char sha_done:1;
- };
- };
- uint8_t val[2];
- } int_status_t;
-
- union led_range {
- struct {
- uint8_t led1:2;
- uint8_t led2:2;
- uint8_t led3:2;
- uint8_t:2;
- uint8_t led4:2;
- uint8_t led5:2;
- uint8_t led6:2;
- uint8_t:2;
- };
- uint8_t val[2];
- };
-
- typedef struct {
- uint32_t green;
- uint32_t ir;
- uint32_t red;
- } ppg_data_t;
-
- struct led_control {
- uint32_t diode_sum[NUM_OF_LED];
- uint32_t state;
- uint32_t prox_sum;
- uint32_t prox_sample_cnt;
- int32_t led_current[NUM_OF_LED];
- uint32_t default_current[NUM_OF_LED];
- int32_t agc_led_out_percent;
- int32_t agc_corr_coeff;
- int32_t agc_min_num_samples;
- int32_t agc_sensitivity_percent;
- int32_t change_by_percent_of_range[NUM_OF_LED];
- int32_t change_by_percent_of_current_setting[NUM_OF_LED];
- int32_t change_led_by_absolute_count[NUM_OF_LED];
- uint32_t sample_cnt;
- union led_range led_range_settings;
- uint8_t led_ranges;
- uint8_t agc_is_enabled;
- uint8_t lpm_is_enabled;
- };
-
- /* PRIVATE VARIABLES */
- SPI &m_spiBus;
- DigitalOut m_cs;
- std::queue<ppg_data_t> ppg_queue;
- struct led_control led_ctrl;
- int vdd_oor_cnt;
- die_temp_t die_temp;
- bool irq_triggered;
-
- /* PRIVATE FUNCTION DECLARATIONS */
- int reset();
-
- int poweroff();
-
- int enable_die_temp();
-
- int read_die_temp();
-
- int get_num_samples_in_fifo();
-
- void irq();
-
- int irq_handler();
-
- int fifo_irq_handler();
-
- int read_fifo_data(uint8_t *fifo_data, int num_samples);
-
- /* AGC functions */
- int max8614x_update_led_range(int new_range, uint8_t led_num,
- union led_range *led_range_settings);
-
- int max8614x_update_led_current(union led_range *led_range_settings,
- int led_new_val, max8614x_led_t led_num);
-
- void ppg_auto_gain_ctrl(struct led_control *led_ctrl,
- uint32_t sample_cnt, int diode_data, max8614x_led_t led_num);
-
- void max8614x_agc_handler(struct led_control *led_ctrl,
- int *samples);
-
- int led_control_sm(struct led_control *led_ctrl,
- int diode_data, char lpm);
-
- void led_control_reset(struct led_control *led_ctrl);
-
- int led_prox_init(struct led_control *led_ctrl, char lpm);
-
- int led_daq_init(struct led_control *led_ctrl, char lpm);
-
- void led_control_init(struct led_control *led_ctrl);
-
-
-};
-
-#endif /* _MAX8614X_H_ */
--- a/Drivers/MAX8614X/MAX8614X_agc.cpp Wed Apr 10 14:56:25 2019 +0300
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,505 +0,0 @@
-/*******************************************************************************
-* Copyright (C) 2017 Maxim Integrated Products, Inc., All Rights Reserved.
-*
-* Permission is hereby granted, free of charge, to any person obtaining a
-* copy of this software and associated documentation files (the "Software"),
-* to deal in the Software without restriction, including without limitation
-* the rights to use, copy, modify, merge, publish, distribute, sublicense,
-* and/or sell copies of the Software, and to permit persons to whom the
-* Software is furnished to do so, subject to the following conditions:
-*
-* The above copyright notice and this permission notice shall be included
-* in all copies or substantial portions of the Software.
-*
-* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
-* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
-* IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
-* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
-* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
-* OTHER DEALINGS IN THE SOFTWARE.
-*
-* Except as contained in this notice, the name of Maxim Integrated
-* Products, Inc. shall not be used except as stated in the Maxim Integrated
-* Products, Inc. Branding Policy.
-*
-* The mere transfer of this software does not imply any licenses
-* of trade secrets, proprietary technology, copyrights, patents,
-* trademarks, maskwork rights, or any other form of intellectual
-* property whatsoever. Maxim Integrated Products, Inc. retains all
-* ownership rights.
-*******************************************************************************
-*/
-
-#include "MAX8614X.h"
-#include <errno.h>
-#include "Peripherals.h"
-
-#define ARRAY_SIZE(array) (sizeof(array)/sizeof(array[0]))
-
-#define ILLEGAL_OUTPUT_POINTER 1
-#define ILLEGAL_DIODE_OUTPUT_MIN_MAX_PAIR 2
-#define ILLEGAL_LED_SETTING_MIN_MAX_PAIR 3
-#define CONSTRAINT_VIOLATION 4
-
-#define MAX8614X_LED_DRIVE_CURRENT_FULL_SCALE \
- (MAX8614X_MAX_LED_DRIVE_CURRENT - MAX8614X_MIN_LED_DRIVE_CURRENT)
-
-// that parameter is moved to header file to enable modification
-//#define MAX8614X_AGC_DEFAULT_LED_OUT_RANGE 30
-#define MAX8614X_AGC_DEFAULT_CORRECTION_COEFF 50
-#define MAX8614X_AGC_DEFAULT_SENSITIVITY_PERCENT 10
-#define MAX8614X_AGC_DEFAULT_NUM_SAMPLES_TO_AVG 25
-
-#define MAX8614X_PROX_THRESHOLD_1 10000
-#define MAX8614X_PROX_THRESHOLD_2 40000
-#define MAX8614X_PROX_DEBOUNCE_SPS 2
-#define MAX8614X_DAQ_DEBOUNCE_SPS 20
-
-#define MAX8614X_DEFAULT_DAQ_LED_CURRENT_1 40000
-#define MAX8614X_DEFAULT_DAQ_LED_CURRENT_2 40000
-#define MAX8614X_DEFAULT_DAQ_LED_CURRENT_3 0
-#define MAX8614X_DEFAULT_PROX_LED_CURRENT_1 10000
-#define MAX8614X_DEFAULT_PROX_LED_CURRENT_2 0
-#define MAX8614X_DEFAULT_PROX_LED_CURRENT_3 0
-
-
-#define MAX8614X_MIN_LED_DRIVE_CURRENT 0
-#define MAX8614X_MAX_LED_DRIVE_CURRENT 60000
-
-#define MAX8614X_MAX_PPG_DIODE_VAL ((1 << 19) - 1)
-#define MAX8614X_MIN_PPG_DIODE_VAL 0
-
-#define MAX8614X_DEFAULT_CURRENT1 0x30
-#define MAX8614X_DEFAULT_CURRENT2 0
-#define MAX8614X_DEFAULT_CURRENT3 0
-
-
-int MAX8614X::max8614x_update_led_range(
- int new_range, uint8_t led_num,
- union led_range *led_range_settings)
-{
- int old_range;
- Registers reg_addr;
-
- switch (led_num) {
- case LED_1:
- old_range = led_range_settings->led1;
- led_range_settings->led1 = new_range;
- reg_addr = MAX8614X_LED_RANGE1_REG;
- break;
- case LED_2:
- old_range = led_range_settings->led2;
- led_range_settings->led2 = new_range;
- reg_addr = MAX8614X_LED_RANGE1_REG;
- break;
- case LED_3:
- old_range = led_range_settings->led3;
- led_range_settings->led3 = new_range;
- reg_addr = MAX8614X_LED_RANGE1_REG;
- break;
- case LED_4:
- old_range = led_range_settings->led4;
- led_range_settings->led4 = new_range;
- reg_addr = MAX8614X_LED_RANGE2_REG;
- break;
- case LED_5:
- old_range = led_range_settings->led5;
- led_range_settings->led5 = new_range;
- reg_addr = MAX8614X_LED_RANGE2_REG;
- break;
- case LED_6:
- old_range = led_range_settings->led6;
- led_range_settings->led6 = new_range;
- reg_addr = MAX8614X_LED_RANGE2_REG;
- break;
-
- default:
- return -EINVAL;
- }
-
- if (old_range == new_range)
- return 0;
-
- return writeRegister( reg_addr,
- led_range_settings->val[led_num < LED_4 ? 0 : 1]);
-}
-
-int MAX8614X::max8614x_update_led_current(
- union led_range *led_range_settings,
- int led_new_val,
- max8614x_led_t led_num)
-{
- int ret = 0;
- Registers led_current_reg_addr;
- int led_range;
- uint8_t led_current_reg_val;
- int led_range_index = led_new_val / 25000;
- const int led_range_steps[] = {
- LED_RANGE_STEP_25uA,
- LED_RANGE_STEP_50uA,
- LED_RANGE_STEP_75uA,
- LED_RANGE_STEP_100uA,
- LED_RANGE_STEP_100uA, /* For led current greater than 100uA */
- };
-
- switch(led_num) {
- case LED_1:
- led_current_reg_addr = MAX8614X_LED1_PA_REG;
- break;
- case LED_2:
- led_current_reg_addr = MAX8614X_LED2_PA_REG;
- break;
- case LED_3:
- led_current_reg_addr = MAX8614X_LED3_PA_REG;
- break;
- case LED_4:
- led_current_reg_addr = MAX8614X_LED4_PA_REG;
- break;
- case LED_5:
- led_current_reg_addr = MAX8614X_LED5_PA_REG;
- break;
- case LED_6:
- led_current_reg_addr = MAX8614X_LED6_PA_REG;
- break;
- default:
- pr_err("Invalid led number: %d\n", led_num);
- return -EINVAL;
- }
-
- if (led_new_val < MAX8614X_MIN_LED_DRIVE_CURRENT
- || led_new_val > MAX8614X_MAX_LED_DRIVE_CURRENT) {
- pr_err("Invalid led value: %d\n", led_new_val);
- return -EINVAL;
- }
-
- led_current_reg_val = led_new_val / led_range_steps[led_range_index];
-
- pr_debug("Updating LED%d current to %d. led_rge_idx: %d, reg_val: %.2X",
- led_num, led_new_val, led_range_index, led_current_reg_val);
-
- ret = writeRegister(led_current_reg_addr, led_current_reg_val);
- if (ret < 0)
- return ret;
-
-
- led_range = led_range_index;
- pr_debug("Updating LED%d range to %d.", led_num, led_range);
- if (led_range > 3)
- led_range = 3;
- ret = max8614x_update_led_range( led_range, led_num, led_range_settings);
- if (ret < 0)
- return ret;
- return ret;
-}
-
-int32_t agc_adj_calculator(
- int32_t *change_by_percent_of_range,
- int32_t *change_by_percent_of_current_setting,
- int32_t *change_led_by_absolute_count,
- int32_t *set_led_to_absolute_count,
- int32_t target_percent_of_range,
- int32_t correction_coefficient,
- int32_t allowed_error_in_percentage,
- int32_t current_average,
- int32_t number_of_samples_averaged,
- int32_t led_drive_current_value)
-{
- int32_t current_percent_of_range = 0;
- int32_t delta = 0;
- int32_t desired_delta = 0;
- int32_t current_power_percent = 0;
-
- if (change_by_percent_of_range == 0
- || change_by_percent_of_current_setting == 0
- || change_led_by_absolute_count == 0
- || set_led_to_absolute_count == 0)
- return ILLEGAL_OUTPUT_POINTER;
-
- if (target_percent_of_range > 90 || target_percent_of_range < 10)
- return CONSTRAINT_VIOLATION;
-
- if (correction_coefficient > 100 || correction_coefficient < 0)
- return CONSTRAINT_VIOLATION;
-
- if (allowed_error_in_percentage > 100
- || allowed_error_in_percentage < 0)
- return CONSTRAINT_VIOLATION;
-
-#if ((MAX8614X_MAX_PPG_DIODE_VAL - MAX8614X_MIN_PPG_DIODE_VAL) <= 0 \
- || (MAX8614X_MAX_PPG_DIODE_VAL < 0) || (MAX8614X_MIN_PPG_DIODE_VAL < 0))
- #error "Illegal diode Min/Max Pair"
-#endif
-
-#if ((MAX8614X_MAX_LED_DRIVE_CURRENT - MAX8614X_MIN_LED_DRIVE_CURRENT) <= 0 \
- || (MAX8614X_MAX_LED_DRIVE_CURRENT < 0) || (MAX8614X_MIN_LED_DRIVE_CURRENT < 0))
- #error "Illegal LED Min/Max current Pair"
-#endif
-
- if (led_drive_current_value > MAX8614X_MAX_LED_DRIVE_CURRENT
- || led_drive_current_value < MAX8614X_MIN_LED_DRIVE_CURRENT)
- return CONSTRAINT_VIOLATION;
-
- if (current_average < MAX8614X_MIN_PPG_DIODE_VAL
- || current_average > MAX8614X_MAX_PPG_DIODE_VAL)
- return CONSTRAINT_VIOLATION;
-
- current_percent_of_range = 100 *
- (current_average - MAX8614X_MIN_PPG_DIODE_VAL) /
- (MAX8614X_MAX_PPG_DIODE_VAL - MAX8614X_MIN_PPG_DIODE_VAL) ;
-
- delta = current_percent_of_range - target_percent_of_range;
- delta = delta * correction_coefficient / 100;
-
- if (delta > -allowed_error_in_percentage
- && delta < allowed_error_in_percentage) {
- *change_by_percent_of_range = 0;
- *change_by_percent_of_current_setting = 0;
- *change_led_by_absolute_count = 0;
- *set_led_to_absolute_count = led_drive_current_value;
- return 0;
- }
-
- current_power_percent = 100 *
- (led_drive_current_value - MAX8614X_MIN_LED_DRIVE_CURRENT) /
- (MAX8614X_MAX_LED_DRIVE_CURRENT - MAX8614X_MIN_LED_DRIVE_CURRENT);
- if (delta < 0)
- desired_delta = -delta * (100 - current_power_percent) /
- (100 - current_percent_of_range);
-
- if (delta > 0)
- desired_delta = -delta * (current_power_percent)
- / (current_percent_of_range);
-
- *change_by_percent_of_range = desired_delta;
-
- *change_led_by_absolute_count = (desired_delta
- * MAX8614X_LED_DRIVE_CURRENT_FULL_SCALE / 100);
- *change_by_percent_of_current_setting =
- (*change_led_by_absolute_count * 100)
- / (led_drive_current_value);
- *set_led_to_absolute_count = led_drive_current_value
- + *change_led_by_absolute_count;
-
- //If we are saturated, cut power in half
- if (current_percent_of_range >= 100)
- {
- *change_by_percent_of_range = -100; //Unknown, set fake value
- *change_by_percent_of_current_setting = -50;
- *change_led_by_absolute_count = 0 - (led_drive_current_value / 2);
- *set_led_to_absolute_count = led_drive_current_value / 2;
- }
-
- return 0;
-}
-
-void MAX8614X::ppg_auto_gain_ctrl(
- struct led_control *led_ctrl,
- uint32_t sample_cnt, int diode_data, max8614x_led_t led_num)
-{
- int ret;
- int diode_avg;
-
- if (led_num > LED_3) /* TODO: why3? */
- return;
-
- led_ctrl->diode_sum[led_num] += diode_data;
- if (sample_cnt % led_ctrl->agc_min_num_samples == 0) {
- diode_avg = led_ctrl->diode_sum[led_num]
- / led_ctrl->agc_min_num_samples;
- led_ctrl->diode_sum[led_num] = 0;
- } else
- return;
-
- ret = agc_adj_calculator(
- &led_ctrl->change_by_percent_of_range[led_num],
- &led_ctrl->change_by_percent_of_current_setting[led_num],
- &led_ctrl->change_led_by_absolute_count[led_num],
- &led_ctrl->led_current[led_num],
- led_ctrl->agc_led_out_percent,
- led_ctrl->agc_corr_coeff,
- led_ctrl->agc_sensitivity_percent,
- diode_avg,
- led_ctrl->agc_min_num_samples,
- led_ctrl->led_current[led_num]);
- if (ret)
- return;
-
- if (led_ctrl->change_led_by_absolute_count[led_num] == 0)
- return;
-
- ret = max8614x_update_led_current(&led_ctrl->led_range_settings,
- led_ctrl->led_current[led_num], led_num);
- if (ret < 0)
- pr_err("%s failed", __func__);
- return;
-}
-
-void MAX8614X::max8614x_agc_handler(struct led_control *led_ctrl,
- int *samples)
-{
- static int ret = -1;
-
- if (!led_ctrl->agc_is_enabled)
- return;
-
- led_ctrl->sample_cnt++;
- ret = led_control_sm(led_ctrl,
- samples[DATA_TYPE_PPG1_LEDC1],
- led_ctrl->lpm_is_enabled);
-
- if (ret == LED_DATA_ACQ) {
- ppg_auto_gain_ctrl(led_ctrl,
- led_ctrl->sample_cnt,
- samples[DATA_TYPE_PPG1_LEDC1],
- LED_1);
- ppg_auto_gain_ctrl(led_ctrl,
- led_ctrl->sample_cnt,
- samples[DATA_TYPE_PPG1_LEDC1],
- LED_2);
-// ppg_auto_gain_ctrl(led_ctrl,
-// led_ctrl->sample_cnt,
-// samples[DATA_TYPE_PPG1_LEDC3],
-// LED_3);
- }
-
- return;
-}
-
-int MAX8614X::led_prox_init(struct led_control *led_ctrl, char lpm)
-{
- int ret;
- const RegisterMap low_pm_settings[] = {
- { MAX8614X_PPG_CFG1_REG, MAX8614X_PPG_LED_PW_115_2_US_MASK // PPG_LED_PW = 3 (115.2us)
- | MAX8614X_PPG1_ADC_RGE_32768_MASK // PPG1_ADC_RGE = 3(32768nA)
- | MAX8614X_PPG2_ADC_RGE_32768_MASK }, // PPG2_ADC_RGE = 3(32768nA)
-// { MAX8614X_PPG_CFG2_REG, MAX8614X_PPG_SR_25_SPS},
- { MAX8614X_INT_ENABLE1_REG, MAX8614X_INT1_EN_DATA_RDY_MASK },
- };
-
- led_ctrl->led_current[LED_1] = MAX8614X_DEFAULT_PROX_LED_CURRENT_1;
- ret = max8614x_update_led_current(&led_ctrl->led_range_settings,
- led_ctrl->led_current[LED_1], LED_1);
-
- led_ctrl->led_current[LED_2] = MAX8614X_DEFAULT_PROX_LED_CURRENT_2;
- ret |= max8614x_update_led_current(&led_ctrl->led_range_settings,
- led_ctrl->led_current[LED_2], LED_2);
-
- led_ctrl->led_current[LED_3] = MAX8614X_DEFAULT_PROX_LED_CURRENT_3;
- ret |= max8614x_update_led_current(&led_ctrl->led_range_settings,
- led_ctrl->led_current[LED_3], LED_3);
-
- if (lpm)
- ret |= writeBlock(low_pm_settings,
- ARRAY_SIZE(low_pm_settings));
- return ret;
-}
-
-int MAX8614X::led_daq_init(struct led_control *led_ctrl, char lpm)
-{
- int ret;
- const RegisterMap non_lpm_settings[] = {
- { MAX8614X_PPG_CFG1_REG, MAX8614X_PPG_LED_PW_115_2_US_MASK // PPG_LED_PW = 3 (115.2us)
- | MAX8614X_PPG1_ADC_RGE_32768_MASK // PPG1_ADC_RGE = 3(32768nA)
- | MAX8614X_PPG2_ADC_RGE_32768_MASK }, // PPG2_ADC_RGE = 3(32768nA)
-// { MAX8614X_PPG_CFG2_REG, MAX8614X_PPG_SR_25_SPS},
- { MAX8614X_INT_ENABLE1_REG, MAX8614X_INT1_EN_DATA_RDY_MASK },
- };
-
- led_ctrl->led_current[LED_1] = MAX8614X_DEFAULT_DAQ_LED_CURRENT_1;
- ret = max8614x_update_led_current(&led_ctrl->led_range_settings,
- led_ctrl->led_current[LED_1], LED_1);
-
- led_ctrl->led_current[LED_2] = MAX8614X_DEFAULT_DAQ_LED_CURRENT_2;
- ret |= max8614x_update_led_current(&led_ctrl->led_range_settings,
- led_ctrl->led_current[LED_2], LED_2);
-
- led_ctrl->led_current[LED_3] = MAX8614X_DEFAULT_DAQ_LED_CURRENT_3;
- ret |= max8614x_update_led_current(&led_ctrl->led_range_settings,
- led_ctrl->led_current[LED_3], LED_3);
-
- if (lpm)
- ret |= writeBlock(non_lpm_settings,
- ARRAY_SIZE(non_lpm_settings));
-
- return ret;
-}
-
-int MAX8614X::led_control_sm(struct led_control *led_ctrl, int diode_data, char lpm)
-{
- int ret = led_ctrl->state;
- int avg = 0;
-
- led_ctrl->prox_sample_cnt++;
- led_ctrl->prox_sum += diode_data;
-
- switch (led_ctrl->state) {
- case LED_PROX:
- if (led_ctrl->prox_sample_cnt % MAX8614X_PROX_DEBOUNCE_SPS != 0)
- break;
-
- avg = led_ctrl->prox_sum / MAX8614X_PROX_DEBOUNCE_SPS;
- if (avg >= MAX8614X_PROX_THRESHOLD_1) {
- led_ctrl->state = LED_DATA_ACQ;
- ret = led_daq_init(led_ctrl, lpm);
- led_ctrl->prox_sample_cnt = 0;
- }
- led_ctrl->prox_sum = 0;
- break;
-
- case LED_DATA_ACQ:
- if (led_ctrl->prox_sample_cnt % MAX8614X_DAQ_DEBOUNCE_SPS != 0)
- break;
-
- avg = led_ctrl->prox_sum / MAX8614X_DAQ_DEBOUNCE_SPS;
- if (avg <= MAX8614X_PROX_THRESHOLD_2) {
- led_ctrl->state = LED_PROX;
- ret = led_prox_init(led_ctrl, lpm);
- led_ctrl->prox_sample_cnt = 0;
- }
- led_ctrl->prox_sum = 0;
- break;
-
- default:
- led_ctrl->state = LED_PROX;
- led_ctrl->prox_sum = 0;
- led_ctrl->prox_sample_cnt = 0;
- return -EINVAL;
- }
-
- return ret;
-}
-
-void MAX8614X::led_control_reset(struct led_control *led_ctrl)
-{
- led_ctrl->led_current[LED_1] = led_ctrl->default_current[LED_1];
- led_ctrl->led_current[LED_2] = led_ctrl->default_current[LED_2];
- led_ctrl->led_current[LED_3] = led_ctrl->default_current[LED_3];
-
- memset(led_ctrl->change_by_percent_of_range, 0,
- sizeof(led_ctrl->change_by_percent_of_range));
- memset(led_ctrl->change_by_percent_of_current_setting, 0,
- sizeof(led_ctrl->change_by_percent_of_range));
- memset(led_ctrl->change_led_by_absolute_count, 0,
- sizeof(led_ctrl->change_by_percent_of_range));
- memset(led_ctrl->diode_sum, 0, sizeof(led_ctrl->diode_sum));
-
- led_ctrl->agc_is_enabled = 1;
- led_ctrl->prox_sum = 0;
- led_ctrl->prox_sample_cnt = 0;
- led_ctrl->sample_cnt = 0;
- led_ctrl->state = LED_PROX;
-}
-
-void MAX8614X::led_control_init(struct led_control *led_ctrl)
-{
- memset(led_ctrl, 0, sizeof(struct led_control));
-
- led_ctrl->default_current[LED_1] = MAX8614X_DEFAULT_CURRENT1;
- led_ctrl->default_current[LED_2] = MAX8614X_DEFAULT_CURRENT2;
- led_ctrl->default_current[LED_3] = MAX8614X_DEFAULT_CURRENT3;
- led_ctrl->agc_led_out_percent = kMax8614xDefaultLedOutRange;
- led_ctrl->agc_corr_coeff = MAX8614X_AGC_DEFAULT_CORRECTION_COEFF;
- led_ctrl->agc_min_num_samples = MAX8614X_AGC_DEFAULT_NUM_SAMPLES_TO_AVG;
- led_ctrl->agc_sensitivity_percent = MAX8614X_AGC_DEFAULT_SENSITIVITY_PERCENT;
-}