MAX77734 Demo

Dependencies:   max32630fthr USBDevice

main.cpp

Committer:
daniel_gs_jeong
Date:
2018-12-27
Revision:
8:ba930f3cc7fa
Parent:
6:684c51f32c1d

File content as of revision 8:ba930f3cc7fa:

/*******************************************************************************
 * Copyright (C) 2018 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 "mbed.h"
#include "max32630fthr.h"
#include "USBSerial.h"
#include <string> 
#include "max77734.h"
#include "serial_gui_if.h"

void IRQB_FALL_ISR();
void serial_function_info(SERIAL_GUI_INTERFACE *sif);
void handle_serial_cmd(SERIAL_GUI_INTERFACE *sif, int id, MAX77734 *max77734);

MAX32630FTHR pegasus(MAX32630FTHR::VIO_3V3);

// Hardware serial port over DAPLink
Serial daplink(P2_1, P2_0);

// Virtual serial port over USB
USBSerial microUSB;

// Interrupt Pin
DigitalIn PIN_IRQB(P5_0, PullUp); 
InterruptIn IRQB(P5_0);

// LED
DigitalOut rLED(LED1);
DigitalOut gLED(LED2);
DigitalOut bLED(LED3);

// I2C Pins
I2C i2cBus(P5_7, P6_0);

// MAX77734
MAX77734 max77734(&i2cBus);    
//******************************************************************************
// main() runs in its own thread in the OS
// (note the calls to Thread::wait below for delays)
int main()
{
    int cmd = 0;

    rLED = LED_OFF;
    gLED = LED_OFF;
    bLED = LED_OFF;
    
    SERIAL_GUI_INTERFACE sif = SERIAL_GUI_INTERFACE(&daplink);
    daplink.printf("daplink serial port\r\n");
    microUSB.printf("micro USB serial port\r\n");
        
    i2cBus.frequency(400000);
    wait_ms(100);    
    // register Interrupt Service Routine
    IRQB.fall(&IRQB_FALL_ISR);
    // MAX77734 Driver and Initialize
    max77734.init();
    // register the information of functions
    serial_function_info(&sif);
    
    while(1) {
         
        gLED = LED_OFF;
        rLED = LED_ON;
        if((cmd = sif.get_serial_cmd()) > 0)
        {
            handle_serial_cmd(&sif, cmd, &max77734);
            daplink.printf("cmd id %d handled\n", cmd);
            
            rLED = LED_OFF;
            gLED = LED_ON;
            //daplink.printf("arg1 %d\n",sif.get_argument(1));
            //daplink.printf("arg1 %d\n",sif.get_argument(2));            
            //daplink.printf("arg1 %d %s\n",sif.get_argi(1),sif.get_arg(1));
        }        
        
        wait_ms(50); 
        daplink.printf("main\n");   
    }
}

//******************************************************************************
void IRQB_FALL_ISR()
{
    int32_t rData;
    rData = max77734.interrupt_isr();
    if(rData < 0){
        daplink.printf("0x%x = 0x%x", MAX77734::REG_INT_GLBL, rData);
        daplink.printf("0x%x = 0x%x", MAX77734::REG_INT_CHG,  rData);
        return;
    }
        
    daplink.printf("0x%x = 0x%x", MAX77734::REG_INT_GLBL, rData & 0xff);
    daplink.printf("0x%x = 0x%x", MAX77734::REG_INT_CHG, (rData >>8) & 0xff);    
}

//******************************************************************************
void handle_serial_cmd(SERIAL_GUI_INTERFACE *sif, int id, MAX77734 *max77734)
{
    int reg_no, data, mask, rData;
        
    daplink.printf("handle_serial_cmd id  %d\n",id);
    switch(id){
    case 0x100:
        sif->req_function_info();
        break; 
        
    case 0x101://i2c read byte    
        reg_no = sif->get_argi(1);
        rData = max77734->i2c_read_byte((MAX77734::REG_TYPE)reg_no);
        sif->send_reg_value(reg_no, rData);
        break;
        
    case 0x102://i2c write byte
        reg_no = sif->get_argi(1);
        data   = sif->get_argi(2);        
        rData = max77734->i2c_write_byte((MAX77734::REG_TYPE)reg_no,
                                         (char)data);
        if(rData < 0)         
            sif->send_reg_value(reg_no, rData);
        else 
            sif->send_reg_value(reg_no, 0);
        break;
        
    case 0x103://i2c update byte
        reg_no = sif->get_argi(1);
        data   = sif->get_argi(2);
        mask   = sif->get_argi(3);
        rData = max77734->i2c_update_byte((MAX77734::REG_TYPE)reg_no,
                                         (char)data, (char)mask);
                                         
        if(rData < 0)         
            sif->send_reg_value(reg_no, rData);
        else 
            sif->send_reg_value(reg_no, 0);
        break;
    
    default:    
        rData = max77734->read_register((MAX77734::REG_TYPE) id);
        sif->send_reg_value(id, rData);
        break;
        
    }    
}

//******************************************************************************
void serial_function_info(SERIAL_GUI_INTERFACE *sif)
{
    //Function Name, name length, id, argc, arg[n]min, arg[n]max.....
    sif->set_function_info("req_func_info",
                          strlen("req_func_info"),   0x100, 0);
    sif->set_function_info("i2c_read_byte",    
                          strlen("i2c_read_byte"),   0x101, 2, 0x0, 0xff);
    sif->set_function_info("i2c_write_byte",   
                      strlen("i2c_write_byte"), 0x102, 3, 0x0, 0xff, 0x0, 0xff);
    sif->set_function_info("i2c_update_byte",  strlen("i2c_update_byte"), 
                                     0x103, 4, 0x0, 0xff, 0x0, 0xff, 0x0, 0xff);
 
    sif->set_function_info("REG_INT_GLBL",     strlen("REG_INT_GLBL"),    
                                                MAX77734::REG_INT_GLBL,    0);
    sif->set_function_info("REG_INT_CHG",      strlen("REG_INT_CHG"),     
                                                MAX77734::REG_INT_CHG,     0);
    sif->set_function_info("REG_STAT_CHG_A",   strlen("REG_STAT_CHG_A"),  
                                                MAX77734::REG_STAT_CHG_A,  0);
    sif->set_function_info("REG_STAT_CHG_B",   strlen("REG_STAT_CHG_B"),  
                                                MAX77734::REG_STAT_CHG_B,  0);
    sif->set_function_info("REG_ERCFLAG",      strlen("REG_ERCFLAG"),     
                                                MAX77734::REG_ERCFLAG,     0);
    sif->set_function_info("REG_STAT_GLBL",    strlen("REG_STAT_GLBL"),   
                                                MAX77734::REG_STAT_GLBL,   0);
    sif->set_function_info("REG_INTM_GLBL",    strlen("REG_INTM_GLBL"),   
                                                MAX77734::REG_INTM_GLBL,   0);
    sif->set_function_info("REG_INTM_CHG",     strlen("REG_INTM_CHG"),    
                                                MAX77734::REG_INTM_CHG,    0);
    sif->set_function_info("REG_CNFG_GLBL",    strlen("REG_CNFG_GLBL"),   
                                                MAX77734::REG_CNFG_GLBL,   0);
    sif->set_function_info("REG_CID",          strlen("REG_CID"),         
                                                MAX77734::REG_CID,         0);
    sif->set_function_info("REG_CNFG_WDT",     strlen("REG_CNFG_WDT"),    
                                                MAX77734::REG_CNFG_WDT,    0);
    sif->set_function_info("REG_CNFG_CHG_A",   strlen("REG_CNFG_CHG_A"),  
                                                MAX77734::REG_CNFG_CHG_A,  0);
    sif->set_function_info("REG_CNFG_CHG_B",   strlen("REG_CNFG_CHG_B"),  
                                                MAX77734::REG_CNFG_CHG_B,  0);
    sif->set_function_info("REG_CNFG_CHG_C",   strlen("REG_CNFG_CHG_C"),  
                                                MAX77734::REG_CNFG_CHG_C,  0);
    sif->set_function_info("REG_CNFG_CHG_D",   strlen("REG_CNFG_CHG_D"),  
                                                MAX77734::REG_CNFG_CHG_D,  0);
    sif->set_function_info("REG_CNFG_CHG_E",   strlen("REG_CNFG_CHG_E"),  
                                                MAX77734::REG_CNFG_CHG_E,  0);
    sif->set_function_info("REG_CNFG_CHG_F",   strlen("REG_CNFG_CHG_F"),  
                                                MAX77734::REG_CNFG_CHG_F,  0);
    sif->set_function_info("REG_CNFG_CHG_G",   strlen("REG_CNFG_CHG_G"),  
                                                MAX77734::REG_CNFG_CHG_G,  0);
    sif->set_function_info("REG_CNFG_CHG_H",   strlen("REG_CNFG_CHG_H"),  
                                                MAX77734::REG_CNFG_CHG_H,  0);
    sif->set_function_info("REG_CNFG_CHG_I",   strlen("REG_CNFG_CHG_I"),  
                                                MAX77734::REG_CNFG_CHG_I,  0);
    sif->set_function_info("REG_CNFG_LDO_A",   strlen("REG_CNFG_LDO_A"),  
                                                MAX77734::REG_CNFG_LDO_A,  0);
    sif->set_function_info("REG_CNFG_LDO_B",   strlen("REG_CNFG_LDO_B"),  
                                                MAX77734::REG_CNFG_LDO_B,  0);
    sif->set_function_info("REG_CNFG_SNK1_A",  strlen("REG_CNFG_SNK1_A"), 
                                                MAX77734::REG_CNFG_SNK1_A, 0);
    sif->set_function_info("REG_CNFG_SNK1_B",  strlen("REG_CNFG_SNK1_B"), 
                                                MAX77734::REG_CNFG_SNK1_B, 0);
    sif->set_function_info("REG_CNFG_SNK2_A",  strlen("REG_CNFG_SNK2_A"), 
                                                MAX77734::REG_CNFG_SNK2_A, 0);
    sif->set_function_info("REG_CNFG_SNK2_B",  strlen("REG_CNFG_SNK2_B"), 
                                                MAX77734::REG_CNFG_SNK2_B, 0);
    sif->set_function_info("REG_CNFG_SNK_TOP", strlen("REG_CNFG_SNK_TOP"),
                                                MAX77734::REG_CNFG_SNK_TOP,0);
}