rosflight / airbourne_f4

Be the Jason Bourne of the skies
BSD 3-Clause "New" or "Revised" License
6 stars 6 forks source link

Working SPI between F4 and MPU6000 #1

Closed len0rd closed 7 years ago

len0rd commented 7 years ago

Just going to use this to keep track of some thoughts, if anyone wants to join in and help.

From the F2 std_periph documentation(pg 484), I think current implementation is a little shaky on steps 2&3:

2. Enable SCK, MOSI, MISO and NSS GPIO clocks using
RCC_AHB1PeriphClockCmd() function. In I2S mode, if an external clock source is
used then the I2S CKIN pin GPIO clock should also be enabled.
3. Peripherals alternate function:
    - Connect the pin to the desired peripherals' Alternate Function (AF) using 
       GPIO_PinAFConfig() function - Configure the desired pin in alternate function 
       by: GPIO_InitStruct->GPIO_Mode = GPIO_Mode_AF
    - Select the type, pull-up/pull-down and output speed via GPIO_PuPd, 
      GPIO_OType and GPIO_Speed members
    - Call GPIO_Init() function In I2S mode, if an external clock source is used then the I2S CKI

Also, reading the f4 reference, I think we want NSS to be in SPI_NSS_Hard mode:

Hardware NSS management (SSM = 0)
Two configurations are possible depending on the NSS output configuration 
(SSOE bit in register SPI_CR2).
    – NSS output enabled (SSM = 0, SSOE = 1) This configuration is used only when 
      the device operates in master mode. The NSS signal is driven low when the master 
      starts the communication and is kept low until the SPI is disabled. 

This is the functionality required by the mpu6000. The SPI1 NSS line is connected to a pull-up resistor, so it needs to be driven low whenever we want to send/receive data.

len0rd commented 7 years ago

Looks like most of the mpu6000 init stuff I was worried about is handled in mpu6000_spi constructor. At this point i think the biggest thing to check is the spi.transfer and spi.set_ss_low/high functions.

It may be necessary to adjust the baudrate_prescaler to adhere to mpu6000 1Mhz max

len0rd commented 7 years ago

now might be a good time to try configuring the nss pin as od with no pull

len0rd commented 7 years ago

Running out of ideas, perhaps using spi with the mpu is spec'ed differently than I'm assuming. I hooked up the logic analyzer to SPI2, which comes out the Flex-IO port. SPI seems to work fine, the only strange thing is on this particular transfer, MOSI changes from inactive high to inactive low, despite setting it to have a PU resistor in the GPIO conf:

mosi-changes-idle

In the code, its this send, in the mpu6000_spi constructor:

spi->send(MPU_RA_SIGNAL_PATH_RESET, 0x07);//BIT_GYRO | BIT_ACC | BIT_TEMP);

Not sure if this could cause issue, but its certainly unexpected. Also need to test the receive function

len0rd commented 7 years ago

Think I finally have this working! At least I'm getting data back, so that's good. I kind of ended up rewriting all of the spi and mpu stuff though, so its going to be a few messy commits here...

len0rd commented 7 years ago

blink and spi examples are working! (vcp would be totally broken even if I didnt remove the vcp.cpp files. Planning on fixing next) Currently mpu just spits back the raw int16_t data of all the sensors, which I think is fine since this is how the breezy mpu6050 driver works. Next is getting exti working, however I think working pwm may take priority over that.

superjax commented 7 years ago

Nice!!

len0rd commented 7 years ago

closing for now