Add the skeleton of the client-side program.

This commit is contained in:
David Given
2018-09-23 13:13:29 +02:00
parent 755ae2950d
commit 264cf47c00
7 changed files with 133 additions and 6 deletions

View File

Binary file not shown.

View File

@@ -1,8 +1,8 @@
#include <stdint.h>
#include <stdbool.h>
#include "project.h"
#include "../protocol.h"
#define BUFFER_SIZE 63
#define BUFFER_COUNT 2
static uint8_t td[BUFFER_COUNT];
@@ -50,10 +50,11 @@ CY_ISR(dma_finished_isr)
else
{
LED_REG_Write(0);
static uint8_t sendbuffer[BUFFER_SIZE + 1];
sendbuffer[0] = 0;
memcpy(sendbuffer+1, buffer[last_td], BUFFER_SIZE);
USBFS_PutData(sendbuffer, sizeof(sendbuffer));
static frame_t sendframe;
sendframe.id = FLUXENGINE_ID;
sendframe.type = FRAME_OK;
memcpy(sendframe.u.buffer, buffer[last_td], BUFFER_SIZE);
USBFS_PutData((const uint8_t*) &sendframe, sizeof(sendframe));
}
}
@@ -67,7 +68,8 @@ int main(void)
init_dma();
UART_PutString("GO\r");
LED_REG_Write(0);
for (;;)
{
if (!USBFS_GetConfiguration() || USBFS_IsConfigurationChanged())

13
Makefile Normal file
View File

@@ -0,0 +1,13 @@
CFLAGS = -Wall
SRCS = main.c windows.c
OBJS = $(patsubst %.c, .objs/%.o, $(SRCS))
fluxclient: $(OBJS)
gcc -o fluxclient $(OBJS)
.objs/%.o: %.c
@mkdir -p $(dir $@)
gcc -c $(CFLAGS) -o $@ $<
$(OBJS): globals.h protocol.h

16
globals.h Normal file
View File

@@ -0,0 +1,16 @@
#ifndef GLOBALS_H
#define GLOBALS_H
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include "protocol.h"
extern void error(const char* message, ...);
extern void open_serial_port(const char* port);
extern void read_frame(frame_t* frame);
#endif

26
main.c Normal file
View File

@@ -0,0 +1,26 @@
#include "globals.h"
#include <stdarg.h>
void error(const char* message, ...)
{
va_list ap;
va_start(ap, message);
fprintf(stderr, "fluxengine: ");
vfprintf(stderr, message, ap);
fprintf(stderr, "\n");
va_end(ap);
exit(1);
}
int main(int argc, const char** argv)
{
open_serial_port("COM4");
for (int i=0; i<10000; i++)
{
frame_t frame;
read_frame(&frame);
}
return 0;
}

36
protocol.h Normal file
View File

@@ -0,0 +1,36 @@
#ifndef PROTOCOL_H
#define PROTOCOL_H
#define BUFFER_SIZE 61
enum
{
FLUXENGINE_ID = 'F'
};
enum
{
FRAME_OK = 0,
FRAME_FINISHED,
FRAME_COMMAND,
};
enum
{
ERROR_NONE = 0,
ERROR_UNDERRUN
};
typedef struct
{
uint8_t id;
uint8_t type;
union
{
uint8_t buffer[BUFFER_SIZE];
uint8_t status;
} u;
}
frame_t;
#endif

34
windows.c Normal file
View File

@@ -0,0 +1,34 @@
#include "globals.h"
#include <windows.h>
static HANDLE handle;
static char error_buffer[128];
static const char* get_windows_error(void)
{
FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
error_buffer, sizeof(error_buffer), NULL);
return error_buffer;
}
void open_serial_port(const char *name)
{
handle = CreateFile(name, GENERIC_READ | GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
0,
0);
if (handle == INVALID_HANDLE_VALUE)
error("failed to open serial port %s: %s", name, get_windows_error());
}
void read_frame(frame_t* frame)
{
DWORD bytes_read = 0;
if (!ReadFile(handle, frame, sizeof(frame_t), &bytes_read, NULL))
error("couldn't read from serial port: %s", get_windows_error());
if ((bytes_read != sizeof(frame_t)) || (frame->id != FLUXENGINE_ID))
error("sequencing error");
}