Prusa MINI Firmware overview
uartslave.c File Reference
#include "uartslave.h"
#include <stdarg.h>
#include "stm32f4xx_hal.h"
#include "dbg.h"

Functions

int uartslave_parse_mod_mask (uartslave_t *pslave, char *pstr, uint16_t *pmod_msk)
 
int uartslave_parse_cmd_id (uartslave_t *pslave, char *pstr, uint16_t *pcmd_id)
 
int uartslave_do_cmd (uartslave_t *pslave, uint16_t mod_msk, char cmd, uint16_t cmd_id, char *pstr)
 
void uartslave_init (uartslave_t *pslave, uartrxbuff_t *prxbuff, int size, char *pline)
 
void uartslave_cycle (uartslave_t *pslave)
 
int uartslave_printf (uartslave_t *pslave, const char *fmt,...)
 

Function Documentation

◆ uartslave_parse_mod_mask()

int uartslave_parse_mod_mask ( uartslave_t pslave,
char *  pstr,
uint16_t *  pmod_msk 
)
10  {
11  int ret = 0;
12  char ch;
13  do {
14  ch = *pstr++;
15  switch (ch) {
16  case '!':
17  case '?':
18  if (ret == 0)
19  *pmod_msk = UARTSLAVE_MOD_MSK_0;
20  else if (ret == 3)
21 #ifdef UARTSLAVE_TST
22  if (strncmp(pstr - 4, "tst", 3) == 0)
23  *pmod_msk = 1;
24 #else //UARTSLAVE_TST
25  *pmod_msk = UARTSLAVE_MOD_MSK_0;
26 #endif //UARTSLAVE_TST
27  return ret;
28  }
29  ret++;
30  } while (ret <= 3);
31  return UARTSLAVE_ERR_SYN;
32 }
Here is the caller graph for this function:

◆ uartslave_parse_cmd_id()

int uartslave_parse_cmd_id ( uartslave_t pslave,
char *  pstr,
uint16_t *  pcmd_id 
)
34  {
35  uint16_t cmd_id = UARTSLAVE_CMD_ID_UNK;
36  if (pstr[0] == 0) {
37  *pcmd_id = UARTSLAVE_CMD_ID_0;
38  return 0;
39  } else if ((pstr[3] == 0) || (pstr[3] == ' ')) {
40 #ifdef UARTSLAVE_TST
41  if (strncmp(pstr, "tst", 3) == 0)
42  cmd_id = 1;
43 #endif //UARTSLAVE_TST
44  if (cmd_id != UARTSLAVE_CMD_ID_UNK) {
45  *pcmd_id = cmd_id;
46  return 3;
47  }
48  }
49  return UARTSLAVE_ERR_SYN;
50 }
Here is the caller graph for this function:

◆ uartslave_do_cmd()

int uartslave_do_cmd ( uartslave_t pslave,
uint16_t  mod_msk,
char  cmd,
uint16_t  cmd_id,
char *  pstr 
)
52  {
53  _dbg0("uartslave_do_cmd: pstr='%s', cmd='%c', mod_msk=%d, cmd_id = %d", pstr ? pstr : "null", cmd, mod_msk, cmd_id);
54  return UARTSLAVE_OK;
55 }
Here is the caller graph for this function:

◆ uartslave_init()

void uartslave_init ( uartslave_t pslave,
uartrxbuff_t prxbuff,
int  size,
char *  pline 
)
57  {
58  pslave->prxbuff = prxbuff;
59  pslave->flags = 0;
60  pslave->count = 0;
61  pslave->size = size;
62  pslave->pline = pline;
65  pslave->do_cmd = uartslave_do_cmd;
66 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ uartslave_cycle()

void uartslave_cycle ( uartslave_t pslave)
68  {
69  int ch = -1;
70  int ret;
71  char *pstr;
72  uint16_t mod_msk = 0;
73  uint16_t cmd_id = 0;
74  if (pslave->count < pslave->size) {
75  while ((ch = uartrxbuff_getchar(pslave->prxbuff)) >= 0) {
76  if (ch == '\r')
77  ch = 0;
78  if (ch == '\n')
79  ch = 0;
80  pslave->pline[pslave->count] = ch;
81  if (ch)
82  pslave->count++;
83  if ((ch == 0) || (pslave->count >= pslave->size))
84  break;
85  }
86  }
87  if (pslave->count >= pslave->size) { //command overflow
88  _dbg0("ERROR: command overflow\n");
89  pslave->count = 0;
90  } else if ((pslave->count > 0) && (ch == 0)) { //line received
91  //_dbg0("line received: '%s'", pslave->pline);
92  if (pslave->flags & UARTSLAVE_FLG_ECHO)
93  uartslave_printf(pslave, "%s\n", pslave->pline);
94  pstr = pslave->pline;
95  if (pslave->parse_mod_mask)
96  ret = pslave->parse_mod_mask(pslave, pstr, &mod_msk);
97  else
98  ret = 0;
99  //_dbg0("ret = %d, mod_msk = %d", ret, mod_msk);
100  if (ret >= 0) {
101  pstr += ret;
102  ch = *pstr;
103  pstr++;
104  if (pslave->parse_cmd_id)
105  ret = pslave->parse_cmd_id(pslave, pstr, &cmd_id);
106  else
107  ret = 0;
108  //_dbg0("ret = %d, ch='%c', cmd_id = %d", ret, ch, cmd_id);
109  if (ret >= 0) {
110  pstr += ret;
111  if (*pstr == 0)
112  pstr = 0;
113  if (pslave->do_cmd)
114  ret = pslave->do_cmd(pslave, mod_msk, ch, cmd_id, pstr);
115  else
116  ret = 0;
117  } else
118  ret = UARTSLAVE_ERR_SYN;
119  }
120  //_dbg0("ret = %d", ret);
121  if (ret == UARTSLAVE_OK)
122  uartslave_printf(pslave, "ok\n");
123  else
124  uartslave_printf(pslave, "e%d\n", -((int)ret));
125  pslave->count = 0;
126  }
127 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ uartslave_printf()

int uartslave_printf ( uartslave_t pslave,
const char *  fmt,
  ... 
)
129  {
130  char text[32];
131  va_list va;
132  va_start(va, fmt);
133  int len = vsprintf(text, fmt, va);
134  va_end(va);
136  if (ret == HAL_OK)
137  return len;
138  return -1;
139 }
Here is the call graph for this function:
Here is the caller graph for this function:
_uartslave_t::parse_mod_mask
uartslave_parse_mod_mask_t * parse_mod_mask
Definition: uartslave.h:38
_uartslave_t::count
int count
Definition: uartslave.h:35
uartslave_printf
int uartslave_printf(uartslave_t *pslave, const char *fmt,...)
Definition: uartslave.c:129
UARTSLAVE_ERR_SYN
#define UARTSLAVE_ERR_SYN
Definition: uartslave.h:17
HAL_OK
Definition: stm32f4xx_hal_def.h:57
_dbg0
#define _dbg0
Definition: dbg.h:31
_uartslave_t::flags
uint8_t flags
Definition: uartslave.h:34
UARTSLAVE_MOD_MSK_0
#define UARTSLAVE_MOD_MSK_0
Definition: uartslave.h:9
uartslave_do_cmd
int uartslave_do_cmd(uartslave_t *pslave, uint16_t mod_msk, char cmd, uint16_t cmd_id, char *pstr)
Definition: uartslave.c:52
_uartrxbuff_t::phuart
UART_HandleTypeDef * phuart
Definition: uartrxbuff.h:17
UARTSLAVE_CMD_ID_UNK
#define UARTSLAVE_CMD_ID_UNK
Definition: uartslave.h:12
_uartslave_t::size
int size
Definition: uartslave.h:36
uartrxbuff_getchar
int uartrxbuff_getchar(uartrxbuff_t *prxbuff)
Definition: uartrxbuff.c:38
HAL_StatusTypeDef
HAL_StatusTypeDef
HAL Status structures definition.
Definition: stm32f4xx_hal_def.h:55
_uartslave_t::prxbuff
uartrxbuff_t * prxbuff
Definition: uartslave.h:33
UARTSLAVE_FLG_ECHO
#define UARTSLAVE_FLG_ECHO
Definition: uartslave.h:7
uint8_t
const uint8_t[]
Definition: 404_html.c:3
_uartslave_t::pline
char * pline
Definition: uartslave.h:37
_uartslave_t::do_cmd
uartslave_do_cmd_t * do_cmd
Definition: uartslave.h:40
uartslave_parse_mod_mask
int uartslave_parse_mod_mask(uartslave_t *pslave, char *pstr, uint16_t *pmod_msk)
Definition: uartslave.c:10
UARTSLAVE_OK
#define UARTSLAVE_OK
Definition: uartslave.h:14
uartslave_parse_cmd_id
int uartslave_parse_cmd_id(uartslave_t *pslave, char *pstr, uint16_t *pcmd_id)
Definition: uartslave.c:34
HAL_MAX_DELAY
#define HAL_MAX_DELAY
Definition: stm32f4xx_hal_def.h:76
UARTSLAVE_CMD_ID_0
#define UARTSLAVE_CMD_ID_0
Definition: uartslave.h:11
_uartslave_t::parse_cmd_id
uartslave_parse_cmd_id_t * parse_cmd_id
Definition: uartslave.h:39
size
static png_bytep size_t size
Definition: pngwrite.c:2170
HAL_UART_Transmit
HAL_StatusTypeDef HAL_UART_Transmit(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout)