mirror of
https://github.com/justinian/jsix.git
synced 2025-12-10 00:14:32 -08:00
[drv.uart] Replace user code with new channels
Move all the user space uses of channels to use j6::channel.
This commit is contained in:
@@ -4,12 +4,14 @@
|
||||
#include <stdio.h>
|
||||
|
||||
#include <j6/cap_flags.h>
|
||||
#include <j6/channel.hh>
|
||||
#include <j6/errors.h>
|
||||
#include <j6/flags.h>
|
||||
#include <j6/init.h>
|
||||
#include <j6/protocols/service_locator.h>
|
||||
#include <j6/syscalls.h>
|
||||
#include <j6/sysconf.h>
|
||||
#include <j6/syslog.hh>
|
||||
#include <j6/types.h>
|
||||
#include <util/hash.h>
|
||||
|
||||
@@ -70,20 +72,14 @@ main(int argc, const char **argv)
|
||||
if (slp == j6_handle_invalid)
|
||||
return 1;
|
||||
|
||||
j6_handle_t cout = j6_handle_invalid;
|
||||
result = j6_channel_create(&cout);
|
||||
if (result != j6_status_ok)
|
||||
j6::channel *cout = j6::channel::create(0x2000);
|
||||
if (!cout)
|
||||
return 2;
|
||||
|
||||
j6_handle_t cout_write = j6_handle_invalid;
|
||||
result = j6_handle_clone(cout, &cout_write,
|
||||
j6_cap_channel_send | j6_cap_object_clone);
|
||||
if (result != j6_status_ok)
|
||||
return 3;
|
||||
|
||||
uint64_t tag = j6_proto_sl_register;
|
||||
uint64_t proto_id = "jsix.protocol.stream.ouput"_id;
|
||||
result = j6_mailbox_call(slp, &tag, &proto_id, &cout_write);
|
||||
uint64_t handle = cout->handle();
|
||||
result = j6_mailbox_call(slp, &tag, &proto_id, &handle);
|
||||
if (result != j6_status_ok)
|
||||
return 4;
|
||||
|
||||
@@ -96,21 +92,29 @@ main(int argc, const char **argv)
|
||||
|
||||
while (true) {
|
||||
size_t size = buffer_size;
|
||||
result = j6_channel_receive(cout, buffer, &size, 0);
|
||||
if (result == j6_status_ok)
|
||||
while (true) {
|
||||
result = cout->receive(buffer, &size, 0);
|
||||
if (result != j6_status_ok)
|
||||
break;
|
||||
|
||||
j6::syslog("uart driver: got %d bytes from channel", size);
|
||||
com1.write(buffer, size);
|
||||
}
|
||||
if (result != j6_status_would_block)
|
||||
j6::syslog("uart driver: error %lx receiving from channel", result);
|
||||
|
||||
uint64_t signals = 0;
|
||||
result = j6_event_wait(event, &signals, 100);
|
||||
result = j6_event_wait(event, &signals, 500);
|
||||
if (result == j6_err_timed_out) {
|
||||
com1.handle_interrupt();
|
||||
com2.handle_interrupt();
|
||||
com1.do_write();
|
||||
continue;
|
||||
}
|
||||
|
||||
if (result != j6_status_ok) {
|
||||
j6_log("uart driver got error waiting for irq");
|
||||
j6::syslog("uart driver: error %lx waiting for irq", result);
|
||||
continue;
|
||||
} else {
|
||||
j6::syslog("uart driver: irq signals: %lx", signals);
|
||||
}
|
||||
|
||||
if (signals & (1<<0))
|
||||
|
||||
@@ -56,9 +56,11 @@ serial_port::handle_interrupt()
|
||||
break;
|
||||
|
||||
case 1: // Transmit buffer empty
|
||||
do_write();
|
||||
break;
|
||||
|
||||
case 2: // Received data available
|
||||
if (read_ready(m_port)) do_read();
|
||||
if (write_ready(m_port)) do_write();
|
||||
do_read();
|
||||
break;
|
||||
|
||||
case 3: // Line status change
|
||||
@@ -89,8 +91,6 @@ serial_port::do_read()
|
||||
void
|
||||
serial_port::do_write()
|
||||
{
|
||||
// If another thread is writing data, just give up and
|
||||
// try again later
|
||||
util::scoped_lock lock {m_lock};
|
||||
|
||||
uint8_t *data = nullptr;
|
||||
|
||||
@@ -19,6 +19,7 @@ public:
|
||||
char read();
|
||||
|
||||
void handle_interrupt();
|
||||
void do_write();
|
||||
|
||||
private:
|
||||
bool m_writing;
|
||||
@@ -28,6 +29,5 @@ private:
|
||||
util::spinlock m_lock;
|
||||
|
||||
void do_read();
|
||||
void do_write();
|
||||
void handle_error(uint16_t reg, uint8_t value);
|
||||
};
|
||||
|
||||
@@ -32,7 +32,7 @@ service_locator_start(j6_handle_t mb)
|
||||
|
||||
while (true) {
|
||||
j6_status_t s = j6_mailbox_respond(mb, &tag, &subtag, &give_handle,
|
||||
&reply_tag, j6_mailbox_block);
|
||||
&reply_tag, j6_flag_block);
|
||||
|
||||
if (s != j6_status_ok)
|
||||
while (1);
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include <j6/channel.hh>
|
||||
#include <j6/cap_flags.h>
|
||||
#include <j6/errors.h>
|
||||
#include <j6/flags.h>
|
||||
@@ -30,21 +31,7 @@ char const * const area_names[] = {
|
||||
};
|
||||
|
||||
void
|
||||
send_all(j6_handle_t cout, char *data, size_t len)
|
||||
{
|
||||
do {
|
||||
size_t sent = len;
|
||||
j6_status_t s = j6_channel_send(cout, data, &sent);
|
||||
if (s != j6_status_ok)
|
||||
return;
|
||||
|
||||
len -= sent;
|
||||
data += sent;
|
||||
} while (len);
|
||||
}
|
||||
|
||||
void
|
||||
print_header(j6_handle_t cout)
|
||||
print_header(j6::channel *cout)
|
||||
{
|
||||
char stringbuf[150];
|
||||
|
||||
@@ -57,11 +44,11 @@ print_header(j6_handle_t cout)
|
||||
"\e[38;5;21mjsix OS\e[38;5;8m %d.%d.%d (%07x) booting...\e[0m\r\n",
|
||||
version_major, version_minor, version_patch, version_git);
|
||||
|
||||
send_all(cout, stringbuf, len);
|
||||
cout->send(stringbuf, len);
|
||||
}
|
||||
|
||||
void
|
||||
log_pump_proc(j6_handle_t cout)
|
||||
log_pump_proc(j6::channel *cout)
|
||||
{
|
||||
size_t buffer_size = 0;
|
||||
void *message_buffer = nullptr;
|
||||
@@ -96,10 +83,10 @@ log_pump_proc(j6_handle_t cout)
|
||||
|
||||
int message_len = static_cast<int>(e->bytes - sizeof(j6_log_entry));
|
||||
size_t len = snprintf(stringbuf, sizeof(stringbuf),
|
||||
"\e[38;5;%dm%7s %7s: %.*s\e[38;5;0m\r\n",
|
||||
level_color, area_name, level_name,
|
||||
"\e[38;5;%dm%5lx %7s %7s: %.*s\e[38;5;0m\r\n",
|
||||
level_color, seen, area_name, level_name,
|
||||
message_len, e->message);
|
||||
send_all(cout, stringbuf, len + 1);
|
||||
cout->send(stringbuf, len+1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -117,25 +104,29 @@ main(int argc, const char **argv)
|
||||
if (g_handle_sys == j6_handle_invalid)
|
||||
return 2;
|
||||
|
||||
j6_handle_t cout = j6_handle_invalid;
|
||||
j6_handle_t cout_vma = j6_handle_invalid;
|
||||
|
||||
for (unsigned i = 0; i < 100; ++i) {
|
||||
uint64_t tag = j6_proto_sl_find;
|
||||
uint64_t proto_id = "jsix.protocol.stream.ouput"_id;
|
||||
|
||||
j6_status_t s = j6_mailbox_call(slp, &tag, &proto_id, &cout);
|
||||
j6_status_t s = j6_mailbox_call(slp, &tag, &proto_id, &cout_vma);
|
||||
if (s == j6_status_ok &&
|
||||
tag == j6_proto_sl_result &&
|
||||
cout != j6_handle_invalid)
|
||||
cout_vma != j6_handle_invalid)
|
||||
break;
|
||||
|
||||
cout = j6_handle_invalid;
|
||||
cout_vma = j6_handle_invalid;
|
||||
j6_thread_sleep(10000); // 10ms
|
||||
}
|
||||
|
||||
if (cout == j6_handle_invalid)
|
||||
if (cout_vma == j6_handle_invalid)
|
||||
return 3;
|
||||
|
||||
j6::channel *cout = j6::channel::open(cout_vma);
|
||||
if (!cout)
|
||||
return 4;
|
||||
|
||||
print_header(cout);
|
||||
log_pump_proc(cout);
|
||||
return 0;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <j6/channel.hh>
|
||||
#include <j6/errors.h>
|
||||
#include <j6/flags.h>
|
||||
#include <j6/syscalls.h>
|
||||
@@ -17,13 +18,15 @@ constexpr uintptr_t stack_top = 0xf80000000;
|
||||
uint32_t flipflop = 0;
|
||||
|
||||
void
|
||||
thread_proc(void*)
|
||||
thread_proc(void* channelp)
|
||||
{
|
||||
j6_log("sub thread starting");
|
||||
|
||||
j6::channel *chan = reinterpret_cast<j6::channel*>(channelp);
|
||||
|
||||
char buffer[512];
|
||||
size_t len = sizeof(buffer);
|
||||
j6_status_t result = j6_channel_receive(endp, (void*)buffer, &len, j6_channel_block);
|
||||
j6_status_t result = chan->receive((void*)buffer, &len);
|
||||
|
||||
__sync_synchronize();
|
||||
flipflop = 1;
|
||||
@@ -37,7 +40,7 @@ thread_proc(void*)
|
||||
if (buffer[i] >= 'A' && buffer[i] <= 'Z')
|
||||
buffer[i] += 0x20;
|
||||
|
||||
result = j6_channel_send(endp, (void*)buffer, &len);
|
||||
result = chan->send((void*)buffer, len);
|
||||
if (result != j6_status_ok)
|
||||
j6_thread_exit();
|
||||
|
||||
@@ -70,14 +73,14 @@ main(int argc, const char **argv)
|
||||
|
||||
j6_log("main thread wrote to memory area");
|
||||
|
||||
j6_status_t result = j6_channel_create(&endp);
|
||||
if (result != j6_status_ok)
|
||||
return result;
|
||||
j6::channel *chan = j6::channel::create(0x2000);
|
||||
if (!chan)
|
||||
return 2;
|
||||
|
||||
j6_log("main thread created channel");
|
||||
|
||||
j6::thread child_thread {thread_proc, stack_top};
|
||||
result = child_thread.start();
|
||||
j6_status_t result = child_thread.start(chan);
|
||||
if (result != j6_status_ok)
|
||||
return result;
|
||||
|
||||
@@ -86,13 +89,13 @@ main(int argc, const char **argv)
|
||||
char message[] = "MAIN THREAD SUCCESSFULLY CALLED SEND AND RECEIVE IF THIS IS LOWERCASE";
|
||||
size_t size = sizeof(message);
|
||||
|
||||
result = j6_channel_send(endp, (void*)message, &size);
|
||||
result = chan->send((void*)message, size);
|
||||
if (result != j6_status_ok)
|
||||
return result;
|
||||
|
||||
while (!flipflop);
|
||||
|
||||
result = j6_channel_receive(endp, (void*)message, &size, j6_channel_block);
|
||||
result = chan->receive((void*)message, &size);
|
||||
if (result != j6_status_ok)
|
||||
return result;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user