/* USER CODE BEGIN Header */
/**
 ******************************************************************************
 * @file           : main.c
 * @brief          : Main program body
 ******************************************************************************
 * @attention
 *
 * Copyright (c) 2022 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"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stm32l4xx_hal.h"
#include "vl6180x_api.h"
#include "vl6180x_glue.h"

/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
#define VL6180X_ADDR (0x29 << 1)
#define VL6180X_REG_SYSTEM_INTERRUPT_CONFIG_GPIO 0x14
#define VL6180X_REG_SYSTEM_INTERRUPT_CLEAR 0x15
#define VL6180X_REG_SYSTEM_FRESH_OUT_OF_RESET 0x16
#define VL6180X_REG_SYSRANGE_START 0x18
#define VL6180X_REG_RESULT_RANGE_STATUS 0x4D
#define VL6180X_REG_RESULT_INTERRUPT_STATUS_GPIO 0x4F
#define VL6180X_REG_RESULT_RANGE_VAL 0x62
/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
I2C_HandleTypeDef hi2c1;

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_I2C1_Init(void);
static void MX_USART2_UART_Init(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
volatile uint16_t range = 0;

#if false
void tof_write_reg(uint16_t reg_addr, uint32_t data, int len_bytes) {
  uint8_t buf[len_bytes + 2];
  buf[0] = reg_addr >> 8;
  buf[1] = reg_addr & 0xFF;
  for (int idx = len_bytes + 1; idx >= 2; idx--) {
    buf[idx] = data & 0xFF;
    data >>= 8;
  }

  if (HAL_I2C_Master_Transmit(&hi2c1, VL6180X_ADDR, buf, len_bytes, 100) !=
      HAL_OK) {
    Error_Handler();
  }
}

uint32_t tof_read_reg(uint16_t reg_addr, int len_bytes) {
  // Write index
  uint8_t addr_buf[2] = {reg_addr >> 8, reg_addr & 0xFF};
  if (HAL_I2C_Master_Transmit(&hi2c1, VL6180X_ADDR, addr_buf, 2, 100) !=
      HAL_OK) {
    Error_Handler();
  }

  // Read data
  uint8_t buf[len_bytes];
  if (HAL_I2C_Master_Receive(&hi2c1, VL6180X_ADDR | 1, buf, len_bytes, 100) !=
      HAL_OK) {
    Error_Handler();
  }

  // Demarshal
  uint32_t result = 0;
  for (int i = 0; i < len_bytes; i++) {
    int shift = (len_bytes - i - 1) * 8;
    result |= ((uint32_t)buf[i]) << shift;
  }

  return result;
}

void tof_init(void) {
  if (tof_read_reg(VL6180X_REG_SYSTEM_FRESH_OUT_OF_RESET, 1) & 1) {
    // LOAD SETTINGS AS PER AN4545, SECTION 9
    // Mandatory : private registers
    tof_write_reg(0x0207, 0x01, 1);
    tof_write_reg(0x0208, 0x01, 1);
    tof_write_reg(0x0096, 0x00, 1);
    tof_write_reg(0x0097, 0xfd, 1);
    tof_write_reg(0x00e3, 0x01, 1);
    tof_write_reg(0x00e4, 0x03, 1);
    tof_write_reg(0x00e5, 0x02, 1);
    tof_write_reg(0x00e6, 0x01, 1);
    tof_write_reg(0x00e7, 0x03, 1);
    tof_write_reg(0x00f5, 0x02, 1);
    tof_write_reg(0x00d9, 0x05, 1);
    tof_write_reg(0x00db, 0xce, 1);
    tof_write_reg(0x00dc, 0x03, 1);
    tof_write_reg(0x00dd, 0xf8, 1);
    tof_write_reg(0x009f, 0x00, 1);
    tof_write_reg(0x00a3, 0x3c, 1);
    tof_write_reg(0x00b7, 0x00, 1);
    tof_write_reg(0x00bb, 0x3c, 1);
    tof_write_reg(0x00b2, 0x09, 1);
    tof_write_reg(0x00ca, 0x09, 1);
    tof_write_reg(0x0198, 0x01, 1);
    tof_write_reg(0x01b0, 0x17, 1);
    tof_write_reg(0x01ad, 0x00, 1);
    tof_write_reg(0x00ff, 0x05, 1);
    tof_write_reg(0x0100, 0x05, 1);
    tof_write_reg(0x0199, 0x05, 1);
    tof_write_reg(0x01a6, 0x1b, 1);
    tof_write_reg(0x01ac, 0x3e, 1);
    tof_write_reg(0x01a7, 0x1f, 1);
    tof_write_reg(0x0030, 0x00, 1);

    // Recommended : Public registers - See data sheet for more detail
    tof_write_reg(0x0011, 0x10, 1); // Enables polling for ‘New Sample ready’
    // when measurement completes
    tof_write_reg(0x010a, 0x30, 1); // Set the averaging sample period
    // (compromise between lower noise and
    // increased execution time)
    tof_write_reg(0x003f, 0x46, 1); // Sets the light and dark gain (upper
    // nibble). Dark gain should not be
    // changed.
    tof_write_reg(0x0031, 0xFF, 1); // sets the # of range measurements after
    // which auto calibration of system is
    // performed
    tof_write_reg(0x0041, 0x63, 1); // Set ALS integration time to 100ms
    tof_write_reg(0x002e, 0x01, 1); // perform a single temperature calibration
    // of the ranging sensor

    // Optional: Public registers - See data sheet for more detail
    tof_write_reg(0x001b, 0x09, 1); // Set default ranging inter-measurement
    // period to 100ms
    tof_write_reg(0x003e, 0x31, 1); // Set default ALS inter-measurement period
    // to 500ms
    tof_write_reg(0x0014, 0x24, 1); // Configures interrupt on ‘New Sample
                                    // Ready threshold event’

    tof_write_reg(VL6180X_REG_SYSTEM_FRESH_OUT_OF_RESET, 0, 1);
    HAL_Delay(10);
    if (tof_read_reg(VL6180X_REG_SYSTEM_FRESH_OUT_OF_RESET, 1) & 1) {
      Error_Handler();
    }
  }
  // tof_write_reg(VL6180X_REG_SYSTEM_INTERRUPT_CONFIG_GPIO, 4, 1);
}

uint8_t tof_range_poll(void) {
  uint8_t status = tof_read_reg(VL6180X_REG_RESULT_RANGE_STATUS, 1);
  uint8_t fresh = tof_read_reg(VL6180X_REG_SYSTEM_FRESH_OUT_OF_RESET, 1);
  if (!(status & 1) || (fresh & 1)) {
    // Not ready
    Error_Handler();
  }

  tof_write_reg(VL6180X_REG_SYSRANGE_START, 0b01, 1);
  while (1) {
    uint8_t status = tof_read_reg(VL6180X_REG_RESULT_INTERRUPT_STATUS_GPIO, 1);
    if (status & 0b11000000) {
      // Error bits
      Error_Handler();
    }
    if ((status & 0b111) == 4) {
      // New sample ready threshold event
      break;
    }
    status = tof_read_reg(VL6180X_REG_RESULT_RANGE_STATUS, 1);
    if (status & 0xF0) {
      // Error code
      Error_Handler();
    }
    HAL_Delay(10);
  }
  uint8_t range = tof_read_reg(VL6180X_REG_RESULT_RANGE_VAL, 2);

  // Clear interrupt
  tof_write_reg(VL6180X_REG_SYSTEM_INTERRUPT_CLEAR, 0x07, 1);

  return range;
}
#endif
/* USER CODE END 0 */

/**
 * @brief  The application entry point.
 * @retval int
 */
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_I2C1_Init();
  MX_USART2_UART_Init();
  /* USER CODE BEGIN 2 */
  HAL_Delay(10);
  vl6180x_glue_init(&hi2c1);

  VL6180xDev_t my_dev;
  VL6180x_RangeData_t my_range;
  VL6180x_InitData(my_dev);
  VL6180x_Prepare(my_dev);
  /* USER CODE END 2 */

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

    /* USER CODE BEGIN 3 */
    VL6180x_RangePollMeasurement(my_dev, &my_range);
    if (my_range.errorStatus == 0) {
      range = my_range.range_mm;
    } else {
      HAL_Delay(100);
    }
  }
  /* 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_HSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  RCC_OscInitStruct.PLL.PLLM = 1;
  RCC_OscInitStruct.PLL.PLLN = 10;
  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();
  }
}

/**
 * @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 = 0x10909CEC;
  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 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;
  huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  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_GPIOH_CLK_ENABLE();
  __HAL_RCC_GPIOA_CLK_ENABLE();
  __HAL_RCC_GPIOB_CLK_ENABLE();

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_RESET);

  /*Configure GPIO pin : B1_Pin */
  GPIO_InitStruct.Pin = B1_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  HAL_GPIO_Init(B1_GPIO_Port, &GPIO_InitStruct);

  /*Configure GPIO pin : LD2_Pin */
  GPIO_InitStruct.Pin = LD2_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(LD2_GPIO_Port, &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 */
  __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 */