Ejemplo n.º 1
0
/*!
 * @Brief enable the trigger source of LPTimer
 */
void init_trigger_source(uint32_t adcInstance)
{
    uint32_t freqUs;

    lptmr_user_config_t lptmrUserConfig =
    {
        .timerMode = kLptmrTimerModeTimeCounter,
        .freeRunningEnable = false,
        .prescalerEnable = false, // bypass perscaler
#if (CLOCK_INIT_CONFIG == CLOCK_VLPR)
        // use MCGIRCCLK, 4M or 32KHz
        .prescalerClockSource = kClockLptmrSrcMcgIrClk,
#else
        // Use LPO clock 1KHz
        .prescalerClockSource = kClockLptmrSrcLpoClk,
#endif
        .isInterruptEnabled = false
    };

    // Init LPTimer driver
    LPTMR_DRV_Init(0, &gLPTMRState, &lptmrUserConfig);

    // Set the LPTimer period
    freqUs = 1000000U/(INPUT_SIGNAL_FREQ*NR_SAMPLES)*2;
    LPTMR_DRV_SetTimerPeriodUs(0, freqUs);

    // Start the LPTimer
    LPTMR_DRV_Start(0);

    // Configure SIM for ADC hw trigger source selection
#if defined(KM34Z7_SERIES)
    SIM_HAL_EnableClock(gSimBase[0], kSimClockGateXbar0);
    SIM_HAL_SetAdcTrgSelMode(gSimBase[0], kSimAdcTrgSelXbar);
    XBAR_DRV_ConfigSignalConnection(kXbaraInputLPTMR0_Output, kXbaraOutputADC_TRGA);
#else
    SIM_HAL_SetAdcAlternativeTriggerCmd(gSimBase[0], adcInstance, true);
    SIM_HAL_SetAdcPreTriggerMode(gSimBase[0], adcInstance, kSimAdcPretrgselA);
    SIM_HAL_SetAdcTriggerMode(gSimBase[0], adcInstance, kSimAdcTrgSelLptimer);
#endif
}

/*!
 * @Brief disable the trigger source
 */
void deinit_trigger_source(uint32_t adcInstance)
{
    LPTMR_DRV_Stop(0);
    LPTMR_DRV_Deinit(0);
}
Ejemplo n.º 2
0
/*lint -save  -e970 Disable MISRA rule (6.3) checking. */
int main(void)
/*lint -restore Enable MISRA rule (6.3) checking. */
{
  /* Write your local variable definition here */
   uint32_t      currentCounter = 0;
  /*** Processor Expert internal initialization. DON'T REMOVE THIS CODE!!! ***/
  PE_low_level_init();
  /*** End of Processor Expert internal initialization.                    ***/
  hw_average_config.hwAverageCountMode = kAdc16HwAverageCountOf32;      //enable hardware average.
  ADC16_DRV_ConfigHwAverage(FSL_ADCONV1,&hw_average_config);
  ADC16_DRV_GetAutoCalibrationParam(FSL_ADCONV1,&calibration_param);

  calibrateParams();
  //ADC16_DRV_Init(FSL_ADCONV1, &adConv1_InitConfig0);

  LPTMR_DRV_SetTimerPeriodUs(FSL_LPTMR1,1000000);  // Set lptmr period
  printf("\r\nLPTMR is running!!\r\n");
  LPTMR_DRV_Start(FSL_LPTMR1);
  /* Write your code here */
  /* For example: for(;;) { } */
  while(1)
  {
      if(currentCounter != lptmrCounter)
      {
          currentCounter = lptmrCounter;
          printf("\r\nLPTMR interrupt No.%d \r\n",currentCounter);
          GPIO_DRV_TogglePinOutput(LEDRGB_GREEN);
      }
  }

  /*** Don't write any code pass this line, or it will be deleted during code generation. ***/
  /*** RTOS startup code. Macro PEX_RTOS_START is defined by the RTOS component. DON'T MODIFY THIS CODE!!! ***/
  #ifdef PEX_RTOS_START
    PEX_RTOS_START();                  /* Startup of the selected RTOS. Macro is defined by the RTOS component. */
  #endif
  /*** End of RTOS startup code.  ***/
  /*** Processor Expert end of main routine. DON'T MODIFY THIS CODE!!! ***/
  for(;;){}
  /*** Processor Expert end of main routine. DON'T WRITE CODE BELOW!!! ***/
} /*** End of main routine. DO NOT MODIFY THIS TEXT!!! ***/
Ejemplo n.º 3
0
/*!
 * @Brief enable the trigger source of LPTimer
 */
void init_trigger_source(uint32_t adcInstance)
{
    uint32_t freqUs;
    
    lptmr_user_config_t lptmrUserConfig =
    {
        .timerMode = kLptmrTimerModeTimeCounter,
        .freeRunningEnable = false,
        .prescalerEnable = false, // bypass perscaler
        .prescalerClockSource = kClockLptmrSrcMcgIrClk, // use MCGIRCCLK, 4M or 32KHz
        .isInterruptEnabled = false
    };
    
    // Init LPTimer driver
    LPTMR_DRV_Init(0, &lptmrUserConfig, &gLPTMRState);

    // Set the LPTimer period
    freqUs = 1000000U/(INPUT_SIGNAL_FREQ*NR_SAMPLES)*2;
    LPTMR_DRV_SetTimerPeriodUs(0, freqUs);

    // Start the LPTimer
    LPTMR_DRV_Start(0);

    // Configure SIM for ADC hw trigger source selection
    SIM_HAL_SetAdcAlternativeTriggerCmd(gSimBase[0], adcInstance, true);
    SIM_HAL_SetAdcPreTriggerMode(gSimBase[0], adcInstance, kSimAdcPretrgselA);
    SIM_HAL_SetAdcTriggerMode(gSimBase[0], adcInstance, kSimAdcTrgSelLptimer);
}

/*!
 * @Brief disable the trigger source
 */
void deinit_trigger_source(uint32_t adcInstance)
{
    LPTMR_DRV_Stop(0);
    LPTMR_DRV_Deinit(0);
}
Ejemplo n.º 4
0
void Application()
{
	uint32_t minutes=0, hours=0, samples=0, arrayIndex=0;
	static state_t currentState = RECEIVE_CONFIG;
	static uint32_t sendPeriodHours, samplesPerHour,minutesLeaveIdle;
	static uint32_t distanceSamplesArray[MAX_ALLOWED_SEND_PERIOD_HOURS];
	static message_t messageType = SAMPLES;
	static uint16_t distance;
	static uint8_t HTTP_BUFFER[256];
	static float temperature;
	static SIM800L_error_t exitCode;
	static MMA8451_state_t boardState;
	uint8_t fullAlarmSent = 0, fireAlarmSent = 0, fallAlarmSent = 0;

	while(1)
	{
		switch(currentState)
		{
			case RECEIVE_CONFIG:
				RECEIVE_CONFIG_TASK(&sendPeriodHours,&samplesPerHour,&minutesLeaveIdle);
				currentState = IDLE;
				break;

			case IDLE:
				LPTMR_DRV_SetTimerPeriodUs(LPTMR_0_IDX,LPTMR_INTERRUPT_PERIOD_US);
				LPTMR_DRV_Start(LPTMR_0_IDX);

				CONSOLE_INIT();
				CONSOLE_SEND("TO IDLE...\r\n",12);

				/*PROCESSOR IN LOW POWER MODE*/
				POWER_SYS_SetWakeupModule(kLlwuWakeupModule0,true);
				if( POWER_SYS_SetMode(1,kPowerManagerPolicyAgreement) != kPowerManagerSuccess)
				{
					GPIO_DRV_ClearPinOutput(LEDRGB_RED);
				}

				/*AFTER LLWU INTERRUPT*/
				GPIO_DRV_TogglePinOutput(LEDRGB_BLUE);
				minutes++;
				currentState = MEASURE_TEMPERATURE;
				break;

			case MEASURE_TEMPERATURE:				/*EVERY MINUTE*/
				LM35_INIT();
				temperature = LM35_GET_TEMPERATURE_CELSIUS();
				LM35_DEINIT();
				MMA8451Q_INIT();



				/*TIME TO MEASURE DISTANCE*/
				if(minutes == minutesLeaveIdle)
				{
					minutes=0;
					currentState = MEASURE_DISTANCE;
				}
				else
				{
					currentState = IDLE;
				}

				/*CONTAINER FALL*/
				if  ( (boardState = MMA8451_GET_STATE(MMA8451_VERTICAL)) == MMA8451_FALL)
				{
					if(!fallAlarmSent)
					{
						messageType = FALL_ALARM;
						currentState = SEND_DATA;
					}
				}
				else
				{
					/*CONTAINER AGAIN IN ITS RIGHT PLACE*/
					if(fallAlarmSent)
					{
						fallAlarmSent = 0;
					}
				}

				/*EXTREME CASE: SET FIRE ALARM*/
				if(temperature > TEMPERATURE_THRESHOLD)
				{
					if(!fireAlarmSent)
					{
						messageType = FIRE_ALARM;
						currentState = SEND_DATA;
					}
				}
				else
				{	/*TEMPERATURE WENT DOWN*/
					if(fireAlarmSent)
					{
						fireAlarmSent = 0;
					}
				}
				break;

			case MEASURE_DISTANCE:
				MB7360_INIT();
				MB7360_START_RANGING();
				distance = MB7360_GET_DISTANCE_MM();
				MB7360_DEINIT();

				/*ONE HOUR LAPSE*/
				if (++samples == samplesPerHour)
				{
					samples = 0;
					distanceSamplesArray[arrayIndex++] = distance;
					/*SEND PERIOD HOUR LAPSE: TIME TO SEND DATA*/
					if(++hours == sendPeriodHours)
					{
						hours = 0;
						arrayIndex = 0;
						LPTMR_DRV_Stop(LPTMR_0_IDX);
						messageType = SAMPLES;
						currentState = SEND_DATA;
					}
					else
					{
						currentState = IDLE;
					}
				}
				else
				{
					currentState = IDLE;
				}

				/*EXTREME CASE: SET FULL ALARM*/
				if(distance < DISTANCE_THRESHOLD)
				{
					if(!fullAlarmSent)
					{
						messageType = FULL_ALARM;
						currentState = SEND_DATA;
					}
				}
				else
				{	/*COINTAINER EMPTY AGAIN, PREPARE FOR NEXT FULL ALARM*/
					if(fullAlarmSent)
					{
						fullAlarmSent = 0;
					}
				}
				break;

			case SEND_DATA:
				switch( exitCode = SEND_DATA_GPRS_TASK(messageType, distanceSamplesArray,sendPeriodHours) )
				{
					case SIM800L_SUCCESS_GPRS:
						switch(messageType)
						{
							 case SAMPLES:
								 CONSOLE_SEND("SAMPLES SENT - GPRS\r\n",21);
								 break;

							 case FULL_ALARM:
								 CONSOLE_SEND("FULL ALARM SENT - GPRS\r\n",24);
								 fullAlarmSent = 1;
								 break;

							 case FIRE_ALARM:
								 CONSOLE_SEND("FIRE ALARM SENT - GPRS\r\n",24);
								 fireAlarmSent = 1;
								 break;

							 case FALL_ALARM:
								 CONSOLE_SEND("FALL ALARM SENT - GPRS\r\n",24);
								 fallAlarmSent = 1;
								 break;

							 default:
								 break;
						 }
						 break;

					case SIM800L_NO_GPRS:
						switch( exitCode = SEND_DATA_SMS_TASK(messageType, distanceSamplesArray,sendPeriodHours))
						{
							case SIM800L_SUCCESS_SMS:
								switch(messageType)
								{
									 case SAMPLES:
										 CONSOLE_SEND("SAMPLES SENT - SMS\r\n",2);
										 break;

									 case FULL_ALARM:
										 CONSOLE_SEND("FULL ALARM SENT - SMS\r\n",23);
										 fullAlarmSent = 1;
										 break;

									 case FIRE_ALARM:
										 CONSOLE_SEND("FIRE ALARM SENT - SMS\r\n",23);
										 fireAlarmSent = 1;
										 break;

									 case FALL_ALARM:
										 CONSOLE_SEND("FALL ALARM SENT - SMS\r\n",23);
										 fallAlarmSent = 1;
										 break;

									 default:
										 break;
								}
								break;

							default:
								CONSOLE_SEND("COULD NOT SEND SMS\r\n",20);
								break;
						}
						break;

					/*NO GPRS OR SMS*/
					default:
						CONSOLE_SEND("COULD NOT SEND DATA\r\n",21);
						break;
				}
				currentState = IDLE;
				break;

			default:
				break;
		}
	}
}
Ejemplo n.º 5
0
/*!
 * @brief Main function
 */
int main (void)
{
    /* enable clock for PORTs */
    CLOCK_SYS_EnablePortClock(PORTA_IDX);
    //CLOCK_SYS_EnablePortClock(PORTB_IDX);
    CLOCK_SYS_EnablePortClock(PORTC_IDX);
    CLOCK_SYS_EnablePortClock(PORTD_IDX);
    CLOCK_SYS_EnablePortClock(PORTE_IDX);

    /* Set allowed power mode, allow all. */
    SMC_HAL_SetProtection(SMC, kAllowPowerModeAll);

    /* Set system clock configuration. */
    CLOCK_SYS_SetConfiguration(&g_defaultClockConfigVlpr);

    /* Initialize LPTMR */
    lptmr_state_t lptmrState;
    LPTMR_DRV_Init(LPTMR0_IDX, &lptmrState, &g_lptmrConfig);
    LPTMR_DRV_SetTimerPeriodUs(LPTMR0_IDX, 100000);
    LPTMR_DRV_InstallCallback(LPTMR0_IDX, lptmr_call_back);

    /* Initialize DMA */
    dma_state_t dma_state;
    DMA_DRV_Init(&dma_state);

    /* Initialize PIT */
    PIT_DRV_Init(0, false);
    PIT_DRV_InitChannel(0, 0, &g_pitChan0);

    /* Initialize CMP */
    CMP_DRV_Init(0, &g_cmpState, &g_cmpConf);
    CMP_DRV_ConfigDacChn(0, &g_cmpDacConf);
    PORT_HAL_SetMuxMode(g_portBase[GPIOC_IDX], 0, kPortMuxAlt5);
    CMP_DRV_Start(0);

    /* Buttons */
    GPIO_DRV_InputPinInit(&g_switch1);
    GPIO_DRV_InputPinInit(&g_switch2);
    GPIO_DRV_InputPinInit(&g_switchUp);
    GPIO_DRV_InputPinInit(&g_switchDown);
    GPIO_DRV_InputPinInit(&g_switchLeft);
    GPIO_DRV_InputPinInit(&g_switchRight);
    GPIO_DRV_InputPinInit(&g_switchSelect);

    /* Start LPTMR */
    LPTMR_DRV_Start(LPTMR0_IDX);

    /* Setup LPUART1 */
    LPUART_DRV_Init(1, &g_lpuartState, &g_lpuartConfig);
    LPUART_DRV_InstallRxCallback(1, lpuartRxCallback, rxBuff, NULL, true);
    LPUART_DRV_InstallTxCallback(1, lpuartTxCallback, NULL, NULL);
    LPUART_BWR_CTRL_TXINV(g_lpuartBase[1], 1);
    PORT_HAL_SetMuxMode(g_portBase[GPIOE_IDX], 0, kPortMuxAlt3);
    PORT_HAL_SetMuxMode(g_portBase[GPIOE_IDX], 1, kPortMuxAlt3);

    /* Setup FlexIO for the WS2812B */
    FLEXIO_Type *fiobase = g_flexioBase[0];
    CLOCK_SYS_SetFlexioSrc(0, kClockFlexioSrcMcgIrClk);
    FLEXIO_DRV_Init(0, &g_flexioConfig);
    FLEXIO_HAL_ConfigureTimer(fiobase, 0, &g_timerConfig);
    FLEXIO_HAL_ConfigureShifter(fiobase, 0, &g_shifterConfig);
    PORT_HAL_SetMuxMode(g_portBase[GPIOE_IDX], 20, kPortMuxAlt6);
    FLEXIO_DRV_Start(0);

    FLEXIO_HAL_SetShifterStatusDmaCmd(fiobase, 1, true);
    DMA_DRV_RequestChannel(kDmaAnyChannel, kDmaRequestMux0FlexIOChannel0,
            &g_fioChan);
    DMA_DRV_RegisterCallback(&g_fioChan, fioDmaCallback, NULL);

    /* Connect buzzer to TPM0_CH3 */
    PORT_HAL_SetMuxMode(g_portBase[GPIOE_IDX], 30, kPortMuxAlt3);
    tpm_general_config_t tmpConfig = {
        .isDBGMode = false,
        .isGlobalTimeBase = false,
        .isTriggerMode = false,
        .isStopCountOnOveflow = false,
        .isCountReloadOnTrig = false,
        .triggerSource = kTpmTrigSel0,
    };
    TPM_DRV_Init(0, &tmpConfig);
    TPM_DRV_SetClock(0, kTpmClockSourceModuleMCGIRCLK, kTpmDividedBy1);

    /* Blank LED just in case, saves power */
    led(0x00, 0x00, 0x00);

    /* Init e-paper display */
    EPD_Init();

    /* Throw up first image */
    int ret = EPD_Draw(NULL, images[current_image]);
    if (-1 == ret) {
        led(0xff, 0x00, 0x00);
    } else if (-2 == ret) {
        led(0xff, 0xff, 0x00);
    } else if (-3 == ret) {
        led(0x00, 0x00, 0xff);
    } else {
        led(0x00, 0xff, 0x00);
    }
    blank_led = 30;

    /* Deinit so we can mess around on the bus pirate */
    //EPD_Deinit();

    /* We're done, everything else is triggered through interrupts */
    for(;;) {
        if (cue_next_image) {
            int old_image = current_image;
            current_image = (current_image + 1) % image_count;
            EPD_Draw(images[old_image], images[current_image]);
            cue_next_image = 0;
        }
#ifndef DEBUG
        SMC_HAL_SetMode(SMC, &g_idlePowerMode);
#endif
    }
}