C Libruary for BCM2835(BCM2835函数库头文件)

This is the header file bcm2835.h for the BCM2835 C library, providing support for interacting with the Broadcom BCM2835 chip used in Raspberry Pi. It contains definitions and function prototypes for GPIO, SPI, I2C, PWM, and system timer registers and functions. The header defines constants, structures, and enums for configuring and controlling the Raspberry Pi's peripherals." 112892654,10544933,Python网络编程:服务器架构详解,"['Python编程', '网络服务器', '异步I/O', '并发处理']
 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.13 2013/12/06 22:24:52 mikem Exp mikem $
8 //
310 
311 
312 
313 // Defines for BCM2835
314 #ifndef BCM2835_H
315 #define BCM2835_H
316 
317 #include <stdint.h>
318 
322 
323 
325 #define HIGH 0x1
326 
327 #define LOW 0x0
328 
330 #define BCM2835_CORE_CLK_HZ 250000000
331 
332 // Physical addresses for various peripheral register sets
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 // Defines for GPIO
392 // The BCM2835 has 54 GPIO pins.
393 // BCM2835 data sheet, Page 90 onwards.
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 {
439 
442 typedef enum
443 {
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 {
475 
486 typedef enum
487 {
505 
506  // RPi Version 2
524 
525  // RPi Version 2, new plug P5
530 
531 } RPiGPIOPin;
532 
533 // Defines for SPI
534 // GPIO register offsets from BCM2835_SPI0_BASE.
535 // Offsets into the SPI Peripheral block in bytes per 10.5 SPI Register Map
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 // Register masks for SPI0_CS
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 {
578 
581 typedef enum
582 {
588 
591 typedef enum
592 {
598 
605 typedef enum
606 {
625 
626 // Defines for I2C
627 // GPIO register offsets from BCM2835_BSC*_BASE.
628 // Offsets into the BSC Peripheral block in bytes per 3.1 BSC Register Map
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 // Register masks for BSC_C
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 // Register masks for BSC_S
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 {
672 
675 typedef enum
676 {
682 
683 // Defines for ST
684 // GPIO register offsets from BCM2835_ST_BASE.
685 // Offsets into the ST Peripheral block in bytes per 12.1 System Timer Registers
686 // The System Timer peripheral provides four 32-bit timer channels and a single 64-bit free running counter.
687 // BCM2835_ST_CLO is the System Timer Counter Lower bits register.
688 // The system timer free-running counter lower register is a read-only register that returns the current value
689 // of the lower 32-bits of the free running counter.
690 // BCM2835_ST_CHI is the System Timer Counter Upper bits register.
691 // The system timer free-running counter upper register is a read-only register that returns the current value
692 // of the upper 32-bits of the free running counter.
693 #define BCM2835_ST_CS 0x0000
694 #define BCM2835_ST_CLO 0x0004
695 #define BCM2835_ST_CHI 0x0008
696 
697 
698 
699 
700 // Defines for PWM, word offsets (ie 4 byte multiples)
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 // Defines for PWM Clock, word offsets (ie 4 byte multiples)
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 // Historical name compatibility
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 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值