wip rx handler
This commit is contained in:
+10
@@ -0,0 +1,10 @@
|
||||
//
|
||||
// Created by MightyPork on 2018/03/31.
|
||||
//
|
||||
|
||||
#ifndef GEX_NRF_DEBUG_H
|
||||
#define GEX_NRF_DEBUG_H
|
||||
|
||||
void dbg(const char *format, ...);
|
||||
|
||||
#endif //GEX_NRF_DEBUG_H
|
||||
@@ -0,0 +1,11 @@
|
||||
//
|
||||
// Created by MightyPork on 2018/03/31.
|
||||
//
|
||||
|
||||
#ifndef GEX_NRF_GEX_GATEWAY_H
|
||||
#define GEX_NRF_GEX_GATEWAY_H
|
||||
|
||||
#include "main.h"
|
||||
void gw_process(void);
|
||||
|
||||
#endif //GEX_NRF_GEX_GATEWAY_H
|
||||
@@ -64,6 +64,9 @@
|
||||
#include "stm32f1xx_ll_usart.h"
|
||||
#include "stm32f1xx.h"
|
||||
#include "stm32f1xx_ll_gpio.h"
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <inttypes.h>
|
||||
|
||||
/* USER CODE BEGIN Includes */
|
||||
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
//
|
||||
// Created by MightyPork on 2018/04/01.
|
||||
//
|
||||
|
||||
#ifndef GEX_NRF_MINMAX_H
|
||||
#define GEX_NRF_MINMAX_H
|
||||
|
||||
#ifndef MAX
|
||||
#define MAX(a,b) ((a) > (b) ? (a) : (b))
|
||||
#endif
|
||||
|
||||
#ifndef MIN
|
||||
#define MIN(a,b) ((a) > (b) ? (b) : (a))
|
||||
#endif
|
||||
|
||||
#endif //GEX_NRF_MINMAX_H
|
||||
@@ -0,0 +1,37 @@
|
||||
//
|
||||
// Created by MightyPork on 2018/03/31.
|
||||
//
|
||||
|
||||
#ifndef GEX_NRF_MSG_QUEUE_H
|
||||
#define GEX_NRF_MSG_QUEUE_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#define MQ_LEN 16
|
||||
#define MQ_SLOT_LEN 64
|
||||
|
||||
typedef struct {
|
||||
uint8_t packet[MQ_SLOT_LEN];
|
||||
} MQSlot;
|
||||
|
||||
typedef struct {
|
||||
MQSlot slots[MQ_LEN];
|
||||
volatile uint32_t lr;
|
||||
volatile uint32_t nw;
|
||||
} MQueue;
|
||||
|
||||
extern MQueue usb_rxq;
|
||||
extern MQueue usb_txq;
|
||||
|
||||
void mq_init(MQueue *mq);
|
||||
|
||||
bool mq_can_post(MQueue *mq);
|
||||
|
||||
bool mq_post(MQueue *mq, const uint8_t *buffer, uint32_t len);
|
||||
|
||||
bool mq_can_read(MQueue *mq);
|
||||
|
||||
bool mq_read(MQueue *mq, uint8_t *buffer);
|
||||
|
||||
#endif //GEX_NRF_MSG_QUEUE_H
|
||||
@@ -0,0 +1,110 @@
|
||||
#ifndef PAYLOAD_BUILDER_H
|
||||
#define PAYLOAD_BUILDER_H
|
||||
|
||||
/**
|
||||
* PayloadBuilder, part of the TinyFrame utilities collection
|
||||
*
|
||||
* (c) Ondřej Hruška, 2014-2017. MIT license.
|
||||
*
|
||||
* The builder supports big and little endian which is selected when
|
||||
* initializing it or by accessing the bigendian struct field.
|
||||
*
|
||||
* This module helps you with building payloads (not only for TinyFrame)
|
||||
*
|
||||
* The builder performs bounds checking and calls the provided handler when
|
||||
* the requested write wouldn't fit. Use the handler to realloc / flush the buffer
|
||||
* or report an error.
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include "type_coerce.h"
|
||||
|
||||
typedef struct PayloadBuilder_ PayloadBuilder;
|
||||
|
||||
/**
|
||||
* Full buffer handler.
|
||||
*
|
||||
* 'needed' more bytes should be written but the end of the buffer was reached.
|
||||
*
|
||||
* Return true if the problem was solved (e.g. buffer was flushed and the
|
||||
* 'current' pointer moved to the beginning).
|
||||
*
|
||||
* If false is returned, the 'ok' flag on the struct is set to false
|
||||
* and all following writes are discarded.
|
||||
*/
|
||||
typedef bool (*pb_full_handler)(PayloadBuilder *pb, uint32_t needed);
|
||||
|
||||
struct PayloadBuilder_ {
|
||||
uint8_t *start; //!< Pointer to the beginning of the buffer
|
||||
uint8_t *current; //!< Pointer to the next byte to be read
|
||||
uint8_t *end; //!< Pointer to the end of the buffer (start + length)
|
||||
pb_full_handler full_handler; //!< Callback for buffer overrun
|
||||
bool bigendian; //!< Flag to use big-endian parsing
|
||||
bool ok; //!< Indicates that all reads were successful
|
||||
};
|
||||
|
||||
// --- initializer helper macros ---
|
||||
|
||||
/** Start the builder. */
|
||||
#define pb_start_e(buf, capacity, bigendian, full_handler) ((PayloadBuilder){buf, buf, (buf)+(capacity), full_handler, bigendian, 1})
|
||||
|
||||
/** Start the builder in big-endian mode */
|
||||
#define pb_start_be(buf, capacity, full_handler) pb_start_e(buf, capacity, 1, full_handler)
|
||||
|
||||
/** Start the builder in little-endian mode */
|
||||
#define pb_start_le(buf, capacity, full_handler) pb_start_e(buf, capacity, 0, full_handler)
|
||||
|
||||
/** Start the parser in little-endian mode (default) */
|
||||
#define pb_start(buf, capacity, full_handler) pb_start_le(buf, capacity, full_handler)
|
||||
|
||||
// --- utilities ---
|
||||
|
||||
/** Get already used bytes count */
|
||||
#define pb_length(pb) ((pb)->current - (pb)->start)
|
||||
|
||||
/** Reset the current pointer to start */
|
||||
#define pb_rewind(pb) do { pb->current = pb->start; } while (0)
|
||||
|
||||
|
||||
/** Write from a buffer */
|
||||
bool pb_buf(PayloadBuilder *pb, const uint8_t *buf, uint32_t len);
|
||||
|
||||
/** Write a zero terminated string */
|
||||
bool pb_string(PayloadBuilder *pb, const char *str);
|
||||
|
||||
/** Write uint8_t to the buffer */
|
||||
bool pb_u8(PayloadBuilder *pb, uint8_t byte);
|
||||
|
||||
/** Write boolean to the buffer. */
|
||||
static inline bool pb_bool(PayloadBuilder *pb, bool b)
|
||||
{
|
||||
return pb_u8(pb, (uint8_t) b);
|
||||
}
|
||||
|
||||
/** Write uint16_t to the buffer. */
|
||||
bool pb_u16(PayloadBuilder *pb, uint16_t word);
|
||||
|
||||
/** Write uint32_t to the buffer. */
|
||||
bool pb_u32(PayloadBuilder *pb, uint32_t word);
|
||||
|
||||
/** Write int8_t to the buffer. */
|
||||
bool pb_i8(PayloadBuilder *pb, int8_t byte);
|
||||
|
||||
/** Write char (int8_t) to the buffer. */
|
||||
static inline bool pb_char(PayloadBuilder *pb, char c)
|
||||
{
|
||||
return pb_i8(pb, c);
|
||||
}
|
||||
|
||||
/** Write int16_t to the buffer. */
|
||||
bool pb_i16(PayloadBuilder *pb, int16_t word);
|
||||
|
||||
/** Write int32_t to the buffer. */
|
||||
bool pb_i32(PayloadBuilder *pb, int32_t word);
|
||||
|
||||
/** Write 4-byte float to the buffer. */
|
||||
bool pb_float(PayloadBuilder *pb, float f);
|
||||
|
||||
#endif // PAYLOAD_BUILDER_H
|
||||
@@ -0,0 +1,143 @@
|
||||
#ifndef PAYLOAD_PARSER_H
|
||||
#define PAYLOAD_PARSER_H
|
||||
|
||||
/**
|
||||
* PayloadParser, part of the TinyFrame utilities collection
|
||||
*
|
||||
* (c) Ondřej Hruška, 2016-2017. MIT license.
|
||||
*
|
||||
* This module helps you with parsing payloads (not only from TinyFrame).
|
||||
*
|
||||
* The parser supports big and little-endian which is selected when
|
||||
* initializing it or by accessing the bigendian struct field.
|
||||
*
|
||||
* The parser performs bounds checking and calls the provided handler when
|
||||
* the requested read doesn't have enough data. Use the callback to take
|
||||
* appropriate action, e.g. report an error.
|
||||
*
|
||||
* If the handler function is not defined, the pb->ok flag is set to false
|
||||
* (use this to check for success), and further reads won't have any effect
|
||||
* and always result in 0 or empty array.
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include <stdbool.h>
|
||||
#include "type_coerce.h"
|
||||
|
||||
typedef struct PayloadParser_ PayloadParser;
|
||||
|
||||
/**
|
||||
* Empty buffer handler.
|
||||
*
|
||||
* 'needed' more bytes should be read but the end was reached.
|
||||
*
|
||||
* Return true if the problem was solved (e.g. new data loaded into
|
||||
* the buffer and the 'current' pointer moved to the beginning).
|
||||
*
|
||||
* If false is returned, the 'ok' flag on the struct is set to false
|
||||
* and all following reads will fail / return 0.
|
||||
*/
|
||||
typedef bool (*pp_empty_handler)(PayloadParser *pp, uint32_t needed);
|
||||
|
||||
struct PayloadParser_ {
|
||||
uint8_t *start; //!< Pointer to the beginning of the buffer
|
||||
uint8_t *current; //!< Pointer to the next byte to be read
|
||||
uint8_t *end; //!< Pointer to the end of the buffer (start + length)
|
||||
pp_empty_handler empty_handler; //!< Callback for buffer underrun
|
||||
bool bigendian; //!< Flag to use big-endian parsing
|
||||
bool ok; //!< Indicates that all reads were successful
|
||||
};
|
||||
|
||||
// --- initializer helper macros ---
|
||||
|
||||
/** Start the parser. */
|
||||
#define pp_start_e(buf, length, bigendian, empty_handler) ((PayloadParser){buf, buf, (buf)+(length), empty_handler, bigendian, 1})
|
||||
|
||||
/** Start the parser in big-endian mode */
|
||||
#define pp_start_be(buf, length, empty_handler) pp_start_e(buf, length, 1, empty_handler)
|
||||
|
||||
/** Start the parser in little-endian mode */
|
||||
#define pp_start_le(buf, length, empty_handler) pp_start_e(buf, length, 0, empty_handler)
|
||||
|
||||
/** Start the parser in little-endian mode (default) */
|
||||
#define pp_start(buf, length, empty_handler) pp_start_le(buf, length, empty_handler)
|
||||
|
||||
// --- utilities ---
|
||||
|
||||
/** Get remaining length */
|
||||
#define pp_length(pp) ((pp)->end - (pp)->current)
|
||||
|
||||
/** Reset the current pointer to start */
|
||||
#define pp_rewind(pp) do { pp->current = pp->start; } while (0)
|
||||
|
||||
|
||||
/**
|
||||
* @brief Get the remainder of the buffer.
|
||||
*
|
||||
* Returns NULL and sets 'length' to 0 if there are no bytes left.
|
||||
*
|
||||
* @param pp
|
||||
* @param length : here the buffer length will be stored. NULL to do not store.
|
||||
* @return the remaining portion of the input buffer
|
||||
*/
|
||||
const uint8_t *pp_tail(PayloadParser *pp, uint32_t *length);
|
||||
|
||||
/** Read uint8_t from the payload. */
|
||||
uint8_t pp_u8(PayloadParser *pp);
|
||||
|
||||
/** Read bool from the payload. */
|
||||
static inline int8_t pp_bool(PayloadParser *pp)
|
||||
{
|
||||
return pp_u8(pp) != 0;
|
||||
}
|
||||
|
||||
/** Skip bytes */
|
||||
void pp_skip(PayloadParser *pp, uint32_t num);
|
||||
|
||||
/** Read uint16_t from the payload. */
|
||||
uint16_t pp_u16(PayloadParser *pp);
|
||||
|
||||
/** Read uint32_t from the payload. */
|
||||
uint32_t pp_u32(PayloadParser *pp);
|
||||
|
||||
/** Read int8_t from the payload. */
|
||||
int8_t pp_i8(PayloadParser *pp);
|
||||
|
||||
/** Read char (int8_t) from the payload. */
|
||||
static inline int8_t pp_char(PayloadParser *pp)
|
||||
{
|
||||
return pp_i8(pp);
|
||||
}
|
||||
|
||||
/** Read int16_t from the payload. */
|
||||
int16_t pp_i16(PayloadParser *pp);
|
||||
|
||||
/** Read int32_t from the payload. */
|
||||
int32_t pp_i32(PayloadParser *pp);
|
||||
|
||||
/** Read 4-byte float from the payload. */
|
||||
float pp_float(PayloadParser *pp);
|
||||
|
||||
/**
|
||||
* Parse a zero-terminated string
|
||||
*
|
||||
* @param pp - parser
|
||||
* @param buffer - target buffer
|
||||
* @param maxlen - buffer size
|
||||
* @return actual number of bytes, excluding terminator
|
||||
*/
|
||||
uint32_t pp_string(PayloadParser *pp, char *buffer, uint32_t maxlen);
|
||||
|
||||
/**
|
||||
* Parse a buffer
|
||||
*
|
||||
* @param pp - parser
|
||||
* @param buffer - target buffer
|
||||
* @param maxlen - buffer size
|
||||
* @return actual number of bytes, excluding terminator
|
||||
*/
|
||||
uint32_t pp_buf(PayloadParser *pp, uint8_t *buffer, uint32_t maxlen);
|
||||
|
||||
|
||||
#endif // PAYLOAD_PARSER_H
|
||||
@@ -0,0 +1,32 @@
|
||||
#ifndef TYPE_COERCE_H
|
||||
#define TYPE_COERCE_H
|
||||
|
||||
/**
|
||||
* Structs for conversion between types,
|
||||
* part of the TinyFrame utilities collection
|
||||
*
|
||||
* (c) Ondřej Hruška, 2016-2017. MIT license.
|
||||
*
|
||||
* This is a support header file for PayloadParser and PayloadBuilder.
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
|
||||
union conv8 {
|
||||
uint8_t u8;
|
||||
int8_t i8;
|
||||
};
|
||||
|
||||
union conv16 {
|
||||
uint16_t u16;
|
||||
int16_t i16;
|
||||
};
|
||||
|
||||
union conv32 {
|
||||
uint32_t u32;
|
||||
int32_t i32;
|
||||
float f32;
|
||||
};
|
||||
|
||||
#endif // TYPE_COERCE_H
|
||||
@@ -81,6 +81,9 @@ void MX_USART1_UART_Init(void);
|
||||
|
||||
/* USER CODE BEGIN Prototypes */
|
||||
|
||||
void MX_USART_DmaWaitReady(void);
|
||||
void MX_USART_DmaSend(uint8_t *buffer, uint32_t length);
|
||||
|
||||
/* USER CODE END Prototypes */
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
||||
+3
-3
@@ -95,13 +95,13 @@
|
||||
/*---------- -----------*/
|
||||
#define USBD_MAX_NUM_CONFIGURATION 1
|
||||
/*---------- -----------*/
|
||||
#define USBD_MAX_STR_DESC_SIZ 64
|
||||
#define USBD_MAX_STR_DESC_SIZ 128
|
||||
/*---------- -----------*/
|
||||
#define USBD_SUPPORT_USER_STRING 0
|
||||
#define USBD_SUPPORT_USER_STRING 1
|
||||
/*---------- -----------*/
|
||||
#define USBD_DEBUG_LEVEL 0
|
||||
/*---------- -----------*/
|
||||
#define USBD_SELF_POWERED 1
|
||||
#define USBD_SELF_POWERED 0
|
||||
/*---------- -----------*/
|
||||
#define MAX_STATIC_ALLOC_SIZE 512
|
||||
|
||||
|
||||
Reference in New Issue
Block a user