/* USER CODE BEGIN Header */ /** ****************************************************************************** * @file : main.c * @brief : Main program body ****************************************************************************** * @attention * * Copyright (c) 2023 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** */ /* USER CODE END Header */ /* Includes ------------------------------------------------------------------*/ #include "main.h" #include "adc.h" #include "usart.h" #include "spi.h" #include "tim.h" #include "gpio.h" /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ #include #include "miscdev.h" #include "devsn.h" #include "ds18b20.h" #include "sht20.h" #include "isl1208.h" #include "ws2812b.h" #include "w25q32.h" #include "comport.h" #include "hal_oled.h" #include "esp32.h" /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ /* USER CODE BEGIN PTD */ /* USER CODE END PTD */ /* Private define ------------------------------------------------------------*/ /* USER CODE BEGIN PD */ /* USER CODE END PD */ /* Private macro -------------------------------------------------------------*/ /* USER CODE BEGIN PM */ /* USER CODE END PM */ /* Private variables ---------------------------------------------------------*/ /* USER CODE BEGIN PV */ /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ void SystemClock_Config(void); /* USER CODE BEGIN PFP */ /* USER CODE END PFP */ /* Private user code ---------------------------------------------------------*/ /* USER CODE BEGIN 0 */ #define POS_X 5 #define POS_Y 1 #define SHOWTIME 1000 static inline void oled_show(char *msg) { OLED_Init(); OLED_ShowString(POS_X, POS_Y, msg, OLED_FONT16); HAL_Delay(SHOWTIME); } int check_devsn(void); int check_flash(void); int check_wifi(void); int show_rtc(void); int show_adc(void); int show_ds18b20(void); int show_sht20(void); /* USER CODE END 0 */ /** * @brief The application entry point. * @retval int */ int main(void) { /* USER CODE BEGIN 1 */ rtc_time_t tm = { .tm_year=2023, .tm_mon=4, .tm_mday=8, .tm_hour=8, .tm_min=8, .tm_sec=8 }; /* 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_USART2_UART_Init(); MX_USART3_UART_Init(); MX_USART1_UART_Init(); MX_LPUART1_UART_Init(); MX_ADC1_Init(); MX_TIM6_Init(); MX_TIM1_Init(); MX_SPI1_Init(); /* USER CODE BEGIN 2 */ printf("Welcome to ISKBoard v1.0, compiled on %s %s\r\n", __DATE__, __TIME__); beep_start(2, 200); /* Check device S/N, it will hang when failed */ check_devsn(); OLED_Init(); ws2812b_init(); set_rtc_time(tm); check_flash(); check_wifi(); /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { ws2812b_blink(); show_rtc(); show_adc(); show_ds18b20(); show_sht20(); /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ } /* USER CODE END 3 */ } /** * @brief System Clock Configuration * @retval None */ void SystemClock_Config(void) { RCC_OscInitTypeDef RCC_OscInitStruct = {0}; RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; /** Configure the main internal regulator output voltage */ if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK) { Error_Handler(); } /** Initializes the RCC Oscillators according to the specified parameters * in the RCC_OscInitTypeDef structure. */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; RCC_OscInitStruct.HSEState = RCC_HSE_ON; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; RCC_OscInitStruct.PLL.PLLM = 1; RCC_OscInitStruct.PLL.PLLN = 20; RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7; RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2; RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2; if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { Error_Handler(); } /** Initializes the CPU, AHB and APB buses clocks */ RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK) { Error_Handler(); } } /* USER CODE BEGIN 4 */ void system_hanges(void) { while(1) { blink_led(LedRed, 500); //beep_start(1, 300); } } int check_devsn(void) { comport_t *comport = NULL; char *ptr; char buf[32] = {0}; char devsn[DEVSN_LEN+1]; char oledstr[32] = {0}; int bytes; if( !read_devsn(devsn, sizeof(devsn)) ) { printf("Read Serial Number okay\r\n"); goto SHOW_SN; } /* AT+SN=ISK102300x */ printf("ISKBoard S/N not written, please send SN by AT command:\r\n"); memset(buf, 0, sizeof(buf)); comport = comport_open(&huart1); if( !comport ) { printf("ERROR: Console serial port open failed!\r\n"); goto ERROR; } while( 1 ) { bytes = comport_recv(comport, buf, sizeof(buf), 10000); if(bytes > DEVSN_LEN) break; } if( strncmp(buf, "AT+SN=ISK", 9) ) { printf("ERROR: Input SN with wrong AT command.\r\n"); goto ERROR; } ptr = strstr(buf, "ISK"); if( !ptr || strlen(ptr)!=DEVSN_LEN ) { printf("ERROR: Input invalid Serial Number!\r\n"); goto ERROR; } if( write_devsn(ptr, DEVSN_LEN) < 0 ) { printf("ERROR: Write Serial Number failed\r\n"); goto ERROR; } if( read_devsn(devsn, sizeof(devsn)) ) { printf("ERROR: Read Serial Number after write failed\r\n"); goto ERROR; } SHOW_SN: printf("ISKBoard S/N:%s\r\n", devsn); snprintf(oledstr, sizeof(oledstr), "S/N: %s", devsn); oled_show(oledstr); comport_term(comport); return 0; ERROR: system_hanges(); return -2; } int check_flash(void) { int rv; char oledstr[32] = {0}; rv = w25q_Init(); printf("SPI Flash W25Q32 detected\r\n"); snprintf(oledstr, sizeof(oledstr), "W25Q: %s", rv?"[FAIL]":"[OK]"); oled_show(oledstr); return 0; } int check_wifi(void) { comport_t *comport = NULL; int rv = 0; char oledstr[32] = {0}; char version[256] = {0}; comport = comport_open(&huart2); if( !comport ) { rv = -2; goto cleanup; } esp32_hwreset(comport); esp32_set_echo(comport, DISABLE); rv = esp32_get_firmware(comport, version, sizeof(version)); if( rv < 0) { printf("Check ESP32 WiFi module failed, rv=%d\r\n", rv); goto cleanup; } printf("ESP32 firmware version:\r\n%s\r\n", version); cleanup: snprintf(oledstr, sizeof(oledstr), "WiFi: %s", rv?"[FAIL]":"[OK]"); oled_show(oledstr); return 0; } int show_rtc(void) { char oledstr[32] = {0}; rtc_time_t tm; int rv = 0; if( (rv=get_rtc_time(&tm)) < 0 ) { printf("RTC get date & time failed, rv=%d\r\n", rv); oled_show("RTC: [FAIL]"); return -1; } printf("RTC: %04d-%02d-%02d %02d:%02d:%02d %s\r\n", tm.tm_year, tm.tm_mon, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, weekday[tm.tm_wday]); snprintf(oledstr, sizeof(oledstr), "RTC: %04d-%02d-%02d", tm.tm_year, tm.tm_mon, tm.tm_mday); oled_show(oledstr); snprintf(oledstr, sizeof(oledstr), "RTC: %02d:%02d:%02d", tm.tm_hour, tm.tm_min, tm.tm_sec); oled_show(oledstr); return 0; } int show_adc(void) { char oledstr[32] = {0}; uint32_t lux, noisy; adc_sample_lux_noisy(&lux, &noisy); printf("Lux: %lu Noisy: %lu\r\n", lux, noisy); snprintf(oledstr, sizeof(oledstr), "lux:%lu db:%lu", lux, noisy); oled_show(oledstr); return 0; } int show_ds18b20(void) { int rv = 0; float temperature_f = 0.0; char oledstr[32] = {0}; rv = ds18b20_sample(&temperature_f); if( rv < 0 ) { printf("DS18B20 sample temperature failed, rv=%d\r\n", rv); oled_show("DS18B20: [FAIL]"); return -1; } printf("DS18B20 sample temperature: %.3f\'C\r\n", temperature_f); snprintf(oledstr, sizeof(oledstr), "DS18B20: %.3f", temperature_f); oled_show(oledstr); return 0; } int show_sht20(void) { uint32_t TrH, temperature, humdity; char oledstr[32] = {0}; TrH = sht20_sample_TrH(&temperature, &humdity); if( TRH_FAIL_VAL == TrH) { printf("SHT20 sample temperature & humidity failed!\r\n"); oled_show("DS18B20: [FAIL]"); return -1; } printf("SHT20 sample temperature: %lu.%02lu\'C humidity: %lu.%02lu%%\r\n", temperature/100, temperature%100, humdity/100, humdity%100); snprintf(oledstr, sizeof(oledstr), "SHT20: %lu.%02lu\'C", temperature/100, temperature%100); oled_show(oledstr); snprintf(oledstr, sizeof(oledstr), "SHT20: %lu.%02lu%%", humdity/100, humdity%100); oled_show(oledstr); return 0; } /* USER CODE END 4 */ /** * @brief This function is executed in case of error occurrence. * @retval None */ void Error_Handler(void) { /* USER CODE BEGIN Error_Handler_Debug */ /* User can add his own implementation to report the HAL error return state */ __disable_irq(); while (1) { } /* USER CODE END Error_Handler_Debug */ } #ifdef USE_FULL_ASSERT /** * @brief Reports the name of the source file and the source line number * where the assert_param error has occurred. * @param file: pointer to the source file name * @param line: assert_param error line source number * @retval None */ void assert_failed(uint8_t *file, uint32_t line) { /* USER CODE BEGIN 6 */ /* User can add his own implementation to report the file name and line number, ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ /* USER CODE END 6 */ } #endif /* USE_FULL_ASSERT */