diff options
| author | Uros Majstorovic <majstor@majstor.org> | 2026-01-08 01:01:17 +0100 |
|---|---|---|
| committer | Uros Majstorovic <majstor@majstor.org> | 2026-01-08 01:01:17 +0100 |
| commit | f4c5fb24507080506741b8ac2c6b027b401d02c8 (patch) | |
| tree | ca9ce3d7f5db62df2e7b3f2ffd7b65f8e35b98aa | |
| parent | b9a36b4bcc6ba144c8376172083e00bbdfa9ec78 (diff) | |
| -rw-r--r-- | yocto/README | 122 | ||||
| -rwxr-xr-x | yocto/esp32mod/Makefile | 15 | ||||
| -rwxr-xr-x | yocto/esp32mod/esp32.c | 535 | ||||
| -rwxr-xr-x | yocto/esp32mod/esp32.h | 16 | ||||
| -rwxr-xr-x | yocto/esp32mod/msgq.c | 50 | ||||
| -rwxr-xr-x | yocto/esp32mod/msgq.h | 15 | ||||
| -rwxr-xr-x | yocto/esp32tun/Makefile | 15 | ||||
| -rwxr-xr-x | yocto/esp32tun/esp32tun.c | 173 | ||||
| -rwxr-xr-x | yocto/overlays/mikroPhone-esp32_overlay.dts | 29 | ||||
| -rw-r--r-- | yocto/overlays/mikroPhone-panel_overlay.dts | 65 |
10 files changed, 962 insertions, 73 deletions
diff --git a/yocto/README b/yocto/README index 6c92a6e..8e63844 100644 --- a/yocto/README +++ b/yocto/README @@ -73,6 +73,7 @@ yocto image ----------- - clone meta-information +cd /build mkdir oe-core cd oe-core # branch: @@ -94,12 +95,16 @@ MACHINE ?= "verdin-imx8mp" # set debian packages PACKAGE_CLASSES ?= "package_deb" ... +# set distro +DISTRO ?= "mikrophone" +... # append ACCEPT_FSL_EULA = "1" - append mikrophone layer: vi conf/bblayers.conf # append to BBLAYERS variable: +BBLAYERS ?= " \ ... ${TOPDIR}/../layers/meta-mikrophone \ " @@ -111,9 +116,20 @@ bitbake -k mikrophone-image bitbake -k mikrophone-image -c populate_sdk - install sdk: -deploy/sdk/tdx-xwayland-glibc-x86_64-mikroPhone-Image-armv8a-verdin-imx8mp-toolchain-7.x.y.sh +deploy/sdk/mikrophone-glibc-x86_64-mikroPhone-Image-armv8a-verdin-imx8mp-toolchain-7.x.y.sh # install into: /build/tdx-xwayland/7.x.y +- prepare sdk for building kernel modules: +vi /build/tdx-xwayland/7.x.y/environment-setup-armv8a-tdx-linux +... +# append +export KERNEL_SRC="$SDKTARGETSYSROOT/lib/modules/6.x.y-$OECORE_SDK_VERSION-devel/build" +... + +. /build/tdx-xwayland/7.x.y/environment-setup-armv8a-tdx-linux +cd /build/tdx-xwayland/7.x.y/sysroots/armv8a-tdx-linux/usr/lib/modules/6.x.y-7.x.y-devel/build +make prepare + - machine.conf in: layers/meta-toradex-nxp/conf/machine/verdin-imx8mp.conf layers/meta-mikrophone/conf/machine/include/verdin-imx8mp.inc - mikrophone distro.conf in: layers/meta-mikrophone/conf/distro/mikrophone.conf @@ -144,6 +160,14 @@ setenv fdtfile imx8mp-verdin-wifi-dev.dtb boot +install image (Tezi) +-------------------- + +Verdin iMX8MP # editenv fdtfile +edit: imx8mp-verdin-wifi-dev.dtb +Verdin iMX8MP # boot + + u-boot ------ @@ -191,74 +215,72 @@ git clone -b toradex_6.6-2.1.x-imx git://git.toradex.com/linux-toradex.git cd linux-toradex git checkout 3493ccd66900420e2462daf4db187b315b50469e cp ../oe-core/build/deploy/images/verdin-imx8mp/kernel-config .config +# enable vivante gpu driver +# should be exactly the same as kernel module from NXP's linux-imx buit by oe-core +# diff -ur ../linux-imx/drivers/mxc/gpu-viv drivers/mxc/gpu-viv +vi .config +... +CONFIG_MXC_GPU_VIV=m +... # verify config make olddefconfig # copy yocto/kernel_lvds_freq.patch from mikroPhone repo # apply LVDS patch patch < ../kernel_lvds_freq.patch -# build kernel and dtb -make -j$(nproc) Image.gz 2>&1 | tee build.log -make freescale/imx8mp-verdin-wifi-dev.dtb +# build kernel (remove + from kernel version string) +make LOCALVERSION= -j$(nproc) Image.gz 2>&1 | tee build.log # ls ./arch/arm64/boot/Image.gz -# ls ./arch/arm64/boot/dts/freescale/imx8mp-verdin-wifi-dev.dtb # build kernel modules -make -j$(nproc) modules 2>&1 | tee build.log -mkdir modules -INSTALL_MOD_PATH=`pwd`/modules make modules_install -cd modules -tar -czf ../modules.tar.gz . -cd .. - +make LOCALVERSION= -j$(nproc) modules 2>&1 | tee build.log +# copy Module.symvers if there are some additional function exports: EXPORT_SYMBOL_GPL(...) +cp Module.symvers /build/tdx-xwayland/7.x.y/sysroots/armv8a-tdx-linux/usr/lib/modules/6.x.y-7.x.y-devel/build/ +make INSTALL_MOD_PATH=modules modules_install +# build dtb +cp ../oe-core/layers/meta-mikrophone/recipes-kernel/linux/linux-toradex/*.dts arch/arm64/boot/dts/freescale/ +cp ../oe-core/layers/meta-mikrophone/recipes-kernel/linux/linux-toradex/*.dtsi arch/arm64/boot/dts/freescale/ +make DTC_FLAGS="-@" freescale/imx8mp-verdin-nonwifi-mikrophone.dtb +make DTC_FLAGS="-@" freescale/imx8mp-verdin-wifi-mikrophone.dtb +# ls ./arch/arm64/boot/dts/freescale/*.dtb +# deploy +tar -czf modules.tar.gz modules/ +scp ./arch/arm64/boot/Image.gz root@mikrophone:/boot/ +scp ./arch/arm64/boot/dts/freescale/*.dtb root@mikrophone:/boot/ +scp modules.tar.gz root@mikrophone:. + + +esp32mod +-------- -overlay -------- +- setup environment: +. /build/tdx-xwayland/7.x.y/environment-setup-armv8a-tdx-linux -# toradex overlays (match branch to kernel branch) -# git clone -b toradex_6.6-2.1.x-imx git://git.toradex.com/device-tree-overlays.git -# copy yocto/overlays from mikroPhone repo -cd overlays -STAGING_KERNEL_DIR=../linux-toradex make mikroPhone-panel_overlay.dtbo +- build: +# copy yocto/esp32mod from mikroPhone repo +cd esp32mod +make cd .. -esp32d ------- +esp32tun +-------- - setup environment: . /build/tdx-xwayland/7.x.y/environment-setup-armv8a-tdx-linux - build: -# copy yocto/esp32d from mikroPhone repo -cd esp32d +# copy yocto/esp32tun from mikroPhone repo +cd esp32tun make cd .. -debian repository ------------------ +overlays +-------- -- install aptly and configure aptly: -apt-get install aptly gnupg1 gpgv1 -aptly # creates config file -vi ~/.aptly.conf -... - "gpgProvider": "internal", -... - "FileSystemPublishEndpoints": { - "mikrophone": { - "rootDir": "/build/repo", - "linkMethod": "copy", - "verifyMethod": "md5" - } - }, -... - -- generate gpg signing key: -gpg1 --gen-key -gpg1 --export --armor # signing key for apt-key add - -- create and publish repository: -aptly repo create -distribution=koshuta -component=main mikrophone -aptly repo add mikrophone /build/oe-core/build/deploy/deb -aptly publish repo mikrophone filesystem:mikrophone: -aptly publish update koshuta filesystem:mikrophone: +# toradex overlays (match branch to kernel branch) +# git clone -b toradex_6.6-2.1.x-imx git://git.toradex.com/device-tree-overlays.git +# copy yocto/overlays from mikroPhone repo +cd overlays +STAGING_KERNEL_DIR=../linux-toradex make mikroPhone-panel_overlay.dtbo +STAGING_KERNEL_DIR=../linux-toradex make mikroPhone-esp32_overlay.dtbo +cd .. diff --git a/yocto/esp32mod/Makefile b/yocto/esp32mod/Makefile new file mode 100755 index 0000000..31b58fb --- /dev/null +++ b/yocto/esp32mod/Makefile @@ -0,0 +1,15 @@ +obj-m := esp32mod.o +esp32mod-y := esp32.o msgq.o + +SRC := $(shell pwd) + +all: + $(MAKE) -C $(KERNEL_SRC) M=$(SRC) + +modules_install: + $(MAKE) -C $(KERNEL_SRC) M=$(SRC) modules_install + +clean: + rm -f *.o *~ core .depend .*.cmd *.ko *.mod.c *.mod + rm -f Module.markers Module.symvers modules.order + rm -rf .tmp_versions Modules.symvers diff --git a/yocto/esp32mod/esp32.c b/yocto/esp32mod/esp32.c new file mode 100755 index 0000000..8c7b5e4 --- /dev/null +++ b/yocto/esp32mod/esp32.c @@ -0,0 +1,535 @@ +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/jiffies.h> +#include <linux/delay.h> +#include <linux/spinlock.h> +#include <linux/wait.h> +#include <linux/completion.h> +#include <linux/kthread.h> +#include <linux/interrupt.h> +#include <linux/device.h> +#include <linux/cdev.h> +#include <linux/spi/spi.h> +#include <linux/gpio.h> + +#include <linux/of_device.h> +#include <linux/uaccess.h> + +#include "msgq.h" +#include "esp32.h" + +#define wait_event_interruptible_lock(wq_head, condition, lock) \ +({ \ + int __ret = 0; \ + if (!(condition)) \ + __ret = ___wait_event(wq_head, condition, TASK_INTERRUPTIBLE, 0, 0, \ + spin_unlock(&lock); \ + schedule(); \ + spin_lock(&lock)); \ + __ret; \ +}) + +/* has to be power of 2 */ +#define MSGQ_SIZE_SND 128 +#define MSGQ_SIZE_RCV 128 + +#define ESP32_SIZE_HDR 3 +#define ESP32_SIZE_BUF (ESP32_MTU + 4) + +static int esp32_open(struct inode *inode, struct file *file); +static int esp32_release(struct inode *inode, struct file *file); +static ssize_t esp32_read(struct file *file, char __user *buf, size_t len, loff_t *off); +static ssize_t esp32_write(struct file *file, const char __user *buf, size_t len, loff_t *off); + +static int esp32_probe(struct spi_device *spi); +static void esp32_remove(struct spi_device *spi); + +static DECLARE_WAIT_QUEUE_HEAD(xchg_wq); +static DECLARE_WAIT_QUEUE_HEAD(cts_wq); + +static MSGQueue snd_msgq; +static MSGQueue rcv_msgq[ESP32_MAX_MTYPE]; + +static struct spi_device *esp32_spi_dev; +static struct gpio_desc *gpio_rts, *gpio_cts; +static int irq_rts, irq_cts; +static int cts_val; +static struct task_struct *kthd; + +static dev_t dev; +static struct class *dev_class; +static struct cdev dev_cdev; +static struct file_operations dev_fops = { + .owner = THIS_MODULE, + .open = esp32_open, + .release = esp32_release, + .read = esp32_read, + .write = esp32_write, +}; + +static const struct of_device_id esp32_of_match[] = { + { + .compatible = "mikrophone,esp32", + }, + { /* sentinel */ } +}; +MODULE_DEVICE_TABLE(of, esp32_of_match); + +static const struct spi_device_id esp32_spi_ids[] = { + { "esp32", 0 }, + { /* sentinel */ } +}; +MODULE_DEVICE_TABLE(spi, esp32_spi_ids); + +static struct spi_driver esp32_spi_driver = { + .probe = esp32_probe, + .remove = esp32_remove, + .driver = { + .name = "esp32", + .of_match_table = of_match_ptr(esp32_of_match), + }, + .id_table = esp32_spi_ids, +}; + +static irqreturn_t rts_irq_handler(int irq, void *dev_id) { + wake_up(&xchg_wq); + + return IRQ_HANDLED; +} + +static irqreturn_t cts_irq_handler(int irq, void *dev_id) { + cts_val = 0; + wake_up(&cts_wq); + + return IRQ_HANDLED; +} + +/* esp32 dma workaraund */ +static inline unsigned int esp32_fix_len(unsigned int len) { + if (len < 8) { + return 8; + } else if (len % 4 != 0) { + return (len / 4 + 1) * 4; + } + return len; +} + +static inline int wait4cts(void) { + return wait_event_interruptible(cts_wq, !cts_val); +} + +static inline int spi_xchg(struct spi_transfer *spi_tr) { + cts_val = 1; + return spi_sync_transfer(esp32_spi_dev, spi_tr, 1); +} + +static int xchg_thd(void *arg) { + unsigned char *buffer, *_buffer, *buf_rx; + unsigned char mtype, mtype_flags; + int rv, wake_sndq, wake_rcvq, bfree; + struct spi_transfer spi_tr; + uint16_t len_tx, len_rx; + + _buffer = NULL; + memset(&spi_tr, 0, sizeof(spi_tr)); + + buf_rx = esp32_alloc(); + if (buf_rx == NULL) return -ENOMEM; + + while (!kthread_should_stop()) { + /* preallocate buffer outside of spin lock: GFP_KERNEL allocator can sleep */ + if (_buffer == NULL) { + _buffer = esp32_alloc(); + if (_buffer == NULL) return -ENOMEM; + _buffer[0] = 0; + _buffer[1] = 0; + _buffer[2] = 0; + } + + rv = 0; + wake_sndq = 0; + wake_rcvq = 0; + bfree = 1; + + spin_lock(&snd_msgq.lock); + do { + buffer = msgq_pop(&snd_msgq); + if (buffer && (msgq_len(&snd_msgq) == (msgq_size(&snd_msgq) - 1))) { + wake_sndq = 1; + } + if ((buffer == NULL) && (gpiod_get_value(gpio_rts) == 0)) { + buffer = _buffer; + _buffer = NULL; + } + if (buffer == NULL) { + rv = wait_event_interruptible_lock(xchg_wq, (msgq_len(&snd_msgq) != 0) || (gpiod_get_value(gpio_rts) == 0), snd_msgq.lock); + if (rv) break; + } + } while (buffer == NULL); + spin_unlock(&snd_msgq.lock); + + /* kthread_should_stop() might be true */ + if (rv) continue; + + if (wake_sndq) wake_up(&snd_msgq.wq); + + len_tx = (uint16_t)buffer[1] << 8; + len_tx |= (uint16_t)buffer[2] & 0xFF; + if (len_tx > ESP32_MTU) goto xchg_thd_fin; + + spi_tr.tx_buf = buffer; + spi_tr.rx_buf = buf_rx; + spi_tr.len = len_tx + ESP32_SIZE_HDR; + spi_tr.len = esp32_fix_len(spi_tr.len); + + /* wait for cts */ + rv = wait4cts(); + if (rv) goto xchg_thd_fin; + + /* nothing to send/receive */ + if ((buffer[0] == 0) && gpiod_get_value(gpio_rts)) goto xchg_thd_fin; + + rv = spi_xchg(&spi_tr); + if (rv) goto xchg_thd_fin; + + mtype = buf_rx[0] & ESP32_MTYPE_MASK; + mtype_flags = buf_rx[0] & ~ESP32_MTYPE_MASK; + if (mtype >= ESP32_MAX_MTYPE) goto xchg_thd_fin; + + if (mtype) { + memcpy(buffer, spi_tr.rx_buf, spi_tr.len); + + len_rx = (uint16_t)buffer[1] << 8; + len_rx |= (uint16_t)buffer[2] & 0xFF; + if (len_rx > ESP32_MTU) goto xchg_thd_fin; + + if (len_rx + ESP32_SIZE_HDR > spi_tr.len) { + spi_tr.tx_buf = NULL; + spi_tr.rx_buf = buffer + spi_tr.len; + spi_tr.len = len_rx + ESP32_SIZE_HDR - spi_tr.len; + spi_tr.len = esp32_fix_len(spi_tr.len); + + /* wait for cts, again */ + rv = wait4cts(); + if (rv) goto xchg_thd_fin; + + rv = spi_xchg(&spi_tr); + if (rv) goto xchg_thd_fin; + } + + spin_lock(&rcv_msgq[mtype].lock); + rv = msgq_push(&rcv_msgq[mtype], buffer); + if (!rv && (msgq_len(&rcv_msgq[mtype]) == 1)) { + wake_rcvq = 1; + } + spin_unlock(&rcv_msgq[mtype].lock); + + if (!rv) bfree = 0; + if (wake_rcvq) wake_up(&rcv_msgq[mtype].wq); + } + +xchg_thd_fin: + if (bfree) { + if (_buffer == NULL) { + _buffer = buffer; + _buffer[0] = 0; + _buffer[1] = 0; + _buffer[2] = 0; + } else { + esp32_free(buffer); + } + } + } + + esp32_free(_buffer); + esp32_free(buf_rx); + return 0; +} + +static int esp32_open(struct inode *inode, struct file *file) { + unsigned int major, minor; + + major = MAJOR(dev); + minor = MINOR(dev); + if (imajor(inode) != major) return -EINVAL; + if ((iminor(inode) < minor) || (iminor(inode) >= minor + ESP32_MAX_MTYPE)) return -EINVAL; + file->private_data = (void *)(iminor(inode) - minor); + + return 0; +} + +static int esp32_release(struct inode *inode, struct file *file) { + return 0; +} + +static ssize_t esp32_read(struct file *file, char __user *buf, size_t len, loff_t *off) { + unsigned char mtype; + unsigned char *buffer; + uint16_t msg_len; + unsigned long _rv; + int block, rv = 0; + + block = !(file->f_flags & O_NONBLOCK); + mtype = (unsigned char)(file->private_data); + rv = esp32_receive(mtype, block, &buffer); + if (rv) goto esp32_read_fin; + + msg_len = (uint16_t)buffer[1] << 8; + msg_len |= (uint16_t)buffer[2] & 0xFF; + if (len < msg_len) rv = -EINVAL; + if (rv) goto esp32_read_fin; + + _rv = copy_to_user(buf, buffer + ESP32_SIZE_HDR, msg_len); + if (_rv) rv = -EFAULT; + +esp32_read_fin: + if (buffer) esp32_free(buffer); + if (rv) return rv; + return msg_len; +} + +static ssize_t esp32_write(struct file *file, const char __user *buf, size_t len, loff_t *off) { + unsigned char mtype; + unsigned char *buffer; + unsigned long _rv; + int block, rv = 0; + + if (len > ESP32_MTU) return -EINVAL; + + buffer = esp32_alloc(); + if (buffer == NULL) return -ENOMEM; + + _rv = copy_from_user(buffer + ESP32_SIZE_HDR, buf, len); + if (_rv) rv = -EFAULT; + if (rv) { + esp32_free(buffer); + return rv; + } + + block = !(file->f_flags & O_NONBLOCK); + mtype = (unsigned char)(file->private_data); + buffer[0] = mtype; + buffer[1] = len >> 8; + buffer[2] = len & 0xFF; + + rv = esp32_send(buffer, block); + if (rv) return rv; + + return len; +} + +static int esp32_probe(struct spi_device *spi) { + dev_t _dev; + struct device *dev_ptr; + const char *fmt; + int i, rv, major, minor; + const struct of_device_id *match; + + match = of_match_device(of_match_ptr(esp32_of_match), &spi->dev); + if (match == NULL) return -ENODEV; + + esp32_spi_dev = spi; + + gpio_rts = gpiod_get(&spi->dev, "rts", GPIOD_IN); + if (IS_ERR(gpio_rts)) goto err_gpiod_get_rts; + + irq_rts = gpiod_to_irq(gpio_rts); + if (irq_rts < 0) goto err_gpiod_get_rts; + + gpio_cts = gpiod_get(&spi->dev, "cts", GPIOD_IN); + if (IS_ERR(gpio_cts) || gpiod_cansleep(gpio_rts)) goto err_gpiod_get_cts; + + irq_cts = gpiod_to_irq(gpio_cts); + if (irq_cts < 0) goto err_gpiod_get_cts; + + rv = request_irq(irq_rts, rts_irq_handler, IRQF_TRIGGER_FALLING, "esp32_rts", NULL); + if (rv < 0) goto err_request_irq_rts; + + rv = request_irq(irq_cts, cts_irq_handler, IRQF_TRIGGER_FALLING, "esp32_cts", NULL); + if (rv < 0) goto err_request_irq_cts; + + kthd = kthread_run(xchg_thd, NULL, "esp32"); + if (IS_ERR(kthd)) goto err_kthread_run; + + rv = alloc_chrdev_region(&dev, 0, ESP32_MAX_MTYPE, "esp32"); + if (rv) goto err_alloc_chrdev; + + cdev_init(&dev_cdev, &dev_fops); + + rv = cdev_add(&dev_cdev, dev, ESP32_MAX_MTYPE); + if (rv) goto err_cdev_add; + + dev_class = class_create("esp32"); + if (IS_ERR(dev_class)) goto err_class_create; + + major = MAJOR(dev); + minor = MINOR(dev); + for (i=0; i<ESP32_MAX_MTYPE; i++) { + _dev = MKDEV(major, minor + i); + fmt = i & ESP32_MTYPE_FLAG_BRIDGE ? "fe310.%d" : "esp32.%d"; + dev_ptr = device_create(dev_class, NULL, _dev, NULL, fmt, i & ~ESP32_MTYPE_FLAG_BRIDGE); + if (IS_ERR(dev_ptr)) goto err_device_create; + } + + return 0; + +err_device_create: + for (i--; i>=0; i--) { + _dev = MKDEV(major, minor + i); + device_destroy(dev_class, _dev); + } + class_destroy(dev_class); + +err_class_create: + cdev_del(&dev_cdev); + +err_cdev_add: + unregister_chrdev_region(dev, ESP32_MAX_MTYPE); + +err_alloc_chrdev: + kthread_stop(kthd); + +err_kthread_run: + free_irq(irq_cts, NULL); + +err_request_irq_cts: + free_irq(irq_rts, NULL); + +err_request_irq_rts: +err_gpiod_get_cts: + if (!IS_ERR(gpio_cts)) gpiod_put(gpio_rts); + +err_gpiod_get_rts: + if (!IS_ERR(gpio_rts)) gpiod_put(gpio_rts); + return -1; +} + +static void esp32_remove(struct spi_device *spi) { + unsigned char *buffer; + dev_t _dev; + int i, major, minor; + + major = MAJOR(dev); + minor = MINOR(dev); + for (i=0; i<ESP32_MAX_MTYPE; i++) { + _dev = MKDEV(major, minor + i); + device_destroy(dev_class, _dev); + } + class_destroy(dev_class); + cdev_del(&dev_cdev); + unregister_chrdev_region(dev, ESP32_MAX_MTYPE); + kthread_stop(kthd); + free_irq(irq_cts, NULL); + free_irq(irq_rts, NULL); + gpiod_put(gpio_cts); + gpiod_put(gpio_rts); + + /* flush remaining buffers */ + spin_lock(&snd_msgq.lock); + while ((buffer = msgq_pop(&snd_msgq))) { + esp32_free(buffer); + } + msgq_destroy(&snd_msgq); + spin_unlock(&snd_msgq.lock); + for (i=0; i<ESP32_MAX_MTYPE; i++) { + spin_lock(&rcv_msgq[i].lock); + while ((buffer = msgq_pop(&rcv_msgq[i]))) { + esp32_free(buffer); + } + msgq_destroy(&rcv_msgq[i]); + spin_unlock(&rcv_msgq[i].lock); + } +} + +static int __init esp32_init(void) { + int i, rv; + + rv = msgq_create(&snd_msgq, MSGQ_SIZE_SND); + if (rv) return rv; + + for (i=0; i<ESP32_MAX_MTYPE; i++) { + rv = msgq_create(&rcv_msgq[i], MSGQ_SIZE_RCV); + if (rv) goto esp32_init_err; + } + + rv = spi_register_driver(&esp32_spi_driver); + if (rv) goto esp32_init_err; + + return 0; + +esp32_init_err: + for (i--; i>=0; i--) { + msgq_destroy(&rcv_msgq[i]); + } + msgq_destroy(&snd_msgq); + return rv; +} + +static void __exit esp32_exit(void) { + spi_unregister_driver(&esp32_spi_driver); +} + +module_init(esp32_init); +module_exit(esp32_exit); + +MODULE_LICENSE("GPL"); +MODULE_AUTHOR("Uros Majstorovic <majstor@majstor.org>"); +MODULE_DESCRIPTION("esp32 SPI driver"); +MODULE_VERSION("1.0"); + +int esp32_send(unsigned char *buffer, int block) { + int rv, wake_xchg; + + spin_lock(&snd_msgq.lock); + do { + wake_xchg = 0; + rv = msgq_push(&snd_msgq, buffer); + if (!rv && (msgq_len(&snd_msgq) == 1)) { + wake_xchg = 1; + } + if (block && rv) { + int _rv; + + _rv = wait_event_interruptible_lock(snd_msgq.wq, msgq_len(&snd_msgq) != msgq_size(&snd_msgq), snd_msgq.lock); + if (_rv) { + rv = _rv; + break; + } + } + } while (block && rv); + spin_unlock(&snd_msgq.lock); + + if (wake_xchg) wake_up(&xchg_wq); + if (rv) esp32_free(buffer); + + return rv; +} + +int esp32_receive(unsigned char mtype, int block, unsigned char **buffer) { + int rv; + + *buffer = NULL; + if (mtype >= ESP32_MAX_MTYPE) return -EINVAL; + + rv = 0; + spin_lock(&rcv_msgq[mtype].lock); + *buffer = msgq_pop(&rcv_msgq[mtype]); + while (block && (*buffer == NULL)) { + rv = wait_event_interruptible_lock(rcv_msgq[mtype].wq, msgq_len(&rcv_msgq[mtype]) != 0, rcv_msgq[mtype].lock); + if (rv) break; + + *buffer = msgq_pop(&rcv_msgq[mtype]); + } + spin_unlock(&rcv_msgq[mtype].lock); + + if (!rv && (*buffer == NULL)) rv = -EAGAIN; + return rv; +} + +unsigned char *esp32_alloc(void) { + return kmalloc(ESP32_SIZE_BUF, GFP_KERNEL); +} + +void esp32_free(unsigned char *buffer) { + kfree(buffer); +} diff --git a/yocto/esp32mod/esp32.h b/yocto/esp32mod/esp32.h new file mode 100755 index 0000000..b00cc4f --- /dev/null +++ b/yocto/esp32mod/esp32.h @@ -0,0 +1,16 @@ +#define ESP32_MTU 1500 + +#define ESP32_MAX_MTYPE 16 + +#define ESP32_MTYPE_SLEEP 0x20 /* does not have net handler */ + +#define ESP32_MTYPE_FLAG_ONEW 0x80 +#define ESP32_MTYPE_FLAG_REPL 0x40 +#define ESP32_MTYPE_FLAG_BRIDGE 0x08 + +#define ESP32_MTYPE_MASK 0x0F + +int esp32_send(unsigned char *buffer, int block); +int esp32_receive(unsigned char msg_type, int block, unsigned char **buffer); +unsigned char *esp32_alloc(void); +void esp32_free(unsigned char *buffer);
\ No newline at end of file diff --git a/yocto/esp32mod/msgq.c b/yocto/esp32mod/msgq.c new file mode 100755 index 0000000..77ece4b --- /dev/null +++ b/yocto/esp32mod/msgq.c @@ -0,0 +1,50 @@ +#include <linux/module.h> + +#include "msgq.h" + +#define IDX_MASK(IDX, SIZE) ((IDX) & ((SIZE) - 1)) + +int msgq_create(MSGQueue *msgq, uint16_t size) { + unsigned char **array; + + array = kmalloc(size * sizeof(unsigned char *), GFP_KERNEL); + if (array == NULL) return -ENOMEM; + + msgq->idx_r = 0; + msgq->idx_w = 0; + msgq->size = size; + msgq->array = array; + spin_lock_init(&msgq->lock); + init_waitqueue_head(&msgq->wq); + + return 0; +} + +void msgq_destroy(MSGQueue *msgq) { + kfree(msgq->array); + msgq->idx_r = 0; + msgq->idx_w = 0; + msgq->size = 0; + msgq->array = NULL; +} + +uint16_t msgq_len(MSGQueue *msgq) { + return (uint16_t)(msgq->idx_w - msgq->idx_r); +} + +uint16_t msgq_size(MSGQueue *msgq) { + return msgq->size; +} + +int msgq_push(MSGQueue *msgq, unsigned char *buffer) { + if ((uint16_t)(msgq->idx_w - msgq->idx_r) == msgq->size) return -EAGAIN; + + msgq->array[IDX_MASK(msgq->idx_w++, msgq->size)] = buffer; + return 0; +} + +unsigned char *msgq_pop(MSGQueue *msgq) { + if (msgq->idx_r == msgq->idx_w) return NULL; + + return msgq->array[IDX_MASK(msgq->idx_r++, msgq->size)]; +} diff --git a/yocto/esp32mod/msgq.h b/yocto/esp32mod/msgq.h new file mode 100755 index 0000000..f0d82f4 --- /dev/null +++ b/yocto/esp32mod/msgq.h @@ -0,0 +1,15 @@ +typedef struct MSGQueue { + uint16_t idx_r; + uint16_t idx_w; + uint16_t size; + unsigned char **array; + spinlock_t lock; + wait_queue_head_t wq; +} MSGQueue; + +int msgq_create(MSGQueue *msgq, uint16_t size); +void msgq_destroy(MSGQueue *msgq); +uint16_t msgq_len(MSGQueue *msgq); +uint16_t msgq_size(MSGQueue *msgq); +int msgq_push(MSGQueue *msgq, unsigned char *buffer); +unsigned char *msgq_pop(MSGQueue *msgq); diff --git a/yocto/esp32tun/Makefile b/yocto/esp32tun/Makefile new file mode 100755 index 0000000..574c480 --- /dev/null +++ b/yocto/esp32tun/Makefile @@ -0,0 +1,15 @@ +#CFLAGS = +LDFLAGS = -pthread -lgpiod +TARGET = esp32tun +obj = esp32tun.o + +all: $(TARGET) + +%.o: %.c %.h + $(CC) $(CFLAGS) -c $< + +$(TARGET): $(obj) + $(CC) $(obj) $(LDFLAGS) -o $@ + +clean: + rm -f $(TARGET) *.o diff --git a/yocto/esp32tun/esp32tun.c b/yocto/esp32tun/esp32tun.c new file mode 100755 index 0000000..8f9e3bd --- /dev/null +++ b/yocto/esp32tun/esp32tun.c @@ -0,0 +1,173 @@ +#include <stdlib.h> +#include <string.h> +#include <stdio.h> +#include <assert.h> +#include <unistd.h> +#include <fcntl.h> +#include <sys/ioctl.h> + +#include <linux/if.h> +#include <linux/if_tun.h> + +#include <pthread.h> + +#define TUN_NAME "tun0" + +#define ESP32_MTU 1500 +#define ESP32_FNAMSIZ 64 +#define ESP32_DEVFN "/dev/esp32.?" +#define ESP32_MTYPE_TUN 1 +#define ESP32_MAX_MTYPE 8 + +static pthread_t tun_handler_thd, esp32_handler_thd; + +static int tun_fd, esp32_fd; +static char tun_name[IFNAMSIZ]; + +/* Arguments taken by the function: + * + * char *dev: the name of an interface (or '\0'). MUST have enough + * space to hold the interface name if '\0' is passed + * int flags: interface flags (eg, IFF_TUN etc.) + */ +int tun_alloc(char *dev, int flags) { + struct ifreq ifr; + int fd, rv; + char *clonedev = "/dev/net/tun"; + + fd = open(clonedev, O_RDWR); + if (fd < 0) return fd; + + memset(&ifr, 0, sizeof(ifr)); + ifr.ifr_flags = flags; /* IFF_TUN or IFF_TAP, plus maybe IFF_NO_PI */ + + if (*dev) { + /* if a device name was specified, put it in the structure; otherwise, + * the kernel will try to allocate the "next" device of the + * specified type */ + strncpy(ifr.ifr_name, dev, IFNAMSIZ); + } + + /* try to create the device */ + rv = ioctl(fd, TUNSETIFF, (void *) &ifr); + if (rv < 0) { + close(fd); + return rv; + } + + /* if the operation was successful, write back the name of the + * interface to the variable "dev", so the caller can know + * it. Note that the caller MUST reserve space in *dev (see calling + * code below) */ + strcpy(dev, ifr.ifr_name); + + /* this is the special file descriptor that the caller will use to talk + * with the virtual interface */ + return fd; +} + +ssize_t tun_read(unsigned char *buffer, size_t buf_size) { + return read(tun_fd, buffer, buf_size); +} + +ssize_t tun_write(unsigned char *buffer, size_t buf_len) { + return write(tun_fd, buffer, buf_len); +} + +int tun_init(char *name) { + if (strlen(name) + 1 > sizeof(tun_name)) return -1; + + strcpy(tun_name, name); + tun_fd = tun_alloc(tun_name, IFF_TUN | IFF_NO_PI); + + if (tun_fd < 0) return tun_fd; + return 0; +} + +ssize_t esp32_read(unsigned char *buffer, size_t buf_size) { + return read(esp32_fd, buffer, buf_size); +} + +ssize_t esp32_write(unsigned char *buffer, size_t buf_len) { + return write(esp32_fd, buffer, buf_len); +} + +int esp32_fname(char *fname, int mtype) { + if (mtype >= ESP32_MAX_MTYPE) return -1; + + strcpy(fname, ESP32_DEVFN); + fname[strlen(fname) - 1] = '0' + mtype; + + return 0; +} + +int esp32_init(void) { + char fname[ESP32_FNAMSIZ]; + int rv; + + rv = esp32_fname(fname, ESP32_MTYPE_TUN); + if (rv) return rv; + + esp32_fd = open(fname, O_RDWR); + + if (esp32_fd < 0) return esp32_fd; + return 0; +} + +void *tun_handler(void *arg) { + unsigned char buffer[ESP32_MTU]; + ssize_t len, rv; + + while (1) { + len = tun_read(buffer, ESP32_MTU); + if (len < 0) { + perror("tun read"); + continue; + } + rv = esp32_write(buffer, len); + if (rv < 0) { + perror("esp32 write"); + continue; + } + } + + return NULL; +} + +void *esp32_handler(void *arg) { + unsigned char buffer[ESP32_MTU]; + ssize_t len, rv; + + while (1) { + len = esp32_read(buffer, ESP32_MTU); + if (len < 0) { + perror("esp32 read"); + continue; + } + rv = tun_write(buffer, len); + if (rv < 0) { + perror("tun write"); + continue; + } + } + + return NULL; +} + +int main(int argc, char *argv[]) { + int rv; + + rv = tun_init(TUN_NAME); + assert(rv == 0); + + rv = esp32_init(); + assert(rv == 0); + + rv = pthread_create(&tun_handler_thd, NULL, tun_handler, NULL); + assert(rv == 0); + + rv = pthread_create(&esp32_handler_thd, NULL, esp32_handler, NULL); + assert(rv == 0); + + while(1); +} diff --git a/yocto/overlays/mikroPhone-esp32_overlay.dts b/yocto/overlays/mikroPhone-esp32_overlay.dts new file mode 100755 index 0000000..9a6f61a --- /dev/null +++ b/yocto/overlays/mikroPhone-esp32_overlay.dts @@ -0,0 +1,29 @@ +// SPDX-License-Identifier: GPL-2.0-or-later OR MIT +/* + * Copyright 2025 Uros Majstorovic + */ + +// adapted from: https://git.toradex.com/cgit/device-tree-overlays.git/tree/overlays/verdin-imx8mp_spidev_overlay.dts?h=toradex_6.6-2.2.x-imx +// Verdin imx8mp esp32 + +/dts-v1/; +/plugin/; + +/ { + compatible = "toradex,verdin-imx8mp"; +}; + +/* Verdin SPI_1 */ +&ecspi1 { + #address-cells = <1>; + #size-cells = <0>; + status = "okay"; + + esp32@0 { + compatible = "mikrophone,esp32"; + reg = <0>; + spi-max-frequency = <20000000>; + rts-gpio = <&gpio4 25 0>; + cts-gpio = <&gpio4 28 0>; + }; +}; diff --git a/yocto/overlays/mikroPhone-panel_overlay.dts b/yocto/overlays/mikroPhone-panel_overlay.dts index bee2682..aaf494c 100644 --- a/yocto/overlays/mikroPhone-panel_overlay.dts +++ b/yocto/overlays/mikroPhone-panel_overlay.dts @@ -19,46 +19,53 @@ }; &{/} { - backlight_lvds_native: backlight-lvds-native { + backlight_lvds: backlight-lvds { compatible = "pwm-backlight"; pinctrl-names = "default"; brightness-levels = <0 45 63 88 119 158 203 255>; - default-brightness-level = <4>; + default-brightness-level = <0>; /* Verdin PWM_1 (SODIMM 15) */ - pwms = <&pwm1 0 6666667>; + pwms = <&pwm1 0 6666667 0>; }; - panel-lvds-native { + lvds0_panel: panel-lvds-focuslcds { compatible = "panel-lvds"; - backlight = <&backlight_lvds_native>; + backlight = <&backlight_lvds>; data-mapping = "vesa-24"; - height-mm = <110>; width-mm = <62>; + height-mm = <110>; panel-timing { - clock-frequency = <36000000>; + clock-frequency = <26250000>; + hfront-porch = <8>; + hsync-len = <4>; + hback-porch = <8>; hactive = <480>; - hfront-porch = <23 60 71>; - hsync-len = <15 40 47>; - hback-porch = <23 60 71>; + vfront-porch = <7>; + vsync-len = <7>; + vback-porch = <7>; vactive = <854>; - vfront-porch = <5 7 10>; - vsync-len = <6 9 12>; - vback-porch = <5 7 10>; hsync-active = <0>; vsync-active = <0>; - de-active = <0>; - pixelclk-active = <1>; /* positive edge */ + de-active = <1>; + // pixelclk-active = <0>; }; port { - panel_lvds_native_in: endpoint { + panel_lvds_in: endpoint { remote-endpoint = <&lvds_out>; }; }; }; + + dmux { + compatible = "mikrophone,dmux"; + dmux-gpio = <&gpio1 1 0>; + ctp-dev-name = "3-005d"; + status = "okay"; + }; }; /* Verdin I2C_1 */ @@ -70,13 +77,11 @@ gt911@5d { compatible = "goodix,gt911"; reg = <0x5d>; - pinctrl-names = "default"; - pinctrl-0 = <&gpio1>; - /* Verdin GPIO_3 (SODIMM 210) */ + interrupts = <5 IRQ_TYPE_LEVEL_LOW>; interrupt-parent = <&gpio1>; - interrupts = <5 IRQ_TYPE_EDGE_FALLING>; - /* Verdin GPIO_4 (SODIMM 212) */ - reset-gpios = <&gpio1 6 GPIO_ACTIVE_LOW>; + irq-gpio = <&gpio1 5 0>; + reset-gpio = <&gpio1 6 0>; + defer-init; status = "okay"; }; }; @@ -86,11 +91,14 @@ }; &ldb { + #address-cells = <1>; + #size-cells = <0>; status = "okay"; lvds-channel@0 { #address-cells = <1>; #size-cells = <0>; + reg = <0>; fsl,data-mapping = "spwg"; fsl,data-width = <24>; status = "okay"; @@ -99,7 +107,7 @@ reg = <1>; lvds_out: endpoint { - remote-endpoint = <&panel_lvds_native_in>; + remote-endpoint = <&panel_lvds_in>; }; }; }; @@ -108,3 +116,14 @@ &ldb_phy { status = "okay"; }; + +&pwm1 { + status = "okay"; +}; + +/* +&media_blk_ctrl { + assigned-clock-rates = <500000000>, <200000000>, + <0>, <0>, <183750000>; +}; +*/ |
