49 #include <sys/ioctl.h> 54 #include <sys/types.h> 57 #include <sys/socket.h> 58 #include <sys/sysmacros.h> 59 #include <netinet/tcp.h> 60 #include <arpa/inet.h> 61 #include <sys/select.h> 191 #define THOUSAND 1000 192 #define MILLION 1000000 193 #define BILLION 1000000000 195 #define BANK (gpio>>5) 197 #define BIT (1<<(gpio&0x1F)) 199 #ifndef EMBEDDED_IN_VM 200 #define DBG(level, format, arg...) DO_DBG(level, format, ## arg) 202 #define DBG(level, format, arg...) 205 #define DO_DBG(level, format, arg...) \ 207 if ((gpioCfg.dbgLevel >= level) && \ 208 (!(gpioCfg.internals & PI_CFG_NOSIGHANDLER))) \ 209 fprintf(stderr, "%s %s: " format "\n" , \ 210 myTimeStamp(), __FUNCTION__ , ## arg); \ 213 #ifndef DISABLE_SER_CHECK_INITED 214 #define SER_CHECK_INITED CHECK_INITED 216 #define SER_CHECK_INITED 219 #define CHECK_INITED \ 222 if (!libInitialised) \ 225 "pigpio uninitialised, call gpioInitialise()"); \ 226 return PI_NOT_INITIALISED; \ 231 #define CHECK_INITED_RET_NULL_PTR \ 234 if (!libInitialised) \ 237 "pigpio uninitialised, call gpioInitialise()"); \ 243 #define CHECK_INITED_RET_NIL \ 246 if (!libInitialised) \ 249 "pigpio uninitialised, call gpioInitialise()"); \ 254 #define CHECK_NOT_INITED \ 257 if (libInitialised) \ 260 "pigpio initialised, call gpioTerminate()"); \ 261 return PI_INITIALISED; \ 266 #define SOFT_ERROR(x, format, arg...) \ 269 DBG(DBG_ALWAYS, format, ## arg); \ 274 #define TIMER_ADD(a, b, result) \ 277 (result)->tv_sec = (a)->tv_sec + (b)->tv_sec; \ 278 (result)->tv_nsec = (a)->tv_nsec + (b)->tv_nsec; \ 279 if ((result)->tv_nsec >= BILLION) \ 281 ++(result)->tv_sec; \ 282 (result)->tv_nsec -= BILLION; \ 287 #define TIMER_SUB(a, b, result) \ 290 (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \ 291 (result)->tv_nsec = (a)->tv_nsec - (b)->tv_nsec; \ 292 if ((result)->tv_nsec < 0) \ 294 --(result)->tv_sec; \ 295 (result)->tv_nsec += BILLION; \ 300 #define PI_PERI_BUS 0x7E000000 302 #define AUX_BASE (pi_peri_phys + 0x00215000) 303 #define BSCS_BASE (pi_peri_phys + 0x00214000) 304 #define CLK_BASE (pi_peri_phys + 0x00101000) 305 #define DMA_BASE (pi_peri_phys + 0x00007000) 306 #define DMA15_BASE (pi_peri_phys + 0x00E05000) 307 #define GPIO_BASE (pi_peri_phys + 0x00200000) 308 #define PADS_BASE (pi_peri_phys + 0x00100000) 309 #define PCM_BASE (pi_peri_phys + 0x00203000) 310 #define PWM_BASE (pi_peri_phys + 0x0020C000) 311 #define SPI_BASE (pi_peri_phys + 0x00204000) 312 #define SYST_BASE (pi_peri_phys + 0x00003000) 315 #define BSCS_LEN 0x40 317 #define DMA_LEN 0x1000 318 #define GPIO_LEN 0xB4 319 #define PADS_LEN 0x38 323 #define SYST_LEN 0x1C 325 #define DMA_ENABLE (0xFF0/4) 359 #define DMA_CONBLK_AD 1 363 #define DMA_CHANNEL_RESET (1<<31) 364 #define DMA_WAIT_ON_WRITES (1<<28) 365 #define DMA_PANIC_PRIORITY(x) ((x)<<20) 366 #define DMA_PRIORITY(x) ((x)<<16) 367 #define DMA_INTERRUPT_STATUS (1<< 2) 368 #define DMA_END_FLAG (1<< 1) 369 #define DMA_ACTIVATE (1<< 0) 372 #define DMA_NO_WIDE_BURSTS (1<<26) 373 #define DMA_PERIPHERAL_MAPPING(x) ((x)<<16) 374 #define DMA_BURST_LENGTH(x) ((x)<<12) 375 #define DMA_SRC_IGNORE (1<<11) 376 #define DMA_SRC_DREQ (1<<10) 377 #define DMA_SRC_WIDTH (1<< 9) 378 #define DMA_SRC_INC (1<< 8) 379 #define DMA_DEST_IGNORE (1<< 7) 380 #define DMA_DEST_DREQ (1<< 6) 381 #define DMA_DEST_WIDTH (1<< 5) 382 #define DMA_DEST_INC (1<< 4) 383 #define DMA_WAIT_RESP (1<< 3) 385 #define DMA_DEBUG_READ_ERR (1<<2) 386 #define DMA_DEBUG_FIFO_ERR (1<<1) 387 #define DMA_DEBUG_RD_LST_NOT_SET_ERR (1<<0) 389 #define DMA_LITE_FIRST 7 390 #define DMA_LITE_MAX 0xfffc 401 #define PWM_CTL_MSEN2 (1<<15) 402 #define PWM_CTL_PWEN2 (1<<8) 403 #define PWM_CTL_MSEN1 (1<<7) 404 #define PWM_CTL_CLRF1 (1<<6) 405 #define PWM_CTL_USEF1 (1<<5) 406 #define PWM_CTL_MODE1 (1<<1) 407 #define PWM_CTL_PWEN1 (1<<0) 409 #define PWM_DMAC_ENAB (1 <<31) 410 #define PWM_DMAC_PANIC(x) ((x)<< 8) 411 #define PWM_DMAC_DREQ(x) (x) 423 #define PCM_CS_STBY (1 <<25) 424 #define PCM_CS_SYNC (1 <<24) 425 #define PCM_CS_RXSEX (1 <<23) 426 #define PCM_CS_RXERR (1 <<16) 427 #define PCM_CS_TXERR (1 <<15) 428 #define PCM_CS_DMAEN (1 <<9) 429 #define PCM_CS_RXTHR(x) ((x)<<7) 430 #define PCM_CS_TXTHR(x) ((x)<<5) 431 #define PCM_CS_RXCLR (1 <<4) 432 #define PCM_CS_TXCLR (1 <<3) 433 #define PCM_CS_TXON (1 <<2) 434 #define PCM_CS_RXON (1 <<1) 435 #define PCM_CS_EN (1 <<0) 437 #define PCM_MODE_CLK_DIS (1 <<28) 438 #define PCM_MODE_PDMN (1 <<27) 439 #define PCM_MODE_PDME (1 <<26) 440 #define PCM_MODE_FRXP (1 <<25) 441 #define PCM_MODE_FTXP (1 <<24) 442 #define PCM_MODE_CLKM (1 <<23) 443 #define PCM_MODE_CLKI (1 <<22) 444 #define PCM_MODE_FSM (1 <<21) 445 #define PCM_MODE_FSI (1 <<20) 446 #define PCM_MODE_FLEN(x) ((x)<<10) 447 #define PCM_MODE_FSLEN(x) ((x)<< 0) 449 #define PCM_RXC_CH1WEX (1 <<31) 450 #define PCM_RXC_CH1EN (1 <<30) 451 #define PCM_RXC_CH1POS(x) ((x)<<20) 452 #define PCM_RXC_CH1WID(x) ((x)<<16) 453 #define PCM_RXC_CH2WEX (1 <<15) 454 #define PCM_RXC_CH2EN (1 <<14) 455 #define PCM_RXC_CH2POS(x) ((x)<< 4) 456 #define PCM_RXC_CH2WID(x) ((x)<< 0) 458 #define PCM_TXC_CH1WEX (1 <<31) 459 #define PCM_TXC_CH1EN (1 <<30) 460 #define PCM_TXC_CH1POS(x) ((x)<<20) 461 #define PCM_TXC_CH1WID(x) ((x)<<16) 462 #define PCM_TXC_CH2WEX (1 <<15) 463 #define PCM_TXC_CH2EN (1 <<14) 464 #define PCM_TXC_CH2POS(x) ((x)<< 4) 465 #define PCM_TXC_CH2WID(x) ((x)<< 0) 467 #define PCM_DREQ_TX_PANIC(x) ((x)<<24) 468 #define PCM_DREQ_RX_PANIC(x) ((x)<<16) 469 #define PCM_DREQ_TX_REQ_L(x) ((x)<< 8) 470 #define PCM_DREQ_RX_REQ_L(x) ((x)<< 0) 472 #define PCM_INTEN_RXERR (1<<3) 473 #define PCM_INTEN_TXERR (1<<2) 474 #define PCM_INTEN_RXR (1<<1) 475 #define PCM_INTEN_TXW (1<<0) 477 #define PCM_INTSTC_RXERR (1<<3) 478 #define PCM_INTSTC_TXERR (1<<2) 479 #define PCM_INTSTC_RXR (1<<1) 480 #define PCM_INTSTC_TXW (1<<0) 482 #define PCM_GRAY_FLUSH (1<<2) 483 #define PCM_GRAY_CLR (1<<1) 484 #define PCM_GRAY_EN (1<<0) 486 #define BCM_PASSWD (0x5A<<24) 488 #define CLK_CTL_MASH(x)((x)<<9) 489 #define CLK_CTL_BUSY (1 <<7) 490 #define CLK_CTL_KILL (1 <<5) 491 #define CLK_CTL_ENAB (1 <<4) 492 #define CLK_CTL_SRC(x) ((x)<<0) 496 #define CLK_CTL_SRC_OSC 1 497 #define CLK_CTL_SRC_PLLD 6 499 #define CLK_OSC_FREQ 19200000 500 #define CLK_PLLD_FREQ 500000000 502 #define CLK_DIV_DIVI(x) ((x)<<12) 503 #define CLK_DIV_DIVF(x) ((x)<< 0) 505 #define CLK_GP0_CTL 28 506 #define CLK_GP0_DIV 29 507 #define CLK_GP1_CTL 30 508 #define CLK_GP1_DIV 31 509 #define CLK_GP2_CTL 32 510 #define CLK_GP2_DIV 33 512 #define CLK_PCMCTL 38 513 #define CLK_PCMDIV 39 515 #define CLK_PWMCTL 40 516 #define CLK_PWMDIV 41 531 #define SPI_CS_LEN_LONG (1<<25) 532 #define SPI_CS_DMA_LEN (1<<24) 533 #define SPI_CS_CSPOLS(x) ((x)<<21) 534 #define SPI_CS_RXF (1<<20) 535 #define SPI_CS_RXR (1<<19) 536 #define SPI_CS_TXD (1<<18) 537 #define SPI_CS_RXD (1<<17) 538 #define SPI_CS_DONE (1<<16) 539 #define SPI_CS_LEN (1<<13) 540 #define SPI_CS_REN (1<<12) 541 #define SPI_CS_ADCS (1<<11) 542 #define SPI_CS_INTR (1<<10) 543 #define SPI_CS_INTD (1<<9) 544 #define SPI_CS_DMAEN (1<<8) 545 #define SPI_CS_TA (1<<7) 546 #define SPI_CS_CSPOL(x) ((x)<<6) 547 #define SPI_CS_CLEAR(x) ((x)<<4) 548 #define SPI_CS_MODE(x) ((x)<<2) 549 #define SPI_CS_CS(x) ((x)<<0) 551 #define SPI_DC_RPANIC(x) ((x)<<24) 552 #define SPI_DC_RDREQ(x) ((x)<<16) 553 #define SPI_DC_TPANIC(x) ((x)<<8) 554 #define SPI_DC_TDREQ(x) ((x)<<0) 569 #define PI_SPI_SCLK 11 570 #define PI_SPI_MISO 9 571 #define PI_SPI_MOSI 10 575 #define PI_ASPI_CE0 18 576 #define PI_ASPI_CE1 17 577 #define PI_ASPI_CE2 16 578 #define PI_ASPI_MISO 19 579 #define PI_ASPI_MOSI 20 580 #define PI_ASPI_SCLK 21 585 #define AUX_ENABLES 1 587 #define AUX_MU_IO_REG 16 588 #define AUX_MU_IER_REG 17 589 #define AUX_MU_IIR_REG 18 590 #define AUX_MU_LCR_REG 19 591 #define AUX_MU_MCR_REG 20 592 #define AUX_MU_LSR_REG 21 593 #define AUX_MU_MSR_REG 22 594 #define AUX_MU_SCRATCH 23 595 #define AUX_MU_CNTL_REG 24 596 #define AUX_MU_STAT_REG 25 597 #define AUX_MU_BAUD_REG 26 599 #define AUX_SPI0_CNTL0_REG 32 600 #define AUX_SPI0_CNTL1_REG 33 601 #define AUX_SPI0_STAT_REG 34 602 #define AUX_SPI0_PEEK_REG 35 604 #define AUX_SPI0_IO_REG 40 605 #define AUX_SPI0_TX_HOLD 44 607 #define AUX_SPI1_CNTL0_REG 48 608 #define AUX_SPI1_CNTL1_REG 49 609 #define AUX_SPI1_STAT_REG 50 610 #define AUX_SPI1_PEEK_REG 51 612 #define AUX_SPI1_IO_REG 56 613 #define AUX_SPI1_TX_HOLD 60 615 #define AUXENB_SPI2 (1<<2) 616 #define AUXENB_SPI1 (1<<1) 617 #define AUXENB_UART (1<<0) 619 #define AUXSPI_CNTL0_SPEED(x) ((x)<<20) 620 #define AUXSPI_CNTL0_CS(x) ((x)<<17) 621 #define AUXSPI_CNTL0_POSTINP (1<<16) 622 #define AUXSPI_CNTL0_VAR_CS (1<<15) 623 #define AUXSPI_CNTL0_VAR_WIDTH (1<<14) 624 #define AUXSPI_CNTL0_DOUT_HOLD(x) ((x)<<12) 625 #define AUXSPI_CNTL0_ENABLE (1<<11) 626 #define AUXSPI_CNTL0_IN_RISING(x) ((x)<<10) 627 #define AUXSPI_CNTL0_CLR_FIFOS (1<<9) 628 #define AUXSPI_CNTL0_OUT_RISING(x) ((x)<<8) 629 #define AUXSPI_CNTL0_INVERT_CLK(x) ((x)<<7) 630 #define AUXSPI_CNTL0_MSB_FIRST(x) ((x)<<6) 631 #define AUXSPI_CNTL0_SHIFT_LEN(x) ((x)<<0) 633 #define AUXSPI_CNTL1_CS_HIGH(x) ((x)<<8) 634 #define AUXSPI_CNTL1_TX_IRQ (1<<7) 635 #define AUXSPI_CNTL1_DONE_IRQ (1<<6) 636 #define AUXSPI_CNTL1_MSB_FIRST(x)((x)<<1) 637 #define AUXSPI_CNTL1_KEEP_INPUT (1<<0) 639 #define AUXSPI_STAT_TX_FIFO(x) ((x)<<28) 640 #define AUXSPI_STAT_RX_FIFO(x) ((x)<<20) 641 #define AUXSPI_STAT_TX_FULL (1<<10) 642 #define AUXSPI_STAT_TX_EMPTY (1<<9) 643 #define AUXSPI_STAT_RX_EMPTY (1<<7) 644 #define AUXSPI_STAT_BUSY (1<<6) 645 #define AUXSPI_STAT_BITS(x) ((x)<<0) 649 #define NORMAL_DMA (DMA_NO_WIDE_BURSTS | DMA_WAIT_RESP) 651 #define TIMED_DMA(x) (DMA_DEST_DREQ | DMA_PERIPHERAL_MAPPING(x)) 653 #define PCM_TIMER (((PCM_BASE + PCM_FIFO*4) & 0x00ffffff) | PI_PERI_BUS) 654 #define PWM_TIMER (((PWM_BASE + PWM_FIFO*4) & 0x00ffffff) | PI_PERI_BUS) 656 #define DBG_MIN_LEVEL 0 658 #define DBG_STARTUP 1 662 #define DBG_INTERNAL 5 663 #define DBG_SLOW_TICK 6 664 #define DBG_FAST_TICK 7 665 #define DBG_MAX_LEVEL 8 667 #define GPIO_UNDEFINED 0 671 #define GPIO_HW_CLK 4 672 #define GPIO_HW_PWM 5 676 #define STACK_SIZE (256*1024) 678 #define PAGE_SIZE 4096 682 #define CYCLES_PER_BLOCK 80 683 #define PULSE_PER_CYCLE 25 685 #define PAGES_PER_BLOCK 53 687 #define CBS_PER_IPAGE 117 688 #define LVS_PER_IPAGE 38 689 #define OFF_PER_IPAGE 38 690 #define TCK_PER_IPAGE 2 691 #define ON_PER_IPAGE 2 692 #define PAD_PER_IPAGE 7 694 #define CBS_PER_OPAGE 118 695 #define OOL_PER_OPAGE 79 710 #define WCB_CNT_PER_PAGE 2 711 #define WCB_COUNTERS (WCB_CNT_PER_PAGE * PI_WAVE_COUNT_PAGES) 712 #define WCB_CNT_CBS 13 713 #define WCB_CNT_OOL 68 714 #define WCB_COUNTER_OOL (WCB_CNT_PER_PAGE * WCB_CNT_OOL) 715 #define WCB_COUNTER_CBS (WCB_CNT_PER_PAGE * WCB_CNT_CBS) 716 #define WCB_CHAIN_CBS 60 717 #define WCB_CHAIN_OOL 60 719 #define CBS_PER_CYCLE ((PULSE_PER_CYCLE*3)+2) 721 #define NUM_CBS (CBS_PER_CYCLE * bufferCycles) 723 #define SUPERCYCLE 800 724 #define SUPERLEVEL 20000 726 #define BLOCK_SIZE (PAGES_PER_BLOCK*PAGE_SIZE) 728 #define DMAI_PAGES (PAGES_PER_BLOCK * bufferBlocks) 730 #define DMAO_PAGES (PAGES_PER_BLOCK * PI_WAVE_BLOCKS) 732 #define NUM_WAVE_OOL (DMAO_PAGES * OOL_PER_OPAGE) 733 #define NUM_WAVE_CBS (DMAO_PAGES * CBS_PER_OPAGE) 737 #define PI_I2C_CLOSED 0 738 #define PI_I2C_RESERVED 1 739 #define PI_I2C_OPENED 2 741 #define PI_SPI_CLOSED 0 742 #define PI_SPI_RESERVED 1 743 #define PI_SPI_OPENED 2 745 #define PI_SER_CLOSED 0 746 #define PI_SER_RESERVED 1 747 #define PI_SER_OPENED 2 749 #define PI_FILE_CLOSED 0 750 #define PI_FILE_RESERVED 1 751 #define PI_FILE_OPENED 2 753 #define PI_NOTIFY_CLOSED 0 754 #define PI_NOTIFY_RESERVED 1 755 #define PI_NOTIFY_CLOSING 2 756 #define PI_NOTIFY_OPENED 3 757 #define PI_NOTIFY_RUNNING 4 758 #define PI_NOTIFY_PAUSED 5 760 #define PI_WFRX_NONE 0 761 #define PI_WFRX_SERIAL 1 762 #define PI_WFRX_I2C_SDA 2 763 #define PI_WFRX_I2C_SCL 3 764 #define PI_WFRX_SPI_SCLK 4 765 #define PI_WFRX_SPI_MISO 5 766 #define PI_WFRX_SPI_MOSI 6 767 #define PI_WFRX_SPI_CS 7 769 #define PI_WF_MICROS 1 773 #define MAX_REPORT 250 774 #define MAX_SAMPLE 4000 776 #define DEFAULT_PWM_IDX 5 778 #define MAX_EMITS (PIPE_BUF / sizeof(gpioReport_t)) 780 #define SRX_BUF_SIZE 8192 782 #define PI_I2C_RETRIES 0x0701 783 #define PI_I2C_TIMEOUT 0x0702 784 #define PI_I2C_SLAVE 0x0703 785 #define PI_I2C_FUNCS 0x0705 786 #define PI_I2C_RDWR 0x0707 787 #define PI_I2C_SMBUS 0x0720 789 #define PI_I2C_SMBUS_READ 1 790 #define PI_I2C_SMBUS_WRITE 0 792 #define PI_I2C_SMBUS_QUICK 0 793 #define PI_I2C_SMBUS_BYTE 1 794 #define PI_I2C_SMBUS_BYTE_DATA 2 795 #define PI_I2C_SMBUS_WORD_DATA 3 796 #define PI_I2C_SMBUS_PROC_CALL 4 797 #define PI_I2C_SMBUS_BLOCK_DATA 5 798 #define PI_I2C_SMBUS_I2C_BLOCK_BROKEN 6 799 #define PI_I2C_SMBUS_BLOCK_PROC_CALL 7 800 #define PI_I2C_SMBUS_I2C_BLOCK_DATA 8 802 #define PI_I2C_SMBUS_BLOCK_MAX 32 803 #define PI_I2C_SMBUS_I2C_BLOCK_MAX 32 805 #define PI_I2C_FUNC_SMBUS_QUICK 0x00010000 806 #define PI_I2C_FUNC_SMBUS_READ_BYTE 0x00020000 807 #define PI_I2C_FUNC_SMBUS_WRITE_BYTE 0x00040000 808 #define PI_I2C_FUNC_SMBUS_READ_BYTE_DATA 0x00080000 809 #define PI_I2C_FUNC_SMBUS_WRITE_BYTE_DATA 0x00100000 810 #define PI_I2C_FUNC_SMBUS_READ_WORD_DATA 0x00200000 811 #define PI_I2C_FUNC_SMBUS_WRITE_WORD_DATA 0x00400000 812 #define PI_I2C_FUNC_SMBUS_PROC_CALL 0x00800000 813 #define PI_I2C_FUNC_SMBUS_READ_BLOCK_DATA 0x01000000 814 #define PI_I2C_FUNC_SMBUS_WRITE_BLOCK_DATA 0x02000000 815 #define PI_I2C_FUNC_SMBUS_READ_I2C_BLOCK 0x04000000 816 #define PI_I2C_FUNC_SMBUS_WRITE_I2C_BLOCK 0x08000000 818 #define PI_MASH_MAX_FREQ 23800000 820 #define FLUSH_PAGES 1024 822 #define MB_DEV_MAJOR 100 824 #define MB_IOCTL _IOWR(MB_DEV_MAJOR, 0, char *) 826 #define MB_DEV1 "/dev/vcio" 827 #define MB_DEV2 "/dev/pigpio-mb" 829 #define BUS_TO_PHYS(x) ((x)&~0xC0000000) 832 #define MB_PROCESS_REQUEST 0 834 #define MB_ALLOCATE_MEMORY_TAG 0x3000C 835 #define MB_LOCK_MEMORY_TAG 0x3000D 836 #define MB_UNLOCK_MEMORY_TAG 0x3000E 837 #define MB_RELEASE_MEMORY_TAG 0x3000F 839 #define PI_SCRIPT_FREE 0 840 #define PI_SCRIPT_RESERVED 1 841 #define PI_SCRIPT_IN_USE 2 842 #define PI_SCRIPT_DYING 3 844 #define PI_SCRIPT_HALT 0 845 #define PI_SCRIPT_RUN 1 846 #define PI_SCRIPT_DELETE 2 848 #define PI_SCRIPT_STACK_SIZE 256 850 #define PI_SPI_FLAGS_CHANNEL(x) ((x&7)<<29) 852 #define PI_SPI_FLAGS_GET_CHANNEL(x) (((x)>>29)&7) 853 #define PI_SPI_FLAGS_GET_BITLEN(x) (((x)>>16)&63) 854 #define PI_SPI_FLAGS_GET_RX_LSB(x) (((x)>>15)&1) 855 #define PI_SPI_FLAGS_GET_TX_LSB(x) (((x)>>14)&1) 856 #define PI_SPI_FLAGS_GET_3WREN(x) (((x)>>10)&15) 857 #define PI_SPI_FLAGS_GET_3WIRE(x) (((x)>>9)&1) 858 #define PI_SPI_FLAGS_GET_AUX_SPI(x) (((x)>>8)&1) 859 #define PI_SPI_FLAGS_GET_RESVD(x) (((x)>>5)&7) 860 #define PI_SPI_FLAGS_GET_CSPOLS(x) (((x)>>2)&7) 861 #define PI_SPI_FLAGS_GET_MODE(x) ((x)&3) 863 #define PI_SPI_FLAGS_GET_CPHA(x) ((x)&1) 864 #define PI_SPI_FLAGS_GET_CPOL(x) ((x)&2) 865 #define PI_SPI_FLAGS_GET_CSPOL(x) ((x)&4) 867 #define PI_STARTING 0 871 #define PI_THREAD_NONE 0 872 #define PI_THREAD_STARTED 1 873 #define PI_THREAD_RUNNING 2 875 #define PI_MAX_PATH 512 1317 static volatile uint32_t *
auxReg = MAP_FAILED;
1319 static volatile uint32_t *
clkReg = MAP_FAILED;
1320 static volatile uint32_t *
dmaReg = MAP_FAILED;
1323 static volatile uint32_t *
pcmReg = MAP_FAILED;
1324 static volatile uint32_t *
pwmReg = MAP_FAILED;
1325 static volatile uint32_t *
spiReg = MAP_FAILED;
1328 static volatile uint32_t *
dmaIn = MAP_FAILED;
1329 static volatile uint32_t *
dmaOut = MAP_FAILED;
1388 0x00, 0x00, 0x00, 0x00, 0x84, 0x94, 0xA4, 0x00, 0x00, 0x00,
1389 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1390 0x82, 0x92, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1391 0x00, 0x00, 0x84, 0x00, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00,
1392 0x00, 0x00, 0x94, 0xA4, 0x94, 0x00, 0x00, 0x00, 0x00, 0x00,
1393 0x00, 0x00, 0x00, 0x00,
1420 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1421 0x00, 0x00, 0x84, 0x94, 0x00, 0x00, 0x00, 0x00, 0x82, 0x92,
1422 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1423 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1424 0x84, 0x94, 0x00, 0x00, 0x00, 0x94, 0x00, 0x00, 0x00, 0x00,
1425 0x00, 0x00, 0x85, 0x95,
1464 { 1, 2, 4, 5, 8, 10, 16, 20, 25,
1465 32, 40, 50, 80, 100, 160, 200, 400, 800};
1468 { 25, 50, 100, 125, 200, 250, 400, 500, 625,
1469 800, 1000, 1250, 2000, 2500, 4000, 5000, 10000, 20000};
1482 (
int clkCtl,
int clkDiv,
int clkSrc,
int divI,
int divF,
int MASH);
1484 static void initDMAgo(
volatile uint32_t *dmaAddr, uint32_t cbAddr);
1495 int i,
c, len, valid;
1501 for (i=0; i<len; i++)
1505 if ((!isalnum(c)) && (c !=
'_') && (c !=
'-'))
1518 static struct timeval last;
1519 static char buf[32];
1524 gettimeofday(&now, NULL);
1526 if (now.tv_sec != last.tv_sec)
1528 localtime_r(&now.tv_sec, &tmp);
1529 strftime(buf,
sizeof(buf),
"%F %T", &tmp);
1530 last.tv_sec = now.tv_sec;
1540 int i,
c, len, in_part, parts, last_char_dot;
1549 for (i=0; i<len; i++)
1553 if (memchr(bad, c, 4))
1557 if (last_char_dot)
return 1;
1560 else last_char_dot = 0;
1568 if (!in_part) parts++;
1574 if (parts < 2)
return 1;
else return 0;
1581 static char str[128];
1586 if (count > 40) c = 40;
else c = count;
1588 for (i=0; i<
c; i++) sprintf(str+(3*i),
"%02X ", buf[i]);
1599 int fd,
char rw, uint8_t cmd,
int size,
union my_smbus_data *data)
1621 shift = (gpio%10) * 3;
1648 struct timespec ts, rem;
1650 ts.tv_sec = seconds;
1651 ts.tv_nsec = micros * 1000;
1653 while (clock_nanosleep(CLOCK_REALTIME, 0, &ts, &rem))
1688 if (chmod(name, perm) < 0)
1690 DBG(
DBG_ALWAYS,
"Can't set permissions (%d) for %s, %m", perm, name);
1728 level = dmaIVirt[page]->
level[slot];
1739 if (*esc) bytes = 2;
else bytes = 1;
1743 if (*inPos <= (inLen - bytes))
1747 return inBuf[(*inPos)++];
1752 return inBuf[*inPos-2] + (inBuf[*inPos-1]<<8);
1767 tick = dmaIVirt[page]->
tick[slot];
1776 if (
gpioMask & ((uint64_t)(1)<<gpio))
return 1;
1790 PROT_READ|PROT_WRITE|PROT_EXEC,
1791 MAP_SHARED|MAP_ANONYMOUS|MAP_NORESERVE|MAP_LOCKED,
1794 if (dummy == MAP_FAILED)
1810 pthread_mutex_lock(&wfRx[i].mutex);
1817 pthread_mutex_unlock(&wfRx[i].mutex);
1837 uint32_t tmp1, tmp2, tmp3, tmp4, tmp5;
1850 if ((mask | p[1]) != mask)
1853 "gpioWrite_Bits_0_31_Clear: bad bits %08X (permissions %08X)",
1864 if ((mask | p[1]) != mask)
1867 "gpioWrite_Bits_32_53_Clear: bad bits %08X (permissions %08X)",
1878 memcpy(&p[4], buf, 4);
1884 if (p[3] > (bufSize/2)) p[3] = bufSize/2;
1885 res =
bbI2CZip(p[1], buf, p[3], buf+(bufSize/2), bufSize/2);
1888 memcpy(buf, buf+(bufSize/2), res);
1896 if (p[3]) memcpy(&xfer.
txBuf, buf, p[3]);
1900 memcpy(buf, &res, 4);
1901 res = 4 + xfer.
rxCnt;
1902 if (res > 4) memcpy(buf+4, &xfer.
rxBuf, res-4);
1908 memcpy(&tmp1, buf+ 0, 4);
1909 memcpy(&tmp2, buf+ 4, 4);
1910 memcpy(&tmp3, buf+ 8, 4);
1911 memcpy(&tmp4, buf+12, 4);
1912 memcpy(&tmp5, buf+16, 4);
1917 "bbSPIOpen: gpio %d, no permission to update CS", p[1]);
1924 "bbSPIOpen: gpio %d, no permission to update MISO", tmp1);
1931 "bbSPIOpen: gpio %d, no permission to update MOSI", tmp2);
1938 "bbSPIOpen: gpio %d, no permission to update SCLK", tmp3);
1942 if (!res) res =
bbSPIOpen(p[1], tmp1, tmp2, tmp3, tmp4, tmp5);
1950 if (p[3] > bufSize) p[3] = bufSize;
1963 if ((mask | p[1]) != mask)
1966 "gpioWrite_Bits_0_31_Set: bad bits %08X (permissions %08X)",
1977 if ((mask | p[1]) != mask)
1980 "gpioWrite_Bits_32_53_Set: bad bits %08X (permissions %08X)",
1992 if (p[2] > bufSize) p[2] = bufSize;
1994 if (res > p[2]) res = p[2];
2012 if (p[1] > bufSize) p[1] = bufSize;
2017 memcpy(&p[4], buf, 4);
2024 if (p[2] > bufSize) p[2] = bufSize;
2029 memcpy(&p[4], buf, 4);
2045 "gpioHardwareClock: gpio %d, no permission to update",
2056 memcpy(&p[4], buf, 4);
2062 "gpioHardwarePWM: gpio %d, no permission to update", p[1]);
2074 memcpy(&p[4], buf, 4);
2075 res =
i2cOpen(p[1], p[2], p[4]);
2079 memcpy(&p[4], buf, 4);
2090 if (p[2] > bufSize) p[2] = bufSize;
2095 memcpy(&p[4], buf, 4);
2108 memcpy(&p[4], buf, 4);
2129 memcpy(&p[4], buf, 4);
2135 if (p[3] > (bufSize/2)) p[3] = bufSize/2;
2136 res =
i2cZip(p[1], buf, p[3], buf+(bufSize/2), bufSize/2);
2139 memcpy(buf, buf+(bufSize/2), res);
2160 "gpioSetMode: gpio %d, no permission to update", p[1]);
2184 "gpioSetPWMfrequency: gpio %d, no permission to update", p[1]);
2220 "gpioSetPWMrange: gpio %d, no permission to update", p[1]);
2230 "gpioSetPullUpDown: gpio %d, no permission to update", p[1]);
2239 DBG(
DBG_USER,
"gpioPWM: gpio %d, no permission to update", p[1]);
2251 "gpioServo: gpio %d, no permission to update", p[1]);
2269 if (p[2] > bufSize) p[2] = bufSize;
2270 res =
serRead(p[1], buf, p[2]);
2277 res =
shell(buf, buf+p[1]+1);
2282 if (p[2] > bufSize) p[2] = bufSize;
2289 memcpy(&p[4], buf, 4);
2299 memcpy(&p[4], buf, 4);
2300 res =
spiOpen(p[1], p[2], p[4]);
2304 if (p[2] > bufSize) p[2] = bufSize;
2305 res =
spiRead(p[1], buf, p[2]);
2309 if (p[3] > bufSize) p[3] = bufSize;
2314 if (p[3] > bufSize) p[3] = bufSize;
2315 res =
spiXfer(p[1], buf, buf, p[3]);
2323 memcpy(&p[4], buf, 4);
2329 "gpioTrigger: gpio %d, no permission to update", p[1]);
2340 DBG(
DBG_USER,
"gpioWrite: gpio %d, no permission to update", p[1]);
2358 tmp1 = pulse[i].
gpioOn & mask;
2359 if (tmp1 != pulse[i].gpioOn)
2365 tmp1 = pulse[i].
gpioOff & mask;
2366 if (tmp1 != pulse[i].gpioOff)
2372 pulse[i].gpioOn, pulse[i].gpioOff, pulse[i].usDelay);
2385 memcpy(&tmp1, buf, 4);
2386 memcpy(&tmp2, buf+4, 4);
2387 memcpy(&tmp3, buf+8, 4);
2389 (p[1], p[2], tmp1, tmp2, tmp3, p[3]-12, buf+12);
2395 "gpioWaveAddSerial: gpio %d, no permission to update", p[1]);
2403 if (p[3] > bufSize) p[3] = bufSize;
2479 dmaIVirt[page]->
gpioOff[slot] |= (1<<gpio);
2490 dmaIVirt[page]->
gpioOff[slot] &= ~(1<<gpio);
2502 dmaIVirt[page]->
gpioOn[slot] |= (1<<gpio);
2514 dmaIVirt[page]->
gpioOn[slot] &= ~(1<<gpio);
2522 int newOff, oldOff, realRange, cycles, i;
2523 int deferOff, deferRng;
2526 "myGpioSetPwm %d from %d to %d", gpio, oldVal, newVal);
2532 cycles =
pwmCycles [gpioInfo[gpio].freqIdx];
2534 newOff = (newVal * realRange)/gpioInfo[gpio].range;
2535 oldOff = (oldVal * realRange)/gpioInfo[gpio].range;
2537 deferOff = gpioInfo[gpio].deferOff;
2538 deferRng = gpioInfo[gpio].deferRng;
2540 if (gpioInfo[gpio].deferOff)
2546 gpioInfo[gpio].deferOff = 0;
2549 if (newOff != oldOff)
2551 if (newOff && oldOff)
2553 if (newOff != realRange)
2558 if (newOff > oldOff)
2565 gpioInfo[gpio].deferOff = oldOff;
2566 gpioInfo[gpio].deferRng = realRange;
2571 if (newOff != realRange)
2605 int newOff, oldOff, realRange, cycles, i;
2606 int deferOff, deferRng;
2609 "myGpioSetServo %d from %d to %d", gpio, oldVal, newVal);
2614 newOff = (newVal * realRange)/20000;
2615 oldOff = (oldVal * realRange)/20000;
2617 deferOff = gpioInfo[gpio].
deferOff;
2618 deferRng = gpioInfo[gpio].
deferRng;
2620 if (gpioInfo[gpio].deferOff)
2629 if (newOff != oldOff)
2631 if (newOff && oldOff)
2636 if (newOff > oldOff)
2644 gpioInfo[gpio].
deferRng = realRange;
2687 return mknod(dev, S_IFCHR|0600, makedev(
MB_DEV_MAJOR, 0));
2717 int fd,
unsigned size,
unsigned align,
unsigned flags)
2730 p[0] = i*
sizeof(*p);
2748 p[0] = i*
sizeof(*p);
2766 p[0] = i*
sizeof(*p);
2784 p[0] = i*
sizeof(*p);
2793 void *mem = MAP_FAILED;
2795 mem = mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED,
fdMem, base);
2803 return munmap(addr, size);
2819 DMAMemP->
size = size;
2846 return &dmaOVirt[page]->
cb[slot];
2859 return (uint32_t) &dmaOBus[page]->
cb[slot];
2879 dmaOVirt[page]->
OOL[slot] = OOL;
2890 return (uint32_t) &dmaOBus[page]->
OOL[slot];
2897 (
unsigned baud,
unsigned bits,
unsigned stops,
unsigned *bitDelay)
2899 unsigned fullBit, last, diff, t, i;
2903 fullBit = 1000000000 / baud;
2906 for (i=0; i<=bits; i++)
2908 t = (((i+1)*fullBit)+500)/1000;
2914 t = (((bits+1)*fullBit) + ((stops*fullBit)/2) + 500)/1000;
2923 if (!delay)
return 0;
2934 int numCB=0, numBOOL=0, numTOOL=0;
2949 for (i=0; i<numWaves; i++)
2951 if (waves[i].gpioOn) {numCB++; numBOOL++;}
2952 if (waves[i].gpioOff) {numCB++; numBOOL++;}
2960 *numBOOLs = numBOOL;
2961 *numTOOLs = numTOOL;
2966 static int wave2Cbs(
unsigned wave_mode,
int *CB,
int *BOOL,
int *TOOL)
2968 int botCB=*CB, botOOL=*BOOL, topOOL=*TOOL;
2974 unsigned i, repeatCB;
2978 unsigned delayCBs, dcb;
3004 p->
src = (uint32_t) (&dmaOBus[0]->periphData);
3010 for (i=0; i<numWaves; i++)
3012 if (waves[i].gpioOn)
3025 if (waves[i].gpioOff)
3060 if (waves[i].usDelay)
3066 for (dcb=0; dcb<delayCBs; dcb++)
3083 p->
src = (uint32_t) (&dmaOBus[0]->periphData);
3107 status = botCB - *CB;
3120 int diffTicks, lastLevel;
3134 else lastLevel = w->
s.
level;
3141 if (lastLevel) w->
s.
data |= (1<<(w->
s.
bit-1));
3194 switch (wfRx[gpio].
mode)
3206 unsigned inPos1=0, inPos2=0, outPos=0, level =
NUM_WAVE_OOL;
3210 unsigned numIn2, numOut;
3212 uint32_t tNow, tNext1, tNext2, tDelay;
3224 if (!numIn1) tNext1 = -1;
else tNext1 = 0;
3225 if (!numIn2) tNext2 = -1;
else tNext2 = 0;
3227 while (((inPos1<numIn1) || (inPos2<numIn2)) && (outPos<numOut))
3229 if (tNext1 < tNext2)
3236 out[outPos-1].
usDelay += (tNext1 - tNow);
3244 tNext1 = tNow + in1[inPos1].
usDelay; ++inPos1;
3246 else if (tNext2 < tNext1)
3253 out[outPos-1].
usDelay += (tNext2 - tNow);
3261 tNext2 = tNow + in2[inPos2].
usDelay; ++inPos2;
3270 out[outPos-1].
usDelay += (tNext1 - tNow);
3278 tNext1 = tNow + in1[inPos1].
usDelay; ++inPos1;
3279 tNext2 = tNow + in2[inPos2].
usDelay; ++inPos2;
3282 if (tNext1 <= tNext2) { tDelay = tNext1 - tNow; tNow = tNext1; }
3283 else { tDelay = tNext2 - tNow; tNow = tNext2; }
3289 if (out[outPos].gpioOn) cbs++;
3291 if (out[outPos].gpioOff) cbs++;
3307 if (inPos1 >= numIn1) tNext1 = -1;
3308 if (inPos2 >= numIn2) tNext2 = -1;
3312 if ((outPos < numOut) && (outPos < level))
3395 return 0xFF & data.
byte;
3466 return 0xFF & data.
byte;
3476 DBG(
DBG_USER,
"handle=%d reg=%d bVal=%d", handle, reg, bVal);
3548 return 0xFFFF & data.
word;
3558 DBG(
DBG_USER,
"handle=%d reg=%d wVal=%d", handle, reg, wVal);
3601 DBG(
DBG_USER,
"handle=%d reg=%d wVal=%d", handle, reg, wVal);
3634 return 0xFFFF & data.
word;
3644 DBG(
DBG_USER,
"handle=%d reg=%d buf=%08X", handle, reg, (
unsigned)buf);
3676 for (i=0; i<data.
block[0]; i++) buf[i] = data.
block[i+1];
3677 return data.
block[0];
3685 unsigned handle,
unsigned reg,
char *buf,
unsigned count)
3692 handle, reg, count,
myBuf2Str(count, buf));
3708 if ((count < 1) || (count > 32))
3711 for (i=1; i<=count; i++) data.
block[i] = buf[i-1];
3712 data.
block[0] = count;
3732 unsigned handle,
unsigned reg,
char *buf,
unsigned count)
3739 handle, reg, count,
myBuf2Str(count, buf));
3755 if ((count < 1) || (count > 32))
3758 for (i=1; i<=count; i++) data.
block[i] = buf[i-1];
3759 data.
block[0] = count;
3774 for (i=0; i<data.
block[0]; i++) buf[i] = data.
block[i+1];
3775 return data.
block[0];
3783 unsigned handle,
unsigned reg,
char *buf,
unsigned count)
3790 DBG(
DBG_USER,
"handle=%d reg=%d count=%d buf=%08X",
3791 handle, reg, count, (
unsigned)buf);
3807 if ((count < 1) || (count > 32))
3815 data.
block[0] = count;
3829 for (i=0; i<data.
block[0]; i++) buf[i] = data.
block[i+1];
3830 return data.
block[0];
3838 unsigned handle,
unsigned reg,
char *buf,
unsigned count)
3845 handle, reg, count,
myBuf2Str(count, buf));
3861 if ((count < 1) || (count > 32))
3864 for (i=1; i<=count; i++) data.
block[i] = buf[i-1];
3866 data.
block[0] = count;
3902 bytes = write(i2cInfo[handle].fd, buf, count);
3918 handle, count, (
unsigned)buf);
3931 bytes = read(i2cInfo[handle].fd, buf, count);
3942 int i2cOpen(
unsigned i2cBus,
unsigned i2cAddr,
unsigned i2cFlags)
3944 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
3950 i2cBus, i2cAddr, i2cFlags);
3962 pthread_mutex_lock(&mutex);
3974 pthread_mutex_unlock(&mutex);
3978 sprintf(dev,
"/dev/i2c-%d", i2cBus);
3980 if ((fd = open(dev, O_RDWR)) < 0)
3984 if (system(
"/sbin/modprobe i2c_dev") == -1) { }
3985 if (system(
"/sbin/modprobe i2c_bcm2835") == -1) { }
3989 if ((fd = open(dev, O_RDWR)) < 0)
4008 i2cInfo[slot].
fd = fd;
4009 i2cInfo[slot].
addr = i2cAddr;
4010 i2cInfo[slot].
flags = i2cFlags;
4011 i2cInfo[slot].
funcs = funcs;
4029 if (i2cInfo[handle].fd >= 0) close(i2cInfo[handle].fd);
4049 if (write(fd,
"1\n", 2) == -1) { }
4053 if (write(fd,
"0\n", 2) == -1) { }
4082 rdwr.
nmsgs = numSegs;
4084 retval = ioctl(i2cInfo[handle].fd,
PI_I2C_RDWR, &rdwr);
4086 if (retval >= 0)
return retval;
4092 char *inBuf,
unsigned inLen,
char *outBuf,
unsigned outLen)
4094 int numSegs, inPos, outPos,
status, bytes, flags, addr;
4098 DBG(
DBG_USER,
"handle=%d inBuf=%s outBuf=%08X len=%d",
4099 handle,
myBuf2Str(inLen, (
char *)inBuf), (
int)outBuf, outLen);
4109 if (!inBuf || !inLen)
4112 if (!outBuf && outLen)
4126 while (!status && (inPos < inLen))
4128 DBG(
DBG_INTERNAL,
"status=%d inpos=%d inlen=%d cmd=%d addr=%d flags=%x",
4129 status, inPos, inLen, inBuf[inPos], addr, flags);
4131 switch (inBuf[inPos++])
4142 if (status >= 0) status = 0;
4153 if (status >= 0) status = 0;
4181 if ((bytes + outPos) < outLen)
4183 segs[numSegs].
addr = addr;
4184 segs[numSegs].
flags = (flags|1);
4185 segs[numSegs].
len = bytes;
4186 segs[numSegs].
buf = (uint8_t *)(outBuf + outPos);
4192 if (status >= 0) status = 0;
4207 if ((bytes + inPos) < inLen)
4209 segs[numSegs].
addr = addr;
4210 segs[numSegs].
flags = (flags&0xfffe);
4211 segs[numSegs].
len = bytes;
4212 segs[numSegs].
buf = (uint8_t *)(inBuf + inPos);
4218 if (status >= 0) status = 0;
4231 if (setesc) esc = 1;
else esc = 0;
4238 if (numSegs) status =
i2cSegments(handle, segs, numSegs);
4241 if (status >= 0) status = outPos;
4256 if (bitlen <= 8) bits = *((( uint8_t*)buf)+
pos);
4257 else if (bitlen <= 16) bits = *(((uint16_t*)buf)+
pos);
4258 else bits = *(((uint32_t*)buf)+
pos);
4260 if (msbf) bits <<= (32-bitlen);
4267 char *buf,
int pos,
int bitlen,
int msbf, uint32_t bits)
4271 if (!msbf) bits >>= (32-bitlen);
4273 if (bitlen <= 8) *((( uint8_t*)buf)+
pos) = bits;
4274 else if (bitlen <= 16) *(((uint16_t*)buf)+
pos) = bits;
4275 else *(((uint32_t*)buf)+
pos) = bits;
4300 char bit_ir[4] = {1, 0, 0, 1};
4301 char bit_or[4] = {0, 1, 1, 0};
4302 char bit_ic[4] = {0, 0, 1, 1};
4304 int mode, bitlen, txmsbf, rxmsbf, channel;
4307 uint32_t spiDefaults;
4309 int txFull, rxEmpty;
4316 if (!bitlen) bitlen = 8;
4320 if (bitlen > 8) count /= 2;
4321 if (bitlen > 16) count /= 2;
4355 while ((txCnt < count) || (rxCnt < count))
4361 txFull = (((statusReg>>28)&15) > 2);
4376 if (txCnt != (count-1))
4404 unsigned cnt, cnt4w, cnt3w;
4405 uint32_t spiDefaults;
4406 unsigned mode, channel, cspol, cspols, flag3w, ren3w;
4411 cspol = (cspols>>channel) & 1;
4432 cnt3w = count - ren3w;
4452 while((txCnt < cnt) || (rxCnt < cnt))
4477 while((txCnt < cnt) || (rxCnt < cnt))
4506 static pthread_mutex_t main_mutex = PTHREAD_MUTEX_INITIALIZER;
4507 static pthread_mutex_t aux_mutex = PTHREAD_MUTEX_INITIALIZER;
4511 pthread_mutex_lock(&aux_mutex);
4512 spiGoA(speed, flags, txBuf, rxBuf, count);
4513 pthread_mutex_unlock(&aux_mutex);
4517 pthread_mutex_lock(&main_mutex);
4518 spiGoS(speed, flags, txBuf, rxBuf, count);
4519 pthread_mutex_unlock(&main_mutex);
4540 uint32_t resvd, cspols;
4654 int spiOpen(
unsigned spiChan,
unsigned baud,
unsigned spiFlags)
4656 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
4660 spiChan, baud, spiFlags);
4680 if (spiFlags > (1<<22))
4686 spiGo(baud, spiFlags, NULL, NULL, 0);
4691 pthread_mutex_lock(&mutex);
4703 pthread_mutex_unlock(&mutex);
4707 spiInfo[slot].
speed = baud;
4729 spiTerm(spiInfo[handle].flags);
4750 spiGo(spiInfo[handle].speed, spiInfo[handle].flags, NULL, buf, count);
4771 spiGo(spiInfo[handle].speed, spiInfo[handle].flags, buf, NULL, count);
4779 handle, count,
myBuf2Str(count, txBuf));
4792 spiGo(spiInfo[handle].speed, spiInfo[handle].flags, txBuf, rxBuf, count);
4800 int serOpen(
char *tty,
unsigned serBaud,
unsigned serFlags)
4802 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
4808 DBG(
DBG_USER,
"tty=%s serBaud=%d serFlags=0x%X", tty, serBaud, serFlags);
4812 if (strncmp(
"/dev/tty", tty, 8) && strncmp(
"/dev/serial", tty, 11))
4817 case 50: speed = B50;
break;
4818 case 75: speed = B75;
break;
4819 case 110: speed = B110;
break;
4820 case 134: speed = B134;
break;
4821 case 150: speed = B150;
break;
4822 case 200: speed = B200;
break;
4823 case 300: speed = B300;
break;
4824 case 600: speed = B600;
break;
4825 case 1200: speed = B1200;
break;
4826 case 1800: speed = B1800;
break;
4827 case 2400: speed = B2400;
break;
4828 case 4800: speed = B4800;
break;
4829 case 9600: speed = B9600;
break;
4830 case 19200: speed = B19200;
break;
4831 case 38400: speed = B38400;
break;
4832 case 57600: speed = B57600;
break;
4833 case 115200: speed = B115200;
break;
4834 case 230400: speed = B230400;
break;
4845 pthread_mutex_lock(&mutex);
4857 pthread_mutex_unlock(&mutex);
4861 if ((fd = open(tty, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK)) == -1)
4867 tcgetattr(fd, &
new);
4871 cfsetispeed(&
new, speed);
4872 cfsetospeed(&
new, speed);
4874 new.c_cc [VMIN] = 0;
4875 new.c_cc [VTIME] = 0;
4877 tcflush(fd, TCIFLUSH);
4878 tcsetattr(fd, TCSANOW, &
new);
4882 serInfo[slot].
fd = fd;
4883 serInfo[slot].
flags = serFlags;
4901 if (serInfo[handle].fd >= 0) close(serInfo[handle].fd);
4928 if (write(serInfo[handle].fd, &c, 1) != 1)
4949 r = read(serInfo[handle].fd, &x, 1);
4952 return ((
int)x) & 0xFF;
4957 else if ((r == -1) && (errno == EAGAIN))
4966 int written=0, wrote=0;
4982 while ((written != count) && (wrote >= 0))
4984 wrote = write(serInfo[handle].fd, buf+written, count-written);
4994 if (written != count)
5004 DBG(
DBG_USER,
"handle=%d count=%d buf=0x%X", handle, count, (
unsigned)buf);
5017 r = read(serInfo[handle].fd, buf, count);
5021 if (errno == EAGAIN)
5028 if (r < count) buf[
r] = 0;
5047 if (ioctl(serInfo[handle].fd, FIONREAD, &result) == -1)
return 0;
5055 (
clkInf_t *clkInf,
unsigned f,
unsigned numc,
unsigned *cf)
5057 int c, valid, offby, offby2, best_offby;
5064 for (c=0; c<numc; c++)
5068 if ((div > 1) && (div < 4096))
5072 frac = cf[
c] - (div * f);
5073 frac = (frac * 4096) / f;
5074 offby = cf[
c] - (div * f) - ((frac * f) / 4096);
5077 offby2 = cf[
c] - (div * f) - (((frac+1) * f) / 4096);
5078 if (offby2 < 0) offby2 = -offby2;
5089 offby = cf[
c] - (div * f);
5092 offby2 = cf[
c] - ((div+1) * f);
5093 if (offby2 < 0) offby2 = -offby2;
5102 if ((!valid) || (offby <= best_offby))
5106 clkInf->
frac = frac;
5124 return &dmaIVirt[page]->
cb[slot];
5135 fprintf(stderr,
"i=%x s=%x d=%x len=%x s=%x nxt=%x\n",
5144 static unsigned lastPage=0;
5147 uint32_t startTick, endTick;
5159 cb = (cbAddr - ((int)dmaIBus[page])) / 32;
5165 if (endTick != startTick)
5166 gpioStats.
cbTicks += (endTick - startTick);
5177 if (page == lastPage)
break;
5201 cb = (cbAddr - ((int)dmaOBus[page])) / 32;
5220 cb = (cbAddr - ((int)dmaOBus[page])) / 32;
5235 static unsigned lastPage=0;
5241 if (!cbAddr)
return -1;
5249 cb = (cbAddr - ((int)dmaOBus[page])) / 32;
5260 if (page == lastPage)
break;
5270 unsigned cycle=0, slot=0, tmp;
5275 if (tmp > 2) slot = ((tmp-2)/3);
5296 return (uint32_t) &dmaIBus[page]->
gpioOn[slot];
5307 return (uint32_t) &dmaIBus[page]->
gpioOff[slot];
5318 return (uint32_t) &dmaIBus[page]->
tick[slot];
5329 return (uint32_t) &dmaIBus[page]->
level[slot];
5341 return (uint32_t) &dmaIBus[page]->
cb[slot];
5432 int b, pulse, level, cycle;
5481 if (gpioSignal[signum].func)
5483 if (gpioSignal[signum].ex)
5485 (gpioSignal[signum].
func)(signum, gpioSignal[signum].userdata);
5489 (gpioSignal[signum].
func)(signum);
5519 DBG(
DBG_ALWAYS,
"Unhandled signal %d, terminating\n", signum);
5529 DBG(
DBG_ALWAYS,
"Unhandled signal %d, terminating\n", signum);
5540 struct sigaction new;
5545 memset(&
new, 0,
sizeof(
new));
5548 sigaction(i, &
new, NULL);
5574 900000, 225000, 240000, 257142, 276923, 300000, 327272, 360000,
5575 400000, 450000, 514285, 600000, 720000, 900000, 1200000, 1800000
5583 uint32_t steadyUs, changedTick, RBitV, LBitV;
5595 changedTick = gpioAlert[i].
gfTick;
5597 for (j=0; j<numSamples; j++)
5599 bitV = sample[j].
level & bit;
5606 changedTick = sample[j].
tick;
5614 diff = sample[j].
tick - changedTick;
5616 if (diff >= steadyUs)
5625 sample[j].
level ^= bit;
5633 gpioAlert[i].
gfTick = changedTick;
5653 for (j=0; j<numSamples; j++)
5655 bitV = sample[j].
level & bit;
5656 nowTick = sample[j].
tick;
5658 if (gpioAlert[i].nfActive)
5660 diff = nowTick - gpioAlert[i].
nfTick2;
5667 gpioAlert[i].
nfTick1 = nowTick;
5674 diff = nowTick - gpioAlert[i].
nfTick1;
5675 gpioAlert[i].
nfTick1 = nowTick;
5677 if (diff >= gpioAlert[i].nfSteadyUs)
5689 if (!gpioAlert[i].nfActive)
5691 if (bitV != gpioAlert[i].nfRBitV)
5692 sample[j].
level ^= bit;
5705 gpioSample_t *sample,
int numSamples, uint32_t changedBits, uint32_t eTick)
5707 uint32_t oldLevel, newLevel;
5709 int emit, seqno, emitted;
5710 uint32_t changes, bits, timeoutBits, eventBits;
5721 if (gpioGetSamples.
func)
5723 if (gpioGetSamples.
ex)
5725 (gpioGetSamples.
func)
5726 (sample, numSamples, gpioGetSamples.
userdata);
5730 (gpioGetSamples.
func)(sample, numSamples);
5745 if (eventAlert[b].fired && (!eventAlert[b].ignore))
5747 eventBits |= (1<<b);
5749 if (eventAlert[b].func)
5751 if (eventAlert[b].ex)
5753 (eventAlert[b].
func)(b, eTick, eventAlert[b].userdata);
5757 (eventAlert[b].
func)(b, eTick);
5762 eventAlert[b].
fired = 0;
5771 for (d=0; d<numSamples; d++)
5775 if (newLevel != oldLevel)
5777 changes = (newLevel ^ oldLevel);
5781 if (changes & (1<<b))
5783 if (newLevel & (1<<b)) v = 1;
else v = 0;
5785 if (gpioAlert[b].func)
5787 if (gpioAlert[b].ex)
5790 (b, v, sample[d].tick,
5791 gpioAlert[b].userdata);
5795 (gpioAlert[b].
func)(b, v, sample[d].tick);
5800 oldLevel = newLevel;
5813 if (gpioAlert[b].wdSteadyUs)
5815 diff = eTick - gpioAlert[b].
wdTick;
5817 if (diff >= gpioAlert[b].wdSteadyUs)
5819 timeoutBits |= (1<<b);
5821 gpioAlert[b].
wdTick = eTick;
5823 if (gpioAlert[b].func)
5825 if (gpioAlert[b].ex)
5828 gpioAlert[b].userdata);
5844 if (gpioNotify[n].pipe)
5847 close(gpioNotify[n].fd);
5849 sprintf(fifo,
"/dev/pigpio%d", n);
5858 bits = gpioNotify[n].
bits;
5862 seqno = gpioNotify[n].
seqno;
5873 if (changedBits & bits)
5877 for (d=0; d<numSamples; d++)
5879 newLevel = sample[d].
level & bits;
5881 if (newLevel != oldLevel)
5883 report[emit].
seqno = seqno;
5884 report[emit].
flags = 0;
5885 report[emit].
tick = sample[d].
tick;
5888 oldLevel = newLevel;
5903 bits = gpioNotify[n].
bits;
5905 if (timeoutBits & bits)
5913 if (timeoutBits & bits & (1<<b))
5916 newLevel = sample[numSamples-1].
level;
5920 report[emit].
seqno = seqno;
5921 report[emit].
flags =
5923 report[emit].
tick = eTick;
5924 report[emit].
level = newLevel;
5938 if (eventBits & gpioNotify[n].eventBits)
5942 if (eventBits & gpioNotify[n].eventBits & (1<<b))
5945 newLevel = sample[numSamples-1].
level;
5949 report[emit].
seqno = seqno;
5950 report[emit].
flags =
5952 report[emit].
tick = eTick;
5953 report[emit].
level = newLevel;
5963 if ((
int)(eTick - gpioNotify[n].lastReportTick) > 60000000)
5966 newLevel = sample[numSamples-1].
level;
5970 report[emit].
seqno = seqno;
5972 report[emit].
tick = eTick;
5973 report[emit].
level = newLevel;
5983 n, emit, report[0].seqno, report[emit-1].seqno);
5993 if (emit > max_emits)
5997 err = write(gpioNotify[n].fd,
6005 if ((errno != EAGAIN) && (errno != EWOULDBLOCK))
6010 gpioNotify[n].fd, err, errno);
6014 gpioNotify[n].
bits = 0;
6033 emitted += max_emits;
6038 err = write(gpioNotify[n].fd,
6046 if ((errno != EAGAIN) && (errno != EWOULDBLOCK))
6049 gpioNotify[n].fd, err, errno);
6054 gpioNotify[n].
bits = 0;
6078 gpioNotify[n].
seqno = seqno;
6089 (gpioScript[n].waitBits & changedBits))
6091 pthread_mutex_lock(&gpioScript[n].pthMutex);
6096 gpioScript[n].
waitBits & changedBits;
6097 pthread_cond_signal(&gpioScript[n].pthCond);
6100 pthread_mutex_unlock(&gpioScript[n].pthMutex);
6111 (gpioScript[n].eventBits & eventBits))
6113 pthread_mutex_lock(&gpioScript[n].pthMutex);
6119 pthread_cond_signal(&gpioScript[n].pthCond);
6122 pthread_mutex_unlock(&gpioScript[n].pthMutex);
6148 for (j=0; j<numSamples; j++)
6150 if ((sample[j].level & bit) != LBitV)
6152 LBitV = sample[j].
level & bit;
6164 struct timespec req, rem;
6165 uint32_t oldLevel, newLevel, level;
6166 uint32_t oldSlot, newSlot;
6167 uint32_t expected, ft, sTick;
6168 uint32_t changedBits;
6169 int32_t diff, minDiff, stickInited;
6171 int numSamples, ticks, i;
6172 int rp, reports, totalSamples;
6248 while ((oldSlot != newSlot) && (numSamples <
MAX_SAMPLE))
6252 sample[numSamples].
tick = sTick;
6253 sample[numSamples].
level = level;
6275 diff = sTick - expected;
6277 if (abs(diff) > minDiff)
6285 sample[numSamples-PULSE_PER_CYCLE+i].
tick =
6286 ((i*ticks)/PULSE_PER_CYCLE) + ft;
6316 if (oldSlot == newSlot) moreToDo = 0;
else moreToDo = 1;
6333 for (rp=0; rp<numSamples; rp++)
6337 if (newLevel != oldLevel)
6339 sample[reports].
tick = sample[rp].
tick;
6341 changedBits |= (newLevel ^ oldLevel);
6342 oldLevel = newLevel;
6348 totalSamples += reports;
6355 alertEmit(sample, reports, changedBits, sample[rp].tick);
6365 totalSamples += reports;
6373 alertEmit(sample, reports, changedBits, sTick);
6389 while (nanosleep(&req, &rem))
6391 req.tv_sec = rem.tv_sec;
6392 req.tv_nsec = rem.tv_nsec;
6437 t=*v1; *v1=*v2; *v2= t;
6459 pthread_mutex_unlock(&s->
pthMutex);
6483 pthread_mutex_unlock(&s->
pthMutex);
6490 static int scrSys(
char *cmd, uint32_t p1, uint32_t p2)
6498 snprintf(buf,
sizeof(buf),
"/opt/pigpio/cgi/%s %u %u", cmd, p1, p2);
6502 status = system(buf);
6515 int p1, p2, p1o, p2o, p3o, *
t1, *
t2;
6530 pthread_mutex_unlock(&s->
pthMutex);
6564 memcpy((
char*)&p3o, (
char *)instr.
p[4],
sizeof(
int));
6566 else memcpy(buf, (
char *)&(s->
script.
par[p3o]),
sizeof(
int));
6570 memcpy(buf, (
char *)instr.
p[4], instr.
p[3]);
6621 case PI_CMD_JM:
if (F<0) PC=p1;
else PC++;
break;
6625 case PI_CMD_JNZ:
if (F) PC=p1;
else PC++;
break;
6627 case PI_CMD_JP:
if (F>=0) PC=p1;
else PC++;
break;
6629 case PI_CMD_JZ:
if (!F) PC=p1;
else PC++;
break;
6640 if ((p1 >= 0) && (p1 <
sizeof(buf))) A = buf[p1];
6648 case PI_CMD_OR: A|=p1; F=A; PC++;
break;
6699 if ((p1 >= 0) && (p1 <
sizeof(buf))) buf[p1] = A;
6754 struct timespec req, rem;
6763 while (nanosleep(&req, &rem))
6765 req.tv_sec = rem.tv_sec;
6766 req.tv_nsec = rem.tv_nsec;
6769 if (tp->ex) (tp->func)(tp->userdata);
6782 int idx, flags, len, res, i;
6800 flags = fcntl(fileno(
outFifo), F_GETFL, 0);
6801 fcntl(fileno(
outFifo), F_SETFL, flags | O_NONBLOCK);
6809 if (fgets(buf,
sizeof(buf),
inpFifo) == NULL)
6823 while (((ctl.
eaten)<len) && (idx >= 0))
6836 fprintf(
outFifo,
"%d\n", res);
6840 fprintf(
outFifo,
"%d\n", res);
6844 fprintf(
outFifo,
"%d\n", res);
6848 fprintf(
outFifo,
"%08X\n", res);
6852 fprintf(
outFifo,
"%u\n", res);
6863 for (i=0; i<res; i++)
6865 fprintf(
outFifo,
" %d", v[i]);
6872 if (res < 0) fprintf(
outFifo,
"%d\n", res);
6876 param = (uint32_t *)v;
6879 fprintf(
outFifo,
" %d", param[i]);
6899 int sock = *(
int*)fdC;
6908 setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (
char*)&opt,
sizeof(
int));
6912 if (recv(sock, p, 16, MSG_WAITALL) != 16)
break;
6916 if (p[3] <
sizeof(buf))
6919 if (recv(sock, buf, p[3], MSG_WAITALL) != p[3])
6923 "recv failed for %d bytes, sock=%d", p[3], sock);
6936 "ext too large %d(%d), sock=%d", p[3],
sizeof(buf), sock);
6959 sock, IPPROTO_TCP, TCP_NODELAY, (
char*)&opt,
sizeof(
int));
6964 p[3] =
myDoCommand(p,
sizeof(buf)-1, buf+
sizeof(
int));
6965 if (((
int)p[3]) >= 0)
6967 memcpy(buf, &p[3], 4);
6976 if (write(sock, p, 16) == -1) { }
6999 if (((
int)p[3]) > 0)
7001 if (write(sock, buf, p[3]) == 1) { }
7025 if (saddr->sa_family != AF_INET)
return 0;
7027 addr = ((
struct sockaddr_in *) saddr)->sin_addr.s_addr;
7040 int fdC=0,
c, *sock;
7041 struct sockaddr_storage client;
7042 pthread_attr_t attr;
7044 if (pthread_attr_init(&attr))
7046 "pthread_attr_init failed (%m)");
7048 if (pthread_attr_setstacksize(&attr,
STACK_SIZE))
7050 "pthread_attr_setstacksize failed (%m)");
7052 if (pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED))
7054 "pthread_attr_setdetachstate failed (%m)");
7071 fdC = accept(
fdSock, (
struct sockaddr *)&client, (socklen_t*)&
c);
7077 sock = malloc(
sizeof(
int));
7084 "socket pthread_create failed (%m)");
7116 count = read(fd, str,
sizeof(str)-1);
7122 if (!err)
delete = 0;
7143 fd = open(
PI_LOCKFILE, O_WRONLY|O_CREAT|O_EXCL|O_TRUNC, 0644);
7147 lockResult = flock(fd, LOCK_EX|LOCK_NB);
7151 sprintf(pidStr,
"%d\n", (
int)getpid());
7153 if (write(fd, pidStr, strlen(pidStr)) == -1)
7170 static uint32_t *
initMapMem(
int fd, uint32_t addr, uint32_t len)
7172 return (uint32_t *) mmap(0, len,
7173 PROT_READ|PROT_WRITE|PROT_EXEC,
7174 MAP_SHARED|MAP_LOCKED,
7184 if ((
fdMem = open(
"/dev/mem", O_RDWR | O_SYNC) ) < 0)
7188 "+---------------------------------------------------------+\n" \
7189 "|Sorry, you don't have permission to run this program. |\n" \
7190 "|Try running as root, e.g. precede the command with sudo. |\n" \
7191 "+---------------------------------------------------------+\n\n");
7220 if (
dmaReg == MAP_FAILED)
7236 if (
clkReg == MAP_FAILED)
7246 if (
spiReg == MAP_FAILED)
7251 if (
pwmReg == MAP_FAILED)
7256 if (
pcmReg == MAP_FAILED)
7261 if (
auxReg == MAP_FAILED)
7280 (
int pmapFd,
void *virtualBase,
int basePage,
int pages)
7289 unsigned long long pa;
7295 pageAdr = (uint32_t) dmaVirt[basePage];
7297 index = ((uint32_t)virtualBase /
PAGE_SIZE) * 8;
7299 offset = lseek(pmapFd, index, SEEK_SET);
7301 if (offset != index)
7304 for (n=0; n<pages; n++)
7306 t = read(pmapFd, &pa,
sizeof(pa));
7308 if (t !=
sizeof(pa))
7313 physical = 0x3FFFFFFF & (
PAGE_SIZE * (pa & 0xFFFFFFFF));
7319 dmaVirt[basePage+n] = mmap
7323 PROT_READ|PROT_WRITE,
7324 MAP_SHARED|MAP_FIXED|MAP_LOCKED|MAP_NORESERVE,
7348 PROT_READ|PROT_WRITE,
7349 MAP_SHARED|MAP_ANONYMOUS|MAP_NORESERVE|MAP_LOCKED,
7365 dmaVirt[pageNum] = mmap(
7366 0, (PAGES_PER_BLOCK*PAGE_SIZE),
7367 PROT_READ|PROT_WRITE,
7368 MAP_SHARED|MAP_ANONYMOUS|MAP_NORESERVE|MAP_LOCKED,
7371 if (dmaVirt[pageNum] == MAP_FAILED)
7374 munmap(dmaVirt[pageNum], PAGES_PER_BLOCK*PAGE_SIZE);
7379 while ((trys < 10) && !ok)
7384 PAGES_PER_BLOCK) == 0) ok = 1;
7399 uintptr_t virtualAdr;
7411 virtualAdr = (uintptr_t) dmaMboxBlk[block].virtual_addr;
7416 dmaVirt[page+n] = (
dmaPage_t *) virtualAdr;
7429 int i, servoCycles, superCycles;
7458 PROT_READ|PROT_WRITE,
7459 MAP_PRIVATE|MAP_ANONYMOUS|MAP_LOCKED,
7462 if (dmaVirt == MAP_FAILED)
7467 PROT_READ|PROT_WRITE,
7468 MAP_PRIVATE|MAP_ANONYMOUS|MAP_LOCKED,
7471 if (dmaBus == MAP_FAILED)
7488 PROT_READ|PROT_WRITE,
7489 MAP_PRIVATE|MAP_ANONYMOUS|MAP_LOCKED,
7495 fdPmap = open(
"/proc/self/pagemap", O_RDONLY);
7521 PROT_READ|PROT_WRITE,
7522 MAP_PRIVATE|MAP_ANONYMOUS|MAP_LOCKED,
7525 if (dmaMboxBlk == MAP_FAILED)
7546 (uint32_t)dmaMboxBlk, (uint32_t)
dmaIn);
7550 "gpioReg=%08X pwmReg=%08X pcmReg=%08X clkReg=%08X auxReg=%08X",
7559 fprintf(stderr,
"*** INPUT DMA CONTROL BLOCKS ***\n");
7664 (
int clkCtl,
int clkDiv,
int clkSrc,
int divI,
int divF,
int MASH)
7667 clkCtl, clkDiv, clkSrc, divI, divF, MASH);
7677 while (
clkReg[clkCtl] & CLK_CTL_BUSY);
7693 const unsigned BITS=10;
7695 unsigned clkCtl, clkDiv, clkSrc, clkDivI, clkDivF, clkMash, clkBits;
7720 clkDivI = 50 * micros;
7726 per, clkDivI, clkDivF, clkMash, clkBits);
7728 initHWClk(clkCtl, clkDiv, clkSrc, clkDivI, clkDivF, clkMash);
7738 static void initDMAgo(
volatile uint32_t *dmaAddr, uint32_t cbAddr)
7799 gpioGetSamples.
func = NULL;
7800 gpioGetSamples.
ex = 0;
7802 gpioGetSamples.
bits = 0;
7807 pthread_mutex_init(&wfRx[i].mutex, NULL);
7808 gpioAlert[i].
func = NULL;
7814 gpioInfo [i].
width = 0;
7821 gpioNotify[i].
seqno = 0;
7827 gpioSignal[i].
func = NULL;
7828 gpioSignal[i].
ex = 0;
7835 gpioTimer[i].
func = NULL;
7840 eventAlert[i].
func = NULL;
7841 eventAlert[i].
ignore = 0;
7842 eventAlert[i].
fired = 0;
7863 dmaMboxBlk = MAP_FAILED;
7865 dmaVirt = MAP_FAILED;
7866 dmaBus = MAP_FAILED;
7900 if (gpioTimer[i].running)
7904 pthread_cancel(gpioTimer[i].pthId);
7905 pthread_join(gpioTimer[i].pthId, NULL);
7953 if (dmaBus != MAP_FAILED)
7959 dmaBus = MAP_FAILED;
7961 if (dmaVirt != MAP_FAILED)
7972 dmaVirt = MAP_FAILED;
7986 if (dmaMboxBlk != MAP_FAILED)
8000 dmaMboxBlk = MAP_FAILED;
8056 struct sockaddr_in server;
8057 struct sockaddr_in6 server6;
8060 struct sched_param param;
8061 pthread_attr_t pthAttr;
8092 model = (rev >> 4) & 0xFF;
8116 #ifndef EMBEDDED_IN_VM 8133 param.sched_priority = sched_get_priority_max(SCHED_FIFO);
8136 sched_setscheduler(0, SCHED_FIFO, ¶m);
8142 if (pthread_attr_init(&pthAttr))
8145 if (pthread_attr_setstacksize(&pthAttr,
STACK_SIZE))
8167 if (portStr) port = atoi(portStr);
else port = gpioCfg.
socketPort;
8172 fdSock = socket(AF_INET6, SOCK_STREAM , 0);
8176 bzero((
char *)&server6,
sizeof(server6));
8177 server6.sin6_family = AF_INET6;
8180 server6.sin6_addr = in6addr_loopback;
8184 server6.sin6_addr = in6addr_any;
8186 server6.sin6_port = htons(port);
8188 if (bind(
fdSock,(
struct sockaddr *)&server6,
sizeof(server6)) < 0)
8195 fdSock = socket(AF_INET , SOCK_STREAM , 0);
8200 server.sin_family = AF_INET;
8203 server.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
8207 server.sin_addr.s_addr = htonl(INADDR_ANY);
8209 server.sin_port = htons(port);
8211 if (bind(
fdSock,(
struct sockaddr *)&server ,
sizeof(server)) < 0)
8239 if (bitPos < numBits)
8242 bitp = 7 - (bitPos % 8);
8243 if (buf[bufp] & (1<<bitp))
return 1;
8256 bitp = 7 - (bitPos % 8);
8258 if (bit) buf[bufp] |= (1<<bitp);
8259 else buf[bufp] &= (~(1<<bitp));
8271 return (dmaOVirt[page]->OOL[slot]);
8286 dmaOVirt[page]->
OOL[slot] = value;
8300 return (dmaOVirt[page]->OOL[slot]);
8315 dmaOVirt[page]->
OOL[slot] = value;
8329 return (dmaOVirt[page]->OOL[slot]);
8344 dmaOVirt[page]->
OOL[slot] = value;
8354 if ((wave_id >=0) && (wave_id <
PI_MAX_WAVES))
return waveInfo[wave_id];
8365 gettimeofday(&tv, 0);
8367 t = (double)tv.tv_sec + ((
double)tv.tv_usec / 1E6);
8376 struct timespec ts, rem;
8380 ts.tv_sec = seconds;
8381 ts.tv_nsec = (seconds-(double)ts.tv_sec) * 1E9;
8383 while (clock_nanosleep(CLOCK_REALTIME, 0, &ts, &rem))
8386 ts.tv_sec = rem.tv_sec;
8387 ts.tv_nsec = rem.tv_nsec;
8398 unsigned numWaves, t;
8407 for (i=0; i<numWaves; i++)
8409 fprintf(stderr,
"%10u %08X %08X %08X %10u\n",
8410 t, waves[i].gpioOn, waves[i].gpioOff,
8411 waves[i].flags, waves[i].usDelay);
8429 fprintf(stderr,
"p%d=%d ", i, gpioScript[script_id].script.par[i]);
8432 fprintf(stderr,
"\n");
8436 fprintf(stderr,
"v%d=%d ", i, gpioScript[script_id].script.var[i]);
8439 fprintf(stderr,
"\n");
8444 "c%d=[%d, %d(%d), %d(%d), %d, %d]\n",
8446 gpioScript[script_id].script.instr[i].p[0],
8514 #ifndef EMBEDDED_IN_VM 8519 "\n#####################################################\n");
8520 fprintf(stderr,
"pigpio version=%d internals=%X\n",
8524 "micros=%d allocMode=%d dmaInitCbs=%d DMARestarts=%d\n",
8529 "samples %u maxSamples %u maxEmit %u emitFrags %u\n",
8533 fprintf(stderr,
"cbTicks %d, cbCalls %u\n",
8536 fprintf(stderr,
"pipe: good %u, short %u, would block %u\n",
8540 fprintf(stderr,
"alertTicks %u, lateTicks %u, moreToDo %u\n",
8544 fprintf(stderr,
"%9u ", gpioStats.
diffTick[i]);
8547 "\n#####################################################\n\n\n");
8560 switch (gpioInfo[gpio].is)
8565 gpioInfo[gpio].
width = 0;
8571 gpioInfo[gpio].
width = 0;
8576 gpioInfo[gpio].
width = 0;
8581 gpioInfo[gpio].
width = 0;
8594 pwm = (
PWMDef[i] >> 4) & 3;
8599 gpioInfo[i].
width = 0;
8609 int reg, shift, old_mode;
8622 shift = (gpio%10) * 3;
8624 old_mode = (
gpioReg[reg] >> shift) & 7;
8626 if (mode != old_mode)
8653 shift = (gpio%10) * 3;
8655 return (
gpioReg[reg] >> shift) & 7;
8753 if (val > gpioInfo[gpio].range)
8769 gpioInfo[gpio].
width=val;
8787 switch (gpioInfo[gpio].is)
8790 return gpioInfo[gpio].
width;
8793 pwm = (
PWMDef[gpio] >> 4) & 3;
8809 int oldWidth, newWidth;
8821 oldWidth = gpioInfo[gpio].
width;
8827 newWidth = (range * oldWidth) / gpioInfo[gpio].range;
8830 gpioInfo[gpio].
range = range;
8831 gpioInfo[gpio].
width = newWidth;
8836 gpioInfo[gpio].
range = range;
8855 switch (gpioInfo[gpio].is)
8862 return gpioInfo[gpio].
range;
8880 switch (gpioInfo[gpio].is)
8883 pwm = (
PWMDef[gpio] >> 4) & 3;
8900 unsigned diff, best, idx;
8902 DBG(
DBG_USER,
"gpio=%d frequency=%d", gpio, frequency);
8909 if (frequency >
pwmFreq[0]) idx = 0;
8919 else diff = pwmFreq[i] - frequency;
8929 width = gpioInfo[gpio].
width;
8951 unsigned pwm, clock;
8960 switch (gpioInfo[gpio].is)
8963 pwm = (
PWMDef[gpio] >> 4) & 3;
8967 clock = (
clkDef[gpio] >> 4) & 3;
8980 DBG(
DBG_USER,
"gpio=%d pulsewidth=%d", gpio, val);
8989 "gpio %d, bad pulsewidth (%d)", gpio, val);
8993 "gpio %d, bad pulsewidth (%d)", gpio, val);
9008 gpioInfo[gpio].
width=val;
9028 return gpioInfo[gpio].
width;
9088 DBG(
DBG_USER,
"numPulses=%u pulses=%08X", numPulses, (uint32_t)pulses);
9097 for (p=0; p<numPulses; p++)
9099 wf[2][p].
gpioOff = pulses[p].gpioOff;
9100 wf[2][p].
gpioOn = pulses[p].gpioOn;
9101 wf[2][p].
usDelay = pulses[p].usDelay;
9119 int i, b, p, lev,
c,
v;
9121 uint16_t *wstr = (uint16_t *)bstr;
9122 uint32_t *lstr = (uint32_t *)bstr;
9124 unsigned bitDelay[32];
9127 "gpio=%d baud=%d bits=%d stops=%d offset=%d numBytes=%d str=[%s]",
9128 gpio, baud, data_bits, stop_bits, offset,
9129 numBytes,
myBuf2Str(numBytes, (
char *)bstr));
9146 "bad number of (half) stop bits (%d)", stop_bits);
9157 if (data_bits > 8) numBytes /= 2;
9158 if (data_bits > 16) numBytes /= 2;
9160 if (!numBytes)
return 0;
9166 wf[2][p].
gpioOn = (1<<gpio);
9170 if (offset > bitDelay[0]) wf[2][p].
usDelay = offset;
9171 else wf[2][p].
usDelay = bitDelay[0];
9173 for (i=0; i<numBytes; i++)
9181 wf[2][p].
usDelay = bitDelay[0];
9186 if (data_bits < 9) c = bstr[i];
9187 else if (data_bits < 17) c = wstr[i];
9190 for (b=0; b<data_bits; b++)
9192 if (c & (1<<b)) v=1;
else v=0;
9194 if (v == lev) wf[2][p].
usDelay += bitDelay[b+1];
9203 wf[2][p].
gpioOn = (1<<gpio);
9214 wf[2][p].
usDelay = bitDelay[b+1];
9220 if (lev) wf[2][p].
usDelay += bitDelay[data_bits+1];
9225 wf[2][p].
gpioOn = (1<<gpio);
9227 wf[2][p].
usDelay = bitDelay[data_bits+1];
9234 wf[2][p].
gpioOn = (1<<gpio);
9236 wf[2][p].
usDelay = bitDelay[0];
9250 unsigned spiBitFirst,
9251 unsigned spiBitLast,
9254 int p, bit, dbv, halfbit;
9255 int rising_edge[2], read_cycle[2];
9256 uint32_t on_bits, off_bits;
9260 "spi=%08X off=%d spiSS=%d tx=%08X, num=%d fb=%d lb=%d spiBits=%d",
9261 (uint32_t)spi, offset, spiSS, (uint32_t)buf, spiTxBits,
9262 spiBitFirst, spiBitLast, spiBits);
9277 if (spi->clk_pol) {rising_edge[0] = 0; rising_edge[1] = 1;}
9278 else {rising_edge[0] = 1; rising_edge[1] = 0;}
9280 if (spi->clk_pha) {read_cycle[0] = 0; read_cycle[1] = 1;}
9281 else {read_cycle[0] = 1; read_cycle[1] = 0;}
9303 on_bits |= (1<<(spi->mosi));
9308 off_bits |= (1<<(spi->mosi));
9312 if (!spi->clk_pha) tx_bit_pos ++;
9314 if (spi->ss_pol) off_bits |= (1<<spiSS);
9315 else on_bits |= (1<<spiSS);
9317 if (spi->clk_pol) on_bits |= (1<<(spi->clk));
9318 else off_bits |= (1<<(spi->clk));
9320 wf[2][p].
gpioOn = on_bits;
9324 if (spi->clk_us > spi->ss_us) wf[2][p].
usDelay = spi->clk_us;
9325 else wf[2][p].
usDelay = spi->ss_us;
9329 for (bit=1; bit<=spiBits; bit++)
9331 for (halfbit=0; halfbit<2; halfbit++)
9333 wf[2][p].
usDelay = spi->clk_us;
9339 if (read_cycle[halfbit])
9341 if ((bit>=spiBitFirst) && (bit<=spiBitLast))
9348 if (!dbv) on_bits |= (1<<(spi->mosi));
9353 if (dbv) off_bits |= (1<<(spi->mosi));
9360 if (rising_edge[halfbit]) on_bits |= (1<<(spi->clk));
9361 else off_bits |= (1<<(spi->clk));
9363 wf[2][p].
gpioOn = on_bits;
9373 if (spi->ss_pol) on_bits |= (1<<spiSS);
9374 else off_bits |= (1<<spiSS);
9376 wf[2][p].
gpioOn = on_bits;
9391 int numCB, numBOOL, numTOOL;
9410 if (waveInfo[i].deleted &&
9411 (waveInfo[i].numCB == numCB) &&
9412 (waveInfo[i].numBOOL == numBOOL) &&
9413 (waveInfo[i].numTOOL == numTOOL))
9434 wid = waveOutCount++;
9440 waveInfo[wid].
numCB = numCB;
9441 waveInfo[wid].
numBOOL = numBOOL;
9442 waveInfo[wid].
numTOOL = numTOOL;
9451 CB = waveInfo[wid].
botCB;
9452 BOOL = waveInfo[wid].
botOOL;
9453 TOOL = waveInfo[wid].
topOOL;
9459 if ( (numCB != (CB-waveInfo[wid].botCB)) ||
9460 (numBOOL != (BOOL-waveInfo[wid].botOOL)) ||
9461 (numTOOL != (waveInfo[wid].topOOL-TOOL)) )
9463 DBG(
DBG_ALWAYS,
"ERROR wid=%d CBs %d=%d BOOL %d=%d TOOL %d=%d", wid,
9464 numCB, CB-waveInfo[wid].botCB,
9465 numBOOL, BOOL-waveInfo[wid].botOOL,
9466 numTOOL, waveInfo[wid].topOOL-TOOL);
9490 if ((wave_id >=
waveOutCount) || waveInfo[wave_id].deleted)
9493 waveInfo[wave_id].
deleted = 1;
9499 while ((wave_id > 0) && (waveInfo[wave_id-1].deleted)) --wave_id;
9528 DBG(
DBG_USER,
"wave_id=%d wave_mode=%d", wave_id, wave_mode);
9532 if ((wave_id >=
waveOutCount) || waveInfo[wave_id].deleted)
9578 return (waveInfo[wave_id].topCB - waveInfo[wave_id].botCB) + 1;
9621 return (uint32_t) (p + index);
9684 for (b=0; b < (blocks*(blklen+1)); b++)
chainSetCntVal(counter, b, repeat);
9686 for (b=0; b<blocks; b++)
9688 ((b*(blklen+1))+blklen),
9691 for (b=0; b<blocks; b++)
9739 p->
length = blocks*(blklen+1)*4;
9744 while (count && (b<blocks))
9746 dig = count % blklen;
9758 for (b=0; b < (blocks*(blklen+1)); b++)
9766 unsigned blklen=16, blocks=4;
9769 int i, wid, cmd, loop, counters;
9770 unsigned cycles, delayCBs, dcb, delayLeft;
9771 uint32_t repeat, next, *endPtr;
9772 int stk_pos[10], stk_lev=0;
9811 p->
src = (uint32_t) (&dmaOBus[0]->periphData);
9822 wid = (unsigned)buf[i];
9826 if ((i+2) > bufSize)
9828 "incomplete chain command (at %d)", i);
9834 if (stk_lev >= (
sizeof(stk_pos)/
sizeof(
int)))
9836 "chain counters nested too deep (at %d)", i);
9838 stk_pos[stk_lev++] =
cb;
9846 "too many chain counters (at %d)", i);
9848 if ((i+4) > bufSize)
9850 "incomplete chain command (at %d)", i);
9853 if (--stk_lev >= 0) loop = stk_pos[stk_lev];
9855 if ((loop < 1) || (loop == cb))
9857 "empty chain loop (at %d)", i);
9859 cycles = ((unsigned)buf[i+3] << 8) + (unsigned)buf[i+2];
9865 "bad chain loop count (%d)", cycles);
9876 else if (cycles == 1)
9901 p->
src = (uint32_t) (&dmaOBus[0]->periphData);
9902 p->
dst = (uint32_t) (&dmaOBus[0]->periphData);
9907 cycles-1, repeat, next);
9914 if ((i+4) > bufSize)
9916 "incomplete chain command (at %d)", i);
9918 cycles = ((unsigned)buf[i+3] << 8) + (unsigned)buf[i+2];
9924 "bad chain delay micros (%d)", cycles);
9930 for (dcb=0; dcb<delayCBs; dcb++)
9953 p->
src = (uint32_t) (&dmaOBus[0]->periphData);
9974 if (--stk_lev >= 0) loop = stk_pos[stk_lev];
9976 if ((loop < 1) || (loop == cb))
9978 "empty chain loop (at %d)", i);
9986 "loop forever must be last command");
9992 p->
src = (uint32_t) (&dmaOBus[0]->periphData);
9993 p->
dst = (uint32_t) (&dmaOBus[0]->periphData);
10000 "unknown chain command (255 %d)", cmd);
10002 else if ((wid >=
waveOutCount) || waveInfo[wid].deleted)
10040 p->
src = (uint32_t) (&dmaOBus[0]->periphData);
10041 p->
dst = (uint32_t) (&dmaOBus[0]->periphData);
10045 if (!endPtr) endPtr = &p->
next;
10080 if (cb < 0)
return -
cb;
10084 if ( !waveInfo[i].deleted &&
10085 (cb >= waveInfo[i].botCB) &&
10086 (cb <= waveInfo[i].topCB) )
return i;
10182 return wfStats.
cbs;
10239 uint32_t now, max_stretch=100000;
10305 for(bit=0; bit<8; bit++)
10320 for (bit=0; bit<8; bit++)
10334 DBG(
DBG_USER,
"SDA=%d SCL=%d baud=%d", SDA, SCL, baud);
10346 "SDA %d, bad baud rate (%d)", SDA, baud);
10354 wfRx[SDA].
gpio = SDA;
10356 wfRx[SDA].
baud = baud;
10359 wfRx[SDA].
I.
SDA = SDA;
10360 wfRx[SDA].
I.
SCL = SCL;
10361 wfRx[SDA].
I.
delay = 500000 / baud;
10365 wfRx[SCL].
gpio = SCL;
10385 switch(wfRx[SDA].
mode)
10417 int i, ack, inPos, outPos,
status, bytes;
10418 int addr, flags, esc, setesc;
10421 DBG(
DBG_USER,
"gpio=%d inBuf=%s outBuf=%08X len=%d",
10422 SDA,
myBuf2Str(inLen, (
char *)inBuf), (
int)outBuf, outLen);
10432 if (!inBuf || !inLen)
10435 if (!outBuf && outLen)
10451 while (!status && (inPos < inLen))
10453 DBG(
DBG_INTERNAL,
"status=%d inpos=%d inlen=%d cmd=%d addr=%d flags=%x",
10454 status, inPos, inLen, inBuf[inPos], addr, flags);
10456 switch (inBuf[inPos++])
10490 if (bytes >= 0) ack =
I2CPutByte(w, (addr<<1)|1);
10496 if ((bytes + outPos) <= outLen)
10498 for (i=0; i<(bytes-1); i++)
10515 if (bytes >= 0) ack =
I2CPutByte(w, addr<<1);
10521 if ((bytes + inPos) <= inLen)
10523 for (i=0; i<(bytes-1); i++)
10540 if (setesc) esc = 1;
else esc = 0;
10547 if (status >= 0) status = outPos;
10590 static int bscMode = 0;
10595 DBG(
DBG_USER,
"control=0x%X (sa=0x%X, cr=0x%X) tx=%d [%s]",
10612 if (xfer->
control & 2) mode = 2;
10615 if (mode > bscMode)
10623 if (bscMode)
bscTerm(bscMode);
10640 while ((copied < xfer->txCnt) &&
10666 return (copied<<16) |
bscFR;
10720 uint8_t bit, rxByte=0;
10730 for (bit=0; bit<8; bit++)
10769 for (bit=0; bit<8; bit++)
10807 unsigned CS,
unsigned MISO,
unsigned MOSI,
unsigned SCLK,
10808 unsigned baud,
unsigned spiFlags)
10813 DBG(
DBG_USER,
"CS=%d MISO=%d MOSI=%d SCLK=%d baud=%d flags=%d",
10814 CS, MISO, MOSI, SCLK, baud, spiFlags);
10835 "CS %d is already being used, mode %d", CS, wfRx[CS].
mode);
10841 bits = (1<<CS) | (1<<MISO) | (1<<MOSI) | (1<<SCLK);
10843 if (__builtin_popcount(bits) == 4)
10865 "GPIO already being used (%d=%d %d=%d, %d=%d %d=%d)",
10867 MISO, wfRx[MISO].mode,
10868 MOSI, wfRx[MOSI].mode,
10869 SCLK, wfRx[SCLK].mode);
10873 wfRx[CS].
baud = baud;
10875 wfRx[CS].
S.
CS = CS;
10876 wfRx[CS].
S.
SCLK = SCLK;
10879 wfRx[CS].
S.
delay = (500000 / baud) - 1;
10903 wfRx[SCLK].
S.
SCLK = SCLK;
10904 wfRx[SCLK].
S.
MISO = MISO;
10905 wfRx[SCLK].
S.
MOSI = MOSI;
10932 switch(wfRx[CS].
mode)
10939 SCLK = wfRx[CS].
S.
SCLK;
10941 if (--wfRx[SCLK].S.usage <= 0)
10977 DBG(
DBG_USER,
"CS=%d inBuf=%s outBuf=%08X count=%d",
10978 CS,
myBuf2Str(count, (
char *)inBuf), (
int)outBuf, count);
10988 if (!inBuf || !count)
10991 if (!outBuf && count)
10994 SCLK = wfRx[CS].
S.
SCLK;
10996 wfRx[SCLK].
S.
CS = CS;
11007 for (pos=0; pos < count; pos++)
11023 int bitTime, timeout;
11025 DBG(
DBG_USER,
"gpio=%d baud=%d data_bits=%d", gpio, baud, data_bits);
11034 "gpio %d, bad baud rate (%d)", gpio, baud);
11039 "gpio %d, bad data bits (%d)", gpio, data_bits);
11044 bitTime = (1000 *
MILLION) / baud;
11046 timeout = ((data_bits+2) * bitTime)/
MILLION;
11048 if (timeout < 1) timeout = 1;
11050 wfRx[gpio].
gpio = gpio;
11052 wfRx[gpio].
baud = baud;
11058 wfRx[gpio].
s.
halfBit = (bitTime/2)+500;
11061 wfRx[gpio].
s.
bit = -1;
11065 if (data_bits < 9) wfRx[gpio].
s.
bytes = 1;
11066 else if (data_bits < 17) wfRx[gpio].
s.
bytes = 2;
11067 else wfRx[gpio].
s.
bytes = 4;
11078 DBG(
DBG_USER,
"gpio=%d invert=%d", gpio, invert);
11091 "bad invert level for gpio %d (%d)", gpio, invert);
11093 wfRx[gpio].
s.
invert = invert;
11102 unsigned bytes=0, wpos;
11105 DBG(
DBG_USER,
"gpio=%d buf=%08X bufSize=%d", gpio, (
int)buf, bufSize);
11127 if (bytes > bufSize) bytes = bufSize;
11154 switch(wfRx[gpio].
mode)
11164 free(wfRx[gpio].
s.buf);
11188 event, (uint32_t)f, user, (uint32_t)userdata);
11190 eventAlert[event].
ex = user;
11191 eventAlert[event].
userdata = userdata;
11193 eventAlert[event].
func = f;
11203 DBG(
DBG_USER,
"event=%d function=%08X", event, (uint32_t)f);
11220 DBG(
DBG_USER,
"event=%d function=%08X userdata=%08X",
11221 event, (uint32_t)f, (uint32_t)userdata);
11238 DBG(
DBG_USER,
"handle=%d bits=%08X", handle, bits);
11265 eventAlert[event].
fired = 1;
11280 gpio, (uint32_t)f, user, (uint32_t)userdata);
11282 gpioAlert[gpio].
ex = user;
11283 gpioAlert[gpio].
userdata = userdata;
11285 gpioAlert[gpio].
func = f;
11305 DBG(
DBG_USER,
"gpio=%d function=%08X", gpio, (uint32_t)f);
11322 DBG(
DBG_USER,
"gpio=%d function=%08X userdata=%08X",
11323 gpio, (uint32_t)f, (uint32_t)userdata);
11346 DBG(
DBG_USER,
"gpio=%d edge=%d timeout=%d f=%x u=%d data=%x",
11350 sprintf(buf,
"/sys/class/gpio/gpio%d/value", isr->
gpio);
11352 if ((fd = open(buf, O_RDONLY)) < 0)
11360 pfd.events = POLLPRI;
11362 lseek(fd, 0, SEEK_SET);
11363 if (read(fd, buf,
sizeof buf) == -1) { }
11367 retval = poll(&pfd, 1, isr->
timeout);
11375 lseek(fd, 0, SEEK_SET);
11376 if (read(fd, buf,
sizeof buf) == -1) { }
11385 else (isr->
func)(isr->
gpio, level, tick);
11405 char *edge_str[]={
"rising\n",
"falling\n",
"both\n"};
11410 "gpio=%d edge=%d timeout=%d function=%08X user=%d userdata=%08X",
11411 gpio, edge, timeout, (uint32_t)f, user, (uint32_t)userdata);
11415 if (!gpioISR[gpio].inited)
11417 fd = open(
"/sys/class/gpio/export", O_WRONLY);
11421 sprintf(buf,
"%d\n", gpio);
11422 err = write(fd, buf, strlen(buf));
11425 sprintf(buf,
"/sys/class/gpio/gpio%d/direction", gpio);
11426 fd = open(buf, O_WRONLY);
11429 err = write(fd,
"in\n", 3);
11433 gpioISR[gpio].
gpio = gpio;
11434 gpioISR[gpio].
edge = -1;
11437 gpioISR[gpio].
inited = 1;
11440 if (gpioISR[gpio].edge != edge)
11442 sprintf(buf,
"/sys/class/gpio/gpio%d/edge", gpio);
11443 fd = open(buf, O_WRONLY);
11446 err = write(fd, edge_str[edge], strlen(edge_str[edge]));
11450 gpioISR[gpio].
edge = edge;
11452 if (gpioISR[gpio].pth != NULL)
11453 pthread_kill(*gpioISR[gpio].pth, SIGCHLD);
11456 if (timeout <= 0) timeout = -1;
11457 if (gpioISR[gpio].timeout != timeout)
11459 gpioISR[gpio].
timeout = timeout;
11461 if (gpioISR[gpio].pth != NULL)
11462 pthread_kill(*gpioISR[gpio].pth, SIGCHLD);
11465 gpioISR[gpio].
func = f;
11466 gpioISR[gpio].
ex = user;
11467 gpioISR[gpio].
userdata = userdata;
11469 if (gpioISR[gpio].pth == NULL)
11474 if (gpioISR[gpio].pth)
11477 gpioISR[gpio].
func = NULL;
11478 gpioISR[gpio].
pth = NULL;
11481 if (gpioISR[gpio].inited)
11483 fd = open(
"/sys/class/gpio/unexport", O_WRONLY);
11485 sprintf(buf,
"%d\n", gpio);
11486 err = write(fd, buf, strlen(buf));
11489 gpioISR[gpio].
inited = 0;
11504 DBG(
DBG_USER,
"gpio=%d edge=%d timeout=%d function=%08X",
11505 gpio, edge, timeout, (uint32_t)f);
11528 DBG(
DBG_USER,
"gpio=%d edge=%d timeout=%d function=%08X userdata=%08X",
11529 gpio, edge, timeout, (uint32_t)f, (uint32_t)userdata);
11552 (gpioNotify[i].fd == fd))
11554 DBG(
DBG_USER,
"closed orphaned fd=%d (handle=%d)", fd, i);
11565 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
11566 if (lock) pthread_mutex_lock(&mutex);
11567 else pthread_mutex_unlock(&mutex);
11599 sprintf(name,
"/dev/pigpio%d", slot);
11603 fd = open(name, O_RDWR|O_NONBLOCK);
11613 i = fcntl(fd, F_SETPIPE_SZ, bufSize);
11618 "fcntl %s size %d failed (%m)", name, bufSize);
11622 gpioNotify[slot].
seqno = 0;
11623 gpioNotify[slot].
bits = 0;
11624 gpioNotify[slot].
fd = fd;
11625 gpioNotify[slot].
pipe = 1;
11668 gpioNotify[slot].
seqno = 0;
11669 gpioNotify[slot].
bits = 0;
11670 gpioNotify[slot].
fd = fd;
11671 gpioNotify[slot].
pipe = 0;
11735 bits |= gpioNotify[i].
bits;
11749 DBG(
DBG_USER,
"handle=%d bits=%08X", handle, bits);
11822 DBG(
DBG_USER,
"gpio=%d pulseLen=%d level=%d", gpio, pulseLen, level);
11834 "gpio %d, bad pulseLen (%d)", gpio, pulseLen);
11852 DBG(
DBG_USER,
"gpio=%d timeout=%d", gpio, timeout);
11861 "gpio %d, bad timeout (%d)", gpio, timeout);
11866 if (timeout)
wdogBits |= (1<<gpio);
11876 DBG(
DBG_USER,
"gpio=%d steady=%d active=%d", gpio, steady, active);
11906 DBG(
DBG_USER,
"gpio=%d steady=%d", gpio, steady);
11922 gpioAlert[gpio].
gfLBitV = (1<<gpio);
11923 gpioAlert[gpio].
gfRBitV = 0 ;
11927 gpioAlert[gpio].
gfLBitV = 0 ;
11928 gpioAlert[gpio].
gfRBitV = (1<<gpio);
11944 DBG(
DBG_USER,
"function=%08X bits=%08X", (uint32_t)f, bits);
11948 gpioGetSamples.
ex = 0;
11950 gpioGetSamples.
func = f;
11952 if (f) gpioGetSamples.
bits = bits;
11953 else gpioGetSamples.
bits = 0;
11967 DBG(
DBG_USER,
"function=%08X bits=%08X", (uint32_t)f, bits);
11971 gpioGetSamples.
ex = 1;
11972 gpioGetSamples.
userdata = userdata;
11973 gpioGetSamples.
func = f;
11975 if (f) gpioGetSamples.
bits = bits;
11976 else gpioGetSamples.
bits = 0;
11992 pthread_attr_t pthAttr;
11994 DBG(
DBG_INTERNAL,
"id=%d millis=%d function=%08X user=%d userdata=%08X",
11995 id, millis, (uint32_t)f, user, (uint32_t)userdata);
11997 gpioTimer[id].
id = id;
12001 gpioTimer[id].
func = f;
12002 gpioTimer[id].
ex = user;
12003 gpioTimer[id].
userdata = userdata;
12004 gpioTimer[id].
millis = millis;
12006 if (!gpioTimer[
id].running)
12008 if (pthread_attr_init(&pthAttr))
12010 "pthread_attr_init failed (%m)");
12012 if (pthread_attr_setstacksize(&pthAttr,
STACK_SIZE))
12014 "pthread_attr_setstacksize failed (%m)");
12016 if (pthread_create(
12017 &gpioTimer[
id].pthId, &pthAttr,
pthTimerTick, &gpioTimer[
id]))
12019 "timer %d, create failed (%m)",
id);
12026 if (gpioTimer[
id].running)
12031 if (pthread_self() == gpioTimer[
id].pthId)
12034 gpioTimer[id].
func = 0;
12035 pthread_exit(NULL);
12039 if (pthread_cancel(gpioTimer[
id].pthId))
12042 if (pthread_join(gpioTimer[
id].pthId, NULL))
12046 gpioTimer[id].
func = 0;
12059 DBG(
DBG_USER,
"id=%d millis=%d function=%08X",
id, millis, (uint32_t)f);
12080 DBG(
DBG_USER,
"id=%d millis=%d function=%08X, userdata=%08X",
12081 id, millis, (uint32_t)f, (uint32_t)userdata);
12101 pthread_attr_t pthAttr;
12103 DBG(
DBG_USER,
"f=%08X, userdata=%08X", (uint32_t)f, (uint32_t)userdata);
12107 pth = malloc(
sizeof(pthread_t));
12111 if (pthread_attr_init(&pthAttr))
12114 SOFT_ERROR(NULL,
"pthread_attr_init failed");
12117 if (pthread_attr_setstacksize(&pthAttr,
STACK_SIZE))
12120 SOFT_ERROR(NULL,
"pthread_attr_setstacksize failed");
12123 if (pthread_create(pth, &pthAttr, f, userdata))
12142 if (pthread_self() == *pth)
12145 pthread_exit(NULL);
12149 pthread_cancel(*pth);
12150 pthread_join(*pth, NULL);
12160 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
12170 pthread_mutex_lock(&mutex);
12182 pthread_mutex_unlock(&mutex);
12186 s = &gpioScript[slot];
12195 pthread_cond_init(&s->
pthCond, NULL);
12196 pthread_mutex_init(&s->
pthMutex, NULL);
12223 DBG(
DBG_USER,
"script_id=%d numParam=%d param=%08X",
12224 script_id, numParam, (uint32_t)param);
12236 pthread_mutex_lock(&gpioScript[script_id].pthMutex);
12240 if ((numParam > 0) && (param != 0))
12242 memcpy(gpioScript[script_id].script.par, param,
12243 sizeof(uint32_t) * numParam);
12248 pthread_cond_signal(&gpioScript[script_id].pthCond);
12255 pthread_mutex_unlock(&gpioScript[script_id].pthMutex);
12270 DBG(
DBG_USER,
"script_id=%d numParam=%d param=%08X",
12271 script_id, numParam, (uint32_t)param);
12283 if ((numParam > 0) && (param != 0))
12285 memcpy(gpioScript[script_id].script.par, param,
12286 sizeof(uint32_t) * numParam);
12302 DBG(
DBG_USER,
"script_id=%d param=%08X", script_id, (uint32_t)param);
12313 memcpy(param, gpioScript[script_id].script.par,
12317 return gpioScript[script_id].
run_state;
12336 pthread_mutex_lock(&gpioScript[script_id].pthMutex);
12342 pthread_cond_signal(&gpioScript[script_id].pthCond);
12345 pthread_mutex_unlock(&gpioScript[script_id].pthMutex);
12367 pthread_mutex_lock(&gpioScript[script_id].pthMutex);
12373 pthread_cond_signal(&gpioScript[script_id].pthCond);
12376 pthread_mutex_unlock(&gpioScript[script_id].pthMutex);
12385 if (gpioScript[script_id].script.par)
12386 free(gpioScript[script_id].script.par);
12388 gpioScript[script_id].
script.
par = NULL;
12403 DBG(
DBG_USER,
"signum=%d function=%08X", signum, (uint32_t)f);
12410 gpioSignal[signum].
ex = 0;
12411 gpioSignal[signum].
userdata = NULL;
12413 gpioSignal[signum].
func = f;
12424 DBG(
DBG_USER,
"signum=%d function=%08X userdata=%08X",
12425 signum, (uint32_t)f, (uint32_t)userdata);
12432 gpioSignal[signum].
ex = 1;
12433 gpioSignal[signum].
userdata = userdata;
12435 gpioSignal[signum].
func = f;
12528 unsigned clock,
mode, mash;
12533 DBG(
DBG_USER,
"gpio=%d frequency=%d", gpio, frequency);
12537 if ((gpio >> 24) == 0x5A) password = 1;
12551 "bad hardware clock frequency (%d)", frequency);
12553 clock = (
clkDef[gpio] >> 4) & 3;
12555 if ((clock == 1) && (!password))
12557 "Need password to use clock 1 (%d)", gpio);
12559 mode =
clkDef[gpio] & 7;
12566 if (clkInf.
frac == 0) mash = 0;
12575 f = (double) cfreq[clkInf.
clock] /
12576 ((
double)clkInf.
div + ((double)clkInf.
frac / 4096.0));
12586 "bad hardware clock frequency (%d)", frequency);
12604 unsigned gpio,
unsigned frequency,
unsigned dutycycle)
12606 uint32_t old_PWM_CTL;
12607 unsigned pwm,
mode;
12608 uint32_t real_range, real_dutycycle;
12610 DBG(
DBG_USER,
"gpio=%d frequency=%d dutycycle=%d",
12611 gpio, frequency, dutycycle);
12628 "bad hardware PWM frequency (%d)", frequency);
12633 pwm = (
PWMDef[gpio] >> 4) & 3;
12634 mode =
PWMDef[gpio] & 7;
12638 real_range = ((double)
CLK_PLLD_FREQ / (2.0 * frequency)) + 0.5;
12639 real_dutycycle = ((uint64_t)dutycycle * real_range) /
PI_HW_PWM_RANGE;
12719 DBG(
DBG_USER,
"pad=%d padStrength=%d", pad, padStrength);
12752 strength =
padsReg[11+pad] & 7;
12760 int shell(
char *scriptName,
char *scriptString)
12765 DBG(
DBG_USER,
"name=%s string=%s", scriptName, scriptString);
12772 snprintf(buf,
sizeof(buf),
12773 "/opt/pigpio/cgi/%s %s", scriptName, scriptString);
12777 status = system(buf);
12798 f = fopen(
"/opt/pigpio/access",
"r");
12807 if (fgets(line,
sizeof(line), f))
12809 sscanf(line,
" %511s %c%c", buffer, &perm, &term);
12814 if (fnmatch(buffer, filename, 0) == 0)
12818 if (fnmatch(match, buffer, 0) == 0)
12820 strcpy(match, buffer);
12826 strcpy(match, buffer);
12838 switch (toupper(mperm))
12852 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
12854 int i, slot, oflag, omode;
12855 struct stat statbuf;
12871 pthread_mutex_lock(&mutex);
12883 pthread_mutex_unlock(&mutex);
12899 omode |= (S_IRUSR|S_IWUSR);
12908 switch(mode&PI_FILE_RW)
12911 fd = open(file, O_RDONLY|oflag, omode);
12915 fd = open(file, O_WRONLY|oflag, omode);
12919 fd = open(file, O_RDWR|oflag, omode);
12930 if (stat(file, &statbuf) == 0)
12932 if (S_ISDIR(statbuf.st_mode))
12941 fileInfo[slot].
fd = fd;
12960 if (fileInfo[handle].fd >= 0) close(fileInfo[handle].fd);
12989 w = write(fileInfo[handle].fd, buf, count);
12993 if (w == -1)
DBG(
DBG_USER,
"write failed with errno %d", errno);
13004 DBG(
DBG_USER,
"handle=%d count=%d buf=0x%X", handle, count, (
unsigned)buf);
13020 r = read(fileInfo[handle].fd, buf, count);
13024 DBG(
DBG_USER,
"read failed with errno %d", errno);
13040 handle, seekOffset, seekFrom);
13068 s = lseek(fileInfo[handle].fd, seekOffset, whence);
13072 DBG(
DBG_USER,
"seek failed with errno %d", errno);
13085 DBG(
DBG_USER,
"fpat=%s count=%d buf=%x", fpat, count, (
unsigned)buf);
13094 if (glob(fpat, GLOB_MARK, NULL, &pglob) == 0)
13096 for (i=0; i<pglob.gl_pathc; i++)
13098 len = strlen(pglob.gl_pathv[i]);
13099 if ((bufpos + len + 1) < count)
13101 strcpy(buf+bufpos, pglob.gl_pathv[i]);
13103 buf[bufpos++] =
'\n';
13123 struct timespec ts;
13125 DBG(
DBG_USER,
"timetype=%d &seconds=%08X µs=%08X",
13126 timetype, (uint32_t)seconds, (uint32_t)micros);
13135 clock_gettime(CLOCK_REALTIME, &ts);
13136 *seconds = ts.tv_sec;
13137 *micros = ts.tv_nsec/1000;
13141 clock_gettime(CLOCK_REALTIME, &ts);
13145 *seconds = ts.tv_sec;
13146 *micros = ts.tv_nsec/1000;
13157 struct timespec ts, rem;
13159 DBG(
DBG_USER,
"timetype=%d seconds=%d micros=%d",
13160 timetype, seconds, micros);
13170 if ((micros < 0) || (micros > 999999))
13173 ts.tv_sec = seconds;
13174 ts.tv_nsec = micros * 1000;
13178 while (clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME, &ts, &rem));
13182 while (clock_nanosleep(CLOCK_REALTIME, 0, &ts, &rem))
13185 ts.tv_sec = rem.tv_sec;
13186 ts.tv_nsec = rem.tv_nsec;
13261 static unsigned rev = 0;
13269 if (rev)
return rev;
13273 filp = fopen (
"/proc/cpuinfo",
"r");
13277 while (fgets(buf,
sizeof(buf), filp) != NULL)
13281 if (!strncasecmp(
"model name", buf, 10))
13283 if (strstr (buf,
"ARMv6") != NULL)
13290 else if (strstr (buf,
"ARMv7") != NULL)
13297 else if (strstr (buf,
"ARMv8") != NULL)
13307 if (!strncasecmp(
"revision\t:", buf, 10))
13309 if (sscanf(buf+10,
"%x%c", &rev, &term) == 2)
13311 if (term !=
'\n') rev = 0;
13312 else rev &= 0xFFFFFF;
13344 DBG(
DBG_USER,
"micros=%d peripheral=%d", micros, peripheral);
13348 if ((micros < 1) || (micros > 10))
13351 if (!clkCfg[micros].valid)
13385 DBG(
DBG_USER,
"primary channel=%d, secondary channel=%d",
13386 primaryChannel, secondaryChannel);
13395 (secondaryChannel == primaryChannel))
13410 DBG(
DBG_USER,
"gpio update mask=%llX", updateMask);
13479 numSockAddr, (
unsigned)sockAddr);
13489 for (i=0; i<numSockAddr; i++)
sockNetAddr[i] = sockAddr[i];
13516 DBG(
DBG_USER,
"cfgWhat=%u, cfgVal=%d", cfgWhat, cfgVal);
13553 #include "custom.cext" static void * pthTimerTick(void *x)
void(* gpioISRFunc_t)(int gpio, int level, uint32_t tick)
#define PI_I2C_FUNC_SMBUS_READ_BYTE_DATA
int fileRead(unsigned handle, char *buf, unsigned count)
#define PI_SPI_FLAGS_GET_CSPOL(x)
#define PI_FIL_OPEN_FAILED
static volatile uint32_t nFilterBits
unsigned DMAsecondaryChannel
static void myLvsPageSlot(int pos, int *page, int *slot)
static int scrSys(char *cmd, uint32_t p1, uint32_t p2)
static int mbCreate(char *dev)
int gpioSetSignalFuncEx(unsigned signum, gpioSignalFuncEx_t f, void *userdata)
static uint32_t dmaPwmDataAdr(int pos)
int gpioSetSignalFunc(unsigned signum, gpioSignalFunc_t f)
char rxBuf[BSC_FIFO_SIZE]
#define MAX_CONNECT_ADDRESSES
#define PI_BAD_CHAIN_DELAY
static int read_SDA(wfRx_t *w)
static volatile uint32_t scriptEventBits
int fileOpen(char *file, unsigned mode)
int i2cBlockProcessCall(unsigned handle, unsigned reg, char *buf, unsigned count)
int eventSetFuncEx(unsigned event, eventFuncEx_t f, void *userdata)
#define PI_SPI_FLAGS_GET_AUX_SPI(x)
int gpioServo(unsigned gpio, unsigned val)
int spiRead(unsigned handle, char *buf, unsigned count)
int myPathBad(char *name)
static gpioISR_t gpioISR[PI_MAX_GPIO+1]
int i2cReadI2CBlockData(unsigned handle, unsigned reg, char *buf, unsigned count)
uint32_t level[LVS_PER_IPAGE]
#define PI_SPI_FLAGS_GET_CPHA(x)
static uint64_t updateMask
static uint32_t hw_pwm_duty[2]
int i2cWriteI2CBlockData(unsigned handle, unsigned reg, char *buf, unsigned count)
static void * pthSocketThread(void *x)
static fileInfo_t fileInfo[PI_FILE_SLOTS]
static void chainSetCntVal(int counter, int slot, uint32_t value)
static unsigned dmaCurrentSlot(unsigned pos)
bool param(const std::string ¶m_name, T ¶m_val, const T &default_val)
#define PI_DEFAULT_UPDATE_MASK_ZERO
#define PI_SPI_FLAGS_GET_RESVD(x)
static void stopHardwarePWM(void)
static unsigned old_mode_miso
static void * pthAlertThread(void *x)
#define AUXSPI_STAT_RX_EMPTY
int serWriteByte(unsigned handle, unsigned bVal)
#define PI_MAX_BUSY_DELAY
#define PI_DEFAULT_CLK_PERIPHERAL
int gpioWaveGetHighCbs(void)
static uintptr_t ** dmaPMapBlk
#define PI_BAD_CFG_INTERNAL
static int dmaNowAtOCB(void)
static volatile uint32_t * pwmReg
static int scrWait(gpioScript_t *s, uint32_t bits)
static void myTckPageSlot(int pos, int *page, int *slot)
#define PI_BB_SER_MAX_BAUD
static unsigned mbReleaseMemory(int fd, unsigned handle)
static void intScriptEventBits(void)
static void spiGo(unsigned speed, uint32_t flags, char *txBuf, char *rxBuf, unsigned count)
#define PI_I2C_FUNC_SMBUS_WRITE_I2C_BLOCK
#define DMA_WAIT_ON_WRITES
#define DMA_CHANNEL_RESET
static void * pthISRThread(void *x)
static void waveRxBit(int gpio, int level, uint32_t tick)
int fileSeek(unsigned handle, int32_t seekOffset, int seekFrom)
#define AUXSPI_CNTL0_ENABLE
#define PI_BAD_MALLOC_MODE
void rawWaveSetIn(int pos, uint32_t value)
static volatile uint32_t piCores
#define PCM_DREQ_TX_REQ_L(x)
static void intNotifyBits(void)
#define PI_I2C_COMBINED_OFF
#define PI_BAD_SHELL_STATUS
static uint32_t _spiTXBits(char *buf, int pos, int bitlen, int msbf)
static void set_CS(wfRx_t *w)
int serRead(unsigned handle, char *buf, unsigned count)
int gpioSetAlertFunc(unsigned gpio, gpioAlertFunc_t f)
static volatile uint32_t * auxReg
static uint32_t hw_clk_freq[3]
static void clear_SDA(wfRx_t *w)
#define PI_NTFY_FLAGS_EVENT
#define PI_BAD_WVSP_COMMND
uint32_t gpioCfgGetInternals(void)
static unsigned dmaNowAtICB(void)
#define PI_I2C_SMBUS_QUICK
rawWaveInfo_t rawWaveInfo(int wave_id)
#define DMA_INTERRUPT_STATUS
#define PI_CHAIN_LOOP_CNT
static int spiAnyOpen(uint32_t flags)
int gpioNotifyBegin(unsigned handle, uint32_t bits)
static void waveSetOOL(int pos, uint32_t OOL)
int gpioSetWatchdog(unsigned gpio, unsigned timeout)
#define PI_SER_READ_FAILED
static void _spiRXBits(char *buf, int pos, int bitlen, int msbf, uint32_t bits)
static void * pthSocketThreadHandler(void *fdC)
uint32_t OOL[OOL_PER_OPAGE]
#define PI_I2C_FUNC_SMBUS_WRITE_WORD_DATA
static char * myBuf2Str(unsigned count, char *buf)
static uint32_t hw_pwm_freq[2]
static void waveCBsOOLs(int *numCBs, int *numBOOLs, int *numTOOLs)
#define PI_BB_SPI_MAX_BAUD
#define PI_SPI_FLAGS_GET_BITLEN(x)
int gpioCfgDMAchannels(unsigned primaryChannel, unsigned secondaryChannel)
int gpioSetPWMrange(unsigned gpio, unsigned range)
int gpioNotifyOpenWithSize(int bufSize)
#define PI_SPI_FLAGS_GET_RX_LSB(x)
#define PI_I2C_SMBUS_BYTE
static uint32_t chainGetCntValPadr(int counter, int slot)
static int addrAllowed(struct sockaddr *saddr)
int bbI2CZip(unsigned SDA, char *inBuf, unsigned inLen, char *outBuf, unsigned outLen)
int gpioSerialRead(unsigned gpio, void *buf, size_t bufSize)
int gpioWaveTxStart(unsigned wave_mode)
int i2cReadByteData(unsigned handle, unsigned reg)
static dmaIPage_t ** dmaIBus
static void mbClose(int fd)
static volatile uint32_t * padsReg
#define AUXSPI_CNTL0_MSB_FIRST(x)
static dmaPage_t ** dmaVirt
static void myOffPageSlot(int pos, int *page, int *slot)
int gpioWaveGetHighMicros(void)
#define PI_BAD_SOCKET_PORT
pthread_t * gpioStartThread(gpioThreadFunc_t f, void *userdata)
uint32_t rawWaveGetIn(int pos)
#define PI_LOCALHOST_SOCK_IF
#define PI_DEFAULT_SOCKET_PORT
static volatile uint32_t * clkReg
#define PI_MIN_SERVO_PULSEWIDTH
static void sigSetHandler(void)
#define PI_WAVE_MODE_REPEAT
#define PI_SPI_FLAGS_GET_TX_LSB(x)
#define PI_DEFAULT_UPDATE_MASK_COMPUTE
#define PI_BAD_CLK_MICROS
#define PI_WAVE_MAX_MICROS
#define PI_WAVE_MODE_ONE_SHOT
int i2cReadWordData(unsigned handle, unsigned reg)
#define PI_BB_I2C_MIN_BAUD
#define PI_DEFAULT_UPDATE_MASK_B1
void(* gpioTimerFuncEx_t)(void *userdata)
static void alertWdogCheck(gpioSample_t *sample, int numSamples)
static int pthAlertRunning
static uint32_t * waveEndPtr
int eventSetFunc(unsigned event, eventFunc_t f)
int bbI2COpen(unsigned SDA, unsigned SCL, unsigned baud)
#define PI_I2C_RDRW_IOCTL_MAX_MSGS
static void clear_SCLK(wfRx_t *w)
rawCbs_t * rawWaveCBAdr(int cbNum)
static void sigHandler(int signum)
int getBitInBytes(int bitPos, char *buf, int numBits)
int gpioGetServoPulsewidth(unsigned gpio)
#define CMD_MAX_EXTENSION
#define PI_CFG_ALERT_FREQ
#define PI_EMPTY_WAVEFORM
#define PI_HW_PWM_MAX_FREQ
static const uint16_t pwmRealRange[PWM_FREQS]
#define PI_BAD_FILE_WRITE
static void bbSPIStop(wfRx_t *w)
static void wfRx_unlock(int i)
#define PI_WAVE_MAX_PULSES
static uint32_t old_spi_cntl0
#define PCM_TXC_CH1WID(x)
void(* eventFuncEx_t)(int event, uint32_t tick, void *userdata)
static void myGpioSleep(int seconds, int micros)
static void I2CStart(wfRx_t *w)
#define PI_BB_I2C_MAX_BAUD
#define AUX_SPI0_CNTL0_REG
#define AUXSPI_CNTL0_IN_RISING(x)
static unsigned old_mode_asclk
#define PI_SPI_FLAGS_GET_CHANNEL(x)
static void initClearGlobals(void)
uint32_t gpioOn[ON_PER_IPAGE]
#define PI_NOTIFY_RESERVED
static void dmaCbPrint(int pos)
#define PI_HW_CLK_MIN_FREQ
int gpioGetMode(unsigned gpio)
#define DMA_DEBUG_READ_ERR
int gpioWaveAddSerial(unsigned gpio, unsigned baud, unsigned data_bits, unsigned stop_bits, unsigned offset, unsigned numBytes, char *bstr)
#define PI_I2C_SMBUS_PROC_CALL
static uint32_t chainGetValPadr(int n)
static void notifyMutex(int lock)
uint32_t tick[TCK_PER_IPAGE]
static unsigned old_mode_mosi
#define PI_DEFAULT_MEM_ALLOC_MODE
#define PCM_DREQ_TX_PANIC(x)
int rawWaveAddSPI(rawSPI_t *spi, unsigned offset, unsigned spiSS, char *buf, unsigned spiTxBits, unsigned spiBitFirst, unsigned spiBitLast, unsigned spiBits)
int gpioSetPWMfrequency(unsigned gpio, unsigned frequency)
static volatile uint32_t * bscsReg
#define PI_I2C_READ_FAILED
int gpioWaveGetMaxCbs(void)
static void bbSPIStart(wfRx_t *w)
#define PI_DEFAULT_UPDATE_MASK_PI2B
#define PI_MAX_I2C_DEVICE_COUNT
int serDataAvailable(unsigned handle)
static int my_smbus_access(int fd, char rw, uint8_t cmd, int size, union my_smbus_data *data)
#define PI_FILE_NOT_WOPEN
int shell(char *scriptName, char *scriptString)
void(* gpioGetSamplesFunc_t)(const gpioSample_t *samples, int numSamples)
int gpioSetMode(unsigned gpio, unsigned mode)
static void dmaGpioOnCb(int b, int pos)
uint32_t diffTick[TICKSLOTS]
#define PI_I2C_SMBUS_BLOCK_DATA
#define PI_MAX_DUTYCYCLE_RANGE
static uint32_t dmaCbAdr(int pos)
int gpioCustom2(unsigned arg1, char *argx, unsigned argc, char *retBuf, unsigned retMax)
#define PI_I2C_SMBUS_I2C_BLOCK_BROKEN
uint32_t gpioRead_Bits_32_53(void)
static volatile uint32_t notifyBits
int bbI2CClose(unsigned SDA)
#define PI_MIN_WAVE_DATABITS
int i2cWriteByteData(unsigned handle, unsigned reg, unsigned bVal)
int gpioWaveDelete(unsigned wave_id)
#define MB_ALLOCATE_MEMORY_TAG
#define PI_SCRIPT_RUNNING
#define PI_I2C_SMBUS_BLOCK_PROC_CALL
static int initZaps(int pmapFd, void *virtualBase, int basePage, int pages)
static void * pthScript(void *x)
static volatile uint32_t * dmaIn
int gpioSetGetSamplesFunc(gpioGetSamplesFunc_t f, uint32_t bits)
static void dmaReadLevelsCb(int b, int pos)
#define DMA_DEBUG_RD_LST_NOT_SET_ERR
static void intScriptBits(void)
#define PI_BAD_SECO_CHANNEL
static uint32_t dmaTickAdr(int pos)
#define PI_BAD_BUF_MILLIS
#define PI_BAD_PULSEWIDTH
int cmdParseScript(char *script, cmdScript_t *s, int diags)
static unsigned mbUnlockMemory(int fd, unsigned handle)
int fileClose(unsigned handle)
int gpioWaveGetHighPulses(void)
#define PI_THREAD_STARTED
int bbSPIXfer(unsigned CS, char *inBuf, char *outBuf, unsigned count)
static int waveClockInited
static volatile uint32_t pi_dram_bus
#define PI_SOME_PERMITTED
static void clear_SCL(wfRx_t *w)
static int chooseBestClock(clkInf_t *clkInf, unsigned f, unsigned numc, unsigned *cf)
static unsigned old_mode_ace2
static int intGpioSetAlertFunc(unsigned gpio, void *f, int user, void *userdata)
unsigned DMAprimaryChannel
int eventMonitor(unsigned handle, uint32_t bits)
static const uint8_t PWMDef[PI_MAX_GPIO+1]
int gpioCfgDMAchannel(unsigned DMAchannel)
int serClose(unsigned handle)
#define PI_SPI_FLAGS_GET_CPOL(x)
static int I2CPutByte(wfRx_t *w, int byte)
static void set_SDA(wfRx_t *w)
static volatile uint32_t pi_mem_flag
#define PI_BAD_FIFO_COMMAND
int gpioSetPad(unsigned pad, unsigned padStrength)
static void spinWhileStarting(void)
#define PI_NO_WAVEFORM_ID
unsigned bufferMilliseconds
static uint32_t waveOOLPOadr(int pos)
static void scrSwap(int *v1, int *v2)
static void SPI_delay(wfRx_t *w)
#define PI_MAX_SCRIPT_VARS
int gpioSerialReadClose(unsigned gpio)
int gpioCfgPermissions(uint64_t updateMask)
#define CHECK_INITED_RET_NULL_PTR
int gpioStoreScript(char *script)
int gpioGetPWMrange(unsigned gpio)
static int pthFifoRunning
static int initMboxBlock(int block)
int gpioCfgSetInternals(uint32_t cfgVal)
static void alertGlitchFilter(gpioSample_t *sample, int numSamples)
#define PI_MAX_DMA_CHANNEL
#define PI_MIN_DUTYCYCLE_RANGE
int gpioSetGetSamplesFuncEx(gpioGetSamplesFuncEx_t f, uint32_t bits, void *userdata)
#define PI_MIN_WAVE_HALFSTOPBITS
#define PI_BUF_MILLIS_MIN
int gpioGetPWMdutycycle(unsigned gpio)
static volatile uint32_t wdogBits
static uint32_t waveCbPOadr(int pos)
uint8_t block[PI_I2C_SMBUS_BLOCK_MAX+2]
#define PI_MAX_WAVE_CYCLES
static unsigned mbLockMemory(int fd, unsigned handle)
#define PI_I2C_SMBUS_WORD_DATA
static eventAlert_t eventAlert[PI_MAX_EVENT+1]
int bbSPIOpen(unsigned CS, unsigned MISO, unsigned MOSI, unsigned SCLK, unsigned baud, unsigned spiFlags)
static volatile uint32_t gFilterBits
#define PI_BAD_WDOG_TIMEOUT
#define PI_DEFAULT_UPDATE_MASK_APLUS_BPLUS
#define PI_SCRIPT_NOT_READY
void gpioStopThread(pthread_t *pth)
static int initCheckPermitted(void)
#define PI_MAX_MILS_DELAY
int i2cZip(unsigned handle, char *inBuf, unsigned inLen, char *outBuf, unsigned outLen)
static void * mbMapMem(unsigned base, unsigned size)
uint32_t rawWaveGetOOL(int pos)
static rawWaveInfo_t waveInfo[PI_MAX_WAVES]
#define PI_MAX_SCRIPT_PARAMS
static DMAMem_t * dmaMboxBlk
int i2cWriteDevice(unsigned handle, char *buf, unsigned count)
static void spiTerm(uint32_t flags)
#define PI_I2C_FUNC_SMBUS_PROC_CALL
static uint32_t dmaGpioOnAdr(int pos)
#define PI_SCRIPT_INITING
#define PI_DEFAULT_UPDATE_MASK_A_B2
static void closeOrphanedNotifications(int slot, int fd)
void(* gpioAlertFunc_t)(int gpio, int level, uint32_t tick)
#define PI_WAVE_MODE_REPEAT_SYNC
#define PI_I2C_FUNC_SMBUS_WRITE_BYTE_DATA
unsigned gpioVersion(void)
static volatile gpioStats_t gpioStats
static int intEventSetFunc(unsigned event, void *f, int user, void *userdata)
int i2cWriteByte(unsigned handle, unsigned bVal)
static int myI2CGetPar(char *inBuf, int *inPos, int inLen, int *esc)
static void * pthFifoThread(void *x)
int gpioWaveGetMaxMicros(void)
int gpioHardwareClock(unsigned gpio, unsigned frequency)
uint32_t gpioDelay(uint32_t micros)
static unsigned bufferCycles
#define PI_BAD_SCRIPT_NAME
static uint32_t hw_pwm_real_range[2]
static uint32_t myGetTick(int pos)
static pthread_t pthSocket
static void spiInit(uint32_t flags)
static int scrEvtWait(gpioScript_t *s, uint32_t bits)
uint32_t gpioOff[OFF_PER_IPAGE]
int spiWrite(unsigned handle, char *buf, unsigned count)
void(* gpioSignalFunc_t)(int signum)
static volatile gpioCfg_t gpioCfg
static const uint16_t pwmCycles[PWM_FREQS]
union my_smbus_data * data
static void set_SCLK(wfRx_t *w)
int gpioSetISRFunc(unsigned gpio, unsigned edge, int timeout, gpioISRFunc_t f)
static int myPermit(unsigned gpio)
static gpioTimer_t gpioTimer[PI_MAX_TIMER+1]
#define PI_SER_WRITE_FAILED
static struct timespec libStarted
void(* eventFunc_t)(int event, uint32_t tick)
int gpioPWM(unsigned gpio, unsigned val)
static unsigned old_mode_ce0
#define MB_PROCESS_REQUEST
static wfRx_t wfRx[PI_MAX_USER_GPIO+1]
#define PI_I2C_FUNC_SMBUS_READ_BYTE
int gpioCfgSocketPort(unsigned port)
int gpioHardwarePWM(unsigned gpio, unsigned frequency, unsigned dutycycle)
#define PI_DISABLE_FIFO_IF
#define AUXSPI_CNTL0_CLR_FIFOS
#define PI_NOTIFY_RUNNING
int gpioWaveGetPulses(void)
static int initPagemapBlock(int block)
#define PI_BAD_CLK_PERIPH
static void waveOOLPageSlot(int pos, int *page, int *slot)
static void initPCM(unsigned bits)
#define PI_BUF_MILLIS_MAX
static void initCheckLockFile(void)
static unsigned bufferBlocks
#define PI_NOT_SERIAL_GPIO
int gpioScriptStatus(unsigned script_id, uint32_t *param)
int gpioRead(unsigned gpio)
int i2cProcessCall(unsigned handle, unsigned reg, unsigned wVal)
#define PI_BAD_WVSM_COMMND
#define PI_BAD_SER_DEVICE
static void dmaDelayCb(int b)
static void chainSetVal(int n, uint32_t val)
static void flushMemory(void)
static int intGpioSetISRFunc(unsigned gpio, unsigned edge, int timeout, void *f, int user, void *userdata)
int i2cClose(unsigned handle)
#define PI_DEFAULT_DMA_PRIMARY_CHANNEL
#define PI_THREAD_RUNNING
#define DMA_DEBUG_FIFO_ERR
int gpioSerialReadOpen(unsigned gpio, unsigned baud, unsigned data_bits)
int gpioCustom1(unsigned arg1, unsigned arg2, char *argx, unsigned argc)
#define PI_WAVE_NOT_FOUND
#define PI_MAX_SPI_DEVICE_COUNT
#define PI_NOTIFY_CLOSING
int gpioGetPad(unsigned pad)
#define PI_SPI_FLAGS_GET_CSPOLS(x)
void(* gpioGetSamplesFuncEx_t)(const gpioSample_t *samples, int numSamples, void *userdata)
static gpioNotify_t gpioNotify[PI_NOTIFY_SLOTS]
#define PI_SCRIPT_STACK_SIZE
static i2cInfo_t i2cInfo[PI_I2C_SLOTS]
static void switchFunctionOff(unsigned gpio)
#define PI_I2C_SMBUS_WRITE
int gpioSetAlertFuncEx(unsigned gpio, gpioAlertFuncEx_t f, void *userdata)
static uint32_t reportedLevel
#define PI_I2C_SMBUS_I2C_BLOCK_DATA
#define MB_LOCK_MEMORY_TAG
static volatile uint32_t * dmaOut
static int gpioNotifyOpenInBand(int fd)
int fileWrite(unsigned handle, char *buf, unsigned count)
int fileApprove(char *filename)
#define TIMER_SUB(a, b, result)
#define PI_I2C_SMBUS_BLOCK_MAX
static volatile int runState
rawCbs_t cb[CBS_PER_IPAGE]
#define PI_MAX_WDOG_TIMEOUT
#define PI_SER_OPEN_FAILED
static int initGrabLockFile(void)
static void clear_CS(wfRx_t *w)
#define PI_MAX_WAVE_DELAY
#define PI_DISABLE_SOCK_IF
void *( gpioThreadFunc_t)(void *)
static uint32_t old_spi_cntl1
char txBuf[BSC_FIFO_SIZE]
#define AUX_SPI0_STAT_REG
int gpioDeleteScript(unsigned script_id)
int gpioSetISRFuncEx(unsigned gpio, unsigned edge, int timeout, gpioAlertFuncEx_t f, void *userdata)
rawCbs_t cb[CBS_PER_OPAGE]
#define PI_NTFY_FLAGS_WDOG
#define PI_SER_READ_NO_DATA
int gpioWaveGetMaxPulses(void)
#define PI_SPI_FLAGS_GET_MODE(x)
#define PI_I2C_FUNC_SMBUS_READ_BLOCK_DATA
int gpioNoiseFilter(unsigned gpio, unsigned steady, unsigned active)
#define PI_BB_SPI_MIN_BAUD
int i2cReadBlockData(unsigned handle, unsigned reg, char *buf)
static void spiACS(int channel, int on)
static int initAllocDMAMem(void)
int gpioCfgClock(unsigned micros, unsigned peripheral, unsigned source)
static rawWave_t wf[3][PI_WAVE_MAX_PULSES]
static int chainGetCB(int n)
static unsigned old_mode_ce1
#define PI_MAX_SOCKET_PORT
#define PI_TOO_MANY_CHARS
static void myGpioSetMode(unsigned gpio, unsigned mode)
static volatile uint32_t * spiReg
#define PI_DEFAULT_UPDATE_MASK_UNKNOWN
static int numSockNetAddr
#define PI_SPI_FLAGS_CHANNEL(x)
int gpioWrite_Bits_0_31_Clear(uint32_t bits)
static int myDoCommand(uint32_t *p, unsigned bufSize, char *buf)
static unsigned old_mode_ace0
#define PI_FILE_NOT_ROPEN
static volatile uint32_t * systReg
int gpioGetPWMfrequency(unsigned gpio)
static int wave2Cbs(unsigned wave_mode, int *CB, int *BOOL, int *TOOL)
#define PI_SPI_FLAGS_GET_3WREN(x)
static void initPWM(unsigned bits)
int serWrite(unsigned handle, char *buf, unsigned count)
#define PI_I2C_SMBUS_I2C_BLOCK_MAX
static uint32_t sockNetAddr[MAX_CONNECT_ADDRESSES]
static void myCreatePipe(char *name, int perm)
static uint32_t dmaGpioOffAdr(int pos)
static unsigned old_mode_ace1
#define AUXSPI_CNTL1_MSB_FIRST(x)
int cmdParse(char *buf, uint32_t *p, unsigned ext_len, char *ext, cmdCtlParse_t *ctl)
#define PI_SCRIPT_RESERVED
#define DMA_PANIC_PRIORITY(x)
static uint32_t chainGetCntVal(int counter, int slot)
void(* gpioTimerFunc_t)(void)
#define PI_SPI_FLAGS_GET_3WIRE(x)
void rawDumpScript(unsigned script_id)
#define PI_MIN_DMA_CHANNEL
static void I2C_clock_stretch(wfRx_t *w)
int gpioWrite(unsigned gpio, unsigned level)
static unsigned old_mode_amosi
unsigned gpioHardwareRevision(void)
int gpioGlitchFilter(unsigned gpio, unsigned steady)
void(* gpioAlertFuncEx_t)(int gpio, int level, uint32_t tick, void *userdata)
#define PI_MAX_WAVE_DATABITS
#define PI_NTFY_FLAGS_BIT(x)
#define PI_SCRIPT_WAITING
#define CHECK_INITED_RET_NIL
int fileList(char *fpat, char *buf, unsigned count)
static volatile uint32_t * dmaReg
static int PWMClockInited
static unsigned old_mode_amiso
static int initPeripherals(void)
#define PI_NO_FILE_ACCESS
#define PI_I2C_FUNC_SMBUS_READ_I2C_BLOCK
static volatile uint32_t monitorBits
int gpioTrigger(unsigned gpio, unsigned pulseLen, unsigned level)
static void myClearGpioOn(unsigned gpio, int pos)
#define SOFT_ERROR(x, format, arg...)
#define PI_BAD_MILS_DELAY
static uint32_t myGetLevel(int pos)
static spiInfo_t spiInfo[PI_SPI_SLOTS]
static unsigned memAllocMode
int gpioCfgMemAlloc(unsigned memAllocMode)
static volatile uint32_t scriptBits
int bbSPIClose(unsigned CS)
#define PI_I2C_SMBUS_READ
#define AUXSPI_CNTL0_INVERT_CLK(x)
int spiOpen(unsigned spiChan, unsigned baud, unsigned spiFlags)
#define PI_MAX_SERVO_PULSEWIDTH
static int intGpioSetTimerFunc(unsigned id, unsigned millis, void *f, int user, void *userdata)
static void initHWClk(int clkCtl, int clkDiv, int clkSrc, int divI, int divF, int MASH)
int bscXfer(bsc_xfer_t *xfer)
int gpioCfgInterfaces(unsigned ifFlags)
static void dmaGpioOffCb(int b, int pos)
#define PI_DEFAULT_DMA_SECONDARY_CHANNEL
#define AUXSPI_CNTL0_SHIFT_LEN(x)
static void myGpioSetPwm(unsigned gpio, int oldVal, int newVal)
static uint32_t dmaReadLevelsAdr(int pos)
static dmaOPage_t ** dmaOBus
static volatile uint32_t * pcmReg
#define PI_NUM_AUX_SPI_CHANNEL
int gpioSetPullUpDown(unsigned gpio, unsigned pud)
static uint32_t spi_dummy
int gpioWrite_Bits_32_53_Clear(uint32_t bits)
int gpioWaveTxSend(unsigned wave_id, unsigned wave_mode)
#define PI_CFG_RT_PRIORITY
int i2cReadDevice(unsigned handle, char *buf, unsigned count)
static int pwmFreq[PWM_FREQS]
#define PI_I2C_SMBUS_BYTE_DATA
int spiClose(unsigned handle)
int i2cOpen(unsigned i2cBus, unsigned i2cAddr, unsigned i2cFlags)
#define PI_TOO_MANY_PULSES
static void I2C_delay(wfRx_t *w)
#define MB_RELEASE_MEMORY_TAG
int gpioWrite_Bits_0_31_Set(uint32_t bits)
static gpioInfo_t gpioInfo[PI_MAX_GPIO+1]
#define PI_WAVE_MAX_CHARS
static volatile uint32_t alertBits
static int pthSocketRunning
void i2cSwitchCombined(int setting)
static void waveRxSerial(wfRx_t *w, int level, uint32_t tick)
int spiXfer(unsigned handle, char *txBuf, char *rxBuf, unsigned count)
static void spiGoS(unsigned speed, uint32_t flags, char *txBuf, char *rxBuf, unsigned count)
int i2cWriteBlockData(unsigned handle, unsigned reg, char *buf, unsigned count)
void rawWaveSetOOL(int pos, uint32_t value)
#define PI_I2C_COMBINED_ON
#define PI_HW_PWM_MIN_FREQ
void time_sleep(double seconds)
int myScriptNameValid(char *name)
#define PI_CFG_NOSIGHANDLER
#define PI_BAD_SER_OFFSET
#define PI_I2C_FUNC_SMBUS_WRITE_BLOCK_DATA
#define MB_UNLOCK_MEMORY_TAG
int gpioNotifyPause(unsigned handle)
#define AUX_SPI0_CNTL1_REG
static void initDMAgo(volatile uint32_t *dmaAddr, uint32_t cbAddr)
int gpioTime(unsigned timetype, int *seconds, int *micros)
#define PI_BB_SER_MIN_BAUD
static int scrPop(gpioScript_t *s, int *SP, int *S)
#define PI_I2C_FUNC_SMBUS_WRITE_BYTE
static const clkCfg_t clkCfg[]
#define PI_MAX_MICS_DELAY
static void alertEmit(gpioSample_t *sample, int numSamples, uint32_t changedBits, uint32_t eTick)
static void spiGoA(unsigned speed, uint32_t flags, char *txBuf, char *rxBuf, unsigned count)
#define PI_BAD_SER_INVERT
uint32_t rawWaveGetOut(int pos)
#define PI_MIN_PAD_STRENGTH
static int mbUnmapMem(void *addr, unsigned size)
static pthread_t pthAlert
static void I2CStop(wfRx_t *w)
static uint32_t * initMapMem(int fd, uint32_t addr, uint32_t len)
static void chainMakeCounter(unsigned counter, unsigned blklen, unsigned blocks, unsigned count, uint32_t repeat, uint32_t next)
#define PI_MAX_PAD_STRENGTH
int gpioNotifyClose(unsigned handle)
static gpioGetSamples_t gpioGetSamples
static void mySetGpioOff(unsigned gpio, int pos)
static void scrPush(gpioScript_t *s, int *SP, int *S, int val)
static void I2CPutBit(wfRx_t *w, int bit)
static int I2CGetBit(wfRx_t *w)
static gpioAlert_t gpioAlert[PI_MAX_USER_GPIO+1]
static int mbProperty(int fd, void *buf)
static rawCbs_t * dmaCB2adr(int pos)
#define DBG(level, format, arg...)
#define PI_DEFAULT_IF_FLAGS
uint32_t gpioRead_Bits_0_31(void)
static const uint8_t clkDef[PI_MAX_GPIO+1]
#define PI_I2C_OPEN_FAILED
#define PI_BAD_CHAIN_LOOP
static volatile uint32_t pi_peri_phys
#define PI_NO_SCRIPT_ROOM
int gpioWrite_Bits_32_53_Set(uint32_t bits)
#define PI_NTFY_FLAGS_ALIVE
#define PI_NOT_SERVO_GPIO
int gpioGetPWMrealRange(unsigned gpio)
static int chainGetCntCB(int counter)
static void alertNoiseFilter(gpioSample_t *sample, int numSamples)
#define PI_TOO_MANY_PARAM
#define PI_WAVE_COUNT_PAGES
int gpioRunScript(unsigned script_id, unsigned numParam, uint32_t *param)
static int libInitialised
int i2cWriteQuick(unsigned handle, unsigned bit)
#define PI_BAD_PRIM_CHANNEL
static void initReleaseResources(void)
#define PI_MAX_WAVE_HALFSTOPBITS
#define PI_MEM_ALLOC_AUTO
int gpioCfgNetAddr(int numSockAddr, uint32_t *sockAddr)
#define PI_DEFAULT_CLK_MICROS
#define PI_UNKNOWN_COMMAND
static gpioSignal_t gpioSignal[PI_MAX_SIGNUM+1]
int serReadByte(unsigned handle)
static volatile uint32_t * gpioReg
#define AUXSPI_CNTL0_OUT_RISING(x)
static void mySetGpioOn(unsigned gpio, int pos)
uint32_t wouldBlockPipeWrite
#define PI_BAD_SERIAL_COUNT
static uint8_t I2CGetByte(wfRx_t *w, int nack)
static uint32_t old_spi_cs
void putBitInBytes(int bitPos, char *buf, int bit)
int rawWaveAddGeneric(unsigned numIn1, rawWave_t *in1)
#define PI_NUM_STD_SPI_CHANNEL
static uint32_t myGpioDelay(uint32_t micros)
int gpioSleep(unsigned timetype, int seconds, int micros)
static void myGpioWrite(unsigned gpio, unsigned level)
static dmaIPage_t ** dmaIVirt
static uint8_t bbSPIXferByte(wfRx_t *w, char txByte)
static unsigned mbAllocateMemory(int fd, unsigned size, unsigned align, unsigned flags)
int gpioWaveGetMicros(void)
int gpioSerialReadInvert(unsigned gpio, unsigned invert)
int gpioSetTimerFunc(unsigned id, unsigned millis, gpioTimerFunc_t f)
int gpioCfgBufferSize(unsigned millis)
static void myClearGpioOff(unsigned gpio, int pos)
int gpioWaveChain(char *buf, unsigned bufSize)
int eventTrigger(unsigned event)
void(* gpioSignalFuncEx_t)(int signum, void *userdata)
static dmaPage_t ** dmaBus
static gpioScript_t gpioScript[PI_MAX_SCRIPTS]
static uint32_t old_spi_clk
static void wfRx_lock(int i)
#define PI_BAD_MICS_DELAY
int gpioUpdateScript(unsigned script_id, unsigned numParam, uint32_t *param)
#define PI_MIN_SOCKET_PORT
int i2cWriteWordData(unsigned handle, unsigned reg, unsigned wVal)
static void waveBitDelay(unsigned baud, unsigned bits, unsigned stops, unsigned *bitDelay)
#define PI_DEFAULT_UPDATE_MASK_PI3B
#define PI_MEM_ALLOC_PAGEMAP
#define PI_HW_CLK_MAX_FREQ
#define AUXSPI_CNTL0_SPEED(x)
int i2cSegments(unsigned handle, pi_i2c_msg_t *segs, unsigned numSegs)
#define PI_DEFAULT_DUTYCYCLE_RANGE
void report(int err, char *fmt,...)
int gpioStopScript(unsigned script_id)
static void dmaInitCbs(void)
#define PI_I2C_WRITE_FAILED
static int waveDelayCBs(uint32_t delay)
#define PWM_DMAC_PANIC(x)
static int myGpioRead(unsigned gpio)
int gpioWaveAddGeneric(unsigned numPulses, gpioPulse_t *pulses)
static void initClock(int mainClock)
int serOpen(char *tty, unsigned serBaud, unsigned serFlags)
int gpioSetTimerFuncEx(unsigned id, unsigned millis, gpioTimerFuncEx_t f, void *userdata)
static serInfo_t serInfo[PI_SER_SLOTS]
static void dmaTickCb(int b, int pos)
static char * myTimeStamp()
#define PI_I2C_FUNC_SMBUS_READ_WORD_DATA
#define PI_BAD_WVSC_COMMND
static void myGpioSetServo(unsigned gpio, int oldVal, int newVal)
static dmaOPage_t ** dmaOVirt
#define PI_WAVE_MODE_ONE_SHOT_SYNC
static int mbDMAAlloc(DMAMem_t *DMAMemP, unsigned size, uint32_t pi_mem_flag)
int gpioCfgInternals(unsigned cfgWhat, unsigned cfgVal)
#define PI_DEFAULT_BUFFER_MILLIS
static unsigned old_mode_sclk
#define PI_BAD_SPI_CHANNEL
int i2cReadByte(unsigned handle)
static void mbDMAFree(DMAMem_t *DMAMemP)
#define PI_I2C_FUNC_SMBUS_QUICK
void rawWaveSetOut(int pos, uint32_t value)
#define PI_MEM_ALLOC_MAILBOX