2023-05-03 03:01 PM - edited 2023-11-20 06:31 AM
Hello,
I'm trying to do a basic spi master transmit, and am having issues with the MOSI pin output. It seems the value is getting written, but the voltage swing is ~200mV rather than full value. I suspect I have the GPIO pin mis-configured, but I'm at a loss as to where my mistake is after reading the datasheet and searching online. Here's my basic code for stm32l476 (using the nucleo board)...
void basic_spi() {
GPIO_TypeDef* gpio = GPIOA;
SPI_TypeDef* spi = SPI1;
uint8_t data = 0x12;
// gpio config
RCC->AHB2ENR |= (1<<0);
GPIOA->MODER &= ~((3<<10)|(3<<12)|(3<<14)|(3<<18));
//af pa5,pa6,pa7,output pa9
GPIOA->MODER |= (2<<10)|(2<<12)|(2<<14)|(1<<18);
// high speed
GPIOA->OSPEEDR |= (3<<10)|(3<<12)|(3<<14)|(3<<18);
// af5 for pa5,pa6,pa7
GPIOA->AFR[0] |= (5<<20)|(5<<24)|(5<<28);
// spi config
RCC->APB2ENR |= (1<<12); //clock enable
SPI1->CR1 &= ~(SPI_CR1_RXONLY | SPI_CR1_LSBFIRST | SPI_CR1_BR);
SPI1->CR1 |= (SPI_CR1_CPOL | SPI_CR1_CPHA | SPI_CR1_SSM | SPI_CR1_SSI |SPI_CR1_BR_0 | SPI_CR1_BR_1 | SPI_CR1_MSTR);
SPI1->CR2 = 0;
// spi enable
SPI1->CR1 |= (1<<6);
while (1) {
GPIOA->BSRR |= (1<<9)<<16;
while (!((SPI1->SR)&(1<<1))) {};
*(volatile uint8_t*)&SPI1->DR = (uint8_t) data;
while (!((SPI1->SR)&(1<<1))) {};
while (((SPI1->SR)&(1<<7))) {};
GPIOA->BSRR |= (1<<9);
for (uint32_t i = 0; i < 100000; i++)
{
__NOP();
}
}
}
CH1 - CS (PA9)
CH2 - SCK (PA5)
CH3 - MISO (PA6) - not connected to any other device
CH4 - MOSI (PA7)
I'd appreciate any help you have, thanks!
Solved! Go to Solution.
2023-05-04 09:36 AM
Update to this...
I had the oscilloscope misconfigured. The (passive) probe was set to 10X when the scope expected 1X. Correctly configuring the probe makes everything work fine.
Thanks for the help!
2023-05-03 11:14 PM
To me “Runt pulse�? suggests one that is short duration, perhaps just a spike. But your description is one of low voltage. (Maybe I’m the one who doesn’t know the terminology.)
To me low voltage suggests that output pin is connected to another output - rather than an input. Does it go anywhere on the Nucleo board?
Where does PA7 connect - is it to MOSI or MISO of the SPI device you’re talking to?
2023-05-04 07:59 AM
Thanks Danish,
Your'e probably right, runt pulse is the wrong terminology. The pulses arrive when they are supposed to; I can change the data input and get the corresponding bits; it's just the level that is suspect. This seems to indicate something other than the spi peripheral is also driving the pin, like you suggest.
All the pins above are only connected to the oscilloscope however, which is why I suspect a misconfiguration.
2023-05-04 09:36 AM
Update to this...
I had the oscilloscope misconfigured. The (passive) probe was set to 10X when the scope expected 1X. Correctly configuring the probe makes everything work fine.
Thanks for the help!