Project

General

Profile

New Model #7599 » ftlx011.py

first dev version of the driver - Pavel Milanes, 07/29/2021 08:47 PM

 
# Copyright 2019 Pavel Milanes, CO7WT <pavelmc@gmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import struct
import os
import logging
import time

from time import sleep
from chirp import chirp_common, directory, memmap, errors, util, bitwise
from textwrap import dedent

LOG = logging.getLogger(__name__)

### SAMPLE MEM DUMP as sent from the radios

# FTL-1011
#0x000000 52 f0 16 90 04 08 38 c0 00 00 00 01 00 00 00 ff |R.....8.........|
#0x000010 20 f1 00 20 00 00 00 20 04 47 25 04 47 25 00 00 | .. ... .G%.G%..|

# FTL-2011
#0x000000: 50 90 21 40 04 80 fc 40 00 00 00 01 00 00 00 ff |P.!@...@........|
#0x000010: 20 f1 00 0b 00 00 00 0b 14 51 70 14 45 70 00 00 |.........Qp.Ep..|


MEM_FORMAT = """
#seekto 0x000;
u8 rid[2]; // Radio Identification?
bbcd if[2]; // Radio internal IF (21.40 Mhz)
u8 chcount; // how many channels are programmed
u8 unknownB1:1,
unknownB2:1,
unknownB3:1,
monitor:1, // monitor disable; 1 = disabled
offhook:1, // offhook disable; 1 = disabled
unknownB6:1,
unknownB7:1,
unknownB8:1;
u8 TOT:4, // TOT in 30 sec steps, 0 = disable / 15 = 7.5min
unknownB:4;

#seekto 0x010;
struct {
u8 empty:1, // channel is empty = 1
notx:1, // TX inhibit (TX freq is ignored)
tot:1, // TOT: 0 = disabled / 1 = enabled
low:1, // low power; 1 = low / 0 = high
busylock:1, // PTT carrier lockout on busy channel
unknownA5:1,
unknownA6:1,
unknownA7:1;
u8 chname;
u8 rx_tone[2]; // empty value is \x00\x0B / disabled is \x00\x00
u8 unknown4;
u8 unknown5;
u8 tx_tone[2]; // empty value is \x00\x0B / disabled is \x00\x00
bbcd rx_freq[3]; // RX freq
bbcd tx_freq[3]; // TX freq
u8 unknownA[2];
} memory[24];

#seekto 0x0190;
char filename[11];

#seekto 0x19C;
u8 checksum;
"""

MEM_SIZE = 0x019C
DTCS_CODES = chirp_common.DTCS_CODES
# make a copy of the tones, is not funny to work with this directly
TONES = list(chirp_common.TONES)
# this old radios has not the full tone ranges in CST
invalid_tones = (
69.3,
159.8,
165.5,
171.3,
177.3,
183.5,
189.9,
196.6,
199.5,
206.5,
229.1,
245.1)

# remove invalid tones
for tone in invalid_tones:
try:
TONES.remove(tone)
except:
pass


def _set_serial(radio):
"""Set the serial protocol settings"""
radio.pipe.timeout = 10
radio.pipe.parity = "N"
radio.pipe.baudrate = 9600


def _checksum(data):
"""the radio block checksum algorithm"""
cs = 0
for byte in data:
cs += ord(byte)

return cs % 256


def _update_cs(radio):
"""Update the checksum on the mmap"""
payload = str(radio.get_mmap())[:-1]
cs = _checksum(payload)
radio._mmap[MEM_SIZE - 1] = cs


def _do_download(radio):
""" The download function """
# Get the whole 413 bytes (0x019D) bytes one at a time with plenty of time
# to get to the user's pace

# set serial discipline
_set_serial(radio)

# UI progress
status = chirp_common.Status()
status.cur = 0
status.max = MEM_SIZE
status.msg = " Press A to clone. "
radio.status_fn(status)

data = ""
for i in range(0, MEM_SIZE):
a = radio.pipe.read(1)
if len(a) == 0:
# error, no received data
if len(data) != 0:
# received some data, not the complete stream
msg = "Just %02i bytes of the %02i received, try again." % \
(len(data), MEM_SIZE)
else:
# timeout, please retry
msg = "No data received, try again."

raise errors.RadioError(msg)

data += a
# UI Update
status.cur = len(data)
radio.status_fn(status)

if len(data) != MEM_SIZE:
msg = "Incomplete data, we need %02i but got %02i bytes." % \
(MEM_SIZE, len(data))
raise errors.RadioError(msg)

if ord(data[-1]) != _checksum(data[:-1]):
msg = "Bad checksum, please try again."
raise errors.RadioError(msg)

return data


def _do_upload(radio):
"""The upload function"""
# set serial discipline
_set_serial(radio)

# UI progress
status = chirp_common.Status()

# 10 seconds timeout
status.cur = 0
status.max = 100
status.msg = " Quick, press MON on the radio to start. "
radio.status_fn(status)

for byte in range(0,100):
status.cur = byte
radio.status_fn(status)
time.sleep(0.1)


# real upload if user don't cancel the timeout
status.cur = 0
status.max = MEM_SIZE
status.msg = " Cloning to radio... "
radio.status_fn(status)

# send data
data = str(radio.get_mmap())

# this radio has a trick, the EEPROM is an ancient SPI one, so it needs
# some time to write, so we send every byte and then allow
# a 0.01 seg to complete the write from the MCU to the SPI EEPROM
c = 0
for byte in data:
radio.pipe.write(byte)
time.sleep(0.01)

# UI Update
status.cur = c
radio.status_fn(status)

# counter
c = c + 1


def _model_match(cls, data):
"""Use a experimental guess to determine if the radio you just
downloaded or the img you opened is for this model"""

# It's hard to tell when this radio is really this radio.
# I use the first 4 bytes, that appears to be the ID and FI settings

LOG.debug("Drivers's ID string:")
LOG.debug(util.hexprint(data[0:4]))
LOG.debug("Radio's ID string:")
LOG.debug(cls.finger)

fp = data[0:4]
if fp == cls.finger:
return True
else:
return False


def bcd_to_int(data):
"""Convert an array of bcdDataElement like \x12
into an int like 12"""
value = 0
a = (data & 0xF0) >> 4
b = data & 0x0F
value = (a * 10) + b
return value


def int_to_bcd(data):
"""Convert a int like 94 to 0x94"""
data, lsb = divmod(data, 10)
data, msb = divmod(data, 10)
res = (msb << 4) + lsb
return res


class ftlx011(chirp_common.CloneModeRadio, chirp_common.ExperimentalRadio):
"""Vertex FTL1011/2011/7011 4/8/12/24 channels"""
VENDOR = "Vertex Standard"
_memsize = MEM_SIZE
_upper = 0
_range = []
finger = ""

@classmethod
def get_prompts(cls):
rp = chirp_common.RadioPrompts()
rp.experimental = \
('This is a experimental driver, use it on your own risk.\n'
'\n'
'By now only the 5.0 khz step models are supported. The 6.25 '
'kHz steps models will be ignored as we do not have one of '
'these to process and support.\n'
)
rp.pre_download = _(dedent("""\
Please follow this steps carefully:

1 - Turn on your radio
2 - Connect the interface cable to your radio.
3 - Click the button on this window to start download
(Radio will beep and led will flash)
4 - Then press the "A" button in your radio to start cloning.
(At the end radio will beep)
"""))
rp.pre_upload = _(dedent("""\
Please follow this steps carefully:

1 - Turn on your radio
2 - Connect the interface cable to your radio
3 - Click the button on this window to start download
(you may see another dialog, click ok)
4 - Radio will beep and led will flash
5 - You will get a 10 seconds timeout to press "MON" before
data upload start
6 - If all goes right radio will beep at end.

After cloning remove the cable and power cycle your radio to
get into normal mode.
"""))
return rp

def get_features(self):
"""Return information about this radio's features"""
rf = chirp_common.RadioFeatures()
rf.has_settings = False
rf.has_bank = False
rf.has_tuning_step = False
rf.has_name = False
#rf.valid_characters = VALID_CHARS
#rf.valid_name_length = 2
rf.has_offset = True
rf.has_mode = True
rf.has_dtcs = True
rf.has_rx_dtcs = True
rf.has_dtcs_polarity = False
rf.has_ctone = True
rf.has_cross = True
rf.valid_duplexes = ["", "-", "+", "off"]
rf.valid_tmodes = ['', 'Tone', 'TSQL', 'DTCS', 'Cross']
rf.valid_cross_modes = [
"Tone->Tone",
"DTCS->DTCS",
"DTCS->",
"->DTCS"]
rf.valid_dtcs_codes = DTCS_CODES
rf.valid_skips = []
rf.valid_modes = ["FM"]
#rf.valid_tuning_steps = [5.0]
rf.valid_bands = [self._range]
rf.memory_bounds = (1, self._upper)
return rf

def sync_in(self):
"""Do a download of the radio eeprom"""
try:
data = _do_download(self)
except Exception, e:
raise errors.RadioError("Failed to communicate with radio:\n %s" % e)

# match model
if _model_match(self, data) is False:
raise errors.RadioError("Incorrect radio model")

self._mmap = memmap.MemoryMap(data)
self.process_mmap()

# set the channel count from the radio eeprom
self._upper = int(ord(data[4]))

def sync_out(self):
"""Do an upload to the radio eeprom"""
# update checksum
_update_cs(self)

# sanity check, match model
data = str(self.get_mmap())
if len(data) != MEM_SIZE:
raise errors.RadioError("Wrong radio image? Size miss match.")

if _model_match(self, data) is False:
raise errors.RadioError("Wrong image? Fingerprint miss match")

try:
_do_upload(self)
except Exception, e:
msg = "Failed to communicate with radio:\n%s" % e
raise errors.RadioError(msg)

def process_mmap(self):
"""Process the memory object"""
self._memobj = bitwise.parse(MEM_FORMAT, self._mmap)

def get_raw_memory(self, number):
"""Return a raw representation of the memory object"""
return repr(self._memobj.memory[number])

def _decode_tone(self, mem, rx=True):
"""Parse the tone data to decode from mem tones are encodded like this
CTCS: mapped [0x80...0xa5] = [67.0...250.3]
DTCS: mixed [0x88, 0x23] last is BCD and first is the 100 power - 88

It return: ((''|DTCS|Tone), Value (None|###), None)"""
mode = ""
tone = None

# get the tone depending of rx or tx
if rx:
t = mem.rx_tone
else:
t = mem.tx_tone
tMSB = t[0]
tLSB = t[1]

print ("==>> %s, %i, %i" % (t, tMSB, tLSB))

# no tone at all
if (tMSB == 0 and tLSB < 128):
print("No tone")
return ('', None, None)

# extract the tone info
if tMSB == 0x00:
# CTCS
mode = "Tone"
try:
tone = TONES[tLSB - 128]
print (" >> %i" % tone)
except IndexError:
LOG.debug("Error decoding a CTCS tone")
pass
else:
# DTCS
mode = "DTCS"
try:
tone = ((tMSB - 0x88) * 100) + bcd_to_int(tLSB)
print(" >> %i" % tone)
except IndexError:
LOG.debug("Error decoding a DTCS tone")
pass

return (mode, tone, None)

def _encode_tone(self, mem, mode, value, pol, rx=True):
"""Parse the tone data to encode from UI to mem
CTCS: mapped [0x80...0xa5] = [67.0...250.3]
DTCS: mixed [0x88, 0x23] last is BCD and first is the 100 power - 88
"""

# array to pass
tone = [0x00, 0x00]

# which mod
if mode == "DTCS":
tone[0] = int(value / 100) + 0x88
tone[1] = int_to_bcd(value % 100)
if mode == "Tone":
#CTCS
tone[1] = TONES.index(value) + 128

# set it
if rx:
mem.rx_tone = tone
else:
mem.tx_tone = tone

def get_memory(self, number):
"""Extract a memory object from the memory map"""
# Get a low-level memory object mapped to the image
_mem = self._memobj.memory[number - 1]
# Create a high-level memory object to return to the UI
mem = chirp_common.Memory()
# number
mem.number = number

# empty
if bool(_mem.empty) is True:
mem.empty = True
return mem

# rx freq
mem.freq = int(_mem.rx_freq) * 1000

# checking if tx freq is disabled
if bool(_mem.notx) is True:
mem.duplex = "off"
mem.offset = 0
else:
tx = int(_mem.tx_freq) * 1000
if tx == mem.freq:
mem.offset = 0
mem.duplex = ""
else:
mem.duplex = mem.freq > tx and "-" or "+"
mem.offset = abs(tx - mem.freq)

# tone data
rxtone = txtone = None
rxtone = self._decode_tone(_mem)
txtone = self._decode_tone(_mem, False)
chirp_common.split_tone_decode(mem, txtone, rxtone)

# this radio has a primitive mode to show the channel number on a 7-segment
# two digit LCD, we will use channel number
# we will use a trick to show the numbers < 10 wit a space not a zero in front
chname = int_to_bcd(mem.number)
if mem.number < 10:
# convert to F# as BCD
chname = mem.number + 240

_mem.chname = chname

# return mem
return mem

def set_memory(self, mem):
"""Store details about a high-level memory to the memory map
This is called when a user edits a memory in the UI"""
# Get a low-level memory object mapped to the image
_mem = self._memobj.memory[mem.number - 1]

# Empty memory
if mem.empty:
_mem.empty = True
_mem.rx_freq = _mem.tx_freq = 0
return

# freq rx
_mem.rx_freq = mem.freq / 1000

# freq tx
if mem.duplex == "+":
_mem.tx_freq = (mem.freq + mem.offset) / 1000
elif mem.duplex == "-":
_mem.tx_freq = (mem.freq - mem.offset) / 1000
elif mem.duplex == "off":
_mem.notx = 1
_mem.tx_freq = _mem.rx_freq
else:
_mem.tx_freq = mem.freq / 1000

# tone data
((txmode, txtone, txpol), (rxmode, rxtone, rxpol)) = \
chirp_common.split_tone_encode(mem)

# validate tone data from here
if rxmode == "Tone" and rxtone in invalid_tones:
msg = "The tone %shz is not valid for this radio" % rxtone
raise errors.UnsupportedToneError(msg)

if txmode == "Tone" and txtone in invalid_tones:
msg = "The tone %shz is not valid for this radio" % txtone
raise errors.UnsupportedToneError(msg)

if rxmode == "DTCS" and rxtone not in DTCS_CODES:
msg = "The digital tone %s is not valid for this radio" % rxtone
raise errors.UnsupportedToneError(msg)

if txmode == "DTCS" and txtone not in DTCS_CODES:
msg = "The digital tone %s is not valid for this radio" % txtone
raise errors.UnsupportedToneError(msg)

self._encode_tone(_mem, rxmode, rxtone, rxpol)
self._encode_tone(_mem, txmode, txtone, txpol, False)

# this radio has a primitive mode to show the channel number on a 7-segment
# two digit LCD, we will use channel number
# we will use a trick to show the numbers < 10 wit a space not a zero in front
chname = int_to_bcd(mem.number)
if mem.number < 10:
# convert to F# as BCD
chname = mem.number + 240

_mem.chname = chname

return mem

@classmethod
def match_model(cls, filedata, filename):
match_size = False
match_model = False

# testing the file data size
if len(filedata) == cls._memsize:
match_size = True
print("Comp: %i file / %i memzise" % (len(filedata), cls._memsize) )

# testing the firmware fingerprint, this experimental
match_model = _model_match(cls, filedata)

if match_size and match_model:
return True
else:
return False


@directory.register
class ftl1011(ftlx011):
"""Vertex FTL1011"""
MODEL = "FTL-1011"
_memsize = MEM_SIZE
_upper = 4
_range = [44000000, 56000000]
finger = "\x52\xf0\x16\x90"


@directory.register
class ftl2011(ftlx011):
"""Vertex FTL2011"""
MODEL = "FTL-2011"
_memsize = MEM_SIZE
_upper = 24
_range = [134000000, 174000000]
finger = "\x50\x90\x21\x40"

(2-2/16)