8000 Control I2C bus on external connector over SCPI by zartavr · Pull Request #4 · everypinio/PwrBlock-sw · GitHub
[go: up one dir, main page]

Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
00a8c94
Fix Makefile to remove .ld generated by CubeMX
zartavr Sep 28, 2026
e780d9b
Switch UART5 to UART3 for logging to free pins for external connector
zartavr Sep 28, 2026
47c90a9
[doc] Add scpi bus interface description
zartavr Sep 28, 2026
7613592
[scpi] Add periph control handlers
zartavr Sep 28, 2026
792f6fb
[periph_ctl] Add digital gpio backend for SCPI commands
zartavr Sep 28, 2026
83c3019
[periph_ctl] Rm mask rw and logic level
zartavr Sep 28, 2026
61eda1f
[periph_ctl] Format
zartavr Sep 28, 2026
8a222e8
[periph_ctl] Clean scpi dependancy over gpio driver
zartavr Sep 28, 2026
90b638c
[bsp] Define pins in cube, fix io7 pin
zartavr Sep 28, 2026
a3d2375
[periph_ctl] Rm debug printf
zartavr Sep 29, 2026
998a486
[periph_ctl] Split driver and scpi handler
zartavr Oct 5, 2026
db78aa2
[periph_ctl] Rm draft docs
zartavr Oct 7, 2026
7895d98
[digital_io] Refactoring
zartavr Oct 7, 2026
7030dea
[periph_ctl] Implement I2C over SCPI
zartavr Oct 7, 2026
0eb22cc
[periph_ctl] Rm I2C bus scan
zartavr Oct 7, 2026
079ccbc
[periph_ctl] Rm I2C bus recovery
zartavr Oct 7, 2026
48c8bea
[periph_ctl] Add I2C bus definition file
zartavr Oct 7, 2026
490f68d
[periph_ctl] Use standart mode 100kHz for I2C bus communication
zartavr Oct 7, 2026
b67560c
[periph_ctl] Rm I2C bus address setup, set address on r/w
zartavr Oct 7, 2026
2e39b86
[periph_ctl] Use fixed timeout 100 ms for i2c bus
zartavr Oct 7, 2026
2360101
[periph_ctl] Rm internal sda scl pullups option
zartavr Oct 7, 2026
ad33f5a
[periph_ctl] Refactoring
zartavr Oct 7, 2026
b45f5f7
[docs] Update i2c function description
zartavr Oct 7, 2026
a07bc35
[periph_ctl] Split driver and scpi handler
zartavr Oct 7, 2026
2a1c273
[i2c_bus] Draft of non-blocking i2c operations
zartavr Oct 7, 2026
3a2827f
[i2c_bus] Refactor
zartavr Oct 7, 2026
c330da6
[i2c_bus] Update docs
zartavr Oct 7, 2026
c30343a
[i2c_bus] Remove isr handler duplicate
zartavr Oct 7, 2026
640ab94
[scpi] Apply styles for bus format
zartavr Oct 7, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ format:
find ./src -iname '*.h' -o -iname '*.c' | clang-format --style=file -i --files=/dev/stdin

cube_remove_extras:
rm -rf src/bsp/periph_setup/cmake src/bsp/periph_setup/CMakePresets.json src/bsp/periph_setup/STM32G0B1XX_FLASH.ld
rm -rf src/bsp/periph_setup/cmake src/bsp/periph_setup/CMakePresets.json src/bsp/periph_setup/STM32G0B1*.ld

# List of source files to lint limited to src directory
LINT_FILES_LIST := $(shell find src/ -name "*.c")
Expand Down
31 changes: 31 additions & 0 deletions docs/scpi_info.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,34 @@ List of supported SCPI commands:
- `CALibration:CURRent:OFFSet {<Offset>}` - sets the offset of the linear approximation for the current setpoint function. The `Offset` parameter is in mA.
- `CALibration:CURRent:OFFSet?` - query for the current offset of the linear approximation for the current setpoint function, in mA.
- `CALibration:STORe` - command to save the current values of all calibration constants to non-volatile memory.
- Digital I/O (GPIO) control:
- `<n>` - pin number, 1 to the value returned by `[SOURce]:DIGital:COUNt?`; if omitted, pin 1 is used. Pin `<n>` corresponds to the IO`<n>` label on the external connector.
- `[SOURce]:DIGital:COUNt?` - query for the number of GPIO pins, example output "7".
- `[SOURce]:DIGital:PIN<n>:FUNCtion?` - query for the current pin owner, example output "GPIO". Every bus is wired to its own dedicated pins, so IO1 - IO7 are always GPIO and the reply is always "GPIO".
- `[SOURce]:DIGital:PIN<n>:DIRection {INPut | OUTPut}` - sets the pin direction. Default is INPut.
- `[SOURce]:DIGital:PIN<n>:DIRection?` - pin direction query.
- `[SOURce]:DIGital:PIN<n>:MODE {PUSHpull | ODRain}` - sets the output driver type: push-pull or open-drain. Default is PUSHpull.
- `[SOURce]:DIGital:PIN<n>:MODE?` - output driver type query.
- `[SOURce]:DIGital:PIN<n>:PULL {NONE | UP | DOWN}` - sets the internal pull resistor. Default is NONE.
- `[SOURce]:DIGital:PIN<n>:PULL?` - internal pull resistor query.
- `[SOURce]:DIGital:PIN<n>[:LEVel] {OFF | ON | 0 | 1}` - sets the output level of the pin. For an input pin, the value is stored and applied when the pin is switched to output.
- `[SOURce]:DIGital:PIN<n>[:LEVel]?` - query for the actual level on the pin (for both input and output pins), example output "1".
- Bus data format:
- `<Data>` - data parameter of bus write commands, up to 32 bytes. Accepted in any of these forms, regardless of `FORMat[:DATA]`:
- comma-separated list of bytes, e.g. `#H50,#H01,255`;
- definite-length arbitrary block, e.g. `#13ABC` (the first digit is the number of length digits, then the length, then the bytes).
- `FORMat[:DATA] {ASCii | HEXadecimal | INTeger}` - sets the response format of bus read queries: ASCii - comma-separated decimal bytes, example output "80,1,255"; HEXadecimal - comma-separated hexadecimal bytes, example output "#H50,#H01,#HFF"; INTeger - definite-length arbitrary block of raw bytes, example output "#13ABC". Default is HEXadecimal.
- `FORMat[:DATA]?` - response format query.
- Bus I2C control (master mode):
- The bus uses dedicated pins of the external connector, SCL and SDA, not shared with IO1 - IO7. The SCL clock is fixed at 100 kHz and the transaction timeout at 100 ms. There are no internal pull-up resistors, SDA and SCL need external ones.
- `BUS:I2C:STATe {OFF | ON | 0 | 1}` - enables and disables the I2C bus. Default is OFF.
- `BUS:I2C:STATe?` - I2C bus state query.
- `BUS:I2C:ADDRess:WIDTh {7 | 10}` - sets the slave address width in bits. Default is 7. It defines the range of the `Address` argument of `WRITe` and `READ?`: #H00 to #H7F for 7 bits, #H000 to #H3FF for 10 bits. It can be changed while the bus is enabled, the new width applies at once. Any other width returns error -224.
- `BUS:I2C:ADDRess:WIDTh?` - slave address width query, example output "7".
- `BUS:I2C:WRITe {<Address>},{<Data>}` - writes data to the slave: START, address + W, data, STOP. `Address` is the unshifted slave address, e.g. `#H50`, `Data` is 1 to 32 bytes, example: `BUS:I2C:WRIT #H50,#H10,#H01`.
- `BUS:I2C:READ? {<Address>},{<Count>}` - reads `Count` bytes from the slave: START, address + R, data, STOP. `Count` is 1 to 32. Response format is set by `FORMat[:DATA]`, example: `BUS:I2C:READ? #H50,2` returns "#H12,#H34" in the default HEXadecimal format, or "18,52" in ASCii.
- `WRITe` and `READ?` on a disabled bus return error -221. An address out of range for the current width, a data byte above 255, an empty `Data` block, or a `Count` of 0, returns error -222. More than 32 bytes of `Data`, or a `Count` above 32, returns error -223.
- Bus and GPIO state after `*RST` and power-on:
- all buses are disabled with default settings, all GPIO pins are inputs with default settings, `FORMat[:DATA]` is HEXadecimal.
- I2C: the bus is disabled and the address width is 7 bits.
- `*SAV` and `*RCL` do not affect bus and GPIO state.
1 change: 1 addition & 0 deletions lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,4 @@ target_sources(
scpi_parser/libscpi/src/utils.c
)
target_include_directories(scpi_parser SYSTEM PUBLIC scpi_parser/libscpi/inc)
target_compile_definitions(scpi_parser PUBLIC USE_FULL_ERROR_LIST=1)
4 changes: 4 additions & 0 deletions src/app.c
Original file line number Diff line number Diff line change
Expand Up 8BD0 @@ -14,6 +14,8 @@
* limitations under the License.
*/

#include "bsp/drivers/ext_conn/digital_gpio.h"
#include "bsp/drivers/ext_conn/i2c_bus.h"
#include "cmsis_os2.h"
#include "middleware/adc_mgr/adc_mgr.h"
#include "middleware/i2c_mgr/i2c_mgr.h"
Expand All @@ -23,6 +25,8 @@ int app(void)
{
i2c_manager_init();
adc_manager_init();
digital_gpio_init();
i2c_bus_init();

thread_usbpd_init();
thread_usbtmc_init();
Expand Down
2 changes: 2 additions & 0 deletions src/bsp/drivers/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ target_sources(
dac/dac.c
24aa02/24aa02.c
exti/exti.c
ext_conn/digital_gpio.c
ext_conn/i2c_bus.c
)
236 changes: 236 additions & 0 deletions src/bsp/drivers/ext_conn/digital_gpio.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,236 @@
/*
* Copyright 2026 Everypin
*
* 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 "digital_gpio.h"

#include <stm32g0xx_hal.h>

/// Port and pin of a GPIO of the external connector
typedef struct
{
GPIO_TypeDef* port;
uint16_t mask;
} DigitalPinHw;

typedef struct
{
DigitalDirection direction;
DigitalMode mode;
DigitalPull pull;
bool level;
} DigitalPinCfg;

// Pins of the external connector, indexed by pin number - 1.
static const DigitalPinHw PIN_HW[DIGITAL_PIN_COUNT] = {
{GPIOD, GPIO_PIN_0 }, // IO1
{GPIOD, GPIO_PIN_1 }, // IO2
{GPIOD, GPIO_PIN_2 }, // IO3
{GPIOD, GPIO_PIN_3 }, // IO4
{GPIOC, GPIO_PIN_6 }, // IO5
{GPIOC, GPIO_PIN_7 }, // IO6
{GPIOC, GPIO_PIN_13}, // IO7
};

static DigitalPinCfg pin_cfg[DIGITAL_PIN_COUNT];

// Private function prototypes
static bool digital_pin_index(uint32_t pin, uint32_t* index);
static void digital_pin_drive(uint32_t index, bool level);
static void digital_pin_apply(uint32_t index);

void digital_gpio_init(void)
{
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOD_CLK_ENABLE();

digital_gpio_reset();
}

void digital_gpio_reset(void)
{
for (uint32_t index = 0; index < DIGITAL_PIN_COUNT; index++) {
pin_cfg[index].direction = DIGITAL_DIRECTION_INPUT;
pin_cfg[index].mode = DIGITAL_MODE_PUSHPULL;
pin_cfg[index].pull = DIGITAL_PULL_NONE;
pin_cfg[index].level = false;

digital_pin_apply(index);
}
}

void digital_gpio_direction_set(uint32_t pin, DigitalDirection direction)
{
uint32_t index = 0;
if (!digital_pin_index(pin, &index)) {
return;
}

pin_cfg[index].direction = direction;
digital_pin_apply(index);
}

DigitalDirection digital_gpio_direction_get(uint32_t pin)
{
uint32_t index = 0;
if (!digital_pin_index(pin, &index)) {
return DIGITAL_DIRECTION_INPUT;
}

return pin_cfg[index].direction;
}

void digital_gpio_mode_set(uint32_t pin, DigitalMode mode)
{
uint32_t index = 0;
if (!digital_pin_index(pin, &index)) {
return;
}

pin_cfg[index].mode = mode;
digital_pin_apply(index);
}

DigitalMode digital_gpio_mode_get(uint32_t pin)
{
uint32_t index = 0;
if (!digital_pin_index(pin, &index)) {
return DIGITAL_MODE_PUSHPULL;
}

return pin_cfg[index].mode;
}

void digital_gpio_pull_set(uint32_t pin, DigitalPull pull)
{
uint32_t index = 0;
if (!digital_pin_index(pin, &index)) {
return;
}

pin_cfg[index].pull = pull;
digital_pin_apply(index);
}

DigitalPull digital_gpio_pull_get(uint32_t pin)
{
uint32_t index = 0;
if (!digital_pin_index(pin, &index)) {
return DIGITAL_PULL_NONE;
}

return pin_cfg[index].pull;
}

void digital_gpio_level_set(uint32_t pin, bool level)
{
uint32_t index = 0;
if (!digital_pin_index(pin, &index)) {
return;
}

pin_cfg[index].level = level;
digital_pin_drive(index, level);
}

bool digital_gpio_level_get(uint32_t pin)
{
uint32_t index = 0;
if (!digital_pin_index(pin, &index)) {
return false;
}

return HAL_GPIO_ReadPin(PIN_HW[index].port, PIN_HW[index].mask) ==
GPIO_PIN_SET;
}

/**
* @brief Check a pin number and convert it into an index of the pin tables.
*
* @param pin Pin number, 1 to DIGITAL_PIN_COUNT.
* @param index Index of the pin, pin number - 1.
* @return bool false if the pin number is out of range.
*/
static bool digital_pin_index(uint32_t pin, uint32_t* index)
{
if (pin < 1 || pin > DIGITAL_PIN_COUNT) {
return false;
}

*index = pin - 1;
return true;
}

/**
* @brief Drive the level of a pin.
*
* Has no effect on the pin while it is an input, the output register keeps
* the level until the pin is switched to output.
*
* @param index Index of the pin.
* @param level Level to drive.
*/
static void digital_pin_drive(uint32_t index, bool level)
{
HAL_GPIO_WritePin(
PIN_HW[index].port,
PIN_HW[index].mask,
level ? GPIO_PIN_SET : GPIO_PIN_RESET
);
}

/**
* @brief Apply the stored configuration of a pin to the port registers.
*
* @param index Index of the pin.
*/
static void digital_pin_apply(uint32_t index)
{
const DigitalPinCfg* cfg = &pin_cfg[index];
GPIO_InitTypeDef init = {0};

init.Pin = PIN_HW[index].mask;
init.Speed = GPIO_SPEED_FREQ_LOW;

switch (cfg->pull) {
case DIGITAL_PULL_UP: {
init.Pull = GPIO_PULLUP;
break;
}
case DIGITAL_PULL_DOWN: {
init.Pull = GPIO_PULLDOWN;
break;
}
case DIGITAL_PULL_NONE:
default: {
init.Pull = GPIO_NOPULL;
break;
}
}

if (cfg->direction == DIGITAL_DIRECTION_OUTPUT) {
init.Mode = (cfg->mode == DIGITAL_MODE_ODRAIN) ? GPIO_MODE_OUTPUT_OD :
GPIO_MODE_OUTPUT_PP;
}
else {
init.Mode = GPIO_MODE_INPUT;
}

// Load the output register before the mode is applied, so switching a pin
// to output cannot emit a pulse of the level left by a previous setup
digital_pin_drive(index, cfg->level);

HAL_GPIO_Init(PIN_HW[index].port, &init);
}
Loading
0