Skip to content

Commit

Permalink
Merge pull request #332 from spacelab-ufsc/wdt_bug_fix
Browse files Browse the repository at this point in the history
Merge pull request from wdt_bug_fix, closes #330
  • Loading branch information
miguelboing authored Mar 20, 2023
2 parents b6a5e14 + df4bf93 commit 6dbe90e
Show file tree
Hide file tree
Showing 7 changed files with 283 additions and 17 deletions.
219 changes: 217 additions & 2 deletions firmware/drivers/gpio/gpio.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* gpio.c
*
* Copyright (C) 2021, SpaceLab.
* Copyright The OBDH 2.0 Contributors.
*
* This file is part of OBDH 2.0.
*
Expand All @@ -25,7 +25,7 @@
*
* \author Gabriel Mariano Marcelino <[email protected]>
*
* \version 0.8.8
* \version 0.10.2
*
* \date 2020/01/13
*
Expand All @@ -40,6 +40,95 @@

#include "gpio.h"

static const uint16_t GPIO_PORT_TO_BASE[] = { // cppcheck-suppress misra-c2012-8.9
0x00,
#if defined(__MSP430_HAS_PORT1_R__)
__MSP430_BASEADDRESS_PORT1_R__,
#elif defined(__MSP430_HAS_PORT1__)
__MSP430_BASEADDRESS_PORT1__,
#else
0xFFFF,
#endif
#if defined(__MSP430_HAS_PORT2_R__)
__MSP430_BASEADDRESS_PORT2_R__,
#elif defined(__MSP430_HAS_PORT2__)
__MSP430_BASEADDRESS_PORT2__,
#else
0xFFFF,
#endif
#if defined(__MSP430_HAS_PORT3_R__)
__MSP430_BASEADDRESS_PORT3_R__,
#elif defined(__MSP430_HAS_PORT3__)
__MSP430_BASEADDRESS_PORT3__,
#else
0xFFFF,
#endif
#if defined(__MSP430_HAS_PORT4_R__)
__MSP430_BASEADDRESS_PORT4_R__,
#elif defined(__MSP430_HAS_PORT4__)
__MSP430_BASEADDRESS_PORT4__,
#else
0xFFFF,
#endif
#if defined(__MSP430_HAS_PORT5_R__)
__MSP430_BASEADDRESS_PORT5_R__,
#elif defined(__MSP430_HAS_PORT5__)
__MSP430_BASEADDRESS_PORT5__,
#else
0xFFFF,
#endif
#if defined(__MSP430_HAS_PORT6_R__)
__MSP430_BASEADDRESS_PORT6_R__,
#elif defined(__MSP430_HAS_PORT6__)
__MSP430_BASEADDRESS_PORT6__,
#else
0xFFFF,
#endif
#if defined(__MSP430_HAS_PORT7_R__)
__MSP430_BASEADDRESS_PORT7_R__,
#elif defined(__MSP430_HAS_PORT7__)
__MSP430_BASEADDRESS_PORT7__,
#else
0xFFFF,
#endif
#if defined(__MSP430_HAS_PORT8_R__)
__MSP430_BASEADDRESS_PORT8_R__,
#elif defined(__MSP430_HAS_PORT8__)
__MSP430_BASEADDRESS_PORT8__,
#else
0xFFFF,
#endif
#if defined(__MSP430_HAS_PORT9_R__)
__MSP430_BASEADDRESS_PORT9_R__,
#elif defined(__MSP430_HAS_PORT9__)
__MSP430_BASEADDRESS_PORT9__,
#else
0xFFFF,
#endif
#if defined(__MSP430_HAS_PORT10_R__)
__MSP430_BASEADDRESS_PORT10_R__,
#elif defined(__MSP430_HAS_PORT10__)
__MSP430_BASEADDRESS_PORT10__,
#else
0xFFFF,
#endif
#if defined(__MSP430_HAS_PORT11_R__)
__MSP430_BASEADDRESS_PORT11_R__,
#elif defined(__MSP430_HAS_PORT11__)
__MSP430_BASEADDRESS_PORT11__,
#else
0xFFFF,
#endif
0xFFFF,
#if defined(__MSP430_HAS_PORTJ_R__)
__MSP430_BASEADDRESS_PORTJ_R__
#elif defined(__MSP430_HAS_PORTJ__)
__MSP430_BASEADDRESS_PORTJ__
#else
0xFFFF
#endif
};

int gpio_init(gpio_pin_t pin, gpio_config_t config)
{
int err = 0;
Expand Down Expand Up @@ -448,4 +537,130 @@ int gpio_toggle(gpio_pin_t pin)
return err;
}

int gpio_init_mr_pin(gpio_pin_t mr_pin)
{
int err = 0;

uint8_t msp_port = UINT8_MAX;
uint16_t msp_pin = UINT16_MAX;

switch(mr_pin)
{
case GPIO_PIN_0: msp_port = GPIO_PORT_P1; msp_pin = GPIO_PIN0; break;
case GPIO_PIN_1: msp_port = GPIO_PORT_P1; msp_pin = GPIO_PIN1; break;
case GPIO_PIN_2: msp_port = GPIO_PORT_P1; msp_pin = GPIO_PIN2; break;
case GPIO_PIN_3: msp_port = GPIO_PORT_P1; msp_pin = GPIO_PIN3; break;
case GPIO_PIN_4: msp_port = GPIO_PORT_P1; msp_pin = GPIO_PIN4; break;
case GPIO_PIN_5: msp_port = GPIO_PORT_P1; msp_pin = GPIO_PIN5; break;
case GPIO_PIN_6: msp_port = GPIO_PORT_P1; msp_pin = GPIO_PIN6; break;
case GPIO_PIN_7: msp_port = GPIO_PORT_P1; msp_pin = GPIO_PIN7; break;
case GPIO_PIN_8: msp_port = GPIO_PORT_P2; msp_pin = GPIO_PIN0; break;
case GPIO_PIN_9: msp_port = GPIO_PORT_P2; msp_pin = GPIO_PIN1; break;
case GPIO_PIN_10: msp_port = GPIO_PORT_P2; msp_pin = GPIO_PIN2; break;
case GPIO_PIN_11: msp_port = GPIO_PORT_P2; msp_pin = GPIO_PIN3; break;
case GPIO_PIN_12: msp_port = GPIO_PORT_P2; msp_pin = GPIO_PIN4; break;
case GPIO_PIN_13: msp_port = GPIO_PORT_P2; msp_pin = GPIO_PIN5; break;
case GPIO_PIN_14: msp_port = GPIO_PORT_P2; msp_pin = GPIO_PIN6; break;
case GPIO_PIN_15: msp_port = GPIO_PORT_P2; msp_pin = GPIO_PIN7; break;
case GPIO_PIN_16: msp_port = GPIO_PORT_P3; msp_pin = GPIO_PIN0; break;
case GPIO_PIN_17: msp_port = GPIO_PORT_P3; msp_pin = GPIO_PIN1; break;
case GPIO_PIN_18: msp_port = GPIO_PORT_P3; msp_pin = GPIO_PIN2; break;
case GPIO_PIN_19: msp_port = GPIO_PORT_P3; msp_pin = GPIO_PIN3; break;
case GPIO_PIN_20: msp_port = GPIO_PORT_P3; msp_pin = GPIO_PIN4; break;
case GPIO_PIN_21: msp_port = GPIO_PORT_P3; msp_pin = GPIO_PIN5; break;
case GPIO_PIN_22: msp_port = GPIO_PORT_P3; msp_pin = GPIO_PIN6; break;
case GPIO_PIN_23: msp_port = GPIO_PORT_P3; msp_pin = GPIO_PIN7; break;
case GPIO_PIN_24: msp_port = GPIO_PORT_P4; msp_pin = GPIO_PIN0; break;
case GPIO_PIN_25: msp_port = GPIO_PORT_P4; msp_pin = GPIO_PIN1; break;
case GPIO_PIN_26: msp_port = GPIO_PORT_P4; msp_pin = GPIO_PIN2; break;
case GPIO_PIN_27: msp_port = GPIO_PORT_P4; msp_pin = GPIO_PIN3; break;
case GPIO_PIN_28: msp_port = GPIO_PORT_P4; msp_pin = GPIO_PIN4; break;
case GPIO_PIN_29: msp_port = GPIO_PORT_P4; msp_pin = GPIO_PIN5; break;
case GPIO_PIN_30: msp_port = GPIO_PORT_P4; msp_pin = GPIO_PIN6; break;
case GPIO_PIN_31: msp_port = GPIO_PORT_P4; msp_pin = GPIO_PIN7; break;
case GPIO_PIN_32: msp_port = GPIO_PORT_P5; msp_pin = GPIO_PIN0; break;
case GPIO_PIN_33: msp_port = GPIO_PORT_P5; msp_pin = GPIO_PIN1; break;
case GPIO_PIN_34: msp_port = GPIO_PORT_P5; msp_pin = GPIO_PIN2; break;
case GPIO_PIN_35: msp_port = GPIO_PORT_P5; msp_pin = GPIO_PIN3; break;
case GPIO_PIN_36: msp_port = GPIO_PORT_P5; msp_pin = GPIO_PIN4; break;
case GPIO_PIN_37: msp_port = GPIO_PORT_P5; msp_pin = GPIO_PIN5; break;
case GPIO_PIN_38: msp_port = GPIO_PORT_P5; msp_pin = GPIO_PIN6; break;
case GPIO_PIN_39: msp_port = GPIO_PORT_P5; msp_pin = GPIO_PIN7; break;
case GPIO_PIN_40: msp_port = GPIO_PORT_P6; msp_pin = GPIO_PIN0; break;
case GPIO_PIN_41: msp_port = GPIO_PORT_P6; msp_pin = GPIO_PIN1; break;
case GPIO_PIN_42: msp_port = GPIO_PORT_P6; msp_pin = GPIO_PIN2; break;
case GPIO_PIN_43: msp_port = GPIO_PORT_P6; msp_pin = GPIO_PIN3; break;
case GPIO_PIN_44: msp_port = GPIO_PORT_P6; msp_pin = GPIO_PIN4; break;
case GPIO_PIN_45: msp_port = GPIO_PORT_P6; msp_pin = GPIO_PIN5; break;
case GPIO_PIN_46: msp_port = GPIO_PORT_P6; msp_pin = GPIO_PIN6; break;
case GPIO_PIN_47: msp_port = GPIO_PORT_P6; msp_pin = GPIO_PIN7; break;
case GPIO_PIN_48: msp_port = GPIO_PORT_P7; msp_pin = GPIO_PIN2; break;
case GPIO_PIN_49: msp_port = GPIO_PORT_P7; msp_pin = GPIO_PIN3; break;
case GPIO_PIN_50: msp_port = GPIO_PORT_P7; msp_pin = GPIO_PIN4; break;
case GPIO_PIN_51: msp_port = GPIO_PORT_P7; msp_pin = GPIO_PIN5; break;
case GPIO_PIN_52: msp_port = GPIO_PORT_P7; msp_pin = GPIO_PIN6; break;
case GPIO_PIN_53: msp_port = GPIO_PORT_P7; msp_pin = GPIO_PIN7; break;
case GPIO_PIN_54: msp_port = GPIO_PORT_P8; msp_pin = GPIO_PIN0; break;
case GPIO_PIN_55: msp_port = GPIO_PORT_P8; msp_pin = GPIO_PIN1; break;
case GPIO_PIN_56: msp_port = GPIO_PORT_P8; msp_pin = GPIO_PIN2; break;
case GPIO_PIN_57: msp_port = GPIO_PORT_P8; msp_pin = GPIO_PIN3; break;
case GPIO_PIN_58: msp_port = GPIO_PORT_P8; msp_pin = GPIO_PIN4; break;
case GPIO_PIN_59: msp_port = GPIO_PORT_P8; msp_pin = GPIO_PIN5; break;
case GPIO_PIN_60: msp_port = GPIO_PORT_P8; msp_pin = GPIO_PIN6; break;
case GPIO_PIN_61: msp_port = GPIO_PORT_P8; msp_pin = GPIO_PIN7; break;
case GPIO_PIN_62: msp_port = GPIO_PORT_P9; msp_pin = GPIO_PIN0; break;
case GPIO_PIN_63: msp_port = GPIO_PORT_P9; msp_pin = GPIO_PIN1; break;
case GPIO_PIN_64: msp_port = GPIO_PORT_P9; msp_pin = GPIO_PIN2; break;
case GPIO_PIN_65: msp_port = GPIO_PORT_P9; msp_pin = GPIO_PIN3; break;
case GPIO_PIN_66: msp_port = GPIO_PORT_P9; msp_pin = GPIO_PIN4; break;
case GPIO_PIN_67: msp_port = GPIO_PORT_P9; msp_pin = GPIO_PIN5; break;
case GPIO_PIN_68: msp_port = GPIO_PORT_P9; msp_pin = GPIO_PIN6; break;
case GPIO_PIN_69: msp_port = GPIO_PORT_P9; msp_pin = GPIO_PIN7; break;
default:
#if defined(CONFIG_DRIVERS_DEBUG_ENABLED) && (CONFIG_DRIVERS_DEBUG_ENABLED == 1)
sys_log_print_event_from_module(SYS_LOG_ERROR, GPIO_MODULE_NAME, "Invalid mr pin to initialize!");
sys_log_new_line();
#endif /* CONFIG_DRIVERS_DEBUG_ENABLED */
err = -1; /* Invalid GPIO pin */

break;
}

if (err == 0)
{
uint16_t base_address = GPIO_PORT_TO_BASE[msp_port];

#ifndef NDEBUG
if (base_address != 0xFFFFU)
{
/* Shift by 8 if port is even (upper 8-bits) */
if (((msp_port & 1U) ^ 1U) > 0U)
{
msp_pin <<= 8;
}

/* Instruction to impose MR pin as high and avoid auto-reset. */
HWREG16(base_address + OFS_PAOUT) |= msp_pin;

HWREG16(base_address + OFS_PASEL) &= ~msp_pin;
HWREG16(base_address + OFS_PADIR) |= msp_pin;
}
#else
/* Shift by 8 if port is even (upper 8-bits) */
if (((msp_port & 1U) ^ 1U) > 0U)
{
msp_pin <<= 8;
}

/* Instruction to impose MR pin as high and avoid auto-reset. */
HWREG16(base_address + OFS_PAOUT) |= msp_pin;

HWREG16(base_address + OFS_PASEL) &= ~msp_pin;
HWREG16(base_address + OFS_PADIR) |= msp_pin;
#endif /* NDEBUG */
}

return err;
}

/** \} End of gpio group */
18 changes: 16 additions & 2 deletions firmware/drivers/gpio/gpio.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* gpio.h
*
* Copyright (C) 2021, SpaceLab.
* Copyright The OBDH 2.0 Contributors.
*
* This file is part of OBDH 2.0.
*
Expand All @@ -25,7 +25,7 @@
*
* \author Gabriel Mariano Marcelino <[email protected]>
*
* \version 0.7.40
* \version 0.10.2
*
* \date 2020/01/13
*
Expand Down Expand Up @@ -193,6 +193,20 @@ int gpio_get_state(gpio_pin_t pin);
*/
int gpio_toggle(gpio_pin_t pin);

/**
* \brief Configures the MR GPIO pin to initialize in high as output.
*
* Adaptation from the hal/gpio function, this function must be used to configure the
* external watchdog MR pin as output and avoid accidental resets during configuration.
*
* \see SLVS165L - TPS382x Voltage Monitor With Watchdog Timer.
*
* \param[in] mr_pin is the watchdog Manual Reset pin.
*
* \return status/error code.
*/
int gpio_init_mr_pin(gpio_pin_t mr_pin);

#endif /* GPIO_H_ */

/** \} End of gpio group */
9 changes: 6 additions & 3 deletions firmware/drivers/tps382x/tps382x.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* tps382x.c
*
* Copyright (C) 2021, SpaceLab.
* Copyright The OBDH 2.0 Contributors.
*
* This file is part of OBDH 2.0.
*
Expand All @@ -25,7 +25,7 @@
*
* \author Gabriel Mariano Marcelino <[email protected]>
*
* \version 0.8.20
* \version 0.10.2
*
* \date 2020/01/15
*
Expand All @@ -45,12 +45,15 @@ int tps382x_init(tps382x_config_t config)

if (gpio_init(config.wdi_pin, gpio_conf) == 0)
{
if (gpio_init(config.mr_pin, gpio_conf) == 0)
if (gpio_init_mr_pin(config.mr_pin) == 0)
{
err = 0;
}
}

gpio_set_state(config.mr_pin, true);
gpio_set_state(config.wdi_pin, true);

return err;
}

Expand Down
2 changes: 1 addition & 1 deletion firmware/tests/drivers/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ FLAGS=-fpic -std=c99 -Wall -pedantic -Wshadow -Wpointer-arith -Wcast-qual -Wstri

CY15X102QN_TEST_FLAGS=$(FLAGS),--wrap=spi_init,--wrap=spi_select_slave,--wrap=spi_write,--wrap=spi_read,--wrap=spi_transfer,--wrap=gpio_init,--wrap=gpio_set_state,--wrap=gpio_get_state,--wrap=gpio_toggle

TPS382X_TEST_FLAGS=$(FLAGS),--wrap=gpio_init,--wrap=gpio_set_state,--wrap=gpio_get_state,--wrap=gpio_toggle
TPS382X_TEST_FLAGS=$(FLAGS),--wrap=gpio_init,--wrap=gpio_set_state,--wrap=gpio_get_state,--wrap=gpio_toggle,--wrap=gpio_init_mr_pin

TCA4311A_TEST_FLAGS=$(FLAGS),--wrap=i2c_init,--wrap=i2c_write,--wrap=i2c_read,--wrap=gpio_init,--wrap=gpio_set_state,--wrap=gpio_get_state,--wrap=gpio_toggle

Expand Down
23 changes: 18 additions & 5 deletions firmware/tests/drivers/tps382x_test.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* tps382x_test.c
*
* Copyright (C) 2021, SpaceLab.
* Copyright The OBDH 2.0 Contributors.
*
* This file is part of OBDH 2.0.
*
Expand All @@ -25,7 +25,7 @@
*
* \author Gabriel Mariano Marcelino <[email protected]>
*
* \version 0.8.19
* \version 0.10.2
*
* \date 2021/08/29
*
Expand Down Expand Up @@ -56,10 +56,23 @@ static void tps382x_init_test(void **state)

will_return(__wrap_gpio_init, 0);

expect_value(__wrap_gpio_init, pin, TPS382X_MR_PIN);
expect_value(__wrap_gpio_init, config.mode, GPIO_MODE_OUTPUT);

will_return(__wrap_gpio_init, 0);
expect_value(__wrap_gpio_init_mr_pin, mr_pin, TPS382X_MR_PIN);

will_return(__wrap_gpio_init_mr_pin, 0);


expect_value(__wrap_gpio_set_state, pin, TPS382X_MR_PIN);
expect_value(__wrap_gpio_set_state, level, true);

will_return(__wrap_gpio_set_state, 0);


expect_value(__wrap_gpio_set_state, pin, TPS382X_WDI_PIN);
expect_value(__wrap_gpio_set_state, level, true);

will_return(__wrap_gpio_set_state, 0);


assert_return_code(tps382x_init(conf), 0);
}
Expand Down
Loading

0 comments on commit 6dbe90e

Please sign in to comment.