Prusa MINI Firmware overview
filament_sensor.c File Reference
#include "filament_sensor.h"
#include "hwio_pindef.h"
#include "stm32f4xx_hal.h"
#include "gpio.h"
#include "eeprom.h"
#include "FreeRTOS.h"
#include "task.h"
#include "cmsis_os.h"
#include "marlin_client.h"

Classes

struct  status_t
 

Enumerations

enum  send_M600_on_t { M600_on_edge = 0, M600_on_level = 1, M600_never = 2 }
 

Functions

static void _set_state (fsensor_t st)
 
static void _enable ()
 
static void _disable ()
 
fsensor_t fs_get_state ()
 
int fs_did_filament_runout ()
 
void fs_send_M600_on_edge ()
 
void fs_send_M600_on_level ()
 
void fs_send_M600_never ()
 
void fs_enable ()
 
void fs_disable ()
 
fsensor_t fs_wait_inicialized ()
 
static void _init ()
 
void fs_init_on_edge ()
 
void fs_init_on_level ()
 
void fs_init_never ()
 
static void _injectM600 ()
 
static void _cycle0 ()
 
static void _cycle1 ()
 
void fs_cycle ()
 

Variables

static volatile fsensor_t state = FS_NOT_INICIALIZED
 
static volatile fsensor_t last_state = FS_NOT_INICIALIZED
 
static status_t status = { 0, M600_on_edge, 0 }
 

Enumeration Type Documentation

◆ send_M600_on_t

Enumerator
M600_on_edge 
M600_on_level 
M600_never 
26  {
27  M600_on_edge = 0,
28  M600_on_level = 1,
29  M600_never = 2

Function Documentation

◆ _set_state()

static void _set_state ( fsensor_t  st)
static
44  {
46  if (last_state == st)
47  state = st;
48  last_state = st;
50 }
Here is the caller graph for this function:

◆ _enable()

static void _enable ( )
static
Here is the call graph for this function:
Here is the caller graph for this function:

◆ _disable()

static void _disable ( )
static
59  {
62  status.meas_cycle = 0;
63 }
Here is the caller graph for this function:

◆ fs_get_state()

fsensor_t fs_get_state ( )
67  {
68  return state;
69 }
Here is the caller graph for this function:

◆ fs_did_filament_runout()

int fs_did_filament_runout ( )
72  {
73  return state == FS_NO_FILAMENT;
74 }
Here is the caller graph for this function:

◆ fs_send_M600_on_edge()

void fs_send_M600_on_edge ( )
76  {
78 }
Here is the caller graph for this function:

◆ fs_send_M600_on_level()

void fs_send_M600_on_level ( )
80  {
82 }
Here is the caller graph for this function:

◆ fs_send_M600_never()

void fs_send_M600_never ( )
84  {
86 }
Here is the caller graph for this function:

◆ fs_enable()

void fs_enable ( )
Here is the call graph for this function:
Here is the caller graph for this function:

◆ fs_disable()

void fs_disable ( )
Here is the call graph for this function:
Here is the caller graph for this function:

◆ fs_wait_inicialized()

fsensor_t fs_wait_inicialized ( )
104  {
105  fsensor_t ret = fs_get_state();
106  while (ret == FS_NOT_INICIALIZED) {
107  osDelay(0); // switch to other threads
108  ret = fs_get_state();
109  }
110  return ret;
111 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ _init()

static void _init ( )
static
115  {
116  int enabled = eeprom_get_var(EEVAR_FSENSOR_ENABLED).ui8 ? 1 : 0;
117 
118  if (enabled)
119  _enable();
120  else
121  _disable();
122 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ fs_init_on_edge()

void fs_init_on_edge ( )
124  {
125  _init();
127 }
Here is the call graph for this function:

◆ fs_init_on_level()

void fs_init_on_level ( )
128  {
129  _init();
131 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ fs_init_never()

void fs_init_never ( )
132  {
133  _init();
135 }
Here is the call graph for this function:

◆ _injectM600()

static void _injectM600 ( )
static
139  {
141  if (vars->sd_printing && (!vars->wait_user) /*&& (!vars->wait_heat)*/) {
142  marlin_gcode_push_front("M600"); //change filament
143  status.M600_sent = 1;
144  }
145 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ _cycle0()

static void _cycle0 ( )
static
147  {
148  if (gpio_get(PIN_FSENSOR) == 1) {
150  status.meas_cycle = 1; //next cycle shall be 1
151  } else {
152  int had_filament = state == FS_HAS_FILAMENT ? 1 : 0;
153  _set_state(FS_NO_FILAMENT); //it is filtered, 2 requests are needed to change state
154  //M600_on_edge == inject after state was changed from FS_HAS_FILAMENT to FS_NO_FILAMENT
155  //M600_on_level == inject on FS_NO_FILAMENT
156  //M600_never == do not inject
157  if (status.M600_sent == 0 && state == FS_NO_FILAMENT) {
158  switch (status.send_M600_on) {
159  case M600_on_edge:
160  if (!had_filament)
161  break;
162  //if had_filament == 1 - do not break
163  case M600_on_level:
164  _injectM600();
165  break;
166  case M600_never:
167  default:
168  break;
169  }
170  }
171 
172  status.meas_cycle = 0; //remain in cycle 0
173  }
174 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ _cycle1()

static void _cycle1 ( )
static
177  {
178  //pulldown was set in cycle 0
181  status.meas_cycle = 0; //next cycle shall be 0
182 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ fs_cycle()

void fs_cycle ( )
185  {
186  //sensor is disabled (only init can enable it)
187  if (state == FS_DISABLED)
188  return;
189 
190  //sensor is enabled
191  if (status.meas_cycle == 0) {
192  _cycle0();
193  } else {
194  _cycle1();
195  }
196 
197  //clear M600_sent status if marlin is paused
199  status.M600_sent = 0;
200  }
201 }
Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ state

volatile fsensor_t state = FS_NOT_INICIALIZED
static

◆ last_state

volatile fsensor_t last_state = FS_NOT_INICIALIZED
static

◆ status

status_t status = { 0, M600_on_edge, 0 }
static
M600_on_edge
Definition: filament_sensor.c:27
fs_send_M600_on_edge
void fs_send_M600_on_edge()
Definition: filament_sensor.c:76
variant8_ui8
variant8_t variant8_ui8(uint8_t ui8)
Definition: variant8.c:18
status_t::meas_cycle
uint8_t meas_cycle
Definition: filament_sensor.c:35
EEVAR_FSENSOR_ENABLED
#define EEVAR_FSENSOR_ENABLED
Definition: eeprom.h:17
send_M600_on_t
send_M600_on_t
Definition: filament_sensor.c:26
FS_DISABLED
Definition: filament_sensor.h:22
eeprom_get_var
variant8_t eeprom_get_var(uint8_t id)
Definition: eeprom.c:75
_variant8_t::ui8
uint8_t ui8
Definition: variant8.h:36
marlin_gcode_push_front
void marlin_gcode_push_front(const char *gcode)
Definition: marlin_client.c:222
state
static volatile fsensor_t state
Definition: filament_sensor.c:23
eeprom_set_var
void eeprom_set_var(uint8_t id, variant8_t var)
Definition: eeprom.c:88
fs_send_M600_never
void fs_send_M600_never()
Definition: filament_sensor.c:84
FS_NOT_INICIALIZED
Definition: filament_sensor.h:18
_disable
static void _disable()
Definition: filament_sensor.c:59
GPIO_PULLUP
#define GPIO_PULLUP
Definition: stm32f4xx_hal_gpio.h:171
osDelay
osStatus osDelay(uint32_t millisec)
Wait for Timeout (Time Delay)
Definition: cmsis_os.c:365
gpio_get
static int gpio_get(uint8_t pin8)
Definition: gpio.h:94
MARLIN_VAR_WAITUSER
#define MARLIN_VAR_WAITUSER
Definition: marlin_vars.h:28
FS_HAS_FILAMENT
Definition: filament_sensor.h:19
taskENTER_CRITICAL
#define taskENTER_CRITICAL()
Definition: task.h:217
_cycle1
static void _cycle1()
Definition: filament_sensor.c:177
GPIO_PULLDOWN
#define GPIO_PULLDOWN
Definition: stm32f4xx_hal_gpio.h:172
gpio_init
static void gpio_init(uint8_t pin8, uint32_t mode, uint32_t pull, uint32_t speed)
Definition: gpio.h:102
fs_send_M600_on_level
void fs_send_M600_on_level()
Definition: filament_sensor.c:80
MARLIN_VAR_WAITHEAT
#define MARLIN_VAR_WAITHEAT
Definition: marlin_vars.h:27
_injectM600
static void _injectM600()
Definition: filament_sensor.c:139
FS_NO_FILAMENT
Definition: filament_sensor.h:20
fs_get_state
fsensor_t fs_get_state()
Definition: filament_sensor.c:67
GPIO_SPEED_FREQ_VERY_HIGH
#define GPIO_SPEED_FREQ_VERY_HIGH
Definition: stm32f4xx_hal_gpio.h:161
marlin_update_vars
marlin_vars_t * marlin_update_vars(uint64_t msk)
Definition: marlin_client.c:368
_marlin_vars_t::wait_user
uint8_t wait_user
Definition: marlin_vars.h:92
PIN_FSENSOR
#define PIN_FSENSOR
Definition: hwio_pindef.h:44
MARLIN_VAR_MSK
#define MARLIN_VAR_MSK(v_id)
Definition: marlin_vars.h:35
_set_state
static void _set_state(fsensor_t st)
Definition: filament_sensor.c:44
status_t::M600_sent
uint8_t M600_sent
Definition: filament_sensor.c:33
M600_never
Definition: filament_sensor.c:29
status_t::send_M600_on
uint8_t send_M600_on
Definition: filament_sensor.c:34
MARLIN_VAR_SD_PRINT
#define MARLIN_VAR_SD_PRINT
Definition: marlin_vars.h:29
M600_on_level
Definition: filament_sensor.c:28
status
static status_t status
Definition: filament_sensor.c:37
FS_NOT_CONNECTED
Definition: filament_sensor.h:21
_marlin_vars_t
Definition: marlin_vars.h:77
last_state
static volatile fsensor_t last_state
Definition: filament_sensor.c:24
_cycle0
static void _cycle0()
Definition: filament_sensor.c:147
fsensor_t
fsensor_t
Definition: filament_sensor.h:17
taskEXIT_CRITICAL
#define taskEXIT_CRITICAL()
Definition: task.h:232
GPIO_MODE_INPUT
#define GPIO_MODE_INPUT
Definition: stm32f4xx_hal_gpio.h:135
_init
static void _init()
Definition: filament_sensor.c:115
_marlin_vars_t::sd_printing
uint8_t sd_printing
Definition: marlin_vars.h:93
_enable
static void _enable()
Definition: filament_sensor.c:52