Configurable i2c-pins

This commit is contained in:
Benjamin Vedder 2022-04-19 23:44:43 +02:00
parent cb16b441d2
commit a26c9ac97b
2 changed files with 62 additions and 31 deletions

View File

@ -903,17 +903,26 @@ Same as uart-read-bytes, but will return when the byte end is read.
#### i2c-start
```clj
(i2c-start optRate)
(i2c-start optRate optPinSda optPinScl)
```
Start the I2C driver on the COMM-port on the VESC. If any app is using the I2C pins it will be stopped first. optRate is an optional argument for the I2C bitrate. Example:
Start the I2C driver on the COMM-port on the VESC. If any app is using the I2C pins it will be stopped first. optRate is an optional argument for the I2C bitrate. optPinSda and optPinScl are optional arguments for using different SDA and SCL pins. Example:
```clj
; These are the bitrate options
(i2c-start 'rate-100k) ; 100 kHz bitrate
(i2c-start 'rate-200k) ; 200 kHz bitrate (default if no argument is passed)
(i2c-start 'rate-400k) ; 400 kHz bitrate
(i2c-start 'rate-700k) ; 700 kHz bitrate
(i2c-start 'rate-400k) ; 400 kbps and the default SDA and SDC pins
(i2c-start 'rate-200k 'pin-swdio 'pin-swclk) ; 100 kbps and SWDIO and SWCLK as SDA and SCL
; Available bitrates
'rate-100k
'rate-200k
'rate-400k
'rate-700k
; Available pins
'pin-rx
'pin-tx
'pin-swdio
'pin-swclk
```
#### i2c-tx-rx

View File

@ -289,6 +289,28 @@ static bool is_number_all(lbm_value *args, lbm_uint argn) {
#define CHECK_ARGN(n) if (argn != n) {return lbm_enc_sym(SYM_EERROR);}
#define CHECK_ARGN_NUMBER(n) if (argn != n || !is_number_all(args, argn)) {return lbm_enc_sym(SYM_EERROR);}
static bool gpio_get_pin(lbm_uint sym, stm32_gpio_t **port, int *pin) {
if (compare_symbol(sym, &syms_vesc.pin_rx)) {
#ifdef HW_UART_RX_PORT
*port = HW_UART_RX_PORT; *pin = HW_UART_RX_PIN;
return true;
#endif
} else if (compare_symbol(sym, &syms_vesc.pin_tx)) {
#ifdef HW_UART_TX_PORT
*port = HW_UART_TX_PORT; *pin = HW_UART_TX_PIN;
return true;
#endif
} else if (compare_symbol(sym, &syms_vesc.pin_swdio)) {
*port = GPIOA; *pin = 13;
return true;
} else if (compare_symbol(sym, &syms_vesc.pin_swclk)) {
*port = GPIOA; *pin = 14;
return true;
}
return false;
}
// Various commands
static lbm_value ext_print(lbm_value *args, lbm_uint argn) {
@ -1739,12 +1761,12 @@ static i2c_bb_state i2c_cfg = {
static bool i2c_started = false;
static lbm_value ext_i2c_start(lbm_value *args, lbm_uint argn) {
if (argn != 0 && argn != 1) {
if (argn > 3) {
return lbm_enc_sym(SYM_EERROR);
}
i2c_cfg.rate = I2C_BB_RATE_200K;
if (argn == 1) {
if (argn >= 1) {
if (!lbm_is_symbol(args[0])) {
return lbm_enc_sym(SYM_EERROR);
}
@ -1762,6 +1784,28 @@ static lbm_value ext_i2c_start(lbm_value *args, lbm_uint argn) {
}
}
stm32_gpio_t *sda_gpio = HW_UART_RX_PORT;
int sda_pin = HW_UART_RX_PIN;
if (argn >= 2) {
if (!lbm_is_symbol(args[1]) ||
!gpio_get_pin(lbm_dec_sym(args[1]), &sda_gpio, &sda_pin)) {
return lbm_enc_sym(SYM_EERROR);
}
}
i2c_cfg.sda_gpio = sda_gpio;
i2c_cfg.sda_pin = sda_pin;
stm32_gpio_t *scl_gpio = HW_UART_TX_PORT;
int scl_pin = HW_UART_TX_PIN;
if (argn >= 3) {
if (!lbm_is_symbol(args[2]) ||
!gpio_get_pin(lbm_dec_sym(args[2]), &scl_gpio, &scl_pin)) {
return lbm_enc_sym(SYM_EERROR);
}
}
i2c_cfg.scl_gpio = scl_gpio;
i2c_cfg.scl_pin = scl_pin;
app_configuration *appconf = mempools_alloc_appconf();
conf_general_read_app_configuration(appconf);
if (appconf->app_to_use == APP_UART ||
@ -1858,28 +1902,6 @@ static lbm_value ext_i2c_restore(lbm_value *args, lbm_uint argn) {
return lbm_enc_i(1);
}
static bool gpio_get_pin(lbm_uint sym, stm32_gpio_t **port, int *pin) {
if (compare_symbol(sym, &syms_vesc.pin_rx)) {
#ifdef HW_UART_RX_PORT
*port = HW_UART_RX_PORT; *pin = HW_UART_RX_PIN;
return true;
#endif
} else if (compare_symbol(sym, &syms_vesc.pin_tx)) {
#ifdef HW_UART_TX_PORT
*port = HW_UART_TX_PORT; *pin = HW_UART_TX_PIN;
return true;
#endif
} else if (compare_symbol(sym, &syms_vesc.pin_swdio)) {
*port = GPIOA; *pin = 13;
return true;
} else if (compare_symbol(sym, &syms_vesc.pin_swclk)) {
*port = GPIOA; *pin = 14;
return true;
}
return false;
}
static lbm_value ext_gpio_configure(lbm_value *args, lbm_uint argn) {
CHECK_ARGN(2);