Example #1
0
void radio_spiReadReg(uint8_t reg, cc2420_status_t* statusRead, uint8_t* regValueRead) {
    uint8_t              spi_tx_buffer[3];
    uint8_t              spi_rx_buffer[3];

    spi_tx_buffer[0]     = (CC2420_FLAG_READ | CC2420_FLAG_REG | reg);
    spi_tx_buffer[1]     = 0x00;
    spi_tx_buffer[2]     = 0x00;

    spi_txrx(spi_tx_buffer,
             sizeof(spi_tx_buffer),
             SPI_BUFFER,
             spi_rx_buffer,
             sizeof(spi_rx_buffer),
             SPI_FIRST,
             SPI_LAST);

    *statusRead          = *(cc2420_status_t*)&spi_rx_buffer[0];
    *(regValueRead+0)    = spi_rx_buffer[2];
    *(regValueRead+1)    = spi_rx_buffer[1];
}
Example #2
0
uint8_t radio_spiReadRadioInfo(){
   uint8_t              spi_tx_buffer[3];
   uint8_t              spi_rx_buffer[3];

   // prepare buffer to send over SPI
   spi_tx_buffer[0]     =  (0x80 | 0x1E);        // [b7]    Read/Write:    1    (read)
   // [b6]    RAM/Register : 1    (register)
   // [b5-0]  address:       0x1E (Manufacturer ID, Lower 16 Bit)
   spi_tx_buffer[1]     =  0x00;                 // send a SNOP strobe just to get the reg value
   spi_tx_buffer[2]     =  0x00;                 // send a SNOP strobe just to get the reg value

   // retrieve radio manufacturer ID over SPI
   spi_txrx(spi_tx_buffer,
         sizeof(spi_tx_buffer),
         SPI_BUFFER,
         spi_rx_buffer,
         sizeof(spi_rx_buffer),
         SPI_FIRST,
         SPI_LAST);

   return spi_rx_buffer[2];
}
Example #3
0
static u8 sd_readblock( u32 addr, u8 *data ) {
	u8 tmp, i, resp[ R2 ];
	u24 count;
	u16 crc;

	addr <<= 9;

	//sd_waitidle();

	if( sd_send_command( CMD17_READ_SINGLE_BLOCK, &addr, resp ) != SD_ERR_OK ) return( SD_ERR_GENERIC );

	if( resp[ 0 ] ) return( SD_ERR_READ );

	sd_assert();

	// Wait (with timeout) for data token
	i = 0;
	do {
		tmp = spi_txrx( CMD_IDLE );
	} while( ++i != SD_READ_TIMEOUT && tmp == 0xFF );

	// Bail on error
	if( !( tmp & MSK_TOK_DATAERROR ) ) {
		spi_txrx( CMD_IDLE );
		sd_deassert();
		return( SD_ERR_READ );
	}

	// Read data
	count = 512;
	while( count-- ) {
		*data++ = spi_txrx( CMD_IDLE );
	}

	// Read CRC
	high8( crc ) = spi_txrx( CMD_IDLE );
	low8( crc ) = spi_txrx( CMD_IDLE );

	tmp = spi_txrx( CMD_IDLE );

	sd_deassert();

	return( SD_ERR_OK );
}
Example #4
0
void radio_spiReadRxFifo(uint8_t* pBufRead,
                         uint8_t* pLenRead,
                         uint8_t  maxBufLen,
                         uint8_t* pLqi) {
   // when reading the packet over SPI from the RX buffer, you get the following:
   // - *[1B]     dummy byte because of SPI
   // - *[1B]     length byte
   // -  [0-125B] packet (excluding CRC)
   // - *[2B]     CRC
   // - *[1B]     LQI
   uint8_t spi_tx_buffer[125];
   uint8_t spi_rx_buffer[3];
   
   spi_tx_buffer[0] = 0x20;
   
   // 2 first bytes
   spi_txrx(spi_tx_buffer,
            2,
            SPI_BUFFER,
            spi_rx_buffer,
            sizeof(spi_rx_buffer),
            SPI_FIRST,
            SPI_NOTLAST);
   
   *pLenRead  = spi_rx_buffer[1];
   
   if (*pLenRead>2 && *pLenRead<=127) {
      // valid length
      
      //read packet
      spi_txrx(spi_tx_buffer,
               *pLenRead,
               SPI_BUFFER,
               pBufRead,
               125,
               SPI_NOTFIRST,
               SPI_NOTLAST);
      
      // CRC (2B) and LQI (1B)
      spi_txrx(spi_tx_buffer,
               2+1,
               SPI_BUFFER,
               spi_rx_buffer,
               3,
               SPI_NOTFIRST,
               SPI_LAST);
      
      *pLqi   = spi_rx_buffer[2];
      
   } else {
      // invalid length
      
      // read a just byte to close spi
      spi_txrx(spi_tx_buffer,
               1,
               SPI_BUFFER,
               spi_rx_buffer,
               sizeof(spi_rx_buffer),
               SPI_NOTFIRST,
               SPI_LAST);
   }
}
Example #5
0
/* Transfers packet to ENC28J60 and initiates packet transmission */
void macphy_sendpkt(void)
{
  u16 i;
  u8 x;
  u8 rlo, rhi;
  u8 status[7];
  //u8* ptr_uip_appdata;
  //ptr_uip_appdata = (u8 *)(uip_appdata);

  /* Everything we need is in bank 0 */
  macphy_setbank(0);

  /* Retrieve the rx pointer location -- set it back after we're done */
  rlo = macphy_eth_readreg(ERDPTL);
  rhi = macphy_eth_readreg(ERDPTH);

  macphy_clearbit(ESTAT, TXABRT);
  macphy_clearbit(ESTAT, LATECOL);

  /* This code catches a potential silicon errata with the ENC28J60 */
  x = macphy_eth_readreg(ECON1);
  while (x & TXRTS) {
    x = macphy_eth_readreg(EIR);
    if (x & TXERIF) {
      /* Chip is stuck. Reset */
      //cprintf(C" -> MAC TX is stuck. Reset. \r\n");
      Serial_PrintString("MAC TX is stuck. Reset. \r\n");
      macphy_setbit(ECON1, TXRST);
      macphy_clearbit(ECON1, TXRST);
      macphy_clearbit(EIR, TXERIF);
    }
    x = macphy_eth_readreg(ECON1);
  }

  /* Set the SPI write buffer pointer location */
  macphy_writereg(EWRPTL, 0x00);
  macphy_writereg(EWRPTH, 0x10);
  
  //Serial_PrintString("macphy_sendpkt() uip_len=");
  //Serial_PrintNumber(uip_len);
  //Serial_PrintString("\r\n");
  /* Write the link-layer header to the chip */
  /* SPI Write Pointer Start (ETXSTL) already set by macphy_init() */
  spi_ss(0);
  spi_txrx(CMD_WBM);
  spi_txrx(0x00); /* "options" byte header needed by the ENC chip */

  for (i = 0; i < UIP_LLH_LEN; i++) {
    /* uip_buf starts at offset 6, first 6 bytes are from the old RX packet */
    spi_txrx(pkt_buf[6+i]);
  }
  if (uip_len <= UIP_LLH_LEN + UIP_TCPIP_HLEN) {
    /* Probably an ARP update or somesuch. No need to send appdata */
    for (i = 0; i < uip_len - UIP_LLH_LEN; i++) {
      spi_txrx(pkt_buf[6+UIP_LLH_LEN+i]);
    }
  } else {
    /* We have link-layer header, IP header, and appdata */
    for (i = 0; i < UIP_TCPIP_HLEN; i++) {
      /* IP header */
      spi_txrx(pkt_buf[6+UIP_LLH_LEN+i]);
    }
    for (i = 0; i < uip_len - UIP_TCPIP_HLEN - UIP_LLH_LEN; i++) {
      spi_txrx(((u8 *)uip_appdata)[i]);
      //spi_txrx(*ptr_uip_appdata);
      //ptr_uip_appdata++;
    }
  }
  spi_ss(1);

  /* Set the MAC TX end pointer */
  macphy_writereg(ETXNDL, (0x1000 + uip_len) & 0x00ff);
  macphy_writereg(ETXNDH, (0x1000 + uip_len) >> 8);

  /* Fire and forget! */
  macphy_setbit(ECON1, TXRTS);

  /* Just in case, clear the transmit abort and late collision flags */
  macphy_clearbit(ESTAT, TXABRT);
  macphy_clearbit(ESTAT, LATECOL);

}
Example #6
0
/* Reads packet from the ENC28J60 and decrements pktcnt */
void macphy_readpkt(void)
{
  u8 rlo, rhi;
  u32 i, plen, nxtpkt, rptr;

  macphy_setbank(0);
  /* Retrieve the rx pointer location */
  rlo = macphy_eth_readreg(ERDPTL);
  rhi = macphy_eth_readreg(ERDPTH);
  
  rptr = (rhi << 8) + rlo;

  spi_ss(0);
  spi_txrx(CMD_RBM);
  pkt_buf[0] = spi_txrx(0xff); /* Next packet pointer LSB */
  pkt_buf[1] = spi_txrx(0xff); /* Next packet pointer MSB */

  pkt_buf[2] = spi_txrx(0xff); /* status[7:0] */
  pkt_buf[3] = spi_txrx(0xff); /* status[15:8] */
  pkt_buf[4] = spi_txrx(0xff); /* status[23:16] */
  pkt_buf[5] = spi_txrx(0xff); /* status[31:24] */
  /* Calculate the remaining length of this packet */
  nxtpkt = plen = (pkt_buf[1] << 8) + pkt_buf[0];
  if (nxtpkt < rptr) {
    /* Wrapping case */
    /* Explanation: RX buffer ends at byte 0xfff, so bytes between rptr and 0x1000 are */
    /* part of the packet, plus the stuff that wrapped around to nxtpkt. Since plen holds */
    /* nxtpkt already, we just need to add in the bytes before the wrap */
    plen += (0x1000 - rptr);
  } else {
    /* Normal case */
    plen -= rptr;
  }

  if (plen > (MAX_ETH_FRAMELEN + 6 + 1)) {
    //cprintf(C" - !! - PACKET LENGTH CORRUPTION - !! -\r\n");
    Serial_PrintString(" - !! - PACKET LENGTH CORRUPTION - !! -\r\n");
    while (1);
  }
  /* Could be off by one here due to the word alignment. If so, go ahead and accept. */
  if ((plen != ((pkt_buf[3] << 8) + pkt_buf[2] + 6)) &&
      (plen != ((pkt_buf[3] << 8) + pkt_buf[2] + 7))) {
    //cprintf(C" - !! - PACKET LENGTH MISMATCH - !! -\r\n");
    Serial_PrintString(" - !! - PACKET LENGTH MISMATCH - !! -\r\n");
    //cprintf(C" Status Vector said 0x%02x%02x (+6/+7), plen = 0x%04x\r\n", pkt_buf[3], pkt_buf[2], plen);
    Serial_PrintString(" Status Vector said, plen= ");
    Serial_PrintNumber(plen);
    Serial_PrintString("\r\n pkt_buf[3]= ");
    Serial_PrintByte(pkt_buf[3], 1);
    Serial_PrintString("\r\n pkt_buf[2]= ");
    Serial_PrintByte(pkt_buf[2], 1);
    //cprintf(C" Next packet pointer = 0x%02x%02x\r\n", pkt_buf[1], pkt_buf[0]);
    Serial_PrintString("\r\n Next packet pointer : pkt_buf[1]= ");
    Serial_PrintByte(pkt_buf[1], 1);
    Serial_PrintString("pkt_buf[0]= ");
    Serial_PrintByte(pkt_buf[0], 1);
    //cprintf(C" SPI Read Pointer = 0x%04x\r\n", rptr);
    Serial_PrintString("\r\n SPI Read Pointer = ");
    Serial_PrintNumber(rptr);
    Serial_PrintString("\r\n");
    macphy_readback();
  }
  /* Ethernet frame follows here */
  for (i = 6; i < plen; i++) {
    pkt_buf[i] = spi_txrx(0xff);
  }
  spi_ss(1);

  /* Free up space by moving ERXRDPT forward and setting the PKTDEC bit */
  if (nxtpkt > 0xfff) {
    //cprintf(C" - !! - NEXT PACKET POINTER CORRUPTION - !! -\r\n");
    Serial_PrintString(" - !! - NEXT PACKET POINTER CORRUPTION - !! -\r\n");
    while (1);
  }
  nxtpkt -= 1;
  if (nxtpkt > 0xfff) {
    /* Wrap around. Fix up. */
    nxtpkt = 0xfff;
  }

  macphy_writereg(ERXRDPTL, nxtpkt & 0xff); 
  macphy_writereg(ERXRDPTH, nxtpkt >> 8);
  macphy_pktdec();
}
Example #7
0
static u8 sd_send_command( u16 cmd, void *arg, void* resp ) {
	u8 i, tmp;
	// Prefix with an ACMD?
	if( cmd & 0x0100 ) {
		tmp = sd_send_command( CMD55_APP_CMD, NULL, NULL );
		if( tmp != SD_ERR_OK ) return( tmp );
	}
	// Select SD card
	sd_assert();
	// Send command
	spi_txrx( ( ( ( u8 )cmd ) & 0x3F ) | 0x40 );
	// Send arguments
	if( arg ) {
		spi_txrx( *( ( ( u8* )arg ) + 3 ) );
		spi_txrx( *( ( ( u8* )arg ) + 2 ) );
		spi_txrx( *( ( ( u8* )arg ) + 1 ) );
		spi_txrx( *( ( ( u8* )arg ) + 0 ) );
	} else {
		spi_txrx( 0 );
		spi_txrx( 0 );
		spi_txrx( 0 );
		spi_txrx( 0 );
	}
	// CRC, ignored for all commands except reset - where it is always 0x95
	spi_txrx( 0x95 );
	// Read first response byte with timeout
	i = 0;
	do {
		tmp = spi_txrx( CMD_IDLE );
		i++;
	} while( ( tmp & 0x80 ) != 0 && i < SD_CMD_TIMEOUT );
	// Bail if timed out
	if( ( tmp & 0x80 ) != 0 ) {
		sd_deassert();
		return( SD_ERR_TIMEOUT );
	}
	switch( cmd ) {
		case CMD12_STOP_TRANSMISSION:
			i = R1b;
			break;
		case CMD8_SEND_IF_COND:
			i = R7;
			break;
		case CMD58_READ_OCR:
			i = R3;
			break;
		case CMD9_SEND_CSD:
		case CMD10_SEND_CID:
			i = R1 + 16 + 2;
			break;
		default:
			i = R1;
	}
	// Read remaining response data
	while( i-- ) {
		if( resp ) ( ( u8* )resp )[ i ] = tmp;
		tmp = spi_txrx( CMD_IDLE );
	}
	// Deselect card
	sd_deassert();
	// All seems to be in order
	return( SD_ERR_OK );
}
Example #8
0
static void sd_delay( u8 count ) {
	u8 i;
	for( i = 0; i != count; i++ ) {
		spi_txrx( CMD_IDLE );
	}
}
Example #9
0
void
nRFCMD_ReadWriteBuffer (const uint8_t * tx_data, uint8_t * rx_data,
			uint32_t len)
{
  spi_txrx (SPI_CS_NRF, tx_data, len, rx_data, len);
}