Explorar el Código

basic communication

Juraj Ďuďák hace 2 años
padre
commit
e5055769bf
Se han modificado 6 ficheros con 149 adiciones y 77 borrados
  1. 2 6
      Inc/nbus_app.h
  2. 1 3
      Inc/nbus_cmd.h
  3. 6 2
      Inc/nbus_impl.h
  4. 2 1
      Inc/nbus_types.h
  5. 88 58
      Src/nbus_app.c
  6. 50 7
      Src/nbus_impl.c

+ 2 - 6
Inc/nbus_app.h

@@ -12,13 +12,9 @@
 #include "nbus_cmd.h"
 
 void nbus_init(Peripheral_typeDef *periph);
-void nbus_cb_UART_RX(UART_HandleTypeDef *huart);
-void nbus_cb_TIM_periodElapsed(TIM_HandleTypeDef *htim);
-void nbus_cb_TIM_periodElapsed_base(TIM_HandleTypeDef *htim);
-void receiveOneByte();
-void process_request();
-
 void nbus_stack(void);
 
+
+
 #endif
 

+ 1 - 3
Inc/nbus_cmd.h

@@ -32,14 +32,12 @@
 
 #define OK_CODE 				(0x00)
 #define ILLEGAL_FUNCTION 		(0x01)
-#define ILLEGAL_DEVICE_ADDRESS 	(0x02)
+#define ILLEGAL_DEVICE_ADDRESS 	(0x02)  // ?
 #define ILLEGAL_DATA_VALUE 		(0x03)
 #define SLAVE_DEVICE_FAILURE 	(0x04)
 #define ACKNOWLEDGE 			(0x05)
 #define DEVICE_BUSY 			(0x06)
 #define PARAM_NOT_IMPLEMENTED 	(0x10)
-#define ERR_VALUE_WRITE 		(0x11)
-#define DEVICE_LOW_VOLTAGE 		(0x12)
 
 /**
  * Chyba pri vykonaní príkazu v subslave, napr. ak senzor nie je pripojený.

+ 6 - 2
Inc/nbus_impl.h

@@ -11,8 +11,12 @@
 #include "nbus_cmd.h"
 
 //nBusCommandType_t get_request_type();
-nBus_functionCode_t* nbus_unicastToSensor(nBus_TypeDef *nbus, nBus_functionCode_t *code);
-nBus_functionCode_t* nbus_unicastToModule(nBus_TypeDef *nBus, nBus_functionCode_t *code);
+void nbus_unicastToSensorGet(nBus_TypeDef *nbus);
+void nbus_unicastToModuleGet(nBus_TypeDef *nBus);
+
+void nbus_unicastToSensorSet(nBus_TypeDef *nbus);
+void nbus_unicastToModuleSet(nBus_TypeDef *nBus);
+
 void nbus_broadcast(nBus_TypeDef *nBus, nBusCommandType_t request_type);
 #endif
 

+ 2 - 1
Inc/nbus_types.h

@@ -27,7 +27,8 @@
  */
 #define BUFF_SIZE (PAYLOAD_SIZE + META_SIZE)
 
-
+#define CMD_FLAG_GET	1
+#define CMD_FLAG_SET	0
 /**
 * @brief Symbolické konštanty pre príznak pripravenosti dát
 */

+ 88 - 58
Src/nbus_app.c

@@ -23,7 +23,11 @@ static uint8_t const crc8x_table[] = {
 };
 
 
-uint8_t crc8x_fast(void const *mem, uint16_t len) {
+/* -------------------------------------------------------- */
+/* ------------------ PRIVATE FUNCTIONS-------------------- */
+/* -------------------------------------------------------- */
+
+static uint8_t crc8x_fast(void const *mem, uint16_t len) {
     uint8_t crc = CRC8_INIT_VALUE;
    	uint8_t const *data = (uint8_t*)mem;
     if (data == NULL)
@@ -34,27 +38,13 @@ uint8_t crc8x_fast(void const *mem, uint16_t len) {
     return crc;
 }
 
-void nbus_init(Peripheral_typeDef *periph){
-  nBus.periph = periph;
-
-  HAL_TIM_RegisterCallback(nBus.periph->uart_timer, HAL_TIM_PERIOD_ELAPSED_CB_ID, nbus_cb_TIM_periodElapsed);
-  HAL_TIM_RegisterCallback(nBus.periph->measure_timer, HAL_TIM_PERIOD_ELAPSED_CB_ID, nbus_cb_TIM_periodElapsed_base);
-  HAL_UART_RegisterCallback(nBus.periph->huart, HAL_UART_RX_COMPLETE_CB_ID, nbus_cb_UART_RX);
 
-  nBus.rx_length = 0;
-  nBus.data_timebase = 0;
-  nBus.measure_active = MEASURE_STOPPED;
 
-  // init restart timer
-  nBus.uart_state = UART_RX_1B;
-  HAL_TIM_Base_Start_IT(periph->uart_timer);
-  HAL_TIM_Base_Stop_IT(periph->uart_timer);
-
-}
-
-inline void receiveOneByte(){
+inline static void receiveOneByte(){
+	HAL_UART_AbortReceive_IT(nBus.periph->huart);
 	HAL_UART_Receive_IT(nBus.periph->huart, nBus.rx_buffer, 1);
 	nBus.uart_state = UART_RX_1B;
+	nBus.rx_length = 1;
 }
 
 inline static void receiveNBytes(uint8_t n){
@@ -69,43 +59,14 @@ inline static void send_response(){
 #if USE_USART_DMA_TX == 1
 		HAL_UART_Transmit_DMA(nBus.periph->huart, nBus.tx_buffer, nBus.tx_length);
 #else
-		HAL_UART_Transmi(nBus.periph->huart, nBus.tx_buffer, nBus.tx_length, 10);
+		HAL_UART_Transmit(nBus.periph->huart, nBus.tx_buffer, nBus.tx_length, 10);
 #endif
 	}
+	HAL_GPIO_WritePin(nBus.periph->led->port, nBus.periph->led->pin, GPIO_PIN_RESET);
 }
 
-void nbus_cb_UART_RX(UART_HandleTypeDef *huart){
-	if(nBus.uart_state == UART_RX_PAYLOAD) {
-		nBus.uart_state = UART_RECEIVED;
-		HAL_GPIO_WritePin(nBus.periph->led->port, nBus.periph->led->pin, GPIO_PIN_RESET);
-		HAL_TIM_Base_Stop_IT(nBus.periph->uart_timer);
-	}
-
-	if(nBus.uart_state == UART_RX_1B) {
-		HAL_GPIO_WritePin(nBus.periph->led->port, nBus.periph->led->pin, GPIO_PIN_SET);
-		receiveNBytes(nBus.rx_buffer[0]);
-
-		HAL_TIM_Base_Stop_IT(nBus.periph->uart_timer);
-		nBus.periph->uart_timer->Instance->CNT = 1;
-		HAL_TIM_Base_Start_IT(nBus.periph->uart_timer);
-	}
-
-}
-
-void nbus_cb_TIM_periodElapsed_base(TIM_HandleTypeDef *htim) {
-	HAL_GPIO_TogglePin(nBus.periph->led->port, nBus.periph->led->pin);
-}
-
-void nbus_cb_TIM_periodElapsed(TIM_HandleTypeDef *htim) {
-	if(nBus.uart_state == UART_RX_PAYLOAD) {
-		HAL_TIM_Base_Stop_IT(nBus.periph->uart_timer);
-		HAL_UART_AbortReceive_IT(nBus.periph->huart);
-		receiveOneByte();
-	}
-}
 
 static nBusCommandType_t get_request_type(){
-
 	nBus.addressModule = nBus.rx_buffer[0];
 	nBus.sensorInfo = *(const nBus_sensorByte_t*)&nBus.rx_buffer[1];
 	nBus.function_code = *(const nBus_functionCode_t*)&nBus.rx_buffer[2];
@@ -129,9 +90,8 @@ static nBusCommandType_t get_request_type(){
 	return BROADCAST_GLOBAL;
 }
 
-void process_request(){
+static void process_request(){
 	nBusCommandType_t request_type = get_request_type();
-	nBus_functionCode_t *fc = &nBus.function_code;
 	nBus.send_response = SEND_RESPONSE;
 
 	nBus.tx_buffer[0] = 0;
@@ -156,28 +116,98 @@ void process_request(){
 		return;
 	}
 
-	fc->error = 0;
+	nBus.function_code.error = 0;
 	nBus.tx_length = META_SIZE;
 
 	if(nBus.function_code.notReadWrite == REQUEST_GET) {
 		if(request_type == UNICAST_TO_SENSOR) {
-			fc = nbus_unicastToSensor(&nBus, fc);
+			nbus_unicastToSensorGet(&nBus);
 		}
 
 		if(request_type == UNICAST_TO_MODULE) {
-			fc = nbus_unicastToModule(&nBus, fc);
+			nbus_unicastToModuleGet(&nBus);
 		}
 
-		nBus.tx_buffer[3] = *(uint8_t*)&fc;
-		nBus.tx_buffer[nBus.tx_length-1] = crc8x_fast(&nBus.tx_buffer[1], nBus.tx_length-2);
-		nBus.tx_buffer[0] = nBus.tx_length;
+	} else {
+		// else request is REQUEST_SET
+		if(request_type == UNICAST_TO_SENSOR) {
+			nbus_unicastToSensorSet(&nBus);
+		}
 
-	} else {  // else request if REQUEST_SET
+		if(request_type == UNICAST_TO_MODULE) {
+			nbus_unicastToModuleSet(&nBus);
+		}
+	}
 
+	nBus.tx_buffer[3] = *(uint8_t*)&nBus.function_code;
+	nBus.tx_buffer[nBus.tx_length-1] = crc8x_fast(&nBus.tx_buffer[1], nBus.tx_length-2);
+	nBus.tx_buffer[0] = nBus.tx_length;
+
+}
+
+/* -------------------------------------------------------- */
+/* ----------------------- CALLBACKS----------------------- */
+/* -------------------------------------------------------- */
+static void nbus_cb_UART_RX(UART_HandleTypeDef *huart){
+	if(nBus.uart_state == UART_RX_PAYLOAD) {
+		nBus.uart_state = UART_RECEIVED;
+		HAL_TIM_Base_Stop_IT(nBus.periph->uart_timer);
+	}
+
+	if(nBus.uart_state == UART_RX_1B) {
+		HAL_GPIO_WritePin(nBus.periph->led->port, nBus.periph->led->pin, GPIO_PIN_SET);
+		if(nBus.rx_buffer[0] > 0) {
+			receiveNBytes(nBus.rx_buffer[0]);
+
+			HAL_TIM_Base_Stop_IT(nBus.periph->uart_timer);
+			nBus.periph->uart_timer->Instance->CNT = 1;
+			nBus.periph->uart_timer->Instance->ARR = 40*nBus.rx_buffer[0] + 400;
+			HAL_TIM_Base_Start_IT(nBus.periph->uart_timer);
+		} else {
+			receiveOneByte();
+		}
 	}
 
 }
 
+static void nbus_cb_TIM_periodElapsed_base(TIM_HandleTypeDef *htim) {
+	HAL_GPIO_TogglePin(nBus.periph->led->port, nBus.periph->led->pin);
+}
+
+static void nbus_cb_TIM_periodElapsed(TIM_HandleTypeDef *htim) {
+	if(nBus.uart_state == UART_RX_PAYLOAD) {
+		HAL_GPIO_WritePin(nBus.periph->led->port, nBus.periph->led->pin, GPIO_PIN_RESET);
+		HAL_TIM_Base_Stop_IT(nBus.periph->uart_timer);
+		HAL_UART_AbortReceive_IT(nBus.periph->huart);
+		receiveOneByte();
+	}
+}
+
+
+
+/* -------------------------------------------------------- */
+/* ------------------ PUBLIC FUNCTIONS-------------------- */
+/* -------------------------------------------------------- */
+
+void nbus_init(Peripheral_typeDef *periph){
+  nBus.periph = periph;
+
+  HAL_TIM_RegisterCallback(nBus.periph->uart_timer, HAL_TIM_PERIOD_ELAPSED_CB_ID, nbus_cb_TIM_periodElapsed);
+  HAL_TIM_RegisterCallback(nBus.periph->measure_timer, HAL_TIM_PERIOD_ELAPSED_CB_ID, nbus_cb_TIM_periodElapsed_base);
+  HAL_UART_RegisterCallback(nBus.periph->huart, HAL_UART_RX_COMPLETE_CB_ID, nbus_cb_UART_RX);
+
+  nBus.rx_length = 0;
+  nBus.data_timebase = 0;
+  nBus.measure_active = MEASURE_STOPPED;
+
+  // init restart timer
+  nBus.uart_state = UART_RX_1B;
+  HAL_TIM_Base_Start_IT(periph->uart_timer);
+  HAL_TIM_Base_Stop_IT(periph->uart_timer);
+
+}
+
+
 void nbus_stack(void){
 	receiveOneByte();
 

+ 50 - 7
Src/nbus_impl.c

@@ -1,12 +1,15 @@
 #include "nbus_impl.h"
 
 
-nBus_functionCode_t* nbus_unicastToSensor(nBus_TypeDef *nbus, nBus_functionCode_t *code){
-	return code;
+void nbus_unicastToSensorGet(nBus_TypeDef *nbus){
+	;
 }
 
-nBus_functionCode_t* nbus_unicastToModule(nBus_TypeDef *nbus, nBus_functionCode_t *code){
+void nbus_unicastToSensorSet(nBus_TypeDef *nbus){
+	;
+}
 
+void nbus_unicastToModuleGet(nBus_TypeDef *nbus){
 	switch(nbus->function_code.function){
 	case CMD_VERSION:
 	{
@@ -26,17 +29,56 @@ nBus_functionCode_t* nbus_unicastToModule(nBus_TypeDef *nbus, nBus_functionCode_
 	}
 	break;
 
+	case CMD_PARAM:
+	{
+		nbus->tx_buffer[4] = OK_CODE;
+		nbus->tx_length += 1;
+	}
+	break;
+
 	default:
 	{
-		code->error = 1;
+		nbus->function_code.error = 1;
 		nbus->tx_buffer[4] = ILLEGAL_FUNCTION;
 		nbus->tx_length += 1;
 	}
 	}
 
-	return code;
+
 }
 
+void nbus_unicastToModuleSet(nBus_TypeDef *nbus){
+	switch(nbus->function_code.function){
+	case CMD_PARAM:
+	{
+		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:
+	{
+		nbus->function_code.error = 1;
+		nbus->tx_buffer[4] = ILLEGAL_FUNCTION;
+		nbus->tx_length += 1;
+	}
+	}
+
+}
 
 void nbus_broadcast(nBus_TypeDef *nbus, nBusCommandType_t request_type){
 	if(request_type == BROADCAST_SPECIFIC_SENSORS) {
@@ -45,6 +87,7 @@ void nbus_broadcast(nBus_TypeDef *nbus, nBusCommandType_t request_type){
 
 	if(request_type == BROADCAST_GLOBAL) {
 		switch(nbus->function_code.function) {
+#if MODULE_MASTER
 		case CMD_SYNC:
 		{
 			if(nbus->rx_length < 11){
@@ -62,15 +105,15 @@ void nbus_broadcast(nBus_TypeDef *nbus, nBusCommandType_t request_type){
 			HAL_RTC_SetTime(nbus->periph->rtc, &sTime, RTC_FORMAT_BCD);
 
 			sDate.WeekDay = nbus->rx_buffer[6];
-			sDate.Month = nbus->rx_buffer[4];
 			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;