rjw | 6c1fd8f | 2022-11-30 14:33:01 +0800 | [diff] [blame] | 1 | #include "drv_features_gpt.h" |
| 2 | #include "drv_comm.h" |
| 3 | |
| 4 | #include "drv_litegpt.h" |
| 5 | //#include "intrCtrl.h" |
| 6 | |
| 7 | #include "dcl.h" |
| 8 | #include "kal_general_types.h" |
| 9 | #include "kal_public_api.h" |
| 10 | #include "sync_data.h" |
| 11 | #include "us_timer.h" |
| 12 | |
| 13 | /***************************************************************************** |
| 14 | * Basic Driver Implementation * |
| 15 | *****************************************************************************/ |
| 16 | |
| 17 | |
| 18 | void drv_litegpt_reset(void) |
| 19 | { |
| 20 | kal_uint32 i; |
| 21 | |
| 22 | //PDN_CLR(PDN_GPT_CTL); |
| 23 | //Way: Need PDN setting?? |
| 24 | |
| 25 | for(i = 0; i < 2; i++){ |
| 26 | DRV_WriteReg32(LITE_GPT_BASE+(i<<3), 0); //CTRL |
| 27 | DRV_WriteReg32(LITE_GPT_BASE+0x04+(i<<3), 0xFFFFUL);//Len |
| 28 | DRV_WriteReg32(LITE_GPT_BASE+0x14+(i<<2), 0x4);//Prescalar |
| 29 | } |
| 30 | DRV_Reg32(LITE_GPT_BASE+0x10);//STAT1/STATE2 clear |
| 31 | |
| 32 | } |
| 33 | |
| 34 | /* time = count value */ |
| 35 | void drv_litegpt_set_timer(kal_uint32 num, kal_uint32 time, kal_uint32 mode, kal_uint32 prescaler) |
| 36 | { |
| 37 | |
| 38 | ASSERT((3 <= num) && (num <= 4) && (time <= (0xFFFFUL + 1))); |
| 39 | DRV_WriteReg32_NPW(LITE_GPT_BASE + 0x04 + ((num-3)<<3 ), (time - 1));//[Way] 1T prescaler error range still here? |
| 40 | } |
| 41 | |
| 42 | void drv_litegpt_start_timer(kal_uint32 num) |
| 43 | { |
| 44 | ASSERT((3 <= num) && (num <= 4)); |
| 45 | DRV_WriteReg32_NPW(LITE_GPT_BASE + ((num-3)<<3 ), 0x1);//[Way] bit0 and bit1 can write together? |
| 46 | |
| 47 | Data_Sync_Barrier(); |
| 48 | } |
| 49 | |
| 50 | void drv_litegpt_stop_timer(kal_uint32 num) |
| 51 | { |
| 52 | ASSERT((3 <= num) && (num <= 4)); |
| 53 | DRV_WriteReg32_NPW(LITE_GPT_BASE + ((num-3)<<3 ), 0x2);//[Way] mask bit1 |
| 54 | |
| 55 | Data_Sync_Barrier(); |
| 56 | } |
| 57 | |
| 58 | kal_uint32 drv_litegpt_time_remaining(kal_uint32 num) |
| 59 | { |
| 60 | kal_uint32 remain_tick; |
| 61 | kal_uint32 cmp_val1 = 0; |
| 62 | kal_uint32 cmp_val2 = 0; |
| 63 | kal_uint32 loop = 0; |
| 64 | |
| 65 | ASSERT((3 <= num) && (num <= 4)); |
| 66 | //[Way] designer will provide algorithm for remain tick |
| 67 | cmp_val1 = DRV_Reg32(LITE_GPT_BASE + 0x38 + ((num-3)<<2)) & 0xFFFF0000; |
| 68 | while(1) |
| 69 | { |
| 70 | cmp_val2 = DRV_Reg32(LITE_GPT_BASE + 0x38 + ((num-3)<<2)) & 0xFFFF0000; |
| 71 | if(cmp_val1 == cmp_val2) |
| 72 | { |
| 73 | break; |
| 74 | } |
| 75 | else |
| 76 | { |
| 77 | loop++; |
| 78 | if(loop >= 4)//less than 4 times |
| 79 | { |
| 80 | ASSERT(0); |
| 81 | } |
| 82 | cmp_val1 = cmp_val2; |
| 83 | } |
| 84 | } |
| 85 | remain_tick = (cmp_val2 >> 16); |
| 86 | |
| 87 | if(0xFFFF == remain_tick)//alreay start but in 0xFFFF stage |
| 88 | { |
| 89 | return DRV_Reg32((LITE_GPT_BASE + 0x4 + ((num - 3)<<3))); |
| 90 | } |
| 91 | |
| 92 | return remain_tick; |
| 93 | } |
| 94 | |
| 95 | void drv_litegpt_set_wakeup_gpt(kal_uint32 time_tick)//the duration is about 1/32K*time_tick us, min/max = 4/0xffff |
| 96 | { |
| 97 | ASSERT((4 <= time_tick) && (time_tick <= 0xFFFEUL)); |
| 98 | drv_litegpt_set_timer(TOPSM_TEST_LITEGPT_WAKEUP, time_tick - 1,LITEGPT_CTRL_MODE_ONESHOT , LITEGPT_MIN_PRESCALE); |
| 99 | drv_litegpt_start_timer(TOPSM_TEST_LITEGPT_WAKEUP); |
| 100 | } |
| 101 | |
| 102 | void drv_litegpt_clr_wakeup_event(void) |
| 103 | { |
| 104 | kal_uint16 INT_Status; |
| 105 | //kal_uint16 Wakeup_Status; |
| 106 | INT_Status = DRV_Reg32(LITE_GPT_BASE + 0x10);//RU type register |
| 107 | //DRV_WriteReg32_NPW(LITE_GPT_BASE + 0x10, INT_Status);//W1C interrupt status |
| 108 | |
| 109 | //Wakeup_Status = DRV_Reg32(LITE_GPT_BASE + 0x34); |
| 110 | DRV_WriteReg32_NPW(LITE_GPT_BASE + 0x34, INT_Status); //clear wakeup event/interrupt |
| 111 | while(0 != DRV_Reg32(LITE_GPT_BASE + 0x10)); |
| 112 | } |
| 113 | |
| 114 | |
| 115 | |