Dear all,
I do apologize for the mundane and boring question, but unfortunately I am learning all this by myself, so I need some very basic help.
I am using the Eclipse/GCC setup to develop a simple STM32F407 program to be able to communicate with the MPU9250.
So I though a logical first step would be to try and spit out some data out of the pins. Here is my code:
#include <stdio.h>
#include <stdlib.h>
#include "diag/Trace.h"
#include <stm32f4xx.h>
#include <stm32f4xx_hal.h>
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#pragma GCC diagnostic ignored "-Wmissing-declarations"
#pragma GCC diagnostic ignored "-Wreturn-type"
int
main(int argc, char* argv[])
{
__HAL_RCC_SPI1_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
//PA4 NSS
//PA5 SCK
//PA6 MISO
//PA7 MOSI
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.Pin = GPIO_PIN_4|GPIO_PIN_5|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_6;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
SPI_InitTypeDef SPI_InitStruct;
SPI_InitStruct.Mode = SPI_MODE_MASTER;
SPI_InitStruct.Direction = SPI_DIRECTION_2LINES;
SPI_InitStruct.DataSize = SPI_DATASIZE_8BIT;
SPI_InitStruct.CLKPolarity = SPI_POLARITY_HIGH;
SPI_InitStruct.CLKPhase = SPI_PHASE_2EDGE;
SPI_InitStruct.NSS = SPI_NSS_HARD_OUTPUT;
SPI_InitStruct.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;
SPI_InitStruct.FirstBit = SPI_FIRSTBIT_MSB;
SPI_InitStruct.TIMode = SPI_TIMODE_DISABLE;
SPI_InitStruct.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
SPI_HandleTypeDef SPI_MPU;
SPI_MPU.Init = SPI_InitStruct;
SPI_MPU.Instance = SPI1;
HAL_SPI_Init(&SPI_MPU);
trace_printf("Spi on \n");
uint8_t t;
uint8_t r;
while(1){
t = 0x1A;
HAL_SPI_TransmitReceive(&SPI_MPU,&t,&r,1,1000);
t = 0x00;
HAL_SPI_TransmitReceive(&SPI_MPU,&t,&r,1,1000);
trace_printf("Sent: %#x Received: %#x \n",0x1A,r);
t = 0x1A | 0x80;
HAL_SPI_TransmitReceive(&SPI_MPU,&t,&r,1,1000);
t = 0xFF;
HAL_SPI_TransmitReceive(&SPI_MPU,&t,&r,1,1000);
trace_printf("Sent: %#x Received: %#x \n",0x1A | 0x80,r);
}
}
#pragma GCC diagnostic pop
My problem: When I probe the pins with my oscilloscope I get a square wave of around 12khz on the data lines and spiking on the clock, which seems to occur every transition of the data and every half period of the data.
And In all honesty I dont know where to start debugging. I feel like the GPIO pins are setup correctly because something is coming out, but i dont know why it is not the signal I am looking for.
On a different note, I am using semihosting on the stlink v2 and it is awfully slow (think a second per transmission slow). Am I doing something wrong? Is the stlink that slow? Is semihosting in general that slow? Any better options that are cheap? FTDI on a UART?
Thank you for all your considerations, and please do scream at me for any malpractices etc. This forum is my only way to interact about this kind of stuff so the more I learn the better