GPS Device
Loading...
Searching...
No Matches
Console Public API

Public functions to interact with the console subsystem. More...

Collaboration diagram for Console Public API:

Functions

ERR_te console_init (CONSOLE_HANDLE_ts *console_handle)
 Initializes the console subsystem.
ERR_te console_run (void)
 Runs the console state machine. Must be called periodically.

Detailed Description

Public functions to interact with the console subsystem.

Function Documentation

◆ console_init()

ERR_te console_init ( CONSOLE_HANDLE_ts * console_handle)

Initializes the console subsystem.

Configures the internal state, initializes the RX GPIO pin in alternate function mode, and initializes the USART peripheral with interrupt-driven reception. USART reception is enabled before the function returns.

Note
CONFIG_CONSOLE_USART_CBUF_SIZE must be a power of two. Initialization will fail with ERR_INVALID_CONFIGURATION otherwise.
Parameters
[in]console_handlePointer to the console configuration structure.
Returns
  • ERR_OK on success
  • ERR_INVALID_CONFIGURATION if the USART circular buffer size is not a power of two
See also
console_init

Definition at line 92 of file console.c.

92 {
93 if(is_pow(CONFIG_CONSOLE_USART_CBUF_SIZE) == false) {
95 }
96
97 internal_state.usart_instance = console_handle->usart_instance;
98
99 init_log();
100
101 GPIO_CFG_ts console_rx_pin = { 0 };
102 console_rx_pin.port = console_handle->gpio_port;
103 console_rx_pin.pin = console_handle->gpio_pin;
104 console_rx_pin.mode = GPIO_MODE_ALTERNATE_FUNCTION;
105 console_rx_pin.alternate_function = console_handle->gpio_alternate_function;
106 gpio_init(&console_rx_pin);
107
108 USART_CFG_ts console_usart = { 0 };
109 console_usart.instance = console_handle->usart_instance;
110 console_usart.baud_rate = console_handle->usart_baud_rate;
112 usart_init(&console_usart);
113
115
116 return ERR_OK;
117}
static struct internal_state_s internal_state
Singleton instance of the SysTick driver internal state.
bool is_pow(uint32_t num)
Checks whether a number is a power of two.
Definition common.c:364
@ ENABLE
Definition common.h:100
@ ERR_INVALID_CONFIGURATION
Definition err.h:40
@ ERR_OK
Definition err.h:36
ERR_te init_log(void)
Initializes the logging subsystem.
Definition init.c:25
void gpio_init(GPIO_CFG_ts *gpio_cfg)
Initializes a GPIO pin according to the given configuration.
@ GPIO_MODE_ALTERNATE_FUNCTION
void usart_init(USART_CFG_ts *usart_cfg)
Initializes the USART peripheral with the given configuration.
void usart_set_reception(USART_REGDEF_ts *usart_instance, EN_STATUS_te en_status)
Enables or disables the USART receiver (RE bit).
@ USART_INTERRUPT_EN_TRUE
GPIO_PIN_te gpio_pin
Definition console.h:68
USART_BAUD_RATE_te usart_baud_rate
Definition console.h:62
GPIO_ALTERNATE_FUNCTION_te gpio_alternate_function
Definition console.h:71
GPIO_REGDEF_ts * gpio_port
Definition console.h:65
USART_REGDEF_ts * usart_instance
Definition console.h:59
Configuration structure for initializing a GPIO pin.
GPIO_REGDEF_ts * port
GPIO_ALTERNATE_FUNCTION_te alternate_function
GPIO_PIN_te pin
GPIO_MODE_te mode
Configuration structure for initializing a USART peripheral.
USART_BAUD_RATE_te baud_rate
USART_REGDEF_ts * instance
USART_INTERRUPT_EN_te interrupt_en
Here is the call graph for this function:
Here is the caller graph for this function:

◆ console_run()

ERR_te console_run ( void )

Runs the console state machine. Must be called periodically.

Each call performs the following steps:

  1. Drains any bytes received into the USART circular buffer by the ISR.
  2. Handles console mode entry and exit via the Ctrl+C character.
  3. In console mode, echoes typed characters and accumulates them in an internal buffer.
  4. On carriage return, processes backspace characters, then passes the resulting command string to cmd_execute.
Returns
  • ERR_OK on success
  • Propagated error from internal circular buffer or command operations

Runs the console state machine. Must be called periodically.

See also
console_run

Definition at line 120 of file console.c.

120 {
121 ERR_te err;
122 uint8_t data_len = 0;
123
124 err = cbuf_len(&internal_state.usart_data_recv_cbuf, &data_len);
125 if(err != ERR_OK) {
126 return err;
127 }
128
129 if(data_len) {
130 uint8_t data[data_len];
131
132 err = cbuf_read(&internal_state.usart_data_recv_cbuf, data);
133 if(err != ERR_OK) {
134 return err;
135 }
136
137 if(*data == CONSOLE_MODE_CMD && internal_state.console_mode == false) {
138 internal_state.console_mode = true;
140 usart_send(internal_state.usart_instance, (uint8_t*)"$", 1);
141 }
142 else if(*data == CONSOLE_MODE_CMD && internal_state.console_mode == true) {
143 internal_state.console_mode = false;
145
146 uint8_t console_text_len = 0;
147 err = cbuf_len(&internal_state.console_cbuf, &console_text_len);
148 if(err != ERR_OK) {
149 return err;
150 }
151
152 for(uint8_t i = 0; i < console_text_len + 1; i++) {
153 usart_send(internal_state.usart_instance, (uint8_t*)"\x7F", 1);
154 }
155 }
156
157 if(*data != CONSOLE_MODE_CMD && internal_state.console_mode == true) {
158 usart_send(internal_state.usart_instance, data, data_len);
159
160 err = cbuf_write(&internal_state.console_cbuf, data, data_len);
161 if(err != ERR_OK) {
162 return err;
163 }
164
165 for(uint32_t i = 0; i < data_len; i++) {
166 if(data[i] == '\r') {
168
169 uint8_t console_text_len = 0;
170 err = cbuf_len(&internal_state.console_cbuf, &console_text_len);
171 if(err != ERR_OK) {
172 return err;
173 }
174
175 char console_text[console_text_len];
176
177 err = cbuf_read(&internal_state.console_cbuf, (uint8_t*)console_text);
178 if(err != ERR_OK) {
179 return err;
180 }
181
182 console_text[console_text_len - 1] = '\0';
183
184 uint8_t backspace_counter = 0;
185
186 for(int16_t j = console_text_len - 1; j >= 0; j--) {
187 while(console_text[j] == 127) {
188 backspace_counter++;
189 console_text[j] = 0;
190 j--;
191 }
192
193 while(backspace_counter) {
194 console_text[j] = 0;
195 backspace_counter--;
196 j--;
197 }
198 }
199
200 arr_cmprs((char*)console_text, console_text_len);
201 internal_state.console_mode = false;
202 usart_send(internal_state.usart_instance, (uint8_t*)"\r\n", 2);
203 cmd_execute(console_text);
204 }
205 }
206 }
207 }
208
209 return ERR_OK;
210}
#define CONSOLE_MODE_CMD
ASCII control code used to enter and exit console mode (Ctrl+C).
Definition console.c:29
ERR_te cbuf_read(CBUF_HANDLE_ts *cbuf_handle, uint8_t *output_buf_o)
Reads all available data from the circular buffer into an output buffer.
Definition cbuf.c:22
ERR_te cbuf_len(CBUF_HANDLE_ts const *cbuf_handle, uint8_t *len_o)
Returns the number of bytes currently stored in the circular buffer.
Definition cbuf.c:61
ERR_te cbuf_write(CBUF_HANDLE_ts *cbuf_handle, uint8_t *input_buf, uint32_t input_len)
Writes data from an input buffer into the circular buffer.
Definition cbuf.c:41
ERR_te cmd_execute(char *console_text)
Parses and executes a command from a console text string.
Definition cmd.c:81
void arr_cmprs(char *arr, uint8_t len)
Compresses an array by removing null bytes and shifting remaining elements left.
Definition common.c:231
ERR_te
Standard return type used by all public API functions.
Definition err.h:35
ERR_te log_set_force_disable(bool bool_status)
Enables or disables forced suppression of all log output.
Definition log.c:219
void usart_send(USART_REGDEF_ts *usart_instance, uint8_t *tx_buffer, uint32_t len)
Blocking USART transmit. Sends len bytes from tx_buffer.
Here is the call graph for this function:
Here is the caller graph for this function: