spi write documentation is avalable how about spi read?
this is where I only seen a read...
http://leaflabs.com/docs/maple/spi/
spi write documentation is avalable how about spi read?
this is where I only seen a read...
http://leaflabs.com/docs/maple/spi/
Hey josheeg!
Glancing at the source code (http://github.com/leaflabs/libmaple/blob/master/wirish/comm/HardwareSPI.cpp) it looks like it's Spi.recv()
, though it seems like a Spi.available()
or similar would be required. I'll poke at this more this weekend and update the docs.
Hi josheeg,
The spi interface currently only supports SPI in the master configuration. In the SPI protocol, data is clocked out of the master at every clock edge, and clocked out of one or more slaves back to the master. So in order to read data, you need to send data. Spi.send() will return you the last byte read in from the slave.
Many common devices will typically have a multi-byte long command sequence clocked out by the master, and then a final dummy byte sent, which allows the master to read back a response from the slaves.
Other devices will often have a command byte protocol that includes a READ or a WRITE opcode, again, allowing you to read data back by sending data.
If you have a specific device that you're trying to get working, point it out and I'll take a look.
Hi,
I am trying to get HMC5843 work.
Arduino implementation here:
http://eclecti.cc/tag/hmc5843
and the library files here:
http://eclecti.cc/files/2009/11/HMC.zip
/* PUBLIC METHODS */
HMC5843::HMC5843()
{
}
// note that you need to wait at least 5ms after power on to initialize
void HMC5843::init()
{
PORTC = 0b00110000; // Use the internal pull up resistors
// Choose 100KHz for the bus. Formula from 21.5.2 in ATmega168 datasheet.
TWSR &= ~((1<<TWPS1)&(1<<TWPS0));
TWBR = (unsigned char)(F_CPU/200000l-8);
// Put the HMC5843 into continuous mode
sendStart();
sendByte(0x3C);
sendByte(0x02);
sendByte(0x00);
sendStop();
// note that you need to wait 100ms after this before first calling recieve
}
// This can be called at 100ms intervals to get new data
void HMC5843::getValues(int *x, int *y, int *z)
{
unsigned char xin, yin, zin;
// start the reading
sendStart();
sendByte(0x3D);
// read out the 3 values, 2 bytes each. lsb first, then msb.
xin = receiveByte();
*x = (xin<<8)|receiveByte();
yin = receiveByte();
*y = (yin<<8)|receiveByte();
zin = receiveByte();
*z = (zin<<8)|receiveByte();
// wrap back around for the next set of reads and close
sendByte(0x3D);
sendStop();
}
/* PRIVATE METHODS */
// start i2c as the master
void HMC5843::sendStart(void)
{
TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN);
}
// close i2c
void HMC5843::sendStop(void)
{
waitForReady();
TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWSTO);
}
// send a byte when the channel is ready
void HMC5843::sendByte(unsigned char data)
{
waitForReady();
TWDR = data;
TWCR = (1<<TWINT)|(1<<TWEN);
}
// ask for a byte, wait for it arrive, and return it
unsigned char HMC5843::receiveByte(void)
{
waitForReady();
TWCR = ((TWCR&0x0F)|(1<<TWINT)|(1<<TWEA));
waitForReady();
return(TWDR);
}
// get status register. the bits 0 and 1 are zeroed in init. see datasheet
unsigned char HMC5843::readStatus()
{
waitForReady();
return(TWSR);
}
// wait for TWINT to be set before touching the other registers.
void HMC5843::waitForReady(void)
{
// timeout after some time to avoid locking up if something goes wrong
int timeout = 200;
while ((!(TWCR & (1<<TWINT))) && (timeout--));
}
// Set the default object
HMC5843 HMC = HMC5843();
The Spi.send and Spi.recv may work but how about the other functions Spi.wait for ready start() etc..What should be the corresponding registers to deal with or is there a simple way to code it..
This following is my sample code..(doesn't work right now though).
//HMC5843 maple SPI
HardwareSPI Spi(1);
void setup() {
Spi.begin(SPI_140_625KHZ , MSBFIRST, 0);
Spi.send(0x3C);
Spi.send(0x02);
Spi.send(0x00);
;
}
void loop()
{
unsigned char xin, yin, zin;
while(1)
{
SerialUSB.println("Hello World!");
//sendStart();
Spi.send(0x3D);
// read out the 3 values, 2 bytes each. lsb first, then msb.
xin = Spi.recv();
SerialUSB.println(xin);
//x = (xin<<8)|Spi.recv();
//yin = receiveByte();
//y = (yin<<8)|receiveByte();
//zin = receiveByte();
//z = (zin<<8)|receiveByte();
//Spi.send(0x3D);
}
// sendStop();
}
Does it work?
It won't work since he's using the slowest 'speed' on Spi port 1 which doesn't support it. Also it appears, that the device is I2C, not SPI, but I'm only reading the code provided above, so I'm not sure.
You must log in to post.