Hi again,
I've now untangled the code in the original post (I used the "Insert Code" toolbar option, and then just re-organised it a little so it is sensible to read), and can see the answers to a few of your questions. It is mainly some simple changes.
A) Setting up SPI at the beginning of each interruption is weird, but needed. This will increase the interrupt latency. How to setup SPI once for all just after reset, without mbed compiler reporting a "device unknown" error ?
In your code, you currently create the SPI device within a function:
void LRCK_in() { // interrupt service routine starts here
SPI device(p5, p6, p7); //mosi, miso, clk
//mbed needs configuring SPI here otherwise "device unknown" error
:
:
What this means is that the SPI device will only exist whilst the function does; it is created when the function is called, and destroyed when the function returns. This will happen every time the function is called, which is not what you are after in this case.
What you really want to do is just create the SPI device as part of startup, by making it just a global object:
SPI device(p5, p6, p7); //mosi, miso, clk
void LRCK_in() { // interrupt service routine starts here
:
:
Here you can see the SPI device sits on its own. Before main is called, it will get created.
Also in the interrupt you are trying to setup the SPI device:
void LRCK_in() { // interrupt service routine starts here
void format (int bits = 8, int mode = 0);
void frequency (int hz = 2116800); //44.1 KHz * 48 bits
:
Here, the syntax is slightly wrong in a few places. I can see how you got to it from the API documentation so we might be able to improve that. The API documentation provides the standard way to specify methods and functions, but the way you actually call them must interpret that.
For example, format and frequency are "methods" (functions) of the SPI object, so you need to call it on a specific SPI object using the object-dot-method notation. And the function arguments (the numbers you pass) just require the value.
Also, you again probably only want to do this once, so may be best to do this setup in main. For example:
int main() {
device.format(8, 0); // bits, mode
device.frequency(2116800); //44.1 KHz * 48 bits
:
Hope this makes some of the programming syntax more clear.
Simon
hi,
any idea how to use the SPI at 2,116,800 bit per second sending 6 consecutive bytes representing left 24 bit + right 24 bit Digital Audio to go into CS4331 Audio DAC ? I'm trying getting SPI emulating I2S. Please note this thread is not related to I2C. I2S is interfacing audio codecs like CS4331 and CS5331. LRCK is the frame sync signal, at 44.1 Khz.
As you can see, I'm trying to send a fixed pattern (55-01-02-04-08-55 hex). The above routine to be called upon receiving the LRCK interrupt at 44.1 KHz on pin16.
A) Setting up SPI at the beginning of each interruption is weird, but needed. This will increase the interrupt latency. How to setup SPI once for all just after reset, without mbed compiler reporting a "device unknown" error ?
B) The first byte may be sent, but I doubt the next 5 bytes are going to be sent, unless mbed is using an undocumented buffering scheme. But wait a minute : anyway, upon execution, the succession of "device.write" calls is way too fast for getting any meaningful data into the receiving variables (rx_ch1_byte2 untill rx_ch2_byte3). It confirms this approach is completely wrong, isn't ?
C) One should send the first byte, still using the LRCK interrupt, but the five remaining bytes should be sent using a second interrupt source, generating an interrupt each time a byte just got sent by the SPI block. How to setup such a "SPI_byte_sent" interrupt ?
Steph