pseudo.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*====================================================================*
  2. *
  3. * encapsulate and transmit as many Ethernet frames as possible
  4. * without overflowing the SPI write buffer;
  5. *
  6. *--------------------------------------------------------------------*/
  7. function transmit frame do read write buffer space available register;
  8. while transmit queue is not empty and write buffer space is available do encapsulate transmit frame;
  9. write transmit frame to write buffer;
  10. if error then return failure;
  11. end if;
  12. update transmit statistics;
  13. remove frame from transmit queue;
  14. end while;
  15. return (success);
  16. end function;
  17. /*====================================================================*
  18. *
  19. * read as many frames as possible from the read buffer;
  20. *
  21. *--------------------------------------------------------------------*/
  22. function receive frame do allocate an input buffer;
  23. if no memory available then return failure;
  24. end if;
  25. read read space available register;
  26. if read buffer is empty then return failure;
  27. end if;
  28. write frame length into buffer size register;
  29. if mode is legacy then send read command;
  30. end if;
  31. while read buffer is not empty do read read buffer into frame buffer;
  32. extract Ethernet frame from frame buffer;
  33. update read statistics;
  34. end while;
  35. return (success);
  36. end function;
  37. /*====================================================================*
  38. *
  39. * state machine to manage QCA7000 SPI slave synchronization;
  40. *
  41. *--------------------------------------------------------------------*/
  42. function synchronize SPI slave do allocate a static reset counter;
  43. if synchronization state is CPU on then read QCA7000 signature register;
  44. read QCA7000 signature register;
  45. if signature is invalid then set synchronization state to unknown;
  46. else read SPI write buffer space available register;
  47. if write buffer is empty then qca->SynchState = QCASPI_SYNC_READY;
  48. set synchronization state to ready;
  49. return;
  50. else set synchronization state to unknown;
  51. end if;
  52. end if;
  53. end if;
  54. if synchronization stats is ready then if mode is legacy then return;
  55. end if;
  56. read QCA7000 signature register;
  57. if signature is invalid then set synchronization state to unknown;
  58. return;
  59. end if;
  60. end if;
  61. if synchronization state is unknown then if mode is legacy then use GPIO to reset QCA7000;
  62. else read QCA7000 signature register;
  63. if signature is invalid then return;
  64. end if;
  65. set soc_core_reset bit in QCA SPI configuration register;
  66. end if set synchronization state to reset;
  67. clear reset counter;
  68. return;
  69. end if;
  70. if synchronization state is reset then increment reset counter;
  71. if reset counter exceeds reset limit then set synchronization state to unknown;
  72. end if;
  73. end if;
  74. return;
  75. end function;
  76. /*====================================================================*
  77. *
  78. * handle QCA7000 interrupts and transmit requests; interrupts are
  79. * from the QCA7000; transmit requests are from the host computer;
  80. *
  81. *--------------------------------------------------------------------*/
  82. function interrupt serivce routine do while terminate is false do set thread state to interruptable;
  83. if no interrupts and synchronization state is synchronized and transmit queue is empty then allow other tasks to run;
  84. end if set thread state to busy;
  85. check synchronization state;
  86. if syncrhonization state is not synchronized then flush transmit queue;
  87. suspend for a while;
  88. end if;
  89. if interrupt occurred then disable SPI interrupts;
  90. read SPI interrupt cause register;
  91. if SPI interrupt cause is CPU on then update synchronization state;
  92. if synchronization state is synchronized then continue;
  93. end if;
  94. end if;
  95. if SPI interrupt cause is packet available then if synchronization state is synchronized then call receive frame function;
  96. end if;
  97. end if;
  98. if SPI interrupt cause is read buffer error then set synchronization state to unknown;
  99. continue;
  100. end if;
  101. if SPI interrupt cause is write buffer error then set synchronization state to unknown;
  102. continue;
  103. end if;
  104. clear SPI interrupt cause register;
  105. clear SPI interrupt register;
  106. end if;
  107. if transmit queue is not empty then call transmit frame function;
  108. end if;
  109. end while;
  110. set thread state to dormant;
  111. return;
  112. end function;
  113. /*====================================================================*
  114. *
  115. * basic interrupt handler; count interrupts since last service;
  116. * wake up dormant service routine;
  117. *
  118. *--------------------------------------------------------------------*/
  119. function interrupt handler do increment interrupt count;
  120. if thread is available and thread is dormant then wake up thread to service interrupt;
  121. end if;
  122. return (success);
  123. end function;