Skip to content

Commit

Permalink
Introduce referenced gpio pins
Browse files Browse the repository at this point in the history
  • Loading branch information
ein-shved committed Nov 10, 2022
1 parent 7827159 commit 01290c9
Show file tree
Hide file tree
Showing 17 changed files with 664 additions and 114 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# General Target Settings
TARGET = bluepill-serial-monster
SRCS = main.c system_clock.c system_interrupts.c status_led.c usb_core.c usb_descriptors.c\
usb_io.c usb_uid.c usb_panic.c usb_cdc.c cdc_shell.c gpio.c device_config.c
usb_io.c usb_uid.c usb_panic.c usb_cdc.c cdc_shell.c gpio.c gpion.c device_config.c default_config.c

# Toolchain & Utils
CROSS_COMPILE ?= arm-none-eabi-
Expand All @@ -24,8 +24,8 @@ STM32_INCLUDES += -I$(STM32CUBE)/Drivers/CMSIS/Device/ST/STM32F1xx/Include

DEFINES = -DSTM32F103xB -DHSE_VALUE=8000000U
CPUFLAGS = -mthumb -mcpu=cortex-m3
WARNINGS = -Wall
OPTIMIZATION = -O3
WARNINGS = -Wall -Werror # -Wextra
OPTIMIZATION = #-O3
DEBUG = -ggdb

CFLAGS = $(DEFINES) $(STM32_INCLUDES) $(CPUFLAGS) $(WARNINGS) $(OPTIMIZATION) $(DEBUG)
Expand Down
8 changes: 4 additions & 4 deletions cdc_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
#ifndef CDC_CONFIG_H
#define CDC_CONFIG_H

#include "gpio.h"
#include "gpion.h"
#include "usb_cdc.h"

typedef struct {
gpio_pin_t pins[cdc_pin_last];
} __attribute__ ((packed)) cdc_port_t;
gpion_pin_t pins[cdc_pin_last];
} cdc_port_t;

typedef struct {
cdc_port_t port_config[USB_CDC_NUM_PORTS];
} __attribute__ ((packed)) cdc_config_t;
} cdc_config_t;

#endif /* CDC_CONFIG_H */
18 changes: 12 additions & 6 deletions cdc_shell.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ static const char cdc_shell_err_uart_invalid_pull_type[] = "Error, in
static const char cdc_shell_err_cannot_set_output_type_for_input[] = "Error, cannot set output type for input pin.\r\n";
static const char cdc_shell_err_cannot_change_polarity[] = "Error, cannot change polarity of alternate function pins.\r\n";
static const char cdc_shell_err_cannot_set_pull_for_output[] = "Error, cannot pull type for output pin.\r\n";
static const char cdc_shell_err_pin_is_detached[] = "Error, pin is detached.\r\n";


static const char *_cdc_uart_signal_names[cdc_pin_last] = {
Expand Down Expand Up @@ -145,11 +146,12 @@ static void cdc_shell_cmd_uart_show(int port) {
cdc_shell_write_string(colon_str);
cdc_shell_write_string(cdc_shell_new_line);
for (cdc_pin_t pin = 0; pin < cdc_pin_last; pin++) {
const gpio_pin_t *cdc_pin = &cdc_port->pins[pin];
const gpio_pin_t *cdc_pin = gpion_to_gpio(cdc_port->pins[pin]);
gpio_hal_t hal = gpion_to_hal(cdc_port->pins[pin]);
const char *pin_name = _cdc_uart_signal_names[pin];
cdc_shell_write_string(pin_name);
cdc_shell_write_string(cdc_shell_delim);
if (cdc_pin->port) {
if (cdc_pin && hal.port) {
const char *active_value = _cdc_uart_polarities[cdc_pin->polarity];
if (cdc_pin->dir == gpio_dir_input) {
cdc_shell_write_string(in_str);
Expand Down Expand Up @@ -180,8 +182,10 @@ static int cdc_shell_cmd_uart_set_output_type(int port, cdc_pin_t uart_pin, gpio
for (int port_index = ((port == -1) ? 0 : port);
port_index < ((port == -1) ? USB_CDC_NUM_PORTS : port + 1);
port_index++) {
gpio_pin_t *pin = &device_config_get()->cdc_config.port_config[port_index].pins[uart_pin];
if (pin->dir == gpio_dir_output) {
gpio_pin_t *pin = gpion_to_gpio(device_config_get()->cdc_config.port_config[port_index].pins[uart_pin]);
if (pin == 0) {
cdc_shell_write_string(cdc_shell_err_pin_is_detached);
} else if (pin->dir == gpio_dir_output) {
pin->output = output;
usb_cdc_reconfigure_port_pin(port, uart_pin);
} else {
Expand All @@ -196,7 +200,7 @@ static int cdc_shell_cmd_uart_set_polarity(int port, cdc_pin_t uart_pin, gpio_po
for (int port_index = ((port == -1) ? 0 : port);
port_index < ((port == -1) ? USB_CDC_NUM_PORTS : port + 1);
port_index++) {
gpio_pin_t *pin = &device_config_get()->cdc_config.port_config[port_index].pins[uart_pin];
gpio_pin_t *pin = gpion_to_gpio(device_config_get()->cdc_config.port_config[port_index].pins[uart_pin]);
if (pin->func == gpio_func_general && (uart_pin != cdc_pin_rx) && (uart_pin != cdc_pin_cts)) {
pin->polarity = polarity;
usb_cdc_reconfigure_port_pin(port, uart_pin);
Expand All @@ -212,7 +216,7 @@ static int cdc_shell_cmd_uart_set_pull_type(int port, cdc_pin_t uart_pin, gpio_p
for (int port_index = ((port == -1) ? 0 : port);
port_index < ((port == -1) ? USB_CDC_NUM_PORTS : port + 1);
port_index++) {
gpio_pin_t *pin = &device_config_get()->cdc_config.port_config[port_index].pins[uart_pin];
gpio_pin_t *pin = gpion_to_gpio(device_config_get()->cdc_config.port_config[port_index].pins[uart_pin]);
if (pin->dir == gpio_dir_input) {
pin->pull = pull;
usb_cdc_reconfigure_port_pin(port, uart_pin);
Expand Down Expand Up @@ -352,6 +356,8 @@ static void cdc_shell_cmd_config(int argc, char *argv[]) {
static const char cdc_shell_device_version[] = DEVICE_VERSION_STRING;

static void cdc_shell_cmd_version(int argc, char *argv[]) {
(void) argc;
(void) argv;
cdc_shell_write_string(cdc_shell_device_version);
cdc_shell_write_string(cdc_shell_new_line);
}
Expand Down
184 changes: 184 additions & 0 deletions default_config.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
/*
* MIT License
*
* Copyright (c) 2020 Kirill Kotyagin
* Copyright (c) 2022 Yury Shvedov
*/

#include "aux.h"
#include "default_config.h"

static const default_config_t default_config = {
.status_led_pin = { .pin = gpio_pin_pc13, .dir = gpio_dir_output, .speed = gpio_speed_low, .func = gpio_func_general, .output = gpio_output_od, .polarity = gpio_polarity_low, .status = gpio_status_occupied, },
.config_pin = { .pin = gpio_pin_pb5, .dir = gpio_dir_input, .pull = gpio_pull_up, .polarity = gpio_polarity_high, .status = gpio_status_occupied, },
.cdc_config = {
.port_config = {
/* Port 0 */
{
.pins =
{
[cdc_pin_rx] = { .pin = gpio_pin_pa10, .dir = gpio_dir_input, .pull = gpio_pull_up, .polarity = gpio_polarity_high, .status = gpio_status_occupied, },
[cdc_pin_tx] = { .pin = gpio_pin_pa9, .dir = gpio_dir_output, .speed = gpio_speed_medium, .func = gpio_func_alternate, .output = gpio_output_pp, .polarity = gpio_polarity_high, .status = gpio_status_occupied, },
[cdc_pin_rts] = { .pin = gpio_pin_pa15, .dir = gpio_dir_output, .speed = gpio_speed_medium, .func = gpio_func_general, .output = gpio_output_pp, .polarity = gpio_polarity_low, .status = gpio_status_occupied, },
[cdc_pin_cts] = { .pin = gpio_pin_pa11, .dir = gpio_dir_input, .pull = gpio_pull_down, .polarity = gpio_polarity_low, .status = gpio_status_occupied, },
[cdc_pin_dsr] = { .pin = gpio_pin_pb7, .dir = gpio_dir_input, .pull = gpio_pull_up, .polarity = gpio_polarity_low, .status = gpio_status_occupied, },
[cdc_pin_dtr] = { .pin = gpio_pin_pa4, .dir = gpio_dir_output, .speed = gpio_speed_medium, .func = gpio_func_general, .output = gpio_output_pp, .polarity = gpio_polarity_low, .status = gpio_status_occupied, },
[cdc_pin_dcd] = { .pin = gpio_pin_pb15, .dir = gpio_dir_input, .pull = gpio_pull_up, .polarity = gpio_polarity_low, .status = gpio_status_occupied, },
[cdc_pin_ri] = { .pin = gpio_pin_pb3, .dir = gpio_dir_input, .pull = gpio_pull_up, .polarity = gpio_polarity_low, .status = gpio_status_occupied, },
[cdc_pin_txa] = { .pin = gpio_pin_pb0, .dir = gpio_dir_output, .speed = gpio_speed_medium, .func = gpio_func_general, .output = gpio_output_pp, .polarity = gpio_polarity_high, .status = gpio_status_occupied, },
}
},
/* Port 1 */
{
.pins =
{
[cdc_pin_rx] = { .pin = gpio_pin_pa3, .dir = gpio_dir_input, .pull = gpio_pull_up, .polarity = gpio_polarity_high, .status = gpio_status_occupied, },
[cdc_pin_tx] = { .pin = gpio_pin_pa2, .dir = gpio_dir_output, .speed = gpio_speed_medium, .func = gpio_func_alternate, .output = gpio_output_pp, .polarity = gpio_polarity_high, .status = gpio_status_occupied, },
[cdc_pin_rts] = { .pin = gpio_pin_pa1, .dir = gpio_dir_output, .speed = gpio_speed_medium, .func = gpio_func_general, .output = gpio_output_pp, .polarity = gpio_polarity_low, .status = gpio_status_occupied, },
[cdc_pin_cts] = { .pin = gpio_pin_pa0, .dir = gpio_dir_input, .pull = gpio_pull_down, .polarity = gpio_polarity_low, .status = gpio_status_occupied, },
[cdc_pin_dsr] = { .pin = gpio_pin_pb4, .dir = gpio_dir_input, .pull = gpio_pull_up, .polarity = gpio_polarity_low, .status = gpio_status_occupied, },
[cdc_pin_dtr] = { .pin = gpio_pin_pa5, .dir = gpio_dir_output, .speed = gpio_speed_medium, .func = gpio_func_general, .output = gpio_output_pp, .polarity = gpio_polarity_low, .status = gpio_status_occupied, },
[cdc_pin_dcd] = { .pin = gpio_pin_pb8, .dir = gpio_dir_input, .pull = gpio_pull_up, .polarity = gpio_polarity_low, .status = gpio_status_occupied, },
[cdc_pin_ri] = { .pin = gpio_pin_pb12, .dir = gpio_dir_input, .pull = gpio_pull_up, .polarity = gpio_polarity_low, .status = gpio_status_occupied, },
[cdc_pin_txa] = { .pin = gpio_pin_pb1, .dir = gpio_dir_output, .speed = gpio_speed_medium, .func = gpio_func_general, .output = gpio_output_pp, .polarity = gpio_polarity_high, .status = gpio_status_occupied, },
}
},
/* Port 2 */
{
.pins =
{
[cdc_pin_rx] = { .pin = gpio_pin_pb11, .dir = gpio_dir_input, .pull = gpio_pull_up, .polarity = gpio_polarity_high, .status = gpio_status_occupied, },
[cdc_pin_tx] = { .pin = gpio_pin_pb10, .dir = gpio_dir_output, .speed = gpio_speed_medium, .func = gpio_func_alternate, .output = gpio_output_pp, .polarity = gpio_polarity_high, .status = gpio_status_occupied, },
[cdc_pin_rts] = { .pin = gpio_pin_pb14, .dir = gpio_dir_output, .speed = gpio_speed_medium, .func = gpio_func_general, .output = gpio_output_pp, .polarity = gpio_polarity_low, .status = gpio_status_occupied, },
[cdc_pin_cts] = { .pin = gpio_pin_pb13, .dir = gpio_dir_input, .pull = gpio_pull_down, .polarity = gpio_polarity_low, .status = gpio_status_occupied, },
[cdc_pin_dsr] = { .pin = gpio_pin_pb6, .dir = gpio_dir_input, .pull = gpio_pull_up, .polarity = gpio_polarity_low, .status = gpio_status_occupied, },
[cdc_pin_dtr] = { .pin = gpio_pin_pa6, .dir = gpio_dir_output, .speed = gpio_speed_medium, .func = gpio_func_general, .output = gpio_output_pp, .polarity = gpio_polarity_low, .status = gpio_status_occupied, },
[cdc_pin_dcd] = { .pin = gpio_pin_pb9, .dir = gpio_dir_input, .pull = gpio_pull_up, .polarity = gpio_polarity_low, .status = gpio_status_occupied, },
[cdc_pin_ri] = { .pin = gpio_pin_pa8, .dir = gpio_dir_input, .pull = gpio_pull_up, .polarity = gpio_polarity_low, .status = gpio_status_occupied, },
[cdc_pin_txa] = { .pin = gpio_pin_pa7, .dir = gpio_dir_output, .speed = gpio_speed_medium, .func = gpio_func_general, .output = gpio_output_pp, .polarity = gpio_polarity_high, .status = gpio_status_occupied, },
}
},
}
},
.blocked_pins = {
{
.pin = gpio_pin_pa11,
.reason = "usb dm",
},
{
.pin = gpio_pin_pa12,
.reason = "usb dp",
},
{
.pin = gpio_pin_pa13,
.reason = "debug swdio",
},
{
.pin = gpio_pin_pa14,
.reason = "debug swclk",
},
{
.pin = gpio_pin_pb2,
.reason = "boot control",
},

{
.pin = default_blocked_pin_end,
}
},
};

static gpion_pin_t default_config_pin_load(device_config_t *target, const default_gpio_pin_t *source);
static void default_config_pin_last(device_config_t *target, gpion_pin_t pin);

void default_config_load(device_config_t *target) {
default_gpio_pin_t default_free = {
.dir = gpio_dir_input,
.func = gpio_func_general,
.output = gpio_output_pp,
.pull = gpio_pull_up,
.polarity = gpio_polarity_high,
.speed = gpio_speed_medium,
.status = gpio_status_free,
};
default_gpio_pin_t default_blocked = {
.dir = gpio_dir_unknown,
.func = gpio_func_alternate,
.output = gpio_output_unknown,
.pull = gpio_pull_unknown,
.polarity = gpio_polarity_unknown,
.speed = gpio_speed_unknown,
.status = gpio_status_blocked,
};
if (target != 0) {
// reset all gpio pins to free.
for (gpion_pin_t pin = 0; pin < gpio_pin_last; ++pin) {
default_free.pin = pin;
target->gpio_config.pins[pin].status = gpio_status_free;
default_config_pin_load(target, &default_free);
}
// configure system pins
for (int i = 0; default_config.blocked_pins[i].pin != default_blocked_pin_end; ++i) {
gpion_pin_t pin = default_config.blocked_pins[i].pin;
default_blocked.pin = pin;
default_config_pin_load(target, &default_blocked);
}
// configure misc pins
target->status_led_pin = default_config_pin_load(target, &default_config.status_led_pin);
target->config_pin = default_config_pin_load(target, &default_config.config_pin);
// configure uart pins
for (int port = 0; port < ARRAY_SIZE(target->cdc_config.port_config) && port < ARRAY_SIZE(default_config.cdc_config.port_config); ++port) {
const default_port_t *default_port = &default_config.cdc_config.port_config[port];
cdc_port_t *port_config = &target->cdc_config.port_config[port];
for (int pin = 0; pin < ARRAY_SIZE(default_port->pins) && pin < ARRAY_SIZE(port_config->pins); ++pin) {
port_config->pins[pin] = default_config_pin_load(target, &default_port->pins[pin]);
}
}
// configure other pins
for (gpion_pin_t pin = 0; pin < gpio_pin_last; ++pin) {
default_config_pin_last(target, pin);
}
}
}

const char *default_config_get_blocked_reason(gpion_pin_t pin) {
static const char *not_blocked = "pin is not blocked";
for (int i = 0; default_config.blocked_pins[i].pin != default_blocked_pin_end; ++i) {
if (pin == default_config.blocked_pins[i].pin) {
return default_config.blocked_pins[i].reason;
}
}
return not_blocked;
}

static gpion_pin_t default_config_pin_load(device_config_t *target, const default_gpio_pin_t *source){
if (source->pin == gpio_pin_unknown) {
return gpio_pin_unknown;
}
gpio_pin_t *pin = &target->gpio_config.pins[source->pin];
if (pin->status == gpio_status_blocked) {
return source->pin;
}

pin->dir = source->dir;
pin->func = source->func;
pin->output = source->output;
pin->pull = source->pull;
pin->polarity = source->polarity;
pin->speed = source->speed;
pin->status = source->status;

return source->pin;
}

static void default_config_pin_last(device_config_t *target, gpion_pin_t pinn) {
gpio_pin_t *pin = &target->gpio_config.pins[pinn];
if (pinn < gpio_pin_last && pin->status == gpio_status_free) {
pin->dir = gpio_dir_unknown;
pin->func = gpio_func_general;
pin->output = gpio_output_unknown;
pin->pull = gpio_pull_unknown;
pin->polarity = gpio_polarity_unknown;
pin->speed = gpio_speed_unknown;
}
}
50 changes: 50 additions & 0 deletions default_config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* MIT License
*
* Copyright (c) 2022 Yury Shvedov
*/

#ifndef DEFAULT_CONFIG_H
#define DEFAULT_CONFIG_H

#include "gpion.h"
#include "usb_cdc.h"
#include "device_config.h"

typedef struct {
gpion_pin_t pin;
gpio_dir_t dir;
gpio_func_t func;
gpio_output_t output;
gpio_pull_t pull;
gpio_polarity_t polarity;
gpio_speed_t speed;
gpio_status_t status;
} default_gpio_pin_t;

typedef struct {
default_gpio_pin_t pins[cdc_pin_last];
} default_port_t;

typedef struct {
default_port_t port_config[USB_CDC_NUM_PORTS];
} default_cdc_t;

typedef struct {
gpion_pin_t pin;
const char *reason;
} default_blocked_pin_t;

#define default_blocked_pin_end gpio_pin_unknown

typedef struct {
default_gpio_pin_t status_led_pin;
default_gpio_pin_t config_pin;
default_cdc_t cdc_config;
default_blocked_pin_t blocked_pins[];
} default_config_t;

void default_config_load(device_config_t *target);
const char *default_config_get_blocked_reason(gpion_pin_t pin);

#endif /* DEFAULT_CONFIG_H */
Loading

0 comments on commit 01290c9

Please sign in to comment.