add spi flash and sd for ch32v307
add spi flash and sd for ch32v307
This commit is contained in:
@@ -91,6 +91,7 @@
|
|||||||
<option id="ilg.gnumcueclipse.managedbuild.cross.riscv.option.c.linker.usenewlibnosys.351964161" name="Do not use syscalls (--specs=nosys.specs)" superClass="ilg.gnumcueclipse.managedbuild.cross.riscv.option.c.linker.usenewlibnosys" useByScannerDiscovery="false" value="true" valueType="boolean"/>
|
<option id="ilg.gnumcueclipse.managedbuild.cross.riscv.option.c.linker.usenewlibnosys.351964161" name="Do not use syscalls (--specs=nosys.specs)" superClass="ilg.gnumcueclipse.managedbuild.cross.riscv.option.c.linker.usenewlibnosys" useByScannerDiscovery="false" value="true" valueType="boolean"/>
|
||||||
<option IS_BUILTIN_EMPTY="false" IS_VALUE_EMPTY="true" id="ilg.gnumcueclipse.managedbuild.cross.riscv.option.c.linker.otherobjs.16994550" name="Other objects" superClass="ilg.gnumcueclipse.managedbuild.cross.riscv.option.c.linker.otherobjs" useByScannerDiscovery="false" valueType="userObjs"/>
|
<option IS_BUILTIN_EMPTY="false" IS_VALUE_EMPTY="true" id="ilg.gnumcueclipse.managedbuild.cross.riscv.option.c.linker.otherobjs.16994550" name="Other objects" superClass="ilg.gnumcueclipse.managedbuild.cross.riscv.option.c.linker.otherobjs" useByScannerDiscovery="false" valueType="userObjs"/>
|
||||||
<option IS_BUILTIN_EMPTY="false" IS_VALUE_EMPTY="true" id="ilg.gnumcueclipse.managedbuild.cross.riscv.option.c.linker.flags.1125808200" name="Linker flags (-Xlinker [option])" superClass="ilg.gnumcueclipse.managedbuild.cross.riscv.option.c.linker.flags" useByScannerDiscovery="false" valueType="stringList"/>
|
<option IS_BUILTIN_EMPTY="false" IS_VALUE_EMPTY="true" id="ilg.gnumcueclipse.managedbuild.cross.riscv.option.c.linker.flags.1125808200" name="Linker flags (-Xlinker [option])" superClass="ilg.gnumcueclipse.managedbuild.cross.riscv.option.c.linker.flags" useByScannerDiscovery="false" valueType="stringList"/>
|
||||||
|
<option IS_BUILTIN_EMPTY="false" IS_VALUE_EMPTY="true" id="ilg.gnumcueclipse.managedbuild.cross.riscv.option.c.linker.libs.834465884" name="Libraries (-l)" superClass="ilg.gnumcueclipse.managedbuild.cross.riscv.option.c.linker.libs" useByScannerDiscovery="false" valueType="libs"/>
|
||||||
<inputType id="ilg.gnumcueclipse.managedbuild.cross.riscv.tool.c.linker.input.1859223768" superClass="ilg.gnumcueclipse.managedbuild.cross.riscv.tool.c.linker.input">
|
<inputType id="ilg.gnumcueclipse.managedbuild.cross.riscv.tool.c.linker.input.1859223768" superClass="ilg.gnumcueclipse.managedbuild.cross.riscv.tool.c.linker.input">
|
||||||
<additionalInput kind="additionalinputdependency" paths="$(USER_OBJS)"/>
|
<additionalInput kind="additionalinputdependency" paths="$(USER_OBJS)"/>
|
||||||
<additionalInput kind="additionalinput" paths="$(LIBS)"/>
|
<additionalInput kind="additionalinput" paths="$(LIBS)"/>
|
||||||
|
@@ -1,61 +1,61 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
<?xml version="1.0" encoding="UTF-8" ?>
|
||||||
<launchConfiguration type="ilg.gnumcueclipse.debug.gdbjtag.openocd.launchConfigurationType">
|
<launchConfiguration type="ilg.gnumcueclipse.debug.gdbjtag.openocd.launchConfigurationType">
|
||||||
<booleanAttribute key="ilg.gnumcueclipse.debug.gdbjtag.openocd.doContinue" value="true"/>
|
<booleanAttribute key="ilg.gnumcueclipse.debug.gdbjtag.openocd.doContinue" value="true" />
|
||||||
<booleanAttribute key="ilg.gnumcueclipse.debug.gdbjtag.openocd.doDebugInRam" value="false"/>
|
<booleanAttribute key="ilg.gnumcueclipse.debug.gdbjtag.openocd.doDebugInRam" value="false" />
|
||||||
<booleanAttribute key="ilg.gnumcueclipse.debug.gdbjtag.openocd.doFirstReset" value="true"/>
|
<booleanAttribute key="ilg.gnumcueclipse.debug.gdbjtag.openocd.doFirstReset" value="true" />
|
||||||
<booleanAttribute key="ilg.gnumcueclipse.debug.gdbjtag.openocd.doGdbServerAllocateConsole" value="true"/>
|
<booleanAttribute key="ilg.gnumcueclipse.debug.gdbjtag.openocd.doGdbServerAllocateConsole" value="true" />
|
||||||
<booleanAttribute key="ilg.gnumcueclipse.debug.gdbjtag.openocd.doGdbServerAllocateTelnetConsole" value="false"/>
|
<booleanAttribute key="ilg.gnumcueclipse.debug.gdbjtag.openocd.doGdbServerAllocateTelnetConsole" value="false" />
|
||||||
<booleanAttribute key="ilg.gnumcueclipse.debug.gdbjtag.openocd.doSecondReset" value="true"/>
|
<booleanAttribute key="ilg.gnumcueclipse.debug.gdbjtag.openocd.doSecondReset" value="true" />
|
||||||
<booleanAttribute key="ilg.gnumcueclipse.debug.gdbjtag.openocd.doStartGdbCLient" value="true"/>
|
<booleanAttribute key="ilg.gnumcueclipse.debug.gdbjtag.openocd.doStartGdbCLient" value="true" />
|
||||||
<booleanAttribute key="ilg.gnumcueclipse.debug.gdbjtag.openocd.doStartGdbServer" value="true"/>
|
<booleanAttribute key="ilg.gnumcueclipse.debug.gdbjtag.openocd.doStartGdbServer" value="true" />
|
||||||
<booleanAttribute key="ilg.gnumcueclipse.debug.gdbjtag.openocd.enableSemihosting" value="false"/>
|
<booleanAttribute key="ilg.gnumcueclipse.debug.gdbjtag.openocd.enableSemihosting" value="false" />
|
||||||
<stringAttribute key="ilg.gnumcueclipse.debug.gdbjtag.openocd.firstResetType" value="init"/>
|
<stringAttribute key="ilg.gnumcueclipse.debug.gdbjtag.openocd.firstResetType" value="init" />
|
||||||
<stringAttribute key="ilg.gnumcueclipse.debug.gdbjtag.openocd.gdbClientOtherCommands" value="set mem inaccessible-by-default off set architecture riscv:rv32 set remotetimeout unlimited"/>
|
<stringAttribute key="ilg.gnumcueclipse.debug.gdbjtag.openocd.gdbClientOtherCommands" value="set mem inaccessible-by-default off
set architecture riscv:rv32
set remotetimeout unlimited" />
|
||||||
<stringAttribute key="ilg.gnumcueclipse.debug.gdbjtag.openocd.gdbClientOtherOptions" value=""/>
|
<stringAttribute key="ilg.gnumcueclipse.debug.gdbjtag.openocd.gdbClientOtherOptions" value="" />
|
||||||
<stringAttribute key="ilg.gnumcueclipse.debug.gdbjtag.openocd.gdbServerConnectionAddress" value=""/>
|
<stringAttribute key="ilg.gnumcueclipse.debug.gdbjtag.openocd.gdbServerConnectionAddress" value="" />
|
||||||
<stringAttribute key="ilg.gnumcueclipse.debug.gdbjtag.openocd.gdbServerExecutable" value="${eclipse_home}toolchain/OpenOCD/bin/openocd.exe"/>
|
<stringAttribute key="ilg.gnumcueclipse.debug.gdbjtag.openocd.gdbServerExecutable" value="${eclipse_home}toolchain/OpenOCD/bin/openocd.exe" />
|
||||||
<intAttribute key="ilg.gnumcueclipse.debug.gdbjtag.openocd.gdbServerGdbPortNumber" value="3333"/>
|
<intAttribute key="ilg.gnumcueclipse.debug.gdbjtag.openocd.gdbServerGdbPortNumber" value="3333" />
|
||||||
<stringAttribute key="ilg.gnumcueclipse.debug.gdbjtag.openocd.gdbServerLog" value=""/>
|
<stringAttribute key="ilg.gnumcueclipse.debug.gdbjtag.openocd.gdbServerLog" value="" />
|
||||||
<stringAttribute key="ilg.gnumcueclipse.debug.gdbjtag.openocd.gdbServerOther" value="-f "${eclipse_home}toolchain/OpenOCD/bin/wch-riscv.cfg""/>
|
<stringAttribute key="ilg.gnumcueclipse.debug.gdbjtag.openocd.gdbServerOther" value="-f "${eclipse_home}toolchain/OpenOCD/bin/wch-riscv.cfg"" />
|
||||||
<stringAttribute key="ilg.gnumcueclipse.debug.gdbjtag.openocd.gdbServerTclPortNumber" value="6666"/>
|
<stringAttribute key="ilg.gnumcueclipse.debug.gdbjtag.openocd.gdbServerTclPortNumber" value="6666" />
|
||||||
<intAttribute key="ilg.gnumcueclipse.debug.gdbjtag.openocd.gdbServerTelnetPortNumber" value="4444"/>
|
<intAttribute key="ilg.gnumcueclipse.debug.gdbjtag.openocd.gdbServerTelnetPortNumber" value="4444" />
|
||||||
<stringAttribute key="ilg.gnumcueclipse.debug.gdbjtag.openocd.otherInitCommands" value=""/>
|
<stringAttribute key="ilg.gnumcueclipse.debug.gdbjtag.openocd.otherInitCommands" value="" />
|
||||||
<stringAttribute key="ilg.gnumcueclipse.debug.gdbjtag.openocd.otherRunCommands" value=""/>
|
<stringAttribute key="ilg.gnumcueclipse.debug.gdbjtag.openocd.otherRunCommands" value="" />
|
||||||
<stringAttribute key="ilg.gnumcueclipse.debug.gdbjtag.openocd.secondResetType" value="halt"/>
|
<stringAttribute key="ilg.gnumcueclipse.debug.gdbjtag.openocd.secondResetType" value="halt" />
|
||||||
<stringAttribute key="ilg.gnumcueclipse.debug.gdbjtag.svdPath" value="${eclipse_home}template/wizard/WCH/RISC-V/CH32V307/NoneOS/CH32V307xx.svd"/>
|
<stringAttribute key="ilg.gnumcueclipse.debug.gdbjtag.svdPath" value="${eclipse_home}template/wizard/WCH/RISC-V/CH32V307/NoneOS/CH32V307xx.svd" />
|
||||||
<stringAttribute key="org.eclipse.cdt.debug.gdbjtag.core.imageFileName" value=""/>
|
<stringAttribute key="org.eclipse.cdt.debug.gdbjtag.core.imageFileName" value="" />
|
||||||
<stringAttribute key="org.eclipse.cdt.debug.gdbjtag.core.imageOffset" value=""/>
|
<stringAttribute key="org.eclipse.cdt.debug.gdbjtag.core.imageOffset" value="" />
|
||||||
<stringAttribute key="org.eclipse.cdt.debug.gdbjtag.core.ipAddress" value="localhost"/>
|
<stringAttribute key="org.eclipse.cdt.debug.gdbjtag.core.ipAddress" value="localhost" />
|
||||||
<stringAttribute key="org.eclipse.cdt.debug.gdbjtag.core.jtagDevice" value="GNU MCU OpenOCD"/>
|
<stringAttribute key="org.eclipse.cdt.debug.gdbjtag.core.jtagDevice" value="GNU MCU OpenOCD" />
|
||||||
<booleanAttribute key="org.eclipse.cdt.debug.gdbjtag.core.loadImage" value="true"/>
|
<booleanAttribute key="org.eclipse.cdt.debug.gdbjtag.core.loadImage" value="true" />
|
||||||
<booleanAttribute key="org.eclipse.cdt.debug.gdbjtag.core.loadSymbols" value="true"/>
|
<booleanAttribute key="org.eclipse.cdt.debug.gdbjtag.core.loadSymbols" value="true" />
|
||||||
<stringAttribute key="org.eclipse.cdt.debug.gdbjtag.core.pcRegister" value=""/>
|
<stringAttribute key="org.eclipse.cdt.debug.gdbjtag.core.pcRegister" value="" />
|
||||||
<intAttribute key="org.eclipse.cdt.debug.gdbjtag.core.portNumber" value="3333"/>
|
<intAttribute key="org.eclipse.cdt.debug.gdbjtag.core.portNumber" value="3333" />
|
||||||
<booleanAttribute key="org.eclipse.cdt.debug.gdbjtag.core.setPcRegister" value="false"/>
|
<booleanAttribute key="org.eclipse.cdt.debug.gdbjtag.core.setPcRegister" value="false" />
|
||||||
<booleanAttribute key="org.eclipse.cdt.debug.gdbjtag.core.setResume" value="false"/>
|
<booleanAttribute key="org.eclipse.cdt.debug.gdbjtag.core.setResume" value="false" />
|
||||||
<booleanAttribute key="org.eclipse.cdt.debug.gdbjtag.core.setStopAt" value="true"/>
|
<booleanAttribute key="org.eclipse.cdt.debug.gdbjtag.core.setStopAt" value="true" />
|
||||||
<stringAttribute key="org.eclipse.cdt.debug.gdbjtag.core.stopAt" value="handle_reset"/>
|
<stringAttribute key="org.eclipse.cdt.debug.gdbjtag.core.stopAt" value="handle_reset" />
|
||||||
<stringAttribute key="org.eclipse.cdt.debug.gdbjtag.core.symbolsFileName" value=""/>
|
<stringAttribute key="org.eclipse.cdt.debug.gdbjtag.core.symbolsFileName" value="" />
|
||||||
<stringAttribute key="org.eclipse.cdt.debug.gdbjtag.core.symbolsOffset" value=""/>
|
<stringAttribute key="org.eclipse.cdt.debug.gdbjtag.core.symbolsOffset" value="" />
|
||||||
<booleanAttribute key="org.eclipse.cdt.debug.gdbjtag.core.useFileForImage" value="false"/>
|
<booleanAttribute key="org.eclipse.cdt.debug.gdbjtag.core.useFileForImage" value="false" />
|
||||||
<booleanAttribute key="org.eclipse.cdt.debug.gdbjtag.core.useFileForSymbols" value="false"/>
|
<booleanAttribute key="org.eclipse.cdt.debug.gdbjtag.core.useFileForSymbols" value="false" />
|
||||||
<booleanAttribute key="org.eclipse.cdt.debug.gdbjtag.core.useProjBinaryForImage" value="true"/>
|
<booleanAttribute key="org.eclipse.cdt.debug.gdbjtag.core.useProjBinaryForImage" value="true" />
|
||||||
<booleanAttribute key="org.eclipse.cdt.debug.gdbjtag.core.useProjBinaryForSymbols" value="true"/>
|
<booleanAttribute key="org.eclipse.cdt.debug.gdbjtag.core.useProjBinaryForSymbols" value="true" />
|
||||||
<booleanAttribute key="org.eclipse.cdt.debug.gdbjtag.core.useRemoteTarget" value="true"/>
|
<booleanAttribute key="org.eclipse.cdt.debug.gdbjtag.core.useRemoteTarget" value="true" />
|
||||||
<stringAttribute key="org.eclipse.cdt.dsf.gdb.DEBUG_NAME" value="${eclipse_home}toolchain/RISC-V Embedded GCC/bin/riscv-none-embed-gdb.exe"/>
|
<stringAttribute key="org.eclipse.cdt.dsf.gdb.DEBUG_NAME" value="${eclipse_home}toolchain/RISC-V Embedded GCC/bin/riscv-none-embed-gdb.exe" />
|
||||||
<booleanAttribute key="org.eclipse.cdt.dsf.gdb.UPDATE_THREADLIST_ON_SUSPEND" value="false"/>
|
<booleanAttribute key="org.eclipse.cdt.dsf.gdb.UPDATE_THREADLIST_ON_SUSPEND" value="false" />
|
||||||
<intAttribute key="org.eclipse.cdt.launch.ATTR_BUILD_BEFORE_LAUNCH_ATTR" value="2"/>
|
<intAttribute key="org.eclipse.cdt.launch.ATTR_BUILD_BEFORE_LAUNCH_ATTR" value="2" />
|
||||||
<stringAttribute key="org.eclipse.cdt.launch.COREFILE_PATH" value=""/>
|
<stringAttribute key="org.eclipse.cdt.launch.COREFILE_PATH" value="" />
|
||||||
<stringAttribute key="org.eclipse.cdt.launch.PROGRAM_NAME" value="obj\TencentOS-Tiny.elf"/>
|
<stringAttribute key="org.eclipse.cdt.launch.PROGRAM_NAME" value="obj\TencentOS-Tiny.elf" />
|
||||||
<stringAttribute key="org.eclipse.cdt.launch.PROJECT_ATTR" value="TencentOS-Tiny"/>
|
<stringAttribute key="org.eclipse.cdt.launch.PROJECT_ATTR" value="TencentOS-Tiny" />
|
||||||
<booleanAttribute key="org.eclipse.cdt.launch.PROJECT_BUILD_CONFIG_AUTO_ATTR" value="true"/>
|
<booleanAttribute key="org.eclipse.cdt.launch.PROJECT_BUILD_CONFIG_AUTO_ATTR" value="true" />
|
||||||
<stringAttribute key="org.eclipse.cdt.launch.PROJECT_BUILD_CONFIG_ID_ATTR" value=""/>
|
<stringAttribute key="org.eclipse.cdt.launch.PROJECT_BUILD_CONFIG_ID_ATTR" value="" />
|
||||||
<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_PATHS">
|
<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_PATHS">
|
||||||
<listEntry value="/TencentOS-Tiny"/>
|
<listEntry value="/TencentOS-Tiny" />
|
||||||
</listAttribute>
|
</listAttribute>
|
||||||
<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_TYPES">
|
<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_TYPES">
|
||||||
<listEntry value="4"/>
|
<listEntry value="4" />
|
||||||
</listAttribute>
|
</listAttribute>
|
||||||
<stringAttribute key="org.eclipse.dsf.launch.MEMORY_BLOCKS" value="<?xml version="1.0" encoding="UTF-8" standalone="no"?> <memoryBlockExpressionList context="Context string"/> "/>
|
<stringAttribute key="org.eclipse.dsf.launch.MEMORY_BLOCKS" value="<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<memoryBlockExpressionList context="Context string"/>
" />
|
||||||
<stringAttribute key="process_factory_id" value="org.eclipse.cdt.dsf.gdb.GdbProcessFactory"/>
|
<stringAttribute key="process_factory_id" value="org.eclipse.cdt.dsf.gdb.GdbProcessFactory" />
|
||||||
</launchConfiguration>
|
</launchConfiguration>
|
||||||
|
@@ -1,18 +1,18 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8" ?>
|
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||||
<projectDescription>
|
<projectDescription>
|
||||||
<name>TencentOS-Tiny</name>
|
<name>TencentOS-Tiny</name>
|
||||||
<comment />
|
<comment/>
|
||||||
<projects />
|
<projects/>
|
||||||
<buildSpec>
|
<buildSpec>
|
||||||
<buildCommand>
|
<buildCommand>
|
||||||
<name>org.eclipse.cdt.managedbuilder.core.genmakebuilder</name>
|
<name>org.eclipse.cdt.managedbuilder.core.genmakebuilder</name>
|
||||||
<triggers>clean,full,incremental,</triggers>
|
<triggers>clean,full,incremental,</triggers>
|
||||||
<arguments />
|
<arguments/>
|
||||||
</buildCommand>
|
</buildCommand>
|
||||||
<buildCommand>
|
<buildCommand>
|
||||||
<name>org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder</name>
|
<name>org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder</name>
|
||||||
<triggers>full,incremental,</triggers>
|
<triggers>full,incremental,</triggers>
|
||||||
<arguments />
|
<arguments/>
|
||||||
</buildCommand>
|
</buildCommand>
|
||||||
</buildSpec>
|
</buildSpec>
|
||||||
<natures>
|
<natures>
|
||||||
@@ -60,7 +60,7 @@
|
|||||||
<filteredResources>
|
<filteredResources>
|
||||||
<filter>
|
<filter>
|
||||||
<id>1595986042669</id>
|
<id>1595986042669</id>
|
||||||
<name />
|
<name/>
|
||||||
<type>22</type>
|
<type>22</type>
|
||||||
<matcher>
|
<matcher>
|
||||||
<id>org.eclipse.ui.ide.multiFilter</id>
|
<id>org.eclipse.ui.ide.multiFilter</id>
|
||||||
|
@@ -8,12 +8,10 @@
|
|||||||
#include "ch32v30x_it.h"
|
#include "ch32v30x_it.h"
|
||||||
#include "tos_k.h"
|
#include "tos_k.h"
|
||||||
#include "tos_at.h"
|
#include "tos_at.h"
|
||||||
|
|
||||||
extern at_agent_t esp8266_tf_agent;
|
extern at_agent_t esp8266_tf_agent;
|
||||||
|
|
||||||
void NMI_Handler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
|
void NMI_Handler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
|
||||||
void HardFault_Handler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
|
void HardFault_Handler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
|
||||||
|
|
||||||
void USART2_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
|
void USART2_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
|
||||||
void UART6_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
|
void UART6_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
|
||||||
void UART7_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
|
void UART7_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
|
||||||
|
@@ -7,21 +7,186 @@
|
|||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
#include "debug.h"
|
#include "debug.h"
|
||||||
#include "tos_k.h"
|
#include "tos_k.h"
|
||||||
|
#include "sdio.h"
|
||||||
#include "lcd_init.h"
|
#include "lcd_init.h"
|
||||||
#include "lcd.h"
|
#include "lcd.h"
|
||||||
#include "pic.h"
|
#include "pic.h"
|
||||||
|
#include "spi_flash.h"
|
||||||
|
|
||||||
|
void led_key_init(void)
|
||||||
void led_init(void)
|
|
||||||
{
|
{
|
||||||
GPIO_InitTypeDef GPIO_InitStructure = {0};
|
GPIO_InitTypeDef GPIO_InitStructure = {0};
|
||||||
|
|
||||||
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE, ENABLE);
|
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE|RCC_APB2Periph_GPIOA, ENABLE);
|
||||||
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5;
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5;
|
||||||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
|
||||||
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
||||||
GPIO_Init(GPIOE, &GPIO_InitStructure);
|
GPIO_Init(GPIOE, &GPIO_InitStructure);
|
||||||
|
GPIO_SetBits(GPIOE,GPIO_Pin_2);
|
||||||
|
GPIO_SetBits(GPIOE,GPIO_Pin_3);
|
||||||
|
GPIO_SetBits(GPIOE,GPIO_Pin_4);
|
||||||
|
GPIO_SetBits(GPIOE,GPIO_Pin_5);
|
||||||
|
/* key 1 2 3 */
|
||||||
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_8;
|
||||||
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
|
||||||
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
||||||
|
GPIO_Init(GPIOA, &GPIO_InitStructure);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
uint8_t get_key(void)
|
||||||
|
{
|
||||||
|
uint16_t value=0;
|
||||||
|
if((GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0) & GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_1)\
|
||||||
|
& GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_8)) == 0 )
|
||||||
|
{
|
||||||
|
Delay_Ms(15);
|
||||||
|
if((GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0) & GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_1)\
|
||||||
|
& GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_8)) == 0 )
|
||||||
|
{
|
||||||
|
value= GPIO_ReadInputData(GPIOA);
|
||||||
|
if((value&0x1)==0) return 1;
|
||||||
|
else if((value&0x2)==0)return 2;
|
||||||
|
else if((value&0x100)==0) return 3;
|
||||||
|
else return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*********************************************************************
|
||||||
|
* @fn show_sdcard_info
|
||||||
|
*
|
||||||
|
* @brief SD Card information.
|
||||||
|
*
|
||||||
|
* @return none
|
||||||
|
*/
|
||||||
|
void show_sdcard_info(void)
|
||||||
|
{
|
||||||
|
switch(SDCardInfo.CardType)
|
||||||
|
{
|
||||||
|
case SDIO_STD_CAPACITY_SD_CARD_V1_1:printf("Card Type:SDSC V1.1\r\n");break;
|
||||||
|
case SDIO_STD_CAPACITY_SD_CARD_V2_0:printf("Card Type:SDSC V2.0\r\n");break;
|
||||||
|
case SDIO_HIGH_CAPACITY_SD_CARD:printf("Card Type:SDHC V2.0\r\n");break;
|
||||||
|
case SDIO_MULTIMEDIA_CARD:printf("Card Type:MMC Card\r\n");break;
|
||||||
|
}
|
||||||
|
printf("Card ManufacturerID:%d\r\n",SDCardInfo.SD_cid.ManufacturerID);
|
||||||
|
printf("Card RCA:%d\r\n",SDCardInfo.RCA);
|
||||||
|
printf("Card Capacity:%d MB\r\n",(u32)(SDCardInfo.CardCapacity>>20));
|
||||||
|
printf("Card BlockSize:%d\r\n\r\n",SDCardInfo.CardBlockSize);
|
||||||
|
}
|
||||||
|
const u8 TEXT_Buf[]={"CH32V307 SPI FLASH W25Qxx"};
|
||||||
|
void spi_flash_test(void)
|
||||||
|
{
|
||||||
|
u8 datap[SIZE];
|
||||||
|
u16 Flash_Model;
|
||||||
|
// SPI_Flash_Init();
|
||||||
|
Delay_Ms(50);
|
||||||
|
|
||||||
|
Flash_Model = SPI_Flash_ReadID();
|
||||||
|
|
||||||
|
switch(Flash_Model)
|
||||||
|
{
|
||||||
|
case W25Q80:
|
||||||
|
printf("W25Q80 OK!\r\n");
|
||||||
|
break;
|
||||||
|
|
||||||
|
case W25Q16:
|
||||||
|
printf("W25Q16 OK!\r\n");
|
||||||
|
break;
|
||||||
|
|
||||||
|
case W25Q32:
|
||||||
|
printf("W25Q32 OK!\r\n");
|
||||||
|
break;
|
||||||
|
|
||||||
|
case W25Q64:
|
||||||
|
printf("W25Q64 OK!\r\n");
|
||||||
|
break;
|
||||||
|
|
||||||
|
case W25Q128:
|
||||||
|
printf("W25Q128 OK!\r\n");
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
printf("Fail!\r\n");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("Start Erase W25Qxx....\r\n");
|
||||||
|
SPI_Flash_Erase_Sector(0);
|
||||||
|
Delay_Ms(500);
|
||||||
|
printf("Start Read W25Qxx....\r\n");
|
||||||
|
SPI_Flash_Read(datap,0x0,SIZE);
|
||||||
|
for(int16_t i=0;i<SIZE;i++)
|
||||||
|
{
|
||||||
|
printf("%x ", datap[i] );
|
||||||
|
}
|
||||||
|
printf("\r\n" );
|
||||||
|
Delay_Ms(500);
|
||||||
|
printf("Start Write W25Qxx....\r\n");
|
||||||
|
SPI_Flash_Write((u8*)TEXT_Buf,0,SIZE);
|
||||||
|
Delay_Ms(500);
|
||||||
|
SPI_Flash_Read(datap,0x0,SIZE);
|
||||||
|
printf("%s\r\n", datap );
|
||||||
|
|
||||||
|
if(memcmp(TEXT_Buf, datap, SIZE))
|
||||||
|
{
|
||||||
|
LCD_ShowString(16,16+16+16,"SPI Flash: Fail",WHITE,BLACK,16,0);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
LCD_ShowString(16,16+16+16,"SPI Flash: OK",WHITE,BLACK,16,0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
u8 buf[512];
|
||||||
|
u8 Readbuf[512];
|
||||||
|
|
||||||
|
/* sdcard test */
|
||||||
|
void SD_Card_test(void)
|
||||||
|
{
|
||||||
|
u32 i;
|
||||||
|
u32 Sector_Nums;
|
||||||
|
|
||||||
|
while(SD_Init())
|
||||||
|
{
|
||||||
|
printf("SD Card Error!\r\n");
|
||||||
|
delay_ms(1000);
|
||||||
|
}
|
||||||
|
|
||||||
|
show_sdcard_info();
|
||||||
|
|
||||||
|
printf("SD Card OK\r\n");
|
||||||
|
Sector_Nums = ((u32)(SDCardInfo.CardCapacity>>20))/2;
|
||||||
|
printf("Sector_Nums:%d\r\n", Sector_Nums);
|
||||||
|
|
||||||
|
for(i=0; i<512; i++){
|
||||||
|
buf[i] = i;
|
||||||
|
}
|
||||||
|
|
||||||
|
for(i=0; i<Sector_Nums/2; i++)
|
||||||
|
{
|
||||||
|
if(SD_WriteDisk(buf,i,1)) {printf("Wr %d sector fail\n", i);}
|
||||||
|
if(SD_ReadDisk(Readbuf,i,1)){ printf("Rd %d sector fail\n", i);}
|
||||||
|
|
||||||
|
if(memcmp(buf, Readbuf, 512))
|
||||||
|
{
|
||||||
|
printf(" %d sector Verify fail\n", i);
|
||||||
|
LCD_ShowString(16,16+16+16+16,"SD Sector:FAIL",WHITE,BLACK,16,0);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(i==Sector_Nums/2)
|
||||||
|
{
|
||||||
|
LCD_ShowString(16,16+16+16+16+16,"SD: OK",WHITE,BLACK,16,0);
|
||||||
|
printf("SD OK\r\n");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
LCD_ShowString(16,16+16+16+16+16,"SD ALL: FAIL",WHITE,BLACK,16,0);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
#define TASK1_STK_SIZE 1024
|
#define TASK1_STK_SIZE 1024
|
||||||
k_task_t task1;
|
k_task_t task1;
|
||||||
__aligned(4) uint8_t task1_stk[TASK1_STK_SIZE];
|
__aligned(4) uint8_t task1_stk[TASK1_STK_SIZE];
|
||||||
@@ -56,7 +221,7 @@ void task2_entry(void *arg)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
volatile uint8_t key=0;
|
||||||
/*******************************************************************************
|
/*******************************************************************************
|
||||||
* Function Name : main
|
* Function Name : main
|
||||||
* Description : Main program.
|
* Description : Main program.
|
||||||
@@ -70,12 +235,14 @@ int main(void)
|
|||||||
Delay_Init();
|
Delay_Init();
|
||||||
USART_Printf_Init(115200);
|
USART_Printf_Init(115200);
|
||||||
printf("SystemClk:%d\r\n",SystemCoreClock);
|
printf("SystemClk:%d\r\n",SystemCoreClock);
|
||||||
led_init();
|
led_key_init();
|
||||||
LCD_Init();//LCD<43><44>ʼ<EFBFBD><CABC>
|
LCD_Init();//LCD<43><44>ʼ<EFBFBD><CABC>
|
||||||
LCD_Fill(0,0,LCD_W,LCD_H,WHITE);
|
SPI_Flash_Init();
|
||||||
|
|
||||||
printf("Welcome to TencentOS tiny(%s)\r\n", TOS_VERSION);
|
printf("Welcome to TencentOS tiny(%s)\r\n", TOS_VERSION);
|
||||||
LCD_ShowString(0,0,"Welcome to TencentOS",RED,WHITE,16,0);
|
LCD_Fill(0,0,LCD_W,LCD_H,BLACK);
|
||||||
|
LCD_ShowString(0,0,"Welcome to TencentOS",WHITE,BLACK,16,0);
|
||||||
|
|
||||||
|
|
||||||
tos_knl_init();
|
tos_knl_init();
|
||||||
tos_task_create(&application_task, "application_task", application_entry1, NULL, 4, application_task_stk, APPLICATION_TASK_STK_SIZE, 0);
|
tos_task_create(&application_task, "application_task", application_entry1, NULL, 4, application_task_stk, APPLICATION_TASK_STK_SIZE, 0);
|
||||||
//tos_task_create(&task1, "task1", task1_entry, NULL, 3, task1_stk, TASK1_STK_SIZE, 0); // Create task1
|
//tos_task_create(&task1, "task1", task1_entry, NULL, 3, task1_stk, TASK1_STK_SIZE, 0); // Create task1
|
||||||
@@ -83,17 +250,37 @@ int main(void)
|
|||||||
tos_knl_start();
|
tos_knl_start();
|
||||||
|
|
||||||
printf("should not run at here!\r\n");
|
printf("should not run at here!\r\n");
|
||||||
|
while(!key)
|
||||||
|
{
|
||||||
|
key=get_key();
|
||||||
|
}
|
||||||
|
|
||||||
|
switch(key)
|
||||||
|
{
|
||||||
|
case 1:
|
||||||
|
//lcd_test();
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
LCD_Fill(0,0,LCD_W,LCD_H,BLACK);
|
||||||
|
spi_flash_test();
|
||||||
|
SD_Card_test();
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
LCD_Fill(0,0,LCD_W,LCD_H,BLACK);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
while(1)
|
while(1)
|
||||||
{
|
{
|
||||||
LCD_ShowString(0,0,"Welcome TencentOS",RED,WHITE,32,0);
|
LCD_ShowString(0,0,"Welcome TencentOS",WHITE,BLACK,32,0);
|
||||||
LCD_ShowString(0,40,"LCD_W:",RED,WHITE,16,0);
|
LCD_ShowString(0,40,"LCD_W:",WHITE,BLACK,16,0);
|
||||||
LCD_ShowIntNum(48,40,LCD_W,3,RED,WHITE,16);
|
LCD_ShowIntNum(48,40,LCD_W,3,WHITE,BLACK,16);
|
||||||
LCD_ShowString(80,40,"LCD_H:",RED,WHITE,16,0);
|
LCD_ShowString(80,40,"LCD_H:",WHITE,BLACK,16,0);
|
||||||
LCD_ShowIntNum(128,40,LCD_H,3,RED,WHITE,16);
|
LCD_ShowIntNum(128,40,LCD_H,3,WHITE,BLACK,16);
|
||||||
LCD_ShowString(80,40,"LCD_H:",RED,WHITE,16,0);
|
LCD_ShowString(80,40,"LCD_H:",WHITE,BLACK,16,0);
|
||||||
LCD_ShowString(0,70,"Increaseing Nun:",RED,WHITE,16,0);
|
LCD_ShowString(0,70,"Increaseing Nun:",WHITE,BLACK,16,0);
|
||||||
LCD_ShowFloatNum1(128,70,t,4,RED,WHITE,16);
|
LCD_ShowFloatNum1(128,70,t,4,WHITE,BLACK,16);
|
||||||
t+=0.11;
|
t+=0.11;
|
||||||
for(j=0;j<3;j++)
|
for(j=0;j<3;j++)
|
||||||
{
|
{
|
||||||
|
1795
board/TencentOS_Tiny_CH32V307_EVB/User/sdio.c
Normal file
1795
board/TencentOS_Tiny_CH32V307_EVB/User/sdio.c
Normal file
File diff suppressed because it is too large
Load Diff
357
board/TencentOS_Tiny_CH32V307_EVB/User/sdio.h
Normal file
357
board/TencentOS_Tiny_CH32V307_EVB/User/sdio.h
Normal file
@@ -0,0 +1,357 @@
|
|||||||
|
/********************************** (C) COPYRIGHT *******************************
|
||||||
|
* File Name : sdio.h
|
||||||
|
* Author : WCH
|
||||||
|
* Version : V1.0.0
|
||||||
|
* Date : 2021/06/06
|
||||||
|
* Description : This file contains the headers of the SDIO.
|
||||||
|
*******************************************************************************/
|
||||||
|
#ifndef __SDIO_H
|
||||||
|
#define __SDIO_H
|
||||||
|
|
||||||
|
#include"debug.h"
|
||||||
|
|
||||||
|
#define delay_ms(x) Delay_Ms(x)
|
||||||
|
|
||||||
|
|
||||||
|
/*SDIO flag*/
|
||||||
|
#define SDIO_FLAG_CCRCFAIL ((uint32_t)0x00000001)
|
||||||
|
#define SDIO_FLAG_DCRCFAIL ((uint32_t)0x00000002)
|
||||||
|
#define SDIO_FLAG_CTIMEOUT ((uint32_t)0x00000004)
|
||||||
|
#define SDIO_FLAG_DTIMEOUT ((uint32_t)0x00000008)
|
||||||
|
#define SDIO_FLAG_TXUNDERR ((uint32_t)0x00000010)
|
||||||
|
#define SDIO_FLAG_RXOVERR ((uint32_t)0x00000020)
|
||||||
|
#define SDIO_FLAG_CMDREND ((uint32_t)0x00000040)
|
||||||
|
#define SDIO_FLAG_CMDSENT ((uint32_t)0x00000080)
|
||||||
|
#define SDIO_FLAG_DATAEND ((uint32_t)0x00000100)
|
||||||
|
#define SDIO_FLAG_STBITERR ((uint32_t)0x00000200)
|
||||||
|
#define SDIO_FLAG_DBCKEND ((uint32_t)0x00000400)
|
||||||
|
#define SDIO_FLAG_CMDACT ((uint32_t)0x00000800)
|
||||||
|
#define SDIO_FLAG_TXACT ((uint32_t)0x00001000)
|
||||||
|
#define SDIO_FLAG_RXACT ((uint32_t)0x00002000)
|
||||||
|
#define SDIO_FLAG_TXFIFOHE ((uint32_t)0x00004000)
|
||||||
|
#define SDIO_FLAG_RXFIFOHF ((uint32_t)0x00008000)
|
||||||
|
#define SDIO_FLAG_TXFIFOF ((uint32_t)0x00010000)
|
||||||
|
#define SDIO_FLAG_RXFIFOF ((uint32_t)0x00020000)
|
||||||
|
#define SDIO_FLAG_TXFIFOE ((uint32_t)0x00040000)
|
||||||
|
#define SDIO_FLAG_RXFIFOE ((uint32_t)0x00080000)
|
||||||
|
#define SDIO_FLAG_TXDAVL ((uint32_t)0x00100000)
|
||||||
|
#define SDIO_FLAG_RXDAVL ((uint32_t)0x00200000)
|
||||||
|
#define SDIO_FLAG_SDIOIT ((uint32_t)0x00400000)
|
||||||
|
#define SDIO_FLAG_CEATAEND ((uint32_t)0x00800000)
|
||||||
|
|
||||||
|
/* SDIO clock */
|
||||||
|
#define SDIO_INIT_CLK_DIV 0xFE
|
||||||
|
#define SDIO_TRANSFER_CLK_DIV 0x00
|
||||||
|
|
||||||
|
/* SDIO work mode */
|
||||||
|
#define SD_POLLING_MODE 0
|
||||||
|
#define SD_DMA_MODE 1
|
||||||
|
|
||||||
|
/*SDIO Err define */
|
||||||
|
typedef enum
|
||||||
|
{
|
||||||
|
/* special err */
|
||||||
|
SD_CMD_CRC_FAIL = (1),
|
||||||
|
SD_DATA_CRC_FAIL = (2),
|
||||||
|
SD_CMD_RSP_TIMEOUT = (3),
|
||||||
|
SD_DATA_TIMEOUT = (4),
|
||||||
|
SD_TX_UNDERRUN = (5),
|
||||||
|
SD_RX_OVERRUN = (6),
|
||||||
|
SD_START_BIT_ERR = (7),
|
||||||
|
SD_CMD_OUT_OF_RANGE = (8),
|
||||||
|
SD_ADDR_MISALIGNED = (9),
|
||||||
|
SD_BLOCK_LEN_ERR = (10),
|
||||||
|
SD_ERASE_SEQ_ERR = (11),
|
||||||
|
SD_BAD_ERASE_PARAM = (12),
|
||||||
|
SD_WRITE_PROT_VIOLATION = (13),
|
||||||
|
SD_LOCK_UNLOCK_FAILED = (14),
|
||||||
|
SD_COM_CRC_FAILED = (15),
|
||||||
|
SD_ILLEGAL_CMD = (16),
|
||||||
|
SD_CARD_ECC_FAILED = (17),
|
||||||
|
SD_CC_ERROR = (18),
|
||||||
|
SD_GENERAL_UNKNOWN_ERROR = (19),
|
||||||
|
SD_STREAM_READ_UNDERRUN = (20),
|
||||||
|
SD_STREAM_WRITE_OVERRUN = (21),
|
||||||
|
SD_CID_CSD_OVERWRITE = (22),
|
||||||
|
SD_WP_ERASE_SKIP = (23),
|
||||||
|
SD_CARD_ECC_DISABLED = (24),
|
||||||
|
SD_ERASE_RESET = (25),
|
||||||
|
SD_AKE_SEQ_ERROR = (26),
|
||||||
|
SD_INVALID_VOLTRANGE = (27),
|
||||||
|
SD_ADDR_OUT_OF_RANGE = (28),
|
||||||
|
SD_SWITCH_ERROR = (29),
|
||||||
|
SD_SDIO_DISABLED = (30),
|
||||||
|
SD_SDIO_FUNCTION_BUSY = (31),
|
||||||
|
SD_SDIO_FUNCTION_FAILED = (32),
|
||||||
|
SD_SDIO_UNKNOWN_FUNCTION = (33),
|
||||||
|
/* Standard err */
|
||||||
|
SD_INTERNAL_ERROR,
|
||||||
|
SD_NOT_CONFIGURED,
|
||||||
|
SD_REQUEST_PENDING,
|
||||||
|
SD_REQUEST_NOT_APPLICABLE,
|
||||||
|
SD_INVALID_PARAMETER,
|
||||||
|
SD_UNSUPPORTED_FEATURE,
|
||||||
|
SD_UNSUPPORTED_HW,
|
||||||
|
SD_ERROR,
|
||||||
|
SD_OK = 0
|
||||||
|
} SD_Error;
|
||||||
|
|
||||||
|
/*SD——CSD*/
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
u8 CSDStruct;
|
||||||
|
u8 SysSpecVersion;
|
||||||
|
u8 Reserved1;
|
||||||
|
u8 TAAC;
|
||||||
|
u8 NSAC;
|
||||||
|
u8 MaxBusClkFrec;
|
||||||
|
u16 CardComdClasses;
|
||||||
|
u8 RdBlockLen;
|
||||||
|
u8 PartBlockRead;
|
||||||
|
u8 WrBlockMisalign;
|
||||||
|
u8 RdBlockMisalign;
|
||||||
|
u8 DSRImpl;
|
||||||
|
u8 Reserved2;
|
||||||
|
u32 DeviceSize;
|
||||||
|
u8 MaxRdCurrentVDDMin;
|
||||||
|
u8 MaxRdCurrentVDDMax;
|
||||||
|
u8 MaxWrCurrentVDDMin;
|
||||||
|
u8 MaxWrCurrentVDDMax;
|
||||||
|
u8 DeviceSizeMul;
|
||||||
|
u8 EraseGrSize;
|
||||||
|
u8 EraseGrMul;
|
||||||
|
u8 WrProtectGrSize;
|
||||||
|
u8 WrProtectGrEnable;
|
||||||
|
u8 ManDeflECC;
|
||||||
|
u8 WrSpeedFact;
|
||||||
|
u8 MaxWrBlockLen;
|
||||||
|
u8 WriteBlockPaPartial;
|
||||||
|
u8 Reserved3;
|
||||||
|
u8 ContentProtectAppli;
|
||||||
|
u8 FileFormatGrouop;
|
||||||
|
u8 CopyFlag;
|
||||||
|
u8 PermWrProtect;
|
||||||
|
u8 TempWrProtect;
|
||||||
|
u8 FileFormat;
|
||||||
|
u8 ECC;
|
||||||
|
u8 CSD_CRC;
|
||||||
|
u8 Reserved4;
|
||||||
|
} SD_CSD;
|
||||||
|
|
||||||
|
/*SD——CID*/
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
u8 ManufacturerID;
|
||||||
|
u16 OEM_AppliID;
|
||||||
|
u32 ProdName1;
|
||||||
|
u8 ProdName2;
|
||||||
|
u8 ProdRev;
|
||||||
|
u32 ProdSN;
|
||||||
|
u8 Reserved1;
|
||||||
|
u16 ManufactDate;
|
||||||
|
u8 CID_CRC;
|
||||||
|
u8 Reserved2;
|
||||||
|
} SD_CID;
|
||||||
|
|
||||||
|
/* SD statue */
|
||||||
|
typedef enum
|
||||||
|
{
|
||||||
|
SD_CARD_READY = ((uint32_t)0x00000001),
|
||||||
|
SD_CARD_IDENTIFICATION = ((uint32_t)0x00000002),
|
||||||
|
SD_CARD_STANDBY = ((uint32_t)0x00000003),
|
||||||
|
SD_CARD_TRANSFER = ((uint32_t)0x00000004),
|
||||||
|
SD_CARD_SENDING = ((uint32_t)0x00000005),
|
||||||
|
SD_CARD_RECEIVING = ((uint32_t)0x00000006),
|
||||||
|
SD_CARD_PROGRAMMING = ((uint32_t)0x00000007),
|
||||||
|
SD_CARD_DISCONNECTED = ((uint32_t)0x00000008),
|
||||||
|
SD_CARD_ERROR = ((uint32_t)0x000000FF)
|
||||||
|
}SDCardState;
|
||||||
|
|
||||||
|
/* SD informatiion */
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
SD_CSD SD_csd;
|
||||||
|
SD_CID SD_cid;
|
||||||
|
long long CardCapacity;
|
||||||
|
u32 CardBlockSize;
|
||||||
|
u16 RCA;
|
||||||
|
u8 CardType;
|
||||||
|
} SD_CardInfo;
|
||||||
|
extern SD_CardInfo SDCardInfo;
|
||||||
|
|
||||||
|
/* SDIO instruction*/
|
||||||
|
#define SD_CMD_GO_IDLE_STATE ((u8)0)
|
||||||
|
#define SD_CMD_SEND_OP_COND ((u8)1)
|
||||||
|
#define SD_CMD_ALL_SEND_CID ((u8)2)
|
||||||
|
#define SD_CMD_SET_REL_ADDR ((u8)3)
|
||||||
|
#define SD_CMD_SET_DSR ((u8)4)
|
||||||
|
#define SD_CMD_SDIO_SEN_OP_COND ((u8)5)
|
||||||
|
#define SD_CMD_HS_SWITCH ((u8)6)
|
||||||
|
#define SD_CMD_SEL_DESEL_CARD ((u8)7)
|
||||||
|
#define SD_CMD_HS_SEND_EXT_CSD ((u8)8)
|
||||||
|
#define SD_CMD_SEND_CSD ((u8)9)
|
||||||
|
#define SD_CMD_SEND_CID ((u8)10)
|
||||||
|
#define SD_CMD_READ_DAT_UNTIL_STOP ((u8)11)
|
||||||
|
#define SD_CMD_STOP_TRANSMISSION ((u8)12)
|
||||||
|
#define SD_CMD_SEND_STATUS ((u8)13)
|
||||||
|
#define SD_CMD_HS_BUSTEST_READ ((u8)14)
|
||||||
|
#define SD_CMD_GO_INACTIVE_STATE ((u8)15)
|
||||||
|
#define SD_CMD_SET_BLOCKLEN ((u8)16)
|
||||||
|
#define SD_CMD_READ_SINGLE_BLOCK ((u8)17)
|
||||||
|
#define SD_CMD_READ_MULT_BLOCK ((u8)18)
|
||||||
|
#define SD_CMD_HS_BUSTEST_WRITE ((u8)19)
|
||||||
|
#define SD_CMD_WRITE_DAT_UNTIL_STOP ((u8)20)
|
||||||
|
#define SD_CMD_SET_BLOCK_COUNT ((u8)23)
|
||||||
|
#define SD_CMD_WRITE_SINGLE_BLOCK ((u8)24)
|
||||||
|
#define SD_CMD_WRITE_MULT_BLOCK ((u8)25)
|
||||||
|
#define SD_CMD_PROG_CID ((u8)26)
|
||||||
|
#define SD_CMD_PROG_CSD ((u8)27)
|
||||||
|
#define SD_CMD_SET_WRITE_PROT ((u8)28)
|
||||||
|
#define SD_CMD_CLR_WRITE_PROT ((u8)29)
|
||||||
|
#define SD_CMD_SEND_WRITE_PROT ((u8)30)
|
||||||
|
#define SD_CMD_SD_ERASE_GRP_START ((u8)32)
|
||||||
|
#define SD_CMD_SD_ERASE_GRP_END ((u8)33)
|
||||||
|
#define SD_CMD_ERASE_GRP_START ((u8)35)
|
||||||
|
#define SD_CMD_ERASE_GRP_END ((u8)36)
|
||||||
|
#define SD_CMD_ERASE ((u8)38)
|
||||||
|
#define SD_CMD_FAST_IO ((u8)39)
|
||||||
|
#define SD_CMD_GO_IRQ_STATE ((u8)40)
|
||||||
|
#define SD_CMD_LOCK_UNLOCK ((u8)42)
|
||||||
|
#define SD_CMD_APP_CMD ((u8)55)
|
||||||
|
#define SD_CMD_GEN_CMD ((u8)56)
|
||||||
|
#define SD_CMD_NO_CMD ((u8)64)
|
||||||
|
|
||||||
|
/*Following commands are SD Card Specific commands.
|
||||||
|
SDIO_APP_CMD :CMD55 should be sent before sending these commands.
|
||||||
|
*/
|
||||||
|
#define SD_CMD_APP_SD_SET_BUSWIDTH ((u8)6)
|
||||||
|
#define SD_CMD_SD_APP_STAUS ((u8)13)
|
||||||
|
#define SD_CMD_SD_APP_SEND_NUM_WRITE_BLOCKS ((u8)22)
|
||||||
|
#define SD_CMD_SD_APP_OP_COND ((u8)41)
|
||||||
|
#define SD_CMD_SD_APP_SET_CLR_CARD_DETECT ((u8)42)
|
||||||
|
#define SD_CMD_SD_APP_SEND_SCR ((u8)51)
|
||||||
|
#define SD_CMD_SDIO_RW_DIRECT ((u8)52)
|
||||||
|
#define SD_CMD_SDIO_RW_EXTENDED ((u8)53)
|
||||||
|
|
||||||
|
/*Following commands are SD Card Specific security commands.
|
||||||
|
SDIO_APP_CMD should be sent before sending these commands.
|
||||||
|
*/
|
||||||
|
#define SD_CMD_SD_APP_GET_MKB ((u8)43)
|
||||||
|
#define SD_CMD_SD_APP_GET_MID ((u8)44)
|
||||||
|
#define SD_CMD_SD_APP_SET_CER_RN1 ((u8)45)
|
||||||
|
#define SD_CMD_SD_APP_GET_CER_RN2 ((u8)46)
|
||||||
|
#define SD_CMD_SD_APP_SET_CER_RES2 ((u8)47)
|
||||||
|
#define SD_CMD_SD_APP_GET_CER_RES1 ((u8)48)
|
||||||
|
#define SD_CMD_SD_APP_SECURE_READ_MULTIPLE_BLOCK ((u8)18)
|
||||||
|
#define SD_CMD_SD_APP_SECURE_WRITE_MULTIPLE_BLOCK ((u8)25)
|
||||||
|
#define SD_CMD_SD_APP_SECURE_ERASE ((u8)38)
|
||||||
|
#define SD_CMD_SD_APP_CHANGE_SECURE_AREA ((u8)49)
|
||||||
|
#define SD_CMD_SD_APP_SECURE_WRITE_MKB ((u8)48)
|
||||||
|
|
||||||
|
/* SD definition */
|
||||||
|
#define SDIO_STD_CAPACITY_SD_CARD_V1_1 ((u32)0x00000000)
|
||||||
|
#define SDIO_STD_CAPACITY_SD_CARD_V2_0 ((u32)0x00000001)
|
||||||
|
#define SDIO_HIGH_CAPACITY_SD_CARD ((u32)0x00000002)
|
||||||
|
#define SDIO_MULTIMEDIA_CARD ((u32)0x00000003)
|
||||||
|
#define SDIO_SECURE_DIGITAL_IO_CARD ((u32)0x00000004)
|
||||||
|
#define SDIO_HIGH_SPEED_MULTIMEDIA_CARD ((u32)0x00000005)
|
||||||
|
#define SDIO_SECURE_DIGITAL_IO_COMBO_CARD ((u32)0x00000006)
|
||||||
|
#define SDIO_HIGH_CAPACITY_MMC_CARD ((u32)0x00000007)
|
||||||
|
|
||||||
|
/* SDIO parameter definition */
|
||||||
|
#define SDIO_STATIC_FLAGS ((u32)0x000005FF)
|
||||||
|
#define SDIO_CMD0TIMEOUT ((u32)0x00010000)
|
||||||
|
#define SDIO_DATATIMEOUT ((u32)0xFFFFFFFF)
|
||||||
|
#define SDIO_FIFO_Address ((u32)0x40018080)
|
||||||
|
|
||||||
|
/* Mask for errors Card Status R1 (OCR Register) */
|
||||||
|
#define SD_OCR_ADDR_OUT_OF_RANGE ((u32)0x80000000)
|
||||||
|
#define SD_OCR_ADDR_MISALIGNED ((u32)0x40000000)
|
||||||
|
#define SD_OCR_BLOCK_LEN_ERR ((u32)0x20000000)
|
||||||
|
#define SD_OCR_ERASE_SEQ_ERR ((u32)0x10000000)
|
||||||
|
#define SD_OCR_BAD_ERASE_PARAM ((u32)0x08000000)
|
||||||
|
#define SD_OCR_WRITE_PROT_VIOLATION ((u32)0x04000000)
|
||||||
|
#define SD_OCR_LOCK_UNLOCK_FAILED ((u32)0x01000000)
|
||||||
|
#define SD_OCR_COM_CRC_FAILED ((u32)0x00800000)
|
||||||
|
#define SD_OCR_ILLEGAL_CMD ((u32)0x00400000)
|
||||||
|
#define SD_OCR_CARD_ECC_FAILED ((u32)0x00200000)
|
||||||
|
#define SD_OCR_CC_ERROR ((u32)0x00100000)
|
||||||
|
#define SD_OCR_GENERAL_UNKNOWN_ERROR ((u32)0x00080000)
|
||||||
|
#define SD_OCR_STREAM_READ_UNDERRUN ((u32)0x00040000)
|
||||||
|
#define SD_OCR_STREAM_WRITE_OVERRUN ((u32)0x00020000)
|
||||||
|
#define SD_OCR_CID_CSD_OVERWRIETE ((u32)0x00010000)
|
||||||
|
#define SD_OCR_WP_ERASE_SKIP ((u32)0x00008000)
|
||||||
|
#define SD_OCR_CARD_ECC_DISABLED ((u32)0x00004000)
|
||||||
|
#define SD_OCR_ERASE_RESET ((u32)0x00002000)
|
||||||
|
#define SD_OCR_AKE_SEQ_ERROR ((u32)0x00000008)
|
||||||
|
#define SD_OCR_ERRORBITS ((u32)0xFDFFE008)
|
||||||
|
|
||||||
|
/* Masks for R6 Response */
|
||||||
|
#define SD_R6_GENERAL_UNKNOWN_ERROR ((u32)0x00002000)
|
||||||
|
#define SD_R6_ILLEGAL_CMD ((u32)0x00004000)
|
||||||
|
#define SD_R6_COM_CRC_FAILED ((u32)0x00008000)
|
||||||
|
|
||||||
|
#define SD_VOLTAGE_WINDOW_SD ((u32)0x80100000)
|
||||||
|
#define SD_HIGH_CAPACITY ((u32)0x40000000)
|
||||||
|
#define SD_STD_CAPACITY ((u32)0x00000000)
|
||||||
|
#define SD_CHECK_PATTERN ((u32)0x000001AA)
|
||||||
|
#define SD_VOLTAGE_WINDOW_MMC ((u32)0x80FF8000)
|
||||||
|
|
||||||
|
#define SD_MAX_VOLT_TRIAL ((u32)0x0000FFFF)
|
||||||
|
#define SD_ALLZERO ((u32)0x00000000)
|
||||||
|
|
||||||
|
#define SD_WIDE_BUS_SUPPORT ((u32)0x00040000)
|
||||||
|
#define SD_SINGLE_BUS_SUPPORT ((u32)0x00010000)
|
||||||
|
#define SD_CARD_LOCKED ((u32)0x02000000)
|
||||||
|
#define SD_CARD_PROGRAMMING ((u32)0x00000007)
|
||||||
|
#define SD_CARD_RECEIVING ((u32)0x00000006)
|
||||||
|
#define SD_DATATIMEOUT ((u32)0xFFFFFFFF)
|
||||||
|
#define SD_0TO7BITS ((u32)0x000000FF)
|
||||||
|
#define SD_8TO15BITS ((u32)0x0000FF00)
|
||||||
|
#define SD_16TO23BITS ((u32)0x00FF0000)
|
||||||
|
#define SD_24TO31BITS ((u32)0xFF000000)
|
||||||
|
#define SD_MAX_DATA_LENGTH ((u32)0x01FFFFFF)
|
||||||
|
|
||||||
|
#define SD_HALFFIFO ((u32)0x00000008)
|
||||||
|
#define SD_HALFFIFOBYTES ((u32)0x00000020)
|
||||||
|
|
||||||
|
/*Command Class Supported */
|
||||||
|
#define SD_CCCC_LOCK_UNLOCK ((u32)0x00000080)
|
||||||
|
#define SD_CCCC_WRITE_PROT ((u32)0x00000040)
|
||||||
|
#define SD_CCCC_ERASE ((u32)0x00000020)
|
||||||
|
|
||||||
|
/*CMD8*/
|
||||||
|
#define SDIO_SEND_IF_COND ((u32)0x00000008)
|
||||||
|
|
||||||
|
SD_Error SD_Init(void);
|
||||||
|
void SDIO_Clock_Set(u8 clkdiv);
|
||||||
|
void SDIO_Send_Cmd(u8 cmdindex,u8 waitrsp,u32 arg);
|
||||||
|
void SDIO_Send_Data_Cfg(u32 datatimeout,u32 datalen,u8 blksize,u8 dir);
|
||||||
|
SD_Error SD_PowerON(void);
|
||||||
|
SD_Error SD_PowerOFF(void);
|
||||||
|
SD_Error SD_InitializeCards(void);
|
||||||
|
SD_Error SD_GetCardInfo(SD_CardInfo *cardinfo);
|
||||||
|
SD_Error SD_EnableWideBusOperation(u32 wmode);
|
||||||
|
SD_Error SD_SetDeviceMode(u32 mode);
|
||||||
|
SD_Error SD_SelectDeselect(u32 addr);
|
||||||
|
SD_Error SD_SendStatus(uint32_t *pcardstatus);
|
||||||
|
SDCardState SD_GetState(void);
|
||||||
|
SD_Error SD_ReadBlock(u8 *buf,long long addr,u16 blksize);
|
||||||
|
SD_Error SD_ReadMultiBlocks(u8 *buf,long long addr,u16 blksize,u32 nblks);
|
||||||
|
SD_Error SD_WriteBlock(u8 *buf,long long addr, u16 blksize);
|
||||||
|
SD_Error SD_WriteMultiBlocks(u8 *buf,long long addr,u16 blksize,u32 nblks);
|
||||||
|
SD_Error SD_ProcessIRQSrc(void);
|
||||||
|
void SD_DMA_Config(u32*mbuf,u32 bufsize,u32 dir);
|
||||||
|
u8 SD_ReadDisk(u8*buf,u32 sector,u8 cnt);
|
||||||
|
u8 SD_WriteDisk(u8*buf,u32 sector,u8 cnt);
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
434
board/TencentOS_Tiny_CH32V307_EVB/User/spi_flash.c
Normal file
434
board/TencentOS_Tiny_CH32V307_EVB/User/spi_flash.c
Normal file
@@ -0,0 +1,434 @@
|
|||||||
|
#include "spi_flash.h"
|
||||||
|
#include "debug.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*********************************************************************
|
||||||
|
* @fn SPI1_ReadWriteByte
|
||||||
|
*
|
||||||
|
* @brief SPI1 read or write one byte.
|
||||||
|
*
|
||||||
|
* @param TxData - write one byte data.
|
||||||
|
*
|
||||||
|
* @return Read one byte data.
|
||||||
|
*/
|
||||||
|
u8 SPI1_ReadWriteByte(u8 TxData)
|
||||||
|
{
|
||||||
|
u8 i=0;
|
||||||
|
|
||||||
|
while (SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_TXE) == RESET)
|
||||||
|
{
|
||||||
|
i++;
|
||||||
|
if(i>200)return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
SPI_I2S_SendData(SPI3, TxData);
|
||||||
|
i=0;
|
||||||
|
|
||||||
|
while (SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_RXNE) == RESET)
|
||||||
|
{
|
||||||
|
i++;
|
||||||
|
if(i>200)return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return SPI_I2S_ReceiveData(SPI3);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*********************************************************************
|
||||||
|
* @fn SPI_Flash_Init
|
||||||
|
*
|
||||||
|
* @brief Configuring the SPI for operation flash.
|
||||||
|
*
|
||||||
|
* @return none
|
||||||
|
*/
|
||||||
|
void SPI_Flash_Init(void)
|
||||||
|
{
|
||||||
|
GPIO_InitTypeDef GPIO_InitStructure={0};
|
||||||
|
SPI_InitTypeDef SPI_InitStructure={0};
|
||||||
|
|
||||||
|
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA/*|RCC_APB2Periph_SPI1*/|RCC_APB2Periph_GPIOB, ENABLE );
|
||||||
|
RCC_APB1PeriphClockCmd( RCC_APB1Periph_SPI3, ENABLE );
|
||||||
|
|
||||||
|
|
||||||
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
|
||||||
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
|
||||||
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
||||||
|
GPIO_Init(GPIOA, &GPIO_InitStructure);
|
||||||
|
GPIO_SetBits(GPIOA, GPIO_Pin_15); //SPI3_NSS
|
||||||
|
|
||||||
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
|
||||||
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
|
||||||
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
||||||
|
GPIO_Init( GPIOB, &GPIO_InitStructure );//SPI3_SCK
|
||||||
|
|
||||||
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
|
||||||
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
|
||||||
|
GPIO_Init( GPIOB, &GPIO_InitStructure );//SPI3_MISO
|
||||||
|
|
||||||
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
|
||||||
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
|
||||||
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
||||||
|
GPIO_Init( GPIOB, &GPIO_InitStructure );//SPI3_MOSI
|
||||||
|
|
||||||
|
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
|
||||||
|
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
|
||||||
|
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
|
||||||
|
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
|
||||||
|
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
|
||||||
|
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
|
||||||
|
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16;
|
||||||
|
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
|
||||||
|
SPI_InitStructure.SPI_CRCPolynomial = 7;
|
||||||
|
SPI_Init(SPI3, &SPI_InitStructure);
|
||||||
|
|
||||||
|
SPI_Cmd(SPI3, ENABLE);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*********************************************************************
|
||||||
|
* @fn SPI_Flash_ReadSR
|
||||||
|
*
|
||||||
|
* @brief Read W25Qxx status register.
|
||||||
|
* <20><><EFBFBD><EFBFBD>BIT7 6 5 4 3 2 1 0
|
||||||
|
* <20><><EFBFBD><EFBFBD>SPR RV TB BP2 BP1 BP0 WEL BUSY
|
||||||
|
*
|
||||||
|
* @return byte - status register value.
|
||||||
|
*/
|
||||||
|
u8 SPI_Flash_ReadSR(void)
|
||||||
|
{
|
||||||
|
u8 byte=0;
|
||||||
|
|
||||||
|
GPIO_WriteBit(GPIOA, GPIO_Pin_15, 0);
|
||||||
|
SPI1_ReadWriteByte(W25X_ReadStatusReg);
|
||||||
|
byte=SPI1_ReadWriteByte(0Xff);
|
||||||
|
GPIO_WriteBit(GPIOA, GPIO_Pin_15, 1);
|
||||||
|
|
||||||
|
return byte;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*********************************************************************
|
||||||
|
* @fn SPI_FLASH_Write_SR
|
||||||
|
*
|
||||||
|
* @brief Write W25Qxx status register.
|
||||||
|
*
|
||||||
|
* @param sr - status register value.
|
||||||
|
*
|
||||||
|
* @return none
|
||||||
|
*/
|
||||||
|
void SPI_FLASH_Write_SR(u8 sr)
|
||||||
|
{
|
||||||
|
GPIO_WriteBit(GPIOA, GPIO_Pin_15, 0);
|
||||||
|
SPI1_ReadWriteByte(W25X_WriteStatusReg);
|
||||||
|
SPI1_ReadWriteByte(sr);
|
||||||
|
GPIO_WriteBit(GPIOA, GPIO_Pin_15, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*********************************************************************
|
||||||
|
* @fn SPI_Flash_Wait_Busy
|
||||||
|
*
|
||||||
|
* @brief Wait flash free.
|
||||||
|
*
|
||||||
|
* @return none
|
||||||
|
*/
|
||||||
|
void SPI_Flash_Wait_Busy(void)
|
||||||
|
{
|
||||||
|
while((SPI_Flash_ReadSR()&0x01)==0x01);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*********************************************************************
|
||||||
|
* @fn SPI_FLASH_Write_Enable
|
||||||
|
*
|
||||||
|
* @brief Enable flash write.
|
||||||
|
*
|
||||||
|
* @return none
|
||||||
|
*/
|
||||||
|
void SPI_FLASH_Write_Enable(void)
|
||||||
|
{
|
||||||
|
GPIO_WriteBit(GPIOA, GPIO_Pin_15, 0);
|
||||||
|
SPI1_ReadWriteByte(W25X_WriteEnable);
|
||||||
|
GPIO_WriteBit(GPIOA, GPIO_Pin_15, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*********************************************************************
|
||||||
|
* @fn SPI_FLASH_Write_Disable
|
||||||
|
*
|
||||||
|
* @brief Disable flash write.
|
||||||
|
*
|
||||||
|
* @return none
|
||||||
|
*/
|
||||||
|
void SPI_FLASH_Write_Disable(void)
|
||||||
|
{
|
||||||
|
GPIO_WriteBit(GPIOA, GPIO_Pin_15, 0);
|
||||||
|
SPI1_ReadWriteByte(W25X_WriteDisable);
|
||||||
|
GPIO_WriteBit(GPIOA, GPIO_Pin_15, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*********************************************************************
|
||||||
|
* @fn SPI_Flash_ReadID
|
||||||
|
*
|
||||||
|
* @brief Read flash ID.
|
||||||
|
*
|
||||||
|
* @return Temp - FLASH ID.
|
||||||
|
*/
|
||||||
|
u16 SPI_Flash_ReadID(void)
|
||||||
|
{
|
||||||
|
u16 Temp = 0;
|
||||||
|
|
||||||
|
GPIO_WriteBit(GPIOA, GPIO_Pin_15, 0);
|
||||||
|
Delay_Ms(10);
|
||||||
|
SPI1_ReadWriteByte(W25X_ManufactDeviceID);
|
||||||
|
SPI1_ReadWriteByte(0x00);
|
||||||
|
SPI1_ReadWriteByte(0x00);
|
||||||
|
SPI1_ReadWriteByte(0x00);
|
||||||
|
Temp|=SPI1_ReadWriteByte(0xFF)<<8;
|
||||||
|
Temp|=SPI1_ReadWriteByte(0xFF);
|
||||||
|
GPIO_WriteBit(GPIOA, GPIO_Pin_15, 1);
|
||||||
|
|
||||||
|
return Temp;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*********************************************************************
|
||||||
|
* @fn SPI_Flash_Erase_Sector
|
||||||
|
*
|
||||||
|
* @brief Erase one sector(4Kbyte).
|
||||||
|
*
|
||||||
|
* @param Dst_Addr - 0 <20><><EFBFBD><EFBFBD> 2047
|
||||||
|
*
|
||||||
|
* @return none
|
||||||
|
*/
|
||||||
|
void SPI_Flash_Erase_Sector(u32 Dst_Addr)
|
||||||
|
{
|
||||||
|
Dst_Addr*=4096;
|
||||||
|
SPI_FLASH_Write_Enable();
|
||||||
|
SPI_Flash_Wait_Busy();
|
||||||
|
GPIO_WriteBit(GPIOA, GPIO_Pin_15, 0);
|
||||||
|
SPI1_ReadWriteByte(W25X_SectorErase);
|
||||||
|
SPI1_ReadWriteByte((u8)((Dst_Addr)>>16));
|
||||||
|
SPI1_ReadWriteByte((u8)((Dst_Addr)>>8));
|
||||||
|
SPI1_ReadWriteByte((u8)Dst_Addr);
|
||||||
|
GPIO_WriteBit(GPIOA, GPIO_Pin_15, 1);
|
||||||
|
SPI_Flash_Wait_Busy();
|
||||||
|
}
|
||||||
|
|
||||||
|
/*********************************************************************
|
||||||
|
* @fn SPI_Flash_Read
|
||||||
|
*
|
||||||
|
* @brief Read data from flash.
|
||||||
|
*
|
||||||
|
* @param pBuffer -
|
||||||
|
* ReadAddr -Initial address(24bit).
|
||||||
|
* size - Data length.
|
||||||
|
*
|
||||||
|
* @return none
|
||||||
|
*/
|
||||||
|
void SPI_Flash_Read(u8* pBuffer,u32 ReadAddr,u16 size)
|
||||||
|
{
|
||||||
|
u16 i;
|
||||||
|
|
||||||
|
GPIO_WriteBit(GPIOA, GPIO_Pin_15, 0);
|
||||||
|
SPI1_ReadWriteByte(W25X_ReadData);
|
||||||
|
SPI1_ReadWriteByte((u8)((ReadAddr)>>16));
|
||||||
|
SPI1_ReadWriteByte((u8)((ReadAddr)>>8));
|
||||||
|
SPI1_ReadWriteByte((u8)ReadAddr);
|
||||||
|
|
||||||
|
for(i=0;i<size;i++)
|
||||||
|
{
|
||||||
|
pBuffer[i]=SPI1_ReadWriteByte(0XFF);
|
||||||
|
}
|
||||||
|
|
||||||
|
GPIO_WriteBit(GPIOA, GPIO_Pin_15, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*********************************************************************
|
||||||
|
* @fn SPI_Flash_Write_Page
|
||||||
|
*
|
||||||
|
* @brief Write data by one page.
|
||||||
|
*
|
||||||
|
* @param pBuffer -
|
||||||
|
* WriteAddr - Initial address(24bit).
|
||||||
|
* size - Data length.
|
||||||
|
*
|
||||||
|
* @return none
|
||||||
|
*/
|
||||||
|
void SPI_Flash_Write_Page(u8* pBuffer,u32 WriteAddr,u16 size)
|
||||||
|
{
|
||||||
|
u16 i;
|
||||||
|
|
||||||
|
SPI_FLASH_Write_Enable();
|
||||||
|
GPIO_WriteBit(GPIOA, GPIO_Pin_15, 0);
|
||||||
|
SPI1_ReadWriteByte(W25X_PageProgram);
|
||||||
|
SPI1_ReadWriteByte((u8)((WriteAddr)>>16));
|
||||||
|
SPI1_ReadWriteByte((u8)((WriteAddr)>>8));
|
||||||
|
SPI1_ReadWriteByte((u8)WriteAddr);
|
||||||
|
|
||||||
|
for(i=0;i<size;i++)
|
||||||
|
{
|
||||||
|
SPI1_ReadWriteByte(pBuffer[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
GPIO_WriteBit(GPIOA, GPIO_Pin_15, 1);
|
||||||
|
SPI_Flash_Wait_Busy();
|
||||||
|
}
|
||||||
|
|
||||||
|
/*********************************************************************
|
||||||
|
* @fn SPI_Flash_Write_NoCheck
|
||||||
|
*
|
||||||
|
* @brief Write data to flash.(need Erase)
|
||||||
|
* All data in address rang is 0xFF.
|
||||||
|
*
|
||||||
|
* @param pBuffer -
|
||||||
|
* WriteAddr - Initial address(24bit).
|
||||||
|
* size - Data length.
|
||||||
|
*
|
||||||
|
* @return none
|
||||||
|
*/
|
||||||
|
void SPI_Flash_Write_NoCheck(u8* pBuffer,u32 WriteAddr,u16 size)
|
||||||
|
{
|
||||||
|
u16 pageremain;
|
||||||
|
|
||||||
|
pageremain=256-WriteAddr%256;
|
||||||
|
|
||||||
|
if(size<=pageremain)pageremain=size;
|
||||||
|
|
||||||
|
while(1)
|
||||||
|
{
|
||||||
|
SPI_Flash_Write_Page(pBuffer,WriteAddr,pageremain);
|
||||||
|
|
||||||
|
if(size==pageremain)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
pBuffer+=pageremain;
|
||||||
|
WriteAddr+=pageremain;
|
||||||
|
size-=pageremain;
|
||||||
|
|
||||||
|
if(size>256)pageremain=256;
|
||||||
|
else pageremain=size;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*********************************************************************
|
||||||
|
* @fn SPI_Flash_Write
|
||||||
|
*
|
||||||
|
* @brief Write data to flash.(no need Erase)
|
||||||
|
*
|
||||||
|
* @param pBuffer -
|
||||||
|
* WriteAddr - Initial address(24bit).
|
||||||
|
* size - Data length.
|
||||||
|
*
|
||||||
|
* @return none
|
||||||
|
*/
|
||||||
|
void SPI_Flash_Write(u8* pBuffer,u32 WriteAddr,u16 size)
|
||||||
|
{
|
||||||
|
u32 secpos;
|
||||||
|
u16 secoff;
|
||||||
|
u16 secremain;
|
||||||
|
u16 i;
|
||||||
|
|
||||||
|
secpos=WriteAddr/4096;
|
||||||
|
secoff=WriteAddr%4096;
|
||||||
|
secremain=4096-secoff;
|
||||||
|
|
||||||
|
if(size<=secremain)secremain=size;
|
||||||
|
|
||||||
|
while(1)
|
||||||
|
{
|
||||||
|
SPI_Flash_Read(SPI_FLASH_BUF,secpos*4096,4096);
|
||||||
|
|
||||||
|
for(i=0;i<secremain;i++)
|
||||||
|
{
|
||||||
|
if(SPI_FLASH_BUF[secoff+i]!=0XFF)break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(i<secremain)
|
||||||
|
{
|
||||||
|
SPI_Flash_Erase_Sector(secpos);
|
||||||
|
|
||||||
|
for(i=0;i<secremain;i++)
|
||||||
|
{
|
||||||
|
SPI_FLASH_BUF[i+secoff]=pBuffer[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
SPI_Flash_Write_NoCheck(SPI_FLASH_BUF,secpos*4096,4096);
|
||||||
|
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
SPI_Flash_Write_NoCheck(pBuffer,WriteAddr,secremain);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(size==secremain){
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
secpos++;
|
||||||
|
secoff=0;
|
||||||
|
|
||||||
|
pBuffer+=secremain;
|
||||||
|
WriteAddr+=secremain;
|
||||||
|
size-=secremain;
|
||||||
|
|
||||||
|
if(size>4096)
|
||||||
|
{
|
||||||
|
secremain=4096;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
secremain=size;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*********************************************************************
|
||||||
|
* @fn SPI_Flash_Erase_Chip
|
||||||
|
*
|
||||||
|
* @brief Erase all FLASH pages.
|
||||||
|
*
|
||||||
|
* @return none
|
||||||
|
*/
|
||||||
|
void SPI_Flash_Erase_Chip(void)
|
||||||
|
{
|
||||||
|
SPI_FLASH_Write_Enable();
|
||||||
|
SPI_Flash_Wait_Busy();
|
||||||
|
GPIO_WriteBit(GPIOA, GPIO_Pin_15, 0);
|
||||||
|
SPI1_ReadWriteByte(W25X_ChipErase);
|
||||||
|
GPIO_WriteBit(GPIOA, GPIO_Pin_15, 1);
|
||||||
|
SPI_Flash_Wait_Busy();
|
||||||
|
}
|
||||||
|
|
||||||
|
/*********************************************************************
|
||||||
|
* @fn SPI_Flash_PowerDown
|
||||||
|
*
|
||||||
|
* @brief Enter power down mode.
|
||||||
|
*
|
||||||
|
* @return none
|
||||||
|
*/
|
||||||
|
void SPI_Flash_PowerDown(void)
|
||||||
|
{
|
||||||
|
GPIO_WriteBit(GPIOA, GPIO_Pin_15, 0);
|
||||||
|
SPI1_ReadWriteByte(W25X_PowerDown);
|
||||||
|
GPIO_WriteBit(GPIOA, GPIO_Pin_15, 1);
|
||||||
|
Delay_Us(3);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*********************************************************************
|
||||||
|
* @fn SPI_Flash_WAKEUP
|
||||||
|
*
|
||||||
|
* @brief Power down wake up.
|
||||||
|
*
|
||||||
|
* @return none
|
||||||
|
*/
|
||||||
|
void SPI_Flash_WAKEUP(void)
|
||||||
|
{
|
||||||
|
GPIO_WriteBit(GPIOA, GPIO_Pin_15, 0);
|
||||||
|
SPI1_ReadWriteByte(W25X_ReleasePowerDown);
|
||||||
|
GPIO_WriteBit(GPIOA, GPIO_Pin_15, 1);
|
||||||
|
Delay_Us(3);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
63
board/TencentOS_Tiny_CH32V307_EVB/User/spi_flash.h
Normal file
63
board/TencentOS_Tiny_CH32V307_EVB/User/spi_flash.h
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
#ifndef __SPI_FLASH_H
|
||||||
|
#define __SPI_FLASH_H
|
||||||
|
#include "debug.h"
|
||||||
|
#include "string.h"
|
||||||
|
|
||||||
|
/* Winbond SPIFalsh ID */
|
||||||
|
#define W25Q80 0XEF13
|
||||||
|
#define W25Q16 0XEF14
|
||||||
|
#define W25Q32 0XEF15
|
||||||
|
#define W25Q64 0XEF16
|
||||||
|
#define W25Q128 0XEF17
|
||||||
|
|
||||||
|
/* Winbond SPIFalsh Instruction List */
|
||||||
|
#define W25X_WriteEnable 0x06
|
||||||
|
#define W25X_WriteDisable 0x04
|
||||||
|
#define W25X_ReadStatusReg 0x05
|
||||||
|
#define W25X_WriteStatusReg 0x01
|
||||||
|
#define W25X_ReadData 0x03
|
||||||
|
#define W25X_FastReadData 0x0B
|
||||||
|
#define W25X_FastReadDual 0x3B
|
||||||
|
#define W25X_PageProgram 0x02
|
||||||
|
#define W25X_BlockErase 0xD8
|
||||||
|
#define W25X_SectorErase 0x20
|
||||||
|
#define W25X_ChipErase 0xC7
|
||||||
|
#define W25X_PowerDown 0xB9
|
||||||
|
#define W25X_ReleasePowerDown 0xAB
|
||||||
|
#define W25X_DeviceID 0xAB
|
||||||
|
#define W25X_ManufactDeviceID 0x90
|
||||||
|
#define W25X_JedecDeviceID 0x9F
|
||||||
|
|
||||||
|
#define SIZE sizeof(TEXT_Buf)
|
||||||
|
u8 SPI_FLASH_BUF[4096];
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
u8 SPI1_ReadWriteByte(u8 TxData);
|
||||||
|
void SPI_Flash_Init(void);
|
||||||
|
u8 SPI_Flash_ReadSR(void);
|
||||||
|
void SPI_FLASH_Write_SR(u8 sr);
|
||||||
|
void SPI_Flash_Wait_Busy(void);
|
||||||
|
void SPI_FLASH_Write_Enable(void);
|
||||||
|
void SPI_FLASH_Write_Disable(void);
|
||||||
|
u16 SPI_Flash_ReadID(void);
|
||||||
|
void SPI_Flash_Erase_Sector(u32 Dst_Addr);
|
||||||
|
void SPI_Flash_Read(u8* pBuffer,u32 ReadAddr,u16 size);
|
||||||
|
void SPI_Flash_Write_Page(u8* pBuffer,u32 WriteAddr,u16 size);
|
||||||
|
void SPI_Flash_Write_NoCheck(u8* pBuffer,u32 WriteAddr,u16 size);
|
||||||
|
void SPI_Flash_Write(u8* pBuffer,u32 WriteAddr,u16 size);
|
||||||
|
void SPI_Flash_Erase_Chip(void);
|
||||||
|
void SPI_Flash_PowerDown(void);
|
||||||
|
void SPI_Flash_WAKEUP(void);
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Reference in New Issue
Block a user