1 /* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 #ifndef TENSORFLOW_LITE_MICRO_EXAMPLES_MICRO_SPEECH_ESP_RINGBUF_H_
17 #define TENSORFLOW_LITE_MICRO_EXAMPLES_MICRO_SPEECH_ESP_RINGBUF_H_
18 
19 #include <aos/kernel.h>
20 #include <stdint.h>
21 
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25 
26 #define pdTRUE (1)
27 #define pdFALSE (-1)
28 
29 #define RB_FAIL -1
30 #define RB_ABORT -1
31 #define RB_WRITER_FINISHED -2
32 #define RB_READER_UNBLOCK -3
33 
34 typedef struct ringbuf {
35   char* name;
36   uint8_t* base; /**< Original pointer */
37   /* XXX: these need to be volatile? */
38   uint8_t* volatile readptr;  /**< Read pointer */
39   uint8_t* volatile writeptr; /**< Write pointer */
40   volatile ssize_t fill_cnt;  /**< Number of filled slots */
41   ssize_t size;               /**< Buffer size */
42   aos_sem_t can_read;
43   aos_sem_t can_write;
44   aos_sem_t lock;
45   int abort_read;
46   int abort_write;
47   int writer_finished;  // to prevent infinite blocking for buffer read
48   int reader_unblock;
49 } ringbuf_t;
50 
51 ringbuf_t* rb_init(const char* rb_name, uint32_t size);
52 void rb_abort_read(ringbuf_t* rb);
53 void rb_abort_write(ringbuf_t* rb);
54 void rb_abort(ringbuf_t* rb);
55 void rb_reset(ringbuf_t* rb);
56 /**
57  * @brief Special function to reset the buffer while keeping rb_write aborted.
58  *        This rb needs to be reset again before being useful.
59  */
60 void rb_reset_and_abort_write(ringbuf_t* rb);
61 void rb_stat(ringbuf_t* rb);
62 ssize_t rb_filled(ringbuf_t* rb);
63 ssize_t rb_available(ringbuf_t* rb);
64 int rb_read(ringbuf_t* rb, uint8_t* buf, int len, uint32_t ticks_to_wait);
65 int rb_write(ringbuf_t* rb, const uint8_t* buf, int len,
66              uint32_t ticks_to_wait);
67 void rb_cleanup(ringbuf_t* rb);
68 void rb_signal_writer_finished(ringbuf_t* rb);
69 void rb_wakeup_reader(ringbuf_t* rb);
70 int rb_is_writer_finished(ringbuf_t* rb);
71 
72 #ifdef __cplusplus
73 }
74 #endif
75 
76 #endif  // TENSORFLOW_LITE_MICRO_EXAMPLES_MICRO_SPEECH_ESP_RINGBUF_H_
77