Example for onboard MAX77650 PMIC driver and MAX32620FTHR platform driver

Dependencies:   MAX32620FTHR MAX77650

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "MAX32620FTHR.h"
00003 #include "MAX77650.h"
00004 
00005 // Configure GPIO voltage
00006 MAX32620FTHR max32620fthr(MAX32620FTHR::VIO_3V3);
00007 
00008 // LEDs connected to MAX32620
00009 DigitalOut redLed(LED_RED, LED_OFF);
00010 DigitalOut grnLed(LED_GREEN, LED_OFF);
00011 DigitalOut bluLed(LED_BLUE, LED_OFF);
00012 
00013 // I2C master 2
00014 I2C i2c2(I2C2_SDA, I2C2_SCL);
00015 
00016 // MAX77650 PMIC and Charger
00017 MAX77650 max77650(i2c2);
00018 
00019 // MAX77650 IRQ
00020 InterruptIn max77650IntIn(P2_3);
00021 
00022 // Configure PMIC LEDs to rotate colors
00023 static const char ledRotate[] = {
00024     MAX77650::CNFG_LED0_A,
00025     0x44, 0x44, 0x44,
00026     0x17, 0x37, 0x77,
00027     0x01,
00028 };
00029 
00030 // Configure PMIC to drive green LED
00031 static const char ledGreen[] = {
00032     MAX77650::CNFG_LED0_A,
00033     0x04, 0x44, 0x04,
00034     0x0F, 0x0F, 0x0F,
00035     0x01,
00036 };
00037 
00038 // Disable LED's connected to PMIC
00039 static const char ledNone[] = {
00040     MAX77650::CNFG_LED0_A,
00041     0x04, 0x04, 0x04,
00042     0x0F, 0x0F, 0x0F,
00043     0x01,
00044 };
00045 
00046 // Cycle through PMIC LED's with every button push
00047 void max77650IrqHandler(void)
00048 {
00049     char irqStatus;
00050     static char ledPhase = 0;
00051     static char ledData[] = {
00052         MAX77650::CNFG_LED0_A,
00053         0x44, 0x04, 0x04,
00054         0x0F, 0x0F, 0x0F,
00055         0x01,
00056     };
00057 
00058     while (!(max77650.readReg(MAX77650::INT_GLBL, &irqStatus))) {
00059         if (irqStatus) {
00060             if (irqStatus & 0x04) {
00061                 ledPhase++;
00062                 switch (ledPhase % 4) {
00063                     case 0: ledData[1] ^= 0x40; break;
00064                     case 1: ledData[1] ^= 0x40; ledData[2] ^= 0x40; break;
00065                     case 2: ledData[2] ^= 0x40; ledData[3] ^= 0x40; break;
00066                     case 3: ledData[3] ^= 0x40; break;
00067                 }
00068                 if (!(max77650.writeReg(ledData, sizeof(ledData)))) {
00069                     break;
00070                 }
00071             }
00072         } else {
00073             break;
00074         }
00075     }
00076 }
00077 
00078 // MAX77650 IRQ configuration
00079 void max77650IrqInit(void)
00080 {
00081     // Configure MAX77650 to generate an interrupt request when push buton SW2 is pressed
00082     max77650.writeReg(MAX77650::CNFG_GLBL, MAX77650::SBIA_LPM | MAX77650::DBEN_30_MS);
00083     max77650.writeReg(MAX77650::INTM_GLBL, MAX77650::NEN_FALL);
00084 
00085     // Manually invoke the MAX77650 interrupt handler to service any pending interrupts
00086     max77650IrqHandler();
00087 
00088     // Configure InterruptIn object to service MAX77650 IRQ signal
00089     max77650IntIn.mode(PullUp);
00090     max77650IntIn.fall(max77650IrqHandler);
00091     max77650IntIn.enable_irq();
00092 }
00093 
00094 int main()
00095 {
00096     int state = 0;
00097 
00098     // Turn off Low-Dropout Linear Regulator (LDO); unused on MAX32620FTHR platform
00099     max77650.disableLDO();
00100     
00101     // Set SIMO Buck-Boost Regulator 2 target voltage; provides VDDIOH
00102     max77650.setSBB2Voltage(3.3f);
00103 
00104     // Turn PMIC green LED on, red and blue off
00105     max77650.writeReg(ledGreen, sizeof(ledGreen));
00106 
00107     Thread::wait(3000);
00108 
00109     // Configure PMIC LED's to cycle between colors
00110     max77650.writeReg(ledRotate, sizeof(ledRotate));
00111 
00112     Thread::wait(3000);
00113 
00114     // Turn off PMIC LED's
00115     max77650.writeReg(ledNone, sizeof(ledNone));
00116 
00117     // Enable PMIC push button interrupt
00118     max77650IrqInit();
00119     
00120     // Cycle MAX32620FTHR LED's
00121     for (;;) {
00122         Thread::wait(500);
00123         switch (state++ & 0x3) {
00124             case 0: redLed = LED_ON; break;
00125             case 1: redLed = LED_OFF; grnLed = LED_ON; break;
00126             case 2: grnLed = LED_OFF; bluLed = LED_ON; break;
00127             case 3: bluLed = LED_OFF; break;
00128         }
00129     }
00130 }