Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 

179 linhas
5.1 KiB

  1. // WiringPi test program: SPI functions (need XO hardware)
  2. // Compile: gcc -Wall wiringpi_xotest_test1_spi.c -o wiringpi_xotest_test1_spi -lwiringPi
  3. #include <unistd.h>
  4. #include <stdint.h>
  5. #include <signal.h>
  6. #include <time.h>
  7. #include "wpi_test.h"
  8. #include <wiringPiSPI.h>
  9. #define TRUE (1==1)
  10. #define FALSE (!TRUE)
  11. #define CHAN_CONFIG_SINGLE 8
  12. #define CHAN_CONFIG_DIFF 0
  13. const float fRefVoltage = 3.3f;
  14. const float fResolution = 4096; //12-Bit
  15. const int spiChannel = 1;
  16. const int spiSpeed = 1000000; // MHz
  17. int AnalogRead(int spiChannel, int analogChannel, int* returnvalue) {
  18. if (analogChannel<0 || analogChannel>1) {
  19. return -1;
  20. }
  21. unsigned char spiData[3];
  22. unsigned char chanBits;
  23. if (analogChannel == 0) {
  24. chanBits = 0b11010000;
  25. } else {
  26. chanBits = 0b11110000;
  27. }
  28. spiData[0] = chanBits;
  29. spiData[1] = 0;
  30. spiData[2] = 0;
  31. *returnvalue = wiringPiSPIxDataRW(0, spiChannel, spiData, 3);
  32. return ((spiData [0] << 9) | (spiData [1] << 1) | (spiData[2] >> 7)) & 0xFFF;
  33. }
  34. void checkVoltage(float expect, const char* szexpect) {
  35. int returnvalue;
  36. //int CH0 = AnalogRead(spiChannel, 0, &returnvalue);
  37. int CH1 = AnalogRead(spiChannel, 1, &returnvalue);
  38. //float value0 = CH0 * fRefVoltage / fResolution;
  39. float value1 = CH1 * fRefVoltage / fResolution;
  40. CheckSameFloat(szexpect, value1, expect);
  41. delayMicroseconds(300);
  42. }
  43. int main(int argc, char *argv []){
  44. const int GPIOIn = 29;
  45. int hSPI;
  46. //int CH0;
  47. int CH1;
  48. int major, minor;
  49. wiringPiVersion(&major, &minor);
  50. printf("Testing SPI functions with WiringPi %d.%d\n",major, minor);
  51. printf("------------------------------------------\n\n");
  52. wiringPiSetup();
  53. if ((hSPI = wiringPiSPISetup (spiChannel, spiSpeed)) < 0) {
  54. FailAndExitWithErrno("wiringPiSPISetup", hSPI);
  55. }
  56. int hSPIOld=hSPI;
  57. //printf("\nSPI fd = %d\n call close now\n", hSPI);
  58. int ret = wiringPiSPIClose(spiChannel);
  59. if (ret!=0) {
  60. FailAndExitWithErrno("wiringPiSPIClose", ret);
  61. }
  62. if ((hSPI = wiringPiSPIxSetupMode(0, spiChannel, spiSpeed, 0)) < 0) {
  63. FailAndExitWithErrno("wiringPiSPIxSetup", hSPI);
  64. }
  65. CheckSame("SPISetup, Close and SPIxSetup handle", hSPI, hSPIOld);
  66. int returnvalue;
  67. //CH0 = AnalogRead(spiChannel, 0, &returnvalue);
  68. CH1 = AnalogRead(spiChannel, 1, &returnvalue);
  69. CheckSame("SPI reading ioctl result (byte count) ", returnvalue, 3);
  70. //float value0 = CH0 * fRefVoltage / fResolution;
  71. //float value1 = CH1 * fRefVoltage / fResolution;
  72. pinMode(21, OUTPUT);
  73. pinMode(22, INPUT);
  74. pinMode(24, INPUT);
  75. pinMode(25, INPUT);
  76. pinMode(27, INPUT);
  77. pinMode(28, INPUT);
  78. pinMode(GPIOIn, INPUT);
  79. digitalWriteEx(21, GPIOIn, LOW);
  80. checkVoltage(0.1f, "Analog value 1xLow");
  81. checkVoltage(0.1f, "Analog value 1xLow");
  82. digitalWriteEx(21, GPIOIn, HIGH);
  83. checkVoltage(3.1f, "Analog value 1xHigh");
  84. pinMode(22, OUTPUT);
  85. digitalWriteEx(22, -1, LOW);
  86. checkVoltage(1.65f, "Analog value Half (1H/1L)");
  87. digitalWriteEx(22, GPIOIn, HIGH);
  88. checkVoltage(3.2f, "Analog value 2xHigh");
  89. pinMode(24, OUTPUT);
  90. digitalWriteEx(24, GPIOIn, HIGH);
  91. checkVoltage(3.2f, "Analog value 3xHigh");
  92. digitalWriteEx(24, -1, LOW);
  93. checkVoltage(2.2f, "Analog value 2xHigh/1xLow");
  94. checkVoltage(2.2f, "Analog value 2xHigh/1xLow");
  95. pinMode(25, OUTPUT);
  96. digitalWriteEx(25, GPIOIn, HIGH);
  97. checkVoltage(2.475f, "Analog value 3xHigh/1xLow");
  98. digitalWriteEx(25, -1, LOW);
  99. checkVoltage(1.65f, "Analog value Half (2H/2L)");
  100. pinMode(27, OUTPUT);
  101. digitalWriteEx(27, GPIOIn, HIGH);
  102. checkVoltage(1.98f, "Analog value 3xHigh/2xLow");
  103. digitalWriteEx(27, -1, LOW);
  104. checkVoltage(1.32f, "Analog value Half (2H/3L)");
  105. pinMode(28, OUTPUT);
  106. digitalWriteEx(28, GPIOIn, LOW);
  107. checkVoltage(1.100f, "Analog value 2xHigh/4xLow");
  108. digitalWriteEx(28, GPIOIn, HIGH);
  109. checkVoltage(1.65f, "Analog value Half (3H/3L)");
  110. digitalWriteEx(27, GPIOIn, HIGH);
  111. checkVoltage(2.2f, "Analog value 4xHigh/2xLow");
  112. digitalWriteEx(25, GPIOIn, HIGH);
  113. checkVoltage(2.75f, "Analog value 5xHigh/1xLow");
  114. digitalWriteEx(24, GPIOIn, HIGH);
  115. checkVoltage(3.3f, "Analog value 6xHigh");
  116. CH1 = AnalogRead(3, 1, &returnvalue);
  117. CheckSame("\nReading Wrong channel 3 result ", CH1, 0);
  118. CheckSame("\nReading Wrong channel 3 ioctl result ", returnvalue, -EINVAL);
  119. CH1 = AnalogRead(2, 1, &returnvalue);
  120. CheckSame("\nReading Wrong channel 2 result ", CH1, 0);
  121. CheckSame("\nReading Wrong channel 3 ioctl result ", returnvalue, -EBADF);
  122. pinMode(22, INPUT);
  123. pinMode(21, INPUT);
  124. pinMode(24, INPUT);
  125. pinMode(25, INPUT);
  126. pinMode(27, INPUT);
  127. pinMode(28, INPUT);
  128. ret = wiringPiSPIxClose(0, spiChannel);
  129. CheckSame("wiringPiSPIxClose result", ret, 0);
  130. if (ret!=0) {
  131. FailAndExitWithErrno("wiringPiSPIxClose", ret);
  132. }
  133. ret = wiringPiSPIxGetFd(0, spiChannel);
  134. CheckSame("Fd after close", ret, -1);
  135. return UnitTestState();
  136. }