Example #1
0
File: rf233.c Project: Idolf/tock
void trx_frame_write(uint8_t *data, uint8_t length) {
  spi_hold_low();
  spi_write_byte(TRX_CMD_FW);
  for (uint8_t i = 0; i < length; i++) {
    spi_write_byte(data[i]);
  }
  spi_release_low();
}
Example #2
0
//-------------------------------------------------------------------
void clear_screen ( void )
{
    SPI_CS_ON;
    spi_delay();
    spi_write_byte(0x20);
    spi_write_byte(0x00);
    spi_delay();
    SPI_CS_OFF;
}
/**
 * Access a configuration register on the CC2500
 * NOT used for multi-byte registers or command strobes
 *
 * @returns status byte for writes, or value for reads
 */
uint8_t cc2500_write_register(uint8_t reg, uint8_t val) {
	uint8_t status = 0;
	
	gpio_low(cs_pin);
	spi_write_byte(SPIx, reg);
	status = spi_write_byte(SPIx, val);
	gpio_high(cs_pin);
	
	return status;
}
Example #4
0
void spi_write(uint8_t addr , uint8_t *data, uint8_t len)
{
	uint8_t i;

	spi_start();
	spi_write_byte((0x80|addr));

	for (i = 0; i < len; i++){
		spi_write_byte(*(data + i));
	}
	spi_stop();
}
Example #5
0
uint8_t sd_reset() {
    uint8_t i, r1, time = 0;
    //set CS high
    cs_disable();
    //send 128 clocks  
    for (i = 0; i < 16; i++) {
        spi_write_byte(0xff);
    }
    //set CS low  
    cs_enable();

    //send CMD0 till the response is 0x01  
    do {
        r1 = sd_send_cmd(CMD0, 0, 0x95);
        time++;
        //if time out,set CS high and return r1  
        if (time > 254) {
            //set CS high and send 8 clocks  
            cs_disable();
            return r1;
        }
    } while (r1 != 0x01);
    //set CS high and send 8 clocks  
    cs_disable();
    serial_printf("sd_reset ok\n");
    return 0;
}
Example #6
0
void lcd_command(unsigned char com) 
{
    LCD_OP = LCD_OP_CMD;
    LCD_CS = 0; // CS
    spi_write_byte(&com);
    LCD_CS = 1; // CS
}
Example #7
0
void lcd_data(unsigned char dat) 
{
    LCD_OP = LCD_OP_DAT;
    LCD_CS = 0; // CS
    spi_write_byte(&dat);
    LCD_CS = 1; // CS
}
/*================================================================================
      
      Function : send_gpio_to_spi_emulation
      Make : dalyong.cha
      Modify :
      Date : 2011 .5 .25
      Description : SSD2825 Register setting part
      Parameter : None
      Return : Node
      
================================================================================*/
static int send_gpio_to_spi_emulation(unsigned int reg)
{
	//reg = reg | 0x00010000;
	spi_write_byte(reg);

	return 0;

}
static void spi_send_audio(CHAR * buf,int len )
{
    int  i;
    for(i = 0; i<len; i++)
    {
        spi_write_byte(*buf++);
    }
}
/*================================================================================
      
      Function : set_gpio_to_spi_emulation
      Make : dalyong.cha
      Modify :
      Date : 2011 .5 .25
      Description : SSD2825 Data setting part
      Parameter : None
      Return : Node
      
================================================================================*/
static int set_gpio_to_spi_emulation(unsigned int data)
{
	unsigned int cmd_read = 0x00010000;
//	data = data | cmd_read;
	spi_write_byte(data);

	return 0;

}
/**
 * Send a command strobe (one-byte register access that initiates an action)
 *
 * @returns status byte
 */
uint8_t cc2500_send_strobe(uint8_t reg) {
	uint8_t status = 0;
	
	gpio_low(cs_pin);
	status = spi_write_byte(SPIx, reg);
	gpio_high(cs_pin);
	
	return status;
}
Example #12
0
File: rf233.c Project: Idolf/tock
void trx_sram_read(uint8_t addr, uint8_t *data, uint8_t length)  {
  spi_hold_low();
  /* Send the command byte */
  uint8_t tmp1 = spi_write_byte(TRX_CMD_SR);
  /* Send the command byte */
  uint8_t tmp2 = spi_write_byte(addr);
  
  PRINTF("RF233: SRAM read");
  PRINTF("0x%x 0x%x, ", tmp1, tmp2);
  /* Send the address from which the read operation should start */
  /* Upload the received byte in the user provided location */
  for (uint8_t i = 0; i < length; i++) {
    PRINTF("%02x ", data[i]);
    data[i] = spi_write_byte(0);
  }
  spi_release_low();
  PRINTF("\n");

}
Example #13
0
uint8_t sd_send_cmd(uint8_t cmd, uint32_t arg, uint8_t crc) {
    uint8_t r1, time = 0;

    //send the command,arguments and CRC  
    spi_write_byte((cmd & 0x3f) | 0x40);
    spi_write_byte(arg >> 24);
    spi_write_byte(arg >> 16);
    spi_write_byte(arg >> 8);
    spi_write_byte(arg);
    spi_write_byte(crc);

    //read the respond until responds is not '0xff' or timeout  
    do {
        r1 = spi_read_byte();
        time++;
        //if time out,return
        if (time > 254) break;
    } while (r1 == 0xff);

    return r1;
}
Example #14
0
void spi_read(uint8_t addr , uint8_t *data, uint8_t len)
{
	uint8_t i;

	spi_start();
	spi_write_byte(addr);

	for (i = 0; i < len; i++){
		*((data + i)) = spi_read_byte();
	}
	spi_stop();
}
Example #15
0
void spi_write (unsigned char* buf, int len)  
{  
    int i;  
    //spi_init();           
    //ss_enable(1);			
    //delay_us(10); 
	//for (i=0; i<len; i++)  
	//	spi_write_byte(buf[i]);  
    //delay_us(10);  
    //ss_enable(0);
	
	for (i=0; i<len; i++)
		spi_write_byte(buf[i]);		 
}  
Example #16
0
void spi_tx_word(uint8_t addr, uint16_t w)
{
	uint8_t i;

	spi_start();
	spi_write_byte((0x80|addr));//send addr
	for (i = 0; i< 16; i++){
		if (w & 0x8000) {
			spi_tx_bit(1);
		}
		else {
			spi_tx_bit(0);
		}
		w <<= 1;
	}
	spi_stop();
}
Example #17
0
uint16_t spi_rx_word(uint8_t addr)
{
	uint8_t i;
	uint16_t w = 0;
	uint8_t spi_bit;

	spi_start();
	spi_write_byte(addr);// send addr
	for (i = 0; i< 16; i++){
		spi_rx_bit(spi_bit);
		w <<= 1;
		if (spi_bit)
			w |= 1;
	}
	spi_stop();

	return w;
}
Example #18
0
void lcd_write_dat(u8 dat)
{
	ST_A0 = 1;
	spi_write_byte(dat);
	
}
Example #19
0
static void spi_write_com(u8 com)
{
	ST_A0 = 0;
	spi_write_byte(com);
}
Example #20
0
void show_time ( void )
{
    unsigned short nowtime;
    unsigned short ra;
    unsigned short rc;
    unsigned short ry;
    unsigned char ca,cb,cc;
    unsigned char base[4];

    nowtime =xstring[0]&0xF;
    nowtime<<=4;
    nowtime|=xstring[1]&0xF;
    nowtime<<=4;
    nowtime|=xstring[2]&0xF;
    nowtime<<=4;
    nowtime|=xstring[3]&0xF;
    if(nowtime==lasttime) return;

    lasttime=nowtime;

    for(ra=0;ra<4;ra++)
    {
        base[ra]=((xstring[ra]&0xF)<<2)+(xstring[ra]&0xF);
    }


    for(ry=0,ra=4;ra<44;ra++,ry++)
    {
        //P1OUT^=0x40;

        SPI_CS_ON;
        spi_delay();
        spi_write_byte(0x80);
        spi_write_sharp_address(ra&0xFF);

        spi_write_byte(0xFF);

        rc=base[0]+(ry>>3);
        ca=numfont[rc];

        for(cb=8;cb;cb>>=1)
        {
            if(cb&ca) cc=0x00; else cc=0xFF;
            spi_write_byte(cc);
        }

        spi_write_byte(0xFF);

        rc=base[1]+(ry>>3);
        ca=numfont[rc];

        for(cb=8;cb;cb>>=1)
        {
            if(cb&ca) cc=0x00; else cc=0xFF;
            spi_write_byte(cc);
        }

        spi_write_byte(0xFF);
        spi_write_byte(0xFF);

        spi_write_byte(0x00);
        spi_write_byte(0x00);
        spi_delay();
        SPI_CS_OFF;
    }

    for(ry=0,ra=44+8;ra<44+48;ra++,ry++)
    {

        SPI_CS_ON;
        spi_delay();
        spi_write_byte(0x80);
        spi_write_sharp_address(ra&0xFF);

        spi_write_byte(0xFF);

        rc=base[2]+(ry>>3);
        ca=numfont[rc];

        for(cb=8;cb;cb>>=1)
        {
            if(cb&ca) cc=0x00; else cc=0xFF;
            spi_write_byte(cc);
        }

        spi_write_byte(0xFF);

        rc=base[3]+(ry>>3);
        ca=numfont[rc];

        for(cb=8;cb;cb>>=1)
        {
            if(cb&ca) cc=0x00; else cc=0xFF;
            spi_write_byte(cc);
        }

        spi_write_byte(0xFF);
        spi_write_byte(0xFF);

        spi_write_byte(0x00);
        spi_write_byte(0x00);
        spi_delay();
        SPI_CS_OFF;
    }
}
Example #21
0
/**
 * @function:   spi_write_word
 * @brief:      Writes a word over the SPI interface
 */
void
spi_write_word(uint16_t data)
{
    spi_write_byte(data << 8);
    spi_write_byte(data);
}
Example #22
0
File: rf233.c Project: Idolf/tock
/**
 * \brief      read a received frame out of the radio buffer 
 * \param buf         pointer to where to copy received data
 * \param bufsize     Maximum size we can copy into bufsize
 * \return     Returns length of data read (> 0) if successful
 * \retval -1  Failed, was transmitting so FIFO is invalid
 * \retval -2  Failed, rx timed out (stuck in rx?)
 * \retval -3  Failed, too large frame for buffer
 * \retval -4  Failed, CRC/FCS failed (if USE_HW_FCS_CHECK is true)
 */
int rf233_read(void *buf, unsigned short bufsize) {
//  uint8_t radio_state;
  //uint8_t ed;       /* frame metadata */
  uint8_t frame_len = 0;
  uint8_t len = 0;
  char wbuf[PACKETBUF_SIZE];
  for (int i = 0; i < bufsize; i++) {
    wbuf[i] = 0;
  }

  PRINTF("RF233: Receiving.\n");
  
  if (pending_frame == 0) {
    PRINTF("RF233: No frame pending, abort.\n");
    return 0;
  }
  pending_frame = 0;
  
  /* get length of data in FIFO */
  spi_hold_low();
  spi_write_byte(TRX_CMD_FR);
  frame_len = spi_write_byte(0);

  if (frame_len <= 2 ||
      (frame_len - 2) > bufsize) {
    spi_release_low();
    flush_buffer();
    PRINTF("Frame (is not long enough or too large for buffer, abort.\n");
    return 0;
  }

  len = frame_len - 2;

  if (len > bufsize) {
    spi_release_low();
    /* too large frame for the buffer, drop */
    PRINTF("RF233: too large frame for buffer, dropping (%u > %u).\n", frame_len, bufsize);

    return 0;
  }
  PRINTF("RF233 read %u bytes:\n", frame_len);

  /* read out the data into the buffer, disregarding the length and metadata bytes */
  //spi_read_write_sync(wbuf, (char*)buf, len - 1);
  for (uint8_t i = 0; i < len - 1; i++) {
    uint8_t val = spi_write_byte(0);
    ((uint8_t*)buf)[i] = val;
    PRINTF("%02x ", ((uint8_t*)buf)[i]);
    if (i % 10 == 9) {
      PRINTF("\n");
    }
  }
  
  PRINTF("\n");
  spi_release_low();
  
  //trx_sram_read(1, (uint8_t *)buf, len);
  if (len >= 10) {
    header_t* header = (header_t*)buf;
    PRINTF("  FCF: %x\n", header->fcf);
    PRINTF("  SEQ: %x\n", header->seq);
    PRINTF("  PAN: %x\n", header->pan);
    PRINTF("  DST: %x\n", header->dest);
    PRINTF("  SRC: %x\n", header->src);
  }

  flush_buffer();

  return len;
}
Example #23
0
uint8_t nrf24_port_spi_write_byte(uint8_t data)
{
    return spi_write_byte(data);
}
Example #24
0
//-------------------------------------------------------------------
void notmain ( void )
{
    unsigned short ra;
    unsigned short rb;
    unsigned char rt;
    unsigned char up;
    unsigned char xbit;
    unsigned char xbyte;
    unsigned char y;
    //unsigned char lasty;

    WDTCTL = 0x5A80;

    // use calibrated clock
    DCOCTL = 0x00;
    BCSCTL1 = CALBC1_16MHZ;
    DCOCTL = CALDCO_16MHZ;


    //P1.0 LCD Power Control
    //P1.3 LCD Enable
    //P1.4 SPI CS
    //P1.5 SPI CLK
    //P1.6   led2
    //p1.7 SPI MOSI

    //76543210
    //11111001
    //0x00xxxx
    //x1xx1xx1




    P1DIR|=0xF9;
    P1OUT&=~(0xB0);
    P1OUT|=0x49;

    TACTL = 0x02E0;


    clear_screen();

    up=0;
    rt=1;
    y=5;
    xbit=0x04;
    xbyte=1;
    //lasty=y;

    while(1)
    {
        //for(ra=1;ra<=96;ra++)
        ra=y;
        {
            P1OUT^=0x40;
            SPI_CS_ON;
            spi_delay();
            spi_write_byte(0x80);
            spi_write_sharp_address(ra&0xFF);
            for(rb=0;rb<xbyte;rb++)
            {
                spi_write_byte(0);
            }
            spi_write_byte(xbit);
            for(rb++;rb<12;rb++)
            {
                spi_write_byte(0);
            }
            spi_write_byte(0x00);
            spi_write_byte(0x00);
            spi_delay();
            SPI_CS_OFF;
        }
        //ra=lasty;
        //{
            //P1OUT^=0x40;
            //SPI_CS_ON;
            //spi_delay();
            //spi_write_byte(0x80);
            //spi_write_sharp_address(ra&0xFF);
            //for(rb=0;rb<12;rb++)
            //{
                //spi_write_byte(0);
            //}
            //spi_write_byte(0x00);
            //spi_write_byte(0x00);
            //spi_delay();
            //SPI_CS_OFF;
        //}
        //lasty=y;
        if(up)
        {
            if(y==1)
            {
                up=0;
                y=2;
            }
            else y--;
        }
        else
        {
            if(y==96)
            {
                up=1;
                y=95;
            }
            else y++;
        }
        if(rt)
        {
            if(xbit==0x01)
            {
                if(xbyte==11)
                {
                    rt=0;
                }
                else
                {
                    xbit=0x80;
                    xbyte++;
                }
            }
            else
            {
                xbit>>=1;
            }
        }
        else
        {
            if(xbit==0x80)
            {
                if(xbyte==0)
                {
                    rt=1;
                }
                else
                {
                    xbit=0x01;
                    xbyte--;
                }
            }
            else
            {
                xbit<<=1;
            }
        }
    }
Example #25
0
/*
 * Function to write data to the Output FIFO
 */
static int __blsp_spi_write(struct ipq_spi_slave *ds, const u8 *cmd_buffer,
				unsigned int bytes)
{
	uint32_t val;
	unsigned int i;
	unsigned int write_len = bytes;
	unsigned int read_len = bytes;
	unsigned int fifo_count;
	int ret = SUCCESS;
	int state_config;

	state_config = config_spi_state(ds, QUP_STATE_RESET);
	if (state_config)
		return state_config;

	/* No of bytes to be written in Output FIFO */
	write32(ds->regs->qup_mx_output_count, bytes);
	write32(ds->regs->qup_mx_input_count, bytes);
	state_config = config_spi_state(ds, QUP_STATE_RUN);
	if (state_config)
		return state_config;

	/* Configure input and output enable */
	enable_io_config(ds, write_len, read_len);

	/*
	 * read_len considered to ensure that we read the dummy data for the
	 * write we performed. This is needed to ensure with WR-RD transaction
	 * to get the actual data on the subsequent read cycle that happens
	 */
	while (write_len || read_len) {

		ret = check_fifo_status(ds->regs->qup_operational);
		if (ret != SUCCESS)
			goto out;

		val = read32(ds->regs->qup_operational);
		if (val & OUTPUT_SERVICE_FLAG) {
			/*
			 * acknowledge to hw that software will write
			 * expected output data
			 */
			val &= OUTPUT_SERVICE_FLAG;
			write32(ds->regs->qup_operational, val);

			if (write_len > SPI_OUTPUT_BLOCK_SIZE)
				fifo_count = SPI_OUTPUT_BLOCK_SIZE;
			else
				fifo_count = write_len;

			for (i = 0; i < fifo_count; i++) {
				/* Write actual data to output FIFO */
				spi_write_byte(ds, *cmd_buffer);
				cmd_buffer++;
				write_len--;
			}
		}
		if (val & INPUT_SERVICE_FLAG) {
			/*
			 * acknowledge to hw that software
			 * will read input data
			 */
			val &= INPUT_SERVICE_FLAG;
			write32(ds->regs->qup_operational, val);

			if (read_len > SPI_INPUT_BLOCK_SIZE)
				fifo_count = SPI_INPUT_BLOCK_SIZE;
			else
				fifo_count = read_len;

			for (i = 0; i < fifo_count; i++) {
				/* Read dummy data for the data written */
				(void)spi_read_byte(ds);

				/* Decrement the read count after reading the
				 * dummy data from the device. This is to make
				 * sure we read dummy data before we write the
				 * data to fifo
				 */
				read_len--;
			}
		}
	}

out:
	/*
	 * Put the SPI Core back in the Reset State
	 * to end the transfer
	 */
	(void)config_spi_state(ds, QUP_STATE_RESET);

	return ret;
}
Example #26
0
uint8_t sd_write_multi_sector(uint32_t addr, uint8_t sector_num, uint8_t * buffer) {
    uint16_t i, time = 0;
    uint8_t r1;

    //set CS low  
    cs_enable();

    //send CMD25 for multiple block read  
    r1 = sd_send_cmd(CMD25, addr << 9, 0xff);
    //if CMD25 fail,return  
    if (r1 != 0x00) {
        //set CS high and send 8 clocks  
        cs_disable();
        return r1;
    }

    do {
        do {
            //send several dummy clocks  
            for (i = 0; i < 5; i++) {
                spi_write_byte(0xff);
            }

            //write start byte  
            spi_write_byte(0xfc);

            //write 512 byte of data  
            for (i = 0; i < 512; i++) {
                spi_write_byte(*buffer++);
            }

            //write 2 byte of CRC  
            spi_write_byte(0xff);
            spi_write_byte(0xff);

            //read response  
            r1 = spi_read_byte();
            time++;
            //if time out,set CS high and return r1  
            if (time > 254) {
                //set CS high and send 8 clocks  
                cs_disable();
                return r1;
            }
        } while ((r1 & 0x1f) != 0x05);
        time = 0;

        //check busy  
        do {
            r1 = spi_read_byte();
            time++;
            //if time out,set CS high and return r1  
            if (time > 30000) {
                //set CS high and send 8 clocks  
                cs_disable();
                return r1;
            }
        } while (r1 != 0xff);
        time = 0;
    } while (--sector_num);

    //send stop byte  
    spi_write_byte(0xfd);

    //check busy  
    do {
        r1 = spi_read_byte();
        time++;
        //if time out,set CS high and return r1  
        if (time > 30000) {
            //set CS high and send 8 clocks  
            cs_disable();
            return r1;
        }
    } while (r1 != 0xff);

    //set CS high and send 8 clocks  
    cs_disable();

    return 0;
}
Example #27
0
void cs_disable() {
    //set CS high  
    spi_disable();
    //send 8 clocks  
    spi_write_byte(0xFF);
}
Example #28
0
uint8_t sd_write_sector(uint32_t addr, uint8_t * buffer) {
    uint16_t i, time = 0;
    uint8_t r1;

    //set CS low  
    cs_enable();

    do {
        do {
            //send CMD24 for single block write  
            r1 = sd_send_cmd(CMD24, addr << 9, 0xff);
            time++;
            //if time out,set CS high and return r1  
            if (time > 254) {
                //set CS high and send 8 clocks  
                cs_disable();
                return r1;
            }
        } while (r1 != 0x00);
        time = 0;

        //send some dummy clocks  
        for (i = 0; i < 5; i++) {
            spi_write_byte(0xff);
        }

        //write start byte  
        spi_write_byte(0xfe);

        //write 512 bytes of data  
        for (i = 0; i < 512; i++) {
            spi_write_byte(buffer[i]);
        }

        //write 2 bytes of CRC  
        spi_write_byte(0xff);
        spi_write_byte(0xff);

        //read response  
        r1 = spi_read_byte();
        time++;
        //if time out,set CS high and return r1  
        if (time > 254) {
            //set CS high and send 8 clocks  
            cs_disable();
            return r1;
        }
    } while ((r1 & 0x1f) != 0x05);
    time = 0;

    //check busy  
    do {
        r1 = spi_read_byte();
        time++;
        //if time out,set CS high and return r1  
        if (time > 60000) {
            //set CS high and send 8 clocks  
            cs_disable();
            return r1;
        }
    } while (r1 != 0xff);

    //set CS high and send 8 clocks  
    cs_disable();

    return 0;
}