aos/usr/shelly/shelly.c

109 lines
3.4 KiB
C

/**
* \file
* \brief shelly - a shell process
*/
#include <stdio.h>
#include <stdlib.h>
#include <drivers/lpuart.h>
#include <drivers/gic_dist.h>
#include <maps/imx8x_map.h>
#include <maps/qemu_map.h>
#include <aos/aos.h>
#include <aos/inthandler.h>
#include "shelly.h"
__attribute__((__used__))
static void shelly_interrupt_handler(void * arg) {
errval_t err;
struct shelly_st * shelly_s = (struct shelly_st *) arg;
// SHELLY_DEBUG("[shelly_interrupt_handler]\n");
char input_char;
err = lpuart_getchar(shelly_s->uart_s, &input_char);
if (err_is_fail(err)) USER_PANIC_ERR(err, "while trying to get character");
err = lpuart_putchar(shelly_s->uart_s, input_char);
if (err_is_fail(err)) USER_PANIC_ERR(err, "while trying to put character");
}
int main(int argc, char *argv[])
{
errval_t err;
debug_printf("welcome to shelly!\n");
struct shelly_st * shelly_s = calloc(sizeof(struct shelly_st), 1);
assert(shelly_s);
//afeer: map lpuart registers
struct capref cap_arg0 = {
.cnode = cnode_arg,
.slot = 0
};
err = paging_map_frame_attr(get_current_paging_state(),
(void **) &shelly_s->lpuart_base, IMX8X_UART_SIZE,
cap_arg0, VREGION_FLAGS_READ_WRITE_NOCACHE);
if (err_is_fail(err)) USER_PANIC_ERR(err, "while mapping lpuart registers");
SHELLY_DEBUG("mapped lpuart\n");
//afeer: map gic registers
struct capref cap_arg1 = {
.cnode = cnode_arg,
.slot = 1
};
err = paging_map_frame_attr(get_current_paging_state(),
(void**) &shelly_s->gic_base, IMX8X_GIC_DIST_SIZE,
cap_arg1, VREGION_FLAGS_READ_WRITE_NOCACHE);
if (err_is_fail(err)) USER_PANIC_ERR(err, "while mapping gic registers");
SHELLY_DEBUG("mapped gic\n");
//afeer: init gic driver
err = gic_dist_init(&shelly_s->gic_s, shelly_s->gic_base);
if (err_is_fail(err)) USER_PANIC_ERR(err, "couln't init gic_dist interrupts");
SHELLY_DEBUG("initialized gic driver\n");
//afeer: init lpuart driver
err = lpuart_init(&shelly_s->uart_s, shelly_s->lpuart_base);
if (err_is_fail(err)) {
USER_PANIC_ERR(err, "couldn't init lpuart");
}
SHELLY_DEBUG("initialized lpuart driver\n");
struct capref inter_cap;
err = inthandler_alloc_dest_irq_cap(IMX8X_UART3_INT, &inter_cap);
if (err_is_fail(err)) USER_PANIC_ERR(err, "couln't alloc dest cap");
SHELLY_DEBUG("created interrupt cap\n");
char buf[256];
debug_print_cap_at_capref(buf, 256, inter_cap);
debug_printf(buf);
struct waitset * ws = get_default_waitset();
err = inthandler_setup(inter_cap, ws, MKCLOSURE(shelly_interrupt_handler, (void *) shelly_s));
if (err_is_fail(err)) USER_PANIC_ERR(err, "couln't setup interrupt handler");
SHELLY_DEBUG("setup interrupt handler\n");
err = gic_dist_enable_interrupt(shelly_s->gic_s, IMX8X_UART3_INT, 0b11111111, 0);
if (err_is_fail(err)) USER_PANIC_ERR(err, "couln't enable gic interrupts");
SHELLY_DEBUG("enabled gic interrupt\n");
err = lpuart_enable_interrupt(shelly_s->uart_s);
if (err_is_fail(err)) USER_PANIC_ERR(err, "couln't enable lpuart interrupts");
SHELLY_DEBUG("enabled lpuart interrupt\n");
while (true) {
err = event_dispatch(ws);
if (err_is_fail(err)) {
DEBUG_ERR(err, "in event_dispatch");
abort();
}
}
}