1
2
3
4
5
6
7
8
310
311
312
313
314 #ifndef BCM2835_H
315 #define BCM2835_H
316
317 #include
318
322
323
325 #define HIGH 0x1
326
327 #define LOW 0x0
328
330 #define BCM2835_CORE_CLK_HZ 250000000
331
332
334 #define BCM2835_PERI_BASE 0x20000000
335
336 #define BCM2835_ST_BASE (BCM2835_PERI_BASE + 0x3000)
337
338 #define BCM2835_GPIO_PADS (BCM2835_PERI_BASE + 0x100000)
339
340 #define BCM2835_CLOCK_BASE (BCM2835_PERI_BASE + 0x101000)
341
342 #define BCM2835_GPIO_BASE (BCM2835_PERI_BASE + 0x200000)
343
344 #define BCM2835_SPI0_BASE (BCM2835_PERI_BASE + 0x204000)
345
346 #define BCM2835_BSC0_BASE (BCM2835_PERI_BASE + 0x205000)
347
348 #define BCM2835_GPIO_PWM (BCM2835_PERI_BASE + 0x20C000)
349
350 #define BCM2835_BSC1_BASE (BCM2835_PERI_BASE + 0x804000)
351
352
355 extern volatile uint32_t *bcm2835_st;
356
359 extern volatile uint32_t *bcm2835_gpio;
360
363 extern volatile uint32_t *bcm2835_pwm;
364
367 extern volatile uint32_t *bcm2835_clk;
368
371 extern volatile uint32_t *bcm2835_pads;
372
375 extern volatile uint32_t *bcm2835_spi0;
376
379 extern volatile uint32_t *bcm2835_bsc0;
380
383 extern volatile uint32_t *bcm2835_bsc1;
384
386 #define BCM2835_PAGE_SIZE (4*1024)
387
388 #define BCM2835_BLOCK_SIZE (4*1024)
389
390
391
392
393
395 #define BCM2835_GPFSEL0 0x0000
396 #define BCM2835_GPFSEL1 0x0004
397 #define BCM2835_GPFSEL2 0x0008
398 #define BCM2835_GPFSEL3 0x000c
399 #define BCM2835_GPFSEL4 0x0010
400 #define BCM2835_GPFSEL5 0x0014
401 #define BCM2835_GPSET0 0x001c
402 #define BCM2835_GPSET1 0x0020
403 #define BCM2835_GPCLR0 0x0028
404 #define BCM2835_GPCLR1 0x002c
405 #define BCM2835_GPLEV0 0x0034
406 #define BCM2835_GPLEV1 0x0038
407 #define BCM2835_GPEDS0 0x0040
408 #define BCM2835_GPEDS1 0x0044
409 #define BCM2835_GPREN0 0x004c
410 #define BCM2835_GPREN1 0x0050
411 #define BCM2835_GPFEN0 0x0058
412 #define BCM2835_GPFEN1 0x005c
413 #define BCM2835_GPHEN0 0x0064
414 #define BCM2835_GPHEN1 0x0068
415 #define BCM2835_GPLEN0 0x0070
416 #define BCM2835_GPLEN1 0x0074
417 #define BCM2835_GPAREN0 0x007c
418 #define BCM2835_GPAREN1 0x0080
419 #define BCM2835_GPAFEN0 0x0088
420 #define BCM2835_GPAFEN1 0x008c
421 #define BCM2835_GPPUD 0x0094
422 #define BCM2835_GPPUDCLK0 0x0098
423 #define BCM2835_GPPUDCLK1 0x009c
424
425
426
427 typedef enum
428 {
429 BCM2835_GPIO_FSEL_INPT = 0b000,
430 BCM2835_GPIO_FSEL_OUTP = 0b001,
431 BCM2835_GPIO_FSEL_ALT0 = 0b100,
432 BCM2835_GPIO_FSEL_ALT1 = 0b101,
433 BCM2835_GPIO_FSEL_ALT2 = 0b110,
434 BCM2835_GPIO_FSEL_ALT3 = 0b111,
435 BCM2835_GPIO_FSEL_ALT4 = 0b011,
436 BCM2835_GPIO_FSEL_ALT5 = 0b010,
437 BCM2835_GPIO_FSEL_MASK = 0b111
438 } bcm2835FunctionSelect;
439
442 typedef enum
443 {
444 BCM2835_GPIO_PUD_OFF = 0b00,
445 BCM2835_GPIO_PUD_DOWN = 0b01,
446 BCM2835_GPIO_PUD_UP = 0b10
447 } bcm2835PUDControl;
448
450 #define BCM2835_PADS_GPIO_0_27 0x002c
451 #define BCM2835_PADS_GPIO_28_45 0x0030
452 #define BCM2835_PADS_GPIO_46_53 0x0034
453
454
455 #define BCM2835_PAD_PASSWRD (0x5A << 24)
456 #define BCM2835_PAD_SLEW_RATE_UNLIMITED 0x10
457 #define BCM2835_PAD_HYSTERESIS_ENABLED 0x08
458 #define BCM2835_PAD_DRIVE_2mA 0x00
459 #define BCM2835_PAD_DRIVE_4mA 0x01
460 #define BCM2835_PAD_DRIVE_6mA 0x02
461 #define BCM2835_PAD_DRIVE_8mA 0x03
462 #define BCM2835_PAD_DRIVE_10mA 0x04
463 #define BCM2835_PAD_DRIVE_12mA 0x05
464 #define BCM2835_PAD_DRIVE_14mA 0x06
465 #define BCM2835_PAD_DRIVE_16mA 0x07
466
467
468
469 typedef enum
470 {
471 BCM2835_PAD_GROUP_GPIO_0_27 = 0,
472 BCM2835_PAD_GROUP_GPIO_28_45 = 1,
473 BCM2835_PAD_GROUP_GPIO_46_53 = 2
474 } bcm2835PadGroup;
475
486 typedef enum
487 {
488 RPI_GPIO_P1_03 = 0,
489 RPI_GPIO_P1_05 = 1,
490 RPI_GPIO_P1_07 = 4,
491 RPI_GPIO_P1_08 = 14,
492 RPI_GPIO_P1_10 = 15,
493 RPI_GPIO_P1_11 = 17,
494 RPI_GPIO_P1_12 = 18,
495 RPI_GPIO_P1_13 = 21,
496 RPI_GPIO_P1_15 = 22,
497 RPI_GPIO_P1_16 = 23,
498 RPI_GPIO_P1_18 = 24,
499 RPI_GPIO_P1_19 = 10,
500 RPI_GPIO_P1_21 = 9,
501 RPI_GPIO_P1_22 = 25,
502 RPI_GPIO_P1_23 = 11,
503 RPI_GPIO_P1_24 = 8,
504 RPI_GPIO_P1_26 = 7,
505
506
507 RPI_V2_GPIO_P1_03 = 2,
508 RPI_V2_GPIO_P1_05 = 3,
509 RPI_V2_GPIO_P1_07 = 4,
510 RPI_V2_GPIO_P1_08 = 14,
511 RPI_V2_GPIO_P1_10 = 15,
512 RPI_V2_GPIO_P1_11 = 17,
513 RPI_V2_GPIO_P1_12 = 18,
514 RPI_V2_GPIO_P1_13 = 27,
515 RPI_V2_GPIO_P1_15 = 22,
516 RPI_V2_GPIO_P1_16 = 23,
517 RPI_V2_GPIO_P1_18 = 24,
518 RPI_V2_GPIO_P1_19 = 10,
519 RPI_V2_GPIO_P1_21 = 9,
520 RPI_V2_GPIO_P1_22 = 25,
521 RPI_V2_GPIO_P1_23 = 11,
522 RPI_V2_GPIO_P1_24 = 8,
523 RPI_V2_GPIO_P1_26 = 7,
524
525
526 RPI_V2_GPIO_P5_03 = 28,
527 RPI_V2_GPIO_P5_04 = 29,
528 RPI_V2_GPIO_P5_05 = 30,
529 RPI_V2_GPIO_P5_06 = 31,
530
531 } RPiGPIOPin;
532
533
534
535
536 #define BCM2835_SPI0_CS 0x0000
537 #define BCM2835_SPI0_FIFO 0x0004
538 #define BCM2835_SPI0_CLK 0x0008
539 #define BCM2835_SPI0_DLEN 0x000c
540 #define BCM2835_SPI0_LTOH 0x0010
541 #define BCM2835_SPI0_DC 0x0014
542
543
544 #define BCM2835_SPI0_CS_LEN_LONG 0x02000000
545 #define BCM2835_SPI0_CS_DMA_LEN 0x01000000
546 #define BCM2835_SPI0_CS_CSPOL2 0x00800000
547 #define BCM2835_SPI0_CS_CSPOL1 0x00400000
548 #define BCM2835_SPI0_CS_CSPOL0 0x00200000
549 #define BCM2835_SPI0_CS_RXF 0x00100000
550 #define BCM2835_SPI0_CS_RXR 0x00080000
551 #define BCM2835_SPI0_CS_TXD 0x00040000
552 #define BCM2835_SPI0_CS_RXD 0x00020000
553 #define BCM2835_SPI0_CS_DONE 0x00010000
554 #define BCM2835_SPI0_CS_TE_EN 0x00008000
555 #define BCM2835_SPI0_CS_LMONO 0x00004000
556 #define BCM2835_SPI0_CS_LEN 0x00002000
557 #define BCM2835_SPI0_CS_REN 0x00001000
558 #define BCM2835_SPI0_CS_ADCS 0x00000800
559 #define BCM2835_SPI0_CS_INTR 0x00000400
560 #define BCM2835_SPI0_CS_INTD 0x00000200
561 #define BCM2835_SPI0_CS_DMAEN 0x00000100
562 #define BCM2835_SPI0_CS_TA 0x00000080
563 #define BCM2835_SPI0_CS_CSPOL 0x00000040
564 #define BCM2835_SPI0_CS_CLEAR 0x00000030
565 #define BCM2835_SPI0_CS_CLEAR_RX 0x00000020
566 #define BCM2835_SPI0_CS_CLEAR_TX 0x00000010
567 #define BCM2835_SPI0_CS_CPOL 0x00000008
568 #define BCM2835_SPI0_CS_CPHA 0x00000004
569 #define BCM2835_SPI0_CS_CS 0x00000003
570
571
572
573 typedef enum
574 {
575 BCM2835_SPI_BIT_ORDER_LSBFIRST = 0,
576 BCM2835_SPI_BIT_ORDER_MSBFIRST = 1
577 }bcm2835SPIBitOrder;
578
581 typedef enum
582 {
583 BCM2835_SPI_MODE0 = 0,
584 BCM2835_SPI_MODE1 = 1,
585 BCM2835_SPI_MODE2 = 2,
586 BCM2835_SPI_MODE3 = 3,
587 }bcm2835SPIMode;
588
591 typedef enum
592 {
593 BCM2835_SPI_CS0 = 0,
594 BCM2835_SPI_CS1 = 1,
595 BCM2835_SPI_CS2 = 2,
596 BCM2835_SPI_CS_NONE = 3,
597 } bcm2835SPIChipSelect;
598
605 typedef enum
606 {
607 BCM2835_SPI_CLOCK_DIVIDER_65536 = 0,
608 BCM2835_SPI_CLOCK_DIVIDER_32768 = 32768,
609 BCM2835_SPI_CLOCK_DIVIDER_16384 = 16384,
610 BCM2835_SPI_CLOCK_DIVIDER_8192 = 8192,
611 BCM2835_SPI_CLOCK_DIVIDER_4096 = 4096,
612 BCM2835_SPI_CLOCK_DIVIDER_2048 = 2048,
613 BCM2835_SPI_CLOCK_DIVIDER_1024 = 1024,
614 BCM2835_SPI_CLOCK_DIVIDER_512 = 512,
615 BCM2835_SPI_CLOCK_DIVIDER_256 = 256,
616 BCM2835_SPI_CLOCK_DIVIDER_128 = 128,
617 BCM2835_SPI_CLOCK_DIVIDER_64 = 64,
618 BCM2835_SPI_CLOCK_DIVIDER_32 = 32,
619 BCM2835_SPI_CLOCK_DIVIDER_16 = 16,
620 BCM2835_SPI_CLOCK_DIVIDER_8 = 8,
621 BCM2835_SPI_CLOCK_DIVIDER_4 = 4,
622 BCM2835_SPI_CLOCK_DIVIDER_2 = 2,
623 BCM2835_SPI_CLOCK_DIVIDER_1 = 1,
624 } bcm2835SPIClockDivider;
625
626
627
628
629 #define BCM2835_BSC_C 0x0000
630 #define BCM2835_BSC_S 0x0004
631 #define BCM2835_BSC_DLEN 0x0008
632 #define BCM2835_BSC_A 0x000c
633 #define BCM2835_BSC_FIFO 0x0010
634 #define BCM2835_BSC_DIV 0x0014
635 #define BCM2835_BSC_DEL 0x0018
636 #define BCM2835_BSC_CLKT 0x001c
637
638
639 #define BCM2835_BSC_C_I2CEN 0x00008000
640 #define BCM2835_BSC_C_INTR 0x00000400
641 #define BCM2835_BSC_C_INTT 0x00000200
642 #define BCM2835_BSC_C_INTD 0x00000100
643 #define BCM2835_BSC_C_ST 0x00000080
644 #define BCM2835_BSC_C_CLEAR_1 0x00000020
645 #define BCM2835_BSC_C_CLEAR_2 0x00000010
646 #define BCM2835_BSC_C_READ 0x00000001
647
648
649 #define BCM2835_BSC_S_CLKT 0x00000200
650 #define BCM2835_BSC_S_ERR 0x00000100
651 #define BCM2835_BSC_S_RXF 0x00000080
652 #define BCM2835_BSC_S_TXE 0x00000040
653 #define BCM2835_BSC_S_RXD 0x00000020
654 #define BCM2835_BSC_S_TXD 0x00000010
655 #define BCM2835_BSC_S_RXR 0x00000008
656 #define BCM2835_BSC_S_TXW 0x00000004
657 #define BCM2835_BSC_S_DONE 0x00000002
658 #define BCM2835_BSC_S_TA 0x00000001
659
660 #define BCM2835_BSC_FIFO_SIZE 16
661
662
663
664
665 typedef enum
666 {
667 BCM2835_I2C_CLOCK_DIVIDER_2500 = 2500,
668 BCM2835_I2C_CLOCK_DIVIDER_626 = 626,
669 BCM2835_I2C_CLOCK_DIVIDER_150 = 150,
670 BCM2835_I2C_CLOCK_DIVIDER_148 = 148,
671 } bcm2835I2CClockDivider;
672
675 typedef enum
676 {
677 BCM2835_I2C_REASON_OK = 0x00,
678 BCM2835_I2C_REASON_ERROR_NACK = 0x01,
679 BCM2835_I2C_REASON_ERROR_CLKT = 0x02,
680 BCM2835_I2C_REASON_ERROR_DATA = 0x04,
681 } bcm2835I2CReasonCodes;
682
683
684
685
686
687
688
689
690
691
692
693 #define BCM2835_ST_CS 0x0000
694 #define BCM2835_ST_CLO 0x0004
695 #define BCM2835_ST_CHI 0x0008
696
697
698
699
700
701 #define BCM2835_PWM_CONTROL 0
702 #define BCM2835_PWM_STATUS 1
703 #define BCM2835_PWM_DMAC 2
704 #define BCM2835_PWM0_RANGE 4
705 #define BCM2835_PWM0_DATA 5
706 #define BCM2835_PWM_FIF1 6
707 #define BCM2835_PWM1_RANGE 8
708 #define BCM2835_PWM1_DATA 9
709
710
711 #define BCM2835_PWMCLK_CNTL 40
712 #define BCM2835_PWMCLK_DIV 41
713 #define BCM2835_PWM_PASSWRD (0x5A << 24)
714
715 #define BCM2835_PWM1_MS_MODE 0x8000
716 #define BCM2835_PWM1_USEFIFO 0x2000
717 #define BCM2835_PWM1_REVPOLAR 0x1000
718 #define BCM2835_PWM1_OFFSTATE 0x0800
719 #define BCM2835_PWM1_REPEATFF 0x0400
720 #define BCM2835_PWM1_SERIAL 0x0200
721 #define BCM2835_PWM1_ENABLE 0x0100
722
723 #define BCM2835_PWM0_MS_MODE 0x0080
724 #define BCM2835_PWM_CLEAR_FIFO 0x0040
725 #define BCM2835_PWM0_USEFIFO 0x0020
726 #define BCM2835_PWM0_REVPOLAR 0x0010
727 #define BCM2835_PWM0_OFFSTATE 0x0008
728 #define BCM2835_PWM0_REPEATFF 0x0004
729 #define BCM2835_PWM0_SERIAL 0x0002
730 #define BCM2835_PWM0_ENABLE 0x0001
731
732
733
734
735
736
737 typedef enum
738 {
739 BCM2835_PWM_CLOCK_DIVIDER_32768 = 32768,
740 BCM2835_PWM_CLOCK_DIVIDER_16384 = 16384,
741 BCM2835_PWM_CLOCK_DIVIDER_8192 = 8192,
742 BCM2835_PWM_CLOCK_DIVIDER_4096 = 4096,
743 BCM2835_PWM_CLOCK_DIVIDER_2048 = 2048,
744 BCM2835_PWM_CLOCK_DIVIDER_1024 = 1024,
745 BCM2835_PWM_CLOCK_DIVIDER_512 = 512,
746 BCM2835_PWM_CLOCK_DIVIDER_256 = 256,
747 BCM2835_PWM_CLOCK_DIVIDER_128 = 128,
748 BCM2835_PWM_CLOCK_DIVIDER_64 = 64,
749 BCM2835_PWM_CLOCK_DIVIDER_32 = 32,
750 BCM2835_PWM_CLOCK_DIVIDER_16 = 16,
751 BCM2835_PWM_CLOCK_DIVIDER_8 = 8,
752 BCM2835_PWM_CLOCK_DIVIDER_4 = 4,
753 BCM2835_PWM_CLOCK_DIVIDER_2 = 2,
754 BCM2835_PWM_CLOCK_DIVIDER_1 = 1,
755 } bcm2835PWMClockDivider;
756
757
758 #ifndef BCM2835_NO_DELAY_COMPATIBILITY
759 #define delay(x) bcm2835_delay(x)
760 #define delayMicroseconds(x) bcm2835_delayMicroseconds(x)
761 #endif
762
763 #ifdef __cplusplus
764 extern "C" {
765 #endif
766
770
779 extern int bcm2835_init(void);
780
783 extern int bcm2835_close(void);
784
791 extern void bcm2835_set_debug(uint8_t debug);
792
794
800
807 extern uint32_t bcm2835_peri_read(volatile uint32_t* paddr);
808
809
816 extern uint32_t bcm2835_peri_read_nb(volatile uint32_t* paddr);
817
818
825 extern void bcm2835_peri_write(volatile uint32_t* paddr, uint32_t value);
826
833 extern void bcm2835_peri_write_nb(volatile uint32_t* paddr, uint32_t value);
834
846 extern void bcm2835_peri_set_bits(volatile uint32_t* paddr, uint32_t value, uint32_t mask);
848
853
858 extern void bcm2835_gpio_fsel(uint8_t pin, uint8_t mode);
859
864 extern void bcm2835_gpio_set(uint8_t pin);
865
870 extern void bcm2835_gpio_clr(uint8_t pin);
871
876 extern void bcm2835_gpio_set_multi(uint32_t mask);
877
882 extern void bcm2835_gpio_clr_multi(uint32_t mask);
883
889 extern uint8_t bcm2835_gpio_lev(uint8_t pin);
890
898 extern uint8_t bcm2835_gpio_eds(uint8_t pin);
899
904 extern void bcm2835_gpio_set_eds(uint8_t pin);
905
913 extern void bcm2835_gpio_ren(uint8_t pin);
914
917 extern void bcm2835_gpio_clr_ren(uint8_t pin);
918
926 extern void bcm2835_gpio_fen(uint8_t pin);
927
930 extern void bcm2835_gpio_clr_fen(uint8_t pin);
931
935 extern void bcm2835_gpio_hen(uint8_t pin);
936
939 extern void bcm2835_gpio_clr_hen(uint8_t pin);
940
944 extern void bcm2835_gpio_len(uint8_t pin);
945
948 extern void bcm2835_gpio_clr_len(uint8_t pin);
949
955 extern void bcm2835_gpio_aren(uint8_t pin);
956
959 extern void bcm2835_gpio_clr_aren(uint8_t pin);
960
966 extern void bcm2835_gpio_afen(uint8_t pin);
967
970 extern void bcm2835_gpio_clr_afen(uint8_t pin);
971
977 extern void bcm2835_gpio_pud(uint8_t pud);
978
984 extern void bcm2835_gpio_pudclk(uint8_t pin, uint8_t on);
985
989 extern uint32_t bcm2835_gpio_pad(uint8_t group);
990
996 extern void bcm2835_gpio_set_pad(uint8_t group, uint32_t control);
997
1007 extern void bcm2835_delay (unsigned int millis);
1008
1021 extern void bcm2835_delayMicroseconds (uint64_t micros);
1022
1026 extern void bcm2835_gpio_write(uint8_t pin, uint8_t on);
1027
1031 extern void bcm2835_gpio_write_multi(uint32_t mask, uint8_t on);
1032
1036 extern void bcm2835_gpio_write_mask(uint32_t value, uint32_t mask);
1037
1042 extern void bcm2835_gpio_set_pud(uint8_t pin, uint8_t pud);
1043
1045
1050
1057 extern void bcm2835_spi_begin(void);
1058
1062 extern void bcm2835_spi_end(void);
1063
1069 extern void bcm2835_spi_setBitOrder(uint8_t order);
1070
1075 extern void bcm2835_spi_setClockDivider(uint16_t divider);
1076
1081 extern void bcm2835_spi_setDataMode(uint8_t mode);
1082
1088 extern void bcm2835_spi_chipSelect(uint8_t cs);
1089
1097 extern void bcm2835_spi_setChipSelectPolarity(uint8_t cs, uint8_t active);
1098
1108 extern uint8_t bcm2835_spi_transfer(uint8_t value);
1109
1120 extern void bcm2835_spi_transfernb(char* tbuf, char* rbuf, uint32_t len);
1121
1128 extern void bcm2835_spi_transfern(char* buf, uint32_t len);
1129
1135 extern void bcm2835_spi_writenb(char* buf, uint32_t len);
1136
1138
1143
1150 extern void bcm2835_i2c_begin(void);
1151
1155 extern void bcm2835_i2c_end(void);
1156
1159 extern void bcm2835_i2c_setSlaveAddress(uint8_t addr);
1160
1164 extern void bcm2835_i2c_setClockDivider(uint16_t divider);
1165
1171 extern void bcm2835_i2c_set_baudrate(uint32_t baudrate);
1172
1178 extern uint8_t bcm2835_i2c_write(const char * buf, uint32_t len);
1179
1185 extern uint8_t bcm2835_i2c_read(char* buf, uint32_t len);
1186
1200 extern uint8_t bcm2835_i2c_read_register_rs(char* regaddr, char* buf, uint32_t len);
1201
1211 extern uint8_t bcm2835_i2c_write_read_rs(char* cmds, uint32_t cmds_len, char* buf, uint32_t buf_len);
1212
1214
1218
1221 extern uint64_t bcm2835_st_read(void);
1222
1226 extern void bcm2835_st_delay(uint64_t offset_micros, uint64_t micros);
1227
1229
1236
1241 extern void bcm2835_pwm_set_clock(uint32_t divisor);
1242
1248 extern void bcm2835_pwm_set_mode(uint8_t channel, uint8_t markspace, uint8_t enabled);
1249
1254 extern void bcm2835_pwm_set_range(uint8_t channel, uint32_t range);
1255
1261 extern void bcm2835_pwm_set_data(uint8_t channel, uint32_t data);
1262
1264 #ifdef __cplusplus
1265 }
1266 #endif
1267
1268 #endif // BCM2835_H
1269
1272
1275
1278