/ components / boards / m5stick / src / m5stick.c
m5stick.c
  1  #include "sipeed_i2c.h"
  2  #include "stdint.h"
  3  #include "stdbool.h"
  4  #include "fpioa.h"
  5  #include "sysctl.h"
  6  #include "global_config.h"
  7  #include "fpioa.h"
  8  #include "gpiohs.h"
  9  #include "sleep.h"
 10  
 11  #ifdef CONFIG_BOARD_M5STICK
 12  
 13  #define AXP192_I2C_PIN_SCL  28
 14  #define AXP192_I2C_PIN_SDA  29
 15  #define AXP192_ADDR         0x34
 16  
 17  /**
 18   * 
 19   * @attention Will use I2C0, and release after init complete
 20   * 
 21   */
 22  bool m5stick_init()
 23  {
 24      uint8_t cmd[2];
 25      int ret = 0;
 26  
 27      sysctl_set_power_mode(SYSCTL_POWER_BANK3,SYSCTL_POWER_V33);
 28  
 29      //init power manager //TODO: pack API for power saving
 30      fpioa_set_function(AXP192_I2C_PIN_SCL, FUNC_I2C0_SCLK);
 31      fpioa_set_function(AXP192_I2C_PIN_SDA, FUNC_I2C0_SDA);
 32      maix_i2c_init(I2C_DEVICE_0, 7, 400000);
 33      ret = maix_i2c_recv_data(I2C_DEVICE_0, AXP192_ADDR, NULL, 0, cmd, 1, 10);
 34      if (ret != 0)
 35          goto end;
 36      cmd[0] = 0x23;
 37      cmd[1] = 0x08; //K210_VCore(DCDC2) set to 0.9V
 38      ret = maix_i2c_send_data(I2C_DEVICE_0, AXP192_ADDR, cmd, 2, 10);
 39      if (ret != 0)
 40          goto end;
 41      cmd[0] = 0x33;
 42      cmd[1] = 0xC1; //190mA Charging Current
 43      ret = maix_i2c_send_data(I2C_DEVICE_0, AXP192_ADDR, cmd, 2, 10);
 44      if(ret!=0)
 45          goto end;
 46      cmd[0] = 0x36;
 47      cmd[1] = 0x6C; //4s shutdown
 48      ret = maix_i2c_send_data(I2C_DEVICE_0, AXP192_ADDR, cmd, 2, 10);
 49      if(ret!=0)
 50          goto end;
 51      cmd[0] = 0x91;
 52      cmd[1] = 0xF0; //LCD Backlight: GPIO0 3.3V
 53      ret = maix_i2c_send_data(I2C_DEVICE_0, AXP192_ADDR, cmd, 2, 10);
 54      if(ret!=0)
 55          goto end;
 56      cmd[0] = 0x90;
 57      cmd[1] = 0x02; //GPIO LDO mode
 58      ret = maix_i2c_send_data(I2C_DEVICE_0, AXP192_ADDR, cmd, 2, 10);
 59      if(ret!=0)
 60          goto end;
 61      cmd[0] = 0x28;
 62      cmd[1] = 0xF0; //VDD2.8V net: LDO2 3.3V,  VDD 1.5V net: LDO3 1.8V
 63      ret = maix_i2c_send_data(I2C_DEVICE_0, AXP192_ADDR, cmd, 2, 10);
 64      if(ret!=0)
 65          goto end;
 66      cmd[0] = 0x27;
 67      cmd[1] = 0x2C; //VDD1.8V net:  DC-DC3 1.8V
 68      ret = maix_i2c_send_data(I2C_DEVICE_0, AXP192_ADDR, cmd, 2, 10);
 69      if(ret!=0)
 70          goto end;
 71      cmd[0] = 0x12;
 72      cmd[1] = 0xFF; //open all power and EXTEN
 73      ret = maix_i2c_send_data(I2C_DEVICE_0, AXP192_ADDR, cmd, 2, 10);
 74      if(ret!=0)
 75          goto end;
 76      cmd[0] = 0x23;
 77      cmd[1] = 0x08; //VDD 0.9v net: DC-DC2 0.9V
 78      ret = maix_i2c_send_data(I2C_DEVICE_0, AXP192_ADDR, cmd, 2, 10);
 79      if(ret!=0)
 80          goto end;
 81      cmd[0] = 0x31;
 82      cmd[1] = 0x03; //Cutoff voltage 3.2V
 83      ret = maix_i2c_send_data(I2C_DEVICE_0, AXP192_ADDR, cmd, 2, 10);
 84      if(ret!=0)
 85          goto end;
 86      cmd[0] = 0x39;
 87      cmd[1] = 0xFC; //Turnoff Temp Protect (Sensor not exist!)
 88      ret = maix_i2c_send_data(I2C_DEVICE_0, AXP192_ADDR, cmd, 2, 10);
 89      if(ret!=0)
 90          goto end;
 91      fpioa_set_function(23, FUNC_GPIOHS0 + 26);
 92      gpiohs_set_drive_mode(26, GPIO_DM_OUTPUT);
 93      gpiohs_set_pin(26, GPIO_PV_HIGH); //Disable VBUS As Input, BAT->5V Boost->VBUS->Charing Cycle
 94  
 95      msleep(20);
 96  end:
 97      maix_i2c_deinit(I2C_DEVICE_0);
 98      return ret==0;
 99  }
100  
101  
102  #endif
103