Skip to content

Instantly share code, notes, and snippets.

@jlesech
Created July 30, 2012 17:25

Revisions

  1. jlesech revised this gist Jul 31, 2012. 1 changed file with 26 additions and 23 deletions.
    49 changes: 26 additions & 23 deletions pyTWI.py
    Original file line number Diff line number Diff line change
    @@ -19,30 +19,28 @@
    """

    import argparse
    import sys
    import sys

    """
    Prescaler configuration.
    """
    class PrescalerConfiguration:

    def __init__(self, prescalerValue, prescalerBits):
    self.value = prescalerValue
    self.bits = prescalerBits

    """
    Compute rate.
    TWI bus max frequency.
    """
    def ComputeBitRate(f_cpu, f_scl, prescalerValue):
    bitRate = (f_cpu - 16 * f_scl) / (2 * f_scl * prescalerValue)
    return bitRate
    TWI_MAX_FREQUENCY = 400000

    """
    Prescaler bits in TWPS register.
    """
    TWPS0 = 0
    TWPS1 = 1

    """
    Prescaler configuration.
    """
    class PrescalerConfiguration:

    def __init__(self, prescalerValue, prescalerBits):
    self.value = prescalerValue
    self.bits = prescalerBits

    """
    Available prescaler configurations.
    """
    @@ -53,33 +51,38 @@ def ComputeBitRate(f_cpu, f_scl, prescalerValue):
    PrescalerConfiguration(64, ((1 << TWPS1) | (1 << TWPS0)))
    ]

    """
    Compute bit rate.
    """
    def ComputeBitRate(f_cpu, f_scl, prescalerValue):
    bitRate = (f_cpu - 16 * f_scl) / (2 * f_scl * prescalerValue)
    return bitRate

    """
    Main function.
    """
    def main():
    # Parse arguments.
    parser = argparse.ArgumentParser()
    parser.add_argument(
    "-f_cpu", dest = "f_cpu", default = "16000000",
    "-f_cpu", dest = "f_cpu", type = int, default = 16000000,
    help = "CPU frequency in Hz")
    parser.add_argument(
    "-f_scl", dest = "f_scl", default = "400000",
    "-f_scl", dest = "f_scl", type = int, default = 400000,
    help = "SPI frequency in Hz")
    args = parser.parse_args()

    print "F_CPU = " + args.f_cpu + " Hz"
    print "F_SCL = " + args.f_scl + " Hz"

    f_cpu = int(args.f_cpu)
    f_scl = int(args.f_scl)
    print "F_CPU = %d Hz" % (args.f_cpu)
    print "F_SCL = %d Hz" % (args.f_scl)

    # TODO: check frequencies.
    # Check the preconditions.
    assert(args.f_scl <= TWI_MAX_FREQUENCY)

    bitRate = 65535
    index = 0

    for index in range(len(prescalerConfigurations)):
    bitRate = ComputeBitRate(f_cpu, f_scl, prescalerConfigurations[index].value)
    bitRate = ComputeBitRate(args.f_cpu, args.f_scl, prescalerConfigurations[index].value)
    if bitRate <= 255:
    break

  2. jlesech revised this gist Jul 30, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion pyTWI.py
    Original file line number Diff line number Diff line change
    @@ -78,7 +78,7 @@ def main():
    bitRate = 65535
    index = 0

    for index in range(4):
    for index in range(len(prescalerConfigurations)):
    bitRate = ComputeBitRate(f_cpu, f_scl, prescalerConfigurations[index].value)
    if bitRate <= 255:
    break
  3. jlesech created this gist Jul 30, 2012.
    95 changes: 95 additions & 0 deletions pyTWI.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,95 @@
    #!/usr/bin/env python
    # encoding: utf-8

    """
    Copyright (C) 2012 Julien Le Sech - www.idreammicro.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 3 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 argparse
    import sys

    """
    Prescaler configuration.
    """
    class PrescalerConfiguration:

    def __init__(self, prescalerValue, prescalerBits):
    self.value = prescalerValue
    self.bits = prescalerBits

    """
    Compute rate.
    """
    def ComputeBitRate(f_cpu, f_scl, prescalerValue):
    bitRate = (f_cpu - 16 * f_scl) / (2 * f_scl * prescalerValue)
    return bitRate

    """
    Prescaler bits in TWPS register.
    """
    TWPS0 = 0
    TWPS1 = 1

    """
    Available prescaler configurations.
    """
    prescalerConfigurations = [
    PrescalerConfiguration(1, 0),
    PrescalerConfiguration(4, (1 << TWPS0)),
    PrescalerConfiguration(16, (1 << TWPS1)),
    PrescalerConfiguration(64, ((1 << TWPS1) | (1 << TWPS0)))
    ]

    """
    Main function.
    """
    def main():
    # Parse arguments.
    parser = argparse.ArgumentParser()
    parser.add_argument(
    "-f_cpu", dest = "f_cpu", default = "16000000",
    help = "CPU frequency in Hz")
    parser.add_argument(
    "-f_scl", dest = "f_scl", default = "400000",
    help = "SPI frequency in Hz")
    args = parser.parse_args()

    print "F_CPU = " + args.f_cpu + " Hz"
    print "F_SCL = " + args.f_scl + " Hz"

    f_cpu = int(args.f_cpu)
    f_scl = int(args.f_scl)

    # TODO: check frequencies.

    bitRate = 65535
    index = 0

    for index in range(4):
    bitRate = ComputeBitRate(f_cpu, f_scl, prescalerConfigurations[index].value)
    if bitRate <= 255:
    break

    # TODO: check bitrate value.

    print "Bit rate (TWBR) = %d" % (bitRate)
    print "Prescaler value = %d => prescaler bits (TWPS) = %d" % (prescalerConfigurations[index].value, prescalerConfigurations[index].bits)

    """
    """
    if __name__ == '__main__':
    main()