Skip to content

Commit

Permalink
Store peripheral state in store
Browse files Browse the repository at this point in the history
  • Loading branch information
pipe01 committed Dec 18, 2024
1 parent 444dab4 commit 6395e37
Show file tree
Hide file tree
Showing 18 changed files with 171 additions and 78 deletions.
1 change: 1 addition & 0 deletions include/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ static inline uint32_t size_mask(byte_size_t size)

typedef enum {
OP_RESET = 0,
OP_LOAD_DATA = 0xFF,
OP_READ_BYTE = SIZE_BYTE,
OP_READ_HALFWORD = SIZE_HALFWORD,
OP_READ_WORD = SIZE_WORD,
Expand Down
1 change: 1 addition & 0 deletions include/peripherals/nrf52832/ppi.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ typedef void (*ppi_task_cb_t)(PPI_t *, uint8_t peripheral, uint8_t task, void *u
PPI_t *ppi_new(cpu_t **cpu);

void ppi_add_peripheral(PPI_t *, uint8_t id, ppi_task_cb_t cb, void *userdata);
void ppi_replace_peripheral(PPI_t *, uint8_t id, ppi_task_cb_t cb, void *userdata);
void ppi_remove_peripheral(PPI_t *, uint8_t id);
void ppi_fire_task(PPI_t *, uint8_t peripheral_id, uint8_t task_id);

Expand Down
11 changes: 10 additions & 1 deletion include/state_store.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,19 @@ typedef uint16_t state_key_t;

#define PERIPHERAL_KEY(id) (0xFF00 | (id))

enum
{
STATE_KEY_POWER = 1,
STATE_KEY_CLOCK,

STATE_KEY_SPIM0 = 0x0100,
STATE_KEY_TWIM0 = 0x0200,
};

state_store_t *state_store_new();
void state_store_free(state_store_t *);

void *state_store_alloc(state_store_t *, state_key_t key, size_t size);
void state_store_register(state_store_t *store, state_key_t key, void *data, size_t size);
void state_store_freeze(state_store_t *);

uint8_t *state_store_save(state_store_t *, size_t *size);
Expand Down
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ set(WASM_EXPORTS

set(LIBS m tiny-aes capstone)

add_compile_options(-Werror -Wall -Wextra -Wno-unused-parameter)
add_compile_options(-Werror -Wall -Wextra -Wno-unused-parameter -fms-extensions)

set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)
Expand Down
4 changes: 3 additions & 1 deletion src/peripherals/nrf52832/clock.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ enum

OPERATION(clock)
{
CLOCK_t *clock = (CLOCK_t *)userdata;
CLOCK_t *clock = userdata;

if (op == OP_RESET)
{
Expand Down Expand Up @@ -150,6 +150,8 @@ NRF52_PERIPHERAL_CONSTRUCTOR(CLOCK, clock)
{
CLOCK_t *clock = malloc(sizeof(CLOCK_t));

state_store_register(ctx.state_store, STATE_KEY_CLOCK, clock, sizeof(*clock));

ppi_add_peripheral(ctx.ppi, ctx.id, clock_task_handler, clock);

return clock;
Expand Down
15 changes: 10 additions & 5 deletions src/peripherals/nrf52832/ecb.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ enum
EVENTS_ERRORECB = 0x104, // ECB block encrypt aborted because of a STOPECB task or due to an error
};


typedef struct
{
union
Expand All @@ -27,7 +26,6 @@ typedef struct
uint32_t value;
} inten_t;


typedef struct
{
uint8_t key[16];
Expand All @@ -36,12 +34,17 @@ typedef struct
} ecbdata_t;
static_assert(sizeof(ecbdata_t) == 48, "ecbdata_t size is not 48 bytes");

struct ECB_inst_t
typedef struct
{
dma_t *dma;

uint32_t ecbdataptr;
inten_t inten;
} state_t;

struct ECB_inst_t
{
state_t;

dma_t *dma;
};

OPERATION(ecb)
Expand Down Expand Up @@ -113,6 +116,8 @@ NRF52_PERIPHERAL_CONSTRUCTOR(ECB, ecb)
ECB_t *ecb = malloc(sizeof(ECB_t));
ecb->dma = ctx.dma;

state_store_register(ctx.state_store, PERIPHERAL_KEY(ctx.id), ecb, sizeof(state_t));

ppi_add_peripheral(ctx.ppi, ctx.id, ecb_task_handler, ecb);

return ecb;
Expand Down
13 changes: 10 additions & 3 deletions src/peripherals/nrf52832/gpiote.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,19 @@ typedef union
uint32_t value;
} inten_t;

struct GPIOTE_inst_t
typedef struct
{
pins_t *pins;

inten_t inten;
config_t config[8];

uint32_t latch_old;
} state_t;

struct GPIOTE_inst_t
{
state_t;

pins_t *pins;
};

OPERATION(gpiote)
Expand Down Expand Up @@ -203,6 +208,8 @@ NRF52_PERIPHERAL_CONSTRUCTOR(GPIOTE, gpiote)
GPIOTE_t *gpiote = malloc(sizeof(GPIOTE_t));
gpiote->pins = ctx.pins;

state_store_register(ctx.state_store, PERIPHERAL_KEY(ctx.id), gpiote, sizeof(state_t));

ppi_add_peripheral(current_ppi, ctx.id, gpiote_task_handler, gpiote);

return gpiote;
Expand Down
7 changes: 5 additions & 2 deletions src/peripherals/nrf52832/nvmc.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ typedef union

struct NVMC_inst_t
{
config_t config;

uint8_t *data;
size_t size;

config_t config;
};

OPERATION(nvmc)
Expand Down Expand Up @@ -90,5 +90,8 @@ NRF52_PERIPHERAL_CONSTRUCTOR(NVMC, nvmc, uint8_t *data, size_t size)
NVMC_t *nvmc = malloc(sizeof(NVMC_t));
nvmc->data = data;
nvmc->size = size;

state_store_register(ctx.state_store, PERIPHERAL_KEY(ctx.id), nvmc, sizeof(config_t));

return nvmc;
}
6 changes: 5 additions & 1 deletion src/peripherals/nrf52832/power.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,9 @@ OPERATION(power)

NRF52_PERIPHERAL_CONSTRUCTOR(POWER, power)
{
return malloc(sizeof(POWER_t));
POWER_t *power = malloc(sizeof(POWER_t));

state_store_register(ctx.state_store, STATE_KEY_POWER, power, sizeof(POWER_t));

return power;
}
13 changes: 13 additions & 0 deletions src/peripherals/nrf52832/ppi.c
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,19 @@ void ppi_add_peripheral(PPI_t *ppi, uint8_t id, ppi_task_cb_t cb, void *userdata
ppi->peripherals[id] = peripheral;
}

void ppi_replace_peripheral(PPI_t *ppi, uint8_t id, ppi_task_cb_t cb, void *userdata)
{
assert(id <= PERIPHERALS_COUNT - 1);

peripheral_t *peripheral = ppi->peripherals[id];

if (!peripheral)
ppi->peripherals[id] = peripheral = malloc(sizeof(peripheral_t));

peripheral->cb = cb;
peripheral->userdata = userdata;
}

void ppi_remove_peripheral(PPI_t *ppi, uint8_t id)
{
assert(id < PERIPHERALS_COUNT);
Expand Down
65 changes: 33 additions & 32 deletions src/peripherals/nrf52832/rtc.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ typedef struct

struct RTC_inst_t
{
state_t *s;
state_t;

cpu_t **cpu;
ticker_t *ticker;
Expand All @@ -48,31 +48,31 @@ void rtc_tick(void *userdata)
{
RTC_t *rtc = userdata;

rtc->s->counter++;
rtc->counter++;

ppi_fire_event(current_ppi, rtc->id, EVENT_ID(RTC_EVENTS_TICK), rtc->s->inten.TICK);
ppi_fire_event(current_ppi, rtc->id, EVENT_ID(RTC_EVENTS_TICK), rtc->inten.TICK);

if (rtc->s->counter == (1 << 24))
if (rtc->counter == (1 << 24))
{
rtc->s->counter = 0;
rtc->counter = 0;

ppi_fire_event(current_ppi, rtc->id, EVENT_ID(RTC_EVENTS_OVRFLW), rtc->s->inten.OVRFLW);
ppi_fire_event(current_ppi, rtc->id, EVENT_ID(RTC_EVENTS_OVRFLW), rtc->inten.OVRFLW);
}

for (size_t i = 0; i < rtc->cc_num; i++)
{
if (rtc->s->counter == rtc->s->cc[i])
ppi_fire_event(current_ppi, rtc->id, EVENT_ID(RTC_EVENTS_COMPARE0) + i, rtc->s->inten.COMPARE & (1 << i));
if (rtc->counter == rtc->cc[i])
ppi_fire_event(current_ppi, rtc->id, EVENT_ID(RTC_EVENTS_COMPARE0) + i, rtc->inten.COMPARE & (1 << i));
}
}

OPERATION(rtc)
{
state_t *state = ((RTC_t *)userdata)->s;
state_t *rtc = userdata;

if (op == OP_RESET)
{
memset(state, 0, sizeof(state_t));
memset(rtc, 0, sizeof(state_t));
return MEMREG_RESULT_OK;
}

Expand All @@ -90,32 +90,32 @@ OPERATION(rtc)
OP_EVENT(RTC_EVENTS_COMPARE2)
OP_EVENT(RTC_EVENTS_COMPARE3)

OP_INTENSET(state)
OP_INTENCLR(state)
OP_INTENSET(rtc)
OP_INTENCLR(rtc)

case 0x340: // EVTEN
OP_RETURN_REG(state->evten.value, WORD);
OP_RETURN_REG(rtc->evten.value, WORD);

case 0x344: // EVTENSET
OP_RETURN_REG_SET(state->evten.value, WORD);
OP_RETURN_REG_SET(rtc->evten.value, WORD);

case 0x348: // EVTENCLR
OP_RETURN_REG_CLR(state->evten.value, WORD);
OP_RETURN_REG_CLR(rtc->evten.value, WORD);

case 0x504: // COUNTER
OP_RETURN_REG(state->counter, WORD);
OP_RETURN_REG(rtc->counter, WORD);

case 0x508: // PRESCALER
if (OP_IS_READ(op))
{
*value = state->prescaler;
*value = rtc->prescaler;
}
else
{
if (state->running)
if (rtc->running)
fault_take(FAULT_RTC_INVALID_STATE);

state->prescaler = *value;
rtc->prescaler = *value;
}

return MEMREG_RESULT_OK;
Expand All @@ -125,39 +125,39 @@ OPERATION(rtc)
{
uint32_t idx = (offset - 0x540) / 4;

OP_RETURN_REG(state->cc[idx], WORD);
OP_RETURN_REG(rtc->cc[idx], WORD);
}

return MEMREG_RESULT_UNHANDLED;
}

PPI_TASK_HANDLER(rtc_task_handler)
{
RTC_t *rtc = (RTC_t *)userdata;
RTC_t *rtc = userdata;

switch (task)
{
case TASK_ID(RTC_TASKS_START):
if (!rtc->s->running)
if (!rtc->running)
{
ticker_add(rtc->ticker, CLOCK_LFCLK, rtc_tick, rtc, rtc->s->prescaler + 1, true);
ticker_add(rtc->ticker, CLOCK_LFCLK, rtc_tick, rtc, rtc->prescaler + 1, true);

rtc->s->running = true;
rtc->running = true;
}
break;

case TASK_ID(RTC_TASKS_STOP):
if (rtc->s->running)
if (rtc->running)
{
ticker_remove(rtc->ticker, CLOCK_LFCLK, rtc_tick);

rtc->s->running = false;
rtc->running = false;
}
break;

case TASK_ID(RTC_TASKS_CLEAR):
rtc->s->counter = 0;
rtc->s->prescaler_counter = 0;
rtc->counter = 0;
rtc->prescaler_counter = 0;
break;
}
}
Expand All @@ -171,7 +171,8 @@ NRF52_PERIPHERAL_CONSTRUCTOR(RTC, rtc, size_t cc_num)
rtc->cpu = ctx.cpu;
rtc->id = ctx.id;
rtc->ticker = ctx.ticker;
rtc->s = state_store_alloc(ctx.state_store, PERIPHERAL_KEY(ctx.id), sizeof(state_t));

state_store_register(ctx.state_store, PERIPHERAL_KEY(ctx.id), rtc, sizeof(state_t));

ppi_add_peripheral(ctx.ppi, ctx.id, rtc_task_handler, rtc);

Expand All @@ -180,15 +181,15 @@ NRF52_PERIPHERAL_CONSTRUCTOR(RTC, rtc, size_t cc_num)

uint32_t rtc_is_running(RTC_t *rtc)
{
return rtc->s->running;
return rtc->running;
}

uint32_t rtc_get_counter(RTC_t *rtc)
{
return rtc->s->counter;
return rtc->counter;
}

double rtc_get_tick_interval_us(RTC_t *rtc)
{
return ((double)(rtc->s->prescaler + 1) * 1e6) / 32768.0;
return ((double)(rtc->prescaler + 1) * 1e6) / 32768.0;
}
11 changes: 9 additions & 2 deletions src/peripherals/nrf52832/spi.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,17 @@ enum

#define READ_BUFFER_SIZE 50

typedef struct
{
bool enabled;
} state_t;

struct SPI_inst_t
{
state_t;

uint8_t id;
bus_spi_t *bus;
bool enabled;
};

PPI_TASK_HANDLER(spi_task_handler)
Expand Down Expand Up @@ -112,7 +118,8 @@ NRF52_PERIPHERAL_CONSTRUCTOR(SPI, spi)
SPI_t *spi = malloc(sizeof(SPI_t));
spi->bus = ctx.spi;
spi->id = ctx.id;
spi->enabled = false;

state_store_register(ctx.state_store, PERIPHERAL_KEY(ctx.id), spi, sizeof(state_t));

return spi;
}
Loading

0 comments on commit 6395e37

Please sign in to comment.