libeasymcp2221 2.0.3
Functions
mcp2221_flash.h File Reference

Low-level access to MCP2221 persistent flash sections. More...

#include <stddef.h>
#include <stdint.h>
#include "mcp2221.h"
Include dependency graph for mcp2221_flash.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

mcp2221_error_code_t mcp2221_flash_read (mcp2221_t *dev, uint8_t section, uint8_t out[60])
 Read one MCP2221 flash section. More...
 
mcp2221_error_code_t mcp2221_flash_write (mcp2221_t *dev, uint8_t section, const uint8_t data[60])
 Write one MCP2221 flash section using the legacy 60-byte payload. More...
 
mcp2221_error_code_t mcp2221_flash_write_ex (mcp2221_t *dev, uint8_t section, const uint8_t *data, size_t data_len)
 Write a length-delimited MCP2221 Write Flash Data payload. More...
 
mcp2221_error_code_t mcp2221_flash_send_password (mcp2221_t *dev, const uint8_t pwd[8])
 Send the eight-byte flash access password. More...
 

Detailed Description

Low-level access to MCP2221 persistent flash sections.

Definition in file mcp2221_flash.h.

Function Documentation

◆ mcp2221_flash_read()

mcp2221_error_code_t mcp2221_flash_read ( mcp2221_t dev,
uint8_t  section,
uint8_t  out[60] 
)

Read one MCP2221 flash section.

The raw 60-byte payload returned by the selected section is copied to out without further interpretation.

Public section identifiers include:

  • MCP2221_FLASH_DATA_CHIP_SETTINGS
  • MCP2221_FLASH_DATA_GP_SETTINGS
  • MCP2221_FLASH_DATA_USB_MANUFACTURER
  • MCP2221_FLASH_DATA_USB_PRODUCT
  • MCP2221_FLASH_DATA_USB_SERIALNUM
  • MCP2221_FLASH_DATA_CHIP_SERIALNUM
Parameters
[in]devOpen MCP2221 device handle.
[in]sectionFlash section identifier. Must be one of the public flash section identifiers.
[out]outBuffer receiving exactly 60 section-data bytes.
Returns
MCP2221_ERR_OK on success, MCP2221_ERR_FLASH_READ when the device rejects the flash-read command, MCP2221_ERR_INVALID for invalid arguments, or another mcp2221_error_code_t value on failure.

◆ mcp2221_flash_send_password()

mcp2221_error_code_t mcp2221_flash_send_password ( mcp2221_t dev,
const uint8_t  pwd[8] 
)

Send the eight-byte flash access password.

This command supplies the password used by subsequent flash-write commands on password-protected devices. The MCP2221 does not validate pwd when this command is accepted; the password is checked only when a flash write is attempted. MCP2221_ERR_OK therefore means that the password command was accepted, not that pwd was correct.

Parameters
[in]devOpen MCP2221 device handle.
[in]pwdEight-byte flash access password.
Returns
MCP2221_ERR_OK when the password command is accepted, MCP2221_ERR_FLASH_PASSWD when the device refuses the password command (for example after the failed-write password limit has been reached), MCP2221_ERR_INVALID for invalid arguments, or another mcp2221_error_code_t value on failure.
Warning
Three flash writes attempted with an incorrect supplied password exhaust the device's password-attempt limit. The MCP2221 then refuses further passwords until the device is reset. Do not retry password-protected flash writes blindly after a write fails.

◆ mcp2221_flash_write()

mcp2221_error_code_t mcp2221_flash_write ( mcp2221_t dev,
uint8_t  section,
const uint8_t  data[60] 
)

Write one MCP2221 flash section using the legacy 60-byte payload.

The 60 bytes are copied to Write Flash Data report bytes 2 through 61. This is sufficient for chip settings, GP settings, and USB string descriptors of up to 29 UTF-16 code units. Use mcp2221_flash_write_ex() when the complete 62-byte write payload is required.

Parameters
[in]devOpen MCP2221 device handle.
[in]sectionWritable flash section identifier. Valid values are MCP2221_FLASH_DATA_CHIP_SETTINGS through MCP2221_FLASH_DATA_USB_SERIALNUM. MCP2221_FLASH_DATA_CHIP_SERIALNUM is read-only.
[in]dataExactly 60 Write Flash Data payload bytes, corresponding to report bytes 2 through 61.
Returns
MCP2221_ERR_OK on success, MCP2221_ERR_FLASH_WRITE when the device rejects the flash-write command, MCP2221_ERR_INVALID for invalid arguments, or another mcp2221_error_code_t value on failure.
Warning
This is a low-level persistent write. The caller is responsible for supplying a valid payload for the selected section.
See also
mcp2221_flash_write_ex()

◆ mcp2221_flash_write_ex()

mcp2221_error_code_t mcp2221_flash_write_ex ( mcp2221_t dev,
uint8_t  section,
const uint8_t *  data,
size_t  data_len 
)

Write a length-delimited MCP2221 Write Flash Data payload.

Bytes from data are copied starting at report byte 2, after the command and flash-section bytes. Up to 62 bytes can therefore be supplied. This permits a maximum-length MCP2221 USB string descriptor: its two descriptor metadata bytes followed by 60 UTF-16LE data bytes.

For USB manufacturer, product, and serial-number sections, data starts with the USB descriptor length and descriptor type (0x03), followed by the UTF-16LE string data. The caller remains responsible for constructing a section-valid payload.

Parameters
[in]devOpen MCP2221 device handle.
[in]sectionWritable flash section identifier. Valid values are MCP2221_FLASH_DATA_CHIP_SETTINGS through MCP2221_FLASH_DATA_USB_SERIALNUM. MCP2221_FLASH_DATA_CHIP_SERIALNUM is read-only.
[in]dataWrite Flash Data payload copied to report byte 2 onward.
[in]data_lenNumber of bytes in data. Must be from 1 through 62.
Returns
MCP2221_ERR_OK on success, MCP2221_ERR_FLASH_WRITE when the device rejects the flash-write command, MCP2221_ERR_INVALID for an invalid section, pointer, or payload length, or another mcp2221_error_code_t value on failure.
Warning
This is a low-level persistent write. The caller is responsible for supplying a valid payload for the selected section.