cancel
Showing results for 
Search instead for 
Did you mean: 

Discovery & SPI2

lee23
Associate II
Posted on July 27, 2015 at 19:50

I'm trying what seems like it should be a simple SPI setup: Using SPI2 in master mode, setting pins PB13...PB15 to AF0, using A8 as a manual chip select. SPI2 is enabled in APB1ENR, all the settings are basically default (only bits set are SSM, MSTR, and or course SPE and BR). When I invoke my xfer function, it waits for TXE, sends data, waits for RXNE, the returns data. Simple polling, no interrupts.

But I am not seeing any signals on PB13...PB15. PB13 should be clock, so that should be the easiest to identify, but it does nothing. My manual chip select A8 is fine, but I never see the clock or an data transfer. I don't think the Discovery board is using PB13 for anything else, and I don't see any code in samples I've looked at (like  STM32Cube_FW_L0_V1.1.0/Projects/STM32L053C8-Discovery/Examples/SPI/SPI_FullDuplex_ComIT/Src) that I'm leaving out. Do I need to turn on another clock or something?

7 REPLIES 7
Posted on July 27, 2015 at 20:39

They should present at the NFC Header.

You'd need the GPIOB clock enabled before programming the pins or AF functionality. And the SPI2 clock, which is on APB1, and not APB2 like SPI1 is.

The snippets examples works with SPI2, really can't help you with HAL/Cube stuff.

STM32L0xx_Snippets_Package_V1.1.1\Projects\SPI\01_FullDuplexCommunication\main.c

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
lee23
Associate II
Posted on July 27, 2015 at 22:40

I'm pretty sure I'm doing all that. Still no recognizable clock signal either from the PB13 pin or from pin 6 of CN1 (the NFC connector). I'm trying to interface to an ST MKI/160V1 accelerometer eval board, but if I can't even get a clock signal...

Below is my code in its entirety. I've tried adding pin 12 (which I don't need, and didn't make a difference), I've tried witching to the HSI clock (because the Snippets example used that)--made no difference. I get nothing but a nice level 0V on the scope, and waiting for TXE times out.

(BTW, I'm using an open-source library whose register names might not be exactly the same as yours, but nothing unrecognizable, and I did check the addresses and such).

<code>

int spi_setup(void) {

    RCC->IOPENR |= (RCC_IOPENR_IOPAEN | RCC_IOPENR_IOPBEN);

    RCC->APB1ENR |= RCC_APB1ENR_SPI2EN;

    // Set up B13..B15 for SPI2 (AF 0)

    uint32_t v = GPIOB->MODER;

    v &= ~(MASK_GPIO_MODER_MODE13 | MASK_GPIO_MODER_MODE14 | MASK_GPIO_MODER_MODE15);

    v |= ((2 << SHIFT_GPIO_MODER_MODE13) | (2 << SHIFT_GPIO_MODER_MODE14) | (2 << SHIFT_GPIO_MODER_MODE15));

    GPIOB->MODER = v;

    v = GPIOB->AFR[1];

    v &= ~(MASK_GPIO_AFRH_AFSEL13 | MASK_GPIO_AFRH_AFSEL14 | MASK_GPIO_AFRH_AFSEL15);

    GPIOB->AFR[1] = v;

    // Set up GPIO A8 as chip select

    v = GPIOA->MODER;

    v &= ~MASK_GPIO_MODER_MODE8;

    v |= (1 << SHIFT_GPIO_MODER_MODE8);

    GPIOA->MODER = v;

    GPIOA->BSRR = GPIO_BSRR_BS8;

    int rate = 3;

    SPI2->CR1 = (SPI_CR1_SSM | SPI_CR1_MSTR | (rate << SHIFT_SPI_CR1_BR));

    SPI2->CR2 = 0;

    SPI2->CR1 |= SPI_CR1_SPE;

    return 0;

}

</code>

Posted on July 28, 2015 at 04:31

Do you see an SPI clock with the attached .HEX?

________________

Attachments :

L0_SPI2_Rel1.hex : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I01S&d=%2Fa%2F0X0000000bSh%2FR_iTq85ATKa2eqY63_ieW212zQuh3Eir2SkJeJJl0S0&asPdf=false
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
lee23
Associate II
Posted on July 28, 2015 at 18:07

Not a peep, at either connector (I loaded it with ''st-flash --reset write L0_SPI2_Rel1.hex 0x8000000''). Tried pressing buttons to see if that caused any reaction.

FYI, if I set up PB13 as a normal GPIO output, I can set it and read it fine at both the pin and CN1. Also, I tried setting the speed to max, no difference.

The board is an STM32L0538-DISCO MB1143B. I see nothing in the docs about having to enable SPI2 with a solder bridge or something. I can't rule out that I've fried something, but this symptom seems awfully specific.

Posted on July 28, 2015 at 18:30

I guess you need to step back and confirm it's actually running any code.

Pretty sure SPI2 is directly wired to the header, and not otherwise used on the board.

Make sure BOOT0 is low. Use a debugger and step the code execution.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
lee23
Associate II
Posted on July 28, 2015 at 20:15

It's definitely running my code...I'm running a command-line UI on usart1, and it's responding to all my commands (things like print the time from RTC, print an ADC-measured voltage, go to Standby mode, etc. When I give it the command to do the SPI2 xfer, it times out waiting for TXE.

The debugger is kind of a pain, but I'll try it. I'll also double-check things the I/O addresses. Thanks again for the help. I'll let you know if I find something.

lee23
Associate II
Posted on July 28, 2015 at 23:42

Stupid mistake: all it was was a simple  == / != bug: I was waiting for TXE to clear (which never happens) instead of for it to be set, and so timing out and never writing to DR which triggers the clock. Amazing what happens when you RTFM *carefully*. Again, thanks for all the help.