/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2025 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 "dma.h"
#include "fdcan.h"
#include "memorymap.h"
#include "tim.h"
#include "gpio.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include <string.h>

#include "mappings.h"
/* USER CODE END Includes */

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

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
#define TIM_BASE_FREQ 96000000UL
/* USER CODE END PD */

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

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */
unsigned int mscounter;
unsigned int setup_complete;

FDCAN_HandleTypeDef *hMainCAN, *hPeriCAN;
FDCAN_TxHeaderTypeDef txHeader;

/* Declare buffer in D1 domain SRAM */
static uint16_t adc_values[NUM_ADC_PINS];
static uint8_t  dio_values[NUM_DIO_PINS];

// See mappings.h pwm_tim_t
TIM_HandleTypeDef* PWM_TIM_MAP[3] = {&htim1, &htim4, &htim3};

static uint8_t  pwm_ch_active[8];

static uint16_t wss_flanks[2];
/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_NVIC_Init(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

void loop_1kHz() {

  if (!setup_complete)
    return;

  mscounter++;

  for (int di = 0; di < NUM_DIO_PINS; di++) {
    dio_values[di] = HAL_GPIO_ReadPin(
      DIO_PIN_MAP[di].port,
      DIO_PIN_MAP[di].pin
    );
  }

  for (int pi = 0; pi < NUM_TX_PKT; pi++) {

    can_pkt_t* pktinfo = &(CAN_SIGNAL_MAP[pi]);

    if (pktinfo->num_signals < 0)
      continue;

    if (mscounter % pktinfo->period == 0) {

      txHeader.Identifier = pktinfo->can_id;
      txHeader.DataLength = pktinfo->dlc;
      uint64_t txData = 0;

      for (int si = 0; si < pktinfo->num_signals; si++) {

        can_signal_t* signal = &(pktinfo->signals[si]);
        uint16_t value = 0;
        switch (signal->type) {
          case DIN:
            value = dio_values[signal->channel];
            break;

          case AIN:
            value = signal->factor * adc_values[signal->channel];
            break;

          case FIN:
            value = wss_flanks[signal->channel];
            wss_flanks[signal->channel] = 0;
            break;

          default:
            break;
        }

        uint16_t mask = 0xFFFF >> (16 - signal->length);
        txData |= ((uint64_t) (value & mask)) << (signal->start);

      }

      if (HAL_FDCAN_AddMessageToTxFifoQ(hMainCAN, &txHeader, (uint8_t*) &txData) != HAL_OK)
        Error_Handler();

    }

    if (mscounter >= 500) {
      mscounter = 0;
      HAL_GPIO_TogglePin(STATUS_G_GPIO_Port, STATUS_G_Pin);
    }

  }

}

/* 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_DMA_Init();
  MX_ADC1_Init();
  MX_FDCAN1_Init();
  MX_FDCAN2_Init();
  MX_TIM1_Init();
  MX_TIM3_Init();
  MX_TIM8_Init();
  MX_TIM6_Init();
  MX_TIM4_Init();

  /* Initialize interrupts */
  MX_NVIC_Init();
  /* USER CODE BEGIN 2 */
  
  hMainCAN = &hfdcan1;
  hPeriCAN = &hfdcan2;

  if (HAL_ADCEx_Calibration_Start(&hadc1, ADC_CALIB_OFFSET, ADC_SINGLE_ENDED) != HAL_OK)
    Error_Handler();

  if (HAL_ADC_Start_DMA(&hadc1, (uint32_t*)adc_values, NUM_ADC_PINS) != HAL_OK)
    Error_Handler();

  HAL_TIM_Base_Start_IT(&htim6);

  // CAN TX PREP

  // Prep the tx frame
  txHeader.ErrorStateIndicator = FDCAN_ESI_PASSIVE;
  txHeader.BitRateSwitch = FDCAN_BRS_OFF;
  txHeader.FDFormat = FDCAN_CLASSIC_CAN;
  txHeader.TxEventFifoControl = FDCAN_NO_TX_EVENTS;
  txHeader.IdType = FDCAN_STANDARD_ID;
  txHeader.Identifier = 0x0;
  txHeader.TxFrameType = FDCAN_DATA_FRAME;
  txHeader.DataLength = 8;

  if (HAL_FDCAN_ActivateNotification(hMainCAN, FDCAN_IT_RX_FIFO0_NEW_MESSAGE, 0) != HAL_OK)
    Error_Handler();

  if (HAL_FDCAN_ConfigGlobalFilter(hMainCAN, FDCAN_REJECT, FDCAN_REJECT,
                                    FDCAN_REJECT_REMOTE, FDCAN_REJECT_REMOTE) != HAL_OK)
    Error_Handler();

  FDCAN_FilterTypeDef filter;
  filter.IdType = FDCAN_STANDARD_ID;
  filter.FilterIndex = 0;
  filter.FilterType = FDCAN_FILTER_MASK;
  filter.FilterConfig = FDCAN_FILTER_TO_RXFIFO0;
  filter.FilterID1 = CAN_PWM_BASE_ID;
  filter.FilterID2 = CAN_PWM_FILTER_MASK;

  if (HAL_FDCAN_ConfigFilter(hMainCAN, &filter) != HAL_OK)
    Error_Handler();

  if (HAL_FDCAN_Start(hMainCAN) != HAL_OK)
    Error_Handler();

  // Init all channels as stopped
  memset(pwm_ch_active, 0, 8);

  // Start input capture for WSS
  HAL_TIM_IC_Start_IT(&htim8, TIM_CHANNEL_1);
  HAL_TIM_IC_Start_IT(&htim8, TIM_CHANNEL_2);

  mscounter = 0;
  setup_complete = 1;

  /* USER CODE END 2 */

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

    /* USER CODE BEGIN 3 */
    //HAL_SuspendTick();
    //HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFI);
  }
  /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /*AXI clock gating */
  RCC->CKGAENR = 0xFFFFFFFF;

  /** Supply configuration update enable
  */
  HAL_PWREx_ConfigSupply(PWR_LDO_SUPPLY);

  /** Configure the main internal regulator output voltage
  */
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

  while(!__HAL_PWR_GET_FLAG(PWR_FLAG_VOSRDY)) {}

  /** 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 = 2;
  RCC_OscInitStruct.PLL.PLLN = 32;
  RCC_OscInitStruct.PLL.PLLP = 2;
  RCC_OscInitStruct.PLL.PLLQ = 32;
  RCC_OscInitStruct.PLL.PLLR = 32;
  RCC_OscInitStruct.PLL.PLLRGE = RCC_PLL1VCIRANGE_3;
  RCC_OscInitStruct.PLL.PLLVCOSEL = RCC_PLL1VCOWIDE;
  RCC_OscInitStruct.PLL.PLLFRACN = 0;
  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_CLOCKTYPE_D3PCLK1|RCC_CLOCKTYPE_D1PCLK1;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV2;
  RCC_ClkInitStruct.APB3CLKDivider = RCC_APB3_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV1;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_APB2_DIV1;
  RCC_ClkInitStruct.APB4CLKDivider = RCC_APB4_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  {
    Error_Handler();
  }
}

/**
  * @brief NVIC Configuration.
  * @retval None
  */
static void MX_NVIC_Init(void)
{
  /* DMA1_Stream0_IRQn interrupt configuration */
  HAL_NVIC_SetPriority(DMA1_Stream0_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(DMA1_Stream0_IRQn);
}

/* USER CODE BEGIN 4 */
/*void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
{
  if (hadc->Instance == ADC1)
  {
    __asm volatile ("NOP");
  }
}*/

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) {

  //HAL_GPIO_TogglePin(STATUS_B_GPIO_Port, STATUS_B_Pin);

  if (htim != &htim6)
    return;

  loop_1kHz();

};

/*
 * Input capture timing calculations:
 * wheel speed = 8000 rpm * (11/48) = 30.55 rot/s
 * if both flanks then 64 flanks per rot = 1956 flanks/s
 * That's ~500µs per flank or ~1ms per notch
 *
 * TIM8 trigger prescaled = 96MHz/64 = 3MHz
 * Max Filter is 15 samples, at 666.666ns per sample
 * that's 10µs of filtering (flanks that are unstable
 * for this long will not trigger a capture interrupt)
 *
 * If we want to do further filtering, we can either
 * increase the prescaler and decrease the counter
 * period or do it in software using
 * HAL_TIM_ReadCapturedValue(&htim8, TIM_CHANNEL_X);
 * to measure the time between events.
 */

void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim) {

  if (htim != &htim8 || htim->Channel < 1 || htim->Channel > 2)
    return;

  /*
   * Channels go 1,2,4,8... so we count the trailing zeros to get the index.
   * For this, we could use the nifty CTZ instruction, which gives us exactly that.
   * BUT according to godbolt we save 2 instructions by using subtraction,
   * which we can only do because we only use the first two channels.
   * Why 2 instructions? well, one because -1 can be done in the immediate when
   * writing to memory. Another one because ARMv7 32bit doesn't have CTZ but only
   * CLZ, so it must do RBIT before to reverse the result.
   *
   * CAREFUL: When using more than 2 channels, you MUST use CTZ instead.
   */
  wss_flanks[htim->Channel-1]++;
  //wss_flanks[__builtin_ctz(htim->Channel)]++;

}

void SetCCR(TIM_TypeDef* Instance, unsigned int ch, uint8_t dc) {
  (&(Instance->CCR1))[ch] = dc;
}

void HAL_FDCAN_RxFifo0Callback(FDCAN_HandleTypeDef *handle, uint32_t RxFifo0ITs)
{
  if (handle != hMainCAN || (RxFifo0ITs & FDCAN_IT_RX_FIFO0_NEW_MESSAGE) == RESET)
    return; // TODO: handle Peripheral CAN
  
  static FDCAN_RxHeaderTypeDef header;
  static uint8_t data[8];
  if (HAL_FDCAN_GetRxMessage(hMainCAN, FDCAN_RX_FIFO0, &header, data) != HAL_OK)
    return;

  if (header.FDFormat != FDCAN_CLASSIC_CAN ||
      header.RxFrameType != FDCAN_DATA_FRAME ||
      header.IdType != FDCAN_STANDARD_ID)
    return;
  
  switch (header.Identifier) {

    case CAN_PWM_DC_ID:

      uint8_t* dcs = data;
      for (int i = 0; i < header.DataLength; i++) {

        TIM_HandleTypeDef* htim = PWM_TIM_MAP[PWM_CH_MAP[i].tim];

        if ((pwm_ch_active[i] == 0) && (dcs[i] == 0)) {
          HAL_TIM_PWM_Stop(htim, PWM_CH_MAP[i].ch << 2);
          pwm_ch_active[i] = 0;
          continue;
        }

        SetCCR(htim->Instance, PWM_CH_MAP[i].ch, dcs[i]);

        if (pwm_ch_active[i] == 0) {
          HAL_TIM_PWM_Start(htim, PWM_CH_MAP[i].ch << 2);
          pwm_ch_active[i] = 1;
        }

      }
      break;

    case CAN_PWM_CONF_ID:

      uint16_t* freqs = (uint16_t*) data;
      for (int i = 0; i < (header.DataLength/2); i++) {

        uint32_t prescaler = (TIM_BASE_FREQ / (255UL * freqs[i])); // cast?
        PWM_TIM_MAP[i]->Instance->PSC = prescaler;

      }
      break;

    default:
      break;

  }

}

void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
  HAL_ResumeTick();
}

/* 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 */
  HAL_GPIO_WritePin(STATUS_R_GPIO_Port, STATUS_R_Pin, GPIO_PIN_SET);
  __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 */