|
|
@@ -1,4 +1,5 @@
|
|
|
#include "nbus_app.h"
|
|
|
+#include <string.h>
|
|
|
|
|
|
nBus_TypeDef nBus;
|
|
|
|
|
|
@@ -38,17 +39,8 @@ static uint8_t crc8x_fast(void const *mem, uint16_t len) {
|
|
|
}
|
|
|
|
|
|
|
|
|
-inline static void receiveOneByte(){
|
|
|
- nBus.hw_platform->uart_abort_receive();
|
|
|
- nBus.hw_platform->uart_receive(nBus.rx_buffer, 1);
|
|
|
- nBus.uart_state = UART_RX_1B;
|
|
|
- nBus.rx_length = 1;
|
|
|
-}
|
|
|
-
|
|
|
-inline static void receiveNBytes(uint8_t n){
|
|
|
- nBus.rx_length = n;
|
|
|
- nBus.hw_platform->uart_receive(nBus.rx_buffer, nBus.rx_length);
|
|
|
- nBus.uart_state = UART_RX_PAYLOAD;
|
|
|
+inline static void receivePacket(void){
|
|
|
+ nBus.hw_platform->uart_receive(nBus.rx_buffer, 64);
|
|
|
}
|
|
|
|
|
|
inline static void send_response(){
|
|
|
@@ -156,40 +148,7 @@ static void process_request(){
|
|
|
|
|
|
}
|
|
|
|
|
|
-/* -------------------------------------------------------- */
|
|
|
-/* ----------------------- CALLBACKS----------------------- */
|
|
|
-/* -------------------------------------------------------- */
|
|
|
-
|
|
|
-void nbus_cb_UART_RX(void){
|
|
|
- if(nBus.uart_state == UART_RX_PAYLOAD) {
|
|
|
- nBus.uart_state = UART_RECEIVED;
|
|
|
- nBus.hw_platform->timer_uart_stop();
|
|
|
- }
|
|
|
-
|
|
|
- if(nBus.uart_state == UART_RX_1B) {
|
|
|
- nBus.hw_platform->led_on();
|
|
|
- if(nBus.rx_buffer[0] > 0) {
|
|
|
- receiveNBytes(nBus.rx_buffer[0]);
|
|
|
- nBus.hw_platform->timer_uart_stop();
|
|
|
- nBus.hw_platform->timer_uart_start(nBus.rx_buffer[0]);
|
|
|
- } else {
|
|
|
- receiveOneByte();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-void nbus_cb_TIM_periodElapsed(void) {
|
|
|
- if(nBus.uart_state == UART_RX_PAYLOAD) {
|
|
|
- nBus.hw_platform->led_off();
|
|
|
- nBus.hw_platform->timer_uart_stop();
|
|
|
- nBus.hw_platform->uart_abort_receive();
|
|
|
- receiveOneByte();
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-void nbus_blink_LED(uint8_t delay) {
|
|
|
+static void nbus_blink_LED(uint8_t delay) {
|
|
|
nBus.hw_platform->led_on();
|
|
|
nBus.hw_platform->delay_ms(delay);
|
|
|
nBus.hw_platform->led_off();
|
|
|
@@ -200,10 +159,34 @@ void nbus_blink_LED(uint8_t delay) {
|
|
|
nBus.hw_platform->delay_ms(delay);
|
|
|
}
|
|
|
|
|
|
+/* -------------------------------------------------------- */
|
|
|
+/* ----------------------- CALLBACKS----------------------- */
|
|
|
+/* -------------------------------------------------------- */
|
|
|
+
|
|
|
+/**
|
|
|
+ * @brief UART receive complete.
|
|
|
+ * This callback have to valled from application, when RX data is ready.
|
|
|
+ * @param int size Size of received packet
|
|
|
+ * Received packet is located in uBus.rx_buffer
|
|
|
+ */
|
|
|
+void nbus_cb_UART_RX(int size){
|
|
|
+ nBus.rx_length = size;
|
|
|
+ nBus.uart_state = UART_RX_RECEIVED;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
/* -------------------------------------------------------- */
|
|
|
/* ------------------ PUBLIC FUNCTIONS-------------------- */
|
|
|
/* -------------------------------------------------------- */
|
|
|
|
|
|
+/**
|
|
|
+ * @brief Initialize of nBus stack
|
|
|
+ * @param interface Driver for module application.
|
|
|
+ * Every application have to implement base functions as Init, Reset, GetData, SetData, GetParam, SetParam, .... @see nBusAppInterface_t
|
|
|
+ * These are application dependent functions.
|
|
|
+ * @param hw Application level implementation of base functions (uart_receive, uart_transmit, led_on/off/toggle, ...).
|
|
|
+ * These are MCU dependent functions.
|
|
|
+ */
|
|
|
void nbus_init(nBusAppInterface_t *interface, nBusPlatformInterface_t *hw){
|
|
|
nBus.hw_platform = hw;
|
|
|
|
|
|
@@ -211,37 +194,62 @@ void nbus_init(nBusAppInterface_t *interface, nBusPlatformInterface_t *hw){
|
|
|
nBus.data_timebase = 0;
|
|
|
nBus.measure_active = MEASURE_STOPPED;
|
|
|
|
|
|
- // init restart timer
|
|
|
- nBus.uart_state = UART_RX_1B;
|
|
|
- nBus.hw_platform->timer_uart_start(100); // dummy value
|
|
|
- nBus.hw_platform->timer_uart_stop();
|
|
|
+ nBus.uart_state = UART_RX_WAIT;
|
|
|
|
|
|
nBus.interface = interface;
|
|
|
- receiveOneByte();
|
|
|
+
|
|
|
+ receivePacket();
|
|
|
|
|
|
}
|
|
|
|
|
|
+/**
|
|
|
+ * @brief Initialize concrete application, if it is needed.
|
|
|
+ * This function call "init()" function from nBusPlatformInterface_t, and this function is implemented in concrete application.
|
|
|
+ * @param hw_interface Pointer to hardware definition structure. In STM32 it can be hSPI, hUART, hADC, ...
|
|
|
+ * @param hw_config configuration for hardware structure object.
|
|
|
+ */
|
|
|
void nbus_init_app(void *hw_interface, void *hw_config){
|
|
|
nBus.interface->init(hw_interface, hw_config);
|
|
|
}
|
|
|
|
|
|
+/**
|
|
|
+ * @brief Init memory driver for storing module/sensor parameters.
|
|
|
+ * Implementation of memory driver is independent from nBus. To provide nonvolatile parameters store, is nneded implement this driver.
|
|
|
+ * @param memDriver Memory driver for bas operation: read/write 1/2/4 byte from/to memory
|
|
|
+ * @param capacity TBD
|
|
|
+ * @todo imeplement capacity parameter
|
|
|
+ */
|
|
|
void nbus_init_memory_driver(nBus_MemoryDriver *memDriver, uint16_t capacity){
|
|
|
nbus_memory_init(memDriver);
|
|
|
nBus.memoryInterface = getnbusMemoryInterface();
|
|
|
nbus_blink_LED(100);
|
|
|
}
|
|
|
|
|
|
+/**
|
|
|
+ * @brief Run protocol stack.
|
|
|
+ * This is infinity protocol loop.
|
|
|
+ * When stack is started (or reset) it blink as boot sign.
|
|
|
+ *
|
|
|
+ * Content of loop:
|
|
|
+ * - if data was received: process request. Determine addressee and type of request
|
|
|
+ * - execute request or discard request
|
|
|
+ * - send response (only for unicast requests)
|
|
|
+ *
|
|
|
+ * In each iteration, the state of application flags are performed. The state of application is noticed from loop_callback() (if it is defined).
|
|
|
+ * Meaning of return values:
|
|
|
+ * - 0 - No event occurs,
|
|
|
+ * - 1 - data from connected sensors are prepared, it will be read with read() function automatically.
|
|
|
+ * - 2 - communication error/timeout. The reception of packed process will be reset.
|
|
|
+ */
|
|
|
void nbus_stack(void){
|
|
|
|
|
|
nbus_blink_LED(50);
|
|
|
|
|
|
while(1){
|
|
|
- if(nBus.uart_state == UART_RECEIVED){
|
|
|
+ if(nBus.uart_state == UART_RX_RECEIVED){
|
|
|
|
|
|
process_request();
|
|
|
-
|
|
|
- // ready for next receiving
|
|
|
- receiveOneByte();
|
|
|
+ nBus.uart_state = UART_RX_WAIT;
|
|
|
|
|
|
send_response();
|
|
|
#if MODULE_MASTER == 1
|
|
|
@@ -250,8 +258,13 @@ void nbus_stack(void){
|
|
|
}
|
|
|
|
|
|
if (nBus.hw_platform->loop_callback != NULL) {
|
|
|
- if (nBus.hw_platform->loop_callback() == 1){
|
|
|
+ switch (nBus.hw_platform->loop_callback()){
|
|
|
+ case 1:
|
|
|
nBus.interface->read();
|
|
|
+ break;
|
|
|
+ case 2:
|
|
|
+ nBus.uart_state = UART_RX_WAIT;
|
|
|
+ break;
|
|
|
}
|
|
|
}
|
|
|
}
|