123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369 |
- /* 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 7
- #define RADAR_MAX_BUFFERSIZE 32
- #define RadarCmd_NULL (0)
- #define RadarCmd_AirHeight (1)
- #define RadarCmd_Trans (2)
- 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 int16_t tmp_v;
- //static uint16_t _crc = 0;
- static uint8_t recvbuffer_len = 0;
- static uint8_t databuffer_len = 0;
- static uint8_t trans_recvbuffer[RADAR_MAX_BUFFERSIZE] = {0}; //透传指令暂存buffer
- static uint8_t trans_databuffer[RADAR_MAX_BUFFERSIZE] = {0}; //透传回应数据暂存buffer
- #define WAITTING_TIMEOUT (100) //单位 ms
- static uint8_t cmd_radar = RadarCmd_NULL;
- uint8_t waiting_time_ladar =0; //等待数据时间, ms
- static uint8_t radar_timeout_count = 0; //雷达超时次数
- static float airHeight = 0;
- static uint8_t _status = 0;
- //uint8_t radar_cmd[8] = {0x7F, 0x04, 0x0A, 0x0F, 0x00, 0x02, 0x48, 0x0E};
- uint8_t radar_cmd[8] = {0xFF, 0x03, 0x00, 0x01, 0x00, 0x01, 0xC0, 0x14};
- //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;
- uint16_t _crc = 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;
- waiting_time_ladar =0;
-
- if(RadarCmd_AirHeight == cmd_radar){
- radar_timeout_count = 0;
- g_radarRxDataBuff[g_radarRxDataIndex++] = data;
- if (g_radarRxDataIndex >= RADAR_RX_BUFF_LENGTH)
- {
- g_radarRxDataIndex = 0;
-
- _crc = crc16(g_radarRxDataBuff, RADAR_RX_BUFF_LENGTH-2);
-
- if( _crc == (((uint16_t)g_radarRxDataBuff[5]<<8) | g_radarRxDataBuff[6])){
- _status = STATUS_NORMAL;
- tmp_v = (int16_t)(g_radarRxDataBuff[3]<<8)|g_radarRxDataBuff[4];
- airHeight = tmp_v/1000.0;
-
- }else{
- _status = STATUS_DataError;
- airHeight = 0.0;
- }
-
- //读空高命令结束
- cmd_radar = RadarCmd_NULL;
-
- // ladar_printfLen = snprintf((char*)ladar_printfBuff, 64, "Radar st[%d], airheight: %f m [%x][%x][%x][%x][%x][%x][%x]\r\n",
- // _status, airHeight,
- // g_radarRxDataBuff[0],
- // g_radarRxDataBuff[1],
- // g_radarRxDataBuff[2],
- // g_radarRxDataBuff[3],
- // g_radarRxDataBuff[4],
- // g_radarRxDataBuff[5],
- // g_radarRxDataBuff[6]);
- // rs485_TransmitData((uint8_t*)ladar_printfBuff, ladar_printfLen);
-
- }
-
- }else if(RadarCmd_Trans == cmd_radar){
-
- if(databuffer_len < RADAR_MAX_BUFFERSIZE){
- trans_databuffer[databuffer_len++]=data;
- }
- }
-
-
- }
-
- /*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 = 115200;
- 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;
- }
- void Radar_Init(void)
- {
- uart0_Initialize();
- _status = STATUS_NORMAL;
- airHeight = 0;
- radar_timeout_count = 0;
-
- }
- //uint8_t Radar_ReadAirHeight(void)
- //{
- //// uint16_t crc = 0;
- ////
- //// //清空、接收缓冲
- //// g_radarRxDataIndex = 0;
- //// radar_cmd[0] = 0xFF,
- //// radar_cmd[1] = 0x03,
- //// radar_cmd[2] = 0x00,
- //// radar_cmd[3] = 0x01,
- //// radar_cmd[4] = 0x00,
- //// radar_cmd[5] = 0x01,
- //// crc = crc16(radar_cmd, 6);
- //// radar_cmd[6] = (uint8_t)((crc>>8) & 0x00ff);
- //// radar_cmd[7] = (uint8_t)(crc & 0x00ff);
- //
- // TransmitData(radar_cmd, 8);
- // return 0;
- //}
- void Radar_CheckResponse(void)
- {
- if(RadarCmd_NULL != cmd_radar){
- if(waiting_time_ladar >= WAITTING_TIMEOUT){
- //通信命令结束
- cmd_radar = RadarCmd_NULL;
-
- if(RadarCmd_AirHeight == cmd_radar){
- radar_timeout_count++;
- g_radarRxDataIndex = 0;
-
- if(radar_timeout_count >= 3){
- radar_timeout_count =3;
- _status = STATUS_NoResponse;
- airHeight = 0.0;
- }
-
-
-
- // ladar_printfLen = snprintf((char*)ladar_printfBuff, 64, "Radar_CheckResponse st[%d], airheight: %f m \r\n", _status, airHeight);
- // rs485_TransmitData((uint8_t*)ladar_printfBuff, ladar_printfLen);
-
- }else {
- recvbuffer_len = 0;
- }
-
- }
-
- }
- }
- void Radar_SendRequest(void)
- {
- if(RadarCmd_NULL != cmd_radar){
- return;
- }
-
- // ladar_printfLen = snprintf((char*)ladar_printfBuff, 64, "Radar_SendRequest recvbuffer_len[%d] m \r\n", recvbuffer_len);
- // rs485_TransmitData((uint8_t*)ladar_printfBuff, ladar_printfLen);
-
- if(recvbuffer_len > 0){
- //有透传数据需要发送
- cmd_radar = RadarCmd_Trans;
- TransmitData(trans_recvbuffer, recvbuffer_len);
- waiting_time_ladar = 0;
- //清空接收缓存
- databuffer_len=0;
- memset(trans_databuffer, 0, RADAR_MAX_BUFFERSIZE);
-
- }else{
- //读取空高数据
- cmd_radar = RadarCmd_AirHeight;
- TransmitData(radar_cmd, 8);
- waiting_time_ladar = 0;
- g_radarRxDataIndex = 0;
- }
- }
- void Radar_SendData(uint8_t* pData, uint8_t data_len)
- {
- if(data_len > RADAR_MAX_BUFFERSIZE){
- return ;
- }
-
- if(RadarCmd_Trans == cmd_radar){ //上一个透传指令还没结束
- return;
- }
-
- memcpy(trans_recvbuffer, pData, data_len);
- recvbuffer_len = data_len;
-
- }
- void Radar_ReadData(uint8_t* pBuffer, uint8_t read_len)
- {
- if(read_len > RADAR_MAX_BUFFERSIZE){
- read_len = RADAR_MAX_BUFFERSIZE;
- }
-
- memcpy(pBuffer, trans_databuffer, read_len);
-
- //清空接收缓存
- databuffer_len=0;
- memset(trans_databuffer, 0, RADAR_MAX_BUFFERSIZE);
-
- }
- //uint8_t Radar_SetInstallHeight(uint16_t install_height)
- //{
- // //清空、接收缓冲
- // 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)
- {
- }
|