Author Topic: [SOLVED] STM32L4 SPI RX-only not reading anything... Help?  (Read 1533 times)

0 Members and 1 Guest are viewing this topic.

Offline technixTopic starter

  • Super Contributor
  • ***
  • Posts: 3507
  • Country: cn
  • From Shanghai With Love
    • My Untitled Blog
[SOLVED] STM32L4 SPI RX-only not reading anything... Help?
« on: November 24, 2018, 07:46:56 pm »
Edit: Solved:my GPIO configuration is wrong.

I am trying to build something that uses an RX-only SPI master with DMA on a STM32L433. However regardless how I try the SPI is not reading anything. Help?

The following code contains the SPI logic. TIM7 here controls the beginnings of the read bursts, and it is disabled here.

Code: [Select]
/*
 * spitemp.c
 *
 *  Created on: Nov 25, 2018
 *      Author: technix
 */

#include "spitemp.h"

volatile uint32_t temp_reading = 0;
static volatile uint32_t temp_buffer = 0;

static void spitemp_begin_capture(void);
static void spitemp_end_capture(void);

void spitemp_begin(void)
{
RCC->AHB2ENR |= RCC_AHB2ENR_GPIOAEN;
RCC->AHB1ENR |= RCC_AHB1ENR_DMA1EN;
RCC->APB1ENR1 |= RCC_APB1ENR1_TIM7EN;
RCC->APB2ENR |= RCC_APB2ENR_SPI1EN;

SET_FIELD(GPIOA->AFR[0], 0x0fff0000, 0x05550000);
SET_FIELD(GPIOA->MODER, 0x000003f0, 0x000002a0);

SPI1->CR1 =
SPI_CR1_CPHA |
SPI_CR1_MSTR |
(4 << SPI_CR1_BR_Pos) |
SPI_CR1_RXONLY;
SPI1->CR2 =
SPI_CR2_SSOE |
(7 << SPI_CR2_DS_Pos) |
SPI_CR2_FRXTH;

SET_FIELD(DMA1_CSELR->CSELR, DMA_CSELR_C2S_Msk, 1 << DMA_CSELR_C2S_Pos);
DMA1_Channel2->CCR =
DMA_CCR_TCIE |
DMA_CCR_MINC |
(0 << DMA_CCR_PSIZE_Pos) |
(0 << DMA_CCR_MSIZE_Pos);
DMA1_Channel2->CPAR = (uint32_t)&(SPI1->DR);
__DSB();
NVIC_EnableIRQ(DMA1_Channel2_IRQn);

TIM7->CR1 = TIM_CR1_URS;
TIM7->DIER = TIM_DIER_UIE;
TIM7->CNT = 0;
TIM7->PSC = 10000 - 1;
TIM7->ARR = 8000 - 1;
NVIC_EnableIRQ(TIM7_IRQn);
//TIM7->CR1 |= TIM_CR1_CEN;

spitemp_begin_capture();
}

static void spitemp_begin_capture(void)
{
DMA1_Channel2->CMAR = (uint32_t)&temp_buffer;
DMA1_Channel2->CNDTR = sizeof(temp_buffer);
__DSB();

SPI1->CR2 |= SPI_CR2_RXDMAEN;
DMA1_Channel2->CCR |= DMA_CCR_EN;
SPI1->CR1 |= SPI_CR1_SPE;
}

static void spitemp_end_capture(void)
{
DMA1_Channel2->CCR &= ~DMA_CCR_EN;
SPI1->CR1 &= ~SPI_CR1_SPE;
SPI1->CR2 &= ~(SPI_CR2_RXDMAEN | SPI_CR2_TXDMAEN);

temp_reading = temp_buffer;
__BKPT(0);
}

void TIM7_IRQHandler(void)
{
TIM7->SR = 0;
spitemp_begin_capture();
}

void DMA1_Channel2_IRQHandler(void)
{
if (DMA1->ISR & DMA_ISR_TCIF2)
{
DMA1->IFCR = DMA_IFCR_CTCIF2;
spitemp_end_capture();
}
}
« Last Edit: November 28, 2018, 04:56:03 am by technix »
 

Offline capt bullshot

  • Super Contributor
  • ***
  • Posts: 3033
  • Country: de
    • Mostly useless stuff, but nice to have: wunderkis.de
Re: STM32L4 SPI RX-only not reading anything... Help?
« Reply #1 on: November 24, 2018, 07:57:01 pm »
Didn't check details, but in general with SPI you have to send dummy words to receive anything. I didn't see writes to the SPI data register.
Safety devices hinder evolution
 

Offline technixTopic starter

  • Super Contributor
  • ***
  • Posts: 3507
  • Country: cn
  • From Shanghai With Love
    • My Untitled Blog
Re: STM32L4 SPI RX-only not reading anything... Help?
« Reply #2 on: November 24, 2018, 08:00:02 pm »
Didn't check details, but in general with SPI you have to send dummy words to receive anything. I didn't see writes to the SPI data register.
When configured as RX-only, as the documentation suggests, it does not need or even accept TX data.

I have tried setting up DMA to write dummy bytes but that never worked either.
 

Offline technixTopic starter

  • Super Contributor
  • ***
  • Posts: 3507
  • Country: cn
  • From Shanghai With Love
    • My Untitled Blog
Re: STM32L4 SPI RX-only not reading anything... Help?
« Reply #3 on: November 28, 2018, 04:55:19 am »
Fixed: my GPIO settings are wrong.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf