Exemplo n.º 1
0
void UART_Init(void)
{
  char *test_str = "\nUART_Init Done!\n";
	UartHandle.Instance        = USARTx;
	UartHandle.Init.BaudRate   = 115200;
	UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
	UartHandle.Init.StopBits   = UART_STOPBITS_1;
	UartHandle.Init.Parity     = UART_PARITY_NONE;
	UartHandle.Init.HwFlowCtl  = UART_HWCONTROL_NONE;
	UartHandle.Init.Mode       = UART_MODE_TX_RX;

	if(HAL_UART_Init(&UartHandle) != HAL_OK)
	{

	}
		/* Enable the UART Parity Error Interrupt */
	__HAL_UART_ENABLE_IT(&UartHandle, UART_IT_PE);

	/* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */
	__HAL_UART_ENABLE_IT(&UartHandle, UART_IT_ERR);

	/* Enable the UART Data Register not empty Interrupt */
	__HAL_UART_ENABLE_IT(&UartHandle, UART_IT_RXNE);

	#ifdef USE_DEBUG
	HAL_UART_SendBytes( (uint8_t *)test_str , strlen(test_str) );
	#endif
}
u16 APP_ProcessEvent( u8 task_id, u16 events )
{
	loraMAC_msg_t* pMsgSend = NULL;
  loraMAC_msg_t* pMsgRecieve = NULL;

        u8 tmp_buf[64];
        u8 len = 0 ;

        //system event
        if(events & SYS_EVENT_MSG)
        {
					//receive msg loop
					while(NULL != (pMsgRecieve = (loraMAC_msg_t*)osal_msg_receive(APP_taskID)))
					{
							//pMsgRecieve[0] is system event type
							switch(pMsgRecieve->msgID)
							{
							//tx done
							case TXDONE :
									//调度下一次发送
									//RedLED(ON);
									HalLedSet (HAL_LED_1, HAL_LED_MODE_ON);
									if(Txpacket_count > 0)
									{
										Txpacket_count--;
										HAL_UART_SendBytes(uart1_rxBuf,uart1_Rxcount);
										pMsgSend = (loraMAC_msg_t*)osal_msg_allocate(72);
										if(pMsgSend != NULL)
										{
											osal_memset(pMsgSend,0,72);//需要全部置0,要不然会出现发完串口数据包后不再进入TXDONE
											pMsgSend->msgID = TXREQUEST;
											pMsgSend->msgLen = uart1_Rxcount+2;
											osal_memcpy(pMsgSend->msgData,uart1_rxBuf,uart1_Rxcount);
											osal_msg_send(LoraMAC_taskID,(u8*)pMsgSend);
											osal_msg_deallocate((u8*)pMsgSend);
										}
									}
									else
									{
										//send a packet to LoRaMac osal (then can be send by the radio)
										pMsgSend = (loraMAC_msg_t*)osal_msg_allocate(72);
										if(pMsgSend != NULL)
										{
											osal_memset(pMsgSend,0,72);
											pMsgSend->msgID = TXREQUEST;
											pMsgSend->msgLen = 70;
											for(u8 dataCount = 0; dataCount < 70; dataCount++)
											{
												pMsgSend->msgData[dataCount] = dataCount;
											}
											osal_msg_send(LoraMAC_taskID,(u8*)pMsgSend);
											osal_msg_deallocate((u8*)pMsgSend);

											HAL_UART_SendBytes("app send\n", osal_strlen("app send\n"));
										}
									}
        							//RedLED(OFF);
        							HalLedSet (HAL_LED_1, HAL_LED_MODE_OFF);
        							break;
        					//rx done
        					case RXDONE:
                          
                                HalLedSet (HAL_LED_2, HAL_LED_MODE_ON);
                                OLED_Clear_Half();//先把屏幕下一半清空
                                APP_ShowMoteID(g_appData.devAddr);
                                len = 0 ;
                                g_number++ ;
                                memset(Rx_buf , 0 ,sizeof(Rx_buf));                               
                                osal_memcpy(Rx_buf,pMsgRecieve->msgData,pMsgRecieve->msgLen);
				    len = pMsgRecieve->msgLen;
				    Rx_buf[len] = 0;
                                OLED_ShowString( 0,36, (u8*)Rx_buf,12 );
                                OLED_Refresh_Gram();
                                HAL_UART_SendBytes("\n",1);
                                HAL_UART_SendBytes((uint8_t *)Rx_buf,strlen(Rx_buf));
				     HalLedSet (HAL_LED_2, HAL_LED_MODE_OFF);

                                break;

                             case TXERR_STATUS:
                             {
                                //TODO MOTE send packet error deal
                                memset( tmp_buf ,0 ,sizeof(tmp_buf) );
                                sprintf( (char *)tmp_buf,"send err ret=%d,no=%d",pMsgRecieve->msgData[0],
                                                                                 pMsgRecieve->msgData[1]+( pMsgRecieve->msgData[2]<<8 ) );
                                OLED_ShowString( 0,36,tmp_buf,12 );
                                OLED_Refresh_Gram();
                                break;
                             }


                    default:
					    break;
		  	}

			osal_msg_deallocate((u8*)pMsgRecieve);
		}

		return (events ^ SYS_EVENT_MSG);

	}

	//send a packet event
	if(events & APP_PERIOD_SEND)
	{
		//RedLED(OFF);
		 HalLedSet (HAL_LED_1, HAL_LED_MODE_OFF);
		//send a packet to LoRaMac osal (then can be send by the radio)
		pMsgSend = (loraMAC_msg_t*)osal_msg_allocate(72);
		if(pMsgSend != NULL)
		{
			osal_memset(pMsgSend,0,72);
			pMsgSend->msgID = TXREQUEST;
			pMsgSend->msgLen = 70;
			for(u8 dataCount = 0; dataCount < 70; dataCount++)
			{
				pMsgSend->msgData[dataCount] = dataCount;
			}
				osal_msg_send(LoraMAC_taskID,(u8*)pMsgSend);
		}

	  //osal_start_timerEx(APP_taskID, APP_PERIOD_SEND,1000);//延时继续发送

		return (events ^ APP_PERIOD_SEND);
	}

	//uart test event
	if(events & APP_TEST_UART)
	{

		 Txpacket_count = 1;//串口收到一个数据包,就发一遍无线包出去add by hxz
		//HAL_UART_SendBytes("hello,world!", 10);
		//osal_start_timerEx(APP_taskID, APP_TEST_UART,1000);//延时继续发送
		return (events ^ APP_TEST_UART);
	}

	return 0 ;
}