summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAditya Naik2020-08-07 12:41:48 -0400
committerAditya Naik2020-08-07 12:41:48 -0400
commitcda9397f911dea0b57ffd3c041121885ab03b79c (patch)
treecc7a97b32ee45b707d6985287de8bd529d7506ab
parent4e8a6d4122e188ed2ca6668e30c0500e63fba8ef (diff)
Device abstraction work
-rw-r--r--include/stream.h10
-rw-r--r--include/stream_i2c.h4
-rw-r--r--include/stream_stdio.h4
-rw-r--r--ports/posix/makefile1
-rw-r--r--ports/posix/src/port.h2
-rw-r--r--ports/posix/src/stream_stdio.c16
-rw-r--r--ports/stm32f4/src/stream_i2c.c6
-rw-r--r--src/master_posix.c26
8 files changed, 34 insertions, 35 deletions
diff --git a/include/stream.h b/include/stream.h
index 73b3daa..9c6da54 100644
--- a/include/stream.h
+++ b/include/stream.h
@@ -3,7 +3,8 @@
#ifndef __STREAM_H
#define __STREAM_H
-/*
+
+/**
* Struct for abstract stream
*
* Stream properties: generalized variables for individual devices
@@ -19,4 +20,9 @@ typedef struct {
void **props;
} p_stream_t;
-#endif
+typedef enum {
+ STDIO,
+} devices;
+
+
+#endif /* __STREAM_H */
diff --git a/include/stream_i2c.h b/include/stream_i2c.h
index ccbccde..5b2ab90 100644
--- a/include/stream_i2c.h
+++ b/include/stream_i2c.h
@@ -1,5 +1,5 @@
#include <stdint.h>
#include <stddef.h>
-int i2c_read(uint8_t* buf, size_t count, void **vptr, void *sptr);
-int i2c_write(uint8_t* buf, size_t count, void **vptr, void *sptr);
+int read_I2C(uint8_t* buf, size_t count, void **vptr, void *sptr);
+int write_I2C(uint8_t* buf, size_t count, void **vptr, void *sptr);
diff --git a/include/stream_stdio.h b/include/stream_stdio.h
index c72183b..dbcff9f 100644
--- a/include/stream_stdio.h
+++ b/include/stream_stdio.h
@@ -1,5 +1,5 @@
#include <stdint.h>
#include <stddef.h>
-int stdio_read(uint8_t* buf, size_t count, void **vptr, void *sptr);
-int stdio_write(uint8_t* buf, size_t count, void **vptr, void *sptr);
+int read_STDIO(uint8_t* buf, size_t count, void **vptr, void *sptr);
+int write_STDIO(uint8_t* buf, size_t count, void **vptr, void *sptr);
diff --git a/ports/posix/makefile b/ports/posix/makefile
index 6b9367e..58d42be 100644
--- a/ports/posix/makefile
+++ b/ports/posix/makefile
@@ -11,4 +11,3 @@ C_INCLUDES += \
C_SOURCES += \
lib/FreeRTOS/FreeRTOS/Source/portable/ThirdParty/GCC/Posix/port.c \
ports/posix/src/stream_stdio.c \
-ports/posix/src/port_devices.c
diff --git a/ports/posix/src/port.h b/ports/posix/src/port.h
index 6f10699..7c94ce8 100644
--- a/ports/posix/src/port.h
+++ b/ports/posix/src/port.h
@@ -13,6 +13,4 @@
#include <errno.h>
#include "stream.h"
-int initialize_devices(int num_devices, p_stream_t *streams);
-
#endif
diff --git a/ports/posix/src/stream_stdio.c b/ports/posix/src/stream_stdio.c
index e9b6f09..d49c46e 100644
--- a/ports/posix/src/stream_stdio.c
+++ b/ports/posix/src/stream_stdio.c
@@ -1,21 +1,11 @@
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
#include "stream_stdio.h"
#include "stream.h"
#include "main.h"
-int stdio_init(void **vptr, void *sptr)
-{
- /* TODO */
- /* p_stream_t *stream = (p_stream_t*)sptr; */
- /* int num_files = BUS_DEVICE_LIMIT; */
- /* int x; */
- /* char fname_base[] = "devio_", fname[10]; */
-
- return 0;
-}
-
-int stdio_read(uint8_t* buf, size_t count, void **vptr, void *sptr)
+int read_STDIO(uint8_t* buf, size_t count, void **vptr, void *sptr)
{
int x;
for (x = 0; x < count; x++) {
@@ -24,7 +14,7 @@ int stdio_read(uint8_t* buf, size_t count, void **vptr, void *sptr)
return 0;
}
-int stdio_write(uint8_t* buf, size_t count, void **vptr, void *sptr)
+int write_STDIO(uint8_t* buf, size_t count, void **vptr, void *sptr)
{
int x;
for (x = 0; x < count; x++) {
diff --git a/ports/stm32f4/src/stream_i2c.c b/ports/stm32f4/src/stream_i2c.c
index 8c6c65b..621e11e 100644
--- a/ports/stm32f4/src/stream_i2c.c
+++ b/ports/stm32f4/src/stream_i2c.c
@@ -2,7 +2,7 @@
#include "port.h"
#include "stream.h"
-int i2c_read(uint8_t* buf, size_t count, void **vptr, void *sptr)
+int read_I2C(uint8_t* buf, size_t count, void **vptr, void *sptr)
{
p_stream_t *stream = (p_stream_t*)sptr;
I2C_HandleTypeDef dev = *(I2C_HandleTypeDef*)stream->props[0];
@@ -23,7 +23,7 @@ int i2c_read(uint8_t* buf, size_t count, void **vptr, void *sptr)
return 0;
}
-int i2c_write(uint8_t* buf, size_t count, void **vptr, void *sptr)
+int write_I2C(uint8_t* buf, size_t count, void **vptr, void *sptr)
{
p_stream_t *stream = (p_stream_t*)sptr;
I2C_HandleTypeDef dev = *(I2C_HandleTypeDef*)stream->props[0];
@@ -32,6 +32,6 @@ int i2c_write(uint8_t* buf, size_t count, void **vptr, void *sptr)
while (HAL_I2C_Master_Transmit(&dev, addr, buf, count, timeout) != HAL_OK) {
return HAL_I2C_GetError(&dev);
- }
+ }
return 0;
}
diff --git a/src/master_posix.c b/src/master_posix.c
index c312757..dc8be7a 100644
--- a/src/master_posix.c
+++ b/src/master_posix.c
@@ -4,6 +4,7 @@
* Actually, target-specific includes should go in the port folder
* This should be specified in the master config file
* TODO This file should be moved to the posix port folder
+ *
*/
/* FreeRTOS includes. */
@@ -18,7 +19,7 @@
#include <pb_encode.h>
#include <pb_decode.h>
-/* Project includes. */
+/* (Ideally) platform-agnostic project includes. */
#include "master_posix.h"
#include "main.h"
#include "devices.h"
@@ -46,6 +47,11 @@
#define SET_BIT_FROM_IDX(a, b) a[b>>5]|=(1<<(b%32))
#define COUNTOF(__BUFFER__) (sizeof(__BUFFER__) / sizeof(*(__BUFFER__)))
+#define STREAM_INIT(PERIPH, vptr, sptr) \
+ sptr.read=&read_##PERIPH; \
+ sptr.write=&write_##PERIPH; \
+ sptr.props=vptr;
+
#define BUS_DEVICE_LIMIT 2
device_info_t *device_info[BUS_DEVICE_LIMIT] = {NULL};
@@ -66,7 +72,7 @@ uint32_t data_len_buf[ROUTING_BUFSIZE];
uint32_t data_routing_ptr = 0; /*< Pointer to tail of both data and data index buffers */
uint32_t cmd_routing_ptr = 0; /*< Pointer to tail of cmd and cmd index buffers */
-p_stream_t stream = {&stdio_read, &stdio_write, NULL, NULL};
+p_stream_t stream;
static void *handshake_func(void * pvArgs);
static void *dataflow_func(void *pvArgs);
@@ -96,6 +102,12 @@ bool master_encode_MDR_callback(pb_ostream_t *ostream, const pb_field_t *field,
void vStartPOSIXMaster(void *pvParams)
{
pthread_t handshake_thread, dataflow_thread, routing_thread;
+ p_stream_t device_streams[2];
+ STREAM_INIT(STDIO, NULL, device_streams[0]);
+ STREAM_INIT(STDIO, NULL, device_streams[1]);
+
+ /* This is the global stream that is being used by handhsake and dataflow */
+ STREAM_INIT(STDIO, NULL, stream);
pthread_create(&handshake_thread, NULL, handshake_func, NULL);
pthread_create(&dataflow_thread, NULL, dataflow_func, NULL);
@@ -107,11 +119,6 @@ void vStartPOSIXMaster(void *pvParams)
* either in a custom format or in a devicetree format (preferred)
*
*/
-
- p_stream_t *device_streams;
- device_streams = malloc(sizeof(p_stream_t)*BUS_DEVICE_LIMIT);
-
- initialize_devices(BUS_DEVICE_LIMIT, device_streams);
/* Add device-specific stream/thread declerations here, if needed */
/* ... */
@@ -163,7 +170,6 @@ static void *routing_func(void *pvArgs)
/* This function should go into its own file, with a normalized state machine implementation */
hs_status_t handshake(uint32_t i2c_addr)
{
-
/* Handshake variables */
uint8_t hs_sts = IDLE;
uint8_t *MDR_buf;
@@ -186,7 +192,7 @@ hs_status_t handshake(uint32_t i2c_addr)
}
else {
hs_sts = HS_MDR_ACK;
- }
+ }
break;
}
case (HS_MDR_ACK):
@@ -238,7 +244,7 @@ hs_status_t handshake(uint32_t i2c_addr)
device_info[dev_idx]->MDR = MDR_res_message;
hs_sts = HS_REGISTERED;
- }
+ }
}
break;
}