// File: /workspace/github_cpp_makefile_star2_O2/mrlordkaj[P]i2cdevlib-pi/Arduino/wiringPi/wiringPi.c
// Function: wiringPiSetup
int wiringPiSetup (void)
{
  int   fd ;
  int   model, rev, mem, maker, overVolted ;

  if (wiringPiSetuped)
    return 0 ;

  wiringPiSetuped = TRUE ;

  if (getenv (ENV_DEBUG) != NULL)
    wiringPiDebug = TRUE ;

  if (getenv (ENV_CODES) != NULL)
    wiringPiReturnCodes = TRUE ;

  if (wiringPiDebug)
    printf ("wiringPi: wiringPiSetup called\n") ;

// Get the board ID information. We're not really using the information here,
//	but it will give us information like the GPIO layout scheme (2 variants
//	on the older 26-pin Pi's) and the GPIO peripheral base address.
//	and if we're running on a compute module, then wiringPi pin numbers
//	don't really many anything, so force native BCM mode anyway.

  piBoardId (&model, &rev, &mem, &maker, &overVolted) ;

  if ((model == PI_MODEL_CM) ||
      (model == PI_MODEL_CM3) ||
      (model == PI_MODEL_CM3P))
    wiringPiMode = WPI_MODE_GPIO ;
  else
    wiringPiMode = WPI_MODE_PINS ;

  /**/ if (piGpioLayout () == 1)	// A, B, Rev 1, 1.1
  {
     pinToGpio =  pinToGpioR1 ;
    physToGpio = physToGpioR1 ;
  }
  else 					// A2, B2, A+, B+, CM, Pi2, Pi3, Zero, Zero W, Zero 2 W
  {
     pinToGpio =  pinToGpioR2 ;
    physToGpio = physToGpioR2 ;
  }

// ...

  switch (model)
  {
    case PI_MODEL_A:	case PI_MODEL_B:
    case PI_MODEL_AP:	case PI_MODEL_BP:
    case PI_ALPHA:	case PI_MODEL_CM:
    case PI_MODEL_ZERO:	case PI_MODEL_ZERO_W:
      piGpioBase = GPIO_PERI_BASE_OLD ;
      piGpioPupOffset = GPPUD ;
      break ;

    case PI_MODEL_4B:
    case PI_MODEL_400:
    case PI_MODEL_CM4:
      piGpioBase = GPIO_PERI_BASE_2711 ;
      piGpioPupOffset = GPPUPPDN0 ;
      break ;

    default:
      piGpioBase = GPIO_PERI_BASE_2835 ;
      piGpioPupOffset = GPPUD ;
      break ;
  }

// Open the master /dev/ memory control device
// Device strategy: December 2016:
//	Try /dev/mem. If that fails, then
//	try /dev/gpiomem. If that fails then game over.

  if ((fd = open ("/dev/mem", O_RDWR | O_SYNC | O_CLOEXEC)) < 0)
  {
    if ((fd = open ("/dev/gpiomem", O_RDWR | O_SYNC | O_CLOEXEC) ) >= 0)	// We're using gpiomem
    {
      piGpioBase   = 0 ;
      usingGpioMem = TRUE ;
    }
    else
      return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: Unable to open /dev/mem or /dev/gpiomem: %s.\n"
	"  Aborting your program because if it can not access the GPIO\n"
	"  hardware then it most certianly won't work\n"
	"  Try running with sudo?\n", strerror (errno)) ;
  }

// Set the offsets into the memory interface.

  GPIO_PADS 	  = piGpioBase + 0x00100000 ;
  GPIO_CLOCK_BASE = piGpioBase + 0x00101000 ;
  GPIO_BASE	  = piGpioBase + 0x00200000 ;
  GPIO_TIMER	  = piGpioBase + 0x0000B000 ;
  GPIO_PWM	  = piGpioBase + 0x0020C000 ;

// Map the individual hardware components

//	GPIO:

  gpio = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_BASE) ;
  if (gpio == MAP_FAILED)
    return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (GPIO) failed: %s\n", strerror (errno)) ;

//	PWM

  pwm = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_PWM) ;
  if (pwm == MAP_FAILED)
    return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (PWM) failed: %s\n", strerror (errno)) ;

//	Clock control (needed for PWM)

  clk = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_CLOCK_BASE) ;
  if (clk == MAP_FAILED)
    return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (CLOCK) failed: %s\n", strerror (errno)) ;

//	The drive pads

  pads = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_PADS) ;
  if (pads == MAP_FAILED)
    return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (PADS) failed: %s\n", strerror (errno)) ;

//	The system timer

  timer = (uint32_t *)mmap(0, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, GPIO_TIMER) ;
  if (timer == MAP_FAILED)
    return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: mmap (TIMER) failed: %s\n", strerror (errno)) ;

// Set the timer to free-running, 1MHz.
//	0xF9 is 249, the timer divide is base clock / (divide+1)
//	so base clock is 250MHz / 250 = 1MHz.

  *(timer + TIMER_CONTROL) = 0x0000280 ;
  *(timer + TIMER_PRE_DIV) = 0x00000F9 ;
  timerIrqRaw = timer + TIMER_IRQ_RAW ;

// Export the base addresses for any external software that might need them

  _wiringPiGpio  = gpio ;
  _wiringPiPwm   = pwm ;
  _wiringPiClk   = clk ;
  _wiringPiPads  = pads ;
  _wiringPiTimer = timer ;

  initialiseEpoch () ;

  return 0 ;
}

// Assembly:
pushq %r14
pushq %rbx
subq $0x18, %rsp
xorl %r14d, %r14d
cmpb $0x0, 0x25dfc(%rip)     # 0x4a9b8
jne 0x24f4f
movb $0x1, 0x25def(%rip)     # 0x4a9b8
leaq 0xf05c(%rip), %rdi      # 0x33c2c
callq 0x1f9f0
testq %rax, %rax
je 0x24be7
movq 0x2437f(%rip), %rax     # 0x48f60
movl $0x1, (%rax)
leaq 0xf04d(%rip), %rdi      # 0x33c3b
callq 0x1f9f0
testq %rax, %rax
je 0x24c05
movq 0x243b1(%rip), %rax     # 0x48fb0
movl $0x1, (%rax)
movq 0x24354(%rip), %rax     # 0x48f60
cmpl $0x0, (%rax)
je 0x24c1d
leaq 0xf2f5(%rip), %rdi      # 0x33f0d
callq 0x1fd00
leaq 0x14(%rsp), %rbx
leaq 0x10(%rsp), %rsi
leaq 0xc(%rsp), %rdx
leaq 0x8(%rsp), %rcx
leaq 0x4(%rsp), %r8
movq %rbx, %rdi
callq 0x1f390
movl (%rbx), %ebx
cmpq $0x10, %rbx
ja 0x24c55
movl $0x10440, %eax          # imm = 0x10440
btl %ebx, %eax
jae 0x24c55
pushq $0x1
popq %rax
jmp 0x24c57
xorl %eax, %eax
movl %eax, 0x24dd7(%rip)     # 0x49a34
callq 0x20090
cmpl $0x1, %eax
leaq 0x24ee4(%rip), %rax     # 0x49b50
leaq 0x250dd(%rip), %rcx     # 0x49d50
cmoveq %rax, %rcx
leaq 0x24fd2(%rip), %rax     # 0x49c50
leaq 0x251cb(%rip), %rdx     # 0x49e50
cmoveq %rax, %rdx
movq %rcx, 0x25aa8(%rip)     # 0x4a738
movq %rdx, 0x25aa9(%rip)     # 0x4a740
cmpl $0x14, %ebx
ja 0x24cb2
leaq 0xe859(%rip), %rax      # 0x334fc
movl (%rax,%rbx,4), %eax
leaq 0xe8a3(%rip), %rcx      # 0x33550
movl (%rcx,%rbx,4), %ecx
jmp 0x24cba
pushq $0x25
popq %rcx
movl $0x3f000000, %eax       # imm = 0x3F000000
movl %eax, 0x25aa0(%rip)     # 0x4a760
movl %ecx, 0x25aaa(%rip)     # 0x4a770
leaq 0xef7d(%rip), %rdi      # 0x33c4a
movl $0x181002, %esi         # imm = 0x181002
xorl %eax, %eax
callq 0x1f540
movl %eax, %ebx
testl %eax, %eax
jns 0x24d0d
leaq 0xef6d(%rip), %rdi      # 0x33c53
movl $0x181002, %esi         # imm = 0x181002
xorl %eax, %eax
callq 0x1f540
testl %eax, %eax
js 0x24eed
movl %eax, %ebx
movl $0x0, 0x25a5a(%rip)     # 0x4a760
movb $0x1, 0x25caf(%rip)     # 0x4a9bc
movl $0x100000, %eax         # imm = 0x100000
addl 0x25a48(%rip), %eax     # 0x4a760
movl %eax, 0x25ca2(%rip)     # 0x4a9c0
movl $0x101000, %eax         # imm = 0x101000
addl 0x25a37(%rip), %eax     # 0x4a760
movl %eax, 0x25c95(%rip)     # 0x4a9c4
movl $0x200000, %eax         # imm = 0x200000
addl 0x25a26(%rip), %eax     # 0x4a760
movl %eax, 0x25c88(%rip)     # 0x4a9c8
movl $0xb000, %eax           # imm = 0xB000
addl 0x25a15(%rip), %eax     # 0x4a760
movl %eax, 0x25c7b(%rip)     # 0x4a9cc
movl $0x20c000, %eax         # imm = 0x20C000
addl 0x25a04(%rip), %eax     # 0x4a760
movl %eax, 0x25c6e(%rip)     # 0x4a9d0
movl 0x25c5f(%rip), %r9d     # 0x4a9c8
pushq $0x3
popq %rdx
pushq $0x1
popq %rcx
movl $0x1000, %esi           # imm = 0x1000
xorl %edi, %edi
movl %ebx, %r8d
callq 0x1ff20
movq %rax, 0x259cb(%rip)     # 0x4a750
cmpq $-0x1, %rax
je 0x24ec3
movl 0x25c3a(%rip), %r9d     # 0x4a9d0
pushq $0x3
popq %rdx
pushq $0x1
popq %rcx
movl $0x1000, %esi           # imm = 0x1000
xorl %edi, %edi
movl %ebx, %r8d
callq 0x1ff20
movq %rax, 0x259a6(%rip)     # 0x4a758
cmpq $-0x1, %rax
je 0x24ed8
movl 0x25c01(%rip), %r9d     # 0x4a9c4
pushq $0x3
popq %rdx
pushq $0x1
popq %rcx
movl $0x1000, %esi           # imm = 0x1000
xorl %edi, %edi
movl %ebx, %r8d
callq 0x1ff20
movq %rax, 0x25989(%rip)     # 0x4a768
cmpq $-0x1, %rax
je 0x24f02
movl 0x25bd0(%rip), %r9d     # 0x4a9c0
pushq $0x3
popq %rdx
pushq $0x1
popq %rcx
movl $0x1000, %esi           # imm = 0x1000
xorl %edi, %edi
movl %ebx, %r8d
callq 0x1ff20
movq %rax, 0x2593c(%rip)     # 0x4a748
cmpq $-0x1, %rax
je 0x24f17
movl 0x25baf(%rip), %r9d     # 0x4a9cc
xorl %r14d, %r14d
pushq $0x3
popq %rdx
pushq $0x1
popq %rcx
movl $0x1000, %esi           # imm = 0x1000
xorl %edi, %edi
movl %ebx, %r8d
callq 0x1ff20
movq %rax, 0x25b9c(%rip)     # 0x4a9d8
cmpq $-0x1, %rax
je 0x24f2c
movl $0x280, 0x408(%rax)     # imm = 0x280
movl $0xf9, 0x41c(%rax)
movq %rax, %rcx
addq $0x410, %rcx            # imm = 0x410
movq %rcx, 0x25b75(%rip)     # 0x4a9e0
movq 0x258de(%rip), %rcx     # 0x4a750
movq 0x240ef(%rip), %rdx     # 0x48f68
movq %rcx, (%rdx)
movq 0x258d5(%rip), %rcx     # 0x4a758
movq 0x240e6(%rip), %rdx     # 0x48f70
movq %rcx, (%rdx)
movq 0x258d4(%rip), %rcx     # 0x4a768
movq 0x240b5(%rip), %rdx     # 0x48f50
movq %rcx, (%rdx)
movq 0x258a3(%rip), %rcx     # 0x4a748
movq 0x240f4(%rip), %rdx     # 0x48fa0
movq %rcx, (%rdx)
movq 0x2410a(%rip), %rcx     # 0x48fc0
movq %rax, (%rcx)
callq 0x24f5a
jmp 0x24f4f
callq 0x1f040
movl (%rax), %edi
callq 0x1f230
leaq 0xee4b(%rip), %rsi      # 0x33d21
jmp 0x24f3f
callq 0x1f040
movl (%rax), %edi
callq 0x1f230
leaq 0xee5d(%rip), %rsi      # 0x33d48
jmp 0x24f3f
callq 0x1f040
movl (%rax), %edi
callq 0x1f230
leaq 0xed60(%rip), %rsi      # 0x33c60
jmp 0x24f3f
callq 0x1f040
movl (%rax), %edi
callq 0x1f230
leaq 0xee59(%rip), %rsi      # 0x33d6e
jmp 0x24f3f
callq 0x1f040
movl (%rax), %edi
callq 0x1f230
leaq 0xee6c(%rip), %rsi      # 0x33d96
jmp 0x24f3f
callq 0x1f040
movl (%rax), %edi
callq 0x1f230
leaq 0xee7e(%rip), %rsi      # 0x33dbd
xorl %edi, %edi
movq %rax, %rdx
xorl %eax, %eax
callq 0x1f380
pushq $-0x1
popq %r14
movl %r14d, %eax
addq $0x18, %rsp
popq %rbx
popq %r14
retq