/** * @brief Configures the SPI Peripheral. */ void spi_config(void) { SPI_InitTypeDef SPI_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; /* Enable the SPI clock */ SPIx_CLK_INIT(SPIx_CLK, ENABLE); /* Enable GPIO clocks */ RCC_AHB1PeriphClockCmd(SPIx_SCK_GPIO_CLK | SPIx_MISO_GPIO_CLK | SPIx_MOSI_GPIO_CLK | l3gd20_CS_GPIO_CLK, ENABLE); /* Connect SPI pins to AF5 */ GPIO_PinAFConfig(SPIx_SCK_GPIO_PORT, SPIx_SCK_SOURCE, SPIx_SCK_AF); GPIO_PinAFConfig(SPIx_MISO_GPIO_PORT, SPIx_MISO_SOURCE, SPIx_MISO_AF); GPIO_PinAFConfig(SPIx_MOSI_GPIO_PORT, SPIx_MOSI_SOURCE, SPIx_MOSI_AF); 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_DOWN; /* SPI SCK pin configuration */ GPIO_InitStructure.GPIO_Pin = SPIx_SCK_PIN; GPIO_Init(SPIx_SCK_GPIO_PORT, &GPIO_InitStructure); /* SPI MOSI pin configuration */ GPIO_InitStructure.GPIO_Pin = SPIx_MOSI_PIN; GPIO_Init(SPIx_MOSI_GPIO_PORT, &GPIO_InitStructure); /* SPI MISO pin configuration */ GPIO_InitStructure.GPIO_Pin = SPIx_MISO_PIN; GPIO_Init(SPIx_MISO_GPIO_PORT, &GPIO_InitStructure); /* Configure l3gd20 CS pin in output pushpull mode */ GPIO_InitStructure.GPIO_Pin = l3gd20_CS_PIN; 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(l3gd20_CS_GPIO_PORT, &GPIO_InitStructure); /* Deselect the l3gd20: Chip Select high */ l3gd20_CS_HIGH(); /* SPI 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 = DISABLE; SPI_Init(SPIx, &SPI_InitStructure); SPI_Cmd(SPIx, ENABLE); }
//Draws a single pixel void ssd1306_SPI_Config(void) { GPIO_InitTypeDef GPIO_InitStructure; SPI_InitTypeDef SPI_InitStructure; /* Peripheral Clock Enable -------------------------------------------------*/ /* Enable the SPI clock */ SPIx_CLK_INIT(SPIx_CLK, ENABLE); /* Enable GPIO clocks */ RCC_AHB1PeriphClockCmd(SPIx_SCK_GPIO_CLK | SPIx_MOSI_GPIO_CLK, ENABLE); /* SPI GPIO Configuration --------------------------------------------------*/ /* Connect SPI pins to AF5 */ GPIO_PinAFConfig(SPIx_SCK_GPIO_PORT, SPIx_SCK_SOURCE, SPIx_SCK_AF); GPIO_PinAFConfig(SPIx_MOSI_GPIO_PORT, SPIx_MOSI_SOURCE, SPIx_MOSI_AF); 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_DOWN; /* SPI SCK pin configuration */ GPIO_InitStructure.GPIO_Pin = SPIx_SCK_PIN; GPIO_Init(SPIx_SCK_GPIO_PORT, &GPIO_InitStructure); /* SPI MOSI pin configuration */ GPIO_InitStructure.GPIO_Pin = SPIx_MOSI_PIN; GPIO_Init(SPIx_MOSI_GPIO_PORT, &GPIO_InitStructure); /* SPI configuration -------------------------------------------------------*/ SPI_I2S_DeInit(SPIx); SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; // Clock Polarity ACTIVE state is High or Low SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; // Clock Phase Data is sampled on the first or second clock edge transition SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_InitStructure.SPI_CRCPolynomial = 7; /* Configure the Priority Group to 1 bit NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); Configure the SPI interrupt priority NVIC_InitStructure.NVIC_IRQChannel = SPIx_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); */ /* Initializes the SPI communication -----------------------------------------*/ SPI_InitStructure.SPI_Mode = SPI_Mode_Master; SPI_Init(SPIx, &SPI_InitStructure); /* The Data transfer is performed in the SPI interrupt routine----------------*/ /* Enable the SPI peripheral */ SPI_Cmd(SPIx, ENABLE); }
void SPI1_Config(void) { GPIO_InitTypeDef GPIO_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; /* Enable the SPI clock */ SPIx_CLK_INIT(SPIx_CLK, ENABLE); /* Enable GPIO clocks */ RCC_AHB1PeriphClockCmd(SPIx_SCK_GPIO_CLK | SPIx_MISO_GPIO_CLK | SPIx_MOSI_GPIO_CLK, ENABLE); /* Connect SPI pins to AF5 */ GPIO_PinAFConfig(SPIx_SCK_GPIO_PORT, SPIx_SCK_SOURCE, SPIx_SCK_AF); GPIO_PinAFConfig(SPIx_MOSI_GPIO_PORT, SPIx_MOSI_SOURCE, SPIx_MOSI_AF); GPIO_PinAFConfig(SPIx_MISO_GPIO_PORT, SPIx_MISO_SOURCE, SPIx_MOSI_AF); 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_DOWN; /* SPI SCK pin configuration */ GPIO_InitStructure.GPIO_Pin = SPIx_SCK_PIN; GPIO_Init(SPIx_SCK_GPIO_PORT, &GPIO_InitStructure); /* SPI MOSI pin configuration */ GPIO_InitStructure.GPIO_Pin = SPIx_MOSI_PIN; GPIO_Init(SPIx_MOSI_GPIO_PORT, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = SPIx_MISO_PIN; GPIO_Init(SPIx_MISO_GPIO_PORT, &GPIO_InitStructure); /* SPI configuration -------------------------------------------------------*/ SPI_I2S_DeInit(SPIx); 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; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_InitStructure.SPI_CRCPolynomial = 0; SPI_Init(SPIx, &SPI_InitStructure); /* Configure the Priority Group to 1 bit */ NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); /* Configure the SPI interrupt priority */ NVIC_InitStructure.NVIC_IRQChannel = SPI1_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); }
// copy-paste from ST.com examples with DMA void nrf24init_dma(void){ GPIO_InitTypeDef GPIO_InitStructure; DMA_InitTypeDef DMA_InitStructure; SPI_InitTypeDef SPI_InitStructure; /* Peripheral Clock Enable -------------------------------------------------*/ /* Enable the SPI clock */ SPIx_CLK_INIT(NRF24_SPIx_CLK, ENABLE); /* Enable GPIO clocks */ RCC_AHB1PeriphClockCmd(NRF24_SPIx_SCK_GPIO_CLK | NRF24_SPIx_MISO_GPIO_CLK | NRF24_SPIx_MOSI_GPIO_CLK, ENABLE); /* Enable DMA clock */ RCC_AHB1PeriphClockCmd(NRF24_SPIx_DMA_CLK, ENABLE); /* SPI GPIO Configuration --------------------------------------------------*/ /* GPIO Deinitialisation */ GPIO_DeInit(NRF24_SPIx_SCK_GPIO_PORT); GPIO_DeInit(NRF24_SPIx_MISO_GPIO_PORT); GPIO_DeInit(NRF24_SPIx_MOSI_GPIO_PORT); /* Connect SPI pins to AF5 */ GPIO_PinAFConfig(NRF24_SPIx_SCK_GPIO_PORT, NRF24_SPIx_SCK_SOURCE, NRF24_SPIx_SCK_AF); GPIO_PinAFConfig(NRF24_SPIx_MISO_GPIO_PORT, NRF24_SPIx_MISO_SOURCE, NRF24_SPIx_MISO_AF); GPIO_PinAFConfig(NRF24_SPIx_MOSI_GPIO_PORT, NRF24_SPIx_MOSI_SOURCE, NRF24_SPIx_MOSI_AF); 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_DOWN; /* SPI SCK pin configuration */ GPIO_InitStructure.GPIO_Pin = NRF24_SPIx_SCK_PIN; GPIO_Init(NRF24_SPIx_SCK_GPIO_PORT, &GPIO_InitStructure); /* SPI MISO pin configuration */ GPIO_InitStructure.GPIO_Pin = NRF24_SPIx_MISO_PIN; GPIO_Init(NRF24_SPIx_MISO_GPIO_PORT, &GPIO_InitStructure); /* SPI MOSI pin configuration */ GPIO_InitStructure.GPIO_Pin = NRF24_SPIx_MOSI_PIN; GPIO_Init(NRF24_SPIx_MOSI_GPIO_PORT, &GPIO_InitStructure); /* SPI configuration -------------------------------------------------------*/ SPI_I2S_DeInit(NRF24_SPIx); 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; // master setting? NRF24_SPIx->CR2 |= (uint16_t)SPI_CR2_SSOE; // set nss pin as output SPI1->CR2 |= SPI_CR2_SSOE; NRF24_SPIx->CR1 |= SPI_NSSInternalSoft_Set; /* DMA configuration -------------------------------------------------------*/ /* Deinitialize DMA Streams */ DMA_DeInit(NRF24_SPIx_TX_DMA_STREAM); DMA_DeInit(NRF24_SPIx_RX_DMA_STREAM); /* Configure DMA Initialization Structure */ DMA_InitStructure.DMA_BufferSize = NRF24_DMA_BUFFERSIZE; 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_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; DMA_InitStructure.DMA_PeripheralBaseAddr =(uint32_t) (&(NRF24_SPIx->DR)); 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 = NRF24_SPIx_TX_DMA_CHANNEL; DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral; DMA_InitStructure.DMA_Memory0BaseAddr =(uint32_t)aTxBuffer; DMA_Init(NRF24_SPIx_TX_DMA_STREAM, &DMA_InitStructure); /* Configure RX DMA */ DMA_InitStructure.DMA_Channel = NRF24_SPIx_RX_DMA_CHANNEL; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; DMA_InitStructure.DMA_Memory0BaseAddr =(uint32_t)aRxBuffer; DMA_Init(NRF24_SPIx_RX_DMA_STREAM, &DMA_InitStructure); }
void glcd_init(void) { /* Initialization of device */ /* Declare GPIO and SPI init structures */ GPIO_InitTypeDef GPIO_InitStructure; SPI_InitTypeDef SPI_InitStructure; /* Configuring CS, DC (A0) and RST pin */ /* Peripheral clock init. */ RCC_AHB1PeriphClockCmd(CONTROLLER_SPI_DC_RCC | CONTROLLER_SPI_SS_RCC | \ CONTROLLER_SPI_RST_RCC, ENABLE); /* CS pin */ GPIO_InitStructure.GPIO_Pin = CONTROLLER_SPI_SS_PIN; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_Init(CONTROLLER_SPI_SS_PORT, &GPIO_InitStructure); /* DC (A0) pin */ GPIO_InitStructure.GPIO_Pin = CONTROLLER_SPI_DC_PIN; GPIO_Init(CONTROLLER_SPI_DC_PORT, &GPIO_InitStructure); /* RST pin */ GPIO_InitStructure.GPIO_Pin = CONTROLLER_SPI_RST_PIN; GPIO_Init(CONTROLLER_SPI_RST_PORT, &GPIO_InitStructure); /* Make sure chip is de-selected by default */ GLCD_DESELECT(); /* * Configuring SPI: */ /* Enable the SPI clock */ SPIx_CLK_INIT(SPIx_CLK, ENABLE); /* Enable SPI GPIO clocks */ RCC_AHB1PeriphClockCmd(SPIx_SCK_GPIO_CLK | SPIx_MOSI_GPIO_CLK, ENABLE); /* Connect SPI pins to the corresponding alternate function */ GPIO_PinAFConfig(SPIx_SCK_GPIO_PORT, SPIx_SCK_SOURCE, SPIx_SCK_AF); GPIO_PinAFConfig(SPIx_MOSI_GPIO_PORT, SPIx_MOSI_SOURCE, SPIx_MOSI_AF); // GPIO_PinAFConfig(SPIx_MISO_GPIO_PORT, SPIx_MISO_SOURCE, SPIx_MISO_AF); 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_DOWN; /* SPI SCK pin configuration */ GPIO_InitStructure.GPIO_Pin = SPIx_SCK_PIN; GPIO_Init(SPIx_SCK_GPIO_PORT, &GPIO_InitStructure); /* SPI MOSI pin configuration */ GPIO_InitStructure.GPIO_Pin = SPIx_MOSI_PIN; GPIO_Init(SPIx_MOSI_GPIO_PORT, &GPIO_InitStructure); #if 0 GPIO_InitStructure.GPIO_Pin = SPIx_MISO_PIN; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_OD; GPIO_Init(SPIx_MISO_GPIO_PORT, &GPIO_InitStructure); #endif /* SPI configuration */ SPI_I2S_DeInit(SPIx); SPI_InitStructure.SPI_Mode = SPI_Mode_Master; SPI_InitStructure.SPI_Direction = SPI_Direction_1Line_Tx; 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; /* Clock set to 8MHz */ SPI_InitStructure.SPI_BaudRatePrescaler = SPIx_PRESCALLER; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_Init(SPIx, &SPI_InitStructure); /* Enable the SPI peripheral */ SPI_Cmd(SPIx, ENABLE); RCC_AHB1PeriphClockCmd(LCD_LED_GPIO_RCC, ENABLE); GPIO_InitStructure.GPIO_Pin = LCD_LED_PIN; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_Init(LCD_LED_PORT, &GPIO_InitStructure); #if defined(GLCD_CONTROLLER_PCD8544) /* Initialisation sequence of controller */ glcd_PCD8544_init(); #elif defined(GLCD_CONTROLLER_ST7565R) glcd_reset(); glcd_enable_backlight(ENABLE); glcd_ST7565R_init(); #else #error "Controller not supported by STM32F0xx" #endif }