123
Fork of LG by
DeviceCCS.c@174:0f86eedd511c, 2016-06-26 (annotated)
- Committer:
- Diletant
- Date:
- Sun Jun 26 10:16:39 2016 +0000
- Revision:
- 174:0f86eedd511c
Device&... update. Not final!!!
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Diletant | 174:0f86eedd511c | 1 | #include "Device.h" |
Diletant | 174:0f86eedd511c | 2 | extern Device device; |
Diletant | 174:0f86eedd511c | 3 | |
Diletant | 174:0f86eedd511c | 4 | void InitCCSDefaultSettings(void){ |
Diletant | 174:0f86eedd511c | 5 | //TODO: real values |
Diletant | 174:0f86eedd511c | 6 | device.ccs.current[0].settings.transfer.points = 16; |
Diletant | 174:0f86eedd511c | 7 | device.ccs.current[0].settings.transfer.raw[0] = 0x0; //0.0 |
Diletant | 174:0f86eedd511c | 8 | device.ccs.current[0].settings.transfer.mA[0] = 0x0; //0.0 |
Diletant | 174:0f86eedd511c | 9 | device.ccs.current[0].settings.transfer.raw[1] = 0x1111111; //273.06666666666666 |
Diletant | 174:0f86eedd511c | 10 | device.ccs.current[0].settings.transfer.mA[1] = 0x6aaaa; //6.666666666666667 |
Diletant | 174:0f86eedd511c | 11 | device.ccs.current[0].settings.transfer.raw[2] = 0x2222222; //546.1333333333333 |
Diletant | 174:0f86eedd511c | 12 | device.ccs.current[0].settings.transfer.mA[2] = 0xd5555; //13.333333333333334 |
Diletant | 174:0f86eedd511c | 13 | device.ccs.current[0].settings.transfer.raw[3] = 0x3333333; //819.2 |
Diletant | 174:0f86eedd511c | 14 | device.ccs.current[0].settings.transfer.mA[3] = 0x140000; //20.0 |
Diletant | 174:0f86eedd511c | 15 | device.ccs.current[0].settings.transfer.raw[4] = 0x4444444; //1092.2666666666667 |
Diletant | 174:0f86eedd511c | 16 | device.ccs.current[0].settings.transfer.mA[4] = 0x1aaaaa; //26.666666666666668 |
Diletant | 174:0f86eedd511c | 17 | device.ccs.current[0].settings.transfer.raw[5] = 0x5555555; //1365.3333333333333 |
Diletant | 174:0f86eedd511c | 18 | device.ccs.current[0].settings.transfer.mA[5] = 0x215555; //33.333333333333336 |
Diletant | 174:0f86eedd511c | 19 | device.ccs.current[0].settings.transfer.raw[6] = 0x6666666; //1638.4 |
Diletant | 174:0f86eedd511c | 20 | device.ccs.current[0].settings.transfer.mA[6] = 0x280000; //40.0 |
Diletant | 174:0f86eedd511c | 21 | device.ccs.current[0].settings.transfer.raw[7] = 0x7777777; //1911.4666666666667 |
Diletant | 174:0f86eedd511c | 22 | device.ccs.current[0].settings.transfer.mA[7] = 0x2eaaaa; //46.66666666666667 |
Diletant | 174:0f86eedd511c | 23 | device.ccs.current[0].settings.transfer.raw[8] = 0x8888888; //2184.5333333333333 |
Diletant | 174:0f86eedd511c | 24 | device.ccs.current[0].settings.transfer.mA[8] = 0x355555; //53.333333333333336 |
Diletant | 174:0f86eedd511c | 25 | device.ccs.current[0].settings.transfer.raw[9] = 0x9999999; //2457.6 |
Diletant | 174:0f86eedd511c | 26 | device.ccs.current[0].settings.transfer.mA[9] = 0x3c0000; //60.0 |
Diletant | 174:0f86eedd511c | 27 | device.ccs.current[0].settings.transfer.raw[10] = 0xaaaaaaa; //2730.6666666666665 |
Diletant | 174:0f86eedd511c | 28 | device.ccs.current[0].settings.transfer.mA[10] = 0x42aaaa; //66.66666666666667 |
Diletant | 174:0f86eedd511c | 29 | device.ccs.current[0].settings.transfer.raw[11] = 0xbbbbbbb; //3003.733333333333 |
Diletant | 174:0f86eedd511c | 30 | device.ccs.current[0].settings.transfer.mA[11] = 0x495555; //73.33333333333334 |
Diletant | 174:0f86eedd511c | 31 | device.ccs.current[0].settings.transfer.raw[12] = 0xccccccc; //3276.8 |
Diletant | 174:0f86eedd511c | 32 | device.ccs.current[0].settings.transfer.mA[12] = 0x500000; //80.0 |
Diletant | 174:0f86eedd511c | 33 | device.ccs.current[0].settings.transfer.raw[13] = 0xddddddd; //3549.866666666667 |
Diletant | 174:0f86eedd511c | 34 | device.ccs.current[0].settings.transfer.mA[13] = 0x56aaaa; //86.66666666666667 |
Diletant | 174:0f86eedd511c | 35 | device.ccs.current[0].settings.transfer.raw[14] = 0xeeeeeee; //3822.9333333333334 |
Diletant | 174:0f86eedd511c | 36 | device.ccs.current[0].settings.transfer.mA[14] = 0x5d5555; //93.33333333333334 |
Diletant | 174:0f86eedd511c | 37 | device.ccs.current[0].settings.transfer.raw[15] = 0x10000000; //4096.0 |
Diletant | 174:0f86eedd511c | 38 | device.ccs.current[0].settings.transfer.mA[15] = 0x640000; //100.0 |
Diletant | 174:0f86eedd511c | 39 | |
Diletant | 174:0f86eedd511c | 40 | //TODO: real values |
Diletant | 174:0f86eedd511c | 41 | device.ccs.current[1].settings.transfer.points = 16; |
Diletant | 174:0f86eedd511c | 42 | device.ccs.current[1].settings.transfer.raw[0] = 0x0; //0.0 |
Diletant | 174:0f86eedd511c | 43 | device.ccs.current[1].settings.transfer.mA[0] = 0x0; //0.0 |
Diletant | 174:0f86eedd511c | 44 | device.ccs.current[1].settings.transfer.raw[1] = 0x1111111; //273.06666666666666 |
Diletant | 174:0f86eedd511c | 45 | device.ccs.current[1].settings.transfer.mA[1] = 0x6aaaa; //6.666666666666667 |
Diletant | 174:0f86eedd511c | 46 | device.ccs.current[1].settings.transfer.raw[2] = 0x2222222; //546.1333333333333 |
Diletant | 174:0f86eedd511c | 47 | device.ccs.current[1].settings.transfer.mA[2] = 0xd5555; //13.333333333333334 |
Diletant | 174:0f86eedd511c | 48 | device.ccs.current[1].settings.transfer.raw[3] = 0x3333333; //819.2 |
Diletant | 174:0f86eedd511c | 49 | device.ccs.current[1].settings.transfer.mA[3] = 0x140000; //20.0 |
Diletant | 174:0f86eedd511c | 50 | device.ccs.current[1].settings.transfer.raw[4] = 0x4444444; //1092.2666666666667 |
Diletant | 174:0f86eedd511c | 51 | device.ccs.current[1].settings.transfer.mA[4] = 0x1aaaaa; //26.666666666666668 |
Diletant | 174:0f86eedd511c | 52 | device.ccs.current[1].settings.transfer.raw[5] = 0x5555555; //1365.3333333333333 |
Diletant | 174:0f86eedd511c | 53 | device.ccs.current[1].settings.transfer.mA[5] = 0x215555; //33.333333333333336 |
Diletant | 174:0f86eedd511c | 54 | device.ccs.current[1].settings.transfer.raw[6] = 0x6666666; //1638.4 |
Diletant | 174:0f86eedd511c | 55 | device.ccs.current[1].settings.transfer.mA[6] = 0x280000; //40.0 |
Diletant | 174:0f86eedd511c | 56 | device.ccs.current[1].settings.transfer.raw[7] = 0x7777777; //1911.4666666666667 |
Diletant | 174:0f86eedd511c | 57 | device.ccs.current[1].settings.transfer.mA[7] = 0x2eaaaa; //46.66666666666667 |
Diletant | 174:0f86eedd511c | 58 | device.ccs.current[1].settings.transfer.raw[8] = 0x8888888; //2184.5333333333333 |
Diletant | 174:0f86eedd511c | 59 | device.ccs.current[1].settings.transfer.mA[8] = 0x355555; //53.333333333333336 |
Diletant | 174:0f86eedd511c | 60 | device.ccs.current[1].settings.transfer.raw[9] = 0x9999999; //2457.6 |
Diletant | 174:0f86eedd511c | 61 | device.ccs.current[1].settings.transfer.mA[9] = 0x3c0000; //60.0 |
Diletant | 174:0f86eedd511c | 62 | device.ccs.current[1].settings.transfer.raw[10] = 0xaaaaaaa; //2730.6666666666665 |
Diletant | 174:0f86eedd511c | 63 | device.ccs.current[1].settings.transfer.mA[10] = 0x42aaaa; //66.66666666666667 |
Diletant | 174:0f86eedd511c | 64 | device.ccs.current[1].settings.transfer.raw[11] = 0xbbbbbbb; //3003.733333333333 |
Diletant | 174:0f86eedd511c | 65 | device.ccs.current[1].settings.transfer.mA[11] = 0x495555; //73.33333333333334 |
Diletant | 174:0f86eedd511c | 66 | device.ccs.current[1].settings.transfer.raw[12] = 0xccccccc; //3276.8 |
Diletant | 174:0f86eedd511c | 67 | device.ccs.current[1].settings.transfer.mA[12] = 0x500000; //80.0 |
Diletant | 174:0f86eedd511c | 68 | device.ccs.current[1].settings.transfer.raw[13] = 0xddddddd; //3549.866666666667 |
Diletant | 174:0f86eedd511c | 69 | device.ccs.current[1].settings.transfer.mA[13] = 0x56aaaa; //86.66666666666667 |
Diletant | 174:0f86eedd511c | 70 | device.ccs.current[1].settings.transfer.raw[14] = 0xeeeeeee; //3822.9333333333334 |
Diletant | 174:0f86eedd511c | 71 | device.ccs.current[1].settings.transfer.mA[14] = 0x5d5555; //93.33333333333334 |
Diletant | 174:0f86eedd511c | 72 | device.ccs.current[1].settings.transfer.raw[15] = 0x10000000; //4096.0 |
Diletant | 174:0f86eedd511c | 73 | device.ccs.current[1].settings.transfer.mA[15] = 0x640000; //100.0 |
Diletant | 174:0f86eedd511c | 74 | } |
Diletant | 174:0f86eedd511c | 75 | |
Diletant | 174:0f86eedd511c | 76 | void InitCCSState(void) { |
Diletant | 174:0f86eedd511c | 77 | device.ccs.current[0].state.sum = 0; |
Diletant | 174:0f86eedd511c | 78 | device.ccs.current[0].state.raw = 0; |
Diletant | 174:0f86eedd511c | 79 | device.ccs.current[0].state.mA = ccsCurrent(0, device.ccs.current[0].state.raw); |
Diletant | 174:0f86eedd511c | 80 | |
Diletant | 174:0f86eedd511c | 81 | device.ccs.current[1].state.sum = 0; |
Diletant | 174:0f86eedd511c | 82 | device.ccs.current[1].state.raw = 0; |
Diletant | 174:0f86eedd511c | 83 | device.ccs.current[1].state.mA = ccsCurrent(1, device.ccs.current[0].state.raw); |
Diletant | 174:0f86eedd511c | 84 | } |
Diletant | 174:0f86eedd511c | 85 | |
Diletant | 174:0f86eedd511c | 86 | void DeviceStartCCS(void) { |
Diletant | 174:0f86eedd511c | 87 | } |
Diletant | 174:0f86eedd511c | 88 | |
Diletant | 174:0f86eedd511c | 89 | int32_t ccsInterpolate(int32_t a,int32_t a1,int32_t a2,int32_t b1,int32_t b2) { |
Diletant | 174:0f86eedd511c | 90 | int32_t ma, mb; |
Diletant | 174:0f86eedd511c | 91 | while (1) { |
Diletant | 174:0f86eedd511c | 92 | if (a1 == a) return b1; |
Diletant | 174:0f86eedd511c | 93 | if (a2 == a) return b2; |
Diletant | 174:0f86eedd511c | 94 | |
Diletant | 174:0f86eedd511c | 95 | ma = (a1 + a2) >> 1; |
Diletant | 174:0f86eedd511c | 96 | mb = (b1 + b2) >> 1; |
Diletant | 174:0f86eedd511c | 97 | |
Diletant | 174:0f86eedd511c | 98 | if (a < ma) { |
Diletant | 174:0f86eedd511c | 99 | if (a2 == ma) return mb; |
Diletant | 174:0f86eedd511c | 100 | if (b1 == mb) return mb; |
Diletant | 174:0f86eedd511c | 101 | a2 = ma; b2 = mb; |
Diletant | 174:0f86eedd511c | 102 | } else if (a > ma) { |
Diletant | 174:0f86eedd511c | 103 | if (a1 == ma) return mb; |
Diletant | 174:0f86eedd511c | 104 | if (b2 == mb) return mb; |
Diletant | 174:0f86eedd511c | 105 | a1 = ma; b1 = mb; |
Diletant | 174:0f86eedd511c | 106 | } else return mb; |
Diletant | 174:0f86eedd511c | 107 | } |
Diletant | 174:0f86eedd511c | 108 | } |
Diletant | 174:0f86eedd511c | 109 | |
Diletant | 174:0f86eedd511c | 110 | int32_t ccsCurrent(uint32_t channel, uint32_t raw) { |
Diletant | 174:0f86eedd511c | 111 | int32_t r1, r2; |
Diletant | 174:0f86eedd511c | 112 | for (uint8_t i = 1; i < device.ccs.current[channel].settings.transfer.points; i++) { |
Diletant | 174:0f86eedd511c | 113 | r1 = device.ccs.current[channel].settings.transfer.raw[i - 1]; |
Diletant | 174:0f86eedd511c | 114 | if (raw < r1) { |
Diletant | 174:0f86eedd511c | 115 | return device.ccs.current[channel].settings.transfer.mA[i - 1]; |
Diletant | 174:0f86eedd511c | 116 | } |
Diletant | 174:0f86eedd511c | 117 | |
Diletant | 174:0f86eedd511c | 118 | r2 = device.ccs.current[channel].settings.transfer.raw[i]; |
Diletant | 174:0f86eedd511c | 119 | if (raw < r2) { |
Diletant | 174:0f86eedd511c | 120 | int32_t c1 = device.ccs.current[channel].settings.transfer.mA[i - 1]; |
Diletant | 174:0f86eedd511c | 121 | int32_t c2 = device.ccs.current[channel].settings.transfer.mA[i]; |
Diletant | 174:0f86eedd511c | 122 | return ccsInterpolate(raw, r1, r2, c1, c2); |
Diletant | 174:0f86eedd511c | 123 | } |
Diletant | 174:0f86eedd511c | 124 | } |
Diletant | 174:0f86eedd511c | 125 | return device.ccs.current[channel].settings.transfer.mA[device.ccs.current[channel].settings.transfer.points - 1]; |
Diletant | 174:0f86eedd511c | 126 | } |
Diletant | 174:0f86eedd511c | 127 | |
Diletant | 174:0f86eedd511c | 128 | void ccsProcess(void) { |
Diletant | 174:0f86eedd511c | 129 | device.ccs.current[0].state.sum += device.controller.SSP.in[1]; |
Diletant | 174:0f86eedd511c | 130 | device.ccs.current[1].state.sum += device.controller.SSP.in[2]; |
Diletant | 174:0f86eedd511c | 131 | |
Diletant | 174:0f86eedd511c | 132 | if (device.measurement.counter == 31) { |
Diletant | 174:0f86eedd511c | 133 | device.ccs.current[0].state.raw = device.ccs.current[0].state.sum >> 5; |
Diletant | 174:0f86eedd511c | 134 | device.ccs.current[0].state.mA = ccsCurrent(0, device.ccs.current[0].state.raw); |
Diletant | 174:0f86eedd511c | 135 | device.ccs.current[0].state.sum = 0; |
Diletant | 174:0f86eedd511c | 136 | device.ccs.current[1].state.raw = device.ccs.current[1].state.sum >> 5; |
Diletant | 174:0f86eedd511c | 137 | device.ccs.current[1].state.mA = ccsCurrent(1, device.ccs.current[1].state.raw); |
Diletant | 174:0f86eedd511c | 138 | device.ccs.current[1].state.sum = 0; |
Diletant | 174:0f86eedd511c | 139 | } |
Diletant | 174:0f86eedd511c | 140 | } |