AnsweredAssumed Answered

STM32F401RET6 - Nucleo Board - SPI Problems

Question asked by price.kyle on Oct 14, 2015
Latest reply on Oct 14, 2015 by waclawek.jan
I have been working on setting up and trying to test the SPI output from the Nucleo-F401RE.  I am currently using a logic analyzer to view the data and clock lines, though I have not yet been able to see any change on either line...

Here is what I have so far (I am very new to STM and the HAL libraries...):

01.#include "stm32f4xx.h"
02.#include "NONPRECISIONDELAY.h"
03. 
04.int main(void)
05.{
06.    RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN;
07.    RCC->AHB1ENR |= RCC_AHB1ENR_GPIOBEN;
08.    RCC->APB1ENR |= RCC_APB1ENR_SPI2EN;
09. 
10.    // HAL GPIO - Onboard LED init
11.    GPIO_InitTypeDef ledInit;
12.    ledInit.Pin     = GPIO_PIN_5;
13.    ledInit.Mode    = GPIO_MODE_OUTPUT_PP;
14.    ledInit.Pull    = GPIO_NOPULL;
15.    ledInit.Speed   = GPIO_SPEED_HIGH;
16.    HAL_GPIO_Init(GPIOA, &ledInit);
17.    /* END GPIO INIT */
18. 
19.    // CS
20.    GPIO_InitTypeDef spiOneGpioInit_t;
21.    spiOneGpioInit_t.Pin        = GPIO_PIN_12;
22.    spiOneGpioInit_t.Mode       = GPIO_MODE_AF_PP;
23.    spiOneGpioInit_t.Pull       = GPIO_PULLUP;
24.    spiOneGpioInit_t.Speed      = GPIO_SPEED_LOW;
25.    spiOneGpioInit_t.Alternate  = GPIO_AF5_SPI2;
26.    HAL_GPIO_Init(GPIOB, &spiOneGpioInit_t);
27. 
28.    // SPI 2
29.    spiOneGpioInit_t.Pin        = GPIO_PIN_15 | GPIO_PIN_14 | GPIO_PIN_13;
30.    spiOneGpioInit_t.Mode       = GPIO_MODE_AF_PP;
31.    spiOneGpioInit_t.Pull       = GPIO_PULLDOWN;
32.    spiOneGpioInit_t.Speed      = GPIO_SPEED_LOW;
33.    spiOneGpioInit_t.Alternate  = GPIO_AF5_SPI2;
34.    HAL_GPIO_Init(GPIOB, &spiOneGpioInit_t);
35. 
36.    // SPI Handler
37.    SPI_HandleTypeDef hspi;
38.    hspi.Instance               = SPI2;
39.    hspi.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_32;
40.    hspi.Init.Direction         = SPI_DIRECTION_2LINES;
41.    hspi.Init.CLKPhase          = SPI_PHASE_1EDGE;
42.    hspi.Init.CLKPolarity       = SPI_POLARITY_LOW;
43.    hspi.Init.DataSize          = SPI_DATASIZE_8BIT;
44.    hspi.Init.FirstBit          = SPI_FIRSTBIT_MSB;
45.    hspi.Init.TIMode            = SPI_TIMODE_DISABLED;
46.    hspi.Init.CRCCalculation    = SPI_CRCCALCULATION_DISABLED;
47.    hspi.Init.NSS               = SPI_NSS_HARD_OUTPUT;
48. 
49. 
50.    if (HAL_SPI_Init(&hspi) != HAL_OK)
51.    {
52.        HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);  // On board LED for indication
53.        return 1;
54.    }
55. 
56.    uint8_t data[] = {0xff};
57. 
58.    while(1)
59.    {
60.        HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET);   // On board LED for indication
61.        delayMs(500);
62.        HAL_SPI_Transmit(&hspi, data, 1, 10);
63.        HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET);     // On board LED for indication
64.        delayMs(500);
65.    }
66.}

Outcomes