• AVR Freaks

Hot!PIC24 SPI RECEIVE ISR NOT WORKING

Author
binaryman
Super Member
  • Total Posts : 209
  • Reward points : 0
  • Joined: 2015/11/19 17:11:04
  • Location: Saint Louis, Missouri USA
  • Status: offline
2020/04/04 17:10:46 (permalink)
0

PIC24 SPI RECEIVE ISR NOT WORKING

Greetings from Saint Louis Missouri 
 
I'm working with pic 24FJ64GA004.  The motherboard has a small SPI EEPROM 25LC02A  I trying to update/modify good code from blocking to ISR driven.  Attached is working firmware that can read and write to the 8 bit SPI EEPROM in blocking mode.   The PIC24 SPI RECEIVE ISR is NOT WORKING. 
 
Please take notice: When writing to the EEPROM the ISR fires OK.   The problem is the ISR does get get called when reading the EEPROM.   What am I doing wrong / not realizing ?   I
 
It was originally written and tested as a blocking API without an ISR.  I was going to change it to a state machine driven ISR software, FIFO BUFFERED background, processing so the calling code with not have blocking delays.  
 
When the HAL_EEPROM_read8 function is called the ISR never fires.   However when the HAL_EEPROM_write8 function is called the ISR fires just fine.  The counter goes up 60+;  
 
 
 
 
Am attached the code as a zip file and also pasting it inline.  
 

 //p24FJ64GA004
#include "xc.h"
/*
EEPROM 25LC02A
1 CS [Chip Select Input] to Pic pin 1 /RP9
2 SO [Serial Data Output] to Pic pin 42Pic pin /RP6
3 WP [Write-Protect] to Pic pin 38/RP21
4 VSS
5 SI [Serial Data Input] to Pic pin 37/RP20
6 SCK [Serial Clock Input] to Pic pin 36/RP19
7 HOLD [Hold Input] to Pic pin 34/T1CK
8 VCC

*/
#define EEPROM_CS_TRIS (TRISBbits.TRISB9) // PIC-01-RB9 to EPROM pin 1 CS/Chip_Select_input
#define EEPROM_CS_LATCH (LATBbits.LATB9) // PIC-01-RB9 to EPROM pin 1 CS/Chip_Select_input
#define EEPROM_SCK_TRIS (TRISCbits.TRISC3) // PIC-36-RC3 to EPROM pin 6 *SCK/Serial_Clock_Input
#define EEPROM_SCK_LATCH (LATCbits.LATC3) // PIC-36-RC3 to EPROM pin 6 *SCK/Serial_Clock_Input
#define EEPROM_SDO_TRIS (TRISBbits.TRISB6) // PIC-42-RB6 to EPROM pin 2 SDO/Serial_Data_Output
#define EEPROM_SDO_LATCH (LATBbits.LATB6) // PIC-42-RB6 to EPROM pin 2 SDO/Serial_Data_Output
#define EEPROM_SDI_TRIS (TRISCbits.TRISC4) // PIC-37-RC4 to EPROM pin 5 Si/Serial_Data_Input
#define EEPROM_SDI_LATCH (LATCbits.LATC4) // PIC-37-RC4 to EPROM pin 5 Si/Serial_Data_Input
#define EEPROM_HOLD_TRIS (TRISAbits.TRISA4) // PIC-34-RA4 to EPROM pin 7 *Hold_Input
#define EEPROM_HOLD_LATCH (LATAbits.LATA4) // PIC-34-RA4 to EPROM pin 7 *Hold_Input
#define EEPROM_WP_TRIS (TRISCbits.TRISC5) // PIC-38-RC5 to EPROM pin 3 *Write_Protect_Input
#define EEPROM_WP_LATCH (LATCbits.LATC5) // PIC-38-RC5 to EPROM pin 3 *Write_Protect_Input
#define EEPROM_CMD_READ (unsigned)0b00000011
#define EEPROM_CMD_WRITE (unsigned)0b00000010
#define EEPROM_CMD_WRDI (unsigned)0b00000100
#define EEPROM_CMD_WREN (unsigned)0b00000110
#define EEPROM_CMD_RDSR (unsigned)0b00000101
struct STATREG
{
unsigned WIP:1;
unsigned WEL:1;
unsigned BP0:1;
unsigned BP1:1;
unsigned RESERVED:3;
unsigned WPEN:1;
};
union EEPROM_STATUS_REG
{
struct STATREG bit;
unsigned char byte;
};

#define Lo(X) (unsigned char)(X&0x00ff)

#define HAL_EEPROM_CS_LO() EEPROM_CS_LATCH = 0;
#define HAL_EEPROM_CS_HI() EEPROM_CS_LATCH = 1;
volatile Uint8 spi_isr;

void HAL_EEPROM_Map_Pins()
{
Pic24_IO_Unock();
RPINR22bits.SDI2R = 6; // Assign SDI2R to PIC pin 42-RB6 and EPROM pin 2 SDO
_RP20R = 10; // PIC pin 37/RP20 to SDO2/10 and EPROM pin 5 SDI
_RP19R = 11; // PIC pin 36/RP19 to SCK2OUT/8 and EPROM pin 6 SCK
// Map both SCK2OUT and SCK2IN to RC3, as that is the pin connected to the EEPROM
// RPINR22bits.SCK2R = 19; // Map SCK2OUT to RP19
Pic24_IO_Lock();
}
void HAL_EEPROM_init()
{
HAL_EEPROM_Map_Pins();
EEPROM_CS_LATCH = 1;
EEPROM_CS_TRIS = 0;
EEPROM_HOLD_LATCH = 1;
EEPROM_HOLD_TRIS = 0;
EEPROM_WP_LATCH = 1;
EEPROM_WP_TRIS = 0;
IFS2bits.SPI2IF = 0; // clear interrupt flag
IEC2bits.SPI2IE = 0; // enable interrupt

SPI2CON1bits.PPRE= 0B10; // 10 = Primary prescale 4:1
SPI2CON1bits.SPRE= 0B111; // 111 = Secondary prescale 1:1
SPI2CON1bits.MSTEN= 1; // MSTEN: Master Mode Enable bit
SPI2CON1bits.CKP=0; // CKP: Clock Polarity Select bit
SPI2CON1bits.SSEN=0; // Slave Select Enable bit
SPI2CON1bits.CKE=0; // Clock Edge Select bit(3)
SPI2CON1bits.SMP=0; // Data Input Sample Phase 0=middle
SPI2CON1bits.MODE16=0; // 8 bit mode
SPI2CON1bits.DISSDO=0; // 0=Module
SPI2CON1bits.DISSCK=0; // 0 = Internal SPI clock is enabled
IFS2bits.SPI2IF = 0; // clear interrupt flag
IPC8bits.SPI2IP=3;
IEC2bits.SPI2IE = 1; // enable interrupt
SPI2STATbits.SPIEN = 1; //Enable SPI Module
}
// send one byte of data and receive one back at the same time
unsigned char HAL_write_SPI(unsigned char i)
{
SPI2BUF = i; // write to buffer for TX
while(! SPI2STATbits.SPIRBF)
{
// wait for transfer to complete
}
return SPI2BUF; // read the received value
}
void HAL_EEPROM_Write_Disable()
{
HAL_EEPROM_CS_LO();
HAL_write_SPI(EEPROM_CMD_WRDI);
HAL_EEPROM_CS_HI();
}
void HAL_EEPROM_Write_Enable()
{
HAL_EEPROM_CS_LO();
HAL_write_SPI(EEPROM_CMD_WREN);
HAL_EEPROM_CS_HI();
}
union EEPROM_STATUS_REG HAL_EEPROM_status()
{
unsigned char rc;
HAL_EEPROM_CS_LO();
HAL_write_SPI(EEPROM_CMD_RDSR);
rc = HAL_write_SPI(0);
HAL_EEPROM_CS_HI();
return (union EEPROM_STATUS_REG)rc;
}
unsigned char HAL_EEPROM_read8(unsigned int addr)
{
unsigned char value;
HAL_EEPROM_CS_LO();
HAL_write_SPI(EEPROM_CMD_READ);
HAL_write_SPI(addr);
value = HAL_write_SPI(0);
HAL_EEPROM_CS_HI();
return value;
}
void HAL_EEPROM_write8(unsigned char data, unsigned int address)
{
HAL_EEPROM_Write_Enable(); // 1
HAL_EEPROM_CS_LO();
HAL_write_SPI(EEPROM_CMD_WRITE); // 2
HAL_write_SPI(address); // 3
HAL_write_SPI(data); // 4
HAL_EEPROM_CS_HI();
while( HAL_EEPROM_status().bit.WIP )// 5
{
// wait for completion of previous write operation
}
HAL_EEPROM_Write_Disable(); // 6
}
void __attribute__((interrupt, no_auto_psv)) _SPI2Interrupt()
{
IFS2bits.SPI2IF = 0;
spi_isr++;
}
void HAL_EEPROM_secs() // This called once per second
{
if (spi_isr)
{
printf("SPI-ISR:%d\n",spi_isr);
spi_isr=0;
}
}

 
 
post edited by binaryman - 2020/04/07 06:06:29
#1

10 Replies Related Threads

    ric
    Super Member
    • Total Posts : 28019
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: PIC24 SPI RECEIVE ISR NOT WORKING 2020/04/04 19:46:14 (permalink)
    0
    You have not shown all the code.
    i.e. there is no main() function calling this code.
    I suspect the problem is in the code you have not shown.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #2
    Aussie Susan
    Super Member
    • Total Posts : 3738
    • Reward points : 0
    • Joined: 2008/08/18 22:20:40
    • Location: Melbourne, Australia
    • Status: offline
    Re: PIC24 SPI RECEIVE ISR NOT WORKING 2020/04/05 19:55:42 (permalink)
    0
    Please use code tags.
    Also why have you commented out the PPS setting to map the SCK both ways (In and Out) as per the comment? Some MCUs (and this could well be one) requires the clock to be mapped both ways for the Rx side to work.
    Susan
    #3
    binaryman
    Super Member
    • Total Posts : 209
    • Reward points : 0
    • Joined: 2015/11/19 17:11:04
    • Location: Saint Louis, Missouri USA
    • Status: offline
    Re: PIC24 SPI RECEIVE ISR NOT WORKING 2020/04/06 07:04:38 (permalink)
    0
    April 6, 2020 , St Louis Missouri USA
    Good news .... There is nothing wrong with the ISR getting called on reads.  Its working fine. It was just me getting confused because there is so much code, so many layers and so many CPU's involved.   Last night I figured out what the problem was.  It was me not realizing the READ function was not getting called because of a RAM cache and many other layers.  There are four CPUs linked together via a ZIGBEE WIFI bridge. At initialization OTHER layers of code are reading everything into RAM and avoiding read hits after initialization.   Trust me you don't want me posting more code then I did.  There is allot of code.   
     
    Attached are pictures and one sheet of the schematic with the P24 and EEPROM
    post edited by binaryman - 2020/04/06 15:47:01

    Attached Image(s)

    #4
    binaryman
    Super Member
    • Total Posts : 209
    • Reward points : 0
    • Joined: 2015/11/19 17:11:04
    • Location: Saint Louis, Missouri USA
    • Status: offline
    Re: PIC24 SPI RECEIVE ISR NOT WORKING 2020/04/06 07:06:15 (permalink)
    1 (1)
    What are code tags ?
     
     
     
    #5
    rodims
    Super Member
    • Total Posts : 1557
    • Reward points : 0
    • Joined: 2009/02/10 11:08:59
    • Location: 51.9627, 7.6262
    • Status: online
    Re: PIC24 SPI RECEIVE ISR NOT WORKING 2020/04/06 09:14:13 (permalink)
    5 (1)
    See here: https://www.microchip.com/forums/m292988.aspx
    Edit your first post and add the tags, then you will see, what they do.
    #6
    binaryman
    Super Member
    • Total Posts : 209
    • Reward points : 0
    • Joined: 2015/11/19 17:11:04
    • Location: Saint Louis, Missouri USA
    • Status: offline
    Re: PIC24 SPI RECEIVE ISR NOT WORKING 2020/04/06 16:15:32 (permalink)
    0
    OK about the PPS setting to map the SCK both ways (In and Out) as per the comment? 
    This is an abandoned product from a company called enecsys that went out of business. I bought inventory from a bankruptcy liquidation, unpaid debt.  They manufactured 200,000 of these without finishing the software or any bootloaders.  I attached a zip file of one sheet of a schematic which shows how U16, EEPROM 25LC02A connects to the PIC24.   OK, I just looked at a backup before me changing anything.   I threw it away and wrote new code 4 years ago.  Because they were using 16 bit addressing and command so it wasnt working right.  They thought this EEPROM was 2k bytes.  Its only 2k bits (256 bytes) so the addressing is only 8 bits.  I'm guessing the comment was from a some spi example I was using that for something else. 
     
    Here is the fitted EPROM pin connections  
     
    EEPROM 25LC02A
    1 CS [Chip Select Input] to Pic pin 1 /RP9
    2 SO [Serial Data Output] to Pic pin 42Pic pin /RP6
    3 WP [Write-Protect] to Pic pin 38/RP21
    4 VSS
    5 SI [Serial Data Input] to Pic pin 37/RP20
    6 SCK [Serial Clock Input] to Pic pin 36/RP19
    7 HOLD [Hold Input] to Pic pin 34/T1CK
    8 VCC
     
    post edited by binaryman - 2020/04/06 19:15:29
    #7
    rodims
    Super Member
    • Total Posts : 1557
    • Reward points : 0
    • Joined: 2009/02/10 11:08:59
    • Location: 51.9627, 7.6262
    • Status: online
    Re: PIC24 SPI RECEIVE ISR NOT WORKING 2020/04/07 03:50:37 (permalink)
    4 (1)
    While you added code tags to your original post, you simply copied and pasted them from the Forum Guidelines.
    So you are copying HTML formatting attributes (may be more) from the HTML page, they are no longer pure formatting tags and thus will not work as tags.
    Simply delete your bold pasted formatting tags and manually write these tags into your post.
    And please use the preview feature, which would allow you to see what you are doing before posting. 
     

    int i;
    for (i=0;i<10;++)
       printf("That's what you should expect to see\n");

     
    post edited by rodims - 2020/04/07 03:52:13
    #8
    binaryman
    Super Member
    • Total Posts : 209
    • Reward points : 0
    • Joined: 2015/11/19 17:11:04
    • Location: Saint Louis, Missouri USA
    • Status: offline
    Re: PIC24 SPI RECEIVE ISR NOT WORKING 2020/04/07 06:15:23 (permalink)
    5 (1)
    April 7, 2020  8:07 
     
    Iv'e been coming to this forum 4 years.  I didn't realize/know about the TAGS.   Now I know.  The past 4 years I randomly have had allot of problems posting messages.  Sometimes you cant edit save anything , you get a permssion denied error.  It seems to be when you edit a post.  Its not on my end.  I have tried every web browser. I have several different computers and have tried logging out then back into forum.  Logging them out and back in.  I read that if you change your profile to mobile it helps.  Ive changed my profile to mobile it helps but randomly my profile goes back.  Maybe its me being in a hurry.  Ive got two small children, age 3 and 5, lots of craziness.  To survive you have to be in a hurry.
    #9
    1and0
    Access is Denied
    • Total Posts : 11000
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: PIC24 SPI RECEIVE ISR NOT WORKING 2020/04/07 07:04:11 (permalink)
    0
    dambrose
    Iv'e been coming to this forum 4 years.  I didn't realize/know about the TAGS.   Now I know.  The past 4 years I randomly have had allot of problems posting messages.  Sometimes you cant edit save anything , you get a permssion denied error.  It seems to be when you edit a post.  Its not on my end.  I have tried every web browser. I have several different computers and have tried logging out then back into forum.  Logging them out and back in.  I read that if you change your profile to mobile it helps.  Ive changed my profile to mobile it helps but randomly my profile goes back.  Maybe its me being in a hurry.  Ive got two small children, age 3 and 5, lots of craziness.  To survive you have to be in a hurry.

    Take a look to the workaround link in Ric's Post #2. I've few young children myself. :)
    #10
    binaryman
    Super Member
    • Total Posts : 209
    • Reward points : 0
    • Joined: 2015/11/19 17:11:04
    • Location: Saint Louis, Missouri USA
    • Status: offline
    Re: PIC24 SPI RECEIVE ISR NOT WORKING 2020/04/25 17:54:37 (permalink)
    0
    April 25. 2020 
     
    Just a follow up.  I got everything working just fine.  I have an ISR driven state machine for the SPI EEPROM.
    The writes to the EEPROM are FIFO/RING buffered with the address and value.  The SPI ISR drives a state machine which interfaces with the EEPROM.  Reads are done the same way but since mt EEPROM is only 256 bytes I'm storing a RAM cache of the entire EEPROM.  The entire 256 bytes of eerom are read in during boot/init.  So the result is the micro inverter firmware can run without any blocking.  
    #11
    Jump to:
    © 2020 APG vNext Commercial Version 4.5