1 // bcm2835.h 2 // 3 // C and C++ support for Broadcom BCM 2835 as used in Raspberry Pi 4 // 5 // Author: Mike McCauley 6 // Copyright (C) 2011-2013 Mike McCauley 7 // $Id: bcm2835.h,v 1.8 2013/02/15 22:06:09 mikem Exp mikem $ 8 // 231 232 233 234 // Defines for BCM2835 235 #ifndef BCM2835_H 236 #define BCM2835_H 237 238 #include 239 243 244 246 #define HIGH 0x1 247 248 #define LOW 0x0 249 251 #define BCM2835_CORE_CLK_HZ 250000000 252 253 // Physical addresses for various peripheral register sets 255 #define BCM2835_PERI_BASE 0x20000000 256 257 #define BCM2835_ST_BASE (BCM2835_PERI_BASE + 0x3000) 258 259 #define BCM2835_GPIO_PADS (BCM2835_PERI_BASE + 0x100000) 260 261 #define BCM2835_CLOCK_BASE (BCM2835_PERI_BASE + 0x101000) 262 263 #define BCM2835_GPIO_BASE (BCM2835_PERI_BASE + 0x200000) 264 265 #define BCM2835_SPI0_BASE (BCM2835_PERI_BASE + 0x204000) 266 267 #define BCM2835_BSC0_BASE (BCM2835_PERI_BASE + 0x205000) 268 269 #define BCM2835_GPIO_PWM (BCM2835_PERI_BASE + 0x20C000) 270 271 #define BCM2835_BSC1_BASE (BCM2835_PERI_BASE + 0x804000) 272 273 276 extern volatile uint32_t *bcm2835_st; 277 280 extern volatile uint32_t *bcm2835_gpio; 281 284 extern volatile uint32_t *bcm2835_pwm; 285 288 extern volatile uint32_t *bcm2835_clk; 289 292 extern volatile uint32_t *bcm2835_pads; 293 296 extern volatile uint32_t *bcm2835_spi0; 297 300 extern volatile uint32_t *bcm2835_bsc0; 301 304 extern volatile uint32_t *bcm2835_bsc1; 305 307 #define BCM2835_PAGE_SIZE (4*1024) 308 309 #define BCM2835_BLOCK_SIZE (4*1024) 310 311 312 // Defines for GPIO 313 // The BCM2835 has 54 GPIO pins. 314 // BCM2835 data sheet, Page 90 onwards. 316 #define BCM2835_GPFSEL0 0x0000 317 #define BCM2835_GPFSEL1 0x0004 318 #define BCM2835_GPFSEL2 0x0008 319 #define BCM2835_GPFSEL3 0x000c 320 #define BCM2835_GPFSEL4 0x0010 321 #define BCM2835_GPFSEL5 0x0014 322 #define BCM2835_GPSET0 0x001c 323 #define BCM2835_GPSET1 0x0020 324 #define BCM2835_GPCLR0 0x0028 325 #define BCM2835_GPCLR1 0x002c 326 #define BCM2835_GPLEV0 0x0034 327 #define BCM2835_GPLEV1 0x0038 328 #define BCM2835_GPEDS0 0x0040 329 #define BCM2835_GPEDS1 0x0044 330 #define BCM2835_GPREN0 0x004c 331 #define BCM2835_GPREN1 0x0050 332 #define BCM2835_GPFEN0 0x0058 333 #define BCM2835_GPFEN1 0x005c 334 #define BCM2835_GPHEN0 0x0064 335 #define BCM2835_GPHEN1 0x0068 336 #define BCM2835_GPLEN0 0x0070 337 #define BCM2835_GPLEN1 0x0074 338 #define BCM2835_GPAREN0 0x007c 339 #define BCM2835_GPAREN1 0x0080 340 #define BCM2835_GPAFEN0 0x0088 341 #define BCM2835_GPAFEN1 0x008c 342 #define BCM2835_GPPUD 0x0094 343 #define BCM2835_GPPUDCLK0 0x0098 344 #define BCM2835_GPPUDCLK1 0x009c 345 346 347 348 typedef enum 349 { 350 BCM2835_GPIO_FSEL_INPT = 0b000, 351 BCM2835_GPIO_FSEL_OUTP = 0b001, 352 BCM2835_GPIO_FSEL_ALT0 = 0b100, 353 BCM2835_GPIO_FSEL_ALT1 = 0b101, 354 BCM2835_GPIO_FSEL_ALT2 = 0b110, 355 BCM2835_GPIO_FSEL_ALT3 = 0b111, 356 BCM2835_GPIO_FSEL_ALT4 = 0b011, 357 BCM2835_GPIO_FSEL_ALT5 = 0b010, 358 BCM2835_GPIO_FSEL_MASK = 0b111 359 } bcm2835FunctionSelect; 360 363 typedef enum 364 { 365 BCM2835_GPIO_PUD_OFF = 0b00, 366 BCM2835_GPIO_PUD_DOWN = 0b01, 367 BCM2835_GPIO_PUD_UP = 0b10 368 } bcm2835PUDControl; 369 371 #define BCM2835_PADS_GPIO_0_27 0x002c 372 #define BCM2835_PADS_GPIO_28_45 0x0030 373 #define BCM2835_PADS_GPIO_46_53 0x0034 374 375 376 #define BCM2835_PAD_PASSWRD (0x5A << 24) 377 #define BCM2835_PAD_SLEW_RATE_UNLIMITED 0x10 378 #define BCM2835_PAD_HYSTERESIS_ENABLED 0x08 379 #define BCM2835_PAD_DRIVE_2mA 0x00 380 #define BCM2835_PAD_DRIVE_4mA 0x01 381 #define BCM2835_PAD_DRIVE_6mA 0x02 382 #define BCM2835_PAD_DRIVE_8mA 0x03 383 #define BCM2835_PAD_DRIVE_10mA 0x04 384 #define BCM2835_PAD_DRIVE_12mA 0x05 385 #define BCM2835_PAD_DRIVE_14mA 0x06 386 #define BCM2835_PAD_DRIVE_16mA 0x07 387 388 389 390 typedef enum 391 { 392 BCM2835_PAD_GROUP_GPIO_0_27 = 0, 393 BCM2835_PAD_GROUP_GPIO_28_45 = 1, 394 BCM2835_PAD_GROUP_GPIO_46_53 = 2 395 } bcm2835PadGroup; 396 407 typedef enum 408 { 409 RPI_GPIO_P1_03 = 0, 410 RPI_GPIO_P1_05 = 1, 411 RPI_GPIO_P1_07 = 4, 412 RPI_GPIO_P1_08 = 14, 413 RPI_GPIO_P1_10 = 15, 414 RPI_GPIO_P1_11 = 17, 415 RPI_GPIO_P1_12 = 18, 416 RPI_GPIO_P1_13 = 21, 417 RPI_GPIO_P1_15 = 22, 418 RPI_GPIO_P1_16 = 23, 419 RPI_GPIO_P1_18 = 24, 420 RPI_GPIO_P1_19 = 10, 421 RPI_GPIO_P1_21 = 9, 422 RPI_GPIO_P1_22 = 25, 423 RPI_GPIO_P1_23 = 11, 424 RPI_GPIO_P1_24 = 8, 425 RPI_GPIO_P1_26 = 7, 426 427 // RPi Version 2 428 RPI_V2_GPIO_P1_03 = 2, 429 RPI_V2_GPIO_P1_05 = 3, 430 RPI_V2_GPIO_P1_07 = 4, 431 RPI_V2_GPIO_P1_08 = 14, 432 RPI_V2_GPIO_P1_10 = 15, 433 RPI_V2_GPIO_P1_11 = 17, 434 RPI_V2_GPIO_P1_12 = 18, 435 RPI_V2_GPIO_P1_13 = 27, 436 RPI_V2_GPIO_P1_15 = 22, 437 RPI_V2_GPIO_P1_16 = 23, 438 RPI_V2_GPIO_P1_18 = 24, 439 RPI_V2_GPIO_P1_19 = 10, 440 RPI_V2_GPIO_P1_21 = 9, 441 RPI_V2_GPIO_P1_22 = 25, 442 RPI_V2_GPIO_P1_23 = 11, 443 RPI_V2_GPIO_P1_24 = 8, 444 RPI_V2_GPIO_P1_26 = 7, 445 446 // RPi Version 2, new plug P5 447 RPI_V2_GPIO_P5_03 = 28, 448 RPI_V2_GPIO_P5_04 = 29, 449 RPI_V2_GPIO_P5_05 = 30, 450 RPI_V2_GPIO_P5_06 = 31, 451 452 } RPiGPIOPin; 453 454 // Defines for SPI 455 // GPIO register offsets from BCM2835_SPI0_BASE. 456 // Offsets into the SPI Peripheral block in bytes per 10.5 SPI Register Map 457 #define BCM2835_SPI0_CS 0x0000 458 #define BCM2835_SPI0_FIFO 0x0004 459 #define BCM2835_SPI0_CLK 0x0008 460 #define BCM2835_SPI0_DLEN 0x000c 461 #define BCM2835_SPI0_LTOH 0x0010 462 #define BCM2835_SPI0_DC 0x0014 463 464 // Register masks for SPI0_CS 465 #define BCM2835_SPI0_CS_LEN_LONG 0x02000000 466 #define BCM2835_SPI0_CS_DMA_LEN 0x01000000 467 #define BCM2835_SPI0_CS_CSPOL2 0x00800000 468 #define BCM2835_SPI0_CS_CSPOL1 0x00400000 469 #define BCM2835_SPI0_CS_CSPOL0 0x00200000 470 #define BCM2835_SPI0_CS_RXF 0x00100000 471 #define BCM2835_SPI0_CS_RXR 0x00080000 472 #define BCM2835_SPI0_CS_TXD 0x00040000 473 #define BCM2835_SPI0_CS_RXD 0x00020000 474 #define BCM2835_SPI0_CS_DONE 0x00010000 475 #define BCM2835_SPI0_CS_TE_EN 0x00008000 476 #define BCM2835_SPI0_CS_LMONO 0x00004000 477 #define BCM2835_SPI0_CS_LEN 0x00002000 478 #define BCM2835_SPI0_CS_REN 0x00001000 479 #define BCM2835_SPI0_CS_ADCS 0x00000800 480 #define BCM2835_SPI0_CS_INTR 0x00000400 481 #define BCM2835_SPI0_CS_INTD 0x00000200 482 #define BCM2835_SPI0_CS_DMAEN 0x00000100 483 #define BCM2835_SPI0_CS_TA 0x00000080 484 #define BCM2835_SPI0_CS_CSPOL 0x00000040 485 #define BCM2835_SPI0_CS_CLEAR 0x00000030 486 #define BCM2835_SPI0_CS_CLEAR_RX 0x00000020 487 #define BCM2835_SPI0_CS_CLEAR_TX 0x00000010 488 #define BCM2835_SPI0_CS_CPOL 0x00000008 489 #define BCM2835_SPI0_CS_CPHA 0x00000004 490 #define BCM2835_SPI0_CS_CS 0x00000003 491 492 493 494 typedef enum 495 { 496 BCM2835_SPI_BIT_ORDER_LSBFIRST = 0, 497 BCM2835_SPI_BIT_ORDER_MSBFIRST = 1 498 }bcm2835SPIBitOrder; 499 502 typedef enum 503 { 504 BCM2835_SPI_MODE0 = 0, 505 BCM2835_SPI_MODE1 = 1, 506 BCM2835_SPI_MODE2 = 2, 507 BCM2835_SPI_MODE3 = 3, 508 }bcm2835SPIMode; 509 512 typedef enum 513 { 514 BCM2835_SPI_CS0 = 0, 515 BCM2835_SPI_CS1 = 1, 516 BCM2835_SPI_CS2 = 2, 517 BCM2835_SPI_CS_NONE = 3, 518 } bcm2835SPIChipSelect; 519 526 typedef enum 527 { 528 BCM2835_SPI_CLOCK_DIVIDER_65536 = 0, 529 BCM2835_SPI_CLOCK_DIVIDER_32768 = 32768, 530 BCM2835_SPI_CLOCK_DIVIDER_16384 = 16384, 531 BCM2835_SPI_CLOCK_DIVIDER_8192 = 8192, 532 BCM2835_SPI_CLOCK_DIVIDER_4096 = 4096, 533 BCM2835_SPI_CLOCK_DIVIDER_2048 = 2048, 534 BCM2835_SPI_CLOCK_DIVIDER_1024 = 1024, 535 BCM2835_SPI_CLOCK_DIVIDER_512 = 512, 536 BCM2835_SPI_CLOCK_DIVIDER_256 = 256, 537 BCM2835_SPI_CLOCK_DIVIDER_128 = 128, 538 BCM2835_SPI_CLOCK_DIVIDER_64 = 64, 539 BCM2835_SPI_CLOCK_DIVIDER_32 = 32, 540 BCM2835_SPI_CLOCK_DIVIDER_16 = 16, 541 BCM2835_SPI_CLOCK_DIVIDER_8 = 8, 542 BCM2835_SPI_CLOCK_DIVIDER_4 = 4, 543 BCM2835_SPI_CLOCK_DIVIDER_2 = 2, 544 BCM2835_SPI_CLOCK_DIVIDER_1 = 1, 545 } bcm2835SPIClockDivider; 546 547 // Defines for I2C 548 // GPIO register offsets from BCM2835_BSC*_BASE. 549 // Offsets into the BSC Peripheral block in bytes per 3.1 BSC Register Map 550 #define BCM2835_BSC_C 0x0000 551 #define BCM2835_BSC_S 0x0004 552 #define BCM2835_BSC_DLEN 0x0008 553 #define BCM2835_BSC_A 0x000c 554 #define BCM2835_BSC_FIFO 0x0010 555 #define BCM2835_BSC_DIV 0x0014 556 #define BCM2835_BSC_DEL 0x0018 557 #define BCM2835_BSC_CLKT 0x001c 558 559 // Register masks for BSC_C 560 #define BCM2835_BSC_C_I2CEN 0x00008000 561 #define BCM2835_BSC_C_INTR 0x00000400 562 #define BCM2835_BSC_C_INTT 0x00000200 563 #define BCM2835_BSC_C_INTD 0x00000100 564 #define BCM2835_BSC_C_ST 0x00000080 565 #define BCM2835_BSC_C_CLEAR_1 0x00000020 566 #define BCM2835_BSC_C_CLEAR_2 0x00000010 567 #define BCM2835_BSC_C_READ 0x00000001 568 569 // Register masks for BSC_S 570 #define BCM2835_BSC_S_CLKT 0x00000200 571 #define BCM2835_BSC_S_ERR 0x00000100 572 #define BCM2835_BSC_S_RXF 0x00000080 573 #define BCM2835_BSC_S_TXE 0x00000040 574 #define BCM2835_BSC_S_RXD 0x00000020 575 #define BCM2835_BSC_S_TXD 0x00000010 576 #define BCM2835_BSC_S_RXR 0x00000008 577 #define BCM2835_BSC_S_TXW 0x00000004 578 #define BCM2835_BSC_S_DONE 0x00000002 579 #define BCM2835_BSC_S_TA 0x00000001 580 581 #define BCM2835_BSC_FIFO_SIZE 16 582 583 584 585 586 typedef enum 587 { 588 BCM2835_I2C_CLOCK_DIVIDER_2500 = 2500, 589 BCM2835_I2C_CLOCK_DIVIDER_626 = 626, 590 BCM2835_I2C_CLOCK_DIVIDER_150 = 150, 591 BCM2835_I2C_CLOCK_DIVIDER_148 = 148, 592 } bcm2835I2CClockDivider; 593 596 typedef enum 597 { 598 BCM2835_I2C_REASON_OK = 0x00, 599 BCM2835_I2C_REASON_ERROR_NACK = 0x01, 600 BCM2835_I2C_REASON_ERROR_CLKT = 0x02, 601 BCM2835_I2C_REASON_ERROR_DATA = 0x04, 602 } bcm2835I2CReasonCodes; 603 604 // Defines for ST 605 // GPIO register offsets from BCM2835_ST_BASE. 606 // Offsets into the ST Peripheral block in bytes per 12.1 System Timer Registers 607 // The System Timer peripheral provides four 32-bit timer channels and a single 64-bit free running counter. 608 // BCM2835_ST_CLO is the System Timer Counter Lower bits register. 609 // The system timer free-running counter lower register is a read-only register that returns the current value 610 // of the lower 32-bits of the free running counter. 611 // BCM2835_ST_CHI is the System Timer Counter Upper bits register. 612 // The system timer free-running counter upper register is a read-only register that returns the current value 613 // of the upper 32-bits of the free running counter. 614 #define BCM2835_ST_CS 0x0000 615 #define BCM2835_ST_CLO 0x0004 616 #define BCM2835_ST_CHI 0x0008 617 618 619 620 621 // Defines for PWM 622 #define BCM2835_PWM_CONTROL 0 623 #define BCM2835_PWM_STATUS 1 624 #define BCM2835_PWM0_RANGE 4 625 #define BCM2835_PWM0_DATA 5 626 #define BCM2835_PWM1_RANGE 8 627 #define BCM2835_PWM1_DATA 9 628 629 #define BCM2835_PWMCLK_CNTL 40 630 #define BCM2835_PWMCLK_DIV 41 631 632 #define BCM2835_PWM1_MS_MODE 0x8000 633 #define BCM2835_PWM1_USEFIFO 0x2000 634 #define BCM2835_PWM1_REVPOLAR 0x1000 635 #define BCM2835_PWM1_OFFSTATE 0x0800 636 #define BCM2835_PWM1_REPEATFF 0x0400 637 #define BCM2835_PWM1_SERIAL 0x0200 638 #define BCM2835_PWM1_ENABLE 0x0100 639 640 #define BCM2835_PWM0_MS_MODE 0x0080 641 #define BCM2835_PWM0_USEFIFO 0x0020 642 #define BCM2835_PWM0_REVPOLAR 0x0010 643 #define BCM2835_PWM0_OFFSTATE 0x0008 644 #define BCM2835_PWM0_REPEATFF 0x0004 645 #define BCM2835_PWM0_SERIAL 0x0002 646 #define BCM2835_PWM0_ENABLE 0x0001 647 648 // Historical name compatibility 649 #ifndef BCM2835_NO_DELAY_COMPATIBILITY 650 #define delay(x) bcm2835_delay(x) 651 #define delayMicroseconds(x) bcm2835_delayMicroseconds(x) 652 #endif 653 654 #ifdef __cplusplus 655 extern "C" { 656 #endif 657 661 670 extern int bcm2835_init(void); 671 674 extern int bcm2835_close(void); 675 682 extern void bcm2835_set_debug(uint8_t debug); 683 685 691 698 extern uint32_t bcm2835_peri_read(volatile uint32_t* paddr); 699 700 707 extern uint32_t bcm2835_peri_read_nb(volatile uint32_t* paddr); 708 709 716 extern void bcm2835_peri_write(volatile uint32_t* paddr, uint32_t value); 717 724 extern void bcm2835_peri_write_nb(volatile uint32_t* paddr, uint32_t value); 725 737 extern void bcm2835_peri_set_bits(volatile uint32_t* paddr, uint32_t value, uint32_t mask); 739 744 749 extern void bcm2835_gpio_fsel(uint8_t pin, uint8_t mode); 750 755 extern void bcm2835_gpio_set(uint8_t pin); 756 761 extern void bcm2835_gpio_clr(uint8_t pin); 762 767 extern void bcm2835_gpio_set_multi(uint32_t mask); 768 773 extern void bcm2835_gpio_clr_multi(uint32_t mask); 774 780 extern uint8_t bcm2835_gpio_lev(uint8_t pin); 781 789 extern uint8_t bcm2835_gpio_eds(uint8_t pin); 790 795 extern void bcm2835_gpio_set_eds(uint8_t pin); 796 804 extern void bcm2835_gpio_ren(uint8_t pin); 805 808 extern void bcm2835_gpio_clr_ren(uint8_t pin); 809 817 extern void bcm2835_gpio_fen(uint8_t pin); 818 821 extern void bcm2835_gpio_clr_fen(uint8_t pin); 822 826 extern void bcm2835_gpio_hen(uint8_t pin); 827 830 extern void bcm2835_gpio_clr_hen(uint8_t pin); 831 835 extern void bcm2835_gpio_len(uint8_t pin); 836 839 extern void bcm2835_gpio_clr_len(uint8_t pin); 840 846 extern void bcm2835_gpio_aren(uint8_t pin); 847 850 extern void bcm2835_gpio_clr_aren(uint8_t pin); 851 857 extern void bcm2835_gpio_afen(uint8_t pin); 858 861 extern void bcm2835_gpio_clr_afen(uint8_t pin); 862 868 extern void bcm2835_gpio_pud(uint8_t pud); 869 875 extern void bcm2835_gpio_pudclk(uint8_t pin, uint8_t on); 876 880 extern uint32_t bcm2835_gpio_pad(uint8_t group); 881 885 extern void bcm2835_gpio_set_pad(uint8_t group, uint32_t control); 886 896 extern void bcm2835_delay (unsigned int millis); 897 910 extern void bcm2835_delayMicroseconds (uint64_t micros); 911 915 extern void bcm2835_gpio_write(uint8_t pin, uint8_t on); 916 920 extern void bcm2835_gpio_write_multi(uint32_t mask, uint8_t on); 921 925 extern void bcm2835_gpio_write_mask(uint32_t value, uint32_t mask); 926 931 extern void bcm2835_gpio_set_pud(uint8_t pin, uint8_t pud); 932 934 939 946 extern void bcm2835_spi_begin(void); 947 951 extern void bcm2835_spi_end(void); 952 958 extern void bcm2835_spi_setBitOrder(uint8_t order); 959 964 extern void bcm2835_spi_setClockDivider(uint16_t divider); 965 970 extern void bcm2835_spi_setDataMode(uint8_t mode); 971 977 extern void bcm2835_spi_chipSelect(uint8_t cs); 978 986 extern void bcm2835_spi_setChipSelectPolarity(uint8_t cs, uint8_t active); 987 997 extern uint8_t bcm2835_spi_transfer(uint8_t value); 998 1009 extern void bcm2835_spi_transfernb(char* tbuf, char* rbuf, uint32_t len); 1010 1017 extern void bcm2835_spi_transfern(char* buf, uint32_t len); 1018 1024 extern void bcm2835_spi_writenb(char* buf, uint32_t len); 1025 1027 1032 1039 extern void bcm2835_i2c_begin(void); 1040 1044 extern void bcm2835_i2c_end(void); 1045 1048 extern void bcm2835_i2c_setSlaveAddress(uint8_t addr); 1049 1053 extern void bcm2835_i2c_setClockDivider(uint16_t divider); 1054 1060 extern void bcm2835_i2c_set_baudrate(uint32_t baudrate); 1061 1067 extern uint8_t bcm2835_i2c_write(const char * buf, uint32_t len); 1068 1074 extern uint8_t bcm2835_i2c_read(char* buf, uint32_t len); 1075 1089 extern uint8_t bcm2835_i2c_read_register_rs(char* regaddr, char* buf, uint32_t len); 1090 1092 1096 1099 uint64_t bcm2835_st_read(void); 1100 1104 extern void bcm2835_st_delay(uint64_t offset_micros, uint64_t micros); 1105 1107 1108 #ifdef __cplusplus 1109 } 1110 #endif 1111 1112 #endif // BCM2835_H