Unit test supporting 5 SPI CS channels

This commit is contained in:
mstroh
2026-01-17 19:45:30 +01:00
committed by GitHub
parent 4d74f33200
commit 111f4f36d1

View File

@@ -1,4 +1,4 @@
// WiringPi test program: SPI functions (need XO hardware) // WiringPi test program: SPI functions (need XO hardware)
// Compile: gcc -Wall wiringpi_xotest_test1_spi.c -o wiringpi_xotest_test1_spi -lwiringPi // Compile: gcc -Wall wiringpi_xotest_test1_spi.c -o wiringpi_xotest_test1_spi -lwiringPi
#include <unistd.h> #include <unistd.h>
@@ -65,17 +65,19 @@ int main(int argc, char *argv []){
wiringPiSetup(); wiringPiSetup();
if ((hSPI = wiringPiSPISetup (spiChannel, spiSpeed)) < 0) { printf("wiringPiSPISetup (spiChannel=%d, spiSpeed=%d)\n",spiChannel, spiSpeed);
if ((hSPI = wiringPiSPISetup(spiChannel, spiSpeed)) < 0) {
FailAndExitWithErrno("wiringPiSPISetup", hSPI); FailAndExitWithErrno("wiringPiSPISetup", hSPI);
} }
int hSPIOld=hSPI; int hSPIOld=hSPI;
//printf("\nSPI fd = %d\n call close now\n", hSPI); printf("wiringPiSPIClose(spiChannel=%d)\n",spiChannel);
int ret = wiringPiSPIClose(spiChannel); int ret = wiringPiSPIClose(spiChannel);
if (ret!=0) { if (ret!=0) {
FailAndExitWithErrno("wiringPiSPIClose", ret); FailAndExitWithErrno("wiringPiSPIClose", ret);
} }
printf("wiringPiSPIxSetupMode(0, spiChannel=%d, spiSpeed=%d, 0)\n",spiChannel, spiSpeed);
if ((hSPI = wiringPiSPIxSetupMode(0, spiChannel, spiSpeed, 0)) < 0) { if ((hSPI = wiringPiSPIxSetupMode(0, spiChannel, spiSpeed, 0)) < 0) {
FailAndExitWithErrno("wiringPiSPIxSetup", hSPI); FailAndExitWithErrno("wiringPiSPIxSetup", hSPI);
} }
@@ -150,12 +152,18 @@ int main(int argc, char *argv []){
digitalWriteEx(24, GPIOIn, HIGH); digitalWriteEx(24, GPIOIn, HIGH);
checkVoltage(3.3f, "Analog value 6xHigh"); checkVoltage(3.3f, "Analog value 6xHigh");
CH1 = AnalogRead(3, 1, &returnvalue);
CheckSame("\nReading Wrong channel 3 result ", CH1, 0); int unsupportedSPICh = 5;
CheckSame("\nReading Wrong channel 3 ioctl result ", returnvalue, -EINVAL); printf("\nCheck error for unsupported channel %d\n", unsupportedSPICh);
CH1 = AnalogRead(2, 1, &returnvalue); CH1 = AnalogRead(unsupportedSPICh, 1, &returnvalue);
CheckSame("\nReading Wrong channel 2 result ", CH1, 0); CheckSame("\nReading unsupported channel result ", CH1, 0);
CheckSame("\nReading Wrong channel 3 ioctl result ", returnvalue, -EBADF); CheckSame("Reading unsupported channel ioctl result ", returnvalue, -EINVAL);
int uninitSPICh = 2;
printf("\nCheck error for uninitialized channel %d\n", uninitSPICh);
CH1 = AnalogRead(uninitSPICh, 1, &returnvalue);
CheckSame("\nReading uninitialized channel result ", CH1, 0);
CheckSame("Reading uninitialized channel ioctl result ", returnvalue, -EBADF);
pinMode(22, INPUT); pinMode(22, INPUT);
pinMode(21, INPUT); pinMode(21, INPUT);
@@ -164,6 +172,7 @@ int main(int argc, char *argv []){
pinMode(27, INPUT); pinMode(27, INPUT);
pinMode(28, INPUT); pinMode(28, INPUT);
printf("\nwiringPiSPIxClose(0, spiChannel=%d)\n",spiChannel);
ret = wiringPiSPIxClose(0, spiChannel); ret = wiringPiSPIxClose(0, spiChannel);
CheckSame("wiringPiSPIxClose result", ret, 0); CheckSame("wiringPiSPIxClose result", ret, 0);
if (ret!=0) { if (ret!=0) {
@@ -174,4 +183,3 @@ int main(int argc, char *argv []){
return UnitTestState(); return UnitTestState();
} }