Skip to content

Commit

Permalink
Close #121 - Side filament sensors for XL
Browse files Browse the repository at this point in the history
  • Loading branch information
vintagepc committed Apr 30, 2023
1 parent 68ac9bd commit 32f4104
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 38 deletions.
56 changes: 43 additions & 13 deletions hw/arm/prusa/parts/hall_sensor.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "hw/irq.h"
#include "qom/object.h"
#include "hw/sysbus.h"
#include "hw/qdev-properties.h"

#define TYPE_HALL_SENSOR "hall-sensor"

Expand All @@ -41,9 +42,12 @@ struct HallState {
/*< private >*/
/*< public >*/
uint8_t state;
uint32_t present_val;
uint32_t missing_val;
qemu_irq irq;
qemu_irq status;

uint8_t index;
p404_key_handle p_key;
};

enum {
Expand All @@ -57,23 +61,19 @@ enum {
STATE_RUNOUT
};

static const int hall_values[3] = {2, 1048575, 250};

OBJECT_DEFINE_TYPE_SIMPLE_WITH_INTERFACES(HallState, hall_sensor, HALL_SENSOR, SYS_BUS_DEVICE, {TYPE_P404_SCRIPTABLE}, {TYPE_P404_KEYCLIENT},{NULL})


static void hall_sensor_finalize(Object *obj)
{
}

static void hall_sensor_update(HallState *s) {
qemu_set_irq(s->irq,hall_values[s->state]);
printf("Hall FS state: %d\n",hall_values[s->state]);
static void hall_sensor_update(HallState *s)
{
uint32_t values[3] = {2, s->present_val, s->missing_val};
qemu_set_irq(s->irq,values[s->state]);
qemu_set_irq(s->status,s->state == STATE_PRESENT);
}



static void hall_sensor_toggle(HallState *s)
{
if (s->state == STATE_RUNOUT)
Expand Down Expand Up @@ -117,9 +117,30 @@ static int hall_sensor_process_action(P404ScriptIF *obj, unsigned int action, sc
static void hall_sensor_input_handle_key(P404KeyIF *opaque, Key keycode)
{
HallState *s = HALL_SENSOR(opaque);
if (keycode == 'f')
switch (keycode)
{
hall_sensor_toggle(s);
case 'f':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
hall_sensor_toggle(s);
break;
}
}

static void hall_sensor_realize(DeviceState *dev, Error **errp)
{
HallState *s = HALL_SENSOR(dev);
if (s->index == 0)
{
p404_register_keyhandler(s->p_key, 'f',"Toggles filament sensor state");
}
else
{
p404_register_keyhandler(s->p_key, '0' + s->index ,"Toggles filament sensor state");
}
}

Expand All @@ -137,8 +158,7 @@ static void hall_sensor_init(Object *obj)
script_register_action(pScript, "Toggle", "Toggles hall filament sensor enum state", ACT_TOGGLE);

scripthost_register_scriptable(pScript);
p404_key_handle pKey = p404_new_keyhandler(P404_KEYCLIENT(obj));
p404_register_keyhandler(pKey, 'f',"Toggles filament sensor state");
s->p_key = p404_new_keyhandler(P404_KEYCLIENT(obj));
}

static const VMStateDescription vmstate_hall_sensor = {
Expand All @@ -151,11 +171,21 @@ static const VMStateDescription vmstate_hall_sensor = {
}
};

static Property hall_sensor_properties[] = {
DEFINE_PROP_UINT8("index",HallState, index, 0),
DEFINE_PROP_UINT32("present-value",HallState, present_val, 1048575),
DEFINE_PROP_UINT32("missing-value",HallState, missing_val, 250),
DEFINE_PROP_END_OF_LIST(),
};

static void hall_sensor_class_init(ObjectClass *oc, void *data)
{
DeviceClass *dc = DEVICE_CLASS(oc);
dc->reset = hall_sensor_reset;
dc->realize = hall_sensor_realize;
dc->vmsd = &vmstate_hall_sensor;

device_class_set_props(dc, hall_sensor_properties);
P404ScriptIFClass *sc = P404_SCRIPTABLE_CLASS(oc);
sc->ScriptHandler = hall_sensor_process_action;

Expand Down
71 changes: 47 additions & 24 deletions hw/arm/prusa/prusa-xl.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ typedef struct xl_cfg_t {
stm_pin z_min;
bool has_at21;
int e_table_index;
bool has_hbr_pt100;
uint8_t motor;
char m_label[AXIS_MAX];
bool m_inversion[AXIS_MAX];
Expand Down Expand Up @@ -122,7 +121,6 @@ static const xl_cfg_t xl_cfg = {
.z_min = STM_PIN(GPIOB, 8),
.has_at21 = false, // NOT IMPLEMENTED YET
.e_table_index = 2005,
.has_hbr_pt100 = false,
.motor = TMC2130,
.m_label = {'A','B','Z','E'},
.m_inversion = {1,1,0,0},
Expand Down Expand Up @@ -153,7 +151,6 @@ static const xl_cfg_t xl_cfg_050 = {
.z_min = STM_PIN(GPIOB, 8),
.has_at21 = false, // NOT IMPLEMENTED YET
.e_table_index = 2005,
.has_hbr_pt100 = false,
.motor = TMC2130,
.m_label = {'A','B','Z','E'},
.m_inversion = {1,0,0,0},
Expand Down Expand Up @@ -337,11 +334,25 @@ static void xl_init(MachineState *machine, xl_cfg_t cfg)
sysbus_realize(SYS_BUS_DEVICE(gl_db), &error_fatal);
#endif

enum {
IND_Z,
IND_RGB,
IND_WHITE,
IND_CHEESE,
IND_F_UNUSED,
IND_FS0,
IND_FS1,
IND_FS2,
IND_FS3,
IND_FS4,
IND_FS5
};

DeviceState *db2 = qdev_new("2d-dashboard");
static const char* links[4] = {"motor[0]","motor[1]","motor[4]"};
qdev_prop_set_uint8(db2, "fans", 0);
qdev_prop_set_uint8(db2, "thermistors", 0);
qdev_prop_set_string(db2, "indicators", "ZFRWC");
qdev_prop_set_string(db2, "indicators", "ZRWCF123456");

{
static int32_t stepsize[4] = { 80*16, 80*16, 800*16, 400*16 };
Expand Down Expand Up @@ -463,24 +474,21 @@ static void xl_init(MachineState *machine, xl_cfg_t cfg)

// Sidebar RGB
qdev_connect_gpio_out_named(npixel[4],"colour",0,
qdev_get_gpio_in_named(db2,"led-rgb",2)
qdev_get_gpio_in_named(db2,"led-rgb",IND_RGB)
);
// Sidebar W
qdev_connect_gpio_out_named(npixel[5],"rgb-out",0,
qdev_get_gpio_in_named(db2,"led-w",3)
qdev_get_gpio_in_named(db2,"led-w",IND_WHITE)
);
// Cheese LED
qdev_connect_gpio_out_named(npixel[5],"rgb-out",1,
qdev_get_gpio_in_named(db2,"led-w",4)
qdev_get_gpio_in_named(db2,"led-w",IND_CHEESE)
);


uint16_t startvals[] = {18,20, 25, 512, 20};
uint8_t channels[] = {10,4,3,5,6};
int tables[] = {cfg.e_table_index, 1, 2000,0,5};
if (cfg.has_hbr_pt100) {
tables[4] = 22;
}
const int tables[] = {cfg.e_table_index, 1, 2000,0,5};
for (int i=0; i<5; i++)
{
dev = qdev_new("thermistor");
Expand All @@ -505,26 +513,41 @@ static void xl_init(MachineState *machine, xl_cfg_t cfg)
sysbus_realize(SYS_BUS_DEVICE(lc), &error_fatal);
qdev_connect_gpio_out_named(motors[2],"um-out",0,qdev_get_gpio_in(lc,0));

DeviceState *hs = qdev_new("hall-sensor");
sysbus_realize(SYS_BUS_DEVICE(hs), &error_fatal);
#ifdef BUDDY_HAS_GL
qdev_connect_gpio_out_named(hs, "status", 0,qdev_get_gpio_in_named(gl_db,"indicator-analog",DB_IND_FSENS));
#endif

dev = qdev_new("hx717");
sysbus_realize(SYS_BUS_DEVICE(dev), &error_fatal);
qdev_connect_gpio_out(dev, 0, qdev_get_gpio_in(stm32_soc_get_periph(dev_soc, BANK(cfg.hx717_data)),PIN(cfg.hx717_data))); // EXTR_DATA
qdev_connect_gpio_out(stm32_soc_get_periph(dev_soc, BANK(cfg.hx717_sck)),PIN(cfg.hx717_sck),qdev_get_gpio_in(dev, 0)); // EXTR_SCK

// PT100
qdev_connect_gpio_out(lc,0, qdev_get_gpio_in_named(dev,"input_x1000",0));
if (cfg.has_hbr_pt100)
{
//qdev_connect_gpio_out_named(hotend, "value_x1000",0, qdev_get_gpio_in_named(dev,"input_x1000",1));
}
else

// Hall sensor mux
dev = qdev_new("hc4052");
qdev_prop_set_bit(dev, "debug",true);
sysbus_realize(SYS_BUS_DEVICE(dev), &error_fatal);
qdev_connect_gpio_out(stm32_soc_get_periph(dev_soc, STM32_P_GPIOF),12,qdev_get_gpio_in_named(dev,"select", 1)); // S0
qdev_connect_gpio_out(stm32_soc_get_periph(dev_soc, STM32_P_GPIOG),6,qdev_get_gpio_in_named(dev, "select", 0)); // S1
qdev_connect_gpio_out_named(stm32_soc_get_periph(dev_soc, STM32_P_ADC3),"adc_read", 10, qdev_get_gpio_in_named(dev, "adc_read_request",0));
qdev_connect_gpio_out_named(stm32_soc_get_periph(dev_soc, STM32_P_ADC3),"adc_read", 4, qdev_get_gpio_in_named(dev, "adc_read_request",1));
qdev_connect_gpio_out(dev,0, qdev_get_gpio_in_named(stm32_soc_get_periph(dev_soc, STM32_P_ADC3),"adc_data_in", 10));
qdev_connect_gpio_out(dev,1, qdev_get_gpio_in_named(stm32_soc_get_periph(dev_soc, STM32_P_ADC3),"adc_data_in", 4));

#define N_HALL 6
DeviceState* hall[N_HALL];
for (int i=0; i<N_HALL; i++)
{
qdev_connect_gpio_out(hs,0, qdev_get_gpio_in_named(dev,"input_x1000",1));
hall[i] = qdev_new("hall-sensor");
qdev_prop_set_uint8(hall[i],"index",1U + i);
qdev_prop_set_uint32(hall[i],"present-value",4010);
sysbus_realize(SYS_BUS_DEVICE(hall[i]), &error_fatal);
qdev_connect_gpio_out_named(hall[i],"status", 0, qdev_get_gpio_in_named(db2,"led-digital",IND_FS0 + i));
if (i<4)
{
qdev_connect_gpio_out(hall[i], 0, qdev_get_gpio_in_named(dev,"1Y", i));
}
else
{
qdev_connect_gpio_out(hall[i], 0, qdev_get_gpio_in_named(dev,"2Y", i-4));
}
}


Expand Down
8 changes: 7 additions & 1 deletion hw/arm/prusa/stm32f407/stm32f4xx_adc.c
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,13 @@ static void stm32f4xx_adc_schedule_next(STM32F4XXADCState *s) {
conv_cycles += adc_lookup_smpr(s->adc_smprs[channel]);

uint64_t delay_ns = 1000000000000U / (clock/conv_cycles);
// printf("ADC conversion: %u cycles @ %"PRIu64" Hz (%lu nSec)\n", conv_cycles, clock, delay_ns);
if (s->parent.periph == STM32_P_ADC3)
{
// Yes, this is an ugly-ass hack. The above calc is off by 1000 and I still need to determine
// how to deal with the other channels bogging down the simulation when they run at the "real" specified rate.
delay_ns /= 1000;
//printf("ADC conversion: %u cycles @ %"PRIu64" Hz (%lu nSec)\n", conv_cycles, clock, delay_ns);
}
timer_mod_ns(s->next_eoc, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL)+delay_ns);

}
Expand Down
4 changes: 4 additions & 0 deletions hw/arm/prusa/utility/KeyController.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ class KeyController: private Scriptable
{ {0x21 ,false} , 'f'},
{ {0x02, false}, '1'},
{ {0x03, false}, '2'},
{ {0x04, false}, '3'},
{ {0x05, false}, '4'},
{ {0x06, false}, '5'},
{ {0x07, false}, '6'},
{ {0x19 ,false} , 'p'},
{ {0x14 ,false} , 't'}
};
Expand Down

0 comments on commit 32f4104

Please sign in to comment.