Skip to content

Commit

Permalink
All the states
Browse files Browse the repository at this point in the history
  • Loading branch information
pipe01 committed Dec 17, 2024
1 parent 3e9a673 commit 2cabb5c
Show file tree
Hide file tree
Showing 38 changed files with 322 additions and 153 deletions.
3 changes: 2 additions & 1 deletion include/bus_spi.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <stdint.h>

#include "pins.h"
#include "state_store.h"

typedef struct bus_spi_t bus_spi_t;

Expand All @@ -28,7 +29,7 @@ typedef struct
spi_cs_changed_f cs_changed;
} spi_slave_t;

bus_spi_t *bus_spi_new(pins_t *pins, uint8_t *ram, size_t ram_size);
bus_spi_t *bus_spi_new(pins_t *pins, uint8_t *ram, size_t ram_size, state_store_t *store);
void bus_spi_reset(bus_spi_t *);
void bus_spi_free(bus_spi_t *);
void bus_spi_step(bus_spi_t *);
Expand Down
3 changes: 2 additions & 1 deletion include/components/i2c/bma425.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include "bus_i2c.h"
#include "state_store.h"

i2c_slave_t bma425_new();
i2c_slave_t bma425_new(state_store_t *store);
3 changes: 2 additions & 1 deletion include/components/i2c/cst816s.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "bus_i2c.h"
#include "pins.h"
#include "state_store.h"

typedef enum
{
Expand All @@ -17,7 +18,7 @@ typedef enum

typedef struct cst816s_t cst816s_t;

cst816s_t *cst816s_new(pins_t *pins, int irqPin);
cst816s_t *cst816s_new(pins_t *pins, state_store_t *store, int irqPin);
i2c_slave_t cst816s_get_slave(cst816s_t *);

void cst816s_do_touch(cst816s_t *, touch_gesture_t gesture, uint16_t x, uint16_t y);
Expand Down
3 changes: 2 additions & 1 deletion include/components/i2c/hrs3300.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#pragma once

#include "bus_i2c.h"
#include "state_store.h"

typedef struct hrs3300_t hrs3300_t;

hrs3300_t *hrs3300_new();
hrs3300_t *hrs3300_new(state_store_t *store);
i2c_slave_t hrs3300_get_slave(hrs3300_t *);

void hrs3300_set_ch0(hrs3300_t *, uint32_t value);
Expand Down
3 changes: 2 additions & 1 deletion include/components/spi/spinorflash.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#pragma once

#include "bus_spi.h"
#include "state_store.h"

#include <stddef.h>

typedef struct spinorflash_t spinorflash_t;

spinorflash_t *spinorflash_new(size_t size, size_t sector_size);
spinorflash_t *spinorflash_new(state_store_t *store, size_t size, size_t sector_size);
spi_slave_t spinorflash_get_slave(spinorflash_t *);

size_t spinorflash_get_write_count(spinorflash_t *);
Expand Down
2 changes: 1 addition & 1 deletion include/components/spi/st7789.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ typedef struct st7789_t st7789_t;
#define DISPLAY_BUFFER_HEIGHT 320
#define BYTES_PER_PIXEL 2 // We assume 16bpp format

st7789_t* st7789_new();
st7789_t* st7789_new(state_store_t *store);
spi_slave_t st7789_get_slave(st7789_t *);

void st7789_read_screen(st7789_t *, uint8_t *data, size_t width, size_t height);
Expand Down
2 changes: 1 addition & 1 deletion include/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
#endif

#ifndef ENABLE_MEASUREMENT
#define ENABLE_MEASUREMENT 0
#define ENABLE_MEASUREMENT 1
#endif

#ifndef ABORT_ON_INVALID_MEM_ACCESS
Expand Down
3 changes: 2 additions & 1 deletion include/cpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
#include "arm.h"
#include "memory.h"
#include "runlog.h"
#include "state_store.h"

typedef struct cpu_inst_t cpu_t;

typedef void (*branch_cb_t)(cpu_t *, uint32_t old_pc, uint32_t new_pc, void *userdata);
typedef void (*mem_watchpoint_cb_t)(cpu_t *, bool isWrite, uint32_t addr, size_t size, uint32_t value_old, uint32_t value_new, void *userdata);

cpu_t *cpu_new(const uint8_t *program, size_t program_size, memory_map_t *mem, size_t max_external_interrupts, size_t priority_bits);
cpu_t *cpu_new(const uint8_t *program, size_t program_size, memory_map_t *mem, state_store_t *store, size_t max_external_interrupts, size_t priority_bits);
void cpu_free(cpu_t *);
void cpu_reset(cpu_t *);
// Returns the number of cycles that the CPU ran for
Expand Down
5 changes: 4 additions & 1 deletion include/nrf52832.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "bus_i2c.h"
#include "bus_spi.h"
#include "program.h"
#include "state_store.h"
#include "peripherals/nrf52832/rtc.h"

#define NRF52832_SRAM_SIZE 0x10000
Expand Down Expand Up @@ -84,7 +85,7 @@ enum

typedef struct NRF52832_inst_t NRF52832_t;

NRF52832_t *nrf52832_new(const program_t *flash, size_t sram_size);
NRF52832_t *nrf52832_new(const program_t *flash, size_t sram_size, state_store_t *store);
void nrf52832_reset(NRF52832_t *);
int nrf52832_step(NRF52832_t *);

Expand All @@ -99,3 +100,5 @@ size_t nrf52832_get_sram_size(NRF52832_t *);
uint64_t nrf52832_get_cycle_counter(NRF52832_t *);

bool nrf52832_flash_write(NRF52832_t *, uint32_t addr, uint8_t value);

void nrf52832_reload_state(NRF52832_t *);
2 changes: 1 addition & 1 deletion include/peripherals/dcb.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

#include "peripherals/peripheral.h"

PERIPHERAL(DCB, dcb)
PERIPHERAL(DCB, dcb, state_store_t *store)
2 changes: 1 addition & 1 deletion include/peripherals/dwt.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

#define DWT_CYCCNTENA 0

PERIPHERAL(DWT, dwt)
PERIPHERAL(DWT, dwt, state_store_t *store)

void dwt_increment_cycle(DWT_t *dwt, unsigned int count);
2 changes: 1 addition & 1 deletion include/peripherals/nvic.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

#include "cpu.h"

PERIPHERAL(NVIC, nvic, cpu_t *cpu, size_t priority_bits)
PERIPHERAL(NVIC, nvic, cpu_t *cpu, state_store_t *store, size_t priority_bits)
2 changes: 1 addition & 1 deletion include/peripherals/scb.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ typedef union

static_assert(sizeof(SCB_CCR_t) == 4, "SCB_CCR_t size is incorrect");

PERIPHERAL(SCB, scb, cpu_t *cpu)
PERIPHERAL(SCB, scb, cpu_t *cpu, state_store_t *store)

uint32_t scb_get_prigroup(SCB_t *);
SCB_CCR_t scb_get_ccr(SCB_t *);
Expand Down
2 changes: 1 addition & 1 deletion include/peripherals/scb_fp.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include "arm.h"

PERIPHERAL(SCB_FP, scb_fp)
PERIPHERAL(SCB_FP, scb_fp, state_store_t *store)

FPCCR_t scb_fp_get_fpccr(SCB_FP_t *scb_fp);
uint32_t scb_fp_get_fpscr(SCB_FP_t *scb_fp);
Expand Down
3 changes: 3 additions & 0 deletions include/pinetime.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,6 @@ st7789_t *pinetime_get_st7789(pinetime_t *);
cst816s_t *pinetime_get_cst816s(pinetime_t *);
hrs3300_t *pinetime_get_hrs3300(pinetime_t *);
spinorflash_t *pinetime_get_spinorflash(pinetime_t *pt);

uint8_t *pinetime_save_state(pinetime_t *pt, size_t *size);
bool pinetime_load_state(pinetime_t *pt, uint8_t *data, size_t size);
4 changes: 3 additions & 1 deletion include/pins.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#define PINS_COUNT 32

#include "state_store.h"

#include <stdbool.h>
#include <stdint.h>

Expand Down Expand Up @@ -34,7 +36,7 @@ typedef enum
#define pins_set_input(pins, pin) pins_set_dir(pins, pin, pins_get_dir(pins, pin) & ~PIN_OUTPUT)
#define pins_set_output(pins, pin) pins_set_dir(pins, pin, pins_get_dir(pins, pin) | PIN_OUTPUT)

pins_t *pins_new(void);
pins_t *pins_new(state_store_t *store);
void pins_free(pins_t *);

void pins_reset(pins_t *);
Expand Down
23 changes: 19 additions & 4 deletions include/state_store.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,26 @@ typedef uint16_t state_key_t;

enum
{
STATE_KEY_POWER = 1,
STATE_KEY_CPU = 1,
STATE_KEY_MEMORY,
STATE_KEY_PINS,
STATE_KEY_BUS_SPI,
STATE_KEY_DCB,
STATE_KEY_DWT,
STATE_KEY_NVIC,
STATE_KEY_SCB_FP,
STATE_KEY_SCB,
STATE_KEY_BMA425,
STATE_KEY_CST816S,
STATE_KEY_HRS3300,
STATE_KEY_SPINORFLASH,
STATE_KEY_ST7789,

STATE_KEY_POWER = 0x0100,
STATE_KEY_CLOCK,

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

state_store_t *state_store_new();
Expand All @@ -25,4 +40,4 @@ void state_store_register(state_store_t *store, state_key_t key, void *data, siz
void state_store_freeze(state_store_t *);

uint8_t *state_store_save(state_store_t *, size_t *size);
bool state_store_load(state_store_t *, uint8_t *data, size_t size);
bool state_store_load(state_store_t *, const uint8_t *data, size_t size);
30 changes: 30 additions & 0 deletions include/util.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#pragma once

#include <stddef.h>
#include <stdint.h>
#include <stdio.h>

static inline uint8_t *read_file_u8(const char *path, size_t *size)
{
FILE *f = fopen(path, "rb");
if (f == NULL)
{
fprintf(stderr, "Failed to open %s\n", path);
return NULL;
}

fseek(f, 0, SEEK_END);
long fsize = ftell(f);
fseek(f, 0, SEEK_SET);

uint8_t *data = malloc(fsize);
if (fread(data, 1, fsize, f) != (size_t)fsize)
{
fprintf(stderr, "Failed to read %s\n", path);
return NULL;
}
fclose(f);

*size = fsize;
return data;
}
13 changes: 10 additions & 3 deletions src/bus_spi.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,30 @@

struct bus_spi_t
{
struct state
{
bool was_selected[MAX_SLAVES];
};

uint8_t *ram;
size_t ram_size;

bool was_selected[MAX_SLAVES];
spi_slave_t *slaves[MAX_SLAVES];
uint8_t slave_pin[MAX_SLAVES];
size_t slave_count;

pins_t *pins;
};

bus_spi_t *bus_spi_new(pins_t *pins, uint8_t *ram, size_t ram_size)
bus_spi_t *bus_spi_new(pins_t *pins, uint8_t *ram, size_t ram_size, state_store_t *store)
{
bus_spi_t *spi = (bus_spi_t *)calloc(1, sizeof(bus_spi_t));
bus_spi_t *spi = calloc(1, sizeof(bus_spi_t));
spi->pins = pins;
spi->ram = ram;
spi->ram_size = ram_size;

state_store_register(store, STATE_KEY_BUS_SPI, spi, sizeof(struct state));

return spi;
}

Expand Down
4 changes: 3 additions & 1 deletion src/component/i2c/bma425.c
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,12 @@ size_t bma425_read(uint8_t *data, size_t data_size, void *userdata)
return data_size;
}

i2c_slave_t bma425_new()
i2c_slave_t bma425_new(state_store_t *store)
{
bma425_t *bma425 = malloc(sizeof(bma425_t));

state_store_register(store, STATE_KEY_BMA425, bma425, sizeof(bma425_t));

return (i2c_slave_t){
.userdata = bma425,
.write = bma425_write,
Expand Down
19 changes: 12 additions & 7 deletions src/component/i2c/cst816s.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,17 @@ typedef struct __attribute__((packed))

struct cst816s_t
{
pins_t *pins;
int irqPin;
struct state
{
uint8_t next_read[MAX_READ_SIZE];
size_t next_read_size;

uint8_t next_read[MAX_READ_SIZE];
size_t next_read_size;
touchdata_t touchdata;
bool has_touch;
};

touchdata_t touchdata;
bool has_touch;
pins_t *pins;
int irqPin;
};

void cst816s_reset(void *userdata)
Expand Down Expand Up @@ -114,12 +117,14 @@ size_t cst816s_read(uint8_t *data, size_t data_size, void *userdata)
return data_size;
}

cst816s_t *cst816s_new(pins_t *pins, int irqPin)
cst816s_t *cst816s_new(pins_t *pins, state_store_t *store, int irqPin)
{
cst816s_t *cst816s = calloc(1, sizeof(cst816s_t));
cst816s->pins = pins;
cst816s->irqPin = irqPin;

state_store_register(store, STATE_KEY_CST816S, cst816s, sizeof(struct state));

return cst816s;
}

Expand Down
8 changes: 6 additions & 2 deletions src/component/i2c/hrs3300.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,13 @@ size_t hrs3300_read(uint8_t *data, size_t data_size, void *userdata)
return data_size;
}

hrs3300_t *hrs3300_new()
hrs3300_t *hrs3300_new(state_store_t *store)
{
return calloc(1, sizeof(hrs3300_t));
hrs3300_t *hrs = calloc(1, sizeof(hrs3300_t));

state_store_register(store, STATE_KEY_HRS3300, hrs, sizeof(hrs3300_t));

return hrs;
}

i2c_slave_t hrs3300_get_slave(hrs3300_t *hrs3300)
Expand Down
Loading

0 comments on commit 2cabb5c

Please sign in to comment.