示例#1
0
int main(void)
{

  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration----------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* Configure the system clock */
  SystemClock_Config();

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_ADC_Init();
  MX_USART1_UART_Init();

  /* USER CODE BEGIN 2 */
  char ch;
  ch = 'x';
  HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 100);

  sprintf(strbuf,"Hello\n");

  uint32_t adcvals[5];
  HAL_ADC_Start(&hadc);

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
  /* USER CODE END WHILE */

  /* USER CODE BEGIN 3 */
  	HAL_ADC_Start(&hadc);
	for (int i=0;i<3;i++){
		while(HAL_IS_BIT_CLR(hadc.Instance->ISR, (ADC_FLAG_EOC|ADC_FLAG_EOS))){}
		adcvals[i] = hadc.Instance->DR;
	}
	for (int i=0;i<5;i++){
		sprintf(strbuf,"i:%d,adc:%4d	",i,adcvals[i]);
		HAL_UART_Transmit(&huart1,strbuf,strlen(strbuf),1000);
	}
	
	sprintf(strbuf,"\n");
	HAL_UART_Transmit(&huart1,strbuf,strlen(strbuf),1000);
	HAL_Delay(1000);


  }
  /* USER CODE END 3 */

}
示例#2
0
文件: main.c 项目: Ilydan/ROwen
int main(void)
{

  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration----------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* Configure the system clock */
  SystemClock_Config();

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_COMP1_Init();
  MX_COMP2_Init();
  MX_I2C1_Init();
  MX_TIM3_Init();
  MX_USART1_UART_Init();
	MX_ADC_Init();
	MX_DMA_Init();

  /* USER CODE BEGIN 2 */
  init_all();
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
		period_time_check_flags();
//		if (HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_4) != GPIO_PIN_RESET)
//		{
//			HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_10);
//			buzzer(beeps,1);
//			buzzer_speed(100);
//		}
		
		
		
  /* USER CODE END WHILE */

  /* USER CODE BEGIN 3 */
		speed_counter++;
  }
  /* USER CODE END 3 */

}
示例#3
0
void board_init() {
    /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
    HAL_Init();

    /* Configure the system clock */
    SystemClock_Config();

    MX_GPIO_Init();

#if ENABLE_UART
    MX_USART1_UART_Init();
    uart_printf("\r\n\r\nUART online!\r\n");
#endif

    MX_ADC_Init();

    MX_I2C1_Init();

    uart_printf("board_init done\r\n");

}
示例#4
0
文件: aos.c 项目: OUWECAD/MOWE
/* --- AOS Initialization --- */
void AOS_init(void)
{		
	/* MCU Configuration */

	/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
	HAL_Init();

	/* Configure the system clock */
	SystemClock_Config();

	/* Unlock the Flash Program Erase controller */
	//HAL_FLASH_Unlock();

	/* Initialize all configured peripherals */
	MX_GPIO_Init();	
	MX_UART_Init();
	MX_TIM2_Init();
	MX_TIM7_Init();
	/* PWM Timers will override P1, P3 & P4 */
	MX_TIM3_Init();
  MX_TIM15_Init();
	
	#ifndef TX
		MX_ADC_Init();
	#endif

	/* AOS Initialization */
	
	/* Startup indicator sequence */
	blink(500);
	HAL_Delay(100);
	blink(100);
	HAL_Delay(100);
	blink(100);	

	/* Initialize random seed for PRNG */
	srand(atoi(_firmTime));
	
}
示例#5
0
int main(void)
{
int i=5000;
	
uint8_t ultrasoundEnableFlag;
 uint16_t voltage; 
	/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();
  /* Configure the system clock */
  SystemClock_Config_8MHz();
  //__HAL_RCC_PWR_CLK_ENABLE();
  /* Initialize all configured peripherals */
	
	MX_GPIO_Init();
			HAL_GPIO_WritePin( DCDC_EN_GPIO_Port , DCDC_EN_Pin , 0);								//
			HAL_GPIO_WritePin( DCDC_PWR_GPIO_Port , DCDC_PWR_Pin , 0 );									//open drain kimenet,pch fet,
  MX_ADC_Init();
  //MX_I2C1_Init(); 
	 //MX_TIM3_Init();
	 MX_USART1_UART_Init();

		GPIO_TIM3_OFF();
	//HAL_Delay(100);
		 MX_RTC_Init(); 
 /* Enable Power Clock */
  //__HAL_RCC_PWR_CLK_ENABLE();
	
		 uartTester();
		//	GPIO_TIM3_OFF();
		

//Feszültseg ellenörzése,sippogás-----------------------------------------------------
voltage=voltage_read(ADC_CHANNEL_2,2);
if(Full<voltage)
	batteryLevel=3;

if(Medium < voltage && voltage<= Full)
	batteryLevel=2;

if(Medium >= voltage)
	batteryLevel=1;

HAL_Delay(1000);
beeps(batteryLevel,1);
PutString("Software version 2016.07.26.\n");
PutString("batteryLevel: ");
			PutNumber(batteryLevel);
			PutString("\n");

while(1){
	//Feszültseg ellenörzése, paraméterek beállítása,fesz fvében------------------------
	voltage=voltage_read(ADC_CHANNEL_2,2);
	if(Low > voltage){
			if(UltraLow > voltage){
				voltage=voltage_read(ADC_CHANNEL_2,2);
				if(UltraLow > voltage){
					HAL_GPIO_WritePin( DCDC_PWR_GPIO_Port , DCDC_PWR_Pin , 1 );									//open drain kimenet,pch fet,
				HAL_PWR_EnterSTANDBYMode();
				}
			}
			ultrasoundEnableFlag=0;
			sleepNum=LowVoltSleep;
	}else{
			ultrasoundEnableFlag=1;
			sleepNum= linear(sleepNumMax,sleepNumMin, voltageMin,voltageMax,voltage);											
			soundNum=	linear(soundNumMin,soundNumMax,voltageMin,voltageMax,voltage);
			sounDelay=sDelay;
	}
			PutString("sleepNum");
			PutNumber(sleepNum);
			PutString("\n");
				PutString("soundNum");
			PutNumber(soundNum);
			PutString("\n");
	
	//Ultrahang generálás---------------------------------------------------------------
	
	if(ultrasoundEnableFlag){
		
		PutString("ultrasound(1)\n ");
			ultrasound(1);		
		
	}

	//Sleep-----------------------------------------------------------------------------
		
		//	HAL_ADC_MspDeInit(&hadc);
	HAL_Delay(10);
PutString("sleep!\n");


	
	HAL_NVIC_SetPriority(RTC_IRQn, 0, 0);
				HAL_NVIC_EnableIRQ(RTC_IRQn);
	

	
	for(uint8_t e=0 ; e < sleepNum ; e++){

			
			HAL_RTC_GetTime(&hrtc,&sTim,RTC_FORMAT_BIN); 
		  HAL_RTC_GetDate(&hrtc, &sDat, RTC_FORMAT_BIN);
		
		HAL_RTC_GetTime(&hrtc,&sTim,RTC_FORMAT_BIN); 
		  HAL_RTC_GetDate(&hrtc, &sDat, RTC_FORMAT_BIN);

		PutNumber((uint32_t)sTim.Hours);
		
		PutString(":");
		PutNumber((uint32_t)sTim.Minutes);
	
		PutString(":");
		PutNumber((uint32_t)sTim.Seconds);
			PutString("\n");
		
				if(sTim.Minutes==59){
						sAlarm.AlarmTime.Minutes=0;
					if(sTim.Hours==23){
							sAlarm.AlarmTime.Hours=0;
						}else{
							sAlarm.AlarmTime.Hours=sTim.Hours+1;
						}
					}else{
						sAlarm.AlarmTime.Minutes=sTim.Minutes+1;
						sAlarm.AlarmTime.Hours=sTim.Hours;
					}
					HAL_RTC_SetAlarm_IT(&hrtc, &sAlarm, FORMAT_BIN);		

				
					PutString("alarm: ");
		PutNumber((uint32_t)sAlarm.AlarmTime.Hours);		
		PutString(":");
		PutNumber((uint32_t)sAlarm.AlarmTime.Minutes);
							PutString(":");
		PutNumber((uint32_t)sAlarm.AlarmTime.Seconds);
		PutString("\n");
					
		SleepMode();
	}
					HAL_NVIC_DisableIRQ(RTC_IRQn);

	
	}			

}
示例#6
0
文件: main.c 项目: theapi/solar
int main(void)
{

  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration----------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_ADC_Init();
  MX_I2C1_Init();
  MX_RTC_Init();
  MX_USART1_UART_Init();
  MX_SPI2_Init();

  /* Initialize interrupts */
  MX_NVIC_Init();

  /* USER CODE BEGIN 2 */

  HAL_ADCEx_Calibration_Start(&hadc, ADC_SINGLE_ENDED);

    /* Enable Ultra low power mode */
    HAL_PWREx_EnableUltraLowPower();
    __HAL_RCC_WAKEUPSTOP_CLK_CONFIG(RCC_STOP_WAKEUPCLOCK_HSI);

    /* Buffer used for transmission on USART1 */
    char tx1_buffer[120];


    RFM95_init(&hspi2);

    uint8_t payload_buff[14];
    PAYLOAD_Garden payload_garden;
    payload_garden.MessageType = 50;
    payload_garden.MessageId = 0;


    // Start in sensing mode.
    state = MAIN_STATE_SENSE;

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
    while (1) {
  /* USER CODE END WHILE */

  /* USER CODE BEGIN 3 */

        /* Do some work */
        if (state == MAIN_STATE_SENSE) {
            HAL_ADC_Start(&hadc);
            // junk readings
            payload_garden.Temperature = TEMPERATURE_external();
            payload_garden.CpuTemperature = TEMPERATURE_cpu();

            HAL_GPIO_WritePin(GPIOA, LED_Pin, GPIO_PIN_SET);

            payload_garden.MessageId++;
            payload_garden.VCC = BATTERY_vcc();
            payload_garden.ChargeMv = BATTERY_ChargeMv();
            payload_garden.ChargeMa = BATTERY_ChargeMa();



            /* Get the light reading while the adc gets ready */
            payload_garden.Light = LIGHT_lux();

            payload_garden.Temperature = TEMPERATURE_external();
            payload_garden.CpuTemperature = TEMPERATURE_cpu();
            HAL_ADC_Stop(&hadc);

            sprintf(tx1_buffer, "id:%d, vcc:%d, mv:%d, ma:%d, C:%d, cpuC:%d, lux:%d\n",
                    payload_garden.MessageId,
                    payload_garden.VCC,
                    payload_garden.ChargeMv,
                    payload_garden.ChargeMa,
                    payload_garden.Temperature,
                    payload_garden.CpuTemperature,
                    payload_garden.Light);
            HAL_UART_Transmit(&huart1, (uint8_t*) tx1_buffer, strlen(tx1_buffer), 1000);

            PAYLOAD_Garden_serialize(payload_garden, payload_buff);
            RFM95_send(&hspi2, payload_buff, 14);

            state = MAIN_STATE_TX;
        }

        /* Do nothing while the transmission is in progress */
        else if (state == MAIN_STATE_TX) {
            if (dio0_action == 1) {
                RFM95_setMode(&hspi2, RFM95_MODE_SLEEP);
                state = MAIN_STATE_SLEEP;
            }

            //TMP while interrupts are investigated
            //HAL_Delay(30);
            //state = MAIN_STATE_SLEEP;
        }

        /* Now that all the work is done, sleep until its time to do it all again */
        else if (state == MAIN_STATE_SLEEP) {
            HAL_GPIO_WritePin(GPIOA, LED_Pin, GPIO_PIN_RESET);

            //TMP while RFM is diabled
            //HAL_Delay(1000);

            /* Turn off the pin interrupts */
            HAL_NVIC_DisableIRQ(EXTI4_15_IRQn);

            HAL_SuspendTick();

            /* Enter Stop Mode */
            HAL_RTCEx_SetWakeUpTimer_IT(&hrtc, 60,
            RTC_WAKEUPCLOCK_CK_SPRE_16BITS);
            HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);
            HAL_RTCEx_DeactivateWakeUpTimer(&hrtc);
            HAL_ResumeTick();

            /* Turn on the pin interrupts */
            HAL_NVIC_EnableIRQ(EXTI4_15_IRQn);

            state = MAIN_STATE_SENSE;
        }

    }
  /* USER CODE END 3 */

}
示例#7
0
int main(void)
{
    /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
    HAL_Init();

    /* Configure the system clock */
    SystemClock_Config();

    /* Initialize all configured peripherals */
    MX_GPIO_Init();
    MX_DMA_Init();
    MX_ADC_Init();
    MX_SPI1_Init();
    MX_USB_DEVICE_Init();
    MX_TIM14_Init();

    HAL_FLASH_Unlock();
    EE_Init();

    EE_ReadVariable(VirtAddVarTab[0], &x_low_th);
    EE_ReadVariable(VirtAddVarTab[1], &x_high_th);
    EE_ReadVariable(VirtAddVarTab[2], &y_low_th);
    EE_ReadVariable(VirtAddVarTab[3], &y_high_th);

    HAL_FLASH_Lock();

    HAL_ADC_Start_DMA(&hadc, (uint32_t*)axis, 5);

    while (1)
    {
        HAL_GPIO_WritePin(SPI1_nCS_GPIO_Port, SPI1_nCS_Pin, GPIO_PIN_SET);

        HAL_TIM_Base_Start_IT(&htim14);
        while (!u100ticks) /* do nothing for 100 us */;
        HAL_TIM_Base_Stop_IT(&htim14);
        u100ticks = 0;

        HAL_StatusTypeDef status =
            HAL_SPI_Receive(&hspi1, rx_buffer, sizeof(rx_buffer), 3000);

        switch(status)
        {
        case HAL_OK:
            report.buttons[0] = 0x00;
            // report.buttons[1] = 0x00;
            report.buttons[2] = 0x00;

            if ((rx_buffer[0] & 0xff) != 0xff)   // if all bits of rx_buffer[0] is 1 assume shifter is disconnected
            {
                if (rx_buffer[0] & 4)   report.buttons[0] |= 1;         else report.buttons[0] &= ~1;
                if (rx_buffer[0] & 1)   report.buttons[0] |= (1 << 1);  else report.buttons[0] &= ~(1 << 1);
                if (rx_buffer[0] & 2)   report.buttons[0] |= (1 << 2);  else report.buttons[0] &= ~(1 << 2);
                if (rx_buffer[0] & 8)   report.buttons[0] |= (1 << 3);  else report.buttons[0] &= ~(1 << 3);

                if (rx_buffer[1] & 1)   report.buttons[2] |= 1;         else report.buttons[2] &= ~1;
                if (rx_buffer[1] & 2)   report.buttons[2] |= (1 << 1);  else report.buttons[2] &= ~(1 << 1);
                if (rx_buffer[1] & 4)   report.buttons[2] |= (1 << 2);  else report.buttons[2] &= ~(1 << 2);
                if (rx_buffer[1] & 8)   report.buttons[2] |= (1 << 3);  else report.buttons[2] &= ~(1 << 3);

                if (rx_buffer[1] & 32)  report.buttons[0] |= (1 << 4);  else report.buttons[0] &= ~(1 << 4);
                if (rx_buffer[1] & 128) report.buttons[0] |= (1 << 5);  else report.buttons[0] &= ~(1 << 5);
                if (rx_buffer[1] & 64)  report.buttons[0] |= (1 << 6);  else report.buttons[0] &= ~(1 << 6);
                if (rx_buffer[1] & 16)  report.buttons[0] |= (1 << 7);  else report.buttons[0] &= ~(1 << 7);
            }
            break;

        case HAL_TIMEOUT:
        case HAL_BUSY:
        case HAL_ERROR:
            Error_Handler();
        default:
            report.buttons[0] = 0xff;
            break;
        }

        HAL_GPIO_WritePin(SPI1_nCS_GPIO_Port, SPI1_nCS_Pin, GPIO_PIN_RESET);
        report.id = 0x01;

        x_axis = ((X_AXIS << 2) + x_axis * 96) / 100;
        y_axis = ((Y_AXIS << 2) + y_axis * 96) / 100;

        if (rx_buffer[0] & 16)
        {
            if (y_axis < y_low_th)   // stick towards player
            {
                report.buttons[1] = 128;
                report.buttons[2] &= ~(1 << 4);
            }
            else if (y_axis > y_high_th)   // stick opposite to player
            {
                report.buttons[1] = 0; // neutral
                report.buttons[2] |= (1 << 4);
            }
            else
            {
                report.buttons[1] = 0; // neutral
                report.buttons[2] &= ~(1 << 4);
            }
        }
        else
        {
            report.buttons[1] &= ~(1 << 7);
            report.buttons[2] &= ~(1 << 4);

            if (y_axis < y_low_th)   // stick towards player
            {
                if (x_axis < x_low_th)
                {
                    if (!report.buttons[1]) report.buttons[1] = 2; // 2nd gear
                }
                else if (!report.buttons[1])
                {
                    report.buttons[1] = (x_axis > x_high_th) ? ((rx_buffer[0] & 64) ? 64 : 32) : 8;
                }
            }
            else
            {
                if (y_axis > y_high_th)   // stick opposite to player
                {
                    if (x_axis < x_low_th)
                    {
                        if (!report.buttons[1]) report.buttons[1] = 1; // 1st gear
                    }
                    else if (!report.buttons[1])
                    {
                        report.buttons[1] = (x_axis > x_high_th) ? 16 : 4;
                    }
                }
                else
                {
                    report.buttons[1] = 0; // neutral
                }
            }
        }

        report.axis[0] = x_axis;
        report.axis[1] = y_axis;

        if (report2send == 2)
        {
            HAL_FLASH_Unlock();

            EE_WriteVariable(VirtAddVarTab[0], x_low_th);
            EE_WriteVariable(VirtAddVarTab[1], x_high_th);
            EE_WriteVariable(VirtAddVarTab[2], y_low_th);
            EE_WriteVariable(VirtAddVarTab[3], y_high_th);

            HAL_FLASH_Lock();
            report2send = 0;
        }

        if (hUsbDeviceFS.pClassData
                && ((USBD_HID_HandleTypeDef *)hUsbDeviceFS.pClassData)->state == HID_IDLE)
        {
            USBD_HID_SendReport(&hUsbDeviceFS, (uint8_t *)&report, sizeof(report));
        }
    }
}
示例#8
0
文件: main.c 项目: shorai/stm32L05
int main(void)
{

  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration----------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* Configure the system clock */
  SystemClock_Config();

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_DMA_Init();
  MX_ADC_Init();
  APP_ADC_Init(&hadc);

#ifdef USE_COMPARATORS
  MX_COMP1_Init();
  MX_COMP2_Init();
#endif
  MX_DAC_Init();
  MX_USART2_UART_Init();

  /* USER CODE BEGIN 2 */


  HAL_UART_Transmit(&huart2,str,strlen((const char *) str),1000);
  HAL_UART_Transmit(&huart2,heading,strlen((const char *) heading),1000);

  dacConfig.DAC_Trigger =  DAC_TRIGGER_NONE;
  dacConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_DISABLE;
  HAL_DAC_ConfigChannel(&hdac,&dacConfig,DAC_CHANNEL_1);
  HAL_DAC_SetValue(&hdac,DAC_CHANNEL_1,DAC_ALIGN_12B_R,2048);
  HAL_DAC_Start(&hdac,DAC_CHANNEL_1);


  /* Initialize ADC peripheral according to the passed parameters */
  if (HAL_ADC_Init(&hadc) != HAL_OK)
  {
    while(-1);
  }


  /* ### - 2 - Start calibration ############################################ */
  if (HAL_ADCEx_Calibration_Start(&hadc, ADC_SINGLE_ENDED) != HAL_OK)
  {
	  while(-1);
  }

  /* ### - 3 - Channel configuration ######################################## */
  /* Select Channel 0 to be converted */
  ///sConfig.Channel = ADC_CHANNEL_0;
  //if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
  //{
  //  Error_Handler();
  //}

  /* ### - 4 - Start conversion in DMA mode ################################# */
  if (HAL_ADC_Start_DMA(&hadc, (uint32_t *) adcBuf,ADC_BUFSIZE) != HAL_OK)
  {
	  while(-1);
  }
  compValues[0]= '-';
  compValues[1]= '-';

#ifdef USE_COMPARATORS

  HAL_COMP_Start_IT(&hcomp1);
  HAL_COMP_Start_IT(&hcomp2);
#endif

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  uint16_t cnt=0;
  uint16_t dacValue = 0;
  memset(dataline, ' ', sizeof(dataline));
  dataline[sizeof(dataline)-1] = 0;

  while (1)
  {
  /* USER CODE END WHILE */

  /* USER CODE BEGIN 3 */
	  HAL_DAC_SetValue(&hdac,DAC_CHANNEL_1,DAC_ALIGN_12B_R,dacValue);


#ifdef WAIT_BEFORE_READING
	  int i;
	  for(i=0; i < 10000; i++);
#endif
	  itoa (cnt,(char *) &dataline[0],5);
	  itoa (dacValue,(char *)&dataline[10],5);

	  itoa (adcBuf[0],(char *)&dataline[20],4);
	  dataline[25] = '/';
	  itoa (adcBuf[2],(char *)&dataline[25],4);

	  itoa (adcBuf[1],(char *)&dataline[40],4);
	  dataline[45] = '/';
	  itoa (adcBuf[3],(char *)&dataline[45],4);

#ifdef USE_COMPARATORS
	  dataline[60] =  compValues[0];
	  dataline[65] =  compValues[1];
#else
//	  memcpy("Not Used",dataline[60],8);
#endif
	  dataline[70] =  (HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_6)==GPIO_PIN_SET)?'X':'0';
	  dataline[75] =  (HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_7)==GPIO_PIN_SET)?'X':'0';

	  dataline[87] = '\r';
	  dataline[88] = '\n';
	  dataline[89] = 0;

	  HAL_UART_Transmit(&huart2,dataline,strlen((const char *) dataline),1000);
	  cnt++;

	  dacValue = (dacValue >= (4096-DAC_STEPSIZE))?0:dacValue+DAC_STEPSIZE;
  }
  /* USER CODE END 3 */

}
示例#9
0
int main(void)
{

  /* USER CODE BEGIN 1 */

//  AxesRaw_t data;
	
	FRESULT res; /* FatFs function common result code */
	uint32_t byteswritten, bytesread; /* File write/read counts */
	char rtext[256]; /* File read buffer */
	
	//	HALL_SENS_PWR_ON;
	//	CLAMP_SENS_PWR_ON;
	
	//	1. SPI1 is for ACC 
	
	
  /* USER CODE END 1 */

  /* MCU Configuration----------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* Configure the system clock */
  SystemClock_Config();

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_ADC_Init();
  MX_SDIO_SD_Init();
  MX_SPI1_Init();
  MX_SPI2_Init();
  MX_USART1_UART_Init();
  MX_USART2_UART_Init();
  MX_FATFS_Init();
  MX_RTC_Init();

  /* USER CODE BEGIN 2 */
	HAL_RTCEx_DeactivateWakeUpTimer(&hrtc);
	HAL_RTCEx_SetWakeUpTimer_IT(&hrtc, 1, RTC_WAKEUPCLOCK_CK_SPRE_16BITS);

  EXTI0_IRQHandler_Config();
//	EXTI13_IRQHandler_Config();
	
//	HAL_DBGMCU_EnableDBGStopMode();


	//HALL_SENS_PWR_ON;
	//CLAMP_SENS_PWR_OFF;
	
	
	// init gpio before!
	UC_2_8V;
 
 
	printf("FW start\r\n");


	LIS3DH_PreInit();
	LIS3DH_SetMode(LIS3DH_NORMAL); 				//reg1
	LIS3DH_SetODR(LIS3DH_ODR_400Hz); 			//reg1
	LIS3DH_SetAxis(LIS3DH_X_ENABLE | LIS3DH_Y_ENABLE | LIS3DH_Z_ENABLE ); //reg1

	//Direct IRQ from watemark and overrun to 1st pin. reg3
	LIS3DH_SetInt1Pin(LIS3DH_CLICK_ON_PIN_INT1_DISABLE 		| 	LIS3DH_I1_INT1_ON_PIN_INT1_DISABLE 	| 	LIS3DH_I1_INT2_ON_PIN_INT1_DISABLE 	| 
										LIS3DH_I1_DRDY1_ON_INT1_DISABLE			| 	LIS3DH_I1_DRDY2_ON_INT1_DISABLE 		| 	LIS3DH_WTM_ON_INT1_ENABLE 					| LIS3DH_INT1_OVERRUN_DISABLE);

	//REG4
	LIS3DH_SetBDU(MEMS_ENABLE);
  LIS3DH_SetFullScale(LIS3DH_FULLSCALE_2);  //reg4

	//REG5
  LIS3DH_FIFOModeEnable(LIS3DH_FIFO_STREAM_MODE);	//Enable store into FIFO reg5
	LIS3DH_Int1LatchEnable(MEMS_ENABLE);
	
	
	//LIS3DH_FIFO_CTRL_REG
	LIS3DH_SetTriggerInt(LIS3DH_TRIG_INT1);
	LIS3DH_SetWaterMark(15); 									// watermark for irq generation from fifo  LIS3DH_FIFO_CTRL_REG

	LIS3DH_ReadFIFO();												// clean FIFO and reset IRQ


while (1) {
	//LIS3DH_GetInt1Src(&resp);
	//printf("INT1SRC %i ", resp);
	
	/*LIS3DH_GetFifoSourceBit(LIS3DH_FIFO_SRC_WTM, &resp);
	printf("WTM %i ", resp);
	
	LIS3DH_GetReg3Bit(LIS3DH_I1_WTM, &resp);
	printf("WMBIT %i ", resp);
	
	LIS3DH_GetFifoSourceFSS(&resp);
	printf("FIFO %i\r\n", resp);
	
	
	//LIS3DH_GetIntCounter(&resp);
	//printf("Interrupts counter=%i\r\n", resp);	
	if (resp > 26) {
		
		//rep:
			//LIS3DH_GetFifoSourceFSS(&resp);
			//printf("> %i recs in FIFO (while)\r\n", resp);
			//HAL_Delay(15);
		//LIS3DH_GetInt1Src(&resp);
/*		rep:
		if(LIS3DH_GetAccAxesRaw(&data)==1){
				LIS3DH_GetFifoSourceBit(LIS3DH_FIFO_SRC_WTM, &resp);
				//printf("X=%6d Y=%6d Z=%6d \r\n", data.AXIS_X, data.AXIS_Y, data.AXIS_Z); 
			printf("  READ WTM%i\r\n", resp);
		} else {
				printf("ER\r\n");
		}
		
		LIS3DH_GetFifoSourceFSS(&resp);
		if (resp > 0) goto rep;
		
		LIS3DH_ResetInt1Latch();
		
		
		LIS3DH_FIFOModeEnable(LIS3DH_FIFO_STREAM_MODE);*/

			LIS3DH_ReadFIFO();
	HAL_Delay(2000);
}
//ENABLE ALL IRQs
//LIS3DH_SetInt1Pin(LIS3DH_CLICK_ON_PIN_INT1_DISABLE | LIS3DH_I1_INT1_ON_PIN_INT1_ENABLE | LIS3DH_I1_INT2_ON_PIN_INT1_ENABLE | LIS3DH_I1_DRDY1_ON_INT1_ENABLE | LIS3DH_I1_DRDY2_ON_INT1_ENABLE | LIS3DH_WTM_ON_INT1_ENABLE | LIS3DH_INT1_OVERRUN_ENABLE);
//LIS3DH_SetInt2Pin(LIS3DH_CLICK_ON_PIN_INT2_DISABLE | LIS3DH_I2_INT1_ON_PIN_INT2_ENABLE | LIS3DH_I2_INT2_ON_PIN_INT2_ENABLE | LIS3DH_I2_BOOT_ON_INT2_ENABLE | LIS3DH_INT_ACTIVE_HIGH);
//	HAL_Delay(2000);
	
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)  {
  /* USER CODE END WHILE */

  /* USER CODE BEGIN 3 */
		
//		if(LIS3DH_GetAccAxesRaw(&data)==1){
//			printf("X=%6d Y=%6d Z=%6d \r\n", data.AXIS_X, data.AXIS_Y, data.AXIS_Z); 
//		} else {
//			printf("ER\r\n");
//		}
		HAL_Delay(50);

		//	lowest_power();		//go to stop. Wakeup on RTC wakeup or 1st or 2d PIN wakeup
  }
	
	
//SDIO FAT PART.
	
	//ENABLE_2_5V;	//DC-DC enable
	SD_PWR_ON;		//Power to SD card
	//INIT SD and CARD after because no power to sd
	
	HAL_Delay(50);
  MX_SDIO_SD_Init();
  MX_FATFS_Init();

/*##-1- FatFS: Link the SD disk I/O driver ##########*/
 
 if(FATFS_LinkDriver(&SD_Driver, SDPath) == 0){
		/* success: set the orange LED on */
		 //HAL_GPIO_WritePin(GPIOG, GPIO_PIN_7, GPIO_PIN_RESET);
		/*##-2- Register the file system object to the FatFs module 
	 
		NB! mout right now!
	 ###*/
		res = f_mount(&SDFatFs, (TCHAR const*)SD_Path, 1) ;
	 
		 if(res != FR_OK){
				 /* FatFs Initialization Error : set the red LED on */
				 printf ("Problem fmount\r\n");
				 while(1);
		 } else {
				/*##-3- Create a FAT file system (format) on the logical drive#*/
				 /* WARNING: Formatting the uSD card will delete all content on the device */
					res = f_mkfs((TCHAR const*)SD_Path, 0, 0);
					if(res != FR_OK){
						 /* FatFs Format Error : set the red LED on */
							printf ("Problem f_mkfs\r\n");
						 while(1);
					} else {
						/*##-4- Create & Open a new text file object with write access#*/
						 if(f_open(&MyFile, "Hello.txt", FA_CREATE_ALWAYS | FA_WRITE) != FR_OK){
								 /* 'Hello.txt' file Open for write Error : set the red LED on */
								 printf ("Problem f_open\r\n");
								 while(1);
						 } else {
								 /*##-5- Write data to the text file ####################*/
								 res = f_write(&MyFile, wtext, sizeof(wtext), (void*)&byteswritten);
									 
								 if((byteswritten == 0) || (res != FR_OK)){
										 /* 'Hello.txt' file Write or EOF Error : set the red LED on */
										 printf ("Problem f_write\r\n");
										 while(1);
								 } else {
									 
										 /*##-6- Successful open/write : set the blue LED on */
										// HAL_GPIO_WritePin(GPIOG, GPIO_PIN_12, GPIO_PIN_RESET);
										 f_close(&MyFile);
									 
										 /*##-7- Open the text file object with read access #*/
										 if(f_open(&MyFile, "Hello.txt", FA_READ) != FR_OK){
												 /* 'Hello.txt' file Open for read Error : set the red LED on */
												//HAL_GPIO_WritePin(GPIOG, GPIO_PIN_10, GPIO_PIN_RESET);
													printf ("Problem f_open\r\n");
												 while(1);
										 } else {
												 /*##-8- Read data from the text file #########*/
												 res = f_read(&MyFile, rtext, sizeof(wtext), &bytesread);
//												 if((strcmp(rtext,wtext)!=0)|| (res != FR_OK)){
//													 /* 'Hello.txt' file Read or EOF Error : set the red LED on */
//													 				 printf ("Problem f_read\r\n");
//													 while(1);
//												 } else {
//													 printf ("FAT operation done OK!\r\n");
													 /* Successful read : set the green LED On */
													 //HAL_GPIO_WritePin(GPIOG, GPIO_PIN_6, GPIO_PIN_RESET);
													 /*##-9- Close the open text file ################*/
													 f_close(&MyFile);
												 }
										 }
								 }
						 }
				 }
		 }
 
 /*##-10- Unlink the micro SD disk I/O driver #########*/
 FATFS_UnLinkDriver(SD_Path);
	
  /* USER CODE END 3 */

}
示例#10
0
int main(void)
{

  /* MCU Configuration----------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* Configure the system clock */
  SystemClock_Config();

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_DMA_Init();
  MX_ADC_Init();
  MX_TIM2_Init();
  MX_TIM21_Init();

  batpins battery3;
  batpins battery4;
  pwm_timers b3_tims;
  pwm_timers b4_tims;
  batprops props_bat3;
  batprops props_bat4;

  /* Battery 3 */
  b3_tims.conv_timer = htim2;
  b3_tims.dchg_timer = htim21;

  battery3.v_adc_chan = ADC_CHANNEL_4;
  battery3.i_adc_chan = ADC_CHANNEL_8;
  battery3.chg_port = chg_onoff_3_GPIO_Port;
  battery3.chg_pin = chg_onoff_3_Pin;
  battery3.dchg_pin = TIM_CHANNEL_1;
  battery3.conv_chg_pin = TIM_CHANNEL_1;
  battery3.conv_dchg_pin = TIM_CHANNEL_2;
  battery3.pwm_tims = b3_tims;

  props_bat3.i_adc_val = 0;
  props_bat3.v_adc_val = 0;
  props_bat3.adc_val_old = adc_read(battery3.i_adc_chan);
  props_bat3.id_adc_stpt = 400 + props_bat3.adc_val_old;
  props_bat3.ic_adc_stpt = props_bat3.adc_val_old - 600;
  props_bat3.conv_bst_stpt = 200; // Need to calibrate this to boost to desired voltage
  props_bat3.pwm_chg_stpt = 0; 	  // Initialized to 0. Program will change as needed.
  props_bat3.pwm_dchg_stpt = 720; // Initialize near where discharge FET turns on
  props_bat3.pi = 0;

  /* Battery 4 */
  b4_tims.conv_timer = htim2;
  b4_tims.dchg_timer = htim21;

  battery4.v_adc_chan = ADC_CHANNEL_11;
  battery4.i_adc_chan = ADC_CHANNEL_10;
  battery4.chg_port = chg_onoff_4_GPIO_Port;
  battery4.chg_pin = chg_onoff_4_Pin;
  battery4.dchg_pin = TIM_CHANNEL_2;
  battery4.conv_chg_pin = B4_CHG_CHAN; // Change in h file (used in multiple locations, dma_offset func)
  battery4.conv_dchg_pin = TIM_CHANNEL_4;
  battery4.pwm_tims = b4_tims;

  props_bat4.i_adc_val = 0;
  props_bat4.v_adc_val = 0;
  props_bat4.adc_val_old = adc_read(battery4.i_adc_chan);
  props_bat4.id_adc_stpt = 500 + props_bat4.adc_val_old;
  props_bat4.ic_adc_stpt = props_bat4.adc_val_old - 200;
  props_bat4.conv_bst_stpt = 200; // Need to calibrate this to boost to desired voltage
  props_bat4.pwm_chg_stpt = 0; 	  // Initialized to 0. Program will change as needed.
  props_bat4.pwm_dchg_stpt = 720; // Initialize near where discharge FET turns on
  props_bat4.pi = 0;

  /* Initialize global variables */
#ifdef BAT1
  TimeCounter3 = 0;
  TimeCounter4 = 0;
  uint32_t restStartms3 = 0;
  uint32_t i3 = 0;
  uint32_t voltage3 = 0;
  uint32_t current3 = 720;
  status bat_stat3 = OK;
  i3_origin = props_bat3.adc_val_old;
#endif

#ifdef BAT2
  uint32_t restStartms4 = 0;
  uint32_t i4 = 0;
  uint32_t voltage4 = 0;
  uint32_t current4 = 720;
  status bat_stat4 = OK;
  i4_origin = props_bat4.adc_val_old;
#endif

  //uint32_t dc_pwm[1] = {800};//, 500, 200, 300, 400, 500, 600, 700, 250, 750};
  //uint32_t test2[2] = {100, 900};
  //uint32_t sine = 0;

  /* Initialize converter and charge / discharge pins   */
  conv_init(battery3);
  conv_init(battery4);

  //HAL_TIM_PWM_Start_DMA(&htim2, TIM_CHANNEL_3, &dc_pwm, (uint16_t)1);
  //HAL_Delay(10);
  //HAL_TIM_PWM_Start_DMA(&htim2, TIM_CHANNEL_4, &dc_pwm[8], (uint16_t)2);
  //HAL_TIM_PWM_Start_DMA(&htim2, TIM_CHANNEL_1, 200, (uint16_t)SINE_RES_500HZ);
  //HAL_TIM_PWM_Start_DMA(&htim2, TIM_CHANNEL_4, 800, (uint16_t)SINE_RES_500HZ); //Bat2 conv dchg
  //pwm_sine_Start(battery3.pwm_tims.conv_timer, battery3.conv_dchg_pin, dc_pwm, sine); // Boost (discharge)
  //pwm_sine_Start(battery3.pwm_tims.conv_timer, battery3.conv_chg_pin,  dc_pwm, sine); // Buck (charge)
  //pwm_sine_Start(battery4.pwm_tims.conv_timer, battery4.conv_dchg_pin, test2, sine); // Boost (discharge)
  //pwm_sine_Start(battery4.pwm_tims.conv_timer, battery4.conv_chg_pin, 400, sine); // Buck (charge)
  //pwm_Set(battery3.pwm_tims.dchg_timer, battery3.dchg_pin, 750);
 // pwm_Set(battery4.pwm_tims.dchg_timer, battery4.dchg_pin, 760);
  //pwm_sine_Start(battery3.pwm_tims.conv_timer, battery3.conv_chg_pin, dc_pwm, sine); // Buck (charge)
  //HAL_GPIO_WritePin(battery3.chg_port, battery3.chg_pin, GPIO_PIN_SET); // Charging On
  //HAL_GPIO_WritePin(battery4.chg_port, battery4.chg_pin, GPIO_PIN_SET); // Charging On
  //pwm_sine_Start(battery4.pwm_tims.conv_timer, battery4.conv_chg_pin, dc_pwm, sine); // Buck (charge)
  //HAL_TIM_PWM_Start(battery4.pwm_tims.conv_timer, battery4.conv_dchg_pin);
  //HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_2); // Bat1 conv dchg

  uint8_t u8_oc3 = 0;
  uint8_t u8_oc4 = 0;

  // Wait for batteries to be connected
  //while(adc_read(battery3.v_adc_chan) < 500 || adc_read(battery4.v_adc_chan) < 500) {}

  /* Infinite loop */
  while (1)
  {

#ifdef BAT1
	  /* First battery */
	  if(TimeCounter3>=5) // 4ms, ie 2 periods of 500Hz sine wave
	  	  {
	  		  switch(bat_stat3) {
	  		  case DISCHARGE:
	  			  bat_stat3 = discharge_main(battery3, &props_bat3, &restStartms3, i3, bat_stat3);
	  			  break;
	  		  case CC:
	  			  bat_stat3 = chg_ctrl(battery3, &props_bat3, i3, i3_origin);
	  			  //HAL_Delay(1);
	  			  break;
	  		  case CV:
	  			  bat_stat3 = cv_main(battery3, &props_bat3, &restStartms3, i3, i3_origin, bat_stat3);
	  			  break;
	  		  case FULL:
	  			  if(HAL_GetTick() - restStartms3 >= REST)
	  			  {
						props_bat3.i_adc_val = 0;
						props_bat3.v_adc_val = 0;
						props_bat3.adc_val_old = adc_read(battery3.i_adc_chan);
						bat_stat3 = DISCHARGE;
	  			  }
	  			  break;
	  		  case LVDC:
	  			if(HAL_GetTick() - restStartms3 >= REST)
	  			  {
					  props_bat3.i_adc_val = 0;
					  props_bat3.v_adc_val = 0;
					  props_bat3.adc_val_old = adc_read(battery3.i_adc_chan);
					  bat_stat3 = CC;
	  			  }
	  			  break;
	  		  case OK:
	  			  props_bat3.i_adc_val = 0; // normally reset in d/chg func, but not used so reset here
	  			  props_bat3.v_adc_val = 0; // normally reset in d/chg func, but not used so reset here
	  			  bat_stat3 = CC;
	  			  break;
	  		  case OVERCURRENT:
	  			  bat_stat3 = OVERCURRENT;
	  			  break;
	  		  default:
	  			  bat_stat3 = OK;
	  			  break;
	  		  }
	  		  TimeCounter3 = 0;
	  		  i3 = 0;
	  	  }

  	  /* Update ADC readings */
  	  current3 = adc_read(battery3.i_adc_chan);
  	  voltage3 = adc_read(battery3.v_adc_chan);
  	  props_bat3.i_adc_val = props_bat3.i_adc_val + current3;
  	  props_bat3.v_adc_val = props_bat3.v_adc_val + voltage3;

  	  /* Over-current protection */
  	  if(current3>3950 || current3<100)
  	  {
  		  u8_oc3++;
  		  if(u8_oc3 > 15)
  		  {
  			conv_init(battery3);
  			bat_stat3 = OVERCURRENT;
  		  }
  	  }
  	  else
  	  {
  		  u8_oc3 = 0;
  	  }
  	  i3++;
#endif

#ifdef BAT2
	  /* Second battery */
	  if(TimeCounter4>=5) // 4ms, ie 2 periods of 500Hz sine wave
		  {
			  switch(bat_stat4) {
			  case DISCHARGE:
				  bat_stat4 = discharge_main(battery4, &props_bat4, &restStartms4, i4, bat_stat4);
				  break;
			  case CC:
				  bat_stat4 = chg_ctrl(battery4, &props_bat4, i4, i4_origin);
				  break;
			  case CV:
				  bat_stat4 = cv_main(battery4, &props_bat4, &restStartms4, i4, i4_origin, bat_stat4);
				  break;
			  case FULL:
				  if(HAL_GetTick() - restStartms4 >= REST)
				  {
						props_bat4.i_adc_val = 0;
						props_bat4.v_adc_val = 0;
						props_bat4.adc_val_old = adc_read(battery4.i_adc_chan);
						bat_stat4 = DISCHARGE;
				  }
				  break;
			  case LVDC:
				if(HAL_GetTick() - restStartms4 >= REST)
				  {
					  props_bat4.i_adc_val = 0;
					  props_bat4.v_adc_val = 0;
					  props_bat4.adc_val_old = adc_read(battery4.i_adc_chan);
					  bat_stat4 = CC;
				  }
				  break;
			  case OK:
				  props_bat4.i_adc_val = 0; // normally reset in d/chg func, but not used so reset here
				  props_bat4.v_adc_val = 0; // normally reset in d/chg func, but not used so reset here
				  bat_stat4 = CC;
				  break;
			  case OVERCURRENT:
				  bat_stat4 = OVERCURRENT;
				  break;
			  default:
				  bat_stat4 = OK;
				  break;
			  }
			  TimeCounter4 = 0;
			  i4 = 0;
		  }

	  /* Update ADC readings */
  	  current4 = adc_read(battery4.i_adc_chan);
  	  voltage4 = adc_read(battery4.v_adc_chan);
  	  props_bat4.i_adc_val = props_bat4.i_adc_val + current4;
  	  props_bat4.v_adc_val = props_bat4.v_adc_val + voltage4;

  	  /* Over-current protection */
  	  if(current4>3950 || current4<100)
  	  {
  		  u8_oc4++;
  		  if(u8_oc4 > 15)
  		  {
  			conv_init(battery4);
  			bat_stat4 = OVERCURRENT;
  		  }
  	  }
  	  else
  	  {
  		  u8_oc4 = 0;
  	  }
  	  i4++;

#endif

	  HAL_SYSTICK_IRQHandler();

  }


}
示例#11
0
文件: main.c 项目: theapi/solar
int main(void)
{

  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration----------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_ADC_Init();
  MX_I2C1_Init();
  MX_RTC_Init();
  MX_USART1_UART_Init();
  MX_SPI2_Init();

  /* USER CODE BEGIN 2 */

  /* Buffer used for transmission on USART1 */
  char tx1_buffer[80];
  uint8_t count = 0;

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
  /* USER CODE END WHILE */

  /* USER CODE BEGIN 3 */

      sprintf(tx1_buffer, "Count is %d\n", count);
      HAL_UART_Transmit(&huart1, (uint8_t*) tx1_buffer, strlen(tx1_buffer), 5000);
      count++;

      uint16_t adc_val = ADS1015_SingleEnded(&hi2c1, ADS1015_ADDRESS, 3, ADS1015_GAIN_ONE);
      sprintf(tx1_buffer, "adc_val: %d\n", adc_val);
      HAL_UART_Transmit(&huart1, (uint8_t*) tx1_buffer, strlen(tx1_buffer), 5000);

      //ADS1015_writeRegister(&hi2c1, ADS1015_ADDRESS, ADS1015_REG_POINTER_CONFIG, ADS1015_REG_CONFIG_MUX_SINGLE_3);

      //HAL_Delay(ADS1015_CONVERSIONDELAY);

//      uint16_t adc_val = ADS1015_readRegister(&hi2c1, ADS1015_ADDRESS, ADS1015_REG_POINTER_CONVERT);
//sprintf(tx1_buffer, "adc_val: %04X\n", adc_val);
//      HAL_UART_Transmit(&huart1, (uint8_t*) tx1_buffer, strlen(tx1_buffer), 5000);

      HAL_GPIO_TogglePin(GPIOA, LED_Pin);
      HAL_Delay(1000);

  }
  /* USER CODE END 3 */

}
示例#12
0
文件: main.c 项目: kqzca/prj
int main(void)
{

  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration----------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* Configure the system clock */
  SystemClock_Config();

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_ADC_Init();
  MX_DAC_Init();
  MX_I2C1_Init();
  MX_RTC_Init();

  /* USER CODE BEGIN 2 */
				
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
	//int loopCnt = 0;
  while (1)
  {
		Button_Function buttonFunction = checkButtonFunction();
		if (buttonFunction == BTNFUNC_STATECHN)
		{
			wakeupSleepState = (wakeupSleepState == STATE_WAKEUP) ? STATE_SLEEP : STATE_WAKEUP;
		}
		
		if (buttonFunction == BTNFUNC_CAL)
		{
			setSensorDevPwr(DEV_PWR_ON);
			getSnrDrvDacVal();
			setSensorDevPwr(DEV_PWR_OFF);
			wakeupSleepState = STATE_WAKEUP;
		}
				
		if (wakeupSleepState == STATE_WAKEUP)
		{
			uint16_t snrAdcVal = 0;
			
			setBlueToothDevPwr(DEV_PWR_ON);
			setSensorDevPwr(DEV_PWR_ON);
			setSysWakeUpLed(LED_ON);
			setSysStsLed(LED_OFF);
			
			int snrDrvDacVal = readEEPROM32(EEPROM_ADDR_SNRDRVVAL);
			if ((snrDrvDacVal < 1) || (snrDrvDacVal > 0xFFF))
			{
				getSnrDrvDacVal();
			}
			snrAdcVal = (uint16_t)dacOutAdcIn(snrDrvDacVal, ADC_CHANNEL_8);
			if (snrAdcVal > SNR_PASS_THRESHOLD)
			{
				setTestPassLed(LED_ON);
				setTestFailLed(LED_OFF);
			}
			else
			{
				setTestFailLed(LED_ON);
				setTestPassLed(LED_OFF);
			}
			
			if (hi2c1.State == HAL_I2C_STATE_READY)
			{
				setBleSig(BLESIG_HI);
				if (HAL_I2C_Slave_Transmit(&hi2c1, (uint8_t *)&snrAdcVal, 2, 3000) == HAL_OK)
				{
					setSysStsLed(LED_ON);
					HAL_Delay(100);
					setSysStsLed(LED_OFF);
					HAL_Delay(100);
					setSysStsLed(LED_ON);
					HAL_Delay(100);
					setSysStsLed(LED_OFF);
				}
				else
				{
					setSysStsLed(LED_ON);
					HAL_Delay(300);
					setSysStsLed(LED_OFF);
				}
			}
			else
			{
				setSysWakeUpLed(LED_OFF);
				HAL_Delay(100);
				setSysWakeUpLed(LED_ON);
				HAL_Delay(100);
				setSysWakeUpLed(LED_OFF);
				HAL_Delay(100);
				setSysWakeUpLed(LED_ON);
				HAL_Delay(100);
			}
			
			setTestFailLed(LED_OFF);
			setTestPassLed(LED_OFF);
			HAL_Delay(1000);
			setBleSig(BLESIG_LO);
			HAL_Delay(1000);
		}
		else
		{
			goSleep();
		}
		
		/*
		loopCnt++;
		if (loopCnt >= 2)
		{
			loopCnt = 0;
		}
		*/
  /* USER CODE END WHILE */

  /* USER CODE BEGIN 3 */

  }
  /* USER CODE END 3 */

}