r2axz / bluepill-serial-monster

USB to 3 Port Serial (UART) adapter firmware for STM32 Blue Pill.
MIT License
329 stars 78 forks source link

Feature proposal: Gpio control #45

Closed kzyapkov closed 2 years ago

kzyapkov commented 3 years ago

This is a proposal for enhancing the capabilities of the serial monster by adding arbitrary GPIO control.

Having serial ports with all the bells and whistles is awesome. However, sometimes not all 3 ports are required, or not all signals for a given port are required. It then makes sense to enable those pins to be controlled with console commands. This enables many use cases where slow GPIO access is enough to sense the state of the attached system or operate attached peripherals.

This PR implements a proof-of-concept of the above proposal. It tries to follow the original coding style, but does take many shortcuts and does add stdio to the linked libraries, bloating the size of the firmware by a few KB. The envisioned use case is for the first port to always be a control/config console, and the second and third UARTs to be available for use as either serial ports or their pins to be used as generic inputs or outputs. The control console can be scripted (with expect or expect-like tool) or operated manually by typing in commands.

By default, the monster behaves just like before -- all pins are associated to serial ports an controlled by the CDC-ACM machinery. If the use parameter of a signal is set to gpio it is detached from said machinery and is available as IO.

A sample command session, annotated:

# switch uart1 dcd signal to be used as gpio (PB15)
>uart 1 dcd use gpio
# when switched to GPIO use, pins are high-Z inputs by default
# read the state of all GPIO-configured signals:
>gpio read
B15:1 
# switch uart3 cts to be used as gpio (PB13)
>uart 3 cts use gpio
>gpio read
B13:1 B15:1 
# configure PB13 as output, set to low
>gpio config b13 dir out val 0
B13: output pp floating 0
# configure PB15 with pulldown
>gpio config b15 pull down
B15: input pp down 0
# read a single gpio
>gpio read b15
B15:0 
# or all gpios
>gpio read
B13:0 B15:0 
>gpio write b13 1
>gpio read
B13:1 B15:0 

While quite crude, the implementation is functional enough. If deemed upstreamable I'll try to polish it, probably add a few more features to facilitate programatic usage.

r2axz commented 3 years ago

@kzyapkov I think we can start thinking about integrating this functionality. If this does not interfere with the existing CDC code and does not degrade its performance, there is no harm in having the GPIO stuff available. Please address the cppcheck issues so that the PR passes all checks.

r2axz commented 2 years ago

This PR is inactive for quite a long time. Closing for now. If you are still willing to work on this, pease feel free to reopen.