Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 11 Jun 2003 23:27:49 +0200
From:      Bernard Dugas <bernard.dugas@is-production.com>
To:        Malcolm Kay <malcolm.kay@internode.on.net>
Cc:        freebsd-questions@freebsd.org
Subject:   Re: how to talk to the serial and parallel ports through a C
Message-ID:  <3EE79ED4.93DB9224@is-production.com>
References:  <20030609175804.043AA37B405@hub.freebsd.org> <3EE65F83.9A1890FA@is-production.com> <200306112123.47587.malcolm.kay@internode.on.net>

next in thread | previous in thread | raw e-mail | index | archive | help
Hello,

Malcolm Kay a écrit :
> The i/o space in FreeBSD is normally reserved for management
> by the kernel. In my opinion this makes it a real hosted OS rather
> than some mickey mouse thing.

This is why i'm using FreeBSD :-)

> However if a process run by root opens /dev/io then while it is
> held open the process can make direct access to i/o ports. Nice
> for experimenting with software, but for production it is better to
> write a kernel module for special driver requirements.
> 
> Are you sure the standard serial driver interfaces are not suitable
> for your needs -- they are very flexible.

Not sure, but i'm using the serial port only to read and write
electrical values, to command power relays. On mickey systems, serial
drivers are far to complicated for that.

Did anybody write a simple kernel module doing just that ?

#include <sys/io.h>
#include <stdio.h>

void getComValues(int adr,unsigned char *p_TxdS3,
unsigned char *p_DtrS4,unsigned char *p_RtsS7,
unsigned char *p_CtsE8,unsigned char *p_DsrE6,
unsigned char *p_RiE9,unsigned char *p_DcdE1)
{
  unsigned char val3,val4,val6;

  val3=inb(adr+3);
  *p_TxdS3=(val3&64)>>6 ; //récupère le bit 6

  val4=inb(adr+4);
  *p_DtrS4=val4&1 ;       //récupère le bit 0
  *p_RtsS7=(val4&2)>>1 ;  //récupère le bit 1

  val6=inb(adr+6);
  *p_CtsE8=(val6&16)>>4 ;  //récupère le bit 4
  *p_DsrE6=(val6&32)>>5 ;  //récupère le bit 5
  *p_RiE9=(val6&64)>>6 ;   //récupère le bit 6
  *p_DcdE1=(val6&128)>>7 ; //récupère le bit 7

}

void setComValues(int adr,unsigned char c_TxdS3,unsigned char TxdS3,
unsigned char c_DtrS4, unsigned char DtrS4,
unsigned char c_RtsS7, unsigned char RtsS7)
{
unsigned char pmask3,pmask4,nmask3,nmask4,nnTxdS3,nnDtrS4,nnRtsS7;
// Quand c_x est faux, la variable x n'est pas modifiée.
//Le OU du pmask est utile quand x est à 1
//Le ET du nmask est utile quand x est à 0
//On fait les 2 opérations à la fois pour éviter une combinatoire
//conditionnelle.
//Il faut formatter x au cas où il serait strictment supérieur
//à 1
//Attention ! ~1=254 alors que !1=0
if(c_TxdS3)
{
nnTxdS3=!TxdS3;
TxdS3=!nnTxdS3;

//décalage de TxdS3 en bit 6
pmask3=TxdS3<<6;
nmask3=~(nnTxdS3<<6);

//on modifie seulement le(s) bit(s) modifié(s) de la valeur
//lue immédiatement avant, pour limiter les risques de modification
//intermédiaire.
outb(((inb(adr+3)|pmask3)&nmask3),adr+3);
}
//on ne touche pas au registre +3 si c_TxdS3 est faux
if(c_DtrS4 || c_RtsS7)
{
if(c_DtrS4)
{
nnDtrS4=!DtrS4;
DtrS4=!nnDtrS4;
}
else
{
//les 2 variables à 0 ne modifient aucun masque
nnDtrS4=0;
DtrS4=0;
}

if(c_RtsS7)
{
nnRtsS7=!RtsS7;
RtsS7=!nnRtsS7;
}
else
{
//les 2 variables à 0 ne modifient aucun masque
RtsS7=0;
nnRtsS7=0;
}

//décalage de RtsS7 en bit 1
pmask4=DtrS4|(RtsS7<<1);
nmask4=~(nnDtrS4|(nnRtsS7<<1));

//on modifie seulement le(s) bit(s) modifié(s) de la valeur
//lue immédiatement avant, pour limiter les risques de modification
//intermédiaire.
outb(((inb(adr+4)|pmask4)&nmask4),adr+4);
}
}


Best regards,
-- 

 __________ Bernard DUGAS ________________________________________
|                                                                 |
|  Technoparc Pays de Gex  mailto:bernard.dugas@is-production.com |
|  30 Rue Auguste Piccard           Tel.: +33 450 205 105         |
| FR 01630 St Genis Pouilly         Fax : +33 450 205 106         |
|_________________________________________________________________|




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?3EE79ED4.93DB9224>