PiGPIO and SPI - Need a bit of help using the pipes - Raspberry Pi Forums
over last day or 2 i've been trying redo of program remove need run sudo. joan , jojopi, able learn how communicate on pipes control servo. now, i'm trying use pipes control spi.
code seems working, though haven't stress tested requires sudo i'd move way it. important thing note though, implies spi_channel, spi_speed, , spi_mode correct (0, 2 million, , 0 respectively): when running code, following output:
spi handle: 0
spi read: ffc0
spi read: ffc0
i'm reading 12-bit adc left adjusted output, , pin pulled vcc resistor, expected result.
i'm trying use pipes control adc on spi port, , i'm running trouble. here's code pipe control: here, typical output is:
spi handle: 0
spi read: 0 0
spi read: 0 0
if remember correctly, running program second time results in either failure open pipes or failure obtain handle. don't recall one, restarting pigpiod fixes it.
read in thread if issue "pigs spix 0 0x01 0x80 0x00 | od -x" command (that memory, might bit off), response should formatted in hexadecimal format. i'm not sure format should trying read in code above. i've tried few different things, nothing seems working.
code seems working, though haven't stress tested requires sudo i'd move way it. important thing note though, implies spi_channel, spi_speed, , spi_mode correct (0, 2 million, , 0 respectively):
code: select all
int result = 0; char write_cmd[2] = { 0 }; char read_data[2]; gpioinitialise(); g_spi_handle = spiopen(spi_channel, spi_speed, spi_mode); printf("spi handle: %d\n", g_spi_handle); // send command start conversion of channel 0 have // valid data when enter service routine spixfer(g_spi_handle, (char*)&write_cmd, (char*)&read_data, sizeof(read_data)); printf("spi read: %02x%02x\n", read_data[0], read_data[1]); usleep(200); spixfer(g_spi_handle, (char*)&write_cmd, (char*)&read_data, sizeof(read_data)); printf("spi read: %02x%02x\n", read_data[0], read_data[1]); spiclose(g_spi_handle);
spi handle: 0
spi read: ffc0
spi read: ffc0
i'm reading 12-bit adc left adjusted output, , pin pulled vcc resistor, expected result.
i'm trying use pipes control adc on spi port, , i'm running trouble. here's code pipe control:
code: select all
file *pigpio_write; file *pigpio_read; int pigpio_handle = -1; int pigpio_response[2]; int result = 0; printf("initializing tlc1543\n"); pigpio_write = fopen("/dev/pigpio", "w"); pigpio_read = fopen("/dev/pigout", "r"); if ((pigpio_write == null) || (pigpio_read == null)) { printf("error opening pipes: %s.%d\n", __file__, __line__); result = -1; } else { fprintf(pigpio_write, "spio %d %d %d\n", spi_channel, spi_speed, spi_mode); fflush(pigpio_write); fscanf(pigpio_read, "%d", &pigpio_handle); if (pigpio_handle < 0) { printf("error retrieving handle: %d\n", pigpio_handle); result = -1; } else { printf("spi handle: %d\n", pigpio_handle); fprintf(pigpio_write, "spix %u 0x00 0x00\n", pigpio_handle); fflush(pigpio_write); fscanf(pigpio_read, "%d%d", &pigpio_response[0], &pigpio_response[1]); printf("spi read: %d %d\n", pigpio_response[0], pigpio_response[1]); usleep(200); fprintf(pigpio_write, "spix %u 0x00 0x00\n", pigpio_handle); fflush(pigpio_write); fscanf(pigpio_read, "%d%d", &pigpio_response[0], &pigpio_response[1]); printf("spi read: %d %d\n", pigpio_response[0], pigpio_response[1]); fprintf(pigpio_write, "spic %d\n", pigpio_handle); fflush(pigpio_write); } } if (pigpio_write) fclose(pigpio_write); if (pigpio_read) fclose(pigpio_read); return result;
spi handle: 0
spi read: 0 0
spi read: 0 0
if remember correctly, running program second time results in either failure open pipes or failure obtain handle. don't recall one, restarting pigpiod fixes it.
read in thread if issue "pigs spix 0 0x01 0x80 0x00 | od -x" command (that memory, might bit off), response should formatted in hexadecimal format. i'm not sure format should trying read in code above. i've tried few different things, nothing seems working.
think i'm getting closer. i'm doing now:
first read returns 0x0a30 , second read returns 0xffc0 or 0xff80. not quite sure why first 1 returning 0xa30 sending 0x00 0x00 should cause adc convert channel 1. expect every read return near 0xffc0 there isn't should causing adc forget value.
might have right way though. need implement , see how reads every few milliseconds.
i'm open suggestions too, way make file read timeout in case pigpiod crashes. don't want threads hung up.
code: select all
printf("spi handle: %d\n", pigpio_handle); fprintf(pigpio_write, "spix %u 0x00 0x00\n", pigpio_handle); fflush(pigpio_write); fscanf(pigpio_read, "%c%c%d", &data_read[0], &data_read[1], &pigpio_response); printf("spi read: %02x%02x %d\n", data_read[0], data_read[1], pigpio_response); usleep(200); fprintf(pigpio_write, "spix %u 0x00 0x00\n", pigpio_handle); fflush(pigpio_write); fscanf(pigpio_read, "%c%c%d", &data_read[0], &data_read[1], &pigpio_response); printf("spi read: %02x%02x %d\n", data_read[0], data_read[1], pigpio_response); fprintf(pigpio_write, "spic %d\n", pigpio_handle); fflush(pigpio_write);
might have right way though. need implement , see how reads every few milliseconds.
i'm open suggestions too, way make file read timeout in case pigpiod crashes. don't want threads hung up.
raspberrypi
Comments
Post a Comment