Reputation: 391
I am writing a network driver that should send packets to an Arduino using Serial communication. This is a homework assignment and this is only for educational purposes. Please, take that into account before suggesting that everything could be accomplished in user space.
This answer stated about filp_open
and filp_close
, and I thought that I could use them to open /dev/ttyACMx
or /dev/ttyUSBx
, but I read somewhere else that it is not a good idea to use I/O file operations inside the kernel, so I am looking for a better approach.
I have also read about outb
and inb
, but I have not found any way to get the port
number argument required for both functions. I have tried using lsusb and writing to some of those 0xXX
endpoint addresses but it did not work.
This is my transmit function, where I want to, instead of printing, writing to a serial port. My aux variable is just a union
that contains two members: struct sk_buff skb
and unsigned char bytes[sizeof(struct sk_buff)]
. But at this point in the assignment, I just want to send the contents of skb.data
to my Arduino serial.
aux = skb;
while(aux != NULL) {
p.skb = *aux;
for(i = 0; i < p.skb.len; i++) {
if(i == 0) {
printk(KERN_INFO "\\%02x", p.skb.data[i]);
}
else {
printk(KERN_CONT "\\%02x", p.skb.data[i]);
}
}
aux = aux->next;
}
And this is Arduino code is the following:
void setup() {
Serial.begin(9600);
Serial.println("Start");
}
void loop() {
while(Serial.available() > 0)
Serial.print(Serial.read());
Serial.println();
}
It is simple as that. I want to read the content of a packet inside my Arduino. How can I write in a /dev/ttyACMx
or /dev/ttyUSBx
port inside my network driver?
Upvotes: 1
Views: 1063
Reputation: 71
The reason why file IO is not recommended: file IO are blocking the file at the moment. Since you are opening the file in kernelspace, that would be a bad idea. Imagine that another process would like to open the same file.
On the otherhand, if you realy need to use file operations, filp_open / filp_close are the ones to use.
If your assignment doesn't specify what to use, please use the memory address to write to. It takes a lot of work (compared to file IO) to get your write/read operation to work, but it's a better approach. You don't block anything since you are writing to the address itself.
My advice: take some time, study your datasheet / memory map and write directly to the address. You will have to write a function for setting the direction of the register (write / read) and a function that reads the register or writes the register.
Upvotes: 1