From f830389c332d7e67312fc72d534c7a32c94b25f3 Mon Sep 17 00:00:00 2001 From: azeemshatp Date: Wed, 22 Jan 2020 17:53:18 +0000 Subject: [PATCH] Implemented Thingset Serial functionality in Zephyr framework. Implemented using UART driver APIs. --- platformio.ini | 1 + src/ext/serial.cpp | 148 ++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 135 insertions(+), 14 deletions(-) diff --git a/platformio.ini b/platformio.ini index 2a73d5d..f80500b 100644 --- a/platformio.ini +++ b/platformio.ini @@ -18,6 +18,7 @@ default_envs = # pwm-2420-lus-v0.2 # pwm-2420-lus-v0.3 # pwm-2420-lus-v0.3-bl +# pwm-2420-lus-v0.3-zephyr # unit-test-native # Or create your own custom.ini file to overwrite settings in this file, e.g. for board selection. diff --git a/src/ext/serial.cpp b/src/ext/serial.cpp index 954c6ae..23748d2 100644 --- a/src/ext/serial.cpp +++ b/src/ext/serial.cpp @@ -4,39 +4,98 @@ * SPDX-License-Identifier: Apache-2.0 */ -#ifdef __MBED__ +#ifndef UNIT_TEST -#include "mbed.h" #include "config.h" #include "thingset.h" #include "ext.h" +#define SIZE_BUF_REQ 500 + +#ifdef __MBED__ + +#include "mbed.h" + +#elif defined(__ZEPHYR__) + +#include +#include +#include +#include +#include + +#define UART_DEVICE_NAME DT_ALIAS_UART_DBG_LABEL +struct device *uart_dev = device_get_binding(UART_DEVICE_NAME); + +static unsigned char new_line[] = "\n"; + +#endif /* MBED or ZEPHYR */ + class ThingSetStream: public ExtInterface { public: + #ifdef __MBED__ ThingSetStream(Stream& s, const unsigned int c): channel(c), stream(&s) {}; + #elif defined(__ZEPHYR__) + ThingSetStream(const unsigned int c): channel(c){}; + #endif virtual void process_asap(); virtual void process_1s(); protected: + #ifdef __MBED__ virtual void process_input(); // this is called from the ISR typically + #elif defined (__ZEPHYR__) + static void process_input(void*); + #endif + const unsigned int channel; + #ifdef __MBED__ virtual bool readable() { return stream->readable(); } - private: - Stream* stream; + #elif defined (__ZEPHYR__) + static bool readable() + { + // Start processing interrupts in ISR. + // This function should be called the first thing in the ISR. + if (!uart_irq_update(uart_dev)) { + // The function output should always be 1. + return false; + } + // Check if data is available to read. + return uart_irq_rx_ready(uart_dev); + } - static char buf_resp[1000]; // only one response buffer needed for all objects - char buf_req[500]; + typedef struct { + char *buf_req_ptr; + size_t *req_pos_ptr; + bool *command_flag_ptr; + } DataStreamStruct; + + // Data structure used to pass member variables into a static callback ISR. + // Callback registration requires the function to be static. + // But static functions dont have access to the class members. + // Hence these members are passed as a structure of pointers. + DataStreamStruct data_stream = {buf_req, &req_pos, &command_flag}; + #endif /* MBED */ + + private: + #ifdef __MBED__ + Stream* stream; + #endif /* MBED */ + + char buf_resp[1000]; + char buf_req[SIZE_BUF_REQ]; size_t req_pos = 0; bool command_flag = false; }; +#ifdef __MBED__ template class ThingSetSerial: public ThingSetStream { public: @@ -57,6 +116,24 @@ template class ThingSetSerial: public ThingSetStream T& ser; }; +#elif defined(__ZEPHYR__) + +class ThingSetSerial: public ThingSetStream +{ + public: + ThingSetSerial(const int c): ThingSetStream(c){} + + void enable() { + /* Configure UART ISR: Configure a Callback function */ + // Callback function to be given access to the class members using the below API. + // These members are accessed by the ISR using a pointer to the data structure. + uart_irq_callback_user_data_set(uart_dev, process_input, (void*)&data_stream); + + /* Enable Rx interrupt in the IER */ + uart_irq_rx_enable(uart_dev); + } +}; +#endif /* MBED or ZEPHYR*/ /* * Construct all global ExtInterfaces here. @@ -66,23 +143,36 @@ template class ThingSetSerial: public ThingSetStream #ifdef UART_SERIAL_ENABLED extern const int pub_channel_serial; - extern Serial serial; + #ifdef __MBED__ + extern Serial serial; + ThingSetSerial ts_uart(serial, pub_channel_serial); + + #elif defined(__ZEPHYR__) + ThingSetSerial ts_uart(pub_channel_serial); + #endif /* MBED or ZEPHYR */ - ThingSetSerial ts_uart(serial, pub_channel_serial); #endif /* UART_SERIAL_ENABLED */ // ToDo: Add USB serial again (previous library was broken with recent mbed releases) -char ThingSetStream::buf_resp[1000]; - extern ThingSet ts; void ThingSetStream::process_1s() { if (ts.get_pub_channel(channel)->enabled) { + #ifdef __MBED__ ts.pub_msg_json(buf_resp, sizeof(buf_resp), channel); stream->puts(buf_resp); stream->putc('\n'); + + #elif defined(__ZEPHYR__) + int len = ts.pub_msg_json(buf_resp, sizeof(buf_resp), channel); + for (int i = 0; i < len; i++) { + uart_poll_out(uart_dev, buf_resp[i]); + } + uart_poll_out(uart_dev, new_line[0]); + + #endif /* MBED or ZREPHYR */ } } @@ -91,11 +181,21 @@ void ThingSetStream::process_asap() if (command_flag) { // commands must have 2 or more characters if (req_pos > 1) { + #ifdef __MBED__ stream->printf("Received Request (%d bytes): %s\n", strlen(buf_req), buf_req); ts.process((uint8_t *)buf_req, strlen(buf_req), (uint8_t *)buf_resp, sizeof(buf_resp)); stream->puts(buf_resp); stream->putc('\n'); fflush(*stream); + + #elif defined(__ZEPHYR__) + printf("Received Request (%d bytes): %s\n", strlen(buf_req), buf_req); + int len = ts.process((uint8_t *)buf_req, strlen(buf_req), (uint8_t *)buf_resp, sizeof(buf_resp)); + for (int i = 0; i < len; i++) { + uart_poll_out(uart_dev, buf_resp[i]); + } + uart_poll_out(uart_dev, new_line[0]); + #endif /* MBED or ZEPHYR */ } // start listening for new commands @@ -108,11 +208,26 @@ void ThingSetStream::process_asap() * Read characters from stream until line end \n detected, signal command available then * and wait for processing */ +#ifdef __MBED__ void ThingSetStream::process_input() +#elif defined (__ZEPHYR__) +void ThingSetStream::process_input(void* user_data) +#endif /* MBED or ZEPHYR */ { - while (readable() && command_flag == false) { + #ifdef __ZEPHYR__ + u8_t c; + DataStreamStruct *data_stream_ptr = (DataStreamStruct *)user_data; + bool command_flag = *(data_stream_ptr->command_flag_ptr); + char *buf_req = data_stream_ptr->buf_req_ptr; + size_t req_pos = *(data_stream_ptr->req_pos_ptr); + #endif /* __ZEPHYR__ */ + while (readable() && command_flag == false) { + #ifdef __MBED__ int c = stream->getc(); + #elif defined(__ZEPHYR__) + uart_fifo_read(uart_dev, &c, 1); + #endif /* MBED */ // \r\n and \n are markers for line end, i.e. command end // we accept this at any time, even if the buffer is 'full', since @@ -127,7 +242,6 @@ void ThingSetStream::process_input() // start processing command_flag = true; } - // backspace // can be done always unless there is nothing in the buffer else if (req_pos > 0 && c == '\b') { @@ -137,9 +251,15 @@ void ThingSetStream::process_input() // the last character is reserved for '\0' // more read characters are simply dropped, unless it is \n // which ends the command input and triggers processing - else if (req_pos < (sizeof(buf_req)-1)) { + // else if (req_pos < (sizeof(buf_req)-1)) { + else if (req_pos < (SIZE_BUF_REQ-1)) { buf_req[req_pos++] = c; } + + #ifdef __ZEPHYR__ + *(data_stream_ptr->command_flag_ptr) = command_flag; + *(data_stream_ptr->req_pos_ptr) = req_pos; + #endif } } -#endif /* __MBED__ */ +#endif /* UNIT_TEST */