| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Device::BCM2835::Timer; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 1 |  |  | 1 |  | 27633 | use v5.12; | 
|  | 1 |  |  |  |  | 5 |  | 
|  | 1 |  |  |  |  | 55 |  | 
| 4 |  |  |  |  |  |  |  | 
| 5 | 1 |  |  | 1 |  | 5 | use Fcntl qw(:DEFAULT O_ASYNC O_DIRECT); | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 554 |  | 
| 6 | 1 |  |  | 1 |  | 1175 | use Sys::Mmap; | 
|  | 1 |  |  |  |  | 1748 |  | 
|  | 1 |  |  |  |  | 595 |  | 
| 7 |  |  |  |  |  |  |  | 
| 8 |  |  |  |  |  |  | =head1 NAME | 
| 9 |  |  |  |  |  |  |  | 
| 10 |  |  |  |  |  |  | Device::BCM2835::Timer - Access to Raspberry Pi's timer | 
| 11 |  |  |  |  |  |  |  | 
| 12 |  |  |  |  |  |  | =head1 VERSION | 
| 13 |  |  |  |  |  |  |  | 
| 14 |  |  |  |  |  |  | Version 1.01 | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | =cut | 
| 17 |  |  |  |  |  |  |  | 
| 18 |  |  |  |  |  |  | our $VERSION = '1.01'; | 
| 19 |  |  |  |  |  |  |  | 
| 20 |  |  |  |  |  |  | my $DEVMEMFH; | 
| 21 |  |  |  |  |  |  |  | 
| 22 |  |  |  |  |  |  | my $timer_CLO_reg; | 
| 23 |  |  |  |  |  |  | my $timer_CHI_reg; | 
| 24 |  |  |  |  |  |  |  | 
| 25 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 26 |  |  |  |  |  |  |  | 
| 27 |  |  |  |  |  |  | This module's C method returns the current value of the Raspberry Pi's internal timer. | 
| 28 |  |  |  |  |  |  |  | 
| 29 |  |  |  |  |  |  | use Device::BCM2835::Timer; | 
| 30 |  |  |  |  |  |  | say Device::BCM2835::Timer::timer(); | 
| 31 |  |  |  |  |  |  |  | 
| 32 |  |  |  |  |  |  | =head1 FUNCTIONS | 
| 33 |  |  |  |  |  |  |  | 
| 34 |  |  |  |  |  |  | =head2 timer() | 
| 35 |  |  |  |  |  |  |  | 
| 36 |  |  |  |  |  |  | This function returns the number of microseconds elapsed since the Raspberry Pi in hand was | 
| 37 |  |  |  |  |  |  | turned on. | 
| 38 |  |  |  |  |  |  |  | 
| 39 |  |  |  |  |  |  | The function gives the value of the 64 bit timer counter installed on the BCM2835 chip. | 
| 40 |  |  |  |  |  |  | It ossillates at 1 MHz, thus every tick corresponds to 1 microsecond. Internally, it is a | 
| 41 |  |  |  |  |  |  | pair of two 32 bit registers, which are read and added up correspondently in pure Perl. | 
| 42 |  |  |  |  |  |  |  | 
| 43 |  |  |  |  |  |  | =cut | 
| 44 |  |  |  |  |  |  |  | 
| 45 |  |  |  |  |  |  | my $_init_done = 0; | 
| 46 |  |  |  |  |  |  |  | 
| 47 |  |  |  |  |  |  | sub _init { | 
| 48 | 0 | 0 |  | 0 |  |  | unless ( | 
| 49 |  |  |  |  |  |  | sysopen($DEVMEMFH, "/dev/mem", O_RDWR|O_SYNC, 0666) | 
| 50 |  |  |  |  |  |  | ) { | 
| 51 | 0 |  |  |  |  |  | return; | 
| 52 |  |  |  |  |  |  | } | 
| 53 |  |  |  |  |  |  |  | 
| 54 | 0 | 0 | 0 |  |  |  | unless ( | 
| 55 |  |  |  |  |  |  | # Mmapping CLO and CHI timer registers, each 32 bits long. | 
| 56 |  |  |  |  |  |  | # See chapter 12 System Timer of the BCM2835 manual: | 
| 57 |  |  |  |  |  |  | # http://www.raspberrypi.org/wp-content/uploads/2012/02/BCM2835-ARM-Peripherals.pdf | 
| 58 |  |  |  |  |  |  | mmap($timer_CLO_reg, 4, PROT_READ|PROT_WRITE, MAP_SHARED, $DEVMEMFH, 0x20003004) && | 
| 59 |  |  |  |  |  |  | mmap($timer_CHI_reg, 4, PROT_READ|PROT_WRITE, MAP_SHARED, $DEVMEMFH, 0x20003008) | 
| 60 |  |  |  |  |  |  | ) { | 
| 61 | 0 |  |  |  |  |  | close($DEVMEMFH); | 
| 62 | 0 |  |  |  |  |  | return; | 
| 63 |  |  |  |  |  |  | } | 
| 64 |  |  |  |  |  |  |  | 
| 65 | 0 |  |  |  |  |  | $_init_done = 1; | 
| 66 |  |  |  |  |  |  | } | 
| 67 |  |  |  |  |  |  |  | 
| 68 |  |  |  |  |  |  | sub timer { | 
| 69 | 0 | 0 |  | 0 | 1 |  | _init() unless $_init_done; | 
| 70 |  |  |  |  |  |  |  | 
| 71 | 0 |  |  |  |  |  | my $timer_lo = unpack 'L', $timer_CLO_reg; | 
| 72 | 0 |  |  |  |  |  | my $timer_hi = unpack 'L', $timer_CHI_reg; | 
| 73 |  |  |  |  |  |  |  | 
| 74 | 0 |  |  |  |  |  | my $timer = $timer_lo + ($timer_hi << 32); | 
| 75 |  |  |  |  |  |  |  | 
| 76 | 0 |  |  |  |  |  | return $timer; | 
| 77 |  |  |  |  |  |  | } | 
| 78 |  |  |  |  |  |  |  | 
| 79 |  |  |  |  |  |  | =head1 AUTHOR | 
| 80 |  |  |  |  |  |  |  | 
| 81 |  |  |  |  |  |  | Andrew Shitov, C<<  >> | 
| 82 |  |  |  |  |  |  |  | 
| 83 |  |  |  |  |  |  | =head1 BUGS | 
| 84 |  |  |  |  |  |  |  | 
| 85 |  |  |  |  |  |  | Please report any bugs or feature requests to C, or through | 
| 86 |  |  |  |  |  |  | the web interface at L.  I will be notified, and then you'll | 
| 87 |  |  |  |  |  |  | automatically be notified of progress on your bug as I make changes. | 
| 88 |  |  |  |  |  |  |  | 
| 89 |  |  |  |  |  |  | =head1 SUPPORT | 
| 90 |  |  |  |  |  |  |  | 
| 91 |  |  |  |  |  |  | You can find documentation for this module with the perldoc command. | 
| 92 |  |  |  |  |  |  |  | 
| 93 |  |  |  |  |  |  | perldoc Device::BCM2835::Timer | 
| 94 |  |  |  |  |  |  |  | 
| 95 |  |  |  |  |  |  |  | 
| 96 |  |  |  |  |  |  | =head1 LICENSE AND COPYRIGHT | 
| 97 |  |  |  |  |  |  |  | 
| 98 |  |  |  |  |  |  | Copyright 2014 Andrew Shitov. | 
| 99 |  |  |  |  |  |  |  | 
| 100 |  |  |  |  |  |  | This program is free software; you can redistribute it and/or modify it | 
| 101 |  |  |  |  |  |  | under the terms of the the Artistic License (2.0). | 
| 102 |  |  |  |  |  |  |  | 
| 103 |  |  |  |  |  |  | Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER | 
| 104 |  |  |  |  |  |  | AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. | 
| 105 |  |  |  |  |  |  | THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR | 
| 106 |  |  |  |  |  |  | PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY | 
| 107 |  |  |  |  |  |  | YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR | 
| 108 |  |  |  |  |  |  | CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR | 
| 109 |  |  |  |  |  |  | CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, | 
| 110 |  |  |  |  |  |  | EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 
| 111 |  |  |  |  |  |  |  | 
| 112 |  |  |  |  |  |  | =cut | 
| 113 |  |  |  |  |  |  |  | 
| 114 |  |  |  |  |  |  | 1; |