Skip to content

Commit

Permalink
Merge branch 'main' into echavemann/demo-code
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenewald authored Dec 10, 2024
2 parents b759d28 + 577792f commit efb9631
Show file tree
Hide file tree
Showing 28 changed files with 337 additions and 206 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ SOFTDEVICE_MODEL = blank

# ETL
ETL_INCLUDES = ./external/etl/include/etl/array.h
ETL_INCLUDES = ./external/etl/include/etl/variant.h
ETL_INCLUDES += ./external/etl/include/etl/vector.h
ETL_INCLUDES += ./external/etl/include/etl/optional.h
ETL_INCLUDES += ./external/etl/include/etl/string.h
Expand Down
9 changes: 8 additions & 1 deletion edge_os.ld
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,14 @@ SECTIONS
{
PROVIDE(__start_user_programs_data = .);
_build/user_*.o(.data*)
_build/user_*.o(.bss*)
/* _build/user_*.o(.bss*)
* bss needs to be within __bss_start__ and __bss_end__ to be zeroed by the c lib on startup
* however, that makes it really really hard to isolate
* I don't think this current isolation technique will work for bss, so I've enabled user read/write of all bss code
* If we made a real OS, we would have a different binary for each program, but because this is a mini os, we didn't bother.
*
* Because of this reason (and discovering it during finals week), I have disabled the MPU
* */
PROVIDE(__end_user_programs_data = .);
ASSERT((__end_user_programs_data - __start_user_programs_data) <= 16384, "Error: .user_programs_data section exceeds 16 KB");
} > RAM
Expand Down
46 changes: 46 additions & 0 deletions include/drivers/captouch_controller.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#pragma once

#include "drivers/driver_enums.hpp"
#include "drivers/gpio_pin.hpp"
#include "drivers/gpio_pin_event.hpp"
#include "microbit_v2.h"
#include "nrf_gpio.h"
#include "scheduler/user_callback_storage.hpp"
#include "util.hpp"

namespace edge::drivers {

class CapsenseController {
GPIOPin touch_logo{TOUCH_LOGO, GPIOConfiguration::IN_NORES};

UserCallbackStorage subscriptions;

bool touched;

bool prev_touched;

uint32_t time_test_started;

GPIOPinEvent event;

CapsenseController();
~CapsenseController() = default;

public:
CapsenseController(const CapsenseController&) = delete;
CapsenseController(CapsenseController&&) = delete;
CapsenseController& operator=(const CapsenseController&) = delete;
CapsenseController& operator=(CapsenseController&&) = delete;

static CapsenseController& get();

void subscribe_captouch_press(ProcessCallbackPtr callback, uint8_t process_id);

bool get_captouch_pressed();

void handle_gpio_interrupt(nrf_gpio_pin_sense_t sense, int pin);

void start_capacitive_test();
};

} // namespace edge::drivers
3 changes: 0 additions & 3 deletions include/drivers/driver_commands.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@

namespace edge::drivers {

// SHOULD BE DEPRECATED SOON
void do_async_work();

etl::optional<int> handle_command(DriverCommand type, int arg1, int arg2, int arg3);

etl::optional<int> handle_subscribe(
Expand Down
11 changes: 10 additions & 1 deletion include/drivers/driver_enums.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include "hal/hal_enums.hpp"
#include "util.hpp"

namespace edge::drivers {
Expand All @@ -9,9 +10,14 @@ enum class DriverCommand {
BUTTONS = 2,
TERMINAL_OUTPUT = 3,
TIMER_CANCEL = 4,
CAPTOUCH = 5,
};

enum class DriverSubscribe { NOTIFY_BUTTON_PRESS = 0, TIMER_START = 1 };
enum class DriverSubscribe {
NOTIFY_BUTTON_PRESS = 0,
TIMER_START = 1,
NOTIFY_CAPTOUCH = 2
};

enum class GPIOConfiguration { OUT, IN_NORES, IN_PDR, IN_PUR };

Expand All @@ -24,4 +30,7 @@ struct subscribe_callback {
int arg2;
};

aidan::PinPullMode to_pin_pull_mode(drivers::GPIOConfiguration config);
drivers::ButtonState to_cap_sense_state(nrf_gpio_pin_sense_t sense);

} // namespace edge::drivers
6 changes: 3 additions & 3 deletions include/drivers/gpio_pin_event.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ class GPIOPinEvent {
GPIOPinEvent(
uint8_t pin, GPIOConfiguration resistance,
aidan::GPIOEventController::GPIOEventCallback callback
) : pin(pin)
) :
pin(pin)
{
// TODO: add resistance
aidan::GPIOEventController::get().set_gpio_callback(
pin, aidan::PinPullMode::PUR, callback
pin, to_pin_pull_mode(resistance), callback
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#pragma once
#include "drivers/gpio_pin.hpp"
#include "drivers/virtual_timer_controller.hpp"
#include "microbit_v2.h"

namespace edge::drivers {

// Synchronous
class LedDisplay {
class LedMatrixController {
static constexpr uint8_t WIDTH = 5;
static constexpr uint8_t HEIGHT = 5;

Expand All @@ -27,15 +28,21 @@ class LedDisplay {
GPIOPin{LED_COL5, GPIOConfiguration::OUT}
};

void set_output(uint8_t row, uint8_t col, bool enabled);

public:
LedDisplay();
static LedMatrixController& get();

void set_led(uint8_t row, uint8_t col, bool enabled);

void do_async_work();
LedMatrixController(LedMatrixController&) = delete;
LedMatrixController(LedMatrixController&&) = delete;
LedMatrixController& operator=(VirtualTimerController&) = delete;
LedMatrixController& operator=(VirtualTimerController&&) = delete;

friend void handle_matrix_controller();

private:
LedMatrixController();
~LedMatrixController() = default;
};

extern LedDisplay led_display;
} // namespace edge::drivers
77 changes: 0 additions & 77 deletions include/drivers/timer.hpp

This file was deleted.

11 changes: 6 additions & 5 deletions include/drivers/virtual_timer_controller.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ void TIMER3_IRQHandler(void);
struct timer {
uint32_t id;

ProcessCallbackPtr callback;
etl::variant<KernelCallbackPtr, ProcessCallbackPtr> callback;

uint32_t timer_value;
ProcessId process_id;
Expand All @@ -35,11 +35,14 @@ class VirtualTimerController {

public:
uint32_t virtual_timer_start(
uint32_t microseconds, ProcessCallbackPtr callback, ProcessId timer_creator,
bool periodic
uint32_t microseconds,
etl::variant<KernelCallbackPtr, ProcessCallbackPtr> callback,
ProcessId timer_creator, bool periodic
);
void virtual_timer_cancel(uint32_t timer_id);

uint32_t read_timer() const;

static VirtualTimerController& get();

VirtualTimerController(VirtualTimerController&) = delete;
Expand All @@ -56,8 +59,6 @@ class VirtualTimerController {

void enqueue_next_timer() const;

uint32_t read_timer() const;

uint32_t timer_start(
uint32_t microseconds, ProcessCallbackPtr callback, ProcessId timer_creator
);
Expand Down
3 changes: 2 additions & 1 deletion include/scheduler/scheduler.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once
#include "config.hpp"
#include "util.hpp"
#include "task.hpp"
#include "util.hpp"

#include <stdio.h>

Expand Down Expand Up @@ -31,6 +31,7 @@ class Scheduler {
void change_current_task_priority(uint8_t new_priority);

void yield_current_task();
void restore_current_task_regs(exception_stack_registers* regs);

private:
void handle_first_svc_hit();
Expand Down
1 change: 1 addition & 0 deletions include/scheduler/task.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "config.hpp"
#include "util.hpp"

namespace edge {

Expand Down
4 changes: 4 additions & 0 deletions include/userlib/syscalls.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ void get_button_pressed(
void (*callback)(drivers::ButtonType, drivers::ButtonState)
);

bool get_captouch_pressed();

void subscribe_captouch_pressed(void (*callback)(drivers::ButtonState));

void send_ipc(const char* name, uint32_t message);

void subscribe_ipc(const char* name, void (*callback)(int message));
Expand Down
3 changes: 2 additions & 1 deletion include/userlib/system_call_type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ enum class SystemCallType : uint8_t {
COMMAND = 2,
SUBSCRIBE = 3,
IPC = 4,
SET_FAULT_HANDLER = 5
SET_FAULT_HANDLER = 5,
RESTORE_REGS = 6
};
}
1 change: 1 addition & 0 deletions include/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace edge {

// Arg1 and arg2 are optional
using ProcessCallbackPtr = void (*)(int arg1, int arg2);
using KernelCallbackPtr = etl::delegate<void()>;

using ProcessId = uint8_t;

Expand Down
62 changes: 62 additions & 0 deletions src/drivers/captouch_controller.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include "drivers/captouch_controller.hpp"

#include "config.hpp"
#include "microbit_v2.h"
#include "nrf_gpio.h"

namespace edge::drivers {

CapsenseController::CapsenseController() :
touched(false), prev_touched(false),
event{
TOUCH_LOGO, GPIOConfiguration::IN_NORES,
aidan::GPIOEventController::GPIOEventCallback::create<
CapsenseController, &CapsenseController::handle_gpio_interrupt>(*this)
}
{
touch_logo.clear();
nrf_gpio_cfg(
TOUCH_LOGO, NRF_GPIO_PIN_DIR_OUTPUT, NRF_GPIO_PIN_INPUT_DISCONNECT,
NRF_GPIO_PIN_NOPULL, NRF_GPIO_PIN_S0S1, NRF_GPIO_PIN_NOSENSE
);
};

CapsenseController& CapsenseController::get()
{
static CapsenseController capsenseController;
return capsenseController;
}

void CapsenseController::subscribe_captouch_press(
ProcessCallbackPtr callback, uint8_t process_id
)
{
subscriptions.set_callback(process_id, callback);
}

bool CapsenseController::get_captouch_pressed()
{
return touched;
}

void CapsenseController::handle_gpio_interrupt(nrf_gpio_pin_sense_t sense, int pin)
{
if (sense == NRF_GPIO_PIN_SENSE_LOW) {
touched = true;
}
else if (sense == NRF_GPIO_PIN_SENSE_HIGH) {
touched = false;
}
if ((prev_touched != touched)) {
for (int process_id = 0; process_id < MAX_PROCESSES; ++process_id) {
if (subscriptions.has_callback(process_id)) {
subscriptions.call_callback(
process_id, static_cast<int>(to_cap_sense_state(sense))
);
}
}
}
prev_touched = touched;
}

} // namespace edge::drivers
Loading

0 comments on commit efb9631

Please sign in to comment.