Comments

USB AVR Programmer and SPI interface. Adafruit's USBtinyISP.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
Hamito
 
Posts: 4
Joined: Mon May 07, 2012 12:07 pm

Comments

Post by Hamito »

Could somebody write more detailed comments to this parts of the code. I know what made these codes but i want to know what mean each line, and pls wrote comments to variables. Without them its really hard to understand code. Thank u very much for your help

Code: Select all

static	void	spi ( byte_t* cmd, byte_t* res, int i )
{
	byte_t	c;
	byte_t	r;
	byte_t	mask;

	while (i != 0)
	{
	  i--;
		c = *cmd++;
		r = 0;
		for	( mask = 0x80; mask; mask >>= 1 )
		{
			if	( c & mask )
			{
				PORT |= MOSI_MASK;
			}
			delay();
			PORT |= SCK_MASK;
			delay();
			r <<= 1;
			if	( PIN & MISO_MASK )
			{
				r++;
			}
			PORT &= ~ MOSI_MASK;
			PORT &= ~ SCK_MASK;
		}
		*res++ = r;
	}
}

Code: Select all

static	void	spi_rw ( void )
{
	uint_t	a;

	a = address++;
	if	( cmd0 & 0x80 )
	{	// eeprom
		a <<= 1;
	}
	cmd[0] = cmd0;
	if	( a & 1 )
	{
		cmd[0] |= 0x08;
	}
	cmd[1] = a >> 9;
	cmd[2] = a >> 1;
	spi( cmd, res, 4 );

Code: Select all

static	void	usb_transmit ( void )
{
	byte_t	len;
	byte_t*	src;
	byte_t*	dst;
	byte_t	i;
	byte_t	b;

	usb_tx_buf[0] ^= (USB_PID_DATA0 ^ USB_PID_DATA1);
	len = usb_tx_total;
	if	( len > 8 )
	{
		len = 8;
	}
	dst = usb_tx_buf + 1;
	if	( len > 0 )
	{
#if	USBTINY_CALLBACK_IN
		if	( usb_tx_state == TX_STATE_CALLBACK )
		{
			len = usb_in( dst, len );
		}
		else
#endif
		{
			src = usb_tx_data;
			if	( usb_tx_state == TX_STATE_RAM )
			{
				for	( i = 0; i < len; i++ )
				{
					*dst++ = *src++;
				}
			}
			else	// usb_tx_state == TX_STATE_ROM
			{
				for	( i = 0; i < len; i++ )
				{
					b = pgm_read_byte( src );
					src++;
					*dst++ = b;
				}
			}
			usb_tx_data = src;
		}
		usb_tx_total -= len;
	}
	crc( usb_tx_buf + 1, len );
	usb_tx_len = len + 3;
	if	( len < 8 )
	{	// this is the last packet
		usb_tx_state = TX_STATE_IDLE;
	}
}

Locked
Please be positive and constructive with your questions and comments.

Return to “USBtinyISP”