-
Notifications
You must be signed in to change notification settings - Fork 0
/
msp430.py
144 lines (115 loc) · 3.9 KB
/
msp430.py
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
#
# Copyright 2014 Jari Ojanen
#
from codegen import OClass, OMethod, OStruct, OMacro, OArg, PRIVATE, OEmptyLine
from codegen import write_file, write_file_n
from parseOrg import ParseOrg
from config import Spi, Port
import codegen as gen
PATH = "../msp430/"
#PATH = "tmp/"
class Int(OArg):
def __init__(self, name):
OArg.__init__(self, name, "int")
class Byte(OArg):
def __init__(self, name):
OArg.__init__(self, name, "byte")
def gen_class(cname, attribs, methods):
c = OClass(cname)
s = OStruct(cname)
cargs = []
ccode = []
tname = cname+"_t"
for name, tpe in attribs:
if name[0]=="-":
name = name[1:]
else:
cargs.append(OArg(name, tpe))
ccode.append("self->"+name+" = "+name+";")
s << OArg(name,tpe)
for mname, args, tpe in methods:
if mname == "init":
args = cargs
args = [OArg("*self", tname)] + args
m = OMethod(mname, tpe, args)
if mname == "init":
for cl in ccode:
m << cl
if mname.startswith("_"):
m = OMethod(mname[1:], "void", args, mods={PRIVATE})
c << m
write_file(c, PATH)
def bitRead(pin, reg):
return "((" + pin.port_name() + reg + " & " + pin.bit_name() + ") == " + pin.bit_name() + ")"
def bitSet(pin, reg):
return pin.port_name() + reg + " |= " + pin.bit_name()
def bitClr(pin, reg):
return pin.port_name() + reg + " &= ~" + pin.bit_name()
def bitTgl(pin, reg):
return pin.port_name() + reg + " ^= " + pin.bit_name()
class PinDef:
def __init__(self, row):
self.port = row[0][0]
self.pin = row[0][2]
self.af = row[1].strip()
self.direction = row[2]
self.name = row[3]
def port_name(self):
return "P" + self.port
def bit_name(self):
return "BIT" + self.pin
def reg(self, reg):
pass
# Read MSP430 pin configuration from text file and generate macros to access output pins.
#
org = ParseOrg("launchpad.org")
org.parse()
c = Port()
gen.handleExports(c)
spi = Spi()
gen.handleExports(spi)
table = org.items[0].items[0]
tablePins = [PinDef(x) for x in table[1:] if len(x[2]) > 0]
c << OEmptyLine()
for pin in tablePins:
if pin.direction in ["OUT", "IN/OUT"]:
c << OMacro("set_"+pin.name, bitSet(pin, "OUT"))
c << OMacro("clr_"+pin.name, bitClr(pin, "OUT"))
c << OMacro("tgl_"+pin.name, bitTgl(pin, "OUT"))
c << OEmptyLine()
if pin.direction in ["IN/OUT"]:
c << OMacro("get_"+pin.name, bitRead(pin, "IN"))
c << OMacro("out_"+pin.name, bitSet(pin, "DIR"))
c << OMacro("in_"+pin.name, bitClr(pin, "DIR"))
c << OEmptyLine()
for pin in tablePins:
if pin.direction in ["OUT", "IN/OUT"]:
#pdir.append(bname)
c.add(pin.name, bitSet(pin, "OUT"), bitClr(pin, "OUT"))
if pin.direction in ["IN", "IN/OUT"]:
c.add_in(pin.name, bitRead(pin, "IN"))
c.sm.add("PIN_"+pin.name, [bitClr(pin, "DIR") + ";",
"if (out)",
"{",
bitSet(pin, "DIR") + ";",
"}"])
if 1:
for port in ["1", "2"]:
items = [pin.bit_name() for pin in tablePins if pin.port == port and ("OUT" in pin.direction)]
c.m << "P"+port+"DIR = " + ("|".join(items)) + ";"
write_file_n(PATH+"config", c, spi)
if 0:
# Generate some template classes
#
gen_class("ow", [["init", []],
["reset", []],
["read", []],
["_readByte", [Byte('val')]],
["_writeByte", [Byte('val')]],
])
gen_class("hd44", [["init", []],
["clear", []],
["goto", [Int("x"), Int("y")]],
["print", [Int("ch")]],
["_command", [Int("cmd")]],
])