Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "AT91SAM9263.h"
- #define BUFFERSIZE 20
- typedef struct FIFO {
- char buffer [BUFFERSIZE];
- int head;
- int tail;
- };
- void FIFO_Init (struct FIFO *Fifo);
- void FIFO_Empty (struct FIFO *Fifo);
- int FIFO_Put (struct FIFO *Fifo, char Data);
- int FIFO_Get (struct FIFO *Fifo, char *Data);
- void dbgu_print_ascii(char Buffer[]){
- int i = 0;
- while (Buffer[i]) {
- while ( !(AT91C_BASE_DBGU->DBGU_CSR & (AT91C_US_TXRDY) )){};
- AT91C_BASE_DBGU->DBGU_THR = Buffer[i];
- i++;
- }
- }
- void dbgu_read_ascii (char *Buffer){
- while (! (AT91C_BASE_DBGU->DBGU_CSR & AT91C_US_RXRDY )){};
- *Buffer = AT91C_BASE_DBGU->DBGU_RHR;
- *(Buffer+1) = '\0';
- }
- static void Open_DBGU (void){
- //Wyłącz przerwania od portu DBGU
- AT91C_BASE_DBGU->DBGU_IDR = 0xFFFFFFFF;
- //Resetuj i wyłącz odbiornik
- AT91C_BASE_DBGU->DBGU_CR = (AT91C_US_RSTRX | AT91C_US_RSTTX | AT91C_US_RXDIS | AT91C_US_TXDIS);
- //Konfiguracja portów wejścia-wyjścia jako porty RxD i TxD DBGU
- AT91C_BASE_PIOC->PIO_ASR = (1<<30 | 1<<31);
- AT91C_BASE_PIOC->PIO_PDR = (1<<30 | 1<<31);
- //Konfiguracja szybkości transmisji portu szeregowego
- AT91C_BASE_DBGU->DBGU_BRGR = (651);
- //Konfiguracja trybu pracy, tryb normalny bez przystości (8N1)
- AT91C_BASE_DBGU->DBGU_MR = (AT91C_US_CHMODE_NORMAL | AT91C_US_PAR_NONE);
- //Włącz odbiornik
- AT91C_BASE_DBGU->DBGU_CR = (AT91C_US_RXEN | AT91C_US_TXEN);
- }
- int main(void){
- char *Bufferr;
- char *Bufferr2;
- struct FIFO kolejka;
- FIFO_Init(&kolejka);
- FIFO_Empty(&kolejka);
- Open_DBGU();
- dbgu_print_ascii("Alfabet ");
- int i;
- while (1)
- {
- dbgu_read_ascii(Bufferr);
- if(*Bufferr == 0x0D){
- for (i = 0; i < 5; i++){
- if (FIFO_Get(&kolejka,Bufferr2) == 1){
- dbgu_print_ascii(Bufferr2);
- }
- }
- dbgu_print_ascii(" Wydrukowano Fifo ");
- *Bufferr = 0x0D;
- *(Bufferr+1) = 0x0A;
- *(Bufferr+2) = '\0';
- dbgu_print_ascii(Bufferr);
- }
- else{
- if(FIFO_Put(&kolejka,*Bufferr) == -1);
- }
- }
- }
- void FIFO_Init (struct FIFO *Fifo){
- Fifo->head = 0;
- Fifo->tail = 0;
- }
- void FIFO_Empty (struct FIFO *Fifo){
- Fifo->head = Fifo->tail = 0;
- }
- int FIFO_Put (struct FIFO *Fifo, char Data){
- if ( ( Fifo->tail-Fifo->head) == 1 || ( (Fifo->tail == 0 ) && (Fifo->head == BUFFERSIZE-1)) ){
- return -1;
- };
- Fifo->buffer[Fifo->head] = Data;
- if(Fifo->head < BUFFERSIZE - 1)
- Fifo->head++;
- else {
- Fifo->head = 0;
- Fifo->head++;
- }
- return 1;
- }
- int FIFO_Get (struct FIFO *Fifo, char *Data){
- if ((Fifo->head != Fifo->tail)){
- *Data = Fifo->buffer[Fifo->tail];
- if(Fifo->tail < BUFFERSIZE - 1)
- Fifo->tail++;
- else Fifo->tail = 0;
- return 1;
- }
- else {
- Fifo->head = Fifo->tail = 0;
- return -1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement