// filename: spi.c
// RealSYS KCO
#include "common.h"
#include "spi.h"

void init_spi1(void){   // for MASTER mode
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef   SPI_InitStructure;
    /* Enable GPIO clock for SPI_MASTER */
    RCC_APB2PeriphClockCmd(SPI_MASTER_GPIO_CLK | RCC_APB2Periph_AFIO, ENABLE);
    
    /* Enable SPI_MASTER Periph clock */
    RCC_APB2PeriphClockCmd(SPI_MASTER_CLK, ENABLE);                           

    /* Configure SPI pins: SCK, MISO and MOSI */
    GPIO_InitStructure.GPIO_Pin = SPI_MASTER_PIN_SCK | SPI_MASTER_PIN_MISO 
      | SPI_MASTER_PIN_MOSI;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_Init(SPI_MASTER_GPIO, &GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
    GPIO_Init(SPI_MASTER_GPIO, &GPIO_InitStructure);
    H_SS1;

    /* SPI_MASTER configuration ------------------------------------------------*/
    SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
    SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
    SPI_InitStructure.SPI_DataSize = SPI_DataSize_16b;  // or SPI_DataSize_8b;
    SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; // or SPI_CPOL_Low;
    SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
    SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
    SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8;
    SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
    SPI_InitStructure.SPI_CRCPolynomial = 7;
    SPI_Init(SPI_MASTER, &SPI_InitStructure);

    /* Enable SPI_MASTER */
    SPI_Cmd(SPI_MASTER, ENABLE);
}

void init_spi2(void){   // for SLAVE mode
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef   SPI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
    /* Enable GPIO clock for SPI_SLAVE */
    RCC_APB1PeriphClockCmd(SPI_SLAVE_GPIO_CLK | RCC_APB2Periph_AFIO, ENABLE);
    /* Enable SPI_SLAVE Periph clock */
    RCC_APB1PeriphClockCmd(SPI_SLAVE_CLK, ENABLE);                           

    /* Configure SPI pins: SCK, MISO and MOSI */
    GPIO_InitStructure.GPIO_Pin = SPI_SLAVE_PIN_SCK 
      | SPI_SLAVE_PIN_MISO | SPI_SLAVE_PIN_MOSI;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_Init(SPI_SLAVE_GPIO, &GPIO_InitStructure);

    /* SPI_SLAVE configuration ------------------------------------------------*/
    SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
    SPI_InitStructure.SPI_Mode = SPI_Mode_Slave;
    SPI_InitStructure.SPI_DataSize = SPI_DataSize_16b;  //or SPI_DataSize_8b;
    SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; // SPI_CPOL_Low;
    SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
    SPI_InitStructure.SPI_NSS = SPI_NSS_Hard;
//    SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
    SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
    SPI_InitStructure.SPI_CRCPolynomial = 7;
    SPI_Init(SPI_SLAVE, &SPI_InitStructure);

    /* 1 bit for pre-emption priority, 3 bits for subpriority */
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);

    /* Configure and enable SPI_SLAVE interrupt --------------------------------*/
    NVIC_InitStructure.NVIC_IRQChannel = SPI_SLAVE_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
    NVIC_Init(&NVIC_InitStructure);

    /* Enable SPI_SLAVE RXNE interrupt */
    SPI_I2S_ITConfig(SPI_SLAVE, SPI_I2S_IT_RXNE, ENABLE);
    /* Enable SPI_SLAVE */
    SPI_Cmd(SPI_SLAVE, ENABLE);
}

#define SPI_BUF_LEN 20
u16 spi2_rx_data[SPI_BUF_LEN+1];
u8 spi2_rx_idx=0,spi2_rx_flag=0;
void SPI2_IRQHandler(void){
    if(spi2_rx_idx<SPI_BUF_LEN)
      spi2_rx_data[spi2_rx_idx++] = SPI_I2S_ReceiveData(SPI_SLAVE);
    spi2_rx_flag = 1;  
}

void send_spi1_data(u16 data){
    L_SS1;
    SPI_I2S_SendData(SPI_MASTER, data);
    while(SPI_I2S_GetFlagStatus(SPI_MASTER, SPI_I2S_FLAG_BSY) == SET);
    H_SS1;
}

void send_spi1_datas(u8 len, u16 *dat){
int i;
    L_SS1;
    for(i=0;i<len;i++){
        SPI_I2S_SendData(SPI_MASTER, dat[i]);
        while(SPI_I2S_GetFlagStatus(SPI_MASTER, SPI_I2S_FLAG_BSY) == SET);
    }
    H_SS1;
}