Author Topic: [SOLVED] STM32F4 Hal: SPI problems: SPI sets itself to slave mode  (Read 25483 times)

0 Members and 1 Guest are viewing this topic.

Offline BurnedResistorTopic starter

  • Regular Contributor
  • *
  • Posts: 192
  • Country: at
Re: STM32F4 Hal: SPI problems: SPI sets itself to slave mode (does not enable?)
« Reply #25 on: December 01, 2016, 11:00:19 am »
I noticed you had to set the SSM bit for MSTR and SPE to stay engaged. Makes sense if you think about it.
Code: [Select]
  __HAL_RCC_SPI2_FORCE_RESET();
  __NOP();
  __HAL_RCC_SPI2_RELEASE_RESET();
  SPI2->CR1 |= (4<<SPI_CR1_BR_Pos);
  SPI2->CR1 |= SPI_CR1_SSM;           // Software chip select
  SPI2->CR1 |= SPI_CR1_SSI;
  SPI2->CR1 |= SPI_CR1_MSTR | SPI_CR1_SPE;
Btw: this is all init required for master mode.


Oh wow! That did it :)

Thank you so much!

Not noted in the cube mx anywhere, what a great piece of software :(


Thank you all for your help!

For future reference, I will attach the final working code :)


« Last Edit: December 01, 2016, 11:23:08 am by BurnedResistor »
 

Offline Jeroen3

  • Super Contributor
  • ***
  • Posts: 4078
  • Country: nl
  • Embedded Engineer
    • jeroen3.nl
Re: STM32F4 Hal: SPI problems: SPI sets itself to slave mode (does not enable?)
« Reply #26 on: December 02, 2016, 01:40:52 pm »
I try to avoid hal and cube as much as possible. It is included when starting, but converted to save ROM and complexity.
Only the headers stay inluded for the definitions, and the rcc. Since that is usually working fine, and significantly complicated.
 

Offline BurnedResistorTopic starter

  • Regular Contributor
  • *
  • Posts: 192
  • Country: at
Re: STM32F4 Hal: SPI problems: SPI sets itself to slave mode (does not enable?)
« Reply #27 on: December 03, 2016, 12:12:38 pm »
I try to avoid hal and cube as much as possible. It is included when starting, but converted to save ROM and complexity.
Only the headers stay inluded for the definitions, and the rcc. Since that is usually working fine, and significantly complicated.

I am planning on implementing the project with the cube hal, to gain an understanding of what the exact requirements for me are, and then write my own HAL which perfecty meets those requirements. I tried going straight to that, but this is my first time developing such a big project and on this platform so I did not get far!

Interestingly I did not see the whole SSM bit business anywhere in the datasheet nor reference manual!
 Fun times....

Thank you everybody for helping!
 

Offline Jeroen3

  • Super Contributor
  • ***
  • Posts: 4078
  • Country: nl
  • Embedded Engineer
    • jeroen3.nl
Re: STM32F4 Hal: SPI problems: SPI sets itself to slave mode (does not enable?)
« Reply #28 on: December 03, 2016, 01:08:30 pm »
I am planning on implementing the project with the cube hal, to gain an understanding of what the exact requirements for me are, and then write my own HAL which perfecty meets those requirements. I tried going straight to that, but this is my first time developing such a big project and on this platform so I did not get far!

Not quite true.
Quote
Configuring the SPI in master mode
In the master configuration, the serial clock is generated on the SCK pin.
Procedure
1. Select the BR[2:0] bits to define the serial clock baud rate (see SPI_CR1 register).
2. Select the CPOL and CPHA bits to define one of the four relationships between the
data transfer and the serial clock (see Figure 239).
3. Set the DFF bit to define 8- or 16-bit data frame format
4. Configure the LSBFIRST bit in the SPI_CR1 register to define the frame format.
5. If the NSS pin is required in input mode, in hardware mode, connect the NSS pin to a high-level signal during the complete byte transmit sequence. In NSS software mode, set the SSM and SSI bits in the SPI_CR1 register. If the NSS pin is required in output mode, the SSOE bit only should be set.
6. The MSTR and SPE bits must be set (they remain set only if the NSS pin is connected to a high-level signal).

These things make mcu development hell sometimes. There is this peripheral that only works if you set some bit in specific way, described only at one place in the middle of the text. Which you didn't read since you skipped to the registers immediately.  :(

Goodluck!
 

Offline Geryboy666

  • Newbie
  • Posts: 1
  • Country: de
Re: [SOLVED] STM32F4 Hal: SPI problems: SPI sets itself to slave mode
« Reply #29 on: March 14, 2017, 12:30:39 pm »
I had the same problem, STM32F411RE, Gnu Arm Eclipse HAL Lib 1.4.xx

However the problem wasn't enabling any Bits in the Control Registers...

1st I got a Timeout Error because my Baudratescaler was to small, I used 2 and got Timeouts, then I used 64 which was fine.

In the inital post his MISO PIN was declared Input, however all 3 PINS, MISO, MOSI, SCK need to be AF_PP in MODE, and AF5_SPIx in Alternate.

then use NSS as Software, and declare another random GPIO PIN as NSS and select the Slave via your own Code.


Code: [Select]
//GLOBAL VAR
SPI_HandleTypeDef SPI_Handle; // global fuer ISR
int
main(int argc, char* argv[])
{
// CLKs Enable
__HAL_RCC_SPI1_CLK_ENABLE(); // MUSS an sein, ist richtig
__HAL_RCC_GPIOA_CLK_ENABLE();
// SPI Config start
SPI_Handle.Instance = SPI1; // D13 bis D11
SPI_Handle.Init.Mode = SPI_MODE_MASTER;
SPI_Handle.Init.Direction = SPI_DIRECTION_2LINES; // transmit and receive
SPI_Handle.Init.DataSize = SPI_DATASIZE_8BIT; // 8 bit Packete
SPI_Handle.Init.CLKPolarity = SPI_POLARITY_LOW; // idle @ low state
SPI_Handle.Init.CLKPhase =SPI_PHASE_1EDGE; // Phase = 0, rising edge = sample
SPI_Handle.Init.NSS = SPI_NSS_SOFT;
SPI_Handle.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_64;
SPI_Handle.Init.FirstBit = SPI_FIRSTBIT_MSB;
SPI_Handle.Init.TIMode = SPI_TIMODE_DISABLE; // Motorola standard verwenden
SPI_Handle.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
SPI_Handle.Init.CRCPolynomial =0;
// SPI Config end
// SPI Init
if (HAL_SPI_Init(&SPI_Handle)!= HAL_OK)
{
printf("Fehler bei SPI Init\n");
}
// Interrupt enable
HAL_NVIC_SetPriority(SPI1_IRQn, 2,1);
HAL_NVIC_EnableIRQ(SPI1_IRQn);
// GPIO Config start
// CLK,MISO,MOSI

GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.Pin = GPIO_PIN_9; // D8
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

GPIO_InitStruct.Pin =  GPIO_PIN_5| GPIO_PIN_6 |GPIO_PIN_7; // D13 = CLK, A2 = NSS , D11 = MOSI, D12 = MISO
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; // Push Pull
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Alternate = GPIO_AF5_SPI1;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM;
//GPIO Init
HAL_GPIO_Init(GPIOA,&GPIO_InitStruct);

//var

const uint32_t timeout = 100;
const uint16_t SIZE = 3; // Puffergroeße = 1 Byte
double temp;
HAL_StatusTypeDef HAL_Status; // 0 = ok, 1 = Error, 2 = Busy, 3 = timeout
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_9, GPIO_PIN_SET); // Slave sleep

  while (1)
    {

  uint8_t transmit[3] = {0b00000110, 0b00000000, 0b00000000};
  uint8_t receive [3] = {0,0,0};
  HAL_Delay(2000);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_9, GPIO_PIN_RESET); // Slave wake up

if( (HAL_Status = HAL_SPI_TransmitReceive(&SPI_Handle,&transmit,&receive,SIZE,timeout)) != HAL_OK)
{
printf("1st TransmitReceive Error: %d\n", HAL_Status);
}

HAL_GPIO_WritePin(GPIOA, GPIO_PIN_9, GPIO_PIN_SET); // Slave sleep
  //temp = (receive2 <<8) | receive3; // 12 BIT ADC Wert zusammenfuegen
  printf("Receive: %d\t%d\n", receive[1],  receive[2] ); // debug
  printf("Transmit: %d\t%d\t %d\n", transmit[1],transmit[2],transmit[0] ); // debug
  printf("Temp = %f\n", temp); // debug
  printf("SIZE = %d\n", SIZE);
  //temp = (temp *500.0)/4096; // 5 V Ref, 1°C pro 10mV => 500/2^12
  //printf("Temp = %f\n", temp);
    }
}

this codes makes the nucleo master, and uses the MCP3208 as Slave, which reads out a Temp sensor on Channel 0. The 1st Transfer Byte is the setup, the 2nd is the channel select, the 3rd is 0 to give the IC enough clocks. The 2nd and 3rd Receive Bytes are the result.
« Last Edit: March 14, 2017, 12:34:45 pm by Geryboy666 »
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf