/**
  ******************************************************************************
  * @file     SPI_CFG.c
  * @author   SINOMCU-AE
  * @brief    SPI1 config
  *    
  *          This file provides functions to SPI master mode0 initialization:
  *    PA5   --> SPI1_SCK
  *    PA6   <-- SPI1_MISO
  *    PA7   --> SPI1_MOSI
  *    PA4   --> CS GPIO 
  *    CPHA=0,CPOL=0
  *         
  ******************************************************************************
  * @attention 
  *                           DISCLAIMER
  * The present firmware which is for guidance only aims at providing customers
  * with coding information regarding their products in order for them to save
  * time. As a result, SinoMCU shall not be held liable for any direct,indirect
  * or consequential damages with respect to any claims arising from the content 
  * of such firmware and/or the use made by customers of the coding information 
  * contained herein in connection with their products.
  *
  * <h2><center>&copy; Copyright (c) 2021 sinomcu All rights reserved.</center></h2>
  *
  ******************************************************************************
  */

#include "SPI_CFG.h"

/**
  * @brief  SPI IO Initialization
  *    PA5   --> SPI1_SCK
  *    PA6   <-- SPI1_MISO
  *    PA7   --> SPI1_MOSI
  *    PA4   --> CS GPIO   
  * @param  None
  * @retval None
  */
void spi_gpio_init(void)
{
  MS32_GPIO_InitTypeDef GpioInitStruct = {0};

  MS32_AHB1_GRP1_EnableClock(MS32_AHB1_GRP1_PERIPH_GPIOA);

  // PA4 --> GPIO for CS
  MS32_GPIO_SetOutputPin(SPICS_GPIO_Port, SPICS_Pin);

  MS32_GPIO_StructInit(&GpioInitStruct);  
  GpioInitStruct.Pin = SPICS_Pin;
  GpioInitStruct.Mode = MS32_GPIO_MODE_OUTPUT;
  GpioInitStruct.Speed = MS32_GPIO_SPEED_FREQ_LOW;
  GpioInitStruct.OutputType = MS32_GPIO_OUTPUT_PUSHPULL;
  GpioInitStruct.Pull = MS32_GPIO_PULL_UP;
  MS32_GPIO_Init(SPICS_GPIO_Port, &GpioInitStruct);

  // PA5   --> SPI1_SCK
  // PA6   <-- SPI1_MISO
  // PA7   --> SPI1_MOSI
  GpioInitStruct.Pin = MS32_GPIO_PIN_5 | MS32_GPIO_PIN_6 | MS32_GPIO_PIN_7;
  GpioInitStruct.Mode = MS32_GPIO_MODE_ALTERNATE;
  GpioInitStruct.Speed = MS32_GPIO_SPEED_FREQ_HIGH;
  GpioInitStruct.OutputType = MS32_GPIO_OUTPUT_PUSHPULL;
  GpioInitStruct.Pull = MS32_GPIO_PULL_NO;
  GpioInitStruct.Alternate = MS32_GPIO_AF_0;
  MS32_GPIO_Init(GPIOA, &GpioInitStruct);
}

/**
  * @brief  SPI  Initialization
  *    CPHA=0,CPOL=0,8Bit 
  * @param  None
  * @retval None
  */
void spi_master_init(void)
{
  MS32_SPI_InitTypeDef SpiInitStruct = {0};

  spi_gpio_init();

  // master,8bit,Mode 0(CPHA=0,CPOL=0),CS soft
  MS32_SPI_StructInit(&SpiInitStruct);    
  SpiInitStruct.TransferDirection = MS32_SPI_FULL_DUPLEX;
  SpiInitStruct.Mode = MS32_SPI_MODE_MASTER;
  SpiInitStruct.DataWidth = MS32_SPI_DATAWIDTH_8BIT;
  SpiInitStruct.ClockPolarity = MS32_SPI_POLARITY_LOW;
  SpiInitStruct.ClockPhase = MS32_SPI_PHASE_1EDGE;
  SpiInitStruct.NSS = MS32_SPI_NSS_SOFT;
  // master BaudRate no more than 18M
  SpiInitStruct.BaudRate = MS32_SPI_BAUDRATEPRESCALER_DIV4;     // HSI 8M,BaudRate 2M; PLL 48M,BaudRate 12M
  SpiInitStruct.BitOrder = MS32_SPI_MSB_FIRST;
  SpiInitStruct.CRCCalculation = MS32_SPI_CRCCALCULATION_DISABLE;
  SpiInitStruct.CRCPoly = 7;
  MS32_SPI_Init(SPI1, &SpiInitStruct);
  MS32_SPI_SetStandard(SPI1, MS32_SPI_PROTOCOL_MOTOROLA);
  MS32_SPI_EnableNSSPulseMgt(SPI1);

  MS32_SPI_Enable(SPI1);
}


/******************************** END OF FILE *********************************/
