cancel
Showing results for 
Search instead for 
Did you mean: 

Nucleo STM32F401 testing HAL SPI driver

fabiocapozzi9
Associate
Posted on March 04, 2016 at 23:47

Hi, i wrote a simple program in Keil uVision to test the HAL SPI driver

The logic analyzer shows that there is clock activity as expected but the MOSI ouput stay low all time even with pull-up enabled (see attached screenshot)  Surely i made some mistake somewhere.  

Here is the listing. Can you help ?

#define USE_HAL_DRIVER

#include <stm32f4xx.h>

int main () {

uint8_t byte;

SPI_HandleTypeDef hspi;

GPIO_InitTypeDef GPIO_InitTypeDefStruct;

__GPIOA_CLK_ENABLE();

__SPI1_CLK_ENABLE();

 hspi.Instance = SPI1;

 hspi.Init.Mode=SPI_MODE_MASTER;

 hspi.Init.Direction=SPI_DIRECTION_2LINES;

 hspi.Init.DataSize=SPI_DATASIZE_8BIT;

 hspi.Init.CLKPolarity=SPI_POLARITY_HIGH;

 hspi.Init.CLKPhase=SPI_PHASE_2EDGE;

 hspi.Init.NSS=SPI_NSS_SOFT;

 hspi.Init.BaudRatePrescaler=SPI_BAUDRATEPRESCALER_16;

 hspi.Init.FirstBit=SPI_FIRSTBIT_MSB;

 hspi.Init.TIMode=SPI_TIMODE_DISABLED;

 hspi.Init.CRCCalculation=SPI_CRCCALCULATION_DISABLED;

 hspi.Init.CRCPolynomial=0;

  HAL_SPI_MspInit(&hspi);

 HAL_SPI_Init(&hspi);

    GPIO_InitTypeDefStruct.Pin = GPIO_PIN_5 | GPIO_PIN_7 | GPIO_PIN_6;

    GPIO_InitTypeDefStruct.Mode = GPIO_MODE_AF_PP;

    GPIO_InitTypeDefStruct.Speed = GPIO_SPEED_HIGH;

    GPIO_InitTypeDefStruct.Pull = GPIO_PULLUP;

    GPIO_InitTypeDefStruct.Alternate = GPIO_AF5_SPI1;

    HAL_GPIO_Init(GPIOA, &GPIO_InitTypeDefStruct);

 

    GPIO_InitTypeDefStruct.Pin = GPIO_PIN_4;

    GPIO_InitTypeDefStruct.Mode = GPIO_MODE_OUTPUT_PP;

    GPIO_InitTypeDefStruct.Speed = GPIO_SPEED_HIGH;

    GPIO_InitTypeDefStruct.Pull = GPIO_PULLUP;

    HAL_GPIO_Init(GPIOA, &GPIO_InitTypeDefStruct);

byte = 0xAA;   // data to be transmitted  0b10101010 

while(1) {

HAL_GPIO_WritePin(GPIOA,GPIO_PIN_4,GPIO_PIN_RESET);   // assert slave select low

HAL_SPI_Transmit(&hspi,&byte,1,4);

HAL_GPIO_WritePin(GPIOA,GPIO_PIN_4,GPIO_PIN_SET);     // assert slave select high

}

}

0 REPLIES 0