2017-11-08 02:36:35 +00:00
|
|
|
import io
|
|
|
|
|
|
|
|
const portCOM1 = 0x3f8'u16
|
|
|
|
|
|
|
|
proc init*() =
|
|
|
|
io.outb((portCOM1 + 1, 0x00'u8)); # Disable all interrupts
|
|
|
|
io.outb((portCOM1 + 3, 0x80'u8)); # Enable DLAB (set baud rate divisor)
|
|
|
|
io.outb((portCOM1 + 0, 0x03'u8)); # Set divisor to 3 (lo byte) 38400 baud
|
|
|
|
io.outb((portCOM1 + 1, 0x00'u8)); # (hi byte)
|
|
|
|
io.outb((portCOM1 + 3, 0x03'u8)); # 8 bits, no parity, one stop bit
|
|
|
|
io.outb((portCOM1 + 2, 0xC7'u8)); # Enable FIFO, clear them, with 14-byte threshold
|
|
|
|
io.outb((portCOM1 + 4, 0x0B'u8)); # IRQs enabled, RTS/DSR set
|
|
|
|
|
|
|
|
proc isTransitEmpty(): uint =
|
|
|
|
result = io.inb(portCOM1 + 5) and 0x20'u
|
|
|
|
|
|
|
|
proc write*(input: char) =
|
|
|
|
while isTransitEmpty() == 0:
|
|
|
|
discard
|
|
|
|
io.outb((portCOM1, uint8(input)))
|
|
|
|
|
|
|
|
proc write*(input: string) =
|
|
|
|
for i in 0 .. input.len - 1:
|
2018-04-28 22:22:05 +01:00
|
|
|
serial.write(input[i])
|
2017-11-08 02:36:35 +00:00
|
|
|
|
2017-11-09 23:21:18 +00:00
|
|
|
var parsedData: array[0..9, char]
|
|
|
|
|
|
|
|
proc write*(data: SomeSignedInt) =
|
|
|
|
if(data == 0):
|
2018-04-28 22:22:05 +01:00
|
|
|
serial.write('0')
|
2017-11-09 23:21:18 +00:00
|
|
|
return
|
|
|
|
var input = data
|
|
|
|
var i = 0
|
|
|
|
if(input < 0):
|
2018-04-28 22:22:05 +01:00
|
|
|
serial.write('-')
|
2017-11-09 23:21:18 +00:00
|
|
|
input = input * -1
|
|
|
|
while(input != 0):
|
|
|
|
let parsedChar: char = char(48 + (input mod 10))
|
|
|
|
parsedData[i] = parsedChar
|
|
|
|
i = i + 1
|
|
|
|
input = input div 10
|
|
|
|
while i > 0:
|
|
|
|
i = i - 1
|
2018-04-28 22:22:05 +01:00
|
|
|
serial.write(parsedData[i])
|
2017-11-09 23:21:18 +00:00
|
|
|
|
|
|
|
proc write*(data: SomeUnsignedInt) =
|
|
|
|
if(data == 0):
|
2018-04-28 22:22:05 +01:00
|
|
|
serial.write('0')
|
2017-11-09 23:21:18 +00:00
|
|
|
return
|
|
|
|
var input = data
|
|
|
|
var i = 0
|
|
|
|
while(input != 0):
|
|
|
|
let parsedChar: char = char(48'u + (input mod 10))
|
|
|
|
parsedData[i] = parsedChar
|
|
|
|
i = i + 1
|
|
|
|
input = input div 10
|
|
|
|
while i > 0:
|
|
|
|
i = i - 1
|
2018-04-28 22:22:05 +01:00
|
|
|
serial.write(parsedData[i])
|
2017-11-09 23:21:18 +00:00
|
|
|
|
|
|
|
proc newLine*() = write('\L')
|
2018-04-28 22:22:05 +01:00
|
|
|
|
|
|
|
proc writeLine*(data: any) =
|
|
|
|
serial.write(data)
|
|
|
|
newLine()
|