XGATE semaphore question

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

XGATE semaphore question

Jump to solution
583 Views
roberthiebert
Senior Contributor I

MC9S12XEP100, CW Special Edition, USBDM, XGATE in RAM, coding in relocatable assembler.

I have a project that was written in absolute assembler which uses a lot of interrupts. It runs just fine now, but I want to incorporate XGATE to handle most, if not all of the interrupts. Why? Probably just to see if I can. To that end, I have been able to convert the project to relocatable assembler, (which wasn't easy for me) but it seems to work.

Anyway, I spend a lot of time studying the reference manuals and AN2685, but I 'm still not quite clear on the basics of XGATE semaphores. 

When I change an interrupt service routine to be serviced by XGATE instead of the CPU, I have read that semaphores need to be involved, but I m not clear under just what circumstances.

Do they need to be used just when a variable is modified within an ISR serviced by XGATE, and, modified by an ISR service by the CPU?

Or.

Also, when a variable is modified within an ISR serviced by XGATE, but it is only used by the CPU, but not modified?

Regards,

Robert 

0 Kudos
Reply
1 Solution
554 Views
lama
NXP TechSupport
NXP TechSupport

Hi,

Semaphores are needed only when both cores (S12X and XGATE) access the same shared resource, typically a variable in RAM. The are needed to avoid simultaneous R/W from both cores. For example, CPU writes some data in one or two steps and XGATE reads them at the same moment. Data consistency is not guaranteed.

 

Use semaphores when:

  1. A variable is modified by both XGATE and S12X ISRs.

Example: A status flag or counter updated by both cores. Or writing to a port.

  1. A variable is modified by one core and read by the other, and the read must be consistent.

Example: A buffer index updated by XGATE and read by S12X in a loop.

You do not need semaphores when:

  • A variable is only modified by XGATE, and only read by S12X, and consistency is not critical (e.g., occasional stale reads are acceptable).
  • A variable is only accessed by one core (either read or write).

 

Note, I found in my old code following. It is probably not important for some mask sets but better to implement to be sure it works everywhere.

....  *.cxgate module

interrupt void PIT0isr(void)

{

      //--- Protected software region starts     // AN2685.pdf

     //-----------------------------------------

      // use this

      asm LOOP1:

      asm SSEM #0 // try to lock semaphore 0

      asm SSEM #0 // try to lock semaphore 0     // once more due to internal silicon bug

      asm BCC LOOP1 // retry if locked

      //-----------------------------------------

      //or this

      //asm SSEM #0 // try to lock semaphore 0

      //asm SSEM #0 // try to lock semaphore 0     // once more due to internal silicon bug

      //asm BCC PIT0isr_RTI // return without flag clearing to enable service of an interrupt with higher priority

      //-----------------------------------------

 

    ....

    .....

    ....

      //-----------------------------------------
      asm CSEM #0 // release semaphore
      //-----------------------------------------

asm PIT0isr_RTI:

      //-----------------------------------------
      PITTF = 0x01; // clear interrupt flag of PIT0isr

}

Best regards,

Ladislav

View solution in original post

0 Kudos
Reply
2 Replies
555 Views
lama
NXP TechSupport
NXP TechSupport

Hi,

Semaphores are needed only when both cores (S12X and XGATE) access the same shared resource, typically a variable in RAM. The are needed to avoid simultaneous R/W from both cores. For example, CPU writes some data in one or two steps and XGATE reads them at the same moment. Data consistency is not guaranteed.

 

Use semaphores when:

  1. A variable is modified by both XGATE and S12X ISRs.

Example: A status flag or counter updated by both cores. Or writing to a port.

  1. A variable is modified by one core and read by the other, and the read must be consistent.

Example: A buffer index updated by XGATE and read by S12X in a loop.

You do not need semaphores when:

  • A variable is only modified by XGATE, and only read by S12X, and consistency is not critical (e.g., occasional stale reads are acceptable).
  • A variable is only accessed by one core (either read or write).

 

Note, I found in my old code following. It is probably not important for some mask sets but better to implement to be sure it works everywhere.

....  *.cxgate module

interrupt void PIT0isr(void)

{

      //--- Protected software region starts     // AN2685.pdf

     //-----------------------------------------

      // use this

      asm LOOP1:

      asm SSEM #0 // try to lock semaphore 0

      asm SSEM #0 // try to lock semaphore 0     // once more due to internal silicon bug

      asm BCC LOOP1 // retry if locked

      //-----------------------------------------

      //or this

      //asm SSEM #0 // try to lock semaphore 0

      //asm SSEM #0 // try to lock semaphore 0     // once more due to internal silicon bug

      //asm BCC PIT0isr_RTI // return without flag clearing to enable service of an interrupt with higher priority

      //-----------------------------------------

 

    ....

    .....

    ....

      //-----------------------------------------
      asm CSEM #0 // release semaphore
      //-----------------------------------------

asm PIT0isr_RTI:

      //-----------------------------------------
      PITTF = 0x01; // clear interrupt flag of PIT0isr

}

Best regards,

Ladislav

0 Kudos
Reply
537 Views
roberthiebert
Senior Contributor I

Hi Ladislav,

Thanks for the clarification. That's how I thought it should work, but I wasn't sure.

Regards,

Robert

0 Kudos
Reply
%3CLINGO-SUB%20id%3D%22lingo-sub-2195354%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3EXGATE%20semaphore%20question%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2195354%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3EMC9S12XEP100%2C%20CW%20Special%20Edition%2C%20USBDM%2C%20XGATE%20in%20RAM%2C%20coding%20in%20relocatable%20assembler.%3C%2FP%3E%3CP%3EI%20have%20a%20project%20that%20was%20written%20in%20absolute%20assembler%20which%20uses%20a%20lot%20of%20interrupts.%20It%20runs%20just%20fine%20now%2C%20but%20I%20want%20to%20incorporate%20XGATE%20to%20handle%20most%2C%20if%20not%20all%20of%20the%20interrupts.%20Why%3F%20Probably%20just%20to%20see%20if%20I%20can.%20To%20that%20end%2C%20I%20have%20been%20able%20to%20convert%20the%20project%20to%20relocatable%20assembler%2C%20(which%20wasn't%20easy%20for%20me)%20but%20it%20seems%20to%20work.%3C%2FP%3E%3CP%3EAnyway%2C%20I%20spend%20a%20lot%20of%20time%20studying%20the%20reference%20manuals%20and%20AN2685%2C%20but%20I%20'm%20still%20not%20quite%20clear%20on%20the%20basics%20of%20XGATE%20semaphores.%26nbsp%3B%3C%2FP%3E%3CP%3EWhen%20I%20change%20an%20interrupt%20service%20routine%20to%20be%20serviced%20by%20XGATE%20instead%20of%20the%20CPU%2C%20I%20have%20read%20that%20semaphores%20need%20to%20be%20involved%2C%20but%20I%20m%20not%20clear%20under%20just%20what%20circumstances.%3C%2FP%3E%3CP%3EDo%20they%20need%20to%20be%20used%20just%20when%20a%20variable%20is%20modified%20within%20an%20ISR%20serviced%20by%20XGATE%2C%20and%2C%20modified%20by%20an%20ISR%20service%20by%20the%20CPU%3F%3C%2FP%3E%3CP%3EOr.%3C%2FP%3E%3CP%3EAlso%2C%20when%20a%20variable%20is%20modified%20within%20an%20ISR%20serviced%20by%20XGATE%2C%20but%20it%20is%20only%20used%20by%20the%20CPU%2C%20but%20not%20modified%3F%3C%2FP%3E%3CP%3ERegards%2C%3C%2FP%3E%3CP%3ERobert%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2195950%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%20translate%3D%22no%22%3ERe%3A%20XGATE%20semaphore%20question%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2195950%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3EHi%20Ladislav%2C%3C%2FP%3E%3CP%3EThanks%20for%20the%20clarification.%20That's%20how%20I%20thought%20it%20should%20work%2C%20but%20I%20wasn't%20sure.%3C%2FP%3E%3CP%3ERegards%2C%3C%2FP%3E%3CP%3ERobert%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2195763%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%20translate%3D%22no%22%3ERe%3A%20XGATE%20semaphore%20question%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2195763%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3EHi%2C%3C%2FP%3E%0A%3CP%3ESemaphores%20are%20needed%20%3CSTRONG%3Eonly%20when%20both%20cores%20(S12X%20and%20XGATE)%20access%20the%20same%20shared%20resource%3C%2FSTRONG%3E%2C%20typically%20a%20variable%20in%20RAM.%20The%20are%20needed%20to%20avoid%20simultaneous%20R%2FW%20from%20both%20cores.%20For%20example%2C%20CPU%20writes%20some%20data%20in%20one%20or%20two%20steps%20and%20XGATE%20reads%20them%20at%20the%20same%20moment.%20Data%20consistency%20is%20not%20guaranteed.%3C%2FP%3E%0A%3CBR%20%2F%3E%0A%3CP%3E%3CSTRONG%3EUse%20semaphores%20when%3A%3C%2FSTRONG%3E%3C%2FP%3E%0A%3COL%3E%0A%3CLI%3E%3CSTRONG%3EA%20variable%20is%20modified%20by%20both%20XGATE%20and%20S12X%20ISRs.%3C%2FSTRONG%3E%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%3EExample%3A%20A%20status%20flag%20or%20counter%20updated%20by%20both%20cores.%20Or%20writing%20to%20a%20port.%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3COL%3E%0A%3CLI%3E%3CSTRONG%3EA%20variable%20is%20modified%20by%20one%20core%20and%20read%20by%20the%20other%3C%2FSTRONG%3E%2C%20and%20the%20read%20must%20be%20consistent.%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%3EExample%3A%20A%20buffer%20index%20updated%20by%20XGATE%20and%20read%20by%20S12X%20in%20a%20loop.%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EYou%20do%20%3CEM%3Enot%3C%2FEM%3E%20need%20semaphores%20when%3A%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3EA%20variable%20is%20%3CSTRONG%3Eonly%20modified%20by%20XGATE%3C%2FSTRONG%3E%2C%20and%20%3CSTRONG%3Eonly%20read%20by%20S12X%3C%2FSTRONG%3E%2C%20and%20%3CSTRONG%3Econsistency%20is%20not%20critical%3C%2FSTRONG%3E%20(e.g.%2C%20occasional%20stale%20reads%20are%20acceptable).%3C%2FLI%3E%0A%3CLI%3EA%20variable%20is%20%3CSTRONG%3Eonly%20accessed%20by%20one%20core%3C%2FSTRONG%3E%20(either%20read%20or%20write).%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CBR%20%2F%3E%0A%3CP%3ENote%2C%20I%20found%20in%20my%20old%20code%20following.%20It%20is%20probably%20not%20important%20for%20some%20mask%20sets%20but%20better%20to%20implement%20to%20be%20sure%20it%20works%20everywhere.%3C%2FP%3E%0A%3CP%3E....%26nbsp%3B%20*.cxgate%20module%3C%2FP%3E%0A%3CP%3Einterrupt%20void%20PIT0isr(void)%3C%2FP%3E%0A%3CP%3E%7B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%2F%2F---%20Protected%20software%20region%20starts%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%2F%2F%20AN2685.pdf%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%2F%2F-----------------------------------------%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%2F%2F%20use%20this%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20asm%20LOOP1%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20asm%20SSEM%20%230%20%2F%2F%20try%20to%20lock%20semaphore%200%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20asm%20SSEM%20%230%20%2F%2F%20try%20to%20lock%20semaphore%200%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%2F%2F%20once%20more%20due%20to%20internal%20silicon%20bug%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20asm%20BCC%20LOOP1%20%2F%2F%20retry%20if%20locked%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%2F%2F-----------------------------------------%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%2F%2For%20this%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%2F%2Fasm%20SSEM%20%230%20%2F%2F%20try%20to%20lock%20semaphore%200%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%2F%2Fasm%20SSEM%20%230%20%2F%2F%20try%20to%20lock%20semaphore%200%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%2F%2F%20once%20more%20due%20to%20internal%20silicon%20bug%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%2F%2Fasm%20BCC%20PIT0isr_RTI%20%2F%2F%20return%20without%20flag%20clearing%20to%20enable%20service%20of%20an%20interrupt%20with%20higher%20priority%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%2F%2F-----------------------------------------%3C%2FP%3E%0A%3CBR%20%2F%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20....%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20.....%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20....%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%2F%2F-----------------------------------------%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20asm%20CSEM%20%230%20%2F%2F%20release%20semaphore%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%2F%2F-----------------------------------------%3C%2FP%3E%0A%3CP%3Easm%20PIT0isr_RTI%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%2F%2F-----------------------------------------%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20PITTF%20%3D%200x01%3B%20%2F%2F%20clear%20interrupt%20flag%20of%20PIT0isr%3C%2FP%3E%0A%3CP%3E%7D%3C%2FP%3E%0A%3CP%3EBest%20regards%2C%3C%2FP%3E%0A%3CP%3ELadislav%3C%2FP%3E%3C%2FLINGO-BODY%3E