Example #1
0
int fzip()
{
	unsigned char * b, p;
	int i;

	do
	{
		b=read_byte(F_SRC);
		//printf("%d\n",(int)(*b));
		if (b!=NULL)
		{
			for (i=0;i<tree_map[*b].l;i++)
			{
				p=get_bit(tree_map[*b].code,i/8,i%8);
				if (p)
					write_bit(F_DST,1);
				else
					write_bit(F_DST,0);
			}
		}
	}while(b!=NULL);

	for (i=0;i<tree_map[256].l;i++)
	{
		p=get_bit(tree_map[256].code,i/8,i%8);
		if (p)
			write_bit(F_DST,1);
		else
			write_bit(F_DST,0);
	}

	return 0;
}
/**
 * Generates a write-0 or write-1 cycle.
 * Only call if dev->bus_master->touch_bit is NULL
 */
static void w1_gpio_write_bit(void *data, u8 bit)
{
	struct w1_gpio_msm_platform_data *pdata = data;
	void	(*write_bit)(void *, u8);
	unsigned long irq_flags;

	if (pdata->is_open_drain) {
		write_bit = w1_gpio_write_bit_val;
	} else {
		write_bit = w1_gpio_write_bit_dir;
	}

	spin_lock_irqsave(&w1_gpio_msm_lock, irq_flags);

	if (bit) {
		write_bit(data, 0);
		write_bit(data, 1);

		(pdata->slave_speed == 0)? w1_delay(64) : w1_delay(10);
	} else {
		write_bit(data, 0);
#ifdef CONFIG_W1_SLAVE_DS28E15 /* delay time for DS28EL15 */
		(pdata->slave_speed == 0)? w1_delay(60) : w1_delay(8);
#else /* delay time for DS28EL35 */
		(pdata->slave_speed == 0)? w1_delay(60) : w1_delay(9);
#endif
		write_bit(data, 1);
		(pdata->slave_speed == 0)? w1_delay(10) : w1_delay(10);
	}
	spin_unlock_irqrestore(&w1_gpio_msm_lock, irq_flags);

}
Example #3
0
void transmit(int gpio, unsigned int address, unsigned char receiver,
              unsigned char command) {
    unsigned int mask;

    sync_transmit(gpio);

    for (mask = 0x2000000; mask != 0x0; mask >>= 1) {
        if (address & mask) {
            write_bit(gpio, 1);
        } else {
            write_bit(gpio, 0);
        }
    }

    // never grouped
    write_bit(gpio, 0);

    write_bit(gpio, command);

    for (mask = 0b1000; mask != 0x0; mask >>= 1) {
        write_bit(gpio, receiver & mask);
    }

    write_interval_gap(gpio);
}
/**
 * Generates a write-0 or write-1 cycle.
 * Only call if dev->bus_master->touch_bit is NULL
 */
static void w1_gpio_write_bit(void *data, u8 bit)
{
	struct w1_gpio_msm_platform_data *pdata = data;
	void	(*write_bit)(void *, u8);
	unsigned long irq_flags;

	if (pdata->is_open_drain) {
		write_bit = w1_gpio_write_bit_val;
	} else {
		write_bit = w1_gpio_write_bit_dir;
	}

	spin_lock_irqsave(&w1_gpio_msm_lock, irq_flags);

	if (bit) {
		write_bit(data, 0);
		write_bit(data, 1);

		(pdata->slave_speed == 0)? w1_delay(64) : w1_delay(10);
	} else {
		write_bit(data, 0);
		(pdata->slave_speed == 0)? w1_delay(60) : w1_delay(8);
		write_bit(data, 1);
		(pdata->slave_speed == 0)? w1_delay(10) : w1_delay(5);
	}
	spin_unlock_irqrestore(&w1_gpio_msm_lock, irq_flags);

}
/**
 * Generates a write-1 cycle and samples the level.
 * Only call if dev->bus_master->touch_bit is NULL
 */
static u8 w1_gpio_read_bit(void *data)
{
	struct w1_gpio_msm_platform_data *pdata = data;
	int result;
	void	(*write_bit)(void *, u8);
	unsigned long irq_flags;

	if (pdata->is_open_drain) {
		write_bit = w1_gpio_write_bit_val;
	} else {
		write_bit = w1_gpio_write_bit_dir;
	}

	spin_lock_irqsave(&w1_gpio_msm_lock, irq_flags);

	/* sample timing is critical here */
	write_bit(data, 0);
	write_bit(data, 1);
	
	result = w1_gpio_read_bit_val(data);

	(pdata->slave_speed == 0)? w1_delay(55) : w1_delay(8);

	spin_unlock_irqrestore(&w1_gpio_msm_lock, irq_flags);
	
	return result & 0x1;
}
/**
 * Generates a write-0 or write-1 cycle.
 * Only call if dev->bus_master->touch_bit is NULL
 */
static void w1_gpio_write_bit_in8(void *data, u8 bit)
{
	struct w1_gpio_msm_platform_data *pdata = data;
	void	(*write_bit)(void *, u8);

	if (pdata->is_open_drain) {
		write_bit = w1_gpio_write_bit_val;
	} else {
		write_bit = w1_gpio_write_bit_dir;
	}

	if (bit) {
		write_bit(data, 0);
		write_bit(data, 1);

		(pdata->slave_speed == 0)? w1_delay(64) : w1_delay(10);
	} else {
		write_bit(data, 0);
#ifdef CONFIG_W1_SLAVE_DS28E15 /* delay time for DS28EL15 */
		(pdata->slave_speed == 0)? w1_delay(60) : w1_delay(8);
#else /* delay time for DS28EL35 */
		(pdata->slave_speed == 0)? w1_delay(60) : w1_delay(9);
#endif
		write_bit(data, 1);
		(pdata->slave_speed == 0)? w1_delay(10) : w1_delay(10);
	}
}
Example #7
0
// write encoded value to stream
void write_length( int value )
{
unsigned int mask = 32768;

	++value;
	
	// find highest bit set
	while ( !( value & mask ) )
		mask >>= 1;
		
	while( true )
	{
		// if mask equals 1, last bit has been written
		if ( mask == 1 )
		{
			write_bit( 0 );
			return;
		}
		
     		mask >>= 1;
     
     		// more data
     		write_bit(1);
		    
		// write data bit
		write_bit( value & mask );
	}
}
Example #8
0
//功能 : 写串口数据
void write_data(uchar data)
{
 CS;
 write_bit(0xfa);//写数据
 write_bit(data & 0xf0);//写高4位
 write_bit((data<<4) & 0xf0);//写低4位
 _delay_ms(2);
}
Example #9
0
//功能 : 写串口指令
void write_command(uchar command)
{
 CS;
 write_bit(0xf8);//串口格式,写控制指令
 write_bit(command & 0xf0);//写高4位
 write_bit((command<<4) & 0xf0);//写低4位
 _delay_ms(2);
}
Example #10
0
File: zx7b.c Project: DSkywalk/fase
unsigned char *compress(Optimal *optimal, unsigned char *input_data, size_t input_size, size_t *output_size) {
  size_t input_index;
  size_t input_prev;
  int offset1;
  int mask;
  int i;

  /* calculate and allocate output buffer */
  input_index= input_size-1;
  *output_size= (optimal[input_index].bits+16+7)/8;
  output_data= (unsigned char *)malloc(*output_size);
  if( !output_data )
    fprintf(stderr, "Error: Insufficient memory\n"),
    exit(1);

  /* un-reverse optimal sequence */
  optimal[input_index].bits= 0;
  while ( input_index > 0 )
    input_prev= input_index - (optimal[input_index].len > 0 ? optimal[input_index].len : 1),
    optimal[input_prev].bits= input_index,
    input_index= input_prev;

  output_index= 0;
  bit_mask= 0;

  /* first byte is always literal */
  write_byte(input_data[0]);

  /* process remaining bytes */
  while ( (input_index= optimal[input_index].bits) > 0)
    if( optimal[input_index].len == 0)
      write_bit(0),
      write_byte(input_data[input_index]);
    else{
      /* sequence indicator */
      write_bit(1);

      /* sequence length */
      write_elias_gamma(optimal[input_index].len-1);

      /* sequence offset */
      offset1= optimal[input_index].offset-1;
      if( offset1 < 128 )
        write_byte(offset1);
      else{
        offset1-= 128;
        write_byte((offset1 & 127) | 128);
        for ( mask= 1024; mask > 127; mask >>= 1)
          write_bit(offset1 & mask);
      }
    }

  /* end mark */
  write_bit(1);
  write_elias_gamma(0xff);
  return output_data;
}
Example #11
0
void write_compression(unsigned char sigbits[255], unsigned long bitencodings[255], FILE * fphuff, FILE * fporig)
{//write the huffman compression to the output file
	//compression variables
	int readchar;
	int numsignificant;
	unsigned long encoding;
	//1000 0000 0000 0000 0000 0000 0000 0000
	unsigned long mask = 0x80000000;

	bitspot = 0;
	currentbyte = 0;

	//write file 
	while (!feof(fporig))
	{//read every character in the file until the EOF is found
		readchar = fgetc(fporig);
		if (readchar != EOF) //keeps the standard EOF file from being written, we must write our own pseudo EOF
		{//save encoding, save amount of digits, shift the most significant bit to the far left
			encoding = bitencodings[(int) readchar];
			numsignificant = sigbits[(int) readchar];
			encoding <<= (32 - numsignificant);
			while (numsignificant > 0)
			{//loop until we have used all of the significant bits in our encoding
				if ((mask & encoding) == mask)
				{//if the furthest left bit is 1, pack a 1
					write_bit(fphuff, 1);
				} else
				{//if the furthest left bit is 0, pack a 0
					write_bit(fphuff, 0);
				}
				//set up the next bit to write, decrement the number of bits still to write
				encoding <<= 1;
				--numsignificant;
			}
		}
	}
	//write the pseudo EOF character, using ASCII NULL = 0000 0000
	encoding = bitencodings[0];
	numsignificant = sigbits[0];
	encoding <<= (32 - numsignificant);
	//same as above, except dedicated to writing pseudo EOF
	while (numsignificant > 0)
	{
		if ((mask & encoding) == mask)
		{
			write_bit(fphuff, 1);
		} else
		{
			write_bit(fphuff, 0);
		}
		encoding <<= 1;
		--numsignificant;
	}
	//finish off the byte with 0's and write it
	pad_file(fphuff);
	return;
}
Example #12
0
File: spi_bit.c Project: 42wim/ipxe
/**
 * Transfer bits over SPI bit-bashing bus
 *
 * @v bus		SPI bus
 * @v data_out		TX data buffer (or NULL)
 * @v data_in		RX data buffer (or NULL)
 * @v len		Length of transfer (in @b bits)
 * @v endianness	Endianness of this data transfer
 *
 * This issues @c len clock cycles on the SPI bus, shifting out data
 * from the @c data_out buffer to the MOSI line and shifting in data
 * from the MISO line to the @c data_in buffer.  If @c data_out is
 * NULL, then the data sent will be all zeroes.  If @c data_in is
 * NULL, then the incoming data will be discarded.
 */
static void spi_bit_transfer ( struct spi_bit_basher *spibit,
			       const void *data_out, void *data_in,
			       unsigned int len, int endianness ) {
	struct spi_bus *bus = &spibit->bus;
	struct bit_basher *basher = &spibit->basher;
	unsigned int sclk = ( ( bus->mode & SPI_MODE_CPOL ) ? 1 : 0 );
	unsigned int cpha = ( ( bus->mode & SPI_MODE_CPHA ) ? 1 : 0 );
	unsigned int bit_offset;
	unsigned int byte_offset;
	unsigned int byte_mask;
	unsigned int bit;
	unsigned int step;

	DBGC2 ( spibit, "SPIBIT %p transferring %d bits in mode %#x\n",
		spibit, len, bus->mode );

	for ( step = 0 ; step < ( len * 2 ) ; step++ ) {
		/* Calculate byte offset and byte mask */
		bit_offset = ( ( endianness == SPI_BIT_BIG_ENDIAN ) ?
			       ( len - ( step / 2 ) - 1 ) : ( step / 2 ) );
		byte_offset = ( bit_offset / 8 );
		byte_mask = ( 1 << ( bit_offset % 8 ) );

		/* Shift data in or out */
		if ( sclk == cpha ) {
			const uint8_t *byte;

			/* Shift data out */
			if ( data_out ) {
				byte = ( data_out + byte_offset );
				bit = ( *byte & byte_mask );
				DBGCP ( spibit, "SPIBIT %p wrote bit %d\n",
					spibit, ( bit ? 1 : 0 ) );
			} else {
				bit = 0;
			}
			write_bit ( basher, SPI_BIT_MOSI, bit );
		} else {
			uint8_t *byte;

			/* Shift data in */
			bit = read_bit ( basher, SPI_BIT_MISO );
			if ( data_in ) {
				DBGCP ( spibit, "SPIBIT %p read bit %d\n",
					spibit, ( bit ? 1 : 0 ) );
				byte = ( data_in + byte_offset );
				*byte &= ~byte_mask;
				*byte |= ( bit & byte_mask );
			}
		}

		/* Toggle clock line */
		spi_bit_delay();
		sclk ^= 1;
		write_bit ( basher, SPI_BIT_SCLK, sclk );
	}
}
Example #13
0
void write_chunk_fd_file(void *fptr, struct cp_file *file)
{
    int have_contents = !!(file->contents);
    write_string(fptr, file->filename);
    write_bit(fptr, &file->deleted, sizeof(int));
    write_bit(fptr, &file->size, sizeof(int));
    write_bit(fptr, &have_contents, sizeof(int));
    if (file->contents)
	write_bit(fptr, file->contents, file->size);
}
/**
 * Issues a reset bus sequence.
 *
 * @param  dev The bus master pointer
 * @return     0=Device present, 1=No device present or error
 */
static u8 w1_gpio_reset_bus(void *data)
{
	int result=1, i;
	struct w1_gpio_msm_platform_data *pdata = data;
	void	(*write_bit)(void *, u8);
	unsigned long irq_flags;
	int temp_read[MAX_SAMPLE]={'1',};
	int loop_cnt = 3;

	if (pdata->is_open_drain) {
		write_bit = w1_gpio_write_bit_val;
	} else {
		write_bit = w1_gpio_write_bit_dir;
	}

	spin_lock_irqsave(&w1_gpio_msm_lock, irq_flags);

	while (loop_cnt > 0) {
		write_bit(data, 0);
			/* minimum 48, max 80 us(In DS Documnet)
			 * be nice and sleep, except 18b20 spec lists 960us maximum,
			 * so until we can sleep with microsecond accuracy, spin.
			 * Feel free to come up with some other way to give up the
			 * cpu for such a short amount of time AND get it back in
			 * the maximum amount of time.
			 */
		(pdata->slave_speed == 0)? __const_udelay(500*UDELAY_MULT) : __const_udelay(50*UDELAY_MULT);
		write_bit(data, 1);

		(pdata->slave_speed == 0)? __const_udelay(60*UDELAY_MULT) : __const_udelay(1*UDELAY_MULT);

		for(i=0;i<MAX_SAMPLE;i++)
			temp_read[i] = gpio_get_value_msm(pdata->pin);

		for(i=0;i<MAX_SAMPLE;i++)
			result &= temp_read[i];

		/* minmum 70 (above) + 410 = 480 us
		 * There aren't any timing requirements between a reset and
		 * the following transactions.  Sleeping is safe here.
		 */
		/* w1_delay(410); min required time */
		(pdata->slave_speed == 0)? msleep(1) : __const_udelay(40*UDELAY_MULT);

		if (result)
			loop_cnt--;
		else
			break;
	}

	spin_unlock_irqrestore(&w1_gpio_msm_lock, irq_flags);

	return result;
}
Example #15
0
void oled_reset(void)
{
	// repeat 2 times
	write_bit(RES_n, 0);		//clr(CTRL, RES_n); //RES_n = 0;
	oled_delay(DELAY_CAL2);
	write_bit(RES_n, 1);		//set(CTRL, RES_n); //RES_n = 1;
	oled_delay(DELAY_CAL2);
	write_bit(RES_n, 0);		//clr(CTRL, RES_n); //RES_n = 0;
	oled_delay(DELAY_CAL2);
	write_bit(RES_n, 1);		//set(CTRL, RES_n); //RES_n = 1;
	oled_delay(DELAY_CAL2);
}
Example #16
0
File: zx7b.c Project: DSkywalk/fase
void write_elias_gamma(int value) {
  int bits= 0, rvalue= 0;
  while ( value>1 )
    ++bits,
    rvalue<<= 1,
    rvalue|= value&1,
    value>>= 1;
  while ( bits-- )
    write_bit(0),
    write_bit(rvalue & 1),
    rvalue>>= 1;
  write_bit(1);
}
Example #17
0
// FIXME: parity
void tape::write(uint8_t b) {
#if defined(PWM_DUTY)
	pwm.set_duty(PWM_DUTY);
#endif
	write_bit(0);
	for (int i = 0; i < _data_bits; i++) {
		write_bit(b & 1);
		b >>= 1;
	}
	write_bit(1);
	if (_stop_bits == 2)
		write_bit(1);
	pwm.set_duty(0);
}
Example #18
0
void oled_write_cmd(unsigned char command)
{
	write_bit(CS_n, 0);			//clr(CTRL, CS_n); //CS_n =0;
	oled_delay(DELAY_CAL);	
	write_bit(DC_n, 0);			//clr(CTRL, DC_n); //DC_n =0;
	oled_delay(DELAY_CAL);	

	unsigned char i;
	for(i = 0; i<8; i++)
	{
		unsigned char tmp;
		write_bit(SCLK, 0);		//clr(DATA, SCLK); // low
		oled_delay(DELAY_CAL);	
		
		tmp = check(command, (7-i));
		if(tmp != 0)
		{
			write_bit(SDIN, 1);		//set(DATA, SDIN); // data
			oled_delay(DELAY_CAL);	
			write_bit(SCLK, 1);		//set(DATA, SCLK); // hi
			oled_delay(DELAY_CAL);	
		}
				
		if(tmp == 0)
		{
			write_bit(SDIN, 0);		//clr(DATA, SDIN); // data
			oled_delay(DELAY_CAL);	
			write_bit(SCLK, 1);		//set(DATA, SCLK); // hi
			oled_delay(DELAY_CAL);	
		}
	}
	oled_delay(DELAY_CAL);	
	write_bit(SCLK, 0);				//set(CTRL, CS_n); //CS_n=1;
}
Example #19
0
// Write path in reverse to ouput
// root -> point
static void write_path_inverse() {
    // Index
    int i;

    // Write each direction as a bit
    for (i = path_size - 1 ; i >= 0 ; --i) {
        if (path[i] == 0) {
            // Left direction
            write_bit(0);
        } else {
            // Right direction
            write_bit(1);
        }
    }
}
Example #20
0
int i2c_bitbang_read_byte (struct i2c *base, u8 devaddr, u8 regoffset, u8 * value)
{
//	printf("i2c-bb read dev=%02x regoffset=%02x ", devaddr, regoffset);
	start(base);
	if (write_byte(base, (devaddr<<1))) { // send chip address for write command
		if (write_byte(base, regoffset)) { // send register offset - this defines where we want to read
			start(base);	// repeated start
			if (write_byte(base, (devaddr<<1) | 1)) { // send chip address for read command
				*value = read_byte(base);
//				printf("%02x\n", *value);
				write_bit(base, 1);	// NAK
				stop(base);	// send stop condition
				return 0;	// ok
			}
			else {
//				printf(" 2nd chip address error ");
			}
		}
		else {
//			printf(" offset error ");
		}
	}
	else {
//		printf(" chip address error ");
	}

	stop(base);	// send stop condition
	return 1;	// fail
}
Example #21
0
/********************************************************************
 * write_byte  
 * Writes one byte to the COM
 *
 * Inputs:  serdevice - opened file handle
 *          byte - byte to write 
 * 
 * Returns: nothing
 *
 ********************************************************************/
int write_byte(WEATHERSTATION ws, int byte) {
  int status;
  int i;
  char str[20];

  sprintf(str,"Writing byte %i",byte);
  print_log(3,str);

  for (i = 0; i < 8; i++)
  {
    write_bit(ws, byte & 0x80);
    byte <<= 1;
    byte &= 0xff;
  }

  set_RTS(ws,0);
  nanodelay();
  status = get_CTS(ws);
  //TODO: checking value of status, error routine
  nanodelay();
  set_DTR(ws,0);
  nanodelay();
  set_DTR(ws,1);
  nanodelay();
  if (status)
    return 1;
  else
    return 0;
}
Example #22
0
void compucolor_floppy_device::tx(UINT8 state)
{
	if (!m_sel && m_rw)
	{
		write_bit(state);
	}
}
Example #23
0
/**
 * Write one byte.
 * @param value  Value to write.
 * @param verify Whether the write has to be verified (default: true).
 * @return       Whether the write was successful.
 */
bool SerialInterface::write_byte(byte value, bool verify)
{
    //clog(trace) << "Send byte 0x" << std::hex << value << std::endl;

    for (size_t i = 0; i < 8; i++)
    {
        write_bit((value & 0x80) > 0);
        value <<= 1;
    }
    set_RTS(false);
    nanodelay();

    bool status = true;
    if (verify)
    {
        status = get_CTS();
        //TODO: checking value of status, error routine
        // TODO: exceptions?
        nanodelay();
        set_DTR(false);
        nanodelay();
        set_DTR(true);
        nanodelay();
    }
    return status;
}
static void test_serialization_2(void)
{
	byte serialized[2] = {0};
	byte const expected[2] =
	{
		0xfe,
		0x05
	};
	bit_writer writer = {&serialized[0], 1};
	writer = write_byte(writer, 0xff);
	writer = write_bit(writer, 0);
	writer = write_bit(writer, 1);
	TEST(writer.current_byte == &serialized[1]);
	TEST(writer.used_bits_in_byte == 3);
	TEST(memcmp(expected, serialized, sizeof(expected)) == 0);
}
Example #25
0
static int write_byte(struct i2c *base, int byte)
{
	int i;
	
	for (i=7; i>=0; i--)
		write_bit(base, (byte & (1<<i)) != 0);
	return read_bit(base) == 0; // ok (1) if NACK is asserted
}
Example #26
0
void pad_file(FILE * fphuff)
{//pad the remaining bits with 0's until the byte is written
	while (bitspot != 0)
	{
		write_bit(fphuff, 0);
	}
	return;
}
Example #27
0
void write_header(Node * root, FILE * fphuff)
{//write the header information recursively as a post-order tree traversal
	if (root != NULL)
	{
		if ((root -> left == NULL) && (root -> right == NULL))
		{//found a leaf node, header should always start with 1
			write_bit(fphuff, 1);
			write_bit_char(fphuff, root -> data);
			return;
		}
	}
	//post-order traversal of tree
	write_header(root -> left, fphuff);
	write_header(root -> right, fphuff);
	write_bit(fphuff, 0);
	return;
}
Example #28
0
void oled_init(void)
{
	oled_write_cmd(0x81); // contrast
	oled_write_cmd(0x00); // 0..0xFF
	oled_write_cmd(0xAF); // OLED on
	
	write_bit(1, 1);
}
Example #29
0
File: eeprom.c Project: shkmr/dfw
unsigned i2c_write(uint8_t x)
{
  int        i;
  
  for(i = 0; i < 8; i++) {
    write_bit(x&0x80);
    x  <<= 1;
  }
  return read_bit();
}
Example #30
0
void write_byte(uint8_t value)
{
	uint8_t i;
	for(i = 0; i < 8; i++)
	{
		write_bit(value & 0x01);
		value >>= 1; /* Shift to get ready for next bit */
		_delay_us(10); /* Give interrupt a chance to happen*/
	}
}