You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 lines
3.9KB

  1. #define SPI_IDLE 0x00
  2. #define SPI_WAIT 0x01
  3. #define SPI_BUSY 0x02
  4. #define SPI_DONE 0x03
  5. #define SPI_ERR 0x04
  6. #define CMD_NOP 0x00
  7. #define CMD_CONTINUE 0x01
  8. #define CMD_FINISH 0x02
  9. #define CMD_CLEAR 0x8A
  10. #define CMD_SET_SERVOS 0x10
  11. #define CMD_SET_LED 0x11
  12. #define CMD_SET_LASER 0x12
  13. #define CMD_GET_SERVOS 0x20
  14. struct spi_buffer {
  15. unsigned int cmd;
  16. unsigned int length;
  17. unsigned int checksum;
  18. unsigned int data[255];
  19. };
  20. volatile unsigned int *pwm=(unsigned int *)0x40000000;
  21. volatile struct spi_buffer *spi_buffer=(struct spi_buffer *)0x50000000;
  22. volatile unsigned int *gpio_set=(unsigned int *)0x20000030;
  23. volatile unsigned int *gpio_clear=(unsigned int *)0x20000040;
  24. volatile unsigned int *gpio_in=(unsigned int *)0x20000050;
  25. volatile unsigned int *counter=(unsigned int *)0x20000060;
  26. int main() {
  27. int i,length;
  28. unsigned char cmd;
  29. unsigned char checksum;
  30. unsigned char led=0, pulsing_led=0;
  31. for(i=0;i<24;i++) pwm[i]=0;
  32. while(1) {
  33. if(((*counter)>>17)&0x100) {
  34. pulsing_led=((*counter)>>18)&0x7f;
  35. } else {
  36. pulsing_led=0x7f-(((*counter)>>18)&0xff);
  37. }
  38. if(led>((*counter)&0xff)) { *gpio_set=0x01; } else { *gpio_clear=0x01; }
  39. if(pulsing_led>((*counter)&0xff)) { *gpio_set=0x02; } else { *gpio_clear=0x02; }
  40. cmd=spi_buffer->cmd;
  41. switch(cmd) {
  42. case CMD_FINISH:
  43. spi_buffer->cmd=SPI_IDLE;
  44. break;
  45. case CMD_CLEAR:
  46. spi_buffer->cmd=SPI_BUSY;
  47. spi_buffer->length=0x00;
  48. spi_buffer->checksum=0x00;
  49. for(i=1;i<2000;i++) {
  50. spi_buffer->data[i]=0x00;
  51. }
  52. for(i=0;i<24;i++) {
  53. pwm[i]=0;
  54. }
  55. spi_buffer->cmd=SPI_IDLE;
  56. break;
  57. case CMD_SET_SERVOS:
  58. spi_buffer->cmd=SPI_WAIT;
  59. while(spi_buffer->cmd!=CMD_CONTINUE);
  60. spi_buffer->cmd=SPI_BUSY;
  61. checksum=0;
  62. length=spi_buffer->length;
  63. for(i=0;i<length;i++) {
  64. checksum^=spi_buffer->data[i];
  65. }
  66. if(checksum != spi_buffer->checksum ) {
  67. spi_buffer->cmd=SPI_ERR;
  68. } else {
  69. for(i=0;i<24;i++) {
  70. pwm[i]=(spi_buffer->data[4*i+1]<<8)|spi_buffer->data[4*i];
  71. }
  72. spi_buffer->cmd=SPI_DONE;
  73. }
  74. break;
  75. case CMD_SET_LED:
  76. spi_buffer->cmd=SPI_WAIT;
  77. while(spi_buffer->cmd!=CMD_CONTINUE);
  78. spi_buffer->cmd=SPI_BUSY;
  79. checksum=0;
  80. length=spi_buffer->length;
  81. for(i=0;i<length;i++) {
  82. checksum^=spi_buffer->data[i];
  83. }
  84. if(checksum != spi_buffer->checksum ) {
  85. spi_buffer->cmd=SPI_ERR;
  86. } else {
  87. led=spi_buffer->data[0];
  88. spi_buffer->cmd=SPI_DONE;
  89. }
  90. case CMD_SET_LASER:
  91. spi_buffer->cmd=SPI_WAIT;
  92. while(spi_buffer->cmd!=CMD_CONTINUE);
  93. spi_buffer->cmd=SPI_BUSY;
  94. checksum=0;
  95. length=spi_buffer->length;
  96. for(i=0;i<length;i++) {
  97. checksum^=spi_buffer->data[i];
  98. }
  99. if(checksum != spi_buffer->checksum ) {
  100. spi_buffer->cmd=SPI_ERR;
  101. } else {
  102. if(spi_buffer->data[0]) {
  103. *gpio_set=0x04;
  104. } else {
  105. *gpio_clear=0x04;
  106. }
  107. spi_buffer->cmd=SPI_DONE;
  108. }
  109. /* case CMD_GET_SERVOS:
  110. spi_buffer->cmd=SPI_BUSY;
  111. spi_buffer->length=0x00;
  112. spi_buffer->checksum=0x00;
  113. for(i=0;i<24;i++) {
  114. spi_buffer->data[2*i]=pwm[i];
  115. spi_buffer->data[2*i+1]=pwm[i]>>8;
  116. }
  117. spi_buffer->cmd=SPI_DONE;
  118. break;
  119. */
  120. default:
  121. break;
  122. }
  123. }
  124. return 0;
  125. }