2
0
mirror of https://github.com/m-labs/artiq.git synced 2024-12-24 10:54:02 +08:00
artiq/soc/runtime/corecom_serial.c

129 lines
2.5 KiB
C
Raw Normal View History

2014-08-28 16:56:48 +08:00
#include <stdarg.h>
#include <crc.h>
#include <uart.h>
#include <generated/csr.h>
#include "corecom.h"
enum {
2014-09-05 12:03:22 +08:00
MSGTYPE_REQUEST_IDENT = 0x01,
MSGTYPE_LOAD_KERNEL = 0x02,
MSGTYPE_KERNEL_FINISHED = 0x03,
MSGTYPE_RPC_REQUEST = 0x04,
2014-08-28 16:56:48 +08:00
};
static int receive_int(void)
{
2014-09-05 12:03:22 +08:00
unsigned int r;
int i;
r = 0;
for(i=0;i<4;i++) {
r <<= 8;
r |= (unsigned char)uart_read();
}
return r;
2014-08-28 16:56:48 +08:00
}
static char receive_char(void)
{
2014-09-05 12:03:22 +08:00
return uart_read();
2014-08-28 16:56:48 +08:00
}
static void send_int(int x)
{
2014-09-05 12:03:22 +08:00
int i;
2014-08-28 16:56:48 +08:00
2014-09-05 12:03:22 +08:00
for(i=0;i<4;i++) {
uart_write((x & 0xff000000) >> 24);
x <<= 8;
}
2014-08-28 16:56:48 +08:00
}
static void send_sint(short int i)
{
2014-09-05 12:03:22 +08:00
uart_write((i >> 8) & 0xff);
uart_write(i & 0xff);
2014-08-28 16:56:48 +08:00
}
static void send_char(char c)
{
2014-09-05 12:03:22 +08:00
uart_write(c);
2014-08-28 16:56:48 +08:00
}
static void receive_sync(void)
{
2014-09-05 12:03:22 +08:00
char c;
int recognized;
recognized = 0;
while(recognized < 4) {
c = uart_read();
if(c == 0x5a)
recognized++;
else
recognized = 0;
}
2014-08-28 16:56:48 +08:00
}
static void send_sync(void)
{
2014-09-05 12:03:22 +08:00
send_int(0x5a5a5a5a);
2014-08-28 16:56:48 +08:00
}
int ident_and_download_kernel(void *buffer, int maxlength)
{
2014-09-05 12:03:22 +08:00
int length;
unsigned int crc;
int i;
char msgtype;
unsigned char *_buffer = buffer;
while(1) {
receive_sync();
msgtype = receive_char();
if(msgtype == MSGTYPE_REQUEST_IDENT) {
send_int(0x41524f52); /* "AROR" - ARTIQ runtime on OpenRISC */
send_int(1000000000000LL/identifier_frequency_read()); /* RTIO clock period in picoseconds */
} else if(msgtype == MSGTYPE_LOAD_KERNEL) {
length = receive_int();
if(length > maxlength) {
send_char(0x4c); /* Incorrect length */
return -1;
}
crc = receive_int();
for(i=0;i<length;i++)
_buffer[i] = receive_char();
if(crc32(buffer, length) != crc) {
send_char(0x43); /* CRC failed */
return -1;
}
send_char(0x4f); /* kernel reception OK */
return length;
} else
return -1;
}
2014-08-28 16:56:48 +08:00
}
int rpc(int rpc_num, int n_args, ...)
{
2014-09-05 12:03:22 +08:00
send_sync();
send_char(MSGTYPE_RPC_REQUEST);
send_sint(rpc_num);
send_char(n_args);
va_list args;
va_start(args, n_args);
while(n_args--)
send_int(va_arg(args, int));
va_end(args);
return receive_int();
2014-08-28 16:56:48 +08:00
}
void kernel_finished(void)
{
2014-09-05 12:03:22 +08:00
send_sync();
send_char(MSGTYPE_KERNEL_FINISHED);
2014-08-28 16:56:48 +08:00
}