Interrupt based UART while running VUSB on ATmega8

Discussion in 'Embedded Systems and Microcontrollers' started by AkashG, Apr 14, 2016.

  1. AkashG

    Thread Starter New Member

    Apr 14, 2016
    2
    0
    I've built a wireless mouse that sends data through UART over RF to base station which is just an ATmega8 with VUSB Ckt and RF reciever connected.



    things that i've tried :



    1. Running UART on interrupt mode. This didnt work because VUSB already uses an interrupt and there was clashes between VUSb n UART .. so the device would disconnect from the computer or simply be not recognized.



    2. Then i've used while loops to capture data from Rf but the micro controller gets stuck on the while loop because the transmitter is not on and information is not received and hence the flag never gets set.



    What is a better solution for this .. my device acts as a HID compliant mouse ..
     
  2. shteii01

    AAC Fanatic!

    Feb 19, 2010
    3,386
    496
    Set your own "flag" variable. You can either use the size of received data as a flag or you can use time as a flag. Check if you received X bytes, if you did receive X bytes, then the "flag" variable is set, program exits while loop, executes whatever you want it to execute, resets the "flat" variable, goes back into while loop. Check the time, check if 100 milliseconds have passed, if it did, set the "flag" variable, exit while loop, do whatever you want to do, reset the "flag" variable, go back into while loop.
     
  3. AkashG

    Thread Starter New Member

    Apr 14, 2016
    2
    0
    I didnt get what u meant .. but here is the code ...
    Code (C):
    1.  
    2.  
    3. #define  F_CPU 16000000UL
    4. #define  NULL 0
    5.  
    6. #define  false 0
    7. #define  true 1
    8.  
    9. #include "usbdrv/usbdrv.h"
    10. #include "IMU.h"
    11. #include <util/delay.h>
    12. #include <avr/wdt.h>
    13. #include <avr/pgmspace.h>
    14. #include <avr/interrupt.h>
    15. #include <avr/io.h>
    16.  
    17. volatile uint8_t x=0,y=0;
    18. volatile uint8_t prev_x=0,prev_y=0;
    19.  
    20. volatile uchar xRecieved = false, yRecieved = false;
    21.  
    22. int resolution = 256;
    23.  
    24. #pragma region usb
    25.  
    26. PROGMEM const char usbHidReportDescriptor[52] = { /* USB report descriptor, size must match usbconfig.h */
    27.     0x05, 0x01,                    // USAGE_PAGE (Generic Desktop)
    28.     0x09, 0x02,                    // USAGE (Mouse)
    29.     0xa1, 0x01,                    // COLLECTION (Application)
    30.     0x09, 0x01,                    //   USAGE (Pointer)
    31.     0xA1, 0x00,                    //   COLLECTION (Physical)
    32.     0x05, 0x09,                    //     USAGE_PAGE (Button)
    33.     0x19, 0x01,                    //     USAGE_MINIMUM
    34.     0x29, 0x03,                    //     USAGE_MAXIMUM
    35.     0x15, 0x00,                    //     LOGICAL_MINIMUM (0)
    36.     0x25, 0x01,                    //     LOGICAL_MAXIMUM (1)
    37.     0x95, 0x03,                    //     REPORT_COUNT (3)
    38.     0x75, 0x01,                    //     REPORT_SIZE (1)
    39.     0x81, 0x02,                    //     INPUT (Data,Var,Abs)
    40.     0x95, 0x01,                    //     REPORT_COUNT (1)
    41.     0x75, 0x05,                    //     REPORT_SIZE (5)
    42.     0x81, 0x03,                    //     INPUT (Const,Var,Abs)
    43.     0x05, 0x01,                    //     USAGE_PAGE (Generic Desktop)
    44.     0x09, 0x30,                    //     USAGE (X)
    45.     0x09, 0x31,                    //     USAGE (Y)
    46.     0x09, 0x38,                    //     USAGE (Wheel)
    47.     0x15, 0x81,                    //     LOGICAL_MINIMUM (-127)
    48.     0x25, 0x7F,                    //     LOGICAL_MAXIMUM (127)
    49.     0x75, 0x08,                    //     REPORT_SIZE (8)
    50.     0x95, 0x03,                    //     REPORT_COUNT (3)
    51.     0x81, 0x06,                    //     INPUT (Data,Var,Rel)
    52.     0xC0,                          //   END_COLLECTION
    53.     0xC0,                          // END COLLECTION
    54. };
    55.  
    56. typedef struct{
    57.     uchar   buttonMask;
    58.     char    dx;
    59.     char    dy;
    60.     char    dWheel;
    61. }report_t;
    62.  
    63.  
    64. static report_t reportBuffer;
    65. static uchar    idleRate;
    66. #pragma endregion usb
    67. #pragma region usbFunction
    68. usbMsgLen_t usbFunctionSetup(uchar data[8])
    69. {
    70.     usbRequest_t    *rq = (void *)data;
    71.  
    72.     /* The following requests are never used. But since they are required by
    73.     * the specification, we implement them in this example.
    74.     */
    75.     if((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS)
    76.     {    /* class request type */
    77.         if(rq->bRequest == USBRQ_HID_GET_REPORT)
    78.         {  /* wValue: ReportType (highbyte), ReportID (lowbyte) */
    79.             /* we only have one report type, so don't look at wValue */
    80.             usbMsgPtr = (void *)&reportBuffer;
    81.             return sizeof(reportBuffer);
    82.         }
    83.         else if(rq->bRequest == USBRQ_HID_GET_IDLE){
    84.             usbMsgPtr = &idleRate;
    85.             return 1;
    86.         }
    87.         else if(rq->bRequest == USBRQ_HID_SET_IDLE){
    88.             idleRate = rq->wValue.bytes[1];
    89.         }
    90.     }
    91.     else{
    92.         /* no vendor specific requests implemented */
    93.     }
    94.     return 0;   /* default for not implemented requests: return no data back to host */
    95. }
    96. #pragma endregion usb
    97.  
    98.  
    99.  
    100. unsigned char Receive( void )
    101. {
    102.     /* Wait for data to be received */
    103.     while ( !(UCSRA & (1<<RXC)) );
    104.     /* Get and return received data from buffer */
    105.     return UDR;
    106. }
    107.  
    108. void USART_Init( unsigned int baud)
    109. {
    110.     unsigned int ubrr = ( F_CPU /  ( 16 * baud ) ) - 1;
    111.  
    112.     /* Set baud rate */
    113.     UBRRH = (ubrr>>8);
    114.     UBRRL = ubrr;
    115.  
    116.     /* Enable receiver*/
    117.     UCSRB |= (1<<RXEN);
    118.  
    119.     /*Enable the Interrupt mode*/
    120.     UCSRB |= (1<<RXCIE);
    121.  
    122.     /* Set frame format: 8data, 2stop bit */
    123.     UCSRC |= (1<<URSEL)|(1<<USBS)|(3<<UCSZ0);
    124. }
    125.  
    126.  
    127. int main(void)
    128. {
    129.  
    130.     DDRC=0xff;
    131.  
    132.     uchar i;
    133.  
    134.     wdt_enable(WDTO_1S);
    135.  
    136.     usbInit();
    137.     usbDeviceDisconnect();  /* enforce re-enumeration, do this while interrupts are disabled! */
    138.  
    139.     i = 0;
    140.  
    141.     while(--i){             /* fake USB disconnect for > 250 ms */
    142.         wdt_reset();
    143.         _delay_ms(1);
    144.     }
    145.  
    146.     usbDeviceConnect();
    147.  
    148.     sei();
    149.  
    150.     USART_Init(36482);
    151.  
    152.  
    153.     while(1)
    154.     {
    155.         wdt_reset();
    156.  
    157.         if( !(UCSRA & (1<<RXC)) )
    158.     x=UDR;
    159.     if( !(UCSRA & (1<<RXC)) )
    160.     y=UDR;
    161.  
    162.  
    163.  
    164.         reportBuffer.dx = x;
    165.         reportBuffer.dy = y;
    166.  
    167.         usbPoll();
    168.  
    169.         if(usbInterruptIsReady()){
    170.             /* called after every poll of the interrupt endpoint */
    171.             usbSetInterrupt((void *)&reportBuffer, sizeof(reportBuffer));
    172.         }
    173.  
    174.     }
    175.     return 0;
    176. }
    177.  
    Moderators note: Changed code tags.
     
Loading...