📜 ⬆️ ⬇️

GPIO settings in Coremodule 920

It was required to be able to access GPIO on the Coremodule 920 from under Windows 7 x86. An application that needs access is written by me in Java.

Searching of decisions



It was decided to follow the path of least resistance and ask technical support, which was done. The answering machine wrote to me that everyone on vacation will be back in a week and a half. It was decided to wait for this time. I write again in about two weeks, I get an answer .

From the answer to my question it followed that they had a driver for C and only under Linux, which did not suit me.

Register Search

Open User Reference Manual . We look, we see: 0500-053F PCH GPIO Configuration Ports . We decided on a range, we look for specific addresses, and in the description a non-working link to PCH BD82QM67 .
')
We are looking for, we find: Intel® 6 Series Chipset and Intel® C200 Series Chipset Datasheet . Found even too much GPIO. We take a closer look and see that the GPIO needed is GP_LVL3 — GPIO Level for Input or Output 3 Register and GP_LVL2 — GPIO Level for Input or Output 2 Register . We look at the addresses: GPIOBASE + 48h and GPIOBASE + 38h, respectively. GPIOBASE - 0x500 . This is out of the range 0x500-0x53F , we will assume that there is another error in the User Reference Manual (after all, there was already an incorrect link).

Privileged Libraries

Addresses have been found, and you must still be able to contact them. Privileged access required. IOPort, Jioport were tried, something went wrong (they demanded that I be elevated (that is, privileged access)). Maybe I did something wrong, but it did not work out. Then I was advised to program UserPort. The description of the program is about working on Windows NT / XP.

Attempt # 1

We take UserPort.sys, we put in C: \ Windows \ System32 \ drivers. Run UserPort.exe. We see that there are entered address spaces. Add 0x500 - 0x53F. Click Stop-> Update-> Start, reboot the OS and ... blue screen.



Attempt # 2

The only difference between runs is UserPort. Boot in safe mode, remove UserPort.sys, reboot and ... the system started, everything is correct. We are looking for the cause. We return the driver to the place. We remove the address spaces that were entered by default, reboot and ... the system started. The reason is found: the same address spaces entered by default.

C ++ Library

A sample for working with code is attached to UserPort:

Sample
void outport(unsigned int portid, unsigned int value) { __asm mov edx, portid; __asm mov eax, value; __asm out dx, ax; } void outportb(unsigned int portid, unsigned char value) { __asm mov edx, portid __asm mov al, value __asm out dx, al } unsigned char inportb(unsigned int portid) { unsigned char value; __asm mov edx, portid __asm in al, dx __asm mov value, al return value; } unsigned int inport(unsigned int portid) { auto value = 0; __asm mov edx, portid __asm in ax, dx __asm mov value, eax return value; } 


Writing library code

Then the article How to make friends with Java and C ++ helped me . Part one and the program - libraries jnaerator-0.11-shaded, Bridj. Auto-generated header:

dllmain.cpp
 // dllmain.cpp:      DLL. #include "stdafx.h" BOOL APIENTRY DllMain( HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ) { switch (ul_reason_for_call) { case DLL_PROCESS_ATTACH: case DLL_THREAD_ATTACH: case DLL_THREAD_DETACH: case DLL_PROCESS_DETACH: break; } return TRUE; } 


File received via javah:

NativeGPIO.h
 #pragma once #include <jni.h> /* Header for class NativeGPIO */ #include "stdafx.h" /** * », ¤ ¤   ¤  (.dll) */ extern "C" { #define GPIO1 0x538 #define GPIO2 0x548 /**„ gpio1 1     integer*/ extern __declspec(dllexport) unsigned int readGPIO1(); /**„ gpio2 1     integer*/ extern __declspec(dllexport) unsigned int readGPIO2(); /**¤     */ extern __declspec(dllexport) unsigned char inportb(unsigned int portid); /**¤  int   */ extern __declspec(dllexport) unsigned int inport(unsigned int portid); /**¤  int   */ extern __declspec(dllexport) void outport(unsigned int portid, unsigned int value); /**¤     */ extern __declspec(dllexport) void outportb(unsigned int portid, unsigned char value); } 


File with the description of the functions:

NativeGPIO.cpp
 // dllmain.cpp:      DLL. // NativeGPIO.cpp:      DLL. // #include "stdafx.h" #include "NativeGPIO.h" #include <windows.h> void outport(unsigned int portid, unsigned int value) { __asm mov edx, portid; __asm mov eax, value; __asm out dx, ax; } void outportb(unsigned int portid, unsigned char value) { __asm mov edx, portid __asm mov al, value __asm out dx, al } unsigned char inportb(unsigned int portid) { unsigned char value; __asm mov edx, portid __asm in al, dx __asm mov value, al return value; } unsigned int inport(unsigned int portid) { auto value = 0; __asm mov edx, portid __asm in ax, dx __asm mov value, eax return value; } unsigned readGPIO2() { jint value = inportb(GPIO2); return value; } unsigned readGPIO1() { jint value = inportb(GPIO1); return value; } 


Methods inport / outport forwarding just in case. We compile into .dll, the library is ready.

We introduce in the program

We use JNAetrator and we get the following:

NativeGPIOLibrary
 import org.bridj.BridJ; import org.bridj.CRuntime; import org.bridj.ann.Library; /** * Created by Koksharov on 04.09.2015. * package = com.astra.app.neptun.applicationmanager * project = ApplicationManager */ @Library( "NativeGPIO" ) @org.bridj.ann.Runtime( CRuntime.class ) class NativeGPIOLibrary { { BridJ.register(); } public final int GPIO2 = ( int ) 1352; public final int GPIO1 = ( int ) 1336; /** * Original signature : <code>int readGPIO1()</code> * <i>native declaration : line 8</i> *     GPIO 1,    0x538 * * @return ,    1-4 */ native public int readGPIO1 (); /** * Original signature : <code>int readGPIO2()</code> * <i>native declaration : line 9</i> *     GPIO 2,    0x548 * * @return ,    5-8 */ native public int readGPIO2 (); /** * Original signature : <code>char inportb(unsigned int)</code> * <i>native declaration : line 10</i> * ,      ( ,  *    ) * * @return ,    (-) */ native public byte inportb ( int portid ); /** * Original signature : <code>int inport(unsigned int)</code> * <i>native declaration : line 11</i> * ,      ( ,  *    ) * * @return  ,    (-) */ native public int inport ( int portid ); /** * Original signature : <code>void outport(unsigned int, unsigned int)</code> * <i>native declaration : line 12</i> * ,      ( ,  *    ,        * ) */ native public void outport ( int portid, int value ); /** * Original signature : <code>void outportb(unsigned int, unsigned char)</code> * <i>native declaration : line 13</i> * ,      ( ,  *    ,        * ) */ native public void outportb ( int portid, byte value ); } 


Now let's write the reading in an infinite loop:

Usage example
  for (;;){ System.out.println(NativeGPIOLibrary.readGPIO1()); System.out.println(NativeGPIOLibrary.readGPIO2()); int GPIOValue1 = (NativeGPIOLibrary.inport(NativeGPIOLibrary.GPIO1)); int GPIOValue2 = (NativeGPIOLibrary.inport(NativeGPIOLibrary.GPIO2)); int GPIOValue_34 = (NativeGPIOLibrary.inport(NativeGPIOLibrary.GPIO_34)); int GPIOValue_44 = (NativeGPIOLibrary.inport(NativeGPIOLibrary.GPIO_44)); System.out.println(GPIOValue1 + " " + GPIOValue2 + " " + GPIOValue_34 + " " + GPIOValue_44); int gp1 = GPIOValue1;// & 0b1111110; int gp2 = GPIOValue2;// & 0b1111111; if ( (gp1 & 0b00001000) == 0 ){ System.out.println("button 1 1 pressed"); } if ( (gp1 & 0b00010000) == 0 ){ System.out.println("button 1 2 pressed"); } if ( (gp1 & 0b00100000) == 0 ){ System.out.println("button 1 3 pressed"); } if ( (gp2 & 0b00100000) == 0 ){ System.out.println("button 2 1 pressed"); } if ( (gp2 & 0b00010000) == 0 ){ System.out.println("button 2 2 pressed"); } if ( (gp2 & 0b01000000) == 0 ){ System.out.println("button 2 3 pressed"); } if ( (gp2 & 0b10000000) != 0 ){ System.out.println("button OFF pressed"); } try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } 


We start, we look: lack of privileges. Then I had to think about what was wrong, and then remember about 0x500-0x53F and 0x500 + 0x48. Expanded allowable space, earned.

Conclusion


In fact, almost at each stage much more time was spent than described in this article, so I hope the article will be useful to someone else.

Source: https://habr.com/ru/post/268523/


All Articles