Juraj Ďuďák 2 лет назад
Родитель
Сommit
c1ae1538d9
7 измененных файлов с 308 добавлено и 280 удалено
  1. 2 0
      Inc/app_bridge.h
  2. 1 0
      Inc/nbus_impl.h
  3. 8 7
      Src/nbus_app.c
  4. 0 273
      Src/nbus_impl.c
  5. 69 0
      Src/nbus_impl_broadcast.c
  6. 137 0
      Src/nbus_impl_module_unicast.c
  7. 91 0
      Src/nbus_impl_sensor_unicast.c

+ 2 - 0
Inc/app_bridge.h

@@ -48,6 +48,8 @@ typedef struct{
 	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);
+	void (*start)();
+	void (*stop)();
 }nBusAppInterface_t;
 
 

+ 1 - 0
Inc/nbus_impl.h

@@ -10,6 +10,7 @@
 #include "nbus_types.h"
 #include "nbus_cmd.h"
 
+void setErrorResponse(nBus_TypeDef *nbus, uint8_t code);
 //nBusCommandType_t get_request_type();
 void nbus_unicastToSensorGet(nBus_TypeDef *nbus);
 void nbus_unicastToModuleGet(nBus_TypeDef *nBus);

+ 8 - 7
Src/nbus_app.c

@@ -103,18 +103,18 @@ static void process_request(){
 		return;
 	}
 
-	// paket nie je adresovany tomuto modulu
-	if(nBus.addressModule != MODULE_ADDRESS) {
-		nBus.send_response = NO_RESPONSE;
-		return;
-	}
-
 	// spracovanie broadcast komunikacie
 	if((request_type == BROADCAST_SPECIFIC_SENSORS || request_type == BROADCAST_GLOBAL)) {
 		nbus_broadcast(&nBus, request_type);
 		return;
 	}
 
+	// paket nie je adresovany tomuto modulu
+	if(nBus.addressModule != MODULE_ADDRESS) {
+		nBus.send_response = NO_RESPONSE;
+		return;
+	}
+
 	nBus.function_code.error = 0;
 	nBus.tx_length = META_SIZE;
 
@@ -210,6 +210,8 @@ void nbus_init(Peripheral_typeDef *periph, nBusAppInterface_t *interface){
 
 void nbus_init_app(void *hw_interface, void *hw_config){
 	nBus.interface->init(hw_interface, hw_config);
+
+	receiveOneByte();
 }
 
 void nbus_init_memory_driver(nBus_MemoryDriver *memDriver, uint16_t capacity){
@@ -218,7 +220,6 @@ void nbus_init_memory_driver(nBus_MemoryDriver *memDriver, uint16_t capacity){
 }
 
 void nbus_stack(void){
-	receiveOneByte();
 
 	while(1){
 		if(nBus.uart_state == UART_RECEIVED){

+ 0 - 273
Src/nbus_impl.c

@@ -1,273 +0,0 @@
-#include "nbus_impl.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 CMD_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;
-				}
-			}
-
-		}
-		// alebo
-		nbus->memoryInterface->getParam(nbus->sensorInfo.address, 0x05);
-	}
-	break;
-
-	case CMD_DATA:
-	{
-		uint8_t cnt = nbus->interface->getData(nbus->sensorInfo.address, &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){
-	switch(nbus->function_code.function){
-
-	case CMD_ECHO:
-	{
-		for(uint8_t i=3 ; i<nbus->rx_length-1 ; i++){
-			nbus->tx_buffer[i+1] = nbus->rx_buffer[i];
-		}
-		nbus->tx_length += (nbus->rx_length-4);
-	}
-
-	break;
-
-	case CMD_SENSOR_CNT:
-	{
-		nbus->tx_buffer[4] = nbus->interface->getSensorCount();
-		nbus->tx_length += 1;
-	}
-	break;
-
-	case CMD_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;
-
-	case CMD_INFO:
-	{
-		switch(nbus->rx_buffer[3]){
-		case INFO_MODULE_NAME:
-			nbus->tx_buffer[4] = MODULE_NAME[0];
-			nbus->tx_buffer[5] = MODULE_NAME[1];
-			nbus->tx_buffer[6] = MODULE_NAME[2];
-			nbus->tx_buffer[7] = MODULE_NAME[3];
-			nbus->tx_buffer[8] = MODULE_NAME[4];
-			nbus->tx_buffer[9] = MODULE_NAME[5];
-			nbus->tx_buffer[10] = MODULE_NAME[6];
-			nbus->tx_buffer[11] = MODULE_NAME[7];
-			nbus->tx_length += 8;
-			break;
-		case INFO_MODULE_TYPE:
-			nbus->tx_buffer[4] = MODULE_TYPE[0];
-			nbus->tx_buffer[5] = MODULE_TYPE[1];
-			nbus->tx_buffer[6] = MODULE_TYPE[2];
-			nbus->tx_length += 3;
-			break;
-		case INFO_MODULE_UUID:
-			// Reference manual: Unique device ID registers
-			uint32_t (*unique_id_3) = (uint32_t*)(0x1FF80064); // BASE address + 0x14 0ffset
-			*(nbus->tx_buffer) = (uint32_t)unique_id_3;
-			nbus->tx_length += 4;
-			break;
-		case INFO_MODULE_FW:
-			nbus->tx_buffer[4] = VERSION_FW[0];
-			nbus->tx_buffer[5] = '.';
-			nbus->tx_buffer[6] = VERSION_FW[1];
-			nbus->tx_length += 3;
-			break;
-		case INFO_MODULE_HW:
-			nbus->tx_buffer[4] = VERSION_HW[0];
-			nbus->tx_buffer[5] = '.';
-			nbus->tx_buffer[6] = VERSION_HW[1];
-			nbus->tx_length += 3;
-			break;
-		}
-	}
-	break;
-
-	default:
-	{
-		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;
-	}
-	break;
-
-	case CMD_SLEEP:
-	{
-		nbus->tx_buffer[4] = OK_CODE;
-		nbus->tx_length += 1;
-	}
-	break;
-
-	case CMD_WAKEUP:
-	{
-		nbus->tx_buffer[4] = OK_CODE;
-		nbus->tx_length += 1;
-	}
-	break;
-
-	default:
-	{
-		setErrorResponse(nbus, ILLEGAL_FUNCTION);
-	}
-	}
-
-}
-
-void nbus_broadcast(nBus_TypeDef *nbus, nBusCommandType_t request_type){
-	if(request_type == BROADCAST_SPECIFIC_SENSORS) {
-
-	}
-
-	if(request_type == BROADCAST_GLOBAL) {
-		switch(nbus->function_code.function) {
-#if MODULE_MASTER
-		case CMD_SYNC:
-		{
-			if(nbus->rx_length < 11){
-				return;
-			}
-
-			RTC_TimeTypeDef sTime = {0};
-			RTC_DateTypeDef sDate = {0};
-
-			sTime.Hours = nbus->rx_buffer[7];
-			sTime.Minutes = nbus->rx_buffer[8];
-			sTime.Seconds = nbus->rx_buffer[9];
-			sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
-			sTime.StoreOperation = RTC_STOREOPERATION_RESET;
-			HAL_RTC_SetTime(nbus->periph->rtc, &sTime, RTC_FORMAT_BCD);
-
-			sDate.WeekDay = nbus->rx_buffer[6];
-			sDate.Date = nbus->rx_buffer[5];
-			sDate.Month = nbus->rx_buffer[4];
-			sDate.Year = nbus->rx_buffer[3];
-
-			HAL_RTC_SetDate(nbus->periph->rtc, &sDate, RTC_FORMAT_BCD);
-
-		}
-		break;
-#endif
-		case CMD_START:
-		{
-			nbus->measure_active = MEASURE_RUNNING;
-		}
-		break;
-
-		case CMD_STOP:
-		{
-			nbus->measure_active = MEASURE_STOPPED;
-		}
-		break;
-
-		default:
-			; // nothing
-		}
-
-	}
-
-	nbus->send_response = NO_RESPONSE;
-}

+ 69 - 0
Src/nbus_impl_broadcast.c

@@ -0,0 +1,69 @@
+#include "nbus_impl.h"
+
+
+inline void setErrorResponse(nBus_TypeDef *nbus, uint8_t code){
+	nbus->function_code.error = 1;
+	nbus->tx_buffer[4] = code;
+	nbus->tx_length += 1;
+}
+
+
+
+void nbus_broadcast(nBus_TypeDef *nbus, nBusCommandType_t request_type){
+	if(request_type == BROADCAST_SPECIFIC_SENSORS) {
+
+	}
+
+	if(request_type == BROADCAST_GLOBAL) {
+		switch(nbus->function_code.function) {
+#if MODULE_MASTER
+		case CMD_SYNC:
+		{
+			if(nbus->rx_length < 11){
+				return;
+			}
+
+			RTC_TimeTypeDef sTime = {0};
+			RTC_DateTypeDef sDate = {0};
+
+			sTime.Hours = nbus->rx_buffer[7];
+			sTime.Minutes = nbus->rx_buffer[8];
+			sTime.Seconds = nbus->rx_buffer[9];
+			sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
+			sTime.StoreOperation = RTC_STOREOPERATION_RESET;
+			HAL_RTC_SetTime(nbus->periph->rtc, &sTime, RTC_FORMAT_BCD);
+
+			sDate.WeekDay = nbus->rx_buffer[6];
+			sDate.Date = nbus->rx_buffer[5];
+			sDate.Month = nbus->rx_buffer[4];
+			sDate.Year = nbus->rx_buffer[3];
+
+			HAL_RTC_SetDate(nbus->periph->rtc, &sDate, RTC_FORMAT_BCD);
+
+		}
+		break;
+#endif
+		case CMD_START:
+		{
+			nbus->measure_active = MEASURE_RUNNING;
+			nbus->interface->start();
+			HAL_GPIO_WritePin(nbus->periph->led->port, nbus->periph->led->pin, GPIO_PIN_SET);
+		}
+		break;
+
+		case CMD_STOP:
+		{
+			nbus->measure_active = MEASURE_STOPPED;
+			nbus->interface->stop();
+			HAL_GPIO_WritePin(nbus->periph->led->port, nbus->periph->led->pin, GPIO_PIN_RESET);
+		}
+		break;
+
+		default:
+			; // nothing
+		}
+
+	}
+
+	nbus->send_response = NO_RESPONSE;
+}

+ 137 - 0
Src/nbus_impl_module_unicast.c

@@ -0,0 +1,137 @@
+#include "nbus_impl.h"
+
+
+
+
+void nbus_unicastToModuleGet(nBus_TypeDef *nbus){
+	switch(nbus->function_code.function){
+
+	case CMD_ECHO:
+	{
+		for(uint8_t i=3 ; i<nbus->rx_length-1 ; i++){
+			nbus->tx_buffer[i+1] = nbus->rx_buffer[i];
+		}
+		nbus->tx_length += (nbus->rx_length-4);
+	}
+
+	break;
+
+	case CMD_SENSOR_CNT:
+	{
+		nbus->tx_buffer[4] = nbus->interface->getSensorCount();
+		nbus->tx_length += 1;
+	}
+	break;
+
+	case CMD_DATA:
+	{
+		//response: sensor1_index:sensor1_data | sensor2_index:sensor2_data | ...
+		nbus->tx_length +=  nbus->interface->getData(0 , &nbus->tx_buffer[4]);
+	}
+	break;
+
+	case CMD_SENSOR_TYPE:
+	{
+		//response: sensor1_index:sensor1_type | sensor2_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;
+
+	case CMD_INFO:
+	{
+		switch(nbus->rx_buffer[3]){
+		case INFO_MODULE_NAME:
+			nbus->tx_buffer[4] = MODULE_NAME[0];
+			nbus->tx_buffer[5] = MODULE_NAME[1];
+			nbus->tx_buffer[6] = MODULE_NAME[2];
+			nbus->tx_buffer[7] = MODULE_NAME[3];
+			nbus->tx_buffer[8] = MODULE_NAME[4];
+			nbus->tx_buffer[9] = MODULE_NAME[5];
+			nbus->tx_buffer[10] = MODULE_NAME[6];
+			nbus->tx_buffer[11] = MODULE_NAME[7];
+			nbus->tx_length += 8;
+			break;
+		case INFO_MODULE_TYPE:
+			nbus->tx_buffer[4] = MODULE_TYPE[0];
+			nbus->tx_buffer[5] = MODULE_TYPE[1];
+			nbus->tx_buffer[6] = MODULE_TYPE[2];
+			nbus->tx_length += 3;
+			break;
+		case INFO_MODULE_UUID:
+			// Reference manual: Unique device ID registers
+			uint32_t (*unique_id_3) = (uint32_t*)(0x1FF80064); // BASE address + 0x14 0ffset
+			*(nbus->tx_buffer) = (uint32_t)unique_id_3;
+			nbus->tx_length += 4;
+			break;
+		case INFO_MODULE_FW:
+			nbus->tx_buffer[4] = VERSION_FW[0];
+			nbus->tx_buffer[5] = '.';
+			nbus->tx_buffer[6] = VERSION_FW[1];
+			nbus->tx_length += 3;
+			break;
+		case INFO_MODULE_HW:
+			nbus->tx_buffer[4] = VERSION_HW[0];
+			nbus->tx_buffer[5] = '.';
+			nbus->tx_buffer[6] = VERSION_HW[1];
+			nbus->tx_length += 3;
+			break;
+		}
+	}
+	break;
+
+	default:
+	{
+		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;
+	}
+	break;
+
+	case CMD_SLEEP:
+	{
+		nbus->tx_buffer[4] = OK_CODE;
+		nbus->tx_length += 1;
+	}
+	break;
+
+	case CMD_WAKEUP:
+	{
+		nbus->tx_buffer[4] = OK_CODE;
+		nbus->tx_length += 1;
+	}
+	break;
+
+	default:
+	{
+		setErrorResponse(nbus, ILLEGAL_FUNCTION);
+	}
+	}
+
+}

+ 91 - 0
Src/nbus_impl_sensor_unicast.c

@@ -0,0 +1,91 @@
+#include "nbus_impl.h"
+
+
+
+void nbus_unicastToSensorGet(nBus_TypeDef *nbus){
+	switch(nbus->function_code.function){
+	case CMD_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;
+				}
+			}
+
+		}
+		// alebo
+		nbus->memoryInterface->getParam(nbus->sensorInfo.address, 0x05);
+	}
+	break;
+
+	case CMD_DATA:
+	{
+		uint8_t cnt = nbus->interface->getData(nbus->sensorInfo.address, &nbus->tx_buffer[4]);
+		if (cnt == 0){
+			setErrorResponse(nbus, DEVICE_BUSY);
+			return;
+		}
+		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);
+	}
+	}
+}