JNI - Java Native Interface
(EN google-translate)
(PL google-translate)
An einem konkreten Beispiel wird gezeigt, wie der Weg aussieht von einem C-Programm, das Konsolenbefehle ausführt zu einem Java-Programm, das eine Native-Funktion aufruft.
avrdude -c ponyser -p m32 -P /dev/ttyS0 -v
Code 0-1: Konsolenbefehl.
Der Konsolenbefehl liefert beispielsweise folgende Ausgabe, wenn ein Mikrocontroller richtig verbunden ist:
avrdude: Version 5.5, compiled on Jan 14 2009 at 06:21:50
Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
System wide configuration file is "/etc/avrdude.conf"
User configuration file is "/root/.avrduderc"
User configuration file does not exist or is not a regular file, skipping
Using Port : /dev/ttyS0
Using Programmer : ponyser
AVR Part : ATMEGA32
Chip Erase delay : 9000 us
PAGEL : PD7
BS2 : PA0
RESET disposition : dedicated
RETRY pulse : SCK
serial program mode : yes
parallel program mode : yes
Timeout : 200
StabDelay : 100
CmdexeDelay : 25
SyncLoops : 32
ByteDelay : 0
PollIndex : 3
PollValue : 0x53
Memory Detail :
Block Poll Page Polled
Memory Type Mode Delay Size Indx Paged Size Size #Pages MinW MaxW ReadBack
----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
eeprom 4 10 64 0 no 1024 4 0 9000 9000 0xff 0xff
flash 33 6 64 0 yes 32768 128 256 4500 4500 0xff 0xff
lfuse 0 0 0 0 no 1 0 0 2000 2000 0x00 0x00
hfuse 0 0 0 0 no 1 0 0 2000 2000 0x00 0x00
lock 0 0 0 0 no 1 0 0 2000 2000 0x00 0x00
signature 0 0 0 0 no 3 0 0 0 0 0x00 0x00
calibration 0 0 0 0 no 4 0 0 0 0 0x00 0x00
Programmer Type : SERBB
Description : design ponyprog serial, reset=!txd sck=rts mosi=dtr miso=cts
avrdude: AVR device initialized and ready to accept instructions
Reading | ################################################## | 100% 0.00s
avrdude: Device signature = 0x1e9502
avrdude: safemode: lfuse reads as FD
avrdude: safemode: hfuse reads as D9
avrdude: safemode: lfuse reads as FD
avrdude: safemode: hfuse reads as D9
avrdude: safemode: Fuses OK
avrdude done. Thank you.
Code 0-2: Konsolenausgabe zu obigem Befehl.
Durch folgende Ergänzung ist es möglich die Ausgabe auf dem Standardkanal in die Datei out.txt umzuleiten und die Ausgabe auf dem Fehlerkanal in die Datei err.txt. Diese Technik nennt man "pipen". Als absoluter Pfad wird /mnt-system gewählt:
avrdude -c ponyser -p m32 -P /dev/ttyS0 -v > /mnt-system/out.txt 2> /mnt-system/err.txt
Code 0-3: Konsolenbefehl mit Umleitung der Ausgaben in die Dateien out.txt und err.txt:
Wenn ein Mikrocontroller richtig angeschlossen ist, findet sich in den Dateien z.B. folgendes:
avrdude: safemode: lfuse reads as FD avrdude: safemode: hfuse reads as D9 avrdude: safemode: lfuse reads as FD avrdude: safemode: hfuse reads as D9
Code 0-4: out.txt bei korrekt verbundenem Mikrocontroller.
avrdude: Version 5.5, compiled on Jan 14 2009 at 06:21:50
Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
System wide configuration file is "/etc/avrdude.conf"
User configuration file is "/root/.avrduderc"
User configuration file does not exist or is not a regular file, skipping
Using Port : /dev/ttyS0
Using Programmer : ponyser
AVR Part : ATMEGA32
Chip Erase delay : 9000 us
PAGEL : PD7
BS2 : PA0
RESET disposition : dedicated
RETRY pulse : SCK
serial program mode : yes
parallel program mode : yes
Timeout : 200
StabDelay : 100
CmdexeDelay : 25
SyncLoops : 32
ByteDelay : 0
PollIndex : 3
PollValue : 0x53
Memory Detail :
Block Poll Page Polled
Memory Type Mode Delay Size Indx Paged Size Size #Pages MinW MaxW ReadBack
----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
eeprom 4 10 64 0 no 1024 4 0 9000 9000 0xff 0xff
flash 33 6 64 0 yes 32768 128 256 4500 4500 0xff 0xff
lfuse 0 0 0 0 no 1 0 0 2000 2000 0x00 0x00
hfuse 0 0 0 0 no 1 0 0 2000 2000 0x00 0x00
lock 0 0 0 0 no 1 0 0 2000 2000 0x00 0x00
signature 0 0 0 0 no 3 0 0 0 0 0x00 0x00
calibration 0 0 0 0 no 4 0 0 0 0 0x00 0x00
Programmer Type : SERBB
Description : design ponyprog serial, reset=!txd sck=rts mosi=dtr miso=cts
avrdude: AVR device initialized and ready to accept instructions
Reading | ################################################## | 100% 0.00s
avrdude: Device signature = 0x1e9502
avrdude: safemode: Fuses OK
avrdude done. Thank you.
Code 0-5: err.txt bei korrekt verbundenem Mikrocontroller.
Wird die Versorgungsspannung des Mikrocontrollers unterbrochen, oder das Programmiergerät entfernt, liefert der gleiche Aufruf folgendes:
Code 0-6: out.txt bei NICHT korrekt verbundenem Mikrocontroller. (Datei ist leer.)
avrdude: Version 5.5, compiled on Jan 14 2009 at 06:21:50
Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
System wide configuration file is "/etc/avrdude.conf"
User configuration file is "/root/.avrduderc"
User configuration file does not exist or is not a regular file, skipping
Using Port : /dev/ttyS0
Using Programmer : ponyser
AVR Part : ATMEGA32
Chip Erase delay : 9000 us
PAGEL : PD7
BS2 : PA0
RESET disposition : dedicated
RETRY pulse : SCK
serial program mode : yes
parallel program mode : yes
Timeout : 200
StabDelay : 100
CmdexeDelay : 25
SyncLoops : 32
ByteDelay : 0
PollIndex : 3
PollValue : 0x53
Memory Detail :
Block Poll Page Polled
Memory Type Mode Delay Size Indx Paged Size Size #Pages MinW MaxW ReadBack
----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
eeprom 4 10 64 0 no 1024 4 0 9000 9000 0xff 0xff
flash 33 6 64 0 yes 32768 128 256 4500 4500 0xff 0xff
lfuse 0 0 0 0 no 1 0 0 2000 2000 0x00 0x00
hfuse 0 0 0 0 no 1 0 0 2000 2000 0x00 0x00
lock 0 0 0 0 no 1 0 0 2000 2000 0x00 0x00
signature 0 0 0 0 no 3 0 0 0 0 0x00 0x00
calibration 0 0 0 0 no 4 0 0 0 0 0x00 0x00
Programmer Type : SERBB
Description : design ponyprog serial, reset=!txd sck=rts mosi=dtr miso=cts
avrdude: AVR device not responding
avrdude: initialization failed, rc=-1
Double check connections and try again, or use -F to override
this check.
avrdude done. Thank you.
Code 0-7: err.txt bei NICHT korrekt verbundenem Mikrocontroller.
Um also festzustellen, ob der Mikrocontroller korrekt verbunden ist, kann einfach überprüft werden, ob out.txt leer ist. Das folgende C-Programm nutzt genau dies, um eine Aussage zu treffen, ob der Mikrocontroller angeschlossen ist:
#include <stdlib.h>
#include <stdio.h>
int main()
{
FILE* f;
int i=0;
int c;
char *puffer;
int anz;
const char* befehl = "avrdude -c ponyser -p m32 -P /dev/ttyS0 -v > /mnt-system/out.txt 2> /mnt-system/err.txt";
//Eventuell zuvor vorhandene Dateien out.txt und err.txt löschen:
system("rm -f /mnt-system/out.txt");
system("rm -f /mnt-system/err.txt");
//Befehl aufrufen:
system(befehl);
//Zählen, wieviele Zeichen in out.txt vorhanden sind:
f = fopen("/mnt-system/out.txt","r");
c = fgetc(f);
anz=0;
while(c>=0)
{
//puffer[anz]=c;
c = fgetc(f);
anz++;
}
fclose(f);
printf("Die Datei out.txt enthaelt %i Zeichen.\n",anz);
system(befehl);
}
Code 0-8: avrangeschlossen.c - Einfaches C-Programm, um zu prüfen, ob ein Mikrocontroller korrekt angeschlossen ist.