diff options
author | Eugeniy E. Mikhailov <evgmik@gmail.com> | 2021-06-02 09:53:27 -0400 |
---|---|---|
committer | Eugeniy E. Mikhailov <evgmik@gmail.com> | 2021-06-02 09:55:12 -0400 |
commit | 4048b59b03ddc88f8cc9ec65f720aefddd9b3ea4 (patch) | |
tree | 89e13ec248d5359f0f06c4aa08a9de8598bf4f7a | |
parent | 9d02b07fef4a2679fc8e50e0340d72dace0f1e57 (diff) | |
download | nodeMCU_rf_source_lmx2487-4048b59b03ddc88f8cc9ec65f720aefddd9b3ea4.tar.gz nodeMCU_rf_source_lmx2487-4048b59b03ddc88f8cc9ec65f720aefddd9b3ea4.zip |
working prototype, written together with Charris
-rw-r--r-- | binaryLib.lua | 112 | ||||
-rw-r--r-- | freq2lmx2487settings.lua | 119 |
2 files changed, 231 insertions, 0 deletions
diff --git a/binaryLib.lua b/binaryLib.lua new file mode 100644 index 0000000..cf8a167 --- /dev/null +++ b/binaryLib.lua @@ -0,0 +1,112 @@ + --[[ +Eugeniy Mikhailov +1 June 2021 + ]] +------------ BITS TO DECIMAL ------------ +function toNum(bits) + local n = 0; local m = 1 -- m represents power of 2 + for i = 1, #bits do -- '#' indicates number of bits in variable + n = n + bits[i]*m + m = m*2 + end + return n +end + + +------------ DECIMAL TO BIT ------------ +-- ****NOTE: Suitable only for positive integer inputs**** +-- continue to divide by two until quotient is zero +function toBits(num) + local t = {} + while num > 0 do + remainder = num % 2 -- Basics of binary. If num is divisible by two, 0 in table. If not, 1 in table + table.insert(t, remainder)-- defaults to #t+1 key + num = (num - remainder)/2 + --num = math.floor((num - remainder)/2) -- carry out calculation + -- note math.floor used here to eliminate floating values + --we subtract the number with the binary bit found, then divide by two to find the next bit + end + return t +end + +------------ BINARY SEPARATION TO STRING ------------ +function bin2string(bTable, separator) + if nil == separator then separator="" end + local str="" + for i=#bTable,1,-1 do + if (i % 8) == 0 then str=str..separator end -- space between every 8 bits + if bTable[i] == 0 -- to take care of floats + then + str=str.."0" + else + str=str.."1" -- assumes only nonzeros are ones + -- if nonzeros/ones involved, will also be counted as ones here + end + end + return str +end + +------------ TESTS FOR bin2string------------ +if "11111110" ~= bin2string(toBits(254)) +then + print("Error in bits conversion") +end +if 2376 ~= toNum(toBits(2376)) +then + print("Error in bits conversion") +end + +------------ SLICE STRING ------------ +function bitSlice(bTable, low, high) + -- make sure that 1 <= low <= high + local slice={} + local cnt=1 --counter + for i=low,high + do + slice[cnt] = (bTable[i]) + cnt = cnt+1 + end + return slice +end + +------------ TESTS FOR bitSlice ------------ +if "345" ~= table.concat(bitSlice({1,2,3,4,5,6,7,8}, 3,5)) +then + print("Error bitSlice is compromised") +end + +------------ COPY TABLE ------------ +-- issue with index naming in future table. +function copyTableArray(src) + local dst={} + for i=1,#src do + dst[i] = (src[i]) + end + return dst +end + +------------ COMBINE SLICES ------------ +function tableReplace(bTable1, bTable2, low, high) -- low=first index, high=last index + local dest = copyTableArray(bTable1) + local cnt=1 + for i=low,high do + dest[i]=(bTable2[cnt]) + cnt = cnt + 1 + end + return dest -- destination +end + +------------ PAD ZEROS INTO EMPTY INDICES ------------ +function pad2NumBitsInPlace(bTable, num) + for i= #bTable+1, num do + bTable[i]=0 + end + return bTable +end + +------------ TEST PAD ZEROS ------------ +if "00010111" ~= bin2string(pad2NumBitsInPlace({1,1,1,0,1},8)) +then + print("Error in pad2NumBitsInPlace") +end + diff --git a/freq2lmx2487settings.lua b/freq2lmx2487settings.lua new file mode 100644 index 0000000..c3031e9 --- /dev/null +++ b/freq2lmx2487settings.lua @@ -0,0 +1,119 @@ + --[[ +Eugeniy Mikhailov +1 June 2021 + ]] + +--------- FREQ TO BITS --------- +function freq2regestersValues(freq) + + local F_COMP = 5000000 -- magic number + local N = freq / F_COMP + + local RF_N = math.floor(N) + + local frac = math.abs(N - RF_N) + + local RF_FD = 4000000 --magic number + local RF_FN = math.floor( (frac * RF_FD) + 0.5) + + local bRF_N=toBits(RF_N) + local bRF_FN=toBits(RF_FN) + local bRF_FD=toBits(RF_FD) + pad2NumBitsInPlace(bRF_N, 11) + pad2NumBitsInPlace(bRF_FN, 22) + pad2NumBitsInPlace(bRF_FD, 22) + + -- print("bRF_N = "..bin2string(bRF_N, " ")) + -- print("bRF_FN = "..bin2string(bRF_FN, " ")) + -- print("bRF_FD = "..bin2string(bRF_FD, " ")) + + return freq, bRF_N, bRF_FN, bRF_FD +end + +------ INITIAL SETTINGS (send to chip)------- +function initSettings( ) + local setting = { } + -- prefill with magic numbers & pad emtpy bits with 0's + setting["R0"] = pad2NumBitsInPlace( toBits(11197502), 24) + setting["R1"] = pad2NumBitsInPlace( toBits(4352515), 24) + setting["R2"] = pad2NumBitsInPlace( toBits(48238), 24) + setting["R3"] = pad2NumBitsInPlace( toBits(15925287), 24) + setting["R4"] = pad2NumBitsInPlace( toBits(10618633), 24) + setting["R5"] = pad2NumBitsInPlace( toBits(393579), 24) + setting["R6"] = pad2NumBitsInPlace( toBits(8126541), 24) + setting["R7"] = pad2NumBitsInPlace( toBits(10639), 24) + return setting +end + + +---------- COPY TABLE WITH DIFFERNT INDEX NAME ----------- +function copySettings(old) + local dst={} + dst["R0"]= copyTableArray(old["R0"]) + dst["R1"]= copyTableArray(old["R1"]) + dst["R2"]= copyTableArray(old["R2"]) + dst["R3"]= copyTableArray(old["R3"]) + dst["R4"]= copyTableArray(old["R4"]) + dst["R5"]= copyTableArray(old["R5"]) + dst["R6"]= copyTableArray(old["R6"]) + dst["R7"]= copyTableArray(old["R7"]) + return dst +end + +--------- SLICE AND REPLACE ------------ +function FreqToSettings(freq, old_settings) --create table with new values of R0, R1, and R5 + local settings= copySettings(old_settings) + local freq, bRF_N, bRF_FN, bRF_FD = freq2regestersValues(freq) + + --print(bin2string(settings["R0"], " ")) + settings["R0"]=tableReplace(settings["R0"], bRF_N, 14, 24) + --print(bin2string(settings["R0"], " ")) + settings["R0"]=tableReplace(settings["R0"], bitSlice(bRF_FN, 1, 12), 2, 13) + --print(bin2string(settings["R0"], " ")) + + --print(bin2string(settings["R1"], " ")) + settings["R1"]=tableReplace(settings["R1"], bitSlice(bRF_FD, 1, 12), 5, 16) + --print(bin2string(settings["R1"], " ")) + + --print(bin2string(settings["R5"], " ")) + settings["R5"]=tableReplace(settings["R5"], bitSlice(bRF_FN, 13, 22), 5, 14) + --print(bin2string(settings["R5"], " ")) + settings["R5"]=tableReplace(settings["R5"], bitSlice(bRF_FD, 13, 22), 15, 24) + --print(bin2string(settings["R5"], " ")) + + return settings +end + + + +-- test +sinit= initSettings() +snew = FreqToSettings(6.834e9, sinit) +if bin2string(snew["R0"]) ~= "101010101100100000000000" then + print("Frequency to R0 register value conversion is broken") +end +if bin2string(snew["R1"]) ~= "010000101001000000000011" then + print("Frequency to R1 register value conversion is broken") +end +if bin2string(snew["R5"]) ~= "111101000011000011011011" then + print("Frequency to R5 register value conversion is broken") +end +sinit=nil +snew=nil + +------- no changes to previous code ------------- +-- send new register binary strings to chip +function set_lmx2487_board_to_new_state(settings) + + set = {"R0", "R1", "R2", "R3", "R4", "R5", "R6", "R7"} + for i = 1, #set do + set_lmx2487register(toNum(settings[(set[i])]), (i-1)) + end + update_lmx2487state() +end + +function setFreq(freq) + settings = FreqToSettings(freq, settings) + set_lmx2487_board_to_new_state(settings) +end + |