MDM9206 with Motion Detector

Skill LevelArea of FocusOperating SystemPlatform/HardwareCloud Services/Platform
IntermediateHealthcare, SensorsRTOSMDM920x LTE for IoTGizwits Cloud Platform

This project is designed to use the ADXL345 sensor to detect three axis acceleration values using the Qualcomm® MDM9206 LTE modem, a multimode communication chipset that supports Cat-M1/eMTC, Cat-NB1/NB-IoT, 2G/E-GPRS technologies and more than 15 RF bands, making it ideal for global IoT applications. When axis acceleration values change beyond the set range, an LED lights up.

The main objective of this project is to connect the ADXL345 sensor to the SPI interface of the MDM9206 chipset provided by the GoKit development board and collect three axis acceleration values. When axis acceleration values change beyond the set range, an LED lights up.

Parts Used

Below are the items used in this project.

  1. Win7 PC
  2. GoKit4 development board
  3. ADXL345 sensor, used to detect axis acceleration values
  4. LED, when axis acceleration values changes beyond the set range, the LED lights up
  5. DuPont cable, used to connect other components as wires

How does it work?

Below are some usage instructions to test the project.

Now let's introduce the demo-Smart-Motion-detector’s workflow.

  gagentMain---->sensorInit---->led_init---->motion_init---->tx_timer_create

Register a callback in the interface named "tx_timer_create", when time is up, call the callback "userTimerCB".

  demo-Smart-Motion-detector/main/main.c
  void gagentMain( void )
  {
      getFreeHeap();
      sensorInit();
      gizwitsInit();
  }

The function named "gagentMain" was called by "GAgent", the main role of GAgent is data forwarding, which is a data interaction bridge between device data, Wit Cloud, and the application. Function "sensorInit", does some sensor initialization:

  void sensorInit(void)
  {
      int32 ret = -1; 
      gizLog(LOG_INFO,"Sensor initialization ...\n"); 
    led_init();
    motion_init();
      txm_module_object_allocate(&userTimer, sizeof(TX_TIMER));
      ret = tx_timer_create(userTimer, "userTimer", userTimerCB, NULL, 1,
                            200, TX_AUTO_ACTIVATE);			
      if(ret != TX_SUCCESS)
      {
          gizLog(LOG_ERROR,"Failed to create UserTimer.\n");
      }
  }
  
  void led_init()
  {
    led_gpio_config();
    led_on_off(false,led_green);	    //init status is off
    led_on_off(true,GPIO_BLUE);		//init status is off
  }
  
  void motion_init()
  { 
    adxl345_init();
  }
  
  demo-Smart-Motion-detector/driver/adxl345/adxl345.c
  
  /*do some initialization about SPI and ADXL345 sensor.*/
  
  void adxl345_init()
  {
    static uint8 data;
    uint8 *data_addr;
    spi_init();
    data_addr = spi_reg_read(0x00);  //read device id;
    qapi_Timer_Sleep(10, QAPI_TIMER_UNIT_TICK, true);
    data = *data_addr;
    gizLog(LOG_INFO, "Device id is 0x%02x\n", data);
  
    spi_reg_write(0x31, 0x2B);	
    spi_reg_write(0x2C, 0x08); 	
    spi_reg_write(0x2D, 0x08); 	
    spi_reg_write(0x2E, 0x00); 	
    spi_reg_write(0x2F, 0x00); 	
    spi_reg_write(0x2E, 0x02); 	
    spi_reg_write(0x38, 0x9F); 
  }
  demo-Smart-Motion-detector/driver/spi/spi.c
  /*do SPI initialization*/
  void spi_init(void)
  {
      /*Get a handle to an SPI instance.*/
  qapi_SPIM_Open(QAPI_SPIM_INSTANCE_6_E, &spi_hdl); 
  
  /*Turn on all resources required for a successful SPI transaction.*/
      qapi_SPIM_Power_On(spi_hdl);
  
  /*spi interface config*/
  
  /*set the spi mode, determined by slave device*/
  spi_config.SPIM_Mode = QAPI_SPIM_MODE_3_E;                      
  
  /*set CS low as active, determined by slave device*/
  spi_config.SPIM_CS_Polarity = QAPI_SPIM_CS_ACTIVE_LOW_E;        
  
      spi_config.SPIM_endianness  = SPI_LITTLE_ENDIAN;
      spi_config.SPIM_Bits_Per_Word = 8;
  spi_config.SPIM_Slave_Index = 0;
  
  /*config spi clk about 1Mhz */
      spi_config.Clk_Freq_Hz = 1000000;                               
  spi_config.SPIM_CS_Mode = QAPI_SPIM_CS_KEEP_ASSERTED_E;
  
  /*don't care, set 0 is ok.*/
  spi_config.CS_Clk_Delay_Cycles = 0;    
  
  /*don't care, set 0 is ok.*/ 
      spi_config.Inter_Word_Delay_Cycles = 0;  
      spi_config.loopback_Mode = 0;
  }
  
  /*used to write data to registers*/
  
  qapi_Status_t spi_reg_write(uint8 reg, uint8 data)
  {
      qapi_Status_t res = QAPI_OK; 
      qapi_SPIM_Descriptor_t spi_desc;
      int num;
  
      tx_buf[0] = reg & 0x3f;
      tx_buf[1] = data;
  
      spi_desc.rx_buf = rx_buf;
      spi_desc.tx_buf = tx_buf;
      spi_desc.len = 2;
      spi_status.status = 0;
      spi_status.read_addr = NULL;
      spi_status.len = 1;
  
      /*used to performs a data transfer over the SPI bus */
      res = qapi_SPIM_Full_Duplex(spi_hdl, &spi_config, &spi_desc, 1, qapi_spi_cb_func, &spi_status, false);
  
      if (res != QAPI_OK)
          return res;
  
  for(num = 0; num  X_axis_Value ? old_axis_val.X - X_axis_Value:X_axis_Value - old_axis_val.X;
        tmp_y = old_axis_val.Y > Y_axis_Value ? old_axis_val.Y - Y_axis_Value:Y_axis_Value - old_axis_val.Y;
        tmp_z = old_axis_val.Z > Z_axis_Value ? old_axis_val.Z - Z_axis_Value:Z_axis_Value - old_axis_val.Z;
        gizLog(LOG_INFO, "tmp_x= %d, tmp_y = %d , tmp_z = %d\n",  tmp_x, tmp_y, tmp_z);
        if(tmp_x > X_AXIS_THRESHOLD || tmp_y > Y_AXIS_THRESHOLD || tmp_z > Z_AXIS_THRESHOLD)
        { 	
          led_on_off(true,led_green);	
          gizLog(LOG_INFO, "LED ON\n");
        }
        else
        {
          led_on_off(false, led_green);
          gizLog(LOG_INFO, "LED OFF\n");
        }
        old_axis_val.X =  X_axis_Value;
        old_axis_val.Y =  Y_axis_Value;
        old_axis_val.Z =  Z_axis_Value;
          }
      currentDataPoint.valueX_axis_Value = X_axis_Value;
      currentDataPoint.valueY_axis_Value = Y_axis_Value;
      currentDataPoint.valueZ_axis_Value = Z_axis_Value;
    }
      ctime++;  
  }
  
  demo-Smart-Motion-detector/driver/adxl345/adxl345.c
  
  /*used to get three axis acceleration values*/
  
  uint8_t getaxis(int32_t *X_axis_Value, int32_t *Y_axis_Value, int32_t *Z_axis_Value)
  {
    qapi_Status_t res = QAPI_ERROR;
    uint8_t data;
    uint8_t *data_addr;
    SENSOR_DATA_TypeDef axis_converted_avg;
  
    data_addr = spi_reg_read(0x30);
  
    if (data_addr == NULL) {
      gizLog(LOG_ERROR, "read 0x30 err\n");
      return GETAXIS_ERR;
    }
  
    qapi_Timer_Sleep(10, QAPI_TIMER_UNIT_TICK, true);
    data = *data_addr;
  
    if (data & 0x02)
    {
      res = ADXL345_READ_FIFO(&axis_converted_avg);
      if (res != QAPI_OK)
        return GETAXIS_ERR;
  
      *X_axis_Value = axis_converted_avg.X;
      *Y_axis_Value = axis_converted_avg.Y;
      *Z_axis_Value = axis_converted_avg.Z;
      return GETAXIS_OK;
    }
    else
    {
      return GETAXIS_NO_INT;
    }
  }
  
  demo-Smart-Motion-detector/driver/spi/spi.c
  
  /*used to read data from registers*/
  
  uint8 *spi_reg_read(uint8 reg)
  {
      qapi_Status_t res = QAPI_OK; 
      qapi_SPIM_Descriptor_t spi_desc;
      int num;
  
      tx_buf[0] = reg | 0x80 & 0xbf;
      tx_buf[1] = 0xff;
  
      rx_buf [0] = 0;
      rx_buf [1] = 0;
  
      spi_desc.rx_buf = rx_buf;
      spi_desc.tx_buf = tx_buf;
      spi_desc.len = 2;
      spi_status.status = 0;
      spi_status.read_addr = NULL;
      spi_status.len = 1;
  
  
      res = qapi_SPIM_Full_Duplex(spi_hdl, &spi_config, &spi_desc, 1, qapi_spi_cb_func, &spi_status, false);
  
      gizLog(LOG_INFO,"read spi end\n");
  
      if (res != QAPI_OK)
          return NULL;
  
      return rx_buf + 1;
  }
  
  demo-Smart-Motion-detector/driver/adxl345/adxl345.c
  
  /*used to get data from slave device*/
  
  qapi_Status_t ADXL345_READ_FIFO(SENSOR_DATA_TypeDef *axis_converted_avg)
  {
    uint16_t axis_raw_data[10][3];
    static uint8_t data_buf[6];
    uint8 *data_addr;
    SENSOR_DATA_TypeDef axis_converted[10];
  
    for (uint8_t i = 0; i 
  1. Download code from GitHub the repository: “https://github.com/ThunderSoft-XA/demo-Smart-Motion-detector
  2. Compile the code and flash the image to GoKit4 development kit.
  3. Connect the ADXL345 sensor to the SPI interface of the GoKit development board.
  4. Connect one pin of the LED to the D9 pin of the development board, and the other pin is connected to the Vcc.
  5. Connect the USB data cable between the PC and GoKit development board.
  6. Open the serial debugging assistant.
  7. Shake the ADXL345 sensor and you can see that the data has changed.

When axis acceleration values change beyond the set range, the LED will be lit.

Parts used for the GoKit4 development kit demo-Smart-Motion-detector

Parts used for the GoKit4 development kit demo-Smart-Motion-detector

NameEmailTitle/Company
Zhensunzhen@thundersoft.comThundersoft
Rongyangrong0925@thundersoft.comThundersoft
Jiewangjie0508@thundersoft.comThundersoft
Koukouzw0723@thundersoft.comThundersoft
Ericyansh0810@thundersoft.comThundersoft