04. Writing to a serial device (UART)

Download & read the i.MX8M's Reference Manual, chapter 16.2. Ahem, not really all of it, just check the memory map / register definition for UTXD and UTS.

Next, we will enhance our character device to print the using IMX UART peripheral using a simple MMIO interface. Useful resources for mapping, reading & writing IO memory: https://www.kernel.org/doc/html/latest/driver-api/device-io.html (read the introduction + MMIO parts).

In principle, these are the steps you'll have to do:

  • create a function, let's say, myuart_put_char(unsigned char ch) – sends the specified character over the UART.
  • inside the module's init function, map the UART1's BASE memory using kernel's ioremap API (check either the Reference Manual or the device tree for the addresses!); you'll need to provide the base address + size (since the offset of the IO registers you'll access is pretty small, you can limit it to 0x1000);
  • implementation for our putchar:
    • first, you need to wait for the UTS TX ready flag (bit) to be set; use a while() busy loop, but call ''cpu_relax'' to temporarily lower power consumption (optional, but good practice); use readl() to read IO from registers;
    • afterwards, simply copy the character to the UART TXD register (use writel() for this);
  • finally, call your putchar function for every character in your buffer inside the character's device write callback!
  • test it by writing something in your char device, e.g.: echo hello uart > /dev/<mychardevicename>.
ass/laboratoare/04/tasks/04.txt · Last modified: 2023/07/22 08:45 by florin.stancu
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0