
/**
 * @file xmc_prng.c
 * @date 2015-06-20
 *
 * @cond
 *****************************************************************************
 * XMClib v2.2.0 - XMC Peripheral Driver Library
 *
 * Copyright (c) 2015-2020, Infineon Technologies AG
 * All rights reserved.
 *
 * Boost Software License - Version 1.0 - August 17th, 2003
 *
 * Permission is hereby granted, free of charge, to any person or organization
 * obtaining a copy of the software and accompanying documentation covered by
 * this license (the "Software") to use, reproduce, display, distribute,
 * execute, and transmit the Software, and to prepare derivative works of the
 * Software, and to permit third-parties to whom the Software is furnished to
 * do so, all subject to the following:
 *
 * The copyright notices in the Software and this entire statement, including
 * the above license grant, this restriction and the following disclaimer,
 * must be included in all copies of the Software, in whole or in part, and
 * all derivative works of the Software, unless such copies or derivative
 * works are solely in the form of machine-executable object code generated by
 * a source language processor.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
 * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
 * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 *
 * To improve the quality of the software, users are encouraged to share
 * modifications, enhancements or bug fixes with Infineon Technologies AG
 * at XMCSupport@infineon.com.
 *****************************************************************************
 *
 * Change History
 * --------------
 *
 * 2015-02-20:
 *     - Initial <br>
 *     - Removed GetDriverVersion API <br>
 *
 * 2015-06-20
 *     - Removed definition of GetDriverVersion API <br>
 *
 * @endcond
 */

#include "xmc_prng.h"

#if defined (PRNG)

/*********************************************************************************************************************
 * API IMPLEMENTATION
 *********************************************************************************************************************/

/*
 * Initializes the PRNG peripheral with the settings in the
 * initialization structure XMC_PRNG_INIT_t
 */
XMC_PRNG_INIT_STATUS_t XMC_PRNG_Init(const XMC_PRNG_INIT_t *prng)
{
  volatile uint16_t read_warm_up;
  uint16_t reg_val, iter;
  XMC_PRNG_INIT_STATUS_t status = XMC_PRNG_INITIALIZED;

  XMC_ASSERT("XMC_PRNG_Init:Null Pointer", (prng != (XMC_PRNG_INIT_t *)NULL));

  /* Configure block size for key loading mode */
  XMC_PRNG_SetRandomDataBlockSize(XMC_PRNG_RDBS_WORD);

  /* Enable key loading mode */
  XMC_PRNG_EnableKeyLoadingMode();

  /* Load key words (80 bits) and wait till RDV is set */
  for (iter = (uint16_t)0UL; iter < (uint16_t)5UL; iter++)
  {
    XMC_PRNG_LoadKeyWords(prng->key_words[iter]);
    while (PRNG_CHK_RDV_Msk != XMC_PRNG_CheckValidStatus());
  }

  XMC_PRNG_EnableStreamingMode();

  /* Warm up phase: Read and discard 64 bits */
  read_warm_up = PRNG->WORD;
  read_warm_up = PRNG->WORD;
  read_warm_up = PRNG->WORD;
  reg_val      = PRNG->WORD;
  read_warm_up &= reg_val;

  /* Configure block size either byte (8 bit) or word (16 bit) */
  XMC_PRNG_SetRandomDataBlockSize(prng->block_size);

  /*
   * Checks for reset value for "random data block size". If reset,
   * PRNG is not initialized
   */
  if ((uint16_t)XMC_PRNG_RDBS_RESET == (PRNG->CTRL & (uint16_t)PRNG_CTRL_RDBS_Msk))
  {
    status = XMC_PRNG_NOT_INITIALIZED;
  }

  return status;
}

#endif /* #if defined (PRNG) */
