add the NxScope library - a real-time data logging tool

This commit is contained in:
raiden00pl 2022-12-07 15:51:53 +01:00 committed by Xiang Xiao
parent ea7241a1a6
commit cd499f9d37
18 changed files with 4893 additions and 0 deletions

View File

@ -0,0 +1,488 @@
/****************************************************************************
* apps/include/logging/nxscope/nxscope.h
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
#ifndef __APPS_INCLUDE_LOGGING_NXSCOPE_NXSCOPE_H
#define __APPS_INCLUDE_LOGGING_NXSCOPE_NXSCOPE_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <nuttx/compiler.h>
#include <pthread.h>
#include <stdint.h>
#include <logging/nxscope/nxscope_chan.h>
#include <logging/nxscope/nxscope_intf.h>
#include <logging/nxscope/nxscope_proto.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define NXSCOPE_ENABLE_LEN (sizeof(struct nxscope_enable_data_s))
#define NXSCOPE_DIV_LEN (sizeof(struct nxscope_div_data_s))
#define NXSCOPE_START_LEN (sizeof(struct nxscope_start_data_s))
/* MSB bit in the channel type means a critical channel */
#define NXSCOPE_IS_CRICHAN(chtype) (chtype & 0x80)
/****************************************************************************
* Public Types
****************************************************************************/
/* Nxscope header ID */
enum nxscope_hdr_id_e
{
NXSCOPE_HDRID_UNDEF = 0, /* Reserved */
/* Stream frames */
NXSCOPE_HDRID_STREAM = 1, /* Stream data */
/* Get frames */
NXSCOPE_HDRID_CMNINFO = 2, /* Get nxscope common info */
NXSCOPE_HDRID_CHINFO = 3, /* Get nxscope channel info */
/* Special frames */
NXSCOPE_HDRID_ACK = 4, /* ACK/NACK */
/* Set frames.
*
* If CONFIG_LOGGING_NXSCOPE_ACKFRAMES=y this requests must
* be confirmed with ACK frames to report success or failure.
*/
NXSCOPE_HDRID_START = 5, /* Start/stop stream.
* NOTE: this frame do not follow
* 'struct nxscope_set_frame_s' format
*/
NXSCOPE_HDRID_ENABLE = 6, /* Snable/disable channels */
NXSCOPE_HDRID_DIV = 7, /* Channels divider */
/* User defined frames.
* Must be alway the last element.
*/
NXSCOPE_HDRID_USER = 8
};
/* Nxscope flags */
enum nxscope_info_flags_e
{
NXSCOPE_FLAGS_DIVIDER_SUPPORT = (1 << 0),
NXSCOPE_FLAGS_ACK_SUPPORT = (1 << 1),
NXSCOPE_FLAGS_RES2 = (1 << 2),
NXSCOPE_FLAGS_RES3 = (1 << 3),
NXSCOPE_FLAGS_RES4 = (1 << 4),
NXSCOPE_FLAGS_RES5 = (1 << 5),
NXSCOPE_FLAGS_RES6 = (1 << 6),
NXSCOPE_FLAGS_RES7 = (1 << 7),
};
/* Nxscope stream flags */
enum nxscope_stream_flags_s
{
NXSCOPE_STREAM_FLAGS_OVERFLOW = (1 << 0)
};
/* Nxscope start frame data */
begin_packed_struct struct nxscope_start_data_s
{
uint8_t start; /* Start/stop flag */
} end_packed_struct;
/* Nxscope enable channel data */
begin_packed_struct struct nxscope_enable_ch_data_s
{
uint8_t en; /* Channel enable */
} end_packed_struct;
/* Nxscope divider channel data */
begin_packed_struct struct nxscope_div_ch_data_s
{
uint8_t div; /* Channel divider - starts from 0 */
} end_packed_struct;
/* Nxscope enable frame data */
begin_packed_struct struct nxscope_enable_data_s
{
struct nxscope_enable_ch_data_s ch[1]; /* Chmax elements */
} end_packed_struct;
/* Nxscope divider frame data */
begin_packed_struct struct nxscope_div_data_s
{
struct nxscope_div_ch_data_s ch[1]; /* Chmax elements */
} end_packed_struct;
/* Set channel frame */
enum nxscope_set_frame_req_s
{
NXSCOPE_SET_REQ_SINGLE = 0, /* Single channel request */
NXSCOPE_SET_REQ_BULK = 1, /* Set different values for all channels */
NXSCOPE_SET_REQ_ALL = 2 /* Set one value for all channles */
};
/* Set frame common data */
begin_packed_struct struct nxscope_set_frame_s
{
uint8_t req; /* Request type */
uint8_t chan; /* Channel id */
uint8_t data[1]; /* n bytes data - depends on the request type */
} end_packed_struct;
/* Chanel type */
begin_packed_struct struct nxscope_chinfo_type_s
{
uint8_t dtype:5; /* Data type */
uint8_t _res:2; /* Reserved for future use */
uint8_t cri:1; /* Criticial channel - no buffering */
} end_packed_struct;
/* Chanel type union */
union nxscope_chinfo_type_u
{
struct nxscope_chinfo_type_s s;
uint8_t u8;
};
/* Nxscope channel info */
begin_packed_struct struct nxscope_chinfo_s
{
uint8_t enable; /* Enable flag */
union nxscope_chinfo_type_u type; /* Channel data type */
uint8_t vdim; /* Vector dimention */
uint8_t div; /* Divider - starts from 0 */
uint8_t mlen; /* Metadata size */
FAR char *name; /* Chanel name */
} end_packed_struct;
/* Nxscope info common */
begin_packed_struct struct nxscope_info_cmn_s
{
uint8_t chmax; /* Supported channels */
uint8_t flags; /* Flags (enum nxscope_info_flags_e) */
uint8_t rx_padding; /* RX padding (>0 if used) */
} end_packed_struct;
/* Nxscope sample hdr:
*
* +----------+-------------+----------+
* | channel | sample data | metadata |
* +----------+-------------+----------+
* | 1B | n bytes [1] | m bytes |
* +----------+-------------+----------+
*
* [1] - sizeof(channel_type) * channel_vdim
* NOTE: sample data always little-endian !
*
*/
struct nxscope_sample_s
{
uint8_t chan; /* 1 byte: Channel id - starts from 0 */
/* n bytes: Data */
/* m bytes: Metadata */
};
/* Nxscope stream data:
*
* +----------+--------------+
* | flags | samples data |
* +----------+--------------+
* | 1B | n bytes |
* +----------+--------------+
*
*/
struct nxscope_stream_s
{
uint8_t flags; /* stream flags */
struct nxscope_sample_s samples[1]; /* stream samples */
};
/* Nxscope callbacks */
struct nxscope_callbacks_s
{
/* User-defined id callback */
FAR void *userid_priv;
CODE int (*userid)(FAR void *priv, uint8_t id, FAR uint8_t *buff);
/* Start request callback */
FAR void *start_priv;
CODE int (*start)(FAR void *priv, bool start);
};
/* Nxscope general configuration */
struct nxscope_cfg_s
{
/* NOTE: It is possible to configure separate interface
* and protocol for control commands and data stream.
*/
/* Interface implementation for commands */
FAR struct nxscope_intf_s *intf_cmd;
/* Interface implementation for stream data */
FAR struct nxscope_intf_s *intf_stream;
/* Protocol implementation for commands */
FAR struct nxscope_proto_s *proto_cmd;
/* Protocol implementation for stream data */
FAR struct nxscope_proto_s *proto_stream;
/* Callbacks */
FAR struct nxscope_callbacks_s *callbacks;
/* Number of suppoted channels */
uint8_t channels;
/* Stream buffer len */
size_t streambuf_len;
/* RX buffer len */
size_t rxbuf_len;
#ifdef CONFIG_LOGGING_NXSCOPE_CRICHANNELS
/* Critical buffer len.
*
* It's the user's responsibility to correctly choose this value.
* The minimal buffer size for critical channels can be calculate
* with this formula:
* buff_size = max(proto_stream->hdrlen + 1 + type_size * vdim +
* meta_len + proto_stream->footlen)
*
* where max() means the maximum value from all initialized critical
* channels.
*
* When CONFIG_DEBUG_FEATURES is enabled the correct buffer size is
* verified in run-time.
*/
size_t cribuf_len;
#endif
/* RX padding.
*
* This option will be provided for client in common info data
* and can be useful if we use DMA for receiving frames
*/
uint8_t rx_padding;
};
/* Nxscope data */
struct nxscope_s
{
/* Nxscope interface handlers */
FAR struct nxscope_intf_s *intf_cmd;
FAR struct nxscope_intf_s *intf_stream;
/* Nxscope protocol handlers */
FAR struct nxscope_proto_s *proto_cmd;
FAR struct nxscope_proto_s *proto_stream;
/* Callbacks */
FAR struct nxscope_callbacks_s *callbacks;
/* Nxscope common info */
struct nxscope_info_cmn_s cmninfo;
/* Channels info, chmax elements */
FAR struct nxscope_chinfo_s *chinfo;
size_t chinfo_size;
/* Nxscope data */
#ifdef CONFIG_LOGGING_NXSCOPE_DIVIDER
FAR uint32_t *cntr;
#endif
uint8_t start;
/* Stream data */
FAR uint8_t *streambuf;
size_t streambuf_len;
size_t stream_i;
#ifdef CONFIG_LOGGING_NXSCOPE_CRICHANNELS
/* Critical buffer data */
FAR uint8_t *cribuf;
size_t cribuf_len;
#endif
/* RX data buffer */
FAR uint8_t *rxbuf;
size_t rxbuf_len;
size_t rxbuf_i;
/* TX data buffer */
FAR uint8_t *txbuf;
size_t txbuf_len;
/* Exclusive access */
pthread_mutex_t lock;
};
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
/****************************************************************************
* Name: nxscope_init
*
* Description:
* Initialize a nxscope instance
*
* Input Parameters:
* s - a pointer to a nxscope instance
* cfg - a pointer to a nxscope configuration data
*
****************************************************************************/
int nxscope_init(FAR struct nxscope_s *s, FAR struct nxscope_cfg_s *cfg);
/****************************************************************************
* Name: nxscope_deinit
*
* Description:
* De-initialize a nxscope instance
*
* Input Parameters:
* s - a pointer to a nxscope instance
*
****************************************************************************/
void nxscope_deinit(FAR struct nxscope_s *s);
/****************************************************************************
* Name: nxscope_lock
*
* Description:
* Lock a nxscope instance
*
* Input Parameters:
* s - a pointer to a nxscope instance
*
****************************************************************************/
void nxscope_lock(FAR struct nxscope_s *s);
/****************************************************************************
* Name: nxscope_unlock
*
* Description:
* Unlock a nxscope instance
*
* Input Parameters:
* s - a pointer to a nxscope instance
*
****************************************************************************/
void nxscope_unlock(FAR struct nxscope_s *s);
/****************************************************************************
* Name: nxscope_stream
*
* Description:
* Send nxscope stream data.
*
* NOTE: It's the user's responsibility to periodically call this function.
*
* Input Parameters:
* s - a pointer to a nxscope instance
*
****************************************************************************/
int nxscope_stream(FAR struct nxscope_s *s);
/****************************************************************************
* Name: nxscope_recv
*
* Description:
* Receive and handle nxscope protocol data.
*
* NOTE: It's the user's responsibility to periodically call this function.
*
* Input Parameters:
* s - a pointer to a nxscope instance
*
****************************************************************************/
int nxscope_recv(FAR struct nxscope_s *s);
/****************************************************************************
* Name: nxscope_stream_start
*
* Description:
* Start/stop data stream
*
* Input Parameters:
* s - a pointer to a nxscope instance
* start - start/stop
*
****************************************************************************/
int nxscope_stream_start(FAR struct nxscope_s *s, bool start);
#endif /* __APPS_INCLUDE_LOGGING_NXSCOPE_NXSCOPE_H */

View File

@ -0,0 +1,419 @@
/****************************************************************************
* apps/include/logging/nxscope/nxscope_chan.h
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
#ifndef __APPS_INCLUDE_LOGGING_NXSCOPE_NXSCOPE_CHAN_H
#define __APPS_INCLUDE_LOGGING_NXSCOPE_NXSCOPE_CHAN_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <fixedmath.h>
#include <logging/nxscope/nxscope.h>
/****************************************************************************
* Public Types
****************************************************************************/
/* Nxscope sample type */
enum nxscope_sample_dtype_e
{
/* Default numerical types */
NXSCOPE_TYPE_UNDEF = 0,
NXSCOPE_TYPE_NONE = 1,
NXSCOPE_TYPE_UINT8 = 2,
NXSCOPE_TYPE_INT8 = 3,
NXSCOPE_TYPE_UINT16 = 4,
NXSCOPE_TYPE_INT16 = 5,
NXSCOPE_TYPE_UINT32 = 6,
NXSCOPE_TYPE_INT32 = 7,
NXSCOPE_TYPE_UINT64 = 8,
NXSCOPE_TYPE_INT64 = 9,
NXSCOPE_TYPE_FLOAT = 10,
NXSCOPE_TYPE_DOUBLE = 11,
NXSCOPE_TYPE_UB8 = 12,
NXSCOPE_TYPE_B8 = 13,
NXSCOPE_TYPE_UB16 = 14,
NXSCOPE_TYPE_B16 = 15,
NXSCOPE_TYPE_UB32 = 16,
NXSCOPE_TYPE_B32 = 17,
/* Char/string data */
NXSCOPE_TYPE_CHAR = 18,
#if 0
/* Reserved for future use */
NXSCOPE_TYPE_WCHAR = 19,
#endif
#ifdef CONFIG_LOGGING_NXSCOPE_USERTYPES
/* User defined types starts from here.
* NXSCOPE_TYPE_USER must be always the last element.
*
* Type of size 1B. Together with channel.vdim this
* can be used to send custom protocol data.
*/
NXSCOPE_TYPE_USER = 20,
#endif
/* 5 bits reserved for data type */
NXSCOPE_TYPE_LAST = 31,
};
/* Forward declaration */
struct nxscope_s;
/****************************************************************************
* Public Function Puttypes
****************************************************************************/
/****************************************************************************
* Name: nxscope_chan_init
*
* Description:
* Initialize nxscope channel
*
* Input Parameters:
* s - a pointer to a nxscope instance
* ch - a channel id
* name - a channel name
* type - a channel data type (union nxscope_chinfo_type_u)
* vdim - a vector data dimension (vdim=1 for a point)
* mlen - a length of metadata
*
****************************************************************************/
int nxscope_chan_init(FAR struct nxscope_s *s, uint8_t ch,
FAR char *name, uint8_t type, uint8_t vdim,
uint8_t mlen);
/****************************************************************************
* Name: nxscope_chan_en
*
* Description:
* Enable/disable a given channel
*
* Input Parameters:
* s - a pointer to a nxscope instance
* ch - a channel id
* en - enable/disable
*
****************************************************************************/
int nxscope_chan_en(FAR struct nxscope_s *s, uint8_t chan, bool en);
#ifdef CONFIG_LOGGING_NXSCOPE_DIVIDER
/****************************************************************************
* Name: nxscope_chan_div
*
* Description:
* Configure divider for a given channel
*
* Input Parameters:
* s - a pointer to a nxscope instance
* ch - a channel id
* div - divider value - starts from 0
*
****************************************************************************/
int nxscope_chan_div(FAR struct nxscope_s *s, uint8_t chan, uint8_t div);
#endif
/****************************************************************************
* Name: nxscope_chan_all_en
*
* Description:
* Enable/disable all channels
*
* Input Parameters:
* s - a pointer to a nxscope instance
* en - enable/disable
*
****************************************************************************/
int nxscope_chan_all_en(FAR struct nxscope_s *s, bool en);
/****************************************************************************
* Name: nxscope_put_vXXXX_m
*
* Description:
* Put a vector with metadata on the stream buffer
*
* Input Parameters:
* s - a pointer to a nxscope instance
* ch - a channel id
* val - a pointer to a sample data vector
* d - a dimmention of sample data vector
* meta - a pointer to metadata
* mlen - a length of metadata
*
****************************************************************************/
int nxscope_put_vuint8_m(FAR struct nxscope_s *s, uint8_t ch,
FAR uint8_t *val, uint8_t d,
FAR uint8_t *meta, uint8_t mlen);
int nxscope_put_vint8_m(FAR struct nxscope_s *s, uint8_t ch,
FAR int8_t *val, uint8_t d,
FAR uint8_t *meta, uint8_t mlen);
int nxscope_put_vuint16_m(FAR struct nxscope_s *s, uint8_t ch,
FAR uint16_t *val, uint8_t d,
FAR uint8_t *meta, uint8_t mlen);
int nxscope_put_vint16_m(FAR struct nxscope_s *s, uint8_t ch,
FAR int16_t *val, uint8_t d,
FAR uint8_t *meta, uint8_t mlen);
int nxscope_put_vuint32_m(FAR struct nxscope_s *s, uint8_t ch,
FAR uint32_t *val, uint8_t d,
FAR uint8_t *meta, uint8_t mlen);
int nxscope_put_vint32_m(FAR struct nxscope_s *s, uint8_t ch,
FAR int32_t *val, uint8_t d,
FAR uint8_t *meta, uint8_t mlen);
int nxscope_put_vuint64_m(FAR struct nxscope_s *s, uint8_t ch,
FAR uint64_t *val, uint8_t d,
FAR uint8_t *meta, uint8_t mlen);
int nxscope_put_vint64_m(FAR struct nxscope_s *s, uint8_t ch,
FAR int64_t *val, uint8_t d,
FAR uint8_t *meta, uint8_t mlen);
int nxscope_put_vfloat_m(FAR struct nxscope_s *s, uint8_t ch,
FAR float *val, uint8_t d,
FAR uint8_t *meta, uint8_t mlen);
int nxscope_put_vdouble_m(FAR struct nxscope_s *s, uint8_t ch,
FAR double *val, uint8_t d,
FAR uint8_t *meta, uint8_t mlen);
int nxscope_put_vub8_m(FAR struct nxscope_s *s, uint8_t ch,
FAR ub8_t *val, uint8_t d,
FAR uint8_t *meta, uint8_t mlen);
int nxscope_put_vb8_m(FAR struct nxscope_s *s, uint8_t ch,
FAR b8_t *val, uint8_t d,
FAR uint8_t *meta, uint8_t mlen);
int nxscope_put_vub16_m(FAR struct nxscope_s *s, uint8_t ch,
FAR ub16_t *val, uint8_t d,
FAR uint8_t *meta, uint8_t mlen);
int nxscope_put_vb16_m(FAR struct nxscope_s *s, uint8_t ch,
FAR b16_t *val, uint8_t d,
FAR uint8_t *meta, uint8_t mlen);
int nxscope_put_vub32_m(FAR struct nxscope_s *s, uint8_t ch,
FAR ub32_t *val, uint8_t d,
FAR uint8_t *meta, uint8_t mlen);
int nxscope_put_vb32_m(FAR struct nxscope_s *s, uint8_t ch,
FAR b32_t *val, uint8_t d,
FAR uint8_t *meta, uint8_t mlen);
/****************************************************************************
* Name: nxscope_put_vchar_m
*
* NOTE: if a given string is shorten than initialized channel vdim,
* we put only string bytes + '\0'
*
****************************************************************************/
int nxscope_put_vchar_m(FAR struct nxscope_s *s, uint8_t ch,
FAR char *val, uint8_t d,
FAR uint8_t *meta, uint8_t mlen);
/****************************************************************************
* Name: nxscope_put_vXXXX
*
* Description:
* Put a vector on the stream buffer
*
* Input Parameters:
* s - a pointer to a nxscope instance
* ch - a channel id
* val - a pointer to a sample data vector
* d - a dimmention of sample data vector
*
****************************************************************************/
int nxscope_put_vuint8(FAR struct nxscope_s *s, uint8_t ch,
FAR uint8_t *val, uint8_t d);
int nxscope_put_vint8(FAR struct nxscope_s *s, uint8_t ch,
FAR int8_t *val, uint8_t d);
int nxscope_put_vuint16(FAR struct nxscope_s *s, uint8_t ch,
FAR uint16_t *val, uint8_t d);
int nxscope_put_vint16(FAR struct nxscope_s *s, uint8_t ch,
FAR int16_t *val, uint8_t d);
int nxscope_put_vuint32(FAR struct nxscope_s *s, uint8_t ch,
FAR uint32_t *val, uint8_t d);
int nxscope_put_vint32(FAR struct nxscope_s *s, uint8_t ch,
FAR int32_t *val, uint8_t d);
int nxscope_put_vuint64(FAR struct nxscope_s *s, uint8_t ch,
FAR uint64_t *val, uint8_t d);
int nxscope_put_vint64(FAR struct nxscope_s *s, uint8_t ch,
FAR int64_t *val, uint8_t d);
int nxscope_put_vfloat(FAR struct nxscope_s *s, uint8_t ch,
FAR float *val, uint8_t d);
int nxscope_put_vdouble(FAR struct nxscope_s *s, uint8_t ch,
FAR double *val, uint8_t d);
int nxscope_put_vub8(FAR struct nxscope_s *s, uint8_t ch,
FAR ub8_t *val, uint8_t d);
int nxscope_put_vb8(FAR struct nxscope_s *s, uint8_t ch,
FAR b8_t *val, uint8_t d);
int nxscope_put_vub16(FAR struct nxscope_s *s, uint8_t ch,
FAR ub16_t *val, uint8_t d);
int nxscope_put_vb16(FAR struct nxscope_s *s, uint8_t ch,
FAR b16_t *val, uint8_t d);
int nxscope_put_vub32(FAR struct nxscope_s *s, uint8_t ch,
FAR ub32_t *val, uint8_t d);
int nxscope_put_vb32(FAR struct nxscope_s *s, uint8_t ch,
FAR b32_t *val, uint8_t d);
/****************************************************************************
* Name: nxscope_put_vchar
*
* NOTE: if a given string is shorten than initialized channel vdim,
* we put only string bytes + '\0'
*
****************************************************************************/
int nxscope_put_vchar(FAR struct nxscope_s *s, uint8_t ch,
FAR char *val, uint8_t d);
/****************************************************************************
* Name: nxscope_put_XXXX_m
*
* Description:
* Put a point with metadata on the stream buffer
*
* Input Parameters:
* s - a pointer to a nxscope instance
* ch - a channel id
* val - sample data
* meta - a pointer to metadata
* mlen - a length of metadata
*
****************************************************************************/
int nxscope_put_uint8_m(FAR struct nxscope_s *s, uint8_t ch, uint8_t val,
FAR uint8_t *meta, uint8_t mlen);
int nxscope_put_int8_m(FAR struct nxscope_s *s, uint8_t ch, int8_t val,
FAR uint8_t *meta, uint8_t mlen);
int nxscope_put_uint16_m(FAR struct nxscope_s *s, uint8_t ch, uint16_t val,
FAR uint8_t *meta, uint8_t mlen);
int nxscope_put_int16_m(FAR struct nxscope_s *s, uint8_t ch, int16_t val,
FAR uint8_t *meta, uint8_t mlen);
int nxscope_put_uint32_m(FAR struct nxscope_s *s, uint8_t ch, uint32_t val,
FAR uint8_t *meta, uint8_t mlen);
int nxscope_put_int32_m(FAR struct nxscope_s *s, uint8_t ch, int32_t val,
FAR uint8_t *meta, uint8_t mlen);
int nxscope_put_uint64_m(FAR struct nxscope_s *s, uint8_t ch, uint64_t val,
FAR uint8_t *meta, uint8_t mlen);
int nxscope_put_int64_m(FAR struct nxscope_s *s, uint8_t ch, int64_t val,
FAR uint8_t *meta, uint8_t mlen);
int nxscope_put_float_m(FAR struct nxscope_s *s, uint8_t ch, float val,
FAR uint8_t *meta, uint8_t mlen);
int nxscope_put_double_m(FAR struct nxscope_s *s, uint8_t ch, double val,
FAR uint8_t *meta, uint8_t mlen);
int nxscope_put_ub8_m(FAR struct nxscope_s *s, uint8_t ch, ub8_t val,
FAR uint8_t *meta, uint8_t mlen);
int nxscope_put_b8_m(FAR struct nxscope_s *s, uint8_t ch, b8_t val,
FAR uint8_t *meta, uint8_t mlen);
int nxscope_put_ub16_m(FAR struct nxscope_s *s, uint8_t ch, ub16_t val,
FAR uint8_t *meta, uint8_t mlen);
int nxscope_put_b16_m(FAR struct nxscope_s *s, uint8_t ch, b16_t val,
FAR uint8_t *meta, uint8_t mlen);
int nxscope_put_ub32_m(FAR struct nxscope_s *s, uint8_t ch, ub32_t val,
FAR uint8_t *meta, uint8_t mlen);
int nxscope_put_b32_m(FAR struct nxscope_s *s, uint8_t ch, b32_t val,
FAR uint8_t *meta, uint8_t mlen);
int nxscope_put_char_m(FAR struct nxscope_s *s, uint8_t ch, char val,
FAR uint8_t *meta, uint8_t mlen);
/****************************************************************************
* Name: nxscope_put_none_m
*
* Description:
* Put metadata only on the stream buffer
*
* Input Parameters:
* s - a pointer to a nxscope instance
* ch - a channel id
* meta - a pointer to metadata
* mlen - a length of metadata
*
****************************************************************************/
int nxscope_put_none_m(FAR struct nxscope_s *s, uint8_t ch,
FAR uint8_t *meta, uint8_t mlen);
#ifdef CONFIG_LOGGING_NXSCOPE_USERTYPES
/****************************************************************************
* Name: nxscope_put_user_m
*
* Description:
* Put a user specific data on the stream buffer
*
* Input Parameters:
* s - a pointer to a nxscope instance
* type - a channel type (starts from NXSCOPE_TYPE_USER)
* ch - a channel id
* val - a pointer to a sample data vector
* d - a dimmention of sample data vector
* meta - a pointer to metadata
* mlen - a length of metadata
*
****************************************************************************/
int nxscope_put_user_m(FAR struct nxscope_s *s, uint8_t ch, uint8_t type,
FAR uint8_t *val, uint8_t d,
FAR uint8_t *meta, uint8_t mlen);
#endif
/****************************************************************************
* Name: nxscope_put_user_m
*
* Description:
* Put a point on the stream buffer
*
* Input Parameters:
* s - a pointer to a nxscope instance
* ch - a channel id
* val - sample data
*
****************************************************************************/
int nxscope_put_uint8(FAR struct nxscope_s *s, uint8_t ch, uint8_t val);
int nxscope_put_int8(FAR struct nxscope_s *s, uint8_t ch, int8_t val);
int nxscope_put_uint16(FAR struct nxscope_s *s, uint8_t ch, uint16_t val);
int nxscope_put_int16(FAR struct nxscope_s *s, uint8_t ch, int16_t val);
int nxscope_put_uint32(FAR struct nxscope_s *s, uint8_t ch, uint32_t val);
int nxscope_put_int32(FAR struct nxscope_s *s, uint8_t ch, int32_t val);
int nxscope_put_uint64(FAR struct nxscope_s *s, uint8_t ch, uint64_t val);
int nxscope_put_int64(FAR struct nxscope_s *s, uint8_t ch, int64_t val);
int nxscope_put_float(FAR struct nxscope_s *s, uint8_t ch, float val);
int nxscope_put_double(FAR struct nxscope_s *s, uint8_t ch, double val);
int nxscope_put_ub8(FAR struct nxscope_s *s, uint8_t ch, ub8_t val);
int nxscope_put_b8(FAR struct nxscope_s *s, uint8_t ch, b8_t val);
int nxscope_put_ub16(FAR struct nxscope_s *s, uint8_t ch, ub16_t val);
int nxscope_put_b16(FAR struct nxscope_s *s, uint8_t ch, b16_t val);
int nxscope_put_ub32(FAR struct nxscope_s *s, uint8_t ch, ub32_t val);
int nxscope_put_b32(FAR struct nxscope_s *s, uint8_t ch, b32_t val);
int nxscope_put_char(FAR struct nxscope_s *s, uint8_t ch, char val);
#endif /* __APPS_INCLUDE_LOGGING_NXSCOPE_NXSCOPE_CHAN_H */

View File

@ -0,0 +1,126 @@
/****************************************************************************
* apps/include/logging/nxscope/nxscope_intf.h
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
#ifndef __APPS_INCLUDE_LOGGING_NXSCOPE_NXSCOPE_INTF_H
#define __APPS_INCLUDE_LOGGING_NXSCOPE_NXSCOPE_INTF_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#ifdef CONFIG_LOGGING_NXSCOPE_INTF_SERIAL
# include <termios.h>
#endif
/****************************************************************************
* Public Types
****************************************************************************/
/* Forward declaration */
struct nxscope_intf_s;
/* Nxscope interface ops */
struct nxscope_intf_ops_s
{
/* Send data */
CODE int (*send)(FAR struct nxscope_intf_s *s, FAR uint8_t *buff, int len);
/* Receive data */
CODE int (*recv)(FAR struct nxscope_intf_s *s, FAR uint8_t *buff, int len);
};
/* Nxscope interface */
struct nxscope_intf_s
{
/* Initialized flag */
bool initialized;
/* Nxscope interface private data */
FAR void *priv;
/* Nxscope interface ops */
FAR struct nxscope_intf_ops_s *ops;
};
#ifdef CONFIG_LOGGING_NXSCOPE_INTF_DUMMY
/* Nxscope dummy interface configuration */
struct nxscope_dummy_cfg_s
{
int res; /* Reserved */
};
#endif
#ifdef CONFIG_LOGGING_NXSCOPE_INTF_SERIAL
/* Nxscope serial interface configuration */
struct nxscope_ser_cfg_s
{
FAR char *path; /* Device path */
bool nonblock; /* Nonblocking operation */
speed_t baud; /* Baud rate */
};
#endif
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
#ifdef CONFIG_LOGGING_NXSCOPE_INTF_DUMMY
/****************************************************************************
* Name: nxscope_dummy_init
****************************************************************************/
int nxscope_dummy_init(FAR struct nxscope_intf_s *intf,
FAR struct nxscope_dummy_cfg_s *cfg);
/****************************************************************************
* Name: nxscope_dummy_deinit
****************************************************************************/
void nxscope_dummy_deinit(FAR struct nxscope_intf_s *intf);
#endif
#ifdef CONFIG_LOGGING_NXSCOPE_INTF_SERIAL
/****************************************************************************
* Name: nxscope_ser_init
****************************************************************************/
int nxscope_ser_init(FAR struct nxscope_intf_s *intf,
FAR struct nxscope_ser_cfg_s *cfg);
/****************************************************************************
* Name: nxscope_ser_deinit
****************************************************************************/
void nxscope_ser_deinit(FAR struct nxscope_intf_s *intf);
#endif
#endif /* __APPS_INCLUDE_LOGGING_NXSCOPE_NXSCOPE_INTF_H */

View File

@ -0,0 +1,105 @@
/****************************************************************************
* apps/include/logging/nxscope/nxscope_proto.h
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
#ifndef __APPS_INCLUDE_LOGGING_NXSCOPE_NXSCOPE_PROTO_H
#define __APPS_INCLUDE_LOGGING_NXSCOPE_NXSCOPE_PROTO_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
/****************************************************************************
* Public Types
****************************************************************************/
/* Nxscope frame handler */
struct nxscope_frame_s
{
uint8_t id; /* Frame id */
size_t drop; /* Data to be droped from recv buffer */
size_t dlen; /* Data len (without header and footer) */
FAR uint8_t *data; /* A pointer to a frame data */
};
/* Forward declaration */
struct nxscope_proto_s;
/* Nxscope protocol ops */
struct nxscope_proto_ops_s
{
/* Get a frame from a buffer */
CODE int (*frame_get)(FAR struct nxscope_proto_s *p,
FAR uint8_t *buff, size_t len,
FAR struct nxscope_frame_s *frame);
/* Finalize a frame in a given buffer */
CODE int (*frame_final)(FAR struct nxscope_proto_s *p,
uint8_t id,
FAR uint8_t *buff, FAR size_t *len);
};
/* Nxscope protocol handler */
struct nxscope_proto_s
{
/* Initialized flag */
bool initialized;
/* Nxscope protocol private data */
FAR void *priv;
/* Nxscope protocol ops */
FAR struct nxscope_proto_ops_s *ops;
/* Header and foot size */
size_t hdrlen;
size_t footlen;
};
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
#ifdef CONFIG_LOGGING_NXSCOPE_PROTO_SER
/****************************************************************************
* Name: nxscope_proto_ser_init
****************************************************************************/
int nxscope_proto_ser_init(FAR struct nxscope_proto_s *proto, FAR void *cfg);
/****************************************************************************
* Name: nxscope_proto_ser_deinit
****************************************************************************/
void nxscope_proto_ser_deinit(FAR struct nxscope_proto_s *proto);
#endif
#endif /* __APPS_INCLUDE_LOGGING_NXSCOPE_NXSCOPE_PROTO_H */

1
logging/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/Kconfig

21
logging/Make.defs Normal file
View File

@ -0,0 +1,21 @@
############################################################################
# apps/logging/Make.defs
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership. The
# ASF licenses this file to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance with the
# License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
############################################################################
include $(wildcard $(APPDIR)/logging/*/Make.defs)

23
logging/Makefile Normal file
View File

@ -0,0 +1,23 @@
############################################################################
# apps/logging/Makefile
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership. The
# ASF licenses this file to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance with the
# License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
############################################################################
MENUDESC = "Logging Utilities"
include $(APPDIR)/Directory.mk

56
logging/nxscope/Kconfig Normal file
View File

@ -0,0 +1,56 @@
#
# For a description of the syntax of this configuration file,
# see the file kconfig-language.txt in the NuttX tools repository.
#
config LOGGING_NXSCOPE
bool "NxScope interface"
default n
if LOGGING_NXSCOPE
config LOGGING_NXSCOPE_INTF_SERIAL
bool "NxScope serial port interface support"
select SERIAL_TERMIOS
default n
---help---
For details, see logging/nxscope/nxscope_iserial.c
config LOGGING_NXSCOPE_INTF_DUMMY
bool "NxScope dummy interface support"
default n
---help---
Useful for debug purposes. For details, see logging/nxscope/nxscope_idummy.c
config LOGGING_NXSCOPE_PROTO_SER
bool "NxScope default serial protocol support"
default y
---help---
For frame details, see logging/nxscope/nxscope_pser.c
config LOGGING_NXSCOPE_DIVIDER
bool "NxScope support for samples divider"
default n
---help---
This option enables interface that allows you to reduce
the rate of samples written to the stream buffer.
config LOGGING_NXSCOPE_ACKFRAMES
bool "NxScope support for ACK frames"
default n
---help---
This option enables ACK frames for set requests
config LOGGING_NXSCOPE_USERTYPES
bool "NxScope support for user types"
default n
---help---
This option enables user-defined frames (see enum nxscope_hdr_id_e)
config LOGGING_NXSCOPE_CRICHANNELS
bool "NxScope support for critical channels"
default n
---help---
Enable the support for non-buffered critical channels
endif # LOGGING_NXSCOPE

23
logging/nxscope/Make.defs Normal file
View File

@ -0,0 +1,23 @@
############################################################################
# apps/logging/nxscope/Make.defs
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership. The
# ASF licenses this file to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance with the
# License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
############################################################################
ifneq ($(CONFIG_LOGGING_NXSCOPE),)
CONFIGURED_APPS += $(APPDIR)/logging/nxscope
endif

39
logging/nxscope/Makefile Normal file
View File

@ -0,0 +1,39 @@
############################################################################
# apps/logging/nxscope/Makefile
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership. The
# ASF licenses this file to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance with the
# License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
############################################################################
include $(APPDIR)/Make.defs
# NxScope Library
CSRCS = nxscope.c nxscope_chan.c nxscope_internals.c
ifeq ($(CONFIG_LOGGING_NXSCOPE_INTF_SERIAL),y)
CSRCS += nxscope_iser.c
endif
ifeq ($(CONFIG_LOGGING_NXSCOPE_INTF_DUMMY),y)
CSRCS += nxscope_idummy.c
endif
ifeq ($(CONFIG_LOGGING_NXSCOPE_PROTO_SER),y)
CSRCS += nxscope_pser.c
endif
include $(APPDIR)/Application.mk

32
logging/nxscope/README.md Normal file
View File

@ -0,0 +1,32 @@
# `NxScope Library`
This library provides real-time data logging functionality for NuttX.
The principle of action is to accumulate data gathered in virtual channels
and periodically send buffered data through a dedicated interface packed
with a custom protocol.
Supported features:
- up to 255 channels possible
- support for standard data types and user-specific data (`enum nxscope_sample_dtype_e`)
- support for vector data or point data
- support for character-based channels (text messages)
- support for channel metadata - can be used to enumerate samples or timestamp
- stream buffer overflow detection (`NXSCOPE_STREAM_FLAGS_OVERFLOW`)
- remote control with commands (`enum nxscope_hdr_id_e`)
- protocol and interface implementation can be different for control commands and stream data
- (optional) support for user-specific commands (`NXSCOPE_HDRID_USER` and `struct nxscope_callbacks_s`)
- (optional) support for samples divider (`CONFIG_LOGGING_NXSCOPE_DIVIDER`)
- (optional) support for ACK frames (`CONFIG_LOGGING_NXSCOPE_ACKFRAMES`)
- (optional) support for user-defined types (`CONFIG_LOGGING_NXSCOPE_USERTYPES`)
- (optional) support for non-buffered critical channels (`CONFIG_LOGGING_NXSCOPE_CRICHANNELS`)
A custom interface and a custom protocol can be implemented with
`struct nxscope_intf_s` and `struct nxscope_proto_s` structures.
Supported interfaces:
1. a serial port: `logging/nxscope/nxscope_iser.c`
2. a dummy interface for debug purposes: `logging/nxscope/nxscope_idummy.c`
A default serial protocol is implemented in `apps/logging/nxscope/nxscope_pser.c`
It just packs NxScope data into simple frames with a CRC-16 checksum.

1168
logging/nxscope/nxscope.c Normal file

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,194 @@
/****************************************************************************
* apps/logging/nxscope/nxscope_idummy.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <assert.h>
#include <debug.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <logging/nxscope/nxscope.h>
/****************************************************************************
* Private Type Definition
****************************************************************************/
struct nxscope_intf_dummy_s
{
FAR struct nxscope_dummy_cfg_s *cfg;
};
/****************************************************************************
* Private Function Protototypes
****************************************************************************/
static int nxscope_dummy_send(FAR struct nxscope_intf_s *intf,
FAR uint8_t *buff, int len);
static int nxscope_dummy_recv(FAR struct nxscope_intf_s *intf,
FAR uint8_t *buff, int len);
/****************************************************************************
* Private Data
****************************************************************************/
static struct nxscope_intf_ops_s g_nxscope_dummy_ops =
{
nxscope_dummy_send,
nxscope_dummy_recv
};
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: nxscope_dummy_send
****************************************************************************/
static int nxscope_dummy_send(FAR struct nxscope_intf_s *intf,
FAR uint8_t *buff, int len)
{
FAR struct nxscope_intf_dummy_s *priv = NULL;
DEBUGASSERT(intf);
DEBUGASSERT(intf->priv);
_info("nxscope_dummy_send\n");
/* Get priv data */
priv = (FAR struct nxscope_intf_dummy_s *)intf->priv;
UNUSED(priv);
/* Dump send buffer */
lib_dumpbuffer("nxscope_dummy_send", buff, len);
return OK;
}
/****************************************************************************
* Name: nxscope_dummy_recv
****************************************************************************/
static int nxscope_dummy_recv(FAR struct nxscope_intf_s *intf,
FAR uint8_t *buff, int len)
{
FAR struct nxscope_intf_dummy_s *priv = NULL;
DEBUGASSERT(intf);
DEBUGASSERT(intf->priv);
_info("nxscope_dummy_recv\n");
/* Get priv data */
priv = (FAR struct nxscope_intf_dummy_s *)intf->priv;
UNUSED(priv);
/* Dump recv buffer */
lib_dumpbuffer("nxscope_dummy_recv", buff, len);
return OK;
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: nxscope_dummy_init
****************************************************************************/
int nxscope_dummy_init(FAR struct nxscope_intf_s *intf,
FAR struct nxscope_dummy_cfg_s *cfg)
{
FAR struct nxscope_intf_dummy_s *priv = NULL;
int ret = OK;
DEBUGASSERT(intf);
DEBUGASSERT(cfg);
_info("nxscope_dummy_init\n");
/* Allocate priv data */
intf->priv = zalloc(sizeof(struct nxscope_intf_dummy_s));
if (intf->priv == NULL)
{
_err("ERROR: intf->priv alloc failed %d\n", errno);
ret = -errno;
goto errout;
}
/* Get priv data */
priv = (FAR struct nxscope_intf_dummy_s *)intf->priv;
/* Connect configuration */
priv->cfg = cfg;
/* Connect ops */
intf->ops = &g_nxscope_dummy_ops;
/* Initialized */
intf->initialized = true;
errout:
return ret;
}
/****************************************************************************
* Name: nxscope_dummy_deinit
****************************************************************************/
void nxscope_dummy_deinit(FAR struct nxscope_intf_s *intf)
{
FAR struct nxscope_intf_dummy_s *priv = NULL;
DEBUGASSERT(intf);
_info("nxscope_dummy_deinit\n");
/* Get priv data */
priv = (FAR struct nxscope_intf_dummy_s *)intf->priv;
if (priv != NULL)
{
free(priv);
}
/* Reset structure */
memset(intf, 0, sizeof(struct nxscope_intf_s));
}

View File

@ -0,0 +1,82 @@
/****************************************************************************
* apps/logging/nxscope/nxscope_internals.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <assert.h>
#include <debug.h>
#include <errno.h>
#include <stdlib.h>
#include <logging/nxscope/nxscope.h>
#include "nxscope_internals.h"
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: nxscope_stream_send
*
* Description:
* Send stream buffer
*
* Input Parameters:
* s - a pointer to a nxscope instance
* buff - buffer to send
* buff_i - buffer cursor
*
****************************************************************************/
int nxscope_stream_send(FAR struct nxscope_s *s, FAR uint8_t *buff,
FAR size_t *buff_i)
{
int ret = OK;
DEBUGASSERT(s);
DEBUGASSERT(buff);
DEBUGASSERT(buff_i);
/* Finalize stream frame */
ret = PROTO_FRAME_FINAL(s, s->proto_stream,
NXSCOPE_HDRID_STREAM, buff, buff_i);
if (ret < 0)
{
_err("ERROR: PROTO_FRAME_FINAL failed %d\n", ret);
goto errout;
}
/* Send stream data */
ret = INTF_SEND(s, s->intf_stream, buff, *buff_i);
if (ret < 0)
{
_err("ERROR: INTF_SEND failed %d\n", ret);
}
errout:
return ret;
}

View File

@ -0,0 +1,72 @@
/****************************************************************************
* apps/logging/nxscope/nxscope_internals.h
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
#ifndef __APPS_LOGGING_NXSCOPE_NXSCOPE_INTERNALS_H
#define __APPS_LOGGING_NXSCOPE_NXSCOPE_INTERNALS_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <logging/nxscope/nxscope.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/* Max channel name length */
#define CHAN_NAMELEN_MAX (32)
/* Helpers */
#define PROTO_FRAME_FINAL(s, proto, id, buff, i) \
(s)->proto_stream->ops->frame_final(proto, id, buff, i)
#define PROTO_FRAME_GET(s, proto, buff, i, frame) \
(s)->proto_stream->ops->frame_get(proto, buff, i, frame)
#define INTF_SEND(s, intf, buff, i) \
(s)->intf_stream->ops->send(intf, buff, i)
#define INTF_RECV(s, intf, buff, i) \
(s)->intf_stream->ops->recv(intf, buff, i)
/****************************************************************************
* Public Function Puttypes
****************************************************************************/
/****************************************************************************
* Name: nxscope_stream_send
*
* Description:
* Send stream buffers
*
* Input Parameters:
* s - a pointer to a nxscope instance
* buff - buffer to send
* buff_i - buffer cursor
*
****************************************************************************/
int nxscope_stream_send(FAR struct nxscope_s *s, FAR uint8_t *buff,
FAR size_t *buff_i);
#endif /* __APPS_LOGGING_NXSCOPE_NXSCOPE_INTERNALS_H */

View File

@ -0,0 +1,248 @@
/****************************************************************************
* apps/logging/nxscope/nxscope_iser.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <debug.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <unistd.h>
#include <logging/nxscope/nxscope.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#ifndef CONFIG_SERIAL_TERMIOS
# error Termios support must be enabled
#endif
/****************************************************************************
* Private Type Definition
****************************************************************************/
struct nxscope_intf_ser_s
{
FAR struct nxscope_ser_cfg_s *cfg;
int fd;
};
/****************************************************************************
* Private Function Protototypes
****************************************************************************/
static int nxscope_ser_send(FAR struct nxscope_intf_s *intf,
FAR uint8_t *buff, int len);
static int nxscope_ser_recv(FAR struct nxscope_intf_s *intf,
FAR uint8_t *buff, int len);
/****************************************************************************
* Private Data
****************************************************************************/
static struct nxscope_intf_ops_s g_nxscope_ser_ops =
{
nxscope_ser_send,
nxscope_ser_recv
};
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: nxscope_ser_send
****************************************************************************/
static int nxscope_ser_send(FAR struct nxscope_intf_s *intf,
FAR uint8_t *buff, int len)
{
FAR struct nxscope_intf_ser_s *priv = NULL;
DEBUGASSERT(intf);
DEBUGASSERT(intf->priv);
/* Get priv data */
priv = (FAR struct nxscope_intf_ser_s *)intf->priv;
/* Write data */
return write(priv->fd, buff, len);
}
/****************************************************************************
* Name: nxscope_ser_recv
****************************************************************************/
static int nxscope_ser_recv(FAR struct nxscope_intf_s *intf,
FAR uint8_t *buff, int len)
{
FAR struct nxscope_intf_ser_s *priv = NULL;
int ret = OK;
DEBUGASSERT(intf);
DEBUGASSERT(intf->priv);
/* Get priv data */
priv = (FAR struct nxscope_intf_ser_s *)intf->priv;
/* Read data */
ret = read(priv->fd, buff, len);
if (ret < 0)
{
if (priv->cfg->nonblock && (errno == EAGAIN))
{
ret = 0;
}
}
return ret;
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: nxscope_ser_init
****************************************************************************/
int nxscope_ser_init(FAR struct nxscope_intf_s *intf,
FAR struct nxscope_ser_cfg_s *cfg)
{
FAR struct nxscope_intf_ser_s *priv = NULL;
struct termios tio;
int ret = OK;
int flags = 0;
DEBUGASSERT(intf);
DEBUGASSERT(cfg);
/* Allocate priv data */
intf->priv = zalloc(sizeof(struct nxscope_intf_ser_s));
if (intf->priv == NULL)
{
_err("ERROR: intf->priv alloc failed %d\n", errno);
ret = -errno;
goto errout;
}
/* Get priv data */
priv = (FAR struct nxscope_intf_ser_s *)intf->priv;
/* Connect configuration */
priv->cfg = (FAR struct nxscope_ser_cfg_s *)cfg;
/* Connect ops */
intf->ops = &g_nxscope_ser_ops;
/* Open serial port */
flags = O_RDWR;
if (priv->cfg->nonblock)
{
flags |= O_NONBLOCK;
}
priv->fd = open(priv->cfg->path, flags);
if (priv->fd < 0)
{
_err("ERROR: failed to open %s\n", priv->cfg->path);
ret = -errno;
goto errout;
}
/* Get the attributes */
tcgetattr(priv->fd, &tio);
/* Configure a baud rate */
DEBUGASSERT(priv->cfg->baud > 0);
ret = cfsetspeed(&tio, priv->cfg->baud);
if (ret < 0)
{
_err("ERROR: failed to set baud rate %d\n", errno);
goto errout;
}
/* Configure RAW mode */
cfmakeraw(&tio);
/* Change the attributes now */
tcsetattr(priv->fd, TCSANOW, &tio);
/* Initialized */
intf->initialized = true;
errout:
return ret;
}
/****************************************************************************
* Name: nxscope_ser_deinit
****************************************************************************/
void nxscope_ser_deinit(FAR struct nxscope_intf_s *intf)
{
FAR struct nxscope_intf_ser_s *priv = NULL;
DEBUGASSERT(intf);
/* Get priv data */
priv = (FAR struct nxscope_intf_ser_s *)intf->priv;
/* Close dev */
if (priv->fd != -1)
{
close(priv->fd);
}
if (priv != NULL)
{
free(priv);
}
/* Reset structure */
memset(intf, 0, sizeof(struct nxscope_intf_s));
}

View File

@ -0,0 +1,293 @@
/****************************************************************************
* apps/logging/nxscope/nxscope_pser.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <assert.h>
#include <debug.h>
#include <endian.h>
#include <errno.h>
#include <string.h>
#include <nuttx/crc16.h>
#include <logging/nxscope/nxscope.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define NXSCOPE_HDR_LEN (sizeof(struct nxscope_hdr_s))
#define NXSCOPE_DATA_START (NXSCOPE_HDR_LEN)
#define NXSCOPE_BASEFRAME_LEN (NXSCOPE_HDR_LEN + NXSCOPE_CRC_LEN)
#define NXSCOPE_HDR_SOF (0x55)
#define NXSCOPE_CRC_LEN (sizeof(uint16_t))
/****************************************************************************
* Private Type Definition
****************************************************************************/
/* Nxscope serial protocol frame:
* +--------------------+------------+-------------+
* | HDR [4B] | frame data | footer [2B] |
* +-----+---------+----+------------+-------------+
* | SOF | len [1] | id | frame data | crc16 [2] |
* +-----+----+----+----+------------+------+------+
* | 0 | 1 | 2 | 3 | ... | n+1 | n+2 |
* +-----+----+----+----+------------+------+------+
*
* [1] - always little-endian
* [2] - always big-endian
*/
/* Nxscope header */
begin_packed_struct struct nxscope_hdr_s
{
uint8_t sof; /* SOF */
uint16_t len; /* Frame length */
uint8_t id; /* Frame ID */
} end_packed_struct;
/* Nxscope footer */
begin_packed_struct struct nxscope_footer_s
{
uint16_t crc16; /* check sum (see nxscope_frame_final()) */
} end_packed_struct;
/****************************************************************************
* Private Function Protototypes
****************************************************************************/
static int nxscope_frame_get(FAR struct nxscope_proto_s *p,
FAR uint8_t *buff, size_t len,
FAR struct nxscope_frame_s *frame);
static int nxscope_frame_final(FAR struct nxscope_proto_s *p,
uint8_t id,
FAR uint8_t *buff, FAR size_t *len);
/****************************************************************************
* Public Data
****************************************************************************/
static struct nxscope_proto_ops_s g_nxscope_proto_ser_ops =
{
nxscope_frame_get,
nxscope_frame_final,
};
static struct nxscope_proto_s g_nxscope_proto_ser =
{
false,
NULL,
&g_nxscope_proto_ser_ops,
(size_t)NXSCOPE_HDR_LEN,
(size_t)NXSCOPE_CRC_LEN
};
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: nxscope_hdr_fill
****************************************************************************/
static void nxscope_hdr_fill(FAR uint8_t *buff, uint8_t id, uint16_t len)
{
FAR struct nxscope_hdr_s *hdr = NULL;
DEBUGASSERT(buff);
hdr = (FAR struct nxscope_hdr_s *)buff;
hdr->sof = NXSCOPE_HDR_SOF;
/* Length always as little endian */
hdr->len = htole16(len);
hdr->id = id;
}
/****************************************************************************
* Name: nxscope_frame_get
****************************************************************************/
static int nxscope_frame_get(FAR struct nxscope_proto_s *p,
FAR uint8_t *buff, size_t len,
FAR struct nxscope_frame_s *frame)
{
FAR struct nxscope_hdr_s *hdr = NULL;
uint16_t crc = 0;
int ret = OK;
int i = 0;
DEBUGASSERT(p);
DEBUGASSERT(buff);
DEBUGASSERT(frame);
UNUSED(p);
/* Find SOF */
for (i = 0; i < len - NXSCOPE_HDR_LEN; i++)
{
hdr = (FAR struct nxscope_hdr_s *)&buff[i];
/* Verify SOF */
if (hdr->sof == NXSCOPE_HDR_SOF)
{
break;
}
}
if (hdr->sof != NXSCOPE_HDR_SOF)
{
ret = -EINVAL;
goto errout;
}
/* Verify len - always little-endian */
hdr->len = htole16(hdr->len);
if ((len - i) < hdr->len)
{
ret = -EINVAL;
goto errout;
}
/* Verify crc16 for the whole frame */
crc = crc16(&buff[i], hdr->len);
if (crc != 0)
{
_err("ERROR: invalid crc16 %d\n", crc);
ret = -EINVAL;
goto errout;
}
/* Return the frame data */
frame->id = hdr->id;
frame->data = &buff[i + NXSCOPE_DATA_START];
frame->drop = hdr->len + i;
frame->dlen = hdr->len - NXSCOPE_BASEFRAME_LEN;
errout:
return ret;
}
/****************************************************************************
* Name: nxscope_frame_final
****************************************************************************/
static int nxscope_frame_final(FAR struct nxscope_proto_s *p,
uint8_t id,
FAR uint8_t *buff, FAR size_t *len)
{
uint16_t crc = 0;
int ret = OK;
DEBUGASSERT(p);
DEBUGASSERT(buff);
DEBUGASSERT(len);
if (*len <= NXSCOPE_HDR_LEN)
{
/* No data */
ret = -ENODATA;
goto errout;
}
/* Fill hdr */
nxscope_hdr_fill(buff, id, *len + NXSCOPE_CRC_LEN);
/* Add crc16 - always as big-endian.
*
* crc16 xmodem:
* polynominal = 0x1021
* initial value = 0x0000
* final xor value = 0x0000
*/
crc = crc16(buff, *len);
#ifdef CONFIG_ENDIAN_BIG
buff[(*len)++] = (crc >> 0) & 0xff;
buff[(*len)++] = (crc >> 8) & 0xff;
#else
buff[(*len)++] = (crc >> 8) & 0xff;
buff[(*len)++] = (crc >> 0) & 0xff;
#endif
errout:
return ret;
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: nxscope_proto_ser_init
****************************************************************************/
int nxscope_proto_ser_init(FAR struct nxscope_proto_s *proto, FAR void *cfg)
{
DEBUGASSERT(proto);
/* cfg argument not used, but keept here for compatibility with
* future protocol implementations.
*/
UNUSED(cfg);
/* Just copy protocol data */
memcpy(proto, &g_nxscope_proto_ser, sizeof(struct nxscope_proto_s));
/* Initialized */
proto->initialized = true;
return OK;
}
/****************************************************************************
* Name: nxscope_proto_ser_deinit
****************************************************************************/
void nxscope_proto_ser_deinit(FAR struct nxscope_proto_s *proto)
{
DEBUGASSERT(proto);
/* Nothings here */
UNUSED(proto);
}