can_ring.c

Go to the documentation of this file.
00001 
00008 #include <includes.h>
00009 
00010 /*
00011 #define CAN_TX_BUFFER_LENGTH 128
00012 CAN_FRAME can_tx_buffer[CAN_TX_BUFFER_LENGTH];
00013 
00014 //Make these globals volatile to allow mixed context usage.
00015 volatile int can_tx_in_idx = CAN_TX_BUFFER_LENGTH-1;
00016 volatile int can_tx_out_idx = CAN_TX_BUFFER_LENGTH-1;
00017 */
00018 
00026 void can_ring_init(CAN_RING * ring, CAN_FRAME * frame_buf, int buf_len){
00027   ring->buf = frame_buf;
00028   ring->buf_len = buf_len;
00029   /*
00030   ring->in_idx = buf_len - 1;
00031   ring->out_idx = buf_len - 1;
00032   */
00033   ring->in_idx = 0;
00034   ring->out_idx = 0;
00035 }
00036 
00044 int can_ring_push(CAN_RING * ring, CAN_FRAME * frame) {
00045  // int  ret;
00046   int next_in_idx;
00047   /*
00048   int next_in_idx = ring->in_idx + 1;
00049 
00050   if(next_in_idx >= ring->buf_len){
00051     next_in_idx -= ring->buf_len;
00052   }  
00053   if(next_in_idx == ring->out_idx) {
00054     ret = 1;
00055   } else {
00056     ring->buf[next_in_idx] = *frame;
00057     ret = 0;
00058     ring->in_idx = next_in_idx;
00059   }
00060   */
00061 
00062   next_in_idx = ring->in_idx;
00063   if (++next_in_idx == ring->buf_len) {next_in_idx = 0;}
00064   if (next_in_idx == ring->out_idx) {return 1;} //buffer full
00065 
00066   ring->buf[next_in_idx] = *frame;
00067   ring->in_idx = next_in_idx;
00068 
00069   return 0;
00070 }
00071 
00079 int can_ring_pop(CAN_RING * ring, CAN_FRAME * frame){
00080  
00081   if (ring->out_idx != ring->in_idx)
00082   {
00083     *frame = ring->buf[ring->out_idx];
00084     if (ring->out_idx == (ring->buf_len) - 1)
00085     {
00086       ring->out_idx = 0;
00087     }
00088     else
00089     {
00090       ++ring->out_idx;
00091     }
00092     return 0;   //frame popped successfully
00093   }
00094   else
00095   {
00096     return 1;   //buffer empty
00097   }
00098  
00099  /* int out_idx = ring->out_idx;
00100   int  ret;
00101   int next_out_idx = out_idx + 1;
00102 
00103   if(next_out_idx >= ring->buf_len){
00104     next_out_idx -= ring->buf_len;
00105   }
00106   if(out_idx == ring->in_idx){
00107     ret = 1;
00108   } else {
00109     *frame = ring->buf[next_out_idx];
00110     ret = 0;
00111     ring->out_idx = next_out_idx;
00112   }
00113   return ret;
00114 
00115   */
00116 }
00117 
00118 
Generated on Tue Jun 29 16:36:14 2010 by  doxygen 1.6.3