2015-06-02 02:01:27 +00:00
|
|
|
/* mbed Microcontroller Library
|
|
|
|
*******************************************************************************
|
|
|
|
* Copyright (c) 2015 WIZnet Co.,Ltd. All rights reserved.
|
|
|
|
* All rights reserved.
|
2015-05-14 07:46:10 +00:00
|
|
|
*
|
2015-06-02 02:01:27 +00:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
2015-05-14 07:46:10 +00:00
|
|
|
*
|
2015-06-02 02:01:27 +00:00
|
|
|
* 1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of ARM Limited nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
2015-05-14 07:46:10 +00:00
|
|
|
*
|
2015-06-02 02:01:27 +00:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*******************************************************************************
|
2015-05-14 07:46:10 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
#include "us_ticker_api.h"
|
|
|
|
#include "PeripheralNames.h"
|
|
|
|
#include "system_W7500x.h"
|
2015-09-02 08:55:26 +00:00
|
|
|
#include "W7500x_dualtimer.h"
|
|
|
|
#include "W7500x_pwm.h"
|
2015-05-14 07:46:10 +00:00
|
|
|
|
2015-07-09 00:23:43 +00:00
|
|
|
#define TIMER_0 DUALTIMER0_0
|
|
|
|
#define TIMER_1 PWM_CH1
|
|
|
|
#define TIMER_IRQn DUALTIMER0_IRQn
|
2015-05-14 07:46:10 +00:00
|
|
|
|
2015-07-09 00:23:43 +00:00
|
|
|
static PWM_TimerModeInitTypeDef TimerInitType;
|
2015-07-24 05:31:48 +00:00
|
|
|
static DUALTIMER_InitTypDef TimerHandler;
|
2015-05-14 07:46:10 +00:00
|
|
|
|
|
|
|
static int us_ticker_inited = 0;
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C"{
|
|
|
|
#endif
|
|
|
|
|
2015-07-09 00:23:43 +00:00
|
|
|
void DUALTIMER0_Handler(void)
|
|
|
|
{
|
2018-07-16 05:44:15 +00:00
|
|
|
DUALTIMER_IntClear(DUALTIMER0_0);
|
|
|
|
us_ticker_irq_handler();
|
2015-05-14 07:46:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void us_ticker_init(void)
|
|
|
|
{
|
|
|
|
if (us_ticker_inited) return;
|
|
|
|
us_ticker_inited = 1;
|
|
|
|
|
|
|
|
SystemCoreClockUpdate();
|
2015-07-09 00:23:43 +00:00
|
|
|
TimerInitType.PWM_CHn_PR = (GetSystemClock() / 1000000) -1;
|
|
|
|
TimerInitType.PWM_CHn_LR = 0xFFFFFFFF;
|
|
|
|
TimerInitType.PWM_CHn_PDMR = 1;
|
2015-05-14 07:46:10 +00:00
|
|
|
|
2015-07-09 00:23:43 +00:00
|
|
|
PWM_TimerModeInit(TIMER_1, &TimerInitType);
|
|
|
|
PWM_CHn_Start(TIMER_1);
|
2015-05-14 07:46:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
uint32_t us_ticker_read()
|
|
|
|
{
|
|
|
|
if (!us_ticker_inited) us_ticker_init();
|
2015-07-09 00:23:43 +00:00
|
|
|
return (TIMER_1->TCR);
|
2015-05-14 07:46:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void us_ticker_set_interrupt(timestamp_t timestamp)
|
|
|
|
{
|
|
|
|
int32_t dev = 0;
|
2015-07-09 00:23:43 +00:00
|
|
|
|
2015-05-14 07:46:10 +00:00
|
|
|
if (!us_ticker_inited)
|
|
|
|
{
|
|
|
|
us_ticker_init();
|
|
|
|
}
|
2015-05-25 23:55:30 +00:00
|
|
|
|
2015-07-20 05:54:56 +00:00
|
|
|
dev = (int32_t)(timestamp - us_ticker_read());
|
2015-07-17 02:01:48 +00:00
|
|
|
dev = dev * ((GetSystemClock() / 1000000) / 16);
|
|
|
|
|
2015-07-09 00:23:43 +00:00
|
|
|
DUALTIMER_ClockEnable(TIMER_0);
|
|
|
|
DUALTIMER_Stop(TIMER_0);
|
|
|
|
|
|
|
|
TimerHandler.TimerControl_Mode = DUALTIMER_TimerControl_Periodic;
|
|
|
|
TimerHandler.TimerControl_OneShot = DUALTIMER_TimerControl_OneShot;
|
|
|
|
TimerHandler.TimerControl_Pre = DUALTIMER_TimerControl_Pre_16;
|
|
|
|
TimerHandler.TimerControl_Size = DUALTIMER_TimerControl_Size_32;
|
|
|
|
|
|
|
|
TimerHandler.TimerLoad = (uint32_t)dev;
|
|
|
|
|
|
|
|
DUALTIMER_Init(TIMER_0, &TimerHandler);
|
|
|
|
|
|
|
|
DUALTIMER_IntConfig(TIMER_0, ENABLE);
|
|
|
|
|
|
|
|
NVIC_EnableIRQ(TIMER_IRQn);
|
|
|
|
|
|
|
|
DUALTIMER_Start(TIMER_0);
|
|
|
|
|
2015-05-14 07:46:10 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
Ticker: add fire interrupt now function
fire_interrupt function should be used for events in the past. As we have now
64bit timestamp, we can figure out what is in the past, and ask a target to invoke
an interrupt immediately. The previous attemps in the target HAL tickers were not ideal, as it can wrap around easily (16 or 32 bit counters). This new
functionality should solve this problem.
set_interrupt for tickers in HAL code should not handle anything but the next match interrupt. If it was in the past is handled by the upper layer.
It is possible that we are setting next event to the close future, so once it is set it is already in the past. Therefore we add a check after set interrupt to verify it is in future.
If it is not, we fire interrupt immediately. This results in
two events - first one immediate, correct one. The second one might be scheduled in far future (almost entire ticker range),
that should be discarded.
The specification for the fire_interrupts are:
- should set pending bit for the ticker interrupt (as soon as possible),
the event we are scheduling is already in the past, and we do not want to skip
any events
- no arguments are provided, neither return value, not needed
- ticker should be initialized prior calling this function (no need to check if it is already initialized)
All our targets provide this new functionality, removing old misleading if (timestamp is in the past) checks.
2017-06-27 11:18:59 +00:00
|
|
|
void us_ticker_fire_interrupt(void)
|
|
|
|
{
|
|
|
|
NVIC_SetPendingIRQ(TIMER_IRQn);
|
|
|
|
}
|
|
|
|
|
2015-05-14 07:46:10 +00:00
|
|
|
void us_ticker_disable_interrupt(void)
|
|
|
|
{
|
2015-07-09 00:23:43 +00:00
|
|
|
NVIC_DisableIRQ(TIMER_IRQn);
|
|
|
|
|
|
|
|
DUALTIMER_IntConfig(TIMER_0, DISABLE);
|
2015-05-14 07:46:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void us_ticker_clear_interrupt(void)
|
|
|
|
{
|
2015-07-09 00:23:43 +00:00
|
|
|
DUALTIMER_IntClear(TIMER_0);
|
2015-05-14 07:46:10 +00:00
|
|
|
}
|
2018-07-25 06:40:30 +00:00
|
|
|
|
|
|
|
void us_ticker_free(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|