diff --git a/.gitignore b/.gitignore index 8f4c3bc..2b8b572 100644 --- a/.gitignore +++ b/.gitignore @@ -25,6 +25,7 @@ packages/vsky/libdsp/driver/Uart.h packages/vsky/libdsp/lib packages/vsky/libdsp/Settings.h packages/vsky/libdsp/Uart.h +packages/vsky/libdsp/Serial.h packages/vsky/libdsp/SerialSystem.h packages/vsky/libdsp/Interrupt.h packages/vsky/libdsp/driver/cfi_flash.orig.c diff --git a/packages/vsky/libdsp/Serial.c b/packages/vsky/libdsp/Serial.c new file mode 100644 index 0000000..203e41f --- /dev/null +++ b/packages/vsky/libdsp/Serial.c @@ -0,0 +1,46 @@ +#include +#include +#include "inc/uart.h" +#include "package/internal/Serial.xdc.h" + +Int Serial_Instance_init(Serial_Object *obj, Int p, const Serial_Params *param, Error_Block *eb) +{ + obj->_uart = uart_open(p); + if (obj->_uart == NULL) { + if (eb) { + Error_raise(eb, Error_E_generic, 0, 0); + } + return -1; + } + uart_set_baudrate(obj->_uart, param->baudrate); + uart_set_databits(obj->_uart, param->databits); + uart_set_stopbits(obj->_uart, param->stopbits); + return 0; +} + +void Serial_Instance_finalize(Serial_Object *obj, Int state) +{ + if (state == 0) { + uart_close(obj->_uart); + } +} + +UInt8 Serial_Instance_read(Serial_Object *obj) +{ + return uart_read(obj->_uart); +} + +void Serial_Instance_write(Serial_Object *obj, UInt8 ch) +{ + uart_write(obj->_uart, ch); +} + +Bool Serial_Instance_isReady(Serial_Object *obj) +{ + return uart_is_ready(obj->_uart); +} + +Ptr Serial_Instance_uart(Serial_Object *obj) +{ + return obj->_uart; +} diff --git a/packages/vsky/libdsp/Serial.xdc b/packages/vsky/libdsp/Serial.xdc new file mode 100644 index 0000000..b931a88 --- /dev/null +++ b/packages/vsky/libdsp/Serial.xdc @@ -0,0 +1,52 @@ +@InstanceFinalize +@InstanceInitError +module Serial { + /* + * Serial Port driver. + */ +instance: + config UInt32 baudrate = 115200; + config UInt32 databits = 8; + config UInt32 stopbits = 1; + + /*! + * Create serial port. + * + * @p(html) + * Serial_Handle h; + * Serial_Params p; + * Serial_Params_init(&p); + * + * h = Serial_create(0, &p); + * if (h != NULL) { + * ... + * } + * @p + */ + create(Int port); + + /*! + * read a byte from serial. + */ + UInt8 read(); + + /*! + * write a byte to serial. + */ + void write(UInt8 ch); + + /*! + * data is comming. + * + Bool isReady(); + + /*! + * get the lowlevel uart handle. + */ + Ptr uart(); + +internal: + @Opaque struct Instance_State { + Ptr _uart; + }; +} diff --git a/packages/vsky/libdsp/Serial.xs b/packages/vsky/libdsp/Serial.xs new file mode 100644 index 0000000..75a60d3 --- /dev/null +++ b/packages/vsky/libdsp/Serial.xs @@ -0,0 +1,4 @@ +function module$use() +{ + xdc.useModule('xdc.runtime.Error'); +} \ No newline at end of file diff --git a/packages/vsky/libdsp/driver/uart.cpp b/packages/vsky/libdsp/driver/uart.cpp index 8f1fc5a..e30a856 100644 --- a/packages/vsky/libdsp/driver/uart.cpp +++ b/packages/vsky/libdsp/driver/uart.cpp @@ -21,6 +21,14 @@ struct Uart : public UartImpl { return 0; } + void reset() + { + _baudrate = 115200; + _databits = DATA_BITS_8; + _stopbits = STOP_BITS_1; + _parity = PARITY_NONE; + } + void set_baudrate(int rate) { this->setBaudrate(rate); @@ -87,6 +95,11 @@ uart_t uart_open(int bus) return _uart; } +void uart_close(uart_t _uart) +{ + _uart->reset(); +} + void uart_set_baudrate(uart_t _uart, int rate) { _uart->set_baudrate(rate); diff --git a/packages/vsky/libdsp/inc/uart.h b/packages/vsky/libdsp/inc/uart.h index cbc5644..dd2c52d 100644 --- a/packages/vsky/libdsp/inc/uart.h +++ b/packages/vsky/libdsp/inc/uart.h @@ -21,6 +21,8 @@ typedef struct Uart * uart_t; uart_t uart_open(int uart); +void uart_close(uart_t _uart); + /* data in */ int uart_is_ready(uart_t); diff --git a/packages/vsky/libdsp/package.bld b/packages/vsky/libdsp/package.bld index f96f08d..14aecca 100644 --- a/packages/vsky/libdsp/package.bld +++ b/packages/vsky/libdsp/package.bld @@ -9,6 +9,7 @@ Pkg.otherFiles = [ "GPIO.xdc", "Board.xdc", "board.xdt", + "Serial.xdc", ]; var drvFiles = [ @@ -35,6 +36,7 @@ var emacFiles = [ var xdcFiles = [ "Board.c", + "Serial.c", "SerialSystem.c", "NorFlash.c", "GPIO.c", diff --git a/packages/vsky/libdsp/package.xdc b/packages/vsky/libdsp/package.xdc index f0aa3ee..ec89256 100644 --- a/packages/vsky/libdsp/package.xdc +++ b/packages/vsky/libdsp/package.xdc @@ -8,6 +8,7 @@ */ package vsky.libdsp [0, 9, 9, 0] { module Settings; + module Serial; module SerialSystem; module NorFlash; module GPIO;