/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2020 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under BSD 3-Clause license,
  * the "License"; You may not use this file except in compliance with the
  * License. You may obtain a copy of the License at:
  *                        opensource.org/licenses/BSD-3-Clause
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "main.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stdio.h"
#include "string.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 ---------------------------------------------------------*/
I2C_HandleTypeDef hi2c1;

UART_HandleTypeDef huart4;

/* USER CODE BEGIN PV */
static const uint8_t MACID_RW = 0xA9;
static const uint8_t MACID_WR = 0xA8<<1;
static const uint8_t start_ADDR = 0xFA;
static const uint8_t I2C_addr = 0x01;
#define HAL_DELAY 10

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_I2C1_Init(void);
static void MX_UART4_Init(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */
	uint8_t buf[50],buf1[10];
	char arr7[10];
	uint8_t i;
	int x,val;

  /* 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_I2C1_Init();
  MX_UART4_Init();
  /* USER CODE BEGIN 2 */
  //for(i=0;i<255;i++)
  //{
   // if(HAL_I2C_IsDeviceReady(&hi2c1,i, 2, HAL_DELAY) == HAL_OK)
   // {
    //   strcpy((char*)buf, "I2C ready to read \n\r");
   // }
  //}

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {

	  char MSG[] = "start of I2C2 comm \n\r";
	  HAL_UART_Transmit(&huart4,(uint8_t*)MSG,sizeof(MSG),1000);
	  HAL_Delay(2000);

	  //for(i=0;i<255;i++)
	  //{
	        //if(HAL_I2C_IsDeviceReady(&hi2c1,i, 2, HAL_DELAY) == HAL_OK)
	        //{
	        //   strcpy((char*)buf, "I2C  ready to read\r\n");
	        //}
		
	
	 	

		//HAL_UART_Transmit(&huart4, buf, strlen((char*)buf), HAL_DELAY);

		//buf[0] = 0x01;            
		//buf[1] = 0x08;
		HAL_I2C_Master_Transmit(&hi2c1,0xA8, buf, 2, HAL_DELAY);

		if(HAL_I2C_IsDeviceReady(&hi2c1,0xA9, 2, HAL_DELAY) == HAL_OK)
   		{
    		   strcpy((char*)buf, "I2C ready to read \n\r");
   		}


      		//HAL_Delay(20);
		//buf[0] = 0x01;
		//buf[1] = 0x88;
		//HAL_I2C_Master_Transmit(&hi2c1,0xA8, buf, 2, HAL_DELAY);
		//buf1[1] = 0;
      		//HAL_I2C_Master_Receive(&hi2c1, 0xA9, &buf1[1], 2, HAL_DELAY);
      		//HAL_Delay(20);
		//
		//
		//}

		
		

		//buf[0] = start_ADDR;
      		//HAL_I2C_Master_Transmit(&hi2c1, start_ADDR, buf, 2, HAL_DELAY);
      		//HAL_Delay(20);
		//
		//I2C  read write
		//buf[0] = 0x01; 
		//buf[1] = 50;
		//buf1[1]= 0;
		
      		//HAL_I2C_Master_Transmit(&hi2c1, I2C_addr<<1, buf, 2, HAL_DELAY);


      		//HAL_I2C_Master_Receive(&hi2c1,0x01, &buf1[1], 2, HAL_DELAY);
		//sprintf((char*)buf,"%d \n",buf1[1]);

		//Combine the bytes
                //val = ((int16_t)buf[2] << 4);
		//sprintf((char*)buf,"%u \n",val);

		HAL_UART_Transmit(&huart4, buf, strlen((char*)buf), HAL_DELAY);
		//buf[2] = 0;

		//Combine the bytes
                //val = ((int16_t)buf[2] << 4) | (buf[1] >> 4);

#if 0	
		buf[1] = 55;
		buf[2] =0;
      		HAL_I2C_Master_Transmit(&hi2c1, 0x01, buf, 2, HAL_DELAY);
      		HAL_I2C_Master_Receive(&hi2c1, 0x01, &buf[2], 2, HAL_DELAY);
		sprintf((char*)buf,"%u \r\n",buf[2]);
		HAL_UART_Transmit(&huart4, buf, strlen((char*)buf), HAL_DELAY);

		buf[1] = 60;
      		HAL_I2C_Master_Transmit(&hi2c1, 0x02, buf, 2, HAL_DELAY);
		HAL_I2C_Master_Receive(&hi2c1, 0x02, &buf[2], 2, HAL_DELAY);
		sprintf((char*)buf,"%u \r\n",buf[2]);
		HAL_UART_Transmit(&huart4, buf, strlen((char*)buf), HAL_DELAY);

		buf[2] = 0;
      		//HAL_I2C_Master_Transmit(&hi2c1, 0x02, buf, 2, HAL_DELAY);
		HAL_I2C_Master_Receive(&hi2c1, 0xFC, &buf[2], 2, HAL_DELAY);
		sprintf((char*)buf,"%u \r\n",buf[2]);
		HAL_UART_Transmit(&huart4, buf, strlen((char*)buf), HAL_DELAY);
		buf[1] = 0x00;
#endif 



#if 0	
	for (i=0;i<=5;++i){
        x=(buf[i]&0xF0)>>4;

	

	switch(x){
            case 0://send_serial_byte('0'); 
		   strcpy(arr7,"0");
		   break;
            case 1://send_serial_byte('1');
		   strcpy(arr7,"1");
		   break;
            case 2:///send_serial_byte('2');
		   strcpy(arr7,"2");
		   break;
            case 3://send_serial_byte('3'); 
		   strcpy(arr7,"3");
		   break;
            case 4://send_serial_byte('4');
		   strcpy(arr7,"4");
		   break;
            case 5://send_serial_byte('5');
		   strcpy(arr7,"5");
		   break;
            case 6://send_serial_byte('6');
		   strcpy(arr7,"6");
		   break;
            case 7://send_serial_byte('7'); 
		   strcpy(arr7,"7");
		   break;
            case 8://send_serial_byte('8'); 
		   strcpy(arr7,"8");
		   break;
            case 9://send_serial_byte('9'); 
		   strcpy(arr7,"9");
		   break;
            case 10://send_serial_byte('A');
		    strcpy(arr7,"A");
		    break;
            case 11://send_serial_byte('B'); 
		    strcpy(arr7,"B");
		    break;
            case 12://send_serial_byte('C'); 
		    strcpy(arr7,"C");
		    break;
            case 13://send_serial_byte('D');
		    strcpy(arr7,"D");
		    break;
            case 14://send_serial_byte('E'); 
		    strcpy(arr7,"E");
		    break;
            case 15://send_serial_byte('F'); 
		    strcpy(arr7,"F");
		    break;
        }
	//strcat(arr8,arr7);
        x=buf[i]&0x0F;
        switch(x){
            case 0://send_serial_byte('0'); 
		   strcpy(arr7,"0");
		   break;
            case 1://send_serial_byte('1'); 
		   strcpy(arr7,"1");
		   break;
            case 2://send_serial_byte('2');
		   strcpy(arr7,"2");
		   break;
            case 3://send_serial_byte('3'); 
		   strcpy(arr7,"3");
		   break;
            case 4://send_serial_byte('4');
		   strcpy(arr7,"4");
		   break;
            case 5://send_serial_byte('5'); 
		   strcpy(arr7,"5");
		   break;
            case 6://send_serial_byte('6'); 
		   strcpy(arr7,"6");
		   break;
            case 7://send_serial_byte('7'); 
		   strcpy(arr7,"7");
		   break;
            case 8://send_serial_byte('8');
		   strcpy(arr7,"8");
		   break;
            case 9://send_serial_byte('9'); 
		   strcpy(arr7,"9");
		   break;
            case 10://send_serial_byte('A'); 
		   strcpy(arr7,"A");
		    break;
            case 11://send_serial_byte('B');
		   strcpy(arr7,"B");
		    break;
            case 12://send_serial_byte('C'); 
		   strcpy(arr7,"C");
		    break;
            case 13://send_serial_byte('D'); 
		   strcpy(arr7,"D");
		    break;
            case 14://send_serial_byte('E'); 
		   strcpy(arr7,"E");
		    break;
            case 15://send_serial_byte('F'); 
		   strcpy(arr7,"F");
		    break;
        }
	//strcat(arr8,arr7);
        //if(i!=5){
		//send_serial_byte(':'); 
	//	strcat(arr8,":");
	//}
	}
#endif 	


	//sprintf((char*)buf,"%u \r\n",buf);
  	//HAL_UART_Transmit(&huart4, buf, strlen((char*)buf), HAL_DELAY);
	#if 0
      		HAL_I2C_Master_Receive(&hi2c1, 0xFC, &buf[1], 2, HAL_DELAY);
		sprintf((char*)buf,"%u \r\n",buf[1]);
		HAL_UART_Transmit(&huart4, buf, strlen((char*)buf), HAL_DELAY);
      		
		HAL_I2C_Master_Receive(&hi2c1, 0xFD, &buf[1], 2, HAL_DELAY);
		sprintf((char*)buf,"%u \r\n",buf[1]);
		HAL_UART_Transmit(&huart4, buf, strlen((char*)buf), HAL_DELAY);

		HAL_I2C_Master_Receive(&hi2c1, 0xFE, &buf[1], 2, HAL_DELAY);
		sprintf((char*)buf,"%u \r\n",buf[1]);
		HAL_UART_Transmit(&huart4, buf, strlen((char*)buf), HAL_DELAY);

		HAL_I2C_Master_Receive(&hi2c1, 0xFF, &buf[1], 2, HAL_DELAY);
		sprintf((char*)buf,"%u \r\n",buf[1]);
		HAL_UART_Transmit(&huart4, buf, strlen((char*)buf), HAL_DELAY);
	#endif 








		//sprintf((char*)buf,"%u \r\n",buf[1]);
		//HAL_UART_Transmit(&huart4, buf, strlen((char*)buf), HAL_DELAY);



	   //}

	  //HAL_UART_Transmit(&huart4, buf, strlen((char*)buf), HAL_DELAY);
    /* 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_MSI;
  RCC_OscInitStruct.MSIState = RCC_MSI_ON;
  RCC_OscInitStruct.MSICalibrationValue = 0;
  RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_6;
  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_MSI;
  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_UART4|RCC_PERIPHCLK_I2C1;
  PeriphClkInit.Uart4ClockSelection = RCC_UART4CLKSOURCE_PCLK1;
  PeriphClkInit.I2c1ClockSelection = RCC_I2C1CLKSOURCE_PCLK1;
  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  {
    Error_Handler();
  }
  /** Configure the main internal regulator output voltage 
  */
  if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
  {
    Error_Handler();
  }
}

/**
  * @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.Timing = 0x00000E14;
  hi2c1.Init.OwnAddress1 = 0;
  hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  hi2c1.Init.OwnAddress2 = 0;
  hi2c1.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
  hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  if (HAL_I2C_Init(&hi2c1) != HAL_OK)
  {
    Error_Handler();
  }
  /** Configure Analogue filter 
  */
  if (HAL_I2CEx_ConfigAnalogFilter(&hi2c1, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
  {
    Error_Handler();
  }
  /** Configure Digital filter 
  */
  if (HAL_I2CEx_ConfigDigitalFilter(&hi2c1, 0) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN I2C1_Init 2 */

  /* USER CODE END I2C1_Init 2 */

}

/**
  * @brief UART4 Initialization Function
  * @param None
  * @retval None
  */
static void MX_UART4_Init(void)
{

  /* USER CODE BEGIN UART4_Init 0 */

  /* USER CODE END UART4_Init 0 */

  /* USER CODE BEGIN UART4_Init 1 */

  /* USER CODE END UART4_Init 1 */
  huart4.Instance = UART4;
  huart4.Init.BaudRate = 9600;
  huart4.Init.WordLength = UART_WORDLENGTH_8B;
  huart4.Init.StopBits = UART_STOPBITS_1;
  huart4.Init.Parity = UART_PARITY_NONE;
  huart4.Init.Mode = UART_MODE_TX_RX;
  huart4.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart4.Init.OverSampling = UART_OVERSAMPLING_16;
  huart4.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  huart4.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  if (HAL_UART_Init(&huart4) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN UART4_Init 2 */

  /* USER CODE END UART4_Init 2 */

}

/**
  * @brief GPIO Initialization Function
  * @param None
  * @retval None
  */
static void MX_GPIO_Init(void)
{

  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOA_CLK_ENABLE();
  __HAL_RCC_GPIOB_CLK_ENABLE();

}

/* 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****/
