/**
  **************************************************************************
  * @file     main.c
  * @version  v2.0.0
  * @date     2021-11-26
  * @brief    main program
  **************************************************************************
  *                       Copyright notice & Disclaimer
  *
  * The software Board Support Package (BSP) that is made available to 
  * download from Artery official website is the copyrighted work of Artery. 
  * Artery authorizes customers to use, copy, and distribute the BSP 
  * software and its related documentation for the purpose of design and 
  * development in conjunction with Artery microcontrollers. Use of the 
  * software is governed by this copyright notice and the following disclaimer.
  *
  * THIS SOFTWARE IS PROVIDED ON "AS IS" BASIS WITHOUT WARRANTIES,
  * GUARANTEES OR REPRESENTATIONS OF ANY KIND. ARTERY EXPRESSLY DISCLAIMS,
  * TO THE FULLEST EXTENT PERMITTED BY LAW, ALL EXPRESS, IMPLIED OR
  * STATUTORY OR OTHER WARRANTIES, GUARANTEES OR REPRESENTATIONS,
  * INCLUDING BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
  *
  **************************************************************************
  */

#include "at32f413_board.h"
#include "at32f413_clock.h"
#include <stdio.h>

/** @addtogroup AT32F413_periph_examples
  * @{
  */

/** @addtogroup 413_I2S_spii2s_switch_halfduplex_polling I2S_spii2s_switch_halfduplex_polling
  * @{
  */

uint16_t i2s1_buffer_tx[32] = {0x0102, 0x0304, 0x0506, 0x0708, 0x090A, 0x0B0C,
                               0x0D0E, 0x0F10, 0x1112, 0x1314, 0x1516, 0x1718,
                               0x191A, 0x1B1C, 0x1D1E, 0x1F20, 0x2122, 0x2324,
                               0x2526, 0x2728, 0x292A, 0x2B2C, 0x2D2E, 0x2F30,
                               0x3132, 0x3334, 0x3536, 0x3738, 0x393A, 0x3B3C,
                               0x3D3E, 0x3F40
                              };
uint16_t spi1_buffer_tx[32] = {0x5152, 0x5354, 0x5556, 0x5758, 0x595A, 0x5B5C,
                               0x5D5E, 0x5F60, 0x6162, 0x6364, 0x6566, 0x6768,
                               0x696A, 0x6B6C, 0x6D6E, 0x6F70, 0x7172, 0x7374,
                               0x7576, 0x7778, 0x797A, 0x7B7C, 0x7D7E, 0x7F80,
                               0x8182, 0x8384, 0x8586, 0x8788, 0x898A, 0x8B8C,
                               0x8D8E, 0x8F90
                              };
uint16_t i2s2_buffer_rx[32];
uint16_t spi2_buffer_rx[32];
__IO uint32_t tx_index = 0, rx_index = 0;
volatile error_status transfer_status1 = ERROR, transfer_status2 = ERROR, transfer_status3 = ERROR;
                                  
static void gpio_config(void);
static void spi_config(void);
static void i2s_config(i2s_operation_mode_type i2s1_mode, i2s_operation_mode_type i2s2_mode);
error_status buffer_compare(uint16_t* pbuffer1, uint16_t* pbuffer2, uint16_t buffer_length);

/**
  * @brief  buffer_compare function.
  * @param  none
  * @retval the result of compare
  */
error_status buffer_compare(uint16_t* pbuffer1, uint16_t* pbuffer2, uint16_t buffer_length)
{
  while(buffer_length--)
  {
    if(*pbuffer1 != *pbuffer2)
    {
      return ERROR;
    }

    pbuffer1++;
    pbuffer2++;
  }
  return SUCCESS;
}
                                        
/**
  * @brief  i2s configuration.
  * @param  none
  * @retval none
  */
static void i2s_config(i2s_operation_mode_type i2s1_mode, i2s_operation_mode_type i2s2_mode)
{
  i2s_init_type i2s_init_struct;
  
  crm_periph_clock_enable(CRM_SPI1_PERIPH_CLOCK, TRUE);
  crm_periph_clock_enable(CRM_SPI2_PERIPH_CLOCK, TRUE);
  
  spi_i2s_reset(SPI2);
  spi_i2s_reset(SPI1);
  i2s_default_para_init(&i2s_init_struct);
  i2s_init_struct.audio_protocol = I2S_AUDIO_PROTOCOL_PHILLIPS;
  i2s_init_struct.data_channel_format = I2S_DATA_16BIT_CHANNEL_32BIT;
  i2s_init_struct.mclk_output_enable = FALSE;
  i2s_init_struct.audio_sampling_freq = I2S_AUDIO_FREQUENCY_48K;
  i2s_init_struct.clock_polarity = I2S_CLOCK_POLARITY_LOW;
  i2s_init_struct.operation_mode = i2s1_mode;
  i2s_init(SPI1, &i2s_init_struct);
  
  i2s_init_struct.operation_mode =i2s2_mode;
  i2s_init(SPI2, &i2s_init_struct);
}

/**
  * @brief  spi configuration.
  * @param  none
  * @retval none
  */
static void spi_config(void)
{
  spi_init_type spi_init_struct;
  crm_periph_clock_enable(CRM_SPI1_PERIPH_CLOCK, TRUE);
  crm_periph_clock_enable(CRM_SPI2_PERIPH_CLOCK, TRUE);
  
  spi_i2s_reset(SPI2);
  spi_i2s_reset(SPI1);
  spi_default_para_init(&spi_init_struct);
  spi_init_struct.transmission_mode = SPI_TRANSMIT_HALF_DUPLEX_TX;
  spi_init_struct.master_slave_mode =SPI_MODE_MASTER;
  spi_init_struct.mclk_freq_division = SPI_MCLK_DIV_8;
  spi_init_struct.first_bit_transmission = SPI_FIRST_BIT_MSB;
  spi_init_struct.frame_bit_num = SPI_FRAME_16BIT;
  spi_init_struct.clock_polarity = SPI_CLOCK_POLARITY_LOW;
  spi_init_struct.clock_phase = SPI_CLOCK_PHASE_2EDGE;
  spi_init_struct.cs_mode_selection = SPI_CS_SOFTWARE_MODE;
  spi_init(SPI1, &spi_init_struct);
  
  spi_init_struct.transmission_mode = SPI_TRANSMIT_SIMPLEX_RX;
  spi_init_struct.master_slave_mode =SPI_MODE_SLAVE;
  spi_init(SPI2, &spi_init_struct);
  
  spi_enable(SPI2, TRUE);
  spi_enable(SPI1, TRUE);
}

/**
  * @brief  gpio configuration.
  * @param  none
  * @retval none
  */
static void gpio_config(void)
{
  gpio_init_type gpio_initstructure;
  crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, TRUE);
  crm_periph_clock_enable(CRM_GPIOB_PERIPH_CLOCK, TRUE);
  
  /* master ws pin */
  gpio_initstructure.gpio_out_type       = GPIO_OUTPUT_PUSH_PULL;  
  gpio_initstructure.gpio_pull           = GPIO_PULL_UP;  
  gpio_initstructure.gpio_mode           = GPIO_MODE_MUX;  
  gpio_initstructure.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
  gpio_initstructure.gpio_pins           = GPIO_PINS_4;
  gpio_init(GPIOA, &gpio_initstructure);
  
  /* master ck pin */
  gpio_initstructure.gpio_pull           = GPIO_PULL_DOWN;  
  gpio_initstructure.gpio_mode           = GPIO_MODE_MUX;  
  gpio_initstructure.gpio_pins           = GPIO_PINS_5;
  gpio_init(GPIOA, &gpio_initstructure);
  
  /* master sd pin */ 
  gpio_initstructure.gpio_pull           = GPIO_PULL_DOWN;  
  gpio_initstructure.gpio_mode           = GPIO_MODE_MUX;  
  gpio_initstructure.gpio_pins           = GPIO_PINS_7;
  gpio_init(GPIOA, &gpio_initstructure);
  
  /* master mck pin */
  gpio_initstructure.gpio_pull           = GPIO_PULL_DOWN;  
  gpio_initstructure.gpio_mode           = GPIO_MODE_MUX;  
  gpio_initstructure.gpio_pins           = GPIO_PINS_0;
  gpio_init(GPIOB, &gpio_initstructure);
  
  /* slave ws pin */
  gpio_initstructure.gpio_pull           = GPIO_PULL_UP;  
  gpio_initstructure.gpio_mode           = GPIO_MODE_MUX;  
  gpio_initstructure.gpio_pins           = GPIO_PINS_12;
  gpio_init(GPIOB, &gpio_initstructure);
  
  /* slave ck pin */
  gpio_initstructure.gpio_pull           = GPIO_PULL_DOWN;  
  gpio_initstructure.gpio_mode           = GPIO_MODE_MUX;  
  gpio_initstructure.gpio_pins           = GPIO_PINS_13;
  gpio_init(GPIOB, &gpio_initstructure);
  
  /* slave sd pin */ 
  gpio_initstructure.gpio_pull           = GPIO_PULL_DOWN;  
  gpio_initstructure.gpio_mode           = GPIO_MODE_MUX;  
  gpio_initstructure.gpio_pins           = GPIO_PINS_15;
  gpio_init(GPIOB, &gpio_initstructure);
}

/**
  * @brief  main function.
  * @param  none
  * @retval none
  */
int main(void)
{
  __IO uint32_t index = 0;
  system_clock_config();
  at32_board_init();
  at32_led_off(LED2);
  at32_led_off(LED3);
  at32_led_off(LED4);
  gpio_config();
  i2s_config(I2S_MODE_MASTER_TX, I2S_MODE_SLAVE_RX);
  i2s_enable(SPI2, TRUE);
  i2s_enable(SPI1, TRUE);
  while(rx_index < 32)
  {
    while(spi_i2s_flag_get(SPI1, SPI_I2S_TDBE_FLAG) == RESET);
    spi_i2s_data_transmit(SPI1, i2s1_buffer_tx[tx_index++]);
    while(spi_i2s_flag_get(SPI2, SPI_I2S_RDBF_FLAG) == RESET);
    i2s2_buffer_rx[rx_index++] = spi_i2s_data_receive(SPI2);
  }
  
  /* test result:the data check */
  transfer_status1 = buffer_compare(i2s2_buffer_rx, i2s1_buffer_tx, 32);
  
  tx_index = 0;
  rx_index = 0;
  spi_config();
  while(rx_index < 32)
  {
    while(spi_i2s_flag_get(SPI1, SPI_I2S_TDBE_FLAG) == RESET);
    spi_i2s_data_transmit(SPI1, spi1_buffer_tx[tx_index++]);
    while(spi_i2s_flag_get(SPI2, SPI_I2S_RDBF_FLAG) == RESET);
    spi2_buffer_rx[rx_index++] = spi_i2s_data_receive(SPI2);
  }
  
  /* test result:the data check */
  transfer_status2 = buffer_compare(spi2_buffer_rx, spi1_buffer_tx, 32);

  for(index = 0; index < 32; index++)  i2s2_buffer_rx[index] = 0;
  tx_index = 0;
  rx_index = 0;
  i2s_config(I2S_MODE_SLAVE_TX, I2S_MODE_MASTER_RX);
  while(spi_i2s_flag_get(SPI1, SPI_I2S_TDBE_FLAG) == RESET);
  spi_i2s_data_transmit(SPI1, i2s1_buffer_tx[tx_index++]);
  i2s_enable(SPI1, TRUE);
  i2s_enable(SPI2, TRUE);
  while(rx_index < 32)
  {
    while(spi_i2s_flag_get(SPI2, SPI_I2S_RDBF_FLAG) == RESET);
    i2s2_buffer_rx[rx_index++] = spi_i2s_data_receive(SPI2);
    while(spi_i2s_flag_get(SPI1, SPI_I2S_TDBE_FLAG) == RESET);
    spi_i2s_data_transmit(SPI1, i2s1_buffer_tx[tx_index++]);
  }
  
  /* test result:the data check */
  transfer_status3 = buffer_compare(i2s2_buffer_rx, i2s1_buffer_tx, 32);
  
  /* test result indicate:if passed ,led2 lights */
  if((transfer_status1 == SUCCESS) && (transfer_status2 == SUCCESS) && (transfer_status3 == SUCCESS))
  {
    at32_led_on(LED2);
  }    
  else
  {
    at32_led_off(LED2);
  }
  while(1)
  {
  }
}

/**
  * @}
  */ 

/**
  * @}
  */ 
