/*
 * Characters are properly escaped and checksum is updated.
 *
 * \return 0 on success, -1 in case of any errors.
 */
static int SendHdlcData(AHDLCDCB * dcb, CONST uint8_t * data, uint16_t len, uint16_t * txfcs)
{
    uint16_t tbx;
    uint16_t fcs;

    if (txfcs)
        fcs = *txfcs;
    else
        fcs = 0;

    while (len) {
        tbx = (uint16_t) ((uint8_t) fcs ^ *data) << 1;
        fcs >>= 8;
        fcs ^= ((uint16_t) PRG_RDB(fcstab + tbx) << 8) | PRG_RDB(fcstab + tbx + 1);

        if (IN_ACC_MAP(*data, dcb->dcb_tx_accm) || *data == AHDLC_FLAG || *data == AHDLC_ESCAPE) {
            if (SendRawByte(dcb, AHDLC_ESCAPE, 0)) {
                return -1;
            }
            if (SendRawByte(dcb, *data ^ AHDLC_TRANS, 0)) {
                return -1;
            }
        } else if (SendRawByte(dcb, *data, 0)) {
            return -1;
        }
        data++;
        len--;
    }
    if (txfcs)
        *txfcs = fcs;

    return 0;
}
Ejemplo n.º 2
0
/*-----------------------------------------------------------------------------------*/
void
debug_print(prog_char *str)
{
  unsigned char i;

  for(i = 0; PRG_RDB(str + i) != 0; ++i) {
    buffer[i] = PRG_RDB(str + i);
  }
  print_buffer(i);
}
Ejemplo n.º 3
0
void ginv( iu8* v, const iu8* k, iu8 kidx )
{
#if 0
	v[W1_R] ^= fTable[k[(kidx+3)%10]^v[W1_L]];
	v[W1_L] ^= fTable[k[(kidx+2)%10]^v[W1_R]];
	v[W1_R] ^= fTable[k[kidx+1]^v[W1_L]];
	v[W1_L] ^= fTable[k[kidx]^v[W1_R]];
#endif
	v[W1_R] ^= PRG_RDB(fTable+(k[(kidx+3)%10]^v[W1_L]));
	v[W1_L] ^= PRG_RDB(fTable+(k[(kidx+2)%10]^v[W1_R]));
	v[W1_R] ^= PRG_RDB(fTable+(k[kidx+1]^v[W1_L]));
	v[W1_L] ^= PRG_RDB(fTable+(k[kidx]^v[W1_R]));
}
Ejemplo n.º 4
0
void g( iu8* v, const iu8* k, iu8 kidx )
{
#if 0
	v[W1_L] ^= fTable[k[kidx++]^v[W1_R]];
	v[W1_R] ^= fTable[k[kidx++]^v[W1_L]];
	if( kidx>=10 ) kidx-=10;
	v[W1_L] ^= fTable[k[kidx++]^v[W1_R]];
	v[W1_R] ^= fTable[k[kidx++]^v[W1_L]];
#endif
	v[W1_L] ^= PRG_RDB(fTable+(k[kidx++]^v[W1_R]));
	v[W1_R] ^= PRG_RDB(fTable+(k[kidx++]^v[W1_L]));
	if( kidx>=10 ) kidx-=10;
	v[W1_L] ^= PRG_RDB(fTable+(k[kidx++]^v[W1_R]));
	v[W1_R] ^= PRG_RDB(fTable+(k[kidx++]^v[W1_L]));
}
Ejemplo n.º 5
0
Archivo: usbdrv.c Proyecto: unhold/avr
static uchar    usbRead(uchar *data, uchar len)
{
#if USB_CFG_IMPLEMENT_FN_READ
    if(usbMsgFlags & USB_FLG_USE_DEFAULT_RW){
#endif
        uchar i = len, *r = usbMsgPtr;
        if(usbMsgFlags & USB_FLG_MSGPTR_IS_ROM){    /* ROM data */
            while(i--){
                uchar c = PRG_RDB(r);    /* assign to char size variable to enforce byte ops */
                *data++ = c;
                r++;
            }
        }else{                  /* RAM data */
            while(i--)
                *data++ = *r++;
        }
        usbMsgPtr = r;
        return len;
#if USB_CFG_IMPLEMENT_FN_READ
    }else{
        if(len != 0)    /* don't bother app with 0 sized reads */
            return usbFunctionRead(data, len);
        return 0;
    }
#endif
}
Ejemplo n.º 6
0
void wyslijtekstROM(char *tekst)  //wysyłanie danych z pamięci programu
{
 romram=1;                   //dane będą z pamięci programu
 pfifosio=tekst;            //ustaw wskaźnik na dane do wysłania
 sbi(UCR,TXEN);              //włącz nadajnik
 UDR=PRG_RDB(pfifosio++);   //wyślij pierwszy znak, pozostałe będą pobierane
                             //w procedurze obsługi przerwania TXC
}
Ejemplo n.º 7
0
/*!
 * \brief Send characters from progam memory to debug device 0.
 *
 * A carriage return character will be automatically appended
 * to any linefeed.
 *
 * \return Number of characters sent.
 */
static int DebugWrite_P(NUTFILE * fp, PGM_P buffer, int len)
{
    int c = len;
    PGM_P cp = buffer;

    while(c--) {
        DebugPut(PRG_RDB(cp));
        cp++;
    }
    return len;
}
Ejemplo n.º 8
0
/*-----------------------------------------------------------------------------------*/
void
debug_print8(unsigned char v)
{
  buffer[0] = v / 100 + '0';
  buffer[1] = (v / 10) % 10 + '0';
  buffer[2] = v % 10 + '0';
  buffer[3] = ' ';
  buffer[4] = PRG_RDB(hextab + (v >> 4));
  buffer[5] = PRG_RDB(hextab + (v & 0x0f));    
  buffer[6] = '\n';
  print_buffer(7);
}
Ejemplo n.º 9
0
char *encode_base64(const char* str)
{
    char    *encoded;
    size_t  length;
    size_t  encoded_length;
    int     idx, enc_pos;
    int32_t bits;
    int     cols;
    int     char_count; 
    
    /* Calculate buffer size for encoded data 
     * 8 Bit code converted into 6 Bit code ==> encoded code will be 4/3 larger
     * The algorithm will padd the encoded data with == so that the resulting 
     * length can be exactly deviced by 3
     * Also every 72 chars a <cr><lf> will be added
     */
    
    length = strlen(str);
    
    /* Calc the base code length, add one to the inital length to have space for rounding errors */
    encoded_length = ((length + 1) * 4) / 3;
    /* Add the size for the padding characters */
    encoded_length += encoded_length % 3;
    /* Now add the space for the inserted <cr><lf> characters and add one byte for the end of string NUL character*/
    encoded_length += (encoded_length / 72) * 2 + 3;
    /* Allocate the memory. */;
    encoded = calloc(1,encoded_length);
    
    if (encoded == 0) return 0;
    
    enc_pos = 0;
    char_count = 0;
    bits = 0;
    cols = 0;
    
    for (idx = 0; idx < length; idx ++) {
        bits += (int32_t)str[idx];
        char_count ++;
        if (char_count == 3) {
            encoded[enc_pos++] = PRG_RDB(&base64etab[(bits >> 18) & 0x3f]);
            encoded[enc_pos++] = PRG_RDB(&base64etab[(bits >> 12) & 0x3f]);
            encoded[enc_pos++] = PRG_RDB(&base64etab[(bits >> 6) & 0x3f]);
            encoded[enc_pos++] = PRG_RDB(&base64etab[bits & 0x3f]);
            cols += 4;
            if (cols == 72) {
                encoded[enc_pos++] = '\r';
                encoded[enc_pos++] = '\n';
                cols =0;
            }
            bits = 0;
            char_count = 0;
        } else {
Ejemplo n.º 10
0
void oscWriteHeader(char *address, u08 arglen)
{
	u08 len;
	// write OSC packet header
	bufferAddToEnd(uartGetTxBuffer(), OSC_HEADER);
	// determine padded length of address
	for(len=0; PRG_RDB(address+len); len++);
	len++;						// count a minumum of one null for termination
	if(len&0x03)				// are pad bytes necessary?
		len += 4-(len&0x03);	// add null pad bytes to reach multiple of four
	// write length to packet header
	bufferAddToEnd(uartGetTxBuffer(), len+arglen);
}
Ejemplo n.º 11
0
/*-----------------------------------------------------------------------------------*/
void
debug_print16(unsigned short v)
{
  buffer[0] = v / 10000 + '0';
  buffer[1] = (v / 1000) % 10 + '0';
  buffer[2] = (v / 100) % 10 + '0';
  buffer[3] = (v / 10) % 10 + '0';
  buffer[4] = v % 10 + '0';
  buffer[5] = ' ';
  buffer[6] = PRG_RDB(hextab + ((v & 0xf000) >> 12));
  buffer[7] = PRG_RDB(hextab + ((v & 0x0f00) >> 8));
  buffer[8] = PRG_RDB(hextab + ((v & 0xf0) >> 4));
  buffer[9] = PRG_RDB(hextab + (v & 0x0f));    
  buffer[10] = '\n';
  print_buffer(11);
}
Ejemplo n.º 12
0
void ds18x20_select_device(unsigned char *mem_array, unsigned char mem_type) 
{

unsigned char x=0;

if(mem_type == LOCATION_IS_RAM) 
{  
    for(x=0; x<8; x++) { *(ds18x20_rom_code+x)=(*(mem_array+x)); }  
} 
else if(mem_type == LOCATION_IS_FLASH) 
      {  
         for(x=0; x<8; x++) { *(ds18x20_rom_code+x)=PRG_RDB((const unsigned char*)(mem_array+x)); } 
      } 
else if(mem_type == LOCATION_IS_EEPROM) 
      { 
         for(x=0; x<8; x++) { ds18x20_rom_code[x]=eeprom_rb((unsigned int)(mem_array+x)); } 
      }

return; 
} 
Ejemplo n.º 13
0
void oscSendMessageString(char *address, char *arg)
{
	u08 len;

	// calculate length of argument string
	for(len=0; PRG_RDB(arg+len); len++);
	len++;						// count a minumum of one null for termination
	if(len&0x03)				// are pad bytes necessary?
		len += 4-(len&0x03);	// add null pad bytes to reach multiple of four

	// write OSC packet header, argument length = 8bytes
	oscWriteHeader(address, len);
	// write OSC address to packet
	oscWriteString(address);
	// write string buffer
	oscWriteString(arg);
	// apply checksum
	oscWriteChecksum();
	// send buffer
	uartSendTxBuffer();
	// wait for completion, transmitter to be ready
	while(!uartReadyTx);
}
Ejemplo n.º 14
0
void oscWriteString(char *string)
{
	u08 temp=1;
	u08 len=0;

	// write OSC string to packet
	// copy string's null-termination intentionally
	while(temp)
	{
		temp = PRG_RDB(string++);
		bufferAddToEnd(uartGetTxBuffer(), temp);
		len++;
	}

	// pad the string as necessary to reach a 4-byte multiple
	// Note: (len&0x03) == (len%4)
	//for(; (len&0x03); len++)
	while(len&0x03)
	{
		bufferAddToEnd(uartGetTxBuffer(), 0);
		len++;
	}
}
Ejemplo n.º 15
0
static uchar    usbRead(uchar *data, uchar len)
{
#if USB_CFG_IMPLEMENT_FN_READ
    if(usbMsgFlags & USB_FLG_USE_DEFAULT_RW){
#endif
        uchar i = len, *r = usbMsgPtr;
        if(usbMsgFlags & USB_FLG_MSGPTR_IS_ROM){    /* ROM data */
            while(i--){
                uchar c = PRG_RDB(r);    /* assign to char size variable to enforce byte ops */
#ifdef USB_CFG_HID_REPORT_DESCRIPTOR_RUNTIME
		if ((usbDescrConfig + 18 + 7) == (void*)r) {
			*data++ = rt_usbHidReportDescriptorSize;
		}
		else {
			*data++ = c;
		}
#else
		*data++ = c;
#endif
		
                r++;
            }
        }else{                  /* RAM data */
            while(i--)
                *data++ = *r++;
        }
        usbMsgPtr = r;
        return len;
#if USB_CFG_IMPLEMENT_FN_READ
    }else{
        if(len != 0)    /* don't bother app with 0 sized reads */
            return usbFunctionRead(data, len);
        return 0;
    }
#endif
}
Ejemplo n.º 16
0
void sendprgstr(char *s)
{
  char c;
  while ((c = PRG_RDB(s++)))
    SendByte(c);
}
Ejemplo n.º 17
0
void kgets(unsigned char *data, unsigned char max_chars, unsigned char x, unsigned char y)
{
 
unsigned char buffer=0, aux_buffer1=0, aux_buffer2=0;                          /* char buffers          */
unsigned char index=0, index_counter=0,  max=3, capital=0;                     /* char array controls   */
unsigned char backspace_hit_counter=0, keystroke_counter=0;                    /* char editor controls  */
                                          
#if ACTIVATE_AUTO_ADVANCE == 1 
unsigned char auto_advance=0;                                                 /* auto advance semaphore */
unsigned int  auto_advance_counter=0;                                         /* cursor advance timer   */
#endif

/* char array containing the keyboard characters */
static const char  key1[7]__attribute__((progmem))=  {' ','?','!',',','.',':','1' };
static const char  key2[4]__attribute__((progmem))=  {'a','b','c','2' };
static const char  key3[4]__attribute__((progmem))=  {'d','e','f','3' };
static const char  key4[4]__attribute__((progmem))=  {'g','h','i','4' };
static const char  key5[4]__attribute__((progmem))=  {'j','k','l','5' };
static const char  key6[4]__attribute__((progmem))=  {'m','n','o','6' };
static const char  key7[5]__attribute__((progmem))=  {'p','q','r','s','7' };
static const char  key8[4]__attribute__((progmem))=  {'t','u','v','8' };
static const char  key9[5]__attribute__((progmem))=  {'w','x','y','z','9' };
static const char key10[7]__attribute__((progmem))=  {'0','+','-','*','/','%','#' };

/* Because the lcd display advances automatically every time a char is displayed
   a lcd_gotoxy() command must be issued after any lcd_put command 
   in order to reposition the cursor to the desired position!!!                  */

/* Initialization */ 
for(index=0;index<=max_chars;index++) { *(data+index)='\0'; } /* Fill buffer with nulls */
if(max_chars) max_chars=max_chars-1; else return;             /* Adjust the char limit  */ 
index=0;
LCD_CURSOR_ON_BLINK(); 
     
/*------------------------------------------------------------------------------------------------------*/
while(1)  
    {  /*main loop start */
/* position cursor, wait for all keys to be released and wait 0,1 sec to prevent key double click */
      LCD_GOTOXY(x, y);  while((buffer=scan_keyb())); wait(DELAY_100_MS);       
                                                   
#if ACTIVATE_AUTO_ADVANCE == 1 
      while( !(buffer=scan_keyb()) )                         /* Cursor auto advance routine */
          {
            
            auto_advance_counter++;
            if( auto_advance && (auto_advance_counter>=CURSOR_ADVANCE_VALUE) && index<max_chars )
             {
               if((x==LCD_CHARS_PER_LINE-1) && (y<(LCD_LINES-1)) ) { x=0; y++; }
               else if(x<LCD_CHARS_PER_LINE-1) { x++; }  LCD_GOTOXY(x,y); 
               index++; 
               auto_advance=0;  keystroke_counter=0; backspace_hit_counter=1;
               aux_buffer1='\0'; index_counter=0;
             }
          }
#else
      while( !(buffer=scan_keyb()) );                              /* Just wait untill a key is pressed */
#endif                
/*------------------------------------------------------------------------------------------------------*/
      if( buffer==ENTER )                              /* ENTER */
       { 
         for(index=(max_chars+1);index;index--)        /* Trim trailing spaces  */
           { 
             if( *(data+index)=='\0' || *(data+index)==' ' ) { *(data+index)='\0'; } else { break; }
           }
         break;
         
       }     /* Terminate string and exit */
       
/*------------------------------------------------------------------------------------------------------*/
      /* ESCAPE  Turn off cursor and set string to zero */
      else if( buffer==ESCAPE ) { *data='\0'; break; }
             
/*------------------------------------------------------------------------------------------------------*/
      else if( buffer==CAPITAL ) { capital=~capital;  }  /* a->A */           

/*------------------------------------------------------------------------------------------------------*/
      else if(buffer==BACKSPACE)                         /* B  BACKSPACE                              */
            {                                     
              *(data+index)=' ';                         /* write a NULL to string and a space to lcd */
              LCD_PUTC(' '); 
              backspace_hit_counter++;                   /* BACKSPACE consecutive hit counter         */
              if(backspace_hit_counter==2 && index)      /* backwards movement control                */
               {
                 index--; 
                 if((x==0) && (y!=0)) { x=(LCD_CHARS_PER_LINE-1) ; y--; } else if(x>0) x--;
                 if(*(data+index)==' ') backspace_hit_counter=1; else backspace_hit_counter=0; 
               }
              aux_buffer1='\0'; index_counter=0;  keystroke_counter=0;
#if ACTIVATE_AUTO_ADVANCE == 1
              auto_advance=0; auto_advance_counter=0;
#endif                           
              
            } 
/*-----------------------------------------------------------------------------------------------------*/
#if ACTIVATE_ARROWS == 1
      else if(buffer==MOVE_LEFT)                         /* LEFT ARROW <--  */
            {
              if(index)  
               {
                 index--;
                 if((x==0) && (y!=0)) { x=(LCD_CHARS_PER_LINE-1) ; y--; } else if(x>0) x--;
                 aux_buffer1='\0'; index_counter=0; keystroke_counter=0;
#if ACTIVATE_AUTO_ADVANCE == 1
                 auto_advance=0; auto_advance_counter=0;
#endif                 
               }  
            }  
   
/*-----------------------------------------------------------------------------------------------------*/
      else if(buffer==MOVE_RIGHT)                       /* RIGHT ARROW -->  */
            {
              if( index<max_chars ) 
               {
                 if(*(data+index)=='\0') { *(data+index)=' '; }
                 index++; 
                 if((x==LCD_CHARS_PER_LINE-1) && (y<(LCD_LINES-1)) ) { x=0; y++; }
                 else if(x<LCD_CHARS_PER_LINE-1) { x++; }  LCD_GOTOXY(x,y); 
                 if(*(data+index)=='\0') { *(data+index)=' '; LCD_PUTC(' '); }
                 aux_buffer1='\0'; index_counter=0; 
                 keystroke_counter=0;
#if ACTIVATE_AUTO_ADVANCE == 1
                 auto_advance=0; auto_advance_counter=0;
#endif                                  
               }     
            }  

#endif              
/*------------------------------------------------------------------------------------------------------*/
      else if(index<=max_chars)  /* start of write char to array if block */  
            { /* If the same key is pressed more than once */
              if(buffer==aux_buffer1) { index_counter++; if(index_counter>=max) index_counter=0;  } 
              else index_counter=0; /* else if not the same key print the first array char */ 
              switch(buffer)   /* copy the appropriate char from the array to aux_buffer2  */
                   {
                     case(KEY1): max=sizeof(key1);   aux_buffer2=PRG_RDB(&key1[index_counter]);   break; 
                     case(KEY2): max=sizeof(key2);   aux_buffer2=PRG_RDB(&key2[index_counter]);   break; 
                     case(KEY3): max=sizeof(key3);   aux_buffer2=PRG_RDB(&key3[index_counter]);   break;
                     case(KEY4): max=sizeof(key4);   aux_buffer2=PRG_RDB(&key4[index_counter]);   break;
                     case(KEY5): max=sizeof(key5);   aux_buffer2=PRG_RDB(&key5[index_counter]);   break;
                     case(KEY6): max=sizeof(key6);   aux_buffer2=PRG_RDB(&key6[index_counter]);   break;
                     case(KEY7): max=sizeof(key7);   aux_buffer2=PRG_RDB(&key7[index_counter]);   break;
                     case(KEY8): max=sizeof(key8);   aux_buffer2=PRG_RDB(&key8[index_counter]);   break;
                     case(KEY9): max=sizeof(key9);   aux_buffer2=PRG_RDB(&key9[index_counter]);   break;
                     case(KEY10): max=sizeof(key10); aux_buffer2=PRG_RDB(&key10[index_counter]);  break;
                     default  : continue; /* if other key start the loop again */
                        
                   }   
               /* if it is not the same key or it is the first keystroke */
               if(buffer!=aux_buffer1) 
                {
                  aux_buffer1=buffer; backspace_hit_counter=0; keystroke_counter++;
                  /* If it is not the first keystroke advance the cursor */
                  if( keystroke_counter>=2 && index<max_chars )
                   {
                     if((x==LCD_CHARS_PER_LINE-1) && (y<(LCD_LINES-1)) ) { x=0; y++; }
                     else if(x<LCD_CHARS_PER_LINE-1) { x++; }  LCD_GOTOXY(x,y); 
                     index++;   backspace_hit_counter=0;
                   }
                }
               /* If capital letters wanted convert small to capital */
               if( capital && (aux_buffer2>='a' && aux_buffer2<='z') ) aux_buffer2-=32;
               
               /* Write char to destination array and LCD screen */
               *(data+index)=aux_buffer2;             
               LCD_PUTC(*(data+index));
#if ACTIVATE_AUTO_ADVANCE == 1                
               auto_advance=1; auto_advance_counter=0;
#endif               
            }           /*  end of write char to array if block */ 
             
    }                   /*main loop end                         */            
LCD_CURSOR_OFF();       /* Turn off cursor and exit             */     
return;
}
/*! \fn AhdlcRx(void *arg)
 * \brief Asynchronous HDLC receiver thread.
 *
 *
 * Running at high priority.
 */
THREAD(AhdlcRx, arg)
{
    NUTDEVICE *dev = arg;
    NUTDEVICE *netdev;
    AHDLCDCB *dcb = dev->dev_dcb;
    IFNET *ifn;
    NETBUF *nb;
    uint8_t *rxbuf;
    uint8_t *rxptr;
    uint16_t rxcnt;
    uint8_t ch;
    uint16_t tbx;
    uint8_t inframe;
    uint8_t escaped;
    uint16_t rxfcs;

    NutThreadSetPriority(9);
    for (;;) {
        /*
         * Reset variables to their initial state
         */
        rxptr = 0;
        rxcnt = 0;
        escaped = 0;
        rxfcs = AHDLC_INITFCS;
        inframe = 0;

        for (;;) {
            /*
             * Wait until the network interface has been attached.
             * This will be initiated by the application calling
             * NutNetIfConfig(), which in turn calls a HDLC_SETIFNET
             * ioctl() to store the NUTDEVICE pointer of the network
             * device in dev_icb and trigger an event on dcb_mf_evt.
             */
            while ((netdev = dev->dev_icb) == 0) {
                if (NutEventWait(&dcb->dcb_mf_evt, 1000) == 0) {
                    NutSleep(100);
                }
            }
            ifn = netdev->dev_icb;
            dcb->dcb_rtimeout = 1000;
            inframe = 0;

            /*
             * Allocate the receive buffer, if this fails, we are in a
             * low memory situation. Take a nap and see, if the
             * situation improved.
             */
            if ((rxbuf = NutHeapAlloc(dcb->dcb_rx_mru)) != 0) {
                break;
            }
            NutSleep(1000);
        }

        /*
         * Signal the link driver that we are up.
         */
        ifn->if_send = AhdlcOutput;
        netdev->dev_ioctl(netdev, LCP_LOWERUP, 0);

        for (;;) {
            /*
             * If we are still connected to a network, fetch the next
             * character from the buffer.
             */
            while (dcb->dcb_rd_idx == dcb->dcb_rx_idx) {
                if (dev->dev_icb == 0)
                    break;
                // TODO: Check for idle timeout. 
                if (NutEventWait(&dcb->dcb_rx_rdy, dcb->dcb_rtimeout)) {
                    continue;
                }
            }

            /*
             * Leave loop if network interface is detached
             */
            if (dev->dev_icb == 0)
                break;

            /*
             * If RAW mode is active, we are not allowing any data encapsulation
             * processing. So we just sleep for a while.
             */
            if (dcb->dcb_modeflags & UART_MF_RAWMODE) {
                /*
                 * It is a must to sleep here, because if we just yield it could create
                 * too much processing in here and stall processing elsewhere. This gives
                 * opportunity to other threads to process incoming data from USART.
                 */
                NutSleep(100);
                continue;
            }

            /*
             * Read next character from input buffer
             */
            ch = dcb->dcb_rx_buf[dcb->dcb_rd_idx++];

            if (inframe) {
                if (ch != AHDLC_FLAG) {
                    if (ch == AHDLC_ESCAPE) {
                        escaped = 1;
                        continue;
                    }
                    if (escaped) {
                        ch ^= AHDLC_TRANS;
                        escaped = 0;
                    }

                    /*
                     * Unless the peer lied to us about the negotiated MRU,
                     * we should never get a frame which is too long. If it
                     * happens, toss it away and grab the next incoming one.
                     */
                    if (rxcnt++ < dcb->dcb_rx_mru) {
                        /* Update calculated checksum and store character in buffer. */
                        tbx = (uint16_t) ((uint8_t) rxfcs ^ ch) << 1;
                        rxfcs >>= 8;
                        rxfcs ^= ((uint16_t) PRG_RDB(fcstab + tbx) << 8) | PRG_RDB(fcstab + tbx + 1);
                        *rxptr++ = ch;
                    } else
                        inframe = 0;
                    continue;
                }

                if (rxcnt > 6 && rxfcs == AHDLC_GOODFCS) {
                    /*
                       * If the frame checksum is valid, create a NETBUF
                       * and pass it to the network specific receive handler.
                     */
                    rxcnt -= 2;
                    if ((nb = NutNetBufAlloc(0, NBAF_DATALINK, rxcnt)) != 0) {
                        memcpy(nb->nb_dl.vp, rxbuf, rxcnt);
                        (*ifn->if_recv) (netdev, nb);
                    }
                }
            }

            /*
             * If frame flag is received, resync frame processing
             */
            if (ch == AHDLC_FLAG) {
                inframe = 1;
                escaped = 0;
                rxptr = rxbuf;
                rxcnt = 0;
                rxfcs = AHDLC_INITFCS;
            }
        }
Ejemplo n.º 19
0
/*!
 * \brief EEPROM emulator.
 *
 * Forces the chip to re-read the EEPROM contents and emulates a serial
 * EEPROM.
 *
 * If the hardware does not support this feature, then this call will
 * never return. Thus, make sure to have the driver properly configured.
 */
static void EmulateNicEeprom(void)
{
#ifdef __AVR_ENHANCED__
    register uint8_t clk;
    register uint8_t cnt;
    register uint8_t val;

    /*
     * Prepare the EEPROM emulation port bits. Configure the EEDO and
     * the EEMU lines as outputs and set EEDO to low and EEMU to high.
     */
    outb(PORTC, 0xC0);
    outb(DDRC, 0xC0);

    /*
     * Start EEPROM configuration. Stop/abort any activity and select
     * configuration page 3. Setting bit EEM0 will force the controller
     * to read the EEPROM contents.
     */

    /* Select page 3, stop and abort/complete. */
    nic_outlb(NIC_CR, NIC_CR_STP | NIC_CR_RD2 | NIC_CR_PS0 | NIC_CR_PS1);
    nic_outlb(NIC_PG3_EECR, NIC_EECR_EEM0);

    /*
     * We can avoid wasting port pins for EEPROM emulation by using the
     * upper bits of the address bus.
     */
    /*
     * No external memory access beyond this point.
     */
#if defined(__AVR_AT90CAN128__) || defined(__AVR_ATmega2561__)
    cbi(XMCRA, SRE);
#else
    cbi(MCUCR, SRE);
#endif

    /*
     * Loop for all EEPROM words.
     */
    for(cnt = 0; cnt < sizeof(nic_eeprom); ) {

        /*
         *
         * 1 start bit, always high
         * 2 op-code bits
         * 7 address bits
         * 1 dir change bit, always low
         */
        for(clk = 0; clk < 11; clk++) {
            while(bit_is_clear(RTL_EESK_PIN, RTL_EESK_BIT));
            while(bit_is_set(RTL_EESK_PIN, RTL_EESK_BIT));
        }

        /*
         * Shift out the high byte, MSB first. Our data changes at the EESK
         * rising edge. Data is sampled by the Realtek at the falling edge.
         */
        val = PRG_RDB(nic_eeprom + cnt);
        cnt++;
        for(clk = 0x80; clk; clk >>= 1) {
            while(bit_is_clear(RTL_EESK_PIN, RTL_EESK_BIT));
            if(val & clk)
                sbi(RTL_EEDO_PORT, RTL_EEDO_BIT);
            while(bit_is_set(RTL_EESK_PIN, RTL_EESK_BIT));
            cbi(RTL_EEDO_PORT, RTL_EEDO_BIT);
        }

        /*
         * Shift out the low byte.
         */
        val = PRG_RDB(nic_eeprom + cnt);
        cnt++;
        for(clk = 0x80; clk; clk >>= 1) {
            while(bit_is_clear(RTL_EESK_PIN, RTL_EESK_BIT));
            if(val & clk)
                sbi(RTL_EEDO_PORT, RTL_EEDO_BIT);
            while(bit_is_set(RTL_EESK_PIN, RTL_EESK_BIT));
            cbi(RTL_EEDO_PORT, RTL_EEDO_BIT);
        }


        /* 5 remaining clock cycles. */
        for(clk = 0; clk < 5; clk++) {
            while(bit_is_clear(RTL_EESK_PIN, RTL_EESK_BIT));
            while(bit_is_set(RTL_EESK_PIN, RTL_EESK_BIT));
        }
    }

    /*
     * Enable memory interface.
     */
#if defined(__AVR_AT90CAN128__) || defined(__AVR_ATmega2561__)
    sbi(XMCRA, SRE);
#else
    sbi(MCUCR, SRE);
#endif

    /* Reset port outputs to default. */
    outb(PORTC, 0x00);
    outb(DDRC, 0x00);
#endif
}
Ejemplo n.º 20
0
int main(void)
{
	unsigned char i;
	char * _ptr;
	const char * _ptr1;

	//Initialize display buffer with StartUp strings
	strcpy_P(&str_array[vga_symbols_per_row*18],&str1[0]); 	
	strcpy_P(&str_array[vga_symbols_per_row*19],&str2[0]); 	
	strcpy_P(&str_array[vga_symbols_per_row*0],&str3[0]); 	
	strcpy_P(&str_array[vga_symbols_per_row*5],&str4[0]); 	
	strcpy_P(&str_array[vga_symbols_per_row*10],&str5[0]); 	
    avr_init();
	for(;;)
    {

		//Wait compare interrupt signal from Timer1
		sleep_mode();
		
		
		//Check visible field
		if(video_enable_flg)
		{
		//OK, visible
		//Main render routine
				//Set pointer for render line (display bufffer)
				_ptr = &str_array[raw_render * vga_symbols_per_row];

				//Set pointer for render line (character generator)
				_ptr1 = &symbol[0][y_line_render >> 1];
	
				//Cycle for render line 
				i = vga_symbols_per_row;
				while(i--)
				{
					SPDR = PRG_RDB(_ptr1 + (* _ptr++)*symbol_height/2);
				    video_on;
					//That's a great pity can't shift data faster (8Mhz at FOSC=16Mhz)!!
					NOP;
					NOP;
				}
				//Delay for draw last symbol
				NOP;
				NOP;
				NOP;
				NOP;
				NOP;
				NOP;
				video_off;

		}
		else
		{
		//Not visible
		//Can do something else..	
		//*****Users handlers example
					static unsigned int framecount, time;
					//Simplest task switcher (linecount<45 not visible)
					switch ( linecount )
					{
					  //Task for make ADC0 convertion
					  case  0:  
						//Enable conversion only at the begin of video line count
						//to avoid flickering at the top display
						ADCSRA |= 1<<ADSC;//start the single conversion 
						while(bit_is_set(ADCSRA, ADSC));
					  break;
					  
					  //Task for refresh ADC0 convertion result
					  case  3:
						_display_adc(196*ADCH);
					  break;

					  //Task for show and count time value
					  case  9: 
						//Count frame for time count
						framecount++;
						//Time count
						if (framecount==60)
						{
							framecount = 0;
							//Running timer only when PinC.1 = "0";
							if (bit_is_clear(PINC,1))
							{
							//Increment every second
							_display_time(++time);
							}
							else
							{
								time = 0;
							}
						}
					  break;

					  //Default task Check and show state of PINC.1
					  default: 
						if (bit_is_set(PINC,1))
							str_array[7] = '1';
						else
							str_array[7] = '0';
					}
		//*****Users handlers example
		}
		
    }//for(;;)