示例#1
0
/**
 * Initializes all of the device systems. If the clear button
 * is held down at the end of the initialization then defaults
 * are restored to all EEPROM settings.
 *
 * @return Returns 'OK' if previous data was found by data_Init(), 'ERROR' otherwise.
 */
status init(void)
{
	Time time;
    int i;

    cli();

    user_Init();
    timer_Wait_MS(1000);
	// The serial port is now connected to the GSM modem, not the FTDI chip...
//	coms_Init();
    gsm_modem_Init();
    usart_Print_Num(SERIAL, UBRR1L);
    sensor_Init(true);
    data_Init();

    if (user_Get_Button(BUTTON_CLEAR))
    {

        for (i = 0; i < 50; i++)
        {
            timer_Wait_MS(50);
            user_Toggle_LED(LED_CLEAR);
            user_Toggle_LED(LED_STOP);
            user_Toggle_LED(LED_START);
            user_Toggle_LED(LED_POWER);
        }

        adx_Calibrate();
        rtc_Init();
        data_Reset_EEPROM();

        wdt_enable(WDTO_120MS);
    }

    // Hard-coded time and date for now since we don't have
	// an application to set the time with...
	// We should really try to get this from the GSM modem
	time.hours = 12; // set to noon
	time.minutes = 0;
	time.seconds = 0;
	
	time.month = 2; // March
	time.dow = 5;   // Thursday
	time.year = 12; // 2012
	time.date = 1; // The 1st
	rtc_Set_Time(&time);

    sei();
    return OK;
}
示例#2
0
/**
 * Initializes all of the device systems. If the clear button
 * is held down at the end of the initialization then defaults
 * are restored to all EEPROM settings.
 *
 * @return Returns 'OK' if previous data was found by data_Init(), 'ERROR' otherwise.
 */
status init(void)
{
    int i;

    cli();

    user_Init();
    timer_Wait_MS(1000);
    coms_Init();
    sensor_Init(true);
    
    data_Init();

    if (user_Get_Button(BUTTON_CLEAR))
    {

        for (i = 0; i < 50; i++)
        {
            timer_Wait_MS(50);
            user_Toggle_LED(LED_CLEAR);
            user_Toggle_LED(LED_STOP);
            user_Toggle_LED(LED_START);
            user_Toggle_LED(LED_POWER);
        }

        adx_Calibrate();
        rtc_Init();
        data_Reset_EEPROM();

        wdt_enable(WDTO_120MS);
    }

    sei();

    return OK;
}
int main(void)
{
     
    /* Clock initialization */
    SystemInit();

    /* test system initialization */
    test_Init();
        //adi_gpio_OutputEnable(EN_5V, true);
        //adi_gpio_SetHigh(EN_5V);
    
  /*    SWITCHED TO LOW POWER MODE - ACTIVE MODE    */
  pwrResult = adi_pwr_EnterLowPowerMode(ADI_PWR_MODE_ACTIVE,NULL,0x00);  //Low Power Active mode
  DEBUG_RESULT("\n Failed to enter active mode %04d",pwrResult,ADI_PWR_SUCCESS);
  /*    BUCK CONVERTER ENABLED TO REDUCE POWER      */
  adi_pwr_EnableHPBuck(true); 
  
  NUM_FAN_500MS_CYCLES = fanOnTime/0.5;//Number of 500ms cycles equals ratio of given fanOnTime to 0.5 
  
    do 
    {
      if(ADI_PWR_SUCCESS != adi_pwr_Init())
        {
          DEBUG_MESSAGE("Failed to intialize the power service\n");
          break;
        }
      
      if(ADI_PWR_SUCCESS != adi_pwr_SetLFClockMux(ADI_CLOCK_MUX_LFCLK_LFXTAL))
      {
        return(eResult);
      }
      
      if(ADI_PWR_SUCCESS != adi_pwr_EnableClockSource(ADI_CLOCK_SOURCE_HFXTAL, true))
      {
        return(eResult);
      }

      if(ADI_PWR_SUCCESS != adi_pwr_SetRootClockMux(ADI_CLOCK_MUX_ROOT_HFXTAL))
      {
        return(eResult);
      }

      
      if(ADI_PWR_SUCCESS != adi_pwr_EnableClockSource(ADI_CLOCK_SOURCE_LFXTAL,true))
      {
        return(eResult);
      }
   
      if (ADI_PWR_SUCCESS != adi_pwr_SetClockDivider(ADI_CLOCK_HCLK,1))
      {
          DEBUG_MESSAGE("Failed to intialize the power service\n");
      }
      if (ADI_PWR_SUCCESS != adi_pwr_SetClockDivider(ADI_CLOCK_PCLK,1))
      {
          DEBUG_MESSAGE("Failed to intialize the power service\n");
      }

  
      if(ADI_RTC_SUCCESS !=rtc_Init())
        {
          DEBUG_MESSAGE("\nFailed to initialize RTC device \n");
        }
      

      
      if(ADI_GPIO_SUCCESS != adi_gpio_Init(gpioMemory, ADI_GPIO_MEMORY_SIZE))
        {
            DEBUG_MESSAGE("adi_gpio_Init failed\n");
            break;
        }
      //P0.13 --> LED3
      adi_gpio_OutputEnable(LED3, true);
      //P1.12 --> LED4 
      adi_gpio_OutputEnable(LED4, true);
      adi_gpio_OutputEnable(CO_HEATER, true);
      adi_gpio_OutputEnable(CO_SENSE, true);
      adi_gpio_OutputEnable(PM25_LED, true);
      adi_gpio_OutputEnable(PM25_FAN, true);
      adi_gpio_OutputEnable(DBG_ST8_PIN, true);
      adi_gpio_OutputEnable(DBG_ADC_PIN, true);

      adi_gpio_SetLow(CO_HEATER);
      adi_gpio_SetLow(CO_SENSE);
      adi_gpio_SetLow(PM25_LED);
      adi_gpio_SetLow(PM25_FAN);
      adi_gpio_SetLow(DBG_ADC_PIN);   
      adi_gpio_SetHigh(LED3);
      adi_gpio_SetHigh(LED4);
      
      ADC_Setup();
      
      adi_tmr_Open(TIMER_DEVICE_1,aDeviceMemory1,ADI_TMR_MEMORY_SIZE,&hDevice1); 
      adi_tmr_RegisterCallback( hDevice1, GPTimer1Callback ,hDevice1);
      
      adi_tmr_SetPrescaler(hDevice1, ADI_GPT_PRESCALER_256);
      adi_tmr_SetLoadValue( hDevice1, GPT1_LOAD_1SEC);
     
      DEBUG_MESSAGE("AQ Sensor initializing!\n");    
        
    }while(0);
    
    do
    {}
    while(1);
    
    
 
}
示例#4
0
void sys_Init()
{

	//创建系统守护定时(1S)
#if OS_TYPE
	rt_timer_init(&timer_daemon, "daemon", sys_Daemon, NULL, 100 / OS_TICK_MS, RT_TIMER_FLAG_PERIODIC);
	rt_timer_start(&timer_daemon);
#endif

#if OS_TYPE
	buf_Init();
#endif

#if OS_QUEUE_QTY
	os_que_Init();
#endif

#if IRQ_ENABLE
	irq_Init();
#endif

#if BATTERY_ENABLE
	bat_On();
#endif

//-------------------------------------------------------------------------
//GPIO Functions
//-------------------------------------------------------------------------
#if PULSE_COL_ENABLE
	pulse_Init();
#endif
#if HT1621_ENABLE
	ht1621_Init();
#endif

//-------------------------------------------------------------------------
//External Parallel Interface Functions
//-------------------------------------------------------------------------
#if NANDFLASH_ENABLE
	nand_Init();
#endif

//-------------------------------------------------------------------------
//I2C Interface Functions
//-------------------------------------------------------------------------
#if PCA955X_ENABLE
	pca955x_Init();
#endif

//-------------------------------------------------------------------------
//SPI Interface Functions
//-------------------------------------------------------------------------
#if SC16IS7X_ENABLE
	sc16is7x_Init();
#endif
#if ATT7022_ENABLE
	att7022_Init();
#endif
#if NRSEC3000_ENABLE
	nrsec3000_Init();
#endif
#if VOICE_ENABLE
	wtv_Init();
#endif
#if MIFARE_ENABLE
	mf_InitGpio();
#endif

//-------------------------------------------------------------------------
//UART Interface Functions
//-------------------------------------------------------------------------
#if VK321X_ENABLE
	vk321x_Init();
#endif
#if TDK6515_ENABLE
	tdk6515_Init();
#endif
#if MODEM_ENABLE
	modem_Init();
#endif

//-------------------------------------------------------------------------
//Real Time Clock Functions
//-------------------------------------------------------------------------
#if RTC_ENABLE
	rtc_Init();
#endif

//-------------------------------------------------------------------------
//Backup Functions
//-------------------------------------------------------------------------
#if BKP_ENABLE
	bkp_Init();
#endif

//-------------------------------------------------------------------------
//Flash System Functions
//-------------------------------------------------------------------------
#if INTFLASH_ENABLE
	intf_Init();
#endif
#if NORFLASH_ENABLE
	norf_Init();
#endif
#if SPIFLASH_ENABLE
	spif_Init();
#endif
#if FLASH_ENABLE
	flash_Init();
#endif


//-------------------------------------------------------------------------
//File System Functions
//-------------------------------------------------------------------------
#if FS_ENABLE
	fs_init();
#endif

//-------------------------------------------------------------------------
//USB Interface Functions
//-------------------------------------------------------------------------
#if USB_ENABLE
	usb_Init();
#endif


//-------------------------------------------------------------------------
//Register Subsystem Functions
//-------------------------------------------------------------------------
#if REGISTER_ENABLE
	reg_Init();
#endif



//-------------------------------------------------------------------------
//Network Subsystem Functions
//-------------------------------------------------------------------------
#if TCPPS_ENABLE
	net_Init();
#endif

//-------------------------------------------------------------------------
//In Application Programming Functions
//-------------------------------------------------------------------------
#if GDFTS_ENABLE
	gdfts_Init();
#endif

#if GUI_ENABLE
	gui_Init();
#endif

#if OS_TYPE
	//创建系统IO处理线程
#if SYS_TIMER_ENABLE
	rt_timer_init(&timer_IOHandle, "sysio", sys_IOHandle, NULL, 1, RT_TIMER_FLAG_PERIODIC);
	rt_timer_start(&timer_IOHandle);
#else
	os_thd_Create(SysIo, 240);
#endif

	//创建应用层线程
	app_Entry();

	//系统维护线程
	sys_Maintain();
#endif
}
int main(void)
{
     
    /* Clock initialization */
    SystemInit();

    /* test system initialization */
    test_Init();
        //adi_gpio_OutputEnable(EN_5V, true);
        //adi_gpio_SetHigh(EN_5V);
        
    do 
    {
      if(ADI_PWR_SUCCESS != adi_pwr_Init())
        {
          DEBUG_MESSAGE("Failed to intialize the power service\n");
          break;
        }
      
      if(ADI_PWR_SUCCESS != adi_pwr_SetLFClockMux(ADI_CLOCK_MUX_LFCLK_LFXTAL))
      {
        return(eResult);
      }
      
      if(ADI_PWR_SUCCESS != adi_pwr_EnableClockSource(ADI_CLOCK_SOURCE_HFXTAL, true))
      {
        return(eResult);
      }

      if(ADI_PWR_SUCCESS != adi_pwr_SetRootClockMux(ADI_CLOCK_MUX_ROOT_HFXTAL))
      {
        return(eResult);
      }

      
      if(ADI_PWR_SUCCESS != adi_pwr_EnableClockSource(ADI_CLOCK_SOURCE_LFXTAL,true))
      {
        return(eResult);
      }
   
      if (ADI_PWR_SUCCESS != adi_pwr_SetClockDivider(ADI_CLOCK_HCLK,1))
      {
          DEBUG_MESSAGE("Failed to intialize the power service\n");
      }
      if (ADI_PWR_SUCCESS != adi_pwr_SetClockDivider(ADI_CLOCK_PCLK,1))
      {
          DEBUG_MESSAGE("Failed to intialize the power service\n");
      }

  
      if(ADI_RTC_SUCCESS !=rtc_Init())
        {
          DEBUG_MESSAGE("\nFailed to initialize RTC device \n");
        }
      

      
      if(ADI_GPIO_SUCCESS != adi_gpio_Init(gpioMemory, ADI_GPIO_MEMORY_SIZE))
        {
            DEBUG_MESSAGE("adi_gpio_Init failed\n");
            break;
        }
      //P0.13 --> LED3
      adi_gpio_OutputEnable(LED3, true);
      //P1.12 --> LED4 
      adi_gpio_OutputEnable(LED4, true);
      adi_gpio_OutputEnable(CO_HEATER, true);
      adi_gpio_OutputEnable(CO_SENSE, true);
      adi_gpio_OutputEnable(PM25_LED, true);
      adi_gpio_OutputEnable(PM25_FAN, true);
      adi_gpio_OutputEnable(DBG_ST8_PIN, true);
      adi_gpio_OutputEnable(DBG_ADC_PIN, true);

      adi_gpio_SetLow(CO_HEATER);
      adi_gpio_SetLow(CO_SENSE);
      adi_gpio_SetLow(PM25_LED);
      adi_gpio_SetLow(PM25_FAN);
      adi_gpio_SetLow(DBG_ADC_PIN);   
      adi_gpio_SetHigh(LED3);
      adi_gpio_SetHigh(LED4);
      
      ADC_Setup();
      
      adi_tmr_Open(TIMER_DEVICE_1,aDeviceMemory1,ADI_TMR_MEMORY_SIZE,&hDevice1); 
      adi_tmr_RegisterCallback( hDevice1, GPTimer1Callback ,hDevice1);
      
      adi_tmr_SetPrescaler(hDevice1, ADI_GPT_PRESCALER_256);
      adi_tmr_SetLoadValue( hDevice1, GPT1_LOAD_1SEC);
     
      DEBUG_MESSAGE("AQ Sensor initializing!\n");    
        
    }while(0);
    
    do
    {}
    while(1);
    
    
 
}
示例#6
0
void MainTask(void *arg) {
  ADI_AFE_DEV_HANDLE hDevice;
  int16_t dft_results[DFT_RESULTS_COUNT];
  q15_t dft_results_q15[DFT_RESULTS_COUNT];
  q31_t dft_results_q31[DFT_RESULTS_COUNT];
  q31_t magnitude[DFT_RESULTS_COUNT / 2];
  q15_t phase[DFT_RESULTS_COUNT / 2];
  fixed32_t magnitude_result[DFT_RESULTS_COUNT / 2 - 1];
  fixed32_t phase_result[DFT_RESULTS_COUNT / 2 - 1];
  char msg[MSG_MAXLEN];
  uint8_t err;
  done = 0;
  uint16_t pressure_analog;
  uint32_t pressure;
  nummeasurements = 0;
  uint32_t rtcCount;
  ADI_I2C_RESULT_TYPE i2cResult;

  // Initialize driver.
  rtc_Init();

  // Calibrate.
  rtc_Calibrate();

  // Initialize UART.
  if (uart_Init()) {
    FAIL("ADI_UART_SUCCESS");
  }

  // Initialize I2C.
  i2c_Init(&i2cDevice);
  
  // Initialize flags.
  bRtcAlarmFlag = bRtcInterrupt = bWdtInterrupt = false;

  // Get the current count.
  if (adi_RTC_GetCount(hRTC, &rtcCount)) {
    FAIL("adi_RTC_GetCount failed");
  }

  // Initialize the AFE API.
  if (adi_AFE_Init(&hDevice)) {
    FAIL("adi_AFE_Init");
  }

  // AFE power up.
  if (adi_AFE_PowerUp(hDevice)) {
    FAIL("adi_AFE_PowerUp");
  }

  // Excitation Channel Power-up.
  if (adi_AFE_ExciteChanPowerUp(hDevice)) {
    FAIL("adi_AFE_ExciteChanPowerUp");
  }

  // TIA Channel Calibration.
  if (adi_AFE_TiaChanCal(hDevice)) {
    FAIL("adi_AFE_TiaChanCal");
  }

  // Excitation Channel Calibration (Attenuation Enabled).
  if (adi_AFE_ExciteChanCalAtten(hDevice)) {
    FAIL("adi_AFE_ExciteChanCalAtten");
  }

  // Update FCW in the sequence.
  seq_afe_acmeas2wire[3] = SEQ_MMR_WRITE(REG_AFE_AFE_WG_FCW, FCW);
  // Update sine amplitude in the sequence.
  seq_afe_acmeas2wire[4] =
      SEQ_MMR_WRITE(REG_AFE_AFE_WG_AMPLITUDE, SINE_AMPLITUDE);

  // Recalculate CRC in software for the AC measurement, because we changed.
  // FCW and sine amplitude settings.
  adi_AFE_EnableSoftwareCRC(hDevice, true);

  // Perform the impedance measurement.
  if (adi_AFE_RunSequence(hDevice, seq_afe_acmeas2wire, (uint16_t *)dft_results,
                          DFT_RESULTS_COUNT)) {
    FAIL("Impedance Measurement");
  }

  // Set RTC alarm.
  printf("rtcCount: %d\r\n", rtcCount);
  if (ADI_RTC_SUCCESS != adi_RTC_SetAlarm(hRTC, rtcCount + 120)) {
    FAIL("adi_RTC_SetAlarm failed");
  }

  // Enable RTC alarm.
  if (ADI_RTC_SUCCESS != adi_RTC_EnableAlarm(hRTC, true)) {
    FAIL("adi_RTC_EnableAlarm failed");
  }

  // Read the initial impedance.
  q31_t magnitudecal;
  q15_t phasecal;

  convert_dft_results(dft_results, dft_results_q15, dft_results_q31);
  arm_cmplx_mag_q31(dft_results_q31, &magnitudecal, 2);

  phasecal = arctan(dft_results[1], dft_results[0]);

  printf("raw rcal data: %d, %d\r\n", dft_results[1], dft_results[0]);
  printf("rcal (magnitude, phase) = (%d, %d)\r\n", magnitudecal, phasecal);

  // Create the message queue for communicating between the ISR and this task.
  dft_queue = OSQCreate(&dft_queue_msg[0], DFT_QUEUE_SIZE);

  // Hook into the DFT interrupt.
  if (ADI_AFE_SUCCESS !=
      adi_AFE_RegisterAfeCallback(
          hDevice, ADI_AFE_INT_GROUP_CAPTURE, AFE_DFT_Callback,
          BITM_AFE_AFE_ANALOG_CAPTURE_IEN_DFT_RESULT_READY_IEN)) {
    FAIL("adi_AFE_RegisterAfeCallback");
  }
  if (ADI_AFE_SUCCESS !=
      adi_AFE_ClearInterruptSource(
          hDevice, ADI_AFE_INT_GROUP_CAPTURE,
          BITM_AFE_AFE_ANALOG_CAPTURE_IEN_DFT_RESULT_READY_IEN)) {
    FAIL("adi_AFE_ClearInterruptSource (1)");
  }

  packed32_t q_result;
  void *q_result_void;
  OS_Q_DATA q_data;
  uint16_t q_size;
  bool inflated = false;
  while (true) {
    // Wait for the user to press the button.
    printf("MainTask: waiting for button.\n");
    OSSemPend(ux_button_semaphore, 0, &err);
    if (err != OS_ERR_NONE) {
      FAIL("OSSemPend: MainTask");
    }
    
    // TODO: fix bug when pressing button multiple times.
    // Have the pump task inflate the cuff.
    printf("MainTask: button detected. Resuming pump task.\n");
    err = OSTaskResume(TASK_PUMP_PRIO);
    if (err != OS_ERR_NONE) {
      FAIL("OSTaskResume: MainTask (1)");
    }
    
    // Wait a bit.
    printf("MainTask: waiting a bit.\n");
    err = OSTimeDlyHMSM(0, 0, 1, 0);
    if (err != OS_ERR_NONE) {
      FAIL("OSTimeDlyHMSM: MainTask (3)");
    }
    
    // Enable the DFT interrupt.
    printf("MainTask: enabling DFT interrupt.\n");
    if (ADI_AFE_SUCCESS !=
        adi_AFE_EnableInterruptSource(
            hDevice, ADI_AFE_INT_GROUP_CAPTURE,
            BITM_AFE_AFE_ANALOG_CAPTURE_IEN_DFT_RESULT_READY_IEN, true)) {
      FAIL("adi_AFE_EnableInterruptSource");
    }
    
    PRINT("START\r\n");
    printf("START\r\n");

    while (true) {
      // Wait on the queue to get DFT data from the ISR (~76 Hz).
      //printf("MainTask: pending on DFT queue.\n");
      q_result_void = OSQPend(dft_queue, 0, &err);
      q_result.pointer = q_result_void;
      if (err != OS_ERR_NONE) {
        FAIL("OSQPend: dft_queue");
      }
      OSQQuery(dft_queue, &q_data);
      q_size = q_data.OSNMsgs;
    
      // Right after we get this data, get the transducer's value from the
      // Arduino.
      //printf("MainTask: getting transducer value via I2C.\n");
      i2cResult = adi_I2C_MasterReceive(i2cDevice, I2C_PUMP_SLAVE_ADDRESS, 0x0,
                                        ADI_I2C_8_BIT_DATA_ADDRESS_WIDTH,
                                        i2c_rx, 3, false);
      if (i2cResult != ADI_I2C_SUCCESS) {
        FAIL("adi_I2C_MasterReceive: get pressure from Arduino");
      }

      // Get the analog pressure value from the Arduino.
      if (i2c_rx[0] == ARDUINO_PRESSURE_AVAILABLE
          || i2c_rx[0] == ARDUINO_STILL_INFLATING) {
        pressure_analog = i2c_rx[1] | (i2c_rx[2] << 8);
      } else {
        FAIL("Corrupted or unexpected data from Arduino.");
      }
      
      // Convert the analog value to mmHg.
      pressure = transducer_to_mmhg(pressure_analog);
      //printf("MainTask: got pressure value: %d mmHg.\n", pressure);
      
      // If the pressure is below the threshold, we're done; break the loop.
      if (inflated && pressure < LOWEST_PRESSURE_THRESHOLD_MMHG) {
        PRINT("END\r\n");
        printf("END\r\n");
        inflated = false;
        break;
      } else if (pressure > LOWEST_PRESSURE_THRESHOLD_MMHG * 1.1) {
        inflated = true;
      }

      // Convert DFT results to 1.15 and 1.31 formats.
      dft_results[0] = q_result.parts.magnitude;
      dft_results[1] = q_result.parts.phase;
      convert_dft_results(dft_results, dft_results_q15, dft_results_q31);

      // Compute the magnitude using CMSIS.
      arm_cmplx_mag_q31(dft_results_q31, magnitude, DFT_RESULTS_COUNT / 2);

      // Calculate final magnitude values, calibrated with RCAL.
      fixed32_t magnituderesult;
      magnituderesult = calculate_magnitude(magnitudecal, magnitude[0]);
      q15_t phaseresult;
      phaseresult = arctan(dft_results[1], dft_results[0]);
      fixed32_t phasecalibrated;

      // Calibrate with phase from rcal.
      phasecalibrated = calculate_phase(phasecal, phaseresult);
      
      // TODO: dispatch to another thread?
      //printf("MainTask: sending data via UART.\n");;
      print_PressureMagnitudePhase("", pressure, magnituderesult, phasecalibrated,
                                   q_size);
      nummeasurements++;
    }

    
    // We're done measuring, for now. Disable the DFT interrupts.
    printf("MainTask: disabling DFT interrupts.\n");
    if (ADI_AFE_SUCCESS !=
        adi_AFE_EnableInterruptSource(
            hDevice, ADI_AFE_INT_GROUP_CAPTURE,
            BITM_AFE_AFE_ANALOG_CAPTURE_IEN_DFT_RESULT_READY_IEN, false)) {
      FAIL("adi_AFE_EnableInterruptSource (false)");
    }
    
    // Tell the pump task to deflate the cuff.
    printf("MainTask: resuming pump task to deflate the cuff.\n");
    err = OSTaskResume(TASK_PUMP_PRIO);
    if (err != OS_ERR_NONE) {
      FAIL("OSTaskResume: MainTask (2)");
    }

    // Suspend until the pump finishes deflating. We can then go back to
    // listening for the user input.
    printf("MainTask: suspending to wait for pump task.\n");
    err = OSTaskSuspend(OS_PRIO_SELF);
    if (err != OS_ERR_NONE) {
      FAIL("OSTaskSuspend: MainTask (3)");
    }

    // Tell the UX that we're done for now.
    UX_Disengage();
  }
}