|
1 |
| -/* |
2 |
| - * ESPRSSIF MIT License |
3 |
| - * |
4 |
| - * Copyright (c) 2015 <ESPRESSIF SYSTEMS (SHANGHAI) PTE LTD> |
5 |
| - * |
6 |
| - * Permission is hereby granted for use on ESPRESSIF SYSTEMS ESP8266 only, in which case, |
7 |
| - * it is free of charge, to any person obtaining a copy of this software and associated |
8 |
| - * documentation files (the "Software"), to deal in the Software without restriction, including |
9 |
| - * without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, |
10 |
| - * and/or sell copies of the Software, and to permit persons to whom the Software is furnished |
11 |
| - * to do so, subject to the following conditions: |
12 |
| - * |
13 |
| - * The above copyright notice and this permission notice shall be included in all copies or |
14 |
| - * substantial portions of the Software. |
15 |
| - * |
16 |
| - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
17 |
| - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
18 |
| - * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
19 |
| - * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
20 |
| - * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
21 |
| - * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
22 |
| - * |
23 |
| - */ |
24 |
| - |
25 |
| -#include "esp_common.h" |
26 |
| - |
27 |
| -#define US_TO_RTC_TIMER_TICKS(t) \ |
28 |
| - ((t) ? \ |
29 |
| - (((t) > 0x35A) ? \ |
30 |
| - (((t) >> 2) * ((APB_CLK_FREQ >> 4) / 250000) + ((t)&0x3) * ((APB_CLK_FREQ >> 4) / 1000000)) : \ |
31 |
| - (((t) *(APB_CLK_FREQ>>4)) / 1000000)) : \ |
32 |
| - 0) |
33 |
| - |
34 |
| -#define FRC1_ENABLE_TIMER BIT7 |
35 |
| -#define FRC1_AUTO_LOAD BIT6 |
36 |
| - |
37 |
| -typedef enum { // timer provided mode |
38 |
| - DIVDED_BY_1 = 0, // timer clock |
39 |
| - DIVDED_BY_16 = 4, // divided by 16 |
40 |
| - DIVDED_BY_256 = 8, // divided by 256 |
41 |
| -} TIMER_PREDIVED_MODE; |
42 |
| - |
43 |
| -typedef enum { // timer interrupt mode |
44 |
| - TM_LEVEL_INT = 1, // level interrupt |
45 |
| - TM_EDGE_INT = 0, // edge interrupt |
46 |
| -} TIMER_INT_MODE; |
47 |
| - |
48 |
| -#define RTC_REG_WRITE(addr, val) WRITE_PERI_REG(addr, val) |
49 |
| - |
50 |
| -static void (* user_hw_timer_cb)(void) = NULL; |
51 |
| - |
52 |
| -static void hw_timer_isr_cb(void *arg) |
53 |
| -{ |
54 |
| - if (user_hw_timer_cb != NULL) { |
55 |
| - (*(user_hw_timer_cb))(); |
56 |
| - } |
57 |
| -} |
58 |
| - |
59 |
| -void hw_timer_arm(uint32 val) |
60 |
| -{ |
61 |
| - RTC_REG_WRITE(FRC1_LOAD_ADDRESS, US_TO_RTC_TIMER_TICKS(val)); |
62 |
| -} |
63 |
| - |
64 |
| -void hw_timer_set_func(void (* user_hw_timer_cb_set)(void)) |
65 |
| -{ |
66 |
| - user_hw_timer_cb = user_hw_timer_cb_set; |
67 |
| -} |
68 |
| - |
69 |
| -void hw_timer_init(uint8 req) |
70 |
| -{ |
71 |
| - if (req == 1) { |
72 |
| - RTC_REG_WRITE(FRC1_CTRL_ADDRESS, |
73 |
| - FRC1_AUTO_LOAD | DIVDED_BY_16 | FRC1_ENABLE_TIMER | TM_EDGE_INT); |
74 |
| - } else { |
75 |
| - RTC_REG_WRITE(FRC1_CTRL_ADDRESS, |
76 |
| - DIVDED_BY_16 | FRC1_ENABLE_TIMER | TM_EDGE_INT); |
77 |
| - } |
78 |
| - |
79 |
| - _xt_isr_attach(ETS_FRC_TIMER1_INUM, hw_timer_isr_cb, NULL); |
80 |
| - |
81 |
| - TM1_EDGE_INT_ENABLE(); |
82 |
| - _xt_isr_unmask(1 << ETS_FRC_TIMER1_INUM); |
83 |
| -} |
84 |
| - |
85 |
| -//-------------------------------Test Code Below-------------------------------------- |
86 |
| -#if 0 |
87 |
| -#include "hw_timer.h" |
88 |
| - |
89 |
| -#define REG_WRITE(_r,_v) (*(volatile uint32 *)(_r)) = (_v) |
90 |
| -#define REG_READ(_r) (*(volatile uint32 *)(_r)) |
91 |
| -#define WDEV_NOW() REG_READ(0x3ff20c00) |
92 |
| - |
93 |
| -uint32 tick_now2 = 0; |
94 |
| -void hw_test_timer_cb(void) |
95 |
| -{ |
96 |
| - static uint16 j = 0; |
97 |
| - j++; |
98 |
| - |
99 |
| - if ((WDEV_NOW() - tick_now2) >= 1000000) { |
100 |
| - static uint32 idx = 1; |
101 |
| - tick_now2 = WDEV_NOW(); |
102 |
| - os_printf("b%u:%d\n", idx++, j); |
103 |
| - j = 0; |
104 |
| - } |
105 |
| - |
106 |
| - //hw_timer_arm(50); |
107 |
| -} |
108 |
| - |
109 |
| -void user_init(void) |
110 |
| -{ |
111 |
| - hw_timer_init(1); |
112 |
| - hw_timer_set_func(hw_test_timer_cb); |
113 |
| - hw_timer_arm(100); |
114 |
| -} |
115 |
| -#endif |
116 |
| -
|
| 1 | +/* |
| 2 | + * ESPRSSIF MIT License |
| 3 | + * |
| 4 | + * Copyright (c) 2015 <ESPRESSIF SYSTEMS (SHANGHAI) PTE LTD> |
| 5 | + * |
| 6 | + * Permission is hereby granted for use on ESPRESSIF SYSTEMS ESP8266 only, in which case, |
| 7 | + * it is free of charge, to any person obtaining a copy of this software and associated |
| 8 | + * documentation files (the "Software"), to deal in the Software without restriction, including |
| 9 | + * without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 10 | + * and/or sell copies of the Software, and to permit persons to whom the Software is furnished |
| 11 | + * to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all copies or |
| 14 | + * substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
| 18 | + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
| 19 | + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
| 20 | + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 21 | + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 22 | + * |
| 23 | + */ |
| 24 | + |
| 25 | +#include "esp_common.h" |
| 26 | + |
| 27 | +#define US_TO_RTC_TIMER_TICKS(t) \ |
| 28 | + ((t) ? \ |
| 29 | + (((t) > 0x35A) ? \ |
| 30 | + (((t) >> 2) * ((APB_CLK_FREQ >> 4) / 250000) + ((t)&0x3) * ((APB_CLK_FREQ >> 4) / 1000000)) : \ |
| 31 | + (((t) *(APB_CLK_FREQ>>4)) / 1000000)) : \ |
| 32 | + 0) |
| 33 | + |
| 34 | +#define FRC1_ENABLE_TIMER BIT7 |
| 35 | +#define FRC1_AUTO_LOAD BIT6 |
| 36 | + |
| 37 | +typedef enum { // timer provided mode |
| 38 | + DIVDED_BY_1 = 0, // timer clock |
| 39 | + DIVDED_BY_16 = 4, // divided by 16 |
| 40 | + DIVDED_BY_256 = 8, // divided by 256 |
| 41 | +} TIMER_PREDIVED_MODE; |
| 42 | + |
| 43 | +typedef enum { // timer interrupt mode |
| 44 | + TM_LEVEL_INT = 1, // level interrupt |
| 45 | + TM_EDGE_INT = 0, // edge interrupt |
| 46 | +} TIMER_INT_MODE; |
| 47 | + |
| 48 | +#define RTC_REG_WRITE(addr, val) WRITE_PERI_REG(addr, val) |
| 49 | + |
| 50 | +static void (* user_hw_timer_cb)(void) = NULL; |
| 51 | + |
| 52 | +static void hw_timer_isr_cb(void *arg) |
| 53 | +{ |
| 54 | + if (user_hw_timer_cb != NULL) { |
| 55 | + (*(user_hw_timer_cb))(); |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +void hw_timer_arm(uint32 val) |
| 60 | +{ |
| 61 | + RTC_REG_WRITE(FRC1_LOAD_ADDRESS, US_TO_RTC_TIMER_TICKS(val)); |
| 62 | +} |
| 63 | + |
| 64 | +void hw_timer_set_func(void (* user_hw_timer_cb_set)(void)) |
| 65 | +{ |
| 66 | + user_hw_timer_cb = user_hw_timer_cb_set; |
| 67 | +} |
| 68 | + |
| 69 | +void hw_timer_init(uint8 req) |
| 70 | +{ |
| 71 | + if (req == 1) { |
| 72 | + RTC_REG_WRITE(FRC1_CTRL_ADDRESS, |
| 73 | + FRC1_AUTO_LOAD | DIVDED_BY_16 | FRC1_ENABLE_TIMER | TM_EDGE_INT); |
| 74 | + } else { |
| 75 | + RTC_REG_WRITE(FRC1_CTRL_ADDRESS, |
| 76 | + DIVDED_BY_16 | FRC1_ENABLE_TIMER | TM_EDGE_INT); |
| 77 | + } |
| 78 | + |
| 79 | + _xt_isr_attach(ETS_FRC_TIMER1_INUM, hw_timer_isr_cb, NULL); |
| 80 | + |
| 81 | + TM1_EDGE_INT_ENABLE(); |
| 82 | + _xt_isr_unmask(1 << ETS_FRC_TIMER1_INUM); |
| 83 | +} |
| 84 | + |
| 85 | +//-------------------------------Test Code Below-------------------------------------- |
| 86 | +#if 0 |
| 87 | +#include "hw_timer.h" |
| 88 | + |
| 89 | +#define REG_WRITE(_r,_v) (*(volatile uint32 *)(_r)) = (_v) |
| 90 | +#define REG_READ(_r) (*(volatile uint32 *)(_r)) |
| 91 | +#define WDEV_NOW() REG_READ(0x3ff20c00) |
| 92 | + |
| 93 | +uint32 tick_now2 = 0; |
| 94 | +void hw_test_timer_cb(void) |
| 95 | +{ |
| 96 | + static uint16 j = 0; |
| 97 | + j++; |
| 98 | + |
| 99 | + if ((WDEV_NOW() - tick_now2) >= 1000000) { |
| 100 | + static uint32 idx = 1; |
| 101 | + tick_now2 = WDEV_NOW(); |
| 102 | + os_printf("b%u:%d\n", idx++, j); |
| 103 | + j = 0; |
| 104 | + } |
| 105 | + |
| 106 | + //hw_timer_arm(50); |
| 107 | +} |
| 108 | + |
| 109 | +void user_init(void) |
| 110 | +{ |
| 111 | + hw_timer_init(1); |
| 112 | + hw_timer_set_func(hw_test_timer_cb); |
| 113 | + hw_timer_arm(100); |
| 114 | +} |
| 115 | +#endif |
| 116 | + |
0 commit comments