Beagle_GPIO::Beagle_GPIO() { GPIO_PRINT( "Beagle_GPIO::Beagle_GPIO()" ); // Not initialized by default m_active = false; // Opening /dev/mem first GPIO_PRINT( "Opening /dev/mem" ); m_gpio_fd = open( "/dev/mem", O_RDWR | O_SYNC ); if ( m_gpio_fd < 0 ) { GPIO_ERROR( "Cannot open /dev/mem" ); return; } // Map Control Module m_controlModule = (unsigned long *)mmap( NULL, 0x1FFF, PROT_READ | PROT_WRITE, MAP_SHARED, m_gpio_fd, GPIO_Control_Module_Registers ); if ( m_controlModule == MAP_FAILED ) { GPIO_ERROR( "Control Module Mapping failed" ); return; } // Now mapping the GPIO registers for ( int i=0; i<4; ++i) { // Map a GPIO bank m_gpio[i] = (unsigned long *)mmap( NULL, 0xFFF, PROT_READ | PROT_WRITE, MAP_SHARED, m_gpio_fd, GPIO_Base[i] ); if ( m_gpio[i] == MAP_FAILED ) { GPIO_ERROR( "GPIO Mapping failed for GPIO Module " << i ); return; } } // Init complete and successfull m_active = true; GPIO_PRINT( "Beagle GPIO Initialized" ); }
/********************************************************************************************** * Functions visible to this file only ******************************************************************************************** */ static int bcmgpio_drvinit () { #if defined(__ECOS) if (!(gpio_sih = (void *)si_kattach(SI_OSH))) return -1; bcmgpio_fd = 1; si_gpiosetcore(gpio_sih); return 0; #else bcmgpio_fd = open("/dev/gpio", O_RDWR); if (bcmgpio_fd == -1) { GPIO_ERROR ("Failed to open /dev/gpio\n"); return -1; } return 0; #endif }
// Send SPI Buffer void Beagle_GPIO::sendSPIBuffer( unsigned long _buffer, int _size ) { gp_assert( m_spi_fd >= 0 ); gp_assert( _buffer > 0 ); gp_assert( _size > 0 ); m_spi_ioc_tr.tx_buf = _buffer; m_spi_ioc_tr.rx_buf = (unsigned long)(m_spi_buffer_rx); m_spi_ioc_tr.len = _size; m_spi_ioc_tr.delay_usecs = m_spi_delay; m_spi_ioc_tr.speed_hz = m_spi_speed; m_spi_ioc_tr.bits_per_word = m_spi_bits; if ( ioctl( m_spi_fd, SPI_IOC_MESSAGE(1), &m_spi_ioc_tr ) < 1 ) { GPIO_ERROR( "Cannot send SPI Buffer, size=" << std::dec << _size ); return; } }
int bcmgpio_strobe_start (int gpio_pin, bcmgpio_strobe_t *strobe_info) { unsigned long regout; int status; struct itimerspec its; assert ((gpio_pin >= 0) && (gpio_pin <= BCMGPIO_MAXINDEX)); if (! strobe_info->timer_module) { GPIO_ERROR ("bcmgpio_strobe: Invalid timer module ID\n"); return -1; } if (! bcmgpio_info[gpio_pin].connected) return -1; if (bcmgpio_info[gpio_pin].dirn == BCMGPIO_DIRN_IN) return -1; if (bcmgpio_info[gpio_pin].strobing) return 0; if ((status = bcm_timer_create (strobe_info->timer_module, &bcmgpio_info[gpio_pin].timer_id)) != 0) { bcmgpio_info[gpio_pin].timer_id = 0; GPIO_ERROR ("bcmgpio_strobe: Timer creation failed with error %d\n", status); return -1; } if ((status = bcm_timer_connect (bcmgpio_info[gpio_pin].timer_id, (bcm_timer_cb) bcmgpio_timercb, (int) gpio_pin)) != 0) { bcm_timer_delete (bcmgpio_info[gpio_pin].timer_id); bcmgpio_info[gpio_pin].timer_id = 0; GPIO_ERROR ("bcmgpio_strobe: Timer connect failed with error %d\n", status); return -1; } its.it_interval.tv_sec = 0; its.it_interval.tv_nsec = BCMGPIO_STRB_NS_TIME; its.it_value.tv_sec = 0; its.it_value.tv_nsec = BCMGPIO_STRB_NS_TIME; if ((status = bcm_timer_settime (bcmgpio_info[gpio_pin].timer_id, &its)) != 0) { bcm_timer_delete (bcmgpio_info[gpio_pin].timer_id); bcmgpio_info[gpio_pin].timer_id = 0; GPIO_ERROR ("bcmgpio_strobe: Timer set failed with error %d\n", status); return -1; } regout = bcmgpio_ioctl (BCMGPIO_REG_OUT, 0, 0); bcmgpio_info[gpio_pin].orig_state = regout & ((unsigned long) 1 << gpio_pin); bcmgpio_info[gpio_pin].strobe_period = strobe_info->strobe_period_in_ms / BCMGPIO_STRB_MS_TIME; bcmgpio_info[gpio_pin].on_time = (strobe_info->duty_percent * bcmgpio_info[gpio_pin].strobe_period) / 100; bcmgpio_info[gpio_pin].tot_strobes = strobe_info->num_strobes; bcmgpio_info[gpio_pin].strobe_count = 0; bcmgpio_info[gpio_pin].timer_count = 0; bcmgpio_info[gpio_pin].strobing = 1; bcmgpio_info[gpio_pin].strobe_done = strobe_info->strobe_done; if (bcmgpio_info[gpio_pin].strobe_done != NULL) *(bcmgpio_info[gpio_pin].strobe_done) = 0; return 0; }
static int bcmgpio_ioctl(int gpioreg, unsigned int mask , unsigned int val) { #if defined(__ECOS) int value; switch (gpioreg) { case BCMGPIO_REG_IN: value = si_gpioin(gpio_sih); break; case BCMGPIO_REG_OUT: value = si_gpioout(gpio_sih, mask, val,GPIO_APP_PRIORITY); break; case BCMGPIO_REG_OUTEN: value = si_gpioouten(gpio_sih, mask, val,GPIO_APP_PRIORITY); break; case BCMGPIO_REG_RESERVE: value = si_gpioreserve(gpio_sih, mask, GPIO_APP_PRIORITY); break; case BCMGPIO_REG_RELEASE: /* * releasing the gpio doesn't change the current * value on the GPIO last write value * persists till some one overwrites it */ value = si_gpiorelease(gpio_sih, mask, GPIO_APP_PRIORITY); break; default: GPIO_ERROR ("invalid gpioreg %d\n", gpioreg); value = -1; break; } return value; #else struct gpio_ioctl gpio; int type; gpio.val = val; gpio.mask = mask; switch (gpioreg) { case BCMGPIO_REG_IN: type = GPIO_IOC_IN; break; case BCMGPIO_REG_OUT: type = GPIO_IOC_OUT; break; case BCMGPIO_REG_OUTEN: type = GPIO_IOC_OUTEN; break; case BCMGPIO_REG_RESERVE: type = GPIO_IOC_RESERVE; break; case BCMGPIO_REG_RELEASE: type = GPIO_IOC_RELEASE; break; default: GPIO_ERROR ("invalid gpioreg %d\n", gpioreg); return -1; } if (ioctl(bcmgpio_fd, type, &gpio) < 0) { GPIO_ERROR ("invalid gpioreg %d\n", gpioreg); return -1; } return (gpio.val); #endif }
// Open SPI Channel void Beagle_GPIO::openSPI( unsigned char _mode, unsigned char _bits, unsigned long _speed, unsigned short _delay ) { GPIO_PRINT( "Opening SPI Device" ); m_spi_fd = open( spi_device, O_RDWR ); if ( m_spi_fd < 0 ) { GPIO_ERROR( "Error opening SPI Device" ); return; } int ret = 0; // Save settings m_spi_mode = _mode; m_spi_bits = _bits; m_spi_speed = _speed; m_spi_delay = _delay; m_spi_buffer_rx = new unsigned char[65536]; // SPI Mode ret = ioctl(m_spi_fd, SPI_IOC_WR_MODE, &m_spi_mode); if (ret == -1) { GPIO_ERROR( "Error setting SPI Mode"); return; } ret = ioctl(m_spi_fd, SPI_IOC_RD_MODE, &m_spi_mode); if (ret == -1) { GPIO_ERROR( "Error getting SPI Mode"); return; } // SPI Bits Per Word ret = ioctl(m_spi_fd, SPI_IOC_WR_BITS_PER_WORD, &m_spi_bits); if (ret == -1) { GPIO_ERROR( "Error setting SPI Bits Per Word"); return; } ret = ioctl(m_spi_fd, SPI_IOC_RD_BITS_PER_WORD, &m_spi_bits); if (ret == -1) { GPIO_ERROR( "Error getting SPI Bits Per Word"); return; } // SPI Max Speed ret = ioctl(m_spi_fd, SPI_IOC_WR_MAX_SPEED_HZ, &m_spi_speed); if (ret == -1) { GPIO_ERROR( "Error setting SPI Max Speed"); return; } ret = ioctl(m_spi_fd, SPI_IOC_RD_MAX_SPEED_HZ, &m_spi_speed); if (ret == -1) { GPIO_ERROR( "Error getting SPI Max Speed"); return; } GPIO_PRINT( "SPI Mode : " << std::hex << (int)(m_spi_mode) ); GPIO_PRINT( "SPI Bits Per Word : " << std::dec << (int)(m_spi_bits) ); GPIO_PRINT( "SPI Max Speed : " << std::dec << m_spi_speed ); GPIO_PRINT( "SPI Delay : " << std::dec << m_spi_delay ); GPIO_PRINT( "SPI Opened" ); }