Advertisement
Domy131097

lv5_ps2_polled.c

Dec 11th, 2020
670
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.90 KB | None
  1. #include "xps2.h"
  2. #include "xparameters.h"
  3. #include "xstatus.h"
  4. #include "stdio.h"
  5.  
  6. //***********************************************TO DO 9*************************************************//
  7. //***********************************Definirati konstantu KEYBOARD_ACK***********************************//
  8. #define KEYBOARD_ACK 0xFA
  9.  
  10. //******************************************************************************************************//
  11.  
  12.  
  13. /************************** Prototipi funkcija ******************************/
  14. int Ps2Initialize(u16 Ps2DeviceId);
  15. int Ps2SendData(u8 data);
  16. int Ps2ReceiveData();
  17.  
  18.  
  19. /******************** Definicija globalnih varijabli ***********************/
  20. static XPs2 Ps2Inst;
  21. u8 SendCODE = 0xED;
  22. u8 RxBuffer;
  23.  
  24.  
  25. int main(void)
  26. {
  27.  
  28.     int Status, i, j;
  29.  
  30.     Status = Ps2Initialize(XPAR_PS2_0_DEVICE_ID);
  31.     if (Status != XST_SUCCESS) {
  32.         return XST_FAILURE;
  33.     }
  34.  
  35.     print("Sending data to keyboard\r\n");
  36.     for(i=0; i<8; i++){
  37.  
  38.         //***********************************************TO DO 6************************************************//
  39.         //***Poslati vrijednost varijable SendCODE i nakon toga vrijednost varijable i putem PS2 suèelja********//
  40.         Ps2SendData(SendCODE);
  41.         Ps2SendData(i);
  42.  
  43.         //******************************************************************************************************//
  44.  
  45.         //delay od cca 1s
  46.         for(j=0; j<66670000/12; j++);
  47.     }
  48.  
  49.     print("Receiving data from keyboard:\r\n");
  50.     while(1){
  51.  
  52.         //***********************************************TO DO 7*************************************************//
  53.         //*********************************Primiti 1 byte podataka od tipkovnice********************************//
  54.         Status = Ps2ReceiveData();
  55.  
  56.         //******************************************************************************************************//
  57.  
  58.  
  59.         if (Status != XST_SUCCESS){
  60.             return XST_FAILURE;
  61.             print("Data receiving FAILED!\r\n");
  62.         }
  63.         else
  64.             //***********************************************TO DO 8********************************************//
  65.             //****************Ispisati vrijednost primljenu od tipkovnice u heksadecimalnom obliku**************//
  66.             xil_printf("%x\r\n", RxBuffer);
  67.  
  68.             //**************************************************************************************************//
  69.     }
  70. }
  71.  
  72. /****************************************************************************/
  73. /* Funkcija za inicijalizaciju tipkovnice koja prima device ID tipkovnice
  74. ****************************************************************************/
  75. int Ps2Initialize(u16 Ps2DeviceId)
  76. {
  77.     XPs2_Config *Ps2ConfigPtr;
  78.  
  79.     //***********************************************TO DO 1*************************************************//
  80.     //******Dohvatiti konfiguraciju PS2 suèelja, povratnu vrijednost spremiti u varijablu Ps2ConfigPtr*******//
  81.     Ps2ConfigPtr = XPs2_LookupConfig(Ps2DeviceId);
  82.  
  83.     //******************************************************************************************************//
  84.     if (Ps2ConfigPtr == NULL) {
  85.         print("Config Lookup FAILED!\r\n");
  86.         return XST_FAILURE;
  87.     }
  88.     else
  89.         print("Config Lookup SUCCESS!\r\n");
  90.  
  91.  
  92.     //***********************************************TO DO 2*************************************************//
  93.     //*************************************Inicijalizirati PS2 kontroler*************************************//
  94.     XPs2_CfgInitialize(&Ps2Inst, Ps2ConfigPtr, Ps2ConfigPtr->BaseAddress);
  95.  
  96.     //*******************************************************************************************************//
  97.  
  98.     print("PS2 Initialize SUCCESS!\r\n");
  99.     return XST_SUCCESS;
  100. }
  101.  
  102. /****************************************************************************/
  103. /* Funkcija za slanje podataka putem PS2 suèelja.
  104.  * Funkcija prima podatak koji treba poslati.
  105. ****************************************************************************/
  106. int Ps2SendData(u8 data){
  107.  
  108.     u32 StatusReg;
  109.     u32 BytesSent;
  110.     u32 BytesReceived;
  111.     int ACK_WAIT = 10000;
  112.  
  113.     //***********************************************TO DO 3*************************************************//
  114.     //****Poslati 1 byte podataka putem PS2 sucelja, povratnu vrijdnost spremiti u varijablu BytesSent*******//
  115.     BytesSent = XPs2_Send(&Ps2Inst, &data, 1);
  116.  
  117.     //*******************************************************************************************************//
  118.     if( BytesSent != 1) {
  119.         print("Data Send FAILED!\r\n");
  120.         return XST_FAILURE;
  121.     }
  122.  
  123.     //Cekati dok tipkovnica ne posalje odgovor (ACK byte)
  124.     do {
  125.         StatusReg = XPs2_GetStatus(&Ps2Inst);
  126.         ACK_WAIT--;
  127.     }while((StatusReg & XPS2_STATUS_RX_FULL) == 0 && ACK_WAIT > 0);
  128.  
  129.     //***********************************************TO DO 4*************************************************//
  130.     //****Primiti 1 byte podataka putem PS2 sucelja, povratnu vrijdnost spremiti u varijablu BytesReceived***//
  131.     BytesReceived = XPs2_Recv(&Ps2Inst, &RxBuffer, 1);
  132.  
  133.     //*******************************************************************************************************//
  134.  
  135.     //Provjeriti jel primljen ACK byte
  136.     if (RxBuffer != KEYBOARD_ACK) {
  137.         print("Nije primljen ACK byte!\r\n");
  138.         return XST_FAILURE;
  139.     }
  140.     else
  141.         xil_printf("Data %x Send SUCESS!\r\n", data);
  142.  
  143.     return XST_SUCCESS;
  144. }
  145.  
  146. /****************************************************************************/
  147. /* Funkcija za primanje podataka putem PS2 suèelja.
  148. ****************************************************************************/
  149. int Ps2ReceiveData(){
  150.  
  151.     u32 StatusReg;
  152.     u32 BytesReceived;
  153.  
  154.     do {
  155.         StatusReg = XPs2_GetStatus(&Ps2Inst);
  156.     }while((StatusReg & XPS2_STATUS_RX_FULL) == 0);
  157.  
  158.     //***********************************************TO DO 5*************************************************//
  159.     //***Primiti 1 byte podataka putem PS2 sucelja, povratnu vrijdnost spremiti u varijablu BytesReceived****//
  160.     BytesReceived = XPs2_Recv(&Ps2Inst, &RxBuffer, 1);
  161.  
  162.     //*******************************************************************************************************//
  163.  
  164.     return XST_SUCCESS;
  165. }
  166.  
Advertisement
RAW Paste Data Copied
Advertisement