stm32 SPI receive only mode need a transmit? - stm32

sorry in advance, i am new at this kind of projects.
I tried to implement a connection to an ADC (LTC1609) via SPI. I just want to get the data from the ADC as quick as possible. The ADC has just a output-line, so i chose the "read only mode". But i also tried to use the HAL_SPI_TransmitReceive funktion because i remembered that the spi exchange starts with writeing in the transmit register (at least with the HCS12).
The ADC needs also some additional signals changes between pulling down the NSS and starting the spi connection so i put the NSS in software mode.
The Cofiguration looks like this:
static void MX_SPI1_Init(void)
{
/* SPI1 parameter configuration*/
hspi1.Instance = SPI1;
hspi1.Init.Mode = SPI_MODE_MASTER;
hspi1.Init.Direction = SPI_DIRECTION_2LINES_RXONLY;
hspi1.Init.DataSize = SPI_DATASIZE_16BIT;
hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
hspi1.Init.NSS = SPI_NSS_SOFT;
hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_4;
hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
hspi1.Init.CRCPolynomial = 7;
hspi1.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;
hspi1.Init.NSSPMode = SPI_NSS_PULSE_DISABLE;
if (HAL_SPI_Init(&hspi1) != HAL_OK)
{
Error_Handler();
}
}
I write a funktion to read from ADC and store the data.
void LTC1609_ADU_Read(uint8_t *data)
{
uint8_t buffer_rx[2];
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, 0); // NSS low (Pin PA4) Start
HAL_GPIO_WritePin(GPIOG, GPIO_PIN_6, 0); // ADU R/NC low (Pin PG6) Start conversion
delay_hns (1); // wait 1us (10 x 100ns)
HAL_GPIO_WritePin(GPIOG, GPIO_PIN_6, 1); // ADU R/NC high (Pin PG6)
if(HAL_GPIO_ReadPin(GPIOG, GPIO_PIN_8) == 1) // /BUSY is high? -> start SPI
{
if(HAL_SPI_Receive(&hspi1, buffer_rx,2,10)!= HAL_OK);
{
SPI_error = HAL_SPI_GetError(&hspi1); // get the SPI error
Error_Handler(); // stop in error
}
}
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, 1); // Set ADU /CS high (Pin PA4) fin
*data = buffer_rx[1]; // store received data
}
In the while(1) i called it like this with a delay of 100 ms for testing:
LTC1609_ADU_Read(&buffer_A_rx);
Problem:
The uC (STM32L4R5ZIT6P) dont start the SPI clk to trigger out the data from ADC. Also the Clock polarity is somtimes low (like i want) and sometimes high [could also be a messuring problem, i messure it with a sheep logic analyser 24Mhz 8CH from amazon].
If i start the programm, it runs into the error handler and the errorcode is 0x20. I found this explaining:
HAL_SPI_ERROR_FLAG 0x00000020U /*!< Flag: RXNE,TXE, BSY */
Can anyone give me a tip on what could be the reason? Did iam doing something wrong or could the hardware be damaged? Thanks in advance!
screenshot signals without clk

One problem that I see in your code is that you check the BUSY pin immediately after setting R/C high and abort the transaction if BUSY is low.
According to the datasheet, BUSY line may be low for up to 3 us (t3).
Try waiting for the busy pin to go high, for example by adding a loop as shown below.
void LTC1609_ADU_Read(uint8_t *data)
{
uint8_t buffer_rx[2];
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, 0); // NSS low (Pin PA4) Start
HAL_GPIO_WritePin(GPIOG, GPIO_PIN_6, 0); // ADU R/NC low (Pin PG6) Start conversion
delay_hns(1); // wait 1us (10 x 100ns)
HAL_GPIO_WritePin(GPIOG, GPIO_PIN_6, 1); // ADU R/NC high (Pin PG6)
uint8_t retries = 0;
do {
delay_hns(1);
} while (HAL_GPIO_ReadPin(GPIOG, GPIO_PIN_8) == 0 && ++retries < 5);
if (HAL_GPIO_ReadPin(GPIOG, GPIO_PIN_8) == 1) // /BUSY is high? -> start SPI
{
if (HAL_SPI_Receive(&hspi1, buffer_rx, 2, 10) != HAL_OK)
{
SPI_error = HAL_SPI_GetError(&hspi1); // get the SPI error
Error_Handler(); // stop in error
}
}
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, 1); // Set ADU /CS high (Pin PA4) fin
*data = buffer_rx[1]; // store received data
}
Also, there is a stray ; in your code, on this line:
if(HAL_SPI_Receive(&hspi1, buffer_rx,2,10)!= HAL_OK);

Related

STM32 Multi-Channel ADC. Unexpected behaviour when unpopulated

I have added ADC functionality to my Nucleo-F446RE development board. 4 channels, DMA enabled, scan and continuous conversion mode enabled, DMA continuous requests enabled, varying sample time per channel. I'll post code at the bottom of this post (all HAL, all done in STM32CubeMX).
I have found some strange behaviour when the channels are unpopulated (e.g., analog channel pin left open). All four channels will hover at around 0.9V with no channels connected. If I add a 3.3V source to channel 0, it'll show 3.3V, but CH1 will show 2.5V, CH2 will show 1.9V, CH3 1.6V. A waterfall effect. That waterfall effect is the same if I move the 3.3V source to CH1 and leave the rest unpopulated, and the waterfall effect loops back around to CH0.
If I give each channel their own source, they'll all show them correctly, but when unpopulated the channels are influenced by the populated channel. Why is this? I have found some sources saying that this is because of the sample+hold capacitor, and the solution is to correct the sampling times, but I have played a lot with the times going from very fast to as slow as possible sampling (I am only interested in sampling the data at 1kHz, but the ADC conversion seems to be, at a minimum, a magnitude above this), but it doesn't make a change. I wondered if changing the analog channel pin configuration to pull-down would help, but again no change.
I am hoping that this isn't something to be too concerned about, as the channels appear correct when populated, but perhaps there is some background influence that I am not seeing even when populated that I want to avoid. I am certain I haven't optimised my circuit, so any advice on that would also be great. There are lots of tutorials and examples online for STM32 ADC DMA with a single channel, but not so many with multi-channel. I also don't find the STM32 provided examples to be too helpful and often seem very inefficient.
ADC definitions
(main clock 180MHz, APB2 prescaler 2 = 90MHz, although I have also dropped it to a prescaler of 16 (11.25MHz) which didn't help)
/** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
*/
hadc1.Instance = ADC1;
hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV8;
hadc1.Init.Resolution = ADC_RESOLUTION_12B;
hadc1.Init.ScanConvMode = ENABLE;
hadc1.Init.ContinuousConvMode = ENABLE;
hadc1.Init.DiscontinuousConvMode = DISABLE;
hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc1.Init.NbrOfConversion = 4;
hadc1.Init.DMAContinuousRequests = ENABLE;
hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
if (HAL_ADC_Init(&hadc1) != HAL_OK)
{
Error_Handler();
}
/** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
*/
sConfig.Channel = ADC_CHANNEL_0;
sConfig.Rank = 1;
sConfig.SamplingTime = ADC_SAMPLETIME_480CYCLES;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
/** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
*/
sConfig.Channel = ADC_CHANNEL_1;
sConfig.Rank = 2;
sConfig.SamplingTime = ADC_SAMPLETIME_112CYCLES;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
/** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
*/
sConfig.Channel = ADC_CHANNEL_4;
sConfig.Rank = 3;
sConfig.SamplingTime = ADC_SAMPLETIME_56CYCLES;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
/** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
*/
sConfig.Channel = ADC_CHANNEL_8;
sConfig.Rank = 4;
sConfig.SamplingTime = ADC_SAMPLETIME_15CYCLES;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
DMA definition
__HAL_RCC_ADC1_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
/**ADC1 GPIO Configuration
PA0-WKUP ------> ADC1_IN0
PA1 ------> ADC1_IN1
PA4 ------> ADC1_IN4
PB0 ------> ADC1_IN8
*/
GPIO_InitStruct.Pin = analog1_Pin|analog2_Pin|analog3_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.Pin = analog4_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(analog4_GPIO_Port, &GPIO_InitStruct);
/* ADC1 DMA Init */
/* ADC1 Init */
hdma_adc1.Instance = DMA2_Stream0;
hdma_adc1.Init.Channel = DMA_CHANNEL_0;
hdma_adc1.Init.Direction = DMA_PERIPH_TO_MEMORY;
hdma_adc1.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_adc1.Init.MemInc = DMA_MINC_ENABLE;
hdma_adc1.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;
hdma_adc1.Init.MemDataAlignment = DMA_MDATAALIGN_WORD;
hdma_adc1.Init.Mode = DMA_NORMAL;
hdma_adc1.Init.Priority = DMA_PRIORITY_MEDIUM;
hdma_adc1.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
if (HAL_DMA_Init(&hdma_adc1) != HAL_OK)
{
Error_Handler();
}
__HAL_LINKDMA(adcHandle,DMA_Handle,hdma_adc1);
/* ADC1 interrupt Init */
HAL_NVIC_SetPriority(ADC_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(ADC_IRQn);
Analog read code
(analog_scale is called once per channel every 1kHz)
#include "dma.h"
#include "adc.h"
#include "analog.h"
volatile uint32_t analogBuffer[4];
void analog_init()
{
HAL_ADC_Start_DMA(&hadc1, (uint32_t *)&analogBuffer, 4);
}
uint16_t analog_scale(char ch)
{
return (uint16_t)(((analogBuffer[ch] * 3.3) / 4096.0) * 1000.0);
}
void HAL_ADC_ConvHalfCpltCallback(ADC_HandleTypeDef* hadc)
{
}
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
{
HAL_ADC_Start_DMA(&hadc1, (uint32_t *)&analogBuffer, 4);
HAL_GPIO_TogglePin(test4_GPIO_Port, test4_Pin);
}
That's not a Software issue, it's normal hardware behavior.
If ADC pins are floating, they "gather" stray voltages, e.g. from adjacent Sample and Hold Capacitors, from the Voltage Reference or any voltage that is induced in the traces on the PCB or attached cables.
The "Waterfall" effect you see, is simply your input voltage on Channel 0 or 1 coupling through the sample and hold capacitors and resistors from one channel to the next, transferred by the multiplexers parasitic capacitances: a small amount of charge is transferred from one voltage path to the next while switching through the channels, and this charge has no path to flow when the connections are open, except through the ADC, resulting in a pseudo-voltage reading.
To prevent this, tie all unused channels to ground, using appropriate pull down resistors (10 kOhm should be OK …), or if you want a software solution: multiply all unused channels with 0.

problem with using SPI with DMA on STM32F1

I have a problem with triggering NSS pin, when transmitting SPI with DMA.
I use a CubeMX to generate whole core of project.
Time before triggering NSS to low, and sending data(also between end of transmission, and NSS to high) is too long. How can i make this times shorter?
I tried to use
HAL_DMA_PollForTransfer(&hdma_spi1_tx, HAL_DMA_FULL_TRANSFER, HAL_MAX_DELAY);
for detecting end of SPI DMA transmission but once it worked, and later when i changed something in CubeIDE it completely stoped working whole program...
int dmabusy = 0;
while (1)
{
uint8_t testing[] = {5, 10, 15, 20, 25, 30};
//HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_SET);
dmaBusy = 1;
GPIOB->BSRR = GPIO_BSRR_BS12;
HAL_SPI_Transmit_DMA(&hspi1, testing, 6);
while(dmaBusy == 1);
GPIOB->BSRR = GPIO_BSRR_BR12;
//HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_RESET);
for(int i=0; i<80000; i++){ // OPOZNIACZ START
asm("NOP");
} // OPOZNIACZ STOP
}
/* USER CODE END 3 */
}
void HAL_SPI_TxCpltCallback (SPI_HandleTypeDef * hspi){
dmaBusy=0;
}

STM32F4 SPI interrupts stop firing with FreeRTOS

I'm trying to make an SPI communication between a F410 MCU and a RPi using SPI.
I post below the code that currently works (without FreeRTOS usage):
main.c
volatile int tx_done = 0;
volatile int rx_done = 0;
void HAL_SPI_TxCpltCallback(SPI_HandleTypeDef *hspi)
{
tx_done = 1;
}
void HAL_SPI_RxCpltCallback(SPI_HandleTypeDef *hspi)
{
rx_done = 1;
}
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_SPI5_Init();
MX_USART2_UART_Init();
const uint8_t BUF_SIZE = 16 * sizeof(uint8_t);
uint8_t buf[16];
// For UART debug
uint8_t dbg_buffer[64];
while (1) {
memset(buf, 0, BUF_SIZE);
HAL_StatusTypeDef ret = HAL_SPI_Receive_IT(&hspi5, (uint8_t*)&buf, BUF_SIZE);
while (rx_done == 0) {};
rx_done = 0;
sprintf((char*) dbg_buffer, "%d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d]\r\n",
buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6],
buf[7], buf[8], buf[9], buf[10], buf[11], buf[12],
buf[13], buf[14], buf[15]);
HAL_UART_Transmit(&huart2, dbg_buffer, strlen((char const*) dbg_buffer), 50);
HAL_SPI_Transmit_IT(&hspi5, (uint8_t*) &buf, BUF_SIZE);
while (tx_done == 0) {};
tx_done = 0;
}
}
stm32f4xx_it.c
/**
* #brief This function handles TIM1 trigger and commutation interrupts and TIM11 global interrupt.
*/
void TIM1_TRG_COM_TIM11_IRQHandler(void)
{
HAL_TIM_IRQHandler(&htim11);
}
/**
* #brief This function handles SPI5 global interrupt.
*/
void SPI5_IRQHandler(void)
{
HAL_SPI_IRQHandler(&hspi5);
}
spi.c
/* SPI5 init function */
void MX_SPI5_Init(void)
{
hspi5.Instance = SPI5;
hspi5.Init.Mode = SPI_MODE_SLAVE;
hspi5.Init.Direction = SPI_DIRECTION_2LINES;
hspi5.Init.DataSize = SPI_DATASIZE_8BIT;
hspi5.Init.CLKPolarity = SPI_POLARITY_LOW;
hspi5.Init.CLKPhase = SPI_PHASE_1EDGE;
hspi5.Init.NSS = SPI_NSS_HARD_INPUT;
hspi5.Init.FirstBit = SPI_FIRSTBIT_MSB;
hspi5.Init.TIMode = SPI_TIMODE_DISABLE;
hspi5.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
hspi5.Init.CRCPolynomial = 15;
if (HAL_SPI_Init(&hspi5) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
}
void HAL_SPI_MspInit(SPI_HandleTypeDef* spiHandle)
{
[...]
HAL_NVIC_SetPriority(SPI5_IRQn, 5, 0);
HAL_NVIC_EnableIRQ(SPI5_IRQn);
}
This working fine with my test code on the other (raspberry pi) side, sending a SPI frame every second, waiting 100ms and reading the answer from the F410.
Now, when I activate FreeRTOS, I move the while(1) loop content to a task, and creates it with
BaseType_t task1 = xTaskCreate(task_1, "task_1", 512, NULL, tskIDLE_PRIORITY, &xHandle);
and osKernelStart(). I also use the TIM11 as Timebase Source (under SYS tab on CubeMX as advised by the software itself)
Then I have the following behavior: If I place breakpoints inside both Tx/Rx SPI interrupt, I found that a couple (3-4 ?) of them are fired, then never again. If I stop my code I see I'm stucked in the
while (rx_done == 0) {};
loop, confirming that I don't get SPI RX interrupts anymore whereas there is still frame coming on the SPI bus.
To dig a little into that theory, I made another test with this in my task:
while(1) {
memset(buf, 0, 16);
HAL_StatusTypeDef ret = HAL_SPI_Receive_IT(&hspi5, (uint8_t*)&buf, 16);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET);
// Wait for RX interrupt, task is suspended to give processing time to (incoming) others tasks
vTaskSuspend(NULL);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET);
}
and in my Rx interrupt, I simply call
xTaskResumeFromISR(task1Handle);
With this code, the first packet sent is read correctly by the STM32F4, and task print it on USART2 and suspend itself again. From then, (checked with a breakpoint inside), the Rx interrupt is never called again, so the task resume inside neither, and my code is frozen...
It really looks like there is a messing between FreeRTOS and STM32 HAL SPI/interrupt handling ?
Any help will be gladly accepted !
Do you call NVIC_PriorityGroupConfig( NVIC_PriorityGroup_4 ); as described in the red text on this page: https://www.freertos.org/RTOS-Cortex-M3-M4.html ?
If you are using an STM32 with the STM32 driver library then ensure all the priority bits are assigned to be preempt priority bits by calling NVIC_PriorityGroupConfig( NVIC_PriorityGroup_4 ); before the RTOS is started.

STM32F4 SPI1 working, SPI5 not working?

I got a STM32 Nucleo-F410RB development board and was able to get my external DAC working with SPI1, both with busy-wait and with DMA. I then designed my own custom PCB, built it and was able to flash it. During the design phase I switched from using SPI1 to SPI5 because I needed the SPI1 pins for other functions. But I couldn't get SPI5 to work in my new design - no signal on the SCK and MOSI pins. When I changed my code to use SPI1, I see signals on the respective SPI1 SCK and MOSI pins.
I went back to my Nucleo board and have the same problem - SPI1 works fine but SPI5 doesn't work at all. I'm using Eclipse with the ARM GNU compiler and the most recent version of the Standard Peripheral Library (not HAL).
SPI init function:
void init_spi(void) {
//initialize MOSI and SCK pins
//initialize SPI
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
GPIO_InitTypeDef gpio_init;
gpio_init.GPIO_Pin = GPIO_Pin_0; //SCK
gpio_init.GPIO_Speed = GPIO_Fast_Speed;
gpio_init.GPIO_Mode = GPIO_Mode_AF;
gpio_init.GPIO_OType = GPIO_OType_PP;
gpio_init.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOB, &gpio_init);
gpio_init.GPIO_Pin = GPIO_Pin_8; //MOSI
gpio_init.GPIO_Speed = GPIO_Fast_Speed;
gpio_init.GPIO_Mode = GPIO_Mode_AF;
gpio_init.GPIO_OType = GPIO_OType_PP;
gpio_init.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOB, &gpio_init);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource0, GPIO_AF_SPI5);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource8, GPIO_AF_SPI5);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
//initialize DAC CS PIN
gpio_init.GPIO_Pin = DAC_CS_PIN;
gpio_init.GPIO_Speed = GPIO_Fast_Speed;
gpio_init.GPIO_Mode = GPIO_Mode_OUT;
gpio_init.GPIO_OType = GPIO_OType_PP;
gpio_init.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &gpio_init);
SPI_I2S_DeInit(SPI5);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI5, ENABLE); //enable SPI clock
SPI_InitTypeDef spi_init;
spi_init.SPI_Direction = SPI_Direction_1Line_Tx;
spi_init.SPI_Mode = SPI_Mode_Master;
spi_init.SPI_DataSize = SPI_DataSize_8b; //8b? Need to clock in 24 bits of data per DAC channel
spi_init.SPI_CPOL = SPI_CPOL_Low; //5134 uses low to high and high to low clock transitions. ie. idle state is LOW
spi_init.SPI_CPHA = SPI_CPHA_2Edge; //clock phase - data is clocked on falling edge of clock pulse
spi_init.SPI_NSS = SPI_NSS_Soft; //DAC chip select is handled in software
spi_init.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2; //APB2 clock/2, so 25 MHz SPI clock speed
spi_init.SPI_FirstBit = SPI_FirstBit_MSB; //check datasheet
spi_init.SPI_CRCPolynomial = 7; //what is this?
SPI_Init(SPI5, &spi_init);
SPI_Cmd(SPI5, ENABLE);
}
SPI Write function:
void spi_write_dac(uint16_t value, uint8_t channel) { //currently just use busy/wait to transmit data to test DAC
uint8_t dac_low = value & 0xFF; //take bottom 8 bits
uint8_t dac_high = value >> 8; //take top 8 bits
GPIO_ResetBits(GPIOA, DAC_CS_PIN); //CS low
while (SPI_I2S_GetFlagStatus(SPI5, SPI_I2S_FLAG_TXE) == RESET);//wait for empty buffer
SPI_I2S_SendData(SPI5, channel); //send control byte
while (SPI_I2S_GetFlagStatus(SPI5, SPI_I2S_FLAG_BSY) == SET); //wait for byte to be sent
SPI_I2S_SendData(SPI5, dac_high); //send first data byte
while (SPI_I2S_GetFlagStatus(SPI5, SPI_I2S_FLAG_BSY) == SET); //wait for byte to be sent
SPI_I2S_SendData(SPI5, dac_low); //send second data byte
while (SPI_I2S_GetFlagStatus(SPI5, SPI_I2S_FLAG_BSY) == SET); //wait for byte to be sent
GPIO_SetBits(GPIOA, DAC_CS_PIN);
}
This code does not work but when I change all SPI5 references to SPI1 and use PB3 for SCK and PB5 for MOSI then SPI is working. I've checked the SPI control registers and they look like they are correctly configured for SPI5 so I'm starting to get to my wit's end.
Why will SPI1 work fine on both my own design and on the Nucleo board, but SPI5 will not work on either board?
That is easy answer. SPI5 is not mapped to PB3 and PB5...
If you look at the datasheet on page 39 (datasheet rev 5), you could see that:
On PB3 you can use JTDO-SWO, I2C4_SDA, SPI1_SCK/I2S1_CK, USART1_RX, I2C2_SDA, EVENTOUT, but no SPI5
On PB5, you can use LPTIM1_IN1, I2C1_SMBA, SPI1_MOSI/I2S1_SD, EVENTOUT, but no SPI5
If you really want to use SPI5, you can use the following IOs:
SPI5_MISO: PA12
SPI5_MOSI: PA10 or PB8
SPI5_SCK: PB0
I did the same mistake.
GPIO_PinAFConfig(GPIOB, GPIO_PinSource0, GPIO_AF_SPI5);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource8, GPIO_AF_SPI5);
GPIO_AF_SPI5 must be GPIO_AF6_SPI5 for STM32F410.
#define GPIO_AF6_SPI5 ((uint8_t)0x06) /* SPI5 Alternate Function mapping (Only for STM32F410xx/STM32F411xE Devices) */
Is the SPI_I2S_DeInit(SPI1); normal in your init_spi() function when all your references are for SPI5 peripheral ?
If I am not wrong the target STM32 is a STM32F410RBT6. I let here the Datasheet and Reference Manual for future purposes :
STM32F410RBT6 Datasheet
STM32F410RBT6 Reference Manual

stm32 DMA cannot send data to SPI1 DR (Cannot use DMA to send data to SPI)

I am trying to use DMA to send data to SPI1. SPI1 will then control DAC for voltage update.
The chip used is STM32F407. Therefore, the corresponding channel/stream is: channel3/stream5, as is shown in reference manual. However, when the DMA is enabled, no data is shown in SPI1->DR and there are no results shown in the oscilloscope.
The SPI works fine when SPI1->DR is written by software. Could anyone help to check out what has happened? Here comes the code:
uint16_t DACData[1];
void InitDMA(void)
{
DMA_InitTypeDef DMA_InitStructure;
DMA_Cmd(DMA2_Stream2, DISABLE);
while (DMA2_Stream2->CR & DMA_SxCR_EN);
//SPI1 Tx DMA
DMA_DeInit(DMA2_Stream5);
DMA_StructInit(&DMA_InitStructure);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE); //Init DMA clock
DMA_InitStructure.DMA_Channel = DMA_Channel_3;//SPI1 Tx
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&(SPI1->DR); //Set the SPI1 Tx
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&DACData; //Set the memory location
DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral; //
DMA_InitStructure.DMA_BufferSize = 1; //Define the number of bytes to send
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
DMA_InitStructure.DMA_MemoryDataSize = DMA_PeripheralDataSize_HalfWord;
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; //Normal mode (not circular)
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable; //Operate in 'direct mode'
DMA_InitStructure.DMA_MemoryBurst =DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_PeripheralBurst =DMA_PeripheralBurst_Single;
DMA_Init(DMA2_Stream5, &DMA_InitStructure); //DMA2, Channel 3, stream 5
//Enable the transfer complete interrupt for DMA2 Stream5
DMA_ITConfig(DMA2_Stream5, DMA_IT_TC, ENABLE);
}
Here is how SPI1 initialized
void InitSPI1(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
SPI_InitTypeDef SPI_InitStruct;
// enable clock for used IO pins
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_6 | GPIO_Pin_5;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStruct);
// connect SPI1 pins to SPI alternate function
GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_SPI1); //SCK
GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_SPI1); //MISO
GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_SPI1); //MOSI
// enable peripheral clock
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
/* configure SPI1 in Mode 0
* CPOL = 0 --> clock is low when idle
* CPHA = 0 --> data is sampled at the first edge
*/
SPI_InitStruct.SPI_Direction = SPI_Direction_1Line_Tx; // one line transmission
SPI_InitStruct.SPI_Mode = SPI_Mode_Master; // transmit in master mode, NSS pin has to be always high
SPI_InitStruct.SPI_DataSize = SPI_DataSize_16b; // one packet of data is 16 bits wide
SPI_InitStruct.SPI_CPOL = SPI_CPOL_Low; // clock is low when idle
SPI_InitStruct.SPI_CPHA = SPI_CPHA_1Edge; // data sampled at first edge
SPI_InitStruct.SPI_NSS = SPI_NSS_Soft | SPI_NSSInternalSoft_Set;; // set the NSS management to hardware
SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256; // SPI frequency is APB2 frequency / 4 = 21MHz
SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;// data is transmitted MSB first
SPI_Init(SPI1, &SPI_InitStruct);
SPI_Cmd(SPI1, ENABLE); // enable SPI1
}
I try to use these commands to start DMA:
DACData[0]=0xff00;
DMA_Cmd(DMA2_Stream5, ENABLE);
In case it may help, here comes the register values for DMA2, Stream 5
Such a line has been missed:
SPI_I2S_DMACmd(SPI1, SPI_I2S_DMAReq_Tx, ENABLE);
It enables SPI1 to use the DMA.
Just to add:
If you want to restart DMA, it seems you'll have to call not just:
DMA_Cmd(DMA2_Stream5, ENABLE);
but
DMA_ClearFlag(DMA1_Stream5, DMA_FLAG_TCIF5);
as well. Even though I didn't enable interrupts it seems this is still required.
Based on a quick look, the first thing that stands out is that you try to use parts of the DMA peripheral before enabling it. Try moving the
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);
call before
DMA_Cmd(DMA2_Stream2, DISABLE);
while (DMA2_Stream2->CR & DMA_SxCR_EN);
as it could be eg. getting stuck in the loop. (I'm not sure which way the bits get read if the peripheral is not enabled when reading its registers)
If that doesn't help, leave a comment and I can try looking at it a bit more closely later.
I had a problem but nothing on the internet could help me, my solution was to change this;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
Other than that, my code is almost simliar, I don't get why yours would work.