<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-gb">
	<link rel="self" type="application/atom+xml" href="https://forums.obdev.at/app.php/feed/topic/10499" />

	<title>Objective Development Forums</title>
	
	<link href="https://forums.obdev.at/index.php" />
	<updated>2016-07-05T16:13:36+02:00</updated>

	<author><name><![CDATA[Objective Development Forums]]></name></author>
	<id>https://forums.obdev.at/app.php/feed/topic/10499</id>

		<entry>
		<author><name><![CDATA[sriharir_91]]></name></author>
		<updated>2016-07-05T16:13:36+02:00</updated>

		<published>2016-07-05T16:13:36+02:00</published>
		<id>https://forums.obdev.at/viewtopic.php?t=10499&amp;p=31344#p31344</id>
		<link href="https://forums.obdev.at/viewtopic.php?t=10499&amp;p=31344#p31344"/>
		<title type="html"><![CDATA[V USB HID sensor data logger]]></title>

		
		<content type="html" xml:base="https://forums.obdev.at/viewtopic.php?t=10499&amp;p=31344#p31344"><![CDATA[
Hi,<br /><br />I am trying to design a device which logs the sensor data and presents itself as a keyboard to print out the values to a notepad.<br /><br />The design is as follows: Sensor -&gt; atmega8(transmitter) ----- (Via UART)---&gt; atmega8(receiver) ----(via USB)---&gt; PC<br /><br />However, the code only works for the first time.. i.e., the first time i plug my device, it gets recognized and the values get typed into a notepad. But the second time i plug my device, it isn't recognized. I tried it out on different systems with the same result. <br /><br />for the UART, I've used a 9600 bps baud rate.<br />for communicating with PC, I've used a HID protocol. (Without the UART, If I type out random values, it works all the time)<br /><br />Please find the transmitter code snippet which is responsible for the UART:<br /><div class="codebox"><p>Code: </p><pre><code>void ComputerDlg()<br />{<br />    unsigned char a,i; <br />    while(1)<br />    {<br />        a=uart_getc();<br />        if(a=='X')<br />        {<br />            uart_putc('X'); //// signature <br />         uart_putc(log_count);   <br />        }                  <br />        else if(a=='A') /// sync log memory<br />        {                            <br />            for(i=0;i&lt;50;i++)<br />            {<br />                uart_putc(logs&#91;i&#93;.hour);<br />                _delay_ms(EEPROM_DELAY); <br />                uart_putc(logs&#91;i&#93;.minute);<br />                _delay_ms(EEPROM_DELAY);<br />                uart_putc(logs&#91;i&#93;.second);<br />                _delay_ms(EEPROM_DELAY);<br />                uart_putc(logs&#91;i&#93;.day);<br />                _delay_ms(EEPROM_DELAY);<br />                uart_putc(logs&#91;i&#93;.month);<br />                _delay_ms(EEPROM_DELAY);<br />                uart_putc(logs&#91;i&#93;.year);<br />                _delay_ms(EEPROM_DELAY);<br />                uart_putc(logs&#91;i&#93;.percentage/100);<br />                _delay_ms(EEPROM_DELAY);<br />                uart_putc(logs&#91;i&#93;.percentage%100);<br />                _delay_ms(EEPROM_DELAY);                <br />            }    <br />        }<br />        else if(a=='Z')<br />        {<br />            for(i=0;i&lt;50;i++)<br />            {<br />                logs&#91;i&#93;.hour=0xFF;<br />                _delay_ms(EEPROM_DELAY);<br />                logs&#91;i&#93;.minute=0xFF;<br />                _delay_ms(EEPROM_DELAY);<br />                logs&#91;i&#93;.second=0xFF;<br />                _delay_ms(EEPROM_DELAY);<br />                logs&#91;i&#93;.day=0xFF;<br />                _delay_ms(EEPROM_DELAY);<br />                logs&#91;i&#93;.month=0xFF;<br />                _delay_ms(EEPROM_DELAY);<br />                logs&#91;i&#93;.year=0xFF;<br />                _delay_ms(EEPROM_DELAY);<br />                logs&#91;i&#93;.percentage=0xFFFF;<br />                _delay_ms(EEPROM_DELAY);<br />            }<br />        }<br />    }<br />    <br />}<br /></code></pre></div><br /><br />Please find the receiver code below:<br /><br /><div class="codebox"><p>Code: </p><pre><code>#define F_CPU 12000000L            //for util/delay.h<br />#define EEPROM_DELAY        5<br />#define HIDSERIAL_INBUFFER_SIZE 32<br /><br />#include &lt;avr/io.h&gt;<br />#include &lt;avr/wdt.h&gt;<br />#include &lt;avr/eeprom.h&gt;<br />#include &lt;avr/interrupt.h&gt;<br />#include &lt;avr/pgmspace.h&gt;<br />#include &lt;stdlib.h&gt;<br />#include &lt;string.h&gt;               //For parsing data into strings<br />#include &lt;util/delay.h&gt;<br /><br />#include &quot;./usbdrv/usbdrv.h&quot;      //USB Header File<br />#include &quot;./usbdrv/oddebug.h&quot;   <br /><br />uchar logs&#91;50&#93;&#91;8&#93;;<br />static uchar    reportBuffer&#91;2&#93;;    // buffer for HID reports <br />static uchar    idleRate;           // in 4 ms units<br />int i=0;<br />int value=0;<br />int m=0;<br /><br />/************************USB HID Keyboard report descriptor*************************/<br /><br />PROGMEM const char usbHidReportDescriptor&#91;USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH&#93; = <br />{<br />    0x05, 0x01,                    //   USAGE_PAGE (Generic Desktop)<br />    0x09, 0x06,                    //   USAGE (Keyboard)<br />    0xa1, 0x01,                    //   COLLECTION (Application)<br />    0x05, 0x07,                    //   USAGE_PAGE (Keyboard)<br />    0x19, 0xe0,                    //   USAGE_MINIMUM (Keyboard LeftControl)<br />    0x29, 0xe7,                    //   USAGE_MAXIMUM (Keyboard Right GUI)<br />    0x15, 0x00,                    //   LOGICAL_MINIMUM (0)<br />    0x25, 0x01,                    //   LOGICAL_MAXIMUM (1)<br />    0x75, 0x01,                    //   REPORT_SIZE (1)<br />    0x95, 0x08,                    //   REPORT_COUNT (8)<br />    0x81, 0x02,                    //   INPUT (Data,Var,Abs)<br />    0x95, 0x01,                    //   REPORT_COUNT (1)<br />    0x75, 0x08,                    //   REPORT_SIZE (8)<br />    0x25, 0x65,                    //   LOGICAL_MAXIMUM (101)s<br />    0x19, 0x00,                    //   USAGE_MINIMUM (Reserved (no event indicated))<br />    0x29, 0x65,                    //   USAGE_MAXIMUM (Keyboard Application)<br />    0x81, 0x00,                    //   INPUT (Data,Ary,Abs)<br />    0xc0                           //   END_COLLECTION<br />};<br /><br />//Initialize I/O<br />void io_init()<br />{<br />   /******************UART initialisation********************/<br />   DDRD|=1&lt;&lt;PIND1;<br />   DDRD&amp;=~(1&lt;&lt;PIND0);<br />    UCSRA=0x00;<br />    UCSRB=0x18;<br />    UCSRC=0x86;<br />    UBRRH=0x00;<br />    UBRRL=0x4D;      //BAUD RATE = 9600 bps<br />   <br />   /****************Emptying the log buffer******************/<br />   int a,b;<br />   <br />   for(a=0;a&lt;50;a++)<br />   {<br />      for(b=0;b&lt;8;b++)<br />      {<br />         logs&#91;a&#93;&#91;b&#93;='\0';<br />      }<br />   }<br />}<br /><br />//UART Transmit function<br />void uart_putc(uint8_t c)      <br />{<br />   while(!(UCSRA &amp; (1 &lt;&lt; UDRE)));<br />   UDR = c;<br />}<br /><br />//UART Receive function<br />uint8_t uart_getc()            <br />{<br />   uint8_t b;<br />   while(!(UCSRA &amp; (1 &lt;&lt; RXC)));   //Blocking call - wait until receive buffer is full<br />   b = UDR; <br />   return b;<br />}<br /><br />//Receive the data from main controller and buffer it in RAM<br />void buff_log()<br />{<br />   unsigned char a;<br />   <br />   int k=0;<br />   uart_putc('X');               //Handshake<br />   a=uart_getc();<br />   <br />   a='X';<br />   while (k==0)               //Polling<br />   {<br />   <br />      if(a=='X'||a=='x')         //Signature authentication<br />      {<br />         k=1;<br />         uart_putc('A');         //Sync log memory<br />         <br />         int j;<br />         for(j=0;j&lt;50;j++)<br />         {<br />            logs&#91;j&#93;&#91;0&#93;=uart_getc();<br />            logs&#91;j&#93;&#91;1&#93;=uart_getc();<br />            logs&#91;j&#93;&#91;2&#93;=uart_getc();<br />            logs&#91;j&#93;&#91;3&#93;=uart_getc();<br />            logs&#91;j&#93;&#91;4&#93;=uart_getc();<br />            logs&#91;j&#93;&#91;5&#93;=uart_getc();<br />            logs&#91;j&#93;&#91;6&#93;=uart_getc();<br />            logs&#91;j&#93;&#91;7&#93;=uart_getc();      <br />         }<br />      <br />      }   //End of if() block<br />   }      //end of while() polling loop<br />   <br />}         //End of buff_log()<br /><br />/*******************************Parsing the data into a string************************************/<br />static void buildReport()<br />{   <br />   memset(reportBuffer,'\0',sizeof(reportBuffer));<br />   <br />   reportBuffer&#91;0&#93;=0;<br />   if(value&lt;=7)<br />   {<br />      reportBuffer&#91;1&#93;=logs&#91;i&#93;&#91;value&#93;;   <br />   }<br />   else if(value&gt;7)<br />   {<br />      reportBuffer&#91;1&#93;=28;<br />   }<br />}<br />/*************************************************************************************************/<br /><br />USB_PUBLIC uchar usbFunctionSetup(uchar data&#91;8&#93;) <br />{<br />   usbRequest_t *rq = (void *)data;               //Casting the data to correct type<br />       <br />      usbMsgPtr =  reportBuffer;<br />      <br />       if((rq-&gt;bmRequestType &amp; USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS)<br />      {                                    // class request type<br />          if(rq-&gt;bRequest == USBRQ_HID_GET_REPORT)   // wValue: ReportType (highbyte), ReportID (lowbyte) <br />         {  <br />            buildReport();                  // we only have one report type, so don't look at wValue<br />            return sizeof(reportBuffer);         // The entire buffer has to be sent at one shot. <br />         }<br />            <br />         else if(rq-&gt;bRequest == USBRQ_HID_GET_IDLE)<br />         {<br />             usbMsgPtr = &amp;idleRate;<br />             return 1;<br />         }<br />         <br />         else if(rq-&gt;bRequest == USBRQ_HID_SET_IDLE)<br />         {<br />             idleRate = rq-&gt;wValue.bytes&#91;1&#93;;<br />         }<br />      }<br />      <br />      else<br />      {<br />          /* no vendor specific requests implemented */<br />       }<br />      <br />   return 0;                           <br /><br />/******************************************MAIN FUNCTION******************************************/<br />int main(void)<br />{<br />   int j=0;<br />/******************************************* Initialization***************************************/   <br />   io_init();<br />   sei();               //Enable global interrupts<br />   buff_log();            //To retrieve logs from the main controller unit<br />   odDebugInit();<br />   usbDeviceDisconnect();   //Enforce enumeration by disconnecting and connecting for the first time<br />   <br />   for(j=0;j&lt;20;j++)      // 300 ms disconnect<br />   {   <br />      _delay_ms(15);<br />   }<br />   <br />   usbDeviceConnect();<br />   <br />   wdt_enable(WDTO_1S);   //Enable the watchdog timer<br />   usbInit();<br />   <br />/*******************************************Loop forever******************************************/   <br />   while(1)<br />    {   <br />      wdt_reset();                  //Reset the watchdog timer<br />      usbPoll();<br />       j=0;    <br />      if(usbInterruptIsReady()&amp;&amp;i&lt;50)      // we can send another data<br />      { <br />         buildReport();<br />         value++;<br />         <br />         if(value&gt;7)<br />         {<br />            i++;<br />            value=0;<br />         }<br />         usbSetInterrupt(reportBuffer, sizeof(reportBuffer));<br />      <br />         if(i&gt;=50)<br />         {<br />            i=0;<br />         }<br />      <br />      }<br />   }//End of while(1) Infinite loop<br /><br />return 0;<br />}//End of main()<br /></code></pre></div><p>Statistics: Posted by <a href="https://forums.obdev.at/memberlist.php?mode=viewprofile&amp;u=22095">sriharir_91</a> — Tue Jul 05, 2016 4:13 pm</p><hr />
]]></content>
	</entry>
	</feed>
