Advertisement
Mary_99

FIFO FINAL

Dec 20th, 2020 (edited)
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.77 KB | None | 0 0
  1. #include <stdint.h>
  2. #include <stdio.h>
  3. #include <stdbool.h>
  4. #include <string.h>
  5. #include <ctype.h>
  6. #include "include/AT91SAM9263.h"
  7.  
  8. #define MCK 100000000
  9. #define CD 115200
  10. #define BUFFERSIZE 0xF
  11. #define ASCII_ENTER 10
  12.  
  13. typedef struct FIFO
  14. {
  15.   char buffer[BUFFERSIZE+1];
  16.   unsigned int head;
  17.   unsigned int tail;
  18. };
  19.  
  20. void fifoInit (struct FIFO *fifo)
  21. {
  22.   fifo->head = 0;
  23.   fifo->tail = 0;
  24. }
  25.  
  26. void fifoEmpty (struct FIFO *fifo)
  27. {
  28.   fifo->head = fifo->tail;
  29. }
  30.  
  31. int fifoPut (struct FIFO *fifo, char data)
  32. {
  33.   if((fifo->tail - fifo->head) == 1 || (fifo->head - fifo->tail) == BUFFERSIZE)
  34.   {// fifo  overflow
  35.     return -1;
  36.   }
  37.   fifo->buffer[fifo->head] = data;
  38.   fifo->head = (fifo->head + 1) & BUFFERSIZE; //put 1 byte successfully
  39.   return 1;
  40. }
  41.  
  42. int fifoGet (struct FIFO *fifo, char *data)
  43. {
  44.   if(fifo->head != fifo->tail)
  45.   {
  46.     *data = fifo->buffer[fifo->tail];
  47.     fifo->tail = (fifo->tail +1) & BUFFERSIZE;
  48.     return 1; // get 1 byte successfully
  49.   }
  50.   else
  51.   {
  52.     return -1; //no data in FIFO
  53.   }
  54. }
  55.  
  56.  
  57. void disableInterrupts(void)
  58. {
  59.   AT91C_BASE_DBGU->DBGU_IDR = (AT91C_US_RXRDY|AT91C_US_TXRDY|AT91C_US_ENDRX|AT91C_US_ENDTX|
  60.                               AT91C_US_OVRE|AT91C_US_FRAME|AT91C_US_PARE|AT91C_US_TXEMPTY|
  61.                               AT91C_US_TXBUFE|AT91C_US_RXBUFF|AT91C_US_COMM_TX|AT91C_US_COMM_RX); //desactivate debug interrupt
  62. }
  63.  
  64. void configurePeripheralPorts(void)
  65. {
  66.   AT91C_BASE_PIOC->PIO_ASR |= AT91C_PIO_PC30; // debug receiver data RxD//Pio Periherial A select Register//reciver
  67.   AT91C_BASE_PIOC->PIO_PDR |= AT91C_PIO_PC30; //PIO Disable Register
  68.  
  69.   AT91C_BASE_PIOC->PIO_ASR |= AT91C_PIO_PC31; //debug receiver data TXD //transmitter
  70.   AT91C_BASE_PIOC->PIO_PDR |= AT91C_PIO_PC31;
  71. }
  72.  
  73. void turnOffReciver(void)
  74. {
  75.   AT91C_BASE_DBGU->DBGU_CR |= AT91C_US_RSTRX; //reset and turn off/disable receiver
  76. }
  77.  
  78. void turnOffTransmitter(void)
  79. {
  80.     AT91C_BASE_DBGU->DBGU_CR |= AT91C_US_RSTTX; // reset and turn off/disable treansmitter
  81. }
  82.  
  83. void disabeReciver(void)
  84. {
  85.   AT91C_BASE_DBGU->DBGU_CR |= AT91C_US_RXDIS; //disable receiver
  86. }
  87.  
  88. void disableTransmitter(void)
  89. {
  90.     AT91C_BASE_DBGU->DBGU_CR |= AT91C_US_TXDIS; // disable treansmitter
  91. }
  92.  
  93. void configureThroughput(void)
  94. {
  95.   AT91C_BASE_DBGU->DBGU_BRGR = MCK/(16*CD); // MCK mster CLock/ (CD //clock Divisor *16) //baud Rate Generator Register //speed of transmiition
  96. }
  97.  
  98. void configureOperationMode(void)
  99. {
  100.     AT91C_BASE_DBGU->DBGU_MR = AT91C_US_CHMODE_NORMAL| AT91C_US_PAR_NONE ; // set nomral mode 14 15 bit 0 0 // set no parity
  101. }
  102.  
  103. void turnOnReciver(void)
  104. {
  105.     AT91C_BASE_DBGU->DBGU_CR |= AT91C_US_RXEN;
  106. }
  107.  
  108. void turnOnTransmitter(void)
  109. {
  110.   AT91C_BASE_DBGU->DBGU_CR |= AT91C_US_TXEN;
  111. }
  112.  
  113. void dbguInit(void)
  114. {
  115.   disableInterrupts();
  116.   turnOffReciver();
  117.   turnOffTransmitter();
  118.   disabeReciver();
  119.   disableTransmitter();
  120.   configurePeripheralPorts();
  121.   configureThroughput();
  122.   configureOperationMode();
  123.   turnOnReciver();
  124.   turnOnTransmitter();
  125. }
  126.  
  127. void dbguPrintNewLine()
  128. {
  129.     while(!(AT91C_BASE_DBGU->DBGU_CSR & AT91C_US_TXRDY)){}; //wait until Tx buffer busy - check TXRDY flag
  130.     //CSR channel status regster //TXRDY Interrupt
  131.     AT91C_BASE_DBGU->DBGU_THR = '\n'; //write a singel char to Transmitter Holding Register
  132. }
  133.  
  134. void printMessage(const char*StringToPrint)
  135. {
  136.   int iterrator = 0;
  137.   while(StringToPrint[iterrator] != '\0')
  138.   {
  139.     while(!(AT91C_BASE_DBGU->DBGU_CSR & AT91C_US_TXRDY)){}; //wait until Tx buffer busy - checj TXRDY flag
  140.     //CSR channel status regster //TXRDY Interrupt
  141.     AT91C_BASE_DBGU->DBGU_THR =StringToPrint[iterrator]; //write a singel char to Transmitter Holding Register
  142.     iterrator++;
  143.   }
  144. }
  145.  
  146. void dbguPrintBufferOverflowErrorMessage()
  147. {
  148.   dbguPrintNewLine();
  149.   printMessage("There is no space left in the buffer");
  150. }
  151.  
  152. void dbguPrintAsciiFifo(struct FIFO * fifo, char *data)
  153. {
  154.   dbguPrintNewLine();
  155.   while(fifoGet(fifo,data)!=-1)
  156.   {
  157.     while(!(AT91C_BASE_DBGU->DBGU_CSR & AT91C_US_TXRDY)){}; //wait until Tx buffer busy - checj TXRDY flag //CSR channel status regster //TXRDY Interrupt
  158.     AT91C_BASE_DBGU->DBGU_THR = *data;
  159.   }
  160.   dbguPrintNewLine();
  161.   fifoEmpty(fifo);
  162. }
  163.  
  164. char dbguReadAsciiFifo(struct FIFO * fifo, char *data)
  165. {
  166.     while(!(AT91C_BASE_DBGU->DBGU_CSR & AT91C_US_RXRDY)){}; //data avaible
  167.  
  168.     char currentCharacter = AT91C_BASE_DBGU->DBGU_RHR;
  169.     if(currentCharacter == ASCII_ENTER)
  170.     {
  171.       dbguPrintAsciiFifo(fifo,data);
  172.     }
  173.     else if(fifoPut(fifo, AT91C_BASE_DBGU->DBGU_RHR)==-1)
  174.     {
  175.       dbguPrintBufferOverflowErrorMessage();
  176.       dbguPrintAsciiFifo(fifo,data);
  177.     }
  178. }
  179.  
  180. int main()
  181. {
  182.   struct FIFO * fifo;
  183.   fifoInit(fifo);
  184.   dbguInit();
  185.  
  186.   char character [BUFFERSIZE];
  187.   while(true)
  188.   {
  189.     dbguReadAsciiFifo(fifo, character);
  190.   }
  191.  
  192.   return 0;
  193. }
  194.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement