Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
test_vector.cpp@0:be95bfb06686, 2022-01-17 (annotated)
- Committer:
- wuliqunyy
- Date:
- Mon Jan 17 13:20:09 2022 +0000
- Revision:
- 0:be95bfb06686
a working non_flat + adc_didt for ehp regulation version
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| wuliqunyy | 0:be95bfb06686 | 1 | #include "test_vector.h" |
| wuliqunyy | 0:be95bfb06686 | 2 | #include "main_init.h" |
| wuliqunyy | 0:be95bfb06686 | 3 | |
| wuliqunyy | 0:be95bfb06686 | 4 | test_vector::test_vector(PinName pin, captureMode mode, uint32_t bufferSize) : _interrupt(pin) |
| wuliqunyy | 0:be95bfb06686 | 5 | { |
| wuliqunyy | 0:be95bfb06686 | 6 | _pinName = pin; |
| wuliqunyy | 0:be95bfb06686 | 7 | _bufferSize = bufferSize; |
| wuliqunyy | 0:be95bfb06686 | 8 | _mode = mode; |
| wuliqunyy | 0:be95bfb06686 | 9 | } |
| wuliqunyy | 0:be95bfb06686 | 10 | |
| wuliqunyy | 0:be95bfb06686 | 11 | test_vector::~test_vector() |
| wuliqunyy | 0:be95bfb06686 | 12 | { |
| wuliqunyy | 0:be95bfb06686 | 13 | } |
| wuliqunyy | 0:be95bfb06686 | 14 | |
| wuliqunyy | 0:be95bfb06686 | 15 | |
| wuliqunyy | 0:be95bfb06686 | 16 | |
| wuliqunyy | 0:be95bfb06686 | 17 | void test_vector::_handlePinRiseIRQ() |
| wuliqunyy | 0:be95bfb06686 | 18 | { |
| wuliqunyy | 0:be95bfb06686 | 19 | switch(_mode){ |
| wuliqunyy | 0:be95bfb06686 | 20 | case rise_rise: |
| wuliqunyy | 0:be95bfb06686 | 21 | _savePeriod(); |
| wuliqunyy | 0:be95bfb06686 | 22 | _sampleCaptureTimer(); |
| wuliqunyy | 0:be95bfb06686 | 23 | break; |
| wuliqunyy | 0:be95bfb06686 | 24 | case rise_fall: |
| wuliqunyy | 0:be95bfb06686 | 25 | _sampleCaptureTimer(); |
| wuliqunyy | 0:be95bfb06686 | 26 | break; |
| wuliqunyy | 0:be95bfb06686 | 27 | case fall_rise: |
| wuliqunyy | 0:be95bfb06686 | 28 | _savePeriod(); |
| wuliqunyy | 0:be95bfb06686 | 29 | break; |
| wuliqunyy | 0:be95bfb06686 | 30 | case fall_fall: |
| wuliqunyy | 0:be95bfb06686 | 31 | break; |
| wuliqunyy | 0:be95bfb06686 | 32 | case both: |
| wuliqunyy | 0:be95bfb06686 | 33 | _savePeriod(); |
| wuliqunyy | 0:be95bfb06686 | 34 | _sampleCaptureTimer(); |
| wuliqunyy | 0:be95bfb06686 | 35 | break; |
| wuliqunyy | 0:be95bfb06686 | 36 | default: |
| wuliqunyy | 0:be95bfb06686 | 37 | _savePeriod(); |
| wuliqunyy | 0:be95bfb06686 | 38 | _sampleCaptureTimer(); |
| wuliqunyy | 0:be95bfb06686 | 39 | } |
| wuliqunyy | 0:be95bfb06686 | 40 | } |
| wuliqunyy | 0:be95bfb06686 | 41 | |
| wuliqunyy | 0:be95bfb06686 | 42 | void test_vector::_handlePinFallIRQ() |
| wuliqunyy | 0:be95bfb06686 | 43 | { |
| wuliqunyy | 0:be95bfb06686 | 44 | switch(_mode){ |
| wuliqunyy | 0:be95bfb06686 | 45 | case rise_rise: |
| wuliqunyy | 0:be95bfb06686 | 46 | break; |
| wuliqunyy | 0:be95bfb06686 | 47 | case rise_fall: |
| wuliqunyy | 0:be95bfb06686 | 48 | _savePeriod(); |
| wuliqunyy | 0:be95bfb06686 | 49 | break; |
| wuliqunyy | 0:be95bfb06686 | 50 | case fall_rise: |
| wuliqunyy | 0:be95bfb06686 | 51 | _sampleCaptureTimer(); |
| wuliqunyy | 0:be95bfb06686 | 52 | break; |
| wuliqunyy | 0:be95bfb06686 | 53 | case fall_fall: |
| wuliqunyy | 0:be95bfb06686 | 54 | _savePeriod(); |
| wuliqunyy | 0:be95bfb06686 | 55 | _sampleCaptureTimer(); |
| wuliqunyy | 0:be95bfb06686 | 56 | break; |
| wuliqunyy | 0:be95bfb06686 | 57 | case both: |
| wuliqunyy | 0:be95bfb06686 | 58 | _savePeriod(); |
| wuliqunyy | 0:be95bfb06686 | 59 | _sampleCaptureTimer(); |
| wuliqunyy | 0:be95bfb06686 | 60 | break; |
| wuliqunyy | 0:be95bfb06686 | 61 | default: |
| wuliqunyy | 0:be95bfb06686 | 62 | _savePeriod(); |
| wuliqunyy | 0:be95bfb06686 | 63 | _sampleCaptureTimer(); |
| wuliqunyy | 0:be95bfb06686 | 64 | } |
| wuliqunyy | 0:be95bfb06686 | 65 | } |
| wuliqunyy | 0:be95bfb06686 | 66 | |
| wuliqunyy | 0:be95bfb06686 | 67 | |
| wuliqunyy | 0:be95bfb06686 | 68 | |
| wuliqunyy | 0:be95bfb06686 | 69 | |
| wuliqunyy | 0:be95bfb06686 | 70 | void test_vector::configureTimer() |
| wuliqunyy | 0:be95bfb06686 | 71 | { |
| wuliqunyy | 0:be95bfb06686 | 72 | |
| wuliqunyy | 0:be95bfb06686 | 73 | _timer_value = 0; |
| wuliqunyy | 0:be95bfb06686 | 74 | _period = 0; |
| wuliqunyy | 0:be95bfb06686 | 75 | |
| wuliqunyy | 0:be95bfb06686 | 76 | LPC_SC->PCONP |= (1UL << 23) | (1UL << 15); /* enable timer 3 power and the gpio*/ |
| wuliqunyy | 0:be95bfb06686 | 77 | LPC_SC->PCLKSEL1 |= (1UL << 14); /* Timer3 clock is SystemCLK */ |
| wuliqunyy | 0:be95bfb06686 | 78 | |
| wuliqunyy | 0:be95bfb06686 | 79 | LPC_TIM3->PR = SystemCoreClock/1000000; /* should increment once a microsecond. */ |
| wuliqunyy | 0:be95bfb06686 | 80 | LPC_TIM3->MR0 = 0; |
| wuliqunyy | 0:be95bfb06686 | 81 | LPC_TIM3->MR1 = 0; |
| wuliqunyy | 0:be95bfb06686 | 82 | LPC_TIM3->MR2 = 0; |
| wuliqunyy | 0:be95bfb06686 | 83 | LPC_TIM3->MR3 = 0; |
| wuliqunyy | 0:be95bfb06686 | 84 | LPC_TIM3->MCR = 0; |
| wuliqunyy | 0:be95bfb06686 | 85 | LPC_TIM3->CTCR &= ~(3 << 0); /* Timer mode */ |
| wuliqunyy | 0:be95bfb06686 | 86 | LPC_TIM3->CCR|=((1<<0)|(1<<1)); /* capture rising & falling without interrupt on channel 0 */ |
| wuliqunyy | 0:be95bfb06686 | 87 | |
| wuliqunyy | 0:be95bfb06686 | 88 | switch(_pinName){ |
| wuliqunyy | 0:be95bfb06686 | 89 | /* configure the pin ==> 2'11 at offset 14 is to enable CAP3.0 or p15 */ |
| wuliqunyy | 0:be95bfb06686 | 90 | case p15: |
| wuliqunyy | 0:be95bfb06686 | 91 | LPC_PINCON->PINSEL1 |= (3UL << 14); |
| wuliqunyy | 0:be95bfb06686 | 92 | LPC_TIM3->IR |= (1 << 4UL); //enbale cpature channel 0 event |
| wuliqunyy | 0:be95bfb06686 | 93 | break; |
| wuliqunyy | 0:be95bfb06686 | 94 | /* configure the pin ==> 2'11 at offset 16 is to enable CAP3.1 or p16 */ |
| wuliqunyy | 0:be95bfb06686 | 95 | case p16: |
| wuliqunyy | 0:be95bfb06686 | 96 | LPC_PINCON->PINSEL0 |= (3UL << 16); |
| wuliqunyy | 0:be95bfb06686 | 97 | LPC_TIM3->IR |= (1 << 5UL); /* enbale cpature channel 1 event */ |
| wuliqunyy | 0:be95bfb06686 | 98 | break; |
| wuliqunyy | 0:be95bfb06686 | 99 | /* default is p15 */ |
| wuliqunyy | 0:be95bfb06686 | 100 | default: |
| wuliqunyy | 0:be95bfb06686 | 101 | LPC_PINCON->PINSEL1 |= (3UL << 14); |
| wuliqunyy | 0:be95bfb06686 | 102 | LPC_TIM3->IR |= (1 << 4UL); /* enbale cpature channel 0 event */ |
| wuliqunyy | 0:be95bfb06686 | 103 | } |
| wuliqunyy | 0:be95bfb06686 | 104 | |
| wuliqunyy | 0:be95bfb06686 | 105 | } |
| wuliqunyy | 0:be95bfb06686 | 106 | |
| wuliqunyy | 0:be95bfb06686 | 107 | void test_vector::startTimer() |
| wuliqunyy | 0:be95bfb06686 | 108 | { |
| wuliqunyy | 0:be95bfb06686 | 109 | |
| wuliqunyy | 0:be95bfb06686 | 110 | LPC_TIM3->TCR = 2; // reset |
| wuliqunyy | 0:be95bfb06686 | 111 | LPC_TIM3->TCR = 1; //start timer |
| wuliqunyy | 0:be95bfb06686 | 112 | _interrupt.rise(callback(this, &test_vector::_handlePinRiseIRQ)); /* rising edge IRQ routine*/ |
| wuliqunyy | 0:be95bfb06686 | 113 | _interrupt.fall(callback(this, &test_vector::_handlePinFallIRQ)); /* falling edge IRQ routine*/ |
| wuliqunyy | 0:be95bfb06686 | 114 | |
| wuliqunyy | 0:be95bfb06686 | 115 | } |
| wuliqunyy | 0:be95bfb06686 | 116 | |
| wuliqunyy | 0:be95bfb06686 | 117 | void test_vector::stopTimer() |
| wuliqunyy | 0:be95bfb06686 | 118 | { |
| wuliqunyy | 0:be95bfb06686 | 119 | |
| wuliqunyy | 0:be95bfb06686 | 120 | LPC_TIM3->TCR = 2; // reset |
| wuliqunyy | 0:be95bfb06686 | 121 | _interrupt.rise(NULL); /*disable pin interrupt*/ |
| wuliqunyy | 0:be95bfb06686 | 122 | _interrupt.fall(NULL); /*disable pin interrupt*/ |
| wuliqunyy | 0:be95bfb06686 | 123 | led1 = 0; |
| wuliqunyy | 0:be95bfb06686 | 124 | led2 = 0; |
| wuliqunyy | 0:be95bfb06686 | 125 | } |
| wuliqunyy | 0:be95bfb06686 | 126 | |
| wuliqunyy | 0:be95bfb06686 | 127 | void test_vector::_sampleCaptureTimer() |
| wuliqunyy | 0:be95bfb06686 | 128 | { |
| wuliqunyy | 0:be95bfb06686 | 129 | _timer_value = LPC_TIM3->CR0; |
| wuliqunyy | 0:be95bfb06686 | 130 | } |
| wuliqunyy | 0:be95bfb06686 | 131 | |
| wuliqunyy | 0:be95bfb06686 | 132 | void test_vector::_savePeriod() |
| wuliqunyy | 0:be95bfb06686 | 133 | { |
| wuliqunyy | 0:be95bfb06686 | 134 | _period = LPC_TIM3->CR0 - _timer_value; |
| wuliqunyy | 0:be95bfb06686 | 135 | if( _period_idx <= _bufferSize && !_bufferFull) |
| wuliqunyy | 0:be95bfb06686 | 136 | { |
| wuliqunyy | 0:be95bfb06686 | 137 | _ptr_period_arr[_period_idx] = _period; |
| wuliqunyy | 0:be95bfb06686 | 138 | // _ptr_period_arr[_period_idx] = _bufferSize - _period_idx; /* to test */ |
| wuliqunyy | 0:be95bfb06686 | 139 | _period_idx++; |
| wuliqunyy | 0:be95bfb06686 | 140 | _bufferFull = false; |
| wuliqunyy | 0:be95bfb06686 | 141 | led1 = 1; |
| wuliqunyy | 0:be95bfb06686 | 142 | } |
| wuliqunyy | 0:be95bfb06686 | 143 | else |
| wuliqunyy | 0:be95bfb06686 | 144 | { |
| wuliqunyy | 0:be95bfb06686 | 145 | _bufferFull = true; |
| wuliqunyy | 0:be95bfb06686 | 146 | led1 = 0; |
| wuliqunyy | 0:be95bfb06686 | 147 | } |
| wuliqunyy | 0:be95bfb06686 | 148 | } |
| wuliqunyy | 0:be95bfb06686 | 149 | |
| wuliqunyy | 0:be95bfb06686 | 150 | |
| wuliqunyy | 0:be95bfb06686 | 151 | void test_vector::setUpTimerBuffer() |
| wuliqunyy | 0:be95bfb06686 | 152 | { |
| wuliqunyy | 0:be95bfb06686 | 153 | // _ptr_period_arr = (uint32_t*) malloc(_bufferSize * sizeof(uint32_t)); /* always pre-allocate 1000 words buffer*/ |
| wuliqunyy | 0:be95bfb06686 | 154 | _ptr_period_arr = new uint32_t[_bufferSize]; |
| wuliqunyy | 0:be95bfb06686 | 155 | _period_idx = 0; |
| wuliqunyy | 0:be95bfb06686 | 156 | _bufferFull = false; |
| wuliqunyy | 0:be95bfb06686 | 157 | } |
| wuliqunyy | 0:be95bfb06686 | 158 | |
| wuliqunyy | 0:be95bfb06686 | 159 | |
| wuliqunyy | 0:be95bfb06686 | 160 | void test_vector::releaseTimerBuffer() |
| wuliqunyy | 0:be95bfb06686 | 161 | { |
| wuliqunyy | 0:be95bfb06686 | 162 | // free(_ptr_period_arr); /*free up the memory*/ |
| wuliqunyy | 0:be95bfb06686 | 163 | if( _ptr_period_arr != NULL) |
| wuliqunyy | 0:be95bfb06686 | 164 | { |
| wuliqunyy | 0:be95bfb06686 | 165 | delete []_ptr_period_arr; |
| wuliqunyy | 0:be95bfb06686 | 166 | _ptr_period_arr = NULL; |
| wuliqunyy | 0:be95bfb06686 | 167 | } |
| wuliqunyy | 0:be95bfb06686 | 168 | } |
| wuliqunyy | 0:be95bfb06686 | 169 | |
| wuliqunyy | 0:be95bfb06686 | 170 | |
| wuliqunyy | 0:be95bfb06686 | 171 | bool test_vector:: getTimerBufferFull() |
| wuliqunyy | 0:be95bfb06686 | 172 | { |
| wuliqunyy | 0:be95bfb06686 | 173 | return _bufferFull; |
| wuliqunyy | 0:be95bfb06686 | 174 | } |
| wuliqunyy | 0:be95bfb06686 | 175 | |
| wuliqunyy | 0:be95bfb06686 | 176 | uint32_t test_vector::getTimerBufferSize() |
| wuliqunyy | 0:be95bfb06686 | 177 | { |
| wuliqunyy | 0:be95bfb06686 | 178 | return _bufferSize; |
| wuliqunyy | 0:be95bfb06686 | 179 | } |
| wuliqunyy | 0:be95bfb06686 | 180 | |
| wuliqunyy | 0:be95bfb06686 | 181 | uint32_t* test_vector::getTimerBufferPointer() |
| wuliqunyy | 0:be95bfb06686 | 182 | { |
| wuliqunyy | 0:be95bfb06686 | 183 | return _ptr_period_arr; |
| wuliqunyy | 0:be95bfb06686 | 184 | } |
| wuliqunyy | 0:be95bfb06686 | 185 | |
| wuliqunyy | 0:be95bfb06686 | 186 | uint32_t test_vector::getPeriodIdx() |
| wuliqunyy | 0:be95bfb06686 | 187 | { |
| wuliqunyy | 0:be95bfb06686 | 188 | return _period_idx; |
| wuliqunyy | 0:be95bfb06686 | 189 | } |
| wuliqunyy | 0:be95bfb06686 | 190 | |
| wuliqunyy | 0:be95bfb06686 | 191 | uint32_t test_vector::getCurrentTimerBufferValue() |
| wuliqunyy | 0:be95bfb06686 | 192 | { |
| wuliqunyy | 0:be95bfb06686 | 193 | return *(_ptr_period_arr+_period_idx); |
| wuliqunyy | 0:be95bfb06686 | 194 | } |
