0 | module Bindings.RtlSdr.Frequency
 1 |
 2 | import Bindings.RtlSdr.Device
 3 | import Bindings.RtlSdr.Error
 4 | import Bindings.RtlSdr.Raw.Frequency
 5 | import Bindings.RtlSdr.Raw.Support
 6 |
 7 | import System.FFI
 8 |
 9 | %default total
10 |
11 | ||| Set crystal oscillator frequencies used for the RTL2832 and the tuner IC.
12 | |||
13 | ||| Usually both ICs use the same clock. Changing the clock may make sense if
14 | ||| you are applying an external clock to the tuner or to compensate the
15 | ||| frequency (and samplerate) error caused by the original (cheap) crystal.
16 | |||
17 | ||| NOTE: Call this function only if you fully understand the implications.
18 | |||
19 | ||| @h is the device handle
20 | ||| @f  is the frequency used to clock the RTL2832 in Hz
21 | ||| @tf is the frequency value used to clock the tuner IC in Hz
22 | export
23 | setXTALFreq : Ptr RtlSdrHandle -> Int -> Int -> IO (Either RTLSDR_ERROR ())
24 | setXTALFreq h f tf = do
25 |   r <- fromPrim $ set_xtal_freq h f tf
26 |   io_pure $ if r == 0 then Right () else Left RtlSdrError
27 |
28 | ||| Get crystal oscillator frequencies used for the RTL2832 and the tuner IC.
29 | |||
30 | ||| Usually both ICs use the same clock.
31 | |||
32 | ||| This will return a tuple of (rtl_freq, tuner_freq) where,
33 | |||   rtl_freq frequency value used to clock the RTL2832 in Hz
34 | |||   tuner_freq frequency value used to clock the tuner IC in Hz
35 | |||
36 | ||| @h is the device handle
37 | export
38 | getXTALFreq : Ptr RtlSdrHandle -> IO (Either RTLSDR_ERROR (Int, Int))
39 | getXTALFreq h = do
40 |   rtl_freq   <- prim__castPtr <$> malloc 4 -- rtl_freq frequency value used to clock the RTL2832 in Hz
41 |   tuner_freq <- prim__castPtr <$> malloc 4 -- tuner_freq frequency value used to clock the tuner IC in Hz
42 |   r <- fromPrim $ get_xtal_freq h rtl_freq tuner_freq
43 |   let v = (peekInt rtl_freq, peekInt tuner_freq)
44 |   free $ prim__forgetPtr rtl_freq
45 |   free $ prim__forgetPtr tuner_freq
46 |   io_pure $ if r == 0 then Right v else Left RtlSdrError
47 |
48 |
49 | ||| Set actual frequency the device is tuned to.
50 | |||
51 | ||| @h is the device handle
52 | ||| @f is the frequency in Hz
53 | export
54 | setCenterFreq : Ptr RtlSdrHandle -> Int -> IO (Either RTLSDR_ERROR ())
55 | setCenterFreq h f = do
56 |   r <- fromPrim $ set_center_freq h f
57 |   io_pure $ if r == 0 then Right () else Left RtlSdrInvalidFreq
58 |
59 | ||| Get actual frequency the device is tuned to.
60 | |||
61 | ||| @h is the device handle
62 | export
63 | getCenterFreq : Ptr RtlSdrHandle -> IO (Either RTLSDR_ERROR Int)
64 | getCenterFreq h = do
65 |   r <- fromPrim $ get_center_freq h
66 |   io_pure $ if r == 0 then Left RtlSdrError else Right r
67 |
68 | ||| Set the frequency correction value for the device.
69 | |||
70 | ||| @h is the device handle
71 | ||| @ppm correction value in parts per million (ppm)
72 | export
73 | setFreqCorrection : Ptr RtlSdrHandle -> Int -> IO (Either RTLSDR_ERROR ())
74 | setFreqCorrection h ppm = do
75 |   r <- fromPrim $ set_freq_correction h ppm
76 |   io_pure $ if r == 0 then Right () else Left RtlSdrError
77 |
78 | ||| Get actual frequency correction value of the device.
79 | |||
80 | ||| @h is the device handle
81 | export
82 | getFreqCorrection : Ptr RtlSdrHandle -> IO Int
83 | getFreqCorrection h = fromPrim $ get_freq_correction h
84 |