From 121310bbfe2772c59235b77596a2ae60ab643d4a Mon Sep 17 00:00:00 2001 From: Dimitris Mantzouranis Date: Tue, 9 May 2023 15:00:37 +0300 Subject: [PATCH 1/3] sn32 pal: fix the pal modes --- .../SN32/LLD/SN32F2xx/GPIO/hal_pal_lld.c | 35 +++++++++---------- .../SN32/LLD/SN32F2xx/GPIO/hal_pal_lld.h | 18 ++++++++-- 2 files changed, 31 insertions(+), 22 deletions(-) diff --git a/os/hal/ports/SN32/LLD/SN32F2xx/GPIO/hal_pal_lld.c b/os/hal/ports/SN32/LLD/SN32F2xx/GPIO/hal_pal_lld.c index 5935e92ddb..a771c4bac2 100644 --- a/os/hal/ports/SN32/LLD/SN32F2xx/GPIO/hal_pal_lld.c +++ b/os/hal/ports/SN32/LLD/SN32F2xx/GPIO/hal_pal_lld.c @@ -103,43 +103,40 @@ void _pal_lld_setpadmode(ioportid_t port, uint32_t pad, iomode_t mode) { - switch (mode) - { + switch (mode) { - case PAL_MODE_UNCONNECTED: + case PAL_MODE_INPUT_ANALOG: + //set MODE as INPUT + port->MODE &= ~(1 << pad); + // disable pull up resistor + // disable Schmitt trigger + // keep DATA low + port->CFG |= (3 << (pad * 2)); break; case PAL_MODE_INPUT: + //set MODE as INPUT port->MODE &= ~(1 << pad); + // disable pull up resistor + // enable Schmitt trigger + port->CFG |= (2 << (pad * 2)); break; case PAL_MODE_INPUT_PULLUP: + //set MODE as INPUT port->MODE &= ~(1 << pad); - port->CFG &= ~(3 << (pad * 2)); - // port->BSET = (1 << pad); // High 1 - break; - - case PAL_MODE_INPUT_PULLDOWN: - port->MODE &= ~(1 << pad); - port->CFG &= ~(3 << (pad * 2)); - // port->BCLR = (1 << pad); // Low 0 - break; - - case PAL_MODE_INPUT_ANALOG: - port->MODE &= ~(1 << pad); + //enable pull up resistor port->CFG &= ~(3 << (pad * 2)); break; case PAL_MODE_OUTPUT_PUSHPULL: + //set MODE as OUTPUT port->MODE |= (1 << pad); break; - case 7: - break; - default: break; - } + } } #endif /* HAL_USE_PAL */ diff --git a/os/hal/ports/SN32/LLD/SN32F2xx/GPIO/hal_pal_lld.h b/os/hal/ports/SN32/LLD/SN32F2xx/GPIO/hal_pal_lld.h index ba73799838..e6a4aac6de 100644 --- a/os/hal/ports/SN32/LLD/SN32F2xx/GPIO/hal_pal_lld.h +++ b/os/hal/ports/SN32/LLD/SN32F2xx/GPIO/hal_pal_lld.h @@ -76,10 +76,22 @@ #define PAL_WHOLE_PORT ((ioportmask_t)0xFFFF) /** @} */ -// GPIO0 = 40044000 -// pad = 5 +/** + * @name Standard I/O mode flags + * @{ + */ +#undef PAL_MODE_RESET +#undef PAL_MODE_UNCONNECTED + +/** + * @brief Implemented as input. + */ +#define PAL_MODE_RESET PAL_MODE_INPUT -// line = 40044005 +/** + * @brief Implemented as input with DATA keep low. + */ +#define PAL_MODE_UNCONNECTED PAL_MODE_INPUT_ANALOG /** * @name Line handling macros From 9061c941c4b663cec990ffdeb11b70f5c6e80bf1 Mon Sep 17 00:00:00 2001 From: Dimitris Mantzouranis Date: Thu, 11 May 2023 14:41:56 +0300 Subject: [PATCH 2/3] sn32 pal: fix pal_lld_writepad We need to set the logical HIGH/LOW state here NOT mess with DATA --- os/hal/ports/SN32/LLD/SN32F2xx/GPIO/hal_pal_lld.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/os/hal/ports/SN32/LLD/SN32F2xx/GPIO/hal_pal_lld.h b/os/hal/ports/SN32/LLD/SN32F2xx/GPIO/hal_pal_lld.h index e6a4aac6de..a29a8c80b0 100644 --- a/os/hal/ports/SN32/LLD/SN32F2xx/GPIO/hal_pal_lld.h +++ b/os/hal/ports/SN32/LLD/SN32F2xx/GPIO/hal_pal_lld.h @@ -356,7 +356,10 @@ typedef SN_GPIO0_Type * ioportid_t; * * @notapi */ -#define pal_lld_writepad(port, pad, bit) ((port)->DATA = (bit << pad)) +#define pal_lld_writepad(port, pad, bit) do { \ + if(bit > PAL_LOW) pal_lld_setpad(port, pad); \ + else pal_lld_clearpad(port, pad); \ +} while(0) /** * @brief Sets a pad logical state to @p PAL_HIGH. From 06a69e0a613dd15e0d61cbd29d30eab935bc33ef Mon Sep 17 00:00:00 2001 From: Dimitris Mantzouranis Date: Mon, 22 May 2023 17:02:48 +0300 Subject: [PATCH 3/3] sn32: 2xx: GPT: cleanup the configuration add missing hooks for GPTD2 fixup the prescaler assert check update the interrupt handler --- .../ports/SN32/LLD/SN32F2xx/CT/hal_gpt_lld.c | 46 ++++++++++++++----- .../ports/SN32/LLD/SN32F2xx/CT/hal_gpt_lld.h | 2 +- 2 files changed, 35 insertions(+), 13 deletions(-) diff --git a/os/hal/ports/SN32/LLD/SN32F2xx/CT/hal_gpt_lld.c b/os/hal/ports/SN32/LLD/SN32F2xx/CT/hal_gpt_lld.c index 3d26251089..4c37029cf6 100644 --- a/os/hal/ports/SN32/LLD/SN32F2xx/CT/hal_gpt_lld.c +++ b/os/hal/ports/SN32/LLD/SN32F2xx/CT/hal_gpt_lld.c @@ -162,14 +162,28 @@ void gpt_lld_start(GPTDriver *gptp) { } #endif } + else { + /* Driver re-configuration scenario, it must be stopped first.*/ + gptp->ct->TMRCTRL = CT16_CEN_DIS; /* Timer disabled. */ +#if SN32_GPT_USE_CT16B0 + if (&GPTD1 == gptp) { + CT16B0_ResetTimer(); /* Counter reset to zero. */ + } +#endif +#if SN32_GPT_USE_CT16B1 + if (&GPTD2 == gptp) { + CT16B1_ResetTimer(); /* Counter reset to zero. */ + } +#endif + } /* Prescaler value calculation.*/ - psc = (uint8_t)((gptp->clock / gptp->config->frequency) - 1); - osalDbgAssert(((uint32_t)(psc + 1) * gptp->config->frequency) == gptp->clock, + psc = ((gptp->clock / gptp->config->frequency) - 1); + osalDbgAssert((psc <= 0xFF) && /* Prescaler calculation. */ + ((psc + 1) * gptp->config->frequency) == gptp->clock, "invalid frequency"); /* Timer configuration.*/ - gptp->ct->TMRCTRL = CT16_CEN_DIS; /* Initially stopped. */ gptp->ct->CNTCTRL = gptp->config->cntctrl; gptp->ct->PRE = psc; /* Prescaler value. */ gptp->ct->IC &= 0x1FFFFFF; /* Clear pending IRQs. */ @@ -219,7 +233,16 @@ void gpt_lld_stop(GPTDriver *gptp) { void gpt_lld_start_timer(GPTDriver *gptp, gptcnt_t interval) { gptp->ct->MR0 = (uint32_t)(interval - 1U); /* Time constant. */ - gptp->ct->TMRCTRL = mskCT16_CRST; /* Reset counter. */ +#if SN32_GPT_USE_CT16B0 + if (&GPTD1 == gptp) { + CT16B0_ResetTimer(); /* Counter reset to zero. */ + } +#endif +#if SN32_GPT_USE_CT16B1 + if (&GPTD2 == gptp) { + CT16B1_ResetTimer(); /* Counter reset to zero. */ + } +#endif gptp->ct->IC &= 0x1FFFFFF; /* Clear pending IRQs. */ if (NULL != gptp->config->callback) gptp->ct->MCTRL |= mskCT16_MR0IE_EN; @@ -255,10 +278,10 @@ void gpt_lld_stop_timer(GPTDriver *gptp) { void gpt_lld_polled_delay(GPTDriver *gptp, gptcnt_t interval) { gptp->ct->MR0 = (uint32_t)(interval - 1U); /* Time constant. */ - gptp->ct->IC &= 0x1FFFFFF; /* Clear pending IRQs. */ gptp->ct->MCTRL = (mskCT16_MR0IE_EN | mskCT16_MR0STOP_EN); + gptp->ct->IC &= 0x1FFFFFF; /* Clear pending IRQs. */ gptp->ct->TMRCTRL |= mskCT16_CEN_EN; - while (!(gptp->ct->RIS & mskCT16_MR0IF)) + while ((gptp->ct->RIS & mskCT16_MR0IF)!= 0) ; gptp->ct->IC &= 0x1FFFFFF; /* Clear pending IRQs. */ } @@ -271,13 +294,12 @@ void gpt_lld_polled_delay(GPTDriver *gptp, gptcnt_t interval) { * @notapi */ void gpt_lld_serve_interrupt(GPTDriver *gptp) { + uint32_t ris; - gptp->ct->IC &= 0x1FFFFFF; - if (gptp->state == GPT_ONESHOT) { - gptp->state = GPT_READY; /* Back in GPT_READY state. */ - gpt_lld_stop_timer(gptp); /* Timer automatically stopped. */ - } - gptp->config->callback(gptp); + ris = gptp->ct->RIS; + gptp->ct->IC = ris; + if ((ris & mskCT16_MR0IF) != 0) + _gpt_isr_invoke_cb(gptp); } #endif /* HAL_USE_GPT */ diff --git a/os/hal/ports/SN32/LLD/SN32F2xx/CT/hal_gpt_lld.h b/os/hal/ports/SN32/LLD/SN32F2xx/CT/hal_gpt_lld.h index cdfb960d49..5d4870e3fb 100644 --- a/os/hal/ports/SN32/LLD/SN32F2xx/CT/hal_gpt_lld.h +++ b/os/hal/ports/SN32/LLD/SN32F2xx/CT/hal_gpt_lld.h @@ -139,7 +139,7 @@ typedef uint32_t gptfreq_t; /** * @brief GPT counter type. */ -typedef uint16_t gptcnt_t; +typedef uint32_t gptcnt_t; /** * @brief Driver configuration structure.