#include "stm32c0xx_hal.h"
#include <stdio.h>
#include <string.h>

// UART handle
UART_HandleTypeDef huart1;

// Timer handle
TIM_HandleTypeDef htim1;

// Global counter
volatile uint64_t counter = 0; // To keep track of the number of times printed

// Function prototypes
void SystemClock_Config(void);
static void MX_USART1_UART_Init(void);
static void MX_TIM1_Init(void);
void Serial_Init(void);
void Serial_print(const char *msg);

int main(void) {
    // Initialize HAL Library
    HAL_Init();
  
    // Configure the system clock
    SystemClock_Config();

    // Initialize UART1, Timer1, and Serial
    MX_USART1_UART_Init();
    MX_TIM1_Init();
    Serial_Init();

    // Start the timer in interrupt mode
    HAL_TIM_Base_Start_IT(&htim1);

    // Main loop
    while (1) {
        // Infinite loop, all actions happen in the interrupt
    }
}

// UART1 initialization function
static void MX_USART1_UART_Init(void) {
    huart1.Instance = USART1;
    huart1.Init.BaudRate = 9600;
    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;
    HAL_UART_Init(&huart1);
}

// Timer1 initialization function
static void MX_TIM1_Init(void) {
    htim1.Instance = TIM1;
    htim1.Init.Prescaler = 7999; // Prescaler to divide the clock to 1 kHz
    htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
    htim1.Init.Period = 1000;    // 1-second period
    htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
    HAL_TIM_Base_Init(&htim1);

    // Enable the interrupt for TIM1
    HAL_NVIC_SetPriority(TIM1_BRK_UP_TRG_COM_IRQn, 0, 0);
    HAL_NVIC_EnableIRQ(TIM1_BRK_UP_TRG_COM_IRQn);
}

// Serial initialization function
void Serial_Init(void) {
    // UART1 is already initialized to 9600 baud.
    const char *init_msg = "Serial initialized at 9600 baud.\r\n";
    HAL_UART_Transmit(&huart1, (uint8_t *)init_msg, strlen(init_msg), HAL_MAX_DELAY);
}

// Serial print function to display the data
void Serial_print(const char *msg) {
    // Transmit the message over UART1
    HAL_UART_Transmit(&huart1, (uint8_t *)msg, strlen(msg), HAL_MAX_DELAY);
}

// System Clock configuration function
void SystemClock_Config(void) {
    RCC_OscInitTypeDef RCC_OscInitStruct = {0};
    RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

    // Configure the clock
    RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
    RCC_OscInitStruct.HSIState = RCC_HSI_ON;
    RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
    HAL_RCC_OscConfig(&RCC_OscInitStruct);

    RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1;
    RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
    RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
    RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;

    HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0);
}

// Timer interrupt callback function
void HAL_TIM_PeriodElapsedCallback_1(TIM_HandleTypeDef *htim) {
    if (htim->Instance == TIM1) {
        // Prepare the message
        char msg[50];
        uint8_t n_bytes = sprintf(msg, "STM32 Timer Test %llu\n\r", counter);

        // Transmit the message over UART1
        HAL_UART_Transmit(&huart1, (uint8_t *)msg, n_bytes, HAL_MAX_DELAY);

        // Also print the message using Serial.print equivalent
        Serial_print(msg);

        counter++;
    }
}

// TIM1 interrupt handler
void TIM1_BRK_UP_TRG_COM_IRQHandler(void) {
    HAL_TIM_IRQHandler(&htim1); // Call HAL handler
}