驅動程式 - VxWorks - 23.09 - C/C++ - File



參考資訊:
https://zhuanlan.zhihu.com/p/597577575
https://studies.ac.upc.edu/doctorat/ENGRAP/VxWorks-device-drivers.htm
https://forums.windriver.com/t/vxworks-software-development-kit-sdk/43
https://mail.prz-rzeszow.pl/~ssamolej/vxworks/vxworks_kernel_programmers_guide_6.6.pdf
https://d13321s3lxgewa.cloudfront.net/downloads/wrsdk-vxworks7-docs/2309/README_qemu.html
https://learning.windriver.com/path/vxworks7-essentials-workbench-and-tools/vxworks-kernel-shell

main.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iosLib.h>
 
static int drvNum = 0;
static DEV_HDR hdr = {0};

static void* myOpen(struct dev_hdr *dev, const char *remainder, int access, int mode)
{
    if (remainder[0] != '\0') {
        return NULL;
    }

    printf("%s()\n", __func__);
    return dev;
}
 
static STATUS myClose(void *dev)
{
    if (dev == NULL) {
        return ERROR;
    }

    printf("%s()\n", __func__);
    return OK;
}

static long myRead(void *dev, char *buf, size_t size)
{
    if (dev == NULL) {
        return 0;
    }

    printf("%s()\n", __func__);
    return 0;
}

static long myWrite(void *dev, const char *buf, size_t size)
{
    if (dev == NULL) {
        return 0;
    }

    printf("%s()\n", __func__);
    return 0;
}
 
STATUS myProbe(void)
{
    drvNum = iosDrvInstall(NULL, NULL, myOpen, myClose, myRead, myWrite, NULL);
    iosDevAdd(&hdr, "/dev/hello", drvNum);
    printf("Hello, world!\n");
    return OK;
}

編譯

$ wr-cc main.c -o hello -dkm

執行

-> ld < hello

-> myProbe
    Hello, world!

-> devs
    drv refs name
     22 [ 5] /dev/hello
     13 [ 5] /dev/random
     13 [ 5] /dev/urandom
     12 [ 5] /dev/zero
     11 [ 5] /fifos
     16 [ 5] /host.host
     14 [ 5] /input/event
      0 [ 7] /null
      5 [ 5] /ram
     10 [ 5] /ram0
      9 [ 5] /romfs
      3 [ 5] /shm
     10 [ 5] /tmp
      2 [ 7] /ttyS0
    value = 0 = 0x0

-> open("/dev/hello")
    myOpen()
    value = 18 = 0x12

-> close(18)
    myClose()
    value = 0 = 0x0

-> unld "hello"