summaryrefslogtreecommitdiffstats
path: root/pyprofibus/phy.py
blob: e51c55ff2e9c879bc90800b151c119adefb57cfa (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# -*- coding: utf-8 -*-
#
# PROFIBUS DP - Communication Processor PHY access library
#
# Copyright (c) 2013-2016 Michael Buesch <m@bues.ch>
#
# Licensed under the terms of the GNU General Public License version 2,
# or (at your option) any later version.
#

from __future__ import division, absolute_import, print_function, unicode_literals
from pyprofibus.compat import *

import time
import sys
from collections import deque

from pyprofibus.util import *


__all__ = [
	"PhyError",
	"CpPhy",
]


class PhyError(ProfibusError):
	"""PHY exception.
	"""

class CpPhy(object):
	"""PROFIBUS CP PHYsical layer base class.
	"""

	PFX = "PHY: "

	# Profibus baud-rates
	BAUD_9600	= 9600
	BAUD_19200	= 19200
	BAUD_45450	= 45450
	BAUD_93750	= 93750
	BAUD_187500	= 187500
	BAUD_500000	= 500000
	BAUD_1500000	= 1500000
	BAUD_3000000	= 3000000
	BAUD_6000000	= 6000000
	BAUD_12000000	= 12000000

	__slots__ = (
		"debug",
		"__txQueueDAs",
		"__txQueueTelegrams",
		"__allocUntil",
		"__secPerFrame",
	)

	def __init__(self, debug=False, *args, **kwargs):
		self.debug = debug
		self.__close()

	def _debugMsg(self, msg):
		if self.debug:
			print(self.PFX + str(msg))

	def close(self):
		"""Close the PHY device.
		This method may be reimplemented in the PHY driver.
		"""
		self.__close()

	def __close(self):
		self.__txQueueDAs = deque()
		self.__txQueueTelegrams = [None] * (0x7F + 1)
		self.__allocUntil = monotonic_time()
		self.__secPerFrame = 0.0

	def sendData(self, telegramData, srd):
		"""Send data to the physical line.
		Reimplement this method in the PHY driver.
		"""
		raise NotImplementedError

	def pollData(self, timeout):
		"""Poll received data from the physical line.
		timeout => timeout in seconds.
			   0.0 = no timeout, return immediately.
			   negative = unlimited.
		Reimplement this method in the PHY driver.
		"""
		raise NotImplementedError

	def poll(self, timeout=0.0):
		"""timeout => timeout in seconds.
			      0.0 = no timeout, return immediately.
			      negative = unlimited.
		"""
		if self.__txQueueDAs:
			self.__send()
		return self.pollData(timeout)

	def __send(self):
		now = monotonic_time()
		if self.__canAllocateBus(now):
			da = self.__txQueueDAs.popleft()
			telegram, srd, maxReplyLen = self.__txQueueTelegrams[da]
			self.__txQueueTelegrams[da] = None
			telegramData = telegram.getRawData()
			self.__allocateBus(now, len(telegramData), maxReplyLen)
			self.sendData(telegramData, srd)

	def send(self, telegram, srd, maxReplyLen=-1):
		if maxReplyLen < 0 or maxReplyLen > 255:
			maxReplyLen = 255

		da = telegram.da
		if self.__txQueueTelegrams[da] is None:
			self.__txQueueDAs.append(da)
		self.__txQueueTelegrams[da] = (telegram, srd, maxReplyLen)

		self.__send()

	def setConfig(self, baudrate=BAUD_9600, *args, **kwargs):
		"""Set the PHY configuration.
		This method may be reimplemented in the PHY driver.
		"""
		symLen = 1.0 / baudrate
		self.__secPerFrame = symLen * float(1 + 8 + 1 + 1)

	def __canAllocateBus(self, now):
		return now >= self.__allocUntil

	def __allocateBus(self, now, nrSendOctets, nrReplyOctets):
		secPerFrame = self.__secPerFrame
		seconds = secPerFrame * nrSendOctets
		if nrReplyOctets:
			pass#TODO IFS
			seconds += secPerFrame * nrReplyOctets
		pass#TODO
		self.__allocUntil = now + seconds

	def releaseBus(self):
		self.__allocUntil = monotonic_time()
		if self.__txQueueDAs:
			self.__send()

	def clearTxQueueAddr(self, da):
		"""Remove all TX queue entries for the given destination address.
		"""
		if da in self.__txQueueDAs:
			self.__txQueueDAs.remove(da)
		self.__txQueueTelegrams[da] = None
bues.ch cgit interface