Compare commits
8 commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
90351402d8 | ||
|
|
bd1902939a | ||
|
|
fe11958be3 | ||
|
|
3b6408a650 | ||
|
|
ec2ed2d196 | ||
|
|
7d46287def | ||
|
|
73d440be4d | ||
|
|
69b9efac27 |
40 changed files with 14052 additions and 11548 deletions
23
.gitignore
vendored
23
.gitignore
vendored
|
|
@ -1,12 +1,11 @@
|
|||
/plan_manage_main/out
|
||||
/plan_manage_main/Debug
|
||||
|
||||
|
||||
*.swp
|
||||
.*
|
||||
|
||||
|
||||
|
||||
!.gitignore
|
||||
|
||||
|
||||
/plan_manage_main/out
|
||||
|
||||
|
||||
*.swp
|
||||
.*
|
||||
|
||||
|
||||
|
||||
!.gitignore
|
||||
|
||||
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load diff
File diff suppressed because it is too large
Load diff
File diff suppressed because it is too large
Load diff
File diff suppressed because it is too large
Load diff
|
|
@ -1,24 +1,24 @@
|
|||
@REM This batch file has been generated by the IAR Embedded Workbench
|
||||
@REM C-SPY Debugger, as an aid to preparing a command line for running
|
||||
@REM the cspybat command line utility using the appropriate settings.
|
||||
@REM
|
||||
@REM Note that this file is generated every time a new debug session
|
||||
@REM is initialized, so you may want to move or rename the file before
|
||||
@REM making changes.
|
||||
@REM
|
||||
@REM You can launch cspybat by typing the name of this batch file followed
|
||||
@REM by the name of the debug file (usually an ELF/DWARF or UBROF file).
|
||||
@REM
|
||||
@REM Read about available command line parameters in the C-SPY Debugging
|
||||
@REM Guide. Hints about additional command line parameters that may be
|
||||
@REM useful in specific cases:
|
||||
@REM --download_only Downloads a code image without starting a debug
|
||||
@REM session afterwards.
|
||||
@REM --silent Omits the sign-on message.
|
||||
@REM --timeout Limits the maximum allowed execution time.
|
||||
@REM
|
||||
|
||||
|
||||
"D:\work_soft\iar\arm\common\bin\cspybat" "D:\work_soft\iar\arm\arm\bin\armproc.dll" "D:\work_soft\iar\arm\arm\bin\armjlink.dll" %1 --plugin "D:\work_soft\iar\arm\arm\bin\armbat.dll" --device_macro "D:\work_soft\iar\arm\arm\config\debugger\Freescale\Kinetis.dmac" --backend -B "--endian=little" "--cpu=Cortex-M0+" "--fpu=None" "-p" "D:\work_soft\iar\arm\arm\CONFIG\debugger\Freescale\MKL25Z128xxx4.ddf" "--semihosting" "--device=MKL25Z128xxx4" "--drv_mtb_regbase=0xF0000000" "--drv_mtb_dwtregbase=0xF0001000" "--drv_mtb_minramaddr=0x20000000" "--drv_mtb_maxramaddr=0x20002FFF" "--drv_communication=USB0" "--jlink_speed=auto" "--jlink_initial_speed=1000" "--jlink_reset_strategy=0,0" "--jlink_interface=SWD" "--drv_catch_exceptions=0x000" "--drv_swo_clock_setup=72000000,0,2000000"
|
||||
|
||||
|
||||
@REM This batch file has been generated by the IAR Embedded Workbench
|
||||
@REM C-SPY Debugger, as an aid to preparing a command line for running
|
||||
@REM the cspybat command line utility using the appropriate settings.
|
||||
@REM
|
||||
@REM Note that this file is generated every time a new debug session
|
||||
@REM is initialized, so you may want to move or rename the file before
|
||||
@REM making changes.
|
||||
@REM
|
||||
@REM You can launch cspybat by typing the name of this batch file followed
|
||||
@REM by the name of the debug file (usually an ELF/DWARF or UBROF file).
|
||||
@REM
|
||||
@REM Read about available command line parameters in the C-SPY Debugging
|
||||
@REM Guide. Hints about additional command line parameters that may be
|
||||
@REM useful in specific cases:
|
||||
@REM --download_only Downloads a code image without starting a debug
|
||||
@REM session afterwards.
|
||||
@REM --silent Omits the sign-on message.
|
||||
@REM --timeout Limits the maximum allowed execution time.
|
||||
@REM
|
||||
|
||||
|
||||
"D:\work_soft\iar\arm\common\bin\cspybat" "D:\work_soft\iar\arm\arm\bin\armproc.dll" "D:\work_soft\iar\arm\arm\bin\armjlink.dll" %1 --plugin "D:\work_soft\iar\arm\arm\bin\armbat.dll" --device_macro "D:\work_soft\iar\arm\arm\config\debugger\Freescale\Kinetis.dmac" --backend -B "--endian=little" "--cpu=Cortex-M0+" "--fpu=None" "-p" "D:\work_soft\iar\arm\arm\CONFIG\debugger\Freescale\MKL25Z128xxx4.ddf" "--drv_attach_to_program" "--semihosting" "--device=MKL25Z128xxx4" "--drv_mtb_regbase=0xF0000000" "--drv_mtb_dwtregbase=0xF0001000" "--drv_mtb_minramaddr=0x20000000" "--drv_mtb_maxramaddr=0x20002FFF" "--drv_communication=USB0" "--jlink_speed=auto" "--jlink_initial_speed=1000" "--jlink_reset_strategy=0,0" "--jlink_interface=SWD" "--drv_catch_exceptions=0x000" "--drv_swo_clock_setup=72000000,0,2000000"
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,24 @@
|
|||
@REM This batch file has been generated by the IAR Embedded Workbench
|
||||
@REM C-SPY Debugger, as an aid to preparing a command line for running
|
||||
@REM the cspybat command line utility using the appropriate settings.
|
||||
@REM
|
||||
@REM Note that this file is generated every time a new debug session
|
||||
@REM is initialized, so you may want to move or rename the file before
|
||||
@REM making changes.
|
||||
@REM
|
||||
@REM You can launch cspybat by typing the name of this batch file followed
|
||||
@REM by the name of the debug file (usually an ELF/DWARF or UBROF file).
|
||||
@REM
|
||||
@REM Read about available command line parameters in the C-SPY Debugging
|
||||
@REM Guide. Hints about additional command line parameters that may be
|
||||
@REM useful in specific cases:
|
||||
@REM --download_only Downloads a code image without starting a debug
|
||||
@REM session afterwards.
|
||||
@REM --silent Omits the sign-on message.
|
||||
@REM --timeout Limits the maximum allowed execution time.
|
||||
@REM
|
||||
|
||||
|
||||
"D:\work_soft\iar\arm\common\bin\cspybat" "D:\work_soft\iar\arm\arm\bin\armproc.dll" "D:\work_soft\iar\arm\arm\bin\armsim2.dll" %1 --plugin "D:\work_soft\iar\arm\arm\bin\armbat.dll" --device_macro "D:\work_soft\iar\arm\arm\config\debugger\Freescale\Kinetis.dmac" --backend -B "--endian=little" "--cpu=Cortex-M0+" "--fpu=None" "-p" "D:\work_soft\iar\arm\arm\CONFIG\debugger\Freescale\MKL25Z128xxx4.ddf" "--semihosting=none" "--device=MKL25Z128xxx4" "--drv_mtb_regbase=0xF0000000" "--drv_mtb_dwtregbase=0xF0001000" "--drv_mtb_minramaddr=0x20000000" "--drv_mtb_maxramaddr=0x20002FFF" "--multicore_nr_of_cores=1"
|
||||
|
||||
|
||||
|
|
@ -1,16 +1,16 @@
|
|||
<?xml version="1.0" encoding="iso-8859-1"?>
|
||||
|
||||
<crun>
|
||||
<version>1</version>
|
||||
<filter_entries>
|
||||
<filter index="0" type="default">
|
||||
<type>*</type>
|
||||
<start_file>*</start_file>
|
||||
<end_file>*</end_file>
|
||||
<action_debugger>0</action_debugger>
|
||||
<action_log>1</action_log>
|
||||
</filter>
|
||||
</filter_entries>
|
||||
</crun>
|
||||
|
||||
|
||||
<?xml version="1.0" encoding="iso-8859-1"?>
|
||||
|
||||
<crun>
|
||||
<version>1</version>
|
||||
<filter_entries>
|
||||
<filter index="0" type="default">
|
||||
<type>*</type>
|
||||
<start_file>*</start_file>
|
||||
<end_file>*</end_file>
|
||||
<action_debugger>0</action_debugger>
|
||||
<action_log>1</action_log>
|
||||
</filter>
|
||||
</filter_entries>
|
||||
</crun>
|
||||
|
||||
|
||||
|
|
|
|||
File diff suppressed because one or more lines are too long
|
|
@ -1,76 +1,118 @@
|
|||
[Stack]
|
||||
FillEnabled=0
|
||||
OverflowWarningsEnabled=1
|
||||
WarningThreshold=90
|
||||
SpWarningsEnabled=1
|
||||
WarnLogOnly=1
|
||||
UseTrigger=1
|
||||
TriggerName=main
|
||||
LimitSize=0
|
||||
ByteLimit=50
|
||||
[DebugChecksum]
|
||||
Checksum=130347300
|
||||
[CodeCoverage]
|
||||
Enabled=_ 0
|
||||
[Exceptions]
|
||||
StopOnUncaught=_ 0
|
||||
StopOnThrow=_ 0
|
||||
[CallStack]
|
||||
ShowArgs=0
|
||||
[Disassembly]
|
||||
MixedMode=1
|
||||
[InterruptLog]
|
||||
LogEnabled=0
|
||||
SumEnabled=0
|
||||
GraphEnabled=0
|
||||
ShowTimeLog=1
|
||||
ShowTimeSum=1
|
||||
SumSortOrder=0
|
||||
[DataLog]
|
||||
LogEnabled=0
|
||||
SumEnabled=0
|
||||
GraphEnabled=0
|
||||
ShowTimeLog=1
|
||||
ShowTimeSum=1
|
||||
[Interrupts]
|
||||
Enabled=1
|
||||
[MemConfig]
|
||||
Base=1
|
||||
Manual=0
|
||||
Ddf=1
|
||||
TypeViol=0
|
||||
Stop=1
|
||||
[Simulator]
|
||||
Freq=10000000
|
||||
MultiCoreRunAll=1
|
||||
[JLinkDriver]
|
||||
CStepIntDis=_ 0
|
||||
TraceBufferSize=0x00010000
|
||||
TraceStallIfFIFOFull=0x00000000
|
||||
TracePortSize=0x00000004
|
||||
[Trace1]
|
||||
Enabled=0
|
||||
ShowSource=1
|
||||
[Disassemble mode]
|
||||
mode=0
|
||||
[Breakpoints2]
|
||||
Bp0=_ 0 "EMUL_CODE" "{$PROJ_DIR$\..\src\app\flash.c}.156.9" 0 0 1 "" 0 "" 0
|
||||
Count=1
|
||||
[Log file]
|
||||
LoggingEnabled=_ 0
|
||||
LogFile=_ ""
|
||||
Category=_ 0
|
||||
[TermIOLog]
|
||||
LoggingEnabled=_ 0
|
||||
LogFile=_ ""
|
||||
[Aliases]
|
||||
Count=0
|
||||
SuppressDialog=0
|
||||
[CallStackLog]
|
||||
Enabled=0
|
||||
[DriverProfiling]
|
||||
Enabled=0
|
||||
Mode=1
|
||||
Graph=0
|
||||
Symbiont=0
|
||||
Exclusions=
|
||||
[Stack]
|
||||
FillEnabled=0
|
||||
OverflowWarningsEnabled=1
|
||||
WarningThreshold=90
|
||||
SpWarningsEnabled=1
|
||||
WarnLogOnly=1
|
||||
UseTrigger=1
|
||||
TriggerName=main
|
||||
LimitSize=0
|
||||
ByteLimit=50
|
||||
[DebugChecksum]
|
||||
Checksum=2097835749
|
||||
[CodeCoverage]
|
||||
Enabled=_ 0
|
||||
[Exceptions]
|
||||
StopOnUncaught=_ 0
|
||||
StopOnThrow=_ 0
|
||||
[CallStack]
|
||||
ShowArgs=0
|
||||
[Disassembly]
|
||||
MixedMode=1
|
||||
[JLinkDriver]
|
||||
CStepIntDis=_ 0
|
||||
TraceBufferSize=0x00010000
|
||||
TraceStallIfFIFOFull=0x00000000
|
||||
TracePortSize=0x00000004
|
||||
[SWOTraceHWSettings]
|
||||
OverrideDefaultClocks=0
|
||||
CpuClock=72000000
|
||||
ClockAutoDetect=0
|
||||
ClockWanted=2000000
|
||||
JtagSpeed=2000000
|
||||
Prescaler=36
|
||||
TimeStampPrescIndex=0
|
||||
TimeStampPrescData=0
|
||||
PcSampCYCTAP=1
|
||||
PcSampPOSTCNT=15
|
||||
PcSampIndex=0
|
||||
DataLogMode=0
|
||||
ITMportsEnable=0
|
||||
ITMportsTermIO=0
|
||||
ITMportsLogFile=0
|
||||
ITMlogFile=$PROJ_DIR$\ITM.log
|
||||
[SWOTraceWindow]
|
||||
PcSampling=0
|
||||
InterruptLogs=0
|
||||
ForcedTimeStamps=0
|
||||
EventCPI=0
|
||||
EventEXC=0
|
||||
EventFOLD=0
|
||||
EventLSU=0
|
||||
EventSLEEP=0
|
||||
[PowerLog]
|
||||
LogEnabled=0
|
||||
GraphEnabled=0
|
||||
ShowTimeLog=1
|
||||
ShowTimeSum=0
|
||||
Title_0=I0
|
||||
Symbol_0=0 4 1
|
||||
LiveEnabled=0
|
||||
LiveFile=PowerLogLive.log
|
||||
[EventLog]
|
||||
LogEnabled=0
|
||||
SumEnabled=0
|
||||
GraphEnabled=0
|
||||
ShowTimeLog=1
|
||||
ShowTimeSum=1
|
||||
SumSortOrder=0
|
||||
[Log file]
|
||||
LoggingEnabled=_ 0
|
||||
LogFile=_ ""
|
||||
Category=_ 0
|
||||
[TermIOLog]
|
||||
LoggingEnabled=_ 0
|
||||
LogFile=_ ""
|
||||
[CallStackLog]
|
||||
Enabled=0
|
||||
[PowerProbe]
|
||||
Frequency=10000
|
||||
Probe0=I0
|
||||
ProbeSetup0=2 1 1 2 0 0
|
||||
[DriverProfiling]
|
||||
Enabled=0
|
||||
Mode=1
|
||||
Graph=0
|
||||
Symbiont=0
|
||||
Exclusions=
|
||||
[InterruptLog]
|
||||
LogEnabled=0
|
||||
SumEnabled=0
|
||||
GraphEnabled=0
|
||||
ShowTimeLog=1
|
||||
ShowTimeSum=1
|
||||
SumSortOrder=0
|
||||
[DataLog]
|
||||
LogEnabled=0
|
||||
SumEnabled=0
|
||||
GraphEnabled=0
|
||||
ShowTimeLog=1
|
||||
ShowTimeSum=1
|
||||
[Interrupts]
|
||||
Enabled=1
|
||||
[MemConfig]
|
||||
Base=1
|
||||
Manual=0
|
||||
Ddf=1
|
||||
TypeViol=0
|
||||
Stop=1
|
||||
[Simulator]
|
||||
Freq=10000000
|
||||
MultiCoreRunAll=1
|
||||
[Disassemble mode]
|
||||
mode=0
|
||||
[Breakpoints2]
|
||||
Count=0
|
||||
[Aliases]
|
||||
Count=0
|
||||
SuppressDialog=0
|
||||
|
|
|
|||
BIN
plan_manage_main/res/pm_tft_001.HMI
Normal file
BIN
plan_manage_main/res/pm_tft_001.HMI
Normal file
Binary file not shown.
|
|
@ -1,533 +0,0 @@
|
|||
/****************************************************************************
|
||||
(c) Copyright 2013-2014 Freescale Semiconductor, Inc.
|
||||
ALL RIGHTS RESERVED.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
* Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in the
|
||||
documentation and/or other materials provided with the distribution.
|
||||
* Neither the name of the <organization> nor the
|
||||
names of its contributors may be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY DIRECT,
|
||||
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*****************************************************************************
|
||||
|
||||
*************************************************************************
|
||||
* *
|
||||
* Standard Software Flash Driver For FTFx *
|
||||
* *
|
||||
* FILE NAME : NormalDemo_Flash.c *
|
||||
* DATE : April 08, 2014 *
|
||||
* *
|
||||
* AUTHOR : FPT Team *
|
||||
* E-mail : r56611@freescale.com *
|
||||
* *
|
||||
*************************************************************************/
|
||||
|
||||
/************************** CHANGES *************************************
|
||||
0.1.0 04.23.2013 FPT Team Initial Version
|
||||
1.0.0 11.25.2013 FPT Team Optimize Version
|
||||
1.0.2 08.04.2014 FPT Team Update to follow SDK convention(MISRA-C)
|
||||
*************************************************************************/
|
||||
/* include the header files */
|
||||
#include "SSD_FTFx.h"
|
||||
#include "demo_cfg.h"
|
||||
|
||||
|
||||
FLASH_SSD_CONFIG flashSSDConfig =
|
||||
{
|
||||
FTFx_REG_BASE, /* FTFx control register base */
|
||||
PFLASH_BLOCK_BASE, /* base address of PFlash block */
|
||||
PBLOCK_SIZE, /* size of PFlash block */
|
||||
DEFLASH_BLOCK_BASE, /* base address of DFlash block */
|
||||
0x0U, /* size of DFlash block */
|
||||
EERAM_BLOCK_BASE, /* base address of EERAM block */
|
||||
0x0U, /* size of EEE block */
|
||||
DEBUGENABLE, /* background debug mode enable bit */
|
||||
NULL_CALLBACK /* pointer to callback function */
|
||||
};
|
||||
|
||||
#define CALLBACK_SIZE 0x30U
|
||||
#define LAUNCH_CMD_SIZE 0x80U
|
||||
|
||||
uint8_t DataArray[PGM_SIZE_BYTE];
|
||||
uint8_t buffer[BUFFER_SIZE_BYTE];
|
||||
uint32_t gCallBackCnt; /* global counter in callback(). */
|
||||
pFLASHCOMMANDSEQUENCE g_FlashLaunchCommand = (pFLASHCOMMANDSEQUENCE)0xFFFFFFFF;
|
||||
|
||||
uint16_t __ram_func[LAUNCH_CMD_SIZE/2]; /* array to copy __Launch_Command func to RAM */
|
||||
uint16_t __ram_for_callback[CALLBACK_SIZE/2]; /* length of this array depends on total size of the functions need to be copied to RAM*/
|
||||
void callback(void);
|
||||
|
||||
/*********************************************************************
|
||||
*
|
||||
* Function Name : main
|
||||
* Description : Main function
|
||||
*
|
||||
* Arguments : void
|
||||
* Return Value : UNIT32
|
||||
*
|
||||
**********************************************************************/
|
||||
void main(void)
|
||||
{
|
||||
uint32_t ret; /* Return code from each SSD function */
|
||||
uint32_t dest; /* Address of the target location */
|
||||
uint32_t size;
|
||||
uint8_t securityStatus; /* Return protection status */
|
||||
uint32_t protectStatus; /* Store Protection Status Value of PFLSH or DFLASH or EEPROM */
|
||||
uint32_t FailAddr;
|
||||
uint16_t number; /* Number of longword or phrase to be program or verify*/
|
||||
uint32_t sum;
|
||||
uint32_t temp;
|
||||
uint32_t i;
|
||||
uint32_t j;
|
||||
|
||||
gCallBackCnt = 0x0U;
|
||||
|
||||
#if ((defined(X_TWR_KV10Z32)) || defined(KM34Z50M_BACES) || (defined(TWR_KL46Z48M)))
|
||||
CACHE_DISABLE
|
||||
#else
|
||||
//CACHE_DISABLE
|
||||
#endif
|
||||
|
||||
/* Set CallBack to callback function */
|
||||
flashSSDConfig.CallBack = (PCALLBACK)RelocateFunction((uint32_t)__ram_for_callback , CALLBACK_SIZE , (uint32_t)callback);
|
||||
g_FlashLaunchCommand = (pFLASHCOMMANDSEQUENCE)RelocateFunction((uint32_t)__ram_func , LAUNCH_CMD_SIZE ,(uint32_t)FlashCommandSequence);
|
||||
|
||||
/**************************************************************************
|
||||
* FlashInit() *
|
||||
***************************************************************************/
|
||||
ret = FlashInit(&flashSSDConfig);
|
||||
if (FTFx_OK != ret)
|
||||
{
|
||||
ErrorTrap(ret);
|
||||
}
|
||||
|
||||
#ifdef twr_mc56f82748
|
||||
PE_low_level_init();
|
||||
#endif
|
||||
|
||||
#if ((!(defined(FTFA_M))) || (defined(BLOCK_COMMANDS)))
|
||||
/**************************************************************************
|
||||
* FlashEraseBlock() and FlashVerifyBlock() *
|
||||
***************************************************************************/
|
||||
/* Erase for each individual Pflash block */
|
||||
for (i = 0x1U; i < PBLOCK_NUM; i++)
|
||||
{
|
||||
dest = flashSSDConfig.PFlashBlockBase + BYTE2WORD(i*flashSSDConfig.PFlashBlockSize/PBLOCK_NUM);
|
||||
ret = FlashEraseBlock(&flashSSDConfig, dest, g_FlashLaunchCommand);
|
||||
if (FTFx_OK != ret)
|
||||
{
|
||||
ErrorTrap(ret);
|
||||
}
|
||||
/* verification for normal and user margin levels */
|
||||
for (j = 0x0U; j < 0x2U; j++)
|
||||
{
|
||||
ret = FlashVerifyBlock(&flashSSDConfig, dest, j, g_FlashLaunchCommand);
|
||||
if (FTFx_OK != ret)
|
||||
{
|
||||
ErrorTrap(ret);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
/**************************************************************************
|
||||
* FlashEraseSector() and FlashVerifySection() *
|
||||
***************************************************************************/
|
||||
/* Erase several sectors on Pflash*/
|
||||
dest = flashSSDConfig.PFlashBlockBase + BYTE2WORD(flashSSDConfig.PFlashBlockSize - 0x4U * FTFx_PSECTOR_SIZE);
|
||||
while ((dest + BYTE2WORD(FTFx_PSECTOR_SIZE)) < (flashSSDConfig.PFlashBlockBase + BYTE2WORD(flashSSDConfig.PFlashBlockSize)))
|
||||
{
|
||||
size = FTFx_PSECTOR_SIZE;
|
||||
ret = FlashEraseSector(&flashSSDConfig, dest, size, g_FlashLaunchCommand);
|
||||
if (FTFx_OK != ret)
|
||||
{
|
||||
ErrorTrap(ret);
|
||||
}
|
||||
|
||||
/* Verify section for several sector of PFLASH */
|
||||
number = FTFx_PSECTOR_SIZE / PRD1SEC_ALIGN_SIZE;
|
||||
for(i = 0x0U; i < 0x2U; i++)
|
||||
{
|
||||
ret = FlashVerifySection(&flashSSDConfig, dest, number, i, g_FlashLaunchCommand);
|
||||
if (FTFx_OK != ret)
|
||||
{
|
||||
ErrorTrap(ret);
|
||||
}
|
||||
}
|
||||
dest += BYTE2WORD(size);
|
||||
}
|
||||
|
||||
#if (0x0U != DEBLOCK_SIZE)
|
||||
/* Erase several sectors on Dflash*/
|
||||
dest = flashSSDConfig.DFlashBlockBase;
|
||||
while ((dest + BYTE2WORD(FTFx_DSECTOR_SIZE)) < (flashSSDConfig.DFlashBlockBase + BYTE2WORD(4*FTFx_DSECTOR_SIZE)))
|
||||
{
|
||||
size = FTFx_DSECTOR_SIZE;
|
||||
ret = FlashEraseSector(&flashSSDConfig, dest, size, g_FlashLaunchCommand);
|
||||
if (FTFx_OK != ret)
|
||||
{
|
||||
ErrorTrap(ret);
|
||||
}
|
||||
|
||||
/* Verify section for several sector of DFLASH */
|
||||
number = FTFx_DSECTOR_SIZE/DRD1SEC_ALIGN_SIZE;
|
||||
for(i = 0x0U; i < 0x2U; i ++)
|
||||
{
|
||||
ret = FlashVerifySection(&flashSSDConfig, dest, number, i, g_FlashLaunchCommand);
|
||||
if (FTFx_OK != ret)
|
||||
{
|
||||
ErrorTrap(ret);
|
||||
}
|
||||
}
|
||||
dest += BYTE2WORD(FTFx_DSECTOR_SIZE);
|
||||
|
||||
}
|
||||
#endif /* DEBLOCK_SIZE */
|
||||
|
||||
/**************************************************************************
|
||||
* FlashProgram() FlashCheckSum and FlashProgramCheck() *
|
||||
***************************************************************************/
|
||||
/* Initialize source buffer */
|
||||
for (i = 0x0U; i < BUFFER_SIZE_BYTE; i++)
|
||||
{
|
||||
/* Set source buffer */
|
||||
buffer[i] = i;
|
||||
}
|
||||
|
||||
/* Program to the end location of PFLASH */
|
||||
size = BUFFER_SIZE_BYTE;
|
||||
dest = flashSSDConfig.PFlashBlockBase + BYTE2WORD(flashSSDConfig.PFlashBlockSize - (uint32_t)(0x3U * FTFx_PSECTOR_SIZE));
|
||||
|
||||
while ((dest + BYTE2WORD(size)) < (flashSSDConfig.PFlashBlockBase + BYTE2WORD(flashSSDConfig.PFlashBlockSize)))
|
||||
{
|
||||
ret = FlashProgram(&flashSSDConfig, dest, size, \
|
||||
buffer, g_FlashLaunchCommand);
|
||||
if (FTFx_OK != ret)
|
||||
{
|
||||
ErrorTrap(ret);
|
||||
}
|
||||
|
||||
/* Flash CheckSum */
|
||||
sum = temp = 0x0U;
|
||||
for (i = 0x0U; i < size; i++)
|
||||
{
|
||||
temp += buffer[i];
|
||||
}
|
||||
ret = FlashCheckSum(&flashSSDConfig, dest, size, &sum);
|
||||
if ((FTFx_OK != ret) || (temp != sum))
|
||||
{
|
||||
ErrorTrap(ret);
|
||||
}
|
||||
|
||||
/* Program Check for normal and user margin levels*/
|
||||
for (i = 0x1U; i < 0x3U; i ++)
|
||||
{
|
||||
ret = FlashProgramCheck(&flashSSDConfig, dest, size, buffer, \
|
||||
&FailAddr, i, g_FlashLaunchCommand);
|
||||
if (FTFx_OK != ret)
|
||||
{
|
||||
ErrorTrap(ret);
|
||||
}
|
||||
}
|
||||
|
||||
dest += BYTE2WORD(BUFFER_SIZE_BYTE);
|
||||
}
|
||||
|
||||
#if (0x0 != DEBLOCK_SIZE)
|
||||
/* Program to the DFLASH block*/
|
||||
size = BUFFER_SIZE_BYTE;
|
||||
dest = flashSSDConfig.DFlashBlockBase;
|
||||
while ((dest + BYTE2WORD(size)) < (flashSSDConfig.DFlashBlockBase + BYTE2WORD(0x4U * FTFx_DSECTOR_SIZE)))
|
||||
{
|
||||
ret = FlashProgram(&flashSSDConfig, dest, size, \
|
||||
buffer, g_FlashLaunchCommand);
|
||||
if (FTFx_OK != ret)
|
||||
{
|
||||
ErrorTrap(ret);
|
||||
}
|
||||
|
||||
/* Flash CheckSum */
|
||||
sum = 0x0U;
|
||||
ret = FlashCheckSum(&flashSSDConfig, dest, size, &sum);
|
||||
if ((FTFx_OK != ret) || (temp != sum))
|
||||
{
|
||||
ErrorTrap(ret);
|
||||
}
|
||||
|
||||
/* Program Check for normal and user margin levels*/
|
||||
for (i = 0x1U; i < 0x3U; i++)
|
||||
{
|
||||
ret = FlashProgramCheck(&flashSSDConfig, dest, size, buffer, \
|
||||
&FailAddr, i, g_FlashLaunchCommand);
|
||||
if (FTFx_OK != ret)
|
||||
{
|
||||
ErrorTrap(ret);
|
||||
}
|
||||
}
|
||||
dest += BYTE2WORD(BUFFER_SIZE_BYTE);
|
||||
}
|
||||
|
||||
/**************************************************************************
|
||||
* SetEEEEnable() *
|
||||
***************************************************************************/
|
||||
|
||||
ret = SetEEEEnable(&flashSSDConfig, RAM_ENABLE ,g_FlashLaunchCommand);
|
||||
if (FTFx_OK != ret)
|
||||
{
|
||||
ErrorTrap(ret);
|
||||
}
|
||||
|
||||
#endif /* DEBLOCK_SIZE */
|
||||
|
||||
#ifndef FTFA_M
|
||||
/**************************************************************************
|
||||
* FlashProgramSection() *
|
||||
***************************************************************************/
|
||||
/* Write some values to EERAM */
|
||||
for (i = 0x0U; i < 0x100U; i += 0x4U)
|
||||
{
|
||||
WRITE32(flashSSDConfig.EERAMBlockBase + i,0x11223344U);
|
||||
}
|
||||
/* Erase sector for program section */
|
||||
dest = flashSSDConfig.PFlashBlockBase + BYTE2WORD(flashSSDConfig.PFlashBlockSize/PBLOCK_NUM - 0x4U * FTFx_PSECTOR_SIZE);
|
||||
ret = FlashEraseSector(&flashSSDConfig, dest, FTFx_PSECTOR_SIZE, \
|
||||
g_FlashLaunchCommand);
|
||||
if (FTFx_OK != ret)
|
||||
{
|
||||
ErrorTrap(ret);
|
||||
}
|
||||
|
||||
/* Program section to the flash block*/
|
||||
number = 0x2U;
|
||||
ret = FlashProgramSection(&flashSSDConfig, dest, number, g_FlashLaunchCommand);
|
||||
if (FTFx_OK != ret)
|
||||
{
|
||||
ErrorTrap(ret);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if (0x0U != DEBLOCK_SIZE)
|
||||
/* Erase the first sector of DFlash */
|
||||
dest = flashSSDConfig.DFlashBlockBase;
|
||||
ret = FlashEraseSector(&flashSSDConfig, dest, FTFx_PSECTOR_SIZE, \
|
||||
g_FlashLaunchCommand);
|
||||
if (FTFx_OK != ret)
|
||||
{
|
||||
ErrorTrap(ret);
|
||||
}
|
||||
/* Program the 1st sector of DFLASH */
|
||||
number = FTFx_DSECTOR_SIZE / (DPGMSEC_ALIGN_SIZE * 0x4U);
|
||||
dest = flashSSDConfig.DFlashBlockBase;
|
||||
ret = FlashProgramSection(&flashSSDConfig, dest, number, g_FlashLaunchCommand);
|
||||
if (FTFx_OK != ret)
|
||||
{
|
||||
ErrorTrap(ret);
|
||||
}
|
||||
#endif /* DEBLOCK_SIZE */
|
||||
|
||||
/**************************************************************************
|
||||
* FlashGetSecurityState() *
|
||||
***************************************************************************/
|
||||
securityStatus = 0x0U;
|
||||
ret = FlashGetSecurityState(&flashSSDConfig, &securityStatus);
|
||||
|
||||
if (FTFx_OK != ret)
|
||||
{
|
||||
ErrorTrap(ret);
|
||||
}
|
||||
/**************************************************************************
|
||||
* FlashReadResource() *
|
||||
***************************************************************************/
|
||||
/* Read on P-Flash */
|
||||
dest = flashSSDConfig.PFlashBlockBase + PFLASH_IFR; /* Start address of Program Once Field */
|
||||
ret = FlashReadResource(&flashSSDConfig, dest, DataArray, 0x0U, g_FlashLaunchCommand);
|
||||
|
||||
if (FTFx_OK != ret)
|
||||
{
|
||||
ErrorTrap(ret);
|
||||
}
|
||||
|
||||
#if (DEBLOCK_SIZE != 0)
|
||||
/* Read on D-Flash */
|
||||
dest = flashSSDConfig.DFlashBlockBase + DFLASH_IFR;
|
||||
ret = FlashReadResource(&flashSSDConfig, dest, DataArray, 0x0U, g_FlashLaunchCommand);
|
||||
|
||||
if (FTFx_OK != ret)
|
||||
{
|
||||
ErrorTrap(ret);
|
||||
}
|
||||
|
||||
/**************************************************************************
|
||||
* DEFlashPartition() *
|
||||
***************************************************************************/
|
||||
if (0x0U == flashSSDConfig.EEEBlockSize)
|
||||
{
|
||||
ret = DEFlashPartition(&flashSSDConfig, \
|
||||
EEE_DATA_SIZE_CODE, \
|
||||
DE_PARTITION_CODE, \
|
||||
g_FlashLaunchCommand);
|
||||
if (FTFx_OK != ret)
|
||||
{
|
||||
ErrorTrap(ret);
|
||||
}
|
||||
|
||||
/* Call FlashInit again to get the new Flash configuration */
|
||||
ret = FlashInit(&flashSSDConfig);
|
||||
if (FTFx_OK != ret)
|
||||
{
|
||||
ErrorTrap(ret);
|
||||
}
|
||||
}
|
||||
|
||||
/**************************************************************************
|
||||
* EEEWrite() *
|
||||
***************************************************************************/
|
||||
dest = flashSSDConfig.EERAMBlockBase;
|
||||
size = FTFx_WORD_SIZE;
|
||||
ret = EEEWrite(&flashSSDConfig, dest, size, buffer);
|
||||
if (FTFx_OK != ret)
|
||||
{
|
||||
ErrorTrap(ret);
|
||||
}
|
||||
|
||||
/**************************************************************************
|
||||
* EEEWrite() *
|
||||
***************************************************************************/
|
||||
ret = SetEEEEnable(&flashSSDConfig, EE_ENABLE ,g_FlashLaunchCommand);
|
||||
if (FTFx_OK != ret)
|
||||
{
|
||||
ErrorTrap(ret);
|
||||
}
|
||||
|
||||
dest = flashSSDConfig.EERAMBlockBase;
|
||||
size = FTFx_WORD_SIZE;
|
||||
ret = EEEWrite(&flashSSDConfig, dest, size, buffer);
|
||||
if (FTFx_OK != ret)
|
||||
{
|
||||
ErrorTrap(ret);
|
||||
}
|
||||
|
||||
/**************************************************************************
|
||||
* EERAMGetProtection() *
|
||||
***************************************************************************/
|
||||
ret = EERAMGetProtection(&flashSSDConfig, (uint8_t *)&protectStatus);
|
||||
if (FTFx_OK != ret)
|
||||
{
|
||||
ErrorTrap(ret);
|
||||
}
|
||||
|
||||
/**************************************************************************
|
||||
* EERAMSetProtection() *
|
||||
***************************************************************************/
|
||||
protectStatus = 0xABU;
|
||||
ret = EERAMSetProtection(&flashSSDConfig, (uint8_t)protectStatus);
|
||||
if (FTFx_OK != ret)
|
||||
{
|
||||
ErrorTrap(ret);
|
||||
}
|
||||
|
||||
/* Call EERAMGetProtection to verify the set step */
|
||||
ret = EERAMGetProtection(&flashSSDConfig, (uint8_t *)&protectStatus);
|
||||
if ((FTFx_OK != ret) || (0xABU != (uint8_t)protectStatus))
|
||||
{
|
||||
ErrorTrap(ret);
|
||||
}
|
||||
|
||||
/**************************************************************************
|
||||
* DFlashGetProtection() *
|
||||
***************************************************************************/
|
||||
ret = DFlashGetProtection(&flashSSDConfig, (uint8_t *)&protectStatus);
|
||||
if (FTFx_OK != ret)
|
||||
{
|
||||
ErrorTrap(ret);
|
||||
}
|
||||
|
||||
/**************************************************************************
|
||||
* DFlashSetProtection() *
|
||||
***************************************************************************/
|
||||
protectStatus = 0xAAU;
|
||||
ret = DFlashSetProtection(&flashSSDConfig, (uint8_t)protectStatus);
|
||||
if (FTFx_OK != ret)
|
||||
{
|
||||
ErrorTrap(ret);
|
||||
}
|
||||
|
||||
/* Call DFlashGetProtection to verify the set step */
|
||||
ret = DFlashGetProtection(&flashSSDConfig, (uint8_t *)&protectStatus);
|
||||
if ((FTFx_OK != ret) || (0xAAU != (uint8_t)protectStatus))
|
||||
{
|
||||
ErrorTrap(ret);
|
||||
}
|
||||
|
||||
#endif /* DEBLOCK_SIZE */
|
||||
|
||||
/**************************************************************************
|
||||
* PFlashGetProtection() *
|
||||
***************************************************************************/
|
||||
ret = PFlashGetProtection(&flashSSDConfig, &protectStatus);
|
||||
if (FTFx_OK != ret)
|
||||
{
|
||||
ErrorTrap(ret);
|
||||
}
|
||||
|
||||
/**************************************************************************
|
||||
* PFlashSetProtection() *
|
||||
***************************************************************************/
|
||||
protectStatus = 0x12ABCDEFU;
|
||||
ret = PFlashSetProtection(&flashSSDConfig, protectStatus);
|
||||
if (FTFx_OK != ret)
|
||||
{
|
||||
ErrorTrap(ret);
|
||||
}
|
||||
|
||||
/* Call PFlashGetProtection to verify the set step */
|
||||
ret = PFlashGetProtection(&flashSSDConfig, &protectStatus);
|
||||
if ((FTFx_OK != ret) || (0x12ABCDEFU != protectStatus))
|
||||
{
|
||||
ErrorTrap(ret);
|
||||
}
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
while(1);
|
||||
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
*
|
||||
* Function Name : ErrorTrap
|
||||
* Description : Gets called when an error occurs.
|
||||
* Arguments : uint32_t
|
||||
* Return Value :
|
||||
*
|
||||
*********************************************************************/
|
||||
void ErrorTrap(uint32_t ret)
|
||||
{
|
||||
while (1)
|
||||
{
|
||||
;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void callback(void)
|
||||
{
|
||||
/* just increase this variable to observer that this callback() func has been involked */
|
||||
gCallBackCnt++;
|
||||
}
|
||||
/* end of file */
|
||||
|
|
@ -1,74 +1,83 @@
|
|||
/*
|
||||
* config.h - 配置整个工程
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef CONFIG_H
|
||||
#define CONFIG_H
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* 模拟定时器使用的定时器
|
||||
*/
|
||||
#define SIMULAT_TIMER_PITX PIT1
|
||||
|
||||
|
||||
/*
|
||||
* 8266
|
||||
*/
|
||||
#define WIFI_UARTX UART0 // PTA1,PTA2
|
||||
|
||||
/*
|
||||
* 调试模块,或云台
|
||||
*/
|
||||
#define DEBUG_UARTX UART1 // PTC3,PTC4
|
||||
|
||||
|
||||
/*
|
||||
* 输入模块
|
||||
*/
|
||||
#define UP_KEY_PINX PTE20
|
||||
#define DOWN_KEY_PINX PTA17
|
||||
#define OK_KEY_PINX PTE23
|
||||
#define RET_KEY_PINX PTA4
|
||||
#define AMS_KEY_PINX PTA5
|
||||
#define WIFI_KEY_PINX PTD7
|
||||
#define MR_KEY_PINX PTE30
|
||||
#define MB_KEY_PINX PTA16
|
||||
#define MUVB_KEY_PINX PTD6
|
||||
// knob用必须是PTA或PTD端口的
|
||||
#define KNOB_A_PINX PTD6
|
||||
#define KNOB_B_PINX PTD7
|
||||
#define KNOB_KEY_PINX PTD4
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* 显示模块
|
||||
*/
|
||||
#define TFT_UARTX UART2 // PTD2,PTD3
|
||||
|
||||
|
||||
/*
|
||||
* 时间模块
|
||||
*/
|
||||
#define DS1302_CE_PINX PTB2
|
||||
#define DS1302_CLK_PINX PTB1
|
||||
#define DS1302_IO_PINX PTB0
|
||||
|
||||
/*
|
||||
* 控制执行模块
|
||||
*
|
||||
*/
|
||||
#define LGRED_PINX PTC12
|
||||
#define LGBLUE_PINX PTC13
|
||||
#define LGUVB_PINX PTC15
|
||||
#define WATER_PINX PTC16
|
||||
|
||||
/*
|
||||
* led控制,多选几个任意端口
|
||||
*/
|
||||
|
||||
#endif // CONFIG_H
|
||||
/*
|
||||
* config.h - 配置整个工程
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef CONFIG_H
|
||||
#define CONFIG_H
|
||||
|
||||
|
||||
|
||||
#define PM_DEBUG
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* 模拟定时器使用的定时器
|
||||
*/
|
||||
#define SIMULAT_TIMER_PITX PIT1
|
||||
|
||||
|
||||
/*
|
||||
* 调试模块,或8266
|
||||
*/
|
||||
#define WIFI_UARTX UART0 // PTA1,PTA2
|
||||
#define DEBUG_UARTX UART0 // PTA1,PTA2
|
||||
|
||||
/*
|
||||
* 云台
|
||||
*/
|
||||
#define ORIENT_UARTX UART1 // PTC3,PTC4
|
||||
#define RE_DE_PINX PTC1
|
||||
|
||||
/*
|
||||
* 输入模块
|
||||
*/
|
||||
#define UP_KEY_PINX PTA4
|
||||
#define DOWN_KEY_PINX PTA5
|
||||
#define OK_KEY_PINX PTE20
|
||||
#define RET_KEY_PINX PTE30
|
||||
#define AMS_KEY_PINX PTD5
|
||||
#define WIFI_KEY_PINX PTD7
|
||||
#define MR_KEY_PINX PTE23
|
||||
#define MB_KEY_PINX PTD4
|
||||
#define MUVB_KEY_PINX PTA16
|
||||
// knob用必须是PTA或PTD端口的
|
||||
#define KNOB_A_PINX PTD7
|
||||
#define KNOB_B_PINX PTD6
|
||||
#define KNOB_KEY_PINX PTE20
|
||||
|
||||
#define MOISTURE_ADC0_CHN ADC0_DP1 // PTE16
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* 显示模块
|
||||
*/
|
||||
#define TFT_UARTX UART2 // PTD2,PTD3
|
||||
|
||||
|
||||
/*
|
||||
* 时间模块
|
||||
*/
|
||||
#define DS1302_CE_PINX PTB2
|
||||
#define DS1302_CLK_PINX PTB1
|
||||
#define DS1302_IO_PINX PTB3
|
||||
|
||||
/*
|
||||
* 控制执行模块
|
||||
*
|
||||
*/
|
||||
#define LGRED_PINX PTC17
|
||||
#define LGBLUE_PINX PTC13
|
||||
#define LGUVB_PINX PTC16
|
||||
#define WATER_PINX PTD1
|
||||
|
||||
/*
|
||||
* led控制,多选几个任意端口
|
||||
*/
|
||||
#define LG1_PINX PTC9
|
||||
#define LG2_PINX PTC8
|
||||
#define LG3_PINX PTC7
|
||||
#endif // CONFIG_H
|
||||
|
|
|
|||
|
|
@ -1,37 +0,0 @@
|
|||
/*
|
||||
* 文件:input.h
|
||||
* 功能:将外设输入的数据转换为单片机内存中的数据
|
||||
*/
|
||||
|
||||
#ifndef INPUT_H
|
||||
#define INPUT_H
|
||||
|
||||
|
||||
typedef struct InData_
|
||||
{
|
||||
uint8_t isWifiOn : 1;
|
||||
uint8_t isPlanMode : 1;
|
||||
|
||||
uint8_t isRedL : 1;
|
||||
uint8_t isBlueL : 1;
|
||||
uint8_t isUvbL : 1;
|
||||
|
||||
uint8_t knobV;
|
||||
} InData;
|
||||
|
||||
|
||||
void InitInput(void);
|
||||
|
||||
void
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#endif // INPUT_H
|
||||
|
|
@ -1,58 +1,40 @@
|
|||
/* key.h - 监测并采集按键的输入
|
||||
* 共有四种输入,无键,单击,双击,长击。
|
||||
* 使用一个定时器每10ms采集一次io状态来实现的。
|
||||
*/
|
||||
|
||||
#ifndef KEY_H
|
||||
#define KEY_H
|
||||
|
||||
|
||||
typedef enum key_mean_
|
||||
{
|
||||
N_KEY = 0, S_KEY, D_KEY, L_KEY
|
||||
} key_mean;
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* key_type - 按键的种类
|
||||
*/
|
||||
typedef enum key_type_
|
||||
{
|
||||
UP_KEY = 0, DOWN_KEY, OK_KEY, RET_KEY, AMS_KEY, WIFI_KEY, MR_KEY, MB_KEY, MUVB_KEY
|
||||
} key_type;
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* key_init() - 按键模块初始化
|
||||
*
|
||||
*/
|
||||
void key_init(void);
|
||||
|
||||
|
||||
/*
|
||||
* key_detect() - 检测按键
|
||||
*
|
||||
* 要求每隔10ms调用一次
|
||||
*/
|
||||
void key_detect(void);
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* get_key_mean() - 得到指定的按键信息
|
||||
*
|
||||
* 这个函数中在取得信息后会将对应按键重新标记为N_KEY
|
||||
*/
|
||||
key_mean get_key_mean(key_type key_t);
|
||||
|
||||
|
||||
|
||||
|
||||
void clear_key_m(void);
|
||||
|
||||
|
||||
|
||||
|
||||
#endif // KEY_H
|
||||
/* key.h - 监测并采集按键的输入
|
||||
* 共有四种输入,无键,单击,双击,长击。
|
||||
* 使用一个定时器每10ms采集一次io状态来实现的。
|
||||
*/
|
||||
|
||||
#ifndef KEY_H
|
||||
#define KEY_H
|
||||
|
||||
|
||||
/*
|
||||
* key_mean - 按键的状态标志符号
|
||||
* N_KEY表示无键按下状态,S_KEY表示单击,D_KEY表示双击,L_KEY表示长按
|
||||
*/
|
||||
typedef enum key_mean_
|
||||
{
|
||||
N_KEY = 0, S_KEY, D_KEY, L_KEY
|
||||
} key_mean;
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* key_type - 按键的种类
|
||||
*/
|
||||
typedef enum key_type_
|
||||
{
|
||||
UP_KEY = 0, DOWN_KEY, OK_KEY, RET_KEY, AMS_KEY, WIFI_KEY, MR_KEY, MB_KEY, MUVB_KEY
|
||||
} key_type;
|
||||
|
||||
|
||||
|
||||
|
||||
void key_init(void);
|
||||
void key_detect(void);
|
||||
key_mean get_key_mean(key_type key_t);
|
||||
void clear_key_m(void);
|
||||
|
||||
|
||||
|
||||
|
||||
#endif // KEY_H
|
||||
|
|
|
|||
|
|
@ -1,56 +1,29 @@
|
|||
/*
|
||||
* knob.h - 提供旋钮输入的接口函数
|
||||
*
|
||||
* 使用的是一个数字旋钮,利用计数来采集输入
|
||||
*/
|
||||
|
||||
#ifndef KNOB_H
|
||||
#define KNOB_H
|
||||
|
||||
#include "arm_cm0.h"
|
||||
#include "vectors.h"
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* knob_init() - 为旋钮输入而执行的初始化
|
||||
*
|
||||
* 旋钮的检测初始化后默认是禁止的
|
||||
*/
|
||||
void knob_init(void);
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* knob_enable() - 使能旋钮脉冲检测
|
||||
*/
|
||||
void knob_enable(void);
|
||||
|
||||
|
||||
/*
|
||||
* knob_disable() - 禁止旋钮脉冲检测
|
||||
*/
|
||||
void knob_disable(void);
|
||||
|
||||
|
||||
/*
|
||||
* knob_clear() - 清除旋钮值
|
||||
*/
|
||||
void knob_clear(void);
|
||||
|
||||
/*
|
||||
* get_knob_val() - 返回旋钮值
|
||||
*/
|
||||
int32_t get_knob_val(void);
|
||||
|
||||
|
||||
/*
|
||||
* knob_detect() - 旋钮检测
|
||||
*
|
||||
* 这个函数需要在旋钮编码器的A相输出发生沿跳变时调用,即上下沿中断时
|
||||
*/
|
||||
void knob_detect(void);
|
||||
|
||||
|
||||
#endif /* KNOB_H */
|
||||
/*
|
||||
* knob.h - 提供旋钮输入的接口函数
|
||||
*
|
||||
* 使用的是一个数字旋钮,利用计数来采集输入
|
||||
*/
|
||||
|
||||
#ifndef KNOB_H
|
||||
#define KNOB_H
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void knob_init(void);
|
||||
|
||||
|
||||
void knob_enable(void);
|
||||
|
||||
void knob_disable(void);
|
||||
|
||||
void knob_clear(void);
|
||||
|
||||
int32_t get_knob_val(void);
|
||||
|
||||
void knob_detect(void);
|
||||
|
||||
|
||||
#endif /* KNOB_H */
|
||||
|
|
|
|||
51
plan_manage_main/src/app/include/orient.h
Normal file
51
plan_manage_main/src/app/include/orient.h
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
/*
|
||||
* orient.h - 方向控制
|
||||
*
|
||||
* 所有如上,所有如下
|
||||
*/
|
||||
|
||||
|
||||
#ifndef ORIENT_H
|
||||
#define ORIENT_H
|
||||
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
|
||||
|
||||
|
||||
typedef enum orient_
|
||||
{
|
||||
ORIENT_UP = 0, ORIENT_DOWN, ORIENT_RIGHT, ORIENT_LEFT
|
||||
} orient;
|
||||
|
||||
typedef enum preset_op_
|
||||
{
|
||||
PRESET_SET = 0, PRESET_CLEAR, PRESET_CALL
|
||||
} preset_op;
|
||||
|
||||
typedef enum orient_mode_
|
||||
{
|
||||
MODE_AUTO = 0, MODE_MANUL
|
||||
} orient_mode;
|
||||
|
||||
|
||||
|
||||
void orient_init(void);
|
||||
|
||||
void orient_setspeed(uint8_t addr, orient ori, uint8_t spd);
|
||||
|
||||
void orient_presetop(uint8_t addr, preset_op op, uint8_t npre);
|
||||
|
||||
void orient_setmode(uint8_t addr, orient_mode mod);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#endif /* ORIENT_H */
|
||||
|
||||
|
|
@ -1,13 +1,19 @@
|
|||
/*
|
||||
* plan_handle.h - 计划处理
|
||||
*/
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void plan_handle_init(void);
|
||||
|
||||
void plan_handle(void);
|
||||
|
||||
/*
|
||||
* plan_handle.h - 计划处理
|
||||
*/
|
||||
|
||||
#ifndef PLAN_HANDLE_H
|
||||
#define PLAN_HANDLE_H
|
||||
|
||||
|
||||
|
||||
|
||||
void plan_handle_init(void);
|
||||
|
||||
void plan_handle(void);
|
||||
|
||||
void key_func(void);
|
||||
|
||||
|
||||
|
||||
#endif /* PLAN_HANDLE_H */
|
||||
|
|
@ -14,7 +14,7 @@ void pm_flash_init(void);
|
|||
void flash_write(uint8_t *saddr, uint16_t nbyte);
|
||||
|
||||
|
||||
void flash_read(uint8_t *daddr, uint8_t nbyte);
|
||||
void flash_read(uint8_t *daddr, uint16_t nbyte);
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -1,9 +1,19 @@
|
|||
/*
|
||||
* pm_init.h - 对整个应用程序的初始化
|
||||
*/
|
||||
|
||||
|
||||
/*
|
||||
* pm_init() - 初始化
|
||||
*/
|
||||
void pm_init(void);
|
||||
/*
|
||||
* pm_init.h - 对整个应用程序的初始化
|
||||
*/
|
||||
|
||||
|
||||
#ifndef PM_INIT_H
|
||||
#define PM_INIT_H
|
||||
|
||||
/*
|
||||
* pm_init() - 初始化
|
||||
*/
|
||||
void pm_init(void);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#endif /* PM_INIT_H */
|
||||
|
|
@ -1,37 +1,41 @@
|
|||
/*
|
||||
* time.h - 时间处理
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
|
||||
typedef struct calendar_info_
|
||||
{
|
||||
uint8_t sec;
|
||||
uint8_t min;
|
||||
uint8_t hour;
|
||||
uint8_t mday;
|
||||
uint8_t month;
|
||||
uint16_t year;
|
||||
uint8_t wday;
|
||||
uint16_t yday;
|
||||
} calendar_info;
|
||||
|
||||
|
||||
uint8_t is_leapyear(uint16_t year);
|
||||
|
||||
uint8_t get_month_days(uint16_t year, uint8_t month);
|
||||
|
||||
void ds1302_init(void);
|
||||
|
||||
void ds1302_set_time(calendar_info *cal);
|
||||
|
||||
void ds1302_read_time(calendar_info *cal);
|
||||
|
||||
void maintain_system_time(void);
|
||||
|
||||
calendar_info get_system_time(void);
|
||||
|
||||
uint32_t calendar_to_sec(calendar_info *cal);
|
||||
|
||||
calendar_info sec_to_calendar(uint32_t sec);
|
||||
/*
|
||||
* time.h - 时间处理
|
||||
*/
|
||||
|
||||
#ifndef TIME_H
|
||||
#define TIME_H
|
||||
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
|
||||
/*
|
||||
* calendar_info - 分解时间
|
||||
*/
|
||||
typedef struct calendar_info_
|
||||
{
|
||||
uint8_t sec;
|
||||
uint8_t min;
|
||||
uint8_t hour;
|
||||
uint8_t mday;
|
||||
uint8_t month;
|
||||
uint16_t year;
|
||||
uint8_t wday;
|
||||
uint16_t yday;
|
||||
} calendar_info;
|
||||
|
||||
// START_YEAR为计算日历时间时的起始时间
|
||||
enum { START_YEAR = 2000, SEC_IN_DAY = 24 * 60 * 60};
|
||||
|
||||
uint8_t is_leapyear(uint16_t year);
|
||||
uint8_t get_month_days(uint16_t year, uint8_t month);
|
||||
void ds1302_init(void);
|
||||
void ds1302_set_time(calendar_info *cal);
|
||||
void ds1302_read_time(calendar_info *cal);
|
||||
void maintain_system_time(void);
|
||||
calendar_info get_system_time(void);
|
||||
uint32_t calendar_to_sec(calendar_info *cal);
|
||||
calendar_info sec_to_calendar(uint32_t sec);
|
||||
|
||||
|
||||
#endif /* TIME_H */
|
||||
|
|
|
|||
|
|
@ -1,107 +1,52 @@
|
|||
/*
|
||||
* simulat_timer.h - 实现一个模拟定时器
|
||||
*/
|
||||
|
||||
#ifndef SIMULAT_TIMER_H
|
||||
#define SIMULAT_TIMER_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
/*
|
||||
* 模拟定时器的数量
|
||||
*/
|
||||
#define SIMULAT_TIMER_NUM 5
|
||||
|
||||
|
||||
|
||||
typedef struct st_register_
|
||||
{
|
||||
uint32_t count; // 定时器计数
|
||||
uint32_t cmr; // 比较寄存器
|
||||
struct
|
||||
{
|
||||
uint8_t ten : 1; // 使能标志
|
||||
uint8_t tov : 1; // 溢出标志
|
||||
uint8_t tfc : 1; // 自由运行标志
|
||||
uint8_t tcf : 1; // 比较标志
|
||||
} flag;
|
||||
} st_register;
|
||||
|
||||
|
||||
typedef enum st_mode_
|
||||
{
|
||||
COMPARE = 0,
|
||||
NO_COMPARE
|
||||
} st_mode;
|
||||
|
||||
/*
|
||||
* st_init() - 初始化实现模拟定时器所需的pit定时器
|
||||
*
|
||||
* 基础定时为1ms
|
||||
*/
|
||||
void st_base_init(void);
|
||||
|
||||
|
||||
/*
|
||||
* st_init() - 初始化一个给定编号的模拟定时器
|
||||
* @n: 定时器编号
|
||||
* @st_m: 定时模式
|
||||
* @cmr_v: 定时器比较值,在非比较模式可以为任意值
|
||||
*
|
||||
* 这个函数返回0表示正常完成了初始化任务,返回~0表示定时器已打开或不存在
|
||||
*/
|
||||
uint8_t st_init(uint8_t n, st_mode st_m, uint32_t cmr_v);
|
||||
|
||||
|
||||
/*
|
||||
* st_close() - 关闭一个给定编号的模拟定时器
|
||||
* @n: 定时器编号
|
||||
*/
|
||||
void st_close(uint8_t n);
|
||||
|
||||
/*
|
||||
* st_tcf() - 查询tcf位返回
|
||||
* @n: 定时器编号
|
||||
*
|
||||
* 这个函数在没有过比较值的时候返回0,已过比较值得时候返回1,对于不存在或未打开
|
||||
*/
|
||||
uint8_t st_tcf(uint8_t n);
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* st_tov() - 查询tov位返回
|
||||
* @n: 定时器编号
|
||||
*
|
||||
* 这个函数在没有溢出时返回0,没有溢出时返回1,对于不存在或未打开
|
||||
*/
|
||||
uint8_t st_tov(uint8_t n);
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* st_count() - 查询当前模拟定时值
|
||||
* @n: 定时器编号
|
||||
*
|
||||
* 对于不存在或未打开
|
||||
*/
|
||||
uint32_t st_count(uint8_t n);
|
||||
|
||||
|
||||
|
||||
/********************ISR******************/
|
||||
|
||||
/*
|
||||
* st_base() -定时调用以模拟N路定时器
|
||||
*
|
||||
* 定时的最小间隔等于这个函数被调用的周期
|
||||
*/
|
||||
void st_base(void);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#endif /* SIMULAT_TIMER_H */
|
||||
/*
|
||||
* simulat_timer.h - 实现一个模拟定时器
|
||||
*/
|
||||
|
||||
#ifndef SIMULAT_TIMER_H
|
||||
#define SIMULAT_TIMER_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
/*
|
||||
* 模拟定时器的数量
|
||||
*/
|
||||
#define SIMULAT_TIMER_NUM 5
|
||||
|
||||
|
||||
|
||||
typedef struct st_register_
|
||||
{
|
||||
uint32_t count; // 定时器计数
|
||||
uint32_t cmr; // 比较寄存器
|
||||
struct
|
||||
{
|
||||
uint8_t ten : 1; // 使能标志
|
||||
uint8_t tov : 1; // 溢出标志
|
||||
uint8_t tfc : 1; // 自由运行标志
|
||||
uint8_t tcf : 1; // 比较标志
|
||||
} flag;
|
||||
} st_register;
|
||||
|
||||
|
||||
typedef enum st_mode_
|
||||
{
|
||||
COMPARE = 0,
|
||||
NO_COMPARE
|
||||
} st_mode;
|
||||
|
||||
|
||||
void st_base_init(void);
|
||||
uint8_t st_init(uint8_t n, st_mode st_m, uint32_t cmr_v);
|
||||
void st_close(uint8_t n);
|
||||
uint8_t st_tcf(uint8_t n);
|
||||
uint8_t st_tov(uint8_t n);
|
||||
uint32_t st_count(uint8_t n);
|
||||
void st_base(void);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#endif /* SIMULAT_TIMER_H */
|
||||
|
|
|
|||
|
|
@ -1,45 +1,42 @@
|
|||
/*
|
||||
* tft.h - tft½Ó¿Ú
|
||||
*/
|
||||
|
||||
|
||||
#ifndef TFT_H
|
||||
#define TFT_H
|
||||
|
||||
enum { PLAN_DATA_NUM = 19 };
|
||||
|
||||
typedef struct input_limit_
|
||||
{
|
||||
int16_t max;
|
||||
int16_t min;
|
||||
} input_limit;
|
||||
|
||||
void tft_init(void);
|
||||
|
||||
void tft_send_cmd(const char *cmd);
|
||||
|
||||
void tft_left(void);
|
||||
|
||||
void tft_right(void);
|
||||
|
||||
void tft_up(void);
|
||||
|
||||
void tft_down(void);
|
||||
|
||||
void tft_ok(void);
|
||||
|
||||
void tft_ret(void);
|
||||
|
||||
void tft_page_refresh(void);
|
||||
|
||||
int16_t *get_value_of_kvp(char *name, uint8_t objn);
|
||||
|
||||
input_limit tft_input_limit(char *name);
|
||||
|
||||
//const kv_pair *get_plan_data(uint8_t objn)[][PLAN_DATA_NUM];
|
||||
|
||||
void tft_to_plan_input(uint8_t objn);
|
||||
|
||||
|
||||
|
||||
#endif /* TFT_H */
|
||||
/*
|
||||
* tft.h - tft½Ó¿Ú
|
||||
*/
|
||||
|
||||
|
||||
#ifndef TFT_H
|
||||
#define TFT_H
|
||||
|
||||
|
||||
typedef struct input_limit_
|
||||
{
|
||||
int16_t max;
|
||||
int16_t min;
|
||||
} input_limit;
|
||||
|
||||
void tft_init(void);
|
||||
|
||||
void tft_send_cmd(const char *cmd);
|
||||
|
||||
void tft_left(void);
|
||||
|
||||
void tft_right(void);
|
||||
|
||||
void tft_up(void);
|
||||
|
||||
void tft_down(void);
|
||||
|
||||
void tft_ok(void);
|
||||
|
||||
void tft_ret(void);
|
||||
|
||||
void tft_page_refresh(void);
|
||||
|
||||
int16_t *get_value_of_kvp(char *name, uint8_t objn);
|
||||
|
||||
input_limit tft_input_limit(char *name);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#endif /* TFT_H */
|
||||
|
|
|
|||
55
plan_manage_main/src/app/include/tft_plan_internal.h
Normal file
55
plan_manage_main/src/app/include/tft_plan_internal.h
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
/*
|
||||
* tft_handle_internal.h - tft和plan_handle模块共同使用部分的内部声明
|
||||
*
|
||||
* 这个文件只应包含在tft和plan_handle模块
|
||||
*/
|
||||
|
||||
#ifndef TFT_PLAN_INTERNAL_H
|
||||
#define TFT_PLAN_INTERNAL_H
|
||||
|
||||
|
||||
#include "include/pm_time.h"
|
||||
|
||||
/*
|
||||
* plan_input - 表示计划输入的类型
|
||||
*
|
||||
*/
|
||||
typedef struct plan_input_
|
||||
{
|
||||
calendar_info bg_t; // 开始时间
|
||||
calendar_info ed_t; // 结束时间
|
||||
calendar_info pd_t; // 周期时长
|
||||
|
||||
uint8_t x_orient; // x方向值,未使用
|
||||
uint8_t y_orient; // y方向值,未使用
|
||||
uint8_t lg_r : 1; // 红灯
|
||||
uint8_t lg_b : 1; // 蓝灯
|
||||
uint8_t lg_uvb : 1; // uvb
|
||||
uint8_t water : 1; // 浇水,未使用
|
||||
uint8_t sw : 1; // 是否加入计划
|
||||
uint8_t cnt; // 完成次数
|
||||
} plan_input;
|
||||
|
||||
/*
|
||||
* kv_pair - 表示页面中的元素
|
||||
* key为存储元素ID的字符串,value为一个整形值,attr为元素属性
|
||||
*/
|
||||
typedef struct kv_pair_
|
||||
{
|
||||
char *key;
|
||||
int16_t value;
|
||||
int8_t attr;
|
||||
} kv_pair;
|
||||
|
||||
enum { PLAN_DATA_NUM = 19, OBJ_NUM = 8 };
|
||||
|
||||
extern plan_input plan_in[OBJ_NUM];
|
||||
|
||||
extern kv_pair kvp_obj_set[][PLAN_DATA_NUM];
|
||||
|
||||
|
||||
|
||||
void tft_to_plan_input(uint8_t objn);
|
||||
|
||||
|
||||
#endif /* TFT_PLAN_INTERNAL_H */
|
||||
28
plan_manage_main/src/app/include/water.h
Normal file
28
plan_manage_main/src/app/include/water.h
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
/*
|
||||
* water.h - ½½Ë®
|
||||
*/
|
||||
|
||||
|
||||
#ifndef WARER_H
|
||||
#define WARER_H
|
||||
|
||||
|
||||
#define MOISTURE_RATIO 0.1
|
||||
#define MOISTURE_MIN 1
|
||||
|
||||
|
||||
|
||||
|
||||
void water_init(void);
|
||||
uint16_t get_moisture(void);
|
||||
void water_ctr(uint16_t sv_moist);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#endif /* WARER_H */
|
||||
|
|
@ -1,352 +1,364 @@
|
|||
/*
|
||||
* key.c - 监测并采集按键的输入
|
||||
*
|
||||
* 共有四种输入,无键,单击,双击,长击。
|
||||
* 每10ms需要采集一次io状态。
|
||||
* 每增减一个按键,需要设置的有:
|
||||
*
|
||||
* key_type枚举类型中对应的枚举元素
|
||||
*
|
||||
* 对应的key_infomation变量
|
||||
*
|
||||
* key_init()函数中对应的初始化条目
|
||||
* key_detect()函数中对应检测条目
|
||||
* get_key_mean()函数中对应获取条目
|
||||
*/
|
||||
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include "include.h"
|
||||
|
||||
#include "include/key.h"
|
||||
#include "include/config.h"
|
||||
|
||||
|
||||
/*
|
||||
* key_state - 表示按键的状态
|
||||
* key_state_0 初始状态,或者表示第一次单击状态
|
||||
* key_state_1 消抖和确认状态,或表示第二次单击状态
|
||||
* key_state_2 按键计时状态
|
||||
* key_state_3 按键等待释放状态
|
||||
*/
|
||||
typedef enum key_state_
|
||||
{
|
||||
key_state_0 = 0, key_state_1, key_state_2, key_state_3
|
||||
} key_state;
|
||||
|
||||
/*
|
||||
* IO状态符号定义
|
||||
*/
|
||||
typedef enum state_sign_
|
||||
{
|
||||
INVALID = 0, VALID = 1
|
||||
} state_sign;
|
||||
|
||||
|
||||
typedef struct key_infomation_
|
||||
{
|
||||
key_state state_base;
|
||||
uint8_t time_base;
|
||||
|
||||
key_state state;
|
||||
uint8_t time;
|
||||
|
||||
key_mean key_m;
|
||||
|
||||
state_sign (*get_state_io)(void);
|
||||
} key_infomation;
|
||||
|
||||
|
||||
|
||||
state_sign get_state_up_key_io(void)
|
||||
{ return !gpio_get(UP_KEY_PINX) ? VALID : INVALID; }
|
||||
|
||||
state_sign get_state_down_key_io(void)
|
||||
{ return !gpio_get(DOWN_KEY_PINX) ? VALID : INVALID; }
|
||||
|
||||
state_sign get_state_ok_key_io(void)
|
||||
{ return !gpio_get(OK_KEY_PINX) ? VALID : INVALID; }
|
||||
|
||||
state_sign get_state_ret_key_io(void)
|
||||
{ return !gpio_get(RET_KEY_PINX) ? VALID : INVALID; }
|
||||
|
||||
state_sign get_state_ams_key_io(void)
|
||||
{ return !gpio_get(AMS_KEY_PINX) ? VALID : INVALID; }
|
||||
|
||||
state_sign get_state_wifi_key_io(void)
|
||||
{ return !gpio_get(WIFI_KEY_PINX) ? VALID : INVALID; }
|
||||
|
||||
state_sign get_state_mr_key_io(void)
|
||||
{ return !gpio_get(MR_KEY_PINX) ? VALID : INVALID; }
|
||||
|
||||
state_sign get_state_mb_key_io(void)
|
||||
{ return !gpio_get(MB_KEY_PINX) ? VALID : INVALID; }
|
||||
|
||||
state_sign get_state_muvb_key_io(void)
|
||||
{ return !gpio_get(MUVB_KEY_PINX) ? VALID : INVALID; }
|
||||
|
||||
|
||||
static volatile key_infomation
|
||||
up_key_info = { key_state_0, 0, key_state_0, 0, N_KEY, get_state_up_key_io },
|
||||
down_key_info = { key_state_0, 0, key_state_0, 0, N_KEY, get_state_down_key_io },
|
||||
ok_key_info = { key_state_0, 0, key_state_0, 0, N_KEY, get_state_ok_key_io },
|
||||
ret_key_info = { key_state_0, 0, key_state_0, 0, N_KEY, get_state_ret_key_io },
|
||||
ams_key_info = { key_state_0, 0, key_state_0, 0, N_KEY, get_state_ams_key_io },
|
||||
wifi_key_info = { key_state_0, 0, key_state_0, 0, N_KEY, get_state_wifi_key_io },
|
||||
mr_key_info = { key_state_0, 0, key_state_0, 0, N_KEY, get_state_mr_key_io },
|
||||
mb_key_info = { key_state_0, 0, key_state_0, 0, N_KEY, get_state_mb_key_io },
|
||||
muvb_key_info = { key_state_0, 0, key_state_0, 0, N_KEY, get_state_muvb_key_io };
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* key_init() - 按键模块初始化
|
||||
*
|
||||
*/
|
||||
void key_init(void)
|
||||
{
|
||||
gpio_Interrupt_init(UP_KEY_PINX, GPI_UP_PF, GPI_DISAB);
|
||||
gpio_Interrupt_init(DOWN_KEY_PINX, GPI_UP_PF, GPI_DISAB);
|
||||
gpio_Interrupt_init(OK_KEY_PINX, GPI_UP_PF, GPI_DISAB);
|
||||
gpio_Interrupt_init(RET_KEY_PINX, GPI_UP_PF, GPI_DISAB);
|
||||
gpio_Interrupt_init(AMS_KEY_PINX, GPI_UP_PF, GPI_DISAB);
|
||||
gpio_Interrupt_init(WIFI_KEY_PINX, GPI_UP_PF, GPI_DISAB);
|
||||
gpio_Interrupt_init(MR_KEY_PINX, GPI_UP_PF, GPI_DISAB);
|
||||
gpio_Interrupt_init(MB_KEY_PINX, GPI_UP_PF, GPI_DISAB);
|
||||
gpio_Interrupt_init(MUVB_KEY_PINX, GPI_UP_PF, GPI_DISAB);
|
||||
|
||||
|
||||
/*
|
||||
* 初始化定时器为10ms定时中断,用于按键检测的定时执行
|
||||
*/
|
||||
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* key_read_base - 按键基础读取函数,
|
||||
* @key_info: 要检测按键的所需信息,必须在是全局变量区中存储的
|
||||
*
|
||||
* 这个函数返回 N_KEY,S_KEY,L_KEY三种情况
|
||||
*/
|
||||
key_mean key_read_base(volatile key_infomation *key_info)
|
||||
{
|
||||
state_sign key_state_io;
|
||||
key_mean key_m = N_KEY;
|
||||
|
||||
key_state_io = (*((*key_info).get_state_io))(); // 获取io状态
|
||||
switch((*key_info).state_base)
|
||||
{
|
||||
case key_state_0:
|
||||
if (key_state_io == VALID)
|
||||
{
|
||||
(*key_info).state_base = key_state_1; // 按键按下后首先进入消抖和按键确
|
||||
// 认状态
|
||||
}
|
||||
break;
|
||||
case key_state_1:
|
||||
if (key_state_io == VALID)
|
||||
{
|
||||
(*key_info).time_base = 0;
|
||||
(*key_info).state_base = key_state_2; // 按键如果仍然按下,则消抖完成,
|
||||
// 状态转换,此时返回无按键事件
|
||||
}
|
||||
else
|
||||
{
|
||||
(*key_info).state_base = key_state_0;
|
||||
}
|
||||
break;
|
||||
case key_state_2:
|
||||
if (key_state_io == INVALID)
|
||||
{
|
||||
key_m = S_KEY; // 此时按键释放说明为一次短操作
|
||||
(*key_info).state_base = key_state_0;
|
||||
}
|
||||
else if (++(*key_info).time_base > 100) // 继续按下,计时加10ms,即本函
|
||||
// 数调用周期
|
||||
{
|
||||
key_m = L_KEY; // 按下时间大于1s,返回 长按键
|
||||
(*key_info).state_base = key_state_3; // 转换为等待按键释放状态
|
||||
}
|
||||
break;
|
||||
case key_state_3:
|
||||
if (key_state_io == INVALID)
|
||||
{
|
||||
(*key_info).state_base = key_state_0; // 按键此时已释放,转换为初始状态
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return key_m;
|
||||
}
|
||||
|
||||
/*
|
||||
* key_read - 按键读取函数
|
||||
* @key_info: 要检测的按键所需的信息,必须是在全局变量区中存储的
|
||||
*
|
||||
* 这个函数会返回N_KEY,S_KEY,D_KEY,L_KEY四情况
|
||||
*/
|
||||
key_mean key_read(volatile key_infomation *key_info)
|
||||
{
|
||||
key_mean key_m = N_KEY,
|
||||
key_m_temp;
|
||||
|
||||
key_m_temp = key_read_base(key_info);
|
||||
switch((*key_info).state)
|
||||
{
|
||||
case key_state_0:
|
||||
if (key_m_temp == S_KEY)
|
||||
{
|
||||
(*key_info).time = 0; // 第一次单击状态,仍然返回无键,到下个周期
|
||||
// 判断是否出现双击
|
||||
(*key_info).state = key_state_1;
|
||||
}
|
||||
else
|
||||
{
|
||||
key_m = key_m_temp; // 对于非单击,返回原事件
|
||||
}
|
||||
break;
|
||||
case key_state_1:
|
||||
if (key_m_temp == S_KEY) // 又一次单击,间隔 < 300ms
|
||||
{
|
||||
key_m = D_KEY;
|
||||
(*key_info).state = key_state_0;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (++(*key_info).time > 30)
|
||||
{
|
||||
key_m = S_KEY; // 300ms内没有再出现单击事件,则返回上一次单
|
||||
// 击事件
|
||||
(*key_info).state = key_state_0;
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return key_m;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* key_detect() - 检测按键
|
||||
*
|
||||
* 这个函数仅在N_KEY状态时记录按键信息,N_KEY是在应用程序获取按键信息的时候同时被设
|
||||
* 置的,这样做的目的是保证采集到的按键一定会被执行
|
||||
* 要求每隔10ms调用一次
|
||||
*/
|
||||
void key_detect(void)
|
||||
{
|
||||
key_mean key_m;
|
||||
|
||||
if ((key_m = key_read(&up_key_info)) != N_KEY)
|
||||
{
|
||||
up_key_info.key_m = key_m;
|
||||
}
|
||||
if ((key_m = key_read(&down_key_info)) != N_KEY)
|
||||
{
|
||||
down_key_info.key_m = key_m;
|
||||
}
|
||||
if ((key_m = key_read(&ok_key_info)) != N_KEY)
|
||||
{
|
||||
ok_key_info.key_m = key_m;
|
||||
}
|
||||
if ((key_m = key_read(&ret_key_info)) != N_KEY)
|
||||
{
|
||||
ret_key_info.key_m = key_m;
|
||||
}
|
||||
if ((key_m = key_read(&ams_key_info)) != N_KEY)
|
||||
{
|
||||
ams_key_info.key_m = key_m;
|
||||
}
|
||||
if ((key_m = key_read(&wifi_key_info)) != N_KEY)
|
||||
{
|
||||
wifi_key_info.key_m = key_m;
|
||||
}
|
||||
if ((key_m = key_read(&mr_key_info)) != N_KEY)
|
||||
{
|
||||
mr_key_info.key_m = key_m;
|
||||
}
|
||||
if ((key_m = key_read(&mb_key_info)) != N_KEY)
|
||||
{
|
||||
mb_key_info.key_m = key_m;
|
||||
}
|
||||
if ((key_m = key_read(&muvb_key_info)) != N_KEY)
|
||||
{
|
||||
muvb_key_info.key_m = key_m;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* get_key_mean() - 得到指定的按键信息
|
||||
*
|
||||
* 这个函数中在取得信息后会将对应按键重新标记为N_KEY
|
||||
*/
|
||||
key_mean get_key_mean(key_type key_t)
|
||||
{
|
||||
key_mean key_m;
|
||||
|
||||
switch (key_t)
|
||||
{
|
||||
case UP_KEY:
|
||||
key_m = up_key_info.key_m;
|
||||
up_key_info.key_m = N_KEY;
|
||||
break;
|
||||
case DOWN_KEY:
|
||||
key_m = down_key_info.key_m;
|
||||
down_key_info.key_m = N_KEY;
|
||||
break;
|
||||
case OK_KEY:
|
||||
key_m = ok_key_info.key_m;
|
||||
ok_key_info.key_m = N_KEY;
|
||||
break;
|
||||
case RET_KEY:
|
||||
key_m = ret_key_info.key_m;
|
||||
ret_key_info.key_m = N_KEY;
|
||||
break;
|
||||
case AMS_KEY:
|
||||
key_m = ams_key_info.key_m;
|
||||
ams_key_info.key_m = N_KEY;
|
||||
break;
|
||||
case WIFI_KEY:
|
||||
key_m = wifi_key_info.key_m;
|
||||
wifi_key_info.key_m = N_KEY;
|
||||
break;
|
||||
case MR_KEY:
|
||||
key_m = mr_key_info.key_m;
|
||||
mr_key_info.key_m = N_KEY;
|
||||
break;
|
||||
case MB_KEY:
|
||||
key_m = mb_key_info.key_m;
|
||||
mb_key_info.key_m = N_KEY;
|
||||
break;
|
||||
case MUVB_KEY:
|
||||
key_m = muvb_key_info.key_m;
|
||||
muvb_key_info.key_m = N_KEY;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return key_m;
|
||||
}
|
||||
|
||||
|
||||
void clear_key_m(void)
|
||||
{
|
||||
down_key_info.key_m = N_KEY;
|
||||
up_key_info.key_m = N_KEY;
|
||||
ok_key_info.key_m = N_KEY;
|
||||
ret_key_info.key_m = N_KEY;
|
||||
ams_key_info.key_m = N_KEY;
|
||||
wifi_key_info.key_m = N_KEY;
|
||||
mr_key_info.key_m = N_KEY;
|
||||
mb_key_info.key_m = N_KEY;
|
||||
muvb_key_info.key_m = N_KEY;
|
||||
}
|
||||
/*
|
||||
* key.c - 监测并采集按键的输入
|
||||
*
|
||||
* 共有四种输入,无键,单击,双击,长击。
|
||||
* 每10ms需要采集一次io状态。
|
||||
* 每增减一个按键,需要设置的有:
|
||||
*
|
||||
* key_type枚举类型中对应的枚举元素
|
||||
*
|
||||
* 对应的key_infomation变量
|
||||
*
|
||||
* key_init()函数中对应的初始化条目
|
||||
* key_detect()函数中对应检测条目
|
||||
* get_key_mean()函数中对应获取条目
|
||||
*/
|
||||
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include "include.h"
|
||||
|
||||
#include "include/key.h"
|
||||
#include "include/config.h"
|
||||
|
||||
|
||||
/*
|
||||
* key_state - 表示按键的状态
|
||||
* key_state_0 初始状态,或者表示第一次单击状态
|
||||
* key_state_1 消抖和确认状态,或表示第二次单击状态
|
||||
* key_state_2 按键计时状态
|
||||
* key_state_3 按键等待释放状态
|
||||
*/
|
||||
typedef enum key_state_
|
||||
{
|
||||
key_state_0 = 0, key_state_1, key_state_2, key_state_3
|
||||
} key_state;
|
||||
|
||||
/*
|
||||
* IO状态符号定义
|
||||
*/
|
||||
typedef enum state_sign_
|
||||
{
|
||||
INVALID = 0, VALID = 1
|
||||
} state_sign;
|
||||
|
||||
|
||||
/*
|
||||
* key_infomation - 表示一个按键的信息
|
||||
* state_base和time_base在key_read_base函数中使用的状态变量
|
||||
* state和time在key_read函数中使用的状态变量
|
||||
* key_m保存当前按键读取的结果
|
||||
* get_state_io变量指向获取按键对应输入io状态的函数
|
||||
*/
|
||||
typedef struct key_infomation_
|
||||
{
|
||||
key_state state_base;
|
||||
uint8_t time_base;
|
||||
|
||||
key_state state;
|
||||
uint8_t time;
|
||||
|
||||
key_mean key_m;
|
||||
|
||||
state_sign (*get_state_io)(void);
|
||||
} key_infomation;
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* 下边这几个函数定义的是获取按键io状态的函数
|
||||
*/
|
||||
state_sign get_state_up_key_io(void)
|
||||
{ return !gpio_get(UP_KEY_PINX) ? VALID : INVALID; }
|
||||
|
||||
state_sign get_state_down_key_io(void)
|
||||
{ return !gpio_get(DOWN_KEY_PINX) ? VALID : INVALID; }
|
||||
|
||||
state_sign get_state_ok_key_io(void)
|
||||
{ return !gpio_get(OK_KEY_PINX) ? VALID : INVALID; }
|
||||
|
||||
state_sign get_state_ret_key_io(void)
|
||||
{ return !gpio_get(RET_KEY_PINX) ? VALID : INVALID; }
|
||||
|
||||
state_sign get_state_ams_key_io(void)
|
||||
{ return !gpio_get(AMS_KEY_PINX) ? VALID : INVALID; }
|
||||
|
||||
state_sign get_state_wifi_key_io(void)
|
||||
{ return !gpio_get(WIFI_KEY_PINX) ? VALID : INVALID; }
|
||||
|
||||
state_sign get_state_mr_key_io(void)
|
||||
{ return !gpio_get(MR_KEY_PINX) ? VALID : INVALID; }
|
||||
|
||||
state_sign get_state_mb_key_io(void)
|
||||
{ return !gpio_get(MB_KEY_PINX) ? VALID : INVALID; }
|
||||
|
||||
state_sign get_state_muvb_key_io(void)
|
||||
{ return !gpio_get(MUVB_KEY_PINX) ? VALID : INVALID; }
|
||||
|
||||
|
||||
// 定义按键变量并初始化
|
||||
static volatile key_infomation
|
||||
up_key_info = { key_state_0, 0, key_state_0, 0, N_KEY, get_state_up_key_io },
|
||||
down_key_info = { key_state_0, 0, key_state_0, 0, N_KEY, get_state_down_key_io },
|
||||
ok_key_info = { key_state_0, 0, key_state_0, 0, N_KEY, get_state_ok_key_io },
|
||||
ret_key_info = { key_state_0, 0, key_state_0, 0, N_KEY, get_state_ret_key_io },
|
||||
ams_key_info = { key_state_0, 0, key_state_0, 0, N_KEY, get_state_ams_key_io },
|
||||
wifi_key_info = { key_state_0, 0, key_state_0, 0, N_KEY, get_state_wifi_key_io },
|
||||
mr_key_info = { key_state_0, 0, key_state_0, 0, N_KEY, get_state_mr_key_io },
|
||||
mb_key_info = { key_state_0, 0, key_state_0, 0, N_KEY, get_state_mb_key_io },
|
||||
muvb_key_info = { key_state_0, 0, key_state_0, 0, N_KEY, get_state_muvb_key_io };
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* key_init() - 按键模块初始化
|
||||
*
|
||||
*/
|
||||
void key_init(void)
|
||||
{
|
||||
gpio_Interrupt_init(UP_KEY_PINX, GPI_UP_PF, GPI_DISAB);
|
||||
gpio_Interrupt_init(DOWN_KEY_PINX, GPI_UP_PF, GPI_DISAB);
|
||||
gpio_Interrupt_init(OK_KEY_PINX, GPI_UP_PF, GPI_DISAB);
|
||||
gpio_Interrupt_init(RET_KEY_PINX, GPI_UP_PF, GPI_DISAB);
|
||||
gpio_Interrupt_init(AMS_KEY_PINX, GPI_UP_PF, GPI_DISAB);
|
||||
gpio_Interrupt_init(WIFI_KEY_PINX, GPI_UP_PF, GPI_DISAB);
|
||||
gpio_Interrupt_init(MR_KEY_PINX, GPI_UP_PF, GPI_DISAB);
|
||||
gpio_Interrupt_init(MB_KEY_PINX, GPI_UP_PF, GPI_DISAB);
|
||||
gpio_Interrupt_init(MUVB_KEY_PINX, GPI_UP_PF, GPI_DISAB);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* key_read_base - 按键基础读取函数,
|
||||
* @key_info: 要检测按键的所需信息,必须在是全局变量区中存储的
|
||||
*
|
||||
* 这个函数返回 N_KEY,S_KEY,L_KEY三种情况
|
||||
*/
|
||||
key_mean key_read_base(volatile key_infomation *key_info)
|
||||
{
|
||||
state_sign key_state_io;
|
||||
key_mean key_m = N_KEY;
|
||||
|
||||
// 获取io状态
|
||||
key_state_io = (*(key_info->get_state_io))();
|
||||
switch(key_info->state_base)
|
||||
{
|
||||
case key_state_0:
|
||||
if (key_state_io == VALID)
|
||||
{
|
||||
// 按键按下后首先进入消抖和按键确认状态
|
||||
key_info->state_base = key_state_1;
|
||||
}
|
||||
break;
|
||||
case key_state_1:
|
||||
if (key_state_io == VALID)
|
||||
{
|
||||
// 按键如果仍然按下,则消抖完成
|
||||
// 进行状态转换,并且返回无按键事件
|
||||
key_info->time_base = 0;
|
||||
key_info->state_base = key_state_2;
|
||||
}
|
||||
else
|
||||
{
|
||||
key_info->state_base = key_state_0;
|
||||
}
|
||||
break;
|
||||
case key_state_2:
|
||||
if (key_state_io == INVALID)
|
||||
{
|
||||
// 此时按键释放说明为一次短操作
|
||||
key_m = S_KEY;
|
||||
key_info->state_base = key_state_0;
|
||||
}
|
||||
// 继续按下,计时加10ms,即本函数调用周期
|
||||
else if (++key_info->time_base > 100)
|
||||
{
|
||||
// 按下时间大于1s,返回长按键,并且转换为等待按键释放状态
|
||||
key_m = L_KEY;
|
||||
key_info->state_base = key_state_3;
|
||||
}
|
||||
break;
|
||||
case key_state_3:
|
||||
if (key_state_io == INVALID)
|
||||
{
|
||||
// 按键此时已释放,转换为初始状态
|
||||
key_info->state_base = key_state_0;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return key_m;
|
||||
}
|
||||
|
||||
/**
|
||||
* key_read - 按键读取函数
|
||||
* @key_info: 要检测的按键所需的信息,必须是在全局变量区中存储的
|
||||
*
|
||||
* 这个函数会返回N_KEY,S_KEY,D_KEY,L_KEY四情况
|
||||
*/
|
||||
key_mean key_read(volatile key_infomation *key_info)
|
||||
{
|
||||
key_mean key_m = N_KEY,
|
||||
key_m_temp;
|
||||
|
||||
key_m_temp = key_read_base(key_info);
|
||||
switch(key_info->state)
|
||||
{
|
||||
case key_state_0:
|
||||
if (key_m_temp == S_KEY)
|
||||
{
|
||||
// 第一次单击状态,仍然返回无键,到下个周期判断是否出现双击
|
||||
key_info->time = 0;
|
||||
key_info->state = key_state_1;
|
||||
}
|
||||
else
|
||||
{
|
||||
// 对于非单击,返回原事件
|
||||
key_m = key_m_temp;
|
||||
}
|
||||
break;
|
||||
case key_state_1:
|
||||
// 又一次单击,间隔 < 300ms
|
||||
if (key_m_temp == S_KEY)
|
||||
{
|
||||
key_m = D_KEY;
|
||||
key_info->state = key_state_0;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (++key_info->time > 30)
|
||||
{
|
||||
// 300ms内没有再出现单击事件,则返回上一次单击事件
|
||||
key_m = S_KEY;
|
||||
key_info->state = key_state_0;
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return key_m;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* key_detect() - 检测按键
|
||||
*
|
||||
* 这个函数仅在N_KEY状态时记录按键信息,N_KEY是在应用程序获取按键信息的时候同时
|
||||
* 被设置的,这样做的目的是保证采集到的按键一定会被执行
|
||||
* 要求每隔10ms调用一次
|
||||
*/
|
||||
void key_detect(void)
|
||||
{
|
||||
key_mean key_m;
|
||||
|
||||
if ((key_m = key_read(&up_key_info)) != N_KEY)
|
||||
{
|
||||
up_key_info.key_m = key_m;
|
||||
}
|
||||
if ((key_m = key_read(&down_key_info)) != N_KEY)
|
||||
{
|
||||
down_key_info.key_m = key_m;
|
||||
}
|
||||
if ((key_m = key_read(&ok_key_info)) != N_KEY)
|
||||
{
|
||||
ok_key_info.key_m = key_m;
|
||||
}
|
||||
if ((key_m = key_read(&ret_key_info)) != N_KEY)
|
||||
{
|
||||
ret_key_info.key_m = key_m;
|
||||
}
|
||||
if ((key_m = key_read(&ams_key_info)) != N_KEY)
|
||||
{
|
||||
ams_key_info.key_m = key_m;
|
||||
}
|
||||
if ((key_m = key_read(&wifi_key_info)) != N_KEY)
|
||||
{
|
||||
wifi_key_info.key_m = key_m;
|
||||
}
|
||||
if ((key_m = key_read(&mr_key_info)) != N_KEY)
|
||||
{
|
||||
mr_key_info.key_m = key_m;
|
||||
}
|
||||
if ((key_m = key_read(&mb_key_info)) != N_KEY)
|
||||
{
|
||||
mb_key_info.key_m = key_m;
|
||||
}
|
||||
if ((key_m = key_read(&muvb_key_info)) != N_KEY)
|
||||
{
|
||||
muvb_key_info.key_m = key_m;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* get_key_mean() - 得到指定的按键信息
|
||||
*
|
||||
* 这个函数中在取得信息后会将对应按键重新标记为N_KEY
|
||||
*/
|
||||
key_mean get_key_mean(key_type key_t)
|
||||
{
|
||||
key_mean key_m;
|
||||
|
||||
switch (key_t)
|
||||
{
|
||||
case UP_KEY:
|
||||
key_m = up_key_info.key_m;
|
||||
up_key_info.key_m = N_KEY;
|
||||
break;
|
||||
case DOWN_KEY:
|
||||
key_m = down_key_info.key_m;
|
||||
down_key_info.key_m = N_KEY;
|
||||
break;
|
||||
case OK_KEY:
|
||||
key_m = ok_key_info.key_m;
|
||||
ok_key_info.key_m = N_KEY;
|
||||
break;
|
||||
case RET_KEY:
|
||||
key_m = ret_key_info.key_m;
|
||||
ret_key_info.key_m = N_KEY;
|
||||
break;
|
||||
case AMS_KEY:
|
||||
key_m = ams_key_info.key_m;
|
||||
ams_key_info.key_m = N_KEY;
|
||||
break;
|
||||
case WIFI_KEY:
|
||||
key_m = wifi_key_info.key_m;
|
||||
wifi_key_info.key_m = N_KEY;
|
||||
break;
|
||||
case MR_KEY:
|
||||
key_m = mr_key_info.key_m;
|
||||
mr_key_info.key_m = N_KEY;
|
||||
break;
|
||||
case MB_KEY:
|
||||
key_m = mb_key_info.key_m;
|
||||
mb_key_info.key_m = N_KEY;
|
||||
break;
|
||||
case MUVB_KEY:
|
||||
key_m = muvb_key_info.key_m;
|
||||
muvb_key_info.key_m = N_KEY;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return key_m;
|
||||
}
|
||||
|
||||
|
||||
void clear_key_m(void)
|
||||
{
|
||||
down_key_info.key_m = N_KEY;
|
||||
up_key_info.key_m = N_KEY;
|
||||
ok_key_info.key_m = N_KEY;
|
||||
ret_key_info.key_m = N_KEY;
|
||||
ams_key_info.key_m = N_KEY;
|
||||
wifi_key_info.key_m = N_KEY;
|
||||
mr_key_info.key_m = N_KEY;
|
||||
mb_key_info.key_m = N_KEY;
|
||||
muvb_key_info.key_m = N_KEY;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,95 +1,97 @@
|
|||
/*
|
||||
* knob.c - 提供旋钮输入的接口函数
|
||||
*
|
||||
* 使用的是一个数字旋钮,利用计数来采集输入
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include "include.h"
|
||||
|
||||
|
||||
#include "include/knob.h"
|
||||
#include "include/config.h"
|
||||
|
||||
|
||||
|
||||
static volatile int32_t knob_val;
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* knob_init() - 为旋钮输入而执行的初始化
|
||||
*
|
||||
* 旋钮的检测初始化后默认是禁止的
|
||||
*/
|
||||
void knob_init(void)
|
||||
{
|
||||
gpio_Interrupt_init(KNOB_A_PINX, GPI_UP_PF, GPI_INP_EITH);
|
||||
gpio_Interrupt_init(KNOB_B_PINX, GPI_UP_PF, GPI_DISAB);
|
||||
// gpio_Interrupt_init(KNOB_KEY_PINX, GPI_UP_PF, GPI_INP_EITH);
|
||||
disable_irq(PortD_irq_no);
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* knob_enable() - 使能旋钮脉冲检测
|
||||
*/
|
||||
void knob_enable(void)
|
||||
{
|
||||
enable_irq(PortD_irq_no);
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
* knob_disable() - 禁止旋钮脉冲检测
|
||||
*/
|
||||
void knob_disable(void)
|
||||
{
|
||||
disable_irq(PortA_irq_no);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* knob_clear() - 清除旋钮值
|
||||
*/
|
||||
void knob_clear(void)
|
||||
{
|
||||
knob_val = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
* get_knob_val() - 返回旋钮值
|
||||
*/
|
||||
int32_t get_knob_val(void)
|
||||
{
|
||||
return knob_val;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* knob_detect() - 旋钮检测
|
||||
*
|
||||
* 这个函数需要在旋钮编码器的A相输出发生沿跳变时调用,即上下沿中断时
|
||||
*/
|
||||
void knob_detect(void)
|
||||
{
|
||||
uint8_t knob_A, knob_B;
|
||||
|
||||
knob_A = gpio_get(KNOB_A_PINX);
|
||||
knob_B = gpio_get(KNOB_B_PINX);
|
||||
if ((knob_A == 0 && knob_B == 1) || (knob_A == 1 && knob_B == 0))
|
||||
{
|
||||
knob_val++;
|
||||
}
|
||||
else // 即if ((knob_A == 0 && knob_B == 0) || (knob_A == 1 && knob_B == 1))
|
||||
{
|
||||
knob_val--;
|
||||
}
|
||||
return;
|
||||
}
|
||||
/*
|
||||
* knob.c - 提供旋钮输入的接口函数
|
||||
*
|
||||
* 使用的是一个数字旋钮,利用计数来采集输入
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include "include.h"
|
||||
#include "arm_cm0.h"
|
||||
#include "vectors.h"
|
||||
|
||||
#include "include/config.h"
|
||||
#include "include/knob.h"
|
||||
|
||||
|
||||
|
||||
static volatile int32_t knob_val;
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* knob_init() - 为旋钮输入而执行的初始化
|
||||
*
|
||||
* 旋钮的检测初始化后默认是禁止的
|
||||
*/
|
||||
void knob_init(void)
|
||||
{
|
||||
gpio_Interrupt_init(KNOB_A_PINX, GPI_UP_PF, GPI_INP_EITH);
|
||||
gpio_Interrupt_init(KNOB_B_PINX, GPI_UP_PF, GPI_DISAB);
|
||||
// gpio_Interrupt_init(KNOB_KEY_PINX, GPI_UP_PF, GPI_INP_EITH);
|
||||
disable_irq(PortD_irq_no);
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* knob_enable() - 使能旋钮脉冲检测
|
||||
*/
|
||||
void knob_enable(void)
|
||||
{
|
||||
enable_irq(PortD_irq_no);
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
* knob_disable() - 禁止旋钮脉冲检测
|
||||
*/
|
||||
void knob_disable(void)
|
||||
{
|
||||
disable_irq(PortD_irq_no);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* knob_clear() - 清除旋钮值
|
||||
*/
|
||||
void knob_clear(void)
|
||||
{
|
||||
knob_val = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
* get_knob_val() - 返回旋钮值
|
||||
*/
|
||||
int32_t get_knob_val(void)
|
||||
{
|
||||
return knob_val;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* knob_detect() - 旋钮检测
|
||||
*
|
||||
* 这个函数需要在旋钮编码器的A相输出发生沿跳变时调用,即上下沿中断时
|
||||
*/
|
||||
void knob_detect(void)
|
||||
{
|
||||
uint8_t knob_A, knob_B;
|
||||
|
||||
knob_A = gpio_get(KNOB_A_PINX);
|
||||
knob_B = gpio_get(KNOB_B_PINX);
|
||||
if ((knob_A == 0 && knob_B == 1) || (knob_A == 1 && knob_B == 0))
|
||||
{
|
||||
knob_val++;
|
||||
}
|
||||
// 即if ((knob_A == 0 && knob_B == 0) || (knob_A == 1 && knob_B == 1))
|
||||
else
|
||||
{
|
||||
knob_val--;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,163 +1,108 @@
|
|||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "common.h"
|
||||
|
||||
#include "include/pm_init.h"
|
||||
#include "include/simulat_timer.h"
|
||||
#include "include/key.h"
|
||||
#include "include/knob.h"
|
||||
#include "include/tft.h"
|
||||
#include "include/config.h"
|
||||
#include "include/pm_time.h"
|
||||
#include "include/plan_handle.h"
|
||||
#include "include/pm_flash.h"
|
||||
|
||||
|
||||
|
||||
void main(void)
|
||||
{
|
||||
|
||||
pm_init();
|
||||
|
||||
st_init(0, COMPARE, 10); // 作为按键扫描函数的定时使用,在simulat_timer.c的底层中使用
|
||||
st_init(1, COMPARE, 500);
|
||||
|
||||
st_init(2, COMPARE, 1);
|
||||
st_init(3, COMPARE, 500);
|
||||
|
||||
calendar_info sys_cal = {
|
||||
0, 1, 2, 3, 4, 2012, 0, 1
|
||||
};
|
||||
ds1302_set_time(&sys_cal);
|
||||
|
||||
knob_enable();
|
||||
|
||||
while (1)
|
||||
{
|
||||
/*
|
||||
* 时间以及计划处理
|
||||
*/
|
||||
if (st_tcf(1) == 1)
|
||||
{
|
||||
maintain_system_time();
|
||||
|
||||
calendar_info st= get_system_time();
|
||||
*get_value_of_kvp("st_y", 0) = st.year;
|
||||
*get_value_of_kvp("st_mo", 0) = st.month;
|
||||
*get_value_of_kvp("st_d", 0) = st.mday;
|
||||
*get_value_of_kvp("st_h", 0) = st.hour;
|
||||
*get_value_of_kvp("st_mi", 0) = st.min;
|
||||
*get_value_of_kvp("st_s", 0) = st.sec;
|
||||
tft_page_refresh();
|
||||
|
||||
plan_handle();
|
||||
}
|
||||
|
||||
/*
|
||||
* 调试相关,较短时间响应
|
||||
*/
|
||||
if (st_tcf(2) == 1)
|
||||
{
|
||||
static int32_t log = 0, knob_v;
|
||||
if (log != (knob_v = get_knob_val()))
|
||||
{
|
||||
printf("knob = %d\n", knob_v);
|
||||
log = knob_v;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* 调试相关,较长时间响应
|
||||
*/
|
||||
if (st_tcf(3) == 1)
|
||||
{
|
||||
calendar_info st = get_system_time();
|
||||
|
||||
printf("%d 年 %d 月 %d 星期 %d 日 %d 时 %d 分 %d 秒\n",
|
||||
st.year, st.month, st.wday, st.mday,
|
||||
st.hour, st.min, st.sec);
|
||||
printf("sec = %d\n", calendar_to_sec(&st));
|
||||
}
|
||||
|
||||
/*
|
||||
* 按键功能注册部分
|
||||
*/
|
||||
switch (get_key_mean(UP_KEY))
|
||||
{
|
||||
case N_KEY:
|
||||
//printf("无键\n");
|
||||
break;
|
||||
case S_KEY:
|
||||
printf("单击\n");
|
||||
tft_left();
|
||||
break;
|
||||
case D_KEY:
|
||||
printf("双击\n");
|
||||
tft_up();
|
||||
break;
|
||||
case L_KEY:
|
||||
printf("长按\n");
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
switch (get_key_mean(DOWN_KEY))
|
||||
{
|
||||
case N_KEY:
|
||||
//printf("无键\n");
|
||||
break;
|
||||
case S_KEY:
|
||||
printf("单击\n");
|
||||
tft_right();
|
||||
break;
|
||||
case D_KEY:
|
||||
printf("双击\n");
|
||||
tft_down();
|
||||
break;
|
||||
case L_KEY:
|
||||
printf("长按\n");
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
switch (get_key_mean(OK_KEY))
|
||||
{
|
||||
case N_KEY:
|
||||
//printf("无键\n");
|
||||
break;
|
||||
case S_KEY:
|
||||
printf("单击\n");
|
||||
tft_ok();
|
||||
break;
|
||||
case D_KEY:
|
||||
printf("双击\n");
|
||||
break;
|
||||
case L_KEY:
|
||||
printf("长按\n");
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
switch (get_key_mean(RET_KEY))
|
||||
{
|
||||
case N_KEY:
|
||||
//printf("无键\n");
|
||||
break;
|
||||
case S_KEY:
|
||||
printf("单击\n");
|
||||
tft_ret();
|
||||
break;
|
||||
case D_KEY:
|
||||
printf("双击\n");
|
||||
break;
|
||||
case L_KEY:
|
||||
printf("长按\n");
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "common.h"
|
||||
|
||||
#include "include/pm_init.h"
|
||||
#include "include/simulat_timer.h"
|
||||
#include "include/key.h"
|
||||
#include "include/knob.h"
|
||||
#include "include/tft.h"
|
||||
#include "include/config.h"
|
||||
#include "include/pm_time.h"
|
||||
#include "include/plan_handle.h"
|
||||
#include "include/pm_flash.h"
|
||||
#include "include/orient.h"
|
||||
|
||||
|
||||
|
||||
void main(void)
|
||||
{
|
||||
|
||||
printf("\n\n\n程序开始执行!!!\n\n\n");
|
||||
enter_critical();
|
||||
pm_init();
|
||||
exit_critical();
|
||||
/*
|
||||
* 作为按键扫描函数的定时使用,在simulat_timer.c的底层中使用
|
||||
*/
|
||||
st_init(0, COMPARE, 10);
|
||||
st_init(1, COMPARE, 100);
|
||||
|
||||
#ifdef PM_DEBUG
|
||||
st_init(2, COMPARE, 1);
|
||||
st_init(3, COMPARE, 500);
|
||||
#endif /* PM_DEBUG */
|
||||
|
||||
knob_enable();
|
||||
|
||||
|
||||
while (1)
|
||||
{
|
||||
/*
|
||||
* 时间以及计划处理
|
||||
*/
|
||||
if (st_tcf(1) == 1)
|
||||
{
|
||||
maintain_system_time();
|
||||
|
||||
calendar_info st= get_system_time();
|
||||
*get_value_of_kvp("st_y", 0) = st.year;
|
||||
*get_value_of_kvp("st_mo", 0) = st.month;
|
||||
*get_value_of_kvp("st_d", 0) = st.mday;
|
||||
*get_value_of_kvp("st_h", 0) = st.hour;
|
||||
*get_value_of_kvp("st_mi", 0) = st.min;
|
||||
*get_value_of_kvp("st_s", 0) = st.sec;
|
||||
tft_page_refresh();
|
||||
|
||||
plan_handle();
|
||||
}
|
||||
|
||||
#ifdef PM_DEBUG
|
||||
/*
|
||||
* 调试相关,较短时间响应
|
||||
*/
|
||||
if (st_tcf(2) == 1)
|
||||
{
|
||||
static int32_t log = 0, knob_v;
|
||||
if (log != (knob_v = get_knob_val()))
|
||||
{
|
||||
printf("knob = %d\n", knob_v);
|
||||
log = knob_v;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* 调试相关,较长时间响应
|
||||
*/
|
||||
if (st_tcf(3) == 1)
|
||||
{
|
||||
calendar_info st = get_system_time();
|
||||
|
||||
printf("%d 年 %d 月 %d 星期 %d 日 %d 时 %d 分 %d 秒\n",
|
||||
st.year, st.month, st.wday, st.mday,
|
||||
st.hour, st.min, st.sec);
|
||||
printf("sec = %d\n", calendar_to_sec(&st));
|
||||
|
||||
static uint8_t lg_test;
|
||||
lg_test = !lg_test;
|
||||
gpio_set(LG1_PINX, 0);
|
||||
gpio_set(LG2_PINX, 0);
|
||||
gpio_set(LG3_PINX, 0);
|
||||
|
||||
if (gpio_get(AMS_KEY_PINX))
|
||||
{
|
||||
printf("计划!!!!!!\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("手动!!!!!!\n");
|
||||
}
|
||||
}
|
||||
#endif /* PM_DEBUG */
|
||||
|
||||
key_func();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
|
|
|||
120
plan_manage_main/src/app/orient.c
Normal file
120
plan_manage_main/src/app/orient.c
Normal file
|
|
@ -0,0 +1,120 @@
|
|||
/*
|
||||
* orient.c - 关于光照方向控制的模块
|
||||
*
|
||||
* 所有如上,所有如下
|
||||
*/
|
||||
|
||||
|
||||
#include "uart.h"
|
||||
|
||||
#include "include/orient.h"
|
||||
#include "include/config.h"
|
||||
|
||||
|
||||
void orient_init(void)
|
||||
{
|
||||
uart_init(ORIENT_UARTX, 2400);
|
||||
gpio_init(RE_DE_PINX, 1, 1);
|
||||
|
||||
orient_setmode(0, MODE_MANUL);
|
||||
orient_setmode(1, MODE_MANUL);
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
* orient_setspeed() - 设置方向控制的速度以及角度
|
||||
*
|
||||
* @addr: 地址码
|
||||
* @ori: 方向
|
||||
* @spd: 表示速度,范围在0~100
|
||||
*/
|
||||
void orient_setspeed(uint8_t addr, orient ori, uint8_t spd)
|
||||
{
|
||||
uint8_t cmd[7] = { 0xff, addr, 0x00, 0x00, 0x00, 0x00, 0x00 };
|
||||
|
||||
switch (ori)
|
||||
{
|
||||
case ORIENT_UP:
|
||||
cmd[3] = 0x08;
|
||||
break;
|
||||
case ORIENT_DOWN:
|
||||
cmd[3] = 0x10;
|
||||
break;
|
||||
case ORIENT_LEFT:
|
||||
cmd[3] = 0x04;
|
||||
break;
|
||||
case ORIENT_RIGHT:
|
||||
cmd[3] = 0x02;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
if (spd > 100)
|
||||
{
|
||||
spd = 100;
|
||||
}
|
||||
cmd[4] = (uint16_t)0x3f * spd / 100;
|
||||
cmd[6] = cmd[1] + cmd[2] + cmd[3] + cmd[4] + cmd[5];
|
||||
|
||||
uart_sendN(ORIENT_UARTX, cmd, 7);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* orient_presetop() - 设置预置点
|
||||
*
|
||||
* @addr: 地址码
|
||||
* @op: 对预置点的操作
|
||||
* @npre: 预置点编号
|
||||
*/
|
||||
void orient_presetop(uint8_t addr, preset_op op, uint8_t npre)
|
||||
{
|
||||
uint8_t cmd[7] = { 0xff, addr, 0x00, 0x00, 0x00, npre, 0x00 };
|
||||
|
||||
switch (op)
|
||||
{
|
||||
case PRESET_SET:
|
||||
cmd[3] = 0x03;
|
||||
break;
|
||||
case PRESET_CALL:
|
||||
cmd[3] = 0x07;
|
||||
break;
|
||||
case PRESET_CLEAR:
|
||||
cmd[3] = 0x05;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
cmd[6] = cmd[1] + cmd[2] + cmd[3] + cmd[4] + cmd[5];
|
||||
uart_sendN(ORIENT_UARTX, cmd, 7);
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
* orient_setmode() - 设置云台模式
|
||||
*
|
||||
* @addr: 地址码
|
||||
* @mod: 模式标志
|
||||
*/
|
||||
void orient_setmode(uint8_t addr, orient_mode mod)
|
||||
{
|
||||
uint8_t cmd[7] = { 0xff, addr, 0x00, 0x00, 0x00, 0x00, 0x00 };
|
||||
|
||||
switch (mod)
|
||||
{
|
||||
case MODE_AUTO:
|
||||
cmd[2] = 0x90;
|
||||
break;
|
||||
case MODE_MANUL:
|
||||
cmd[2] = 0x00;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
cmd[6] = cmd[1] + cmd[2] + cmd[3] + cmd[4] + cmd[5];
|
||||
|
||||
uart_sendN(ORIENT_UARTX, cmd, 7);
|
||||
return;
|
||||
}
|
||||
|
|
@ -1,154 +1,714 @@
|
|||
/*
|
||||
* plan_handle.c - 计划处理模块
|
||||
*
|
||||
* 计划处理,指的是根据内存中的计划数据和时间数据得出输出数据,输出数据包括继电器信
|
||||
* 号、植物属性值。
|
||||
* 关于植物属性值,包括已完成计划周期次数,等。
|
||||
* 基本原则是,起始周期时间区间加上重复周期数可以得到一个区间集合,判断当前时间是否
|
||||
* 属于这个时间区间集合中的一个区间中的一个时间点。由此得到继电器信号,并在继电器信
|
||||
* 号被置为无效的时候将计划周期次数加一。
|
||||
* 需要解决的问题有,如何访问计划数据与时间数据,以及如何写入输出数据。
|
||||
* 计划数据在tft.c模块,属于静态数据,如果要访问的话,有两种方式,一是声明计划数据
|
||||
* 的类型并返回其整体地址,一个整体地址可以访问到所有的对象属性,不过需要知道这个地
|
||||
* 址上数据的分布情况,需要额外的数据。二是根据每个元素的名字通过查询得到其单独的值。
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include "gpio.h"
|
||||
|
||||
#include "include/plan_handle.h"
|
||||
#include "include/pm_time.h"
|
||||
#include "include/tft.h"
|
||||
#include "config.h"
|
||||
|
||||
typedef struct plan_output_
|
||||
{
|
||||
uint8_t is_lgreach;
|
||||
uint8_t is_wtreach;
|
||||
uint8_t lg_cnt;
|
||||
uint8_t wt_cnt;
|
||||
} plan_output;
|
||||
|
||||
typedef struct plan_input_
|
||||
{
|
||||
calendar_info lgbg_t;
|
||||
calendar_info lged_t;
|
||||
calendar_info lgpd_t;
|
||||
|
||||
calendar_info wtbg_t;
|
||||
calendar_info wted_t;
|
||||
calendar_info wtpd_t;
|
||||
|
||||
uint8_t x_orient;
|
||||
uint8_t y_orient;
|
||||
uint8_t lg_r : 1;
|
||||
uint8_t lg_b : 1;
|
||||
uint8_t lg_uvb : 1;
|
||||
uint8_t water : 1;
|
||||
uint8_t sw : 1;
|
||||
} plan_input;
|
||||
|
||||
static void indata_to_outdata(plan_input *ind, plan_output *outd);
|
||||
static void ctr_exe(uint8_t activity);
|
||||
|
||||
|
||||
static plan_output plan_out[PLAN_DATA_NUM] = { 0 };
|
||||
plan_input plan_in[PLAN_DATA_NUM] = { 0 };
|
||||
|
||||
|
||||
|
||||
void plan_handle_init(void)
|
||||
{
|
||||
gpio_Interrupt_init(LGRED_PINX, GPO, GPI_DISAB);
|
||||
gpio_Interrupt_init(LGBLUE_PINX, GPO, GPI_DISAB);
|
||||
gpio_Interrupt_init(LGUVB_PINX, GPO, GPI_DISAB);
|
||||
gpio_Interrupt_init(WATER_PINX, GPO, GPI_DISAB);
|
||||
// 还有设置方向的初始化
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static void indata_to_outdata(plan_input *ind, plan_output *outd)
|
||||
{
|
||||
calendar_info st = get_system_time();
|
||||
uint32_t sys_sec = calendar_to_sec(&st);
|
||||
|
||||
uint32_t lgpd_sec = calendar_to_sec(&ind->lgpd_t);
|
||||
uint32_t crt_lgbg_sec = calendar_to_sec(&ind->lgbg_t) + lgpd_sec * outd->lg_cnt;
|
||||
uint32_t crt_lged_sec = calendar_to_sec(&ind->lged_t) + lgpd_sec * outd->lg_cnt;
|
||||
|
||||
uint32_t wtpd_sec = calendar_to_sec(&ind->wtpd_t);
|
||||
uint32_t crt_wtbg_sec = calendar_to_sec(&ind->wtbg_t) + wtpd_sec * outd->wt_cnt;
|
||||
uint32_t crt_wted_sec = calendar_to_sec(&ind->wted_t) + wtpd_sec * outd->wt_cnt;
|
||||
|
||||
if (crt_lgbg_sec < sys_sec && crt_lged_sec > sys_sec)
|
||||
{
|
||||
outd->is_lgreach = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (outd->is_lgreach == 1)
|
||||
{
|
||||
outd->lg_cnt++;
|
||||
}
|
||||
outd->is_lgreach = 0;
|
||||
}
|
||||
|
||||
if (crt_wtbg_sec < sys_sec && crt_wted_sec > sys_sec)
|
||||
{
|
||||
outd->is_wtreach = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (outd->is_wtreach== 1)
|
||||
{
|
||||
outd->wt_cnt++;
|
||||
}
|
||||
outd->is_wtreach = 0;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
static void ctr_exe(uint8_t activity)
|
||||
{
|
||||
if (activity >= PLAN_DATA_NUM)
|
||||
{
|
||||
gpio_set(LGRED_PINX, 0);
|
||||
gpio_set(LGBLUE_PINX, 0);
|
||||
gpio_set(LGUVB_PINX, 0);
|
||||
gpio_set(WATER_PINX, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
//set_orient(plan_in[activity].x_orient, plan_in[activity].y_orient);
|
||||
gpio_set(LGRED_PINX, plan_in[activity].lg_r);
|
||||
gpio_set(LGBLUE_PINX, plan_in[activity].lg_b);
|
||||
gpio_set(LGUVB_PINX, plan_in[activity].lg_uvb);
|
||||
gpio_set(WATER_PINX, plan_in[activity].water);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
void plan_handle(void)
|
||||
{
|
||||
uint8_t activity = PLAN_DATA_NUM;
|
||||
|
||||
for (uint8_t i = 0; i < PLAN_DATA_NUM; i++)
|
||||
{
|
||||
indata_to_outdata(&plan_in[i], &plan_out[i]);
|
||||
if (plan_in[i].sw == 1)
|
||||
{
|
||||
if (plan_out[i].is_lgreach == 1 || plan_out[i].is_wtreach == 1)
|
||||
{
|
||||
activity = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
ctr_exe(activity);
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
* plan_handle.c - 计划处理模块
|
||||
*
|
||||
* 计划处理,指的是根据内存中的计划数据和时间数据得出输出数据,输出数据包括继电
|
||||
* 器信号、植物属性值。
|
||||
* 关于植物属性值,包括已完成计划周期次数,等。
|
||||
* 基本原则是,起始周期时间区间加上重复周期数可以得到一个区间集合,判断当前时间
|
||||
* 是否属于这个时间区间集合中的一个区间中的一个时间点。由此得到继电器信号,并在
|
||||
* 继电器信号被置为无效的时候将计划周期次数加一。
|
||||
* 需要解决的问题有,如何访问计划数据与时间数据,以及如何写入输出数据。
|
||||
* 计划数据在tft.c模块,属于静态数据,如果要访问的话,有两种方式,一是声明计划
|
||||
* 数据的类型并返回其整体地址,一个整体地址可以访问到所有的对象属性,不过需要知
|
||||
* 道这个地址上数据的分布情况,需要额外的数据。二是根据每个元素的名字通过查询得
|
||||
* 到其单独的值。
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include "gpio.h"
|
||||
|
||||
#include "include/tft_plan_internal.h"
|
||||
|
||||
#include "include/plan_handle.h"
|
||||
#include "include/tft.h"
|
||||
#include "include/config.h"
|
||||
#include "include/pm_flash.h"
|
||||
#include "include/orient.h"
|
||||
#include "include/key.h"
|
||||
|
||||
/*
|
||||
* 计划输出数据类型定义
|
||||
* is_reach表示是否到达计划执行时间,
|
||||
* note是一个字符串,可以用来存储一些提示性字符
|
||||
*/
|
||||
typedef struct plan_output_
|
||||
{
|
||||
uint8_t is_reach;
|
||||
uint8_t note[10];
|
||||
} plan_output;
|
||||
|
||||
typedef enum lg_state_
|
||||
{
|
||||
LIGHT = 1, NO_LIGHT = 0
|
||||
} lg_state;
|
||||
|
||||
|
||||
/*
|
||||
* 静态函数声明
|
||||
*/
|
||||
static void indata_to_outdata(plan_input *ind, plan_output *outd);
|
||||
static void plan_ctr_exe(uint8_t activity);
|
||||
static void plan_inpu_to_tft(void);
|
||||
static void plan_key_func(void);
|
||||
static void manul_key_func(void);
|
||||
static void manul_ctr(void);
|
||||
static void plan_ctr(void);
|
||||
|
||||
/*
|
||||
* 输出和输入变量定义,初始化全部为0
|
||||
*/
|
||||
static plan_output plan_out[OBJ_NUM] = { 0 };
|
||||
plan_input plan_in[OBJ_NUM] = { 0 };
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* plan_handle_init() - 对计划处理模块进行初始化
|
||||
*
|
||||
* 主要包括IO初始化和计划数据的上电恢复工作
|
||||
*/
|
||||
void plan_handle_init(void)
|
||||
{
|
||||
gpio_init(LGRED_PINX, 1, NO_LIGHT);
|
||||
gpio_init(LGBLUE_PINX, 1, NO_LIGHT);
|
||||
gpio_init(LGUVB_PINX, 1, NO_LIGHT);
|
||||
gpio_init(WATER_PINX, 1, 0);
|
||||
|
||||
gpio_init(LG1_PINX, 1, 1);
|
||||
gpio_init(LG2_PINX, 1, 1);
|
||||
gpio_init(LG3_PINX, 1, 1);
|
||||
|
||||
/*
|
||||
* 数据恢复,数据从从flash到plan_handle模块,再从plan_handle模块到tft模块
|
||||
*/
|
||||
flash_read((uint8_t *)&plan_in, sizeof(plan_in));
|
||||
plan_inpu_to_tft();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* indata_to_outdata() - 将一个植物输入的计划数据转换为对应的输出数据
|
||||
* @ind: 计划输入数据的地址
|
||||
* @outd: 转换后的值得地址
|
||||
*
|
||||
* 主要的功能是根据输入的数据得到是否到达计划执行时间,以及完成计划的次数
|
||||
*/
|
||||
static void indata_to_outdata(plan_input *ind, plan_output *outd)
|
||||
{
|
||||
// 首先得到系统时间并转换为日历时间格式
|
||||
calendar_info st = get_system_time();
|
||||
uint32_t sys_sec = calendar_to_sec(&st);
|
||||
|
||||
/*
|
||||
* 因为在周期时间输入的时候没有输入年、月、秒的地方,所以将其初始化为起始值
|
||||
* 以方便计算周期时间
|
||||
*/
|
||||
ind->pd_t.year = START_YEAR;
|
||||
ind->pd_t.month = 1;
|
||||
ind->pd_t.sec = 0;
|
||||
uint32_t pd_sec = calendar_to_sec(&ind->pd_t);
|
||||
|
||||
/*
|
||||
* 将第一次开始的时间加上周期时长乘以已完成次数,得到当前的开始时间;
|
||||
* 结束时间的计算同开始时间。
|
||||
* 由这两个时间可以得到当前执行计划的区间。
|
||||
*/
|
||||
uint32_t ctr_bg_sec = calendar_to_sec(&ind->bg_t) + pd_sec * ind->cnt;
|
||||
uint32_t ctr_ed_sec = calendar_to_sec(&ind->ed_t) + pd_sec * ind->cnt;
|
||||
|
||||
// 比较系统时间是否在执行计划的区间
|
||||
if (ctr_bg_sec < sys_sec && ctr_ed_sec > sys_sec)
|
||||
{
|
||||
outd->is_reach = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (outd->is_reach == 1)
|
||||
{
|
||||
/*
|
||||
* 如果没有在执行区间,并且上一次在执行区间的话,表明一次计划执行完
|
||||
* 成,给计划完成次数加一。
|
||||
*/
|
||||
ind->cnt++;
|
||||
}
|
||||
outd->is_reach = 0;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* plan_ctr_exe() - 计划控制的输出执行
|
||||
* @activity: 表示执行哪一个计划活动,一个活动编号对应一个植物,当activity大于
|
||||
* 植物的编号时,表示没有计划任务需要执行。
|
||||
*
|
||||
* 主要包括IO输出控制以及云台预置位控制
|
||||
*/
|
||||
static void plan_ctr_exe(uint8_t activity)
|
||||
{
|
||||
/*
|
||||
* isnt_first数组是为了标记每一个活动在一次计划区间内是不是第一次执行,主要
|
||||
* 是因为不能频繁的多次调用云台的预置位
|
||||
*/
|
||||
static uint8_t isnt_first[OBJ_NUM] = { 0 };
|
||||
if (activity >= OBJ_NUM)
|
||||
{
|
||||
// activity大于植物的编号的话,代表没有计划任务需要执行
|
||||
for (uint8_t i = 0; i < OBJ_NUM; i++)
|
||||
{
|
||||
isnt_first[i] = 0;
|
||||
}
|
||||
gpio_set(LGRED_PINX, NO_LIGHT);
|
||||
gpio_set(LGBLUE_PINX, NO_LIGHT);
|
||||
gpio_set(LGUVB_PINX, NO_LIGHT);
|
||||
gpio_set(WATER_PINX, NO_LIGHT);
|
||||
}
|
||||
else
|
||||
{
|
||||
// 当activity对应于一个植物编号并且是第一次执行的时候,就执行计划
|
||||
if (isnt_first[activity] == 0)
|
||||
{
|
||||
orient_presetop(0, PRESET_CALL, activity + 1);
|
||||
orient_presetop(1, PRESET_CALL, activity + 1);
|
||||
gpio_set(LGRED_PINX, plan_in[activity].lg_r == 1 ? LIGHT : NO_LIGHT);
|
||||
gpio_set(LGBLUE_PINX, plan_in[activity].lg_b == 1 ? LIGHT : NO_LIGHT);
|
||||
gpio_set(LGUVB_PINX, plan_in[activity].lg_uvb == 1 ? LIGHT : NO_LIGHT);
|
||||
gpio_set(WATER_PINX, plan_in[activity].water == 1 ? LIGHT : NO_LIGHT);
|
||||
isnt_first[activity] = 1;
|
||||
}
|
||||
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* plan_ctr() - 计划控制
|
||||
*
|
||||
* 完成从计划数据到输出执行的功能。
|
||||
*/
|
||||
void plan_ctr(void)
|
||||
{
|
||||
// 首先初始化活动不为任意一个植物编号
|
||||
uint8_t activity = OBJ_NUM;
|
||||
|
||||
// 依次遍历每一个植物,得到需要执行的活动号,即对应的植物编号
|
||||
for (uint8_t i = 0; i < OBJ_NUM; i++)
|
||||
{
|
||||
indata_to_outdata(&plan_in[i], &plan_out[i]);
|
||||
if (plan_in[i].sw == 1)
|
||||
{
|
||||
if (plan_out[i].is_reach == 1)
|
||||
{
|
||||
activity = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
plan_ctr_exe(activity);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* plan_handle() - 计划处理
|
||||
*
|
||||
* 完成计划模式的数据处理及输出功能,手动模式下因为简单不需要复杂的数据运算,
|
||||
* 此处处理为空,直接在其输入部分执行
|
||||
*/
|
||||
void plan_handle(void)
|
||||
{
|
||||
if (gpio_get(AMS_KEY_PINX))
|
||||
{
|
||||
plan_ctr();
|
||||
}
|
||||
else
|
||||
{
|
||||
manul_ctr();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* key_func() - 完成手动和自动模式下的按键输入功能
|
||||
*
|
||||
* 在自动模式下只是数据的输入,在手动模式在包括数据的输入和对应的输出执行部分。
|
||||
*/
|
||||
void key_func(void)
|
||||
{
|
||||
if (gpio_get(AMS_KEY_PINX))
|
||||
{
|
||||
plan_key_func();
|
||||
}
|
||||
else
|
||||
{
|
||||
manul_key_func();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* manul_ctr() - 手动控制部分
|
||||
*
|
||||
* 为空函数
|
||||
*/
|
||||
void manul_ctr(void)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* tft_to_plan_input() - 将tft显示的数据提取到计划处理的输入数据结构中
|
||||
* @objn: 提取的对象号,0~7
|
||||
*
|
||||
* 这个函数应该在tft输入有改变是被调用。
|
||||
*/
|
||||
void tft_to_plan_input(uint8_t objn)
|
||||
{
|
||||
plan_in[objn].bg_t.year = *get_value_of_kvp("bg_y", objn);
|
||||
plan_in[objn].bg_t.month = *get_value_of_kvp("bg_mo", objn);
|
||||
plan_in[objn].bg_t.mday = *get_value_of_kvp("bg_d", objn);
|
||||
plan_in[objn].bg_t.hour = *get_value_of_kvp("bg_h", objn);
|
||||
plan_in[objn].bg_t.min = *get_value_of_kvp("bg_mi", objn);
|
||||
plan_in[objn].bg_t.sec = 0;
|
||||
|
||||
/*
|
||||
* 结束时间在tft条目没有年、月、日,赋值为和开始时间相同
|
||||
*/
|
||||
plan_in[objn].ed_t.year = plan_in[objn].bg_t.year;
|
||||
plan_in[objn].ed_t.month = plan_in[objn].bg_t.month;
|
||||
plan_in[objn].ed_t.mday = plan_in[objn].bg_t.mday;
|
||||
plan_in[objn].ed_t.hour = *get_value_of_kvp("ed_h", objn);
|
||||
plan_in[objn].ed_t.min = *get_value_of_kvp("ed_mi", objn);
|
||||
plan_in[objn].ed_t.sec = 0;
|
||||
|
||||
plan_in[objn].pd_t.mday = *get_value_of_kvp("pd_d", objn);
|
||||
plan_in[objn].pd_t.hour = *get_value_of_kvp("pd_h", objn);
|
||||
plan_in[objn].pd_t.min = *get_value_of_kvp("pd_mi", objn);
|
||||
|
||||
plan_in[objn].lg_r = *get_value_of_kvp("lg_r", objn);
|
||||
plan_in[objn].lg_b = *get_value_of_kvp("lg_b", objn);
|
||||
plan_in[objn].lg_uvb = *get_value_of_kvp("lg_uvb", objn);
|
||||
plan_in[objn].water = *get_value_of_kvp("water", objn);
|
||||
|
||||
plan_in[objn].sw = *get_value_of_kvp("sw", objn);
|
||||
plan_in[objn].cnt = *get_value_of_kvp("cnt", objn);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* plan_inpu_to_tft() - 计划数据到tft
|
||||
*
|
||||
* 这个函数是为了在初始化时将从flash中恢复的计划数据重新装入tft
|
||||
*/
|
||||
static void plan_inpu_to_tft(void)
|
||||
{
|
||||
for(uint8_t objn = 0; objn < 8; objn++)
|
||||
{
|
||||
*get_value_of_kvp("bg_y", objn) = plan_in[objn].bg_t.year;
|
||||
*get_value_of_kvp("bg_mo", objn) = plan_in[objn].bg_t.month;
|
||||
*get_value_of_kvp("bg_d", objn) = plan_in[objn].bg_t.mday;
|
||||
*get_value_of_kvp("bg_h", objn) = plan_in[objn].bg_t.hour;
|
||||
*get_value_of_kvp("bg_mi", objn) = plan_in[objn].bg_t.min;
|
||||
|
||||
/*
|
||||
* 结束时间在tft条目没有年、月、日
|
||||
* *get_value_of_kvp("ed_y", objn) = plan_in[objn].ed_t.year;
|
||||
* *get_value_of_kvp("ed_mo", objn) = plan_in[objn].ed_t.month;
|
||||
* *get_value_of_kvp("ed_d", objn) = plan_in[objn].ed_t.mday;
|
||||
*/
|
||||
*get_value_of_kvp("ed_h", objn) = plan_in[objn].ed_t.hour;
|
||||
*get_value_of_kvp("ed_mi", objn) = plan_in[objn].ed_t.min;
|
||||
|
||||
*get_value_of_kvp("pd_d", objn) = plan_in[objn].pd_t.mday;
|
||||
*get_value_of_kvp("pd_h", objn) = plan_in[objn].pd_t.hour;
|
||||
*get_value_of_kvp("pd_mi", objn) = plan_in[objn].pd_t.min;
|
||||
|
||||
*get_value_of_kvp("lg_r", objn) = plan_in[objn].lg_r;
|
||||
*get_value_of_kvp("lg_b", objn) = plan_in[objn].lg_b;
|
||||
*get_value_of_kvp("lg_uvb", objn) = plan_in[objn].lg_uvb;
|
||||
*get_value_of_kvp("water", objn) = plan_in[objn].water;
|
||||
|
||||
*get_value_of_kvp("sw", objn) = plan_in[objn].sw;
|
||||
*get_value_of_kvp("cnt", objn) = plan_in[objn].cnt;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* plan_key_func() - 计划模式下按键的处理
|
||||
*
|
||||
* 主要和tft显示屏关
|
||||
*/
|
||||
static void plan_key_func(void)
|
||||
{
|
||||
/*
|
||||
* 按键功能注册部分
|
||||
*/
|
||||
switch (get_key_mean(UP_KEY))
|
||||
{
|
||||
case N_KEY:
|
||||
//printf("无键\n");
|
||||
break;
|
||||
case S_KEY:
|
||||
#ifdef PM_DEBUG
|
||||
printf("UP_KEY 单击\n");
|
||||
#endif /* PM_DEBUG */
|
||||
tft_left();
|
||||
break;
|
||||
case D_KEY:
|
||||
#ifdef PM_DEBUG
|
||||
printf("UP_KEY 双击\n");
|
||||
#endif /* PM_DEBUG */
|
||||
tft_up();
|
||||
break;
|
||||
case L_KEY:
|
||||
#ifdef PM_DEBUG
|
||||
printf("UP_KEY 长按\n");
|
||||
#endif /* PM_DEBUG */
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
switch (get_key_mean(DOWN_KEY))
|
||||
{
|
||||
case N_KEY:
|
||||
//printf("无键\n");
|
||||
break;
|
||||
case S_KEY:
|
||||
#ifdef PM_DEBUG
|
||||
printf("DOWN_KEY 单击\n");
|
||||
#endif /* PM_DEBUG */
|
||||
tft_right();
|
||||
break;
|
||||
case D_KEY:
|
||||
#ifdef PM_DEBUG
|
||||
printf("DOWN_KEY 双击\n");
|
||||
#endif /* PM_DEBUG */
|
||||
tft_down();
|
||||
break;
|
||||
case L_KEY:
|
||||
#ifdef PM_DEBUG
|
||||
printf("DOWN_KEY 长按\n");
|
||||
#endif /* PM_DEBUG */
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
switch (get_key_mean(OK_KEY))
|
||||
{
|
||||
case N_KEY:
|
||||
//printf("无键\n");
|
||||
break;
|
||||
case S_KEY:
|
||||
#ifdef PM_DEBUG
|
||||
printf("OK_KEY 单击\n");
|
||||
tft_ok();
|
||||
#endif /* PM_DEBUG */
|
||||
break;
|
||||
case D_KEY:
|
||||
#ifdef PM_DEBUG
|
||||
printf("OK_KEY 双击\n");
|
||||
#endif /* PM_DEBUG */
|
||||
break;
|
||||
case L_KEY:
|
||||
#ifdef PM_DEBUG
|
||||
printf("OK_KEY 长按\n");
|
||||
#endif /* PM_DEBUG */
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
switch (get_key_mean(RET_KEY))
|
||||
{
|
||||
case N_KEY:
|
||||
//printf("无键\n");
|
||||
break;
|
||||
case S_KEY:
|
||||
#ifdef PM_DEBUG
|
||||
printf("RET_KEY 单击\n");
|
||||
#endif /* PM_DEBUG */
|
||||
tft_ret();
|
||||
break;
|
||||
case D_KEY:
|
||||
#ifdef PM_DEBUG
|
||||
printf("RET_KEY 双击\n");
|
||||
#endif /* PM_DEBUG */
|
||||
break;
|
||||
case L_KEY:
|
||||
#ifdef PM_DEBUG
|
||||
printf("RET_KEY 长按\n");
|
||||
#endif /* PM_DEBUG */
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
switch (get_key_mean(MR_KEY))
|
||||
{
|
||||
case N_KEY:
|
||||
//printf("无键\n");
|
||||
break;
|
||||
case S_KEY:
|
||||
#ifdef PM_DEBUG
|
||||
printf("MR_KEY 单击\n");
|
||||
#endif /* PM_DEBUG */
|
||||
break;
|
||||
case D_KEY:
|
||||
#ifdef PM_DEBUG
|
||||
printf("MR_KEY 双击\n");
|
||||
#endif /* PM_DEBUG */
|
||||
break;
|
||||
case L_KEY:
|
||||
#ifdef PM_DEBUG
|
||||
printf("MR_KEY 长按\n");
|
||||
#endif /* PM_DEBUG */
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
switch (get_key_mean(MB_KEY))
|
||||
{
|
||||
case N_KEY:
|
||||
//printf("无键\n");
|
||||
break;
|
||||
case S_KEY:
|
||||
#ifdef PM_DEBUG
|
||||
printf("MB_KEY 单击\n");
|
||||
#endif /* PM_DEBUG */
|
||||
break;
|
||||
case D_KEY:
|
||||
#ifdef PM_DEBUG
|
||||
printf("MB_KEY 双击\n");
|
||||
#endif /* PM_DEBUG */
|
||||
break;
|
||||
case L_KEY:
|
||||
#ifdef PM_DEBUG
|
||||
printf("MB_KEY 长按\n");
|
||||
#endif /* PM_DEBUG */
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
switch (get_key_mean(MUVB_KEY))
|
||||
{
|
||||
case N_KEY:
|
||||
//printf("无键\n");
|
||||
break;
|
||||
case S_KEY:
|
||||
#ifdef PM_DEBUG
|
||||
printf("MUVB_KEY 单击\n");
|
||||
#endif /* PM_DEBUG */
|
||||
break;
|
||||
case D_KEY:
|
||||
#ifdef PM_DEBUG
|
||||
printf("MUVB_KEY 双击\n");
|
||||
#endif /* PM_DEBUG */
|
||||
break;
|
||||
case L_KEY:
|
||||
#ifdef PM_DEBUG
|
||||
printf("MUVB_KEY 长按\n");
|
||||
#endif /* PM_DEBUG */
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* manul_key_func() - 手动模式下的按键功能
|
||||
*
|
||||
* 包括了按键的输入和对应的输出执行部分
|
||||
*/
|
||||
static void manul_key_func(void)
|
||||
{
|
||||
/*
|
||||
* 按键功能注册部分
|
||||
*/
|
||||
static uint8_t mlgr, mlgb, mlguvb;
|
||||
|
||||
switch (get_key_mean(UP_KEY))
|
||||
{
|
||||
case N_KEY:
|
||||
//printf("无键\n");
|
||||
break;
|
||||
case S_KEY:
|
||||
#ifdef PM_DEBUG
|
||||
printf("UP_KEY 单击\n");
|
||||
#endif /* PM_DEBUG */
|
||||
orient_setspeed(0, ORIENT_LEFT, 100);
|
||||
break;
|
||||
case D_KEY:
|
||||
#ifdef PM_DEBUG
|
||||
printf("UP_KEY 双击\n");
|
||||
#endif /* PM_DEBUG */
|
||||
orient_setspeed(0, ORIENT_RIGHT, 100);
|
||||
break;
|
||||
case L_KEY:
|
||||
#ifdef PM_DEBUG
|
||||
printf("UP_KEY 长按\n");
|
||||
#endif /* PM_DEBUG */
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
switch (get_key_mean(DOWN_KEY))
|
||||
{
|
||||
case N_KEY:
|
||||
//printf("无键\n");
|
||||
break;
|
||||
case S_KEY:
|
||||
#ifdef PM_DEBUG
|
||||
printf("DOWN_KEY 单击\n");
|
||||
#endif /* PM_DEBUG */
|
||||
orient_setspeed(1, ORIENT_LEFT, 100);
|
||||
break;
|
||||
case D_KEY:
|
||||
#ifdef PM_DEBUG
|
||||
printf("DOWN_KEY 双击\n");
|
||||
#endif /* PM_DEBUG */
|
||||
orient_setspeed(1, ORIENT_RIGHT, 100);
|
||||
break;
|
||||
case L_KEY:
|
||||
#ifdef PM_DEBUG
|
||||
printf("DOWN_KEY 长按\n");
|
||||
#endif /* PM_DEBUG */
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
switch (get_key_mean(OK_KEY))
|
||||
{
|
||||
case N_KEY:
|
||||
//printf("无键\n");
|
||||
break;
|
||||
case S_KEY:
|
||||
#ifdef PM_DEBUG
|
||||
printf("OK_KEY 单击\n");
|
||||
#endif /* PM_DEBUG */
|
||||
break;
|
||||
case D_KEY:
|
||||
#ifdef PM_DEBUG
|
||||
printf("OK_KEY 双击\n");
|
||||
#endif /* PM_DEBUG */
|
||||
break;
|
||||
case L_KEY:
|
||||
#ifdef PM_DEBUG
|
||||
printf("OK_KEY 长按\n");
|
||||
#endif /* PM_DEBUG */
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
switch (get_key_mean(RET_KEY))
|
||||
{
|
||||
case N_KEY:
|
||||
//printf("无键\n");
|
||||
break;
|
||||
case S_KEY:
|
||||
#ifdef PM_DEBUG
|
||||
printf("RET_KEY 单击\n");
|
||||
#endif /* PM_DEBUG */
|
||||
orient_setmode(0, MODE_MANUL);
|
||||
orient_setmode(1, MODE_MANUL);
|
||||
break;
|
||||
case D_KEY:
|
||||
#ifdef PM_DEBUG
|
||||
printf("RET_KEY 双击\n");
|
||||
#endif /* PM_DEBUG */
|
||||
break;
|
||||
case L_KEY:
|
||||
#ifdef PM_DEBUG
|
||||
printf("RET_KEY 长按\n");
|
||||
#endif /* PM_DEBUG */
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
switch (get_key_mean(MR_KEY))
|
||||
{
|
||||
case N_KEY:
|
||||
//printf("无键\n");
|
||||
break;
|
||||
case S_KEY:
|
||||
#ifdef PM_DEBUG
|
||||
printf("MR_KEY 单击\n");
|
||||
#endif /* PM_DEBUG */
|
||||
mlgr = !mlgr;
|
||||
gpio_set(LGRED_PINX, mlgr == 1 ? LIGHT : NO_LIGHT);
|
||||
break;
|
||||
case D_KEY:
|
||||
#ifdef PM_DEBUG
|
||||
printf("MR_KEY 双击\n");
|
||||
#endif /* PM_DEBUG */
|
||||
break;
|
||||
case L_KEY:
|
||||
#ifdef PM_DEBUG
|
||||
printf("MR_KEY 长按\n");
|
||||
#endif /* PM_DEBUG */
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
switch (get_key_mean(MB_KEY))
|
||||
{
|
||||
case N_KEY:
|
||||
//printf("无键\n");
|
||||
break;
|
||||
case S_KEY:
|
||||
#ifdef PM_DEBUG
|
||||
printf("MB_KEY 单击\n");
|
||||
#endif /* PM_DEBUG */
|
||||
mlgb = !mlgb;
|
||||
gpio_set(LGBLUE_PINX, mlgb == 1 ? LIGHT : NO_LIGHT);
|
||||
break;
|
||||
case D_KEY:
|
||||
#ifdef PM_DEBUG
|
||||
printf("MB_KEY 双击\n");
|
||||
#endif /* PM_DEBUG */
|
||||
break;
|
||||
case L_KEY:
|
||||
#ifdef PM_DEBUG
|
||||
printf("MB_KEY 长按\n");
|
||||
#endif /* PM_DEBUG */
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
switch (get_key_mean(MUVB_KEY))
|
||||
{
|
||||
case N_KEY:
|
||||
//printf("无键\n");
|
||||
break;
|
||||
case S_KEY:
|
||||
#ifdef PM_DEBUG
|
||||
printf("MUVB_KEY 单击\n");
|
||||
#endif /* PM_DEBUG */
|
||||
mlguvb = !mlguvb;
|
||||
gpio_set(LGUVB_PINX, mlguvb == 1 ? LIGHT : NO_LIGHT);
|
||||
break;
|
||||
case D_KEY:
|
||||
#ifdef PM_DEBUG
|
||||
printf("MUVB_KEY 双击\n");
|
||||
#endif /* PM_DEBUG */
|
||||
break;
|
||||
case L_KEY:
|
||||
#ifdef PM_DEBUG
|
||||
printf("MUVB_KEY 长按\n");
|
||||
#endif /* PM_DEBUG */
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,159 +1,171 @@
|
|||
|
||||
|
||||
#include "SSD_FTFx.h"
|
||||
|
||||
#include "pm_flash_cfg.h"
|
||||
|
||||
#define USED_SECTOR_NUM 0x01U
|
||||
|
||||
|
||||
FLASH_SSD_CONFIG flashSSDConfig =
|
||||
{
|
||||
FTFx_REG_BASE, /* FTFx control register base */
|
||||
PFLASH_BLOCK_BASE, /* base address of PFlash block */
|
||||
PBLOCK_SIZE, /* size of PFlash block */
|
||||
DEFLASH_BLOCK_BASE, /* base address of DFlash block */
|
||||
0x0U, /* size of DFlash block */
|
||||
EERAM_BLOCK_BASE, /* base address of EERAM block */
|
||||
0x0U, /* size of EEE block */
|
||||
DEBUGENABLE, /* background debug mode enable bit */
|
||||
NULL_CALLBACK /* pointer to callback function */
|
||||
};
|
||||
|
||||
#define LAUNCH_CMD_SIZE 0x80U
|
||||
|
||||
pFLASHCOMMANDSEQUENCE g_FlashLaunchCommand = (pFLASHCOMMANDSEQUENCE)0xFFFFFFFF;
|
||||
|
||||
uint16_t __ram_func[LAUNCH_CMD_SIZE/2]; /* array to copy __Launch_Command func to RAM */
|
||||
|
||||
void pm_flash_init(void)
|
||||
{
|
||||
uint32_t ret;
|
||||
|
||||
g_FlashLaunchCommand =
|
||||
(pFLASHCOMMANDSEQUENCE)RelocateFunction((uint32_t)__ram_func ,
|
||||
LAUNCH_CMD_SIZE ,(uint32_t)FlashCommandSequence);
|
||||
ret = FlashInit(&flashSSDConfig);
|
||||
if (FTFx_OK != ret)
|
||||
{
|
||||
ErrorTrap(ret);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
* flash_write() - 将数据写入flash
|
||||
* @saddr: 要写入的数据的起始地址
|
||||
* @size: 写入数据的字节数
|
||||
*
|
||||
* 这个函数的执行过程包括擦除和写入,默认的写入范围为flash的最后一个扇区,大小为
|
||||
* 1KB,如果要改变大小,改参数就可以了,注意不要覆盖了code,具体可看map文件。
|
||||
* 擦除是一次性擦除全部选择的扇区范围,所以在写入的时候要注意保存先前写入的数据
|
||||
*/
|
||||
void flash_write(uint8_t *saddr, uint16_t nbyte)
|
||||
{
|
||||
uint32_t ret;
|
||||
uint32_t dest;
|
||||
uint32_t size;
|
||||
uint16_t number;
|
||||
uint32_t temp;
|
||||
|
||||
/*
|
||||
* 擦除扇区
|
||||
*/
|
||||
dest = flashSSDConfig.PFlashBlockBase + BYTE2WORD(flashSSDConfig.PFlashBlockSize
|
||||
- USED_SECTOR_NUM * FTFx_PSECTOR_SIZE);
|
||||
while ((dest + BYTE2WORD(FTFx_PSECTOR_SIZE)) <= (flashSSDConfig.PFlashBlockBase
|
||||
+ BYTE2WORD(flashSSDConfig.PFlashBlockSize)))
|
||||
{
|
||||
size = FTFx_PSECTOR_SIZE;
|
||||
ret = FlashEraseSector(&flashSSDConfig, dest, size, g_FlashLaunchCommand);
|
||||
if (FTFx_OK != ret)
|
||||
{
|
||||
ErrorTrap(ret);
|
||||
}
|
||||
|
||||
/* 检验 */
|
||||
number = FTFx_PSECTOR_SIZE / PRD1SEC_ALIGN_SIZE;
|
||||
for(uint8_t i = 0x0U; i < 0x2U; i++)
|
||||
{
|
||||
ret = FlashVerifySection(&flashSSDConfig, dest, number, i, g_FlashLaunchCommand);
|
||||
if (FTFx_OK != ret)
|
||||
{
|
||||
ErrorTrap(ret);
|
||||
}
|
||||
}
|
||||
dest += BYTE2WORD(size);
|
||||
}
|
||||
|
||||
/*
|
||||
* 写入数据
|
||||
*/
|
||||
dest = flashSSDConfig.PFlashBlockBase + BYTE2WORD(flashSSDConfig.PFlashBlockSize
|
||||
- (uint32_t)(USED_SECTOR_NUM * FTFx_PSECTOR_SIZE));
|
||||
|
||||
if ((dest + BYTE2WORD(size)) <= (flashSSDConfig.PFlashBlockBase
|
||||
+ BYTE2WORD(flashSSDConfig.PFlashBlockSize)))
|
||||
{
|
||||
ret = FlashProgram(&flashSSDConfig, dest, nbyte, \
|
||||
saddr, g_FlashLaunchCommand);
|
||||
if (FTFx_OK != ret)
|
||||
{
|
||||
ErrorTrap(ret);
|
||||
}
|
||||
|
||||
/* 校验和检验 */
|
||||
uint32_t sum = temp = 0x0U;
|
||||
for (uint8_t i = 0x0U; i < nbyte; i++)
|
||||
{
|
||||
temp += saddr[i];
|
||||
}
|
||||
ret = FlashCheckSum(&flashSSDConfig, dest, nbyte, &sum);
|
||||
if ((FTFx_OK != ret) || (temp != sum))
|
||||
{
|
||||
ErrorTrap(ret);
|
||||
}
|
||||
|
||||
/* 正确测试 */
|
||||
uint32_t FailAddr;
|
||||
for (uint8_t i = 0x1U; i < 0x3U; i ++)
|
||||
{
|
||||
ret = FlashProgramCheck(&flashSSDConfig, dest, nbyte, saddr, \
|
||||
&FailAddr, i, g_FlashLaunchCommand);
|
||||
if (FTFx_OK != ret)
|
||||
{
|
||||
ErrorTrap(ret);
|
||||
}
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
void flash_read(uint8_t *daddr, uint8_t nbyte)
|
||||
{
|
||||
uint8_t *saddr = (uint8_t *)(flashSSDConfig.PFlashBlockBase + BYTE2WORD(flashSSDConfig.PFlashBlockSize
|
||||
- (uint32_t)(USED_SECTOR_NUM * FTFx_PSECTOR_SIZE)));
|
||||
|
||||
for (uint16_t i = 0; i < nbyte; i++)
|
||||
{
|
||||
daddr[i] = saddr[i];
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
*
|
||||
* Function Name : ErrorTrap
|
||||
* Description : Gets called when an error occurs.
|
||||
* Arguments : uint32_t
|
||||
* Return Value :
|
||||
*
|
||||
*********************************************************************/
|
||||
void ErrorTrap(uint32_t ret)
|
||||
{
|
||||
while (1)
|
||||
{
|
||||
printf("flash操作错误,返回码为 %d\n", ret);
|
||||
}
|
||||
return;
|
||||
}
|
||||
/*
|
||||
* pm_flash.c - flash读写模块
|
||||
*/
|
||||
|
||||
#include "SSD_FTFx.h"
|
||||
|
||||
#include "pm_flash_cfg.h"
|
||||
|
||||
#define USED_SECTOR_NUM 0x01U
|
||||
|
||||
|
||||
FLASH_SSD_CONFIG flashSSDConfig =
|
||||
{
|
||||
FTFx_REG_BASE, /* FTFx control register base */
|
||||
PFLASH_BLOCK_BASE, /* base address of PFlash block */
|
||||
PBLOCK_SIZE, /* size of PFlash block */
|
||||
DEFLASH_BLOCK_BASE, /* base address of DFlash block */
|
||||
0x0U, /* size of DFlash block */
|
||||
EERAM_BLOCK_BASE, /* base address of EERAM block */
|
||||
0x0U, /* size of EEE block */
|
||||
DEBUGENABLE, /* background debug mode enable bit */
|
||||
NULL_CALLBACK /* pointer to callback function */
|
||||
};
|
||||
|
||||
#define LAUNCH_CMD_SIZE 0x80U
|
||||
|
||||
pFLASHCOMMANDSEQUENCE g_FlashLaunchCommand = (pFLASHCOMMANDSEQUENCE)0xFFFFFFFF;
|
||||
|
||||
/* array to copy __Launch_Command func to RAM */
|
||||
uint16_t __ram_func[LAUNCH_CMD_SIZE/2];
|
||||
|
||||
void pm_flash_init(void)
|
||||
{
|
||||
uint32_t ret;
|
||||
|
||||
g_FlashLaunchCommand =
|
||||
(pFLASHCOMMANDSEQUENCE)RelocateFunction((uint32_t)__ram_func ,
|
||||
LAUNCH_CMD_SIZE ,(uint32_t)FlashCommandSequence);
|
||||
ret = FlashInit(&flashSSDConfig);
|
||||
if (FTFx_OK != ret)
|
||||
{
|
||||
ErrorTrap(ret);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
* flash_write() - 将数据写入flash
|
||||
* @saddr: 要写入的数据的起始地址
|
||||
* @size: 写入数据的字节数,字节数必须要以4对齐!!!
|
||||
*
|
||||
* 这个函数的执行过程包括擦除和写入,默认的写入范围为flash的最后一个扇区,大小为
|
||||
* 1KB,如果要改变大小,改参数就可以了,注意不要覆盖了code,具体可看map文件。
|
||||
* 擦除是一次性擦除全部选择的扇区范围,所以在写入的时候要注意保存先前写入的数据
|
||||
* flash读取函数的起始地址等于flash写入函数的写入起始地址
|
||||
*/
|
||||
void flash_write(uint8_t *saddr, uint16_t nbyte)
|
||||
{
|
||||
uint32_t ret;
|
||||
uint32_t dest;
|
||||
uint32_t size;
|
||||
uint16_t number;
|
||||
uint32_t temp;
|
||||
|
||||
/*
|
||||
* 擦除扇区
|
||||
*/
|
||||
dest = flashSSDConfig.PFlashBlockBase + BYTE2WORD(
|
||||
flashSSDConfig.PFlashBlockSize
|
||||
- USED_SECTOR_NUM * FTFx_PSECTOR_SIZE);
|
||||
while ((dest + BYTE2WORD(FTFx_PSECTOR_SIZE)) <=
|
||||
(flashSSDConfig.PFlashBlockBase
|
||||
+ BYTE2WORD(flashSSDConfig.PFlashBlockSize)))
|
||||
{
|
||||
size = FTFx_PSECTOR_SIZE;
|
||||
ret = FlashEraseSector(&flashSSDConfig, dest, size,
|
||||
g_FlashLaunchCommand);
|
||||
if (FTFx_OK != ret)
|
||||
{
|
||||
ErrorTrap(ret);
|
||||
}
|
||||
|
||||
/* 检验 */
|
||||
number = FTFx_PSECTOR_SIZE / PRD1SEC_ALIGN_SIZE;
|
||||
for(uint8_t i = 0x0U; i < 0x2U; i++)
|
||||
{
|
||||
ret = FlashVerifySection(&flashSSDConfig, dest, number, i,
|
||||
g_FlashLaunchCommand);
|
||||
if (FTFx_OK != ret)
|
||||
{
|
||||
ErrorTrap(ret);
|
||||
}
|
||||
}
|
||||
dest += BYTE2WORD(size);
|
||||
}
|
||||
|
||||
/*
|
||||
* 写入数据
|
||||
*/
|
||||
dest = flashSSDConfig.PFlashBlockBase + BYTE2WORD(
|
||||
flashSSDConfig.PFlashBlockSize
|
||||
- (uint32_t)(USED_SECTOR_NUM * FTFx_PSECTOR_SIZE));
|
||||
|
||||
if ((dest + BYTE2WORD(size)) <= (flashSSDConfig.PFlashBlockBase
|
||||
+ BYTE2WORD(flashSSDConfig.PFlashBlockSize)))
|
||||
{
|
||||
ret = FlashProgram(&flashSSDConfig, dest, nbyte, \
|
||||
saddr, g_FlashLaunchCommand);
|
||||
if (FTFx_OK != ret)
|
||||
{
|
||||
ErrorTrap(ret);
|
||||
}
|
||||
|
||||
/* 校验和检验 */
|
||||
uint32_t sum = temp = 0x0U;
|
||||
for (uint16_t i = 0x0U; i < nbyte; i++)
|
||||
{
|
||||
temp += saddr[i];
|
||||
}
|
||||
ret = FlashCheckSum(&flashSSDConfig, dest, nbyte, &sum);
|
||||
if ((FTFx_OK != ret) || (temp != sum))
|
||||
{
|
||||
ErrorTrap(ret);
|
||||
}
|
||||
|
||||
/* 正确测试 */
|
||||
uint32_t FailAddr;
|
||||
for (uint8_t i = 0x1U; i < 0x3U; i ++)
|
||||
{
|
||||
ret = FlashProgramCheck(&flashSSDConfig, dest, nbyte, saddr, \
|
||||
&FailAddr, i, g_FlashLaunchCommand);
|
||||
if (FTFx_OK != ret)
|
||||
{
|
||||
ErrorTrap(ret);
|
||||
}
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
* flash_read() - 读取flash中写入的数据
|
||||
* @daddr: 读取数据要保存的地址
|
||||
* @nbyte: 要读取的字节数
|
||||
*
|
||||
* flash读取函数的起始地址等于flash写入函数的写入起始地址
|
||||
*/
|
||||
void flash_read(uint8_t *daddr, uint16_t nbyte)
|
||||
{
|
||||
uint8_t *saddr = (uint8_t *)(flashSSDConfig.PFlashBlockBase
|
||||
+ BYTE2WORD(flashSSDConfig.PFlashBlockSize
|
||||
- (uint32_t)(USED_SECTOR_NUM * FTFx_PSECTOR_SIZE)));
|
||||
|
||||
for (uint16_t i = 0; i < nbyte; i++)
|
||||
{
|
||||
daddr[i] = saddr[i];
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
* ErrorTrap() - flash操作错误处理
|
||||
*/
|
||||
void ErrorTrap(uint32_t ret)
|
||||
{
|
||||
while (1)
|
||||
{
|
||||
printf("flash操作错误,返回码为 %d\n", ret);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
|
@ -11,6 +11,7 @@
|
|||
#include "include/pm_time.h"
|
||||
#include "include/plan_handle.h"
|
||||
#include "include/pm_flash.h"
|
||||
#include "include/orient.h"
|
||||
|
||||
|
||||
/*
|
||||
|
|
@ -18,13 +19,14 @@
|
|||
*/
|
||||
void pm_init(void)
|
||||
{
|
||||
pm_flash_init();
|
||||
debug_init();
|
||||
key_init();
|
||||
knob_init();
|
||||
st_base_init();
|
||||
tft_init();
|
||||
ds1302_init();
|
||||
plan_handle_init();
|
||||
pm_flash_init();
|
||||
plan_handle_init(); // 计划处理初始化必须在flash初始化之后
|
||||
orient_init();
|
||||
return;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,322 +1,394 @@
|
|||
/*
|
||||
* pm_time.c - 时间处理
|
||||
*/
|
||||
|
||||
#include "gpio.h"
|
||||
|
||||
#include "include/pm_time.h"
|
||||
#include "include/config.h"
|
||||
|
||||
|
||||
|
||||
static calendar_info system_time;
|
||||
|
||||
enum { START_YEAR = 2000, SEC_IN_DAY = 24 * 60 * 60};
|
||||
#define DAY_IN_YEAR(nyear) (is_leapyear(nyear) ? 366 : 365)
|
||||
|
||||
static const uint8_t day_leap[] = {
|
||||
0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
|
||||
};
|
||||
|
||||
static const uint8_t day_noleap[] = {
|
||||
0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
|
||||
};
|
||||
|
||||
|
||||
|
||||
static void ds1302_byte_write(uint8_t data);
|
||||
static uint8_t ds1302_byte_read(void);
|
||||
static uint8_t ds1302_single_read(uint8_t addr);
|
||||
static uint8_t ds1302_single_read(uint8_t addr);
|
||||
static int16_t ymd_to_wday(int16_t year, int16_t month, int16_t mday);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
uint8_t is_leapyear(uint16_t year)
|
||||
{
|
||||
return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
|
||||
}
|
||||
|
||||
uint8_t get_month_days(uint16_t year, uint8_t month)
|
||||
{
|
||||
switch (month)
|
||||
{
|
||||
case 4:
|
||||
case 6:
|
||||
case 9:
|
||||
case 11:
|
||||
return 30;
|
||||
case 2:
|
||||
if (is_leapyear(year))
|
||||
return 29;
|
||||
else
|
||||
return 28;
|
||||
default:
|
||||
return 31;
|
||||
}
|
||||
}
|
||||
|
||||
static void ds1302_byte_write(uint8_t data)
|
||||
{
|
||||
gpio_init(DS1302_IO_PINX, 1, 0);
|
||||
for (uint8_t mask = 0x01; mask != 0; mask <<= 1)
|
||||
{
|
||||
gpio_set(DS1302_CLK_PINX, 0);
|
||||
if (data & mask)
|
||||
{
|
||||
gpio_set(DS1302_IO_PINX, 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
gpio_set(DS1302_IO_PINX, 0);
|
||||
}
|
||||
// tdc,200ns,数据建立时间,tcl,1000ns,sclk低电平保持时间
|
||||
for (uint8_t i = 0; i < 50; i++);
|
||||
gpio_set(DS1302_CLK_PINX, 1);
|
||||
// tcdh,280ns,数据采集时间,tch,1000ns,sclk高电平保持时间
|
||||
for (uint8_t i = 0; i < 50; i++);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
static uint8_t ds1302_byte_read(void)
|
||||
{
|
||||
uint8_t data = 0;
|
||||
|
||||
//gpio_init(DS1302_IO_PINX, 0, 1);
|
||||
gpio_Interrupt_init(DS1302_IO_PINX, GPI_UP_PF, GPI_DISAB);
|
||||
gpio_set(DS1302_CLK_PINX, 1);
|
||||
// tccz,280ns,sclk到高阻态
|
||||
for (uint8_t i = 0; i < 50; i++);
|
||||
for (uint8_t mask = 0x01; mask != 0; mask <<= 1)
|
||||
{
|
||||
gpio_set(DS1302_CLK_PINX, 0); // 产生下降沿
|
||||
// tcdd,800ns,数据输出延迟
|
||||
for (uint8_t i = 0; i < 50; i++);
|
||||
for (uint8_t i = 0; i < 50; i++);
|
||||
if (gpio_get(DS1302_IO_PINX))
|
||||
{
|
||||
data |= mask;
|
||||
}
|
||||
gpio_set(DS1302_CLK_PINX, 1);
|
||||
// tccz,280ns,sclk到高阻态
|
||||
for (uint8_t i = 0; i < 50; i++);
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
static uint8_t ds1302_single_read(uint8_t addr)
|
||||
{
|
||||
uint8_t cmd = 0,
|
||||
data = 0;
|
||||
|
||||
cmd = (1 << 7) | (addr << 1) | 1;
|
||||
/*
|
||||
* 初始化ce和clk状态
|
||||
*/
|
||||
gpio_set(DS1302_CE_PINX, 0);
|
||||
for (uint8_t i = 0; i < 50; i++);
|
||||
gpio_set(DS1302_CLK_PINX, 0);
|
||||
for (uint8_t i = 0; i < 50; i++);
|
||||
|
||||
gpio_set(DS1302_CE_PINX, 1);
|
||||
// tcc,ce到时钟建立时间,4us
|
||||
for (uint8_t i = 0; i < 200; i++);
|
||||
ds1302_byte_write(cmd);
|
||||
data = ds1302_byte_read();
|
||||
gpio_set(DS1302_CE_PINX, 0);
|
||||
// tcdz,ce到高阻态时间,280ns
|
||||
for (uint8_t i = 0; i < 20; i++);
|
||||
return data;
|
||||
}
|
||||
|
||||
void ds1302_single_write(uint8_t addr, uint8_t data)
|
||||
{
|
||||
uint8_t cmd = 0;
|
||||
|
||||
cmd = (1 << 7) | (addr << 1);
|
||||
/*
|
||||
* 初始化ce和clk状态
|
||||
*/
|
||||
gpio_set(DS1302_CE_PINX, 0);
|
||||
for (uint8_t i = 0; i < 50; i++);
|
||||
gpio_set(DS1302_CLK_PINX, 0);
|
||||
for (uint8_t i = 0; i < 50; i++);
|
||||
|
||||
gpio_set(DS1302_CE_PINX, 1);
|
||||
// tcc,ce到时钟建立时间,4us
|
||||
for (uint8_t i = 0; i < 200; i++);
|
||||
ds1302_byte_write(cmd);
|
||||
ds1302_byte_write(data);
|
||||
gpio_set(DS1302_CE_PINX, 0);
|
||||
return;
|
||||
}
|
||||
|
||||
void ds1302_set_time(calendar_info *cal)
|
||||
{
|
||||
ds1302_single_write(7, 0x00);
|
||||
ds1302_single_write(0, cal->sec % 10 + (cal->sec / 10 << 4));
|
||||
ds1302_single_write(1, cal->min % 10 + (cal->min / 10 << 4));
|
||||
ds1302_single_write(2, cal->hour % 10 + (cal->hour / 10 << 4));
|
||||
ds1302_single_write(3, cal->mday % 10 + (cal->mday / 10 << 4));
|
||||
ds1302_single_write(4, cal->month % 10 + (cal->month / 10 << 4));
|
||||
ds1302_single_write(5, cal->wday);
|
||||
ds1302_single_write(6, (cal->year - 2000) % 10 + ((cal->year - 2000) / 10 << 4));
|
||||
return;
|
||||
}
|
||||
|
||||
void ds1302_read_time(calendar_info *cal)
|
||||
{
|
||||
uint8_t rval;
|
||||
|
||||
rval = ds1302_single_read(0);
|
||||
cal->sec = (rval & 0x0f) + ((rval & 0x70) >> 4) * 10;
|
||||
rval = ds1302_single_read(1);
|
||||
cal->min = (rval & 0x0f) + ((rval & 0x70) >> 4) * 10;
|
||||
rval = ds1302_single_read(2);
|
||||
cal->hour = (rval & 0x0f) + ((rval & 0x30) >> 4) * 10;
|
||||
rval = ds1302_single_read(3);
|
||||
cal->mday = (rval & 0x0f) + ((rval & 0x10) >> 4) * 10;
|
||||
rval = ds1302_single_read(4);
|
||||
cal->month = (rval & 0x0f) + ((rval & 0x10) >> 4) * 10;
|
||||
rval = ds1302_single_read(5);
|
||||
cal->wday = rval & 0x07;
|
||||
rval = ds1302_single_read(6);
|
||||
cal->year = (rval & 0x0f) + ((rval & 0xf0) >> 4) * 10 + 2000;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void ds1302_init(void)
|
||||
{
|
||||
//gpio_Interrupt_init(DS1302_CE_PINX, GPO, GPI_DISAB);
|
||||
//gpio_Interrupt_init(DS1302_CLK_PINX, GPO, GPI_DISAB);
|
||||
gpio_init(DS1302_CE_PINX, 1, 0);
|
||||
gpio_init(DS1302_CLK_PINX, 1, 0);
|
||||
gpio_Interrupt_init(DS1302_IO_PINX, GPI_UP_PF, GPI_DISAB);
|
||||
ds1302_single_write(7, 0x00);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* 这个函数需要按所需的时间精度来定时调用
|
||||
*/
|
||||
void maintain_system_time(void)
|
||||
{
|
||||
enter_critical();
|
||||
ds1302_read_time(&system_time);
|
||||
exit_critical();
|
||||
return;
|
||||
}
|
||||
|
||||
calendar_info get_system_time(void)
|
||||
{
|
||||
return system_time;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*************************************************************/
|
||||
|
||||
|
||||
uint32_t calendar_to_sec(calendar_info *cal)
|
||||
{
|
||||
uint32_t sec = cal->sec;
|
||||
uint32_t year = cal->year;
|
||||
uint32_t month = cal->month;
|
||||
|
||||
if (year < START_YEAR || year > (START_YEAR + 135))
|
||||
return 0;
|
||||
|
||||
sec += (uint32_t)cal->min * 60;
|
||||
sec += (uint32_t)cal->hour * 3600;
|
||||
sec += (uint32_t)(cal->mday - 1) * SEC_IN_DAY;
|
||||
if (is_leapyear(year))
|
||||
{
|
||||
while (month > 1)
|
||||
{
|
||||
sec += (uint32_t)day_leap[--month] * SEC_IN_DAY;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
while (month > 1)
|
||||
{
|
||||
sec += (uint32_t)day_noleap[--month] * SEC_IN_DAY;
|
||||
}
|
||||
}
|
||||
while (year > START_YEAR)
|
||||
{
|
||||
sec += (uint32_t)DAY_IN_YEAR(--year) * SEC_IN_DAY;
|
||||
}
|
||||
return sec;
|
||||
}
|
||||
|
||||
calendar_info sec_to_calendar(uint32_t sec)
|
||||
{
|
||||
calendar_info cal;
|
||||
uint32_t day, left;
|
||||
|
||||
cal.year = START_YEAR;
|
||||
cal.month = 1;
|
||||
cal.mday = 1;
|
||||
cal.yday = 1;
|
||||
cal.wday = ymd_to_wday(START_YEAR, 1, 1);
|
||||
|
||||
day = sec / SEC_IN_DAY;
|
||||
left = sec % SEC_IN_DAY;
|
||||
|
||||
cal.wday = (day + cal.wday) % 7;
|
||||
|
||||
cal.hour = left / 3600;
|
||||
cal.min = left / 60 % 60;
|
||||
cal.sec = left % 60;
|
||||
|
||||
while (day >= DAY_IN_YEAR(cal.year))
|
||||
{
|
||||
day -= DAY_IN_YEAR(cal.year++);
|
||||
}
|
||||
cal.yday += day;
|
||||
|
||||
if (is_leapyear(cal.year))
|
||||
{
|
||||
while (day >= day_leap[cal.month])
|
||||
{
|
||||
day -= day_leap[cal.month++];
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
while (day >= day_noleap[cal.month])
|
||||
{
|
||||
day -= day_noleap[cal.month++];
|
||||
}
|
||||
}
|
||||
cal.mday += day;
|
||||
|
||||
return cal;
|
||||
}
|
||||
|
||||
|
||||
static int16_t ymd_to_wday(int16_t year, int16_t month, int16_t mday)
|
||||
{
|
||||
if (is_leapyear(year))
|
||||
{
|
||||
for (uint8_t i = 1; i < month; i++)
|
||||
mday += day_leap[i];
|
||||
}
|
||||
else
|
||||
{
|
||||
for (uint8_t i = 1; i < month; i++)
|
||||
mday += day_noleap[i];
|
||||
}
|
||||
return (year + year / 4 - year / 100 + year / 400 + mday) % 7;
|
||||
}
|
||||
/*
|
||||
* pm_time.c - 时间处理
|
||||
*/
|
||||
|
||||
#include "gpio.h"
|
||||
|
||||
#include "include/pm_time.h"
|
||||
#include "include/config.h"
|
||||
|
||||
|
||||
// 被维护的系统时间
|
||||
static calendar_info system_time;
|
||||
|
||||
#define DAY_IN_YEAR(nyear) (is_leapyear(nyear) ? 366 : 365)
|
||||
|
||||
static const uint8_t day_leap[] = {
|
||||
0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
|
||||
};
|
||||
|
||||
static const uint8_t day_noleap[] = {
|
||||
0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
|
||||
};
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* 静态函数声明
|
||||
*/
|
||||
static void ds1302_byte_write(uint8_t data);
|
||||
static uint8_t ds1302_byte_read(void);
|
||||
static uint8_t ds1302_single_read(uint8_t addr);
|
||||
void ds1302_single_write(uint8_t addr, uint8_t data);
|
||||
static int16_t ymd_to_wday(int16_t year, int16_t month, int16_t mday);
|
||||
|
||||
|
||||
/**
|
||||
* ds1302_init() - ds1302的初始化
|
||||
*
|
||||
* 包括io的初始化以及当ds1302掉电时对其时间值设置初始值
|
||||
*/
|
||||
void ds1302_init(void)
|
||||
{
|
||||
//gpio_Interrupt_init(DS1302_CE_PINX, GPO, GPI_DISAB);
|
||||
//gpio_Interrupt_init(DS1302_CLK_PINX, GPO, GPI_DISAB);
|
||||
gpio_init(DS1302_CE_PINX, 1, 0);
|
||||
gpio_init(DS1302_CLK_PINX, 1, 0);
|
||||
gpio_Interrupt_init(DS1302_IO_PINX, GPI_UP_PF, GPI_DISAB);
|
||||
|
||||
// 使能写
|
||||
ds1302_single_write(7, 0x00);
|
||||
// 当ds1302掉电时设置时间初始值
|
||||
if ((ds1302_single_read(0) & 0x80) == 1)
|
||||
{
|
||||
calendar_info sys_cal = { 0, 1, 2, 3, 4, 2012, 0, 1 };
|
||||
ds1302_set_time(&sys_cal);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* is_leapyear() - 判断是否为闰年
|
||||
* @year: 要判断的年份
|
||||
*
|
||||
* 是闰年返回1,不是闰年返回0
|
||||
*/
|
||||
uint8_t is_leapyear(uint16_t year)
|
||||
{
|
||||
return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* get_month_days() - 得到给定年份和月份时对应月份的天数
|
||||
* @year: 给定年份
|
||||
* @month: 给定月份
|
||||
*
|
||||
* 返回值为对应的月份的天数
|
||||
*/
|
||||
uint8_t get_month_days(uint16_t year, uint8_t month)
|
||||
{
|
||||
switch (month)
|
||||
{
|
||||
case 4:
|
||||
case 6:
|
||||
case 9:
|
||||
case 11:
|
||||
return 30;
|
||||
case 2:
|
||||
if (is_leapyear(year))
|
||||
return 29;
|
||||
else
|
||||
return 28;
|
||||
default:
|
||||
return 31;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* ds1302_byte_write() - 向ds1302写入一个字节
|
||||
* @data: 要写入的字节
|
||||
*/
|
||||
static void ds1302_byte_write(uint8_t data)
|
||||
{
|
||||
gpio_init(DS1302_IO_PINX, 1, 0);
|
||||
for (uint8_t mask = 0x01; mask != 0; mask <<= 1)
|
||||
{
|
||||
gpio_set(DS1302_CLK_PINX, 0);
|
||||
if (data & mask)
|
||||
{
|
||||
gpio_set(DS1302_IO_PINX, 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
gpio_set(DS1302_IO_PINX, 0);
|
||||
}
|
||||
// tdc,200ns,数据建立时间,tcl,1000ns,sclk低电平保持时间
|
||||
for (uint8_t i = 0; i < 50; i++);
|
||||
gpio_set(DS1302_CLK_PINX, 1);
|
||||
// tcdh,280ns,数据采集时间,tch,1000ns,sclk高电平保持时间
|
||||
for (uint8_t i = 0; i < 50; i++);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* ds1302_byte_read() - 从ds1302读取一个字节
|
||||
*
|
||||
* 返回读取到的字节
|
||||
*/
|
||||
static uint8_t ds1302_byte_read(void)
|
||||
{
|
||||
uint8_t data = 0;
|
||||
|
||||
//gpio_init(DS1302_IO_PINX, 0, 1);
|
||||
gpio_Interrupt_init(DS1302_IO_PINX, GPI_UP_PF, GPI_DISAB);
|
||||
gpio_set(DS1302_CLK_PINX, 1);
|
||||
// tccz,280ns,sclk到高阻态
|
||||
for (uint8_t i = 0; i < 50; i++);
|
||||
for (uint8_t mask = 0x01; mask != 0; mask <<= 1)
|
||||
{
|
||||
gpio_set(DS1302_CLK_PINX, 0); // 产生下降沿
|
||||
// tcdd,800ns,数据输出延迟
|
||||
for (uint8_t i = 0; i < 50; i++);
|
||||
for (uint8_t i = 0; i < 50; i++);
|
||||
if (gpio_get(DS1302_IO_PINX))
|
||||
{
|
||||
data |= mask;
|
||||
}
|
||||
gpio_set(DS1302_CLK_PINX, 1);
|
||||
// tccz,280ns,sclk到高阻态
|
||||
for (uint8_t i = 0; i < 50; i++);
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* ds1302_single_read() - ds1302在单字节模式下读取一个地址上的数据
|
||||
* @addr: 要读取的地址
|
||||
*
|
||||
* 返回读取的到的字节值
|
||||
*/
|
||||
static uint8_t ds1302_single_read(uint8_t addr)
|
||||
{
|
||||
uint8_t cmd = 0,
|
||||
data = 0;
|
||||
|
||||
cmd = (1 << 7) | (addr << 1) | 1;
|
||||
/*
|
||||
* 初始化ce和clk状态
|
||||
*/
|
||||
gpio_set(DS1302_CE_PINX, 0);
|
||||
for (uint8_t i = 0; i < 50; i++);
|
||||
gpio_set(DS1302_CLK_PINX, 0);
|
||||
for (uint8_t i = 0; i < 50; i++);
|
||||
|
||||
gpio_set(DS1302_CE_PINX, 1);
|
||||
// tcc,ce到时钟建立时间,4us
|
||||
for (uint8_t i = 0; i < 200; i++);
|
||||
ds1302_byte_write(cmd);
|
||||
data = ds1302_byte_read();
|
||||
gpio_set(DS1302_CE_PINX, 0);
|
||||
// tcdz,ce到高阻态时间,280ns
|
||||
for (uint8_t i = 0; i < 20; i++);
|
||||
return data;
|
||||
}
|
||||
|
||||
/**
|
||||
* ds1302_single_write() - 在单字节模式下向ds1302的一个地址上写入一个字节的数据
|
||||
* @addr: 要写入数据的地址
|
||||
* @data: 要写入的数据
|
||||
*/
|
||||
void ds1302_single_write(uint8_t addr, uint8_t data)
|
||||
{
|
||||
uint8_t cmd = 0;
|
||||
|
||||
cmd = (1 << 7) | (addr << 1);
|
||||
/*
|
||||
* 初始化ce和clk状态
|
||||
*/
|
||||
gpio_set(DS1302_CE_PINX, 0);
|
||||
for (uint8_t i = 0; i < 50; i++);
|
||||
gpio_set(DS1302_CLK_PINX, 0);
|
||||
for (uint8_t i = 0; i < 50; i++);
|
||||
|
||||
gpio_set(DS1302_CE_PINX, 1);
|
||||
// tcc,ce到时钟建立时间,4us
|
||||
for (uint8_t i = 0; i < 200; i++);
|
||||
ds1302_byte_write(cmd);
|
||||
ds1302_byte_write(data);
|
||||
gpio_set(DS1302_CE_PINX, 0);
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* ds1302_set_time() - 向ds1302中写入时间
|
||||
* @cal: 要写入的时间值地址
|
||||
*/
|
||||
void ds1302_set_time(calendar_info *cal)
|
||||
{
|
||||
ds1302_single_write(7, 0x00);
|
||||
ds1302_single_write(0, cal->sec % 10 + (cal->sec / 10 << 4));
|
||||
ds1302_single_write(1, cal->min % 10 + (cal->min / 10 << 4));
|
||||
ds1302_single_write(2, cal->hour % 10 + (cal->hour / 10 << 4));
|
||||
ds1302_single_write(3, cal->mday % 10 + (cal->mday / 10 << 4));
|
||||
ds1302_single_write(4, cal->month % 10 + (cal->month / 10 << 4));
|
||||
ds1302_single_write(5, cal->wday);
|
||||
ds1302_single_write(6, (cal->year - 2000) % 10 + ((cal->year - 2000) / 10 << 4));
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* ds1302_read_time() - 从ds1302中读取时间
|
||||
* @cal: 读取的时间值被存放的地址
|
||||
*/
|
||||
void ds1302_read_time(calendar_info *cal)
|
||||
{
|
||||
uint8_t rval;
|
||||
|
||||
rval = ds1302_single_read(0);
|
||||
cal->sec = (rval & 0x0f) + ((rval & 0x70) >> 4) * 10;
|
||||
rval = ds1302_single_read(1);
|
||||
cal->min = (rval & 0x0f) + ((rval & 0x70) >> 4) * 10;
|
||||
rval = ds1302_single_read(2);
|
||||
cal->hour = (rval & 0x0f) + ((rval & 0x30) >> 4) * 10;
|
||||
rval = ds1302_single_read(3);
|
||||
cal->mday = (rval & 0x0f) + ((rval & 0x30) >> 4) * 10;
|
||||
rval = ds1302_single_read(4);
|
||||
cal->month = (rval & 0x0f) + ((rval & 0x10) >> 4) * 10;
|
||||
rval = ds1302_single_read(5);
|
||||
cal->wday = rval & 0x07;
|
||||
rval = ds1302_single_read(6);
|
||||
cal->year = (rval & 0x0f) + ((rval & 0xf0) >> 4) * 10 + 2000;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* maintain_system_time() - 设置系统时间变量
|
||||
*
|
||||
* 这个函数需要按所需的时间精度来定时调用
|
||||
*/
|
||||
void maintain_system_time(void)
|
||||
{
|
||||
enter_critical();
|
||||
ds1302_read_time(&system_time);
|
||||
exit_critical();
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* get_system_time() - 返回系统时间
|
||||
*/
|
||||
calendar_info get_system_time(void)
|
||||
{
|
||||
return system_time;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* calendar_to_sec() - 分解时间到日历时间的转换
|
||||
* @cal: 被转换的分解时间
|
||||
*
|
||||
* 返回的是日历时间,即从某一个时间点到当前转换时间所经过的秒数
|
||||
*/
|
||||
uint32_t calendar_to_sec(calendar_info *cal)
|
||||
{
|
||||
uint32_t sec = cal->sec;
|
||||
uint32_t year = cal->year;
|
||||
uint32_t month = cal->month;
|
||||
|
||||
if (year < START_YEAR || year > (START_YEAR + 135))
|
||||
return 0;
|
||||
|
||||
sec += (uint32_t)cal->min * 60;
|
||||
sec += (uint32_t)cal->hour * 3600;
|
||||
sec += (uint32_t)(cal->mday - 1) * SEC_IN_DAY;
|
||||
if (is_leapyear(year))
|
||||
{
|
||||
while (month > 1)
|
||||
{
|
||||
sec += (uint32_t)day_leap[--month] * SEC_IN_DAY;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
while (month > 1)
|
||||
{
|
||||
sec += (uint32_t)day_noleap[--month] * SEC_IN_DAY;
|
||||
}
|
||||
}
|
||||
while (year > START_YEAR)
|
||||
{
|
||||
sec += (uint32_t)DAY_IN_YEAR(--year) * SEC_IN_DAY;
|
||||
}
|
||||
return sec;
|
||||
}
|
||||
|
||||
/**
|
||||
* sec_to_calendar() - 日历时间到分解时间的转换
|
||||
* @cal: 被转换的日历时间
|
||||
*
|
||||
* 返回的是分解时间,即以年月日时分秒形式表示的时间
|
||||
*/
|
||||
calendar_info sec_to_calendar(uint32_t sec)
|
||||
{
|
||||
calendar_info cal;
|
||||
uint32_t day, left;
|
||||
|
||||
cal.year = START_YEAR;
|
||||
cal.month = 1;
|
||||
cal.mday = 1;
|
||||
cal.yday = 1;
|
||||
cal.wday = ymd_to_wday(START_YEAR, 1, 1);
|
||||
|
||||
day = sec / SEC_IN_DAY;
|
||||
left = sec % SEC_IN_DAY;
|
||||
|
||||
cal.wday = (day + cal.wday) % 7;
|
||||
|
||||
cal.hour = left / 3600;
|
||||
cal.min = left / 60 % 60;
|
||||
cal.sec = left % 60;
|
||||
|
||||
while (day >= DAY_IN_YEAR(cal.year))
|
||||
{
|
||||
day -= DAY_IN_YEAR(cal.year++);
|
||||
}
|
||||
cal.yday += day;
|
||||
|
||||
if (is_leapyear(cal.year))
|
||||
{
|
||||
while (day >= day_leap[cal.month])
|
||||
{
|
||||
day -= day_leap[cal.month++];
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
while (day >= day_noleap[cal.month])
|
||||
{
|
||||
day -= day_noleap[cal.month++];
|
||||
}
|
||||
}
|
||||
cal.mday += day;
|
||||
|
||||
return cal;
|
||||
}
|
||||
|
||||
/**
|
||||
* ymd_to_wday() - 从年份、月份、天数这三个数据得到对应在一个星期中的天数
|
||||
* @year: 年份
|
||||
* @month: 月份
|
||||
* @mday: 当前月中已经过的天数
|
||||
*
|
||||
* 返回对应的一个星期中的天数
|
||||
*/
|
||||
static int16_t ymd_to_wday(int16_t year, int16_t month, int16_t mday)
|
||||
{
|
||||
if (is_leapyear(year))
|
||||
{
|
||||
for (uint8_t i = 1; i < month; i++)
|
||||
mday += day_leap[i];
|
||||
}
|
||||
else
|
||||
{
|
||||
for (uint8_t i = 1; i < month; i++)
|
||||
mday += day_noleap[i];
|
||||
}
|
||||
return (year + year / 4 - year / 100 + year / 400 + mday) % 7;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,160 +1,161 @@
|
|||
/*
|
||||
* simulat_timer.h - 实现一个模拟定时器
|
||||
*/
|
||||
|
||||
#include "include.h"
|
||||
|
||||
#include "include/simulat_timer.h"
|
||||
#include "include/config.h"
|
||||
#include "include/key.h"
|
||||
|
||||
static volatile st_register st_r[SIMULAT_TIMER_NUM];
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* st_init() - 初始化实现模拟定时器所需的pit定时器
|
||||
*
|
||||
* 基础定时为1ms
|
||||
*/
|
||||
void st_base_init(void)
|
||||
{
|
||||
uint32_t cnt;
|
||||
|
||||
cnt = bus_clk_khz * 1;
|
||||
pit_init(SIMULAT_TIMER_PITX, cnt); // 1ms硬件定时中断周期
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* st_init() - 初始化一个给定编号的模拟定时器
|
||||
* @n: 定时器编号
|
||||
* @st_m: 定时模式
|
||||
* @cmr_v: 定时器比较值,在非比较模式可以为任意值
|
||||
*
|
||||
* 这个函数返回0表示正常完成了初始化任务,返回~0表示定时器已打开或不存在
|
||||
*/
|
||||
uint8_t st_init(uint8_t n, st_mode st_m, uint32_t cmr_v)
|
||||
{
|
||||
if (st_r[n].flag.ten == 1 || n >= SIMULAT_TIMER_NUM)
|
||||
{
|
||||
return ~0;
|
||||
}
|
||||
st_r[n].flag.ten = 1;
|
||||
switch(st_m)
|
||||
{
|
||||
case COMPARE:
|
||||
st_r[n].flag.tfc = 1;
|
||||
st_r[n].cmr = cmr_v;
|
||||
break;
|
||||
case NO_COMPARE:
|
||||
st_r[n].flag.tfc = 0;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* st_close() - 关闭一个给定编号的模拟定时器
|
||||
* @n: 定时器编号
|
||||
*/
|
||||
void st_close(uint8_t n)
|
||||
{
|
||||
st_r[n].flag.ten = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
* st_tcf() - 查询tcf位返回
|
||||
* @n: 定时器编号
|
||||
*
|
||||
* 这个函数在没有过比较值的时候返回0,已过比较值得时候返回1,对于不存在或未打开
|
||||
*/
|
||||
uint8_t st_tcf(uint8_t n)
|
||||
{
|
||||
uint8_t temp;
|
||||
|
||||
if (st_r[n].flag.tcf == 1)
|
||||
{
|
||||
st_r[n].flag.tcf = 0;
|
||||
temp = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
temp = 0;
|
||||
}
|
||||
return temp;
|
||||
}
|
||||
|
||||
/*
|
||||
* st_tov() - 查询tov位返回
|
||||
* @n: 定时器编号
|
||||
*
|
||||
* 这个函数在没有溢出时返回0,没有溢出时返回1,对于不存在或未打开
|
||||
*/
|
||||
uint8_t st_tov(uint8_t n)
|
||||
{
|
||||
uint8_t temp;
|
||||
if (st_r[n].flag.tov == 1)
|
||||
{
|
||||
st_r[n].flag.tov = 0;
|
||||
temp = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
temp = 0;
|
||||
}
|
||||
return temp;
|
||||
}
|
||||
|
||||
/*
|
||||
* st_count() - 查询当前模拟定时值
|
||||
* @n: 定时器编号
|
||||
*
|
||||
* 对于不存在或未打开
|
||||
*/
|
||||
uint32_t st_count(uint8_t n)
|
||||
{
|
||||
return st_r[n].count;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* st_base() -定时调用以模拟N路定时器
|
||||
*
|
||||
* 定时的最小间隔等于这个函数被调用的周期
|
||||
*/
|
||||
void st_base(void)
|
||||
{
|
||||
uint8_t i;
|
||||
|
||||
for (i = 0; i < SIMULAT_TIMER_NUM; i++)
|
||||
{
|
||||
if (st_r[i].flag.ten == 1) // 使能
|
||||
{
|
||||
if (++st_r[i].count == 0) // 溢出
|
||||
{
|
||||
st_r[i].flag.tov = 1;
|
||||
} // 溢出
|
||||
if (st_r[i].flag.tfc == 1) // 比较
|
||||
{
|
||||
if (st_r[i].count == st_r[i].cmr)
|
||||
{
|
||||
st_r[i].count = 0;
|
||||
st_r[i].flag.tcf = 1;
|
||||
}
|
||||
} // 比较
|
||||
} // 使能
|
||||
}
|
||||
|
||||
if (st_tcf(0) == 1)
|
||||
{
|
||||
key_detect();
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
/*
|
||||
* simulat_timer.h - 实现一个模拟定时器
|
||||
*/
|
||||
|
||||
#include "include.h"
|
||||
|
||||
#include "include/config.h"
|
||||
#include "include/key.h"
|
||||
|
||||
#include "include/simulat_timer.h"
|
||||
|
||||
static volatile st_register st_r[SIMULAT_TIMER_NUM];
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* st_init() - 初始化实现模拟定时器所需的pit定时器
|
||||
*
|
||||
* 基础定时为1ms
|
||||
*/
|
||||
void st_base_init(void)
|
||||
{
|
||||
uint32_t cnt;
|
||||
|
||||
cnt = bus_clk_khz * 1;
|
||||
pit_init(SIMULAT_TIMER_PITX, cnt); // 1ms硬件定时中断周期
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* st_init() - 初始化一个给定编号的模拟定时器
|
||||
* @n: 定时器编号
|
||||
* @st_m: 定时模式
|
||||
* @cmr_v: 定时器比较值,在非比较模式可以为任意值
|
||||
*
|
||||
* 这个函数返回0表示正常完成了初始化任务,返回~0表示定时器已打开或不存在
|
||||
*/
|
||||
uint8_t st_init(uint8_t n, st_mode st_m, uint32_t cmr_v)
|
||||
{
|
||||
if (st_r[n].flag.ten == 1 || n >= SIMULAT_TIMER_NUM)
|
||||
{
|
||||
return ~0;
|
||||
}
|
||||
st_r[n].flag.ten = 1;
|
||||
switch(st_m)
|
||||
{
|
||||
case COMPARE:
|
||||
st_r[n].flag.tfc = 1;
|
||||
st_r[n].cmr = cmr_v;
|
||||
break;
|
||||
case NO_COMPARE:
|
||||
st_r[n].flag.tfc = 0;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* st_close() - 关闭一个给定编号的模拟定时器
|
||||
* @n: 定时器编号
|
||||
*/
|
||||
void st_close(uint8_t n)
|
||||
{
|
||||
st_r[n].flag.ten = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* st_tcf() - 查询tcf位返回
|
||||
* @n: 定时器编号
|
||||
*
|
||||
* 这个函数在没有过比较值的时候返回0,已过比较值得时候返回1,对于不存在或未打开
|
||||
*/
|
||||
uint8_t st_tcf(uint8_t n)
|
||||
{
|
||||
uint8_t temp;
|
||||
|
||||
if (st_r[n].flag.tcf == 1)
|
||||
{
|
||||
st_r[n].flag.tcf = 0;
|
||||
temp = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
temp = 0;
|
||||
}
|
||||
return temp;
|
||||
}
|
||||
|
||||
/**
|
||||
* st_tov() - 查询tov位返回
|
||||
* @n: 定时器编号
|
||||
*
|
||||
* 这个函数在没有溢出时返回0,没有溢出时返回1,对于不存在或未打开
|
||||
*/
|
||||
uint8_t st_tov(uint8_t n)
|
||||
{
|
||||
uint8_t temp;
|
||||
if (st_r[n].flag.tov == 1)
|
||||
{
|
||||
st_r[n].flag.tov = 0;
|
||||
temp = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
temp = 0;
|
||||
}
|
||||
return temp;
|
||||
}
|
||||
|
||||
/**
|
||||
* st_count() - 查询当前模拟定时值
|
||||
* @n: 定时器编号
|
||||
*
|
||||
* 对于不存在或未打开
|
||||
*/
|
||||
uint32_t st_count(uint8_t n)
|
||||
{
|
||||
return st_r[n].count;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* st_base() -定时调用以模拟N路定时器
|
||||
*
|
||||
* 定时的最小间隔等于这个函数被调用的周期
|
||||
*/
|
||||
void st_base(void)
|
||||
{
|
||||
uint8_t i;
|
||||
|
||||
for (i = 0; i < SIMULAT_TIMER_NUM; i++)
|
||||
{
|
||||
if (st_r[i].flag.ten == 1) // 使能
|
||||
{
|
||||
if (++st_r[i].count == 0) // 溢出
|
||||
{
|
||||
st_r[i].flag.tov = 1;
|
||||
} // 溢出
|
||||
if (st_r[i].flag.tfc == 1) // 比较
|
||||
{
|
||||
if (st_r[i].count == st_r[i].cmr)
|
||||
{
|
||||
st_r[i].count = 0;
|
||||
st_r[i].flag.tcf = 1;
|
||||
}
|
||||
} // 比较
|
||||
} // 使能
|
||||
}
|
||||
|
||||
if (st_tcf(0) == 1)
|
||||
{
|
||||
key_detect();
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load diff
41
plan_manage_main/src/app/water.c
Normal file
41
plan_manage_main/src/app/water.c
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
#include <stdint.h>
|
||||
|
||||
#include "adc.h"
|
||||
|
||||
#include "config.h"
|
||||
#include "water.h"
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void water_init(void)
|
||||
{
|
||||
gpio_init(WATER_PINX, 1, 1);
|
||||
adc_init(MOISTURE_ADC0_CHN);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
uint16_t get_moisture(void)
|
||||
{
|
||||
uint16_t res;
|
||||
|
||||
res = adc_ave(MOISTURE_ADC0_CHN, ADC_16bit, 3);
|
||||
return (uint16_t)((float)res / 0xffff * MOISTURE_RATIO + MOISTURE_MIN);
|
||||
}
|
||||
|
||||
void water_ctr(uint16_t sv_moist)
|
||||
{
|
||||
uint16_t crt_moist = get_moisture();
|
||||
|
||||
if (sv_moist > crt_moist)
|
||||
{
|
||||
gpio_set(WATER_PINX, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
gpio_set(WATER_PINX, 1);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
|
@ -18,7 +18,7 @@
|
|||
* OSCILLATO_50 50MH¾§Õñ
|
||||
* OSCILLATO_8 8MH¾§Õñ
|
||||
*/
|
||||
#define OSCILLATO_8
|
||||
#define OSCILLATO_50
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
File diff suppressed because one or more lines are too long
|
|
@ -1,2 +1,2 @@
|
|||
[MainWindow]
|
||||
WindowPlacement=_ 129 165 1154 691 3
|
||||
[MainWindow]
|
||||
WindowPlacement=_ 139 121 1164 647 3
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue