/* USER CODE BEGIN Header */ /** ****************************************************************************** * @file : main.c * @brief : Main program body ****************************************************************************** ** This notice applies to any and all portions of this file * that are not between comment pairs USER CODE BEGIN and * USER CODE END. Other portions of this file, whether * inserted by the user or by software development tools * are owned by their respective copyright owners. * * COPYRIGHT(c) 2019 STMicroelectronics * * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of STMicroelectronics nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * ****************************************************************************** */ /* USER CODE END Header */ /* Includes ------------------------------------------------------------------*/ #include "main.h" /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ /* USER CODE BEGIN PTD */ uint8_t rx2_data[1]; uint8_t rx1_data[1]; uint8_t ring_buf[buf_size]; volatile uint8_t count_in1 = 0;//, count_out = 0; volatile uint8_t count_in2 = 0;//, count_out = 0; uint8_t UartDataisReved; volatile uint32_t UartTimerCnt = 0; uint32_t LedTimerCnt = 0; uint8_t buf[buf_size] = {0,}; /* 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 ---------------------------------------------------------*/ ADC_HandleTypeDef hadc1; I2C_HandleTypeDef hi2c1; TIM_HandleTypeDef htim7; UART_HandleTypeDef huart1; UART_HandleTypeDef huart2; /* USER CODE BEGIN PV */ /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ void SystemClock_Config(void); static void MX_GPIO_Init(void); static void MX_USART1_UART_Init(void); static void MX_USART2_UART_Init(void); static void MX_TIM7_Init(void); static void MX_I2C1_Init(void); static void MX_ADC1_Init(void); static void MX_NVIC_Init(void); /* USER CODE BEGIN PFP */ void UartDataRecvSet(uint8_t val); uint8_t UartDataRecvGet(void); /* USER CODE END PFP */ /* Private user code ---------------------------------------------------------*/ /* USER CODE BEGIN 0 */ void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) { if(huart->Instance == USART1){ buf[count_in1] = rx1_data[0];//(uint8_t)USART2->DR; if(buf[count_in1++] == 0xEB){ if(buf[Bluecell_Length] == (count_in1 - 3)) UartDataRecvSet(1); else count_in1 = 0; } HAL_UART_Receive_IT(&huart1,&rx1_data[0],1); } if(huart->Instance == USART2){ buf[count_in2] = rx2_data[0]; if(buf[count_in2++] == 0xEB){ if(buf[Bluecell_Length] == (count_in2 - 3)) UartDataRecvSet(2); else count_in2 = 0; } HAL_UART_Receive_IT(&huart2,&rx2_data[0],1); } } void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) { if(htim->Instance == TIM7){ UartTimerCnt++; LedTimerCnt++; } } void UartDataRecvSet(uint8_t val){ UartDataisReved = val; } uint8_t UartDataRecvGet(void){ return UartDataisReved; } int _write (int file, uint8_t *ptr, uint16_t len) { HAL_UART_Transmit (&huart1, ptr, len, 10); return len; } void Uart1_Data_Send(uint8_t* data,uint8_t size){ //Firmware Download Cable HAL_UART_Transmit(&huart1, data,size, 10); } void Uart2_Data_Send(uint8_t* data,uint8_t size){ // Controller Comunication Cable HAL_UART_Transmit(&huart2, data,size, 10); } void Uart_dataCheck(uint8_t* cnt){ etError crccheck = 0; #if 1 // PYJ.2019.03.17_BEGIN -- for(uint8_t i = 0; i < (* cnt); i++){ printf("%02x ",buf[i]); } printf("\r\n"); #endif // PYJ.2019.03.17_END -- crccheck = STH30_CheckCrc(&buf[Bluecell_Type],buf[Bluecell_Length],buf[buf[Bluecell_Length] + 1]); if(crccheck == CHECKSUM_ERROR){ for(uint8_t i = 0; i < (*cnt); i++){ printf("%02x ",buf[i]); } printf("Original CRC : %02x RecvCRC : %02x \r\n",crccheck,buf[buf[Bluecell_Length] + 1]); } else if(crccheck == NO_ERROR){ RGB_Sensor_Func(&buf[bluecell_stx]); } else{ printf("What Happen?\r\n"); } *cnt = 0; UartDataRecvSet(0); memset(buf,0x00,buf_size); } #define ADDR_FLASH_PAGE_TEST ((uint32_t)0x08007FF0) /* Base @ of Page 127, 1 Kbytes */ #define FLASH_USER_START_ADDR ADDR_FLASH_PAGE_TEST /* Start @ of user Flash area */ #define FLASH_USER_END_ADDR ADDR_FLASH_PAGE_TEST //+ ((uint32_t)0x0000000F) /* End @ of user Flash area */ void Flash_write(uint8_t* data) // ?“°ê¸°í•¨?ˆ˜ { /*Variable used for Erase procedure*/ static FLASH_EraseInitTypeDef EraseInitStruct; uint32_t Address = 0, PAGEError = 0; uint16_t dataret = data[bluecell_srcid] & 0x00FF; /* Fill EraseInit structure*/ EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES; EraseInitStruct.PageAddress = FLASH_USER_START_ADDR; EraseInitStruct.Banks = FLASH_BANK_1; EraseInitStruct.NbPages = (FLASH_USER_END_ADDR - FLASH_USER_START_ADDR) / FLASH_PAGE_SIZE; Address = FLASH_USER_START_ADDR; __HAL_RCC_TIM7_CLK_DISABLE(); // 매ì¸???´ë¨¸ë?? ? •ì§??•©?‹ˆ?‹¤ __HAL_FLASH_HALF_CYCLE_ACCESS_ENABLE(); if(HAL_FLASH_Unlock() == HAL_OK) // lock ??ê¸? printf("Flash Unlock Success\r\n"); else printf("Flash Unlock Failed\r\n"); __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_BSY | FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPERR); if (HAL_FLASHEx_Erase(&EraseInitStruct, &PAGEError) != HAL_OK){ printf("Erase Failed \r\n"); }else{ printf("Erase Success \r\n"); } if(HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD,Address , (uint16_t)dataret) == HAL_OK){ printf("Flash Write Success\r\n"); }else{ printf("Flash Write Failed\r\n"); } if(HAL_FLASH_Lock() == HAL_OK) // lock ?ž ê·¸ê¸° printf("Flash lock Success\r\n"); else printf("Flash lock Failed\r\n"); __HAL_FLASH_HALF_CYCLE_ACCESS_DISABLE(); __HAL_RCC_TIM7_CLK_ENABLE(); // 매ì¸???´ë¨¸ë?? ?ž¬?‹œ?ž‘?•©?‹ˆ?‹¤ } void Flash_RGB_IDData_Write(uint32_t Addr,uint8_t* data){ uint16_t temp_Red = 0,temp_Green = 0,temp_Blue = 0,temp_ret1 = 0,temp_ret2 = 0; HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD,Addr + 0 , (uint16_t)temp_Red); } void Flash_InitRead(void) // ?“°ê¸°í•¨?ˆ˜ { uint32_t Address = 0; Address = FLASH_USER_START_ADDR; My_RGB_ID = (*(uint16_t*)Address); printf("My id is %d ",My_RGB_ID); printf("%08x : %04X \n",Address ,*(uint16_t*)Address); } /* USER CODE END 0 */ /** * @brief The application entry point. * @retval int */ int main(void) { /* USER CODE BEGIN 1 */ uint8_t startdata[5] = {0xbe,RGB_ID_Allocate_Request,0x02,0,0xeb}; uint8_t tempdata[100] = {0,}; /* 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_USART1_UART_Init(); MX_USART2_UART_Init(); MX_TIM7_Init(); MX_I2C1_Init(); MX_ADC1_Init(); /* Initialize interrupts */ MX_NVIC_Init(); /* USER CODE BEGIN 2 */ HAL_TIM_Base_Start_IT(&htim7); HAL_UART_Receive_IT(&huart1, &rx1_data,1); HAL_UART_Receive_IT(&huart2, &rx2_data,1); setbuf(stdout, NULL); // \n ?��?��?��, printf ï¿????????��?��ï¿??????? ?��?��?�� #if 1 // PYJ.2019.03.04_BEGIN -- printf("****************************************\r\n"); printf("RGB Sensor Project\r\n"); printf("Build at %s %s\r\n", __DATE__, __TIME__); printf("Copyright (c) 2019. BLUECELL\r\n"); printf("****************************************\r\n"); #endif // PYJ.2019.03.04_END -- printf("My ID %02x \r\n",My_RGB_ID); // TCS34725_init(); TCS34725_enable(); uint8_t uartdatarecv = 0; /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { // HAL_Delay(500); // TCS34725_getrawdata(); #if 1 // PYJ.2019.03.15_BEGIN -- uartdatarecv = UartDataRecvGet(); if(uartdatarecv != 0){ if(uartdatarecv == 1){ Uart_dataCheck(&count_in1); }else if(uartdatarecv == 2){ Uart_dataCheck(&count_in2); } } else{ if(LedTimerCnt > 100){ TCS34725_getrawdata(); HAL_GPIO_TogglePin(GPIOC,GPIO_PIN_15); LedTimerCnt = 0; } } #endif // PYJ.2019.03.15_END -- /* 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}; RCC_PeriphCLKInitTypeDef PeriphClkInit = {0}; /**Initializes the CPU, AHB and APB busses clocks */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; RCC_OscInitStruct.HSIState = RCC_HSI_ON; RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { Error_Handler(); } /**Initializes the CPU, AHB and APB busses clocks */ RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI; 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_0) != HAL_OK) { Error_Handler(); } PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC; PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV2; if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) { Error_Handler(); } } /** * @brief NVIC Configuration. * @retval None */ static void MX_NVIC_Init(void) { /* USART1_IRQn interrupt configuration */ HAL_NVIC_SetPriority(USART1_IRQn, 0, 0); HAL_NVIC_EnableIRQ(USART1_IRQn); /* USART2_IRQn interrupt configuration */ HAL_NVIC_SetPriority(USART2_IRQn, 0, 0); HAL_NVIC_EnableIRQ(USART2_IRQn); /* TIM7_IRQn interrupt configuration */ HAL_NVIC_SetPriority(TIM7_IRQn, 0, 0); HAL_NVIC_EnableIRQ(TIM7_IRQn); } /** * @brief ADC1 Initialization Function * @param None * @retval None */ static void MX_ADC1_Init(void) { /* USER CODE BEGIN ADC1_Init 0 */ /* USER CODE END ADC1_Init 0 */ ADC_ChannelConfTypeDef sConfig = {0}; /* USER CODE BEGIN ADC1_Init 1 */ /* USER CODE END ADC1_Init 1 */ /**Common config */ hadc1.Instance = ADC1; hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE; hadc1.Init.ContinuousConvMode = DISABLE; hadc1.Init.DiscontinuousConvMode = DISABLE; hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START; hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT; hadc1.Init.NbrOfConversion = 1; if (HAL_ADC_Init(&hadc1) != HAL_OK) { Error_Handler(); } /**Configure Regular Channel */ sConfig.Channel = ADC_CHANNEL_0; sConfig.Rank = ADC_REGULAR_RANK_1; sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5; if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) { Error_Handler(); } /* USER CODE BEGIN ADC1_Init 2 */ /* USER CODE END ADC1_Init 2 */ } /** * @brief I2C1 Initialization Function * @param None * @retval None */ static void MX_I2C1_Init(void) { /* USER CODE BEGIN I2C1_Init 0 */ /* USER CODE END I2C1_Init 0 */ /* USER CODE BEGIN I2C1_Init 1 */ /* USER CODE END I2C1_Init 1 */ hi2c1.Instance = I2C1; hi2c1.Init.ClockSpeed = 100000; hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2; hi2c1.Init.OwnAddress1 = 0; hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE; hi2c1.Init.OwnAddress2 = 0; hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE; hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE; if (HAL_I2C_Init(&hi2c1) != HAL_OK) { Error_Handler(); } /* USER CODE BEGIN I2C1_Init 2 */ /* USER CODE END I2C1_Init 2 */ } /** * @brief TIM7 Initialization Function * @param None * @retval None */ static void MX_TIM7_Init(void) { /* USER CODE BEGIN TIM7_Init 0 */ /* USER CODE END TIM7_Init 0 */ TIM_MasterConfigTypeDef sMasterConfig = {0}; /* USER CODE BEGIN TIM7_Init 1 */ /* USER CODE END TIM7_Init 1 */ htim7.Instance = TIM7; htim7.Init.Prescaler = 800 - 1; htim7.Init.CounterMode = TIM_COUNTERMODE_UP; htim7.Init.Period = 10- 1; htim7.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; if (HAL_TIM_Base_Init(&htim7) != HAL_OK) { Error_Handler(); } sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; if (HAL_TIMEx_MasterConfigSynchronization(&htim7, &sMasterConfig) != HAL_OK) { Error_Handler(); } /* USER CODE BEGIN TIM7_Init 2 */ /* USER CODE END TIM7_Init 2 */ } /** * @brief USART1 Initialization Function * @param None * @retval None */ static void MX_USART1_UART_Init(void) { /* USER CODE BEGIN USART1_Init 0 */ /* USER CODE END USART1_Init 0 */ /* USER CODE BEGIN USART1_Init 1 */ /* USER CODE END USART1_Init 1 */ huart1.Instance = USART1; huart1.Init.BaudRate = 115200; huart1.Init.WordLength = UART_WORDLENGTH_8B; huart1.Init.StopBits = UART_STOPBITS_1; huart1.Init.Parity = UART_PARITY_NONE; huart1.Init.Mode = UART_MODE_TX_RX; huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE; huart1.Init.OverSampling = UART_OVERSAMPLING_16; if (HAL_UART_Init(&huart1) != HAL_OK) { Error_Handler(); } /* USER CODE BEGIN USART1_Init 2 */ /* USER CODE END USART1_Init 2 */ } /** * @brief USART2 Initialization Function * @param None * @retval None */ static void MX_USART2_UART_Init(void) { /* USER CODE BEGIN USART2_Init 0 */ /* USER CODE END USART2_Init 0 */ /* USER CODE BEGIN USART2_Init 1 */ /* USER CODE END USART2_Init 1 */ huart2.Instance = USART2; huart2.Init.BaudRate = 115200; huart2.Init.WordLength = UART_WORDLENGTH_8B; huart2.Init.StopBits = UART_STOPBITS_1; huart2.Init.Parity = UART_PARITY_NONE; huart2.Init.Mode = UART_MODE_TX_RX; huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE; huart2.Init.OverSampling = UART_OVERSAMPLING_16; if (HAL_UART_Init(&huart2) != HAL_OK) { Error_Handler(); } /* USER CODE BEGIN USART2_Init 2 */ /* USER CODE END USART2_Init 2 */ } /** * @brief GPIO Initialization Function * @param None * @retval None */ static void MX_GPIO_Init(void) { GPIO_InitTypeDef GPIO_InitStruct = {0}; /* GPIO Ports Clock Enable */ __HAL_RCC_GPIOC_CLK_ENABLE(); __HAL_RCC_GPIOA_CLK_ENABLE(); __HAL_RCC_GPIOB_CLK_ENABLE(); /*Configure GPIO pin Output Level */ HAL_GPIO_WritePin(GPIOC, GPIO_PIN_15, GPIO_PIN_RESET); /*Configure GPIO pin Output Level */ HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8|GPIO_PIN_15, GPIO_PIN_RESET); /*Configure GPIO pin Output Level */ HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5, GPIO_PIN_RESET); /*Configure GPIO pin : PC15 */ GPIO_InitStruct.Pin = GPIO_PIN_15; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); /*Configure GPIO pins : PA8 PA15 */ GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_15; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); /*Configure GPIO pins : PB3 PB4 PB5 */ GPIO_InitStruct.Pin = GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); } /* USER CODE BEGIN 4 */ /* 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 */ /* 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, tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ /* USER CODE END 6 */ } #endif /* USE_FULL_ASSERT */ /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/