summaryrefslogtreecommitdiff
path: root/freq2lmx2487settings.lua
blob: 1de1cad4eab51b65c2b8959535b6c69ae5ea4501 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
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)
   
  return freq, RF_N, RF_FN, RF_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, RF_N, RF_FN, RF_FD = freq2regestersValues(freq)

    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, " "))

    --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