diff options
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/nanopb/SConscript | 13 | ||||
| -rw-r--r-- | examples/nanopb/simple.c | 73 | ||||
| -rw-r--r-- | examples/nanopb/simple.options | 1 | ||||
| -rw-r--r-- | examples/nanopb/simple.pb.c | 18 | ||||
| -rw-r--r-- | examples/nanopb/simple.pb.h | 45 | ||||
| -rw-r--r-- | examples/nanopb/simple.proto | 10 | ||||
| -rw-r--r-- | examples/ymodem/echo.c | 4 | ||||
| -rw-r--r-- | examples/ymodem/null.c | 4 | ||||
| -rw-r--r-- | examples/ymodem/tofile.c | 8 |
9 files changed, 168 insertions, 8 deletions
diff --git a/examples/nanopb/SConscript b/examples/nanopb/SConscript new file mode 100644 index 0000000000..7c8bce9f26 --- /dev/null +++ b/examples/nanopb/SConscript @@ -0,0 +1,13 @@ +Import('RTT_ROOT') +Import('rtconfig') +from building import * + +src = Split(''' +simple.c +simple.pb.c +''') +CPPPATH = [RTT_ROOT + '/examples/nanopb'] + +group = DefineGroup('Nanopb_test', src, depend = ['RT_USING_NANOPB'], CPPPATH = CPPPATH) + +Return('group')
\ No newline at end of file diff --git a/examples/nanopb/simple.c b/examples/nanopb/simple.c new file mode 100644 index 0000000000..2b793c014f --- /dev/null +++ b/examples/nanopb/simple.c @@ -0,0 +1,73 @@ +#include <rthw.h> +#include <stm32f10x.h> +#include <pb_encode.h> +#include <pb_decode.h> +#include "simple.pb.h" + +int nanopb_test() +{ + /* This is the buffer where we will store our message. */ + uint8_t buffer[128]; + size_t message_length; + bool status; + + /* Encode our message */ + { + /* Allocate space on the stack to store the message data. + * + * Nanopb generates simple struct definitions for all the messages. + * - check out the contents of simple.pb.h! */ + SimpleMessage message = SimpleMessage_init_zero; + + /* Create a stream that will write to our buffer. */ + pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer)); + + /* Fill in the lucky number */ + message.lucky_number = 13; + + /* Now we are ready to encode the message! */ + status = pb_encode(&stream, SimpleMessage_fields, &message); + message_length = stream.bytes_written; + + /* Then just check for any errors.. */ + if (!status) + { + rt_kprintf("Encoding failed: %s\n", PB_GET_ERROR(&stream)); + return 1; + } + } + + /* Now we could transmit the message over network, store it in a file or + * wrap it to a pigeon's leg. + */ + + /* But because we are lazy, we will just decode it immediately. */ + + { + /* Allocate space for the decoded message. */ + SimpleMessage message; + + /* Create a stream that reads from the buffer. */ + pb_istream_t stream = pb_istream_from_buffer(buffer, message_length); + + /* Now we are ready to decode the message. */ + status = pb_decode(&stream, SimpleMessage_fields, &message); + + /* Check for errors... */ + if (!status) + { + rt_kprintf("Decoding failed: %s\n", PB_GET_ERROR(&stream)); + return 1; + } + + /* Print the data contained in the message. */ + rt_kprintf("Your lucky number was %d!\n", message.lucky_number); + } + + return 0; +} + +#ifdef RT_USING_FINSH +#include <finsh.h> +FINSH_FUNCTION_EXPORT(nanopb_test, nanopb encode/decode test.) +#endif diff --git a/examples/nanopb/simple.options b/examples/nanopb/simple.options new file mode 100644 index 0000000000..90960d0d34 --- /dev/null +++ b/examples/nanopb/simple.options @@ -0,0 +1 @@ +SimpleMessage.name max_size:16
\ No newline at end of file diff --git a/examples/nanopb/simple.pb.c b/examples/nanopb/simple.pb.c new file mode 100644 index 0000000000..eaf09e58da --- /dev/null +++ b/examples/nanopb/simple.pb.c @@ -0,0 +1,18 @@ +/* Automatically generated nanopb constant definitions */ +/* Generated by nanopb-0.3.1 at Tue Mar 10 01:16:15 2015. */ + +#include "simple.pb.h" + +#if PB_PROTO_HEADER_VERSION != 30 +#error Regenerate this file with the current version of nanopb generator. +#endif + + + +const pb_field_t SimpleMessage_fields[3] = { + PB_FIELD( 1, INT32 , REQUIRED, STATIC , FIRST, SimpleMessage, lucky_number, lucky_number, 0), + PB_FIELD( 2, BYTES , REQUIRED, STATIC , OTHER, SimpleMessage, name, lucky_number, 0), + PB_LAST_FIELD +}; + + diff --git a/examples/nanopb/simple.pb.h b/examples/nanopb/simple.pb.h new file mode 100644 index 0000000000..47521dee69 --- /dev/null +++ b/examples/nanopb/simple.pb.h @@ -0,0 +1,45 @@ +/* Automatically generated nanopb header */ +/* Generated by nanopb-0.3.1 at Tue Mar 10 01:16:15 2015. */ + +#ifndef PB_SIMPLE_PB_H_INCLUDED +#define PB_SIMPLE_PB_H_INCLUDED +#include <pb.h> + +#if PB_PROTO_HEADER_VERSION != 30 +#error Regenerate this file with the current version of nanopb generator. +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +/* Enum definitions */ +/* Struct definitions */ +typedef PB_BYTES_ARRAY_T(16) SimpleMessage_name_t; + +typedef struct _SimpleMessage { + int32_t lucky_number; + SimpleMessage_name_t name; +} SimpleMessage; + +/* Default values for struct fields */ + +/* Initializer values for message structs */ +#define SimpleMessage_init_default {0, {0, {0}}} +#define SimpleMessage_init_zero {0, {0, {0}}} + +/* Field tags (for use in manual encoding/decoding) */ +#define SimpleMessage_lucky_number_tag 1 +#define SimpleMessage_name_tag 2 + +/* Struct field encoding specification for nanopb */ +extern const pb_field_t SimpleMessage_fields[3]; + +/* Maximum encoded size of messages (where known) */ +#define SimpleMessage_size 29 + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif diff --git a/examples/nanopb/simple.proto b/examples/nanopb/simple.proto new file mode 100644 index 0000000000..1905d0e50b --- /dev/null +++ b/examples/nanopb/simple.proto @@ -0,0 +1,10 @@ +// A very simple protocol definition, consisting of only +// one message. + +message SimpleMessage { + required int32 lucky_number = 1; + required bytes name = 2; +} + + + diff --git a/examples/ymodem/echo.c b/examples/ymodem/echo.c index 5ffa5ff417..5cb807f519 100644 --- a/examples/ymodem/echo.c +++ b/examples/ymodem/echo.c @@ -25,8 +25,8 @@ rt_err_t rym_cat_to_dev(rt_device_t idev, rt_device_t odev) rt_kprintf("open output device error: 0x%x", -res); return res; } - res = rym_recv_on_device(&rctx, idev, - RT_NULL, _rym_echo_data, RT_NULL, 1000); + res = rym_recv_on_device(&rctx, idev, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_INT_RX, + RT_NULL, _rym_echo_data, RT_NULL, 1000); rt_device_close(_odev); rt_kprintf("leaving RYM mode with code %X\n", res); return res; diff --git a/examples/ymodem/null.c b/examples/ymodem/null.c index 5e3baa9870..c72ea064f8 100644 --- a/examples/ymodem/null.c +++ b/examples/ymodem/null.c @@ -20,8 +20,8 @@ rt_err_t rym_null(char *devname) return -1; } - return rym_recv_on_device(&rctx, dev, - RT_NULL, _rym_dummy_write, RT_NULL, 1000); + return rym_recv_on_device(&rctx, dev, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_INT_RX, + RT_NULL, _rym_dummy_write, RT_NULL, 1000); } FINSH_FUNCTION_EXPORT(rym_null, dump data to null); #endif diff --git a/examples/ymodem/tofile.c b/examples/ymodem/tofile.c index 2deef73c2c..4a3705ede7 100644 --- a/examples/ymodem/tofile.c +++ b/examples/ymodem/tofile.c @@ -21,7 +21,7 @@ static enum rym_code _rym_bg( struct custom_ctx *cctx = (struct custom_ctx*)ctx; cctx->fpath[0] = '/'; /* the buf should be the file name */ - strcpy(&(cctx->fpath[1]), buf); + strcpy(&(cctx->fpath[1]), (const char*)buf); cctx->fd = open(cctx->fpath, O_CREAT | O_WRONLY | O_TRUNC, 0); if (cctx->fd < 0) { @@ -31,7 +31,7 @@ static enum rym_code _rym_bg( return RYM_CODE_CAN; } - cctx->flen = atoi(buf+strlen(buf)+1); + cctx->flen = atoi((const char*)buf+strlen((const char*)buf)+1); if (cctx->flen == 0) cctx->flen = -1; return RYM_CODE_ACK; @@ -80,8 +80,8 @@ rt_err_t rym_write_to_file(rt_device_t idev) rt_kprintf("entering RYM mode\n"); - res = rym_recv_on_device(&ctx->parent, idev, - _rym_bg, _rym_tof, _rym_end, 1000); + res = rym_recv_on_device(&ctx->parent, idev, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_INT_RX, + _rym_bg, _rym_tof, _rym_end, 1000); /* there is no Ymodem traffic on the line so print out info. */ rt_kprintf("leaving RYM mode with code %d\n", res); |
