cancel
Showing results for 
Search instead for 
Did you mean: 

SPI1 is not starting in my controller?

no 36
Associate
Posted on June 09, 2017 at 14:08

why spi1 is not starting in my controller.

here below is my code

#include 'stm32f10x.h'
 #include 'stm32f10x_rcc.h'
 #include 'stm32f10x_gpio.h'
 #include 'stm32f10x_spi.h'
#define SPIx_RCC RCC_APB2Periph_SPI1
 #define SPIx SPI1
 #define SPI_GPIO_RCC RCC_APB2Periph_GPIOA
 #define SPI_GPIO GPIOA
 #define SPI_PIN_MOSI GPIO_Pin_7
 #define SPI_PIN_MISO GPIO_Pin_6
 #define SPI_PIN_SCK GPIO_Pin_5
 #define SPI_PIN_SS GPIO_Pin_4
 SPI_InitTypeDef SPI_InitStruct;
 GPIO_InitTypeDef GPIO_InitStruct;
 GPIO_InitTypeDef GPIO_InitStructure;
//SPI_InitTypeDef SPI_InitStructure;
 #define BufferSize 32
 #define SEND_LIMIT 3
 uint8_t SPIz_Buffer_Tx[BufferSize] = {0x1, 0x2, 0x4, 0x54, 0x55, 0x56, 0x57,
 0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E,
 0x5F, 0x60, 0x61, 0x62, 0x63, 0x64, 0x65,
 0x66, 0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C,
 0x6D, 0x6E, 0x6F, 0x70};
__IO uint8_t TxIdx = 0, RxIdx = 0, k = 0;
void SPIx_EnableSlave()
 {
 // Set slave SS pin low
 SPI_GPIO->BRR = SPI_PIN_SS;
 }
 void SPIx_DisableSlave()
 {
 // Set slave SS pin high
 SPI_GPIO->BSRR = SPI_PIN_SS;
 }
void SPIx_Init()
 {
 /* Initialize LED which connected to PC13 */
 // Enable PORTC Clock
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
 /* Configure the GPIO_LED pin */
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
 GPIO_Init(GPIOC, &GPIO_InitStructure);
//GPIO_SetBits(GPIOC, GPIO_Pin_13); // Set C13 to High level ('1')
 GPIO_ResetBits(GPIOC, GPIO_Pin_13); // Set C13 to Low level ('0')
// Step 1: Initialize SPI
 RCC_APB2PeriphClockCmd(SPIx_RCC, ENABLE);
 SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_128;
 SPI_InitStruct.SPI_CPHA = SPI_CPHA_1Edge;
 SPI_InitStruct.SPI_CPOL = SPI_CPOL_Low;
 SPI_InitStruct.SPI_DataSize = SPI_DataSize_8b;
 SPI_InitStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
 SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;
 SPI_InitStruct.SPI_Mode = SPI_Mode_Master;
 SPI_InitStruct.SPI_NSS = SPI_NSS_Soft | SPI_NSSInternalSoft_Set;
 SPI_Init(SPIx, &SPI_InitStruct);
 SPI_Cmd(SPIx, ENABLE);
// Step 2: Initialize GPIO
 RCC_APB2PeriphClockCmd(SPI_GPIO_RCC, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
 // GPIO pins for MOSI, MISO, and SCK
 GPIO_InitStructure.GPIO_Pin = SPI_PIN_MOSI | SPI_PIN_SCK;
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;//GPIO_Mode_Out_PP;//
 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
 GPIO_Init(SPI_GPIO, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = SPI_PIN_MISO ;
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//GPIO_Mode_Out_PP;//
 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
 GPIO_Init(SPI_GPIO, &GPIO_InitStructure);
// GPIO pin for SS
 GPIO_InitStructure.GPIO_Pin = SPI_PIN_SS;
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
 GPIO_Init(SPI_GPIO, &GPIO_InitStructure);
 // Disable SPI slave device
 SPIx_DisableSlave();
 }
 uint8_t SPIx_Transfer(uint8_t data)
 {
 // Write data to be transmitted to the SPI data register
 SPIx->DR = data;
 // Wait until transmit complete
 while (!(SPIx->SR & (SPI_I2S_FLAG_TXE)));
 // Wait until receive comple
 while (!(SPIx->SR & (SPI_I2S_FLAG_RXNE)));
 // Wait until SPI is not busy anymore
 while (SPIx->SR & (SPI_I2S_FLAG_BSY));
 // Return received data from SPI data register
 return SPIx->DR;
 }

int main(void)
 {
 int i;
SPIx_Init();
while (1) {
GPIOC->ODR ^= GPIO_Pin_13; // Invert C13
 SPIx_EnableSlave();
 /* delay */
 for(i=0;i<0x100000;i++);
 // SPIx_Transfer('1');
 while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET)
 {
 }
 /* Send dummy data */
 SPI_I2S_SendData(SPI1, 'b');
// SPI_I2S_SendData (SPI1,0xff);
 for(i=0;i<0x100000;i++);
 SPIx_DisableSlave();
 /* Toggle LED which connected to PC13*/
 GPIOC->ODR ^= GPIO_Pin_13;
/* delay */
 for(i=0;i<0x100000;i++);
 }
}�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?

#stm32-f
1 REPLY 1
Nesrine M_O
Lead II
Posted on June 09, 2017 at 14:51

Hi

no36

,

  • You are using a custom board or an ST board?
  • Could you please try to start from ready example under the STM32F0 standard peripherals library : STM32F0xx_StdPeriph_Lib_V1.5.0\Projects\STM32F0xx_StdPeriph_Examples\SPI\SPI_TwoBoards\DataExchangeInterrupt

-Nesrine-