All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.diozero.internal.board.tinkerboard.TinkerBoardMmapGpio Maven / Gradle / Ivy

There is a newer version: 1.4.0
Show newest version
package com.diozero.internal.board.tinkerboard;

/*
 * #%L
 * Organisation: diozero
 * Project:      Device I/O Zero - Core
 * Filename:     TinkerBoardMmapGpio.java  
 * 
 * This file is part of the diozero project. More information about this project
 * can be found at http://www.diozero.com/
 * %%
 * Copyright (C) 2016 - 2020 diozero
 * %%
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 * #L%
 */


import java.nio.ByteOrder;
import java.nio.IntBuffer;

import org.tinylog.Logger;

import com.diozero.api.DeviceMode;
import com.diozero.api.GpioPullUpDown;
import com.diozero.internal.board.chip.ChipMmapGpio;
import com.diozero.internal.provider.MmapGpioInterface;
import com.diozero.util.LibraryLoader;
import com.diozero.util.MmapBufferNative;
import com.diozero.util.MmapByteBuffer;
import com.diozero.util.SleepUtil;

/*
 * https://github.com/torvalds/linux/blob/master/arch/arm/boot/dts/rk3288.dtsi
 * https://chromium.googlesource.com/chromiumos/third_party/coreboot/+/chromeos-2013.04/src/soc/rockchip/rk3288/gpio.c
 */
public class TinkerBoardMmapGpio implements MmapGpioInterface {
	private static final String MEM_DEVICE = "/dev/mem";
	private static final int PMU_BASE		= 0xff730000;
	private static final int GPIO_BASE		= 0xff750000;
	private static final int GRF_BASE		= 0xff770000;
	private static final int GPIO_LENGTH	= 0x00010000;
	private static final int GPIO_CHANNEL	= 0x00020000;
	
	private static final int PAGE_SIZE = 4 * 0x400;
	
	private static final int UNKNOWN = -1;
	
	private static final int PMU_GPIO0C_IOMUX_INT_OFFSET = 0x008c / 4;
	private static final int GRF_GPIO5B_IOMUX_INT_OFFSET = 0x0050 / 4;
	private static final int GRF_GPIO5C_IOMUX_INT_OFFSET = 0x0054 / 4;
	private static final int GRF_GPIO6A_IOMUX_INT_OFFSET = 0x005c / 4;
	//private static final int GRF_GPIO6B_IOMUX_INT_OFFSET = 0x0060 / 4;
	//private static final int GRF_GPIO6C_IOMUX_INT_OFFSET = 0x0064 / 4;
	private static final int GRF_GPIO7A_IOMUX_INT_OFFSET = 0x006c / 4;
	private static final int GRF_GPIO7B_IOMUX_INT_OFFSET = 0x0070 / 4;
	private static final int GRF_GPIO7CL_IOMUX_INT_OFFSET = 0x0074 / 4;
	private static final int GRF_GPIO7CH_IOMUX_INT_OFFSET = 0x0078 / 4;
	private static final int GRF_GPIO8A_IOMUX_INT_OFFSET = 0x0080 / 4;
	private static final int GRF_GPIO8B_IOMUX_INT_OFFSET = 0x0084 / 4;

	private static final int PMU_GPIO0C_P_INT_OFFSET = 0x006c / 4;
	private static final int GRF_GPIO5B_P_INT_OFFSET = 0x0184 / 4;
	private static final int GRF_GPIO5C_P_INT_OFFSET = 0x0188 / 4;
	private static final int GRF_GPIO6A_P_INT_OFFSET = 0x0190 / 4;
	//private static final int GRF_GPIO6B_P_INT_OFFSET = 0x0194 / 4;
	//private static final int GRF_GPIO6C_P_INT_OFFSET = 0x0198 / 4;
	private static final int GRF_GPIO7A_P_INT_OFFSET = 0x01a0 / 4;
	private static final int GRF_GPIO7B_P_INT_OFFSET = 0x01a4 / 4;
	private static final int GRF_GPIO7C_P_INT_OFFSET = 0x01a8 / 4;
	private static final int GRF_GPIO8A_P_INT_OFFSET = 0x01b0 / 4;
	private static final int GRF_GPIO8B_P_INT_OFFSET = 0x01b4 / 4;
	
	private static final int GPIO_SWPORTA_DR_INT_OFFSET = 0x0000 / 4;
	private static final int GPIO_SWPORTA_DDR_INT_OFFSET = 0x0004 / 4;
	private static final int GPIO_EXT_PORTA_INT_OFFSET = 0x0050 / 4;
	private static final int MUX_FUNC_GPIO = 0;
	private static final int MUX_FUNC_PWM = 3;
	
	private GpioBank[] gpioBanks;
	private MmapByteBuffer pmuMmap;
	private IntBuffer pmuIntBuffer;
	private MmapByteBuffer grfMmap;
	private IntBuffer grfIntBuffer;
	
	@Override
	public synchronized void initialise() {
		if (gpioBanks == null) {
			gpioBanks = new GpioBank[9];
			for (int i=0; i0 ? GPIO_CHANNEL : 0));
			}
			pmuMmap = MmapBufferNative.createMmapBuffer(MEM_DEVICE, PMU_BASE, PAGE_SIZE);
			pmuIntBuffer = pmuMmap.getBuffer().order(ByteOrder.LITTLE_ENDIAN).asIntBuffer();
			grfMmap = MmapBufferNative.createMmapBuffer(MEM_DEVICE, GRF_BASE, PAGE_SIZE);
			grfIntBuffer = grfMmap.getBuffer().order(ByteOrder.LITTLE_ENDIAN).asIntBuffer();
		}
	}

	@Override
	public synchronized void close() {
		if (gpioBanks != null) {
			for (int i=0; i> (((gpio-4)%8)*4)) & mux_mask;
		} else {
			mux_func = (config_val >> ((gpio%8)*2)) & mux_mask;
		}

		DeviceMode mode;
		if (mux_func == MUX_FUNC_GPIO) {
			int bank;
			int shift;
			if (gpio < 24) {
				bank = gpio / 32;
				shift = gpio % 32;
			} else {
				bank = (gpio + 8) / 32;
				shift = (gpio + 8) % 32;
			}
			if ((gpioBanks[bank].gpioIntBuffer.get(GPIO_SWPORTA_DDR_INT_OFFSET) & 1<




© 2015 - 2024 Weber Informatics LLC | Privacy Policy