HSPI as master, receiven data.

Oshio
Posts: 2
Joined: Tue Jan 30, 2018 5:11 am

HSPI as master, receiven data.

Postby Oshio » Tue Jan 30, 2018 6:09 am

Hello,
I'm trying to establish a serial connection between ESP8266 and MPU-9250 using HSPI.
The MPU-9250 states the following about the SPI protocol:

1. Data is delivered MSB first and LSB last.
2. Data is latched on the rising edge of SCLK
3. Data should be transitioned on the falling edge of SCLK
4. The maximum frequency of SCLK is 1MHz
5. SPI read and write operations are completed in 16 or more clock cycles (two or more bytes). The
first byte contains the SPI Address, and the following byte(s) contain(s) the SPI data. The first bit
of the first byte contains the Read/Write bit and indicates the Read (1) or Write (0) operation. The
following 7 bits contain the Register Address. In cases of multiple-byte Read/Writes, data is two
or more bytes
6. Supports Single or Burst Read/Writes.

This device has a register WHO_AM_I(0x75) which contains the value 0x71. So my first attempt is read this value, I already did it with other microcontroller, so I know for sure that the device is working.

Here is the code:

Code: Select all

#include "driver/spi_interface.h"
#include "uart.h"
#include "osapi.h"
#include "eagle_soc.h"
#include "ets_sys.h"

#define WHO_AM_I_MPU9250 0x75 // Should return 0x71

void ICACHE_FLASH_ATTR user_init(void)
{
   uint8_t subAddress = WHO_AM_I_MPU9250 | 0x80;
   uint8_t sendData = 0;

   uart_div_modify(0, UART_CLK_FREQ / BIT_RATE_115200);
   os_printf("\n\n ESP8266 is alive !!!\r\n");

   SpiAttr hSpiAttr;
   hSpiAttr.bitOrder = SpiBitOrder_MSBFirst;
   hSpiAttr.speed = SpiSpeed_1MHz;
   hSpiAttr.mode = SpiMode_Master;
   hSpiAttr.subMode = SpiSubMode_3;

   // Init HSPI GPIO
   WRITE_PERI_REG(PERIPHS_IO_MUX, 0x105);
   PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDI_U, 2); //configure io to spi mode MISO
   PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTCK_U, 2); //configure io to spi mode MOSI
   PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTMS_U, 2); //configure io to spi mode CLOCK
   // PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDO_U, 2); //configure io to spi mode CS

   SPIInit(SpiNum_HSPI, &hSpiAttr);

   SpiData spiData;
   spiData.cmd = MASTER_READ_DATA_FROM_SLAVE_CMD;
   spiData.cmdLen = 0;
   spiData.addr = &subAddress;
   spiData.addrLen = 1;
   spiData.data = &sendData;
   spiData.dataLen = 1;

   SPIMasterRecvData(SpiNum_HSPI, &spiData);
   os_printf(" Recv Slave data = [0x%02x]\r\n", sendData);
}


When I run this code I see only garbage in terminal's screen.
So I tried again but this time, I plug out MPU-9250's CS pin from GPIO15, and connected direct to the ground.
Then I've got the following output in terminal:

Code: Select all

 
 ESP8266 is alive !!!
Fatal exception 9(LoadStoreAlignmentCause):
epc1=0x4023dbb3, epc2=0x00000000, epc3=0x00000000, excvaddr=0x3ffffaa1, depc=0x00000000

 ets Jan  8 2013,rst cause:2, boot mode:(3,7)

load 0x3ffe8000, len 892, room 16
tail 12
chksum 0xb7
ho 0 tail 12 room 4
load 0x3ffe8380, len 372, room 12
tail 8
chksum 0x8c
load 0x40100000, len 27916, room 0
tail 12
chksum 0x78
csum 0x78


1 - Why I'm getting "Fatal exception 9(LoadStoreAlignmentCause)"?
2 - Should the code run with MPU-9250's CS pin connected in GPIO15?

I appreciate your time.

Her Mary
Posts: 537
Joined: Mon Oct 27, 2014 11:09 am

Re: HSPI as master, receiven data.

Postby Her Mary » Wed Jan 31, 2018 2:07 pm

There is an ESP8266 HSPI example, hope it can help you. https://github.com/espressif/esp8266-rtos-sample-code/tree/master/02Peripheral

Oshio
Posts: 2
Joined: Tue Jan 30, 2018 5:11 am

Re: HSPI as master, receiven data.

Postby Oshio » Thu Feb 01, 2018 6:50 am

Reading the HSPI_Master example I realize that I'm not understanding the struct SpiData.
How should I organize this struct if I want to read the value located at 0xF5 address of my slave device?

Here is what I've tried and failed:

Code: Select all

   uint8_t Data[1] = {0};
   
   SpiData spiData;
   spiData.cmd = 0xF5;
   spiData.cmdLen = 1;
   spiData.addr = NULL;
   spiData.addrLen = 0;
   spiData.data = Data;
   spiData.dataLen = 1;
   
   SPIMasterRecvData(SpiNum_HSPI, &spiData);


This seems ok or there is a mistake?

Who is online

Users browsing this forum: No registered users and 117 guests