Hot!Problems connection to EEPROM with SPI

Author
Hugo Sousa
New Member
  • Total Posts : 5
  • Reward points : 0
  • Joined: 2018/10/14 16:23:33
  • Location: 0
  • Status: offline
2018/10/23 10:11:49 (permalink)
0

Problems connection to EEPROM with SPI

Hi, 
 
I'm working with a PIC32MX570F512L in a Explorer 16/32 board, and for the last week i had been trying to connect to an EEPROM (25LC256 that is built in the board) using SPI. 
 
For what I´ve understand the EEPROM is connected trough SPI2 but i'm not even being able to write in the SPI2BUF.
 
This is my code,
 

#include <stdio.h>
#include <stdlib.h>
#include <p32xxxx.h>
#include <xc.h>
#include <sys/attribs.h>
 
// DEVCFG3
// USERID = No Setting
#pragma config PMDL1WAY = ON // Peripheral Module Disable Configuration (Allow only one reconfiguration)
#pragma config IOL1WAY = ON // Peripheral Pin Select Configuration (Allow only one reconfiguration)
#pragma config FUSBIDIO = ON // USB USID Selection (Controlled by the USB Module)
#pragma config FVBUSONIO = ON // USB VBUS ON Selection (Controlled by USB Module)
// DEVCFG2
#pragma config FPLLIDIV = DIV_2 // PLL Input Divider (2x Divider)
#pragma config FPLLMUL = MUL_18 // PLL Multiplier (18x Multiplier)
#pragma config UPLLIDIV = DIV_12 // USB PLL Input Divider (12x Divider)
#pragma config UPLLEN = OFF // USB PLL Enable (Disabled and Bypassed)
#pragma config FPLLODIV = DIV_1 // System PLL Output Clock Divider (PLL Divide by 1)
// DEVCFG1
#pragma config FNOSC = PRIPLL // Oscillator Selection Bits (Primary Osc w/PLL (XT+,HS+,EC+PLL))
#pragma config FSOSCEN = OFF // Secondary Oscillator Enable (Disabled)
#pragma config IESO = OFF // Internal/External Switch Over (Disabled)
#pragma config POSCMOD = XT // Primary Oscillator Configuration (XT osc mode)
#pragma config OSCIOFNC = ON // CLKO Output Signal Active on the OSCO Pin (Enabled)
#pragma config FPBDIV = DIV_8 // Peripheral Clock Divisor (Pb_Clk is Sys_Clk/8)
#pragma config FCKSM = CSDCMD // Clock Switching and Monitor Selection (Clock Switch Disable, FSCM Disabled)
#pragma config WDTPS = PS1048576 // Watchdog Timer Postscaler (1:1048576)
#pragma config WINDIS = OFF // Watchdog Timer Window Enable (Watchdog Timer is in Non-Window Mode)
#pragma config FWDTEN = OFF // Watchdog Timer Enable (WDT Disabled (SWDTEN Bit Controls))
#pragma config FWDTWINSZ = WINSZ_25 // Watchdog Timer Window Size (Window Size is 25%)
// DEVCFG0
#pragma config JTAGEN = OFF // JTAG Enable (JTAG Disabled)
#pragma config ICESEL = ICS_PGx1 // ICE/ICD Comm Channel Select (Communicate on PGEC1/PGED1)
#pragma config PWP = OFF // Program Flash Write Protect (Disable)
#pragma config BWP = OFF // Boot Flash Write Protect bit (Protection Disabled)
#pragma config CP = OFF // Code Protect (Protection Disabled)
 
// peripheral configuration
# define SPI_CONF 0x8120
# define SPI_BAUD 15 // clock divider Fpb / (2 * (15 + 1))
# define CS PORTDbits.RD12 // chip select
 
void InitSPI2(void)
{
int clean;

IFS1CLR = 0xE0; // disable interrupts
SPI2CON = 0; // stop and reset SPI2
clean = SPI2BUF; // clears the recieve buffer
SPI2BRG = SPI_BAUD; // select clock speed
SPI2STATCLR = 0x40; // clear the overflow
SPI2CON = SPI_CONF; // select mode
TRISDbits.TRISD12 = 0; // Define chip select as output
CS = 1; // set chip select in high

TRISGbits.TRISG7 = 1; // set MISO as input
TRISGbits.TRISG8 = 0; // set MOSI as output
TRISGbits.TRISG6 = 0; // set SCK2 as output

ANSELD = 0; // set PORTD as digital
ANSELG = 0; // set PORTG as digital

SDI2R = 1; // configure the input SDI2 to pin RG7
RPG8R = 6; // configure the output SDO2 to pin RPG8
}
 
int WriteSPI2( int data)
{
SPI2BUF = data; // write to buffer for TX
while(!(SPI2STATbits.SPIRBF)); // wait until the transfer is complete
// SPIRBF recieve buffer full status bit
return SPI2BUF; // read the recieved value
} // writeSPI2
 
void main(void)
{
   InitSPI2(); // start SPI
   int data = 7; 
   TRISA = 0xff00;
   LATA = 0;
   LATA = WriteSPI2( data );
}

 
The idea was to turn on the first 3 leds, but nothing happens. Any idea why?
 
Thanks
#1

5 Replies Related Threads

    qhb
    Superb Member
    • Total Posts : 9625
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: Problems connection to EEPROM with SPI 2018/10/23 12:21:35 (permalink)
    0
    Hugo Sousa
    For what I´ve understand the EEPROM is connected trough SPI2 but i'm not even being able to write in the SPI2BUF.

    How do you know?
    Have you actually read the datasheet for the EEPROM?
    Your code sends command 0x07, then writes the value received while the command was being sent, then exits.
    That's three errors in three lines.
     
    What do you expect sending command 0x07 to do?
    What do you expect to receive in the same transfer as sending that command
    NEVER EVER let the main function exit in an embedded program.
     
     
    #2
    Hugo Sousa
    New Member
    • Total Posts : 5
    • Reward points : 0
    • Joined: 2018/10/14 16:23:33
    • Location: 0
    • Status: offline
    Re: Problems connection to EEPROM with SPI 2018/10/23 14:41:30 (permalink)
    0
    Hi,
    I was expecting it to return the value 0x07, since that is what i'm writing to the buffer. And yes, i had read the datasheet and the EEPROM clock is connected to pin 10 of the pic which is the SCK2, but at the same time the chip select (of the EEPROM) is connected to pin 79 of the board which, in the pic, is connected to a pin that supports the peripheral pin select feature (RPD12) that can only select the SS1, SS3 and SS4 but not SS2.  
    Also, sorry for my dumb questions but  i'm new to this world of embedded programming.
    #3
    qhb
    Superb Member
    • Total Posts : 9625
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: Problems connection to EEPROM with SPI 2018/10/23 14:54:30 (permalink)
    0
    Hugo Sousa
    Hi,
    I was expecting it to return the value 0x07, since that is what i'm writing to the buffer.

    That is an incorrect expectation.
    It will return whatever value the EEPROM output while receiving that command.
    I really suspect you have not read the EEPROM datasheet yet.
     

    And yes, i had read the datasheet

    Which datasheet?
    You seem completely oblivious to how an SPI EEPROM works.

    and the EEPROM clock is connected to pin 10 of the pic which is the SCK2, but at the same time the chip select (of the EEPROM) is connected to pin 79 of the board which, in the pic, is connected to a pin that supports the peripheral pin select feature (RPD12) that can only select the SS1, SS3 and SS4 but not SS2. 

    Those SS pins are only for when your device is acting as an SPI slave.
    When you are a Master, it is your job to manually drive the pin low to assert SS at the beginning of each cycle.
     
    The EEPROM datasheet is here
    http://ww1.microchip.com/...n/DeviceDoc/21822G.pdf
     
    The commands are documented on page 7 and 8.
    In the first two examples (FIGURE 2-1: READ SEQUENCE and FIGURE 2-2 BYTE WRITE SEQUENCE) you can see the data output pin is just "high impedance", i.e. there's nothing coming back while the command is being sent.
     
    post edited by qhb - 2018/10/23 15:15:00
    #4
    JorgeF
    Super Member
    • Total Posts : 3340
    • Reward points : 0
    • Joined: 2011/07/09 11:56:58
    • Location: PT/EU @ Third rock from the Sun
    • Status: offline
    Re: Problems connection to EEPROM with SPI 2018/10/23 14:57:13 (permalink)
    0
    Hi
    Hugo Sousa
    Hi,
    I was expecting it to return the value 0x07, since that is what i'm writing to the buffer.



    You have the wrong idea on how SPI works.
    The SPI connection is like 2 shift registers chained in a loop, so while the byte you placed in the SSPBUFF is shifted out, the byte currently on the EEPROM buffer is shifted in. The bytes in PIC and EEPROM buffers are swapped.
    Until you send a read command to the EEPROM, all you receive from it will be idle data.
     
    HIH
     
     

    Best regards
    Jorge
     
    I'm here http://picforum.ric323.com too!
    And it works better....
    #5
    Hugo Sousa
    New Member
    • Total Posts : 5
    • Reward points : 0
    • Joined: 2018/10/14 16:23:33
    • Location: 0
    • Status: offline
    Re: Problems connection to EEPROM with SPI 2018/10/24 08:03:23 (permalink)
    0
    I thought the problem was on the buffer. Now i'm able to write and read to the EEPROM. Thanks for the help.
    #6
    Jump to:
    © 2019 APG vNext Commercial Version 4.5