summaryrefslogtreecommitdiff
path: root/lmx2487lib.lua
diff options
context:
space:
mode:
Diffstat (limited to 'lmx2487lib.lua')
-rw-r--r--lmx2487lib.lua83
1 files changed, 83 insertions, 0 deletions
diff --git a/lmx2487lib.lua b/lmx2487lib.lua
index b115d4e..c01da6e 100644
--- a/lmx2487lib.lua
+++ b/lmx2487lib.lua
@@ -62,3 +62,86 @@ function set_lmx2487_board_to_default_state()
update_lmx2487state()
end
+
+-- bits in mask counted starting from position 1,
+-- i.e. 1 sets bit in 1st pos, 2 sets bit in 2nd pos
+mask_bits14to24=bit.lshift( bit.bit(11)-1, 13)
+mask_bits2to13=bit.lshift( bit.bit(12)-1, 1)
+mask_bits5to16=bit.lshift( bit.bit(12)-1, 4)
+mask_bits5to14=bit.lshift( bit.bit(10)-1, 4)
+mask_bits15to24=bit.lshift( bit.bit(10)-1, 14)
+mask_bits5to24=bit.bor(mask_bits5to14, mask_bits15to24)
+mask_low12bits = bit.bit(12)-1
+mask_bits13andAbove = bit.bnot(mask_low12bits)
+
+function FreqToSettingsBin(freq)
+ local freq, RF_N, RF_FN, RF_FD = freq2regestersValues(freq)
+ R0_new=get_lmx2487register(0)
+ R1_new=get_lmx2487register(1)
+ R5_new=get_lmx2487register(5)
+
+ -- this code is extremely convoluted refer to the lmx2487 manual
+ -- to see what bits are used in what place in a register.
+ -- But I show in comments obsolete but human readable notation:
+ -- tableReplace( Rn, bitSlice(..), stPos, endPos)
+
+ -- settings["R0"]=tableReplace(settings["R0"], bRF_N, 14, 24)
+ bitsRF_N = bit.lshift(RF_N, 13)
+ R0_new = bit.band(R0_new, bit.bnot(mask_bits14to24))
+ R0_new = bit.bor(R0_new, bitsRF_N)
+
+ -- settings["R0"]=tableReplace(settings["R0"], bitSlice(bRF_FN, 1, 12), 2, 13)
+ bitsRF_FN_low = bit.band( RF_FN, mask_low12bits)
+ R0_new = bit.band(R0_new, bit.bnot(mask_bits2to13))
+ R0_new = bit.bor(R0_new, bit.lshift(bitsRF_FN_low,1))
+
+ -- settings["R1"]=tableReplace(settings["R1"], bitSlice(bRF_FD, 1, 12), 5, 16)
+ bitsRF_FD_low = bit.band( RF_FD, mask_low12bits)
+ R1_new = bit.band(R1_new, bit.bnot(mask_bits5to16))
+ R1_new = bit.bor(R1_new, bit.lshift(bitsRF_FD_low,4))
+
+ -- settings["R5"]=tableReplace(settings["R5"], bitSlice(bRF_FN, 13, 22), 5, 14)
+ -- settings["R5"]=tableReplace(settings["R5"], bitSlice(bRF_FD, 13, 22), 15, 24)
+ RF_FN_above_12 = bit.band( RF_FN, mask_bits13andAbove)
+ RF_FN_part = bit.rshift(RF_FN_above_12, 12)
+ RF_FD_above_12 = bit.band( RF_FD, mask_bits13andAbove)
+ RF_FD_above_12 = bit.rshift( RF_FD_above_12, 2)
+ new_bits = bit.bor(RF_FN_part, RF_FD_above_12)
+ R5_new = bit.band(R5_new, bit.bnot(mask_bits5to24))
+ R5_new = bit.bor(R5_new, bit.lshift(new_bits,4))
+
+ set_lmx2487register(R0_new, 0)
+ set_lmx2487register(R1_new, 1)
+ set_lmx2487register(R5_new, 5)
+end
+
+-- test setFreq
+set_lmx2487_board_to_default_state()
+FreqToSettingsBin(6.834e9)
+if
+ get_lmx2487register(0) ~= 11192320
+ and get_lmx2487register(1) ~= 4362243
+ and get_lmx2487register(2) ~= 8560613 -- should not change
+ and get_lmx2487register(5) ~= 16003291
+then
+ print("ERROR in setFreq: registers do not match")
+end
+FreqToSettingsBin(6.8204e9)
+if
+ get_lmx2487register(0) ~= 11174912
+ and get_lmx2487register(1) ~= 4362243 -- should not change
+ and get_lmx2487register(2) ~= 8560613 -- should not change
+ and get_lmx2487register(5) ~= 15992043
+then
+ print("ERROR in setFreq: registers do not match")
+end
+FreqToSettingsBin(6.8346e9)
+if
+ get_lmx2487register(0) ~= 11193856
+ and get_lmx2487register(1) ~= 4362243 -- should not change
+ and get_lmx2487register(2) ~= 8560613 -- should not change
+ and get_lmx2487register(5) ~= 16005163
+then
+ print("ERROR in setFreq: registers do not match")
+end
+