Skip to content

Commit

Permalink
console config support
Browse files Browse the repository at this point in the history
  • Loading branch information
AAsyunkin-se committed Jul 23, 2020
1 parent 01f90d4 commit ad0cc91
Show file tree
Hide file tree
Showing 10 changed files with 255 additions and 14 deletions.
32 changes: 32 additions & 0 deletions contrib/console_config/console
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# HydraBus console configuration file
# This file must be present in the Root of Hydrabus SD Card. However, its presense is optional.
# If it is not found (or no SD card found for that matter) at startup then only USB1 and USB2
# consoles are spawn as soon as USB bus becomes active (i.e. old 'compatibility' behaviour)
#
# Comments start with #
#
# USB1/USB2 usage: "USB<num>=<status>", where
# num is 1 for USB1 or 2 for USB2;
# status is 1 to enable console or 0 to disable (important if you want to power via USB but don't want to use the console)
#
# USART options: "USART=<status>;<num>;<speed>", where
# status is 1 to enable console or 0 to disable;
# num is USART number (1,2 or 3)
# speed is speed in bps (make sure you set the speed within Hydrabus USART speed range)
# max speed compatible with Linux non-custom speeds is 4000000 bps (check with stty on your system)
# only one USART console allowed at the moment. Note that each console instance take 4k+ more memory from the heap!
#
# At the moment USART3 is WIP; and USART2 has max tested speed of 2000000 bps.
#
# --- example 1: enable USB2 and USART1 ---
# USB1=0
# USB2=1
# USART=1;1;4000000
#
# --- example 2: enable USB1 and USB2 only ---
# USB1=1
# USB2=1
# USART=0;1;4000000
USB1=1
USB2=0
USART=1;2;2000000
2 changes: 2 additions & 0 deletions src/common/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,13 @@ typedef struct hydra_console {
thread_t *thread;
union {
SerialUSBDriver *sdu;
SerialDriver *sd;
BaseSequentialStream *bss;
};
t_tokenline *tl;
t_mode_config *mode;
int console_mode;
bool is_enabled;
FIL log_file;
} t_hydra_console;

Expand Down
2 changes: 1 addition & 1 deletion src/common/halconf.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@
* @brief Enables the SERIAL subsystem.
*/
#if !defined(HAL_USE_SERIAL) || defined(__DOXYGEN__)
#define HAL_USE_SERIAL FALSE
#define HAL_USE_SERIAL TRUE
#endif

/**
Expand Down
6 changes: 3 additions & 3 deletions src/common/mcuconf.h
Original file line number Diff line number Diff line change
Expand Up @@ -253,9 +253,9 @@
/*
* SERIAL driver system settings.
*/
#define STM32_SERIAL_USE_USART1 FALSE
#define STM32_SERIAL_USE_USART2 FALSE
#define STM32_SERIAL_USE_USART3 FALSE
#define STM32_SERIAL_USE_USART1 TRUE
#define STM32_SERIAL_USE_USART2 TRUE
#define STM32_SERIAL_USE_USART3 TRUE
#define STM32_SERIAL_USE_UART4 FALSE
#define STM32_SERIAL_USE_UART5 FALSE
#define STM32_SERIAL_USE_USART6 FALSE
Expand Down
4 changes: 4 additions & 0 deletions src/drv/stm32cube/bsp_print_dbg.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
* \return void
*
*/
#ifdef MAKE_DEBUG
void print_dbg(const char *data, const uint32_t size)
{
#ifdef USE_HOST_DEBUG
Expand Down Expand Up @@ -60,6 +61,7 @@ void print_dbg(const char *data, const uint32_t size)
(void)size;
#endif
}
#endif

/** \brief printf debug through Semi Hosting(SWD debug)
*
Expand All @@ -68,6 +70,7 @@ void print_dbg(const char *data, const uint32_t size)
* \return void
*
*/
#ifdef MAKE_DEBUG
void printf_dbg(const char *fmt, ...)
{
#ifdef USE_HOST_DEBUG
Expand Down Expand Up @@ -101,3 +104,4 @@ void printf_dbg(const char *fmt, ...)
(void)fmt;
#endif
}
#endif
8 changes: 8 additions & 0 deletions src/drv/stm32cube/bsp_print_dbg.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,18 @@ limitations under the License.
#ifndef _BSP_PRINT_DBG_H_
#define _BSP_PRINT_DBG_H_

#ifndef MAKE_DEBUG

#define print_dbg(...)
#define printf_dbg(...)

#else

/* Low level print debug through Semi Hosting(SWD debug) */
void print_dbg(const char *data, const uint32_t size);

/* Low level printf debug through Semi Hosting(SWD debug) */
void printf_dbg(const char *fmt, ...);
#endif

#endif /* _BSP_PRINT_DBG_H_ */
104 changes: 104 additions & 0 deletions src/hydrabus/console_cfg.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* HydraBus/HydraNFC
*
* Copyright (C) 2020 Alexander ASYUNKIN
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "ff.h"
#include "microsd.h"
#include "bsp_print_dbg.h"
#include <stdlib.h>
#include <stdio.h>

#define CONSOLE_CONFIG_FILENAME "console"
#define LINEBUF_SZE 256

bool console_read_sd_config(int *ptr_usart_no, uint32_t *ptr_speed, bool *ptr_usb1, bool *ptr_usb2)
{
uint8_t result = 0;
int usb_no, usb_status;
int usart_status, usart_number;
uint32_t usart_speed;
// we all hate malloc, but main's stack is very small...
FIL *fp = (FIL *)malloc(sizeof(FIL));
if (fp==NULL) {
printf_dbg("Cannot allocate FIL resources\n");
return FALSE;
}

uint8_t *linebuf = (uint8_t *)malloc(LINEBUF_SZE);
if (linebuf==NULL) {
printf_dbg("Cannot allocate linebuf resources\n");
free(fp);
return FALSE;
}

if (!is_file_present(CONSOLE_CONFIG_FILENAME)) {
printf_dbg("File %s is not present on SD card or SD not ready\n", CONSOLE_CONFIG_FILENAME);
free(fp);
free(linebuf);
return FALSE;
}

if (!file_open(fp, CONSOLE_CONFIG_FILENAME, 'r')) {
printf_dbg("Failed to open file %s\r\n", CONSOLE_CONFIG_FILENAME);
free(fp);
free(linebuf);
return FALSE;
}

while(file_readline(fp, linebuf, LINEBUF_SZE)) {

if(linebuf[0] == '#') {
continue;
}

usb_no = usb_status = 0;
// result = sscanf((char *)linebuf, "%*USB%1d=%1d", &usb_no, &usb_status);
result = sscanf((char *)linebuf, "USB%d=%d", (int *)&usb_no, (int *)&usb_status);
if (result == 2) {
switch (usb_no)
{
case 1:
*ptr_usb1 = (usb_status == 1) ? TRUE : FALSE;
printf_dbg("Processed USB1 entry\r\n");
break;
case 2:
*ptr_usb2 = (usb_status == 1) ? TRUE : FALSE;
printf_dbg("Processed USB2 entry\r\n");
break;
default:
break;

}
}

usart_status = usart_number = usart_speed = 0;
// result = sscanf((char *)linebuf, "%*USART=%1d", &usart_status, &usart_number, &usart_speed);
result = sscanf((char *)linebuf, "USART=%d;%d;%ld", (int *)&usart_status, (int *)&usart_number, &usart_speed);
if (result == 3) {

*ptr_usart_no = (usart_status == 1) ? usart_number : 0;
*ptr_speed = usart_speed;
printf_dbg("Processed USART entry\r\n");
}
}
file_close(fp);
free(fp);
free(linebuf);

return TRUE;
}

19 changes: 19 additions & 0 deletions src/hydrabus/console_cfg.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* HydraBus/HydraNFC
*
* Copyright (C) 2020 Alexander ASYUNKIN
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

extern bool console_read_sd_config(int *ptr_usart_no, uint32_t *ptr_speed, bool *ptr_usb1, bool *ptr_usb2);
3 changes: 2 additions & 1 deletion src/hydrabus/hydrabus.mk
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ HYDRABUSSRC = hydrabus/hydrabus.c \
hydrabus/hydrabus_mode_wiegand.c \
hydrabus/hydrabus_mode_lin.c \
hydrabus/hydrabus_bbio_aux.c \
hydrabus/hydrabus_aux.c
hydrabus/hydrabus_aux.c \
hydrabus/console_cfg.c

# Required include directories
HYDRABUSINC = ./hydrabus
89 changes: 80 additions & 9 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
#include "bsp_print_dbg.h"

#include "script.h"
#include "hydrabus/console_cfg.h"

#define INIT_SCRIPT_NAME "initscript"

Expand All @@ -56,6 +57,13 @@ SerialUSBDriver SDU1;
/* USB2: Virtual serial port over USB. */
SerialUSBDriver SDU2;

static SerialConfig ser_usart_default_cfg = {
115200,
0,
0,
0,
};

extern t_token tl_tokens[];
extern t_token_dict tl_dict[];

Expand All @@ -66,9 +74,13 @@ t_mode_config mode_con1 = { .proto={ .dev_num = 0 }, .cmd={ 0 } };
t_tokenline tl_con2;
t_mode_config mode_con2 = { .proto={ .dev_num = 0 }, .cmd={ 0 } };

t_tokenline tl_con3;
t_mode_config mode_con3 = { .proto={ .dev_num = 0 }, .cmd={ 0 } };

t_hydra_console consoles[] = {
{ .thread_name="console USB1", .sdu=&SDU1, .tl=&tl_con1, .mode = &mode_con1 },
{ .thread_name="console USB2", .sdu=&SDU2, .tl=&tl_con2, .mode = &mode_con2 }
{ .thread_name="console USB1", .sdu=&SDU1, .tl=&tl_con1, .mode = &mode_con1, .is_enabled = TRUE },
{ .thread_name="console USB2", .sdu=&SDU2, .tl=&tl_con2, .mode = &mode_con2, .is_enabled = TRUE },
{ .thread_name="console USART", .sd=NULL, .tl=&tl_con3, .mode = &mode_con3, .is_enabled = FALSE }
};

THD_FUNCTION(console, arg)
Expand Down Expand Up @@ -200,29 +212,88 @@ int main(void)
* Normal main() thread activity.
*/
chRegSetThreadName("main");

uint32_t usart_speed = 0;
bool custom_console_config = FALSE, useUSB1 = TRUE, useUSB2 = TRUE;
int usart_console_no = 0;

custom_console_config = console_read_sd_config(&usart_console_no, &usart_speed, &useUSB1, &useUSB2);

if (custom_console_config) {

switch(usart_console_no)
{
case 1:
sdInit();
/* Configure & Enable USART) */
ser_usart_default_cfg.speed = usart_speed;
sdStart(&SD1, &ser_usart_default_cfg);

// USART1
palSetPadMode(GPIOA, 9, PAL_MODE_ALTERNATE(7)); // USART1_TX => RX FTDI YELLOW
palSetPadMode(GPIOA, 10, PAL_MODE_ALTERNATE(7)); // USART1_RX <= TX FTDI ORANGE

consoles[2].sd = &SD1;
break;
case 2:
sdInit();
/* Configure & Enable USART) */
ser_usart_default_cfg.speed = usart_speed;
sdStart(&SD2, &ser_usart_default_cfg);

// USART2
palSetPadMode(GPIOA, 2, PAL_MODE_ALTERNATE(7));
palSetPadMode(GPIOA, 3, PAL_MODE_ALTERNATE(7));

consoles[2].sd = &SD2;
break;
case 3:
// TBC
break;
default:
break;
}

if (consoles[2].sd != NULL) {

consoles[2].thread = chThdCreateFromHeap(NULL,
CONSOLE_WA_SIZE, consoles[2].thread_name, NORMALPRIO,
console, &consoles[2]);
consoles[2].is_enabled = TRUE;
nb_console++;
printf_dbg("USART %d console started (%p) at %ldbps\n",
usart_console_no, consoles[2].thread, ser_usart_default_cfg.speed);
}

// disable USB consoles that we do not need
consoles[0].is_enabled = useUSB1;
consoles[1].is_enabled = useUSB2;
}

while (TRUE) {

local_nb_console = 0;
for (i = 0; i < 2; i++) {
if (!consoles[i].thread) {
if (consoles[i].sdu->config->usbp->state != USB_ACTIVE)
continue;
/* Spawn new console thread.*/
/* Spawn new console thread if enabled*/
if (!consoles[0].is_enabled)
continue;
consoles[i].thread = chThdCreateFromHeap(NULL,
CONSOLE_WA_SIZE, consoles[i].thread_name, NORMALPRIO,
console, &consoles[i]);
#ifdef MAKE_DEBUG
local_nb_console++;
printf_dbg("USB %d console started (%p)\n", i+1, consoles[i].thread);
#endif
} else {
if (chThdTerminatedX(consoles[i].thread))
if (chThdTerminatedX(consoles[i].thread)) {
/* This console thread terminated. */
consoles[i].thread = NULL;
printf_dbg("USB %d console terminated\n", i+1);
}
}
if (consoles[i].sdu->config->usbp->state == USB_ACTIVE)
local_nb_console++;
}
nb_console = local_nb_console;
nb_console += local_nb_console;

/* HydraBus ULED blink. */
if (hydrabus_ubtn())
Expand Down

0 comments on commit ad0cc91

Please sign in to comment.