refactor(vd960Loop): 算法回退到 DLD154V4B,四通道适配
- 用 DLD154V4B vd1_task/per_channel 替换 vds_task 复杂算法
- 移除 FUNCTION_B/二次判断/快速变化/多重确认等增强特性
- 保留平坦性离开算法 (CN200910309382),每通道独立状态
- 灵敏度表改为 DLD154V4B 4级: {216,108,36,10} / {108,72,18,9}
- 清理废弃类型: FltHistoryManager, Loop_ACS_Info, StageRangeConfig 等
- 首次添加 vd960DBN 完整源码
This commit is contained in:
35
vd960DBN/.gitignore
vendored
Normal file
35
vd960DBN/.gitignore
vendored
Normal file
@@ -0,0 +1,35 @@
|
||||
MDK-ARM/
|
||||
RTE/
|
||||
settings/
|
||||
*.cproject
|
||||
*.settings/
|
||||
*.uvguix*
|
||||
*.crf
|
||||
*.TMP
|
||||
*.o
|
||||
*.obj
|
||||
*.out
|
||||
*.dep
|
||||
*.i
|
||||
*.d
|
||||
*.old
|
||||
project/
|
||||
*.project
|
||||
*.template
|
||||
*.wvproj
|
||||
obj/
|
||||
bak/
|
||||
*.bak
|
||||
*.pdf
|
||||
*.zip
|
||||
*.mrs/
|
||||
*.exe
|
||||
*.launch
|
||||
*.pdf
|
||||
*.PDF
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
140
vd960DBN/BLE/HAL/KEY.c
Normal file
140
vd960DBN/BLE/HAL/KEY.c
Normal file
@@ -0,0 +1,140 @@
|
||||
/********************************** (C) COPYRIGHT *******************************
|
||||
* File Name : KEY.c
|
||||
* Author : WCH
|
||||
* Version : V1.2
|
||||
* Date : 2022/01/18
|
||||
* Description :
|
||||
*********************************************************************************
|
||||
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
|
||||
* Attention: This software (modified or not) and binary are used for
|
||||
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
|
||||
*******************************************************************************/
|
||||
|
||||
/******************************************************************************/
|
||||
/* Header file contains */
|
||||
#include "HAL.h"
|
||||
|
||||
/**************************************************************************************************
|
||||
* GLOBAL VARIABLES
|
||||
**************************************************************************************************/
|
||||
|
||||
static uint8_t halKeySavedKeys; /* Keep the last state of the button to query whether there is a key value change */
|
||||
|
||||
/**************************************************************************************************
|
||||
* FUNCTIONS - Local
|
||||
**************************************************************************************************/
|
||||
static halKeyCBack_t pHalKeyProcessFunction; /* callback function */
|
||||
|
||||
/**************************************************************************************************
|
||||
* @fn HAL_KeyInit
|
||||
*
|
||||
* @brief Initilize Key Service
|
||||
*
|
||||
* @param none
|
||||
*
|
||||
* @return None
|
||||
**************************************************************************************************/
|
||||
void HAL_KeyInit(void)
|
||||
{
|
||||
/* Initialize previous key to 0 */
|
||||
halKeySavedKeys = 0;
|
||||
/* Initialize callback function */
|
||||
pHalKeyProcessFunction = NULL;
|
||||
|
||||
RCC_APB2PeriphClockCmd(KEY1_PCENR, ENABLE);
|
||||
GPIO_InitTypeDef GPIO_InitStructure;
|
||||
GPIO_InitStructure.GPIO_Pin = KEY1_BV;
|
||||
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
||||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
|
||||
GPIO_Init(KEY1_GPIO, &GPIO_InitStructure);
|
||||
}
|
||||
|
||||
/**************************************************************************************************
|
||||
* @fn HalKeyConfig
|
||||
*
|
||||
* @brief Configure the Key serivce
|
||||
*
|
||||
* @param cback - pointer to the CallBack function
|
||||
*
|
||||
* @return None
|
||||
**************************************************************************************************/
|
||||
void HalKeyConfig(halKeyCBack_t cback)
|
||||
{
|
||||
/* Register the callback fucntion */
|
||||
pHalKeyProcessFunction = cback;
|
||||
tmos_start_task(halTaskID, HAL_KEY_EVENT, HAL_KEY_POLLING_VALUE); /* Kick off polling */
|
||||
}
|
||||
|
||||
/**************************************************************************************************
|
||||
* @fn HalKeyRead
|
||||
*
|
||||
* @brief Read the current value of a key
|
||||
*
|
||||
* @param None
|
||||
*
|
||||
* @return keys - current keys status
|
||||
**************************************************************************************************/
|
||||
uint8_t HalKeyRead(void)
|
||||
{
|
||||
uint8_t keys = 0;
|
||||
|
||||
if(HAL_PUSH_BUTTON1())
|
||||
{ //Read button 1
|
||||
keys |= HAL_KEY_SW_1;
|
||||
}
|
||||
if(HAL_PUSH_BUTTON2())
|
||||
{ //Read button 1
|
||||
keys |= HAL_KEY_SW_2;
|
||||
}
|
||||
if(HAL_PUSH_BUTTON3())
|
||||
{ //Read button 1
|
||||
keys |= HAL_KEY_SW_3;
|
||||
}
|
||||
if(HAL_PUSH_BUTTON4())
|
||||
{ //Read button 1
|
||||
keys |= HAL_KEY_SW_4;
|
||||
}
|
||||
return keys;
|
||||
}
|
||||
|
||||
/**************************************************************************************************
|
||||
* @fn HAL_KeyPoll
|
||||
*
|
||||
* @brief Called by hal_driver to poll the keys
|
||||
*
|
||||
* @param None
|
||||
*
|
||||
* @return None
|
||||
**************************************************************************************************/
|
||||
void HAL_KeyPoll(void)
|
||||
{
|
||||
uint8_t keys = 0;
|
||||
if(HAL_PUSH_BUTTON1())
|
||||
{
|
||||
keys |= HAL_KEY_SW_1;
|
||||
}
|
||||
if(HAL_PUSH_BUTTON2())
|
||||
{
|
||||
keys |= HAL_KEY_SW_2;
|
||||
}
|
||||
if(HAL_PUSH_BUTTON3())
|
||||
{
|
||||
keys |= HAL_KEY_SW_3;
|
||||
}
|
||||
if(HAL_PUSH_BUTTON4())
|
||||
{
|
||||
keys |= HAL_KEY_SW_4;
|
||||
}
|
||||
if(keys == halKeySavedKeys)
|
||||
{ /* Exit - since no keys have changed */
|
||||
return;
|
||||
}
|
||||
halKeySavedKeys = keys; /* Store the current keys for comparation next time */
|
||||
/* Invoke Callback if new keys were depressed */
|
||||
if(keys && (pHalKeyProcessFunction))
|
||||
{
|
||||
(pHalKeyProcessFunction)(keys);
|
||||
}
|
||||
}
|
||||
|
||||
/******************************** endfile @ key ******************************/
|
||||
366
vd960DBN/BLE/HAL/LED.c
Normal file
366
vd960DBN/BLE/HAL/LED.c
Normal file
@@ -0,0 +1,366 @@
|
||||
/********************************** (C) COPYRIGHT *******************************
|
||||
* File Name : LED.c
|
||||
* Author : WCH
|
||||
* Version : V1.2
|
||||
* Date : 2022/01/18
|
||||
* Description :
|
||||
*********************************************************************************
|
||||
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
|
||||
* Attention: This software (modified or not) and binary are used for
|
||||
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
|
||||
*******************************************************************************/
|
||||
|
||||
/******************************************************************************/
|
||||
/* Header file contains */
|
||||
#include "HAL.h"
|
||||
|
||||
/* LED control structure */
|
||||
typedef struct
|
||||
{
|
||||
uint8_t mode; /* Operation mode */
|
||||
uint8_t todo; /* Blink cycles left */
|
||||
uint8_t onPct; /* On cycle percentage */
|
||||
uint16_t time; /* On/off cycle time (msec) */
|
||||
uint32_t next; /* Time for next change */
|
||||
} HalLedControl_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
HalLedControl_t HalLedControlTable[HAL_LED_DEFAULT_MAX_LEDS];
|
||||
uint8_t sleepActive;
|
||||
} HalLedStatus_t;
|
||||
|
||||
/***************************************************************************************************
|
||||
* GLOBAL VARIABLES
|
||||
***************************************************************************************************/
|
||||
static uint8_t HalLedState; // LED state at last set/clr/blink update
|
||||
|
||||
static uint8_t preBlinkState; // Original State before going to blink mode
|
||||
// bit 0, 1, 2, 3 represent led 0, 1, 2, 3
|
||||
static HalLedStatus_t HalLedStatusControl;
|
||||
|
||||
/***************************************************************************************************
|
||||
* LOCAL FUNCTION
|
||||
***************************************************************************************************/
|
||||
void HalLedOnOff(uint8_t leds, uint8_t mode);
|
||||
|
||||
/***************************************************************************************************
|
||||
* FUNCTIONS - API
|
||||
***************************************************************************************************/
|
||||
|
||||
/*********************************************************************
|
||||
* @fn HAL_LedInit
|
||||
*
|
||||
* @brief Initialize LED Service
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
void HAL_LedInit(void)
|
||||
{
|
||||
/* Initialize all LEDs to OFF */
|
||||
RCC_APB2PeriphClockCmd(LED1_PCENR, ENABLE);
|
||||
GPIO_InitTypeDef GPIO_InitStructure;
|
||||
GPIO_InitStructure.GPIO_Pin = LED1_BV;
|
||||
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
||||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
|
||||
GPIO_Init(LED1_GPIO, &GPIO_InitStructure);
|
||||
HalLedSet(HAL_LED_ALL, HAL_LED_MODE_OFF);
|
||||
// just test
|
||||
HalLedBlink(HAL_LED_1, 10, 30, 4000);
|
||||
/* Initialize sleepActive to FALSE */
|
||||
HalLedStatusControl.sleepActive = FALSE;
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn HalLedSet
|
||||
*
|
||||
* @brief Turn ON/OFF/TOGGLE given LEDs
|
||||
*
|
||||
* @param led - bit mask value of leds to be turned ON/OFF/TOGGLE
|
||||
* @param mode - BLINK, FLASH, TOGGLE, ON, OFF
|
||||
*
|
||||
* @return 0
|
||||
*/
|
||||
uint8_t HalLedSet(uint8_t leds, uint8_t mode)
|
||||
{
|
||||
uint8_t led;
|
||||
HalLedControl_t *sts;
|
||||
|
||||
switch(mode)
|
||||
{
|
||||
case HAL_LED_MODE_BLINK:
|
||||
{
|
||||
/* Default blink, 1 time, D% duty cycle */
|
||||
HalLedBlink(leds, 1, HAL_LED_DEFAULT_DUTY_CYCLE, HAL_LED_DEFAULT_FLASH_TIME);
|
||||
break;
|
||||
}
|
||||
|
||||
case HAL_LED_MODE_FLASH:
|
||||
{
|
||||
/* Default flash, N times, D% duty cycle */
|
||||
HalLedBlink(leds, HAL_LED_DEFAULT_FLASH_COUNT, HAL_LED_DEFAULT_DUTY_CYCLE, HAL_LED_DEFAULT_FLASH_TIME);
|
||||
break;
|
||||
}
|
||||
|
||||
case HAL_LED_MODE_ON:
|
||||
case HAL_LED_MODE_OFF:
|
||||
case HAL_LED_MODE_TOGGLE:
|
||||
{
|
||||
led = HAL_LED_1;
|
||||
leds &= HAL_LED_ALL;
|
||||
sts = HalLedStatusControl.HalLedControlTable;
|
||||
while(leds)
|
||||
{
|
||||
if(leds & led)
|
||||
{
|
||||
if(mode != HAL_LED_MODE_TOGGLE)
|
||||
{
|
||||
sts->mode = mode; /* ON or OFF */
|
||||
}
|
||||
else
|
||||
{
|
||||
sts->mode ^= HAL_LED_MODE_ON; /* Toggle */
|
||||
}
|
||||
HalLedOnOff(led, sts->mode);
|
||||
leds ^= led;
|
||||
}
|
||||
led <<= 1;
|
||||
sts++;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn HalLedBlink
|
||||
*
|
||||
* @brief Blink the leds
|
||||
*
|
||||
* @param led - bit mask value of leds to be turned ON/OFF/TOGGLE
|
||||
* @param numBlinks - number of blinks
|
||||
* @param percent - the percentage in each period where the led will be on
|
||||
* @param period - length of each cycle in milliseconds
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
void HalLedBlink(uint8_t leds, uint8_t numBlinks, uint8_t percent, uint16_t period)
|
||||
{
|
||||
uint8_t led;
|
||||
HalLedControl_t *sts;
|
||||
|
||||
if(leds && percent && period)
|
||||
{
|
||||
if(percent < 100)
|
||||
{
|
||||
led = HAL_LED_1;
|
||||
leds &= HAL_LED_ALL;
|
||||
sts = HalLedStatusControl.HalLedControlTable;
|
||||
while(leds)
|
||||
{
|
||||
if(leds & led)
|
||||
{
|
||||
/* Store the current state of the led before going to blinking */
|
||||
preBlinkState |= (led & HalLedState);
|
||||
sts->mode = HAL_LED_MODE_OFF; /* Stop previous blink */
|
||||
sts->time = period; /* Time for one on/off cycle */
|
||||
sts->onPct = percent; /* % of cycle LED is on */
|
||||
sts->todo = numBlinks; /* Number of blink cycles */
|
||||
if(!numBlinks)
|
||||
{
|
||||
sts->mode |= HAL_LED_MODE_FLASH; /* Continuous */
|
||||
}
|
||||
sts->next = TMOS_GetSystemClock(); /* Start now */
|
||||
sts->mode |= HAL_LED_MODE_BLINK; /* Enable blinking */
|
||||
leds ^= led;
|
||||
}
|
||||
led <<= 1;
|
||||
sts++;
|
||||
}
|
||||
tmos_start_task(halTaskID, LED_BLINK_EVENT, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
HalLedSet(leds, HAL_LED_MODE_ON); /* >= 100%, turn on */
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
HalLedSet(leds, HAL_LED_MODE_OFF); /* No on time, turn off */
|
||||
}
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn HalLedUpdate
|
||||
*
|
||||
* @brief Update leds to work with blink
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
void HalLedUpdate(void)
|
||||
{
|
||||
uint8_t led, pct, leds;
|
||||
uint16_t next, wait;
|
||||
uint32_t time;
|
||||
HalLedControl_t *sts;
|
||||
|
||||
next = 0;
|
||||
led = HAL_LED_1;
|
||||
leds = HAL_LED_ALL;
|
||||
sts = HalLedStatusControl.HalLedControlTable;
|
||||
|
||||
/* Check if sleep is active or not */
|
||||
if(!HalLedStatusControl.sleepActive)
|
||||
{
|
||||
while(leds)
|
||||
{
|
||||
if(leds & led)
|
||||
{
|
||||
if(sts->mode & HAL_LED_MODE_BLINK)
|
||||
{
|
||||
time = TMOS_GetSystemClock();
|
||||
if(time >= sts->next)
|
||||
{
|
||||
if(sts->mode & HAL_LED_MODE_ON)
|
||||
{
|
||||
pct = 100 - sts->onPct; /* Percentage of cycle for off */
|
||||
sts->mode &= ~HAL_LED_MODE_ON; /* Say it's not on */
|
||||
HalLedOnOff(led, HAL_LED_MODE_OFF); /* Turn it off */
|
||||
if(!(sts->mode & HAL_LED_MODE_FLASH))
|
||||
{
|
||||
if(sts->todo != 0xff)
|
||||
{
|
||||
sts->todo--; /* Not continuous, reduce count */
|
||||
}
|
||||
if(!sts->todo)
|
||||
{
|
||||
sts->mode ^= HAL_LED_MODE_BLINK; /* No more blinks */
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
pct = sts->onPct; /* Percentage of cycle for on */
|
||||
sts->mode |= HAL_LED_MODE_ON; /* Say it's on */
|
||||
HalLedOnOff(led, HAL_LED_MODE_ON); /* Turn it on */
|
||||
}
|
||||
if(sts->mode & HAL_LED_MODE_BLINK)
|
||||
{
|
||||
wait = (((uint32_t)pct * (uint32_t)sts->time) / 100);
|
||||
sts->next = time + wait;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* no more blink, no more wait */
|
||||
wait = 0;
|
||||
/* After blinking, set the LED back to the state before it blinks */
|
||||
HalLedSet(led, ((preBlinkState & led) != 0) ? HAL_LED_MODE_ON : HAL_LED_MODE_OFF);
|
||||
/* Clear the saved bit */
|
||||
preBlinkState &= (led ^ 0xFF);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
wait = sts->next - time; /* Time left */
|
||||
}
|
||||
if(!next || (wait && (wait < next)))
|
||||
{
|
||||
next = wait;
|
||||
}
|
||||
}
|
||||
leds ^= led;
|
||||
}
|
||||
led <<= 1;
|
||||
sts++;
|
||||
}
|
||||
if(next)
|
||||
{
|
||||
tmos_start_task(halTaskID, LED_BLINK_EVENT, next); /* Schedule event */
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn HalLedOnOff
|
||||
*
|
||||
* @brief Turns specified LED ON or OFF
|
||||
*
|
||||
* @param led - LED bit mask
|
||||
* @param mode - LED_ON,LED_OFF,
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
void HalLedOnOff(uint8_t leds, uint8_t mode)
|
||||
{
|
||||
if(leds & HAL_LED_1)
|
||||
{
|
||||
if(mode == HAL_LED_MODE_ON)
|
||||
{
|
||||
HAL_TURN_ON_LED1();
|
||||
}
|
||||
else
|
||||
{
|
||||
HAL_TURN_OFF_LED1();
|
||||
}
|
||||
}
|
||||
if(leds & HAL_LED_2)
|
||||
{
|
||||
if(mode == HAL_LED_MODE_ON)
|
||||
{
|
||||
HAL_TURN_ON_LED2();
|
||||
}
|
||||
else
|
||||
{
|
||||
HAL_TURN_OFF_LED2();
|
||||
}
|
||||
}
|
||||
if(leds & HAL_LED_3)
|
||||
{
|
||||
if(mode == HAL_LED_MODE_ON)
|
||||
{
|
||||
HAL_TURN_ON_LED3();
|
||||
}
|
||||
else
|
||||
{
|
||||
HAL_TURN_OFF_LED3();
|
||||
}
|
||||
}
|
||||
if(leds & HAL_LED_4)
|
||||
{
|
||||
if(mode == HAL_LED_MODE_ON)
|
||||
{
|
||||
HAL_TURN_ON_LED4();
|
||||
}
|
||||
else
|
||||
{
|
||||
HAL_TURN_OFF_LED4();
|
||||
}
|
||||
}
|
||||
/* Remember current state */
|
||||
if(mode)
|
||||
{
|
||||
HalLedState |= leds;
|
||||
}
|
||||
else
|
||||
{
|
||||
HalLedState &= (leds ^ 0xFF);
|
||||
}
|
||||
}
|
||||
|
||||
/***************************************************************************************************
|
||||
* @fn HalGetLedState
|
||||
*
|
||||
* @brief Dim LED2 - Dim (set level) of LED2
|
||||
*
|
||||
* @return led state
|
||||
*/
|
||||
uint8_t HalLedGetState()
|
||||
{
|
||||
return HalLedState;
|
||||
}
|
||||
|
||||
/******************************** endfile @ led ******************************/
|
||||
204
vd960DBN/BLE/HAL/Link.ld
Normal file
204
vd960DBN/BLE/HAL/Link.ld
Normal file
@@ -0,0 +1,204 @@
|
||||
ENTRY( _start )
|
||||
|
||||
__stack_size = 2048;
|
||||
|
||||
PROVIDE( _stack_size = __stack_size );
|
||||
|
||||
|
||||
MEMORY
|
||||
{
|
||||
/* CH32V20x_D6 - CH32V203F6-CH32V203G6-CH32V203C6 */
|
||||
/*
|
||||
FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 32K
|
||||
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 10K
|
||||
*/
|
||||
|
||||
/* CH32V20x_D6 - CH32V203K8-CH32V203C8-CH32V203G8-CH32V203F8 */
|
||||
/*
|
||||
FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 64K
|
||||
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 20K
|
||||
*/
|
||||
|
||||
/* CH32V20x_D8 - CH32V203RB
|
||||
CH32V20x_D8W - CH32V208x
|
||||
FLASH + RAM supports the following configuration
|
||||
FLASH-128K + RAM-64K
|
||||
FLASH-144K + RAM-48K
|
||||
FLASH-160K + RAM-32K
|
||||
*/
|
||||
FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 448K
|
||||
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 64K
|
||||
|
||||
}
|
||||
|
||||
|
||||
SECTIONS
|
||||
{
|
||||
|
||||
.init :
|
||||
{
|
||||
_sinit = .;
|
||||
. = ALIGN(4);
|
||||
KEEP(*(SORT_NONE(.init)))
|
||||
. = ALIGN(4);
|
||||
_einit = .;
|
||||
} >FLASH AT>FLASH
|
||||
|
||||
.vector :
|
||||
{
|
||||
*(.vector);
|
||||
. = ALIGN(64);
|
||||
KEEP(*(SORT_NONE(.handle_reset)))
|
||||
} >FLASH AT>FLASH
|
||||
|
||||
.highcode :
|
||||
{
|
||||
. = ALIGN(4);
|
||||
*(.highcode);
|
||||
*(.highcode.*);
|
||||
. = ALIGN(4);
|
||||
} >FLASH AT>FLASH
|
||||
|
||||
.text :
|
||||
{
|
||||
. = ALIGN(4);
|
||||
|
||||
EXCLUDE_FILE (*wchble.a) *(.text .text*)
|
||||
|
||||
*(.text)
|
||||
*(.text.*)
|
||||
*(.rodata)
|
||||
*(.rodata*)
|
||||
*(.sdata2.*)
|
||||
*(.glue_7)
|
||||
*(.glue_7t)
|
||||
*(.gnu.linkonce.t.*)
|
||||
. = ALIGN(4);
|
||||
} >FLASH AT>FLASH
|
||||
|
||||
.fini :
|
||||
{
|
||||
KEEP(*(SORT_NONE(.fini)))
|
||||
. = ALIGN(4);
|
||||
} >FLASH AT>FLASH
|
||||
|
||||
PROVIDE( _etext = . );
|
||||
PROVIDE( _eitcm = . );
|
||||
|
||||
.preinit_array :
|
||||
{
|
||||
PROVIDE_HIDDEN (__preinit_array_start = .);
|
||||
KEEP (*(.preinit_array))
|
||||
PROVIDE_HIDDEN (__preinit_array_end = .);
|
||||
} >FLASH AT>FLASH
|
||||
|
||||
.init_array :
|
||||
{
|
||||
PROVIDE_HIDDEN (__init_array_start = .);
|
||||
KEEP (*(SORT_BY_INIT_PRIORITY(.init_array.*) SORT_BY_INIT_PRIORITY(.ctors.*)))
|
||||
KEEP (*(.init_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .ctors))
|
||||
PROVIDE_HIDDEN (__init_array_end = .);
|
||||
} >FLASH AT>FLASH
|
||||
|
||||
.fini_array :
|
||||
{
|
||||
PROVIDE_HIDDEN (__fini_array_start = .);
|
||||
KEEP (*(SORT_BY_INIT_PRIORITY(.fini_array.*) SORT_BY_INIT_PRIORITY(.dtors.*)))
|
||||
KEEP (*(.fini_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .dtors))
|
||||
PROVIDE_HIDDEN (__fini_array_end = .);
|
||||
} >FLASH AT>FLASH
|
||||
|
||||
.ctors :
|
||||
{
|
||||
/* gcc uses crtbegin.o to find the start of
|
||||
the constructors, so we make sure it is
|
||||
first. Because this is a wildcard, it
|
||||
doesn't matter if the user does not
|
||||
actually link against crtbegin.o; the
|
||||
linker won't look for a file to match a
|
||||
wildcard. The wildcard also means that it
|
||||
doesn't matter which directory crtbegin.o
|
||||
is in. */
|
||||
KEEP (*crtbegin.o(.ctors))
|
||||
KEEP (*crtbegin?.o(.ctors))
|
||||
/* We don't want to include the .ctor section from
|
||||
the crtend.o file until after the sorted ctors.
|
||||
The .ctor section from the crtend file contains the
|
||||
end of ctors marker and it must be last */
|
||||
KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .ctors))
|
||||
KEEP (*(SORT(.ctors.*)))
|
||||
KEEP (*(.ctors))
|
||||
} >FLASH AT>FLASH
|
||||
|
||||
.dtors :
|
||||
{
|
||||
KEEP (*crtbegin.o(.dtors))
|
||||
KEEP (*crtbegin?.o(.dtors))
|
||||
KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .dtors))
|
||||
KEEP (*(SORT(.dtors.*)))
|
||||
KEEP (*(.dtors))
|
||||
} >FLASH AT>FLASH
|
||||
|
||||
.dalign :
|
||||
{
|
||||
. = ALIGN(4);
|
||||
PROVIDE(_data_vma = .);
|
||||
} >RAM AT>FLASH
|
||||
|
||||
.dlalign :
|
||||
{
|
||||
. = ALIGN(4);
|
||||
PROVIDE(_data_lma = .);
|
||||
} >FLASH AT>FLASH
|
||||
|
||||
.data :
|
||||
{
|
||||
*(.gnu.linkonce.r.*)
|
||||
*(.data .data.*)
|
||||
*(.gnu.linkonce.d.*)
|
||||
. = ALIGN(8);
|
||||
PROVIDE( __global_pointer$ = . + 0x800 );
|
||||
*(.sdata .sdata.*)
|
||||
*(.gnu.linkonce.s.*)
|
||||
. = ALIGN(8);
|
||||
*(.srodata.cst16)
|
||||
*(.srodata.cst8)
|
||||
*(.srodata.cst4)
|
||||
*(.srodata.cst2)
|
||||
*(.srodata .srodata.*)
|
||||
. = ALIGN(4);
|
||||
PROVIDE( _edata = .);
|
||||
} >RAM AT>FLASH
|
||||
|
||||
.bss :
|
||||
{
|
||||
. = ALIGN(4);
|
||||
PROVIDE( _sbss = .);
|
||||
*(.sbss*)
|
||||
*(.gnu.linkonce.sb.*)
|
||||
*(.bss*)
|
||||
*(.gnu.linkonce.b.*)
|
||||
*(COMMON*)
|
||||
. = ALIGN(4);
|
||||
PROVIDE( _ebss = .);
|
||||
} >RAM AT>FLASH
|
||||
|
||||
PROVIDE( _end = _ebss);
|
||||
PROVIDE( end = . );
|
||||
|
||||
.stack ORIGIN(RAM) + LENGTH(RAM) - __stack_size :
|
||||
{
|
||||
PROVIDE( _heap_end = . );
|
||||
. = ALIGN(4);
|
||||
PROVIDE(_susrstack = . );
|
||||
. = . + __stack_size;
|
||||
PROVIDE( _eusrstack = .);
|
||||
} >RAM
|
||||
|
||||
/*.stack ORIGIN(RAM)+LENGTH(RAM) :
|
||||
{
|
||||
PROVIDE( _heap_end = . );
|
||||
. = ALIGN(4);
|
||||
PROVIDE(_eusrstack = . );
|
||||
} >RAM */
|
||||
}
|
||||
326
vd960DBN/BLE/HAL/MCU.c
Normal file
326
vd960DBN/BLE/HAL/MCU.c
Normal file
@@ -0,0 +1,326 @@
|
||||
/********************************** (C) COPYRIGHT *******************************
|
||||
* File Name : MCU.c
|
||||
* Author : WCH
|
||||
* Version : V1.2
|
||||
* Date : 2022/01/18
|
||||
* Description : HAL task processing function and BLE and hardware initialization
|
||||
*********************************************************************************
|
||||
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
|
||||
* Attention: This software (modified or not) and binary are used for
|
||||
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
|
||||
*******************************************************************************/
|
||||
|
||||
/******************************************************************************/
|
||||
/* Header file contains */
|
||||
#include "HAL.h"
|
||||
#include "string.h"
|
||||
|
||||
tmosTaskID halTaskID;
|
||||
uint32_t g_LLE_IRQLibHandlerLocation;
|
||||
|
||||
/*******************************************************************************
|
||||
* @fn Lib_Calibration_LSI
|
||||
*
|
||||
* @brief Internal 32K calibration
|
||||
*
|
||||
* @param None.
|
||||
*
|
||||
* @return None.
|
||||
*/
|
||||
void Lib_Calibration_LSI(void)
|
||||
{
|
||||
Calibration_LSI(Level_64);
|
||||
}
|
||||
|
||||
#if(defined(BLE_SNV)) && (BLE_SNV == TRUE)
|
||||
/*******************************************************************************
|
||||
* @fn Lib_Read_Flash
|
||||
*
|
||||
* @brief Callback function used for BLE lib.
|
||||
*
|
||||
* @param addr.
|
||||
* @param num.
|
||||
* @param pBuf.
|
||||
*
|
||||
* @return None.
|
||||
*/
|
||||
uint32_t Lib_Read_Flash(uint32_t addr, uint32_t num, uint32_t *pBuf)
|
||||
{
|
||||
tmos_memcpy(pBuf, (uint32_t*)addr, num*4);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*******************************************************************************
|
||||
* @fn Lib_Write_Flash
|
||||
*
|
||||
* @brief Callback function used for BLE lib.
|
||||
*
|
||||
* @param addr.
|
||||
* @param num.
|
||||
* @param pBuf.
|
||||
*
|
||||
* @return None.
|
||||
*/
|
||||
uint32_t Lib_Write_Flash(uint32_t addr, uint32_t num, uint32_t *pBuf)
|
||||
{
|
||||
FLASH_Unlock_Fast();
|
||||
FLASH_ErasePage_Fast( addr );
|
||||
FLASH_ProgramPage_Fast( addr, pBuf);
|
||||
FLASH_Lock_Fast();
|
||||
Delay_Us(1);
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
/*******************************************************************************
|
||||
* @fn WCHBLE_Init
|
||||
*
|
||||
* @brief BLE library initialization
|
||||
*
|
||||
* @param None.
|
||||
*
|
||||
* @return None.
|
||||
*/
|
||||
void WCHBLE_Init(void)
|
||||
{
|
||||
uint8_t i;
|
||||
bleConfig_t cfg;
|
||||
|
||||
g_LLE_IRQLibHandlerLocation = (uint32_t)LLE_IRQLibHandler;
|
||||
|
||||
if(!tmos_memcmp(VER_LIB, VER_FILE, strlen(VER_FILE)))
|
||||
{
|
||||
PRINT("head file error...\n");
|
||||
while(1);
|
||||
}
|
||||
|
||||
// 32M crystal capacitance and current
|
||||
OSC->HSE_CAL_CTRL &= ~(0x07<<28);
|
||||
OSC->HSE_CAL_CTRL |= 0x03<<28;
|
||||
OSC->HSE_CAL_CTRL |= 3<<24;
|
||||
|
||||
tmos_memset(&cfg, 0, sizeof(bleConfig_t));
|
||||
cfg.MEMAddr = (uint32_t)MEM_BUF;
|
||||
cfg.MEMLen = (uint32_t)BLE_MEMHEAP_SIZE;
|
||||
cfg.BufMaxLen = (uint32_t)BLE_BUFF_MAX_LEN;
|
||||
cfg.BufNumber = (uint32_t)BLE_BUFF_NUM;
|
||||
cfg.TxNumEvent = (uint32_t)BLE_TX_NUM_EVENT;
|
||||
cfg.TxPower = (uint32_t)BLE_TX_POWER;
|
||||
#if(defined(BLE_SNV)) && (BLE_SNV == TRUE)
|
||||
cfg.SNVAddr = (uint32_t)BLE_SNV_ADDR;
|
||||
cfg.SNVNum = (uint32_t)BLE_SNV_NUM;
|
||||
cfg.readFlashCB = Lib_Read_Flash;
|
||||
cfg.writeFlashCB = Lib_Write_Flash;
|
||||
#endif
|
||||
cfg.ClockFrequency = CAB_LSIFQ/2;
|
||||
#if(CLK_OSC32K==0)
|
||||
cfg.ClockAccuracy = 50;
|
||||
#else
|
||||
cfg.ClockAccuracy = 1000;
|
||||
#endif
|
||||
cfg.ConnectNumber = (PERIPHERAL_MAX_CONNECTION & 3) | (CENTRAL_MAX_CONNECTION << 2);
|
||||
#if(defined TEM_SAMPLE) && (TEM_SAMPLE == TRUE)
|
||||
// Calibrate RF and internal RC according to temperature changes (greater than 7 degrees Celsius)
|
||||
cfg.tsCB = HAL_GetInterTempValue;
|
||||
#if(CLK_OSC32K)
|
||||
cfg.rcCB = Lib_Calibration_LSI; // Internal 32K clock calibration
|
||||
#endif
|
||||
#endif
|
||||
#if(defined(HAL_SLEEP)) && (HAL_SLEEP == TRUE)
|
||||
cfg.idleCB = BLE_LowPower; // Enable sleep
|
||||
#endif
|
||||
#if(defined(BLE_MAC)) && (BLE_MAC == TRUE)
|
||||
for(i = 0; i < 6; i++)
|
||||
{
|
||||
cfg.MacAddr[i] = MacAddr[5 - i];
|
||||
}
|
||||
#else
|
||||
{
|
||||
uint8_t MacAddr[6];
|
||||
FLASH_GetMACAddress(MacAddr);
|
||||
for(i = 0; i < 6; i++)
|
||||
{
|
||||
cfg.MacAddr[i] = MacAddr[i]; // Use chip mac address
|
||||
}
|
||||
}
|
||||
#endif
|
||||
if(!cfg.MEMAddr || cfg.MEMLen < 4 * 1024)
|
||||
{
|
||||
while(1);
|
||||
}
|
||||
i = BLE_LibInit(&cfg);
|
||||
if(i)
|
||||
{
|
||||
PRINT("LIB init error code: %x ...\n", i);
|
||||
while(1);
|
||||
}
|
||||
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_CRC, ENABLE );
|
||||
NVIC_EnableIRQ( BB_IRQn );
|
||||
NVIC_EnableIRQ( LLE_IRQn );
|
||||
}
|
||||
|
||||
/*******************************************************************************
|
||||
* @fn HAL_ProcessEvent
|
||||
*
|
||||
* @brief HAL processing
|
||||
*
|
||||
* @param task_id - The TMOS assigned task ID.
|
||||
* @param events - events to process. This is a bit map and can
|
||||
* contain more than one event.
|
||||
*
|
||||
* @return events.
|
||||
*/
|
||||
tmosEvents HAL_ProcessEvent(tmosTaskID task_id, tmosEvents events)
|
||||
{
|
||||
uint8_t *msgPtr;
|
||||
|
||||
if(events & SYS_EVENT_MSG)
|
||||
{
|
||||
/**
|
||||
* Process the HAL layer message, call tmos_msg_receive to read the message,
|
||||
* and delete the message after processing.
|
||||
*/
|
||||
msgPtr = tmos_msg_receive(task_id);
|
||||
if(msgPtr)
|
||||
{
|
||||
/* De-allocate */
|
||||
tmos_msg_deallocate(msgPtr);
|
||||
}
|
||||
return events ^ SYS_EVENT_MSG;
|
||||
}
|
||||
if(events & LED_BLINK_EVENT)
|
||||
{
|
||||
#if(defined HAL_LED) && (HAL_LED == TRUE)
|
||||
HalLedUpdate();
|
||||
#endif // HAL_LED
|
||||
return events ^ LED_BLINK_EVENT;
|
||||
}
|
||||
if(events & HAL_KEY_EVENT)
|
||||
{
|
||||
#if(defined HAL_KEY) && (HAL_KEY == TRUE)
|
||||
HAL_KeyPoll(); /* Check for keys */
|
||||
tmos_start_task(halTaskID, HAL_KEY_EVENT, MS1_TO_SYSTEM_TIME(100));
|
||||
return events ^ HAL_KEY_EVENT;
|
||||
#endif
|
||||
}
|
||||
if(events & HAL_REG_INIT_EVENT)
|
||||
{
|
||||
#if(defined BLE_CALIBRATION_ENABLE) && (BLE_CALIBRATION_ENABLE == TRUE) // Calibration tasks, a single time is less than 10ms
|
||||
BLE_RegInit(); // Calibrate RF
|
||||
#if(CLK_OSC32K)
|
||||
Lib_Calibration_LSI(); // Calibrate internal RC
|
||||
#endif
|
||||
tmos_start_task(halTaskID, HAL_REG_INIT_EVENT, MS1_TO_SYSTEM_TIME(BLE_CALIBRATION_PERIOD));
|
||||
return events ^ HAL_REG_INIT_EVENT;
|
||||
#endif
|
||||
}
|
||||
if(events & HAL_TEST_EVENT)
|
||||
{
|
||||
PRINT("* \n");
|
||||
tmos_start_task(halTaskID, HAL_TEST_EVENT, MS1_TO_SYSTEM_TIME(1000));
|
||||
return events ^ HAL_TEST_EVENT;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*******************************************************************************
|
||||
* @fn HAL_Init
|
||||
*
|
||||
* @brief Ó²¼þ³õʼ»¯
|
||||
*
|
||||
* @param None.
|
||||
*
|
||||
* @return None.
|
||||
*/
|
||||
void HAL_Init()
|
||||
{
|
||||
halTaskID = TMOS_ProcessEventRegister(HAL_ProcessEvent);
|
||||
HAL_TimeInit();
|
||||
#if(defined HAL_SLEEP) && (HAL_SLEEP == TRUE)
|
||||
HAL_SleepInit();
|
||||
#endif
|
||||
#if(defined HAL_LED) && (HAL_LED == TRUE)
|
||||
HAL_LedInit();
|
||||
#endif
|
||||
#if(defined HAL_KEY) && (HAL_KEY == TRUE)
|
||||
HAL_KeyInit();
|
||||
#endif
|
||||
#if(defined BLE_CALIBRATION_ENABLE) && (BLE_CALIBRATION_ENABLE == TRUE)
|
||||
// Add a calibration task, and a single calibration takes less than 10ms
|
||||
tmos_start_task(halTaskID, HAL_REG_INIT_EVENT, MS1_TO_SYSTEM_TIME(BLE_CALIBRATION_PERIOD));
|
||||
#endif
|
||||
// tmos_start_task(halTaskID, HAL_TEST_EVENT, MS1_TO_SYSTEM_TIME(1000)); // Add a test task
|
||||
}
|
||||
|
||||
/*******************************************************************************
|
||||
* @fn HAL_GetInterTempValue
|
||||
*
|
||||
* @brief Get the internal temperature sampling value, if the ADC interrupt sampling is used,
|
||||
* it is necessary to temporarily shield the interrupt in this function.
|
||||
*
|
||||
* @return Internal temperature sampling value.
|
||||
*/
|
||||
uint16_t HAL_GetInterTempValue(void)
|
||||
{
|
||||
uint32_t rcc_apb2pcenr, rcc_cfgr0, adc1_ctrl1, adc1_ctrl2, adc1_rsqr1, adc1_rsqr2, adc1_rsqr3, adc1_samptr1, adc1_samptr2;
|
||||
uint32_t adc1_iofr1, adc1_iofr2, adc1_iofr3, adc1_iofr4, adc1_wdhtr, adc1_wdltr, adc1_isqr;
|
||||
ADC_InitTypeDef ADC_InitStructure = {0};
|
||||
uint16_t adc_data;
|
||||
|
||||
rcc_apb2pcenr = RCC->APB2PCENR;
|
||||
rcc_cfgr0 = RCC->CFGR0;
|
||||
adc1_ctrl1 = ADC1->CTLR1;
|
||||
adc1_ctrl2 = ADC1->CTLR2;
|
||||
adc1_rsqr1 = ADC1->RSQR1;
|
||||
adc1_rsqr2 = ADC1->RSQR2;
|
||||
adc1_rsqr3 = ADC1->RSQR3;
|
||||
adc1_samptr1 = ADC1->SAMPTR1;
|
||||
adc1_samptr2 = ADC1->SAMPTR2;
|
||||
adc1_iofr1 = ADC1->IOFR1;
|
||||
adc1_iofr2 = ADC1->IOFR2;
|
||||
adc1_iofr3 = ADC1->IOFR3;
|
||||
adc1_iofr4 = ADC1->IOFR4;
|
||||
adc1_wdhtr = ADC1->WDHTR;
|
||||
adc1_wdltr = ADC1->WDLTR;
|
||||
adc1_isqr = ADC1->ISQR;
|
||||
|
||||
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
|
||||
RCC_ADCCLKConfig(RCC_PCLK2_Div8);
|
||||
ADC_DeInit(ADC1);
|
||||
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
|
||||
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
|
||||
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
|
||||
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
|
||||
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
|
||||
ADC_InitStructure.ADC_NbrOfChannel = 1;
|
||||
ADC_Init(ADC1, &ADC_InitStructure);
|
||||
|
||||
ADC_Cmd(ADC1, ENABLE);
|
||||
ADC_BufferCmd(ADC1, ENABLE); //enable buffer
|
||||
ADC_TempSensorVrefintCmd(ENABLE);
|
||||
ADC_RegularChannelConfig(ADC1, ADC_Channel_TempSensor, 1, ADC_SampleTime_239Cycles5);
|
||||
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
|
||||
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC));
|
||||
adc_data = ADC_GetConversionValue(ADC1);
|
||||
|
||||
ADC_DeInit(ADC1);
|
||||
RCC->APB2PCENR = rcc_apb2pcenr;
|
||||
RCC->CFGR0 = rcc_cfgr0;
|
||||
ADC1->CTLR1 = adc1_ctrl1;
|
||||
ADC1->CTLR2 = adc1_ctrl2;
|
||||
ADC1->RSQR1 = adc1_rsqr1;
|
||||
ADC1->RSQR2 = adc1_rsqr2;
|
||||
ADC1->RSQR3 = adc1_rsqr3;
|
||||
ADC1->SAMPTR1 = adc1_samptr1;
|
||||
ADC1->SAMPTR2 = adc1_samptr2;
|
||||
ADC1->IOFR1 = adc1_iofr1;
|
||||
ADC1->IOFR2 = adc1_iofr2;
|
||||
ADC1->IOFR3 = adc1_iofr3;
|
||||
ADC1->IOFR4 = adc1_iofr4;
|
||||
ADC1->WDHTR = adc1_wdhtr;
|
||||
ADC1->WDLTR = adc1_wdltr;
|
||||
ADC1->ISQR = adc1_isqr;
|
||||
return (adc_data);
|
||||
}
|
||||
|
||||
/******************************** endfile @ mcu ******************************/
|
||||
113
vd960DBN/BLE/HAL/RTC.c
Normal file
113
vd960DBN/BLE/HAL/RTC.c
Normal file
@@ -0,0 +1,113 @@
|
||||
/********************************** (C) COPYRIGHT *******************************
|
||||
* File Name : RTC.c
|
||||
* Author : WCH
|
||||
* Version : V1.2
|
||||
* Date : 2022/01/18
|
||||
* Description : RTC configuration and its initialization
|
||||
*********************************************************************************
|
||||
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
|
||||
* Attention: This software (modified or not) and binary are used for
|
||||
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
|
||||
*******************************************************************************/
|
||||
|
||||
/******************************************************************************/
|
||||
/* Header file contains */
|
||||
#include "HAL.h"
|
||||
|
||||
/*********************************************************************
|
||||
* CONSTANTS
|
||||
*/
|
||||
#define RTC_INIT_TIME_HOUR 0
|
||||
#define RTC_INIT_TIME_MINUTE 0
|
||||
#define RTC_INIT_TIME_SECEND 0
|
||||
|
||||
/***************************************************
|
||||
* Global variables
|
||||
*/
|
||||
volatile uint32_t RTCTigFlag;
|
||||
|
||||
/*******************************************************************************
|
||||
* @fn RTC_SetTignTime
|
||||
*
|
||||
* @brief Configure RTC trigger time
|
||||
*
|
||||
* @param time - Trigger time.
|
||||
*
|
||||
* @return None.
|
||||
*/
|
||||
void RTC_SetTignTime(uint32_t time)
|
||||
{
|
||||
RTC_WaitForLastTask();
|
||||
RTC_SetAlarm(time);
|
||||
RTC_WaitForLastTask();
|
||||
RTCTigFlag = 0;
|
||||
}
|
||||
|
||||
/*******************************************************************************
|
||||
* @fn HAL_Time0Init
|
||||
*
|
||||
* @brief System timer initialization
|
||||
*
|
||||
* @param None.
|
||||
*
|
||||
* @return None.
|
||||
*/
|
||||
void HAL_TimeInit(void)
|
||||
{
|
||||
uint16_t temp=0;
|
||||
uint8_t state=0;
|
||||
bleClockConfig_t conf={0};
|
||||
|
||||
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR|RCC_APB1Periph_BKP, ENABLE);
|
||||
PWR_BackupAccessCmd(ENABLE);
|
||||
#if( CLK_OSC32K )
|
||||
RCC_LSICmd(ENABLE);
|
||||
RCC_LSEConfig(RCC_LSE_OFF);
|
||||
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);
|
||||
#else
|
||||
RCC_LSEConfig(RCC_LSE_ON);
|
||||
/* Check the specified RCC logo position settings or not,
|
||||
* wait for the low-speed crystal oscillator to be ready */
|
||||
while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
|
||||
{
|
||||
temp++;
|
||||
Delay_Ms(10);
|
||||
}
|
||||
if(temp>=250)
|
||||
{
|
||||
printf("time error..\n");
|
||||
}
|
||||
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
|
||||
#endif
|
||||
RCC_RTCCLKCmd(ENABLE);
|
||||
RTC_WaitForLastTask();
|
||||
RTC_WaitForLastTask();
|
||||
RTC_SetPrescaler(1);
|
||||
RTC_WaitForLastTask();
|
||||
RTC_SetCounter(0);
|
||||
RTC_WaitForLastTask();
|
||||
#if( CLK_OSC32K )
|
||||
Lib_Calibration_LSI();
|
||||
#endif
|
||||
conf.ClockAccuracy = CLK_OSC32K?1000:100;
|
||||
conf.ClockFrequency = CAB_LSIFQ/2;
|
||||
conf.ClockMaxCount = 0xFFFFFFFF;
|
||||
conf.getClockValue = RTC_GetCounter;
|
||||
state = TMOS_TimerInit( &conf );
|
||||
if(state)
|
||||
{
|
||||
PRINT("TMOS_TimerInit err %x\n",state);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
__attribute__((interrupt("WCH-Interrupt-fast")))
|
||||
void RTCAlarm_IRQHandler(void)
|
||||
{
|
||||
RTCTigFlag = 1;
|
||||
EXTI_ClearITPendingBit(EXTI_Line17);
|
||||
RTC_ClearITPendingBit(RTC_IT_ALR);
|
||||
RTC_WaitForLastTask();
|
||||
}
|
||||
|
||||
/******************************** endfile @ time ******************************/
|
||||
107
vd960DBN/BLE/HAL/SLEEP.c
Normal file
107
vd960DBN/BLE/HAL/SLEEP.c
Normal file
@@ -0,0 +1,107 @@
|
||||
/********************************** (C) COPYRIGHT *******************************
|
||||
* File Name : SLEEP.c
|
||||
* Author : WCH
|
||||
* Version : V1.2
|
||||
* Date : 2022/01/18
|
||||
* Description : Sleep configuration and its initialization
|
||||
*********************************************************************************
|
||||
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
|
||||
* Attention: This software (modified or not) and binary are used for
|
||||
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
|
||||
*******************************************************************************/
|
||||
|
||||
/******************************************************************************/
|
||||
/* Header file contains */
|
||||
#include "HAL.h"
|
||||
|
||||
#define US_TO_TICK(us) (uint32_t)((us) / (1000000 / ((CAB_LSIFQ / 2))))
|
||||
|
||||
#define SLEEP_PERIOD_MIN_US 200
|
||||
#define SLEEP_PERIOD_MAX_TICK 0xFFD2393F
|
||||
#define SLEEP_PERIOD_MIN_TICK US_TO_TICK(SLEEP_PERIOD_MIN_US)
|
||||
#define HESREADY_TICK US_TO_TICK(WAKE_UP_MAX_TIME_US)
|
||||
|
||||
/*******************************************************************************
|
||||
* @fn BLE_LowPower
|
||||
*
|
||||
* @brief 启动睡眠
|
||||
*
|
||||
* @param time - 唤醒的时间点(RTC绝对值)
|
||||
*
|
||||
* @return state.
|
||||
*/
|
||||
uint32_t BLE_LowPower(uint32_t time)
|
||||
{
|
||||
#if(defined(HAL_SLEEP)) && (HAL_SLEEP == TRUE)
|
||||
uint32_t current_time;
|
||||
uint32_t sleep_period;
|
||||
uint32_t wake_time;
|
||||
|
||||
wake_time = time - HESREADY_TICK;
|
||||
|
||||
__disable_irq();
|
||||
current_time = RTC_GetCounter();
|
||||
sleep_period = wake_time - current_time;
|
||||
|
||||
if((sleep_period < SLEEP_PERIOD_MIN_TICK) || (sleep_period > SLEEP_PERIOD_MAX_TICK))
|
||||
{
|
||||
__enable_irq();
|
||||
return 2;
|
||||
}
|
||||
|
||||
RTC_SetTignTime(wake_time);
|
||||
__enable_irq();
|
||||
|
||||
#if(DEBUG == DEBUG_UART1) // To use other serial ports to output printing information, you need to modify this line of code
|
||||
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
|
||||
{
|
||||
__NOP();
|
||||
}
|
||||
#endif
|
||||
// LOW POWER-sleep
|
||||
if(!RTCTigFlag)
|
||||
{
|
||||
PWR_EnterSTOPMode_RAM_LV(PWR_Regulator_LowPower, PWR_STOPEntry_WFI);
|
||||
SystemInit();
|
||||
}
|
||||
else
|
||||
{
|
||||
return 3;
|
||||
}
|
||||
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*******************************************************************************
|
||||
* @fn HAL_SleepInit
|
||||
*
|
||||
* @brief Configure sleep Wake-up source - RTC wake up, trigger mode
|
||||
*
|
||||
* @param None.
|
||||
*
|
||||
* @return None.
|
||||
*/
|
||||
void HAL_SleepInit(void)
|
||||
{
|
||||
#if(defined(HAL_SLEEP)) && (HAL_SLEEP == TRUE)
|
||||
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
|
||||
RTC_WaitForLastTask();
|
||||
|
||||
RTC_ITConfig(RTC_IT_ALR, ENABLE);
|
||||
EXTI_InitTypeDef EXTI_InitStructure = {0};
|
||||
NVIC_InitTypeDef NVIC_InitStructure = {0};
|
||||
|
||||
EXTI_InitStructure.EXTI_Line = EXTI_Line17;
|
||||
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
|
||||
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
|
||||
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
|
||||
EXTI_Init(&EXTI_InitStructure);
|
||||
|
||||
NVIC_InitStructure.NVIC_IRQChannel = RTCAlarm_IRQn;
|
||||
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
|
||||
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
|
||||
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
|
||||
NVIC_Init(&NVIC_InitStructure);
|
||||
#endif
|
||||
}
|
||||
82
vd960DBN/BLE/HAL/include/HAL.h
Normal file
82
vd960DBN/BLE/HAL/include/HAL.h
Normal file
@@ -0,0 +1,82 @@
|
||||
/********************************** (C) COPYRIGHT *******************************
|
||||
* File Name : HAL.h
|
||||
* Author : WCH
|
||||
* Version : V1.0
|
||||
* Date : 2016/05/05
|
||||
* Description :
|
||||
*********************************************************************************
|
||||
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
|
||||
* Attention: This software (modified or not) and binary are used for
|
||||
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
|
||||
*******************************************************************************/
|
||||
|
||||
/******************************************************************************/
|
||||
#ifndef __HAL_H
|
||||
#define __HAL_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "config.h"
|
||||
#include "RTC.h"
|
||||
#include "SLEEP.h"
|
||||
#include "KEY.h"
|
||||
#include "LED.h"
|
||||
|
||||
/* hal task Event */
|
||||
#define LED_BLINK_EVENT 0x0001
|
||||
#define HAL_KEY_EVENT 0x0002
|
||||
#define HAL_REG_INIT_EVENT 0x2000
|
||||
#define HAL_TEST_EVENT 0x4000
|
||||
|
||||
/*********************************************************************
|
||||
* GLOBAL VARIABLES
|
||||
*/
|
||||
extern tmosTaskID halTaskID;
|
||||
|
||||
/*********************************************************************
|
||||
* GLOBAL FUNCTIONS
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief Hardware initialization
|
||||
*/
|
||||
extern void HAL_Init(void);
|
||||
|
||||
/**
|
||||
* @brief HAL processing
|
||||
*
|
||||
* @param task_id - The TMOS assigned task ID.
|
||||
* @param events - events to process. This is a bit map and can
|
||||
* contain more than one event.
|
||||
*/
|
||||
extern tmosEvents HAL_ProcessEvent(tmosTaskID task_id, tmosEvents events);
|
||||
|
||||
/**
|
||||
* @brief Initialization of the BLE library
|
||||
*/
|
||||
extern void WCHBLE_Init(void);
|
||||
|
||||
/**
|
||||
* @brief Get the internal temperature sampling value.
|
||||
* If the ADC interrupt sampling is used,
|
||||
* the interrupt is temporarily shielded in this function.
|
||||
*
|
||||
* @return Internal temperature sampling value.
|
||||
*/
|
||||
extern uint16_t HAL_GetInterTempValue(void);
|
||||
|
||||
/**
|
||||
* @brief Internal 32K calibration
|
||||
*/
|
||||
extern void Lib_Calibration_LSI(void);
|
||||
|
||||
/*********************************************************************
|
||||
*********************************************************************/
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
112
vd960DBN/BLE/HAL/include/KEY.h
Normal file
112
vd960DBN/BLE/HAL/include/KEY.h
Normal file
@@ -0,0 +1,112 @@
|
||||
/********************************** (C) COPYRIGHT *******************************
|
||||
* File Name : KEY.h
|
||||
* Author : WCH
|
||||
* Version : V1.0
|
||||
* Date : 2016/04/12
|
||||
* Description :
|
||||
*********************************************************************************
|
||||
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
|
||||
* Attention: This software (modified or not) and binary are used for
|
||||
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
|
||||
*******************************************************************************/
|
||||
|
||||
/******************************************************************************/
|
||||
#ifndef __KEY_H
|
||||
#define __KEY_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**************************************************************************************************
|
||||
* MACROS
|
||||
**************************************************************************************************/
|
||||
#define HAL_KEY_POLLING_VALUE 100
|
||||
|
||||
/* Switches (keys) */
|
||||
#define HAL_KEY_SW_1 0x01 // key1
|
||||
#define HAL_KEY_SW_2 0x02 // key2
|
||||
#define HAL_KEY_SW_3 0x04 // key3
|
||||
#define HAL_KEY_SW_4 0x08 // key4
|
||||
|
||||
/* Key definition */
|
||||
|
||||
/* 1 - KEY */
|
||||
#define KEY1_PCENR (RCC_APB2Periph_GPIOB)
|
||||
#define KEY2_PCENR ()
|
||||
#define KEY3_PCENR ()
|
||||
#define KEY4_PCENR ()
|
||||
|
||||
#define KEY1_GPIO (GPIOB)
|
||||
#define KEY2_GPIO ()
|
||||
#define KEY3_GPIO ()
|
||||
#define KEY4_GPIO ()
|
||||
|
||||
#define KEY1_BV BV(13)
|
||||
#define KEY2_BV ()
|
||||
#define KEY3_BV ()
|
||||
#define KEY4_BV ()
|
||||
|
||||
#define KEY1_IN (GPIO_ReadInputDataBit(KEY1_GPIO, KEY1_BV)==0)
|
||||
#define KEY2_IN ()
|
||||
#define KEY3_IN ()
|
||||
#define KEY4_IN ()
|
||||
|
||||
#define HAL_PUSH_BUTTON1() (KEY1_IN) //Add custom button
|
||||
#define HAL_PUSH_BUTTON2() (0)
|
||||
#define HAL_PUSH_BUTTON3() (0)
|
||||
#define HAL_PUSH_BUTTON4() (0)
|
||||
|
||||
/**************************************************************************************************
|
||||
* TYPEDEFS
|
||||
**************************************************************************************************/
|
||||
typedef void (*halKeyCBack_t)(uint8_t keys);
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint8_t keys; // keys
|
||||
} keyChange_t;
|
||||
|
||||
/**************************************************************************************************
|
||||
* GLOBAL VARIABLES
|
||||
**************************************************************************************************/
|
||||
|
||||
/*********************************************************************
|
||||
* FUNCTIONS
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief Initialize the Key Service
|
||||
*/
|
||||
void HAL_KeyInit(void);
|
||||
|
||||
/**
|
||||
* @brief This is for internal used by hal_driver
|
||||
*/
|
||||
void HAL_KeyPoll(void);
|
||||
|
||||
/**
|
||||
* @brief Configure the Key serivce
|
||||
*
|
||||
* @param cback - pointer to the CallBack function
|
||||
*/
|
||||
void HalKeyConfig(const halKeyCBack_t cback);
|
||||
|
||||
/**
|
||||
* @brief Read the Key callback
|
||||
*/
|
||||
void HalKeyCallback(uint8_t keys);
|
||||
|
||||
/**
|
||||
* @brief Read the Key status
|
||||
*/
|
||||
uint8_t HalKeyRead(void);
|
||||
|
||||
/**************************************************************************************************
|
||||
**************************************************************************************************/
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
133
vd960DBN/BLE/HAL/include/LED.h
Normal file
133
vd960DBN/BLE/HAL/include/LED.h
Normal file
@@ -0,0 +1,133 @@
|
||||
/********************************** (C) COPYRIGHT *******************************
|
||||
* File Name : LED.h
|
||||
* Author : WCH
|
||||
* Version : V1.0
|
||||
* Date : 2016/04/12
|
||||
* Description :
|
||||
*********************************************************************************
|
||||
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
|
||||
* Attention: This software (modified or not) and binary are used for
|
||||
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
|
||||
*******************************************************************************/
|
||||
|
||||
/******************************************************************************/
|
||||
#ifndef __LED_H
|
||||
#define __LED_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*********************************************************************
|
||||
* CONSTANTS
|
||||
*/
|
||||
|
||||
/* LEDS - The LED number is the same as the bit position */
|
||||
#define HAL_LED_1 0x01
|
||||
#define HAL_LED_2 0x02
|
||||
#define HAL_LED_3 0x04
|
||||
#define HAL_LED_4 0x08
|
||||
#define HAL_LED_ALL (HAL_LED_1 | HAL_LED_2 | HAL_LED_3 | HAL_LED_4)
|
||||
|
||||
/* Modes */
|
||||
#define HAL_LED_MODE_OFF 0x00
|
||||
#define HAL_LED_MODE_ON 0x01
|
||||
#define HAL_LED_MODE_BLINK 0x02
|
||||
#define HAL_LED_MODE_FLASH 0x04
|
||||
#define HAL_LED_MODE_TOGGLE 0x08
|
||||
|
||||
/* Defaults */
|
||||
#define HAL_LED_DEFAULT_MAX_LEDS 4
|
||||
#define HAL_LED_DEFAULT_DUTY_CYCLE 5
|
||||
#define HAL_LED_DEFAULT_FLASH_COUNT 50
|
||||
#define HAL_LED_DEFAULT_FLASH_TIME 1000
|
||||
|
||||
/*********************************************************************
|
||||
* TYPEDEFS
|
||||
*/
|
||||
|
||||
/* Connect an LED to monitor the progress of the demo program, the low-level LED is on */
|
||||
|
||||
/* 1 - LED */
|
||||
#define LED1_PCENR (RCC_APB2Periph_GPIOB)
|
||||
#define LED2_PCENR
|
||||
#define LED3_PCENR
|
||||
|
||||
#define LED1_GPIO (GPIOB)
|
||||
#define LED2_GPIO
|
||||
#define LED3_GPIO
|
||||
|
||||
#define LED1_BV BV(15)
|
||||
#define LED2_BV
|
||||
#define LED3_BV
|
||||
|
||||
#define HAL_TURN_OFF_LED1() (GPIO_WriteBit(LED1_GPIO, LED1_BV, Bit_SET))
|
||||
#define HAL_TURN_OFF_LED2()
|
||||
#define HAL_TURN_OFF_LED3()
|
||||
#define HAL_TURN_OFF_LED4()
|
||||
|
||||
#define HAL_TURN_ON_LED1() (GPIO_WriteBit(LED1_GPIO, LED1_BV, Bit_RESET))
|
||||
#define HAL_TURN_ON_LED2()
|
||||
#define HAL_TURN_ON_LED3()
|
||||
#define HAL_TURN_ON_LED4()
|
||||
|
||||
#define HAL_STATE_LED1() 0
|
||||
#define HAL_STATE_LED2() 0
|
||||
#define HAL_STATE_LED3() 0
|
||||
#define HAL_STATE_LED4() 0
|
||||
|
||||
/*********************************************************************
|
||||
* GLOBAL VARIABLES
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief Initialize LED Service.
|
||||
*/
|
||||
void HAL_LedInit(void);
|
||||
|
||||
/**
|
||||
* @brief update time LED Service.
|
||||
*/
|
||||
void HalLedUpdate(void);
|
||||
|
||||
/**
|
||||
* @brief Turn ON/OFF/TOGGLE given LEDs
|
||||
*
|
||||
* @param led - bit mask value of leds to be turned ON/OFF/TOGGLE
|
||||
* @param mode - BLINK, FLASH, TOGGLE, ON, OFF
|
||||
*/
|
||||
extern uint8_t HalLedSet(uint8_t led, uint8_t mode);
|
||||
|
||||
/**
|
||||
* @brief Blink the leds
|
||||
*
|
||||
* @param led - bit mask value of leds to be turned ON/OFF/TOGGLE
|
||||
* @param numBlinks - number of blinks
|
||||
* @param percent - the percentage in each period where the led will be on
|
||||
* @param period - length of each cycle in milliseconds
|
||||
*/
|
||||
extern void HalLedBlink(uint8_t leds, uint8_t cnt, uint8_t duty, uint16_t time);
|
||||
|
||||
/**
|
||||
* @brief Put LEDs in sleep state - store current values
|
||||
*/
|
||||
extern void HalLedEnterSleep(void);
|
||||
|
||||
/**
|
||||
* @brief Retore LEDs from sleep state
|
||||
*/
|
||||
extern void HalLedExitSleep(void);
|
||||
|
||||
/**
|
||||
* @brief Return LED state
|
||||
*/
|
||||
extern uint8_t HalLedGetState(void);
|
||||
|
||||
/*********************************************************************
|
||||
*********************************************************************/
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
40
vd960DBN/BLE/HAL/include/RTC.h
Normal file
40
vd960DBN/BLE/HAL/include/RTC.h
Normal file
@@ -0,0 +1,40 @@
|
||||
/********************************** (C) COPYRIGHT *******************************
|
||||
* File Name : RTC.h
|
||||
* Author : WCH
|
||||
* Version : V1.0
|
||||
* Date : 2016/04/12
|
||||
* Description :
|
||||
*********************************************************************************
|
||||
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
|
||||
* Attention: This software (modified or not) and binary are used for
|
||||
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
|
||||
*******************************************************************************/
|
||||
|
||||
/******************************************************************************/
|
||||
#ifndef __RTC_H
|
||||
#define __RTC_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
extern volatile uint32_t RTCTigFlag;
|
||||
|
||||
/**
|
||||
* @brief Initialize time Service.
|
||||
*/
|
||||
void HAL_TimeInit(void);
|
||||
|
||||
/**
|
||||
* @brief Configure RTC trigger time
|
||||
*
|
||||
* @param time - Trigger time.
|
||||
*/
|
||||
extern void RTC_SetTignTime(uint32_t time);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
50
vd960DBN/BLE/HAL/include/SLEEP.h
Normal file
50
vd960DBN/BLE/HAL/include/SLEEP.h
Normal file
@@ -0,0 +1,50 @@
|
||||
/********************************** (C) COPYRIGHT *******************************
|
||||
* File Name : SLEEP.h
|
||||
* Author : WCH
|
||||
* Version : V1.0
|
||||
* Date : 2018/11/12
|
||||
* Description :
|
||||
*********************************************************************************
|
||||
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
|
||||
* Attention: This software (modified or not) and binary are used for
|
||||
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
|
||||
*******************************************************************************/
|
||||
|
||||
/******************************************************************************/
|
||||
#ifndef __SLEEP_H
|
||||
#define __SLEEP_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*********************************************************************
|
||||
* GLOBAL VARIABLES
|
||||
*/
|
||||
|
||||
/*********************************************************************
|
||||
* FUNCTIONS
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief Configure sleep Wake-up source - RTC wake up, trigger mode
|
||||
*/
|
||||
extern void HAL_SleepInit(void);
|
||||
|
||||
/**
|
||||
* @brief Start sleep
|
||||
*
|
||||
* @param time - Wake-up time (RTC absolute value)
|
||||
*
|
||||
* @return state.
|
||||
*/
|
||||
extern uint32_t BLE_LowPower(uint32_t time);
|
||||
|
||||
/*********************************************************************
|
||||
*********************************************************************/
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
137
vd960DBN/BLE/HAL/include/config.h
Normal file
137
vd960DBN/BLE/HAL/include/config.h
Normal file
@@ -0,0 +1,137 @@
|
||||
/********************************** (C) COPYRIGHT *******************************
|
||||
* File Name : CONFIG.h
|
||||
* Author : WCH
|
||||
* Version : V1.2
|
||||
* Date : 2022/01/18
|
||||
* Description : Configuration description and default value,
|
||||
* it is recommended to modify the current value in the
|
||||
* pre-processing of the engineering configuration
|
||||
*********************************************************************************
|
||||
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
|
||||
* Attention: This software (modified or not) and binary are used for
|
||||
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
|
||||
*******************************************************************************/
|
||||
|
||||
/******************************************************************************/
|
||||
#ifndef __CONFIG_H
|
||||
#define __CONFIG_H
|
||||
|
||||
#define ID_CH32V208 0x0208
|
||||
|
||||
#define CHIP_ID ID_CH32V208
|
||||
|
||||
#ifdef WCHBLE_ROM
|
||||
#include "WCHBLE_ROM.H"
|
||||
#else
|
||||
#include "wchble.H"
|
||||
#endif
|
||||
|
||||
#include "ch32v20x.h"
|
||||
|
||||
/*********************************************************************
|
||||
【MAC】
|
||||
BLE_MAC - 是否自定义蓝牙Mac地址 ( 默认:FALSE - 使用芯片Mac地址 ),需要在main.c修改Mac地址定义
|
||||
|
||||
【SLEEP】
|
||||
HAL_SLEEP - 是否开启睡眠功能 ( 默认:FALSE )
|
||||
WAKE_UP_MAX_TIME_US - 提前唤醒时间,即系统时钟稳定所需要时间
|
||||
暂停模式 - 45
|
||||
空闲模式 - 5
|
||||
|
||||
【TEMPERATION】
|
||||
TEM_SAMPLE - 是否打开根据温度变化校准的功能,单次校准耗时小于10ms( 默认:TRUE )
|
||||
|
||||
【CALIBRATION】
|
||||
BLE_CALIBRATION_ENABLE - 是否打开定时校准的功能,单次校准耗时小于10ms( 默认:TRUE )
|
||||
BLE_CALIBRATION_PERIOD - 定时校准的周期,单位ms( 默认:120000 )
|
||||
|
||||
【SNV】
|
||||
BLE_SNV - 是否开启SNV功能,用于储存绑定信息( 默认:TRUE )
|
||||
BLE_SNV_ADDR - SNV信息保存地址,使用data flash最后( 默认:0x77E00 )
|
||||
BLE_SNV_NUM - SNV信息存储扇区数量等于可存储的绑定数量( 默认:3 )
|
||||
- 如果配置了SNVNum参数,则需要对应修改Lib_Write_Flash函数内擦除的flash大小,大小为SNVBlock*SNVNum
|
||||
|
||||
【RTC】
|
||||
CLK_OSC32K - RTC时钟选择,如包含主机角色必须使用外部32K( 0 外部(32768Hz),默认:1:内部(32000Hz),2:内部(32768Hz) )
|
||||
|
||||
【MEMORY】
|
||||
BLE_MEMHEAP_SIZE - 蓝牙协议栈使用的RAM大小,不小于6K ( 默认:(1024*6) )
|
||||
|
||||
【DATA】
|
||||
BLE_BUFF_MAX_LEN - 单个连接最大包长度( 默认:27 (ATT_MTU=23),取值范围[27~251] )
|
||||
BLE_BUFF_NUM - 控制器缓存的包数量( 默认:5 )
|
||||
BLE_TX_NUM_EVENT - 单个连接事件最多可以发多少个数据包( 默认:1 )
|
||||
BLE_TX_POWER - 发射功率( 默认:LL_TX_POWEER_0_DBM (0dBm) )
|
||||
|
||||
【MULTICONN】
|
||||
PERIPHERAL_MAX_CONNECTION - 最多可同时做多少从机角色( 默认:1 )
|
||||
CENTRAL_MAX_CONNECTION - 最多可同时做多少主机角色( 默认:3 )
|
||||
|
||||
**********************************************************************/
|
||||
|
||||
/*********************************************************************
|
||||
* 默认配置值
|
||||
*/
|
||||
#ifndef BLE_MAC
|
||||
#define BLE_MAC FALSE
|
||||
#endif
|
||||
#ifndef HAL_SLEEP
|
||||
#define HAL_SLEEP FALSE
|
||||
#endif
|
||||
#ifndef WAKE_UP_MAX_TIME_US
|
||||
#define WAKE_UP_MAX_TIME_US 2400
|
||||
#endif
|
||||
#ifndef HAL_KEY
|
||||
#define HAL_KEY FALSE
|
||||
#endif
|
||||
#ifndef HAL_LED
|
||||
#define HAL_LED FALSE
|
||||
#endif
|
||||
#ifndef TEM_SAMPLE
|
||||
#define TEM_SAMPLE TRUE
|
||||
#endif
|
||||
#ifndef BLE_CALIBRATION_ENABLE
|
||||
#define BLE_CALIBRATION_ENABLE TRUE
|
||||
#endif
|
||||
#ifndef BLE_CALIBRATION_PERIOD
|
||||
#define BLE_CALIBRATION_PERIOD 120000
|
||||
#endif
|
||||
#ifndef BLE_SNV
|
||||
#define BLE_SNV TRUE
|
||||
#endif
|
||||
#ifndef BLE_SNV_ADDR
|
||||
#define BLE_SNV_ADDR 0x08077C00
|
||||
#endif
|
||||
#ifndef BLE_SNV_NUM
|
||||
#define BLE_SNV_NUM 3
|
||||
#endif
|
||||
#ifndef CLK_OSC32K
|
||||
#define CLK_OSC32K 1 // 该项请勿在此修改,必须在工程配置里的预处理中修改,如包含主机角色必须使用外部32K
|
||||
#endif
|
||||
#ifndef BLE_MEMHEAP_SIZE
|
||||
#define BLE_MEMHEAP_SIZE (1024*7)
|
||||
#endif
|
||||
#ifndef BLE_BUFF_MAX_LEN
|
||||
#define BLE_BUFF_MAX_LEN 100 //27
|
||||
#endif
|
||||
#ifndef BLE_BUFF_NUM
|
||||
#define BLE_BUFF_NUM 5
|
||||
#endif
|
||||
#ifndef BLE_TX_NUM_EVENT
|
||||
#define BLE_TX_NUM_EVENT 1
|
||||
#endif
|
||||
#ifndef BLE_TX_POWER
|
||||
#define BLE_TX_POWER LL_TX_POWEER_7_DBM //LL_TX_POWEER_7_DBM //LL_TX_POWEER_0_DBM
|
||||
#endif
|
||||
#ifndef PERIPHERAL_MAX_CONNECTION
|
||||
#define PERIPHERAL_MAX_CONNECTION 1
|
||||
#endif
|
||||
#ifndef CENTRAL_MAX_CONNECTION
|
||||
#define CENTRAL_MAX_CONNECTION 3
|
||||
#endif
|
||||
|
||||
extern uint32_t MEM_BUF[BLE_MEMHEAP_SIZE / 4];
|
||||
extern const uint8_t MacAddr[6];
|
||||
|
||||
#endif
|
||||
|
||||
BIN
vd960DBN/BLE/LIB/WCHBLE_ROM.BIN
Normal file
BIN
vd960DBN/BLE/LIB/WCHBLE_ROM.BIN
Normal file
Binary file not shown.
127
vd960DBN/BLE/LIB/ble_task_scheduler.S
Normal file
127
vd960DBN/BLE/LIB/ble_task_scheduler.S
Normal file
@@ -0,0 +1,127 @@
|
||||
.global Ecall_M_Mode_Handler
|
||||
.global Ecall_U_Mode_Handler
|
||||
.global LLE_IRQHandler
|
||||
|
||||
.extern g_LLE_IRQLibHandlerLocation
|
||||
|
||||
.section .highcode,"ax",@progbits
|
||||
.align 2
|
||||
.func
|
||||
Ecall_M_Mode_Handler:
|
||||
Ecall_U_Mode_Handler:
|
||||
|
||||
addi a1, x0, 0x20
|
||||
csrs 0x804, a1
|
||||
|
||||
lw a1, 0 * 4( sp )
|
||||
csrw mepc, a1
|
||||
|
||||
lw x1, 1 * 4( sp )
|
||||
lw x4, 2 * 4( sp )
|
||||
lw x5, 3 * 4( sp )
|
||||
lw x6, 4 * 4( sp )
|
||||
lw x7, 5 * 4( sp )
|
||||
lw x8, 6 * 4( sp )
|
||||
lw x9, 7 * 4( sp )
|
||||
lw x10, 8 * 4( sp )
|
||||
lw x11, 9 * 4( sp )
|
||||
lw x12, 10 * 4( sp )
|
||||
lw x13, 11 * 4( sp )
|
||||
lw x14, 12 * 4( sp )
|
||||
lw x15, 13 * 4( sp )
|
||||
lw x16, 14 * 4( sp )
|
||||
lw x17, 15 * 4( sp )
|
||||
lw x18, 16 * 4( sp )
|
||||
lw x19, 17 * 4( sp )
|
||||
lw x20, 18 * 4( sp )
|
||||
lw x21, 19 * 4( sp )
|
||||
lw x22, 20 * 4( sp )
|
||||
lw x23, 21 * 4( sp )
|
||||
lw x24, 22 * 4( sp )
|
||||
lw x25, 23 * 4( sp )
|
||||
lw x26, 24 * 4( sp )
|
||||
lw x27, 25 * 4( sp )
|
||||
lw x28, 26 * 4( sp )
|
||||
lw x29, 27 * 4( sp )
|
||||
lw x30, 28 * 4( sp )
|
||||
lw x31, 29 * 4( sp )
|
||||
|
||||
addi sp, sp, 32*4
|
||||
|
||||
mret
|
||||
.endfunc
|
||||
|
||||
.section .highcode.LLE_IRQHandler,"ax",@progbits
|
||||
.align 2
|
||||
.func
|
||||
LLE_IRQHandler:
|
||||
addi sp, sp, -32*4
|
||||
|
||||
sw x1, 1 * 4( sp )
|
||||
sw x4, 2 * 4( sp )
|
||||
sw x5, 3 * 4( sp )
|
||||
sw x6, 4 * 4( sp )
|
||||
sw x7, 5 * 4( sp )
|
||||
sw x8, 6 * 4( sp )
|
||||
sw x9, 7 * 4( sp )
|
||||
sw x10, 8 * 4( sp )
|
||||
sw x11, 9 * 4( sp )
|
||||
sw x12, 10 * 4( sp )
|
||||
sw x13, 11 * 4( sp )
|
||||
sw x14, 12 * 4( sp )
|
||||
sw x15, 13 * 4( sp )
|
||||
sw x16, 14 * 4( sp )
|
||||
sw x17, 15 * 4( sp )
|
||||
sw x18, 16 * 4( sp )
|
||||
sw x19, 17 * 4( sp )
|
||||
sw x20, 18 * 4( sp )
|
||||
sw x21, 19 * 4( sp )
|
||||
sw x22, 20 * 4( sp )
|
||||
sw x23, 21 * 4( sp )
|
||||
sw x24, 22 * 4( sp )
|
||||
sw x25, 23 * 4( sp )
|
||||
sw x26, 24 * 4( sp )
|
||||
sw x27, 25 * 4( sp )
|
||||
sw x28, 26 * 4( sp )
|
||||
sw x29, 27 * 4( sp )
|
||||
sw x30, 28 * 4( sp )
|
||||
sw x31, 29 * 4( sp )
|
||||
|
||||
la a1, g_LLE_IRQLibHandlerLocation
|
||||
lw a0, 0(a1)
|
||||
jalr x1, 0(a0)
|
||||
|
||||
lw x1, 1 * 4( sp )
|
||||
lw x4, 2 * 4( sp )
|
||||
lw x5, 3 * 4( sp )
|
||||
lw x6, 4 * 4( sp )
|
||||
lw x7, 5 * 4( sp )
|
||||
lw x8, 6 * 4( sp )
|
||||
lw x9, 7 * 4( sp )
|
||||
lw x10, 8 * 4( sp )
|
||||
lw x11, 9 * 4( sp )
|
||||
lw x12, 10 * 4( sp )
|
||||
lw x13, 11 * 4( sp )
|
||||
lw x14, 12 * 4( sp )
|
||||
lw x15, 13 * 4( sp )
|
||||
lw x16, 14 * 4( sp )
|
||||
lw x17, 15 * 4( sp )
|
||||
lw x18, 16 * 4( sp )
|
||||
lw x19, 17 * 4( sp )
|
||||
lw x20, 18 * 4( sp )
|
||||
lw x21, 19 * 4( sp )
|
||||
lw x22, 20 * 4( sp )
|
||||
lw x23, 21 * 4( sp )
|
||||
lw x24, 22 * 4( sp )
|
||||
lw x25, 23 * 4( sp )
|
||||
lw x26, 24 * 4( sp )
|
||||
lw x27, 25 * 4( sp )
|
||||
lw x28, 26 * 4( sp )
|
||||
lw x29, 27 * 4( sp )
|
||||
lw x30, 28 * 4( sp )
|
||||
lw x31, 29 * 4( sp )
|
||||
|
||||
addi sp, sp, 32*4
|
||||
|
||||
mret
|
||||
.endfunc
|
||||
4910
vd960DBN/BLE/LIB/wchble.h
Normal file
4910
vd960DBN/BLE/LIB/wchble.h
Normal file
File diff suppressed because it is too large
Load Diff
4933
vd960DBN/BLE/LIB/wchble_rom.h
Normal file
4933
vd960DBN/BLE/LIB/wchble_rom.h
Normal file
File diff suppressed because it is too large
Load Diff
62
vd960DBN/BLE/OnlyUpdateApp_IAP/APP/ch32v20x_it.c
Normal file
62
vd960DBN/BLE/OnlyUpdateApp_IAP/APP/ch32v20x_it.c
Normal file
@@ -0,0 +1,62 @@
|
||||
/********************************** (C) COPYRIGHT *******************************
|
||||
* File Name : ch32v20x_it.c
|
||||
* Author : WCH
|
||||
* Version : V1.0.0
|
||||
* Date : 2022/06/16
|
||||
* Description : Main Interrupt Service Routines.
|
||||
*********************************************************************************
|
||||
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
|
||||
* Attention: This software (modified or not) and binary are used for
|
||||
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
|
||||
*******************************************************************************/
|
||||
|
||||
/*********************************************************************
|
||||
* INCLUDES
|
||||
*/
|
||||
#include "ch32v20x_it.h"
|
||||
#include "CONFIG.h"
|
||||
|
||||
/*********************************************************************
|
||||
* LOCAL FUNCTIONS
|
||||
*/
|
||||
void NMI_Handler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
|
||||
void HardFault_Handler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
|
||||
void BB_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
|
||||
|
||||
/*********************************************************************
|
||||
* @fn NMI_Handler
|
||||
*
|
||||
* @brief This function handles NMI exception.
|
||||
*
|
||||
* @return None
|
||||
*/
|
||||
void NMI_Handler(void)
|
||||
{
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn HardFault_Handler
|
||||
*
|
||||
* @brief This function handles Hard Fault exception.
|
||||
*
|
||||
* @return None
|
||||
*/
|
||||
void HardFault_Handler(void)
|
||||
{
|
||||
NVIC_SystemReset();
|
||||
while(1)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn BB_IRQHandler
|
||||
*
|
||||
* @brief BB Interrupt for BLE.
|
||||
*
|
||||
* @return None
|
||||
*/
|
||||
void BB_IRQHandler(void)
|
||||
{
|
||||
BB_IRQLibHandler();
|
||||
}
|
||||
37
vd960DBN/BLE/OnlyUpdateApp_IAP/APP/include/ch32v20x_conf.h
Normal file
37
vd960DBN/BLE/OnlyUpdateApp_IAP/APP/include/ch32v20x_conf.h
Normal file
@@ -0,0 +1,37 @@
|
||||
/********************************** (C) COPYRIGHT *******************************
|
||||
* File Name : ch32v20x_conf.h
|
||||
* Author : WCH
|
||||
* Version : V1.0.0
|
||||
* Date : 2022/06/16
|
||||
* Description : Library configuration file.
|
||||
*********************************************************************************
|
||||
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
|
||||
* Attention: This software (modified or not) and binary are used for
|
||||
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
|
||||
*******************************************************************************/
|
||||
|
||||
#ifndef __CH32V20x_CONF_H
|
||||
#define __CH32V20x_CONF_H
|
||||
|
||||
#include "ch32v20x_adc.h"
|
||||
#include "ch32v20x_bkp.h"
|
||||
#include "ch32v20x_can.h"
|
||||
#include "ch32v20x_crc.h"
|
||||
#include "ch32v20x_dbgmcu.h"
|
||||
#include "ch32v20x_dma.h"
|
||||
#include "ch32v20x_exti.h"
|
||||
#include "ch32v20x_flash.h"
|
||||
#include "ch32v20x_gpio.h"
|
||||
#include "ch32v20x_i2c.h"
|
||||
#include "ch32v20x_iwdg.h"
|
||||
#include "ch32v20x_pwr.h"
|
||||
#include "ch32v20x_rcc.h"
|
||||
#include "ch32v20x_rtc.h"
|
||||
#include "ch32v20x_spi.h"
|
||||
#include "ch32v20x_tim.h"
|
||||
#include "ch32v20x_usart.h"
|
||||
#include "ch32v20x_wwdg.h"
|
||||
#include "ch32v20x_it.h"
|
||||
#include "ch32v20x_misc.h"
|
||||
|
||||
#endif /* __CH32V20x_CONF_H */
|
||||
18
vd960DBN/BLE/OnlyUpdateApp_IAP/APP/include/ch32v20x_it.h
Normal file
18
vd960DBN/BLE/OnlyUpdateApp_IAP/APP/include/ch32v20x_it.h
Normal file
@@ -0,0 +1,18 @@
|
||||
/********************************** (C) COPYRIGHT *******************************
|
||||
* File Name : ch32v20x_it.h
|
||||
* Author : WCH
|
||||
* Version : V1.0.0
|
||||
* Date : 2022/06/16
|
||||
* Description : This file contains the headers of the interrupt handlers.
|
||||
*********************************************************************************
|
||||
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
|
||||
* Attention: This software (modified or not) and binary are used for
|
||||
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
|
||||
*******************************************************************************/
|
||||
|
||||
#ifndef __CH32V20x_IT_H
|
||||
#define __CH32V20x_IT_H
|
||||
|
||||
#include "debug.h"
|
||||
|
||||
#endif /* __CH32V20x_IT_H */
|
||||
111
vd960DBN/BLE/OnlyUpdateApp_IAP/APP/include/ota.h
Normal file
111
vd960DBN/BLE/OnlyUpdateApp_IAP/APP/include/ota.h
Normal file
@@ -0,0 +1,111 @@
|
||||
/********************************** (C) COPYRIGHT *******************************
|
||||
* File Name : ota.h
|
||||
* Author : WCH
|
||||
* Version : V1.10
|
||||
* Date : 2018/12/14
|
||||
* Description : oad相关配置定义
|
||||
*********************************************************************************
|
||||
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
|
||||
* Attention: This software (modified or not) and binary are used for
|
||||
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
|
||||
*******************************************************************************/
|
||||
|
||||
/******************************************************************************/
|
||||
#ifndef __OTA_H
|
||||
#define __OTA_H
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* OTA FLASH
|
||||
* ------------------------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
/* 整个用户code区分成3块,依次为16K,240K,192K 分别叫做imageIAP(IAP),imageA(APP),LIB */
|
||||
|
||||
/* FLASH定义 */
|
||||
|
||||
#define FLASH_BLOCK_SIZE 4096
|
||||
#define FLASH_PAGE_SIZE 256
|
||||
|
||||
/* imageIAP定义 */
|
||||
#define IMAGE_IAP_FLAG 0x02
|
||||
#define IMAGE_IAP_START_ADD 0x08000000
|
||||
#define IMAGE_IAP_SIZE 16 * 1024
|
||||
|
||||
/* imageA定义 */
|
||||
#define IMAGE_A_FLAG 0x01
|
||||
#define IMAGE_A_START_ADD (IMAGE_IAP_START_ADD+IMAGE_IAP_SIZE)
|
||||
#define IMAGE_A_SIZE 240 * 1024
|
||||
|
||||
#define IMAGE_OTA_FLAG 0x03
|
||||
|
||||
#define jumpApp ((void (*)(void))((int *)(IMAGE_A_START_ADD-0x08000000)))
|
||||
|
||||
/* IAP定义 */
|
||||
/* 以下为IAP下载命令定义 */
|
||||
#define CMD_IAP_PROM 0x80 // IAP编程命令
|
||||
#define CMD_IAP_ERASE 0x81 // IAP擦除命令
|
||||
#define CMD_IAP_VERIFY 0x82 // IAP校验命令
|
||||
#define CMD_IAP_END 0x83 // IAP结束标志
|
||||
#define CMD_IAP_INFO 0x84 // IAP获取设备信息
|
||||
|
||||
/* 数据帧长度定义 */
|
||||
#define IAP_LEN 247
|
||||
|
||||
/* 存放在DataFlash地址,不能占用蓝牙的位置 */
|
||||
#define OTA_DATAFLASH_ADD 0x08077000
|
||||
|
||||
/* 存放在DataFlash里的OTA信息 */
|
||||
typedef struct
|
||||
{
|
||||
unsigned char ImageFlag; //记录的当前的image标志
|
||||
unsigned char flag[3];
|
||||
} OTADataFlashInfo_t;
|
||||
|
||||
/* OTA IAP通讯协议定义 */
|
||||
/* 地址使用4倍偏移 */
|
||||
typedef union
|
||||
{
|
||||
struct
|
||||
{
|
||||
unsigned char cmd; /* 命令码 0x81 */
|
||||
unsigned char len; /* 后续数据长度 */
|
||||
unsigned char addr[2]; /* 擦除地址 */
|
||||
unsigned char block_num[2]; /* 擦除块数 */
|
||||
|
||||
} erase; /* 擦除命令 */
|
||||
struct
|
||||
{
|
||||
unsigned char cmd; /* 命令码 0x83 */
|
||||
unsigned char len; /* 后续数据长度 */
|
||||
unsigned char status[2]; /* 两字节状态,保留 */
|
||||
} end; /* 结束命令 */
|
||||
struct
|
||||
{
|
||||
unsigned char cmd; /* 命令码 0x82 */
|
||||
unsigned char len; /* 后续数据长度 */
|
||||
unsigned char addr[2]; /* 校验地址 */
|
||||
unsigned char buf[IAP_LEN - 4]; /* 校验数据 */
|
||||
} verify; /* 校验命令 */
|
||||
struct
|
||||
{
|
||||
unsigned char cmd; /* 命令码 0x80 */
|
||||
unsigned char len; /* 后续数据长度 */
|
||||
unsigned char addr[2]; /* 地址 */
|
||||
unsigned char buf[IAP_LEN - 4]; /* 后续数据 */
|
||||
} program; /* 编程命令 */
|
||||
struct
|
||||
{
|
||||
unsigned char cmd; /* 命令码 0x84 */
|
||||
unsigned char len; /* 后续数据长度 */
|
||||
unsigned char buf[IAP_LEN - 2]; /* 后续数据 */
|
||||
} info; /* 编程命令 */
|
||||
struct
|
||||
{
|
||||
unsigned char buf[IAP_LEN]; /* 接收数据包*/
|
||||
} other;
|
||||
} OTA_IAP_CMD_t;
|
||||
|
||||
/* 记录当前的Image */
|
||||
extern unsigned char CurrImageFlag;
|
||||
|
||||
#endif
|
||||
63
vd960DBN/BLE/OnlyUpdateApp_IAP/APP/include/peripheral.h
Normal file
63
vd960DBN/BLE/OnlyUpdateApp_IAP/APP/include/peripheral.h
Normal file
@@ -0,0 +1,63 @@
|
||||
/********************************** (C) COPYRIGHT *******************************
|
||||
* File Name : peripheral.h
|
||||
* Author : WCH
|
||||
* Version : V1.0
|
||||
* Date : 2018/12/11
|
||||
* Description :
|
||||
*********************************************************************************
|
||||
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
|
||||
* Attention: This software (modified or not) and binary are used for
|
||||
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
|
||||
*******************************************************************************/
|
||||
|
||||
#ifndef PERIPHERAL_H
|
||||
#define PERIPHERAL_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*********************************************************************
|
||||
* INCLUDES
|
||||
*/
|
||||
|
||||
/*********************************************************************
|
||||
* CONSTANTS
|
||||
*/
|
||||
|
||||
// Simple BLE Peripheral Task Events
|
||||
#define SBP_START_DEVICE_EVT 0x0001
|
||||
#define SBP_PERIODIC_EVT 0x0002
|
||||
#define OTA_FLASH_ERASE_EVT 0x0004 //OTA Flash²Á³ýÈÎÎñ
|
||||
|
||||
/*********************************************************************
|
||||
* MACROS
|
||||
*/
|
||||
|
||||
/*********************************************************************
|
||||
* FUNCTIONS
|
||||
*/
|
||||
|
||||
/*
|
||||
* Task Initialization for the BLE Application
|
||||
*/
|
||||
extern void Peripheral_Init(void);
|
||||
|
||||
/*
|
||||
* Task Event Processor for the BLE Application
|
||||
*/
|
||||
extern uint16_t Peripheral_ProcessEvent(uint8_t task_id, uint16_t events);
|
||||
|
||||
/*
|
||||
* Read flash
|
||||
*/
|
||||
void FLASH_read(uint32_t addr, uint8_t *pData, uint32_t len);
|
||||
|
||||
/*********************************************************************
|
||||
*********************************************************************/
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
31
vd960DBN/BLE/OnlyUpdateApp_IAP/APP/include/system_ch32v20x.h
Normal file
31
vd960DBN/BLE/OnlyUpdateApp_IAP/APP/include/system_ch32v20x.h
Normal file
@@ -0,0 +1,31 @@
|
||||
/********************************** (C) COPYRIGHT *******************************
|
||||
* File Name : system_ch32v20x.h
|
||||
* Author : WCH
|
||||
* Version : V1.0.0
|
||||
* Date : 2022/06/16
|
||||
* Description : CH32V20x Device Peripheral Access Layer System Header File.
|
||||
*********************************************************************************
|
||||
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
|
||||
* Attention: This software (modified or not) and binary are used for
|
||||
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
|
||||
*******************************************************************************/
|
||||
|
||||
#ifndef __SYSTEM_ch32v20x_H
|
||||
#define __SYSTEM_ch32v20x_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern uint32_t SystemCoreClock; /* System Clock Frequency (Core Clock) */
|
||||
|
||||
/* System_Exported_Functions */
|
||||
extern void SystemInit(void);
|
||||
extern void SystemCoreClockUpdate(void);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /*__CH32V20x_SYSTEM_H */
|
||||
752
vd960DBN/BLE/OnlyUpdateApp_IAP/APP/peripheral.c
Normal file
752
vd960DBN/BLE/OnlyUpdateApp_IAP/APP/peripheral.c
Normal file
@@ -0,0 +1,752 @@
|
||||
/********************************** (C) COPYRIGHT *******************************
|
||||
* File Name : Peripheral.C
|
||||
* Author : WCH
|
||||
* Version : V1.0
|
||||
* Date : 2018/12/10
|
||||
* Description : Peripheral slave multi-connection application, initialize
|
||||
* broadcast connection parameters, then broadcast, after connecting
|
||||
* to the host, request to update connection parameters,
|
||||
* and transmit data through custom services.
|
||||
*********************************************************************************
|
||||
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
|
||||
* Attention: This software (modified or not) and binary are used for
|
||||
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
|
||||
*******************************************************************************/
|
||||
|
||||
/*********************************************************************
|
||||
* INCLUDES
|
||||
*/
|
||||
#include "CONFIG.h"
|
||||
#include "GATTprofile.h"
|
||||
#include "Peripheral.h"
|
||||
#include "OTA.h"
|
||||
#include "OTAprofile.h"
|
||||
|
||||
/*********************************************************************
|
||||
* MACROS
|
||||
*/
|
||||
|
||||
/*********************************************************************
|
||||
* CONSTANTS
|
||||
*/
|
||||
|
||||
// How often to perform periodic event
|
||||
#define SBP_PERIODIC_EVT_PERIOD 1000
|
||||
|
||||
// What is the advertising interval when device is discoverable (units of 625us, 32=20ms)
|
||||
#define DEFAULT_ADVERTISING_INTERVAL 32
|
||||
|
||||
// Limited discoverable mode advertises for 30.72s, and then stops
|
||||
// General discoverable mode advertises indefinitely
|
||||
|
||||
#define DEFAULT_DISCOVERABLE_MODE GAP_ADTYPE_FLAGS_GENERAL
|
||||
|
||||
// Minimum connection interval (units of 1.25ms, 6=7.5ms) if automatic parameter update request is enabled
|
||||
#define DEFAULT_DESIRED_MIN_CONN_INTERVAL 6
|
||||
|
||||
// Maximum connection interval (units of 1.25ms, 12=15ms) if automatic parameter update request is enabled
|
||||
#define DEFAULT_DESIRED_MAX_CONN_INTERVAL 12
|
||||
|
||||
// Slave latency to use if automatic parameter update request is enabled
|
||||
#define DEFAULT_DESIRED_SLAVE_LATENCY 0
|
||||
|
||||
// Supervision timeout value (units of 10ms, 1000=10s) if automatic parameter update request is enabled
|
||||
#define DEFAULT_DESIRED_CONN_TIMEOUT 1000
|
||||
|
||||
// Whether to enable automatic parameter update request when a connection is formed
|
||||
#define DEFAULT_ENABLE_UPDATE_REQUEST TRUE
|
||||
|
||||
// Connection Pause Peripheral time value (in seconds)
|
||||
#define DEFAULT_CONN_PAUSE_PERIPHERAL 6
|
||||
|
||||
// Company Identifier: WCH
|
||||
#define WCH_COMPANY_ID 0x07D7
|
||||
|
||||
#define INVALID_CONNHANDLE 0xFFFF
|
||||
|
||||
// Length of bd addr as a string
|
||||
#define B_ADDR_STR_LEN 15
|
||||
|
||||
/*********************************************************************
|
||||
* TYPEDEFS
|
||||
*/
|
||||
|
||||
/*********************************************************************
|
||||
* GLOBAL VARIABLES
|
||||
*/
|
||||
|
||||
/*********************************************************************
|
||||
* EXTERNAL VARIABLES
|
||||
*/
|
||||
|
||||
/*********************************************************************
|
||||
* EXTERNAL FUNCTIONS
|
||||
*/
|
||||
|
||||
/*********************************************************************
|
||||
* LOCAL VARIABLES
|
||||
*/
|
||||
static uint8_t Peripheral_TaskID = INVALID_TASK_ID; // Task ID for internal task/event processing
|
||||
|
||||
// GAP - SCAN RSP data (max size = 31 bytes)
|
||||
static uint8_t scanRspData[31] = {
|
||||
// complete name
|
||||
0x12, // length of this data
|
||||
GAP_ADTYPE_LOCAL_NAME_COMPLETE,
|
||||
'O', 'T', 'A', 'O', 'T', 'A', '_', 'O', 'T', 'A', 'O', 'T', 'A', '_', 'O', 'T', 'A',
|
||||
// connection interval range
|
||||
0x05, // length of this data
|
||||
GAP_ADTYPE_SLAVE_CONN_INTERVAL_RANGE,
|
||||
LO_UINT16(DEFAULT_DESIRED_MIN_CONN_INTERVAL), // 100ms
|
||||
HI_UINT16(DEFAULT_DESIRED_MIN_CONN_INTERVAL),
|
||||
LO_UINT16(DEFAULT_DESIRED_MAX_CONN_INTERVAL), // 1s
|
||||
HI_UINT16(DEFAULT_DESIRED_MAX_CONN_INTERVAL),
|
||||
|
||||
// Tx power level
|
||||
0x02, // length of this data
|
||||
GAP_ADTYPE_POWER_LEVEL,
|
||||
0 // 0dBm
|
||||
};
|
||||
|
||||
// GAP - Advertisement data (max size = 31 bytes, though this is
|
||||
// best kept short to conserve power while advertisting)
|
||||
static uint8_t advertData[] = {
|
||||
// Flags; this sets the device to use limited discoverable
|
||||
// mode (advertises for 30 seconds at a time) instead of general
|
||||
// discoverable mode (advertises indefinitely)
|
||||
0x02, // length of this data
|
||||
GAP_ADTYPE_FLAGS,
|
||||
DEFAULT_DISCOVERABLE_MODE | GAP_ADTYPE_FLAGS_BREDR_NOT_SUPPORTED,
|
||||
|
||||
// service UUID, to notify central devices what services are included
|
||||
// in this peripheral
|
||||
0x03, // length of this data
|
||||
GAP_ADTYPE_16BIT_MORE, // some of the UUID's, but not all
|
||||
LO_UINT16(SIMPLEPROFILE_SERV_UUID),
|
||||
HI_UINT16(SIMPLEPROFILE_SERV_UUID)
|
||||
|
||||
};
|
||||
|
||||
// GAP GATT Attributes
|
||||
static uint8_t attDeviceName[GAP_DEVICE_NAME_LEN] = "OTAOTA_OTAOTA_OTA";
|
||||
|
||||
// OTA IAP VARIABLES
|
||||
/* OTA communication frame */
|
||||
OTA_IAP_CMD_t iap_rec_data;
|
||||
|
||||
/* OTA analysis results */
|
||||
uint32_t OpParaDataLen = 0;
|
||||
uint32_t OpAdd = 0;
|
||||
uint16_t block_buf_len=0;
|
||||
uint32_t prom_addr=0;
|
||||
|
||||
/* Flash data temporary storage */
|
||||
__attribute__((aligned(8))) uint8_t block_buf[512];
|
||||
|
||||
/* IMAGE jump function address definition */
|
||||
typedef int (*pImageTaskFn)(void);
|
||||
pImageTaskFn user_image_tasks;
|
||||
|
||||
/* Flash erase */
|
||||
uint32_t EraseAdd = 0; //Removal address
|
||||
uint32_t EraseBlockNum = 0; //Number of blocks that need to be erased
|
||||
uint32_t EraseBlockCnt = 0; //Scratching block count
|
||||
|
||||
/* FLASH verification status */
|
||||
uint8_t VerifyStatus = 0;
|
||||
|
||||
/*********************************************************************
|
||||
* LOCAL FUNCTIONS
|
||||
*/
|
||||
static void Peripheral_ProcessTMOSMsg(tmos_event_hdr_t *pMsg);
|
||||
static void peripheralStateNotificationCB(gapRole_States_t newState, gapRoleEvent_t *pEvent);
|
||||
static void performPeriodicTask(void);
|
||||
|
||||
void OTA_IAPReadDataComplete(unsigned char index);
|
||||
void OTA_IAPWriteData(unsigned char index, unsigned char *p_data, unsigned char w_len);
|
||||
void Rec_OTA_IAP_DataDeal(void);
|
||||
void OTA_IAP_SendCMDDealSta(uint8_t deal_status);
|
||||
|
||||
/*********************************************************************
|
||||
* PROFILE CALLBACKS
|
||||
*/
|
||||
|
||||
// GAP Role Callbacks
|
||||
static gapRolesCBs_t Peripheral_PeripheralCBs = {
|
||||
peripheralStateNotificationCB, // Profile State Change Callbacks
|
||||
NULL, // When a valid RSSI is read from controller (not used by application)
|
||||
NULL};
|
||||
|
||||
// GAP Bond Manager Callbacks
|
||||
static gapBondCBs_t Peripheral_BondMgrCBs = {
|
||||
NULL, // Passcode callback (not used by application)
|
||||
NULL // Pairing / Bonding state Callback (not used by application)
|
||||
};
|
||||
|
||||
// Simple GATT Profile Callbacks
|
||||
static OTAProfileCBs_t Peripheral_OTA_IAPProfileCBs = {
|
||||
OTA_IAPReadDataComplete, // Charactersitic value change callback
|
||||
OTA_IAPWriteData};
|
||||
|
||||
// Callback when the connection parameteres are updated.
|
||||
void PeripheralParamUpdate(uint16_t connInterval, uint16_t connSlaveLatency, uint16_t connTimeout);
|
||||
|
||||
gapRolesParamUpdateCB_t PeripheralParamUpdate_t = NULL;
|
||||
|
||||
/*********************************************************************
|
||||
* PUBLIC FUNCTIONS
|
||||
*/
|
||||
|
||||
/*********************************************************************
|
||||
* @fn Peripheral_Init
|
||||
*
|
||||
* @brief Initialization function for the Peripheral App Task.
|
||||
* This is called during initialization and should contain
|
||||
* any application specific initialization (ie. hardware
|
||||
* initialization/setup, table initialization, power up
|
||||
* notificaiton ... ).
|
||||
*
|
||||
* @param task_id - the ID assigned by TMOS. This ID should be
|
||||
* used to send messages and set timers.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
void Peripheral_Init()
|
||||
{
|
||||
Peripheral_TaskID = TMOS_ProcessEventRegister(Peripheral_ProcessEvent);
|
||||
|
||||
// Setup the GAP Peripheral Role Profile
|
||||
{
|
||||
// For other hardware platforms, device starts advertising upon initialization
|
||||
uint8_t initial_advertising_enable = TRUE;
|
||||
|
||||
// Set the GAP Role Parameters
|
||||
GAPRole_SetParameter(GAPROLE_ADVERT_ENABLED, sizeof(uint8_t), &initial_advertising_enable);
|
||||
GAPRole_SetParameter(GAPROLE_SCAN_RSP_DATA, sizeof(scanRspData), scanRspData);
|
||||
GAPRole_SetParameter(GAPROLE_ADVERT_DATA, sizeof(advertData), advertData);
|
||||
}
|
||||
|
||||
// Set advertising interval
|
||||
{
|
||||
uint16_t advInt = DEFAULT_ADVERTISING_INTERVAL;
|
||||
|
||||
GAP_SetParamValue(TGAP_DISC_ADV_INT_MIN, advInt);
|
||||
GAP_SetParamValue(TGAP_DISC_ADV_INT_MAX, advInt);
|
||||
}
|
||||
|
||||
// Initialize GATT attributes
|
||||
GGS_AddService(GATT_ALL_SERVICES); // GAP
|
||||
GATTServApp_AddService(GATT_ALL_SERVICES); // GATT attributes
|
||||
OTAProfile_AddService(GATT_ALL_SERVICES);
|
||||
|
||||
// Set the GAP Characteristics
|
||||
GGS_SetParameter(GGS_DEVICE_NAME_ATT, sizeof(attDeviceName), attDeviceName);
|
||||
|
||||
// Register callback with OTAGATTprofile
|
||||
OTAProfile_RegisterAppCBs(&Peripheral_OTA_IAPProfileCBs);
|
||||
|
||||
// Setup a delayed profile startup
|
||||
tmos_set_event(Peripheral_TaskID, SBP_START_DEVICE_EVT);
|
||||
}
|
||||
|
||||
void PeripheralParamUpdate(uint16_t connInterval, uint16_t connSlaveLatency, uint16_t connTimeout)
|
||||
{
|
||||
PRINT("update %d %d %d \n", connInterval, connSlaveLatency, connTimeout);
|
||||
|
||||
// GAPRole_SendUpdateParam( DEFAULT_DESIRED_MIN_CONN_INTERVAL, DEFAULT_DESIRED_MAX_CONN_INTERVAL,
|
||||
// DEFAULT_DESIRED_SLAVE_LATENCY, DEFAULT_DESIRED_CONN_TIMEOUT, GAPROLE_NO_ACTION );
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn Peripheral_ProcessEvent
|
||||
*
|
||||
* @brief Peripheral Application Task event processor. This function
|
||||
* is called to process all events for the task. Events
|
||||
* include timers, messages and any other user defined events.
|
||||
*
|
||||
* @param task_id - The TMOS assigned task ID.
|
||||
* @param events - events to process. This is a bit map and can
|
||||
* contain more than one event.
|
||||
*
|
||||
* @return events not processed
|
||||
*/
|
||||
uint16_t Peripheral_ProcessEvent(uint8_t task_id, uint16_t events)
|
||||
{
|
||||
// VOID task_id; // TMOS required parameter that isn't used in this function
|
||||
|
||||
if(events & SYS_EVENT_MSG)
|
||||
{
|
||||
uint8_t *pMsg;
|
||||
|
||||
if((pMsg = tmos_msg_receive(Peripheral_TaskID)) != NULL)
|
||||
{
|
||||
Peripheral_ProcessTMOSMsg((tmos_event_hdr_t *)pMsg);
|
||||
// Release the TMOS message
|
||||
tmos_msg_deallocate(pMsg);
|
||||
}
|
||||
// return unprocessed events
|
||||
return (events ^ SYS_EVENT_MSG);
|
||||
}
|
||||
|
||||
if(events & SBP_START_DEVICE_EVT)
|
||||
{
|
||||
// Start the Device
|
||||
GAPRole_PeripheralStartDevice(Peripheral_TaskID, &Peripheral_BondMgrCBs, &Peripheral_PeripheralCBs);
|
||||
// Set timer for first periodic event
|
||||
tmos_start_task(Peripheral_TaskID, SBP_PERIODIC_EVT, SBP_PERIODIC_EVT_PERIOD);
|
||||
return (events ^ SBP_START_DEVICE_EVT);
|
||||
}
|
||||
|
||||
if(events & SBP_PERIODIC_EVT)
|
||||
{
|
||||
// Restart timer
|
||||
if(SBP_PERIODIC_EVT_PERIOD)
|
||||
{
|
||||
tmos_start_task(Peripheral_TaskID, SBP_PERIODIC_EVT, SBP_PERIODIC_EVT_PERIOD);
|
||||
}
|
||||
// Perform periodic application task
|
||||
performPeriodicTask();
|
||||
return (events ^ SBP_PERIODIC_EVT);
|
||||
}
|
||||
|
||||
//OTA_FLASH_ERASE_EVT
|
||||
if(events & OTA_FLASH_ERASE_EVT)
|
||||
{
|
||||
uint8_t status;
|
||||
|
||||
PRINT("ERASE:%08x num:%d\r\n", (int)(EraseAdd + EraseBlockCnt * FLASH_BLOCK_SIZE), (int)EraseBlockCnt);
|
||||
FLASH_Unlock();
|
||||
status = FLASH_ErasePage(EraseAdd + EraseBlockCnt * FLASH_BLOCK_SIZE);
|
||||
FLASH_Lock();
|
||||
|
||||
/* Erase failed */
|
||||
if(status != FLASH_COMPLETE)
|
||||
{
|
||||
OTA_IAP_SendCMDDealSta(status);
|
||||
return (events ^ OTA_FLASH_ERASE_EVT);
|
||||
}
|
||||
|
||||
EraseBlockCnt++;
|
||||
|
||||
/* End of erasing */
|
||||
if(EraseBlockCnt >= EraseBlockNum)
|
||||
{
|
||||
PRINT("ERASE Complete\r\n");
|
||||
OTA_IAP_SendCMDDealSta(SUCCESS);
|
||||
return (events ^ OTA_FLASH_ERASE_EVT);
|
||||
}
|
||||
|
||||
return (events);
|
||||
}
|
||||
|
||||
// Discard unknown events
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn Peripheral_ProcessTMOSMsg
|
||||
*
|
||||
* @brief Process an incoming task message.
|
||||
*
|
||||
* @param pMsg - message to process
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
static void Peripheral_ProcessTMOSMsg(tmos_event_hdr_t *pMsg)
|
||||
{
|
||||
switch(pMsg->event)
|
||||
{
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn peripheralStateNotificationCB
|
||||
*
|
||||
* @brief Notification from the profile of a state change.
|
||||
*
|
||||
* @param newState - new state
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
static void peripheralStateNotificationCB(gapRole_States_t newState, gapRoleEvent_t *pEvent)
|
||||
{
|
||||
switch(newState & GAPROLE_STATE_ADV_MASK)
|
||||
{
|
||||
case GAPROLE_STARTED:
|
||||
PRINT("Initialized..\n");
|
||||
break;
|
||||
|
||||
case GAPROLE_ADVERTISING:
|
||||
PRINT("Advertising..\n");
|
||||
break;
|
||||
|
||||
case GAPROLE_CONNECTED:
|
||||
{
|
||||
gapEstLinkReqEvent_t *event = (gapEstLinkReqEvent_t *)pEvent;
|
||||
uint16_t conn_interval = 0;
|
||||
|
||||
conn_interval = event->connInterval;
|
||||
PRINT("Connected.. \n");
|
||||
|
||||
if(conn_interval > DEFAULT_DESIRED_MAX_CONN_INTERVAL)
|
||||
{
|
||||
PRINT("Send Update\r\n");
|
||||
GAPRole_PeripheralConnParamUpdateReq(event->connectionHandle,
|
||||
DEFAULT_DESIRED_MIN_CONN_INTERVAL,
|
||||
DEFAULT_DESIRED_MAX_CONN_INTERVAL,
|
||||
DEFAULT_DESIRED_SLAVE_LATENCY,
|
||||
DEFAULT_DESIRED_CONN_TIMEOUT,
|
||||
Peripheral_TaskID);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case GAPROLE_CONNECTED_ADV:
|
||||
PRINT("Connected Advertising..\n");
|
||||
break;
|
||||
case GAPROLE_WAITING:
|
||||
{
|
||||
uint8_t initial_advertising_enable = TRUE;
|
||||
|
||||
// Set the GAP Role Parameters
|
||||
GAPRole_SetParameter(GAPROLE_ADVERT_ENABLED, sizeof(uint8_t), &initial_advertising_enable);
|
||||
PRINT("Disconnected..\n");
|
||||
}
|
||||
break;
|
||||
|
||||
case GAPROLE_ERROR:
|
||||
PRINT("Error..\n");
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn performPeriodicTask
|
||||
*
|
||||
* @brief Perform a periodic application task. This function gets
|
||||
* called every five seconds as a result of the SBP_PERIODIC_EVT
|
||||
* TMOS event. In this example, the value of the third
|
||||
* characteristic in the SimpleGATTProfile service is retrieved
|
||||
* from the profile, and then copied into the value of the
|
||||
* the fourth characteristic.
|
||||
*
|
||||
* @param none
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
static void performPeriodicTask(void)
|
||||
{
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn OTA_IAP_SendData
|
||||
*
|
||||
* @brief OTA IAP sends data, limits within 20 bytes when used
|
||||
*
|
||||
* @param p_send_data - Poems of sending data
|
||||
* @param send_len - Send data length
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
void OTA_IAP_SendData(uint8_t *p_send_data, uint8_t send_len)
|
||||
{
|
||||
OTAProfile_SendData(OTAPROFILE_CHAR, p_send_data, send_len);
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn OTA_IAP_SendCMDDealSta
|
||||
*
|
||||
* @brief OTA IAP execution status returns
|
||||
*
|
||||
* @param deal_status - Return state
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
void OTA_IAP_SendCMDDealSta(uint8_t deal_status)
|
||||
{
|
||||
uint8_t send_buf[2];
|
||||
|
||||
send_buf[0] = deal_status;
|
||||
send_buf[1] = 0;
|
||||
OTA_IAP_SendData(send_buf, 2);
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn OTA_IAP_CMDErrDeal
|
||||
*
|
||||
* @brief OTA IAP abnormal command code processing
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
void OTA_IAP_CMDErrDeal(void)
|
||||
{
|
||||
OTA_IAP_SendCMDDealSta(0xfe);
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn SwitchImageFlag
|
||||
*
|
||||
* @brief Switch the ImageFlag in DataFlash
|
||||
*
|
||||
* @param new_flag - Switching ImageFlag
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
void SwitchImageFlag(uint8_t new_flag)
|
||||
{
|
||||
uint16_t i;
|
||||
uint32_t ver_flag;
|
||||
|
||||
/* Read the first block */
|
||||
FLASH_read(OTA_DATAFLASH_ADD, &block_buf[0], 4);
|
||||
|
||||
FLASH_Unlock_Fast();
|
||||
/* Erase the first block */
|
||||
FLASH_ErasePage_Fast( OTA_DATAFLASH_ADD );
|
||||
|
||||
/* Update Image information */
|
||||
block_buf[0] = new_flag;
|
||||
block_buf[1] = 0x5A;
|
||||
block_buf[2] = 0x5A;
|
||||
block_buf[3] = 0x5A;
|
||||
|
||||
/* Programming DataFlash */
|
||||
FLASH_ProgramPage_Fast( OTA_DATAFLASH_ADD, (uint32_t *)&block_buf[0]);
|
||||
FLASH_Lock_Fast();
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn DisableAllIRQ
|
||||
*
|
||||
* @brief Turn off all the interrupts
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
void DisableAllIRQ(void)
|
||||
{
|
||||
__disable_irq();
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn Rec_OTA_IAP_DataDeal
|
||||
*
|
||||
* @brief Receive OTA packet processing
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
void Rec_OTA_IAP_DataDeal(void)
|
||||
{
|
||||
switch(iap_rec_data.other.buf[0])
|
||||
{
|
||||
/* Programming */
|
||||
case CMD_IAP_PROM:
|
||||
{
|
||||
uint32_t i;
|
||||
uint8_t status;
|
||||
|
||||
OpParaDataLen = iap_rec_data.program.len;
|
||||
OpAdd = (uint32_t)(iap_rec_data.program.addr[0]);
|
||||
OpAdd |= ((uint32_t)(iap_rec_data.program.addr[1]) << 8);
|
||||
OpAdd = OpAdd * 16;
|
||||
|
||||
PRINT("IAP_PROM: %08x len:%d \r\n", (int)OpAdd, (int)OpParaDataLen);
|
||||
|
||||
/* Current is ImageA, programming directly */
|
||||
tmos_memcpy(&block_buf[block_buf_len], iap_rec_data.program.buf, OpParaDataLen);
|
||||
block_buf_len+=OpParaDataLen;
|
||||
if( block_buf_len>=FLASH_PAGE_SIZE )
|
||||
{
|
||||
FLASH_Unlock_Fast();
|
||||
FLASH_ProgramPage_Fast(prom_addr, (uint32_t*)block_buf);
|
||||
FLASH_Lock_Fast();
|
||||
tmos_memcpy(block_buf, &block_buf[FLASH_PAGE_SIZE], block_buf_len-FLASH_PAGE_SIZE);
|
||||
block_buf_len-=FLASH_PAGE_SIZE;
|
||||
prom_addr+=FLASH_PAGE_SIZE;
|
||||
}
|
||||
OTA_IAP_SendCMDDealSta(status);
|
||||
break;
|
||||
}
|
||||
/* Erase -- Bluetooth erase is controlled by the host */
|
||||
case CMD_IAP_ERASE:
|
||||
{
|
||||
OpAdd = (uint32_t)(iap_rec_data.erase.addr[0]);
|
||||
OpAdd |= ((uint32_t)(iap_rec_data.erase.addr[1]) << 8);
|
||||
OpAdd = OpAdd * 16;
|
||||
|
||||
OpAdd += 0x08000000;
|
||||
|
||||
EraseBlockNum = (uint32_t)(iap_rec_data.erase.block_num[0]);
|
||||
EraseBlockNum |= ((uint32_t)(iap_rec_data.erase.block_num[1]) << 8);
|
||||
EraseAdd = OpAdd;
|
||||
EraseBlockCnt = 0;
|
||||
|
||||
/* The inspection is placed in the era of clearing 0 */
|
||||
VerifyStatus = 0;
|
||||
|
||||
prom_addr = IMAGE_A_START_ADD;
|
||||
PRINT("IAP_ERASE start:%08x num:%d\r\n", (int)OpAdd, (int)EraseBlockNum);
|
||||
|
||||
if(EraseAdd < IMAGE_A_START_ADD || (EraseAdd + (EraseBlockNum - 1) * FLASH_BLOCK_SIZE) > (IMAGE_A_START_ADD+IMAGE_A_SIZE))
|
||||
{
|
||||
OTA_IAP_SendCMDDealSta(0xFF);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Modify DataFlash, switch to ImageB */
|
||||
SwitchImageFlag(IMAGE_IAP_FLAG);
|
||||
|
||||
/* Start erasing */
|
||||
tmos_set_event(Peripheral_TaskID, OTA_FLASH_ERASE_EVT);
|
||||
}
|
||||
break;
|
||||
}
|
||||
/* Verify */
|
||||
case CMD_IAP_VERIFY:
|
||||
{
|
||||
uint32_t i;
|
||||
uint8_t status = 0;
|
||||
uint8_t verifyData[iap_rec_data.verify.len];
|
||||
|
||||
if( block_buf_len )
|
||||
{
|
||||
FLASH_Unlock_Fast();
|
||||
FLASH_ProgramPage_Fast(prom_addr, (uint32_t*)block_buf);
|
||||
FLASH_Lock_Fast();
|
||||
block_buf_len=0;
|
||||
prom_addr=0;
|
||||
}
|
||||
|
||||
OpParaDataLen = iap_rec_data.verify.len;
|
||||
|
||||
OpAdd = (uint32_t)(iap_rec_data.verify.addr[0]);
|
||||
OpAdd |= ((uint32_t)(iap_rec_data.verify.addr[1]) << 8);
|
||||
OpAdd = OpAdd * 16;
|
||||
|
||||
OpAdd += 0x08000000;
|
||||
PRINT("IAP_VERIFY: %08x len:%d \r\n", (int)OpAdd, (int)OpParaDataLen);
|
||||
FLASH_read(OpAdd, verifyData, OpParaDataLen);
|
||||
/* It is currently ImageA, read the ImageB check directly */
|
||||
status = tmos_memcmp(verifyData, iap_rec_data.verify.buf, OpParaDataLen);
|
||||
if(status == FALSE)
|
||||
{
|
||||
PRINT("IAP_VERIFY err \r\n");
|
||||
VerifyStatus = 0xFF;
|
||||
}
|
||||
OTA_IAP_SendCMDDealSta(VerifyStatus);
|
||||
break;
|
||||
}
|
||||
/* End of rogramming */
|
||||
case CMD_IAP_END:
|
||||
{
|
||||
PRINT("IAP_END \r\n");
|
||||
|
||||
/* Close all the current use interrupt, or it is convenient to directly close */
|
||||
DisableAllIRQ();
|
||||
|
||||
/* Modify data flash, switch to ImageA */
|
||||
SwitchImageFlag(IMAGE_A_FLAG);
|
||||
|
||||
/* Waiting for printing, jump to ImageB*/
|
||||
Delay_Ms(10);
|
||||
jumpApp();
|
||||
|
||||
/* Will not execute here */
|
||||
NVIC_SystemReset();
|
||||
|
||||
break;
|
||||
}
|
||||
case CMD_IAP_INFO:
|
||||
{
|
||||
uint8_t send_buf[20];
|
||||
|
||||
PRINT("IAP_INFO \r\n");
|
||||
|
||||
/* IMAGE FLAG */
|
||||
send_buf[0] = IMAGE_IAP_FLAG;
|
||||
|
||||
/* IMAGE_IAP_START_ADD */
|
||||
send_buf[1] = (uint8_t)(IMAGE_IAP_START_ADD & 0xff);
|
||||
send_buf[2] = (uint8_t)((IMAGE_IAP_START_ADD >> 8) & 0xff);
|
||||
send_buf[3] = (uint8_t)((IMAGE_IAP_START_ADD >> 16) & 0xff);
|
||||
send_buf[4] = (uint8_t)((IMAGE_IAP_START_ADD >> 24) & 0xff);
|
||||
|
||||
/* BLOCK SIZE */
|
||||
send_buf[5] = (uint8_t)(FLASH_BLOCK_SIZE & 0xff);
|
||||
send_buf[6] = (uint8_t)((FLASH_BLOCK_SIZE >> 8) & 0xff);
|
||||
|
||||
send_buf[7] = CHIP_ID&0xFF;
|
||||
send_buf[8] = (CHIP_ID>>8)&0xFF;
|
||||
/* Add more if necessary */
|
||||
|
||||
/* send message */
|
||||
OTA_IAP_SendData(send_buf, 20);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
{
|
||||
OTA_IAP_CMDErrDeal();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn OTA_IAPReadDataComplete
|
||||
*
|
||||
* @brief OTA data reading complete processing
|
||||
*
|
||||
* @param index - OTA channel serial number
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
void OTA_IAPReadDataComplete(unsigned char index)
|
||||
{
|
||||
PRINT("OTA Send Comp \r\n");
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn OTA_IAPWriteData
|
||||
*
|
||||
* @brief OTA channel data receiving complete processing
|
||||
*
|
||||
* @param index - OTA channel serial number
|
||||
* @param p_data - Written data
|
||||
* @param w_len - Length
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
void OTA_IAPWriteData(unsigned char index, unsigned char *p_data, unsigned char w_len)
|
||||
{
|
||||
unsigned char rec_len;
|
||||
unsigned char *rec_data;
|
||||
|
||||
rec_len = w_len;
|
||||
rec_data = p_data;
|
||||
tmos_memcpy((unsigned char *)&iap_rec_data, rec_data, rec_len);
|
||||
Rec_OTA_IAP_DataDeal();
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn FLASH_read
|
||||
*
|
||||
* @brief Read flash
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
void FLASH_read(uint32_t addr, uint8_t *pData, uint32_t len)
|
||||
{
|
||||
uint32_t i;
|
||||
for(i=0;i<len;i++)
|
||||
{
|
||||
*pData++ = *(uint8_t*)addr++;
|
||||
}
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
*********************************************************************/
|
||||
122
vd960DBN/BLE/OnlyUpdateApp_IAP/APP/peripheral_main.c
Normal file
122
vd960DBN/BLE/OnlyUpdateApp_IAP/APP/peripheral_main.c
Normal file
@@ -0,0 +1,122 @@
|
||||
/********************************** (C) COPYRIGHT *******************************
|
||||
* File Name : main.c
|
||||
* Author : WCH
|
||||
* Version : V1.1
|
||||
* Date : 2019/11/05
|
||||
* Description : Upgrade slave application main function and task system initialization
|
||||
*********************************************************************************
|
||||
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
|
||||
* Attention: This software (modified or not) and binary are used for
|
||||
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
|
||||
*******************************************************************************/
|
||||
|
||||
/******************************************************************************/
|
||||
/* Header file contains */
|
||||
#include "CONFIG.h"
|
||||
#include "HAL.h"
|
||||
#include "Peripheral.h"
|
||||
#include "OTA.h"
|
||||
#include "OTAprofile.h"
|
||||
|
||||
/* ¼Ç¼µ±Ç°µÄImage */
|
||||
unsigned char CurrImageFlag = 0xff;
|
||||
|
||||
/*********************************************************************
|
||||
* GLOBAL TYPEDEFS
|
||||
*/
|
||||
__attribute__((aligned(4))) uint32_t MEM_BUF[BLE_MEMHEAP_SIZE / 4];
|
||||
|
||||
#if(defined(BLE_MAC)) && (BLE_MAC == TRUE)
|
||||
const uint8_t MacAddr[6] = {0x84, 0xC2, 0xE4, 0x03, 0x02, 0x02};
|
||||
#endif
|
||||
|
||||
/* Note: The operation of Flash after the program is upgraded must be performed first
|
||||
* without turning on any interruption to prevent operation interruption and failure
|
||||
*/
|
||||
/*********************************************************************
|
||||
* @fn ReadImageFlag
|
||||
*
|
||||
* @brief Read the iMage logo of the current program.
|
||||
* If the DataFlash is empty, it will be Imagea by default.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
void ReadImageFlag(void)
|
||||
{
|
||||
OTADataFlashInfo_t p_image_flash;
|
||||
|
||||
FLASH_read(OTA_DATAFLASH_ADD, (uint8_t *)&p_image_flash, 4);
|
||||
CurrImageFlag = p_image_flash.ImageFlag;
|
||||
|
||||
/* The program is executed for the first time, or it has not been updated,
|
||||
* and the DataFLASH is erased after being updated in the future
|
||||
*/
|
||||
if((p_image_flash.flag[0] != 0x5A) || (p_image_flash.flag[1] != 0x5A) || (p_image_flash.flag[2] != 0x5A))
|
||||
{
|
||||
CurrImageFlag = IMAGE_A_FLAG;
|
||||
}
|
||||
|
||||
PRINT("Image Flag %02x\n", CurrImageFlag);
|
||||
|
||||
if(CurrImageFlag == IMAGE_A_FLAG)
|
||||
{
|
||||
PRINT("jump App \n");
|
||||
Delay_Ms(5);
|
||||
jumpApp();
|
||||
}
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn Main_Circulation
|
||||
*
|
||||
* @brief Main loop
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
__attribute__((section(".highcode")))
|
||||
__attribute__((noinline))
|
||||
void Main_Circulation(void)
|
||||
{
|
||||
while(1)
|
||||
{
|
||||
TMOS_SystemProcess();
|
||||
}
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn main
|
||||
*
|
||||
* @brief Main function
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
int main(void)
|
||||
{
|
||||
SystemCoreClockUpdate();
|
||||
Delay_Init();
|
||||
#ifdef DEBUG
|
||||
USART_Printf_Init(115200);
|
||||
#endif
|
||||
PRINT("%s\n", VER_LIB);
|
||||
ReadImageFlag();
|
||||
if(RCC_GetFlagStatus(RCC_FLAG_SFTRST) == SET)
|
||||
{
|
||||
// Soft reset does not jump app app
|
||||
}
|
||||
else
|
||||
{
|
||||
if(CurrImageFlag == IMAGE_OTA_FLAG)
|
||||
{
|
||||
PRINT("jump App \n");
|
||||
Delay_Ms(5);
|
||||
jumpApp();
|
||||
}
|
||||
}
|
||||
WCHBLE_Init();
|
||||
HAL_Init();
|
||||
GAPRole_PeripheralInit();
|
||||
Peripheral_Init();
|
||||
Main_Circulation();
|
||||
}
|
||||
|
||||
/******************************** endfile @ main ******************************/
|
||||
990
vd960DBN/BLE/OnlyUpdateApp_IAP/APP/system_ch32v20x.c
Normal file
990
vd960DBN/BLE/OnlyUpdateApp_IAP/APP/system_ch32v20x.c
Normal file
@@ -0,0 +1,990 @@
|
||||
/********************************** (C) COPYRIGHT *******************************
|
||||
* File Name : system_ch32v20x.c
|
||||
* Author : WCH
|
||||
* Version : V1.0.0
|
||||
* Date : 2021/06/06
|
||||
* Description : CH32V20x Device Peripheral Access Layer System Source File.
|
||||
* For HSE = 32Mhz (CH32V208x/CH32V203RBT6)
|
||||
* For HSE = 8Mhz (other CH32V203x)
|
||||
*********************************************************************************
|
||||
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
|
||||
* Attention: This software (modified or not) and binary are used for
|
||||
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
|
||||
*******************************************************************************/
|
||||
#include "ch32v20x.h"
|
||||
|
||||
/*
|
||||
* Uncomment the line corresponding to the desired System clock (SYSCLK) frequency (after
|
||||
* reset the HSI is used as SYSCLK source).
|
||||
* If none of the define below is enabled, the HSI is used as System clock source.
|
||||
*/
|
||||
//#define SYSCLK_FREQ_HSE HSE_VALUE
|
||||
//#define SYSCLK_FREQ_48MHz_HSE 48000000
|
||||
//#define SYSCLK_FREQ_56MHz_HSE 56000000
|
||||
//#define SYSCLK_FREQ_72MHz_HSE 72000000
|
||||
#define SYSCLK_FREQ_96MHz_HSE 96000000
|
||||
//#define SYSCLK_FREQ_120MHz_HSE 120000000
|
||||
//#define SYSCLK_FREQ_144MHz_HSE 144000000
|
||||
//#define SYSCLK_FREQ_HSI HSI_VALUE
|
||||
//#define SYSCLK_FREQ_48MHz_HSI 48000000
|
||||
//#define SYSCLK_FREQ_56MHz_HSI 56000000
|
||||
//#define SYSCLK_FREQ_72MHz_HSI 72000000
|
||||
//#define SYSCLK_FREQ_96MHz_HSI 96000000
|
||||
//#define SYSCLK_FREQ_120MHz_HSI 120000000
|
||||
//#define SYSCLK_FREQ_144MHz_HSI 144000000
|
||||
|
||||
/* Clock Definitions */
|
||||
#ifdef SYSCLK_FREQ_HSE
|
||||
uint32_t SystemCoreClock = SYSCLK_FREQ_HSE; /* System Clock Frequency (Core Clock) */
|
||||
#elif defined SYSCLK_FREQ_48MHz_HSE
|
||||
uint32_t SystemCoreClock = SYSCLK_FREQ_48MHz_HSE; /* System Clock Frequency (Core Clock) */
|
||||
#elif defined SYSCLK_FREQ_56MHz_HSE
|
||||
uint32_t SystemCoreClock = SYSCLK_FREQ_56MHz_HSE; /* System Clock Frequency (Core Clock) */
|
||||
#elif defined SYSCLK_FREQ_72MHz_HSE
|
||||
uint32_t SystemCoreClock = SYSCLK_FREQ_72MHz_HSE; /* System Clock Frequency (Core Clock) */
|
||||
#elif defined SYSCLK_FREQ_96MHz_HSE
|
||||
uint32_t SystemCoreClock = SYSCLK_FREQ_96MHz_HSE; /* System Clock Frequency (Core Clock) */
|
||||
#elif defined SYSCLK_FREQ_120MHz_HSE
|
||||
uint32_t SystemCoreClock = SYSCLK_FREQ_120MHz_HSE; /* System Clock Frequency (Core Clock) */
|
||||
#elif defined SYSCLK_FREQ_144MHz_HSE
|
||||
uint32_t SystemCoreClock = SYSCLK_FREQ_144MHz_HSE; /* System Clock Frequency (Core Clock) */
|
||||
#elif defined SYSCLK_FREQ_48MHz_HSI
|
||||
uint32_t SystemCoreClock = SYSCLK_FREQ_48MHz_HSI; /* System Clock Frequency (Core Clock) */
|
||||
#elif defined SYSCLK_FREQ_56MHz_HSI
|
||||
uint32_t SystemCoreClock = SYSCLK_FREQ_56MHz_HSI; /* System Clock Frequency (Core Clock) */
|
||||
#elif defined SYSCLK_FREQ_72MHz_HSI
|
||||
uint32_t SystemCoreClock = SYSCLK_FREQ_72MHz_HSI; /* System Clock Frequency (Core Clock) */
|
||||
#elif defined SYSCLK_FREQ_96MHz_HSI
|
||||
uint32_t SystemCoreClock = SYSCLK_FREQ_96MHz_HSI; /* System Clock Frequency (Core Clock) */
|
||||
#elif defined SYSCLK_FREQ_120MHz_HSI
|
||||
uint32_t SystemCoreClock = SYSCLK_FREQ_120MHz_HSI; /* System Clock Frequency (Core Clock) */
|
||||
#elif defined SYSCLK_FREQ_144MHz_HSI
|
||||
uint32_t SystemCoreClock = SYSCLK_FREQ_144MHz_HSI; /* System Clock Frequency (Core Clock) */
|
||||
#else
|
||||
uint32_t SystemCoreClock = HSI_VALUE; /* System Clock Frequency (Core Clock) */
|
||||
|
||||
#endif
|
||||
|
||||
__I uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
|
||||
|
||||
|
||||
/* system_private_function_proto_types */
|
||||
static void SetSysClock(void);
|
||||
|
||||
#ifdef SYSCLK_FREQ_HSE
|
||||
static void SetSysClockToHSE( void );
|
||||
#elif defined SYSCLK_FREQ_48MHz_HSE
|
||||
static void SetSysClockTo48_HSE( void );
|
||||
#elif defined SYSCLK_FREQ_56MHz_HSE
|
||||
static void SetSysClockTo56_HSE( void );
|
||||
#elif defined SYSCLK_FREQ_72MHz_HSE
|
||||
static void SetSysClockTo72_HSE( void );
|
||||
#elif defined SYSCLK_FREQ_96MHz_HSE
|
||||
static void SetSysClockTo96_HSE( void );
|
||||
#elif defined SYSCLK_FREQ_120MHz_HSE
|
||||
static void SetSysClockTo120_HSE( void );
|
||||
#elif defined SYSCLK_FREQ_144MHz_HSE
|
||||
static void SetSysClockTo144_HSE( void );
|
||||
#elif defined SYSCLK_FREQ_48MHz_HSI
|
||||
static void SetSysClockTo48_HSI( void );
|
||||
#elif defined SYSCLK_FREQ_56MHz_HSI
|
||||
static void SetSysClockTo56_HSI( void );
|
||||
#elif defined SYSCLK_FREQ_72MHz_HSI
|
||||
static void SetSysClockTo72_HSI( void );
|
||||
#elif defined SYSCLK_FREQ_96MHz_HSI
|
||||
static void SetSysClockTo96_HSI( void );
|
||||
#elif defined SYSCLK_FREQ_120MHz_HSI
|
||||
static void SetSysClockTo120_HSI( void );
|
||||
#elif defined SYSCLK_FREQ_144MHz_HSI
|
||||
static void SetSysClockTo144_HSI( void );
|
||||
|
||||
#endif
|
||||
|
||||
/*********************************************************************
|
||||
* @fn SystemInit
|
||||
*
|
||||
* @brief Setup the microcontroller system Initialize the Embedded Flash Interface,
|
||||
* the PLL and update the SystemCoreClock variable.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
void SystemInit (void)
|
||||
{
|
||||
RCC->CTLR |= (uint32_t)0x00000001;
|
||||
RCC->CFGR0 &= (uint32_t)0xF0FF0000;
|
||||
RCC->CTLR &= (uint32_t)0xFEF6FFFF;
|
||||
RCC->CTLR &= (uint32_t)0xFFFBFFFF;
|
||||
RCC->CFGR0 &= (uint32_t)0xFF00FFFF;
|
||||
RCC->INTR = 0x009F0000;
|
||||
SetSysClock();
|
||||
}
|
||||
|
||||
|
||||
/*********************************************************************
|
||||
* @fn SystemCoreClockUpdate
|
||||
*
|
||||
* @brief Update SystemCoreClock variable according to Clock Register Values.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
void SystemCoreClockUpdate (void)
|
||||
{
|
||||
uint32_t tmp = 0, pllmull = 0, pllsource = 0, Pll_6_5 = 0;
|
||||
|
||||
tmp = RCC->CFGR0 & RCC_SWS;
|
||||
|
||||
switch (tmp)
|
||||
{
|
||||
case 0x00:
|
||||
SystemCoreClock = HSI_VALUE;
|
||||
break;
|
||||
case 0x04:
|
||||
SystemCoreClock = HSE_VALUE;
|
||||
break;
|
||||
case 0x08:
|
||||
pllmull = RCC->CFGR0 & RCC_PLLMULL;
|
||||
pllsource = RCC->CFGR0 & RCC_PLLSRC;
|
||||
pllmull = ( pllmull >> 18) + 2;
|
||||
|
||||
if(pllmull == 17) pllmull = 18;
|
||||
|
||||
if (pllsource == 0x00)
|
||||
{
|
||||
if(EXTEN->EXTEN_CTR & EXTEN_PLL_HSI_PRE){
|
||||
SystemCoreClock = HSI_VALUE * pllmull;
|
||||
}
|
||||
else{
|
||||
SystemCoreClock = (HSI_VALUE >> 1) * pllmull;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
#if defined (CH32V20x_D8W) || defined (CH32V20x_D8)
|
||||
if(((RCC->CFGR0 & (3<<22)) == (3<<22)) && (RCC_USB5PRE_JUDGE()== SET))
|
||||
{
|
||||
SystemCoreClock = ((HSE_VALUE>>1)) * pllmull;
|
||||
}
|
||||
else
|
||||
#endif
|
||||
if ((RCC->CFGR0 & RCC_PLLXTPRE) != (uint32_t)RESET)
|
||||
{
|
||||
#if defined (CH32V20x_D8) || defined (CH32V20x_D8W)
|
||||
SystemCoreClock = ((HSE_VALUE>>2) >> 1) * pllmull;
|
||||
#else
|
||||
SystemCoreClock = (HSE_VALUE >> 1) * pllmull;
|
||||
#endif
|
||||
}
|
||||
else
|
||||
{
|
||||
#if defined (CH32V20x_D8) || defined (CH32V20x_D8W)
|
||||
SystemCoreClock = (HSE_VALUE>>2) * pllmull;
|
||||
#else
|
||||
SystemCoreClock = HSE_VALUE * pllmull;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
if(Pll_6_5 == 1) SystemCoreClock = (SystemCoreClock / 2);
|
||||
|
||||
break;
|
||||
default:
|
||||
SystemCoreClock = HSI_VALUE;
|
||||
break;
|
||||
}
|
||||
|
||||
tmp = AHBPrescTable[((RCC->CFGR0 & RCC_HPRE) >> 4)];
|
||||
SystemCoreClock >>= tmp;
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn SetSysClock
|
||||
*
|
||||
* @brief Configures the System clock frequency, HCLK, PCLK2 and PCLK1 prescalers.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
static void SetSysClock(void)
|
||||
{
|
||||
//GPIO_IPD_Unused();
|
||||
#ifdef SYSCLK_FREQ_HSE
|
||||
SetSysClockToHSE();
|
||||
#elif defined SYSCLK_FREQ_48MHz_HSE
|
||||
SetSysClockTo48_HSE();
|
||||
#elif defined SYSCLK_FREQ_56MHz_HSE
|
||||
SetSysClockTo56_HSE();
|
||||
#elif defined SYSCLK_FREQ_72MHz_HSE
|
||||
SetSysClockTo72_HSE();
|
||||
#elif defined SYSCLK_FREQ_96MHz_HSE
|
||||
SetSysClockTo96_HSE();
|
||||
#elif defined SYSCLK_FREQ_120MHz_HSE
|
||||
SetSysClockTo120_HSE();
|
||||
#elif defined SYSCLK_FREQ_144MHz_HSE
|
||||
SetSysClockTo144_HSE();
|
||||
#elif defined SYSCLK_FREQ_48MHz_HSI
|
||||
SetSysClockTo48_HSI();
|
||||
#elif defined SYSCLK_FREQ_56MHz_HSI
|
||||
SetSysClockTo56_HSI();
|
||||
#elif defined SYSCLK_FREQ_72MHz_HSI
|
||||
SetSysClockTo72_HSI();
|
||||
#elif defined SYSCLK_FREQ_96MHz_HSI
|
||||
SetSysClockTo96_HSI();
|
||||
#elif defined SYSCLK_FREQ_120MHz_HSI
|
||||
SetSysClockTo120_HSI();
|
||||
#elif defined SYSCLK_FREQ_144MHz_HSI
|
||||
SetSysClockTo144_HSI();
|
||||
|
||||
#endif
|
||||
|
||||
/* If none of the define above is enabled, the HSI is used as System clock
|
||||
* source (default after reset)
|
||||
*/
|
||||
}
|
||||
|
||||
|
||||
#ifdef SYSCLK_FREQ_HSE
|
||||
|
||||
/*********************************************************************
|
||||
* @fn SetSysClockToHSE
|
||||
*
|
||||
* @brief Sets HSE as System clock source and configure HCLK, PCLK2 and PCLK1 prescalers.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
static void SetSysClockToHSE(void)
|
||||
{
|
||||
__IO uint32_t StartUpCounter = 0, HSEStatus = 0;
|
||||
|
||||
|
||||
RCC->CTLR |= ((uint32_t)RCC_HSEON);
|
||||
|
||||
/* Wait till HSE is ready and if Time out is reached exit */
|
||||
do
|
||||
{
|
||||
HSEStatus = RCC->CTLR & RCC_HSERDY;
|
||||
StartUpCounter++;
|
||||
} while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
|
||||
|
||||
if ((RCC->CTLR & RCC_HSERDY) != RESET)
|
||||
{
|
||||
HSEStatus = (uint32_t)0x01;
|
||||
}
|
||||
else
|
||||
{
|
||||
HSEStatus = (uint32_t)0x00;
|
||||
}
|
||||
|
||||
if (HSEStatus == (uint32_t)0x01)
|
||||
{
|
||||
/* HCLK = SYSCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV1;
|
||||
/* PCLK2 = HCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_PPRE2_DIV1;
|
||||
/* PCLK1 = HCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_PPRE1_DIV1;
|
||||
|
||||
/* Select HSE as system clock source
|
||||
* CH32V20x_D6 (HSE=8MHZ)
|
||||
* CH32V20x_D8 (HSE=32MHZ)
|
||||
* CH32V20x_D8W (HSE=32MHZ)
|
||||
*/
|
||||
RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_SW));
|
||||
RCC->CFGR0 |= (uint32_t)RCC_SW_HSE;
|
||||
|
||||
/* Wait till HSE is used as system clock source */
|
||||
while ((RCC->CFGR0 & (uint32_t)RCC_SWS) != (uint32_t)0x04)
|
||||
{
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/* If HSE fails to start-up, the application will have wrong clock
|
||||
* configuration. User can add here some code to deal with this error
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
||||
#elif defined SYSCLK_FREQ_48MHz_HSE
|
||||
|
||||
/*********************************************************************
|
||||
* @fn SetSysClockTo48_HSE
|
||||
*
|
||||
* @brief Sets System clock frequency to 48MHz and configure HCLK, PCLK2 and PCLK1 prescalers.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
static void SetSysClockTo48_HSE(void)
|
||||
{
|
||||
__IO uint32_t StartUpCounter = 0, HSEStatus = 0;
|
||||
|
||||
|
||||
RCC->CTLR |= ((uint32_t)RCC_HSEON);
|
||||
/* Wait till HSE is ready and if Time out is reached exit */
|
||||
do
|
||||
{
|
||||
HSEStatus = RCC->CTLR & RCC_HSERDY;
|
||||
StartUpCounter++;
|
||||
} while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
|
||||
|
||||
if ((RCC->CTLR & RCC_HSERDY) != RESET)
|
||||
{
|
||||
HSEStatus = (uint32_t)0x01;
|
||||
}
|
||||
else
|
||||
{
|
||||
HSEStatus = (uint32_t)0x00;
|
||||
}
|
||||
|
||||
if (HSEStatus == (uint32_t)0x01)
|
||||
{
|
||||
/* HCLK = SYSCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV1;
|
||||
/* PCLK2 = HCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_PPRE2_DIV1;
|
||||
/* PCLK1 = HCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_PPRE1_DIV2;
|
||||
|
||||
/* CH32V20x_D6-PLL configuration: PLLCLK = HSE * 6 = 48 MHz (HSE=8MHZ)
|
||||
* CH32V20x_D8-PLL configuration: PLLCLK = HSE/4 * 6 = 48 MHz (HSE=32MHZ)
|
||||
* CH32V20x_D8W-PLL configuration: PLLCLK = HSE/4 * 6 = 48 MHz (HSE=32MHZ)
|
||||
*/
|
||||
RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_PLLSRC | RCC_PLLXTPRE | RCC_PLLMULL));
|
||||
|
||||
RCC->CFGR0 |= (uint32_t)(RCC_PLLSRC_HSE | RCC_PLLXTPRE_HSE | RCC_PLLMULL6);
|
||||
|
||||
/* Enable PLL */
|
||||
RCC->CTLR |= RCC_PLLON;
|
||||
/* Wait till PLL is ready */
|
||||
while((RCC->CTLR & RCC_PLLRDY) == 0)
|
||||
{
|
||||
}
|
||||
/* Select PLL as system clock source */
|
||||
RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_SW));
|
||||
RCC->CFGR0 |= (uint32_t)RCC_SW_PLL;
|
||||
/* Wait till PLL is used as system clock source */
|
||||
while ((RCC->CFGR0 & (uint32_t)RCC_SWS) != (uint32_t)0x08)
|
||||
{
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/*
|
||||
* If HSE fails to start-up, the application will have wrong clock
|
||||
* configuration. User can add here some code to deal with this error
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
||||
#elif defined SYSCLK_FREQ_56MHz_HSE
|
||||
|
||||
/*********************************************************************
|
||||
* @fn SetSysClockTo56_HSE
|
||||
*
|
||||
* @brief Sets System clock frequency to 56MHz and configure HCLK, PCLK2 and PCLK1 prescalers.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
static void SetSysClockTo56_HSE(void)
|
||||
{
|
||||
__IO uint32_t StartUpCounter = 0, HSEStatus = 0;
|
||||
|
||||
RCC->CTLR |= ((uint32_t)RCC_HSEON);
|
||||
|
||||
/* Wait till HSE is ready and if Time out is reached exit */
|
||||
do
|
||||
{
|
||||
HSEStatus = RCC->CTLR & RCC_HSERDY;
|
||||
StartUpCounter++;
|
||||
} while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
|
||||
|
||||
if ((RCC->CTLR & RCC_HSERDY) != RESET)
|
||||
{
|
||||
HSEStatus = (uint32_t)0x01;
|
||||
}
|
||||
else
|
||||
{
|
||||
HSEStatus = (uint32_t)0x00;
|
||||
}
|
||||
|
||||
if (HSEStatus == (uint32_t)0x01)
|
||||
{
|
||||
/* HCLK = SYSCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV1;
|
||||
/* PCLK2 = HCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_PPRE2_DIV1;
|
||||
/* PCLK1 = HCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_PPRE1_DIV2;
|
||||
|
||||
/* CH32V20x_D6-PLL configuration: PLLCLK = HSE * 7 = 56 MHz (HSE=8MHZ)
|
||||
* CH32V20x_D8-PLL configuration: PLLCLK = HSE/4 * 7 = 56 MHz (HSE=32MHZ)
|
||||
* CH32V20x_D8W-PLL configuration: PLLCLK = HSE/4 * 7 = 56 MHz (HSE=32MHZ)
|
||||
*/
|
||||
RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_PLLSRC | RCC_PLLXTPRE | RCC_PLLMULL));
|
||||
|
||||
RCC->CFGR0 |= (uint32_t)(RCC_PLLSRC_HSE | RCC_PLLXTPRE_HSE | RCC_PLLMULL7);
|
||||
|
||||
/* Enable PLL */
|
||||
RCC->CTLR |= RCC_PLLON;
|
||||
/* Wait till PLL is ready */
|
||||
while((RCC->CTLR & RCC_PLLRDY) == 0)
|
||||
{
|
||||
}
|
||||
|
||||
/* Select PLL as system clock source */
|
||||
RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_SW));
|
||||
RCC->CFGR0 |= (uint32_t)RCC_SW_PLL;
|
||||
/* Wait till PLL is used as system clock source */
|
||||
while ((RCC->CFGR0 & (uint32_t)RCC_SWS) != (uint32_t)0x08)
|
||||
{
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/*
|
||||
* If HSE fails to start-up, the application will have wrong clock
|
||||
* configuration. User can add here some code to deal with this error
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
||||
#elif defined SYSCLK_FREQ_72MHz_HSE
|
||||
|
||||
/*********************************************************************
|
||||
* @fn SetSysClockTo72_HSE
|
||||
*
|
||||
* @brief Sets System clock frequency to 72MHz and configure HCLK, PCLK2 and PCLK1 prescalers.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
static void SetSysClockTo72_HSE(void)
|
||||
{
|
||||
__IO uint32_t StartUpCounter = 0, HSEStatus = 0;
|
||||
|
||||
RCC->CTLR |= ((uint32_t)RCC_HSEON);
|
||||
|
||||
/* Wait till HSE is ready and if Time out is reached exit */
|
||||
do
|
||||
{
|
||||
HSEStatus = RCC->CTLR & RCC_HSERDY;
|
||||
StartUpCounter++;
|
||||
} while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
|
||||
|
||||
if ((RCC->CTLR & RCC_HSERDY) != RESET)
|
||||
{
|
||||
HSEStatus = (uint32_t)0x01;
|
||||
}
|
||||
else
|
||||
{
|
||||
HSEStatus = (uint32_t)0x00;
|
||||
}
|
||||
|
||||
if (HSEStatus == (uint32_t)0x01)
|
||||
{
|
||||
/* HCLK = SYSCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV1;
|
||||
/* PCLK2 = HCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_PPRE2_DIV1;
|
||||
/* PCLK1 = HCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_PPRE1_DIV2;
|
||||
|
||||
|
||||
/* CH32V20x_D6-PLL configuration: PLLCLK = HSE * 9 = 72 MHz (HSE=8MHZ)
|
||||
* CH32V20x_D8-PLL configuration: PLLCLK = HSE/4 * 9 = 72 MHz (HSE=32MHZ)
|
||||
* CH32V20x_D8W-PLL configuration: PLLCLK = HSE/4 * 9 = 72 MHz (HSE=32MHZ)
|
||||
*/
|
||||
RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_PLLSRC | RCC_PLLXTPRE |
|
||||
RCC_PLLMULL));
|
||||
|
||||
RCC->CFGR0 |= (uint32_t)(RCC_PLLSRC_HSE | RCC_PLLXTPRE_HSE | RCC_PLLMULL9);
|
||||
|
||||
/* Enable PLL */
|
||||
RCC->CTLR |= RCC_PLLON;
|
||||
/* Wait till PLL is ready */
|
||||
while((RCC->CTLR & RCC_PLLRDY) == 0)
|
||||
{
|
||||
}
|
||||
/* Select PLL as system clock source */
|
||||
RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_SW));
|
||||
RCC->CFGR0 |= (uint32_t)RCC_SW_PLL;
|
||||
/* Wait till PLL is used as system clock source */
|
||||
while ((RCC->CFGR0 & (uint32_t)RCC_SWS) != (uint32_t)0x08)
|
||||
{
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/*
|
||||
* If HSE fails to start-up, the application will have wrong clock
|
||||
* configuration. User can add here some code to deal with this error
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#elif defined SYSCLK_FREQ_96MHz_HSE
|
||||
|
||||
/*********************************************************************
|
||||
* @fn SetSysClockTo96_HSE
|
||||
*
|
||||
* @brief Sets System clock frequency to 96MHz and configure HCLK, PCLK2 and PCLK1 prescalers.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
static void SetSysClockTo96_HSE(void)
|
||||
{
|
||||
__IO uint32_t StartUpCounter = 0, HSEStatus = 0;
|
||||
|
||||
RCC->CTLR |= ((uint32_t)RCC_HSEON);
|
||||
|
||||
/* Wait till HSE is ready and if Time out is reached exit */
|
||||
do
|
||||
{
|
||||
HSEStatus = RCC->CTLR & RCC_HSERDY;
|
||||
StartUpCounter++;
|
||||
} while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
|
||||
|
||||
if ((RCC->CTLR & RCC_HSERDY) != RESET)
|
||||
{
|
||||
HSEStatus = (uint32_t)0x01;
|
||||
}
|
||||
else
|
||||
{
|
||||
HSEStatus = (uint32_t)0x00;
|
||||
}
|
||||
|
||||
if (HSEStatus == (uint32_t)0x01)
|
||||
{
|
||||
/* HCLK = SYSCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV1;
|
||||
/* PCLK2 = HCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_PPRE2_DIV1;
|
||||
/* PCLK1 = HCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_PPRE1_DIV2;
|
||||
|
||||
|
||||
/* CH32V20x_D6-PLL configuration: PLLCLK = HSE * 12 = 96 MHz (HSE=8MHZ)
|
||||
* CH32V20x_D8-PLL configuration: PLLCLK = HSE/4 * 12 = 96 MHz (HSE=32MHZ)
|
||||
* CH32V20x_D8W-PLL configuration: PLLCLK = HSE/4 * 12 = 96 MHz (HSE=32MHZ)
|
||||
*/
|
||||
RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_PLLSRC | RCC_PLLXTPRE |
|
||||
RCC_PLLMULL));
|
||||
|
||||
RCC->CFGR0 |= (uint32_t)(RCC_PLLSRC_HSE | RCC_PLLXTPRE_HSE | RCC_PLLMULL12);
|
||||
|
||||
/* Enable PLL */
|
||||
RCC->CTLR |= RCC_PLLON;
|
||||
/* Wait till PLL is ready */
|
||||
while((RCC->CTLR & RCC_PLLRDY) == 0)
|
||||
{
|
||||
}
|
||||
/* Select PLL as system clock source */
|
||||
RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_SW));
|
||||
RCC->CFGR0 |= (uint32_t)RCC_SW_PLL;
|
||||
/* Wait till PLL is used as system clock source */
|
||||
while ((RCC->CFGR0 & (uint32_t)RCC_SWS) != (uint32_t)0x08)
|
||||
{
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/*
|
||||
* If HSE fails to start-up, the application will have wrong clock
|
||||
* configuration. User can add here some code to deal with this error
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#elif defined SYSCLK_FREQ_120MHz_HSE
|
||||
|
||||
/*********************************************************************
|
||||
* @fn SetSysClockTo120_HSE
|
||||
*
|
||||
* @brief Sets System clock frequency to 120MHz and configure HCLK, PCLK2 and PCLK1 prescalers.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
static void SetSysClockTo120_HSE(void)
|
||||
{
|
||||
__IO uint32_t StartUpCounter = 0, HSEStatus = 0;
|
||||
|
||||
RCC->CTLR |= ((uint32_t)RCC_HSEON);
|
||||
|
||||
/* Wait till HSE is ready and if Time out is reached exit */
|
||||
do
|
||||
{
|
||||
HSEStatus = RCC->CTLR & RCC_HSERDY;
|
||||
StartUpCounter++;
|
||||
} while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
|
||||
|
||||
if((RCC->CTLR & RCC_HSERDY) != RESET)
|
||||
{
|
||||
HSEStatus = (uint32_t)0x01;
|
||||
}
|
||||
else
|
||||
{
|
||||
HSEStatus = (uint32_t)0x00;
|
||||
}
|
||||
|
||||
if(HSEStatus == (uint32_t)0x01)
|
||||
{
|
||||
#if defined (CH32V20x_D8W)
|
||||
RCC->CFGR0 |= (uint32_t)(3<<22);
|
||||
/* HCLK = SYSCLK/2 */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV2;
|
||||
#else
|
||||
/* HCLK = SYSCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV1;
|
||||
#endif
|
||||
/* PCLK2 = HCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_PPRE2_DIV1;
|
||||
/* PCLK1 = HCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_PPRE1_DIV2;
|
||||
|
||||
/* CH32V20x_D6-PLL configuration: PLLCLK = HSE * 15 = 120 MHz (HSE=8MHZ)
|
||||
* CH32V20x_D8-PLL configuration: PLLCLK = HSE/4 * 15 = 120 MHz (HSE=32MHZ)
|
||||
* CH32V20x_D8W-PLL configuration: PLLCLK = HSE/2 * 15 = 240 MHz (HSE=32MHZ)
|
||||
*/
|
||||
RCC->CFGR0 &= (uint32_t)((uint32_t) ~(RCC_PLLSRC | RCC_PLLXTPRE |
|
||||
RCC_PLLMULL));
|
||||
|
||||
RCC->CFGR0 |= (uint32_t)(RCC_PLLSRC_HSE | RCC_PLLXTPRE_HSE | RCC_PLLMULL15);
|
||||
|
||||
/* Enable PLL */
|
||||
RCC->CTLR |= RCC_PLLON;
|
||||
/* Wait till PLL is ready */
|
||||
while((RCC->CTLR & RCC_PLLRDY) == 0)
|
||||
{
|
||||
}
|
||||
/* Select PLL as system clock source */
|
||||
RCC->CFGR0 &= (uint32_t)((uint32_t) ~(RCC_SW));
|
||||
RCC->CFGR0 |= (uint32_t)RCC_SW_PLL;
|
||||
/* Wait till PLL is used as system clock source */
|
||||
while((RCC->CFGR0 & (uint32_t)RCC_SWS) != (uint32_t)0x08)
|
||||
{
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/*
|
||||
* If HSE fails to start-up, the application will have wrong clock
|
||||
* configuration. User can add here some code to deal with this error
|
||||
*/
|
||||
}
|
||||
}
|
||||
#elif defined SYSCLK_FREQ_144MHz_HSE
|
||||
|
||||
/*********************************************************************
|
||||
* @fn SetSysClockTo144_HSE
|
||||
*
|
||||
* @brief Sets System clock frequency to 144MHz and configure HCLK, PCLK2 and PCLK1 prescalers.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
static void SetSysClockTo144_HSE(void)
|
||||
{
|
||||
__IO uint32_t StartUpCounter = 0, HSEStatus = 0;
|
||||
|
||||
RCC->CTLR |= ((uint32_t)RCC_HSEON);
|
||||
|
||||
/* Wait till HSE is ready and if Time out is reached exit */
|
||||
do
|
||||
{
|
||||
HSEStatus = RCC->CTLR & RCC_HSERDY;
|
||||
StartUpCounter++;
|
||||
} while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
|
||||
|
||||
if ((RCC->CTLR & RCC_HSERDY) != RESET)
|
||||
{
|
||||
HSEStatus = (uint32_t)0x01;
|
||||
}
|
||||
else
|
||||
{
|
||||
HSEStatus = (uint32_t)0x00;
|
||||
}
|
||||
|
||||
if (HSEStatus == (uint32_t)0x01)
|
||||
{
|
||||
/* HCLK = SYSCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV1;
|
||||
/* PCLK2 = HCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_PPRE2_DIV1;
|
||||
/* PCLK1 = HCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_PPRE1_DIV2;
|
||||
|
||||
|
||||
/* CH32V20x_D6-PLL configuration: PLLCLK = HSE * 18 = 144 MHz (HSE=8MHZ)
|
||||
* CH32V20x_D8-PLL configuration: PLLCLK = HSE/4 * 18 = 144 MHz (HSE=32MHZ)
|
||||
* CH32V20x_D8W-PLL configuration: PLLCLK = HSE/4 * 18 = 144 MHz (HSE=32MHZ)
|
||||
*/
|
||||
RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_PLLSRC | RCC_PLLXTPRE |
|
||||
RCC_PLLMULL));
|
||||
|
||||
RCC->CFGR0 |= (uint32_t)(RCC_PLLSRC_HSE | RCC_PLLXTPRE_HSE | RCC_PLLMULL18);
|
||||
|
||||
/* Enable PLL */
|
||||
RCC->CTLR |= RCC_PLLON;
|
||||
/* Wait till PLL is ready */
|
||||
while((RCC->CTLR & RCC_PLLRDY) == 0)
|
||||
{
|
||||
}
|
||||
/* Select PLL as system clock source */
|
||||
RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_SW));
|
||||
RCC->CFGR0 |= (uint32_t)RCC_SW_PLL;
|
||||
/* Wait till PLL is used as system clock source */
|
||||
while ((RCC->CFGR0 & (uint32_t)RCC_SWS) != (uint32_t)0x08)
|
||||
{
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/*
|
||||
* If HSE fails to start-up, the application will have wrong clock
|
||||
* configuration. User can add here some code to deal with this error
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
||||
#elif defined SYSCLK_FREQ_48MHz_HSI
|
||||
|
||||
/*********************************************************************
|
||||
* @fn SetSysClockTo48_HSI
|
||||
*
|
||||
* @brief Sets System clock frequency to 48MHz and configure HCLK, PCLK2 and PCLK1 prescalers.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
static void SetSysClockTo48_HSI(void)
|
||||
{
|
||||
EXTEN->EXTEN_CTR |= EXTEN_PLL_HSI_PRE;
|
||||
|
||||
/* HCLK = SYSCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV1;
|
||||
/* PCLK2 = HCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_PPRE2_DIV1;
|
||||
/* PCLK1 = HCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_PPRE1_DIV2;
|
||||
|
||||
/* PLL configuration: PLLCLK = HSI * 6 = 48 MHz */
|
||||
RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_PLLSRC | RCC_PLLXTPRE | RCC_PLLMULL));
|
||||
|
||||
RCC->CFGR0 |= (uint32_t)(RCC_PLLSRC_HSI_Div2 | RCC_PLLMULL6);
|
||||
|
||||
/* Enable PLL */
|
||||
RCC->CTLR |= RCC_PLLON;
|
||||
/* Wait till PLL is ready */
|
||||
while((RCC->CTLR & RCC_PLLRDY) == 0)
|
||||
{
|
||||
}
|
||||
/* Select PLL as system clock source */
|
||||
RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_SW));
|
||||
RCC->CFGR0 |= (uint32_t)RCC_SW_PLL;
|
||||
/* Wait till PLL is used as system clock source */
|
||||
while ((RCC->CFGR0 & (uint32_t)RCC_SWS) != (uint32_t)0x08)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
#elif defined SYSCLK_FREQ_56MHz_HSI
|
||||
|
||||
/*********************************************************************
|
||||
* @fn SetSysClockTo56_HSI
|
||||
*
|
||||
* @brief Sets System clock frequency to 56MHz and configure HCLK, PCLK2 and PCLK1 prescalers.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
static void SetSysClockTo56_HSI(void)
|
||||
{
|
||||
EXTEN->EXTEN_CTR |= EXTEN_PLL_HSI_PRE;
|
||||
|
||||
/* HCLK = SYSCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV1;
|
||||
/* PCLK2 = HCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_PPRE2_DIV1;
|
||||
/* PCLK1 = HCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_PPRE1_DIV2;
|
||||
|
||||
/* PLL configuration: PLLCLK = HSI * 7 = 48 MHz */
|
||||
RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_PLLSRC | RCC_PLLXTPRE | RCC_PLLMULL));
|
||||
|
||||
RCC->CFGR0 |= (uint32_t)(RCC_PLLSRC_HSI_Div2 | RCC_PLLMULL7);
|
||||
|
||||
/* Enable PLL */
|
||||
RCC->CTLR |= RCC_PLLON;
|
||||
/* Wait till PLL is ready */
|
||||
while((RCC->CTLR & RCC_PLLRDY) == 0)
|
||||
{
|
||||
}
|
||||
/* Select PLL as system clock source */
|
||||
RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_SW));
|
||||
RCC->CFGR0 |= (uint32_t)RCC_SW_PLL;
|
||||
/* Wait till PLL is used as system clock source */
|
||||
while ((RCC->CFGR0 & (uint32_t)RCC_SWS) != (uint32_t)0x08)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
#elif defined SYSCLK_FREQ_72MHz_HSI
|
||||
|
||||
/*********************************************************************
|
||||
* @fn SetSysClockTo72_HSI
|
||||
*
|
||||
* @brief Sets System clock frequency to 72MHz and configure HCLK, PCLK2 and PCLK1 prescalers.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
static void SetSysClockTo72_HSI(void)
|
||||
{
|
||||
EXTEN->EXTEN_CTR |= EXTEN_PLL_HSI_PRE;
|
||||
|
||||
/* HCLK = SYSCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV1;
|
||||
/* PCLK2 = HCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_PPRE2_DIV1;
|
||||
/* PCLK1 = HCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_PPRE1_DIV2;
|
||||
|
||||
/* PLL configuration: PLLCLK = HSI * 9 = 72 MHz */
|
||||
RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_PLLSRC | RCC_PLLXTPRE | RCC_PLLMULL));
|
||||
|
||||
RCC->CFGR0 |= (uint32_t)(RCC_PLLSRC_HSI_Div2 | RCC_PLLMULL9);
|
||||
|
||||
/* Enable PLL */
|
||||
RCC->CTLR |= RCC_PLLON;
|
||||
/* Wait till PLL is ready */
|
||||
while((RCC->CTLR & RCC_PLLRDY) == 0)
|
||||
{
|
||||
}
|
||||
/* Select PLL as system clock source */
|
||||
RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_SW));
|
||||
RCC->CFGR0 |= (uint32_t)RCC_SW_PLL;
|
||||
/* Wait till PLL is used as system clock source */
|
||||
while ((RCC->CFGR0 & (uint32_t)RCC_SWS) != (uint32_t)0x08)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#elif defined SYSCLK_FREQ_96MHz_HSI
|
||||
|
||||
/*********************************************************************
|
||||
* @fn SetSysClockTo96_HSI
|
||||
*
|
||||
* @brief Sets System clock frequency to 96MHz and configure HCLK, PCLK2 and PCLK1 prescalers.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
static void SetSysClockTo96_HSI(void)
|
||||
{
|
||||
EXTEN->EXTEN_CTR |= EXTEN_PLL_HSI_PRE;
|
||||
|
||||
/* HCLK = SYSCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV1;
|
||||
/* PCLK2 = HCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_PPRE2_DIV1;
|
||||
/* PCLK1 = HCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_PPRE1_DIV2;
|
||||
|
||||
/* PLL configuration: PLLCLK = HSI * 12 = 96 MHz */
|
||||
RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_PLLSRC | RCC_PLLXTPRE | RCC_PLLMULL));
|
||||
|
||||
RCC->CFGR0 |= (uint32_t)(RCC_PLLSRC_HSI_Div2 | RCC_PLLMULL12);
|
||||
|
||||
/* Enable PLL */
|
||||
RCC->CTLR |= RCC_PLLON;
|
||||
/* Wait till PLL is ready */
|
||||
while((RCC->CTLR & RCC_PLLRDY) == 0)
|
||||
{
|
||||
}
|
||||
/* Select PLL as system clock source */
|
||||
RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_SW));
|
||||
RCC->CFGR0 |= (uint32_t)RCC_SW_PLL;
|
||||
/* Wait till PLL is used as system clock source */
|
||||
while ((RCC->CFGR0 & (uint32_t)RCC_SWS) != (uint32_t)0x08)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#elif defined SYSCLK_FREQ_120MHz_HSI
|
||||
|
||||
/*********************************************************************
|
||||
* @fn SetSysClockTo120_HSI
|
||||
*
|
||||
* @brief Sets System clock frequency to 120MHz and configure HCLK, PCLK2 and PCLK1 prescalers.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
static void SetSysClockTo120_HSI(void)
|
||||
{
|
||||
EXTEN->EXTEN_CTR |= EXTEN_PLL_HSI_PRE;
|
||||
|
||||
/* HCLK = SYSCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV1;
|
||||
/* PCLK2 = HCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_PPRE2_DIV1;
|
||||
/* PCLK1 = HCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_PPRE1_DIV2;
|
||||
|
||||
/* PLL configuration: PLLCLK = HSI * 15 = 120 MHz */
|
||||
RCC->CFGR0 &= (uint32_t)((uint32_t) ~(RCC_PLLSRC | RCC_PLLXTPRE |
|
||||
RCC_PLLMULL));
|
||||
|
||||
RCC->CFGR0 |= (uint32_t)(RCC_PLLSRC_HSI_Div2 | RCC_PLLMULL15);
|
||||
|
||||
/* Enable PLL */
|
||||
RCC->CTLR |= RCC_PLLON;
|
||||
/* Wait till PLL is ready */
|
||||
while((RCC->CTLR & RCC_PLLRDY) == 0)
|
||||
{
|
||||
}
|
||||
/* Select PLL as system clock source */
|
||||
RCC->CFGR0 &= (uint32_t)((uint32_t) ~(RCC_SW));
|
||||
RCC->CFGR0 |= (uint32_t)RCC_SW_PLL;
|
||||
/* Wait till PLL is used as system clock source */
|
||||
while((RCC->CFGR0 & (uint32_t)RCC_SWS) != (uint32_t)0x08)
|
||||
{
|
||||
}
|
||||
}
|
||||
#elif defined SYSCLK_FREQ_144MHz_HSI
|
||||
|
||||
/*********************************************************************
|
||||
* @fn SetSysClockTo144_HSI
|
||||
*
|
||||
* @brief Sets System clock frequency to 144MHz and configure HCLK, PCLK2 and PCLK1 prescalers.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
static void SetSysClockTo144_HSI(void)
|
||||
{
|
||||
EXTEN->EXTEN_CTR |= EXTEN_PLL_HSI_PRE;
|
||||
|
||||
/* HCLK = SYSCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV1;
|
||||
/* PCLK2 = HCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_PPRE2_DIV1;
|
||||
/* PCLK1 = HCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_PPRE1_DIV2;
|
||||
|
||||
/* PLL configuration: PLLCLK = HSI * 18 = 144 MHz */
|
||||
RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_PLLSRC | RCC_PLLXTPRE | RCC_PLLMULL));
|
||||
|
||||
RCC->CFGR0 |= (uint32_t)(RCC_PLLSRC_HSI_Div2 | RCC_PLLMULL18);
|
||||
|
||||
/* Enable PLL */
|
||||
RCC->CTLR |= RCC_PLLON;
|
||||
/* Wait till PLL is ready */
|
||||
while((RCC->CTLR & RCC_PLLRDY) == 0)
|
||||
{
|
||||
}
|
||||
/* Select PLL as system clock source */
|
||||
RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_SW));
|
||||
RCC->CFGR0 |= (uint32_t)RCC_SW_PLL;
|
||||
/* Wait till PLL is used as system clock source */
|
||||
while ((RCC->CFGR0 & (uint32_t)RCC_SWS) != (uint32_t)0x08)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
199
vd960DBN/BLE/OnlyUpdateApp_IAP/Ld/Link.ld
Normal file
199
vd960DBN/BLE/OnlyUpdateApp_IAP/Ld/Link.ld
Normal file
@@ -0,0 +1,199 @@
|
||||
ENTRY( _start )
|
||||
|
||||
__stack_size = 2048;
|
||||
|
||||
PROVIDE( _stack_size = __stack_size );
|
||||
|
||||
|
||||
MEMORY
|
||||
{
|
||||
/* CH32V20x_D6 - CH32V203F6-CH32V203G6-CH32V203C6 */
|
||||
/*
|
||||
FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 32K
|
||||
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 10K
|
||||
*/
|
||||
|
||||
/* CH32V20x_D6 - CH32V203K8-CH32V203C8-CH32V203G8-CH32V203F8 */
|
||||
/*
|
||||
FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 64K
|
||||
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 20K
|
||||
*/
|
||||
|
||||
/* CH32V20x_D8 - CH32V203RB
|
||||
CH32V20x_D8W - CH32V208x
|
||||
FLASH + RAM supports the following configuration
|
||||
FLASH-128K + RAM-64K
|
||||
FLASH-144K + RAM-48K
|
||||
FLASH-160K + RAM-32K
|
||||
*/
|
||||
FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 16K
|
||||
RAM (xrw) : ORIGIN = 0x20004000, LENGTH = 48K
|
||||
}
|
||||
|
||||
PROVIDE( __global_pointer$ = 0x20004000 );
|
||||
|
||||
SECTIONS
|
||||
{
|
||||
|
||||
.init :
|
||||
{
|
||||
_sinit = .;
|
||||
. = ALIGN(4);
|
||||
KEEP(*(SORT_NONE(.init)))
|
||||
. = ALIGN(4);
|
||||
_einit = .;
|
||||
} >FLASH AT>FLASH
|
||||
|
||||
.vector :
|
||||
{
|
||||
*(.vector);
|
||||
. = ALIGN(64);
|
||||
KEEP(*(SORT_NONE(.handle_reset)))
|
||||
} >FLASH AT>FLASH
|
||||
|
||||
.highcode :
|
||||
{
|
||||
. = ALIGN(4);
|
||||
*(.highcode);
|
||||
*(.highcode.*);
|
||||
. = ALIGN(4);
|
||||
} >FLASH AT>FLASH
|
||||
|
||||
.text :
|
||||
{
|
||||
. = ALIGN(4);
|
||||
*(.text)
|
||||
*(.text.*)
|
||||
*(.rodata)
|
||||
*(.rodata*)
|
||||
*(.sdata2.*)
|
||||
*(.gnu.linkonce.t.*)
|
||||
. = ALIGN(4);
|
||||
} >FLASH AT>FLASH
|
||||
|
||||
.fini :
|
||||
{
|
||||
KEEP(*(SORT_NONE(.fini)))
|
||||
. = ALIGN(4);
|
||||
} >FLASH AT>FLASH
|
||||
|
||||
PROVIDE( _etext = . );
|
||||
PROVIDE( _eitcm = . );
|
||||
|
||||
.preinit_array :
|
||||
{
|
||||
PROVIDE_HIDDEN (__preinit_array_start = .);
|
||||
KEEP (*(.preinit_array))
|
||||
PROVIDE_HIDDEN (__preinit_array_end = .);
|
||||
} >FLASH AT>FLASH
|
||||
|
||||
.init_array :
|
||||
{
|
||||
PROVIDE_HIDDEN (__init_array_start = .);
|
||||
KEEP (*(SORT_BY_INIT_PRIORITY(.init_array.*) SORT_BY_INIT_PRIORITY(.ctors.*)))
|
||||
KEEP (*(.init_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .ctors))
|
||||
PROVIDE_HIDDEN (__init_array_end = .);
|
||||
} >FLASH AT>FLASH
|
||||
|
||||
.fini_array :
|
||||
{
|
||||
PROVIDE_HIDDEN (__fini_array_start = .);
|
||||
KEEP (*(SORT_BY_INIT_PRIORITY(.fini_array.*) SORT_BY_INIT_PRIORITY(.dtors.*)))
|
||||
KEEP (*(.fini_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .dtors))
|
||||
PROVIDE_HIDDEN (__fini_array_end = .);
|
||||
} >FLASH AT>FLASH
|
||||
|
||||
.ctors :
|
||||
{
|
||||
/* gcc uses crtbegin.o to find the start of
|
||||
the constructors, so we make sure it is
|
||||
first. Because this is a wildcard, it
|
||||
doesn't matter if the user does not
|
||||
actually link against crtbegin.o; the
|
||||
linker won't look for a file to match a
|
||||
wildcard. The wildcard also means that it
|
||||
doesn't matter which directory crtbegin.o
|
||||
is in. */
|
||||
KEEP (*crtbegin.o(.ctors))
|
||||
KEEP (*crtbegin?.o(.ctors))
|
||||
/* We don't want to include the .ctor section from
|
||||
the crtend.o file until after the sorted ctors.
|
||||
The .ctor section from the crtend file contains the
|
||||
end of ctors marker and it must be last */
|
||||
KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .ctors))
|
||||
KEEP (*(SORT(.ctors.*)))
|
||||
KEEP (*(.ctors))
|
||||
} >FLASH AT>FLASH
|
||||
|
||||
.dtors :
|
||||
{
|
||||
KEEP (*crtbegin.o(.dtors))
|
||||
KEEP (*crtbegin?.o(.dtors))
|
||||
KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .dtors))
|
||||
KEEP (*(SORT(.dtors.*)))
|
||||
KEEP (*(.dtors))
|
||||
} >FLASH AT>FLASH
|
||||
|
||||
.dalign :
|
||||
{
|
||||
. = ALIGN(4);
|
||||
PROVIDE(_data_vma = .);
|
||||
} >RAM AT>FLASH
|
||||
|
||||
.dlalign :
|
||||
{
|
||||
. = ALIGN(4);
|
||||
PROVIDE(_data_lma = .);
|
||||
} >FLASH AT>FLASH
|
||||
|
||||
.data :
|
||||
{
|
||||
*(.gnu.linkonce.r.*)
|
||||
*(.data .data.*)
|
||||
*(.gnu.linkonce.d.*)
|
||||
. = ALIGN(8);
|
||||
PROVIDE( __global_pointer$ = . + 0x800 );
|
||||
*(.sdata .sdata.*)
|
||||
*(.gnu.linkonce.s.*)
|
||||
. = ALIGN(8);
|
||||
*(.srodata.cst16)
|
||||
*(.srodata.cst8)
|
||||
*(.srodata.cst4)
|
||||
*(.srodata.cst2)
|
||||
*(.srodata .srodata.*)
|
||||
. = ALIGN(4);
|
||||
PROVIDE( _edata = .);
|
||||
} >RAM AT>FLASH
|
||||
|
||||
.bss :
|
||||
{
|
||||
. = ALIGN(4);
|
||||
PROVIDE( _sbss = .);
|
||||
*(.sbss*)
|
||||
*(.gnu.linkonce.sb.*)
|
||||
*(.bss*)
|
||||
*(.gnu.linkonce.b.*)
|
||||
*(COMMON*)
|
||||
. = ALIGN(4);
|
||||
PROVIDE( _ebss = .);
|
||||
} >RAM AT>FLASH
|
||||
|
||||
PROVIDE( _end = _ebss);
|
||||
PROVIDE( end = . );
|
||||
|
||||
/*.stack ORIGIN(RAM) + LENGTH(RAM) - __stack_size :
|
||||
{
|
||||
PROVIDE( _heap_end = . );
|
||||
. = ALIGN(4);
|
||||
PROVIDE(_susrstack = . );
|
||||
. = . + __stack_size;
|
||||
PROVIDE( _eusrstack = .);
|
||||
} >RAM */
|
||||
|
||||
.stack ORIGIN(RAM)+LENGTH(RAM) :
|
||||
{
|
||||
PROVIDE( _heap_end = . );
|
||||
. = ALIGN(4);
|
||||
PROVIDE(_eusrstack = . );
|
||||
} >RAM
|
||||
}
|
||||
334
vd960DBN/BLE/OnlyUpdateApp_IAP/Profile/OTAprofile.c
Normal file
334
vd960DBN/BLE/OnlyUpdateApp_IAP/Profile/OTAprofile.c
Normal file
@@ -0,0 +1,334 @@
|
||||
/********************************** (C) COPYRIGHT *******************************
|
||||
* File Name : OTAprofile.C
|
||||
* Author : WCH
|
||||
* Version : V1.0
|
||||
* Date : 2018/12/10
|
||||
* Description : OTA upgrade Bluetooth communication interface
|
||||
*********************************************************************************
|
||||
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
|
||||
* Attention: This software (modified or not) and binary are used for
|
||||
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
|
||||
*******************************************************************************/
|
||||
|
||||
/*********************************************************************
|
||||
* INCLUDES
|
||||
*/
|
||||
#include "CONFIG.h"
|
||||
#include "OTAprofile.h"
|
||||
#include "debug.h"
|
||||
#include "ota.h"
|
||||
|
||||
/*********************************************************************
|
||||
* MACROS
|
||||
*/
|
||||
|
||||
/*********************************************************************
|
||||
* CONSTANTS
|
||||
*/
|
||||
|
||||
/*********************************************************************
|
||||
* TYPEDEFS
|
||||
*/
|
||||
|
||||
/*********************************************************************
|
||||
* GLOBAL VARIABLES
|
||||
*/
|
||||
// Simple GATT Profile Service UUID: 0xFFF0
|
||||
const uint8_t OTAProfileServUUID[ATT_BT_UUID_SIZE] = {
|
||||
LO_UINT16(OTAPROFILE_SERV_UUID), HI_UINT16(OTAPROFILE_SERV_UUID)};
|
||||
|
||||
// Characteristic 1 UUID: 0xFFF1
|
||||
const uint8_t OTAProfilechar1UUID[ATT_BT_UUID_SIZE] = {
|
||||
LO_UINT16(OTAPROFILE_CHAR_UUID), HI_UINT16(OTAPROFILE_CHAR_UUID)};
|
||||
|
||||
/*********************************************************************
|
||||
* EXTERNAL VARIABLES
|
||||
*/
|
||||
|
||||
/*********************************************************************
|
||||
* EXTERNAL FUNCTIONS
|
||||
*/
|
||||
|
||||
/*********************************************************************
|
||||
* LOCAL VARIABLES
|
||||
*/
|
||||
|
||||
static OTAProfileCBs_t *OTAProfile_AppCBs = NULL;
|
||||
|
||||
/*********************************************************************
|
||||
* Profile Attributes - variables
|
||||
*/
|
||||
|
||||
// Simple Profile Service attribute
|
||||
static const gattAttrType_t OTAProfileService = {ATT_BT_UUID_SIZE, OTAProfileServUUID};
|
||||
|
||||
// Simple Profile Characteristic 1 Properties
|
||||
static uint8_t OTAProfileCharProps = GATT_PROP_READ | GATT_PROP_WRITE | GATT_PROP_WRITE_NO_RSP;
|
||||
|
||||
// Characteristic 1 Value
|
||||
static uint8_t OTAProfileChar = 0;
|
||||
|
||||
// Simple Profile Characteristic 1 User Description
|
||||
static uint8_t OTAProfileCharUserDesp[12] = "OTA Channel";
|
||||
|
||||
// write and read buffer
|
||||
static uint8_t OTAProfileReadLen;
|
||||
static uint8_t OTAProfileReadBuf[IAP_LEN];
|
||||
static uint8_t OTAProfileWriteLen;
|
||||
static uint8_t OTAProfileWriteBuf[IAP_LEN];
|
||||
|
||||
/*********************************************************************
|
||||
* Profile Attributes - Table
|
||||
*/
|
||||
|
||||
static gattAttribute_t OTAProfileAttrTbl[4] = {
|
||||
// Simple Profile Service
|
||||
{
|
||||
{ATT_BT_UUID_SIZE, primaryServiceUUID}, /* type */
|
||||
GATT_PERMIT_READ, /* permissions */
|
||||
0, /* handle */
|
||||
(uint8_t *)&OTAProfileService /* pValue */
|
||||
},
|
||||
|
||||
// Characteristic Declaration
|
||||
{
|
||||
{ATT_BT_UUID_SIZE, characterUUID},
|
||||
GATT_PERMIT_READ,
|
||||
0,
|
||||
&OTAProfileCharProps},
|
||||
|
||||
// Characteristic Value
|
||||
{
|
||||
{ATT_BT_UUID_SIZE, OTAProfilechar1UUID},
|
||||
GATT_PERMIT_READ | GATT_PERMIT_WRITE,
|
||||
0,
|
||||
&OTAProfileChar},
|
||||
|
||||
// Characteristic User Description
|
||||
{
|
||||
{ATT_BT_UUID_SIZE, charUserDescUUID},
|
||||
GATT_PERMIT_READ,
|
||||
0,
|
||||
OTAProfileCharUserDesp},
|
||||
};
|
||||
|
||||
/*********************************************************************
|
||||
* LOCAL FUNCTIONS
|
||||
*/
|
||||
static bStatus_t OTAProfile_ReadAttrCB(uint16_t connHandle, gattAttribute_t *pAttr,
|
||||
uint8_t *pValue, uint16_t *pLen, uint16_t offset, uint16_t maxLen, uint8_t method);
|
||||
static bStatus_t OTAProfile_WriteAttrCB(uint16_t connHandle, gattAttribute_t *pAttr,
|
||||
uint8_t *pValue, uint16_t len, uint16_t offset, uint8_t method);
|
||||
|
||||
/*********************************************************************
|
||||
* PROFILE CALLBACKS
|
||||
*/
|
||||
// OTA Profile Service Callbacks
|
||||
gattServiceCBs_t OTAProfileCBs = {
|
||||
OTAProfile_ReadAttrCB, // Read callback function pointer
|
||||
OTAProfile_WriteAttrCB, // Write callback function pointer
|
||||
NULL // Authorization callback function pointer
|
||||
};
|
||||
|
||||
/*********************************************************************
|
||||
* PUBLIC FUNCTIONS
|
||||
*/
|
||||
|
||||
/*********************************************************************
|
||||
* @fn OTAProfile_AddService
|
||||
*
|
||||
* @brief OTA Profile initialization
|
||||
*
|
||||
* @param services - Service control
|
||||
*
|
||||
* @return Initialization state
|
||||
*/
|
||||
bStatus_t OTAProfile_AddService(uint32_t services)
|
||||
{
|
||||
uint8_t status = SUCCESS;
|
||||
|
||||
if(services & OTAPROFILE_SERVICE)
|
||||
{
|
||||
// Register GATT attribute list and CBs with GATT Server App
|
||||
status = GATTServApp_RegisterService(OTAProfileAttrTbl,
|
||||
GATT_NUM_ATTRS(OTAProfileAttrTbl),
|
||||
GATT_MAX_ENCRYPT_KEY_SIZE,
|
||||
&OTAProfileCBs);
|
||||
}
|
||||
|
||||
return (status);
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn OTAProfile_RegisterAppCBs
|
||||
*
|
||||
* @brief OTA Profile read and write recovery function registration
|
||||
*
|
||||
* @param appCallbacks - Function structure pointer
|
||||
*
|
||||
* @return Function execution status
|
||||
*/
|
||||
bStatus_t OTAProfile_RegisterAppCBs(OTAProfileCBs_t *appCallbacks)
|
||||
{
|
||||
if(appCallbacks)
|
||||
{
|
||||
OTAProfile_AppCBs = appCallbacks;
|
||||
|
||||
return (SUCCESS);
|
||||
}
|
||||
else
|
||||
{
|
||||
return (bleAlreadyInRequestedMode);
|
||||
}
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn OTAProfile_ReadAttrCB
|
||||
*
|
||||
* @brief Read an attribute.
|
||||
*
|
||||
* @param connHandle - connection message was received on
|
||||
* @param pAttr - pointer to attribute
|
||||
* @param pValue - pointer to data to be read
|
||||
* @param pLen - length of data to be read
|
||||
* @param offset - offset of the first octet to be read
|
||||
* @param maxLen - maximum length of data to be read
|
||||
*
|
||||
* @return Success or Failure
|
||||
*/
|
||||
static bStatus_t OTAProfile_ReadAttrCB(uint16_t connHandle, gattAttribute_t *pAttr,
|
||||
uint8_t *pValue, uint16_t *pLen, uint16_t offset, uint16_t maxLen, uint8_t method)
|
||||
{
|
||||
bStatus_t status = SUCCESS;
|
||||
|
||||
if(pAttr->type.len == ATT_BT_UUID_SIZE)
|
||||
{
|
||||
// 16-bit UUID
|
||||
uint16_t uuid = BUILD_UINT16(pAttr->type.uuid[0], pAttr->type.uuid[1]);
|
||||
|
||||
switch(uuid)
|
||||
{
|
||||
case OTAPROFILE_CHAR_UUID:
|
||||
{
|
||||
*pLen = 0;
|
||||
if(OTAProfileReadLen)
|
||||
{
|
||||
*pLen = OTAProfileReadLen;
|
||||
tmos_memcpy(pValue, OTAProfileReadBuf, OTAProfileReadLen);
|
||||
OTAProfileReadLen = 0;
|
||||
if(OTAProfile_AppCBs && OTAProfile_AppCBs->pfnOTAProfileRead)
|
||||
{
|
||||
OTAProfile_AppCBs->pfnOTAProfileRead(OTAPROFILE_CHAR);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
// Should never get here! (characteristics 3 and 4 do not have read permissions)
|
||||
*pLen = 0;
|
||||
status = ATT_ERR_ATTR_NOT_FOUND;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// 128-bit UUID
|
||||
*pLen = 0;
|
||||
status = ATT_ERR_INVALID_HANDLE;
|
||||
}
|
||||
|
||||
return (status);
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn OTAProfile_WriteAttrCB
|
||||
*
|
||||
* @brief Validate attribute data prior to a write operation
|
||||
*
|
||||
* @param connHandle - connection message was received on
|
||||
* @param pAttr - pointer to attribute
|
||||
* @param pValue - pointer to data to be written
|
||||
* @param len - length of data
|
||||
* @param offset - offset of the first octet to be written
|
||||
*
|
||||
* @return Success or Failure
|
||||
*/
|
||||
static bStatus_t OTAProfile_WriteAttrCB(uint16_t connHandle, gattAttribute_t *pAttr,
|
||||
uint8_t *pValue, uint16_t len, uint16_t offset, uint8_t method)
|
||||
{
|
||||
bStatus_t status = SUCCESS;
|
||||
//uint8_t notifyApp = 0xFF;
|
||||
|
||||
if(pAttr->type.len == ATT_BT_UUID_SIZE)
|
||||
{
|
||||
// 16-bit UUID
|
||||
uint16_t uuid = BUILD_UINT16(pAttr->type.uuid[0], pAttr->type.uuid[1]);
|
||||
|
||||
switch(uuid)
|
||||
{
|
||||
case OTAPROFILE_CHAR_UUID:
|
||||
{
|
||||
//Write the value
|
||||
if(status == SUCCESS)
|
||||
{
|
||||
uint16_t i;
|
||||
uint8_t *p_rec_buf;
|
||||
|
||||
OTAProfileWriteLen = len;
|
||||
p_rec_buf = pValue;
|
||||
for(i = 0; i < OTAProfileWriteLen; i++)
|
||||
OTAProfileWriteBuf[i] = p_rec_buf[i];
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
// Should never get here! (characteristics 2 and 4 do not have write permissions)
|
||||
status = ATT_ERR_ATTR_NOT_FOUND;
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// 128-bit UUID
|
||||
status = ATT_ERR_INVALID_HANDLE;
|
||||
}
|
||||
|
||||
if(OTAProfileWriteLen && OTAProfile_AppCBs && OTAProfile_AppCBs->pfnOTAProfileWrite)
|
||||
{
|
||||
OTAProfile_AppCBs->pfnOTAProfileWrite(OTAPROFILE_CHAR, OTAProfileWriteBuf, OTAProfileWriteLen);
|
||||
OTAProfileWriteLen = 0;
|
||||
}
|
||||
|
||||
return (status);
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn OTAProfile_SendData
|
||||
*
|
||||
* @brief OTA Profile channel send data
|
||||
*
|
||||
* @param paramID - OTA channel selection
|
||||
* @param p_data - Data pointer
|
||||
* @param send_len - Send data length
|
||||
*
|
||||
* @return Function execution status
|
||||
*/
|
||||
bStatus_t OTAProfile_SendData(unsigned char paramID, unsigned char *p_data, unsigned char send_len)
|
||||
{
|
||||
bStatus_t status = SUCCESS;
|
||||
|
||||
/* Data length exceeds range */
|
||||
if(send_len > 20)
|
||||
return 0xfe;
|
||||
|
||||
OTAProfileReadLen = send_len;
|
||||
tmos_memcpy(OTAProfileReadBuf, p_data, OTAProfileReadLen);
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
*********************************************************************/
|
||||
102
vd960DBN/BLE/OnlyUpdateApp_IAP/Profile/include/OTAprofile.h
Normal file
102
vd960DBN/BLE/OnlyUpdateApp_IAP/Profile/include/OTAprofile.h
Normal file
@@ -0,0 +1,102 @@
|
||||
/********************************** (C) COPYRIGHT *******************************
|
||||
* File Name : OTAprofile.h
|
||||
* Author : WCH
|
||||
* Version : V1.0
|
||||
* Date : 2018/12/11
|
||||
* Description :
|
||||
*********************************************************************************
|
||||
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
|
||||
* Attention: This software (modified or not) and binary are used for
|
||||
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
|
||||
*******************************************************************************/
|
||||
|
||||
#ifndef OTAPROFILE_H
|
||||
#define OTAPROFILE_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*********************************************************************
|
||||
* INCLUDES
|
||||
*/
|
||||
|
||||
/*********************************************************************
|
||||
* CONSTANTS
|
||||
*/
|
||||
|
||||
// OTA Profile通道Index定义
|
||||
#define OTAPROFILE_CHAR 0
|
||||
|
||||
// OTA 服务的UUID定义
|
||||
#define OTAPROFILE_SERV_UUID 0xFEE0
|
||||
|
||||
// OTA 通讯通道UUID定义
|
||||
#define OTAPROFILE_CHAR_UUID 0xFEE1
|
||||
|
||||
// Simple Keys Profile Services bit fields
|
||||
#define OTAPROFILE_SERVICE 0x00000001
|
||||
|
||||
/*********************************************************************
|
||||
* TYPEDEFS
|
||||
*/
|
||||
|
||||
/*********************************************************************
|
||||
* MACROS
|
||||
*/
|
||||
|
||||
/*********************************************************************
|
||||
* Profile Callbacks
|
||||
*/
|
||||
|
||||
// 读写操作函数回调
|
||||
typedef void (*OTAProfileRead_t)(unsigned char paramID);
|
||||
typedef void (*OTAProfileWrite_t)(unsigned char paramID, unsigned char *p_data, unsigned char w_len);
|
||||
|
||||
typedef struct
|
||||
{
|
||||
OTAProfileRead_t pfnOTAProfileRead;
|
||||
OTAProfileWrite_t pfnOTAProfileWrite;
|
||||
} OTAProfileCBs_t;
|
||||
|
||||
/*********************************************************************
|
||||
* API FUNCTIONS
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief OTA Profile初始化
|
||||
*
|
||||
* @param services - 服务控制字
|
||||
*
|
||||
* @return 初始化的状态
|
||||
*/
|
||||
bStatus_t OTAProfile_AddService(uint32_t services);
|
||||
|
||||
/**
|
||||
* @brief OTA Profile读写回调函数注册
|
||||
*
|
||||
* @param appCallbacks - 函数结构体指针
|
||||
*
|
||||
* @return 函数执行状态
|
||||
*/
|
||||
bStatus_t OTAProfile_RegisterAppCBs(OTAProfileCBs_t *appCallbacks);
|
||||
|
||||
/**
|
||||
* @brief OTA Profile通道发送数据
|
||||
*
|
||||
* @param paramID - OTA通道选择
|
||||
* @param p_data - 数据指针
|
||||
* @param send_len - 发送数据长度
|
||||
*
|
||||
* @return 函数执行状态
|
||||
*/
|
||||
bStatus_t OTAProfile_SendData(unsigned char paramID, unsigned char *p_data, unsigned char send_len);
|
||||
|
||||
/*********************************************************************
|
||||
*********************************************************************/
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
123
vd960DBN/BLE/OnlyUpdateApp_IAP/Profile/include/gattprofile.h
Normal file
123
vd960DBN/BLE/OnlyUpdateApp_IAP/Profile/include/gattprofile.h
Normal file
@@ -0,0 +1,123 @@
|
||||
/********************************** (C) COPYRIGHT *******************************
|
||||
* File Name : gattprofile.h
|
||||
* Author : WCH
|
||||
* Version : V1.0
|
||||
* Date : 2018/12/11
|
||||
* Description :
|
||||
*********************************************************************************
|
||||
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
|
||||
* Attention: This software (modified or not) and binary are used for
|
||||
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
|
||||
*******************************************************************************/
|
||||
|
||||
#ifndef GATTPROFILE_H
|
||||
#define GATTPROFILE_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*********************************************************************
|
||||
* INCLUDES
|
||||
*/
|
||||
|
||||
/*********************************************************************
|
||||
* CONSTANTS
|
||||
*/
|
||||
|
||||
// Profile Parameters
|
||||
#define SIMPLEPROFILE_CHAR1 0 // RW uint8_t - Profile Characteristic 1 value
|
||||
#define SIMPLEPROFILE_CHAR2 1 // RW uint8_t - Profile Characteristic 2 value
|
||||
#define SIMPLEPROFILE_CHAR3 2 // RW uint8_t - Profile Characteristic 3 value
|
||||
#define SIMPLEPROFILE_CHAR4 3 // RW uint8_t - Profile Characteristic 4 value
|
||||
#define SIMPLEPROFILE_CHAR5 4 // RW uint8_t - Profile Characteristic 4 value
|
||||
|
||||
// Simple Profile Service UUID
|
||||
#define SIMPLEPROFILE_SERV_UUID 0xFFE0
|
||||
|
||||
// Key Pressed UUID
|
||||
#define SIMPLEPROFILE_CHAR1_UUID 0xFFE1
|
||||
#define SIMPLEPROFILE_CHAR2_UUID 0xFFE2
|
||||
#define SIMPLEPROFILE_CHAR3_UUID 0xFFE3
|
||||
#define SIMPLEPROFILE_CHAR4_UUID 0xFFE4
|
||||
#define SIMPLEPROFILE_CHAR5_UUID 0xFFE5
|
||||
|
||||
// Simple Keys Profile Services bit fields
|
||||
#define SIMPLEPROFILE_SERVICE 0x00000001
|
||||
|
||||
// Length of Characteristic 5 in bytes
|
||||
#define SIMPLEPROFILE_CHAR4_LEN 8
|
||||
#define SIMPLEPROFILE_CHAR5_LEN 5
|
||||
|
||||
/*********************************************************************
|
||||
* TYPEDEFS
|
||||
*/
|
||||
|
||||
/*********************************************************************
|
||||
* MACROS
|
||||
*/
|
||||
|
||||
/*********************************************************************
|
||||
* Profile Callbacks
|
||||
*/
|
||||
|
||||
// Callback when a characteristic value has changed
|
||||
typedef void (*simpleProfileChange_t)(uint8_t paramID);
|
||||
|
||||
typedef struct
|
||||
{
|
||||
simpleProfileChange_t pfnSimpleProfileChange; // Called when characteristic value changes
|
||||
} simpleProfileCBs_t;
|
||||
|
||||
/*********************************************************************
|
||||
* API FUNCTIONS
|
||||
*/
|
||||
|
||||
/*
|
||||
* SimpleProfile_AddService- Initializes the Simple GATT Profile service by registering
|
||||
* GATT attributes with the GATT server.
|
||||
*
|
||||
* @param services - services to add. This is a bit map and can
|
||||
* contain more than one service.
|
||||
*/
|
||||
extern bStatus_t SimpleProfile_AddService(uint32_t services);
|
||||
|
||||
/*
|
||||
* SimpleProfile_RegisterAppCBs - Registers the application callback function.
|
||||
* Only call this function once.
|
||||
*
|
||||
* appCallbacks - pointer to application callbacks.
|
||||
*/
|
||||
extern bStatus_t SimpleProfile_RegisterAppCBs(simpleProfileCBs_t *appCallbacks);
|
||||
|
||||
/*
|
||||
* SimpleProfile_SetParameter - Set a Simple GATT Profile parameter.
|
||||
*
|
||||
* param - Profile parameter ID
|
||||
* len - length of data to right
|
||||
* value - pointer to data to write. This is dependent on
|
||||
* the parameter ID and WILL be cast to the appropriate
|
||||
* data type (example: data type of uint16_t will be cast to
|
||||
* uint16_t pointer).
|
||||
*/
|
||||
extern bStatus_t SimpleProfile_SetParameter(uint8_t param, uint16_t len, void *value);
|
||||
|
||||
/*
|
||||
* SimpleProfile_GetParameter - Get a Simple GATT Profile parameter.
|
||||
*
|
||||
* param - Profile parameter ID
|
||||
* value - pointer to data to write. This is dependent on
|
||||
* the parameter ID and WILL be cast to the appropriate
|
||||
* data type (example: data type of uint16_t will be cast to
|
||||
* uint16_t pointer).
|
||||
*/
|
||||
extern bStatus_t SimpleProfile_GetParameter(uint8_t param, void *value);
|
||||
|
||||
/*********************************************************************
|
||||
*********************************************************************/
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
289
vd960DBN/BLE/OnlyUpdateApp_IAP/Startup/startup_ch32v20x_D8W.S
Normal file
289
vd960DBN/BLE/OnlyUpdateApp_IAP/Startup/startup_ch32v20x_D8W.S
Normal file
@@ -0,0 +1,289 @@
|
||||
;/********************************** (C) COPYRIGHT *******************************
|
||||
;* File Name : startup_ch32v20x_D8W.s
|
||||
;* Author : WCH
|
||||
;* Version : V1.0.0
|
||||
;* Date : 2021/06/06
|
||||
;* Description : CH32V208x
|
||||
;* vector table for eclipse toolchain.
|
||||
;*********************************************************************************
|
||||
;* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
|
||||
;* Attention: This software (modified or not) and binary are used for
|
||||
;* microcontroller manufactured by Nanjing Qinheng Microelectronics.
|
||||
*******************************************************************************/
|
||||
|
||||
.section .init,"ax",@progbits
|
||||
.global _start
|
||||
.align 1
|
||||
_start:
|
||||
j handle_reset
|
||||
.word 0x00000013
|
||||
.word 0x00000013
|
||||
.word 0x00000013
|
||||
.word 0x00000013
|
||||
.word 0x00000013
|
||||
.word 0x00000013
|
||||
.word 0x00000013
|
||||
.word 0x00000013
|
||||
.word 0x00000013
|
||||
.word 0x00000013
|
||||
.word 0x00000013
|
||||
.word 0x00000013
|
||||
.word 0x00100073
|
||||
.section .vector,"ax",@progbits
|
||||
.align 1
|
||||
_vector_base:
|
||||
.option norvc;
|
||||
.word _start
|
||||
.word 0
|
||||
.word NMI_Handler /* NMI */
|
||||
.word HardFault_Handler /* Hard Fault */
|
||||
.word 0
|
||||
.word Ecall_M_Mode_Handler /* Ecall M Mode */
|
||||
.word 0
|
||||
.word 0
|
||||
.word Ecall_U_Mode_Handler /* Ecall U Mode */
|
||||
.word Break_Point_Handler /* Break Point */
|
||||
.word 0
|
||||
.word 0
|
||||
.word SysTick_Handler /* SysTick */
|
||||
.word 0
|
||||
.word SW_Handler /* SW */
|
||||
.word 0
|
||||
/* External Interrupts */
|
||||
.word WWDG_IRQHandler /* Window Watchdog */
|
||||
.word PVD_IRQHandler /* PVD through EXTI Line detect */
|
||||
.word TAMPER_IRQHandler /* TAMPER */
|
||||
.word RTC_IRQHandler /* RTC */
|
||||
.word FLASH_IRQHandler /* Flash */
|
||||
.word RCC_IRQHandler /* RCC */
|
||||
.word EXTI0_IRQHandler /* EXTI Line 0 */
|
||||
.word EXTI1_IRQHandler /* EXTI Line 1 */
|
||||
.word EXTI2_IRQHandler /* EXTI Line 2 */
|
||||
.word EXTI3_IRQHandler /* EXTI Line 3 */
|
||||
.word EXTI4_IRQHandler /* EXTI Line 4 */
|
||||
.word DMA1_Channel1_IRQHandler /* DMA1 Channel 1 */
|
||||
.word DMA1_Channel2_IRQHandler /* DMA1 Channel 2 */
|
||||
.word DMA1_Channel3_IRQHandler /* DMA1 Channel 3 */
|
||||
.word DMA1_Channel4_IRQHandler /* DMA1 Channel 4 */
|
||||
.word DMA1_Channel5_IRQHandler /* DMA1 Channel 5 */
|
||||
.word DMA1_Channel6_IRQHandler /* DMA1 Channel 6 */
|
||||
.word DMA1_Channel7_IRQHandler /* DMA1 Channel 7 */
|
||||
.word ADC1_2_IRQHandler /* ADC1_2 */
|
||||
.word USB_HP_CAN1_TX_IRQHandler /* USB HP and CAN1 TX */
|
||||
.word USB_LP_CAN1_RX0_IRQHandler /* USB LP and CAN1RX0 */
|
||||
.word CAN1_RX1_IRQHandler /* CAN1 RX1 */
|
||||
.word CAN1_SCE_IRQHandler /* CAN1 SCE */
|
||||
.word EXTI9_5_IRQHandler /* EXTI Line 9..5 */
|
||||
.word TIM1_BRK_IRQHandler /* TIM1 Break */
|
||||
.word TIM1_UP_IRQHandler /* TIM1 Update */
|
||||
.word TIM1_TRG_COM_IRQHandler /* TIM1 Trigger and Commutation */
|
||||
.word TIM1_CC_IRQHandler /* TIM1 Capture Compare */
|
||||
.word TIM2_IRQHandler /* TIM2 */
|
||||
.word TIM3_IRQHandler /* TIM3 */
|
||||
.word TIM4_IRQHandler /* TIM4 */
|
||||
.word I2C1_EV_IRQHandler /* I2C1 Event */
|
||||
.word I2C1_ER_IRQHandler /* I2C1 Error */
|
||||
.word I2C2_EV_IRQHandler /* I2C2 Event */
|
||||
.word I2C2_ER_IRQHandler /* I2C2 Error */
|
||||
.word SPI1_IRQHandler /* SPI1 */
|
||||
.word SPI2_IRQHandler /* SPI2 */
|
||||
.word USART1_IRQHandler /* USART1 */
|
||||
.word USART2_IRQHandler /* USART2 */
|
||||
.word USART3_IRQHandler /* USART3 */
|
||||
.word EXTI15_10_IRQHandler /* EXTI Line 15..10 */
|
||||
.word RTCAlarm_IRQHandler /* RTC Alarm through EXTI Line */
|
||||
.word USBWakeUp_IRQHandler /* USB Wake up from suspend */
|
||||
.word USBFS_IRQHandler /* USBFS Break */
|
||||
.word USBFSWakeUp_IRQHandler /* USBFS Wake up from suspend */
|
||||
.word ETH_IRQHandler /* ETH global */
|
||||
.word ETHWakeUp_IRQHandler /* ETH Wake up */
|
||||
.word BB_IRQHandler /* BLE BB */
|
||||
.word LLE_IRQHandler /* BLE LLE */
|
||||
.word TIM5_IRQHandler /* TIM5 */
|
||||
.word UART4_IRQHandler /* UART4 */
|
||||
.word DMA1_Channel8_IRQHandler /* DMA1 Channel8 */
|
||||
.word OSC32KCal_IRQHandler /* OSC32KCal */
|
||||
.word OSCWakeUp_IRQHandler /* OSC Wake Up */
|
||||
|
||||
.option rvc;
|
||||
|
||||
.section .text.vector_handler, "ax", @progbits
|
||||
.weak NMI_Handler /* NMI */
|
||||
.weak HardFault_Handler /* Hard Fault */
|
||||
.weak Ecall_M_Mode_Handler /* Ecall M Mode */
|
||||
.weak Ecall_U_Mode_Handler /* Ecall U Mode */
|
||||
.weak Break_Point_Handler /* Break Point */
|
||||
.weak SysTick_Handler /* SysTick */
|
||||
.weak SW_Handler /* SW */
|
||||
.weak WWDG_IRQHandler /* Window Watchdog */
|
||||
.weak PVD_IRQHandler /* PVD through EXTI Line detect */
|
||||
.weak TAMPER_IRQHandler /* TAMPER */
|
||||
.weak RTC_IRQHandler /* RTC */
|
||||
.weak FLASH_IRQHandler /* Flash */
|
||||
.weak RCC_IRQHandler /* RCC */
|
||||
.weak EXTI0_IRQHandler /* EXTI Line 0 */
|
||||
.weak EXTI1_IRQHandler /* EXTI Line 1 */
|
||||
.weak EXTI2_IRQHandler /* EXTI Line 2 */
|
||||
.weak EXTI3_IRQHandler /* EXTI Line 3 */
|
||||
.weak EXTI4_IRQHandler /* EXTI Line 4 */
|
||||
.weak DMA1_Channel1_IRQHandler /* DMA1 Channel 1 */
|
||||
.weak DMA1_Channel2_IRQHandler /* DMA1 Channel 2 */
|
||||
.weak DMA1_Channel3_IRQHandler /* DMA1 Channel 3 */
|
||||
.weak DMA1_Channel4_IRQHandler /* DMA1 Channel 4 */
|
||||
.weak DMA1_Channel5_IRQHandler /* DMA1 Channel 5 */
|
||||
.weak DMA1_Channel6_IRQHandler /* DMA1 Channel 6 */
|
||||
.weak DMA1_Channel7_IRQHandler /* DMA1 Channel 7 */
|
||||
.weak ADC1_2_IRQHandler /* ADC1_2 */
|
||||
.weak USB_HP_CAN1_TX_IRQHandler /* USB HP and CAN1 TX */
|
||||
.weak USB_LP_CAN1_RX0_IRQHandler /* USB LP and CAN1RX0 */
|
||||
.weak CAN1_RX1_IRQHandler /* CAN1 RX1 */
|
||||
.weak CAN1_SCE_IRQHandler /* CAN1 SCE */
|
||||
.weak EXTI9_5_IRQHandler /* EXTI Line 9..5 */
|
||||
.weak TIM1_BRK_IRQHandler /* TIM1 Break */
|
||||
.weak TIM1_UP_IRQHandler /* TIM1 Update */
|
||||
.weak TIM1_TRG_COM_IRQHandler /* TIM1 Trigger and Commutation */
|
||||
.weak TIM1_CC_IRQHandler /* TIM1 Capture Compare */
|
||||
.weak TIM2_IRQHandler /* TIM2 */
|
||||
.weak TIM3_IRQHandler /* TIM3 */
|
||||
.weak TIM4_IRQHandler /* TIM4 */
|
||||
.weak I2C1_EV_IRQHandler /* I2C1 Event */
|
||||
.weak I2C1_ER_IRQHandler /* I2C1 Error */
|
||||
.weak I2C2_EV_IRQHandler /* I2C2 Event */
|
||||
.weak I2C2_ER_IRQHandler /* I2C2 Error */
|
||||
.weak SPI1_IRQHandler /* SPI1 */
|
||||
.weak SPI2_IRQHandler /* SPI2 */
|
||||
.weak USART1_IRQHandler /* USART1 */
|
||||
.weak USART2_IRQHandler /* USART2 */
|
||||
.weak USART3_IRQHandler /* USART3 */
|
||||
.weak EXTI15_10_IRQHandler /* EXTI Line 15..10 */
|
||||
.weak RTCAlarm_IRQHandler /* RTC Alarm through EXTI Line */
|
||||
.weak USBWakeUp_IRQHandler /* USB Wakeup from suspend */
|
||||
.weak USBFS_IRQHandler /* USBFS */
|
||||
.weak USBFSWakeUp_IRQHandler /* USBFS Wake Up */
|
||||
.weak ETH_IRQHandler /* ETH global */
|
||||
.weak ETHWakeUp_IRQHandler /* ETHWakeUp */
|
||||
.weak BB_IRQHandler /* BB */
|
||||
.weak LLE_IRQHandler /* LLE */
|
||||
.weak TIM5_IRQHandler /* TIM5 */
|
||||
.weak UART4_IRQHandler /* UART4 */
|
||||
.weak DMA1_Channel8_IRQHandler /* DMA1 Channel8 */
|
||||
.weak OSC32KCal_IRQHandler /* OSC32 KCal */
|
||||
.weak OSCWakeUp_IRQHandler /* OSC Wake Up */
|
||||
|
||||
NMI_Handler: 1: j 1b
|
||||
HardFault_Handler: 1: j 1b
|
||||
Ecall_M_Mode_Handler: 1: j 1b
|
||||
Ecall_U_Mode_Handler: 1: j 1b
|
||||
Break_Point_Handler: 1: j 1b
|
||||
SysTick_Handler: 1: j 1b
|
||||
SW_Handler: 1: j 1b
|
||||
WWDG_IRQHandler: 1: j 1b
|
||||
PVD_IRQHandler: 1: j 1b
|
||||
TAMPER_IRQHandler: 1: j 1b
|
||||
RTC_IRQHandler: 1: j 1b
|
||||
FLASH_IRQHandler: 1: j 1b
|
||||
RCC_IRQHandler: 1: j 1b
|
||||
EXTI0_IRQHandler: 1: j 1b
|
||||
EXTI1_IRQHandler: 1: j 1b
|
||||
EXTI2_IRQHandler: 1: j 1b
|
||||
EXTI3_IRQHandler: 1: j 1b
|
||||
EXTI4_IRQHandler: 1: j 1b
|
||||
DMA1_Channel1_IRQHandler: 1: j 1b
|
||||
DMA1_Channel2_IRQHandler: 1: j 1b
|
||||
DMA1_Channel3_IRQHandler: 1: j 1b
|
||||
DMA1_Channel4_IRQHandler: 1: j 1b
|
||||
DMA1_Channel5_IRQHandler: 1: j 1b
|
||||
DMA1_Channel6_IRQHandler: 1: j 1b
|
||||
DMA1_Channel7_IRQHandler: 1: j 1b
|
||||
ADC1_2_IRQHandler: 1: j 1b
|
||||
USB_HP_CAN1_TX_IRQHandler: 1: j 1b
|
||||
USB_LP_CAN1_RX0_IRQHandler: 1: j 1b
|
||||
CAN1_RX1_IRQHandler: 1: j 1b
|
||||
CAN1_SCE_IRQHandler: 1: j 1b
|
||||
EXTI9_5_IRQHandler: 1: j 1b
|
||||
TIM1_BRK_IRQHandler: 1: j 1b
|
||||
TIM1_UP_IRQHandler: 1: j 1b
|
||||
TIM1_TRG_COM_IRQHandler: 1: j 1b
|
||||
TIM1_CC_IRQHandler: 1: j 1b
|
||||
TIM2_IRQHandler: 1: j 1b
|
||||
TIM3_IRQHandler: 1: j 1b
|
||||
TIM4_IRQHandler: 1: j 1b
|
||||
I2C1_EV_IRQHandler: 1: j 1b
|
||||
I2C1_ER_IRQHandler: 1: j 1b
|
||||
I2C2_EV_IRQHandler: 1: j 1b
|
||||
I2C2_ER_IRQHandler: 1: j 1b
|
||||
SPI1_IRQHandler: 1: j 1b
|
||||
SPI2_IRQHandler: 1: j 1b
|
||||
USART1_IRQHandler: 1: j 1b
|
||||
USART2_IRQHandler: 1: j 1b
|
||||
USART3_IRQHandler: 1: j 1b
|
||||
EXTI15_10_IRQHandler: 1: j 1b
|
||||
RTCAlarm_IRQHandler: 1: j 1b
|
||||
USBWakeUp_IRQHandler: 1: j 1b
|
||||
USBFS_IRQHandler: 1: j 1b
|
||||
USBFSWakeUp_IRQHandler: 1: j 1b
|
||||
ETH_IRQHandler: 1: j 1b
|
||||
ETHWakeUp_IRQHandler: 1: j 1b
|
||||
BB_IRQHandler: 1: j 1b
|
||||
LLE_IRQHandler: 1: j 1b
|
||||
TIM5_IRQHandler: 1: j 1b
|
||||
UART4_IRQHandler: 1: j 1b
|
||||
DMA1_Channel8_IRQHandler: 1: j 1b
|
||||
OSC32KCal_IRQHandler: 1: j 1b
|
||||
OSCWakeUp_IRQHandler: 1: j 1b
|
||||
|
||||
.section .text.handle_reset,"ax",@progbits
|
||||
.weak handle_reset
|
||||
.align 1
|
||||
handle_reset:
|
||||
.option push
|
||||
.option norelax
|
||||
la gp, __global_pointer$
|
||||
.option pop
|
||||
1:
|
||||
la sp, _eusrstack
|
||||
2:
|
||||
/* Load data section from flash to RAM */
|
||||
la a0, _data_lma
|
||||
la a1, _data_vma
|
||||
la a2, _edata
|
||||
bgeu a1, a2, 2f
|
||||
1:
|
||||
lw t0, (a0)
|
||||
sw t0, (a1)
|
||||
addi a0, a0, 4
|
||||
addi a1, a1, 4
|
||||
bltu a1, a2, 1b
|
||||
2:
|
||||
/* Clear bss section */
|
||||
la a0, _sbss
|
||||
la a1, _ebss
|
||||
bgeu a0, a1, 2f
|
||||
1:
|
||||
sw zero, (a0)
|
||||
addi a0, a0, 4
|
||||
bltu a0, a1, 1b
|
||||
2:
|
||||
li t0, 0x1f
|
||||
csrw 0xbc0, t0
|
||||
|
||||
/* Enable nested and hardware stack */
|
||||
li t0, 0x3
|
||||
csrw 0x804, t0
|
||||
|
||||
/* Enable interrupt */
|
||||
li t0, 0x1888
|
||||
csrs mstatus, t0
|
||||
|
||||
la t0, _vector_base
|
||||
ori t0, t0, 3
|
||||
csrw mtvec, t0
|
||||
|
||||
jal SystemInit
|
||||
la t0, main
|
||||
csrw mepc, t0
|
||||
|
||||
j 0x40000
|
||||
mret
|
||||
|
||||
|
||||
428
vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/cfig_flash.c
Normal file
428
vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/cfig_flash.c
Normal file
@@ -0,0 +1,428 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file CFIG_FLASH.h
|
||||
* @author wfq
|
||||
* @version V1.0
|
||||
* @date 2025-02-07
|
||||
* @brief ??????????¡ã?¡À???????????????
|
||||
* @attention
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
#include "storage.h"
|
||||
#include "config.h"
|
||||
#include "cmcng.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include "net_srv.h"
|
||||
|
||||
#define DEV_USER_FLASH_MAGIC PRODUCT_MODEL
|
||||
|
||||
#define CFG_FLASH_BLOCK_SIZE 0x200
|
||||
|
||||
#define DEV_CNG_ADDR_BASE (0x00) // + 0x200)
|
||||
|
||||
|
||||
|
||||
void factory_dev_info(void)
|
||||
{
|
||||
uint16_t i, j;
|
||||
uint32_t _offset = 0;
|
||||
uint16_t len = 0;
|
||||
uint8_t *mBuff = (uint8_t *)malloc(CFG_FLASH_BLOCK_SIZE);
|
||||
if(mBuff == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
memset(mBuff, 0, CFG_FLASH_BLOCK_SIZE);
|
||||
|
||||
memcpy(mBuff, DEV_USER_FLASH_MAGIC, sizeof(DEV_USER_FLASH_MAGIC));
|
||||
_offset = DEV_NUMER_ADDR_OFFSET;
|
||||
|
||||
memcpy(g_dev_number, gMacAddr, 6);
|
||||
memcpy(&mBuff[_offset], g_dev_number, sizeof(g_dev_number));
|
||||
|
||||
mBuff[DEV_SUB_CODE_ADDR_OFFSET] = 0x01; // SSC net
|
||||
|
||||
|
||||
mBuff[DEV_BUS_BAUD_ADDR_OFFSET] = BT_DISABLE_IDLE_TIMEOUT;
|
||||
mBuff[DEV_BUS_BAUD_ADDR_OFFSET + 1] = (uint8_t)UART_BAUD_DEFAULT_PORT_1;
|
||||
mBuff[DEV_BUS_BAUD_ADDR_OFFSET + 2] = (uint8_t)(UART_BAUD_DEFAULT_PORT_1 >> 8);
|
||||
mBuff[DEV_BUS_BAUD_ADDR_OFFSET + 3] = (uint8_t)(UART_BAUD_DEFAULT_PORT_1 >> 16);
|
||||
|
||||
mBuff[DEV_BUS_BAUD_ADDR_OFFSET + 4] = 0x02;
|
||||
mBuff[DEV_BUS_BAUD_ADDR_OFFSET + 5] = (uint8_t)UART_BAUD_DEFAULT_PORT_2;
|
||||
mBuff[DEV_BUS_BAUD_ADDR_OFFSET + 6] = (uint8_t)(UART_BAUD_DEFAULT_PORT_2 >> 8);
|
||||
mBuff[DEV_BUS_BAUD_ADDR_OFFSET + 7] = (uint8_t)(UART_BAUD_DEFAULT_PORT_2 >> 16);
|
||||
|
||||
sprintf((char *)(&mBuff[DEV_PASSWORD_ADDR_OFFSET]), "%s", "123456");
|
||||
// for(i = 0; i < CFG_FLASH_BLOCK_SIZE; i++){
|
||||
// PRINT(" %02X", mBuff[i]);
|
||||
// }
|
||||
// PRINT("\nWill_Write_cng:");
|
||||
SPI_Flash_Write(mBuff, DEV_CNG_ADDR_BASE, CFG_FLASH_BLOCK_SIZE);
|
||||
|
||||
free(mBuff);
|
||||
|
||||
Delay_Ms(50);
|
||||
|
||||
|
||||
Local_Net_Cfg lcfg = {
|
||||
{gMacAddr[0], gMacAddr[1], gMacAddr[2], gMacAddr[3], gMacAddr[4], gMacAddr[5]},
|
||||
{192,168,1,188},
|
||||
{255,255,255,0},
|
||||
{192,168,1,1},
|
||||
{192,168,1,1},
|
||||
5550,NET_REPORT_INTERVAL, 5505, // Master Port: tcp, udp, udp_message
|
||||
5550, 4900 // Dev Port: tcp, udp
|
||||
};
|
||||
NET_CENTER_INFO ccfg = {
|
||||
{192,168,1,222},
|
||||
4999,5550,NET_REPORT_INTERVAL, {1,2}
|
||||
};
|
||||
IOT_NET_INFO icfg = {
|
||||
"121.37.20.199",
|
||||
1883,
|
||||
" ",
|
||||
"admin",
|
||||
"password",
|
||||
0
|
||||
};
|
||||
IOT_Topic _topic = {
|
||||
0,
|
||||
TOPIC_DEFAULT_PUBLISH,
|
||||
TOPIC_DEFAULT_SUBSCRIBE
|
||||
};
|
||||
|
||||
write_net_config(&lcfg, &ccfg, &icfg, &_topic);
|
||||
}
|
||||
|
||||
char get_ble_safe_flag(void)
|
||||
{
|
||||
return g_ble_safe_flag;
|
||||
}
|
||||
|
||||
|
||||
void open_ble_safe_flag(void)
|
||||
{
|
||||
if(g_ble_safe_flag == 1)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
g_ble_safe_counter_dst = 0;
|
||||
g_ble_safe_counter_ori = g_ble_safe_counter_dst;
|
||||
g_ble_safe_flag = 1;
|
||||
}
|
||||
|
||||
void close_ble_safe_flag(void)
|
||||
{
|
||||
if(g_ble_safe_flag == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
g_ble_safe_flag = 0;
|
||||
}
|
||||
|
||||
|
||||
char check_ble_safe_pass(uint8_t * devpass)
|
||||
{
|
||||
if((g_dev_password[0] != devpass[0]) ||
|
||||
(g_dev_password[1] != devpass[1]) ||
|
||||
(g_dev_password[2] != devpass[2]) ||
|
||||
(g_dev_password[3] != devpass[3]) ||
|
||||
(g_dev_password[4] != devpass[4]) ||
|
||||
(g_dev_password[5] != devpass[5]))
|
||||
{
|
||||
close_ble_safe_flag();
|
||||
return 0;
|
||||
}
|
||||
open_ble_safe_flag();
|
||||
return 1;
|
||||
|
||||
}
|
||||
|
||||
|
||||
void set_ble_safe_pass(uint8_t * devpass)
|
||||
{
|
||||
uint16_t len = 0;
|
||||
uint16_t _offset = 0;
|
||||
uint8_t *mBuff = (uint8_t *)malloc(CFG_FLASH_BLOCK_SIZE);
|
||||
if(mBuff == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
memset(mBuff, 0, CFG_FLASH_BLOCK_SIZE);
|
||||
SPI_Flash_Read(mBuff, DEV_CNG_ADDR_BASE, CFG_FLASH_BLOCK_SIZE);
|
||||
|
||||
_offset = DEV_PASSWORD_ADDR_OFFSET;
|
||||
mBuff[_offset++] = devpass[0];
|
||||
mBuff[_offset++] = devpass[1];
|
||||
mBuff[_offset++] = devpass[2];
|
||||
mBuff[_offset++] = devpass[3];
|
||||
mBuff[_offset++] = devpass[4];
|
||||
mBuff[_offset++] = devpass[5];
|
||||
|
||||
SPI_Flash_Write(mBuff, DEV_CNG_ADDR_BASE, CFG_FLASH_BLOCK_SIZE);
|
||||
|
||||
free(mBuff);
|
||||
}
|
||||
|
||||
|
||||
void write_net_config(Local_Net_Cfg *local, NET_CENTER_INFO *center, IOT_NET_INFO *iotcfg, IOT_Topic *iottopic)
|
||||
{
|
||||
uint16_t i;
|
||||
uint32_t _offset = 0;
|
||||
uint8_t *mBuff = (uint8_t *)malloc(CFG_FLASH_BLOCK_SIZE);
|
||||
if(mBuff == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
memset(mBuff, 0, CFG_FLASH_BLOCK_SIZE);
|
||||
|
||||
SPI_Flash_Read(mBuff, DEV_CNG_ADDR_BASE, CFG_FLASH_BLOCK_SIZE);
|
||||
|
||||
_offset = NET_LOCAL_CFG_ADDR_OFFSET;
|
||||
memcpy(&mBuff[_offset], local, sizeof(Local_Net_Cfg));
|
||||
_offset = NET_CENTER_CFG_ADDR_OFFSET;
|
||||
memcpy(&mBuff[_offset], center, sizeof(NET_CENTER_INFO));
|
||||
_offset = IOT_NET_CFG_ADDR;
|
||||
memcpy(&mBuff[_offset], iotcfg, sizeof(IOT_NET_INFO));
|
||||
_offset = IOT_TOPIC_CFG_ADDR;
|
||||
memcpy(&mBuff[_offset], iottopic, sizeof(IOT_Topic));
|
||||
|
||||
// for(i = 0; i < CFG_FLASH_BLOCK_SIZE; i++){
|
||||
// PRINT(" %02X", mBuff[i]);
|
||||
// }
|
||||
// PRINT(" \nEND_Factory\n");
|
||||
|
||||
SPI_Flash_Write(mBuff, DEV_CNG_ADDR_BASE, CFG_FLASH_BLOCK_SIZE);
|
||||
|
||||
free(mBuff);
|
||||
|
||||
Delay_Ms(50);
|
||||
}
|
||||
|
||||
|
||||
void load_cfg_from_flash(void)
|
||||
{
|
||||
uint16_t i, j;
|
||||
uint32_t _offset = 0;
|
||||
|
||||
uint16_t len = 0;
|
||||
uint8_t *mBuff = (uint8_t *)malloc(CFG_FLASH_BLOCK_SIZE);
|
||||
if(mBuff == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
memset(mBuff, 0, CFG_FLASH_BLOCK_SIZE);
|
||||
|
||||
SPI_Flash_Read(mBuff, DEV_CNG_ADDR_BASE, CFG_FLASH_BLOCK_SIZE);
|
||||
|
||||
// for(i = 0; i < CFG_FLASH_BLOCK_SIZE; i++){
|
||||
// PRINT(" %02X", mBuff[i]);
|
||||
// }
|
||||
// PRINT("\n");
|
||||
|
||||
if(strncmp(mBuff, DEV_USER_FLASH_MAGIC, sizeof(DEV_USER_FLASH_MAGIC)) != 0)
|
||||
{
|
||||
free(mBuff);
|
||||
PRINT("Will_Factory_dev_____\n");
|
||||
|
||||
factory_dev_info();
|
||||
|
||||
NVIC_SystemReset();
|
||||
}
|
||||
|
||||
_offset = DEV_NUMER_ADDR_OFFSET;
|
||||
memcpy(g_dev_number, &mBuff[_offset], 6);
|
||||
|
||||
memcpy(g_dev_number, &mBuff[DEV_NUMER_ADDR_OFFSET], 6); //
|
||||
sprintf(g_dev_number_str, "%02X%02X%02X%02X%02X%02X",g_dev_number[0],g_dev_number[1],g_dev_number[2],g_dev_number[3],g_dev_number[4],g_dev_number[5]);
|
||||
|
||||
g_sub_code_enable.code_set = *(uint16_t *)(&mBuff[DEV_SUB_CODE_ADDR_OFFSET]);
|
||||
|
||||
g_max_counter_bt_min = mBuff[DEV_BUS_BAUD_ADDR_OFFSET];
|
||||
g_max_counter_bt_timeout = g_max_counter_bt_min * 60 * 1000;
|
||||
g_storage_uart_baud = (mBuff[DEV_BUS_BAUD_ADDR_OFFSET + 1]) | (mBuff[DEV_BUS_BAUD_ADDR_OFFSET + 2] << 8) | (mBuff[DEV_BUS_BAUD_ADDR_OFFSET + 3] << 16);
|
||||
g_storage_uart_baud_2 = (mBuff[DEV_BUS_BAUD_ADDR_OFFSET + 5]) | (mBuff[DEV_BUS_BAUD_ADDR_OFFSET + 6] << 8) | (mBuff[DEV_BUS_BAUD_ADDR_OFFSET + 7] << 16);
|
||||
|
||||
|
||||
memcpy(g_dev_password, &mBuff[DEV_PASSWORD_ADDR_OFFSET], 6);
|
||||
memcpy(local_net_cfg.mac, g_dev_number, 6);
|
||||
|
||||
_offset = NET_LOCAL_CFG_ADDR_OFFSET;
|
||||
Local_Net_Cfg *lnet = (Local_Net_Cfg *)(&mBuff[_offset]);
|
||||
// memcpy(local_net_cfg.mac, lnet->mac, 6);
|
||||
memcpy(local_net_cfg.lip, lnet->lip, 4);
|
||||
memcpy(local_net_cfg.sub, lnet->sub, 4);
|
||||
memcpy(local_net_cfg.gw, lnet->gw, 4);
|
||||
memcpy(local_net_cfg.dns, lnet->dns, 4);
|
||||
local_net_cfg.port_ssc_tcp = lnet->port_ssc_tcp;
|
||||
local_net_cfg.port_ssc_udp = lnet->port_ssc_udp;
|
||||
local_net_cfg.port_ssc_udp_message = lnet->port_ssc_udp_message;
|
||||
local_net_cfg.port_dev_tcp = lnet->port_dev_tcp;
|
||||
local_net_cfg.port_dev_udp = lnet->port_dev_udp;
|
||||
|
||||
_offset = NET_CENTER_CFG_ADDR_OFFSET;
|
||||
NET_CENTER_INFO *cinfo = (NET_CENTER_INFO *)(&mBuff[_offset]);
|
||||
memcpy(net_center_info.lssc_ip, cinfo->lssc_ip, 4);
|
||||
net_center_info.msg_port = cinfo->msg_port;
|
||||
net_center_info.tcp_port = cinfo->tcp_port;
|
||||
net_center_info.udp_port = cinfo->udp_port;
|
||||
memcpy(net_center_info.sw_ver, cinfo->sw_ver, 2);
|
||||
|
||||
|
||||
_offset = IOT_NET_CFG_ADDR;
|
||||
IOT_NET_INFO *iinfo = (IOT_NET_INFO *)(&mBuff[_offset]);
|
||||
memcpy(iot_net_info.remote_addr, iinfo->remote_addr, 64);
|
||||
iot_net_info.mqtt_port = iinfo->mqtt_port;
|
||||
memcpy(iot_net_info.client_id, iinfo->client_id, 64);
|
||||
memcpy(iot_net_info.username, iinfo->username, 64);
|
||||
memcpy(iot_net_info.password, iinfo->password, 32);
|
||||
//check dns or ip mode
|
||||
iot_net_info.mode = iinfo->mode; //dns or ip mode
|
||||
|
||||
_offset = IOT_TOPIC_CFG_ADDR;
|
||||
IOT_Topic *_topic = (IOT_Topic *)(&mBuff[_offset]);
|
||||
g_iot_topic.clientid_enable = _topic->clientid_enable;
|
||||
memcpy(g_iot_topic.topic_pub, _topic->topic_pub, 64);
|
||||
memcpy(g_iot_topic.topic_sub, _topic->topic_sub, 64);
|
||||
|
||||
|
||||
|
||||
free(mBuff);
|
||||
|
||||
g_sub_code_enable.net_enable = g_sub_code_enable.code_set & 0x01;
|
||||
g_sub_code_enable.iot_enable = (g_sub_code_enable.code_set >> 1) & 0x01;
|
||||
g_sub_code_enable.custom_enable = (g_sub_code_enable.code_set >> 2) & 0x01;
|
||||
g_sub_code_enable.loop_enable = (g_sub_code_enable.code_set >> 3) & 0x01;
|
||||
g_sub_code_enable.dgdus_enable = (g_sub_code_enable.code_set >> 4) & 0x01;
|
||||
g_sub_code_enable.wbdus_enable = (g_sub_code_enable.code_set >> 5) & 0x01;
|
||||
g_sub_code_enable.radar_enable = (g_sub_code_enable.code_set >> 6) & 0x01;
|
||||
|
||||
// g_sub_code_enable.laser_enable = (g_sub_code_enable.code_set >> 9) & 0x01;
|
||||
// g_sub_code_enable.lora_enable = (g_sub_code_enable.code_set >> 15) & 0x01;
|
||||
|
||||
|
||||
}
|
||||
|
||||
void output_cfg_from_flash(void)
|
||||
{
|
||||
// if(g_flag_debug)
|
||||
// {
|
||||
PRINT("\nBaud1:%d, Baud2:%d\n", g_storage_uart_baud, g_storage_uart_baud_2);
|
||||
PRINT("\nGet Net config:\n");
|
||||
PRINT("Local Net Config: "
|
||||
" mac:%02X-%02X-%02X-%02X-%02X-%02X\n"
|
||||
" ip:%d.%d.%d.%d\n"
|
||||
" submask:%d.%d.%d.%d\n"
|
||||
" gateway:%d.%d.%d.%d\n"
|
||||
" dns:%d.%d.%d.%d\n"
|
||||
" ssc_port tcp: %d, udp:%d, udp_msg:%d\n"
|
||||
" dev_port tcp: %d, udp:%d\n",
|
||||
local_net_cfg.mac[0],local_net_cfg.mac[1],local_net_cfg.mac[2],local_net_cfg.mac[3],local_net_cfg.mac[4],local_net_cfg.mac[5],
|
||||
local_net_cfg.lip[0],local_net_cfg.lip[1],local_net_cfg.lip[2],local_net_cfg.lip[3],
|
||||
local_net_cfg.sub[0],local_net_cfg.sub[1],local_net_cfg.sub[2],local_net_cfg.sub[3],
|
||||
local_net_cfg.gw[0],local_net_cfg.gw[1],local_net_cfg.gw[2],local_net_cfg.gw[3],
|
||||
local_net_cfg.dns[0],local_net_cfg.dns[1],local_net_cfg.dns[2],local_net_cfg.dns[3],
|
||||
local_net_cfg.port_ssc_tcp, local_net_cfg.port_ssc_udp, local_net_cfg.port_ssc_udp_message,
|
||||
local_net_cfg.port_dev_tcp, local_net_cfg.port_dev_udp
|
||||
);
|
||||
PRINT("Net Center Config:\n");
|
||||
PRINT(
|
||||
" lssc_ip:%d.%d.%d.%d\n"
|
||||
" msg_port:%d, tcp_port:%d, udp_port:%d, sw_ver:%d.%d\n",
|
||||
net_center_info.lssc_ip[0], net_center_info.lssc_ip[1], net_center_info.lssc_ip[2], net_center_info.lssc_ip[3],
|
||||
net_center_info.msg_port, net_center_info.tcp_port, net_center_info.udp_port,
|
||||
net_center_info.sw_ver[0], net_center_info.sw_ver[1]);
|
||||
PRINT("Iot Net Config:\n");
|
||||
PRINT(
|
||||
" remote_addr:%s\n"
|
||||
" mqtt_port:%d, client_id:%s\n"
|
||||
" username:%s\n"
|
||||
" password:%s\n"
|
||||
" mode:%d\n\n",
|
||||
iot_net_info.remote_addr, iot_net_info.mqtt_port, iot_net_info.client_id,
|
||||
iot_net_info.username, iot_net_info.password, iot_net_info.mode);
|
||||
|
||||
PRINT("Iot Topic Config:\n");
|
||||
PRINT(
|
||||
" clientid_enable:%d\n"
|
||||
" topic_pub:%s\n"
|
||||
" topic_sub:%s\n",
|
||||
g_iot_topic.clientid_enable, g_iot_topic.topic_pub, g_iot_topic.topic_sub);
|
||||
// }
|
||||
}
|
||||
|
||||
|
||||
// g_sub_code_enable.code_set get ready first.
|
||||
void update_sub_code_enable(void)
|
||||
{
|
||||
uint32_t _offset = 0;
|
||||
uint16_t len = 0;
|
||||
|
||||
uint8_t *mBuff = (uint8_t *)malloc(CFG_FLASH_BLOCK_SIZE);
|
||||
if(mBuff == NULL)
|
||||
{
|
||||
PRINT("Err_When_malloc__\n");
|
||||
free(mBuff);
|
||||
return;
|
||||
}
|
||||
memset(mBuff, 0, CFG_FLASH_BLOCK_SIZE);
|
||||
|
||||
SPI_Flash_Read(mBuff, DEV_CNG_ADDR_BASE, CFG_FLASH_BLOCK_SIZE);
|
||||
|
||||
_offset = DEV_SUB_CODE_ADDR_OFFSET;
|
||||
memcpy(&mBuff[_offset], &(g_sub_code_enable.code_set), sizeof(g_sub_code_enable.code_set));
|
||||
|
||||
SPI_Flash_Write(mBuff, DEV_CNG_ADDR_BASE, CFG_FLASH_BLOCK_SIZE);
|
||||
|
||||
free(mBuff);
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
void alter_dev_serila(uint8_t *serial)
|
||||
{
|
||||
uint32_t _offset = 0;
|
||||
uint16_t len = 0;
|
||||
|
||||
uint8_t *mBuff = (uint8_t *)malloc(CFG_FLASH_BLOCK_SIZE);
|
||||
if(mBuff == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
memset(mBuff, 0, CFG_FLASH_BLOCK_SIZE);
|
||||
SPI_Flash_Read(mBuff, DEV_CNG_ADDR_BASE, CFG_FLASH_BLOCK_SIZE);
|
||||
|
||||
memcpy(&mBuff[DEV_NUMER_ADDR_OFFSET], serial, 6);
|
||||
|
||||
SPI_Flash_Write(mBuff, DEV_CNG_ADDR_BASE, CFG_FLASH_BLOCK_SIZE);
|
||||
|
||||
free(mBuff);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void alter_dev_baud(uint8_t *baudbuf)
|
||||
{
|
||||
uint32_t _offset = 0;
|
||||
|
||||
uint8_t *mBuff = (uint8_t *)malloc(CFG_FLASH_BLOCK_SIZE);
|
||||
if(mBuff == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
memset(mBuff, 0, CFG_FLASH_BLOCK_SIZE);
|
||||
SPI_Flash_Read(mBuff, DEV_CNG_ADDR_BASE, CFG_FLASH_BLOCK_SIZE);
|
||||
|
||||
memcpy(&mBuff[DEV_BUS_BAUD_ADDR_OFFSET], baudbuf, 8);
|
||||
|
||||
|
||||
free(mBuff);
|
||||
}
|
||||
|
||||
|
||||
86
vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/ch32v20x_it.c
Normal file
86
vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/ch32v20x_it.c
Normal file
@@ -0,0 +1,86 @@
|
||||
/********************************** (C) COPYRIGHT *******************************
|
||||
* File Name : ch32v20x_it.c
|
||||
* Author : WCH
|
||||
* Version : V1.0.0
|
||||
* Date : 2022/06/16
|
||||
* Description : Main Interrupt Service Routines.
|
||||
*********************************************************************************
|
||||
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
|
||||
* Attention: This software (modified or not) and binary are used for
|
||||
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
|
||||
*******************************************************************************/
|
||||
|
||||
/*********************************************************************
|
||||
* INCLUDES
|
||||
*/
|
||||
#include "ch32v20x_it.h"
|
||||
#include "eth_driver.h"
|
||||
#include "CONFIG.h"
|
||||
|
||||
/*********************************************************************
|
||||
* LOCAL FUNCTIONS
|
||||
*/
|
||||
void NMI_Handler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
|
||||
void HardFault_Handler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
|
||||
void BB_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
|
||||
void ETH_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
|
||||
void TIM2_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
|
||||
|
||||
|
||||
/*********************************************************************
|
||||
* @fn NMI_Handler
|
||||
*
|
||||
* @brief This function handles NMI exception.
|
||||
*
|
||||
* @return None
|
||||
*/
|
||||
void NMI_Handler(void)
|
||||
{
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn HardFault_Handler
|
||||
*
|
||||
* @brief This function handles Hard Fault exception.
|
||||
*
|
||||
* @return None
|
||||
*/
|
||||
void HardFault_Handler(void)
|
||||
{
|
||||
NVIC_SystemReset();
|
||||
while(1)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn BB_IRQHandler
|
||||
*
|
||||
* @brief BB Interrupt for BLE.
|
||||
*
|
||||
* @return None
|
||||
*/
|
||||
void BB_IRQHandler(void)
|
||||
{
|
||||
BB_IRQLibHandler();
|
||||
}
|
||||
|
||||
|
||||
/*********************************************************************
|
||||
* @fn ETH_IRQHandler
|
||||
*
|
||||
* @brief This function handles ETH exception.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
void ETH_IRQHandler(void)
|
||||
{
|
||||
WCHNET_ETHIsr();
|
||||
}
|
||||
|
||||
void TIM2_IRQHandler( void )
|
||||
{
|
||||
WCHNET_TimeIsr(WCHNETTIMERPERIOD);
|
||||
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
|
||||
}
|
||||
|
||||
922
vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/dbn_ble_srv.c
Normal file
922
vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/dbn_ble_srv.c
Normal file
@@ -0,0 +1,922 @@
|
||||
/*
|
||||
* dbn_ble_srv.c
|
||||
*
|
||||
* Created on: Aug 27, 2024
|
||||
* Author: Thinkpad
|
||||
*/
|
||||
|
||||
#include "dbn_ble_srv.h"
|
||||
#include "cmcng.h"
|
||||
#include "storage.h"
|
||||
#include <string.h>
|
||||
#include "net_srv.h"
|
||||
|
||||
|
||||
uint8_t g_flag_notify_temp = 0; //临时通知notify flag, 0 disable, 1 enable
|
||||
|
||||
BLE_Notify_Buf g_notify_buftemp = {0,0, ""};
|
||||
BLE_Notify_Buf g_ble_rcv_buf = {0,0, ""};
|
||||
BLE_Notify_Buf g_tmp_report_buf = {0,0, ""};
|
||||
|
||||
Buf_DBN_BLE g_buf_ble_response = {0,0,0,0,0,0,0,""};
|
||||
|
||||
DBN_BLE_State g_dbn_ble_state_acs_enable = {0};
|
||||
|
||||
uint8_t tmp_ble_buf[MAX_BLE_TMP_BUF_LEN] = "";
|
||||
|
||||
extern uint8_t loop1_FLAG_CUT;
|
||||
|
||||
|
||||
void clear_ble_notify_buf(BLE_Notify_Buf * buf)
|
||||
{
|
||||
buf->len = 0;
|
||||
buf->flag = 0;
|
||||
memset(buf, 0, MAX_BLE_Notify_Buf_LEN);
|
||||
}
|
||||
|
||||
void clear_buf_dbn_ble(Buf_DBN_BLE * buf)
|
||||
{
|
||||
uint8_t i = 0;
|
||||
|
||||
|
||||
buf->flag = 0;
|
||||
buf->magic = 0;
|
||||
buf->cmd = 0;
|
||||
memset(buf->dat, 0, buf->dat_len);
|
||||
buf->dat_len = 0;
|
||||
buf->dat_offset = 0;
|
||||
buf->pkg_amount = 0;
|
||||
buf->pkg_seq = 0;
|
||||
}
|
||||
|
||||
void clear_buf_dbn_ble_all(Buf_DBN_BLE * buf)
|
||||
{
|
||||
uint8_t i = 0;
|
||||
// if(g_flag_debug)
|
||||
{
|
||||
// PRINT("\nclear_buf_dbn_ble_flag:%02X,magic:%02X,pkg_amount:%02X,seq:%02X,cmd:%02X, dat_len:%d,offset:%d\n",
|
||||
// buf->flag,buf->magic,buf->pkg_amount, buf->pkg_seq,buf->cmd, buf->dat_len,buf->dat_offset);
|
||||
// for(i = 0; i < buf->dat_len; i++)
|
||||
// {
|
||||
// PRINT(" %02X", buf->dat[i]);
|
||||
// }
|
||||
// PRINT("\n");
|
||||
}
|
||||
buf->flag = 0;
|
||||
buf->magic = 0;
|
||||
buf->cmd = 0;
|
||||
memset(buf->dat, 0, MAX_BLE_BUF_LEN);
|
||||
buf->dat_len = 0;
|
||||
buf->dat_offset = 0;
|
||||
buf->pkg_amount = 0;
|
||||
buf->pkg_seq = 0;
|
||||
}
|
||||
|
||||
|
||||
// 0: 包检测异常
|
||||
//检测包是否完整,Magic Byte, Header Byte, CK Byte, 校验字节(异或校验,以及和校验)
|
||||
uint8_t check_pkg(uint8_t * pkg, uint8_t len)
|
||||
{
|
||||
if(len < 6) //min length of ble pkg is 6
|
||||
return 0;
|
||||
uint8_t magic = pkg[0];
|
||||
uint8_t _len = pkg[2]; //data_len
|
||||
if(magic == 0x9F){
|
||||
// if(_len + 4 < len){
|
||||
// return 0;
|
||||
// }
|
||||
if(pkg[1] == 0x01 && pkg[2] == 0x00 && pkg[3] == 0x01 && pkg[4] == 0xA5 && pkg[5] == 0xA7){
|
||||
g_flag_counter_ota.tick = 0;
|
||||
g_flag_counter_ota.flag = 1;
|
||||
PRINT("Come into OTA___\n");
|
||||
}
|
||||
}
|
||||
if(_len + 5 < len)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
uint8_t _dxor = 0;
|
||||
uint8_t _dsum = 0;
|
||||
uint8_t _cklen = _len + 3; //不计算 校验字节
|
||||
uint8_t i = 0;
|
||||
for(i = 1; i < _cklen; i++)
|
||||
{
|
||||
_dxor ^= pkg[i];
|
||||
_dsum += pkg[i];
|
||||
}
|
||||
if((_dxor != pkg[_cklen]) || (_dsum != pkg[_cklen+1]))
|
||||
{
|
||||
// if(g_flag_debug)
|
||||
// {
|
||||
// PRINT("_dxor:0x%02X - 0x%02X, _dsum:0x%02X - 0x%02X\r\n", _dxor, pkg[_cklen], _dsum, pkg[_cklen + 1] );
|
||||
// }
|
||||
return 0;
|
||||
}
|
||||
|
||||
return magic;
|
||||
}
|
||||
|
||||
static uint8_t set_dev_info_to_ready(uint8_t *dat_dst)
|
||||
{
|
||||
uint8_t i = 0, j = 0;
|
||||
for(i = 0; i < 6; i++)
|
||||
{
|
||||
dat_dst[i] = gMacAddr[i];
|
||||
// PRINT(" %02X",dat_dst[i] );
|
||||
}
|
||||
// memcpy(dat_dst, MACAddr, sizeof(MACAddr));
|
||||
// i += sizeof(MACAddr);
|
||||
dat_dst[i++] = HARDWARE_VER_MAIN;
|
||||
dat_dst[i++] = HARDWARE_VER_SUB;
|
||||
dat_dst[i++] = FIRMWARE_VER_MAIN;
|
||||
dat_dst[i++] = FIRMWARE_VER_SUB;
|
||||
|
||||
// Dev Model
|
||||
dat_dst[i++] = strlen(PRODUCT_MODEL);
|
||||
memcpy(&dat_dst[i], PRODUCT_MODEL, strlen(PRODUCT_MODEL));
|
||||
i += strlen(PRODUCT_MODEL);
|
||||
|
||||
// Dev Number
|
||||
for(j = 0; j < 6; j++, i++)
|
||||
{
|
||||
dat_dst[i] = g_dev_number[j];
|
||||
}
|
||||
// sub_code
|
||||
// memcpy(&dat_dst[i], &(g_sub_code_enable.code_set), sizeof(g_sub_code_enable.code_set));
|
||||
// i += sizeof(g_sub_code_enable.code_set);
|
||||
dat_dst[i++] = 0;
|
||||
dat_dst[i++] = 0;
|
||||
|
||||
|
||||
dat_dst[i++] = 0; // g_bus_dus_amount_stored.bus1_amount;
|
||||
dat_dst[i++] = 0; //g_bus_dus_amount_stored.bus2_amount;
|
||||
return i;
|
||||
}
|
||||
|
||||
static uint8_t set_net_info_to_ready(uint8_t * dat_dst)
|
||||
{
|
||||
uint8_t i = 0;
|
||||
memcpy(&dat_dst[i], local_net_cfg.lip, 4);
|
||||
i += 4;
|
||||
memcpy(&dat_dst[i], local_net_cfg.sub, 4);
|
||||
i += 4;
|
||||
memcpy(&dat_dst[i], local_net_cfg.gw, 4);
|
||||
i += 4;
|
||||
memcpy(&dat_dst[i], net_center_info.lssc_ip, 4);
|
||||
i += 4;
|
||||
memcpy(&dat_dst[i], local_net_cfg.dns, 4);
|
||||
i += 4;
|
||||
dat_dst[i++] = local_net_cfg.port_ssc_tcp;
|
||||
dat_dst[i++] = local_net_cfg.port_ssc_tcp >> 8;
|
||||
dat_dst[i++] = local_net_cfg.port_ssc_udp;
|
||||
dat_dst[i++] = local_net_cfg.port_ssc_udp >> 8;
|
||||
dat_dst[i++] = local_net_cfg.port_ssc_udp_message;
|
||||
dat_dst[i++] = local_net_cfg.port_ssc_udp_message >> 8;
|
||||
|
||||
dat_dst[i++] = local_net_cfg.port_dev_tcp;
|
||||
dat_dst[i++] = local_net_cfg.port_dev_tcp >> 8;
|
||||
dat_dst[i++] = local_net_cfg.port_dev_udp;
|
||||
dat_dst[i++] = local_net_cfg.port_dev_udp >> 8;
|
||||
|
||||
return i;
|
||||
}
|
||||
|
||||
void set_response_iot_net(Buf_DBN_BLE *response_dst)
|
||||
{ // config iot_net
|
||||
uint8_t ret = 0;
|
||||
uint8_t i = 0;
|
||||
int _len = 0;
|
||||
clear_buf_dbn_ble(response_dst);
|
||||
response_dst->magic = MAGIC_BYTE_DBN_DEFAULT;
|
||||
response_dst->cmd = CMD_DBN_GET_IOT_NET;
|
||||
|
||||
memcpy(response_dst->dat, iot_net_info.remote_addr, strlen(iot_net_info.remote_addr));
|
||||
i += strlen(iot_net_info.remote_addr);
|
||||
i++;
|
||||
_len = sprintf(&(response_dst->dat[i]), "%d", iot_net_info.mqtt_port);
|
||||
i += _len;
|
||||
i++;
|
||||
memcpy(&(response_dst->dat[i]), iot_net_info.client_id, strlen(iot_net_info.client_id));
|
||||
i += strlen(iot_net_info.client_id);
|
||||
i++;
|
||||
memcpy(&(response_dst->dat[i]), iot_net_info.username, strlen(iot_net_info.username));
|
||||
i += strlen(iot_net_info.username);
|
||||
i++;
|
||||
memcpy(&(response_dst->dat[i]), iot_net_info.password, strlen(iot_net_info.password));
|
||||
i += strlen(iot_net_info.password);
|
||||
response_dst->dat_len = i;
|
||||
response_dst->pkg_amount = i / MAX_BLE_DAT_RESPONSE_LEN;
|
||||
if((i % MAX_BLE_DAT_RESPONSE_LEN) > 0)
|
||||
{
|
||||
response_dst->pkg_amount += 1;
|
||||
}
|
||||
response_dst->pkg_seq = 0;
|
||||
|
||||
response_dst->flag = 1;
|
||||
}
|
||||
|
||||
void set_response_iot_topic(Buf_DBN_BLE *response_dst)
|
||||
{ //config iot_topic
|
||||
uint8_t ret = 0;
|
||||
uint8_t i = 0;
|
||||
int _len = 0;
|
||||
clear_buf_dbn_ble(response_dst);
|
||||
response_dst->magic = MAGIC_BYTE_DBN_DEFAULT;
|
||||
response_dst->cmd = CMD_DBN_GET_IOT_TOPIC;
|
||||
response_dst->dat[0] = g_iot_topic.clientid_enable + 0x30;
|
||||
i++;
|
||||
i++;
|
||||
memcpy(&(response_dst->dat[i]), g_iot_topic.topic_pub, strlen(g_iot_topic.topic_pub));
|
||||
i += strlen(g_iot_topic.topic_pub);
|
||||
i++;
|
||||
memcpy(&(response_dst->dat[i]), g_iot_topic.topic_sub, strlen(g_iot_topic.topic_sub));
|
||||
i += strlen(g_iot_topic.topic_sub);
|
||||
response_dst->dat_len = i;
|
||||
response_dst->pkg_amount = i / MAX_BLE_DAT_RESPONSE_LEN;
|
||||
if((i % MAX_BLE_DAT_RESPONSE_LEN) > 0)
|
||||
{
|
||||
response_dst->pkg_amount += 1;
|
||||
}
|
||||
response_dst->pkg_seq = 0;
|
||||
response_dst->flag = 1;
|
||||
}
|
||||
|
||||
|
||||
uint8_t set_response_buf(Buf_DBN_BLE *response_dst, uint8_t magic, uint8_t cmd, uint8_t * dat, uint8_t dat_len)
|
||||
{
|
||||
uint8_t ret = 0;
|
||||
uint8_t i = 0;
|
||||
clear_buf_dbn_ble(response_dst);
|
||||
response_dst->magic = magic;
|
||||
response_dst->cmd = cmd;
|
||||
|
||||
uint8_t _amount = dat_len / MAX_BLE_DAT_RESPONSE_LEN;
|
||||
if((dat_len % MAX_BLE_DAT_RESPONSE_LEN) > 0)
|
||||
{
|
||||
_amount++;
|
||||
}
|
||||
response_dst->pkg_amount = _amount;
|
||||
response_dst->pkg_seq = 0;
|
||||
response_dst->dat_len = dat_len;
|
||||
for(i = 0; i < dat_len; i++)
|
||||
{
|
||||
response_dst->dat[i] = dat[i];
|
||||
}
|
||||
response_dst->flag = 1;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
uint16_t compute_ckb(uint8_t header_0, uint8_t header_1, uint8_t header_cmd, uint8_t *dat, uint8_t dat_len)
|
||||
{
|
||||
uint16_t ret = 0;
|
||||
uint8_t i = 0;
|
||||
uint8_t _dxor = header_0 ^ header_1 ^ header_cmd;
|
||||
uint8_t _dsum = header_0 + header_1 + header_cmd;
|
||||
|
||||
for(i = 0; i < dat_len; i++)
|
||||
{
|
||||
_dxor ^= dat[i];
|
||||
_dsum += dat[i];
|
||||
}
|
||||
ret = _dxor;
|
||||
ret = (ret << 8) + (_dsum % 0x100);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
uint8_t set_response_tran_to_notify(uint8_t *dat_ori, uint8_t dat_len, BLE_Notify_Buf * notify_dst)
|
||||
{
|
||||
uint8_t ret = 0;
|
||||
uint8_t i = 0;
|
||||
if(g_flag_bt_state == 0){
|
||||
return ret;
|
||||
}
|
||||
if(notify_dst->flag)
|
||||
{ //dst not idle
|
||||
return ret;
|
||||
}
|
||||
if(dat_len < 6)
|
||||
{
|
||||
return ret;
|
||||
}
|
||||
|
||||
for(i = 0; i < dat_len; i++)
|
||||
{
|
||||
notify_dst->buf[i] = dat_ori[i];
|
||||
}
|
||||
notify_dst->len = dat_len;
|
||||
notify_dst->flag = 1;
|
||||
}
|
||||
|
||||
|
||||
//response_ori must set_response_buf first
|
||||
uint8_t set_response_to_notify(Buf_DBN_BLE *response_ori, BLE_Notify_Buf * notify_dst)
|
||||
{
|
||||
uint8_t ret = 0;
|
||||
uint8_t i = 0;
|
||||
uint16_t _ckbl = 0;
|
||||
if(notify_dst->flag)
|
||||
{ //dst not idle
|
||||
return ret;
|
||||
}
|
||||
if(response_ori->flag == 0)
|
||||
{ //ori idle
|
||||
return ret;
|
||||
}
|
||||
if(response_ori->pkg_amount <= 1)
|
||||
{
|
||||
//没有分包的情况
|
||||
notify_dst->buf[0] = response_ori->magic;
|
||||
if(response_ori->magic == MAGIC_BYTE_DBN_TRANSPARENT)
|
||||
{
|
||||
notify_dst->buf[1] = response_ori->pkg_seq;
|
||||
}
|
||||
else
|
||||
{
|
||||
notify_dst->buf[1] = 0;
|
||||
}
|
||||
notify_dst->buf[2] = response_ori->dat_len + 1; // 1 is cmd_byte
|
||||
notify_dst->buf[3] = response_ori->cmd;
|
||||
_ckbl = compute_ckb(notify_dst->buf[1], notify_dst->buf[2], notify_dst->buf[3], response_ori->dat, response_ori->dat_len);
|
||||
for(i = 0; i < response_ori->dat_len; i++)
|
||||
{
|
||||
notify_dst->buf[i + 4] = response_ori->dat[i];
|
||||
}
|
||||
notify_dst->buf[i + 4] = (uint8_t)(_ckbl >> 8);
|
||||
i++;
|
||||
notify_dst->buf[i + 4] = (uint8_t)(_ckbl);
|
||||
i++;
|
||||
notify_dst->len = i + 4;
|
||||
// if(g_flag_debug)
|
||||
// {
|
||||
// PRINT("set_response_to_notify_dst__\n");
|
||||
// for(i = 0; i < notify_dst->len; i++)
|
||||
// {
|
||||
// PRINT(" %02X", notify_dst->buf[i]);
|
||||
// }
|
||||
// }
|
||||
notify_dst->flag = 1;
|
||||
|
||||
clear_buf_dbn_ble(response_ori);
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
notify_dst->buf[0] = response_ori->magic;
|
||||
uint8_t _pkg_amount = response_ori->pkg_amount;
|
||||
uint8_t _pkg_seq = response_ori->pkg_seq;
|
||||
uint8_t _remain_len = response_ori->dat_len - response_ori->dat_offset;
|
||||
if(_remain_len > MAX_BLE_DAT_RESPONSE_LEN)
|
||||
{
|
||||
_remain_len = MAX_BLE_DAT_RESPONSE_LEN;
|
||||
}
|
||||
_pkg_seq += 1;
|
||||
notify_dst->buf[1] = (_pkg_amount << 4)|(_pkg_seq);
|
||||
notify_dst->buf[2] = _remain_len + 1; // 1 is cmd_byte
|
||||
notify_dst->buf[3] = response_ori->cmd;
|
||||
|
||||
_ckbl = compute_ckb(notify_dst->buf[1], notify_dst->buf[2], notify_dst->buf[3],
|
||||
&(response_ori->dat[response_ori->dat_offset]), _remain_len);
|
||||
for(i = 0; i < _remain_len; i++)
|
||||
{
|
||||
notify_dst->buf[i + 4] = response_ori->dat[response_ori->dat_offset + i];
|
||||
}
|
||||
notify_dst->buf[i + 4] = (uint8_t)(_ckbl >> 8);
|
||||
i++;
|
||||
notify_dst->buf[i + 4] = (uint8_t)(_ckbl);
|
||||
i++;
|
||||
|
||||
response_ori->dat_offset += _remain_len;
|
||||
response_ori->pkg_seq += 1;
|
||||
notify_dst->len = i + 4;
|
||||
|
||||
|
||||
// if(g_flag_debug)
|
||||
// {
|
||||
// PRINT("\nset_notify_dst,amount:%d,seq:%d,offset:%d\n", response_ori->pkg_amount,response_ori->pkg_seq, response_ori->dat_offset);
|
||||
// for(i = 0; i < notify_dst->len; i++)
|
||||
// {
|
||||
// PRINT(" %02X", notify_dst->buf[i]);
|
||||
// }
|
||||
// PRINT("\n");
|
||||
// }
|
||||
|
||||
notify_dst->flag = 1;
|
||||
if(response_ori->pkg_amount == response_ori->pkg_seq)
|
||||
{
|
||||
clear_buf_dbn_ble(response_ori);
|
||||
}
|
||||
return 1;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
static uint8_t unpack_packs(uint8_t *pkg, uint8_t len)
|
||||
{
|
||||
static uint8_t _offset = 0;
|
||||
uint8_t _header_0 = pkg[1];
|
||||
uint8_t _header_0_high = _header_0 >> 4;
|
||||
uint8_t _header_0_low = _header_0 % 0x10;
|
||||
uint8_t _len = pkg[2];
|
||||
uint8_t _cmd = pkg[3];
|
||||
uint8_t i = 0, j = 0, k = 0;
|
||||
uint8_t _amount = 0;
|
||||
|
||||
if(_header_0_high)
|
||||
{
|
||||
if(_header_0_low == 1)
|
||||
{
|
||||
g_buf_ble_response.magic = pkg[0];
|
||||
g_buf_ble_response.pkg_amount = _header_0_high;
|
||||
g_buf_ble_response.pkg_seq = _header_0_low;
|
||||
g_buf_ble_response.cmd = _cmd;
|
||||
memcpy(&g_buf_ble_response.dat, &pkg[4], _len - 1);
|
||||
_offset = _len - 1;
|
||||
g_buf_ble_response.dat_len = _len - 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
if((g_buf_ble_response.pkg_amount != _header_0_high) || (g_buf_ble_response.cmd != _cmd))
|
||||
{
|
||||
//printf("\nNot_the_right_sub_pack!\n");
|
||||
clear_buf_dbn_ble_all(&g_buf_ble_response);
|
||||
return 0;
|
||||
}
|
||||
if(g_buf_ble_response.pkg_seq != (_header_0_low -1))
|
||||
{
|
||||
//printf("\nNot_the_right_sub_pack!!\n");
|
||||
clear_buf_dbn_ble_all(&g_buf_ble_response);
|
||||
return 0;
|
||||
}
|
||||
g_buf_ble_response.pkg_seq = _header_0_low;
|
||||
g_buf_ble_response.dat_len += (_len - 1); // not include cmd_byte
|
||||
memcpy(&g_buf_ble_response.dat[_offset], &pkg[4], _len - 1);
|
||||
_offset += (_len - 1);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
clear_buf_dbn_ble(&g_buf_ble_response);
|
||||
g_buf_ble_response.magic = pkg[0];
|
||||
g_buf_ble_response.pkg_amount = 1;
|
||||
g_buf_ble_response.pkg_seq = 0;
|
||||
g_buf_ble_response.cmd = _cmd;
|
||||
g_buf_ble_response.dat_len = (_len - 1);
|
||||
memcpy(g_buf_ble_response.dat, &pkg[4], _len - 1);
|
||||
}
|
||||
|
||||
if(_header_0_high == _header_0_low)
|
||||
{
|
||||
// received end.
|
||||
// if(g_flag_debug)
|
||||
// {
|
||||
// printf("\nReceive_pack_over:\n");
|
||||
// for(i = 0; i < g_buf_ble_response.dat_len; i++)
|
||||
// {
|
||||
// printf(" %02X", g_buf_ble_response.dat[i]);
|
||||
// }
|
||||
// printf("\nEnd\n");
|
||||
// }
|
||||
|
||||
|
||||
if(g_buf_ble_response.cmd == CMD_DBN_CHECK_PASS)
|
||||
{
|
||||
memset(tmp_ble_buf, 0, MAX_BLE_TMP_BUF_LEN);
|
||||
if(g_buf_ble_response.dat_len != 6)
|
||||
{
|
||||
//response pass failed
|
||||
tmp_ble_buf[0] = 0x01;
|
||||
set_response_buf(&g_buf_ble_response, MAGIC_BYTE_DBN_DEFAULT, _cmd, tmp_ble_buf, 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
char _pass = check_ble_safe_pass(g_buf_ble_response.dat);
|
||||
if(_pass)
|
||||
{
|
||||
//response ok
|
||||
tmp_ble_buf[0] = 0x0;
|
||||
set_response_buf(&g_buf_ble_response, MAGIC_BYTE_DBN_DEFAULT, _cmd, tmp_ble_buf, 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
//response failed
|
||||
tmp_ble_buf[0] = 0x01;
|
||||
set_response_buf(&g_buf_ble_response, MAGIC_BYTE_DBN_DEFAULT, _cmd, tmp_ble_buf, 1);
|
||||
}
|
||||
}
|
||||
|
||||
} //end if g_buf_ble_response.cmd == CMD_DBN_CHECK_PASS
|
||||
else if(g_buf_ble_response.cmd == CMD_DBN_MODIFY_PASS)
|
||||
{
|
||||
memset(tmp_ble_buf, 0, MAX_BLE_TMP_BUF_LEN);
|
||||
if(g_buf_ble_response.dat_len != 12)
|
||||
{
|
||||
//response failed
|
||||
tmp_ble_buf[0] = 0x01;
|
||||
}
|
||||
else
|
||||
{
|
||||
char _pass = check_ble_safe_pass(g_buf_ble_response.dat);
|
||||
if(_pass)
|
||||
{
|
||||
//response ok
|
||||
set_ble_safe_pass(&g_buf_ble_response.dat[6]);
|
||||
tmp_ble_buf[0] = 0x00;
|
||||
}
|
||||
else
|
||||
{
|
||||
//response failed
|
||||
tmp_ble_buf[0] = 0x01;
|
||||
}
|
||||
}
|
||||
set_response_buf(&g_buf_ble_response, MAGIC_BYTE_DBN_DEFAULT, _cmd, tmp_ble_buf, 1);
|
||||
} //end if g_buf_ble_response.cmd == CMD_DBN_MODIFY_PASS
|
||||
else if(g_buf_ble_response.cmd == CMD_DBN_SET_IOT_NET)
|
||||
{
|
||||
for(i = 0, j = 0, k = 0; i < g_buf_ble_response.dat_len; i++)
|
||||
{
|
||||
if(g_buf_ble_response.dat[i] == 0x00)
|
||||
{
|
||||
j++;
|
||||
if(k == 0)
|
||||
{
|
||||
//iot _host
|
||||
PRINT("Rcv_Host:%s\n", tmp_ble_buf);
|
||||
memcpy(iot_net_info.remote_addr, tmp_ble_buf, j);
|
||||
|
||||
}
|
||||
else if(k == 1)
|
||||
{
|
||||
PRINT("Rcv_Port:%s\n", tmp_ble_buf);
|
||||
uint16_t _port = 0;
|
||||
for(j = 0; j < strlen(tmp_ble_buf); j++)
|
||||
{
|
||||
_port *= 10;
|
||||
_port += tmp_ble_buf[j] - 0x30;
|
||||
}
|
||||
iot_net_info.mqtt_port = _port;
|
||||
}
|
||||
else if(k == 2)
|
||||
{
|
||||
PRINT("Rcv_Client:%s\n", tmp_ble_buf);
|
||||
if((strlen(tmp_ble_buf) == 1) && (tmp_ble_buf[0] == 0x20))
|
||||
{
|
||||
PRINT("Client_use local_serial\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
memcpy(iot_net_info.client_id, tmp_ble_buf, j);
|
||||
}
|
||||
}
|
||||
else if(k == 3)
|
||||
{
|
||||
PRINT("Rcv_Username:%s\n", tmp_ble_buf);
|
||||
memcpy(iot_net_info.username, tmp_ble_buf, j);
|
||||
}
|
||||
else if(k == 4)
|
||||
{
|
||||
PRINT("Rcv_Pass:%s\n", tmp_ble_buf);
|
||||
memcpy(iot_net_info.password, tmp_ble_buf, j);
|
||||
}
|
||||
k++;
|
||||
memset(tmp_ble_buf, 0, MAX_BLE_TMP_BUF_LEN);
|
||||
j = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
tmp_ble_buf[j] = g_buf_ble_response.dat[i];
|
||||
j++;
|
||||
}
|
||||
}
|
||||
if(k == 4)
|
||||
{
|
||||
PRINT("Rcv_Pass:%s\n", tmp_ble_buf);
|
||||
|
||||
memcpy(iot_net_info.password, tmp_ble_buf, strlen(tmp_ble_buf) + 1);
|
||||
}
|
||||
memset(tmp_ble_buf, 0, MAX_BLE_TMP_BUF_LEN);
|
||||
write_net_config(&local_net_cfg, &net_center_info, &iot_net_info, &g_iot_topic);
|
||||
|
||||
// response finally
|
||||
set_response_buf(&g_buf_ble_response, MAGIC_BYTE_DBN_DEFAULT, _cmd, tmp_ble_buf, 1);
|
||||
//clear_buf_dbn_ble_all(&g_buf_ble_response);
|
||||
} //end if CMD_DBN_SET_IOT_NET
|
||||
else if(g_buf_ble_response.cmd == CMD_DBN_SET_IOT_TOPIC)
|
||||
{
|
||||
for(i = 0, j = 0, k = 0; i < g_buf_ble_response.dat_len; i++)
|
||||
{
|
||||
if(g_buf_ble_response.dat[i] == 0x00)
|
||||
{
|
||||
//j++;
|
||||
if(k == 0)
|
||||
{
|
||||
g_iot_topic.clientid_enable = tmp_ble_buf[0] - 0x30;
|
||||
}
|
||||
else if(k == 1)
|
||||
{
|
||||
// if(g_flag_debug)
|
||||
// {
|
||||
// printf("rcv_topic_pub_:%s\n", tmp_ble_buf);
|
||||
// for(f = 0; f < j; f++)
|
||||
// {
|
||||
// printf(" %02X", tmp_ble_buf[f]);
|
||||
// }
|
||||
// printf(", j:%d\n", j);
|
||||
// }
|
||||
memset(g_iot_topic.topic_pub, 0, MAX_TOPIC_LENGTH);
|
||||
memcpy(g_iot_topic.topic_pub, tmp_ble_buf, j + 1);
|
||||
}
|
||||
|
||||
k++;
|
||||
j = 0;
|
||||
memset(tmp_ble_buf, 0, MAX_BLE_TMP_BUF_LEN);
|
||||
}
|
||||
else
|
||||
{
|
||||
tmp_ble_buf[j] = g_buf_ble_response.dat[i];
|
||||
j++;
|
||||
}
|
||||
}
|
||||
if(k == 2)
|
||||
{
|
||||
// if(g_flag_debug)
|
||||
// {
|
||||
// printf("rcv_topic_sub_:%s\n", tmp_ble_buf);
|
||||
// }
|
||||
memset(g_iot_topic.topic_sub, 0, MAX_TOPIC_LENGTH);
|
||||
memcpy(g_iot_topic.topic_sub, tmp_ble_buf, strlen(tmp_ble_buf) + 1);
|
||||
}
|
||||
memset(tmp_ble_buf, 0, MAX_BLE_TMP_BUF_LEN);
|
||||
write_net_config(&local_net_cfg, &net_center_info, &iot_net_info, &g_iot_topic);
|
||||
|
||||
// response finally
|
||||
set_response_buf(&g_buf_ble_response, MAGIC_BYTE_DBN_DEFAULT, _cmd, tmp_ble_buf, 1);
|
||||
} // end if CMD_DBN_SET_IOT_TOPIC
|
||||
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void manage_dbn_ble_default(uint8_t *pkg, uint8_t len)
|
||||
{
|
||||
uint8_t _header_0 = pkg[1];
|
||||
uint8_t _header_0_high = 0;
|
||||
uint8_t _header_0_low = _header_0 % 0x10;
|
||||
uint8_t _len = pkg[2];
|
||||
uint8_t _cmd = pkg[3];
|
||||
uint8_t _tmp = 0;
|
||||
uint8_t i = 0, k = 0;
|
||||
|
||||
memset(tmp_ble_buf, 0, MAX_BLE_TMP_BUF_LEN);
|
||||
|
||||
switch(_cmd)
|
||||
{
|
||||
case CMD_DBN_GET_DEV_INFO:
|
||||
{
|
||||
i = set_dev_info_to_ready(tmp_ble_buf);
|
||||
|
||||
set_response_buf(&g_buf_ble_response, MAGIC_BYTE_DBN_DEFAULT, _cmd, tmp_ble_buf, i);
|
||||
|
||||
// printf("\nGet_dev_info\n");
|
||||
} break;
|
||||
case CMD_DBN_SET_LSSC_NET:
|
||||
{
|
||||
//printf("Set_Net_info_len:%d\n", len);
|
||||
if(len < 34)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
i = 4;
|
||||
memcpy(local_net_cfg.lip, &pkg[i], 4);
|
||||
i += 4;
|
||||
memcpy(local_net_cfg.sub, &pkg[i], 4);
|
||||
i += 4;
|
||||
memcpy(local_net_cfg.gw, &pkg[i], 4);
|
||||
i += 4;
|
||||
memcpy(net_center_info.lssc_ip, &pkg[i], 4);
|
||||
//i += 4;
|
||||
//printf("Reset_lip:%d\n",local_net_cfg.lip[3]);
|
||||
i = 24;
|
||||
//printf("Reset_lport:%02X %02X\n",pkg[24], pkg[25]);
|
||||
//memcpy(local_net_cfg.dns
|
||||
// local_net_cfg.port_ssc_tcp = *(uint16_t *)(&pkg[24]); //TOOD: Will err, why?
|
||||
local_net_cfg.port_ssc_tcp = (pkg[25] << 8) | pkg[24];// This is OK
|
||||
i += 2;
|
||||
// local_net_cfg.port_ssc_udp = *(uint16_t *)(&pkg[i]);
|
||||
local_net_cfg.port_ssc_udp =(pkg[27] <<8 ) | pkg[26];
|
||||
i += 2;
|
||||
// local_net_cfg.port_ssc_udp_message = *(uint16_t *)(&pkg[i]);
|
||||
local_net_cfg.port_ssc_udp_message = (pkg[29] << 8) | pkg[28];
|
||||
i += 2;
|
||||
// local_net_cfg.port_dev_tcp = *(uint16_t *)(&pkg[i]);
|
||||
local_net_cfg.port_dev_tcp = (pkg[31] << 8) | pkg[30];
|
||||
net_center_info.tcp_port = local_net_cfg.port_dev_tcp;
|
||||
i += 2;
|
||||
// local_net_cfg.port_dev_udp = *(uint16_t *)(&pkg[32]);
|
||||
local_net_cfg.port_dev_udp = (pkg[33] << 8) | pkg[32];
|
||||
//printf("Will_reset_net_param___\n");
|
||||
write_net_config(&local_net_cfg, &net_center_info, &iot_net_info, &g_iot_topic);
|
||||
//response
|
||||
tmp_ble_buf[0] = 0;
|
||||
set_response_buf(&g_buf_ble_response, MAGIC_BYTE_DBN_DEFAULT, _cmd, tmp_ble_buf, 1);
|
||||
|
||||
} break;
|
||||
case CMD_DBN_GET_LSSC_NET:
|
||||
{
|
||||
i = set_net_info_to_ready(tmp_ble_buf);
|
||||
set_response_buf(&g_buf_ble_response, MAGIC_BYTE_DBN_DEFAULT, _cmd, tmp_ble_buf, i);
|
||||
// printf("Get_Net_Info\n");
|
||||
} break;
|
||||
case CMD_DBN_GET_IOT_NET:
|
||||
{
|
||||
set_response_iot_net(&g_buf_ble_response);
|
||||
} break;
|
||||
case CMD_DBN_SET_IOT_NET:
|
||||
{
|
||||
unpack_packs(pkg, len);
|
||||
} break;
|
||||
case CMD_DBN_SET_IOT_TOPIC:
|
||||
{
|
||||
unpack_packs(pkg, len);
|
||||
} break;
|
||||
case CMD_DBN_GET_IOT_TOPIC:
|
||||
{
|
||||
set_response_iot_topic(&g_buf_ble_response);
|
||||
} break;
|
||||
case CMD_DBN_RESET_DEV:
|
||||
{
|
||||
//Restart Dev
|
||||
NVIC_SystemReset();
|
||||
} break;
|
||||
case CMD_DBN_CHECK_PASS:
|
||||
{
|
||||
unpack_packs(pkg, len);
|
||||
} break;
|
||||
case CMD_DBN_SET_CJQ_PARAM:
|
||||
{
|
||||
// PRINT("\nSet_Cjq_param\n");
|
||||
// unpack_pkg_set_cjq_param(pkg, len);
|
||||
tmp_ble_buf[0] = 0;
|
||||
set_response_buf(&g_buf_ble_response, MAGIC_BYTE_DBN_DEFAULT, _cmd, tmp_ble_buf, 1);
|
||||
|
||||
// para_store_init();
|
||||
} break;
|
||||
case CMD_DBN_GET_CJQ_PARAM:
|
||||
{
|
||||
i = 0;
|
||||
|
||||
set_response_buf(&g_buf_ble_response, MAGIC_BYTE_DBN_DEFAULT, _cmd, tmp_ble_buf, i);
|
||||
// PRINT("\nGet_Cjq_param\n");
|
||||
} break;
|
||||
case CMD_DBN_LOOP_SENS_LIST: {
|
||||
// PRINT("SENS_CNG: %02X\n", pkg[4]);
|
||||
memset(tmp_ble_buf, 0, MAX_BLE_TMP_BUF_LEN);
|
||||
|
||||
|
||||
} break;
|
||||
case CMD_DBN_SET_FACTORY:
|
||||
case CMD_DBN_SET_CJQ_FACTORY: {
|
||||
// 出厂初始化
|
||||
tmp_ble_buf[0] = 0;
|
||||
set_response_buf(&g_buf_ble_response, MAGIC_BYTE_DBN_DEFAULT, _cmd, tmp_ble_buf, 1);
|
||||
factory_dev_info();
|
||||
|
||||
Delay_Ms(10);
|
||||
|
||||
//TODO: 不整个设备复位,复位除了蓝牙的部分,即蓝牙正常,其他部分重新初始化
|
||||
|
||||
} break;
|
||||
case CMD_DBN_RW_UART_BAUD:{ //
|
||||
uint8_t _rw = pkg[4];
|
||||
uint32_t _baud = 0;
|
||||
i = 0;
|
||||
tmp_ble_buf[i++] = _rw;
|
||||
tmp_ble_buf[i++] = 0x01; //status
|
||||
|
||||
if(_rw == 0){// read baud
|
||||
tmp_ble_buf[i++] = g_max_counter_bt_min; // Idle Ble timeout
|
||||
tmp_ble_buf[i++] = (uint8_t)g_storage_uart_baud; //1 g_storage_uart_baud? 2 g_storage_uart_baud_2
|
||||
tmp_ble_buf[i++] = (uint8_t)(g_storage_uart_baud >> 8);
|
||||
tmp_ble_buf[i++] = (uint8_t)(g_storage_uart_baud >> 16);
|
||||
tmp_ble_buf[i++] = 0x02;
|
||||
tmp_ble_buf[i++] = (uint8_t)g_storage_uart_baud_2;
|
||||
tmp_ble_buf[i++] = (uint8_t)(g_storage_uart_baud_2 >> 8);
|
||||
tmp_ble_buf[i++] = (uint8_t)(g_storage_uart_baud_2 >> 16);
|
||||
// 4BYte Reserved
|
||||
|
||||
}
|
||||
else { // write baud
|
||||
alter_dev_baud(&pkg[5]); //
|
||||
g_storage_uart_num = pkg[5];
|
||||
g_storage_uart_baud = pkg[6];
|
||||
g_storage_uart_baud |= (uint16_t)(pkg[7] << 8) & 0xFF00;
|
||||
g_storage_uart_baud |= (uint32_t)(pkg[8] << 16) & 0xFF0000;
|
||||
|
||||
tmp_ble_buf[i++] = g_storage_uart_baud;
|
||||
tmp_ble_buf[i++] = (uint8_t)g_storage_uart_baud;
|
||||
tmp_ble_buf[i++] = (uint8_t)(g_storage_uart_baud >> 8);
|
||||
tmp_ble_buf[i++] = (uint8_t)(g_storage_uart_baud >> 16);
|
||||
tmp_ble_buf[i++] = 0x00; tmp_ble_buf[i++] = 0x00; tmp_ble_buf[i++] = 0x00; tmp_ble_buf[i++] = 0x00; // 4BYte Reserved
|
||||
}
|
||||
set_response_buf(&g_buf_ble_response, MAGIC_BYTE_DBN_DEFAULT, _cmd, tmp_ble_buf, i);
|
||||
} break;
|
||||
case CMD_DBN_UPDATE_DEV_SERIAL:
|
||||
{
|
||||
alter_dev_serila(&pkg[4]);
|
||||
//response
|
||||
tmp_ble_buf[0] = 1;
|
||||
set_response_buf(&g_buf_ble_response, MAGIC_BYTE_DBN_DEFAULT, _cmd, tmp_ble_buf, 1);
|
||||
} break;
|
||||
case CMD_SENS_ACS_ENABLE: {
|
||||
// 线圈动态
|
||||
g_dbn_ble_state_acs_enable.enable = pkg[5];
|
||||
|
||||
if(g_dbn_ble_state_acs_enable.enable){
|
||||
g_dbn_ble_state_acs_enable.interval = pkg[6] * 0x7F;
|
||||
g_dbn_ble_state_acs_enable.timeout_min = pkg[7];
|
||||
if(g_dbn_ble_state_acs_enable.timeout_min){
|
||||
g_dbn_ble_state_acs_enable.counter = mstick();
|
||||
g_dbn_ble_state_acs_enable.timeout_counter = g_dbn_ble_state_acs_enable.timeout_min * 60 * 100; // TODO: min --> ms
|
||||
}
|
||||
g_dbn_ble_state_acs_enable.flag = 1;
|
||||
}
|
||||
else{
|
||||
g_dbn_ble_state_acs_enable.flag = 0;
|
||||
}
|
||||
PRINT("Rcv_acs_enable_flag: %d\n", g_dbn_ble_state_acs_enable.flag);
|
||||
} break;
|
||||
default:
|
||||
{
|
||||
} break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
void manage_dbn_ble_transparent(uint8_t *pkg, uint8_t len)
|
||||
{
|
||||
//
|
||||
// transparent_485(pkg, len);
|
||||
// UART1_SendString(pkg, len);
|
||||
UART2_SendString(pkg, len);
|
||||
}
|
||||
|
||||
void poll_dbn_ble(void)
|
||||
{
|
||||
uint8_t i = 0;
|
||||
uint8_t magic = 0;
|
||||
if(g_ble_rcv_buf.flag)
|
||||
{
|
||||
if(g_flag_debug && g_flag_counter_ota.flag == 0)
|
||||
{
|
||||
PRINT("profile ChangeCB CHAR1?..:%s, len:%d \n", g_ble_rcv_buf.buf, g_ble_rcv_buf.len);
|
||||
for(i = 0; i < g_ble_rcv_buf.len; i++)
|
||||
{
|
||||
PRINT(" %02X", g_ble_rcv_buf.buf[i]);
|
||||
}
|
||||
PRINT("\n");
|
||||
}
|
||||
if(g_flag_counter_ota.flag == 0){
|
||||
magic = check_pkg(g_ble_rcv_buf.buf, g_ble_rcv_buf.len);
|
||||
}
|
||||
|
||||
// printf("_magic: %02X\n", magic);
|
||||
if(g_flag_counter_ota.flag){
|
||||
manage_dbn_ble_transparent(g_ble_rcv_buf.buf, g_ble_rcv_buf.len);
|
||||
}
|
||||
else{
|
||||
if(magic != 0)
|
||||
{
|
||||
if(magic == MAGIC_BYTE_DBN_DEFAULT)
|
||||
{
|
||||
manage_dbn_ble_default(g_ble_rcv_buf.buf, g_ble_rcv_buf.len);
|
||||
}
|
||||
else if (magic == MAGIC_BYTE_DBN_TRANSPARENT)
|
||||
{
|
||||
manage_dbn_ble_transparent(g_ble_rcv_buf.buf, g_ble_rcv_buf.len);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// memcpy(g_notify_buftemp.buf, g_ble_rcv_buf.buf, g_ble_rcv_buf.len);
|
||||
// g_notify_buftemp.len = g_ble_rcv_buf.len;
|
||||
// g_flag_notify_temp = 1;
|
||||
clear_ble_notify_buf(&g_ble_rcv_buf);
|
||||
}
|
||||
if(g_notify_buftemp.flag == 0)
|
||||
{
|
||||
// report_sens_acs();
|
||||
g_flag_notify_temp = set_response_to_notify(&g_buf_ble_response, &g_notify_buftemp);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
/********************************** (C) COPYRIGHT *******************************
|
||||
* File Name : ch32v20x_conf.h
|
||||
* Author : WCH
|
||||
* Version : V1.0.0
|
||||
* Date : 2022/06/16
|
||||
* Description : Library configuration file.
|
||||
*********************************************************************************
|
||||
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
|
||||
* Attention: This software (modified or not) and binary are used for
|
||||
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
|
||||
*******************************************************************************/
|
||||
|
||||
#ifndef __CH32V20x_CONF_H
|
||||
#define __CH32V20x_CONF_H
|
||||
|
||||
#include "ch32v20x_adc.h"
|
||||
#include "ch32v20x_bkp.h"
|
||||
#include "ch32v20x_can.h"
|
||||
#include "ch32v20x_crc.h"
|
||||
#include "ch32v20x_dbgmcu.h"
|
||||
#include "ch32v20x_dma.h"
|
||||
#include "ch32v20x_exti.h"
|
||||
#include "ch32v20x_flash.h"
|
||||
#include "ch32v20x_gpio.h"
|
||||
#include "ch32v20x_i2c.h"
|
||||
#include "ch32v20x_iwdg.h"
|
||||
#include "ch32v20x_pwr.h"
|
||||
#include "ch32v20x_rcc.h"
|
||||
#include "ch32v20x_rtc.h"
|
||||
#include "ch32v20x_spi.h"
|
||||
#include "ch32v20x_tim.h"
|
||||
#include "ch32v20x_usart.h"
|
||||
#include "ch32v20x_wwdg.h"
|
||||
#include "ch32v20x_it.h"
|
||||
#include "ch32v20x_misc.h"
|
||||
|
||||
#endif /* __CH32V20x_CONF_H */
|
||||
@@ -0,0 +1,18 @@
|
||||
/********************************** (C) COPYRIGHT *******************************
|
||||
* File Name : ch32v20x_it.h
|
||||
* Author : WCH
|
||||
* Version : V1.0.0
|
||||
* Date : 2022/06/16
|
||||
* Description : This file contains the headers of the interrupt handlers.
|
||||
*********************************************************************************
|
||||
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
|
||||
* Attention: This software (modified or not) and binary are used for
|
||||
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
|
||||
*******************************************************************************/
|
||||
|
||||
#ifndef __CH32V20x_IT_H
|
||||
#define __CH32V20x_IT_H
|
||||
|
||||
#include "debug.h"
|
||||
|
||||
#endif /* __CH32V20x_IT_H */
|
||||
165
vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/include/cmcng.h
Normal file
165
vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/include/cmcng.h
Normal file
@@ -0,0 +1,165 @@
|
||||
/*
|
||||
* cmcng.h
|
||||
*
|
||||
* Created on: Aug 27, 2024
|
||||
* Author: Thinkpad
|
||||
*/
|
||||
|
||||
#ifndef INCLUDE_CMCNG_H_
|
||||
#define INCLUDE_CMCNG_H_
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#define PRODUCT_MODEL "DLD960GA"
|
||||
#define FIRMWARE_VER "1.0"
|
||||
#define HARDWARE_VER "1.0"
|
||||
#define FIRMWARE_VER_MAIN 1
|
||||
#define FIRMWARE_VER_SUB 1
|
||||
#define HARDWARE_VER_MAIN 1
|
||||
#define HARDWARE_VER_SUB 1
|
||||
|
||||
|
||||
#define BUFF_STACK_SIZE 512 //128
|
||||
|
||||
#define MAX_UART_TXRX_LEN 32
|
||||
|
||||
#define MAX_TOUCHUAN_DISABLE_TIME 15
|
||||
#define BT_DISABLE_IDLE_TIMEOUT 0 //10 // 蓝牙空闲超时 关闭时间,默认10 min
|
||||
|
||||
|
||||
typedef enum
|
||||
{
|
||||
DDType_DNT900 = 1, //1 ???????? DNT900
|
||||
DDType_WireDUS, //2???ù?¨???????? DG
|
||||
DDType_4G_1,
|
||||
DDType_WireDus_Wb, //4 ???ù?¨???????? ?ò??
|
||||
DDType_LoraAreaMain, //5 Lora ?????÷?ú
|
||||
DDType_LoraAreaSub, //6 Lora ???????ú
|
||||
DDType_LoraLotMain, //7 Lora ?????÷?ú
|
||||
DDType_LoraLotSub, //8 Lora ???????ú
|
||||
DDType_LoraBroadcast, //9 Lora ????????
|
||||
DDType_LoraRelay, //10 Lora ?????? ???? DLR101
|
||||
DDType_Relay, //11 ?????????? ?¨??RS485-2 ??·?
|
||||
DDType_4G_IOT, //12 4G ??±¨??????????????MQTT
|
||||
DDType_Wire_Collect_IOT, //13 ????????±¨??????????????MQTT
|
||||
DDType_Wire_Display_IOT, //14 ????????·?????????MQTT
|
||||
DDType_Radar_Lot, //15 ?×??????
|
||||
|
||||
DDType_DLD950 = 20, //DLD950
|
||||
DDType_DBN101 = 21,
|
||||
DDType_DNT910 = 22, //DNT910,DNT920
|
||||
DDType_DNT920 = 23,
|
||||
DDType_DLD950V4 = 24,
|
||||
}DG_Device_Type;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
KEY_ET_None = 0,
|
||||
KEY_ET_BLE_ENABLE, //1
|
||||
KEY_ET_REBOOT,
|
||||
KEY_ET_FACTORY,
|
||||
KEY_ET_NETBLE_FACTORY
|
||||
} KEY_EVENT_Type;
|
||||
|
||||
typedef struct _SUB_CODE_ENABLE_
|
||||
{
|
||||
uint16_t code_set;
|
||||
uint8_t net_enable;
|
||||
uint8_t iot_enable;
|
||||
uint8_t custom_enable;
|
||||
uint8_t loop_enable; // ?÷??????
|
||||
uint8_t dgdus_enable;
|
||||
uint8_t wbdus_enable;
|
||||
uint8_t radar_enable;
|
||||
uint8_t laser_enable; // lora?¤???×??????
|
||||
uint8_t lora_enable; // Lora ????
|
||||
//uint8_t input_enable;
|
||||
} Sub_Code_Enable;
|
||||
|
||||
extern Sub_Code_Enable g_sub_code_enable;
|
||||
|
||||
|
||||
typedef struct _PKG_UART_
|
||||
{
|
||||
uint8_t flag;
|
||||
uint16_t offset;
|
||||
uint16_t len;
|
||||
uint8_t pkg[BUFF_STACK_SIZE];
|
||||
uint32_t tick;
|
||||
}Pkg_Uart;
|
||||
extern Pkg_Uart g_pkg_uart_1;
|
||||
extern Pkg_Uart g_pkg_uart_2;
|
||||
|
||||
|
||||
#define RX_BUFFER_LEN BUFF_STACK_SIZE
|
||||
|
||||
typedef struct
|
||||
{
|
||||
volatile uint8_t DMA_USE_BUFFER;
|
||||
uint8_t Rx_Buffer[2][RX_BUFFER_LEN];
|
||||
|
||||
} USART_DMA_UNIT;
|
||||
|
||||
|
||||
|
||||
typedef struct _DBN_BLE_STATE_
|
||||
{
|
||||
uint8_t flag; // if need to report
|
||||
uint8_t enable; // 是否使能
|
||||
uint8_t send_flag;
|
||||
uint8_t obj_amount;
|
||||
uint8_t cmd;
|
||||
uint8_t sens_type;
|
||||
uint8_t dat_len ;
|
||||
uint8_t dat_offset;
|
||||
uint8_t pkg_amount;
|
||||
uint8_t pkg_seq;
|
||||
uint16_t interval;
|
||||
uint32_t counter;
|
||||
uint8_t timeout_min; // minute
|
||||
uint32_t timeout_counter;
|
||||
} DBN_BLE_State;
|
||||
|
||||
extern DBN_BLE_State g_dbn_ble_state_acs_enable;
|
||||
|
||||
typedef struct _FLAG_COUNTER_
|
||||
{
|
||||
uint32_t flag;
|
||||
uint32_t timeout;
|
||||
uint32_t tick;
|
||||
} Flag_Counter;
|
||||
extern Flag_Counter g_flag_counter_key;
|
||||
extern Flag_Counter g_flag_counter_ota;
|
||||
|
||||
|
||||
extern char g_flag_debug;
|
||||
extern uint8_t g_dev_number[6];
|
||||
extern uint8_t g_dev_password[6];
|
||||
extern uint8_t g_ble_safe_flag;
|
||||
extern uint32_t g_ble_safe_counter_ori;
|
||||
extern uint32_t g_ble_safe_counter_dst;
|
||||
// extern uint8_t MACAddr[6];
|
||||
extern uint8_t gMacAddr[6];
|
||||
extern char g_dev_number_str[13];
|
||||
extern uint32_t g_activ_counter;
|
||||
extern uint8_t g_dg_sub_dev_type;
|
||||
extern uint8_t g_dg_device_type;
|
||||
extern uint8_t g_max_counter_bt_min;
|
||||
extern uint32_t g_max_counter_bt_timeout;
|
||||
|
||||
extern uint8_t g_storage_uart_num;
|
||||
extern uint8_t g_storage_uart_num_2;
|
||||
extern uint32_t g_storage_uart_baud;
|
||||
extern uint32_t g_storage_uart_baud_2;
|
||||
|
||||
extern uint8_t g_flag_bt_state;
|
||||
|
||||
uint32_t mstick(void);
|
||||
void InitPkgUart(Pkg_Uart * pkg);
|
||||
|
||||
void uart_init(void);
|
||||
void uart_srv(void);
|
||||
void UART2_SendString(uint8_t *buf, uint16_t len);
|
||||
void UART1_SendString(uint8_t *buf, uint16_t len);
|
||||
|
||||
#endif /* INCLUDE_CMCNG_H_ */
|
||||
124
vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/include/dbn_ble_srv.h
Normal file
124
vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/include/dbn_ble_srv.h
Normal file
@@ -0,0 +1,124 @@
|
||||
/*
|
||||
* dbn_ble_srv.h
|
||||
*
|
||||
* Created on: Aug 27, 2024
|
||||
* Author: Thinkpad
|
||||
*/
|
||||
|
||||
#ifndef INCLUDE_DBN_BLE_SRV_H_
|
||||
#define INCLUDE_DBN_BLE_SRV_H_
|
||||
|
||||
#include "config.h"
|
||||
#include <stdint.h>
|
||||
|
||||
/*
|
||||
* Magic | Header | Data | CheckByte
|
||||
* | Addr/Sub Len CMD | | Xor Sum
|
||||
* 1Byte | 1B 1B 1B | xx | 1B 1B
|
||||
*
|
||||
* * Len = len(CMD) + len(Data)
|
||||
*/
|
||||
#define MAGIC_BYTE_DBN_DEFAULT 0x8F
|
||||
#define MAGIC_BYTE_DBN_TRANSPARENT 0x7F // 用于 显示屏协议、地感协议、探头协议等
|
||||
#define MAGIC_BYTE_DBN_SUB_OTA 0x9F // 用于 透传子设备OTA升级
|
||||
|
||||
|
||||
#define CMD_DBN_UPDATE_DEV_SERIAL 0x09
|
||||
#define CMD_DBN_GET_DEV_INFO 0x10
|
||||
#define CMD_DBN_SET_LSSC_NET 0x11
|
||||
#define CMD_DBN_GET_LSSC_NET 0x12
|
||||
#define CMD_DBN_SET_IOT_NET 0x13
|
||||
#define CMD_DBN_GET_IOT_NET 0x14
|
||||
#define CMD_DBN_SET_IOT_TOPIC 0x15
|
||||
#define CMD_DBN_GET_IOT_TOPIC 0x16
|
||||
#define CMD_DBN_SET_SUB_TRAFFIC_PARAM 0x17
|
||||
#define CMD_DBN_GET_SUB_TRAFFIC_PARAM 0x18
|
||||
|
||||
|
||||
#define CMD_DBN_CHECK_PASS 0x1C
|
||||
#define CMD_DBN_MODIFY_PASS 0x1D
|
||||
|
||||
|
||||
|
||||
#define CMD_DBN_SET_FACTORY 0x1E
|
||||
#define CMD_DBN_RESET_DEV 0x1F
|
||||
|
||||
#define CMD_DBN_SET_NOTIFY 0x20
|
||||
#define CMD_DBN_SET_NOTIFY_LOOP 0x21
|
||||
#define CMD_DBN_SET_SUB_CODE 0x22 //使能 子功能
|
||||
#define CMD_DBN_SET_CJQ_PARAM 0x23 // 设置 车检器参数
|
||||
#define CMD_DBN_GET_CJQ_PARAM 0x24 // 读取 车检器参数
|
||||
|
||||
|
||||
#define CMD_DBN_RW_UART_BAUD 0x31
|
||||
|
||||
|
||||
#define CMD_DBN_LOOP_SAMPLE_PARAM 0x87
|
||||
#define CMD_DBN_LOOP_BALANCE_PARAM 0x88
|
||||
#define CMD_DBN_LOOP_RELEASE_PLANB 0x89
|
||||
#define CMD_DBN_LOOP_SENS_LIST 0x8A
|
||||
#define CMD_DBN_SET_CJQ_FACTORY 0x92
|
||||
|
||||
|
||||
#define CMD_SUB_SENS_REPORT 0xC0 //设备主动上报传感信息
|
||||
// #define CMD_REQUIRE_DEV_MAC_ADDR 0xC1
|
||||
// #define CMD_CHANGE_DEV_ADDR_BY_MAC 0xC2
|
||||
// #define CMD_SET_LED 0xC3
|
||||
// #define CMD_SET_RELAY 0xC4
|
||||
#define CMD_SENS_ACS_ENABLE 0xC5 // 设备主动上报 使能
|
||||
|
||||
|
||||
|
||||
|
||||
#define MAX_BLE_DAT_RESPONSE_LEN (BLE_BUFF_MAX_LEN - 4) // 14 //返回 的所有字节数不能超过20个字节,除去 magic、header和ckb校验字节,剩下数据的字节数为14
|
||||
#define MAX_BLE_Notify_Buf_LEN BLE_BUFF_MAX_LEN //24
|
||||
#define MAX_BLE_BUF_LEN BLE_BUFF_MAX_LEN //128
|
||||
#define MAX_BLE_TMP_BUF_LEN BLE_BUFF_MAX_LEN
|
||||
|
||||
typedef struct _BLE_Notify_Buf_
|
||||
{
|
||||
uint8_t flag; //0 idle, 1 buf ready
|
||||
uint8_t len;
|
||||
uint8_t buf[MAX_BLE_Notify_Buf_LEN];
|
||||
}BLE_Notify_Buf;
|
||||
|
||||
typedef struct _BUF_DBN_BLE_
|
||||
{
|
||||
uint8_t flag;
|
||||
uint8_t magic;
|
||||
uint8_t cmd;
|
||||
uint8_t dat_len ;
|
||||
uint8_t dat_offset;
|
||||
uint8_t pkg_amount;
|
||||
uint8_t pkg_seq;
|
||||
uint8_t dat[MAX_BLE_BUF_LEN];
|
||||
} Buf_DBN_BLE;
|
||||
|
||||
extern Buf_DBN_BLE g_buf_ble_response;
|
||||
|
||||
extern BLE_Notify_Buf g_notify_buftemp;
|
||||
extern uint8_t g_flag_notify_temp;
|
||||
extern BLE_Notify_Buf g_ble_rcv_buf;
|
||||
|
||||
|
||||
extern uint16_t loop1_LPCNT;
|
||||
extern uint8_t g_freq_sens;
|
||||
extern uint8_t loop1_SensLevel;
|
||||
extern uint8_t g_freq_level;
|
||||
extern uint32_t g_sys_freq;
|
||||
extern float g_freq_tmp;
|
||||
|
||||
|
||||
|
||||
void clear_ble_notify_buf(BLE_Notify_Buf * buf);
|
||||
uint8_t set_response_tran_to_notify(uint8_t *dat_ori, uint8_t dat_len, BLE_Notify_Buf * notify_dst);
|
||||
|
||||
void poll_dbn_ble(void);
|
||||
void report_sens_acs(void);
|
||||
|
||||
void set_factory_param(void);
|
||||
void factory_dev(void);
|
||||
void para_store_init(void);
|
||||
|
||||
|
||||
#endif /* INCLUDE_DBN_BLE_SRV_H_ */
|
||||
167
vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/include/net_config.h
Normal file
167
vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/include/net_config.h
Normal file
@@ -0,0 +1,167 @@
|
||||
/********************************** (C) COPYRIGHT *******************************
|
||||
* File Name : net_config.h
|
||||
* Author : WCH
|
||||
* Version : V1.30
|
||||
* Date : 2022/06/02
|
||||
* Description : This file contains the configurations of
|
||||
* Ethernet protocol stack library
|
||||
*********************************************************************************
|
||||
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
|
||||
* Attention: This software (modified or not) and binary are used for
|
||||
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
|
||||
*******************************************************************************/
|
||||
#ifndef __NET_CONFIG_H__
|
||||
#define __NET_CONFIG_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*********************************************************************
|
||||
* socket configuration, IPRAW + UDP + TCP + TCP_LISTEN = number of sockets
|
||||
*/
|
||||
#define WCHNET_NUM_IPRAW 0 /* Number of IPRAW connections */
|
||||
|
||||
#define WCHNET_NUM_UDP 1 /* The number of UDP connections */
|
||||
|
||||
#define WCHNET_NUM_TCP 1 /* Number of TCP connections */
|
||||
|
||||
#define WCHNET_NUM_TCP_LISTEN 0 /* Number of TCP listening */
|
||||
|
||||
/* The number of sockets, the maximum is 31 */
|
||||
#define WCHNET_MAX_SOCKET_NUM (WCHNET_NUM_IPRAW+WCHNET_NUM_UDP+WCHNET_NUM_TCP+WCHNET_NUM_TCP_LISTEN)
|
||||
|
||||
#define WCHNET_TCP_MSS 576 //1024 //1460 /* Size of TCP MSS*/
|
||||
|
||||
#define WCHNET_NUM_POOL_BUF (WCHNET_NUM_TCP*2+2) /* The number of POOL BUFs, the number of receive queues */
|
||||
|
||||
/*********************************************************************
|
||||
* MAC queue configuration
|
||||
*/
|
||||
#define ETH_TXBUFNB 1 /* The number of descriptors sent by the MAC */
|
||||
|
||||
#define ETH_RXBUFNB 4 /* Number of MAC received descriptors */
|
||||
|
||||
#ifndef ETH_MAX_PACKET_SIZE
|
||||
#define ETH_RX_BUF_SZE 1520 /* MAC receive buffer length, an integer multiple of 4 */
|
||||
#define ETH_TX_BUF_SZE 1520 /* MAC send buffer length, an integer multiple of 4 */
|
||||
#else
|
||||
#define ETH_RX_BUF_SZE ETH_MAX_PACKET_SIZE
|
||||
#define ETH_TX_BUF_SZE ETH_MAX_PACKET_SIZE
|
||||
#endif
|
||||
|
||||
/*********************************************************************
|
||||
* Functional configuration
|
||||
*/
|
||||
#define WCHNET_PING_ENABLE 1 /* PING is enabled, PING is enabled by default */
|
||||
|
||||
#define TCP_RETRY_COUNT 20 /* The number of TCP retransmissions, the default value is 20 */
|
||||
|
||||
#define TCP_RETRY_PERIOD 10 /* TCP retransmission period, the default value is 10, the unit is 50ms */
|
||||
|
||||
#define SOCKET_SEND_RETRY 0 /* Send failed retry configuration, 1: enable, 0: disable */
|
||||
|
||||
#define FINE_DHCP_PERIOD 8 /* Fine DHCP period, the default value is 8, the unit is 250ms */
|
||||
|
||||
#define CFG0_TCP_SEND_COPY 1 /* TCP send buffer copy, 1: copy, 0: not copy */
|
||||
|
||||
#define CFG0_TCP_RECV_COPY 1 /* TCP receive replication optimization, internal debugging use */
|
||||
|
||||
#define CFG0_TCP_OLD_DELETE 0 /* Delete oldest TCP connection, 1: enable, 0: disable */
|
||||
|
||||
#define CFG0_IP_REASS_PBUFS 0 /* Number of reassembled IP PBUFs */
|
||||
|
||||
#define CFG0_TCP_DEALY_ACK_DISABLE 1 /* 1: disable TCP delay ACK 0: enable TCP delay ACK */
|
||||
|
||||
/*********************************************************************
|
||||
* Memory related configuration
|
||||
*/
|
||||
/* If you want to achieve a higher transmission speed,
|
||||
* try to increase RECE_BUF_LEN to (WCHNET_TCP_MSS*4)
|
||||
* and increase WCHNET_NUM_TCP_SEG to (WCHNET_NUM_TCP*4)*/
|
||||
#define RECE_BUF_LEN (WCHNET_TCP_MSS*2) /* socket receive buffer size */
|
||||
|
||||
#define WCHNET_NUM_PBUF WCHNET_NUM_POOL_BUF /* Number of PBUF structures */
|
||||
|
||||
#define WCHNET_NUM_TCP_SEG (WCHNET_NUM_TCP*2) /* The number of TCP segments used to send */
|
||||
|
||||
#define WCHNET_MEM_HEAP_SIZE (((WCHNET_TCP_MSS+0x10+54+8)*WCHNET_NUM_TCP_SEG)+ETH_TX_BUF_SZE+64+2*0x18) /* memory heap size */
|
||||
|
||||
#define WCHNET_NUM_ARP_TABLE 50 /* Number of ARP lists */
|
||||
|
||||
#define WCHNET_MEM_ALIGNMENT 4 /* 4 byte alignment */
|
||||
|
||||
#if CFG0_IP_REASS_PBUFS
|
||||
#define WCHNET_NUM_IP_REASSDATA 2 /* Number of reassembled IP structures */
|
||||
/*1: When using the fragmentation function,
|
||||
* ensure that the size of WCHNET_SIZE_POOL_BUF is large enough to store a single fragmented packet*/
|
||||
#define WCHNET_SIZE_POOL_BUF (((1500 + 14 + 4) + 3) & ~3) /* Buffer size for receiving a single packet */
|
||||
/*2: When creating a socket that can receive fragmented packets,
|
||||
* ensure that "RecvBufLen" member of the "struct _SOCK_INF" structure
|
||||
* (the parameter initialized when calling WCHNET_SocketCreat) is sufficient
|
||||
* to receive a complete fragmented packet */
|
||||
#else
|
||||
#define WCHNET_NUM_IP_REASSDATA 0 /* Number of reassembled IP structures */
|
||||
#define WCHNET_SIZE_POOL_BUF (((WCHNET_TCP_MSS + 40 + 14 + 4) + 3) & ~3) /* Buffer size for receiving a single packet */
|
||||
#endif
|
||||
|
||||
/* Check receive buffer */
|
||||
#if(WCHNET_NUM_POOL_BUF * WCHNET_SIZE_POOL_BUF < ETH_RX_BUF_SZE)
|
||||
#error "WCHNET_NUM_POOL_BUF or WCHNET_TCP_MSS Error"
|
||||
#error "Please Increase WCHNET_NUM_POOL_BUF or WCHNET_TCP_MSS to make sure the receive buffer is sufficient"
|
||||
#endif
|
||||
/* Check the configuration of the SOCKET quantity */
|
||||
#if( WCHNET_NUM_TCP_LISTEN && !WCHNET_NUM_TCP )
|
||||
#error "WCHNET_NUM_TCP Error,Please Configure WCHNET_NUM_TCP >= 1"
|
||||
#endif
|
||||
/* Check byte alignment must be a multiple of 4 */
|
||||
#if((WCHNET_MEM_ALIGNMENT % 4) || (WCHNET_MEM_ALIGNMENT == 0))
|
||||
#error "WCHNET_MEM_ALIGNMENT Error,Please Configure WCHNET_MEM_ALIGNMENT = 4 * N, N >=1"
|
||||
#endif
|
||||
/* TCP maximum segment length */
|
||||
#if((WCHNET_TCP_MSS > 1460) || (WCHNET_TCP_MSS < 60))
|
||||
#error "WCHNET_TCP_MSS Error,Please Configure WCHNET_TCP_MSS >= 60 && WCHNET_TCP_MSS <= 1460"
|
||||
#endif
|
||||
/* Number of ARP cache tables */
|
||||
#if((WCHNET_NUM_ARP_TABLE > 0X7F) || (WCHNET_NUM_ARP_TABLE < 1))
|
||||
#error "WCHNET_NUM_ARP_TABLE Error,Please Configure WCHNET_NUM_ARP_TABLE >= 1 && WCHNET_NUM_ARP_TABLE <= 0X7F"
|
||||
#endif
|
||||
/* Check POOL BUF configuration */
|
||||
#if(WCHNET_NUM_POOL_BUF < 1)
|
||||
#error "WCHNET_NUM_POOL_BUF Error,Please Configure WCHNET_NUM_POOL_BUF >= 1"
|
||||
#endif
|
||||
/* Check PBUF structure configuration */
|
||||
#if(WCHNET_NUM_PBUF < 1)
|
||||
#error "WCHNET_NUM_PBUF Error,Please Configure WCHNET_NUM_PBUF >= 1"
|
||||
#endif
|
||||
/* Check IP Assignment Configuration */
|
||||
#if(CFG0_IP_REASS_PBUFS && ((WCHNET_NUM_IP_REASSDATA > 10) || (WCHNET_NUM_IP_REASSDATA < 1)))
|
||||
#error "WCHNET_NUM_IP_REASSDATA Error,Please Configure WCHNET_NUM_IP_REASSDATA < 10 && WCHNET_NUM_IP_REASSDATA >= 1 "
|
||||
#endif
|
||||
/* Check the number of reassembled IP PBUFs */
|
||||
#if(CFG0_IP_REASS_PBUFS > WCHNET_NUM_POOL_BUF)
|
||||
#error "WCHNET_NUM_POOL_BUF Error,Please Configure CFG0_IP_REASS_PBUFS < WCHNET_NUM_POOL_BUF"
|
||||
#endif
|
||||
/* Check Timer period, in Ms. */
|
||||
#if(WCHNETTIMERPERIOD > 50)
|
||||
#error "WCHNETTIMERPERIOD Error,Please Configure WCHNETTIMERPERIOD < 50"
|
||||
#endif
|
||||
|
||||
/* Configuration value 0 */
|
||||
#define WCHNET_MISC_CONFIG0 (((CFG0_TCP_SEND_COPY) << 0) |\
|
||||
((CFG0_TCP_RECV_COPY) << 1) |\
|
||||
((CFG0_TCP_OLD_DELETE) << 2) |\
|
||||
((CFG0_IP_REASS_PBUFS) << 3) |\
|
||||
((CFG0_TCP_DEALY_ACK_DISABLE) << 8))
|
||||
/* Configuration value 1 */
|
||||
#define WCHNET_MISC_CONFIG1 (((WCHNET_MAX_SOCKET_NUM)<<0)|\
|
||||
((WCHNET_PING_ENABLE) << 13) |\
|
||||
((TCP_RETRY_COUNT) << 14) |\
|
||||
((TCP_RETRY_PERIOD) << 19) |\
|
||||
((SOCKET_SEND_RETRY) << 25) |\
|
||||
((FINE_DHCP_PERIOD) << 27))
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
289
vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/include/net_srv.h
Normal file
289
vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/include/net_srv.h
Normal file
@@ -0,0 +1,289 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file net_srv.c
|
||||
* @author wangfq
|
||||
* @version V1.0
|
||||
* @date 2026-03-02
|
||||
* @brief net message handle: unpack, exeute, response
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
#ifndef __NET_SRV_H__
|
||||
#define __NET_SRV_H__
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#define MAX_COUNTER_NET_STATE_NORMAL 100
|
||||
//#define RECE_BUF_LEN 512 /* ?????????????ó?? */
|
||||
#define MAX_REPORT_INTERVAL_MS 30000 //60000 //180??
|
||||
#define MIN_REPORT_INTERVAL_MS 6000 // 6??
|
||||
|
||||
#define MAX_REPORT_TIMEOUT_MS 4000 // 4??
|
||||
|
||||
|
||||
#define MAX_REPORT_INTERVAL 3
|
||||
#define MAX_CLEAR_COUNTER_INTERVAL 4900 // 6hour=6*3600
|
||||
|
||||
#define PORT_TCP_DEFAULT 5550
|
||||
|
||||
#define NET_REPORT_INTERVAL 300 // 300 second
|
||||
#define COM_LSSC_MESSAGE_UDP_PORT 5505 //ssc UDP message port
|
||||
#define COM_LSSC_TCP_PORT 5550
|
||||
#define COM_LSSC_UDP_PORT 5500
|
||||
#define COM_DEV_MESSAGE_PORT 4900 //udp dev local message port
|
||||
|
||||
#define TOPIC_DEFAULT_SUBSCRIBE "ptpc/display"
|
||||
#define TOPIC_DEFAULT_PUBLISH "gtpc/display/Initialize"
|
||||
|
||||
#define UART_BAUD_DEFAULT_PORT_1 115200 // 与Loop MCU 通信的波特率 TTL, g_storage_uart_baud_1
|
||||
#define UART_BAUD_DEFAULT_PORT_2 115200 // baud2 TTL g_storage_uart_baud_2
|
||||
|
||||
|
||||
|
||||
typedef enum
|
||||
{
|
||||
IOT_Addr_IP_Mode = 0,
|
||||
IOT_Addr_DN_Mode // Domain Name Mode
|
||||
}IOT_Host_Mode;
|
||||
|
||||
typedef struct _LOCAL_NET_CFG
|
||||
{
|
||||
uint8_t mac[6];
|
||||
uint8_t lip[4]; /*local IP±???IP???·*/
|
||||
uint8_t sub[4]; /*×???????*/
|
||||
uint8_t gw[4];
|
||||
uint8_t dns[4]; /*DNS·????÷???·*/
|
||||
|
||||
uint16_t port_ssc_tcp;
|
||||
uint16_t port_ssc_udp;
|
||||
uint16_t port_ssc_udp_message; // use for interval of report
|
||||
uint16_t port_dev_tcp; //use for server http port
|
||||
uint16_t port_dev_udp;
|
||||
|
||||
}Local_Net_Cfg; //32
|
||||
|
||||
extern Local_Net_Cfg local_net_cfg;
|
||||
|
||||
typedef struct _NET_CENTER_INFO_
|
||||
{
|
||||
uint8_t lssc_ip[4]; //Local Sotfware Service Center ±????í??·???????
|
||||
uint16_t msg_port;
|
||||
uint16_t tcp_port; // use for server http port
|
||||
uint16_t udp_port;
|
||||
uint8_t sw_ver[2];
|
||||
}NET_CENTER_INFO;
|
||||
|
||||
extern NET_CENTER_INFO net_center_info;
|
||||
|
||||
typedef struct _IOT_NET_INFO_
|
||||
{
|
||||
uint8_t remote_addr[64];
|
||||
uint16_t mqtt_port;
|
||||
uint8_t client_id[64]; //?????±±í??????±??ú?ò????
|
||||
uint8_t username[64];
|
||||
uint8_t password[32];
|
||||
|
||||
uint8_t mode; //0xXX ??0???? 0 IP, 1 dns, ??1???? 0 disable, 1 enable
|
||||
//ip, iot_disable, 0x00:0000 0000,
|
||||
//dns,iot_disable, 0x01:0000 0001,
|
||||
//ip, iot_enable, 0x02:0000 0010,
|
||||
//dns,iot_enable, 0x03:0000 0011
|
||||
}IOT_NET_INFO;
|
||||
extern IOT_NET_INFO iot_net_info;
|
||||
|
||||
#define MAX_TOPIC_LENGTH 64
|
||||
|
||||
typedef struct _IOT_TOPIC_
|
||||
{
|
||||
uint8_t clientid_enable; // ?÷????·?°ü??¤CClientID
|
||||
uint8_t topic_pub[MAX_TOPIC_LENGTH];
|
||||
uint8_t topic_sub[MAX_TOPIC_LENGTH];
|
||||
} IOT_Topic;
|
||||
extern IOT_Topic g_iot_topic;
|
||||
|
||||
#define MQTT_KEEPALIVE_INTERVAL 9
|
||||
|
||||
|
||||
typedef struct _NET_STATE_
|
||||
{
|
||||
uint8_t flag;
|
||||
uint8_t intstat;
|
||||
uint32_t counter;
|
||||
} Net_State;
|
||||
extern Net_State g_net_state;
|
||||
|
||||
|
||||
void write_net_config(Local_Net_Cfg *local, NET_CENTER_INFO *center, IOT_NET_INFO *iotcfg, IOT_Topic *iottopic);
|
||||
|
||||
|
||||
|
||||
#define DPG_API_KEY_METHOD "\"Method\""
|
||||
#define DPG_API_KEY_CODE "\"Code\""
|
||||
#define DPG_API_KEY_DATA "\"Data\""
|
||||
#define DPG_API_KEY_IP "\"Ip\""
|
||||
#define DPG_API_KEY_PORT "\"Port\""
|
||||
#define DPG_API_KEY_PORT_MST "\"PortMsg\""
|
||||
#define DPG_API_KEY_Mac "\"Mac\""
|
||||
#define DPG_API_KEY_SubnetMask "\"SubnetMask\""
|
||||
#define DPG_API_KEY_DNS "\"DNS\""
|
||||
#define DPG_API_KEY_DEVICE_CODE "\"Device_code\""
|
||||
#define DPG_API_KEY_DEVICE_ID "\"Device_id\""
|
||||
#define DPG_API_KEY_EXTRA_ID "\"Extra_id\""
|
||||
#define DPG_API_KEY_PARAMS "\"Params\""
|
||||
#define DPG_API_KEY_AREA_NUM "\"Area_num\""
|
||||
#define DPG_API_KEY_EXTRA_ID "\"Extra_id\""
|
||||
#define DPG_API_KEY_AREA_AMOUNT "\"Area_amount\""
|
||||
#define DPG_API_KEY_DEV_AMOUNT "\"Dev_amount\""
|
||||
#define DPG_API_KEY_DEV_MODE "\"Dev_mode\""
|
||||
#define DPG_API_KEY_TOTAL_AMOUNT "\"Total_amount\""
|
||||
#define DPG_API_KEY_FREE_LOT "\"Free_lot\""
|
||||
#define DPG_API_KEY_DEV_MODE "\"Dev_mode\""
|
||||
#define DPG_API_KEY_DisplayLED "\"DisplayLED\""
|
||||
#define DPG_API_KEY_LED_Addr "\"LED_Addr\""
|
||||
#define DPG_API_KEY_Content "\"_Content\""
|
||||
|
||||
#define DPG_API_KEY_INFO_ENABLE "\"Info_Enable\""
|
||||
#define DPG_API_KEY_Color "\"Color\""
|
||||
#define DPG_API_KEY_Duration "\"Duration\""
|
||||
#define DPG_API_KEY_Bus_Num "\"Bus_Num\""
|
||||
|
||||
|
||||
#define DPG_API_KEY_Sub_Amount "\"Sub_Amount\""
|
||||
#define DPG_API_KEY_Sub_Dev "\"Sub_Dev\""
|
||||
|
||||
|
||||
#define DPG_API_KEY_Dev_Ip "\"Dev_Ip\""
|
||||
#define DPG_API_KEY_Gateway_Ip "\"Gateway_Ip\""
|
||||
#define DPG_API_KEY_Gateway_Port "\"Gateway_Port\""
|
||||
#define DPG_API_KEY_Server_Ip "\"Server_Ip\""
|
||||
#define DPG_API_KEY_Server_Port "\"Server_Port\""
|
||||
|
||||
|
||||
#define IOT_API_KEY_HOST "\"Iot_Host\""
|
||||
#define IOT_API_KEY_PORT "\"Iot_Port\""
|
||||
#define IOT_API_KEY_USERNAME "\"UserName\""
|
||||
|
||||
|
||||
//add by wfq 20190812
|
||||
#define DPG_KEY_PARAMS_JSON "\"Params\""
|
||||
#define KEY_CODE_AccessReport "AccessReport"
|
||||
#define KEY_CODE_DPG_Addr "\"Dpg_Addr\""
|
||||
#define KEY_CODE_Access_Channel "\"Access_Channel\""
|
||||
#define KEY_CODE_State_InOut "\"State_InOut\""
|
||||
#define KEY_CODE_Time_Stamp "\"TimeStamp\""
|
||||
|
||||
#define KEY_CODE_Time_Counter "\"Time_Counter\""
|
||||
|
||||
#define KEY_CODE_Bus_Num "\"Bus_Num\""
|
||||
#define KEY_CODE_Dus_Addr_Enable "\"Dus_Addr_Enable\""
|
||||
|
||||
//-------------------
|
||||
|
||||
#define SSC_Code_Initialize "\"Initialize\""
|
||||
#define SSC_Code_UnInitialize "\"UnInitialize\""
|
||||
#define SSC_Code_Connect "\"Connect\""
|
||||
#define SSC_Code_DisConnect "\"DisConnect\""
|
||||
#define SSC_Code_SendCommand "\"SendCommand\""
|
||||
#define SSC_Code_Speak "\"Speak\""
|
||||
|
||||
#define SSC_Code_Count_Off "Count_Off"
|
||||
#define SSC_Code_Device_Info "Device_Info"
|
||||
#define SSC_Code_Devs_Info "Devs_Info" //add by wfq 2020-07-14 ?????????????°??×??è±????è±?????
|
||||
#define SSC_Code_Global_Require "Global_Get"
|
||||
#define SSC_Code_Global_Set "Global_Set"
|
||||
#define SSC_Code_Collect_ALL "Collect_All" //?????ù????????????
|
||||
#define SSC_Code_Collect_Single "Collect_Single" //??????????????????
|
||||
#define SSC_Code_Config "Config"
|
||||
#define SSC_Code_DisplayLED "DisplayLED"
|
||||
#define SSC_Code_Device_Net_Set "Device_Net_Set" //?????è±?????????
|
||||
#define SSC_Code_HeartBeat "HeartBeat"
|
||||
#define SSC_Code_CDev_Subs "CDev_Subs" //????×??è±????·????
|
||||
#define SSC_Code_Device_IOT_Set "Dev_IOT_Set" //?è??IOT???????? MQTT???·?????????????§??????
|
||||
|
||||
|
||||
#define SSC_Code_ACS_Collect_Area "ACS_Collect_Area"
|
||||
#define SSC_Code_ACS_Collect_Counter "ACS_Collect_Counter" //?????????????????¨?????? add 2021-01-04
|
||||
|
||||
#define SSC_Code_Collect_NT_BUS "Collect_NT_BUS" //??????????????×??????·????
|
||||
#define SSC_Code_CDetail_NT_BUS "CDetail_NT_BUS" //??????????????×????ê?????·???? add 2022-04-19
|
||||
|
||||
#define SSC_Code_Radar_Reset "Radar_Reset" //?×?????? 2021-04-23
|
||||
#define SSC_Code_SerialNet "SerialNet" //485???????? 2021-04-23
|
||||
#define SSC_Code_Dev_Reset "Dev_Reset" // ?è±??????? 2024-12-13
|
||||
|
||||
|
||||
#define SSC_Code_GetDeviceInfo "\"GetDeviceInfo\""
|
||||
#define SSC_Code_SetDeviceInfo "\"SetDeviceInfo\""
|
||||
#define SSC_Code_SetDeviceInfoEX "\"SetDeviceInfoEX\""
|
||||
#define SSC_Code_StateChangedEvent "\"StateChangedEventReg\""
|
||||
|
||||
#define KEY_IOT_HOST "\"Host\""
|
||||
#define KEY_IOT_PORT "\"Port\""
|
||||
#define KEY_IOT_USERNAME "\"UserName\""
|
||||
#define KEY_IOT_PASSWORD "\"Password\""
|
||||
#define KEY_IOT_MODE "\"Mode\""
|
||||
|
||||
|
||||
//add by wfq 2019-08-14
|
||||
#define SSC_Code_ACS_Collect_Access "ACS_Collect_Access"
|
||||
#define SSC_Code_TimeStamp "TimeStamp"
|
||||
|
||||
#define SSC_Code_Dev_Time "Dev_Time"
|
||||
#define SSC_Code_SSC_Time "\"SSC_Time\""
|
||||
|
||||
//add by wfq 2020-7-11
|
||||
#define SSC_Code_ACS_NT_Event "ACS_NT_Event"
|
||||
|
||||
#define SSC_Code_GetDusCFG "GetDusCFG" //add by wfq 2020-7-23
|
||||
#define SSC_Code_SetDusCFG "SetDusCFG" //add by wfq 2020-7-23
|
||||
|
||||
#define SSC_Code_ACS_Collect_CJQ "ACS_Collect_CJQ"
|
||||
|
||||
//add by wangfq 2022-04-14
|
||||
#define SSC_Code_Tran_Sub "Tran_Sub" // ??×??è±??¨??????
|
||||
#define SSC_Code_Dev_Type "\"Dev_Type\"" // ?è±??à?? ×?·????? 20 DLD950
|
||||
#define SSC_Code_Cmd "Cmd"
|
||||
#define SSC_Code_Detail "Detail"
|
||||
|
||||
|
||||
extern uint8_t g_flag_timestamp;
|
||||
extern uint8_t RemoteIP[4];
|
||||
|
||||
|
||||
void manage_udp_message(uint8_t socket, uint8_t *ip, uint16_t port, uint8_t *buf, uint32_t len);
|
||||
|
||||
void unpack_ssc_count_off(uint8_t socket, uint8_t count_mode, uint8_t *ip, uint16_t port, uint8_t *buf, uint32_t len);
|
||||
void unpack_ssc_device_reset(uint8_t socket, uint8_t *ip, uint16_t port, uint8_t *buf, uint32_t len);
|
||||
void unpack_ssc_device_net_set(uint8_t socket, uint8_t *ip, uint16_t port, uint8_t *buf, uint32_t len);
|
||||
void unpack_dev_iot_config(uint8_t socket, uint8_t *ip, uint16_t port, uint8_t *buf, uint32_t len);
|
||||
void unpack_ssc_set_display_led(uint8_t socket, uint8_t *ip, uint16_t port, uint8_t *buf, uint32_t len);
|
||||
void unpack_ssc_tran_sub(uint8_t socket, uint8_t *ip, uint16_t port, uint8_t *buf, uint32_t len);
|
||||
void unpack_ssc_radar_reset(uint8_t socket, uint8_t *ip, uint16_t port, uint8_t *buf, uint32_t len);
|
||||
|
||||
void unpack_ssc_timestamp(uint8_t *buf, uint32_t len);
|
||||
void unpack_ssc_collect_cjq_acs_response(uint8_t socket, uint8_t *buf, uint32_t len);
|
||||
|
||||
//void acs_dus_report(void);
|
||||
//void rcv_acs_dus_report_reponse(void);
|
||||
|
||||
void dev_response_heartbeat(char *dat);
|
||||
|
||||
void dev_send_heartbeat(void);
|
||||
void dev_get_timestamp_send(void);
|
||||
void manage_tcp_message(uint8_t socket, uint8_t *buf, uint32_t len);
|
||||
void manage_mqtt_recv_message(char * msg, int length);
|
||||
|
||||
|
||||
void GetMacAddr(unsigned char *pMAC);
|
||||
void mStopIfError(u8 iError);
|
||||
|
||||
void net_srv_init(void);
|
||||
void WCHNET_CreateTcpMqttSocket(void);
|
||||
void WCHNET_CreateTcpSocket(void);
|
||||
void WCHNET_HandleSockInt(uint8_t socketid, uint8_t intstat);
|
||||
int get_ipstr_to_array(char *src, uint8_t *dst);
|
||||
void WCHNET_HandleGlobalInt(void);
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,67 @@
|
||||
/********************************** (C) COPYRIGHT *******************************
|
||||
* File Name : peripheral.h
|
||||
* Author : WCH
|
||||
* Version : V1.0
|
||||
* Date : 2018/12/11
|
||||
* Description :
|
||||
*********************************************************************************
|
||||
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
|
||||
* Attention: This software (modified or not) and binary are used for
|
||||
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
|
||||
*******************************************************************************/
|
||||
|
||||
#ifndef PERIPHERAL_H
|
||||
#define PERIPHERAL_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*********************************************************************
|
||||
* INCLUDES
|
||||
*/
|
||||
#include "dbn_ble_srv.h"
|
||||
/*********************************************************************
|
||||
* CONSTANTS
|
||||
*/
|
||||
|
||||
// Peripheral Task Events
|
||||
#define SBP_START_DEVICE_EVT 0x0001
|
||||
#define SBP_PERIODIC_EVT 0x0002
|
||||
#define SBP_READ_RSSI_EVT 0x0004
|
||||
#define SBP_PARAM_UPDATE_EVT 0x0008
|
||||
#define SBP_PHY_UPDATE_EVT 0x0010
|
||||
|
||||
/*********************************************************************
|
||||
* MACROS
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
uint16_t connHandle; // Connection handle of current connection
|
||||
uint16_t connInterval;
|
||||
uint16_t connSlaveLatency;
|
||||
uint16_t connTimeout;
|
||||
} peripheralConnItem_t;
|
||||
|
||||
/*********************************************************************
|
||||
* FUNCTIONS
|
||||
*/
|
||||
|
||||
/*
|
||||
* Task Initialization for the BLE Application
|
||||
*/
|
||||
extern void Peripheral_Init(void);
|
||||
|
||||
/*
|
||||
* Task Event Processor for the BLE Application
|
||||
*/
|
||||
extern uint16_t Peripheral_ProcessEvent(uint8_t task_id, uint16_t events);
|
||||
|
||||
/*********************************************************************
|
||||
*********************************************************************/
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,8 @@
|
||||
|
||||
#ifndef __SIMPLE_JSON_H_
|
||||
#define __SIMPLE_JSON_H_
|
||||
|
||||
void simple_parse_json(const char * data, char *key_str, char *value_str);
|
||||
|
||||
|
||||
#endif
|
||||
36
vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/include/storage.h
Normal file
36
vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/include/storage.h
Normal file
@@ -0,0 +1,36 @@
|
||||
#ifndef _STORAGE_H__
|
||||
#define _STORAGE_H__
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#define DEV_NUMER_ADDR_OFFSET 0x10
|
||||
#define DEV_SUB_CODE_ADDR_OFFSET 0x20 //2byte [0x20,0x21]
|
||||
#define DEV_BUS_BAUD_ADDR_OFFSET 0x22 //8byte [0x22,0x29]
|
||||
#define DEV_PASSWORD_ADDR_OFFSET 0x2A //6byte [0x2A, 0x2F]
|
||||
|
||||
#define NET_LOCAL_CFG_ADDR_OFFSET 0x30 // 0x70 // sizeof(Local_Net_Cfg):32byte
|
||||
#define NET_CENTER_CFG_ADDR_OFFSET 0x50 // sizeof(NET_CENTER_INFO): 12byte
|
||||
#define IOT_NET_CFG_ADDR 0x60 // sizeof(IOT_NET_INFO):226(0xE2),
|
||||
#define IOT_TOPIC_CFG_ADDR (0x60 + 232) // sizeof(IOT_Topic): 129(0x81)
|
||||
|
||||
|
||||
char get_ble_safe_flag(void);
|
||||
void open_ble_safe_flag(void);
|
||||
void close_ble_safe_flag(void);
|
||||
char check_ble_safe_pass(uint8_t * devpass);
|
||||
void set_ble_safe_pass(uint8_t * devpass);
|
||||
|
||||
void alter_dev_serila(uint8_t *serial);
|
||||
void alter_dev_baud(uint8_t *baudbuf);
|
||||
|
||||
void factory_dev_info(void);
|
||||
void output_cfg_from_flash(void);
|
||||
void load_cfg_from_flash(void);
|
||||
|
||||
|
||||
void SPI_Flash_Read(uint8_t *pBuffer, uint32_t ReadAddr, uint16_t size);
|
||||
void SPI_Flash_Write(uint8_t *pBuffer, uint32_t WriteAddr, uint16_t size);
|
||||
|
||||
void storage_init(void);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,31 @@
|
||||
/********************************** (C) COPYRIGHT *******************************
|
||||
* File Name : system_ch32v20x.h
|
||||
* Author : WCH
|
||||
* Version : V1.0.0
|
||||
* Date : 2022/06/16
|
||||
* Description : CH32V20x Device Peripheral Access Layer System Header File.
|
||||
*********************************************************************************
|
||||
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
|
||||
* Attention: This software (modified or not) and binary are used for
|
||||
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
|
||||
*******************************************************************************/
|
||||
|
||||
#ifndef __SYSTEM_ch32v20x_H
|
||||
#define __SYSTEM_ch32v20x_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern uint32_t SystemCoreClock; /* System Clock Frequency (Core Clock) */
|
||||
|
||||
/* System_Exported_Functions */
|
||||
extern void SystemInit(void);
|
||||
extern void SystemCoreClockUpdate(void);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /*__CH32V20x_SYSTEM_H */
|
||||
1115
vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/net_srv.c
Normal file
1115
vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/net_srv.c
Normal file
File diff suppressed because it is too large
Load Diff
828
vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/peripheral.c
Normal file
828
vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/peripheral.c
Normal file
@@ -0,0 +1,828 @@
|
||||
/********************************** (C) COPYRIGHT *******************************
|
||||
* File Name : peripheral.C
|
||||
* Author : WCH
|
||||
* Version : V1.0
|
||||
* Date : 2018/12/10
|
||||
* Description : Peripheral slave multi-connection application,
|
||||
* initialize broadcast connection parameters, then broadcast,
|
||||
* after connecting to the host, request to update connection parameters,
|
||||
* and transmit data through custom services
|
||||
*********************************************************************************
|
||||
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
|
||||
* Attention: This software (modified or not) and binary are used for
|
||||
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
|
||||
*******************************************************************************/
|
||||
|
||||
/*********************************************************************
|
||||
* INCLUDES
|
||||
*/
|
||||
#include "CONFIG.h"
|
||||
#include "devinfoservice.h"
|
||||
#include "gattprofile.h"
|
||||
#include "peripheral.h"
|
||||
#include "cmcng.h"
|
||||
|
||||
/*********************************************************************
|
||||
* MACROS
|
||||
*/
|
||||
|
||||
/*********************************************************************
|
||||
* CONSTANTS
|
||||
*/
|
||||
|
||||
// How often to perform periodic event
|
||||
#define SBP_PERIODIC_EVT_PERIOD 50 //100 //200 // 1600
|
||||
|
||||
// How often to perform read rssi event
|
||||
#define SBP_READ_RSSI_EVT_PERIOD 3200
|
||||
|
||||
// Parameter update delay
|
||||
#define SBP_PARAM_UPDATE_DELAY 200 //6400
|
||||
|
||||
// PHY update delay
|
||||
#define SBP_PHY_UPDATE_DELAY 200 //2400
|
||||
|
||||
// What is the advertising interval when device is discoverable (units of 625us, 80=50ms)
|
||||
#define DEFAULT_ADVERTISING_INTERVAL 80
|
||||
|
||||
// Limited discoverable mode advertises for 30.72s, and then stops
|
||||
// General discoverable mode advertises indefinitely
|
||||
#define DEFAULT_DISCOVERABLE_MODE GAP_ADTYPE_FLAGS_GENERAL
|
||||
|
||||
// Minimum connection interval (units of 1.25ms, 6=7.5ms)
|
||||
#define DEFAULT_DESIRED_MIN_CONN_INTERVAL 6
|
||||
|
||||
// Maximum connection interval (units of 1.25ms, 100=125ms)
|
||||
#define DEFAULT_DESIRED_MAX_CONN_INTERVAL 100
|
||||
|
||||
// Slave latency to use parameter update
|
||||
#define DEFAULT_DESIRED_SLAVE_LATENCY 0
|
||||
|
||||
// Supervision timeout value (units of 10ms, 100=1s)
|
||||
#define DEFAULT_DESIRED_CONN_TIMEOUT 100
|
||||
|
||||
// Company Identifier: WCH
|
||||
#define WCH_COMPANY_ID 0x07D7
|
||||
|
||||
/*********************************************************************
|
||||
* TYPEDEFS
|
||||
*/
|
||||
|
||||
/*********************************************************************
|
||||
* GLOBAL VARIABLES
|
||||
*/
|
||||
|
||||
/*********************************************************************
|
||||
* EXTERNAL VARIABLES
|
||||
*/
|
||||
|
||||
/*********************************************************************
|
||||
* EXTERNAL FUNCTIONS
|
||||
*/
|
||||
|
||||
/*********************************************************************
|
||||
* LOCAL VARIABLES
|
||||
*/
|
||||
static uint8_t Peripheral_TaskID = INVALID_TASK_ID; // Task ID for internal task/event processing
|
||||
|
||||
// GAP - SCAN RSP data (max size = 31 bytes)
|
||||
static uint8_t scanRspData[] = {
|
||||
// complete name
|
||||
0x12, // length of this data
|
||||
GAP_ADTYPE_LOCAL_NAME_COMPLETE,
|
||||
// 'S',
|
||||
// 'i',
|
||||
// 'm',
|
||||
// 'p',
|
||||
// 'l',
|
||||
// 'e',
|
||||
// ' ',
|
||||
// 'P',
|
||||
// 'e',
|
||||
// 'r',
|
||||
// 'i',
|
||||
// 'p',
|
||||
// 'h',
|
||||
// 'e',
|
||||
// 'r',
|
||||
// 'a',
|
||||
// 'l',
|
||||
'D','L','D','9','6','0', 'G', 'A', '\0',
|
||||
// connection interval range
|
||||
0x05, // length of this data
|
||||
GAP_ADTYPE_SLAVE_CONN_INTERVAL_RANGE,
|
||||
LO_UINT16(DEFAULT_DESIRED_MIN_CONN_INTERVAL), // 100ms
|
||||
HI_UINT16(DEFAULT_DESIRED_MIN_CONN_INTERVAL),
|
||||
LO_UINT16(DEFAULT_DESIRED_MAX_CONN_INTERVAL), // 1s
|
||||
HI_UINT16(DEFAULT_DESIRED_MAX_CONN_INTERVAL),
|
||||
|
||||
// Tx power level
|
||||
0x02, // length of this data
|
||||
GAP_ADTYPE_POWER_LEVEL,
|
||||
0 // 0dBm
|
||||
};
|
||||
|
||||
// GAP - Advertisement data (max size = 31 bytes, though this is
|
||||
// best kept short to conserve power while advertising)
|
||||
static uint8_t advertData[] = {
|
||||
// Flags; this sets the device to use limited discoverable
|
||||
// mode (advertises for 30 seconds at a time) instead of general
|
||||
// discoverable mode (advertises indefinitely)
|
||||
0x02, // length of this data
|
||||
GAP_ADTYPE_FLAGS,
|
||||
DEFAULT_DISCOVERABLE_MODE | GAP_ADTYPE_FLAGS_BREDR_NOT_SUPPORTED,
|
||||
|
||||
// service UUID, to notify central devices what services are included
|
||||
// in this peripheral
|
||||
0x03, // length of this data
|
||||
GAP_ADTYPE_16BIT_MORE, // some of the UUID's, but not all
|
||||
LO_UINT16(SIMPLEPROFILE_SERV_UUID),
|
||||
HI_UINT16(SIMPLEPROFILE_SERV_UUID)
|
||||
};
|
||||
|
||||
// GAP GATT Attributes
|
||||
static uint8_t attDeviceName[GAP_DEVICE_NAME_LEN] = "DLD960GA"; // "Simple Peripheral";
|
||||
|
||||
// Connection item list
|
||||
static peripheralConnItem_t peripheralConnList;
|
||||
|
||||
static uint16_t peripheralMTU = ATT_MTU_SIZE;
|
||||
/*********************************************************************
|
||||
* LOCAL FUNCTIONS
|
||||
*/
|
||||
static void Peripheral_ProcessTMOSMsg(tmos_event_hdr_t *pMsg);
|
||||
static void peripheralStateNotificationCB(gapRole_States_t newState, gapRoleEvent_t *pEvent);
|
||||
static void performPeriodicTask(void);
|
||||
static void simpleProfileChangeCB(uint8_t paramID, uint8_t *pValue, uint16_t len);
|
||||
static void peripheralParamUpdateCB(uint16_t connHandle, uint16_t connInterval,
|
||||
uint16_t connSlaveLatency, uint16_t connTimeout);
|
||||
static void peripheralInitConnItem(peripheralConnItem_t *peripheralConnList);
|
||||
static void peripheralRssiCB(uint16_t connHandle, int8_t rssi);
|
||||
static void peripheralChar4Notify(uint8_t *pValue, uint16_t len);
|
||||
|
||||
/*********************************************************************
|
||||
* PROFILE CALLBACKS
|
||||
*/
|
||||
|
||||
// GAP Role Callbacks
|
||||
static gapRolesCBs_t Peripheral_PeripheralCBs = {
|
||||
peripheralStateNotificationCB, // Profile State Change Callbacks
|
||||
peripheralRssiCB, // When a valid RSSI is read from controller (not used by application)
|
||||
peripheralParamUpdateCB
|
||||
};
|
||||
|
||||
// Broadcast Callbacks
|
||||
static gapRolesBroadcasterCBs_t Broadcaster_BroadcasterCBs = {
|
||||
NULL, // Not used in peripheral role
|
||||
NULL // Receive scan request callback
|
||||
};
|
||||
|
||||
// GAP Bond Manager Callbacks
|
||||
static gapBondCBs_t Peripheral_BondMgrCBs = {
|
||||
NULL, // Passcode callback (not used by application)
|
||||
NULL, // Pairing / Bonding state Callback (not used by application)
|
||||
NULL // oob callback
|
||||
};
|
||||
|
||||
// Simple GATT Profile Callbacks
|
||||
static simpleProfileCBs_t Peripheral_SimpleProfileCBs = {
|
||||
simpleProfileChangeCB // Characteristic value change callback
|
||||
};
|
||||
/*********************************************************************
|
||||
* PUBLIC FUNCTIONS
|
||||
*/
|
||||
|
||||
/*********************************************************************
|
||||
* @fn Peripheral_Init
|
||||
*
|
||||
* @brief Initialization function for the Peripheral App Task.
|
||||
* This is called during initialization and should contain
|
||||
* any application specific initialization (ie. hardware
|
||||
* initialization/setup, table initialization, power up
|
||||
* notificaiton ... ).
|
||||
*
|
||||
* @param task_id - the ID assigned by TMOS. This ID should be
|
||||
* used to send messages and set timers.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
void Peripheral_Init()
|
||||
{
|
||||
Peripheral_TaskID = TMOS_ProcessEventRegister(Peripheral_ProcessEvent);
|
||||
|
||||
// Setup the GAP Peripheral Role Profile
|
||||
{
|
||||
uint8_t initial_advertising_enable = TRUE;
|
||||
uint16_t desired_min_interval = DEFAULT_DESIRED_MIN_CONN_INTERVAL;
|
||||
uint16_t desired_max_interval = DEFAULT_DESIRED_MAX_CONN_INTERVAL;
|
||||
|
||||
// Set the GAP Role Parameters
|
||||
GAPRole_SetParameter(GAPROLE_ADVERT_ENABLED, sizeof(uint8_t), &initial_advertising_enable);
|
||||
GAPRole_SetParameter(GAPROLE_SCAN_RSP_DATA, sizeof(scanRspData), scanRspData);
|
||||
GAPRole_SetParameter(GAPROLE_ADVERT_DATA, sizeof(advertData), advertData);
|
||||
GAPRole_SetParameter(GAPROLE_MIN_CONN_INTERVAL, sizeof(uint16_t), &desired_min_interval);
|
||||
GAPRole_SetParameter(GAPROLE_MAX_CONN_INTERVAL, sizeof(uint16_t), &desired_max_interval);
|
||||
}
|
||||
|
||||
{
|
||||
uint16_t advInt = DEFAULT_ADVERTISING_INTERVAL;
|
||||
|
||||
// Set advertising interval
|
||||
GAP_SetParamValue(TGAP_DISC_ADV_INT_MIN, advInt);
|
||||
GAP_SetParamValue(TGAP_DISC_ADV_INT_MAX, advInt);
|
||||
|
||||
// Enable scan req notify
|
||||
GAP_SetParamValue(TGAP_ADV_SCAN_REQ_NOTIFY, ENABLE);
|
||||
}
|
||||
|
||||
// Setup the GAP Bond Manager
|
||||
{
|
||||
uint32_t passkey = 0; // passkey "000000"
|
||||
uint8_t pairMode = GAPBOND_PAIRING_MODE_WAIT_FOR_REQ;
|
||||
uint8_t mitm = TRUE;
|
||||
uint8_t bonding = TRUE;
|
||||
uint8_t ioCap = GAPBOND_IO_CAP_DISPLAY_ONLY;
|
||||
GAPBondMgr_SetParameter(GAPBOND_PERI_DEFAULT_PASSCODE, sizeof(uint32_t), &passkey);
|
||||
GAPBondMgr_SetParameter(GAPBOND_PERI_PAIRING_MODE, sizeof(uint8_t), &pairMode);
|
||||
GAPBondMgr_SetParameter(GAPBOND_PERI_MITM_PROTECTION, sizeof(uint8_t), &mitm);
|
||||
GAPBondMgr_SetParameter(GAPBOND_PERI_IO_CAPABILITIES, sizeof(uint8_t), &ioCap);
|
||||
GAPBondMgr_SetParameter(GAPBOND_PERI_BONDING_ENABLED, sizeof(uint8_t), &bonding);
|
||||
}
|
||||
|
||||
// Initialize GATT attributes
|
||||
GGS_AddService(GATT_ALL_SERVICES); // GAP
|
||||
GATTServApp_AddService(GATT_ALL_SERVICES); // GATT attributes
|
||||
DevInfo_AddService(); // Device Information Service
|
||||
SimpleProfile_AddService(GATT_ALL_SERVICES); // Simple GATT Profile
|
||||
|
||||
// Set the GAP Characteristics
|
||||
GGS_SetParameter(GGS_DEVICE_NAME_ATT, sizeof(attDeviceName), attDeviceName);
|
||||
|
||||
// Setup the SimpleProfile Characteristic Values
|
||||
{
|
||||
uint8_t charValue1[SIMPLEPROFILE_CHAR1_LEN] = {1};
|
||||
uint8_t charValue2[SIMPLEPROFILE_CHAR2_LEN] = {2};
|
||||
uint8_t charValue3[SIMPLEPROFILE_CHAR3_LEN] = {3};
|
||||
uint8_t charValue4[SIMPLEPROFILE_CHAR4_LEN] = {4};
|
||||
// uint8_t charValue5[SIMPLEPROFILE_CHAR5_LEN] = {1, 2, 3, 4, 5};
|
||||
|
||||
SimpleProfile_SetParameter(SIMPLEPROFILE_CHAR1, SIMPLEPROFILE_CHAR1_LEN, charValue1);
|
||||
SimpleProfile_SetParameter(SIMPLEPROFILE_CHAR2, SIMPLEPROFILE_CHAR2_LEN, charValue2);
|
||||
SimpleProfile_SetParameter(SIMPLEPROFILE_CHAR3, SIMPLEPROFILE_CHAR3_LEN, charValue3);
|
||||
SimpleProfile_SetParameter(SIMPLEPROFILE_CHAR4, SIMPLEPROFILE_CHAR4_LEN, charValue4);
|
||||
// SimpleProfile_SetParameter(SIMPLEPROFILE_CHAR5, SIMPLEPROFILE_CHAR5_LEN, charValue5);
|
||||
}
|
||||
|
||||
// Init Connection Item
|
||||
peripheralInitConnItem(&peripheralConnList);
|
||||
|
||||
// Register callback with SimpleGATTprofile
|
||||
SimpleProfile_RegisterAppCBs(&Peripheral_SimpleProfileCBs);
|
||||
|
||||
// Register receive scan request callback
|
||||
GAPRole_BroadcasterSetCB(&Broadcaster_BroadcasterCBs);
|
||||
|
||||
// Setup a delayed profile startup
|
||||
tmos_set_event(Peripheral_TaskID, SBP_START_DEVICE_EVT);
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn peripheralInitConnItem
|
||||
*
|
||||
* @brief Init Connection Item
|
||||
*
|
||||
* @param peripheralConnList -
|
||||
*
|
||||
* @return NULL
|
||||
*/
|
||||
static void peripheralInitConnItem(peripheralConnItem_t *peripheralConnList)
|
||||
{
|
||||
peripheralConnList->connHandle = GAP_CONNHANDLE_INIT;
|
||||
peripheralConnList->connInterval = 0;
|
||||
peripheralConnList->connSlaveLatency = 0;
|
||||
peripheralConnList->connTimeout = 0;
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn Peripheral_ProcessEvent
|
||||
*
|
||||
* @brief Peripheral Application Task event processor. This function
|
||||
* is called to process all events for the task. Events
|
||||
* include timers, messages and any other user defined events.
|
||||
*
|
||||
* @param task_id - The TMOS assigned task ID.
|
||||
* @param events - events to process. This is a bit map and can
|
||||
* contain more than one event.
|
||||
*
|
||||
* @return events not processed
|
||||
*/
|
||||
uint16_t Peripheral_ProcessEvent(uint8_t task_id, uint16_t events)
|
||||
{
|
||||
// VOID task_id; // TMOS required parameter that isn't used in this function
|
||||
|
||||
if(events & SYS_EVENT_MSG)
|
||||
{
|
||||
uint8_t *pMsg;
|
||||
|
||||
if((pMsg = tmos_msg_receive(Peripheral_TaskID)) != NULL)
|
||||
{
|
||||
Peripheral_ProcessTMOSMsg((tmos_event_hdr_t *)pMsg);
|
||||
// Release the TMOS message
|
||||
tmos_msg_deallocate(pMsg);
|
||||
}
|
||||
// return unprocessed events
|
||||
return (events ^ SYS_EVENT_MSG);
|
||||
}
|
||||
|
||||
if(events & SBP_START_DEVICE_EVT)
|
||||
{
|
||||
// Start the Device
|
||||
GAPRole_PeripheralStartDevice(Peripheral_TaskID, &Peripheral_BondMgrCBs, &Peripheral_PeripheralCBs);
|
||||
return (events ^ SBP_START_DEVICE_EVT);
|
||||
}
|
||||
|
||||
if(events & SBP_PERIODIC_EVT)
|
||||
{
|
||||
// Restart timer
|
||||
if(SBP_PERIODIC_EVT_PERIOD)
|
||||
{
|
||||
tmos_start_task(Peripheral_TaskID, SBP_PERIODIC_EVT, SBP_PERIODIC_EVT_PERIOD);
|
||||
}
|
||||
// Perform periodic application task
|
||||
performPeriodicTask();
|
||||
return (events ^ SBP_PERIODIC_EVT);
|
||||
}
|
||||
|
||||
if(events & SBP_PARAM_UPDATE_EVT)
|
||||
{
|
||||
// Send connect param update request
|
||||
GAPRole_PeripheralConnParamUpdateReq(peripheralConnList.connHandle,
|
||||
DEFAULT_DESIRED_MIN_CONN_INTERVAL,
|
||||
DEFAULT_DESIRED_MAX_CONN_INTERVAL,
|
||||
DEFAULT_DESIRED_SLAVE_LATENCY,
|
||||
DEFAULT_DESIRED_CONN_TIMEOUT,
|
||||
Peripheral_TaskID);
|
||||
|
||||
return (events ^ SBP_PARAM_UPDATE_EVT);
|
||||
}
|
||||
|
||||
if(events & SBP_PHY_UPDATE_EVT)
|
||||
{
|
||||
// start phy update
|
||||
PRINT("PHY Update %x...\n", GAPRole_UpdatePHY(peripheralConnList.connHandle, 0, GAP_PHY_BIT_LE_2M,
|
||||
GAP_PHY_BIT_LE_2M, 0));
|
||||
|
||||
return (events ^ SBP_PHY_UPDATE_EVT);
|
||||
}
|
||||
|
||||
if(events & SBP_READ_RSSI_EVT)
|
||||
{
|
||||
GAPRole_ReadRssiCmd(peripheralConnList.connHandle);
|
||||
tmos_start_task(Peripheral_TaskID, SBP_READ_RSSI_EVT, SBP_READ_RSSI_EVT_PERIOD);
|
||||
return (events ^ SBP_READ_RSSI_EVT);
|
||||
}
|
||||
|
||||
// Discard unknown events
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn Peripheral_ProcessGAPMsg
|
||||
*
|
||||
* @brief Process an incoming task message.
|
||||
*
|
||||
* @param pMsg - message to process
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
static void Peripheral_ProcessGAPMsg(gapRoleEvent_t *pEvent)
|
||||
{
|
||||
switch(pEvent->gap.opcode)
|
||||
{
|
||||
case GAP_SCAN_REQUEST_EVENT:
|
||||
{
|
||||
// PRINT("Receive scan req from %x %x %x %x %x %x ..\n", pEvent->scanReqEvt.scannerAddr[0],
|
||||
// pEvent->scanReqEvt.scannerAddr[1], pEvent->scanReqEvt.scannerAddr[2], pEvent->scanReqEvt.scannerAddr[3],
|
||||
// pEvent->scanReqEvt.scannerAddr[4], pEvent->scanReqEvt.scannerAddr[5]);
|
||||
break;
|
||||
}
|
||||
|
||||
case GAP_PHY_UPDATE_EVENT:
|
||||
{
|
||||
PRINT("Phy update Rx:%x Tx:%x ..\n", pEvent->linkPhyUpdate.connRxPHYS, pEvent->linkPhyUpdate.connTxPHYS);
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn Peripheral_ProcessTMOSMsg
|
||||
*
|
||||
* @brief Process an incoming task message.
|
||||
*
|
||||
* @param pMsg - message to process
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
static void Peripheral_ProcessTMOSMsg(tmos_event_hdr_t *pMsg)
|
||||
{
|
||||
switch(pMsg->event)
|
||||
{
|
||||
case GAP_MSG_EVENT:
|
||||
{
|
||||
Peripheral_ProcessGAPMsg((gapRoleEvent_t *)pMsg);
|
||||
break;
|
||||
}
|
||||
|
||||
case GATT_MSG_EVENT:
|
||||
{
|
||||
gattMsgEvent_t *pMsgEvent;
|
||||
|
||||
pMsgEvent = (gattMsgEvent_t *)pMsg;
|
||||
if(pMsgEvent->method == ATT_MTU_UPDATED_EVENT)
|
||||
{
|
||||
peripheralMTU = pMsgEvent->msg.exchangeMTUReq.clientRxMTU;
|
||||
PRINT("mtu exchange: %d\n", pMsgEvent->msg.exchangeMTUReq.clientRxMTU);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn Peripheral_LinkEstablished
|
||||
*
|
||||
* @brief Process link established.
|
||||
*
|
||||
* @param pEvent - event to process
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
static void Peripheral_LinkEstablished(gapRoleEvent_t *pEvent)
|
||||
{
|
||||
gapEstLinkReqEvent_t *event = (gapEstLinkReqEvent_t *)pEvent;
|
||||
|
||||
// See if already connected
|
||||
if(peripheralConnList.connHandle != GAP_CONNHANDLE_INIT)
|
||||
{
|
||||
GAPRole_TerminateLink(pEvent->linkCmpl.connectionHandle);
|
||||
PRINT("Connection max...\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
peripheralConnList.connHandle = event->connectionHandle;
|
||||
peripheralConnList.connInterval = event->connInterval;
|
||||
peripheralConnList.connSlaveLatency = event->connLatency;
|
||||
peripheralConnList.connTimeout = event->connTimeout;
|
||||
|
||||
// Set timer for periodic event
|
||||
tmos_start_task(Peripheral_TaskID, SBP_PERIODIC_EVT, SBP_PERIODIC_EVT_PERIOD);
|
||||
|
||||
// Set timer for param update event
|
||||
tmos_start_task(Peripheral_TaskID, SBP_PARAM_UPDATE_EVT, SBP_PARAM_UPDATE_DELAY);
|
||||
|
||||
// Start read rssi
|
||||
tmos_start_task(Peripheral_TaskID, SBP_READ_RSSI_EVT, SBP_READ_RSSI_EVT_PERIOD);
|
||||
|
||||
PRINT("Conn %x - Int %x \n", event->connectionHandle, event->connInterval);
|
||||
char _tmp[32] = "";
|
||||
sprintf(_tmp, "Conn %x - Int %x \n", event->connectionHandle, event->connInterval);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn Peripheral_LinkTerminated
|
||||
*
|
||||
* @brief Process link terminated.
|
||||
*
|
||||
* @param pEvent - event to process
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
static void Peripheral_LinkTerminated(gapRoleEvent_t *pEvent)
|
||||
{
|
||||
gapTerminateLinkEvent_t *event = (gapTerminateLinkEvent_t *)pEvent;
|
||||
|
||||
if(event->connectionHandle == peripheralConnList.connHandle)
|
||||
{
|
||||
peripheralConnList.connHandle = GAP_CONNHANDLE_INIT;
|
||||
peripheralConnList.connInterval = 0;
|
||||
peripheralConnList.connSlaveLatency = 0;
|
||||
peripheralConnList.connTimeout = 0;
|
||||
tmos_stop_task(Peripheral_TaskID, SBP_PERIODIC_EVT);
|
||||
tmos_stop_task(Peripheral_TaskID, SBP_READ_RSSI_EVT);
|
||||
|
||||
// Restart advertising
|
||||
{
|
||||
uint8_t advertising_enable = TRUE;
|
||||
GAPRole_SetParameter(GAPROLE_ADVERT_ENABLED, sizeof(uint8_t), &advertising_enable);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
PRINT("ERR..\n");
|
||||
}
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn peripheralRssiCB
|
||||
*
|
||||
* @brief RSSI callback.
|
||||
*
|
||||
* @param connHandle - connection handle
|
||||
* @param rssi - RSSI
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
static void peripheralRssiCB(uint16_t connHandle, int8_t rssi)
|
||||
{
|
||||
// PRINT("RSSI -%d dB Conn %x \n", -rssi, connHandle);
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn peripheralParamUpdateCB
|
||||
*
|
||||
* @brief Parameter update complete callback
|
||||
*
|
||||
* @param connHandle - connect handle
|
||||
* connInterval - connect interval
|
||||
* connSlaveLatency - connect slave latency
|
||||
* connTimeout - connect timeout
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
static void peripheralParamUpdateCB(uint16_t connHandle, uint16_t connInterval,
|
||||
uint16_t connSlaveLatency, uint16_t connTimeout)
|
||||
{
|
||||
if(connHandle == peripheralConnList.connHandle)
|
||||
{
|
||||
peripheralConnList.connInterval = connInterval;
|
||||
peripheralConnList.connSlaveLatency = connSlaveLatency;
|
||||
peripheralConnList.connTimeout = connTimeout;
|
||||
|
||||
PRINT("Update %x - Int %x \n", connHandle, connInterval);
|
||||
}
|
||||
else
|
||||
{
|
||||
PRINT("ERR..\n");
|
||||
}
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn peripheralStateNotificationCB
|
||||
*
|
||||
* @brief Notification from the profile of a state change.
|
||||
*
|
||||
* @param newState - new state
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
static void peripheralStateNotificationCB(gapRole_States_t newState, gapRoleEvent_t *pEvent)
|
||||
{
|
||||
switch(newState & GAPROLE_STATE_ADV_MASK)
|
||||
{
|
||||
case GAPROLE_STARTED:
|
||||
PRINT("Initialized..\n");
|
||||
break;
|
||||
|
||||
case GAPROLE_ADVERTISING:
|
||||
if(pEvent->gap.opcode == GAP_LINK_TERMINATED_EVENT)
|
||||
{
|
||||
Peripheral_LinkTerminated(pEvent);
|
||||
PRINT("Disconnected.. Reason1:%x\n", pEvent->linkTerminate.reason);
|
||||
PRINT("Advertising..\n");
|
||||
char _tmp[32] = "";
|
||||
sprintf(_tmp, "Disconnected.. Reason:%x\n", pEvent->linkTerminate.reason);
|
||||
g_flag_bt_state = 0;
|
||||
}
|
||||
else if(pEvent->gap.opcode == GAP_MAKE_DISCOVERABLE_DONE_EVENT)
|
||||
{
|
||||
PRINT("Advertising..\n");
|
||||
}
|
||||
break;
|
||||
|
||||
case GAPROLE_CONNECTED:
|
||||
if(pEvent->gap.opcode == GAP_LINK_ESTABLISHED_EVENT)
|
||||
{
|
||||
Peripheral_LinkEstablished(pEvent);
|
||||
PRINT("Connected..\n");
|
||||
g_flag_bt_state = 1;
|
||||
}
|
||||
break;
|
||||
|
||||
case GAPROLE_CONNECTED_ADV:
|
||||
if(pEvent->gap.opcode == GAP_MAKE_DISCOVERABLE_DONE_EVENT)
|
||||
{
|
||||
PRINT("Connected Advertising..\n");
|
||||
}
|
||||
break;
|
||||
|
||||
case GAPROLE_WAITING:
|
||||
if(pEvent->gap.opcode == GAP_END_DISCOVERABLE_DONE_EVENT)
|
||||
{
|
||||
PRINT("Waiting for advertising..\n");
|
||||
}
|
||||
else if(pEvent->gap.opcode == GAP_LINK_TERMINATED_EVENT)
|
||||
{
|
||||
Peripheral_LinkTerminated(pEvent);
|
||||
g_flag_bt_state = 0;
|
||||
PRINT("Disconnected.. Reason2:%x\n", pEvent->linkTerminate.reason);
|
||||
NVIC_SystemReset();
|
||||
}
|
||||
else if(pEvent->gap.opcode == GAP_LINK_ESTABLISHED_EVENT)
|
||||
{
|
||||
if(pEvent->gap.hdr.status != SUCCESS)
|
||||
{
|
||||
PRINT("Waiting for advertising..\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
PRINT("Error..\n");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
PRINT("Error..%x\n", pEvent->gap.opcode);
|
||||
}
|
||||
break;
|
||||
|
||||
case GAPROLE_ERROR:
|
||||
g_flag_bt_state = 0;
|
||||
PRINT("Error..\n");
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn performPeriodicTask
|
||||
*
|
||||
* @brief Perform a periodic application task. This function gets
|
||||
* called every five seconds as a result of the SBP_PERIODIC_EVT
|
||||
* TMOS event. In this example, the value of the third
|
||||
* characteristic in the SimpleGATTProfile service is retrieved
|
||||
* from the profile, and then copied into the value of the
|
||||
* the fourth characteristic.
|
||||
*
|
||||
* @param none
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
static void performPeriodicTask(void)
|
||||
{
|
||||
// uint8_t notiData[SIMPLEPROFILE_CHAR4_LEN] = {0x88};
|
||||
// peripheralChar4Notify(notiData, SIMPLEPROFILE_CHAR4_LEN);
|
||||
if(g_flag_notify_temp){
|
||||
g_flag_notify_temp = 0;
|
||||
peripheralChar4Notify(g_notify_buftemp.buf, g_notify_buftemp.len);
|
||||
clear_ble_notify_buf(&g_notify_buftemp);
|
||||
}
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn peripheralChar4Notify
|
||||
*
|
||||
* @brief Prepare and send simpleProfileChar4 notification
|
||||
*
|
||||
* @param pValue - data to notify
|
||||
* len - length of data
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
static void peripheralChar4Notify(uint8_t *pValue, uint16_t len)
|
||||
{
|
||||
attHandleValueNoti_t noti;
|
||||
if(len > (peripheralMTU - 3))
|
||||
{
|
||||
PRINT("Too large noti\n");
|
||||
return;
|
||||
}
|
||||
noti.len = len;
|
||||
noti.pValue = GATT_bm_alloc(peripheralConnList.connHandle, ATT_HANDLE_VALUE_NOTI, noti.len, NULL, 0);
|
||||
if(noti.pValue)
|
||||
{
|
||||
tmos_memcpy(noti.pValue, pValue, noti.len);
|
||||
if(simpleProfile_Notify(peripheralConnList.connHandle, ¬i) != SUCCESS)
|
||||
{
|
||||
GATT_bm_free((gattMsg_t *)¬i, ATT_HANDLE_VALUE_NOTI);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Jump_OTA(void);
|
||||
/*********************************************************************
|
||||
* @fn simpleProfileChangeCB
|
||||
*
|
||||
* @brief Callback from SimpleBLEProfile indicating a value change
|
||||
*
|
||||
* @param paramID - parameter ID of the value that was changed.
|
||||
* pValue - pointer to data that was changed
|
||||
* len - length of data
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
static void simpleProfileChangeCB(uint8_t paramID, uint8_t *pValue, uint16_t len)
|
||||
{
|
||||
switch(paramID)
|
||||
{
|
||||
case SIMPLEPROFILE_CHAR1:
|
||||
{
|
||||
// uint8_t newValue[SIMPLEPROFILE_CHAR1_LEN];
|
||||
// tmos_memcpy(newValue, pValue, len);
|
||||
// PRINT("profile ChangeCB CHAR1.. \n");
|
||||
|
||||
g_ble_rcv_buf.len = len;
|
||||
tmos_memcpy(g_ble_rcv_buf.buf, pValue, len);
|
||||
g_ble_rcv_buf.flag = 1;
|
||||
break;
|
||||
}
|
||||
|
||||
case SIMPLEPROFILE_CHAR3:
|
||||
{
|
||||
uint8_t newValue[SIMPLEPROFILE_CHAR3_LEN];
|
||||
tmos_memcpy(newValue, pValue, len);
|
||||
if(newValue[0] == 0x3F && newValue[1] == 0xF3 && newValue[2] == 0x23 &&
|
||||
newValue[3] == 'O' && newValue[4] == 'T' && newValue[5] == 'A'){
|
||||
PRINT("profile ChangeCB CHAR3..\n");
|
||||
PRINT("jump OTA \n");
|
||||
Delay_Ms(5);
|
||||
Jump_OTA();
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
// should not reach here!
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* OTA upgrade logo */
|
||||
#define IMAGE_OTA_FLAG 0x03
|
||||
|
||||
/* Store on the DataFlash address, the position of Bluetooth cannot be occupied */
|
||||
#define OTA_DATAFLASH_ADD 0x08077000
|
||||
|
||||
/* Flash data temporary storage */
|
||||
__attribute__((aligned(8))) uint8_t block_buf[256];
|
||||
|
||||
/*********************************************************************
|
||||
* @fn FLASH_read
|
||||
*
|
||||
* @brief ¶Á flash
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
void FLASH_read(uint32_t addr, uint8_t *pData, uint32_t len)
|
||||
{
|
||||
uint32_t i;
|
||||
for(i=0;i<len;i++)
|
||||
{
|
||||
*pData++ = *(uint8_t*)addr++;
|
||||
}
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn Jump_OTA
|
||||
*
|
||||
* @brief Jump to OTA upgrade
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
void Jump_OTA(void)
|
||||
{
|
||||
uint16_t i;
|
||||
uint32_t ver_flag;
|
||||
|
||||
/* Read the first block */
|
||||
FLASH_read(OTA_DATAFLASH_ADD, (uint8_t *)&block_buf[0], 4);
|
||||
|
||||
FLASH_Unlock_Fast();
|
||||
/* Erase the first block */
|
||||
FLASH_ErasePage_Fast( OTA_DATAFLASH_ADD );
|
||||
|
||||
/* Update Image information */
|
||||
block_buf[0] = IMAGE_OTA_FLAG;
|
||||
block_buf[1] = 0x5A;
|
||||
block_buf[2] = 0x5A;
|
||||
block_buf[3] = 0x5A;
|
||||
|
||||
/* Programming DataFlash */
|
||||
FLASH_ProgramPage_Fast(OTA_DATAFLASH_ADD, (uint32_t *)&block_buf[0]);
|
||||
FLASH_Lock_Fast();
|
||||
|
||||
/* Software reset */
|
||||
NVIC_SystemReset();
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
*********************************************************************/
|
||||
360
vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/peripheral_main.c
Normal file
360
vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/peripheral_main.c
Normal file
@@ -0,0 +1,360 @@
|
||||
/********************************** (C) COPYRIGHT *******************************
|
||||
* File Name : main.c
|
||||
* Author : WCH
|
||||
* Version : V1.1
|
||||
* Date : 2020/08/06
|
||||
* Description : Peripheral slave application main function and task system initialization
|
||||
*********************************************************************************
|
||||
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
|
||||
* Attention: This software (modified or not) and binary are used for
|
||||
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
|
||||
*******************************************************************************/
|
||||
|
||||
/******************************************************************************/
|
||||
/* Header file contains */
|
||||
#include "CONFIG.h"
|
||||
#include "HAL.h"
|
||||
#include "gattprofile.h"
|
||||
#include "peripheral.h"
|
||||
#include "cmcng.h"
|
||||
#include <string.h>
|
||||
#include "dbn_ble_srv.h"
|
||||
#include "eth_driver.h"
|
||||
#include "net_srv.h"
|
||||
#include "storage.h"
|
||||
|
||||
/*********************************************************************
|
||||
* GLOBAL TYPEDEFS
|
||||
*/
|
||||
__attribute__((aligned(4))) uint32_t MEM_BUF[BLE_MEMHEAP_SIZE / 4];
|
||||
|
||||
#define KEY_GPIO (RCC_APB2Periph_GPIOA)
|
||||
#define KEY_BV BV(0)
|
||||
#define KEY_IN (GPIO_ReadInputDataBit(KEY_GPIO, KEY_BV)==0)
|
||||
#define HAL_PUSH_BUTTON() (KEY1_IN) //Add custom button
|
||||
|
||||
#if(defined(BLE_MAC)) && (BLE_MAC == TRUE)
|
||||
const uint8_t MacAddr[6] = {0x84, 0xC2, 0xE4, 0x03, 0x02, 0x02};
|
||||
// #else
|
||||
// uint8_t MACAddr[6] = {0x84, 0xC2, 0xE4, 0x03, 0x02, 0x02};
|
||||
#endif
|
||||
uint8_t gMacAddr[6] = { 0x38, 0x3B, 0x26, 0x11, 0xA4 ,0x35 };
|
||||
|
||||
/* Used for app judgment file effectiveness */
|
||||
const uint32_t Address = 0xFFFFFFFF;
|
||||
|
||||
__attribute__((aligned(4))) uint32_t Image_Flag __attribute__((section(".ImageFlag"))) = (uint32_t)&Address;
|
||||
|
||||
uint8_t g_dev_number[6] = ""; // É豸±àºÅ ²úÆ·±àºÅ
|
||||
uint8_t g_dev_password[6] = {0x31, 0x32, 0x33, 0x34, 0x35, 0x36};
|
||||
uint8_t g_ble_safe_flag = 0;
|
||||
uint32_t g_ble_safe_counter_ori = 0;
|
||||
uint32_t g_ble_safe_counter_dst = 0;
|
||||
|
||||
char g_flag_debug = 1;
|
||||
|
||||
uint8_t g_dg_device_type = DDType_DLD950V4; // ???¨¨¡À?????????????
|
||||
uint8_t g_dg_sub_dev_type = DDType_DLD950V4;
|
||||
|
||||
Sub_Code_Enable g_sub_code_enable = {0,};
|
||||
|
||||
uint32_t g_activ_counter = 0;
|
||||
|
||||
uint32_t g_counter_bt_timeout = 0;
|
||||
uint8_t g_flag_bt_state = 0;
|
||||
uint8_t g_flag_bt_disable = 0;
|
||||
|
||||
uint8_t g_max_counter_bt_min = 0;
|
||||
uint32_t g_max_counter_bt_timeout = 0; //BT_DISABLE_IDLE_TIMEOUT * 60 * 1000; // ms unit
|
||||
|
||||
|
||||
__IO uint32_t TimingDelayInc;
|
||||
__IO uint32_t TimingDelayDec;
|
||||
|
||||
uint8_t trigB;
|
||||
Pkg_Uart g_pkg_uart_1 = { 0, 0, 0, "", 0};
|
||||
Pkg_Uart g_pkg_uart_2 = { 0, 0, 0, "", 0};
|
||||
|
||||
Flag_Counter g_flag_counter_key = {0, 0, 0};
|
||||
Flag_Counter g_flag_counter_ota = {0, 0, 0};
|
||||
|
||||
uint8_t g_storage_uart_num = 0;
|
||||
uint32_t g_storage_uart_baud = 19200;//9600;
|
||||
uint8_t g_storage_uart_num_2 = 1;
|
||||
uint32_t g_storage_uart_baud_2 = 115200; //115200;
|
||||
|
||||
uint32_t mstick(void){
|
||||
return TimingDelayInc;
|
||||
}
|
||||
|
||||
|
||||
void InitPkgUart(Pkg_Uart * pkg){
|
||||
memset(pkg->pkg, 0, BUFF_STACK_SIZE);
|
||||
pkg->flag = 0;
|
||||
pkg->tick = 0;
|
||||
pkg->len = 0;
|
||||
pkg->offset = 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*********************************************************************
|
||||
* @fn TIM2_Init
|
||||
*
|
||||
* @brief Initializes TIM2.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
void TIM2_Init( void )
|
||||
{
|
||||
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure={0};
|
||||
|
||||
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
|
||||
|
||||
TIM_TimeBaseStructure.TIM_Period = SystemCoreClock / 1000000;
|
||||
TIM_TimeBaseStructure.TIM_Prescaler = WCHNETTIMERPERIOD * 1000 - 1;
|
||||
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
|
||||
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
|
||||
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
|
||||
TIM_ITConfig(TIM2, TIM_IT_Update ,ENABLE);
|
||||
|
||||
TIM_Cmd(TIM2, ENABLE);
|
||||
TIM_ClearITPendingBit(TIM2, TIM_IT_Update );
|
||||
NVIC_EnableIRQ(TIM2_IRQn);
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn TIM3_Init
|
||||
*
|
||||
* @brief Initializes TIM3.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
void TIM3_Init(void)
|
||||
{
|
||||
NVIC_InitTypeDef NVIC_InitStructure={0};
|
||||
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure = { 0 };
|
||||
|
||||
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
|
||||
|
||||
////10ms
|
||||
// TIM_TimeBaseStructure.TIM_Period = SystemCoreClock / 1000000;
|
||||
// TIM_TimeBaseStructure.TIM_Prescaler = 10 * 1000 - 1;
|
||||
|
||||
//1ms
|
||||
TIM_TimeBaseStructure.TIM_Period = SystemCoreClock / 1000000;
|
||||
TIM_TimeBaseStructure.TIM_Prescaler = 1 * 1000 - 1;
|
||||
|
||||
|
||||
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
|
||||
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
|
||||
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
|
||||
TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
|
||||
|
||||
TIM_Cmd(TIM3, ENABLE);
|
||||
TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
|
||||
NVIC_EnableIRQ(TIM3_IRQn);
|
||||
}
|
||||
|
||||
|
||||
void key_event_srv(void){
|
||||
// if(g_flag_counter_key.tick > 1000){
|
||||
// PRINT("key_tick_timeup_______: %d\n", GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0));
|
||||
// g_flag_counter_key.tick = 0;
|
||||
// }
|
||||
if(g_flag_counter_key.flag){
|
||||
uint8_t _pkg[7] = {0};
|
||||
uint8_t i = 0;
|
||||
switch (g_flag_counter_key.flag) {
|
||||
case KEY_ET_BLE_ENABLE: {
|
||||
PRINT("______KEY_ET_BLE_ENABLE\n");
|
||||
NVIC_SystemReset();
|
||||
} break;
|
||||
case KEY_ET_REBOOT: {
|
||||
PRINT("__________KEY_ET_REBOOT\n");
|
||||
_pkg[i++] = 0x7F;
|
||||
_pkg[i++] = 0x00;
|
||||
_pkg[i++] = 0x01;
|
||||
_pkg[i++] = 0x6D;
|
||||
_pkg[i++] = 0x6C;
|
||||
_pkg[i++] = 0x6E;
|
||||
UART2_SendString(_pkg, i); //1 send loop mcu reboot;
|
||||
|
||||
Delay_Ms(10);
|
||||
// 2 DBN dev reboot
|
||||
NVIC_SystemReset();
|
||||
} break;
|
||||
case KEY_ET_FACTORY: {
|
||||
PRINT("_____________KEY_ET_FACTORY\n");
|
||||
_pkg[i++] = 0x7F;
|
||||
_pkg[i++] = 0x00;
|
||||
_pkg[i++] = 0x01;
|
||||
_pkg[i++] = 0x92;
|
||||
_pkg[i++] = 0x93;
|
||||
_pkg[i++] = 0x93;
|
||||
UART2_SendString(_pkg, i); //1 send loop mcu factory init
|
||||
|
||||
|
||||
} break;
|
||||
case KEY_ET_NETBLE_FACTORY: {
|
||||
//2 DBN dev factory init;
|
||||
factory_dev_info();
|
||||
Delay_Ms(10);
|
||||
|
||||
NVIC_SystemReset();
|
||||
} break;
|
||||
|
||||
}
|
||||
g_flag_counter_key.flag = 0;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*********************************************************************
|
||||
* @fn Main_Circulation
|
||||
*
|
||||
* @brief Main loop
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
__attribute__((section(".highcode")))
|
||||
__attribute__((noinline))
|
||||
void Main_Circulation(void)
|
||||
{
|
||||
uint32_t _counter = 0;
|
||||
while(1)
|
||||
{
|
||||
TMOS_SystemProcess();
|
||||
|
||||
if(g_net_state.flag < 2)
|
||||
{
|
||||
net_srv_init();
|
||||
}
|
||||
if(g_net_state.flag == 2)
|
||||
{
|
||||
if(g_sub_code_enable.iot_enable){
|
||||
if(iot_net_info.mode == IOT_Addr_IP_Mode){
|
||||
if(get_ipstr_to_array(iot_net_info.remote_addr, RemoteIP) == 0){
|
||||
WCHNET_CreateTcpMqttSocket();
|
||||
}
|
||||
}
|
||||
else{
|
||||
//DNS
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
WCHNET_CreateTcpSocket();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*Ethernet library main task function,
|
||||
* which needs to be called cyclically*/
|
||||
WCHNET_MainTask();
|
||||
/*Query the Ethernet global interrupt,
|
||||
* if there is an interrupt, call the global interrupt handler*/
|
||||
if(WCHNET_QueryGlobalInt())
|
||||
{
|
||||
WCHNET_HandleGlobalInt();
|
||||
}
|
||||
|
||||
uart_srv();
|
||||
|
||||
poll_dbn_ble();
|
||||
|
||||
key_event_srv();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*********************************************************************
|
||||
* @fn main
|
||||
*
|
||||
* @brief Main function
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
int main(void)
|
||||
{
|
||||
SystemCoreClockUpdate();
|
||||
Delay_Init();
|
||||
#ifdef DEBUG
|
||||
// USART_Printf_Init( 115200 );
|
||||
USART_Printf_Init( 256000 );
|
||||
#endif
|
||||
PRINT("%s\n", VER_LIB);
|
||||
PRINT("SystemCoreClock:%d\n", SystemCoreClock);
|
||||
GetMacAddr(gMacAddr);
|
||||
storage_init();
|
||||
load_cfg_from_flash();
|
||||
output_cfg_from_flash();
|
||||
|
||||
PRINT("MAC: %02X %02X %02X %02X %02X %02X\r\n", gMacAddr[0],gMacAddr[1], gMacAddr[2], gMacAddr[3],gMacAddr[4],gMacAddr[5]);
|
||||
PRINT("net version:%x\n", WCHNET_GetVer());
|
||||
WCHBLE_Init();
|
||||
HAL_Init();
|
||||
|
||||
uart_init();
|
||||
TIM3_Init();
|
||||
TIM2_Init();
|
||||
|
||||
GAPRole_PeripheralInit();
|
||||
Peripheral_Init();
|
||||
Main_Circulation();
|
||||
}
|
||||
|
||||
|
||||
void TIM3_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
|
||||
/*********************************************************************
|
||||
* @fn TIM3_IRQHandler, 1ms
|
||||
*
|
||||
* @brief This function handles TIM2 exception.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
void TIM3_IRQHandler(void)
|
||||
{
|
||||
TimingDelayInc++;
|
||||
g_activ_counter++;
|
||||
|
||||
if(g_pkg_uart_2.offset){
|
||||
if(g_pkg_uart_2.flag == 0){
|
||||
g_pkg_uart_2.tick++;
|
||||
if(g_pkg_uart_2.tick > 8){
|
||||
g_pkg_uart_2.flag = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0) == 0){//HAL_PUSH_BUTTON()){
|
||||
g_flag_counter_key.tick++;
|
||||
}
|
||||
else{
|
||||
if(g_flag_counter_key.tick){
|
||||
if(g_flag_counter_key.tick < 2000){
|
||||
g_flag_counter_key.flag = KEY_ET_BLE_ENABLE;
|
||||
}
|
||||
else if(g_flag_counter_key.tick < 6000){
|
||||
g_flag_counter_key.flag = KEY_ET_REBOOT;
|
||||
}
|
||||
else if(g_flag_counter_key.tick < 10000){
|
||||
g_flag_counter_key.flag = KEY_ET_FACTORY;
|
||||
}
|
||||
else{
|
||||
g_flag_counter_key.flag = KEY_ET_NETBLE_FACTORY;
|
||||
}
|
||||
g_flag_counter_key.tick = 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
|
||||
}
|
||||
|
||||
|
||||
/******************************** endfile @ main ******************************/
|
||||
211
vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/simple_json.c
Normal file
211
vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/simple_json.c
Normal file
@@ -0,0 +1,211 @@
|
||||
/*******************************************************
|
||||
* File name: simple_json.c
|
||||
* Author : wfq
|
||||
* Versions : 1.0
|
||||
* Description: simple json parse function.
|
||||
* Do not solve the completed json_string or string with json-string.
|
||||
* Completed: like array.
|
||||
* Must satisfy the yipai dev-protocol, and test by yipai dev-protocol.
|
||||
* History:
|
||||
* 1.Date: 2016-10-10
|
||||
* Author: wfq
|
||||
* Action: create
|
||||
*********************************************************/
|
||||
|
||||
|
||||
#include "simple_json.h"
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
void simple_parse_json(const char * data, char *key_str, char *value_str)
|
||||
{
|
||||
char *p, *q, *t, *y;
|
||||
//char * zq; //[ ]
|
||||
int len = 0;
|
||||
|
||||
if(data == NULL)
|
||||
{
|
||||
printf("\r\nsimple_parse_json data is NULL\r\n");
|
||||
return;
|
||||
}
|
||||
if(strlen(value_str) != 0)
|
||||
{
|
||||
printf("clear value:%s\n", value_str);
|
||||
memset(value_str, 0, strlen(value_str));
|
||||
}
|
||||
|
||||
p = strstr(data, key_str);
|
||||
if(p == NULL) return;
|
||||
p += strlen(key_str) + 1;
|
||||
|
||||
// printf("\nmy_parse_json2, first,data:%s\n", p);
|
||||
while(*p == ' ')
|
||||
{
|
||||
p++;
|
||||
}
|
||||
// printf("\nmy_parse_json2, sec,data:%s\n", p);
|
||||
|
||||
q = strchr(p, '{');
|
||||
t = strchr(p, '\"');
|
||||
y = strchr(p, ',');
|
||||
//zq = strchr(p, '[');
|
||||
|
||||
if((q == NULL) && (t == NULL) && (y == NULL))
|
||||
{
|
||||
q = strstr(p, "\r\n");
|
||||
if(q == NULL) q = strstr(p, "\n");
|
||||
if(q == NULL) q = strstr(p, "}");
|
||||
|
||||
if(q == NULL) len = strlen(p);
|
||||
else len = strlen(p) - strlen(q);
|
||||
|
||||
memcpy(value_str, p, len);
|
||||
return;
|
||||
}
|
||||
|
||||
if((q == NULL) || ((t != NULL) && (strlen(t) > strlen(q))))
|
||||
{
|
||||
if((y != NULL) && (strlen(y) > strlen(t)))
|
||||
{
|
||||
len = strlen(p) - strlen(y);
|
||||
memcpy(value_str, p, len);
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
t += 1;
|
||||
q = strstr(t, "\",");
|
||||
if(q == NULL)
|
||||
{
|
||||
q = strstr(t, "\"\r\n");
|
||||
if(q == NULL)
|
||||
{
|
||||
q = strstr(t, "\"\n");
|
||||
if(q == NULL)
|
||||
{
|
||||
q = strstr(t, "\"");
|
||||
}
|
||||
}
|
||||
}
|
||||
len = strlen(t) - strlen(q);
|
||||
memcpy(value_str, t, len);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
q += 1;
|
||||
y = strstr(q, "},");
|
||||
if(y == NULL)
|
||||
{
|
||||
y = strstr(q, "}\r\n");
|
||||
if(y == NULL) strstr(q, "}\n");
|
||||
}
|
||||
len = strlen(q) - strlen(y);
|
||||
memcpy(value_str, q, len);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
char * simpleJsonGetObject(const char * data, const char * key)
|
||||
{
|
||||
if(data == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void simple_json_getarray_item(const char * data, char *key_str, char *value_str)
|
||||
{
|
||||
char *p, *q, *t, *y;
|
||||
//char * zq; //[ ]
|
||||
int len = 0;
|
||||
|
||||
if(data == NULL)
|
||||
{
|
||||
printf("\r\nsimple_parse_json data is NULL\r\n");
|
||||
return;
|
||||
}
|
||||
if(strlen(value_str) != 0)
|
||||
{
|
||||
printf("clear value:%s\n", value_str);
|
||||
memset(value_str, 0, strlen(value_str));
|
||||
}
|
||||
|
||||
p = strstr(data, key_str);
|
||||
if(p == NULL) return;
|
||||
p += strlen(key_str) + 1;
|
||||
|
||||
// printf("\nmy_parse_json2, first,data:%s\n", p);
|
||||
while(*p == ' ')
|
||||
{
|
||||
p++;
|
||||
}
|
||||
// printf("\nmy_parse_json2, sec,data:%s\n", p);
|
||||
|
||||
q = strchr(p, '{');
|
||||
t = strchr(p, '\"');
|
||||
y = strchr(p, ',');
|
||||
//zq = strchr(p, '[');
|
||||
|
||||
if((q == NULL) && (t == NULL) && (y == NULL))
|
||||
{
|
||||
q = strstr(p, "\r\n");
|
||||
if(q == NULL) q = strstr(p, "\n");
|
||||
if(q == NULL) q = strstr(p, "}");
|
||||
|
||||
if(q == NULL) len = strlen(p);
|
||||
else len = strlen(p) - strlen(q);
|
||||
|
||||
memcpy(value_str, p, len);
|
||||
return;
|
||||
}
|
||||
|
||||
if((q == NULL) || ((t != NULL) && (strlen(t) > strlen(q))))
|
||||
{
|
||||
if((y != NULL) && (strlen(y) > strlen(t)))
|
||||
{
|
||||
len = strlen(p) - strlen(y);
|
||||
memcpy(value_str, p, len);
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
t += 1;
|
||||
q = strstr(t, "\",");
|
||||
if(q == NULL)
|
||||
{
|
||||
q = strstr(t, "\"\r\n");
|
||||
if(q == NULL)
|
||||
{
|
||||
q = strstr(t, "\"\n");
|
||||
if(q == NULL)
|
||||
{
|
||||
q = strstr(t, "\"");
|
||||
}
|
||||
}
|
||||
}
|
||||
len = strlen(t) - strlen(q);
|
||||
memcpy(value_str, t, len);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
q += 1;
|
||||
y = strstr(q, "},");
|
||||
if(y == NULL)
|
||||
{
|
||||
y = strstr(q, "}\r\n");
|
||||
if(y == NULL) strstr(q, "}\n");
|
||||
}
|
||||
len = strlen(q) - strlen(y);
|
||||
memcpy(value_str, q, len);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
499
vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/storage.c
Normal file
499
vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/storage.c
Normal file
@@ -0,0 +1,499 @@
|
||||
/*
|
||||
*@Note
|
||||
*SPI interface operation flash peripheral routine:
|
||||
*Master:SPI1_SCK(PA5)¡¢SPI1_MISO(PA6)¡¢SPI1_MOSI(PA7).
|
||||
*This example demonstrates SPI operation Winbond W25Qxx SPIFLASH.
|
||||
*
|
||||
*pins:
|
||||
* CS -- PA4
|
||||
* DO -- PA6(SPI1_MISO)
|
||||
* WP -- 3.3V
|
||||
* DI -- PA7(SPI1_MOSI)
|
||||
* CLK -- PA5(SPI1_SCK)
|
||||
* HOLD -- 3.3V
|
||||
*
|
||||
*/
|
||||
|
||||
#include "debug.h"
|
||||
#include "string.h"
|
||||
|
||||
|
||||
/* Winbond SPIFalsh ID */
|
||||
#define W25Q80 0XEF13
|
||||
#define W25Q16 0XEF14
|
||||
#define W25Q32 0XEF15
|
||||
#define W25Q64 0XEF16 //64Mbit, 8MByte
|
||||
#define W25Q128 0XEF17
|
||||
|
||||
/* Winbond SPIFalsh Instruction List */
|
||||
#define W25X_WriteEnable 0x06
|
||||
#define W25X_WriteDisable 0x04
|
||||
#define W25X_ReadStatusReg 0x05
|
||||
#define W25X_WriteStatusReg 0x01
|
||||
#define W25X_ReadData 0x03
|
||||
#define W25X_FastReadData 0x0B
|
||||
#define W25X_FastReadDual 0x3B
|
||||
#define W25X_PageProgram 0x02
|
||||
#define W25X_BlockErase 0xD8
|
||||
#define W25X_SectorErase 0x20
|
||||
#define W25X_ChipErase 0xC7
|
||||
#define W25X_PowerDown 0xB9
|
||||
#define W25X_ReleasePowerDown 0xAB
|
||||
#define W25X_DeviceID 0xAB
|
||||
#define W25X_ManufactDeviceID 0x90
|
||||
#define W25X_JedecDeviceID 0x9F
|
||||
|
||||
/* Global define */
|
||||
|
||||
/* Global Variable */
|
||||
u8 SPI_FLASH_BUF[4096];
|
||||
const u8 TEXT_Buf[] = {"CH32V203 SPI FLASH W25Qxx"};
|
||||
#define SIZE sizeof(TEXT_Buf)
|
||||
|
||||
/*********************************************************************
|
||||
* @fn SPI1_ReadWriteByte
|
||||
*
|
||||
* @brief SPI1 read or write one byte.
|
||||
*
|
||||
* @param TxData - write one byte data.
|
||||
*
|
||||
* @return Read one byte data.
|
||||
*/
|
||||
u8 SPI1_ReadWriteByte(u8 TxData)
|
||||
{
|
||||
u8 i = 0;
|
||||
|
||||
while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
|
||||
|
||||
SPI_I2S_SendData(SPI1, TxData);
|
||||
i = 0;
|
||||
|
||||
while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
|
||||
|
||||
return SPI_I2S_ReceiveData(SPI1);
|
||||
}
|
||||
|
||||
|
||||
/*********************************************************************
|
||||
* @fn SPI_Flash_Init
|
||||
*
|
||||
* @brief Configuring the SPI for operation flash.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
void SPI_Flash_Init(void)
|
||||
{
|
||||
GPIO_InitTypeDef GPIO_InitStructure = {0};
|
||||
SPI_InitTypeDef SPI_InitStructure = {0};
|
||||
|
||||
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_SPI1, ENABLE);
|
||||
|
||||
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
|
||||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
|
||||
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
||||
GPIO_Init(GPIOA, &GPIO_InitStructure);
|
||||
GPIO_SetBits(GPIOA, GPIO_Pin_4);
|
||||
|
||||
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
|
||||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
|
||||
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
||||
GPIO_Init(GPIOA, &GPIO_InitStructure);
|
||||
|
||||
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
|
||||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
|
||||
GPIO_Init(GPIOA, &GPIO_InitStructure);
|
||||
|
||||
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
|
||||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
|
||||
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
||||
GPIO_Init(GPIOA, &GPIO_InitStructure);
|
||||
|
||||
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
|
||||
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
|
||||
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
|
||||
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
|
||||
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
|
||||
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
|
||||
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
|
||||
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
|
||||
SPI_InitStructure.SPI_CRCPolynomial = 7;
|
||||
SPI_Init(SPI1, &SPI_InitStructure);
|
||||
|
||||
SPI_Cmd(SPI1, ENABLE);
|
||||
}
|
||||
|
||||
|
||||
/*********************************************************************
|
||||
* @fn SPI_Flash_ReadSR
|
||||
*
|
||||
* @brief Read W25Qxx status register.
|
||||
* £¡£¡BIT7 6 5 4 3 2 1 0
|
||||
* £¡£¡SPR RV TB BP2 BP1 BP0 WEL BUSY
|
||||
*
|
||||
* @return byte - status register value.
|
||||
*/
|
||||
u8 SPI_Flash_ReadSR(void)
|
||||
{
|
||||
u8 byte = 0;
|
||||
|
||||
GPIO_WriteBit(GPIOA, GPIO_Pin_4, 0);
|
||||
SPI1_ReadWriteByte(W25X_ReadStatusReg);
|
||||
byte = SPI1_ReadWriteByte(0Xff);
|
||||
GPIO_WriteBit(GPIOA, GPIO_Pin_4, 1);
|
||||
|
||||
return byte;
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn SPI_FLASH_Write_SR
|
||||
*
|
||||
* @brief Write W25Qxx status register.
|
||||
*
|
||||
* @param sr - status register value.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
void SPI_FLASH_Write_SR(u8 sr)
|
||||
{
|
||||
GPIO_WriteBit(GPIOA, GPIO_Pin_4, 0);
|
||||
SPI1_ReadWriteByte(W25X_WriteStatusReg);
|
||||
SPI1_ReadWriteByte(sr);
|
||||
GPIO_WriteBit(GPIOA, GPIO_Pin_4, 1);
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn SPI_Flash_Wait_Busy
|
||||
*
|
||||
* @brief Wait flash free.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
void SPI_Flash_Wait_Busy(void)
|
||||
{
|
||||
while((SPI_Flash_ReadSR() & 0x01) == 0x01);
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn SPI_FLASH_Write_Enable
|
||||
*
|
||||
* @brief Enable flash write.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
void SPI_FLASH_Write_Enable(void)
|
||||
{
|
||||
GPIO_WriteBit(GPIOA, GPIO_Pin_4, 0);
|
||||
SPI1_ReadWriteByte(W25X_WriteEnable);
|
||||
GPIO_WriteBit(GPIOA, GPIO_Pin_4, 1);
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn SPI_FLASH_Write_Disable
|
||||
*
|
||||
* @brief Disable flash write.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
void SPI_FLASH_Write_Disable(void)
|
||||
{
|
||||
GPIO_WriteBit(GPIOA, GPIO_Pin_4, 0);
|
||||
SPI1_ReadWriteByte(W25X_WriteDisable);
|
||||
GPIO_WriteBit(GPIOA, GPIO_Pin_4, 1);
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn SPI_Flash_ReadID
|
||||
*
|
||||
* @brief Read flash ID.
|
||||
*
|
||||
* @return Temp - FLASH ID.
|
||||
*/
|
||||
u16 SPI_Flash_ReadID(void)
|
||||
{
|
||||
u16 Temp = 0;
|
||||
|
||||
GPIO_WriteBit(GPIOA, GPIO_Pin_4, 0);
|
||||
SPI1_ReadWriteByte(W25X_ManufactDeviceID);
|
||||
SPI1_ReadWriteByte(0x00);
|
||||
SPI1_ReadWriteByte(0x00);
|
||||
SPI1_ReadWriteByte(0x00);
|
||||
Temp |= SPI1_ReadWriteByte(0xFF) << 8;
|
||||
Temp |= SPI1_ReadWriteByte(0xFF);
|
||||
GPIO_WriteBit(GPIOA, GPIO_Pin_4, 1);
|
||||
|
||||
return Temp;
|
||||
}
|
||||
|
||||
|
||||
/*********************************************************************
|
||||
* @fn SPI_Flash_Erase_Sector
|
||||
*
|
||||
* @brief Erase one sector(4Kbyte).
|
||||
*
|
||||
* @param Dst_Addr - 0 £¡£¡ 2047
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
void SPI_Flash_Erase_Sector(u32 Dst_Addr)
|
||||
{
|
||||
Dst_Addr *= 4096;
|
||||
SPI_FLASH_Write_Enable();
|
||||
SPI_Flash_Wait_Busy();
|
||||
GPIO_WriteBit(GPIOA, GPIO_Pin_4, 0);
|
||||
SPI1_ReadWriteByte(W25X_SectorErase);
|
||||
SPI1_ReadWriteByte((u8)((Dst_Addr) >> 16));
|
||||
SPI1_ReadWriteByte((u8)((Dst_Addr) >> 8));
|
||||
SPI1_ReadWriteByte((u8)Dst_Addr);
|
||||
GPIO_WriteBit(GPIOA, GPIO_Pin_4, 1);
|
||||
SPI_Flash_Wait_Busy();
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*********************************************************************
|
||||
* @fn SPI_Flash_Read
|
||||
*
|
||||
* @brief Read data from flash.
|
||||
*
|
||||
* @param pBuffer -
|
||||
* ReadAddr -Initial address(24bit).
|
||||
* size - Data length.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
void SPI_Flash_Read(u8 *pBuffer, u32 ReadAddr, u16 size)
|
||||
{
|
||||
u16 i;
|
||||
|
||||
GPIO_WriteBit(GPIOA, GPIO_Pin_4, 0);
|
||||
SPI1_ReadWriteByte(W25X_ReadData);
|
||||
SPI1_ReadWriteByte((u8)((ReadAddr) >> 16));
|
||||
SPI1_ReadWriteByte((u8)((ReadAddr) >> 8));
|
||||
SPI1_ReadWriteByte((u8)ReadAddr);
|
||||
|
||||
for(i = 0; i < size; i++){
|
||||
pBuffer[i] = SPI1_ReadWriteByte(0XFF);
|
||||
}
|
||||
|
||||
GPIO_WriteBit(GPIOA, GPIO_Pin_4, 1);
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn SPI_Flash_Write_Page
|
||||
*
|
||||
* @brief Write data by one page.
|
||||
*
|
||||
* @param pBuffer -
|
||||
* WriteAddr - Initial address(24bit).
|
||||
* size - Data length.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
void SPI_Flash_Write_Page(u8 *pBuffer, u32 WriteAddr, u16 size)
|
||||
{
|
||||
u16 i;
|
||||
|
||||
SPI_FLASH_Write_Enable();
|
||||
GPIO_WriteBit(GPIOA, GPIO_Pin_4, 0);
|
||||
SPI1_ReadWriteByte(W25X_PageProgram);
|
||||
SPI1_ReadWriteByte((u8)((WriteAddr) >> 16));
|
||||
SPI1_ReadWriteByte((u8)((WriteAddr) >> 8));
|
||||
SPI1_ReadWriteByte((u8)WriteAddr);
|
||||
|
||||
for(i = 0; i < size; i++){
|
||||
SPI1_ReadWriteByte(pBuffer[i]);
|
||||
}
|
||||
|
||||
GPIO_WriteBit(GPIOA, GPIO_Pin_4, 1);
|
||||
SPI_Flash_Wait_Busy();
|
||||
}
|
||||
|
||||
|
||||
/*********************************************************************
|
||||
* @fn SPI_Flash_Write_NoCheck
|
||||
*
|
||||
* @brief Write data to flash.(need Erase)
|
||||
* All data in address rang is 0xFF.
|
||||
*
|
||||
* @param pBuffer -
|
||||
* WriteAddr - Initial address(24bit).
|
||||
* size - Data length.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
void SPI_Flash_Write_NoCheck(u8 *pBuffer, u32 WriteAddr, u16 size)
|
||||
{
|
||||
u16 pageremain;
|
||||
|
||||
pageremain = 256 - WriteAddr % 256;
|
||||
|
||||
if(size <= pageremain)
|
||||
pageremain = size;
|
||||
|
||||
while(1)
|
||||
{
|
||||
SPI_Flash_Write_Page(pBuffer, WriteAddr, pageremain);
|
||||
|
||||
if(size == pageremain)
|
||||
{
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
pBuffer += pageremain;
|
||||
WriteAddr += pageremain;
|
||||
size -= pageremain;
|
||||
|
||||
if(size > 256)
|
||||
pageremain = 256;
|
||||
else
|
||||
pageremain = size;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*********************************************************************
|
||||
* @fn SPI_Flash_Write
|
||||
*
|
||||
* @brief Write data to flash.(no need Erase)
|
||||
*
|
||||
* @param pBuffer -
|
||||
* WriteAddr - Initial address(24bit).
|
||||
* size - Data length.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
void SPI_Flash_Write(u8 *pBuffer, u32 WriteAddr, u16 size)
|
||||
{
|
||||
u32 secpos;
|
||||
u16 secoff;
|
||||
u16 secremain;
|
||||
u16 i;
|
||||
|
||||
secpos = WriteAddr / 4096;
|
||||
secoff = WriteAddr % 4096;
|
||||
secremain = 4096 - secoff;
|
||||
|
||||
if(size <= secremain)
|
||||
secremain = size;
|
||||
|
||||
while(1)
|
||||
{
|
||||
SPI_Flash_Read(SPI_FLASH_BUF, secpos * 4096, 4096);
|
||||
|
||||
for(i = 0; i < secremain; i++){
|
||||
if(SPI_FLASH_BUF[secoff + i] != 0XFF)
|
||||
break;
|
||||
}
|
||||
|
||||
if(i < secremain)
|
||||
{
|
||||
SPI_Flash_Erase_Sector(secpos);
|
||||
|
||||
for(i = 0; i < secremain; i++)
|
||||
{
|
||||
SPI_FLASH_BUF[i + secoff] = pBuffer[i];
|
||||
}
|
||||
|
||||
SPI_Flash_Write_NoCheck(SPI_FLASH_BUF, secpos * 4096, 4096);
|
||||
}
|
||||
else
|
||||
{
|
||||
SPI_Flash_Write_NoCheck(pBuffer, WriteAddr, secremain);
|
||||
}
|
||||
|
||||
if(size == secremain)
|
||||
{
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
secpos++;
|
||||
secoff = 0;
|
||||
|
||||
pBuffer += secremain;
|
||||
WriteAddr += secremain;
|
||||
size -= secremain;
|
||||
|
||||
if(size > 4096)
|
||||
{
|
||||
secremain = 4096;
|
||||
}
|
||||
else
|
||||
{
|
||||
secremain = size;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn SPI_Flash_Erase_Chip
|
||||
*
|
||||
* @brief Erase all FLASH pages.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
void SPI_Flash_Erase_Chip(void)
|
||||
{
|
||||
SPI_FLASH_Write_Enable();
|
||||
SPI_Flash_Wait_Busy();
|
||||
GPIO_WriteBit(GPIOA, GPIO_Pin_4, 0);
|
||||
SPI1_ReadWriteByte(W25X_ChipErase);
|
||||
GPIO_WriteBit(GPIOA, GPIO_Pin_4, 1);
|
||||
SPI_Flash_Wait_Busy();
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn SPI_Flash_PowerDown
|
||||
*
|
||||
* @brief Enter power down mode.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
void SPI_Flash_PowerDown(void)
|
||||
{
|
||||
GPIO_WriteBit(GPIOA, GPIO_Pin_4, 0);
|
||||
SPI1_ReadWriteByte(W25X_PowerDown);
|
||||
GPIO_WriteBit(GPIOA, GPIO_Pin_4, 1);
|
||||
Delay_Us(3);
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn SPI_Flash_WAKEUP
|
||||
*
|
||||
* @brief Power down wake up.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
void SPI_Flash_WAKEUP(void)
|
||||
{
|
||||
GPIO_WriteBit(GPIOA, GPIO_Pin_4, 0);
|
||||
SPI1_ReadWriteByte(W25X_ReleasePowerDown);
|
||||
GPIO_WriteBit(GPIOA, GPIO_Pin_4, 1);
|
||||
Delay_Us(3);
|
||||
}
|
||||
|
||||
|
||||
void storage_init(void)
|
||||
{
|
||||
SPI_Flash_Init();
|
||||
|
||||
uint16_t Flash_Model = SPI_Flash_ReadID();
|
||||
switch(Flash_Model)
|
||||
{
|
||||
case W25Q80: PRINT("W25Q80 OK!\r\n"); break;
|
||||
case W25Q16: PRINT("W25Q16 OK!\r\n"); break;
|
||||
case W25Q32: PRINT("W25Q32 OK!\r\n"); break;
|
||||
case W25Q64: PRINT("W25Q64 OK!\r\n"); break;
|
||||
case W25Q128: PRINT("W25Q128 OK!\r\n"); break;
|
||||
default: PRINT("Fail!\r\n"); break;
|
||||
}
|
||||
|
||||
// TODO: if read Flash_Model failed, must stop running.
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
990
vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/system_ch32v20x.c
Normal file
990
vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/system_ch32v20x.c
Normal file
@@ -0,0 +1,990 @@
|
||||
/********************************** (C) COPYRIGHT *******************************
|
||||
* File Name : system_ch32v20x.c
|
||||
* Author : WCH
|
||||
* Version : V1.0.0
|
||||
* Date : 2021/06/06
|
||||
* Description : CH32V20x Device Peripheral Access Layer System Source File.
|
||||
* For HSE = 32Mhz (CH32V208x/CH32V203RBT6)
|
||||
* For HSE = 8Mhz (other CH32V203x)
|
||||
*********************************************************************************
|
||||
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
|
||||
* Attention: This software (modified or not) and binary are used for
|
||||
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
|
||||
*******************************************************************************/
|
||||
#include "ch32v20x.h"
|
||||
|
||||
/*
|
||||
* Uncomment the line corresponding to the desired System clock (SYSCLK) frequency (after
|
||||
* reset the HSI is used as SYSCLK source).
|
||||
* If none of the define below is enabled, the HSI is used as System clock source.
|
||||
*/
|
||||
//#define SYSCLK_FREQ_HSE HSE_VALUE
|
||||
//#define SYSCLK_FREQ_48MHz_HSE 48000000
|
||||
//#define SYSCLK_FREQ_56MHz_HSE 56000000
|
||||
//#define SYSCLK_FREQ_72MHz_HSE 72000000
|
||||
// #define SYSCLK_FREQ_96MHz_HSE 96000000
|
||||
#define SYSCLK_FREQ_120MHz_HSE 120000000
|
||||
// #define SYSCLK_FREQ_144MHz_HSE 144000000
|
||||
//#define SYSCLK_FREQ_HSI HSI_VALUE
|
||||
//#define SYSCLK_FREQ_48MHz_HSI 48000000
|
||||
//#define SYSCLK_FREQ_56MHz_HSI 56000000
|
||||
//#define SYSCLK_FREQ_72MHz_HSI 72000000
|
||||
//#define SYSCLK_FREQ_96MHz_HSI 96000000
|
||||
//#define SYSCLK_FREQ_120MHz_HSI 120000000
|
||||
//#define SYSCLK_FREQ_144MHz_HSI 144000000
|
||||
|
||||
/* Clock Definitions */
|
||||
#ifdef SYSCLK_FREQ_HSE
|
||||
uint32_t SystemCoreClock = SYSCLK_FREQ_HSE; /* System Clock Frequency (Core Clock) */
|
||||
#elif defined SYSCLK_FREQ_48MHz_HSE
|
||||
uint32_t SystemCoreClock = SYSCLK_FREQ_48MHz_HSE; /* System Clock Frequency (Core Clock) */
|
||||
#elif defined SYSCLK_FREQ_56MHz_HSE
|
||||
uint32_t SystemCoreClock = SYSCLK_FREQ_56MHz_HSE; /* System Clock Frequency (Core Clock) */
|
||||
#elif defined SYSCLK_FREQ_72MHz_HSE
|
||||
uint32_t SystemCoreClock = SYSCLK_FREQ_72MHz_HSE; /* System Clock Frequency (Core Clock) */
|
||||
#elif defined SYSCLK_FREQ_96MHz_HSE
|
||||
uint32_t SystemCoreClock = SYSCLK_FREQ_96MHz_HSE; /* System Clock Frequency (Core Clock) */
|
||||
#elif defined SYSCLK_FREQ_120MHz_HSE
|
||||
uint32_t SystemCoreClock = SYSCLK_FREQ_120MHz_HSE; /* System Clock Frequency (Core Clock) */
|
||||
#elif defined SYSCLK_FREQ_144MHz_HSE
|
||||
uint32_t SystemCoreClock = SYSCLK_FREQ_144MHz_HSE; /* System Clock Frequency (Core Clock) */
|
||||
#elif defined SYSCLK_FREQ_48MHz_HSI
|
||||
uint32_t SystemCoreClock = SYSCLK_FREQ_48MHz_HSI; /* System Clock Frequency (Core Clock) */
|
||||
#elif defined SYSCLK_FREQ_56MHz_HSI
|
||||
uint32_t SystemCoreClock = SYSCLK_FREQ_56MHz_HSI; /* System Clock Frequency (Core Clock) */
|
||||
#elif defined SYSCLK_FREQ_72MHz_HSI
|
||||
uint32_t SystemCoreClock = SYSCLK_FREQ_72MHz_HSI; /* System Clock Frequency (Core Clock) */
|
||||
#elif defined SYSCLK_FREQ_96MHz_HSI
|
||||
uint32_t SystemCoreClock = SYSCLK_FREQ_96MHz_HSI; /* System Clock Frequency (Core Clock) */
|
||||
#elif defined SYSCLK_FREQ_120MHz_HSI
|
||||
uint32_t SystemCoreClock = SYSCLK_FREQ_120MHz_HSI; /* System Clock Frequency (Core Clock) */
|
||||
#elif defined SYSCLK_FREQ_144MHz_HSI
|
||||
uint32_t SystemCoreClock = SYSCLK_FREQ_144MHz_HSI; /* System Clock Frequency (Core Clock) */
|
||||
#else
|
||||
uint32_t SystemCoreClock = HSI_VALUE; /* System Clock Frequency (Core Clock) */
|
||||
|
||||
#endif
|
||||
|
||||
__I uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
|
||||
|
||||
|
||||
/* system_private_function_proto_types */
|
||||
static void SetSysClock(void);
|
||||
|
||||
#ifdef SYSCLK_FREQ_HSE
|
||||
static void SetSysClockToHSE( void );
|
||||
#elif defined SYSCLK_FREQ_48MHz_HSE
|
||||
static void SetSysClockTo48_HSE( void );
|
||||
#elif defined SYSCLK_FREQ_56MHz_HSE
|
||||
static void SetSysClockTo56_HSE( void );
|
||||
#elif defined SYSCLK_FREQ_72MHz_HSE
|
||||
static void SetSysClockTo72_HSE( void );
|
||||
#elif defined SYSCLK_FREQ_96MHz_HSE
|
||||
static void SetSysClockTo96_HSE( void );
|
||||
#elif defined SYSCLK_FREQ_120MHz_HSE
|
||||
static void SetSysClockTo120_HSE( void );
|
||||
#elif defined SYSCLK_FREQ_144MHz_HSE
|
||||
static void SetSysClockTo144_HSE( void );
|
||||
#elif defined SYSCLK_FREQ_48MHz_HSI
|
||||
static void SetSysClockTo48_HSI( void );
|
||||
#elif defined SYSCLK_FREQ_56MHz_HSI
|
||||
static void SetSysClockTo56_HSI( void );
|
||||
#elif defined SYSCLK_FREQ_72MHz_HSI
|
||||
static void SetSysClockTo72_HSI( void );
|
||||
#elif defined SYSCLK_FREQ_96MHz_HSI
|
||||
static void SetSysClockTo96_HSI( void );
|
||||
#elif defined SYSCLK_FREQ_120MHz_HSI
|
||||
static void SetSysClockTo120_HSI( void );
|
||||
#elif defined SYSCLK_FREQ_144MHz_HSI
|
||||
static void SetSysClockTo144_HSI( void );
|
||||
|
||||
#endif
|
||||
|
||||
/*********************************************************************
|
||||
* @fn SystemInit
|
||||
*
|
||||
* @brief Setup the microcontroller system Initialize the Embedded Flash Interface,
|
||||
* the PLL and update the SystemCoreClock variable.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
void SystemInit (void)
|
||||
{
|
||||
RCC->CTLR |= (uint32_t)0x00000001;
|
||||
RCC->CFGR0 &= (uint32_t)0xF0FF0000;
|
||||
RCC->CTLR &= (uint32_t)0xFEF6FFFF;
|
||||
RCC->CTLR &= (uint32_t)0xFFFBFFFF;
|
||||
RCC->CFGR0 &= (uint32_t)0xFF00FFFF;
|
||||
RCC->INTR = 0x009F0000;
|
||||
SetSysClock();
|
||||
}
|
||||
|
||||
|
||||
/*********************************************************************
|
||||
* @fn SystemCoreClockUpdate
|
||||
*
|
||||
* @brief Update SystemCoreClock variable according to Clock Register Values.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
void SystemCoreClockUpdate (void)
|
||||
{
|
||||
uint32_t tmp = 0, pllmull = 0, pllsource = 0, Pll_6_5 = 0;
|
||||
|
||||
tmp = RCC->CFGR0 & RCC_SWS;
|
||||
|
||||
switch (tmp)
|
||||
{
|
||||
case 0x00:
|
||||
SystemCoreClock = HSI_VALUE;
|
||||
break;
|
||||
case 0x04:
|
||||
SystemCoreClock = HSE_VALUE;
|
||||
break;
|
||||
case 0x08:
|
||||
pllmull = RCC->CFGR0 & RCC_PLLMULL;
|
||||
pllsource = RCC->CFGR0 & RCC_PLLSRC;
|
||||
pllmull = ( pllmull >> 18) + 2;
|
||||
|
||||
if(pllmull == 17) pllmull = 18;
|
||||
|
||||
if (pllsource == 0x00)
|
||||
{
|
||||
if(EXTEN->EXTEN_CTR & EXTEN_PLL_HSI_PRE){
|
||||
SystemCoreClock = HSI_VALUE * pllmull;
|
||||
}
|
||||
else{
|
||||
SystemCoreClock = (HSI_VALUE >> 1) * pllmull;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
#if defined (CH32V20x_D8W) || defined (CH32V20x_D8)
|
||||
if(((RCC->CFGR0 & (3<<22)) == (3<<22)) && (RCC_USB5PRE_JUDGE()== SET))
|
||||
{
|
||||
SystemCoreClock = ((HSE_VALUE>>1)) * pllmull;
|
||||
}
|
||||
else
|
||||
#endif
|
||||
if ((RCC->CFGR0 & RCC_PLLXTPRE) != (uint32_t)RESET)
|
||||
{
|
||||
#if defined (CH32V20x_D8) || defined (CH32V20x_D8W)
|
||||
SystemCoreClock = ((HSE_VALUE>>2) >> 1) * pllmull;
|
||||
#else
|
||||
SystemCoreClock = (HSE_VALUE >> 1) * pllmull;
|
||||
#endif
|
||||
}
|
||||
else
|
||||
{
|
||||
#if defined (CH32V20x_D8) || defined (CH32V20x_D8W)
|
||||
SystemCoreClock = (HSE_VALUE>>2) * pllmull;
|
||||
#else
|
||||
SystemCoreClock = HSE_VALUE * pllmull;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
if(Pll_6_5 == 1) SystemCoreClock = (SystemCoreClock / 2);
|
||||
|
||||
break;
|
||||
default:
|
||||
SystemCoreClock = HSI_VALUE;
|
||||
break;
|
||||
}
|
||||
|
||||
tmp = AHBPrescTable[((RCC->CFGR0 & RCC_HPRE) >> 4)];
|
||||
SystemCoreClock >>= tmp;
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn SetSysClock
|
||||
*
|
||||
* @brief Configures the System clock frequency, HCLK, PCLK2 and PCLK1 prescalers.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
static void SetSysClock(void)
|
||||
{
|
||||
//GPIO_IPD_Unused();
|
||||
#ifdef SYSCLK_FREQ_HSE
|
||||
SetSysClockToHSE();
|
||||
#elif defined SYSCLK_FREQ_48MHz_HSE
|
||||
SetSysClockTo48_HSE();
|
||||
#elif defined SYSCLK_FREQ_56MHz_HSE
|
||||
SetSysClockTo56_HSE();
|
||||
#elif defined SYSCLK_FREQ_72MHz_HSE
|
||||
SetSysClockTo72_HSE();
|
||||
#elif defined SYSCLK_FREQ_96MHz_HSE
|
||||
SetSysClockTo96_HSE();
|
||||
#elif defined SYSCLK_FREQ_120MHz_HSE
|
||||
SetSysClockTo120_HSE();
|
||||
#elif defined SYSCLK_FREQ_144MHz_HSE
|
||||
SetSysClockTo144_HSE();
|
||||
#elif defined SYSCLK_FREQ_48MHz_HSI
|
||||
SetSysClockTo48_HSI();
|
||||
#elif defined SYSCLK_FREQ_56MHz_HSI
|
||||
SetSysClockTo56_HSI();
|
||||
#elif defined SYSCLK_FREQ_72MHz_HSI
|
||||
SetSysClockTo72_HSI();
|
||||
#elif defined SYSCLK_FREQ_96MHz_HSI
|
||||
SetSysClockTo96_HSI();
|
||||
#elif defined SYSCLK_FREQ_120MHz_HSI
|
||||
SetSysClockTo120_HSI();
|
||||
#elif defined SYSCLK_FREQ_144MHz_HSI
|
||||
SetSysClockTo144_HSI();
|
||||
|
||||
#endif
|
||||
|
||||
/* If none of the define above is enabled, the HSI is used as System clock
|
||||
* source (default after reset)
|
||||
*/
|
||||
}
|
||||
|
||||
|
||||
#ifdef SYSCLK_FREQ_HSE
|
||||
|
||||
/*********************************************************************
|
||||
* @fn SetSysClockToHSE
|
||||
*
|
||||
* @brief Sets HSE as System clock source and configure HCLK, PCLK2 and PCLK1 prescalers.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
static void SetSysClockToHSE(void)
|
||||
{
|
||||
__IO uint32_t StartUpCounter = 0, HSEStatus = 0;
|
||||
|
||||
|
||||
RCC->CTLR |= ((uint32_t)RCC_HSEON);
|
||||
|
||||
/* Wait till HSE is ready and if Time out is reached exit */
|
||||
do
|
||||
{
|
||||
HSEStatus = RCC->CTLR & RCC_HSERDY;
|
||||
StartUpCounter++;
|
||||
} while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
|
||||
|
||||
if ((RCC->CTLR & RCC_HSERDY) != RESET)
|
||||
{
|
||||
HSEStatus = (uint32_t)0x01;
|
||||
}
|
||||
else
|
||||
{
|
||||
HSEStatus = (uint32_t)0x00;
|
||||
}
|
||||
|
||||
if (HSEStatus == (uint32_t)0x01)
|
||||
{
|
||||
/* HCLK = SYSCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV1;
|
||||
/* PCLK2 = HCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_PPRE2_DIV1;
|
||||
/* PCLK1 = HCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_PPRE1_DIV1;
|
||||
|
||||
/* Select HSE as system clock source
|
||||
* CH32V20x_D6 (HSE=8MHZ)
|
||||
* CH32V20x_D8 (HSE=32MHZ)
|
||||
* CH32V20x_D8W (HSE=32MHZ)
|
||||
*/
|
||||
RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_SW));
|
||||
RCC->CFGR0 |= (uint32_t)RCC_SW_HSE;
|
||||
|
||||
/* Wait till HSE is used as system clock source */
|
||||
while ((RCC->CFGR0 & (uint32_t)RCC_SWS) != (uint32_t)0x04)
|
||||
{
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/* If HSE fails to start-up, the application will have wrong clock
|
||||
* configuration. User can add here some code to deal with this error
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
||||
#elif defined SYSCLK_FREQ_48MHz_HSE
|
||||
|
||||
/*********************************************************************
|
||||
* @fn SetSysClockTo48_HSE
|
||||
*
|
||||
* @brief Sets System clock frequency to 48MHz and configure HCLK, PCLK2 and PCLK1 prescalers.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
static void SetSysClockTo48_HSE(void)
|
||||
{
|
||||
__IO uint32_t StartUpCounter = 0, HSEStatus = 0;
|
||||
|
||||
|
||||
RCC->CTLR |= ((uint32_t)RCC_HSEON);
|
||||
/* Wait till HSE is ready and if Time out is reached exit */
|
||||
do
|
||||
{
|
||||
HSEStatus = RCC->CTLR & RCC_HSERDY;
|
||||
StartUpCounter++;
|
||||
} while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
|
||||
|
||||
if ((RCC->CTLR & RCC_HSERDY) != RESET)
|
||||
{
|
||||
HSEStatus = (uint32_t)0x01;
|
||||
}
|
||||
else
|
||||
{
|
||||
HSEStatus = (uint32_t)0x00;
|
||||
}
|
||||
|
||||
if (HSEStatus == (uint32_t)0x01)
|
||||
{
|
||||
/* HCLK = SYSCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV1;
|
||||
/* PCLK2 = HCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_PPRE2_DIV1;
|
||||
/* PCLK1 = HCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_PPRE1_DIV2;
|
||||
|
||||
/* CH32V20x_D6-PLL configuration: PLLCLK = HSE * 6 = 48 MHz (HSE=8MHZ)
|
||||
* CH32V20x_D8-PLL configuration: PLLCLK = HSE/4 * 6 = 48 MHz (HSE=32MHZ)
|
||||
* CH32V20x_D8W-PLL configuration: PLLCLK = HSE/4 * 6 = 48 MHz (HSE=32MHZ)
|
||||
*/
|
||||
RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_PLLSRC | RCC_PLLXTPRE | RCC_PLLMULL));
|
||||
|
||||
RCC->CFGR0 |= (uint32_t)(RCC_PLLSRC_HSE | RCC_PLLXTPRE_HSE | RCC_PLLMULL6);
|
||||
|
||||
/* Enable PLL */
|
||||
RCC->CTLR |= RCC_PLLON;
|
||||
/* Wait till PLL is ready */
|
||||
while((RCC->CTLR & RCC_PLLRDY) == 0)
|
||||
{
|
||||
}
|
||||
/* Select PLL as system clock source */
|
||||
RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_SW));
|
||||
RCC->CFGR0 |= (uint32_t)RCC_SW_PLL;
|
||||
/* Wait till PLL is used as system clock source */
|
||||
while ((RCC->CFGR0 & (uint32_t)RCC_SWS) != (uint32_t)0x08)
|
||||
{
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/*
|
||||
* If HSE fails to start-up, the application will have wrong clock
|
||||
* configuration. User can add here some code to deal with this error
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
||||
#elif defined SYSCLK_FREQ_56MHz_HSE
|
||||
|
||||
/*********************************************************************
|
||||
* @fn SetSysClockTo56_HSE
|
||||
*
|
||||
* @brief Sets System clock frequency to 56MHz and configure HCLK, PCLK2 and PCLK1 prescalers.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
static void SetSysClockTo56_HSE(void)
|
||||
{
|
||||
__IO uint32_t StartUpCounter = 0, HSEStatus = 0;
|
||||
|
||||
RCC->CTLR |= ((uint32_t)RCC_HSEON);
|
||||
|
||||
/* Wait till HSE is ready and if Time out is reached exit */
|
||||
do
|
||||
{
|
||||
HSEStatus = RCC->CTLR & RCC_HSERDY;
|
||||
StartUpCounter++;
|
||||
} while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
|
||||
|
||||
if ((RCC->CTLR & RCC_HSERDY) != RESET)
|
||||
{
|
||||
HSEStatus = (uint32_t)0x01;
|
||||
}
|
||||
else
|
||||
{
|
||||
HSEStatus = (uint32_t)0x00;
|
||||
}
|
||||
|
||||
if (HSEStatus == (uint32_t)0x01)
|
||||
{
|
||||
/* HCLK = SYSCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV1;
|
||||
/* PCLK2 = HCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_PPRE2_DIV1;
|
||||
/* PCLK1 = HCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_PPRE1_DIV2;
|
||||
|
||||
/* CH32V20x_D6-PLL configuration: PLLCLK = HSE * 7 = 56 MHz (HSE=8MHZ)
|
||||
* CH32V20x_D8-PLL configuration: PLLCLK = HSE/4 * 7 = 56 MHz (HSE=32MHZ)
|
||||
* CH32V20x_D8W-PLL configuration: PLLCLK = HSE/4 * 7 = 56 MHz (HSE=32MHZ)
|
||||
*/
|
||||
RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_PLLSRC | RCC_PLLXTPRE | RCC_PLLMULL));
|
||||
|
||||
RCC->CFGR0 |= (uint32_t)(RCC_PLLSRC_HSE | RCC_PLLXTPRE_HSE | RCC_PLLMULL7);
|
||||
|
||||
/* Enable PLL */
|
||||
RCC->CTLR |= RCC_PLLON;
|
||||
/* Wait till PLL is ready */
|
||||
while((RCC->CTLR & RCC_PLLRDY) == 0)
|
||||
{
|
||||
}
|
||||
|
||||
/* Select PLL as system clock source */
|
||||
RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_SW));
|
||||
RCC->CFGR0 |= (uint32_t)RCC_SW_PLL;
|
||||
/* Wait till PLL is used as system clock source */
|
||||
while ((RCC->CFGR0 & (uint32_t)RCC_SWS) != (uint32_t)0x08)
|
||||
{
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/*
|
||||
* If HSE fails to start-up, the application will have wrong clock
|
||||
* configuration. User can add here some code to deal with this error
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
||||
#elif defined SYSCLK_FREQ_72MHz_HSE
|
||||
|
||||
/*********************************************************************
|
||||
* @fn SetSysClockTo72_HSE
|
||||
*
|
||||
* @brief Sets System clock frequency to 72MHz and configure HCLK, PCLK2 and PCLK1 prescalers.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
static void SetSysClockTo72_HSE(void)
|
||||
{
|
||||
__IO uint32_t StartUpCounter = 0, HSEStatus = 0;
|
||||
|
||||
RCC->CTLR |= ((uint32_t)RCC_HSEON);
|
||||
|
||||
/* Wait till HSE is ready and if Time out is reached exit */
|
||||
do
|
||||
{
|
||||
HSEStatus = RCC->CTLR & RCC_HSERDY;
|
||||
StartUpCounter++;
|
||||
} while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
|
||||
|
||||
if ((RCC->CTLR & RCC_HSERDY) != RESET)
|
||||
{
|
||||
HSEStatus = (uint32_t)0x01;
|
||||
}
|
||||
else
|
||||
{
|
||||
HSEStatus = (uint32_t)0x00;
|
||||
}
|
||||
|
||||
if (HSEStatus == (uint32_t)0x01)
|
||||
{
|
||||
/* HCLK = SYSCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV1;
|
||||
/* PCLK2 = HCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_PPRE2_DIV1;
|
||||
/* PCLK1 = HCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_PPRE1_DIV2;
|
||||
|
||||
|
||||
/* CH32V20x_D6-PLL configuration: PLLCLK = HSE * 9 = 72 MHz (HSE=8MHZ)
|
||||
* CH32V20x_D8-PLL configuration: PLLCLK = HSE/4 * 9 = 72 MHz (HSE=32MHZ)
|
||||
* CH32V20x_D8W-PLL configuration: PLLCLK = HSE/4 * 9 = 72 MHz (HSE=32MHZ)
|
||||
*/
|
||||
RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_PLLSRC | RCC_PLLXTPRE |
|
||||
RCC_PLLMULL));
|
||||
|
||||
RCC->CFGR0 |= (uint32_t)(RCC_PLLSRC_HSE | RCC_PLLXTPRE_HSE | RCC_PLLMULL9);
|
||||
|
||||
/* Enable PLL */
|
||||
RCC->CTLR |= RCC_PLLON;
|
||||
/* Wait till PLL is ready */
|
||||
while((RCC->CTLR & RCC_PLLRDY) == 0)
|
||||
{
|
||||
}
|
||||
/* Select PLL as system clock source */
|
||||
RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_SW));
|
||||
RCC->CFGR0 |= (uint32_t)RCC_SW_PLL;
|
||||
/* Wait till PLL is used as system clock source */
|
||||
while ((RCC->CFGR0 & (uint32_t)RCC_SWS) != (uint32_t)0x08)
|
||||
{
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/*
|
||||
* If HSE fails to start-up, the application will have wrong clock
|
||||
* configuration. User can add here some code to deal with this error
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#elif defined SYSCLK_FREQ_96MHz_HSE
|
||||
|
||||
/*********************************************************************
|
||||
* @fn SetSysClockTo96_HSE
|
||||
*
|
||||
* @brief Sets System clock frequency to 96MHz and configure HCLK, PCLK2 and PCLK1 prescalers.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
static void SetSysClockTo96_HSE(void)
|
||||
{
|
||||
__IO uint32_t StartUpCounter = 0, HSEStatus = 0;
|
||||
|
||||
RCC->CTLR |= ((uint32_t)RCC_HSEON);
|
||||
|
||||
/* Wait till HSE is ready and if Time out is reached exit */
|
||||
do
|
||||
{
|
||||
HSEStatus = RCC->CTLR & RCC_HSERDY;
|
||||
StartUpCounter++;
|
||||
} while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
|
||||
|
||||
if ((RCC->CTLR & RCC_HSERDY) != RESET)
|
||||
{
|
||||
HSEStatus = (uint32_t)0x01;
|
||||
}
|
||||
else
|
||||
{
|
||||
HSEStatus = (uint32_t)0x00;
|
||||
}
|
||||
|
||||
if (HSEStatus == (uint32_t)0x01)
|
||||
{
|
||||
/* HCLK = SYSCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV1;
|
||||
/* PCLK2 = HCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_PPRE2_DIV1;
|
||||
/* PCLK1 = HCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_PPRE1_DIV2;
|
||||
|
||||
|
||||
/* CH32V20x_D6-PLL configuration: PLLCLK = HSE * 12 = 96 MHz (HSE=8MHZ)
|
||||
* CH32V20x_D8-PLL configuration: PLLCLK = HSE/4 * 12 = 96 MHz (HSE=32MHZ)
|
||||
* CH32V20x_D8W-PLL configuration: PLLCLK = HSE/4 * 12 = 96 MHz (HSE=32MHZ)
|
||||
*/
|
||||
RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_PLLSRC | RCC_PLLXTPRE |
|
||||
RCC_PLLMULL));
|
||||
|
||||
RCC->CFGR0 |= (uint32_t)(RCC_PLLSRC_HSE | RCC_PLLXTPRE_HSE | RCC_PLLMULL12);
|
||||
|
||||
/* Enable PLL */
|
||||
RCC->CTLR |= RCC_PLLON;
|
||||
/* Wait till PLL is ready */
|
||||
while((RCC->CTLR & RCC_PLLRDY) == 0)
|
||||
{
|
||||
}
|
||||
/* Select PLL as system clock source */
|
||||
RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_SW));
|
||||
RCC->CFGR0 |= (uint32_t)RCC_SW_PLL;
|
||||
/* Wait till PLL is used as system clock source */
|
||||
while ((RCC->CFGR0 & (uint32_t)RCC_SWS) != (uint32_t)0x08)
|
||||
{
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/*
|
||||
* If HSE fails to start-up, the application will have wrong clock
|
||||
* configuration. User can add here some code to deal with this error
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#elif defined SYSCLK_FREQ_120MHz_HSE
|
||||
|
||||
/*********************************************************************
|
||||
* @fn SetSysClockTo120_HSE
|
||||
*
|
||||
* @brief Sets System clock frequency to 120MHz and configure HCLK, PCLK2 and PCLK1 prescalers.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
static void SetSysClockTo120_HSE(void)
|
||||
{
|
||||
__IO uint32_t StartUpCounter = 0, HSEStatus = 0;
|
||||
|
||||
RCC->CTLR |= ((uint32_t)RCC_HSEON);
|
||||
|
||||
/* Wait till HSE is ready and if Time out is reached exit */
|
||||
do
|
||||
{
|
||||
HSEStatus = RCC->CTLR & RCC_HSERDY;
|
||||
StartUpCounter++;
|
||||
} while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
|
||||
|
||||
if((RCC->CTLR & RCC_HSERDY) != RESET)
|
||||
{
|
||||
HSEStatus = (uint32_t)0x01;
|
||||
}
|
||||
else
|
||||
{
|
||||
HSEStatus = (uint32_t)0x00;
|
||||
}
|
||||
|
||||
if(HSEStatus == (uint32_t)0x01)
|
||||
{
|
||||
#if defined (CH32V20x_D8W)
|
||||
RCC->CFGR0 |= (uint32_t)(3<<22);
|
||||
/* HCLK = SYSCLK/2 */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV2;
|
||||
#else
|
||||
/* HCLK = SYSCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV1;
|
||||
#endif
|
||||
/* PCLK2 = HCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_PPRE2_DIV1;
|
||||
/* PCLK1 = HCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_PPRE1_DIV2;
|
||||
|
||||
/* CH32V20x_D6-PLL configuration: PLLCLK = HSE * 15 = 120 MHz (HSE=8MHZ)
|
||||
* CH32V20x_D8-PLL configuration: PLLCLK = HSE/4 * 15 = 120 MHz (HSE=32MHZ)
|
||||
* CH32V20x_D8W-PLL configuration: PLLCLK = HSE/2 * 15 = 240 MHz (HSE=32MHZ)
|
||||
*/
|
||||
RCC->CFGR0 &= (uint32_t)((uint32_t) ~(RCC_PLLSRC | RCC_PLLXTPRE |
|
||||
RCC_PLLMULL));
|
||||
|
||||
RCC->CFGR0 |= (uint32_t)(RCC_PLLSRC_HSE | RCC_PLLXTPRE_HSE | RCC_PLLMULL15);
|
||||
|
||||
/* Enable PLL */
|
||||
RCC->CTLR |= RCC_PLLON;
|
||||
/* Wait till PLL is ready */
|
||||
while((RCC->CTLR & RCC_PLLRDY) == 0)
|
||||
{
|
||||
}
|
||||
/* Select PLL as system clock source */
|
||||
RCC->CFGR0 &= (uint32_t)((uint32_t) ~(RCC_SW));
|
||||
RCC->CFGR0 |= (uint32_t)RCC_SW_PLL;
|
||||
/* Wait till PLL is used as system clock source */
|
||||
while((RCC->CFGR0 & (uint32_t)RCC_SWS) != (uint32_t)0x08)
|
||||
{
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/*
|
||||
* If HSE fails to start-up, the application will have wrong clock
|
||||
* configuration. User can add here some code to deal with this error
|
||||
*/
|
||||
}
|
||||
}
|
||||
#elif defined SYSCLK_FREQ_144MHz_HSE
|
||||
|
||||
/*********************************************************************
|
||||
* @fn SetSysClockTo144_HSE
|
||||
*
|
||||
* @brief Sets System clock frequency to 144MHz and configure HCLK, PCLK2 and PCLK1 prescalers.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
static void SetSysClockTo144_HSE(void)
|
||||
{
|
||||
__IO uint32_t StartUpCounter = 0, HSEStatus = 0;
|
||||
|
||||
RCC->CTLR |= ((uint32_t)RCC_HSEON);
|
||||
|
||||
/* Wait till HSE is ready and if Time out is reached exit */
|
||||
do
|
||||
{
|
||||
HSEStatus = RCC->CTLR & RCC_HSERDY;
|
||||
StartUpCounter++;
|
||||
} while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
|
||||
|
||||
if ((RCC->CTLR & RCC_HSERDY) != RESET)
|
||||
{
|
||||
HSEStatus = (uint32_t)0x01;
|
||||
}
|
||||
else
|
||||
{
|
||||
HSEStatus = (uint32_t)0x00;
|
||||
}
|
||||
|
||||
if (HSEStatus == (uint32_t)0x01)
|
||||
{
|
||||
/* HCLK = SYSCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV1;
|
||||
/* PCLK2 = HCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_PPRE2_DIV1;
|
||||
/* PCLK1 = HCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_PPRE1_DIV2;
|
||||
|
||||
|
||||
/* CH32V20x_D6-PLL configuration: PLLCLK = HSE * 18 = 144 MHz (HSE=8MHZ)
|
||||
* CH32V20x_D8-PLL configuration: PLLCLK = HSE/4 * 18 = 144 MHz (HSE=32MHZ)
|
||||
* CH32V20x_D8W-PLL configuration: PLLCLK = HSE/4 * 18 = 144 MHz (HSE=32MHZ)
|
||||
*/
|
||||
RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_PLLSRC | RCC_PLLXTPRE |
|
||||
RCC_PLLMULL));
|
||||
|
||||
RCC->CFGR0 |= (uint32_t)(RCC_PLLSRC_HSE | RCC_PLLXTPRE_HSE | RCC_PLLMULL18);
|
||||
|
||||
/* Enable PLL */
|
||||
RCC->CTLR |= RCC_PLLON;
|
||||
/* Wait till PLL is ready */
|
||||
while((RCC->CTLR & RCC_PLLRDY) == 0)
|
||||
{
|
||||
}
|
||||
/* Select PLL as system clock source */
|
||||
RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_SW));
|
||||
RCC->CFGR0 |= (uint32_t)RCC_SW_PLL;
|
||||
/* Wait till PLL is used as system clock source */
|
||||
while ((RCC->CFGR0 & (uint32_t)RCC_SWS) != (uint32_t)0x08)
|
||||
{
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/*
|
||||
* If HSE fails to start-up, the application will have wrong clock
|
||||
* configuration. User can add here some code to deal with this error
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
||||
#elif defined SYSCLK_FREQ_48MHz_HSI
|
||||
|
||||
/*********************************************************************
|
||||
* @fn SetSysClockTo48_HSI
|
||||
*
|
||||
* @brief Sets System clock frequency to 48MHz and configure HCLK, PCLK2 and PCLK1 prescalers.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
static void SetSysClockTo48_HSI(void)
|
||||
{
|
||||
EXTEN->EXTEN_CTR |= EXTEN_PLL_HSI_PRE;
|
||||
|
||||
/* HCLK = SYSCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV1;
|
||||
/* PCLK2 = HCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_PPRE2_DIV1;
|
||||
/* PCLK1 = HCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_PPRE1_DIV2;
|
||||
|
||||
/* PLL configuration: PLLCLK = HSI * 6 = 48 MHz */
|
||||
RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_PLLSRC | RCC_PLLXTPRE | RCC_PLLMULL));
|
||||
|
||||
RCC->CFGR0 |= (uint32_t)(RCC_PLLSRC_HSI_Div2 | RCC_PLLMULL6);
|
||||
|
||||
/* Enable PLL */
|
||||
RCC->CTLR |= RCC_PLLON;
|
||||
/* Wait till PLL is ready */
|
||||
while((RCC->CTLR & RCC_PLLRDY) == 0)
|
||||
{
|
||||
}
|
||||
/* Select PLL as system clock source */
|
||||
RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_SW));
|
||||
RCC->CFGR0 |= (uint32_t)RCC_SW_PLL;
|
||||
/* Wait till PLL is used as system clock source */
|
||||
while ((RCC->CFGR0 & (uint32_t)RCC_SWS) != (uint32_t)0x08)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
#elif defined SYSCLK_FREQ_56MHz_HSI
|
||||
|
||||
/*********************************************************************
|
||||
* @fn SetSysClockTo56_HSI
|
||||
*
|
||||
* @brief Sets System clock frequency to 56MHz and configure HCLK, PCLK2 and PCLK1 prescalers.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
static void SetSysClockTo56_HSI(void)
|
||||
{
|
||||
EXTEN->EXTEN_CTR |= EXTEN_PLL_HSI_PRE;
|
||||
|
||||
/* HCLK = SYSCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV1;
|
||||
/* PCLK2 = HCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_PPRE2_DIV1;
|
||||
/* PCLK1 = HCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_PPRE1_DIV2;
|
||||
|
||||
/* PLL configuration: PLLCLK = HSI * 7 = 48 MHz */
|
||||
RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_PLLSRC | RCC_PLLXTPRE | RCC_PLLMULL));
|
||||
|
||||
RCC->CFGR0 |= (uint32_t)(RCC_PLLSRC_HSI_Div2 | RCC_PLLMULL7);
|
||||
|
||||
/* Enable PLL */
|
||||
RCC->CTLR |= RCC_PLLON;
|
||||
/* Wait till PLL is ready */
|
||||
while((RCC->CTLR & RCC_PLLRDY) == 0)
|
||||
{
|
||||
}
|
||||
/* Select PLL as system clock source */
|
||||
RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_SW));
|
||||
RCC->CFGR0 |= (uint32_t)RCC_SW_PLL;
|
||||
/* Wait till PLL is used as system clock source */
|
||||
while ((RCC->CFGR0 & (uint32_t)RCC_SWS) != (uint32_t)0x08)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
#elif defined SYSCLK_FREQ_72MHz_HSI
|
||||
|
||||
/*********************************************************************
|
||||
* @fn SetSysClockTo72_HSI
|
||||
*
|
||||
* @brief Sets System clock frequency to 72MHz and configure HCLK, PCLK2 and PCLK1 prescalers.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
static void SetSysClockTo72_HSI(void)
|
||||
{
|
||||
EXTEN->EXTEN_CTR |= EXTEN_PLL_HSI_PRE;
|
||||
|
||||
/* HCLK = SYSCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV1;
|
||||
/* PCLK2 = HCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_PPRE2_DIV1;
|
||||
/* PCLK1 = HCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_PPRE1_DIV2;
|
||||
|
||||
/* PLL configuration: PLLCLK = HSI * 9 = 72 MHz */
|
||||
RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_PLLSRC | RCC_PLLXTPRE | RCC_PLLMULL));
|
||||
|
||||
RCC->CFGR0 |= (uint32_t)(RCC_PLLSRC_HSI_Div2 | RCC_PLLMULL9);
|
||||
|
||||
/* Enable PLL */
|
||||
RCC->CTLR |= RCC_PLLON;
|
||||
/* Wait till PLL is ready */
|
||||
while((RCC->CTLR & RCC_PLLRDY) == 0)
|
||||
{
|
||||
}
|
||||
/* Select PLL as system clock source */
|
||||
RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_SW));
|
||||
RCC->CFGR0 |= (uint32_t)RCC_SW_PLL;
|
||||
/* Wait till PLL is used as system clock source */
|
||||
while ((RCC->CFGR0 & (uint32_t)RCC_SWS) != (uint32_t)0x08)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#elif defined SYSCLK_FREQ_96MHz_HSI
|
||||
|
||||
/*********************************************************************
|
||||
* @fn SetSysClockTo96_HSI
|
||||
*
|
||||
* @brief Sets System clock frequency to 96MHz and configure HCLK, PCLK2 and PCLK1 prescalers.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
static void SetSysClockTo96_HSI(void)
|
||||
{
|
||||
EXTEN->EXTEN_CTR |= EXTEN_PLL_HSI_PRE;
|
||||
|
||||
/* HCLK = SYSCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV1;
|
||||
/* PCLK2 = HCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_PPRE2_DIV1;
|
||||
/* PCLK1 = HCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_PPRE1_DIV2;
|
||||
|
||||
/* PLL configuration: PLLCLK = HSI * 12 = 96 MHz */
|
||||
RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_PLLSRC | RCC_PLLXTPRE | RCC_PLLMULL));
|
||||
|
||||
RCC->CFGR0 |= (uint32_t)(RCC_PLLSRC_HSI_Div2 | RCC_PLLMULL12);
|
||||
|
||||
/* Enable PLL */
|
||||
RCC->CTLR |= RCC_PLLON;
|
||||
/* Wait till PLL is ready */
|
||||
while((RCC->CTLR & RCC_PLLRDY) == 0)
|
||||
{
|
||||
}
|
||||
/* Select PLL as system clock source */
|
||||
RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_SW));
|
||||
RCC->CFGR0 |= (uint32_t)RCC_SW_PLL;
|
||||
/* Wait till PLL is used as system clock source */
|
||||
while ((RCC->CFGR0 & (uint32_t)RCC_SWS) != (uint32_t)0x08)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#elif defined SYSCLK_FREQ_120MHz_HSI
|
||||
|
||||
/*********************************************************************
|
||||
* @fn SetSysClockTo120_HSI
|
||||
*
|
||||
* @brief Sets System clock frequency to 120MHz and configure HCLK, PCLK2 and PCLK1 prescalers.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
static void SetSysClockTo120_HSI(void)
|
||||
{
|
||||
EXTEN->EXTEN_CTR |= EXTEN_PLL_HSI_PRE;
|
||||
|
||||
/* HCLK = SYSCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV1;
|
||||
/* PCLK2 = HCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_PPRE2_DIV1;
|
||||
/* PCLK1 = HCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_PPRE1_DIV2;
|
||||
|
||||
/* PLL configuration: PLLCLK = HSI * 15 = 120 MHz */
|
||||
RCC->CFGR0 &= (uint32_t)((uint32_t) ~(RCC_PLLSRC | RCC_PLLXTPRE |
|
||||
RCC_PLLMULL));
|
||||
|
||||
RCC->CFGR0 |= (uint32_t)(RCC_PLLSRC_HSI_Div2 | RCC_PLLMULL15);
|
||||
|
||||
/* Enable PLL */
|
||||
RCC->CTLR |= RCC_PLLON;
|
||||
/* Wait till PLL is ready */
|
||||
while((RCC->CTLR & RCC_PLLRDY) == 0)
|
||||
{
|
||||
}
|
||||
/* Select PLL as system clock source */
|
||||
RCC->CFGR0 &= (uint32_t)((uint32_t) ~(RCC_SW));
|
||||
RCC->CFGR0 |= (uint32_t)RCC_SW_PLL;
|
||||
/* Wait till PLL is used as system clock source */
|
||||
while((RCC->CFGR0 & (uint32_t)RCC_SWS) != (uint32_t)0x08)
|
||||
{
|
||||
}
|
||||
}
|
||||
#elif defined SYSCLK_FREQ_144MHz_HSI
|
||||
|
||||
/*********************************************************************
|
||||
* @fn SetSysClockTo144_HSI
|
||||
*
|
||||
* @brief Sets System clock frequency to 144MHz and configure HCLK, PCLK2 and PCLK1 prescalers.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
static void SetSysClockTo144_HSI(void)
|
||||
{
|
||||
EXTEN->EXTEN_CTR |= EXTEN_PLL_HSI_PRE;
|
||||
|
||||
/* HCLK = SYSCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV1;
|
||||
/* PCLK2 = HCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_PPRE2_DIV1;
|
||||
/* PCLK1 = HCLK */
|
||||
RCC->CFGR0 |= (uint32_t)RCC_PPRE1_DIV2;
|
||||
|
||||
/* PLL configuration: PLLCLK = HSI * 18 = 144 MHz */
|
||||
RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_PLLSRC | RCC_PLLXTPRE | RCC_PLLMULL));
|
||||
|
||||
RCC->CFGR0 |= (uint32_t)(RCC_PLLSRC_HSI_Div2 | RCC_PLLMULL18);
|
||||
|
||||
/* Enable PLL */
|
||||
RCC->CTLR |= RCC_PLLON;
|
||||
/* Wait till PLL is ready */
|
||||
while((RCC->CTLR & RCC_PLLRDY) == 0)
|
||||
{
|
||||
}
|
||||
/* Select PLL as system clock source */
|
||||
RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_SW));
|
||||
RCC->CFGR0 |= (uint32_t)RCC_SW_PLL;
|
||||
/* Wait till PLL is used as system clock source */
|
||||
while ((RCC->CFGR0 & (uint32_t)RCC_SWS) != (uint32_t)0x08)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
186
vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/usart_biz.c
Normal file
186
vd960DBN/BLE/OnlyUpdateApp_Peripheral/APP/usart_biz.c
Normal file
@@ -0,0 +1,186 @@
|
||||
/*
|
||||
* usart_biz.c
|
||||
*
|
||||
* Created on: 2026-02-26
|
||||
* Author: wangfq
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
#include "cmcng.h"
|
||||
#include <string.h>
|
||||
#include "dbn_ble_srv.h"
|
||||
|
||||
void USART1_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
|
||||
void USART2_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
|
||||
|
||||
|
||||
void uart_init(void){
|
||||
GPIO_InitTypeDef GPIO_InitStructure = {0};
|
||||
USART_InitTypeDef USART_InitStructure = {0};
|
||||
NVIC_InitTypeDef NVIC_InitStructure = {0};
|
||||
|
||||
// usart1 : peripheral / DEBUG
|
||||
|
||||
|
||||
//usart2 :loop mcu
|
||||
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
|
||||
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
|
||||
|
||||
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
|
||||
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
||||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // Tx
|
||||
GPIO_Init(GPIOA, &GPIO_InitStructure);
|
||||
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
|
||||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; // Rx
|
||||
GPIO_Init(GPIOA, &GPIO_InitStructure);
|
||||
|
||||
USART_InitStructure.USART_BaudRate = 192000;//115200;
|
||||
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
|
||||
USART_InitStructure.USART_StopBits = USART_StopBits_1;
|
||||
USART_InitStructure.USART_Parity = USART_Parity_No;
|
||||
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
|
||||
USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
|
||||
|
||||
USART_Init(USART2, &USART_InitStructure);
|
||||
// USART_ITConfig(USART2, USART_IT_IDLE, ENABLE); // 配合DMA,但测试不能接收,原因未知。
|
||||
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
|
||||
|
||||
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
|
||||
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
|
||||
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
|
||||
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
|
||||
NVIC_Init(&NVIC_InitStructure);
|
||||
|
||||
USART_Cmd(USART2, ENABLE);
|
||||
|
||||
|
||||
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
|
||||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//GPIO_Mode_IPU; // Key
|
||||
GPIO_Init(GPIOA, &GPIO_InitStructure);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/*********************************************************************
|
||||
* @fn USART1_IRQHandler
|
||||
*
|
||||
* @brief This function handles USART1 global interrupt request.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
void USART1_IRQHandler(void)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*********************************************************************
|
||||
* @fn USART2_IRQHandler
|
||||
*
|
||||
* @brief This function handles USART2 global interrupt request.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
void USART2_IRQHandler(void)
|
||||
{
|
||||
if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
|
||||
{
|
||||
uint8_t _dat = USART_ReceiveData(USART2);
|
||||
if(g_pkg_uart_2.offset == 0){
|
||||
if(_dat != 0){
|
||||
g_pkg_uart_2.pkg[g_pkg_uart_2.offset++] = _dat;
|
||||
}
|
||||
}
|
||||
else{
|
||||
if(g_pkg_uart_2.offset < BUFF_STACK_SIZE){
|
||||
g_pkg_uart_2.pkg[g_pkg_uart_2.offset++] = _dat;
|
||||
}
|
||||
else{
|
||||
g_pkg_uart_2.flag = 1;
|
||||
}
|
||||
}
|
||||
|
||||
if(g_pkg_uart_2.offset){
|
||||
g_pkg_uart_2.tick = 0;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void UART2_SendString(uint8_t *buf, uint16_t len)
|
||||
{
|
||||
uint16_t _len = len;
|
||||
while(_len){
|
||||
while(USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET);
|
||||
USART_SendData(USART2, *buf++);
|
||||
_len--;
|
||||
}
|
||||
while(USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET);
|
||||
}
|
||||
|
||||
void UART1_SendString(uint8_t *buf, uint16_t len)
|
||||
{
|
||||
uint16_t _len = len;
|
||||
while(_len){
|
||||
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
|
||||
USART_SendData(USART1, *buf++);
|
||||
_len--;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void uart_srv(void)
|
||||
{
|
||||
uint8_t i;
|
||||
uint8_t _report_flag = 0;
|
||||
|
||||
if(g_pkg_uart_2.flag){
|
||||
|
||||
if(g_flag_counter_ota.flag == 0){
|
||||
if(g_pkg_uart_2.pkg[0] == 0x7F){
|
||||
if(g_pkg_uart_2.pkg[3] == 0xC0 && g_pkg_uart_2.pkg[4] == 0x0C)
|
||||
{
|
||||
if(g_dbn_ble_state_acs_enable.flag == 0){
|
||||
_report_flag = 1;
|
||||
}
|
||||
else{
|
||||
g_pkg_uart_2.pkg[0] = 0x8F;
|
||||
}
|
||||
}
|
||||
|
||||
for(i = 0; i < g_pkg_uart_2.offset; i++){
|
||||
PRINT(" %02X", g_pkg_uart_2.pkg[i]);
|
||||
}
|
||||
PRINT("\n");
|
||||
|
||||
if(_report_flag){
|
||||
InitPkgUart(&g_pkg_uart_2);
|
||||
}
|
||||
}
|
||||
else {
|
||||
PRINT("Rcv_len:%d,dat: %s\n", g_pkg_uart_2.offset, g_pkg_uart_2.pkg);
|
||||
}
|
||||
}
|
||||
else{
|
||||
// PRINT("From_Loop: ");
|
||||
// for(i = 0; i < g_pkg_uart_2.offset; i++){
|
||||
// PRINT(" %02X", g_pkg_uart_2.pkg[i]);
|
||||
// }
|
||||
// PRINT("\n");
|
||||
}
|
||||
|
||||
|
||||
if(g_flag_bt_state){
|
||||
g_flag_notify_temp = set_response_tran_to_notify(g_pkg_uart_2.pkg, g_pkg_uart_2.offset, &g_notify_buftemp);
|
||||
}
|
||||
else{
|
||||
g_dbn_ble_state_acs_enable.flag = 0;
|
||||
}
|
||||
InitPkgUart(&g_pkg_uart_2);
|
||||
}
|
||||
}
|
||||
|
||||
201
vd960DBN/BLE/OnlyUpdateApp_Peripheral/Ld/Link.ld
Normal file
201
vd960DBN/BLE/OnlyUpdateApp_Peripheral/Ld/Link.ld
Normal file
@@ -0,0 +1,201 @@
|
||||
ENTRY( _start )
|
||||
|
||||
__stack_size = 2048;
|
||||
|
||||
PROVIDE( _stack_size = __stack_size );
|
||||
|
||||
|
||||
MEMORY
|
||||
{
|
||||
/* CH32V20x_D6 - CH32V203F6-CH32V203G6-CH32V203C6 */
|
||||
/*
|
||||
FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 32K
|
||||
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 10K
|
||||
*/
|
||||
|
||||
/* CH32V20x_D6 - CH32V203K8-CH32V203C8-CH32V203G8-CH32V203F8 */
|
||||
/*
|
||||
FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 64K
|
||||
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 20K
|
||||
*/
|
||||
|
||||
/* CH32V20x_D8 - CH32V203RB
|
||||
CH32V20x_D8W - CH32V208x
|
||||
FLASH + RAM supports the following configuration
|
||||
FLASH-128K + RAM-64K
|
||||
FLASH-144K + RAM-48K
|
||||
FLASH-160K + RAM-32K
|
||||
*/
|
||||
FLASH (rx) : ORIGIN = 0x00004000, LENGTH = 240K
|
||||
RAM (xrw) : ORIGIN = 0x20004000, LENGTH = 48K
|
||||
}
|
||||
|
||||
PROVIDE( __global_pointer$ = 0x20004000 );
|
||||
|
||||
SECTIONS
|
||||
{
|
||||
|
||||
.init :
|
||||
{
|
||||
_sinit = .;
|
||||
. = ALIGN(4);
|
||||
KEEP(*(SORT_NONE(.init)))
|
||||
KEEP(*(.ImageFlag))
|
||||
KEEP(*(.ImageFlag.*))
|
||||
. = ALIGN(4);
|
||||
_einit = .;
|
||||
} >FLASH AT>FLASH
|
||||
|
||||
.vector :
|
||||
{
|
||||
*(.vector);
|
||||
. = ALIGN(64);
|
||||
KEEP(*(SORT_NONE(.handle_reset)))
|
||||
} >FLASH AT>FLASH
|
||||
|
||||
.highcode :
|
||||
{
|
||||
. = ALIGN(4);
|
||||
*(.highcode);
|
||||
*(.highcode.*);
|
||||
. = ALIGN(4);
|
||||
} >FLASH AT>FLASH
|
||||
|
||||
.text :
|
||||
{
|
||||
. = ALIGN(4);
|
||||
*(.text)
|
||||
*(.text.*)
|
||||
*(.rodata)
|
||||
*(.rodata*)
|
||||
*(.sdata2.*)
|
||||
*(.gnu.linkonce.t.*)
|
||||
. = ALIGN(4);
|
||||
} >FLASH AT>FLASH
|
||||
|
||||
.fini :
|
||||
{
|
||||
KEEP(*(SORT_NONE(.fini)))
|
||||
. = ALIGN(4);
|
||||
} >FLASH AT>FLASH
|
||||
|
||||
PROVIDE( _etext = . );
|
||||
PROVIDE( _eitcm = . );
|
||||
|
||||
.preinit_array :
|
||||
{
|
||||
PROVIDE_HIDDEN (__preinit_array_start = .);
|
||||
KEEP (*(.preinit_array))
|
||||
PROVIDE_HIDDEN (__preinit_array_end = .);
|
||||
} >FLASH AT>FLASH
|
||||
|
||||
.init_array :
|
||||
{
|
||||
PROVIDE_HIDDEN (__init_array_start = .);
|
||||
KEEP (*(SORT_BY_INIT_PRIORITY(.init_array.*) SORT_BY_INIT_PRIORITY(.ctors.*)))
|
||||
KEEP (*(.init_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .ctors))
|
||||
PROVIDE_HIDDEN (__init_array_end = .);
|
||||
} >FLASH AT>FLASH
|
||||
|
||||
.fini_array :
|
||||
{
|
||||
PROVIDE_HIDDEN (__fini_array_start = .);
|
||||
KEEP (*(SORT_BY_INIT_PRIORITY(.fini_array.*) SORT_BY_INIT_PRIORITY(.dtors.*)))
|
||||
KEEP (*(.fini_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .dtors))
|
||||
PROVIDE_HIDDEN (__fini_array_end = .);
|
||||
} >FLASH AT>FLASH
|
||||
|
||||
.ctors :
|
||||
{
|
||||
/* gcc uses crtbegin.o to find the start of
|
||||
the constructors, so we make sure it is
|
||||
first. Because this is a wildcard, it
|
||||
doesn't matter if the user does not
|
||||
actually link against crtbegin.o; the
|
||||
linker won't look for a file to match a
|
||||
wildcard. The wildcard also means that it
|
||||
doesn't matter which directory crtbegin.o
|
||||
is in. */
|
||||
KEEP (*crtbegin.o(.ctors))
|
||||
KEEP (*crtbegin?.o(.ctors))
|
||||
/* We don't want to include the .ctor section from
|
||||
the crtend.o file until after the sorted ctors.
|
||||
The .ctor section from the crtend file contains the
|
||||
end of ctors marker and it must be last */
|
||||
KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .ctors))
|
||||
KEEP (*(SORT(.ctors.*)))
|
||||
KEEP (*(.ctors))
|
||||
} >FLASH AT>FLASH
|
||||
|
||||
.dtors :
|
||||
{
|
||||
KEEP (*crtbegin.o(.dtors))
|
||||
KEEP (*crtbegin?.o(.dtors))
|
||||
KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .dtors))
|
||||
KEEP (*(SORT(.dtors.*)))
|
||||
KEEP (*(.dtors))
|
||||
} >FLASH AT>FLASH
|
||||
|
||||
.dalign :
|
||||
{
|
||||
. = ALIGN(4);
|
||||
PROVIDE(_data_vma = .);
|
||||
} >RAM AT>FLASH
|
||||
|
||||
.dlalign :
|
||||
{
|
||||
. = ALIGN(4);
|
||||
PROVIDE(_data_lma = .);
|
||||
} >FLASH AT>FLASH
|
||||
|
||||
.data :
|
||||
{
|
||||
*(.gnu.linkonce.r.*)
|
||||
*(.data .data.*)
|
||||
*(.gnu.linkonce.d.*)
|
||||
. = ALIGN(8);
|
||||
/*PROVIDE( __global_pointer$ = . + 0x800 );*/
|
||||
*(.sdata .sdata.*)
|
||||
*(.gnu.linkonce.s.*)
|
||||
. = ALIGN(8);
|
||||
*(.srodata.cst16)
|
||||
*(.srodata.cst8)
|
||||
*(.srodata.cst4)
|
||||
*(.srodata.cst2)
|
||||
*(.srodata .srodata.*)
|
||||
. = ALIGN(4);
|
||||
PROVIDE( _edata = .);
|
||||
} >RAM AT>FLASH
|
||||
|
||||
.bss :
|
||||
{
|
||||
. = ALIGN(4);
|
||||
PROVIDE( _sbss = .);
|
||||
*(.sbss*)
|
||||
*(.gnu.linkonce.sb.*)
|
||||
*(.bss*)
|
||||
*(.gnu.linkonce.b.*)
|
||||
*(COMMON*)
|
||||
. = ALIGN(4);
|
||||
PROVIDE( _ebss = .);
|
||||
} >RAM AT>FLASH
|
||||
|
||||
PROVIDE( _end = _ebss);
|
||||
PROVIDE( end = . );
|
||||
|
||||
/*.stack ORIGIN(RAM) + LENGTH(RAM) - __stack_size :
|
||||
{
|
||||
PROVIDE( _heap_end = . );
|
||||
. = ALIGN(4);
|
||||
PROVIDE(_susrstack = . );
|
||||
. = . + __stack_size;
|
||||
PROVIDE( _eusrstack = .);
|
||||
} >RAM */
|
||||
|
||||
.stack ORIGIN(RAM)+LENGTH(RAM) :
|
||||
{
|
||||
PROVIDE( _heap_end = . );
|
||||
. = ALIGN(4);
|
||||
PROVIDE(_eusrstack = . );
|
||||
} >RAM
|
||||
}
|
||||
136
vd960DBN/BLE/OnlyUpdateApp_Peripheral/MQTT_SRC/MQTTConnect.h
Normal file
136
vd960DBN/BLE/OnlyUpdateApp_Peripheral/MQTT_SRC/MQTTConnect.h
Normal file
@@ -0,0 +1,136 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2014 IBM Corp.
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
* and the Eclipse Distribution License is available at
|
||||
* http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
*
|
||||
* Contributors:
|
||||
* Ian Craggs - initial API and implementation and/or initial documentation
|
||||
* Xiang Rong - 442039 Add makefile to Embedded C client
|
||||
*******************************************************************************/
|
||||
|
||||
#ifndef MQTTCONNECT_H_
|
||||
#define MQTTCONNECT_H_
|
||||
|
||||
#if !defined(DLLImport)
|
||||
#define DLLImport
|
||||
#endif
|
||||
#if !defined(DLLExport)
|
||||
#define DLLExport
|
||||
#endif
|
||||
|
||||
|
||||
typedef union
|
||||
{
|
||||
unsigned char all; /**< all connect flags */
|
||||
#if defined(REVERSED)
|
||||
struct
|
||||
{
|
||||
unsigned int username : 1; /**< 3.1 user name */
|
||||
unsigned int password : 1; /**< 3.1 password */
|
||||
unsigned int willRetain : 1; /**< will retain setting */
|
||||
unsigned int willQoS : 2; /**< will QoS value */
|
||||
unsigned int will : 1; /**< will flag */
|
||||
unsigned int cleansession : 1; /**< clean session flag */
|
||||
unsigned int : 1; /**< unused */
|
||||
} bits;
|
||||
#else
|
||||
struct
|
||||
{
|
||||
unsigned int : 1; /**< unused */
|
||||
unsigned int cleansession : 1; /**< cleansession flag */
|
||||
unsigned int will : 1; /**< will flag */
|
||||
unsigned int willQoS : 2; /**< will QoS value */
|
||||
unsigned int willRetain : 1; /**< will retain setting */
|
||||
unsigned int password : 1; /**< 3.1 password */
|
||||
unsigned int username : 1; /**< 3.1 user name */
|
||||
} bits;
|
||||
#endif
|
||||
} MQTTConnectFlags; /**< connect flags byte */
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Defines the MQTT "Last Will and Testament" (LWT) settings for
|
||||
* the connect packet.
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
/** The eyecatcher for this structure. must be MQTW. */
|
||||
char struct_id[4];
|
||||
/** The version number of this structure. Must be 0 */
|
||||
int struct_version;
|
||||
/** The LWT topic to which the LWT message will be published. */
|
||||
MQTTString topicName;
|
||||
/** The LWT payload. */
|
||||
MQTTString message;
|
||||
/**
|
||||
* The retained flag for the LWT message (see MQTTAsync_message.retained).
|
||||
*/
|
||||
unsigned char retained;
|
||||
/**
|
||||
* The quality of service setting for the LWT message (see
|
||||
* MQTTAsync_message.qos and @ref qos).
|
||||
*/
|
||||
char qos;
|
||||
} MQTTPacket_willOptions;
|
||||
|
||||
|
||||
#define MQTTPacket_willOptions_initializer { {'M', 'Q', 'T', 'W'}, 0, {NULL, {0, NULL}}, {NULL, {0, NULL}}, 0, 0 }
|
||||
|
||||
|
||||
typedef struct
|
||||
{
|
||||
/** The eyecatcher for this structure. must be MQTC. */
|
||||
char struct_id[4];
|
||||
/** The version number of this structure. Must be 0 */
|
||||
int struct_version;
|
||||
/** Version of MQTT to be used. 3 = 3.1 4 = 3.1.1
|
||||
*/
|
||||
unsigned char MQTTVersion;
|
||||
MQTTString clientID;
|
||||
unsigned short keepAliveInterval;
|
||||
unsigned char cleansession;
|
||||
unsigned char willFlag;
|
||||
MQTTPacket_willOptions will;
|
||||
MQTTString username;
|
||||
MQTTString password;
|
||||
} MQTTPacket_connectData;
|
||||
|
||||
typedef union
|
||||
{
|
||||
unsigned char all; /**< all connack flags */
|
||||
#if defined(REVERSED)
|
||||
struct
|
||||
{
|
||||
unsigned int sessionpresent : 1; /**< session present flag */
|
||||
unsigned int : 7; /**< unused */
|
||||
} bits;
|
||||
#else
|
||||
struct
|
||||
{
|
||||
unsigned int : 7; /**< unused */
|
||||
unsigned int sessionpresent : 1; /**< session present flag */
|
||||
} bits;
|
||||
#endif
|
||||
} MQTTConnackFlags; /**< connack flags byte */
|
||||
|
||||
#define MQTTPacket_connectData_initializer { {'M', 'Q', 'T', 'C'}, 0, 4, {NULL, {0, NULL}}, 60, 1, 0, \
|
||||
MQTTPacket_willOptions_initializer, {NULL, {0, NULL}}, {NULL, {0, NULL}} }
|
||||
|
||||
DLLExport int MQTTSerialize_connect(unsigned char* buf, int buflen, MQTTPacket_connectData* options);
|
||||
DLLExport int MQTTDeserialize_connect(MQTTPacket_connectData* data, unsigned char* buf, int len);
|
||||
|
||||
DLLExport int MQTTSerialize_connack(unsigned char* buf, int buflen, unsigned char connack_rc, unsigned char sessionPresent);
|
||||
DLLExport int MQTTDeserialize_connack(unsigned char* sessionPresent, unsigned char* connack_rc, unsigned char* buf, int buflen);
|
||||
|
||||
DLLExport int MQTTSerialize_disconnect(unsigned char* buf, int buflen);
|
||||
DLLExport int MQTTSerialize_pingreq(unsigned char* buf, int buflen);
|
||||
|
||||
#endif /* MQTTCONNECT_H_ */
|
||||
@@ -0,0 +1,214 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2014 IBM Corp.
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
* and the Eclipse Distribution License is available at
|
||||
* http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
*
|
||||
* Contributors:
|
||||
* Ian Craggs - initial API and implementation and/or initial documentation
|
||||
*******************************************************************************/
|
||||
|
||||
#include "MQTTPacket.h"
|
||||
#include "StackTrace.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
/**
|
||||
* Determines the length of the MQTT connect packet that would be produced using the supplied connect options.
|
||||
* @param options the options to be used to build the connect packet
|
||||
* @return the length of buffer needed to contain the serialized version of the packet
|
||||
*/
|
||||
int MQTTSerialize_connectLength(MQTTPacket_connectData* options)
|
||||
{
|
||||
int len = 0;
|
||||
|
||||
FUNC_ENTRY;
|
||||
|
||||
if (options->MQTTVersion == 3)
|
||||
len = 12; /* variable depending on MQTT or MQIsdp */
|
||||
else if (options->MQTTVersion == 4)
|
||||
len = 10;
|
||||
|
||||
len += MQTTstrlen(options->clientID)+2;
|
||||
if (options->willFlag)
|
||||
len += MQTTstrlen(options->will.topicName)+2 + MQTTstrlen(options->will.message)+2;
|
||||
if (options->username.cstring || options->username.lenstring.data)
|
||||
len += MQTTstrlen(options->username)+2;
|
||||
if (options->password.cstring || options->password.lenstring.data)
|
||||
len += MQTTstrlen(options->password)+2;
|
||||
|
||||
FUNC_EXIT_RC(len);
|
||||
return len;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Serializes the connect options into the buffer.
|
||||
* @param buf the buffer into which the packet will be serialized
|
||||
* @param len the length in bytes of the supplied buffer
|
||||
* @param options the options to be used to build the connect packet
|
||||
* @return serialized length, or error if 0
|
||||
*/
|
||||
int MQTTSerialize_connect(unsigned char* buf, int buflen, MQTTPacket_connectData* options)
|
||||
{
|
||||
unsigned char *ptr = buf;
|
||||
MQTTHeader header = {0};
|
||||
MQTTConnectFlags flags = {0};
|
||||
int len = 0;
|
||||
int rc = -1;
|
||||
|
||||
FUNC_ENTRY;
|
||||
if (MQTTPacket_len(len = MQTTSerialize_connectLength(options)) > buflen)
|
||||
{
|
||||
rc = MQTTPACKET_BUFFER_TOO_SHORT;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
header.byte = 0;
|
||||
header.bits.type = CONNECT;
|
||||
writeChar(&ptr, header.byte); /* write header */
|
||||
|
||||
ptr += MQTTPacket_encode(ptr, len); /* write remaining length */
|
||||
|
||||
if (options->MQTTVersion == 4)
|
||||
{
|
||||
writeCString(&ptr, "MQTT");
|
||||
writeChar(&ptr, (char) 4);
|
||||
}
|
||||
else
|
||||
{
|
||||
writeCString(&ptr, "MQIsdp");
|
||||
writeChar(&ptr, (char) 3);
|
||||
}
|
||||
|
||||
flags.all = 0;
|
||||
flags.bits.cleansession = options->cleansession;
|
||||
flags.bits.will = (options->willFlag) ? 1 : 0;
|
||||
if (flags.bits.will)
|
||||
{
|
||||
flags.bits.willQoS = options->will.qos;
|
||||
flags.bits.willRetain = options->will.retained;
|
||||
}
|
||||
|
||||
if (options->username.cstring || options->username.lenstring.data)
|
||||
flags.bits.username = 1;
|
||||
if (options->password.cstring || options->password.lenstring.data)
|
||||
flags.bits.password = 1;
|
||||
|
||||
writeChar(&ptr, flags.all);
|
||||
writeInt(&ptr, options->keepAliveInterval);
|
||||
writeMQTTString(&ptr, options->clientID);
|
||||
if (options->willFlag)
|
||||
{
|
||||
writeMQTTString(&ptr, options->will.topicName);
|
||||
writeMQTTString(&ptr, options->will.message);
|
||||
}
|
||||
if (flags.bits.username)
|
||||
writeMQTTString(&ptr, options->username);
|
||||
if (flags.bits.password)
|
||||
writeMQTTString(&ptr, options->password);
|
||||
|
||||
rc = ptr - buf;
|
||||
|
||||
exit: FUNC_EXIT_RC(rc);
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Deserializes the supplied (wire) buffer into connack data - return code
|
||||
* @param sessionPresent the session present flag returned (only for MQTT 3.1.1)
|
||||
* @param connack_rc returned integer value of the connack return code
|
||||
* @param buf the raw buffer data, of the correct length determined by the remaining length field
|
||||
* @param len the length in bytes of the data in the supplied buffer
|
||||
* @return error code. 1 is success, 0 is failure
|
||||
*/
|
||||
int MQTTDeserialize_connack(unsigned char* sessionPresent, unsigned char* connack_rc, unsigned char* buf, int buflen)
|
||||
{
|
||||
MQTTHeader header = {0};
|
||||
unsigned char* curdata = buf;
|
||||
unsigned char* enddata = NULL;
|
||||
int rc = 0;
|
||||
int mylen;
|
||||
MQTTConnackFlags flags = {0};
|
||||
|
||||
FUNC_ENTRY;
|
||||
header.byte = readChar(&curdata);
|
||||
if (header.bits.type != CONNACK)
|
||||
goto exit;
|
||||
|
||||
curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */
|
||||
enddata = curdata + mylen;
|
||||
if (enddata - curdata < 2)
|
||||
goto exit;
|
||||
|
||||
flags.all = readChar(&curdata);
|
||||
*sessionPresent = flags.bits.sessionpresent;
|
||||
*connack_rc = readChar(&curdata);
|
||||
|
||||
rc = 1;
|
||||
exit:
|
||||
FUNC_EXIT_RC(rc);
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Serializes a 0-length packet into the supplied buffer, ready for writing to a socket
|
||||
* @param buf the buffer into which the packet will be serialized
|
||||
* @param buflen the length in bytes of the supplied buffer, to avoid overruns
|
||||
* @param packettype the message type
|
||||
* @return serialized length, or error if 0
|
||||
*/
|
||||
int MQTTSerialize_zero(unsigned char* buf, int buflen, unsigned char packettype)
|
||||
{
|
||||
MQTTHeader header = {0};
|
||||
int rc = -1;
|
||||
unsigned char *ptr = buf;
|
||||
|
||||
FUNC_ENTRY;
|
||||
if (buflen < 2)
|
||||
{
|
||||
rc = MQTTPACKET_BUFFER_TOO_SHORT;
|
||||
goto exit;
|
||||
}
|
||||
header.byte = 0;
|
||||
header.bits.type = packettype;
|
||||
writeChar(&ptr, header.byte); /* write header */
|
||||
|
||||
ptr += MQTTPacket_encode(ptr, 0); /* write remaining length */
|
||||
rc = ptr - buf;
|
||||
exit:
|
||||
FUNC_EXIT_RC(rc);
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Serializes a disconnect packet into the supplied buffer, ready for writing to a socket
|
||||
* @param buf the buffer into which the packet will be serialized
|
||||
* @param buflen the length in bytes of the supplied buffer, to avoid overruns
|
||||
* @return serialized length, or error if 0
|
||||
*/
|
||||
int MQTTSerialize_disconnect(unsigned char* buf, int buflen)
|
||||
{
|
||||
return MQTTSerialize_zero(buf, buflen, DISCONNECT);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Serializes a disconnect packet into the supplied buffer, ready for writing to a socket
|
||||
* @param buf the buffer into which the packet will be serialized
|
||||
* @param buflen the length in bytes of the supplied buffer, to avoid overruns
|
||||
* @return serialized length, or error if 0
|
||||
*/
|
||||
int MQTTSerialize_pingreq(unsigned char* buf, int buflen)
|
||||
{
|
||||
return MQTTSerialize_zero(buf, buflen, PINGREQ);
|
||||
}
|
||||
@@ -0,0 +1,148 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2014 IBM Corp.
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
* and the Eclipse Distribution License is available at
|
||||
* http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
*
|
||||
* Contributors:
|
||||
* Ian Craggs - initial API and implementation and/or initial documentation
|
||||
*******************************************************************************/
|
||||
|
||||
#include "StackTrace.h"
|
||||
#include "MQTTPacket.h"
|
||||
#include <string.h>
|
||||
|
||||
#define min(a, b) ((a < b) ? a : b)
|
||||
|
||||
|
||||
/**
|
||||
* Validates MQTT protocol name and version combinations
|
||||
* @param protocol the MQTT protocol name as an MQTTString
|
||||
* @param version the MQTT protocol version number, as in the connect packet
|
||||
* @return correct MQTT combination? 1 is true, 0 is false
|
||||
*/
|
||||
int MQTTPacket_checkVersion(MQTTString* protocol, int version)
|
||||
{
|
||||
int rc = 0;
|
||||
|
||||
if (version == 3 && memcmp(protocol->lenstring.data, "MQIsdp",
|
||||
min(6, protocol->lenstring.len)) == 0)
|
||||
rc = 1;
|
||||
else if (version == 4 && memcmp(protocol->lenstring.data, "MQTT",
|
||||
min(4, protocol->lenstring.len)) == 0)
|
||||
rc = 1;
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Deserializes the supplied (wire) buffer into connect data structure
|
||||
* @param data the connect data structure to be filled out
|
||||
* @param buf the raw buffer data, of the correct length determined by the remaining length field
|
||||
* @param len the length in bytes of the data in the supplied buffer
|
||||
* @return error code. 1 is success, 0 is failure
|
||||
*/
|
||||
int MQTTDeserialize_connect(MQTTPacket_connectData* data, unsigned char* buf, int len)
|
||||
{
|
||||
MQTTHeader header = {0};
|
||||
MQTTConnectFlags flags = {0};
|
||||
unsigned char* curdata = buf;
|
||||
unsigned char* enddata = &buf[len];
|
||||
int rc = 0;
|
||||
MQTTString Protocol;
|
||||
int version;
|
||||
int mylen = 0;
|
||||
|
||||
FUNC_ENTRY;
|
||||
header.byte = readChar(&curdata);
|
||||
if (header.bits.type != CONNECT)
|
||||
goto exit;
|
||||
|
||||
curdata += MQTTPacket_decodeBuf(curdata, &mylen); /* read remaining length */
|
||||
|
||||
if (!readMQTTLenString(&Protocol, &curdata, enddata) ||
|
||||
enddata - curdata < 0) /* do we have enough data to read the protocol version byte? */
|
||||
goto exit;
|
||||
|
||||
version = (int)readChar(&curdata); /* Protocol version */
|
||||
/* If we don't recognize the protocol version, we don't parse the connect packet on the
|
||||
* basis that we don't know what the format will be.
|
||||
*/
|
||||
if (MQTTPacket_checkVersion(&Protocol, version))
|
||||
{
|
||||
flags.all = readChar(&curdata);
|
||||
data->cleansession = flags.bits.cleansession;
|
||||
data->keepAliveInterval = readInt(&curdata);
|
||||
if (!readMQTTLenString(&data->clientID, &curdata, enddata))
|
||||
goto exit;
|
||||
data->willFlag = flags.bits.will;
|
||||
if (flags.bits.will)
|
||||
{
|
||||
data->will.qos = flags.bits.willQoS;
|
||||
data->will.retained = flags.bits.willRetain;
|
||||
if (!readMQTTLenString(&data->will.topicName, &curdata, enddata) ||
|
||||
!readMQTTLenString(&data->will.message, &curdata, enddata))
|
||||
goto exit;
|
||||
}
|
||||
if (flags.bits.username)
|
||||
{
|
||||
if (enddata - curdata < 3 || !readMQTTLenString(&data->username, &curdata, enddata))
|
||||
goto exit; /* username flag set, but no username supplied - invalid */
|
||||
if (flags.bits.password &&
|
||||
(enddata - curdata < 3 || !readMQTTLenString(&data->password, &curdata, enddata)))
|
||||
goto exit; /* password flag set, but no password supplied - invalid */
|
||||
}
|
||||
else if (flags.bits.password)
|
||||
goto exit; /* password flag set without username - invalid */
|
||||
rc = 1;
|
||||
}
|
||||
exit:
|
||||
FUNC_EXIT_RC(rc);
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Serializes the connack packet into the supplied buffer.
|
||||
* @param buf the buffer into which the packet will be serialized
|
||||
* @param buflen the length in bytes of the supplied buffer
|
||||
* @param connack_rc the integer connack return code to be used
|
||||
* @param sessionPresent the MQTT 3.1.1 sessionPresent flag
|
||||
* @return serialized length, or error if 0
|
||||
*/
|
||||
int MQTTSerialize_connack(unsigned char* buf, int buflen, unsigned char connack_rc, unsigned char sessionPresent)
|
||||
{
|
||||
MQTTHeader header = {0};
|
||||
int rc = 0;
|
||||
unsigned char *ptr = buf;
|
||||
MQTTConnackFlags flags = {0};
|
||||
|
||||
FUNC_ENTRY;
|
||||
if (buflen < 2)
|
||||
{
|
||||
rc = MQTTPACKET_BUFFER_TOO_SHORT;
|
||||
goto exit;
|
||||
}
|
||||
header.byte = 0;
|
||||
header.bits.type = CONNACK;
|
||||
writeChar(&ptr, header.byte); /* write header */
|
||||
|
||||
ptr += MQTTPacket_encode(ptr, 2); /* write remaining length */
|
||||
|
||||
flags.all = 0;
|
||||
flags.bits.sessionpresent = sessionPresent;
|
||||
writeChar(&ptr, flags.all);
|
||||
writeChar(&ptr, connack_rc);
|
||||
|
||||
rc = ptr - buf;
|
||||
exit:
|
||||
FUNC_EXIT_RC(rc);
|
||||
return rc;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,107 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2014 IBM Corp.
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
* and the Eclipse Distribution License is available at
|
||||
* http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
*
|
||||
* Contributors:
|
||||
* Ian Craggs - initial API and implementation and/or initial documentation
|
||||
*******************************************************************************/
|
||||
|
||||
#include "StackTrace.h"
|
||||
#include "MQTTPacket.h"
|
||||
#include <string.h>
|
||||
|
||||
#define min(a, b) ((a < b) ? 1 : 0)
|
||||
|
||||
/**
|
||||
* Deserializes the supplied (wire) buffer into publish data
|
||||
* @param dup returned integer - the MQTT dup flag
|
||||
* @param qos returned integer - the MQTT QoS value
|
||||
* @param retained returned integer - the MQTT retained flag
|
||||
* @param packetid returned integer - the MQTT packet identifier
|
||||
* @param topicName returned MQTTString - the MQTT topic in the publish
|
||||
* @param payload returned byte buffer - the MQTT publish payload
|
||||
* @param payloadlen returned integer - the length of the MQTT payload
|
||||
* @param buf the raw buffer data, of the correct length determined by the remaining length field
|
||||
* @param buflen the length in bytes of the data in the supplied buffer
|
||||
* @return error code. 1 is success
|
||||
*/
|
||||
int MQTTDeserialize_publish(unsigned char* dup, int* qos, unsigned char* retained, unsigned short* packetid, MQTTString* topicName,
|
||||
unsigned char** payload, int* payloadlen, unsigned char* buf, int buflen)
|
||||
{
|
||||
MQTTHeader header = {0};
|
||||
unsigned char* curdata = buf;
|
||||
unsigned char* enddata = NULL;
|
||||
int rc = 0;
|
||||
int mylen = 0;
|
||||
|
||||
FUNC_ENTRY;
|
||||
header.byte = readChar(&curdata);
|
||||
if (header.bits.type != PUBLISH)
|
||||
goto exit;
|
||||
*dup = header.bits.dup;
|
||||
*qos = header.bits.qos;
|
||||
*retained = header.bits.retain;
|
||||
|
||||
curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */
|
||||
enddata = curdata + mylen;
|
||||
|
||||
if (!readMQTTLenString(topicName, &curdata, enddata) ||
|
||||
enddata - curdata < 0) /* do we have enough data to read the protocol version byte? */
|
||||
goto exit;
|
||||
|
||||
if (*qos > 0)
|
||||
*packetid = readInt(&curdata);
|
||||
|
||||
*payloadlen = enddata - curdata;
|
||||
*payload = curdata;
|
||||
rc = 1;
|
||||
exit:
|
||||
FUNC_EXIT_RC(rc);
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Deserializes the supplied (wire) buffer into an ack
|
||||
* @param packettype returned integer - the MQTT packet type
|
||||
* @param dup returned integer - the MQTT dup flag
|
||||
* @param packetid returned integer - the MQTT packet identifier
|
||||
* @param buf the raw buffer data, of the correct length determined by the remaining length field
|
||||
* @param buflen the length in bytes of the data in the supplied buffer
|
||||
* @return error code. 1 is success, 0 is failure
|
||||
*/
|
||||
int MQTTDeserialize_ack(unsigned char* packettype, unsigned char* dup, unsigned short* packetid, unsigned char* buf, int buflen)
|
||||
{
|
||||
MQTTHeader header = {0};
|
||||
unsigned char* curdata = buf;
|
||||
unsigned char* enddata = NULL;
|
||||
int rc = 0;
|
||||
int mylen;
|
||||
|
||||
FUNC_ENTRY;
|
||||
header.byte = readChar(&curdata);
|
||||
*dup = header.bits.dup;
|
||||
*packettype = header.bits.type;
|
||||
|
||||
curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */
|
||||
enddata = curdata + mylen;
|
||||
|
||||
if (enddata - curdata < 2)
|
||||
goto exit;
|
||||
*packetid = readInt(&curdata);
|
||||
|
||||
rc = 1;
|
||||
exit:
|
||||
FUNC_EXIT_RC(rc);
|
||||
return rc;
|
||||
}
|
||||
|
||||
258
vd960DBN/BLE/OnlyUpdateApp_Peripheral/MQTT_SRC/MQTTFormat.c
Normal file
258
vd960DBN/BLE/OnlyUpdateApp_Peripheral/MQTT_SRC/MQTTFormat.c
Normal file
@@ -0,0 +1,258 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2014 IBM Corp.
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
* and the Eclipse Distribution License is available at
|
||||
* http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
*
|
||||
* Contributors:
|
||||
* Ian Craggs - initial API and implementation and/or initial documentation
|
||||
*******************************************************************************/
|
||||
|
||||
#include "StackTrace.h"
|
||||
#include "MQTTPacket.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
|
||||
const char* MQTTPacket_names[] =
|
||||
{
|
||||
"RESERVED", "CONNECT", "CONNACK", "PUBLISH", "PUBACK", "PUBREC", "PUBREL",
|
||||
"PUBCOMP", "SUBSCRIBE", "SUBACK", "UNSUBSCRIBE", "UNSUBACK",
|
||||
"PINGREQ", "PINGRESP", "DISCONNECT"
|
||||
};
|
||||
|
||||
|
||||
const char* MQTTPacket_getName(unsigned short packetid)
|
||||
{
|
||||
return MQTTPacket_names[packetid];
|
||||
}
|
||||
|
||||
|
||||
int MQTTStringFormat_connect(char* strbuf, int strbuflen, MQTTPacket_connectData* data)
|
||||
{
|
||||
int strindex = 0;
|
||||
|
||||
strindex = snprintf(strbuf, strbuflen,
|
||||
"CONNECT MQTT version %d, client id %.*s, clean session %d, keep alive %d",
|
||||
(int)data->MQTTVersion, data->clientID.lenstring.len, data->clientID.lenstring.data,
|
||||
(int)data->cleansession, data->keepAliveInterval);
|
||||
if (data->willFlag)
|
||||
strindex += snprintf(&strbuf[strindex], strbuflen - strindex,
|
||||
", will QoS %d, will retain %d, will topic %.*s, will message %.*s",
|
||||
data->will.qos, data->will.retained,
|
||||
data->will.topicName.lenstring.len, data->will.topicName.lenstring.data,
|
||||
data->will.message.lenstring.len, data->will.message.lenstring.data);
|
||||
if (data->username.lenstring.data && data->username.lenstring.len > 0)
|
||||
strindex += snprintf(&strbuf[strindex], strbuflen - strindex,
|
||||
", user name %.*s", data->username.lenstring.len, data->username.lenstring.data);
|
||||
if (data->password.lenstring.data && data->password.lenstring.len > 0)
|
||||
strindex += snprintf(&strbuf[strindex], strbuflen - strindex,
|
||||
", password %.*s", data->password.lenstring.len, data->password.lenstring.data);
|
||||
return strindex;
|
||||
}
|
||||
|
||||
|
||||
int MQTTStringFormat_connack(char* strbuf, int strbuflen, unsigned char connack_rc, unsigned char sessionPresent)
|
||||
{
|
||||
int strindex = snprintf(strbuf, strbuflen, "CONNACK session present %d, rc %d", sessionPresent, connack_rc);
|
||||
return strindex;
|
||||
}
|
||||
|
||||
|
||||
int MQTTStringFormat_publish(char* strbuf, int strbuflen, unsigned char dup, int qos, unsigned char retained,
|
||||
unsigned short packetid, MQTTString topicName, unsigned char* payload, int payloadlen)
|
||||
{
|
||||
int strindex = snprintf(strbuf, strbuflen,
|
||||
"PUBLISH dup %d, QoS %d, retained %d, packet id %d, topic %.*s, payload length %d, payload %.*s",
|
||||
dup, qos, retained, packetid,
|
||||
(topicName.lenstring.len < 20) ? topicName.lenstring.len : 20, topicName.lenstring.data,
|
||||
payloadlen, (payloadlen < 20) ? payloadlen : 20, payload);
|
||||
return strindex;
|
||||
}
|
||||
|
||||
|
||||
int MQTTStringFormat_ack(char* strbuf, int strbuflen, unsigned char packettype, unsigned char dup, unsigned short packetid)
|
||||
{
|
||||
int strindex = snprintf(strbuf, strbuflen, "%s, packet id %d", MQTTPacket_names[packettype], packetid);
|
||||
if (dup)
|
||||
strindex += snprintf(strbuf + strindex, strbuflen - strindex, ", dup %d", dup);
|
||||
return strindex;
|
||||
}
|
||||
|
||||
|
||||
int MQTTStringFormat_subscribe(char* strbuf, int strbuflen, unsigned char dup, unsigned short packetid, int count,
|
||||
MQTTString topicFilters[], int requestedQoSs[])
|
||||
{
|
||||
return snprintf(strbuf, strbuflen,
|
||||
"SUBSCRIBE dup %d, packet id %d count %d topic %.*s qos %d",
|
||||
dup, packetid, count,
|
||||
topicFilters[0].lenstring.len, topicFilters[0].lenstring.data,
|
||||
requestedQoSs[0]);
|
||||
}
|
||||
|
||||
|
||||
int MQTTStringFormat_suback(char* strbuf, int strbuflen, unsigned short packetid, int count, int* grantedQoSs)
|
||||
{
|
||||
return snprintf(strbuf, strbuflen,
|
||||
"SUBACK packet id %d count %d granted qos %d", packetid, count, grantedQoSs[0]);
|
||||
}
|
||||
|
||||
|
||||
int MQTTStringFormat_unsubscribe(char* strbuf, int strbuflen, unsigned char dup, unsigned short packetid,
|
||||
int count, MQTTString topicFilters[])
|
||||
{
|
||||
return snprintf(strbuf, strbuflen,
|
||||
"UNSUBSCRIBE dup %d, packet id %d count %d topic %.*s",
|
||||
dup, packetid, count,
|
||||
topicFilters[0].lenstring.len, topicFilters[0].lenstring.data);
|
||||
}
|
||||
|
||||
|
||||
char* MQTTFormat_toClientString(char* strbuf, int strbuflen, unsigned char* buf, int buflen)
|
||||
{
|
||||
int index = 0;
|
||||
int rem_length = 0;
|
||||
MQTTHeader header = {0};
|
||||
int strindex = 0;
|
||||
|
||||
header.byte = buf[index++];
|
||||
index += MQTTPacket_decodeBuf(&buf[index], &rem_length);
|
||||
|
||||
switch (header.bits.type)
|
||||
{
|
||||
case CONNACK:
|
||||
{
|
||||
unsigned char sessionPresent, connack_rc;
|
||||
if (MQTTDeserialize_connack(&sessionPresent, &connack_rc, buf, buflen) == 1)
|
||||
strindex = MQTTStringFormat_connack(strbuf, strbuflen, connack_rc, sessionPresent);
|
||||
}
|
||||
break;
|
||||
case PUBLISH:
|
||||
{
|
||||
unsigned char dup, retained, *payload;
|
||||
unsigned short packetid;
|
||||
int qos, payloadlen;
|
||||
MQTTString topicName = MQTTString_initializer;
|
||||
if (MQTTDeserialize_publish(&dup, &qos, &retained, &packetid, &topicName,
|
||||
&payload, &payloadlen, buf, buflen) == 1)
|
||||
strindex = MQTTStringFormat_publish(strbuf, strbuflen, dup, qos, retained, packetid,
|
||||
topicName, payload, payloadlen);
|
||||
}
|
||||
break;
|
||||
case PUBACK:
|
||||
case PUBREC:
|
||||
case PUBREL:
|
||||
case PUBCOMP:
|
||||
{
|
||||
unsigned char packettype, dup;
|
||||
unsigned short packetid;
|
||||
if (MQTTDeserialize_ack(&packettype, &dup, &packetid, buf, buflen) == 1)
|
||||
strindex = MQTTStringFormat_ack(strbuf, strbuflen, packettype, dup, packetid);
|
||||
}
|
||||
break;
|
||||
case SUBACK:
|
||||
{
|
||||
unsigned short packetid;
|
||||
int maxcount = 1, count = 0;
|
||||
int grantedQoSs[1];
|
||||
if (MQTTDeserialize_suback(&packetid, maxcount, &count, grantedQoSs, buf, buflen) == 1)
|
||||
strindex = MQTTStringFormat_suback(strbuf, strbuflen, packetid, count, grantedQoSs);
|
||||
}
|
||||
break;
|
||||
case UNSUBACK:
|
||||
{
|
||||
unsigned short packetid;
|
||||
if (MQTTDeserialize_unsuback(&packetid, buf, buflen) == 1)
|
||||
strindex = MQTTStringFormat_ack(strbuf, strbuflen, UNSUBACK, 0, packetid);
|
||||
}
|
||||
break;
|
||||
case PINGREQ:
|
||||
case PINGRESP:
|
||||
case DISCONNECT:
|
||||
strindex = snprintf(strbuf, strbuflen, "%s", MQTTPacket_names[header.bits.type]);
|
||||
break;
|
||||
}
|
||||
return strbuf;
|
||||
}
|
||||
|
||||
|
||||
char* MQTTFormat_toServerString(char* strbuf, int strbuflen, unsigned char* buf, int buflen)
|
||||
{
|
||||
int index = 0;
|
||||
int rem_length = 0;
|
||||
MQTTHeader header = {0};
|
||||
int strindex = 0;
|
||||
|
||||
header.byte = buf[index++];
|
||||
index += MQTTPacket_decodeBuf(&buf[index], &rem_length);
|
||||
|
||||
switch (header.bits.type)
|
||||
{
|
||||
case CONNECT:
|
||||
{
|
||||
MQTTPacket_connectData data;
|
||||
int rc;
|
||||
if ((rc = MQTTDeserialize_connect(&data, buf, buflen)) == 1)
|
||||
strindex = MQTTStringFormat_connect(strbuf, strbuflen, &data);
|
||||
}
|
||||
break;
|
||||
case PUBLISH:
|
||||
{
|
||||
unsigned char dup, retained, *payload;
|
||||
unsigned short packetid;
|
||||
int qos, payloadlen;
|
||||
MQTTString topicName = MQTTString_initializer;
|
||||
if (MQTTDeserialize_publish(&dup, &qos, &retained, &packetid, &topicName,
|
||||
&payload, &payloadlen, buf, buflen) == 1)
|
||||
strindex = MQTTStringFormat_publish(strbuf, strbuflen, dup, qos, retained, packetid,
|
||||
topicName, payload, payloadlen);
|
||||
}
|
||||
break;
|
||||
case PUBACK:
|
||||
case PUBREC:
|
||||
case PUBREL:
|
||||
case PUBCOMP:
|
||||
{
|
||||
unsigned char packettype, dup;
|
||||
unsigned short packetid;
|
||||
if (MQTTDeserialize_ack(&packettype, &dup, &packetid, buf, buflen) == 1)
|
||||
strindex = MQTTStringFormat_ack(strbuf, strbuflen, packettype, dup, packetid);
|
||||
}
|
||||
break;
|
||||
case SUBSCRIBE:
|
||||
{
|
||||
unsigned char dup;
|
||||
unsigned short packetid;
|
||||
int maxcount = 1, count = 0;
|
||||
MQTTString topicFilters[1];
|
||||
int requestedQoSs[1];
|
||||
if (MQTTDeserialize_subscribe(&dup, &packetid, maxcount, &count,
|
||||
topicFilters, requestedQoSs, buf, buflen) == 1)
|
||||
strindex = MQTTStringFormat_subscribe(strbuf, strbuflen, dup, packetid, count, topicFilters, requestedQoSs);;
|
||||
}
|
||||
break;
|
||||
case UNSUBSCRIBE:
|
||||
{
|
||||
unsigned char dup;
|
||||
unsigned short packetid;
|
||||
int maxcount = 1, count = 0;
|
||||
MQTTString topicFilters[1];
|
||||
if (MQTTDeserialize_unsubscribe(&dup, &packetid, maxcount, &count, topicFilters, buf, buflen) == 1)
|
||||
strindex = MQTTStringFormat_unsubscribe(strbuf, strbuflen, dup, packetid, count, topicFilters);
|
||||
}
|
||||
break;
|
||||
case PINGREQ:
|
||||
case PINGRESP:
|
||||
case DISCONNECT:
|
||||
strindex = snprintf(strbuf, strbuflen, "%s", MQTTPacket_names[header.bits.type]);
|
||||
break;
|
||||
}
|
||||
strbuf[strbuflen] = '\0';
|
||||
return strbuf;
|
||||
}
|
||||
37
vd960DBN/BLE/OnlyUpdateApp_Peripheral/MQTT_SRC/MQTTFormat.h
Normal file
37
vd960DBN/BLE/OnlyUpdateApp_Peripheral/MQTT_SRC/MQTTFormat.h
Normal file
@@ -0,0 +1,37 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2014 IBM Corp.
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
* and the Eclipse Distribution License is available at
|
||||
* http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
*
|
||||
* Contributors:
|
||||
* Ian Craggs - initial API and implementation and/or initial documentation
|
||||
*******************************************************************************/
|
||||
|
||||
#if !defined(MQTTFORMAT_H)
|
||||
#define MQTTFORMAT_H
|
||||
|
||||
#include "StackTrace.h"
|
||||
#include "MQTTPacket.h"
|
||||
|
||||
const char* MQTTPacket_getName(unsigned short packetid);
|
||||
int MQTTStringFormat_connect(char* strbuf, int strbuflen, MQTTPacket_connectData* data);
|
||||
int MQTTStringFormat_connack(char* strbuf, int strbuflen, unsigned char connack_rc, unsigned char sessionPresent);
|
||||
int MQTTStringFormat_publish(char* strbuf, int strbuflen, unsigned char dup, int qos, unsigned char retained,
|
||||
unsigned short packetid, MQTTString topicName, unsigned char* payload, int payloadlen);
|
||||
int MQTTStringFormat_ack(char* strbuf, int strbuflen, unsigned char packettype, unsigned char dup, unsigned short packetid);
|
||||
int MQTTStringFormat_subscribe(char* strbuf, int strbuflen, unsigned char dup, unsigned short packetid, int count,
|
||||
MQTTString topicFilters[], int requestedQoSs[]);
|
||||
int MQTTStringFormat_suback(char* strbuf, int strbuflen, unsigned short packetid, int count, int* grantedQoSs);
|
||||
int MQTTStringFormat_unsubscribe(char* strbuf, int strbuflen, unsigned char dup, unsigned short packetid,
|
||||
int count, MQTTString topicFilters[]);
|
||||
char* MQTTFormat_toClientString(char* strbuf, int strbuflen, unsigned char* buf, int buflen);
|
||||
char* MQTTFormat_toServerString(char* strbuf, int strbuflen, unsigned char* buf, int buflen);
|
||||
|
||||
#endif
|
||||
412
vd960DBN/BLE/OnlyUpdateApp_Peripheral/MQTT_SRC/MQTTPacket.c
Normal file
412
vd960DBN/BLE/OnlyUpdateApp_Peripheral/MQTT_SRC/MQTTPacket.c
Normal file
@@ -0,0 +1,412 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2014 IBM Corp.
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
* and the Eclipse Distribution License is available at
|
||||
* http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
*
|
||||
* Contributors:
|
||||
* Ian Craggs - initial API and implementation and/or initial documentation
|
||||
* Sergio R. Caprile - non-blocking packet read functions for stream transport
|
||||
*******************************************************************************/
|
||||
|
||||
#include "StackTrace.h"
|
||||
#include "MQTTPacket.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
/**
|
||||
* Encodes the message length according to the MQTT algorithm
|
||||
* @param buf the buffer into which the encoded data is written
|
||||
* @param length the length to be encoded
|
||||
* @return the number of bytes written to buffer
|
||||
*/
|
||||
int MQTTPacket_encode(unsigned char* buf, int length)
|
||||
{
|
||||
int rc = 0;
|
||||
|
||||
FUNC_ENTRY;
|
||||
do
|
||||
{
|
||||
char d = length % 128;
|
||||
length /= 128;
|
||||
/* if there are more digits to encode, set the top bit of this digit */
|
||||
if (length > 0)
|
||||
d |= 0x80;
|
||||
buf[rc++] = d;
|
||||
} while (length > 0);
|
||||
FUNC_EXIT_RC(rc);
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Decodes the message length according to the MQTT algorithm
|
||||
* @param getcharfn pointer to function to read the next character from the data source
|
||||
* @param value the decoded length returned
|
||||
* @return the number of bytes read from the socket
|
||||
*/
|
||||
int MQTTPacket_decode(int (*getcharfn)(unsigned char*, int), int* value)
|
||||
{
|
||||
unsigned char c;
|
||||
int multiplier = 1;
|
||||
int len = 0;
|
||||
#define MAX_NO_OF_REMAINING_LENGTH_BYTES 4
|
||||
|
||||
FUNC_ENTRY;
|
||||
*value = 0;
|
||||
do
|
||||
{
|
||||
int rc = MQTTPACKET_READ_ERROR;
|
||||
|
||||
if (++len > MAX_NO_OF_REMAINING_LENGTH_BYTES)
|
||||
{
|
||||
rc = MQTTPACKET_READ_ERROR; /* bad data */
|
||||
goto exit;
|
||||
}
|
||||
rc = (*getcharfn)(&c, 1);
|
||||
if (rc != 1)
|
||||
goto exit;
|
||||
*value += (c & 127) * multiplier;
|
||||
multiplier *= 128;
|
||||
} while ((c & 128) != 0);
|
||||
exit:
|
||||
FUNC_EXIT_RC(len);
|
||||
return len;
|
||||
}
|
||||
|
||||
|
||||
int MQTTPacket_len(int rem_len)
|
||||
{
|
||||
rem_len += 1; /* header byte */
|
||||
|
||||
/* now remaining_length field */
|
||||
if (rem_len < 128)
|
||||
rem_len += 1;
|
||||
else if (rem_len < 16384)
|
||||
rem_len += 2;
|
||||
else if (rem_len < 2097151)
|
||||
rem_len += 3;
|
||||
else
|
||||
rem_len += 4;
|
||||
return rem_len;
|
||||
}
|
||||
|
||||
|
||||
static unsigned char* bufptr;
|
||||
|
||||
int bufchar(unsigned char* c, int count)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < count; ++i)
|
||||
*c = *bufptr++;
|
||||
return count;
|
||||
}
|
||||
|
||||
|
||||
int MQTTPacket_decodeBuf(unsigned char* buf, int* value)
|
||||
{
|
||||
bufptr = buf;
|
||||
return MQTTPacket_decode(bufchar, value);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Calculates an integer from two bytes read from the input buffer
|
||||
* @param pptr pointer to the input buffer - incremented by the number of bytes used & returned
|
||||
* @return the integer value calculated
|
||||
*/
|
||||
int readInt(unsigned char** pptr)
|
||||
{
|
||||
unsigned char* ptr = *pptr;
|
||||
int len = 256*(*ptr) + (*(ptr+1));
|
||||
*pptr += 2;
|
||||
return len;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Reads one character from the input buffer.
|
||||
* @param pptr pointer to the input buffer - incremented by the number of bytes used & returned
|
||||
* @return the character read
|
||||
*/
|
||||
char readChar(unsigned char** pptr)
|
||||
{
|
||||
char c = **pptr;
|
||||
(*pptr)++;
|
||||
return c;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Writes one character to an output buffer.
|
||||
* @param pptr pointer to the output buffer - incremented by the number of bytes used & returned
|
||||
* @param c the character to write
|
||||
*/
|
||||
void writeChar(unsigned char** pptr, char c)
|
||||
{
|
||||
**pptr = c;
|
||||
(*pptr)++;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Writes an integer as 2 bytes to an output buffer.
|
||||
* @param pptr pointer to the output buffer - incremented by the number of bytes used & returned
|
||||
* @param anInt the integer to write
|
||||
*/
|
||||
void writeInt(unsigned char** pptr, int anInt)
|
||||
{
|
||||
**pptr = (unsigned char)(anInt / 256);
|
||||
(*pptr)++;
|
||||
**pptr = (unsigned char)(anInt % 256);
|
||||
(*pptr)++;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Writes a "UTF" string to an output buffer. Converts C string to length-delimited.
|
||||
* @param pptr pointer to the output buffer - incremented by the number of bytes used & returned
|
||||
* @param string the C string to write
|
||||
*/
|
||||
void writeCString(unsigned char** pptr, const char* string)
|
||||
{
|
||||
int len = strlen(string);
|
||||
writeInt(pptr, len);
|
||||
memcpy(*pptr, string, len);
|
||||
*pptr += len;
|
||||
}
|
||||
|
||||
|
||||
int getLenStringLen(char* ptr)
|
||||
{
|
||||
int len = 256*((unsigned char)(*ptr)) + (unsigned char)(*(ptr+1));
|
||||
return len;
|
||||
}
|
||||
|
||||
|
||||
void writeMQTTString(unsigned char** pptr, MQTTString mqttstring)
|
||||
{
|
||||
if (mqttstring.lenstring.len > 0)
|
||||
{
|
||||
writeInt(pptr, mqttstring.lenstring.len);
|
||||
memcpy(*pptr, mqttstring.lenstring.data, mqttstring.lenstring.len);
|
||||
*pptr += mqttstring.lenstring.len;
|
||||
}
|
||||
else if (mqttstring.cstring)
|
||||
writeCString(pptr, mqttstring.cstring);
|
||||
else
|
||||
writeInt(pptr, 0);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param mqttstring the MQTTString structure into which the data is to be read
|
||||
* @param pptr pointer to the output buffer - incremented by the number of bytes used & returned
|
||||
* @param enddata pointer to the end of the data: do not read beyond
|
||||
* @return 1 if successful, 0 if not
|
||||
*/
|
||||
int readMQTTLenString(MQTTString* mqttstring, unsigned char** pptr, unsigned char* enddata)
|
||||
{
|
||||
int rc = 0;
|
||||
|
||||
FUNC_ENTRY;
|
||||
/* the first two bytes are the length of the string */
|
||||
if (enddata - (*pptr) > 1) /* enough length to read the integer? */
|
||||
{
|
||||
mqttstring->lenstring.len = readInt(pptr); /* increments pptr to point past length */
|
||||
if (&(*pptr)[mqttstring->lenstring.len] <= enddata)
|
||||
{
|
||||
mqttstring->lenstring.data = (char*)*pptr;
|
||||
*pptr += mqttstring->lenstring.len;
|
||||
rc = 1;
|
||||
}
|
||||
}
|
||||
mqttstring->cstring = NULL;
|
||||
FUNC_EXIT_RC(rc);
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return the length of the MQTTstring - C string if there is one, otherwise the length delimited string
|
||||
* @param mqttstring the string to return the length of
|
||||
* @return the length of the string
|
||||
*/
|
||||
int MQTTstrlen(MQTTString mqttstring)
|
||||
{
|
||||
int rc = 0;
|
||||
|
||||
if (mqttstring.cstring)
|
||||
rc = strlen(mqttstring.cstring);
|
||||
else
|
||||
rc = mqttstring.lenstring.len;
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Compares an MQTTString to a C string
|
||||
* @param a the MQTTString to compare
|
||||
* @param bptr the C string to compare
|
||||
* @return boolean - equal or not
|
||||
*/
|
||||
int MQTTPacket_equals(MQTTString* a, char* bptr)
|
||||
{
|
||||
int alen = 0,
|
||||
blen = 0;
|
||||
char *aptr;
|
||||
|
||||
if (a->cstring)
|
||||
{
|
||||
aptr = a->cstring;
|
||||
alen = strlen(a->cstring);
|
||||
}
|
||||
else
|
||||
{
|
||||
aptr = a->lenstring.data;
|
||||
alen = a->lenstring.len;
|
||||
}
|
||||
blen = strlen(bptr);
|
||||
|
||||
return (alen == blen) && (strncmp(aptr, bptr, alen) == 0);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Helper function to read packet data from some source into a buffer
|
||||
* @param buf the buffer into which the packet will be serialized
|
||||
* @param buflen the length in bytes of the supplied buffer
|
||||
* @param getfn pointer to a function which will read any number of bytes from the needed source
|
||||
* @return integer MQTT packet type, or -1 on error
|
||||
* @note the whole message must fit into the caller's buffer
|
||||
*/
|
||||
int MQTTPacket_read(unsigned char* buf, int buflen, int (*getfn)(unsigned char*, int))
|
||||
{
|
||||
int rc = -1;
|
||||
MQTTHeader header = {0};
|
||||
int len = 0;
|
||||
int rem_len = 0;
|
||||
int temp = 0; //add by wfq 2016-11-17
|
||||
|
||||
/* 1. read the header byte. This has the packet type in it */
|
||||
if ((*getfn)(buf, 1) != 1)
|
||||
goto exit;
|
||||
|
||||
len = 1;
|
||||
/* 2. read the remaining length. This is variable in itself */
|
||||
temp = MQTTPacket_decode(getfn, &rem_len);
|
||||
// printf("\r\nMQTTPacket_decode, len:%d, rem_len:%d\r\n", temp, rem_len);
|
||||
len += MQTTPacket_encode(buf + 1, rem_len); /* put the original remaining length back into the buffer */
|
||||
// printf("\r\nrem_len:%d,len:%d, buflen:%d\r\n", rem_len, len, buflen);
|
||||
/* 3. read the rest of the buffer using a callback to supply the rest of the data */
|
||||
if((rem_len + len) > buflen)
|
||||
goto exit;
|
||||
if ((*getfn)(buf + len, rem_len) != rem_len)
|
||||
goto exit;
|
||||
|
||||
header.byte = buf[0];
|
||||
rc = header.bits.type;
|
||||
exit:
|
||||
return rc;
|
||||
}
|
||||
|
||||
/**
|
||||
* Decodes the message length according to the MQTT algorithm, non-blocking
|
||||
* @param trp pointer to a transport structure holding what is needed to solve getting data from it
|
||||
* @param value the decoded length returned
|
||||
* @return integer the number of bytes read from the socket, 0 for call again, or -1 on error
|
||||
*/
|
||||
static int MQTTPacket_decodenb(MQTTTransport *trp)
|
||||
{
|
||||
unsigned char c;
|
||||
int rc = MQTTPACKET_READ_ERROR;
|
||||
|
||||
FUNC_ENTRY;
|
||||
if(trp->len == 0){ /* initialize on first call */
|
||||
trp->multiplier = 1;
|
||||
trp->rem_len = 0;
|
||||
}
|
||||
do {
|
||||
int frc;
|
||||
if (++(trp->len) > MAX_NO_OF_REMAINING_LENGTH_BYTES)
|
||||
goto exit;
|
||||
if ((frc=(*trp->getfn)(trp->sck, &c, 1)) == -1)
|
||||
goto exit;
|
||||
if (frc == 0){
|
||||
rc = 0;
|
||||
goto exit;
|
||||
}
|
||||
trp->rem_len += (c & 127) * trp->multiplier;
|
||||
trp->multiplier *= 128;
|
||||
} while ((c & 128) != 0);
|
||||
rc = trp->len;
|
||||
exit:
|
||||
FUNC_EXIT_RC(rc);
|
||||
return rc;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to read packet data from some source into a buffer, non-blocking
|
||||
* @param buf the buffer into which the packet will be serialized
|
||||
* @param buflen the length in bytes of the supplied buffer
|
||||
* @param trp pointer to a transport structure holding what is needed to solve getting data from it
|
||||
* @return integer MQTT packet type, 0 for call again, or -1 on error
|
||||
* @note the whole message must fit into the caller's buffer
|
||||
*/
|
||||
int MQTTPacket_readnb(unsigned char* buf, int buflen, MQTTTransport *trp)
|
||||
{
|
||||
int rc = -1, frc;
|
||||
MQTTHeader header = {0};
|
||||
|
||||
switch(trp->state){
|
||||
default:
|
||||
trp->state = 0;
|
||||
/*FALLTHROUGH*/
|
||||
case 0:
|
||||
/* read the header byte. This has the packet type in it */
|
||||
if ((frc=(*trp->getfn)(trp->sck, buf, 1)) == -1)
|
||||
goto exit;
|
||||
if (frc == 0)
|
||||
return 0;
|
||||
trp->len = 0;
|
||||
++trp->state;
|
||||
/*FALLTHROUGH*/
|
||||
/* read the remaining length. This is variable in itself */
|
||||
case 1:
|
||||
if((frc=MQTTPacket_decodenb(trp)) == MQTTPACKET_READ_ERROR)
|
||||
goto exit;
|
||||
if(frc == 0)
|
||||
return 0;
|
||||
trp->len = 1 + MQTTPacket_encode(buf + 1, trp->rem_len); /* put the original remaining length back into the buffer */
|
||||
if((trp->rem_len + trp->len) > buflen)
|
||||
goto exit;
|
||||
++trp->state;
|
||||
/*FALLTHROUGH*/
|
||||
case 2:
|
||||
/* read the rest of the buffer using a callback to supply the rest of the data */
|
||||
if ((frc=(*trp->getfn)(trp->sck, buf + trp->len, trp->rem_len)) == -1)
|
||||
goto exit;
|
||||
if (frc == 0)
|
||||
return 0;
|
||||
trp->rem_len -= frc;
|
||||
trp->len += frc;
|
||||
if(trp->rem_len)
|
||||
return 0;
|
||||
|
||||
header.byte = buf[0];
|
||||
rc = header.bits.type;
|
||||
break;
|
||||
}
|
||||
|
||||
exit:
|
||||
trp->state = 0;
|
||||
return rc;
|
||||
}
|
||||
|
||||
133
vd960DBN/BLE/OnlyUpdateApp_Peripheral/MQTT_SRC/MQTTPacket.h
Normal file
133
vd960DBN/BLE/OnlyUpdateApp_Peripheral/MQTT_SRC/MQTTPacket.h
Normal file
@@ -0,0 +1,133 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2014 IBM Corp.
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
* and the Eclipse Distribution License is available at
|
||||
* http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
*
|
||||
* Contributors:
|
||||
* Ian Craggs - initial API and implementation and/or initial documentation
|
||||
* Xiang Rong - 442039 Add makefile to Embedded C client
|
||||
*******************************************************************************/
|
||||
|
||||
#ifndef MQTTPACKET_H_
|
||||
#define MQTTPACKET_H_
|
||||
|
||||
#if defined(__cplusplus) /* If this is a C++ compiler, use C linkage */
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#if defined(WIN32_DLL) || defined(WIN64_DLL)
|
||||
#define DLLImport __declspec(dllimport)
|
||||
#define DLLExport __declspec(dllexport)
|
||||
#elif defined(LINUX_SO)
|
||||
#define DLLImport extern
|
||||
#define DLLExport __attribute__ ((visibility ("default")))
|
||||
#else
|
||||
#define DLLImport
|
||||
#define DLLExport
|
||||
#endif
|
||||
|
||||
enum errors
|
||||
{
|
||||
MQTTPACKET_BUFFER_TOO_SHORT = -2,
|
||||
MQTTPACKET_READ_ERROR = -1,
|
||||
MQTTPACKET_READ_COMPLETE
|
||||
};
|
||||
|
||||
enum msgTypes
|
||||
{
|
||||
CONNECT = 1, CONNACK, PUBLISH, PUBACK, PUBREC, PUBREL,
|
||||
PUBCOMP, SUBSCRIBE, SUBACK, UNSUBSCRIBE, UNSUBACK,
|
||||
PINGREQ, PINGRESP, DISCONNECT
|
||||
};
|
||||
|
||||
/**
|
||||
* Bitfields for the MQTT header byte.
|
||||
*/
|
||||
typedef union
|
||||
{
|
||||
unsigned char byte; /**< the whole byte */
|
||||
#if defined(REVERSED)
|
||||
struct
|
||||
{
|
||||
unsigned int type : 4; /**< message type nibble */
|
||||
unsigned int dup : 1; /**< DUP flag bit */
|
||||
unsigned int qos : 2; /**< QoS value, 0, 1 or 2 */
|
||||
unsigned int retain : 1; /**< retained flag bit */
|
||||
} bits;
|
||||
#else
|
||||
struct
|
||||
{
|
||||
unsigned int retain : 1; /**< retained flag bit */
|
||||
unsigned int qos : 2; /**< QoS value, 0, 1 or 2 */
|
||||
unsigned int dup : 1; /**< DUP flag bit */
|
||||
unsigned int type : 4; /**< message type nibble */
|
||||
} bits;
|
||||
#endif
|
||||
} MQTTHeader;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
int len;
|
||||
char* data;
|
||||
} MQTTLenString;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
char* cstring;
|
||||
MQTTLenString lenstring;
|
||||
} MQTTString;
|
||||
|
||||
#define MQTTString_initializer {NULL, {0, NULL}}
|
||||
|
||||
int MQTTstrlen(MQTTString mqttstring);
|
||||
|
||||
#include "MQTTConnect.h"
|
||||
#include "MQTTPublish.h"
|
||||
#include "MQTTSubscribe.h"
|
||||
#include "MQTTUnsubscribe.h"
|
||||
#include "MQTTFormat.h"
|
||||
|
||||
int MQTTSerialize_ack(unsigned char* buf, int buflen, unsigned char type, unsigned char dup, unsigned short packetid);
|
||||
int MQTTDeserialize_ack(unsigned char* packettype, unsigned char* dup, unsigned short* packetid, unsigned char* buf, int buflen);
|
||||
|
||||
int MQTTPacket_len(int rem_len);
|
||||
int MQTTPacket_equals(MQTTString* a, char* b);
|
||||
|
||||
int MQTTPacket_encode(unsigned char* buf, int length);
|
||||
int MQTTPacket_decode(int (*getcharfn)(unsigned char*, int), int* value);
|
||||
int MQTTPacket_decodeBuf(unsigned char* buf, int* value);
|
||||
|
||||
int readInt(unsigned char** pptr);
|
||||
char readChar(unsigned char** pptr);
|
||||
void writeChar(unsigned char** pptr, char c);
|
||||
void writeInt(unsigned char** pptr, int anInt);
|
||||
int readMQTTLenString(MQTTString* mqttstring, unsigned char** pptr, unsigned char* enddata);
|
||||
void writeCString(unsigned char** pptr, const char* string);
|
||||
void writeMQTTString(unsigned char** pptr, MQTTString mqttstring);
|
||||
|
||||
DLLExport int MQTTPacket_read(unsigned char* buf, int buflen, int (*getfn)(unsigned char*, int));
|
||||
|
||||
typedef struct {
|
||||
int (*getfn)(void *, unsigned char*, int); /* must return -1 for error, 0 for call again, or the number of bytes read */
|
||||
void *sck; /* pointer to whatever the system may use to identify the transport */
|
||||
int multiplier;
|
||||
int rem_len;
|
||||
int len;
|
||||
char state;
|
||||
}MQTTTransport;
|
||||
|
||||
int MQTTPacket_readnb(unsigned char* buf, int buflen, MQTTTransport *trp);
|
||||
|
||||
#ifdef __cplusplus /* If this is a C++ compiler, use C linkage */
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
#endif /* MQTTPACKET_H_ */
|
||||
38
vd960DBN/BLE/OnlyUpdateApp_Peripheral/MQTT_SRC/MQTTPublish.h
Normal file
38
vd960DBN/BLE/OnlyUpdateApp_Peripheral/MQTT_SRC/MQTTPublish.h
Normal file
@@ -0,0 +1,38 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2014 IBM Corp.
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
* and the Eclipse Distribution License is available at
|
||||
* http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
*
|
||||
* Contributors:
|
||||
* Ian Craggs - initial API and implementation and/or initial documentation
|
||||
* Xiang Rong - 442039 Add makefile to Embedded C client
|
||||
*******************************************************************************/
|
||||
|
||||
#ifndef MQTTPUBLISH_H_
|
||||
#define MQTTPUBLISH_H_
|
||||
|
||||
#if !defined(DLLImport)
|
||||
#define DLLImport
|
||||
#endif
|
||||
#if !defined(DLLExport)
|
||||
#define DLLExport
|
||||
#endif
|
||||
|
||||
DLLExport int MQTTSerialize_publish(unsigned char* buf, int buflen, unsigned char dup, int qos, unsigned char retained, unsigned short packetid,
|
||||
MQTTString topicName, unsigned char* payload, int payloadlen);
|
||||
|
||||
DLLExport int MQTTDeserialize_publish(unsigned char* dup, int* qos, unsigned char* retained, unsigned short* packetid, MQTTString* topicName,
|
||||
unsigned char** payload, int* payloadlen, unsigned char* buf, int len);
|
||||
|
||||
DLLExport int MQTTSerialize_puback(unsigned char* buf, int buflen, unsigned short packetid);
|
||||
DLLExport int MQTTSerialize_pubrel(unsigned char* buf, int buflen, unsigned char dup, unsigned short packetid);
|
||||
DLLExport int MQTTSerialize_pubcomp(unsigned char* buf, int buflen, unsigned short packetid);
|
||||
|
||||
#endif /* MQTTPUBLISH_H_ */
|
||||
@@ -0,0 +1,169 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2014 IBM Corp.
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
* and the Eclipse Distribution License is available at
|
||||
* http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
*
|
||||
* Contributors:
|
||||
* Ian Craggs - initial API and implementation and/or initial documentation
|
||||
* Ian Craggs - fix for https://bugs.eclipse.org/bugs/show_bug.cgi?id=453144
|
||||
*******************************************************************************/
|
||||
|
||||
#include "MQTTPacket.h"
|
||||
#include "StackTrace.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
|
||||
/**
|
||||
* Determines the length of the MQTT publish packet that would be produced using the supplied parameters
|
||||
* @param qos the MQTT QoS of the publish (packetid is omitted for QoS 0)
|
||||
* @param topicName the topic name to be used in the publish
|
||||
* @param payloadlen the length of the payload to be sent
|
||||
* @return the length of buffer needed to contain the serialized version of the packet
|
||||
*/
|
||||
int MQTTSerialize_publishLength(int qos, MQTTString topicName, int payloadlen)
|
||||
{
|
||||
int len = 0;
|
||||
|
||||
len += 2 + MQTTstrlen(topicName) + payloadlen;
|
||||
if (qos > 0)
|
||||
len += 2; /* packetid */
|
||||
return len;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Serializes the supplied publish data into the supplied buffer, ready for sending
|
||||
* @param buf the buffer into which the packet will be serialized
|
||||
* @param buflen the length in bytes of the supplied buffer
|
||||
* @param dup integer - the MQTT dup flag
|
||||
* @param qos integer - the MQTT QoS value
|
||||
* @param retained integer - the MQTT retained flag
|
||||
* @param packetid integer - the MQTT packet identifier
|
||||
* @param topicName MQTTString - the MQTT topic in the publish
|
||||
* @param payload byte buffer - the MQTT publish payload
|
||||
* @param payloadlen integer - the length of the MQTT payload
|
||||
* @return the length of the serialized data. <= 0 indicates error
|
||||
*/
|
||||
int MQTTSerialize_publish(unsigned char* buf, int buflen, unsigned char dup, int qos, unsigned char retained, unsigned short packetid,
|
||||
MQTTString topicName, unsigned char* payload, int payloadlen)
|
||||
{
|
||||
unsigned char *ptr = buf;
|
||||
MQTTHeader header = {0};
|
||||
int rem_len = 0;
|
||||
int rc = 0;
|
||||
|
||||
FUNC_ENTRY;
|
||||
if (MQTTPacket_len(rem_len = MQTTSerialize_publishLength(qos, topicName, payloadlen)) > buflen)
|
||||
{
|
||||
rc = MQTTPACKET_BUFFER_TOO_SHORT;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
header.bits.type = PUBLISH;
|
||||
header.bits.dup = dup;
|
||||
header.bits.qos = qos;
|
||||
header.bits.retain = retained;
|
||||
writeChar(&ptr, header.byte); /* write header */
|
||||
|
||||
ptr += MQTTPacket_encode(ptr, rem_len); /* write remaining length */;
|
||||
|
||||
writeMQTTString(&ptr, topicName);
|
||||
|
||||
if (qos > 0)
|
||||
writeInt(&ptr, packetid);
|
||||
|
||||
memcpy(ptr, payload, payloadlen);
|
||||
ptr += payloadlen;
|
||||
|
||||
rc = ptr - buf;
|
||||
|
||||
exit:
|
||||
FUNC_EXIT_RC(rc);
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Serializes the ack packet into the supplied buffer.
|
||||
* @param buf the buffer into which the packet will be serialized
|
||||
* @param buflen the length in bytes of the supplied buffer
|
||||
* @param type the MQTT packet type
|
||||
* @param dup the MQTT dup flag
|
||||
* @param packetid the MQTT packet identifier
|
||||
* @return serialized length, or error if 0
|
||||
*/
|
||||
int MQTTSerialize_ack(unsigned char* buf, int buflen, unsigned char packettype, unsigned char dup, unsigned short packetid)
|
||||
{
|
||||
MQTTHeader header = {0};
|
||||
int rc = 0;
|
||||
unsigned char *ptr = buf;
|
||||
|
||||
FUNC_ENTRY;
|
||||
if (buflen < 4)
|
||||
{
|
||||
rc = MQTTPACKET_BUFFER_TOO_SHORT;
|
||||
goto exit;
|
||||
}
|
||||
header.bits.type = packettype;
|
||||
header.bits.dup = dup;
|
||||
header.bits.qos = (packettype == PUBREL) ? 1 : 0;
|
||||
writeChar(&ptr, header.byte); /* write header */
|
||||
|
||||
ptr += MQTTPacket_encode(ptr, 2); /* write remaining length */
|
||||
writeInt(&ptr, packetid);
|
||||
rc = ptr - buf;
|
||||
exit:
|
||||
FUNC_EXIT_RC(rc);
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Serializes a puback packet into the supplied buffer.
|
||||
* @param buf the buffer into which the packet will be serialized
|
||||
* @param buflen the length in bytes of the supplied buffer
|
||||
* @param packetid integer - the MQTT packet identifier
|
||||
* @return serialized length, or error if 0
|
||||
*/
|
||||
int MQTTSerialize_puback(unsigned char* buf, int buflen, unsigned short packetid)
|
||||
{
|
||||
return MQTTSerialize_ack(buf, buflen, PUBACK, 0, packetid);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Serializes a pubrel packet into the supplied buffer.
|
||||
* @param buf the buffer into which the packet will be serialized
|
||||
* @param buflen the length in bytes of the supplied buffer
|
||||
* @param dup integer - the MQTT dup flag
|
||||
* @param packetid integer - the MQTT packet identifier
|
||||
* @return serialized length, or error if 0
|
||||
*/
|
||||
int MQTTSerialize_pubrel(unsigned char* buf, int buflen, unsigned char dup, unsigned short packetid)
|
||||
{
|
||||
return MQTTSerialize_ack(buf, buflen, PUBREL, dup, packetid);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Serializes a pubrel packet into the supplied buffer.
|
||||
* @param buf the buffer into which the packet will be serialized
|
||||
* @param buflen the length in bytes of the supplied buffer
|
||||
* @param packetid integer - the MQTT packet identifier
|
||||
* @return serialized length, or error if 0
|
||||
*/
|
||||
int MQTTSerialize_pubcomp(unsigned char* buf, int buflen, unsigned short packetid)
|
||||
{
|
||||
return MQTTSerialize_ack(buf, buflen, PUBCOMP, 0, packetid);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2014 IBM Corp.
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
* and the Eclipse Distribution License is available at
|
||||
* http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
*
|
||||
* Contributors:
|
||||
* Ian Craggs - initial API and implementation and/or initial documentation
|
||||
* Xiang Rong - 442039 Add makefile to Embedded C client
|
||||
*******************************************************************************/
|
||||
|
||||
#ifndef MQTTSUBSCRIBE_H_
|
||||
#define MQTTSUBSCRIBE_H_
|
||||
|
||||
#if !defined(DLLImport)
|
||||
#define DLLImport
|
||||
#endif
|
||||
#if !defined(DLLExport)
|
||||
#define DLLExport
|
||||
#endif
|
||||
|
||||
DLLExport int MQTTSerialize_subscribe(unsigned char* buf, int buflen, unsigned char dup, unsigned short packetid,
|
||||
int count, MQTTString topicFilters[], int requestedQoSs[]);
|
||||
|
||||
DLLExport int MQTTDeserialize_subscribe(unsigned char* dup, unsigned short* packetid,
|
||||
int maxcount, int* count, MQTTString topicFilters[], int requestedQoSs[], unsigned char* buf, int len);
|
||||
|
||||
DLLExport int MQTTSerialize_suback(unsigned char* buf, int buflen, unsigned short packetid, int count, int* grantedQoSs);
|
||||
|
||||
DLLExport int MQTTDeserialize_suback(unsigned short* packetid, int maxcount, int* count, int grantedQoSs[], unsigned char* buf, int len);
|
||||
|
||||
|
||||
#endif /* MQTTSUBSCRIBE_H_ */
|
||||
@@ -0,0 +1,137 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2014 IBM Corp.
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
* and the Eclipse Distribution License is available at
|
||||
* http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
*
|
||||
* Contributors:
|
||||
* Ian Craggs - initial API and implementation and/or initial documentation
|
||||
*******************************************************************************/
|
||||
|
||||
#include "MQTTPacket.h"
|
||||
#include "StackTrace.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
/**
|
||||
* Determines the length of the MQTT subscribe packet that would be produced using the supplied parameters
|
||||
* @param count the number of topic filter strings in topicFilters
|
||||
* @param topicFilters the array of topic filter strings to be used in the publish
|
||||
* @return the length of buffer needed to contain the serialized version of the packet
|
||||
*/
|
||||
int MQTTSerialize_subscribeLength(int count, MQTTString topicFilters[])
|
||||
{
|
||||
int i;
|
||||
int len = 2; /* packetid */
|
||||
|
||||
for (i = 0; i < count; ++i)
|
||||
len += 2 + MQTTstrlen(topicFilters[i]) + 1; /* length + topic + req_qos */
|
||||
return len;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Serializes the supplied subscribe data into the supplied buffer, ready for sending
|
||||
* @param buf the buffer into which the packet will be serialized
|
||||
* @param buflen the length in bytes of the supplied bufferr
|
||||
* @param dup integer - the MQTT dup flag
|
||||
* @param packetid integer - the MQTT packet identifier
|
||||
* @param count - number of members in the topicFilters and reqQos arrays
|
||||
* @param topicFilters - array of topic filter names
|
||||
* @param requestedQoSs - array of requested QoS
|
||||
* @return the length of the serialized data. <= 0 indicates error
|
||||
*/
|
||||
int MQTTSerialize_subscribe(unsigned char* buf, int buflen, unsigned char dup, unsigned short packetid, int count,
|
||||
MQTTString topicFilters[], int requestedQoSs[])
|
||||
{
|
||||
unsigned char *ptr = buf;
|
||||
MQTTHeader header = {0};
|
||||
int rem_len = 0;
|
||||
int rc = 0;
|
||||
int i = 0;
|
||||
|
||||
FUNC_ENTRY;
|
||||
if (MQTTPacket_len(rem_len = MQTTSerialize_subscribeLength(count, topicFilters)) > buflen)
|
||||
{
|
||||
rc = MQTTPACKET_BUFFER_TOO_SHORT;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
header.byte = 0;
|
||||
header.bits.type = SUBSCRIBE;
|
||||
header.bits.dup = dup;
|
||||
header.bits.qos = 1;
|
||||
writeChar(&ptr, header.byte); /* write header */
|
||||
|
||||
ptr += MQTTPacket_encode(ptr, rem_len); /* write remaining length */;
|
||||
|
||||
writeInt(&ptr, packetid);
|
||||
|
||||
for (i = 0; i < count; ++i)
|
||||
{
|
||||
writeMQTTString(&ptr, topicFilters[i]);
|
||||
writeChar(&ptr, requestedQoSs[i]);
|
||||
}
|
||||
|
||||
rc = ptr - buf;
|
||||
exit:
|
||||
FUNC_EXIT_RC(rc);
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Deserializes the supplied (wire) buffer into suback data
|
||||
* @param packetid returned integer - the MQTT packet identifier
|
||||
* @param maxcount - the maximum number of members allowed in the grantedQoSs array
|
||||
* @param count returned integer - number of members in the grantedQoSs array
|
||||
* @param grantedQoSs returned array of integers - the granted qualities of service
|
||||
* @param buf the raw buffer data, of the correct length determined by the remaining length field
|
||||
* @param buflen the length in bytes of the data in the supplied buffer
|
||||
* @return error code. 1 is success, 0 is failure
|
||||
*/
|
||||
int MQTTDeserialize_suback(unsigned short* packetid, int maxcount, int* count, int grantedQoSs[], unsigned char* buf, int buflen)
|
||||
{
|
||||
MQTTHeader header = {0};
|
||||
unsigned char* curdata = buf;
|
||||
unsigned char* enddata = NULL;
|
||||
int rc = 0;
|
||||
int mylen;
|
||||
|
||||
FUNC_ENTRY;
|
||||
header.byte = readChar(&curdata);
|
||||
if (header.bits.type != SUBACK)
|
||||
goto exit;
|
||||
|
||||
curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */
|
||||
enddata = curdata + mylen;
|
||||
if (enddata - curdata < 2)
|
||||
goto exit;
|
||||
|
||||
*packetid = readInt(&curdata);
|
||||
|
||||
*count = 0;
|
||||
while (curdata < enddata)
|
||||
{
|
||||
if (*count > maxcount)
|
||||
{
|
||||
rc = -1;
|
||||
goto exit;
|
||||
}
|
||||
grantedQoSs[(*count)++] = readChar(&curdata);
|
||||
}
|
||||
|
||||
rc = 1;
|
||||
exit:
|
||||
FUNC_EXIT_RC(rc);
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,112 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2014 IBM Corp.
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
* and the Eclipse Distribution License is available at
|
||||
* http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
*
|
||||
* Contributors:
|
||||
* Ian Craggs - initial API and implementation and/or initial documentation
|
||||
*******************************************************************************/
|
||||
|
||||
#include "MQTTPacket.h"
|
||||
#include "StackTrace.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
|
||||
/**
|
||||
* Deserializes the supplied (wire) buffer into subscribe data
|
||||
* @param dup integer returned - the MQTT dup flag
|
||||
* @param packetid integer returned - the MQTT packet identifier
|
||||
* @param maxcount - the maximum number of members allowed in the topicFilters and requestedQoSs arrays
|
||||
* @param count - number of members in the topicFilters and requestedQoSs arrays
|
||||
* @param topicFilters - array of topic filter names
|
||||
* @param requestedQoSs - array of requested QoS
|
||||
* @param buf the raw buffer data, of the correct length determined by the remaining length field
|
||||
* @param buflen the length in bytes of the data in the supplied buffer
|
||||
* @return the length of the serialized data. <= 0 indicates error
|
||||
*/
|
||||
int MQTTDeserialize_subscribe(unsigned char* dup, unsigned short* packetid, int maxcount, int* count, MQTTString topicFilters[],
|
||||
int requestedQoSs[], unsigned char* buf, int buflen)
|
||||
{
|
||||
MQTTHeader header = {0};
|
||||
unsigned char* curdata = buf;
|
||||
unsigned char* enddata = NULL;
|
||||
int rc = -1;
|
||||
int mylen = 0;
|
||||
|
||||
FUNC_ENTRY;
|
||||
header.byte = readChar(&curdata);
|
||||
if (header.bits.type != SUBSCRIBE)
|
||||
goto exit;
|
||||
*dup = header.bits.dup;
|
||||
|
||||
curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */
|
||||
enddata = curdata + mylen;
|
||||
|
||||
*packetid = readInt(&curdata);
|
||||
|
||||
*count = 0;
|
||||
while (curdata < enddata)
|
||||
{
|
||||
if (!readMQTTLenString(&topicFilters[*count], &curdata, enddata))
|
||||
goto exit;
|
||||
if (curdata >= enddata) /* do we have enough data to read the req_qos version byte? */
|
||||
goto exit;
|
||||
requestedQoSs[*count] = readChar(&curdata);
|
||||
(*count)++;
|
||||
}
|
||||
|
||||
rc = 1;
|
||||
exit:
|
||||
FUNC_EXIT_RC(rc);
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Serializes the supplied suback data into the supplied buffer, ready for sending
|
||||
* @param buf the buffer into which the packet will be serialized
|
||||
* @param buflen the length in bytes of the supplied buffer
|
||||
* @param packetid integer - the MQTT packet identifier
|
||||
* @param count - number of members in the grantedQoSs array
|
||||
* @param grantedQoSs - array of granted QoS
|
||||
* @return the length of the serialized data. <= 0 indicates error
|
||||
*/
|
||||
int MQTTSerialize_suback(unsigned char* buf, int buflen, unsigned short packetid, int count, int* grantedQoSs)
|
||||
{
|
||||
MQTTHeader header = {0};
|
||||
int rc = -1;
|
||||
unsigned char *ptr = buf;
|
||||
int i;
|
||||
|
||||
FUNC_ENTRY;
|
||||
if (buflen < 2 + count)
|
||||
{
|
||||
rc = MQTTPACKET_BUFFER_TOO_SHORT;
|
||||
goto exit;
|
||||
}
|
||||
header.byte = 0;
|
||||
header.bits.type = SUBACK;
|
||||
writeChar(&ptr, header.byte); /* write header */
|
||||
|
||||
ptr += MQTTPacket_encode(ptr, 2 + count); /* write remaining length */
|
||||
|
||||
writeInt(&ptr, packetid);
|
||||
|
||||
for (i = 0; i < count; ++i)
|
||||
writeChar(&ptr, grantedQoSs[i]);
|
||||
|
||||
rc = ptr - buf;
|
||||
exit:
|
||||
FUNC_EXIT_RC(rc);
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2014 IBM Corp.
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
* and the Eclipse Distribution License is available at
|
||||
* http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
*
|
||||
* Contributors:
|
||||
* Ian Craggs - initial API and implementation and/or initial documentation
|
||||
* Xiang Rong - 442039 Add makefile to Embedded C client
|
||||
*******************************************************************************/
|
||||
|
||||
#ifndef MQTTUNSUBSCRIBE_H_
|
||||
#define MQTTUNSUBSCRIBE_H_
|
||||
|
||||
#if !defined(DLLImport)
|
||||
#define DLLImport
|
||||
#endif
|
||||
#if !defined(DLLExport)
|
||||
#define DLLExport
|
||||
#endif
|
||||
|
||||
DLLExport int MQTTSerialize_unsubscribe(unsigned char* buf, int buflen, unsigned char dup, unsigned short packetid,
|
||||
int count, MQTTString topicFilters[]);
|
||||
|
||||
DLLExport int MQTTDeserialize_unsubscribe(unsigned char* dup, unsigned short* packetid, int max_count, int* count, MQTTString topicFilters[],
|
||||
unsigned char* buf, int len);
|
||||
|
||||
DLLExport int MQTTSerialize_unsuback(unsigned char* buf, int buflen, unsigned short packetid);
|
||||
|
||||
DLLExport int MQTTDeserialize_unsuback(unsigned short* packetid, unsigned char* buf, int len);
|
||||
|
||||
#endif /* MQTTUNSUBSCRIBE_H_ */
|
||||
@@ -0,0 +1,106 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2014 IBM Corp.
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
* and the Eclipse Distribution License is available at
|
||||
* http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
*
|
||||
* Contributors:
|
||||
* Ian Craggs - initial API and implementation and/or initial documentation
|
||||
*******************************************************************************/
|
||||
|
||||
#include "MQTTPacket.h"
|
||||
#include "StackTrace.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
/**
|
||||
* Determines the length of the MQTT unsubscribe packet that would be produced using the supplied parameters
|
||||
* @param count the number of topic filter strings in topicFilters
|
||||
* @param topicFilters the array of topic filter strings to be used in the publish
|
||||
* @return the length of buffer needed to contain the serialized version of the packet
|
||||
*/
|
||||
int MQTTSerialize_unsubscribeLength(int count, MQTTString topicFilters[])
|
||||
{
|
||||
int i;
|
||||
int len = 2; /* packetid */
|
||||
|
||||
for (i = 0; i < count; ++i)
|
||||
len += 2 + MQTTstrlen(topicFilters[i]); /* length + topic*/
|
||||
return len;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Serializes the supplied unsubscribe data into the supplied buffer, ready for sending
|
||||
* @param buf the raw buffer data, of the correct length determined by the remaining length field
|
||||
* @param buflen the length in bytes of the data in the supplied buffer
|
||||
* @param dup integer - the MQTT dup flag
|
||||
* @param packetid integer - the MQTT packet identifier
|
||||
* @param count - number of members in the topicFilters array
|
||||
* @param topicFilters - array of topic filter names
|
||||
* @return the length of the serialized data. <= 0 indicates error
|
||||
*/
|
||||
int MQTTSerialize_unsubscribe(unsigned char* buf, int buflen, unsigned char dup, unsigned short packetid,
|
||||
int count, MQTTString topicFilters[])
|
||||
{
|
||||
unsigned char *ptr = buf;
|
||||
MQTTHeader header = {0};
|
||||
int rem_len = 0;
|
||||
int rc = -1;
|
||||
int i = 0;
|
||||
|
||||
FUNC_ENTRY;
|
||||
if (MQTTPacket_len(rem_len = MQTTSerialize_unsubscribeLength(count, topicFilters)) > buflen)
|
||||
{
|
||||
rc = MQTTPACKET_BUFFER_TOO_SHORT;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
header.byte = 0;
|
||||
header.bits.type = UNSUBSCRIBE;
|
||||
header.bits.dup = dup;
|
||||
header.bits.qos = 1;
|
||||
writeChar(&ptr, header.byte); /* write header */
|
||||
|
||||
ptr += MQTTPacket_encode(ptr, rem_len); /* write remaining length */;
|
||||
|
||||
writeInt(&ptr, packetid);
|
||||
|
||||
for (i = 0; i < count; ++i)
|
||||
writeMQTTString(&ptr, topicFilters[i]);
|
||||
|
||||
rc = ptr - buf;
|
||||
exit:
|
||||
FUNC_EXIT_RC(rc);
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Deserializes the supplied (wire) buffer into unsuback data
|
||||
* @param packetid returned integer - the MQTT packet identifier
|
||||
* @param buf the raw buffer data, of the correct length determined by the remaining length field
|
||||
* @param buflen the length in bytes of the data in the supplied buffer
|
||||
* @return error code. 1 is success, 0 is failure
|
||||
*/
|
||||
int MQTTDeserialize_unsuback(unsigned short* packetid, unsigned char* buf, int buflen)
|
||||
{
|
||||
unsigned char type = 0;
|
||||
unsigned char dup = 0;
|
||||
int rc = 0;
|
||||
|
||||
FUNC_ENTRY;
|
||||
rc = MQTTDeserialize_ack(&type, &dup, packetid, buf, buflen);
|
||||
if (type == UNSUBACK)
|
||||
rc = 1;
|
||||
FUNC_EXIT_RC(rc);
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,102 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2014 IBM Corp.
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
* and the Eclipse Distribution License is available at
|
||||
* http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
*
|
||||
* Contributors:
|
||||
* Ian Craggs - initial API and implementation and/or initial documentation
|
||||
*******************************************************************************/
|
||||
|
||||
#include "MQTTPacket.h"
|
||||
#include "StackTrace.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
|
||||
/**
|
||||
* Deserializes the supplied (wire) buffer into unsubscribe data
|
||||
* @param dup integer returned - the MQTT dup flag
|
||||
* @param packetid integer returned - the MQTT packet identifier
|
||||
* @param maxcount - the maximum number of members allowed in the topicFilters and requestedQoSs arrays
|
||||
* @param count - number of members in the topicFilters and requestedQoSs arrays
|
||||
* @param topicFilters - array of topic filter names
|
||||
* @param buf the raw buffer data, of the correct length determined by the remaining length field
|
||||
* @param buflen the length in bytes of the data in the supplied buffer
|
||||
* @return the length of the serialized data. <= 0 indicates error
|
||||
*/
|
||||
int MQTTDeserialize_unsubscribe(unsigned char* dup, unsigned short* packetid, int maxcount, int* count, MQTTString topicFilters[],
|
||||
unsigned char* buf, int len)
|
||||
{
|
||||
MQTTHeader header = {0};
|
||||
unsigned char* curdata = buf;
|
||||
unsigned char* enddata = NULL;
|
||||
int rc = 0;
|
||||
int mylen = 0;
|
||||
|
||||
FUNC_ENTRY;
|
||||
header.byte = readChar(&curdata);
|
||||
if (header.bits.type != UNSUBSCRIBE)
|
||||
goto exit;
|
||||
*dup = header.bits.dup;
|
||||
|
||||
curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */
|
||||
enddata = curdata + mylen;
|
||||
|
||||
*packetid = readInt(&curdata);
|
||||
|
||||
*count = 0;
|
||||
while (curdata < enddata)
|
||||
{
|
||||
if (!readMQTTLenString(&topicFilters[*count], &curdata, enddata))
|
||||
goto exit;
|
||||
(*count)++;
|
||||
}
|
||||
|
||||
rc = 1;
|
||||
exit:
|
||||
FUNC_EXIT_RC(rc);
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Serializes the supplied unsuback data into the supplied buffer, ready for sending
|
||||
* @param buf the buffer into which the packet will be serialized
|
||||
* @param buflen the length in bytes of the supplied buffer
|
||||
* @param packetid integer - the MQTT packet identifier
|
||||
* @return the length of the serialized data. <= 0 indicates error
|
||||
*/
|
||||
int MQTTSerialize_unsuback(unsigned char* buf, int buflen, unsigned short packetid)
|
||||
{
|
||||
MQTTHeader header = {0};
|
||||
int rc = 0;
|
||||
unsigned char *ptr = buf;
|
||||
|
||||
FUNC_ENTRY;
|
||||
if (buflen < 2)
|
||||
{
|
||||
rc = MQTTPACKET_BUFFER_TOO_SHORT;
|
||||
goto exit;
|
||||
}
|
||||
header.byte = 0;
|
||||
header.bits.type = UNSUBACK;
|
||||
writeChar(&ptr, header.byte); /* write header */
|
||||
|
||||
ptr += MQTTPacket_encode(ptr, 2); /* write remaining length */
|
||||
|
||||
writeInt(&ptr, packetid);
|
||||
|
||||
rc = ptr - buf;
|
||||
exit:
|
||||
FUNC_EXIT_RC(rc);
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
78
vd960DBN/BLE/OnlyUpdateApp_Peripheral/MQTT_SRC/StackTrace.h
Normal file
78
vd960DBN/BLE/OnlyUpdateApp_Peripheral/MQTT_SRC/StackTrace.h
Normal file
@@ -0,0 +1,78 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2014 IBM Corp.
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
* and the Eclipse Distribution License is available at
|
||||
* http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
*
|
||||
* Contributors:
|
||||
* Ian Craggs - initial API and implementation and/or initial documentation
|
||||
* Ian Craggs - fix for bug #434081
|
||||
*******************************************************************************/
|
||||
|
||||
#ifndef STACKTRACE_H_
|
||||
#define STACKTRACE_H_
|
||||
|
||||
#include <stdio.h>
|
||||
#define NOSTACKTRACE 1
|
||||
|
||||
#if defined(NOSTACKTRACE)
|
||||
#define FUNC_ENTRY
|
||||
#define FUNC_ENTRY_NOLOG
|
||||
#define FUNC_ENTRY_MED
|
||||
#define FUNC_ENTRY_MAX
|
||||
#define FUNC_EXIT
|
||||
#define FUNC_EXIT_NOLOG
|
||||
#define FUNC_EXIT_MED
|
||||
#define FUNC_EXIT_MAX
|
||||
#define FUNC_EXIT_RC(x)
|
||||
#define FUNC_EXIT_MED_RC(x)
|
||||
#define FUNC_EXIT_MAX_RC(x)
|
||||
|
||||
#else
|
||||
|
||||
#if defined(WIN32)
|
||||
#define inline __inline
|
||||
#define FUNC_ENTRY StackTrace_entry(__FUNCTION__, __LINE__, TRACE_MINIMUM)
|
||||
#define FUNC_ENTRY_NOLOG StackTrace_entry(__FUNCTION__, __LINE__, -1)
|
||||
#define FUNC_ENTRY_MED StackTrace_entry(__FUNCTION__, __LINE__, TRACE_MEDIUM)
|
||||
#define FUNC_ENTRY_MAX StackTrace_entry(__FUNCTION__, __LINE__, TRACE_MAXIMUM)
|
||||
#define FUNC_EXIT StackTrace_exit(__FUNCTION__, __LINE__, NULL, TRACE_MINIMUM)
|
||||
#define FUNC_EXIT_NOLOG StackTrace_exit(__FUNCTION__, __LINE__, -1)
|
||||
#define FUNC_EXIT_MED StackTrace_exit(__FUNCTION__, __LINE__, NULL, TRACE_MEDIUM)
|
||||
#define FUNC_EXIT_MAX StackTrace_exit(__FUNCTION__, __LINE__, NULL, TRACE_MAXIMUM)
|
||||
#define FUNC_EXIT_RC(x) StackTrace_exit(__FUNCTION__, __LINE__, &x, TRACE_MINIMUM)
|
||||
#define FUNC_EXIT_MED_RC(x) StackTrace_exit(__FUNCTION__, __LINE__, &x, TRACE_MEDIUM)
|
||||
#define FUNC_EXIT_MAX_RC(x) StackTrace_exit(__FUNCTION__, __LINE__, &x, TRACE_MAXIMUM)
|
||||
#else
|
||||
#define FUNC_ENTRY StackTrace_entry(__func__, __LINE__, TRACE_MINIMUM)
|
||||
#define FUNC_ENTRY_NOLOG StackTrace_entry(__func__, __LINE__, -1)
|
||||
#define FUNC_ENTRY_MED StackTrace_entry(__func__, __LINE__, TRACE_MEDIUM)
|
||||
#define FUNC_ENTRY_MAX StackTrace_entry(__func__, __LINE__, TRACE_MAXIMUM)
|
||||
#define FUNC_EXIT StackTrace_exit(__func__, __LINE__, NULL, TRACE_MINIMUM)
|
||||
#define FUNC_EXIT_NOLOG StackTrace_exit(__func__, __LINE__, NULL, -1)
|
||||
#define FUNC_EXIT_MED StackTrace_exit(__func__, __LINE__, NULL, TRACE_MEDIUM)
|
||||
#define FUNC_EXIT_MAX StackTrace_exit(__func__, __LINE__, NULL, TRACE_MAXIMUM)
|
||||
#define FUNC_EXIT_RC(x) StackTrace_exit(__func__, __LINE__, &x, TRACE_MINIMUM)
|
||||
#define FUNC_EXIT_MED_RC(x) StackTrace_exit(__func__, __LINE__, &x, TRACE_MEDIUM)
|
||||
#define FUNC_EXIT_MAX_RC(x) StackTrace_exit(__func__, __LINE__, &x, TRACE_MAXIMUM)
|
||||
|
||||
void StackTrace_entry(const char* name, int line, int trace);
|
||||
void StackTrace_exit(const char* name, int line, void* return_value, int trace);
|
||||
|
||||
void StackTrace_printStack(FILE* dest);
|
||||
char* StackTrace_get(unsigned long);
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
|
||||
#endif /* STACKTRACE_H_ */
|
||||
589
vd960DBN/BLE/OnlyUpdateApp_Peripheral/Profile/devinfoservice.c
Normal file
589
vd960DBN/BLE/OnlyUpdateApp_Peripheral/Profile/devinfoservice.c
Normal file
@@ -0,0 +1,589 @@
|
||||
/********************************** (C) COPYRIGHT *******************************
|
||||
* File Name : devinfoservice.c
|
||||
* Author : WCH
|
||||
* Version : V1.0
|
||||
* Date : 2018/12/10
|
||||
* Description : Device information service
|
||||
*********************************************************************************
|
||||
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
|
||||
* Attention: This software (modified or not) and binary are used for
|
||||
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
|
||||
*******************************************************************************/
|
||||
|
||||
/*********************************************************************
|
||||
* INCLUDES
|
||||
*/
|
||||
#include "CONFIG.h"
|
||||
#include "devinfoservice.h"
|
||||
|
||||
/*********************************************************************
|
||||
* MACROS
|
||||
*/
|
||||
|
||||
/*********************************************************************
|
||||
* CONSTANTS
|
||||
*/
|
||||
|
||||
/*********************************************************************
|
||||
* TYPEDEFS
|
||||
*/
|
||||
|
||||
/*********************************************************************
|
||||
* GLOBAL VARIABLES
|
||||
*/
|
||||
// Device information service
|
||||
const uint8_t devInfoServUUID[ATT_BT_UUID_SIZE] = {
|
||||
LO_UINT16(DEVINFO_SERV_UUID), HI_UINT16(DEVINFO_SERV_UUID)};
|
||||
|
||||
// System ID
|
||||
const uint8_t devInfoSystemIdUUID[ATT_BT_UUID_SIZE] = {
|
||||
LO_UINT16(SYSTEM_ID_UUID), HI_UINT16(SYSTEM_ID_UUID)};
|
||||
|
||||
// Model Number String
|
||||
const uint8_t devInfoModelNumberUUID[ATT_BT_UUID_SIZE] = {
|
||||
LO_UINT16(MODEL_NUMBER_UUID), HI_UINT16(MODEL_NUMBER_UUID)};
|
||||
|
||||
// Serial Number String
|
||||
const uint8_t devInfoSerialNumberUUID[ATT_BT_UUID_SIZE] = {
|
||||
LO_UINT16(SERIAL_NUMBER_UUID), HI_UINT16(SERIAL_NUMBER_UUID)};
|
||||
|
||||
// Firmware Revision String
|
||||
const uint8_t devInfoFirmwareRevUUID[ATT_BT_UUID_SIZE] = {
|
||||
LO_UINT16(FIRMWARE_REV_UUID), HI_UINT16(FIRMWARE_REV_UUID)};
|
||||
|
||||
// Hardware Revision String
|
||||
const uint8_t devInfoHardwareRevUUID[ATT_BT_UUID_SIZE] = {
|
||||
LO_UINT16(HARDWARE_REV_UUID), HI_UINT16(HARDWARE_REV_UUID)};
|
||||
|
||||
// Software Revision String
|
||||
const uint8_t devInfoSoftwareRevUUID[ATT_BT_UUID_SIZE] = {
|
||||
LO_UINT16(SOFTWARE_REV_UUID), HI_UINT16(SOFTWARE_REV_UUID)};
|
||||
|
||||
// Manufacturer Name String
|
||||
const uint8_t devInfoMfrNameUUID[ATT_BT_UUID_SIZE] = {
|
||||
LO_UINT16(MANUFACTURER_NAME_UUID), HI_UINT16(MANUFACTURER_NAME_UUID)};
|
||||
|
||||
// IEEE 11073-20601 Regulatory Certification Data List
|
||||
const uint8_t devInfo11073CertUUID[ATT_BT_UUID_SIZE] = {
|
||||
LO_UINT16(IEEE_11073_CERT_DATA_UUID), HI_UINT16(IEEE_11073_CERT_DATA_UUID)};
|
||||
|
||||
// PnP ID
|
||||
const uint8_t devInfoPnpIdUUID[ATT_BT_UUID_SIZE] = {
|
||||
LO_UINT16(PNP_ID_UUID), HI_UINT16(PNP_ID_UUID)};
|
||||
|
||||
/*********************************************************************
|
||||
* EXTERNAL VARIABLES
|
||||
*/
|
||||
|
||||
/*********************************************************************
|
||||
* EXTERNAL FUNCTIONS
|
||||
*/
|
||||
|
||||
/*********************************************************************
|
||||
* LOCAL VARIABLES
|
||||
*/
|
||||
|
||||
/*********************************************************************
|
||||
* Profile Attributes - variables
|
||||
*/
|
||||
|
||||
// Device Information Service attribute
|
||||
static const gattAttrType_t devInfoService = {ATT_BT_UUID_SIZE, devInfoServUUID};
|
||||
|
||||
// System ID characteristic
|
||||
static uint8_t devInfoSystemIdProps = GATT_PROP_READ;
|
||||
static uint8_t devInfoSystemId[DEVINFO_SYSTEM_ID_LEN] = {0, 0, 0, 0, 0, 0, 0, 0};
|
||||
|
||||
// Model Number String characteristic
|
||||
static uint8_t devInfoModelNumberProps = GATT_PROP_READ;
|
||||
static const uint8_t devInfoModelNumber[] = "Model Number";
|
||||
|
||||
// Serial Number String characteristic
|
||||
static uint8_t devInfoSerialNumberProps = GATT_PROP_READ;
|
||||
static const uint8_t devInfoSerialNumber[] = "Serial Number";
|
||||
|
||||
// Firmware Revision String characteristic
|
||||
static uint8_t devInfoFirmwareRevProps = GATT_PROP_READ;
|
||||
static const uint8_t devInfoFirmwareRev[] = "Firmware Revision";
|
||||
|
||||
// Hardware Revision String characteristic
|
||||
static uint8_t devInfoHardwareRevProps = GATT_PROP_READ;
|
||||
static const uint8_t devInfoHardwareRev[] = "Hardware Revision";
|
||||
|
||||
// Software Revision String characteristic
|
||||
static uint8_t devInfoSoftwareRevProps = GATT_PROP_READ;
|
||||
static const uint8_t devInfoSoftwareRev[] = "Software Revision";
|
||||
|
||||
// Manufacturer Name String characteristic
|
||||
static uint8_t devInfoMfrNameProps = GATT_PROP_READ;
|
||||
static const uint8_t devInfoMfrName[] = "Manufacturer Name";
|
||||
|
||||
// IEEE 11073-20601 Regulatory Certification Data List characteristic
|
||||
static uint8_t devInfo11073CertProps = GATT_PROP_READ;
|
||||
static const uint8_t devInfo11073Cert[] = {
|
||||
DEVINFO_11073_BODY_EXP, // authoritative body type
|
||||
0x00, // authoritative body structure type
|
||||
// authoritative body data follows below:
|
||||
'e', 'x', 'p', 'e', 'r', 'i', 'm', 'e', 'n', 't', 'a', 'l'};
|
||||
|
||||
// System ID characteristic
|
||||
static uint8_t devInfoPnpIdProps = GATT_PROP_READ;
|
||||
static uint8_t devInfoPnpId[DEVINFO_PNP_ID_LEN] = {
|
||||
1, // Vendor ID source (1=Bluetooth SIG)
|
||||
LO_UINT16(0x07D7), HI_UINT16(0x07D7), // Vendor ID (WCH)
|
||||
LO_UINT16(0x0000), HI_UINT16(0x0000), // Product ID (vendor-specific)
|
||||
LO_UINT16(0x0110), HI_UINT16(0x0110) // Product version (JJ.M.N)
|
||||
};
|
||||
|
||||
/*********************************************************************
|
||||
* Profile Attributes - Table
|
||||
*/
|
||||
|
||||
static gattAttribute_t devInfoAttrTbl[] = {
|
||||
// Device Information Service
|
||||
{
|
||||
{ATT_BT_UUID_SIZE, primaryServiceUUID}, /* type */
|
||||
GATT_PERMIT_READ, /* permissions */
|
||||
0, /* handle */
|
||||
(uint8_t *)&devInfoService /* pValue */
|
||||
},
|
||||
|
||||
// System ID Declaration
|
||||
{
|
||||
{ATT_BT_UUID_SIZE, characterUUID},
|
||||
GATT_PERMIT_READ,
|
||||
0,
|
||||
&devInfoSystemIdProps},
|
||||
|
||||
// System ID Value
|
||||
{
|
||||
{ATT_BT_UUID_SIZE, devInfoSystemIdUUID},
|
||||
GATT_PERMIT_READ,
|
||||
0,
|
||||
(uint8_t *)devInfoSystemId},
|
||||
|
||||
// Model Number String Declaration
|
||||
{
|
||||
{ATT_BT_UUID_SIZE, characterUUID},
|
||||
GATT_PERMIT_READ,
|
||||
0,
|
||||
&devInfoModelNumberProps},
|
||||
|
||||
// Model Number Value
|
||||
{
|
||||
{ATT_BT_UUID_SIZE, devInfoModelNumberUUID},
|
||||
GATT_PERMIT_READ,
|
||||
0,
|
||||
(uint8_t *)devInfoModelNumber},
|
||||
|
||||
// Serial Number String Declaration
|
||||
{
|
||||
{ATT_BT_UUID_SIZE, characterUUID},
|
||||
GATT_PERMIT_READ,
|
||||
0,
|
||||
&devInfoSerialNumberProps},
|
||||
|
||||
// Serial Number Value
|
||||
{
|
||||
{ATT_BT_UUID_SIZE, devInfoSerialNumberUUID},
|
||||
GATT_PERMIT_READ,
|
||||
0,
|
||||
(uint8_t *)devInfoSerialNumber},
|
||||
|
||||
// Firmware Revision String Declaration
|
||||
{
|
||||
{ATT_BT_UUID_SIZE, characterUUID},
|
||||
GATT_PERMIT_READ,
|
||||
0,
|
||||
&devInfoFirmwareRevProps},
|
||||
|
||||
// Firmware Revision Value
|
||||
{
|
||||
{ATT_BT_UUID_SIZE, devInfoFirmwareRevUUID},
|
||||
GATT_PERMIT_READ,
|
||||
0,
|
||||
(uint8_t *)devInfoFirmwareRev},
|
||||
|
||||
// Hardware Revision String Declaration
|
||||
{
|
||||
{ATT_BT_UUID_SIZE, characterUUID},
|
||||
GATT_PERMIT_READ,
|
||||
0,
|
||||
&devInfoHardwareRevProps},
|
||||
|
||||
// Hardware Revision Value
|
||||
{
|
||||
{ATT_BT_UUID_SIZE, devInfoHardwareRevUUID},
|
||||
GATT_PERMIT_READ,
|
||||
0,
|
||||
(uint8_t *)devInfoHardwareRev},
|
||||
|
||||
// Software Revision String Declaration
|
||||
{
|
||||
{ATT_BT_UUID_SIZE, characterUUID},
|
||||
GATT_PERMIT_READ,
|
||||
0,
|
||||
&devInfoSoftwareRevProps},
|
||||
|
||||
// Software Revision Value
|
||||
{
|
||||
{ATT_BT_UUID_SIZE, devInfoSoftwareRevUUID},
|
||||
GATT_PERMIT_READ,
|
||||
0,
|
||||
(uint8_t *)devInfoSoftwareRev},
|
||||
|
||||
// Manufacturer Name String Declaration
|
||||
{
|
||||
{ATT_BT_UUID_SIZE, characterUUID},
|
||||
GATT_PERMIT_READ,
|
||||
0,
|
||||
&devInfoMfrNameProps},
|
||||
|
||||
// Manufacturer Name Value
|
||||
{
|
||||
{ATT_BT_UUID_SIZE, devInfoMfrNameUUID},
|
||||
GATT_PERMIT_READ,
|
||||
0,
|
||||
(uint8_t *)devInfoMfrName},
|
||||
|
||||
// IEEE 11073-20601 Regulatory Certification Data List Declaration
|
||||
{
|
||||
{ATT_BT_UUID_SIZE, characterUUID},
|
||||
GATT_PERMIT_READ,
|
||||
0,
|
||||
&devInfo11073CertProps},
|
||||
|
||||
// IEEE 11073-20601 Regulatory Certification Data List Value
|
||||
{
|
||||
{ATT_BT_UUID_SIZE, devInfo11073CertUUID},
|
||||
GATT_PERMIT_READ,
|
||||
0,
|
||||
(uint8_t *)devInfo11073Cert},
|
||||
|
||||
// PnP ID Declaration
|
||||
{
|
||||
{ATT_BT_UUID_SIZE, characterUUID},
|
||||
GATT_PERMIT_READ,
|
||||
0,
|
||||
&devInfoPnpIdProps},
|
||||
|
||||
// PnP ID Value
|
||||
{
|
||||
{ATT_BT_UUID_SIZE, devInfoPnpIdUUID},
|
||||
GATT_PERMIT_READ,
|
||||
0,
|
||||
(uint8_t *)devInfoPnpId}
|
||||
};
|
||||
|
||||
/*********************************************************************
|
||||
* LOCAL FUNCTIONS
|
||||
*/
|
||||
static bStatus_t devInfo_ReadAttrCB(uint16_t connHandle, gattAttribute_t *pAttr,
|
||||
uint8_t *pValue, uint16_t *pLen, uint16_t offset, uint16_t maxLen, uint8_t method);
|
||||
|
||||
/*********************************************************************
|
||||
* PROFILE CALLBACKS
|
||||
*/
|
||||
// Device Info Service Callbacks
|
||||
gattServiceCBs_t devInfoCBs = {
|
||||
devInfo_ReadAttrCB, // Read callback function pointer
|
||||
NULL, // Write callback function pointer
|
||||
NULL // Authorization callback function pointer
|
||||
};
|
||||
|
||||
/*********************************************************************
|
||||
* NETWORK LAYER CALLBACKS
|
||||
*/
|
||||
|
||||
/*********************************************************************
|
||||
* PUBLIC FUNCTIONS
|
||||
*/
|
||||
|
||||
/*********************************************************************
|
||||
* @fn DevInfo_AddService
|
||||
*
|
||||
* @brief Initializes the Device Information service by registering
|
||||
* GATT attributes with the GATT server.
|
||||
*
|
||||
* @return Success or Failure
|
||||
*/
|
||||
bStatus_t DevInfo_AddService(void)
|
||||
{
|
||||
// Register GATT attribute list and CBs with GATT Server App
|
||||
return GATTServApp_RegisterService(devInfoAttrTbl,
|
||||
GATT_NUM_ATTRS(devInfoAttrTbl),
|
||||
GATT_MAX_ENCRYPT_KEY_SIZE,
|
||||
&devInfoCBs);
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn DevInfo_SetParameter
|
||||
*
|
||||
* @brief Set a Device Information parameter.
|
||||
*
|
||||
* @param param - Profile parameter ID
|
||||
* @param len - length of data to write
|
||||
* @param value - pointer to data to write. This is dependent on
|
||||
* the parameter ID and WILL be cast to the appropriate
|
||||
* data type (example: data type of uint16_t will be cast to
|
||||
* uint16_t pointer).
|
||||
*
|
||||
* @return bStatus_t
|
||||
*/
|
||||
bStatus_t DevInfo_SetParameter(uint8_t param, uint16_t len, void *value)
|
||||
{
|
||||
bStatus_t ret = SUCCESS;
|
||||
|
||||
switch(param)
|
||||
{
|
||||
case DEVINFO_SYSTEM_ID:
|
||||
tmos_memcpy(devInfoSystemId, value, len);
|
||||
break;
|
||||
|
||||
default:
|
||||
ret = INVALIDPARAMETER;
|
||||
break;
|
||||
}
|
||||
|
||||
return (ret);
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn DevInfo_GetParameter
|
||||
*
|
||||
* @brief Get a Device Information parameter.
|
||||
*
|
||||
* @param param - Profile parameter ID
|
||||
* @param value - pointer to data to get. This is dependent on
|
||||
* the parameter ID and WILL be cast to the appropriate
|
||||
* data type (example: data type of uint16_t will be cast to
|
||||
* uint16_t pointer).
|
||||
*
|
||||
* @return bStatus_t
|
||||
*/
|
||||
bStatus_t DevInfo_GetParameter(uint8_t param, void *value)
|
||||
{
|
||||
bStatus_t ret = SUCCESS;
|
||||
|
||||
switch(param)
|
||||
{
|
||||
case DEVINFO_SYSTEM_ID:
|
||||
tmos_memcpy(value, devInfoSystemId, sizeof(devInfoSystemId));
|
||||
break;
|
||||
|
||||
case DEVINFO_MODEL_NUMBER:
|
||||
tmos_memcpy(value, devInfoModelNumber, sizeof(devInfoModelNumber));
|
||||
break;
|
||||
case DEVINFO_SERIAL_NUMBER:
|
||||
tmos_memcpy(value, devInfoSerialNumber, sizeof(devInfoSerialNumber));
|
||||
break;
|
||||
|
||||
case DEVINFO_FIRMWARE_REV:
|
||||
tmos_memcpy(value, devInfoFirmwareRev, sizeof(devInfoFirmwareRev));
|
||||
break;
|
||||
|
||||
case DEVINFO_HARDWARE_REV:
|
||||
tmos_memcpy(value, devInfoHardwareRev, sizeof(devInfoHardwareRev));
|
||||
break;
|
||||
|
||||
case DEVINFO_SOFTWARE_REV:
|
||||
tmos_memcpy(value, devInfoSoftwareRev, sizeof(devInfoSoftwareRev));
|
||||
break;
|
||||
|
||||
case DEVINFO_MANUFACTURER_NAME:
|
||||
tmos_memcpy(value, devInfoMfrName, sizeof(devInfoMfrName));
|
||||
break;
|
||||
|
||||
case DEVINFO_11073_CERT_DATA:
|
||||
tmos_memcpy(value, devInfo11073Cert, sizeof(devInfo11073Cert));
|
||||
break;
|
||||
|
||||
case DEVINFO_PNP_ID:
|
||||
tmos_memcpy(value, devInfoPnpId, sizeof(devInfoPnpId));
|
||||
break;
|
||||
|
||||
default:
|
||||
ret = INVALIDPARAMETER;
|
||||
break;
|
||||
}
|
||||
|
||||
return (ret);
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn devInfo_ReadAttrCB
|
||||
*
|
||||
* @brief Read an attribute.
|
||||
*
|
||||
* @param connHandle - connection message was received on
|
||||
* @param pAttr - pointer to attribute
|
||||
* @param pValue - pointer to data to be read
|
||||
* @param pLen - length of data to be read
|
||||
* @param offset - offset of the first octet to be read
|
||||
* @param maxLen - maximum length of data to be read
|
||||
*
|
||||
* @return Success or Failure
|
||||
*/
|
||||
static bStatus_t devInfo_ReadAttrCB(uint16_t connHandle, gattAttribute_t *pAttr,
|
||||
uint8_t *pValue, uint16_t *pLen, uint16_t offset, uint16_t maxLen, uint8_t method)
|
||||
{
|
||||
bStatus_t status = SUCCESS;
|
||||
uint16_t uuid = BUILD_UINT16(pAttr->type.uuid[0], pAttr->type.uuid[1]);
|
||||
|
||||
switch(uuid)
|
||||
{
|
||||
case SYSTEM_ID_UUID:
|
||||
// verify offset
|
||||
if(offset >= sizeof(devInfoSystemId))
|
||||
{
|
||||
status = ATT_ERR_INVALID_OFFSET;
|
||||
}
|
||||
else
|
||||
{
|
||||
// determine read length
|
||||
*pLen = MIN(maxLen, (sizeof(devInfoSystemId) - offset));
|
||||
|
||||
// copy data
|
||||
tmos_memcpy(pValue, &devInfoSystemId[offset], *pLen);
|
||||
}
|
||||
break;
|
||||
|
||||
case MODEL_NUMBER_UUID:
|
||||
// verify offset
|
||||
if(offset >= (sizeof(devInfoModelNumber) - 1))
|
||||
{
|
||||
status = ATT_ERR_INVALID_OFFSET;
|
||||
}
|
||||
else
|
||||
{
|
||||
// determine read length (exclude null terminating character)
|
||||
*pLen = MIN(maxLen, ((sizeof(devInfoModelNumber) - 1) - offset));
|
||||
|
||||
// copy data
|
||||
tmos_memcpy(pValue, &devInfoModelNumber[offset], *pLen);
|
||||
}
|
||||
break;
|
||||
|
||||
case SERIAL_NUMBER_UUID:
|
||||
// verify offset
|
||||
if(offset >= (sizeof(devInfoSerialNumber) - 1))
|
||||
{
|
||||
status = ATT_ERR_INVALID_OFFSET;
|
||||
}
|
||||
else
|
||||
{
|
||||
// determine read length (exclude null terminating character)
|
||||
*pLen = MIN(maxLen, ((sizeof(devInfoSerialNumber) - 1) - offset));
|
||||
|
||||
// copy data
|
||||
tmos_memcpy(pValue, &devInfoSerialNumber[offset], *pLen);
|
||||
}
|
||||
break;
|
||||
|
||||
case FIRMWARE_REV_UUID:
|
||||
// verify offset
|
||||
if(offset >= (sizeof(devInfoFirmwareRev) - 1))
|
||||
{
|
||||
status = ATT_ERR_INVALID_OFFSET;
|
||||
}
|
||||
else
|
||||
{
|
||||
// determine read length (exclude null terminating character)
|
||||
*pLen = MIN(maxLen, ((sizeof(devInfoFirmwareRev) - 1) - offset));
|
||||
|
||||
// copy data
|
||||
tmos_memcpy(pValue, &devInfoFirmwareRev[offset], *pLen);
|
||||
}
|
||||
break;
|
||||
|
||||
case HARDWARE_REV_UUID:
|
||||
// verify offset
|
||||
if(offset >= (sizeof(devInfoHardwareRev) - 1))
|
||||
{
|
||||
status = ATT_ERR_INVALID_OFFSET;
|
||||
}
|
||||
else
|
||||
{
|
||||
// determine read length (exclude null terminating character)
|
||||
*pLen = MIN(maxLen, ((sizeof(devInfoHardwareRev) - 1) - offset));
|
||||
|
||||
// copy data
|
||||
tmos_memcpy(pValue, &devInfoHardwareRev[offset], *pLen);
|
||||
}
|
||||
break;
|
||||
|
||||
case SOFTWARE_REV_UUID:
|
||||
// verify offset
|
||||
if(offset >= (sizeof(devInfoSoftwareRev) - 1))
|
||||
{
|
||||
status = ATT_ERR_INVALID_OFFSET;
|
||||
}
|
||||
else
|
||||
{
|
||||
// determine read length (exclude null terminating character)
|
||||
*pLen = MIN(maxLen, ((sizeof(devInfoSoftwareRev) - 1) - offset));
|
||||
|
||||
// copy data
|
||||
tmos_memcpy(pValue, &devInfoSoftwareRev[offset], *pLen);
|
||||
}
|
||||
break;
|
||||
|
||||
case MANUFACTURER_NAME_UUID:
|
||||
// verify offset
|
||||
if(offset >= (sizeof(devInfoMfrName) - 1))
|
||||
{
|
||||
status = ATT_ERR_INVALID_OFFSET;
|
||||
}
|
||||
else
|
||||
{
|
||||
// determine read length (exclude null terminating character)
|
||||
*pLen = MIN(maxLen, ((sizeof(devInfoMfrName) - 1) - offset));
|
||||
|
||||
// copy data
|
||||
tmos_memcpy(pValue, &devInfoMfrName[offset], *pLen);
|
||||
}
|
||||
break;
|
||||
|
||||
case IEEE_11073_CERT_DATA_UUID:
|
||||
// verify offset
|
||||
if(offset >= sizeof(devInfo11073Cert))
|
||||
{
|
||||
status = ATT_ERR_INVALID_OFFSET;
|
||||
}
|
||||
else
|
||||
{
|
||||
// determine read length
|
||||
*pLen = MIN(maxLen, (sizeof(devInfo11073Cert) - offset));
|
||||
|
||||
// copy data
|
||||
tmos_memcpy(pValue, &devInfo11073Cert[offset], *pLen);
|
||||
}
|
||||
break;
|
||||
|
||||
case PNP_ID_UUID:
|
||||
// verify offset
|
||||
if(offset >= sizeof(devInfoPnpId))
|
||||
{
|
||||
status = ATT_ERR_INVALID_OFFSET;
|
||||
}
|
||||
else
|
||||
{
|
||||
// determine read length
|
||||
*pLen = MIN(maxLen, (sizeof(devInfoPnpId) - offset));
|
||||
|
||||
// copy data
|
||||
tmos_memcpy(pValue, &devInfoPnpId[offset], *pLen);
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
*pLen = 0;
|
||||
status = ATT_ERR_ATTR_NOT_FOUND;
|
||||
break;
|
||||
}
|
||||
|
||||
return (status);
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
*********************************************************************/
|
||||
731
vd960DBN/BLE/OnlyUpdateApp_Peripheral/Profile/gattprofile.c
Normal file
731
vd960DBN/BLE/OnlyUpdateApp_Peripheral/Profile/gattprofile.c
Normal file
@@ -0,0 +1,731 @@
|
||||
/********************************** (C) COPYRIGHT *******************************
|
||||
* File Name : gattprofile.C
|
||||
* Author : WCH
|
||||
* Version : V1.0
|
||||
* Date : 2018/12/10
|
||||
* Description : Customize services with five different attributes,
|
||||
* including readable, writable, notification,
|
||||
* readable and writable, and safe readable
|
||||
*********************************************************************************
|
||||
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
|
||||
* Attention: This software (modified or not) and binary are used for
|
||||
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
|
||||
*******************************************************************************/
|
||||
|
||||
/*********************************************************************
|
||||
* INCLUDES
|
||||
*/
|
||||
#include "CONFIG.h"
|
||||
#include "gattprofile.h"
|
||||
|
||||
/*********************************************************************
|
||||
* MACROS
|
||||
*/
|
||||
|
||||
/*********************************************************************
|
||||
* CONSTANTS
|
||||
*/
|
||||
|
||||
// Position of simpleProfilechar4 value in attribute array
|
||||
#define SIMPLEPROFILE_CHAR4_VALUE_POS 11
|
||||
|
||||
/*********************************************************************
|
||||
* TYPEDEFS
|
||||
*/
|
||||
|
||||
/*********************************************************************
|
||||
* GLOBAL VARIABLES
|
||||
*/
|
||||
// Simple GATT Profile Service UUID: 0xFFF0
|
||||
const uint8_t simpleProfileServUUID[ATT_BT_UUID_SIZE] = {
|
||||
LO_UINT16(SIMPLEPROFILE_SERV_UUID), HI_UINT16(SIMPLEPROFILE_SERV_UUID)};
|
||||
|
||||
// Characteristic 1 UUID: 0xFFF1
|
||||
const uint8_t simpleProfilechar1UUID[ATT_BT_UUID_SIZE] = {
|
||||
LO_UINT16(SIMPLEPROFILE_CHAR1_UUID), HI_UINT16(SIMPLEPROFILE_CHAR1_UUID)};
|
||||
|
||||
// Characteristic 2 UUID: 0xFFF2
|
||||
const uint8_t simpleProfilechar2UUID[ATT_BT_UUID_SIZE] = {
|
||||
LO_UINT16(SIMPLEPROFILE_CHAR2_UUID), HI_UINT16(SIMPLEPROFILE_CHAR2_UUID)};
|
||||
|
||||
// Characteristic 3 UUID: 0xFFF3
|
||||
const uint8_t simpleProfilechar3UUID[ATT_BT_UUID_SIZE] = {
|
||||
LO_UINT16(SIMPLEPROFILE_CHAR3_UUID), HI_UINT16(SIMPLEPROFILE_CHAR3_UUID)};
|
||||
|
||||
// Characteristic 4 UUID: 0xFFF4
|
||||
const uint8_t simpleProfilechar4UUID[ATT_BT_UUID_SIZE] = {
|
||||
LO_UINT16(SIMPLEPROFILE_CHAR4_UUID), HI_UINT16(SIMPLEPROFILE_CHAR4_UUID)};
|
||||
|
||||
// Characteristic 5 UUID: 0xFFF5
|
||||
const uint8_t simpleProfilechar5UUID[ATT_BT_UUID_SIZE] = {
|
||||
LO_UINT16(SIMPLEPROFILE_CHAR5_UUID), HI_UINT16(SIMPLEPROFILE_CHAR5_UUID)};
|
||||
|
||||
/*********************************************************************
|
||||
* EXTERNAL VARIABLES
|
||||
*/
|
||||
|
||||
/*********************************************************************
|
||||
* EXTERNAL FUNCTIONS
|
||||
*/
|
||||
|
||||
/*********************************************************************
|
||||
* LOCAL VARIABLES
|
||||
*/
|
||||
|
||||
static simpleProfileCBs_t *simpleProfile_AppCBs = NULL;
|
||||
|
||||
/*********************************************************************
|
||||
* Profile Attributes - variables
|
||||
*/
|
||||
|
||||
// Simple Profile Service attribute
|
||||
static const gattAttrType_t simpleProfileService = {ATT_BT_UUID_SIZE, simpleProfileServUUID};
|
||||
|
||||
// Simple Profile Characteristic 1 Properties
|
||||
static uint8_t simpleProfileChar1Props = GATT_PROP_READ | GATT_PROP_WRITE;
|
||||
|
||||
// Characteristic 1 Value
|
||||
static uint8_t simpleProfileChar1[SIMPLEPROFILE_CHAR1_LEN] = {0};
|
||||
|
||||
// Simple Profile Characteristic 1 User Description
|
||||
static uint8_t simpleProfileChar1UserDesp[] = "Characteristic 1\0";
|
||||
|
||||
// Simple Profile Characteristic 2 Properties
|
||||
static uint8_t simpleProfileChar2Props = GATT_PROP_READ;
|
||||
|
||||
// Characteristic 2 Value
|
||||
static uint8_t simpleProfileChar2[SIMPLEPROFILE_CHAR2_LEN] = {0};
|
||||
|
||||
// Simple Profile Characteristic 2 User Description
|
||||
static uint8_t simpleProfileChar2UserDesp[] = "Characteristic 2\0";
|
||||
|
||||
// Simple Profile Characteristic 3 Properties
|
||||
static uint8_t simpleProfileChar3Props = GATT_PROP_WRITE;
|
||||
|
||||
// Characteristic 3 Value
|
||||
static uint8_t simpleProfileChar3[SIMPLEPROFILE_CHAR3_LEN] = {0};
|
||||
|
||||
// Simple Profile Characteristic 3 User Description
|
||||
static uint8_t simpleProfileChar3UserDesp[] = "Characteristic 3\0";
|
||||
|
||||
// Simple Profile Characteristic 4 Properties
|
||||
static uint8_t simpleProfileChar4Props = GATT_PROP_NOTIFY;
|
||||
|
||||
// Characteristic 4 Value
|
||||
static uint8_t simpleProfileChar4[SIMPLEPROFILE_CHAR4_LEN] = {0};
|
||||
|
||||
// Simple Profile Characteristic 4 Configuration Each client has its own
|
||||
// instantiation of the Client Characteristic Configuration. Reads of the
|
||||
// Client Characteristic Configuration only shows the configuration for
|
||||
// that client and writes only affect the configuration of that client.
|
||||
static gattCharCfg_t simpleProfileChar4Config[PERIPHERAL_MAX_CONNECTION];
|
||||
|
||||
// Simple Profile Characteristic 4 User Description
|
||||
static uint8_t simpleProfileChar4UserDesp[] = "Characteristic 4\0";
|
||||
|
||||
// Simple Profile Characteristic 5 Properties
|
||||
static uint8_t simpleProfileChar5Props = GATT_PROP_READ;
|
||||
|
||||
// Characteristic 5 Value
|
||||
static uint8_t simpleProfileChar5[SIMPLEPROFILE_CHAR5_LEN] = {0};
|
||||
|
||||
// Simple Profile Characteristic 5 User Description
|
||||
static uint8_t simpleProfileChar5UserDesp[] = "Characteristic 5\0";
|
||||
|
||||
/*********************************************************************
|
||||
* Profile Attributes - Table
|
||||
*/
|
||||
|
||||
static gattAttribute_t simpleProfileAttrTbl[] = {
|
||||
// Simple Profile Service
|
||||
{
|
||||
{ATT_BT_UUID_SIZE, primaryServiceUUID}, /* type */
|
||||
GATT_PERMIT_READ, /* permissions */
|
||||
0, /* handle */
|
||||
(uint8_t *)&simpleProfileService /* pValue */
|
||||
},
|
||||
|
||||
// Characteristic 1 Declaration
|
||||
{
|
||||
{ATT_BT_UUID_SIZE, characterUUID},
|
||||
GATT_PERMIT_READ,
|
||||
0,
|
||||
&simpleProfileChar1Props},
|
||||
|
||||
// Characteristic Value 1
|
||||
{
|
||||
{ATT_BT_UUID_SIZE, simpleProfilechar1UUID},
|
||||
GATT_PERMIT_READ | GATT_PERMIT_WRITE,
|
||||
0,
|
||||
simpleProfileChar1},
|
||||
|
||||
// Characteristic 1 User Description
|
||||
{
|
||||
{ATT_BT_UUID_SIZE, charUserDescUUID},
|
||||
GATT_PERMIT_READ,
|
||||
0,
|
||||
simpleProfileChar1UserDesp},
|
||||
|
||||
// Characteristic 2 Declaration
|
||||
{
|
||||
{ATT_BT_UUID_SIZE, characterUUID},
|
||||
GATT_PERMIT_READ,
|
||||
0,
|
||||
&simpleProfileChar2Props},
|
||||
|
||||
// Characteristic Value 2
|
||||
{
|
||||
{ATT_BT_UUID_SIZE, simpleProfilechar2UUID},
|
||||
GATT_PERMIT_READ,
|
||||
0,
|
||||
simpleProfileChar2},
|
||||
|
||||
// Characteristic 2 User Description
|
||||
{
|
||||
{ATT_BT_UUID_SIZE, charUserDescUUID},
|
||||
GATT_PERMIT_READ,
|
||||
0,
|
||||
simpleProfileChar2UserDesp},
|
||||
|
||||
// Characteristic 3 Declaration
|
||||
{
|
||||
{ATT_BT_UUID_SIZE, characterUUID},
|
||||
GATT_PERMIT_READ,
|
||||
0,
|
||||
&simpleProfileChar3Props},
|
||||
|
||||
// Characteristic Value 3
|
||||
{
|
||||
{ATT_BT_UUID_SIZE, simpleProfilechar3UUID},
|
||||
GATT_PERMIT_WRITE,
|
||||
0,
|
||||
simpleProfileChar3},
|
||||
|
||||
// Characteristic 3 User Description
|
||||
{
|
||||
{ATT_BT_UUID_SIZE, charUserDescUUID},
|
||||
GATT_PERMIT_READ,
|
||||
0,
|
||||
simpleProfileChar3UserDesp},
|
||||
|
||||
// Characteristic 4 Declaration
|
||||
{
|
||||
{ATT_BT_UUID_SIZE, characterUUID},
|
||||
GATT_PERMIT_READ,
|
||||
0,
|
||||
&simpleProfileChar4Props},
|
||||
|
||||
// Characteristic Value 4
|
||||
{
|
||||
{ATT_BT_UUID_SIZE, simpleProfilechar4UUID},
|
||||
0,
|
||||
0,
|
||||
simpleProfileChar4},
|
||||
|
||||
// Characteristic 4 configuration
|
||||
{
|
||||
{ATT_BT_UUID_SIZE, clientCharCfgUUID},
|
||||
GATT_PERMIT_READ | GATT_PERMIT_WRITE,
|
||||
0,
|
||||
(uint8_t *)simpleProfileChar4Config},
|
||||
|
||||
// Characteristic 4 User Description
|
||||
{
|
||||
{ATT_BT_UUID_SIZE, charUserDescUUID},
|
||||
GATT_PERMIT_READ,
|
||||
0,
|
||||
simpleProfileChar4UserDesp},
|
||||
|
||||
// Characteristic 5 Declaration
|
||||
{
|
||||
{ATT_BT_UUID_SIZE, characterUUID},
|
||||
GATT_PERMIT_READ,
|
||||
0,
|
||||
&simpleProfileChar5Props},
|
||||
|
||||
// Characteristic Value 5
|
||||
{
|
||||
{ATT_BT_UUID_SIZE, simpleProfilechar5UUID},
|
||||
GATT_PERMIT_AUTHEN_READ,
|
||||
0,
|
||||
simpleProfileChar5},
|
||||
|
||||
// Characteristic 5 User Description
|
||||
{
|
||||
{ATT_BT_UUID_SIZE, charUserDescUUID},
|
||||
GATT_PERMIT_READ,
|
||||
0,
|
||||
simpleProfileChar5UserDesp},
|
||||
};
|
||||
|
||||
/*********************************************************************
|
||||
* LOCAL FUNCTIONS
|
||||
*/
|
||||
static bStatus_t simpleProfile_ReadAttrCB(uint16_t connHandle, gattAttribute_t *pAttr,
|
||||
uint8_t *pValue, uint16_t *pLen, uint16_t offset, uint16_t maxLen, uint8_t method);
|
||||
static bStatus_t simpleProfile_WriteAttrCB(uint16_t connHandle, gattAttribute_t *pAttr,
|
||||
uint8_t *pValue, uint16_t len, uint16_t offset, uint8_t method);
|
||||
|
||||
static void simpleProfile_HandleConnStatusCB(uint16_t connHandle, uint8_t changeType);
|
||||
|
||||
/*********************************************************************
|
||||
* PROFILE CALLBACKS
|
||||
*/
|
||||
// Simple Profile Service Callbacks
|
||||
gattServiceCBs_t simpleProfileCBs = {
|
||||
simpleProfile_ReadAttrCB, // Read callback function pointer
|
||||
simpleProfile_WriteAttrCB, // Write callback function pointer
|
||||
NULL // Authorization callback function pointer
|
||||
};
|
||||
|
||||
/*********************************************************************
|
||||
* PUBLIC FUNCTIONS
|
||||
*/
|
||||
|
||||
/*********************************************************************
|
||||
* @fn SimpleProfile_AddService
|
||||
*
|
||||
* @brief Initializes the Simple Profile service by registering
|
||||
* GATT attributes with the GATT server.
|
||||
*
|
||||
* @param services - services to add. This is a bit map and can
|
||||
* contain more than one service.
|
||||
*
|
||||
* @return Success or Failure
|
||||
*/
|
||||
bStatus_t SimpleProfile_AddService(uint32_t services)
|
||||
{
|
||||
uint8_t status = SUCCESS;
|
||||
|
||||
// Initialize Client Characteristic Configuration attributes
|
||||
GATTServApp_InitCharCfg(INVALID_CONNHANDLE, simpleProfileChar4Config);
|
||||
|
||||
// Register with Link DB to receive link status change callback
|
||||
linkDB_Register(simpleProfile_HandleConnStatusCB);
|
||||
|
||||
if(services & SIMPLEPROFILE_SERVICE)
|
||||
{
|
||||
// Register GATT attribute list and CBs with GATT Server App
|
||||
status = GATTServApp_RegisterService(simpleProfileAttrTbl,
|
||||
GATT_NUM_ATTRS(simpleProfileAttrTbl),
|
||||
GATT_MAX_ENCRYPT_KEY_SIZE,
|
||||
&simpleProfileCBs);
|
||||
}
|
||||
|
||||
return (status);
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn SimpleProfile_RegisterAppCBs
|
||||
*
|
||||
* @brief Registers the application callback function. Only call
|
||||
* this function once.
|
||||
*
|
||||
* @param callbacks - pointer to application callbacks.
|
||||
*
|
||||
* @return SUCCESS or bleAlreadyInRequestedMode
|
||||
*/
|
||||
bStatus_t SimpleProfile_RegisterAppCBs(simpleProfileCBs_t *appCallbacks)
|
||||
{
|
||||
if(appCallbacks)
|
||||
{
|
||||
simpleProfile_AppCBs = appCallbacks;
|
||||
|
||||
return (SUCCESS);
|
||||
}
|
||||
else
|
||||
{
|
||||
return (bleAlreadyInRequestedMode);
|
||||
}
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn SimpleProfile_SetParameter
|
||||
*
|
||||
* @brief Set a Simple Profile parameter.
|
||||
*
|
||||
* @param param - Profile parameter ID
|
||||
* @param len - length of data to right
|
||||
* @param value - pointer to data to write. This is dependent on
|
||||
* the parameter ID and WILL be cast to the appropriate
|
||||
* data type (example: data type of uint16_t will be cast to
|
||||
* uint16_t pointer).
|
||||
*
|
||||
* @return bStatus_t
|
||||
*/
|
||||
bStatus_t SimpleProfile_SetParameter(uint8_t param, uint16_t len, void *value)
|
||||
{
|
||||
bStatus_t ret = SUCCESS;
|
||||
switch(param)
|
||||
{
|
||||
case SIMPLEPROFILE_CHAR1:
|
||||
if(len == SIMPLEPROFILE_CHAR1_LEN)
|
||||
{
|
||||
tmos_memcpy(simpleProfileChar1, value, SIMPLEPROFILE_CHAR1_LEN);
|
||||
}
|
||||
else
|
||||
{
|
||||
ret = bleInvalidRange;
|
||||
}
|
||||
break;
|
||||
|
||||
case SIMPLEPROFILE_CHAR2:
|
||||
if(len == SIMPLEPROFILE_CHAR2_LEN)
|
||||
{
|
||||
tmos_memcpy(simpleProfileChar2, value, SIMPLEPROFILE_CHAR2_LEN);
|
||||
}
|
||||
else
|
||||
{
|
||||
ret = bleInvalidRange;
|
||||
}
|
||||
break;
|
||||
|
||||
case SIMPLEPROFILE_CHAR3:
|
||||
if(len == SIMPLEPROFILE_CHAR3_LEN)
|
||||
{
|
||||
tmos_memcpy(simpleProfileChar3, value, SIMPLEPROFILE_CHAR3_LEN);
|
||||
}
|
||||
else
|
||||
{
|
||||
ret = bleInvalidRange;
|
||||
}
|
||||
break;
|
||||
|
||||
case SIMPLEPROFILE_CHAR4:
|
||||
if(len == SIMPLEPROFILE_CHAR4_LEN)
|
||||
{
|
||||
tmos_memcpy(simpleProfileChar4, value, SIMPLEPROFILE_CHAR4_LEN);
|
||||
}
|
||||
else
|
||||
{
|
||||
ret = bleInvalidRange;
|
||||
}
|
||||
break;
|
||||
|
||||
case SIMPLEPROFILE_CHAR5:
|
||||
if(len == SIMPLEPROFILE_CHAR5_LEN)
|
||||
{
|
||||
tmos_memcpy(simpleProfileChar5, value, SIMPLEPROFILE_CHAR5_LEN);
|
||||
}
|
||||
else
|
||||
{
|
||||
ret = bleInvalidRange;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
ret = INVALIDPARAMETER;
|
||||
break;
|
||||
}
|
||||
|
||||
return (ret);
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn SimpleProfile_GetParameter
|
||||
*
|
||||
* @brief Get a Simple Profile parameter.
|
||||
*
|
||||
* @param param - Profile parameter ID
|
||||
* @param value - pointer to data to put. This is dependent on
|
||||
* the parameter ID and WILL be cast to the appropriate
|
||||
* data type (example: data type of uint16_t will be cast to
|
||||
* uint16_t pointer).
|
||||
*
|
||||
* @return bStatus_t
|
||||
*/
|
||||
bStatus_t SimpleProfile_GetParameter(uint8_t param, void *value)
|
||||
{
|
||||
bStatus_t ret = SUCCESS;
|
||||
switch(param)
|
||||
{
|
||||
case SIMPLEPROFILE_CHAR1:
|
||||
tmos_memcpy(value, simpleProfileChar1, SIMPLEPROFILE_CHAR1_LEN);
|
||||
break;
|
||||
|
||||
case SIMPLEPROFILE_CHAR2:
|
||||
tmos_memcpy(value, simpleProfileChar2, SIMPLEPROFILE_CHAR2_LEN);
|
||||
break;
|
||||
|
||||
case SIMPLEPROFILE_CHAR3:
|
||||
tmos_memcpy(value, simpleProfileChar3, SIMPLEPROFILE_CHAR3_LEN);
|
||||
break;
|
||||
|
||||
case SIMPLEPROFILE_CHAR4:
|
||||
tmos_memcpy(value, simpleProfileChar4, SIMPLEPROFILE_CHAR4_LEN);
|
||||
break;
|
||||
|
||||
case SIMPLEPROFILE_CHAR5:
|
||||
tmos_memcpy(value, simpleProfileChar5, SIMPLEPROFILE_CHAR5_LEN);
|
||||
break;
|
||||
|
||||
default:
|
||||
ret = INVALIDPARAMETER;
|
||||
break;
|
||||
}
|
||||
|
||||
return (ret);
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn simpleProfile_Notify
|
||||
*
|
||||
* @brief Send a notification containing a heart rate
|
||||
* measurement.
|
||||
*
|
||||
* @param connHandle - connection handle
|
||||
* @param pNoti - pointer to notification structure
|
||||
*
|
||||
* @return Success or Failure
|
||||
*/
|
||||
bStatus_t simpleProfile_Notify(uint16_t connHandle, attHandleValueNoti_t *pNoti)
|
||||
{
|
||||
uint16_t value = GATTServApp_ReadCharCfg(connHandle, simpleProfileChar4Config);
|
||||
|
||||
// If notifications enabled
|
||||
if(value & GATT_CLIENT_CFG_NOTIFY)
|
||||
{
|
||||
// Set the handle
|
||||
pNoti->handle = simpleProfileAttrTbl[SIMPLEPROFILE_CHAR4_VALUE_POS].handle;
|
||||
|
||||
// Send the notification
|
||||
return GATT_Notification(connHandle, pNoti, FALSE);
|
||||
}
|
||||
return bleIncorrectMode;
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn simpleProfile_ReadAttrCB
|
||||
*
|
||||
* @brief Read an attribute.
|
||||
*
|
||||
* @param connHandle - connection message was received on
|
||||
* @param pAttr - pointer to attribute
|
||||
* @param pValue - pointer to data to be read
|
||||
* @param pLen - length of data to be read
|
||||
* @param offset - offset of the first octet to be read
|
||||
* @param maxLen - maximum length of data to be read
|
||||
*
|
||||
* @return Success or Failure
|
||||
*/
|
||||
static bStatus_t simpleProfile_ReadAttrCB(uint16_t connHandle, gattAttribute_t *pAttr,
|
||||
uint8_t *pValue, uint16_t *pLen, uint16_t offset, uint16_t maxLen, uint8_t method)
|
||||
{
|
||||
bStatus_t status = SUCCESS;
|
||||
|
||||
// Make sure it's not a blob operation (no attributes in the profile are long)
|
||||
if(offset > 0)
|
||||
{
|
||||
return (ATT_ERR_ATTR_NOT_LONG);
|
||||
}
|
||||
|
||||
if(pAttr->type.len == ATT_BT_UUID_SIZE)
|
||||
{
|
||||
// 16-bit UUID
|
||||
uint16_t uuid = BUILD_UINT16(pAttr->type.uuid[0], pAttr->type.uuid[1]);
|
||||
switch(uuid)
|
||||
{
|
||||
// No need for "GATT_SERVICE_UUID" or "GATT_CLIENT_CHAR_CFG_UUID" cases;
|
||||
// gattserverapp handles those reads
|
||||
|
||||
// characteristics 1 and 2 have read permissions
|
||||
// characteritisc 3 does not have read permissions; therefore it is not
|
||||
// included here
|
||||
// characteristic 4 does not have read permissions, but because it
|
||||
// can be sent as a notification, it is included here
|
||||
case SIMPLEPROFILE_CHAR1_UUID:
|
||||
if(maxLen > SIMPLEPROFILE_CHAR1_LEN)
|
||||
{
|
||||
*pLen = SIMPLEPROFILE_CHAR1_LEN;
|
||||
}
|
||||
else
|
||||
{
|
||||
*pLen = maxLen;
|
||||
}
|
||||
tmos_memcpy(pValue, pAttr->pValue, *pLen);
|
||||
break;
|
||||
|
||||
case SIMPLEPROFILE_CHAR2_UUID:
|
||||
if(maxLen > SIMPLEPROFILE_CHAR2_LEN)
|
||||
{
|
||||
*pLen = SIMPLEPROFILE_CHAR2_LEN;
|
||||
}
|
||||
else
|
||||
{
|
||||
*pLen = maxLen;
|
||||
}
|
||||
tmos_memcpy(pValue, pAttr->pValue, *pLen);
|
||||
break;
|
||||
|
||||
case SIMPLEPROFILE_CHAR4_UUID:
|
||||
if(maxLen > SIMPLEPROFILE_CHAR4_LEN)
|
||||
{
|
||||
*pLen = SIMPLEPROFILE_CHAR4_LEN;
|
||||
}
|
||||
else
|
||||
{
|
||||
*pLen = maxLen;
|
||||
}
|
||||
tmos_memcpy(pValue, pAttr->pValue, *pLen);
|
||||
break;
|
||||
|
||||
case SIMPLEPROFILE_CHAR5_UUID:
|
||||
if(maxLen > SIMPLEPROFILE_CHAR5_LEN)
|
||||
{
|
||||
*pLen = SIMPLEPROFILE_CHAR5_LEN;
|
||||
}
|
||||
else
|
||||
{
|
||||
*pLen = maxLen;
|
||||
}
|
||||
tmos_memcpy(pValue, pAttr->pValue, *pLen);
|
||||
break;
|
||||
|
||||
default:
|
||||
// Should never get here! (characteristics 3 and 4 do not have read permissions)
|
||||
*pLen = 0;
|
||||
status = ATT_ERR_ATTR_NOT_FOUND;
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// 128-bit UUID
|
||||
*pLen = 0;
|
||||
status = ATT_ERR_INVALID_HANDLE;
|
||||
}
|
||||
|
||||
return (status);
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn simpleProfile_WriteAttrCB
|
||||
*
|
||||
* @brief Validate attribute data prior to a write operation
|
||||
*
|
||||
* @param connHandle - connection message was received on
|
||||
* @param pAttr - pointer to attribute
|
||||
* @param pValue - pointer to data to be written
|
||||
* @param len - length of data
|
||||
* @param offset - offset of the first octet to be written
|
||||
*
|
||||
* @return Success or Failure
|
||||
*/
|
||||
static bStatus_t simpleProfile_WriteAttrCB(uint16_t connHandle, gattAttribute_t *pAttr,
|
||||
uint8_t *pValue, uint16_t len, uint16_t offset, uint8_t method)
|
||||
{
|
||||
bStatus_t status = SUCCESS;
|
||||
uint8_t notifyApp = 0xFF;
|
||||
|
||||
// If attribute permissions require authorization to write, return error
|
||||
if(gattPermitAuthorWrite(pAttr->permissions))
|
||||
{
|
||||
// Insufficient authorization
|
||||
return (ATT_ERR_INSUFFICIENT_AUTHOR);
|
||||
}
|
||||
|
||||
if(pAttr->type.len == ATT_BT_UUID_SIZE)
|
||||
{
|
||||
// 16-bit UUID
|
||||
uint16_t uuid = BUILD_UINT16(pAttr->type.uuid[0], pAttr->type.uuid[1]);
|
||||
switch(uuid)
|
||||
{
|
||||
case SIMPLEPROFILE_CHAR1_UUID:
|
||||
//Validate the value
|
||||
// Make sure it's not a blob oper
|
||||
if(offset == 0)
|
||||
{
|
||||
if(len > SIMPLEPROFILE_CHAR1_LEN)
|
||||
{
|
||||
status = ATT_ERR_INVALID_VALUE_SIZE;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
status = ATT_ERR_ATTR_NOT_LONG;
|
||||
}
|
||||
|
||||
//Write the value
|
||||
if(status == SUCCESS)
|
||||
{
|
||||
tmos_memcpy(pAttr->pValue, pValue, SIMPLEPROFILE_CHAR1_LEN);
|
||||
notifyApp = SIMPLEPROFILE_CHAR1;
|
||||
}
|
||||
break;
|
||||
|
||||
case SIMPLEPROFILE_CHAR3_UUID:
|
||||
//Validate the value
|
||||
// Make sure it's not a blob oper
|
||||
if(offset == 0)
|
||||
{
|
||||
if(len > SIMPLEPROFILE_CHAR3_LEN)
|
||||
{
|
||||
status = ATT_ERR_INVALID_VALUE_SIZE;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
status = ATT_ERR_ATTR_NOT_LONG;
|
||||
}
|
||||
|
||||
//Write the value
|
||||
if(status == SUCCESS)
|
||||
{
|
||||
tmos_memcpy(pAttr->pValue, pValue, SIMPLEPROFILE_CHAR3_LEN);
|
||||
notifyApp = SIMPLEPROFILE_CHAR3;
|
||||
}
|
||||
break;
|
||||
|
||||
case GATT_CLIENT_CHAR_CFG_UUID:
|
||||
status = GATTServApp_ProcessCCCWriteReq(connHandle, pAttr, pValue, len,
|
||||
offset, GATT_CLIENT_CFG_NOTIFY);
|
||||
break;
|
||||
|
||||
default:
|
||||
// Should never get here! (characteristics 2 and 4 do not have write permissions)
|
||||
status = ATT_ERR_ATTR_NOT_FOUND;
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// 128-bit UUID
|
||||
status = ATT_ERR_INVALID_HANDLE;
|
||||
}
|
||||
|
||||
// If a charactersitic value changed then callback function to notify application of change
|
||||
if((notifyApp != 0xFF) && simpleProfile_AppCBs && simpleProfile_AppCBs->pfnSimpleProfileChange)
|
||||
{
|
||||
simpleProfile_AppCBs->pfnSimpleProfileChange(notifyApp, pValue, len);
|
||||
}
|
||||
|
||||
return (status);
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn simpleProfile_HandleConnStatusCB
|
||||
*
|
||||
* @brief Simple Profile link status change handler function.
|
||||
*
|
||||
* @param connHandle - connection handle
|
||||
* @param changeType - type of change
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
static void simpleProfile_HandleConnStatusCB(uint16_t connHandle, uint8_t changeType)
|
||||
{
|
||||
// Make sure this is not loopback connection
|
||||
if(connHandle != LOOPBACK_CONNHANDLE)
|
||||
{
|
||||
// Reset Client Char Config if connection has dropped
|
||||
if((changeType == LINKDB_STATUS_UPDATE_REMOVED) ||
|
||||
((changeType == LINKDB_STATUS_UPDATE_STATEFLAGS) &&
|
||||
(!linkDB_Up(connHandle))))
|
||||
{
|
||||
GATTServApp_InitCharCfg(connHandle, simpleProfileChar4Config);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
*********************************************************************/
|
||||
@@ -0,0 +1,109 @@
|
||||
/********************************** (C) COPYRIGHT *******************************
|
||||
* File Name : devinfoservice.h
|
||||
* Author : WCH
|
||||
* Version : V1.0
|
||||
* Date : 2018/12/11
|
||||
* Description :
|
||||
*********************************************************************************
|
||||
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
|
||||
* Attention: This software (modified or not) and binary are used for
|
||||
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
|
||||
*******************************************************************************/
|
||||
|
||||
#ifndef DEVINFOSERVICE_H
|
||||
#define DEVINFOSERVICE_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*********************************************************************
|
||||
* INCLUDES
|
||||
*/
|
||||
|
||||
/*********************************************************************
|
||||
* CONSTANTS
|
||||
*/
|
||||
|
||||
// Device Information Service Parameters
|
||||
#define DEVINFO_SYSTEM_ID 0
|
||||
#define DEVINFO_MODEL_NUMBER 1
|
||||
#define DEVINFO_SERIAL_NUMBER 2
|
||||
#define DEVINFO_FIRMWARE_REV 3
|
||||
#define DEVINFO_HARDWARE_REV 4
|
||||
#define DEVINFO_SOFTWARE_REV 5
|
||||
#define DEVINFO_MANUFACTURER_NAME 6
|
||||
#define DEVINFO_11073_CERT_DATA 7
|
||||
#define DEVINFO_PNP_ID 8
|
||||
|
||||
// IEEE 11073 authoritative body values
|
||||
#define DEVINFO_11073_BODY_EMPTY 0
|
||||
#define DEVINFO_11073_BODY_IEEE 1
|
||||
#define DEVINFO_11073_BODY_CONTINUA 2
|
||||
#define DEVINFO_11073_BODY_EXP 254
|
||||
|
||||
// System ID length
|
||||
#define DEVINFO_SYSTEM_ID_LEN 8
|
||||
|
||||
// PnP ID length
|
||||
#define DEVINFO_PNP_ID_LEN 7
|
||||
|
||||
/*********************************************************************
|
||||
* TYPEDEFS
|
||||
*/
|
||||
|
||||
/*********************************************************************
|
||||
* MACROS
|
||||
*/
|
||||
|
||||
/*********************************************************************
|
||||
* Profile Callbacks
|
||||
*/
|
||||
|
||||
/*********************************************************************
|
||||
* API FUNCTIONS
|
||||
*/
|
||||
|
||||
/*
|
||||
* DevInfo_AddService- Initializes the Device Information service by registering
|
||||
* GATT attributes with the GATT server.
|
||||
*
|
||||
*/
|
||||
|
||||
extern bStatus_t DevInfo_AddService(void);
|
||||
|
||||
/*********************************************************************
|
||||
* @fn DevInfo_SetParameter
|
||||
*
|
||||
* @brief Set a Device Information parameter.
|
||||
*
|
||||
* @param param - Profile parameter ID
|
||||
* @param len - length of data to right
|
||||
* @param value - pointer to data to write. This is dependent on
|
||||
* the parameter ID and WILL be cast to the appropriate
|
||||
* data type (example: data type of uint16_t will be cast to
|
||||
* uint16_t pointer).
|
||||
*
|
||||
* @return bStatus_t
|
||||
*/
|
||||
bStatus_t DevInfo_SetParameter(uint8_t param, uint16_t len, void *value);
|
||||
|
||||
/*
|
||||
* DevInfo_GetParameter - Get a Device Information parameter.
|
||||
*
|
||||
* param - Profile parameter ID
|
||||
* value - pointer to data to write. This is dependent on
|
||||
* the parameter ID and WILL be cast to the appropriate
|
||||
* data type (example: data type of uint16_t will be cast to
|
||||
* uint16_t pointer).
|
||||
*/
|
||||
extern bStatus_t DevInfo_GetParameter(uint8_t param, void *value);
|
||||
|
||||
/*********************************************************************
|
||||
*********************************************************************/
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* DEVINFOSERVICE_H */
|
||||
@@ -0,0 +1,135 @@
|
||||
/********************************** (C) COPYRIGHT *******************************
|
||||
* File Name : gattprofile.h
|
||||
* Author : WCH
|
||||
* Version : V1.0
|
||||
* Date : 2018/12/11
|
||||
* Description :
|
||||
*********************************************************************************
|
||||
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
|
||||
* Attention: This software (modified or not) and binary are used for
|
||||
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
|
||||
*******************************************************************************/
|
||||
|
||||
#ifndef GATTPROFILE_H
|
||||
#define GATTPROFILE_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*********************************************************************
|
||||
* INCLUDES
|
||||
*/
|
||||
|
||||
/*********************************************************************
|
||||
* CONSTANTS
|
||||
*/
|
||||
|
||||
// Profile Parameters
|
||||
#define SIMPLEPROFILE_CHAR1 0 // RW uint8_t - Profile Characteristic 1 value
|
||||
#define SIMPLEPROFILE_CHAR2 1 // RW uint8_t - Profile Characteristic 2 value
|
||||
#define SIMPLEPROFILE_CHAR3 2 // RW uint8_t - Profile Characteristic 3 value
|
||||
#define SIMPLEPROFILE_CHAR4 3 // RW uint8_t - Profile Characteristic 4 value
|
||||
#define SIMPLEPROFILE_CHAR5 4 // RW uint8_t - Profile Characteristic 4 value
|
||||
|
||||
// Simple Profile Service UUID
|
||||
#define SIMPLEPROFILE_SERV_UUID 0xFFE0
|
||||
|
||||
// Key Pressed UUID
|
||||
#define SIMPLEPROFILE_CHAR1_UUID 0xFFE1
|
||||
#define SIMPLEPROFILE_CHAR2_UUID 0xFFE2
|
||||
#define SIMPLEPROFILE_CHAR3_UUID 0xFFE3
|
||||
#define SIMPLEPROFILE_CHAR4_UUID 0xFFE4
|
||||
#define SIMPLEPROFILE_CHAR5_UUID 0xFFE5
|
||||
|
||||
// Simple Keys Profile Services bit fields
|
||||
#define SIMPLEPROFILE_SERVICE 0x00000001
|
||||
|
||||
// Length of characteristic in bytes ( Default MTU is 23 )
|
||||
#define SIMPLEPROFILE_CHAR1_LEN 100 //64
|
||||
#define SIMPLEPROFILE_CHAR2_LEN 1
|
||||
#define SIMPLEPROFILE_CHAR3_LEN 10
|
||||
#define SIMPLEPROFILE_CHAR4_LEN 100 //32
|
||||
#define SIMPLEPROFILE_CHAR5_LEN 5
|
||||
|
||||
/*********************************************************************
|
||||
* TYPEDEFS
|
||||
*/
|
||||
|
||||
/*********************************************************************
|
||||
* MACROS
|
||||
*/
|
||||
|
||||
/*********************************************************************
|
||||
* Profile Callbacks
|
||||
*/
|
||||
|
||||
// Callback when a characteristic value has changed
|
||||
typedef void (*simpleProfileChange_t)(uint8_t paramID, uint8_t *pValue, uint16_t len);
|
||||
|
||||
typedef struct
|
||||
{
|
||||
simpleProfileChange_t pfnSimpleProfileChange; // Called when characteristic value changes
|
||||
} simpleProfileCBs_t;
|
||||
|
||||
/*********************************************************************
|
||||
* API FUNCTIONS
|
||||
*/
|
||||
|
||||
/*
|
||||
* SimpleProfile_AddService- Initializes the Simple GATT Profile service by registering
|
||||
* GATT attributes with the GATT server.
|
||||
*
|
||||
* @param services - services to add. This is a bit map and can
|
||||
* contain more than one service.
|
||||
*/
|
||||
|
||||
extern bStatus_t SimpleProfile_AddService(uint32_t services);
|
||||
|
||||
/*
|
||||
* SimpleProfile_RegisterAppCBs - Registers the application callback function.
|
||||
* Only call this function once.
|
||||
*
|
||||
* appCallbacks - pointer to application callbacks.
|
||||
*/
|
||||
extern bStatus_t SimpleProfile_RegisterAppCBs(simpleProfileCBs_t *appCallbacks);
|
||||
|
||||
/*
|
||||
* SimpleProfile_SetParameter - Set a Simple GATT Profile parameter.
|
||||
*
|
||||
* param - Profile parameter ID
|
||||
* len - length of data to right
|
||||
* value - pointer to data to write. This is dependent on
|
||||
* the parameter ID and WILL be cast to the appropriate
|
||||
* data type (example: data type of uint16_t will be cast to
|
||||
* uint16_t pointer).
|
||||
*/
|
||||
extern bStatus_t SimpleProfile_SetParameter(uint8_t param, uint16_t len, void *value);
|
||||
|
||||
/*
|
||||
* SimpleProfile_GetParameter - Get a Simple GATT Profile parameter.
|
||||
*
|
||||
* param - Profile parameter ID
|
||||
* value - pointer to data to write. This is dependent on
|
||||
* the parameter ID and WILL be cast to the appropriate
|
||||
* data type (example: data type of uint16_t will be cast to
|
||||
* uint16_t pointer).
|
||||
*/
|
||||
extern bStatus_t SimpleProfile_GetParameter(uint8_t param, void *value);
|
||||
|
||||
/*
|
||||
* simpleProfile_Notify - Send notification.
|
||||
*
|
||||
* connHandle - connect handle
|
||||
* pNoti - pointer to structure to notify.
|
||||
*/
|
||||
extern bStatus_t simpleProfile_Notify(uint16_t connHandle, attHandleValueNoti_t *pNoti);
|
||||
|
||||
/*********************************************************************
|
||||
*********************************************************************/
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,290 @@
|
||||
;/********************************** (C) COPYRIGHT *******************************
|
||||
;* File Name : startup_ch32v20x_D8W.s
|
||||
;* Author : WCH
|
||||
;* Version : V1.0.0
|
||||
;* Date : 2021/06/06
|
||||
;* Description : CH32V208x
|
||||
;* vector table for eclipse toolchain.
|
||||
;*********************************************************************************
|
||||
;* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
|
||||
;* Attention: This software (modified or not) and binary are used for
|
||||
;* microcontroller manufactured by Nanjing Qinheng Microelectronics.
|
||||
*******************************************************************************/
|
||||
|
||||
.section .init,"ax",@progbits
|
||||
.global _start
|
||||
.align 1
|
||||
_start:
|
||||
j handle_reset
|
||||
.word 0x00000013
|
||||
.word 0x00000013
|
||||
.word 0x00000013
|
||||
.word 0x00000013
|
||||
.word 0x00000013
|
||||
.word 0x00000013
|
||||
.word 0x00000013
|
||||
.word 0x00000013
|
||||
.word 0x00000013
|
||||
.word 0x00000013
|
||||
.word 0x00000013
|
||||
.word 0x00000013
|
||||
.word 0x00100073
|
||||
.section .vector,"ax",@progbits
|
||||
.align 1
|
||||
_vector_base:
|
||||
.option norvc;
|
||||
.word _start
|
||||
.word 0
|
||||
.word NMI_Handler /* NMI */
|
||||
.word HardFault_Handler /* Hard Fault */
|
||||
.word 0
|
||||
.word Ecall_M_Mode_Handler /* Ecall M Mode */
|
||||
.word 0
|
||||
.word 0
|
||||
.word Ecall_U_Mode_Handler /* Ecall U Mode */
|
||||
.word Break_Point_Handler /* Break Point */
|
||||
.word 0
|
||||
.word 0
|
||||
.word SysTick_Handler /* SysTick */
|
||||
.word 0
|
||||
.word SW_Handler /* SW */
|
||||
.word 0
|
||||
/* External Interrupts */
|
||||
.word WWDG_IRQHandler /* Window Watchdog */
|
||||
.word PVD_IRQHandler /* PVD through EXTI Line detect */
|
||||
.word TAMPER_IRQHandler /* TAMPER */
|
||||
.word RTC_IRQHandler /* RTC */
|
||||
.word FLASH_IRQHandler /* Flash */
|
||||
.word RCC_IRQHandler /* RCC */
|
||||
.word EXTI0_IRQHandler /* EXTI Line 0 */
|
||||
.word EXTI1_IRQHandler /* EXTI Line 1 */
|
||||
.word EXTI2_IRQHandler /* EXTI Line 2 */
|
||||
.word EXTI3_IRQHandler /* EXTI Line 3 */
|
||||
.word EXTI4_IRQHandler /* EXTI Line 4 */
|
||||
.word DMA1_Channel1_IRQHandler /* DMA1 Channel 1 */
|
||||
.word DMA1_Channel2_IRQHandler /* DMA1 Channel 2 */
|
||||
.word DMA1_Channel3_IRQHandler /* DMA1 Channel 3 */
|
||||
.word DMA1_Channel4_IRQHandler /* DMA1 Channel 4 */
|
||||
.word DMA1_Channel5_IRQHandler /* DMA1 Channel 5 */
|
||||
.word DMA1_Channel6_IRQHandler /* DMA1 Channel 6 */
|
||||
.word DMA1_Channel7_IRQHandler /* DMA1 Channel 7 */
|
||||
.word ADC1_2_IRQHandler /* ADC1_2 */
|
||||
.word USB_HP_CAN1_TX_IRQHandler /* USB HP and CAN1 TX */
|
||||
.word USB_LP_CAN1_RX0_IRQHandler /* USB LP and CAN1RX0 */
|
||||
.word CAN1_RX1_IRQHandler /* CAN1 RX1 */
|
||||
.word CAN1_SCE_IRQHandler /* CAN1 SCE */
|
||||
.word EXTI9_5_IRQHandler /* EXTI Line 9..5 */
|
||||
.word TIM1_BRK_IRQHandler /* TIM1 Break */
|
||||
.word TIM1_UP_IRQHandler /* TIM1 Update */
|
||||
.word TIM1_TRG_COM_IRQHandler /* TIM1 Trigger and Commutation */
|
||||
.word TIM1_CC_IRQHandler /* TIM1 Capture Compare */
|
||||
.word TIM2_IRQHandler /* TIM2 */
|
||||
.word TIM3_IRQHandler /* TIM3 */
|
||||
.word TIM4_IRQHandler /* TIM4 */
|
||||
.word I2C1_EV_IRQHandler /* I2C1 Event */
|
||||
.word I2C1_ER_IRQHandler /* I2C1 Error */
|
||||
.word I2C2_EV_IRQHandler /* I2C2 Event */
|
||||
.word I2C2_ER_IRQHandler /* I2C2 Error */
|
||||
.word SPI1_IRQHandler /* SPI1 */
|
||||
.word SPI2_IRQHandler /* SPI2 */
|
||||
.word USART1_IRQHandler /* USART1 */
|
||||
.word USART2_IRQHandler /* USART2 */
|
||||
.word USART3_IRQHandler /* USART3 */
|
||||
.word EXTI15_10_IRQHandler /* EXTI Line 15..10 */
|
||||
.word RTCAlarm_IRQHandler /* RTC Alarm through EXTI Line */
|
||||
.word USBWakeUp_IRQHandler /* USB Wake up from suspend */
|
||||
.word USBFS_IRQHandler /* USBFS Break */
|
||||
.word USBFSWakeUp_IRQHandler /* USBFS Wake up from suspend */
|
||||
.word ETH_IRQHandler /* ETH global */
|
||||
.word ETHWakeUp_IRQHandler /* ETH Wake up */
|
||||
.word BB_IRQHandler /* BLE BB */
|
||||
.word LLE_IRQHandler /* BLE LLE */
|
||||
.word TIM5_IRQHandler /* TIM5 */
|
||||
.word UART4_IRQHandler /* UART4 */
|
||||
.word DMA1_Channel8_IRQHandler /* DMA1 Channel8 */
|
||||
.word OSC32KCal_IRQHandler /* OSC32KCal */
|
||||
.word OSCWakeUp_IRQHandler /* OSC Wake Up */
|
||||
|
||||
.option rvc;
|
||||
|
||||
.section .text.vector_handler, "ax", @progbits
|
||||
.weak NMI_Handler /* NMI */
|
||||
.weak HardFault_Handler /* Hard Fault */
|
||||
.weak Ecall_M_Mode_Handler /* Ecall M Mode */
|
||||
.weak Ecall_U_Mode_Handler /* Ecall U Mode */
|
||||
.weak Break_Point_Handler /* Break Point */
|
||||
.weak SysTick_Handler /* SysTick */
|
||||
.weak SW_Handler /* SW */
|
||||
.weak WWDG_IRQHandler /* Window Watchdog */
|
||||
.weak PVD_IRQHandler /* PVD through EXTI Line detect */
|
||||
.weak TAMPER_IRQHandler /* TAMPER */
|
||||
.weak RTC_IRQHandler /* RTC */
|
||||
.weak FLASH_IRQHandler /* Flash */
|
||||
.weak RCC_IRQHandler /* RCC */
|
||||
.weak EXTI0_IRQHandler /* EXTI Line 0 */
|
||||
.weak EXTI1_IRQHandler /* EXTI Line 1 */
|
||||
.weak EXTI2_IRQHandler /* EXTI Line 2 */
|
||||
.weak EXTI3_IRQHandler /* EXTI Line 3 */
|
||||
.weak EXTI4_IRQHandler /* EXTI Line 4 */
|
||||
.weak DMA1_Channel1_IRQHandler /* DMA1 Channel 1 */
|
||||
.weak DMA1_Channel2_IRQHandler /* DMA1 Channel 2 */
|
||||
.weak DMA1_Channel3_IRQHandler /* DMA1 Channel 3 */
|
||||
.weak DMA1_Channel4_IRQHandler /* DMA1 Channel 4 */
|
||||
.weak DMA1_Channel5_IRQHandler /* DMA1 Channel 5 */
|
||||
.weak DMA1_Channel6_IRQHandler /* DMA1 Channel 6 */
|
||||
.weak DMA1_Channel7_IRQHandler /* DMA1 Channel 7 */
|
||||
.weak ADC1_2_IRQHandler /* ADC1_2 */
|
||||
.weak USB_HP_CAN1_TX_IRQHandler /* USB HP and CAN1 TX */
|
||||
.weak USB_LP_CAN1_RX0_IRQHandler /* USB LP and CAN1RX0 */
|
||||
.weak CAN1_RX1_IRQHandler /* CAN1 RX1 */
|
||||
.weak CAN1_SCE_IRQHandler /* CAN1 SCE */
|
||||
.weak EXTI9_5_IRQHandler /* EXTI Line 9..5 */
|
||||
.weak TIM1_BRK_IRQHandler /* TIM1 Break */
|
||||
.weak TIM1_UP_IRQHandler /* TIM1 Update */
|
||||
.weak TIM1_TRG_COM_IRQHandler /* TIM1 Trigger and Commutation */
|
||||
.weak TIM1_CC_IRQHandler /* TIM1 Capture Compare */
|
||||
.weak TIM2_IRQHandler /* TIM2 */
|
||||
.weak TIM3_IRQHandler /* TIM3 */
|
||||
.weak TIM4_IRQHandler /* TIM4 */
|
||||
.weak I2C1_EV_IRQHandler /* I2C1 Event */
|
||||
.weak I2C1_ER_IRQHandler /* I2C1 Error */
|
||||
.weak I2C2_EV_IRQHandler /* I2C2 Event */
|
||||
.weak I2C2_ER_IRQHandler /* I2C2 Error */
|
||||
.weak SPI1_IRQHandler /* SPI1 */
|
||||
.weak SPI2_IRQHandler /* SPI2 */
|
||||
.weak USART1_IRQHandler /* USART1 */
|
||||
.weak USART2_IRQHandler /* USART2 */
|
||||
.weak USART3_IRQHandler /* USART3 */
|
||||
.weak EXTI15_10_IRQHandler /* EXTI Line 15..10 */
|
||||
.weak RTCAlarm_IRQHandler /* RTC Alarm through EXTI Line */
|
||||
.weak USBWakeUp_IRQHandler /* USB Wakeup from suspend */
|
||||
.weak USBFS_IRQHandler /* USBFS */
|
||||
.weak USBFSWakeUp_IRQHandler /* USBFS Wake Up */
|
||||
.weak ETH_IRQHandler /* ETH global */
|
||||
.weak ETHWakeUp_IRQHandler /* ETHWakeUp */
|
||||
.weak BB_IRQHandler /* BB */
|
||||
.weak LLE_IRQHandler /* LLE */
|
||||
.weak TIM5_IRQHandler /* TIM5 */
|
||||
.weak UART4_IRQHandler /* UART4 */
|
||||
.weak DMA1_Channel8_IRQHandler /* DMA1 Channel8 */
|
||||
.weak OSC32KCal_IRQHandler /* OSC32 KCal */
|
||||
.weak OSCWakeUp_IRQHandler /* OSC Wake Up */
|
||||
|
||||
NMI_Handler: 1: j 1b
|
||||
HardFault_Handler: 1: j 1b
|
||||
Ecall_M_Mode_Handler: 1: j 1b
|
||||
Ecall_U_Mode_Handler: 1: j 1b
|
||||
Break_Point_Handler: 1: j 1b
|
||||
SysTick_Handler: 1: j 1b
|
||||
SW_Handler: 1: j 1b
|
||||
WWDG_IRQHandler: 1: j 1b
|
||||
PVD_IRQHandler: 1: j 1b
|
||||
TAMPER_IRQHandler: 1: j 1b
|
||||
RTC_IRQHandler: 1: j 1b
|
||||
FLASH_IRQHandler: 1: j 1b
|
||||
RCC_IRQHandler: 1: j 1b
|
||||
EXTI0_IRQHandler: 1: j 1b
|
||||
EXTI1_IRQHandler: 1: j 1b
|
||||
EXTI2_IRQHandler: 1: j 1b
|
||||
EXTI3_IRQHandler: 1: j 1b
|
||||
EXTI4_IRQHandler: 1: j 1b
|
||||
DMA1_Channel1_IRQHandler: 1: j 1b
|
||||
DMA1_Channel2_IRQHandler: 1: j 1b
|
||||
DMA1_Channel3_IRQHandler: 1: j 1b
|
||||
DMA1_Channel4_IRQHandler: 1: j 1b
|
||||
DMA1_Channel5_IRQHandler: 1: j 1b
|
||||
DMA1_Channel6_IRQHandler: 1: j 1b
|
||||
DMA1_Channel7_IRQHandler: 1: j 1b
|
||||
ADC1_2_IRQHandler: 1: j 1b
|
||||
USB_HP_CAN1_TX_IRQHandler: 1: j 1b
|
||||
USB_LP_CAN1_RX0_IRQHandler: 1: j 1b
|
||||
CAN1_RX1_IRQHandler: 1: j 1b
|
||||
CAN1_SCE_IRQHandler: 1: j 1b
|
||||
EXTI9_5_IRQHandler: 1: j 1b
|
||||
TIM1_BRK_IRQHandler: 1: j 1b
|
||||
TIM1_UP_IRQHandler: 1: j 1b
|
||||
TIM1_TRG_COM_IRQHandler: 1: j 1b
|
||||
TIM1_CC_IRQHandler: 1: j 1b
|
||||
TIM2_IRQHandler: 1: j 1b
|
||||
TIM3_IRQHandler: 1: j 1b
|
||||
TIM4_IRQHandler: 1: j 1b
|
||||
I2C1_EV_IRQHandler: 1: j 1b
|
||||
I2C1_ER_IRQHandler: 1: j 1b
|
||||
I2C2_EV_IRQHandler: 1: j 1b
|
||||
I2C2_ER_IRQHandler: 1: j 1b
|
||||
SPI1_IRQHandler: 1: j 1b
|
||||
SPI2_IRQHandler: 1: j 1b
|
||||
USART1_IRQHandler: 1: j 1b
|
||||
USART2_IRQHandler: 1: j 1b
|
||||
USART3_IRQHandler: 1: j 1b
|
||||
EXTI15_10_IRQHandler: 1: j 1b
|
||||
RTCAlarm_IRQHandler: 1: j 1b
|
||||
USBWakeUp_IRQHandler: 1: j 1b
|
||||
USBFS_IRQHandler: 1: j 1b
|
||||
USBFSWakeUp_IRQHandler: 1: j 1b
|
||||
ETH_IRQHandler: 1: j 1b
|
||||
ETHWakeUp_IRQHandler: 1: j 1b
|
||||
BB_IRQHandler: 1: j 1b
|
||||
LLE_IRQHandler: 1: j 1b
|
||||
TIM5_IRQHandler: 1: j 1b
|
||||
UART4_IRQHandler: 1: j 1b
|
||||
DMA1_Channel8_IRQHandler: 1: j 1b
|
||||
OSC32KCal_IRQHandler: 1: j 1b
|
||||
OSCWakeUp_IRQHandler: 1: j 1b
|
||||
|
||||
.section .text.handle_reset,"ax",@progbits
|
||||
.weak handle_reset
|
||||
.align 1
|
||||
handle_reset:
|
||||
.option push
|
||||
.option norelax
|
||||
la gp, __global_pointer$
|
||||
.option pop
|
||||
1:
|
||||
la sp, _eusrstack
|
||||
2:
|
||||
/* Load data section from flash to RAM */
|
||||
la a0, _data_lma
|
||||
la a1, _data_vma
|
||||
la a2, _edata
|
||||
bgeu a1, a2, 2f
|
||||
1:
|
||||
lw t0, (a0)
|
||||
sw t0, (a1)
|
||||
addi a0, a0, 4
|
||||
addi a1, a1, 4
|
||||
bltu a1, a2, 1b
|
||||
2:
|
||||
/* Clear bss section */
|
||||
la a0, _sbss
|
||||
la a1, _ebss
|
||||
bgeu a0, a1, 2f
|
||||
1:
|
||||
sw zero, (a0)
|
||||
addi a0, a0, 4
|
||||
bltu a0, a1, 1b
|
||||
2:
|
||||
li t0, 0x1f
|
||||
csrw 0xbc0, t0
|
||||
|
||||
/* Enable nested and hardware stack */
|
||||
li t0, 0x3
|
||||
csrw 0x804, t0
|
||||
|
||||
/* Enable interrupt */
|
||||
li t0, 0x1888
|
||||
csrs mstatus, t0
|
||||
|
||||
la t0, _vector_base
|
||||
ori t0, t0, 3
|
||||
csrw mtvec, t0
|
||||
|
||||
jal SystemInit
|
||||
la t0, main
|
||||
csrw mepc, t0
|
||||
|
||||
j 0x40000
|
||||
|
||||
mret
|
||||
|
||||
|
||||
136
vd960DBN/ETH/MQTT_SRC/MQTTConnect.h
Normal file
136
vd960DBN/ETH/MQTT_SRC/MQTTConnect.h
Normal file
@@ -0,0 +1,136 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2014 IBM Corp.
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
* and the Eclipse Distribution License is available at
|
||||
* http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
*
|
||||
* Contributors:
|
||||
* Ian Craggs - initial API and implementation and/or initial documentation
|
||||
* Xiang Rong - 442039 Add makefile to Embedded C client
|
||||
*******************************************************************************/
|
||||
|
||||
#ifndef MQTTCONNECT_H_
|
||||
#define MQTTCONNECT_H_
|
||||
|
||||
#if !defined(DLLImport)
|
||||
#define DLLImport
|
||||
#endif
|
||||
#if !defined(DLLExport)
|
||||
#define DLLExport
|
||||
#endif
|
||||
|
||||
|
||||
typedef union
|
||||
{
|
||||
unsigned char all; /**< all connect flags */
|
||||
#if defined(REVERSED)
|
||||
struct
|
||||
{
|
||||
unsigned int username : 1; /**< 3.1 user name */
|
||||
unsigned int password : 1; /**< 3.1 password */
|
||||
unsigned int willRetain : 1; /**< will retain setting */
|
||||
unsigned int willQoS : 2; /**< will QoS value */
|
||||
unsigned int will : 1; /**< will flag */
|
||||
unsigned int cleansession : 1; /**< clean session flag */
|
||||
unsigned int : 1; /**< unused */
|
||||
} bits;
|
||||
#else
|
||||
struct
|
||||
{
|
||||
unsigned int : 1; /**< unused */
|
||||
unsigned int cleansession : 1; /**< cleansession flag */
|
||||
unsigned int will : 1; /**< will flag */
|
||||
unsigned int willQoS : 2; /**< will QoS value */
|
||||
unsigned int willRetain : 1; /**< will retain setting */
|
||||
unsigned int password : 1; /**< 3.1 password */
|
||||
unsigned int username : 1; /**< 3.1 user name */
|
||||
} bits;
|
||||
#endif
|
||||
} MQTTConnectFlags; /**< connect flags byte */
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Defines the MQTT "Last Will and Testament" (LWT) settings for
|
||||
* the connect packet.
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
/** The eyecatcher for this structure. must be MQTW. */
|
||||
char struct_id[4];
|
||||
/** The version number of this structure. Must be 0 */
|
||||
int struct_version;
|
||||
/** The LWT topic to which the LWT message will be published. */
|
||||
MQTTString topicName;
|
||||
/** The LWT payload. */
|
||||
MQTTString message;
|
||||
/**
|
||||
* The retained flag for the LWT message (see MQTTAsync_message.retained).
|
||||
*/
|
||||
unsigned char retained;
|
||||
/**
|
||||
* The quality of service setting for the LWT message (see
|
||||
* MQTTAsync_message.qos and @ref qos).
|
||||
*/
|
||||
char qos;
|
||||
} MQTTPacket_willOptions;
|
||||
|
||||
|
||||
#define MQTTPacket_willOptions_initializer { {'M', 'Q', 'T', 'W'}, 0, {NULL, {0, NULL}}, {NULL, {0, NULL}}, 0, 0 }
|
||||
|
||||
|
||||
typedef struct
|
||||
{
|
||||
/** The eyecatcher for this structure. must be MQTC. */
|
||||
char struct_id[4];
|
||||
/** The version number of this structure. Must be 0 */
|
||||
int struct_version;
|
||||
/** Version of MQTT to be used. 3 = 3.1 4 = 3.1.1
|
||||
*/
|
||||
unsigned char MQTTVersion;
|
||||
MQTTString clientID;
|
||||
unsigned short keepAliveInterval;
|
||||
unsigned char cleansession;
|
||||
unsigned char willFlag;
|
||||
MQTTPacket_willOptions will;
|
||||
MQTTString username;
|
||||
MQTTString password;
|
||||
} MQTTPacket_connectData;
|
||||
|
||||
typedef union
|
||||
{
|
||||
unsigned char all; /**< all connack flags */
|
||||
#if defined(REVERSED)
|
||||
struct
|
||||
{
|
||||
unsigned int sessionpresent : 1; /**< session present flag */
|
||||
unsigned int : 7; /**< unused */
|
||||
} bits;
|
||||
#else
|
||||
struct
|
||||
{
|
||||
unsigned int : 7; /**< unused */
|
||||
unsigned int sessionpresent : 1; /**< session present flag */
|
||||
} bits;
|
||||
#endif
|
||||
} MQTTConnackFlags; /**< connack flags byte */
|
||||
|
||||
#define MQTTPacket_connectData_initializer { {'M', 'Q', 'T', 'C'}, 0, 4, {NULL, {0, NULL}}, 60, 1, 0, \
|
||||
MQTTPacket_willOptions_initializer, {NULL, {0, NULL}}, {NULL, {0, NULL}} }
|
||||
|
||||
DLLExport int MQTTSerialize_connect(unsigned char* buf, int buflen, MQTTPacket_connectData* options);
|
||||
DLLExport int MQTTDeserialize_connect(MQTTPacket_connectData* data, unsigned char* buf, int len);
|
||||
|
||||
DLLExport int MQTTSerialize_connack(unsigned char* buf, int buflen, unsigned char connack_rc, unsigned char sessionPresent);
|
||||
DLLExport int MQTTDeserialize_connack(unsigned char* sessionPresent, unsigned char* connack_rc, unsigned char* buf, int buflen);
|
||||
|
||||
DLLExport int MQTTSerialize_disconnect(unsigned char* buf, int buflen);
|
||||
DLLExport int MQTTSerialize_pingreq(unsigned char* buf, int buflen);
|
||||
|
||||
#endif /* MQTTCONNECT_H_ */
|
||||
214
vd960DBN/ETH/MQTT_SRC/MQTTConnectClient.c
Normal file
214
vd960DBN/ETH/MQTT_SRC/MQTTConnectClient.c
Normal file
@@ -0,0 +1,214 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2014 IBM Corp.
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
* and the Eclipse Distribution License is available at
|
||||
* http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
*
|
||||
* Contributors:
|
||||
* Ian Craggs - initial API and implementation and/or initial documentation
|
||||
*******************************************************************************/
|
||||
|
||||
#include "MQTTPacket.h"
|
||||
#include "StackTrace.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
/**
|
||||
* Determines the length of the MQTT connect packet that would be produced using the supplied connect options.
|
||||
* @param options the options to be used to build the connect packet
|
||||
* @return the length of buffer needed to contain the serialized version of the packet
|
||||
*/
|
||||
int MQTTSerialize_connectLength(MQTTPacket_connectData* options)
|
||||
{
|
||||
int len = 0;
|
||||
|
||||
FUNC_ENTRY;
|
||||
|
||||
if (options->MQTTVersion == 3)
|
||||
len = 12; /* variable depending on MQTT or MQIsdp */
|
||||
else if (options->MQTTVersion == 4)
|
||||
len = 10;
|
||||
|
||||
len += MQTTstrlen(options->clientID)+2;
|
||||
if (options->willFlag)
|
||||
len += MQTTstrlen(options->will.topicName)+2 + MQTTstrlen(options->will.message)+2;
|
||||
if (options->username.cstring || options->username.lenstring.data)
|
||||
len += MQTTstrlen(options->username)+2;
|
||||
if (options->password.cstring || options->password.lenstring.data)
|
||||
len += MQTTstrlen(options->password)+2;
|
||||
|
||||
FUNC_EXIT_RC(len);
|
||||
return len;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Serializes the connect options into the buffer.
|
||||
* @param buf the buffer into which the packet will be serialized
|
||||
* @param len the length in bytes of the supplied buffer
|
||||
* @param options the options to be used to build the connect packet
|
||||
* @return serialized length, or error if 0
|
||||
*/
|
||||
int MQTTSerialize_connect(unsigned char* buf, int buflen, MQTTPacket_connectData* options)
|
||||
{
|
||||
unsigned char *ptr = buf;
|
||||
MQTTHeader header = {0};
|
||||
MQTTConnectFlags flags = {0};
|
||||
int len = 0;
|
||||
int rc = -1;
|
||||
|
||||
FUNC_ENTRY;
|
||||
if (MQTTPacket_len(len = MQTTSerialize_connectLength(options)) > buflen)
|
||||
{
|
||||
rc = MQTTPACKET_BUFFER_TOO_SHORT;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
header.byte = 0;
|
||||
header.bits.type = CONNECT;
|
||||
writeChar(&ptr, header.byte); /* write header */
|
||||
|
||||
ptr += MQTTPacket_encode(ptr, len); /* write remaining length */
|
||||
|
||||
if (options->MQTTVersion == 4)
|
||||
{
|
||||
writeCString(&ptr, "MQTT");
|
||||
writeChar(&ptr, (char) 4);
|
||||
}
|
||||
else
|
||||
{
|
||||
writeCString(&ptr, "MQIsdp");
|
||||
writeChar(&ptr, (char) 3);
|
||||
}
|
||||
|
||||
flags.all = 0;
|
||||
flags.bits.cleansession = options->cleansession;
|
||||
flags.bits.will = (options->willFlag) ? 1 : 0;
|
||||
if (flags.bits.will)
|
||||
{
|
||||
flags.bits.willQoS = options->will.qos;
|
||||
flags.bits.willRetain = options->will.retained;
|
||||
}
|
||||
|
||||
if (options->username.cstring || options->username.lenstring.data)
|
||||
flags.bits.username = 1;
|
||||
if (options->password.cstring || options->password.lenstring.data)
|
||||
flags.bits.password = 1;
|
||||
|
||||
writeChar(&ptr, flags.all);
|
||||
writeInt(&ptr, options->keepAliveInterval);
|
||||
writeMQTTString(&ptr, options->clientID);
|
||||
if (options->willFlag)
|
||||
{
|
||||
writeMQTTString(&ptr, options->will.topicName);
|
||||
writeMQTTString(&ptr, options->will.message);
|
||||
}
|
||||
if (flags.bits.username)
|
||||
writeMQTTString(&ptr, options->username);
|
||||
if (flags.bits.password)
|
||||
writeMQTTString(&ptr, options->password);
|
||||
|
||||
rc = ptr - buf;
|
||||
|
||||
exit: FUNC_EXIT_RC(rc);
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Deserializes the supplied (wire) buffer into connack data - return code
|
||||
* @param sessionPresent the session present flag returned (only for MQTT 3.1.1)
|
||||
* @param connack_rc returned integer value of the connack return code
|
||||
* @param buf the raw buffer data, of the correct length determined by the remaining length field
|
||||
* @param len the length in bytes of the data in the supplied buffer
|
||||
* @return error code. 1 is success, 0 is failure
|
||||
*/
|
||||
int MQTTDeserialize_connack(unsigned char* sessionPresent, unsigned char* connack_rc, unsigned char* buf, int buflen)
|
||||
{
|
||||
MQTTHeader header = {0};
|
||||
unsigned char* curdata = buf;
|
||||
unsigned char* enddata = NULL;
|
||||
int rc = 0;
|
||||
int mylen;
|
||||
MQTTConnackFlags flags = {0};
|
||||
|
||||
FUNC_ENTRY;
|
||||
header.byte = readChar(&curdata);
|
||||
if (header.bits.type != CONNACK)
|
||||
goto exit;
|
||||
|
||||
curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */
|
||||
enddata = curdata + mylen;
|
||||
if (enddata - curdata < 2)
|
||||
goto exit;
|
||||
|
||||
flags.all = readChar(&curdata);
|
||||
*sessionPresent = flags.bits.sessionpresent;
|
||||
*connack_rc = readChar(&curdata);
|
||||
|
||||
rc = 1;
|
||||
exit:
|
||||
FUNC_EXIT_RC(rc);
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Serializes a 0-length packet into the supplied buffer, ready for writing to a socket
|
||||
* @param buf the buffer into which the packet will be serialized
|
||||
* @param buflen the length in bytes of the supplied buffer, to avoid overruns
|
||||
* @param packettype the message type
|
||||
* @return serialized length, or error if 0
|
||||
*/
|
||||
int MQTTSerialize_zero(unsigned char* buf, int buflen, unsigned char packettype)
|
||||
{
|
||||
MQTTHeader header = {0};
|
||||
int rc = -1;
|
||||
unsigned char *ptr = buf;
|
||||
|
||||
FUNC_ENTRY;
|
||||
if (buflen < 2)
|
||||
{
|
||||
rc = MQTTPACKET_BUFFER_TOO_SHORT;
|
||||
goto exit;
|
||||
}
|
||||
header.byte = 0;
|
||||
header.bits.type = packettype;
|
||||
writeChar(&ptr, header.byte); /* write header */
|
||||
|
||||
ptr += MQTTPacket_encode(ptr, 0); /* write remaining length */
|
||||
rc = ptr - buf;
|
||||
exit:
|
||||
FUNC_EXIT_RC(rc);
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Serializes a disconnect packet into the supplied buffer, ready for writing to a socket
|
||||
* @param buf the buffer into which the packet will be serialized
|
||||
* @param buflen the length in bytes of the supplied buffer, to avoid overruns
|
||||
* @return serialized length, or error if 0
|
||||
*/
|
||||
int MQTTSerialize_disconnect(unsigned char* buf, int buflen)
|
||||
{
|
||||
return MQTTSerialize_zero(buf, buflen, DISCONNECT);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Serializes a disconnect packet into the supplied buffer, ready for writing to a socket
|
||||
* @param buf the buffer into which the packet will be serialized
|
||||
* @param buflen the length in bytes of the supplied buffer, to avoid overruns
|
||||
* @return serialized length, or error if 0
|
||||
*/
|
||||
int MQTTSerialize_pingreq(unsigned char* buf, int buflen)
|
||||
{
|
||||
return MQTTSerialize_zero(buf, buflen, PINGREQ);
|
||||
}
|
||||
148
vd960DBN/ETH/MQTT_SRC/MQTTConnectServer.c
Normal file
148
vd960DBN/ETH/MQTT_SRC/MQTTConnectServer.c
Normal file
@@ -0,0 +1,148 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2014 IBM Corp.
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
* and the Eclipse Distribution License is available at
|
||||
* http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
*
|
||||
* Contributors:
|
||||
* Ian Craggs - initial API and implementation and/or initial documentation
|
||||
*******************************************************************************/
|
||||
|
||||
#include "StackTrace.h"
|
||||
#include "MQTTPacket.h"
|
||||
#include <string.h>
|
||||
|
||||
#define min(a, b) ((a < b) ? a : b)
|
||||
|
||||
|
||||
/**
|
||||
* Validates MQTT protocol name and version combinations
|
||||
* @param protocol the MQTT protocol name as an MQTTString
|
||||
* @param version the MQTT protocol version number, as in the connect packet
|
||||
* @return correct MQTT combination? 1 is true, 0 is false
|
||||
*/
|
||||
int MQTTPacket_checkVersion(MQTTString* protocol, int version)
|
||||
{
|
||||
int rc = 0;
|
||||
|
||||
if (version == 3 && memcmp(protocol->lenstring.data, "MQIsdp",
|
||||
min(6, protocol->lenstring.len)) == 0)
|
||||
rc = 1;
|
||||
else if (version == 4 && memcmp(protocol->lenstring.data, "MQTT",
|
||||
min(4, protocol->lenstring.len)) == 0)
|
||||
rc = 1;
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Deserializes the supplied (wire) buffer into connect data structure
|
||||
* @param data the connect data structure to be filled out
|
||||
* @param buf the raw buffer data, of the correct length determined by the remaining length field
|
||||
* @param len the length in bytes of the data in the supplied buffer
|
||||
* @return error code. 1 is success, 0 is failure
|
||||
*/
|
||||
int MQTTDeserialize_connect(MQTTPacket_connectData* data, unsigned char* buf, int len)
|
||||
{
|
||||
MQTTHeader header = {0};
|
||||
MQTTConnectFlags flags = {0};
|
||||
unsigned char* curdata = buf;
|
||||
unsigned char* enddata = &buf[len];
|
||||
int rc = 0;
|
||||
MQTTString Protocol;
|
||||
int version;
|
||||
int mylen = 0;
|
||||
|
||||
FUNC_ENTRY;
|
||||
header.byte = readChar(&curdata);
|
||||
if (header.bits.type != CONNECT)
|
||||
goto exit;
|
||||
|
||||
curdata += MQTTPacket_decodeBuf(curdata, &mylen); /* read remaining length */
|
||||
|
||||
if (!readMQTTLenString(&Protocol, &curdata, enddata) ||
|
||||
enddata - curdata < 0) /* do we have enough data to read the protocol version byte? */
|
||||
goto exit;
|
||||
|
||||
version = (int)readChar(&curdata); /* Protocol version */
|
||||
/* If we don't recognize the protocol version, we don't parse the connect packet on the
|
||||
* basis that we don't know what the format will be.
|
||||
*/
|
||||
if (MQTTPacket_checkVersion(&Protocol, version))
|
||||
{
|
||||
flags.all = readChar(&curdata);
|
||||
data->cleansession = flags.bits.cleansession;
|
||||
data->keepAliveInterval = readInt(&curdata);
|
||||
if (!readMQTTLenString(&data->clientID, &curdata, enddata))
|
||||
goto exit;
|
||||
data->willFlag = flags.bits.will;
|
||||
if (flags.bits.will)
|
||||
{
|
||||
data->will.qos = flags.bits.willQoS;
|
||||
data->will.retained = flags.bits.willRetain;
|
||||
if (!readMQTTLenString(&data->will.topicName, &curdata, enddata) ||
|
||||
!readMQTTLenString(&data->will.message, &curdata, enddata))
|
||||
goto exit;
|
||||
}
|
||||
if (flags.bits.username)
|
||||
{
|
||||
if (enddata - curdata < 3 || !readMQTTLenString(&data->username, &curdata, enddata))
|
||||
goto exit; /* username flag set, but no username supplied - invalid */
|
||||
if (flags.bits.password &&
|
||||
(enddata - curdata < 3 || !readMQTTLenString(&data->password, &curdata, enddata)))
|
||||
goto exit; /* password flag set, but no password supplied - invalid */
|
||||
}
|
||||
else if (flags.bits.password)
|
||||
goto exit; /* password flag set without username - invalid */
|
||||
rc = 1;
|
||||
}
|
||||
exit:
|
||||
FUNC_EXIT_RC(rc);
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Serializes the connack packet into the supplied buffer.
|
||||
* @param buf the buffer into which the packet will be serialized
|
||||
* @param buflen the length in bytes of the supplied buffer
|
||||
* @param connack_rc the integer connack return code to be used
|
||||
* @param sessionPresent the MQTT 3.1.1 sessionPresent flag
|
||||
* @return serialized length, or error if 0
|
||||
*/
|
||||
int MQTTSerialize_connack(unsigned char* buf, int buflen, unsigned char connack_rc, unsigned char sessionPresent)
|
||||
{
|
||||
MQTTHeader header = {0};
|
||||
int rc = 0;
|
||||
unsigned char *ptr = buf;
|
||||
MQTTConnackFlags flags = {0};
|
||||
|
||||
FUNC_ENTRY;
|
||||
if (buflen < 2)
|
||||
{
|
||||
rc = MQTTPACKET_BUFFER_TOO_SHORT;
|
||||
goto exit;
|
||||
}
|
||||
header.byte = 0;
|
||||
header.bits.type = CONNACK;
|
||||
writeChar(&ptr, header.byte); /* write header */
|
||||
|
||||
ptr += MQTTPacket_encode(ptr, 2); /* write remaining length */
|
||||
|
||||
flags.all = 0;
|
||||
flags.bits.sessionpresent = sessionPresent;
|
||||
writeChar(&ptr, flags.all);
|
||||
writeChar(&ptr, connack_rc);
|
||||
|
||||
rc = ptr - buf;
|
||||
exit:
|
||||
FUNC_EXIT_RC(rc);
|
||||
return rc;
|
||||
}
|
||||
|
||||
107
vd960DBN/ETH/MQTT_SRC/MQTTDeserializePublish.c
Normal file
107
vd960DBN/ETH/MQTT_SRC/MQTTDeserializePublish.c
Normal file
@@ -0,0 +1,107 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2014 IBM Corp.
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
* and the Eclipse Distribution License is available at
|
||||
* http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
*
|
||||
* Contributors:
|
||||
* Ian Craggs - initial API and implementation and/or initial documentation
|
||||
*******************************************************************************/
|
||||
|
||||
#include "StackTrace.h"
|
||||
#include "MQTTPacket.h"
|
||||
#include <string.h>
|
||||
|
||||
#define min(a, b) ((a < b) ? 1 : 0)
|
||||
|
||||
/**
|
||||
* Deserializes the supplied (wire) buffer into publish data
|
||||
* @param dup returned integer - the MQTT dup flag
|
||||
* @param qos returned integer - the MQTT QoS value
|
||||
* @param retained returned integer - the MQTT retained flag
|
||||
* @param packetid returned integer - the MQTT packet identifier
|
||||
* @param topicName returned MQTTString - the MQTT topic in the publish
|
||||
* @param payload returned byte buffer - the MQTT publish payload
|
||||
* @param payloadlen returned integer - the length of the MQTT payload
|
||||
* @param buf the raw buffer data, of the correct length determined by the remaining length field
|
||||
* @param buflen the length in bytes of the data in the supplied buffer
|
||||
* @return error code. 1 is success
|
||||
*/
|
||||
int MQTTDeserialize_publish(unsigned char* dup, int* qos, unsigned char* retained, unsigned short* packetid, MQTTString* topicName,
|
||||
unsigned char** payload, int* payloadlen, unsigned char* buf, int buflen)
|
||||
{
|
||||
MQTTHeader header = {0};
|
||||
unsigned char* curdata = buf;
|
||||
unsigned char* enddata = NULL;
|
||||
int rc = 0;
|
||||
int mylen = 0;
|
||||
|
||||
FUNC_ENTRY;
|
||||
header.byte = readChar(&curdata);
|
||||
if (header.bits.type != PUBLISH)
|
||||
goto exit;
|
||||
*dup = header.bits.dup;
|
||||
*qos = header.bits.qos;
|
||||
*retained = header.bits.retain;
|
||||
|
||||
curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */
|
||||
enddata = curdata + mylen;
|
||||
|
||||
if (!readMQTTLenString(topicName, &curdata, enddata) ||
|
||||
enddata - curdata < 0) /* do we have enough data to read the protocol version byte? */
|
||||
goto exit;
|
||||
|
||||
if (*qos > 0)
|
||||
*packetid = readInt(&curdata);
|
||||
|
||||
*payloadlen = enddata - curdata;
|
||||
*payload = curdata;
|
||||
rc = 1;
|
||||
exit:
|
||||
FUNC_EXIT_RC(rc);
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Deserializes the supplied (wire) buffer into an ack
|
||||
* @param packettype returned integer - the MQTT packet type
|
||||
* @param dup returned integer - the MQTT dup flag
|
||||
* @param packetid returned integer - the MQTT packet identifier
|
||||
* @param buf the raw buffer data, of the correct length determined by the remaining length field
|
||||
* @param buflen the length in bytes of the data in the supplied buffer
|
||||
* @return error code. 1 is success, 0 is failure
|
||||
*/
|
||||
int MQTTDeserialize_ack(unsigned char* packettype, unsigned char* dup, unsigned short* packetid, unsigned char* buf, int buflen)
|
||||
{
|
||||
MQTTHeader header = {0};
|
||||
unsigned char* curdata = buf;
|
||||
unsigned char* enddata = NULL;
|
||||
int rc = 0;
|
||||
int mylen;
|
||||
|
||||
FUNC_ENTRY;
|
||||
header.byte = readChar(&curdata);
|
||||
*dup = header.bits.dup;
|
||||
*packettype = header.bits.type;
|
||||
|
||||
curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */
|
||||
enddata = curdata + mylen;
|
||||
|
||||
if (enddata - curdata < 2)
|
||||
goto exit;
|
||||
*packetid = readInt(&curdata);
|
||||
|
||||
rc = 1;
|
||||
exit:
|
||||
FUNC_EXIT_RC(rc);
|
||||
return rc;
|
||||
}
|
||||
|
||||
258
vd960DBN/ETH/MQTT_SRC/MQTTFormat.c
Normal file
258
vd960DBN/ETH/MQTT_SRC/MQTTFormat.c
Normal file
@@ -0,0 +1,258 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2014 IBM Corp.
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
* and the Eclipse Distribution License is available at
|
||||
* http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
*
|
||||
* Contributors:
|
||||
* Ian Craggs - initial API and implementation and/or initial documentation
|
||||
*******************************************************************************/
|
||||
|
||||
#include "StackTrace.h"
|
||||
#include "MQTTPacket.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
|
||||
const char* MQTTPacket_names[] =
|
||||
{
|
||||
"RESERVED", "CONNECT", "CONNACK", "PUBLISH", "PUBACK", "PUBREC", "PUBREL",
|
||||
"PUBCOMP", "SUBSCRIBE", "SUBACK", "UNSUBSCRIBE", "UNSUBACK",
|
||||
"PINGREQ", "PINGRESP", "DISCONNECT"
|
||||
};
|
||||
|
||||
|
||||
const char* MQTTPacket_getName(unsigned short packetid)
|
||||
{
|
||||
return MQTTPacket_names[packetid];
|
||||
}
|
||||
|
||||
|
||||
int MQTTStringFormat_connect(char* strbuf, int strbuflen, MQTTPacket_connectData* data)
|
||||
{
|
||||
int strindex = 0;
|
||||
|
||||
strindex = snprintf(strbuf, strbuflen,
|
||||
"CONNECT MQTT version %d, client id %.*s, clean session %d, keep alive %d",
|
||||
(int)data->MQTTVersion, data->clientID.lenstring.len, data->clientID.lenstring.data,
|
||||
(int)data->cleansession, data->keepAliveInterval);
|
||||
if (data->willFlag)
|
||||
strindex += snprintf(&strbuf[strindex], strbuflen - strindex,
|
||||
", will QoS %d, will retain %d, will topic %.*s, will message %.*s",
|
||||
data->will.qos, data->will.retained,
|
||||
data->will.topicName.lenstring.len, data->will.topicName.lenstring.data,
|
||||
data->will.message.lenstring.len, data->will.message.lenstring.data);
|
||||
if (data->username.lenstring.data && data->username.lenstring.len > 0)
|
||||
strindex += snprintf(&strbuf[strindex], strbuflen - strindex,
|
||||
", user name %.*s", data->username.lenstring.len, data->username.lenstring.data);
|
||||
if (data->password.lenstring.data && data->password.lenstring.len > 0)
|
||||
strindex += snprintf(&strbuf[strindex], strbuflen - strindex,
|
||||
", password %.*s", data->password.lenstring.len, data->password.lenstring.data);
|
||||
return strindex;
|
||||
}
|
||||
|
||||
|
||||
int MQTTStringFormat_connack(char* strbuf, int strbuflen, unsigned char connack_rc, unsigned char sessionPresent)
|
||||
{
|
||||
int strindex = snprintf(strbuf, strbuflen, "CONNACK session present %d, rc %d", sessionPresent, connack_rc);
|
||||
return strindex;
|
||||
}
|
||||
|
||||
|
||||
int MQTTStringFormat_publish(char* strbuf, int strbuflen, unsigned char dup, int qos, unsigned char retained,
|
||||
unsigned short packetid, MQTTString topicName, unsigned char* payload, int payloadlen)
|
||||
{
|
||||
int strindex = snprintf(strbuf, strbuflen,
|
||||
"PUBLISH dup %d, QoS %d, retained %d, packet id %d, topic %.*s, payload length %d, payload %.*s",
|
||||
dup, qos, retained, packetid,
|
||||
(topicName.lenstring.len < 20) ? topicName.lenstring.len : 20, topicName.lenstring.data,
|
||||
payloadlen, (payloadlen < 20) ? payloadlen : 20, payload);
|
||||
return strindex;
|
||||
}
|
||||
|
||||
|
||||
int MQTTStringFormat_ack(char* strbuf, int strbuflen, unsigned char packettype, unsigned char dup, unsigned short packetid)
|
||||
{
|
||||
int strindex = snprintf(strbuf, strbuflen, "%s, packet id %d", MQTTPacket_names[packettype], packetid);
|
||||
if (dup)
|
||||
strindex += snprintf(strbuf + strindex, strbuflen - strindex, ", dup %d", dup);
|
||||
return strindex;
|
||||
}
|
||||
|
||||
|
||||
int MQTTStringFormat_subscribe(char* strbuf, int strbuflen, unsigned char dup, unsigned short packetid, int count,
|
||||
MQTTString topicFilters[], int requestedQoSs[])
|
||||
{
|
||||
return snprintf(strbuf, strbuflen,
|
||||
"SUBSCRIBE dup %d, packet id %d count %d topic %.*s qos %d",
|
||||
dup, packetid, count,
|
||||
topicFilters[0].lenstring.len, topicFilters[0].lenstring.data,
|
||||
requestedQoSs[0]);
|
||||
}
|
||||
|
||||
|
||||
int MQTTStringFormat_suback(char* strbuf, int strbuflen, unsigned short packetid, int count, int* grantedQoSs)
|
||||
{
|
||||
return snprintf(strbuf, strbuflen,
|
||||
"SUBACK packet id %d count %d granted qos %d", packetid, count, grantedQoSs[0]);
|
||||
}
|
||||
|
||||
|
||||
int MQTTStringFormat_unsubscribe(char* strbuf, int strbuflen, unsigned char dup, unsigned short packetid,
|
||||
int count, MQTTString topicFilters[])
|
||||
{
|
||||
return snprintf(strbuf, strbuflen,
|
||||
"UNSUBSCRIBE dup %d, packet id %d count %d topic %.*s",
|
||||
dup, packetid, count,
|
||||
topicFilters[0].lenstring.len, topicFilters[0].lenstring.data);
|
||||
}
|
||||
|
||||
|
||||
char* MQTTFormat_toClientString(char* strbuf, int strbuflen, unsigned char* buf, int buflen)
|
||||
{
|
||||
int index = 0;
|
||||
int rem_length = 0;
|
||||
MQTTHeader header = {0};
|
||||
int strindex = 0;
|
||||
|
||||
header.byte = buf[index++];
|
||||
index += MQTTPacket_decodeBuf(&buf[index], &rem_length);
|
||||
|
||||
switch (header.bits.type)
|
||||
{
|
||||
case CONNACK:
|
||||
{
|
||||
unsigned char sessionPresent, connack_rc;
|
||||
if (MQTTDeserialize_connack(&sessionPresent, &connack_rc, buf, buflen) == 1)
|
||||
strindex = MQTTStringFormat_connack(strbuf, strbuflen, connack_rc, sessionPresent);
|
||||
}
|
||||
break;
|
||||
case PUBLISH:
|
||||
{
|
||||
unsigned char dup, retained, *payload;
|
||||
unsigned short packetid;
|
||||
int qos, payloadlen;
|
||||
MQTTString topicName = MQTTString_initializer;
|
||||
if (MQTTDeserialize_publish(&dup, &qos, &retained, &packetid, &topicName,
|
||||
&payload, &payloadlen, buf, buflen) == 1)
|
||||
strindex = MQTTStringFormat_publish(strbuf, strbuflen, dup, qos, retained, packetid,
|
||||
topicName, payload, payloadlen);
|
||||
}
|
||||
break;
|
||||
case PUBACK:
|
||||
case PUBREC:
|
||||
case PUBREL:
|
||||
case PUBCOMP:
|
||||
{
|
||||
unsigned char packettype, dup;
|
||||
unsigned short packetid;
|
||||
if (MQTTDeserialize_ack(&packettype, &dup, &packetid, buf, buflen) == 1)
|
||||
strindex = MQTTStringFormat_ack(strbuf, strbuflen, packettype, dup, packetid);
|
||||
}
|
||||
break;
|
||||
case SUBACK:
|
||||
{
|
||||
unsigned short packetid;
|
||||
int maxcount = 1, count = 0;
|
||||
int grantedQoSs[1];
|
||||
if (MQTTDeserialize_suback(&packetid, maxcount, &count, grantedQoSs, buf, buflen) == 1)
|
||||
strindex = MQTTStringFormat_suback(strbuf, strbuflen, packetid, count, grantedQoSs);
|
||||
}
|
||||
break;
|
||||
case UNSUBACK:
|
||||
{
|
||||
unsigned short packetid;
|
||||
if (MQTTDeserialize_unsuback(&packetid, buf, buflen) == 1)
|
||||
strindex = MQTTStringFormat_ack(strbuf, strbuflen, UNSUBACK, 0, packetid);
|
||||
}
|
||||
break;
|
||||
case PINGREQ:
|
||||
case PINGRESP:
|
||||
case DISCONNECT:
|
||||
strindex = snprintf(strbuf, strbuflen, "%s", MQTTPacket_names[header.bits.type]);
|
||||
break;
|
||||
}
|
||||
return strbuf;
|
||||
}
|
||||
|
||||
|
||||
char* MQTTFormat_toServerString(char* strbuf, int strbuflen, unsigned char* buf, int buflen)
|
||||
{
|
||||
int index = 0;
|
||||
int rem_length = 0;
|
||||
MQTTHeader header = {0};
|
||||
int strindex = 0;
|
||||
|
||||
header.byte = buf[index++];
|
||||
index += MQTTPacket_decodeBuf(&buf[index], &rem_length);
|
||||
|
||||
switch (header.bits.type)
|
||||
{
|
||||
case CONNECT:
|
||||
{
|
||||
MQTTPacket_connectData data;
|
||||
int rc;
|
||||
if ((rc = MQTTDeserialize_connect(&data, buf, buflen)) == 1)
|
||||
strindex = MQTTStringFormat_connect(strbuf, strbuflen, &data);
|
||||
}
|
||||
break;
|
||||
case PUBLISH:
|
||||
{
|
||||
unsigned char dup, retained, *payload;
|
||||
unsigned short packetid;
|
||||
int qos, payloadlen;
|
||||
MQTTString topicName = MQTTString_initializer;
|
||||
if (MQTTDeserialize_publish(&dup, &qos, &retained, &packetid, &topicName,
|
||||
&payload, &payloadlen, buf, buflen) == 1)
|
||||
strindex = MQTTStringFormat_publish(strbuf, strbuflen, dup, qos, retained, packetid,
|
||||
topicName, payload, payloadlen);
|
||||
}
|
||||
break;
|
||||
case PUBACK:
|
||||
case PUBREC:
|
||||
case PUBREL:
|
||||
case PUBCOMP:
|
||||
{
|
||||
unsigned char packettype, dup;
|
||||
unsigned short packetid;
|
||||
if (MQTTDeserialize_ack(&packettype, &dup, &packetid, buf, buflen) == 1)
|
||||
strindex = MQTTStringFormat_ack(strbuf, strbuflen, packettype, dup, packetid);
|
||||
}
|
||||
break;
|
||||
case SUBSCRIBE:
|
||||
{
|
||||
unsigned char dup;
|
||||
unsigned short packetid;
|
||||
int maxcount = 1, count = 0;
|
||||
MQTTString topicFilters[1];
|
||||
int requestedQoSs[1];
|
||||
if (MQTTDeserialize_subscribe(&dup, &packetid, maxcount, &count,
|
||||
topicFilters, requestedQoSs, buf, buflen) == 1)
|
||||
strindex = MQTTStringFormat_subscribe(strbuf, strbuflen, dup, packetid, count, topicFilters, requestedQoSs);;
|
||||
}
|
||||
break;
|
||||
case UNSUBSCRIBE:
|
||||
{
|
||||
unsigned char dup;
|
||||
unsigned short packetid;
|
||||
int maxcount = 1, count = 0;
|
||||
MQTTString topicFilters[1];
|
||||
if (MQTTDeserialize_unsubscribe(&dup, &packetid, maxcount, &count, topicFilters, buf, buflen) == 1)
|
||||
strindex = MQTTStringFormat_unsubscribe(strbuf, strbuflen, dup, packetid, count, topicFilters);
|
||||
}
|
||||
break;
|
||||
case PINGREQ:
|
||||
case PINGRESP:
|
||||
case DISCONNECT:
|
||||
strindex = snprintf(strbuf, strbuflen, "%s", MQTTPacket_names[header.bits.type]);
|
||||
break;
|
||||
}
|
||||
strbuf[strbuflen] = '\0';
|
||||
return strbuf;
|
||||
}
|
||||
37
vd960DBN/ETH/MQTT_SRC/MQTTFormat.h
Normal file
37
vd960DBN/ETH/MQTT_SRC/MQTTFormat.h
Normal file
@@ -0,0 +1,37 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2014 IBM Corp.
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
* and the Eclipse Distribution License is available at
|
||||
* http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
*
|
||||
* Contributors:
|
||||
* Ian Craggs - initial API and implementation and/or initial documentation
|
||||
*******************************************************************************/
|
||||
|
||||
#if !defined(MQTTFORMAT_H)
|
||||
#define MQTTFORMAT_H
|
||||
|
||||
#include "StackTrace.h"
|
||||
#include "MQTTPacket.h"
|
||||
|
||||
const char* MQTTPacket_getName(unsigned short packetid);
|
||||
int MQTTStringFormat_connect(char* strbuf, int strbuflen, MQTTPacket_connectData* data);
|
||||
int MQTTStringFormat_connack(char* strbuf, int strbuflen, unsigned char connack_rc, unsigned char sessionPresent);
|
||||
int MQTTStringFormat_publish(char* strbuf, int strbuflen, unsigned char dup, int qos, unsigned char retained,
|
||||
unsigned short packetid, MQTTString topicName, unsigned char* payload, int payloadlen);
|
||||
int MQTTStringFormat_ack(char* strbuf, int strbuflen, unsigned char packettype, unsigned char dup, unsigned short packetid);
|
||||
int MQTTStringFormat_subscribe(char* strbuf, int strbuflen, unsigned char dup, unsigned short packetid, int count,
|
||||
MQTTString topicFilters[], int requestedQoSs[]);
|
||||
int MQTTStringFormat_suback(char* strbuf, int strbuflen, unsigned short packetid, int count, int* grantedQoSs);
|
||||
int MQTTStringFormat_unsubscribe(char* strbuf, int strbuflen, unsigned char dup, unsigned short packetid,
|
||||
int count, MQTTString topicFilters[]);
|
||||
char* MQTTFormat_toClientString(char* strbuf, int strbuflen, unsigned char* buf, int buflen);
|
||||
char* MQTTFormat_toServerString(char* strbuf, int strbuflen, unsigned char* buf, int buflen);
|
||||
|
||||
#endif
|
||||
412
vd960DBN/ETH/MQTT_SRC/MQTTPacket.c
Normal file
412
vd960DBN/ETH/MQTT_SRC/MQTTPacket.c
Normal file
@@ -0,0 +1,412 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2014 IBM Corp.
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
* and the Eclipse Distribution License is available at
|
||||
* http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
*
|
||||
* Contributors:
|
||||
* Ian Craggs - initial API and implementation and/or initial documentation
|
||||
* Sergio R. Caprile - non-blocking packet read functions for stream transport
|
||||
*******************************************************************************/
|
||||
|
||||
#include "StackTrace.h"
|
||||
#include "MQTTPacket.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
/**
|
||||
* Encodes the message length according to the MQTT algorithm
|
||||
* @param buf the buffer into which the encoded data is written
|
||||
* @param length the length to be encoded
|
||||
* @return the number of bytes written to buffer
|
||||
*/
|
||||
int MQTTPacket_encode(unsigned char* buf, int length)
|
||||
{
|
||||
int rc = 0;
|
||||
|
||||
FUNC_ENTRY;
|
||||
do
|
||||
{
|
||||
char d = length % 128;
|
||||
length /= 128;
|
||||
/* if there are more digits to encode, set the top bit of this digit */
|
||||
if (length > 0)
|
||||
d |= 0x80;
|
||||
buf[rc++] = d;
|
||||
} while (length > 0);
|
||||
FUNC_EXIT_RC(rc);
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Decodes the message length according to the MQTT algorithm
|
||||
* @param getcharfn pointer to function to read the next character from the data source
|
||||
* @param value the decoded length returned
|
||||
* @return the number of bytes read from the socket
|
||||
*/
|
||||
int MQTTPacket_decode(int (*getcharfn)(unsigned char*, int), int* value)
|
||||
{
|
||||
unsigned char c;
|
||||
int multiplier = 1;
|
||||
int len = 0;
|
||||
#define MAX_NO_OF_REMAINING_LENGTH_BYTES 4
|
||||
|
||||
FUNC_ENTRY;
|
||||
*value = 0;
|
||||
do
|
||||
{
|
||||
int rc = MQTTPACKET_READ_ERROR;
|
||||
|
||||
if (++len > MAX_NO_OF_REMAINING_LENGTH_BYTES)
|
||||
{
|
||||
rc = MQTTPACKET_READ_ERROR; /* bad data */
|
||||
goto exit;
|
||||
}
|
||||
rc = (*getcharfn)(&c, 1);
|
||||
if (rc != 1)
|
||||
goto exit;
|
||||
*value += (c & 127) * multiplier;
|
||||
multiplier *= 128;
|
||||
} while ((c & 128) != 0);
|
||||
exit:
|
||||
FUNC_EXIT_RC(len);
|
||||
return len;
|
||||
}
|
||||
|
||||
|
||||
int MQTTPacket_len(int rem_len)
|
||||
{
|
||||
rem_len += 1; /* header byte */
|
||||
|
||||
/* now remaining_length field */
|
||||
if (rem_len < 128)
|
||||
rem_len += 1;
|
||||
else if (rem_len < 16384)
|
||||
rem_len += 2;
|
||||
else if (rem_len < 2097151)
|
||||
rem_len += 3;
|
||||
else
|
||||
rem_len += 4;
|
||||
return rem_len;
|
||||
}
|
||||
|
||||
|
||||
static unsigned char* bufptr;
|
||||
|
||||
int bufchar(unsigned char* c, int count)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < count; ++i)
|
||||
*c = *bufptr++;
|
||||
return count;
|
||||
}
|
||||
|
||||
|
||||
int MQTTPacket_decodeBuf(unsigned char* buf, int* value)
|
||||
{
|
||||
bufptr = buf;
|
||||
return MQTTPacket_decode(bufchar, value);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Calculates an integer from two bytes read from the input buffer
|
||||
* @param pptr pointer to the input buffer - incremented by the number of bytes used & returned
|
||||
* @return the integer value calculated
|
||||
*/
|
||||
int readInt(unsigned char** pptr)
|
||||
{
|
||||
unsigned char* ptr = *pptr;
|
||||
int len = 256*(*ptr) + (*(ptr+1));
|
||||
*pptr += 2;
|
||||
return len;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Reads one character from the input buffer.
|
||||
* @param pptr pointer to the input buffer - incremented by the number of bytes used & returned
|
||||
* @return the character read
|
||||
*/
|
||||
char readChar(unsigned char** pptr)
|
||||
{
|
||||
char c = **pptr;
|
||||
(*pptr)++;
|
||||
return c;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Writes one character to an output buffer.
|
||||
* @param pptr pointer to the output buffer - incremented by the number of bytes used & returned
|
||||
* @param c the character to write
|
||||
*/
|
||||
void writeChar(unsigned char** pptr, char c)
|
||||
{
|
||||
**pptr = c;
|
||||
(*pptr)++;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Writes an integer as 2 bytes to an output buffer.
|
||||
* @param pptr pointer to the output buffer - incremented by the number of bytes used & returned
|
||||
* @param anInt the integer to write
|
||||
*/
|
||||
void writeInt(unsigned char** pptr, int anInt)
|
||||
{
|
||||
**pptr = (unsigned char)(anInt / 256);
|
||||
(*pptr)++;
|
||||
**pptr = (unsigned char)(anInt % 256);
|
||||
(*pptr)++;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Writes a "UTF" string to an output buffer. Converts C string to length-delimited.
|
||||
* @param pptr pointer to the output buffer - incremented by the number of bytes used & returned
|
||||
* @param string the C string to write
|
||||
*/
|
||||
void writeCString(unsigned char** pptr, const char* string)
|
||||
{
|
||||
int len = strlen(string);
|
||||
writeInt(pptr, len);
|
||||
memcpy(*pptr, string, len);
|
||||
*pptr += len;
|
||||
}
|
||||
|
||||
|
||||
int getLenStringLen(char* ptr)
|
||||
{
|
||||
int len = 256*((unsigned char)(*ptr)) + (unsigned char)(*(ptr+1));
|
||||
return len;
|
||||
}
|
||||
|
||||
|
||||
void writeMQTTString(unsigned char** pptr, MQTTString mqttstring)
|
||||
{
|
||||
if (mqttstring.lenstring.len > 0)
|
||||
{
|
||||
writeInt(pptr, mqttstring.lenstring.len);
|
||||
memcpy(*pptr, mqttstring.lenstring.data, mqttstring.lenstring.len);
|
||||
*pptr += mqttstring.lenstring.len;
|
||||
}
|
||||
else if (mqttstring.cstring)
|
||||
writeCString(pptr, mqttstring.cstring);
|
||||
else
|
||||
writeInt(pptr, 0);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param mqttstring the MQTTString structure into which the data is to be read
|
||||
* @param pptr pointer to the output buffer - incremented by the number of bytes used & returned
|
||||
* @param enddata pointer to the end of the data: do not read beyond
|
||||
* @return 1 if successful, 0 if not
|
||||
*/
|
||||
int readMQTTLenString(MQTTString* mqttstring, unsigned char** pptr, unsigned char* enddata)
|
||||
{
|
||||
int rc = 0;
|
||||
|
||||
FUNC_ENTRY;
|
||||
/* the first two bytes are the length of the string */
|
||||
if (enddata - (*pptr) > 1) /* enough length to read the integer? */
|
||||
{
|
||||
mqttstring->lenstring.len = readInt(pptr); /* increments pptr to point past length */
|
||||
if (&(*pptr)[mqttstring->lenstring.len] <= enddata)
|
||||
{
|
||||
mqttstring->lenstring.data = (char*)*pptr;
|
||||
*pptr += mqttstring->lenstring.len;
|
||||
rc = 1;
|
||||
}
|
||||
}
|
||||
mqttstring->cstring = NULL;
|
||||
FUNC_EXIT_RC(rc);
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return the length of the MQTTstring - C string if there is one, otherwise the length delimited string
|
||||
* @param mqttstring the string to return the length of
|
||||
* @return the length of the string
|
||||
*/
|
||||
int MQTTstrlen(MQTTString mqttstring)
|
||||
{
|
||||
int rc = 0;
|
||||
|
||||
if (mqttstring.cstring)
|
||||
rc = strlen(mqttstring.cstring);
|
||||
else
|
||||
rc = mqttstring.lenstring.len;
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Compares an MQTTString to a C string
|
||||
* @param a the MQTTString to compare
|
||||
* @param bptr the C string to compare
|
||||
* @return boolean - equal or not
|
||||
*/
|
||||
int MQTTPacket_equals(MQTTString* a, char* bptr)
|
||||
{
|
||||
int alen = 0,
|
||||
blen = 0;
|
||||
char *aptr;
|
||||
|
||||
if (a->cstring)
|
||||
{
|
||||
aptr = a->cstring;
|
||||
alen = strlen(a->cstring);
|
||||
}
|
||||
else
|
||||
{
|
||||
aptr = a->lenstring.data;
|
||||
alen = a->lenstring.len;
|
||||
}
|
||||
blen = strlen(bptr);
|
||||
|
||||
return (alen == blen) && (strncmp(aptr, bptr, alen) == 0);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Helper function to read packet data from some source into a buffer
|
||||
* @param buf the buffer into which the packet will be serialized
|
||||
* @param buflen the length in bytes of the supplied buffer
|
||||
* @param getfn pointer to a function which will read any number of bytes from the needed source
|
||||
* @return integer MQTT packet type, or -1 on error
|
||||
* @note the whole message must fit into the caller's buffer
|
||||
*/
|
||||
int MQTTPacket_read(unsigned char* buf, int buflen, int (*getfn)(unsigned char*, int))
|
||||
{
|
||||
int rc = -1;
|
||||
MQTTHeader header = {0};
|
||||
int len = 0;
|
||||
int rem_len = 0;
|
||||
int temp = 0; //add by wfq 2016-11-17
|
||||
|
||||
/* 1. read the header byte. This has the packet type in it */
|
||||
if ((*getfn)(buf, 1) != 1)
|
||||
goto exit;
|
||||
|
||||
len = 1;
|
||||
/* 2. read the remaining length. This is variable in itself */
|
||||
temp = MQTTPacket_decode(getfn, &rem_len);
|
||||
// printf("\r\nMQTTPacket_decode, len:%d, rem_len:%d\r\n", temp, rem_len);
|
||||
len += MQTTPacket_encode(buf + 1, rem_len); /* put the original remaining length back into the buffer */
|
||||
// printf("\r\nrem_len:%d,len:%d, buflen:%d\r\n", rem_len, len, buflen);
|
||||
/* 3. read the rest of the buffer using a callback to supply the rest of the data */
|
||||
if((rem_len + len) > buflen)
|
||||
goto exit;
|
||||
if ((*getfn)(buf + len, rem_len) != rem_len)
|
||||
goto exit;
|
||||
|
||||
header.byte = buf[0];
|
||||
rc = header.bits.type;
|
||||
exit:
|
||||
return rc;
|
||||
}
|
||||
|
||||
/**
|
||||
* Decodes the message length according to the MQTT algorithm, non-blocking
|
||||
* @param trp pointer to a transport structure holding what is needed to solve getting data from it
|
||||
* @param value the decoded length returned
|
||||
* @return integer the number of bytes read from the socket, 0 for call again, or -1 on error
|
||||
*/
|
||||
static int MQTTPacket_decodenb(MQTTTransport *trp)
|
||||
{
|
||||
unsigned char c;
|
||||
int rc = MQTTPACKET_READ_ERROR;
|
||||
|
||||
FUNC_ENTRY;
|
||||
if(trp->len == 0){ /* initialize on first call */
|
||||
trp->multiplier = 1;
|
||||
trp->rem_len = 0;
|
||||
}
|
||||
do {
|
||||
int frc;
|
||||
if (++(trp->len) > MAX_NO_OF_REMAINING_LENGTH_BYTES)
|
||||
goto exit;
|
||||
if ((frc=(*trp->getfn)(trp->sck, &c, 1)) == -1)
|
||||
goto exit;
|
||||
if (frc == 0){
|
||||
rc = 0;
|
||||
goto exit;
|
||||
}
|
||||
trp->rem_len += (c & 127) * trp->multiplier;
|
||||
trp->multiplier *= 128;
|
||||
} while ((c & 128) != 0);
|
||||
rc = trp->len;
|
||||
exit:
|
||||
FUNC_EXIT_RC(rc);
|
||||
return rc;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to read packet data from some source into a buffer, non-blocking
|
||||
* @param buf the buffer into which the packet will be serialized
|
||||
* @param buflen the length in bytes of the supplied buffer
|
||||
* @param trp pointer to a transport structure holding what is needed to solve getting data from it
|
||||
* @return integer MQTT packet type, 0 for call again, or -1 on error
|
||||
* @note the whole message must fit into the caller's buffer
|
||||
*/
|
||||
int MQTTPacket_readnb(unsigned char* buf, int buflen, MQTTTransport *trp)
|
||||
{
|
||||
int rc = -1, frc;
|
||||
MQTTHeader header = {0};
|
||||
|
||||
switch(trp->state){
|
||||
default:
|
||||
trp->state = 0;
|
||||
/*FALLTHROUGH*/
|
||||
case 0:
|
||||
/* read the header byte. This has the packet type in it */
|
||||
if ((frc=(*trp->getfn)(trp->sck, buf, 1)) == -1)
|
||||
goto exit;
|
||||
if (frc == 0)
|
||||
return 0;
|
||||
trp->len = 0;
|
||||
++trp->state;
|
||||
/*FALLTHROUGH*/
|
||||
/* read the remaining length. This is variable in itself */
|
||||
case 1:
|
||||
if((frc=MQTTPacket_decodenb(trp)) == MQTTPACKET_READ_ERROR)
|
||||
goto exit;
|
||||
if(frc == 0)
|
||||
return 0;
|
||||
trp->len = 1 + MQTTPacket_encode(buf + 1, trp->rem_len); /* put the original remaining length back into the buffer */
|
||||
if((trp->rem_len + trp->len) > buflen)
|
||||
goto exit;
|
||||
++trp->state;
|
||||
/*FALLTHROUGH*/
|
||||
case 2:
|
||||
/* read the rest of the buffer using a callback to supply the rest of the data */
|
||||
if ((frc=(*trp->getfn)(trp->sck, buf + trp->len, trp->rem_len)) == -1)
|
||||
goto exit;
|
||||
if (frc == 0)
|
||||
return 0;
|
||||
trp->rem_len -= frc;
|
||||
trp->len += frc;
|
||||
if(trp->rem_len)
|
||||
return 0;
|
||||
|
||||
header.byte = buf[0];
|
||||
rc = header.bits.type;
|
||||
break;
|
||||
}
|
||||
|
||||
exit:
|
||||
trp->state = 0;
|
||||
return rc;
|
||||
}
|
||||
|
||||
133
vd960DBN/ETH/MQTT_SRC/MQTTPacket.h
Normal file
133
vd960DBN/ETH/MQTT_SRC/MQTTPacket.h
Normal file
@@ -0,0 +1,133 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2014 IBM Corp.
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
* and the Eclipse Distribution License is available at
|
||||
* http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
*
|
||||
* Contributors:
|
||||
* Ian Craggs - initial API and implementation and/or initial documentation
|
||||
* Xiang Rong - 442039 Add makefile to Embedded C client
|
||||
*******************************************************************************/
|
||||
|
||||
#ifndef MQTTPACKET_H_
|
||||
#define MQTTPACKET_H_
|
||||
|
||||
#if defined(__cplusplus) /* If this is a C++ compiler, use C linkage */
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#if defined(WIN32_DLL) || defined(WIN64_DLL)
|
||||
#define DLLImport __declspec(dllimport)
|
||||
#define DLLExport __declspec(dllexport)
|
||||
#elif defined(LINUX_SO)
|
||||
#define DLLImport extern
|
||||
#define DLLExport __attribute__ ((visibility ("default")))
|
||||
#else
|
||||
#define DLLImport
|
||||
#define DLLExport
|
||||
#endif
|
||||
|
||||
enum errors
|
||||
{
|
||||
MQTTPACKET_BUFFER_TOO_SHORT = -2,
|
||||
MQTTPACKET_READ_ERROR = -1,
|
||||
MQTTPACKET_READ_COMPLETE
|
||||
};
|
||||
|
||||
enum msgTypes
|
||||
{
|
||||
CONNECT = 1, CONNACK, PUBLISH, PUBACK, PUBREC, PUBREL,
|
||||
PUBCOMP, SUBSCRIBE, SUBACK, UNSUBSCRIBE, UNSUBACK,
|
||||
PINGREQ, PINGRESP, DISCONNECT
|
||||
};
|
||||
|
||||
/**
|
||||
* Bitfields for the MQTT header byte.
|
||||
*/
|
||||
typedef union
|
||||
{
|
||||
unsigned char byte; /**< the whole byte */
|
||||
#if defined(REVERSED)
|
||||
struct
|
||||
{
|
||||
unsigned int type : 4; /**< message type nibble */
|
||||
unsigned int dup : 1; /**< DUP flag bit */
|
||||
unsigned int qos : 2; /**< QoS value, 0, 1 or 2 */
|
||||
unsigned int retain : 1; /**< retained flag bit */
|
||||
} bits;
|
||||
#else
|
||||
struct
|
||||
{
|
||||
unsigned int retain : 1; /**< retained flag bit */
|
||||
unsigned int qos : 2; /**< QoS value, 0, 1 or 2 */
|
||||
unsigned int dup : 1; /**< DUP flag bit */
|
||||
unsigned int type : 4; /**< message type nibble */
|
||||
} bits;
|
||||
#endif
|
||||
} MQTTHeader;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
int len;
|
||||
char* data;
|
||||
} MQTTLenString;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
char* cstring;
|
||||
MQTTLenString lenstring;
|
||||
} MQTTString;
|
||||
|
||||
#define MQTTString_initializer {NULL, {0, NULL}}
|
||||
|
||||
int MQTTstrlen(MQTTString mqttstring);
|
||||
|
||||
#include "MQTTConnect.h"
|
||||
#include "MQTTPublish.h"
|
||||
#include "MQTTSubscribe.h"
|
||||
#include "MQTTUnsubscribe.h"
|
||||
#include "MQTTFormat.h"
|
||||
|
||||
int MQTTSerialize_ack(unsigned char* buf, int buflen, unsigned char type, unsigned char dup, unsigned short packetid);
|
||||
int MQTTDeserialize_ack(unsigned char* packettype, unsigned char* dup, unsigned short* packetid, unsigned char* buf, int buflen);
|
||||
|
||||
int MQTTPacket_len(int rem_len);
|
||||
int MQTTPacket_equals(MQTTString* a, char* b);
|
||||
|
||||
int MQTTPacket_encode(unsigned char* buf, int length);
|
||||
int MQTTPacket_decode(int (*getcharfn)(unsigned char*, int), int* value);
|
||||
int MQTTPacket_decodeBuf(unsigned char* buf, int* value);
|
||||
|
||||
int readInt(unsigned char** pptr);
|
||||
char readChar(unsigned char** pptr);
|
||||
void writeChar(unsigned char** pptr, char c);
|
||||
void writeInt(unsigned char** pptr, int anInt);
|
||||
int readMQTTLenString(MQTTString* mqttstring, unsigned char** pptr, unsigned char* enddata);
|
||||
void writeCString(unsigned char** pptr, const char* string);
|
||||
void writeMQTTString(unsigned char** pptr, MQTTString mqttstring);
|
||||
|
||||
DLLExport int MQTTPacket_read(unsigned char* buf, int buflen, int (*getfn)(unsigned char*, int));
|
||||
|
||||
typedef struct {
|
||||
int (*getfn)(void *, unsigned char*, int); /* must return -1 for error, 0 for call again, or the number of bytes read */
|
||||
void *sck; /* pointer to whatever the system may use to identify the transport */
|
||||
int multiplier;
|
||||
int rem_len;
|
||||
int len;
|
||||
char state;
|
||||
}MQTTTransport;
|
||||
|
||||
int MQTTPacket_readnb(unsigned char* buf, int buflen, MQTTTransport *trp);
|
||||
|
||||
#ifdef __cplusplus /* If this is a C++ compiler, use C linkage */
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
#endif /* MQTTPACKET_H_ */
|
||||
38
vd960DBN/ETH/MQTT_SRC/MQTTPublish.h
Normal file
38
vd960DBN/ETH/MQTT_SRC/MQTTPublish.h
Normal file
@@ -0,0 +1,38 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2014 IBM Corp.
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
* and the Eclipse Distribution License is available at
|
||||
* http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
*
|
||||
* Contributors:
|
||||
* Ian Craggs - initial API and implementation and/or initial documentation
|
||||
* Xiang Rong - 442039 Add makefile to Embedded C client
|
||||
*******************************************************************************/
|
||||
|
||||
#ifndef MQTTPUBLISH_H_
|
||||
#define MQTTPUBLISH_H_
|
||||
|
||||
#if !defined(DLLImport)
|
||||
#define DLLImport
|
||||
#endif
|
||||
#if !defined(DLLExport)
|
||||
#define DLLExport
|
||||
#endif
|
||||
|
||||
DLLExport int MQTTSerialize_publish(unsigned char* buf, int buflen, unsigned char dup, int qos, unsigned char retained, unsigned short packetid,
|
||||
MQTTString topicName, unsigned char* payload, int payloadlen);
|
||||
|
||||
DLLExport int MQTTDeserialize_publish(unsigned char* dup, int* qos, unsigned char* retained, unsigned short* packetid, MQTTString* topicName,
|
||||
unsigned char** payload, int* payloadlen, unsigned char* buf, int len);
|
||||
|
||||
DLLExport int MQTTSerialize_puback(unsigned char* buf, int buflen, unsigned short packetid);
|
||||
DLLExport int MQTTSerialize_pubrel(unsigned char* buf, int buflen, unsigned char dup, unsigned short packetid);
|
||||
DLLExport int MQTTSerialize_pubcomp(unsigned char* buf, int buflen, unsigned short packetid);
|
||||
|
||||
#endif /* MQTTPUBLISH_H_ */
|
||||
169
vd960DBN/ETH/MQTT_SRC/MQTTSerializePublish.c
Normal file
169
vd960DBN/ETH/MQTT_SRC/MQTTSerializePublish.c
Normal file
@@ -0,0 +1,169 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2014 IBM Corp.
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
* and the Eclipse Distribution License is available at
|
||||
* http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
*
|
||||
* Contributors:
|
||||
* Ian Craggs - initial API and implementation and/or initial documentation
|
||||
* Ian Craggs - fix for https://bugs.eclipse.org/bugs/show_bug.cgi?id=453144
|
||||
*******************************************************************************/
|
||||
|
||||
#include "MQTTPacket.h"
|
||||
#include "StackTrace.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
|
||||
/**
|
||||
* Determines the length of the MQTT publish packet that would be produced using the supplied parameters
|
||||
* @param qos the MQTT QoS of the publish (packetid is omitted for QoS 0)
|
||||
* @param topicName the topic name to be used in the publish
|
||||
* @param payloadlen the length of the payload to be sent
|
||||
* @return the length of buffer needed to contain the serialized version of the packet
|
||||
*/
|
||||
int MQTTSerialize_publishLength(int qos, MQTTString topicName, int payloadlen)
|
||||
{
|
||||
int len = 0;
|
||||
|
||||
len += 2 + MQTTstrlen(topicName) + payloadlen;
|
||||
if (qos > 0)
|
||||
len += 2; /* packetid */
|
||||
return len;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Serializes the supplied publish data into the supplied buffer, ready for sending
|
||||
* @param buf the buffer into which the packet will be serialized
|
||||
* @param buflen the length in bytes of the supplied buffer
|
||||
* @param dup integer - the MQTT dup flag
|
||||
* @param qos integer - the MQTT QoS value
|
||||
* @param retained integer - the MQTT retained flag
|
||||
* @param packetid integer - the MQTT packet identifier
|
||||
* @param topicName MQTTString - the MQTT topic in the publish
|
||||
* @param payload byte buffer - the MQTT publish payload
|
||||
* @param payloadlen integer - the length of the MQTT payload
|
||||
* @return the length of the serialized data. <= 0 indicates error
|
||||
*/
|
||||
int MQTTSerialize_publish(unsigned char* buf, int buflen, unsigned char dup, int qos, unsigned char retained, unsigned short packetid,
|
||||
MQTTString topicName, unsigned char* payload, int payloadlen)
|
||||
{
|
||||
unsigned char *ptr = buf;
|
||||
MQTTHeader header = {0};
|
||||
int rem_len = 0;
|
||||
int rc = 0;
|
||||
|
||||
FUNC_ENTRY;
|
||||
if (MQTTPacket_len(rem_len = MQTTSerialize_publishLength(qos, topicName, payloadlen)) > buflen)
|
||||
{
|
||||
rc = MQTTPACKET_BUFFER_TOO_SHORT;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
header.bits.type = PUBLISH;
|
||||
header.bits.dup = dup;
|
||||
header.bits.qos = qos;
|
||||
header.bits.retain = retained;
|
||||
writeChar(&ptr, header.byte); /* write header */
|
||||
|
||||
ptr += MQTTPacket_encode(ptr, rem_len); /* write remaining length */;
|
||||
|
||||
writeMQTTString(&ptr, topicName);
|
||||
|
||||
if (qos > 0)
|
||||
writeInt(&ptr, packetid);
|
||||
|
||||
memcpy(ptr, payload, payloadlen);
|
||||
ptr += payloadlen;
|
||||
|
||||
rc = ptr - buf;
|
||||
|
||||
exit:
|
||||
FUNC_EXIT_RC(rc);
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Serializes the ack packet into the supplied buffer.
|
||||
* @param buf the buffer into which the packet will be serialized
|
||||
* @param buflen the length in bytes of the supplied buffer
|
||||
* @param type the MQTT packet type
|
||||
* @param dup the MQTT dup flag
|
||||
* @param packetid the MQTT packet identifier
|
||||
* @return serialized length, or error if 0
|
||||
*/
|
||||
int MQTTSerialize_ack(unsigned char* buf, int buflen, unsigned char packettype, unsigned char dup, unsigned short packetid)
|
||||
{
|
||||
MQTTHeader header = {0};
|
||||
int rc = 0;
|
||||
unsigned char *ptr = buf;
|
||||
|
||||
FUNC_ENTRY;
|
||||
if (buflen < 4)
|
||||
{
|
||||
rc = MQTTPACKET_BUFFER_TOO_SHORT;
|
||||
goto exit;
|
||||
}
|
||||
header.bits.type = packettype;
|
||||
header.bits.dup = dup;
|
||||
header.bits.qos = (packettype == PUBREL) ? 1 : 0;
|
||||
writeChar(&ptr, header.byte); /* write header */
|
||||
|
||||
ptr += MQTTPacket_encode(ptr, 2); /* write remaining length */
|
||||
writeInt(&ptr, packetid);
|
||||
rc = ptr - buf;
|
||||
exit:
|
||||
FUNC_EXIT_RC(rc);
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Serializes a puback packet into the supplied buffer.
|
||||
* @param buf the buffer into which the packet will be serialized
|
||||
* @param buflen the length in bytes of the supplied buffer
|
||||
* @param packetid integer - the MQTT packet identifier
|
||||
* @return serialized length, or error if 0
|
||||
*/
|
||||
int MQTTSerialize_puback(unsigned char* buf, int buflen, unsigned short packetid)
|
||||
{
|
||||
return MQTTSerialize_ack(buf, buflen, PUBACK, 0, packetid);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Serializes a pubrel packet into the supplied buffer.
|
||||
* @param buf the buffer into which the packet will be serialized
|
||||
* @param buflen the length in bytes of the supplied buffer
|
||||
* @param dup integer - the MQTT dup flag
|
||||
* @param packetid integer - the MQTT packet identifier
|
||||
* @return serialized length, or error if 0
|
||||
*/
|
||||
int MQTTSerialize_pubrel(unsigned char* buf, int buflen, unsigned char dup, unsigned short packetid)
|
||||
{
|
||||
return MQTTSerialize_ack(buf, buflen, PUBREL, dup, packetid);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Serializes a pubrel packet into the supplied buffer.
|
||||
* @param buf the buffer into which the packet will be serialized
|
||||
* @param buflen the length in bytes of the supplied buffer
|
||||
* @param packetid integer - the MQTT packet identifier
|
||||
* @return serialized length, or error if 0
|
||||
*/
|
||||
int MQTTSerialize_pubcomp(unsigned char* buf, int buflen, unsigned short packetid)
|
||||
{
|
||||
return MQTTSerialize_ack(buf, buflen, PUBCOMP, 0, packetid);
|
||||
}
|
||||
|
||||
|
||||
39
vd960DBN/ETH/MQTT_SRC/MQTTSubscribe.h
Normal file
39
vd960DBN/ETH/MQTT_SRC/MQTTSubscribe.h
Normal file
@@ -0,0 +1,39 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2014 IBM Corp.
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
* and the Eclipse Distribution License is available at
|
||||
* http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
*
|
||||
* Contributors:
|
||||
* Ian Craggs - initial API and implementation and/or initial documentation
|
||||
* Xiang Rong - 442039 Add makefile to Embedded C client
|
||||
*******************************************************************************/
|
||||
|
||||
#ifndef MQTTSUBSCRIBE_H_
|
||||
#define MQTTSUBSCRIBE_H_
|
||||
|
||||
#if !defined(DLLImport)
|
||||
#define DLLImport
|
||||
#endif
|
||||
#if !defined(DLLExport)
|
||||
#define DLLExport
|
||||
#endif
|
||||
|
||||
DLLExport int MQTTSerialize_subscribe(unsigned char* buf, int buflen, unsigned char dup, unsigned short packetid,
|
||||
int count, MQTTString topicFilters[], int requestedQoSs[]);
|
||||
|
||||
DLLExport int MQTTDeserialize_subscribe(unsigned char* dup, unsigned short* packetid,
|
||||
int maxcount, int* count, MQTTString topicFilters[], int requestedQoSs[], unsigned char* buf, int len);
|
||||
|
||||
DLLExport int MQTTSerialize_suback(unsigned char* buf, int buflen, unsigned short packetid, int count, int* grantedQoSs);
|
||||
|
||||
DLLExport int MQTTDeserialize_suback(unsigned short* packetid, int maxcount, int* count, int grantedQoSs[], unsigned char* buf, int len);
|
||||
|
||||
|
||||
#endif /* MQTTSUBSCRIBE_H_ */
|
||||
137
vd960DBN/ETH/MQTT_SRC/MQTTSubscribeClient.c
Normal file
137
vd960DBN/ETH/MQTT_SRC/MQTTSubscribeClient.c
Normal file
@@ -0,0 +1,137 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2014 IBM Corp.
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
* and the Eclipse Distribution License is available at
|
||||
* http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
*
|
||||
* Contributors:
|
||||
* Ian Craggs - initial API and implementation and/or initial documentation
|
||||
*******************************************************************************/
|
||||
|
||||
#include "MQTTPacket.h"
|
||||
#include "StackTrace.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
/**
|
||||
* Determines the length of the MQTT subscribe packet that would be produced using the supplied parameters
|
||||
* @param count the number of topic filter strings in topicFilters
|
||||
* @param topicFilters the array of topic filter strings to be used in the publish
|
||||
* @return the length of buffer needed to contain the serialized version of the packet
|
||||
*/
|
||||
int MQTTSerialize_subscribeLength(int count, MQTTString topicFilters[])
|
||||
{
|
||||
int i;
|
||||
int len = 2; /* packetid */
|
||||
|
||||
for (i = 0; i < count; ++i)
|
||||
len += 2 + MQTTstrlen(topicFilters[i]) + 1; /* length + topic + req_qos */
|
||||
return len;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Serializes the supplied subscribe data into the supplied buffer, ready for sending
|
||||
* @param buf the buffer into which the packet will be serialized
|
||||
* @param buflen the length in bytes of the supplied bufferr
|
||||
* @param dup integer - the MQTT dup flag
|
||||
* @param packetid integer - the MQTT packet identifier
|
||||
* @param count - number of members in the topicFilters and reqQos arrays
|
||||
* @param topicFilters - array of topic filter names
|
||||
* @param requestedQoSs - array of requested QoS
|
||||
* @return the length of the serialized data. <= 0 indicates error
|
||||
*/
|
||||
int MQTTSerialize_subscribe(unsigned char* buf, int buflen, unsigned char dup, unsigned short packetid, int count,
|
||||
MQTTString topicFilters[], int requestedQoSs[])
|
||||
{
|
||||
unsigned char *ptr = buf;
|
||||
MQTTHeader header = {0};
|
||||
int rem_len = 0;
|
||||
int rc = 0;
|
||||
int i = 0;
|
||||
|
||||
FUNC_ENTRY;
|
||||
if (MQTTPacket_len(rem_len = MQTTSerialize_subscribeLength(count, topicFilters)) > buflen)
|
||||
{
|
||||
rc = MQTTPACKET_BUFFER_TOO_SHORT;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
header.byte = 0;
|
||||
header.bits.type = SUBSCRIBE;
|
||||
header.bits.dup = dup;
|
||||
header.bits.qos = 1;
|
||||
writeChar(&ptr, header.byte); /* write header */
|
||||
|
||||
ptr += MQTTPacket_encode(ptr, rem_len); /* write remaining length */;
|
||||
|
||||
writeInt(&ptr, packetid);
|
||||
|
||||
for (i = 0; i < count; ++i)
|
||||
{
|
||||
writeMQTTString(&ptr, topicFilters[i]);
|
||||
writeChar(&ptr, requestedQoSs[i]);
|
||||
}
|
||||
|
||||
rc = ptr - buf;
|
||||
exit:
|
||||
FUNC_EXIT_RC(rc);
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Deserializes the supplied (wire) buffer into suback data
|
||||
* @param packetid returned integer - the MQTT packet identifier
|
||||
* @param maxcount - the maximum number of members allowed in the grantedQoSs array
|
||||
* @param count returned integer - number of members in the grantedQoSs array
|
||||
* @param grantedQoSs returned array of integers - the granted qualities of service
|
||||
* @param buf the raw buffer data, of the correct length determined by the remaining length field
|
||||
* @param buflen the length in bytes of the data in the supplied buffer
|
||||
* @return error code. 1 is success, 0 is failure
|
||||
*/
|
||||
int MQTTDeserialize_suback(unsigned short* packetid, int maxcount, int* count, int grantedQoSs[], unsigned char* buf, int buflen)
|
||||
{
|
||||
MQTTHeader header = {0};
|
||||
unsigned char* curdata = buf;
|
||||
unsigned char* enddata = NULL;
|
||||
int rc = 0;
|
||||
int mylen;
|
||||
|
||||
FUNC_ENTRY;
|
||||
header.byte = readChar(&curdata);
|
||||
if (header.bits.type != SUBACK)
|
||||
goto exit;
|
||||
|
||||
curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */
|
||||
enddata = curdata + mylen;
|
||||
if (enddata - curdata < 2)
|
||||
goto exit;
|
||||
|
||||
*packetid = readInt(&curdata);
|
||||
|
||||
*count = 0;
|
||||
while (curdata < enddata)
|
||||
{
|
||||
if (*count > maxcount)
|
||||
{
|
||||
rc = -1;
|
||||
goto exit;
|
||||
}
|
||||
grantedQoSs[(*count)++] = readChar(&curdata);
|
||||
}
|
||||
|
||||
rc = 1;
|
||||
exit:
|
||||
FUNC_EXIT_RC(rc);
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
112
vd960DBN/ETH/MQTT_SRC/MQTTSubscribeServer.c
Normal file
112
vd960DBN/ETH/MQTT_SRC/MQTTSubscribeServer.c
Normal file
@@ -0,0 +1,112 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2014 IBM Corp.
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
* and the Eclipse Distribution License is available at
|
||||
* http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
*
|
||||
* Contributors:
|
||||
* Ian Craggs - initial API and implementation and/or initial documentation
|
||||
*******************************************************************************/
|
||||
|
||||
#include "MQTTPacket.h"
|
||||
#include "StackTrace.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
|
||||
/**
|
||||
* Deserializes the supplied (wire) buffer into subscribe data
|
||||
* @param dup integer returned - the MQTT dup flag
|
||||
* @param packetid integer returned - the MQTT packet identifier
|
||||
* @param maxcount - the maximum number of members allowed in the topicFilters and requestedQoSs arrays
|
||||
* @param count - number of members in the topicFilters and requestedQoSs arrays
|
||||
* @param topicFilters - array of topic filter names
|
||||
* @param requestedQoSs - array of requested QoS
|
||||
* @param buf the raw buffer data, of the correct length determined by the remaining length field
|
||||
* @param buflen the length in bytes of the data in the supplied buffer
|
||||
* @return the length of the serialized data. <= 0 indicates error
|
||||
*/
|
||||
int MQTTDeserialize_subscribe(unsigned char* dup, unsigned short* packetid, int maxcount, int* count, MQTTString topicFilters[],
|
||||
int requestedQoSs[], unsigned char* buf, int buflen)
|
||||
{
|
||||
MQTTHeader header = {0};
|
||||
unsigned char* curdata = buf;
|
||||
unsigned char* enddata = NULL;
|
||||
int rc = -1;
|
||||
int mylen = 0;
|
||||
|
||||
FUNC_ENTRY;
|
||||
header.byte = readChar(&curdata);
|
||||
if (header.bits.type != SUBSCRIBE)
|
||||
goto exit;
|
||||
*dup = header.bits.dup;
|
||||
|
||||
curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */
|
||||
enddata = curdata + mylen;
|
||||
|
||||
*packetid = readInt(&curdata);
|
||||
|
||||
*count = 0;
|
||||
while (curdata < enddata)
|
||||
{
|
||||
if (!readMQTTLenString(&topicFilters[*count], &curdata, enddata))
|
||||
goto exit;
|
||||
if (curdata >= enddata) /* do we have enough data to read the req_qos version byte? */
|
||||
goto exit;
|
||||
requestedQoSs[*count] = readChar(&curdata);
|
||||
(*count)++;
|
||||
}
|
||||
|
||||
rc = 1;
|
||||
exit:
|
||||
FUNC_EXIT_RC(rc);
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Serializes the supplied suback data into the supplied buffer, ready for sending
|
||||
* @param buf the buffer into which the packet will be serialized
|
||||
* @param buflen the length in bytes of the supplied buffer
|
||||
* @param packetid integer - the MQTT packet identifier
|
||||
* @param count - number of members in the grantedQoSs array
|
||||
* @param grantedQoSs - array of granted QoS
|
||||
* @return the length of the serialized data. <= 0 indicates error
|
||||
*/
|
||||
int MQTTSerialize_suback(unsigned char* buf, int buflen, unsigned short packetid, int count, int* grantedQoSs)
|
||||
{
|
||||
MQTTHeader header = {0};
|
||||
int rc = -1;
|
||||
unsigned char *ptr = buf;
|
||||
int i;
|
||||
|
||||
FUNC_ENTRY;
|
||||
if (buflen < 2 + count)
|
||||
{
|
||||
rc = MQTTPACKET_BUFFER_TOO_SHORT;
|
||||
goto exit;
|
||||
}
|
||||
header.byte = 0;
|
||||
header.bits.type = SUBACK;
|
||||
writeChar(&ptr, header.byte); /* write header */
|
||||
|
||||
ptr += MQTTPacket_encode(ptr, 2 + count); /* write remaining length */
|
||||
|
||||
writeInt(&ptr, packetid);
|
||||
|
||||
for (i = 0; i < count; ++i)
|
||||
writeChar(&ptr, grantedQoSs[i]);
|
||||
|
||||
rc = ptr - buf;
|
||||
exit:
|
||||
FUNC_EXIT_RC(rc);
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
38
vd960DBN/ETH/MQTT_SRC/MQTTUnsubscribe.h
Normal file
38
vd960DBN/ETH/MQTT_SRC/MQTTUnsubscribe.h
Normal file
@@ -0,0 +1,38 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2014 IBM Corp.
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
* and the Eclipse Distribution License is available at
|
||||
* http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
*
|
||||
* Contributors:
|
||||
* Ian Craggs - initial API and implementation and/or initial documentation
|
||||
* Xiang Rong - 442039 Add makefile to Embedded C client
|
||||
*******************************************************************************/
|
||||
|
||||
#ifndef MQTTUNSUBSCRIBE_H_
|
||||
#define MQTTUNSUBSCRIBE_H_
|
||||
|
||||
#if !defined(DLLImport)
|
||||
#define DLLImport
|
||||
#endif
|
||||
#if !defined(DLLExport)
|
||||
#define DLLExport
|
||||
#endif
|
||||
|
||||
DLLExport int MQTTSerialize_unsubscribe(unsigned char* buf, int buflen, unsigned char dup, unsigned short packetid,
|
||||
int count, MQTTString topicFilters[]);
|
||||
|
||||
DLLExport int MQTTDeserialize_unsubscribe(unsigned char* dup, unsigned short* packetid, int max_count, int* count, MQTTString topicFilters[],
|
||||
unsigned char* buf, int len);
|
||||
|
||||
DLLExport int MQTTSerialize_unsuback(unsigned char* buf, int buflen, unsigned short packetid);
|
||||
|
||||
DLLExport int MQTTDeserialize_unsuback(unsigned short* packetid, unsigned char* buf, int len);
|
||||
|
||||
#endif /* MQTTUNSUBSCRIBE_H_ */
|
||||
106
vd960DBN/ETH/MQTT_SRC/MQTTUnsubscribeClient.c
Normal file
106
vd960DBN/ETH/MQTT_SRC/MQTTUnsubscribeClient.c
Normal file
@@ -0,0 +1,106 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2014 IBM Corp.
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
* and the Eclipse Distribution License is available at
|
||||
* http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
*
|
||||
* Contributors:
|
||||
* Ian Craggs - initial API and implementation and/or initial documentation
|
||||
*******************************************************************************/
|
||||
|
||||
#include "MQTTPacket.h"
|
||||
#include "StackTrace.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
/**
|
||||
* Determines the length of the MQTT unsubscribe packet that would be produced using the supplied parameters
|
||||
* @param count the number of topic filter strings in topicFilters
|
||||
* @param topicFilters the array of topic filter strings to be used in the publish
|
||||
* @return the length of buffer needed to contain the serialized version of the packet
|
||||
*/
|
||||
int MQTTSerialize_unsubscribeLength(int count, MQTTString topicFilters[])
|
||||
{
|
||||
int i;
|
||||
int len = 2; /* packetid */
|
||||
|
||||
for (i = 0; i < count; ++i)
|
||||
len += 2 + MQTTstrlen(topicFilters[i]); /* length + topic*/
|
||||
return len;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Serializes the supplied unsubscribe data into the supplied buffer, ready for sending
|
||||
* @param buf the raw buffer data, of the correct length determined by the remaining length field
|
||||
* @param buflen the length in bytes of the data in the supplied buffer
|
||||
* @param dup integer - the MQTT dup flag
|
||||
* @param packetid integer - the MQTT packet identifier
|
||||
* @param count - number of members in the topicFilters array
|
||||
* @param topicFilters - array of topic filter names
|
||||
* @return the length of the serialized data. <= 0 indicates error
|
||||
*/
|
||||
int MQTTSerialize_unsubscribe(unsigned char* buf, int buflen, unsigned char dup, unsigned short packetid,
|
||||
int count, MQTTString topicFilters[])
|
||||
{
|
||||
unsigned char *ptr = buf;
|
||||
MQTTHeader header = {0};
|
||||
int rem_len = 0;
|
||||
int rc = -1;
|
||||
int i = 0;
|
||||
|
||||
FUNC_ENTRY;
|
||||
if (MQTTPacket_len(rem_len = MQTTSerialize_unsubscribeLength(count, topicFilters)) > buflen)
|
||||
{
|
||||
rc = MQTTPACKET_BUFFER_TOO_SHORT;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
header.byte = 0;
|
||||
header.bits.type = UNSUBSCRIBE;
|
||||
header.bits.dup = dup;
|
||||
header.bits.qos = 1;
|
||||
writeChar(&ptr, header.byte); /* write header */
|
||||
|
||||
ptr += MQTTPacket_encode(ptr, rem_len); /* write remaining length */;
|
||||
|
||||
writeInt(&ptr, packetid);
|
||||
|
||||
for (i = 0; i < count; ++i)
|
||||
writeMQTTString(&ptr, topicFilters[i]);
|
||||
|
||||
rc = ptr - buf;
|
||||
exit:
|
||||
FUNC_EXIT_RC(rc);
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Deserializes the supplied (wire) buffer into unsuback data
|
||||
* @param packetid returned integer - the MQTT packet identifier
|
||||
* @param buf the raw buffer data, of the correct length determined by the remaining length field
|
||||
* @param buflen the length in bytes of the data in the supplied buffer
|
||||
* @return error code. 1 is success, 0 is failure
|
||||
*/
|
||||
int MQTTDeserialize_unsuback(unsigned short* packetid, unsigned char* buf, int buflen)
|
||||
{
|
||||
unsigned char type = 0;
|
||||
unsigned char dup = 0;
|
||||
int rc = 0;
|
||||
|
||||
FUNC_ENTRY;
|
||||
rc = MQTTDeserialize_ack(&type, &dup, packetid, buf, buflen);
|
||||
if (type == UNSUBACK)
|
||||
rc = 1;
|
||||
FUNC_EXIT_RC(rc);
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
102
vd960DBN/ETH/MQTT_SRC/MQTTUnsubscribeServer.c
Normal file
102
vd960DBN/ETH/MQTT_SRC/MQTTUnsubscribeServer.c
Normal file
@@ -0,0 +1,102 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2014 IBM Corp.
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
* and the Eclipse Distribution License is available at
|
||||
* http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
*
|
||||
* Contributors:
|
||||
* Ian Craggs - initial API and implementation and/or initial documentation
|
||||
*******************************************************************************/
|
||||
|
||||
#include "MQTTPacket.h"
|
||||
#include "StackTrace.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
|
||||
/**
|
||||
* Deserializes the supplied (wire) buffer into unsubscribe data
|
||||
* @param dup integer returned - the MQTT dup flag
|
||||
* @param packetid integer returned - the MQTT packet identifier
|
||||
* @param maxcount - the maximum number of members allowed in the topicFilters and requestedQoSs arrays
|
||||
* @param count - number of members in the topicFilters and requestedQoSs arrays
|
||||
* @param topicFilters - array of topic filter names
|
||||
* @param buf the raw buffer data, of the correct length determined by the remaining length field
|
||||
* @param buflen the length in bytes of the data in the supplied buffer
|
||||
* @return the length of the serialized data. <= 0 indicates error
|
||||
*/
|
||||
int MQTTDeserialize_unsubscribe(unsigned char* dup, unsigned short* packetid, int maxcount, int* count, MQTTString topicFilters[],
|
||||
unsigned char* buf, int len)
|
||||
{
|
||||
MQTTHeader header = {0};
|
||||
unsigned char* curdata = buf;
|
||||
unsigned char* enddata = NULL;
|
||||
int rc = 0;
|
||||
int mylen = 0;
|
||||
|
||||
FUNC_ENTRY;
|
||||
header.byte = readChar(&curdata);
|
||||
if (header.bits.type != UNSUBSCRIBE)
|
||||
goto exit;
|
||||
*dup = header.bits.dup;
|
||||
|
||||
curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */
|
||||
enddata = curdata + mylen;
|
||||
|
||||
*packetid = readInt(&curdata);
|
||||
|
||||
*count = 0;
|
||||
while (curdata < enddata)
|
||||
{
|
||||
if (!readMQTTLenString(&topicFilters[*count], &curdata, enddata))
|
||||
goto exit;
|
||||
(*count)++;
|
||||
}
|
||||
|
||||
rc = 1;
|
||||
exit:
|
||||
FUNC_EXIT_RC(rc);
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Serializes the supplied unsuback data into the supplied buffer, ready for sending
|
||||
* @param buf the buffer into which the packet will be serialized
|
||||
* @param buflen the length in bytes of the supplied buffer
|
||||
* @param packetid integer - the MQTT packet identifier
|
||||
* @return the length of the serialized data. <= 0 indicates error
|
||||
*/
|
||||
int MQTTSerialize_unsuback(unsigned char* buf, int buflen, unsigned short packetid)
|
||||
{
|
||||
MQTTHeader header = {0};
|
||||
int rc = 0;
|
||||
unsigned char *ptr = buf;
|
||||
|
||||
FUNC_ENTRY;
|
||||
if (buflen < 2)
|
||||
{
|
||||
rc = MQTTPACKET_BUFFER_TOO_SHORT;
|
||||
goto exit;
|
||||
}
|
||||
header.byte = 0;
|
||||
header.bits.type = UNSUBACK;
|
||||
writeChar(&ptr, header.byte); /* write header */
|
||||
|
||||
ptr += MQTTPacket_encode(ptr, 2); /* write remaining length */
|
||||
|
||||
writeInt(&ptr, packetid);
|
||||
|
||||
rc = ptr - buf;
|
||||
exit:
|
||||
FUNC_EXIT_RC(rc);
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
78
vd960DBN/ETH/MQTT_SRC/StackTrace.h
Normal file
78
vd960DBN/ETH/MQTT_SRC/StackTrace.h
Normal file
@@ -0,0 +1,78 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2014 IBM Corp.
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
* and the Eclipse Distribution License is available at
|
||||
* http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
*
|
||||
* Contributors:
|
||||
* Ian Craggs - initial API and implementation and/or initial documentation
|
||||
* Ian Craggs - fix for bug #434081
|
||||
*******************************************************************************/
|
||||
|
||||
#ifndef STACKTRACE_H_
|
||||
#define STACKTRACE_H_
|
||||
|
||||
#include <stdio.h>
|
||||
#define NOSTACKTRACE 1
|
||||
|
||||
#if defined(NOSTACKTRACE)
|
||||
#define FUNC_ENTRY
|
||||
#define FUNC_ENTRY_NOLOG
|
||||
#define FUNC_ENTRY_MED
|
||||
#define FUNC_ENTRY_MAX
|
||||
#define FUNC_EXIT
|
||||
#define FUNC_EXIT_NOLOG
|
||||
#define FUNC_EXIT_MED
|
||||
#define FUNC_EXIT_MAX
|
||||
#define FUNC_EXIT_RC(x)
|
||||
#define FUNC_EXIT_MED_RC(x)
|
||||
#define FUNC_EXIT_MAX_RC(x)
|
||||
|
||||
#else
|
||||
|
||||
#if defined(WIN32)
|
||||
#define inline __inline
|
||||
#define FUNC_ENTRY StackTrace_entry(__FUNCTION__, __LINE__, TRACE_MINIMUM)
|
||||
#define FUNC_ENTRY_NOLOG StackTrace_entry(__FUNCTION__, __LINE__, -1)
|
||||
#define FUNC_ENTRY_MED StackTrace_entry(__FUNCTION__, __LINE__, TRACE_MEDIUM)
|
||||
#define FUNC_ENTRY_MAX StackTrace_entry(__FUNCTION__, __LINE__, TRACE_MAXIMUM)
|
||||
#define FUNC_EXIT StackTrace_exit(__FUNCTION__, __LINE__, NULL, TRACE_MINIMUM)
|
||||
#define FUNC_EXIT_NOLOG StackTrace_exit(__FUNCTION__, __LINE__, -1)
|
||||
#define FUNC_EXIT_MED StackTrace_exit(__FUNCTION__, __LINE__, NULL, TRACE_MEDIUM)
|
||||
#define FUNC_EXIT_MAX StackTrace_exit(__FUNCTION__, __LINE__, NULL, TRACE_MAXIMUM)
|
||||
#define FUNC_EXIT_RC(x) StackTrace_exit(__FUNCTION__, __LINE__, &x, TRACE_MINIMUM)
|
||||
#define FUNC_EXIT_MED_RC(x) StackTrace_exit(__FUNCTION__, __LINE__, &x, TRACE_MEDIUM)
|
||||
#define FUNC_EXIT_MAX_RC(x) StackTrace_exit(__FUNCTION__, __LINE__, &x, TRACE_MAXIMUM)
|
||||
#else
|
||||
#define FUNC_ENTRY StackTrace_entry(__func__, __LINE__, TRACE_MINIMUM)
|
||||
#define FUNC_ENTRY_NOLOG StackTrace_entry(__func__, __LINE__, -1)
|
||||
#define FUNC_ENTRY_MED StackTrace_entry(__func__, __LINE__, TRACE_MEDIUM)
|
||||
#define FUNC_ENTRY_MAX StackTrace_entry(__func__, __LINE__, TRACE_MAXIMUM)
|
||||
#define FUNC_EXIT StackTrace_exit(__func__, __LINE__, NULL, TRACE_MINIMUM)
|
||||
#define FUNC_EXIT_NOLOG StackTrace_exit(__func__, __LINE__, NULL, -1)
|
||||
#define FUNC_EXIT_MED StackTrace_exit(__func__, __LINE__, NULL, TRACE_MEDIUM)
|
||||
#define FUNC_EXIT_MAX StackTrace_exit(__func__, __LINE__, NULL, TRACE_MAXIMUM)
|
||||
#define FUNC_EXIT_RC(x) StackTrace_exit(__func__, __LINE__, &x, TRACE_MINIMUM)
|
||||
#define FUNC_EXIT_MED_RC(x) StackTrace_exit(__func__, __LINE__, &x, TRACE_MEDIUM)
|
||||
#define FUNC_EXIT_MAX_RC(x) StackTrace_exit(__func__, __LINE__, &x, TRACE_MAXIMUM)
|
||||
|
||||
void StackTrace_entry(const char* name, int line, int trace);
|
||||
void StackTrace_exit(const char* name, int line, void* return_value, int trace);
|
||||
|
||||
void StackTrace_printStack(FILE* dest);
|
||||
char* StackTrace_get(unsigned long);
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
|
||||
#endif /* STACKTRACE_H_ */
|
||||
745
vd960DBN/ETH/NetLib/eth_driver.c
Normal file
745
vd960DBN/ETH/NetLib/eth_driver.c
Normal file
@@ -0,0 +1,745 @@
|
||||
/********************************** (C) COPYRIGHT *******************************
|
||||
* File Name : eth_driver.c
|
||||
* Author : WCH
|
||||
* Version : V1.3.0
|
||||
* Date : 2022/05/26
|
||||
* Description : eth program body.
|
||||
*********************************************************************************
|
||||
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
|
||||
* Attention: This software (modified or not) and binary are used for
|
||||
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
|
||||
*******************************************************************************/
|
||||
#include "string.h"
|
||||
#include "eth_driver.h"
|
||||
|
||||
__attribute__((__aligned__(4))) ETH_DMADESCTypeDef DMARxDscrTab[ETH_RXBUFNB]; /* MAC receive descriptor, 4-byte aligned*/
|
||||
__attribute__((__aligned__(4))) ETH_DMADESCTypeDef DMATxDscrTab[ETH_TXBUFNB]; /* MAC send descriptor, 4-byte aligned */
|
||||
|
||||
__attribute__((__aligned__(4))) uint8_t MACRxBuf[ETH_RXBUFNB*ETH_RX_BUF_SZE]; /* MAC receive buffer, 4-byte aligned */
|
||||
__attribute__((__aligned__(4))) uint8_t MACTxBuf[ETH_TXBUFNB*ETH_TX_BUF_SZE]; /* MAC send buffer, 4-byte aligned */
|
||||
|
||||
__attribute__((__aligned__(4))) SOCK_INF SocketInf[WCHNET_MAX_SOCKET_NUM]; /* Socket information table, 4-byte alignment */
|
||||
const uint16_t MemNum[8] = {WCHNET_NUM_IPRAW,
|
||||
WCHNET_NUM_UDP,
|
||||
WCHNET_NUM_TCP,
|
||||
WCHNET_NUM_TCP_LISTEN,
|
||||
WCHNET_NUM_TCP_SEG,
|
||||
WCHNET_NUM_IP_REASSDATA,
|
||||
WCHNET_NUM_PBUF,
|
||||
WCHNET_NUM_POOL_BUF
|
||||
};
|
||||
const uint16_t MemSize[8] = {WCHNET_MEM_ALIGN_SIZE(WCHNET_SIZE_IPRAW_PCB),
|
||||
WCHNET_MEM_ALIGN_SIZE(WCHNET_SIZE_UDP_PCB),
|
||||
WCHNET_MEM_ALIGN_SIZE(WCHNET_SIZE_TCP_PCB),
|
||||
WCHNET_MEM_ALIGN_SIZE(WCHNET_SIZE_TCP_PCB_LISTEN),
|
||||
WCHNET_MEM_ALIGN_SIZE(WCHNET_SIZE_TCP_SEG),
|
||||
WCHNET_MEM_ALIGN_SIZE(WCHNET_SIZE_IP_REASSDATA),
|
||||
WCHNET_MEM_ALIGN_SIZE(WCHNET_SIZE_PBUF),
|
||||
WCHNET_MEM_ALIGN_SIZE(WCHNET_SIZE_PBUF) + WCHNET_MEM_ALIGN_SIZE(WCHNET_SIZE_POOL_BUF)
|
||||
};
|
||||
__attribute__((__aligned__(4)))uint8_t Memp_Memory[WCHNET_MEMP_SIZE];
|
||||
__attribute__((__aligned__(4)))uint8_t Mem_Heap_Memory[WCHNET_RAM_HEAP_SIZE];
|
||||
__attribute__((__aligned__(4)))uint8_t Mem_ArpTable[WCHNET_RAM_ARP_TABLE_SIZE];
|
||||
|
||||
uint32_t volatile LocalTime;
|
||||
ETH_DMADESCTypeDef *DMATxDescToSet;
|
||||
ETH_DMADESCTypeDef *DMARxDescToGet;
|
||||
ETH_DMADESCTypeDef *pDMARxSet;
|
||||
|
||||
volatile uint8_t phyLinkReset;
|
||||
volatile uint32_t phyLinkTime;
|
||||
uint8_t phyPN = 0x01;
|
||||
uint8_t phyStatus = 0;
|
||||
uint8_t phySucCnt = 0;
|
||||
uint8_t phyLinkCnt = 0;
|
||||
uint8_t phyRetryCnt = 0;
|
||||
uint8_t CRCErrPktCnt = 0;
|
||||
uint8_t phyLinkStatus = 0;
|
||||
uint8_t phyPNChangeCnt = 0;
|
||||
uint8_t PhyPolarityDetect = 0;
|
||||
/*********************************************************************
|
||||
* @fn WCHNET_GetMacAddr
|
||||
*
|
||||
* @brief Get MAC address
|
||||
*
|
||||
* @return none.
|
||||
*/
|
||||
void WCHNET_GetMacAddr( uint8_t *p )
|
||||
{
|
||||
uint8_t i;
|
||||
uint8_t *macaddr=(uint8_t *)(ROM_CFG_USERADR_ID+5);
|
||||
|
||||
for(i=0;i<6;i++)
|
||||
{
|
||||
*p = *macaddr;
|
||||
p++;
|
||||
macaddr--;
|
||||
}
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn WCHNET_TimeIsr
|
||||
*
|
||||
* @brief
|
||||
*
|
||||
* @return none.
|
||||
*/
|
||||
void WCHNET_TimeIsr( uint16_t timperiod )
|
||||
{
|
||||
LocalTime += timperiod;
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn WritePHYReg
|
||||
*
|
||||
* @brief MCU write PHY register.
|
||||
*
|
||||
* @param reg_add - PHY address,
|
||||
* reg_val - value you want to write.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
void WritePHYReg(uint8_t reg_add,uint16_t reg_val)
|
||||
{
|
||||
R32_ETH_MIWR = (reg_add & RB_ETH_MIREGADR_MIRDL) | (1<<8) | (reg_val<<16);
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn ReadPHYReg
|
||||
*
|
||||
* @brief MCU read PHY register.
|
||||
*
|
||||
* @param reg_add - PHY address.
|
||||
*
|
||||
* @return value you want to get.
|
||||
*/
|
||||
uint16_t ReadPHYReg(uint8_t reg_add)
|
||||
{
|
||||
R8_ETH_MIREGADR = reg_add; // write address
|
||||
return R16_ETH_MIRD; // get data
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn WCHNET_LinkProcess
|
||||
*
|
||||
* @brief link process.
|
||||
*
|
||||
* @param none.
|
||||
*
|
||||
* @return none.
|
||||
*/
|
||||
void WCHNET_LinkProcess( void )
|
||||
{
|
||||
uint16_t phy_anlpar, phy_bmcr, phy_bmsr;
|
||||
|
||||
phy_anlpar = ReadPHYReg(PHY_ANLPAR);
|
||||
phy_bmsr = ReadPHYReg(PHY_BMSR);
|
||||
|
||||
if(phy_anlpar&PHY_ANLPAR_SELECTOR_FIELD)
|
||||
{
|
||||
if( !(phyLinkStatus&PHY_LINK_WAIT_SUC) )
|
||||
{
|
||||
if( (phyPN&0x0C) == PHY_PN_SWITCH_P )
|
||||
{
|
||||
phySucCnt = 0;
|
||||
phyLinkCnt = 0;
|
||||
phyLinkStatus = PHY_LINK_WAIT_SUC;
|
||||
}
|
||||
else
|
||||
{
|
||||
if( !(phyLinkStatus&PHY_LINK_SUC_N) )
|
||||
{
|
||||
phyRetryCnt = 0;
|
||||
phyLinkStatus |= PHY_LINK_SUC_N;
|
||||
phyPN &= ~PHY_PN_SWITCH_N;
|
||||
phy_bmcr = ReadPHYReg(PHY_BMCR);
|
||||
phy_bmcr |= 1<<9;
|
||||
WritePHYReg(PHY_BMCR, phy_bmcr);
|
||||
WritePHYReg(PHY_MDIX, phyPN);
|
||||
}
|
||||
else
|
||||
{
|
||||
phySucCnt = 0;
|
||||
phyLinkCnt = 0;
|
||||
phyLinkStatus = PHY_LINK_WAIT_SUC;
|
||||
}
|
||||
}
|
||||
}
|
||||
else{
|
||||
if((phySucCnt++ == 5) && ((phy_bmsr&PHY_AutoNego_Complete) == 0))
|
||||
{
|
||||
phySucCnt = 0;
|
||||
phyRetryCnt = 0;
|
||||
phyPNChangeCnt = 0;
|
||||
phyLinkStatus = PHY_LINK_INIT;
|
||||
phy_bmcr = ReadPHYReg(PHY_BMCR);
|
||||
phy_bmcr |= 1<<9;
|
||||
WritePHYReg(PHY_BMCR, phy_bmcr);
|
||||
if((phyPN&0x0C) == PHY_PN_SWITCH_P)
|
||||
{
|
||||
phyPN |= PHY_PN_SWITCH_N;
|
||||
}
|
||||
else {
|
||||
phyPN &= ~PHY_PN_SWITCH_N;
|
||||
}
|
||||
WritePHYReg(PHY_MDIX, phyPN);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if(phy_bmsr & PHY_AutoNego_Complete)
|
||||
{
|
||||
phySucCnt = 0;
|
||||
phyLinkCnt = 0;
|
||||
phyLinkStatus = PHY_LINK_WAIT_SUC;
|
||||
}
|
||||
else {
|
||||
if( phyLinkStatus == PHY_LINK_WAIT_SUC )
|
||||
{
|
||||
if(phyLinkCnt++ == 10)
|
||||
{
|
||||
phyLinkCnt = 0;
|
||||
phyRetryCnt = 0;
|
||||
phyPNChangeCnt = 0;
|
||||
phyLinkStatus = PHY_LINK_INIT;
|
||||
}
|
||||
}
|
||||
else if(phyLinkStatus == PHY_LINK_INIT)
|
||||
{
|
||||
if(phyPNChangeCnt++ == 10)
|
||||
{
|
||||
phyPNChangeCnt = 0;
|
||||
phyPN = ReadPHYReg(PHY_MDIX);
|
||||
phyPN &= ~0x0c;
|
||||
phyPN ^= 0x03;
|
||||
WritePHYReg(PHY_MDIX, phyPN);
|
||||
}
|
||||
else{
|
||||
if((phyPN&0x0C) == PHY_PN_SWITCH_P)
|
||||
{
|
||||
phyPN |= PHY_PN_SWITCH_N;
|
||||
}
|
||||
else {
|
||||
phyPN &= ~PHY_PN_SWITCH_N;
|
||||
}
|
||||
WritePHYReg(PHY_MDIX, phyPN);
|
||||
}
|
||||
}
|
||||
else if(phyLinkStatus == PHY_LINK_SUC_N)
|
||||
{
|
||||
if((phyPN&0x0C) == PHY_PN_SWITCH_P)
|
||||
{
|
||||
phyPN |= PHY_PN_SWITCH_N;
|
||||
phy_bmcr = ReadPHYReg(PHY_BMCR);
|
||||
phy_bmcr |= 1<<9;
|
||||
WritePHYReg(PHY_BMCR, phy_bmcr);
|
||||
Delay_Us(10);
|
||||
WritePHYReg(PHY_MDIX, phyPN);
|
||||
}
|
||||
else{
|
||||
if(phyRetryCnt++ == 15)
|
||||
{
|
||||
phyRetryCnt = 0;
|
||||
phyPNChangeCnt = 0;
|
||||
phyLinkStatus = PHY_LINK_INIT;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn WCHNET_PhyPNProcess
|
||||
*
|
||||
* @brief Phy PN Polarity related processing
|
||||
*
|
||||
* @param none.
|
||||
*
|
||||
* @return none.
|
||||
*/
|
||||
void WCHNET_PhyPNProcess(void)
|
||||
{
|
||||
uint32_t PhyVal;
|
||||
|
||||
phyLinkTime = LocalTime;
|
||||
if(CRCErrPktCnt >= 3)
|
||||
{
|
||||
PhyVal = ReadPHYReg(PHY_MDIX);
|
||||
if((PhyVal >> 2) & 0x01)
|
||||
PhyVal &= ~(3 << 2); //change PHY PN Polarity to normal
|
||||
else
|
||||
PhyVal |= 1 << 2; //change PHY PN Polarity to reverse
|
||||
WritePHYReg(PHY_MDIX, PhyVal);
|
||||
CRCErrPktCnt = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn WCHNET_HandlePhyNegotiation
|
||||
*
|
||||
* @brief Handle PHY Negotiation.
|
||||
*
|
||||
* @param none.
|
||||
*
|
||||
* @return none.
|
||||
*/
|
||||
void WCHNET_HandlePhyNegotiation(void)
|
||||
{
|
||||
if(phyLinkReset) /* After the PHY link is disconnected, wait 500ms before turning on the PHY clock*/
|
||||
{
|
||||
if( LocalTime - phyLinkTime >= 500 )
|
||||
{
|
||||
phyLinkReset = 0;
|
||||
EXTEN->EXTEN_CTR |= EXTEN_ETH_10M_EN;
|
||||
WritePHYReg(PHY_BMCR, PHY_Reset);
|
||||
PHY_NEGOTIATION_PARAM_INIT();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if( !phyStatus ) /* Handling PHY Negotiation Exceptions */
|
||||
{
|
||||
if( LocalTime - phyLinkTime >= PHY_LINK_TASK_PERIOD ) /* 50ms cycle timing call */
|
||||
{
|
||||
phyLinkTime = LocalTime;
|
||||
WCHNET_LinkProcess( );
|
||||
}
|
||||
}
|
||||
else{
|
||||
if(PhyPolarityDetect)
|
||||
{
|
||||
if( LocalTime - phyLinkTime >= 2 * PHY_LINK_TASK_PERIOD )
|
||||
{
|
||||
WCHNET_PhyPNProcess();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn WCHNET_MainTask
|
||||
*
|
||||
* @brief library main task function
|
||||
*
|
||||
* @return none.
|
||||
*/
|
||||
void WCHNET_MainTask(void)
|
||||
{
|
||||
WCHNET_NetInput( ); /* Ethernet data input */
|
||||
WCHNET_PeriodicHandle( ); /* Protocol stack time-related task processing */
|
||||
WCHNET_HandlePhyNegotiation( );
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn ETH_LedLinkSet
|
||||
*
|
||||
* @brief set eth link led,setbit 0 or 1,the link led turn on or turn off
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
void ETH_LedLinkSet( uint8_t mode )
|
||||
{
|
||||
if( mode == LED_OFF )
|
||||
{
|
||||
GPIO_SetBits(GPIOA, GPIO_Pin_1);
|
||||
}
|
||||
else
|
||||
{
|
||||
GPIO_ResetBits(GPIOA, GPIO_Pin_1);
|
||||
}
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn ETH_LedDataSet
|
||||
*
|
||||
* @brief set eth data led,setbit 0 or 1,the data led turn on or turn off
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
void ETH_LedDataSet( uint8_t mode )
|
||||
{
|
||||
if( mode == LED_OFF )
|
||||
{
|
||||
GPIO_SetBits(GPIOA, GPIO_Pin_13);
|
||||
}
|
||||
else
|
||||
{
|
||||
GPIO_ResetBits(GPIOA, GPIO_Pin_13);
|
||||
}
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn ETH_LedConfiguration
|
||||
*
|
||||
* @brief set eth data and link led pin
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
void ETH_LedConfiguration(void)
|
||||
{
|
||||
GPIO_InitTypeDef GPIO={0};
|
||||
GPIO_PinRemapConfig(GPIO_Remap_SWJ_Disable, ENABLE);
|
||||
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
|
||||
GPIO.GPIO_Pin = GPIO_Pin_1|GPIO_Pin_13;
|
||||
GPIO.GPIO_Mode = GPIO_Mode_Out_PP;
|
||||
GPIO.GPIO_Speed = GPIO_Speed_50MHz;
|
||||
GPIO_Init(GPIOA,&GPIO);
|
||||
ETH_LedDataSet(LED_OFF);
|
||||
ETH_LedLinkSet(LED_OFF);
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn ETH_DMATxDescChainInit
|
||||
*
|
||||
* @brief Initializes the DMA Tx descriptors in chain mode.
|
||||
*
|
||||
* @param DMATxDescTab - Pointer on the first Tx desc list
|
||||
* TxBuff - Pointer on the first TxBuffer list
|
||||
* TxBuffCount - Number of the used Tx desc in the list
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
void ETH_DMATxDescChainInit(ETH_DMADESCTypeDef *DMATxDescTab, uint8_t *TxBuff, uint32_t TxBuffCount)
|
||||
{
|
||||
ETH_DMADESCTypeDef *DMATxDesc;
|
||||
|
||||
DMATxDescToSet = DMATxDescTab;
|
||||
DMATxDesc = DMATxDescTab;
|
||||
DMATxDesc->Status = 0;
|
||||
DMATxDesc->Buffer1Addr = (uint32_t)TxBuff;
|
||||
DMATxDesc->Buffer2NextDescAddr = (uint32_t)DMATxDescTab;
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn ETH_DMARxDescChainInit
|
||||
*
|
||||
* @brief Initializes the DMA Rx descriptors in chain mode.
|
||||
*
|
||||
* @param DMARxDescTab - Pointer on the first Rx desc list.
|
||||
* RxBuff - Pointer on the first RxBuffer list.
|
||||
* RxBuffCount - Number of the used Rx desc in the list.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
void ETH_DMARxDescChainInit(ETH_DMADESCTypeDef *DMARxDescTab, uint8_t *RxBuff, uint32_t RxBuffCount)
|
||||
{
|
||||
uint8_t i = 0;
|
||||
ETH_DMADESCTypeDef *DMARxDesc;
|
||||
|
||||
DMARxDescToGet = DMARxDescTab;
|
||||
for(i = 0; i < RxBuffCount; i++)
|
||||
{
|
||||
DMARxDesc = DMARxDescTab + i;
|
||||
DMARxDesc->Status = ETH_DMARxDesc_OWN;
|
||||
DMARxDesc->Buffer1Addr = (uint32_t)(&RxBuff[i * ETH_MAX_PACKET_SIZE]);
|
||||
|
||||
if(i < (RxBuffCount - 1))
|
||||
{
|
||||
DMARxDesc->Buffer2NextDescAddr = (uint32_t)(DMARxDescTab + i + 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
DMARxDesc->Buffer2NextDescAddr = (uint32_t)(DMARxDescTab);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn ETH_Start
|
||||
*
|
||||
* @brief Enables ENET MAC and DMA reception/transmission.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
void ETH_Start(void)
|
||||
{
|
||||
R16_ETH_ERXST = DMARxDescToGet->Buffer1Addr;
|
||||
R8_ETH_ECON1 |= RB_ETH_ECON1_RXEN; //receive enable
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn ETH_SetClock
|
||||
*
|
||||
* @brief Set ETH Clock(60MHz).
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
void ETH_SetClock(void)
|
||||
{
|
||||
/* ETH initialization */
|
||||
RCC_ETHDIVConfig(RCC_ETHCLK_Div2); // 120M/2 = 60MHz
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn ETH_Configuration
|
||||
*
|
||||
* @brief Ethernet configure.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
void ETH_Configuration( uint8_t *macAddr )
|
||||
{
|
||||
ETH_SetClock( );
|
||||
R8_ETH_EIE = 0;
|
||||
R8_ETH_EIE |= RB_ETH_EIE_INTIE |
|
||||
RB_ETH_EIE_RXIE|
|
||||
RB_ETH_EIE_LINKIE|
|
||||
RB_ETH_EIE_TXIE |
|
||||
RB_ETH_EIE_TXERIE|
|
||||
RB_ETH_EIE_RXERIE; //Turn on all interrupts
|
||||
|
||||
R8_ETH_EIE |= RB_ETH_EIE_R_EN50; //Turn on 50 ohm pull-up
|
||||
|
||||
R8_ETH_EIR = 0xff; //clear interrupt flag
|
||||
R8_ETH_ESTAT |= RB_ETH_ESTAT_INT | RB_ETH_ESTAT_BUFER; //clear state
|
||||
|
||||
R8_ETH_ECON1 |= (RB_ETH_ECON1_TXRST|RB_ETH_ECON1_RXRST); //Transceiver module reset
|
||||
R8_ETH_ECON1 &= ~(RB_ETH_ECON1_TXRST|RB_ETH_ECON1_RXRST);
|
||||
|
||||
//Filter mode, received packet type
|
||||
R8_ETH_ERXFCON = 0;
|
||||
R8_ETH_MAADRL1 = macAddr[5]; // MAC assignment
|
||||
R8_ETH_MAADRL2 = macAddr[4];
|
||||
R8_ETH_MAADRL3 = macAddr[3];
|
||||
R8_ETH_MAADRL4 = macAddr[2];
|
||||
R8_ETH_MAADRL5 = macAddr[1];
|
||||
R8_ETH_MAADRL6 = macAddr[0];
|
||||
|
||||
//Filter mode, limit packet type
|
||||
R8_ETH_MACON1 |= RB_ETH_MACON1_MARXEN; //MAC receive enable
|
||||
R8_ETH_MACON2 &= ~RB_ETH_MACON2_PADCFG;
|
||||
R8_ETH_MACON2 |= PADCFG_AUTO_3; //All short packets are automatically padded to 60 bytes
|
||||
R8_ETH_MACON2 |= RB_ETH_MACON2_TXCRCEN; //Hardware padded CRC
|
||||
R8_ETH_MACON2 &= ~RB_ETH_MACON2_HFRMEN; //Jumbo frames are not received
|
||||
R8_ETH_MACON2 |= RB_ETH_MACON2_FULDPX;
|
||||
R16_ETH_MAMXFL = ETH_MAX_PACKET_SIZE;
|
||||
R8_ETH_ECON2 &= ~(0x07 << 1);
|
||||
R8_ETH_ECON2 |= 5 << 1;
|
||||
|
||||
EXTEN->EXTEN_CTR |= EXTEN_ETH_10M_EN;
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn ETH_TxPktChainMode
|
||||
*
|
||||
* @brief Ethernet sends data frames in chain mode.
|
||||
*
|
||||
* @param len Send data length
|
||||
* pBuff send buffer pointer
|
||||
*
|
||||
* @return Send status.
|
||||
*/
|
||||
uint32_t ETH_TxPktChainMode(uint16_t len, uint32_t *pBuff )
|
||||
{
|
||||
/* Check if the descriptor is owned by the ETHERNET DMA (when set) or CPU (when reset) */
|
||||
if( DMATxDescToSet->Status & ETH_DMATxDesc_OWN )
|
||||
{
|
||||
/* Return ERROR: OWN bit set */
|
||||
return ETH_ERROR;
|
||||
}
|
||||
DMATxDescToSet->Status |= ETH_DMATxDesc_OWN;
|
||||
R16_ETH_ETXLN = len;
|
||||
R16_ETH_ETXST = (uint32_t)pBuff;
|
||||
R8_ETH_ECON1 |= RB_ETH_ECON1_TXRTS; //start sending
|
||||
/* Update the ETHERNET DMA global Tx descriptor with next Tx descriptor */
|
||||
/* Chained Mode */
|
||||
/* Selects the next DMA Tx descriptor list for next buffer to send */
|
||||
DMATxDescToSet = (ETH_DMADESCTypeDef*) (DMATxDescToSet->Buffer2NextDescAddr);
|
||||
/* Return SUCCESS */
|
||||
return ETH_SUCCESS;
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn ETH_LinkUpCfg
|
||||
*
|
||||
* @brief When the PHY is connected, configure the relevant functions.
|
||||
*
|
||||
* @param regval BMSR register value
|
||||
*
|
||||
* @return none.
|
||||
*/
|
||||
void ETH_LinkUpCfg(uint16_t regval)
|
||||
{
|
||||
WCHNET_PhyStatus( regval );
|
||||
/* Receive CRC error packets */
|
||||
R8_ETH_ERXFCON |= RB_ETH_ERXFCON_CRCEN;
|
||||
CRCErrPktCnt = 0;
|
||||
PhyPolarityDetect = 1;
|
||||
phyLinkTime = LocalTime;
|
||||
phyStatus = PHY_Linked_Status;
|
||||
ETH_Start( );
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn ETH_LinkDownCfg
|
||||
*
|
||||
* @brief When the PHY is disconnected, configure the relevant functions.
|
||||
*
|
||||
* @param regval BMSR register value
|
||||
*
|
||||
* @return none.
|
||||
*/
|
||||
void ETH_LinkDownCfg(uint16_t regval)
|
||||
{
|
||||
WCHNET_PhyStatus( regval );
|
||||
EXTEN->EXTEN_CTR &= ~EXTEN_ETH_10M_EN;
|
||||
phyLinkReset = 1;
|
||||
phyLinkTime = LocalTime;
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn ETH_PHYLink
|
||||
*
|
||||
* @brief
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
void ETH_PHYLink( void )
|
||||
{
|
||||
u16 phy_bsr, phy_anlpar;
|
||||
|
||||
phy_bsr = ReadPHYReg(PHY_BMSR);
|
||||
phy_anlpar = ReadPHYReg(PHY_ANLPAR);
|
||||
|
||||
if(phy_bsr & PHY_Linked_Status) //Valid link established
|
||||
{
|
||||
if(phy_bsr & PHY_AutoNego_Complete) //Auto-negotiation completed -- LinkUp
|
||||
{
|
||||
ETH_LinkUpCfg(phy_bsr);
|
||||
}
|
||||
else {
|
||||
if(phy_anlpar == 0) //The auto-negotiation signal of the peer device is not obtained
|
||||
{
|
||||
WritePHYReg(PHY_BMCR, PHY_Reset);
|
||||
PHY_NEGOTIATION_PARAM_INIT();
|
||||
}
|
||||
else {
|
||||
ETH_LinkDownCfg(phy_bsr);
|
||||
}
|
||||
}
|
||||
}
|
||||
else { //LinkDown
|
||||
ETH_LinkDownCfg(phy_bsr);
|
||||
}
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn WCHNET_ETHIsr
|
||||
*
|
||||
* @brief
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
void WCHNET_ETHIsr( void )
|
||||
{
|
||||
uint8_t eth_irq_flag, estat_regval;
|
||||
|
||||
eth_irq_flag = R8_ETH_EIR;
|
||||
if(eth_irq_flag&RB_ETH_EIR_RXIF) //Receive complete
|
||||
{
|
||||
R8_ETH_EIR = RB_ETH_EIR_RXIF;
|
||||
/* Check if the descriptor is owned by the ETHERNET DMA */
|
||||
if( DMARxDescToGet->Status & ETH_DMARxDesc_OWN )
|
||||
{
|
||||
estat_regval = R8_ETH_ESTAT;
|
||||
if(estat_regval & \
|
||||
(RB_ETH_ESTAT_BUFER | RB_ETH_ESTAT_RXCRCER | RB_ETH_ESTAT_RXNIBBLE | RB_ETH_ESTAT_RXMORE))
|
||||
{
|
||||
return;
|
||||
}
|
||||
if( ((ETH_DMADESCTypeDef*)(DMARxDescToGet->Buffer2NextDescAddr))->Status& ETH_DMARxDesc_OWN )
|
||||
{
|
||||
DMARxDescToGet->Status &= ~ETH_DMARxDesc_OWN;
|
||||
DMARxDescToGet->Status &= ~ETH_DMARxDesc_ES;
|
||||
DMARxDescToGet->Status |= (ETH_DMARxDesc_FS|ETH_DMARxDesc_LS);
|
||||
DMARxDescToGet->Status &= ~ETH_DMARxDesc_FL;
|
||||
DMARxDescToGet->Status |= ((R16_ETH_ERXLN+4)<<ETH_DMARxDesc_FrameLengthShift);
|
||||
/* Update the ETHERNET DMA global Rx descriptor with next Rx descriptor */
|
||||
/* Selects the next DMA Rx descriptor list for next buffer to read */
|
||||
DMARxDescToGet = (ETH_DMADESCTypeDef*) (DMARxDescToGet->Buffer2NextDescAddr);
|
||||
R16_ETH_ERXST = DMARxDescToGet->Buffer1Addr;
|
||||
}
|
||||
}
|
||||
if(PhyPolarityDetect)
|
||||
{
|
||||
PhyPolarityDetect = 0;
|
||||
/* Discard CRC error packet */
|
||||
R8_ETH_ERXFCON &= ~RB_ETH_ERXFCON_CRCEN;
|
||||
}
|
||||
}
|
||||
if(eth_irq_flag&RB_ETH_EIR_TXIF) //send completed
|
||||
{
|
||||
DMATxDescToSet->Status &= ~ETH_DMATxDesc_OWN;
|
||||
R8_ETH_EIR = RB_ETH_EIR_TXIF;
|
||||
}
|
||||
if(eth_irq_flag&RB_ETH_EIR_LINKIF) //Link change
|
||||
{
|
||||
ETH_PHYLink();
|
||||
R8_ETH_EIR = RB_ETH_EIR_LINKIF;
|
||||
}
|
||||
if(eth_irq_flag&RB_ETH_EIR_TXERIF) //send error
|
||||
{
|
||||
DMATxDescToSet->Status &= ~ETH_DMATxDesc_OWN;
|
||||
R8_ETH_EIR = RB_ETH_EIR_TXERIF;
|
||||
}
|
||||
if(eth_irq_flag&RB_ETH_EIR_RXERIF) //receive error
|
||||
{
|
||||
if(PhyPolarityDetect) CRCErrPktCnt++;
|
||||
R8_ETH_EIR = RB_ETH_EIR_RXERIF;
|
||||
}
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn ETH_Init
|
||||
*
|
||||
* @brief Ethernet initialization.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
void ETH_Init( uint8_t *macAddr )
|
||||
{
|
||||
ETH_LedConfiguration( );
|
||||
Delay_Ms(100);
|
||||
ETH_Configuration( macAddr );
|
||||
ETH_DMATxDescChainInit(DMATxDscrTab, MACTxBuf, ETH_TXBUFNB);
|
||||
ETH_DMARxDescChainInit(DMARxDscrTab, MACRxBuf, ETH_RXBUFNB);
|
||||
pDMARxSet = DMARxDscrTab;
|
||||
NVIC_EnableIRQ(ETH_IRQn);
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn ETH_LibInit
|
||||
*
|
||||
* @brief Ethernet library initialization program
|
||||
*
|
||||
* @return command status
|
||||
*/
|
||||
uint8_t ETH_LibInit( uint8_t *ip, uint8_t *gwip, uint8_t *mask, uint8_t *macaddr )
|
||||
{
|
||||
uint8_t s;
|
||||
struct _WCH_CFG cfg;
|
||||
|
||||
memset(&cfg,0,sizeof(cfg));
|
||||
cfg.TxBufSize = ETH_TX_BUF_SZE;
|
||||
cfg.TCPMss = WCHNET_TCP_MSS;
|
||||
cfg.HeapSize = WCHNET_MEM_HEAP_SIZE;
|
||||
cfg.ARPTableNum = WCHNET_NUM_ARP_TABLE;
|
||||
cfg.MiscConfig0 = WCHNET_MISC_CONFIG0;
|
||||
cfg.MiscConfig1 = WCHNET_MISC_CONFIG1;
|
||||
cfg.led_link = ETH_LedLinkSet;
|
||||
cfg.led_data = ETH_LedDataSet;
|
||||
cfg.net_send = ETH_TxPktChainMode;
|
||||
cfg.CheckValid = WCHNET_CFG_VALID;
|
||||
s = WCHNET_ConfigLIB(&cfg);
|
||||
if(s){
|
||||
return (s);
|
||||
}
|
||||
s = WCHNET_Init(ip,gwip,mask,macaddr);
|
||||
ETH_Init(macaddr);
|
||||
return (s);
|
||||
}
|
||||
|
||||
/******************************** endfile @ eth_driver ******************************/
|
||||
103
vd960DBN/ETH/NetLib/eth_driver.h
Normal file
103
vd960DBN/ETH/NetLib/eth_driver.h
Normal file
@@ -0,0 +1,103 @@
|
||||
/********************************** (C) COPYRIGHT *******************************
|
||||
* File Name : eth_driver.h
|
||||
* Author : WCH
|
||||
* Version : V1.3.0
|
||||
* Date : 2022/05/27
|
||||
* Description : This file contains the headers of the ETH Driver.
|
||||
*********************************************************************************
|
||||
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
|
||||
* Attention: This software (modified or not) and binary are used for
|
||||
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
|
||||
*******************************************************************************/
|
||||
#ifndef __ETH_DRIVER__
|
||||
#define __ETH_DRIVER__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "debug.h"
|
||||
|
||||
#define ROM_CFG_USERADR_ID 0x1FFFF7E8
|
||||
|
||||
#define PHY_LINK_TASK_PERIOD 50
|
||||
|
||||
#define PHY_ANLPAR_SELECTOR_FIELD 0x1F
|
||||
#define PHY_ANLPAR_SELECTOR_VALUE 0x01 /* 5B'00001 */
|
||||
|
||||
#define PHY_LINK_INIT 0x00
|
||||
#define PHY_LINK_SUC_P (1<<0)
|
||||
#define PHY_LINK_SUC_N (1<<1)
|
||||
#define PHY_LINK_WAIT_SUC (1<<7)
|
||||
|
||||
#define PHY_PN_SWITCH_P (0<<2)
|
||||
#define PHY_PN_SWITCH_N (1<<2)
|
||||
#define PHY_PN_SWITCH_AUTO (2<<2)
|
||||
|
||||
#ifndef WCHNETTIMERPERIOD
|
||||
#define WCHNETTIMERPERIOD 10 /* Timer period, in Ms. */
|
||||
#endif
|
||||
|
||||
#define PHY_NEGOTIATION_PARAM_INIT() do{\
|
||||
phySucCnt = 0;\
|
||||
phyStatus = 0;\
|
||||
phyLinkCnt = 0;\
|
||||
phyRetryCnt = 0;\
|
||||
phyPNChangeCnt = 0;\
|
||||
phyLinkStatus = PHY_LINK_INIT;\
|
||||
}while(0)
|
||||
|
||||
/* definition for Ethernet frame */
|
||||
#define ETH_MAX_PACKET_SIZE 576 //1024 //1536 /* ETH_HEADER + VLAN_TAG + MAX_ETH_PAYLOAD + ETH_CRC */
|
||||
#define ETH_HEADER 14 /* 6 byte Dest addr, 6 byte Src addr, 2 byte length/type */
|
||||
#define ETH_CRC 4 /* Ethernet CRC */
|
||||
#define ETH_EXTRA 2 /* Extra bytes in some cases */
|
||||
#define VLAN_TAG 4 /* optional 802.1q VLAN Tag */
|
||||
#define MIN_ETH_PAYLOAD 46 /* Minimum Ethernet payload size */
|
||||
#define MAX_ETH_PAYLOAD 1500 /* Maximum Ethernet payload size */
|
||||
|
||||
/* Bit or field definition of TDES0 register (DMA Tx descriptor status register)*/
|
||||
#define ETH_DMATxDesc_OWN ((uint32_t)0x80000000) /* OWN bit: descriptor is owned by DMA engine */
|
||||
|
||||
/* Bit or field definition of RDES0 register (DMA Rx descriptor status register) */
|
||||
#define ETH_DMARxDesc_OWN ((uint32_t)0x80000000) /* OWN bit: descriptor is owned by DMA engine */
|
||||
#define ETH_DMARxDesc_FL ((uint32_t)0x3FFF0000) /* Receive descriptor frame length */
|
||||
#define ETH_DMARxDesc_ES ((uint32_t)0x00008000) /* Error summary: */
|
||||
#define ETH_DMARxDesc_FS ((uint32_t)0x00000200) /* First descriptor of the frame */
|
||||
#define ETH_DMARxDesc_LS ((uint32_t)0x00000100) /* Last descriptor of the frame */
|
||||
|
||||
#define ETH_DMARxDesc_FrameLengthShift 16
|
||||
|
||||
/* ETHERNET errors */
|
||||
#define ETH_ERROR ((uint32_t)0)
|
||||
#define ETH_SUCCESS ((uint32_t)1)
|
||||
|
||||
/* ETH structure definition */
|
||||
typedef struct
|
||||
{
|
||||
uint32_t volatile Status; /* Status */
|
||||
uint32_t ControlBufferSize; /* Control and Buffer1, Buffer2 lengths */
|
||||
uint32_t Buffer1Addr; /* Buffer1 address pointer */
|
||||
uint32_t Buffer2NextDescAddr; /* Buffer2 or next descriptor address pointer */
|
||||
} ETH_DMADESCTypeDef;
|
||||
|
||||
#include "wchnet.h"
|
||||
|
||||
extern SOCK_INF SocketInf[ ];
|
||||
|
||||
void ETH_PHYLink( void );
|
||||
void WCHNET_ETHIsr( void );
|
||||
void WCHNET_MainTask( void );
|
||||
void ETH_LedConfiguration(void);
|
||||
void ETH_Init( uint8_t *macAddr );
|
||||
void ETH_LedLinkSet( uint8_t mode );
|
||||
void ETH_LedDataSet( uint8_t mode );
|
||||
void WCHNET_TimeIsr( uint16_t timperiod );
|
||||
void ETH_Configuration( uint8_t *macAddr );
|
||||
uint8_t ETH_LibInit( uint8_t *ip, uint8_t *gwip, uint8_t *mask, uint8_t *macaddr);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
605
vd960DBN/ETH/NetLib/wchnet.h
Normal file
605
vd960DBN/ETH/NetLib/wchnet.h
Normal file
@@ -0,0 +1,605 @@
|
||||
/********************************** (C) COPYRIGHT *******************************
|
||||
* File Name : wchnet.h
|
||||
* Author : WCH
|
||||
* Version : V1.90
|
||||
* Date : 2023/05/12
|
||||
* Description : This file contains the headers of
|
||||
* the Ethernet protocol stack library.
|
||||
*********************************************************************************
|
||||
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
|
||||
* Attention: This software (modified or not) and binary are used for
|
||||
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
|
||||
*******************************************************************************/
|
||||
#ifndef __WCHNET_H__
|
||||
#define __WCHNET_H__
|
||||
|
||||
#include "stdint.h"
|
||||
#ifndef NET_LIB
|
||||
#include "net_config.h"
|
||||
#endif
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define WCHNET_LIB_VER 0x1A //the library version number
|
||||
#define WCHNET_CFG_VALID 0x12345678 //Configuration value valid flag
|
||||
|
||||
/* LED state @LED_STAT */
|
||||
#define LED_ON 0
|
||||
#define LED_OFF 1
|
||||
|
||||
/* PHY state @PHY_STAT */
|
||||
#define PHY_LINK_SUCCESS (1 << 2) //PHY connection success
|
||||
#define PHY_AUTO_SUCCESS (1 << 5) //PHY auto negotiation completed
|
||||
|
||||
/* Library initialization state @CFG_INIT_STAT */
|
||||
#define INIT_OK 0x00
|
||||
#define INIT_ERR_RX_BUF_SIZE 0x01
|
||||
#define INIT_ERR_TCP_MSS 0x02
|
||||
#define INIT_ERR_HEAP_SIZE 0x03
|
||||
#define INIT_ERR_ARP_TABLE_NEM 0x04
|
||||
#define INIT_ERR_MISC_CONFIG0 0x05
|
||||
#define INIT_ERR_MISC_CONFIG1 0x06
|
||||
#define INIT_ERR_FUNC_SEND 0x09
|
||||
#define INIT_ERR_CHECK_VALID 0xFF
|
||||
|
||||
/* Socket protocol type */
|
||||
#define PROTO_TYPE_IP_RAW 0 //IP layer raw data
|
||||
#define PROTO_TYPE_UDP 2 //UDP protocol
|
||||
#define PROTO_TYPE_TCP 3 //TCP protocol
|
||||
|
||||
/* interrupt status */
|
||||
/* The following are the states
|
||||
* that GLOB_INT will generate */
|
||||
#define GINT_STAT_UNREACH (1 << 0) //unreachable interrupt
|
||||
#define GINT_STAT_IP_CONFLI (1 << 1) //IP conflict interrupt
|
||||
#define GINT_STAT_PHY_CHANGE (1 << 2) //PHY state change interrupt
|
||||
#define GINT_STAT_SOCKET (1 << 4) //socket related interrupt
|
||||
|
||||
/* The following are the states
|
||||
* that Sn_INT will generate*/
|
||||
#define SINT_STAT_RECV (1 << 2) //the socket receives data or the receive buffer is not empty
|
||||
#define SINT_STAT_CONNECT (1 << 3) //connect successfully,generated in TCP mode
|
||||
#define SINT_STAT_DISCONNECT (1 << 4) //disconnect,generated in TCP mode
|
||||
#define SINT_STAT_TIM_OUT (1 << 6) //timeout disconnect,generated in TCP mode
|
||||
|
||||
|
||||
/* Definitions for error constants. @ERR_T */
|
||||
#define ERR_T
|
||||
#define WCHNET_ERR_SUCCESS 0x00 //No error, everything OK
|
||||
#define WCHNET_ERR_BUSY 0x10 //busy
|
||||
#define WCHNET_ERR_MEM 0x11 //Out of memory error
|
||||
#define WCHNET_ERR_BUF 0x12 //Buffer error
|
||||
#define WCHNET_ERR_TIMEOUT 0x13 //Timeout
|
||||
#define WCHNET_ERR_RTE 0x14 //Routing problem
|
||||
#define WCHNET_ERR_ABRT 0x15 //Connection aborted
|
||||
#define WCHNET_ERR_RST 0x16 //Connection reset
|
||||
#define WCHNET_ERR_CLSD 0x17 //Connection closed
|
||||
#define WCHNET_ERR_CONN 0x18 //Not connected
|
||||
#define WCHNET_ERR_VAL 0x19 //Illegal value
|
||||
#define WCHNET_ERR_ARG 0x1a //Illegal argument
|
||||
#define WCHNET_ERR_USE 0x1b //Address in use
|
||||
#define WCHNET_ERR_IF 0x1c //Low-level netif error
|
||||
#define WCHNET_ERR_ISCONN 0x1d //Already connected
|
||||
#define WCHNET_ERR_INPROGRESS 0x1e //Operation in progress
|
||||
#define WCHNET_ERR_SOCKET_MEM 0X20 //Socket information error
|
||||
#define WCHNET_ERR_UNSUPPORT_PROTO 0X21 //unsupported protocol type
|
||||
#define WCHNET_RET_ABORT 0x5F //command process fail
|
||||
#define WCHNET_ERR_UNKNOW 0xFA //unknow
|
||||
|
||||
/* unreachable condition related codes */
|
||||
#define UNREACH_CODE_HOST 0 //host unreachable
|
||||
#define UNREACH_CODE_NET 1 //network unreachable
|
||||
#define UNREACH_CODE_PROTOCOL 2 //protocol unreachable
|
||||
#define UNREACH_CODE_PROT 3 //port unreachable
|
||||
/*For other values, please refer to the RFC792 document*/
|
||||
|
||||
/* TCP disconnect related codes */
|
||||
#define TCP_CLOSE_NORMAL 0 //normal disconnect,a four-way handshake
|
||||
#define TCP_CLOSE_RST 1 //reset the connection and close
|
||||
#define TCP_CLOSE_ABANDON 2 //drop connection, and no termination message is sent
|
||||
|
||||
/* socket state code */
|
||||
#define SOCK_STAT_CLOSED 0X00 //socket close
|
||||
#define SOCK_STAT_OPEN 0X05 //socket open
|
||||
|
||||
/* TCP state code */
|
||||
#define TCP_CLOSED 0 //TCP close
|
||||
#define TCP_LISTEN 1 //TCP listening
|
||||
#define TCP_SYN_SENT 2 //SYN send, connect request
|
||||
#define TCP_SYN_RCVD 3 //SYN received, connection request received
|
||||
#define TCP_ESTABLISHED 4 //TCP connection establishment
|
||||
#define TCP_FIN_WAIT_1 5 //WAIT_1 state
|
||||
#define TCP_FIN_WAIT_2 6 //WAIT_2 state
|
||||
#define TCP_CLOSE_WAIT 7 //wait to close
|
||||
#define TCP_CLOSING 8 //closing
|
||||
#define TCP_LAST_ACK 9 //LAST_ACK
|
||||
#define TCP_TIME_WAIT 10 //2MSL wait
|
||||
|
||||
/* The following values are fixed and cannot be changed */
|
||||
#define WCHNET_MEM_ALIGN_SIZE(size) (((size) + WCHNET_MEM_ALIGNMENT - 1) & ~(WCHNET_MEM_ALIGNMENT - 1))
|
||||
#define WCHNET_SIZE_IPRAW_PCB 0x1C //IPRAW PCB size
|
||||
#define WCHNET_SIZE_UDP_PCB 0x20 //UDP PCB size
|
||||
#define WCHNET_SIZE_TCP_PCB 0xB4 //TCP PCB size
|
||||
#define WCHNET_SIZE_TCP_PCB_LISTEN 0x24 //TCP LISTEN PCB size
|
||||
#define WCHNET_SIZE_IP_REASSDATA 0x20 //IP reassembled Management
|
||||
#define WCHNET_SIZE_PBUF 0x10 //Packet Buf
|
||||
#define WCHNET_SIZE_TCP_SEG 0x14 //TCP SEG structure
|
||||
#define WCHNET_SIZE_MEM 0x08 //sizeof(struct mem)
|
||||
#define WCHNET_SIZE_ARP_TABLE 0x18 //sizeof ARP table
|
||||
|
||||
#define WCHNET_MEMP_SIZE ((WCHNET_MEM_ALIGNMENT - 1) + \
|
||||
(WCHNET_NUM_IPRAW * WCHNET_MEM_ALIGN_SIZE(WCHNET_SIZE_IPRAW_PCB)) + \
|
||||
(WCHNET_NUM_UDP * WCHNET_MEM_ALIGN_SIZE(WCHNET_SIZE_UDP_PCB)) + \
|
||||
(WCHNET_NUM_TCP * WCHNET_MEM_ALIGN_SIZE(WCHNET_SIZE_TCP_PCB)) + \
|
||||
(WCHNET_NUM_TCP_LISTEN * WCHNET_MEM_ALIGN_SIZE(WCHNET_SIZE_TCP_PCB_LISTEN)) + \
|
||||
(WCHNET_NUM_TCP_SEG * WCHNET_MEM_ALIGN_SIZE(WCHNET_SIZE_TCP_SEG)) + \
|
||||
(WCHNET_NUM_IP_REASSDATA * WCHNET_MEM_ALIGN_SIZE(WCHNET_SIZE_IP_REASSDATA)) + \
|
||||
(WCHNET_NUM_PBUF * WCHNET_MEM_ALIGN_SIZE(WCHNET_SIZE_PBUF)) + \
|
||||
(WCHNET_NUM_POOL_BUF * (WCHNET_MEM_ALIGN_SIZE(WCHNET_SIZE_PBUF) + WCHNET_MEM_ALIGN_SIZE(WCHNET_SIZE_POOL_BUF))))
|
||||
|
||||
#define HEAP_MEM_ALIGN_SIZE (WCHNET_MEM_ALIGN_SIZE(WCHNET_SIZE_MEM))
|
||||
#define WCHNET_RAM_HEAP_SIZE (WCHNET_MEM_ALIGN_SIZE(WCHNET_MEM_HEAP_SIZE) + HEAP_MEM_ALIGN_SIZE )
|
||||
#define WCHNET_RAM_ARP_TABLE_SIZE (WCHNET_MEM_ALIGN_SIZE(WCHNET_SIZE_ARP_TABLE) * WCHNET_NUM_ARP_TABLE)
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint32_t length;
|
||||
uint32_t buffer;
|
||||
}ETHFrameType;
|
||||
|
||||
/* LED callback type */
|
||||
typedef void (*led_callback)( uint8_t setbit );
|
||||
|
||||
/* net send callback type */
|
||||
typedef uint32_t (*eth_tx_set )( uint16_t len, uint32_t *pBuff );
|
||||
|
||||
/* net receive callback type */
|
||||
typedef uint32_t (*eth_rx_set )( ETHFrameType *pkt );
|
||||
|
||||
/* DNS callback type */
|
||||
typedef void (*dns_callback)( const char *name, uint8_t *ipaddr, void *callback_arg );
|
||||
|
||||
/* DHCP callback type */
|
||||
typedef uint8_t (*dhcp_callback)( uint8_t status, void * );
|
||||
|
||||
/* socket receive callback type */
|
||||
struct _SOCK_INF;
|
||||
typedef void (*pSockRecv)( struct _SOCK_INF *, uint32_t, uint16_t, uint8_t *, uint32_t);
|
||||
|
||||
/* Socket information struct */
|
||||
typedef struct _SOCK_INF
|
||||
{
|
||||
uint32_t IntStatus; //interrupt state
|
||||
uint32_t SockIndex; //Socket index value
|
||||
uint32_t RecvStartPoint; //Start pointer of the receive buffer
|
||||
uint32_t RecvBufLen; //Receive buffer length
|
||||
uint32_t RecvCurPoint; //current pointer to receive buffer
|
||||
uint32_t RecvReadPoint; //The read pointer of the receive buffer
|
||||
uint32_t RecvRemLen; //The length of the remaining data in the receive buffer
|
||||
uint32_t ProtoType; //protocol type
|
||||
uint32_t SockStatus; //Low byte Socket state, the next low byte is TCP state, only meaningful in TCP mode
|
||||
uint32_t DesPort; //destination port
|
||||
uint32_t SourPort; //Source port, protocol type in IPRAW mode
|
||||
uint8_t IPAddr[4]; //Socket destination IP address
|
||||
void *Resv1; //Reserved, for internal use, for saving individual PCBs
|
||||
void *Resv2; //Reserved, used internally, used by TCP Server
|
||||
pSockRecv AppCallBack; //receive callback function
|
||||
} SOCK_INF;
|
||||
|
||||
struct _WCH_CFG
|
||||
{
|
||||
uint32_t TxBufSize; //MAC send buffer size, reserved for use
|
||||
uint32_t TCPMss; //TCP MSS size
|
||||
uint32_t HeapSize; //heap memory size
|
||||
uint32_t ARPTableNum; //Number of ARP lists
|
||||
uint32_t MiscConfig0; //Miscellaneous Configuration 0
|
||||
/* Bit 0 TCP send buffer copy 1: copy, 0: not copy */
|
||||
/* Bit 1 TCP receive replication optimization, used for internal debugging */
|
||||
/* bit 2 delete oldest TCP connection 1: enable, 0: disable */
|
||||
/* Bits 3-7 Number of PBUFs of IP segments */
|
||||
/* Bit 8 TCP Delay ACK disable */
|
||||
uint32_t MiscConfig1; //Miscellaneous Configuration 1
|
||||
/* Bits 0-7 Number of Sockets*/
|
||||
/* Bits 8-12 Reserved */
|
||||
/* Bit 13 PING enable, 1: On 0: Off */
|
||||
/* Bits 14-18 TCP retransmission times */
|
||||
/* Bits 19-23 TCP retransmission period, in 50 milliseconds */
|
||||
/* bit 25 send failed retry, 1: enable, 0: disable */
|
||||
/* bit 26 Select whether to perform IPv4 checksum check on
|
||||
* the TCP/UDP/ICMP header of the received frame payload by hardware,
|
||||
* and calculate and insert the checksum of the IP header and payload of the sent frame by hardware.*/
|
||||
/* Bits 27-31 period (in 250 milliseconds) of Fine DHCP periodic process */
|
||||
led_callback led_link; //PHY Link Status Indicator
|
||||
led_callback led_data; //Ethernet communication indicator
|
||||
eth_tx_set net_send; //Ethernet send
|
||||
eth_rx_set net_recv; //Ethernet receive
|
||||
uint32_t CheckValid; //Configuration value valid flag, fixed value @WCHNET_CFG_VALID
|
||||
};
|
||||
|
||||
struct _NET_SYS
|
||||
{
|
||||
uint8_t IPAddr[4]; //IP address
|
||||
uint8_t GWIPAddr[4]; //Gateway IP address
|
||||
uint8_t MASKAddr[4]; //subnet mask
|
||||
uint8_t MacAddr[8]; //MAC address
|
||||
uint8_t UnreachIPAddr[4]; //Unreachable IP address
|
||||
uint32_t RetranCount; //number of retries,default is 10 times
|
||||
uint32_t RetranPeriod; //Retry period, unit MS, default 500MS
|
||||
uint32_t PHYStat; //PHY state code
|
||||
uint32_t NetStat; //The status of the Ethernet, including whether it is open, etc.
|
||||
uint32_t MackFilt; //MAC filtering, the default is to receive broadcasts, receive local MAC
|
||||
uint32_t GlobIntStatus; //global interrupt
|
||||
uint32_t UnreachCode; //unreachable code
|
||||
uint32_t UnreachProto; //unreachable protocol
|
||||
uint32_t UnreachPort; //unreachable port
|
||||
uint32_t SendFlag;
|
||||
uint32_t Flags;
|
||||
};
|
||||
|
||||
/* KEEP LIVE configuration structure */
|
||||
struct _KEEP_CFG
|
||||
{
|
||||
uint32_t KLIdle; //KEEPLIVE idle time, in ms
|
||||
uint32_t KLIntvl; //KEEPLIVE period, in ms
|
||||
uint32_t KLCount; //KEEPLIVE times
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Library initialization .
|
||||
*
|
||||
* @param ip - IP address pointer
|
||||
* @param gwip - Gateway address pointer
|
||||
* @param mask - Subnet mask pointer
|
||||
* @param macaddr - MAC address pointer
|
||||
*
|
||||
* @return @ERR_T
|
||||
*/
|
||||
uint8_t WCHNET_Init(const uint8_t *ip, const uint8_t *gwip, const uint8_t *mask, const uint8_t *macaddr);
|
||||
|
||||
/**
|
||||
* @brief get library version
|
||||
*
|
||||
* @param None
|
||||
*
|
||||
* @return library version
|
||||
*/
|
||||
uint8_t WCHNET_GetVer(void);
|
||||
|
||||
/**
|
||||
* @brief Get MAC address.
|
||||
*
|
||||
* @param(in) macaddr - MAC address
|
||||
*
|
||||
* @param(out) MAC address
|
||||
*
|
||||
* @return None
|
||||
*/
|
||||
void WCHNET_GetMacAddr(uint8_t *macaddr);
|
||||
|
||||
/**
|
||||
* @brief Library parameter configuration.
|
||||
*
|
||||
* @param cfg - Configuration parameter @_WCH_CFG
|
||||
*
|
||||
* @return Library configuration initialization state @CFG_INIT_STAT
|
||||
*/
|
||||
uint8_t WCHNET_ConfigLIB(struct _WCH_CFG *cfg);
|
||||
|
||||
/**
|
||||
* @brief Handle periodic tasks in the protocol stack
|
||||
*
|
||||
* @param None
|
||||
*
|
||||
* @return None
|
||||
*/
|
||||
void WCHNET_PeriodicHandle(void);
|
||||
|
||||
/**
|
||||
* @brief Ethernet data input. Always called in the main program,
|
||||
* or called after the reception interrupt is detected.
|
||||
*
|
||||
* @param
|
||||
*
|
||||
* @return None
|
||||
*/
|
||||
void WCHNET_NetInput( void );
|
||||
|
||||
/**
|
||||
* @brief Ethernet interrupt service function. Called after
|
||||
* Ethernet interrupt is generated.
|
||||
*
|
||||
* @param None
|
||||
*
|
||||
* @return None
|
||||
*/
|
||||
void WCHNET_ETHIsr(void);
|
||||
|
||||
/**
|
||||
* @brief Get PHY status
|
||||
*
|
||||
* @param None
|
||||
*
|
||||
* @return PHY status @PHY_STAT
|
||||
*/
|
||||
uint8_t WCHNET_GetPHYStatus(void);
|
||||
|
||||
/**
|
||||
* @brief Query global interrupt status.
|
||||
*
|
||||
* @param None
|
||||
*
|
||||
* @return GLOB_INT
|
||||
*/
|
||||
uint8_t WCHNET_QueryGlobalInt(void);
|
||||
|
||||
/**
|
||||
* @brief Read global interrupt and clear it.
|
||||
*
|
||||
* @param None
|
||||
*
|
||||
* @return GLOB_INT
|
||||
*/
|
||||
uint8_t WCHNET_GetGlobalInt(void);
|
||||
|
||||
/**
|
||||
* @brief create socket
|
||||
*
|
||||
* @param(in) *socketid - socket variable pointer
|
||||
* @param socinf - Configuration parameters for creating sockets @SOCK_INF
|
||||
*
|
||||
* @param(out) *socketid - socket value
|
||||
*
|
||||
* @return @ERR_T
|
||||
*/
|
||||
uint8_t WCHNET_SocketCreat( uint8_t *socketid, SOCK_INF *socinf);
|
||||
|
||||
/**
|
||||
* @brief Socket sends data.
|
||||
*
|
||||
* @param socketid - socket id value
|
||||
* @param *buf - the first address of send buffer
|
||||
* @param(in) *len - pointer to the length of the data expected to be sent
|
||||
*
|
||||
* @param(out) *len - pointer to the length of the data sent actually
|
||||
*
|
||||
* @return @ERR_T
|
||||
*/
|
||||
uint8_t WCHNET_SocketSend( uint8_t socketid, uint8_t *buf, uint32_t *len);
|
||||
|
||||
/**
|
||||
* @brief Socket receives data.
|
||||
*
|
||||
* @param socketid - socket id value
|
||||
* @param *buf - the first address of receive buffer
|
||||
* @param(in) *len - pointer to the length of the data expected to be read
|
||||
*
|
||||
* @param(out) *buf - the first address of data buffer
|
||||
* @param(out) *len - pointer to the length of the data read actually
|
||||
*
|
||||
* @return @ERR_T
|
||||
*/
|
||||
uint8_t WCHNET_SocketRecv( uint8_t socketid, uint8_t *buf, uint32_t *len);
|
||||
|
||||
/**
|
||||
* @brief Get socket interrupt, and clear socket interrupt.
|
||||
*
|
||||
* @param socketid - socket id value
|
||||
*
|
||||
* @return Sn_INT
|
||||
*/
|
||||
uint8_t WCHNET_GetSocketInt( uint8_t socketid );
|
||||
|
||||
/**
|
||||
* @brief Get the length of the data received by socket.
|
||||
*
|
||||
* @param socketid - socket id value
|
||||
* @param(in) *bufaddr - the first address of receive buffer
|
||||
*
|
||||
* @param(out) *bufaddr - the first address of data buffer
|
||||
*
|
||||
* @return the length of the data
|
||||
*/
|
||||
uint32_t WCHNET_SocketRecvLen( uint8_t socketid, uint32_t *bufaddr);
|
||||
|
||||
/**
|
||||
* @brief TCP connect. Used in TCP Client mode.
|
||||
*
|
||||
* @param socketid - socket id value
|
||||
*
|
||||
* @return @ERR_T
|
||||
*/
|
||||
uint8_t WCHNET_SocketConnect( uint8_t socketid);
|
||||
|
||||
/**
|
||||
* @brief TCP listen. Used in TCP SERVER mode.
|
||||
*
|
||||
* @param socketid - socket id value
|
||||
*
|
||||
* @return @ERR_T
|
||||
*/
|
||||
uint8_t WCHNET_SocketListen( uint8_t socketid);
|
||||
|
||||
/**
|
||||
* @brief Close socket.
|
||||
*
|
||||
* @param socketid - socket id value
|
||||
* @param mode - the way of disconnection.Used in TCP connection.
|
||||
* @TCP disconnect related codes
|
||||
*
|
||||
* @return @ERR_T
|
||||
*/
|
||||
uint8_t WCHNET_SocketClose( uint8_t socketid, uint8_t mode );
|
||||
|
||||
/**
|
||||
* @brief Modify socket receive buffer.
|
||||
*
|
||||
* @param socketid - socket id value
|
||||
* @param bufaddr - Address of the receive buffer
|
||||
* @param bufsize - Size of the receive buffer
|
||||
*
|
||||
* @return None
|
||||
*/
|
||||
void WCHNET_ModifyRecvBuf( uint8_t socketid, uint32_t bufaddr, uint32_t bufsize);
|
||||
|
||||
/**
|
||||
* @brief UDP send, specify the target IP and target port
|
||||
*
|
||||
* @param socketid - socket id value
|
||||
* @param *buf - Address of the sent data
|
||||
* @param(in) *slen - Address of the sent length
|
||||
* @param *sip - destination IP address
|
||||
* @param port - destination port
|
||||
*
|
||||
* @param(out) *slen - actual length sent
|
||||
*
|
||||
* @return @ERR_T
|
||||
*/
|
||||
uint8_t WCHNET_SocketUdpSendTo( uint8_t socketid, uint8_t *buf, uint32_t *slen, uint8_t *sip, uint16_t port);
|
||||
|
||||
/**
|
||||
* @brief Convert ASCII address to network address.
|
||||
*
|
||||
* @param *cp - ASCII address to be converted, such as "192.168.1.2"
|
||||
* @param(in) *addr - First address of the memory stored in the converted network address
|
||||
* @param(out) *addr - Converted network address, such as 0xC0A80102
|
||||
* @return 0 - Success. Others - Failure.
|
||||
*/
|
||||
uint8_t WCHNET_Aton(const char *cp, uint8_t *addr);
|
||||
|
||||
/**
|
||||
* @brief Convert network address to ASCII address.
|
||||
*
|
||||
* @param *ipaddr - socket id value
|
||||
*
|
||||
* @return Converted ASCII address
|
||||
*/
|
||||
uint8_t *WCHNET_Ntoa( uint8_t *ipaddr);
|
||||
|
||||
/**
|
||||
* @brief Set socket TTL.
|
||||
*
|
||||
* @param socketid - socket id value
|
||||
* @param ttl - TTL value
|
||||
*
|
||||
* @return @ERR_T
|
||||
*/
|
||||
uint8_t WCHNET_SetSocketTTL( uint8_t socketid, uint8_t ttl);
|
||||
|
||||
/**
|
||||
* @brief Start TCP retry sending immediately.
|
||||
*
|
||||
* @param socketid - TTL value
|
||||
*
|
||||
* @return None
|
||||
*/
|
||||
void WCHNET_RetrySendUnack( uint8_t socketid);
|
||||
|
||||
/**
|
||||
* @brief Query the packets that are not sent successfully.
|
||||
*
|
||||
* @param socketid - TTL value
|
||||
* @param(in) *addrlist - pointer to the address of the address list
|
||||
* @param lislen - Length of the list
|
||||
*
|
||||
* @param(out) *addrlist - Address list of the data packets that are not sent successfully
|
||||
*
|
||||
* @return Number of unsent and unacknowledged segments
|
||||
*/
|
||||
uint8_t WCHNET_QueryUnack( uint8_t socketid, uint32_t *addrlist, uint16_t lislen );
|
||||
|
||||
/**
|
||||
* @brief Start DHCP.
|
||||
*
|
||||
* @param dhcp - Application layer callback function
|
||||
*
|
||||
* @return @ERR_T
|
||||
*/
|
||||
uint8_t WCHNET_DHCPStart( dhcp_callback dhcp );
|
||||
|
||||
/**
|
||||
* @brief Stop DHCP.
|
||||
*
|
||||
* @param None
|
||||
*
|
||||
* @return @ERR_T
|
||||
*/
|
||||
uint8_t WCHNET_DHCPStop( void );
|
||||
|
||||
/**
|
||||
* @brief Configure DHCP host name.
|
||||
*
|
||||
* @param *name - First address of DHCP host name
|
||||
*
|
||||
* @return 0 - Success. Others - Failure.
|
||||
*/
|
||||
uint8_t WCHNET_DHCPSetHostname(char *name);
|
||||
|
||||
/**
|
||||
* @brief Initialize the resolver: set up the UDP pcb and configure the default server
|
||||
*
|
||||
* @param *dnsip - the IP address of dns server
|
||||
* @param port - the port number of dns server
|
||||
*
|
||||
* @return None
|
||||
*/
|
||||
void WCHNET_InitDNS( uint8_t *dnsip, uint16_t port);
|
||||
|
||||
/**
|
||||
* @brief Stop DNS.
|
||||
*
|
||||
* @param None
|
||||
*
|
||||
* @return None
|
||||
*/
|
||||
void WCHNET_DNSStop(void);
|
||||
|
||||
/**
|
||||
* Resolve a hostname (string) into an IP address.
|
||||
*
|
||||
* @param hostname - the hostname that is to be queried
|
||||
* @param addr - pointer to a struct ip_addr where to store the address if it is already
|
||||
* cached in the dns_table (only valid if ERR_OK is returned!)
|
||||
* @param found - a callback function to be called on success, failure or timeout (only if
|
||||
* ERR_INPROGRESS is returned!)
|
||||
* @param arg - argument to pass to the callback function
|
||||
*
|
||||
* @return @ERR_T
|
||||
* WCHNET_ERR_SUCCESS if hostname is a valid IP address string or the host name is already in the local names table.
|
||||
* ERR_INPROGRESS enqueue a request to be sent to the DNS server for resolution if no errors are present.
|
||||
*/
|
||||
uint8_t WCHNET_HostNameGetIp( const char *hostname, uint8_t *addr, dns_callback found, void *arg );
|
||||
|
||||
/**
|
||||
* @brief Configure KEEP LIVE parameter.
|
||||
*
|
||||
* @param *cfg - KEEPLIVE configuration parameter
|
||||
*
|
||||
* @return None
|
||||
*/
|
||||
void WCHNET_ConfigKeepLive( struct _KEEP_CFG *cfg );
|
||||
|
||||
/**
|
||||
* @brief Configure socket KEEP LIVE enable.
|
||||
*
|
||||
* @param socketid - socket id value
|
||||
* @param enable - 1: Enabled. 0: Disabled.
|
||||
*
|
||||
* @return @ERR_T
|
||||
*/
|
||||
uint8_t WCHNET_SocketSetKeepLive( uint8_t socketid, uint8_t enable );
|
||||
|
||||
/**
|
||||
* @brief Configure PHY state
|
||||
*
|
||||
* @param phy_stat - PHY state
|
||||
*
|
||||
* @return None
|
||||
*/
|
||||
void WCHNET_PhyStatus( uint32_t phy_stat );
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
@@ -1,47 +0,0 @@
|
||||
# vd960DBN — 通信子项目
|
||||
|
||||
## MCU
|
||||
|
||||
沁恒 CH32V208(RISC-V RV32IMAC)
|
||||
|
||||
| 参数 | 数值 |
|
||||
|------|------|
|
||||
| 主频 | 144 MHz |
|
||||
| Flash | 128 KB |
|
||||
| SRAM | 64 KB |
|
||||
|
||||
## 功能
|
||||
|
||||
- **蓝牙(BLE)**:无线配置、状态查询、固件升级
|
||||
- **以太网**:TCP/IP 协议栈,上位机通信(Modbus TCP / 私有协议)
|
||||
- **RS485(1路)**:与外部设备/系统集成(Modbus RTU)
|
||||
- **与 Loop 通信**:接收线圈检测结果,协议转发
|
||||
|
||||
## 目录结构
|
||||
|
||||
```
|
||||
vd960DBN/
|
||||
├── src/ # 源代码
|
||||
├── inc/ # 头文件
|
||||
├── lib/ # 库文件(HAL、CMSIS、协议栈)
|
||||
│ ├── HAL/ # CH32V208 标准外设库
|
||||
│ └── CMSIS/ # RISC-V CMSIS 适配
|
||||
├── docs/ # 文档
|
||||
└── tools/ # 辅助工具/脚本
|
||||
```
|
||||
|
||||
## 通信接口
|
||||
|
||||
| 接口 | 用途 | 备注 |
|
||||
|------|------|------|
|
||||
| BLE (2.4GHz) | 现场调试 / 配置 | 手机 APP 连接 |
|
||||
| Ethernet (10/100M) | 上位机实时数据 | Modbus TCP / HTTP |
|
||||
| RS485 | 系统集成 | Modbus RTU |
|
||||
| UART/SPI | 与 Loop MCU 通信 | 内部互联 |
|
||||
|
||||
## 关键技术点
|
||||
|
||||
- 协议栈:lwIP(轻量级 TCP/IP)
|
||||
- BLE Stack:CH32V208 内置 BLE 协议栈
|
||||
- 双协议支持:同时处理以太网和 485,消息路由与优先级管理
|
||||
- OTA 升级:BLE 通道支持 Loop MCU 固件升级
|
||||
306
vd960DBN/SRC/Core/core_riscv.c
Normal file
306
vd960DBN/SRC/Core/core_riscv.c
Normal file
@@ -0,0 +1,306 @@
|
||||
/********************************** (C) COPYRIGHT *******************************
|
||||
* File Name : core_riscv.c
|
||||
* Author : WCH
|
||||
* Version : V1.0.1
|
||||
* Date : 2023/11/11
|
||||
* Description : RISC-V V4 Core Peripheral Access Layer Source File for CH32V20x
|
||||
*********************************************************************************
|
||||
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
|
||||
* Attention: This software (modified or not) and binary are used for
|
||||
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
|
||||
*******************************************************************************/
|
||||
#include <stdint.h>
|
||||
|
||||
/* define compiler specific symbols */
|
||||
#if defined ( __CC_ARM )
|
||||
#define __ASM __asm /* asm keyword for ARM Compiler */
|
||||
#define __INLINE __inline /* inline keyword for ARM Compiler */
|
||||
|
||||
#elif defined ( __ICCARM__ )
|
||||
#define __ASM __asm /* asm keyword for IAR Compiler */
|
||||
#define __INLINE inline /* inline keyword for IAR Compiler. Only avaiable in High optimization mode */
|
||||
|
||||
#elif defined ( __GNUC__ )
|
||||
#define __ASM __asm /* asm keyword for GNU Compiler */
|
||||
#define __INLINE inline /* inline keyword for GNU Compiler */
|
||||
|
||||
#elif defined ( __TASKING__ )
|
||||
#define __ASM __asm /* asm keyword for TASKING Compiler */
|
||||
#define __INLINE inline /* inline keyword for TASKING Compiler */
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
/*********************************************************************
|
||||
* @fn __get_MSTATUS
|
||||
*
|
||||
* @brief Return the Machine Status Register
|
||||
*
|
||||
* @return mstatus value
|
||||
*/
|
||||
uint32_t __get_MSTATUS(void)
|
||||
{
|
||||
uint32_t result;
|
||||
|
||||
__ASM volatile ( "csrr %0," "mstatus" : "=r" (result) );
|
||||
return (result);
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn __set_MSTATUS
|
||||
*
|
||||
* @brief Set the Machine Status Register
|
||||
*
|
||||
* @param value - set mstatus value
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
void __set_MSTATUS(uint32_t value)
|
||||
{
|
||||
__ASM volatile ("csrw mstatus, %0" : : "r" (value) );
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn __get_MISA
|
||||
*
|
||||
* @brief Return the Machine ISA Register
|
||||
*
|
||||
* @return misa value
|
||||
*/
|
||||
uint32_t __get_MISA(void)
|
||||
{
|
||||
uint32_t result;
|
||||
|
||||
__ASM volatile ( "csrr %0," "misa" : "=r" (result) );
|
||||
return (result);
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn __set_MISA
|
||||
*
|
||||
* @brief Set the Machine ISA Register
|
||||
*
|
||||
* @param value - set misa value
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
void __set_MISA(uint32_t value)
|
||||
{
|
||||
__ASM volatile ("csrw misa, %0" : : "r" (value) );
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn __get_MTVEC
|
||||
*
|
||||
* @brief Return the Machine Trap-Vector Base-Address Register
|
||||
*
|
||||
* @return mtvec value
|
||||
*/
|
||||
uint32_t __get_MTVEC(void)
|
||||
{
|
||||
uint32_t result;
|
||||
|
||||
__ASM volatile ( "csrr %0," "mtvec" : "=r" (result) );
|
||||
return (result);
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn __set_MTVEC
|
||||
*
|
||||
* @brief Set the Machine Trap-Vector Base-Address Register
|
||||
*
|
||||
* @param value - set mtvec value
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
void __set_MTVEC(uint32_t value)
|
||||
{
|
||||
__ASM volatile ("csrw mtvec, %0" : : "r" (value) );
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn __get_MSCRATCH
|
||||
*
|
||||
* @brief Return the Machine Seratch Register
|
||||
*
|
||||
* @return mscratch value
|
||||
*/
|
||||
uint32_t __get_MSCRATCH(void)
|
||||
{
|
||||
uint32_t result;
|
||||
|
||||
__ASM volatile ( "csrr %0," "mscratch" : "=r" (result) );
|
||||
return (result);
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn __set_MSCRATCH
|
||||
*
|
||||
* @brief Set the Machine Seratch Register
|
||||
*
|
||||
* @param value - set mscratch value
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
void __set_MSCRATCH(uint32_t value)
|
||||
{
|
||||
__ASM volatile ("csrw mscratch, %0" : : "r" (value) );
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn __get_MEPC
|
||||
*
|
||||
* @brief Return the Machine Exception Program Register
|
||||
*
|
||||
* @return mepc value
|
||||
*/
|
||||
uint32_t __get_MEPC(void)
|
||||
{
|
||||
uint32_t result;
|
||||
|
||||
__ASM volatile ( "csrr %0," "mepc" : "=r" (result) );
|
||||
return (result);
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn __set_MEPC
|
||||
*
|
||||
* @brief Set the Machine Exception Program Register
|
||||
*
|
||||
* @return mepc value
|
||||
*/
|
||||
void __set_MEPC(uint32_t value)
|
||||
{
|
||||
__ASM volatile ("csrw mepc, %0" : : "r" (value) );
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn __get_MCAUSE
|
||||
*
|
||||
* @brief Return the Machine Cause Register
|
||||
*
|
||||
* @return mcause value
|
||||
*/
|
||||
uint32_t __get_MCAUSE(void)
|
||||
{
|
||||
uint32_t result;
|
||||
|
||||
__ASM volatile ( "csrr %0," "mcause" : "=r" (result) );
|
||||
return (result);
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn __set_MEPC
|
||||
*
|
||||
* @brief Set the Machine Cause Register
|
||||
*
|
||||
* @return mcause value
|
||||
*/
|
||||
void __set_MCAUSE(uint32_t value)
|
||||
{
|
||||
__ASM volatile ("csrw mcause, %0" : : "r" (value) );
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn __get_MTVAL
|
||||
*
|
||||
* @brief Return the Machine Trap Value Register
|
||||
*
|
||||
* @return mtval value
|
||||
*/
|
||||
uint32_t __get_MTVAL(void)
|
||||
{
|
||||
uint32_t result;
|
||||
|
||||
__ASM volatile ( "csrr %0," "mtval" : "=r" (result) );
|
||||
return (result);
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn __set_MTVAL
|
||||
*
|
||||
* @brief Set the Machine Trap Value Register
|
||||
*
|
||||
* @return mtval value
|
||||
*/
|
||||
void __set_MTVAL(uint32_t value)
|
||||
{
|
||||
__ASM volatile ("csrw mtval, %0" : : "r" (value) );
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn __get_MVENDORID
|
||||
*
|
||||
* @brief Return Vendor ID Register
|
||||
*
|
||||
* @return mvendorid value
|
||||
*/
|
||||
uint32_t __get_MVENDORID(void)
|
||||
{
|
||||
uint32_t result;
|
||||
|
||||
__ASM volatile ( "csrr %0," "mvendorid" : "=r" (result) );
|
||||
return (result);
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn __get_MARCHID
|
||||
*
|
||||
* @brief Return Machine Architecture ID Register
|
||||
*
|
||||
* @return marchid value
|
||||
*/
|
||||
uint32_t __get_MARCHID(void)
|
||||
{
|
||||
uint32_t result;
|
||||
|
||||
__ASM volatile ( "csrr %0," "marchid" : "=r" (result) );
|
||||
return (result);
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn __get_MIMPID
|
||||
*
|
||||
* @brief Return Machine Implementation ID Register
|
||||
*
|
||||
* @return mimpid value
|
||||
*/
|
||||
uint32_t __get_MIMPID(void)
|
||||
{
|
||||
uint32_t result;
|
||||
|
||||
__ASM volatile ( "csrr %0," "mimpid" : "=r" (result) );
|
||||
return (result);
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn __get_MHARTID
|
||||
*
|
||||
* @brief Return Hart ID Register
|
||||
*
|
||||
* @return mhartid value
|
||||
*/
|
||||
uint32_t __get_MHARTID(void)
|
||||
{
|
||||
uint32_t result;
|
||||
|
||||
__ASM volatile ( "csrr %0," "mhartid" : "=r" (result) );
|
||||
return (result);
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn __get_SP
|
||||
*
|
||||
* @brief Return SP Register
|
||||
*
|
||||
* @return SP value
|
||||
*/
|
||||
uint32_t __get_SP(void)
|
||||
{
|
||||
uint32_t result;
|
||||
|
||||
__ASM volatile ( "mv %0," "sp" : "=r"(result) : );
|
||||
return (result);
|
||||
}
|
||||
|
||||
589
vd960DBN/SRC/Core/core_riscv.h
Normal file
589
vd960DBN/SRC/Core/core_riscv.h
Normal file
@@ -0,0 +1,589 @@
|
||||
/********************************** (C) COPYRIGHT *******************************
|
||||
* File Name : core_riscv.h
|
||||
* Author : WCH
|
||||
* Version : V1.0.1
|
||||
* Date : 2023/11/11
|
||||
* Description : RISC-V V4 Core Peripheral Access Layer Header File for CH32V20x
|
||||
*********************************************************************************
|
||||
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
|
||||
* Attention: This software (modified or not) and binary are used for
|
||||
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
|
||||
*******************************************************************************/
|
||||
#ifndef __CORE_RISCV_H__
|
||||
#define __CORE_RISCV_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* IO definitions */
|
||||
#ifdef __cplusplus
|
||||
#define __I volatile /* defines 'read only' permissions */
|
||||
#else
|
||||
#define __I volatile const /* defines 'read only' permissions */
|
||||
#endif
|
||||
#define __O volatile /* defines 'write only' permissions */
|
||||
#define __IO volatile /* defines 'read / write' permissions */
|
||||
|
||||
/* Standard Peripheral Library old types (maintained for legacy purpose) */
|
||||
typedef __I uint64_t vuc64; /* Read Only */
|
||||
typedef __I uint32_t vuc32; /* Read Only */
|
||||
typedef __I uint16_t vuc16; /* Read Only */
|
||||
typedef __I uint8_t vuc8; /* Read Only */
|
||||
|
||||
typedef const uint64_t uc64; /* Read Only */
|
||||
typedef const uint32_t uc32; /* Read Only */
|
||||
typedef const uint16_t uc16; /* Read Only */
|
||||
typedef const uint8_t uc8; /* Read Only */
|
||||
|
||||
typedef __I int64_t vsc64; /* Read Only */
|
||||
typedef __I int32_t vsc32; /* Read Only */
|
||||
typedef __I int16_t vsc16; /* Read Only */
|
||||
typedef __I int8_t vsc8; /* Read Only */
|
||||
|
||||
typedef const int64_t sc64; /* Read Only */
|
||||
typedef const int32_t sc32; /* Read Only */
|
||||
typedef const int16_t sc16; /* Read Only */
|
||||
typedef const int8_t sc8; /* Read Only */
|
||||
|
||||
typedef __IO uint64_t vu64;
|
||||
typedef __IO uint32_t vu32;
|
||||
typedef __IO uint16_t vu16;
|
||||
typedef __IO uint8_t vu8;
|
||||
|
||||
typedef uint64_t u64;
|
||||
typedef uint32_t u32;
|
||||
typedef uint16_t u16;
|
||||
typedef uint8_t u8;
|
||||
|
||||
typedef __IO int64_t vs64;
|
||||
typedef __IO int32_t vs32;
|
||||
typedef __IO int16_t vs16;
|
||||
typedef __IO int8_t vs8;
|
||||
|
||||
typedef int64_t s64;
|
||||
typedef int32_t s32;
|
||||
typedef int16_t s16;
|
||||
typedef int8_t s8;
|
||||
|
||||
typedef enum {NoREADY = 0, READY = !NoREADY} ErrorStatus;
|
||||
|
||||
typedef enum {DISABLE = 0, ENABLE = !DISABLE} FunctionalState;
|
||||
|
||||
typedef enum {RESET = 0, SET = !RESET} FlagStatus, ITStatus;
|
||||
|
||||
#define RV_STATIC_INLINE static inline
|
||||
|
||||
/* memory mapped structure for Program Fast Interrupt Controller (PFIC) */
|
||||
typedef struct{
|
||||
__I uint32_t ISR[8];
|
||||
__I uint32_t IPR[8];
|
||||
__IO uint32_t ITHRESDR;
|
||||
__IO uint32_t RESERVED;
|
||||
__IO uint32_t CFGR;
|
||||
__I uint32_t GISR;
|
||||
__IO uint8_t VTFIDR[4];
|
||||
uint8_t RESERVED0[12];
|
||||
__IO uint32_t VTFADDR[4];
|
||||
uint8_t RESERVED1[0x90];
|
||||
__O uint32_t IENR[8];
|
||||
uint8_t RESERVED2[0x60];
|
||||
__O uint32_t IRER[8];
|
||||
uint8_t RESERVED3[0x60];
|
||||
__O uint32_t IPSR[8];
|
||||
uint8_t RESERVED4[0x60];
|
||||
__O uint32_t IPRR[8];
|
||||
uint8_t RESERVED5[0x60];
|
||||
__IO uint32_t IACTR[8];
|
||||
uint8_t RESERVED6[0xE0];
|
||||
__IO uint8_t IPRIOR[256];
|
||||
uint8_t RESERVED7[0x810];
|
||||
__IO uint32_t SCTLR;
|
||||
}PFIC_Type;
|
||||
|
||||
/* memory mapped structure for SysTick */
|
||||
typedef struct
|
||||
{
|
||||
__IO uint32_t CTLR;
|
||||
__IO uint32_t SR;
|
||||
__IO uint64_t CNT;
|
||||
__IO uint64_t CMP;
|
||||
}SysTick_Type;
|
||||
|
||||
|
||||
#define PFIC ((PFIC_Type *) 0xE000E000 )
|
||||
#define NVIC PFIC
|
||||
#define NVIC_KEY1 ((uint32_t)0xFA050000)
|
||||
#define NVIC_KEY2 ((uint32_t)0xBCAF0000)
|
||||
#define NVIC_KEY3 ((uint32_t)0xBEEF0000)
|
||||
|
||||
#define SysTick ((SysTick_Type *) 0xE000F000)
|
||||
|
||||
|
||||
/*********************************************************************
|
||||
* @fn __enable_irq
|
||||
*
|
||||
* @brief Enable Global Interrupt
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
__attribute__( ( always_inline ) ) RV_STATIC_INLINE void __enable_irq()
|
||||
{
|
||||
__asm volatile ("csrs 0x800, %0" : : "r" (0x88) );
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn __disable_irq
|
||||
*
|
||||
* @brief Disable Global Interrupt
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
__attribute__( ( always_inline ) ) RV_STATIC_INLINE void __disable_irq()
|
||||
{
|
||||
__asm volatile ("csrc 0x800, %0" : : "r" (0x88) );
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn __NOP
|
||||
*
|
||||
* @brief nop
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
__attribute__( ( always_inline ) ) RV_STATIC_INLINE void __NOP()
|
||||
{
|
||||
__asm volatile ("nop");
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn NVIC_EnableIRQ
|
||||
*
|
||||
* @brief Enable Interrupt
|
||||
*
|
||||
* @param IRQn - Interrupt Numbers
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
__attribute__( ( always_inline ) ) RV_STATIC_INLINE void NVIC_EnableIRQ(IRQn_Type IRQn)
|
||||
{
|
||||
NVIC->IENR[((uint32_t)(IRQn) >> 5)] = (1 << ((uint32_t)(IRQn) & 0x1F));
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn NVIC_DisableIRQ
|
||||
*
|
||||
* @brief Disable Interrupt
|
||||
*
|
||||
* @param IRQn - Interrupt Numbers
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
__attribute__( ( always_inline ) ) RV_STATIC_INLINE void NVIC_DisableIRQ(IRQn_Type IRQn)
|
||||
{
|
||||
NVIC->IRER[((uint32_t)(IRQn) >> 5)] = (1 << ((uint32_t)(IRQn) & 0x1F));
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn NVIC_GetStatusIRQ
|
||||
*
|
||||
* @brief Get Interrupt Enable State
|
||||
*
|
||||
* @param IRQn - Interrupt Numbers
|
||||
*
|
||||
* @return 1 - Interrupt Pending Enable
|
||||
* 0 - Interrupt Pending Disable
|
||||
*/
|
||||
__attribute__( ( always_inline ) ) RV_STATIC_INLINE uint32_t NVIC_GetStatusIRQ(IRQn_Type IRQn)
|
||||
{
|
||||
return((uint32_t) ((NVIC->ISR[(uint32_t)(IRQn) >> 5] & (1 << ((uint32_t)(IRQn) & 0x1F)))?1:0));
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn NVIC_GetPendingIRQ
|
||||
*
|
||||
* @brief Get Interrupt Pending State
|
||||
*
|
||||
* @param IRQn - Interrupt Numbers
|
||||
*
|
||||
* @return 1 - Interrupt Pending Enable
|
||||
* 0 - Interrupt Pending Disable
|
||||
*/
|
||||
__attribute__( ( always_inline ) ) RV_STATIC_INLINE uint32_t NVIC_GetPendingIRQ(IRQn_Type IRQn)
|
||||
{
|
||||
return((uint32_t) ((NVIC->IPR[(uint32_t)(IRQn) >> 5] & (1 << ((uint32_t)(IRQn) & 0x1F)))?1:0));
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn NVIC_SetPendingIRQ
|
||||
*
|
||||
* @brief Set Interrupt Pending
|
||||
*
|
||||
* @param IRQn - Interrupt Numbers
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
__attribute__( ( always_inline ) ) RV_STATIC_INLINE void NVIC_SetPendingIRQ(IRQn_Type IRQn)
|
||||
{
|
||||
NVIC->IPSR[((uint32_t)(IRQn) >> 5)] = (1 << ((uint32_t)(IRQn) & 0x1F));
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn NVIC_ClearPendingIRQ
|
||||
*
|
||||
* @brief Clear Interrupt Pending
|
||||
*
|
||||
* @param IRQn - Interrupt Numbers
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
__attribute__( ( always_inline ) ) RV_STATIC_INLINE void NVIC_ClearPendingIRQ(IRQn_Type IRQn)
|
||||
{
|
||||
NVIC->IPRR[((uint32_t)(IRQn) >> 5)] = (1 << ((uint32_t)(IRQn) & 0x1F));
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn NVIC_GetActive
|
||||
*
|
||||
* @brief Get Interrupt Active State
|
||||
*
|
||||
* @param IRQn - Interrupt Numbers
|
||||
*
|
||||
* @return 1 - Interrupt Active
|
||||
* 0 - Interrupt No Active
|
||||
*/
|
||||
__attribute__( ( always_inline ) ) RV_STATIC_INLINE uint32_t NVIC_GetActive(IRQn_Type IRQn)
|
||||
{
|
||||
return((uint32_t)((NVIC->IACTR[(uint32_t)(IRQn) >> 5] & (1 << ((uint32_t)(IRQn) & 0x1F)))?1:0));
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn NVIC_SetPriority
|
||||
*
|
||||
* @brief Set Interrupt Priority
|
||||
*
|
||||
* @param IRQn - Interrupt Numbers
|
||||
* interrupt nesting enable(CSR-0x804 bit1 = 1)
|
||||
* priority - bit[7] - Preemption Priority
|
||||
* bit[6:5] - Sub priority
|
||||
* bit[4:0] - Reserve
|
||||
* interrupt nesting disable(CSR-0x804 bit1 = 0)
|
||||
* priority - bit[7:5] - Sub priority
|
||||
* bit[4:0] - Reserve
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
__attribute__( ( always_inline ) ) RV_STATIC_INLINE void NVIC_SetPriority(IRQn_Type IRQn, uint8_t priority)
|
||||
{
|
||||
NVIC->IPRIOR[(uint32_t)(IRQn)] = priority;
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn __WFI
|
||||
*
|
||||
* @brief Wait for Interrupt
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
__attribute__( ( always_inline ) ) RV_STATIC_INLINE void __WFI(void)
|
||||
{
|
||||
NVIC->SCTLR &= ~(1<<3); // wfi
|
||||
asm volatile ("wfi");
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn _SEV
|
||||
*
|
||||
* @brief Set Event
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
__attribute__( ( always_inline ) ) RV_STATIC_INLINE void _SEV(void)
|
||||
{
|
||||
NVIC->SCTLR |= (1<<5);
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn _WFE
|
||||
*
|
||||
* @brief Wait for Events
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
__attribute__( ( always_inline ) ) RV_STATIC_INLINE void _WFE(void)
|
||||
{
|
||||
uint32_t tmp= NVIC->SCTLR;
|
||||
tmp &= ~(1<<5);
|
||||
tmp |= (1<<3);
|
||||
NVIC->SCTLR = tmp;
|
||||
asm volatile ("wfi");
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn __WFE
|
||||
*
|
||||
* @brief Wait for Events
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
__attribute__( ( always_inline ) ) RV_STATIC_INLINE void __WFE(void)
|
||||
{
|
||||
_SEV();
|
||||
_WFE();
|
||||
_WFE();
|
||||
if(*(vu32*)(0x40023800) & (1<<6))
|
||||
{
|
||||
NVIC->SCTLR |= (1<<5);
|
||||
}
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn SetVTFIRQ
|
||||
*
|
||||
* @brief Set VTF Interrupt
|
||||
*
|
||||
* @param addr - VTF interrupt service function base address.
|
||||
* IRQn - Interrupt Numbers
|
||||
* num - VTF Interrupt Numbers
|
||||
* NewState - DISABLE or ENABLE
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
__attribute__( ( always_inline ) ) RV_STATIC_INLINE void SetVTFIRQ(uint32_t addr, IRQn_Type IRQn, uint8_t num, FunctionalState NewState)
|
||||
{
|
||||
if(num > 3) return ;
|
||||
|
||||
if (NewState != DISABLE)
|
||||
{
|
||||
NVIC->VTFIDR[num] = IRQn;
|
||||
NVIC->VTFADDR[num] = ((addr&0xFFFFFFFE)|0x1);
|
||||
}
|
||||
else
|
||||
{
|
||||
NVIC->VTFIDR[num] = IRQn;
|
||||
NVIC->VTFADDR[num] = ((addr&0xFFFFFFFE)&(~0x1));
|
||||
}
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn NVIC_SystemReset
|
||||
*
|
||||
* @brief Initiate a system reset request
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
__attribute__( ( always_inline ) ) RV_STATIC_INLINE void NVIC_SystemReset(void)
|
||||
{
|
||||
NVIC->CFGR = NVIC_KEY3|(1<<7);
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn __AMOADD_W
|
||||
*
|
||||
* @brief Atomic Add with 32bit value
|
||||
* Atomically ADD 32bit value with value in memory using amoadd.d.
|
||||
*
|
||||
* @param addr - Address pointer to data, address need to be 4byte aligned
|
||||
* value - value to be ADDed
|
||||
*
|
||||
* @return return memory value + add value
|
||||
*/
|
||||
__attribute__( ( always_inline ) ) RV_STATIC_INLINE int32_t __AMOADD_W(volatile int32_t *addr, int32_t value)
|
||||
{
|
||||
int32_t result;
|
||||
|
||||
__asm volatile ("amoadd.w %0, %2, %1" : \
|
||||
"=r"(result), "+A"(*addr) : "r"(value) : "memory");
|
||||
return *addr;
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn __AMOAND_W
|
||||
*
|
||||
* @brief Atomic And with 32bit value
|
||||
* Atomically AND 32bit value with value in memory using amoand.d.
|
||||
*
|
||||
* @param addr - Address pointer to data, address need to be 4byte aligned
|
||||
* value - value to be ANDed
|
||||
*
|
||||
* @return return memory value & and value
|
||||
*/
|
||||
__attribute__( ( always_inline ) ) RV_STATIC_INLINE int32_t __AMOAND_W(volatile int32_t *addr, int32_t value)
|
||||
{
|
||||
int32_t result;
|
||||
|
||||
__asm volatile ("amoand.w %0, %2, %1" : \
|
||||
"=r"(result), "+A"(*addr) : "r"(value) : "memory");
|
||||
return *addr;
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn __AMOMAX_W
|
||||
*
|
||||
* @brief Atomic signed MAX with 32bit value
|
||||
* Atomically signed max compare 32bit value with value in memory using amomax.d.
|
||||
*
|
||||
* @param addr - Address pointer to data, address need to be 4byte aligned
|
||||
* value - value to be compared
|
||||
*
|
||||
* @return the bigger value
|
||||
*/
|
||||
__attribute__( ( always_inline ) ) RV_STATIC_INLINE int32_t __AMOMAX_W(volatile int32_t *addr, int32_t value)
|
||||
{
|
||||
int32_t result;
|
||||
|
||||
__asm volatile ("amomax.w %0, %2, %1" : \
|
||||
"=r"(result), "+A"(*addr) : "r"(value) : "memory");
|
||||
return *addr;
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn __AMOMAXU_W
|
||||
*
|
||||
* @brief Atomic unsigned MAX with 32bit value
|
||||
* Atomically unsigned max compare 32bit value with value in memory using amomaxu.d.
|
||||
*
|
||||
* @param addr - Address pointer to data, address need to be 4byte aligned
|
||||
* value - value to be compared
|
||||
*
|
||||
* @return return the bigger value
|
||||
*/
|
||||
__attribute__( ( always_inline ) ) RV_STATIC_INLINE uint32_t __AMOMAXU_W(volatile uint32_t *addr, uint32_t value)
|
||||
{
|
||||
uint32_t result;
|
||||
|
||||
__asm volatile ("amomaxu.w %0, %2, %1" : \
|
||||
"=r"(result), "+A"(*addr) : "r"(value) : "memory");
|
||||
return *addr;
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn __AMOMIN_W
|
||||
*
|
||||
* @brief Atomic signed MIN with 32bit value
|
||||
* Atomically signed min compare 32bit value with value in memory using amomin.d.
|
||||
*
|
||||
* @param addr - Address pointer to data, address need to be 4byte aligned
|
||||
* value - value to be compared
|
||||
*
|
||||
* @return the smaller value
|
||||
*/
|
||||
__attribute__( ( always_inline ) ) RV_STATIC_INLINE int32_t __AMOMIN_W(volatile int32_t *addr, int32_t value)
|
||||
{
|
||||
int32_t result;
|
||||
|
||||
__asm volatile ("amomin.w %0, %2, %1" : \
|
||||
"=r"(result), "+A"(*addr) : "r"(value) : "memory");
|
||||
return *addr;
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn __AMOMINU_W
|
||||
*
|
||||
* @brief Atomic unsigned MIN with 32bit value
|
||||
* Atomically unsigned min compare 32bit value with value in memory using amominu.d.
|
||||
*
|
||||
* @param addr - Address pointer to data, address need to be 4byte aligned
|
||||
* value - value to be compared
|
||||
*
|
||||
* @return the smaller value
|
||||
*/
|
||||
__attribute__( ( always_inline ) ) RV_STATIC_INLINE uint32_t __AMOMINU_W(volatile uint32_t *addr, uint32_t value)
|
||||
{
|
||||
uint32_t result;
|
||||
|
||||
__asm volatile ("amominu.w %0, %2, %1" : \
|
||||
"=r"(result), "+A"(*addr) : "r"(value) : "memory");
|
||||
return *addr;
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn __AMOOR_W
|
||||
*
|
||||
* @brief Atomic OR with 32bit value
|
||||
* Atomically OR 32bit value with value in memory using amoor.d.
|
||||
*
|
||||
* @param addr - Address pointer to data, address need to be 4byte aligned
|
||||
* value - value to be ORed
|
||||
*
|
||||
* @return return memory value | and value
|
||||
*/
|
||||
__attribute__( ( always_inline ) ) RV_STATIC_INLINE int32_t __AMOOR_W(volatile int32_t *addr, int32_t value)
|
||||
{
|
||||
int32_t result;
|
||||
|
||||
__asm volatile ("amoor.w %0, %2, %1" : \
|
||||
"=r"(result), "+A"(*addr) : "r"(value) : "memory");
|
||||
return *addr;
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn __AMOSWAP_W
|
||||
*
|
||||
* @brief Atomically swap new 32bit value into memory using amoswap.d.
|
||||
*
|
||||
* @param addr - Address pointer to data, address need to be 4byte aligned
|
||||
* newval - New value to be stored into the address
|
||||
*
|
||||
* @return return the original value in memory
|
||||
*/
|
||||
__attribute__( ( always_inline ) ) RV_STATIC_INLINE uint32_t __AMOSWAP_W(volatile uint32_t *addr, uint32_t newval)
|
||||
{
|
||||
uint32_t result;
|
||||
|
||||
__asm volatile ("amoswap.w %0, %2, %1" : \
|
||||
"=r"(result), "+A"(*addr) : "r"(newval) : "memory");
|
||||
return result;
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn __AMOXOR_W
|
||||
*
|
||||
* @brief Atomic XOR with 32bit value
|
||||
* Atomically XOR 32bit value with value in memory using amoxor.d.
|
||||
*
|
||||
* @param addr - Address pointer to data, address need to be 4byte aligned
|
||||
* value - value to be XORed
|
||||
*
|
||||
* @return return memory value ^ and value
|
||||
*/
|
||||
__attribute__( ( always_inline ) ) RV_STATIC_INLINE int32_t __AMOXOR_W(volatile int32_t *addr, int32_t value)
|
||||
{
|
||||
int32_t result;
|
||||
|
||||
__asm volatile ("amoxor.w %0, %2, %1" : \
|
||||
"=r"(result), "+A"(*addr) : "r"(value) : "memory");
|
||||
return *addr;
|
||||
}
|
||||
|
||||
/* Core_Exported_Functions */
|
||||
extern uint32_t __get_MSTATUS(void);
|
||||
extern void __set_MSTATUS(uint32_t value);
|
||||
extern uint32_t __get_MISA(void);
|
||||
extern void __set_MISA(uint32_t value);
|
||||
extern uint32_t __get_MTVEC(void);
|
||||
extern void __set_MTVEC(uint32_t value);
|
||||
extern uint32_t __get_MSCRATCH(void);
|
||||
extern void __set_MSCRATCH(uint32_t value);
|
||||
extern uint32_t __get_MEPC(void);
|
||||
extern void __set_MEPC(uint32_t value);
|
||||
extern uint32_t __get_MCAUSE(void);
|
||||
extern void __set_MCAUSE(uint32_t value);
|
||||
extern uint32_t __get_MTVAL(void);
|
||||
extern void __set_MTVAL(uint32_t value);
|
||||
extern uint32_t __get_MVENDORID(void);
|
||||
extern uint32_t __get_MARCHID(void);
|
||||
extern uint32_t __get_MIMPID(void);
|
||||
extern uint32_t __get_MHARTID(void);
|
||||
extern uint32_t __get_SP(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
258
vd960DBN/SRC/Debug/debug.c
Normal file
258
vd960DBN/SRC/Debug/debug.c
Normal file
@@ -0,0 +1,258 @@
|
||||
/********************************** (C) COPYRIGHT *******************************
|
||||
* File Name : debug.c
|
||||
* Author : WCH
|
||||
* Version : V1.0.0
|
||||
* Date : 2021/06/06
|
||||
* Description : This file contains all the functions prototypes for UART
|
||||
* Printf , Delay functions.
|
||||
*********************************************************************************
|
||||
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
|
||||
* Attention: This software (modified or not) and binary are used for
|
||||
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
|
||||
*******************************************************************************/
|
||||
#include "debug.h"
|
||||
|
||||
static uint8_t p_us = 0;
|
||||
static uint16_t p_ms = 0;
|
||||
|
||||
#define DEBUG_DATA0_ADDRESS ((volatile uint32_t*)0xE0000380)
|
||||
#define DEBUG_DATA1_ADDRESS ((volatile uint32_t*)0xE0000384)
|
||||
|
||||
/*********************************************************************
|
||||
* @fn Delay_Init
|
||||
*
|
||||
* @brief Initializes Delay Funcation.
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
void Delay_Init(void)
|
||||
{
|
||||
p_us = SystemCoreClock / 8000000;
|
||||
p_ms = (uint16_t)p_us * 1000;
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn Delay_Us
|
||||
*
|
||||
* @brief Microsecond Delay Time.
|
||||
*
|
||||
* @param n - Microsecond number.
|
||||
*
|
||||
* @return None
|
||||
*/
|
||||
void Delay_Us(uint32_t n)
|
||||
{
|
||||
uint32_t i;
|
||||
|
||||
SysTick->SR &= ~(1 << 0);
|
||||
i = (uint32_t)n * p_us;
|
||||
|
||||
SysTick->CMP = i;
|
||||
SysTick->CTLR |= (1 << 4);
|
||||
SysTick->CTLR |= (1 << 5) | (1 << 0);
|
||||
|
||||
while((SysTick->SR & (1 << 0)) != (1 << 0));
|
||||
SysTick->CTLR &= ~(1 << 0);
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn Delay_Ms
|
||||
*
|
||||
* @brief Millisecond Delay Time.
|
||||
*
|
||||
* @param n - Millisecond number.
|
||||
*
|
||||
* @return None
|
||||
*/
|
||||
void Delay_Ms(uint32_t n)
|
||||
{
|
||||
uint32_t i;
|
||||
|
||||
SysTick->SR &= ~(1 << 0);
|
||||
i = (uint32_t)n * p_ms;
|
||||
|
||||
SysTick->CMP = i;
|
||||
SysTick->CTLR |= (1 << 4);
|
||||
SysTick->CTLR |= (1 << 5) | (1 << 0);
|
||||
|
||||
while((SysTick->SR & (1 << 0)) != (1 << 0));
|
||||
SysTick->CTLR &= ~(1 << 0);
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn USART_Printf_Init
|
||||
*
|
||||
* @brief Initializes the USARTx peripheral.
|
||||
*
|
||||
* @param baudrate - USART communication baud rate.
|
||||
*
|
||||
* @return None
|
||||
*/
|
||||
void USART_Printf_Init(uint32_t baudrate)
|
||||
{
|
||||
GPIO_InitTypeDef GPIO_InitStructure;
|
||||
USART_InitTypeDef USART_InitStructure;
|
||||
|
||||
#if(DEBUG == DEBUG_UART1)
|
||||
|
||||
// RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
|
||||
|
||||
// GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
|
||||
// GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
||||
// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
|
||||
// GPIO_Init(GPIOA, &GPIO_InitStructure);
|
||||
|
||||
|
||||
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
|
||||
GPIO_PinRemapConfig(GPIO_Remap_USART1, ENABLE);
|
||||
|
||||
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
|
||||
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
||||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
|
||||
GPIO_Init(GPIOB, &GPIO_InitStructure);
|
||||
|
||||
#elif(DEBUG == DEBUG_UART2)
|
||||
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
|
||||
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
|
||||
|
||||
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
|
||||
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
||||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
|
||||
GPIO_Init(GPIOA, &GPIO_InitStructure);
|
||||
|
||||
#elif(DEBUG == DEBUG_UART3)
|
||||
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
|
||||
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
|
||||
|
||||
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
|
||||
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
||||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
|
||||
GPIO_Init(GPIOB, &GPIO_InitStructure);
|
||||
|
||||
#endif
|
||||
|
||||
USART_InitStructure.USART_BaudRate = baudrate;
|
||||
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
|
||||
USART_InitStructure.USART_StopBits = USART_StopBits_1;
|
||||
USART_InitStructure.USART_Parity = USART_Parity_No;
|
||||
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
|
||||
USART_InitStructure.USART_Mode = USART_Mode_Tx;
|
||||
|
||||
#if(DEBUG == DEBUG_UART1)
|
||||
USART_Init(USART1, &USART_InitStructure);
|
||||
USART_Cmd(USART1, ENABLE);
|
||||
|
||||
#elif(DEBUG == DEBUG_UART2)
|
||||
USART_Init(USART2, &USART_InitStructure);
|
||||
USART_Cmd(USART2, ENABLE);
|
||||
|
||||
#elif(DEBUG == DEBUG_UART3)
|
||||
USART_Init(USART3, &USART_InitStructure);
|
||||
USART_Cmd(USART3, ENABLE);
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn SDI_Printf_Enable
|
||||
*
|
||||
* @brief Initializes the SDI printf Function.
|
||||
*
|
||||
* @param None
|
||||
*
|
||||
* @return None
|
||||
*/
|
||||
void SDI_Printf_Enable(void)
|
||||
{
|
||||
*(DEBUG_DATA0_ADDRESS) = 0;
|
||||
Delay_Init();
|
||||
Delay_Ms(1);
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn _write
|
||||
*
|
||||
* @brief Support Printf Function
|
||||
*
|
||||
* @param *buf - UART send Data.
|
||||
* size - Data length
|
||||
*
|
||||
* @return size: Data length
|
||||
*/
|
||||
__attribute__((used))
|
||||
int _write(int fd, char *buf, int size)
|
||||
{
|
||||
int i = 0;
|
||||
|
||||
#if (SDI_PRINT == SDI_PR_OPEN)
|
||||
int writeSize = size;
|
||||
|
||||
do
|
||||
{
|
||||
|
||||
/**
|
||||
* data0 data1 8 byte
|
||||
* data0 The storage length of the lowest byte, with a maximum of 7 bytes.
|
||||
*/
|
||||
|
||||
while( (*(DEBUG_DATA0_ADDRESS) != 0u))
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
if(writeSize>7)
|
||||
{
|
||||
*(DEBUG_DATA1_ADDRESS) = (*(buf+i+3)) | (*(buf+i+4)<<8) | (*(buf+i+5)<<16) | (*(buf+i+6)<<24);
|
||||
*(DEBUG_DATA0_ADDRESS) = (7u) | (*(buf+i)<<8) | (*(buf+i+1)<<16) | (*(buf+i+2)<<24);
|
||||
|
||||
i += 7;
|
||||
writeSize -= 7;
|
||||
}
|
||||
else
|
||||
{
|
||||
*(DEBUG_DATA1_ADDRESS) = (*(buf+i+3)) | (*(buf+i+4)<<8) | (*(buf+i+5)<<16) | (*(buf+i+6)<<24);
|
||||
*(DEBUG_DATA0_ADDRESS) = (writeSize) | (*(buf+i)<<8) | (*(buf+i+1)<<16) | (*(buf+i+2)<<24);
|
||||
|
||||
writeSize = 0;
|
||||
}
|
||||
|
||||
} while (writeSize);
|
||||
|
||||
|
||||
#else
|
||||
for(i = 0; i < size; i++){
|
||||
#if(DEBUG == DEBUG_UART1)
|
||||
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
|
||||
USART_SendData(USART1, *buf++);
|
||||
#elif(DEBUG == DEBUG_UART2)
|
||||
while(USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET);
|
||||
USART_SendData(USART2, *buf++);
|
||||
#elif(DEBUG == DEBUG_UART3)
|
||||
while(USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET);
|
||||
USART_SendData(USART3, *buf++);
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
return size;
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn _sbrk
|
||||
*
|
||||
* @brief Change the spatial position of data segment.
|
||||
*
|
||||
* @return size: Data length
|
||||
*/
|
||||
__attribute__((used))
|
||||
void *_sbrk(ptrdiff_t incr)
|
||||
{
|
||||
extern char _end[];
|
||||
extern char _heap_end[];
|
||||
static char *curbrk = _end;
|
||||
|
||||
if ((curbrk + incr < _end) || (curbrk + incr > _heap_end))
|
||||
return NULL - 1;
|
||||
|
||||
curbrk += incr;
|
||||
return curbrk - incr;
|
||||
}
|
||||
58
vd960DBN/SRC/Debug/debug.h
Normal file
58
vd960DBN/SRC/Debug/debug.h
Normal file
@@ -0,0 +1,58 @@
|
||||
/********************************** (C) COPYRIGHT *******************************
|
||||
* File Name : debug.h
|
||||
* Author : WCH
|
||||
* Version : V1.0.0
|
||||
* Date : 2023/10/24
|
||||
* Description : This file contains all the functions prototypes for UART
|
||||
* Printf , Delay functions.
|
||||
*********************************************************************************
|
||||
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
|
||||
* Attention: This software (modified or not) and binary are used for
|
||||
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
|
||||
*******************************************************************************/
|
||||
#ifndef __DEBUG_H
|
||||
#define __DEBUG_H
|
||||
|
||||
#include "stdio.h"
|
||||
#include "ch32v20x.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* UART Printf Definition */
|
||||
#define DEBUG_UART1 1
|
||||
#define DEBUG_UART2 2
|
||||
#define DEBUG_UART3 3
|
||||
|
||||
/* DEBUG UATR Definition */
|
||||
#ifndef DEBUG
|
||||
#define DEBUG DEBUG_UART1
|
||||
#endif
|
||||
|
||||
/* SDI Printf Definition */
|
||||
#define SDI_PR_CLOSE 0
|
||||
#define SDI_PR_OPEN 1
|
||||
|
||||
#ifndef SDI_PRINT
|
||||
#define SDI_PRINT SDI_PR_CLOSE
|
||||
#endif
|
||||
|
||||
|
||||
void Delay_Init(void);
|
||||
void Delay_Us(uint32_t n);
|
||||
void Delay_Ms(uint32_t n);
|
||||
void USART_Printf_Init(uint32_t baudrate);
|
||||
void SDI_Printf_Enable(void);
|
||||
|
||||
#if(DEBUG)
|
||||
#define PRINT(format, ...) printf(format, ##__VA_ARGS__)
|
||||
#else
|
||||
#define PRINT(X...)
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
187
vd960DBN/SRC/Ld/Link.ld
Normal file
187
vd960DBN/SRC/Ld/Link.ld
Normal file
@@ -0,0 +1,187 @@
|
||||
ENTRY( _start )
|
||||
|
||||
__stack_size = 2048;
|
||||
|
||||
PROVIDE( _stack_size = __stack_size );
|
||||
|
||||
|
||||
MEMORY
|
||||
{
|
||||
/* CH32V20x_D6 - CH32V203F6-CH32V203G6-CH32V203C6 */
|
||||
/*
|
||||
FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 32K
|
||||
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 10K
|
||||
*/
|
||||
|
||||
/* CH32V20x_D6 - CH32V203K8-CH32V203C8-CH32V203G8-CH32V203F8 */
|
||||
/**/
|
||||
FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 64K
|
||||
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 20K
|
||||
|
||||
|
||||
/* CH32V20x_D8 - CH32V203RB
|
||||
CH32V20x_D8W - CH32V208x
|
||||
FLASH + RAM supports the following configuration
|
||||
FLASH-128K + RAM-64K
|
||||
FLASH-144K + RAM-48K
|
||||
FLASH-160K + RAM-32K
|
||||
|
||||
FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 160K
|
||||
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 32K
|
||||
*/
|
||||
}
|
||||
|
||||
|
||||
SECTIONS
|
||||
{
|
||||
|
||||
.init :
|
||||
{
|
||||
_sinit = .;
|
||||
. = ALIGN(4);
|
||||
KEEP(*(SORT_NONE(.init)))
|
||||
. = ALIGN(4);
|
||||
_einit = .;
|
||||
} >FLASH AT>FLASH
|
||||
|
||||
.vector :
|
||||
{
|
||||
*(.vector);
|
||||
. = ALIGN(64);
|
||||
} >FLASH AT>FLASH
|
||||
|
||||
.text :
|
||||
{
|
||||
. = ALIGN(4);
|
||||
*(.text)
|
||||
*(.text.*)
|
||||
*(.rodata)
|
||||
*(.rodata*)
|
||||
*(.gnu.linkonce.t.*)
|
||||
. = ALIGN(4);
|
||||
} >FLASH AT>FLASH
|
||||
|
||||
.fini :
|
||||
{
|
||||
KEEP(*(SORT_NONE(.fini)))
|
||||
. = ALIGN(4);
|
||||
} >FLASH AT>FLASH
|
||||
|
||||
PROVIDE( _etext = . );
|
||||
PROVIDE( _eitcm = . );
|
||||
|
||||
.preinit_array :
|
||||
{
|
||||
PROVIDE_HIDDEN (__preinit_array_start = .);
|
||||
KEEP (*(.preinit_array))
|
||||
PROVIDE_HIDDEN (__preinit_array_end = .);
|
||||
} >FLASH AT>FLASH
|
||||
|
||||
.init_array :
|
||||
{
|
||||
PROVIDE_HIDDEN (__init_array_start = .);
|
||||
KEEP (*(SORT_BY_INIT_PRIORITY(.init_array.*) SORT_BY_INIT_PRIORITY(.ctors.*)))
|
||||
KEEP (*(.init_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .ctors))
|
||||
PROVIDE_HIDDEN (__init_array_end = .);
|
||||
} >FLASH AT>FLASH
|
||||
|
||||
.fini_array :
|
||||
{
|
||||
PROVIDE_HIDDEN (__fini_array_start = .);
|
||||
KEEP (*(SORT_BY_INIT_PRIORITY(.fini_array.*) SORT_BY_INIT_PRIORITY(.dtors.*)))
|
||||
KEEP (*(.fini_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .dtors))
|
||||
PROVIDE_HIDDEN (__fini_array_end = .);
|
||||
} >FLASH AT>FLASH
|
||||
|
||||
.ctors :
|
||||
{
|
||||
/* gcc uses crtbegin.o to find the start of
|
||||
the constructors, so we make sure it is
|
||||
first. Because this is a wildcard, it
|
||||
doesn't matter if the user does not
|
||||
actually link against crtbegin.o; the
|
||||
linker won't look for a file to match a
|
||||
wildcard. The wildcard also means that it
|
||||
doesn't matter which directory crtbegin.o
|
||||
is in. */
|
||||
KEEP (*crtbegin.o(.ctors))
|
||||
KEEP (*crtbegin?.o(.ctors))
|
||||
/* We don't want to include the .ctor section from
|
||||
the crtend.o file until after the sorted ctors.
|
||||
The .ctor section from the crtend file contains the
|
||||
end of ctors marker and it must be last */
|
||||
KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .ctors))
|
||||
KEEP (*(SORT(.ctors.*)))
|
||||
KEEP (*(.ctors))
|
||||
} >FLASH AT>FLASH
|
||||
|
||||
.dtors :
|
||||
{
|
||||
KEEP (*crtbegin.o(.dtors))
|
||||
KEEP (*crtbegin?.o(.dtors))
|
||||
KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .dtors))
|
||||
KEEP (*(SORT(.dtors.*)))
|
||||
KEEP (*(.dtors))
|
||||
} >FLASH AT>FLASH
|
||||
|
||||
.dalign :
|
||||
{
|
||||
. = ALIGN(4);
|
||||
PROVIDE(_data_vma = .);
|
||||
} >RAM AT>FLASH
|
||||
|
||||
.dlalign :
|
||||
{
|
||||
. = ALIGN(4);
|
||||
PROVIDE(_data_lma = .);
|
||||
} >FLASH AT>FLASH
|
||||
|
||||
.data :
|
||||
{
|
||||
*(.gnu.linkonce.r.*)
|
||||
*(.data .data.*)
|
||||
*(.gnu.linkonce.d.*)
|
||||
. = ALIGN(8);
|
||||
PROVIDE( __global_pointer$ = . + 0x800 );
|
||||
*(.sdata .sdata.*)
|
||||
*(.sdata2.*)
|
||||
*(.gnu.linkonce.s.*)
|
||||
. = ALIGN(8);
|
||||
*(.srodata.cst16)
|
||||
*(.srodata.cst8)
|
||||
*(.srodata.cst4)
|
||||
*(.srodata.cst2)
|
||||
*(.srodata .srodata.*)
|
||||
. = ALIGN(4);
|
||||
PROVIDE( _edata = .);
|
||||
} >RAM AT>FLASH
|
||||
|
||||
.bss :
|
||||
{
|
||||
. = ALIGN(4);
|
||||
PROVIDE( _sbss = .);
|
||||
*(.sbss*)
|
||||
*(.gnu.linkonce.sb.*)
|
||||
*(.bss*)
|
||||
*(.gnu.linkonce.b.*)
|
||||
*(COMMON*)
|
||||
. = ALIGN(4);
|
||||
PROVIDE( _ebss = .);
|
||||
} >RAM AT>FLASH
|
||||
|
||||
PROVIDE( _end = _ebss);
|
||||
PROVIDE( end = . );
|
||||
|
||||
.stack ORIGIN(RAM) + LENGTH(RAM) - __stack_size :
|
||||
{
|
||||
PROVIDE( _heap_end = . );
|
||||
. = ALIGN(4);
|
||||
PROVIDE(_susrstack = . );
|
||||
. = . + __stack_size;
|
||||
PROVIDE( _eusrstack = .);
|
||||
} >RAM
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user