Semaphores on 9S12XDP512

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

Semaphores on 9S12XDP512

2,548 Views
Sten
Contributor IV
I have problems getting a simple semaphore approach to work. The routine in the XGATE receives characters from the SCI0 and puts them in a ring-buffer and the S12X fetches the characters from the buffer for processing. The problem is that one character gets lost now and then (one lost char for about 100..200 correctly received char).
 
The XGATE code looks as follows:
   8:  interrupt void SCI_interrupt(void) {
 0000          L0:    
   9:     while (!_ssem(1)) { }
 0000 01f2             SSEM       #1
 0002 21fe             BCC        L0
  10:     (void)SCI0SR1;       /* read status register to reset flag */
 0004 f200             LDL        R2,#_SCI0SR1
 0006 4240             LDB        R2,(R2,#0)
  11:     rx_buffer[rx_end++] = SCI0DRL;    /* Read character */
 0008 f300             LDL        R3,#PART_0_7(rx_end)
 000a ab00             ORH        R3,#PART_8_15(rx_end)
 000c 4460             LDB        R4,(R3,#0)
 000e 4260             LDB        R2,(R3,#0)
 0010 e201             ADDL       R2,#1
 0012 5260             STB        R2,(R3,#0)
 0014 1212             MOV        R2,R4
 0016 f200             LDL        R2,#_SCI0DRL
 0018 4540             LDB        R5,(R2,#0)
 001a 1212             MOV        R2,R4
 001c e400             ADDL       R4,#PART_0_7(rx_buffer)
 001e ec00             ADDH       R4,#PART_8_15(rx_buffer)
 0020 5580             STB        R5,(R4,#0)
  12:     if (rx_end >= RX_BUF_SIZ) rx_end = 0;
 0022 4260             LDB        R2,(R3,#0)
 0024 d252             CMPL       R2,#82
 0026 2201             BCS        CCR, , L2A
 0028 5060             STB        R0,(R3,#0)
 002a          L2A:   
  13:     _csem(1);
 002a 01f0             CSEM       #1
  14:  }
 002c 0200             RTS       
 
and the routine in the S12X as follows:
 
 193:  BYTE getch_t(char *chr) {
 0000 3b           [2]     PSHD 
 194:     do { SET_SEM(1); } while (!TST_SEM(1));
 0001 cc0202       [2]     LDD   #514
 0004 7c0000       [3]     STD   _XGSEM
 0007 1f000002f5   [5]     BRCLR _XGSEM:1,#2,*-6 ;abs = 0001
 195:     if (rx_start == rx_end) { REL_SEM(1); return 0; } /* no char available, release semaphore and return */
 000c f60000       [3]     LDAB  rx_start
 000f f10000       [3]     CMPB  rx_end
 0012 2606         [3/1]   BNE   *+8 ;abs = 001a
 0014 c7           [1]     CLRB 
 0015 7c0000       [3]     STD   _XGSEM
 0018 201f         [3]     BRA   *+33 ;abs = 0039
 196:     *chr = rx_buffer[rx_start++];           /* read character */
 001a 37           [2]     PSHB 
 001b 52           [1]     INCB 
 001c 7b0000       [3]     STAB  rx_start
 001f ee81         [3]     LDX   1,SP
 0021 cd0000       [2]     LDY   #rx_buffer
 0024 33           [3]     PULB 
 0025 e6ed         [3]     LDAB  B,Y
 0027 6b00         [2]     STAB  0,X
 197:     REL_SEM(1);
 0029 c7           [1]     CLRB 
 002a 7c0000       [3]     STD   _XGSEM
 198:     if (rx_start >= RX_BUF_SIZ) rx_start = 0;
 002d f60000       [3]     LDAB  rx_start
 0030 c152         [1]     CMPB  #82
 0032 2503         [3/1]   BCS   *+5 ;abs = 0037
 0034 790000       [3]     CLR   rx_start
 199:     return 1;
 0037 c601         [1]     LDAB  #1
 200:  }
 0039 1b82         [2]     LEAS  2,SP
 003b 0a           [7]     RTC  
 
If I replace the SSEM #1, BCC L0 in the XGATE-routine with NOP's, everything works fine.
 
I know that these routines propably not would need semaphores, but this is the first (and simplest) routine I'm moving from the S12X to the XGATE, and the other more complex routines will certainly need semaphores, so it would be essential to get them working.
 
Does anybode have an idea what I'm doing wrong?
 
Labels (1)
0 Kudos
1 Reply

405 Views
Sten
Contributor IV

I did figure out what I was doing wrong; I had the interrupts enabled while doing the getch_t(), so if I got an interrupt while the semaphore was reserved by the S12X, the XGATE was prevented from reading the SCI receive buffer, which did cause an receive overflow.

Sorry to have bothered you.

Sten

 

0 Kudos