Skip to content

Commit

Permalink
Merge pull request #369 from dexter93/sn32_hotfix_2
Browse files Browse the repository at this point in the history
SN32: PAL and GPT hotfixes
  • Loading branch information
fpoussin committed Jul 17, 2023
2 parents 6eb8acd + 06a69e0 commit 697a89a
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 36 deletions.
46 changes: 34 additions & 12 deletions os/hal/ports/SN32/LLD/SN32F2xx/CT/hal_gpt_lld.c
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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. */
}
Expand All @@ -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 */
Expand Down
2 changes: 1 addition & 1 deletion os/hal/ports/SN32/LLD/SN32F2xx/CT/hal_gpt_lld.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
35 changes: 16 additions & 19 deletions os/hal/ports/SN32/LLD/SN32F2xx/GPIO/hal_pal_lld.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
23 changes: 19 additions & 4 deletions os/hal/ports/SN32/LLD/SN32F2xx/GPIO/hal_pal_lld.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -344,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.
Expand Down

0 comments on commit 697a89a

Please sign in to comment.