summaryrefslogtreecommitdiff
path: root/bsp/bouffalo_lab/libraries/bl_mcu_sdk/drivers/lhal/include/bflb_i2s.h
blob: e006116d28c2a5d3afe2c0c6a83de73790e007bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#ifndef _BFLB_I2S_H
#define _BFLB_I2S_H

#include "bflb_core.h"

/** @addtogroup LHAL
  * @{
  */

/** @addtogroup I2S
  * @{
  */

/** @defgroup I2S_ROLE i2s role definition
  * @{
  */
#define I2S_ROLE_MASTER 0
#define I2S_ROLE_SLAVE  1
/**
  * @}
  */

/** @defgroup I2S_FORMAT_MODE i2s mode definition
  * @{
  */
#define I2S_MODE_LEFT_JUSTIFIED       0 /* left-justified or Phillips standard */
#define I2S_MODE_RIGHT_JUSTIFIED      1 /* right-justified */
#define I2S_MODE_DSP_SHORT_FRAME_SYNC 2 /* dsp modeA/B short frame sync */
#define I2S_MODE_DSP_LONG_FRAME_SYNC  3 /* dsp modeA/B long frame sync */
/**
  * @}
  */

/** @defgroup I2S_CHANNEL_MODE i2s mode definition
  * @{
  */
#define I2S_CHANNEL_MODE_NUM_1 0
#define I2S_CHANNEL_MODE_NUM_2 1
#define I2S_CHANNEL_MODE_NUM_3 2 /* only DSP mode, frame_size must equal data_size */
#define I2S_CHANNEL_MODE_NUM_4 3 /* only DSP mode, frame_size must equal data_size */
#define I2S_CHANNEL_MODE_NUM_6 4 /* only DSP mode, frame_size must equal data_size */
/**
  * @}
  */

/** @defgroup I2S_SLOT_WIDTH i2s slot width definition
  * @{
  */
#define I2S_SLOT_WIDTH_8  0
#define I2S_SLOT_WIDTH_16 1
#define I2S_SLOT_WIDTH_24 2
#define I2S_SLOT_WIDTH_32 3
/**
  * @}
  */

/** @defgroup I2S_INTSTS i2s interrupt status definition
  * @{
  */
#define I2S_INTSTS_TX_FIFO  (1 << 1)
#define I2S_INTSTS_RX_FIFO  (1 << 2)
#define I2S_INTSTS_FIFO_ERR (1 << 3)
/**
  * @}
  */

/** @defgroup I2S_CMD i2s feature control cmd definition
  * @{
  */
#define I2S_CMD_CLEAR_TX_FIFO    (0x01)
#define I2S_CMD_CLEAR_RX_FIFO    (0x02)
#define I2S_CMD_RX_DEGLITCH      (0x03)
#define I2S_CMD_DATA_ENABLE      (0x04)
#define I2S_CMD_CHANNEL_LR_MERGE (0x05)
#define I2S_CMD_CHANNEL_LR_EXCHG (0x06)
#define I2S_CMD_MUTE             (0x07)
#define I2S_CMD_BIT_REVERSE      (0x08)
/**
  * @}
  */

/** @defgroup I2S_CMD_DATA_ENABLE_TYPE i2s data enable type
  * @{
  */
#define I2S_CMD_DATA_ENABLE_TX (1 << 1)
#define I2S_CMD_DATA_ENABLE_RX (1 << 2)

/**
  * @}
  */

/**
 * @brief I2S configuration structure
 *
 * @param bclk_freq_hz        I2S bit frequence, Sampling_rate = bclk_freq_hz / frame_width / channel_num.
 * @param role                I2S role, use @ref I2S_ROLE
 * @param format_mode         I2S mode, use @ref I2S_FORMAT_MODE
 * @param channel_mode        I2S channel num, Only DSP mode supports more than 2 channels, use @ref I2S_CHANNEL_MODE
 * @param frame_width         I2S frame width, use @ref I2S_SLOT_WIDTH
 * @param data_width          I2S data order, frame_size must equal data_size in 3/4/6-channel mode, use @ref I2S_SLOT_WIDTH
 * @param fs_offset_cycle     I2S first bit offset of the data
 * @param tx_fifo_threshold   I2S tx fifo threshold, should be less than 16
 * @param rx_fifo_threshold   I2S rx fifo threshold, should be less than 16
 */
struct bflb_i2s_config_s {
    uint32_t bclk_freq_hz;
    uint8_t role;
    uint8_t format_mode;
    uint8_t channel_mode;
    uint8_t frame_width;
    uint8_t data_width;
    uint8_t fs_offset_cycle;

    uint8_t tx_fifo_threshold;
    uint8_t rx_fifo_threshold;
};

#ifdef __cplusplus
extern "C" {
#endif

/**
 * @brief Initialize i2s.
 *
 * @param [in] dev device handle
 * @param [in] config pointer to save i2s config
 */
void bflb_i2s_init(struct bflb_device_s *dev, const struct bflb_i2s_config_s *config);

/**
 * @brief Deinitialize i2s.
 *
 * @param [in] dev device handle
 */
void bflb_i2s_deinit(struct bflb_device_s *dev);

/**
 * @brief Enable i2s tx dma.
 *
 * @param [in] dev device handle
 * @param [in] enable true means enable, otherwise disable.
 */
void bflb_i2s_link_txdma(struct bflb_device_s *dev, bool enable);

/**
 * @brief Enable i2s rx dma.
 *
 * @param [in] dev device handle
 * @param [in] enable true means enable, otherwise disable.
 */
void bflb_i2s_link_rxdma(struct bflb_device_s *dev, bool enable);

/**
 * @brief Enable or disable i2s rx fifo threhold interrupt.
 *
 * @param [in] dev device handle
 * @param [in] mask true means disable, false means enable
 */
void bflb_i2s_txint_mask(struct bflb_device_s *dev, bool mask);

/**
 * @brief Enable or disable i2s rx fifo threhold interrupt.
 *
 * @param [in] dev device handle
 * @param [in] mask true means disable, false means enable
 */
void bflb_i2s_rxint_mask(struct bflb_device_s *dev, bool mask);

/**
 * @brief Enable or disable i2s error interrupt.
 *
 * @param [in] dev device handle
 * @param [in] mask true means disable, false means enable
 */
void bflb_i2s_errint_mask(struct bflb_device_s *dev, bool mask);

/**
 * @brief Get i2s interrupt status.
 *
 * @param [in] dev device handle
 * @return interrupt status, use @ref I2S_INTSTS
 */
uint32_t bflb_i2s_get_intstatus(struct bflb_device_s *dev);

/**
 * @brief Control i2s feature.
 *
 * @param [in] dev device handle
 * @param [in] cmd feature command, use @ref I2S_CMD
 * @param [in] arg user data
 * @return A negated errno value on failure.
 */
int bflb_i2s_feature_control(struct bflb_device_s *dev, int cmd, size_t arg);

#ifdef __cplusplus
}
#endif

/**
  * @}
  */

/**
  * @}
  */

#endif