/* 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 "can.h" #include "usart.h" #include "spi.h" #include "tim.h" #include "gpio.h" /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ #include #include "miscdev.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 800 #define BLOCK_TIME 0XFFFFFFFF static inline void oled_show(char *msg) { OLED_Init(); OLED_ShowString(POS_X, POS_Y, msg, OLED_FONT16); HAL_Delay(SHOWTIME); } int test_beep(void); int test_relay(void); int test_led(void); int test_wifi(void); int test_adc(void); int test_spiflash(void); int test_ds18b20(void); int test_sht20(void); int test_rtc(void); int test_usart3(void); int test_can(void); int test_rs485(void); int test_mikrobus(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=2, .tm_hour=6, .tm_min=6, .tm_sec=6 }; /* 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(); MX_CAN1_Init(); /* USER CODE BEGIN 2 */ printf("Welcome to ISKBoard v1.0\r\n"); OLED_Init(); ws2812b_init(); set_rtc_time(tm); test_beep(); test_relay(); /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { test_mikrobus(); test_spiflash(); test_rtc(); test_wifi(); test_adc(); test_ds18b20(); test_sht20(); test_usart3(); test_can(); test_rs485(); test_led(); /* 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 */ int test_beep(void) { printf("Buzzer [TEST]\r\n"); oled_show("Beep [TEST]"); beep_start(2, 300); printf("\r\n"); return 0; } int test_relay(void) { printf("Relay [TEST]\r\n"); oled_show("Relay [TEST]"); turn_relay(ON); HAL_Delay(800); turn_relay(OFF); printf("\r\n"); return 0; } #define MIKROBUS_PIN_GROUP 5 static gpio_t mikrobus_pins[MIKROBUS_PIN_GROUP][2] = { { {"AN", mikrobus_an_GPIO_Port, mikrobus_an_Pin}, {"RST", mikrobus_rst_GPIO_Port, mikrobus_rst_Pin} }, { {"CS", mikrobus_cs_GPIO_Port, mikrobus_cs_Pin}, {"SCK", mikrobus_sck_GPIO_Port, mikrobus_sck_Pin} }, { {"MISO", mikrobus_miso_GPIO_Port, mikrobus_miso_Pin}, {"MOSI", mikrobus_mosi_GPIO_Port, mikrobus_mosi_Pin} }, { {"PWM", mikrobus_pwm_GPIO_Port, mikrobus_pwm_Pin}, {"INT", mikrobus_int_GPIO_Port, mikrobus_int_Pin} }, /* RX3/TX3 initial as comport before, and test as comport but not GPIO mode */ { {"SCL", mikrobus_scl_GPIO_Port, mikrobus_scl_Pin}, {"SDA", mikrobus_sda_GPIO_Port, mikrobus_sda_Pin} }, }; #define PIN_OUTPUT 1 #define PIN_INPUT 0 void set_gpio_mode(gpio_t pin, int mode) { GPIO_InitTypeDef GPIO_InitStruct = {0}; if( PIN_INPUT == mode ) { GPIO_InitStruct.Pin = pin.pin; GPIO_InitStruct.Mode = GPIO_MODE_INPUT; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(pin.group, &GPIO_InitStruct); } else { GPIO_InitStruct.Pin = pin.pin; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(pin.group, &GPIO_InitStruct); } } int comport_test(UART_HandleTypeDef *huart) { comport_t *comport = NULL; char buf[32] = { 0 }; int rv = 0; comport = comport_open(huart); if( !comport ) { //printf("%s() comport open failed\r\n", __func__); return -2; } comport_flush(comport); comport_send(comport, "ISKBoard", strlen("ISKBoard")); comport_recv(comport, buf, sizeof(buf), 50); // printf("%s() receive: %s\r\n", __func__, buf); if( strcmp(buf, "ISKBoard") ) { rv = -3; goto cleanup; } cleanup: comport_term(comport); return rv; } int rs485_test(UART_HandleTypeDef *huart) { comport_t *comport_rs485 = NULL; char buf[32] = { 0 }; int rv = 0; comport_rs485 = comport_open(huart); if( !comport_rs485 ) { //printf("%s() comport open failed\r\n", __func__); return -2; } comport_flush(comport_rs485); rs485_recv(comport_rs485, buf, sizeof(buf), BLOCK_TIME); // printf("%s() receive: %s\r\n", __func__, buf); if( !strstr(buf, "PC2ISKBoard-RS485") ) { rv = -3; goto cleanup; }else { rs485_send(comport_rs485, "ISKBoard2PC-RS485", strlen("ISKBoard2PC-RS485")); } cleanup: rs485_term(comport_rs485); return rv; } int can_test(can_t *pcan) { char rx_data_buf[8] = { 0 }; char test_data_buf[8] = {0x01, 0x02, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F}; int rv = 0; if( !pcan ) { //printf("%s() CAN open failed\r\n", __func__); return -2; } if(can_recv(pcan, rx_data_buf, sizeof(rx_data_buf), BLOCK_TIME) > 0) { #if 0 //echo CAN recv 8bytes data printf("CAN RX DATA:"); for(int i=0; i%4s test [OKAY]\r\n", mikrobus_pins[i][0].name, mikrobus_pins[i][1].name); } if( failed ) { printf("MikroBUS %4s<->%4s test [FAIL]\r\n", mikrobus_pins[i][0].name, mikrobus_pins[i][1].name); rv = -1; goto cleanup; } if( comport_test(&huart3) < 0 ) printf("MikroBUS UART test [FAIL]\r\n"); else printf("MikroBUS UART test [OKAY]\r\n"); cleanup: if( rv ) oled_show("MikroBUS [FAIL]"); else oled_show("MikroBUS [OK]"); printf("\r\n"); return 0; } int test_led(void) { oled_show("LED [TEST]"); printf("RGB Led [TEST]\r\n"); blink_led(LedRed, 200); blink_led(LedGreen, 200); blink_led(LedBlue, 200); printf("Strip Lights [TEST]\r\n"); ws2812b_blink(); printf("\r\n"); return 0; } int test_spiflash(void) { int rv; char oledstr[32] = {0}; rv = w25q_Init(); printf("SPI Flash W25Q32 test [%s]\r\n", rv?"FAIL":"OKAY"); snprintf(oledstr, sizeof(oledstr), "W25Q: %s", rv?"FAIL":"OK"); oled_show(oledstr); printf("\r\n"); return 0; } int test_wifi(void) { static int init = 0; static comport_t *comport = NULL; int rv; char oledstr[32] = {0}; char version[256] = {0}; if( !init ) { comport = comport_open(&huart2); if( !comport ) return -1; esp32_hwreset(comport); esp32_set_echo(comport, DISABLE); rv = esp32_get_firmware(comport, version, sizeof(version)); if( rv < 0) { printf("Query ESP32 firmware version failed: %d\r\n", rv); return rv; } printf("ESP32 firmware version:\r\n%s\r\n", version); init = 1; } rv = send_atcmd_check_ok(comport, "AT", 500); printf("WiFi module AT test [%s]\r\n", rv?"FAIL":"OKAY"); snprintf(oledstr, sizeof(oledstr), "WiFi: %s", rv?"FAIL":"OK"); oled_show(oledstr); printf("\r\n"); return 0; } int test_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); printf("ADC(Lux/Noisy) test [OKAY]\r\n"); snprintf(oledstr, sizeof(oledstr), "lux:%lu db:%lu", lux, noisy); oled_show(oledstr); printf("\r\n"); return 0; } int test_ds18b20(void) { int rv; float temperature_f = 0.0; char oledstr[32] = {0}; rv = ds18b20_sample(&temperature_f); if( rv < 0 ) { printf("DS18B20 test [FAIL]\r\n"); oled_show("DS18B20: [FAIL]"); return -1; } printf("DS18B20 sample temperature: %.3f\'C\r\n", temperature_f); printf("DS18B20 test [OKAY]\r\n"); snprintf(oledstr, sizeof(oledstr), "DS18B20: %.3f", temperature_f); oled_show(oledstr); printf("\r\n"); return 0; } int test_sht20(void) { uint32_t TrH, temperature, humdity; char oledstr[32] = {0}; TrH = sht20_sample_TrH(&temperature, &humdity); if( TRH_FAIL_VAL == TrH) { printf("SHT20 test [FAIL]\r\n"); oled_show("SHT20: [FAIL]"); return -1; } printf("SHT20 sample temperature: %lu.%02lu\'C humidity: %lu.%02lu%%\r\n", temperature/100, temperature%100, humdity/100, humdity%100); printf("SHT20 test [OKAY]\r\n"); 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); printf("\r\n"); return 0; } int test_rtc(void) { char oledstr[32] = {0}; rtc_time_t tm; if( get_rtc_time(&tm) < 0 ) 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]); printf("RTC ISL1208 test [OKAY]\r\n"); 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); printf("\r\n"); return 0; } int test_usart3(void) { printf("Please switch USART3 to RS232.\r\n"); if( comport_test(&huart3) < 0 ) { printf("USART3 RS232 test [FAIL]\r\n"); oled_show("RS232 [FAIL]"); } else { printf("USART3 RS232 test [OKAY]\r\n"); oled_show("RS232 [OK]"); } printf("\r\n"); return 0; } int test_can(void) { printf("Please send {0x01, 0x02, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F} CAN frame data in PC.\r\n"); if( can_test(&can) < 0 ) { printf("CAN test [FAIL]\r\n"); oled_show("CAN [FAIL]"); } else { printf("CAN test [OKAY]\r\n"); oled_show("CAN [OK]"); } printf("\r\n"); return 0; } int test_rs485(void) { printf("Please send {PC2ISKBoard-RS485} rs485 data in PC.\r\n"); if( rs485_test(&hlpuart1) < 0 ) { printf("RS485 test [FAIL]\r\n"); oled_show("RS485 [FAIL]"); } else { printf("RS485 test [OKAY]\r\n"); oled_show("RS485 [OK]"); } printf("\r\n"); 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 */