瀏覽代碼

bling implement most of functions

Juraj Ďuďák 2 年之前
父節點
當前提交
63a402e8b7
共有 7 個文件被更改,包括 219 次插入18 次删除
  1. 55 0
      Inc/app_bridge.h
  2. 3 1
      Inc/nbus_app.h
  3. 6 5
      Inc/nbus_cmd.h
  4. 3 0
      Inc/nbus_types.h
  5. 31 0
      Src/app_bridge.c
  6. 6 1
      Src/nbus_app.c
  7. 115 11
      Src/nbus_impl.c

+ 55 - 0
Inc/app_bridge.h

@@ -0,0 +1,55 @@
+/*
+ * app_bridge.h
+ *
+ *  Created on: Nov 2, 2023
+ *      Author: juraj
+ */
+
+#ifndef MODULES_NBUS_INC_APP_BRIDGE_H_
+#define MODULES_NBUS_INC_APP_BRIDGE_H_
+
+#include <stdint.h>
+
+typedef enum{
+	TYPE_UNKNOWN = 0xFF,
+	TYPE_ACCELEROMETER = 0,
+	TYPE_GYROSCOPE,
+	TYPE_MAGNETOMETER,
+	TYPE_TEMPERATURE,
+	TYPE_HUMIDITY,
+	TYPE_PRESSURE,
+	TYPE_HEART_RATE,
+}nBus_sensorType_t;
+
+typedef enum{
+	PARAM_NONE = 0xFF,
+	PARAM_TIMEBASE = 0,
+	PARAM_RESOLUTION,
+	PARAM_GAIN,
+	PARAM_OFFSET,
+	PARAM_SAMPLERATE,
+	PARAM_RANGE
+}nBus_param_t;
+
+
+nBus_param_t* nbus_interface_allParams();
+uint8_t nbus_interface_allParamsCount();
+
+nBus_sensorType_t *nbus_interface_allTypes();
+uint8_t nbus_interface_allTypesCount();
+
+typedef struct{
+	void (*init)(void *hw_interface, void *hw_config);
+	void (*reset)();
+	nBus_sensorType_t(*getType)(uint8_t sensor_index);
+	uint8_t (*getSensorCount)();
+	uint8_t (*getData)(uint8_t *data);
+	uint8_t (*setData)(uint8_t *data);
+	uint8_t (*hasParam)(uint8_t sensor_index, nBus_param_t param_name);
+	uint8_t (*getParam)(uint8_t sensor_index, nBus_param_t param_name);
+	nBus_param_t (*setParam)(uint8_t sensor_index, nBus_param_t param_name, uint8_t param_value);
+}nBusAppInterface_t;
+
+
+
+#endif /* MODULES_NBUS_INC_APP_BRIDGE_H_ */

+ 3 - 1
Inc/nbus_app.h

@@ -10,8 +10,10 @@
 #include "nbus_types.h"
 #include "nbus_impl.h"
 #include "nbus_cmd.h"
+#include "app_bridge.h"
 
-void nbus_init(Peripheral_typeDef *periph);
+void nbus_init(Peripheral_typeDef *periph, nBusAppInterface_t *interface);
+void nbus_init_app(void *hw_interface, void *hw_config);
 void nbus_stack(void);
 
 

+ 6 - 5
Inc/nbus_cmd.h

@@ -24,11 +24,12 @@
 #define CMD_SENSOR_CNT  0x05
 #define CMD_SLEEP		0x06
 #define CMD_WAKEUP		0x07
-#define CMD_CALIBRATE	0x09
-#define CMD_RESET		0x0A
-#define CMD_STORE		0x0B
-#define CMD_DATA		0x0C
-#define CMD_SYNC		0x0D
+#define CMD_CALIBRATE	0x08
+#define CMD_RESET		0x09
+#define CMD_STORE		0x0A
+#define CMD_DATA		0x0B
+#define CMD_SYNC		0x0C
+#define SENSOR_TYPE		0x0D
 
 #define OK_CODE 				(0x00)
 #define ILLEGAL_FUNCTION 		(0x01)

+ 3 - 0
Inc/nbus_types.h

@@ -11,6 +11,7 @@
 #include <stdint.h>
 #include "mcu_platform.h"
 #include "nbus_config.h"
+#include "app_bridge.h"
 
 /**
 * @brief Počet bajtov v pakete, ktoré tvoria réžiu.
@@ -214,6 +215,8 @@ typedef struct {
 
     volatile uint32_t data_timebase;
 
+    nBusAppInterface_t *interface;
+
 #if USE_USART_DMA_TX == 1
     /**
      * @brief Príznak ukončenia odoslania dát cez DMA

+ 31 - 0
Src/app_bridge.c

@@ -0,0 +1,31 @@
+/*
+ * app_bridge.c
+ *
+ *  Created on: Nov 2, 2023
+ *      Author: juraj
+ */
+#include "app_bridge.h"
+
+nBus_param_t allParams[] = {PARAM_TIMEBASE, PARAM_RESOLUTION, PARAM_GAIN, PARAM_OFFSET, PARAM_SAMPLERATE,PARAM_RANGE};
+nBus_sensorType_t allTypes[] = {TYPE_ACCELEROMETER, TYPE_GYROSCOPE, TYPE_MAGNETOMETER, TYPE_TEMPERATURE, TYPE_HUMIDITY, TYPE_PRESSURE, TYPE_HEART_RATE};
+
+nBus_param_t* nbus_interface_allParams()
+{
+	return allParams;
+}
+
+uint8_t nbus_interface_allParamsCount()
+{
+	return 6;
+}
+
+nBus_sensorType_t* nbus_interface_allTypes()
+{
+	return allTypes;
+}
+
+uint8_t nbus_interface_allTypesCount()
+{
+	return 7;
+}
+

+ 6 - 1
Src/nbus_app.c

@@ -189,7 +189,7 @@ static void nbus_cb_TIM_periodElapsed(TIM_HandleTypeDef *htim) {
 /* ------------------ PUBLIC FUNCTIONS-------------------- */
 /* -------------------------------------------------------- */
 
-void nbus_init(Peripheral_typeDef *periph){
+void nbus_init(Peripheral_typeDef *periph, nBusAppInterface_t *interface){
   nBus.periph = periph;
 
   HAL_TIM_RegisterCallback(nBus.periph->uart_timer, HAL_TIM_PERIOD_ELAPSED_CB_ID, nbus_cb_TIM_periodElapsed);
@@ -205,8 +205,13 @@ void nbus_init(Peripheral_typeDef *periph){
   HAL_TIM_Base_Start_IT(periph->uart_timer);
   HAL_TIM_Base_Stop_IT(periph->uart_timer);
 
+  nBus.interface = interface;
+
 }
 
+void nbus_init_app(void *hw_interface, void *hw_config){
+	nBus.interface->init(hw_interface, hw_config);
+}
 
 void nbus_stack(void){
 	receiveOneByte();

+ 115 - 11
Src/nbus_impl.c

@@ -1,12 +1,95 @@
 #include "nbus_impl.h"
+#include "app_bridge.h"
+
+
+inline static void setErrorResponse(nBus_TypeDef *nbus, uint8_t code){
+	nbus->function_code.error = 1;
+	nbus->tx_buffer[4] = code;
+	nbus->tx_length += 1;
+}
 
 
 void nbus_unicastToSensorGet(nBus_TypeDef *nbus){
-	;
+	switch(nbus->function_code.function){
+	case SENSOR_TYPE:
+	{
+		nBus_sensorType_t t = nbus->interface->getType(nbus->sensorInfo.address);
+		if (t == TYPE_UNKNOWN) {
+			setErrorResponse(nbus, ILLEGAL_DEVICE_ADDRESS);
+			break;
+		}
+		nbus->tx_buffer[4] = t;
+		nbus->tx_length += 1;
+	}
+	break;
+
+	case CMD_PARAM:
+	{
+		if (nbus->rx_length >= 5){
+			if (nbus->interface->hasParam(nbus->sensorInfo.address, (nBus_param_t)nbus->tx_buffer[3]) == 0) {
+				setErrorResponse(nbus, PARAM_NOT_IMPLEMENTED);
+				break;
+			}
+			nbus->tx_buffer[4] = nbus->tx_buffer[3];
+			nbus->tx_buffer[5] = nbus->interface->getParam(nbus->sensorInfo.address, (nBus_param_t)nbus->tx_buffer[3]);
+			nbus->tx_length += 2;
+		} else {
+			nBus_param_t* params = nbus_interface_allParams();
+			for(uint8_t i=0; i < nbus_interface_allParamsCount() ; i++){
+				if(nbus->interface->hasParam(i, params[i])){
+					nbus->tx_buffer[4+2*i] = params[i];
+					nbus->tx_buffer[5+2*i] = nbus->interface->getParam(i, params[i]);
+					nbus->tx_length += 2;
+				}
+				nbus->tx_buffer[4] = nbus->interface->getParam(nbus->rx_buffer[3], (nBus_param_t)nbus->tx_buffer[4]);
+				nbus->tx_length += 1;
+			}
+
+		}
+	}
+	break;
+
+	case CMD_DATA:
+	{
+		uint8_t cnt = nbus->interface->getData(&nbus->tx_buffer[4]);
+		nbus->tx_length += cnt;
+	}
+	break;
+
+	default:
+	{
+		setErrorResponse(nbus, ILLEGAL_FUNCTION);
+	}
+	}
 }
 
 void nbus_unicastToSensorSet(nBus_TypeDef *nbus){
-	;
+	switch(nbus->function_code.function){
+	case CMD_PARAM:
+	{
+		nBus_param_t p = nbus->interface->setParam(nbus->sensorInfo.address, (nBus_param_t)nbus->tx_buffer[3], nbus->tx_buffer[4]);
+		if (p == PARAM_NONE) {
+			setErrorResponse(nbus, PARAM_NOT_IMPLEMENTED);
+			break;
+		}
+		nbus->tx_buffer[4] = OK_CODE;
+		nbus->tx_length += 1;
+	}
+	break;
+
+	case CMD_DATA:
+	{
+		nbus->interface->setData(&nbus->rx_buffer[3]);
+		nbus->tx_buffer[4] = OK_CODE;
+		nbus->tx_length += 1;
+	}
+	break;
+
+	default:
+	{
+		setErrorResponse(nbus, ILLEGAL_FUNCTION);
+	}
+	}
 }
 
 void nbus_unicastToModuleGet(nBus_TypeDef *nbus){
@@ -27,30 +110,53 @@ void nbus_unicastToModuleGet(nBus_TypeDef *nbus){
 		}
 		nbus->tx_length += (nbus->rx_length-4);
 	}
+
 	break;
 
-	case CMD_PARAM:
+	case CMD_SENSOR_CNT:
 	{
-		nbus->tx_buffer[4] = OK_CODE;
+		nbus->tx_buffer[4] = nbus->interface->getSensorCount();
 		nbus->tx_length += 1;
 	}
 	break;
 
+	case SENSOR_TYPE:
+	{
+		//response: sensor1_index:sensor2_type | sensor1_index:sensor2_type | ...
+		for(uint8_t i = 0; i < nbus->interface->getSensorCount() ; i++){
+			nbus->tx_buffer[4+2*i] = i;
+			nbus->tx_buffer[5+2*i] = nbus->interface->getType(i);
+			nbus->tx_length += 2;
+		}
+	}
+	break;
+
 	default:
 	{
-		nbus->function_code.error = 1;
-		nbus->tx_buffer[4] = ILLEGAL_FUNCTION;
-		nbus->tx_length += 1;
+		setErrorResponse(nbus, ILLEGAL_FUNCTION);
 	}
 	}
 
-
 }
 
 void nbus_unicastToModuleSet(nBus_TypeDef *nbus){
 	switch(nbus->function_code.function){
 	case CMD_PARAM:
 	{
+		//same as nbus_unicastToSensorSet
+		nBus_param_t p = nbus->interface->setParam(nbus->sensorInfo.address, (nBus_param_t)nbus->tx_buffer[3], nbus->tx_buffer[4]);
+		if (p == PARAM_NONE) {
+			setErrorResponse(nbus, PARAM_NOT_IMPLEMENTED);
+			break;
+		}
+		nbus->tx_buffer[4] = OK_CODE;
+		nbus->tx_length += 1;
+	}
+	break;
+
+	case CMD_DATA:
+	{
+		nbus->interface->setData(&nbus->rx_buffer[3]);
 		nbus->tx_buffer[4] = OK_CODE;
 		nbus->tx_length += 1;
 	}
@@ -72,9 +178,7 @@ void nbus_unicastToModuleSet(nBus_TypeDef *nbus){
 
 	default:
 	{
-		nbus->function_code.error = 1;
-		nbus->tx_buffer[4] = ILLEGAL_FUNCTION;
-		nbus->tx_length += 1;
+		setErrorResponse(nbus, ILLEGAL_FUNCTION);
 	}
 	}