123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240 |
- /* Copyright Statement:
- *
- * This software/firmware and related documentation ("AutoChips Software") are
- * protected under relevant copyright laws. The information contained herein is
- * confidential and proprietary to AutoChips Inc. and/or its licensors. Without
- * the prior written permission of AutoChips inc. and/or its licensors, any
- * reproduction, modification, use or disclosure of AutoChips Software, and
- * information contained herein, in whole or in part, shall be strictly
- * prohibited.
- *
- * AutoChips Inc. (C) 2016. All rights reserved.
- *
- * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
- * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("AUTOCHIPS SOFTWARE")
- * RECEIVED FROM AUTOCHIPS AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER
- * ON AN "AS-IS" BASIS ONLY. AUTOCHIPS EXPRESSLY DISCLAIMS ANY AND ALL
- * WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
- * NONINFRINGEMENT. NEITHER DOES AUTOCHIPS PROVIDE ANY WARRANTY WHATSOEVER WITH
- * RESPECT TO THE SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY,
- * INCORPORATED IN, OR SUPPLIED WITH THE AUTOCHIPS SOFTWARE, AND RECEIVER AGREES
- * TO LOOK ONLY TO SUCH THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO.
- * RECEIVER EXPRESSLY ACKNOWLEDGES THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO
- * OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES CONTAINED IN AUTOCHIPS
- * SOFTWARE. AUTOCHIPS SHALL ALSO NOT BE RESPONSIBLE FOR ANY AUTOCHIPS SOFTWARE
- * RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
- * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND AUTOCHIPS'S
- * ENTIRE AND CUMULATIVE LIABILITY WITH RESPECT TO THE AUTOCHIPS SOFTWARE
- * RELEASED HEREUNDER WILL BE, AT AUTOCHIPS'S OPTION, TO REVISE OR REPLACE THE
- * AUTOCHIPS SOFTWARE AT ISSUE, OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE
- * CHARGE PAID BY RECEIVER TO AUTOCHIPS FOR SUCH AUTOCHIPS SOFTWARE AT ISSUE.
- */
-
- #include "ac780x_uart_reg.h"
- #include "ac780x_gpio.h"
- #include "Radar.h"
- #include "uart.h"
- #include "string.h"
- #include "crc16.h"
- #define LaDarUARTx UART0
- #define LaDarUARTx_IRQn UART0_IRQn
- #define RADAR_RX_BUFF_LENGTH 9
- uint8_t g_radarRxDataBuff[RADAR_RX_BUFF_LENGTH];
- uint16_t g_radarRxDataIndex;
- uint8_t *g_radarTxDataPoint;
- uint16_t g_radarTxDataLength;
- uint16_t g_radarTxDataIndex;
- static uint8_t * p_float;
- static float tmp_v;
- static uint16_t _crc = 0;
- static float airHeight = 0;
- static uint8_t _status = 0;
- uint8_t radar_cmd[8] = {0x7F, 0x04, 0x0A, 0x0F, 0x00, 0x02, 0x48, 0x0E};
- //static uint8_t radar_cmd[32];
- //uint8_t
- //static uint32_t ladar_printfBuff[16];
- //static uint16_t ladar_printfLen = 0;
- static int TransmitData(uint8_t *pdata, uint16_t length);
- /**
- * UartEventCallback
- *
- * @param[in] device: UART_Type pointer
- * @param[in] wpara: UART lsr0 register
- * @param[in] lpara: UART lsr1 register
- * @return none
- *
- * @brief uart receive handle
- */
- static void UartEventCallback(void *device, uint32_t wpara, uint32_t lpara)
- {
- uint8_t data = 0;
- UART_Type *uart_Device = (UART_Type *)device;
-
- /*rx interrupt*/
- if ((uart_Device->IER & UART_IER_ERXNE_Msk) && (wpara & UART_LSR0_DR_Msk))
- {
- data = uart_Device->RBR;
- g_radarRxDataBuff[g_radarRxDataIndex++] = data;
-
- if (g_radarRxDataIndex >= RADAR_RX_BUFF_LENGTH)
- {
- g_radarRxDataIndex = 0;
- _crc = crc16(g_radarRxDataBuff, 7);
-
- if( _crc == (((uint16_t)g_radarRxDataBuff[7]<<8) | g_radarRxDataBuff[8])){
-
- if((g_radarRxDataBuff[3] == 0xFF)&&(g_radarRxDataBuff[4] == 0xFF)&&(g_radarRxDataBuff[5] == 0xFF)&&(g_radarRxDataBuff[6] == 0xFF)){
- _status = STATUS_EXCEED;
- airHeight = 0;
- }else if((g_radarRxDataBuff[3] == 0xFE)&&(g_radarRxDataBuff[4] == 0xFE)&&(g_radarRxDataBuff[5] == 0xFE)&&(g_radarRxDataBuff[6] == 0xFE)){
- _status = STATUS_BLIND;
- airHeight = 0;
- }else if((g_radarRxDataBuff[3] == 0xFD)&&(g_radarRxDataBuff[4] == 0xFD)&&(g_radarRxDataBuff[5] == 0xFD)&&(g_radarRxDataBuff[6] == 0xFD)){
- _status = STATUS_WEAK;
- airHeight = 0;
- }else{
- _status = STATUS_NORMAL;
- p_float = (uint8_t*)&tmp_v;
-
- p_float[0] = g_radarRxDataBuff[4];
- p_float[1] = g_radarRxDataBuff[3];
- p_float[2] = g_radarRxDataBuff[6];
- p_float[3] = g_radarRxDataBuff[5];
-
- airHeight = tmp_v;
-
- }
-
-
- }
-
- // ladar_printfLen = snprintf((char*)ladar_printfBuff, 64, "Radar[%d] airheight: %f m\r\n",_status, airHeight);
- // rs485_TransmitData((uint8_t*)ladar_printfBuff, ladar_printfLen);
-
- }
- }
-
- /*tx interrupt*/
- if ((uart_Device->IER & UART_IER_ETXE_Msk) && (wpara & UART_LSR0_THRE_Msk))
- {
- uart_Device->RBR = g_radarTxDataPoint[g_radarTxDataIndex++];
- if (g_radarTxDataIndex >= g_radarTxDataLength)
- {
- UART_SetTXEInterrupt(device, DISABLE); ///<发送最后一个字节时关闭发送空中断
- }
- }
- }
- /**
- * uart_Initialize
- *
- * @param[in] none
- * @return none
- *
- * @brief uart 初始化
- */
- static void uart0_Initialize(void)
- {
- UART_ConfigType uartConfig = {0};
- uartConfig.baudrate = 9600;
- uartConfig.dataBits = UART_WORD_LEN_8BIT;
- uartConfig.stopBits = UART_STOP_1BIT;
- uartConfig.parity = UART_PARI_NO;
- uartConfig.fifoByteEn = DISABLE; ///<must enable fifoByte when use DMA
- uartConfig.dmaEn = UART_DMA_TXRX_NONE;
- uartConfig.callBack = UartEventCallback; ///<uart0 interrupt callback
- UART_Init(LaDarUARTx, &uartConfig);
-
- UART_SetRXNEInterrupt(LaDarUARTx, ENABLE);
- ///Enable UARTx interrupt
- NVIC_SetPriority(LaDarUARTx_IRQn, 3);
- NVIC_ClearPendingIRQ(LaDarUARTx_IRQn);
- NVIC_EnableIRQ(LaDarUARTx_IRQn);
-
- }
- /**
- * TransmitData
- *
- * @param[in] pdata :发送数据指针
- * @param[in] length :发送数据长度
- * @return none
- *
- * @brief uart 发送函数,通过中断发送
- */
- int TransmitData(uint8_t *pdata, uint16_t length)
- {
- if (g_radarTxDataIndex < g_radarTxDataLength){
- return -1; ///<有数据正在发送
- }
-
- g_radarTxDataPoint = pdata;
- g_radarTxDataLength = length;
- g_radarTxDataIndex = 0;
- UART_SetTXEInterrupt(LaDarUARTx, ENABLE);
-
- return 0;
- }
- //static void set_collectfreq(uint16_t interval){
- // uint16_t len = 0;
- // len = (uint16_t)snprintf((char*)radar_cmd, 32, "AT+COLLECTFREQ=%d\n", interval);
- // TransmitData(radar_cmd, len);
- //}
- //static void read_airheight(){
- // uint16_t len = 0;
- // len = (uint16_t)snprintf((char*)radar_cmd, 32, "AT+REALAIRHEIGHT\n");
- // TransmitData(radar_cmd, len);
- //}
- void Radar_Init(void)
- {
- uart0_Initialize();
- _status = STATUS_NORMAL;
- airHeight = 0;
-
- }
- uint8_t Radar_MR(void)
- {
- //清空、接收缓冲
- g_radarRxDataIndex = 0;
- TransmitData(radar_cmd, 8);
-
- return 0;
- }
- uint8_t Radar_GetHeight(float* height)
- {
- *height = airHeight;
- return _status;
- }
- uint8_t Radar_Print(void)
- {
- return 0;
- }
- void Radar_DeInit(void)
- {
- }
|