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.

133 lines
3.3KB

  1. #include <stdlib.h>
  2. #include <stdint.h>
  3. #include <string.h>
  4. #include <sys/types.h>
  5. #include <fcntl.h>
  6. #include <sys/stat.h>
  7. #include <linux/spi/spidev.h>
  8. #include "spi.h"
  9. #include "servo.h"
  10. static struct spi_message spi_message;
  11. static struct spi_result spi_result;
  12. static int spi_fd;
  13. static uint8_t spi_mode;
  14. static uint32_t spi_speed;
  15. void
  16. spi_open(uint8_t mode, uint32_t speed) {
  17. spi_fd=open("/dev/spidev1.0",O_RDWR);
  18. if(spi_fd==-1) {
  19. perror("spidev1.0");
  20. exit(20);
  21. }
  22. }
  23. void
  24. spi_close() {
  25. close(spi_fd);
  26. }
  27. void
  28. spi_update_servos() {
  29. int i;
  30. spi_result.status=SPI_BUSY; // assume busy
  31. while(spi_result.status) {
  32. if((spi_result.status==SPI_ERR)||(spi_result.status=SPI_DONE)) {
  33. spi_message.cmd=CMD_FINISH;
  34. write(spi_fd,&spi_message,1);
  35. } else if((spi_result.status==SPI_WAIT)) {
  36. spi_message.cmd=CMD_CONTINUE;
  37. write(spi_fd,&spi_message,1);
  38. }
  39. read(spi_fd,&spi_result,1);
  40. }
  41. spi_message.cmd=CMD_SET_SERVOS;
  42. spi_message.length=sizeof(servo_pwm);
  43. memcpy(spi_message.data,servo_pwm,sizeof(servo_pwm));
  44. spi_message.checksum=0x00;
  45. for(i=0;i<spi_message.length;i++) {
  46. spi_message.checksum^=spi_message.data[i];
  47. }
  48. write(spi_fd,&spi_message,sizeof(spi_message));
  49. while(spi_result.status!=SPI_WAIT) read(spi_fd,&spi_result,1);
  50. spi_message.cmd=CMD_CONTINUE;
  51. write(spi_fd,&spi_message,1);
  52. read(spi_fd,&spi_result,1);
  53. while(spi_result.status==SPI_BUSY) read(spi_fd,&spi_result,1);
  54. }
  55. void
  56. spi_set_led(uint8_t led) {
  57. int i;
  58. spi_result.status=SPI_BUSY; // assume busy
  59. while(spi_result.status) {
  60. if((spi_result.status==SPI_ERR)||(spi_result.status=SPI_DONE)) {
  61. spi_message.cmd=CMD_FINISH;
  62. write(spi_fd,&spi_message,1);
  63. } else if((spi_result.status==SPI_WAIT)) {
  64. spi_message.cmd=CMD_CONTINUE;
  65. write(spi_fd,&spi_message,1);
  66. }
  67. read(spi_fd,&spi_result,1);
  68. }
  69. spi_message.cmd=CMD_SET_LED;
  70. spi_message.length=1;
  71. spi_message.data[0]=led;
  72. spi_message.checksum=0x00;
  73. for(i=0;i<spi_message.length;i++) {
  74. spi_message.checksum^=spi_message.data[i];
  75. }
  76. write(spi_fd,&spi_message,sizeof(spi_message));
  77. while(spi_result.status!=SPI_WAIT) read(spi_fd,&spi_result,1);
  78. spi_message.cmd=CMD_CONTINUE;
  79. write(spi_fd,&spi_message,1);
  80. read(spi_fd,&spi_result,1);
  81. while(spi_result.status==SPI_BUSY) read(spi_fd,&spi_result,1);
  82. }
  83. void
  84. spi_set_laser(uint8_t led) {
  85. int i;
  86. spi_result.status=SPI_BUSY; // assume busy
  87. while(spi_result.status) {
  88. if((spi_result.status==SPI_ERR)||(spi_result.status=SPI_DONE)) {
  89. spi_message.cmd=CMD_FINISH;
  90. write(spi_fd,&spi_message,1);
  91. } else if((spi_result.status==SPI_WAIT)) {
  92. spi_message.cmd=CMD_CONTINUE;
  93. write(spi_fd,&spi_message,1);
  94. }
  95. read(spi_fd,&spi_result,1);
  96. }
  97. spi_message.cmd=CMD_SET_LASER;
  98. spi_message.length=1;
  99. spi_message.data[0]=led;
  100. spi_message.checksum=0x00;
  101. for(i=0;i<spi_message.length;i++) {
  102. spi_message.checksum^=spi_message.data[i];
  103. }
  104. write(spi_fd,&spi_message,sizeof(spi_message));
  105. while(spi_result.status!=SPI_WAIT) read(spi_fd,&spi_result,1);
  106. spi_message.cmd=CMD_CONTINUE;
  107. write(spi_fd,&spi_message,1);
  108. read(spi_fd,&spi_result,1);
  109. while(spi_result.status==SPI_BUSY) read(spi_fd,&spi_result,1);
  110. }