drv_timer.c 9.5 KB
Newer Older
O
onelife.real 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/******************************************************************//**
 * @file 		drv_timer.c
 * @brief 	USART driver of RT-Thread RTOS for EFM32
 * 	COPYRIGHT (C) 2011, RT-Thread Development Team
 * @author 	onelife
 * @version 	0.4 beta
 **********************************************************************
 * @section License
 * The license and distribution terms for this file may be found in the file LICENSE in this 
 * distribution or at http://www.rt-thread.org/license/LICENSE
 **********************************************************************
 * @section Change Logs
 * Date			Author		Notes
 * 2011-01-18	onelife		Initial creation for EFM32
O
onelife.real 已提交
15
 * 2011-06-17	onelife		Modify init function for efm32lib v2 upgrading
O
onelife.real 已提交
16 17 18 19 20 21 22 23 24 25 26
 *********************************************************************/
 
/******************************************************************//**
* @addtogroup efm32
* @{
*********************************************************************/

/* Includes -------------------------------------------------------------------*/
#include "board.h"
#include "drv_timer.h"

O
onelife.real 已提交
27
#if (defined(RT_USING_TIMER0) || defined(RT_USING_TIMER1) || defined(RT_USING_TIMER2))
O
onelife.real 已提交
28 29 30
/* Private typedef -------------------------------------------------------------*/
/* Private define --------------------------------------------------------------*/
/* Private macro --------------------------------------------------------------*/
O
onelife.real 已提交
31 32 33 34 35
#ifdef RT_TIMER_DEBUG
#define timer_debug(format,args...) 		rt_kprintf(format, ##args)
#else
#define timer_debug(format,args...)
#endif
O
onelife.real 已提交
36
#define TIMER_TopCalculate(p) \
O
onelife.real@gmail.com 已提交
37
		(p * (EFM32_HFXO_FREQUENCY / (1 << TMR_CFG_PRESCALER) / 1000))
O
onelife.real 已提交
38 39

/* Private variables ------------------------------------------------------------*/
O
onelife.real 已提交
40 41 42
#ifdef RT_USING_TIMER0
	static struct rt_device timer0_device;
#endif
O
onelife.real 已提交
43 44 45
#ifdef RT_USING_TIMER1
	static struct rt_device timer1_device;
#endif
O
onelife.real 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
#ifdef RT_USING_TIMER2
	static struct rt_device timer2_device;
#endif

/* Private function prototypes ---------------------------------------------------*/
/* Private functions ------------------------------------------------------------*/
/******************************************************************//**
 * @brief
 *   Initialize Timer device
 *
 * @details
 *
 * @note
 *
 * @param[in] dev
 *   Pointer to device descriptor
 *
 * @return
 *   Error code
 *********************************************************************/
 static rt_err_t rt_hs_timer_init (rt_device_t dev)
{
	RT_ASSERT(dev != RT_NULL);

	struct efm32_timer_device_t *timer;

	timer = (struct efm32_timer_device_t *)(dev->user_data);

	timer->hook.cbFunc = RT_NULL;
	timer->hook.userPtr = RT_NULL;

	return RT_EOK;
}

/******************************************************************//**
* @brief
*	Configure Timer device
*
* @details
*
* @note
*
* @param[in] dev
*	Pointer to device descriptor
*
* @param[in] cmd
*	Timer control command
*
* @param[in] args
*	Arguments
*
* @return
*	Error code
*********************************************************************/
static rt_err_t rt_hs_timer_control (
	rt_device_t 	dev, 
	rt_uint8_t 		cmd, 
	void 			*args)
{
O
onelife.real@gmail.com 已提交
105 106
	RT_ASSERT(dev != RT_NULL);

O
onelife.real 已提交
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
	struct efm32_timer_device_t *timer;

	timer = (struct efm32_timer_device_t *)(dev->user_data);

	switch (cmd)
	{
	case RT_DEVICE_CTRL_SUSPEND:
		/* Suspend device */
		dev->flag |= RT_DEVICE_FLAG_SUSPENDED;
		TIMER_Enable(timer->timer_device, false);
		break;

	case RT_DEVICE_CTRL_RESUME:
		/* Resume device */
		dev->flag &= ~RT_DEVICE_FLAG_SUSPENDED;
		TIMER_Enable(timer->timer_device, true);
		break;

O
onelife.real@gmail.com 已提交
125
	case RT_DEVICE_CTRL_TIMER_PERIOD:
O
onelife.real 已提交
126 127 128
		{
			/* change device setting */
			struct efm32_timer_control_t *control;
O
onelife.real 已提交
129
			rt_uint32_t running;
O
onelife.real 已提交
130 131

			control = (struct efm32_timer_control_t *)args;
O
onelife.real 已提交
132 133
			running = timer->timer_device->STATUS & 0x00000001;
			
O
onelife.real 已提交
134 135 136 137 138
			TIMER_Enable(timer->timer_device, false);
			timer->timer_device->CNT = _TIMER_CNT_RESETVALUE;
			TIMER_TopSet(timer->timer_device, TIMER_TopCalculate(control->period));
			timer->hook.cbFunc = control->hook.cbFunc;
			timer->hook.userPtr = control->hook.userPtr;
O
onelife.real 已提交
139 140 141 142
			if (running)
			{
				TIMER_Enable(timer->timer_device, true);
			}
O
onelife.real 已提交
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
		}
		break;
	}

	return RT_EOK;
}

/******************************************************************//**
* @brief
*	Register Timer device
*
* @details
*
* @note
*
* @param[in] device
*	Pointer to device descriptor
*
* @param[in] name
*	Device name
*
* @param[in] flag
*	Configuration flags
*
* @param[in] timer
*	Pointer to Timer device descriptor 
*
* @return
*	Error code
*********************************************************************/
rt_err_t rt_hw_timer_register(
	rt_device_t		device, 
	const char		*name, 
	rt_uint32_t		flag, 
	struct efm32_timer_device_t *timer)
{
	RT_ASSERT(device != RT_NULL);

	device->type 		= RT_Device_Class_Char; /* fixme: should be timer type*/
	device->rx_indicate = RT_NULL;
	device->tx_complete = RT_NULL;
	device->init 		= rt_hs_timer_init;
	device->open		= RT_NULL;
	device->close		= RT_NULL;
	device->read 		= RT_NULL;
	device->write 		= RT_NULL;
	device->control 	= rt_hs_timer_control;
	device->user_data	= timer;

	/* register a character device */
	return rt_device_register(device, name, flag);
}

/******************************************************************//**
 * @brief
 *	Timer counter overflow interrupt handler
 *
 * @details
 *
 * @note
 *********************************************************************/
void rt_hw_timer_isr(rt_device_t dev)
{
O
onelife.real@gmail.com 已提交
206 207
	RT_ASSERT(dev != RT_NULL);

O
onelife.real 已提交
208 209 210 211 212 213 214 215 216 217
	struct efm32_timer_device_t *timer;

	timer = (struct efm32_timer_device_t *)(dev->user_data);

	if (timer->hook.cbFunc != RT_NULL)
	{
		(timer->hook.cbFunc)(timer->hook.userPtr);
	}
}

O
onelife.real 已提交
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
/******************************************************************//**
* @brief
*	Initialize the specified TIMER unit 
*
* @details
*
* @note
*
* @param[in] device
*	Pointer to device descriptor
*
* @param[in] unitNumber
*	Unit number
*
* @return
*	Pointer to TIMER device  
*********************************************************************/
static struct efm32_timer_device_t *rt_hw_timer_unit_init(
	rt_device_t device,
	rt_uint8_t 	unitNumber,
	rt_uint8_t 	mode)
{
	struct efm32_timer_device_t 	*timer;
	TIMER_Init_TypeDef 				init;
	efm32_irq_hook_init_t			hook;
	CMU_Clock_TypeDef				timerClock;
	IRQn_Type 						timerIrq;

	do
	{
		/* Allocate device */
		timer = rt_malloc(sizeof(struct efm32_timer_device_t));
		if (timer == RT_NULL)
		{
			timer_debug("no memory for TIMER%d driver\n"), unitNumber;
			break;
		}
		
		/* Initialization */
		if (unitNumber >= TIMER_COUNT)
		{
			break;
		}
		switch (unitNumber)
		{
		case 0:
			timer->timer_device = TIMER0;
			timerClock 			= (CMU_Clock_TypeDef)cmuClock_TIMER0;
			timerIrq 			= TIMER0_IRQn;
			break;
			
		case 1:
			timer->timer_device = TIMER1;
			timerClock 			= (CMU_Clock_TypeDef)cmuClock_TIMER1;
			timerIrq 			= TIMER1_IRQn;
			break;

		case 2:
			timer->timer_device = TIMER2;
			timerClock 			= (CMU_Clock_TypeDef)cmuClock_TIMER2;
			timerIrq 			= TIMER2_IRQn;
			break;

		default:
			break;
		}

		/* Enable TIMER clock */
		CMU_ClockEnable(timerClock, true);

		/* Reset */
		TIMER_Reset(timer->timer_device);

		/* Init specified TIMER unit */
		init.enable 		= false;
		init.debugRun		= true;
		init.prescale		= TMR_CFG_PRESCALER;
		init.clkSel 		= timerClkSelHFPerClk;
		init.fallAction 	= timerInputActionNone;
		init.riseAction 	= timerInputActionNone;
		init.mode			= timerModeUp;
		init.dmaClrAct		= false;
		init.quadModeX4 	= false;
		init.oneShot		= (mode > 0) ? true : false;
		init.sync			= false;
		TIMER_Init(timer->timer_device, &init);

		/* Config interrupt and NVIC */
		hook.type			= efm32_irq_type_timer;
		hook.unit			= unitNumber;
		hook.cbFunc 		= rt_hw_timer_isr;
		hook.userPtr		= device;
		efm32_irq_hook_register(&hook);

		/* Enable overflow interrupt */
		TIMER_IntEnable(timer->timer_device, TIMER_IF_OF);
		TIMER_IntClear(timer->timer_device, TIMER_IF_OF);

		/* Enable TIMERn interrupt vector in NVIC */
		NVIC_ClearPendingIRQ(timerIrq);
		NVIC_SetPriority(timerIrq, EFM32_IRQ_PRI_DEFAULT);
		NVIC_EnableIRQ(timerIrq);

		return timer;
	} while(0);

	if (timer)
	{
		rt_free(timer);
	}
	rt_kprintf("TIMER: Init failed!\n");
	return RT_NULL;
}

O
onelife.real 已提交
332 333 334 335 336 337 338 339 340 341 342 343
/******************************************************************//**
* @brief
*	Initialize all Timer module related hardware and register Timer device to kernel
*
* @details
*
* @note
*********************************************************************/
void rt_hw_timer_init(void)
{
	struct efm32_timer_device_t	*timer;

O
onelife.real 已提交
344 345 346
#ifdef RT_USING_TIMER0
	if ((timer = rt_hw_timer_unit_init(&timer0_device, 0, RT_USING_TIMER0)) \
		!= RT_NULL)
O
onelife.real 已提交
347
	{
O
onelife.real 已提交
348
	    rt_hw_timer_register(&timer0_device, RT_TIMER0_NAME, 0, timer);	
O
onelife.real 已提交
349 350 351 352
	}
#endif


O
onelife.real 已提交
353 354 355 356 357 358
#ifdef RT_USING_TIMER1
	if ((timer = rt_hw_timer_unit_init(&timer1_device, 1, RT_USING_TIMER1)) \
		!= RT_NULL)
	{
	    rt_hw_timer_register(&timer1_device, RT_TIMER1_NAME, 0, timer);	
	}
O
onelife.real 已提交
359 360
#endif

O
onelife.real 已提交
361
#ifdef RT_USING_TIMER2
O
onelife.real 已提交
362 363
	if ((timer = rt_hw_timer_unit_init(&timer2_device, 2, RT_USING_TIMER2)) \
		!= RT_NULL)
O
onelife.real 已提交
364
	{
O
onelife.real 已提交
365
    	rt_hw_timer_register(&timer2_device, RT_TIMER2_NAME, 0, timer);	
O
onelife.real 已提交
366 367 368 369
	}
#endif
}

O
onelife.real 已提交
370
#endif
O
onelife.real 已提交
371 372 373
/******************************************************************//**
 * @}
*********************************************************************/