/**
*   @file      hal437.c
*   @author    IPC - Industrial BU
*   @date      27 june 2017
*   @brief     This source code includes Background task related functions
*   @note      (C) COPYRIGHT 2017 STMicroelectronics
*
* @attention
* THE PRESENT SOFTWARE 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, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
* CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*
*/

/*******************************************************************************
* INCLUDE FILES:
*******************************************************************************/
#include "stm32l4xx_hal.h"

#include "hal_l4xx.h"
#include "mxconstants_l462.h"

/** @addtogroup GENERIC
  * @{
  */

/*******************************************************************************
* CONSTANTS & MACROS:
*******************************************************************************/

/*******************************************************************************
* TYPES:
*******************************************************************************/

extern SPI_HandleTypeDef hspi1;
void HAL_SFLASH_Conf(void)
{
  GPIO_InitTypeDef GPIO_InitStruct;
  // init already done in main by MX
  // configure GPIO for CS
  GPIO_InitStruct.Pin = SFLASH_SSn_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
  HAL_GPIO_Init(SFLASH_SSn_GPIO_Port, &GPIO_InitStruct);
  // enable SPI4
  __HAL_SPI_ENABLE(&hspi1);
}

void HAL_SFLASH_SSOutputCmd(uint8_t enable)
{
  if (enable != 0)
  {
    HAL_GPIO_WritePin(SFLASH_SSn_GPIO_Port, SFLASH_SSn_Pin, GPIO_PIN_RESET);
  }
  else
  {
    HAL_GPIO_WritePin(SFLASH_SSn_GPIO_Port, SFLASH_SSn_Pin, GPIO_PIN_SET);
  }
}

/**
  * @brief  Sends a byte through the SPI interface and return the byte received
  *         from the SPI bus.
  * @param  byte : byte to send.
  * @return The value of the received byte.
  */
uint8_t HAL_SFLASH_SendByte(uint8_t byte)
{
	UART_Print("HAL_SFLASH_SendByte\r\n");
	volatile uint32_t tmp;
    do
    {
      tmp = SPI1->SR;
    }
    while ((tmp & SPI_FLAG_TXE) == 0);
    tmp = SPI1->DR; // to clear RXNE
    SPI1->DR = byte;
    do
    {
      tmp = SPI1->SR;
    }
    while ((tmp & SPI_FLAG_RXNE) == 0);
    UART_Print("HAL_SFLASH_SendByte %x\r\n",SPI1->DR);
    return(SPI1->DR);
}



/**
* @brief  This function starts reception on the SPI interface
* @param  len is the message length
* @param  pData is a pointer to the message
* @retval None
*/
void HAL_SpiRxStart(uint16_t len,uint8_t* pData)
{
  /* not used */
}

/**
* @brief  This function starts transmission on the SPI interface
* @param  len is the message length
* @retval None
*/
void HAL_SpiTxStart(uint16_t len, uint8_t* pdata)
{
  /* not used */
}

/**
* @brief  This function handles SPI request pin
* @param  enable indicates if request is enabled or not
* @retval None
*/
void HAL_SpiReqpin(uint8_t enable)
{
  /* not used */
}




/* End Of File */
