コード例 #1
0
ファイル: iha_spi.c プロジェクト: scj-devel/hvm-scj
void SPI0_Handler(void) {	
	uint16_t us_data;
	uint32_t us_tx_data;	
			
	uint8_t p_pcs;
	
	uint32_t status = spi_read_status(_spi_base);
		
	if (status & SPI_SR_RDRF) {		
		if ( spi_read( _spi_base, &us_data,	&p_pcs ) == SPI_OK ) {
			// store received byte
			fifo_push_uint8(_this->_spi_rx_fifo_desc, us_data);
			
			// If handler defined - call it with instance and received byte.
			if (_this->_call_back)
			{
				_this->_call_back(_this, (uint8_t)us_data);
			}
		}
	}	
	
	if (status & SPI_SR_TDRE) {
		// more bytes to send?		
		if ( fifo_pull_uint32(_this->_spi_tx_fifo_desc, &us_tx_data) == FIFO_OK ) {
			_spi_base->SPI_TDR = us_tx_data;
		} else {
			// No
			// Disable SPI TX interrupt
			spi_disable_interrupt(_spi_base, SPI_IDR_TDRE);
			_spi_active = 0;
		}
	}
}
コード例 #2
0
ファイル: bfin.c プロジェクト: bensteinberg/aleph
void bfin_get_num_params(volatile u32* num) {
  u16 x;

  app_pause();

  // command 
  spi_selectChip(BFIN_SPI, BFIN_SPI_NPCS);
  spi_write(BFIN_SPI, MSG_GET_NUM_PARAMS_COM);
  spi_unselectChip(BFIN_SPI, BFIN_SPI_NPCS);

  print_dbg("\r\n : spi_write MSG_GET_NUM_PARAMS");

  // read num
  spi_selectChip(BFIN_SPI, BFIN_SPI_NPCS);
  spi_write(BFIN_SPI, 0); //dont care
  spi_read(BFIN_SPI, &x);
  spi_unselectChip(BFIN_SPI, BFIN_SPI_NPCS);  
  *num = (u8)(x & 0xff);

  print_dbg("\r\n : spi_read numparams: ");
  print_dbg_ulong(*num);

  app_resume();

}
コード例 #3
0
ファイル: pfio.c プロジェクト: m15k/piface
uint8_t pfio_read_input(void)
{
    // XOR by 0xFF so we get the right outputs.
    // before a turned off input would read as 1,
    // confusing developers.
    return spi_read(INPUT_PORT) ^ 0xFF;
}
コード例 #4
0
void platform_wifi_spi_rx_dma_irq(void)
{
  uint8_t junk1;
  uint16_t junk2;
  pdc_packet_t pdc_spi_packet = { 0, 1 };
  Pdc* spi_pdc  = spi_get_pdc_base( wifi_spi.port );
  
  uint32_t status = spi_read_status( wifi_spi.port );
  uint32_t mask = spi_read_interrupt_mask( wifi_spi.port );
  
  if ( ( mask & SPI_IMR_RXBUFF ) && ( status & SPI_SR_RXBUFF ) )
  {
    pdc_disable_transfer( spi_pdc, PERIPH_PTCR_RXTDIS | PERIPH_PTCR_TXTDIS );
    pdc_rx_init( spi_pdc, &pdc_spi_packet, NULL );
    pdc_tx_init( spi_pdc, &pdc_spi_packet, NULL );
    spi_disable_interrupt( wifi_spi.port, SPI_IER_RXBUFF );
  }
  
  if ( ( mask & SPI_IMR_ENDTX ) && ( status & SPI_SR_ENDTX ) )
  {
    pdc_disable_transfer( spi_pdc, PERIPH_PTCR_TXTDIS );
    pdc_tx_init( spi_pdc, &pdc_spi_packet, NULL );
    spi_disable_interrupt( wifi_spi.port, SPI_IER_ENDTX );
    /* Clear SPI RX data in a SPI send sequence */
    spi_read( wifi_spi.port, &junk2, &junk1);
  }
  
  mico_rtos_set_semaphore( &spi_transfer_finished_semaphore );
}
コード例 #5
0
//芯片探测
int w25qxx_probe(void)
{
    uint32_t i;
    uint16_t id;
    uint8_t buf[4];
    buf[0] = W25X_ManufactDeviceID;
    buf[1] = 0;
    buf[2] = 0;
    buf[3] = 0;
    /* read id */
    spi_write(&device, buf, 4, false);
    spi_read(&device, buf, 2, true);
    id = ((buf[0]<<8) + buf[1]);
    W25QXX_TRACE("ID:%d\r\n", id);
    //see if we find a match
    for(i = 0; i< ARRAY_SIZE(w25qxx_attr_table);i++)
    {
        if(w25qxx_attr_table[i].id == id)
        {
            // find a match
            w25qxx_type.name = w25qxx_attr_table[i].name;
            w25qxx_type.id = w25qxx_attr_table[i].id;
            w25qxx_type.size = w25qxx_attr_table[i].size;
            w25qxx_power_up();
            buf[0] = w25qxx_read_sr();
            W25QXX_TRACE("SR:0x%X\r\n", buf[0]);
            buf[0] = w25qxx_read_sr2();
            W25QXX_TRACE("SR2:0x%X\r\n", buf[0]);
            // enable full access to all memory regin, something like unlock chip.
            w25qxx_write_sr(0x00);
            return SPI_EOK; 
        }
    }
    return SPI_ERROR;
}
コード例 #6
0
ファイル: spi_drv.c プロジェクト: stas2z/linux-3.10-witi
/*----------------------------------------------------------------------*/
static void eeprom_get_status_reg(u8 *status) 
{
	spi_chip_select(ENABLE);
	spi_write(RDSR_CMD);
	*status = spi_read();		
	spi_chip_select(DISABLE);
}
コード例 #7
0
/*----------------------------------------------------------------------*/
unsigned char spi_eeprom_read(u16 address, u16 nbytes, u8 *dest)
{
	u8	status;
	u16	cnt = 0;
	int i = 0;

	do {
		i++;
		eeprom_get_status_reg(&status);		
	}
	while((status & (1<<RDY)) && (i < max_ee_busy_loop));

	if (i == max_ee_busy_loop)
		return (status);

	/* eeprom ready */
	if (!(status & (1<<RDY))) {	
		spi_chip_select(ENABLE);
		/* read op */
		spi_write(READ_CMD);
		spi_write((u8)(address >> 8));		/* MSB byte First */
		spi_write((u8)(address & 0x00FF));	/* LSB byte */

		while (cnt < nbytes) {
			*(dest++) = spi_read();
			cnt++;
		}
		status = 0;
		/* deassert cs */
		spi_chip_select(DISABLE);
	} 
コード例 #8
0
ファイル: spi1-test.c プロジェクト: JamesLinus/ARM-Ports
int main(void)
{
	int counter = 0;
	uint16_t rx_value = 0x42;

/* Setup Rx/Tx buffers for USART */
	buffer_init(send_buffer,BUFFER_SIZE);
	buffer_init(receive_buffer,BUFFER_SIZE);

	clock_setup();
	gpio_setup();
	usart_setup();
    usart_print_string("SPI-DMA Test\n\r");
	spi_setup();

	while (1) {

        gpio_toggle(GPIOA, GPIO1);

#ifdef LOOPBACK
/* Print what is going to be sent on the SPI bus */
		usart_print_string("Sending  packet ");
        usart_print_int(counter);
        usart_print_string("\n\r");
		spi_send(SPI1, (uint8_t) counter);
		rx_value = spi_read(SPI1);
		usart_print_string("Received  packet ");
        usart_print_int(rx_value);
        usart_print_string("\n\r");
        counter++;
#else
/* This is a 1-byte "reset" command to SD card */
		spi_send(SPI1, 0x40);
		spi_send(SPI1, 0x00);
		spi_send(SPI1, 0x00);
		spi_send(SPI1, 0x00);
		spi_send(SPI1, 0x00);
		spi_send(SPI1, 0x95);
	/* Read the byte that just came in (use a loopback between MISO and MOSI
	 * to get the same byte back)
	 */
		rx_value = spi_read(SPI1);
#endif
	}

	return 0;
}
コード例 #9
0
ファイル: bfin.c プロジェクト: bensteinberg/aleph
// get parameter value
s32 bfin_get_param(u8 idx) {
  ParamValueCommon pval;
  u16 x;
  
  app_pause();

  // command
  spi_selectChip(BFIN_SPI, BFIN_SPI_NPCS);
  spi_write(BFIN_SPI, MSG_GET_PARAM_COM);
  spi_unselectChip(BFIN_SPI, BFIN_SPI_NPCS);

  // idx
  spi_selectChip(BFIN_SPI, BFIN_SPI_NPCS);
  spi_write(BFIN_SPI, idx);
  spi_unselectChip(BFIN_SPI, BFIN_SPI_NPCS);

  /// read value
  spi_selectChip(BFIN_SPI, BFIN_SPI_NPCS);
  spi_write(BFIN_SPI, 0); // don't care
  spi_read(BFIN_SPI, &x);
  spi_unselectChip(BFIN_SPI, BFIN_SPI_NPCS);
  pval.asByte[0] = (u8)x;

  spi_selectChip(BFIN_SPI, BFIN_SPI_NPCS);
  spi_write(BFIN_SPI, 0); // don't care
  spi_read(BFIN_SPI, &x);
  spi_unselectChip(BFIN_SPI, BFIN_SPI_NPCS);
  pval.asByte[1] = (u8)x;

  spi_selectChip(BFIN_SPI, BFIN_SPI_NPCS);
  spi_write(BFIN_SPI, 0); // don't care
  spi_read(BFIN_SPI, &x);
  spi_unselectChip(BFIN_SPI, BFIN_SPI_NPCS);
  pval.asByte[2] = (u8)x;

  spi_selectChip(BFIN_SPI, BFIN_SPI_NPCS);
  spi_write(BFIN_SPI, 0); // don't care
  spi_read(BFIN_SPI, &x);
  spi_unselectChip(BFIN_SPI, BFIN_SPI_NPCS);
  pval.asByte[3] = (u8)x;

  app_resume();

  return pval.asInt;
  
}
コード例 #10
0
//芯片读
static uint8_t w25qxx_read_sr2(void)
{
    uint8_t buf[1];
    buf[0] = W25X_ReadStatusReg2;
    spi_write(&device, buf, 1, false); //false = 保持片选,继续发送
    spi_read(&device, buf, 1, true);
    return buf[0];
}
コード例 #11
0
ファイル: accel_sensor.c プロジェクト: rtr14/libfsw_sensor
static void accel_sensor_write_reg(spi_chip_t * chip, uint8_t reg_base) {
	if (spi_lock_dev(chip->spi_dev) <0)
		return;
	spi_write(chip, reg_base);
	//printf("Writing %#x\r\n", ((reg_base + 1) << 8) | 0x8000 | ((value >> 8) & 0xff));
	spi_read(chip);
	spi_unlock_dev(chip->spi_dev);
}
コード例 #12
0
ファイル: spi.c プロジェクト: eerimoq/simba
ssize_t spi_get(struct spi_driver_t *self_p,
                uint8_t *data_p)
{
    ASSERTN(self_p != NULL, EINVAL);
    ASSERTN(data_p != NULL, EINVAL);

    return (spi_read(self_p, data_p, 1));
}
コード例 #13
0
ファイル: ms5611.c プロジェクト: melsabae/shc_one_month
static inline uint32_t ms5611_convert_pressure(void)
{
	uint32_t data;
	spi_select();
	spi_write(0x48);	//highest resolution option
	spi_deselect();
	//delay_ms(10);
	delay_ms(10);
	spi_select();
	spi_write(0x00);
	data = ((uint32_t)spi_read())<<16;
	data+= ((uint32_t)spi_read())<<8;
	data+= ((uint32_t)spi_read());
	spi_deselect();
	delay_ms(1);
	return data;
}
コード例 #14
0
ファイル: as3935.c プロジェクト: na1pir/as3935_esp8266_iot
void ICACHE_FLASH_ATTR as3935_set_tuning_capacitor(uint8_t cap){
	HSPI_INIT_STUF;
	as3935.x8.d8=spi_read(8);
	as3935.x8.a8.TUN_CAP=cap;
	
	os_printf("\nsetting tun cap to: %d, %d\n",tuncaplookuptable[cap],cap );
	spi_write(8,as3935.x8.d8);		
}
コード例 #15
0
ファイル: adc.c プロジェクト: Someone101/aleph
// perform a conversion on all 4 channels
static void adc_convert(u16 (*dst)[4]) {
#if 1
#else
  u16 cmd, val;

  // data into AD7923 is a left-justified 12-bit value in a 16-bit word
  // so, always lshift the command before sending
  cmd = ( AD7923_CMD_BASE ) << 4;
  spi_selectChip(ADC_SPI, ADC_SPI_NPCS);
  spi_write(ADC_SPI, cmd);
  spi_unselectChip(ADC_SPI, ADC_SPI_NPCS);

  // get channel 0, setup channel 1
  cmd = ( AD7923_CMD_BASE | AD7923_CTL_ADD0 ) << 4;
  spi_selectChip(ADC_SPI, ADC_SPI_NPCS);
  spi_write(ADC_SPI, cmd);
  spi_read(ADC_SPI, &val);
  spi_unselectChip(ADC_SPI, ADC_SPI_NPCS);
  (*dst)[0] = val & 0xfff; 

  // get channel 1, setup channel 2
  cmd = ( AD7923_CMD_BASE | AD7923_CTL_ADD1 ) << 4;
  spi_selectChip(ADC_SPI, ADC_SPI_NPCS);
  spi_write(ADC_SPI, cmd);
  spi_read(ADC_SPI, &val);
  spi_unselectChip(ADC_SPI, ADC_SPI_NPCS);
  (*dst)[1] = val & 0xfff;

  // get channel 2, setup channel 3
  cmd = ( AD7923_CMD_BASE | AD7923_CTL_ADD1 | AD7923_CTL_ADD0 ) << 4;
  spi_selectChip(ADC_SPI, ADC_SPI_NPCS);
  spi_write(ADC_SPI, cmd);
  spi_read(ADC_SPI, &val);
  spi_unselectChip(ADC_SPI, ADC_SPI_NPCS);
  (*dst)[2] = val & 0xfff;

  // get channel 3, dummy write
  cmd = ( AD7923_CMD_BASE ) << 4;
  spi_selectChip(ADC_SPI, ADC_SPI_NPCS);
  spi_write(ADC_SPI, cmd);
  spi_read(ADC_SPI, &val);
  spi_unselectChip(ADC_SPI, ADC_SPI_NPCS);
  (*dst)[3] = val & 0xfff;

#endif
}
コード例 #16
0
ファイル: test_spi.c プロジェクト: PrinceBalabis/mahm3lib
void test_spi_baud_rate_change(void) {
	delay_ms(1);
	uint16_t data1 = 0b0101011101001001;
	spi_set_selector_bit_length(SPI0, SPI_SELECTOR_0, SPI_BITS_16);
	spi_set_selector_clk_polarity(SPI0, SPI_SELECTOR_0, SPI_POLARITY_LOW);
	spi_set_selector_clk_phase(SPI0, SPI_SELECTOR_0, SPI_PHASE_LOW);
	// test case begin
	spi_set_selector_baud_rate(SPI0, SPI_SELECTOR_0, 1);
	spi_write(SPI0, data1); // We send both bytes as 16 consecutive bits
	delay_ms(1);
	TEST_ASSERT_EQUAL_HEX32(data1, spi_read(SPI0));
	// new test
	spi_set_selector_baud_rate(SPI0, SPI_SELECTOR_0, 2);
	spi_write(SPI0, data1); // We send both bytes as 16 consecutive bits
	delay_ms(1);
	TEST_ASSERT_EQUAL_HEX32(data1, spi_read(SPI0));
}
コード例 #17
0
ファイル: baro_ms5611.c プロジェクト: cuspaceflight/Woodchuck
/*
 * Reads an int24 from the MS5611 address `adr`, stores it to `d`.
 */
static void ms5611_read_s24(uint8_t adr, int32_t* d)
{
    uint32_t data_received; //To store result
    uint8_t adc_adr = 0x00; //Command to read ADC from baro

    spi_enable(MS5611_SPID);    // The SPI peripheral is enabled. // CHIBIOS spiSelect(&MS5611_SPID);
    spi_send8(MS5611_SPID, adr); // Data is written to the SPI interface after the previous write transfer has finished.
    
    //TODO: Sleep for 1 ms
    /*
     * Wait for conversion to complete. There doesn't appear to be any way
     * to do this without timing it, unfortunately.
     *
     * If we just watch the clock we'll consume enormous CPU time for little
     * gain. Instead we'll sleep for "roughly" 1ms and then wait until at least
     * the desired 0.6ms have passed.
     */
    
    spi_send8(MS5611_SPID, adc_adr); // Asks the device to send the results from the ADC.
    data_received = spi_read(MS5611_SPID); //A ADC_READ command returns a 3 byte result

    *d = data_received;
    
    /*
    //OLD CODE:
    uint8_t adc_adr = 0x00, rx[3];
    int32_t t0;
    
    */
    /* Start conversion */
    //spiSelect(&MS5611_SPID);
    //spiSend(&MS5611_SPID, 1, (void*)&adr);
    
    /*
     * Wait for conversion to complete. There doesn't appear to be any way
     * to do this without timing it, unfortunately.
     *
     * If we just watch the clock we'll consume enormous CPU time for little
     * gain. Instead we'll sleep for "roughly" 1ms and then wait until at least
     * the desired 0.6ms have passed.
     */
   // t0 = halGetCounterValue();
    //chThdSleepMilliseconds(1);
    //while(halGetCounterValue() - t0 < US2RTT(600)) {
   //     chThdYield();
    //}

    /* Deassert CS */
    //spiUnselect(&MS5611_SPID);

    /* Read ADC result */
//    spiSelect(&MS5611_SPID);
  //  spiSend(&MS5611_SPID, 1, (void*)&adc_adr);
   // spiReceive(&MS5611_SPID, 3, (void*)rx);
    //spiUnselect(&MS5611_SPID);

   // *d = rx[0] << 16 | rx[1] << 8 | rx[2];
}
コード例 #18
0
ファイル: rs_gyro.c プロジェクト: yamanekko/mruby-rs-robot
static mrb_value
mrb_rs_gyro_initialize(mrb_state *mrb, mrb_value self)
{
	//TODO update default value with parameters
	// gpio 7, 8, 9, 10, 11

    unsigned int ra;
    unsigned int ra2;
	unsigned int rcv_data = 0;

    ra=GET32(AUX_ENABLES);
    ra|=2; //enable spi0
    PUT32(AUX_ENABLES,ra);

    ra=GET32(GPFSEL0);
    ra&=~(7<<27); //gpio9	//MISO
    ra|=4<<27;    //alt0
    //ra|=1<<27;    //output
    ra&=~(7<<24); //gpio8	//CE0
    ra|=4<<24;    //alt0
    ra&=~(7<<21); //gpio7	//CE1; not used
    //ra|=4<<21;    //alt0
    ra|=1<<21;    //output
    PUT32(GPFSEL0,ra);
    ra=GET32(GPFSEL1);
    ra&=~(7<<0); //gpio10	//MOSI
    ra|=4<<0;    //alt0
    ra&=~(7<<3); //gpio11	//SCLK
    ra|=4<<3;    //alt0
    PUT32(GPFSEL1,ra);

    *SPI_CONTROL = 0;
	*SPI_CONTROL &= ~(1 << SPI_CS_CSPOL0); //LOW
	*SPI_CONTROL &= ~((1 << SPI_CS_CS0) | (1 << SPI_CS_CS1));//CS0
	// clear send/receive buffer
	*SPI_CONTROL |= (1 << SPI_CS_CLEAR_RX) | (1 << SPI_CS_CLEAR_TX);
	*SPI_CONTROL |= SPI_CS_MODE_00;

    *SPI_CLK = 128;
    // read "Who am I" reg(0x0F) for test
    rcv_data = spi_read(0x0F);

   // 0xD4 is collect
    if(rcv_data != 0xd4){
    	return mrb_false_value();
    }

    // delay 500msec
    for(ra2=0;ra2<0x500000;ra2++) dummy(ra2);

    spi_write(0x20, 0xCF);
    spi_write(0x23, (0x01000000));

    // delay 500msec
    for(ra2=0;ra2<0x500000;ra2++) dummy(ra2);

	return self;
}
コード例 #19
0
ファイル: rtc.c プロジェクト: frazahod/SUBGEN-NuLAB-EcoLAB-
void RTC_read_alarm(){

   int8 RTC_buffer;
   
   RTC_buffer = 0;
   
   setup_spi(SPI_MASTER|SPI_MODE_0_0|SPI_CLK_DIV_16);
   
   output_bit(RTC_CS, ENABLE);
   RTC_buffer = spi_read(0x0A);
   RTC_Al_Mon_Reg = spi_read(RTC_buffer);
   RTC_Al_DOM_Reg = spi_read(RTC_buffer);
   RTC_Al_Hr_Reg = spi_read(RTC_buffer);
   RTC_Al_Min_Reg = spi_read(RTC_buffer);
   RTC_Al_Sec_Reg = spi_read(RTC_buffer);
   RTC_Flags_Reg = spi_read(RTC_buffer);
   output_bit(RTC_CS, DISABLE);
   
   RTC_Al_Mon_Reg = RTC_Al_Mon_Reg & 0b00011111;
   RTC_Al_Mon_Reg = Bcd2Dec(RTC_Al_Mon_Reg);
   RTC_Al_DOM_Reg = RTC_Al_DOM_Reg & 0b00111111;
   RTC_Al_DOM_Reg = Bcd2Dec(RTC_Al_DOM_Reg);
   RTC_Al_Hr_Reg = RTC_Al_Hr_Reg & 0b00111111;
   RTC_Al_Hr_Reg = Bcd2Dec(RTC_Al_Hr_Reg);
   RTC_Al_Min_Reg = RTC_Al_Min_Reg & 0b01111111;
   RTC_Al_Min_Reg = Bcd2Dec(RTC_Al_Min_Reg);
   RTC_Al_Sec_Reg = RTC_Al_Sec_Reg & 0b01111111;
   RTC_Al_Sec_Reg = Bcd2Dec(RTC_Al_Sec_Reg);
}
コード例 #20
0
ファイル: EEPROM.c プロジェクト: josefvargasr/AppTemplateV1.1
/*******************************************************************************
*	The Chip Erase function will erase all bits (0xFF) in the array. 
*
*	While the device is executing the chipErase() function, the WriteInProcess() 
*	macro can be read to determine when the Chip Erase function is complete.
*
*******************************************************************************/
void EEPROM_chipErase(){
	unsigned short r_data = 0;
	unsigned char r_pcs;
	
	_EEPROM_wren();
	spi_write(SPI, CE, spi_get_pcs(3),  1);
	while((spi_read_status(SPI) & SPI_SR_RDRF) == 0);
	spi_read(SPI, (uint16_t*) &r_data, (uint8_t*) &r_pcs);
}
コード例 #21
0
uint8_t enc28j60_readOp(enc28j60_connection* c, uint8_t op, uint8_t address) {
  uint8_t ret;
  
  spi_cs_on(c->cs_pin);

  // issue read command
  spi_write(op | (address & ADDR_MASK));
  // read data
  ret = spi_read();
  // do dummy read if needed (for mac and mii, see datasheet page 29)
  if(address & 0x80) 
    ret = spi_read();
  
  // release CS
  spi_cs_off(c->cs_pin);
  
  return ret;
}
コード例 #22
0
ファイル: ctx2050_spi.c プロジェクト: philsmd/sharpfin
int spidata_read(unsigned char * buffer, int len)
{
  int result = spi_read(ctx2050_data_dev, buffer, len);
  if (result < 0)
  {
    printk(PREFIX "data read fail %d\n", result);
  }
  return result;
}
コード例 #23
0
//读Y坐标的数据
uint32_t ads7843_readY(int * value)
{
    uint8_t buf[2];
    buf[0] = ADS7843_CMD_READ_Y;
    spi_write(&device, buf, 1, false);
    spi_read(&device, buf, 2, true);
    *value = ((buf[0]<<8) + buf[1])>>4; //12bit mode
    return SPI_EOK;
}
コード例 #24
0
ファイル: display.c プロジェクト: bezourokq/Colonia-de-Robo
void ssp_isr(void){
   if(spi_data_is_in()) {
         disable_interrupts(INT_SSP); 
         x = spi_read();//Leitura do Valor recebido pela comunicação
         printf(lcd_putc, "\f Hex %x \n",x); //Escreve no display o valor recebido em Hexa
         printf(lcd_putc, "\Long1 %d",x); //Escreve no display o valor recebido em Long
         enable_interrupts(INT_SSP); 
      }
}
コード例 #25
0
static uint8_t w25qxx_read_sr(void)
{
    uint8_t buf[1];
    buf[0] = W25X_ReadStatusReg;
    spi_write(&device, buf, 1, false); //false = 保持片选,继续发送
    spi_read(&device, buf, 1, true);
    W25QXX_TRACE("W25QXX BUSY\r\n", buf[0]);
    return buf[0];
}
コード例 #26
0
static int sd_generic_read(struct sd_host *host,
			   u8 opcode, u32 arg,
			   void *data, size_t len, int token)
{
	struct sd_command *cmd = &host->cmd;
	u16 crc, calc_crc = 0xffff;
	u8 *d;
	size_t l;
	int retval;

	/* build raw command */
	sd_cmd(cmd, opcode, arg);

	/* select card, send command and wait for response */
	retval = sd_start_command(host, cmd);
	if (retval < 0)
		goto out;
	if (retval != 0x00) {
		retval = -EIO;
		goto out;
	}

	/* wait for read token, then read data */
	retval = sd_read_data(host, data, len, token);
	if (retval < 0)
		goto out;	

	/* read trailing crc */
	spi_read(host, &crc, sizeof(crc));

	retval = 0;

	/* FIXME, rewrite this a bit */
	{
		calc_crc = 0;
		d = data;
		l = len;

		while (l-- > 0)
			calc_crc = crc_xmodem_update(calc_crc, *d++);

		if (calc_crc != crc)
			retval = -EIO;
	}

out:
	/* burn extra cycles and deselect card */
	sd_end_command(host);

	if (retval < 0) {
		DBG("read, offset=%u, len=%d\n", arg, len);
		DBG("crc=%04x, calc_crc=%04x, %s\n", crc, calc_crc,
		    (retval < 0) ? "failed" : "ok");
	}

	return retval;
}
コード例 #27
0
ファイル: pmodad1.c プロジェクト: CSIEMIAT/linux-3.6.0-MIAT
/* 
 * Driver read function 
 *
 * This function uses a generic SPI read to read values from the Pmod.
 * It will only read full values, so if the length from user space is
 * not a multiple of 2, it will read up to length - 1 bytes.
 *
 * Function can possibly error out if:
 *		The mutex cannot be locked
 *		spi_read fails on the first read
 *
 * Otherwise, the function returns the number of successful values read,
 * each with a size of 2 bytes.  So for instance, if 13 bytes are read,
 * the function will return 12, indicating 6 values were read successfully
 * from the pmod.  Additionally, if copy_to_user cannot successfully
 * copy everything, the number of successfully copied full values (2 bytes)
 * will be returned.
 *
 * We use goto in this function because there are multiple exit points,
 * and it prevents us from having to call mutex_unlock() for the mutex
 * each time.
 */
static ssize_t	pmodad1_read(struct file *fp, char __user *buffer, size_t length, loff_t *offset)
{
	int status;		/* spi_read return value */
	int num_reads;		/* Number of values to read from Pmod */
	int i;
	ssize_t retval;		/* Function return value */
	ssize_t ret;		/* copy_to_user return value */
	unsigned short buf;     /* Temporary storage for each read value */
	struct pmodad1_device *dev;

	dev = fp->private_data;	
	status = 0;
	num_reads = length/2;	 

	if (mutex_lock_interruptible(&dev->mutex)) {
		retval = -ERESTARTSYS;
		goto lock_err;
	}

	if (buffer == NULL) {
		retval = -EINVAL;
		goto read_out;
	}


	for (i = 0; i < num_reads; i++) {
		/* Use generic SPI read */
		status = spi_read(dev->spi, &buf, 2);
		if (status)
			break;
		/* Change endianness of result, if necessary
		 * The result from the Pmod hardware is big endian,
		 * whereas Microblaze and other CPU architectures are
		 * little endian.
		 */
		dev->val_buf[i] = be16_to_cpu(buf) & 0x0FFF;  // only 12 bits matters
	}

	if (i == 0) {
		dev_err(&dev->spi->dev, "SPI read failure: %d\n", status);
		retval = status;
		goto read_out;
	}

	/* 
	 * Only copy full values (2 bytes) in the case of a user space length
	 *	that is not a multiple of 2.
	 */
	ret = copy_to_user((void *)buffer, (void *)dev->val_buf, i*2);

	retval = num_reads*2 - (ret + (ret % 2));
read_out:
	mutex_unlock(&dev->mutex);
lock_err:
	return retval;	
}
コード例 #28
0
/** Write a byte out in master mode and receive a value
 *
 * @param[in] obj   The SPI peripheral to use for sending
 * @param[in] value The value to send
 * @return Returns the value received during send
 */
int  spi_master_write(spi_t *obj, int value){
	spi_status_t status=spi_write(obj->spi.spi_base,(uint16_t)value,obj->spi.cs,SPI_LAST);
	if(status ==SPI_OK){
		uint16_t data;
		status =spi_read(obj->spi.spi_base,&data,&obj->spi.cs);
		if(status == SPI_OK)
			return data;
	}
	return 0;
}
コード例 #29
0
ファイル: aj_spi.c プロジェクト: durake/core-ajtcl
aj_spi_status AJ_SPI_READ(Spi* p_spi, uint8_t* us_data, uint8_t* p_pcs)
{
    aj_spi_status status;
    uint16_t data;
    status = spi_read(p_spi, &data, p_pcs);
    AJ_ASSERT(status == SPI_OK);
    *us_data = data & 0xFF;
    //    AJ_InfoPrintf(("=R= %02x\n", (*us_data) & 0xFF));
    return status;
}
コード例 #30
0
ファイル: RFM.c プロジェクト: d21d3q/czterokopter_pilot
/*************************************
the reset of software 
**************************************/
void software_reset(void)
{
	spi_write_command(0x0780);
	while (!(0x02&spi_read(INTERRUPT_STATUS_2)));
	//RF23B_init_parameter();
	//while (!((1<<ICHIPRDY)&spi_read(INTERRUPT_STATUS_2)));
	
	/*#ifdef ODMIERZANIE_CZASU
	typ_czasu temp=czasomierz;
	
	do 
	{
		#ifdef _RFM_NIRQ
		if(!_RFM_NIRQ)
		#else
		if(spi_read(INTERRUPT_STATUS_2)&(1<<ICHIPRDY))
		#endif
		{
			ItStatus1 = spi_read(INTERRUPT_STATUS_1);
			ItStatus2 = spi_read(INTERRUPT_STATUS_2);
			return;
		}
		
	} while (czasomierz<=(temp+6));
	#else
	unsigned char j;
	for(j=0;j<250;j++)					//200us~600us
		#ifdef _RFM_NIRQ
		if(!_RFM_NIRQ)
		#else
		if(spi_read(INTERRUPT_STATUS_2)&(1<<ICHIPRDY))
		#endif
		{
			ItStatus1 = spi_read(0x03);		
			ItStatus2 = spi_read(0x04);
			return;
		}
	#endif
		//hardware_reset();			//hardware reset
		*/	
	to_ready_mode();
	while (!((1<<ICHIPRDY)&spi_read(INTERRUPT_STATUS_2)));
}