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.
 
 
 
 
 

113 lines
3.4 KiB

  1. // WiringPi test program: SPI functions (need MCP3202 hardware @ CE1, ch0=Vdd, ch1=Vdd/2)
  2. // Compile: gcc -Wall wiringpi_spi_test1_mcp3202.c -o wiringpi_spi_test1_mcp3202 -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. const float fRefVoltage = 3.3f;
  10. const float fResolution = 4096; //12-Bit
  11. const int spiChannel = 1;
  12. const int spiSpeedInit = 250000; // Hz
  13. int AnalogRead(int spiChannel, int analogChannel, int* returnvalue) {
  14. if (analogChannel<0 || analogChannel>1) {
  15. return -1;
  16. }
  17. unsigned char spiData[3];
  18. unsigned char chanBits;
  19. if (analogChannel == 0) {
  20. chanBits = 0b11010000;
  21. } else {
  22. chanBits = 0b11110000;
  23. }
  24. spiData[0] = chanBits;
  25. spiData[1] = 0;
  26. spiData[2] = 0;
  27. *returnvalue = wiringPiSPIxDataRW(0, spiChannel, spiData, 3);
  28. return ((spiData [0] << 9) | (spiData [1] << 1) | (spiData[2] >> 7)) & 0xFFF;
  29. }
  30. int main(int argc, char *argv []){
  31. int hSPI;
  32. int CH0,CH1;
  33. float value0, value1;
  34. int returnvalue;
  35. int spiSpeed;
  36. int major, minor;
  37. wiringPiVersion(&major, &minor);
  38. printf("Testing SPI functions with WiringPi %d.%d\n",major, minor);
  39. printf("------------------------------------------\n\n");
  40. wiringPiSetup();
  41. spiSpeed = spiSpeedInit;
  42. for (int testno=0; testno<=2; testno++) {
  43. printf("\nTest 5d with %g MHz SPI Speed\n", spiSpeed/1000000.0f);
  44. if ((hSPI = wiringPiSPISetup (spiChannel, spiSpeed)) < 0) {
  45. FailAndExitWithErrno("wiringPiSPISetup", hSPI);
  46. }
  47. int hSPIOld=hSPI;
  48. //printf("\nSPI fd = %d\n call close now\n", hSPI);
  49. int ret = wiringPiSPIClose(spiChannel);
  50. if (ret!=0) {
  51. FailAndExitWithErrno("wiringPiSPIClose", ret);
  52. }
  53. if ((hSPI = wiringPiSPIxSetupMode(0, spiChannel, spiSpeed, 0)) < 0) {
  54. FailAndExitWithErrno("wiringPiSPIxSetup", hSPI);
  55. }
  56. CheckSame("SPISetup, Close and SPIxSetup handle", hSPI, hSPIOld);
  57. delayMicroseconds(500000);
  58. returnvalue = -1;
  59. CH0 = AnalogRead(spiChannel, 0, &returnvalue);
  60. CheckSame("CH0 wiringPiSPIxDataRW return", returnvalue, 3);
  61. value0 = CH0 * fRefVoltage / fResolution;
  62. CheckSameFloat("CH0 value (VDD)", value0, 3.3f);
  63. printf("\n");
  64. delayMicroseconds(500000);
  65. returnvalue = -1;
  66. CH1 = AnalogRead(spiChannel, 1, &returnvalue);
  67. CheckSame("CH1 wiringPiSPIxDataRW return", returnvalue, 3);
  68. value1 = CH1 * fRefVoltage / fResolution;
  69. CheckSameFloat("CH1 value (1/2)", value1, 1.65f);
  70. printf("\n");
  71. ret = wiringPiSPIxClose(0, spiChannel);
  72. CheckSame("wiringPiSPIxClose result", ret, 0);
  73. if (ret!=0) {
  74. FailAndExitWithErrno("wiringPiSPIxClose", ret);
  75. }
  76. ret = wiringPiSPIxGetFd(0, spiChannel);
  77. CheckSame("Fd after wiringPiSPIxGetFd", ret, -1);
  78. ret = wiringPiSPIGetFd(spiChannel);
  79. CheckSame("Fd after wiringPiSPIGetFd", ret, -1);
  80. spiSpeed += spiSpeed;
  81. }
  82. printf("\n");
  83. hSPI = wiringPiSPISetup (3, spiSpeedInit);
  84. CheckSame("\nwiringPiSPISetup with wrong channel", hSPI, -22);
  85. // will result in exit! - not useful in test code
  86. //hSPI = wiringPiSPIxSetupMode (3, 0, spiSpeedInit,0);
  87. //CheckSame("\nwiringPiSPISetup with wrong channel", hSPI, -22);
  88. return UnitTestState();
  89. }