PDA

View Full Version : usart receive interrupt doesnt come true when usb keyboard emulation works.



celalaslan
1st February 2016, 20:39
I want to write to and read a mifare card reader module from serial ports and also send to pc via usb ports as keyboard emulation. I managed to run keyboard emulation program with timer1 and as well as timerless operation. The pic18f14k50 serial tx pin sends the message to reader and reader respond everytime. T traced from paralel connection to pc serial port input. But as you see clearly as below codes, PIR1bits_RCIF doest come true any time. I begin to think that i can not use usart, i2c or ssp peripherals when i use proton usb stack. Or are they possible? Please help me.
Device = 18F14K50 ' Choose a device with on-board full speed USB
Declare Xtal = 48 ' Inform the compiler we’re operating at 48MHz
On_Hardware_Interrupt GoTo ISR_Handler
Include "Keyboard_Descriptor.inc" ' Load the USB routines and descriptor into the program
Include "18f14k50.def"
Declare Optimiser_Level = 3
Declare Dead_Code_Remove = On


Dim Key As Byte
Dim RxBuf[40] As Byte
Dim RxPacketLen As Byte
Dim RxPacketCmd As Byte
Dim NextRxChar As Byte
Dim RxComplete As Byte
Dim i As Byte
Dim ii As Byte
Dim lownibble As Byte
Dim hnibble As Byte
Dim hextoscan As Byte
Dim index As Byte
Main:
INTCONbits_GIE=1
INTCONbits_PEIE=1
IPR1bits_RCIP=1 'eusart receive interrupt is high priority
PIE1bits_RCIE=1
TRISB= 111111
TXSTA= 100100
RCSTA= 010000
BAUDCONbits_BRG16=1
SPBRGH=0x02
SPBRG=0x70
DelayMS 10 ' Wait for things to stabilise
'--------------------------------------------------------------------------
RxPacketCmd=0
NextRxChar=0
RxComplete=0
While 1 = 1
For i=0 To 4
TXREG=CRead8 version[i]
Next i
Clear HID_Report_In
If USB_tAttached = True Then
HID_Report_In[2] =$04
HID_TxReport(HID_Report_In,8)
If RxComplete<>0 Then
HID_Report_In[2] =0x0A
HID_TxReport(HID_Report_In,8)
RxComplete=0
ii=4+RxPacketLen
For i=0 To ii
index= RxBuf[i]&0xF
hextoscan = LookUp index,[0x27,0x1E,0x1F,0x20,0x21,0x22,0x23,0x24,0x25,0x26, 0x4,0x5,0x6,0x7,0x8,0x9]
lownibble=hextoscan
index= RxBuf[i]&$F0
index=index>>4
hextoscan = LookUp index,[0x27,0x1E,0x1F,0x20,0x21,0x22,0x23,0x24,0x25,0x26, 0x4,0x5,0x6,0x7,0x8,0x9]
hnibble=hextoscan
HID_Report_In[2] =lownibble
HID_TxReport(HID_Report_In,8)
DelayMS 1
HID_Report_In[2] =hnibble
HID_TxReport(HID_Report_In,8)
DelayMS 1
HID_Report_In[2] =0x0
HID_TxReport(HID_Report_In,8)
Next ii
EndIf




EndIf
Wend



version: CData As Byte $FF,$00,$01,$80,$81
ISR_Handler:
Context Save ' Save any variables used in the interrupt

If PIR1bits_RCIF=1 Then

RxBuf[NextRxChar]=RCREG

Select NextRxChar
Case 0
NextRxChar= NextRxChar+1
If RxBuf[0]<>0xFF Then NextRxChar=0 'protokole uygun değil
Break;
Case 1
NextRxChar= NextRxChar+1
Break;
Case 2
NextRxChar= NextRxChar+1
RxPacketLen=RxBuf[2];

Break;
Case 3
NextRxChar= NextRxChar+1
RxPacketCmd=RxBuf[3];
Break;
Case Else
NextRxChar= NextRxChar+1
If NextRxChar>=RxPacketLen+4 Then
NextRxChar=0;
RxComplete=1;
EndIf
EndSelect
EndIf


Context Restore ' Restore any variables and exit the interrupt

Tim
1st February 2016, 21:28
There should be no reason you cannot use the USB stack and the USART at the same time.

But one thing to look at is from memory the new USB stack is interrupt driven. It may be using high level interrupts. Try setting up and using the low level interrupts.

Also checking the include files for code to indicate how the USB Poll is being handled.

hadv215
1st February 2016, 22:37
As far as I know the USB implentation is polling.
This comes from the latest manual "Notes. The method used for USB is polled"

Furthermore I get the impression you are either using an older version of the compiler since the latest version (3.5.7.7) does not recognize things like "HID_TxReport(HID_Report_In,8)" or you are using the USB stuff found in the new samples directory in your user directory.

celalaslan
1st February 2016, 22:44
Dear Tim.Thank you for your reply. Proton usb stack uses Low priority interrupt and declared at usb_userhid.bas file. Even so, i tried timerless routines and disabled low priority interrupt. Nothing changed. I sended characters to pc usb and saw them at notepad. but uart ever never operable to receive interrupt. Pic microcontrollers have no receive buffer full flag without interrupt. There should be another thing related with PIE1_RCIE bits. I doubt that usb stack slinces all interrupts except timer1.

celalaslan
1st February 2016, 22:45
Dear Tim.Thank you for your reply. Proton usb stack uses Low priority interrupt and declared at usb_userhid.bas file. Even so, i tried timerless routines and disabled low priority interrupt. Nothing changed. I sended characters to pc usb and saw them at notepad. but uart ever never operable to receive interrupt. Pic microcontrollers have no receive buffer full flag without interrupt. There should be another thing related with PIE1_RCIE bits. I doubt that usb stack slinces all interrupts except timer1.

celalaslan
1st February 2016, 22:48
My compiler version is 3.5.7.7 and i took the keyboard demo samples from new_sapmles directory that installed 2 weeks ago.

celalaslan
1st February 2016, 22:49
My compiler version is 3.5.7.7 and i took the keyboard demo samples from new_sapmles directory that installed 2 weeks ago.

hadv215
1st February 2016, 23:05
Ok, but I can not find the file "usb_userhid.bas" in that directory.
I took a look at the interrupt, it's basically a low level timer interrupt to keep the connection alive. That's not quite the same as using a "real" USB interrupt mechanism.
Depending on the amount of code executed this may disturb the UART timing.

I do not agree with you on the PIC not having a buffer flag without interrupts.
Look at the implementation of stuff like HSering. Furthermore read the datasheet: RCIF will be set on reception, interrupt or not.


Looked at your code. You do test the RCIF flag, but you do not reset it after reading the RXREG register.Furthermore you should check the other bits in the related registers to ensure continuous reception and error clearing

tumbleweed
2nd February 2016, 10:49
A couple of points:

- on the 14K50 the uart RX pin (RB5) defaults to being an analog pin (AN11) so that must be changed to digital mode by clearing ANSELH bit 3 (ANS11). Otherwise it'll always read '0'.

- when you go to transmit you should make sure there's room in the TX fifo each time you load TXREG

For i=0 To 4
TXREG=CRead8 version[i] <<< make sure TXIF is set before loading TXREG
Next i


- hadv215's right in that you should check for uart receive errors since an OERR will stop the uart from receiving any more chars.
The uart RCIF will automatically clear when you read the RCREG (it's read-only, so you can't clear it manually)

celalaslan
2nd February 2016, 10:56
Ok, but I can not find the file "usb_userhid.bas" in that directory.
I took a look at the interrupt, it's basically a low level timer interrupt to keep the connection alive. That's not quite the same as using a "real" USB interrupt mechanism.
Depending on the amount of code executed this may disturb the UART timing.


I do not agree with you on the PIC not having a buffer flag without interrupts.
Look at the implementation of stuff like HSering. Furthermore read the datasheet: RCIF will be set on reception, interrupt or not.


Looked at your code. You do test the RCIF flag, but you do not reset it after reading the RXREG register.Furthermore you should check the other bits in the related registers to ensure continuous reception and error clearing


The file name should be USB_UserHID.inc. It isnt bas file. MY mistake. RCIF bit is located in peripheral interrupt request register1(PIR). So if you want any interrupt to set these bits you should enable related enable bits in PIE register, in this situation it would be RCIE.
When i send a byte from TXREG, i can control it from TXSTA_TRMT bit whether TXREG is full or empty. Unfortunately there is no control bit like this for receive buffer. Only the way for know whether RXREG is full or empty, you should enable RCIE and checking bit RCIF. And when RCREG is read, rcif bit cleared automaticaly, rcif is r/o bit and no need to reset.

On the other hand UART module is working without any problem when i disable the usb routines. But at the inc files for usb stack, There is some routines for enabling, disabling GIE, and PEIE bits. I think these routines blocked the interrupts and i can not read receive buffer of uart.
celal

celalaslan
2nd February 2016, 11:17
Dear tubleweed. you are totaly right. I overlooked digital input buffer was always disabled. Now its working. My graditude knows no bound.

hadv215
2nd February 2016, 11:50
My mistake about clearing RCIF. I'm so used to clearing Interrupt Flags that I tend to forget when it is not necessary (or does not make any sense).