Onenet Unicorn Seat application development nine: Communicate with SD card and save data

Source: Internet
Author: User
Tags file info

Because of the large amount of data that needs to be recorded, and sometimes the user is inconvenient to upload data in real time, it is required to use SD card to store data and then manually collect uploads. For this we have chosen a Universal SD card reader and writer.

1 , Card reader Introduction

The card reader integrates the SD card specification and the FAT file format specification, which can be stored in the file on the SD card as long as the communication protocol stipulated in this module is used. The reader connection facilitates the use of serial communication methods, such as:

The Reader block communication protocol is relatively simple, the communication protocol of this module is divided into two parts: command sending and command, in which the command format consists of 4 parts, command identification code (0x55 0xAA), command number, number of bytes (number of parameters, 2 bytes, first send low byte, then send High byte), Parameters (depending on the command), checksum (in addition to the command identifier and checksum itself, all the low 8-bit data of the sum of the sent data). Command format

The answer is divided into two parts: the execution of the command (code will be appendix 1), Data. The data differs depending on the command.

2 , Hardware connection

Because the use of serial communication, so the hardware connection is relatively simple. The USART1 (PA9:USART1_TX,PA10:USART1_RX) port on the Kylin seat has been led to the J2_6 and j2_5 of the J2 terminal row, so we are using this interface, as far as the 5V power supply and grounding as well as the control and status signals are suspended with the corresponding pins.

3 , Software design

Next we write the software that reads and writes the SD card according to the agreement, mainly realizes the state detection, creates the file, opens the file, writes the file, closes the file, saves the file and obtains the file information and so on.

( 1 ) Gets the status command for the system

The Get System Status command is used to get the current state of the module. The command encoding is: 0x01, the command format is as follows:

 //  detect system SD card status   uint8_t getsdcardstatus ( void   6 ]={0x55 , 0xaa , 0x01 , 0x00  , 0x00 , 0x01   };  uint8_t statusbyte  =0xaa  ;  Statusbyte  = SendCommand (Commandtext,6   50   return   Statusbyte;}  

( 2 ) to create a file command

The Create File command provides the ability for the host to create files. The file name in the N-byte 8.3 file format (string format, that is, the file name ends with 0), which is the 8-byte base file name (the module does not support Chinese character coding, the letter is case-insensitive), and the 3-byte extension. The command code is: 0x02, the command format is as follows:

//create file, return operation statusuint8_t CreateFile (uint8_t filename[8]) {uint8_t commandtext[ +]={0x55,0xAA,0x02,0x0D,0x00,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x2e,0x74,0x78,0x74,0x00,0x41};  uint8_t Statusbyte; Statusbyte=0xaa;  uint16_t i;  for(i=0;i<8; i++) {Commandtext[i+5]=Filename[i]; } uint8_t Checksum=0x00;  for(i=2;i< -; i++) {Checksum+=Commandtext[i]; } commandtext[ -]=checksum; Statusbyte= SendCommand (CommandText, +); returnStatusbyte;}

(3) Open File Command

This command provides the host with the ability to open files. The file name in the N-byte 8.3 file format (string format, that is, the file name ends with 0), which is the 8-byte base file name (the module does not support Chinese character coding, the letter is case-insensitive), and the 3-byte extension. The command encoding is: The 0X06 command is formatted as follows, where the number is 2 bytes and the low byte is sent first:

//Open Fileuint8_t OpenFile (uint8_t filename[8]) {uint8_t commandtext[ +]={0x55,0xAA,0x06,0x0D,0x00,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x2e,0x74,0x78,0x74,0x00,0x45};//Open File Command 0x06uint8_t Statusbyte=0xaa;  uint16_t i;  for(i=0;i<8; i++) {Commandtext[i+5]=Filename[i]; } uint8_t Checksum=0x00;  for(i=2;i< -; i++) {Checksum+=Commandtext[i]; } commandtext[ -]=checksum; Statusbyte= SendCommand (CommandText, +); returnStatusbyte;}

( 4 ) Get file Information command

This command provides the host with the ability to read the file pointer value and file size of the currently open file. The command encoding is: 0x09, and its command format is as follows:

//get file Info commandvoidgetfilestatus (uint8_t rxdata[]) {uint8_t commandtext[6]={0x55,0xAA,0x09,0x00,0x00,0x09};//get file Information command 0x09uint16_t i;  for(i=0;i<6; i++)  {    //wait for delivery to end     while(Usart_getflagstatus (UART4, usart_flag_txe) = =RESET) {    }    //write a byte to the corresponding serial port to transmit the data registerUsart_senddata (UART4, commandtext[i]); } delayms ( -);  for(i=0;i<9; i++)  {    //Wait bytes are fully received by the corresponding serial port//While (Usart_getflagstatus (UART4, usart_flag_rxne) = = RESET)//{    //}    //gets the bytes receivedRxdata[i]=Usart_receivedata (UART4); }}

( 5 ) write File command

This command provides the host with the ability to write data to the open file. Each write a data file pointer automatically add 1, when the data is finished, the file pointer points to the last data address plus 1 position. The command code is: 0x05, the command format is as follows, where the number is 2 bytes, the low byte is sent first, the starting address occupies 4 bytes, the low byte is sent first:

//writes the file, returns the state of the write Operationuint8_t writetofile (uint8_t*address,uint8_t data[],uint16_t datalength) {uint16_t Count=datalength+Ten+ +; uint8_t commandtext[ -]; uint8_t Statusbyte=0xaa;  uint16_t i; commandtext[0]=0x55; commandtext[1]=0xAA; commandtext[2]=0x05; commandtext[3]=datalength+4+ +; commandtext[4]=0x00; commandtext[5]=0xFF; commandtext[6]=0xFF; commandtext[7]=0xFF; commandtext[8]=0xFF;  for(i=0; i<datalength;i++) {Commandtext[i+9]=Data[i]; } commandtext[datalength+9]= (savedate[0]/Ten)+0x30; Commandtext[datalength+Ten]= (savedate[0]%Ten)+0x30; Commandtext[datalength+ One]=0x2D; Commandtext[datalength+ A]= (savedate[1]/Ten)+0x30; Commandtext[datalength+ -]= (savedate[1]%Ten)+0x30; Commandtext[datalength+ -]=0x2D; Commandtext[datalength+ the]= (savedate[2]/Ten)+0x30; Commandtext[datalength+ -]= (savedate[2]%Ten)+0x30; Commandtext[datalength+ -]=0x20; Commandtext[datalength+ -]= (savedate[3]/Ten)+0x30; Commandtext[datalength+ +]= (savedate[3]%Ten)+0x30; Commandtext[datalength+ -]=0x3A; Commandtext[datalength+ +]= (savedate[4]/Ten)+0x30; Commandtext[datalength+ A]= (savedate[4]%Ten)+0x30; Commandtext[datalength+ at]=0x3A; Commandtext[datalength+ -]= (savedate[5]/Ten)+0x30; Commandtext[datalength+ -]= (savedate[5]%Ten)+0x30; Commandtext[datalength+ -]=0x0D; Commandtext[datalength+ -]=0x0A; uint8_t Checksum=0x00;  for(i=2; i<count-1; i++) {Checksum+=Commandtext[i]; } Commandtext[count-1]=checksum; Statusbyte=SendCommand (Commandtext,count); returnStatusbyte;}

( 6 ) Save File Command

This command provides the host with the ability to save the current open file, in order to prevent the frequent writing of the SD card, each time the data into the module is stored in the module's 512-byte sector buffer, so in order to prevent data loss, complete the transfer of all data, to send save File command to save the file. The command code is: 0x04, the command format is as follows:

 //  save file, return operation execution status  uint8_t SaveFile ( void   6 ]={0x55 , 0xaa , 0x04 , 0x00 , 0x00 , 0x04 }; //  save File command 0x04   uint8_t statusbyte  =0xaa  ;  Statusbyte  = SendCommand (Commandtext,6   return   Statusbyte;}  

( 7 ) Close File command

This command provides the host with the ability to close the currently open file. It is not possible to execute these commands until the file is created, the folder is created, and the file is opened before you open it, otherwise the return fails. The command code is: 0x08, the command format is as follows:

 //  Close file, return operation execution status  uint8_t closefile ( void   19 ]={ 0x55 , 0xaa , 0x08 , 0x00 , 0x00 , 0x08 }; //  close file command 0x08   uint8_t statusbyte  =0xaa  ;  Statusbyte  = SendCommand (Commandtext,6   return   Statusbyte;}  

After writing the program, we test to write some data in the format we want. The file is saved as a text file, with the time file name and the data format consistent with the expected. This is the SD card read and write completed.

Onenet Unicorn Seat application development nine: Communicate with SD card and save data

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.