Method and system for reading and writing MCU embedded memory
Through the combination of upper computer and emulator, the read and write operations of the MCU chip are realized, which solves the problem of tight resources of JTAG and SWD programming and debugging pins, improves data read and write and debugging efficiency, and ensures the normal operation of the MCU chip.
Patent Information
- Application Number
- CN202411217662.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-09-02
- Publication Date
- 2024-12-27
AI Technical Summary
The standard JTAG and SWD programming and debugging methods occupy a large number of chip pins, resulting in tight pin resources, and the need to redesign the interface circuit and programming methods, reducing read, write and debugging efficiency.
The combination of upper computer, emulator and MCU chip is adopted to provide timing through the emulator to realize read and write operations of the MCU chip, avoid the use of JTAG and SWD interfaces, reduce pin resource usage, and improve data read and write and debugging efficiency.
It effectively reduces the use of chip pin resources, improves data reading, writing and debugging efficiency, avoids the need to redesign interface circuits and programming methods, and ensures the normal operation and program download of MCU chips.
Smart Images

Figure CN119200970A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the technical field of data reading and writing, and in particular to a method and system for reading and writing a memory embedded in an MCU. Background Art
[0002] MCU (Microcontroller Unit) refers to a microcontroller. There are many types of MCUs on the market today, and they are widely used. When using MCU for solution design, programming and debugging are required. The program needs to be written to the internal memory or read out for verification through a specific interface. At present, the programming interface of MCU is basically in the form of JTAG or SWD. The JTAG interface includes power, ground, clock, input, output and other pins. SWD includes power, ground, clock, input and output pins. They usually read and write internal memory using a dedicated hardware circuit in the chip. One end of the circuit is connected to the emulator through JTAG or SWD, and can receive commands or data sent by the emulator, and return data to the emulator. The other end is connected to the internal memory to delete and read and write the memory.
[0003] However, standard JTAG and SWD require more pins for programming and debugging, which will lead to tight chip pin resources, and may require redesign of interface circuits and programming methods, reducing reading, writing and debugging efficiency. Summary of the invention
[0004] In order to help solve the problem that standard JTAG and SWD require more pins for programming and debugging, resulting in tight chip pin resources, which in turn requires redesigning interface circuits and programming methods, reducing the efficiency of reading, writing and debugging, the present application provides a method and system for reading and writing MCU embedded memory, adopting the following technical solution: the method is applied to the reading and writing system of MCU embedded memory, the reading and writing system of MCU embedded memory includes a host computer, an emulator and an MCU chip, and the method includes:
[0005] The host computer issues an operation instruction, wherein the operation instruction includes a read operation instruction or a write operation instruction;
[0006] The simulator receives the operation instruction and transmits the operation instruction to the MCU chip;
[0007] The simulator configures the MCU chip and controls the MCU chip to execute an operation corresponding to the operation instruction.
[0008] In a specific implementation scheme, when the operation instruction is a write operation instruction, the emulator receives the operation instruction, and transmits the operation instruction to the MCU chip including:
[0009] The simulator receives the write operation instruction and sends a write data frame to the MCU chip;
[0010] The MCU chip feeds back a write response frame to the emulator.
[0011] In a specific implementation scheme, the MCU chip includes a FLASH memory and an online debugging module, and the online debugging module includes a status configuration register, an instruction register, and an access configuration register;
[0012] The simulator configures the MCU chip and controls the MCU chip to perform an operation corresponding to the operation instruction, including:
[0013] The simulator configures the status configuration register to set the working status of the MCU chip;
[0014] The simulator writes the write operation instruction into the instruction register;
[0015] The simulator configures the access configuration register, specifies the address of the FLASH memory to be accessed, and controls the MCU chip to write the data to be written into the specified address of the FLASH memory in sequence.
[0016] In a specific implementation scheme, when the operation instruction is a read operation instruction, the emulator receives the operation instruction, and transmits the operation instruction to the MCU chip including:
[0017] The simulator receives the read operation instruction and sends a read data frame to the MCU chip;
[0018] The MCU chip feeds back a data frame to the emulator;
[0019] The emulator sends a confirmation frame to the MCU chip;
[0020] The MCU chip feeds back a read response frame to the emulator.
[0021] In a specific implementation scheme, the MCU chip includes a FLASH memory and an online debugging module, and the online debugging module includes a status configuration register, an instruction register, and an access configuration register;
[0022] The simulator configures the MCU chip and controls the MCU chip to perform an operation corresponding to the operation instruction, including:
[0023] The simulator configures the status configuration register to set the working status of the MCU chip;
[0024] The simulator writes the read operation instruction into the instruction register;
[0025] The simulator configures the access configuration register, specifies the address of the FLASH memory to be accessed, and controls the MCU chip to read the data to be read from the specified address of the FLASH memory in sequence.
[0026] In a specific implementation scheme, the FLASH memory includes an option byte area, and the method further includes:
[0027] When the host computer obtains the MCU running firmware, the host computer downloads the MCU running firmware to the FLASH memory through the emulator, and the MCU running firmware includes parameters for configuring the internal modules of the MCU chip in the option byte area;
[0028] The simulator reads the option byte area from the FLASH memory to configure the parameters of the internal modules of the MCU chip;
[0029] The simulator writes the parameters used by the option byte area to configure the internal modules of the MCU chip into the option byte area.
[0030] In a second aspect, the present application provides a reading and writing system of an MCU embedded memory, which adopts the following technical solution: the system includes a host computer, an emulator and an MCU chip;
[0031] The host computer is used to issue operation instructions to the MCU chip, and the operation instructions include reading data and writing data;
[0032] The simulator is connected to the host computer for providing timing for the MCU chip, receiving the operation instruction and transmitting the operation instruction to the MCU chip;
[0033] The MCU chip is connected to the simulator for receiving and executing operation instructions issued by the host computer.
[0034] In a specific implementation scheme, the MCU chip includes a CPU core, a FLASH controller, a FLASH memory and an online debugging module;
[0035] The CPU core is used to run programs and control hardware peripherals, and execute operation instructions issued by the host computer through the FLASH controller;
[0036] The FLASH controller is connected to the CPU core for receiving an operation instruction from the host computer and setting a working state of the FLASH controller according to the operation instruction;
[0037] The FLASH memory is communicatively connected with the FLASH controller and is used for storing data to be written or reading data to be read;
[0038] The online debugging module is connected to the CPU core for controlling the operation mode of the CPU core.
[0039] In a specific implementation scheme, the communication connection mode between the simulator and the host computer is a USB connection; the communication connection mode between the MCU chip and the simulator is a single-wire connection mode of Manchester encoding.
[0040] In a specific implementation scheme, the FLASH memory includes an option byte area, and the option byte area is used to configure the internal modules of the MCU chip.
[0041] In summary, this application has the following beneficial technical effects:
[0042] The host computer, emulator and MCU chip are used to implement the data reading and writing operations. The emulator provides the timing of the chip working for the MCU chip, so that the chip can work normally. In the embodiment of the present application, the use of JTAG and SWD for programming and debugging is avoided, so that the use of the pin resources of the chip itself can be reduced as much as possible, especially for some chips with tight pin resources, there is no need to redesign the interface circuit and programming method, so as to improve the efficiency of data reading and writing in the chip and the efficiency of chip debugging as much as possible. Using this device and method, the data can be downloaded and read back from the MCU embedded memory of Manchester coded single-line communication, and the data can be verified after reading back, so that the MCU can run the program normally. In addition, the parameters configured by Option are integrated into the MCU running program to avoid operation errors and cause the chip to be scrapped. BRIEF DESCRIPTION OF THE DRAWINGS
[0043] Figure 1 It is a flowchart of a method for reading and writing an MCU embedded memory in an embodiment of the present application;
[0044] Figure 2 It is a schematic diagram of the process of the simulator writing to the online debugging module in the embodiment of the present application;
[0045] Figure 3 It is a schematic diagram of the process of the simulator reading the online debugging module in the embodiment of the present application;
[0046] Figure 4 It is a schematic diagram of the framework of the reading and writing system of the MCU embedded memory in the embodiment of the present application;
[0047] Figure 5It is a timing diagram of Manchester coding communication in an embodiment of the present application. DETAILED DESCRIPTION
[0048] The following is combined with Figure 1-5 This application is described in further detail.
[0049] The embodiment of the present application discloses a method for reading and writing an embedded memory of an MCU. The reading and writing system is implemented based on Manchester communication coding. The reading and writing system can realize the reading and writing of MCU chip data, thereby minimizing the occupation of chip pins during programming and debugging.
[0050] There are many types of MCUs (microcontrollers) on the market today, and they are also widely used. When using MCU for solution design, programming and debugging are required. The program needs to be written to the internal memory or read out for verification through a specific interface. At present, the programming interface of MCU is basically in the form of JTAG or SWD. The JTAG interface includes power, ground, clock, input, output and other pins. SWD includes power, ground, clock, input and output pins. They usually use dedicated hardware circuits in the chip to read and write internal memory. One end of the circuit is connected to the emulator through JTAG or SWD, and can receive commands or data sent by the emulator, and return data to the emulator. The other end is connected to the internal memory to delete and read and write the memory. However, standard JTAG and SWD require more pins for programming and debugging, which will lead to tight chip pin resources, which may lead to the need to redesign the interface circuit and programming method, reducing the efficiency of reading, writing and debugging.
[0051] In addition, when using MCU for application development, since different applications require different configurations for the modules in the chip, this configuration parameter needs to take effect during the chip power-on process, before the MCU program runs. The current common practice is to use a burner to burn the solution before it leaves the factory. These parameters are usually stored in the option area of the FLASH memory of the MCU chip. The option area is also called the option byte area. The usual method is to generate the MCU program and the Option separately. However, in the case where the MCU program and the option byte area are generated separately, it is easy to cause misoperation. For example, there are several MCU programs and several option byte area configurations, and one MCU program corresponds to an option byte area configuration. The solution development and program burning are often not done by the same engineer, so it is easy to be misplaced, which may lead to rework or chip scrapping. Therefore, in order to help improve the efficiency of reading, writing and debugging, and avoid rework or chip scrapping as much as possible, the present application provides a method for reading and writing MCU embedded memory.
[0052] Reference Figure 1 , the reading and writing methods of the MCU embedded memory include:
[0053] S10, the host computer issues an operation instruction, which includes a read operation instruction or a write operation instruction.
[0054] Specifically, the host computer issues a read or write operation instruction, a read operation instruction means that the host computer needs to read the required data from the MCU chip, and a write operation instruction means that the host computer needs to write data or programs into the MCU chip for storage. In the embodiment of the present application, the host computer can be a PC.
[0055] S20, the emulator receives the operation instruction and transmits the operation instruction to the MCU chip.
[0056] Specifically, the host computer needs to use the emulator to perform read and write operations on the MCU chip. The emulator needs to provide the timing of the MCU operation to ensure that the MCU can operate normally. The emulator can be understood as the middleware for the host computer to perform read and write operations on the MCU chip. The emulator receives the operation instructions sent by the host computer and then transmits the received operation instructions to the MCU chip.
[0057] S30, the simulator configures the MCU chip, and controls the MCU chip to execute an operation corresponding to the operation instruction.
[0058] Specifically, the emulator configures the internal modules or registers of the MCU and controls the MCU chip to perform operations corresponding to the operation instructions. Specifically, it can be understood that if it is a read operation instruction, the MCU reads the stored data and transmits it to the host computer through the emulator. If it is a write operation instruction, the MCU chip writes the data to be written received from the emulator to the MCU for storage.
[0059] In the present application, a host computer, an emulator and an MCU chip are used to implement data reading and writing operations. The emulator provides the MCU chip with the timing of the chip operation so that the chip can work normally. In the present application embodiment, the use of JTAG and SWD for programming and debugging is avoided, thereby minimizing the use of the chip's own pin resources. In particular, for some chips with limited pin resources, there is no need to redesign the interface circuit and programming method, thereby maximizing the efficiency of data reading and writing in the chip and the efficiency of chip debugging.
[0060] In one embodiment, referring to Figure 2 When the operation instruction is a write operation instruction, the simulator receives the operation instruction, and the steps of transmitting the operation instruction to the MCU chip can be specifically performed as follows:
[0061] First, the emulator receives the write operation instruction and sends a write data frame to the MCU chip, and the MCU chip feeds back a write response frame to the emulator; Figure 2, the emulator sends a write data frame, and after receiving the write data frame, the MCU chip feeds back an ECHO frame to the emulator, wherein the ECHO frame is a request response frame, which can be understood as a reply feedback from the MCU chip to the emulator after receiving the write data frame to inform the emulator that it has been received. In the embodiment of the present application, the format of the write data frame can be set according to the format in Table 1 below:
[0062] Table 1 Example of writing data frame format
[0063] BOF TOF address Data length data 01 10 7-bit 6 bits xxxxxx
[0064] The write response frame fed back by the MCU, that is, the ECHO frame format can be set according to the format in Table 2:
[0065] Table 2 ECHO frame format example
[0066] BOF TOF 01 0
[0067] In Table 1 and Table 2 above, BOF and TOF are fixed bits used for positioning during data transmission, indicating the start of the transmission frame; the address refers to the register address of the online debugging module, and the data length refers to the number of bits of the data to be written, followed by the actual data value to be written. The ECHO frame is the reply from the MCU to the emulator. When the emulator receives this ECHO, it indicates that the previously sent data frame has been received by the MCU, otherwise the data frame can be resent.
[0068] In one embodiment, the MCU chip includes a FLASH memory and an online debugging module, and the online debugging module includes a status configuration register, an instruction register, and an access configuration register. The FLASH memory is contained in the MCU chip, and data writing is writing into the FLASH memory of the MCU chip, and data reading is reading the required data from the FLASH memory in the MCU chip. The MCU chip also includes an online debugging module, which is also an OCDS module. The online debugging module includes registers related to reading and writing the FLASH memory, including a status configuration register, an instruction register, and an access configuration register. The status configuration register, also known as OCDSCTRL, is used to configure the working state of the online modulation module (OCDS); the instruction register, also known as OCDSINSTR, is the instruction register of the online debugging module; the access configuration register, also known as OCDSMAC, is used to configure which memory the online debugging module accesses.
[0069] The steps of configuring the MCU chip by the emulator and controlling the MCU chip to perform the operation corresponding to the operation instruction can be specifically performed as follows: First, the emulator configures the status configuration register, that is, the OCDSCTRL register, sets the working state of the MCU chip, and puts the MCU chip in the debugging mode. At this time, the operation of the MCU chip is controlled by the online debugging module. After that, the emulator writes the write operation instruction into the instruction register, that is, the OCDSINSTR register, and allows the MCU chip to execute the instruction in a single step. After that, the emulator configures the access configuration register, that is, configures the OCDSMAC register, specifies the address of the FLASH memory to be accessed, enables the MCU chip to access the FLASH memory, and controls the MCU chip to write the data to be written into the address of the specified FLASH memory in sequence.
[0070] It should be noted that the FLASH write operation is implemented by single-step execution of the operation instruction. In the embodiment of the present application, the specific instruction used is MOV DPTR, #data16, which is used to write the immediate value #data16 to the DPTR register in the MCU chip. The binary instruction value of MOV DPTR, #data16 is (0x900000+#data16). #data16 represents the address of the Flash memory currently being accessed. When a byte of data is written to the address, the current address of the Flash memory being accessed will automatically increase by 1, so data can be continuously written to the Flash memory. If you want to write a data buffer pdata[len] of len bytes to Flash, the address starts from address, and the specific implementation steps can be:
[0071] Configure OCDSCTRL to put the MCU in debug mode; write the value (0x900000+address) to the OCDSINSTR register; configure OCDSCTRL to single-step the instructions in OCDSINSTR; configure OCDSMAC to enable the MCU to access Flash; i=0~len-1 loop, write data pdata[i] to address 0x00; end data writing.
[0072] In the present application scheme, by configuring the registers related to reading and writing the FLASH memory in the online debugging module, the data received in the online debugging module is stored in the corresponding address of the FLASH memory according to the instruction requirements, so as to realize the writing of data from the host computer to the FLASH memory of the MCU chip. The entire data writing process does not require the use of JTAG or SWD, that is, there is no need to use the chip pins additionally, thereby reducing the use of chip pin resources and improving the reading and writing efficiency.
[0073] In one embodiment, referring to Figure 3When the operation instruction is a read operation instruction, the simulator receives the operation instruction, and the steps of transmitting the operation instruction to the MCU chip can be specifically performed as follows:
[0074] First, the emulator receives the read operation instruction and sends a read data frame to the MCU chip; then, the MCU chip feeds back a data frame to the emulator; the emulator sends a confirmation frame to the MCU chip; the MCU chip feeds back a read response frame to the emulator; refer to Figure 3 , the emulator sends a read data frame. After receiving the read data frame, the MCU chip generates a data frame for the data to be read and feeds it back to the emulator. The data frame can be a DATA data frame. After receiving the DATA data frame, the emulator feeds back a confirmation frame to the MCU chip. The confirmation frame can be an ACKNOWLEDGE frame. After receiving the ACKNOWLEDGE frame, the MCU chip feeds back a read response frame to the emulator. The read response frame can be an ECHO frame. In the read operation, the ECHO frame can be understood as the emulator sending a confirmation signal to the MCU chip after reading the data, and the MCU chip gives a reply feedback to the emulator after receiving the confirmation signal. In the embodiment of the present application, the format of the read response frame, that is, the ECHO frame, can be set with reference to the example in Table 2, and the format of the read data frame can be set according to the format in Table 3 below:
[0075] Table 3 Read data frame format example
[0076] BOF TOF address Data length 01 11 7-bit 6 bits
[0077] The format of the DATA frame fed back by the MCU chip can be:
[0078] Table 4 DATA data frame format example
[0079] BOF TOF data 01 1 xxxxxx
[0080] The ACKNOWLEDGE frame fed back by the emulator to the MCU chip can be:
[0081] Table 5 ACKNOWLEDGE frame format example
[0082] BOF TOF 01 00
[0083] In the above Tables 3, 4 and 5, BOF and TOF are fixed bits, indicating the start of the transmission frame, the address indicates the address of the register to read the data, and the data length in the DAT data frame indicates the length of the data contained in the register. After the emulator sends out the read data frame, the MCU will package the value in the register into a DATA frame and send it back to the emulator. The number of bits of the data here is determined by the data length in the previous read data frame. After receiving the data, the emulator sends an ACKNOWLEDGE frame to indicate that the data has been received, and then the MCU replies ECHO. The ECHO frame in the read operation is the same as that in the above data writing process.
[0084] In one embodiment, the MCU chip includes a FLASH memory and an online debugging module, and the online debugging module includes a status configuration register, an instruction register, and an access configuration register. The FLASH memory is contained in the MCU chip, and data writing is writing into the FLASH memory of the MCU chip, and data reading is reading the required data from the FLASH memory in the MCU chip. The MCU chip also includes an online debugging module, which is also an OCDS module. The online debugging module includes registers related to reading and writing the FLASH memory, including a status configuration register, an instruction register, and an access configuration register. The status configuration register, also known as OCDSCTRL, is used to configure the working state of the online modulation module (OCDS); the instruction register, also known as OCDSINSTR, is the instruction register of the online debugging module; the access configuration register, also known as OCDSMAC, is used to configure which memory the online debugging module accesses.
[0085] The steps of configuring the MCU chip by the emulator and controlling the MCU chip to perform the operation corresponding to the operation instruction can be specifically performed as follows: First, the emulator configures the status configuration register, that is, the OCDSCTRL register, sets the working state of the MCU chip, and puts the MCU in the debugging mode. At this time, the operation of the MCU chip is controlled by the online debugging module (OCDS). After that, the emulator writes the read operation instruction into the instruction register, that is, the OCDSINSTR register, and lets the MCU chip execute the instruction in a single step. After that, the emulator configures the access configuration register, that is, configures the OCDSMAC register, specifies the address of the FLASH memory to be accessed, and lets the MCU chip access the FLASH memory, and controls the MCU chip to read the data to be read from the address of the specified FLASH memory in sequence, one byte at a time.
[0086] It should be noted that the FLASH read operation is also implemented by single-step execution of the operation instruction. The specific instruction used in the above write operation is MOV DPTR, #data16, which writes len bytes of data buffer pdata[len] to Flash, starting from address. Correspondingly, len bytes of data are continuously read from address in the FLASH memory and stored in the buffer pdata[len]. The specific implementation steps can be:
[0087] Configure OCDSCTRL to put the MCU in debug mode; write the value (0x900000+address) to the OCDSINSTR register; configure OCDSCTRL to single-step the instructions in OCDSINSTR; configure OCDSMAC to enable the MCU to access Flash; i=0~len-1 loop, read a byte from address 0x00 and store it in pdata[i]; end of data reading.
[0088] In the present application scheme, the registers related to reading and writing the FLASH memory in the online debugging module are configured to read the data in the corresponding address of the FLASH memory, and the read data is transmitted to the host computer through the emulator, so as to realize reading data from the FLASH memory of the MCU chip and transmitting it to the host computer; the entire data reading process does not require the use of JTAG or SWD, that is, there is no need to use the chip pins additionally, thereby reducing the use of chip pin resources and improving the efficiency of the reading and writing process.
[0089] In one embodiment, the FLASH memory includes an option byte area, which is also the Option area. Considering that the program of the MCU chip and the parameters of the Option configuration are often burned by different engineers, this may cause the MCU program and the parameters of the Option configuration to not match, thereby causing the chip to have problems and become unusable. Therefore, the reading and writing method of the MCU embedded memory also includes:
[0090] When the host computer obtains the MCU running firmware, the host computer downloads the MCU running firmware to the FLASH memory through the emulator. The MCU running firmware includes parameters for configuring the internal modules of the MCU chip in the option byte area; the emulator reads the parameters for configuring the internal modules of the MCU chip in the option byte area from the FLASH memory; the emulator writes the parameters for configuring the internal modules of the MCU chip in the option byte area.
[0091] For MCU chips, the parameters configured by Option usually include the size of the internal capacitor of the crystal oscillator, whether the program is encrypted, the noise filter switch, the gate dog enable, the gate dog overflow cycle selection, the ADC direction, the BGA amplifier configuration, etc. The capacity of these parameters does not exceed 16 bytes, which is very small compared to the capacity of the memory. Usually, the configuration values of these parameters can be written in a certain position of the MCU program, generally placed in the high address of the program. For example, if the program has 64k, the Option configuration parameters are placed in the 16 bytes starting from 0xfff0. If the program has 12k, the Option can be placed at 0x2fff0. After downloading the program to the MCU chip using an emulator or downloader, the Option configuration parameters can be read from a fixed position in the Flash memory (such as 0x2fff0 or 0xfff0) and then written to the Option area of the memory. Among them, the Option area is an area in the FLASH memory. The downloaded MCU program is stored in the FLASH memory, but not in the Option area. Therefore, the Option configuration parameters need to be read out first and then written to the corresponding Option area.
[0092] It should be noted that the MCU running firmware can also be understood as the program running on the MCU chip. Users can usually write it through programming software on the host computer, such as Keil software, to generate a firmware file. The firmware file is a binary file. The binary firmware file is downloaded to the FLASH memory. The specific steps can be:
[0093] Initialize variables add=0, len=0, data_32=0; PC shakes hands with the emulator through Custom HID USB; PC sends a command to erase Flash to the emulator, and the emulator writes the command to the PECMD register of the Flash controller through the OCDS single-wire interface to erase Flash; then a loop is performed, and after the loop ends, the firmware download is completed;
[0094] The specific steps of the loop may be: PC reads 32 bytes of data from the fw.bin file (firmware file) to data_32 in sequence, len=32; if the amount of data read the last time the file is read is less than 32, the actual read data is placed in data_32, len is the actual length of the read data; the data is packaged add+len+data32 and sent to the emulator via USB, and the emulator uses the above Flash write method to download the data to the Flash memory; add=add+len; the loop is exited when the reading of the fw.bin file (firmware file) is completed.
[0095] Ordinary HID device transmission is unidirectional, only from the device to the host computer. Here, the emulator configures the device as a Custom HID, so that the data transmission is bidirectional, and it can receive data from the PC and send data to the PC. At the same time, the polling time of the Custom HID is set to the minimum of 1 ms. A too large default time will affect the program download time. Since the maximum data transmission packet of HID is 64 bytes, and the transmission packet includes information such as address and length in addition to the actual program data, the data transmitted each time is set to 32 bytes here. If it is too small, it will also affect the download speed. The length of the program file to be downloaded is not necessarily a multiple of 32, so the data transmitted in the last time may not be equal to 32.
[0096] If the host computer wants to read data from the Flash memory and store it in the fw.bin firmware file, the following steps can be executed:
[0097] Initialize the variables add = 0, the total length of data to be read total_len, the length read each time len = 32, the number of reads i = 0, data_32 = 0; the PC shakes hands with the emulator through the Custom HID USB; then a loop is performed, and after the loop ends, the firmware reading ends;
[0098] The specific steps of the loop can be: the host computer (PC) sends a read Flash command to the emulator through the USB to read len bytes of data starting from add; the emulator reads the data using the above read Flash method and returns the read data to the PC; the PC stores the data in the fw.bin firmware file; i = i + 1, add = add + len, total_len = total_len - len; if total_len <= 0, then exit the loop; if 0 < total_len < 32, then len = total_len; otherwise len = 32.
[0099] In the solution of this application, when using an MCU for application development, since different applications need to make different configurations for the modules in the chip, one MCU program corresponds to one Option configuration. Since the programs and Option configuration parameters of the MCU chip are often burned by different engineers, this may lead to a mismatch between the MCU program and the Option configuration parameters, and then cause the chip to have problems and become unavailable. Therefore, the MCU program and the Option configuration parameters are downloaded to the FLASH memory together, and then read from the FLASH memory and written into the Option area, so as to avoid the problem of mismatch between the MCU running program and the Option configuration parameters.
[0100] Figure 1FIG. 1 is a flow chart of a method for reading and writing an embedded memory of an MCU in one embodiment. It should be understood that although Figure 1 The steps in the flowchart are shown in sequence as indicated by the arrows, but the steps are not necessarily executed in the order indicated by the arrows; unless otherwise specified in this document, there is no strict order restriction for the execution of the steps, and the steps may be executed in other orders; and Figure 1 At least part of the steps may include multiple sub-steps or multiple stages. These sub-steps or stages are not necessarily executed at the same time, but can be executed at different times. The execution order of these sub-steps or stages is not necessarily sequential, but can be executed in turn or alternately with other steps or at least part of the sub-steps or stages of other steps.
[0101] Based on the above method, an embodiment of the present application also discloses a reading and writing system of an MCU embedded memory.
[0102] Reference Figure 4 ,The system includes a host computer, an emulator and an MCU chip;
[0103] The host computer is used to issue operation instructions to the MCU chip, including reading and writing data; the emulator is connected to the host computer to provide timing for the MCU chip, receive operation instructions and transmit the operation instructions to the MCU chip; the MCU chip is connected to the emulator to receive and execute the operation instructions issued by the host computer. The communication connection between the emulator and the host computer is USB connection; the communication connection between the MCU chip and the emulator is a single-wire connection method of Manchester encoding, refer to Figure 5 , which is a timing diagram of Manchester coding communication. The data changes to 0 when a falling edge occurs in one cycle, and the data changes to 1 when a rising edge occurs in one cycle.
[0104] The MCU chip includes a CPU core, a FLASH controller, a FLASH memory, and an online debugging module; the CPU core is used to run programs and control hardware peripherals, and executes operation instructions issued by the host computer through the FLASH controller; the FLASH controller is connected to the CPU core in communication, and is used to receive operation instructions issued by the host computer, and set the working state of the FLASH controller according to the operation instructions; the FLASH memory is connected to the FLASH controller in communication, and is used to store data that needs to be written or read data that needs to be read; the online debugging module is connected to the CPU core in communication, and is used to control the operation mode of the CPU core. The FLASH memory includes an option byte area, and the option byte area is used to configure the internal modules of the MCU chip.
[0105] Specifically, the MCU chip contains an R8052 CPU core, which is responsible for running programs and controlling hardware peripherals. It performs operations such as erasing, reading and writing on the FLASH memory through the FLASH controller. The FLASH controller contains ISP registers, MCU interfaces, Option interfaces, and online programming interfaces. Among them, the ISP registers usually refer to a set of special registers used to guide and control the internal data programming process of the flash memory. These registers allow external devices to download programs, erase and verify the FLASH chip during system operation; the FLASH controller connects the MCU, system controller and Flash memory. The FLASH controller contains a state machine that receives read / write / erase requests from the upstream interface and generates the corresponding operation timing of the FLASH memory, which includes five states: idle, chip erase, sector erase, programming, and reading. Among them, there is a PECMD register in the ISP register. By writing the corresponding command, the FLASH memory can be erased in its entirety or by sector. The FLASH memory is divided into several areas, including the program area, system adjustment area, and option area. The program area stores the programs running by the MCU, the adjustment area stores the parameters that need to be adjusted when the chip leaves the factory, and the Option area is the configuration of the internal modules of the chip for different application scenarios. OCDS is an online debugging module that can control the operation, stop, single-step execution, read and write registers, RAM, etc. of R8052. The emulator powers on or off the MCU chip through the power cord, sends commands and data to OCDS through a single line, or receives data from OCDS, thereby controlling the operation of the MCU chip. The PC is connected to the emulator via USB. Here, after the emulator runs the firmware, it will inform the PC that it is a Custom HID device, so there is no need to install a driver on the PC, because the Custom HID device is automatically recognized by Windows' built-in driver. Among them, the MCU runs the firmware file, which is stored in the program area of the FLASH memory, and the emulator reads the parameters configured by the Option from the program area, and the emulator writes the parameters configured by the read Option into the Option area.
[0106] This specific embodiment is merely an explanation of the present invention and is not a limitation of the present invention. After reading this specification, those skilled in the art may make non-creative modifications to the present embodiment as needed. However, as long as they are within the scope of the claims of the present invention, they are protected by the patent law.
Claims
1. A method for reading and writing an MCU embedded memory, characterized in that: The method is applied to a read / write system of an MCU embedded memory, wherein the read / write system of the MCU embedded memory includes a host computer, an emulator and an MCU chip, and the method includes: The host computer issues an operation instruction, wherein the operation instruction includes a read operation instruction or a write operation instruction; The simulator receives the operation instruction and transmits the operation instruction to the MCU chip; The simulator configures the MCU chip and controls the MCU chip to execute an operation corresponding to the operation instruction.
2. The method according to claim 1, characterized in that: When the operation instruction is a write operation instruction, the emulator receives the operation instruction, and transmits the operation instruction to the MCU chip, including: The simulator receives the write operation instruction and sends a write data frame to the MCU chip; The MCU chip feeds back a write response frame to the emulator.
3. The method according to claim 2, characterized in that: The MCU chip includes a FLASH memory and an online debugging module, and the online debugging module includes a status configuration register, an instruction register and an access configuration register; The simulator configures the MCU chip and controls the MCU chip to perform an operation corresponding to the operation instruction, including: The simulator configures the status configuration register to set the working status of the MCU chip; The simulator writes the write operation instruction into the instruction register; The simulator configures the access configuration register, specifies the address of the FLASH memory to be accessed, and controls the MCU chip to write the data to be written into the specified address of the FLASH memory in sequence.
4. The method according to claim 1, characterized in that: When the operation instruction is a read operation instruction, the emulator receives the operation instruction, and transmits the operation instruction to the MCU chip, including: The simulator receives the read operation instruction and sends a read data frame to the MCU chip; The MCU chip feeds back a data frame to the emulator; The emulator sends a confirmation frame to the MCU chip; The MCU chip feeds back a read response frame to the emulator.
5. The method according to claim 4, characterized in that: The MCU chip includes a FLASH memory and an online debugging module, and the online debugging module includes a status configuration register, an instruction register and an access configuration register; The simulator configures the MCU chip and controls the MCU chip to perform an operation corresponding to the operation instruction, including: The simulator configures the status configuration register to set the working status of the MCU chip; The simulator writes the read operation instruction into the instruction register; The simulator configures the access configuration register, specifies the address of the FLASH memory to be accessed, and controls the MCU chip to read the data to be read from the specified address of the FLASH memory in sequence.
6. The method according to claim 1, characterized in that: The FLASH memory includes an option byte area, and the method further includes: When the host computer obtains the MCU running firmware, the host computer downloads the MCU running firmware to the FLASH memory through the emulator, and the MCU running firmware includes parameters for configuring the internal modules of the MCU chip in the option byte area; The simulator reads the option byte area from the FLASH memory to configure the parameters of the internal modules of the MCU chip; The simulator writes the parameters used by the option byte area to configure the internal modules of the MCU chip into the option byte area.
7. A read / write system for an MCU embedded memory, characterized in that: The system includes a host computer, a simulator and an MCU chip; The host computer is used to issue operation instructions to the MCU chip, and the operation instructions include reading data and writing data; The simulator is connected to the host computer for providing timing for the MCU chip, receiving the operation instruction and transmitting the operation instruction to the MCU chip; The MCU chip is connected to the simulator for receiving and executing operation instructions issued by the host computer.
8. The reading and writing system of the MCU embedded memory according to claim 7, characterized in that: The MCU chip includes a CPU core, a FLASH controller, a FLASH memory and an online debugging module; The CPU core is used to run programs and control hardware peripherals, and execute operation instructions issued by the host computer through the FLASH controller; The FLASH controller is connected to the CPU core for receiving an operation instruction from the host computer and setting a working state of the FLASH controller according to the operation instruction; The FLASH memory is communicatively connected with the FLASH controller and is used for storing data to be written or reading data to be read; The online debugging module is connected to the CPU core for controlling the operation mode of the CPU core.
9. The reading and writing system of the MCU embedded memory according to claim 7, characterized in that: The communication connection mode between the simulator and the host computer is a USB connection; the communication connection mode between the MCU chip and the simulator is a single-line connection mode of Manchester encoding.
10. The reading and writing system of the MCU embedded memory according to claim 8, characterized in that: The FLASH memory includes an option byte area, and the option byte area is used to configure the internal modules of the MCU chip.
