Imported Upstream version 3.2.2
[debian/gnuradio] / usrp2 / firmware / lib / dbsm.h
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2007 Free Software Foundation, Inc.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 #ifndef INCLUDED_DBSM_H
19 #define INCLUDED_DBSM_H
20
21 /*
22  * Double Buffering State Machine
23  */
24
25 #include <stdint.h>
26 #include "bool.h"
27
28 struct _dbsm;
29 typedef struct _dbsm dbsm_t;
30
31 /*
32  * pointer to function that does packet inspection.
33  *
34  * If one of these returns true, it means that the s/w
35  * handled that packet, and that it should NOT be passed
36  * on to the normal destination port.
37  */
38 typedef bool (*inspector_t)(dbsm_t *sm, int buf_this);
39
40 bool dbsm_nop_inspector(dbsm_t *sm, int buf_this);      // returns false
41
42
43 typedef struct
44 {
45   uint16_t      port;
46   uint16_t      first_line;
47   uint16_t      last_line;
48 } buf_cmd_args_t;
49
50 /*!
51  * double buffer state machine
52  */
53 struct _dbsm
54 {
55   uint8_t         buf0;      // Must be even. This machine uses buf0 and buf0+1
56   uint8_t         running;
57   uint8_t         rx_idle;
58   uint8_t         tx_idle;
59   buf_cmd_args_t  recv_args;
60   buf_cmd_args_t  send_args;
61   inspector_t     inspect;
62   uint32_t        precomputed_receive_to_buf_ctrl_word[2];
63   uint32_t        precomputed_send_from_buf_ctrl_word[2];
64   int             last_line_adj;
65 };
66
67 void dbsm_init(dbsm_t *sm, int buf0,
68                const buf_cmd_args_t *recv, const buf_cmd_args_t *send,
69                inspector_t inspect);
70
71 void dbsm_start(dbsm_t *sm);
72 void dbsm_stop(dbsm_t *sm);
73 void dbsm_process_status(dbsm_t *sm, uint32_t status);
74 void dbsm_handle_tx_underrun(dbsm_t *sm);
75 void dbsm_handle_rx_overrun(dbsm_t *sm);
76
77 /*
78  * The cpu calls this when it want to ensure that it can send a buffer
79  * to the same destination being used by this state machine.
80  *
81  * If neither buffer is EMPTYING it returns immediately.  If a buffer
82  * is EMPYTING, it waits for the h/w to transition to the DONE or
83  * ERROR state.
84  *
85  * When this function returns, the caller queues it's buffer and busy
86  * waits for it to complete.
87  */
88 void dbsm_wait_for_opening(dbsm_t *sm);
89
90 #endif /* INCLUDED_DBSM_H */