]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - keystone-rtos/ibl.git/blob - src/driver/stream/stream_orig.c
Update copyright information
[keystone-rtos/ibl.git] / src / driver / stream / stream_orig.c
1 /**
2  *   @file  stream.c
3  *
4  *   @brief   
5  *      The file implements the STREAM module.
6  *
7  *  \par
8  *  NOTE:
9  *      (C) Copyright 2008, Texas Instruments, Inc.
10  *
11  *  \par
12  */
15 /**********************************************************************
16  ************************** Local Structures **************************
17  **********************************************************************/
18 #include "types.h"
19 #include "iblcfg.h"
20 #include <string.h>
22 /**
23  * @brief 
24  *  The structure describes the Stream Master Control block.
25  *
26  * @details
27  *  The structure stores information about the stream module
28  *  internal buffers and state information.
29  */
30 typedef struct STREAM_MCB
31 {
32     /**
33      * @brief   Flag which indicates if the stream buffer is open or not?
34      */
35     Bool       is_open;
36         
37     /**
38      * @brief   This is the *internal* stream buffer.
39      */
40     Uint8      buffer[MAX_SIZE_STREAM_BUFFER];
42     /**
43      * @brief   This is the read index from where data is read.
44      */
45     Int32      read_idx;
47     /**
48      * @brief   This is the write index to which data is written.
49      */
50     Int32      write_idx;
52     /**
53      * @brief   This is the free size available in the internal buffer.
54      */
55     Int32      free_size;
56 }STREAM_MCB;
58 /**********************************************************************
59  ************************** Global Variables **************************
60  **********************************************************************/
62 /**
63  * @brief   This is the STREAM Master control block which keeps track
64  * of all the stream module information.
65  */
66 STREAM_MCB  stream_mcb;
68 /**********************************************************************
69  ************************** Stream Functions **************************
70  **********************************************************************/
72 /**
73  *  @b Description
74  *  @n
75  *      The function is called to open the stream module
76  *
77  *  @param[in]  chunk_size
78  *      Maximum amount of data that can be received at any
79  *      instant by the boot module.
80  *
81  *  @retval
82  *      Success -   0
83  *  @retval
84  *      Error   -   <0
85  */
86 Int32 stream_open (Uint32 chunk_size)
87 {
88     /* Basic Validations: Ensure that the chunk size is not greater
89      * than the internal buffer size. */
90     if (chunk_size > MAX_SIZE_STREAM_BUFFER)
91         return -1;
93     /* Initialize the Master control block. */
94     stream_mcb.is_open   = TRUE;
95     stream_mcb.read_idx  = 0;
96     stream_mcb.write_idx = 0;
97     stream_mcb.free_size = MAX_SIZE_STREAM_BUFFER;
99     /* Module has been initialized. */
100     return 0;
103 /**
104  *  @b Description
105  *  @n
106  *      The function is called to read data from the stream module.
107  *
108  *  @param[in]  ptr_data
109  *      Pointer to the data buffer where the data will be copied to.
110  *  @param[in]  num_bytes
111  *      Number of bytes to be read.
112  *
113  *  @retval
114  *      Success -   Number of bytes actually read
115  *  @retval
116  *      Error   -   <0
117  */
118 Int32 stream_read (Uint8* ptr_data, Int32 num_bytes)
120     Int32 index;
121     Int32 num_bytes_to_read;
122     
123     /* Determine the number of bytes which can be read. */
124     if (num_bytes > (MAX_SIZE_STREAM_BUFFER - stream_mcb.free_size))
125     {
126         /* User has requested more data than what is available. In this case we 
127          * can return only what we have. */
128         num_bytes_to_read = (MAX_SIZE_STREAM_BUFFER - stream_mcb.free_size);
129     }
130     else
131     {
132         /* User has requested less data than what is available. In this case we 
133          * return only what the user wants. */            
134         num_bytes_to_read = num_bytes;
135     }
137     /* There is data available copy it from the internal to the user supplied buffer.  */
138     for (index = 0; index < num_bytes_to_read; index++)
139     {
140         /* Copy the data to the "write" index. */
141         *(ptr_data + index) = *(stream_mcb.buffer + stream_mcb.read_idx);
143         /* Increment the read index. */
144         stream_mcb.read_idx = (stream_mcb.read_idx + 1) % MAX_SIZE_STREAM_BUFFER;
145     }
147     /* Once data has been copied; increment the free size accordingly */
148     stream_mcb.free_size = stream_mcb.free_size + num_bytes_to_read;
150     /* Return the number of bytes read. */
151     return num_bytes_to_read;
154 /**
155  *  @b Description
156  *  @n
157  *      The function is called to write data to the stream
158  *      module.
159  *
160  *  @param[in]  ptr_data
161  *      Pointer to the data buffer which contains the data to be copied.
162  *  @param[in]  num_bytes
163  *      Number of bytes being written
164  *
165  *  @retval
166  *      Success -   0
167  *  @retval
168  *      Error   -   <0
169  */
170 Int32 stream_write (Uint8* ptr_data, Int32 num_bytes)
172     Int32 index;
174     /* Basic Validations: Ensure there is sufficient space to copy the data. */
175     if (num_bytes > stream_mcb.free_size)
176         return -1;
178     /* Basic Validations: Make sure the pointers are valid. */
179     if (ptr_data == NULL)
180         return -1;
182     /* There was sufficient space to copy the data lets do so but we copy byte by byte
183      * since the internal buffer is circular and we can wrap around... */
184     for (index = 0; index < num_bytes; index++)
185     {
186         /* Copy the data to the "write" index. */
187         *(stream_mcb.buffer + stream_mcb.write_idx) = *(ptr_data + index);
189         /* Increment the write index. */
190         stream_mcb.write_idx = (stream_mcb.write_idx + 1) % MAX_SIZE_STREAM_BUFFER;
191     }
193     /* Once data has been copied; decrement the free size accordingly */
194     stream_mcb.free_size = stream_mcb.free_size - num_bytes;
195     return 0;
198 /**
199  *  @b Description
200  *  @n
201  *      The function is used to check if the stream buffers are empty or
202  *      not?
203  *
204  *  @retval
205  *      Empty       -   TRUE
206  *  @retval
207  *      Not Empty   -   FALSE
208  */
209 Bool stream_isempty (void)
211     /* Check the number of free bytes available? */
212     if (stream_mcb.free_size == MAX_SIZE_STREAM_BUFFER)
213         return TRUE;
215     /* There is data in the stream buffer; so its not empty. */
216     return FALSE;
219 /**
220  *  @b Description
221  *  @n
222  *      The function closes the stream module.
223  *
224  *  @retval
225  *      Not Applicable.
226  */
227 void stream_close (void)
229     /* The stream module is no longer open... */
230     stream_mcb.is_open = FALSE;
231     return;
234 /**
235  *  @b Description
236  *  @n
237  *      The function initializes the stream module.
238  *
239  *  @retval
240  *      Not Applicable.
241  */
242 void stream_init (void)
244     /* Reset the memory contents. */
245     memset ((void *)&stream_mcb, 0, sizeof(STREAM_MCB));
247     /* Make sure we initialize the free size correctly. */
248     stream_mcb.free_size = MAX_SIZE_STREAM_BUFFER;
249     return;