//
// Created by Luca on 9/7/2025.
//

#ifndef SHAL_UART_REG_F072XB_H
#define SHAL_UART_REG_F072XB_H

#include <stm32h753xx.h>
#include <cassert>

#include "SHAL_UART_TYPES.h"

#define NUM_USART_LINES 4

#define SHAL_UART1 UART(1)
#define SHAL_UART2 UART(2)
#define SHAL_UART3 UART(3)
#define SHAL_UART4 UART(4)

//Valid usart Tx and Rx pairings for STM32F072
enum class UART_Pair_Key : uint8_t{
    //UART2
    Tx2A2_Rx2A3,
    Tx3D8_Rx3D9,

    NUM_PAIRS,
    INVALID
};

static SHAL_UART_Pair getUARTPair(const UART_Pair_Key pair){
    switch(pair){
        case UART_Pair_Key::Tx2A2_Rx2A3:
            return {USART2, GPIO_Key::A2, GPIO_Key::A3, GPIO_Alternate_Function::AF7, GPIO_Alternate_Function::AF7};
        case UART_Pair_Key::Tx3D8_Rx3D9:
            return {USART3, GPIO_Key::D8, GPIO_Key::D9, GPIO_Alternate_Function::AF7, GPIO_Alternate_Function::AF7};
        case UART_Pair_Key::NUM_PAIRS:
        case UART_Pair_Key::INVALID:
            assert(false);
            return {nullptr, GPIO_Key::INVALID, GPIO_Key::INVALID, GPIO_Alternate_Function::AF0, GPIO_Alternate_Function::AF0};
        default:
            __builtin_unreachable();
    }
}

constexpr SHAL_UART_Enable_Register getUARTEnableReg(const UART_Pair_Key pair){
    switch(pair){
        case UART_Pair_Key::Tx2A2_Rx2A3:
            return {&RCC->APB1LENR,RCC_APB1LENR_USART2EN};
        case UART_Pair_Key::Tx3D8_Rx3D9:
            return {&RCC->APB1LENR,RCC_APB1LENR_USART3EN};
        case UART_Pair_Key::NUM_PAIRS:
        case UART_Pair_Key::INVALID:
            assert(false);
            return {nullptr, 0};
    }
    __builtin_unreachable();
}

static SHAL_UART_Control_Register_1 getUARTControlRegister1(const UART_Pair_Key key) {
    SHAL_UART_Control_Register_1 res = {
        nullptr,
        USART_CR1_UE,
        USART_CR1_TE,
        USART_CR1_RE,
        USART_CR1_M0,
        USART_CR1_M1,
        USART_CR1_RXNEIE
    };

    res.reg = &getUARTPair(key).USARTReg->CR1;
    return res;
};

static SHAL_UART_Baud_Rate_Generation_Register getUARTBaudRateRegister(const UART_Pair_Key key) {
    SHAL_UART_Baud_Rate_Generation_Register res = {nullptr};

    res.reg = &getUARTPair(key).USARTReg->BRR;
    return res;
}

static SHAL_UART_ISR getUARTISR(const UART_Pair_Key key) { //TODO Support for multiple alternates for FIFO
    SHAL_UART_ISR res = {
        nullptr,
        USART_ISR_TXE_TXFNF,
        USART_ISR_TC,
        USART_ISR_BUSY
    };

    res.reg = &getUARTPair(key).USARTReg->ISR;
    return res;
}

static SHAL_UART_Transmit_Data_Register getUARTTransmitDataRegister(const UART_Pair_Key key) {
    return {&getUARTPair(key).USARTReg->TDR};
}

static

constexpr uint32_t getAFMask(const GPIO_Alternate_Function mask){
    switch(mask){
        case GPIO_Alternate_Function::AF0: return 0x00;
        case GPIO_Alternate_Function::AF1: return 0x01;
        case GPIO_Alternate_Function::AF2: return 0x02;
        case GPIO_Alternate_Function::AF3: return 0x03;
        case GPIO_Alternate_Function::AF4: return 0x04;
        case GPIO_Alternate_Function::AF5: return 0x05;
        case GPIO_Alternate_Function::AF6: return 0x06;
        case GPIO_Alternate_Function::AF7: return 0x07;
    }
    __builtin_unreachable();
}

#endif //SHMINGO_HAL_SHAL_UART_REG_F072XB_H
