Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions os/hal/ports/SN32/LLD/SN32F2xx/CT/hal_gpt_lld.c
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ void gpt_lld_start(GPTDriver *gptp) {
# if SN32_GPT_USE_CT16B0
if (&GPTD1 == gptp) {
sys1EnableCT16B0();
sys1ResetCT16B0();
CT16B0_ResetTimer();
# if !defined(SN32_CT16B0_SUPPRESS_ISR)
nvicEnableVector(SN32_CT16B0_NUMBER, SN32_GPT_CT16B0_IRQ_PRIORITY);
Expand All @@ -276,6 +277,7 @@ void gpt_lld_start(GPTDriver *gptp) {
# if SN32_GPT_USE_CT16B1
if (&GPTD2 == gptp) {
sys1EnableCT16B1();
sys1ResetCT16B1();
CT16B1_ResetTimer();
# if !defined(SN32_CT16B1_SUPPRESS_ISR)
nvicEnableVector(SN32_CT16B1_NUMBER, SN32_GPT_CT16B1_IRQ_PRIORITY);
Expand All @@ -286,6 +288,7 @@ void gpt_lld_start(GPTDriver *gptp) {
# if SN32_GPT_USE_CT16B2
if (&GPTD3 == gptp) {
sys1EnableCT16B2();
sys1ResetCT16B2();
CT16B2_ResetTimer();
# if !defined(SN32_CT16B2_SUPPRESS_ISR)
nvicEnableVector(SN32_CT16B2_NUMBER, SN32_GPT_CT16B2_IRQ_PRIORITY);
Expand All @@ -296,6 +299,7 @@ void gpt_lld_start(GPTDriver *gptp) {
# if SN32_GPT_USE_CT16B3
if (&GPTD4 == gptp) {
sys1EnableCT16B3();
sys1ResetCT16B3();
CT16B3_ResetTimer();
# if !defined(SN32_CT16B3_SUPPRESS_ISR)
nvicEnableVector(SN32_CT16B3_NUMBER, SN32_GPT_CT16B3_IRQ_PRIORITY);
Expand All @@ -306,6 +310,7 @@ void gpt_lld_start(GPTDriver *gptp) {
# if SN32_GPT_USE_CT16B4
if (&GPTD5 == gptp) {
sys1EnableCT16B4();
sys1ResetCT16B4();
CT16B4_ResetTimer();
# if !defined(SN32_CT16B4_SUPPRESS_ISR)
nvicEnableVector(SN32_CT16B4_NUMBER, SN32_GPT_CT16B4_IRQ_PRIORITY);
Expand All @@ -316,7 +321,8 @@ void gpt_lld_start(GPTDriver *gptp) {
# if SN32_GPT_USE_CT16B5
if (&GPTD6 == gptp) {
sys1EnableCT16B5();
CT16B6_ResetTimer();
sys1ResetCT16B5();
CT16B5_ResetTimer();
# if !defined(SN32_CT16B5_SUPPRESS_ISR)
nvicEnableVector(SN32_CT16B5_NUMBER, SN32_GPT_CT16B5_IRQ_PRIORITY);
# endif
Expand All @@ -328,32 +334,38 @@ void gpt_lld_start(GPTDriver *gptp) {
SN32_CT_GPT_SET(gptp, config.TMRCTRL, CT16_CEN_DIS); /* Timer disabled.*/
# if SN32_GPT_USE_CT16B0
if (&GPTD1 == gptp) {
sys1ResetCT16B0();
CT16B0_ResetTimer(); /* Counter reset to zero.*/
}
# endif
# if SN32_GPT_USE_CT16B1
if (&GPTD2 == gptp) {
sys1ResetCT16B1();
CT16B1_ResetTimer(); /* Counter reset to zero.*/
}
# endif
# if SN32_GPT_USE_CT16B2
if (&GPTD3 == gptp) {
sys1ResetCT16B2();
CT16B2_ResetTimer(); /* Counter reset to zero.*/
}
# endif
# if SN32_GPT_USE_CT16B3
if (&GPTD4 == gptp) {
sys1ResetCT16B3();
CT16B3_ResetTimer(); /* Counter reset to zero.*/
}
# endif
# if SN32_GPT_USE_CT16B4
if (&GPTD5 == gptp) {
sys1ResetCT16B4();
CT16B4_ResetTimer(); /* Counter reset to zero.*/
}
# endif
# if SN32_GPT_USE_CT16B5
if (&GPTD6 == gptp) {
CT16B4_ResetTimer(); /* Counter reset to zero.*/
sys1ResetCT16B5();
CT16B5_ResetTimer(); /* Counter reset to zero.*/
}
# endif
}
Expand Down Expand Up @@ -423,7 +435,7 @@ void gpt_lld_stop(GPTDriver *gptp) {
}
# endif
# if SN32_GPT_USE_CT16B5
if (&GPTD5 == gptp) {
if (&GPTD6 == gptp) {
# if !defined(SN32_CT16B5_SUPPRESS_ISR)
nvicDisableVector(SN32_CT16B5_NUMBER);
# endif
Expand Down
3 changes: 1 addition & 2 deletions os/hal/ports/SN32/LLD/SN32F2xx/CT/hal_gpt_lld.h
Original file line number Diff line number Diff line change
Expand Up @@ -638,8 +638,7 @@ struct GPTDriver {
*
* @notapi
*/
#define gpt_lld_get_counter(gptp) (gptcnt_t)SN32_CT_GPT_GET((gptp), config.TC)

#define gpt_lld_get_counter(gptp) (gptcnt_t)(SN32_CT_GPT_GET((gptp), config.TC) & SN32_CT16_TC_LIMIT)
/*===========================================================================*/
/* External declarations. */
/*===========================================================================*/
Expand Down
1 change: 1 addition & 0 deletions os/hal/ports/SN32/LLD/SN32F2xx/CT/hal_pwm_lld.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ void pwm_lld_start(PWMDriver *pwmp) {
# if SN32_PWM_USE_CT16B1
if (&PWMD1 == pwmp) {
sys1EnableCT16B1();
sys1ResetCT16B1();
CT16B1_ResetTimer();
# if !defined(SN32_CT16B1_SUPPRESS_ISR)
nvicEnableVector(SN32_CT16B1_NUMBER, SN32_PWM_CT16B1_IRQ_PRIORITY);
Expand Down
8 changes: 4 additions & 4 deletions os/hal/ports/SN32/LLD/SN32F2xx/GPIO/hal_pal_lld.c
Original file line number Diff line number Diff line change
Expand Up @@ -147,19 +147,19 @@ void _pal_lld_init(const PALConfig *config) {

#if (PAL_USE_WAIT == TRUE) || (PAL_USE_CALLBACKS == TRUE)
#if SN32_HAS_GPIOA
sys1EnableP0();
sys1EnableGPIO0();
nvicEnableVector(SN32_GPIOA_NUMBER, SN32_GPIOA_IRQ_PRIORITY);
#endif
#if SN32_HAS_GPIOB
sys1EnableP1();
sys1EnableGPIO1();
nvicEnableVector(SN32_GPIOB_NUMBER, SN32_GPIOB_IRQ_PRIORITY);
#endif
#if SN32_HAS_GPIOC
sys1EnableP2();
sys1EnableGPIO2();
nvicEnableVector(SN32_GPIOC_NUMBER, SN32_GPIOC_IRQ_PRIORITY);
#endif
#if SN32_HAS_GPIOD
sys1EnableP3();
sys1EnableGPIO3();
nvicEnableVector(SN32_GPIOD_NUMBER, SN32_GPIOD_IRQ_PRIORITY);
#endif

Expand Down
2 changes: 1 addition & 1 deletion os/hal/ports/SN32/LLD/SN32F2xx/GPIO/hal_pal_lld.h
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ typedef uint32_t iopadid_t;
*
* @notapi
*/
#define pal_lld_clearport(port, bits) ((port)->BCLR = ~(uint32_t)(bits))
#define pal_lld_clearport(port, bits) ((port)->BCLR = (uint32_t)(bits))

/**
* @brief Writes a group of bits.
Expand Down
3 changes: 3 additions & 0 deletions os/hal/ports/SN32/LLD/SN32F2xx/SysTick/hal_st_lld.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#endif

#define ST_ENABLE_CLOCK() sys1EnableCT16B0()
#define ST_RESET_CLOCK() sys1ResetCT16B0()
#define ST_INIT_CLOCK() CT16B0_ResetTimer()

#elif SN32_ST_USE_TIMER == SN32_TIM_CT16B1
Expand All @@ -53,6 +54,7 @@
#endif

#define ST_ENABLE_CLOCK() sys1EnableCT16B1()
#define ST_RESET_CLOCK() sys1ResetCT16B1()
#define ST_INIT_CLOCK() CT16B1_ResetTimer()

#else
Expand Down Expand Up @@ -128,6 +130,7 @@ void st_lld_init(void) {

/* Enabling timer clock.*/
ST_ENABLE_CLOCK();
ST_RESET_CLOCK();
ST_INIT_CLOCK();
/* Initializing the counter in free running mode.*/
SN32_ST_TIM->config.PRE = (SYSTICK_CK / OSAL_ST_FREQUENCY) - 1;
Expand Down
61 changes: 37 additions & 24 deletions os/hal/ports/SN32/LLD/SN32F2xx/SysTick/hal_st_lld.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,16 @@ static inline systime_t st_lld_get_counter(void) {
return (systime_t)(SN32_ST_TIM->config.TC & UINT16_MAX);
}

/**
* @brief Stops the alarm interrupt.
*
* @notapi
*/
static inline void st_lld_stop_alarm(void) {
SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk;
SCB->ICSR = SCB_ICSR_PENDSTCLR_Msk;
}

/**
* @brief Starts the alarm.
* @note Makes sure that no spurious alarms are triggered after
Expand All @@ -147,28 +157,42 @@ static inline systime_t st_lld_get_counter(void) {
*/
static inline void st_lld_start_alarm(systime_t abstime) {

/* The requested delay in OSAL_ST_FREQUENCY ticks, decreased by 1 to bring it
* into the 0...0xFFFF range instead of 1...0x10000. */
uint32_t delay = ((uint32_t)abstime - SN32_ST_TIM->config.TC - 1U) & SN32_CT16_TC_LIMIT;
uint32_t now = SN32_ST_TIM->config.TC;
uint32_t delay = ((uint32_t)abstime - now) & SN32_CT16_TC_LIMIT;
uint32_t prescale = (SN32_ST_TIM->config.PRE & UINT8_MAX) + 1U;

/* Minimum safe delay */
uint32_t min_delay_ticks = 4U;

/* Handle wrap-around */
if (delay > (SN32_CT16_TC_LIMIT >> 1)) {
delay = min_delay_ticks;
}

/* The conversion factor between the SN32_ST_TIM and SysTick clock
* frequencies (SN32_HCLK / OSAL_ST_FREQUENCY).
* TODO: Actually use (SN32_HCLK / OSAL_ST_FREQUENCY) instead of reading the
* value from a hardware register (this requires making SN32_HCLK a compile
* time constant). */
uint32_t prescale = (SN32_ST_TIM->config.PRE & UINT8_MAX) + 1;
if (delay < min_delay_ticks) {
delay = min_delay_ticks;
}

/* The requested delay in the SysTick clock ticks. The maximum possible
* value with prescale=256 is 0xFFFFFF, which just fits into the 24-bit
* SysTick timer registers. */
uint32_t systick_delay = delay * prescale + (prescale - 1);
uint32_t systick_delay = delay * prescale;

if (systick_delay > 0xFFFFFFU) {
systick_delay = 0xFFFFFFU;
}

if (systick_delay > 0xFFFFFF) {
systick_delay = 0xFFFFFF;
if (systick_delay < prescale) {
systick_delay = prescale;
}

st_lld_stop_alarm();

/* Start SysTick to generate an interrupt after systick_delay. */
SysTick->LOAD = systick_delay;
SysTick->VAL = 0;
SysTick->VAL = 0U;

SCB->ICSR = SCB_ICSR_PENDSTCLR_Msk;
SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk |
SysTick_CTRL_ENABLE_Msk |
SysTick_CTRL_TICKINT_Msk;
Expand All @@ -182,16 +206,6 @@ static inline void st_lld_start_alarm(systime_t abstime) {
#endif
}

/**
* @brief Stops the alarm interrupt.
*
* @notapi
*/
static inline void st_lld_stop_alarm(void) {
SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk;
SCB->ICSR = SCB_ICSR_PENDSTCLR_Msk;
}

/**
* @brief Sets the alarm time.
*
Expand All @@ -200,7 +214,6 @@ static inline void st_lld_stop_alarm(void) {
* @notapi
*/
static inline void st_lld_set_alarm(systime_t abstime) {
st_lld_stop_alarm();
st_lld_start_alarm(abstime);
}

Expand Down
20 changes: 12 additions & 8 deletions os/hal/ports/SN32/SN32F240/sn32_sys1.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@
* @api
*/
#define sys1EnableGPIO() sys1EnableAHB(0x1<<0)
#define sys1EnableGPIO0() sys1EnableGPIO()
#define sys1EnableGPIO1() sys1EnableGPIO()
#define sys1EnableGPIO2() sys1EnableGPIO()
#define sys1EnableGPIO3() sys1EnableGPIO()

/**
* @brief Disables the GPIO peripheral clock.
Expand All @@ -144,32 +148,32 @@
#define sys1DisableGPIO() sys1DisableAHB(0x1<<0)

/**
* @brief Resets the GPIOP0 peripheral.
* @brief Resets the GPIO0 peripheral.
*
* @api
*/
#define sys1ResetGPIOP0() sys1Reset(0x1<<0)
#define sys1ResetGPIO0() sys1Reset(0x1<<0)

/**
* @brief Resets the GPIOP1 peripheral.
* @brief Resets the GPIO1 peripheral.
*
* @api
*/
#define sys1ResetGPIOP1() sys1Reset(0x1<<1)
#define sys1ResetGPIO1() sys1Reset(0x1<<1)

/**
* @brief Resets the GPIOP2 peripheral.
* @brief Resets the GPIO2 peripheral.
*
* @api
*/
#define sys1ResetGPIOP2() sys1Reset(0x1<<2)
#define sys1ResetGPIO2() sys1Reset(0x1<<2)

/**
* @brief Resets the GPIOP3 peripheral.
* @brief Resets the GPIO3 peripheral.
*
* @api
*/
#define sys1ResetGPIOP3() sys1Reset(0x1<<3)
#define sys1ResetGPIO3() sys1Reset(0x1<<3)

/**
* @name USB peripherals specific SYS1 operations
Expand Down
Loading
Loading