1

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?

15
  • Neither File IO nor serial communication should happen directly in kernel mode. Commented Nov 6, 2018 at 14:19
  • I get why file I/O should not happen directly in the kernel, but can you elaborate on why I should avoid serial communication in kernel space? Commented Nov 6, 2018 at 15:09
  • in Arduino code: use Serial.write(Serial.read); and what for is the endless println()? Commented Nov 6, 2018 at 18:39
  • @Juraj this is not the problem of my question. I just showed an example of how I want to read the data in the Arduino. My question is how to write on the serial in kernel space. Commented Nov 6, 2018 at 18:59
  • 1
    Forget about using outb and inb (or readb and writeb). That is only relevant for serial ports implemented by UARTs in port I/O (or memory-mapped I/O) space. Besides, the I/O addresses are probably already in use by a serial UART driver. From your question, it sounds like you want to access the Arduino board via some sort of USB serial device, but hooked into the kernel's network layer. Commented Nov 9, 2018 at 11:54

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Should I use the outb and inb functions to write directly to the memory addresses? If so, how can I get the memory address that I should use to write? This answer for example, cites the address 0x3F8. How can I get the memory address of my Arduino?
It's not an answer. He needs actually a driver that proxies UART to the network layer/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.