Skip to content

Commit

Permalink
add nrf91 multi driver
Browse files Browse the repository at this point in the history
DONE: RTOS-757
  • Loading branch information
damianloew committed Feb 16, 2024
1 parent ac27d41 commit 1e71b09
Show file tree
Hide file tree
Showing 8 changed files with 935 additions and 0 deletions.
9 changes: 9 additions & 0 deletions _targets/Makefile.armv8m33-nrf9160
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#
# Makefile for Phoenix-RTOS 3 device drivers
#
# NRF9160 drivers
#
# Copyright 2022 Phoenix Systems
#

DEFAULT_COMPONENTS := nrf91-multi
14 changes: 14 additions & 0 deletions multi/nrf91-multi/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#
# Makefile for Phoenix-RTOS nrf91-multi
#
# Copyright 2024 Phoenix Systems
#

LOCAL_PATH := $(call my-dir)

NAME := nrf91-multi
SRCS := $(wildcard $(LOCAL_PATH)*.c)
DEP_LIBS := libtty libklog
LIBS := libdummyfs libklog

include $(binary.mk)
30 changes: 30 additions & 0 deletions multi/nrf91-multi/common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Phoenix-RTOS
*
* nRF91 multidriver common
*
* Copyright 2017, 2018, 2023 Phoenix Systems
* Author: Aleksander Kaminski, Damian Loewnau
*
* This file is part of Phoenix-RTOS.
*
* %LICENSE%
*/

#ifndef _COMMON_H_
#define _COMMON_H_

#include <stdint.h>
#include <stdio.h>
#include <phoenix/arch/armv8m/nrf/91/nrf9160.h>
#include <sys/platform.h>

#include <board_config.h>


static inline void dataBarier(void)
{
__asm__ volatile("dmb");
}

#endif
174 changes: 174 additions & 0 deletions multi/nrf91-multi/fs.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
/*
* Phoenix-RTOS
*
* nRF9160 Filesystem driver
*
* Copyright 2021, 2023 Phoenix Systems
* Author: Maciej Purski, Damian Loewnau
*
* This file is part of Phoenix-RTOS.
*
* %LICENSE%
*/

#include <errno.h>
#include <sys/file.h>
#include <sys/mman.h>
#include <sys/msg.h>
#include <sys/stat.h>
#include <sys/threads.h>
#include <stdlib.h>

#include <phoenix/sysinfo.h>

#include <dummyfs.h>

#include "fs.h"

#define MSGTHR_STACKSZ 4096

static struct {
char stack[MSGTHR_STACKSZ] __attribute__((aligned(8)));
unsigned port;
} fs_common;


static int syspage_create(void *ctx, oid_t *root)
{
oid_t sysoid = { 0 };
oid_t toid = { 0 };
syspageprog_t prog;
int i, progsz;

progsz = syspageprog(NULL, -1);
if (progsz < 0) {
return -1;
}

if (dummyfs_create(ctx, root, "syspage", &sysoid, S_IRWXU | S_IRWXG | S_IRWXO, otDir, NULL) != 0) {
return -ENOMEM;
}

for (i = 0; i < progsz; i++) {
if (syspageprog(&prog, i) != 0) {
continue;
}

dummyfs_createMapped(ctx, &sysoid, prog.name, (void *)prog.addr, prog.size, &toid);
}

return 0;
}


static void msgthr(void *ctx)
{
msg_t msg;
msg_rid_t rid;

for (;;) {
if (msgRecv(fs_common.port, &msg, &rid) < 0) {
continue;
}

switch (msg.type) {
case mtOpen:
msg.o.io.err = dummyfs_open(ctx, &msg.i.openclose.oid);
break;

case mtClose:
msg.o.io.err = dummyfs_close(ctx, &msg.i.openclose.oid);
break;

case mtRead:
msg.o.io.err = dummyfs_read(ctx, &msg.i.io.oid, msg.i.io.offs, msg.o.data, msg.o.size);
break;

case mtWrite:
msg.o.io.err = dummyfs_write(ctx, &msg.i.io.oid, msg.i.io.offs, msg.i.data, msg.i.size);
break;

case mtTruncate:
msg.o.io.err = dummyfs_truncate(ctx, &msg.i.io.oid, msg.i.io.len);
break;

case mtDevCtl:
msg.o.io.err = -EINVAL;
break;

case mtCreate:
msg.o.create.err = dummyfs_create(ctx, &msg.i.create.dir, msg.i.data, &msg.o.create.oid, msg.i.create.mode, msg.i.create.type, &msg.i.create.dev);
break;

case mtDestroy:
msg.o.io.err = dummyfs_destroy(ctx, &msg.i.destroy.oid);
break;

case mtSetAttr:
msg.o.attr.err = dummyfs_setattr(ctx, &msg.i.attr.oid, msg.i.attr.type, msg.i.attr.val, msg.i.data, msg.i.size);
break;

case mtGetAttr:
msg.o.attr.err = dummyfs_getattr(ctx, &msg.i.attr.oid, msg.i.attr.type, &msg.o.attr.val);
break;

case mtLookup:
msg.o.lookup.err = dummyfs_lookup(ctx, &msg.i.lookup.dir, msg.i.data, &msg.o.lookup.fil, &msg.o.lookup.dev);
break;

case mtLink:
msg.o.io.err = dummyfs_link(ctx, &msg.i.ln.dir, msg.i.data, &msg.i.ln.oid);
break;

case mtUnlink:
msg.o.io.err = dummyfs_unlink(ctx, &msg.i.ln.dir, msg.i.data);
break;

case mtReaddir:
msg.o.io.err = dummyfs_readdir(ctx, &msg.i.readdir.dir, msg.i.readdir.offs,
msg.o.data, msg.o.size);
break;
default:
msg.o.io.err = -EINVAL;
break;
}
msgRespond(fs_common.port, &msg, rid);
}
}


int fs_init(void)
{
void *ctx;
oid_t root = { 0 };

if (portCreate(&fs_common.port) != 0) {
return -1;
}

if (portRegister(fs_common.port, "/", &root) != 0) {
portDestroy(fs_common.port);
return -1;
}

root.port = fs_common.port;
if (dummyfs_mount(&ctx, NULL, 0, &root) != 0) {
printf("dummyfs mount failed\n");
portDestroy(fs_common.port);
return -1;
}

if (syspage_create(ctx, &root) != 0) {
dummyfs_unmount(ctx);
portDestroy(fs_common.port);
return -1;
}

if (beginthread(msgthr, 4, fs_common.stack, MSGTHR_STACKSZ, ctx) != 0) {
dummyfs_unmount(ctx);
portDestroy(fs_common.port);
return -1;
}

return 0;
}
18 changes: 18 additions & 0 deletions multi/nrf91-multi/fs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Phoenix-RTOS
*
* STM32L4/nRF91 Filesystem driver header
*
* Copyright 2021, 2023 Phoenix Systems
* Author: Maciej Purski, Damian Loewnau
*
* This file is part of Phoenix-RTOS.
*
* %LICENSE%
*/
#ifndef FS_H_
#define FS_H_

extern int fs_init(void);

#endif /* FS_H_ */
120 changes: 120 additions & 0 deletions multi/nrf91-multi/nrf91-multi.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
* Phoenix-RTOS
*
* nRF91 multi driver server
*
* Copyright 2023, 2024 Phoenix Systems
* Author: Aleksander Kaminski, Damian Loewnau
*
* This file is part of Phoenix-RTOS.
*
* %LICENSE%
*/


#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/threads.h>
#include <sys/msg.h>
#include <sys/pwman.h>

#include <libklog.h>

#include "common.h"

#include "fs.h"
#include "uart.h"

#define THREADS_CNT 3
#define THREAD_PRIORITY 1
#define THREAD_STACKSZ 640


static struct {
char stack[THREADS_CNT - 1][THREAD_STACKSZ] __attribute__((aligned(8)));

unsigned int port;
} multi_common;


static void multi_thread(void *arg)
{
msg_t msg;
msg_rid_t rid;

while (1) {
while (msgRecv(multi_common.port, &msg, &rid) < 0) {
}

priority(msg.priority);

switch (msg.type) {
case mtOpen:
case mtClose:
msg.o.io.err = 0;
break;

case mtRead:
msg.o.io.err = uart_read(msg.o.data, msg.o.size, msg.i.io.mode);
break;

case mtWrite:
msg.o.io.err = uart_log(msg.i.data, msg.i.size, msg.i.io.mode);
break;

case mtDevCtl:
/* TODO: add implementation */
msg.o.io.err = -EINVAL;
break;

case mtCreate:
msg.o.create.err = -EINVAL;
break;

case mtLookup:
msg.o.lookup.err = -EINVAL;
break;

default:
msg.o.io.err = -EINVAL;
break;
}

msgRespond(multi_common.port, &msg, rid);

priority(THREAD_PRIORITY);
}
}


int main(void)
{
int i;
oid_t oid;

priority(THREAD_PRIORITY);

portCreate(&multi_common.port);

fs_init();
uart_init();
libklog_init(uart_log);

/* Do this after klog init to keep shell from overtaking klog */
uart_createConsoleDev();

portRegister(multi_common.port, "/multi", &oid);

/* 2 threads launched in for loop and another one in the main thread */
for (i = 0; i < THREADS_CNT - 1; ++i) {
beginthread(&multi_thread, THREAD_PRIORITY, multi_common.stack[i], THREAD_STACKSZ, (void *)i);
}

printf("multidrv: Started\n");

multi_thread((void *)i);

return 0;
}
Loading

0 comments on commit 1e71b09

Please sign in to comment.