Beispiel #1
0
int cmd_spi_rx(spi_t *spi,char *data, size_t max_len, int timeout_ms)
{
    int i;

    /* wait for ready from client */
    int size;
    int result = wait_for_ready(spi, timeout_ms, &size);
    if(result < 0) {
        return result;
    }

    /* nothing to read */
    if(size == 0) {
        return 0;
    }

    /* send RX data command */
    buffer[0] = CMD_RX;
    buffer[1] = (char)(size);
    result = spi_transmit(spi,NULL,buffer,2);
    if(result < 0) {
        return result;
    }

    /* receive data */
    result = spi_transmit(spi,data,NULL,size);
    if(result < 0) {
        return result;
    }

    return size;
}
Beispiel #2
0
void mfrc522_write(uint8_t reg, uint8_t data)
{
	rc522_select();
	spi_transmit((reg << 1) & 0x7E, SKIP_RECEIVE, RC522_SPI_CH);
	spi_transmit(data, SKIP_RECEIVE, RC522_SPI_CH);
	rc522_release();
}
void lcd_data(int *spih, uint16_t data) {
	uint8_t buff[4] = { 0,0,0,0};
	uint8_t r;
	
	#ifdef _DEBUG_
		printf("LCD_DATA(%04X)\n", data);
	#endif
	
	buff[1] =  data>>8;
	buff[2] = data&0x00ff;
	buff[3] = 0x15; // 0x15 - DATA_BE const from ili9341.c (BE is short form "before")
	r = spi_transmit(spih, &buff[0], 4, LCD_SPI_SPEED, LCD_SPI_BITS_PER_WORD);
	
	if (r < 0) {		
		fprintf(stderr, "SPI.LCD_DATA_1(0x%4X) error (%d,%d) : %s", data, r, errno, strerror(errno));
		return;
	}
	
	buff[1] =  data>>8;
	buff[2] = data&0x00ff;
	buff[3] = 0x1F; // 0x1F - DATA_AF const from ili9341.c (AF is short form "after")
	r = spi_transmit(spih, &buff[0], 4, LCD_SPI_SPEED, LCD_SPI_BITS_PER_WORD);
	if (r < 0) {		
		fprintf(stderr, "SPI.LCD_DATA_2(0x%4X) error (%d,%d) : %s", data, r, errno, strerror(errno));
	}

}
void lcd_cmd(int *spih, uint16_t cmd) {
	uint8_t buff[4] = { 0,0,0,0};
	uint8_t r;
	
	#ifdef _DEBUG_
		printf("LCD_CMD(%04X)\n", cmd);
	#endif
	
	
	buff[1] =  cmd>>8;
	buff[2] = cmd&0x00ff;
	buff[3] = 0x11; // 0x15 - DATA_BE const from ili9341.c (BE is short form "before")
	r = spi_transmit(spih, &buff[0], 4, LCD_SPI_SPEED, LCD_SPI_BITS_PER_WORD);
	if (r < 0) {		
		fprintf(stderr, "SPI.LCD_CMD_1(%4X) error (%d,%d) : %s", cmd, r, errno, strerror(errno));
	}
	
	buff[1] =  cmd>>8;
	buff[2] = cmd&0x00ff;
	buff[3] = 0x1B; // 0x1F - DATA_AF const from ili9341.c (AF is short form "after")
	r = spi_transmit(spih, &buff[0], 4, LCD_SPI_SPEED, LCD_SPI_BITS_PER_WORD);
	if (r < 0) {		
		fprintf(stderr, "SPI.LCD_CMD_2(%4X) error (%d,%d) : %s", cmd, r, errno, strerror(errno));
	}

}
void lcd_reset(int *spih) {
	uint8_t buff[4] = { 0,0,0,0 };
	uint8_t r;
	
	#ifdef _DEBUG_
		printf("LCD_RESET\n");
	#endif	
	
	// set Reset LOW
	r = spi_transmit(spih, &buff[0], 4, LCD_SPI_SPEED, LCD_SPI_BITS_PER_WORD);
	if (r < 0) {		
		fprintf(stderr, "SPI.LCD_RESET_1 error (%d) : %s", errno, strerror(errno));
	}
	
	delayms(50);
	
	// set Reset High
	buff[0]=buff[1]=buff[2] = 0;
	buff[3]=0x01;
	r = spi_transmit(spih, &buff[0], 4, LCD_SPI_SPEED, LCD_SPI_BITS_PER_WORD);
	if (r < 0) {		
		fprintf(stderr, "SPI.LCD_RESET_2 error (%d) : %s", errno, strerror(errno));
	}
	
	#ifdef _DEBUG_
		printf("LCD_RESET end.\n");
	#endif	
	
	
	delayms(200);
}	
void lcd_reset(void) {
	uint8_t buff[4] = { 0,0,0,0 };
	
	#ifdef _DEBUG_
		printf("LCD_RESET\n");
	#endif	
	
	// Select LCD
	//bcm2835_spi_chipSelect(BCM2835_SPI_CS1);
	
	// set Reset LOW
	spi_transmit(LCD_CS, &buff[0], 4);
	
	delayms(50);
	
	// set Reset High
	buff[0]=buff[1]=buff[2] = 0;
	buff[3]=0x01;
	spi_transmit(LCD_CS, &buff[0], 4);
	
	#ifdef _DEBUG_
		printf("LCD_RESET end.\n");
	#endif	
	
	delayms(200);
}	
void mfrc522_write(uint8_t reg, uint8_t data)
{
	ENABLE_CHIP();
	spi_transmit((reg<<1)&0x7E);
	spi_transmit(data);
	DISABLE_CHIP();
}
Beispiel #8
0
 void send_spi_position(uint8_t axis_idx) {
   JOG_SPI_PORT &= ~(1<<JOG_SPI_SS); // Slave select active low
   spi_transmit(axis_idx); // TEST DATA
   spi_transmit(255);
   spi_transmit(85);
   spi_transmit(0);
   JOG_SPI_PORT |= (1<<JOG_SPI_SS);
 }
Beispiel #9
0
uint8_t mfrc522_read(uint8_t reg)
{
	uint8_t data;
	rc522_select();
	spi_transmit(((reg << 1) & 0x7E) | 0x80, SKIP_RECEIVE, RC522_SPI_CH);
	data = spi_transmit(0x00, RECEIVE_BYTE, RC522_SPI_CH);
	rc522_release();
	return data;
}
uint8_t mfrc522_read(uint8_t reg)
{
	uint8_t data;	
	ENABLE_CHIP();
	spi_transmit(((reg<<1)&0x7E)|0x80);
	data = spi_transmit(0x00);
	DISABLE_CHIP();
	return data;
}
Beispiel #11
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 #12
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 #13
0
void mcp2515_bit_modify(char addr, char mask_byte, char data)
{
	spi_select();
	
	spi_transmit(MCP_BITMOD);
	spi_transmit(addr);
	spi_transmit(mask_byte);
	spi_transmit(data);
	
	spi_deselect();
}
Beispiel #14
0
void mcp2515_write(char addr, char data)
{
	spi_select();
	
	spi_transmit(MCP_WRITE);
	spi_transmit(addr);
	spi_transmit(data);
	
	spi_deselect();
	
}
Beispiel #15
0
char mcp2515_read (char addr){
	char result;
	
	spi_select();
	
	spi_transmit(MCP_READ);
	spi_transmit(addr);
	result = spi_receive();
	
	spi_deselect();
	
	return result;
}
void ST7540_setup(){
    reg_acc();
    tx_mode();
    
    while ((PIN_SPI  &   (1<<SCK))!=0);
    
    ss_clear();
    //Send configuration bytes to ST7540 control register
    spi_transmit(0x13);
    spi_transmit(0xB2);
    spi_transmit(0x32);
    ss_set();
}
Beispiel #17
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 #18
0
static int wait_for_ready(spi_t *spi,int timeout_ms,int *result_size)
{
    int i;

    /* wait for READY status with timeout */
    for(i=0; i<timeout_ms; i++) {
        int result = spi_transmit(spi,buffer,NULL,2);
        if(result < 0) {
            return result;
        }

        //printf("%02x %02x\n", (int)buffer[0], (int)buffer[1]);

        // found READY ?
        if((buffer[1] & READY_MASK) == READY_FLAG) {
            if(result_size != NULL) {
                *result_size = (int)(buffer[1] & SIZE_MASK);
            }
            return 0;
        }

        usleep(1000);
    }
    return -CMD_ERR_NOT_READY;
}
Beispiel #19
0
void max1168_read_channel(int channel) {
	max1168_current_channel = channel;
	max1168_read_channel_package.data[0] = (channel << 5) << 8;
	switch (channel){
	case 0:
		max1168_read_channel_package.spi_interrupt_handler = &max1168_on_spi_int_channel0;
		break;
	case 1:
		max1168_read_channel_package.spi_interrupt_handler = &max1168_on_spi_int_channel1;
		break;
	case 2:
		max1168_read_channel_package.spi_interrupt_handler = &max1168_on_spi_int_channel2;
		break;
	case 3:
		max1168_read_channel_package.spi_interrupt_handler = &max1168_on_spi_int_channel3;
		break;
	case 4:
		max1168_read_channel_package.spi_interrupt_handler = &max1168_on_spi_int_channel4;
		break;
	case 5:
		max1168_read_channel_package.spi_interrupt_handler = &max1168_on_spi_int_channel5;
		break;
	case 6:
		max1168_read_channel_package.spi_interrupt_handler = &max1168_on_spi_int_channel6;
		break;
	case 7:
		max1168_read_channel_package.spi_interrupt_handler = &max1168_on_spi_int_channel7;
		break;
	}
	spi_transmit(&max1168_read_channel_package);
	return;
}
Beispiel #20
0
void eeprom_93c06_write_short(unsigned char addr, unsigned short data)
{
	unsigned char trash;
	spi_select();

	eeprom_93c06_start_bit();

	spi_transmit(SPI_OP_93C06_WRITE_MASK | SPI_93C06_ADDR_MASK(addr), &trash);
	spi_transmit(SPI_93C06_WRITE_DATA_BYTE1(data), &trash);
	spi_transmit(SPI_93C06_WRITE_DATA_BYTE2(data), &trash);

	spi_deselect();

	/* Operation takes 2ms to be guaranteed. */
	msleep(2);

}
Beispiel #21
0
void mcp2515_request_to_send(int reg)
{
	spi_select();
	
	spi_transmit(reg);
	
	spi_deselect();
}
Beispiel #22
0
void mcp2515_reset()
{
	spi_select();
	
	spi_transmit(MCP_RESET);
	
	spi_deselect();
}
void ST7540_transmit(uint8_t temp){
    //Set the appropriate flags and transmit
    tx_mode();
    ss_clear();
    spi_transmit(temp);
    ss_set();
    rx_mode();
}
void lcd_cmd(uint16_t cmd) {
	uint8_t b1[4], b2[4];
	
	memset(&b1,0,sizeof(b1));
	memset(&b2,0,sizeof(b2));
	
	// setup buffers
	b2[1] = b1[1] =  cmd>>8;
	b2[2] = b1[2] = cmd&0x00ff;
	b1[3] = 0x11; // 0x11 - CMD_BE const from ili9341.c (BE is short form "before")
	b2[3] = 0x1B; // 0x1B - CMD_AF const from ili9341.c (AF is short form "after")
	
	// Select LCD
	//bcm2835_spi_chipSelect(BCM2835_SPI_CS1);
	// send it - prepare
	spi_transmit(LCD_CS, &b1[0], 4);//bcm2835_spi_transfern(&b1, 4);
	// send it - store in LCD
	spi_transmit(LCD_CS, &b2[0], 4); //bcm2835_spi_transfern(&b2, 4);
}
Beispiel #25
0
void eeprom_93c06_read_short(unsigned char addr, unsigned short *data)
{
	unsigned char r1, r2, r3;
	unsigned char trash;

	spi_select();

	eeprom_93c06_start_bit();

	spi_transmit(SPI_OP_93C06_READ_MASK | SPI_93C06_ADDR_MASK(addr), &trash);
	spi_transmit(SPI_DONT_CARE, &r1);
	spi_transmit(SPI_DONT_CARE, &r2);
	spi_transmit(SPI_DONT_CARE, &r3);

	spi_deselect();

	*data = SPI_93C06_READ_DATA_ASSEMBLE(r1, r2, r3);

}
Beispiel #26
0
void eeprom_93c06_enable_write(int en)
{
	unsigned char trash;
	unsigned char op = (en ? SPI_OP_93C06_EWEN_MASK : SPI_OP_93C06_EWDS_MASK);
	spi_select();

	eeprom_93c06_start_bit();

	spi_transmit(op, &trash);

	spi_deselect();
}
Beispiel #27
0
// Set each LEDs on or off state from a 16-bit value.
//
//    LED d1 = bit 1
//    LED d2 = bit 2
//
// etc, in the pattern:
//
//    1  2  3  4
//    5  6  7  8
//    9 10 11 12
//   13 14 15 16
//
void led_set_state(uint16_t new_state)
{
    // If no lights have changed, transmit nothing. This saves bandwidth on
    // the SPI bus for more important things.
    if (g_led_state == new_state) return;
    // Transmit Most Significant Byte first.
    spi_transmit(new_state >> 8);
    spi_transmit(new_state & 0xff);
    // Latch the result to the LEDs by pulsing high.
    PORTB |= LED_LATCH;
    PORTB &= ~LED_LATCH;
    // record the state.
    g_led_state = new_state;
}
Beispiel #28
0
int main()
{
	uint8_t data=0;
	_delay_ms(50);
	
	spi_init();
	
	while(1)
	{
		ENABLE_CHIP();		
		spi_transmit(data++);
		DISABLE_CHIP();
		_delay_ms(1000);
	}
}
Beispiel #29
0
void sca3000_read_res(void) {
    spi_package package;
	package.bit_mode = SPI_8_BIT_MODE;

	/* trigger 2 bytes read */
    unsigned char cmd=0x05<<2;
	package.data[0] = cmd;
	package.data[1] = 0;
	package.data[2] = 0;
	package.data[3] = 0;
	package.data[4] = 0;
	package.data[5] = 0;
	package.data[6] = 0;
	package.length = 7;
	package.slave_select = &sca3000_select;
	package.slave_unselect = &sca3000_unselect;
	package.spi_interrupt_handler = &sca3000_on_spi_int;
	spi_transmit(&package);
}
Beispiel #30
0
int cmd_spi_tx(spi_t *spi,const char *data, size_t len, int timeout_ms)
{
    if(len > CMD_MAX_SIZE)
        return -CMD_ERR_TOO_LONG;

    /* wait for ready from slave */
    int result = wait_for_ready(spi, timeout_ms, NULL);
    if(result < 0) {
        return result;
    }

    /* setup TX command header with data */
    buffer[0] = CMD_TX;
    buffer[1] = (char)(len & 0xff);
    memcpy(buffer+2,data,len);

    /* send to slave */
    return spi_transmit(spi,NULL,buffer,len+2);
}