示例#1
0
/**
 * This function will initial STM32 Radio board.
 */
void rt_hw_board_init(void)
{
    /* Configure the system clocks */
    SystemInit();

    all_device_reset();

    /* NVIC Configuration */
    NVIC_Configuration();

    /* Configure the SysTick */
    SysTick_Config( SystemCoreClock / RT_TICK_PER_SECOND );

    /* Console Initialization*/
    rt_hw_usart_init();
    rt_console_set_device("uart1");

    rt_kprintf("\r\n\r\nSystemInit......\r\n");

    /* SPI1 config */
    {
        GPIO_InitTypeDef GPIO_InitStructure;
        SPI_InitTypeDef SPI_InitStructure;

        /* Enable SPI1 Periph clock */
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA
                               | RCC_APB2Periph_AFIO | RCC_APB2Periph_SPI1,
                               ENABLE);

        /* Configure SPI1 pins: PA5-SCK, PA6-MISO and PA7-MOSI */
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
        GPIO_Init(GPIOA, &GPIO_InitStructure);

        /*------------------------ SPI1 configuration ------------------------*/
        SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;//SPI_Direction_1Line_Tx;
        SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
        SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
        SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
        SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
        SPI_InitStructure.SPI_NSS  = SPI_NSS_Soft;
        SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16;/* 72M/64=1.125M */
        SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
        SPI_InitStructure.SPI_CRCPolynomial = 7;

        SPI_I2S_DeInit(SPI1);
        SPI_Init(SPI1, &SPI_InitStructure);

        /* Enable SPI_MASTER */
        SPI_Cmd(SPI1, ENABLE);
        SPI_CalculateCRC(SPI1, DISABLE);

        if (rt_sem_init(&spi1_lock, "spi1lock", 1, RT_IPC_FLAG_FIFO) != RT_EOK)
        {
            rt_kprintf("init spi1 lock semaphore failed\n");
        }
    }
}
示例#2
0
void SPI_Configuration(void)
{
	NVIC_InitTypeDef nvic;
	SPI_InitTypeDef SPI_InitStructure;
	DMA_InitTypeDef	DMA_InitStructure;
	GPIO_InitTypeDef GPIO_InitStructure;
	
	GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_7;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructure);

	SPI_Cmd(SPI1, DISABLE);
	DMA_DeInit(DMA1_Channel3);

	DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&SPI1->DR;
	DMA_InitStructure.DMA_MemoryBaseAddr = (u32) &fb[0][0];
	DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
	DMA_InitStructure.DMA_BufferSize = VTOTAL;
	DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
	DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
	DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
	DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
	DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
	DMA_InitStructure.DMA_Priority = DMA_Priority_Low;
	DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
	DMA_Init(DMA1_Channel3, &DMA_InitStructure);

	SPI_InitStructure.SPI_Direction = SPI_Direction_1Line_Tx;
	SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
	SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
	SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
	SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
	SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
	SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
	SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
	SPI_InitStructure.SPI_CRCPolynomial = 7;
	SPI_Init(SPI1, &SPI_InitStructure);
	
	SPI_CalculateCRC(SPI1, DISABLE);
	SPI_Cmd(SPI1, ENABLE);
	
	SPI1->CR2 |= SPI_I2S_DMAReq_Tx;
	
	nvic.NVIC_IRQChannel = DMA1_Channel3_IRQn;
	nvic.NVIC_IRQChannelPreemptionPriority = 0;
	nvic.NVIC_IRQChannelSubPriority = 0;
	nvic.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&nvic);
	
	DMA1_Channel3->CCR &= ~1;
	DMA1_Channel3->CNDTR = VTOTAL;
	DMA1_Channel3->CMAR = (u32) &fb[0][0];
	
	DMA_ITConfig(DMA1_Channel3, DMA_IT_TC, ENABLE);
}
示例#3
0
文件: spi1.c 项目: kvp7206/STM32F4_SD
/**
 * @brief Initialize SPI1 and SS pin.
 */
void SPI1_Init(void) {

  // Enable GPIO clock for SPI pins
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);

  /*
   * Initialize pins as alternate function, push-pull.
   * PA5 = SCK
   * PA6 = MISO
   * PA7 = MOSI
   */
  GPIO_InitTypeDef GPIO_InitStruct;
  GPIO_InitStruct.GPIO_Pin    = GPIO_Pin_7 | GPIO_Pin_6 | GPIO_Pin_5;
  GPIO_InitStruct.GPIO_Mode   = GPIO_Mode_AF;
  GPIO_InitStruct.GPIO_OType  = GPIO_OType_PP;
  GPIO_InitStruct.GPIO_Speed  = GPIO_Speed_100MHz;
  GPIO_InitStruct.GPIO_PuPd   = GPIO_PuPd_NOPULL;
  GPIO_Init(GPIOA, &GPIO_InitStruct);

  // Enable alternate functions of pins
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_SPI1);
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_SPI1);
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_SPI1);

  // Software chip select pin, PA4 = SS
  GPIO_InitStruct.GPIO_Pin    = GPIO_Pin_4;
  GPIO_InitStruct.GPIO_Mode   = GPIO_Mode_OUT;
  GPIO_InitStruct.GPIO_OType  = GPIO_OType_PP;
  GPIO_InitStruct.GPIO_Speed  = GPIO_Speed_100MHz;
  GPIO_InitStruct.GPIO_PuPd   = GPIO_PuPd_NOPULL;
  GPIO_Init(GPIOA, &GPIO_InitStruct);

  GPIO_SetBits(GPIOA, GPIO_Pin_4); // Set SS line

  // Enable SPI1 clock
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);

  SPI_InitTypeDef SPI_InitStruct;

  SPI_InitStruct.SPI_Direction          = SPI_Direction_2Lines_FullDuplex;
  SPI_InitStruct.SPI_Mode               = SPI_Mode_Master;
  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_Soft; // software chip select
  SPI_InitStruct.SPI_BaudRatePrescaler  = SPI_BaudRatePrescaler_256;
  SPI_InitStruct.SPI_FirstBit           = SPI_FirstBit_MSB;
  SPI_InitStruct.SPI_CRCPolynomial      = 7;
  SPI_Init(SPI1, &SPI_InitStruct);

  SPI_CalculateCRC(SPI1, DISABLE);
  SPI_Cmd(SPI1, ENABLE); // enable SPI1

}
示例#4
0
void	spi2_config(void)//
{
	RCC_AHB1PeriphClockCmd(SPI2_GPIO_BUS|SPI2_CS_GPIO_BUS, ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);

    GPIO_InitTypeDef GPIO_InitStructure;
    SPI_InitTypeDef SPI_InitStructure;

    /* Configure SPI1 pins: SCK, MISO and MOSI -------------------------------*/
    GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_13|GPIO_Pin_15;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_Init(SPI2_GPIO, &GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_14;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_Init(SPI2_GPIO, &GPIO_InitStructure);


    GPIO_InitStructure.GPIO_Pin   = SPI2_CS1;
    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_OUT;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(SPI2_GPIO_CS, &GPIO_InitStructure);

	GPIO_PinAFConfig(SPI2_GPIO, GPIO_PinSource13, GPIO_AF_SPI2);
	GPIO_PinAFConfig(SPI2_GPIO, GPIO_PinSource14, GPIO_AF_SPI2);
	GPIO_PinAFConfig(SPI2_GPIO, GPIO_PinSource15, GPIO_AF_SPI2);

    SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
    SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
    SPI_InitStructure.SPI_DataSize = SPI_DataSize_16b;
    SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
    SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
    SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
    SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_32;
    SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
    //SPI_InitStructure.SPI_CRCPolynomial = 7;
    SPI_Init(SPI2, &SPI_InitStructure);

    SPI_CalculateCRC(SPI2, DISABLE);
    SPI_Cmd(SPI2, ENABLE);

    //GPIO_WriteBit(GPIOB, GPIO_Pin_12, Bit_RESET);
    SPI2_GPIO_CS->BSRRH|=SPI2_CS1;
}
示例#5
0
void	SPI2_config(void)//
{
		RCC_AHB1PeriphClockCmd(SPI2_GPIO_BUS, ENABLE);
	    RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);

	    GPIO_InitTypeDef GPIO_InitStructure;
	    SPI_InitTypeDef SPI_InitStructure;

	    /* Configure SPI1 pins: SCK, MISO and MOSI -------------------------------*/
	    GPIO_InitStructure.GPIO_Pin   = SPI2_SCK|SPI2_MOSI;
	    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF;
	    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
	    GPIO_Init(SPI2_GPIO, &GPIO_InitStructure);

	    GPIO_InitStructure.GPIO_Pin   = SPI2_MISO;
	    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF;
	    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
	    GPIO_Init(SPI2_GPIO, &GPIO_InitStructure);


	    GPIO_InitStructure.GPIO_Pin   = SPI2_CS1;
	    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_OUT;
	    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
	    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	    GPIO_Init(SPI2_GPIO_CS, &GPIO_InitStructure);

		GPIO_PinAFConfig(SPI2_GPIO, SPI2_SCK_PinSource, GPIO_AF_SPI2);
		GPIO_PinAFConfig(SPI2_GPIO, SPI2_MOSI_PinSource, GPIO_AF_SPI2);
		GPIO_PinAFConfig(SPI2_GPIO, SPI2_MISO_PinSource, GPIO_AF_SPI2);

	    SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
	    SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
	    SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
	    SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
	    SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
	    SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
	    SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64;
	    SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
	    //SPI_InitStructure.SPI_CRCPolynomial = 7;
	    SPI_Init(SPI2, &SPI_InitStructure);

	    /* Enable SPI2 */
	    SPI_CalculateCRC(SPI2, DISABLE);
	    SPI_Cmd(SPI2, ENABLE);

	    SPI2_GPIO_CS->BSRRL|=SPI2_CS1;// pin up SPI2_CS1
}
示例#6
0
static void  LCD_Config (void)
{
    SPI_InitTypeDef   SPI_init;
    GPIO_InitTypeDef  gpio_init;
    
    gpio_init.GPIO_Pin   = LCD_PWR_Pin;
    gpio_init.GPIO_Speed = GPIO_Speed_50MHz;
    gpio_init.GPIO_Mode  = GPIO_Mode_Out_PP;
    GPIO_Init(LCD_PWR_Port, &gpio_init);    
    
    gpio_init.GPIO_Pin   = LCD_RST_Pin | LCD_DC_Pin | LCD_CS_Pin;
    gpio_init.GPIO_Speed = GPIO_Speed_50MHz;
    gpio_init.GPIO_Mode  = GPIO_Mode_Out_PP;
    GPIO_Init(GPIOB, &gpio_init);
    
    // for SPI interface
    gpio_init.GPIO_Pin   = LCD_SCK_Pin | LCD_MOSI_Pin;
    gpio_init.GPIO_Speed = GPIO_Speed_50MHz;
    gpio_init.GPIO_Mode  = GPIO_Mode_AF_PP;
    GPIO_Init(GPIOB, &gpio_init);
    
    // enable OLED PWREN vcc = 16v
    GPIO_ResetBits(LCD_PWR_Port, LCD_PWR_Pin);

		GPIO_ResetBits(LCD_CS_Port, LCD_CS_Pin);   
    GPIO_ResetBits(LCD_RST_Port, LCD_RST_Pin);
    Delay_Ms(10);
    GPIO_SetBits(LCD_RST_Port, LCD_RST_Pin); 
    GPIO_SetBits(LCD_CS_Port, LCD_CS_Pin);

    SPI_init.SPI_Direction = SPI_Direction_1Line_Tx;
    SPI_init.SPI_Mode = SPI_Mode_Master;
    SPI_init.SPI_DataSize = SPI_DataSize_8b;
    SPI_init.SPI_CPOL = SPI_CPOL_High;
    SPI_init.SPI_CPHA = SPI_CPHA_2Edge;
    SPI_init.SPI_NSS = SPI_NSS_Soft;
    SPI_init.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
    SPI_init.SPI_FirstBit = SPI_FirstBit_MSB;
    SPI_init.SPI_CRCPolynomial = 7;


    SPI_I2S_DeInit(SPI2); 
    SPI_Cmd(SPI2, DISABLE); 
    SPI_Init(SPI2, &SPI_init); 
    SPI_Cmd(SPI2, ENABLE);
    SPI_CalculateCRC(SPI2, DISABLE);
}
示例#7
0
void	spi1_config(void)//
{
		RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB |SPI1_GPIO_BUS, ENABLE);
	    RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1,ENABLE);

	    GPIO_InitTypeDef GPIO_InitStructure;
	    SPI_InitTypeDef SPI_InitStructure;

	    /* Configure SPI1 pins: SCK, MISO and MOSI -------------------------------*/
	    GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_3 |GPIO_Pin_4|GPIO_Pin_5;
	    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF;
	    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
	    GPIO_Init(GPIOB, &GPIO_InitStructure);


	    GPIO_InitStructure.GPIO_Pin   = SPI1_CS1;
	    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_OUT;
	    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
	    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	    GPIO_Init(SPI1_GPIO, &GPIO_InitStructure);

		GPIO_PinAFConfig(GPIOB, GPIO_PinSource3, GPIO_AF_SPI1);
		GPIO_PinAFConfig(GPIOB, GPIO_PinSource4, GPIO_AF_SPI1);
		GPIO_PinAFConfig(GPIOB, GPIO_PinSource5, GPIO_AF_SPI1);

	    SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
	    SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
	    SPI_InitStructure.SPI_DataSize = SPI_DataSize_16b;
	    SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
	    SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
	    SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
	    SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_32;
	    SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
	    //SPI_InitStructure.SPI_CRCPolynomial = 7;
	    SPI_Init(SPI1, &SPI_InitStructure);

	    /* Enable SPI1 */
	    SPI_CalculateCRC(SPI1, DISABLE);
	    SPI_Cmd(SPI1, ENABLE);
	    SPI1_GPIO_CS->BSRRH|=SPI1_CS1;//reset pin SPI1_CS1
}
示例#8
0
文件: hal.c 项目: carpman/OSHWatch
void SPI_Setup(void){
	SPI_InitTypeDef SPI_InitStructure;
		
	SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
	SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
    SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
    SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
    SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
    SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16;
    SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
    SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
    SPI_InitStructure.SPI_CRCPolynomial = 7;

    SPI_Cmd(SPI1, DISABLE);
    SPI_Init(SPI1, &SPI_InitStructure);
    SPI_CalculateCRC(SPI1, DISABLE);
    SPI_RxFIFOThresholdConfig(SPI1, SPI_RxFIFOThreshold_QF);
    SPI_Cmd(SPI1, ENABLE);
}
void SPI_Configuration(void)
{
	SPI_InitTypeDef  SPI_InitStructure;

	/* SPI configuration -------------------------------------------------------*/
	SPI_I2S_DeInit(SPI2);
	SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
	SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
	SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
	SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
	SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
	SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;
	SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
	SPI_InitStructure.SPI_CRCPolynomial = 7;
	SPI_InitStructure.SPI_Mode = SPI_Mode_Master;

	SPI_Init(SPI2, &SPI_InitStructure);
	SPI_CalculateCRC(SPI2, DISABLE);
	/* Enable the SPI peripheral */
	SPI_Cmd(SPI2, ENABLE);
}
示例#10
0
void Spi_Init(void)
{  
	GPIO_InitTypeDef GPIO_InitStructure;
	SPI_InitTypeDef SPI_InitStructure;

	RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA|RCC_APB2Periph_SPI1|RCC_APB2Periph_GPIOC, ENABLE );//PORTA,C,SPI1时钟使能 
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;//PC4 <---> CS_pin
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	//GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	//GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP;
	GPIO_Init(GPIOC, &GPIO_InitStructure);//PC4 as CS
	
	//GPIO_PinAFConfig(GPIOA, GPIO_PinSource5,GPIO_AF_SPI1);
	//GPIO_PinAFConfig(GPIOA, GPIO_PinSource7,GPIO_AF_SPI1);
	//GPIO_PinAFConfig(GPIOA, GPIO_PinSource6,GPIO_AF_SPI1);

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //PA5.6.7用AF功能
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	//GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	//GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_DOWN;
	GPIO_Init(GPIOA, &GPIO_InitStructure);


	SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;//设置SPI单向或者双向的数据模式:SPI设置为双线双向全双工
	SPI_InitStructure.SPI_Mode = SPI_Mode_Master; //设置SPI工作模式:设置为主SPI
	SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; //设置SPI的数据大小:SPI发送接收8位帧结构
	SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; //选择了串行时钟的稳态:时钟悬空高
	SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; //数据捕获于第二个时钟沿
	SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; //NSS信号由硬件(NSS管脚)还是软件(使用SSI位)管理:内部NSS信号有SSI位控制
	SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256; //定义波特率预分频的值:波特率预分频值为16
	SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; //指定数据传输从MSB位还是LSB位开始:数据传输从MSB位开始
	SPI_InitStructure.SPI_CRCPolynomial = 7; //CRC值计算的多项式
	SPI_Init(SPI1, &SPI_InitStructure);
	SPI_CalculateCRC(SPI1, DISABLE);
	SPI_Cmd(SPI1,ENABLE);
	printf("SPI 1 Inited\n\r");
}
示例#11
0
void SPI1_Configuration(void)
{ SPI_InitTypeDef SPI_InitStructure;
  GPIO_InitTypeDef GPIO_InitStructure;

  RCC_APB2PeriphClockCmd (RCC_APB2Periph_SPI1, ENABLE);

  GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_4;                 // SS: output
  GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_Out_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
  SPI1_Deselect();

  GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_5 | GPIO_Pin_7;    // CLK, MOSI: output
  GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

  GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_6;                // MISO: input
  GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_IPU;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

  SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
  SPI_InitStructure.SPI_Mode     = SPI_Mode_Master;
  SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
  SPI_InitStructure.SPI_CPOL     = SPI_CPOL_Low;
  SPI_InitStructure.SPI_CPHA     = SPI_CPHA_1Edge;
  SPI_InitStructure.SPI_NSS      = SPI_NSS_Soft;
  SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8; // 60MHz/8 = 7.5MHz SPI bitrate
  SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
  SPI_InitStructure.SPI_CRCPolynomial = 7;
  SPI_Init(SPI1, &SPI_InitStructure);
  // SPI_RxFIFOThresholdConfig(SPI1, SPI_RxFIFOThreshold_QF);
  SPI_CalculateCRC(SPI1, DISABLE);
  SPI_Cmd(SPI1, ENABLE);
}
示例#12
0
/**
 * Initialise a single Overo device
 */
int32_t PIOS_OVERO_Init(uint32_t *overo_id, const struct pios_overo_cfg *cfg)
{
    PIOS_DEBUG_Assert(overo_id);
    PIOS_DEBUG_Assert(cfg);

    struct pios_overo_dev *overo_dev;

    overo_dev = (struct pios_overo_dev *)PIOS_OVERO_alloc();
    if (!overo_dev) {
        goto out_fail;
    }

    /* Bind the configuration to the device instance */
    overo_dev->cfg = cfg;
    overo_dev->writing_buffer = 1; // First writes to second buffer

    /* Put buffers to a known state */
    memset(&overo_dev->tx_buffer[0][0], 0xFF, PACKET_SIZE);
    memset(&overo_dev->tx_buffer[1][0], 0xFF, PACKET_SIZE);
    memset(&overo_dev->rx_buffer[0][0], 0xFF, PACKET_SIZE);
    memset(&overo_dev->rx_buffer[1][0], 0xFF, PACKET_SIZE);

    /*
     * Enable the SPI device
     *
     * 1. Enable the SPI port
     * 2. Enable DMA with circular buffered DMA (validate config)
     * 3. Enable the DMA Tx IRQ
     */

    // PIOS_Assert(overo_dev->cfg->dma.tx-> == CIRCULAR);
    // PIOS_Assert(overo_dev->cfg->dma.rx-> == CIRCULAR);

    /* only legal for single-slave config */
    PIOS_Assert(overo_dev->cfg->slave_count == 1);
    SPI_SSOutputCmd(overo_dev->cfg->regs, DISABLE);

    /* Initialize the GPIO pins */
    /* note __builtin_ctz() due to the difference between GPIO_PinX and GPIO_PinSourceX */
    GPIO_PinAFConfig(overo_dev->cfg->sclk.gpio,
                     __builtin_ctz(overo_dev->cfg->sclk.init.GPIO_Pin),
                     overo_dev->cfg->remap);
    GPIO_PinAFConfig(overo_dev->cfg->mosi.gpio,
                     __builtin_ctz(overo_dev->cfg->mosi.init.GPIO_Pin),
                     overo_dev->cfg->remap);
    GPIO_PinAFConfig(overo_dev->cfg->miso.gpio,
                     __builtin_ctz(overo_dev->cfg->miso.init.GPIO_Pin),
                     overo_dev->cfg->remap);
    GPIO_PinAFConfig(overo_dev->cfg->ssel[0].gpio,
                     __builtin_ctz(overo_dev->cfg->ssel[0].init.GPIO_Pin),
                     overo_dev->cfg->remap);

    GPIO_Init(overo_dev->cfg->sclk.gpio, (GPIO_InitTypeDef *)&(overo_dev->cfg->sclk.init));
    GPIO_Init(overo_dev->cfg->mosi.gpio, (GPIO_InitTypeDef *)&(overo_dev->cfg->mosi.init));
    GPIO_Init(overo_dev->cfg->miso.gpio, (GPIO_InitTypeDef *)&(overo_dev->cfg->miso.init));

    /* Configure circular buffer targets. Configure 0 to be initially active */
    DMA_InitTypeDef dma_init;

    DMA_DeInit(overo_dev->cfg->dma.rx.channel);
    dma_init = overo_dev->cfg->dma.rx.init;
    dma_init.DMA_Memory0BaseAddr = (uint32_t)overo_dev->rx_buffer[0];
    dma_init.DMA_MemoryInc  = DMA_MemoryInc_Enable;
    dma_init.DMA_BufferSize = PACKET_SIZE;
    DMA_Init(overo_dev->cfg->dma.rx.channel, &dma_init);
    DMA_DoubleBufferModeConfig(overo_dev->cfg->dma.rx.channel, (uint32_t)overo_dev->rx_buffer[1], DMA_Memory_0);
    DMA_DoubleBufferModeCmd(overo_dev->cfg->dma.rx.channel, ENABLE);

    DMA_DeInit(overo_dev->cfg->dma.tx.channel);
    dma_init = overo_dev->cfg->dma.tx.init;
    dma_init.DMA_Memory0BaseAddr = (uint32_t)overo_dev->tx_buffer[0];
    dma_init.DMA_MemoryInc  = DMA_MemoryInc_Enable;
    dma_init.DMA_BufferSize = PACKET_SIZE;
    DMA_Init(overo_dev->cfg->dma.tx.channel, &dma_init);
    DMA_DoubleBufferModeConfig(overo_dev->cfg->dma.tx.channel, (uint32_t)overo_dev->tx_buffer[1], DMA_Memory_0);
    DMA_DoubleBufferModeCmd(overo_dev->cfg->dma.tx.channel, ENABLE);

    /* Set the packet size */
    DMA_SetCurrDataCounter(overo_dev->cfg->dma.rx.channel, PACKET_SIZE);
    DMA_SetCurrDataCounter(overo_dev->cfg->dma.tx.channel, PACKET_SIZE);

    /* Initialize the SPI block */
    SPI_DeInit(overo_dev->cfg->regs);
    SPI_Init(overo_dev->cfg->regs, (SPI_InitTypeDef *)&(overo_dev->cfg->init));

    SPI_CalculateCRC(overo_dev->cfg->regs, DISABLE);

    /* Enable SPI */
    SPI_Cmd(overo_dev->cfg->regs, ENABLE);

    /* Enable SPI interrupts to DMA */
    SPI_I2S_DMACmd(overo_dev->cfg->regs, SPI_I2S_DMAReq_Tx | SPI_I2S_DMAReq_Rx, ENABLE);

    /* Configure DMA interrupt */
    NVIC_Init((NVIC_InitTypeDef *)&(overo_dev->cfg->dma.irq.init));
    DMA_ITConfig(overo_dev->cfg->dma.tx.channel, DMA_IT_TC, ENABLE);

    /* Enable the DMA channels */
    DMA_Cmd(overo_dev->cfg->dma.tx.channel, ENABLE);
    DMA_Cmd(overo_dev->cfg->dma.rx.channel, ENABLE);

    *overo_id = (uint32_t)overo_dev;

    return 0;

out_fail:
    return -1;
}
示例#13
0
void InitSpi(SPI_TypeDef *_s, uint16_t _mode,
			 GPIO_TypeDef *_clkG, uint32_t _clkP,
			 GPIO_TypeDef *_mosiG, uint32_t _mosiP,
			 GPIO_TypeDef *_misoG, uint32_t _misoP,
			 uint16_t _CPOL,uint16_t _CPHA)
{
	assert_param(IS_SPI_CPOL(_CPOL));
	assert_param(IS_SPI_CPHA(_CPHA));
	if (_s == SPI1)
	{
		RCC_APB2PeriphClockCmd(GetRCS_RCC_APB2Periph_SPI(_s), ENABLE);
	}
	else
	{
		RCC_APB1PeriphClockCmd(GetRCS_RCC_APB1Periph_SPI(_s), ENABLE);
	}

	RCC_AHB1PeriphClockCmd(GetRCS_RCC_AHB1Periph_GPIO(_clkG), ENABLE);
	RCC_AHB1PeriphClockCmd(GetRCS_RCC_AHB1Periph_GPIO(_mosiG), ENABLE);
	RCC_AHB1PeriphClockCmd(GetRCS_RCC_AHB1Periph_GPIO(_misoG), ENABLE);

	GPIO_PinAFConfig(_clkG, GetRCS_GPIO_PinSource(_clkP), GetRCS_GPIO_AF_SPI(_s));
	GPIO_PinAFConfig(_mosiG, GetRCS_GPIO_PinSource(_mosiP), GetRCS_GPIO_AF_SPI(_s));
	GPIO_PinAFConfig(_misoG, GetRCS_GPIO_PinSource(_misoP), GetRCS_GPIO_AF_SPI(_s));

	GPIO_InitTypeDef GPIO_Struct;

	GPIO_StructInit(&GPIO_Struct);
	GPIO_Struct.GPIO_Pin = _clkP;
	GPIO_Struct.GPIO_Mode = GPIO_Mode_AF;
	GPIO_Struct.GPIO_Speed = GPIO_Speed_50MHz;
#ifdef  OD_OUTPUT
	GPIO_Struct.GPIO_OType = GPIO_OType_OD;
	GPIO_Struct.GPIO_PuPd = GPIO_PuPd_NOPULL;
#else
	GPIO_Struct.GPIO_OType = GPIO_OType_PP;
	GPIO_Struct.GPIO_PuPd = GPIO_PuPd_UP;
#endif
	GPIO_Init(_clkG, &GPIO_Struct);

	GPIO_StructInit(&GPIO_Struct);
	GPIO_Struct.GPIO_Pin = _mosiP;
	GPIO_Struct.GPIO_Mode = GPIO_Mode_AF;
	GPIO_Struct.GPIO_Speed = GPIO_Speed_50MHz;
#ifdef  OD_OUTPUT
	GPIO_Struct.GPIO_OType = GPIO_OType_OD;
	GPIO_Struct.GPIO_PuPd = GPIO_PuPd_NOPULL;
#else
	GPIO_Struct.GPIO_OType = GPIO_OType_PP;
	GPIO_Struct.GPIO_PuPd = GPIO_PuPd_UP;
#endif
	GPIO_Init(_mosiG, &GPIO_Struct);

	GPIO_StructInit(&GPIO_Struct);
	GPIO_Struct.GPIO_Pin = _misoP;
	GPIO_Struct.GPIO_Mode = GPIO_Mode_AF;
	GPIO_Struct.GPIO_Speed = GPIO_Speed_50MHz;
#ifdef  OD_OUTPUT
	GPIO_Struct.GPIO_OType = GPIO_OType_OD;
	GPIO_Struct.GPIO_PuPd = GPIO_PuPd_NOPULL;
#else
	GPIO_Struct.GPIO_OType = GPIO_OType_PP;
	GPIO_Struct.GPIO_PuPd = GPIO_PuPd_UP;
#endif
	GPIO_Init(_misoG, &GPIO_Struct);

	SPI_InitTypeDef SPI_InitStructure;
	SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;

	SPI_InitStructure.SPI_Mode = _mode;

	SPI_InitStructure.SPI_DataSize = SPI_DataSize_16b;

	SPI_InitStructure.SPI_CPOL = _CPOL;
	SPI_InitStructure.SPI_CPHA = _CPHA;
	if (_mode == SPI_Mode_Master)
	{
		SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
	}
	else
	{
		SPI_InitStructure.SPI_NSS = SPI_NSS_Hard;
	}
	SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_32;
	SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
	SPI_InitStructure.SPI_CRCPolynomial = 7;
	SPI_Init(_s, &SPI_InitStructure);
	SPI_CalculateCRC(_s,DISABLE);
	SPI_Cmd(_s, ENABLE);
	SPI_CalculateCRC(_s,DISABLE);
}
示例#14
0
void spiInit2(void)
{
	GPIO_InitTypeDef GPIO_InitStruct;
	GPIO_InitTypeDef GPIO_InitStructure1;
	GPIO_InitTypeDef GPIO_InitStructure2;
	GPIO_InitTypeDef GPIO_InitStructure3;

	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);

	GPIO_PinAFConfig(GPIOE, GPIO_PinSource10, GPIO_AF_SPI2);

	GPIO_StructInit(&GPIO_InitStruct);
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10;
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
	GPIO_Init(GPIOE, &GPIO_InitStruct);

	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10;
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
	GPIO_Init(GPIOD, &GPIO_InitStruct);

	GPIOE->BSRRL = GPIO_Pin_10; // set PE10 high

	SPI_I2S_DeInit(SPI_BUSE2);

	// enable clock for used IO pins
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
	GPIO_PinAFConfig(SPI_GPIO_2, SPI_SCK_PIN_SOURCE_2, GPIO_AF_SPI2);
	GPIO_PinAFConfig(SPI_GPIO_2, SPI_MISO_PIN_SOURCE_2, GPIO_AF_SPI2);
	GPIO_PinAFConfig(SPI_GPIO_2, SPI_MOSI_PIN_SOURCE_2, GPIO_AF_SPI2);

	// Init pins
	GPIO_InitStructure1.GPIO_Pin = SPI_SCK_PIN_2;
	GPIO_InitStructure1.GPIO_Mode = GPIO_Mode_AF;
	GPIO_InitStructure1.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure1.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStructure1.GPIO_PuPd = GPIO_PuPd_UP;
	GPIO_Init(SPI_GPIO_2, &GPIO_InitStructure1);

	GPIO_InitStructure2.GPIO_Pin = SPI_MISO_PIN_2;
	GPIO_InitStructure2.GPIO_Mode = GPIO_Mode_AF;
	GPIO_InitStructure2.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure2.GPIO_OType = GPIO_OType_OD;
	GPIO_InitStructure2.GPIO_PuPd = GPIO_PuPd_NOPULL;
	GPIO_Init(SPI_GPIO_2, &GPIO_InitStructure2);

	GPIO_InitStructure3.GPIO_Pin = SPI_MOSI_PIN_2;
	GPIO_InitStructure3.GPIO_Mode = GPIO_Mode_AF;
	GPIO_InitStructure3.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure3.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStructure3.GPIO_PuPd = GPIO_PuPd_UP;
	GPIO_Init(SPI_GPIO_2, &GPIO_InitStructure3);

	SPI_I2S_DeInit(SPI_BUSE2);

	// Enable SPI2 clock
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);

	SPI_InitTypeDef SPI_InitStructure;
    SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
    SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
    SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
    SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
    SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
    SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
    SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64;
    SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
    SPI_InitStructure.SPI_CRCPolynomial = 7;
    SPI_Init(SPI_BUSE2, &SPI_InitStructure);

	SPI_CalculateCRC(SPI_BUSE2, DISABLE);

	SPI_Cmd(SPI_BUSE2, ENABLE);
}
示例#15
0
void rt_hw_board_init(void)
{
    //NAND_IDTypeDef NAND_ID;

    /* Configure the system clocks */
    SystemInit();

    all_device_reset();

    /* NVIC Configuration */
    NVIC_Configuration();

    /* Configure the SysTick */
    SysTick_Config( SystemCoreClock / RT_TICK_PER_SECOND );

    /* Console Initialization*/
    rt_hw_usart_init();
#if STM32_CONSOLE_USART == 1
    rt_console_set_device("uart1");
#elif STM32_CONSOLE_USART == 2
    rt_console_set_device("uart2");
#elif STM32_CONSOLE_USART == 3
    rt_console_set_device("uart3");
#endif

    rt_kprintf("\r\n\r\nSystemInit......\r\n");

    /* SRAM init */
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, ENABLE);
    FSMC_SRAM_Init();

    /* memtest */
    {
        unsigned char * p_extram = (unsigned char *)STM32_EXT_SRAM_BEGIN;
        unsigned int temp;

        rt_kprintf("\r\nmem testing....");
        for(temp=0; temp<(STM32_EXT_SRAM_END-STM32_EXT_SRAM_BEGIN); temp++)
        {
            *p_extram++ = (unsigned char)temp;
        }

        p_extram = (unsigned char *)STM32_EXT_SRAM_BEGIN;
        for(temp=0; temp<(STM32_EXT_SRAM_END-STM32_EXT_SRAM_BEGIN); temp++)
        {
            if( *p_extram++ != (unsigned char)temp )
            {
                rt_kprintf("\rmemtest fail @ %08X\r\nsystem halt!!!!!",(unsigned int)p_extram);
                while(1);
            }
        }
        rt_kprintf("\rmem test pass!!\r\n");
    }/* memtest */

    /* SPI1 config */
    {
        GPIO_InitTypeDef GPIO_InitStructure;
        SPI_InitTypeDef SPI_InitStructure;

        /* Enable SPI1 Periph clock */
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA
                               | RCC_APB2Periph_AFIO | RCC_APB2Periph_SPI1,
                               ENABLE);

        /* Configure SPI1 pins: PA5-SCK, PA6-MISO and PA7-MOSI */
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
        GPIO_Init(GPIOA, &GPIO_InitStructure);

        /*------------------------ SPI1 configuration ------------------------*/
        SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;//SPI_Direction_1Line_Tx;
        SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
        SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
        SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
        SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
        SPI_InitStructure.SPI_NSS  = SPI_NSS_Soft;
        SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64;/* 72M/64=1.125M */
        SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
        SPI_InitStructure.SPI_CRCPolynomial = 7;

        SPI_I2S_DeInit(SPI1);
        SPI_Init(SPI1, &SPI_InitStructure);

        /* Enable SPI_MASTER */
        SPI_Cmd(SPI1, ENABLE);
        SPI_CalculateCRC(SPI1, DISABLE);

        if (rt_sem_init(&spi1_lock, "spi1lock", 1, RT_IPC_FLAG_FIFO) != RT_EOK)
        {
            rt_kprintf("init spi1 lock semaphore failed\n");
        }
    }

}/* rt_hw_board_init */
示例#16
0
 void SST25V_Init(void)
 {

        GPIO_InitTypeDef GPIO_InitStructure;
        SPI_InitTypeDef SPI_InitStructure;

        /* Enable DF_SPI Periph clock */
        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE);
	 RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2,ENABLE);
	 GPIO_PinAFConfig(GPIOB, GPIO_PinSource13, GPIO_AF_SPI2  );
        GPIO_PinAFConfig(GPIOB, GPIO_PinSource14, GPIO_AF_SPI2  );
        GPIO_PinAFConfig(GPIOB, GPIO_PinSource15, GPIO_AF_SPI2  );
	 RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
	

        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	 GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF;
	 GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	 GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_DOWN;


         /*!< SPI SCK pin configuration */
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
	GPIO_Init(GPIOB, &GPIO_InitStructure);

	/*!< SPI MOSI pin configuration */
	GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_14;
	GPIO_Init(GPIOB, &GPIO_InitStructure);

	/*!< SPI MISO pin configuration */
	GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_15;
	GPIO_Init(GPIOB, &GPIO_InitStructure);			



    //-------  CS _pin  	
    GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_14;   
	GPIO_InitStructure.GPIO_Mode  =  GPIO_Mode_OUT;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_Init(GPIOD, &GPIO_InitStructure);


      //-------  WP  _pin  	
    GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_15;   
    GPIO_InitStructure.GPIO_Mode  =  GPIO_Mode_OUT;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_Init(GPIOD, &GPIO_InitStructure);	

        /*------------------------ DF_SPI configuration ------------------------*/
        SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;//SPI_Direction_1Line_Tx;
        SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
        SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
        SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;  
        SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
        SPI_InitStructure.SPI_NSS  = SPI_NSS_Soft;
        SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8;/* 42M/64=1.125M */      
        SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
        SPI_InitStructure.SPI_CRCPolynomial = 7;  

	  //SPI1->CR2=0x04; 									//NSS ---SSOE
    //   SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
       //SPI_InitStructure.SPI_Mode = SPI_Mode_Master; 	  //MSTR	

        SPI_I2S_DeInit(DF_SPI);
        SPI_Init(DF_SPI, &SPI_InitStructure);

        /* Enable SPI_MASTER */
        SPI_Cmd(DF_SPI, ENABLE);
        SPI_CalculateCRC(DF_SPI, DISABLE); 

        SST25V_CS_HIGH();
	    SST25V_WP_HIGH();
	  //SST25V_HOLD_HIGH();
    	//  SST25V_EnableWriteStatusRegister();
	  // SST25V_WriteStatusRegister(0x02); 
	   SST25V_DBSY(); 
}
示例#17
0
OSStatus MicoSpiInitialize( const mico_spi_device_t* spi )
{
  GPIO_InitTypeDef gpio_init_structure;
  OSStatus   result;
  SPI_InitTypeDef  spi_init;
  
  check_string( spi != NULL, "Bad args");
  
  MicoMcuPowerSaveConfig(false);
  
  /* Init SPI GPIOs */
  gpio_init_structure.GPIO_Mode  = GPIO_Mode_AF;
  gpio_init_structure.GPIO_OType = GPIO_OType_PP;
  gpio_init_structure.GPIO_Speed = GPIO_Speed_100MHz;
  gpio_init_structure.GPIO_Pin   = ((uint32_t) (1 << spi_mapping[spi->port].pin_clock->number)) |
    ((uint32_t) (1 << spi_mapping[spi->port].pin_miso->number )) |
      ((uint32_t) (1 << spi_mapping[spi->port].pin_mosi->number ));
  GPIO_Init( spi_mapping[spi->port].pin_clock->bank, &gpio_init_structure );
  
  /* Init the chip select GPIO */
  MicoGpioInitialize(spi->chip_select, OUTPUT_PUSH_PULL);
  MicoGpioOutputHigh(spi->chip_select);
  
  GPIO_PinAFConfig( spi_mapping[spi->port].pin_clock->bank, spi_mapping[spi->port].pin_clock->number,  spi_mapping[spi->port].gpio_af );
  GPIO_PinAFConfig( spi_mapping[spi->port].pin_miso->bank,  spi_mapping[spi->port].pin_miso->number,   spi_mapping[spi->port].gpio_af );
  GPIO_PinAFConfig( spi_mapping[spi->port].pin_mosi->bank,  spi_mapping[spi->port].pin_mosi->number,   spi_mapping[spi->port].gpio_af );
  
  /* Configure baudrate */
  result = wiced_spi_configure_baudrate( spi->speed, &spi_init.SPI_BaudRatePrescaler );
  if ( result != kNoErr )
  {
    return result;
  }
  
  /* Configure data-width */
  if ( spi->bits == 8 )
  {
    spi_init.SPI_DataSize = SPI_DataSize_8b;
  }
  else if ( spi->bits == 16 )
  {
    if ( spi->mode & SPI_USE_DMA )
    {
      /* 16 bit mode is not supported for a DMA */
      return kGeneralErr;
    }
    spi_init.SPI_DataSize = SPI_DataSize_16b;
  }
  else
  {
    /* Requested mode is not supported */
    return kOptionErr;
  }
  
  /* Configure MSB or LSB */
  if ( spi->mode & SPI_MSB_FIRST )
  {
    spi_init.SPI_FirstBit = SPI_FirstBit_MSB;
  }
  else
  {
    spi_init.SPI_FirstBit = SPI_FirstBit_LSB;
  }
  
  /* Configure mode CPHA and CPOL */
  if ( spi->mode & SPI_CLOCK_IDLE_HIGH )
  {
    spi_init.SPI_CPOL = SPI_CPOL_High;
  }
  else
  {
    spi_init.SPI_CPOL = SPI_CPOL_Low;
  }
  
  if ( spi->mode & SPI_CLOCK_RISING_EDGE )
  {
    spi_init.SPI_CPHA = ( spi->mode & SPI_CLOCK_IDLE_HIGH )? SPI_CPHA_2Edge : SPI_CPHA_1Edge;
  }
  else
  {
    spi_init.SPI_CPHA = ( spi->mode & SPI_CLOCK_IDLE_HIGH )? SPI_CPHA_1Edge : SPI_CPHA_2Edge;
  }
  
  spi_init.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
  spi_init.SPI_Mode      = SPI_Mode_Master;
  spi_init.SPI_NSS       = SPI_NSS_Soft;
  SPI_CalculateCRC( spi_mapping[spi->port].spi_regs, DISABLE );
  
  /* Enable SPI peripheral clock */
  spi_mapping[spi->port].peripheral_clock_func( spi_mapping[spi->port].peripheral_clock_reg,  ENABLE );
  
  /* Init and enable SPI */
  SPI_Init( spi_mapping[spi->port].spi_regs, &spi_init );
  SPI_Cmd ( spi_mapping[spi->port].spi_regs, ENABLE );
  
  MicoMcuPowerSaveConfig(true);
  
  current_spi_device = (mico_spi_device_t*)spi;
  
  return kNoErr;
}
示例#18
0
/*******************************************************************************
* Function Name  : main
* Description    : Main program
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
int main(void)
{
#ifdef DEBUG
  debug();
#endif

  /* System Clocks Configuration */
  RCC_Configuration();
       
  /* NVIC configuration */
  NVIC_Configuration();

  /* Configure the GPIO ports */
  GPIO_Configuration();

  /* SPI1 configuration ------------------------------------------------------*/
  SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
  SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
  SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
  SPI_InitStructure.SPI_CPOL = 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 = CRCPolynomial;
  SPI_Init(SPI1, &SPI_InitStructure);
  
  /* SPI2 configuration ------------------------------------------------------*/
  SPI_InitStructure.SPI_Mode = SPI_Mode_Slave;
  SPI_Init(SPI2, &SPI_InitStructure);

  /* DMA1 Channel2 configuration ----------------------------------------------*/
  DMA_DeInit(DMA1_Channel2);
  DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)SPI1_DR_Address;
  DMA_InitStructure.DMA_MemoryBaseAddr = (u32)SPI1_Buffer_Rx;
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
  DMA_InitStructure.DMA_BufferSize = BufferSize;
  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
  DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
  DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
  DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
  DMA_InitStructure.DMA_Priority = DMA_Priority_High;
  DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
  DMA_Init(DMA1_Channel2, &DMA_InitStructure);

  /* DMA1 Channel3 configuration ----------------------------------------------*/
  DMA_DeInit(DMA1_Channel3);  
  DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)SPI1_DR_Address;
  DMA_InitStructure.DMA_MemoryBaseAddr = (u32)SPI1_Buffer_Tx;
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
  DMA_InitStructure.DMA_Priority = DMA_Priority_Low;
  DMA_Init(DMA1_Channel3, &DMA_InitStructure);

  /* DMA1 Channel4 configuration ----------------------------------------------*/
  DMA_DeInit(DMA1_Channel4);  
  DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)SPI2_DR_Address;
  DMA_InitStructure.DMA_MemoryBaseAddr = (u32)SPI2_Buffer_Rx;
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
  DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
  DMA_Init(DMA1_Channel4, &DMA_InitStructure);

  /* DMA1 Channel5 configuration ----------------------------------------------*/
  DMA_DeInit(DMA1_Channel5);  
  DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)SPI2_DR_Address;
  DMA_InitStructure.DMA_MemoryBaseAddr = (u32)SPI2_Buffer_Tx;
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
  DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;
  DMA_Init(DMA1_Channel5, &DMA_InitStructure);

  /* Enable SPI1 DMA Tx request */
  SPI_I2S_DMACmd(SPI1, SPI_I2S_DMAReq_Tx, ENABLE);
  /* Enable SPI1 DMA Rx request */
  SPI_I2S_DMACmd(SPI1, SPI_I2S_DMAReq_Rx, ENABLE);
  /* Enable SPI2 DMA Tx request */
  SPI_I2S_DMACmd(SPI2, SPI_I2S_DMAReq_Tx, ENABLE);   
  /* Enable SPI2 DMA Rx request */
  SPI_I2S_DMACmd(SPI2, SPI_I2S_DMAReq_Rx, ENABLE); 
 
  /* Enable SPI1 CRC calculation */
  SPI_CalculateCRC(SPI1, ENABLE);
  /* Enable SPI2 CRC calculation */
  SPI_CalculateCRC(SPI2, ENABLE);

  /* Enable SPI2 */
  SPI_Cmd(SPI2, ENABLE);
  /* Enable SPI1 */
  SPI_Cmd(SPI1, ENABLE);

  /* Enable DMA1 channel5, channel4, channel3 and channel2 */
  DMA_Cmd(DMA1_Channel2, ENABLE);
  DMA_Cmd(DMA1_Channel4, ENABLE);
  DMA_Cmd(DMA1_Channel5, ENABLE);
  DMA_Cmd(DMA1_Channel3, ENABLE);
   
  /* Transfer complete */
  while(!DMA_GetFlagStatus(DMA1_FLAG_TC2));
  while(!DMA_GetFlagStatus(DMA1_FLAG_TC4));
  while(!DMA_GetFlagStatus(DMA1_FLAG_TC5));
  while(!DMA_GetFlagStatus(DMA1_FLAG_TC3));

  /* Wait for SPI1 data reception: CRC transmitted by SPI2 */
  while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE)==RESET);
  /* Wait for SPI2 data reception: CRC transmitted by SPI1 */
  while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE)==RESET);

  /* Check the corectness of written dada */
  TransferStatus1 = Buffercmp(SPI2_Buffer_Rx, SPI1_Buffer_Tx, BufferSize);
  TransferStatus2 = Buffercmp(SPI1_Buffer_Rx, SPI2_Buffer_Tx, BufferSize);
  /* TransferStatus1, TransferStatus2 = PASSED, if the data transmitted and received 
     are correct */
  /* TransferStatus1, TransferStatus2 = FAILED, if the data transmitted and received  
     are different */

  /* Test on the SPI1 CRCR ERROR flag */
  if ((SPI_I2S_GetFlagStatus(SPI1, SPI_FLAG_CRCERR)) != RESET)
  {
    TransferStatus1 = FAILED;
  }
  /* Test on the SPI2 CRCR ERROR flag */
  if ((SPI_I2S_GetFlagStatus(SPI2, SPI_FLAG_CRCERR)) != RESET)
  {
    TransferStatus2 = FAILED;
  } 

  /* Read SPI1 received CRC value */
  SPI1CRCValue = SPI_I2S_ReceiveData(SPI1);
  /* Read SPI2 received CRC value */
  SPI2CRCValue = SPI_I2S_ReceiveData(SPI2);

  while (1)
  {
  }
}
示例#19
0
文件: sst25.c 项目: mildrock/705_1
/***********************************************************
* Function:
* Description:
* Input:
* Input:
* Output:
* Return:
* Others:
***********************************************************/
void sst25_init( void )
{
	GPIO_InitTypeDef	GPIO_InitStructure;
	SPI_InitTypeDef		SPI_InitStructure;
	uint32_t			delay_counter;

	/* Enable DF_SPI Periph clock */
	RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOB, ENABLE );
	RCC_APB1PeriphClockCmd( RCC_APB1Periph_SPI2, ENABLE );
	GPIO_PinAFConfig( GPIOB, GPIO_PinSource13, GPIO_AF_SPI2 );
	GPIO_PinAFConfig( GPIOB, GPIO_PinSource14, GPIO_AF_SPI2 );
	GPIO_PinAFConfig( GPIOB, GPIO_PinSource15, GPIO_AF_SPI2 );
	RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOD, ENABLE );

	GPIO_InitStructure.GPIO_Speed	= GPIO_Speed_25MHz;
	GPIO_InitStructure.GPIO_Mode	= GPIO_Mode_AF;
	GPIO_InitStructure.GPIO_OType	= GPIO_OType_PP;
	GPIO_InitStructure.GPIO_PuPd	= GPIO_PuPd_DOWN;

	/*!< SPI SCK pin configuration */
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
	GPIO_Init( GPIOB, &GPIO_InitStructure );

	/*!< SPI MOSI pin configuration */
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;
	GPIO_Init( GPIOB, &GPIO_InitStructure );

	/*!< SPI MISO pin configuration */
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
	GPIO_Init( GPIOB, &GPIO_InitStructure );

	//-------  CS _pin
	GPIO_InitStructure.GPIO_Pin		= GPIO_Pin_14;
	GPIO_InitStructure.GPIO_Mode	= GPIO_Mode_OUT;
	GPIO_InitStructure.GPIO_OType	= GPIO_OType_PP;
	GPIO_InitStructure.GPIO_Speed	= GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_PuPd	= GPIO_PuPd_NOPULL;
	GPIO_Init( GPIOD, &GPIO_InitStructure );
	
	//PA4 pin: CS		SD_CARD
	GPIO_InitStructure.GPIO_Pin		= GPIO_Pin_4;
	GPIO_InitStructure.GPIO_Mode	= GPIO_Mode_OUT;
	GPIO_InitStructure.GPIO_OType	= GPIO_OType_PP;
	GPIO_InitStructure.GPIO_Speed	= GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_PuPd	= GPIO_PuPd_NOPULL;
	GPIO_Init( GPIOA, &GPIO_InitStructure );
	GPIO_SetBits(GPIOA,GPIO_Pin_4);

	//-------  WP  _pin
	GPIO_InitStructure.GPIO_Pin		= GPIO_Pin_15;
	GPIO_InitStructure.GPIO_Mode	= GPIO_Mode_OUT;
	GPIO_InitStructure.GPIO_OType	= GPIO_OType_PP;
	GPIO_InitStructure.GPIO_Speed	= GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_PuPd	= GPIO_PuPd_NOPULL;
	GPIO_Init( GPIOD, &GPIO_InitStructure );

	/*------------------------ DF_SPI configuration ------------------------*/
	SPI_InitStructure.SPI_Direction			= SPI_Direction_2Lines_FullDuplex;  //SPI_Direction_1Line_Tx;
	SPI_InitStructure.SPI_Mode				= SPI_Mode_Master;
	SPI_InitStructure.SPI_DataSize			= SPI_DataSize_8b;
	SPI_InitStructure.SPI_CPOL				= SPI_CPOL_Low;
	SPI_InitStructure.SPI_CPHA				= SPI_CPHA_1Edge;
	SPI_InitStructure.SPI_NSS				= SPI_NSS_Soft;
	SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;          /* 42M/4=10.5M */
	SPI_InitStructure.SPI_FirstBit			= SPI_FirstBit_MSB;
	SPI_InitStructure.SPI_CRCPolynomial		= 7;

	//SPI1->CR2=0x04;                                   //NSS ---SSOE
	//   SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
	//SPI_InitStructure.SPI_Mode = SPI_Mode_Master;       //MSTR

	SPI_I2S_DeInit( SPI2 );
	SPI_Init( SPI2, &SPI_InitStructure );

	/* Enable SPI_MASTER */
	SPI_Cmd( SPI2, ENABLE );
	SPI_CalculateCRC( SPI2, DISABLE );

	DF_CS_1;
	DF_WP_0;

	DF_CS_0;
	spi_tx( 0x50 );
	DF_CS_1;
	delay(3);
	DF_CS_0;
	spi_tx( 0x01 );
	spi_tx( 0x00 );
	DF_CS_1;

	//delay(3);
	//DF_CS_0;
	//spi_tx( DBSY );
	//DF_CS_1;
	sst25_info( );
	//rt_sem_init( &sem_dataflash, "sem_df", 0, RT_IPC_FLAG_FIFO );
	//rt_sem_release( &sem_dataflash );
}
示例#20
0
/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{
  /*!< At this stage the microcontroller clock setting is already configured, 
       this is done through SystemInit() function which is called from startup
       file (startup_stm32f10x_xx.s) before to branch to application main.
       To reconfigure the default setting of SystemInit() function, refer to
       system_stm32f10x.c file
     */     
       
  /* System clocks configuration ---------------------------------------------*/
  RCC_Configuration();

  /* GPIO configuration ------------------------------------------------------*/
  GPIO_Configuration();

  /* SPI1 configuration ------------------------------------------------------*/
  SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
  SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
  SPI_InitStructure.SPI_DataSize = SPI_DataSize_16b;
  SPI_InitStructure.SPI_CPOL = 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(SPI1, &SPI_InitStructure);

  /* SPI2 configuration ------------------------------------------------------*/
  SPI_InitStructure.SPI_Mode = SPI_Mode_Slave;
  SPI_Init(SPI2, &SPI_InitStructure);

  /* Enable SPI1 CRC calculation */
  SPI_CalculateCRC(SPI1, ENABLE);
  /* Enable SPI2 CRC calculation */
  SPI_CalculateCRC(SPI2, ENABLE);

  /* Enable SPI1 */
  SPI_Cmd(SPI1, ENABLE);
  /* Enable SPI2 */
  SPI_Cmd(SPI2, ENABLE);

  /* Transfer procedure */
  while (TxIdx < BufferSize - 1)
  {
    /* Wait for SPI1 Tx buffer empty */
    while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
    /* Send SPI2 data */
    SPI_I2S_SendData(SPI2, SPI2_Buffer_Tx[TxIdx]);
    /* Send SPI1 data */
    SPI_I2S_SendData(SPI1, SPI1_Buffer_Tx[TxIdx++]);
    /* Wait for SPI2 data reception */
    while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET);
    /* Read SPI2 received data */
    SPI2_Buffer_Rx[RxIdx] = SPI_I2S_ReceiveData(SPI2);
    /* Wait for SPI1 data reception */
    while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
    /* Read SPI1 received data */
    SPI1_Buffer_Rx[RxIdx++] = SPI_I2S_ReceiveData(SPI1);
  }

  /* Wait for SPI1 Tx buffer empty */
  while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
  /* Wait for SPI2 Tx buffer empty */
  while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET);

  /* Send last SPI2_Buffer_Tx data */
  SPI_I2S_SendData(SPI2, SPI2_Buffer_Tx[TxIdx]);
  /* Enable SPI2 CRC transmission */
  SPI_TransmitCRC(SPI2);
  /* Send last SPI1_Buffer_Tx data */
  SPI_I2S_SendData(SPI1, SPI1_Buffer_Tx[TxIdx]);
  /* Enable SPI1 CRC transmission */
  SPI_TransmitCRC(SPI1);

  /* Wait for SPI1 last data reception */
  while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
  /* Read SPI1 last received data */
  SPI1_Buffer_Rx[RxIdx] = SPI_I2S_ReceiveData(SPI1);

  /* Wait for SPI2 last data reception */
  while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET);
  /* Read SPI2 last received data */
  SPI2_Buffer_Rx[RxIdx] = SPI_I2S_ReceiveData(SPI2);

  /* Wait for SPI1 data reception: CRC transmitted by SPI2 */
  while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
  /* Wait for SPI2 data reception: CRC transmitted by SPI1 */
  while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET);

  /* Check the received data with the send ones */
  TransferStatus1 = Buffercmp(SPI2_Buffer_Rx, SPI1_Buffer_Tx, BufferSize);
  TransferStatus2 = Buffercmp(SPI1_Buffer_Rx, SPI2_Buffer_Tx, BufferSize);
  /* TransferStatus1, TransferStatus2 = PASSED, if the data transmitted and received
     are correct */
  /* TransferStatus1, TransferStatus2 = FAILED, if the data transmitted and received
     are different */

  /* Test on the SPI1 CRC Error flag */
  if ((SPI_I2S_GetFlagStatus(SPI1, SPI_FLAG_CRCERR)) == SET)
  {
    TransferStatus2 = FAILED;
  }

  /* Test on the SPI2 CRC Error flag */
  if ((SPI_I2S_GetFlagStatus(SPI2, SPI_FLAG_CRCERR)) == SET)
  {
    TransferStatus1 = FAILED;
  }

  /* Read SPI1 received CRC value */
  CRC1Value = SPI_I2S_ReceiveData(SPI1);
  /* Read SPI2 received CRC value */
  CRC2Value = SPI_I2S_ReceiveData(SPI2);

  while (1)
  {}
}
示例#21
0
platform_result_t platform_spi_init( const platform_spi_t* spi, const platform_spi_config_t* config )
{
    SPI_InitTypeDef   spi_init;
    platform_result_t result;
    uint8_t           spi_number;

    wiced_assert( "bad argument", ( spi != NULL ) && ( config != NULL ) );

    platform_mcu_powersave_disable();

    spi_number = platform_spi_get_port_number( spi->port );

    /* Init SPI GPIOs */
    platform_gpio_set_alternate_function( spi->pin_clock->port, spi->pin_clock->pin_number, GPIO_OType_PP, GPIO_PuPd_NOPULL, spi->gpio_af );
    platform_gpio_set_alternate_function( spi->pin_mosi->port,  spi->pin_mosi->pin_number,  GPIO_OType_PP, GPIO_PuPd_NOPULL, spi->gpio_af );
    platform_gpio_set_alternate_function( spi->pin_miso->port,  spi->pin_miso->pin_number,  GPIO_OType_PP, GPIO_PuPd_NOPULL, spi->gpio_af );

    /* Init the chip select GPIO */
    platform_gpio_init( config->chip_select, OUTPUT_PUSH_PULL );
    platform_gpio_output_high( config->chip_select );

    /* Calculate prescaler */
    result = calculate_prescaler( config->speed, &spi_init.SPI_BaudRatePrescaler );
    if ( result != PLATFORM_SUCCESS )
    {
        platform_mcu_powersave_enable();
        return result;
    }

    /* Configure data-width */
    if ( config->bits == 8 )
    {
        spi_init.SPI_DataSize = SPI_DataSize_8b;
    }
    else if ( config->bits == 16 )
    {
        if ( config->mode & SPI_USE_DMA )
        {
            platform_mcu_powersave_enable();

            /* 16 bit mode is not supported for a DMA */
            return PLATFORM_UNSUPPORTED;
        }

        spi_init.SPI_DataSize = SPI_DataSize_16b;
    }
    else
    {
        platform_mcu_powersave_enable();

        /* Requested mode is not supported */
        return PLATFORM_UNSUPPORTED;
    }

    /* Configure MSB or LSB */
    if ( config->mode & SPI_MSB_FIRST )
    {
        spi_init.SPI_FirstBit = SPI_FirstBit_MSB;
    }
    else
    {
        spi_init.SPI_FirstBit = SPI_FirstBit_LSB;
    }

    /* Configure mode CPHA and CPOL */
    if ( config->mode & SPI_CLOCK_IDLE_HIGH )
    {
        spi_init.SPI_CPOL = SPI_CPOL_High;
    }
    else
    {
        spi_init.SPI_CPOL = SPI_CPOL_Low;
    }

    if ( config->mode & SPI_CLOCK_RISING_EDGE )
    {
        spi_init.SPI_CPHA = ( config->mode & SPI_CLOCK_IDLE_HIGH ) ? SPI_CPHA_2Edge : SPI_CPHA_1Edge;
    }
    else
    {
        spi_init.SPI_CPHA = ( config->mode & SPI_CLOCK_IDLE_HIGH ) ? SPI_CPHA_1Edge : SPI_CPHA_2Edge;
    }

    /* Enable SPI peripheral clock */
    spi_peripheral_clock_functions[ spi_number ]( spi_peripheral_clocks[ spi_number ], ENABLE );

    spi_init.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
    spi_init.SPI_Mode      = SPI_Mode_Master;
    spi_init.SPI_NSS       = SPI_NSS_Soft;
    spi_init.SPI_CRCPolynomial = 0x7; /* reset value */
    SPI_CalculateCRC( spi->port, DISABLE );

    /* Init and enable SPI */
    SPI_Init( spi->port, &spi_init );
    SPI_Cmd ( spi->port, ENABLE );

    platform_mcu_powersave_enable();

    return WICED_SUCCESS;
}
示例#22
0
OSStatus platform_spi_init( platform_spi_driver_t* driver, const platform_spi_t* peripheral, const platform_spi_config_t* config )
{
  SPI_InitTypeDef   spi_init;
  OSStatus          err;
  
  platform_mcu_powersave_disable();
  
  require_action_quiet( ( driver != NULL ) && ( peripheral != NULL ) && ( config != NULL ), exit, err = kParamErr);

/* Calculate prescaler */
  err = calculate_prescaler( config->speed, &spi_init.SPI_BaudRatePrescaler );
  require_noerr(err, exit);
      
  /* Configure data-width */
  if ( config->bits == 8 )
  {
    spi_init.SPI_DataSize = SPI_DataSize_8b;
  }
  else if ( config->bits == 16 )
  {
    require_action( !(config->mode & SPI_USE_DMA), exit, err = kUnsupportedErr);
    spi_init.SPI_DataSize = SPI_DataSize_16b;
  }
  else
  {
    err = kUnsupportedErr;
    goto exit;
  }
  
  /* Configure MSB or LSB */
  if ( config->mode & SPI_MSB_FIRST )
  {
    spi_init.SPI_FirstBit = SPI_FirstBit_MSB;
  }
  else
  {
    spi_init.SPI_FirstBit = SPI_FirstBit_LSB;
  }
  
  /* Configure mode CPHA and CPOL */
  if ( config->mode & SPI_CLOCK_IDLE_HIGH )
  {
    spi_init.SPI_CPOL = SPI_CPOL_High;
  }
  else
  {
    spi_init.SPI_CPOL = SPI_CPOL_Low;
  }
  
  if ( config->mode & SPI_CLOCK_RISING_EDGE )
  {
    spi_init.SPI_CPHA = ( config->mode & SPI_CLOCK_IDLE_HIGH ) ? SPI_CPHA_2Edge : SPI_CPHA_1Edge;
  }
  else
  {
    spi_init.SPI_CPHA = ( config->mode & SPI_CLOCK_IDLE_HIGH ) ? SPI_CPHA_1Edge : SPI_CPHA_2Edge;
  }

  driver->peripheral = (platform_spi_t *)peripheral;
    
  /* Init SPI GPIOs */
  platform_gpio_set_alternate_function( peripheral->pin_clock->port, peripheral->pin_clock->pin_number, GPIO_OType_PP, GPIO_PuPd_NOPULL, peripheral->gpio_af );
  platform_gpio_set_alternate_function( peripheral->pin_mosi->port,  peripheral->pin_mosi->pin_number,  GPIO_OType_PP, GPIO_PuPd_NOPULL, peripheral->gpio_af );
  platform_gpio_set_alternate_function( peripheral->pin_miso->port,  peripheral->pin_miso->pin_number,  GPIO_OType_PP, GPIO_PuPd_UP, peripheral->gpio_af );
  
  /* Init the chip select GPIO */
  platform_gpio_init( config->chip_select, OUTPUT_PUSH_PULL );
  platform_gpio_output_high( config->chip_select );
  
  
  /* Enable SPI peripheral clock */
  (peripheral->peripheral_clock_func)( peripheral->peripheral_clock_reg, ENABLE );
  (peripheral->peripheral_clock_func)( peripheral->peripheral_clock_reg, ENABLE );
  
  SPI_I2S_DeInit( peripheral->port );
  
  spi_init.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
  spi_init.SPI_Mode      = SPI_Mode_Master;
  spi_init.SPI_NSS       = SPI_NSS_Soft;
  spi_init.SPI_CRCPolynomial = 0x7; /* reset value */
  SPI_CalculateCRC( peripheral->port, DISABLE );
  
  /* Init and enable SPI */
  SPI_Init( peripheral->port, &spi_init );
  
  SPI_I2S_DMACmd( peripheral->port, SPI_I2S_DMAReq_Rx, DISABLE );
  SPI_I2S_DMACmd( peripheral->port, SPI_I2S_DMAReq_Tx, DISABLE );
  
  SPI_Cmd ( peripheral->port, ENABLE );
  
  if ( config->mode & SPI_USE_DMA ){
    DMA_DeInit( peripheral->rx_dma.stream );
    DMA_DeInit( peripheral->tx_dma.stream );
    
    if ( peripheral->tx_dma.controller == DMA1 )
    {
      RCC->AHB1ENR |= RCC_AHB1Periph_DMA1;
    }
    else
    {
      RCC->AHB1ENR |= RCC_AHB1Periph_DMA2;
    }
    
     if ( peripheral->rx_dma.controller == DMA1 )
    {
      RCC->AHB1ENR |= RCC_AHB1Periph_DMA1;
    }
    else
    {
      RCC->AHB1ENR |= RCC_AHB1Periph_DMA2;
    }
    SPI_I2S_DMACmd( peripheral->port, SPI_I2S_DMAReq_Rx, ENABLE );
    SPI_I2S_DMACmd( peripheral->port, SPI_I2S_DMAReq_Tx, ENABLE );
  }

exit:
  platform_mcu_powersave_enable();
  return err;
}
void overo_link_arch_init(void) {

  /* Enable SPI_1 DMA clock ---------------------------------------------------*/
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
  /* Enable SPI1 Periph clock -------------------------------------------------*/
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
  /* Configure GPIOs: NSS, SCK, MISO and MOSI  --------------------------------*/
  GPIO_InitTypeDef GPIO_InitStructure;
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
  /* SPI_SLAVE configuration --------------------------------------------------*/
  SPI_InitTypeDef SPI_InitStructure;
  SPI_InitStructure.SPI_Direction         = SPI_Direction_2Lines_FullDuplex;
  SPI_InitStructure.SPI_Mode              = SPI_Mode_Slave;
  SPI_InitStructure.SPI_DataSize          = SPI_DataSize_8b;
  SPI_InitStructure.SPI_CPOL              = SPI_CPOL_Low;
  SPI_InitStructure.SPI_CPHA              = SPI_CPHA_2Edge;
  SPI_InitStructure.SPI_NSS               = SPI_NSS_Hard;
  SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
  SPI_InitStructure.SPI_FirstBit          = SPI_FirstBit_MSB;
  SPI_InitStructure.SPI_CRCPolynomial     = 0x31;
  SPI_Init(SPI1, &SPI_InitStructure);

  SPI_CalculateCRC(SPI1, ENABLE);
  /* Enable SPI_SLAVE */
  // SPI_Cmd(SPI1, DISABLE); why that ?
  SPI_Cmd(SPI1, ENABLE);

  /* Configure DMA1 channel2 IRQ Channel */
  NVIC_InitTypeDef NVIC_init_struct = {
    .NVIC_IRQChannel = DMA1_Channel2_IRQn,
    .NVIC_IRQChannelPreemptionPriority = 0,
    .NVIC_IRQChannelSubPriority = 0,
    .NVIC_IRQChannelCmd = ENABLE
  };
  NVIC_Init(&NVIC_init_struct);

  /* setup DMA for first transfert */
  overo_link_arch_prepare_next_transfert();

}


void overo_link_arch_prepare_next_transfert(void) {

  /* Disable SPI module */
  SPI_Cmd(SPI1, DISABLE);

  /* Make sure RX register is empty */
  uint8_t foo __attribute__ ((unused)) = SPI1->DR;
  /* Read status register to clear OVR, UDR, MODF flags */
  foo = SPI1->SR;
  /* clear possible CRC_ERR flag */
  SPI1->SR = (uint16_t)~SPI_FLAG_CRCERR;

  /* SPI_SLAVE_Rx_DMA_Channel configuration ------------------------------------*/
  DMA_DeInit(DMA1_Channel2);
  const DMA_InitTypeDef  DMA_InitStructure_rx = {
    .DMA_PeripheralBaseAddr = (uint32_t)(SPI1_BASE+0x0C),
    .DMA_MemoryBaseAddr     = (uint32_t)overo_link.down.array,
    .DMA_DIR                = DMA_DIR_PeripheralSRC,
    .DMA_BufferSize         = sizeof(union AutopilotMessage),
    .DMA_PeripheralInc      = DMA_PeripheralInc_Disable,
    .DMA_MemoryInc          = DMA_MemoryInc_Enable,
    .DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte,
    .DMA_MemoryDataSize     = DMA_MemoryDataSize_Byte,
    .DMA_Mode               = DMA_Mode_Normal,
    .DMA_Priority           = DMA_Priority_VeryHigh,
    .DMA_M2M                = DMA_M2M_Disable
  };
  DMA_Init(DMA1_Channel2, (DMA_InitTypeDef*)&DMA_InitStructure_rx);

  /* SPI_SLAVE_Tx_DMA_Channel configuration ------------------------------------*/
  DMA_DeInit(DMA1_Channel3);
  const DMA_InitTypeDef  DMA_InitStructure_tx = {
    .DMA_PeripheralBaseAddr = (uint32_t)(SPI1_BASE+0x0C),
    .DMA_MemoryBaseAddr = (uint32_t)overo_link.up.array,
    .DMA_DIR = DMA_DIR_PeripheralDST,
    .DMA_BufferSize         = sizeof(union AutopilotMessage),
    .DMA_PeripheralInc      = DMA_PeripheralInc_Disable,
    .DMA_MemoryInc          = DMA_MemoryInc_Enable,
    .DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte,
    .DMA_MemoryDataSize     = DMA_MemoryDataSize_Byte,
    .DMA_Mode               = DMA_Mode_Normal,
    .DMA_Priority           = DMA_Priority_Medium,
    .DMA_M2M                = DMA_M2M_Disable
  };
  DMA_Init(DMA1_Channel3, (DMA_InitTypeDef*)&DMA_InitStructure_tx);

  /* Enable SPI_1 Rx request */
  SPI_I2S_DMACmd(SPI1, SPI_I2S_DMAReq_Rx, ENABLE);
  /* Enable DMA1 Channel2 */
  DMA_Cmd(DMA1_Channel2, ENABLE);
  /* Enable SPI_1 Tx request */
  SPI_I2S_DMACmd(SPI1, SPI_I2S_DMAReq_Tx, ENABLE);
  /* Enable DMA1 Channel3 */
  DMA_Cmd(DMA1_Channel3, ENABLE);

  /* Enable DMA1 Channel2 Transfer Complete interrupt */
  DMA_ITConfig(DMA1_Channel2, DMA_IT_TC, ENABLE);

  /* resets CRC module */
  SPI_CalculateCRC(SPI1, DISABLE);
  SPI_CalculateCRC(SPI1, ENABLE);

  /* enable SPI */
  SPI_Cmd(SPI1, ENABLE);
}

void dma1_c2_irq_handler(void) {

  DMA_ITConfig(DMA1_Channel2, DMA_IT_TC, DISABLE);

  overo_link.status = DATA_AVAILABLE;

}
示例#24
0
void USB_cable(void)
{
    rt_device_t dev = RT_NULL;
    SD_CardInfo * sdio_info = RT_NULL;
    dev = rt_device_find("sd0");

    {
        SPI_InitTypeDef SPI_InitStructure;
        /*------------------------ SPI1 configuration ------------------------*/
        SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;//SPI_Direction_1Line_Tx;
        SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
        SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
        SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
        SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
        SPI_InitStructure.SPI_NSS  = SPI_NSS_Soft;
        SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16;/* 72M/64=1.125M */
        SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
        SPI_InitStructure.SPI_CRCPolynomial = 7;

        SPI_I2S_DeInit(SPI1);
        SPI_Init(SPI1, &SPI_InitStructure);

        /* Enable SPI_MASTER */
        SPI_Cmd(SPI1, ENABLE);
        SPI_CalculateCRC(SPI1, DISABLE);
    }

    {
        /* SPI_FLASH */
        struct rt_device_blk_geometry geometry;
        dev_spi_flash = rt_device_find("flash0");
		if(dev_spi_flash != RT_NULL)
		{
			rt_memset(&geometry, 0, sizeof(geometry));
            rt_device_control(dev_spi_flash, RT_DEVICE_CTRL_BLK_GETGEOME, &geometry);

            Mass_Block_Size[1]  = geometry.bytes_per_sector;
            Mass_Block_Count[1] = geometry.sector_count;
            Mass_Memory_Size[1] = geometry.bytes_per_sector * geometry.sector_count;
		}
		else
		{
		    rt_kprintf("\r\nNo find the device flash0 !!!!");
		}
    }

    if(dev != RT_NULL)
    {
        dev_sdio = dev;
        sdio_info = (SD_CardInfo *)dev->user_data;
        Mass_Memory_Size[0] = sdio_info->CardCapacity;
        Mass_Block_Size[0]  = sdio_info->CardBlockSize;
        Mass_Block_Count[0] = Mass_Memory_Size[0] / Mass_Block_Size[0];
    }
    else
    {
        rt_kprintf("\r\nNo find the device sd0 !!!!");
    }

    /* 3:NAND */

    /* usb msc up*/
    Set_System();
    Set_USBClock();
    USB_Interrupts_Config();
    USB_Init();

#if (USB_USE_AUTO_REMOVE == 1)
    /* delete the other thread */

    /* create msc_thread */
    {
        rt_thread_t msc_thread;
        msc_thread = rt_thread_create("msc_thread",
                                      msc_thread_entry, RT_NULL,
                                      1024, RT_THREAD_PRIORITY_MAX-1,1);
        if (msc_thread != RT_NULL) rt_thread_startup(msc_thread);
    }
#endif
}
示例#25
0
DRV_Spi_Error DRV_Spi_Open( const char *pcName , DRV_Spi_Handle *pHandle)
{
	DRV_Spi_Error eError = Spi_Device_Not_Found;
	int iSpiIndex;
	Spi_Device_Data *pSpiData;
	SPI_InitTypeDef  SPI_InitStructure;
	GPIO_InitTypeDef GPIO_InitStructure;
	volatile unsigned char ucDummyread;

	for ( iSpiIndex = 0 ; iSpiIndex < SPI_DEVICE_COUNT ; iSpiIndex++ )
	{
		if( !strcmp( pcName , SPI_DeviceDataList[iSpiIndex].pCfg->pcName) )
		{
			pSpiData = &SPI_DeviceDataList[iSpiIndex];
			eError=Spi_No_Error;
			break;
		}
	}
	if( eError==Spi_Device_Not_Found )
		return eError;
	if( pSpiData->eState == Spi_Device_Open)
		return Spi_AlreadyOpened;
	*pHandle =(DRV_Spi_Handle) pSpiData;
	/* Enable SPI clock */
	pSpiData->pCfg->PeriphClock.Cmd(pSpiData->pCfg->PeriphClock.Clock,ENABLE);
	  /* Enable SPI_SLAVE DMA clock */
	  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1  , ENABLE);
	/* Configure SPI pins: SCK and MOSI with default alternate function (not remapped) push-pull */
	GPIO_InitStructure.GPIO_Pin   = pSpiData->pCfg->PIO.SCK_Pin | pSpiData->pCfg->PIO.MOSI_Pin;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF_PP;
	GPIO_Init(pSpiData->pCfg->PIO.PORT, &GPIO_InitStructure);
	/* Configure MISO as Input with internal pull-up */
	GPIO_InitStructure.GPIO_Pin   = pSpiData->pCfg->PIO.MISO_Pin;
	GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_IPU;
	//GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_IN_FLOATING;
	GPIO_Init(pSpiData->pCfg->PIO.PORT, &GPIO_InitStructure);
	/* SPI configuration */
	SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
	SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
	SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
	SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
	SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
	SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
	SPI_InitStructure.SPI_BaudRatePrescaler =  pSpiData->pCfg->speed[DRV_SPI_SLOW];
	SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
	SPI_InitStructure.SPI_CRCPolynomial = 7;

	SPI_Init(pSpiData->pCfg->pDevice, &SPI_InitStructure);
	SPI_CalculateCRC(pSpiData->pCfg->pDevice, DISABLE);
	SPI_Cmd(pSpiData->pCfg->pDevice, ENABLE);

	/* drain SPI */
	while (SPI_I2S_GetFlagStatus(pSpiData->pCfg->pDevice, SPI_I2S_FLAG_TXE) == RESET)
		;
	ucDummyread = SPI_I2S_ReceiveData(pSpiData->pCfg->pDevice);
	/* Set device unlock */
	pSpiData->ucLock=0;

	return eError;
}
示例#26
0
文件: spi_bus.c 项目: XHelaoshi/USER
 static rt_err_t configure(struct rt_spi_device* device,
                           struct rt_spi_configuration* configuration)
 {
     struct stm32_spi_bus * stm32_spi_bus = (struct stm32_spi_bus *)device->bus;
     SPI_InitTypeDef SPI_InitStructure;
 
     SPI_StructInit(&SPI_InitStructure);
		
     /* data_width */
     if(configuration->data_width <= 8)
     {
         SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
     }
     else if(configuration->data_width <= 16)
     {
         SPI_InitStructure.SPI_DataSize = SPI_DataSize_16b;
     }
     else
     {
         return RT_EIO;
     }
 
     /* baudrate */
     {
         uint32_t SPI_APB_CLOCK;
         uint32_t stm32_spi_max_clock;
         uint32_t max_hz;
 
         stm32_spi_max_clock = 42000000;
         max_hz = configuration->max_hz;
 #ifdef STM32F4XX
         stm32_spi_max_clock = 37500000;
 #elif STM32F2XX
         stm32_spi_max_clock = 30000000;
 #endif
 
         if(max_hz > stm32_spi_max_clock)
         {
             max_hz = stm32_spi_max_clock;
         }
 
         SPI_APB_CLOCK = SystemCoreClock / 4;
 
         /* STM32F2xx SPI MAX 30Mhz */
         /* STM32F4xx SPI MAX 37.5Mhz */
         if(max_hz >= SPI_APB_CLOCK/2 && SPI_APB_CLOCK/2 <= 42000000)
         {
             SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
         }
         else if(max_hz >= SPI_APB_CLOCK/4)
         {
             SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
         }
         else if(max_hz >= SPI_APB_CLOCK/8)
         {
             SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8;
         }
         else if(max_hz >= SPI_APB_CLOCK/16)
         {
             SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16;
         }
         else if(max_hz >= SPI_APB_CLOCK/32)
         {
             SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_32;
         }
         else if(max_hz >= SPI_APB_CLOCK/64)
         {
             SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64;
         }
         else if(max_hz >= SPI_APB_CLOCK/128)
         {
             SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_128;
         }
         else
         {
             /*  min prescaler 256 */
             SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;
         }
     } /* baudrate */
 
     /* CPOL */
     if(configuration->mode & RT_SPI_CPOL)
     {
         SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
     }
     else
     {
         SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
     }
     /* CPHA */
     if(configuration->mode & RT_SPI_CPHA)
     {
         SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
     }
     else
     {
         SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
     }
     /* MSB or LSB */
     if(configuration->mode & RT_SPI_MSB)
     {
         SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
     }
     else
     {
         SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_LSB;
     }
     SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
     SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
     SPI_InitStructure.SPI_NSS  = SPI_NSS_Soft;
 
     /* init SPI */
     SPI_I2S_DeInit(stm32_spi_bus->SPI);
     SPI_Init(stm32_spi_bus->SPI, &SPI_InitStructure);
     /* Enable SPI_MASTER */
     SPI_Cmd(stm32_spi_bus->SPI, ENABLE);
     SPI_CalculateCRC(stm32_spi_bus->SPI, DISABLE);
 
     return RT_EOK;
 };
示例#27
0
/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{
  /* System Clocks Configuration */
  RCC_Configuration();
       
  /* Configure the GPIO ports */
  GPIO_Configuration();

  /* SPI_MASTER configuration ------------------------------------------------*/
  SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
  SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
  SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
  SPI_InitStructure.SPI_CPOL = 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 = CRCPolynomial;
  SPI_Init(SPI_MASTER, &SPI_InitStructure);
  
  /* SPI_SLAVE configuration ------------------------------------------------------*/
  SPI_InitStructure.SPI_Mode = SPI_Mode_Slave;
  SPI_Init(SPI_SLAVE, &SPI_InitStructure);

  /* SPI_MASTER_Rx_DMA_Channel configuration ---------------------------------*/
  DMA_DeInit(SPI_MASTER_Rx_DMA_Channel);
  DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)SPI_MASTER_DR_Base;
  DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)SPI_MASTER_Buffer_Rx;
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
  DMA_InitStructure.DMA_BufferSize = BufferSize;
  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
  DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
  DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
  DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
  DMA_InitStructure.DMA_Priority = DMA_Priority_High;
  DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
  DMA_Init(SPI_MASTER_Rx_DMA_Channel, &DMA_InitStructure);

  /* SPI_MASTER_Tx_DMA_Channel configuration ---------------------------------*/
  DMA_DeInit(SPI_MASTER_Tx_DMA_Channel);  
  DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)SPI_MASTER_DR_Base;
  DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)SPI_MASTER_Buffer_Tx;
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
  DMA_InitStructure.DMA_Priority = DMA_Priority_Low;
  DMA_Init(SPI_MASTER_Tx_DMA_Channel, &DMA_InitStructure);

  /* SPI_SLAVE_Rx_DMA_Channel configuration ----------------------------------*/
  DMA_DeInit(SPI_SLAVE_Rx_DMA_Channel);  
  DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)SPI_SLAVE_DR_Base;
  DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)SPI_SLAVE_Buffer_Rx;
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
  DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
  DMA_Init(SPI_SLAVE_Rx_DMA_Channel, &DMA_InitStructure);

  /* SPI_SLAVE_Tx_DMA_Channel configuration ----------------------------------*/
  DMA_DeInit(SPI_SLAVE_Tx_DMA_Channel);  
  DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)SPI_SLAVE_DR_Base;
  DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)SPI_SLAVE_Buffer_Tx;
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
  DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;
  DMA_Init(SPI_SLAVE_Tx_DMA_Channel, &DMA_InitStructure);

  /* Enable SPI_MASTER DMA Tx request */
  SPI_I2S_DMACmd(SPI_MASTER, SPI_I2S_DMAReq_Tx, ENABLE);
  /* Enable SPI_MASTER DMA Rx request */
  SPI_I2S_DMACmd(SPI_MASTER, SPI_I2S_DMAReq_Rx, ENABLE);
  /* Enable SPI_SLAVE DMA Tx request */
  SPI_I2S_DMACmd(SPI_SLAVE, SPI_I2S_DMAReq_Tx, ENABLE);
  /* Enable SPI_SLAVE DMA Rx request */
  SPI_I2S_DMACmd(SPI_SLAVE, SPI_I2S_DMAReq_Rx, ENABLE);
 
  /* Enable SPI_MASTER CRC calculation */
  SPI_CalculateCRC(SPI_MASTER, ENABLE);
  /* Enable SPI_SLAVE CRC calculation */
  SPI_CalculateCRC(SPI_SLAVE, ENABLE);

  /* Enable SPI_SLAVE */
  SPI_Cmd(SPI_SLAVE, ENABLE);
  /* Enable SPI_MASTER */
  SPI_Cmd(SPI_MASTER, ENABLE);

  /* Enable DMA channels */
  DMA_Cmd(SPI_MASTER_Rx_DMA_Channel, ENABLE);
  DMA_Cmd(SPI_SLAVE_Rx_DMA_Channel, ENABLE);
  DMA_Cmd(SPI_SLAVE_Tx_DMA_Channel, ENABLE);
  DMA_Cmd(SPI_MASTER_Tx_DMA_Channel, ENABLE);
   
  /* Transfer complete */
  while(!DMA_GetFlagStatus(SPI_MASTER_Rx_DMA_FLAG));
  while(!DMA_GetFlagStatus(SPI_SLAVE_Rx_DMA_FLAG));
  while(!DMA_GetFlagStatus(SPI_SLAVE_Tx_DMA_FLAG));
  while(!DMA_GetFlagStatus(SPI_MASTER_Tx_DMA_FLAG));

  /* Wait for SPI_MASTER data reception: CRC transmitted by SPI_SLAVE */
  while(SPI_I2S_GetFlagStatus(SPI_MASTER, SPI_I2S_FLAG_RXNE)==RESET);
  /* Wait for SPI_SLAVE data reception: CRC transmitted by SPI_MASTER */
  while(SPI_I2S_GetFlagStatus(SPI_SLAVE, SPI_I2S_FLAG_RXNE)==RESET);

  /* Check the corectness of written dada */
  TransferStatus1 = Buffercmp(SPI_SLAVE_Buffer_Rx, SPI_MASTER_Buffer_Tx, BufferSize);
  TransferStatus2 = Buffercmp(SPI_MASTER_Buffer_Rx, SPI_SLAVE_Buffer_Tx, BufferSize);
  /* TransferStatus1, TransferStatus2 = PASSED, if the data transmitted and received 
     are correct */
  /* TransferStatus1, TransferStatus2 = FAILED, if the data transmitted and received  
     are different */

  /* Test on the SPI_MASTER CRCR ERROR flag */
  if ((SPI_I2S_GetFlagStatus(SPI_MASTER, SPI_FLAG_CRCERR)) != RESET)
  {
    TransferStatus1 = FAILED;
  }
  /* Test on the SPI_SLAVE CRCR ERROR flag */
  if ((SPI_I2S_GetFlagStatus(SPI_SLAVE, SPI_FLAG_CRCERR)) != RESET)
  {
    TransferStatus2 = FAILED;
  } 

  /* Read SPI_MASTER received CRC value */
  SPI_MASTERCRCValue = SPI_I2S_ReceiveData(SPI_MASTER);
  /* Read SPI_SLAVE received CRC value */
  SPI_SLAVECRCValue = SPI_I2S_ReceiveData(SPI_SLAVE);

  while (1)
  {
  }
}
示例#28
0
void SPIx_Init(SPI_Driver* SPIx)
{
	SPI_InitTypeDef  SPI_InitStructure;
	GPIO_InitTypeDef GPIO_InitStructure;
#ifdef SPIx_USE_DMA
	DMA_InitTypeDef DMA_InitStructure;
#endif
	
	// Enable SPI and GPIO clocks
	SPIx->SPI_CLK(SPIx->SPI_Func, ENABLE);
	SPIx->GPIO_CLK(SPIx->GPIO_Func, ENABLE);

	// Connect SPI pins to AF
	GPIO_PinAFConfig(SPIx->Gpio, SPIx->SCK_Src, SPIx->GPIO_AF_SPI);
	GPIO_PinAFConfig(SPIx->Gpio, SPIx->MISO_Src, SPIx->GPIO_AF_SPI);
	GPIO_PinAFConfig(SPIx->Gpio, SPIx->MOSI_Src, SPIx->GPIO_AF_SPI);

	// SPI SCK/MISO/MOSI pin configuration
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_NOPULL;
	GPIO_InitStructure.GPIO_Pin = SPIx->SCK_Pin | SPIx->MISO_Pin | SPIx->MOSI_Pin;
	GPIO_Init(SPIx->Gpio, &GPIO_InitStructure);

	SPIx->GPIO_CS_CLK(SPIx->CS_Func, ENABLE);
	// Configure GPIO PIN for Chip select
	GPIO_InitStructure.GPIO_Pin = SPIx->CS_Pin;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(SPIx->Gpio_CS, &GPIO_InitStructure);

	// Chip DeSelect high
	CHIP_DESELECT(SPIx);

	//SPI configuration -------------------------------------------------------*/
	SPI_I2S_DeInit(SPIx->SPI);
	SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
	SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
	SPI_InitStructure.SPI_DataSize = SPIx->SPI_DataSize;
	SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
	SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
	SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
	SPI_InitStructure.SPI_BaudRatePrescaler = SPIx->SPI_Prescaler;
	SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
	SPI_InitStructure.SPI_CRCPolynomial = 7;
	SPI_Init(SPIx->SPI, &SPI_InitStructure);

	SPI_CalculateCRC(SPIx->SPI, DISABLE);

	//Enable SPI
	SPI_Cmd(SPIx->SPI, ENABLE);
	while (SPI_I2S_GetFlagStatus(SPIx->SPI, SPI_I2S_FLAG_TXE) == RESET);
	SPI_I2S_ReceiveData(SPIx->SPI);
	
#ifdef SPIx_USE_DMA
	// Enable DMA clock
	SPIx->DMA_CLK(SPIx->DMA_Func, ENABLE);
	//Enable the SPIx_RX_DMA_CHANNEL (SPIx_RX) Interrupt
	NVIC_Init(&SPIx->NVIC_DMA_TX);

	//Enable the SPIx_TX_DMA_CHANNEL (SPIx_TX) Interrupt
	NVIC_Init(&SPIx->NVIC_DMA_RX);

	// Deinitialize DMA Streams
	DMA_DeInit(SPIx->DMA_TX_Stream);
	while (DMA_GetCmdStatus(SPIx->DMA_TX_Stream) != DISABLE);
	DMA_Cmd(SPIx->DMA_TX_Stream, DISABLE);
	DMA_DeInit(SPIx->DMA_RX_Stream);
	while (DMA_GetCmdStatus(SPIx->DMA_RX_Stream) != DISABLE);
	DMA_Cmd(SPIx->DMA_RX_Stream, DISABLE);

	// Configure DMA Initialization Structure
	DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)(&(SPIx->SPI->DR));
	DMA_InitStructure.DMA_BufferSize = 0;
	DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
	DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_1QuarterFull;
	DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
	DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
	DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
	DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
	DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
	DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
	DMA_InitStructure.DMA_Priority = DMA_Priority_High;
	// Configure TX DMA
	DMA_InitStructure.DMA_Channel = SPIx->DMA_TX_CH;
	DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable;
	DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;
	DMA_InitStructure.DMA_Memory0BaseAddr =(uint32_t)0;
	DMA_Init(SPIx->DMA_TX_Stream, &DMA_InitStructure);
	// Configure RX DMA
	DMA_InitStructure.DMA_Channel = SPIx->DMA_RX_CH;
	DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
	DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
	DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)0; 
	DMA_Init(SPIx->DMA_RX_Stream, &DMA_InitStructure);

	SPI_DMACmd(SPIx->SPI, SPI_DMAReq_Rx, ENABLE);
	SPI_DMACmd(SPIx->SPI, SPI_DMAReq_Tx, ENABLE);

	DMA_ClearFlag(SPIx->DMA_TX_Stream, SPIx->DMA_TX_Flag);
	DMA_ClearFlag(SPIx->DMA_RX_Stream, SPIx->DMA_RX_Flag);

	DMA_ITConfig(SPIx->DMA_TX_Stream, DMA_IT_TC | DMA_IT_TE, ENABLE);
	DMA_ITConfig(SPIx->DMA_RX_Stream, DMA_IT_TC | DMA_IT_TE, ENABLE);

	DMA_Cmd(SPIx->DMA_TX_Stream, DISABLE);
	DMA_Cmd(SPIx->DMA_RX_Stream, DISABLE);
#endif
}
示例#29
0
文件: drv_spi.c 项目: UIKit0/AQ32Plus
void spiInit(SPI_TypeDef *SPI)
{
    volatile uint8_t dummyread __attribute__((unused));

    GPIO_InitTypeDef GPIO_InitStructure;
    SPI_InitTypeDef  SPI_InitStructure;

    ///////////////////////////////////

    if (SPI == SPI1)
    {
        RCC_AHB1PeriphClockCmd(SPI1_GPIO_CLOCK,     ENABLE);
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);

        GPIO_PinAFConfig(SPI1_GPIO, SPI1_SCK_PIN_SOURCE,  GPIO_AF_SPI1);
        GPIO_PinAFConfig(SPI1_GPIO, SPI1_MISO_PIN_SOURCE, GPIO_AF_SPI1);
        GPIO_PinAFConfig(SPI1_GPIO, SPI1_MOSI_PIN_SOURCE, GPIO_AF_SPI1);

        GPIO_StructInit(&GPIO_InitStructure);

        // Init pins
        GPIO_InitStructure.GPIO_Pin   = SPI1_SCK_PIN | SPI1_MISO_PIN | SPI1_MOSI_PIN;
        GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
        GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
        GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP;

        GPIO_Init(SPI1_GPIO, &GPIO_InitStructure);

        ///////////////////////////////

        SPI_StructInit(&SPI_InitStructure);

        SPI_InitStructure.SPI_Direction         = SPI_Direction_2Lines_FullDuplex;
        SPI_InitStructure.SPI_Mode              = SPI_Mode_Master;
        SPI_InitStructure.SPI_DataSize          = SPI_DataSize_8b;
        SPI_InitStructure.SPI_CPOL              = SPI_CPOL_Low;
        SPI_InitStructure.SPI_CPHA              = SPI_CPHA_1Edge;
        SPI_InitStructure.SPI_NSS               = SPI_NSS_Soft;
        SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_128;  // 42/128 = 328.125 kHz SPI Clock
        SPI_InitStructure.SPI_FirstBit          = SPI_FirstBit_MSB;
        SPI_InitStructure.SPI_CRCPolynomial     = 7;

        SPI_Init(SPI1, &SPI_InitStructure);

        SPI_CalculateCRC(SPI1, DISABLE);

        SPI_Cmd(SPI1, ENABLE);

        while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);

        dummyread = SPI_I2S_ReceiveData(SPI1);
    }

    ///////////////////////////////////

    if (SPI == SPI2)
    {
        RCC_AHB1PeriphClockCmd(SPI2_GPIO_CLOCK,     ENABLE);
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);

        GPIO_StructInit(&GPIO_InitStructure);

        // Init pins
        GPIO_InitStructure.GPIO_Pin   = SPI2_SCK_PIN | SPI2_MISO_PIN | SPI2_MOSI_PIN;
        GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        //GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
        //GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_NOPULL;

        GPIO_Init(SPI2_GPIO, &GPIO_InitStructure);

        GPIO_PinAFConfig(SPI2_GPIO, SPI2_SCK_PIN_SOURCE,  GPIO_AF_SPI2);
        GPIO_PinAFConfig(SPI2_GPIO, SPI2_MISO_PIN_SOURCE, GPIO_AF_SPI2);
        GPIO_PinAFConfig(SPI2_GPIO, SPI2_MOSI_PIN_SOURCE, GPIO_AF_SPI2);

        ///////////////////////////////

        RCC_AHB1PeriphClockCmd(MAX7456_CS_GPIO_CLOCK, ENABLE);

        GPIO_InitStructure.GPIO_Pin   = MAX7456_CS_PIN;
        GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_OUT;
        //GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        //GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
        //GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_NOPULL;

        GPIO_Init(MAX7456_CS_GPIO, &GPIO_InitStructure);

        ///////////////////////////////

        SPI_StructInit(&SPI_InitStructure);

        SPI_InitStructure.SPI_Direction         = SPI_Direction_2Lines_FullDuplex;
        SPI_InitStructure.SPI_Mode              = SPI_Mode_Master;
        SPI_InitStructure.SPI_DataSize          = SPI_DataSize_8b;
        SPI_InitStructure.SPI_CPOL              = SPI_CPOL_High;
        SPI_InitStructure.SPI_CPHA              = SPI_CPHA_2Edge;
        SPI_InitStructure.SPI_NSS               = SPI_NSS_Soft;
        SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;  // 42/4 = 10.5 MHz SPI Clock
        SPI_InitStructure.SPI_FirstBit          = SPI_FirstBit_MSB;
        SPI_InitStructure.SPI_CRCPolynomial     = 7;

        SPI_Init(SPI2, &SPI_InitStructure);

        SPI_Cmd(SPI2, ENABLE);

        SPI_CalculateCRC(SPI2, DISABLE);

        SPI_Cmd(SPI2, ENABLE);

        DISABLE_MAX7456;

        while (SPI_I2S_GetFlagStatus(MAX7456_SPI, SPI_I2S_FLAG_TXE) == RESET);

        dummyread = SPI_I2S_ReceiveData(MAX7456_SPI);
    }

    ///////////////////////////////////

    if (SPI == SPI3)
    {
        RCC_AHB1PeriphClockCmd(SPI3_GPIO_CLOCK,     ENABLE);
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI3, ENABLE);

        GPIO_StructInit(&GPIO_InitStructure);

        // Init pins
        GPIO_InitStructure.GPIO_Pin   = SPI3_SCK_PIN | SPI3_MISO_PIN | SPI3_MOSI_PIN;
        GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        //GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
        //GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_NOPULL;

        GPIO_Init(SPI3_GPIO, &GPIO_InitStructure);

        GPIO_PinAFConfig(SPI3_GPIO, SPI3_SCK_PIN_SOURCE,  GPIO_AF_SPI3);
        GPIO_PinAFConfig(SPI3_GPIO, SPI3_MISO_PIN_SOURCE, GPIO_AF_SPI3);
        GPIO_PinAFConfig(SPI3_GPIO, SPI3_MOSI_PIN_SOURCE, GPIO_AF_SPI3);

        GPIO_StructInit(&GPIO_InitStructure);

        ///////////////////////////////

        RCC_AHB1PeriphClockCmd(MPU6000_CS_GPIO_CLOCK, ENABLE);

        GPIO_InitStructure.GPIO_Pin   = MPU6000_CS_PIN;
        GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_OUT;
        //GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        //GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
        //GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_NOPULL;

        GPIO_Init(MPU6000_CS_GPIO, &GPIO_InitStructure);

        ///////////////////////////////

        SPI_StructInit(&SPI_InitStructure);

        SPI_InitStructure.SPI_Direction         = SPI_Direction_2Lines_FullDuplex;
        SPI_InitStructure.SPI_Mode              = SPI_Mode_Master;
        SPI_InitStructure.SPI_DataSize          = SPI_DataSize_8b;
        SPI_InitStructure.SPI_CPOL              = SPI_CPOL_High;
        SPI_InitStructure.SPI_CPHA              = SPI_CPHA_2Edge;
        SPI_InitStructure.SPI_NSS               = SPI_NSS_Soft;
        SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64;  // 42/64 = 0.65625 MHz SPI Clock
        SPI_InitStructure.SPI_FirstBit          = SPI_FirstBit_MSB;
        SPI_InitStructure.SPI_CRCPolynomial     = 7;

        SPI_Init(SPI3, &SPI_InitStructure);

        SPI_Cmd(SPI3, ENABLE);

        SPI_CalculateCRC(SPI3, DISABLE);

        SPI_Cmd(SPI3, ENABLE);

        DISABLE_MPU6000;

        while (SPI_I2S_GetFlagStatus(MPU6000_SPI, SPI_I2S_FLAG_TXE) == RESET);

        dummyread = SPI_I2S_ReceiveData(MPU6000_SPI);
    }

    ///////////////////////////////////
}
示例#30
0
static void spi_config(void)
{
	SPI_InitTypeDef   SPI_InitStructure;

#if 1
	/*
	 * SPI1 -- sst25vf016b
	 *
	 * SPI1 clk -- APB2, fCLK2(max = 72MHz)
	 */
	SPI_InitStructure.SPI_Direction 	= SPI_Direction_2Lines_FullDuplex;
	SPI_InitStructure.SPI_Mode 		= SPI_Mode_Master;
	SPI_InitStructure.SPI_DataSize 		= SPI_DataSize_8b;
	SPI_InitStructure.SPI_CPOL 		= SPI_CPOL_High;
	SPI_InitStructure.SPI_CPHA 		= SPI_CPHA_2Edge;
	SPI_InitStructure.SPI_NSS 		= SPI_NSS_Soft;
	SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2; /* mark by David */
	SPI_InitStructure.SPI_FirstBit 		= SPI_FirstBit_MSB;
	SPI_InitStructure.SPI_CRCPolynomial 	= 7;
	SPI_Init(SPI1, &SPI_InitStructure);

	SPI_Cmd(SPI1, ENABLE);
#endif

#if 1
	/* touch chip SPI configuration
	 * tsc2046 fclk(max) = 2.5MHz
	 */

	SPI_InitStructure.SPI_Direction 	= SPI_Direction_2Lines_FullDuplex;
	SPI_InitStructure.SPI_Mode      	= SPI_Mode_Master;
	SPI_InitStructure.SPI_DataSize 		= SPI_DataSize_8b;
	SPI_InitStructure.SPI_CPOL 		= SPI_CPOL_Low;
	SPI_InitStructure.SPI_CPHA 		= SPI_CPHA_1Edge;
	SPI_InitStructure.SPI_NSS  		= SPI_NSS_Soft;
	SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_32;  /* 36M/32=1.125M */
	SPI_InitStructure.SPI_FirstBit 		= SPI_FirstBit_MSB;
	SPI_InitStructure.SPI_CRCPolynomial 	= 7;

	SPI_I2S_DeInit(TOUCH_USE_SPIX);
	SPI_Init(TOUCH_USE_SPIX, &SPI_InitStructure);
	SPI_Cmd(TOUCH_USE_SPIX, ENABLE);
	SPI_CalculateCRC(TOUCH_USE_SPIX, DISABLE);

#endif
#if 0
	/* SPI2 configuration */
	/*
	 * SPI2 clk -- APB1, fCLK1(max = 36MHz)
	 * enc28j60's clk(max) = 10MHz
	 * DS80349C.errata #1:When the SPI clock from the host microcontroller is run at frequencies of less than 8 MHz,
	 * reading or writing to the MAC registers may be unreliable.
	 */
	SPI_InitStructure.SPI_Direction 	= SPI_Direction_2Lines_FullDuplex;
	SPI_InitStructure.SPI_Mode 		= SPI_Mode_Master;
	SPI_InitStructure.SPI_DataSize 		= SPI_DataSize_8b;
	SPI_InitStructure.SPI_CPOL 		= SPI_CPOL_Low;
	SPI_InitStructure.SPI_CPHA 		= SPI_CPHA_1Edge;
	SPI_InitStructure.SPI_NSS 		= SPI_NSS_Soft;
	SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
	SPI_InitStructure.SPI_FirstBit 		= SPI_FirstBit_MSB;
	SPI_InitStructure.SPI_CRCPolynomial 	= 7;
	SPI_Init(SPI2, &SPI_InitStructure);

	SPI_Cmd(SPI2, ENABLE);
#endif

	return;
}