SPI library or driver?
amossam
Posts: 35
Heya!
Is there any SPI library, function or driver, but written in C?
I have written a small function that "should" be generic, but I'm not totally satisfied with it...
Here is a code
If I understand SPI protocol correctly, data is sent on rising edge and read on falling edge, and with following call:
And I need simple SPI read/write functions, because I ordered few wireless board on 433MHz based on CC1101 chip which I plan to use with propeller as master node and few attiny's as clients...
thx!
P.S. Would i gain anything if I put writeReadSPI into .cogc file and use mailbox for communication with SPI devices? (and I'm not sure yet how to do that, but if needed I'll figure it out! )
Is there any SPI library, function or driver, but written in C?
I have written a small function that "should" be generic, but I'm not totally satisfied with it...
Here is a code
int writeReadSPI(int SPI_cs, int SPI_clk, int SPI_do,int SPI_di, int data, int size) { pinLow(SPI_clk); pinHigh(SPI_cs); pinOutput(SPI_do); pinLow(SPI_do); pinInput(SPI_di); int mask = 0b1 << 30; int reg = (0b0100 << 26) + SPI_clk; int result = 0; CTRA = reg; FRQA = 4194304; //2^22 waitpne((1 << SPI_clk), (1 << SPI_clk)); //low waitpeq((1 << SPI_clk), (1 << SPI_clk)); //high pinLow(SPI_cs); int i; for (i = 0; i < 32; i++) { waitpne((1 << SPI_clk), (1 << SPI_clk)); //low pinWrite(SPI_do, ((data & mask) == mask)); waitpeq((1 << SPI_clk), (1 << SPI_clk)); //high result = (result << 1) | pinRead(SPI_di); data <<= 1; } pinHigh(SPI_cs); CTRA = 0; //disable counter result >>= size; return result; }
If I understand SPI protocol correctly, data is sent on rising edge and read on falling edge, and with following call:
writeReadSPI(CS, CLK, DO, DI, (0b011000 << 26), 13);I get data from MCP3208 with this function, but I can't test it anything else currently...
And I need simple SPI read/write functions, because I ordered few wireless board on 433MHz based on CC1101 chip which I plan to use with propeller as master node and few attiny's as clients...
thx!
P.S. Would i gain anything if I put writeReadSPI into .cogc file and use mailbox for communication with SPI devices? (and I'm not sure yet how to do that, but if needed I'll figure it out! )
Comments
Some library functions are a work in progress. We have a list, but it is slow to be provided.
This is a good question. With propeller-gcc supporting CMM, a cogstart thread driver might be smaller and just as effective (though slower) than a driver written for COGC.
Here is a simple example of how to use a cogstart thread. https://sites.google.com/site/propellergcc/documentation/libraries/propeller-h-library/#TOC-cogstart
That I think is wrong.
I wrote something together quickly:
I would try it first without the timer. This device seems to be quite complicated.
well, with your suggestion i got data back (after few small adaptations), but as with my function from first post I would occasionally got stray data every 5000-6000 samples. for ex. result sould be 4086, and I would get 4465 back! and for using your function in other situations I would need to change #define's....
But your code gave me few ideas, and now I can get around 10k samples per second!
Also, it seems (at least when this adc is asked) that data _is_ sent to adc on rising edge of clk and read from adc on falling edge...
here is current version of function, which now doesn't use any other functions, it's completely self-contained!
currently, function is defined to use 32 bit packet, but that can be easily changed (that's next TODO!)...
what I would like is to someone tries this function on something other than MCP320x, and reports how it's working...
thx!
But is it right to clock 32 bits at once?
Maybe I do not understand your code completly.
Well, since "data" parameter of function is int, I automatically put in loop 32, but now that you mentioned it, I see that i'm loosing 15 (or 14?) cycles for nothing! (5 for setup, 12 for reading data in this example!) hmm, will try later to use "size" parameter for packet (send + receive data) size, not for result size! so, you could pass (byte << 24) (shifting left because it's sending MSB first) as "data" and size of 8, and i would sent and receive packet of size byte (8 bits)!
thx!
edit: said and done! when I set function (code below) for 18 bit packet (this size is explained in code), I can read 10k samples in 0.488364 sec!! (if used function for timing is correct! I took it from post improving CMM performance )
here is complete code, with timing:
results in: