STM32L4 - SPI2 clocking issue - c

I am currently working on the STM32L476RG Nucleo board and I am trying to communicate with the SPI2 bus.
It seems that I am sending data with the MOSI pin but I don't have anything on the SCK pin.
Here are my initialisation code and sending data code:
In the main.c:
/Function that initializes the SPI/
void MX_SPI2_Init(void)
{
hspi2.Instance = SPI2;
hspi2.Init.Mode = SPI_MODE_MASTER;
hspi2.Init.Direction = SPI_DIRECTION_2LINES;
hspi2.Init.DataSize = SPI_DATASIZE_8BIT;
hspi2.Init.CLKPolarity = SPI_POLARITY_LOW;
hspi2.Init.CLKPhase = SPI_PHASE_1EDGE;
hspi2.Init.NSS = SPI_NSS_SOFT;
hspi2.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16;
hspi2.Init.FirstBit = SPI_FIRSTBIT_MSB;
hspi2.Init.TIMode = SPI_TIMODE_DISABLED;
hspi2.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED;
hspi2.Init.CRCPolynomial = 7;
hspi2.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;
hspi2.Init.NSSPMode = SPI_NSS_PULSE_DISABLED;
HAL_SPI_Init(&hspi2);
}
/Function that sends data via SPI to the slave/
void ADAS1000_SetRegisterValue(unsigned char regAddress,
unsigned long regVal)
{
unsigned char writeCmd[4] = {0, 0, 0, 0};
writeCmd[0] = 0x80 + regAddress; // Write bit and register address.
writeCmd[1] = (unsigned char)((regVal & 0xFF0000) >> 16);
writeCmd[2] = (unsigned char)((regVal & 0x00FF00) >> 8);
writeCmd[3] = (unsigned char)((regVal & 0x0000FF) >> 0);
HAL_SPI_Transmit(&hspi2, &(writeCmd[0]), (uint16_t) sizeof(writeCmd[0]), 50);
HAL_Delay(500);
HAL_SPI_Transmit(&hspi2, &(writeCmd[1]), (uint16_t) sizeof(writeCmd[1]), 50);
HAL_Delay(500);
HAL_SPI_Transmit(&hspi2, &(writeCmd[2]), (uint16_t) sizeof(writeCmd[2]), 50);
HAL_Delay(500);
HAL_SPI_Transmit(&hspi2, &(writeCmd[3]), (uint16_t) sizeof(writeCmd[3]), 50);
HAL_Delay(500);
}
In the hal_msp.c :
void HAL_SPI_MspInit(SPI_HandleTypeDef* hspi)
{
GPIO_InitTypeDef GPIO_InitStruct;
if(hspi->Instance==SPI2)
{
/* Peripheral clock enable */
__SPI2_CLK_ENABLE();
/**SPI2 GPIO Configuration
PC2 ------> SPI2_MISO
PC3 ------> SPI2_MOSI
PB10 ------> SPI2_SCK
PB12 ------> SPI2_NSS
*/
GPIO_InitStruct.Pin = GPIO_PIN_2|GPIO_PIN_3;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_MEDIUM;
GPIO_InitStruct.Alternate = GPIO_AF5_SPI2;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_10|GPIO_PIN_12;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_MEDIUM;
GPIO_InitStruct.Alternate = GPIO_AF5_SPI2;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}
If you have any ideas or advice, thanks for helping!
EDIT
void MX_GPIO_Init(void)
{
/* GPIO Ports Clock Enable */
__GPIOC_CLK_ENABLE();
__GPIOA_CLK_ENABLE();
__GPIOB_CLK_ENABLE();
}

This might be the problem:
// [...]
hspi2.Init.NSS = SPI_NSS_SOFT;
// [...]
Without checking the definition of your HAL macros, I believe that this configures the SPI not to drive NSS - but to have this done by software. This means that you are responsible to assert the NSS pin manually before putting data into the SPI peripheral.
This in turn may cause the SPI slave side not to respond, which may appear like the SCK signals weren't reaching the slave at all.

Related

No USART communication using hal_Driver

I am using a custom devBoard with a single STM32L071 chip. Using debug mode (System Workbench), I can see the TDR register of USART2, and the data I want to transmit is there, but there is no signal coming from the pins on the board.
There is no hardware issue, I had the board checked.
I found the GPIO configuration online, and the USART configuration is what I need.
My main function:
HAL_Init();
GPIO_Config();
Configure_USART();
char message[] = { 0x41, 0x42, 0x43, 0x44, 0x45, 0x55 };
HAL_UART_Transmit(&g_meterUart, (uint8_t*)message, sizeof(message), 0xFFFF);
GPIO configure
__HAL_RCC_GPIOA_CLK_ENABLE()
;
GPIO_InitTypeDef USART_GPIO_InitStruct;
USART_GPIO_InitStruct.Pull = GPIO_NOPULL;
USART_GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
USART_GPIO_InitStruct.Alternate = LL_GPIO_AF_7;
USART_GPIO_InitStruct.Pin = GPIO_PIN_2;
USART_GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
HAL_GPIO_Init(GPIOA, &USART_GPIO_InitStruct);
USART_GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
USART_GPIO_InitStruct.Pin = GPIO_PIN_3;
HAL_GPIO_Init(GPIOA, &USART_GPIO_InitStruct);
Configuring USART
__USART2_CLK_ENABLE();
g_meterUart.Instance = USART2;
g_meterUart.Init.BaudRate = 9600;
g_meterUart.Init.WordLength = UART_WORDLENGTH_8B;
g_meterUart.Init.StopBits = UART_STOPBITS_1;
g_meterUart.Init.Parity = UART_PARITY_NONE;
g_meterUart.Init.HwFlowCtl = UART_HWCONTROL_NONE;
g_meterUart.Init.Mode = UART_MODE_TX_RX;
g_meterUart.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_DeInit(&g_meterUart) != HAL_OK) {
}
if (HAL_UART_Init(&g_meterUart) != HAL_OK) {
};
Here are a couple suspicious issues I found with a quick review of the STM32L071 datasheet. According to Table 16, page 49:
PA2 is USART2_TX but you have configured GPIO_PIN_2 for GPIO_MODE_INPUT. Do you have the direction of PA2 and PA3 reversed?
The USART2 functions of PA2 and PA3 are selected with Alternate Function 4 but you have specified LL_GPIO_AF_7.

STM32: simple SPI transfer

I am working with a STM32F3DISCOVERY board and I'm trying to dive a bit deeper into the abstractions of the HAL. I made a simple version of a function that transmits data over SPI, sadly it does not work (at least the DAC I'm sending it to does not change state) and I'm not sure what I am missing there. Maybe there's also something in the initialization code that doesn't work with my simple version. I'd be happy for any guidance or references I could check. Thank you!
#include <stm32f3xx_hal.h>
#define PINS_SPI GPIO_PIN_5 | GPIO_PIN_7
#define GPIO_PORT GPIOA
/* This is the simplest function I could come up with to do the transfer but I'm clearly missing something here */
uint8_t SPI_SendReceive(SPI_HandleTypeDef *hspi, uint8_t data) {
/* Loop while DR register in not empty */
while ((hspi->Instance->SR & SPI_FLAG_TXE) == RESET) {
}
/* Send data through the SPI1 peripheral */
hspi->Instance->DR = data;
/* Wait to receive data */
while ((hspi->Instance->SR & SPI_FLAG_RXNE) == RESET) {
}
return hspi->Instance->DR;
}
int main() {
HAL_Init();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_SPI1_CLK_ENABLE();
static SPI_HandleTypeDef spi = {.Instance = SPI1};
spi.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_256;
spi.Init.Direction = SPI_DIRECTION_2LINES;
spi.Init.CLKPhase = SPI_PHASE_1EDGE;
spi.Init.CLKPolarity = SPI_POLARITY_LOW;
spi.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
spi.Init.DataSize = SPI_DATASIZE_8BIT;
spi.Init.FirstBit = SPI_FIRSTBIT_MSB;
spi.Init.NSS = SPI_NSS_HARD_OUTPUT;
spi.Init.TIMode = SPI_TIMODE_DISABLE;
spi.Init.Mode = SPI_MODE_MASTER;
HAL_SPI_Init(&spi);
__HAL_SPI_ENABLE(&spi);
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.Pin = PINS_SPI;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF5_SPI1;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_4;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/* TI 8564 DAC Settings */
uint8_t cmd1 = 0b00010000;
/* DAC output value (16-bit) */
uint16_t cmd23 = 0;
uint8_t cmd2 = cmd23 >> 8;
uint8_t cmd3 = cmd23 & 0xff;
uint8_t command[3] = {cmd1, cmd2, cmd3};
while (true) {
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET);
/* This does not work :( */
SPI_SendReceive(&spi, command[0]);
SPI_SendReceive(&spi, command[1]);
SPI_SendReceive(&spi, command[2]);
/* This works! When commenting in the lines above and commenting this out */
/* HAL_SPI_Transmit(&spi, command, 3, HAL_MAX_DELAY); */
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET);
HAL_Delay(1000);
}
}
Check the contents of HAL_SPI_Init. Most likely this function calls another function which is supposed to do the low-level initialization, and you're responsible to provide this function yourself. To make it more complex, this alleged second function already has a "dummy" weak alias defined, so the toolchain doesn't return any error but just builds a code unable to do anything.

STM32: Receiving SPI data with DMA - always wrong number of bytes in Rx buffer

With an STM32F764 I want to receive 33 bytes from 11 daisy-chained 24 bit ADCs via SPI in read-only mode. The received data looks okay but there are 37 to 39 bytes in the buffer. Also with other byte numbers I receive 4 to 6 bytes more than expected.
Does anyone have an idea why that happens?
HAL_SPI_Receive_DMA is called by a GPIO interrupt from an external ADC when the 33 bytes are ready.
After finishing the DMA the buffer content is processed in Sampling().
Is the external GPIO interrupt handler the correct place for calling HAL_SPI_Receive_DMA?
/*SPI init for external ADCs (only Rx)*/
uint8_t SPI3_Init_ADC(void)
{
hspi3.Instance = SPI3;
hspi3.Init.Mode = SPI_MODE_MASTER;
hspi3.Init.Direction = SPI_DIRECTION_2LINES_RXONLY;
hspi3.Init.DataSize = SPI_DATASIZE_8BIT;
hspi3.Init.CLKPolarity = SPI_POLARITY_LOW;
hspi3.Init.CLKPhase = SPI_PHASE_1EDGE;
hspi3.Init.NSS = SPI_NSS_SOFT;
hspi3.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_4; /*SPI3_CLK = 12.5 MHz*/
hspi3.Init.FirstBit = SPI_FIRSTBIT_MSB;
hspi3.Init.TIMode = SPI_TIMODE_DISABLE;
hspi3.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
hspi3.Init.CRCPolynomial = 7;
hspi3.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;
hspi3.Init.NSSPMode = SPI_NSS_PULSE_DISABLE;
if (HAL_SPI_Init(&hspi3) != HAL_OK)
return ERR;
return OK;
}
/*SPI MSP and DMA Initialization*/
void HAL_SPI_MspInit(SPI_HandleTypeDef* hspi)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/*SPI3: ADCs*/
if(hspi->Instance==SPI3)
{
__HAL_RCC_SPI3_CLK_ENABLE(); /* Peripheral clock enable */
__HAL_RCC_GPIOB_CLK_ENABLE();
/*PB3 --> SPI3_SCK*/
/*PB4 --> SPI3_MISO*/
GPIO_InitStruct.Pin = GPIO_PIN_3|GPIO_PIN_4;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF6_SPI3;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/*DMA 1 / channel 0 / stream 0 transfers data from external ADC to sample registers*/
__HAL_RCC_DMA1_CLK_ENABLE(); /*DMA clock*/
hdma_spi3_rx.Init.Channel = DMA_CHANNEL_0;
hdma_spi3_rx.Init.Direction = DMA_PERIPH_TO_MEMORY; /*from SPI to memory*/
hdma_spi3_rx.Init.PeriphInc = DMA_PINC_DISABLE; /*no increment on SPI side*/
hdma_spi3_rx.Init.MemInc = DMA_MINC_ENABLE; /*increment sample memory address*/
hdma_spi3_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
hdma_spi3_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
hdma_spi3_rx.Init.Mode = DMA_NORMAL;
hdma_spi3_rx.Init.Priority = DMA_PRIORITY_VERY_HIGH;
hdma_spi3_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
hdma_spi3_rx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_HALFFULL;
hdma_spi3_rx.Init.MemBurst = DMA_PBURST_SINGLE;
hdma_spi3_rx.Init.PeriphBurst = DMA_PBURST_SINGLE;
hdma_spi3_rx.Instance = DMA1_Stream0;
HAL_DMA_Init(&hdma_spi3_rx);
HAL_NVIC_SetPriority(DMA1_Stream0_IRQn, 0, 0); /*highest priority for DMA interrupt*/
HAL_NVIC_EnableIRQ(DMA1_Stream0_IRQn); /*enable DMA interrupt*/
__HAL_LINKDMA(hspi, hdmarx, hdma_spi3_rx); /*link DMA1 to SPI3*/
}
/*INTERRUPT HANDLERS*/
/*Data ready interrupt (/ADC_/DRDY) of external ADCs*/
void EXTI9_5_IRQHandler(void)
{
HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_7); /*clear interrupt*/
HAL_SPI_Receive_DMA(&hspi3, SplRxBuff, 33); /*get 33 bytes ADC data*/
}
/*DMA1 stream0 global interrupt (Rx of external ADCs complete)*/
void SPI_DMA_Rx_IRQHandler(void)
{
HAL_DMA_IRQHandler(&hdma_spi3_rx);
Sampling();
}
Screenshot:
The red part is excess data. The ADC delivers actual data here from its unused 12th channel.
I bet as you do not protect the start transmition function call with any synchronisation mechanism (flag, semaphore etc) your interrupt is triggered before other transmit is done. Also the mystery function sampling in the DMA handler may does something strange.

STM32 HAL C QuadSPI Display read and write

I am using QSPI not to connect memory, but a FTDI display.
I used STM32CubeMX and Atollic TrueStudio. I included FreeRTOS, but I'm not using that, yet.
Using QuadSPI, I have trouble to read and write 1, 2 or 4 bytes, where I transmit a 3 byte memory address.
If I try to read an address like this, it times out at HAL_QSPI_Receive, and no signals are generated on the bus, if I configure
s_command.AddressMode = QSPI_ADDRESS_1_LINE;
If I configure
s_command.AddressMode = QSPI_ADDRESS_NONE;
Signals are generated to read a byte, but the address is not send of course.
To send the address and receive bytes afterwards, I send the address in the Alternate bytes. But now the number of bytes can 1 or 2, but not 4, because I will get a time-out again.
My code pieces
uint8_t pData[4];
uint32_t address = REG_ID;
QspiReadData(address, 1, pData);
uint32_t v = 0x12345678;
pData[0] = v >> 24;
pData[1] = (v >> 16) & 0xff;
pData[2] = (v >> 8) & 0xff;
pData[3] = v & 0xff;
QspiWriteData(addr, 4, pData);
uint8_t QspiReadData(uint32_t address, uint32_t size, uint8_t* pData)
{
QSPI_CommandTypeDef s_command;
QSPI_AutoPollingTypeDef s_config;
/* Initialize the read command */
s_command.InstructionMode = QSPI_INSTRUCTION_NONE;
s_command.Instruction = 0;
s_command.AddressMode = QSPI_ADDRESS_1_LINE;
s_command.AddressSize = QSPI_ADDRESS_32_BITS;
s_command.Address = address;
s_command.AlternateByteMode = QSPI_ALTERNATE_BYTES_NONE;
s_command.AlternateBytes = 0;
s_command.AlternateBytesSize = 0;
s_command.DataMode = QSPI_DATA_1_LINE; // QSPI_DATA_4_LINES
s_command.DummyCycles = 0;
s_command.NbData = size;
s_command.DdrMode = QSPI_DDR_MODE_DISABLE;
s_command.DdrHoldHalfCycle = QSPI_DDR_HHC_ANALOG_DELAY;
s_command.SIOOMode = QSPI_SIOO_INST_EVERY_CMD;
/* Configure the command */
printf("HAL_QSPI_Command\n");
if (HAL_QSPI_Command(&hqspi, &s_command, HAL_QPSI_TIMEOUT_DEFAULT_VALUE) != HAL_OK) {
printf("HAL_ERROR\n");
return HAL_ERROR;
}
/* Reception of the data */
printf("HAL_QSPI_Receive\n");
if (HAL_QSPI_Receive(&hqspi, pData, HAL_QPSI_TIMEOUT_DEFAULT_VALUE) != HAL_OK) {
printf("HAL_ERROR\n"); // Timeout after 5000mS
return HAL_ERROR;
}
return HAL_OK;
}
/* QUADSPI init function */
void MX_QUADSPI_Init(void)
{
hqspi.Instance = QUADSPI;
hqspi.Init.ClockPrescaler = 254; /* 4 QSPI Freq= 108 MHz / (1+4) = 21.6 MHz */
hqspi.Init.FifoThreshold = 1;
hqspi.Init.SampleShifting = QSPI_SAMPLE_SHIFTING_NONE;
hqspi.Init.FlashSize = 0;
hqspi.Init.ChipSelectHighTime = QSPI_CS_HIGH_TIME_8_CYCLE;
hqspi.Init.ClockMode = QSPI_CLOCK_MODE_0; // QSPI_CLOCK_MODE_0 Rising edge CPOL=0, QSPI_CLOCK_MODE_3 Falling edge CPOL=1
hqspi.Init.FlashID = QSPI_FLASH_ID_1;
hqspi.Init.DualFlash = QSPI_DUALFLASH_DISABLE;
if (HAL_QSPI_Init(&hqspi) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
}
uint8_t QspiWriteData(uint32_t address, uint32_t size, uint8_t* pData)
{
QSPI_CommandTypeDef s_command;
printf("Ft813QspiReadData8(%ld, %d, pData)\n", address, size);
/* Initialize the read command */
s_command.InstructionMode = QSPI_INSTRUCTION_NONE;
s_command.Instruction = 0;
s_command.AddressMode = QSPI_ADDRESS_NONE;
s_command.AddressSize = QSPI_ADDRESS_24_BITS;
s_command.Address = 0;
s_command.AlternateByteMode = QSPI_ALTERNATE_BYTES_1_LINE;
s_command.AlternateBytes = address;
s_command.AlternateBytesSize = QSPI_ALTERNATE_BYTES_24_BITS;
s_command.DataMode = QSPI_DATA_1_LINE;
s_command.DummyCycles = 0;
s_command.NbData = size;
s_command.DdrMode = QSPI_DDR_MODE_DISABLE;
s_command.DdrHoldHalfCycle = QSPI_DDR_HHC_ANALOG_DELAY;
s_command.SIOOMode = QSPI_SIOO_INST_EVERY_CMD;
/* Configure the command */
printf("HAL_QSPI_Command\n");
if (HAL_QSPI_Command(&hqspi, &s_command, HAL_QPSI_TIMEOUT_DEFAULT_VALUE) != HAL_OK) {
printf("HAL_ERROR\n");
return HAL_ERROR;
}
/* Reception of the data */
printf("HAL_QSPI_Transmit\n");
if (HAL_QSPI_Transmit(&hqspi, pData, HAL_QPSI_TIMEOUT_DEFAULT_VALUE) != HAL_OK) {
printf("HAL_ERROR\n");
return HAL_ERROR;
}
return HAL_OK;
}
What can be the problem?
The GPIO pins were configured like this, in case it helps sombody else:
void HAL_QSPI_MspInit(QSPI_HandleTypeDef* qspiHandle)
{
GPIO_InitTypeDef GPIO_InitStruct;
if(qspiHandle->Instance==QUADSPI)
{
/* USER CODE BEGIN QUADSPI_MspInit 0 */
/* USER CODE END QUADSPI_MspInit 0 */
/* QUADSPI clock enable */
__HAL_RCC_QSPI_CLK_ENABLE();
/**QUADSPI GPIO Configuration
PF6 ------> QUADSPI_BK1_IO3
PF7 ------> QUADSPI_BK1_IO2
PF8 ------> QUADSPI_BK1_IO0
PF9 ------> QUADSPI_BK1_IO1
PF10 ------> QUADSPI_CLK
PB10 ------> QUADSPI_BK1_NCS
*/
GPIO_InitStruct.Pin = QUADSPI_BK1_IO3_Pin|QUADSPI_BK1_IO2_Pin|QUADSPI_CLK_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF9_QUADSPI;
HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);
GPIO_InitStruct.Pin = QUADSPI_BK1_IO0_Pin|QUADSPI_BK1_IO1_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF10_QUADSPI;
HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);
GPIO_InitStruct.Pin = QUADSPI_BK1_NCS_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF9_QUADSPI;
HAL_GPIO_Init(QUADSPI_BK1_NCS_GPIO_Port, &GPIO_InitStruct);
/* QUADSPI DMA Init */
/* QUADSPI Init */
hdma_quadspi.Instance = DMA2_Stream2;
hdma_quadspi.Init.Channel = DMA_CHANNEL_11;
hdma_quadspi.Init.Direction = DMA_PERIPH_TO_MEMORY;
hdma_quadspi.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_quadspi.Init.MemInc = DMA_MINC_ENABLE;
hdma_quadspi.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
hdma_quadspi.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
hdma_quadspi.Init.Mode = DMA_NORMAL;
hdma_quadspi.Init.Priority = DMA_PRIORITY_LOW;
hdma_quadspi.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
if (HAL_DMA_Init(&hdma_quadspi) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
__HAL_LINKDMA(qspiHandle,hdma,hdma_quadspi);
/* QUADSPI interrupt Init */
HAL_NVIC_SetPriority(QUADSPI_IRQn, 5, 0);
HAL_NVIC_EnableIRQ(QUADSPI_IRQn);
/* USER CODE BEGIN QUADSPI_MspInit 1 */
/* USER CODE END QUADSPI_MspInit 1 */
}
}
hqspi.Init.FlashSize = 0; is wrong. QuadSPI module needs to learn the memory of device which will write/read. Change it with 31. It will run.

SPI getting data not accoring to SCK

I have been trying to implement spi on STM32F407 discovery mode in DMA mode. I got the data, but the problem is that, I have 112 bits of data via Pulse Position Modulation, and the frequency of data is 1MHz. Since this info signal is arbitrary, I've configured my board as a slave (although there is no real master) and I make SCK from board itself via PWM. The thing is, that I am receiving data, but I get the whole 112 bits in 42 pulses. I am not sure of how the SPI is triggered by the SCK, since the durrations do not match.
There is configuration of my SPI:
static void MX_SPI1_Init(void)
{
/* SPI1 parameter configuration*/
hspi1.Instance = SPI1;
hspi1.Init.Mode = SPI_MODE_SLAVE;
hspi1.Init.Direction = SPI_DIRECTION_2LINES;
hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
hspi1.Init.CLKPolarity = SPI_POLARITY_HIGH;
hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
hspi1.Init.NSS = SPI_NSS_HARD_INPUT;
hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
hspi1.Init.BaudRatePrescaler=SPI_BAUDRATEPRESCALER_4;
hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
hspi1.Init.CRCPolynomial = 7;
if (HAL_SPI_Init(&hspi1) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}}
My msp.c file
void HAL_SPI_MspInit(SPI_HandleTypeDef* hspi)
{
GPIO_InitTypeDef GPIO_InitStruct;
if(hspi->Instance==SPI1)
{
/* USER CODE BEGIN SPI1_MspInit 0 */
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
/* USER CODE END SPI1_MspInit 0 */
/* Peripheral clock enable */
__HAL_RCC_SPI1_CLK_ENABLE();
/**SPI1 GPIO Configuration
PA4 ------> SPI1_NSS
PA6 ------> SPI1_MISO
PA7 ------> SPI1_MOSI
PB3 ------> SPI1_SCK
*/
GPIO_InitStruct.Pin = GPIO_PIN_4|GPIO_PIN_6|GPIO_PIN_7;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF5_SPI1;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_3;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF5_SPI1;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/* SPI1 DMA Init */
/* SPI1_RX Init */
hdma_spi1_rx.Instance = DMA2_Stream0;
hdma_spi1_rx.Init.Channel = DMA_CHANNEL_3;
hdma_spi1_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
hdma_spi1_rx.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_spi1_rx.Init.MemInc = DMA_MINC_ENABLE;
hdma_spi1_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
hdma_spi1_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
hdma_spi1_rx.Init.Mode = DMA_NORMAL;
hdma_spi1_rx.Init.Priority = DMA_PRIORITY_LOW;
hdma_spi1_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
if (HAL_DMA_Init(&hdma_spi1_rx) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
__HAL_LINKDMA(hspi,hdmarx,hdma_spi1_rx);
/* SPI1 interrupt Init */
HAL_NVIC_SetPriority(SPI1_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(SPI1_IRQn);
/* USER CODE BEGIN SPI1_MspInit 1 */
/* USER CODE END SPI1_MspInit 1 */
}}
I start SPI via:
HAL_SPI_Receive_DMA(&hspi1, data, 14); //data is array uint8_t
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET); //NSS out which is shortened with real NSS of Slave
HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_1); /clock
After I got the data I use this in SPI handler:
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET);
HAL_TIM_PWM_Stop(&htim3, TIM_CHANNEL_1);
In memory I got all of the 112 (14 bytes)
Data
And there is the oscilogram
Oscilogram
Any ideas?
I have used CubeMX as a base of my code

Resources