1 /*
2  * Copyright (C) 2015-2017 Alibaba Group Holding Limited
3  */
4 
5 #include <stdio.h>
6 #include <stdbool.h>
7 #include <string.h>
8 #include <k_api.h>
9 #include "aos/hal/timer.h"
10 #include "board.h"
11 #include "timer_api.h"
12 
13 gtimer_t local_timer;
14 
hal_timer_init(timer_dev_t * tim)15 int32_t hal_timer_init(timer_dev_t *tim)
16 {
17 	int32_t ret = -1;
18 
19 	if(tim == NULL)
20 		return -1;
21 
22 	memset(&local_timer, 0, sizeof(local_timer));
23 
24 	switch(tim->port) {
25 	case MICO_TIMER_2:
26 	case MICO_TIMER_3:
27 		gtimer_init(&local_timer, tim->port);
28 		ret = 0;
29 		break;
30 	default:
31 		printf("ERROR: Please USE Tmer2 and Timer3\n\r");
32 		break;
33 	}
34 	return 0;
35 }
36 
hal_timer_start(timer_dev_t * tim)37 int32_t hal_timer_start(timer_dev_t *tim)
38 {
39 	if(tim == NULL)
40 		return -1;
41 
42 	if(tim->config.reload_mode == TIMER_RELOAD_MANU){
43 		gtimer_start_one_shout(&local_timer, tim->config.period, tim->config.cb, (uint32_t)tim->config.arg);
44 	}else if(tim->config.reload_mode == TIMER_RELOAD_AUTO){
45     	gtimer_start_periodical(&local_timer, tim->config.period, tim->config.cb, (uint32_t)tim->config.arg);
46 	}else{
47 		printf("ERROR: Reload mode Set ERROR\n\r");
48 		return -1;
49 	}
50 
51 	return 0;
52 }
53 
hal_timer_stop(timer_dev_t * tim)54 void hal_timer_stop(timer_dev_t *tim)
55 {
56 	if (tim != NULL){
57 		gtimer_stop(&local_timer);
58 	}
59 }
60 
hal_timer_finalize(timer_dev_t * tim)61 int32_t hal_timer_finalize(timer_dev_t *tim)
62 {
63 	if(tim == NULL)
64 		return -1;
65 
66 	gtimer_deinit(&local_timer);
67     	return 0;
68 }
69