From b35fabdf17ef26b6d14bdbb9cc18f1f4e59b8c0e Mon Sep 17 00:00:00 2001 From: penglei <1624952103@qq.com> Date: Fri, 30 Aug 2024 21:31:32 +0800 Subject: [PATCH] =?utf8?q?=E4=BF=AE=E6=94=B9=E6=B7=BB=E5=8A=A0=E5=AF=86?= =?utf8?q?=E9=92=A5AT=E6=8C=87=E4=BB=A4,=E4=BC=98=E5=8C=96=E6=96=B0?= =?utf8?q?=E7=AE=97=E6=B3=95=E5=85=BC=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../cywee/Inc/CWM_CustomDataStruct.h | 14 + .../peripheral/cywee/src/board_apollo3.c | 319 ------------ .../peripheral/cywee/src/board_nordic.c | 479 ------------------ .../peripheral/utils/custom_utils_at_cmd.c | 4 +- 4 files changed, 16 insertions(+), 800 deletions(-) delete mode 100644 code/sdk/drivers/peripheral/cywee/src/board_apollo3.c delete mode 100644 code/sdk/drivers/peripheral/cywee/src/board_nordic.c diff --git a/code/sdk/drivers/peripheral/cywee/Inc/CWM_CustomDataStruct.h b/code/sdk/drivers/peripheral/cywee/Inc/CWM_CustomDataStruct.h index 533724479..d8f75580b 100644 --- a/code/sdk/drivers/peripheral/cywee/Inc/CWM_CustomDataStruct.h +++ b/code/sdk/drivers/peripheral/cywee/Inc/CWM_CustomDataStruct.h @@ -1,10 +1,24 @@ +/* + * @Author: penglei 1624952103@qq.com + * @Date: 2024-06-07 11:18:26 + * @LastEditors: penglei 1624952103@qq.com + * @LastEditTime: 2024-08-30 21:29:18 + * @FilePath: /FD230/code/sdk/drivers/peripheral/cywee/Inc/CWM_CustomDataStruct.h + * @Description: + * + * Copyright (c) 2024 by ${git_name_email}, All Rights Reserved. + */ #ifndef __CWM_CUSTOMDATASTRUCT_H__ #define __CWM_CUSTOMDATASTRUCT_H__ typedef struct CustomSensorData { uint32_t sensorType; float fData[16]; +#ifdef CYWEE_MAC_ACTIVE double dData[12]; +#else + double dData[10]; +#endif } CustomSensorData; #endif /* __CWM_CUSTOMDATASTRUCT_H__ */ diff --git a/code/sdk/drivers/peripheral/cywee/src/board_apollo3.c b/code/sdk/drivers/peripheral/cywee/src/board_apollo3.c deleted file mode 100644 index 217227327..000000000 --- a/code/sdk/drivers/peripheral/cywee/src/board_apollo3.c +++ /dev/null @@ -1,319 +0,0 @@ -/*********************/ -/*** Include Files ***/ -/*********************/ -#include -#include -#include "am_mcu_apollo.h" -#include "am_bsp.h" -#include "am_util.h" -#include "am_devices_i2c.h" -#include "cwm_lib.h" -#include "config.h" - -/*********************/ -/*** Macro Defines ***/ -/*********************/ -#define CLOCK_FREQ 1000 //It is clock frequency setting. -#define IOM_MODULE_NUMBER 0 -#define IOM_IRQn ((IRQn_Type)(IOMSTR0_IRQn + IOM_MODULE_NUMBER)) - -// -// Take over the interrupt handler for whichever IOM we're using. -// -#define iom_isr \ - am_iom_isr1(IOM_MODULE_NUMBER) -#define am_iom_isr1(n) \ - am_iom_isr(n) -#define am_iom_isr(n) \ - am_iomaster ## n ## _isr - - -/*************************/ -/*** Variables Defines ***/ -/*************************/ -uint32_t DMATCBBuffer[256]; -void *g_pIOMHandle; - -am_hal_iom_config_t g_sIomCfg = -{ - .eInterfaceMode = AM_HAL_IOM_I2C_MODE, - .ui32ClockFreq = AM_HAL_IOM_100KHZ, -}; - -static uint32_t m_tickLast = 0; -static uint64_t m_elpsedTime_us = 0; - - -/************************/ -/*** Function Defines ***/ -/************************/ -int CWM_OS_dbgPrintf(const char * format,...); - - -/********************************/ -/*** Function Implementations ***/ -/********************************/ -void iom_isr(void) -{ - uint32_t ui32Status; - - if (!am_hal_iom_interrupt_status_get(g_pIOMHandle, true, &ui32Status)) - { - if ( ui32Status ) - { - am_hal_iom_interrupt_clear(g_pIOMHandle, ui32Status); - am_hal_iom_interrupt_service(g_pIOMHandle, ui32Status); - } - } -} - -static void I2C_init(void) -{ - g_sIomCfg.ui32NBTxnBufLength = sizeof(DMATCBBuffer) / 4; - g_sIomCfg.pNBTxnBuf = &DMATCBBuffer[0]; - - am_devices_i2c_init(IOM_MODULE_NUMBER, &g_sIomCfg, &g_pIOMHandle); - - return; -} - -static void am_i2c_setting(void) -{ - I2C_init(); - NVIC_EnableIRQ(IOM_IRQn); -} - -static void am_i2c_deinit(void) -{ - am_devices_i2c_term(0); - NVIC_DisableIRQ(IOM_IRQn); -} - -static void APOLLO_I2C_Reset(void) -{ - am_i2c_deinit(); - am_util_delay_ms(20); - am_i2c_setting(); -} - -static void stimer_init(void) -{ - am_hal_stimer_int_enable(AM_HAL_STIMER_INT_COMPAREA); - NVIC_EnableIRQ(STIMER_CMPR0_IRQn); - am_hal_stimer_config(AM_HAL_STIMER_CFG_CLEAR | AM_HAL_STIMER_CFG_FREEZE); - am_hal_stimer_compare_delta_set(0, CLOCK_FREQ);//1 second timer - am_hal_stimer_config(AM_HAL_STIMER_LFRC_1KHZ | - AM_HAL_STIMER_CFG_COMPARE_A_ENABLE); - m_tickLast = am_hal_stimer_counter_get(); -} - -static uint64_t platsystickTicksToUs(uint64_t tick) { - return (uint64_t)(tick * 1000000/CLOCK_FREQ); -} - -static inline uint64_t calcElapsedTicks(uint64_t tickNow, uint64_t tickLast) -{ - if(tickNow >= tickLast) - return tickNow - tickLast; - else - return 0xFFFFFFFF - tickLast + tickNow; -} - -static void systickUpdate(void) -{ - //To implement the CWM_OS_GetTimeNs(), this function needs to be triggered every second - uint64_t tickNow; - uint64_t delta_ticks; - - tickNow = am_hal_stimer_counter_get(); - delta_ticks = calcElapsedTicks(tickNow, m_tickLast); - m_elpsedTime_us += platsystickTicksToUs(delta_ticks); - - m_tickLast = tickNow; -} - -void am_stimer_cmpr0_isr(void) -{ - //To implement the CWM_OS_GetTimeNs(), this function needs to be triggered every second - systickUpdate(); - - am_hal_stimer_int_clear(AM_HAL_STIMER_INT_COMPAREA); - am_hal_stimer_compare_delta_set(0, CLOCK_FREQ);//1 second timer -} - -#define BUS_RETRY_TIMES 3 -#define BUS_RECOVER_TIMES 1 - -int CWM_OS_i2cRead(uint16_t slaveAddr, uint16_t reg, int regLength, uint8_t *readData, int readDataSize, int argument) -{ - int res; - int rty = BUS_RETRY_TIMES; - int rcovery = BUS_RECOVER_TIMES; - - do{ - res = am_devices_i2c_blocking_read(slaveAddr, reg, readData, readDataSize); - - if(res == 0) - break; - - rty --; - if(rcovery > 0) - { - if(rty == 0) - { - rty = BUS_RETRY_TIMES; - rcovery --; - APOLLO_I2C_Reset(); - } - } - }while(rty > 0); - - - return (res>0) ? res*-1: res; -} - -int CWM_OS_i2cWrite(uint16_t slaveAddr, uint16_t reg, int regLength, uint8_t *writeData, int writeDataSize, int argument) -{ - int res; - int rty = BUS_RETRY_TIMES; - int rcovery = BUS_RECOVER_TIMES; - - do{ - res = am_devices_i2c_blocking_write(slaveAddr, reg, writeData, writeDataSize); - - if(res == 0) - break; - - rty --; - if(rcovery > 0) - { - if(rty == 0) - { - rty = BUS_RETRY_TIMES; - rcovery --; - APOLLO_I2C_Reset(); - } - } - }while(rty > 0); - - return (res>0) ? res*-1: res; -} - -int CWM_OS_i2cTransfer(uint16_t slaveAddr, uint16_t reg, int regLength, uint8_t *writeData, int writeDataSize, uint8_t *readData, int readDataSize, int argument) -{ - am_hal_iom_transfer_t desc; - memset(&desc, 0, sizeof(am_hal_iom_transfer_t)); - - desc.ui32InstrLen = regLength; - desc.ui32Instr = reg; - desc.uPeerInfo.ui32I2CDevAddr = slaveAddr >> 1; - desc.ui8RepeatCount = 0; - desc.ui32PauseCondition = 0; - desc.ui32StatusSetClr = 0; - desc.bContinue = false; - - if (readDataSize == 0) { - if (writeDataSize == 0 || writeData == NULL) - return -1; - - // write mode - desc.eDirection = AM_HAL_IOM_TX; - desc.ui32NumBytes = writeDataSize; - desc.pui32TxBuffer = (uint32_t *) writeData; - } - else { - if (readData == NULL) - return -1; - - // read mode - desc.eDirection = AM_HAL_IOM_RX; - desc.ui32NumBytes = readDataSize; - desc.pui32RxBuffer = (uint32_t *) readData; - } - - return am_hal_iom_blocking_transfer(g_pIOMHandle, &desc); -} - -void CWM_OS_uSleep(uint32_t time) -{ - uint32_t ms = time / 1000; - - if (ms == 0) - ms = 1; - am_util_delay_ms(ms); -} - -uint64_t CWM_OS_GetTimeNs(void) -{ - uint64_t tickNow; - uint64_t delta_ticks; - uint64_t timeNow_us; - - tickNow = am_hal_stimer_counter_get(); - delta_ticks = calcElapsedTicks(tickNow, m_tickLast); - timeNow_us = m_elpsedTime_us + platsystickTicksToUs(delta_ticks); - - return timeNow_us * 1000; -} - -void* CWM_OS_malloc(int size) -{ - if (size == 0) - size = 1; - return malloc(size); -} - -void CWM_OS_free(void *ptr) -{ - if (ptr == NULL) - return; - - free(ptr); -} - -int CWM_OS_dbgOutput(const char * format) -{ - am_util_stdio_printf("%s", format); - return 0; -} - -int CWM_OS_dbgOutput_checkLog(const char * format) -{ - static int s_sn = 0; - - am_util_stdio_printf("#_#|%d|%s", s_sn, format); - s_sn ++; - return 0; -} - -int CWM_OS_dbgPrintf(const char * format,...) -{ - char InputBuff[255]; - va_list args; - - va_start(args, format); - am_util_stdio_vsprintf(InputBuff, format, args); - va_end(args); - CWM_OS_dbgOutput(InputBuff); - return 0; -} - -void board_init(void) -{ - am_hal_clkgen_control(AM_HAL_CLKGEN_CONTROL_SYSCLK_MAX, 0); //Set the clock frequency. - am_hal_cachectrl_config(&am_hal_cachectrl_defaults); //default cache configuration - am_hal_cachectrl_enable(); - am_bsp_low_power_init(); //Configure the board for low power operation. - -#ifdef PRINTF_FROM_UART - am_bsp_uart_printf_enable(); //Enable the UART print interface. -#else - am_bsp_itm_printf_enable(); //Enable the ITM print interface. Using "SWO Viewer" to show log -#endif - am_util_stdio_terminal_clear(); //Clear the terminal and print the banner. - am_i2c_setting(); - am_hal_interrupt_master_enable(); - - stimer_init(); //Timer Init -} - diff --git a/code/sdk/drivers/peripheral/cywee/src/board_nordic.c b/code/sdk/drivers/peripheral/cywee/src/board_nordic.c deleted file mode 100644 index 1e1373eff..000000000 --- a/code/sdk/drivers/peripheral/cywee/src/board_nordic.c +++ /dev/null @@ -1,479 +0,0 @@ -/*********************/ -/*** Include Files ***/ -/*********************/ -#include -#include "boards.h" -#include "app_util_platform.h" -#include "nrf_delay.h" -#include "nrf_drv_timer.h" -#include "nrf_log.h" -#include "nrf_log_ctrl.h" -#include "nrf_log_default_backends.h" -#include "nrf_drv_twi.h" -#include "pca10040.h" -#include "cwm_lib.h" -#include "config.h" -#include "nrf_drv_clock.h" -#include "app_timer.h" -#include "nrf_drv_gpiote.h" - - -/*********************/ -/*** Macro Defines ***/ -/*********************/ -#define PCA10040_PIN_25 25 -#define PCA10040_PIN_28 28 -#define BUS_RETRY_TIMES 3 -#define BUS_RECOVER_TIMES 1 -#define TWI_INSTANCE_ID 0 -#define TIMER_PERIOD_1_SEC APP_TIMER_TICKS(1000) - -/*************************/ -/*** Variables Defines ***/ -/*************************/ -nrf_drv_twi_t m_twi = NRF_DRV_TWI_INSTANCE(TWI_INSTANCE_ID); - -static uint64_t m_tickLast = 0; -static uint64_t m_elpsedTime_us = 0; -APP_TIMER_DEF(m_one_sec_timer); - -typedef struct -{ - uint16_t sensorType; - uint16_t sensorPin; - uint16_t mcuPin; - uint16_t mcu_int_method; - uint16_t mcu_int_connect; - PFN_IRQHANDLE callback; -} irq_item; - -typedef struct -{ - uint16_t sensorType; - uint16_t sensorPin; -} irq_argument; - -static irq_item m_irqHandler[] = -{ - { - .sensorType = CUSTOM_ACC, - .sensorPin = PIN_INT1, - .mcuPin = PCA10040_PIN_25, - .mcu_int_method = INT_TRIGGER_TYPE_FALLING, - .mcu_int_connect = INT_RESISTOR_NOPULL, - .callback = NULL, - }, - { - .sensorType = CUSTOM_ACC, - .sensorPin = PIN_INT2, - .mcuPin = PCA10040_PIN_28, - .mcu_int_method = INT_TRIGGER_TYPE_RISING, - .mcu_int_connect = INT_RESISTOR_PULLDOWN, - .callback = NULL, - }, -}; - - -/************************/ -/*** Function Defines ***/ -/************************/ -int CWM_OS_dbgPrintf(const char * format,...); - - -/********************************/ -/*** Function Implementations ***/ -/********************************/ -static void I2C_Init_Nordic(void) -{ - const nrf_drv_twi_config_t twi_config = { - .scl = ARDUINO_SCL_PIN, - .sda = ARDUINO_SDA_PIN, - .frequency = NRF_DRV_TWI_FREQ_400K, - .interrupt_priority = APP_IRQ_PRIORITY_HIGH, - .clear_bus_init = false - }; - - nrf_drv_twi_init(&m_twi, &twi_config, NULL, NULL); - nrf_drv_twi_enable(&m_twi); -} - -static void I2C_Uninit_Nordic(void) -{ - nrf_drv_twi_uninit(&m_twi); - *(volatile uint32_t *)0x40003FFC = 0; - *(volatile uint32_t *)0x40003FFC; - *(volatile uint32_t *)0x40003FFC = 1; -} - -static void I2C_Reset_Nordic(void) -{ - I2C_Uninit_Nordic(); - I2C_Init_Nordic(); -} - -static inline uint64_t calcElapsedTicks(uint64_t tickNow, uint64_t tickLast) -{ - if(tickNow >= tickLast) - return tickNow - tickLast; - else - return APP_TIMER_MAX_CNT_VAL - tickLast + tickNow; //Nordic RTC resolution is 24 bit, APP_TIMER_MAX_CNT_VAL = 0x00FFFFFF -} - -static uint64_t platsystickTicksToUs(uint64_t tick) { - return (uint64_t)(tick * 1000000 / APP_TIMER_CLOCK_FREQ); -} - -static void systickUpdate() -{ - //To implement the CWM_OS_GetTimeNs(), this function needs to be triggered every second - uint64_t tickNow; - uint64_t delta_ticks; - - tickNow = app_timer_cnt_get(); - delta_ticks = calcElapsedTicks(tickNow, m_tickLast); - m_elpsedTime_us += platsystickTicksToUs(delta_ticks); - m_tickLast = tickNow; -} - -static void one_sec_timer_handle(void * p_context) -{ - //To implement the CWM_OS_GetTimeNs(), this function needs to be triggered every second - systickUpdate(); -} - -static void timer_init(void) -{ - ret_code_t ret; - - ret = nrf_drv_clock_init(); - APP_ERROR_CHECK(ret); - - nrf_drv_clock_lfclk_request(NULL); - - ret = app_timer_init(); - APP_ERROR_CHECK(ret); - - ret = app_timer_create(&m_one_sec_timer, APP_TIMER_MODE_REPEATED, one_sec_timer_handle); - APP_ERROR_CHECK(ret); - - ret = app_timer_start(m_one_sec_timer, TIMER_PERIOD_1_SEC, NULL);//1 second timer - APP_ERROR_CHECK(ret); -} - -static void nordic_system_init(void) -{ - bsp_board_init(BSP_INIT_NONE); - APP_ERROR_CHECK(NRF_LOG_INIT(NULL)); - NRF_LOG_DEFAULT_BACKENDS_INIT(); - I2C_Init_Nordic(); -} - -void Irq_callback(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action) -{ - for (int i=0; i> 1, (uint8_t*) ®, regLength, true); - if (res == NRF_SUCCESS) { - nrf_drv_twi_rx(&m_twi, slaveAddr >> 1, readData, readDataSize); - } - - if(res != 0) - break; - - rty --; - if(rcovery > 0) - { - if(rty == 0) - { - rty = BUS_RETRY_TIMES; - rcovery --; - I2C_Reset_Nordic(); - } - } - }while(rty > 0); - - - return (res>0) ? res*-1: res; -} - -int CWM_OS_i2cWrite(uint16_t slaveAddr, uint16_t reg, int regLength, uint8_t *writeData, int writeDataSize, int argument) -{ - int res; - int rty = BUS_RETRY_TIMES; - int rcovery = BUS_RECOVER_TIMES; - - do{ - uint8_t data[20]; - memset(data, 0, sizeof(data)); - - if (writeDataSize + 1 > sizeof(data)) { - return -1; - } - - memcpy(data, ®, regLength); - memcpy(&data[regLength], writeData, writeDataSize); - res = nrf_drv_twi_tx(&m_twi, slaveAddr >> 1, data, writeDataSize + regLength, false); - - if(res != 0) - break; - - rty --; - if(rcovery > 0) - { - if(rty == 0) - { - rty = BUS_RETRY_TIMES; - rcovery --; - I2C_Reset_Nordic(); - } - } - }while(rty > 0); - - return (res>0) ? res*-1: res; -} - -int CWM_OS_i2cTransfer(uint16_t slaveAddr, uint16_t reg, int regLength, uint8_t *writeData, int writeDataSize, uint8_t *readData, int readDataSize, int argument) -{ - uint8_t writeBuf[20]; - memset(writeBuf, 0, sizeof(writeBuf)); - - nrf_drv_twi_xfer_desc_t desc; - memset(&desc, 0, sizeof(nrf_drv_twi_xfer_desc_t)); - - desc.address = slaveAddr >> 1; - memcpy(writeBuf, ®, regLength); - desc.primary_length = regLength; - - if (readDataSize == 0) { - if (writeDataSize == 0 || writeData == NULL) - return -1; - - // write mode - memcpy(writeBuf + regLength, writeData, writeDataSize); - desc.primary_length += writeDataSize; - desc.type = NRF_DRV_TWI_XFER_TX; - } else { - if (readData == NULL) - return -1; - - // read mode - desc.type = NRF_DRV_TWI_XFER_TXRX; - } - - desc.p_primary_buf = writeBuf; - desc.secondary_length = readDataSize; - desc.p_secondary_buf = readData; - - return nrf_drv_twi_xfer(&m_twi, &desc, 0); -} - -void CWM_OS_uSleep(uint32_t time) -{ - uint32_t ms = time / 1000; - - if (ms == 0) - ms = 1; - nrf_delay_ms(ms); -} - -uint64_t CWM_OS_GetTimeNs(void) -{ - uint64_t tick; - uint64_t delta_ticks; - uint64_t timeNow_us; - - tick = app_timer_cnt_get(); - delta_ticks = calcElapsedTicks(tick, m_tickLast); - timeNow_us = m_elpsedTime_us + platsystickTicksToUs(delta_ticks); - - return timeNow_us * 1000; -} - -void* CWM_OS_malloc(int size) -{ - if (size == 0) - size = 1; - return malloc(size); -} - -void CWM_OS_free(void *ptr) -{ - if (ptr == NULL) - return; - - free(ptr); -} - -int CWM_OS_dbgOutput(const char * format) -{ - NRF_LOG_INFO("%s", format); - NRF_LOG_FLUSH(); - - return 0; -} - -int CWM_OS_dbgOutput_checkLog(const char * format) -{ - static int s_sn = 0; - - NRF_LOG_INFO("#_#|%d|%s", s_sn, format); - s_sn ++; - NRF_LOG_FLUSH(); - - return 0; -} - -int CWM_OS_dbgPrintf(const char * format,...) -{ - char InputBuff[255]; - va_list args; - - va_start(args, format); - - vsprintf(InputBuff, format, args); - - va_end(args); - CWM_OS_dbgOutput(InputBuff); - return 0; -} - -void board_init(void) -{ - nordic_system_init(); - timer_init(); -} diff --git a/code/sdk/drivers/peripheral/utils/custom_utils_at_cmd.c b/code/sdk/drivers/peripheral/utils/custom_utils_at_cmd.c index b5aea4591..ad6cf2030 100644 --- a/code/sdk/drivers/peripheral/utils/custom_utils_at_cmd.c +++ b/code/sdk/drivers/peripheral/utils/custom_utils_at_cmd.c @@ -1154,9 +1154,9 @@ static uint32_t custom_cywee_save_mac_key(uint8_t *para, uint32_t paraLen, char wstp_print("CUSTOM_UTILSTEST power_test ATCMD Set Parameter Failed argc %d\r\nPM_DSP_POWER_UP_AND_RUN=2 PM_DSP_POWER_OFF=3\r\n",argc); return ERRCODE_FAIL; } - int16_t len = atol((char*)argv[1]); + int16_t len = atol((char*)argv[0]); - uapi_cywee_save_mac_key(argv[0],len); + uapi_cywee_save_mac_key(argv[1],len); return ERRCODE_SUCC; } -- 2.17.1