fix write function

This commit is contained in:
Florent Kermarrec 2012-09-13 13:15:05 +02:00
parent 8e86be1a6a
commit 619671ad73
1 changed files with 11 additions and 10 deletions

View File

@ -1,9 +1,10 @@
import string import string
import time import time
import serial import serial
from struct import *
def write_ascii(uart, data): def write_b(uart, data):
uart.write(bytes("%c" % data, encoding="ascii")) uart.write(pack('B',data))
class Uart2Spi: class Uart2Spi:
def __init__(self, port, baudrate): def __init__(self, port, baudrate):
@ -13,20 +14,20 @@ class Uart2Spi:
def read(self, addr): def read(self, addr):
while True: while True:
write_ascii(self.uart, 0x02) write_b(self.uart, 0x02)
write_ascii(self.uart, (addr>>8)&0xFF) write_b(self.uart, (addr>>8)&0xFF)
write_ascii(self.uart, (addr&0xFF)) write_b(self.uart, (addr&0xFF))
write_ascii(self.uart, 0x00) write_b(self.uart, 0x00)
read = self.uart.read() read = self.uart.read()
if len(read) == 1: if len(read) == 1:
break break
return read[0] return read[0]
def write(self, addr, data): def write(self, addr, data):
write_ascii(self.uart, 0x01) write_b(self.uart, 0x01)
write_ascii(self.uart, (addr>>8)&0xFF) write_b(self.uart, (addr>>8)&0xFF)
write_ascii(self.uart, (addr&0xFF)) write_b(self.uart, (addr&0xFF))
write_ascii(self.uart, data) write_b(self.uart, data)
def main(): def main():
csr = Uart2Spi(1,115200) csr = Uart2Spi(1,115200)