Skip to content

Commit

Permalink
fix mqtt, pwm on boot
Browse files Browse the repository at this point in the history
  • Loading branch information
NonPIayerCharacter committed Dec 25, 2024
1 parent cf3a883 commit b7b52dc
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 69 deletions.
2 changes: 1 addition & 1 deletion sdk/OpenRTL87X0C
35 changes: 22 additions & 13 deletions src/driver/drv_bl0937.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
extern rtlPinMapping_t g_pins[];
rtlPinMapping_t* rtl_cf;
rtlPinMapping_t* rtl_cf1;
gpio_irq_t irqcf, irqcf1;

#else

Expand Down Expand Up @@ -221,8 +220,12 @@ void BL0937_Shutdown_Pins()

#elif PLATFORM_RTL87X0C

gpio_irq_free(&irqcf1);
gpio_irq_free(&irqcf);
gpio_irq_free(rtl_cf1->irq);
gpio_irq_free(rtl_cf->irq);
os_free(rtl_cf1->irq);
os_free(rtl_cf->irq);
rtl_cf1->irq = NULL;
rtl_cf->irq = NULL;

#endif
}
Expand Down Expand Up @@ -257,16 +260,22 @@ void BL0937_Init_Pins()

rtl_cf = g_pins + GPIO_HLW_CF;
rtl_cf1 = g_pins + GPIO_HLW_CF1;
if(rtl_cf->isInit)
if(rtl_cf->gpio != NULL)
{
hal_pinmux_unregister(rtl_cf->pin, PID_GPIO);
os_free(rtl_cf->gpio);
rtl_cf->gpio = NULL;
}
if(rtl_cf1->isInit)
if(rtl_cf1->gpio != NULL)
{
hal_pinmux_unregister(rtl_cf1->pin, PID_GPIO);
os_free(rtl_cf1->gpio);
rtl_cf1->gpio = NULL;
}
rtl_cf->isInit = true;
rtl_cf1->isInit = true;
rtl_cf1->irq = os_malloc(sizeof(gpio_irq_t));
rtl_cf->irq = os_malloc(sizeof(gpio_irq_t));
memset(rtl_cf1->irq, 0, sizeof(gpio_irq_t));
memset(rtl_cf->irq, 0, sizeof(gpio_irq_t));

#endif

Expand Down Expand Up @@ -300,9 +309,9 @@ void BL0937_Init_Pins()

#elif PLATFORM_RTL87X0C

gpio_irq_init(&irqcf1, rtl_cf1->pin, cf1_irq_handler, NULL);
gpio_irq_set(&irqcf1, IRQ_FALL, 1);
gpio_irq_enable(&irqcf1);
gpio_irq_init(rtl_cf1->irq, rtl_cf1->pin, cf1_irq_handler, NULL);
gpio_irq_set(rtl_cf1->irq, IRQ_FALL, 1);
gpio_irq_enable(rtl_cf1->irq);

#endif

Expand Down Expand Up @@ -331,9 +340,9 @@ void BL0937_Init_Pins()

#elif PLATFORM_RTL87X0C

gpio_irq_init(&irqcf, rtl_cf->pin, cf_irq_handler, NULL);
gpio_irq_set(&irqcf, IRQ_FALL, 1);
gpio_irq_enable(&irqcf);
gpio_irq_init(rtl_cf->irq, rtl_cf->pin, cf_irq_handler, NULL);
gpio_irq_set(rtl_cf->irq, IRQ_FALL, 1);
gpio_irq_enable(rtl_cf->irq);

#endif

Expand Down
6 changes: 3 additions & 3 deletions src/hal/rtl87x0c/hal_generic_rtl87x0c.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ typedef struct rtlPinMapping_s
{
const char* name;
PinName pin;
bool isInit;
gpio_t gpio;
pwmout_t pwm;
gpio_t* gpio;
gpio_irq_t* irq;
pwmout_t* pwm;
} rtlPinMapping_t;

#endif // PLATFORM_RTL87X0C
116 changes: 66 additions & 50 deletions src/hal/rtl87x0c/hal_pins_rtl87x0c.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,30 @@
extern int g_pwmFrequency;

rtlPinMapping_t g_pins[] = {
{ "PA0 (RX1)", PA_0, false },
{ "PA1 (TX1)", PA_1, false },
{ "PA2 (RX1)", PA_2, false },
{ "PA3 (TX1)", PA_3, false },
{ "PA4", PA_4, false },
{ "PA5", PA_5, false },
{ "PA6", PA_6, false },
{ "PA7", PA_7, false },
{ "PA8", PA_8, false },
{ "PA9", PA_9, false },
{ "PA10", PA_10, false },
{ "PA11", PA_11, false },
{ "PA12", PA_12, false },
{ "PA13 (RX0)", PA_13, false },
{ "PA14 (TX0)", PA_14, false },
{ "PA15 (RX2)", PA_15, false },
{ "PA16 (TX2)", PA_16, false },
{ "PA17", PA_17, false },
{ "PA18", PA_18, false },
{ "PA19", PA_19, false },
{ "PA20", PA_20, false },
{ "PA21", PA_21, false },
{ "PA22", PA_22, false },
{ "PA23", PA_23, false },
{ "PA0 (RX1)", PA_0, NULL, NULL },
{ "PA1 (TX1)", PA_1, NULL, NULL },
{ "PA2 (RX1)", PA_2, NULL, NULL },
{ "PA3 (TX1)", PA_3, NULL, NULL },
{ "PA4", PA_4, NULL, NULL },
{ "PA5", PA_5, NULL, NULL },
{ "PA6", PA_6, NULL, NULL },
{ "PA7", PA_7, NULL, NULL },
{ "PA8", PA_8, NULL, NULL },
{ "PA9", PA_9, NULL, NULL },
{ "PA10", PA_10, NULL, NULL },
{ "PA11", PA_11, NULL, NULL },
{ "PA12", PA_12, NULL, NULL },
{ "PA13 (RX0)", PA_13, NULL, NULL },
{ "PA14 (TX0)", PA_14, NULL, NULL },
{ "PA15 (RX2)", PA_15, NULL, NULL },
{ "PA16 (TX2)", PA_16, NULL, NULL },
{ "PA17", PA_17, NULL, NULL },
{ "PA18", PA_18, NULL, NULL },
{ "PA19", PA_19, NULL, NULL },
{ "PA20", PA_20, NULL, NULL },
{ "PA21", PA_21, NULL, NULL },
{ "PA22", PA_22, NULL, NULL },
{ "PA23", PA_23, NULL, NULL },
};

static int g_numPins = sizeof(g_pins) / sizeof(g_pins[0]);
Expand All @@ -55,99 +55,115 @@ int HAL_PIN_CanThisPinBePWM(int index)
return 1;
}

void RTL_Gpio_Init(rtlPinMapping_t* pin)
void RTL_GPIO_Init(rtlPinMapping_t* pin)
{
if(pin->isInit)
if(pin->gpio != NULL)
{
return;
}
gpio_init(&pin->gpio, pin->pin);
pin->isInit = true;
//if(pin->irq != NULL)
//{
// hal_pinmux_unregister(pin->pin, PID_GPIO);
// pin->irq = NULL;
//}
//if(pin->pwm != NULL)
//{
// pwmout_free(pin->pwm);
// pin->pwm = NULL;
//}
pin->gpio = os_malloc(sizeof(gpio_t));
memset(pin->gpio, 0, sizeof(gpio_t));
gpio_init(pin->gpio, pin->pin);
}

void HAL_PIN_SetOutputValue(int index, int iVal)
{
if(index >= g_numPins)
return;
rtlPinMapping_t* pin = g_pins + index;
gpio_write(&pin->gpio, iVal ? 1 : 0);
gpio_write(pin->gpio, iVal ? 1 : 0);
}

int HAL_PIN_ReadDigitalInput(int index)
{
if(index >= g_numPins)
return 0;
rtlPinMapping_t* pin = g_pins + index;
return gpio_read(&pin->gpio);
return gpio_read(pin->gpio);
}

void HAL_PIN_Setup_Input_Pullup(int index)
{
if(index >= g_numPins)
return;
rtlPinMapping_t* pin = g_pins + index;
RTL_Gpio_Init(pin);
gpio_dir(&pin->gpio, PIN_INPUT);
gpio_mode(&pin->gpio, PullUp);
RTL_GPIO_Init(pin);
gpio_dir(pin->gpio, PIN_INPUT);
gpio_mode(pin->gpio, PullUp);
}

void HAL_PIN_Setup_Input_Pulldown(int index)
{
if(index >= g_numPins)
return;
rtlPinMapping_t* pin = g_pins + index;
RTL_Gpio_Init(pin);
gpio_dir(&pin->gpio, PIN_INPUT);
gpio_mode(&pin->gpio, PullDown);
RTL_GPIO_Init(pin);
gpio_dir(pin->gpio, PIN_INPUT);
gpio_mode(pin->gpio, PullDown);
}

void HAL_PIN_Setup_Input(int index)
{
if(index >= g_numPins)
return;
rtlPinMapping_t* pin = g_pins + index;
RTL_Gpio_Init(pin);
gpio_dir(&pin->gpio, PIN_INPUT);
gpio_mode(&pin->gpio, PullNone);
RTL_GPIO_Init(pin);
gpio_dir(pin->gpio, PIN_INPUT);
gpio_mode(pin->gpio, PullNone);
}

void HAL_PIN_Setup_Output(int index)
{
if(index >= g_numPins)
return;
rtlPinMapping_t* pin = g_pins + index;
RTL_Gpio_Init(pin);
gpio_dir(&pin->gpio, PIN_OUTPUT);
gpio_mode(&pin->gpio, PullUp);
RTL_GPIO_Init(pin);
gpio_dir(pin->gpio, PIN_OUTPUT);
gpio_mode(pin->gpio, PullUp);
}

void HAL_PIN_PWM_Stop(int index)
{
if(index >= g_numPins || !HAL_PIN_CanThisPinBePWM(index))
return;
rtlPinMapping_t* pin = g_pins + index;
//pwmout_stop(&pin->pwm);
pwmout_free(&pin->pwm);
pin->isInit = false;
if(pin->pwm == NULL) return;
//pwmout_stop(pin->pwm);
pwmout_free(pin->pwm);
os_free(pin->pwm);
pin->pwm = NULL;
}

void HAL_PIN_PWM_Start(int index)
{
if(index >= g_numPins || !HAL_PIN_CanThisPinBePWM(index))
return;
rtlPinMapping_t* pin = g_pins + index;
pwmout_init(&pin->pwm, pin->pin);
pwmout_period_us(&pin->pwm, g_pwmFrequency);
pwmout_start(&pin->pwm);
pin->isInit = true;
if(pin->pwm != NULL) return;
pin->pwm = os_malloc(sizeof(pwmout_t));
memset(pin->pwm, 0, sizeof(pwmout_t));
pwmout_init(pin->pwm, pin->pin);
pwmout_period_us(pin->pwm, g_pwmFrequency);
pwmout_start(pin->pwm);
}

void HAL_PIN_PWM_Update(int index, float value)
{
if(index >= g_numPins || !HAL_PIN_CanThisPinBePWM(index))
return;
rtlPinMapping_t* pin = g_pins + index;
pwmout_write(&pin->pwm, value / 100);
if(pin->pwm == NULL || !pin->pwm->is_init) return;
pwmout_write(pin->pwm, value / 100);
}

unsigned int HAL_GetGPIOPin(int index)
Expand Down
4 changes: 2 additions & 2 deletions src/new_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -521,8 +521,8 @@ typedef unsigned int UINT32;
#undef ASSERT
#define ASSERT

#define os_malloc malloc
#define os_free free
#define os_malloc pvPortMalloc
#define os_free vPortFree
#define os_memset memset
#define os_strcpy strcpy

Expand Down

0 comments on commit b7b52dc

Please sign in to comment.