//Fetch the next act payload
//Return the payload length
char NRF24L01_RxPacket(char *payload)
{
  int len;
  int i;

  //Get the packet length
  RADIO_EN_CS();
  SPI_SendByte(CMD_RX_PL_WID);
  len = SPI_ReceiveByte();
  RADIO_DIS_CS();  
  
  if (len>0 && len<33)
  {
    //Read the packet from the RX buffer
    RADIO_EN_CS();
    SPI_SendByte(CMD_R_RX_PAYLOAD);
    for(i=0;i<len;i++)
      payload[i] = SPI_ReceiveByte();
    RADIO_DIS_CS();
  } else {
    len=0;
  }
  
  //Pulse CE
  //CE_PULSE();
  
  return len;
}
Exemple #2
0
UINT8 SD_WriteData(UINT8 pu8DataPointer[], UINT32 u32DataLength) {
  UINT8 u8R1;
  UINT8 u8Counter;
  UINT32 u32Counter;

	//SPI_SS = ENABLE;
	
	SPI_SendByte(0xFE);
    
	for(u32Counter = 0; u32Counter < u32DataLength; u32Counter++)
		SPI_SendByte(pu8DataPointer[u32Counter]);

	SPI_SendByte(0xFF);    // checksum Bytes not needed
	SPI_SendByte(0xFF);
	
  /* Response RHandler (R1) */
  u8Counter = SD_WAIT_CYCLES;
  do {
		u8R1 = SPI_ReceiveByte();
    u8Counter--;
  } while(((u8R1 & 0x80) != 0) && (u8Counter > 0)); // #001 wait for SD response <> 0xFF
   	 
	//SPI_SS = DISABLE;
 
  if(u8Counter == 0) return (SD_FAIL_TIMEOUT);
  
	if((u8R1 & 0x1F) != 0x05) return (SD_FAIL_WRITE); // #001 checks if response = 0101 = data accepted
	
	while(SPI_ReceiveByte()==0x00) ;  // Dummy SPI cycle
	
  return(SD_OK);	
}
Exemple #3
0
UINT8 SD_SendCommand(UINT8 u8SDCommand, UINT32 u32Param, UINT8 pu32Response[], UINT8 u8ResponseLength) {
  UINT8 u8R1;
  UINT8 u8Counter;
  UINT8 buffer[4];
  
  buffer[0] = (UINT8)((u32Param & 0xFF000000) >> 24);
  buffer[1] = (UINT8)((u32Param & 0x00FF0000) >> 16);
  buffer[2] = (UINT8)((u32Param & 0x0000FF00) >>  8);
  buffer[3] = (UINT8)((u32Param & 0x000000FF) >>  0);
  
  /* Enable CS */  
  //SPI_SS = ENABLE;
 
  /* Send Start byte */
  SPI_SendByte((UINT8)(u8SDCommand | 0x40));

  /* Send Argument */
  for(u8Counter = 0; u8Counter < 4; u8Counter++) {
     SPI_SendByte(buffer[u8Counter]);
  }

  /* Send CRC */
  if((u8SDCommand & 0x02F) == SD_CMD8_SEND_IF_COND) {
    SPI_SendByte(0x87);
  } else {
    SPI_SendByte(0x95);
  }
  
  /*
  } else if ((u8SDCommand & 0x02F) == SD_CMD1) {
    SPI_SendByte(0x95);
  } else {
    SPI_SendByte(0x01);
  }
  */
  
  /* Response RHandler (R1) */
  u8Counter = SD_WAIT_CYCLES;
  do {
      u8R1 = SPI_ReceiveByte();							//Recibe Respuesta de SD (R1=1byte)
      u8Counter--;
  } while(((u8R1 & 0x80) != 0) && (u8Counter > 0));
 
  if(u8Counter == 0) {
    //SPI_SS = DISABLE;
    return (SD_FAIL_TIMEOUT);
  }
  
   
  for (u8Counter = 0; u8Counter < u8ResponseLength; u8Counter++)  {
    pu32Response[u8Counter] = SPI_ReceiveByte();
  }
  
  /* Disable CS */  
  //SPI_SS = DISABLE;  
  
  return (u8R1);
}
Exemple #4
0
uint8_t hwspi_read_block() {
	uint16_t i;
	uint8_t sreg;
	
	if (SPI_USE_3B_CMDS && SPI_ADDRESS_LENGTH == 4) {
		HWSPI_CS_LOW;
		SPI_SendByte(SPI_COMMAND_EN4B);
		HWSPI_CS_HIGH;
	}

	/* Save global interrupt flag and disable interrupts */
	sreg = SREG;
	cli();

	HWSPI_CS_LOW;

	if (SPI_USE_3B_CMDS || SPI_ADDRESS_LENGTH == 3)
		SPI_SendByte(SPI_COMMAND_3B_READ);
	else
		SPI_SendByte(SPI_COMMAND_4B_READ);
	
	/* address */
	if (SPI_ADDRESS_LENGTH == 4) SPI_SendByte(buf_addr[0]);
	SPI_SendByte(buf_addr[1]);
	SPI_SendByte(buf_addr[2]);
	SPI_SendByte(buf_addr[3]);

	for (uint8_t k = 0; k < SPI_BLOCK_SIZE / BUF_SIZE_RW; ++k) {
		for (i = 0; i < BUF_SIZE_RW; ++i) {
			buf_rw[i] = SPI_ReceiveByte();
		}
		usb_serial_write(buf_rw, BUF_SIZE_RW);
	}

	uint16_t rest = SPI_BLOCK_SIZE - ((SPI_BLOCK_SIZE / BUF_SIZE_RW) * BUF_SIZE_RW);
	for (i = 0; i < rest; ++i) {
		buf_rw[i] = SPI_ReceiveByte();
	}
	
	HWSPI_CS_HIGH;

	usb_serial_write(buf_rw, rest);

	/* Restore global interrupt flag */
	SREG = sreg;

	return 1;
}
Exemple #5
0
UINT8 SD_ReadSector(UINT32 u32SD_Block,UINT8 pu8DataPointer[]) {
  volatile UINT8 u8Temp = 0;

  __RESET_WATCHDOG(); /* feeds the dog */
	
  if (!gSDCard.SDHC) u32SD_Block <<= SD_BLOCK_SHIFT;
     
  SPI_SS = ENABLE;
  //Utilizo u8Temp para capturar respuesta de la tarjeta SD (fines depurativos)
  u8Temp = SD_SendCommand(SD_CMD17_READ_BLOCK, u32SD_Block, NULL, 0); 
  if((u8Temp & SD_R1_ERROR_MASK) != SD_OK) {
    SPI_SS = DISABLE;    
    return (SD_FAIL_READ);
  }
  
	while(u8Temp != 0xFE) u8Temp = SPI_ReceiveByte(); 
	//La SD responde 0xFF...0xFF mientras accede al sector y 0xFE cuando esta lista. 
	//Los datos que envía a continuación corresponden al sector solicitado. 
	
  if (SD_ReadData(pu8DataPointer,SD_BLOCK_SIZE) != SD_OK) return (SD_FAIL_READ);
  
  SPI_SS = DISABLE;
  
  return (SD_OK);
}
Exemple #6
0
byte MMC_Release(byte result)
{
	MMC_SS_HIGH();
	SPI_ReceiveByte(0xFF);	// CS does not release SPI until next clock....flush CS release
	SPI_Disable();
	return result;
}
Exemple #7
0
byte MMC_ReadOCR(byte cmd, uint32_t param, byte* ocr)
{
	byte r = MMC_Command(cmd,param);
	byte i = 4;
	while (i--)
		*ocr++ = SPI_ReceiveByte(0xFF);
	return r;
}
Exemple #8
0
//uint8_t device_code1;
uint8_t hwspi_read_id()
{
	HWSPI_CS_LOW;

	SPI_SendByte(SPI_COMMAND_REMS);
	SPI_SendByte(0);
	SPI_SendByte(0);
	SPI_SendByte(0);
	
	//maker_code = 0xC2;
	//device_code0 = 0x18;
	maker_code = SPI_ReceiveByte();
	device_code0 = SPI_ReceiveByte();

	HWSPI_CS_HIGH;

	return 1;
}
Exemple #9
0
static char sdWaitWriteFinish(void)
{
  unsigned short count = 0xFFFF; // wait for quite some time

  while ((SPI_ReceiveByte(0xFF) == 0) && count )
    count--;

  // If count didn't run out, return success
  return (count != 0);
}
Exemple #10
0
uint8_t hwspi_status() {
	uint8_t status;
	
	HWSPI_CS_LOW;
	SPI_SendByte(SPI_COMMAND_RDSR);
	status = SPI_ReceiveByte();
	HWSPI_CS_HIGH;
	
	return status;
}
Exemple #11
0
uint8_t hwspi_security() {
	uint8_t sec;
	
	HWSPI_CS_LOW;
	SPI_SendByte(SPI_COMMAND_RDSCUR);
	sec = SPI_ReceiveByte();
	HWSPI_CS_HIGH;
	
	return sec;
}
char NRF24L01_ReadReg(char addr)
{
  char value;
  
  RADIO_EN_CS();
  SPI_SendByte(CMD_R_REG | (addr&0x1F));
  value = SPI_ReceiveByte();
  RADIO_DIS_CS();
  
  return value;
}
Exemple #13
0
byte MMC_Token()
{
	int count = 0xFFF;	// was FFF
	byte r;
	while (count--)
	{
		r = SPI_ReceiveByte(0xFF);
		if (r != 0xFF)
			break;
	}
	return r;
}
Exemple #14
0
//LSB of returned register is in DataToRead[0]
bool AD7794ReadReg( uint8_t reg, uint8_t *DataToRead )
{
	if( (reg >= 0) && (reg <= 7) )
	{
		AD7794Select(1);
		SPI_SendByte( AD7794_CR_READ | (reg << 3) );
		
		if( (reg == 0) || (reg == 4) || (reg == 5) )
		{
			//8-bit register
			*DataToRead = SPI_ReceiveByte();
		}
		else if( (reg == 1) || (reg == 2) )
		{
			//16-bit register
			DataToRead[1] = SPI_ReceiveByte();
			DataToRead[0] = SPI_ReceiveByte();
		}
		else
		{
			//16-bit register
			DataToRead[2] = SPI_ReceiveByte();
			DataToRead[1] = SPI_ReceiveByte();
			DataToRead[0] = SPI_ReceiveByte();
		}
		AD7794Select(0);
		return true;
	}
	
	//Invalid register
	return false;
}
Exemple #15
0
UINT8 SD_ReadData(UINT8 pu8DataPointer[], UINT32 u32DataLength) {
  UINT8  CRC[2];
  UINT32 u32Counter;

  /* Enable CS */  
  //SPI_SS = ENABLE;

  for(u32Counter = 0; u32Counter < u32DataLength; u32Counter++) {
    pu8DataPointer[u32Counter] = SPI_ReceiveByte();
  }

  for(u32Counter = 0; u32Counter < 2; u32Counter++) {
    CRC[u32Counter] = SPI_ReceiveByte();
  }

  /* Disable CS */  
  //SPI_SS = DISABLE;

  (void) SPI_ReceiveByte(); // Unknown issue. SPI dummy cicle required by some cards

	return (SD_OK);  
}
Exemple #16
0
UINT8 SD_WriteSector(UINT32 u32SD_Block, UINT8 * pu8DataPointer) {
    UINT16 u16Counter;
    UINT8  SD_response;
    
    __RESET_WATCHDOG(); /* feeds the dog */
  
    SPI_SS = ENABLE;
    
    if(!gSDCard.SDHC) u32SD_Block <<= SD_BLOCK_SHIFT;
    
    if(SD_SendCommand(SD_CMD24_WRITE_BLOCK, u32SD_Block, NULL, 0) != SD_OK) {
      SPI_SS = DISABLE;
      return (SD_FAIL_WRITE);      
    }
    
    SPI_SendByte(0xFE);    
		
     for(u16Counter = 0; u16Counter < SD_BLOCK_SIZE; u16Counter++)
			SPI_SendByte(*pu8DataPointer++);

    SPI_SendByte(0xFF);    // checksum Bytes not needed
    SPI_SendByte(0xFF);
    
	SD_response=0xFF; 
    while (SD_response == 0xFF) SD_response = SPI_ReceiveByte(); 
    if((SD_response & 0x0F) != 0x05) {
        SPI_SS=DISABLE;
        return (SD_FAIL_WRITE);    
    }
		
	while(SPI_ReceiveByte()==0x00) ;  // Dummy SPI cycle
    (void) SPI_ReceiveByte();
    SPI_SS = DISABLE;
	
    return (SD_OK);
}
Exemple #17
0
//	Read 512 bytes, hopefully quickly but not too quickly
//	Data transfer - strips the 2 CRC bytes off the end
void SPI_Receive(byte* buffer, int len)
{
	//	Unroll a bit
    SPDR = 0xFF;    // Byte 0        
    byte i = 0xFF;
    do
    {
        byte a;
        loop_until_bit_is_set(SPSR, 7);
        a = SPDR;
        SPDR = 0xFF;
        *buffer++ = a;
        loop_until_bit_is_set(SPSR, 7);
        a = SPDR;
        SPDR = 0xFF;
        *buffer++ = a;
    } while (i--);

    loop_until_bit_is_set(SPSR, 7);
    i = SPDR;                       // CRC byte 0
    SPI_ReceiveByte(0xFF);          // CRC byte 1
}
Exemple #18
0
byte MMC_WriteSector(byte *buffer, uint32_t sector)
{
	if (!(_mmcState & MMC_INITED))
		return MMC_NOT_INITED;
	if (!(_mmcState & MMC_HIGH_DENSITY))
		sector <<= 9;

	u8 r = WRITE_FAILED;
	SPI_Enable();
	MMC_SS_LOW();
	if (MMC_Command2(24,sector) != 0)
	{
		u8 d = 16;
		while (d--)
			SPI_ReceiveByte(0xFF);	// pad

		SPI_ReceiveByte(0xFE);	// Send token

		SPI_Send(buffer,512);
		SPI_ReceiveByte(0xFF);	// CRC
		SPI_ReceiveByte(0xFF);	// CRC

		u8 status;
		while ((status = SPI_ReceiveByte(0xFF)) == 0xFF)
			;
		if (status == 0xE5)
			r = 0;
		else
			r = status;

		while (SPI_ReceiveByte(0xFF) == 0)
			;
		r = MMC_Release(r);
	} else {
		r = MMC_Release(WRITE_FAILED);
	}
	return r;
}
Exemple #19
0
void SPI_Send(byte* data, int len)
{
	while (len--)
		SPI_ReceiveByte(*data++);
}
Exemple #20
0
byte MMC_Init2()
{
	byte ocr[4];
	MMC_SS_HIGH();	//
	SPI_Init();		// Init slow at first TODO
	_mmcState = 0;

	// initialize the MMC card into SPI mode by sending 80 clks
	int i;
	for (i = 0; i < 10; i++)
		SPI_ReceiveByte(0xFF);

	// Send GO_IDLE_STATE
	MMC_SS_LOW();
	if (MMC_Command(0,0) != 1)
		return GO_IDLE_TIMEOUT;
	_mmcState = MMC_PRESENT;
	MMC_SS_HIGH();

	//	Full speed
	SPI_Enable();
	MMC_SS_LOW();

	if (MMC_ReadOCR(58,0,ocr) != 1)		// Read OCR check voltages
		return READ_OCR_TIMEOUT;		// Can't read OCR
	if ((ocr[1] & 0x10) == 0)
		return BAD_VOLTAGE;				// Card does not like out voltage

	// Try CMD8
	if (MMC_ReadOCR(8,0x1AA,ocr) == 1)
	{
		//	SDHC
		_mmcState |= MMC_SDHC;
		if ((ocr[2] & 0x0F) != 1)
			return BAD_VOLTAGE;				// Card does not like out voltage
		if (ocr[3] != 0xAA)
			return BAD_PATTERN;

		//	Send ACMD41 and wait for it to come out of idle
		i = 0x7FFF;	// Wait a long time. A long, long time - some cards take 700 miliseconds or more.
		while (i--)
		{
			MMC_Command(55,0);						// SEND_APP_CMD
			if (MMC_Command(41,1L << 30) == 0)		// ACMD41 with HCS bit 30 set
				break;
		}
		if (i < 0)
			return OP_COND_TIMEOUT;

		//	Check density again
		if (MMC_ReadOCR(58,0,ocr) != 0)				// Bits[30:29]=1,0
			return READ_OCR_TIMEOUT;				// Can't read OCR
		if ((ocr[0] & 0x60) == 0x40)
			_mmcState |= MMC_HIGH_DENSITY;

	} else {

		//	SDCARD 1vXX
		//	Send SEND_OP_COND and wait for it to come out of idle
		i = 0x7FFF;
		while (i--)
		{
			if ((MMC_Command(1,0) & 1) == 0)
				break;
		}
		if (i < 0)
			return OP_COND_TIMEOUT;
	}

	//	Send SET_BLOCKLEN for 512 byte block
	if (MMC_Command(16,512) != 0)
		return SET_BLOCKLEN_TIMEOUT;

	_mmcState |= MMC_INITED;
	return 0;
}
Exemple #21
0
u8 ReadAcc(u8 r)
{
    SPI_ReceiveByte(r << 1);
    return SPI_ReceiveByte(0);
}
Exemple #22
0
void WriteAcc(u8 r, u8 v)
{
    SPI_ReceiveByte(0x80 | (r << 1));
    SPI_ReceiveByte(v);
}