AnsweredAssumed Answered

Defining SPI2 manually on STM32CubeMx ?

Question asked by H.Rick on Jan 17, 2016
Guys,

I want to manually define SPI2,
I generated SPI1 from STM32CubeMX, but I have done some modification on some files,
and I want to initialize SPI2 myself, is it only at spi.c...or I need to make some changes at other files ?

This is what I have made on spi.c

001./* SPI1 init function */
002.void MX_SPI1_Init(void)
003.{
004. 
005.  hspi1.Instance = SPI1;
006.  hspi1.Init.Mode = SPI_MODE_MASTER;
007.  hspi1.Init.Direction = SPI_DIRECTION_2LINES;
008.  hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
009.  hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
010.  hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
011.  hspi1.Init.NSS = SPI_NSS_SOFT;
012.  hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_4;
013.  hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
014.  hspi1.Init.TIMode = SPI_TIMODE_DISABLED;
015.  hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED;
016.  hspi1.Init.CRCPolynomial = 10;
017.  HAL_SPI_Init(&hspi1);
018. 
019.}
020. 
021./* SPI1 init function */
022.void MX_SPI1_Init_Low(void)
023.{
024. 
025.  hspi1.Instance = SPI1;
026.  hspi1.Init.Mode = SPI_MODE_MASTER;
027.  hspi1.Init.Direction = SPI_DIRECTION_2LINES;
028.  hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
029.  hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
030.  hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
031.  hspi1.Init.NSS = SPI_NSS_SOFT;
032.  hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_256;
033.  hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
034.  hspi1.Init.TIMode = SPI_TIMODE_DISABLED;
035.  hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED;
036.  hspi1.Init.CRCPolynomial = 10;
037.  HAL_SPI_Init(&hspi1);
038. 
039.}
040. 
041./* SPI1 init function */
042.void MX_SPI2_Init(void)
043.{
044. 
045.  hspi2.Instance = SPI2;
046.  hspi2.Init.Mode = SPI_MODE_MASTER;
047.  hspi2.Init.Direction = SPI_DIRECTION_2LINES;
048.  hspi2.Init.DataSize = SPI_DATASIZE_8BIT;
049.  hspi2.Init.CLKPolarity = SPI_POLARITY_LOW;
050.  hspi2.Init.CLKPhase = SPI_PHASE_1EDGE;
051.  hspi2.Init.NSS = SPI_NSS_SOFT;
052.  hspi2.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_4;
053.  hspi2.Init.FirstBit = SPI_FIRSTBIT_MSB;
054.  hspi2.Init.TIMode = SPI_TIMODE_DISABLED;
055.  hspi2.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED;
056.  hspi2.Init.CRCPolynomial = 10;
057.  HAL_SPI_Init(&hspi2);
058. 
059.}
060. 
061./* SPI1 init function */
062. 
063. 
064.void HAL_SPI_MspInit(SPI_HandleTypeDef* hspi)
065.{
066. 
067.  GPIO_InitTypeDef GPIO_InitStruct;
068.  if(hspi->Instance==SPI1)
069.  {
070.  /* USER CODE BEGIN SPI1_MspInit 0 */
071. 
072.  /* USER CODE END SPI1_MspInit 0 */
073.    /* Peripheral clock enable */
074.    __SPI1_CLK_ENABLE();
075.        __SPI2_CLK_ENABLE();
076.   
077.    /**SPI1 GPIO Configuration   
078.    PA5     ------> SPI1_SCK
079.    PA6     ------> SPI1_MISO
080.    PA7     ------> SPI1_MOSI
081.    */
082.    GPIO_InitStruct.Pin = GPIO_PIN_5|GPIO_PIN_7;
083.    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
084.    GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
085.    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
086. 
087.    GPIO_InitStruct.Pin = GPIO_PIN_6;
088.    GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
089.    GPIO_InitStruct.Pull = GPIO_NOPULL;
090.    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
091.         
092.        //SPI2 Init
093.        /**SPI1 GPIO Configuration   
094.    PB13     ------> SPI2_SCK
095.    PB14     ------> SPI2_MISO
096.    PB15     ------> SPI2_MOSI
097.    */
098.    GPIO_InitStruct.Pin = GPIO_PIN_13|GPIO_PIN_15;
099.    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
100.    GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
101.    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
102. 
103.    GPIO_InitStruct.Pin = GPIO_PIN_14;
104.    GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
105.    GPIO_InitStruct.Pull = GPIO_NOPULL;
106.    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
107. 
108.  /* USER CODE BEGIN SPI1_MspInit 1 */
109. 
110.  /* USER CODE END SPI1_MspInit 1 */
111.  }
112.}
113. 
114.void HAL_SPI_MspDeInit(SPI_HandleTypeDef* hspi)
115.{
116. 
117.  if(hspi->Instance==SPI1)
118.  {
119.  /* USER CODE BEGIN SPI1_MspDeInit 0 */
120. 
121.  /* USER CODE END SPI1_MspDeInit 0 */
122.    /* Peripheral clock disable */
123.    __SPI1_CLK_DISABLE();
124.        __SPI2_CLK_DISABLE();
125.   
126.    /**SPI1 GPIO Configuration   
127.    PA5     ------> SPI1_SCK
128.    PA6     ------> SPI1_MISO
129.    PA7     ------> SPI1_MOSI
130.    */
131.    HAL_GPIO_DeInit(GPIOA, GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7);
132.        /**SPI1 GPIO Configuration   
133.    PB13     ------> SPI2_SCK
134.    PB14     ------> SPI2_MISO
135.    PB15     ------> SPI2_MOSI
136.        */
137.        HAL_GPIO_DeInit(GPIOB, GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15);
138. 
139.  }


Thanks

Outcomes