Esempio n. 1
0
int flash_vs_erase(loff_t offs, uint32_t len)
{
	uint8_t tx[8] = {
		FLASH_CMD_WESR, FLASH_CMD_WRSR,
		0, FLASH_CMD_ERAS, FLASH_CMD_WREN,
		0, 0, 0};
	struct spi_transfer td[] = {
		{NULL, &tx[1], 2 },
		{NULL, &tx[3], 1 },
		{NULL, &tx[0], 1 },
	};

	/* clear sr */
//	printf("clear sr\n");
	flash_wren();
	spi_exchange(&td[0], 1);
	flash_poll(FLASH_MOD_POLLREDY);

	/* clear sr: alt */
//	printf("clear sr: alt\n");
	spi_exchange(&td[2], 1);
	spi_exchange(&td[0], 1);
	flash_poll(FLASH_MOD_POLLREDY);

	/* erase */
	flash_wren();
//	printf("erase\n");
	spi_exchange(&td[1], 1);
	flash_poll(FLASH_MOD_POLLREDY);
	return 0;
}
Esempio n. 2
0
int flash_vs_read(uint8_t *buf, loff_t offs, int len, int extra)
{
	uint8_t tx[8] = {0, 0, 0, FLASH_CMD_FARD };
	int remain = len, *d = (int *)&tx[4];
	struct spi_transfer td[] = {
		{NULL, &tx[3], 5 },
		{buf,  NULL, 0 }
	}, *p;

//	printf("spi: r (b=%p, o=0x%llx, l=0x%x)\n", buf, offs, len);

	for(p = &td[1]; remain;) {

		*d = offs;
		*d = __swab32(*d);
		*d >>= 8;

		p->len = (remain < FLASH_READ_BLOCK)?
			remain: FLASH_READ_BLOCK;

		spi_exchange(td, ARRAY_SIZE(td));
		remain -= p->len;
		offs   += p->len;
		p->in  += p->len;
	}

	return len;
}
Esempio n. 3
0
int flash_vs_write(uint8_t *buf, loff_t offs, int len, int extra)
{
	uint8_t tx[8] = { FLASH_CMD_WREN, 0, 0, FLASH_CMD_WRIT };
	int remain = len, *d = (int *)&tx[4];
	struct spi_transfer td[] = {
		{NULL, &tx[4], 4 },
		{NULL, buf,    0 },
	}, *p;
	
//	printf("spi: w (b=%p, o=0x%llx, l=0x%x)\n", buf, offs, len);

	for(p = &td[1]; remain;) {
		*d = offs;
		*d = __swab32(*d);
		*d &= ~0xff;
		*d |= FLASH_CMD_WRIT;
		p->len = (remain < spibytes) ? remain: spibytes;

		flash_wren();
		spi_exchange(td, ARRAY_SIZE(td));
		flash_poll(FLASH_MOD_POLLREDY);

		remain -= p->len;
		offs   += p->len;
		p->out += p->len;
	}

	return len;
}
Esempio n. 4
0
////////////////////////////////////////////////////////////
// Write command to the MCP2515
////////////////////////////////////////////////////////////
void can_write_cmd(char cmd){

	spi_init_buffer();
	spi_load_byte(cmd);
	spi_exchange();
	spi_wait_for_completion();

}
Esempio n. 5
0
void flash_wren(void) {
	uint8_t tx[8] = { FLASH_CMD_WREN,
		FLASH_CMD_WRDI };
	struct spi_transfer td[] = { {NULL, &tx[0], 1 },
		{NULL, &tx[1], 1} };
//	spi_exchange(&td[1], 1);
	spi_exchange(&td[0], 1);
	flash_poll(FLASH_MOD_POLLWREN);
}
Esempio n. 6
0
void drv8301_write_reg(int reg, int data) {
	uint16_t out = 0;
	out |= (reg & 0x0F) << 11;
	out |= data & 0x7FF;

	spi_begin();
	spi_exchange(out);
	spi_end();
}
Esempio n. 7
0
unsigned int drv8301_read_reg(int reg) {
	uint16_t out = 0;
	out |= (1 << 15);
	out |= (reg & 0x0F) << 11;
	out |= 0x807F;

	if (reg != 0) {
		spi_begin();
		spi_exchange(out);
		spi_end();
	}

	spi_begin();
	uint16_t res = spi_exchange(0xFFFF);
	spi_end();

	return res;
}
Esempio n. 8
0
////////////////////////////////////////////////////////////
// Write byte <data> to register with address <reg>
////////////////////////////////////////////////////////////
void can_write_reg(char address, char data){

	spi_init_buffer();
	spi_load_byte(c2515Write);
	spi_load_byte(address);
	spi_load_byte(data);
	spi_exchange();
	spi_wait_for_completion();

}
Esempio n. 9
0
////////////////////////////////////////////////////////////
// Software reset in case the reset pin is not available
////////////////////////////////////////////////////////////
void can_reset(){

	// Reset the SPI buffer
	spi_init_buffer();
	// Load and TX command
	spi_load_byte(c2515Reset);	
	spi_exchange();
	spi_wait_for_completion();
	return;
}
Esempio n. 10
0
void flash_poll(int mod) {
	uint8_t buf[8] = { FLASH_CMD_RDSR, 0 };
	struct spi_transfer td[] = { {NULL, &buf[0], 1},
		{&buf[1], NULL, 1} };

	do { spi_exchange(td, ARRAY_SIZE(td));
//		printf("stat: 0x%02x, 0x%02x, 0x%02x\n", buf[0], buf[1], buf[2]);
       	} while
	  ((mod)? !(buf[1] & (1 << 1)): !!(buf[1] & 1));
}
Esempio n. 11
0
////////////////////////////////////////////////////////////
// Write <data> in <address> using <mask>
////////////////////////////////////////////////////////////
void can_write_bits(char address, char data, char mask){

	spi_init_buffer();
	spi_load_byte(c2515BitMod);
	spi_load_byte(address);
	spi_load_byte(mask);
	spi_load_byte(data);
	spi_exchange();
	spi_wait_for_completion();

}
Esempio n. 12
0
void can_read_status(){
	// Reset the SPI buffer
	spi_init_buffer();
	// Load and TX command
	spi_load_byte(c2515Status);	
	spi_load_byte(0x00);
	spi_exchange();
	spi_wait_for_completion();
	
	serial_printf("Status = ");
	serial_print_hex(spi_get_byte(1));
	
	return;
}
Esempio n. 13
0
////////////////////////////////////////////////////////////
// Read the contents of the register at <address>
////////////////////////////////////////////////////////////
char can_read_reg(char address){

	spi_init_buffer();
	// Read command
	spi_load_byte(c2515Read);
	// Register address
	spi_load_byte(address);
	// Expect one byte answer
	spi_load_zeros(0x01);
	// Start the transfer 
	spi_exchange();
	spi_wait_for_completion();
	
	return spi_get_byte(2);
	
}
Esempio n. 14
0
////////////////////////////////////////////////////////////
// Read several bytes from the other device into the 
// data buffer. Used when you're talking through a 
// one-wire SPI interface (e.g. for nRF2401).
// ! The data output pin should be disabled when this 
// ! routine is called.
////////////////////////////////////////////////////////////
void spi_read(char nr_of_bytes){

	// Buffer init
	spi_init_buffer();
	// Set the number of bytes we want to read
	spi_load_zeros(nr_of_bytes);
	
	// Disable the output pin
	//spi_do_tris = 1;
	
	// Do the exchange
	spi_exchange();
	
	// Wait for completion
	spi_wait_for_completion();
	
	// Enable the output pin again
	//spi_do_tris = 0;
	
	return;
}
Esempio n. 15
0
static uint16_t spi_send(FAR struct spi_dev_s *dev, uint16_t wd)
{
  uint16_t buf = wd;
  spi_exchange(dev, &buf, &buf, 1);
  return buf;
}