Beispiel #1
0
/**
 * @brief Reads a single block from SD card, specifically for the dictionary
 *        functions
 *        Reads 512 bytes (SPI Block) from SD Card
 * @return unsigned char - 0 if no error and response byte if error
 */
unsigned char sd_read_single_dict_block(unsigned long start_block)
{
  unsigned char response;
  unsigned int i, retry = 0;

  response = sd_send_command(READ_SINGLE_BLOCK, start_block); //read a Block command

  if(response != 0x00) return response; //check for SD status: 0x00 - OK (No flags set)

  SD_CS_ASSERT;

  retry = 0;
  while(spi_receive() != 0xfe) //wait for start block token 0xfe (0x11111110)
    if(retry++ > 0xfffe)
    {
      SD_CS_DEASSERT; 
      return 1;
    } //return if time-out

  for(i = 0; i < 512; i++) //read 512 bytes
    dict_buffer[i] = spi_receive();

  spi_receive(); //receive incoming CRC (16-bit), CRC is ignored here
  spi_receive();

  spi_receive(); //extra 8 clock pulses
  SD_CS_DEASSERT;

  return 0;
}
Beispiel #2
0
/**
 * @brief Writes a single block of SD Card. Data that is written is put into the
 *        buffer variables and writes out the 512 charachters.  
 * @param start_block - unsigned long, describes which block you want to right 
 * @return unsigned char - 0 if no error
 *                         response byte will be sent if an error
 */
unsigned char sd_write_single_block(unsigned long start_block)
{
  unsigned char response;
  unsigned int i, retry = 0;

  response = sd_send_command(WRITE_SINGLE_BLOCK, start_block); //write a Block command

  if(response != 0x00) return response; //check for SD status: 0x00 - OK (No flags set)
  SD_CS_ASSERT;

  spi_transmit(0xfe);     //Send start block token 0xfe (0x11111110)

  for(i = 0; i < 512; i++)    //send 512 bytes data
    spi_transmit(buffer[i]);

  spi_transmit(0xff);     //transmit dummy CRC (16-bit), CRC is ignored here
  spi_transmit(0xff);

  response = spi_receive();

  if((response & 0x1f) != 0x05) //response= 0xXXX0AAA1 ; AAA='010' - data accepted
  {                              //AAA='101'-data rejected due to CRC error
    SD_CS_DEASSERT;              //AAA='110'-data rejected due to write error
    return response;
  }

  while(!spi_receive()) //wait for SD card to complete writing and get idle
    if(retry++ > 0xfffe)
    {
      SD_CS_DEASSERT; 
      return 1;
    }

  SD_CS_DEASSERT;
  spi_transmit(0xff);   //just spend 8 clock cycle delay before reasserting the CS line
  SD_CS_ASSERT;         //re-asserting the CS line to verify if card is still busy

  while(!spi_receive()) //wait for SD card to complete writing and get idle
    if(retry++ > 0xfffe)
    {
      SD_CS_DEASSERT; 
      return 1;
    }

  SD_CS_DEASSERT;

  return 0;
}
Beispiel #3
0
int main( ) {
	DDRC |= ( 1<<PC5 );
	PORTC |= ( 1<<PC5 );
	
	DDRC |= (1<<PC2);  // TEST LED
	PORTC |= (1<<PC2);
	
	spi_slave_init();
	sei();
	uint8_t* data;

	while ( 1 ) {
		//data != NULL if there is data in the SPI-buffer
		data = spi_receive( );
		if ( data ) {
			//Disable interrupts for more performance
			cli();
			//Write received data to the ledstrip
			//Technically this function wants a RGB-struct but casts it internal to an uint8 array.
			//So we simply ignore the struct to make it easier for us and to save RAM on the ATmega.
			ws2812_setleds( data, NUM_LEDS );
			//Enable interrupts again
			sei();
			//Set ready to receive to get data
			PORTC |= ( 1<<PC5 );
			PORTC |= (1<<PC2);
		}
	}
}
Beispiel #4
0
int blobsLoop()
{
#if 0
	static int i=0;
	Qval qval;

	while(g_qqueue->dequeue(&qval))
	{
		if (qval==0xffffffff)
			cprintf("%d\n", i++);	
	}
#else
	uint16_t recvBuf[1];

	// create blobs
	g_blobs->blobify();

	// just grab a word-- don't worry about looking at data except to see if we're synchronized.
	if (spi_receive(recvBuf, 1))
	{
		if (recvBuf[0]!=0xa5a5) // if received data isn't correct, we're out of sync
			spi_sync();
		//cprintf("%x\n", recvBuf[0]); 
	}
#endif
	
	return 0;
}
Beispiel #5
0
//----------------------------------------------------------------------------------
//  uint8 halRfReadReg(uint8 addr)
//----------------------------------------------------------------------------------
uint8_t halRfReadReg(uint8_t addr)
{
    uint8_t reg;

    spi_receive(addr | CC2500_READ_SINGLE, &reg, 1);
    return reg;
}
Beispiel #6
0
//----------------------------------------------------------------------------------
//  uint8 halRfReadStatusReg(uint8 addr)
//
//  NOTE:
//      When reading a status register over the SPI interface while the register
//      is updated by the radio hardware, there is a small, but finite, probability
//      that the result is corrupt. The CC1100 and CC2500 errata notes explain the
//      problem and propose several workarounds.
//
//----------------------------------------------------------------------------------
uint8_t halRfReadStatusReg(uint8_t addr)
{
    uint8_t reg;

    spi_receive(addr | CC2500_READ_BURST, &reg, 1);
    return reg;
}
Beispiel #7
0
char mcp2515_read_status()
{
	char output;
	spi_select();
	spi_transmit(MCP_READ);
	spi_transmit(MCP_CANSTAT);
	output = spi_receive();
	
	spi_deselect();
	return output;
}
Beispiel #8
0
char mcp2515_read (char addr){
	char result;
	
	spi_select();
	
	spi_transmit(MCP_READ);
	spi_transmit(addr);
	result = spi_receive();
	
	spi_deselect();
	
	return result;
}
Beispiel #9
0
/**
 * @brief sends a command to the SD card
 * @param cmd - unsigned char, command to send to the SD card
 * @param arg - unsigned long, argument of the command sent
 * @return unsigned char - response byte
 */
unsigned char sd_send_command(unsigned char cmd, unsigned long arg)
{
  unsigned char response, retry = 0, status;

  // SD card accepts byte address while SDHC accepts block address in multiples of 512
  // so, if it's SD card we need to convert block address 
  // into corresponding byte address by 
  // multipying it with 512. which is equivalent to shifting it left 9 times
  // following 'if' loop does that

  if(sdhc_flag == 0)    
    if(cmd == READ_SINGLE_BLOCK     ||
        cmd == READ_MULTIPLE_BLOCKS  ||
        cmd == WRITE_SINGLE_BLOCK    ||
        cmd == WRITE_MULTIPLE_BLOCKS ||
        cmd == ERASE_BLOCK_START_ADDR|| 
        cmd == ERASE_BLOCK_END_ADDR ) 
    {
      arg = arg << 9;
    }

  SD_CS_ASSERT;

  spi_transmit(cmd | 0x40); //send command, first two bits always '01'
  spi_transmit(arg >> 24);
  spi_transmit(arg >> 16);
  spi_transmit(arg >> 8);
  spi_transmit(arg);

  // It is compulsory to send correct CRC for CMD8 (CRC=0x87) & CMD0 (CRC=0x95)
  if(cmd == SEND_IF_COND)
    spi_transmit(0x87);    // for remaining commands, CRC is ignored in SPI mode
  else 
    spi_transmit(0x95); 

  while((response = spi_receive()) == 0xff) // wait response
    if(retry++ > 0xfe) break; // time out error

  if(response == 0x00 && cmd == 58)  // checking response of CMD58
  {
    status = spi_receive() & 0x40;     // first byte of the OCR register (bit 31:24)
    if(status == 0x40) sdhc_flag = 1;  // we need it to verify SDHC card
    else sdhc_flag = 0;

    spi_receive(); // remaining 3 bytes of the OCR register are ignored here
    spi_receive(); // one can use these bytes to check power supply limits of SD
    spi_receive(); 
  }

  spi_receive(); // extra 8 CLK
  SD_CS_DEASSERT;

  return response;
}
Beispiel #10
0
/**
 * @brief Reads multiple blocks from the SD card and send every block to UART
 * @return unsigned char - 0 if no error and response byte if error
 */
unsigned char sd_read_multiple_blocks (unsigned long start_block, 
    unsigned long total_blocks)
{
  unsigned char response;
  unsigned int i, retry = 0;

  retry = 0;

  response = sd_send_command(READ_MULTIPLE_BLOCKS, start_block); //write a Block command

  if(response != 0x00) return response; //check for SD status: 0x00 - OK (No flags set)

  SD_CS_ASSERT;

  while(total_blocks)
  {
    retry = 0;
    while(spi_receive() != 0xfe) //wait for start block token 0xfe (0x11111110)
      if(retry++ > 0xfffe)
      {
        SD_CS_DEASSERT; 
        return 1;
      } //return if time-out

    for(i = 0; i < 512; i++) //read 512 bytes
      buffer[i] = spi_receive();

    spi_receive(); //receive incoming CRC (16-bit), CRC is ignored here
    spi_receive();

    spi_receive(); //extra 8 cycles

    for(i = 0; i < 512; i++) //send the block to UART
    {
      if(buffer[i] == '~') break;
      transmit_byte(buffer[i]);
    }

    total_blocks--;
  }

  sd_send_command(STOP_TRANSMISSION, 0); //command to stop transmission
  SD_CS_DEASSERT;
  spi_receive(); //extra 8 clock pulses

  return 0;
}
Beispiel #11
0
/**
 * @brief Recieves data from UART and writes to multiple blocks of SD card
 * @return unsigned char - response byte
 */
unsigned char sd_write_multiple_blocks(unsigned long start_block, 
    unsigned long total_blocks)
{
  unsigned char response, data;
  unsigned int i, retry = 0;
  unsigned long block_counter = 0, size;

  response = sd_send_command(WRITE_MULTIPLE_BLOCKS, start_block); //write a Block command

  if(response != 0x00) return response; //check for SD status: 0x00 - OK (No flags set)

  SD_CS_ASSERT;

  while( block_counter < total_blocks )
  {
    i = 0;
    do
    {
      data = receive_byte();
      if(data == 0x08)  //'Back Space' key pressed
      { 
        if(i != 0)
        { 
          transmit_byte(data);
          transmit_byte(' '); 
          transmit_byte(data); 
          i--; 
          size--;
        } 
        continue;     
      }
      transmit_byte(data);
      buffer[i++] = data;
      if(data == 0x0d)
      {
        transmit_byte(0x0a);
        buffer[i++] = 0x0a;
      }
      if(i == 512) break;
    }
    while (data != '~');

    spi_transmit(0xfc); //Send start block token 0xfc (0x11111100)

    for(i = 0; i < 512; i++) //send 512 bytes data
      spi_transmit(buffer[i]);

    spi_transmit(0xff); //transmit dummy CRC (16-bit), CRC is ignored here
    spi_transmit(0xff);

    response = spi_receive();
    if((response & 0x1f) != 0x05) //response= 0xXXX0AAA1 ; AAA='010' - data accepted
    {                              //AAA='101'-data rejected due to CRC error
      SD_CS_DEASSERT;             //AAA='110'-data rejected due to write error
      return response;
    }

    while(!spi_receive()) //wait for SD card to complete writing and get idle
      if(retry++ > 0xfffe)
      {
        SD_CS_DEASSERT; 
        return 1;
      }

    spi_receive(); //extra 8 bits
    block_counter++;
  }

  spi_transmit(0xfd); //send 'stop transmission token'
  retry = 0;

  while(!spi_receive()) //wait for SD card to complete writing and get idle
    if(retry++ > 0xfffe)
    {
      SD_CS_DEASSERT; 
      return 1;
    }

  SD_CS_DEASSERT;
  spi_transmit(0xff); //just spend 8 clock cycle delay before reasserting the CS signal

  // re assertion of the CS signal is required to verify if card is still busy
  SD_CS_ASSERT; 

  while(!spi_receive()) //wait for SD card to complete writing and get idle
    if(retry++ > 0xfffe)
    {
      SD_CS_DEASSERT; 
      return 1;
    }

  SD_CS_DEASSERT;

  return 0;
}
Beispiel #12
0
int main(void) 
 {	
 	pixyInit(SRAM3_LOC, &LR0[0], sizeof(LR0));
	cc_init(g_chirpUsb);
#if 0	
	uint32_t a = 0xffffffff;
	uint32_t b = 0;
	uint32_t c = b - a;

	printf("*** %d\n", c);
#endif
#if 0
	uint32_t i = 0;
	uint32_t timer;
	setTimer(&timer);
	while(1)
	{
		if (getTimer(timer)>1000000)
		{
			printf("%d\n", i++);
			setTimer(&timer);
		}
	}
#endif
#if 1

	uint16_t buf[16];

	spi_setCallback(transmitCallback);

 	while(1)
	{
		
		if (spi_receive(buf, 1))
		{
			printf("%x\n", buf[0]);
			if (buf[0]!=0xa5a5)
				spi_sync(); 
		}
	}

#endif
#if 0
	// 0 pan, 0 to 1000 clockwise
	// 1 tilt, 0 to 1000 tilt up
	while(1)
	{
		// bring up
		servoMove(1, 650, 900, r(800, 50), 0, 550, 450);
		servoMove(1, 900, 1000, 600, 0, 450, 350);
		servoMove(1, 1000, 1025, 100, 0, 350, r(320, 20));
		servoMove(1, 1025, 1000, 150, 0, 320, r(360, 20));
		delayus(2000000);
		// bring down
		servoMove(1, 1000, 900, r(600, 50), 0, 350, 460);
		servoMove(1, 900, 650, r(850, 50), 0, 450, 550);
		servoMove(1, 650, 670, 100, 0, 550, r(520, 20));
		servoMove(1, 670, 650, 150, 0, 520, r(550, 20));
		delayus(1500000);

		// test 1
		servoMove(1, 650, 900, r(800, 50), 0, 550, 450);
		servoMove(1, 900, 1000, 600, 0, 450, 350);
		servoMove(1, 1000, 1025, 100, 0, 350, r(320, 20));
		servoMove(1, 1025, 1000, 150, 0, 320, r(360, 20));
		servoMove(1, 1000, 900, r(600, 50), 0, 350, 460);
		servoMove(1, 900, 650, r(850, 50), 0, 450, 550);
		servoMove(1, 650, 670, 100, 0, 550, r(520, 20));
		servoMove(1, 670, 650, 150, 0, 520, r(550, 20));

		// test 2
		servoMove(1, 650, 900, r(800, 50), 0, 550, 450);
		servoMove(1, 900, 1000, 600, 0, 450, 350);
		servoMove(1, 1000, 1025, 100, 0, 350, r(320, 20));
		servoMove(1, 1025, 1000, 150, 0, 320, r(360, 20));
		servoMove(1, 1000, 900, r(600, 50), 0, 350, 460);
		servoMove(1, 900, 650, r(850, 50), 0, 450, 550);
		servoMove(1, 650, 670, 100, 0, 550, r(520, 20));
		servoMove(1, 670, 650, 150, 0, 520, r(550, 20));
		delayus(5000000);

	}
#endif   	
#if 0
	while(1)
	{
		g_chirpUsb->service();
		handleButton();
	}
#endif
#if 0
	g_chirpM0->getProc("getRLSFrame", (ProcPtr)getRLSFrameCallback);

   	blobProcess();

#endif

#if 0
#define SERVO

	int32_t result, row;
	uint32_t i, j, numRls, startCol, length, model, xsum, ysum, n, xavg, yavg;
 	uint32_t *qVals = (uint32_t *)RLS_MEMORY;

	//motor(0, 0);
	// to switch between servo and motor--
	// uncomment servo or motor below, respectively
	// for motor, change pixy_init.cpp, SCT init
	// LPC_SCT->MATCH[0].L = 4000;
	// LPC_SCT->MATCHREL[0].L = 4000;
	// this will increase the pwm freq and reduce the latency
	// (these values are normally 20000)
	// Servo connectors --- black wire down, yellow up.
	// tilt: edge
	// pan: inner
	// note, tilt servo has wire facing forward

	j = 0;
	while(1)
	{
		g_chirpUsb->service();
		handleButton();
		if (g_loop)
		{

			//cc_getRLSFrame(qVals, RLS_MEMORY_SIZE, LUT_MEMORY, &numRls);
			cc_getMaxBlob(NULL);

#if 0
    		for (i=0, row=-1, n=0, xsum=0, ysum=0; i<numRls; i++)
    		{
        		if (qVals[i]==0)
        		{
            		row++;
            		continue;
        		}
        		model = qVals[i]&0x03;
        		qVals[i] >>= 3;
        		startCol = qVals[i]&0x1ff;
        		qVals[i] >>= 9;
        		length = qVals[i]&0x1ff;
				xsum += startCol + (length>>1);
				ysum += row;
				n++;
			}
			if (n>15)
			{
				xavg = xsum/n;
				yavg = ysum/n;
			}
			else
			{
				xavg = XCENTER;
#ifdef SERVO
				yavg = YCENTER;
#else
			 	yavg = YTRACK;
#endif
			}
#ifdef SERVO
			servo(xavg, yavg);
#else
			motor(xavg, yavg);
#endif
//		   	printf("%d %d\n", xavg, yavg);
#endif
		if (j%50==0)
			printf("%d\n", j);
		j++;   			
		}				
    }
#endif
}
Beispiel #13
0
//----------------------------------------------------------------------------------
//  rf_status_t halRfReadFifo(uint8* data, uint8 length)
//----------------------------------------------------------------------------------
rf_status_t halRfReadFifo(uint8_t *data, uint8_t length)
{
    return spi_receive(CC2500_RXFIFO | CC2500_READ_BURST, data, length);
}