/*
**
**                           Main.c
**
**
**********************************************************************/
/*
   Last committed:     $Revision: 00 $
   Last changed by:    $Author: $
   Last changed date:  $Date:  $
   ID:                 $Id:  $

**********************************************************************/
#include "stm32f4xx_conf.h"
#include "stm32f4xx.h"
#include "stm32f4xx_spi.h"



int main(void)
{

    init_SPI1();

    while(1) {

    }
}



void init_SPI1(void)
{
  GPIO_InitTypeDef GPIO_InitStruct;
  SPI_InitTypeDef SPI_InitStruct;
  NVIC_InitTypeDef NVIC_InitStruct;

  // Taktversorgung der benötigten Peripherie
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
  //RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI1,ENABLE);

  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);  // nur PreemptionPriority ist von Bedeutung

  // SPI-Ports
  GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_InitStruct.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStruct.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;  // SPI1_NSS, SPI1_SCK, SPI1_MISO, SPI1_MOSI
  GPIO_Init(GPIOA, &GPIO_InitStruct);

  GPIO_PinAFConfig(GPIOA,GPIO_PinSource4,GPIO_AF_SPI1);
  GPIO_PinAFConfig(GPIOA,GPIO_PinSource5,GPIO_AF_SPI1);
  GPIO_PinAFConfig(GPIOA,GPIO_PinSource6,GPIO_AF_SPI1);
  GPIO_PinAFConfig(GPIOA,GPIO_PinSource7,GPIO_AF_SPI1);

  // SPI1-Schnittstelle
  SPI_InitStruct.SPI_Direction=SPI_Direction_2Lines_FullDuplex;
  SPI_InitStruct.SPI_Mode=SPI_Mode_Slave;
  SPI_InitStruct.SPI_DataSize=SPI_DataSize_8b;
  SPI_InitStruct.SPI_CPOL=SPI_CPOL_Low;
  SPI_InitStruct.SPI_CPHA=SPI_CPHA_1Edge;
  SPI_InitStruct.SPI_NSS=SPI_NSS_Hard;
  SPI_InitStruct.SPI_BaudRatePrescaler=SPI_BaudRatePrescaler_8;
  SPI_InitStruct.SPI_FirstBit=SPI_FirstBit_MSB;
  SPI_InitStruct.SPI_CRCPolynomial=7;
  SPI_Init(SPI1,&SPI_InitStruct);
  SPI_I2S_ITConfig(SPI1,SPI_I2S_IT_RXNE,ENABLE);  // Interrupt, wenn Daten empfangen
  SPI_Cmd(SPI1,ENABLE);

  // SPI1-Interrupt
  NVIC_InitStruct.NVIC_IRQChannel = SPI1_IRQn;
  NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0x0A;
  NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0x08;
  NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStruct);

}


void SPI1_IRQHandler(void)
{
  uint16_t d=SPI1->DR;
}



