pigpio.c
Go to the documentation of this file.
1 /*
2 This is free and unencumbered software released into the public domain.
3 
4 Anyone is free to copy, modify, publish, use, compile, sell, or
5 distribute this software, either in source code form or as a compiled
6 binary, for any purpose, commercial or non-commercial, and by any
7 means.
8 
9 In jurisdictions that recognize copyright laws, the author or authors
10 of this software dedicate any and all copyright interest in the
11 software to the public domain. We make this dedication for the benefit
12 of the public at large and to the detriment of our heirs and
13 successors. We intend this dedication to be an overt act of
14 relinquishment in perpetuity of all present and future rights to this
15 software under copyright law.
16 
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 OTHER DEALINGS IN THE SOFTWARE.
24 
25 For more information, please refer to <http://unlicense.org/>
26 */
27 
28 /* pigpio version 67 */
29 
30 /* include ------------------------------------------------------- */
31 
32 #define _GNU_SOURCE
33 
34 #include <stdio.h>
35 #include <string.h>
36 #include <strings.h>
37 #include <stdlib.h>
38 #include <stdint.h>
39 #include <stdarg.h>
40 #include <ctype.h>
41 #include <syslog.h>
42 #include <poll.h>
43 #include <unistd.h>
44 #include <fcntl.h>
45 #include <termios.h>
46 #include <signal.h>
47 #include <errno.h>
48 #include <time.h>
49 #include <sys/ioctl.h>
50 #include <limits.h>
51 #include <pthread.h>
52 #include <sys/time.h>
53 #include <sys/mman.h>
54 #include <sys/types.h>
55 #include <sys/stat.h>
56 #include <sys/file.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>
62 #include <fnmatch.h>
63 #include <glob.h>
64 
65 #include "pigpio.h"
66 
67 #include "command.h"
68 
69 
70 /* --------------------------------------------------------------- */
71 
72 /*
73  0 GPFSEL0 GPIO Function Select 0
74  1 GPFSEL1 GPIO Function Select 1
75  2 GPFSEL2 GPIO Function Select 2
76  3 GPFSEL3 GPIO Function Select 3
77  4 GPFSEL4 GPIO Function Select 4
78  5 GPFSEL5 GPIO Function Select 5
79  6 - Reserved
80  7 GPSET0 GPIO Pin Output Set 0
81  8 GPSET1 GPIO Pin Output Set 1
82  9 - Reserved
83 10 GPCLR0 GPIO Pin Output Clear 0
84 11 GPCLR1 GPIO Pin Output Clear 1
85 12 - Reserved
86 13 GPLEV0 GPIO Pin Level 0
87 14 GPLEV1 GPIO Pin Level 1
88 15 - Reserved
89 16 GPEDS0 GPIO Pin Event Detect Status 0
90 17 GPEDS1 GPIO Pin Event Detect Status 1
91 18 - Reserved
92 19 GPREN0 GPIO Pin Rising Edge Detect Enable 0
93 20 GPREN1 GPIO Pin Rising Edge Detect Enable 1
94 21 - Reserved
95 22 GPFEN0 GPIO Pin Falling Edge Detect Enable 0
96 23 GPFEN1 GPIO Pin Falling Edge Detect Enable 1
97 24 - Reserved
98 25 GPHEN0 GPIO Pin High Detect Enable 0
99 26 GPHEN1 GPIO Pin High Detect Enable 1
100 27 - Reserved
101 28 GPLEN0 GPIO Pin Low Detect Enable 0
102 29 GPLEN1 GPIO Pin Low Detect Enable 1
103 30 - Reserved
104 31 GPAREN0 GPIO Pin Async. Rising Edge Detect 0
105 32 GPAREN1 GPIO Pin Async. Rising Edge Detect 1
106 33 - Reserved
107 34 GPAFEN0 GPIO Pin Async. Falling Edge Detect 0
108 35 GPAFEN1 GPIO Pin Async. Falling Edge Detect 1
109 36 - Reserved
110 37 GPPUD GPIO Pin Pull-up/down Enable
111 38 GPPUDCLK0 GPIO Pin Pull-up/down Enable Clock 0
112 39 GPPUDCLK1 GPIO Pin Pull-up/down Enable Clock 1
113 40 - Reserved
114 41 - Test
115 */
116 
117 /*
118 0 CS DMA Channel 0 Control and Status
119 1 CPI_ONBLK_AD DMA Channel 0 Control Block Address
120 2 TI DMA Channel 0 CB Word 0 (Transfer Information)
121 3 SOURCE_AD DMA Channel 0 CB Word 1 (Source Address)
122 4 DEST_AD DMA Channel 0 CB Word 2 (Destination Address)
123 5 TXFR_LEN DMA Channel 0 CB Word 3 (Transfer Length)
124 6 STRIDE DMA Channel 0 CB Word 4 (2D Stride)
125 7 NEXTCPI_ONBK DMA Channel 0 CB Word 5 (Next CB Address)
126 8 DEBUG DMA Channel 0 Debug
127 */
128 
129 /*
130 DEBUG register bits
131 
132 bit 2 READ_ERROR
133 
134  Slave Read Response Error RW 0x0
135 
136  Set if the read operation returned an error value on
137  the read response bus. It can be cleared by writing
138  a 1.
139 
140 bit 1 FIFO_ERROR
141 
142  Fifo Error RW 0x0
143 
144  Set if the optional read Fifo records an error
145  condition. It can be cleared by writing a 1.
146 
147 bit 0 READ_LAST_NOT_SET_ERROR
148 
149  Read Last Not Set Error RW 0x0
150 
151  If the AXI read last signal was not set when
152  expected, then this error bit will be set. It can be
153  cleared by writing a 1.
154 */
155 
156 /*
157 0 CTL PWM Control
158 1 STA PWM Status
159 2 DMAC PWM DMA Configuration
160 4 RNG1 PWM Channel 1 Range
161 5 DAT1 PWM Channel 1 Data
162 6 FIF1 PWM FIFO Input
163 8 RNG2 PWM Channel 2 Range
164 9 DAT2 PWM Channel 2 Data
165 */
166 
167 /*
168 0 PCM_CS PCM Control and Status
169 1 PCM_FIFO PCM FIFO Data
170 2 PCM_MODE PCM Mode
171 3 PCM_RXC PCM Receive Configuration
172 4 PCM_TXC PCM Transmit Configuration
173 5 PCM_DREQ PCM DMA Request Level
174 6 PCM_INTEN PCM Interrupt Enables
175 7 PCM_INTSTC PCM Interrupt Status & Clear
176 8 PCM_GRAY PCM Gray Mode Control
177 */
178 
179 /*
180 0 CS System Timer Control/Status
181 1 CLO System Timer Counter Lower 32 bits
182 2 CHI System Timer Counter Higher 32 bits
183 3 C0 System Timer Compare 0
184 4 C1 System Timer Compare 1
185 5 C2 System Timer Compare 2
186 6 C3 System Timer Compare 3
187 */
188 
189 /* define -------------------------------------------------------- */
190 
191 #define THOUSAND 1000
192 #define MILLION 1000000
193 #define BILLION 1000000000
194 
195 #define BANK (gpio>>5)
196 
197 #define BIT (1<<(gpio&0x1F))
198 
199 #ifndef EMBEDDED_IN_VM
200 #define DBG(level, format, arg...) DO_DBG(level, format, ## arg)
201 #else
202 #define DBG(level, format, arg...)
203 #endif
204 
205 #define DO_DBG(level, format, arg...) \
206  { \
207  if ((gpioCfg.dbgLevel >= level) && \
208  (!(gpioCfg.internals & PI_CFG_NOSIGHANDLER))) \
209  fprintf(stderr, "%s %s: " format "\n" , \
210  myTimeStamp(), __FUNCTION__ , ## arg); \
211  }
212 
213 #ifndef DISABLE_SER_CHECK_INITED
214 #define SER_CHECK_INITED CHECK_INITED
215 #else
216 #define SER_CHECK_INITED
217 #endif
218 
219 #define CHECK_INITED \
220  do \
221  { \
222  if (!libInitialised) \
223  { \
224  DBG(DBG_ALWAYS, \
225  "pigpio uninitialised, call gpioInitialise()"); \
226  return PI_NOT_INITIALISED; \
227  } \
228  } \
229  while (0)
230 
231 #define CHECK_INITED_RET_NULL_PTR \
232  do \
233  { \
234  if (!libInitialised) \
235  { \
236  DBG(DBG_ALWAYS, \
237  "pigpio uninitialised, call gpioInitialise()"); \
238  return (NULL); \
239  } \
240  } \
241  while (0)
242 
243 #define CHECK_INITED_RET_NIL \
244  do \
245  { \
246  if (!libInitialised) \
247  { \
248  DBG(DBG_ALWAYS, \
249  "pigpio uninitialised, call gpioInitialise()"); \
250  } \
251  } \
252  while (0)
253 
254 #define CHECK_NOT_INITED \
255  do \
256  { \
257  if (libInitialised) \
258  { \
259  DBG(DBG_ALWAYS, \
260  "pigpio initialised, call gpioTerminate()"); \
261  return PI_INITIALISED; \
262  } \
263  } \
264  while (0)
265 
266 #define SOFT_ERROR(x, format, arg...) \
267  do \
268  { \
269  DBG(DBG_ALWAYS, format, ## arg); \
270  return x; \
271  } \
272  while (0)
273 
274 #define TIMER_ADD(a, b, result) \
275  do \
276  { \
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) \
280  { \
281  ++(result)->tv_sec; \
282  (result)->tv_nsec -= BILLION; \
283  } \
284  } \
285  while (0)
286 
287 #define TIMER_SUB(a, b, result) \
288  do \
289  { \
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) \
293  { \
294  --(result)->tv_sec; \
295  (result)->tv_nsec += BILLION; \
296  } \
297  } \
298  while (0)
299 
300 #define PI_PERI_BUS 0x7E000000
301 
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)
313 
314 #define AUX_LEN 0xD8
315 #define BSCS_LEN 0x40
316 #define CLK_LEN 0xA8
317 #define DMA_LEN 0x1000 /* allow access to all channels */
318 #define GPIO_LEN 0xB4
319 #define PADS_LEN 0x38
320 #define PCM_LEN 0x24
321 #define PWM_LEN 0x28
322 #define SPI_LEN 0x18
323 #define SYST_LEN 0x1C
324 
325 #define DMA_ENABLE (0xFF0/4)
326 
327 #define GPFSEL0 0
328 
329 #define GPSET0 7
330 #define GPSET1 8
331 
332 #define GPCLR0 10
333 #define GPCLR1 11
334 
335 #define GPLEV0 13
336 #define GPLEV1 14
337 
338 #define GPEDS0 16
339 #define GPEDS1 17
340 
341 #define GPREN0 19
342 #define GPREN1 20
343 #define GPFEN0 22
344 #define GPFEN1 23
345 #define GPHEN0 25
346 #define GPHEN1 26
347 #define GPLEN0 28
348 #define GPLEN1 29
349 #define GPAREN0 31
350 #define GPAREN1 32
351 #define GPAFEN0 34
352 #define GPAFEN1 35
353 
354 #define GPPUD 37
355 #define GPPUDCLK0 38
356 #define GPPUDCLK1 39
357 
358 #define DMA_CS 0
359 #define DMA_CONBLK_AD 1
360 #define DMA_DEBUG 8
361 
362 /* DMA CS Control and Status bits */
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)
370 
371 /* DMA control block "info" field bits */
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)
384 
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)
388 
389 #define DMA_LITE_FIRST 7
390 #define DMA_LITE_MAX 0xfffc
391 
392 #define PWM_CTL 0
393 #define PWM_STA 1
394 #define PWM_DMAC 2
395 #define PWM_RNG1 4
396 #define PWM_DAT1 5
397 #define PWM_FIFO 6
398 #define PWM_RNG2 8
399 #define PWM_DAT2 9
400 
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)
408 
409 #define PWM_DMAC_ENAB (1 <<31)
410 #define PWM_DMAC_PANIC(x) ((x)<< 8)
411 #define PWM_DMAC_DREQ(x) (x)
412 
413 #define PCM_CS 0
414 #define PCM_FIFO 1
415 #define PCM_MODE 2
416 #define PCM_RXC 3
417 #define PCM_TXC 4
418 #define PCM_DREQ 5
419 #define PCM_INTEN 6
420 #define PCM_INTSTC 7
421 #define PCM_GRAY 8
422 
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)
436 
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)
448 
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)
457 
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)
466 
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)
471 
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)
476 
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)
481 
482 #define PCM_GRAY_FLUSH (1<<2)
483 #define PCM_GRAY_CLR (1<<1)
484 #define PCM_GRAY_EN (1<<0)
485 
486 #define BCM_PASSWD (0x5A<<24)
487 
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)
493 
494 #define CLK_SRCS 2
495 
496 #define CLK_CTL_SRC_OSC 1
497 #define CLK_CTL_SRC_PLLD 6
498 
499 #define CLK_OSC_FREQ 19200000
500 #define CLK_PLLD_FREQ 500000000
501 
502 #define CLK_DIV_DIVI(x) ((x)<<12)
503 #define CLK_DIV_DIVF(x) ((x)<< 0)
504 
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
511 
512 #define CLK_PCMCTL 38
513 #define CLK_PCMDIV 39
514 
515 #define CLK_PWMCTL 40
516 #define CLK_PWMDIV 41
517 
518 #define SYST_CS 0
519 #define SYST_CLO 1
520 #define SYST_CHI 2
521 
522 /* SPI */
523 
524 #define SPI_CS 0
525 #define SPI_FIFO 1
526 #define SPI_CLK 2
527 #define SPI_DLEN 3
528 #define SPI_LTOH 4
529 #define SPI_DC 5
530 
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)
550 
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)
555 
556 #define SPI_MODE0 0
557 #define SPI_MODE1 1
558 #define SPI_MODE2 2
559 #define SPI_MODE3 3
560 
561 #define SPI_CS0 0
562 #define SPI_CS1 1
563 #define SPI_CS2 2
564 
565 /* standard SPI gpios (ALT0) */
566 
567 #define PI_SPI_CE0 8
568 #define PI_SPI_CE1 7
569 #define PI_SPI_SCLK 11
570 #define PI_SPI_MISO 9
571 #define PI_SPI_MOSI 10
572 
573 /* auxiliary SPI gpios (ALT4) */
574 
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
581 
582 /* AUX */
583 
584 #define AUX_IRQ 0
585 #define AUX_ENABLES 1
586 
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
598 
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
603 
604 #define AUX_SPI0_IO_REG 40
605 #define AUX_SPI0_TX_HOLD 44
606 
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
611 
612 #define AUX_SPI1_IO_REG 56
613 #define AUX_SPI1_TX_HOLD 60
614 
615 #define AUXENB_SPI2 (1<<2)
616 #define AUXENB_SPI1 (1<<1)
617 #define AUXENB_UART (1<<0)
618 
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)
632 
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)
638 
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)
646 
647 /* --------------------------------------------------------------- */
648 
649 #define NORMAL_DMA (DMA_NO_WIDE_BURSTS | DMA_WAIT_RESP)
650 
651 #define TIMED_DMA(x) (DMA_DEST_DREQ | DMA_PERIPHERAL_MAPPING(x))
652 
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)
655 
656 #define DBG_MIN_LEVEL 0
657 #define DBG_ALWAYS 0
658 #define DBG_STARTUP 1
659 #define DBG_DMACBS 2
660 #define DBG_SCRIPT 3
661 #define DBG_USER 4
662 #define DBG_INTERNAL 5
663 #define DBG_SLOW_TICK 6
664 #define DBG_FAST_TICK 7
665 #define DBG_MAX_LEVEL 8
666 
667 #define GPIO_UNDEFINED 0
668 #define GPIO_WRITE 1
669 #define GPIO_PWM 2
670 #define GPIO_SERVO 3
671 #define GPIO_HW_CLK 4
672 #define GPIO_HW_PWM 5
673 #define GPIO_SPI 6
674 #define GPIO_I2C 7
675 
676 #define STACK_SIZE (256*1024)
677 
678 #define PAGE_SIZE 4096
679 
680 #define PWM_FREQS 18
681 
682 #define CYCLES_PER_BLOCK 80
683 #define PULSE_PER_CYCLE 25
684 
685 #define PAGES_PER_BLOCK 53
686 
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
693 
694 #define CBS_PER_OPAGE 118
695 #define OOL_PER_OPAGE 79
696 
697 /*
698 Wave Count Block
699 
700 Assumes two counters per block. Each counter 4 * 16 (16^4=65536)
701  0 CB [13] 13*8 104 CBs for counter 0
702  104 CB [13] 13*8 104 CBs for counter 1
703  208 CB [60] 60*8 480 CBs reserved to construct wave
704  688 OOL [60] 60*1 60 OOL reserved to construct wave
705  748 OOL[136] 136*1 136 OOL for counter 0
706  884 OOL[136] 136*1 136 OOL for counter 1
707 1020 pad [4] 4*1 4 spare
708 */
709 
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
718 
719 #define CBS_PER_CYCLE ((PULSE_PER_CYCLE*3)+2)
720 
721 #define NUM_CBS (CBS_PER_CYCLE * bufferCycles)
722 
723 #define SUPERCYCLE 800
724 #define SUPERLEVEL 20000
725 
726 #define BLOCK_SIZE (PAGES_PER_BLOCK*PAGE_SIZE)
727 
728 #define DMAI_PAGES (PAGES_PER_BLOCK * bufferBlocks)
729 
730 #define DMAO_PAGES (PAGES_PER_BLOCK * PI_WAVE_BLOCKS)
731 
732 #define NUM_WAVE_OOL (DMAO_PAGES * OOL_PER_OPAGE)
733 #define NUM_WAVE_CBS (DMAO_PAGES * CBS_PER_OPAGE)
734 
735 #define TICKSLOTS 50
736 
737 #define PI_I2C_CLOSED 0
738 #define PI_I2C_RESERVED 1
739 #define PI_I2C_OPENED 2
740 
741 #define PI_SPI_CLOSED 0
742 #define PI_SPI_RESERVED 1
743 #define PI_SPI_OPENED 2
744 
745 #define PI_SER_CLOSED 0
746 #define PI_SER_RESERVED 1
747 #define PI_SER_OPENED 2
748 
749 #define PI_FILE_CLOSED 0
750 #define PI_FILE_RESERVED 1
751 #define PI_FILE_OPENED 2
752 
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
759 
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
768 
769 #define PI_WF_MICROS 1
770 
771 #define BPD 4
772 
773 #define MAX_REPORT 250
774 #define MAX_SAMPLE 4000
775 
776 #define DEFAULT_PWM_IDX 5
777 
778 #define MAX_EMITS (PIPE_BUF / sizeof(gpioReport_t))
779 
780 #define SRX_BUF_SIZE 8192
781 
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
788 
789 #define PI_I2C_SMBUS_READ 1
790 #define PI_I2C_SMBUS_WRITE 0
791 
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
801 
802 #define PI_I2C_SMBUS_BLOCK_MAX 32
803 #define PI_I2C_SMBUS_I2C_BLOCK_MAX 32
804 
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
817 
818 #define PI_MASH_MAX_FREQ 23800000
819 
820 #define FLUSH_PAGES 1024
821 
822 #define MB_DEV_MAJOR 100
823 
824 #define MB_IOCTL _IOWR(MB_DEV_MAJOR, 0, char *)
825 
826 #define MB_DEV1 "/dev/vcio"
827 #define MB_DEV2 "/dev/pigpio-mb"
828 
829 #define BUS_TO_PHYS(x) ((x)&~0xC0000000)
830 
831 #define MB_END_TAG 0
832 #define MB_PROCESS_REQUEST 0
833 
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
838 
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
843 
844 #define PI_SCRIPT_HALT 0
845 #define PI_SCRIPT_RUN 1
846 #define PI_SCRIPT_DELETE 2
847 
848 #define PI_SCRIPT_STACK_SIZE 256
849 
850 #define PI_SPI_FLAGS_CHANNEL(x) ((x&7)<<29)
851 
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)
862 
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)
866 
867 #define PI_STARTING 0
868 #define PI_RUNNING 1
869 #define PI_ENDING 2
870 
871 #define PI_THREAD_NONE 0
872 #define PI_THREAD_STARTED 1
873 #define PI_THREAD_RUNNING 2
874 
875 #define PI_MAX_PATH 512
876 
877 /* typedef ------------------------------------------------------- */
878 
879 typedef void (*callbk_t) ();
880 
881 typedef struct
882 {
883  rawCbs_t cb [128];
884 } dmaPage_t;
885 
886 typedef struct
887 {
889  uint32_t level [LVS_PER_IPAGE];
890  uint32_t gpioOff [OFF_PER_IPAGE];
891  uint32_t tick [TCK_PER_IPAGE];
892  uint32_t gpioOn [ON_PER_IPAGE];
893  uint32_t periphData;
894  uint32_t pad [PAD_PER_IPAGE];
895 } dmaIPage_t;
896 
897 typedef struct
898 {
900  uint32_t OOL [OOL_PER_OPAGE];
901  uint32_t periphData;
902 } dmaOPage_t;
903 
904 typedef struct
905 {
906  uint8_t is;
907  uint8_t pad;
908  uint16_t width;
909  uint16_t range; /* dutycycles specified by 0 .. range */
910  uint16_t freqIdx;
911  uint16_t deferOff;
912  uint16_t deferRng;
913 } gpioInfo_t;
914 
915 typedef struct
916 {
918  unsigned ex;
919  void *userdata;
920 
922  uint32_t wdTick;
923  uint32_t wdLBitV;
924 
927  int nfActive;
928  uint32_t nfTick1;
929  uint32_t nfTick2;
930  uint32_t nfLBitV;
931  uint32_t nfRBitV;
932 
933  uint32_t gfSteadyUs;
934  uint32_t gfTick;
935  uint32_t gfLBitV;
936  uint32_t gfRBitV;
937 
938 } gpioAlert_t;
939 
940 typedef struct
941 {
943  unsigned ex;
944  void *userdata;
945  int ignore;
946  int fired;
947 } eventAlert_t;
948 
949 typedef struct
950 {
951  unsigned gpio;
952  pthread_t *pth;
954  unsigned edge;
955  int timeout;
956  unsigned ex;
957  void *userdata;
958  int inited;
959 } gpioISR_t;
960 
961 typedef struct
962 {
964  unsigned ex;
965  void *userdata;
966 } gpioSignal_t;
967 
968 typedef struct
969 {
971  unsigned ex;
972  void *userdata;
973  uint32_t bits;
975 
976 typedef struct
977 {
979  unsigned ex;
980  void *userdata;
981  unsigned id;
982  unsigned running;
983  unsigned millis;
984  pthread_t pthId;
985 } gpioTimer_t;
986 
987 typedef struct
988 {
989  unsigned id;
990  unsigned state;
991  unsigned request;
992  unsigned run_state;
993  uint32_t waitBits;
994  uint32_t eventBits;
995  uint32_t changedBits;
996  pthread_t *pthIdp;
997  pthread_mutex_t pthMutex;
998  pthread_cond_t pthCond;
1000 } gpioScript_t;
1001 
1002 
1003 typedef struct
1004 {
1005  uint16_t valid;
1006  uint16_t servoIdx;
1007 } clkCfg_t;
1008 
1009 typedef struct
1010 {
1011  uint16_t seqno;
1012  uint16_t state;
1013  uint32_t bits;
1014  uint32_t eventBits;
1015  uint32_t lastReportTick;
1016  int fd;
1017  int pipe;
1019 } gpioNotify_t;
1020 
1021 typedef struct
1022 {
1023  uint16_t state;
1024  int16_t fd;
1025  uint32_t mode;
1026 } fileInfo_t;
1027 
1028 typedef struct
1029 {
1030  uint16_t state;
1031  int16_t fd;
1032  uint32_t addr;
1033  uint32_t flags;
1034  uint32_t funcs;
1035 } i2cInfo_t;
1036 
1037 typedef struct
1038 {
1039  uint16_t state;
1040  int16_t fd;
1041  uint32_t flags;
1042 } serInfo_t;
1043 
1044 typedef struct
1045 {
1046  uint16_t state;
1047  unsigned speed;
1048  uint32_t flags;
1049 } spiInfo_t;
1050 
1051 typedef struct
1052 {
1053  uint32_t alertTicks;
1054  uint32_t lateTicks;
1055  uint32_t moreToDo;
1056  uint32_t diffTick[TICKSLOTS];
1057  uint32_t cbTicks;
1058  uint32_t cbCalls;
1059  uint32_t maxEmit;
1060  uint32_t emitFrags;
1061  uint32_t maxSamples;
1062  uint32_t numSamples;
1063  uint32_t DMARestarts;
1065  uint32_t goodPipeWrite;
1066  uint32_t shortPipeWrite;
1068 } gpioStats_t;
1069 
1070 typedef struct
1071 {
1073  unsigned clockMicros;
1074  unsigned clockPeriph;
1077  unsigned socketPort;
1078  unsigned ifFlags;
1079  unsigned memAllocMode;
1080  unsigned dbgLevel;
1081  unsigned alertFreq;
1082  uint32_t internals;
1083  /*
1084  0-3: dbgLevel
1085  4-7: alertFreq
1086  */
1087 } gpioCfg_t;
1088 
1089 typedef struct
1090 {
1091  uint32_t micros;
1092  uint32_t highMicros;
1093  uint32_t maxMicros;
1094  uint32_t pulses;
1095  uint32_t highPulses;
1096  uint32_t maxPulses;
1097  uint32_t cbs;
1098  uint32_t highCbs;
1099  uint32_t maxCbs;
1100 } wfStats_t;
1101 
1102 typedef struct
1103 {
1104  char *buf;
1105  uint32_t bufSize;
1106  int readPos;
1108  uint32_t fullBit; /* nanoseconds */
1109  uint32_t halfBit; /* nanoseconds */
1110  int timeout; /* millisconds */
1111  uint32_t startBitTick; /* microseconds */
1112  uint32_t nextBitDiff; /* nanoseconds */
1113  int bit;
1114  uint32_t data;
1115  int bytes; /* 1, 2, 4 */
1116  int level;
1117  int dataBits; /* 1-32 */
1118  int invert; /* 0, 1 */
1119 } wfRxSerial_t;
1120 
1121 typedef struct
1122 {
1123  int SDA;
1124  int SCL;
1125  int delay;
1126  int SDAMode;
1127  int SCLMode;
1128  int started;
1129 } wfRxI2C_t;
1130 
1131 typedef struct
1132 {
1133  int CS;
1134  int MISO;
1135  int MOSI;
1136  int SCLK;
1137  int usage;
1138  int delay;
1142  int CSMode;
1144 } wfRxSPI_t;
1145 
1146 typedef struct
1147 {
1148  int mode;
1149  int gpio;
1150  uint32_t baud;
1151  pthread_mutex_t mutex;
1152  union
1153  {
1157  };
1158 } wfRx_t;
1159 
1161 {
1162  uint8_t byte;
1163  uint16_t word;
1165 };
1166 
1168 {
1169  uint8_t read_write;
1170  uint8_t command;
1171  uint32_t size;
1173 };
1174 
1175 typedef struct
1176 {
1177  pi_i2c_msg_t *msgs; /* pointers to pi_i2c_msgs */
1178  uint32_t nmsgs; /* number of pi_i2c_msgs */
1180 
1181 typedef struct
1182 {
1183  unsigned div;
1184  unsigned frac;
1185  unsigned clock;
1186 } clkInf_t;
1187 
1188 typedef struct
1189 {
1190  unsigned handle; /* mbAllocateMemory() */
1191  uintptr_t bus_addr; /* mbLockMemory() */
1192  uintptr_t *virtual_addr; /* mbMapMem() */
1193  unsigned size; /* in bytes */
1194 } DMAMem_t;
1195 
1196 /* global -------------------------------------------------------- */
1197 
1198 /* initialise once then preserve */
1199 
1200 static volatile uint32_t piCores = 0;
1201 static volatile uint32_t pi_peri_phys = 0x20000000;
1202 static volatile uint32_t pi_dram_bus = 0x40000000;
1203 static volatile uint32_t pi_mem_flag = 0x0C;
1204 
1205 static int libInitialised = 0;
1206 
1207 /* initialise every gpioInitialise */
1208 
1209 static struct timespec libStarted;
1210 
1212 
1213 static int numSockNetAddr = 0;
1214 
1215 static uint32_t reportedLevel = 0;
1216 
1217 static int waveClockInited = 0;
1218 static int PWMClockInited = 0;
1219 
1220 static volatile gpioStats_t gpioStats;
1221 
1222 static int gpioMaskSet = 0;
1223 
1224 /* initialise if not libInitialised */
1225 
1226 static uint64_t gpioMask;
1227 
1229 
1230 static int wfc[3]={0, 0, 0};
1231 
1232 static int wfcur=0;
1233 
1235 {
1236  0, 0, PI_WAVE_MAX_MICROS,
1237  0, 0, PI_WAVE_MAX_PULSES,
1238  0, 0, (DMAO_PAGES * CBS_PER_OPAGE)
1239 };
1240 
1242 
1244 
1248 static int waveOutCount = 0;
1249 
1250 static uint32_t *waveEndPtr = NULL;
1251 
1252 static volatile uint32_t alertBits = 0;
1253 static volatile uint32_t monitorBits = 0;
1254 static volatile uint32_t notifyBits = 0;
1255 static volatile uint32_t scriptBits = 0;
1256 static volatile uint32_t gFilterBits = 0;
1257 static volatile uint32_t nFilterBits = 0;
1258 static volatile uint32_t wdogBits = 0;
1259 
1260 static volatile uint32_t scriptEventBits = 0;
1261 
1262 static volatile int runState = PI_STARTING;
1263 
1267 
1269 
1271 
1273 
1275 
1277 
1279 
1284 
1286 
1288 
1290 
1291 static int pwmFreq[PWM_FREQS];
1292 
1293 /* reset after gpioTerminated */
1294 
1295 /* resources which must be released on gpioTerminate */
1296 
1297 static FILE * inpFifo = NULL;
1298 static FILE * outFifo = NULL;
1299 
1300 static int fdLock = -1;
1301 static int fdMem = -1;
1302 static int fdSock = -1;
1303 static int fdPmap = -1;
1304 static int fdMbox = -1;
1305 
1306 static DMAMem_t *dmaMboxBlk = MAP_FAILED;
1307 static uintptr_t * * dmaPMapBlk = MAP_FAILED;
1308 static dmaPage_t * * dmaVirt = MAP_FAILED;
1309 static dmaPage_t * * dmaBus = MAP_FAILED;
1310 
1311 static dmaIPage_t * * dmaIVirt = MAP_FAILED;
1312 static dmaIPage_t * * dmaIBus = MAP_FAILED;
1313 
1314 static dmaOPage_t * * dmaOVirt = MAP_FAILED;
1315 static dmaOPage_t * * dmaOBus = MAP_FAILED;
1316 
1317 static volatile uint32_t * auxReg = MAP_FAILED;
1318 static volatile uint32_t * bscsReg = MAP_FAILED;
1319 static volatile uint32_t * clkReg = MAP_FAILED;
1320 static volatile uint32_t * dmaReg = MAP_FAILED;
1321 static volatile uint32_t * gpioReg = MAP_FAILED;
1322 static volatile uint32_t * padsReg = 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;
1326 static volatile uint32_t * systReg = MAP_FAILED;
1327 
1328 static volatile uint32_t * dmaIn = MAP_FAILED;
1329 static volatile uint32_t * dmaOut = MAP_FAILED;
1330 
1331 static uint32_t hw_clk_freq[3];
1332 static uint32_t hw_pwm_freq[2];
1333 static uint32_t hw_pwm_duty[2];
1334 static uint32_t hw_pwm_real_range[2];
1335 
1336 static volatile gpioCfg_t gpioCfg =
1337 {
1346  0, /* dbgLevel */
1347  0, /* alertFreq */
1348  0, /* internals */
1349 };
1350 
1351 /* no initialisation required */
1352 
1353 static unsigned bufferBlocks; /* number of blocks in buffer */
1354 static unsigned bufferCycles; /* number of cycles */
1355 
1356 static pthread_t pthAlert;
1357 static pthread_t pthFifo;
1358 static pthread_t pthSocket;
1359 
1360 static uint32_t spi_dummy;
1361 
1362 static unsigned old_mode_ce0;
1363 static unsigned old_mode_ce1;
1364 static unsigned old_mode_sclk;
1365 static unsigned old_mode_miso;
1366 static unsigned old_mode_mosi;
1367 
1368 static uint32_t old_spi_cs;
1369 static uint32_t old_spi_clk;
1370 
1371 static unsigned old_mode_ace0;
1372 static unsigned old_mode_ace1;
1373 static unsigned old_mode_ace2;
1374 static unsigned old_mode_asclk;
1375 static unsigned old_mode_amiso;
1376 static unsigned old_mode_amosi;
1377 
1378 static uint32_t old_spi_cntl0;
1379 static uint32_t old_spi_cntl1;
1380 
1381 static uint32_t bscFR;
1382 
1383 /* const --------------------------------------------------------- */
1384 
1385 static const uint8_t clkDef[PI_MAX_GPIO + 1] =
1386 {
1387  /* 0 1 2 3 4 5 6 7 8 9 */
1388  /* 0 */ 0x00, 0x00, 0x00, 0x00, 0x84, 0x94, 0xA4, 0x00, 0x00, 0x00,
1389  /* 1 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1390  /* 2 */ 0x82, 0x92, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1391  /* 3 */ 0x00, 0x00, 0x84, 0x00, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00,
1392  /* 4 */ 0x00, 0x00, 0x94, 0xA4, 0x94, 0x00, 0x00, 0x00, 0x00, 0x00,
1393  /* 5 */ 0x00, 0x00, 0x00, 0x00,
1394 };
1395 
1396 /*
1397  7 6 5 4 3 2 1 0
1398  V . C C . M M M
1399 
1400  V: 0 no clock, 1 has a clock
1401 CC: 00 CLK0, 01 CLK1, 10 CLK2
1402  M: 100 ALT0, 010 ALT5
1403 
1404  gpio4 GPCLK0 ALT0
1405  gpio5 GPCLK1 ALT0 B+ and compute module only (reserved for system use)
1406  gpio6 GPCLK2 ALT0 B+ and compute module only
1407  gpio20 GPCLK0 ALT5 B+ and compute module only
1408  gpio21 GPCLK1 ALT5 Not available on Rev.2 B (reserved for system use)
1409 
1410  gpio32 GPCLK0 ALT0 Compute module only
1411  gpio34 GPCLK0 ALT0 Compute module only
1412  gpio42 GPCLK1 ALT0 Compute module only (reserved for system use)
1413  gpio43 GPCLK2 ALT0 Compute module only
1414  gpio44 GPCLK1 ALT0 Compute module only (reserved for system use)
1415 */
1416 
1417 static const uint8_t PWMDef[PI_MAX_GPIO + 1] =
1418 {
1419  /* 0 1 2 3 4 5 6 7 8 9 */
1420  /* 0 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1421  /* 1 */ 0x00, 0x00, 0x84, 0x94, 0x00, 0x00, 0x00, 0x00, 0x82, 0x92,
1422  /* 2 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1423  /* 3 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1424  /* 4 */ 0x84, 0x94, 0x00, 0x00, 0x00, 0x94, 0x00, 0x00, 0x00, 0x00,
1425  /* 5 */ 0x00, 0x00, 0x85, 0x95,
1426 };
1427 
1428 /*
1429  7 6 5 4 3 2 1 0
1430  V . . P . M M M
1431 
1432  V: 0 no PWM, 1 has a PWM
1433  P: 0 PWM0, 1 PWM1
1434  M: 010 ALT5, 100 ALT0, 101 ALT1
1435 
1436  gpio12 pwm0 ALT0
1437  gpio13 pwm1 ALT0
1438  gpio18 pwm0 ALT5
1439  gpio19 pwm1 ALT5
1440  gpio40 pwm0 ALT0
1441  gpio41 pwm1 ALT0
1442  gpio45 pwm1 ALT0
1443  gpio52 pwm0 ALT1
1444  gpio53 pwm1 ALT1
1445 */
1446 
1447 static const clkCfg_t clkCfg[]=
1448 {
1449  /* valid servo */
1450  { 0, 0}, /* 0 */
1451  { 1, 17}, /* 1 */
1452  { 1, 16}, /* 2 */
1453  { 0, 0}, /* 3 */
1454  { 1, 15}, /* 4 */
1455  { 1, 14}, /* 5 */
1456  { 0, 0}, /* 6 */
1457  { 0, 0}, /* 7 */
1458  { 1, 13}, /* 8 */
1459  { 0, 0}, /* 9 */
1460  { 1, 12}, /* 10 */
1461 };
1462 
1463 static const uint16_t pwmCycles[PWM_FREQS]=
1464  { 1, 2, 4, 5, 8, 10, 16, 20, 25,
1465  32, 40, 50, 80, 100, 160, 200, 400, 800};
1466 
1467 static const uint16_t pwmRealRange[PWM_FREQS]=
1468  { 25, 50, 100, 125, 200, 250, 400, 500, 625,
1469  800, 1000, 1250, 2000, 2500, 4000, 5000, 10000, 20000};
1470 
1471 /* prototype ----------------------------------------------------- */
1472 
1473 static void intNotifyBits(void);
1474 
1475 static void intScriptBits(void);
1476 
1477 static void intScriptEventBits(void);
1478 
1479 static int gpioNotifyOpenInBand(int fd);
1480 
1481 static void initHWClk
1482  (int clkCtl, int clkDiv, int clkSrc, int divI, int divF, int MASH);
1483 
1484 static void initDMAgo(volatile uint32_t *dmaAddr, uint32_t cbAddr);
1485 
1486 int gpioWaveTxStart(unsigned wave_mode); /* deprecated */
1487 
1488 static void closeOrphanedNotifications(int slot, int fd);
1489 
1490 
1491 /* ======================================================================= */
1492 
1494 {
1495  int i, c, len, valid;
1496 
1497  len = strlen(name);
1498 
1499  valid = 1;
1500 
1501  for (i=0; i<len; i++)
1502  {
1503  c = name[i];
1504 
1505  if ((!isalnum(c)) && (c != '_') && (c != '-'))
1506  {
1507  valid = 0;
1508  break;
1509  }
1510  }
1511  return valid;
1512 }
1513 
1514 /* ----------------------------------------------------------------------- */
1515 
1516 static char * myTimeStamp()
1517 {
1518  static struct timeval last;
1519  static char buf[32];
1520  struct timeval now;
1521 
1522  struct tm tmp;
1523 
1524  gettimeofday(&now, NULL);
1525 
1526  if (now.tv_sec != last.tv_sec)
1527  {
1528  localtime_r(&now.tv_sec, &tmp);
1529  strftime(buf, sizeof(buf), "%F %T", &tmp);
1530  last.tv_sec = now.tv_sec;
1531  }
1532 
1533  return buf;
1534 }
1535 
1536 /* ----------------------------------------------------------------------- */
1537 
1538 int myPathBad(char *name)
1539 {
1540  int i, c, len, in_part, parts, last_char_dot;
1541  char *bad="/*?.";
1542 
1543  parts = 0;
1544  in_part = 0;
1545  last_char_dot = 0;
1546 
1547  len = strlen(name);
1548 
1549  for (i=0; i<len; i++)
1550  {
1551  c = name[i];
1552 
1553  if (memchr(bad, c, 4)) /* wildcard or directory character */
1554  {
1555  if (c == '.')
1556  {
1557  if (last_char_dot) return 1;
1558  last_char_dot = 1;
1559  }
1560  else last_char_dot = 0;
1561 
1562  in_part = 0;
1563  }
1564  else /* normal character */
1565  {
1566  last_char_dot = 0;
1567 
1568  if (!in_part) parts++;
1569 
1570  in_part = 1;
1571  }
1572  }
1573 
1574  if (parts < 2) return 1; else return 0;
1575 }
1576 
1577 /* ----------------------------------------------------------------------- */
1578 
1579 static char *myBuf2Str(unsigned count, char *buf)
1580 {
1581  static char str[128];
1582  int i, c;
1583 
1584  if (count && buf)
1585  {
1586  if (count > 40) c = 40; else c = count;
1587 
1588  for (i=0; i<c; i++) sprintf(str+(3*i), "%02X ", buf[i]);
1589  str[(3*c)-1] = 0;
1590  }
1591  else str[0] = 0;
1592 
1593  return str;
1594 }
1595 
1596 /* ----------------------------------------------------------------------- */
1597 
1598 static int my_smbus_access(
1599  int fd, char rw, uint8_t cmd, int size, union my_smbus_data *data)
1600 {
1601  struct my_smbus_ioctl_data args;
1602 
1603  DBG(DBG_INTERNAL, "rw=%d reg=%d cmd=%d data=%s",
1604  rw, cmd, size, myBuf2Str(data->byte+1, (char*)data));
1605 
1606  args.read_write = rw;
1607  args.command = cmd;
1608  args.size = size;
1609  args.data = data;
1610 
1611  return ioctl(fd, PI_I2C_SMBUS, &args);
1612 }
1613 
1614 /* ----------------------------------------------------------------------- */
1615 
1616 static void myGpioSetMode(unsigned gpio, unsigned mode)
1617 {
1618  int reg, shift;
1619 
1620  reg = gpio/10;
1621  shift = (gpio%10) * 3;
1622 
1623  gpioReg[reg] = (gpioReg[reg] & ~(7<<shift)) | (mode<<shift);
1624 }
1625 
1626 
1627 /* ----------------------------------------------------------------------- */
1628 
1629 static int myGpioRead(unsigned gpio)
1630 {
1631  if ((*(gpioReg + GPLEV0 + BANK) & BIT) != 0) return PI_ON;
1632  else return PI_OFF;
1633 }
1634 
1635 
1636 /* ----------------------------------------------------------------------- */
1637 
1638 static void myGpioWrite(unsigned gpio, unsigned level)
1639 {
1640  if (level == PI_OFF) *(gpioReg + GPCLR0 + BANK) = BIT;
1641  else *(gpioReg + GPSET0 + BANK) = BIT;
1642 }
1643 
1644 /* ----------------------------------------------------------------------- */
1645 
1646 static void myGpioSleep(int seconds, int micros)
1647 {
1648  struct timespec ts, rem;
1649 
1650  ts.tv_sec = seconds;
1651  ts.tv_nsec = micros * 1000;
1652 
1653  while (clock_nanosleep(CLOCK_REALTIME, 0, &ts, &rem))
1654  {
1655  /* copy remaining time to ts */
1656  ts = rem;
1657  }
1658 }
1659 
1660 /* ----------------------------------------------------------------------- */
1661 
1662 static uint32_t myGpioDelay(uint32_t micros)
1663 {
1664  uint32_t start;
1665 
1666  start = systReg[SYST_CLO];
1667 
1668  if (micros <= PI_MAX_BUSY_DELAY)
1669  {
1670  while ((systReg[SYST_CLO] - start) <= micros);
1671  }
1672  else
1673  {
1674  myGpioSleep(micros/MILLION, micros%MILLION);
1675  }
1676 
1677  return (systReg[SYST_CLO] - start);
1678 }
1679 
1680 /* ----------------------------------------------------------------------- */
1681 
1682 static void myCreatePipe(char * name, int perm)
1683 {
1684  unlink(name);
1685 
1686  mkfifo(name, perm);
1687 
1688  if (chmod(name, perm) < 0)
1689  {
1690  DBG(DBG_ALWAYS, "Can't set permissions (%d) for %s, %m", perm, name);
1691  return;
1692  }
1693 }
1694 
1695 /* ----------------------------------------------------------------------- */
1696 
1697 static void myOffPageSlot(int pos, int * page, int * slot)
1698 {
1699  *page = pos/OFF_PER_IPAGE;
1700  *slot = pos%OFF_PER_IPAGE;
1701 }
1702 
1703 /* ----------------------------------------------------------------------- */
1704 
1705 static void myLvsPageSlot(int pos, int * page, int * slot)
1706 {
1707  *page = pos/LVS_PER_IPAGE;
1708  *slot = pos%LVS_PER_IPAGE;
1709 }
1710 
1711 /* ----------------------------------------------------------------------- */
1712 
1713 static void myTckPageSlot(int pos, int * page, int * slot)
1714 {
1715  *page = pos/TCK_PER_IPAGE;
1716  *slot = pos%TCK_PER_IPAGE;
1717 }
1718 
1719 /* ----------------------------------------------------------------------- */
1720 
1721 static uint32_t myGetLevel(int pos)
1722 {
1723  uint32_t level;
1724  int page, slot;
1725 
1726  myLvsPageSlot(pos, &page, &slot);
1727 
1728  level = dmaIVirt[page]->level[slot];
1729 
1730  return level;
1731 }
1732 
1733 /* ----------------------------------------------------------------------- */
1734 
1735 static int myI2CGetPar(char *inBuf, int *inPos, int inLen, int *esc)
1736 {
1737  int bytes;
1738 
1739  if (*esc) bytes = 2; else bytes = 1;
1740 
1741  *esc = 0;
1742 
1743  if (*inPos <= (inLen - bytes))
1744  {
1745  if (bytes == 1)
1746  {
1747  return inBuf[(*inPos)++];
1748  }
1749  else
1750  {
1751  (*inPos) += 2;
1752  return inBuf[*inPos-2] + (inBuf[*inPos-1]<<8);
1753  }
1754  }
1755  return -1;
1756 }
1757 
1758 /* ----------------------------------------------------------------------- */
1759 
1760 static uint32_t myGetTick(int pos)
1761 {
1762  uint32_t tick;
1763  int page, slot;
1764 
1765  myTckPageSlot(pos, &page, &slot);
1766 
1767  tick = dmaIVirt[page]->tick[slot];
1768 
1769  return tick;
1770 }
1771 
1772 static int myPermit(unsigned gpio)
1773 {
1774  if (gpio <= PI_MAX_GPIO)
1775  {
1776  if (gpioMask & ((uint64_t)(1)<<gpio)) return 1;
1777  else return 0;
1778  }
1779  return 1; /* will fail for bad gpio number */
1780 }
1781 
1782 static void flushMemory(void)
1783 {
1784  static int val = 0;
1785 
1786  void *dummy;
1787 
1788  dummy = mmap(
1789  0, (FLUSH_PAGES*PAGE_SIZE),
1790  PROT_READ|PROT_WRITE|PROT_EXEC,
1791  MAP_SHARED|MAP_ANONYMOUS|MAP_NORESERVE|MAP_LOCKED,
1792  -1, 0);
1793 
1794  if (dummy == MAP_FAILED)
1795  {
1796  DBG(DBG_STARTUP, "mmap dummy failed (%m)");
1797  }
1798  else
1799  {
1800  memset(dummy, val++, (FLUSH_PAGES*PAGE_SIZE));
1801  memset(dummy, val++, (FLUSH_PAGES*PAGE_SIZE));
1802  munmap(dummy, FLUSH_PAGES*PAGE_SIZE);
1803  }
1804 }
1805 
1806 /* ----------------------------------------------------------------------- */
1807 
1808 static void wfRx_lock(int i)
1809 {
1810  pthread_mutex_lock(&wfRx[i].mutex);
1811 }
1812 
1813 /* ----------------------------------------------------------------------- */
1814 
1815 static void wfRx_unlock(int i)
1816 {
1817  pthread_mutex_unlock(&wfRx[i].mutex);
1818 }
1819 
1820 /* ----------------------------------------------------------------------- */
1821 
1822 static void spinWhileStarting(void)
1823 {
1824  while (runState == PI_STARTING)
1825  {
1826  if (piCores == 1) myGpioDelay(1000);
1827  else flushMemory();
1828  }
1829 }
1830 
1831 /* ----------------------------------------------------------------------- */
1832 
1833 static int myDoCommand(uint32_t *p, unsigned bufSize, char *buf)
1834 {
1835  int res, i, j;
1836  uint32_t mask;
1837  uint32_t tmp1, tmp2, tmp3, tmp4, tmp5;
1838  gpioPulse_t *pulse;
1839  bsc_xfer_t xfer;
1840  int masked;
1841  res = 0;
1842 
1843  switch (p[0])
1844  {
1845  case PI_CMD_BC1:
1846  mask = gpioMask;
1847 
1848  res = gpioWrite_Bits_0_31_Clear(p[1]&mask);
1849 
1850  if ((mask | p[1]) != mask)
1851  {
1852  DBG(DBG_USER,
1853  "gpioWrite_Bits_0_31_Clear: bad bits %08X (permissions %08X)",
1854  p[1], mask);
1855  res = PI_SOME_PERMITTED;
1856  }
1857  break;
1858 
1859  case PI_CMD_BC2:
1860  mask = gpioMask>>32;
1861 
1862  res = gpioWrite_Bits_32_53_Clear(p[1]&mask);
1863 
1864  if ((mask | p[1]) != mask)
1865  {
1866  DBG(DBG_USER,
1867  "gpioWrite_Bits_32_53_Clear: bad bits %08X (permissions %08X)",
1868  p[1], mask);
1869  res = PI_SOME_PERMITTED;
1870  }
1871  break;
1872 
1873  case PI_CMD_BI2CC:
1874  res = bbI2CClose(p[1]);
1875  break;
1876 
1877  case PI_CMD_BI2CO:
1878  memcpy(&p[4], buf, 4);
1879  res = bbI2COpen(p[1], p[2], p[4]);
1880  break;
1881 
1882  case PI_CMD_BI2CZ:
1883  /* use half buffer for write, half buffer for read */
1884  if (p[3] > (bufSize/2)) p[3] = bufSize/2;
1885  res = bbI2CZip(p[1], buf, p[3], buf+(bufSize/2), bufSize/2);
1886  if (res > 0)
1887  {
1888  memcpy(buf, buf+(bufSize/2), res);
1889  }
1890  break;
1891 
1892  case PI_CMD_BSCX:
1893  xfer.control = p[1];
1894  if (p[3] > BSC_FIFO_SIZE) p[3] = BSC_FIFO_SIZE;
1895  xfer.txCnt = p[3];
1896  if (p[3]) memcpy(&xfer.txBuf, buf, p[3]);
1897  res = bscXfer(&xfer);
1898  if (res >= 0)
1899  {
1900  memcpy(buf, &res, 4);
1901  res = 4 + xfer.rxCnt;
1902  if (res > 4) memcpy(buf+4, &xfer.rxBuf, res-4);
1903  }
1904  break;
1905 
1906  case PI_CMD_BSPIO:
1907 
1908  memcpy(&tmp1, buf+ 0, 4); // MISO
1909  memcpy(&tmp2, buf+ 4, 4); // MOSI
1910  memcpy(&tmp3, buf+ 8, 4); // SCLK
1911  memcpy(&tmp4, buf+12, 4); // baud
1912  memcpy(&tmp5, buf+16, 4); // flags
1913 
1914  if (!myPermit(p[1]))
1915  {
1916  DBG(DBG_USER,
1917  "bbSPIOpen: gpio %d, no permission to update CS", p[1]);
1918  res = PI_NOT_PERMITTED;
1919  }
1920 
1921  if (!myPermit(tmp1))
1922  {
1923  DBG(DBG_USER,
1924  "bbSPIOpen: gpio %d, no permission to update MISO", tmp1);
1925  res = PI_NOT_PERMITTED;
1926  }
1927 
1928  if (!myPermit(tmp2))
1929  {
1930  DBG(DBG_USER,
1931  "bbSPIOpen: gpio %d, no permission to update MOSI", tmp2);
1932  res = PI_NOT_PERMITTED;
1933  }
1934 
1935  if (!myPermit(tmp3))
1936  {
1937  DBG(DBG_USER,
1938  "bbSPIOpen: gpio %d, no permission to update SCLK", tmp3);
1939  res = PI_NOT_PERMITTED;
1940  }
1941 
1942  if (!res) res = bbSPIOpen(p[1], tmp1, tmp2, tmp3, tmp4, tmp5);
1943  break;
1944 
1945  case PI_CMD_BSPIC:
1946  res = bbSPIClose(p[1]);
1947  break;
1948 
1949  case PI_CMD_BSPIX:
1950  if (p[3] > bufSize) p[3] = bufSize;
1951  res = bbSPIXfer(p[1], buf, buf, p[3]);
1952  break;
1953 
1954  case PI_CMD_BR1: res = gpioRead_Bits_0_31(); break;
1955 
1956  case PI_CMD_BR2: res = gpioRead_Bits_32_53(); break;
1957 
1958  case PI_CMD_BS1:
1959  mask = gpioMask;
1960 
1961  res = gpioWrite_Bits_0_31_Set(p[1]&mask);
1962 
1963  if ((mask | p[1]) != mask)
1964  {
1965  DBG(DBG_USER,
1966  "gpioWrite_Bits_0_31_Set: bad bits %08X (permissions %08X)",
1967  p[1], mask);
1968  res = PI_SOME_PERMITTED;
1969  }
1970  break;
1971 
1972  case PI_CMD_BS2:
1973  mask = gpioMask>>32;
1974 
1975  res = gpioWrite_Bits_32_53_Set(p[1]&mask);
1976 
1977  if ((mask | p[1]) != mask)
1978  {
1979  DBG(DBG_USER,
1980  "gpioWrite_Bits_32_53_Set: bad bits %08X (permissions %08X)",
1981  p[1], mask);
1982  res = PI_SOME_PERMITTED;
1983  }
1984  break;
1985 
1986  case PI_CMD_CF1:
1987  res = gpioCustom1(p[1], p[2], buf, p[3]);
1988  break;
1989 
1990  case PI_CMD_CF2:
1991  /* a couple of extra precautions for untrusted code */
1992  if (p[2] > bufSize) p[2] = bufSize;
1993  res = gpioCustom2(p[1], buf, p[3], buf, p[2]);
1994  if (res > p[2]) res = p[2];
1995  break;
1996 
1997  case PI_CMD_CGI: res = gpioCfgGetInternals(); break;
1998 
1999  case PI_CMD_CSI: res = gpioCfgSetInternals(p[1]); break;
2000 
2001  case PI_CMD_EVM: res = eventMonitor(p[1], p[2]); break;
2002 
2003  case PI_CMD_EVT: res = eventTrigger(p[1]); break;
2004 
2005  case PI_CMD_FC: res = fileClose(p[1]); break;
2006 
2007  case PI_CMD_FG:
2008  res = gpioGlitchFilter(p[1], p[2]);
2009  break;
2010 
2011  case PI_CMD_FL:
2012  if (p[1] > bufSize) p[1] = bufSize;
2013  res = fileList(buf, buf, p[1]);
2014  break;
2015 
2016  case PI_CMD_FN:
2017  memcpy(&p[4], buf, 4);
2018  res = gpioNoiseFilter(p[1], p[2], p[4]);
2019  break;
2020 
2021  case PI_CMD_FO: res = fileOpen(buf, p[1]); break;
2022 
2023  case PI_CMD_FR:
2024  if (p[2] > bufSize) p[2] = bufSize;
2025  res = fileRead(p[1], buf, p[2]);
2026  break;
2027 
2028  case PI_CMD_FS:
2029  memcpy(&p[4], buf, 4);
2030  res = fileSeek(p[1], p[2], p[4]);
2031  break;
2032 
2033  case PI_CMD_FW: res = fileWrite(p[1], buf, p[3]); break;
2034 
2035  case PI_CMD_GDC: res = gpioGetPWMdutycycle(p[1]); break;
2036 
2037  case PI_CMD_GPW: res = gpioGetServoPulsewidth(p[1]); break;
2038 
2039  case PI_CMD_HC:
2040  /* special case to allow password in upper byte */
2041  if (myPermit(p[1]&0xFFFFFF)) res = gpioHardwareClock(p[1], p[2]);
2042  else
2043  {
2044  DBG(DBG_USER,
2045  "gpioHardwareClock: gpio %d, no permission to update",
2046  p[1] & 0xFFFFFF);
2047  res = PI_NOT_PERMITTED;
2048  }
2049  break;
2050 
2051  case PI_CMD_HELP: break;
2052 
2053  case PI_CMD_HP:
2054  if (myPermit(p[1]))
2055  {
2056  memcpy(&p[4], buf, 4);
2057  res = gpioHardwarePWM(p[1], p[2], p[4]);
2058  }
2059  else
2060  {
2061  DBG(DBG_USER,
2062  "gpioHardwarePWM: gpio %d, no permission to update", p[1]);
2063  res = PI_NOT_PERMITTED;
2064  }
2065  break;
2066 
2067  case PI_CMD_HWVER: res = gpioHardwareRevision(); break;
2068 
2069 
2070 
2071  case PI_CMD_I2CC: res = i2cClose(p[1]); break;
2072 
2073  case PI_CMD_I2CO:
2074  memcpy(&p[4], buf, 4);
2075  res = i2cOpen(p[1], p[2], p[4]);
2076  break;
2077 
2078  case PI_CMD_I2CPC:
2079  memcpy(&p[4], buf, 4);
2080  res = i2cProcessCall(p[1], p[2], p[4]);
2081  break;
2082 
2083  case PI_CMD_I2CPK:
2084  res = i2cBlockProcessCall(p[1], p[2], buf, p[3]);
2085  break;
2086 
2087  case PI_CMD_I2CRB: res = i2cReadByteData(p[1], p[2]); break;
2088 
2089  case PI_CMD_I2CRD:
2090  if (p[2] > bufSize) p[2] = bufSize;
2091  res = i2cReadDevice(p[1], buf, p[2]);
2092  break;
2093 
2094  case PI_CMD_I2CRI:
2095  memcpy(&p[4], buf, 4);
2096  res = i2cReadI2CBlockData(p[1], p[2], buf, p[4]);
2097  break;
2098 
2099  case PI_CMD_I2CRK:
2100  res = i2cReadBlockData(p[1], p[2], buf);
2101  break;
2102 
2103  case PI_CMD_I2CRS: res = i2cReadByte(p[1]); break;
2104 
2105  case PI_CMD_I2CRW: res = i2cReadWordData(p[1], p[2]); break;
2106 
2107  case PI_CMD_I2CWB:
2108  memcpy(&p[4], buf, 4);
2109  res = i2cWriteByteData(p[1], p[2], p[4]);
2110  break;
2111 
2112  case PI_CMD_I2CWD:
2113  res = i2cWriteDevice(p[1], buf, p[3]);
2114  break;
2115 
2116  case PI_CMD_I2CWI:
2117  res = i2cWriteI2CBlockData(p[1], p[2], buf, p[3]);
2118  break;
2119 
2120  case PI_CMD_I2CWK:
2121  res = i2cWriteBlockData(p[1], p[2], buf, p[3]);
2122  break;
2123 
2124  case PI_CMD_I2CWQ: res = i2cWriteQuick(p[1], p[2]); break;
2125 
2126  case PI_CMD_I2CWS: res = i2cWriteByte(p[1], p[2]); break;
2127 
2128  case PI_CMD_I2CWW:
2129  memcpy(&p[4], buf, 4);
2130  res = i2cWriteWordData(p[1], p[2], p[4]);
2131  break;
2132 
2133  case PI_CMD_I2CZ:
2134  /* use half buffer for write, half buffer for read */
2135  if (p[3] > (bufSize/2)) p[3] = bufSize/2;
2136  res = i2cZip(p[1], buf, p[3], buf+(bufSize/2), bufSize/2);
2137  if (res > 0)
2138  {
2139  memcpy(buf, buf+(bufSize/2), res);
2140  }
2141  break;
2142 
2143  case PI_CMD_MICS:
2144  if (p[1] <= PI_MAX_MICS_DELAY) myGpioDelay(p[1]);
2145  else res = PI_BAD_MICS_DELAY;
2146  break;
2147 
2148  case PI_CMD_MILS:
2149  if (p[1] <= PI_MAX_MILS_DELAY) myGpioDelay(p[1] * 1000);
2150  else res = PI_BAD_MILS_DELAY;
2151  break;
2152 
2153  case PI_CMD_MODEG: res = gpioGetMode(p[1]); break;
2154 
2155  case PI_CMD_MODES:
2156  if (myPermit(p[1])) res = gpioSetMode(p[1], p[2]);
2157  else
2158  {
2159  DBG(DBG_USER,
2160  "gpioSetMode: gpio %d, no permission to update", p[1]);
2161  res = PI_NOT_PERMITTED;
2162  }
2163  break;
2164 
2165  case PI_CMD_NB: res = gpioNotifyBegin(p[1], p[2]); break;
2166 
2167  case PI_CMD_NC: res = gpioNotifyClose(p[1]); break;
2168 
2169  case PI_CMD_NO: res = gpioNotifyOpen(); break;
2170 
2171  case PI_CMD_NP: res = gpioNotifyPause(p[1]); break;
2172 
2173  case PI_CMD_PADG: res = gpioGetPad(p[1]); break;
2174 
2175  case PI_CMD_PADS: res = gpioSetPad(p[1], p[2]); break;
2176 
2177  case PI_CMD_PFG: res = gpioGetPWMfrequency(p[1]); break;
2178 
2179  case PI_CMD_PFS:
2180  if (myPermit(p[1])) res = gpioSetPWMfrequency(p[1], p[2]);
2181  else
2182  {
2183  DBG(DBG_USER,
2184  "gpioSetPWMfrequency: gpio %d, no permission to update", p[1]);
2185  res = PI_NOT_PERMITTED;
2186  }
2187  break;
2188 
2189  case PI_CMD_PIGPV: res = gpioVersion(); break;
2190 
2191  case PI_CMD_PRG: res = gpioGetPWMrange(p[1]); break;
2192 
2193  case PI_CMD_PROC:
2194  res = gpioStoreScript(buf);
2195  break;
2196 
2197  case PI_CMD_PROCD: res = gpioDeleteScript(p[1]); break;
2198 
2199  case PI_CMD_PROCP:
2200  res = gpioScriptStatus(p[1], (uint32_t *)buf);
2201  break;
2202 
2203  case PI_CMD_PROCR:
2204  res = gpioRunScript(p[1], p[3]/4, (uint32_t *)buf);
2205  break;
2206 
2207  case PI_CMD_PROCS: res = gpioStopScript(p[1]); break;
2208 
2209  case PI_CMD_PROCU:
2210  res = gpioUpdateScript(p[1], p[3]/4, (uint32_t *)buf);
2211  break;
2212 
2213  case PI_CMD_PRRG: res = gpioGetPWMrealRange(p[1]); break;
2214 
2215  case PI_CMD_PRS:
2216  if (myPermit(p[1])) res = gpioSetPWMrange(p[1], p[2]);
2217  else
2218  {
2219  DBG(DBG_USER,
2220  "gpioSetPWMrange: gpio %d, no permission to update", p[1]);
2221  res = PI_NOT_PERMITTED;
2222  }
2223  break;
2224 
2225  case PI_CMD_PUD:
2226  if (myPermit(p[1])) res = gpioSetPullUpDown(p[1], p[2]);
2227  else
2228  {
2229  DBG(DBG_USER,
2230  "gpioSetPullUpDown: gpio %d, no permission to update", p[1]);
2231  res = PI_NOT_PERMITTED;
2232  }
2233  break;
2234 
2235  case PI_CMD_PWM:
2236  if (myPermit(p[1])) res = gpioPWM(p[1], p[2]);
2237  else
2238  {
2239  DBG(DBG_USER, "gpioPWM: gpio %d, no permission to update", p[1]);
2240  res = PI_NOT_PERMITTED;
2241  }
2242  break;
2243 
2244  case PI_CMD_READ: res = gpioRead(p[1]); break;
2245 
2246  case PI_CMD_SERVO:
2247  if (myPermit(p[1])) res = gpioServo(p[1], p[2]);
2248  else
2249  {
2250  DBG(DBG_USER,
2251  "gpioServo: gpio %d, no permission to update", p[1]);
2252  res = PI_NOT_PERMITTED;
2253  }
2254  break;
2255 
2256 
2257 
2258  case PI_CMD_SERRB: res = serReadByte(p[1]); break;
2259 
2260  case PI_CMD_SERWB: res = serWriteByte(p[1], p[2]); break;
2261 
2262  case PI_CMD_SERC: res = serClose(p[1]); break;
2263 
2264  case PI_CMD_SERDA: res = serDataAvailable(p[1]); break;
2265 
2266  case PI_CMD_SERO: res = serOpen(buf, p[1], p[2]); break;
2267 
2268  case PI_CMD_SERR:
2269  if (p[2] > bufSize) p[2] = bufSize;
2270  res = serRead(p[1], buf, p[2]);
2271  break;
2272 
2273  case PI_CMD_SERW: res = serWrite(p[1], buf, p[3]); break;
2274 
2275 
2276  case PI_CMD_SHELL:
2277  res = shell(buf, buf+p[1]+1);
2278  break;
2279 
2280 
2281  case PI_CMD_SLR:
2282  if (p[2] > bufSize) p[2] = bufSize;
2283  res = gpioSerialRead(p[1], buf, p[2]);
2284  break;
2285 
2286  case PI_CMD_SLRC: res = gpioSerialReadClose(p[1]); break;
2287 
2288  case PI_CMD_SLRO:
2289  memcpy(&p[4], buf, 4);
2290  res = gpioSerialReadOpen(p[1], p[2], p[4]); break;
2291 
2292  case PI_CMD_SLRI: res = gpioSerialReadInvert(p[1], p[2]); break;
2293 
2294  case PI_CMD_SPIC:
2295  res = spiClose(p[1]);
2296  break;
2297 
2298  case PI_CMD_SPIO:
2299  memcpy(&p[4], buf, 4);
2300  res = spiOpen(p[1], p[2], p[4]);
2301  break;
2302 
2303  case PI_CMD_SPIR:
2304  if (p[2] > bufSize) p[2] = bufSize;
2305  res = spiRead(p[1], buf, p[2]);
2306  break;
2307 
2308  case PI_CMD_SPIW:
2309  if (p[3] > bufSize) p[3] = bufSize;
2310  res = spiWrite(p[1], buf, p[3]);
2311  break;
2312 
2313  case PI_CMD_SPIX:
2314  if (p[3] > bufSize) p[3] = bufSize;
2315  res = spiXfer(p[1], buf, buf, p[3]);
2316  break;
2317 
2318  case PI_CMD_TICK: res = gpioTick(); break;
2319 
2320  case PI_CMD_TRIG:
2321  if (myPermit(p[1]))
2322  {
2323  memcpy(&p[4], buf, 4);
2324  res = gpioTrigger(p[1], p[2], p[4]);
2325  }
2326  else
2327  {
2328  DBG(DBG_USER,
2329  "gpioTrigger: gpio %d, no permission to update", p[1]);
2330  res = PI_NOT_PERMITTED;
2331  }
2332  break;
2333 
2334  case PI_CMD_WDOG: res = gpioSetWatchdog(p[1], p[2]); break;
2335 
2336  case PI_CMD_WRITE:
2337  if (myPermit(p[1])) res = gpioWrite(p[1], p[2]);
2338  else
2339  {
2340  DBG(DBG_USER, "gpioWrite: gpio %d, no permission to update", p[1]);
2341  res = PI_NOT_PERMITTED;
2342  }
2343  break;
2344 
2345 
2346 
2347  case PI_CMD_WVAG:
2348 
2349  /* need to mask off any non permitted gpios */
2350 
2351  mask = gpioMask;
2352  pulse = (gpioPulse_t *)buf;
2353  j = p[3]/sizeof(gpioPulse_t);
2354  masked = 0;
2355 
2356  for (i=0; i<j; i++)
2357  {
2358  tmp1 = pulse[i].gpioOn & mask;
2359  if (tmp1 != pulse[i].gpioOn)
2360  {
2361  pulse[i].gpioOn = tmp1;
2362  masked = 1;
2363  }
2364 
2365  tmp1 = pulse[i].gpioOff & mask;
2366  if (tmp1 != pulse[i].gpioOff)
2367  {
2368  pulse[i].gpioOff = tmp1;
2369  masked = 1;
2370  }
2371  DBG(DBG_SCRIPT, "on=%X off=%X delay=%d",
2372  pulse[i].gpioOn, pulse[i].gpioOff, pulse[i].usDelay);
2373  }
2374 
2375  res = gpioWaveAddGeneric(j, pulse);
2376 
2377  /* report permission error unless another error occurred */
2378  if (masked && (res >= 0)) res = PI_SOME_PERMITTED;
2379 
2380  break;
2381 
2382  case PI_CMD_WVAS:
2383  if (myPermit(p[1]))
2384  {
2385  memcpy(&tmp1, buf, 4); /* databits */
2386  memcpy(&tmp2, buf+4, 4); /* stophalfbits */
2387  memcpy(&tmp3, buf+8, 4); /* offset */
2388  res = gpioWaveAddSerial
2389  (p[1], p[2], tmp1, tmp2, tmp3, p[3]-12, buf+12);
2390  }
2391  else
2392  {
2393  DBG(
2394  DBG_USER,
2395  "gpioWaveAddSerial: gpio %d, no permission to update", p[1]);
2396  res = PI_NOT_PERMITTED;
2397  }
2398  break;
2399 
2400  case PI_CMD_WVBSY: res = gpioWaveTxBusy(); break;
2401 
2402  case PI_CMD_WVCHA:
2403  if (p[3] > bufSize) p[3] = bufSize;
2404  res = gpioWaveChain(buf, p[3]);
2405  break;
2406 
2407 
2408  case PI_CMD_WVCLR: res = gpioWaveClear(); break;
2409 
2410  case PI_CMD_WVCRE: res = gpioWaveCreate(); break;
2411 
2412  case PI_CMD_WVDEL: res = gpioWaveDelete(p[1]); break;
2413 
2415 
2416  case PI_CMD_WVGOR: res = gpioWaveTxStart(PI_WAVE_MODE_REPEAT); break;
2417 
2418  case PI_CMD_WVHLT: res = gpioWaveTxStop(); break;
2419 
2420  case PI_CMD_WVNEW: res = gpioWaveAddNew(); break;
2421 
2422  case PI_CMD_WVSC:
2423  switch(p[1])
2424  {
2425  case 0: res = gpioWaveGetCbs(); break;
2426  case 1: res = gpioWaveGetHighCbs(); break;
2427  case 2: res = gpioWaveGetMaxCbs(); break;
2428  default: res = PI_BAD_WVSC_COMMND;
2429  }
2430  break;
2431 
2432  case PI_CMD_WVSM:
2433  switch(p[1])
2434  {
2435  case 0: res = gpioWaveGetMicros(); break;
2436  case 1: res = gpioWaveGetHighMicros(); break;
2437  case 2: res = gpioWaveGetMaxMicros(); break;
2438  default: res = PI_BAD_WVSM_COMMND;
2439  }
2440  break;
2441 
2442  case PI_CMD_WVSP:
2443  switch(p[1])
2444  {
2445  case 0: res = gpioWaveGetPulses(); break;
2446  case 1: res = gpioWaveGetHighPulses(); break;
2447  case 2: res = gpioWaveGetMaxPulses(); break;
2448  default: res = PI_BAD_WVSP_COMMND;
2449  }
2450  break;
2451 
2452  case PI_CMD_WVTAT: res = gpioWaveTxAt(); break;
2453 
2454  case PI_CMD_WVTX:
2455  res = gpioWaveTxSend(p[1], PI_WAVE_MODE_ONE_SHOT); break;
2456 
2457  case PI_CMD_WVTXM:
2458  res = gpioWaveTxSend(p[1], p[2]); break;
2459 
2460  case PI_CMD_WVTXR:
2461  res = gpioWaveTxSend(p[1], PI_WAVE_MODE_REPEAT); break;
2462 
2463  default:
2464  res = PI_UNKNOWN_COMMAND;
2465  break;
2466  }
2467 
2468  return res;
2469 }
2470 
2471 /* ----------------------------------------------------------------------- */
2472 
2473 static void mySetGpioOff(unsigned gpio, int pos)
2474 {
2475  int page, slot;
2476 
2477  myOffPageSlot(pos, &page, &slot);
2478 
2479  dmaIVirt[page]->gpioOff[slot] |= (1<<gpio);
2480 }
2481 
2482 /* ----------------------------------------------------------------------- */
2483 
2484 static void myClearGpioOff(unsigned gpio, int pos)
2485 {
2486  int page, slot;
2487 
2488  myOffPageSlot(pos, &page, &slot);
2489 
2490  dmaIVirt[page]->gpioOff[slot] &= ~(1<<gpio);
2491 }
2492 
2493 /* ----------------------------------------------------------------------- */
2494 
2495 static void mySetGpioOn(unsigned gpio, int pos)
2496 {
2497  int page, slot;
2498 
2499  page = pos/ON_PER_IPAGE;
2500  slot = pos%ON_PER_IPAGE;
2501 
2502  dmaIVirt[page]->gpioOn[slot] |= (1<<gpio);
2503 }
2504 
2505 /* ----------------------------------------------------------------------- */
2506 
2507 static void myClearGpioOn(unsigned gpio, int pos)
2508 {
2509  int page, slot;
2510 
2511  page = pos/ON_PER_IPAGE;
2512  slot = pos%ON_PER_IPAGE;
2513 
2514  dmaIVirt[page]->gpioOn[slot] &= ~(1<<gpio);
2515 }
2516 
2517 /* ----------------------------------------------------------------------- */
2518 
2519 static void myGpioSetPwm(unsigned gpio, int oldVal, int newVal)
2520 {
2521  int switchGpioOff;
2522  int newOff, oldOff, realRange, cycles, i;
2523  int deferOff, deferRng;
2524 
2525  DBG(DBG_INTERNAL,
2526  "myGpioSetPwm %d from %d to %d", gpio, oldVal, newVal);
2527 
2528  switchGpioOff = 0;
2529 
2530  realRange = pwmRealRange[gpioInfo[gpio].freqIdx];
2531 
2532  cycles = pwmCycles [gpioInfo[gpio].freqIdx];
2533 
2534  newOff = (newVal * realRange)/gpioInfo[gpio].range;
2535  oldOff = (oldVal * realRange)/gpioInfo[gpio].range;
2536 
2537  deferOff = gpioInfo[gpio].deferOff;
2538  deferRng = gpioInfo[gpio].deferRng;
2539 
2540  if (gpioInfo[gpio].deferOff)
2541  {
2542  for (i=0; i<SUPERLEVEL; i+=deferRng)
2543  {
2544  myClearGpioOff(gpio, i+deferOff);
2545  }
2546  gpioInfo[gpio].deferOff = 0;
2547  }
2548 
2549  if (newOff != oldOff)
2550  {
2551  if (newOff && oldOff) /* PWM CHANGE */
2552  {
2553  if (newOff != realRange)
2554  {
2555  for (i=0; i<SUPERLEVEL; i+=realRange) mySetGpioOff(gpio, i+newOff);
2556  }
2557 
2558  if (newOff > oldOff)
2559  {
2560  for (i=0; i<SUPERLEVEL; i+=realRange)
2561  myClearGpioOff(gpio, i+oldOff);
2562  }
2563  else
2564  {
2565  gpioInfo[gpio].deferOff = oldOff;
2566  gpioInfo[gpio].deferRng = realRange;
2567  }
2568  }
2569  else if (newOff) /* PWM START */
2570  {
2571  if (newOff != realRange)
2572  {
2573  for (i=0; i<SUPERLEVEL; i+=realRange) mySetGpioOff(gpio, i+newOff);
2574  }
2575 
2576  /* schedule new gpio on */
2577 
2578  for (i=0; i<SUPERCYCLE; i+=cycles) mySetGpioOn(gpio, i);
2579  }
2580  else /* PWM STOP */
2581  {
2582  /* deschedule gpio on */
2583 
2584  for (i=0; i<SUPERCYCLE; i+=cycles)
2585  myClearGpioOn(gpio, i);
2586 
2587  for (i=0; i<SUPERLEVEL; i+=realRange)
2588  myClearGpioOff(gpio, i+oldOff);
2589 
2590  switchGpioOff = 1;
2591  }
2592 
2593  if (switchGpioOff)
2594  {
2595  *(gpioReg + GPCLR0) = (1<<gpio);
2596  *(gpioReg + GPCLR0) = (1<<gpio);
2597  }
2598  }
2599 }
2600 
2601 /* ----------------------------------------------------------------------- */
2602 
2603 static void myGpioSetServo(unsigned gpio, int oldVal, int newVal)
2604 {
2605  int newOff, oldOff, realRange, cycles, i;
2606  int deferOff, deferRng;
2607 
2608  DBG(DBG_INTERNAL,
2609  "myGpioSetServo %d from %d to %d", gpio, oldVal, newVal);
2610 
2611  realRange = pwmRealRange[clkCfg[gpioCfg.clockMicros].servoIdx];
2612  cycles = pwmCycles [clkCfg[gpioCfg.clockMicros].servoIdx];
2613 
2614  newOff = (newVal * realRange)/20000;
2615  oldOff = (oldVal * realRange)/20000;
2616 
2617  deferOff = gpioInfo[gpio].deferOff;
2618  deferRng = gpioInfo[gpio].deferRng;
2619 
2620  if (gpioInfo[gpio].deferOff)
2621  {
2622  for (i=0; i<SUPERLEVEL; i+=deferRng)
2623  {
2624  myClearGpioOff(gpio, i+deferOff);
2625  }
2626  gpioInfo[gpio].deferOff = 0;
2627  }
2628 
2629  if (newOff != oldOff)
2630  {
2631  if (newOff && oldOff) /* SERVO CHANGE */
2632  {
2633  for (i=0; i<SUPERLEVEL; i+=realRange)
2634  mySetGpioOff(gpio, i+newOff);
2635 
2636  if (newOff > oldOff)
2637  {
2638  for (i=0; i<SUPERLEVEL; i+=realRange)
2639  myClearGpioOff(gpio, i+oldOff);
2640  }
2641  else
2642  {
2643  gpioInfo[gpio].deferOff = oldOff;
2644  gpioInfo[gpio].deferRng = realRange;
2645  }
2646  }
2647  else if (newOff) /* SERVO START */
2648  {
2649  for (i=0; i<SUPERLEVEL; i+=realRange)
2650  mySetGpioOff(gpio, i+newOff);
2651 
2652  /* schedule new gpio on */
2653 
2654  for (i=0; i<SUPERCYCLE; i+=cycles) mySetGpioOn(gpio, i);
2655  }
2656  else /* SERVO STOP */
2657  {
2658  /* deschedule gpio on */
2659 
2660  for (i=0; i<SUPERCYCLE; i+=cycles)
2661  myClearGpioOn(gpio, i);
2662 
2663  /* if in pulse then delay for the last cycle to complete */
2664 
2666 
2667  /* deschedule gpio off */
2668 
2669  for (i=0; i<SUPERLEVEL; i+=realRange)
2670  myClearGpioOff(gpio, i+oldOff);
2671  }
2672  }
2673 }
2674 
2675 /* ======================================================================= */
2676 
2677 /*
2678 https://github.com/raspberrypi/firmware/wiki/Mailbox-property-interface
2679 */
2680 
2681 static int mbCreate(char *dev)
2682 {
2683  /* <0 error */
2684 
2685  unlink(dev);
2686 
2687  return mknod(dev, S_IFCHR|0600, makedev(MB_DEV_MAJOR, 0));
2688 }
2689 
2690 static int mbOpen(void)
2691 {
2692  /* <0 error */
2693 
2694  int fd;
2695 
2696  fd = open(MB_DEV1, 0);
2697 
2698  if (fd < 0)
2699  {
2700  mbCreate(MB_DEV2);
2701  fd = open(MB_DEV2, 0);
2702  }
2703  return fd;
2704 }
2705 
2706 static void mbClose(int fd)
2707 {
2708  close(fd);
2709 }
2710 
2711 static int mbProperty(int fd, void *buf)
2712 {
2713  return ioctl(fd, MB_IOCTL, buf);
2714 }
2715 
2716 static unsigned mbAllocateMemory(
2717  int fd, unsigned size, unsigned align, unsigned flags)
2718 {
2719  int i=1;
2720  unsigned p[32];
2721 
2722  p[i++] = MB_PROCESS_REQUEST;
2723  p[i++] = MB_ALLOCATE_MEMORY_TAG;
2724  p[i++] = 12;
2725  p[i++] = 12;
2726  p[i++] = size;
2727  p[i++] = align;
2728  p[i++] = flags;
2729  p[i++] = MB_END_TAG;
2730  p[0] = i*sizeof(*p);
2731 
2732  mbProperty(fd, p);
2733 
2734  return p[5];
2735 }
2736 
2737 static unsigned mbLockMemory(int fd, unsigned handle)
2738 {
2739  int i=1;
2740  unsigned p[32];
2741 
2742  p[i++] = MB_PROCESS_REQUEST;
2743  p[i++] = MB_LOCK_MEMORY_TAG;
2744  p[i++] = 4;
2745  p[i++] = 4;
2746  p[i++] = handle;
2747  p[i++] = MB_END_TAG;
2748  p[0] = i*sizeof(*p);
2749 
2750  mbProperty(fd, p);
2751 
2752  return p[5];
2753 }
2754 
2755 static unsigned mbUnlockMemory(int fd, unsigned handle)
2756 {
2757  int i=1;
2758  unsigned p[32];
2759 
2760  p[i++] = MB_PROCESS_REQUEST;
2761  p[i++] = MB_UNLOCK_MEMORY_TAG;
2762  p[i++] = 4;
2763  p[i++] = 4;
2764  p[i++] = handle;
2765  p[i++] = MB_END_TAG;
2766  p[0] = i*sizeof(*p);
2767 
2768  mbProperty(fd, p);
2769 
2770  return p[5];
2771 }
2772 
2773 static unsigned mbReleaseMemory(int fd, unsigned handle)
2774 {
2775  int i=1;
2776  unsigned p[32];
2777 
2778  p[i++] = MB_PROCESS_REQUEST;
2779  p[i++] = MB_RELEASE_MEMORY_TAG;
2780  p[i++] = 4;
2781  p[i++] = 4;
2782  p[i++] = handle;
2783  p[i++] = MB_END_TAG;
2784  p[0] = i*sizeof(*p);
2785 
2786  mbProperty(fd, p);
2787 
2788  return p[5];
2789 }
2790 
2791 static void *mbMapMem(unsigned base, unsigned size)
2792 {
2793  void *mem = MAP_FAILED;
2794 
2795  mem = mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fdMem, base);
2796 
2797  return mem;
2798 }
2799 
2800 static int mbUnmapMem(void *addr, unsigned size)
2801 {
2802  /* 0 okay, -1 fail */
2803  return munmap(addr, size);
2804 }
2805 
2806 static void mbDMAFree(DMAMem_t *DMAMemP)
2807 {
2808  if (DMAMemP->handle)
2809  {
2810  mbUnmapMem(DMAMemP->virtual_addr, DMAMemP->size);
2811  mbUnlockMemory(fdMbox, DMAMemP->handle);
2812  mbReleaseMemory(fdMbox, DMAMemP->handle);
2813  DMAMemP->handle = 0;
2814  }
2815 }
2816 
2817 static int mbDMAAlloc(DMAMem_t *DMAMemP, unsigned size, uint32_t pi_mem_flag)
2818 {
2819  DMAMemP->size = size;
2820 
2821  DMAMemP->handle =
2822  mbAllocateMemory(fdMbox, size, PAGE_SIZE, pi_mem_flag);
2823 
2824  if (DMAMemP->handle)
2825  {
2826  DMAMemP->bus_addr = mbLockMemory(fdMbox, DMAMemP->handle);
2827 
2828  DMAMemP->virtual_addr =
2829  mbMapMem(BUS_TO_PHYS(DMAMemP->bus_addr), size);
2830 
2831  return 1;
2832  }
2833  return 0;
2834 }
2835 
2836 
2837 /* ======================================================================= */
2838 
2840 {
2841  int page, slot;
2842 
2843  page = cbNum/CBS_PER_OPAGE;
2844  slot = cbNum%CBS_PER_OPAGE;
2845 
2846  return &dmaOVirt[page]->cb[slot];
2847 }
2848 
2849 
2850 /* ----------------------------------------------------------------------- */
2851 
2852 static uint32_t waveCbPOadr(int pos)
2853 {
2854  int page, slot;
2855 
2856  page = pos/CBS_PER_OPAGE;
2857  slot = pos%CBS_PER_OPAGE;
2858 
2859  return (uint32_t) &dmaOBus[page]->cb[slot];
2860 }
2861 
2862 /* ----------------------------------------------------------------------- */
2863 
2864 static void waveOOLPageSlot(int pos, int *page, int *slot)
2865 {
2866  *page = pos/OOL_PER_OPAGE;
2867  *slot = pos%OOL_PER_OPAGE;
2868 }
2869 
2870 
2871 /* ----------------------------------------------------------------------- */
2872 
2873 static void waveSetOOL(int pos, uint32_t OOL)
2874 {
2875  int page, slot;
2876 
2877  waveOOLPageSlot(pos, &page, &slot);
2878 
2879  dmaOVirt[page]->OOL[slot] = OOL;
2880 }
2881 
2882 /* ----------------------------------------------------------------------- */
2883 
2884 static uint32_t waveOOLPOadr(int pos)
2885 {
2886  int page, slot;
2887 
2888  waveOOLPageSlot(pos, &page, &slot);
2889 
2890  return (uint32_t) &dmaOBus[page]->OOL[slot];
2891 }
2892 
2893 
2894 /* ----------------------------------------------------------------------- */
2895 
2896 static void waveBitDelay
2897  (unsigned baud, unsigned bits, unsigned stops, unsigned *bitDelay)
2898 {
2899  unsigned fullBit, last, diff, t, i;
2900 
2901  /* scaled 1000X */
2902 
2903  fullBit = 1000000000 / baud;
2904  last = 0;
2905 
2906  for (i=0; i<=bits; i++)
2907  {
2908  t = (((i+1)*fullBit)+500)/1000;
2909  diff = t - last;
2910  last = t;
2911  bitDelay[i] = diff;
2912  }
2913 
2914  t = (((bits+1)*fullBit) + ((stops*fullBit)/2) + 500)/1000;
2915  diff = t - last;
2916  bitDelay[i] = diff;
2917 }
2918 
2919 static int waveDelayCBs(uint32_t delay)
2920 {
2921  uint32_t cbs;
2922 
2923  if (!delay) return 0;
2924  if (gpioCfg.DMAsecondaryChannel < DMA_LITE_FIRST) return 1;
2925  cbs = BPD * delay / DMA_LITE_MAX;
2926  if ((BPD * delay) % DMA_LITE_MAX) cbs++;
2927  return cbs;
2928 }
2929 
2930 /* ----------------------------------------------------------------------- */
2931 
2932 static void waveCBsOOLs(int *numCBs, int *numBOOLs, int *numTOOLs)
2933 {
2934  int numCB=0, numBOOL=0, numTOOL=0;
2935 
2936  unsigned i;
2937 
2938  unsigned numWaves;
2939 
2940  rawWave_t *waves;
2941 
2942  numWaves = wfc[wfcur];
2943  waves = wf [wfcur];
2944 
2945  /* delay cb at start of DMA */
2946 
2947  numCB++;
2948 
2949  for (i=0; i<numWaves; i++)
2950  {
2951  if (waves[i].gpioOn) {numCB++; numBOOL++;}
2952  if (waves[i].gpioOff) {numCB++; numBOOL++;}
2953  if (waves[i].flags & WAVE_FLAG_READ) {numCB++; numTOOL++;}
2954  if (waves[i].flags & WAVE_FLAG_TICK) {numCB++; numTOOL++;}
2955 
2956  numCB += waveDelayCBs(waves[i].usDelay);
2957  }
2958 
2959  *numCBs = numCB;
2960  *numBOOLs = numBOOL;
2961  *numTOOLs = numTOOL;
2962 }
2963 
2964 /* ----------------------------------------------------------------------- */
2965 
2966 static int wave2Cbs(unsigned wave_mode, int *CB, int *BOOL, int *TOOL)
2967 {
2968  int botCB=*CB, botOOL=*BOOL, topOOL=*TOOL;
2969 
2970  int status;
2971 
2972  rawCbs_t *p=NULL;
2973 
2974  unsigned i, repeatCB;
2975 
2976  unsigned numWaves;
2977 
2978  unsigned delayCBs, dcb;
2979 
2980  uint32_t delayLeft;
2981 
2982  rawWave_t * waves;
2983 
2984  numWaves = wfc[wfcur];
2985  waves = wf [wfcur];
2986 
2987  /* add delay cb at start of DMA */
2988 
2989  p = rawWaveCBAdr(botCB++);
2990 
2991  /* use the secondary clock */
2992 
2993  if (gpioCfg.clockPeriph != PI_CLOCK_PCM)
2994  {
2995  p->info = NORMAL_DMA | TIMED_DMA(2);
2996  p->dst = PCM_TIMER;
2997  }
2998  else
2999  {
3000  p->info = NORMAL_DMA | TIMED_DMA(5);
3001  p->dst = PWM_TIMER;
3002  }
3003 
3004  p->src = (uint32_t) (&dmaOBus[0]->periphData);
3005  p->length = BPD * 20 / PI_WF_MICROS; /* 20 micros delay */
3006  p->next = waveCbPOadr(botCB);
3007 
3008  repeatCB = botCB;
3009 
3010  for (i=0; i<numWaves; i++)
3011  {
3012  if (waves[i].gpioOn)
3013  {
3014  waveSetOOL(botOOL, waves[i].gpioOn);
3015 
3016  p = rawWaveCBAdr(botCB++);
3017 
3018  p->info = NORMAL_DMA;
3019  p->src = waveOOLPOadr(botOOL++);
3020  p->dst = ((GPIO_BASE + (GPSET0*4)) & 0x00ffffff) | PI_PERI_BUS;
3021  p->length = 4;
3022  p->next = waveCbPOadr(botCB);
3023  }
3024 
3025  if (waves[i].gpioOff)
3026  {
3027  waveSetOOL(botOOL, waves[i].gpioOff);
3028 
3029  p = rawWaveCBAdr(botCB++);
3030 
3031  p->info = NORMAL_DMA;
3032  p->src = waveOOLPOadr(botOOL++);
3033  p->dst = ((GPIO_BASE + (GPCLR0*4)) & 0x00ffffff) | PI_PERI_BUS;
3034  p->length = 4;
3035  p->next = waveCbPOadr(botCB);
3036  }
3037 
3038  if (waves[i].flags & WAVE_FLAG_READ)
3039  {
3040  p = rawWaveCBAdr(botCB++);
3041 
3042  p->info = NORMAL_DMA;
3043  p->src = ((GPIO_BASE + (GPLEV0*4)) & 0x00ffffff) | PI_PERI_BUS;
3044  p->dst = waveOOLPOadr(--topOOL);
3045  p->length = 4;
3046  p->next = waveCbPOadr(botCB);
3047  }
3048 
3049  if (waves[i].flags & WAVE_FLAG_TICK)
3050  {
3051  p = rawWaveCBAdr(botCB++);
3052 
3053  p->info = NORMAL_DMA;
3054  p->src = ((SYST_BASE + (SYST_CLO*4)) & 0x00ffffff) | PI_PERI_BUS;
3055  p->dst = waveOOLPOadr(--topOOL);
3056  p->length = 4;
3057  p->next = waveCbPOadr(botCB);
3058  }
3059 
3060  if (waves[i].usDelay)
3061  {
3062  delayLeft = waves[i].usDelay;
3063 
3064  delayCBs = waveDelayCBs(delayLeft);
3065 
3066  for (dcb=0; dcb<delayCBs; dcb++)
3067  {
3068  p = rawWaveCBAdr(botCB++);
3069 
3070  /* use the secondary clock */
3071 
3072  if (gpioCfg.clockPeriph != PI_CLOCK_PCM)
3073  {
3074  p->info = NORMAL_DMA | TIMED_DMA(2);
3075  p->dst = PCM_TIMER;
3076  }
3077  else
3078  {
3079  p->info = NORMAL_DMA | TIMED_DMA(5);
3080  p->dst = PWM_TIMER;
3081  }
3082 
3083  p->src = (uint32_t) (&dmaOBus[0]->periphData);
3084 
3085  p->length = BPD * delayLeft / PI_WF_MICROS;
3086 
3087  if ((gpioCfg.DMAsecondaryChannel >= DMA_LITE_FIRST) &&
3088  (p->length > DMA_LITE_MAX))
3089  {
3090  p->length = DMA_LITE_MAX;
3091  }
3092 
3093  delayLeft -= (p->length / BPD);
3094 
3095  p->next = waveCbPOadr(botCB);
3096  }
3097  }
3098  }
3099 
3100  if (p != NULL)
3101  {
3102  if (wave_mode == PI_WAVE_MODE_ONE_SHOT)
3103  p->next = 0;
3104  else p->next = waveCbPOadr(repeatCB);
3105  }
3106 
3107  status = botCB - *CB;
3108 
3109  *CB = botCB;
3110  *BOOL = botOOL;
3111  *TOOL = topOOL;
3112 
3113  return status;
3114 }
3115 
3116 /* ----------------------------------------------------------------------- */
3117 
3118 static void waveRxSerial(wfRx_t *w, int level, uint32_t tick)
3119 {
3120  int diffTicks, lastLevel;
3121  int newWritePos;
3122 
3123  level = level ^ w->s.invert;
3124 
3125  if (w->s.bit >= 0)
3126  {
3127  diffTicks = tick - w->s.startBitTick;
3128 
3129  if (level != PI_TIMEOUT)
3130  {
3131  w->s.level = level;
3132  lastLevel = !level;
3133  }
3134  else lastLevel = w->s.level;
3135 
3136  while ((w->s.bit <= w->s.dataBits) &&
3137  (diffTicks > (w->s.nextBitDiff/1000)))
3138  {
3139  if (w->s.bit)
3140  {
3141  if (lastLevel) w->s.data |= (1<<(w->s.bit-1));
3142  }
3143  else w->s.data = 0;
3144 
3145  ++(w->s.bit);
3146 
3147  w->s.nextBitDiff += w->s.fullBit;
3148  }
3149 
3150  if (w->s.bit > w->s.dataBits)
3151  {
3152  memcpy(w->s.buf + w->s.writePos, &w->s.data, w->s.bytes);
3153 
3154  /* don't let writePos catch readPos */
3155 
3156  newWritePos = (w->s.writePos + w->s.bytes) % (w->s.bufSize);
3157 
3158  if (newWritePos != w->s.readPos) w->s.writePos = newWritePos;
3159 
3160  if (level == 0)
3161  {
3162  gpioSetWatchdog(w->gpio, w->s.timeout);
3163  w->s.bit = 0;
3164  w->s.startBitTick = tick;
3165  w->s.nextBitDiff = w->s.halfBit;
3166  }
3167  else
3168  {
3169  w->s.bit = -1;
3170  gpioSetWatchdog(w->gpio, 0);
3171  }
3172  }
3173  }
3174  else
3175  {
3176  /* start bit if high->low */
3177 
3178  if (level == 0)
3179  {
3180  gpioSetWatchdog(w->gpio, w->s.timeout);
3181  w->s.level = 0;
3182  w->s.bit = 0;
3183  w->s.startBitTick = tick;
3184  w->s.nextBitDiff = w->s.halfBit;
3185  }
3186  }
3187 }
3188 
3189 
3190 /* ----------------------------------------------------------------------- */
3191 
3192 static void waveRxBit(int gpio, int level, uint32_t tick)
3193 {
3194  switch (wfRx[gpio].mode)
3195  {
3196  case PI_WFRX_SERIAL:
3197  waveRxSerial(&wfRx[gpio], level, tick);
3198  }
3199 }
3200 
3201 
3202 /* ----------------------------------------------------------------------- */
3203 
3204 int rawWaveAddGeneric(unsigned numIn1, rawWave_t *in1)
3205 {
3206  unsigned inPos1=0, inPos2=0, outPos=0, level = NUM_WAVE_OOL;
3207 
3208  unsigned cbs=0;
3209 
3210  unsigned numIn2, numOut;
3211 
3212  uint32_t tNow, tNext1, tNext2, tDelay;
3213 
3214  rawWave_t *in2, *out;
3215 
3216  numIn2 = wfc[wfcur];
3217  in2 = wf[wfcur];
3218 
3219  numOut = PI_WAVE_MAX_PULSES;
3220  out = wf[1-wfcur];
3221 
3222  tNow = 0;
3223 
3224  if (!numIn1) tNext1 = -1; else tNext1 = 0;
3225  if (!numIn2) tNext2 = -1; else tNext2 = 0;
3226 
3227  while (((inPos1<numIn1) || (inPos2<numIn2)) && (outPos<numOut))
3228  {
3229  if (tNext1 < tNext2)
3230  {
3231  /* pulse 1 due */
3232 
3233  if (tNow < tNext1)
3234  {
3235  /* extend previous delay */
3236  out[outPos-1].usDelay += (tNext1 - tNow);
3237  tNow = tNext1;
3238  }
3239 
3240  out[outPos].gpioOn = in1[inPos1].gpioOn;
3241  out[outPos].gpioOff = in1[inPos1].gpioOff;
3242  out[outPos].flags = in1[inPos1].flags;
3243 
3244  tNext1 = tNow + in1[inPos1].usDelay; ++inPos1;
3245  }
3246  else if (tNext2 < tNext1)
3247  {
3248  /* pulse 2 due */
3249 
3250  if (tNow < tNext2)
3251  {
3252  /* extend previous delay */
3253  out[outPos-1].usDelay += (tNext2 - tNow);
3254  tNow = tNext2;
3255  }
3256 
3257  out[outPos].gpioOn = in2[inPos2].gpioOn;
3258  out[outPos].gpioOff = in2[inPos2].gpioOff;
3259  out[outPos].flags = in2[inPos2].flags;
3260 
3261  tNext2 = tNow + in2[inPos2].usDelay; ++inPos2;
3262  }
3263  else
3264  {
3265  /* pulse 1 and 2 both due */
3266 
3267  if (tNow < tNext1)
3268  {
3269  /* extend previous delay */
3270  out[outPos-1].usDelay += (tNext1 - tNow);
3271  tNow = tNext1;
3272  }
3273 
3274  out[outPos].gpioOn = in1[inPos1].gpioOn | in2[inPos2].gpioOn;
3275  out[outPos].gpioOff = in1[inPos1].gpioOff | in2[inPos2].gpioOff;
3276  out[outPos].flags = in1[inPos1].flags | in2[inPos2].flags;
3277 
3278  tNext1 = tNow + in1[inPos1].usDelay; ++inPos1;
3279  tNext2 = tNow + in2[inPos2].usDelay; ++inPos2;
3280  }
3281 
3282  if (tNext1 <= tNext2) { tDelay = tNext1 - tNow; tNow = tNext1; }
3283  else { tDelay = tNext2 - tNow; tNow = tNext2; }
3284 
3285  out[outPos].usDelay = tDelay;
3286 
3287  cbs += waveDelayCBs(tDelay);
3288 
3289  if (out[outPos].gpioOn) cbs++; /* one cb if gpio on */
3290 
3291  if (out[outPos].gpioOff) cbs++; /* one cb if gpio off */
3292 
3293  if (out[outPos].flags & WAVE_FLAG_READ)
3294  {
3295  cbs++; /* one cb if read */
3296  --level;
3297  }
3298 
3299  if (out[outPos].flags & WAVE_FLAG_TICK)
3300  {
3301  cbs++; /* one cb if tick */
3302  --level;
3303  }
3304 
3305  outPos++;
3306 
3307  if (inPos1 >= numIn1) tNext1 = -1;
3308  if (inPos2 >= numIn2) tNext2 = -1;
3309 
3310  }
3311 
3312  if ((outPos < numOut) && (outPos < level))
3313  {
3314  wfStats.micros = tNow;
3315 
3316  if (tNow > wfStats.highMicros) wfStats.highMicros = tNow;
3317 
3318  wfStats.pulses = outPos;
3319 
3320  if (outPos > wfStats.highPulses) wfStats.highPulses = outPos;
3321 
3322  wfStats.cbs = cbs;
3323 
3324  if (cbs > wfStats.highCbs) wfStats.highCbs = cbs;
3325 
3326  wfc[1-wfcur] = outPos;
3327  wfcur = 1 - wfcur;
3328 
3329  return outPos;
3330  }
3331  else return PI_TOO_MANY_PULSES;
3332 }
3333 
3334 /* ======================================================================= */
3335 
3336 int i2cWriteQuick(unsigned handle, unsigned bit)
3337 {
3338  int status;
3339 
3340  DBG(DBG_USER, "handle=%d bit=%d", handle, bit);
3341 
3342  CHECK_INITED;
3343 
3344  if (handle >= PI_I2C_SLOTS)
3345  SOFT_ERROR(PI_BAD_HANDLE, "bad handle (%d)", handle);
3346 
3347  if (i2cInfo[handle].state != PI_I2C_OPENED)
3348  SOFT_ERROR(PI_BAD_HANDLE, "bad handle (%d)", handle);
3349 
3350  if ((i2cInfo[handle].funcs & PI_I2C_FUNC_SMBUS_QUICK) == 0)
3351  SOFT_ERROR(PI_BAD_SMBUS_CMD, "SMBUS command not supported by driver");
3352 
3353  if (bit > 1)
3354  SOFT_ERROR(PI_BAD_PARAM, "bad bit (%d)", bit);
3355 
3356  status = my_smbus_access(
3357  i2cInfo[handle].fd, bit, 0, PI_I2C_SMBUS_QUICK, NULL);
3358 
3359  if (status < 0)
3360  {
3361  DBG(DBG_USER, "error=%d (%m)", status);
3362  return PI_I2C_WRITE_FAILED;
3363  }
3364 
3365  return status;
3366 }
3367 
3368 int i2cReadByte(unsigned handle)
3369 {
3370  union my_smbus_data data;
3371  int status;
3372 
3373  DBG(DBG_USER, "handle=%d", handle);
3374 
3375  CHECK_INITED;
3376 
3377  if (handle >= PI_I2C_SLOTS)
3378  SOFT_ERROR(PI_BAD_HANDLE, "bad handle (%d)", handle);
3379 
3380  if (i2cInfo[handle].state != PI_I2C_OPENED)
3381  SOFT_ERROR(PI_BAD_HANDLE, "bad handle (%d)", handle);
3382 
3383  if ((i2cInfo[handle].funcs & PI_I2C_FUNC_SMBUS_READ_BYTE) == 0)
3384  SOFT_ERROR(PI_BAD_SMBUS_CMD, "SMBUS command not supported by driver");
3385 
3386  status = my_smbus_access(
3387  i2cInfo[handle].fd, PI_I2C_SMBUS_READ, 0, PI_I2C_SMBUS_BYTE, &data);
3388 
3389  if (status < 0)
3390  {
3391  DBG(DBG_USER, "error=%d (%m)", status);
3392  return PI_I2C_READ_FAILED;
3393  }
3394 
3395  return 0xFF & data.byte;
3396 }
3397 
3398 
3399 int i2cWriteByte(unsigned handle, unsigned bVal)
3400 {
3401  int status;
3402 
3403  DBG(DBG_USER, "handle=%d bVal=%d", handle, bVal);
3404 
3405  CHECK_INITED;
3406 
3407  if (handle >= PI_I2C_SLOTS)
3408  SOFT_ERROR(PI_BAD_HANDLE, "bad handle (%d)", handle);
3409 
3410  if (i2cInfo[handle].state != PI_I2C_OPENED)
3411  SOFT_ERROR(PI_BAD_HANDLE, "bad handle (%d)", handle);
3412 
3413  if ((i2cInfo[handle].funcs & PI_I2C_FUNC_SMBUS_WRITE_BYTE) == 0)
3414  SOFT_ERROR(PI_BAD_SMBUS_CMD, "SMBUS command not supported by driver");
3415 
3416  if (bVal > 0xFF)
3417  SOFT_ERROR(PI_BAD_PARAM, "bad bVal (%d)", bVal);
3418 
3419  status = my_smbus_access(
3420  i2cInfo[handle].fd,
3422  bVal,
3424  NULL);
3425 
3426  if (status < 0)
3427  {
3428  DBG(DBG_USER, "error=%d (%m)", status);
3429  return PI_I2C_WRITE_FAILED;
3430  }
3431 
3432  return status;
3433 }
3434 
3435 
3436 int i2cReadByteData(unsigned handle, unsigned reg)
3437 {
3438  union my_smbus_data data;
3439  int status;
3440 
3441  DBG(DBG_USER, "handle=%d reg=%d", handle, reg);
3442 
3443  CHECK_INITED;
3444 
3445  if (handle >= PI_I2C_SLOTS)
3446  SOFT_ERROR(PI_BAD_HANDLE, "bad handle (%d)", handle);
3447 
3448  if (i2cInfo[handle].state != PI_I2C_OPENED)
3449  SOFT_ERROR(PI_BAD_HANDLE, "bad handle (%d)", handle);
3450 
3451  if ((i2cInfo[handle].funcs & PI_I2C_FUNC_SMBUS_READ_BYTE_DATA) == 0)
3452  SOFT_ERROR(PI_BAD_SMBUS_CMD, "SMBUS command not supported by driver");
3453 
3454  if (reg > 0xFF)
3455  SOFT_ERROR(PI_BAD_PARAM, "bad reg (%d)", reg);
3456 
3457  status = my_smbus_access(i2cInfo[handle].fd,
3459 
3460  if (status < 0)
3461  {
3462  DBG(DBG_USER, "error=%d (%m)", status);
3463  return PI_I2C_READ_FAILED;
3464  }
3465 
3466  return 0xFF & data.byte;
3467 }
3468 
3469 
3470 int i2cWriteByteData(unsigned handle, unsigned reg, unsigned bVal)
3471 {
3472  union my_smbus_data data;
3473 
3474  int status;
3475 
3476  DBG(DBG_USER, "handle=%d reg=%d bVal=%d", handle, reg, bVal);
3477 
3478  CHECK_INITED;
3479 
3480  if (handle >= PI_I2C_SLOTS)
3481  SOFT_ERROR(PI_BAD_HANDLE, "bad handle (%d)", handle);
3482 
3483  if (i2cInfo[handle].state != PI_I2C_OPENED)
3484  SOFT_ERROR(PI_BAD_HANDLE, "bad handle (%d)", handle);
3485 
3486  if ((i2cInfo[handle].funcs & PI_I2C_FUNC_SMBUS_WRITE_BYTE_DATA) == 0)
3487  SOFT_ERROR(PI_BAD_SMBUS_CMD, "SMBUS command not supported by driver");
3488 
3489  if (reg > 0xFF)
3490  SOFT_ERROR(PI_BAD_PARAM, "bad reg (%d)", reg);
3491 
3492  if (bVal > 0xFF)
3493  SOFT_ERROR(PI_BAD_PARAM, "bad bVal (%d)", bVal);
3494 
3495  data.byte = bVal;
3496 
3497  status = my_smbus_access(
3498  i2cInfo[handle].fd,
3500  reg,
3502  &data);
3503 
3504  if (status < 0)
3505  {
3506  DBG(DBG_USER, "error=%d (%m)", status);
3507  return PI_I2C_WRITE_FAILED;
3508  }
3509 
3510  return status;
3511 }
3512 
3513 
3514 int i2cReadWordData(unsigned handle, unsigned reg)
3515 {
3516  union my_smbus_data data;
3517  int status;
3518 
3519  DBG(DBG_USER, "handle=%d reg=%d", handle, reg);
3520 
3521  CHECK_INITED;
3522 
3523  if (handle >= PI_I2C_SLOTS)
3524  SOFT_ERROR(PI_BAD_HANDLE, "bad handle (%d)", handle);
3525 
3526  if (i2cInfo[handle].state != PI_I2C_OPENED)
3527  SOFT_ERROR(PI_BAD_HANDLE, "bad handle (%d)", handle);
3528 
3529  if ((i2cInfo[handle].funcs & PI_I2C_FUNC_SMBUS_READ_WORD_DATA) == 0)
3530  SOFT_ERROR(PI_BAD_SMBUS_CMD, "SMBUS command not supported by driver");
3531 
3532  if (reg > 0xFF)
3533  SOFT_ERROR(PI_BAD_PARAM, "bad reg (%d)", reg);
3534 
3535  status = (my_smbus_access(
3536  i2cInfo[handle].fd,
3538  reg,
3540  &data));
3541 
3542  if (status < 0)
3543  {
3544  DBG(DBG_USER, "error=%d (%m)", status);
3545  return PI_I2C_READ_FAILED;
3546  }
3547 
3548  return 0xFFFF & data.word;
3549 }
3550 
3551 
3552 int i2cWriteWordData(unsigned handle, unsigned reg, unsigned wVal)
3553 {
3554  union my_smbus_data data;
3555 
3556  int status;
3557 
3558  DBG(DBG_USER, "handle=%d reg=%d wVal=%d", handle, reg, wVal);
3559 
3560  CHECK_INITED;
3561 
3562  if (handle >= PI_I2C_SLOTS)
3563  SOFT_ERROR(PI_BAD_HANDLE, "bad handle (%d)", handle);
3564 
3565  if (i2cInfo[handle].state != PI_I2C_OPENED)
3566  SOFT_ERROR(PI_BAD_HANDLE, "bad handle (%d)", handle);
3567 
3568  if ((i2cInfo[handle].funcs & PI_I2C_FUNC_SMBUS_WRITE_WORD_DATA) == 0)
3569  SOFT_ERROR(PI_BAD_SMBUS_CMD, "SMBUS command not supported by driver");
3570 
3571  if (reg > 0xFF)
3572  SOFT_ERROR(PI_BAD_PARAM, "bad reg (%d)", reg);
3573 
3574  if (wVal > 0xFFFF)
3575  SOFT_ERROR(PI_BAD_PARAM, "bad wVal (%d)", wVal);
3576 
3577  data.word = wVal;
3578 
3579  status = my_smbus_access(
3580  i2cInfo[handle].fd,
3582  reg,
3584  &data);
3585 
3586  if (status < 0)
3587  {
3588  DBG(DBG_USER, "error=%d (%m)", status);
3589  return PI_I2C_WRITE_FAILED;
3590  }
3591 
3592  return status;
3593 }
3594 
3595 
3596 int i2cProcessCall(unsigned handle, unsigned reg, unsigned wVal)
3597 {
3598  union my_smbus_data data;
3599  int status;
3600 
3601  DBG(DBG_USER, "handle=%d reg=%d wVal=%d", handle, reg, wVal);
3602 
3603  CHECK_INITED;
3604 
3605  if (handle >= PI_I2C_SLOTS)
3606  SOFT_ERROR(PI_BAD_HANDLE, "bad handle (%d)", handle);
3607 
3608  if (i2cInfo[handle].state != PI_I2C_OPENED)
3609  SOFT_ERROR(PI_BAD_HANDLE, "bad handle (%d)", handle);
3610 
3611  if ((i2cInfo[handle].funcs & PI_I2C_FUNC_SMBUS_PROC_CALL) == 0)
3612  SOFT_ERROR(PI_BAD_SMBUS_CMD, "SMBUS command not supported by driver");
3613 
3614  if (reg > 0xFF)
3615  SOFT_ERROR(PI_BAD_PARAM, "bad reg (%d)", reg);
3616 
3617  if (wVal > 0xFFFF)
3618  SOFT_ERROR(PI_BAD_PARAM, "bad wVal (%d)", wVal);
3619 
3620  data.word = wVal;
3621 
3622  status = (my_smbus_access(
3623  i2cInfo[handle].fd,
3626  &data));
3627 
3628  if (status < 0)
3629  {
3630  DBG(DBG_USER, "error=%d (%m)", status);
3631  return PI_I2C_READ_FAILED;
3632  }
3633 
3634  return 0xFFFF & data.word;
3635 }
3636 
3637 
3638 int i2cReadBlockData(unsigned handle, unsigned reg, char *buf)
3639 {
3640  union my_smbus_data data;
3641 
3642  int i, status;
3643 
3644  DBG(DBG_USER, "handle=%d reg=%d buf=%08X", handle, reg, (unsigned)buf);
3645 
3646  CHECK_INITED;
3647 
3648  if (handle >= PI_I2C_SLOTS)
3649  SOFT_ERROR(PI_BAD_HANDLE, "bad handle (%d)", handle);
3650 
3651  if (i2cInfo[handle].state != PI_I2C_OPENED)
3652  SOFT_ERROR(PI_BAD_HANDLE, "bad handle (%d)", handle);
3653 
3654  if ((i2cInfo[handle].funcs & PI_I2C_FUNC_SMBUS_READ_BLOCK_DATA) == 0)
3655  SOFT_ERROR(PI_BAD_SMBUS_CMD, "SMBUS command not supported by driver");
3656 
3657  if (reg > 0xFF)
3658  SOFT_ERROR(PI_BAD_PARAM, "bad reg (%d)", reg);
3659 
3660  status = (my_smbus_access(
3661  i2cInfo[handle].fd,
3663  reg,
3665  &data));
3666 
3667  if (status < 0)
3668  {
3669  DBG(DBG_USER, "error=%d (%m)", status);
3670  return PI_I2C_READ_FAILED;
3671  }
3672  else
3673  {
3674  if (data.block[0] <= PI_I2C_SMBUS_BLOCK_MAX)
3675  {
3676  for (i=0; i<data.block[0]; i++) buf[i] = data.block[i+1];
3677  return data.block[0];
3678  }
3679  else return PI_I2C_READ_FAILED;
3680  }
3681 }
3682 
3683 
3685  unsigned handle, unsigned reg, char *buf, unsigned count)
3686 {
3687  union my_smbus_data data;
3688 
3689  int i, status;
3690 
3691  DBG(DBG_USER, "handle=%d reg=%d count=%d [%s]",
3692  handle, reg, count, myBuf2Str(count, buf));
3693 
3694  CHECK_INITED;
3695 
3696  if (handle >= PI_I2C_SLOTS)
3697  SOFT_ERROR(PI_BAD_HANDLE, "bad handle (%d)", handle);
3698 
3699  if (i2cInfo[handle].state != PI_I2C_OPENED)
3700  SOFT_ERROR(PI_BAD_HANDLE, "bad handle (%d)", handle);
3701 
3702  if ((i2cInfo[handle].funcs & PI_I2C_FUNC_SMBUS_WRITE_BLOCK_DATA) == 0)
3703  SOFT_ERROR(PI_BAD_SMBUS_CMD, "SMBUS command not supported by driver");
3704 
3705  if (reg > 0xFF)
3706  SOFT_ERROR(PI_BAD_PARAM, "bad reg (%d)", reg);
3707 
3708  if ((count < 1) || (count > 32))
3709  SOFT_ERROR(PI_BAD_PARAM, "bad count (%d)", count);
3710 
3711  for (i=1; i<=count; i++) data.block[i] = buf[i-1];
3712  data.block[0] = count;
3713 
3714  status = my_smbus_access(
3715  i2cInfo[handle].fd,
3717  reg,
3719  &data);
3720 
3721  if (status < 0)
3722  {
3723  DBG(DBG_USER, "error=%d (%m)", status);
3724  return PI_I2C_WRITE_FAILED;
3725  }
3726 
3727  return status;
3728 }
3729 
3730 
3732  unsigned handle, unsigned reg, char *buf, unsigned count)
3733 {
3734  union my_smbus_data data;
3735 
3736  int i, status;
3737 
3738  DBG(DBG_USER, "handle=%d reg=%d count=%d [%s]",
3739  handle, reg, count, myBuf2Str(count, buf));
3740 
3741  CHECK_INITED;
3742 
3743  if (handle >= PI_I2C_SLOTS)
3744  SOFT_ERROR(PI_BAD_HANDLE, "bad handle (%d)", handle);
3745 
3746  if (i2cInfo[handle].state != PI_I2C_OPENED)
3747  SOFT_ERROR(PI_BAD_HANDLE, "bad handle (%d)", handle);
3748 
3749  if ((i2cInfo[handle].funcs & PI_I2C_FUNC_SMBUS_PROC_CALL) == 0)
3750  SOFT_ERROR(PI_BAD_SMBUS_CMD, "SMBUS command not supported by driver");
3751 
3752  if (reg > 0xFF)
3753  SOFT_ERROR(PI_BAD_PARAM, "bad reg (%d)", reg);
3754 
3755  if ((count < 1) || (count > 32))
3756  SOFT_ERROR(PI_BAD_PARAM, "bad count (%d)", count);
3757 
3758  for (i=1; i<=count; i++) data.block[i] = buf[i-1];
3759  data.block[0] = count;
3760 
3761  status = (my_smbus_access(
3762  i2cInfo[handle].fd, PI_I2C_SMBUS_WRITE, reg,
3764 
3765  if (status < 0)
3766  {
3767  DBG(DBG_USER, "error=%d (%m)", status);
3768  return PI_I2C_READ_FAILED;
3769  }
3770  else
3771  {
3772  if (data.block[0] <= PI_I2C_SMBUS_BLOCK_MAX)
3773  {
3774  for (i=0; i<data.block[0]; i++) buf[i] = data.block[i+1];
3775  return data.block[0];
3776  }
3777  else return PI_I2C_READ_FAILED;
3778  }
3779 }
3780 
3781 
3783  unsigned handle, unsigned reg, char *buf, unsigned count)
3784 {
3785  union my_smbus_data data;
3786 
3787  int i, status;
3788  uint32_t size;
3789 
3790  DBG(DBG_USER, "handle=%d reg=%d count=%d buf=%08X",
3791  handle, reg, count, (unsigned)buf);
3792 
3793  CHECK_INITED;
3794 
3795  if (handle >= PI_I2C_SLOTS)
3796  SOFT_ERROR(PI_BAD_HANDLE, "bad handle (%d)", handle);
3797 
3798  if (i2cInfo[handle].state != PI_I2C_OPENED)
3799  SOFT_ERROR(PI_BAD_HANDLE, "bad handle (%d)", handle);
3800 
3801  if ((i2cInfo[handle].funcs & PI_I2C_FUNC_SMBUS_READ_I2C_BLOCK) == 0)
3802  SOFT_ERROR(PI_BAD_SMBUS_CMD, "SMBUS command not supported by driver");
3803 
3804  if (reg > 0xFF)
3805  SOFT_ERROR(PI_BAD_PARAM, "bad reg (%d)", reg);
3806 
3807  if ((count < 1) || (count > 32))
3808  SOFT_ERROR(PI_BAD_PARAM, "bad count (%d)", count);
3809 
3810  if (count == 32)
3812  else
3814 
3815  data.block[0] = count;
3816 
3817  status = (my_smbus_access(
3818  i2cInfo[handle].fd, PI_I2C_SMBUS_READ, reg, size, &data));
3819 
3820  if (status < 0)
3821  {
3822  DBG(DBG_USER, "error=%d (%m)", status);
3823  return PI_I2C_READ_FAILED;
3824  }
3825  else
3826  {
3827  if (data.block[0] <= PI_I2C_SMBUS_I2C_BLOCK_MAX)
3828  {
3829  for (i=0; i<data.block[0]; i++) buf[i] = data.block[i+1];
3830  return data.block[0];
3831  }
3832  else return PI_I2C_READ_FAILED;
3833  }
3834 }
3835 
3836 
3838  unsigned handle, unsigned reg, char *buf, unsigned count)
3839 {
3840  union my_smbus_data data;
3841 
3842  int i, status;
3843 
3844  DBG(DBG_USER, "handle=%d reg=%d count=%d [%s]",
3845  handle, reg, count, myBuf2Str(count, buf));
3846 
3847  CHECK_INITED;
3848 
3849  if (handle >= PI_I2C_SLOTS)
3850  SOFT_ERROR(PI_BAD_HANDLE, "bad handle (%d)", handle);
3851 
3852  if (i2cInfo[handle].state != PI_I2C_OPENED)
3853  SOFT_ERROR(PI_BAD_HANDLE, "bad handle (%d)", handle);
3854 
3855  if ((i2cInfo[handle].funcs & PI_I2C_FUNC_SMBUS_WRITE_I2C_BLOCK) == 0)
3856  SOFT_ERROR(PI_BAD_SMBUS_CMD, "SMBUS command not supported by driver");
3857 
3858  if (reg > 0xFF)
3859  SOFT_ERROR(PI_BAD_PARAM, "bad reg (%d)", reg);
3860 
3861  if ((count < 1) || (count > 32))
3862  SOFT_ERROR(PI_BAD_PARAM, "bad count (%d)", count);
3863 
3864  for (i=1; i<=count; i++) data.block[i] = buf[i-1];
3865 
3866  data.block[0] = count;
3867 
3868  status = my_smbus_access(
3869  i2cInfo[handle].fd,
3871  reg,
3873  &data);
3874 
3875  if (status < 0)
3876  {
3877  DBG(DBG_USER, "error=%d (%m)", status);
3878  return PI_I2C_WRITE_FAILED;
3879  }
3880 
3881  return status;
3882 }
3883 
3884 int i2cWriteDevice(unsigned handle, char *buf, unsigned count)
3885 {
3886  int bytes;
3887 
3888  DBG(DBG_USER, "handle=%d count=%d [%s]",
3889  handle, count, myBuf2Str(count, buf));
3890 
3891  CHECK_INITED;
3892 
3893  if (handle >= PI_I2C_SLOTS)
3894  SOFT_ERROR(PI_BAD_HANDLE, "bad handle (%d)", handle);
3895 
3896  if (i2cInfo[handle].state != PI_I2C_OPENED)
3897  SOFT_ERROR(PI_BAD_HANDLE, "bad handle (%d)", handle);
3898 
3899  if ((count < 1) || (count > PI_MAX_I2C_DEVICE_COUNT))
3900  SOFT_ERROR(PI_BAD_PARAM, "bad count (%d)", count);
3901 
3902  bytes = write(i2cInfo[handle].fd, buf, count);
3903 
3904  if (bytes != count)
3905  {
3906  DBG(DBG_USER, "error=%d (%m)", bytes);
3907  return PI_I2C_WRITE_FAILED;
3908  }
3909 
3910  return 0;
3911 }
3912 
3913 int i2cReadDevice(unsigned handle, char *buf, unsigned count)
3914 {
3915  int bytes;
3916 
3917  DBG(DBG_USER, "handle=%d count=%d buf=%08X",
3918  handle, count, (unsigned)buf);
3919 
3920  CHECK_INITED;
3921 
3922  if (handle >= PI_I2C_SLOTS)
3923  SOFT_ERROR(PI_BAD_HANDLE, "bad handle (%d)", handle);
3924 
3925  if (i2cInfo[handle].state != PI_I2C_OPENED)
3926  SOFT_ERROR(PI_BAD_HANDLE, "bad handle (%d)", handle);
3927 
3928  if ((count < 1) || (count > PI_MAX_I2C_DEVICE_COUNT))
3929  SOFT_ERROR(PI_BAD_PARAM, "bad count (%d)", count);
3930 
3931  bytes = read(i2cInfo[handle].fd, buf, count);
3932 
3933  if (bytes != count)
3934  {
3935  DBG(DBG_USER, "error=%d (%m)", bytes);
3936  return PI_I2C_READ_FAILED;
3937  }
3938 
3939  return bytes;
3940 }
3941 
3942 int i2cOpen(unsigned i2cBus, unsigned i2cAddr, unsigned i2cFlags)
3943 {
3944  static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
3945  char dev[32];
3946  int i, slot, fd;
3947  uint32_t funcs;
3948 
3949  DBG(DBG_USER, "i2cBus=%d i2cAddr=%d flags=0x%X",
3950  i2cBus, i2cAddr, i2cFlags);
3951 
3952  CHECK_INITED;
3953 
3954  if (i2cAddr > PI_MAX_I2C_ADDR)
3955  SOFT_ERROR(PI_BAD_I2C_ADDR, "bad I2C address (%d)", i2cAddr);
3956 
3957  if (i2cFlags)
3958  SOFT_ERROR(PI_BAD_FLAGS, "bad flags (0x%X)", i2cFlags);
3959 
3960  slot = -1;
3961 
3962  pthread_mutex_lock(&mutex);
3963 
3964  for (i=0; i<PI_I2C_SLOTS; i++)
3965  {
3966  if (i2cInfo[i].state == PI_I2C_CLOSED)
3967  {
3968  slot = i;
3969  i2cInfo[slot].state = PI_I2C_RESERVED;
3970  break;
3971  }
3972  }
3973 
3974  pthread_mutex_unlock(&mutex);
3975 
3976  if (slot < 0) SOFT_ERROR(PI_NO_HANDLE, "no I2C handles");
3977 
3978  sprintf(dev, "/dev/i2c-%d", i2cBus);
3979 
3980  if ((fd = open(dev, O_RDWR)) < 0)
3981  {
3982  /* try a modprobe */
3983 
3984  if (system("/sbin/modprobe i2c_dev") == -1) { /* ignore errors */}
3985  if (system("/sbin/modprobe i2c_bcm2835") == -1) { /* ignore errors */}
3986 
3987  myGpioDelay(100000);
3988 
3989  if ((fd = open(dev, O_RDWR)) < 0)
3990  {
3991  i2cInfo[slot].state = PI_I2C_CLOSED;
3992  return PI_BAD_I2C_BUS;
3993  }
3994  }
3995 
3996  if (ioctl(fd, PI_I2C_SLAVE, i2cAddr) < 0)
3997  {
3998  close(fd);
3999  i2cInfo[slot].state = PI_I2C_CLOSED;
4000  return PI_I2C_OPEN_FAILED;
4001  }
4002 
4003  if (ioctl(fd, PI_I2C_FUNCS, &funcs) < 0)
4004  {
4005  funcs = -1; /* assume all smbus commands allowed */
4006  }
4007 
4008  i2cInfo[slot].fd = fd;
4009  i2cInfo[slot].addr = i2cAddr;
4010  i2cInfo[slot].flags = i2cFlags;
4011  i2cInfo[slot].funcs = funcs;
4012  i2cInfo[i].state = PI_I2C_OPENED;
4013 
4014  return slot;
4015 }
4016 
4017 int i2cClose(unsigned handle)
4018 {
4019  DBG(DBG_USER, "handle=%d", handle);
4020 
4021  CHECK_INITED;
4022 
4023  if (handle >= PI_I2C_SLOTS)
4024  SOFT_ERROR(PI_BAD_HANDLE, "bad handle (%d)", handle);
4025 
4026  if (i2cInfo[handle].state != PI_I2C_OPENED)
4027  SOFT_ERROR(PI_BAD_HANDLE, "bad handle (%d)", handle);
4028 
4029  if (i2cInfo[handle].fd >= 0) close(i2cInfo[handle].fd);
4030 
4031  i2cInfo[handle].fd = -1;
4032  i2cInfo[handle].state = PI_I2C_CLOSED;
4033 
4034  return 0;
4035 }
4036 
4037 void i2cSwitchCombined(int setting)
4038 {
4039  int fd;
4040 
4041  DBG(DBG_USER, "setting=%d", setting);
4042 
4043  fd = open(PI_I2C_COMBINED, O_WRONLY);
4044 
4045  if (fd >= 0)
4046  {
4047  if (setting)
4048  {
4049  if (write(fd, "1\n", 2) == -1) { /* ignore errors */ }
4050  }
4051  else
4052  {
4053  if (write(fd, "0\n", 2) == -1) { /* ignore errors */ }
4054  }
4055 
4056  close(fd);
4057  }
4058 }
4059 
4060 int i2cSegments(unsigned handle, pi_i2c_msg_t *segs, unsigned numSegs)
4061 {
4062  int retval;
4064 
4065  DBG(DBG_USER, "handle=%d", handle);
4066 
4067  CHECK_INITED;
4068 
4069  if (handle >= PI_I2C_SLOTS)
4070  SOFT_ERROR(PI_BAD_HANDLE, "bad handle (%d)", handle);
4071 
4072  if (i2cInfo[handle].state != PI_I2C_OPENED)
4073  SOFT_ERROR(PI_BAD_HANDLE, "bad handle (%d)", handle);
4074 
4075  if (segs == NULL)
4076  SOFT_ERROR(PI_BAD_POINTER, "null segments");
4077 
4078  if (numSegs > PI_I2C_RDRW_IOCTL_MAX_MSGS)
4079  SOFT_ERROR(PI_TOO_MANY_SEGS, "too many segments (%d)", numSegs);
4080 
4081  rdwr.msgs = segs;
4082  rdwr.nmsgs = numSegs;
4083 
4084  retval = ioctl(i2cInfo[handle].fd, PI_I2C_RDWR, &rdwr);
4085 
4086  if (retval >= 0) return retval;
4087  else return PI_BAD_I2C_SEG;
4088 }
4089 
4091  unsigned handle,
4092  char *inBuf, unsigned inLen, char *outBuf, unsigned outLen)
4093 {
4094  int numSegs, inPos, outPos, status, bytes, flags, addr;
4095  int esc, setesc;
4097 
4098  DBG(DBG_USER, "handle=%d inBuf=%s outBuf=%08X len=%d",
4099  handle, myBuf2Str(inLen, (char *)inBuf), (int)outBuf, outLen);
4100 
4101  CHECK_INITED;
4102 
4103  if (handle >= PI_I2C_SLOTS)
4104  SOFT_ERROR(PI_BAD_HANDLE, "bad handle (%d)", handle);
4105 
4106  if (i2cInfo[handle].state != PI_I2C_OPENED)
4107  SOFT_ERROR(PI_BAD_HANDLE, "bad handle (%d)", handle);
4108 
4109  if (!inBuf || !inLen)
4110  SOFT_ERROR(PI_BAD_POINTER, "input buffer can't be NULL");
4111 
4112  if (!outBuf && outLen)
4113  SOFT_ERROR(PI_BAD_POINTER, "output buffer can't be NULL");
4114 
4115  numSegs = 0;
4116 
4117  inPos = 0;
4118  outPos = 0;
4119  status = 0;
4120 
4121  addr = i2cInfo[handle].addr;
4122  flags = 0;
4123  esc = 0;
4124  setesc = 0;
4125 
4126  while (!status && (inPos < inLen))
4127  {
4128  DBG(DBG_INTERNAL, "status=%d inpos=%d inlen=%d cmd=%d addr=%d flags=%x",
4129  status, inPos, inLen, inBuf[inPos], addr, flags);
4130 
4131  switch (inBuf[inPos++])
4132  {
4133  case PI_I2C_END:
4134  status = 1;
4135  break;
4136 
4137  case PI_I2C_COMBINED_ON:
4138  /* Run prior transactions before setting combined flag */
4139  if (numSegs)
4140  {
4141  status = i2cSegments(handle, segs, numSegs);
4142  if (status >= 0) status = 0; /* continue */
4143  numSegs = 0;
4144  }
4145  i2cSwitchCombined(1);
4146  break;
4147 
4148  case PI_I2C_COMBINED_OFF:
4149  /* Run prior transactions before clearing combined flag */
4150  if (numSegs)
4151  {
4152  status = i2cSegments(handle, segs, numSegs);
4153  if (status >= 0) status = 0; /* continue */
4154  numSegs = 0;
4155  }
4156  i2cSwitchCombined(0);
4157  break;
4158 
4159  case PI_I2C_ADDR:
4160  addr = myI2CGetPar(inBuf, &inPos, inLen, &esc);
4161  if (addr < 0) status = PI_BAD_I2C_CMD;
4162  break;
4163 
4164  case PI_I2C_FLAGS:
4165  /* cheat to force two byte flags */
4166  esc = 1;
4167  flags = myI2CGetPar(inBuf, &inPos, inLen, &esc);
4168  if (flags < 0) status = PI_BAD_I2C_CMD;
4169  break;
4170 
4171  case PI_I2C_ESC:
4172  setesc = 1;
4173  break;
4174 
4175  case PI_I2C_READ:
4176 
4177  bytes = myI2CGetPar(inBuf, &inPos, inLen, &esc);
4178 
4179  if (bytes >= 0)
4180  {
4181  if ((bytes + outPos) < outLen)
4182  {
4183  segs[numSegs].addr = addr;
4184  segs[numSegs].flags = (flags|1);
4185  segs[numSegs].len = bytes;
4186  segs[numSegs].buf = (uint8_t *)(outBuf + outPos);
4187  outPos += bytes;
4188  numSegs++;
4189  if (numSegs >= PI_I2C_RDRW_IOCTL_MAX_MSGS)
4190  {
4191  status = i2cSegments(handle, segs, numSegs);
4192  if (status >= 0) status = 0; /* continue */
4193  numSegs = 0;
4194  }
4195  }
4196  else status = PI_BAD_I2C_RLEN;
4197  }
4198  else status = PI_BAD_I2C_RLEN;
4199  break;
4200 
4201  case PI_I2C_WRITE:
4202 
4203  bytes = myI2CGetPar(inBuf, &inPos, inLen, &esc);
4204 
4205  if (bytes >= 0)
4206  {
4207  if ((bytes + inPos) < inLen)
4208  {
4209  segs[numSegs].addr = addr;
4210  segs[numSegs].flags = (flags&0xfffe);
4211  segs[numSegs].len = bytes;
4212  segs[numSegs].buf = (uint8_t *)(inBuf + inPos);
4213  inPos += bytes;
4214  numSegs++;
4215  if (numSegs >= PI_I2C_RDRW_IOCTL_MAX_MSGS)
4216  {
4217  status = i2cSegments(handle, segs, numSegs);
4218  if (status >= 0) status = 0; /* continue */
4219  numSegs = 0;
4220  }
4221  }
4222  else status = PI_BAD_I2C_WLEN;
4223  }
4224  else status = PI_BAD_I2C_WLEN;
4225  break;
4226 
4227  default:
4228  status = PI_BAD_I2C_CMD;
4229  }
4230 
4231  if (setesc) esc = 1; else esc = 0;
4232 
4233  setesc = 0;
4234  }
4235 
4236  if (status >= 0)
4237  {
4238  if (numSegs) status = i2cSegments(handle, segs, numSegs);
4239  }
4240 
4241  if (status >= 0) status = outPos;
4242 
4243  return status;
4244 }
4245 
4246 /* ======================================================================= */
4247 
4248 /*SPI */
4249 
4250 static uint32_t _spiTXBits(char *buf, int pos, int bitlen, int msbf)
4251 {
4252  uint32_t bits=0;
4253 
4254  if (buf)
4255  {
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);
4259 
4260  if (msbf) bits <<= (32-bitlen);
4261  }
4262 
4263  return bits;
4264 }
4265 
4266 static void _spiRXBits(
4267  char *buf, int pos, int bitlen, int msbf, uint32_t bits)
4268 {
4269  if (buf)
4270  {
4271  if (!msbf) bits >>= (32-bitlen);
4272 
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;
4276  }
4277 }
4278 
4279 static void spiACS(int channel, int on)
4280 {
4281  int gpio;
4282 
4283  switch (channel)
4284  {
4285  case 0: gpio = PI_ASPI_CE0; break;
4286  case 1: gpio = PI_ASPI_CE1; break;
4287  default: gpio = PI_ASPI_CE2; break;
4288  }
4289  myGpioWrite(gpio, on);
4290 }
4291 
4292 static void spiGoA(
4293  unsigned speed, /* bits per second */
4294  uint32_t flags, /* flags */
4295  char *txBuf, /* tx buffer */
4296  char *rxBuf, /* rx buffer */
4297  unsigned count) /* number of bytes */
4298 {
4299  int cs;
4300  char bit_ir[4] = {1, 0, 0, 1}; /* read on rising edge */
4301  char bit_or[4] = {0, 1, 1, 0}; /* write on rising edge */
4302  char bit_ic[4] = {0, 0, 1, 1}; /* invert clock */
4303 
4304  int mode, bitlen, txmsbf, rxmsbf, channel;
4305  unsigned txCnt=0;
4306  unsigned rxCnt=0;
4307  uint32_t spiDefaults;
4308  uint32_t statusReg;
4309  int txFull, rxEmpty;
4310 
4311  channel = PI_SPI_FLAGS_GET_CHANNEL(flags);
4312  mode = PI_SPI_FLAGS_GET_MODE (flags);
4313 
4314  bitlen = PI_SPI_FLAGS_GET_BITLEN (flags);
4315 
4316  if (!bitlen) bitlen = 8;
4317 
4318  /* correct count for word size */
4319 
4320  if (bitlen > 8) count /= 2;
4321  if (bitlen > 16) count /= 2;
4322 
4323  txmsbf = !PI_SPI_FLAGS_GET_TX_LSB (flags);
4324  rxmsbf = !PI_SPI_FLAGS_GET_RX_LSB (flags);
4325 
4326  cs = PI_SPI_FLAGS_GET_CSPOLS(flags) & (1<<channel);
4327 
4328  spiDefaults = AUXSPI_CNTL0_SPEED(125000000/speed) |
4329  AUXSPI_CNTL0_IN_RISING(bit_ir[mode]) |
4330  AUXSPI_CNTL0_OUT_RISING(bit_or[mode]) |
4331  AUXSPI_CNTL0_INVERT_CLK(bit_ic[mode]) |
4332  AUXSPI_CNTL0_MSB_FIRST(txmsbf) |
4333  AUXSPI_CNTL0_SHIFT_LEN(bitlen);
4334 
4335  if (!count)
4336  {
4339 
4340  myGpioDelay(10);
4341 
4343 
4345 
4346  return;
4347  }
4348 
4350 
4352 
4353  spiACS(channel, cs);
4354 
4355  while ((txCnt < count) || (rxCnt < count))
4356  {
4357  statusReg = auxReg[AUX_SPI0_STAT_REG];
4358 
4359  rxEmpty = statusReg & AUXSPI_STAT_RX_EMPTY;
4360 
4361  txFull = (((statusReg>>28)&15) > 2);
4362 
4363  if (rxCnt < count)
4364  {
4365  if (!rxEmpty)
4366  {
4367  _spiRXBits(
4368  rxBuf, rxCnt++, bitlen, rxmsbf, auxReg[AUX_SPI0_IO_REG]);
4369  }
4370  }
4371 
4372  if (txCnt < count)
4373  {
4374  if (!txFull)
4375  {
4376  if (txCnt != (count-1))
4377  {
4379  _spiTXBits(txBuf, txCnt++, bitlen, txmsbf);
4380  }
4381  else
4382  {
4384  _spiTXBits(txBuf, txCnt++, bitlen, txmsbf);
4385  }
4386  }
4387  }
4388  }
4389 
4391 
4392  spiACS(channel, !cs);
4393 }
4394 
4395 static void spiGoS(
4396  unsigned speed,
4397  uint32_t flags,
4398  char *txBuf,
4399  char *rxBuf,
4400  unsigned count)
4401 {
4402  unsigned txCnt=0;
4403  unsigned rxCnt=0;
4404  unsigned cnt, cnt4w, cnt3w;
4405  uint32_t spiDefaults;
4406  unsigned mode, channel, cspol, cspols, flag3w, ren3w;
4407 
4408  channel = PI_SPI_FLAGS_GET_CHANNEL(flags);
4409  mode = PI_SPI_FLAGS_GET_MODE (flags);
4410  cspols = PI_SPI_FLAGS_GET_CSPOLS(flags);
4411  cspol = (cspols>>channel) & 1;
4412  flag3w = PI_SPI_FLAGS_GET_3WIRE(flags);
4413  ren3w = PI_SPI_FLAGS_GET_3WREN(flags);
4414 
4415  spiDefaults = SPI_CS_MODE(mode) |
4416  SPI_CS_CSPOLS(cspols) |
4417  SPI_CS_CS(channel) |
4418  SPI_CS_CSPOL(cspol) |
4419  SPI_CS_CLEAR(3);
4420 
4421  spiReg[SPI_DLEN] = 2; /* undocumented, stops inter-byte gap */
4422 
4423  spiReg[SPI_CS] = spiDefaults; /* stop */
4424 
4425  if (!count) return;
4426 
4427  if (flag3w)
4428  {
4429  if (ren3w < count)
4430  {
4431  cnt4w = ren3w;
4432  cnt3w = count - ren3w;
4433  }
4434  else
4435  {
4436  cnt4w = count;
4437  cnt3w = 0;
4438  }
4439  }
4440  else
4441  {
4442  cnt4w = count;
4443  cnt3w = 0;
4444  }
4445 
4446  spiReg[SPI_CLK] = 250000000/speed;
4447 
4448  spiReg[SPI_CS] = spiDefaults | SPI_CS_TA; /* start */
4449 
4450  cnt = cnt4w;
4451 
4452  while((txCnt < cnt) || (rxCnt < cnt))
4453  {
4454  while((rxCnt < cnt) && ((spiReg[SPI_CS] & SPI_CS_RXD)))
4455  {
4456  if (rxBuf) rxBuf[rxCnt] = spiReg[SPI_FIFO];
4457  else spi_dummy = spiReg[SPI_FIFO];
4458  rxCnt++;
4459  }
4460 
4461  while((txCnt < cnt) && ((spiReg[SPI_CS] & SPI_CS_TXD)))
4462  {
4463  if (txBuf) spiReg[SPI_FIFO] = txBuf[txCnt];
4464  else spiReg[SPI_FIFO] = 0;
4465  txCnt++;
4466  }
4467  }
4468 
4469  while (!(spiReg[SPI_CS] & SPI_CS_DONE)) ;
4470 
4471  /* now switch to 3-wire bus */
4472 
4473  cnt += cnt3w;
4474 
4475  spiReg[SPI_CS] |= SPI_CS_REN;
4476 
4477  while((txCnt < cnt) || (rxCnt < cnt))
4478  {
4479  while((rxCnt < cnt) && ((spiReg[SPI_CS] & SPI_CS_RXD)))
4480  {
4481  if (rxBuf) rxBuf[rxCnt] = spiReg[SPI_FIFO];
4482  else spi_dummy = spiReg[SPI_FIFO];
4483  rxCnt++;
4484  }
4485 
4486  while((txCnt < cnt) && ((spiReg[SPI_CS] & SPI_CS_TXD)))
4487  {
4488  if (txBuf) spiReg[SPI_FIFO] = txBuf[txCnt];
4489  else spiReg[SPI_FIFO] = 0;
4490  txCnt++;
4491  }
4492  }
4493 
4494  while (!(spiReg[SPI_CS] & SPI_CS_DONE)) ;
4495 
4496  spiReg[SPI_CS] = spiDefaults; /* stop */
4497 }
4498 
4499 static void spiGo(
4500  unsigned speed,
4501  uint32_t flags,
4502  char *txBuf,
4503  char *rxBuf,
4504  unsigned count)
4505 {
4506  static pthread_mutex_t main_mutex = PTHREAD_MUTEX_INITIALIZER;
4507  static pthread_mutex_t aux_mutex = PTHREAD_MUTEX_INITIALIZER;
4508 
4509  if (PI_SPI_FLAGS_GET_AUX_SPI(flags))
4510  {
4511  pthread_mutex_lock(&aux_mutex);
4512  spiGoA(speed, flags, txBuf, rxBuf, count);
4513  pthread_mutex_unlock(&aux_mutex);
4514  }
4515  else
4516  {
4517  pthread_mutex_lock(&main_mutex);
4518  spiGoS(speed, flags, txBuf, rxBuf, count);
4519  pthread_mutex_unlock(&main_mutex);
4520  }
4521 }
4522 
4523 static int spiAnyOpen(uint32_t flags)
4524 {
4525  int i, aux;
4526 
4527  aux = PI_SPI_FLAGS_GET_AUX_SPI(flags);
4528 
4529  for (i=0; i<PI_SPI_SLOTS; i++)
4530  {
4531  if ((spiInfo[i].state == PI_SPI_OPENED) &&
4532  (PI_SPI_FLAGS_GET_AUX_SPI(spiInfo[i].flags) == aux))
4533  return 1;
4534  }
4535  return 0;
4536 }
4537 
4538 static void spiInit(uint32_t flags)
4539 {
4540  uint32_t resvd, cspols;
4541 
4542  resvd = PI_SPI_FLAGS_GET_RESVD(flags);
4543  cspols = PI_SPI_FLAGS_GET_CSPOLS(flags);
4544 
4545  if (PI_SPI_FLAGS_GET_AUX_SPI(flags))
4546  {
4547  /* enable module and access to registers */
4548 
4550 
4551  /* save original state */
4552 
4559 
4562 
4563  /* manually control auxiliary SPI chip selects */
4564 
4565  if (!(resvd&1))
4566  {
4568  myGpioWrite(PI_ASPI_CE0, !(cspols&1));
4569  }
4570 
4571  if (!(resvd&2))
4572  {
4574  myGpioWrite(PI_ASPI_CE1, !(cspols&2));
4575  }
4576 
4577  if (!(resvd&4))
4578  {
4580  myGpioWrite(PI_ASPI_CE2, !(cspols&4));
4581  }
4582 
4583  /* set gpios to SPI mode */
4584 
4588  }
4589  else
4590  {
4591  /* save original state */
4592 
4598 
4601 
4602  /* set gpios to SPI mode */
4603 
4604  if (!(resvd&1)) myGpioSetMode(PI_SPI_CE0, PI_ALT0);
4605  if (!(resvd&2)) myGpioSetMode(PI_SPI_CE1, PI_ALT0);
4606 
4610  }
4611 }
4612 
4613 static void spiTerm(uint32_t flags)
4614 {
4615  int resvd;
4616 
4617  resvd = PI_SPI_FLAGS_GET_RESVD(flags);
4618 
4619  if (PI_SPI_FLAGS_GET_AUX_SPI(flags))
4620  {
4621  /* disable module and access to registers */
4622 
4624 
4625  /* restore original state */
4626 
4627  if (!(resvd&1)) myGpioSetMode(PI_ASPI_CE0, old_mode_ace0);
4628  if (!(resvd&2)) myGpioSetMode(PI_ASPI_CE1, old_mode_ace1);
4629  if (!(resvd&4)) myGpioSetMode(PI_ASPI_CE2, old_mode_ace2);
4630 
4634 
4637  }
4638  else
4639  {
4640  /* restore original state */
4641 
4642  if (!(resvd&1)) myGpioSetMode(PI_SPI_CE0, old_mode_ce0);
4643  if (!(resvd&2)) myGpioSetMode(PI_SPI_CE1, old_mode_ce1);
4644 
4648 
4651  }
4652 }
4653 
4654 int spiOpen(unsigned spiChan, unsigned baud, unsigned spiFlags)
4655 {
4656  static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
4657  int i, slot;
4658 
4659  DBG(DBG_USER, "spiChan=%d baud=%d spiFlags=0x%X",
4660  spiChan, baud, spiFlags);
4661 
4662  CHECK_INITED;
4663 
4664  if (PI_SPI_FLAGS_GET_AUX_SPI(spiFlags))
4665  {
4666  if (gpioHardwareRevision() < 16)
4667  SOFT_ERROR(PI_NO_AUX_SPI, "no auxiliary SPI on Pi A or B");
4668 
4670  }
4671  else
4673 
4674  if (spiChan >= i)
4675  SOFT_ERROR(PI_BAD_SPI_CHANNEL, "bad spiChan (%d)", spiChan);
4676 
4677  if ((baud < PI_SPI_MIN_BAUD) || (baud > PI_SPI_MAX_BAUD))
4678  SOFT_ERROR(PI_BAD_SPI_SPEED, "bad baud (%d)", baud);
4679 
4680  if (spiFlags > (1<<22))
4681  SOFT_ERROR(PI_BAD_FLAGS, "bad spiFlags (0x%X)", spiFlags);
4682 
4683  if (!spiAnyOpen(spiFlags)) /* initialise on first open */
4684  {
4685  spiInit(spiFlags);
4686  spiGo(baud, spiFlags, NULL, NULL, 0);
4687  }
4688 
4689  slot = -1;
4690 
4691  pthread_mutex_lock(&mutex);
4692 
4693  for (i=0; i<PI_SPI_SLOTS; i++)
4694  {
4695  if (spiInfo[i].state == PI_SPI_CLOSED)
4696  {
4697  slot = i;
4698  spiInfo[slot].state = PI_SPI_RESERVED;
4699  break;
4700  }
4701  }
4702 
4703  pthread_mutex_unlock(&mutex);
4704 
4705  if (slot < 0) SOFT_ERROR(PI_NO_HANDLE, "no SPI handles");
4706 
4707  spiInfo[slot].speed = baud;
4708  spiInfo[slot].flags = spiFlags | PI_SPI_FLAGS_CHANNEL(spiChan);
4709  spiInfo[slot].state = PI_SPI_OPENED;
4710 
4711  return slot;
4712 }
4713 
4714 int spiClose(unsigned handle)
4715 {
4716  DBG(DBG_USER, "handle=%d", handle);
4717 
4718  CHECK_INITED;
4719 
4720  if (handle >= PI_SPI_SLOTS)
4721  SOFT_ERROR(PI_BAD_HANDLE, "bad handle (%d)", handle);
4722 
4723  if (spiInfo[handle].state != PI_SPI_OPENED)
4724  SOFT_ERROR(PI_BAD_HANDLE, "bad handle (%d)", handle);
4725 
4726  spiInfo[handle].state = PI_I2C_CLOSED;
4727 
4728  if (!spiAnyOpen(spiInfo[handle].flags))
4729  spiTerm(spiInfo[handle].flags); /* terminate on last close */
4730 
4731  return 0;
4732 }
4733 
4734 int spiRead(unsigned handle, char *buf, unsigned count)
4735 {
4736  DBG(DBG_USER, "handle=%d count=%d [%s]",
4737  handle, count, myBuf2Str(count, buf));
4738 
4739  CHECK_INITED;
4740 
4741  if (handle >= PI_SPI_SLOTS)
4742  SOFT_ERROR(PI_BAD_HANDLE, "bad handle (%d)", handle);
4743 
4744  if (spiInfo[handle].state != PI_SPI_OPENED)
4745  SOFT_ERROR(PI_BAD_HANDLE, "bad handle (%d)", handle);
4746 
4747  if (count > PI_MAX_SPI_DEVICE_COUNT)
4748  SOFT_ERROR(PI_BAD_SPI_COUNT, "bad count (%d)", count);
4749 
4750  spiGo(spiInfo[handle].speed, spiInfo[handle].flags, NULL, buf, count);
4751 
4752  return count;
4753 }
4754 
4755 int spiWrite(unsigned handle, char *buf, unsigned count)
4756 {
4757  DBG(DBG_USER, "handle=%d count=%d [%s]",
4758  handle, count, myBuf2Str(count, buf));
4759 
4760  CHECK_INITED;
4761 
4762  if (handle >= PI_SPI_SLOTS)
4763  SOFT_ERROR(PI_BAD_HANDLE, "bad handle (%d)", handle);
4764 
4765  if (spiInfo[handle].state != PI_SPI_OPENED)
4766  SOFT_ERROR(PI_BAD_HANDLE, "bad handle (%d)", handle);
4767 
4768  if (count > PI_MAX_SPI_DEVICE_COUNT)
4769  SOFT_ERROR(PI_BAD_SPI_COUNT, "bad count (%d)", count);
4770 
4771  spiGo(spiInfo[handle].speed, spiInfo[handle].flags, buf, NULL, count);
4772 
4773  return count;
4774 }
4775 
4776 int spiXfer(unsigned handle, char *txBuf, char *rxBuf, unsigned count)
4777 {
4778  DBG(DBG_USER, "handle=%d count=%d [%s]",
4779  handle, count, myBuf2Str(count, txBuf));
4780 
4781  CHECK_INITED;
4782 
4783  if (handle >= PI_SPI_SLOTS)
4784  SOFT_ERROR(PI_BAD_HANDLE, "bad handle (%d)", handle);
4785 
4786  if (spiInfo[handle].state != PI_SPI_OPENED)
4787  SOFT_ERROR(PI_BAD_HANDLE, "bad handle (%d)", handle);
4788 
4789  if (count > PI_MAX_SPI_DEVICE_COUNT)
4790  SOFT_ERROR(PI_BAD_SPI_COUNT, "bad count (%d)", count);
4791 
4792  spiGo(spiInfo[handle].speed, spiInfo[handle].flags, txBuf, rxBuf, count);
4793 
4794  return count;
4795 }
4796 
4797 /* ======================================================================= */
4798 
4799 
4800 int serOpen(char *tty, unsigned serBaud, unsigned serFlags)
4801 {
4802  static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
4803  struct termios new;
4804  int speed;
4805  int fd;
4806  int i, slot;
4807 
4808  DBG(DBG_USER, "tty=%s serBaud=%d serFlags=0x%X", tty, serBaud, serFlags);
4809 
4811 
4812  if (strncmp("/dev/tty", tty, 8) && strncmp("/dev/serial", tty, 11))
4813  SOFT_ERROR(PI_BAD_SER_DEVICE, "bad device (%s)", tty);
4814 
4815  switch (serBaud)
4816  {
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;
4835 
4836  default:
4837  SOFT_ERROR(PI_BAD_SER_SPEED, "bad speed (%d)", serBaud);
4838  }
4839 
4840  if (serFlags)
4841  SOFT_ERROR(PI_BAD_FLAGS, "bad flags (0x%X)", serFlags);
4842 
4843  slot = -1;
4844 
4845  pthread_mutex_lock(&mutex);
4846 
4847  for (i=0; i<PI_SER_SLOTS; i++)
4848  {
4849  if (serInfo[i].state == PI_SER_CLOSED)
4850  {
4851  slot = i;
4852  serInfo[slot].state = PI_SER_RESERVED;
4853  break;
4854  }
4855  }
4856 
4857  pthread_mutex_unlock(&mutex);
4858 
4859  if (slot < 0) SOFT_ERROR(PI_NO_HANDLE, "no serial handles");
4860 
4861  if ((fd = open(tty, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK)) == -1)
4862  {
4863  serInfo[slot].state = PI_SER_CLOSED;
4864  return PI_SER_OPEN_FAILED;
4865  }
4866 
4867  tcgetattr(fd, &new);
4868 
4869  cfmakeraw(&new);
4870 
4871  cfsetispeed(&new, speed);
4872  cfsetospeed(&new, speed);
4873 
4874  new.c_cc [VMIN] = 0;
4875  new.c_cc [VTIME] = 0;
4876 
4877  tcflush(fd, TCIFLUSH);
4878  tcsetattr(fd, TCSANOW, &new);
4879 
4880  //fcntl(fd, F_SETFL, O_RDWR);
4881 
4882  serInfo[slot].fd = fd;
4883  serInfo[slot].flags = serFlags;
4884  serInfo[slot].state = PI_SER_OPENED;
4885 
4886  return slot;
4887 }
4888 
4889 int serClose(unsigned handle)
4890 {
4891  DBG(DBG_USER, "handle=%d", handle);
4892 
4894 
4895  if (handle >= PI_SER_SLOTS)
4896  SOFT_ERROR(PI_BAD_HANDLE, "bad handle (%d)", handle);
4897 
4898  if (serInfo[handle].state != PI_SER_OPENED)
4899  SOFT_ERROR(PI_BAD_HANDLE, "bad handle (%d)", handle);
4900 
4901  if (serInfo[handle].fd >= 0) close(serInfo[handle].fd);
4902 
4903  serInfo[handle].fd = -1;
4904  serInfo[handle].state = PI_SER_CLOSED;
4905 
4906  return 0;
4907 }
4908 
4909 int serWriteByte(unsigned handle, unsigned bVal)
4910 {
4911  char c;
4912 
4913  DBG(DBG_USER, "handle=%d bVal=%d", handle, bVal);
4914 
4916 
4917  if (handle >= PI_SER_SLOTS)
4918  SOFT_ERROR(PI_BAD_HANDLE, "bad handle (%d)", handle);
4919 
4920  if (serInfo[handle].state != PI_SER_OPENED)
4921  SOFT_ERROR(PI_BAD_HANDLE, "bad handle (%d)", handle);
4922 
4923  if (bVal > 0xFF)
4924  SOFT_ERROR(PI_BAD_PARAM, "bad parameter (%d)", bVal);
4925 
4926  c = bVal;
4927 
4928  if (write(serInfo[handle].fd, &c, 1) != 1)
4929  return PI_SER_WRITE_FAILED;
4930  else
4931  return 0;
4932 }
4933 
4934 int serReadByte(unsigned handle)
4935 {
4936  int r;
4937  char x;
4938 
4939  DBG(DBG_USER, "handle=%d", handle);
4940 
4942 
4943  if (handle >= PI_SER_SLOTS)
4944  SOFT_ERROR(PI_BAD_HANDLE, "bad handle (%d)", handle);
4945 
4946  if (serInfo[handle].state != PI_SER_OPENED)
4947  SOFT_ERROR(PI_BAD_HANDLE, "bad handle (%d)", handle);
4948 
4949  r = read(serInfo[handle].fd, &x, 1);
4950 
4951  if (r == 1)
4952  return ((int)x) & 0xFF;
4953 
4954  else if (r == 0)
4955  return PI_SER_READ_NO_DATA;
4956 
4957  else if ((r == -1) && (errno == EAGAIN))
4958  return PI_SER_READ_NO_DATA;
4959 
4960  else
4961  return PI_SER_READ_FAILED;
4962 }
4963 
4964 int serWrite(unsigned handle, char *buf, unsigned count)
4965 {
4966  int written=0, wrote=0;
4967 
4968  DBG(DBG_USER, "handle=%d count=%d [%s]",
4969  handle, count, myBuf2Str(count, buf));
4970 
4972 
4973  if (handle >= PI_SER_SLOTS)
4974  SOFT_ERROR(PI_BAD_HANDLE, "bad handle (%d)", handle);
4975 
4976  if (serInfo[handle].state != PI_SER_OPENED)
4977  SOFT_ERROR(PI_BAD_HANDLE, "bad handle (%d)", handle);
4978 
4979  if (!count)
4980  SOFT_ERROR(PI_BAD_PARAM, "bad count (%d)", count);
4981 
4982  while ((written != count) && (wrote >= 0))
4983  {
4984  wrote = write(serInfo[handle].fd, buf+written, count-written);
4985 
4986  if (wrote >= 0)
4987  {
4988  written += wrote;
4989 
4990  if (written != count) time_sleep(0.05);
4991  }
4992  }
4993 
4994  if (written != count)
4995  return PI_SER_WRITE_FAILED;
4996  else
4997  return 0;
4998 }
4999 
5000 int serRead(unsigned handle, char *buf, unsigned count)
5001 {
5002  int r;
5003 
5004  DBG(DBG_USER, "handle=%d count=%d buf=0x%X", handle, count, (unsigned)buf);
5005 
5007 
5008  if (handle >= PI_SER_SLOTS)
5009  SOFT_ERROR(PI_BAD_HANDLE, "bad handle (%d)", handle);
5010 
5011  if (serInfo[handle].state != PI_SER_OPENED)
5012  SOFT_ERROR(PI_BAD_HANDLE, "bad handle (%d)", handle);
5013 
5014  if (!count)
5015  SOFT_ERROR(PI_BAD_PARAM, "bad count (%d)", count);
5016 
5017  r = read(serInfo[handle].fd, buf, count);
5018 
5019  if (r == -1)
5020  {
5021  if (errno == EAGAIN)
5022  return PI_SER_READ_NO_DATA;
5023  else
5024  return PI_SER_READ_FAILED;
5025  }
5026  else
5027  {
5028  if (r < count) buf[r] = 0;
5029  return r;
5030  }
5031 }
5032 
5034 {
5035  int result;
5036 
5037  DBG(DBG_USER, "handle=%d", handle);
5038 
5040 
5041  if (handle >= PI_SER_SLOTS)
5042  SOFT_ERROR(PI_BAD_HANDLE, "bad handle (%d)", handle);
5043 
5044  if (serInfo[handle].state != PI_SER_OPENED)
5045  SOFT_ERROR(PI_BAD_HANDLE, "bad handle (%d)", handle);
5046 
5047  if (ioctl(serInfo[handle].fd, FIONREAD, &result) == -1) return 0;
5048 
5049  return result;
5050 }
5051 
5052 /* ======================================================================= */
5053 
5054 static int chooseBestClock
5055  (clkInf_t *clkInf, unsigned f, unsigned numc, unsigned *cf)
5056 {
5057  int c, valid, offby, offby2, best_offby;
5058  uint32_t div;
5059  uint64_t frac;
5060 
5061  valid = 0;
5062  best_offby = 0;
5063 
5064  for (c=0; c<numc; c++)
5065  {
5066  div = cf[c] / f;
5067 
5068  if ((div > 1) && (div < 4096))
5069  {
5070  if (f < PI_MASH_MAX_FREQ)
5071  {
5072  frac = cf[c] - (div * f);
5073  frac = (frac * 4096) / f;
5074  offby = cf[c] - (div * f) - ((frac * f) / 4096);
5075  if (frac < 4095)
5076  {
5077  offby2 = cf[c] - (div * f) - (((frac+1) * f) / 4096);
5078  if (offby2 < 0) offby2 = -offby2;
5079  if (offby2 < offby)
5080  {
5081  offby = offby2;
5082  frac++;
5083  }
5084  }
5085  }
5086  else
5087  {
5088  frac = 0;
5089  offby = cf[c] - (div * f);
5090  if (div < 4095)
5091  {
5092  offby2 = cf[c] - ((div+1) * f);
5093  if (offby2 < 0) offby2 = -offby2;
5094  if (offby2 < offby)
5095  {
5096  offby = offby2;
5097  div++;
5098  }
5099  }
5100  }
5101 
5102  if ((!valid) || (offby <= best_offby))
5103  {
5104  valid = 1;
5105  clkInf->div = div;
5106  clkInf->frac = frac;
5107  clkInf->clock = c;
5108  best_offby = offby;
5109  }
5110  }
5111  }
5112  return valid;
5113 }
5114 
5115 /* ======================================================================= */
5116 
5117 static rawCbs_t * dmaCB2adr(int pos)
5118 {
5119  int page, slot;
5120 
5121  page = pos/CBS_PER_IPAGE;
5122  slot = pos%CBS_PER_IPAGE;
5123 
5124  return &dmaIVirt[page]->cb[slot];
5125 }
5126 
5127 /* ----------------------------------------------------------------------- */
5128 
5129 static void dmaCbPrint(int pos)
5130 {
5131  rawCbs_t * p;
5132 
5133  p = dmaCB2adr(pos);
5134 
5135  fprintf(stderr, "i=%x s=%x d=%x len=%x s=%x nxt=%x\n",
5136  p->info, p->src, p->dst, p->length, p->stride, p->next);
5137 }
5138 
5139 /* ----------------------------------------------------------------------- */
5140 
5141 static unsigned dmaNowAtICB(void)
5142 {
5143  unsigned cb;
5144  static unsigned lastPage=0;
5145  unsigned page;
5146  uint32_t cbAddr;
5147  uint32_t startTick, endTick;
5148 
5149  startTick = systReg[SYST_CLO];
5150 
5151  cbAddr = dmaIn[DMA_CONBLK_AD];
5152 
5153  page = lastPage;
5154 
5155  /* which page are we dma'ing? */
5156 
5157  while (1)
5158  {
5159  cb = (cbAddr - ((int)dmaIBus[page])) / 32;
5160 
5161  if (cb < CBS_PER_IPAGE)
5162  {
5163  endTick = systReg[SYST_CLO];
5164 
5165  if (endTick != startTick)
5166  gpioStats.cbTicks += (endTick - startTick);
5167 
5168  gpioStats.cbCalls++;
5169 
5170  lastPage = page;
5171 
5172  return (page*CBS_PER_IPAGE) + cb;
5173  }
5174 
5175  if (page++ >= DMAI_PAGES) page=0;
5176 
5177  if (page == lastPage) break;
5178  }
5179 
5180  return 0;
5181 }
5182 
5183 /* ----------------------------------------------------------------------- */
5184 
5185 static int dmaNowAtOCB(void)
5186 {
5187  unsigned cb;
5188  unsigned page;
5189  uint32_t cbAddr;
5190 
5191  cbAddr = dmaOut[DMA_CONBLK_AD];
5192 
5193  if (!cbAddr) return -PI_NO_TX_WAVE;
5194 
5195  page = 0;
5196 
5197  /* which page are we dma'ing? */
5198 
5199  while (1)
5200  {
5201  cb = (cbAddr - ((int)dmaOBus[page])) / 32;
5202 
5203  if (cb < CBS_PER_OPAGE) return (page*CBS_PER_OPAGE) + cb;
5204 
5205  if (page++ >= DMAO_PAGES) break;
5206  }
5207 
5208  /* Try twice */
5209 
5210  cbAddr = dmaOut[DMA_CONBLK_AD];
5211 
5212  if (!cbAddr) return -PI_NO_TX_WAVE;
5213 
5214  page = 0;
5215 
5216  /* which page are we dma'ing? */
5217 
5218  while (1)
5219  {
5220  cb = (cbAddr - ((int)dmaOBus[page])) / 32;
5221 
5222  if (cb < CBS_PER_OPAGE) return (page*CBS_PER_OPAGE) + cb;
5223 
5224  if (page++ >= DMAO_PAGES) break;
5225  }
5226 
5227  return -PI_WAVE_NOT_FOUND;
5228 }
5229 
5230 /* ----------------------------------------------------------------------- */
5231 
5232 unsigned rawWaveCB(void)
5233 {
5234  unsigned cb;
5235  static unsigned lastPage=0;
5236  unsigned page;
5237  uint32_t cbAddr;
5238 
5239  cbAddr = dmaOut[DMA_CONBLK_AD];
5240 
5241  if (!cbAddr) return -1;
5242 
5243  page = lastPage;
5244 
5245  /* which page are we dma'ing? */
5246 
5247  while (1)
5248  {
5249  cb = (cbAddr - ((int)dmaOBus[page])) / 32;
5250 
5251  if (cb < CBS_PER_OPAGE)
5252  {
5253  lastPage = page;
5254 
5255  return (page*CBS_PER_OPAGE) + cb;
5256  }
5257 
5258  if (page++ >= DMAO_PAGES) page=0;
5259 
5260  if (page == lastPage) break;
5261  }
5262 
5263  return 0;
5264 }
5265 
5266 /* ----------------------------------------------------------------------- */
5267 
5268 static unsigned dmaCurrentSlot(unsigned pos)
5269 {
5270  unsigned cycle=0, slot=0, tmp;
5271 
5272  cycle = (pos/CBS_PER_CYCLE);
5273  tmp = (pos%CBS_PER_CYCLE);
5274 
5275  if (tmp > 2) slot = ((tmp-2)/3);
5276 
5277  return (cycle*PULSE_PER_CYCLE)+slot;
5278 }
5279 
5280 /* ----------------------------------------------------------------------- */
5281 
5282 static uint32_t dmaPwmDataAdr(int pos)
5283 {
5284  return (uint32_t) &dmaIBus[pos]->periphData;
5285 }
5286 
5287 /* ----------------------------------------------------------------------- */
5288 
5289 static uint32_t dmaGpioOnAdr(int pos)
5290 {
5291  int page, slot;
5292 
5293  page = pos/ON_PER_IPAGE;
5294  slot = pos%ON_PER_IPAGE;
5295 
5296  return (uint32_t) &dmaIBus[page]->gpioOn[slot];
5297 }
5298 
5299 /* ----------------------------------------------------------------------- */
5300 
5301 static uint32_t dmaGpioOffAdr(int pos)
5302 {
5303  int page, slot;
5304 
5305  myOffPageSlot(pos, &page, &slot);
5306 
5307  return (uint32_t) &dmaIBus[page]->gpioOff[slot];
5308 }
5309 
5310 /* ----------------------------------------------------------------------- */
5311 
5312 static uint32_t dmaTickAdr(int pos)
5313 {
5314  int page, slot;
5315 
5316  myTckPageSlot(pos, &page, &slot);
5317 
5318  return (uint32_t) &dmaIBus[page]->tick[slot];
5319 }
5320 
5321 /* ----------------------------------------------------------------------- */
5322 
5323 static uint32_t dmaReadLevelsAdr(int pos)
5324 {
5325  int page, slot;
5326 
5327  myLvsPageSlot(pos, &page, &slot);
5328 
5329  return (uint32_t) &dmaIBus[page]->level[slot];
5330 }
5331 
5332 /* ----------------------------------------------------------------------- */
5333 
5334 static uint32_t dmaCbAdr(int pos)
5335 {
5336  int page, slot;
5337 
5338  page = (pos/CBS_PER_IPAGE);
5339  slot = (pos%CBS_PER_IPAGE);
5340 
5341  return (uint32_t) &dmaIBus[page]->cb[slot];
5342 }
5343 
5344 /* ----------------------------------------------------------------------- */
5345 
5346 static void dmaGpioOnCb(int b, int pos)
5347 {
5348  rawCbs_t * p;
5349 
5350  p = dmaCB2adr(b);
5351 
5352  p->info = NORMAL_DMA;
5353  p->src = dmaGpioOnAdr(pos);
5354  p->dst = ((GPIO_BASE + (GPSET0*4)) & 0x00ffffff) | PI_PERI_BUS;
5355  p->length = 4;
5356  p->next = dmaCbAdr(b+1);
5357 }
5358 
5359 /* ----------------------------------------------------------------------- */
5360 
5361 static void dmaTickCb(int b, int pos)
5362 {
5363  rawCbs_t * p;
5364 
5365  p = dmaCB2adr(b);
5366 
5367  p->info = NORMAL_DMA;
5368  p->src = ((SYST_BASE + (SYST_CLO*4)) & 0x00ffffff) | PI_PERI_BUS;
5369  p->dst = dmaTickAdr(pos);
5370  p->length = 4;
5371  p->next = dmaCbAdr(b+1);
5372 }
5373 
5374 /* ----------------------------------------------------------------------- */
5375 
5376 static void dmaGpioOffCb(int b, int pos)
5377 {
5378  rawCbs_t * p;
5379 
5380  p = dmaCB2adr(b);
5381 
5382  p->info = NORMAL_DMA;
5383  p->src = dmaGpioOffAdr(pos);
5384  p->dst = ((GPIO_BASE + (GPCLR0*4)) & 0x00ffffff) | PI_PERI_BUS;
5385  p->length = 4;
5386  p->next = dmaCbAdr(b+1);
5387 }
5388 
5389 /* ----------------------------------------------------------------------- */
5390 
5391 static void dmaReadLevelsCb(int b, int pos)
5392 {
5393  rawCbs_t * p;
5394 
5395  p = dmaCB2adr(b);
5396 
5397  p->info = NORMAL_DMA;
5398  p->src = ((GPIO_BASE + (GPLEV0*4)) & 0x00ffffff) | PI_PERI_BUS;
5399  p->dst = dmaReadLevelsAdr(pos);
5400  p->length = 4;
5401  p->next = dmaCbAdr(b+1);
5402 }
5403 
5404 /* ----------------------------------------------------------------------- */
5405 
5406 static void dmaDelayCb(int b)
5407 {
5408  rawCbs_t * p;
5409 
5410  p = dmaCB2adr(b);
5411 
5412  if (gpioCfg.clockPeriph == PI_CLOCK_PCM)
5413  {
5414  p->info = NORMAL_DMA | TIMED_DMA(2);
5415  p->dst = PCM_TIMER;
5416  }
5417  else
5418  {
5419  p->info = NORMAL_DMA | TIMED_DMA(5);
5420  p->dst = PWM_TIMER;
5421  }
5422 
5423  p->src = dmaPwmDataAdr(b%DMAI_PAGES);
5424  p->length = 4;
5425  p->next = dmaCbAdr(b+1);
5426 }
5427 
5428 /* ----------------------------------------------------------------------- */
5429 
5430 static void dmaInitCbs(void)
5431 {
5432  int b, pulse, level, cycle;
5433 
5434  rawCbs_t * p;
5435 
5436  /* set up the DMA control blocks */
5437 
5438  DBG(DBG_STARTUP, "");
5439 
5440  gpioStats.dmaInitCbsCount++;
5441 
5442  b = -1;
5443  level = 0;
5444 
5445  for (cycle=0; cycle<bufferCycles; cycle++)
5446  {
5447  b++; dmaGpioOnCb(b, cycle%SUPERCYCLE); /* gpio on slot */
5448 
5449  b++; dmaTickCb(b, cycle); /* tick slot */
5450 
5451  for (pulse=0; pulse<PULSE_PER_CYCLE; pulse++)
5452  {
5453  b++; dmaReadLevelsCb(b, level); /* read levels slot */
5454 
5455  b++; dmaDelayCb(b); /* delay slot */
5456 
5457  b++; dmaGpioOffCb(b, (level%SUPERLEVEL)+1); /* gpio off slot */
5458 
5459  ++level;
5460  }
5461  }
5462 
5463  /* point last cb back to first for continuous loop */
5464 
5465  p = dmaCB2adr(b);
5466 
5467  p->next = dmaCbAdr(0);
5468 
5469  DBG(DBG_STARTUP, "DMA page type count = %d", sizeof(dmaIPage_t));
5470 
5471  DBG(DBG_STARTUP, "%d control blocks (exp=%d)", b+1, NUM_CBS);
5472 }
5473 
5474 /* ======================================================================= */
5475 
5476 
5477 static void sigHandler(int signum)
5478 {
5479  if ((signum >= PI_MIN_SIGNUM) && (signum <= PI_MAX_SIGNUM))
5480  {
5481  if (gpioSignal[signum].func)
5482  {
5483  if (gpioSignal[signum].ex)
5484  {
5485  (gpioSignal[signum].func)(signum, gpioSignal[signum].userdata);
5486  }
5487  else
5488  {
5489  (gpioSignal[signum].func)(signum);
5490  }
5491  }
5492  else
5493  {
5494  switch(signum)
5495  {
5496  case SIGUSR1:
5497 
5498  if (gpioCfg.dbgLevel > DBG_MIN_LEVEL) --gpioCfg.dbgLevel;
5499  else gpioCfg.dbgLevel = DBG_MIN_LEVEL;
5500  DBG(DBG_USER, "Debug level %d\n", gpioCfg.dbgLevel);
5501  break;
5502 
5503  case SIGUSR2:
5504  if (gpioCfg.dbgLevel < DBG_MAX_LEVEL) ++gpioCfg.dbgLevel;
5505  else gpioCfg.dbgLevel = DBG_MAX_LEVEL;
5506  DBG(DBG_USER, "Debug level %d\n", gpioCfg.dbgLevel);
5507  break;
5508 
5509  case SIGPIPE:
5510  case SIGWINCH:
5511  DBG(DBG_USER, "signal %d ignored", signum);
5512  break;
5513 
5514  case SIGCHLD:
5515  /* Used to notify threads of events */
5516  break;
5517 
5518  default:
5519  DBG(DBG_ALWAYS, "Unhandled signal %d, terminating\n", signum);
5520  gpioTerminate();
5521  exit(-1);
5522  }
5523  }
5524  }
5525  else
5526  {
5527  /* exit */
5528 
5529  DBG(DBG_ALWAYS, "Unhandled signal %d, terminating\n", signum);
5530  gpioTerminate();
5531  exit(-1);
5532  }
5533 }
5534 
5535 /* ----------------------------------------------------------------------- */
5536 
5537 static void sigSetHandler(void)
5538 {
5539  int i;
5540  struct sigaction new;
5541 
5542  for (i=PI_MIN_SIGNUM; i<=PI_MAX_SIGNUM; i++)
5543  {
5544 
5545  memset(&new, 0, sizeof(new));
5546  new.sa_handler = sigHandler;
5547 
5548  sigaction(i, &new, NULL);
5549  }
5550 }
5551 
5552 /*
5553  freq mics net
5554  0 1000 1000 900
5555  1 4000 250 225
5556  2 3750 266 240
5557  3 3500 285 257
5558  4 3250 307 276
5559  5 3000 333 300
5560  6 2750 363 327
5561  7 2500 400 360
5562  8 2250 444 400
5563  9 2000 500 450
5564 10 1750 571 514
5565 11 1500 666 600
5566 12 1250 800 720
5567 13 1000 1000 900
5568 14 750 1333 1200
5569 15 500 2000 1800
5570 */
5571 
5572 unsigned alert_delays[]=
5573 {
5574  900000, 225000, 240000, 257142, 276923, 300000, 327272, 360000,
5575  400000, 450000, 514285, 600000, 720000, 900000, 1200000, 1800000
5576 };
5577 
5578 /* ======================================================================= */
5579 
5580 static void alertGlitchFilter(gpioSample_t *sample, int numSamples)
5581 {
5582  int i, j, diff;
5583  uint32_t steadyUs, changedTick, RBitV, LBitV;
5584  uint32_t bit, bitV;
5585 
5586  for (i=0; i<=PI_MAX_USER_GPIO; i++)
5587  {
5588  bit = (1<<i);
5589 
5590  if (monitorBits & bit & gFilterBits)
5591  {
5592  steadyUs = gpioAlert[i].gfSteadyUs;
5593  RBitV = gpioAlert[i].gfRBitV;
5594  LBitV = gpioAlert[i].gfLBitV;
5595  changedTick = gpioAlert[i].gfTick;
5596 
5597  for (j=0; j<numSamples; j++)
5598  {
5599  bitV = sample[j].level & bit;
5600 
5601  if (bitV != LBitV)
5602  {
5603  /* Difference between level and last level.
5604  Restart steady timer. */
5605 
5606  changedTick = sample[j].tick;
5607  LBitV = bitV;
5608  }
5609 
5610  if (bitV != RBitV)
5611  {
5612  /* Difference between level and reported level. */
5613 
5614  diff = sample[j].tick - changedTick;
5615 
5616  if (diff >= steadyUs)
5617  {
5618  /* Level stable for steady period. */
5619  RBitV = bitV;
5620  }
5621  else
5622  {
5623  /* Keep reporting old level. */
5624 
5625  sample[j].level ^= bit;
5626  }
5627  }
5628 
5629  }
5630 
5631  gpioAlert[i].gfRBitV = RBitV;
5632  gpioAlert[i].gfLBitV = LBitV;
5633  gpioAlert[i].gfTick = changedTick;
5634  }
5635  }
5636 }
5637 
5638 static void alertNoiseFilter(gpioSample_t *sample, int numSamples)
5639 {
5640  int i, j, diff;
5641  uint32_t LBitV;
5642  uint32_t bit, bitV;
5643  uint32_t nowTick;
5644 
5645  for (i=0; i<=PI_MAX_USER_GPIO; i++)
5646  {
5647  bit = (1<<i);
5648 
5649  if (monitorBits & bit & nFilterBits)
5650  {
5651  LBitV = gpioAlert[i].nfLBitV;
5652 
5653  for (j=0; j<numSamples; j++)
5654  {
5655  bitV = sample[j].level & bit;
5656  nowTick = sample[j].tick;
5657 
5658  if (gpioAlert[i].nfActive) /* reporting events */
5659  {
5660  diff = nowTick - gpioAlert[i].nfTick2;
5661 
5662  if (diff >= 0)
5663  {
5664  /* Stop reporting gpio changes */
5665 
5666  gpioAlert[i].nfActive = 0;
5667  gpioAlert[i].nfTick1 = nowTick;
5668  }
5669  }
5670  else /* waiting for steady us */
5671  {
5672  if (bitV != LBitV)
5673  {
5674  diff = nowTick - gpioAlert[i].nfTick1;
5675  gpioAlert[i].nfTick1 = nowTick;
5676 
5677  if (diff >= gpioAlert[i].nfSteadyUs)
5678  {
5679  /* Start reporting gpio changes */
5680 
5681  gpioAlert[i].nfRBitV = LBitV;
5682  gpioAlert[i].nfActive = 1;
5683  gpioAlert[i].nfTick2 =
5684  nowTick + gpioAlert[i].nfActiveUs;
5685  }
5686  }
5687  }
5688 
5689  if (!gpioAlert[i].nfActive)
5690  {
5691  if (bitV != gpioAlert[i].nfRBitV)
5692  sample[j].level ^= bit;
5693  }
5694 
5695  LBitV = bitV;
5696  }
5697 
5698  gpioAlert[i].nfLBitV = LBitV;
5699 
5700  }
5701  }
5702 }
5703 
5704 static void alertEmit(
5705  gpioSample_t *sample, int numSamples, uint32_t changedBits, uint32_t eTick)
5706 {
5707  uint32_t oldLevel, newLevel;
5708  int32_t diff;
5709  int emit, seqno, emitted;
5710  uint32_t changes, bits, timeoutBits, eventBits;
5711  int d;
5712  int b, n, v;
5713  int err;
5714  int max_emits;
5715  char fifo[32];
5716  /* ensure space for maximum number of watchdog and event notifications */
5718 
5719  if (changedBits)
5720  {
5721  if (gpioGetSamples.func)
5722  {
5723  if (gpioGetSamples.ex)
5724  {
5725  (gpioGetSamples.func)
5726  (sample, numSamples, gpioGetSamples.userdata);
5727  }
5728  else
5729  {
5730  (gpioGetSamples.func)(sample, numSamples);
5731  }
5732  }
5733  }
5734 
5735  eventBits = 0;
5736 
5737  if (bscFR != (bscsReg[BSC_FR]&0xffff))
5738  {
5739  bscFR = bscsReg[BSC_FR]&0xffff;
5740  eventAlert[PI_EVENT_BSC].fired = 1;
5741  }
5742 
5743  for (b=0; b<=PI_MAX_EVENT; b++)
5744  {
5745  if (eventAlert[b].fired && (!eventAlert[b].ignore))
5746  {
5747  eventBits |= (1<<b);
5748 
5749  if (eventAlert[b].func)
5750  {
5751  if (eventAlert[b].ex)
5752  {
5753  (eventAlert[b].func)(b, eTick, eventAlert[b].userdata);
5754  }
5755  else
5756  {
5757  (eventAlert[b].func)(b, eTick);
5758  }
5759  }
5760  }
5761 
5762  eventAlert[b].fired = 0;
5763  }
5764 
5765  /* call alert callbacks for each bit transition */
5766 
5767  if (changedBits & alertBits)
5768  {
5769  oldLevel = (reportedLevel & alertBits);
5770 
5771  for (d=0; d<numSamples; d++)
5772  {
5773  newLevel = (sample[d].level & alertBits);
5774 
5775  if (newLevel != oldLevel)
5776  {
5777  changes = (newLevel ^ oldLevel);
5778 
5779  for (b=0; b<=PI_MAX_USER_GPIO; b++)
5780  {
5781  if (changes & (1<<b))
5782  {
5783  if (newLevel & (1<<b)) v = 1; else v = 0;
5784 
5785  if (gpioAlert[b].func)
5786  {
5787  if (gpioAlert[b].ex)
5788  {
5789  (gpioAlert[b].func)
5790  (b, v, sample[d].tick,
5791  gpioAlert[b].userdata);
5792  }
5793  else
5794  {
5795  (gpioAlert[b].func)(b, v, sample[d].tick);
5796  }
5797  }
5798  }
5799  }
5800  oldLevel = newLevel;
5801  }
5802  }
5803  }
5804 
5805  /* check for watchdog timeouts */
5806 
5807  timeoutBits = 0;
5808 
5809  if (wdogBits)
5810  {
5811  for (b=0; b<=PI_MAX_USER_GPIO; b++)
5812  {
5813  if (gpioAlert[b].wdSteadyUs)
5814  {
5815  diff = eTick - gpioAlert[b].wdTick;
5816 
5817  if (diff >= gpioAlert[b].wdSteadyUs)
5818  {
5819  timeoutBits |= (1<<b);
5820 
5821  gpioAlert[b].wdTick = eTick;
5822 
5823  if (gpioAlert[b].func)
5824  {
5825  if (gpioAlert[b].ex)
5826  {
5827  (gpioAlert[b].func)(b, PI_TIMEOUT, eTick,
5828  gpioAlert[b].userdata);
5829  }
5830  else
5831  {
5832  (gpioAlert[b].func)(b, PI_TIMEOUT, eTick);
5833  }
5834  }
5835  }
5836  }
5837  }
5838  }
5839 
5840  for (n=0; n<PI_NOTIFY_SLOTS; n++)
5841  {
5842  if (gpioNotify[n].state == PI_NOTIFY_CLOSING)
5843  {
5844  if (gpioNotify[n].pipe)
5845  {
5846  DBG(DBG_INTERNAL, "close notify pipe %d", gpioNotify[n].fd);
5847  close(gpioNotify[n].fd);
5848 
5849  sprintf(fifo, "/dev/pigpio%d", n);
5850 
5851  unlink(fifo);
5852  }
5853 
5854  gpioNotify[n].state = PI_NOTIFY_CLOSED;
5855  }
5856  else if (gpioNotify[n].state >= PI_NOTIFY_OPENED)
5857  {
5858  bits = gpioNotify[n].bits;
5859 
5860  emit = 0;
5861 
5862  seqno = gpioNotify[n].seqno;
5863 
5864  if (gpioNotify[n].state == PI_NOTIFY_RUNNING)
5865  {
5866  /* check to see if any bits have changed for this
5867  notification.
5868 
5869  bits is the set of notification bits
5870  changedBits is the set of changed bits
5871  */
5872 
5873  if (changedBits & bits)
5874  {
5875  oldLevel = reportedLevel & bits;
5876 
5877  for (d=0; d<numSamples; d++)
5878  {
5879  newLevel = sample[d].level & bits;
5880 
5881  if (newLevel != oldLevel)
5882  {
5883  report[emit].seqno = seqno;
5884  report[emit].flags = 0;
5885  report[emit].tick = sample[d].tick;
5886  report[emit].level = sample[d].level;
5887 
5888  oldLevel = newLevel;
5889 
5890  emit++;
5891  seqno++;
5892  }
5893  }
5894  }
5895 
5896  /* check to see if any watchdogs are due for this
5897  notification.
5898 
5899  bits is the set of notification bits
5900  timeoutBits is the set of timed out bits
5901  */
5902 
5903  bits = gpioNotify[n].bits;
5904 
5905  if (timeoutBits & bits)
5906  {
5907  /* at least one watchdog has fired for this
5908  notification.
5909  */
5910 
5911  for (b=0; b<=PI_MAX_USER_GPIO; b++)
5912  {
5913  if (timeoutBits & bits & (1<<b))
5914  {
5915  if (numSamples)
5916  newLevel = sample[numSamples-1].level;
5917  else
5918  newLevel = reportedLevel;
5919 
5920  report[emit].seqno = seqno;
5921  report[emit].flags =
5923  report[emit].tick = eTick;
5924  report[emit].level = newLevel;
5925 
5926  emit++;
5927  seqno++;
5928  }
5929  }
5930  }
5931  }
5932 
5933  /* check to see if any events are due
5934 
5935  eventBits is the set of events
5936  */
5937 
5938  if (eventBits & gpioNotify[n].eventBits)
5939  {
5940  for (b=0; b<=PI_MAX_EVENT; b++)
5941  {
5942  if (eventBits & gpioNotify[n].eventBits & (1<<b))
5943  {
5944  if (numSamples)
5945  newLevel = sample[numSamples-1].level;
5946  else
5947  newLevel = reportedLevel;
5948 
5949  report[emit].seqno = seqno;
5950  report[emit].flags =
5952  report[emit].tick = eTick;
5953  report[emit].level = newLevel;
5954 
5955  emit++;
5956  seqno++;
5957  }
5958  }
5959  }
5960 
5961  if (!emit)
5962  {
5963  if ((int)(eTick - gpioNotify[n].lastReportTick) > 60000000)
5964  {
5965  if (numSamples)
5966  newLevel = sample[numSamples-1].level;
5967  else
5968  newLevel = reportedLevel;
5969 
5970  report[emit].seqno = seqno;
5971  report[emit].flags = PI_NTFY_FLAGS_ALIVE;
5972  report[emit].tick = eTick;
5973  report[emit].level = newLevel;
5974 
5975  emit++;
5976  seqno++;
5977  }
5978  }
5979 
5980  if (emit)
5981  {
5982  DBG(DBG_FAST_TICK, "notification %d (%d reports, %x-%x)",
5983  n, emit, report[0].seqno, report[emit-1].seqno);
5984  gpioNotify[n].lastReportTick = eTick;
5985  max_emits = gpioNotify[n].max_emits;
5986 
5987  if (emit > gpioStats.maxEmit) gpioStats.maxEmit = emit;
5988 
5989  emitted = 0;
5990 
5991  while (emit > 0)
5992  {
5993  if (emit > max_emits)
5994  {
5995  gpioStats.emitFrags++;
5996 
5997  err = write(gpioNotify[n].fd,
5998  report+emitted,
5999  max_emits*sizeof(gpioReport_t));
6000 
6001  if (err != (max_emits*sizeof(gpioReport_t)))
6002  {
6003  if (err < 0)
6004  {
6005  if ((errno != EAGAIN) && (errno != EWOULDBLOCK))
6006  {
6007  /* serious error, no point continuing */
6008 
6009  DBG(DBG_ALWAYS, "fd=%d err=%d errno=%d",
6010  gpioNotify[n].fd, err, errno);
6011 
6012  DBG(DBG_ALWAYS, "%s", strerror(errno));
6013 
6014  gpioNotify[n].bits = 0;
6015  gpioNotify[n].state = PI_NOTIFY_CLOSING;
6016  intNotifyBits();
6017  break;
6018  }
6019  else gpioStats.wouldBlockPipeWrite++;
6020  }
6021  else
6022  {
6023  gpioStats.shortPipeWrite++;
6024  DBG(DBG_ALWAYS, "emitted %d, asked for %d",
6025  err/sizeof(gpioReport_t), max_emits);
6026  }
6027  }
6028  else
6029  {
6030  gpioStats.goodPipeWrite++;
6031  }
6032 
6033  emitted += max_emits;
6034  emit -= max_emits;
6035  }
6036  else
6037  {
6038  err = write(gpioNotify[n].fd,
6039  report+emitted,
6040  emit*sizeof(gpioReport_t));
6041 
6042  if (err != (emit*sizeof(gpioReport_t)))
6043  {
6044  if (err < 0)
6045  {
6046  if ((errno != EAGAIN) && (errno != EWOULDBLOCK))
6047  {
6048  DBG(DBG_ALWAYS, "fd=%d err=%d errno=%d",
6049  gpioNotify[n].fd, err, errno);
6050 
6051  DBG(DBG_ALWAYS, "%s", strerror(errno));
6052 
6053  /* serious error, no point continuing */
6054  gpioNotify[n].bits = 0;
6055  gpioNotify[n].state = PI_NOTIFY_CLOSING;
6056  intNotifyBits();
6057  break;
6058  }
6059  else gpioStats.wouldBlockPipeWrite++;
6060  }
6061  else
6062  {
6063  gpioStats.shortPipeWrite++;
6064  DBG(DBG_ALWAYS, "emitted %d, asked for %d",
6065  err/sizeof(gpioReport_t), emit);
6066  }
6067  }
6068  else
6069  {
6070  gpioStats.goodPipeWrite++;
6071  }
6072 
6073  emitted += emit;
6074  emit = 0;
6075  }
6076  }
6077 
6078  gpioNotify[n].seqno = seqno;
6079  }
6080  }
6081  }
6082 
6083  if (changedBits & scriptBits)
6084  {
6085  for (n=0; n<PI_MAX_SCRIPTS; n++)
6086  {
6087  if ((gpioScript[n].state == PI_SCRIPT_IN_USE) &&
6088  (gpioScript[n].run_state == PI_SCRIPT_WAITING) &&
6089  (gpioScript[n].waitBits & changedBits))
6090  {
6091  pthread_mutex_lock(&gpioScript[n].pthMutex);
6092 
6093  if (gpioScript[n].run_state == PI_SCRIPT_WAITING)
6094  {
6095  gpioScript[n].changedBits =
6096  gpioScript[n].waitBits & changedBits;
6097  pthread_cond_signal(&gpioScript[n].pthCond);
6098  }
6099 
6100  pthread_mutex_unlock(&gpioScript[n].pthMutex);
6101  }
6102  }
6103  }
6104 
6105  if (eventBits & scriptEventBits)
6106  {
6107  for (n=0; n<PI_MAX_SCRIPTS; n++)
6108  {
6109  if ((gpioScript[n].state == PI_SCRIPT_IN_USE) &&
6110  (gpioScript[n].run_state == PI_SCRIPT_WAITING) &&
6111  (gpioScript[n].eventBits & eventBits))
6112  {
6113  pthread_mutex_lock(&gpioScript[n].pthMutex);
6114 
6115  if (gpioScript[n].run_state == PI_SCRIPT_WAITING)
6116  {
6117  gpioScript[n].changedBits =
6118  gpioScript[n].eventBits & eventBits;
6119  pthread_cond_signal(&gpioScript[n].pthCond);
6120  }
6121 
6122  pthread_mutex_unlock(&gpioScript[n].pthMutex);
6123  }
6124  }
6125  }
6126 
6127  if (numSamples) reportedLevel = sample[numSamples-1].level;
6128 }
6129 
6130 static void alertWdogCheck(gpioSample_t *sample, int numSamples)
6131 {
6132  /*
6133  Go through and set the last time each GPIO with a watchdog changed state.
6134  */
6135 
6136  int i, j;
6137  uint32_t LBitV;
6138  uint32_t bit;
6139 
6140  for (i=0; i<=PI_MAX_USER_GPIO; i++)
6141  {
6142  bit = (1<<i);
6143 
6144  if (monitorBits & bit & wdogBits)
6145  {
6146  LBitV = gpioAlert[i].wdLBitV;
6147 
6148  for (j=0; j<numSamples; j++)
6149  {
6150  if ((sample[j].level & bit) != LBitV)
6151  {
6152  LBitV = sample[j].level & bit;
6153  gpioAlert[i].wdTick = sample[j].tick;
6154  }
6155  }
6156 
6157  gpioAlert[i].wdLBitV = LBitV;
6158  }
6159  }
6160 }
6161 
6162 static void * pthAlertThread(void *x)
6163 {
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;
6170  int cycle, pulse;
6171  int numSamples, ticks, i;
6172  int rp, reports, totalSamples;
6173  int stopped;
6174  int moreToDo;
6175  gpioSample_t sample[MAX_SAMPLE];
6176 
6177  req.tv_sec = 0;
6178 
6179  /* don't start until DMA started */
6180 
6182 
6184 
6185  oldLevel = reportedLevel;
6186 
6187  oldSlot = dmaCurrentSlot(dmaNowAtICB());
6188 
6189  oldSlot = (oldSlot / PULSE_PER_CYCLE) * PULSE_PER_CYCLE;
6190 
6191  cycle = (oldSlot/PULSE_PER_CYCLE);
6192 
6193  pulse = 0;
6194 
6195  stopped = 0;
6196 
6197  moreToDo = 0;
6198 
6199  stickInited = 0;
6200 
6201  sTick = 0;
6202 
6203  minDiff = gpioCfg.clockMicros / 2;
6204 
6205  while (1)
6206  {
6207  /* Check that DMA is running okay */
6208 
6209  if (dmaIn[DMA_CONBLK_AD])
6210  {
6211  if (stopped)
6212  {
6213  DBG(DBG_STARTUP, "****** GOING ******");
6214  stopped = 0;
6215  }
6216  }
6217  else
6218  {
6219  stopped = 1;
6220 
6221  myGpioDelay(5000);
6222 
6223  if (runState == PI_RUNNING)
6224  {
6225  /* should never be executed, leave code just in case */
6226 
6227  gpioCfg.internals |= PI_CFG_STATS;
6228 
6229  dmaInitCbs();
6230  flushMemory();
6231  initDMAgo((uint32_t *)dmaIn, (uint32_t)dmaIBus[0]);
6232  myGpioDelay(5000); /* let DMA run for a while */
6233  oldSlot = dmaCurrentSlot(dmaNowAtICB());
6234  gpioStats.DMARestarts++;
6235  }
6236  }
6237 
6238  newSlot = dmaCurrentSlot(dmaNowAtICB());
6239 
6240  newSlot = (newSlot / PULSE_PER_CYCLE) * PULSE_PER_CYCLE;
6241 
6242  numSamples = 0;
6243 
6244  /*
6245  Extract samples from DMA ring buffer.
6246  */
6247 
6248  while ((oldSlot != newSlot) && (numSamples < MAX_SAMPLE))
6249  {
6250  level = myGetLevel(oldSlot++);
6251 
6252  sample[numSamples].tick = sTick;
6253  sample[numSamples].level = level;
6254 
6255  numSamples++;
6256 
6257  sTick += gpioCfg.clockMicros;
6258 
6259  if (++pulse >= PULSE_PER_CYCLE)
6260  {
6261  pulse = 0;
6262 
6263  if (++cycle >= bufferCycles)
6264  {
6265  cycle = 0;
6266  oldSlot = 0;
6267  }
6268 
6269  expected = sTick;
6270 
6271  sTick = myGetTick(cycle);
6272 
6273  if (stickInited)
6274  {
6275  diff = sTick - expected;
6276 
6277  if (abs(diff) > minDiff)
6278  {
6279  ft = sample[numSamples-PULSE_PER_CYCLE].tick;
6280 
6281  ticks = sTick - ft;
6282 
6283  for (i=1; i<PULSE_PER_CYCLE; i++)
6284  {
6285  sample[numSamples-PULSE_PER_CYCLE+i].tick =
6286  ((i*ticks)/PULSE_PER_CYCLE) + ft;
6287  }
6288  }
6289 
6290  diff += (TICKSLOTS/2);
6291 
6292  if (diff < 0)
6293  {
6294  gpioStats.diffTick[0]++;
6295  }
6296 
6297  else if (diff >= TICKSLOTS)
6298  {
6299  gpioStats.diffTick[TICKSLOTS-1]++;
6300  }
6301 
6302  else gpioStats.diffTick[diff]++;
6303  }
6304  else
6305  {
6306  stickInited = 1;
6307  numSamples = 0;
6308  if (!(gpioCfg.ifFlags & PI_DISABLE_ALERT))
6309  {
6311  }
6312  }
6313  }
6314  }
6315 
6316  if (oldSlot == newSlot) moreToDo = 0; else moreToDo = 1;
6317 
6318  /* Apply glitch filter */
6319 
6320  if (numSamples && gFilterBits) alertGlitchFilter(sample, numSamples);
6321 
6322  /* Apply noise filter */
6323 
6324  if (numSamples && nFilterBits) alertNoiseFilter(sample, numSamples);
6325 
6326  /* Compact samples */
6327 
6328  changedBits = 0;
6329  oldLevel &= monitorBits;
6330  reports = 0;
6331  totalSamples = 0;
6332 
6333  for (rp=0; rp<numSamples; rp++)
6334  {
6335  newLevel = (sample[rp].level & monitorBits);
6336 
6337  if (newLevel != oldLevel)
6338  {
6339  sample[reports].tick = sample[rp].tick;
6340  sample[reports].level = sample[rp].level;
6341  changedBits |= (newLevel ^ oldLevel);
6342  oldLevel = newLevel;
6343 
6344  reports++;
6345 
6346  if (reports >= MAX_REPORT)
6347  {
6348  totalSamples += reports;
6349 
6350  /* Rebase watchdog timeouts */
6351  if (wdogBits) alertWdogCheck(sample, reports);
6352 
6353  gpioStats.numSamples += reports;
6354 
6355  alertEmit(sample, reports, changedBits, sample[rp].tick);
6356 
6357  changedBits = 0;
6358  reports = 0;
6359  }
6360  }
6361  }
6362 
6363  if (reports)
6364  {
6365  totalSamples += reports;
6366 
6367  /* Rebase watchdog timeouts */
6368  if (wdogBits) alertWdogCheck(sample, reports);
6369 
6370  gpioStats.numSamples += reports;
6371  }
6372 
6373  alertEmit(sample, reports, changedBits, sTick);
6374 
6375  if (totalSamples > gpioStats.maxSamples)
6376  gpioStats.maxSamples = numSamples;
6377 
6378  req.tv_sec = 0;
6379  req.tv_nsec = alert_delays[(gpioCfg.internals>>PI_CFG_ALERT_FREQ)&15];
6380 
6381  if (moreToDo)
6382  {
6383  gpioStats.moreToDo++;
6384  }
6385  else
6386  {
6387  gpioStats.alertTicks++;
6388 
6389  while (nanosleep(&req, &rem))
6390  {
6391  req.tv_sec = rem.tv_sec;
6392  req.tv_nsec = rem.tv_nsec;
6393  }
6394  }
6395  }
6396 
6397  return 0;
6398 }
6399 
6400 /* ======================================================================= */
6401 
6402 static int scrPop(gpioScript_t *s, int *SP, int *S)
6403 {
6404  if ((*SP) > 0)
6405  {
6406  return S[--(*SP)];
6407  }
6408  else
6409  {
6411  DBG(DBG_ALWAYS, "script %d too many pops", s->id);
6412  return 0;
6413  }
6414 }
6415 
6416 /* ----------------------------------------------------------------------- */
6417 
6418 static void scrPush(gpioScript_t *s, int *SP, int *S, int val)
6419 {
6420  if ((*SP) < PI_SCRIPT_STACK_SIZE)
6421  {
6422  S[(*SP)++] = val;
6423  }
6424  else
6425  {
6427  DBG(DBG_ALWAYS, "script %d too many pushes", s->id);
6428  }
6429 }
6430 
6431 /* ----------------------------------------------------------------------- */
6432 
6433 static void scrSwap(int *v1, int *v2)
6434 {
6435  int t;
6436 
6437  t=*v1; *v1=*v2; *v2= t;
6438 }
6439 
6440 /* ----------------------------------------------------------------------- */
6441 
6442 static int scrEvtWait(gpioScript_t *s, uint32_t bits)
6443 {
6444  pthread_mutex_lock(&s->pthMutex);
6445 
6446  if (s->request == PI_SCRIPT_RUN)
6447  {
6449  s->eventBits = bits;
6451 
6452  pthread_cond_wait(&s->pthCond, &s->pthMutex);
6453 
6454  s->waitBits = 0;
6457  }
6458 
6459  pthread_mutex_unlock(&s->pthMutex);
6460 
6461  return s->changedBits;
6462 }
6463 
6464 /* ----------------------------------------------------------------------- */
6465 
6466 static int scrWait(gpioScript_t *s, uint32_t bits)
6467 {
6468  pthread_mutex_lock(&s->pthMutex);
6469 
6470  if (s->request == PI_SCRIPT_RUN)
6471  {
6473  s->waitBits = bits;
6474  intScriptBits();
6475 
6476  pthread_cond_wait(&s->pthCond, &s->pthMutex);
6477 
6478  s->waitBits = 0;
6479  intScriptBits();
6481  }
6482 
6483  pthread_mutex_unlock(&s->pthMutex);
6484 
6485  return s->changedBits;
6486 }
6487 
6488 /* ----------------------------------------------------------------------- */
6489 
6490 static int scrSys(char *cmd, uint32_t p1, uint32_t p2)
6491 {
6492  char buf[1024];
6493  int status;
6494 
6495  if (!myScriptNameValid(cmd))
6496  SOFT_ERROR(PI_BAD_SCRIPT_NAME, "bad script name (%s)", cmd);
6497 
6498  snprintf(buf, sizeof(buf), "/opt/pigpio/cgi/%s %u %u", cmd, p1, p2);
6499 
6500  DBG(DBG_USER, "%s", buf);
6501 
6502  status = system(buf);
6503 
6504  if (status < 0) status = PI_BAD_SHELL_STATUS;
6505 
6506  return status;
6507 }
6508 
6509 /* ----------------------------------------------------------------------- */
6510 
6511 static void *pthScript(void *x)
6512 {
6513  gpioScript_t *s;
6514  cmdInstr_t instr;
6515  int p1, p2, p1o, p2o, p3o, *t1, *t2;
6516  int PC, A, F, SP;
6517  int S[PI_SCRIPT_STACK_SIZE];
6518  char buf[CMD_MAX_EXTENSION];
6519 
6520 
6521  S[0] = 0; /* to prevent compiler warning */
6522 
6523  s = x;
6524 
6525  while ((volatile int)s->request != PI_SCRIPT_DELETE)
6526  {
6527  pthread_mutex_lock(&s->pthMutex);
6529  pthread_cond_wait(&s->pthCond, &s->pthMutex);
6530  pthread_mutex_unlock(&s->pthMutex);
6531 
6533 
6534  A = 0;
6535  F = 0;
6536  PC = 0;
6537  SP = 0;
6538 
6539  while (((volatile int)s->request == PI_SCRIPT_RUN ) &&
6540  (s->run_state == PI_SCRIPT_RUNNING))
6541  {
6542  instr = s->script.instr[PC];
6543 
6544  p1o = instr.p[1];
6545  p2o = instr.p[2];
6546 
6547  if (instr.opt[1] == CMD_VAR) instr.p[1] = s->script.var[p1o];
6548  else if (instr.opt[1] == CMD_PAR) instr.p[1] = s->script.par[p1o];
6549 
6550  if (instr.opt[2] == CMD_VAR) instr.p[2] = s->script.var[p2o];
6551  else if (instr.opt[2] == CMD_PAR) instr.p[2] = s->script.par[p2o];
6552 /*
6553  fprintf(stderr, "PC=%d cmd=%d p1o=%d p1=%d p2o=%d p2=%d\n",
6554  PC, instr.p[0], p1o, instr.p[1], p2o, instr.p[2]);
6555  fflush(stderr);
6556 */
6557  if (instr.p[0] < PI_CMD_SCRIPT)
6558  {
6559  if (instr.p[3])
6560  {
6561  if ((instr.p[3] == sizeof(int)) && ((instr.opt[3] == CMD_VAR) || (instr.opt[3] == CMD_PAR)))
6562  {
6563  /* Hack to allow register use in 3rd parameter */
6564  memcpy((char*)&p3o, (char *)instr.p[4], sizeof(int));
6565  if (instr.opt[3] == CMD_VAR) memcpy(buf, (char *)&(s->script.var[p3o]), sizeof(int));
6566  else memcpy(buf, (char *)&(s->script.par[p3o]), sizeof(int));
6567  }
6568  else
6569  {
6570  memcpy(buf, (char *)instr.p[4], instr.p[3]);
6571  }
6572  }
6573 
6574  A = myDoCommand(instr.p, sizeof(buf)-1, buf);
6575 
6576  F = A;
6577 
6578  PC++;
6579  }
6580  else
6581  {
6582  p1 = instr.p[1];
6583  p2 = instr.p[2];
6584 
6585  switch (instr.p[0])
6586  {
6587  case PI_CMD_ADD: A+=p1; F=A; PC++; break;
6588 
6589  case PI_CMD_AND: A&=p1; F=A; PC++; break;
6590 
6591  case PI_CMD_CALL: scrPush(s, &SP, S, PC+1); PC = p1; break;
6592 
6593  case PI_CMD_CMP: F=A-p1; PC++; break;
6594 
6595  case PI_CMD_DCR:
6596  if (instr.opt[1] == CMD_PAR)
6597  {--s->script.par[p1o]; F=s->script.par[p1o];}
6598  else
6599  {--s->script.var[p1o]; F=s->script.var[p1o];}
6600  PC++;
6601  break;
6602 
6603  case PI_CMD_DCRA: --A; F=A; PC++; break;
6604 
6605  case PI_CMD_DIV: A/=p1; F=A; PC++; break;
6606 
6607  case PI_CMD_HALT: s->run_state = PI_SCRIPT_HALTED; break;
6608 
6609  case PI_CMD_EVTWT: A=scrEvtWait(s, p1); F=A; PC++; break;
6610 
6611  case PI_CMD_INR:
6612  if (instr.opt[1] == CMD_PAR)
6613  {++s->script.par[p1o]; F=s->script.par[p1o];}
6614  else
6615  {++s->script.var[p1o]; F=s->script.var[p1o];}
6616  PC++;
6617  break;
6618 
6619  case PI_CMD_INRA: ++A; F=A; PC++; break;
6620 
6621  case PI_CMD_JM: if (F<0) PC=p1; else PC++; break;
6622 
6623  case PI_CMD_JMP: PC=p1; break;
6624 
6625  case PI_CMD_JNZ: if (F) PC=p1; else PC++; break;
6626 
6627  case PI_CMD_JP: if (F>=0) PC=p1; else PC++; break;
6628 
6629  case PI_CMD_JZ: if (!F) PC=p1; else PC++; break;
6630 
6631  case PI_CMD_LD:
6632  if (instr.opt[1] == CMD_PAR) s->script.par[p1o]=p2;
6633  else s->script.var[p1o]=p2;
6634  PC++;
6635  break;
6636 
6637  case PI_CMD_LDA: A=p1; PC++; break;
6638 
6639  case PI_CMD_LDAB:
6640  if ((p1 >= 0) && (p1 < sizeof(buf))) A = buf[p1];
6641  PC++;
6642  break;
6643 
6644  case PI_CMD_MLT: A*=p1; F=A; PC++; break;
6645 
6646  case PI_CMD_MOD: A%=p1; F=A; PC++; break;
6647 
6648  case PI_CMD_OR: A|=p1; F=A; PC++; break;
6649 
6650  case PI_CMD_POP:
6651  if (instr.opt[1] == CMD_PAR)
6652  s->script.par[p1o]=scrPop(s, &SP, S);
6653  else
6654  s->script.var[p1o]=scrPop(s, &SP, S);
6655  PC++;
6656  break;
6657 
6658  case PI_CMD_POPA: A=scrPop(s, &SP, S); PC++; break;
6659 
6660  case PI_CMD_PUSH:
6661  if (instr.opt[1] == CMD_PAR)
6662  scrPush(s, &SP, S, s->script.par[p1o]);
6663  else
6664  scrPush(s, &SP, S, s->script.var[p1o]);
6665  PC++;
6666  break;
6667 
6668  case PI_CMD_PUSHA: scrPush(s, &SP, S, A); PC++; break;
6669 
6670  case PI_CMD_RET: PC=scrPop(s, &SP, S); break;
6671 
6672  case PI_CMD_RL:
6673  if (instr.opt[1] == CMD_PAR)
6674  {s->script.par[p1o]<<=p2; F=s->script.par[p1o];}
6675  else
6676  {s->script.var[p1o]<<=p2; F=s->script.var[p1o];}
6677  PC++;
6678  break;
6679 
6680  case PI_CMD_RLA: A<<=p1; F=A; PC++; break;
6681 
6682  case PI_CMD_RR:
6683  if (instr.opt[1] == CMD_PAR)
6684  {s->script.par[p1o]>>=p2; F=s->script.par[p1o];}
6685  else
6686  {s->script.var[p1o]>>=p2; F=s->script.var[p1o];}
6687  PC++;
6688  break;
6689 
6690  case PI_CMD_RRA: A>>=p1; F=A; PC++; break;
6691 
6692  case PI_CMD_STA:
6693  if (instr.opt[1] == CMD_PAR) s->script.par[p1o]=A;
6694  else s->script.var[p1o]=A;
6695  PC++;
6696  break;
6697 
6698  case PI_CMD_STAB:
6699  if ((p1 >= 0) && (p1 < sizeof(buf))) buf[p1] = A;
6700  PC++;
6701  break;
6702 
6703  case PI_CMD_SUB: A-=p1; F=A; PC++; break;
6704 
6705  case PI_CMD_SYS:
6706  A=scrSys((char*)instr.p[4], A, *(gpioReg + GPLEV0));
6707  F=A;
6708  PC++;
6709  break;
6710 
6711  case PI_CMD_WAIT: A=scrWait(s, p1); F=A; PC++; break;
6712 
6713  case PI_CMD_X:
6714  if (instr.opt[1] == CMD_PAR) t1 = &s->script.par[p1o];
6715  else t1 = &s->script.var[p1o];
6716 
6717  if (instr.opt[2] == CMD_PAR) t2 = &s->script.par[p2o];
6718  else t2 = &s->script.var[p2o];
6719 
6720  scrSwap(t1, t2);
6721  PC++;
6722  break;
6723 
6724  case PI_CMD_XA:
6725  if (instr.opt[1] == CMD_PAR)
6726  scrSwap(&s->script.par[p1o], &A);
6727  else
6728  scrSwap(&s->script.var[p1o], &A);
6729  PC++;
6730  break;
6731 
6732  case PI_CMD_XOR: A^=p1; F=A; PC++; break;
6733 
6734  }
6735  }
6736 
6737  if (PC >= s->script.instrs) s->run_state = PI_SCRIPT_HALTED;
6738 
6739  }
6740 
6741  if ((volatile int)s->request == PI_SCRIPT_HALT)
6743 
6744  }
6745 
6746  return 0;
6747 }
6748 
6749 /* ----------------------------------------------------------------------- */
6750 
6751 static void * pthTimerTick(void *x)
6752 {
6753  gpioTimer_t *tp;
6754  struct timespec req, rem;
6755 
6756  tp = x;
6757 
6758  while (1)
6759  {
6760  req.tv_sec = tp->millis / THOUSAND;
6761  req.tv_nsec = (tp->millis % THOUSAND) * THOUSAND * THOUSAND;
6762 
6763  while (nanosleep(&req, &rem))
6764  {
6765  req.tv_sec = rem.tv_sec;
6766  req.tv_nsec = rem.tv_nsec;
6767  }
6768 
6769  if (tp->ex) (tp->func)(tp->userdata);
6770  else (tp->func)();
6771  }
6772 
6773  return 0;
6774 }
6775 
6776 /* ----------------------------------------------------------------------- */
6777 
6778 
6779 static void * pthFifoThread(void *x)
6780 {
6781  char buf[CMD_MAX_EXTENSION];
6782  int idx, flags, len, res, i;
6783  uint32_t p[CMD_P_ARR];
6784  cmdCtlParse_t ctl;
6785  uint32_t *param;
6786  char v[CMD_MAX_EXTENSION];
6787 
6788  myCreatePipe(PI_INPFIFO, 0662);
6789 
6790  if ((inpFifo = fopen(PI_INPFIFO, "r+")) == NULL)
6791  SOFT_ERROR((void*)PI_INIT_FAILED, "fopen %s failed(%m)", PI_INPFIFO);
6792 
6793  myCreatePipe(PI_OUTFIFO, 0664);
6794 
6795  if ((outFifo = fopen(PI_OUTFIFO, "w+")) == NULL)
6796  SOFT_ERROR((void*)PI_INIT_FAILED, "fopen %s failed (%m)", PI_OUTFIFO);
6797 
6798  /* set outFifo non-blocking */
6799 
6800  flags = fcntl(fileno(outFifo), F_GETFL, 0);
6801  fcntl(fileno(outFifo), F_SETFL, flags | O_NONBLOCK);
6802 
6803  /* don't start until DMA started */
6804 
6806 
6807  while (1)
6808  {
6809  if (fgets(buf, sizeof(buf), inpFifo) == NULL)
6810  SOFT_ERROR((void*)PI_INIT_FAILED, "fifo fgets failed (%m)");
6811 
6812  len = strlen(buf);
6813 
6814  if (len)
6815  {
6816  --len;
6817  buf[len] = 0; /* replace terminating */
6818  }
6819 
6820  ctl.eaten = 0;
6821  idx = 0;
6822 
6823  while (((ctl.eaten)<len) && (idx >= 0))
6824  {
6825  if ((idx=cmdParse(buf, p, CMD_MAX_EXTENSION, v, &ctl)) >= 0)
6826  {
6827  /* make sure extensions are null terminated */
6828 
6829  v[p[3]] = 0;
6830 
6831  res = myDoCommand(p, sizeof(v)-1, v);
6832 
6833  switch (cmdInfo[idx].rv)
6834  {
6835  case 0:
6836  fprintf(outFifo, "%d\n", res);
6837  break;
6838 
6839  case 1:
6840  fprintf(outFifo, "%d\n", res);
6841  break;
6842 
6843  case 2:
6844  fprintf(outFifo, "%d\n", res);
6845  break;
6846 
6847  case 3:
6848  fprintf(outFifo, "%08X\n", res);
6849  break;
6850 
6851  case 4:
6852  fprintf(outFifo, "%u\n", res);
6853  break;
6854 
6855  case 5:
6856  fprintf(outFifo, "%s", cmdUsage);
6857  break;
6858 
6859  case 6:
6860  fprintf(outFifo, "%d", res);
6861  if (res > 0)
6862  {
6863  for (i=0; i<res; i++)
6864  {
6865  fprintf(outFifo, " %d", v[i]);
6866  }
6867  }
6868  fprintf(outFifo, "\n");
6869  break;
6870 
6871  case 7:
6872  if (res < 0) fprintf(outFifo, "%d\n", res);
6873  else
6874  {
6875  fprintf(outFifo, "%d", res);
6876  param = (uint32_t *)v;
6877  for (i=0; i<PI_MAX_SCRIPT_PARAMS; i++)
6878  {
6879  fprintf(outFifo, " %d", param[i]);
6880  }
6881  fprintf(outFifo, "\n");
6882  }
6883  break;
6884  }
6885  }
6886  else fprintf(outFifo, "%d\n", PI_BAD_FIFO_COMMAND);
6887  }
6888 
6889  fflush(outFifo);
6890  }
6891 
6892  return 0;
6893 }
6894 
6895 /* ----------------------------------------------------------------------- */
6896 
6897 static void *pthSocketThreadHandler(void *fdC)
6898 {
6899  int sock = *(int*)fdC;
6900  uint32_t p[10];
6901  int opt;
6902  char buf[CMD_MAX_EXTENSION];
6903 
6904  free(fdC);
6905 
6906  /* Disable the Nagle algorithm. */
6907  opt = 1;
6908  setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (char*)&opt, sizeof(int));
6909 
6910  while (1)
6911  {
6912  if (recv(sock, p, 16, MSG_WAITALL) != 16) break;
6913 
6914  if (p[3])
6915  {
6916  if (p[3] < sizeof(buf))
6917  {
6918  /* read extension into buf */
6919  if (recv(sock, buf, p[3], MSG_WAITALL) != p[3])
6920  {
6921  /* Serious error. No point continuing. */
6922  DBG(DBG_ALWAYS,
6923  "recv failed for %d bytes, sock=%d", p[3], sock);
6924 
6925  closeOrphanedNotifications(-1, sock);
6926 
6927  close(sock);
6928 
6929  return 0;
6930  }
6931  }
6932  else
6933  {
6934  /* Serious error. No point continuing. */
6935  DBG(DBG_ALWAYS,
6936  "ext too large %d(%d), sock=%d", p[3], sizeof(buf), sock);
6937 
6938  closeOrphanedNotifications(-1, sock);
6939 
6940  close(sock);
6941 
6942  return 0;
6943  }
6944  }
6945 
6946  /* add null terminator in case it's a string */
6947 
6948  buf[p[3]] = 0;
6949 
6950  switch (p[0])
6951  {
6952  case PI_CMD_NOIB:
6953 
6954  p[3] = gpioNotifyOpenInBand(sock);
6955 
6956  /* Enable the Nagle algorithm. */
6957  opt = 0;
6958  setsockopt(
6959  sock, IPPROTO_TCP, TCP_NODELAY, (char*)&opt, sizeof(int));
6960 
6961  break;
6962 
6963  case PI_CMD_PROCP:
6964  p[3] = myDoCommand(p, sizeof(buf)-1, buf+sizeof(int));
6965  if (((int)p[3]) >= 0)
6966  {
6967  memcpy(buf, &p[3], 4);
6968  p[3] = 4 + (4*PI_MAX_SCRIPT_PARAMS);
6969  }
6970  break;
6971 
6972  default:
6973  p[3] = myDoCommand(p, sizeof(buf)-1, buf);
6974  }
6975 
6976  if (write(sock, p, 16) == -1) { /* ignore errors */ }
6977 
6978  switch (p[0])
6979  {
6980  /* extensions */
6981 
6982  case PI_CMD_BI2CZ:
6983  case PI_CMD_BSCX:
6984  case PI_CMD_CF2:
6985  case PI_CMD_FL:
6986  case PI_CMD_FR:
6987  case PI_CMD_I2CPK:
6988  case PI_CMD_I2CRD:
6989  case PI_CMD_I2CRI:
6990  case PI_CMD_I2CRK:
6991  case PI_CMD_I2CZ:
6992  case PI_CMD_PROCP:
6993  case PI_CMD_SERR:
6994  case PI_CMD_SLR:
6995  case PI_CMD_SPIX:
6996  case PI_CMD_SPIR:
6997  case PI_CMD_BSPIX:
6998 
6999  if (((int)p[3]) > 0)
7000  {
7001  if (write(sock, buf, p[3]) == 1) { /* ignore errors */ }
7002  }
7003  break;
7004 
7005  default:
7006  break;
7007  }
7008  }
7009 
7010  closeOrphanedNotifications(-1, sock);
7011 
7012  close(sock);
7013 
7014  return 0;
7015 }
7016 
7017 static int addrAllowed(struct sockaddr *saddr)
7018 {
7019  int i;
7020  uint32_t addr;
7021 
7022  if (!numSockNetAddr) return 1;
7023 
7024  // FIXME: add IPv6 whitelisting support
7025  if (saddr->sa_family != AF_INET) return 0;
7026 
7027  addr = ((struct sockaddr_in *) saddr)->sin_addr.s_addr;
7028 
7029  for (i=0; i<numSockNetAddr; i++)
7030  {
7031  if (addr == sockNetAddr[i]) return 1;
7032  }
7033  return 0;
7034 }
7035 
7036 /* ----------------------------------------------------------------------- */
7037 
7038 static void * pthSocketThread(void *x)
7039 {
7040  int fdC=0, c, *sock;
7041  struct sockaddr_storage client;
7042  pthread_attr_t attr;
7043 
7044  if (pthread_attr_init(&attr))
7045  SOFT_ERROR((void*)PI_INIT_FAILED,
7046  "pthread_attr_init failed (%m)");
7047 
7048  if (pthread_attr_setstacksize(&attr, STACK_SIZE))
7049  SOFT_ERROR((void*)PI_INIT_FAILED,
7050  "pthread_attr_setstacksize failed (%m)");
7051 
7052  if (pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED))
7053  SOFT_ERROR((void*)PI_INIT_FAILED,
7054  "pthread_attr_setdetachstate failed (%m)");
7055 
7056  /* fdSock opened in gpioInitialise so that we can treat
7057  failure to bind as fatal. */
7058 
7059  listen(fdSock, 100);
7060 
7061  c = sizeof(client);
7062 
7063  /* don't start until DMA started */
7064 
7066 
7067  while (fdC >= 0)
7068  {
7069  pthread_t thr;
7070 
7071  fdC = accept(fdSock, (struct sockaddr *)&client, (socklen_t*)&c);
7072 
7073  closeOrphanedNotifications(-1, fdC);
7074 
7075  if (addrAllowed((struct sockaddr *)&client))
7076  {
7077  sock = malloc(sizeof(int));
7078 
7079  *sock = fdC;
7080 
7081  if (pthread_create
7082  (&thr, &attr, pthSocketThreadHandler, (void*) sock) < 0)
7083  SOFT_ERROR((void*)PI_INIT_FAILED,
7084  "socket pthread_create failed (%m)");
7085  }
7086  else
7087  {
7088  close(fdC);
7089  }
7090  }
7091 
7092  if (fdC < 0)
7093  SOFT_ERROR((void*)PI_INIT_FAILED, "accept failed (%m)");
7094 
7095  return 0;
7096 }
7097 
7098 /* ======================================================================= */
7099 
7100 static void initCheckLockFile(void)
7101 {
7102  int fd;
7103  int count;
7104  int pid;
7105  int err;
7106  int delete;
7107  char str[20];
7108 
7109  fd = open(PI_LOCKFILE, O_RDONLY);
7110 
7111  if (fd != -1)
7112  {
7113  DBG(DBG_STARTUP, "lock file exists");
7114  delete = 1;
7115 
7116  count = read(fd, str, sizeof(str)-1);
7117 
7118  if (count)
7119  {
7120  pid = atoi(str);
7121  err = kill(pid, 0);
7122  if (!err) delete = 0; /* process still exists */
7123  DBG(DBG_STARTUP, "lock file pid=%d err=%d", pid, err);
7124  }
7125 
7126  close(fd);
7127  DBG(DBG_STARTUP, "lock file delete=%d", delete);
7128 
7129  if (delete) unlink(PI_LOCKFILE);
7130  }
7131 }
7132 
7133 static int initGrabLockFile(void)
7134 {
7135  int fd;
7136  int lockResult;
7137  char pidStr[20];
7138 
7140 
7141  /* try to grab the lock file */
7142 
7143  fd = open(PI_LOCKFILE, O_WRONLY|O_CREAT|O_EXCL|O_TRUNC, 0644);
7144 
7145  if (fd != -1)
7146  {
7147  lockResult = flock(fd, LOCK_EX|LOCK_NB);
7148 
7149  if(lockResult == 0)
7150  {
7151  sprintf(pidStr, "%d\n", (int)getpid());
7152 
7153  if (write(fd, pidStr, strlen(pidStr)) == -1)
7154  {
7155  /* ignore errors */
7156  }
7157  }
7158  else
7159  {
7160  close(fd);
7161  return -1;
7162  }
7163  }
7164 
7165  return fd;
7166 }
7167 
7168 /* ----------------------------------------------------------------------- */
7169 
7170 static uint32_t * initMapMem(int fd, uint32_t addr, uint32_t len)
7171 {
7172  return (uint32_t *) mmap(0, len,
7173  PROT_READ|PROT_WRITE|PROT_EXEC,
7174  MAP_SHARED|MAP_LOCKED,
7175  fd, addr);
7176 }
7177 
7178 /* ----------------------------------------------------------------------- */
7179 
7180 static int initCheckPermitted(void)
7181 {
7182  DBG(DBG_STARTUP, "");
7183 
7184  if ((fdMem = open("/dev/mem", O_RDWR | O_SYNC) ) < 0)
7185  {
7186  DBG(DBG_ALWAYS,
7187  "\n" \
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");
7192  return -1;
7193  }
7194  return 0;
7195 }
7196 
7197 /* ----------------------------------------------------------------------- */
7198 
7199 static int initPeripherals(void)
7200 {
7201  uint32_t dmaBase;
7202 
7203  DBG(DBG_STARTUP, "");
7204 
7206 
7207  if (gpioReg == MAP_FAILED)
7208  SOFT_ERROR(PI_INIT_FAILED, "mmap gpio failed (%m)");
7209 
7210  /* dma channels 0-14 share one page, 15 has another */
7211 
7212  if (gpioCfg.DMAprimaryChannel < 15)
7213  {
7214  dmaBase = DMA_BASE;
7215  }
7216  else dmaBase = DMA15_BASE;
7217 
7218  dmaReg = initMapMem(fdMem, dmaBase, DMA_LEN);
7219 
7220  if (dmaReg == MAP_FAILED)
7221  SOFT_ERROR(PI_INIT_FAILED, "mmap dma failed (%m)");
7222 
7223  if (gpioCfg.DMAprimaryChannel < 15)
7224  {
7225  dmaIn = dmaReg + (gpioCfg.DMAprimaryChannel * 0x40);
7226  dmaOut = dmaReg + (gpioCfg.DMAsecondaryChannel * 0x40);
7227  }
7228 
7229  DBG(DBG_STARTUP, "DMA #%d @ %08X @ %08X",
7230  gpioCfg.DMAprimaryChannel, dmaBase, (uint32_t)dmaIn);
7231 
7232  DBG(DBG_STARTUP, "debug reg is %08X", dmaIn[DMA_DEBUG]);
7233 
7235 
7236  if (clkReg == MAP_FAILED)
7237  SOFT_ERROR(PI_INIT_FAILED, "mmap clk failed (%m)");
7238 
7240 
7241  if (systReg == MAP_FAILED)
7242  SOFT_ERROR(PI_INIT_FAILED, "mmap syst failed (%m)");
7243 
7245 
7246  if (spiReg == MAP_FAILED)
7247  SOFT_ERROR(PI_INIT_FAILED, "mmap spi failed (%m)");
7248 
7250 
7251  if (pwmReg == MAP_FAILED)
7252  SOFT_ERROR(PI_INIT_FAILED, "mmap pwm failed (%m)");
7253 
7255 
7256  if (pcmReg == MAP_FAILED)
7257  SOFT_ERROR(PI_INIT_FAILED, "mmap pcm failed (%m)");
7258 
7260 
7261  if (auxReg == MAP_FAILED)
7262  SOFT_ERROR(PI_INIT_FAILED, "mmap aux failed (%m)");
7263 
7265 
7266  if (padsReg == MAP_FAILED)
7267  SOFT_ERROR(PI_INIT_FAILED, "mmap pads failed (%m)");
7268 
7270 
7271  if (bscsReg == MAP_FAILED)
7272  SOFT_ERROR(PI_INIT_FAILED, "mmap bscs failed (%m)");
7273 
7274  return 0;
7275 }
7276 
7277 /* ----------------------------------------------------------------------- */
7278 
7279 static int initZaps
7280  (int pmapFd, void *virtualBase, int basePage, int pages)
7281 {
7282  int n;
7283  long index;
7284  off_t offset;
7285  ssize_t t;
7286  uint32_t physical;
7287  int status;
7288  uint32_t pageAdr;
7289  unsigned long long pa;
7290 
7291  DBG(DBG_STARTUP, "");
7292 
7293  status = 0;
7294 
7295  pageAdr = (uint32_t) dmaVirt[basePage];
7296 
7297  index = ((uint32_t)virtualBase / PAGE_SIZE) * 8;
7298 
7299  offset = lseek(pmapFd, index, SEEK_SET);
7300 
7301  if (offset != index)
7302  SOFT_ERROR(PI_INIT_FAILED, "lseek pagemap failed (%m)");
7303 
7304  for (n=0; n<pages; n++)
7305  {
7306  t = read(pmapFd, &pa, sizeof(pa));
7307 
7308  if (t != sizeof(pa))
7309  SOFT_ERROR(PI_INIT_FAILED, "read pagemap failed (%m)");
7310 
7311  DBG(DBG_STARTUP, "pf%d=%016llX", n, pa);
7312 
7313  physical = 0x3FFFFFFF & (PAGE_SIZE * (pa & 0xFFFFFFFF));
7314 
7315  if (physical)
7316  {
7317  dmaBus[basePage+n] = (dmaPage_t *) (physical | pi_dram_bus);
7318 
7319  dmaVirt[basePage+n] = mmap
7320  (
7321  (void *)pageAdr,
7322  PAGE_SIZE,
7323  PROT_READ|PROT_WRITE,
7324  MAP_SHARED|MAP_FIXED|MAP_LOCKED|MAP_NORESERVE,
7325  fdMem,
7326  physical
7327  );
7328  }
7329  else status = 1;
7330 
7331  pageAdr += PAGE_SIZE;
7332  }
7333 
7334  return status;
7335 }
7336 
7337 /* ----------------------------------------------------------------------- */
7338 
7339 static int initPagemapBlock(int block)
7340 {
7341  int trys, ok;
7342  unsigned pageNum;
7343 
7344  DBG(DBG_STARTUP, "block=%d", block);
7345 
7346  dmaPMapBlk[block] = mmap(
7348  PROT_READ|PROT_WRITE,
7349  MAP_SHARED|MAP_ANONYMOUS|MAP_NORESERVE|MAP_LOCKED,
7350  -1, 0);
7351 
7352  if (dmaPMapBlk[block] == MAP_FAILED)
7353  SOFT_ERROR(PI_INIT_FAILED, "mmap dma block %d failed (%m)", block);
7354 
7355  /* force allocation of physical memory */
7356 
7357  memset((void *)dmaPMapBlk[block], 0xAA, (PAGES_PER_BLOCK*PAGE_SIZE));
7358 
7359  memset((void *)dmaPMapBlk[block], 0xFF, (PAGES_PER_BLOCK*PAGE_SIZE));
7360 
7361  memset((void *)dmaPMapBlk[block], 0, (PAGES_PER_BLOCK*PAGE_SIZE));
7362 
7363  pageNum = block * PAGES_PER_BLOCK;
7364 
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,
7369  -1, 0);
7370 
7371  if (dmaVirt[pageNum] == MAP_FAILED)
7372  SOFT_ERROR(PI_INIT_FAILED, "mmap dma block %d failed (%m)", block);
7373 
7374  munmap(dmaVirt[pageNum], PAGES_PER_BLOCK*PAGE_SIZE);
7375 
7376  trys = 0;
7377  ok = 0;
7378 
7379  while ((trys < 10) && !ok)
7380  {
7381  if (initZaps(fdPmap,
7382  dmaPMapBlk[block],
7383  pageNum,
7384  PAGES_PER_BLOCK) == 0) ok = 1;
7385  else myGpioDelay(50000);
7386 
7387  ++trys;
7388  }
7389 
7390  if (!ok) SOFT_ERROR(PI_INIT_FAILED, "initZaps failed");
7391 
7392  return 0;
7393 }
7394 
7395 static int initMboxBlock(int block)
7396 {
7397  int n, ok;
7398  unsigned page;
7399  uintptr_t virtualAdr;
7400  uintptr_t busAdr;
7401 
7402  DBG(DBG_STARTUP, "block=%d", block);
7403 
7404  ok = mbDMAAlloc
7405  (&dmaMboxBlk[block], PAGES_PER_BLOCK * PAGE_SIZE, pi_mem_flag);
7406 
7407  if (!ok) SOFT_ERROR(PI_INIT_FAILED, "init mbox zaps failed");
7408 
7409  page = block * PAGES_PER_BLOCK;
7410 
7411  virtualAdr = (uintptr_t) dmaMboxBlk[block].virtual_addr;
7412  busAdr = dmaMboxBlk[block].bus_addr;
7413 
7414  for (n=0; n<PAGES_PER_BLOCK; n++)
7415  {
7416  dmaVirt[page+n] = (dmaPage_t *) virtualAdr;
7417  dmaBus[page+n] = (dmaPage_t *) busAdr;
7418  virtualAdr += PAGE_SIZE;
7419  busAdr += PAGE_SIZE;
7420  }
7421 
7422  return 0;
7423 }
7424 
7425 /* ----------------------------------------------------------------------- */
7426 
7427 static int initAllocDMAMem(void)
7428 {
7429  int i, servoCycles, superCycles;
7430  int status;
7431 
7432  DBG(DBG_STARTUP, "");
7433 
7434  /* Calculate the number of blocks needed for buffers. The number
7435  of blocks must be a multiple of the 20ms servo cycle.
7436  */
7437 
7438  servoCycles = gpioCfg.bufferMilliseconds / 20;
7439  if (gpioCfg.bufferMilliseconds % 20) servoCycles++;
7440 
7441  bufferCycles = (SUPERCYCLE * servoCycles) / gpioCfg.clockMicros;
7442 
7443  superCycles = bufferCycles / SUPERCYCLE;
7444  if (bufferCycles % SUPERCYCLE) superCycles++;
7445 
7446  bufferCycles = SUPERCYCLE * superCycles;
7447 
7449 
7450  DBG(DBG_STARTUP, "bmillis=%d mics=%d bblk=%d bcyc=%d",
7451  gpioCfg.bufferMilliseconds, gpioCfg.clockMicros,
7453 
7454  /* allocate memory for pointers to virtual and bus memory pages */
7455 
7456  dmaVirt = mmap(
7458  PROT_READ|PROT_WRITE,
7459  MAP_PRIVATE|MAP_ANONYMOUS|MAP_LOCKED,
7460  -1, 0);
7461 
7462  if (dmaVirt == MAP_FAILED)
7463  SOFT_ERROR(PI_INIT_FAILED, "mmap dma virtual failed (%m)");
7464 
7465  dmaBus = mmap(
7467  PROT_READ|PROT_WRITE,
7468  MAP_PRIVATE|MAP_ANONYMOUS|MAP_LOCKED,
7469  -1, 0);
7470 
7471  if (dmaBus == MAP_FAILED)
7472  SOFT_ERROR(PI_INIT_FAILED, "mmap dma bus failed (%m)");
7473 
7474  dmaIVirt = (dmaIPage_t **) dmaVirt;
7475  dmaIBus = (dmaIPage_t **) dmaBus;
7476 
7477  dmaOVirt = (dmaOPage_t **)(dmaVirt + (PAGES_PER_BLOCK*bufferBlocks));
7478  dmaOBus = (dmaOPage_t **)(dmaBus + (PAGES_PER_BLOCK*bufferBlocks));
7479 
7480  if ((gpioCfg.memAllocMode == PI_MEM_ALLOC_PAGEMAP) ||
7481  ((gpioCfg.memAllocMode == PI_MEM_ALLOC_AUTO) &&
7483  {
7484  /* pagemap allocation of DMA memory */
7485 
7486  dmaPMapBlk = mmap(
7487  0, (bufferBlocks+PI_WAVE_BLOCKS)*sizeof(dmaPage_t *),
7488  PROT_READ|PROT_WRITE,
7489  MAP_PRIVATE|MAP_ANONYMOUS|MAP_LOCKED,
7490  -1, 0);
7491 
7492  if (dmaPMapBlk == MAP_FAILED)
7493  SOFT_ERROR(PI_INIT_FAILED, "pagemap mmap block failed (%m)");
7494 
7495  fdPmap = open("/proc/self/pagemap", O_RDONLY);
7496 
7497  if (fdPmap < 0)
7498  SOFT_ERROR(PI_INIT_FAILED, "pagemap open failed(%m)");
7499 
7500  for (i=0; i<(bufferBlocks+PI_WAVE_BLOCKS); i++)
7501  {
7502  status = initPagemapBlock(i);
7503  if (status < 0)
7504  {
7505  close(fdPmap);
7506  return status;
7507  }
7508  }
7509 
7510  close(fdPmap);
7511 
7512  DBG(DBG_STARTUP, "dmaPMapBlk=%08X dmaIn=%08X",
7513  (uint32_t)dmaPMapBlk, (uint32_t)dmaIn);
7514  }
7515  else
7516  {
7517  /* mailbox allocation of DMA memory */
7518 
7519  dmaMboxBlk = mmap(
7520  0, (bufferBlocks+PI_WAVE_BLOCKS)*sizeof(DMAMem_t),
7521  PROT_READ|PROT_WRITE,
7522  MAP_PRIVATE|MAP_ANONYMOUS|MAP_LOCKED,
7523  -1, 0);
7524 
7525  if (dmaMboxBlk == MAP_FAILED)
7526  SOFT_ERROR(PI_INIT_FAILED, "mmap mbox block failed (%m)");
7527 
7528  fdMbox = mbOpen();
7529 
7530  if (fdMbox < 0)
7531  SOFT_ERROR(PI_INIT_FAILED, "mbox open failed(%m)");
7532 
7533  for (i=0; i<(bufferBlocks+PI_WAVE_BLOCKS); i++)
7534  {
7535  status = initMboxBlock(i);
7536  if (status < 0)
7537  {
7538  mbClose(fdMbox);
7539  return status;
7540  }
7541  }
7542 
7543  mbClose(fdMbox);
7544 
7545  DBG(DBG_STARTUP, "dmaMboxBlk=%08X dmaIn=%08X",
7546  (uint32_t)dmaMboxBlk, (uint32_t)dmaIn);
7547  }
7548 
7549  DBG(DBG_STARTUP,
7550  "gpioReg=%08X pwmReg=%08X pcmReg=%08X clkReg=%08X auxReg=%08X",
7551  (uint32_t)gpioReg, (uint32_t)pwmReg,
7552  (uint32_t)pcmReg, (uint32_t)clkReg, (uint32_t)auxReg);
7553 
7554  for (i=0; i<DMAI_PAGES; i++)
7555  DBG(DBG_STARTUP, "dmaIBus[%d]=%08X", i, (uint32_t)dmaIBus[i]);
7556 
7557  if (gpioCfg.dbgLevel >= DBG_DMACBS)
7558  {
7559  fprintf(stderr, "*** INPUT DMA CONTROL BLOCKS ***\n");
7560  for (i=0; i<NUM_CBS; i++) dmaCbPrint(i);
7561  }
7562 
7563  return 0;
7564 }
7565 
7566 /* ----------------------------------------------------------------------- */
7567 
7568 static void initPWM(unsigned bits)
7569 {
7570  DBG(DBG_STARTUP, "bits=%d", bits);
7571 
7572  /* reset PWM */
7573 
7574  pwmReg[PWM_CTL] = 0;
7575 
7576  myGpioDelay(10);
7577 
7578  pwmReg[PWM_STA] = -1;
7579 
7580  myGpioDelay(10);
7581 
7582  /* set number of bits to transmit */
7583 
7584  pwmReg[PWM_RNG1] = bits;
7585 
7586  myGpioDelay(10);
7587 
7588  dmaIVirt[0]->periphData = 1;
7589 
7590  /* enable PWM DMA, raise panic and dreq thresholds to 15 */
7591 
7593  PWM_DMAC_PANIC(15) |
7594  PWM_DMAC_DREQ(15);
7595 
7596  myGpioDelay(10);
7597 
7598  /* clear PWM fifo */
7599 
7601 
7602  myGpioDelay(10);
7603 
7604  /* enable PWM channel 1 and use fifo */
7605 
7607 }
7608 
7609 /* ----------------------------------------------------------------------- */
7610 
7611 static void initPCM(unsigned bits)
7612 {
7613  DBG(DBG_STARTUP, "bits=%d", bits);
7614 
7615  /* disable PCM so we can modify the regs */
7616 
7617  pcmReg[PCM_CS] = 0;
7618 
7619  myGpioDelay(1000);
7620 
7621  pcmReg[PCM_FIFO] = 0;
7622  pcmReg[PCM_MODE] = 0;
7623  pcmReg[PCM_RXC] = 0;
7624  pcmReg[PCM_TXC] = 0;
7625  pcmReg[PCM_DREQ] = 0;
7626  pcmReg[PCM_INTEN] = 0;
7627  pcmReg[PCM_INTSTC] = 0;
7628  pcmReg[PCM_GRAY] = 0;
7629 
7630  myGpioDelay(1000);
7631 
7632  pcmReg[PCM_MODE] = PCM_MODE_FLEN(bits-1); /* # bits in frame */
7633 
7634  /* enable channel 1 with # bits width */
7635 
7637 
7638  pcmReg[PCM_CS] |= PCM_CS_STBY; /* clear standby */
7639 
7640  myGpioDelay(1000);
7641 
7642  pcmReg[PCM_CS] |= PCM_CS_TXCLR; /* clear TX FIFO */
7643 
7644  pcmReg[PCM_CS] |= PCM_CS_DMAEN; /* enable DREQ */
7645 
7647 
7648  pcmReg[PCM_INTSTC] = 0b1111; /* clear status bits */
7649 
7650  /* enable PCM */
7651 
7652  pcmReg[PCM_CS] |= PCM_CS_EN;
7653 
7654  /* enable tx */
7655 
7657 
7658  dmaIVirt[0]->periphData = 0x0F;
7659 }
7660 
7661 /* ----------------------------------------------------------------------- */
7662 
7663 static void initHWClk
7664  (int clkCtl, int clkDiv, int clkSrc, int divI, int divF, int MASH)
7665 {
7666  DBG(DBG_INTERNAL, "ctl=%d div=%d src=%d /I=%d /f=%d M=%d",
7667  clkCtl, clkDiv, clkSrc, divI, divF, MASH);
7668 
7669  /* kill the clock if busy, anything else isn't reliable */
7670 
7671  if (clkReg[clkCtl] & CLK_CTL_BUSY)
7672  {
7673  do
7674  {
7675  clkReg[clkCtl] = BCM_PASSWD | CLK_CTL_KILL;
7676  }
7677  while (clkReg[clkCtl] & CLK_CTL_BUSY);
7678  }
7679 
7680  clkReg[clkDiv] = (BCM_PASSWD | CLK_DIV_DIVI(divI) | CLK_DIV_DIVF(divF));
7681 
7682  usleep(10);
7683 
7684  clkReg[clkCtl] = (BCM_PASSWD | CLK_CTL_MASH(MASH) | CLK_CTL_SRC(clkSrc));
7685 
7686  usleep(10);
7687 
7688  clkReg[clkCtl] |= (BCM_PASSWD | CLK_CTL_ENAB);
7689 }
7690 
7691 static void initClock(int mainClock)
7692 {
7693  const unsigned BITS=10;
7694  int clockPWM;
7695  unsigned clkCtl, clkDiv, clkSrc, clkDivI, clkDivF, clkMash, clkBits;
7696  char *per;
7697  unsigned micros;
7698 
7699  DBG(DBG_STARTUP, "mainClock=%d", mainClock);
7700 
7701  if (mainClock) micros = gpioCfg.clockMicros;
7702  else micros = PI_WF_MICROS;
7703 
7704  clockPWM = mainClock ^ (gpioCfg.clockPeriph == PI_CLOCK_PCM);
7705 
7706  if (clockPWM)
7707  {
7708  clkCtl = CLK_PWMCTL;
7709  clkDiv = CLK_PWMDIV;
7710  per = "PWM";
7711  }
7712  else
7713  {
7714  clkCtl = CLK_PCMCTL;
7715  clkDiv = CLK_PCMDIV;
7716  per = "PCM";
7717  }
7718 
7719  clkSrc = CLK_CTL_SRC_PLLD;
7720  clkDivI = 50 * micros; /* 10 MHz - 1 MHz */
7721  clkBits = BITS; /* 10/BITS MHz - 1/BITS MHz */
7722  clkDivF = 0;
7723  clkMash = 0;
7724 
7725  DBG(DBG_STARTUP, "%s PLLD divi=%d divf=%d mash=%d bits=%d",
7726  per, clkDivI, clkDivF, clkMash, clkBits);
7727 
7728  initHWClk(clkCtl, clkDiv, clkSrc, clkDivI, clkDivF, clkMash);
7729 
7730  if (clockPWM) initPWM(BITS);
7731  else initPCM(BITS);
7732 
7733  myGpioDelay(2000);
7734 }
7735 
7736 /* ----------------------------------------------------------------------- */
7737 
7738 static void initDMAgo(volatile uint32_t *dmaAddr, uint32_t cbAddr)
7739 {
7740  DBG(DBG_STARTUP, "");
7741 
7742  dmaAddr[DMA_CS] = DMA_CHANNEL_RESET;
7743 
7745 
7746  dmaAddr[DMA_CONBLK_AD] = cbAddr;
7747 
7748  /* clear READ/FIFO/READ_LAST_NOT_SET error bits */
7749 
7750  dmaAddr[DMA_DEBUG] = DMA_DEBUG_READ_ERR |
7753 
7754 
7755  dmaAddr[DMA_CS] = DMA_WAIT_ON_WRITES |
7756  DMA_PANIC_PRIORITY(8) |
7757  DMA_PRIORITY(8) |
7758  DMA_ACTIVATE;
7759 }
7760 
7761 /* ----------------------------------------------------------------------- */
7762 
7763 static void initClearGlobals(void)
7764 {
7765  int i;
7766 
7767  DBG(DBG_STARTUP, "");
7768 
7769  alertBits = 0;
7770  monitorBits = 0;
7771  notifyBits = 0;
7772  scriptBits = 0;
7773  gFilterBits = 0;
7774  nFilterBits = 0;
7775  wdogBits = 0;
7776 
7780 
7781  wfc[0] = 0;
7782  wfc[1] = 0;
7783  wfc[2] = 0;
7784 
7785  wfcur=0;
7786 
7787  wfStats.micros = 0;
7788  wfStats.highMicros = 0;
7789  wfStats.maxMicros = PI_WAVE_MAX_MICROS;
7790 
7791  wfStats.pulses = 0;
7792  wfStats.highPulses = 0;
7793  wfStats.maxPulses = PI_WAVE_MAX_PULSES;
7794 
7795  wfStats.cbs = 0;
7796  wfStats.highCbs = 0;
7798 
7799  gpioGetSamples.func = NULL;
7800  gpioGetSamples.ex = 0;
7801  gpioGetSamples.userdata = NULL;
7802  gpioGetSamples.bits = 0;
7803 
7804  for (i=0; i<=PI_MAX_USER_GPIO; i++)
7805  {
7806  wfRx[i].mode = PI_WFRX_NONE;
7807  pthread_mutex_init(&wfRx[i].mutex, NULL);
7808  gpioAlert[i].func = NULL;
7809  }
7810 
7811  for (i=0; i<=PI_MAX_GPIO; i++)
7812  {
7813  gpioInfo [i].is = GPIO_UNDEFINED;
7814  gpioInfo [i].width = 0;
7815  gpioInfo [i].range = PI_DEFAULT_DUTYCYCLE_RANGE;
7816  gpioInfo [i].freqIdx = DEFAULT_PWM_IDX;
7817  }
7818 
7819  for (i=0; i<PI_NOTIFY_SLOTS; i++)
7820  {
7821  gpioNotify[i].seqno = 0;
7822  gpioNotify[i].state = PI_NOTIFY_CLOSED;
7823  }
7824 
7825  for (i=0; i<=PI_MAX_SIGNUM; i++)
7826  {
7827  gpioSignal[i].func = NULL;
7828  gpioSignal[i].ex = 0;
7829  gpioSignal[i].userdata = NULL;
7830  }
7831 
7832  for (i=0; i<=PI_MAX_TIMER; i++)
7833  {
7834  gpioTimer[i].running = 0;
7835  gpioTimer[i].func = NULL;
7836  }
7837 
7838  for (i=0; i<=PI_MAX_EVENT; i++)
7839  {
7840  eventAlert[i].func = NULL;
7841  eventAlert[i].ignore = 0;
7842  eventAlert[i].fired = 0;
7843  }
7844 
7845  /* calculate the usable PWM frequencies */
7846 
7847  for (i=0; i<PWM_FREQS; i++)
7848  {
7849  pwmFreq[i]=
7850  (1000000.0/
7851  ((float)PULSE_PER_CYCLE*gpioCfg.clockMicros*pwmCycles[i]))+0.5;
7852 
7853  DBG(DBG_STARTUP, "f%d is %d", i, pwmFreq[i]);
7854  }
7855 
7856  inpFifo = NULL;
7857  outFifo = NULL;
7858 
7859  fdLock = -1;
7860  fdMem = -1;
7861  fdSock = -1;
7862 
7863  dmaMboxBlk = MAP_FAILED;
7864  dmaPMapBlk = MAP_FAILED;
7865  dmaVirt = MAP_FAILED;
7866  dmaBus = MAP_FAILED;
7867 
7868  auxReg = MAP_FAILED;
7869  clkReg = MAP_FAILED;
7870  dmaReg = MAP_FAILED;
7871  gpioReg = MAP_FAILED;
7872  pcmReg = MAP_FAILED;
7873  pwmReg = MAP_FAILED;
7874  systReg = MAP_FAILED;
7875  spiReg = MAP_FAILED;
7876 }
7877 
7878 /* ----------------------------------------------------------------------- */
7879 
7880 static void initReleaseResources(void)
7881 {
7882  int i;
7883 
7884  DBG(DBG_STARTUP, "");
7885 
7886  /* shut down running threads */
7887 
7888  for (i=0; i<=PI_MAX_GPIO; i++)
7889  {
7890  if (gpioISR[i].pth)
7891  {
7892  /* destroy thread, unexport GPIO */
7893 
7894  gpioSetISRFunc(i, 0, 0, NULL);
7895  }
7896  }
7897 
7898  for (i=0; i<=PI_MAX_TIMER; i++)
7899  {
7900  if (gpioTimer[i].running)
7901  {
7902  /* destroy thread */
7903 
7904  pthread_cancel(gpioTimer[i].pthId);
7905  pthread_join(gpioTimer[i].pthId, NULL);
7906  gpioTimer[i].running = 0;
7907  }
7908  }
7909 
7911  {
7912  pthread_cancel(pthAlert);
7913  pthread_join(pthAlert, NULL);
7915  }
7916 
7918  {
7919  pthread_cancel(pthFifo);
7920  pthread_join(pthFifo, NULL);
7922  }
7923 
7925  {
7926  pthread_cancel(pthSocket);
7927  pthread_join(pthSocket, NULL);
7929  }
7930 
7931  /* release mmap'd memory */
7932 
7933  if (auxReg != MAP_FAILED) munmap((void *)auxReg, AUX_LEN);
7934  if (bscsReg != MAP_FAILED) munmap((void *)bscsReg, BSCS_LEN);
7935  if (clkReg != MAP_FAILED) munmap((void *)clkReg, CLK_LEN);
7936  if (dmaReg != MAP_FAILED) munmap((void *)dmaReg, DMA_LEN);
7937  if (gpioReg != MAP_FAILED) munmap((void *)gpioReg, GPIO_LEN);
7938  if (pcmReg != MAP_FAILED) munmap((void *)pcmReg, PCM_LEN);
7939  if (pwmReg != MAP_FAILED) munmap((void *)pwmReg, PWM_LEN);
7940  if (systReg != MAP_FAILED) munmap((void *)systReg, SYST_LEN);
7941  if (spiReg != MAP_FAILED) munmap((void *)spiReg, SPI_LEN);
7942 
7943  auxReg = MAP_FAILED;
7944  bscsReg = MAP_FAILED;
7945  clkReg = MAP_FAILED;
7946  dmaReg = MAP_FAILED;
7947  gpioReg = MAP_FAILED;
7948  pcmReg = MAP_FAILED;
7949  pwmReg = MAP_FAILED;
7950  systReg = MAP_FAILED;
7951  spiReg = MAP_FAILED;
7952 
7953  if (dmaBus != MAP_FAILED)
7954  {
7955  munmap(dmaBus,
7957  }
7958 
7959  dmaBus = MAP_FAILED;
7960 
7961  if (dmaVirt != MAP_FAILED)
7962  {
7963  for (i=0; i<PAGES_PER_BLOCK*(bufferBlocks+PI_WAVE_BLOCKS); i++)
7964  {
7965  munmap(dmaVirt[i], PAGE_SIZE);
7966  }
7967 
7968  munmap(dmaVirt,
7970  }
7971 
7972  dmaVirt = MAP_FAILED;
7973 
7974  if (dmaPMapBlk != MAP_FAILED)
7975  {
7976  for (i=0; i<(bufferBlocks+PI_WAVE_BLOCKS); i++)
7977  {
7978  munmap(dmaPMapBlk[i], PAGES_PER_BLOCK*PAGE_SIZE);
7979  }
7980 
7981  munmap(dmaPMapBlk, (bufferBlocks+PI_WAVE_BLOCKS)*sizeof(dmaPage_t *));
7982  }
7983 
7984  dmaPMapBlk = MAP_FAILED;
7985 
7986  if (dmaMboxBlk != MAP_FAILED)
7987  {
7988  fdMbox = mbOpen();
7989 
7990  for (i=0; i<(bufferBlocks+PI_WAVE_BLOCKS); i++)
7991  {
7992  mbDMAFree(&dmaMboxBlk[bufferBlocks+PI_WAVE_BLOCKS-i-1]);
7993  }
7994 
7995  mbClose(fdMbox);
7996 
7997  munmap(dmaMboxBlk, (bufferBlocks+PI_WAVE_BLOCKS)*sizeof(DMAMem_t));
7998  }
7999 
8000  dmaMboxBlk = MAP_FAILED;
8001 
8002  if (inpFifo != NULL)
8003  {
8004  fclose(inpFifo);
8005  unlink(PI_INPFIFO);
8006  inpFifo = NULL;
8007  }
8008 
8009  if (outFifo != NULL)
8010  {
8011  fclose(outFifo);
8012  unlink(PI_OUTFIFO);
8013  outFifo = NULL;
8014  }
8015 
8016  if (fdMem != -1)
8017  {
8018  close(fdMem);
8019  fdMem = -1;
8020  }
8021 
8022  if (fdLock != -1)
8023  {
8024  close(fdLock);
8025  unlink(PI_LOCKFILE);
8026  fdLock = -1;
8027  }
8028 
8029  if (fdSock != -1)
8030  {
8031  close(fdSock);
8032  fdSock = -1;
8033  }
8034 
8035  if (fdPmap != -1)
8036  {
8037  close(fdPmap);
8038  fdPmap = -1;
8039  }
8040 
8041  if (fdMbox != -1)
8042  {
8043  close(fdMbox);
8044  fdMbox = -1;
8045  }
8046 
8047  gpioStats.DMARestarts = 0;
8048  gpioStats.dmaInitCbsCount = 0;
8049 
8050  numSockNetAddr = 0;
8051 }
8052 
8054 {
8055  int rev, i, model;
8056  struct sockaddr_in server;
8057  struct sockaddr_in6 server6;
8058  char * portStr;
8059  unsigned port;
8060  struct sched_param param;
8061  pthread_attr_t pthAttr;
8062 
8063  DBG(DBG_STARTUP, "");
8064 
8065  waveClockInited = 0;
8066  PWMClockInited = 0;
8067 
8068  clock_gettime(CLOCK_REALTIME, &libStarted);
8069 
8070  rev = gpioHardwareRevision();
8071 
8072  initClearGlobals();
8073 
8074  if (initCheckPermitted() < 0) return PI_INIT_FAILED;
8075 
8077 
8078  if (fdLock < 0)
8079  SOFT_ERROR(PI_INIT_FAILED, "Can't lock %s", PI_LOCKFILE);
8080 
8081  if (!gpioMaskSet)
8082  {
8083  if (rev == 0) gpioMask = PI_DEFAULT_UPDATE_MASK_UNKNOWN;
8084  else if (rev < 4) gpioMask = PI_DEFAULT_UPDATE_MASK_B1;
8085  else if (rev < 16) gpioMask = PI_DEFAULT_UPDATE_MASK_A_B2;
8086  else if (rev == 17) gpioMask = PI_DEFAULT_UPDATE_MASK_COMPUTE;
8087  else if (rev < 20) gpioMask = PI_DEFAULT_UPDATE_MASK_APLUS_BPLUS;
8088  else if (rev == 20) gpioMask = PI_DEFAULT_UPDATE_MASK_COMPUTE;
8089  else if (rev == 21) gpioMask = PI_DEFAULT_UPDATE_MASK_APLUS_BPLUS;
8090  else
8091  {
8092  model = (rev >> 4) & 0xFF;
8093 
8094  /* model
8095  0=A 1=B
8096  2=A+ 3=B+
8097  4=Pi2B
8098  5=Alpha
8099  6=Compute Module
8100  7=Unknown
8101  8=Pi3B
8102  9=Zero
8103  */
8104  if (model < 2) gpioMask = PI_DEFAULT_UPDATE_MASK_A_B2;
8105  else if (model < 4) gpioMask = PI_DEFAULT_UPDATE_MASK_APLUS_BPLUS;
8106  else if (model == 4) gpioMask = PI_DEFAULT_UPDATE_MASK_PI2B;
8107  else if (model == 6) gpioMask = PI_DEFAULT_UPDATE_MASK_COMPUTE;
8108  else if (model == 8) gpioMask = PI_DEFAULT_UPDATE_MASK_PI3B;
8109  else if (model == 9) gpioMask = PI_DEFAULT_UPDATE_MASK_ZERO;
8111  }
8112 
8113  gpioMaskSet = 1;
8114  }
8115 
8116 #ifndef EMBEDDED_IN_VM
8117  if (!(gpioCfg.internals & PI_CFG_NOSIGHANDLER))
8118  sigSetHandler();
8119 #endif
8120 
8121  if (initPeripherals() < 0) return PI_INIT_FAILED;
8122 
8123  if (initAllocDMAMem() < 0) return PI_INIT_FAILED;
8124 
8125  /* done with /dev/mem */
8126 
8127  if (fdMem != -1)
8128  {
8129  close(fdMem);
8130  fdMem = -1;
8131  }
8132 
8133  param.sched_priority = sched_get_priority_max(SCHED_FIFO);
8134 
8135  if (gpioCfg.internals & PI_CFG_RT_PRIORITY)
8136  sched_setscheduler(0, SCHED_FIFO, &param);
8137 
8138  initClock(1); /* initialise main clock */
8139 
8140  atexit(gpioTerminate);
8141 
8142  if (pthread_attr_init(&pthAttr))
8143  SOFT_ERROR(PI_INIT_FAILED, "pthread_attr_init failed (%m)");
8144 
8145  if (pthread_attr_setstacksize(&pthAttr, STACK_SIZE))
8146  SOFT_ERROR(PI_INIT_FAILED, "pthread_attr_setstacksize failed (%m)");
8147 
8148  if (!(gpioCfg.ifFlags & PI_DISABLE_ALERT))
8149  {
8150  if (pthread_create(&pthAlert, &pthAttr, pthAlertThread, &i))
8151  SOFT_ERROR(PI_INIT_FAILED, "pthread_create alert failed (%m)");
8152 
8154  }
8155 
8156  if (!(gpioCfg.ifFlags & PI_DISABLE_FIFO_IF))
8157  {
8158  if (pthread_create(&pthFifo, &pthAttr, pthFifoThread, &i))
8159  SOFT_ERROR(PI_INIT_FAILED, "pthread_create fifo failed (%m)");
8160 
8162  }
8163 
8164  if (!(gpioCfg.ifFlags & PI_DISABLE_SOCK_IF))
8165  {
8166  portStr = getenv(PI_ENVPORT);
8167  if (portStr) port = atoi(portStr); else port = gpioCfg.socketPort;
8168 
8169  // Accept connections on IPv6, unless we have an IPv4-only whitelist
8170  if (!numSockNetAddr)
8171  {
8172  fdSock = socket(AF_INET6, SOCK_STREAM , 0);
8173 
8174  if (fdSock != -1)
8175  {
8176  bzero((char *)&server6, sizeof(server6));
8177  server6.sin6_family = AF_INET6;
8178  if (gpioCfg.ifFlags & PI_LOCALHOST_SOCK_IF)
8179  {
8180  server6.sin6_addr = in6addr_loopback;
8181  }
8182  else
8183  {
8184  server6.sin6_addr = in6addr_any;
8185  }
8186  server6.sin6_port = htons(port);
8187 
8188  if (bind(fdSock,(struct sockaddr *)&server6, sizeof(server6)) < 0)
8189  SOFT_ERROR(PI_INIT_FAILED, "bind to port %d failed (%m)", port);
8190  }
8191  }
8192 
8193  if (numSockNetAddr || fdSock == -1)
8194  {
8195  fdSock = socket(AF_INET , SOCK_STREAM , 0);
8196 
8197  if (fdSock == -1)
8198  SOFT_ERROR(PI_INIT_FAILED, "socket failed (%m)");
8199 
8200  server.sin_family = AF_INET;
8201  if (gpioCfg.ifFlags & PI_LOCALHOST_SOCK_IF)
8202  {
8203  server.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
8204  }
8205  else
8206  {
8207  server.sin_addr.s_addr = htonl(INADDR_ANY);
8208  }
8209  server.sin_port = htons(port);
8210 
8211  if (bind(fdSock,(struct sockaddr *)&server , sizeof(server)) < 0)
8212  SOFT_ERROR(PI_INIT_FAILED, "bind to port %d failed (%m)", port);
8213  }
8214 
8215  if (pthread_create(&pthSocket, &pthAttr, pthSocketThread, &i))
8216  SOFT_ERROR(PI_INIT_FAILED, "pthread_create socket failed (%m)");
8217 
8219  }
8220 
8221  myGpioDelay(1000);
8222 
8223  dmaInitCbs();
8224 
8225  flushMemory();
8226 
8227  initDMAgo((uint32_t *)dmaIn, (uint32_t)dmaIBus[0]);
8228 
8229  return PIGPIO_VERSION;
8230 }
8231 
8232 
8233 /* ======================================================================= */
8234 
8235 int getBitInBytes(int bitPos, char *buf, int numBits)
8236 {
8237  int bitp, bufp;
8238 
8239  if (bitPos < numBits)
8240  {
8241  bufp = bitPos / 8;
8242  bitp = 7 - (bitPos % 8);
8243  if (buf[bufp] & (1<<bitp)) return 1;
8244  }
8245 
8246  return 0;
8247 }
8248 
8249 /* ----------------------------------------------------------------------- */
8250 
8251 void putBitInBytes(int bitPos, char *buf, int bit)
8252 {
8253  int bitp, bufp;
8254 
8255  bufp = bitPos / 8;
8256  bitp = 7 - (bitPos % 8);
8257 
8258  if (bit) buf[bufp] |= (1<<bitp);
8259  else buf[bufp] &= (~(1<<bitp));
8260 }
8261 
8262 /* ----------------------------------------------------------------------- */
8263 
8264 uint32_t rawWaveGetOOL(int pos)
8265 {
8266  int page, slot;
8267 
8268  if ((pos >= 0) && (pos < NUM_WAVE_OOL))
8269  {
8270  waveOOLPageSlot(pos, &page, &slot);
8271  return (dmaOVirt[page]->OOL[slot]);
8272  }
8273 
8274  return -1;
8275 }
8276 
8277 /* ----------------------------------------------------------------------- */
8278 
8279 void rawWaveSetOOL(int pos, uint32_t value)
8280 {
8281  int page, slot;
8282 
8283  if ((pos >= 0) && (pos < NUM_WAVE_OOL))
8284  {
8285  waveOOLPageSlot(pos, &page, &slot);
8286  dmaOVirt[page]->OOL[slot] = value;
8287  }
8288 }
8289 
8290 
8291 /* ----------------------------------------------------------------------- */
8292 
8293 uint32_t rawWaveGetOut(int pos)
8294 {
8295  int page, slot;
8296 
8297  if ((pos >= 0) && (pos < NUM_WAVE_OOL))
8298  {
8299  waveOOLPageSlot(pos, &page, &slot);
8300  return (dmaOVirt[page]->OOL[slot]);
8301  }
8302 
8303  return -1;
8304 }
8305 
8306 /* ----------------------------------------------------------------------- */
8307 
8308 void rawWaveSetOut(int pos, uint32_t value)
8309 {
8310  int page, slot;
8311 
8312  if ((pos >= 0) && (pos < NUM_WAVE_OOL))
8313  {
8314  waveOOLPageSlot(pos, &page, &slot);
8315  dmaOVirt[page]->OOL[slot] = value;
8316  }
8317 }
8318 
8319 
8320 /* ----------------------------------------------------------------------- */
8321 
8322 uint32_t rawWaveGetIn(int pos)
8323 {
8324  int page, slot;
8325 
8326  if ((pos >= 0) && (pos < NUM_WAVE_OOL))
8327  {
8328  waveOOLPageSlot((NUM_WAVE_OOL-1)-pos, &page, &slot);
8329  return (dmaOVirt[page]->OOL[slot]);
8330  }
8331 
8332  return -1;
8333 }
8334 
8335 /* ----------------------------------------------------------------------- */
8336 
8337 void rawWaveSetIn(int pos, uint32_t value)
8338 {
8339  int page, slot;
8340 
8341  if ((pos >= 0) && (pos < NUM_WAVE_OOL))
8342  {
8343  waveOOLPageSlot((NUM_WAVE_OOL-1)-pos, &page, &slot);
8344  dmaOVirt[page]->OOL[slot] = value;
8345  }
8346 }
8347 
8348 /* ----------------------------------------------------------------------- */
8349 
8351 {
8352  rawWaveInfo_t dummy = {0, 0, 0, 0, 0, 0, 0, 0};
8353 
8354  if ((wave_id >=0) && (wave_id < PI_MAX_WAVES)) return waveInfo[wave_id];
8355  else return dummy;
8356 }
8357 
8358 /* ----------------------------------------------------------------------- */
8359 
8360 double time_time(void)
8361 {
8362  struct timeval tv;
8363  double t;
8364 
8365  gettimeofday(&tv, 0);
8366 
8367  t = (double)tv.tv_sec + ((double)tv.tv_usec / 1E6);
8368 
8369  return t;
8370 }
8371 
8372 /* ----------------------------------------------------------------------- */
8373 
8374 void time_sleep(double seconds)
8375 {
8376  struct timespec ts, rem;
8377 
8378  if (seconds > 0.0)
8379  {
8380  ts.tv_sec = seconds;
8381  ts.tv_nsec = (seconds-(double)ts.tv_sec) * 1E9;
8382 
8383  while (clock_nanosleep(CLOCK_REALTIME, 0, &ts, &rem))
8384  {
8385  /* copy remaining time to ts */
8386  ts.tv_sec = rem.tv_sec;
8387  ts.tv_nsec = rem.tv_nsec;
8388  }
8389  }
8390 }
8391 
8392 /* ----------------------------------------------------------------------- */
8393 
8394 void rawDumpWave(void)
8395 {
8396  int i;
8397 
8398  unsigned numWaves, t;
8399 
8400  rawWave_t *waves;
8401 
8402  numWaves = wfc[wfcur];
8403  waves = wf [wfcur];
8404 
8405  t = 0;
8406 
8407  for (i=0; i<numWaves; i++)
8408  {
8409  fprintf(stderr, "%10u %08X %08X %08X %10u\n",
8410  t, waves[i].gpioOn, waves[i].gpioOff,
8411  waves[i].flags, waves[i].usDelay);
8412  t += waves[i].usDelay;
8413  }
8414 }
8415 
8416 /* ----------------------------------------------------------------------- */
8417 
8418 void rawDumpScript(unsigned script_id)
8419 {
8420  int i;
8421 
8422  if (script_id >= PI_MAX_SCRIPTS) return;
8423 
8424  if (gpioScript[script_id].state == PI_SCRIPT_IN_USE)
8425  {
8426 
8427  for (i=0; i<PI_MAX_SCRIPT_PARAMS; i++)
8428  {
8429  fprintf(stderr, "p%d=%d ", i, gpioScript[script_id].script.par[i]);
8430  }
8431 
8432  fprintf(stderr, "\n");
8433 
8434  for (i=0; i<PI_MAX_SCRIPT_VARS; i++)
8435  {
8436  fprintf(stderr, "v%d=%d ", i, gpioScript[script_id].script.var[i]);
8437  }
8438 
8439  fprintf(stderr, "\n");
8440 
8441  for (i=0; i<gpioScript[script_id].script.instrs; i++)
8442  {
8443  fprintf(stderr,
8444  "c%d=[%d, %d(%d), %d(%d), %d, %d]\n",
8445  i,
8446  gpioScript[script_id].script.instr[i].p[0],
8447  gpioScript[script_id].script.instr[i].p[1],
8448  gpioScript[script_id].script.instr[i].opt[1],
8449  gpioScript[script_id].script.instr[i].p[2],
8450  gpioScript[script_id].script.instr[i].opt[2],
8451  gpioScript[script_id].script.instr[i].p[3],
8452  gpioScript[script_id].script.instr[i].p[4]);
8453  }
8454  }
8455 }
8456 
8457 /* ======================================================================= */
8458 
8460 {
8461  int status;
8462 
8463  if (libInitialised) return PIGPIO_VERSION;
8464 
8465  DBG(DBG_STARTUP, "not initialised, initialising");
8466 
8468 
8469  status = initInitialise();
8470 
8471  if (status < 0)
8472  {
8473  runState = PI_ENDING;
8475  }
8476  else
8477  {
8478  libInitialised = 1;
8479 
8480  runState = PI_RUNNING;
8481 
8482  if (!(gpioCfg.ifFlags & PI_DISABLE_ALERT))
8483  {
8485  }
8486 
8487  }
8488 
8489  return status;
8490 }
8491 
8492 
8493 /* ----------------------------------------------------------------------- */
8494 
8495 void gpioTerminate(void)
8496 {
8497  int i;
8498 
8499  DBG(DBG_USER, "");
8500 
8501  if (!libInitialised) return;
8502 
8503  DBG(DBG_STARTUP, "initialised, terminating");
8504 
8505  runState = PI_ENDING;
8506 
8507  gpioMaskSet = 0;
8508 
8509  /* reset DMA */
8510 
8511  if (dmaReg != MAP_FAILED) dmaIn[DMA_CS] = DMA_CHANNEL_RESET;
8512  if (dmaReg != MAP_FAILED) dmaOut[DMA_CS] = DMA_CHANNEL_RESET;
8513 
8514 #ifndef EMBEDDED_IN_VM
8515  if ((gpioCfg.internals & PI_CFG_STATS) &&
8516  (!(gpioCfg.internals & PI_CFG_NOSIGHANDLER)))
8517  {
8518  fprintf(stderr,
8519  "\n#####################################################\n");
8520  fprintf(stderr, "pigpio version=%d internals=%X\n",
8521  PIGPIO_VERSION, gpioCfg.internals);
8522 
8523  fprintf(stderr,
8524  "micros=%d allocMode=%d dmaInitCbs=%d DMARestarts=%d\n",
8525  gpioCfg.clockMicros, gpioCfg.memAllocMode,
8526  gpioStats.dmaInitCbsCount, gpioStats.DMARestarts);
8527 
8528  fprintf(stderr,
8529  "samples %u maxSamples %u maxEmit %u emitFrags %u\n",
8530  gpioStats.numSamples, gpioStats.maxSamples,
8531  gpioStats.maxEmit, gpioStats.emitFrags);
8532 
8533  fprintf(stderr, "cbTicks %d, cbCalls %u\n",
8534  gpioStats.cbTicks, gpioStats.cbCalls);
8535 
8536  fprintf(stderr, "pipe: good %u, short %u, would block %u\n",
8537  gpioStats.goodPipeWrite, gpioStats.shortPipeWrite,
8538  gpioStats.wouldBlockPipeWrite);
8539 
8540  fprintf(stderr, "alertTicks %u, lateTicks %u, moreToDo %u\n",
8541  gpioStats.alertTicks, gpioStats.lateTicks, gpioStats.moreToDo);
8542 
8543  for (i=0; i< TICKSLOTS; i++)
8544  fprintf(stderr, "%9u ", gpioStats.diffTick[i]);
8545 
8546  fprintf(stderr,
8547  "\n#####################################################\n\n\n");
8548  }
8549 
8550 #endif
8552 
8553  fflush(NULL);
8554 
8555  libInitialised = 0;
8556 }
8557 
8558 static void switchFunctionOff(unsigned gpio)
8559 {
8560  switch (gpioInfo[gpio].is)
8561  {
8562  case GPIO_SERVO:
8563  /* switch servo off */
8564  myGpioSetServo(gpio, gpioInfo[gpio].width, 0);
8565  gpioInfo[gpio].width = 0;
8566  break;
8567 
8568  case GPIO_PWM:
8569  /* switch pwm off */
8570  myGpioSetPwm(gpio, gpioInfo[gpio].width, 0);
8571  gpioInfo[gpio].width = 0;
8572  break;
8573 
8574  case GPIO_HW_CLK:
8575  /* No longer disable clock hardware, doing that was a bug. */
8576  gpioInfo[gpio].width = 0;
8577  break;
8578 
8579  case GPIO_HW_PWM:
8580  /* No longer disable PWM hardware, doing that was a bug. */
8581  gpioInfo[gpio].width = 0;
8582  break;
8583  }
8584 }
8585 
8586 static void stopHardwarePWM(void)
8587 {
8588  unsigned i, pwm;
8589 
8590  for (i=0; i<= PI_MAX_GPIO; i++)
8591  {
8592  if (gpioInfo[i].is == GPIO_HW_PWM)
8593  {
8594  pwm = (PWMDef[i] >> 4) & 3;
8595 
8596  if (pwm == 0) pwmReg[PWM_CTL] &= (~PWM_CTL_PWEN1);
8597  else pwmReg[PWM_CTL] &= (~PWM_CTL_PWEN2);
8598 
8599  gpioInfo[i].width = 0;
8600  gpioInfo[i].is = GPIO_UNDEFINED;
8601  }
8602  }
8603 }
8604 
8605 /* ----------------------------------------------------------------------- */
8606 
8607 int gpioSetMode(unsigned gpio, unsigned mode)
8608 {
8609  int reg, shift, old_mode;
8610 
8611  DBG(DBG_USER, "gpio=%d mode=%d", gpio, mode);
8612 
8613  CHECK_INITED;
8614 
8615  if (gpio > PI_MAX_GPIO)
8616  SOFT_ERROR(PI_BAD_GPIO, "bad gpio (%d)", gpio);
8617 
8618  if (mode > PI_ALT3)
8619  SOFT_ERROR(PI_BAD_MODE, "gpio %d, bad mode (%d)", gpio, mode);
8620 
8621  reg = gpio/10;
8622  shift = (gpio%10) * 3;
8623 
8624  old_mode = (gpioReg[reg] >> shift) & 7;
8625 
8626  if (mode != old_mode)
8627  {
8628  switchFunctionOff(gpio);
8629 
8630  gpioInfo[gpio].is = GPIO_UNDEFINED;
8631  }
8632 
8633  gpioReg[reg] = (gpioReg[reg] & ~(7<<shift)) | (mode<<shift);
8634 
8635  return 0;
8636 }
8637 
8638 
8639 /* ----------------------------------------------------------------------- */
8640 
8641 int gpioGetMode(unsigned gpio)
8642 {
8643  int reg, shift;
8644 
8645  DBG(DBG_USER, "gpio=%d", gpio);
8646 
8647  CHECK_INITED;
8648 
8649  if (gpio > PI_MAX_GPIO)
8650  SOFT_ERROR(PI_BAD_GPIO, "bad gpio (%d)", gpio);
8651 
8652  reg = gpio/10;
8653  shift = (gpio%10) * 3;
8654 
8655  return (gpioReg[reg] >> shift) & 7;
8656 }
8657 
8658 
8659 /* ----------------------------------------------------------------------- */
8660 
8661 int gpioSetPullUpDown(unsigned gpio, unsigned pud)
8662 {
8663  DBG(DBG_USER, "gpio=%d pud=%d", gpio, pud);
8664 
8665  CHECK_INITED;
8666 
8667  if (gpio > PI_MAX_GPIO)
8668  SOFT_ERROR(PI_BAD_GPIO, "bad gpio (%d)", gpio);
8669 
8670  if (pud > PI_PUD_UP)
8671  SOFT_ERROR(PI_BAD_PUD, "gpio %d, bad pud (%d)", gpio, pud);
8672 
8673  *(gpioReg + GPPUD) = pud;
8674 
8675  myGpioDelay(1);
8676 
8677  *(gpioReg + GPPUDCLK0 + BANK) = BIT;
8678 
8679  myGpioDelay(1);
8680 
8681  *(gpioReg + GPPUD) = 0;
8682 
8683  *(gpioReg + GPPUDCLK0 + BANK) = 0;
8684 
8685  return 0;
8686 }
8687 
8688 
8689 /* ----------------------------------------------------------------------- */
8690 
8691 int gpioRead(unsigned gpio)
8692 {
8693  DBG(DBG_USER, "gpio=%d", gpio);
8694 
8695  CHECK_INITED;
8696 
8697  if (gpio > PI_MAX_GPIO)
8698  SOFT_ERROR(PI_BAD_GPIO, "bad gpio (%d)", gpio);
8699 
8700  if ((*(gpioReg + GPLEV0 + BANK) & BIT) != 0) return PI_ON;
8701  else return PI_OFF;
8702 }
8703 
8704 
8705 /* ----------------------------------------------------------------------- */
8706 
8707 int gpioWrite(unsigned gpio, unsigned level)
8708 {
8709  DBG(DBG_USER, "gpio=%d level=%d", gpio, level);
8710 
8711  CHECK_INITED;
8712 
8713  if (gpio > PI_MAX_GPIO)
8714  SOFT_ERROR(PI_BAD_GPIO, "bad gpio (%d)", gpio);
8715 
8716  if (level > PI_ON)
8717  SOFT_ERROR(PI_BAD_LEVEL, "gpio %d, bad level (%d)", gpio, level);
8718 
8719  if (gpio <= PI_MAX_GPIO)
8720  {
8721  if (gpioInfo[gpio].is != GPIO_WRITE)
8722  {
8723  /* stop a glitch between setting mode then level */
8724  if (level == PI_OFF) *(gpioReg + GPCLR0 + BANK) = BIT;
8725  else *(gpioReg + GPSET0 + BANK) = BIT;
8726 
8727  switchFunctionOff(gpio);
8728 
8729  gpioInfo[gpio].is = GPIO_WRITE;
8730  }
8731  }
8732 
8733  myGpioSetMode(gpio, PI_OUTPUT);
8734 
8735  if (level == PI_OFF) *(gpioReg + GPCLR0 + BANK) = BIT;
8736  else *(gpioReg + GPSET0 + BANK) = BIT;
8737 
8738  return 0;
8739 }
8740 
8741 
8742 /* ----------------------------------------------------------------------- */
8743 
8744 int gpioPWM(unsigned gpio, unsigned val)
8745 {
8746  DBG(DBG_USER, "gpio=%d dutycycle=%d", gpio, val);
8747 
8748  CHECK_INITED;
8749 
8750  if (gpio > PI_MAX_USER_GPIO)
8751  SOFT_ERROR(PI_BAD_USER_GPIO, "bad gpio (%d)", gpio);
8752 
8753  if (val > gpioInfo[gpio].range)
8754  SOFT_ERROR(PI_BAD_DUTYCYCLE, "gpio %d, bad dutycycle (%d)", gpio, val);
8755 
8756  if (gpioInfo[gpio].is != GPIO_PWM)
8757  {
8758  switchFunctionOff(gpio);
8759 
8760  gpioInfo[gpio].is = GPIO_PWM;
8761 
8762  if (!val) myGpioWrite(gpio, 0);
8763  }
8764 
8765  myGpioSetMode(gpio, PI_OUTPUT);
8766 
8767  myGpioSetPwm(gpio, gpioInfo[gpio].width, val);
8768 
8769  gpioInfo[gpio].width=val;
8770 
8771  return 0;
8772 }
8773 
8774 /* ----------------------------------------------------------------------- */
8775 
8776 int gpioGetPWMdutycycle(unsigned gpio)
8777 {
8778  unsigned pwm;
8779 
8780  DBG(DBG_USER, "gpio=%d", gpio);
8781 
8782  CHECK_INITED;
8783 
8784  if (gpio > PI_MAX_USER_GPIO)
8785  SOFT_ERROR(PI_BAD_USER_GPIO, "bad gpio (%d)", gpio);
8786 
8787  switch (gpioInfo[gpio].is)
8788  {
8789  case GPIO_PWM:
8790  return gpioInfo[gpio].width;
8791 
8792  case GPIO_HW_PWM:
8793  pwm = (PWMDef[gpio] >> 4) & 3;
8794  return hw_pwm_duty[pwm];
8795 
8796  case GPIO_HW_CLK:
8797  return PI_HW_PWM_RANGE/2;
8798 
8799  default:
8800  SOFT_ERROR(PI_NOT_PWM_GPIO, "not a PWM gpio (%d)", gpio);
8801  }
8802 }
8803 
8804 
8805 /* ----------------------------------------------------------------------- */
8806 
8807 int gpioSetPWMrange(unsigned gpio, unsigned range)
8808 {
8809  int oldWidth, newWidth;
8810 
8811  DBG(DBG_USER, "gpio=%d range=%d", gpio, range);
8812 
8813  CHECK_INITED;
8814 
8815  if (gpio > PI_MAX_USER_GPIO)
8816  SOFT_ERROR(PI_BAD_USER_GPIO, "bad gpio (%d)", gpio);
8817 
8818  if ((range < PI_MIN_DUTYCYCLE_RANGE) || (range > PI_MAX_DUTYCYCLE_RANGE))
8819  SOFT_ERROR(PI_BAD_DUTYRANGE, "gpio %d, bad range (%d)", gpio, range);
8820 
8821  oldWidth = gpioInfo[gpio].width;
8822 
8823  if (oldWidth)
8824  {
8825  if (gpioInfo[gpio].is == GPIO_PWM)
8826  {
8827  newWidth = (range * oldWidth) / gpioInfo[gpio].range;
8828 
8829  myGpioSetPwm(gpio, oldWidth, 0);
8830  gpioInfo[gpio].range = range;
8831  gpioInfo[gpio].width = newWidth;
8832  myGpioSetPwm(gpio, 0, newWidth);
8833  }
8834  }
8835 
8836  gpioInfo[gpio].range = range;
8837 
8838  /* return the actual range for the current gpio frequency */
8839 
8840  return pwmRealRange[gpioInfo[gpio].freqIdx];
8841 }
8842 
8843 
8844 /* ----------------------------------------------------------------------- */
8845 
8846 int gpioGetPWMrange(unsigned gpio)
8847 {
8848  DBG(DBG_USER, "gpio=%d", gpio);
8849 
8850  CHECK_INITED;
8851 
8852  if (gpio > PI_MAX_USER_GPIO)
8853  SOFT_ERROR(PI_BAD_USER_GPIO, "bad gpio (%d)", gpio);
8854 
8855  switch (gpioInfo[gpio].is)
8856  {
8857  case GPIO_HW_PWM:
8858  case GPIO_HW_CLK:
8859  return PI_HW_PWM_RANGE;
8860 
8861  default:
8862  return gpioInfo[gpio].range;
8863  }
8864 }
8865 
8866 
8867 /* ----------------------------------------------------------------------- */
8868 
8869 int gpioGetPWMrealRange(unsigned gpio)
8870 {
8871  unsigned pwm;
8872 
8873  DBG(DBG_USER, "gpio=%d", gpio);
8874 
8875  CHECK_INITED;
8876 
8877  if (gpio > PI_MAX_USER_GPIO)
8878  SOFT_ERROR(PI_BAD_USER_GPIO, "bad gpio (%d)", gpio);
8879 
8880  switch (gpioInfo[gpio].is)
8881  {
8882  case GPIO_HW_PWM:
8883  pwm = (PWMDef[gpio] >> 4) & 3;
8884  return hw_pwm_real_range[pwm];
8885 
8886  case GPIO_HW_CLK:
8887  return PI_HW_PWM_RANGE;
8888 
8889  default:
8890  return pwmRealRange[gpioInfo[gpio].freqIdx];
8891  }
8892 }
8893 
8894 
8895 /* ----------------------------------------------------------------------- */
8896 
8897 int gpioSetPWMfrequency(unsigned gpio, unsigned frequency)
8898 {
8899  int i, width;
8900  unsigned diff, best, idx;
8901 
8902  DBG(DBG_USER, "gpio=%d frequency=%d", gpio, frequency);
8903 
8904  CHECK_INITED;
8905 
8906  if (gpio > PI_MAX_USER_GPIO)
8907  SOFT_ERROR(PI_BAD_USER_GPIO, "bad gpio (%d)", gpio);
8908 
8909  if (frequency > pwmFreq[0]) idx = 0;
8910  else if (frequency < pwmFreq[PWM_FREQS-1]) idx = PWM_FREQS-1;
8911  else
8912  {
8913  best = 100000; /* impossibly high frequency difference */
8914  idx = 0;
8915 
8916  for (i=0; i<PWM_FREQS; i++)
8917  {
8918  if (frequency > pwmFreq[i]) diff = frequency - pwmFreq[i];
8919  else diff = pwmFreq[i] - frequency;
8920 
8921  if (diff < best)
8922  {
8923  best = diff;
8924  idx = i;
8925  }
8926  }
8927  }
8928 
8929  width = gpioInfo[gpio].width;
8930 
8931  if (width)
8932  {
8933  if (gpioInfo[gpio].is == GPIO_PWM)
8934  {
8935  myGpioSetPwm(gpio, width, 0);
8936  gpioInfo[gpio].freqIdx = idx;
8937  myGpioSetPwm(gpio, 0, width);
8938  }
8939  }
8940 
8941  gpioInfo[gpio].freqIdx = idx;
8942 
8943  return pwmFreq[idx];
8944 }
8945 
8946 
8947 /* ----------------------------------------------------------------------- */
8948 
8949 int gpioGetPWMfrequency(unsigned gpio)
8950 {
8951  unsigned pwm, clock;
8952 
8953  DBG(DBG_USER, "gpio=%d", gpio);
8954 
8955  CHECK_INITED;
8956 
8957  if (gpio > PI_MAX_USER_GPIO)
8958  SOFT_ERROR(PI_BAD_USER_GPIO, "bad gpio (%d)", gpio);
8959 
8960  switch (gpioInfo[gpio].is)
8961  {
8962  case GPIO_HW_PWM:
8963  pwm = (PWMDef[gpio] >> 4) & 3;
8964  return hw_pwm_freq[pwm];
8965 
8966  case GPIO_HW_CLK:
8967  clock = (clkDef[gpio] >> 4) & 3;
8968  return hw_clk_freq[clock];
8969 
8970  default:
8971  return pwmFreq[gpioInfo[gpio].freqIdx];
8972  }
8973 }
8974 
8975 
8976 /* ----------------------------------------------------------------------- */
8977 
8978 int gpioServo(unsigned gpio, unsigned val)
8979 {
8980  DBG(DBG_USER, "gpio=%d pulsewidth=%d", gpio, val);
8981 
8982  CHECK_INITED;
8983 
8984  if (gpio > PI_MAX_USER_GPIO)
8985  SOFT_ERROR(PI_BAD_USER_GPIO, "bad gpio (%d)", gpio);
8986 
8987  if ((val!=PI_SERVO_OFF) && (val<PI_MIN_SERVO_PULSEWIDTH))
8989  "gpio %d, bad pulsewidth (%d)", gpio, val);
8990 
8991  if (val>PI_MAX_SERVO_PULSEWIDTH)
8993  "gpio %d, bad pulsewidth (%d)", gpio, val);
8994 
8995  if (gpioInfo[gpio].is != GPIO_SERVO)
8996  {
8997  switchFunctionOff(gpio);
8998 
8999  gpioInfo[gpio].is = GPIO_SERVO;
9000 
9001  if (!val) myGpioWrite(gpio, 0);
9002  }
9003 
9004  myGpioSetMode(gpio, PI_OUTPUT);
9005 
9006  myGpioSetServo(gpio, gpioInfo[gpio].width, val);
9007 
9008  gpioInfo[gpio].width=val;
9009 
9010  return 0;
9011 }
9012 
9013 
9014 /* ----------------------------------------------------------------------- */
9015 
9016 int gpioGetServoPulsewidth(unsigned gpio)
9017 {
9018  DBG(DBG_USER, "gpio=%d", gpio);
9019 
9020  CHECK_INITED;
9021 
9022  if (gpio > PI_MAX_USER_GPIO)
9023  SOFT_ERROR(PI_BAD_USER_GPIO, "bad gpio (%d)", gpio);
9024 
9025  if (gpioInfo[gpio].is != GPIO_SERVO)
9026  SOFT_ERROR(PI_NOT_SERVO_GPIO, "not a servo gpio (%d)", gpio);
9027 
9028  return gpioInfo[gpio].width;
9029 }
9030 
9031 
9032 /* ----------------------------------------------------------------------- */
9033 
9034 int gpioWaveClear(void)
9035 {
9036  DBG(DBG_USER, "");
9037 
9038  CHECK_INITED;
9039 
9040  wfc[0] = 0;
9041  wfc[1] = 0;
9042  wfc[2] = 0;
9043 
9044  wfcur = 0;
9045 
9046  wfStats.micros = 0;
9047  wfStats.pulses = 0;
9048  wfStats.cbs = 0;
9049 
9053 
9054  waveOutCount = 0;
9055 
9056  waveEndPtr = NULL;
9057 
9058  return 0;
9059 }
9060 
9061 /* ----------------------------------------------------------------------- */
9062 
9064 {
9065  DBG(DBG_USER, "");
9066 
9067  CHECK_INITED;
9068 
9069  wfc[0] = 0;
9070  wfc[1] = 0;
9071  wfc[2] = 0;
9072 
9073  wfcur = 0;
9074 
9075  wfStats.micros = 0;
9076  wfStats.pulses = 0;
9077  wfStats.cbs = 0;
9078 
9079  return 0;
9080 }
9081 
9082 /* ----------------------------------------------------------------------- */
9083 
9084 int gpioWaveAddGeneric(unsigned numPulses, gpioPulse_t *pulses)
9085 {
9086  int p;
9087 
9088  DBG(DBG_USER, "numPulses=%u pulses=%08X", numPulses, (uint32_t)pulses);
9089 
9090  CHECK_INITED;
9091 
9092  if (numPulses > PI_WAVE_MAX_PULSES)
9093  SOFT_ERROR(PI_TOO_MANY_PULSES, "bad number of pulses (%d)", numPulses);
9094 
9095  if (!pulses) SOFT_ERROR(PI_BAD_POINTER, "bad (NULL) pulses pointer");
9096 
9097  for (p=0; p<numPulses; p++)
9098  {
9099  wf[2][p].gpioOff = pulses[p].gpioOff;
9100  wf[2][p].gpioOn = pulses[p].gpioOn;
9101  wf[2][p].usDelay = pulses[p].usDelay;
9102  wf[2][p].flags = 0;
9103  }
9104 
9105  return rawWaveAddGeneric(numPulses, wf[2]);
9106 }
9107 
9108 /* ----------------------------------------------------------------------- */
9109 
9111  (unsigned gpio,
9112  unsigned baud,
9113  unsigned data_bits,
9114  unsigned stop_bits,
9115  unsigned offset,
9116  unsigned numBytes,
9117  char *bstr)
9118 {
9119  int i, b, p, lev, c, v;
9120 
9121  uint16_t *wstr = (uint16_t *)bstr;
9122  uint32_t *lstr = (uint32_t *)bstr;
9123 
9124  unsigned bitDelay[32];
9125 
9126  DBG(DBG_USER,
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));
9130 
9131  CHECK_INITED;
9132 
9133  if (gpio > PI_MAX_USER_GPIO)
9134  SOFT_ERROR(PI_BAD_USER_GPIO, "bad gpio (%d)", gpio);
9135 
9136  if ((baud < PI_WAVE_MIN_BAUD) || (baud > PI_WAVE_MAX_BAUD))
9137  SOFT_ERROR(PI_BAD_WAVE_BAUD, "bad baud rate (%d)", baud);
9138 
9139  if ((data_bits < PI_MIN_WAVE_DATABITS) ||
9140  (data_bits > PI_MAX_WAVE_DATABITS))
9141  SOFT_ERROR(PI_BAD_DATABITS, "bad number of databits (%d)", data_bits);
9142 
9143  if ((stop_bits < PI_MIN_WAVE_HALFSTOPBITS) ||
9144  (stop_bits > PI_MAX_WAVE_HALFSTOPBITS))
9146  "bad number of (half) stop bits (%d)", stop_bits);
9147 
9148  if (gpio > PI_MAX_USER_GPIO)
9149  SOFT_ERROR(PI_BAD_USER_GPIO, "bad gpio (%d)", gpio);
9150 
9151  if (numBytes > PI_WAVE_MAX_CHARS)
9152  SOFT_ERROR(PI_TOO_MANY_CHARS, "too many chars (%d)", numBytes);
9153 
9154  if (offset > PI_WAVE_MAX_MICROS)
9155  SOFT_ERROR(PI_BAD_SER_OFFSET, "offset too large (%d)", offset);
9156 
9157  if (data_bits > 8) numBytes /= 2;
9158  if (data_bits > 16) numBytes /= 2;
9159 
9160  if (!numBytes) return 0;
9161 
9162  waveBitDelay(baud, data_bits, stop_bits, bitDelay);
9163 
9164  p = 0;
9165 
9166  wf[2][p].gpioOn = (1<<gpio);
9167  wf[2][p].gpioOff = 0;
9168  wf[2][p].flags = 0;
9169 
9170  if (offset > bitDelay[0]) wf[2][p].usDelay = offset;
9171  else wf[2][p].usDelay = bitDelay[0];
9172 
9173  for (i=0; i<numBytes; i++)
9174  {
9175  p++;
9176 
9177  /* start bit */
9178 
9179  wf[2][p].gpioOn = 0;
9180  wf[2][p].gpioOff = (1<<gpio);
9181  wf[2][p].usDelay = bitDelay[0];
9182  wf[2][p].flags = 0;
9183 
9184  lev = 0;
9185 
9186  if (data_bits < 9) c = bstr[i];
9187  else if (data_bits < 17) c = wstr[i];
9188  else c = lstr[i];
9189 
9190  for (b=0; b<data_bits; b++)
9191  {
9192  if (c & (1<<b)) v=1; else v=0;
9193 
9194  if (v == lev) wf[2][p].usDelay += bitDelay[b+1];
9195  else
9196  {
9197  p++;
9198 
9199  lev = v;
9200 
9201  if (lev)
9202  {
9203  wf[2][p].gpioOn = (1<<gpio);
9204  wf[2][p].gpioOff = 0;
9205  wf[2][p].flags = 0;
9206  }
9207  else
9208  {
9209  wf[2][p].gpioOn = 0;
9210  wf[2][p].gpioOff = (1<<gpio);
9211  wf[2][p].flags = 0;
9212  }
9213 
9214  wf[2][p].usDelay = bitDelay[b+1];
9215  }
9216  }
9217 
9218  /* stop bit */
9219 
9220  if (lev) wf[2][p].usDelay += bitDelay[data_bits+1];
9221  else
9222  {
9223  p++;
9224 
9225  wf[2][p].gpioOn = (1<<gpio);
9226  wf[2][p].gpioOff = 0;
9227  wf[2][p].usDelay = bitDelay[data_bits+1];
9228  wf[2][p].flags = 0;
9229  }
9230  }
9231 
9232  p++;
9233 
9234  wf[2][p].gpioOn = (1<<gpio);
9235  wf[2][p].gpioOff = 0;
9236  wf[2][p].usDelay = bitDelay[0];
9237  wf[2][p].flags = 0;
9238 
9239  return rawWaveAddGeneric(p, wf[2]);
9240 }
9241 
9242 /* ----------------------------------------------------------------------- */
9243 
9245  rawSPI_t *spi,
9246  unsigned offset,
9247  unsigned spiSS,
9248  char *buf,
9249  unsigned spiTxBits,
9250  unsigned spiBitFirst,
9251  unsigned spiBitLast,
9252  unsigned spiBits)
9253 {
9254  int p, bit, dbv, halfbit;
9255  int rising_edge[2], read_cycle[2];
9256  uint32_t on_bits, off_bits;
9257  int tx_bit_pos;
9258 
9259  DBG(DBG_USER,
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);
9263 
9264  CHECK_INITED;
9265 
9266  if (spiSS > PI_MAX_USER_GPIO)
9267  SOFT_ERROR(PI_BAD_USER_GPIO, "bad gpio (%d)", spiSS);
9268 
9269  /*
9270  CPOL CPHA
9271  0 0 read rising/write falling
9272  0 1 read falling/write rising
9273  1 0 read falling/write rising
9274  1 1 read rising/write falling
9275  */
9276 
9277  if (spi->clk_pol) {rising_edge[0] = 0; rising_edge[1] = 1;}
9278  else {rising_edge[0] = 1; rising_edge[1] = 0;}
9279 
9280  if (spi->clk_pha) {read_cycle[0] = 0; read_cycle[1] = 1;}
9281  else {read_cycle[0] = 1; read_cycle[1] = 0;}
9282 
9283  p = 0;
9284 
9285  if (offset)
9286  {
9287  wf[2][p].gpioOn = 0;
9288  wf[2][p].gpioOff = 0;
9289  wf[2][p].flags = 0;
9290  wf[2][p].usDelay = offset;
9291  p++;
9292  }
9293 
9294  on_bits = 0;
9295  off_bits = 0;
9296 
9297  tx_bit_pos = 0;
9298 
9299  /* preset initial mosi bit */
9300 
9301  if (getBitInBytes(tx_bit_pos, buf, spiTxBits))
9302  {
9303  on_bits |= (1<<(spi->mosi));
9304  dbv = 1;
9305  }
9306  else
9307  {
9308  off_bits |= (1<<(spi->mosi));
9309  dbv = 0;
9310  }
9311 
9312  if (!spi->clk_pha) tx_bit_pos ++;
9313 
9314  if (spi->ss_pol) off_bits |= (1<<spiSS);
9315  else on_bits |= (1<<spiSS);
9316 
9317  if (spi->clk_pol) on_bits |= (1<<(spi->clk));
9318  else off_bits |= (1<<(spi->clk));
9319 
9320  wf[2][p].gpioOn = on_bits;
9321  wf[2][p].gpioOff = off_bits;
9322  wf[2][p].flags = 0;
9323 
9324  if (spi->clk_us > spi->ss_us) wf[2][p].usDelay = spi->clk_us;
9325  else wf[2][p].usDelay = spi->ss_us;
9326 
9327  p++;
9328 
9329  for (bit=1; bit<=spiBits; bit++)
9330  {
9331  for (halfbit=0; halfbit<2; halfbit++)
9332  {
9333  wf[2][p].usDelay = spi->clk_us;
9334  wf[2][p].flags = 0;
9335 
9336  on_bits = 0;
9337  off_bits = 0;
9338 
9339  if (read_cycle[halfbit])
9340  {
9341  if ((bit>=spiBitFirst) && (bit<=spiBitLast))
9342  wf[2][p].flags = WAVE_FLAG_READ;
9343  }
9344  else
9345  {
9346  if (getBitInBytes(tx_bit_pos, buf, spiTxBits))
9347  {
9348  if (!dbv) on_bits |= (1<<(spi->mosi));
9349  dbv = 1;
9350  }
9351  else
9352  {
9353  if (dbv) off_bits |= (1<<(spi->mosi));
9354  dbv = 0;
9355  }
9356 
9357  ++tx_bit_pos;
9358  }
9359 
9360  if (rising_edge[halfbit]) on_bits |= (1<<(spi->clk));
9361  else off_bits |= (1<<(spi->clk));
9362 
9363  wf[2][p].gpioOn = on_bits;
9364  wf[2][p].gpioOff = off_bits;
9365 
9366  p++;
9367  }
9368  }
9369 
9370  on_bits = 0;
9371  off_bits = 0;
9372 
9373  if (spi->ss_pol) on_bits |= (1<<spiSS);
9374  else off_bits |= (1<<spiSS);
9375 
9376  wf[2][p].gpioOn = on_bits;
9377  wf[2][p].gpioOff = off_bits;
9378  wf[2][p].flags = 0;
9379  wf[2][p].usDelay = 0;
9380 
9381  p++;
9382 
9383  return rawWaveAddGeneric(p, wf[2]);
9384 }
9385 
9386 /* ----------------------------------------------------------------------- */
9387 
9389 {
9390  int i, wid;
9391  int numCB, numBOOL, numTOOL;
9392  int CB, BOOL, TOOL;
9393 
9394  DBG(DBG_USER, "");
9395 
9396  CHECK_INITED;
9397 
9398  if (wfc[wfcur] == 0) return PI_EMPTY_WAVEFORM;
9399 
9400  /* What resources are needed? */
9401 
9402  waveCBsOOLs(&numCB, &numBOOL, &numTOOL);
9403 
9404  wid = -1;
9405 
9406  /* Is there an exact fit with a deleted wave. */
9407 
9408  for (i=0; i<waveOutCount; i++)
9409  {
9410  if (waveInfo[i].deleted &&
9411  (waveInfo[i].numCB == numCB) &&
9412  (waveInfo[i].numBOOL == numBOOL) &&
9413  (waveInfo[i].numTOOL == numTOOL))
9414  {
9415  /* Reuse the deleted waves resources. */
9416  wid = i;
9417  break;
9418  }
9419  }
9420 
9421  if (wid == -1)
9422  {
9423  /* Are there enough spare resources? */
9424 
9425  if ((numCB+waveOutBotCB) >= NUM_WAVE_CBS)
9426  return PI_TOO_MANY_CBS;
9427 
9428  if ((numBOOL+waveOutBotOOL) >= (waveOutTopOOL-numTOOL))
9429  return PI_TOO_MANY_OOL;
9430 
9431  if (wid >= PI_MAX_WAVES)
9432  return PI_NO_WAVEFORM_ID;
9433 
9434  wid = waveOutCount++;
9435 
9436  waveInfo[wid].botCB = waveOutBotCB;
9437  waveInfo[wid].topCB = waveOutBotCB + numCB -1;
9438  waveInfo[wid].botOOL = waveOutBotOOL;
9439  waveInfo[wid].topOOL = waveOutTopOOL;
9440  waveInfo[wid].numCB = numCB;
9441  waveInfo[wid].numBOOL = numBOOL;
9442  waveInfo[wid].numTOOL = numTOOL;
9443 
9444  waveOutBotCB += numCB;
9445  waveOutBotOOL += numBOOL;
9446  waveOutTopOOL -= numTOOL;
9447  }
9448 
9449  /* Must be room if got this far. */
9450 
9451  CB = waveInfo[wid].botCB;
9452  BOOL = waveInfo[wid].botOOL;
9453  TOOL = waveInfo[wid].topOOL;
9454 
9455  wave2Cbs(PI_WAVE_MODE_ONE_SHOT, &CB, &BOOL, &TOOL);
9456 
9457  /* Sanity check. */
9458 
9459  if ( (numCB != (CB-waveInfo[wid].botCB)) ||
9460  (numBOOL != (BOOL-waveInfo[wid].botOOL)) ||
9461  (numTOOL != (waveInfo[wid].topOOL-TOOL)) )
9462  {
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);
9467  }
9468 
9469  waveInfo[wid].deleted = 0;
9470 
9471  /* Consume waves. */
9472 
9473  wfc[0] = 0;
9474  wfc[1] = 0;
9475  wfc[2] = 0;
9476 
9477  wfcur = 0;
9478 
9479  return wid;
9480 }
9481 
9482 /* ----------------------------------------------------------------------- */
9483 
9484 int gpioWaveDelete(unsigned wave_id)
9485 {
9486  DBG(DBG_USER, "wave id=%d", wave_id);
9487 
9488  CHECK_INITED;
9489 
9490  if ((wave_id >= waveOutCount) || waveInfo[wave_id].deleted)
9491  SOFT_ERROR(PI_BAD_WAVE_ID, "bad wave id (%d)", wave_id);
9492 
9493  waveInfo[wave_id].deleted = 1;
9494 
9495  if (wave_id == (waveOutCount-1))
9496  {
9497  /* top wave deleted, garbage collect any other deleted waves */
9498 
9499  while ((wave_id > 0) && (waveInfo[wave_id-1].deleted)) --wave_id;
9500 
9501  waveOutBotCB = waveInfo[wave_id].botCB;
9502  waveOutBotOOL = waveInfo[wave_id].botOOL;
9503  waveOutTopOOL = waveInfo[wave_id].topOOL;
9504 
9505  waveOutCount = wave_id;
9506  }
9507 
9508  return 0;
9509 }
9510 
9511 /* ----------------------------------------------------------------------- */
9512 
9513 int gpioWaveTxStart(unsigned wave_mode)
9514 {
9515  /* This function is deprecated and has been removed. */
9516 
9517  CHECK_INITED;
9518 
9519  SOFT_ERROR(PI_DEPRECATED, "deprected function removed");
9520 }
9521 
9522 /* ----------------------------------------------------------------------- */
9523 
9524 int gpioWaveTxSend(unsigned wave_id, unsigned wave_mode)
9525 {
9526  rawCbs_t *p=NULL;
9527 
9528  DBG(DBG_USER, "wave_id=%d wave_mode=%d", wave_id, wave_mode);
9529 
9530  CHECK_INITED;
9531 
9532  if ((wave_id >= waveOutCount) || waveInfo[wave_id].deleted)
9533  SOFT_ERROR(PI_BAD_WAVE_ID, "bad wave id (%d)", wave_id);
9534 
9535  if (wave_mode > PI_WAVE_MODE_REPEAT_SYNC)
9536  SOFT_ERROR(PI_BAD_WAVE_MODE, "bad wave mode (%d)", wave_mode);
9537 
9538  if (!waveClockInited)
9539  {
9540  stopHardwarePWM();
9541  initClock(0); /* initialise secondary clock */
9542  waveClockInited = 1;
9543  PWMClockInited = 0;
9544  }
9545 
9546  if (wave_mode < PI_WAVE_MODE_ONE_SHOT_SYNC)
9547  {
9549  dmaOut[DMA_CONBLK_AD] = 0;
9550  }
9551 
9552  p = rawWaveCBAdr(waveInfo[wave_id].topCB);
9553 
9554  if ((wave_mode & 1) == PI_WAVE_MODE_ONE_SHOT)
9555  p->next = 0;
9556  else
9557  p->next = waveCbPOadr(waveInfo[wave_id].botCB+1);
9558 
9559  if (waveEndPtr && (wave_mode > PI_WAVE_MODE_REPEAT))
9560  {
9561  *waveEndPtr = waveCbPOadr(waveInfo[wave_id].botCB+1);
9562 
9563  if (!dmaOut[DMA_CONBLK_AD])
9564  {
9565  initDMAgo((uint32_t *)dmaOut, waveCbPOadr(waveInfo[wave_id].botCB));
9566  }
9567  }
9568  else
9569  {
9570  initDMAgo((uint32_t *)dmaOut, waveCbPOadr(waveInfo[wave_id].botCB));
9571  }
9572 
9573  waveEndPtr = &p->next;
9574 
9575  /* for compatability with the deprecated gpioWaveTxStart return the
9576  number of cbs
9577  */
9578  return (waveInfo[wave_id].topCB - waveInfo[wave_id].botCB) + 1;
9579 }
9580 
9581 
9582 /* ----------------------------------------------------------------------- */
9583 
9584 static int chainGetCB(int n)
9585 {
9586  int block, index;
9587 
9588  if (n < (WCB_CHAIN_CBS * PI_WAVE_COUNT_PAGES))
9589  {
9590  block = n / WCB_CHAIN_CBS;
9591  index = n % WCB_CHAIN_CBS;
9592  return (block*CBS_PER_OPAGE) + WCB_COUNTER_CBS + index;
9593  }
9594  return -1;
9595 }
9596 
9597 static void chainSetVal(int n, uint32_t val)
9598 {
9599  int block, index;
9600  uint32_t *p;
9601 
9602  if (n < (WCB_CHAIN_OOL * PI_WAVE_COUNT_PAGES))
9603  {
9604  block = n / WCB_CHAIN_OOL;
9605  index = n % WCB_CHAIN_OOL;
9606  p = (uint32_t *) dmaOVirt[block] + (WCB_COUNTER_CBS+WCB_CHAIN_CBS) * 8;
9607  p[index] = val;
9608  }
9609 }
9610 
9611 static uint32_t chainGetValPadr(int n)
9612 {
9613  int block, index;
9614  uint32_t *p;
9615 
9616  if (n < (WCB_CHAIN_OOL * PI_WAVE_COUNT_PAGES))
9617  {
9618  block = n / WCB_CHAIN_OOL;
9619  index = n % WCB_CHAIN_OOL;
9620  p = (uint32_t *) dmaOBus[block] + (WCB_COUNTER_CBS+WCB_CHAIN_CBS) * 8;
9621  return (uint32_t) (p + index);
9622  }
9623  return 0;
9624 }
9625 
9626 static uint32_t chainGetCntVal(int counter, int slot)
9627 {
9628  uint32_t *p;
9629  int page, offset;
9630  page = counter / 2;
9631  offset = (counter % 2 ? WCB_COUNTER_OOL : 0);
9632  p = (uint32_t *) dmaOVirt[page] + (WCB_COUNTER_CBS+WCB_CHAIN_CBS) * 8;
9633  return p[WCB_CHAIN_OOL+ offset + slot];
9634 }
9635 
9636 static void chainSetCntVal(int counter, int slot, uint32_t value)
9637 {
9638  uint32_t *p;
9639  int page, offset;
9640  page = counter / 2;
9641  offset = (counter % 2 ? WCB_COUNTER_OOL : 0);
9642  p = (uint32_t *) dmaOVirt[page] + (WCB_COUNTER_CBS+WCB_CHAIN_CBS) * 8;
9643  p[WCB_CHAIN_OOL + offset + slot] = value;
9644 }
9645 
9646 static uint32_t chainGetCntValPadr(int counter, int slot)
9647 {
9648  uint32_t *p;
9649  int page, offset;
9650  page = counter / 2;
9651  offset = (counter % 2 ? WCB_COUNTER_OOL : 0);
9652  p = (uint32_t *) dmaOBus[page] + (WCB_COUNTER_CBS+WCB_CHAIN_CBS) * 8;
9653  return (uint32_t)(p + WCB_CHAIN_OOL + offset + slot);
9654 }
9655 
9656 static int chainGetCntCB(int counter)
9657 {
9658  int page, offset;
9659  page = counter / 2;
9660  offset = (counter % 2 ? WCB_CNT_CBS : 0);
9661  return ((page * CBS_PER_OPAGE) + offset);
9662 }
9663 
9664 static void chainMakeCounter(
9665  unsigned counter,
9666  unsigned blklen,
9667  unsigned blocks,
9668  unsigned count,
9669  uint32_t repeat,
9670  uint32_t next)
9671 {
9672  rawCbs_t *p=NULL;
9673 
9674  int b, baseCB, dig;
9675  uint32_t nxt;
9676 
9677  int botCB;
9678 
9679  botCB = chainGetCntCB(counter);
9680 
9681  baseCB = botCB;
9682 
9683  /* set up all the OOLs */
9684  for (b=0; b < (blocks*(blklen+1)); b++) chainSetCntVal(counter, b, repeat);
9685 
9686  for (b=0; b<blocks; b++)
9687  chainSetCntVal(counter,
9688  ((b*(blklen+1))+blklen),
9689  waveCbPOadr(baseCB+((b*3)+3)));
9690 
9691  for (b=0; b<blocks; b++)
9692  {
9693  /* copy BOTTOM to NEXT */
9694 
9695  p = rawWaveCBAdr(botCB++);
9696 
9697  p->info = NORMAL_DMA;
9698 
9699  p->src = chainGetCntValPadr(counter, b*(blklen+1));
9700  p->dst = (waveCbPOadr(botCB+1) + 20);
9701 
9702  p->length = 4;
9703  p->next = waveCbPOadr(botCB);
9704 
9705  /* copy BOTTOM to TOP */
9706 
9707  p = rawWaveCBAdr(botCB++);
9708 
9709  p->info = NORMAL_DMA;
9710 
9711  p->src = chainGetCntValPadr(counter, b*(blklen+1));
9712  p->dst = chainGetCntValPadr(counter, (b*(blklen+1))+blklen);
9713 
9714  p->length = 4;
9715  p->next = waveCbPOadr(botCB);
9716 
9717  /* shift all down one */
9718 
9719  p = rawWaveCBAdr(botCB++);
9720 
9722 
9723  p->src = chainGetCntValPadr(counter, ((b*(blklen+1))+1));
9724  p->dst = chainGetCntValPadr(counter, ((b*(blklen+1))+0));
9725 
9726  p->length = blklen*4;
9727  p->next = repeat;
9728  }
9729 
9730  /* reset the counter */
9731 
9732  p = rawWaveCBAdr(botCB);
9733 
9735 
9736  p->src = chainGetCntValPadr(counter, blocks*(blklen+1));
9737  p->dst = chainGetCntValPadr(counter, 0);
9738 
9739  p->length = blocks*(blklen+1)*4;
9740  p->next = next;
9741 
9742  b = 0;
9743 
9744  while (count && (b<blocks))
9745  {
9746  dig = count % blklen;
9747  count /= blklen;
9748 
9749  if (count) nxt = chainGetCntVal(counter, (b*(blklen+1))+blklen);
9750  else nxt = waveCbPOadr(botCB);
9751 
9752  chainSetCntVal(counter, b*(blklen+1)+dig, nxt);
9753 
9754  b++;
9755  }
9756 
9757  /* copy all the OOLs */
9758  for (b=0; b < (blocks*(blklen+1)); b++)
9760  counter, b+(blocks*(blklen+1)), chainGetCntVal(counter, b));
9761 }
9762 
9763 
9764 int gpioWaveChain(char *buf, unsigned bufSize)
9765 {
9766  unsigned blklen=16, blocks=4;
9767  int cb, chaincb;
9768  rawCbs_t *p;
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;
9773 
9774  cb = 0;
9775  loop = -1;
9776 
9777  DBG(DBG_USER, "bufSize=%d [%s]", bufSize, myBuf2Str(bufSize, buf));
9778 
9779  CHECK_INITED;
9780 
9781  if (!waveClockInited)
9782  {
9783  stopHardwarePWM();
9784  initClock(0); /* initialise secondary clock */
9785  waveClockInited = 1;
9786  PWMClockInited = 0;
9787  }
9788 
9790  dmaOut[DMA_CONBLK_AD] = 0;
9791  waveEndPtr = NULL;
9792  endPtr = NULL;
9793 
9794  /* add delay cb at start of DMA */
9795 
9796  p = rawWaveCBAdr(chainGetCB(cb++));
9797 
9798  /* use the secondary clock */
9799 
9800  if (gpioCfg.clockPeriph != PI_CLOCK_PCM)
9801  {
9802  p->info = NORMAL_DMA | TIMED_DMA(2);
9803  p->dst = PCM_TIMER;
9804  }
9805  else
9806  {
9807  p->info = NORMAL_DMA | TIMED_DMA(5);
9808  p->dst = PWM_TIMER;
9809  }
9810 
9811  p->src = (uint32_t) (&dmaOBus[0]->periphData);
9812  p->length = BPD * 20 / PI_WF_MICROS; /* 20 micros delay */
9813  p->next = waveCbPOadr(chainGetCB(cb));
9814 
9815  counters = 0;
9816  wid = -1;
9817 
9818  i = 0;
9819 
9820  while (i<bufSize)
9821  {
9822  wid = (unsigned)buf[i];
9823 
9824  if (wid == 255) /* wave command */
9825  {
9826  if ((i+2) > bufSize)
9828  "incomplete chain command (at %d)", i);
9829 
9830  cmd = buf[i+1];
9831 
9832  if (cmd == 0) /* loop begin */
9833  {
9834  if (stk_lev >= (sizeof(stk_pos)/sizeof(int)))
9836  "chain counters nested too deep (at %d)", i);
9837 
9838  stk_pos[stk_lev++] = cb;
9839 
9840  i += 2;
9841  }
9842  else if (cmd == 1) /* loop end */
9843  {
9844  if (counters >= WCB_COUNTERS)
9846  "too many chain counters (at %d)", i);
9847 
9848  if ((i+4) > bufSize)
9850  "incomplete chain command (at %d)", i);
9851 
9852  loop = 0;
9853  if (--stk_lev >= 0) loop = stk_pos[stk_lev];
9854 
9855  if ((loop < 1) || (loop == cb))
9857  "empty chain loop (at %d)", i);
9858 
9859  cycles = ((unsigned)buf[i+3] << 8) + (unsigned)buf[i+2];
9860 
9861  i += 4;
9862 
9863  if (cycles > PI_MAX_WAVE_CYCLES)
9865  "bad chain loop count (%d)", cycles);
9866 
9867  if (cycles == 0)
9868  {
9869  /* Skip the complete loop block. Change
9870  the next pointing to the start of the
9871  loop block to the current cb.
9872  */
9873  p = rawWaveCBAdr(chainGetCB(loop));
9874  p->next = waveCbPOadr(chainGetCB(cb));
9875  }
9876  else if (cycles == 1)
9877  {
9878  /* Nothing to do, no need for a counter. */
9879  }
9880  else
9881  {
9882  chaincb = chainGetCB(cb++);
9883  if (chaincb < 0)
9884  SOFT_ERROR(PI_CHAIN_TOO_BIG, "chain is too long (%d)", cb);
9885 
9886  p = rawWaveCBAdr(chaincb);
9887 
9888  repeat = waveCbPOadr(chainGetCB(loop));
9889 
9890  /* Need to check next cb as well. */
9891 
9892  chaincb = chainGetCB(cb);
9893 
9894  if (chaincb < 0)
9895  SOFT_ERROR(PI_CHAIN_TOO_BIG, "chain is too long (%d)", cb);
9896 
9897  next = waveCbPOadr(chainGetCB(cb));
9898 
9899  /* dummy src and dest */
9900  p->info = NORMAL_DMA;
9901  p->src = (uint32_t) (&dmaOBus[0]->periphData);
9902  p->dst = (uint32_t) (&dmaOBus[0]->periphData);
9903  p->length = 4;
9904  p->next = waveCbPOadr(chainGetCntCB(counters));
9905 
9906  chainMakeCounter(counters, blklen, blocks,
9907  cycles-1, repeat, next);
9908 
9909  counters++;
9910  }
9911  }
9912  else if (cmd == 2) /* delay us */
9913  {
9914  if ((i+4) > bufSize)
9916  "incomplete chain command (at %d)", i);
9917 
9918  cycles = ((unsigned)buf[i+3] << 8) + (unsigned)buf[i+2];
9919 
9920  i += 4;
9921 
9922  if (cycles > PI_MAX_WAVE_DELAY)
9924  "bad chain delay micros (%d)", cycles);
9925 
9926  if (cycles)
9927  {
9928  delayLeft = cycles;
9929  delayCBs = waveDelayCBs(delayLeft);
9930  for (dcb=0; dcb<delayCBs; dcb++)
9931  {
9932  chaincb = chainGetCB(cb++);
9933 
9934  if (chaincb < 0)
9935  SOFT_ERROR(
9936  PI_CHAIN_TOO_BIG, "chain is too long (%d)", cb);
9937 
9938  p = rawWaveCBAdr(chaincb);
9939 
9940  /* use the secondary clock */
9941 
9942  if (gpioCfg.clockPeriph != PI_CLOCK_PCM)
9943  {
9944  p->info = NORMAL_DMA | TIMED_DMA(2);
9945  p->dst = PCM_TIMER;
9946  }
9947  else
9948  {
9949  p->info = NORMAL_DMA | TIMED_DMA(5);
9950  p->dst = PWM_TIMER;
9951  }
9952 
9953  p->src = (uint32_t) (&dmaOBus[0]->periphData);
9954 
9955  p->length = BPD * delayLeft / PI_WF_MICROS;
9956 
9957  if ((gpioCfg.DMAsecondaryChannel >= DMA_LITE_FIRST) &&
9958  (p->length > DMA_LITE_MAX))
9959  {
9960  p->length = DMA_LITE_MAX;
9961  }
9962 
9963  delayLeft -= (p->length / BPD);
9964 
9965  p->next = waveCbPOadr(chainGetCB(cb));
9966  }
9967  }
9968  }
9969  else if (cmd == 3) /* repeat loop forever */
9970  {
9971  i += 2;
9972 
9973  loop = 0;
9974  if (--stk_lev >= 0) loop = stk_pos[stk_lev];
9975 
9976  if ((loop < 1) || (loop == cb))
9978  "empty chain loop (at %d)", i);
9979 
9980  chaincb = chainGetCB(cb++);
9981  if (chaincb < 0)
9982  SOFT_ERROR(PI_CHAIN_TOO_BIG, "chain is too long (%d)", cb);
9983 
9984  if (i < bufSize)
9986  "loop forever must be last command");
9987 
9988  p = rawWaveCBAdr(chaincb);
9989 
9990  /* dummy src and dest */
9991  p->info = NORMAL_DMA;
9992  p->src = (uint32_t) (&dmaOBus[0]->periphData);
9993  p->dst = (uint32_t) (&dmaOBus[0]->periphData);
9994  p->length = 4;
9995  p->next = waveCbPOadr(chainGetCB(loop));
9996  endPtr = &p->next;
9997  }
9998  else
10000  "unknown chain command (255 %d)", cmd);
10001  }
10002  else if ((wid >= waveOutCount) || waveInfo[wid].deleted)
10003  SOFT_ERROR(PI_BAD_WAVE_ID, "undefined wave (%d)", wid);
10004  else
10005  {
10006  chaincb = chainGetCB(cb++);
10007 
10008  if (chaincb < 0)
10009  SOFT_ERROR(PI_CHAIN_TOO_BIG, "chain is too long (%d)", cb);
10010 
10011  p = rawWaveCBAdr(chaincb);
10012 
10013  chaincb = chainGetCB(cb);
10014 
10015  if (chaincb < 0)
10016  SOFT_ERROR(PI_CHAIN_TOO_BIG, "chain is too long (%d)", cb);
10017 
10018  chainSetVal(cb-1, waveCbPOadr(chaincb));
10019 
10020  /* patch next of wid topCB to next cb */
10021 
10022  p->info = NORMAL_DMA;
10023  p->src = chainGetValPadr(cb-1); /* this next */
10024  p->dst = waveCbPOadr(waveInfo[wid].topCB) + 20; /* wid next */
10025  p->length = 4;
10026  p->next = waveCbPOadr(waveInfo[wid].botCB+1);
10027 
10028  i += 1;
10029  }
10030  }
10031 
10032  chaincb = chainGetCB(cb++);
10033 
10034  if (chaincb < 0)
10035  SOFT_ERROR(PI_CHAIN_TOO_BIG, "chain is too long (%d)", cb);
10036 
10037  p = rawWaveCBAdr(chaincb);
10038 
10039  p->info = NORMAL_DMA;
10040  p->src = (uint32_t) (&dmaOBus[0]->periphData);
10041  p->dst = (uint32_t) (&dmaOBus[0]->periphData);
10042  p->length = 4;
10043  p->next = 0;
10044 
10045  if (!endPtr) endPtr = &p->next;
10046 
10047  initDMAgo((uint32_t *)dmaOut, waveCbPOadr(chainGetCB(0)));
10048 
10049  waveEndPtr = endPtr;
10050 
10051  return 0;
10052 }
10053 
10054 /*-------------------------------------------------------------------------*/
10055 
10057 {
10058  DBG(DBG_USER, "");
10059 
10060  CHECK_INITED;
10061 
10062  if (dmaOut[DMA_CONBLK_AD])
10063  return 1;
10064  else
10065  return 0;
10066 }
10067 
10068 /*-------------------------------------------------------------------------*/
10069 
10070 int gpioWaveTxAt(void)
10071 {
10072  int i, cb;
10073 
10074  DBG(DBG_USER, "");
10075 
10076  CHECK_INITED;
10077 
10078  cb = dmaNowAtOCB();
10079 
10080  if (cb < 0) return -cb;
10081 
10082  for (i=0; i<PI_MAX_WAVES; i++)
10083  {
10084  if ( !waveInfo[i].deleted &&
10085  (cb >= waveInfo[i].botCB) &&
10086  (cb <= waveInfo[i].topCB) ) return i;
10087  }
10088 
10089  return PI_WAVE_NOT_FOUND;
10090 }
10091 
10092 /* ----------------------------------------------------------------------- */
10093 
10095 {
10096  DBG(DBG_USER, "");
10097 
10098  CHECK_INITED;
10099 
10101  dmaOut[DMA_CONBLK_AD] = 0;
10102 
10103  waveEndPtr = NULL;
10104 
10105  return 0;
10106 }
10107 
10108 /* ----------------------------------------------------------------------- */
10109 
10111 {
10112  DBG(DBG_USER, "");
10113 
10114  CHECK_INITED;
10115 
10116  return wfStats.micros;
10117 }
10118 
10119 /* ----------------------------------------------------------------------- */
10120 
10122 {
10123  DBG(DBG_USER, "");
10124 
10125  CHECK_INITED;
10126 
10127  return wfStats.highMicros;
10128 }
10129 
10130 /* ----------------------------------------------------------------------- */
10131 
10133 {
10134  DBG(DBG_USER, "");
10135 
10136  CHECK_INITED;
10137 
10138  return wfStats.maxMicros;
10139 }
10140 
10141 /* ----------------------------------------------------------------------- */
10142 
10144 {
10145  DBG(DBG_USER, "");
10146 
10147  CHECK_INITED;
10148 
10149  return wfStats.pulses;
10150 }
10151 
10152 /* ----------------------------------------------------------------------- */
10153 
10155 {
10156  DBG(DBG_USER, "");
10157 
10158  CHECK_INITED;
10159 
10160  return wfStats.highPulses;
10161 }
10162 
10163 /* ----------------------------------------------------------------------- */
10164 
10166 {
10167  DBG(DBG_USER, "");
10168 
10169  CHECK_INITED;
10170 
10171  return wfStats.maxPulses;
10172 }
10173 
10174 /* ----------------------------------------------------------------------- */
10175 
10177 {
10178  DBG(DBG_USER, "");
10179 
10180  CHECK_INITED;
10181 
10182  return wfStats.cbs;
10183 }
10184 
10185 /* ----------------------------------------------------------------------- */
10186 
10188 {
10189  DBG(DBG_USER, "");
10190 
10191  CHECK_INITED;
10192 
10193  return wfStats.highCbs;
10194 }
10195 
10196 /* ----------------------------------------------------------------------- */
10197 
10199 {
10200  DBG(DBG_USER, "");
10201 
10202  CHECK_INITED;
10203 
10204  return wfStats.maxCbs;
10205 }
10206 
10207 /* ----------------------------------------------------------------------- */
10208 
10209 static int read_SDA(wfRx_t *w)
10210 {
10211  myGpioSetMode(w->I.SDA, PI_INPUT);
10212  return myGpioRead(w->I.SDA);
10213 }
10214 
10215 static void set_SDA(wfRx_t *w)
10216 {
10217  myGpioSetMode(w->I.SDA, PI_INPUT);
10218 }
10219 
10220 static void clear_SDA(wfRx_t *w)
10221 {
10223  myGpioWrite(w->I.SDA, 0);
10224 }
10225 
10226 static void clear_SCL(wfRx_t *w)
10227 {
10229  myGpioWrite(w->I.SCL, 0);
10230 }
10231 
10232 static void I2C_delay(wfRx_t *w)
10233 {
10234  myGpioDelay(w->I.delay);
10235 }
10236 
10238 {
10239  uint32_t now, max_stretch=100000;
10240 
10241  myGpioSetMode(w->I.SCL, PI_INPUT);
10242  now = gpioTick();
10243  while ((myGpioRead(w->I.SCL) == 0) && ((gpioTick()-now) < max_stretch));
10244 }
10245 
10246 static void I2CStart(wfRx_t *w)
10247 {
10248  if (w->I.started)
10249  {
10250  set_SDA(w);
10251  I2C_delay(w);
10252  I2C_clock_stretch(w);
10253  I2C_delay(w);
10254  }
10255 
10256  clear_SDA(w);
10257  I2C_delay(w);
10258  clear_SCL(w);
10259  I2C_delay(w);
10260 
10261  w->I.started = 1;
10262 }
10263 
10264 static void I2CStop(wfRx_t *w)
10265 {
10266  clear_SDA(w);
10267  I2C_delay(w);
10268  I2C_clock_stretch(w);
10269  I2C_delay(w);
10270  set_SDA(w);
10271  I2C_delay(w);
10272 
10273  w->I.started = 0;
10274 }
10275 
10276 static void I2CPutBit(wfRx_t *w, int bit)
10277 {
10278  if (bit) set_SDA(w);
10279  else clear_SDA(w);
10280 
10281  I2C_delay(w);
10282  I2C_clock_stretch(w);
10283  I2C_delay(w);
10284  clear_SCL(w);
10285 }
10286 
10287 static int I2CGetBit(wfRx_t *w)
10288 {
10289  int bit;
10290 
10291  set_SDA(w); /* let SDA float */
10292  I2C_delay(w);
10293  I2C_clock_stretch(w);
10294  bit = read_SDA(w);
10295  I2C_delay(w);
10296  clear_SCL(w);
10297 
10298  return bit;
10299 }
10300 
10301 static int I2CPutByte(wfRx_t *w, int byte)
10302 {
10303  int bit, nack;
10304 
10305  for(bit=0; bit<8; bit++)
10306  {
10307  I2CPutBit(w, byte & 0x80);
10308  byte <<= 1;
10309  }
10310 
10311  nack = I2CGetBit(w);
10312 
10313  return nack;
10314 }
10315 
10316 static uint8_t I2CGetByte(wfRx_t *w, int nack)
10317 {
10318  int bit, byte=0;
10319 
10320  for (bit=0; bit<8; bit++)
10321  {
10322  byte = (byte << 1) | I2CGetBit(w);
10323  }
10324 
10325  I2CPutBit(w, nack);
10326 
10327  return byte;
10328 }
10329 
10330 /*-------------------------------------------------------------------------*/
10331 
10332 int bbI2COpen(unsigned SDA, unsigned SCL, unsigned baud)
10333 {
10334  DBG(DBG_USER, "SDA=%d SCL=%d baud=%d", SDA, SCL, baud);
10335 
10336  CHECK_INITED;
10337 
10338  if (SDA > PI_MAX_USER_GPIO)
10339  SOFT_ERROR(PI_BAD_USER_GPIO, "bad SDA (%d)", SDA);
10340 
10341  if (SCL > PI_MAX_USER_GPIO)
10342  SOFT_ERROR(PI_BAD_USER_GPIO, "bad SCL (%d)", SCL);
10343 
10344  if ((baud < PI_BB_I2C_MIN_BAUD) || (baud > PI_BB_I2C_MAX_BAUD))
10346  "SDA %d, bad baud rate (%d)", SDA, baud);
10347 
10348  if (wfRx[SDA].mode != PI_WFRX_NONE)
10349  SOFT_ERROR(PI_GPIO_IN_USE, "gpio %d is already being used", SDA);
10350 
10351  if ((wfRx[SCL].mode != PI_WFRX_NONE) || (SCL == SDA))
10352  SOFT_ERROR(PI_GPIO_IN_USE, "gpio %d is already being used", SCL);
10353 
10354  wfRx[SDA].gpio = SDA;
10355  wfRx[SDA].mode = PI_WFRX_I2C_SDA;
10356  wfRx[SDA].baud = baud;
10357 
10358  wfRx[SDA].I.started = 0;
10359  wfRx[SDA].I.SDA = SDA;
10360  wfRx[SDA].I.SCL = SCL;
10361  wfRx[SDA].I.delay = 500000 / baud;
10362  wfRx[SDA].I.SDAMode = gpioGetMode(SDA);
10363  wfRx[SDA].I.SCLMode = gpioGetMode(SCL);
10364 
10365  wfRx[SCL].gpio = SCL;
10366  wfRx[SCL].mode = PI_WFRX_I2C_SCL;
10367 
10368  myGpioSetMode(SDA, PI_INPUT);
10369  myGpioSetMode(SCL, PI_INPUT);
10370 
10371  return 0;
10372 }
10373 
10374 /* ----------------------------------------------------------------------- */
10375 
10376 int bbI2CClose(unsigned SDA)
10377 {
10378  DBG(DBG_USER, "SDA=%d", SDA);
10379 
10380  CHECK_INITED;
10381 
10382  if (SDA > PI_MAX_USER_GPIO)
10383  SOFT_ERROR(PI_BAD_USER_GPIO, "bad gpio (%d)", SDA);
10384 
10385  switch(wfRx[SDA].mode)
10386  {
10387  case PI_WFRX_I2C_SDA:
10388 
10389  myGpioSetMode(wfRx[SDA].I.SDA, wfRx[SDA].I.SDAMode);
10390  myGpioSetMode(wfRx[SDA].I.SCL, wfRx[SDA].I.SCLMode);
10391 
10392  wfRx[wfRx[SDA].I.SDA].mode = PI_WFRX_NONE;
10393  wfRx[wfRx[SDA].I.SCL].mode = PI_WFRX_NONE;
10394 
10395  break;
10396 
10397  default:
10398 
10399  SOFT_ERROR(PI_NOT_I2C_GPIO, "no I2C on gpio (%d)", SDA);
10400 
10401  break;
10402 
10403  }
10404 
10405  return 0;
10406 }
10407 
10408 /*-------------------------------------------------------------------------*/
10409 
10411  unsigned SDA,
10412  char *inBuf,
10413  unsigned inLen,
10414  char *outBuf,
10415  unsigned outLen)
10416 {
10417  int i, ack, inPos, outPos, status, bytes;
10418  int addr, flags, esc, setesc;
10419  wfRx_t *w;
10420 
10421  DBG(DBG_USER, "gpio=%d inBuf=%s outBuf=%08X len=%d",
10422  SDA, myBuf2Str(inLen, (char *)inBuf), (int)outBuf, outLen);
10423 
10424  CHECK_INITED;
10425 
10426  if (SDA > PI_MAX_USER_GPIO)
10427  SOFT_ERROR(PI_BAD_USER_GPIO, "bad gpio (%d)", SDA);
10428 
10429  if (wfRx[SDA].mode != PI_WFRX_I2C_SDA)
10430  SOFT_ERROR(PI_NOT_I2C_GPIO, "no I2C on gpio (%d)", SDA);
10431 
10432  if (!inBuf || !inLen)
10433  SOFT_ERROR(PI_BAD_POINTER, "input buffer can't be NULL");
10434 
10435  if (!outBuf && outLen)
10436  SOFT_ERROR(PI_BAD_POINTER, "output buffer can't be NULL");
10437 
10438  w = &wfRx[SDA];
10439 
10440  inPos = 0;
10441  outPos = 0;
10442  status = 0;
10443 
10444  addr = 0;
10445  flags = 0;
10446  esc = 0;
10447  setesc = 0;
10448 
10449  wfRx_lock(SDA);
10450 
10451  while (!status && (inPos < inLen))
10452  {
10453  DBG(DBG_INTERNAL, "status=%d inpos=%d inlen=%d cmd=%d addr=%d flags=%x",
10454  status, inPos, inLen, inBuf[inPos], addr, flags);
10455 
10456  switch (inBuf[inPos++])
10457  {
10458  case PI_I2C_END:
10459  status = 1;
10460  break;
10461 
10462  case PI_I2C_START:
10463  I2CStart(w);
10464  break;
10465 
10466  case PI_I2C_STOP:
10467  I2CStop(w);
10468  break;
10469 
10470  case PI_I2C_ADDR:
10471  addr = myI2CGetPar(inBuf, &inPos, inLen, &esc);
10472  if (addr < 0) status = PI_BAD_I2C_CMD;
10473  break;
10474 
10475  case PI_I2C_FLAGS:
10476  /* cheat to force two byte flags */
10477  esc = 1;
10478  flags = myI2CGetPar(inBuf, &inPos, inLen, &esc);
10479  if (flags < 0) status = PI_BAD_I2C_CMD;
10480  break;
10481 
10482  case PI_I2C_ESC:
10483  setesc = 1;
10484  break;
10485 
10486  case PI_I2C_READ:
10487 
10488  bytes = myI2CGetPar(inBuf, &inPos, inLen, &esc);
10489 
10490  if (bytes >= 0) ack = I2CPutByte(w, (addr<<1)|1);
10491 
10492  if (bytes > 0)
10493  {
10494  if (!ack)
10495  {
10496  if ((bytes + outPos) <= outLen)
10497  {
10498  for (i=0; i<(bytes-1); i++)
10499  {
10500  outBuf[outPos++] = I2CGetByte(w, 0);
10501  }
10502  outBuf[outPos++] = I2CGetByte(w, 1);
10503  }
10504  else status = PI_BAD_I2C_RLEN;
10505  }
10506  else status = PI_I2C_READ_FAILED;
10507  }
10508  else status = PI_BAD_I2C_CMD;
10509  break;
10510 
10511  case PI_I2C_WRITE:
10512 
10513  bytes = myI2CGetPar(inBuf, &inPos, inLen, &esc);
10514 
10515  if (bytes >= 0) ack = I2CPutByte(w, addr<<1);
10516 
10517  if (bytes > 0)
10518  {
10519  if (!ack)
10520  {
10521  if ((bytes + inPos) <= inLen)
10522  {
10523  for (i=0; i<(bytes-1); i++)
10524  {
10525  ack = I2CPutByte(w, inBuf[inPos++]);
10526  if (ack) status = PI_I2C_WRITE_FAILED;
10527  }
10528  ack = I2CPutByte(w, inBuf[inPos++]);
10529  }
10530  else status = PI_BAD_I2C_WLEN;
10531  } else status = PI_I2C_WRITE_FAILED;
10532  }
10533  else status = PI_BAD_I2C_CMD;
10534  break;
10535 
10536  default:
10537  status = PI_BAD_I2C_CMD;
10538  }
10539 
10540  if (setesc) esc = 1; else esc = 0;
10541 
10542  setesc = 0;
10543  }
10544 
10545  wfRx_unlock(SDA);
10546 
10547  if (status >= 0) status = outPos;
10548 
10549  return status;
10550 }
10551 
10552 /* ----------------------------------------------------------------------- */
10553 
10554 void bscInit(int mode)
10555 {
10556  bscsReg[BSC_CR]=0; /* clear device */
10557  bscsReg[BSC_RSR]=0; /* clear underrun and overrun errors */
10558  bscsReg[BSC_SLV]=0; /* clear I2C slave address */
10559  bscsReg[BSC_IMSC]=0xf; /* mask off all interrupts */
10560  bscsReg[BSC_ICR]=0x0f; /* clear all interrupts */
10561 
10564 
10565  if (mode > 1) /* SPI uses all GPIO */
10566  {
10569  }
10570 }
10571 
10572 void bscTerm(int mode)
10573 {
10574  bscsReg[BSC_CR] = 0; /* clear device */
10575  bscsReg[BSC_RSR]=0; /* clear underrun and overrun errors */
10576  bscsReg[BSC_SLV]=0; /* clear I2C slave address */
10577 
10580 
10581  if (mode > 1)
10582  {
10585  }
10586 }
10587 
10589 {
10590  static int bscMode = 0;
10591 
10592  int copied=0;
10593  int active, mode;
10594 
10595  DBG(DBG_USER, "control=0x%X (sa=0x%X, cr=0x%X) tx=%d [%s]",
10596  xfer->control,
10597  ((xfer->control)>>16) & 127,
10598  (xfer->control) & 0x3fff,
10599  xfer->txCnt,
10600  myBuf2Str(xfer->txCnt, (char *)xfer->txBuf));
10601 
10602  CHECK_INITED;
10603 
10604  eventAlert[PI_EVENT_BSC].ignore = 1;
10605 
10606  if (xfer->control)
10607  {
10608  /*
10609  bscMode (0=None, 1=I2C, 2=SPI) tracks which GPIO have been
10610  set to BSC mode
10611  */
10612  if (xfer->control & 2) mode = 2; /* SPI */
10613  else mode = 1; /* assume I2C */
10614 
10615  if (mode > bscMode)
10616  {
10617  bscInit(bscMode);
10618  bscMode = mode;
10619  }
10620  }
10621  else
10622  {
10623  if (bscMode) bscTerm(bscMode);
10624  bscMode = 0;
10625  return 0; /* leave ignore set */
10626  }
10627 
10628  xfer->rxCnt = 0;
10629 
10630  bscsReg[BSC_SLV] = ((xfer->control)>>16) & 127;
10631  bscsReg[BSC_CR] = (xfer->control) & 0x3fff;
10632  bscsReg[BSC_RSR]=0; /* clear underrun and overrun errors */
10633 
10634  active = 1;
10635 
10636  while (active)
10637  {
10638  active = 0;
10639 
10640  while ((copied < xfer->txCnt) &&
10641  (!(bscsReg[BSC_FR] & BSC_FR_TXFF)))
10642  {
10643  bscsReg[BSC_DR] = xfer->txBuf[copied++];
10644  active = 1;
10645  }
10646 
10647  while ((xfer->rxCnt < BSC_FIFO_SIZE) &&
10648  (!(bscsReg[BSC_FR] & BSC_FR_RXFE)))
10649  {
10650  xfer->rxBuf[xfer->rxCnt++] = bscsReg[BSC_DR];
10651  active = 1;
10652  }
10653 
10654  if (!active)
10655  {
10656  active = bscsReg[BSC_FR] & (BSC_FR_RXBUSY | BSC_FR_TXBUSY);
10657  }
10658 
10659  if (active) myGpioSleep(0, 20);
10660  }
10661 
10662  bscFR = bscsReg[BSC_FR] & 0xffff;
10663 
10664  eventAlert[PI_EVENT_BSC].ignore = 0;
10665 
10666  return (copied<<16) | bscFR;
10667 }
10668 
10669 /* ----------------------------------------------------------------------- */
10670 
10671 static void set_CS(wfRx_t *w)
10672 {
10674 }
10675 
10676 static void clear_CS(wfRx_t *w)
10677 {
10679 }
10680 
10681 static void set_SCLK(wfRx_t *w)
10682 {
10684 }
10685 
10686 static void clear_SCLK(wfRx_t *w)
10687 {
10689 }
10690 
10691 static void SPI_delay(wfRx_t *w)
10692 {
10693  myGpioDelay(w->S.delay);
10694 }
10695 
10696 static void bbSPIStart(wfRx_t *w)
10697 {
10698  clear_SCLK(w);
10699 
10700  SPI_delay(w);
10701 
10702  set_CS(w);
10703 
10704  SPI_delay(w);
10705 }
10706 
10707 static void bbSPIStop(wfRx_t *w)
10708 {
10709  SPI_delay(w);
10710 
10711  clear_CS(w);
10712 
10713  SPI_delay(w);
10714 
10715  clear_SCLK(w);
10716 }
10717 
10718 static uint8_t bbSPIXferByte(wfRx_t *w, char txByte)
10719 {
10720  uint8_t bit, rxByte=0;
10721 
10723  {
10724  /*
10725  CPHA = 1
10726  write on set clock
10727  read on clear clock
10728  */
10729 
10730  for (bit=0; bit<8; bit++)
10731  {
10732  set_SCLK(w);
10733 
10735  {
10736  myGpioWrite(w->S.MOSI, txByte & 0x01);
10737  txByte >>= 1;
10738  }
10739  else
10740  {
10741  myGpioWrite(w->S.MOSI, txByte & 0x80);
10742  txByte <<= 1;
10743  }
10744 
10745  SPI_delay(w);
10746 
10747  clear_SCLK(w);
10748 
10750  {
10751  rxByte = (rxByte >> 1) | myGpioRead(w->S.MISO) << 7;
10752  }
10753  else
10754  {
10755  rxByte = (rxByte << 1) | myGpioRead(w->S.MISO);
10756  }
10757 
10758  SPI_delay(w);
10759  }
10760  }
10761  else
10762  {
10763  /*
10764  CPHA = 0
10765  read on set clock
10766  write on clear clock
10767  */
10768 
10769  for (bit=0; bit<8; bit++)
10770  {
10772  {
10773  myGpioWrite(w->S.MOSI, txByte & 0x01);
10774  txByte >>= 1;
10775  }
10776  else
10777  {
10778  myGpioWrite(w->S.MOSI, txByte & 0x80);
10779  txByte <<= 1;
10780  }
10781 
10782  SPI_delay(w);
10783 
10784  set_SCLK(w);
10785 
10787  {
10788  rxByte = (rxByte >> 1) | myGpioRead(w->S.MISO) << 7;
10789  }
10790  else
10791  {
10792  rxByte = (rxByte << 1) | myGpioRead(w->S.MISO);
10793  }
10794 
10795  SPI_delay(w);
10796 
10797  clear_SCLK(w);
10798  }
10799  }
10800 
10801  return rxByte;
10802 }
10803 
10804 /*-------------------------------------------------------------------------*/
10805 
10807  unsigned CS, unsigned MISO, unsigned MOSI, unsigned SCLK,
10808  unsigned baud, unsigned spiFlags)
10809 {
10810  int valid;
10811  uint32_t bits;
10812 
10813  DBG(DBG_USER, "CS=%d MISO=%d MOSI=%d SCLK=%d baud=%d flags=%d",
10814  CS, MISO, MOSI, SCLK, baud, spiFlags);
10815 
10816  CHECK_INITED;
10817 
10818  if (CS > PI_MAX_USER_GPIO)
10819  SOFT_ERROR(PI_BAD_USER_GPIO, "bad CS (%d)", CS);
10820 
10821  if (MISO > PI_MAX_USER_GPIO)
10822  SOFT_ERROR(PI_BAD_USER_GPIO, "bad MISO (%d)", MISO);
10823 
10824  if (MOSI > PI_MAX_USER_GPIO)
10825  SOFT_ERROR(PI_BAD_USER_GPIO, "bad MOSI (%d)", MOSI);
10826 
10827  if (SCLK > PI_MAX_USER_GPIO)
10828  SOFT_ERROR(PI_BAD_USER_GPIO, "bad SCLK (%d)", SCLK);
10829 
10830  if ((baud < PI_BB_SPI_MIN_BAUD) || (baud > PI_BB_SPI_MAX_BAUD))
10831  SOFT_ERROR(PI_BAD_SPI_BAUD, "CS %d, bad baud (%d)", CS, baud);
10832 
10833  if (wfRx[CS].mode != PI_WFRX_NONE)
10835  "CS %d is already being used, mode %d", CS, wfRx[CS].mode);
10836 
10837  valid = 0;
10838 
10839  /* check all GPIO unique */
10840 
10841  bits = (1<<CS) | (1<<MISO) | (1<<MOSI) | (1<<SCLK);
10842 
10843  if (__builtin_popcount(bits) == 4)
10844  {
10845  if ((wfRx[MISO].mode == PI_WFRX_NONE) &&
10846  (wfRx[MOSI].mode == PI_WFRX_NONE) &&
10847  (wfRx[SCLK].mode == PI_WFRX_NONE))
10848  {
10849  valid = 1; /* first time GPIO used for SPI */
10850  }
10851  else
10852  {
10853  if ((wfRx[MISO].mode == PI_WFRX_SPI_MISO) &&
10854  (wfRx[MOSI].mode == PI_WFRX_SPI_MOSI) &&
10855  (wfRx[SCLK].mode == PI_WFRX_SPI_SCLK))
10856  {
10857  valid = 2; /* new CS for existing SPI GPIO */
10858  }
10859  }
10860  }
10861 
10862  if (!valid)
10863  {
10865  "GPIO already being used (%d=%d %d=%d, %d=%d %d=%d)",
10866  CS, wfRx[CS].mode,
10867  MISO, wfRx[MISO].mode,
10868  MOSI, wfRx[MOSI].mode,
10869  SCLK, wfRx[SCLK].mode);
10870  }
10871 
10872  wfRx[CS].mode = PI_WFRX_SPI_CS;
10873  wfRx[CS].baud = baud;
10874 
10875  wfRx[CS].S.CS = CS;
10876  wfRx[CS].S.SCLK = SCLK;
10877 
10878  wfRx[CS].S.CSMode = gpioGetMode(CS);
10879  wfRx[CS].S.delay = (500000 / baud) - 1;
10880  wfRx[CS].S.spiFlags = spiFlags;
10881 
10882  /* preset CS to off */
10883 
10884  if (PI_SPI_FLAGS_GET_CSPOL(spiFlags))
10885  gpioWrite(CS, 0); /* active high */
10886  else
10887  gpioWrite(CS, 1); /* active low */
10888 
10889  /* The SCLK entry is used to store full information */
10890 
10891  if (valid == 1) /* first time GPIO for SPI */
10892  {
10893  wfRx[SCLK].S.usage = 1;
10894 
10895  wfRx[SCLK].S.SCLKMode = gpioGetMode(SCLK);
10896  wfRx[SCLK].S.MISOMode = gpioGetMode(MISO);
10897  wfRx[SCLK].S.MOSIMode = gpioGetMode(MOSI);
10898 
10899  wfRx[SCLK].mode = PI_WFRX_SPI_SCLK;
10900  wfRx[MISO].mode = PI_WFRX_SPI_MISO;
10901  wfRx[MOSI].mode = PI_WFRX_SPI_MOSI;
10902 
10903  wfRx[SCLK].S.SCLK = SCLK;
10904  wfRx[SCLK].S.MISO = MISO;
10905  wfRx[SCLK].S.MOSI = MOSI;
10906 
10907  myGpioSetMode(MISO, PI_INPUT);
10908  myGpioSetMode(SCLK, PI_OUTPUT);
10909  gpioWrite(MOSI, 0); /* low output */
10910  }
10911  else
10912  {
10913  wfRx[SCLK].S.usage++;
10914  }
10915 
10916  return 0;
10917 }
10918 
10919 /*-------------------------------------------------------------------------*/
10920 
10921 int bbSPIClose(unsigned CS)
10922 {
10923  int SCLK;
10924 
10925  DBG(DBG_USER, "CS=%d", CS);
10926 
10927  CHECK_INITED;
10928 
10929  if (CS > PI_MAX_USER_GPIO)
10930  SOFT_ERROR(PI_BAD_USER_GPIO, "bad gpio (%d)", CS);
10931 
10932  switch(wfRx[CS].mode)
10933  {
10934  case PI_WFRX_SPI_CS:
10935 
10936  myGpioSetMode(wfRx[CS].S.CS, wfRx[CS].S.CSMode);
10937  wfRx[CS].mode = PI_WFRX_NONE;
10938 
10939  SCLK = wfRx[CS].S.SCLK;
10940 
10941  if (--wfRx[SCLK].S.usage <= 0)
10942  {
10943  myGpioSetMode(wfRx[SCLK].S.MISO, wfRx[SCLK].S.MISOMode);
10944  myGpioSetMode(wfRx[SCLK].S.MOSI, wfRx[SCLK].S.MOSIMode);
10945  myGpioSetMode(wfRx[SCLK].S.SCLK, wfRx[SCLK].S.SCLKMode);
10946 
10947  wfRx[wfRx[SCLK].S.MISO].mode = PI_WFRX_NONE;
10948  wfRx[wfRx[SCLK].S.MOSI].mode = PI_WFRX_NONE;
10949  wfRx[wfRx[SCLK].S.SCLK].mode = PI_WFRX_NONE;
10950  }
10951 
10952  break;
10953 
10954  default:
10955 
10956  SOFT_ERROR(PI_NOT_SPI_GPIO, "no SPI on gpio (%d)", CS);
10957 
10958  break;
10959 
10960  }
10961 
10962  return 0;
10963 }
10964 
10965 /*-------------------------------------------------------------------------*/
10966 
10968  unsigned CS,
10969  char *inBuf,
10970  char *outBuf,
10971  unsigned count)
10972 {
10973  int SCLK;
10974  int pos;
10975  wfRx_t *w;
10976 
10977  DBG(DBG_USER, "CS=%d inBuf=%s outBuf=%08X count=%d",
10978  CS, myBuf2Str(count, (char *)inBuf), (int)outBuf, count);
10979 
10980  CHECK_INITED;
10981 
10982  if (CS > PI_MAX_USER_GPIO)
10983  SOFT_ERROR(PI_BAD_USER_GPIO, "bad gpio (%d)", CS);
10984 
10985  if (wfRx[CS].mode != PI_WFRX_SPI_CS)
10986  SOFT_ERROR(PI_NOT_SPI_GPIO, "no SPI on gpio (%d)", CS);
10987 
10988  if (!inBuf || !count)
10989  SOFT_ERROR(PI_BAD_POINTER, "input buffer can't be NULL");
10990 
10991  if (!outBuf && count)
10992  SOFT_ERROR(PI_BAD_POINTER, "output buffer can't be NULL");
10993 
10994  SCLK = wfRx[CS].S.SCLK;
10995 
10996  wfRx[SCLK].S.CS = CS;
10997  wfRx[SCLK].baud = wfRx[CS].baud;
10998  wfRx[SCLK].S.delay = wfRx[CS].S.delay;
10999  wfRx[SCLK].S.spiFlags = wfRx[CS].S.spiFlags;
11000 
11001  w = &wfRx[SCLK];
11002 
11003  wfRx_lock(SCLK);
11004 
11005  bbSPIStart(w);
11006 
11007  for (pos=0; pos < count; pos++)
11008  {
11009  outBuf[pos] = bbSPIXferByte(w, inBuf[pos]);
11010  }
11011 
11012  bbSPIStop(w);
11013 
11014  wfRx_unlock(SCLK);
11015 
11016  return count;
11017 }
11018 
11019 /*-------------------------------------------------------------------------*/
11020 
11021 int gpioSerialReadOpen(unsigned gpio, unsigned baud, unsigned data_bits)
11022 {
11023  int bitTime, timeout;
11024 
11025  DBG(DBG_USER, "gpio=%d baud=%d data_bits=%d", gpio, baud, data_bits);
11026 
11027  CHECK_INITED;
11028 
11029  if (gpio > PI_MAX_USER_GPIO)
11030  SOFT_ERROR(PI_BAD_USER_GPIO, "bad gpio (%d)", gpio);
11031 
11032  if ((baud < PI_BB_SER_MIN_BAUD) || (baud > PI_BB_SER_MAX_BAUD))
11034  "gpio %d, bad baud rate (%d)", gpio, baud);
11035 
11036  if ((data_bits < PI_MIN_WAVE_DATABITS) ||
11037  (data_bits > PI_MAX_WAVE_DATABITS))
11039  "gpio %d, bad data bits (%d)", gpio, data_bits);
11040 
11041  if (wfRx[gpio].mode != PI_WFRX_NONE)
11042  SOFT_ERROR(PI_GPIO_IN_USE, "gpio %d is already being used", gpio);
11043 
11044  bitTime = (1000 * MILLION) / baud; /* nanos */
11045 
11046  timeout = ((data_bits+2) * bitTime)/MILLION; /* millis */
11047 
11048  if (timeout < 1) timeout = 1;
11049 
11050  wfRx[gpio].gpio = gpio;
11051  wfRx[gpio].mode = PI_WFRX_SERIAL;
11052  wfRx[gpio].baud = baud;
11053 
11054  wfRx[gpio].s.buf = malloc(SRX_BUF_SIZE);
11055  wfRx[gpio].s.bufSize = SRX_BUF_SIZE;
11056  wfRx[gpio].s.timeout = timeout;
11057  wfRx[gpio].s.fullBit = bitTime; /* nanos */
11058  wfRx[gpio].s.halfBit = (bitTime/2)+500; /* nanos (500 for rounding) */
11059  wfRx[gpio].s.readPos = 0;
11060  wfRx[gpio].s.writePos = 0;
11061  wfRx[gpio].s.bit = -1;
11062  wfRx[gpio].s.dataBits = data_bits;
11063  wfRx[gpio].s.invert = PI_BB_SER_NORMAL;
11064 
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;
11068 
11069  gpioSetAlertFunc(gpio, waveRxBit);
11070 
11071  return 0;
11072 }
11073 
11074 /*-------------------------------------------------------------------------*/
11075 
11076 int gpioSerialReadInvert(unsigned gpio, unsigned invert)
11077 {
11078  DBG(DBG_USER, "gpio=%d invert=%d", gpio, invert);
11079 
11080  CHECK_INITED;
11081 
11082  if (gpio > PI_MAX_USER_GPIO)
11083  SOFT_ERROR(PI_BAD_USER_GPIO, "bad gpio (%d)", gpio);
11084 
11085  if (wfRx[gpio].mode != PI_WFRX_SERIAL)
11086  SOFT_ERROR(PI_NOT_SERIAL_GPIO, "no serial read on gpio (%d)", gpio);
11087 
11088  if ((invert < PI_BB_SER_NORMAL) ||
11089  (invert > PI_BB_SER_INVERT))
11091  "bad invert level for gpio %d (%d)", gpio, invert);
11092 
11093  wfRx[gpio].s.invert = invert;
11094 
11095  return 0;
11096 }
11097 
11098 /*-------------------------------------------------------------------------*/
11099 
11100 int gpioSerialRead(unsigned gpio, void *buf, size_t bufSize)
11101 {
11102  unsigned bytes=0, wpos;
11103  volatile wfRx_t *w;
11104 
11105  DBG(DBG_USER, "gpio=%d buf=%08X bufSize=%d", gpio, (int)buf, bufSize);
11106 
11107  CHECK_INITED;
11108 
11109  if (gpio > PI_MAX_USER_GPIO)
11110  SOFT_ERROR(PI_BAD_USER_GPIO, "bad gpio (%d)", gpio);
11111 
11112  if (bufSize == 0)
11113  SOFT_ERROR(PI_BAD_SERIAL_COUNT, "buffer size can't be zero");
11114 
11115  if (wfRx[gpio].mode != PI_WFRX_SERIAL)
11116  SOFT_ERROR(PI_NOT_SERIAL_GPIO, "no serial read on gpio (%d)", gpio);
11117 
11118  w = &wfRx[gpio];
11119 
11120  if (w->s.readPos != w->s.writePos)
11121  {
11122  wpos = w->s.writePos;
11123 
11124  if (wpos > w->s.readPos) bytes = wpos - w->s.readPos;
11125  else bytes = w->s.bufSize - w->s.readPos;
11126 
11127  if (bytes > bufSize) bytes = bufSize;
11128 
11129  /* copy in multiples of the data size in bytes */
11130 
11131  bytes = (bytes / w->s.bytes) * w->s.bytes;
11132 
11133  if (buf) memcpy(buf, w->s.buf+w->s.readPos, bytes);
11134 
11135  w->s.readPos += bytes;
11136 
11137  if (w->s.readPos >= w->s.bufSize) w->s.readPos = 0;
11138  }
11139  return bytes;
11140 }
11141 
11142 
11143 /*-------------------------------------------------------------------------*/
11144 
11145 int gpioSerialReadClose(unsigned gpio)
11146 {
11147  DBG(DBG_USER, "gpio=%d", gpio);
11148 
11149  CHECK_INITED;
11150 
11151  if (gpio > PI_MAX_USER_GPIO)
11152  SOFT_ERROR(PI_BAD_USER_GPIO, "bad gpio (%d)", gpio);
11153 
11154  switch(wfRx[gpio].mode)
11155  {
11156  case PI_WFRX_NONE:
11157 
11158  SOFT_ERROR(PI_NOT_SERIAL_GPIO, "no serial read on gpio (%d)", gpio);
11159 
11160  break;
11161 
11162  case PI_WFRX_SERIAL:
11163 
11164  free(wfRx[gpio].s.buf);
11165 
11166  gpioSetWatchdog(gpio, 0); /* switch off timeouts */
11167 
11168  gpioSetAlertFunc(gpio, NULL); /* cancel alert */
11169 
11170  wfRx[gpio].mode = PI_WFRX_NONE;
11171 
11172  break;
11173  }
11174 
11175  return 0;
11176 }
11177 
11178 
11179 /* ----------------------------------------------------------------------- */
11180 
11181 static int intEventSetFunc(
11182  unsigned event,
11183  void * f,
11184  int user,
11185  void * userdata)
11186 {
11187  DBG(DBG_INTERNAL, "event=%d function=%08X, user=%d, userdata=%08X",
11188  event, (uint32_t)f, user, (uint32_t)userdata);
11189 
11190  eventAlert[event].ex = user;
11191  eventAlert[event].userdata = userdata;
11192 
11193  eventAlert[event].func = f;
11194 
11195  return 0;
11196 }
11197 
11198 
11199 /* ----------------------------------------------------------------------- */
11200 
11201 int eventSetFunc(unsigned event, eventFunc_t f)
11202 {
11203  DBG(DBG_USER, "event=%d function=%08X", event, (uint32_t)f);
11204 
11205  CHECK_INITED;
11206 
11207  if (event > PI_MAX_EVENT)
11208  SOFT_ERROR(PI_BAD_EVENT_ID, "bad event (%d)", event);
11209 
11210  intEventSetFunc(event, f, 0, NULL);
11211 
11212  return 0;
11213 }
11214 
11215 
11216 /* ----------------------------------------------------------------------- */
11217 
11218 int eventSetFuncEx(unsigned event, eventFuncEx_t f, void *userdata)
11219 {
11220  DBG(DBG_USER, "event=%d function=%08X userdata=%08X",
11221  event, (uint32_t)f, (uint32_t)userdata);
11222 
11223  CHECK_INITED;
11224 
11225  if (event > PI_MAX_EVENT)
11226  SOFT_ERROR(PI_BAD_EVENT_ID, "bad event (%d)", event);
11227 
11228  intEventSetFunc(event, f, 1, userdata);
11229 
11230  return 0;
11231 }
11232 
11233 
11234 /* ----------------------------------------------------------------------- */
11235 
11236 int eventMonitor(unsigned handle, uint32_t bits)
11237 {
11238  DBG(DBG_USER, "handle=%d bits=%08X", handle, bits);
11239 
11240  CHECK_INITED;
11241 
11242  if (handle >= PI_NOTIFY_SLOTS)
11243  SOFT_ERROR(PI_BAD_HANDLE, "bad handle (%d)", handle);
11244 
11245  if (gpioNotify[handle].state <= PI_NOTIFY_CLOSING)
11246  SOFT_ERROR(PI_BAD_HANDLE, "bad handle (%d)", handle);
11247 
11248  gpioNotify[handle].eventBits = bits;
11249 
11250  return 0;
11251 }
11252 
11253 
11254 /* ----------------------------------------------------------------------- */
11255 
11256 int eventTrigger(unsigned event)
11257 {
11258  DBG(DBG_USER, "event=%d", event);
11259 
11260  CHECK_INITED;
11261 
11262  if (event > PI_MAX_EVENT)
11263  SOFT_ERROR(PI_BAD_EVENT_ID, "bad event (%d)", event);
11264 
11265  eventAlert[event].fired = 1;
11266 
11267  return 0;
11268 }
11269 
11270 
11271 /* ----------------------------------------------------------------------- */
11272 
11274  unsigned gpio,
11275  void * f,
11276  int user,
11277  void * userdata)
11278 {
11279  DBG(DBG_INTERNAL, "gpio=%d function=%08X, user=%d, userdata=%08X",
11280  gpio, (uint32_t)f, user, (uint32_t)userdata);
11281 
11282  gpioAlert[gpio].ex = user;
11283  gpioAlert[gpio].userdata = userdata;
11284 
11285  gpioAlert[gpio].func = f;
11286 
11287  if (f)
11288  {
11289  alertBits |= BIT;
11290  }
11291  else
11292  {
11293  alertBits &= ~BIT;
11294  }
11295 
11296  monitorBits = alertBits | notifyBits | scriptBits | gpioGetSamples.bits;
11297 
11298  return 0;
11299 }
11300 
11301 /* ----------------------------------------------------------------------- */
11302 
11303 int gpioSetAlertFunc(unsigned gpio, gpioAlertFunc_t f)
11304 {
11305  DBG(DBG_USER, "gpio=%d function=%08X", gpio, (uint32_t)f);
11306 
11307  CHECK_INITED;
11308 
11309  if (gpio > PI_MAX_USER_GPIO)
11310  SOFT_ERROR(PI_BAD_USER_GPIO, "bad gpio (%d)", gpio);
11311 
11312  intGpioSetAlertFunc(gpio, f, 0, NULL);
11313 
11314  return 0;
11315 }
11316 
11317 
11318 /* ----------------------------------------------------------------------- */
11319 
11320 int gpioSetAlertFuncEx(unsigned gpio, gpioAlertFuncEx_t f, void *userdata)
11321 {
11322  DBG(DBG_USER, "gpio=%d function=%08X userdata=%08X",
11323  gpio, (uint32_t)f, (uint32_t)userdata);
11324 
11325  CHECK_INITED;
11326 
11327  if (gpio > PI_MAX_USER_GPIO)
11328  SOFT_ERROR(PI_BAD_USER_GPIO, "bad gpio (%d)", gpio);
11329 
11330  intGpioSetAlertFunc(gpio, f, 1, userdata);
11331 
11332  return 0;
11333 }
11334 
11335 static void *pthISRThread(void *x)
11336 {
11337  gpioISR_t *isr = x;
11338  int fd;
11339  int retval;
11340  uint32_t tick;
11341  int level;
11342  uint32_t levels;
11343  struct pollfd pfd;
11344  char buf[64];
11345 
11346  DBG(DBG_USER, "gpio=%d edge=%d timeout=%d f=%x u=%d data=%x",
11347  isr->gpio, isr->edge, isr->timeout, (uint32_t)isr->func,
11348  isr->ex, (uint32_t)isr->userdata);
11349 
11350  sprintf(buf, "/sys/class/gpio/gpio%d/value", isr->gpio);
11351 
11352  if ((fd = open(buf, O_RDONLY)) < 0)
11353  {
11354  DBG(DBG_ALWAYS, "gpio %d not exported", isr->gpio);
11355  return NULL;
11356  }
11357 
11358  pfd.fd = fd;
11359 
11360  pfd.events = POLLPRI;
11361 
11362  lseek(fd, 0, SEEK_SET); /* consume any prior interrupt */
11363  if (read(fd, buf, sizeof buf) == -1) { /* ignore errors */ }
11364 
11365  while (1)
11366  {
11367  retval = poll(&pfd, 1, isr->timeout); /* wait for interrupt */
11368 
11369  tick = systReg[SYST_CLO];
11370 
11371  levels = *(gpioReg + GPLEV0);
11372 
11373  if (retval >= 0)
11374  {
11375  lseek(fd, 0, SEEK_SET); /* consume interrupt */
11376  if (read(fd, buf, sizeof buf) == -1) { /* ignore errors */ }
11377 
11378  if (retval)
11379  {
11380  if (levels & (1<<isr->gpio)) level = PI_ON; else level = PI_OFF;
11381  }
11382  else level = PI_TIMEOUT;
11383 
11384  if (isr->ex) (isr->func)(isr->gpio, level, tick, isr->userdata);
11385  else (isr->func)(isr->gpio, level, tick);
11386  }
11387  }
11388 
11389  return NULL;
11390 }
11391 
11392 
11393 /* ----------------------------------------------------------------------- */
11394 
11396  unsigned gpio,
11397  unsigned edge,
11398  int timeout,
11399  void *f,
11400  int user,
11401  void *userdata)
11402 {
11403  char buf[64];
11404 
11405  char *edge_str[]={"rising\n", "falling\n", "both\n"};
11406  int fd;
11407  int err;
11408 
11409  DBG(DBG_INTERNAL,
11410  "gpio=%d edge=%d timeout=%d function=%08X user=%d userdata=%08X",
11411  gpio, edge, timeout, (uint32_t)f, user, (uint32_t)userdata);
11412 
11413  if (f)
11414  {
11415  if (!gpioISR[gpio].inited) /* export gpio if unexported */
11416  {
11417  fd = open("/sys/class/gpio/export", O_WRONLY);
11418  if (fd < 0) return PI_BAD_ISR_INIT;
11419 
11420  /* ignore write fail if already exported */
11421  sprintf(buf, "%d\n", gpio);
11422  err = write(fd, buf, strlen(buf));
11423  close(fd);
11424 
11425  sprintf(buf, "/sys/class/gpio/gpio%d/direction", gpio);
11426  fd = open(buf, O_WRONLY);
11427  if (fd < 0) return PI_BAD_ISR_INIT;
11428 
11429  err = write(fd, "in\n", 3);
11430  close(fd);
11431  if (err != 3) return PI_BAD_ISR_INIT;
11432 
11433  gpioISR[gpio].gpio = gpio;
11434  gpioISR[gpio].edge = -1;
11435  gpioISR[gpio].timeout = -1;
11436 
11437  gpioISR[gpio].inited = 1;
11438  }
11439 
11440  if (gpioISR[gpio].edge != edge)
11441  {
11442  sprintf(buf, "/sys/class/gpio/gpio%d/edge", gpio);
11443  fd = open(buf, O_WRONLY);
11444  if (fd < 0) return PI_BAD_ISR_INIT;
11445 
11446  err = write(fd, edge_str[edge], strlen(edge_str[edge]));
11447  close(fd);
11448  if (err != strlen(edge_str[edge])) return PI_BAD_ISR_INIT;
11449 
11450  gpioISR[gpio].edge = edge;
11451 
11452  if (gpioISR[gpio].pth != NULL)
11453  pthread_kill(*gpioISR[gpio].pth, SIGCHLD);
11454  }
11455 
11456  if (timeout <= 0) timeout = -1;
11457  if (gpioISR[gpio].timeout != timeout)
11458  {
11459  gpioISR[gpio].timeout = timeout;
11460 
11461  if (gpioISR[gpio].pth != NULL)
11462  pthread_kill(*gpioISR[gpio].pth, SIGCHLD);
11463  }
11464 
11465  gpioISR[gpio].func = f;
11466  gpioISR[gpio].ex = user;
11467  gpioISR[gpio].userdata = userdata;
11468 
11469  if (gpioISR[gpio].pth == NULL)
11470  gpioISR[gpio].pth = gpioStartThread(pthISRThread, &gpioISR[gpio]);
11471  }
11472  else /* null function, delete ISR, unexport gpio */
11473  {
11474  if (gpioISR[gpio].pth) /* delete any existing ISR */
11475  {
11476  gpioStopThread(gpioISR[gpio].pth);
11477  gpioISR[gpio].func = NULL;
11478  gpioISR[gpio].pth = NULL;
11479  }
11480 
11481  if (gpioISR[gpio].inited) /* unexport the gpio */
11482  {
11483  fd = open("/sys/class/gpio/unexport", O_WRONLY);
11484  if (fd < 0) return PI_BAD_ISR_INIT;
11485  sprintf(buf, "%d\n", gpio);
11486  err = write(fd, buf, strlen(buf));
11487  close(fd);
11488  if (err != strlen(buf)) return PI_BAD_ISR_INIT;
11489  gpioISR[gpio].inited = 0;
11490  }
11491  }
11492 
11493  return 0;
11494 }
11495 
11496 /* ----------------------------------------------------------------------- */
11497 
11499  unsigned gpio,
11500  unsigned edge,
11501  int timeout,
11502  gpioISRFunc_t f)
11503 {
11504  DBG(DBG_USER, "gpio=%d edge=%d timeout=%d function=%08X",
11505  gpio, edge, timeout, (uint32_t)f);
11506 
11507  CHECK_INITED;
11508 
11509  if (gpio > PI_MAX_GPIO)
11510  SOFT_ERROR(PI_BAD_GPIO, "bad gpio (%d)", gpio);
11511 
11512  if (edge > EITHER_EDGE)
11513  SOFT_ERROR(PI_BAD_EDGE, "bad ISR edge (%d)", edge);
11514 
11515  return intGpioSetISRFunc(gpio, edge, timeout, f, 0, NULL);
11516 }
11517 
11518 
11519 /* ----------------------------------------------------------------------- */
11520 
11522  unsigned gpio,
11523  unsigned edge,
11524  int timeout,
11526  void *userdata)
11527 {
11528  DBG(DBG_USER, "gpio=%d edge=%d timeout=%d function=%08X userdata=%08X",
11529  gpio, edge, timeout, (uint32_t)f, (uint32_t)userdata);
11530 
11531  CHECK_INITED;
11532 
11533  if (gpio > PI_MAX_GPIO)
11534  SOFT_ERROR(PI_BAD_GPIO, "bad gpio (%d)", gpio);
11535 
11536  if (edge > EITHER_EDGE)
11537  SOFT_ERROR(PI_BAD_EDGE, "bad ISR edge (%d)", edge);
11538 
11539  return intGpioSetISRFunc(gpio, edge, timeout, f, 1, userdata);
11540 }
11541 
11542 static void closeOrphanedNotifications(int slot, int fd)
11543 {
11544  int i;
11545 
11546  /* Check for and close any orphaned notifications. */
11547 
11548  for (i=0; i<PI_NOTIFY_SLOTS; i++)
11549  {
11550  if ((i != slot) &&
11551  (gpioNotify[i].state >= PI_NOTIFY_OPENED) &&
11552  (gpioNotify[i].fd == fd))
11553  {
11554  DBG(DBG_USER, "closed orphaned fd=%d (handle=%d)", fd, i);
11555  gpioNotify[i].state = PI_NOTIFY_CLOSED;
11556  intNotifyBits();
11557  }
11558  }
11559 }
11560 
11561 /* ----------------------------------------------------------------------- */
11562 
11563 static void notifyMutex(int lock)
11564 {
11565  static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
11566  if (lock) pthread_mutex_lock(&mutex);
11567  else pthread_mutex_unlock(&mutex);
11568 }
11569 
11570 /* ----------------------------------------------------------------------- */
11571 
11572 int gpioNotifyOpenWithSize(int bufSize)
11573 {
11574  int i, slot, fd;
11575  char name[32];
11576 
11577  DBG(DBG_USER, "bufSize=%d", bufSize);
11578 
11579  CHECK_INITED;
11580 
11581  slot = -1;
11582 
11583  notifyMutex(1);
11584 
11585  for (i=0; i<PI_NOTIFY_SLOTS; i++)
11586  {
11587  if (gpioNotify[i].state == PI_NOTIFY_CLOSED)
11588  {
11589  slot = i;
11590  gpioNotify[slot].state = PI_NOTIFY_RESERVED;
11591  break;
11592  }
11593  }
11594 
11595  notifyMutex(0);
11596 
11597  if (slot < 0) SOFT_ERROR(PI_NO_HANDLE, "no handle");
11598 
11599  sprintf(name, "/dev/pigpio%d", slot);
11600 
11601  myCreatePipe(name, 0664);
11602 
11603  fd = open(name, O_RDWR|O_NONBLOCK);
11604 
11605  if (fd < 0)
11606  {
11607  gpioNotify[slot].state = PI_NOTIFY_CLOSED;
11608  SOFT_ERROR(PI_BAD_PATHNAME, "open %s failed (%m)", name);
11609  }
11610 
11611  if (bufSize != 0)
11612  {
11613  i = fcntl(fd, F_SETPIPE_SZ, bufSize);
11614  if (i != bufSize)
11615  {
11616  gpioNotify[slot].state = PI_NOTIFY_CLOSED;
11618  "fcntl %s size %d failed (%m)", name, bufSize);
11619  }
11620  }
11621 
11622  gpioNotify[slot].seqno = 0;
11623  gpioNotify[slot].bits = 0;
11624  gpioNotify[slot].fd = fd;
11625  gpioNotify[slot].pipe = 1;
11626  gpioNotify[slot].max_emits = MAX_EMITS;
11627  gpioNotify[slot].lastReportTick = gpioTick();
11628  gpioNotify[i].state = PI_NOTIFY_OPENED;
11629 
11630  closeOrphanedNotifications(slot, fd);
11631 
11632  return slot;
11633 }
11634 
11636 {
11637  return gpioNotifyOpenWithSize(0);
11638 }
11639 
11640 /* ----------------------------------------------------------------------- */
11641 
11642 static int gpioNotifyOpenInBand(int fd)
11643 {
11644  int i, slot;
11645 
11646  DBG(DBG_USER, "fd=%d", fd);
11647 
11648  CHECK_INITED;
11649 
11650  slot = -1;
11651 
11652  notifyMutex(1);
11653 
11654  for (i=0; i<PI_NOTIFY_SLOTS; i++)
11655  {
11656  if (gpioNotify[i].state == PI_NOTIFY_CLOSED)
11657  {
11658  slot = i;
11659  gpioNotify[slot].state = PI_NOTIFY_RESERVED;
11660  break;
11661  }
11662  }
11663 
11664  notifyMutex(0);
11665 
11666  if (slot < 0) SOFT_ERROR(PI_NO_HANDLE, "no handle");
11667 
11668  gpioNotify[slot].seqno = 0;
11669  gpioNotify[slot].bits = 0;
11670  gpioNotify[slot].fd = fd;
11671  gpioNotify[slot].pipe = 0;
11672  gpioNotify[slot].max_emits = MAX_EMITS;
11673  gpioNotify[slot].lastReportTick = gpioTick();
11674  gpioNotify[slot].state = PI_NOTIFY_OPENED;
11675 
11676  closeOrphanedNotifications(slot, fd);
11677 
11678  return slot;
11679 }
11680 
11681 
11682 /* ----------------------------------------------------------------------- */
11683 
11684 static void intScriptBits(void)
11685 {
11686  int i;
11687  uint32_t bits;
11688 
11689  bits = 0;
11690 
11691  for (i=0; i<PI_MAX_SCRIPTS; i++)
11692  {
11693  if (gpioScript[i].state == PI_SCRIPT_IN_USE)
11694  {
11695  bits |= gpioScript[i].waitBits;
11696  }
11697  }
11698 
11699  scriptBits = bits;
11700 
11701  monitorBits = alertBits | notifyBits | scriptBits | gpioGetSamples.bits;
11702 }
11703 
11704 
11705 static void intScriptEventBits(void)
11706 {
11707  int i;
11708  uint32_t bits;
11709 
11710  bits = 0;
11711 
11712  for (i=0; i<PI_MAX_SCRIPTS; i++)
11713  {
11714  if (gpioScript[i].state == PI_SCRIPT_IN_USE)
11715  {
11716  bits |= gpioScript[i].eventBits;
11717  }
11718  }
11719 
11720  scriptEventBits = bits;
11721 }
11722 
11723 
11724 static void intNotifyBits(void)
11725 {
11726  int i;
11727  uint32_t bits;
11728 
11729  bits = 0;
11730 
11731  for (i=0; i<PI_NOTIFY_SLOTS; i++)
11732  {
11733  if (gpioNotify[i].state == PI_NOTIFY_RUNNING)
11734  {
11735  bits |= gpioNotify[i].bits;
11736  }
11737  }
11738 
11739  notifyBits = bits;
11740 
11741  monitorBits = alertBits | notifyBits | scriptBits | gpioGetSamples.bits;
11742 }
11743 
11744 
11745 /* ----------------------------------------------------------------------- */
11746 
11747 int gpioNotifyBegin(unsigned handle, uint32_t bits)
11748 {
11749  DBG(DBG_USER, "handle=%d bits=%08X", handle, bits);
11750 
11751  CHECK_INITED;
11752 
11753  if (handle >= PI_NOTIFY_SLOTS)
11754  SOFT_ERROR(PI_BAD_HANDLE, "bad handle (%d)", handle);
11755 
11756  if (gpioNotify[handle].state <= PI_NOTIFY_CLOSING)
11757  SOFT_ERROR(PI_BAD_HANDLE, "bad handle (%d)", handle);
11758 
11759  gpioNotify[handle].bits = bits;
11760 
11761  gpioNotify[handle].state = PI_NOTIFY_RUNNING;
11762 
11763  intNotifyBits();
11764 
11765  return 0;
11766 }
11767 
11768 
11769 /* ----------------------------------------------------------------------- */
11770 
11771 int gpioNotifyPause (unsigned handle)
11772 {
11773  DBG(DBG_USER, "handle=%d", handle);
11774 
11775  CHECK_INITED;
11776 
11777  if (handle >= PI_NOTIFY_SLOTS)
11778  SOFT_ERROR(PI_BAD_HANDLE, "bad handle (%d)", handle);
11779 
11780  if (gpioNotify[handle].state <= PI_NOTIFY_CLOSING)
11781  SOFT_ERROR(PI_BAD_HANDLE, "bad handle (%d)", handle);
11782 
11783  gpioNotify[handle].bits = 0;
11784 
11785  gpioNotify[handle].state = PI_NOTIFY_PAUSED;
11786 
11787  intNotifyBits();
11788 
11789  return 0;
11790 }
11791 
11792 
11793 /* ----------------------------------------------------------------------- */
11794 
11796 {
11797  DBG(DBG_USER, "handle=%d", handle);
11798 
11799  CHECK_INITED;
11800 
11801  if (handle >= PI_NOTIFY_SLOTS)
11802  SOFT_ERROR(PI_BAD_HANDLE, "bad handle (%d)", handle);
11803 
11804  if (gpioNotify[handle].state <= PI_NOTIFY_CLOSING)
11805  SOFT_ERROR(PI_BAD_HANDLE, "bad handle (%d)", handle);
11806 
11807  gpioNotify[handle].bits = 0;
11808 
11809  gpioNotify[handle].state = PI_NOTIFY_CLOSING;
11810 
11811  intNotifyBits();
11812 
11813  /* actual close done in alert thread */
11814 
11815  return 0;
11816 }
11817 
11818 /* ----------------------------------------------------------------------- */
11819 
11820 int gpioTrigger(unsigned gpio, unsigned pulseLen, unsigned level)
11821 {
11822  DBG(DBG_USER, "gpio=%d pulseLen=%d level=%d", gpio, pulseLen, level);
11823 
11824  CHECK_INITED;
11825 
11826  if (gpio > PI_MAX_USER_GPIO)
11827  SOFT_ERROR(PI_BAD_USER_GPIO, "bad gpio (%d)", gpio);
11828 
11829  if (level > PI_ON)
11830  SOFT_ERROR(PI_BAD_LEVEL, "gpio %d, bad level (%d)", gpio, level);
11831 
11832  if ((pulseLen > PI_MAX_BUSY_DELAY) || (!pulseLen))
11834  "gpio %d, bad pulseLen (%d)", gpio, pulseLen);
11835 
11836  if (level == PI_OFF) *(gpioReg + GPCLR0 + BANK) = BIT;
11837  else *(gpioReg + GPSET0 + BANK) = BIT;
11838 
11839  myGpioDelay(pulseLen);
11840 
11841  if (level != PI_OFF) *(gpioReg + GPCLR0 + BANK) = BIT;
11842  else *(gpioReg + GPSET0 + BANK) = BIT;
11843 
11844  return 0;
11845 }
11846 
11847 
11848 /* ----------------------------------------------------------------------- */
11849 
11850 int gpioSetWatchdog(unsigned gpio, unsigned timeout)
11851 {
11852  DBG(DBG_USER, "gpio=%d timeout=%d", gpio, timeout);
11853 
11854  CHECK_INITED;
11855 
11856  if (gpio > PI_MAX_USER_GPIO)
11857  SOFT_ERROR(PI_BAD_USER_GPIO, "bad gpio (%d)", gpio);
11858 
11859  if (timeout > PI_MAX_WDOG_TIMEOUT)
11861  "gpio %d, bad timeout (%d)", gpio, timeout);
11862 
11863  gpioAlert[gpio].wdTick = systReg[SYST_CLO];
11864  gpioAlert[gpio].wdSteadyUs = timeout*1000;
11865 
11866  if (timeout) wdogBits |= (1<<gpio);
11867  else wdogBits &= (~(1<<gpio));
11868 
11869  return 0;
11870 }
11871 
11872 /* ----------------------------------------------------------------------- */
11873 
11874 int gpioNoiseFilter(unsigned gpio, unsigned steady, unsigned active)
11875 {
11876  DBG(DBG_USER, "gpio=%d steady=%d active=%d", gpio, steady, active);
11877 
11878  CHECK_INITED;
11879 
11880  if (gpio > PI_MAX_USER_GPIO)
11881  SOFT_ERROR(PI_BAD_USER_GPIO, "bad gpio (%d)", gpio);
11882 
11883  if (steady > PI_MAX_STEADY)
11884  SOFT_ERROR(PI_BAD_FILTER, "bad steady (%d)", steady);
11885 
11886  if (active > PI_MAX_ACTIVE)
11887  SOFT_ERROR(PI_BAD_FILTER, "bad active (%d)", active);
11888 
11889  gpioAlert[gpio].nfTick1 = systReg[SYST_CLO];
11890  gpioAlert[gpio].nfTick2 = gpioAlert[gpio].nfTick1;
11891  gpioAlert[gpio].nfSteadyUs = steady;
11892  gpioAlert[gpio].nfActiveUs = active;
11893  gpioAlert[gpio].nfActive = 0;
11894 
11895  if (steady) nFilterBits |= (1<<gpio);
11896  else nFilterBits &= (~(1<<gpio));
11897 
11898  return 0;
11899 }
11900 
11901 
11902 /* ----------------------------------------------------------------------- */
11903 
11904 int gpioGlitchFilter(unsigned gpio, unsigned steady)
11905 {
11906  DBG(DBG_USER, "gpio=%d steady=%d", gpio, steady);
11907 
11908  CHECK_INITED;
11909 
11910  if (gpio > PI_MAX_USER_GPIO)
11911  SOFT_ERROR(PI_BAD_USER_GPIO, "bad gpio (%d)", gpio);
11912 
11913  if (steady > PI_MAX_STEADY)
11914  SOFT_ERROR(PI_BAD_FILTER, "bad steady (%d)", steady);
11915 
11916  if (steady)
11917  {
11918  gpioAlert[gpio].gfTick = systReg[SYST_CLO];
11919 
11920  if (gpioRead_Bits_0_31() & (1<<gpio))
11921  {
11922  gpioAlert[gpio].gfLBitV = (1<<gpio);
11923  gpioAlert[gpio].gfRBitV = 0 ;
11924  }
11925  else
11926  {
11927  gpioAlert[gpio].gfLBitV = 0 ;
11928  gpioAlert[gpio].gfRBitV = (1<<gpio);
11929  }
11930  }
11931 
11932  gpioAlert[gpio].gfSteadyUs = steady;
11933 
11934  if (steady) gFilterBits |= (1<<gpio);
11935  else gFilterBits &= (~(1<<gpio));
11936 
11937  return 0;
11938 }
11939 
11940 /* ----------------------------------------------------------------------- */
11941 
11943 {
11944  DBG(DBG_USER, "function=%08X bits=%08X", (uint32_t)f, bits);
11945 
11946  CHECK_INITED;
11947 
11948  gpioGetSamples.ex = 0;
11949  gpioGetSamples.userdata = NULL;
11950  gpioGetSamples.func = f;
11951 
11952  if (f) gpioGetSamples.bits = bits;
11953  else gpioGetSamples.bits = 0;
11954 
11955  monitorBits = alertBits | notifyBits | scriptBits | gpioGetSamples.bits;
11956 
11957  return 0;
11958 }
11959 
11960 
11961 /* ----------------------------------------------------------------------- */
11962 
11964  uint32_t bits,
11965  void * userdata)
11966 {
11967  DBG(DBG_USER, "function=%08X bits=%08X", (uint32_t)f, bits);
11968 
11969  CHECK_INITED;
11970 
11971  gpioGetSamples.ex = 1;
11972  gpioGetSamples.userdata = userdata;
11973  gpioGetSamples.func = f;
11974 
11975  if (f) gpioGetSamples.bits = bits;
11976  else gpioGetSamples.bits = 0;
11977 
11978  monitorBits = alertBits | notifyBits | scriptBits | gpioGetSamples.bits;
11979 
11980  return 0;
11981 }
11982 
11983 
11984 /* ----------------------------------------------------------------------- */
11985 
11986 static int intGpioSetTimerFunc(unsigned id,
11987  unsigned millis,
11988  void *f,
11989  int user,
11990  void *userdata)
11991 {
11992  pthread_attr_t pthAttr;
11993 
11994  DBG(DBG_INTERNAL, "id=%d millis=%d function=%08X user=%d userdata=%08X",
11995  id, millis, (uint32_t)f, user, (uint32_t)userdata);
11996 
11997  gpioTimer[id].id = id;
11998 
11999  if (f)
12000  {
12001  gpioTimer[id].func = f;
12002  gpioTimer[id].ex = user;
12003  gpioTimer[id].userdata = userdata;
12004  gpioTimer[id].millis = millis;
12005 
12006  if (!gpioTimer[id].running)
12007  {
12008  if (pthread_attr_init(&pthAttr))
12010  "pthread_attr_init failed (%m)");
12011 
12012  if (pthread_attr_setstacksize(&pthAttr, STACK_SIZE))
12014  "pthread_attr_setstacksize failed (%m)");
12015 
12016  if (pthread_create(
12017  &gpioTimer[id].pthId, &pthAttr, pthTimerTick, &gpioTimer[id]))
12019  "timer %d, create failed (%m)", id);
12020 
12021  gpioTimer[id].running = 1;
12022  }
12023  }
12024  else
12025  {
12026  if (gpioTimer[id].running)
12027  {
12028 
12029  /* destroy thread */
12030 
12031  if (pthread_self() == gpioTimer[id].pthId)
12032  {
12033  gpioTimer[id].running = 0;
12034  gpioTimer[id].func = 0;
12035  pthread_exit(NULL);
12036  }
12037  else
12038  {
12039  if (pthread_cancel(gpioTimer[id].pthId))
12040  SOFT_ERROR(PI_TIMER_FAILED, "timer %d, cancel failed (%m)", id);
12041 
12042  if (pthread_join(gpioTimer[id].pthId, NULL))
12043  SOFT_ERROR(PI_TIMER_FAILED, "timer %d, join failed (%m)", id);
12044 
12045  gpioTimer[id].running = 0;
12046  gpioTimer[id].func = 0;
12047  }
12048  }
12049  }
12050 
12051  return 0;
12052 }
12053 
12054 
12055 /* ----------------------------------------------------------------------- */
12056 
12057 int gpioSetTimerFunc(unsigned id, unsigned millis, gpioTimerFunc_t f)
12058 {
12059  DBG(DBG_USER, "id=%d millis=%d function=%08X", id, millis, (uint32_t)f);
12060 
12061  CHECK_INITED;
12062 
12063  if (id > PI_MAX_TIMER)
12064  SOFT_ERROR(PI_BAD_TIMER, "bad timer id (%d)", id);
12065 
12066  if ((millis < PI_MIN_MS) || (millis > PI_MAX_MS))
12067  SOFT_ERROR(PI_BAD_MS, "timer %d, bad millis (%d)", id, millis);
12068 
12069  intGpioSetTimerFunc(id, millis, f, 0, NULL);
12070 
12071  return 0;
12072 }
12073 
12074 
12075 /* ----------------------------------------------------------------------- */
12076 
12077 int gpioSetTimerFuncEx(unsigned id, unsigned millis, gpioTimerFuncEx_t f,
12078  void * userdata)
12079 {
12080  DBG(DBG_USER, "id=%d millis=%d function=%08X, userdata=%08X",
12081  id, millis, (uint32_t)f, (uint32_t)userdata);
12082 
12083  CHECK_INITED;
12084 
12085  if (id > PI_MAX_TIMER)
12086  SOFT_ERROR(PI_BAD_TIMER, "bad timer id (%d)", id);
12087 
12088  if ((millis < PI_MIN_MS) || (millis > PI_MAX_MS))
12089  SOFT_ERROR(PI_BAD_MS, "timer %d, bad millis (%d)", id, millis);
12090 
12091  intGpioSetTimerFunc(id, millis, f, 1, userdata);
12092 
12093  return 0;
12094 }
12095 
12096 /* ----------------------------------------------------------------------- */
12097 
12098 pthread_t *gpioStartThread(gpioThreadFunc_t f, void *userdata)
12099 {
12100  pthread_t *pth;
12101  pthread_attr_t pthAttr;
12102 
12103  DBG(DBG_USER, "f=%08X, userdata=%08X", (uint32_t)f, (uint32_t)userdata);
12104 
12106 
12107  pth = malloc(sizeof(pthread_t));
12108 
12109  if (pth)
12110  {
12111  if (pthread_attr_init(&pthAttr))
12112  {
12113  free(pth);
12114  SOFT_ERROR(NULL, "pthread_attr_init failed");
12115  }
12116 
12117  if (pthread_attr_setstacksize(&pthAttr, STACK_SIZE))
12118  {
12119  free(pth);
12120  SOFT_ERROR(NULL, "pthread_attr_setstacksize failed");
12121  }
12122 
12123  if (pthread_create(pth, &pthAttr, f, userdata))
12124  {
12125  free(pth);
12126  SOFT_ERROR(NULL, "pthread_create failed");
12127  }
12128  }
12129  return pth;
12130 }
12131 
12132 /* ----------------------------------------------------------------------- */
12133 
12134 void gpioStopThread(pthread_t *pth)
12135 {
12136  DBG(DBG_USER, "pth=%08X", (uint32_t)pth);
12137 
12139 
12140  if (pth)
12141  {
12142  if (pthread_self() == *pth)
12143  {
12144  free(pth);
12145  pthread_exit(NULL);
12146  }
12147  else
12148  {
12149  pthread_cancel(*pth);
12150  pthread_join(*pth, NULL);
12151  free(pth);
12152  }
12153  }
12154 }
12155 
12156 /* ----------------------------------------------------------------------- */
12157 
12158 int gpioStoreScript(char *script)
12159 {
12160  static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
12161  gpioScript_t *s;
12162  int status, slot, i;
12163 
12164  DBG(DBG_USER, "script=[%s]", script);
12165 
12166  CHECK_INITED;
12167 
12168  slot = -1;
12169 
12170  pthread_mutex_lock(&mutex);
12171 
12172  for (i=0; i<PI_MAX_SCRIPTS; i++)
12173  {
12174  if (gpioScript[i].state == PI_SCRIPT_FREE)
12175  {
12176  slot = i;
12177  gpioScript[slot].state = PI_SCRIPT_RESERVED;
12178  break;
12179  }
12180  }
12181 
12182  pthread_mutex_unlock(&mutex);
12183 
12184  if (slot < 0) SOFT_ERROR(PI_NO_SCRIPT_ROOM, "no room for scripts");
12185 
12186  s = &gpioScript[slot];
12187 
12188  status = cmdParseScript(script, &s->script, 0);
12189 
12190  if (status == 0)
12191  {
12192  s->request = PI_SCRIPT_HALT;
12194 
12195  pthread_cond_init(&s->pthCond, NULL);
12196  pthread_mutex_init(&s->pthMutex, NULL);
12197 
12198  s->id = slot;
12199 
12200  gpioScript[slot].state = PI_SCRIPT_IN_USE;
12201 
12202  s->pthIdp = gpioStartThread(pthScript, s);
12203 
12204  status = slot;
12205  }
12206  else
12207  {
12208  if (s->script.par) free(s->script.par);
12209  s->script.par = NULL;
12210  gpioScript[slot].state = PI_SCRIPT_FREE;
12211  }
12212 
12213  return status;
12214 }
12215 
12216 
12217 /* ----------------------------------------------------------------------- */
12218 
12219 int gpioRunScript(unsigned script_id, unsigned numParam, uint32_t *param)
12220 {
12221  int status = 0;
12222 
12223  DBG(DBG_USER, "script_id=%d numParam=%d param=%08X",
12224  script_id, numParam, (uint32_t)param);
12225 
12226  CHECK_INITED;
12227 
12228  if (script_id >= PI_MAX_SCRIPTS)
12229  SOFT_ERROR(PI_BAD_SCRIPT_ID, "bad script id(%d)", script_id);
12230 
12231  if (numParam > PI_MAX_SCRIPT_PARAMS)
12232  SOFT_ERROR(PI_TOO_MANY_PARAM, "bad number of parameters(%d)", numParam);
12233 
12234  if (gpioScript[script_id].state == PI_SCRIPT_IN_USE)
12235  {
12236  pthread_mutex_lock(&gpioScript[script_id].pthMutex);
12237 
12238  if (gpioScript[script_id].run_state != PI_SCRIPT_INITING)
12239  {
12240  if ((numParam > 0) && (param != 0))
12241  {
12242  memcpy(gpioScript[script_id].script.par, param,
12243  sizeof(uint32_t) * numParam);
12244  }
12245 
12246  gpioScript[script_id].request = PI_SCRIPT_RUN;
12247 
12248  pthread_cond_signal(&gpioScript[script_id].pthCond);
12249  }
12250  else
12251  {
12252  status = PI_SCRIPT_NOT_READY;
12253  }
12254 
12255  pthread_mutex_unlock(&gpioScript[script_id].pthMutex);
12256 
12257  return status;
12258  }
12259  else
12260  {
12261  return PI_BAD_SCRIPT_ID;
12262  }
12263 }
12264 
12265 
12266 /* ----------------------------------------------------------------------- */
12267 
12268 int gpioUpdateScript(unsigned script_id, unsigned numParam, uint32_t *param)
12269 {
12270  DBG(DBG_USER, "script_id=%d numParam=%d param=%08X",
12271  script_id, numParam, (uint32_t)param);
12272 
12273  CHECK_INITED;
12274 
12275  if (script_id >= PI_MAX_SCRIPTS)
12276  SOFT_ERROR(PI_BAD_SCRIPT_ID, "bad script id(%d)", script_id);
12277 
12278  if (numParam > PI_MAX_SCRIPT_PARAMS)
12279  SOFT_ERROR(PI_TOO_MANY_PARAM, "bad number of parameters(%d)", numParam);
12280 
12281  if (gpioScript[script_id].state == PI_SCRIPT_IN_USE)
12282  {
12283  if ((numParam > 0) && (param != 0))
12284  {
12285  memcpy(gpioScript[script_id].script.par, param,
12286  sizeof(uint32_t) * numParam);
12287  }
12288  }
12289  else
12290  {
12291  return PI_BAD_SCRIPT_ID;
12292  }
12293 
12294  return 0;
12295 }
12296 
12297 
12298 /* ----------------------------------------------------------------------- */
12299 
12300 int gpioScriptStatus(unsigned script_id, uint32_t *param)
12301 {
12302  DBG(DBG_USER, "script_id=%d param=%08X", script_id, (uint32_t)param);
12303 
12304  CHECK_INITED;
12305 
12306  if (script_id >= PI_MAX_SCRIPTS)
12307  SOFT_ERROR(PI_BAD_SCRIPT_ID, "bad script id(%d)", script_id);
12308 
12309  if (gpioScript[script_id].state == PI_SCRIPT_IN_USE)
12310  {
12311  if (param != NULL)
12312  {
12313  memcpy(param, gpioScript[script_id].script.par,
12314  sizeof(uint32_t) * PI_MAX_SCRIPT_PARAMS);
12315  }
12316 
12317  return gpioScript[script_id].run_state;
12318  }
12319  else return PI_BAD_SCRIPT_ID;
12320 }
12321 
12322 
12323 /* ----------------------------------------------------------------------- */
12324 
12325 int gpioStopScript(unsigned script_id)
12326 {
12327  DBG(DBG_USER, "script_id=%d", script_id);
12328 
12329  CHECK_INITED;
12330 
12331  if (script_id >= PI_MAX_SCRIPTS)
12332  SOFT_ERROR(PI_BAD_SCRIPT_ID, "bad script id(%d)", script_id);
12333 
12334  if (gpioScript[script_id].state == PI_SCRIPT_IN_USE)
12335  {
12336  pthread_mutex_lock(&gpioScript[script_id].pthMutex);
12337 
12338  gpioScript[script_id].request = PI_SCRIPT_HALT;
12339 
12340  if (gpioScript[script_id].run_state == PI_SCRIPT_WAITING)
12341  {
12342  pthread_cond_signal(&gpioScript[script_id].pthCond);
12343  }
12344 
12345  pthread_mutex_unlock(&gpioScript[script_id].pthMutex);
12346 
12347  return 0;
12348  }
12349  else return PI_BAD_SCRIPT_ID;
12350 }
12351 
12352 /* ----------------------------------------------------------------------- */
12353 
12354 int gpioDeleteScript(unsigned script_id)
12355 {
12356  DBG(DBG_USER, "script_id=%d", script_id);
12357 
12358  CHECK_INITED;
12359 
12360  if (script_id >= PI_MAX_SCRIPTS)
12361  SOFT_ERROR(PI_BAD_SCRIPT_ID, "bad script id(%d)", script_id);
12362 
12363  if (gpioScript[script_id].state == PI_SCRIPT_IN_USE)
12364  {
12365  gpioScript[script_id].state = PI_SCRIPT_DYING;
12366 
12367  pthread_mutex_lock(&gpioScript[script_id].pthMutex);
12368 
12369  gpioScript[script_id].request = PI_SCRIPT_HALT;
12370 
12371  if (gpioScript[script_id].run_state == PI_SCRIPT_WAITING)
12372  {
12373  pthread_cond_signal(&gpioScript[script_id].pthCond);
12374  }
12375 
12376  pthread_mutex_unlock(&gpioScript[script_id].pthMutex);
12377 
12378  while (gpioScript[script_id].run_state == PI_SCRIPT_RUNNING)
12379  {
12380  myGpioSleep(0, 5000); /* give script time to halt */
12381  }
12382 
12383  gpioStopThread(gpioScript[script_id].pthIdp);
12384 
12385  if (gpioScript[script_id].script.par)
12386  free(gpioScript[script_id].script.par);
12387 
12388  gpioScript[script_id].script.par = NULL;
12389 
12390  gpioScript[script_id].state = PI_SCRIPT_FREE;
12391 
12392  return 0;
12393  }
12394  else return PI_BAD_SCRIPT_ID;
12395 }
12396 
12397 
12398 
12399 /* ----------------------------------------------------------------------- */
12400 
12401 int gpioSetSignalFunc(unsigned signum, gpioSignalFunc_t f)
12402 {
12403  DBG(DBG_USER, "signum=%d function=%08X", signum, (uint32_t)f);
12404 
12405  CHECK_INITED;
12406 
12407  if (signum > PI_MAX_SIGNUM)
12408  SOFT_ERROR(PI_BAD_SIGNUM, "bad signum (%d)", signum);
12409 
12410  gpioSignal[signum].ex = 0;
12411  gpioSignal[signum].userdata = NULL;
12412 
12413  gpioSignal[signum].func = f;
12414 
12415  return 0;
12416 }
12417 
12418 
12419 /* ----------------------------------------------------------------------- */
12420 
12422  void *userdata)
12423 {
12424  DBG(DBG_USER, "signum=%d function=%08X userdata=%08X",
12425  signum, (uint32_t)f, (uint32_t)userdata);
12426 
12427  CHECK_INITED;
12428 
12429  if (signum > PI_MAX_SIGNUM)
12430  SOFT_ERROR(PI_BAD_SIGNUM, "bad signum (%d)", signum);
12431 
12432  gpioSignal[signum].ex = 1;
12433  gpioSignal[signum].userdata = userdata;
12434 
12435  gpioSignal[signum].func = f;
12436 
12437  return 0;
12438 }
12439 
12440 
12441 /* ----------------------------------------------------------------------- */
12442 
12443 uint32_t gpioRead_Bits_0_31(void)
12444 {
12445  DBG(DBG_USER, "");
12446 
12447  CHECK_INITED;
12448 
12449  return (*(gpioReg + GPLEV0));
12450 }
12451 
12452 
12453 /* ----------------------------------------------------------------------- */
12454 
12455 uint32_t gpioRead_Bits_32_53(void)
12456 {
12457  DBG(DBG_USER, "");
12458 
12459  CHECK_INITED;
12460 
12461  return (*(gpioReg + GPLEV1));
12462 }
12463 
12464 
12465 /* ----------------------------------------------------------------------- */
12466 
12467 int gpioWrite_Bits_0_31_Clear(uint32_t bits)
12468 {
12469  DBG(DBG_USER, "bits=%08X", bits);
12470 
12471  CHECK_INITED;
12472 
12473  *(gpioReg + GPCLR0) = bits;
12474 
12475  return 0;
12476 }
12477 
12478 
12479 /* ----------------------------------------------------------------------- */
12480 
12481 int gpioWrite_Bits_32_53_Clear(uint32_t bits)
12482 {
12483  DBG(DBG_USER, "bits=%08X", bits);
12484 
12485  CHECK_INITED;
12486 
12487  *(gpioReg + GPCLR1) = bits;
12488 
12489  return 0;
12490 }
12491 
12492 
12493 /* ----------------------------------------------------------------------- */
12494 
12495 int gpioWrite_Bits_0_31_Set(uint32_t bits)
12496 {
12497  DBG(DBG_USER, "bits=%08X", bits);
12498 
12499  CHECK_INITED;
12500 
12501  *(gpioReg + GPSET0) = bits;
12502 
12503  return 0;
12504 }
12505 
12506 
12507 /* ----------------------------------------------------------------------- */
12508 
12509 int gpioWrite_Bits_32_53_Set(uint32_t bits)
12510 {
12511  DBG(DBG_USER, "bits=%08X", bits);
12512 
12513  CHECK_INITED;
12514 
12515  *(gpioReg + GPSET1) = bits;
12516 
12517  return 0;
12518 }
12519 
12520 /* ----------------------------------------------------------------------- */
12521 
12522 int gpioHardwareClock(unsigned gpio, unsigned frequency)
12523 {
12524  int cctl[] = {CLK_GP0_CTL, CLK_GP1_CTL, CLK_GP2_CTL};
12525  int cdiv[] = {CLK_GP0_DIV, CLK_GP1_DIV, CLK_GP2_DIV};
12526  int csrc[CLK_SRCS] = {CLK_CTL_SRC_OSC, CLK_CTL_SRC_PLLD};
12527  uint32_t cfreq[CLK_SRCS]={CLK_OSC_FREQ, CLK_PLLD_FREQ};
12528  unsigned clock, mode, mash;
12529  int password = 0;
12530  double f;
12531  clkInf_t clkInf={0,0,0};
12532 
12533  DBG(DBG_USER, "gpio=%d frequency=%d", gpio, frequency);
12534 
12535  CHECK_INITED;
12536 
12537  if ((gpio >> 24) == 0x5A) password = 1;
12538 
12539  gpio &= 0xFFFFFF;
12540 
12541  if (gpio > PI_MAX_GPIO)
12542  SOFT_ERROR(PI_BAD_GPIO, "bad gpio (%d)", gpio);
12543 
12544  if (!clkDef[gpio])
12545  SOFT_ERROR(PI_NOT_HCLK_GPIO, "bad gpio for clock (%d)", gpio);
12546 
12547  if (((frequency < PI_HW_CLK_MIN_FREQ) ||
12548  (frequency > PI_HW_CLK_MAX_FREQ)) &&
12549  (frequency))
12551  "bad hardware clock frequency (%d)", frequency);
12552 
12553  clock = (clkDef[gpio] >> 4) & 3;
12554 
12555  if ((clock == 1) && (!password))
12557  "Need password to use clock 1 (%d)", gpio);
12558 
12559  mode = clkDef[gpio] & 7;
12560  mash = frequency < PI_MASH_MAX_FREQ ? 1 : 0;
12561 
12562  if (frequency)
12563  {
12564  if (chooseBestClock(&clkInf, frequency, CLK_SRCS, cfreq))
12565  {
12566  if (clkInf.frac == 0) mash = 0;
12567 
12568  initHWClk(cctl[clock], cdiv[clock],
12569  csrc[clkInf.clock], clkInf.div, clkInf.frac, mash);
12570 
12571  myGpioSetMode(gpio, mode);
12572 
12573  gpioInfo[gpio].is = GPIO_HW_CLK;
12574 
12575  f = (double) cfreq[clkInf.clock] /
12576  ((double)clkInf.div + ((double)clkInf.frac / 4096.0));
12577 
12578  hw_clk_freq[clock] = (f + 0.5);
12579 
12580  DBG(DBG_USER, "cf=%d div=%d frac=%d mash=%d",
12581  cfreq[clkInf.clock], clkInf.div, clkInf.frac, mash);
12582  }
12583  else
12584  {
12586  "bad hardware clock frequency (%d)", frequency);
12587  }
12588  }
12589  else
12590  {
12591  /* frequency 0, stop clock */
12592  clkReg[cctl[clock]] = BCM_PASSWD | CLK_CTL_KILL;
12593 
12594  if (gpioInfo[gpio].is == GPIO_HW_CLK)
12595  gpioInfo[gpio].is = GPIO_UNDEFINED;
12596  }
12597 
12598  return 0;
12599 }
12600 
12601 /* ----------------------------------------------------------------------- */
12602 
12604  unsigned gpio, unsigned frequency, unsigned dutycycle)
12605 {
12606  uint32_t old_PWM_CTL;
12607  unsigned pwm, mode;
12608  uint32_t real_range, real_dutycycle;
12609 
12610  DBG(DBG_USER, "gpio=%d frequency=%d dutycycle=%d",
12611  gpio, frequency, dutycycle);
12612 
12613  CHECK_INITED;
12614 
12615  if (gpio > PI_MAX_GPIO)
12616  SOFT_ERROR(PI_BAD_GPIO, "bad gpio (%d)", gpio);
12617 
12618  if (!PWMDef[gpio])
12619  SOFT_ERROR(PI_NOT_HPWM_GPIO, "bad gpio for PWM (%d)", gpio);
12620 
12621  if (dutycycle > PI_HW_PWM_RANGE)
12622  SOFT_ERROR(PI_BAD_HPWM_DUTY, "bad PWM dutycycle (%d)", dutycycle);
12623 
12624  if (((frequency < PI_HW_PWM_MIN_FREQ) ||
12625  (frequency > PI_HW_PWM_MAX_FREQ)) &&
12626  (frequency))
12628  "bad hardware PWM frequency (%d)", frequency);
12629 
12630  if (gpioCfg.clockPeriph == PI_CLOCK_PWM)
12631  SOFT_ERROR(PI_HPWM_ILLEGAL, "illegal, PWM in use for main clock");
12632 
12633  pwm = (PWMDef[gpio] >> 4) & 3;
12634  mode = PWMDef[gpio] & 7;
12635 
12636  if (frequency)
12637  {
12638  real_range = ((double)CLK_PLLD_FREQ / (2.0 * frequency)) + 0.5;
12639  real_dutycycle = ((uint64_t)dutycycle * real_range) / PI_HW_PWM_RANGE;
12640 
12641  /* record the set PWM frequency and dutycycle */
12642 
12643  hw_pwm_freq[pwm] =
12644  ((double)CLK_PLLD_FREQ / ( 2.0 * real_range)) + 0.5;
12645 
12646  hw_pwm_duty[pwm] = dutycycle;
12647 
12648  hw_pwm_real_range[pwm] = real_range;
12649 
12650  /* Abort any waveform transmission in progress */
12651 
12652  if (gpioWaveTxBusy()) gpioWaveTxStop();
12653 
12654  waveClockInited = 0;
12655 
12656  /* preserve channel enable only and mark space mode */
12657 
12658  old_PWM_CTL = pwmReg[PWM_CTL] &
12660 
12661  if (!PWMClockInited)
12662  {
12663  pwmReg[PWM_CTL] = 0;
12664 
12665  myGpioDelay(10);
12666 
12668 
12669  PWMClockInited = 1;
12670  }
12671 
12672  if (pwm == 0)
12673  {
12674  pwmReg[PWM_RNG1] = real_range;
12675  myGpioDelay(10);
12676  pwmReg[PWM_DAT1] = real_dutycycle;
12677  myGpioDelay(10);
12678 
12679  pwmReg[PWM_CTL] = (old_PWM_CTL | PWM_CTL_PWEN1 | PWM_CTL_MSEN1);
12680  }
12681  else
12682  {
12683  pwmReg[PWM_RNG2] = real_range;
12684  myGpioDelay(10);
12685  pwmReg[PWM_DAT2] = real_dutycycle;
12686  myGpioDelay(10);
12687 
12688  pwmReg[PWM_CTL] = (old_PWM_CTL | PWM_CTL_PWEN2 | PWM_CTL_MSEN2);
12689  }
12690 
12691  if (gpioInfo[gpio].is != GPIO_HW_PWM)
12692  {
12693  switchFunctionOff(gpio);
12694 
12695  myGpioSetMode(gpio, mode);
12696 
12697  gpioInfo[gpio].is = GPIO_HW_PWM;
12698  }
12699  }
12700  else
12701  {
12702  /* frequency 0, stop PWM */
12703 
12704  if (gpioInfo[gpio].is == GPIO_HW_PWM)
12705  {
12706  if (pwm == 0) pwmReg[PWM_CTL] &= (~PWM_CTL_PWEN1);
12707  else pwmReg[PWM_CTL] &= (~PWM_CTL_PWEN2);
12708 
12709  gpioInfo[gpio].is = GPIO_UNDEFINED;
12710  }
12711  }
12712 
12713  return 0;
12714 }
12715 
12716 
12717 int gpioSetPad(unsigned pad, unsigned padStrength)
12718 {
12719  DBG(DBG_USER, "pad=%d padStrength=%d", pad, padStrength);
12720 
12721  CHECK_INITED;
12722 
12723  if (pad > PI_MAX_PAD)
12724  SOFT_ERROR(PI_BAD_PAD, "bad pad number (%d)", pad);
12725 
12726  if ((padStrength < PI_MIN_PAD_STRENGTH) ||
12727  (padStrength > PI_MAX_PAD_STRENGTH))
12728  SOFT_ERROR(PI_BAD_STRENGTH, "bad pad drive strength (%d)", pad);
12729 
12730  /* 1-16 -> 0-7 */
12731 
12732  padStrength += 1;
12733  padStrength /= 2;
12734  padStrength -= 1;
12735 
12736  padsReg[11+pad] = BCM_PASSWD | 0x18 | (padStrength & 7) ;
12737 
12738  return 0;
12739 }
12740 
12741 int gpioGetPad(unsigned pad)
12742 {
12743  int strength;
12744 
12745  DBG(DBG_USER, "pad=%d", pad);
12746 
12747  CHECK_INITED;
12748 
12749  if (pad > PI_MAX_PAD)
12750  SOFT_ERROR(PI_BAD_PAD, "bad pad (%d)", pad);
12751 
12752  strength = padsReg[11+pad] & 7;
12753 
12754  strength *= 2;
12755  strength += 2;
12756 
12757  return strength;
12758 }
12759 
12760 int shell(char *scriptName, char *scriptString)
12761 {
12762  int status;
12763  char buf[4096];
12764 
12765  DBG(DBG_USER, "name=%s string=%s", scriptName, scriptString);
12766 
12767  CHECK_INITED;
12768 
12769  if (!myScriptNameValid(scriptName))
12770  SOFT_ERROR(PI_BAD_SCRIPT_NAME, "bad script name (%s)", scriptName);
12771 
12772  snprintf(buf, sizeof(buf),
12773  "/opt/pigpio/cgi/%s %s", scriptName, scriptString);
12774 
12775  DBG(DBG_USER, "%s", buf);
12776 
12777  status = system(buf);
12778 
12779  if (status < 0) status = PI_BAD_SHELL_STATUS;
12780 
12781  return status;
12782 }
12783 
12784 
12785 int fileApprove(char *filename)
12786 {
12787  char match[PI_MAX_PATH];
12788  char buffer[PI_MAX_PATH];
12789  char line[PI_MAX_PATH];
12790  char mperm=0;
12791  char perm;
12792  char term;
12793  FILE *f;
12794 
12795  buffer[0] = 0;
12796  match[0] = 0;
12797 
12798  f = fopen("/opt/pigpio/access", "r");
12799 
12800  if (!f) return PI_FILE_NONE;
12801 
12802  while (!feof(f))
12803  {
12804  buffer[0] = 0;
12805  perm = 0;
12806  term = 0;
12807  if (fgets(line, sizeof(line), f))
12808  {
12809  sscanf(line, " %511s %c%c", buffer, &perm, &term);
12810  if (term == 10)
12811  {
12812  if (myPathBad(buffer)) continue; /* disallow risky lines */
12813 
12814  if (fnmatch(buffer, filename, 0) == 0)
12815  {
12816  if (match[0])
12817  {
12818  if (fnmatch(match, buffer, 0) == 0)
12819  {
12820  strcpy(match, buffer);
12821  mperm = perm;
12822  }
12823  }
12824  else
12825  {
12826  strcpy(match, buffer);
12827  mperm = perm;
12828  }
12829  }
12830  }
12831  }
12832  }
12833 
12834  fclose(f);
12835 
12836  if (match[0])
12837  {
12838  switch (toupper(mperm))
12839  {
12840  case 'R': return PI_FILE_READ;
12841  case 'W': return PI_FILE_WRITE;
12842  case 'U': return PI_FILE_RW;
12843  default : return PI_FILE_NONE;
12844  }
12845  }
12846 
12847  return PI_FILE_NONE;
12848 }
12849 
12850 int fileOpen(char *file, unsigned mode)
12851 {
12852  static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
12853  int fd=-1;
12854  int i, slot, oflag, omode;
12855  struct stat statbuf;
12856 
12857  DBG(DBG_USER, "file=%s mode=%d", file, mode);
12858 
12859  CHECK_INITED;
12860 
12861  if ( (mode < PI_FILE_MIN) ||
12862  (mode > PI_FILE_MAX) ||
12863  ((mode & PI_FILE_RW) == 0) )
12864  SOFT_ERROR(PI_BAD_FILE_MODE, "bad mode (%d)", mode);
12865 
12866  if ((fileApprove(file) & mode) == PI_FILE_NONE)
12867  SOFT_ERROR(PI_NO_FILE_ACCESS, "no permission to access file (%s)", file);
12868 
12869  slot = -1;
12870 
12871  pthread_mutex_lock(&mutex);
12872 
12873  for (i=0; i<PI_FILE_SLOTS; i++)
12874  {
12875  if (fileInfo[i].state == PI_FILE_CLOSED)
12876  {
12877  slot = i;
12878  fileInfo[slot].state = PI_FILE_RESERVED;
12879  break;
12880  }
12881  }
12882 
12883  pthread_mutex_unlock(&mutex);
12884 
12885  if (slot < 0) SOFT_ERROR(PI_NO_HANDLE, "no file handles");
12886 
12887  omode = 0;
12888  oflag = 0;
12889 
12890  if (mode & PI_FILE_APPEND)
12891  {
12892  mode |= PI_FILE_WRITE;
12893  oflag |= O_APPEND;
12894  }
12895 
12896  if (mode & PI_FILE_CREATE)
12897  {
12898  oflag |= O_CREAT;
12899  omode |= (S_IRUSR|S_IWUSR);
12900  }
12901 
12902  if (mode & PI_FILE_TRUNC)
12903  {
12904  mode |= PI_FILE_WRITE;
12905  oflag |= O_TRUNC;
12906  }
12907 
12908  switch(mode&PI_FILE_RW)
12909  {
12910  case PI_FILE_READ:
12911  fd = open(file, O_RDONLY|oflag, omode);
12912  break;
12913 
12914  case PI_FILE_WRITE:
12915  fd = open(file, O_WRONLY|oflag, omode);
12916  break;
12917 
12918  case PI_FILE_RW:
12919  fd = open(file, O_RDWR|oflag, omode);
12920  break;
12921  }
12922 
12923  if (fd == -1)
12924  {
12925  fileInfo[slot].state = PI_FILE_CLOSED;
12926  return PI_FIL_OPEN_FAILED;
12927  }
12928  else
12929  {
12930  if (stat(file, &statbuf) == 0)
12931  {
12932  if (S_ISDIR(statbuf.st_mode))
12933  {
12934  close(fd);
12935  fileInfo[slot].state = PI_FILE_CLOSED;
12936  SOFT_ERROR(PI_FILE_IS_A_DIR, "file is a directory (%s)", file);
12937  }
12938  }
12939  }
12940 
12941  fileInfo[slot].fd = fd;
12942  fileInfo[slot].mode = mode;
12943  fileInfo[slot].state = PI_FILE_OPENED;
12944 
12945  return slot;
12946 }
12947 
12948 int fileClose(unsigned handle)
12949 {
12950  DBG(DBG_USER, "handle=%d", handle);
12951 
12952  CHECK_INITED;
12953 
12954  if (handle >= PI_FILE_SLOTS)
12955  SOFT_ERROR(PI_BAD_HANDLE, "bad handle (%d)", handle);
12956 
12957  if (fileInfo[handle].state != PI_FILE_OPENED)
12958  SOFT_ERROR(PI_BAD_HANDLE, "bad handle (%d)", handle);
12959 
12960  if (fileInfo[handle].fd >= 0) close(fileInfo[handle].fd);
12961 
12962  fileInfo[handle].fd = -1;
12963  fileInfo[handle].state = PI_FILE_CLOSED;
12964 
12965  return 0;
12966 }
12967 
12968 int fileWrite(unsigned handle, char *buf, unsigned count)
12969 {
12970  int w;
12971 
12972  DBG(DBG_USER, "handle=%d count=%d [%s]",
12973  handle, count, myBuf2Str(count, buf));
12974 
12975  CHECK_INITED;
12976 
12977  if (handle >= PI_FILE_SLOTS)
12978  SOFT_ERROR(PI_BAD_HANDLE, "bad handle (%d)", handle);
12979 
12980  if (fileInfo[handle].state != PI_FILE_OPENED)
12981  SOFT_ERROR(PI_BAD_HANDLE, "bad handle (%d)", handle);
12982 
12983  if (!count)
12984  SOFT_ERROR(PI_BAD_PARAM, "bad count (%d)", count);
12985 
12986  if (!(fileInfo[handle].mode & PI_FILE_WRITE))
12987  SOFT_ERROR(PI_FILE_NOT_WOPEN, "file not opened for write");
12988 
12989  w = write(fileInfo[handle].fd, buf, count);
12990 
12991  if (w != count)
12992  {
12993  if (w == -1) DBG(DBG_USER, "write failed with errno %d", errno);
12994 
12995  return PI_BAD_FILE_WRITE;
12996  }
12997  return 0;
12998 }
12999 
13000 int fileRead(unsigned handle, char *buf, unsigned count)
13001 {
13002  int r;
13003 
13004  DBG(DBG_USER, "handle=%d count=%d buf=0x%X", handle, count, (unsigned)buf);
13005 
13006  CHECK_INITED;
13007 
13008  if (handle >= PI_FILE_SLOTS)
13009  SOFT_ERROR(PI_BAD_HANDLE, "bad handle (%d)", handle);
13010 
13011  if (fileInfo[handle].state != PI_FILE_OPENED)
13012  SOFT_ERROR(PI_BAD_HANDLE, "bad handle (%d)", handle);
13013 
13014  if (!count)
13015  SOFT_ERROR(PI_BAD_PARAM, "bad count (%d)", count);
13016 
13017  if (!(fileInfo[handle].mode & PI_FILE_READ))
13018  SOFT_ERROR(PI_FILE_NOT_ROPEN, "file not opened for read");
13019 
13020  r = read(fileInfo[handle].fd, buf, count);
13021 
13022  if (r == -1)
13023  {
13024  DBG(DBG_USER, "read failed with errno %d", errno);
13025  return PI_BAD_FILE_READ;
13026  }
13027  else
13028  {
13029  buf[r] = 0;
13030  return r;
13031  }
13032 }
13033 
13034 
13035 int fileSeek(unsigned handle, int32_t seekOffset, int seekFrom)
13036 {
13037  int whence, s;
13038 
13039  DBG(DBG_USER, "handle=%d offset=%d from=%d",
13040  handle, seekOffset, seekFrom);
13041 
13042  CHECK_INITED;
13043 
13044  if (handle >= PI_FILE_SLOTS)
13045  SOFT_ERROR(PI_BAD_HANDLE, "bad handle (%d)", handle);
13046 
13047  if (fileInfo[handle].state != PI_FILE_OPENED)
13048  SOFT_ERROR(PI_BAD_HANDLE, "bad handle (%d)", handle);
13049 
13050  switch (seekFrom)
13051  {
13052  case PI_FROM_START:
13053  whence = SEEK_SET;
13054  break;
13055 
13056  case PI_FROM_CURRENT:
13057  whence = SEEK_CUR;
13058  break;
13059 
13060  case PI_FROM_END:
13061  whence = SEEK_END;
13062  break;
13063 
13064  default:
13065  SOFT_ERROR(PI_BAD_FILE_SEEK, "bad seek from (%d)", seekFrom);
13066  }
13067 
13068  s = lseek(fileInfo[handle].fd, seekOffset, whence);
13069 
13070  if (s == -1)
13071  {
13072  DBG(DBG_USER, "seek failed with errno %d", errno);
13073  return PI_BAD_FILE_SEEK;
13074  }
13075 
13076  return s;
13077 }
13078 
13079 int fileList(char *fpat, char *buf, unsigned count)
13080 {
13081  int len, bufpos;
13082  glob_t pglob;
13083  int i;
13084 
13085  DBG(DBG_USER, "fpat=%s count=%d buf=%x", fpat, count, (unsigned)buf);
13086 
13087  CHECK_INITED;
13088 
13089  if (fileApprove(fpat) == PI_FILE_NONE)
13090  SOFT_ERROR(PI_NO_FILE_ACCESS, "no permission to access file (%s)", fpat);
13091 
13092  bufpos = 0;
13093 
13094  if (glob(fpat, GLOB_MARK, NULL, &pglob) == 0)
13095  {
13096  for (i=0; i<pglob.gl_pathc; i++)
13097  {
13098  len = strlen(pglob.gl_pathv[i]);
13099  if ((bufpos + len + 1) < count)
13100  {
13101  strcpy(buf+bufpos, pglob.gl_pathv[i]);
13102  bufpos += len;
13103  buf[bufpos++] = '\n';
13104  }
13105  }
13106  }
13107  else
13108  {
13109  bufpos = PI_NO_FILE_MATCH;
13110  }
13111 
13112  globfree(&pglob);
13113 
13114  return bufpos;
13115 }
13116 
13117 
13118 
13119 /* ----------------------------------------------------------------------- */
13120 
13121 int gpioTime(unsigned timetype, int *seconds, int *micros)
13122 {
13123  struct timespec ts;
13124 
13125  DBG(DBG_USER, "timetype=%d &seconds=%08X &micros=%08X",
13126  timetype, (uint32_t)seconds, (uint32_t)micros);
13127 
13128  CHECK_INITED;
13129 
13130  if (timetype > PI_TIME_ABSOLUTE)
13131  SOFT_ERROR(PI_BAD_TIMETYPE, "bad timetype (%d)", timetype);
13132 
13133  if (timetype == PI_TIME_ABSOLUTE)
13134  {
13135  clock_gettime(CLOCK_REALTIME, &ts);
13136  *seconds = ts.tv_sec;
13137  *micros = ts.tv_nsec/1000;
13138  }
13139  else
13140  {
13141  clock_gettime(CLOCK_REALTIME, &ts);
13142 
13143  TIMER_SUB(&ts, &libStarted, &ts);
13144 
13145  *seconds = ts.tv_sec;
13146  *micros = ts.tv_nsec/1000;
13147  }
13148 
13149  return 0;
13150 }
13151 
13152 
13153 /* ----------------------------------------------------------------------- */
13154 
13155 int gpioSleep(unsigned timetype, int seconds, int micros)
13156 {
13157  struct timespec ts, rem;
13158 
13159  DBG(DBG_USER, "timetype=%d seconds=%d micros=%d",
13160  timetype, seconds, micros);
13161 
13162  CHECK_INITED;
13163 
13164  if (timetype > PI_TIME_ABSOLUTE)
13165  SOFT_ERROR(PI_BAD_TIMETYPE, "bad timetype (%d)", timetype);
13166 
13167  if (seconds < 0)
13168  SOFT_ERROR(PI_BAD_SECONDS, "bad seconds (%d)", seconds);
13169 
13170  if ((micros < 0) || (micros > 999999))
13171  SOFT_ERROR(PI_BAD_MICROS, "bad micros (%d)", micros);
13172 
13173  ts.tv_sec = seconds;
13174  ts.tv_nsec = micros * 1000;
13175 
13176  if (timetype == PI_TIME_ABSOLUTE)
13177  {
13178  while (clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME, &ts, &rem));
13179  }
13180  else
13181  {
13182  while (clock_nanosleep(CLOCK_REALTIME, 0, &ts, &rem))
13183  {
13184  /* copy remaining time to ts */
13185  ts.tv_sec = rem.tv_sec;
13186  ts.tv_nsec = rem.tv_nsec;
13187  }
13188  }
13189 
13190  return 0;
13191 }
13192 
13193 
13194 /* ----------------------------------------------------------------------- */
13195 
13196 uint32_t gpioDelay(uint32_t micros)
13197 {
13198  uint32_t start;
13199 
13200  DBG(DBG_USER, "microseconds=%u", micros);
13201 
13202  CHECK_INITED;
13203 
13204  start = systReg[SYST_CLO];
13205 
13206  if (micros <= PI_MAX_BUSY_DELAY)
13207  while ((systReg[SYST_CLO] - start) <= micros);
13208  else
13209  gpioSleep(PI_TIME_RELATIVE, (micros/MILLION), (micros%MILLION));
13210 
13211  return (systReg[SYST_CLO] - start);
13212 }
13213 
13214 
13215 /* ----------------------------------------------------------------------- */
13216 
13217 uint32_t gpioTick(void)
13218 {
13219  CHECK_INITED;
13220 
13221  return systReg[SYST_CLO];
13222 }
13223 
13224 
13225 /* ----------------------------------------------------------------------- */
13226 
13227 unsigned gpioVersion(void)
13228 {
13229  DBG(DBG_USER, "");
13230 
13231  return PIGPIO_VERSION;
13232 }
13233 
13234 
13235 /* ----------------------------------------------------------------------- */
13236 
13237 /*
13238 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0
13239 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
13240 
13241 W W S M M M B B B B P P P P T T T T T T T T R R R R
13242 
13243 W warranty void if either bit is set
13244 
13245 S 0=old (bits 0-22 are revision number) 1=new (following fields apply)
13246 
13247 M 0=256 1=512 2=1024
13248 
13249 B 0=Sony 1=Egoman 2=Embest 3=Unknown 4=Embest
13250 
13251 P 0=2835, 1=2836, 2=2837
13252 
13253 T 0=A 1=B 2=A+ 3=B+ 4=Pi2B 5=Alpha 6=Compute Module 7=Unknown 8=Pi3B 9=Zero
13254 
13255 R PCB board revision
13256 
13257 */
13258 
13259 unsigned gpioHardwareRevision(void)
13260 {
13261  static unsigned rev = 0;
13262 
13263  FILE * filp;
13264  char buf[512];
13265  char term;
13266 
13267  DBG(DBG_USER, "");
13268 
13269  if (rev) return rev;
13270 
13271  piCores = 0;
13272 
13273  filp = fopen ("/proc/cpuinfo", "r");
13274 
13275  if (filp != NULL)
13276  {
13277  while (fgets(buf, sizeof(buf), filp) != NULL)
13278  {
13279  if (piCores == 0)
13280  {
13281  if (!strncasecmp("model name", buf, 10))
13282  {
13283  if (strstr (buf, "ARMv6") != NULL)
13284  {
13285  piCores = 1;
13286  pi_peri_phys = 0x20000000;
13287  pi_dram_bus = 0x40000000;
13288  pi_mem_flag = 0x0C;
13289  }
13290  else if (strstr (buf, "ARMv7") != NULL)
13291  {
13292  piCores = 4;
13293  pi_peri_phys = 0x3F000000;
13294  pi_dram_bus = 0xC0000000;
13295  pi_mem_flag = 0x04;
13296  }
13297  else if (strstr (buf, "ARMv8") != NULL)
13298  {
13299  piCores = 4;
13300  pi_peri_phys = 0x3F000000;
13301  pi_dram_bus = 0xC0000000;
13302  pi_mem_flag = 0x04;
13303  }
13304  }
13305  }
13306 
13307  if (!strncasecmp("revision\t:", buf, 10))
13308  {
13309  if (sscanf(buf+10, "%x%c", &rev, &term) == 2)
13310  {
13311  if (term != '\n') rev = 0;
13312  else rev &= 0xFFFFFF; /* mask out warranty bit */
13313  }
13314  }
13315  }
13316 
13317  fclose(filp);
13318  }
13319  return rev;
13320 }
13321 
13322 
13323 /* ----------------------------------------------------------------------- */
13324 
13325 int gpioCfgBufferSize(unsigned millis)
13326 {
13327  DBG(DBG_USER, "millis=%d", millis);
13328 
13330 
13331  if ((millis < PI_BUF_MILLIS_MIN) || (millis > PI_BUF_MILLIS_MAX))
13332  SOFT_ERROR(PI_BAD_BUF_MILLIS, "bad millis (%d)", millis);
13333 
13334  gpioCfg.bufferMilliseconds = millis;
13335 
13336  return 0;
13337 }
13338 
13339 
13340 /* ----------------------------------------------------------------------- */
13341 
13342 int gpioCfgClock(unsigned micros, unsigned peripheral, unsigned source)
13343 {
13344  DBG(DBG_USER, "micros=%d peripheral=%d", micros, peripheral);
13345 
13347 
13348  if ((micros < 1) || (micros > 10))
13349  SOFT_ERROR(PI_BAD_CLK_MICROS, "bad micros (%d)", micros);
13350 
13351  if (!clkCfg[micros].valid)
13352  SOFT_ERROR(PI_BAD_CLK_MICROS, "bad micros (%d)", micros);
13353 
13354  if (peripheral > PI_CLOCK_PCM)
13355  SOFT_ERROR(PI_BAD_CLK_PERIPH, "bad peripheral (%d)", peripheral);
13356 
13357  gpioCfg.clockMicros = micros;
13358  gpioCfg.clockPeriph = peripheral;
13359 
13360  return 0;
13361 }
13362 
13363 
13364 /* ----------------------------------------------------------------------- */
13365 
13366 int gpioCfgDMAchannel(unsigned DMAchannel)
13367 {
13368  DBG(DBG_USER, "channel=%d", DMAchannel);
13369 
13371 
13372  if ((DMAchannel < PI_MIN_DMA_CHANNEL) || (DMAchannel > PI_MAX_DMA_CHANNEL))
13373  SOFT_ERROR(PI_BAD_CHANNEL, "bad channel (%d)", DMAchannel);
13374 
13375  gpioCfg.DMAprimaryChannel = DMAchannel;
13376 
13377  return 0;
13378 }
13379 
13380 
13381 /* ----------------------------------------------------------------------- */
13382 
13383 int gpioCfgDMAchannels(unsigned primaryChannel, unsigned secondaryChannel)
13384 {
13385  DBG(DBG_USER, "primary channel=%d, secondary channel=%d",
13386  primaryChannel, secondaryChannel);
13387 
13389 
13390  if (primaryChannel > PI_MAX_DMA_CHANNEL)
13391  SOFT_ERROR(PI_BAD_PRIM_CHANNEL, "bad primary channel (%d)",
13392  primaryChannel);
13393 
13394  if ((secondaryChannel > PI_MAX_DMA_CHANNEL) ||
13395  (secondaryChannel == primaryChannel))
13396  SOFT_ERROR(PI_BAD_SECO_CHANNEL, "bad secondary channel (%d)",
13397  secondaryChannel);
13398 
13399  gpioCfg.DMAprimaryChannel = primaryChannel;
13400  gpioCfg.DMAsecondaryChannel = secondaryChannel;
13401 
13402  return 0;
13403 }
13404 
13405 
13406 /*-------------------------------------------------------------------------*/
13407 
13409 {
13410  DBG(DBG_USER, "gpio update mask=%llX", updateMask);
13411 
13413 
13414  gpioMask = updateMask;
13415 
13416  gpioMaskSet = 1;
13417 
13418  return 0;
13419 }
13420 
13421 
13422 /* ----------------------------------------------------------------------- */
13423 
13425 {
13426  DBG(DBG_USER, "ifFlags=%X", ifFlags);
13427 
13429 
13430  if (ifFlags > 15)
13431  SOFT_ERROR(PI_BAD_IF_FLAGS, "bad ifFlags (%X)", ifFlags);
13432 
13433  gpioCfg.ifFlags = ifFlags;
13434 
13435  return 0;
13436 }
13437 
13438 /* ----------------------------------------------------------------------- */
13439 
13440 int gpioCfgSocketPort(unsigned port)
13441 {
13442  DBG(DBG_USER, "port=%d", port);
13443 
13445 
13446  if ((port < PI_MIN_SOCKET_PORT) || (port > PI_MAX_SOCKET_PORT))
13447  SOFT_ERROR(PI_BAD_SOCKET_PORT, "bad port (%d)", port);
13448 
13449  gpioCfg.socketPort = port;
13450 
13451  return 0;
13452 }
13453 
13454 
13455 /* ----------------------------------------------------------------------- */
13456 
13458 {
13459  DBG(DBG_USER, "memAllocMode=%d", memAllocMode);
13460 
13462 
13463  if (memAllocMode > PI_MEM_ALLOC_MAILBOX)
13464  SOFT_ERROR(
13465  PI_BAD_MALLOC_MODE, "bad mem alloc mode (%d)", memAllocMode);
13466 
13467  gpioCfg.memAllocMode = memAllocMode;
13468 
13469  return 0;
13470 }
13471 
13472 /* ----------------------------------------------------------------------- */
13473 
13474 int gpioCfgNetAddr(int numSockAddr, uint32_t *sockAddr)
13475 {
13476  int i;
13477 
13478  DBG(DBG_USER, "numSockAddr=%d sockAddr=%08X",
13479  numSockAddr, (unsigned)sockAddr);
13480 
13482 
13483  if (numSockAddr <= 0) numSockNetAddr = 0;
13484  else
13485  {
13486  if (numSockAddr >= MAX_CONNECT_ADDRESSES)
13487  numSockAddr = MAX_CONNECT_ADDRESSES;
13488 
13489  for (i=0; i<numSockAddr; i++) sockNetAddr[i] = sockAddr[i];
13490 
13491  numSockNetAddr = numSockAddr;
13492  }
13493  return 0;
13494 }
13495 
13496 
13497 /* ----------------------------------------------------------------------- */
13498 
13499 uint32_t gpioCfgGetInternals(void)
13500 {
13501  return gpioCfg.internals;
13502 }
13503 
13504 int gpioCfgSetInternals(uint32_t cfgVal)
13505 {
13506  gpioCfg.internals = cfgVal;
13507  gpioCfg.dbgLevel = cfgVal & 0xF;
13508  gpioCfg.alertFreq = (cfgVal>>4) & 0xF;
13509  return 0;
13510 }
13511 
13512 int gpioCfgInternals(unsigned cfgWhat, unsigned cfgVal)
13513 {
13514  int retVal = PI_BAD_CFG_INTERNAL;
13515 
13516  DBG(DBG_USER, "cfgWhat=%u, cfgVal=%d", cfgWhat, cfgVal);
13517 
13518  switch(cfgWhat)
13519  {
13520  case 562484977:
13521 
13522  if (cfgVal) gpioCfg.internals |= PI_CFG_STATS;
13523  else gpioCfg.internals &= (~PI_CFG_STATS);
13524 
13525  DBG(DBG_ALWAYS, "show stats is %u", cfgVal);
13526 
13527  retVal = 0;
13528 
13529  break;
13530 
13531  case 984762879:
13532 
13533  if ((cfgVal >= DBG_ALWAYS) && (cfgVal <= DBG_MAX_LEVEL))
13534  {
13535 
13536  gpioCfg.dbgLevel = cfgVal;
13537  gpioCfg.internals = (gpioCfg.internals & (~0xF)) | cfgVal;
13538 
13539  DBG(DBG_ALWAYS, "Debug level is %u", cfgVal);
13540 
13541  retVal = 0;
13542  }
13543 
13544  break;
13545  }
13546 
13547  return retVal;
13548 }
13549 
13550 
13551 /* include any user customisations */
13552 
13553 #include "custom.cext"
13554 
static void * pthTimerTick(void *x)
Definition: pigpio.c:6751
#define PI_DEPRECATED
Definition: pigpio.h:6385
double time_time(void)
Definition: pigpio.c:8360
void(* gpioISRFunc_t)(int gpio, int level, uint32_t tick)
Definition: pigpio.h:527
int readPos
Definition: pigpio.c:1106
#define PI_I2C_FUNC_SMBUS_READ_BYTE_DATA
Definition: pigpio.c:808
int fileRead(unsigned handle, char *buf, unsigned count)
Definition: pigpio.c:13000
#define PI_SPI_FLAGS_GET_CSPOL(x)
Definition: pigpio.c:865
static FILE * outFifo
Definition: pigpio.c:1298
#define PI_LOCKFILE
Definition: pigpio.h:387
#define PI_BAD_USER_GPIO
Definition: pigpio.h:6264
uint32_t tick
Definition: pigpio.h:418
#define PI_SPI_MIN_BAUD
Definition: pigpio.h:648
uint16_t deferRng
Definition: pigpio.c:912
#define PI_FIL_OPEN_FAILED
Definition: pigpio.h:6393
#define PI_BAD_STOPBITS
Definition: pigpio.h:6367
#define PI_WFRX_SPI_CS
Definition: pigpio.c:767
d
#define PI_I2C_SMBUS
Definition: pigpio.c:787
int gpioWaveGetCbs(void)
Definition: pigpio.c:10176
#define PI_CMD_PRS
Definition: pigpio.h:6067
static volatile uint32_t nFilterBits
Definition: pigpio.c:1257
#define PI_FILE_IS_A_DIR
Definition: pigpio.h:6403
unsigned DMAsecondaryChannel
Definition: pigpio.c:1076
#define PI_BAD_CHAIN_CMD
Definition: pigpio.h:6381
static void myLvsPageSlot(int pos, int *page, int *slot)
Definition: pigpio.c:1705
static int scrSys(char *cmd, uint32_t p1, uint32_t p2)
Definition: pigpio.c:6490
static int mbCreate(char *dev)
Definition: pigpio.c:2681
#define DMA_SRC_INC
Definition: pigpio.c:378
int gpioSetSignalFuncEx(unsigned signum, gpioSignalFuncEx_t f, void *userdata)
Definition: pigpio.c:12421
static uint32_t dmaPwmDataAdr(int pos)
Definition: pigpio.c:5282
#define PI_CMD_WVSC
Definition: pigpio.h:6097
int gpioSetSignalFunc(unsigned signum, gpioSignalFunc_t f)
Definition: pigpio.c:12401
#define MAX_EMITS
Definition: pigpio.c:778
#define PCM_CS_DMAEN
Definition: pigpio.c:428
#define PI_CMD_FL
Definition: pigpio.h:6186
char rxBuf[BSC_FIFO_SIZE]
Definition: pigpio.h:505
#define MAX_CONNECT_ADDRESSES
Definition: pigpio.h:906
#define PI_BAD_CHAIN_DELAY
Definition: pigpio.h:6382
#define PI_WF_MICROS
Definition: pigpio.c:769
#define PI_FROM_END
Definition: pigpio.h:902
#define PI_WFRX_I2C_SDA
Definition: pigpio.c:762
#define MILLION
Definition: pigpio.c:192
static int read_SDA(wfRx_t *w)
Definition: pigpio.c:10209
void * userdata
Definition: pigpio.c:972
#define PI_CMD_CF1
Definition: pigpio.h:6154
uint8_t * buf
Definition: pigpio.h:494
static volatile uint32_t scriptEventBits
Definition: pigpio.c:1260
s
Definition: DHT22.py:257
#define DBG_MIN_LEVEL
Definition: pigpio.c:656
#define CLK_GP0_DIV
Definition: pigpio.c:506
int fileOpen(char *file, unsigned mode)
Definition: pigpio.c:12850
int SDAMode
Definition: pigpio.c:1126
#define PI_INIT_FAILED
Definition: pigpio.h:6263
#define PI_CMD_HWVER
Definition: pigpio.h:6078
static int waveOutBotOOL
Definition: pigpio.c:1246
int i2cBlockProcessCall(unsigned handle, unsigned reg, char *buf, unsigned count)
Definition: pigpio.c:3731
uint32_t stride
Definition: pigpio.h:484
int eventSetFuncEx(unsigned event, eventFuncEx_t f, void *userdata)
Definition: pigpio.c:11218
#define PI_CMD_BR1
Definition: pigpio.h:6071
#define PI_SPI_FLAGS_GET_AUX_SPI(x)
Definition: pigpio.c:858
#define PI_BAD_CHANNEL
Definition: pigpio.h:6290
int gpioServo(unsigned gpio, unsigned val)
Definition: pigpio.c:8978
int spiRead(unsigned handle, char *buf, unsigned count)
Definition: pigpio.c:4734
int myPathBad(char *name)
Definition: pigpio.c:1538
static gpioISR_t gpioISR[PI_MAX_GPIO+1]
Definition: pigpio.c:1272
#define CLK_CTL_SRC(x)
Definition: pigpio.c:492
int i2cReadI2CBlockData(unsigned handle, unsigned reg, char *buf, unsigned count)
Definition: pigpio.c:3782
#define DBG_SCRIPT
Definition: pigpio.c:660
#define PI_CMD_BR2
Definition: pigpio.h:6072
uint32_t level[LVS_PER_IPAGE]
Definition: pigpio.c:889
#define PI_SPI_FLAGS_GET_CPHA(x)
Definition: pigpio.c:863
static uint64_t updateMask
Definition: pigpiod.c:63
#define PI_SCRIPT_IN_USE
Definition: pigpio.c:841
#define SUPERLEVEL
Definition: pigpio.c:724
#define PI_CMD_NC
Definition: pigpio.h:6082
#define THOUSAND
Definition: pigpio.c:191
#define BSC_RSR
Definition: pigpio.h:736
static uint32_t hw_pwm_duty[2]
Definition: pigpio.c:1333
void bscTerm(int mode)
Definition: pigpio.c:10572
#define MAX_SAMPLE
Definition: pigpio.c:774
int i2cWriteI2CBlockData(unsigned handle, unsigned reg, char *buf, unsigned count)
Definition: pigpio.c:3837
static void * pthSocketThread(void *x)
Definition: pigpio.c:7038
static fileInfo_t fileInfo[PI_FILE_SLOTS]
Definition: pigpio.c:1280
static void chainSetCntVal(int counter, int slot, uint32_t value)
Definition: pigpio.c:9636
static unsigned dmaCurrentSlot(unsigned pos)
Definition: pigpio.c:5268
#define PI_CMD_XA
Definition: pigpio.h:6257
bool param(const std::string &param_name, T &param_val, const T &default_val)
#define DMA_ACTIVATE
Definition: pigpio.c:369
static unsigned ifFlags
Definition: pigpiod.c:57
#define PI_DEFAULT_UPDATE_MASK_ZERO
Definition: pigpio.h:6436
#define PI_SPI_FLAGS_GET_RESVD(x)
Definition: pigpio.c:859
static void stopHardwarePWM(void)
Definition: pigpio.c:8586
int8_t opt[4]
Definition: command.h:90
#define CLK_PLLD_FREQ
Definition: pigpio.c:500
static unsigned old_mode_miso
Definition: pigpio.c:1365
static void * pthAlertThread(void *x)
Definition: pigpio.c:6162
#define AUXSPI_STAT_RX_EMPTY
Definition: pigpio.c:643
#define PI_CMD_PROCR
Definition: pigpio.h:6101
#define PI_CMD_ADD
Definition: pigpio.h:6217
cmdInfo_t cmdInfo[]
Definition: command.c:41
int serWriteByte(unsigned handle, unsigned bVal)
Definition: pigpio.c:4909
#define PI_MAX_BUSY_DELAY
Definition: pigpio.h:778
#define PI_MAX_SCRIPTS
Definition: pigpio.h:795
#define NORMAL_DMA
Definition: pigpio.c:649
#define PI_CMD_WVTXR
Definition: pigpio.h:6113
#define PI_DEFAULT_CLK_PERIPHERAL
Definition: pigpio.h:6423
int gpioWaveGetHighCbs(void)
Definition: pigpio.c:10187
#define PI_CMD_RL
Definition: pigpio.h:6247
pthread_t * pthIdp
Definition: pigpio.c:996
static uintptr_t ** dmaPMapBlk
Definition: pigpio.c:1307
#define CMD_P_ARR
Definition: command.h:47
#define PI_BAD_WAVE_ID
Definition: pigpio.h:6331
#define PI_BAD_CFG_INTERNAL
Definition: pigpio.h:6298
#define SPI_CS_RXD
Definition: pigpio.c:537
uint32_t src
Definition: pigpio.h:481
int16_t fd
Definition: pigpio.c:1040
static int dmaNowAtOCB(void)
Definition: pigpio.c:5185
#define CHECK_NOT_INITED
Definition: pigpio.c:254
static volatile uint32_t * pwmReg
Definition: pigpio.c:1324
uint32_t shortPipeWrite
Definition: pigpio.c:1066
#define PI_PERI_BUS
Definition: pigpio.c:300
#define PI_CMD_I2CWS
Definition: pigpio.h:6122
static int scrWait(gpioScript_t *s, uint32_t bits)
Definition: pigpio.c:6466
static void myTckPageSlot(int pos, int *page, int *slot)
Definition: pigpio.c:1713
uint16_t numTOOL
Definition: pigpio.h:464
#define PI_BB_SER_MAX_BAUD
Definition: pigpio.h:640
#define PI_CMD_DCRA
Definition: pigpio.h:6224
#define PADS_LEN
Definition: pigpio.c:319
#define PI_MAX_PATH
Definition: pigpio.c:875
wfRxSerial_t s
Definition: pigpio.c:1154
#define PI_ON
Definition: pigpio.h:567
static unsigned mbReleaseMemory(int fd, unsigned handle)
Definition: pigpio.c:2773
static void intScriptEventBits(void)
Definition: pigpio.c:11705
static void spiGo(unsigned speed, uint32_t flags, char *txBuf, char *rxBuf, unsigned count)
Definition: pigpio.c:4499
static int fdSock
Definition: pigpio.c:1302
#define PI_I2C_FUNC_SMBUS_WRITE_I2C_BLOCK
Definition: pigpio.c:816
uint32_t numSamples
Definition: pigpio.c:1062
#define PI_NOT_PWM_GPIO
Definition: pigpio.h:6357
#define DMA_WAIT_ON_WRITES
Definition: pigpio.c:364
unsigned ex
Definition: pigpio.c:943
#define PI_CMD_BC2
Definition: pigpio.h:6074
#define NUM_CBS
Definition: pigpio.c:721
#define MB_END_TAG
Definition: pigpio.c:831
#define PAGE_SIZE
Definition: pigpio.c:678
#define PI_SCRIPT_HALTED
Definition: pigpio.h:804
#define DMA_CHANNEL_RESET
Definition: pigpio.c:363
uint32_t startBitTick
Definition: pigpio.c:1111
int SCLMode
Definition: pigpio.c:1127
static void * pthISRThread(void *x)
Definition: pigpio.c:11335
static void waveRxBit(int gpio, int level, uint32_t tick)
Definition: pigpio.c:3192
uint16_t flags
Definition: pigpio.h:417
int fileSeek(unsigned handle, int32_t seekOffset, int seekFrom)
Definition: pigpio.c:13035
#define AUXSPI_CNTL0_ENABLE
Definition: pigpio.c:625
int txCnt
Definition: pigpio.h:506
#define PI_BAD_MALLOC_MODE
Definition: pigpio.h:6369
void rawWaveSetIn(int pos, uint32_t value)
Definition: pigpio.c:8337
static volatile uint32_t piCores
Definition: pigpio.c:1200
#define PCM_DREQ_TX_REQ_L(x)
Definition: pigpio.c:469
#define GPLEV0
Definition: pigpio.c:335
#define BCM_PASSWD
Definition: pigpio.c:486
uint16_t flags
Definition: pigpio.h:492
#define SYST_BASE
Definition: pigpio.c:312
uint32_t gpioOff
Definition: pigpio.h:435
int gpioInitialise(void)
Definition: pigpio.c:8459
#define CLK_DIV_DIVF(x)
Definition: pigpio.c:503
static void intNotifyBits(void)
Definition: pigpio.c:11724
#define PI_I2C_COMBINED_OFF
Definition: pigpio.h:715
uint32_t data
Definition: pigpio.c:1114
#define PI_BAD_SHELL_STATUS
Definition: pigpio.h:6404
#define SPI_CS_CSPOL(x)
Definition: pigpio.c:546
#define PI_BAD_HPWM_FREQ
Definition: pigpio.h:6361
#define PI_BAD_IF_FLAGS
Definition: pigpio.h:6289
static uint32_t _spiTXBits(char *buf, int pos, int bitlen, int msbf)
Definition: pigpio.c:4250
int rxCnt
Definition: pigpio.h:504
static void set_CS(wfRx_t *w)
Definition: pigpio.c:10671
int serRead(unsigned handle, char *buf, unsigned count)
Definition: pigpio.c:5000
int initInitialise(void)
Definition: pigpio.c:8053
#define PI_CMD_POP
Definition: pigpio.h:6242
#define PI_WAVE_BLOCKS
Definition: pigpio.h:629
unsigned run_state
Definition: pigpio.c:992
int gpioSetAlertFunc(unsigned gpio, gpioAlertFunc_t f)
Definition: pigpio.c:11303
static volatile uint32_t * auxReg
Definition: pigpio.c:1317
#define PI_TIME_RELATIVE
Definition: pigpio.h:816
#define PI_BAD_SPI_SPEED
Definition: pigpio.h:6343
static uint32_t hw_clk_freq[3]
Definition: pigpio.c:1331
#define PWM_TIMER
Definition: pigpio.c:654
#define PI_CMD_FO
Definition: pigpio.h:6181
#define PI_BAD_STRENGTH
Definition: pigpio.h:6392
#define PAGES_PER_BLOCK
Definition: pigpio.c:685
#define PI_STARTING
Definition: pigpio.c:867
#define PI_FILE_RW
Definition: pigpio.h:894
static void clear_SDA(wfRx_t *w)
Definition: pigpio.c:10220
void rawDumpWave(void)
Definition: pigpio.c:8394
static int fdLock
Definition: pigpio.c:1300
#define PI_NTFY_FLAGS_EVENT
Definition: pigpio.h:624
#define PI_BAD_WVSP_COMMND
Definition: pigpio.h:6309
uint32_t bits
Definition: pigpio.c:973
uint32_t gpioCfgGetInternals(void)
Definition: pigpio.c:13499
static unsigned dmaNowAtICB(void)
Definition: pigpio.c:5141
#define PI_I2C_WRITE
Definition: pigpio.h:719
unsigned ex
Definition: pigpio.c:956
uint16_t state
Definition: pigpio.c:1030
#define PI_I2C_SMBUS_QUICK
Definition: pigpio.c:792
callbk_t func
Definition: pigpio.c:963
int * par
Definition: command.h:100
#define PI_CMD_PFS
Definition: pigpio.h:6068
rawWaveInfo_t rawWaveInfo(int wave_id)
Definition: pigpio.c:8350
unsigned gpio
Definition: pigpio.c:951
#define DMA_INTERRUPT_STATUS
Definition: pigpio.c:367
uint32_t pulses
Definition: pigpio.c:1094
#define PWM_DMAC
Definition: pigpio.c:394
#define PI_CHAIN_LOOP_CNT
Definition: pigpio.h:6378
#define PI_TOO_MANY_SEGS
Definition: pigpio.h:6370
static int spiAnyOpen(uint32_t flags)
Definition: pigpio.c:4523
#define SPI_DLEN
Definition: pigpio.c:527
int gpioNotifyBegin(unsigned handle, uint32_t bits)
Definition: pigpio.c:11747
uint16_t range
Definition: pigpio.c:909
#define CLK_CTL_BUSY
Definition: pigpio.c:489
#define PI_BAD_MS
Definition: pigpio.h:6272
#define PI_FROM_START
Definition: pigpio.h:900
#define PADS_BASE
Definition: pigpio.c:308
static void waveSetOOL(int pos, uint32_t OOL)
Definition: pigpio.c:2873
int gpioSetWatchdog(unsigned gpio, unsigned timeout)
Definition: pigpio.c:11850
#define PI_SER_READ_FAILED
Definition: pigpio.h:6351
static void _spiRXBits(char *buf, int pos, int bitlen, int msbf, uint32_t bits)
Definition: pigpio.c:4266
#define PI_INPFIFO
Definition: pigpio.h:380
static void * pthSocketThreadHandler(void *fdC)
Definition: pigpio.c:6897
uint32_t OOL[OOL_PER_OPAGE]
Definition: pigpio.c:900
#define PI_CMD_PFG
Definition: pigpio.h:6084
#define PI_I2C_FUNC_SMBUS_WRITE_WORD_DATA
Definition: pigpio.c:811
static char * myBuf2Str(unsigned count, char *buf)
Definition: pigpio.c:1579
static uint32_t hw_pwm_freq[2]
Definition: pigpio.c:1332
static void waveCBsOOLs(int *numCBs, int *numBOOLs, int *numTOOLs)
Definition: pigpio.c:2932
#define PI_BAD_FOREVER
Definition: pigpio.h:6389
#define PI_BAD_MODE
Definition: pigpio.h:6266
#define PI_MAX_EVENT
Definition: pigpio.h:910
#define PI_BB_SPI_MAX_BAUD
Definition: pigpio.h:637
#define PI_SPI_FLAGS_GET_BITLEN(x)
Definition: pigpio.c:853
uint32_t periphData
Definition: pigpio.c:893
int gpioCfgDMAchannels(unsigned primaryChannel, unsigned secondaryChannel)
Definition: pigpio.c:13383
#define PWM_RNG1
Definition: pigpio.c:395
int gpioSetPWMrange(unsigned gpio, unsigned range)
Definition: pigpio.c:8807
int gpioNotifyOpenWithSize(int bufSize)
Definition: pigpio.c:11572
#define PI_SPI_FLAGS_GET_RX_LSB(x)
Definition: pigpio.c:854
#define PI_I2C_SMBUS_BYTE
Definition: pigpio.c:793
#define PI_ASPI_SCLK
Definition: pigpio.c:580
static uint32_t chainGetCntValPadr(int counter, int slot)
Definition: pigpio.c:9646
static int addrAllowed(struct sockaddr *saddr)
Definition: pigpio.c:7017
#define PI_BAD_PAD
Definition: pigpio.h:6391
uint32_t lastReportTick
Definition: pigpio.c:1015
uint16_t valid
Definition: pigpio.c:1005
int bbI2CZip(unsigned SDA, char *inBuf, unsigned inLen, char *outBuf, unsigned outLen)
Definition: pigpio.c:10410
int gpioSerialRead(unsigned gpio, void *buf, size_t bufSize)
Definition: pigpio.c:11100
int gpioWaveTxStart(unsigned wave_mode)
Definition: pigpio.c:9513
#define DMA_DEBUG
Definition: pigpio.c:360
uint32_t cbs
Definition: pigpio.c:1097
#define PI_HW_PWM_RANGE
Definition: pigpio.h:615
#define AUX_BASE
Definition: pigpio.c:302
#define PI_BAD_MICROS
Definition: pigpio.h:6275
#define PCM_MODE_FLEN(x)
Definition: pigpio.c:446
int i2cReadByteData(unsigned handle, unsigned reg)
Definition: pigpio.c:3436
static dmaIPage_t ** dmaIBus
Definition: pigpio.c:1312
static void mbClose(int fd)
Definition: pigpio.c:2706
#define PI_I2C_FLAGS
Definition: pigpio.h:717
#define MB_DEV1
Definition: pigpio.c:826
unsigned alert_delays[]
Definition: pigpio.c:5572
static volatile uint32_t * padsReg
Definition: pigpio.c:1322
#define AUXSPI_CNTL0_MSB_FIRST(x)
Definition: pigpio.c:630
static dmaPage_t ** dmaVirt
Definition: pigpio.c:1308
#define PI_CMD_FG
Definition: pigpio.h:6170
uint32_t eventBits
Definition: pigpio.c:994
#define PI_MAX_PAD
Definition: pigpio.h:883
static void myOffPageSlot(int pos, int *page, int *slot)
Definition: pigpio.c:1697
int gpioWaveGetHighMicros(void)
Definition: pigpio.c:10121
#define PI_BAD_SOCKET_PORT
Definition: pigpio.h:6292
#define PI_BAD_I2C_WLEN
Definition: pigpio.h:6374
#define PI_CMD_GPW
Definition: pigpio.h:6149
pthread_t * gpioStartThread(gpioThreadFunc_t f, void *userdata)
Definition: pigpio.c:12098
#define PI_SER_SLOTS
Definition: pigpio.h:683
#define PI_MAX_ACTIVE
Definition: pigpio.h:861
#define BSC_FR_TXFF
Definition: pigpio.h:765
#define SRX_BUF_SIZE
Definition: pigpio.c:780
uint32_t rawWaveGetIn(int pos)
Definition: pigpio.c:8322
#define PI_LOCALHOST_SOCK_IF
Definition: pigpio.h:849
#define PI_DEFAULT_SOCKET_PORT
Definition: pigpio.h:6429
static volatile uint32_t * clkReg
Definition: pigpio.c:1319
#define PI_TIME_ABSOLUTE
Definition: pigpio.h:817
#define PI_MIN_SERVO_PULSEWIDTH
Definition: pigpio.h:608
#define MB_DEV2
Definition: pigpio.c:827
static void sigSetHandler(void)
Definition: pigpio.c:5537
#define PI_CMD_SPIR
Definition: pigpio.h:6136
#define PI_WAVE_MODE_REPEAT
Definition: pigpio.h:669
#define CLK_BASE
Definition: pigpio.c:304
#define PI_CMD_PUSHA
Definition: pigpio.h:6245
#define PI_SPI_FLAGS_GET_TX_LSB(x)
Definition: pigpio.c:855
#define PI_BAD_SMBUS_CMD
Definition: pigpio.h:6372
static int fdMem
Definition: pigpio.c:1301
#define PI_BAD_PUD
Definition: pigpio.h:6268
uint32_t maxMicros
Definition: pigpio.c:1093
#define PI_DEFAULT_UPDATE_MASK_COMPUTE
Definition: pigpio.h:6439
#define PI_BAD_CLK_MICROS
Definition: pigpio.h:6281
#define PI_WAVE_MAX_MICROS
Definition: pigpio.h:657
#define PI_WAVE_MODE_ONE_SHOT
Definition: pigpio.h:668
int i2cReadWordData(unsigned handle, unsigned reg)
Definition: pigpio.c:3514
#define PI_CMD_I2CWB
Definition: pigpio.h:6124
uint32_t tick
Definition: pigpio.h:410
uintptr_t bus_addr
Definition: pigpio.c:1191
#define PI_BB_I2C_MIN_BAUD
Definition: pigpio.h:633
unsigned alertFreq
Definition: pigpio.c:1081
#define PI_CMD_SPIX
Definition: pigpio.h:6138
#define PI_SER_CLOSED
Definition: pigpio.c:745
uint32_t control
Definition: pigpio.h:503
#define PI_DEFAULT_UPDATE_MASK_B1
Definition: pigpio.h:6433
#define PI_CHAIN_TOO_BIG
Definition: pigpio.h:6384
#define PI_BAD_FLAGS
Definition: pigpio.h:6342
#define SUPERCYCLE
Definition: pigpio.c:723
#define PI_CMD_MODES
Definition: pigpio.h:6061
#define PI_CMD_BI2CC
Definition: pigpio.h:6157
void(* gpioTimerFuncEx_t)(void *userdata)
Definition: pigpio.h:538
static void alertWdogCheck(gpioSample_t *sample, int numSamples)
Definition: pigpio.c:6130
static int pthAlertRunning
Definition: pigpio.c:1264
static uint32_t * waveEndPtr
Definition: pigpio.c:1250
#define PI_EVENT_BSC
Definition: pigpio.h:914
#define PWM_DAT2
Definition: pigpio.c:399
int eventSetFunc(unsigned event, eventFunc_t f)
Definition: pigpio.c:11201
uint32_t maxPulses
Definition: pigpio.c:1096
#define SPI_CS_CLEAR(x)
Definition: pigpio.c:547
int bbI2COpen(unsigned SDA, unsigned SCL, unsigned baud)
Definition: pigpio.c:10332
uint32_t length
Definition: pigpio.h:483
uint32_t fullBit
Definition: pigpio.c:1108
#define PI_CMD_SPIW
Definition: pigpio.h:6137
#define PI_SCRIPT_DELETE
Definition: pigpio.c:846
#define PI_I2C_RDRW_IOCTL_MAX_MSGS
Definition: pigpio.h:695
static void clear_SCLK(wfRx_t *w)
Definition: pigpio.c:10686
rawCbs_t * rawWaveCBAdr(int cbNum)
Definition: pigpio.c:2839
#define PI_WFRX_SPI_SCLK
Definition: pigpio.c:764
static void sigHandler(int signum)
Definition: pigpio.c:5477
#define PI_FROM_CURRENT
Definition: pigpio.h:901
#define PWM_CTL_MODE1
Definition: pigpio.c:406
int getBitInBytes(int bitPos, char *buf, int numBits)
Definition: pigpio.c:8235
int gpioGetServoPulsewidth(unsigned gpio)
Definition: pigpio.c:9016
#define PCM_GRAY
Definition: pigpio.c:421
#define CMD_MAX_EXTENSION
Definition: command.h:41
int MISO
Definition: pigpio.c:1134
uint32_t changedBits
Definition: pigpio.c:995
#define PI_CFG_ALERT_FREQ
Definition: pigpio.h:866
#define PULSE_PER_CYCLE
Definition: pigpio.c:683
#define PI_EMPTY_WAVEFORM
Definition: pigpio.h:6334
#define PI_HW_PWM_MAX_FREQ
Definition: pigpio.h:614
int spiFlags
Definition: pigpio.c:1139
static const uint16_t pwmRealRange[PWM_FREQS]
Definition: pigpio.c:1467
#define PI_BAD_FILE_WRITE
Definition: pigpio.h:6397
static void bbSPIStop(wfRx_t *w)
Definition: pigpio.c:10707
int nfActiveUs
Definition: pigpio.c:926
#define PI_CMD_BC1
Definition: pigpio.h:6073
static void wfRx_unlock(int i)
Definition: pigpio.c:1815
uint16_t width
Definition: pigpio.c:908
#define PI_BAD_DATABITS
Definition: pigpio.h:6366
#define PI_WAVE_MAX_PULSES
Definition: pigpio.h:630
static uint32_t old_spi_cntl0
Definition: pigpio.c:1378
uint32_t halfBit
Definition: pigpio.c:1109
#define BSC_ICR
Definition: pigpio.h:744
#define PI_CMD_WVNEW
Definition: pigpio.h:6114
#define PCM_TXC_CH1WID(x)
Definition: pigpio.c:461
uint8_t pad
Definition: pigpio.c:907
unsigned memAllocMode
Definition: pigpio.c:1079
#define PI_CMD_PRG
Definition: pigpio.h:6083
void(* eventFuncEx_t)(int event, uint32_t tick, void *userdata)
Definition: pigpio.h:523
static void myGpioSleep(int seconds, int micros)
Definition: pigpio.c:1646
static void I2CStart(wfRx_t *w)
Definition: pigpio.c:10246
#define PI_BB_I2C_MAX_BAUD
Definition: pigpio.h:634
#define PI_CMD_WVSP
Definition: pigpio.h:6096
#define PI_I2C_COMBINED
Definition: pigpio.h:389
#define AUX_SPI0_CNTL0_REG
Definition: pigpio.c:599
#define DMA_BASE
Definition: pigpio.c:305
#define AUXSPI_CNTL0_IN_RISING(x)
Definition: pigpio.c:626
#define SPI_CS_CSPOLS(x)
Definition: pigpio.c:533
#define FLUSH_PAGES
Definition: pigpio.c:820
#define PI_CMD_I2CRK
Definition: pigpio.h:6127
static unsigned old_mode_asclk
Definition: pigpio.c:1374
#define PI_SPI_FLAGS_GET_CHANNEL(x)
Definition: pigpio.c:852
#define PI_I2C_READ
Definition: pigpio.h:718
static void initClearGlobals(void)
Definition: pigpio.c:7763
uint16_t botOOL
Definition: pigpio.h:457
uint32_t gpioOn[ON_PER_IPAGE]
Definition: pigpio.c:892
#define PI_NOTIFY_CLOSED
Definition: pigpio.c:753
#define PI_CMD_LDA
Definition: pigpio.h:6236
#define PI_CMD_SYS
Definition: pigpio.h:6254
#define PI_SER_RESERVED
Definition: pigpio.c:746
#define PI_NOTIFY_RESERVED
Definition: pigpio.c:754
int SCL
Definition: pigpio.c:1124
#define PI_CMD_SERDA
Definition: pigpio.h:6146
#define PI_BAD_PARAM
Definition: pigpio.h:6346
#define PI_CMD_WAIT
Definition: pigpio.h:6255
static void dmaCbPrint(int pos)
Definition: pigpio.c:5129
#define PI_HW_CLK_MIN_FREQ
Definition: pigpio.h:619
int SCLK
Definition: pigpio.c:1136
uint32_t cbTicks
Definition: pigpio.c:1057
int gpioGetMode(unsigned gpio)
Definition: pigpio.c:8641
#define PI_FILE_MAX
Definition: pigpio.h:898
#define DMA_DEBUG_READ_ERR
Definition: pigpio.c:385
int fired
Definition: pigpio.c:946
int gpioWaveTxStop(void)
Definition: pigpio.c:10094
#define CMD_VAR
Definition: command.h:51
#define PI_BB_SER_NORMAL
Definition: pigpio.h:642
#define PI_OFF
Definition: pigpio.h:566
int gpioWaveAddSerial(unsigned gpio, unsigned baud, unsigned data_bits, unsigned stop_bits, unsigned offset, unsigned numBytes, char *bstr)
Definition: pigpio.c:9111
#define PI_CMD_READ
Definition: pigpio.h:6064
#define PI_I2C_SMBUS_PROC_CALL
Definition: pigpio.c:796
static uint32_t chainGetValPadr(int n)
Definition: pigpio.c:9611
static void notifyMutex(int lock)
Definition: pigpio.c:11563
uint32_t tick[TCK_PER_IPAGE]
Definition: pigpio.c:891
int MISOMode
Definition: pigpio.c:1140
#define PWM_RNG2
Definition: pigpio.c:398
static unsigned old_mode_mosi
Definition: pigpio.c:1366
#define PI_DEFAULT_MEM_ALLOC_MODE
Definition: pigpio.h:6440
#define PI_CMD_WVCHA
Definition: pigpio.h:6163
#define OFF_PER_IPAGE
Definition: pigpio.c:689
#define PI_CMD_WVHLT
Definition: pigpio.h:6094
#define PI_CMD_SERO
Definition: pigpio.h:6140
#define PI_CMD_POPA
Definition: pigpio.h:6243
#define GPLEV1
Definition: pigpio.c:336
#define PCM_DREQ_TX_PANIC(x)
Definition: pigpio.c:467
int SCLKMode
Definition: pigpio.c:1143
int timeout
Definition: pigpio.c:955
int rawWaveAddSPI(rawSPI_t *spi, unsigned offset, unsigned spiSS, char *buf, unsigned spiTxBits, unsigned spiBitFirst, unsigned spiBitLast, unsigned spiBits)
Definition: pigpio.c:9244
int gpioSetPWMfrequency(unsigned gpio, unsigned frequency)
Definition: pigpio.c:8897
#define PI_CMD_HP
Definition: pigpio.h:6152
#define PI_CMD_PUD
Definition: pigpio.h:6063
static volatile uint32_t * bscsReg
Definition: pigpio.c:1318
#define PI_BAD_FILE_MODE
Definition: pigpio.h:6394
handle
Definition: PCF8591.py:19
#define PI_I2C_READ_FAILED
Definition: pigpio.h:6348
int gpioWaveGetMaxCbs(void)
Definition: pigpio.c:10198
static void bbSPIStart(wfRx_t *w)
Definition: pigpio.c:10696
#define PI_DEFAULT_UPDATE_MASK_PI2B
Definition: pigpio.h:6437
#define PI_MAX_I2C_DEVICE_COUNT
Definition: pigpio.h:690
#define NUM_WAVE_CBS
Definition: pigpio.c:733
int serDataAvailable(unsigned handle)
Definition: pigpio.c:5033
static int my_smbus_access(int fd, char rw, uint8_t cmd, int size, union my_smbus_data *data)
Definition: pigpio.c:1598
#define WAVE_FLAG_TICK
Definition: pigpio.h:430
#define PI_RUNNING
Definition: pigpio.c:868
#define PCM_CS_TXON
Definition: pigpio.c:433
#define PI_CMD_JM
Definition: pigpio.h:6229
int gpioWaveTxAt(void)
Definition: pigpio.c:10070
#define PI_FILE_NOT_WOPEN
Definition: pigpio.h:6399
int shell(char *scriptName, char *scriptString)
Definition: pigpio.c:12760
void(* gpioGetSamplesFunc_t)(const gpioSample_t *samples, int numSamples)
Definition: pigpio.h:545
#define PI_I2C_ESC
Definition: pigpio.h:711
int gpioSetMode(unsigned gpio, unsigned mode)
Definition: pigpio.c:8607
static void dmaGpioOnCb(int b, int pos)
Definition: pigpio.c:5346
uint32_t diffTick[TICKSLOTS]
Definition: pigpio.c:1056
#define PI_I2C_SMBUS_BLOCK_DATA
Definition: pigpio.c:797
#define PCM_MODE
Definition: pigpio.c:415
#define PWM_CTL
Definition: pigpio.c:392
void * userdata
Definition: pigpio.c:957
#define PI_MAX_DUTYCYCLE_RANGE
Definition: pigpio.h:603
static uint32_t dmaCbAdr(int pos)
Definition: pigpio.c:5334
#define PI_CMD_FC
Definition: pigpio.h:6182
int gpioCustom2(unsigned arg1, char *argx, unsigned argc, char *retBuf, unsigned retMax)
#define PI_FILE_WRITE
Definition: pigpio.h:893
#define PI_SPI_SCLK
Definition: pigpio.c:569
#define PI_I2C_SMBUS_I2C_BLOCK_BROKEN
Definition: pigpio.c:798
pthread_t * pth
Definition: pigpio.c:952
#define PI_I2C_SLAVE
Definition: pigpio.c:784
uint32_t gpioRead_Bits_32_53(void)
Definition: pigpio.c:12455
static volatile uint32_t notifyBits
Definition: pigpio.c:1254
pthread_cond_t pthCond
Definition: pigpio.c:998
#define PI_NOTIFY_OPENED
Definition: pigpio.c:756
int bbI2CClose(unsigned SDA)
Definition: pigpio.c:10376
#define PI_SPI_MAX_BAUD
Definition: pigpio.h:649
#define PI_MIN_WAVE_DATABITS
Definition: pigpio.h:651
name
Definition: setup.py:5
int i2cWriteByteData(unsigned handle, unsigned reg, unsigned bVal)
Definition: pigpio.c:3470
int gpioWaveDelete(unsigned wave_id)
Definition: pigpio.c:9484
uint32_t cbCalls
Definition: pigpio.c:1058
#define MB_ALLOCATE_MEMORY_TAG
Definition: pigpio.c:834
unsigned handle
Definition: pigpio.c:1190
#define PI_NO_FILE_MATCH
Definition: pigpio.h:6401
#define PI_SCRIPT_RUNNING
Definition: pigpio.h:805
#define PI_I2C_SMBUS_BLOCK_PROC_CALL
Definition: pigpio.c:799
static int initZaps(int pmapFd, void *virtualBase, int basePage, int pages)
Definition: pigpio.c:7280
static void * pthScript(void *x)
Definition: pigpio.c:6511
static volatile uint32_t * dmaIn
Definition: pigpio.c:1328
#define PI_CMD_MOD
Definition: pigpio.h:6239
#define SYST_LEN
Definition: pigpio.c:323
#define PI_CMD_SPIO
Definition: pigpio.h:6134
int gpioSetGetSamplesFunc(gpioGetSamplesFunc_t f, uint32_t bits)
Definition: pigpio.c:11942
static void dmaReadLevelsCb(int b, int pos)
Definition: pigpio.c:5391
#define DBG_ALWAYS
Definition: pigpio.c:657
#define BSC_FIFO_SIZE
Definition: pigpio.h:499
#define DMA_DEBUG_RD_LST_NOT_SET_ERR
Definition: pigpio.c:387
#define PI_CMD_PROC
Definition: pigpio.h:6099
static void intScriptBits(void)
Definition: pigpio.c:11684
#define PI_BAD_SECO_CHANNEL
Definition: pigpio.h:6294
callbk_t func
Definition: pigpio.c:917
uint16_t servoIdx
Definition: pigpio.c:1006
static uint32_t dmaTickAdr(int pos)
Definition: pigpio.c:5312
#define PI_CMD_FR
Definition: pigpio.h:6183
#define PI_BAD_BUF_MILLIS
Definition: pigpio.h:6282
#define PI_BAD_PULSEWIDTH
Definition: pigpio.h:6269
int cmdParseScript(char *script, cmdScript_t *s, int diags)
Definition: command.c:1259
#define DBG_USER
Definition: pigpio.c:661
#define PI_SPI_CLOSED
Definition: pigpio.c:741
#define PI_I2C_STOP
Definition: pigpio.h:714
#define PI_CMD_WVTXM
Definition: pigpio.h:6175
int invert
Definition: pigpio.c:1118
#define PI_NOT_HPWM_GPIO
Definition: pigpio.h:6360
static unsigned mbUnlockMemory(int fd, unsigned handle)
Definition: pigpio.c:2755
int fileClose(unsigned handle)
Definition: pigpio.c:12948
int gpioWaveGetHighPulses(void)
Definition: pigpio.c:10154
#define PI_CMD_I2CC
Definition: pigpio.h:6117
#define BSC_SCL_SCLK
Definition: pigpio.h:772
#define PI_THREAD_STARTED
Definition: pigpio.c:872
int bbSPIXfer(unsigned CS, char *inBuf, char *outBuf, unsigned count)
Definition: pigpio.c:10967
#define PI_CMD_JZ
Definition: pigpio.h:6233
static int waveClockInited
Definition: pigpio.c:1217
static volatile uint32_t pi_dram_bus
Definition: pigpio.c:1202
#define PI_CMD_EVT
Definition: pigpio.h:6197
uint16_t state
Definition: pigpio.c:1023
#define PI_CMD_WVAG
Definition: pigpio.h:6089
int * var
Definition: command.h:101
#define PI_SOME_PERMITTED
Definition: pigpio.h:6306
#define PCM_INTEN
Definition: pigpio.c:419
static void clear_SCL(wfRx_t *w)
Definition: pigpio.c:10226
uint32_t gfLBitV
Definition: pigpio.c:935
static int chooseBestClock(clkInf_t *clkInf, unsigned f, unsigned numc, unsigned *cf)
Definition: pigpio.c:5055
#define PI_CMD_WVGOR
Definition: pigpio.h:6092
#define PI_MAX_SIGNUM
Definition: pigpio.h:812
static unsigned old_mode_ace2
Definition: pigpio.c:1373
#define PI_CMD_OR
Definition: pigpio.h:6241
static int intGpioSetAlertFunc(unsigned gpio, void *f, int user, void *userdata)
Definition: pigpio.c:11273
unsigned DMAprimaryChannel
Definition: pigpio.c:1075
unsigned clockPeriph
Definition: pigpio.c:1074
int eventMonitor(unsigned handle, uint32_t bits)
Definition: pigpio.c:11236
#define DMA_CS
Definition: pigpio.c:358
static const uint8_t PWMDef[PI_MAX_GPIO+1]
Definition: pigpio.c:1417
int instrs
Definition: command.h:103
int gpioCfgDMAchannel(unsigned DMAchannel)
Definition: pigpio.c:13366
unsigned clock
Definition: pigpio.c:1185
int serClose(unsigned handle)
Definition: pigpio.c:4889
#define PI_SPI_FLAGS_GET_CPOL(x)
Definition: pigpio.c:864
void * userdata
Definition: pigpio.c:919
static int I2CPutByte(wfRx_t *w, int byte)
Definition: pigpio.c:10301
static void set_SDA(wfRx_t *w)
Definition: pigpio.c:10215
#define PI_BAD_EDGE
Definition: pigpio.h:6387
#define PWM_CTL_MSEN2
Definition: pigpio.c:401
#define PI_BAD_FILE_SEEK
Definition: pigpio.h:6400
static volatile uint32_t pi_mem_flag
Definition: pigpio.c:1203
#define PI_CMD_EVM
Definition: pigpio.h:6196
uint32_t next
Definition: pigpio.h:485
uint32_t maxEmit
Definition: pigpio.c:1059
#define PI_PUD_UP
Definition: pigpio.h:594
#define MB_DEV_MAJOR
Definition: pigpio.c:822
#define PI_BAD_FIFO_COMMAND
Definition: pigpio.h:6293
uint16_t numBOOL
Definition: pigpio.h:463
#define BSC_FR_TXBUSY
Definition: pigpio.h:767
#define PI_CMD_I2CPC
Definition: pigpio.h:6131
#define CLK_PWMDIV
Definition: pigpio.c:516
int gpioSetPad(unsigned pad, unsigned padStrength)
Definition: pigpio.c:12717
unsigned frac
Definition: pigpio.c:1184
#define PI_I2C_SLOTS
Definition: pigpio.h:681
#define PI_I2C_ADDR
Definition: pigpio.h:716
#define PI_NOT_SPI_GPIO
Definition: pigpio.h:6407
#define PI_CFG_STATS
Definition: pigpio.h:868
#define PI_CMD_WVTX
Definition: pigpio.h:6112
static void spinWhileStarting(void)
Definition: pigpio.c:1822
#define DMAI_PAGES
Definition: pigpio.c:728
#define PI_NO_WAVEFORM_ID
Definition: pigpio.h:6335
unsigned bufferMilliseconds
Definition: pigpio.c:1072
static uint32_t waveOOLPOadr(int pos)
Definition: pigpio.c:2884
static int mbOpen(void)
Definition: pigpio.c:2690
static void scrSwap(int *v1, int *v2)
Definition: pigpio.c:6433
#define PI_CMD_RLA
Definition: pigpio.h:6248
#define PCM_TXC_CH1EN
Definition: pigpio.c:459
static void SPI_delay(wfRx_t *w)
Definition: pigpio.c:10691
int MOSIMode
Definition: pigpio.c:1141
#define PI_SPI_CE1
Definition: pigpio.c:568
int max_emits
Definition: pigpio.c:1018
#define PI_MAX_SCRIPT_VARS
Definition: pigpio.h:798
int gpioSerialReadClose(unsigned gpio)
Definition: pigpio.c:11145
int gpioCfgPermissions(uint64_t updateMask)
Definition: pigpio.c:13408
int writePos
Definition: pigpio.c:1107
#define DBG_MAX_LEVEL
Definition: pigpio.c:665
#define CHECK_INITED_RET_NULL_PTR
Definition: pigpio.c:231
uint32_t goodPipeWrite
Definition: pigpio.c:1065
int delay
Definition: pigpio.c:1125
int gpioStoreScript(char *script)
Definition: pigpio.c:12158
#define PI_NOT_HCLK_GPIO
Definition: pigpio.h:6359
#define PI_BAD_EVENT_ID
Definition: pigpio.h:6408
#define PI_SERVO_OFF
Definition: pigpio.h:607
uint8_t byte
Definition: pigpio.c:1162
#define PI_BAD_LEVEL
Definition: pigpio.h:6267
#define PI_NO_AUX_SPI
Definition: pigpio.h:6356
int gpioGetPWMrange(unsigned gpio)
Definition: pigpio.c:8846
static int pthFifoRunning
Definition: pigpio.c:1265
static int initMboxBlock(int block)
Definition: pigpio.c:7395
int gpioCfgSetInternals(uint32_t cfgVal)
Definition: pigpio.c:13504
#define BSC_FR_RXBUSY
Definition: pigpio.h:762
static void alertGlitchFilter(gpioSample_t *sample, int numSamples)
Definition: pigpio.c:5580
#define PWM_FREQS
Definition: pigpio.c:680
#define PI_MAX_DMA_CHANNEL
Definition: pigpio.h:837
uint16_t topCB
Definition: pigpio.h:456
#define PI_CMD_CMP
Definition: pigpio.h:6222
#define BSC_FR_RXFE
Definition: pigpio.h:766
#define PI_I2C_RDWR
Definition: pigpio.c:786
#define CLK_GP0_CTL
Definition: pigpio.c:505
uint32_t funcs
Definition: pigpio.c:1034
#define PI_BAD_I2C_BUS
Definition: pigpio.h:6339
#define PI_CMD_SLR
Definition: pigpio.h:6104
uint32_t flags
Definition: pigpio.c:1041
#define PI_MIN_DUTYCYCLE_RANGE
Definition: pigpio.h:602
int gpioSetGetSamplesFuncEx(gpioGetSamplesFuncEx_t f, uint32_t bits, void *userdata)
Definition: pigpio.c:11963
#define PI_MIN_WAVE_HALFSTOPBITS
Definition: pigpio.h:654
#define PI_BUF_MILLIS_MIN
Definition: pigpio.h:824
int gpioGetPWMdutycycle(unsigned gpio)
Definition: pigpio.c:8776
static volatile uint32_t wdogBits
Definition: pigpio.c:1258
static int waveOutTopOOL
Definition: pigpio.c:1247
#define PI_CMD_TRIG
Definition: pigpio.h:6098
static uint32_t waveCbPOadr(int pos)
Definition: pigpio.c:2852
uint8_t block[PI_I2C_SMBUS_BLOCK_MAX+2]
Definition: pigpio.c:1164
#define PI_MAX_WAVE_CYCLES
Definition: pigpio.h:661
static unsigned mbLockMemory(int fd, unsigned handle)
Definition: pigpio.c:2737
#define PI_I2C_SMBUS_WORD_DATA
Definition: pigpio.c:795
static eventAlert_t eventAlert[PI_MAX_EVENT+1]
Definition: pigpio.c:1270
int bbSPIOpen(unsigned CS, unsigned MISO, unsigned MOSI, unsigned SCLK, unsigned baud, unsigned spiFlags)
Definition: pigpio.c:10806
#define PI_CMD_WVTAT
Definition: pigpio.h:6176
static volatile uint32_t gFilterBits
Definition: pigpio.c:1256
#define STACK_SIZE
Definition: pigpio.c:676
#define PI_BAD_WDOG_TIMEOUT
Definition: pigpio.h:6277
#define PI_DEFAULT_UPDATE_MASK_APLUS_BPLUS
Definition: pigpio.h:6435
#define PI_TOO_MANY_CBS
Definition: pigpio.h:6332
#define PI_SCRIPT_NOT_READY
Definition: pigpio.h:6327
#define PI_CMD_RET
Definition: pigpio.h:6246
void gpioStopThread(pthread_t *pth)
Definition: pigpio.c:12134
#define PI_CMD_SPIC
Definition: pigpio.h:6135
static int initCheckPermitted(void)
Definition: pigpio.c:7180
int inited
Definition: pigpio.c:958
#define PI_BAD_SCRIPT_ID
Definition: pigpio.h:6312
#define PI_MAX_MILS_DELAY
Definition: pigpio.h:820
#define SPI_CS_MODE(x)
Definition: pigpio.c:548
#define DBG_FAST_TICK
Definition: pigpio.c:664
int i2cZip(unsigned handle, char *inBuf, unsigned inLen, char *outBuf, unsigned outLen)
Definition: pigpio.c:4090
#define PI_CMD_PIGPV
Definition: pigpio.h:6087
uint32_t highMicros
Definition: pigpio.c:1092
static void * mbMapMem(unsigned base, unsigned size)
Definition: pigpio.c:2791
#define PI_CMD_NOIB
Definition: pigpio.h:6173
#define PI_FILE_TRUNC
Definition: pigpio.h:897
#define PWM_LEN
Definition: pigpio.c:321
#define PI_CMD_CGI
Definition: pigpio.h:6167
uint32_t highCbs
Definition: pigpio.c:1098
uint32_t rawWaveGetOOL(int pos)
Definition: pigpio.c:8264
static rawWaveInfo_t waveInfo[PI_MAX_WAVES]
Definition: pigpio.c:1241
#define PI_MAX_SCRIPT_PARAMS
Definition: pigpio.h:799
#define PI_CMD_SUB
Definition: pigpio.h:6253
static DMAMem_t * dmaMboxBlk
Definition: pigpio.c:1306
int i2cWriteDevice(unsigned handle, char *buf, unsigned count)
Definition: pigpio.c:3884
static void spiTerm(uint32_t flags)
Definition: pigpio.c:4613
#define PI_CMD_BI2CZ
Definition: pigpio.h:6159
int mode
Definition: pigpio.c:1148
#define PI_ALT0
Definition: pigpio.h:583
#define CLK_CTL_SRC_OSC
Definition: pigpio.c:496
#define PI_I2C_FUNC_SMBUS_PROC_CALL
Definition: pigpio.c:812
#define PI_CHAIN_NESTING
Definition: pigpio.h:6383
static uint32_t dmaGpioOnAdr(int pos)
Definition: pigpio.c:5289
#define PI_SCRIPT_RUN
Definition: pigpio.c:845
uint32_t level
Definition: pigpio.h:411
#define PCM_FIFO
Definition: pigpio.c:414
#define PI_SPI_OPENED
Definition: pigpio.c:743
#define PI_SCRIPT_INITING
Definition: pigpio.h:803
#define BSC_MISO
Definition: pigpio.h:773
#define PI_CMD_WVSM
Definition: pigpio.h:6095
unsigned millis
Definition: pigpio.c:983
#define CYCLES_PER_BLOCK
Definition: pigpio.c:682
#define WCB_COUNTERS
Definition: pigpio.c:711
#define PI_ASPI_CE0
Definition: pigpio.c:575
#define PI_DEFAULT_UPDATE_MASK_A_B2
Definition: pigpio.h:6434
static void closeOrphanedNotifications(int slot, int fd)
Definition: pigpio.c:11542
#define PI_CMD_LDAB
Definition: pigpio.h:6237
void(* gpioAlertFunc_t)(int gpio, int level, uint32_t tick)
Definition: pigpio.h:511
#define PI_CMD_JMP
Definition: pigpio.h:6230
#define PI_WAVE_MODE_REPEAT_SYNC
Definition: pigpio.h:671
#define PI_I2C_FUNC_SMBUS_WRITE_BYTE_DATA
Definition: pigpio.c:809
unsigned gpioVersion(void)
Definition: pigpio.c:13227
static volatile gpioStats_t gpioStats
Definition: pigpio.c:1220
static int intEventSetFunc(unsigned event, void *f, int user, void *userdata)
Definition: pigpio.c:11181
#define PI_FILE_CLOSED
Definition: pigpio.c:749
int i2cWriteByte(unsigned handle, unsigned bVal)
Definition: pigpio.c:3399
static int myI2CGetPar(char *inBuf, int *inPos, int inLen, int *esc)
Definition: pigpio.c:1735
static void * pthFifoThread(void *x)
Definition: pigpio.c:6779
#define PI_CMD_I2CWD
Definition: pigpio.h:6119
int gpioWaveGetMaxMicros(void)
Definition: pigpio.c:10132
int gpioHardwareClock(unsigned gpio, unsigned frequency)
Definition: pigpio.c:12522
#define PI_BAD_PULSELEN
Definition: pigpio.h:6310
int gpioWaveAddNew(void)
Definition: pigpio.c:9063
#define PI_TOO_MANY_OOL
Definition: pigpio.h:6333
uint32_t gpioDelay(uint32_t micros)
Definition: pigpio.c:13196
static unsigned bufferCycles
Definition: pigpio.c:1354
#define GPIO_HW_CLK
Definition: pigpio.c:671
#define PI_BAD_SCRIPT_NAME
Definition: pigpio.h:6405
static uint32_t hw_pwm_real_range[2]
Definition: pigpio.c:1334
#define PI_FILE_SLOTS
Definition: pigpio.h:680
int started
Definition: pigpio.c:1128
#define PI_BAD_SPI_BAUD
Definition: pigpio.h:6406
uint32_t dmaInitCbsCount
Definition: pigpio.c:1064
#define PI_ENDING
Definition: pigpio.c:869
static uint32_t myGetTick(int pos)
Definition: pigpio.c:1760
#define PI_CMD_X
Definition: pigpio.h:6256
static pthread_t pthSocket
Definition: pigpio.c:1358
static void spiInit(uint32_t flags)
Definition: pigpio.c:4538
#define TCK_PER_IPAGE
Definition: pigpio.c:690
static int scrEvtWait(gpioScript_t *s, uint32_t bits)
Definition: pigpio.c:6442
unsigned running
Definition: pigpio.c:982
#define PI_CMD_I2CRW
Definition: pigpio.h:6125
#define PI_CMD_EVTWT
Definition: pigpio.h:6259
uint32_t gpioOff[OFF_PER_IPAGE]
Definition: pigpio.c:890
#define PI_MIN_SIGNUM
Definition: pigpio.h:811
uint32_t gfRBitV
Definition: pigpio.c:936
#define PI_CMD_I2CWK
Definition: pigpio.h:6128
#define WCB_CHAIN_OOL
Definition: pigpio.c:717
int spiWrite(unsigned handle, char *buf, unsigned count)
Definition: pigpio.c:4755
void * userdata
Definition: pigpio.c:980
#define PI_WAVE_MIN_BAUD
Definition: pigpio.h:645
void(* gpioSignalFunc_t)(int signum)
Definition: pigpio.h:540
#define DBG_STARTUP
Definition: pigpio.c:658
#define PI_CMD_BS1
Definition: pigpio.h:6075
static volatile gpioCfg_t gpioCfg
Definition: pigpio.c:1336
static const uint16_t pwmCycles[PWM_FREQS]
Definition: pigpio.c:1463
union my_smbus_data * data
Definition: pigpio.c:1172
#define DMA_END_FLAG
Definition: pigpio.c:368
static void set_SCLK(wfRx_t *w)
Definition: pigpio.c:10681
void * userdata
Definition: pigpio.c:965
#define DMA_LEN
Definition: pigpio.c:317
int gpioSetISRFunc(unsigned gpio, unsigned edge, int timeout, gpioISRFunc_t f)
Definition: pigpio.c:11498
#define PI_CMD_JNZ
Definition: pigpio.h:6231
#define PI_SPI_CE0
Definition: pigpio.c:567
static int myPermit(unsigned gpio)
Definition: pigpio.c:1772
static gpioTimer_t gpioTimer[PI_MAX_TIMER+1]
Definition: pigpio.c:1289
#define PI_FILE_READ
Definition: pigpio.h:892
#define PI_SER_WRITE_FAILED
Definition: pigpio.h:6350
#define PI_CMD_BSCX
Definition: pigpio.h:6194
#define OOL_PER_OPAGE
Definition: pigpio.c:695
static struct timespec libStarted
Definition: pigpio.c:1209
void(* eventFunc_t)(int event, uint32_t tick)
Definition: pigpio.h:520
int gpioPWM(unsigned gpio, unsigned val)
Definition: pigpio.c:8744
static unsigned old_mode_ce0
Definition: pigpio.c:1362
#define PI_INPUT
Definition: pigpio.h:581
#define DMA_LITE_MAX
Definition: pigpio.c:390
#define PI_FILE_NONE
Definition: pigpio.h:890
int16_t fd
Definition: pigpio.c:1031
#define MB_PROCESS_REQUEST
Definition: pigpio.c:832
static wfRx_t wfRx[PI_MAX_USER_GPIO+1]
Definition: pigpio.c:1243
#define PI_CMD_PRRG
Definition: pigpio.h:6085
#define PI_I2C_FUNC_SMBUS_READ_BYTE
Definition: pigpio.c:806
int gpioCfgSocketPort(unsigned port)
Definition: pigpio.c:13440
#define PI_FILE_OPENED
Definition: pigpio.c:751
int gpioHardwarePWM(unsigned gpio, unsigned frequency, unsigned dutycycle)
Definition: pigpio.c:12603
#define PI_OUTFIFO
Definition: pigpio.h:381
#define PI_DISABLE_FIFO_IF
Definition: pigpio.h:847
#define AUXSPI_CNTL0_CLR_FIFOS
Definition: pigpio.c:627
#define PI_I2C_START
Definition: pigpio.h:712
#define PI_NOTIFY_RUNNING
Definition: pigpio.c:757
int r
Definition: DHT22.py:259
int gpioWaveGetPulses(void)
Definition: pigpio.c:10143
static int initPagemapBlock(int block)
Definition: pigpio.c:7339
#define PI_BAD_CLK_PERIPH
Definition: pigpio.h:6279
static void waveOOLPageSlot(int pos, int *page, int *slot)
Definition: pigpio.c:2864
#define SER_CHECK_INITED
Definition: pigpio.c:214
static void initPCM(unsigned bits)
Definition: pigpio.c:7611
#define PI_BUF_MILLIS_MAX
Definition: pigpio.h:825
#define GPSET0
Definition: pigpio.c:329
static void initCheckLockFile(void)
Definition: pigpio.c:7100
static unsigned bufferBlocks
Definition: pigpio.c:1353
uint32_t baud
Definition: pigpio.c:1150
#define PI_NOT_SERIAL_GPIO
Definition: pigpio.h:6302
int gpioScriptStatus(unsigned script_id, uint32_t *param)
Definition: pigpio.c:12300
uint16_t state
Definition: pigpio.c:1039
#define CLK_GP2_DIV
Definition: pigpio.c:510
int gpioRead(unsigned gpio)
Definition: pigpio.c:8691
#define PI_FILE_RESERVED
Definition: pigpio.c:750
unsigned div
Definition: pigpio.c:1183
uint32_t mode
Definition: pigpio.c:1025
#define PI_WFRX_SPI_MISO
Definition: pigpio.c:765
int i2cProcessCall(unsigned handle, unsigned reg, unsigned wVal)
Definition: pigpio.c:3596
#define PI_BAD_WVSM_COMMND
Definition: pigpio.h:6308
#define PI_BAD_SER_DEVICE
Definition: pigpio.h:6344
#define PI_CMD_SERVO
Definition: pigpio.h:6069
#define PI_CMD_I2CWI
Definition: pigpio.h:6130
static void dmaDelayCb(int b)
Definition: pigpio.c:5406
uint32_t dst
Definition: pigpio.h:482
#define GPIO_SERVO
Definition: pigpio.c:670
static void chainSetVal(int n, uint32_t val)
Definition: pigpio.c:9597
static void flushMemory(void)
Definition: pigpio.c:1782
static int intGpioSetISRFunc(unsigned gpio, unsigned edge, int timeout, void *f, int user, void *userdata)
Definition: pigpio.c:11395
int i2cClose(unsigned handle)
Definition: pigpio.c:4017
#define CLK_GP1_DIV
Definition: pigpio.c:508
#define PI_BAD_I2C_SEG
Definition: pigpio.h:6371
#define PI_DEFAULT_DMA_PRIMARY_CHANNEL
Definition: pigpio.h:6427
#define CLK_PCMDIV
Definition: pigpio.c:513
void(* callbk_t)()
Definition: pigpio.c:879
#define PI_THREAD_RUNNING
Definition: pigpio.c:873
#define CLK_LEN
Definition: pigpio.c:316
#define PI_CMD_AND
Definition: pigpio.h:6218
#define CLK_DIV_DIVI(x)
Definition: pigpio.c:502
#define PI_CMD_PWM
Definition: pigpio.h:6066
#define DMA_DEBUG_FIFO_ERR
Definition: pigpio.c:386
int gpioSerialReadOpen(unsigned gpio, unsigned baud, unsigned data_bits)
Definition: pigpio.c:11021
pthread_mutex_t pthMutex
Definition: pigpio.c:997
#define PI_CMD_SERC
Definition: pigpio.h:6141
int gpioCustom1(unsigned arg1, unsigned arg2, char *argx, unsigned argc)
#define PI_WAVE_NOT_FOUND
Definition: pigpio.h:675
#define PI_MAX_SPI_DEVICE_COUNT
Definition: pigpio.h:691
#define PI_CMD_WRITE
Definition: pigpio.h:6065
#define PI_NOTIFY_CLOSING
Definition: pigpio.c:755
int gpioGetPad(unsigned pad)
Definition: pigpio.c:12741
#define PI_SPI_FLAGS_GET_CSPOLS(x)
Definition: pigpio.c:860
unsigned ex
Definition: pigpio.c:971
#define PI_I2C_END
Definition: pigpio.h:710
void(* gpioGetSamplesFuncEx_t)(const gpioSample_t *samples, int numSamples, void *userdata)
Definition: pigpio.h:548
static gpioNotify_t gpioNotify[PI_NOTIFY_SLOTS]
Definition: pigpio.c:1278
#define PI_SCRIPT_STACK_SIZE
Definition: pigpio.c:848
#define PI_I2C_RESERVED
Definition: pigpio.c:738
static i2cInfo_t i2cInfo[PI_I2C_SLOTS]
Definition: pigpio.c:1281
static void switchFunctionOff(unsigned gpio)
Definition: pigpio.c:8558
#define PI_I2C_SMBUS_WRITE
Definition: pigpio.c:790
int gpioSetAlertFuncEx(unsigned gpio, gpioAlertFuncEx_t f, void *userdata)
Definition: pigpio.c:11320
#define PI_WFRX_I2C_SCL
Definition: pigpio.c:763
#define BSCS_LEN
Definition: pigpio.c:315
static uint32_t reportedLevel
Definition: pigpio.c:1215
#define PI_I2C_SMBUS_I2C_BLOCK_DATA
Definition: pigpio.c:800
#define PI_WFRX_NONE
Definition: pigpio.c:760
ROSCPP_DECL bool ok()
#define MB_LOCK_MEMORY_TAG
Definition: pigpio.c:835
uint32_t info
Definition: pigpio.h:480
int gpioNotifyOpen(void)
Definition: pigpio.c:11635
static volatile uint32_t * dmaOut
Definition: pigpio.c:1329
static int gpioNotifyOpenInBand(int fd)
Definition: pigpio.c:11642
#define PI_GPIO_IN_USE
Definition: pigpio.h:6314
int fileWrite(unsigned handle, char *buf, unsigned count)
Definition: pigpio.c:12968
uint16_t deleted
Definition: pigpio.h:461
int fileApprove(char *filename)
Definition: pigpio.c:12785
#define TIMER_SUB(a, b, result)
Definition: pigpio.c:287
#define PI_I2C_SMBUS_BLOCK_MAX
Definition: pigpio.c:802
#define PI_CMD_CSI
Definition: pigpio.h:6168
static volatile int runState
Definition: pigpio.c:1262
int gpio
Definition: pigpio.c:1149
#define CLK_PCMCTL
Definition: pigpio.c:512
rawCbs_t cb[CBS_PER_IPAGE]
Definition: pigpio.c:888
#define PI_MAX_WDOG_TIMEOUT
Definition: pigpio.h:783
#define PI_SER_OPEN_FAILED
Definition: pigpio.h:6337
static int initGrabLockFile(void)
Definition: pigpio.c:7133
static void clear_CS(wfRx_t *w)
Definition: pigpio.c:10676
#define DBG_INTERNAL
Definition: pigpio.c:662
#define PCM_CS_EN
Definition: pigpio.c:435
uint32_t nfTick2
Definition: pigpio.c:929
#define PI_MAX_WAVE_DELAY
Definition: pigpio.h:662
#define PI_DISABLE_SOCK_IF
Definition: pigpio.h:848
void *( gpioThreadFunc_t)(void *)
Definition: pigpio.h:552
static uint32_t old_spi_cntl1
Definition: pigpio.c:1379
#define PI_CMD_PROCU
Definition: pigpio.h:6199
#define PI_CMD_WDOG
Definition: pigpio.h:6070
int16_t fd
Definition: pigpio.c:1024
char txBuf[BSC_FIFO_SIZE]
Definition: pigpio.h:507
#define AUX_SPI0_STAT_REG
Definition: pigpio.c:601
int gpioDeleteScript(unsigned script_id)
Definition: pigpio.c:12354
wfRxI2C_t I
Definition: pigpio.c:1155
#define PI_CMD_FN
Definition: pigpio.h:6171
#define PI_CMD_BSPIC
Definition: pigpio.h:6190
int gpioSetISRFuncEx(unsigned gpio, unsigned edge, int timeout, gpioAlertFuncEx_t f, void *userdata)
Definition: pigpio.c:11521
#define PI_ASPI_CE2
Definition: pigpio.c:577
rawCbs_t cb[CBS_PER_OPAGE]
Definition: pigpio.c:899
#define PI_CMD_SERR
Definition: pigpio.h:6144
#define PI_NTFY_FLAGS_WDOG
Definition: pigpio.h:626
#define PI_SER_READ_NO_DATA
Definition: pigpio.h:6352
int gpioWaveGetMaxPulses(void)
Definition: pigpio.c:10165
#define PI_ALT4
Definition: pigpio.h:587
uint32_t gpioOn
Definition: pigpio.h:434
unsigned id
Definition: pigpio.c:981
#define PWM_CTL_USEF1
Definition: pigpio.c:405
#define PI_FILE_CREATE
Definition: pigpio.h:896
uint16_t state
Definition: pigpio.c:1046
int status
Definition: pigs.c:57
#define PI_SPI_FLAGS_GET_MODE(x)
Definition: pigpio.c:861
#define PI_I2C_FUNC_SMBUS_READ_BLOCK_DATA
Definition: pigpio.c:813
int gpioNoiseFilter(unsigned gpio, unsigned steady, unsigned active)
Definition: pigpio.c:11874
#define PWM_CTL_PWEN1
Definition: pigpio.c:407
#define PI_BB_SPI_MIN_BAUD
Definition: pigpio.h:636
int i2cReadBlockData(unsigned handle, unsigned reg, char *buf)
Definition: pigpio.c:3638
#define PI_CMD_MLT
Definition: pigpio.h:6238
#define SPI_CS_CS(x)
Definition: pigpio.c:549
static pthread_t pthFifo
Definition: pigpio.c:1357
#define PI_SCRIPT_FREE
Definition: pigpio.c:839
#define BSC_SDA_MOSI
Definition: pigpio.h:771
static int waveOutCount
Definition: pigpio.c:1248
#define PWM_CTL_PWEN2
Definition: pigpio.c:402
uint32_t maxSamples
Definition: pigpio.c:1061
static void spiACS(int channel, int on)
Definition: pigpio.c:4279
#define DBG_DMACBS
Definition: pigpio.c:659
static int initAllocDMAMem(void)
Definition: pigpio.c:7427
#define PI_BAD_DUTYRANGE
Definition: pigpio.h:6283
int gpioCfgClock(unsigned micros, unsigned peripheral, unsigned source)
Definition: pigpio.c:13342
#define PI_CMD_I2CRD
Definition: pigpio.h:6118
static rawWave_t wf[3][PI_WAVE_MAX_PULSES]
Definition: pigpio.c:1228
#define BANK
Definition: pigpio.c:195
uint16_t state
Definition: pigpio.c:1012
#define PI_CMD_NP
Definition: pigpio.h:6081
#define PI_MAX_USER_GPIO
Definition: pigpio.h:562
#define PI_CMD_I2CO
Definition: pigpio.h:6116
uint32_t micros
Definition: pigpio.c:1091
#define PI_CMD_I2CRI
Definition: pigpio.h:6129
uint32_t gfTick
Definition: pigpio.c:934
#define DMA_PRIORITY(x)
Definition: pigpio.c:366
static int chainGetCB(int n)
Definition: pigpio.c:9584
static unsigned old_mode_ce1
Definition: pigpio.c:1363
uint16_t addr
Definition: pigpio.h:491
#define PI_MAX_SOCKET_PORT
Definition: pigpio.h:842
#define PI_TOO_MANY_CHARS
Definition: pigpio.h:6301
#define PI_CMD_MICS
Definition: pigpio.h:6107
#define PI_CMD_TICK
Definition: pigpio.h:6077
static void myGpioSetMode(unsigned gpio, unsigned mode)
Definition: pigpio.c:1616
#define PI_CMD_PROCD
Definition: pigpio.h:6100
static volatile uint32_t * spiReg
Definition: pigpio.c:1325
#define PI_DEFAULT_UPDATE_MASK_UNKNOWN
Definition: pigpio.h:6432
static int numSockNetAddr
Definition: pigpio.c:1213
#define PI_SPI_FLAGS_CHANNEL(x)
Definition: pigpio.c:850
pthread_mutex_t mutex
Definition: pigpio.c:1151
#define PI_SPI_MISO
Definition: pigpio.c:570
int gpioWrite_Bits_0_31_Clear(uint32_t bits)
Definition: pigpio.c:12467
static int myDoCommand(uint32_t *p, unsigned bufSize, char *buf)
Definition: pigpio.c:1833
#define DEFAULT_PWM_IDX
Definition: pigpio.c:776
void gpioTerminate(void)
Definition: pigpio.c:8495
static unsigned old_mode_ace0
Definition: pigpio.c:1371
int CSMode
Definition: pigpio.c:1142
#define PI_TIMER_FAILED
Definition: pigpio.h:6276
#define PI_ASPI_MISO
Definition: pigpio.c:578
#define PI_FILE_NOT_ROPEN
Definition: pigpio.h:6398
static volatile uint32_t * systReg
Definition: pigpio.c:1326
uint32_t gpioTick(void)
Definition: pigpio.c:13217
int gpioGetPWMfrequency(unsigned gpio)
Definition: pigpio.c:8949
static int wave2Cbs(unsigned wave_mode, int *CB, int *BOOL, int *TOOL)
Definition: pigpio.c:2966
#define PI_SPI_FLAGS_GET_3WREN(x)
Definition: pigpio.c:856
#define PI_OUTPUT
Definition: pigpio.h:582
static void initPWM(unsigned bits)
Definition: pigpio.c:7568
int serWrite(unsigned handle, char *buf, unsigned count)
Definition: pigpio.c:4964
#define PI_I2C_SMBUS_I2C_BLOCK_MAX
Definition: pigpio.c:803
#define SPI_CS_REN
Definition: pigpio.c:540
int dataBits
Definition: pigpio.c:1117
static uint32_t sockNetAddr[MAX_CONNECT_ADDRESSES]
Definition: pigpio.c:1211
#define PI_CMD_FS
Definition: pigpio.h:6185
pthread_t pthId
Definition: pigpio.c:984
cmdInstr_t * instr
Definition: command.h:102
static void myCreatePipe(char *name, int perm)
Definition: pigpio.c:1682
#define PI_BAD_SER_SPEED
Definition: pigpio.h:6345
static uint64_t gpioMask
Definition: pigpio.c:1226
#define BSC_FR
Definition: pigpio.h:739
static uint32_t dmaGpioOffAdr(int pos)
Definition: pigpio.c:5301
static unsigned old_mode_ace1
Definition: pigpio.c:1372
uint32_t maxCbs
Definition: pigpio.c:1099
#define AUXSPI_CNTL1_MSB_FIRST(x)
Definition: pigpio.c:636
#define PI_NOT_PERMITTED
Definition: pigpio.h:6305
uintptr_t * virtual_addr
Definition: pigpio.c:1192
int cmdParse(char *buf, uint32_t *p, unsigned ext_len, char *ext, cmdCtlParse_t *ctl)
Definition: command.c:629
#define PI_SCRIPT_RESERVED
Definition: pigpio.c:840
#define PI_BAD_DUTYCYCLE
Definition: pigpio.h:6270
#define DMA_PANIC_PRIORITY(x)
Definition: pigpio.c:365
#define CLK_GP2_CTL
Definition: pigpio.c:509
static uint32_t chainGetCntVal(int counter, int slot)
Definition: pigpio.c:9626
void(* gpioTimerFunc_t)(void)
Definition: pigpio.h:536
#define SPI_LEN
Definition: pigpio.c:322
#define CLK_PWMCTL
Definition: pigpio.c:515
#define PI_SPI_FLAGS_GET_3WIRE(x)
Definition: pigpio.c:857
start
Definition: hall.py:25
uint32_t nfTick1
Definition: pigpio.c:928
#define PI_CMD_I2CRS
Definition: pigpio.h:6121
#define GPPUDCLK0
Definition: pigpio.c:355
void rawDumpScript(unsigned script_id)
Definition: pigpio.c:8418
#define PCM_TXC
Definition: pigpio.c:417
#define PI_MIN_DMA_CHANNEL
Definition: pigpio.h:836
pi_i2c_msg_t * msgs
Definition: pigpio.c:1177
static void I2C_clock_stretch(wfRx_t *w)
Definition: pigpio.c:10237
int gpioWrite(unsigned gpio, unsigned level)
Definition: pigpio.c:8707
static unsigned old_mode_amosi
Definition: pigpio.c:1376
#define PI_CMD_HALT
Definition: pigpio.h:6226
#define TIMED_DMA(x)
Definition: pigpio.c:651
unsigned gpioHardwareRevision(void)
Definition: pigpio.c:13259
int gpioGlitchFilter(unsigned gpio, unsigned steady)
Definition: pigpio.c:11904
unsigned state
Definition: pigpio.c:990
#define PI_BAD_SECONDS
Definition: pigpio.h:6274
void(* gpioAlertFuncEx_t)(int gpio, int level, uint32_t tick, void *userdata)
Definition: pigpio.h:515
#define PIGPIO_VERSION
Definition: pigpio.h:34
#define PI_CMD_SERRB
Definition: pigpio.h:6142
#define PI_MAX_WAVE_DATABITS
Definition: pigpio.h:652
int MOSI
Definition: pigpio.c:1135
#define PI_NTFY_FLAGS_BIT(x)
Definition: pigpio.h:627
#define PWM_BASE
Definition: pigpio.c:310
#define PI_I2C_FUNCS
Definition: pigpio.c:785
uint32_t moreToDo
Definition: pigpio.c:1055
#define PI_SCRIPT_WAITING
Definition: pigpio.h:806
#define GPIO_UNDEFINED
Definition: pigpio.c:667
#define CHECK_INITED_RET_NIL
Definition: pigpio.c:243
int fileList(char *fpat, char *buf, unsigned count)
Definition: pigpio.c:13079
#define PWM_DMAC_DREQ(x)
Definition: pigpio.c:411
static volatile uint32_t * dmaReg
Definition: pigpio.c:1320
#define PCM_CS_STBY
Definition: pigpio.c:423
#define PCM_BASE
Definition: pigpio.c:309
#define PI_I2C_CLOSED
Definition: pigpio.c:737
#define PI_SPI_RESERVED
Definition: pigpio.c:742
#define PI_CMD_SCRIPT
Definition: pigpio.h:6215
static int PWMClockInited
Definition: pigpio.c:1218
static unsigned old_mode_amiso
Definition: pigpio.c:1375
uint32_t gpioOn
Definition: pigpio.h:424
static int initPeripherals(void)
Definition: pigpio.c:7199
#define PI_NO_FILE_ACCESS
Definition: pigpio.h:6402
#define PI_I2C_FUNC_SMBUS_READ_I2C_BLOCK
Definition: pigpio.c:815
static volatile uint32_t monitorBits
Definition: pigpio.c:1253
int gpioTrigger(unsigned gpio, unsigned pulseLen, unsigned level)
Definition: pigpio.c:11820
#define PI_SCRIPT_FAILED
Definition: pigpio.h:807
static void myClearGpioOn(unsigned gpio, int pos)
Definition: pigpio.c:2507
#define SOFT_ERROR(x, format, arg...)
Definition: pigpio.c:266
uint32_t addr
Definition: pigpio.c:1032
#define PI_BAD_MILS_DELAY
Definition: pigpio.h:6330
static uint32_t myGetLevel(int pos)
Definition: pigpio.c:1721
static spiInfo_t spiInfo[PI_SPI_SLOTS]
Definition: pigpio.c:1283
#define PI_CMD_STAB
Definition: pigpio.h:6252
#define SPI_FIFO
Definition: pigpio.c:525
#define NUM_WAVE_OOL
Definition: pigpio.c:732
static unsigned memAllocMode
Definition: pigpiod.c:62
int gpioCfgMemAlloc(unsigned memAllocMode)
Definition: pigpio.c:13457
static volatile uint32_t scriptBits
Definition: pigpio.c:1255
int bbSPIClose(unsigned CS)
Definition: pigpio.c:10921
#define PI_CMD_MILS
Definition: pigpio.h:6108
uint32_t eventBits
Definition: pigpio.c:1014
uint32_t lateTicks
Definition: pigpio.c:1054
#define PI_I2C_SMBUS_READ
Definition: pigpio.c:789
#define AUXSPI_CNTL0_INVERT_CLK(x)
Definition: pigpio.c:629
#define CLK_CTL_ENAB
Definition: pigpio.c:491
int spiOpen(unsigned spiChan, unsigned baud, unsigned spiFlags)
Definition: pigpio.c:4654
#define PI_MASH_MAX_FREQ
Definition: pigpio.c:818
#define PI_MAX_SERVO_PULSEWIDTH
Definition: pigpio.h:609
static int intGpioSetTimerFunc(unsigned id, unsigned millis, void *f, int user, void *userdata)
Definition: pigpio.c:11986
#define PAD_PER_IPAGE
Definition: pigpio.c:692
#define PI_BAD_FILTER
Definition: pigpio.h:6390
wfRxSPI_t S
Definition: pigpio.c:1156
#define CLK_OSC_FREQ
Definition: pigpio.c:499
#define AUX_LEN
Definition: pigpio.c:314
static void initHWClk(int clkCtl, int clkDiv, int clkSrc, int divI, int divF, int MASH)
Definition: pigpio.c:7664
#define PI_CMD_I2CPK
Definition: pigpio.h:6132
int bscXfer(bsc_xfer_t *xfer)
Definition: pigpio.c:10588
int gpioCfgInterfaces(unsigned ifFlags)
Definition: pigpio.c:13424
#define EITHER_EDGE
Definition: pigpio.h:878
static void dmaGpioOffCb(int b, int pos)
Definition: pigpio.c:5376
#define PI_MAX_WAVES
Definition: pigpio.h:659
#define PI_DEFAULT_DMA_SECONDARY_CHANNEL
Definition: pigpio.h:6428
#define AUXSPI_CNTL0_SHIFT_LEN(x)
Definition: pigpio.c:631
#define BPD
Definition: pigpio.c:771
uint32_t wdLBitV
Definition: pigpio.c:923
static void myGpioSetPwm(unsigned gpio, int oldVal, int newVal)
Definition: pigpio.c:2519
#define PI_MAX_GPIO
Definition: pigpio.h:558
static uint32_t dmaReadLevelsAdr(int pos)
Definition: pigpio.c:5323
#define PI_BAD_HPWM_DUTY
Definition: pigpio.h:6362
static dmaOPage_t ** dmaOBus
Definition: pigpio.c:1315
static volatile uint32_t * pcmReg
Definition: pigpio.c:1323
#define PI_CMD_SERW
Definition: pigpio.h:6145
#define CLK_CTL_SRC_PLLD
Definition: pigpio.c:497
uint16_t seqno
Definition: pigpio.c:1011
#define PI_CMD_DCR
Definition: pigpio.h:6223
#define PWM_STA
Definition: pigpio.c:393
#define PI_NUM_AUX_SPI_CHANNEL
Definition: pigpio.h:687
int gpioSetPullUpDown(unsigned gpio, unsigned pud)
Definition: pigpio.c:8661
#define AUX_ENABLES
Definition: pigpio.c:585
uint32_t highPulses
Definition: pigpio.c:1095
uint16_t word
Definition: pigpio.c:1163
#define GPIO_WRITE
Definition: pigpio.c:668
static uint32_t spi_dummy
Definition: pigpio.c:1360
#define DMA_CONBLK_AD
Definition: pigpio.c:359
#define GPSET1
Definition: pigpio.c:330
unsigned edge
Definition: pigpio.c:954
callbk_t func
Definition: pigpio.c:970
#define BIT
Definition: pigpio.c:197
uint32_t nfLBitV
Definition: pigpio.c:930
#define PI_FILE_MIN
Definition: pigpio.h:891
int gpioWrite_Bits_32_53_Clear(uint32_t bits)
Definition: pigpio.c:12481
int gpioWaveTxSend(unsigned wave_id, unsigned wave_mode)
Definition: pigpio.c:9524
#define SPI_CLK
Definition: pigpio.c:526
#define GPPUD
Definition: pigpio.c:354
#define PCM_LEN
Definition: pigpio.c:320
#define PI_CMD_WVGO
Definition: pigpio.h:6091
#define PI_BAD_HCLK_PASS
Definition: pigpio.h:6364
#define PI_CMD_HC
Definition: pigpio.h:6151
#define GPCLR1
Definition: pigpio.c:333
#define PI_CFG_RT_PRIORITY
Definition: pigpio.h:867
int i2cReadDevice(unsigned handle, char *buf, unsigned count)
Definition: pigpio.c:3913
static int pwmFreq[PWM_FREQS]
Definition: pigpio.c:1291
uint16_t deferOff
Definition: pigpio.c:911
#define PI_I2C_SMBUS_BYTE_DATA
Definition: pigpio.c:794
char * cmdUsage
Definition: command.c:264
callbk_t func
Definition: pigpio.c:978
callbk_t func
Definition: pigpio.c:942
int spiClose(unsigned handle)
Definition: pigpio.c:4714
int i2cOpen(unsigned i2cBus, unsigned i2cAddr, unsigned i2cFlags)
Definition: pigpio.c:3942
#define PI_TOO_MANY_PULSES
Definition: pigpio.h:6300
static void I2C_delay(wfRx_t *w)
Definition: pigpio.c:10232
#define MB_RELEASE_MEMORY_TAG
Definition: pigpio.c:837
#define WAVE_FLAG_READ
Definition: pigpio.h:429
void * userdata
Definition: pigpio.c:944
int gpioWrite_Bits_0_31_Set(uint32_t bits)
Definition: pigpio.c:12495
static gpioInfo_t gpioInfo[PI_MAX_GPIO+1]
Definition: pigpio.c:1276
#define PI_WAVE_MAX_CHARS
Definition: pigpio.h:631
static volatile uint32_t alertBits
Definition: pigpio.c:1252
#define PI_CMD_PADG
Definition: pigpio.h:6179
#define PI_FILE_APPEND
Definition: pigpio.h:895
static int pthSocketRunning
Definition: pigpio.c:1266
#define PI_CMD_HELP
Definition: pigpio.h:6086
void i2cSwitchCombined(int setting)
Definition: pigpio.c:4037
#define AUXSPI_STAT_BUSY
Definition: pigpio.c:644
unsigned request
Definition: pigpio.c:991
static void waveRxSerial(wfRx_t *w, int level, uint32_t tick)
Definition: pigpio.c:3118
#define PI_SER_OPENED
Definition: pigpio.c:747
#define CLK_CTL_MASH(x)
Definition: pigpio.c:488
#define PI_CMD_XOR
Definition: pigpio.h:6258
#define CLK_GP1_CTL
Definition: pigpio.c:507
int spiXfer(unsigned handle, char *txBuf, char *rxBuf, unsigned count)
Definition: pigpio.c:4776
static void spiGoS(unsigned speed, uint32_t flags, char *txBuf, char *rxBuf, unsigned count)
Definition: pigpio.c:4395
int i2cWriteBlockData(unsigned handle, unsigned reg, char *buf, unsigned count)
Definition: pigpio.c:3684
void rawWaveSetOOL(int pos, uint32_t value)
Definition: pigpio.c:8279
#define PI_I2C_COMBINED_ON
Definition: pigpio.h:713
#define PI_SPI_SLOTS
Definition: pigpio.h:682
#define PI_HW_PWM_MIN_FREQ
Definition: pigpio.h:613
#define DMA_DEST_INC
Definition: pigpio.c:382
#define PCM_CS
Definition: pigpio.c:413
#define PI_CMD_SHELL
Definition: pigpio.h:6188
#define CLK_CTL_KILL
Definition: pigpio.c:490
void time_sleep(double seconds)
Definition: pigpio.c:8374
#define PI_MAX_STEADY
Definition: pigpio.h:860
#define PI_CMD_I2CZ
Definition: pigpio.h:6161
int myScriptNameValid(char *name)
Definition: pigpio.c:1493
#define PI_CFG_NOSIGHANDLER
Definition: pigpio.h:869
#define PI_BAD_SER_OFFSET
Definition: pigpio.h:6313
uint32_t usDelay
Definition: pigpio.h:436
#define PI_CMD_STA
Definition: pigpio.h:6251
#define PI_I2C_FUNC_SMBUS_WRITE_BLOCK_DATA
Definition: pigpio.c:814
#define BSC_IMSC
Definition: pigpio.h:741
#define PI_CMD_WVDEL
Definition: pigpio.h:6111
#define MB_UNLOCK_MEMORY_TAG
Definition: pigpio.c:836
uint32_t periphData
Definition: pigpio.c:901
int gpioNotifyPause(unsigned handle)
Definition: pigpio.c:11771
#define AUX_SPI0_CNTL1_REG
Definition: pigpio.c:600
#define PI_SCRIPT_DYING
Definition: pigpio.c:842
uint32_t nextBitDiff
Definition: pigpio.c:1112
#define BSCS_BASE
Definition: pigpio.c:303
#define PI_CMD_WVCLR
Definition: pigpio.h:6088
static void initDMAgo(volatile uint32_t *dmaAddr, uint32_t cbAddr)
Definition: pigpio.c:7738
int ignore
Definition: pigpio.c:945
int gpioTime(unsigned timetype, int *seconds, int *micros)
Definition: pigpio.c:13121
#define PI_BB_SER_MIN_BAUD
Definition: pigpio.h:639
#define GPCLR0
Definition: pigpio.c:332
uint32_t bits
Definition: pigpio.c:1013
static int scrPop(gpioScript_t *s, int *SP, int *S)
Definition: pigpio.c:6402
#define PI_I2C_FUNC_SMBUS_WRITE_BYTE
Definition: pigpio.c:807
#define PI_CMD_SLRC
Definition: pigpio.h:6105
#define PI_CMD_FW
Definition: pigpio.h:6184
#define CBS_PER_CYCLE
Definition: pigpio.c:719
int gpioWaveTxBusy(void)
Definition: pigpio.c:10056
unsigned rawWaveCB(void)
Definition: pigpio.c:5232
static const clkCfg_t clkCfg[]
Definition: pigpio.c:1447
uint32_t emitFrags
Definition: pigpio.c:1060
#define PI_MAX_MICS_DELAY
Definition: pigpio.h:819
#define PI_WFRX_SERIAL
Definition: pigpio.c:761
static void alertEmit(gpioSample_t *sample, int numSamples, uint32_t changedBits, uint32_t eTick)
Definition: pigpio.c:5704
uint32_t nfRBitV
Definition: pigpio.c:931
static void spiGoA(unsigned speed, uint32_t flags, char *txBuf, char *rxBuf, unsigned count)
Definition: pigpio.c:4292
unsigned ex
Definition: pigpio.c:979
#define SPI_BASE
Definition: pigpio.c:311
#define PI_BAD_SER_INVERT
Definition: pigpio.h:6386
#define PI_CMD_INRA
Definition: pigpio.h:6228
uint32_t rawWaveGetOut(int pos)
Definition: pigpio.c:8293
#define PI_CMD_RR
Definition: pigpio.h:6249
#define PI_MIN_PAD_STRENGTH
Definition: pigpio.h:885
static int mbUnmapMem(void *addr, unsigned size)
Definition: pigpio.c:2800
unsigned socketPort
Definition: pigpio.c:1077
#define SPI_CS_TXD
Definition: pigpio.c:536
#define CBS_PER_OPAGE
Definition: pigpio.c:694
#define PI_BAD_I2C_ADDR
Definition: pigpio.h:6340
#define PI_BAD_HCLK_FREQ
Definition: pigpio.h:6363
#define PI_CMD_SERWB
Definition: pigpio.h:6143
#define PI_BAD_PATHNAME
Definition: pigpio.h:6286
static pthread_t pthAlert
Definition: pigpio.c:1356
unsigned id
Definition: pigpio.c:989
#define PCM_RXC
Definition: pigpio.c:416
static void I2CStop(wfRx_t *w)
Definition: pigpio.c:10264
#define PCM_CS_TXCLR
Definition: pigpio.c:432
#define PI_BAD_GPIO
Definition: pigpio.h:6265
static uint32_t * initMapMem(int fd, uint32_t addr, uint32_t len)
Definition: pigpio.c:7170
#define PI_CMD_BI2CO
Definition: pigpio.h:6158
#define PI_CMD_BSPIO
Definition: pigpio.h:6191
#define PI_BAD_FILE_READ
Definition: pigpio.h:6396
static uint32_t bscFR
Definition: pigpio.c:1381
static void chainMakeCounter(unsigned counter, unsigned blklen, unsigned blocks, unsigned count, uint32_t repeat, uint32_t next)
Definition: pigpio.c:9664
#define PI_MAX_I2C_ADDR
Definition: pigpio.h:685
uint8_t read_write
Definition: pigpio.c:1169
#define PI_ALT3
Definition: pigpio.h:586
#define PI_MAX_PAD_STRENGTH
Definition: pigpio.h:886
int gpioNotifyClose(unsigned handle)
Definition: pigpio.c:11795
#define AUX_SPI0_TX_HOLD
Definition: pigpio.c:605
static gpioGetSamples_t gpioGetSamples
Definition: pigpio.c:1274
#define PI_NO_TX_WAVE
Definition: pigpio.h:676
static void mySetGpioOff(unsigned gpio, int pos)
Definition: pigpio.c:2473
#define BUS_TO_PHYS(x)
Definition: pigpio.c:829
int delay
Definition: bench_1.py:22
#define ON_PER_IPAGE
Definition: pigpio.c:691
static void scrPush(gpioScript_t *s, int *SP, int *S, int val)
Definition: pigpio.c:6418
static void I2CPutBit(wfRx_t *w, int bit)
Definition: pigpio.c:10276
#define SPI_CS
Definition: pigpio.c:524
uint32_t gpioOff
Definition: pigpio.h:425
static int I2CGetBit(wfRx_t *w)
Definition: pigpio.c:10287
#define PI_CHAIN_COUNTER
Definition: pigpio.h:6380
int wdSteadyUs
Definition: pigpio.c:921
static gpioAlert_t gpioAlert[PI_MAX_USER_GPIO+1]
Definition: pigpio.c:1268
uint32_t flags
Definition: pigpio.h:437
static int mbProperty(int fd, void *buf)
Definition: pigpio.c:2711
#define MB_IOCTL
Definition: pigpio.c:824
static rawCbs_t * dmaCB2adr(int pos)
Definition: pigpio.c:5117
#define DBG(level, format, arg...)
Definition: pigpio.c:200
#define PI_THREAD_NONE
Definition: pigpio.c:871
uint32_t wdTick
Definition: pigpio.c:922
#define PI_ASPI_MOSI
Definition: pigpio.c:579
#define PI_DEFAULT_IF_FLAGS
Definition: pigpio.h:6424
#define WCB_COUNTER_CBS
Definition: pigpio.c:715
uint32_t gpioRead_Bits_0_31(void)
Definition: pigpio.c:12443
#define PI_CMD_PADS
Definition: pigpio.h:6178
#define CLK_SRCS
Definition: pigpio.c:494
#define AUXENB_SPI1
Definition: pigpio.c:616
#define PI_SCRIPT_HALT
Definition: pigpio.c:844
#define GPIO_HW_PWM
Definition: pigpio.c:672
static const uint8_t clkDef[PI_MAX_GPIO+1]
Definition: pigpio.c:1385
#define GPIO_LEN
Definition: pigpio.c:318
#define PI_I2C_OPEN_FAILED
Definition: pigpio.h:6336
#define PI_BAD_CHAIN_LOOP
Definition: pigpio.h:6379
#define PI_I2C_OPENED
Definition: pigpio.c:739
#define PI_BAD_HANDLE
Definition: pigpio.h:6288
static volatile uint32_t pi_peri_phys
Definition: pigpio.c:1201
#define PI_NO_SCRIPT_ROOM
Definition: pigpio.h:6321
#define PI_NOTIFY_PAUSED
Definition: pigpio.c:758
int gpioWrite_Bits_32_53_Set(uint32_t bits)
Definition: pigpio.c:12509
#define PI_NTFY_FLAGS_ALIVE
Definition: pigpio.h:625
#define PI_BAD_ISR_INIT
Definition: pigpio.h:6388
int usage
Definition: pigpio.c:1137
int SDA
Definition: pigpio.c:1123
#define PI_CMD_MODEG
Definition: pigpio.h:6062
static int wfc[3]
Definition: pigpio.c:1230
#define PI_BAD_I2C_BAUD
Definition: pigpio.h:6377
#define PI_NOT_SERVO_GPIO
Definition: pigpio.h:6358
int gpioGetPWMrealRange(unsigned gpio)
Definition: pigpio.c:8869
#define PI_CLOCK_PWM
Definition: pigpio.h:831
uint8_t is
Definition: pigpio.c:906
#define PI_CMD_WVAS
Definition: pigpio.h:6090
#define PI_CMD_I2CWW
Definition: pigpio.h:6126
uint16_t botCB
Definition: pigpio.h:455
uint32_t p[5]
Definition: command.h:89
int nfActive
Definition: pigpio.c:927
uint32_t waitBits
Definition: pigpio.c:993
#define BSC_SLV
Definition: pigpio.h:737
#define PI_CMD_CF2
Definition: pigpio.h:6155
#define PI_CMD_I2CRB
Definition: pigpio.h:6123
static int chainGetCntCB(int counter)
Definition: pigpio.c:9656
static void alertNoiseFilter(gpioSample_t *sample, int numSamples)
Definition: pigpio.c:5638
#define PI_TOO_MANY_PARAM
Definition: pigpio.h:6325
#define AUX_SPI0_IO_REG
Definition: pigpio.c:604
#define PI_WAVE_COUNT_PAGES
Definition: pigpio.h:664
int gpioRunScript(unsigned script_id, unsigned numParam, uint32_t *param)
Definition: pigpio.c:12219
static int libInitialised
Definition: pigpio.c:1205
int i2cWriteQuick(unsigned handle, unsigned bit)
Definition: pigpio.c:3336
#define PI_BAD_PRIM_CHANNEL
Definition: pigpio.h:6291
#define PI_CMD_RRA
Definition: pigpio.h:6250
uint32_t flags
Definition: pigpio.c:1033
#define PWM_DAT1
Definition: pigpio.c:396
static void initReleaseResources(void)
Definition: pigpio.c:7880
#define DMA_LITE_FIRST
Definition: pigpio.c:389
#define BSC_DR
Definition: pigpio.h:735
unsigned size
Definition: pigpio.c:1193
#define TICKSLOTS
Definition: pigpio.c:735
#define PI_BAD_POINTER
Definition: pigpio.h:6355
#define PI_MAX_WAVE_HALFSTOPBITS
Definition: pigpio.h:655
#define PI_MEM_ALLOC_AUTO
Definition: pigpio.h:854
int gpioCfgNetAddr(int numSockAddr, uint32_t *sockAddr)
Definition: pigpio.c:13474
#define DMA15_BASE
Definition: pigpio.c:306
#define PI_DEFAULT_CLK_MICROS
Definition: pigpio.h:6422
#define PI_BAD_I2C_RLEN
Definition: pigpio.h:6375
#define PI_BB_SER_INVERT
Definition: pigpio.h:643
static int waveOutBotCB
Definition: pigpio.c:1245
#define PI_UNKNOWN_COMMAND
Definition: pigpio.h:6353
static gpioSignal_t gpioSignal[PI_MAX_SIGNUM+1]
Definition: pigpio.c:1287
#define PI_CMD_PROCP
Definition: pigpio.h:6106
#define PI_HPWM_ILLEGAL
Definition: pigpio.h:6365
#define PI_DISABLE_ALERT
Definition: pigpio.h:850
int serReadByte(unsigned handle)
Definition: pigpio.c:4934
static volatile uint32_t * gpioReg
Definition: pigpio.c:1321
int delay
Definition: pigpio.c:1138
#define AUXSPI_CNTL0_OUT_RISING(x)
Definition: pigpio.c:628
char * buf
Definition: pigpio.c:1104
#define PI_MAX_TIMER
Definition: pigpio.h:788
#define PCM_DREQ
Definition: pigpio.c:418
static void mySetGpioOn(unsigned gpio, int pos)
Definition: pigpio.c:2495
uint32_t wouldBlockPipeWrite
Definition: pigpio.c:1067
#define CMD_PAR
Definition: command.h:52
unsigned clockMicros
Definition: pigpio.c:1073
#define PI_BAD_SERIAL_COUNT
Definition: pigpio.h:6315
static int gpioMaskSet
Definition: pigpio.c:1222
unsigned ex
Definition: pigpio.c:964
callbk_t func
Definition: pigpio.c:953
static uint8_t I2CGetByte(wfRx_t *w, int nack)
Definition: pigpio.c:10316
#define PCM_TIMER
Definition: pigpio.c:653
uint16_t len
Definition: pigpio.h:493
static uint32_t old_spi_cs
Definition: pigpio.c:1368
#define CBS_PER_IPAGE
Definition: pigpio.c:687
void putBitInBytes(int bitPos, char *buf, int bit)
Definition: pigpio.c:8251
int rawWaveAddGeneric(unsigned numIn1, rawWave_t *in1)
Definition: pigpio.c:3204
#define PI_CMD_WVCRE
Definition: pigpio.h:6110
#define PI_CMD_DIV
Definition: pigpio.h:6225
#define PI_NUM_STD_SPI_CHANNEL
Definition: pigpio.h:688
#define PI_MIN_MS
Definition: pigpio.h:792
#define PI_CMD_NO
Definition: pigpio.h:6079
uint32_t bufSize
Definition: pigpio.c:1105
static uint32_t myGpioDelay(uint32_t micros)
Definition: pigpio.c:1662
int gpioSleep(unsigned timetype, int seconds, int micros)
Definition: pigpio.c:13155
#define PI_ENVPORT
Definition: pigpio.h:384
#define PI_CLOCK_PCM
Definition: pigpio.h:832
#define PI_CMD_BS2
Definition: pigpio.h:6076
static void myGpioWrite(unsigned gpio, unsigned level)
Definition: pigpio.c:1638
#define PI_TIMEOUT
Definition: pigpio.h:577
cmdScript_t script
Definition: pigpio.c:999
int gpioWaveClear(void)
Definition: pigpio.c:9034
#define PI_CMD_INR
Definition: pigpio.h:6227
static dmaIPage_t ** dmaIVirt
Definition: pigpio.c:1311
static uint8_t bbSPIXferByte(wfRx_t *w, char txByte)
Definition: pigpio.c:10718
#define PI_BAD_WAVE_MODE
Definition: pigpio.h:6297
int nfSteadyUs
Definition: pigpio.c:925
#define PI_CMD_CALL
Definition: pigpio.h:6219
uint32_t internals
Definition: pigpio.c:1082
#define PI_NOTIFY_SLOTS
Definition: pigpio.h:622
#define LVS_PER_IPAGE
Definition: pigpio.c:688
static unsigned mbAllocateMemory(int fd, unsigned size, unsigned align, unsigned flags)
Definition: pigpio.c:2716
int gpioWaveGetMicros(void)
Definition: pigpio.c:10110
#define PI_CMD_PROCS
Definition: pigpio.h:6102
int CS
Definition: pigpio.c:1133
int gpioSerialReadInvert(unsigned gpio, unsigned invert)
Definition: pigpio.c:11076
#define MAX_REPORT
Definition: pigpio.c:773
#define PI_CMD_PUSH
Definition: pigpio.h:6244
int gpioSetTimerFunc(unsigned id, unsigned millis, gpioTimerFunc_t f)
Definition: pigpio.c:12057
static wfStats_t wfStats
Definition: pigpio.c:1234
int gpioCfgBufferSize(unsigned millis)
Definition: pigpio.c:13325
#define PCM_INTSTC
Definition: pigpio.c:420
#define SPI_CS_TA
Definition: pigpio.c:545
static void myClearGpioOff(unsigned gpio, int pos)
Definition: pigpio.c:2484
int gpioWaveChain(char *buf, unsigned bufSize)
Definition: pigpio.c:9764
int eventTrigger(unsigned event)
Definition: pigpio.c:11256
void(* gpioSignalFuncEx_t)(int signum, void *userdata)
Definition: pigpio.h:542
static dmaPage_t ** dmaBus
Definition: pigpio.c:1309
uint16_t numCB
Definition: pigpio.h:462
uint16_t seqno
Definition: pigpio.h:416
uint32_t gfSteadyUs
Definition: pigpio.c:933
#define PI_MAX_MS
Definition: pigpio.h:793
unsigned ex
Definition: pigpio.c:918
#define WCB_CHAIN_CBS
Definition: pigpio.c:716
static gpioScript_t gpioScript[PI_MAX_SCRIPTS]
Definition: pigpio.c:1285
void bscInit(int mode)
Definition: pigpio.c:10554
static uint32_t old_spi_clk
Definition: pigpio.c:1369
static void wfRx_lock(int i)
Definition: pigpio.c:1808
#define PI_BAD_MICS_DELAY
Definition: pigpio.h:6329
#define PI_CMD_BSPIX
Definition: pigpio.h:6192
#define WCB_COUNTER_OOL
Definition: pigpio.c:714
int gpioUpdateScript(unsigned script_id, unsigned numParam, uint32_t *param)
Definition: pigpio.c:12268
#define PI_MIN_SOCKET_PORT
Definition: pigpio.h:841
unsigned dbgLevel
Definition: pigpio.c:1080
uint32_t level
Definition: pigpio.h:419
uint32_t alertTicks
Definition: pigpio.c:1053
int i2cWriteWordData(unsigned handle, unsigned reg, unsigned wVal)
Definition: pigpio.c:3552
static void waveBitDelay(unsigned baud, unsigned bits, unsigned stops, unsigned *bitDelay)
Definition: pigpio.c:2897
uint32_t DMARestarts
Definition: pigpio.c:1063
#define PI_DEFAULT_UPDATE_MASK_PI3B
Definition: pigpio.h:6438
#define PI_MEM_ALLOC_PAGEMAP
Definition: pigpio.h:855
#define PI_HW_CLK_MAX_FREQ
Definition: pigpio.h:620
#define SPI_CS_DONE
Definition: pigpio.c:538
#define AUXSPI_CNTL0_SPEED(x)
Definition: pigpio.c:619
int i2cSegments(unsigned handle, pi_i2c_msg_t *segs, unsigned numSegs)
Definition: pigpio.c:4060
#define PI_DEFAULT_DUTYCYCLE_RANGE
Definition: pigpio.h:598
#define PI_CMD_I2CWQ
Definition: pigpio.h:6120
#define PWM_CTL_CLRF1
Definition: pigpio.c:404
static int fdMbox
Definition: pigpio.c:1304
#define PI_BAD_TIMETYPE
Definition: pigpio.h:6273
static int fdPmap
Definition: pigpio.c:1303
uint16_t freqIdx
Definition: pigpio.c:910
void report(int err, char *fmt,...)
Definition: pigs.c:64
#define PI_NO_HANDLE
Definition: pigpio.h:6287
#define PI_BAD_SIGNUM
Definition: pigpio.h:6285
#define PI_CMD_WVBSY
Definition: pigpio.h:6093
int gpioStopScript(unsigned script_id)
Definition: pigpio.c:12325
static void dmaInitCbs(void)
Definition: pigpio.c:5430
#define PI_BAD_TIMER
Definition: pigpio.h:6271
#define PI_I2C_WRITE_FAILED
Definition: pigpio.h:6347
#define BSC_CE_N
Definition: pigpio.h:774
#define GPIO_BASE
Definition: pigpio.c:307
#define WCB_CNT_CBS
Definition: pigpio.c:712
static int waveDelayCBs(uint32_t delay)
Definition: pigpio.c:2919
#define PWM_DMAC_PANIC(x)
Definition: pigpio.c:410
static int myGpioRead(unsigned gpio)
Definition: pigpio.c:1629
#define PI_BAD_WAVE_BAUD
Definition: pigpio.h:6299
int gpioWaveAddGeneric(unsigned numPulses, gpioPulse_t *pulses)
Definition: pigpio.c:9084
static void initClock(int mainClock)
Definition: pigpio.c:7691
int serOpen(char *tty, unsigned serBaud, unsigned serFlags)
Definition: pigpio.c:4800
#define PI_BAD_I2C_CMD
Definition: pigpio.h:6376
#define PI_NOT_I2C_GPIO
Definition: pigpio.h:6373
#define PI_CMD_LD
Definition: pigpio.h:6235
int gpioSetTimerFuncEx(unsigned id, unsigned millis, gpioTimerFuncEx_t f, void *userdata)
Definition: pigpio.c:12077
static serInfo_t serInfo[PI_SER_SLOTS]
Definition: pigpio.c:1282
static void dmaTickCb(int b, int pos)
Definition: pigpio.c:5361
static char * myTimeStamp()
Definition: pigpio.c:1516
static int wfcur
Definition: pigpio.c:1232
#define CHECK_INITED
Definition: pigpio.c:219
#define PI_I2C_FUNC_SMBUS_READ_WORD_DATA
Definition: pigpio.c:810
#define PI_BAD_WVSC_COMMND
Definition: pigpio.h:6307
static void myGpioSetServo(unsigned gpio, int oldVal, int newVal)
Definition: pigpio.c:2603
static dmaOPage_t ** dmaOVirt
Definition: pigpio.c:1314
#define PI_WAVE_MAX_BAUD
Definition: pigpio.h:646
#define PI_WFRX_SPI_MOSI
Definition: pigpio.c:766
#define PI_CMD_GDC
Definition: pigpio.h:6148
#define PI_BAD_SPI_COUNT
Definition: pigpio.h:6349
#define PI_WAVE_MODE_ONE_SHOT_SYNC
Definition: pigpio.h:670
unsigned speed
Definition: pigpio.c:1047
static int mbDMAAlloc(DMAMem_t *DMAMemP, unsigned size, uint32_t pi_mem_flag)
Definition: pigpio.c:2817
int gpioCfgInternals(unsigned cfgWhat, unsigned cfgVal)
Definition: pigpio.c:13512
#define GPIO_PWM
Definition: pigpio.c:669
#define DMAO_PAGES
Definition: pigpio.c:730
#define PWM_DMAC_ENAB
Definition: pigpio.c:409
#define PI_DEFAULT_BUFFER_MILLIS
Definition: pigpio.h:6421
#define PI_CMD_SLRO
Definition: pigpio.h:6103
#define SYST_CLO
Definition: pigpio.c:519
static unsigned old_mode_sclk
Definition: pigpio.c:1364
#define PI_BAD_SPI_CHANNEL
Definition: pigpio.h:6341
int i2cReadByte(unsigned handle)
Definition: pigpio.c:3368
#define PI_CMD_SLRI
Definition: pigpio.h:6165
#define PI_CMD_NB
Definition: pigpio.h:6080
uint16_t topOOL
Definition: pigpio.h:459
int timeout
Definition: pigpio.c:1110
#define PI_CMD_JP
Definition: pigpio.h:6232
int gpioWaveCreate(void)
Definition: pigpio.c:9388
static void mbDMAFree(DMAMem_t *DMAMemP)
Definition: pigpio.c:2806
#define PI_SPI_MOSI
Definition: pigpio.c:571
#define PWM_CTL_MSEN1
Definition: pigpio.c:403
#define PI_ASPI_CE1
Definition: pigpio.c:576
unsigned ifFlags
Definition: pigpio.c:1078
uint32_t flags
Definition: pigpio.c:1048
#define PI_I2C_FUNC_SMBUS_QUICK
Definition: pigpio.c:805
#define BSC_CR
Definition: pigpio.h:738
void rawWaveSetOut(int pos, uint32_t value)
Definition: pigpio.c:8308
#define PI_MEM_ALLOC_MAILBOX
Definition: pigpio.h:856
static FILE * inpFifo
Definition: pigpio.c:1297


cob_hand_bridge
Author(s): Mathias Lüdtke
autogenerated on Tue Oct 20 2020 03:35:57