Tianji Wu
/
recv315test5
Receiver program for assisted power disaggregation project at NESL.
main.cpp@0:3c23ac48cdf3, 2012-06-14 (annotated)
- Committer:
- the729
- Date:
- Thu Jun 14 23:14:43 2012 +0000
- Revision:
- 0:3c23ac48cdf3
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
the729 | 0:3c23ac48cdf3 | 1 | #include "mbed.h" |
the729 | 0:3c23ac48cdf3 | 2 | |
the729 | 0:3c23ac48cdf3 | 3 | InterruptIn rfint(p15); |
the729 | 0:3c23ac48cdf3 | 4 | DigitalIn rfdata(p15); |
the729 | 0:3c23ac48cdf3 | 5 | DigitalOut led1(LED1); |
the729 | 0:3c23ac48cdf3 | 6 | DigitalOut led2(LED2); |
the729 | 0:3c23ac48cdf3 | 7 | DigitalOut led3(LED3); |
the729 | 0:3c23ac48cdf3 | 8 | DigitalOut led4(LED4); |
the729 | 0:3c23ac48cdf3 | 9 | DigitalOut pi16(p16); |
the729 | 0:3c23ac48cdf3 | 10 | DigitalOut pi17(p17); |
the729 | 0:3c23ac48cdf3 | 11 | DigitalOut pi18(p18); |
the729 | 0:3c23ac48cdf3 | 12 | DigitalOut pi19(p19); |
the729 | 0:3c23ac48cdf3 | 13 | DigitalOut pi20(p20); |
the729 | 0:3c23ac48cdf3 | 14 | Serial pc(USBTX,USBRX); |
the729 | 0:3c23ac48cdf3 | 15 | |
the729 | 0:3c23ac48cdf3 | 16 | #define TMR LPC_TIM2 |
the729 | 0:3c23ac48cdf3 | 17 | #define CLK_TIMER 12 |
the729 | 0:3c23ac48cdf3 | 18 | #define PCTIM2 22 // Power Control Timer2 |
the729 | 0:3c23ac48cdf3 | 19 | #define PCLK_DEVIDER 3 |
the729 | 0:3c23ac48cdf3 | 20 | |
the729 | 0:3c23ac48cdf3 | 21 | uint8_t flag = 0; |
the729 | 0:3c23ac48cdf3 | 22 | int32_t interval; |
the729 | 0:3c23ac48cdf3 | 23 | uint16_t datarecv; |
the729 | 0:3c23ac48cdf3 | 24 | uint32_t o_spl, n_spl; |
the729 | 0:3c23ac48cdf3 | 25 | static enum { |
the729 | 0:3c23ac48cdf3 | 26 | SYNC0, |
the729 | 0:3c23ac48cdf3 | 27 | SYNC1 |
the729 | 0:3c23ac48cdf3 | 28 | } state = SYNC0; |
the729 | 0:3c23ac48cdf3 | 29 | |
the729 | 0:3c23ac48cdf3 | 30 | int32_t lpf1(int32_t x) { |
the729 | 0:3c23ac48cdf3 | 31 | static int32_t y = 0; |
the729 | 0:3c23ac48cdf3 | 32 | y = (x>>1) + (x>>2) + (y>>2); |
the729 | 0:3c23ac48cdf3 | 33 | return y; |
the729 | 0:3c23ac48cdf3 | 34 | } |
the729 | 0:3c23ac48cdf3 | 35 | |
the729 | 0:3c23ac48cdf3 | 36 | int32_t lpf2(int32_t x) { |
the729 | 0:3c23ac48cdf3 | 37 | static int32_t y = 0; |
the729 | 0:3c23ac48cdf3 | 38 | y = (x>>1) + (x>>2) + (y>>2); |
the729 | 0:3c23ac48cdf3 | 39 | return y; |
the729 | 0:3c23ac48cdf3 | 40 | } |
the729 | 0:3c23ac48cdf3 | 41 | |
the729 | 0:3c23ac48cdf3 | 42 | void rfedge() { |
the729 | 0:3c23ac48cdf3 | 43 | uint32_t tval = TMR->TC; |
the729 | 0:3c23ac48cdf3 | 44 | static uint32_t edges[8]; |
the729 | 0:3c23ac48cdf3 | 45 | static uint32_t ospls[8]; |
the729 | 0:3c23ac48cdf3 | 46 | static uint32_t nspls[8]; |
the729 | 0:3c23ac48cdf3 | 47 | static uint8_t head = 0; |
the729 | 0:3c23ac48cdf3 | 48 | static uint8_t tail = 0; |
the729 | 0:3c23ac48cdf3 | 49 | |
the729 | 0:3c23ac48cdf3 | 50 | if (state == SYNC0) { |
the729 | 0:3c23ac48cdf3 | 51 | edges[tail] = tval; |
the729 | 0:3c23ac48cdf3 | 52 | ospls[tail] = o_spl; |
the729 | 0:3c23ac48cdf3 | 53 | nspls[tail] = n_spl; |
the729 | 0:3c23ac48cdf3 | 54 | tail = (tail + 1) & 0x7; |
the729 | 0:3c23ac48cdf3 | 55 | if (tail == head || tval - edges[head] > 31800) { |
the729 | 0:3c23ac48cdf3 | 56 | do { |
the729 | 0:3c23ac48cdf3 | 57 | head = (head+1)&0x7; |
the729 | 0:3c23ac48cdf3 | 58 | } while (tval - edges[head] > 31800); |
the729 | 0:3c23ac48cdf3 | 59 | } |
the729 | 0:3c23ac48cdf3 | 60 | for (uint8_t i = head; i!= tail; i = (i+1)&0x7) { |
the729 | 0:3c23ac48cdf3 | 61 | if (tval - edges[i] > 29000) { |
the729 | 0:3c23ac48cdf3 | 62 | if ((o_spl - ospls[i]<<4) < (n_spl - nspls[i])) { |
the729 | 0:3c23ac48cdf3 | 63 | o_spl = n_spl = 0; |
the729 | 0:3c23ac48cdf3 | 64 | head = tail = 0; |
the729 | 0:3c23ac48cdf3 | 65 | TMR->TC = 0; |
the729 | 0:3c23ac48cdf3 | 66 | TMR->MR0 = (tval - edges[i]>>3); |
the729 | 0:3c23ac48cdf3 | 67 | TMR->MCR = 3; |
the729 | 0:3c23ac48cdf3 | 68 | interval = TMR->MR0; |
the729 | 0:3c23ac48cdf3 | 69 | state = SYNC1; |
the729 | 0:3c23ac48cdf3 | 70 | led4 = 1; |
the729 | 0:3c23ac48cdf3 | 71 | pi18 = 1; |
the729 | 0:3c23ac48cdf3 | 72 | pi19 = 0; |
the729 | 0:3c23ac48cdf3 | 73 | break; |
the729 | 0:3c23ac48cdf3 | 74 | } |
the729 | 0:3c23ac48cdf3 | 75 | } else |
the729 | 0:3c23ac48cdf3 | 76 | break; |
the729 | 0:3c23ac48cdf3 | 77 | } |
the729 | 0:3c23ac48cdf3 | 78 | } |
the729 | 0:3c23ac48cdf3 | 79 | } |
the729 | 0:3c23ac48cdf3 | 80 | |
the729 | 0:3c23ac48cdf3 | 81 | void tmr_irq() { |
the729 | 0:3c23ac48cdf3 | 82 | static uint32_t bitsrecv = 0; |
the729 | 0:3c23ac48cdf3 | 83 | static uint16_t _datarecv = 0; |
the729 | 0:3c23ac48cdf3 | 84 | static uint8_t bitp = 0; |
the729 | 0:3c23ac48cdf3 | 85 | static uint8_t bitcnt = 0; |
the729 | 0:3c23ac48cdf3 | 86 | static uint8_t parity = 0; |
the729 | 0:3c23ac48cdf3 | 87 | static uint32_t o_spl_1; |
the729 | 0:3c23ac48cdf3 | 88 | static uint32_t o_spl_2; |
the729 | 0:3c23ac48cdf3 | 89 | |
the729 | 0:3c23ac48cdf3 | 90 | if (TMR->IR & 0x1) { |
the729 | 0:3c23ac48cdf3 | 91 | pi16 = !pi16; |
the729 | 0:3c23ac48cdf3 | 92 | |
the729 | 0:3c23ac48cdf3 | 93 | uint8_t bit = (o_spl > (n_spl>>2)+(n_spl>>3)); |
the729 | 0:3c23ac48cdf3 | 94 | pi17 = bit; |
the729 | 0:3c23ac48cdf3 | 95 | switch (state) { |
the729 | 0:3c23ac48cdf3 | 96 | case SYNC0: { |
the729 | 0:3c23ac48cdf3 | 97 | } break; |
the729 | 0:3c23ac48cdf3 | 98 | case SYNC1: { |
the729 | 0:3c23ac48cdf3 | 99 | if (!bitp) { |
the729 | 0:3c23ac48cdf3 | 100 | o_spl_1 = o_spl; |
the729 | 0:3c23ac48cdf3 | 101 | } else { |
the729 | 0:3c23ac48cdf3 | 102 | o_spl_2 = o_spl; |
the729 | 0:3c23ac48cdf3 | 103 | //if (o_spl_first + o_spl < (n_spl>>1) || o_spl_first + o_spl > n_spl + (n_spl>>1)) { |
the729 | 0:3c23ac48cdf3 | 104 | // state = SYNC0; |
the729 | 0:3c23ac48cdf3 | 105 | // pi19 = 1; |
the729 | 0:3c23ac48cdf3 | 106 | //} else |
the729 | 0:3c23ac48cdf3 | 107 | if (o_spl_1 > (o_spl_2<<1)) { |
the729 | 0:3c23ac48cdf3 | 108 | bit = 1; |
the729 | 0:3c23ac48cdf3 | 109 | } else if (o_spl_2 > (o_spl_1<<1)) { |
the729 | 0:3c23ac48cdf3 | 110 | bit = 0; |
the729 | 0:3c23ac48cdf3 | 111 | } else { |
the729 | 0:3c23ac48cdf3 | 112 | TMR->MCR = 0; |
the729 | 0:3c23ac48cdf3 | 113 | bitcnt = 0; |
the729 | 0:3c23ac48cdf3 | 114 | bitp = 0; |
the729 | 0:3c23ac48cdf3 | 115 | _datarecv = 0; |
the729 | 0:3c23ac48cdf3 | 116 | parity = 0; |
the729 | 0:3c23ac48cdf3 | 117 | state = SYNC0; |
the729 | 0:3c23ac48cdf3 | 118 | led4 = 0; |
the729 | 0:3c23ac48cdf3 | 119 | pi18 = 0; |
the729 | 0:3c23ac48cdf3 | 120 | pi19 = 1; |
the729 | 0:3c23ac48cdf3 | 121 | break; |
the729 | 0:3c23ac48cdf3 | 122 | } |
the729 | 0:3c23ac48cdf3 | 123 | o_spl_1 = o_spl_2 = 0; |
the729 | 0:3c23ac48cdf3 | 124 | if (!bitcnt) { |
the729 | 0:3c23ac48cdf3 | 125 | // nothing |
the729 | 0:3c23ac48cdf3 | 126 | } else if (bitcnt < 17) { |
the729 | 0:3c23ac48cdf3 | 127 | parity ^= bit; |
the729 | 0:3c23ac48cdf3 | 128 | _datarecv |= (bit<<(bitcnt-1)); |
the729 | 0:3c23ac48cdf3 | 129 | } else { |
the729 | 0:3c23ac48cdf3 | 130 | parity ^= bit; |
the729 | 0:3c23ac48cdf3 | 131 | led2 = !led2; |
the729 | 0:3c23ac48cdf3 | 132 | if (!(parity & 0x1)) { |
the729 | 0:3c23ac48cdf3 | 133 | led3 = !led3; |
the729 | 0:3c23ac48cdf3 | 134 | if (!flag) { |
the729 | 0:3c23ac48cdf3 | 135 | flag = 1; |
the729 | 0:3c23ac48cdf3 | 136 | datarecv = _datarecv; |
the729 | 0:3c23ac48cdf3 | 137 | } else { |
the729 | 0:3c23ac48cdf3 | 138 | led1 = 1; |
the729 | 0:3c23ac48cdf3 | 139 | } |
the729 | 0:3c23ac48cdf3 | 140 | } else { |
the729 | 0:3c23ac48cdf3 | 141 | pi19 = 1; |
the729 | 0:3c23ac48cdf3 | 142 | } |
the729 | 0:3c23ac48cdf3 | 143 | TMR->MCR = 0; |
the729 | 0:3c23ac48cdf3 | 144 | bitp = 0; |
the729 | 0:3c23ac48cdf3 | 145 | bitcnt = 0; |
the729 | 0:3c23ac48cdf3 | 146 | _datarecv = 0; |
the729 | 0:3c23ac48cdf3 | 147 | parity = 0; |
the729 | 0:3c23ac48cdf3 | 148 | state = SYNC0; |
the729 | 0:3c23ac48cdf3 | 149 | led4 = 0; |
the729 | 0:3c23ac48cdf3 | 150 | pi18 = 0; |
the729 | 0:3c23ac48cdf3 | 151 | break; |
the729 | 0:3c23ac48cdf3 | 152 | } |
the729 | 0:3c23ac48cdf3 | 153 | bitcnt ++; |
the729 | 0:3c23ac48cdf3 | 154 | } |
the729 | 0:3c23ac48cdf3 | 155 | bitp = bitp^1; |
the729 | 0:3c23ac48cdf3 | 156 | } |
the729 | 0:3c23ac48cdf3 | 157 | } |
the729 | 0:3c23ac48cdf3 | 158 | o_spl = n_spl = 0; |
the729 | 0:3c23ac48cdf3 | 159 | } |
the729 | 0:3c23ac48cdf3 | 160 | TMR->IR = -1; |
the729 | 0:3c23ac48cdf3 | 161 | } |
the729 | 0:3c23ac48cdf3 | 162 | |
the729 | 0:3c23ac48cdf3 | 163 | void init() { |
the729 | 0:3c23ac48cdf3 | 164 | // select PCLK |
the729 | 0:3c23ac48cdf3 | 165 | LPC_SC->PCLKSEL1 = (LPC_SC->PCLKSEL1 & ~(3<<CLK_TIMER)) | (PCLK_DEVIDER<<CLK_TIMER); |
the729 | 0:3c23ac48cdf3 | 166 | // power on timer |
the729 | 0:3c23ac48cdf3 | 167 | LPC_SC->PCONP |= (1<<PCTIM2); |
the729 | 0:3c23ac48cdf3 | 168 | |
the729 | 0:3c23ac48cdf3 | 169 | // reset timer |
the729 | 0:3c23ac48cdf3 | 170 | TMR->CTCR = 0; |
the729 | 0:3c23ac48cdf3 | 171 | TMR->PR = 3; |
the729 | 0:3c23ac48cdf3 | 172 | TMR->TCR = 2; |
the729 | 0:3c23ac48cdf3 | 173 | TMR->MCR = 0; |
the729 | 0:3c23ac48cdf3 | 174 | |
the729 | 0:3c23ac48cdf3 | 175 | __enable_irq(); |
the729 | 0:3c23ac48cdf3 | 176 | NVIC_SetVector(TIMER2_IRQn, (uint32_t)&tmr_irq); |
the729 | 0:3c23ac48cdf3 | 177 | NVIC_EnableIRQ(TIMER2_IRQn); |
the729 | 0:3c23ac48cdf3 | 178 | |
the729 | 0:3c23ac48cdf3 | 179 | rfint.rise(&rfedge); |
the729 | 0:3c23ac48cdf3 | 180 | //rfint.fall(&rfedge); |
the729 | 0:3c23ac48cdf3 | 181 | rfint.mode(PullNone); |
the729 | 0:3c23ac48cdf3 | 182 | pc.baud(115200); |
the729 | 0:3c23ac48cdf3 | 183 | |
the729 | 0:3c23ac48cdf3 | 184 | TMR->MR0 = -1; |
the729 | 0:3c23ac48cdf3 | 185 | //TMR->MCR = 3; // reset and interrupt on match |
the729 | 0:3c23ac48cdf3 | 186 | TMR->TCR = 1; |
the729 | 0:3c23ac48cdf3 | 187 | } |
the729 | 0:3c23ac48cdf3 | 188 | |
the729 | 0:3c23ac48cdf3 | 189 | void sample_data() { |
the729 | 0:3c23ac48cdf3 | 190 | o_spl += (rfdata & 1); |
the729 | 0:3c23ac48cdf3 | 191 | n_spl += 1; |
the729 | 0:3c23ac48cdf3 | 192 | } |
the729 | 0:3c23ac48cdf3 | 193 | |
the729 | 0:3c23ac48cdf3 | 194 | int main() { |
the729 | 0:3c23ac48cdf3 | 195 | uint16_t olddata = 0; |
the729 | 0:3c23ac48cdf3 | 196 | uint32_t pktrecv = 0; |
the729 | 0:3c23ac48cdf3 | 197 | uint32_t pktlost = 0; |
the729 | 0:3c23ac48cdf3 | 198 | |
the729 | 0:3c23ac48cdf3 | 199 | init(); |
the729 | 0:3c23ac48cdf3 | 200 | Ticker sampler; |
the729 | 0:3c23ac48cdf3 | 201 | sampler.attach_us(&sample_data, 20); |
the729 | 0:3c23ac48cdf3 | 202 | |
the729 | 0:3c23ac48cdf3 | 203 | while(1) { |
the729 | 0:3c23ac48cdf3 | 204 | if (flag) { |
the729 | 0:3c23ac48cdf3 | 205 | pktrecv ++; |
the729 | 0:3c23ac48cdf3 | 206 | if (olddata) { |
the729 | 0:3c23ac48cdf3 | 207 | pktlost += datarecv-olddata-1; |
the729 | 0:3c23ac48cdf3 | 208 | } |
the729 | 0:3c23ac48cdf3 | 209 | /*printf("RECV %d (id=%d, gp=%d, seq=%d, idx=%d, atv=%d) (%d) %d/%d\r\n", |
the729 | 0:3c23ac48cdf3 | 210 | datarecv, |
the729 | 0:3c23ac48cdf3 | 211 | (datarecv&0xFC00)>>10, |
the729 | 0:3c23ac48cdf3 | 212 | (datarecv&0x0300)>>8, |
the729 | 0:3c23ac48cdf3 | 213 | (datarecv&0xF0)>>4, |
the729 | 0:3c23ac48cdf3 | 214 | (datarecv&0xE)>>1, |
the729 | 0:3c23ac48cdf3 | 215 | (datarecv&0x1), |
the729 | 0:3c23ac48cdf3 | 216 | interval, pktlost, pktlost+pktrecv);*/ |
the729 | 0:3c23ac48cdf3 | 217 | pc.putc(datarecv >> 10); |
the729 | 0:3c23ac48cdf3 | 218 | pc.putc(((datarecv & 0x3F0)>>4) | 0x40); |
the729 | 0:3c23ac48cdf3 | 219 | pc.putc((datarecv & 0xF) | 0x80); |
the729 | 0:3c23ac48cdf3 | 220 | olddata = datarecv; |
the729 | 0:3c23ac48cdf3 | 221 | flag = 0; |
the729 | 0:3c23ac48cdf3 | 222 | } |
the729 | 0:3c23ac48cdf3 | 223 | } |
the729 | 0:3c23ac48cdf3 | 224 | } |