Advertisement
MagnusArias

SW | FIFO

Jun 15th, 2016
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.74 KB | None | 0 0
  1. #include "AT91SAM9263.h"
  2. #define BUFFERSIZE 20
  3.  
  4. typedef struct FIFO {
  5.     char buffer [BUFFERSIZE];
  6.     int head;
  7.     int tail;
  8. };
  9.  
  10. void FIFO_Init (struct FIFO *Fifo);
  11. void FIFO_Empty (struct FIFO *Fifo);
  12. int FIFO_Put (struct FIFO *Fifo, char Data);
  13. int FIFO_Get (struct FIFO *Fifo, char *Data);
  14.  
  15. void dbgu_print_ascii(char Buffer[]){
  16.     int i = 0;
  17.     while (Buffer[i]) {
  18.         while ( !(AT91C_BASE_DBGU->DBGU_CSR & (AT91C_US_TXRDY) )){};
  19.             AT91C_BASE_DBGU->DBGU_THR = Buffer[i];
  20.             i++;
  21.     }
  22. }
  23.  
  24. void dbgu_read_ascii (char *Buffer){
  25.         while (! (AT91C_BASE_DBGU->DBGU_CSR & AT91C_US_RXRDY )){};
  26.        
  27.         *Buffer = AT91C_BASE_DBGU->DBGU_RHR;
  28.         *(Buffer+1) = '\0';
  29. }
  30.  
  31. static void Open_DBGU (void){  
  32.     //Wyłącz przerwania od portu DBGU
  33.     AT91C_BASE_DBGU->DBGU_IDR = 0xFFFFFFFF;
  34.    
  35.     //Resetuj i wyłącz odbiornik
  36.     AT91C_BASE_DBGU->DBGU_CR = (AT91C_US_RSTRX | AT91C_US_RSTTX | AT91C_US_RXDIS | AT91C_US_TXDIS);
  37.    
  38.     //Konfiguracja portów wejścia-wyjścia jako porty RxD i TxD DBGU
  39.     AT91C_BASE_PIOC->PIO_ASR = (1<<30 | 1<<31);
  40.     AT91C_BASE_PIOC->PIO_PDR = (1<<30 | 1<<31);
  41.  
  42.     //Konfiguracja szybkości transmisji portu szeregowego
  43.     AT91C_BASE_DBGU->DBGU_BRGR = (651);
  44.    
  45.     //Konfiguracja trybu pracy, tryb normalny bez przystości (8N1)
  46.     AT91C_BASE_DBGU->DBGU_MR = (AT91C_US_CHMODE_NORMAL | AT91C_US_PAR_NONE);
  47.    
  48.     //Włącz odbiornik
  49.     AT91C_BASE_DBGU->DBGU_CR = (AT91C_US_RXEN | AT91C_US_TXEN);
  50. }
  51.  
  52. int main(void){
  53.     char *Bufferr;
  54.     char *Bufferr2;
  55.     struct FIFO kolejka;
  56.     FIFO_Init(&kolejka);
  57.     FIFO_Empty(&kolejka);
  58.  
  59.     Open_DBGU();
  60.     dbgu_print_ascii("Alfabet ");
  61.     int i;
  62.    
  63.     while (1)
  64.     {  
  65.         dbgu_read_ascii(Bufferr);
  66.  
  67.         if(*Bufferr == 0x0D){
  68.            
  69.             for (i = 0; i < 5; i++){
  70.                
  71.                 if (FIFO_Get(&kolejka,Bufferr2) == 1){
  72.                     dbgu_print_ascii(Bufferr2);
  73.                 }
  74.             }
  75.                        
  76.             dbgu_print_ascii(" Wydrukowano Fifo ");
  77.            
  78.             *Bufferr = 0x0D;
  79.             *(Bufferr+1) = 0x0A;
  80.             *(Bufferr+2) = '\0';
  81.             dbgu_print_ascii(Bufferr);
  82.            
  83.         }
  84.         else{
  85.             if(FIFO_Put(&kolejka,*Bufferr) == -1);
  86.         }  
  87.     }
  88. }
  89.  
  90. void FIFO_Init (struct FIFO *Fifo){
  91.     Fifo->head = 0;
  92.     Fifo->tail = 0;
  93. }
  94.    
  95. void FIFO_Empty (struct FIFO *Fifo){
  96.     Fifo->head = Fifo->tail = 0;
  97. }
  98.  
  99. int FIFO_Put (struct FIFO *Fifo, char Data){
  100.     if ( ( Fifo->tail-Fifo->head) == 1 ||  ( (Fifo->tail == 0 ) && (Fifo->head == BUFFERSIZE-1)) ){
  101.         return -1;
  102.     };
  103.    
  104.     Fifo->buffer[Fifo->head] = Data;
  105.  
  106.     if(Fifo->head < BUFFERSIZE - 1)
  107.         Fifo->head++;
  108.     else {
  109.         Fifo->head = 0;
  110.         Fifo->head++;
  111.     }
  112.    
  113.     return 1;
  114. }
  115.    
  116. int FIFO_Get (struct FIFO *Fifo, char *Data){
  117.     if ((Fifo->head != Fifo->tail)){
  118.         *Data = Fifo->buffer[Fifo->tail];
  119.        
  120.         if(Fifo->tail < BUFFERSIZE - 1)
  121.             Fifo->tail++;
  122.         else Fifo->tail = 0;
  123.        
  124.         return 1;
  125.     }
  126.     else {
  127.         Fifo->head = Fifo->tail = 0;
  128.         return -1;
  129.     }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement