Skip to content

Commit

Permalink
Write IMXRT10xx image to flash
Browse files Browse the repository at this point in the history
  • Loading branch information
robert committed Dec 7, 2023
1 parent fc8ba74 commit 52ade05
Show file tree
Hide file tree
Showing 12 changed files with 587 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "dcd.h" // pin settings for MIMXRT1020-EVK board
#include "flexspi.h" // peripheral structures
#include "hal.h"
#include "flexspi.h" // peripheral structures

extern uint32_t __isr_vector[];

Expand Down
54 changes: 45 additions & 9 deletions examples/nxp/rt1020-evk-make-baremetal-builtin/flexspi.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,19 @@
#include <stdint.h>
#include <stdbool.h>

typedef struct _lut_sequence
{
uint8_t seqNum; //!< Sequence Number, valid number: 1-16
uint8_t seqId; //!< Sequence Index, valid number: 0-15
uint16_t reserved;
} flexspi_lut_seq_t;
#ifndef FLEXSPI_LUT_OPERAND0
#define FLEXSPI_LUT_OPERAND0(x) (((uint32_t) (((uint32_t) (x)))) & 0xFFU)
#define FLEXSPI_LUT_NUM_PADS0(x) \
(((uint32_t) (((uint32_t) (x)) << 8U)) & 0x300U)
#define FLEXSPI_LUT_OPCODE0(x) \
(((uint32_t) (((uint32_t) (x)) << 10U)) & 0xFC00U)
#define FLEXSPI_LUT_OPERAND1(x) \
(((uint32_t) (((uint32_t) (x)) << 16U)) & 0xFF0000U)
#define FLEXSPI_LUT_NUM_PADS1(x) \
(((uint32_t) (((uint32_t) (x)) << 24U)) & 0x3000000U)
#define FLEXSPI_LUT_OPCODE1(x) \
(((uint32_t) (((uint32_t) (x)) << 26U)) & 0xFC000000U)
#endif

typedef struct _FlexSPIConfig
{
Expand All @@ -30,12 +37,12 @@ typedef struct _FlexSPIConfig
//! Generic configuration, etc.
uint16_t waitTimeCfgCommands; //!< [0x012-0x013] Wait time for all configuration commands, unit: 100us, Used for
//! DPI/QPI/OPI switch or reset command
flexspi_lut_seq_t deviceModeSeq; //!< [0x014-0x017] Device mode sequence info, [7:0] - LUT sequence id, [15:8] - LUt
uint32_t deviceModeSeq; //!< [0x014-0x017] Device mode sequence info, [7:0] - LUT sequence id, [15:8] - LUt
//! sequence number, [31:16] Reserved
uint32_t deviceModeArg; //!< [0x018-0x01b] Argument/Parameter for device configuration
uint8_t configCmdEnable; //!< [0x01c-0x01c] Configure command Enable Flag, 1 - Enable, 0 - Disable
uint8_t configModeType[3]; //!< [0x01d-0x01f] Configure Mode Type, similar as deviceModeTpe
flexspi_lut_seq_t
uint32_t
configCmdSeqs[3]; //!< [0x020-0x02b] Sequence info for Device Configuration command, similar as deviceModeSeq
uint32_t reserved1; //!< [0x02c-0x02f] Reserved for future use
uint32_t configCmdArgs[3]; //!< [0x030-0x03b] Arguments/Parameters for device Configuration commands
Expand Down Expand Up @@ -64,7 +71,7 @@ typedef struct _FlexSPIConfig
uint16_t busyBitPolarity; //!< [0x07e-0x07f] Busy flag polarity, 0 - busy flag is 1 when flash device is busy, 1 -
//! busy flag is 0 when flash device is busy
uint32_t lookupTable[64]; //!< [0x080-0x17f] Lookup table holds Flash command sequences
flexspi_lut_seq_t lutCustomSeq[12]; //!< [0x180-0x1af] Customizable LUT Sequences
uint32_t lutCustomSeq[12]; //!< [0x180-0x1af] Customizable LUT Sequences
uint32_t reserved4[4]; //!< [0x1b0-0x1bf] Reserved for future use
} flexspi_mem_config_t;

Expand Down Expand Up @@ -120,3 +127,32 @@ typedef struct _flexspi_nor_config
[4 * 9 + 1] = FLEXSPI_LUT_SEQ(WRITE_SDR, FLEXSPI_1PAD, 0x04, STOP, FLEXSPI_1PAD, 0x0),\
[4 * 11 + 0] = FLEXSPI_LUT_SEQ(CMD_SDR, FLEXSPI_1PAD, 0x60, STOP, FLEXSPI_1PAD, 0x0),\
}

typedef struct {
uint32_t version;
int (*init)(uint32_t instance, flexspi_nor_config_t *config);
int (*program)(uint32_t instance, flexspi_nor_config_t *config,
uint32_t dst_addr, const uint32_t *src);
uint32_t reserved;
int (*erase)(uint32_t instance, flexspi_nor_config_t *config,
uint32_t start, uint32_t lengthInBytes);
uint32_t reserved2;
int (*update_lut)(uint32_t instance, uint32_t seqIndex,
const uint32_t *lutBase, uint32_t seqNumber);
int (*xfer)(uint32_t instance, char *xfer);
void (*clear_cache)(uint32_t instance);
} flexspi_nor_driver_interface_t;

typedef struct {
const uint32_t version; // Bootloader version number
const char *copyright; // Bootloader Copyright
void (*runBootloader)(
void *arg); // Function to start the bootloader executing
const uint32_t *reserved0; // Reserved
const flexspi_nor_driver_interface_t
*flexSpiNorDriver; // FlexSPI NOR Flash API
const uint32_t *reserved1[2]; // Reserved
} bootloader_api_entry_t;

#define bootloader (*(bootloader_api_entry_t **) (0x0020001c))
#define flexspi_nor bootloader->flexSpiNorDriver
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
// See https://mongoose.ws/documentation/#build-options
#define MG_ARCH MG_ARCH_NEWLIB

#ifdef RUNINFLASH
#define MG_OTA MG_OTA_FLASH
#else
#define MG_OTA MG_OTA_FLASH_FROM_RAM
#endif

#define MG_DEVICE MG_DEVICE_RT1020

#define MG_ENABLE_TCPIP 1
#define MG_ENABLE_DRIVER_IMXRT 1
#define MG_IO_SIZE 256
Expand Down
45 changes: 35 additions & 10 deletions examples/nxp/rt1060-evk-make-baremetal-builtin/flexspi.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,6 @@
#include <stdint.h>
#include <stdbool.h>

typedef struct _lut_sequence
{
uint8_t seqNum; //!< Sequence Number, valid number: 1-16
uint8_t seqId; //!< Sequence Index, valid number: 0-15
uint16_t reserved;
} flexspi_lut_seq_t;

typedef struct _FlexSPIConfig
{
uint32_t tag; //!< [0x000-0x003] Tag, fixed value 0x42464346UL
Expand All @@ -30,12 +23,12 @@ typedef struct _FlexSPIConfig
//! Generic configuration, etc.
uint16_t waitTimeCfgCommands; //!< [0x012-0x013] Wait time for all configuration commands, unit: 100us, Used for
//! DPI/QPI/OPI switch or reset command
flexspi_lut_seq_t deviceModeSeq; //!< [0x014-0x017] Device mode sequence info, [7:0] - LUT sequence id, [15:8] - LUt
uint32_t deviceModeSeq; //!< [0x014-0x017] Device mode sequence info, [7:0] - LUT sequence id, [15:8] - LUt
//! sequence number, [31:16] Reserved
uint32_t deviceModeArg; //!< [0x018-0x01b] Argument/Parameter for device configuration
uint8_t configCmdEnable; //!< [0x01c-0x01c] Configure command Enable Flag, 1 - Enable, 0 - Disable
uint8_t configModeType[3]; //!< [0x01d-0x01f] Configure Mode Type, similar as deviceModeTpe
flexspi_lut_seq_t
uint32_t
configCmdSeqs[3]; //!< [0x020-0x02b] Sequence info for Device Configuration command, similar as deviceModeSeq
uint32_t reserved1; //!< [0x02c-0x02f] Reserved for future use
uint32_t configCmdArgs[3]; //!< [0x030-0x03b] Arguments/Parameters for device Configuration commands
Expand Down Expand Up @@ -64,7 +57,7 @@ typedef struct _FlexSPIConfig
uint16_t busyBitPolarity; //!< [0x07e-0x07f] Busy flag polarity, 0 - busy flag is 1 when flash device is busy, 1 -
//! busy flag is 0 when flash device is busy
uint32_t lookupTable[64]; //!< [0x080-0x17f] Lookup table holds Flash command sequences
flexspi_lut_seq_t lutCustomSeq[12]; //!< [0x180-0x1af] Customizable LUT Sequences
uint32_t lutCustomSeq[12]; //!< [0x180-0x1af] Customizable LUT Sequences
uint32_t reserved4[4]; //!< [0x1b0-0x1bf] Reserved for future use
} flexspi_mem_config_t;

Expand Down Expand Up @@ -120,3 +113,35 @@ typedef struct _flexspi_nor_config
[4 * 9 + 1] = FLEXSPI_LUT_SEQ(WRITE_SDR, FLEXSPI_1PAD, 0x04, STOP, FLEXSPI_1PAD, 0x0),\
[4 * 11 + 0] = FLEXSPI_LUT_SEQ(CMD_SDR, FLEXSPI_1PAD, 0x60, STOP, FLEXSPI_1PAD, 0x0),\
}

typedef struct {
uint32_t version;
int (*init)(uint32_t instance, flexspi_nor_config_t *config);
int (*program)(uint32_t instance, flexspi_nor_config_t *config,
uint32_t dst_addr, const uint32_t *src);
int (*erase_all)(uint32_t instance, flexspi_nor_config_t *config);
int (*erase)(uint32_t instance, flexspi_nor_config_t *config,
uint32_t start, uint32_t lengthInBytes);
int (*read)(uint32_t instance, flexspi_nor_config_t *config,
uint32_t *dst, uint32_t addr, uint32_t lengthInBytes);
void (*clear_cache)(uint32_t instance);
int (*xfer)(uint32_t instance, char *xfer);
int (*update_lut)(uint32_t instance, uint32_t seqIndex,
const uint32_t *lutBase, uint32_t seqNumber);
int (*get_config)(uint32_t instance, flexspi_nor_config_t *config,
uint32_t *option);
} flexspi_nor_driver_interface_t;

typedef struct {
const uint32_t version; // Bootloader version number
const char *copyright; // Bootloader Copyright
void (*runBootloader)(
void *arg); // Function to start the bootloader executing
const uint32_t *reserved0; // Reserved
const flexspi_nor_driver_interface_t
*flexSpiNorDriver; // FlexSPI NOR Flash API
const uint32_t *reserved1[2]; // Reserved
} bootloader_api_entry_t;

#define bootloader (*(bootloader_api_entry_t **) (0x0020001c))
#define flexspi_nor bootloader->flexSpiNorDriver
32 changes: 30 additions & 2 deletions examples/nxp/rt1060-evk-make-baremetal-builtin/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ void mg_random(void *buf, size_t len) { // Use on-board RNG
memcpy((char *) buf + n, &r, n + sizeof(r) > len ? len - n : sizeof(r));
}
}

static void timer_fn(void *arg) {
gpio_toggle(LED); // Blink LED
struct mg_tcpip_if *ifp = arg; // And show
Expand All @@ -32,15 +33,42 @@ static void timer_fn(void *arg) {
}

int main(void) {
gpio_output(LED); // Setup blue LED
gpio_output(LED); // Setup green LED
uart_init(UART_DEBUG, 115200); // Initialise debug printf
ethernet_init(); // Initialise ethernet pins

#ifdef MQTT_DASHBOARD
// User can customise the MQTT url, device ID or the root topic below
#define DEVICE_ID "RT1060"
g_url = MQTT_SERVER_URL;
g_device_id = DEVICE_ID;
g_root_topic = DEFAULT_ROOT_TOPIC;
#endif

MG_INFO(("Starting, CPU freq %g MHz", (double) SystemCoreClock / 1000000));

struct mg_mgr mgr; // Initialise
mg_mgr_init(&mgr); // Mongoose event manager
mg_log_set(MG_LL_DEBUG); // Set log level

//mg_ota_boot(); // Call bootloader: continue to load, or boot another FW

#if MG_OTA == MG_OTA_FLASH
// Demonstrate the use of mg_flash_{load/save} functions for keeping device
// configuration data on flash. Increment boot count on every boot.
struct deviceconfig {
uint32_t boot_count;
char some_other_data[40];
};
uint32_t key = 0x12345678; // A unique key, one per data type
struct deviceconfig dc = {}; // Initialise to some default values
mg_flash_load(NULL, key, &dc, sizeof(dc)); // Load from flash
dc.boot_count++; // Increment boot count
mg_flash_save(NULL, key, &dc, sizeof(dc)); // And save back
MG_INFO(("Boot count: %u", dc.boot_count));
#endif

// Initialise Mongoose network stack
// Initialise Mongoose network stack
struct mg_tcpip_driver_imxrt_data driver_data = {.mdc_cr = 24, .phy_addr = 2};
struct mg_tcpip_if mif = {.mac = GENERATE_LOCALLY_ADMINISTERED_MAC(),
Expand All @@ -65,6 +93,6 @@ int main(void) {
for (;;) {
mg_mgr_poll(&mgr, 0);
}

return 0;
}

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
// See https://mongoose.ws/documentation/#build-options
#define MG_ARCH MG_ARCH_NEWLIB

#ifdef RUNINFLASH
#define MG_OTA MG_OTA_FLASH
#else
#define MG_OTA MG_OTA_FLASH_FROM_RAM
#endif

#define MG_DEVICE MG_DEVICE_RT1060

#define MG_ENABLE_TCPIP 1
#define MG_ENABLE_DRIVER_IMXRT 1
#define MG_IO_SIZE 256
Expand Down
Loading

0 comments on commit 52ade05

Please sign in to comment.