/*
 * H_STM32F4_SPI_1_2.c
 *
 *  Created on: 2024/11/21
 *      Author: egaono-electronics
 */

#include "H_STM32F4_SPI_1_2.h"



//----------------------------------------------------------
// SPI1用パラメータ
//----------------------------------------------------------

//----------------------------------
// SPI handler declaration
//----------------------------------
SPI_HandleTypeDef Handle_SPI1;

DMA_HandleTypeDef hdma_spi1_tx;
DMA_HandleTypeDef hdma_spi1_rx;
//----------------------------------


// 送信Buffer : SPI1
uint8_t GLB_uint8_aSPI1_TxBuffer[def_SPI1_TxBufferSize];

// 受信Buffer : SPI1
uint8_t GLB_uint8_aSPI1_RxBuffer[def_SPI1_RxBufferSize];


//------------------------------------------------------
// Handle SPI用パラメータの構造体の宣言 : SPI1
//------------------------------------------------------
struct sParam_Handle_SPIx  GLB_structParam_Handle_SPI1;
//----------------------------------------------------------



//----------------------------------------------------------
// SPI2用パラメータ
//----------------------------------------------------------

//----------------------------------
// SPI handler declaration
//----------------------------------
SPI_HandleTypeDef Handle_SPI2;

DMA_HandleTypeDef hdma_spi2_tx;
DMA_HandleTypeDef hdma_spi2_rx;
//----------------------------------


// 送信Buffer : SPI1
uint8_t GLB_uint8_aSPI2_TxBuffer[def_SPI2_TxBufferSize];

// 受信Buffer : SPI1
uint8_t GLB_uint8_aSPI2_RxBuffer[def_SPI2_RxBufferSize];


//------------------------------------------------------
// Handle SPI用パラメータの構造体の宣言 : SPI2
//------------------------------------------------------
struct sParam_Handle_SPIx  GLB_structParam_Handle_SPI2;
//----------------------------------------------------------



void HAL_SPI_MspInit(SPI_HandleTypeDef* hspi)
{
  GPIO_InitTypeDef GPIO_InitStruct;


  if(hspi->Instance == SPI1)
  {
    // USER CODE BEGIN SPI1_MspInit 0

    //--------------------------------------
    // Peripheral clock enable
    //--------------------------------------
    __HAL_RCC_SPI1_CLK_ENABLE();  // SPI1

    __HAL_RCC_GPIOB_CLK_ENABLE();
            // SPI1_CLK, SPI1_MISO, SPI1_MOSI
    __HAL_RCC_GPIOA_CLK_ENABLE();  // SPI1_NSS


    __HAL_RCC_DMA2_CLK_ENABLE();  // DMA2
    //--------------------------------------



    //------------------------------------------------------
    // SPI1 GPIO Configuration
    //------------------------------------------------------
    // SPI1_CLK, SPI1_MISO, SPI1_MOSI
    GPIO_InitStruct.Pin = def_GPIO_PIN_x_SPI1_SCK
        | def_GPIO_PIN_x_SPI1_MISO | def_GPIO_PIN_x_SPI1_MOSI;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF5_SPI1;

    HAL_GPIO_Init(def_GPIOx_SPI1, &GPIO_InitStruct);


    // SPI1_NSS
    GPIO_InitStruct.Pin = def_GPIO_PIN_x_SPI1_NSS;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    GPIO_InitStruct.Pull = GPIO_PULLUP;
    GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
    HAL_GPIO_Init(def_GPIOx_SPI1_NSS, &GPIO_InitStruct);

    // NSS OFF
    HAL_GPIO_WritePin(def_GPIOx_SPI1_NSS,
                      def_GPIO_PIN_x_SPI1_NSS,
                      GPIO_PIN_SET);
    //------------------------------------------------------



    // Peripheral DMA initialize

    //------------------------------------------------------
    // Transmit
    //------------------------------------------------------
    hdma_spi1_tx.Instance = DMA2_Stream3;
    hdma_spi1_tx.Init.Channel = DMA_CHANNEL_3;
    hdma_spi1_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
    hdma_spi1_tx.Init.PeriphInc = DMA_PINC_DISABLE;
    hdma_spi1_tx.Init.MemInc = DMA_MINC_ENABLE;
    hdma_spi1_tx.Init.PeriphDataAlignment
                                = DMA_PDATAALIGN_BYTE;
    hdma_spi1_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
    hdma_spi1_tx.Init.Mode = DMA_NORMAL;
    hdma_spi1_tx.Init.Priority = DMA_PRIORITY_HIGH;
    hdma_spi1_tx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;

    hdma_spi1_tx.Init.FIFOThreshold
                    = DMA_FIFO_THRESHOLD_1QUARTERFULL;

    hdma_spi1_tx.Init.MemBurst = DMA_MBURST_SINGLE;
    hdma_spi1_tx.Init.PeriphBurst = DMA_PBURST_SINGLE;

    HAL_DMA_Init(&hdma_spi1_tx);

    __HAL_LINKDMA(hspi, hdmatx, hdma_spi1_tx);
    //------------------------------------------------------



    //------------------------------------------------------
    // Receive
    //------------------------------------------------------
    hdma_spi1_rx.Instance = DMA2_Stream2;
    hdma_spi1_rx.Init.Channel = DMA_CHANNEL_3;
    hdma_spi1_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
    hdma_spi1_rx.Init.PeriphInc = DMA_PINC_DISABLE;
    hdma_spi1_rx.Init.MemInc = DMA_MINC_ENABLE;
    hdma_spi1_rx.Init.PeriphDataAlignment
                                = DMA_PDATAALIGN_BYTE;
    hdma_spi1_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
    hdma_spi1_rx.Init.Mode = DMA_NORMAL;
    hdma_spi1_rx.Init.Priority = DMA_PRIORITY_HIGH;
    hdma_spi1_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;

    hdma_spi1_tx.Init.FIFOThreshold
                        = DMA_FIFO_THRESHOLD_1QUARTERFULL;

    hdma_spi1_rx.Init.MemBurst = DMA_MBURST_SINGLE;
    hdma_spi1_rx.Init.PeriphBurst = DMA_PBURST_SINGLE;

    HAL_DMA_Init(&hdma_spi1_rx);

    __HAL_LINKDMA(hspi, hdmarx, hdma_spi1_rx);
    //------------------------------------------------------


    // Configure the DMA IRQ priority
    HAL_NVIC_SetPriority(DMA2_Stream3_IRQn, 12, 0);
                                            // Transmit

    // Configure the DMA IRQ priority
    HAL_NVIC_SetPriority(DMA2_Stream2_IRQn, 12, 0);
                                            // Receive

    // USER CODE END SPI1_MspInit 0
  }
  else if(hspi->Instance == SPI2)
  {
    // USER CODE BEGIN SPI2_MspInit 0

    //--------------------------------------
    // Peripheral clock enable
    //--------------------------------------
    __HAL_RCC_SPI2_CLK_ENABLE();  // SPI2

    __HAL_RCC_GPIOB_CLK_ENABLE();
        // SPI2_CLK, SPI2_MISO, SPI2_MOSI, SPI2_NSS


    __HAL_RCC_DMA1_CLK_ENABLE();  // DMA1
    //--------------------------------------


    //------------------------------------------------------
    // SPI2 GPIO Configuration
    //------------------------------------------------------
    // SPI2_CLK, SPI2_MISO, SPI2_MOSI
    GPIO_InitStruct.Pin = def_GPIO_PIN_x_SPI2_SCK
        | def_GPIO_PIN_x_SPI2_MISO | def_GPIO_PIN_x_SPI2_MOSI;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF5_SPI2;

    HAL_GPIO_Init(def_GPIOx_SPI2, &GPIO_InitStruct);


    // SPI2_NSS
    GPIO_InitStruct.Pin = def_GPIO_PIN_x_SPI2_NSS;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    GPIO_InitStruct.Pull = GPIO_PULLUP;
    GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
    HAL_GPIO_Init(def_GPIOx_SPI2_NSS, &GPIO_InitStruct);

    // NSS OFF
    HAL_GPIO_WritePin(def_GPIOx_SPI2_NSS,
                      def_GPIO_PIN_x_SPI2_NSS,
                      GPIO_PIN_SET);
    //------------------------------------------------------



    //------------------------------------------------------
    // Peripheral DMA initialize
    //------------------------------------------------------

    //------------------------------------------------------
    // Transmit
    //------------------------------------------------------
    hdma_spi2_tx.Instance = DMA1_Stream4;
    hdma_spi2_tx.Init.Channel = DMA_CHANNEL_0;
    hdma_spi2_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
    hdma_spi2_tx.Init.PeriphInc = DMA_PINC_DISABLE;
    hdma_spi2_tx.Init.MemInc = DMA_MINC_ENABLE;
    hdma_spi2_tx.Init.PeriphDataAlignment
                                    = DMA_PDATAALIGN_BYTE;
    hdma_spi2_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
    hdma_spi2_tx.Init.Mode = DMA_NORMAL;
    hdma_spi2_tx.Init.Priority = DMA_PRIORITY_HIGH;
    hdma_spi2_tx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;

    hdma_spi2_tx.Init.FIFOThreshold
                        = DMA_FIFO_THRESHOLD_1QUARTERFULL;

    hdma_spi2_tx.Init.MemBurst = DMA_MBURST_SINGLE;
    hdma_spi2_tx.Init.PeriphBurst = DMA_PBURST_SINGLE;

    HAL_DMA_Init(&hdma_spi2_tx);

    __HAL_LINKDMA(hspi, hdmatx, hdma_spi2_tx);
    //------------------------------------------------------



    //------------------------------------------------------
    // Receive
    //------------------------------------------------------
    hdma_spi2_rx.Instance = DMA1_Stream3;
    hdma_spi2_rx.Init.Channel = DMA_CHANNEL_0;
    hdma_spi2_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
    hdma_spi2_rx.Init.PeriphInc = DMA_PINC_DISABLE;
    hdma_spi2_rx.Init.MemInc = DMA_MINC_ENABLE;
    hdma_spi2_rx.Init.PeriphDataAlignment
                                    = DMA_PDATAALIGN_BYTE;
    hdma_spi2_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
    hdma_spi2_rx.Init.Mode = DMA_NORMAL;
    hdma_spi2_rx.Init.Priority = DMA_PRIORITY_HIGH;
    hdma_spi2_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;

    hdma_spi2_tx.Init.FIFOThreshold
                        = DMA_FIFO_THRESHOLD_1QUARTERFULL;

    hdma_spi2_rx.Init.MemBurst = DMA_MBURST_SINGLE;
    hdma_spi2_rx.Init.PeriphBurst = DMA_PBURST_SINGLE;

    HAL_DMA_Init(&hdma_spi2_rx);

    __HAL_LINKDMA(hspi, hdmarx, hdma_spi2_rx);
    //------------------------------------------------------


    // Configure the DMA IRQ priority
    HAL_NVIC_SetPriority(DMA1_Stream4_IRQn, 12, 0);
                                            // Transmit

    // Configure the DMA IRQ priority
    HAL_NVIC_SetPriority(DMA1_Stream3_IRQn, 12, 0);
                                            // Receive

    // USER CODE END SPI2_MspInit 0
  }
}



void HAL_SPI_MspDeInit(SPI_HandleTypeDef* hspi)
{
  if(hspi->Instance==SPI1)
  {
    // USER CODE BEGIN SPI1_MspDeInit 0

    // Peripheral clock disable
    __HAL_RCC_SPI1_FORCE_RESET();
    __HAL_RCC_SPI1_RELEASE_RESET();


    // SPI1 GPIO Configuration
    // SPI1_CLK, SPI1_MISO, SPI1_MOSI
    HAL_GPIO_DeInit(def_GPIOx_SPI1,
        def_GPIO_PIN_x_SPI1_SCK | def_GPIO_PIN_x_SPI1_MISO
        | def_GPIO_PIN_x_SPI1_MOSI);

    // SPI1_NSS
    HAL_GPIO_DeInit(def_GPIOx_SPI1_NSS, def_GPIO_PIN_x_SPI1_NSS);


    // Peripheral DMA DeInit
    HAL_DMA_DeInit(hspi->hdmatx);
    HAL_DMA_DeInit(hspi->hdmarx);


    // Disable the TIMx global Interrupt
    HAL_NVIC_DisableIRQ(DMA2_Stream3_IRQn);  // Transmit
    HAL_NVIC_DisableIRQ(DMA2_Stream2_IRQn);  // Receive

    // USER CODE END SPI1_MspDeInit 0
  }
  else if(hspi->Instance==SPI2){
    // USER CODE BEGIN SPI2_MspDeInit 0

    // Peripheral clock disable
    __HAL_RCC_SPI2_FORCE_RESET();
    __HAL_RCC_SPI2_RELEASE_RESET();


    // SPI2 GPIO Configuration
    // SPI2_CLK, SPI2_MISO, SPI2_MOSI
    HAL_GPIO_DeInit(def_GPIOx_SPI2,
        def_GPIO_PIN_x_SPI2_SCK | def_GPIO_PIN_x_SPI2_MISO
        | def_GPIO_PIN_x_SPI2_MOSI);

    // SPI2_NSS
    HAL_GPIO_DeInit(def_GPIOx_SPI2_NSS, def_GPIO_PIN_x_SPI2_NSS);


    // Peripheral DMA DeInit
    HAL_DMA_DeInit(hspi->hdmatx);
    HAL_DMA_DeInit(hspi->hdmarx);


    // Disable the TIMx global Interrupt
    HAL_NVIC_DisableIRQ(DMA1_Stream4_IRQn);  // Transmit
    HAL_NVIC_DisableIRQ(DMA1_Stream3_IRQn);  // Receive

    // USER CODE END SPI2_MspDeInit 0
  }
}
