CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

SPI read from normal code and ISR code advice (AD7902)

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
allenhuffman



Joined: 17 Jun 2019
Posts: 648
Location: Des Moines, Iowa, USA

View user's profile Send private message Visit poster's website

SPI read from normal code and ISR code advice (AD7902)
PostPosted: Mon Jan 26, 2026 4:17 pm     Reply with quote

With the PCD PIC24 compiler, if you have any function that is called from an ISR, and also used outside of the ISR, the compiler generates warnings and will mask interrupts around this function. That is an interesting feature that means the developer does not have to do that work. (This makes me uncomfortable.)

I am looking into some of our code where we read a SPI part (AD7902). It connects to an RF detector, and the RF signal can be CW (constant) or PWM (pulsing).

This board generates the RF signal, and then uses the PIC24 pwm_on() capability to do the pulsing. This allows the code to use an ISR to catch when the pulse happens, and read the RF detector during the pulses.

The architect of this code created duplicate functions - such as:

Code:

readDetectors();

readDetectorsISR();


Likely, they saw the compiler warnings and split the code out to remove the warnings and not have to mask when doing the SPI reads when not in PWM mode.

They also initialize two SPI streams, which I assume is done to create two separate blocks of SPI code:

Code:
#use spi(MASTER, SPI2, MODE=1, baud=8000000, stream=SPI_MODE1)

#use spi(MASTER, SPI2, MODE=1, baud=8000000, stream=SPI_MODE1ISR)


While this approach has been in use and working fine for the past decade, I thought I'd ask the PCD experts here:

Is there a better approach?

The PWM and non-PWM routines can never be active at the same time, so I wonder if there is a way to mark a function as "I know what I am doing" and then just manually mask ISRs but only if in PWM mode.

Or something else.

Tips appreciated.
_________________
Allen C. Huffman, Sub-Etha Software (est. 1990) http://www.subethasoftware.com
Embedded C, Arduino, MSP430, ESP8266/32, BASIC Stamp and PIC24 programmer.
http://www.whywouldyouwanttodothat.com ?

Using: 24FJ256GA106, 24EP256GP202 and 24FJ64GA002.
Ttelmah



Joined: 11 Mar 2010
Posts: 20025

View user's profile Send private message

PostPosted: Mon Jan 26, 2026 10:56 pm     Reply with quote

The duplication is the way to avoid this. However using this on the SPI
hardware, worries me. Problem is that since this is hardware, it is a single
resource. If the 'main' code is running and operates the SPI, and while
this is happening the ISR triggers and does another operation on the
SPI, the flags on the return will not be those from the main operation, and
the byte sent in the ISR will come between two being sent in the master.
This would be a problem. I suspect this does not happen, because the 'main'
operations on the SPI, stop while the ISR is running?. If this is the case, then
really the problem need never occur. I think you need to look carefully at
when the transactions actually occur. If the two operations are actually
separated like this, then just be explicit and change where the interrupts
are enabled so the problem disappears.
allenhuffman



Joined: 17 Jun 2019
Posts: 648
Location: Des Moines, Iowa, USA

View user's profile Send private message Visit poster's website

PostPosted: Tue Jan 27, 2026 8:43 am     Reply with quote

Thanks --- for this application, we are either generating a pulsed PWM signal, or a CW constant signal, so only one is used at a time.

CCS has confirmed that the I/O is not interrupts (already protected) but user code that is doing multiple SPI or I2C calls in series could obviously be interrupted.

If duplicating code is the way it is done here, then so be it. Less rework for me Smile

I am going to make in inquiry with CCS to see if there is a #pragma or similar override that will prevent the IRQ masking around functions, which would allow us to do it manually when we know it needs to be done.
_________________
Allen C. Huffman, Sub-Etha Software (est. 1990) http://www.subethasoftware.com
Embedded C, Arduino, MSP430, ESP8266/32, BASIC Stamp and PIC24 programmer.
http://www.whywouldyouwanttodothat.com ?

Using: 24FJ256GA106, 24EP256GP202 and 24FJ64GA002.
Ttelmah



Joined: 11 Mar 2010
Posts: 20025

View user's profile Send private message

PostPosted: Tue Jan 27, 2026 10:54 am     Reply with quote

I wonder if the NESTED_INTERRUPTS setup might accidentally do this?.
Might just be worth trying?.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group