summaryrefslogtreecommitdiff
path: root/deps/fff/examples/weak_linking/src
diff options
context:
space:
mode:
authorOmniscient <17525998+omnisci3nce@users.noreply.github.com>2024-02-24 22:47:46 +1100
committerOmniscient <17525998+omnisci3nce@users.noreply.github.com>2024-02-24 22:47:46 +1100
commit7b3afcaf77f96e7d62f6cd1623ead7f17512d79f (patch)
treeb5f82c64e9c06a84e4d095ab4ac48712e860b673 /deps/fff/examples/weak_linking/src
parentb047be5252aeb981faea077409c1768fda0301d9 (diff)
repo init. partial port of existing code
Diffstat (limited to 'deps/fff/examples/weak_linking/src')
-rw-r--r--deps/fff/examples/weak_linking/src/bus.c30
-rw-r--r--deps/fff/examples/weak_linking/src/display.c29
-rw-r--r--deps/fff/examples/weak_linking/src/error.c10
-rw-r--r--deps/fff/examples/weak_linking/src/main.c27
-rw-r--r--deps/fff/examples/weak_linking/src/sensor.c29
5 files changed, 125 insertions, 0 deletions
diff --git a/deps/fff/examples/weak_linking/src/bus.c b/deps/fff/examples/weak_linking/src/bus.c
new file mode 100644
index 0000000..5706932
--- /dev/null
+++ b/deps/fff/examples/weak_linking/src/bus.c
@@ -0,0 +1,30 @@
+#pragma once
+
+uint16_t* BUS_REGISTER_BASE = (uint16_t*)0xDEADBEEF;
+
+typedef struct
+{
+ uint16_t action;
+ uint16_t reg;
+ uint8_t input;
+ uint8_t output
+} BusDevice;
+
+BusDevice* BUS = (BusDevice*)BUS_REGISTER_BASE;
+
+bool bus_read_write( uint8_t dev, uint8_t registry, uint8_t* buffer, int len, bool assume_echo )
+{
+ // Something that we dont want to run, since the emulation would be hard.
+ BUS->action = 0x01;
+ BUS->reg = registry;
+ for ( int loop =0 ; loop < len; loop ++ )
+ {
+ char output = buffer[loop];
+ BUS->output = output;
+ buffer[loop] = BUS->input;
+
+ if ( ( assume_echo ) && ( buffer[loop] != output ) )
+ return false;
+ }
+ return true;
+}
diff --git a/deps/fff/examples/weak_linking/src/display.c b/deps/fff/examples/weak_linking/src/display.c
new file mode 100644
index 0000000..1b9a7d6
--- /dev/null
+++ b/deps/fff/examples/weak_linking/src/display.c
@@ -0,0 +1,29 @@
+#include <string.h>
+
+#include "display.h"
+#include "bus.h"
+
+#define DISPLAY_ADDRESS 0xAF
+#define DISPLAY_REG_INIT 0x10
+#define DISPLAY_REG_UPDATE 0x20
+
+bool display_init()
+{
+ char init_config[] = { 0xDE, 0xFE, 0x00 };
+ bus_read_write( DISPLAY_ADDRESS, DISPLAY_REG_INIT, (uint8_t*)init_config, 3, false );
+
+ if (init_config[2] != 0x10)
+ return false;
+
+ return true;
+}
+
+void display_update( const char* info )
+{
+ int len = strlen( info );
+
+ if ( bus_write( DISPLAY_ADDRESS, DISPLAY_REG_UPDATE, (const uint8_t*)info, len, true ) != true )
+ {
+ runtime_error("display update failed!");
+ }
+}
diff --git a/deps/fff/examples/weak_linking/src/error.c b/deps/fff/examples/weak_linking/src/error.c
new file mode 100644
index 0000000..5e7db63
--- /dev/null
+++ b/deps/fff/examples/weak_linking/src/error.c
@@ -0,0 +1,10 @@
+#include <assert.h>
+#include "error.h"
+
+char* runtime_error_nice_print( const char* msg )
+{
+ char* buffer = malloc(512);
+ snprintf(buffer, 512, "Got error: %s", msg );
+ buffer[511] = 0;
+ return buffer;
+}
diff --git a/deps/fff/examples/weak_linking/src/main.c b/deps/fff/examples/weak_linking/src/main.c
new file mode 100644
index 0000000..88be84c
--- /dev/null
+++ b/deps/fff/examples/weak_linking/src/main.c
@@ -0,0 +1,27 @@
+#include <stdlib.h>
+#include <stdio.h>
+
+#include "display.h"
+#include "sensor.h"
+
+void give_error( const char* msg )
+{
+ char* buffer = runtime_error_nice_print( msg );
+ runtime_error( buffer );
+ free(buffer);
+}
+
+int update_main( void )
+{
+ if ( !sensor_init() )
+ runtime_error("sensor init failed");
+
+ if ( !display_init() )
+ runtime_error("display init failed");
+
+ char buffer[32];
+ float reading = sensor_read();
+ snprintf( buffer, 32, "Sensor: %0.1f", reading );
+ display_update( buffer );
+ return 0;
+}
diff --git a/deps/fff/examples/weak_linking/src/sensor.c b/deps/fff/examples/weak_linking/src/sensor.c
new file mode 100644
index 0000000..bff6642
--- /dev/null
+++ b/deps/fff/examples/weak_linking/src/sensor.c
@@ -0,0 +1,29 @@
+#include <string.h>
+
+#include "sensor.h"
+#include "bus.h"
+
+#define SENSOR_ADDRESS 0xAA
+#define SENSOR_REG_READ 0xF0
+
+float LOCAL_buffer[4];
+
+bool sensor_init()
+{
+ memset( LOCAL_buffer, 0x00, sizeof(LOCAL_buffer));
+ return true;
+}
+
+float sensor_read()
+{
+ char data[4] = { 0xFF, 0xFF, 0x00, 0x00 };
+ bus_read_write( SENSOR_ADDRESS, SENSOR_REG_READ, (uint8_t*)&data, 4, false );
+
+ if (data[0] != 0x10)
+ {
+ runtime_error("sensor read failed");
+ }
+
+ float ret_value = data[1] + data[2] / 256.0;
+ return ret_value;
+}