Skip to content

Commit

Permalink
Merge pull request #127 from brilliantlabsAR/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
siliconwitch authored Feb 22, 2023
2 parents c8fb70a + b1d78f6 commit e1298e7
Show file tree
Hide file tree
Showing 16 changed files with 341 additions and 220 deletions.
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -103,19 +103,20 @@ SRC_C += monocle-core/monocle-startup.c
SRC_C += mphalport.c

SRC_C += micropython/extmod/moduasyncio.c
SRC_C += micropython/extmod/moduselect.c
SRC_C += micropython/extmod/modubinascii.c
SRC_C += micropython/extmod/moduhashlib.c
SRC_C += micropython/extmod/modujson.c
SRC_C += micropython/extmod/modurandom.c
SRC_C += micropython/extmod/modure.c
SRC_C += micropython/extmod/moduselect.c
SRC_C += micropython/extmod/utime_mphal.c
SRC_C += modules/bluetooth.c
SRC_C += modules/camera.c
SRC_C += modules/device.c
SRC_C += modules/display.c
SRC_C += modules/fpga.c
SRC_C += modules/led.c
SRC_C += modules/bluetooth.c
SRC_C += modules/storage.c
SRC_C += modules/time.c
SRC_C += modules/touch.c
SRC_C += modules/update.c
Expand Down
163 changes: 64 additions & 99 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <stdint.h>

#include "monocle.h"
#include "bluetooth.h"
#include "touch.h"
#include "config-tables.h"

Expand Down Expand Up @@ -70,9 +71,9 @@ static struct ble_handles_t
{
uint16_t connection;
uint8_t advertising;
ble_gatts_char_handles_t repl_rx_unused;
ble_gatts_char_handles_t repl_rx_write;
ble_gatts_char_handles_t repl_tx_notification;
ble_gatts_char_handles_t data_rx_unused;
ble_gatts_char_handles_t data_rx_write;
ble_gatts_char_handles_t data_tx_notification;
} ble_handles = {
.connection = BLE_CONN_HANDLE_INVALID,
Expand Down Expand Up @@ -105,16 +106,6 @@ static struct ble_ring_buffer_t
.buffer = "",
.head = 0,
.tail = 0,
},
data_rx = {
.buffer = "",
.head = 0,
.tail = 0,
},
data_tx = {
.buffer = "",
.head = 0,
.tail = 0,
};

bool ble_are_tx_notifications_enabled(ble_tx_channel_t channel)
Expand Down Expand Up @@ -154,6 +145,11 @@ bool ble_are_tx_notifications_enabled(ble_tx_channel_t channel)
return false;
}

size_t ble_get_max_payload_size(void)
{
return ble_negotiated_mtu;
}

static bool ble_send_repl_data(void)
{
if (ble_handles.connection == BLE_CONN_HANDLE_INVALID)
Expand Down Expand Up @@ -208,7 +204,7 @@ static bool ble_send_repl_data(void)
return false;
}

static bool ble_send_raw_data(void)
bool ble_send_raw_data(const uint8_t *bytes, size_t len)
{
if (ble_handles.connection == BLE_CONN_HANDLE_INVALID)
{
Expand All @@ -220,64 +216,21 @@ static bool ble_send_raw_data(void)
return true;
}

if (data_tx.head == data_tx.tail)
{
return true;
}

uint8_t tx_buffer[BLE_PREFERRED_MAX_MTU] = "";
uint16_t tx_length = 0;

uint16_t buffered_tail = data_tx.tail;

while (buffered_tail != data_tx.head)
{
tx_buffer[tx_length++] = data_tx.buffer[buffered_tail++];

if (buffered_tail == sizeof(data_tx.buffer))
{
buffered_tail = 0;
}

if (tx_length == ble_negotiated_mtu)
{
break;
}
}

// Initialise the handle value parameters
ble_gatts_hvx_params_t hvx_params = {0};
hvx_params.handle = ble_handles.data_tx_notification.value_handle;
hvx_params.p_data = tx_buffer;
hvx_params.p_len = (uint16_t *)&tx_length;
hvx_params.p_data = bytes;
hvx_params.p_len = (uint16_t *)&len;
hvx_params.type = BLE_GATT_HVX_NOTIFICATION;

uint32_t status = sd_ble_gatts_hvx(ble_handles.connection, &hvx_params);

if (status == NRF_SUCCESS)
{
data_tx.tail = buffered_tail;
return false;
}

return false;
}

void ble_buffer_raw_tx_data(const uint8_t *bytes, size_t len)
{
for (uint16_t position = 0; position < len; position++)
{
while (data_tx.head == data_tx.tail - 1)
{
MICROPY_EVENT_POLL_HOOK;
}

data_tx.buffer[data_tx.head++] = bytes[position];

if (data_tx.head == sizeof(data_tx.buffer))
{
data_tx.head = 0;
}
}
return true;
}

void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len)
Expand All @@ -300,10 +253,9 @@ void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len)

int mp_hal_stdin_rx_chr(void)
{
if (repl_rx.head == repl_rx.tail)
while (repl_rx.head == repl_rx.tail)
{
MICROPY_EVENT_POLL_HOOK;
return 0;
}

uint16_t next = repl_rx.tail + 1;
Expand All @@ -326,14 +278,41 @@ static void touch_interrupt_handler(nrfx_gpiote_pin_t pin,
(void)pin;
(void)polarity;

/*
// Read the interrupt registers
i2c_response_t global_reg_0x11 = i2c_read(TOUCH_I2C_ADDRESS, 0x11, 0xFF);
i2c_response_t sar_ui_reg_0x12 = i2c_read(TOUCH_I2C_ADDRESS, 0x12, 0xFF);
i2c_response_t sar_ui_reg_0x13 = i2c_read(TOUCH_I2C_ADDRESS, 0x13, 0xFF);
*/
touch_action_t touch_action = A_TOUCH; // TODO this should be decoded from the I2C responses
touch_event_handler(touch_action);
i2c_response_t interrupt = i2c_read(TOUCH_I2C_ADDRESS, 0x12, 0xFF);
app_err(interrupt.fail);

if (interrupt.value & 0x10)
{
touch_event_handler(TOUCH_A);
}

if (interrupt.value & 0x20)
{
touch_event_handler(TOUCH_B);
}
}

touch_action_t touch_get_state(void)
{
i2c_response_t interrupt = i2c_read(TOUCH_I2C_ADDRESS, 0x12, 0xFF);
app_err(interrupt.fail);

if ((interrupt.value & 0x30) == 0x30)
{
return TOUCH_BOTH;
}

if (interrupt.value & 0x10)
{
return TOUCH_A;
}

if (interrupt.value & 0x20)
{
return TOUCH_B;
}

return TOUCH_NONE;
}

void unused_rtc_event_handler(nrfx_rtc_int_type_t int_type) {}
Expand Down Expand Up @@ -444,7 +423,7 @@ void SD_EVT_IRQHandler(void)
{
// If REPL service
if (ble_evt->evt.gatts_evt.params.write.handle ==
ble_handles.repl_rx_unused.value_handle)
ble_handles.repl_rx_write.value_handle)
{
for (uint16_t i = 0;
i < ble_evt->evt.gatts_evt.params.write.len;
Expand Down Expand Up @@ -482,33 +461,13 @@ void SD_EVT_IRQHandler(void)

// If data service
if (ble_evt->evt.gatts_evt.params.write.handle ==
ble_handles.data_rx_unused.value_handle)
ble_handles.data_rx_write.value_handle)
{
for (uint16_t i = 0;
i < ble_evt->evt.gatts_evt.params.write.len;
i++)
{
uint16_t next = data_rx.head + 1;

if (next == sizeof(data_rx.buffer))
{
next = 0;
}

if (next == data_rx.tail)
{
break;
}

data_rx.buffer[data_rx.head] =
ble_evt->evt.gatts_evt.params.write.data[i];

data_rx.head = next;
}
bluetooth_receive_callback_handler(
ble_evt->evt.gatts_evt.params.write.data,
ble_evt->evt.gatts_evt.params.write.len);
}

// TODO if data service

break;
}

Expand Down Expand Up @@ -577,7 +536,12 @@ int main(void)

// Check if external flash has an FPGA image and boot it
{
// TODO
uint8_t wakeup_device_id[] = {bit_reverse(0xAB), 0, 0, 0};
spi_write(FLASH, wakeup_device_id, 4, true);
spi_read(FLASH, wakeup_device_id, 1);
app_err(bit_reverse(wakeup_device_id[0]) != 0x13);

// TODO check flash for FPGA image

// Otherwise boot from the internal image of the FPGA
nrf_gpio_pin_write(FPGA_CS_INT_MODE_PIN, false);
Expand Down Expand Up @@ -846,7 +810,7 @@ int main(void)
app_err(sd_ble_gatts_characteristic_add(repl_service_handle,
&rx_char_md,
&rx_attr,
&ble_handles.repl_rx_unused));
&ble_handles.repl_rx_write));

app_err(sd_ble_gatts_characteristic_add(repl_service_handle,
&tx_char_md,
Expand All @@ -864,7 +828,7 @@ int main(void)
app_err(sd_ble_gatts_characteristic_add(data_service_handle,
&rx_char_md,
&rx_attr,
&ble_handles.data_rx_unused));
&ble_handles.data_rx_write));

app_err(sd_ble_gatts_characteristic_add(data_service_handle,
&tx_char_md,
Expand Down Expand Up @@ -956,7 +920,8 @@ int main(void)

void mp_event_poll_hook(void)
{
if (ble_send_repl_data() && ble_send_raw_data())
// Keep sending REPL data. Then if no more data is pending
if (ble_send_repl_data())
{
extern void mp_handle_pending(bool);
mp_handle_pending(true);
Expand Down
53 changes: 47 additions & 6 deletions modules/bluetooth.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@
#include "py/runtime.h"
#include "py/objarray.h"

static mp_obj_t receive_callback = mp_const_none;

void bluetooth_receive_callback_handler(const uint8_t *bytes, size_t len)
{
if (receive_callback != mp_const_none)
{
mp_obj_t array = mp_obj_new_bytes(bytes, len);
mp_sched_schedule(receive_callback, array);
}
}

static mp_obj_t bluetooth_send(mp_obj_t buffer_in)
{
if (!ble_are_tx_notifications_enabled(DATA_TX))
Expand All @@ -37,18 +48,41 @@ static mp_obj_t bluetooth_send(mp_obj_t buffer_in)

mp_buffer_info_t array;
mp_get_buffer_raise(buffer_in, &array, MP_BUFFER_READ);
ble_buffer_raw_tx_data(array.buf, array.len);

if (array.len > ble_get_max_payload_size())
{
mp_raise_msg(&mp_type_ValueError,
MP_ERROR_TEXT("input buffer is too large"));
}

if (ble_send_raw_data(array.buf, array.len))
{
mp_raise_msg(&mp_type_OSError,
MP_ERROR_TEXT("raw data service is busy"));
}

return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(bluetooth_send_obj, bluetooth_send);

static mp_obj_t bluetooth_receive(void)
static mp_obj_t bluetooth_receive_callback(size_t n_args, const mp_obj_t *args)
{
// TODO
return mp_const_notimplemented;
if (n_args == 0)
{
return receive_callback;
}

if (!mp_obj_is_callable(args[0]) && (args[0] != mp_const_none))
{
mp_raise_ValueError(
MP_ERROR_TEXT("callback must be None or a callable object"));
}

receive_callback = args[0];

return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(bluetooth_receive_obj, bluetooth_receive);
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(bluetooth_receive_callback_obj, 0, 1, bluetooth_receive_callback);

static mp_obj_t bluetooth_connected(void)
{
Expand All @@ -58,10 +92,17 @@ static mp_obj_t bluetooth_connected(void)
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(bluetooth_connected_obj, bluetooth_connected);

STATIC mp_obj_t bluetooth_max_length(void)
{
return mp_obj_new_int(ble_get_max_payload_size());
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(bluetooth_max_length_obj, bluetooth_max_length);

STATIC const mp_rom_map_elem_t bluetooth_module_globals_table[] = {
{MP_ROM_QSTR(MP_QSTR_send), MP_ROM_PTR(&bluetooth_send_obj)},
{MP_ROM_QSTR(MP_QSTR_receive), MP_ROM_PTR(&bluetooth_receive_obj)},
{MP_ROM_QSTR(MP_QSTR_receive_callback), MP_ROM_PTR(&bluetooth_receive_callback_obj)},
{MP_ROM_QSTR(MP_QSTR_connected), MP_ROM_PTR(&bluetooth_connected_obj)},
{MP_ROM_QSTR(MP_QSTR_max_length), MP_ROM_PTR(&bluetooth_max_length_obj)},
};
STATIC MP_DEFINE_CONST_DICT(bluetooth_module_globals, bluetooth_module_globals_table);

Expand Down
Loading

0 comments on commit e1298e7

Please sign in to comment.