Skip to content

Commit dc2ad67

Browse files
committed
initial api design
1 parent 73cfc7b commit dc2ad67

File tree

5 files changed

+446
-0
lines changed

5 files changed

+446
-0
lines changed

drivers/SAI.cpp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2006-2013 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
#include "drivers/SAI.h"
17+
#include "platform/mbed_critical.h"
18+
19+
#if DEVICE_SAI
20+
21+
namespace mbed {
22+
23+
SAI::SAI(PinName mclk, PinName bclk, PinName wclk, PinName sd,
24+
const sai_format_t *fmt, bool is_input, uint32_t master_clock, bool internal_mclk
25+
) : _sai(), _mutex(), _is_input(is_input) {
26+
// No lock needed in the constructor
27+
sai_init_t init = {};
28+
29+
init.mclk = mclk;
30+
init.bclk = bclk;
31+
init.wclk = wclk;
32+
init.sd = sd;
33+
init.is_receiver = is_input;
34+
init.sample_rate = SAI_DEFAULT_SAMPLE_RATE;
35+
init.mclk_source = SAI_CLOCK_SOURCE_Internal;
36+
init.input_mclk_frequency = 0; // 0 means find it by yourself.
37+
init.output_mclk_frequency = 256 * init.sample_rate;
38+
39+
init.format = *fmt;
40+
41+
if (sai_init(&_sai, &init) != SAI_RESULT_OK) {
42+
// it failed :o
43+
}
44+
}
45+
46+
bool SAI::xfer(uint32_t *value) {
47+
lock();
48+
bool ret = sai_xfer(&_sai, value);
49+
unlock();
50+
return ret;
51+
}
52+
53+
void SAI::lock() {
54+
_mutex.lock();
55+
}
56+
57+
void SAI::unlock() {
58+
_mutex.unlock();
59+
}
60+
61+
} // namespace mbed
62+
63+
#endif

drivers/SAI.h

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2006-2015 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
#ifndef MBED_SAI_H
17+
#define MBED_SAI_H
18+
19+
#include "platform/platform.h"
20+
21+
#if defined (DEVICE_SAI) || defined(DOXYGEN_ONLY)
22+
23+
#include "hal/sai_api.h"
24+
#include "platform/NonCopyable.h"
25+
#include "platform/PlatformMutex.h"
26+
27+
namespace mbed {
28+
/** \addtogroup drivers */
29+
30+
/** A SAI Master, used for communicating with SAI slave devices
31+
*
32+
* @ingroup drivers
33+
*/
34+
class SAI : private NonCopyable<SAI> {
35+
36+
public:
37+
38+
/** Create a SAI
39+
*/
40+
SAI(PinName mclk, PinName bclk, PinName wclk, PinName sd,
41+
const sai_format_t *fmt = &sai_mode_i2s32, bool is_input = false,
42+
uint32_t master_clock = 0, bool internal_mclk = false);
43+
44+
/** Push a sample to the Fifo & try to read a new sample.
45+
* it may return 0 if no sample was available.
46+
*/
47+
virtual bool xfer(uint32_t *value);
48+
49+
/** Acquire exclusive access to this SAI bus
50+
*/
51+
virtual void lock(void);
52+
53+
/** Release exclusive access to this SAI bus
54+
*/
55+
virtual void unlock(void);
56+
57+
58+
public:
59+
virtual ~SAI() {}
60+
61+
protected:
62+
sai_t _sai;
63+
PlatformMutex _mutex;
64+
bool _is_input;
65+
};
66+
67+
class SAITransmitter : private SAI {
68+
public:
69+
SAITransmitter(PinName mclk, PinName bclk, PinName wclk, PinName sd,
70+
const sai_format_t *fmt = &sai_mode_i2s32)
71+
: SAI(mclk, bclk, wclk, sd, fmt, false) { }
72+
73+
bool send(uint32_t sample) {
74+
return this->xfer(&sample);
75+
}
76+
};
77+
78+
class SAIReceiver : private SAI {
79+
public:
80+
SAIReceiver(PinName mclk, PinName bclk, PinName wclk, PinName sd,
81+
const sai_format_t *fmt = &sai_mode_i2s32)
82+
: SAI(mclk, bclk, wclk, sd, fmt, true) { }
83+
84+
bool receive(uint32_t *sample) {
85+
return this->xfer(sample);
86+
}
87+
};
88+
89+
} // namespace mbed
90+
91+
#endif
92+
93+
#endif

hal/mbed_sai_api.c

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2018 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "hal/sai_api.h"
18+
19+
#if DEVICE_SAI
20+
21+
#include "platform/mbed_toolchain.h"
22+
#include <string.h>
23+
24+
const sai_format_t sai_mode_i2s16 = {
25+
.bclk_polarity = true,
26+
.wclk_polarity = true,
27+
.ws_delay = true,
28+
.ws_length = 16,
29+
.frame_length = 1,
30+
.word_mask = 0,
31+
.word_length = 16,
32+
.data_length = 16,
33+
.lsb_first = false,
34+
.aligned_left = true,
35+
.bit_shift = 0
36+
};
37+
38+
const sai_format_t sai_mode_i2s16w32 = {
39+
.bclk_polarity = true,
40+
.wclk_polarity = true,
41+
.ws_delay = true,
42+
.ws_length = 32,
43+
.frame_length = 1,
44+
.word_mask = 0,
45+
.word_length = 32,
46+
.data_length = 16,
47+
.lsb_first = false,
48+
.aligned_left = true,
49+
.bit_shift = 0
50+
};
51+
52+
const sai_format_t sai_mode_i2s32 = {
53+
.bclk_polarity = true,
54+
.wclk_polarity = true,
55+
.ws_delay = true,
56+
.ws_length = 32,
57+
.frame_length = 1,
58+
.word_mask = 0,
59+
.word_length = 32,
60+
.data_length = 32,
61+
.lsb_first = false,
62+
.aligned_left = true,
63+
.bit_shift = 0
64+
};
65+
66+
const sai_format_t sai_mode_pcm16l = {
67+
.bclk_polarity = true,
68+
.wclk_polarity = true,
69+
.ws_delay = true,
70+
.ws_length = 13,
71+
.frame_length = 1,
72+
.word_mask = 0,
73+
.word_length = 16,
74+
.data_length = 16,
75+
.lsb_first = false,
76+
.aligned_left = true,
77+
.bit_shift = 0
78+
};
79+
80+
const sai_format_t sai_mode_pcm16s = {
81+
.bclk_polarity = true,
82+
.wclk_polarity = true,
83+
.ws_delay = true,
84+
.ws_length = 1,
85+
.frame_length = 1,
86+
.word_mask = 0,
87+
.word_length = 16,
88+
.data_length = 16,
89+
.lsb_first = false,
90+
.aligned_left = true,
91+
.bit_shift = 0
92+
};
93+
94+
#endif // DEVICE_SAI

0 commit comments

Comments
 (0)