修改了Web后台的部分界面,增加了HAmqtt中的总电量传感器,后台新增mqtt上报频率设置

This commit is contained in:
OOP
2025-03-03 21:49:41 +08:00
parent e1e00b60ce
commit 9f9d4c7a56
4468 changed files with 1473046 additions and 10728 deletions

View File

@@ -0,0 +1,80 @@
/**
******************************************************************************
* @file mico_config.h
* @author William Xu
* @version V1.0.0
* @date 05-May-2014
* @brief This file provide constant definition and type declaration for MICO
* running.
******************************************************************************
*
* The MIT License
* Copyright (c) 2016 MXCHIP Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
* IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
******************************************************************************
*/
#pragma once
#define APP_INFO "MiCO BASIC Demo"
#define FIRMWARE_REVISION "MICO_BASIC_1_0"
#define MANUFACTURER "MXCHIP Inc."
#define SERIAL_NUMBER "20140606"
#define PROTOCOL "com.mxchip.basic"
/************************************************************************
* Application thread stack size */
#define MICO_DEFAULT_APPLICATION_STACK_SIZE (2000)
/************************************************************************
* Enable wlan connection, start easylink configuration if no wlan settings are existed */
//#define MICO_WLAN_CONNECTION_ENABLE
#define MICO_WLAN_CONFIG_MODE CONFIG_MODE_EASYLINK_WITH_SOFTAP
#define EasyLink_TimeOut 60000 /**< EasyLink timeout 60 seconds. */
#define EasyLink_ConnectWlan_Timeout 20000 /**< Connect to wlan after configured by easylink.
Restart easylink after timeout: 20 seconds. */
/************************************************************************
* Device enter MFG mode if MICO settings are erased. */
//#define MFG_MODE_AUTO
/************************************************************************
* Command line interface */
#define MICO_CLI_ENABLE
/************************************************************************
* Start a system monitor daemon, application can register some monitor
* points, If one of these points is not executed in a predefined period,
* a watchdog reset will occur. */
#define MICO_SYSTEM_MONITOR_ENABLE
/************************************************************************
* Add service _easylink._tcp._local. for discovery */
#define MICO_SYSTEM_DISCOVERY_ENABLE
/************************************************************************
* MiCO TCP server used for configuration and ota. */
#define MICO_CONFIG_SERVER_ENABLE
#define MICO_CONFIG_SERVER_PORT 8000

View File

@@ -0,0 +1,694 @@
#include <stdio.h>
#include <string.h>
#include "mico.h"
#include "platform_config.h"
#include "platform_init.h"
#include "platform_peripheral.h"
#include "platform_toolchain.h"
#include "linker_symbols.h"
/*
* Test mode defines
*
* #define DEBUG_PRINT
* #define WIPE_SFLASH
* #define TEST_SFLASH_WRITE
* #define TEST_SFLASH_READ
* #define DEBUG_PRINT_READ_CONTENT
* #define DEBUG_PRINT_VERIFY_READ_CONTENT
*/
/******************************************************
* Macros
******************************************************/
//#define DEBUG_PRINT
#ifdef DEBUG_PRINT
#define flash_program_log(format, ...) custom_log("Flash", format, ##__VA_ARGS__)
#define DEBUG_PRINTF(x) printf x
#else
#define flash_program_log(format, ...)
#define DEBUG_PRINTF(x)
#endif /* ifdef DEBUG_PRINT */
/******************************************************
* Constants
******************************************************/
/*
* Commands to execute - bitwise OR together
* TCL script write_sflash.tcl must match these defines
*/
#define MFG_SPI_FLASH_COMMAND_NONE (0x00000000)
#define MFG_SPI_FLASH_COMMAND_INITIAL_VERIFY (0x00000001)
#define MFG_SPI_FLASH_COMMAND_ERASE_CHIP (0x00000002)
#define MFG_SPI_FLASH_COMMAND_WRITE (0x00000004)
#define MFG_SPI_FLASH_COMMAND_POST_WRITE_VERIFY (0x00000008)
#define MFG_SPI_FLASH_COMMAND_VERIFY_CHIP_ERASURE (0x00000010)
#define MFG_SPI_FLASH_COMMAND_READ (0x00000040)
#define MFG_SPI_FLASH_COMMAND_WRITE_ERASE_IF_NEEDED (0x00000080)
#define MFG_SPI_FLASH_COMMAND_WRITE_DONE (0x00000100)
#define WRITE_CHUNK_SIZE (8*1024) /* Writing in chunks is only needed to prevent reset by watchdog */
#define SECTOR_SIZE (4096)
int partition_remapping[] =
{
[BOOTLOADER_FIRMWARE_PARTITION_TCL] = MICO_PARTITION_BOOTLOADER,
[APPLICATION_FIRMWARE_PARTITION_TCL] = MICO_PARTITION_APPLICATION,
[ATE_FIRMWARE_PARTITION_TCL] = MICO_PARTITION_ATE,
[WIFI_FIRMWARE_PARTITION_TCL] = MICO_PARTITION_RF_FIRMWARE,
[PARAMETER_1_IMAGE_PARTITION_TCL] = MICO_PARTITION_PARAMETER_1,
[PARAMETER_2_IMAGE_PARTITION_TCL] = MICO_PARTITION_PARAMETER_2,
#ifdef MICO_USE_BT_PARTITION
[BT_PATCH_FIRMWARE_PARTITION_TCL] = MICO_PARTITION_BT_FIRMWARE,
#else
[BT_PATCH_FIRMWARE_PARTITION_TCL] = -1,
#endif
[FILESYSTEM_IMAGE_PARTITION_TCL] = MICO_PARTITION_FILESYS,
};
/******************************************************
* Enumerations
******************************************************/
/*
* Result codes
* TCL script write_sflash.tcl must match this enum
*/
typedef enum
{
MFG_SPI_FLASH_RESULT_IN_PROGRESS = 0xffffffff,
MFG_SPI_FLASH_RESULT_OK = 0,
MFG_SPI_FLASH_RESULT_ERASE_FAILED = 1,
MFG_SPI_FLASH_RESULT_VERIFY_AFTER_WRITE_FAILED = 2,
MFG_SPI_FLASH_RESULT_SIZE_TOO_BIG_BUFFER = 3,
MFG_SPI_FLASH_RESULT_SIZE_TOO_BIG_CHIP = 4,
MFG_SPI_FLASH_RESULT_DCT_LOC_NOT_FOUND = 5,
MFG_SPI_FLASH_RESULT_WRITE_FAILED = 6,
MFG_SPI_FLASH_RESULT_READ_FAILED = 7,
MFG_SPI_FLASH_RESULT_END = 0x7fffffff /* force to 32 bits */
} mfg_spi_flash_result_t;
//extern const platform_qspi_t platform_qspi_peripherals[];
//extern int init_qsflash( /*@out@*/ const platform_qspi_t* peripheral, uint32_t flash_length, /*@out@*/ sflash_handle_t* const handle, sflash_write_allowed_t write_allowed_in );
//extern int qsflash_write( const sflash_handle_t* const handle, unsigned long device_address, const void* const data_addr, unsigned int size );
//extern int qsflash_read( const sflash_handle_t* const handle, unsigned long device_address, void* const data_addr, unsigned int size );
//extern int qsflash_sector_erase ( unsigned long device_address );
extern const mico_logic_partition_t mico_partitions[];
extern const platform_flash_t platform_flash_peripherals[];
/******************************************************
* Type Definitions
******************************************************/
/******************************************************
* Structures
******************************************************/
/*
* TCL script write_sflash.tcl must match this structure
*/
typedef struct
{
void * entry_point;
void * stack_addr;
unsigned long data_buffer_size;
} data_config_area_t;
/*
* TCL script write_sflash.tcl must match this structure
*/
typedef struct
{
unsigned long size;
unsigned long partition;
unsigned long partition_offset;
unsigned long command;
mfg_spi_flash_result_t result;
unsigned char data[__JTAG_FLASH_WRITER_DATA_BUFFER_SIZE__ ];
} data_transfer_area_t;
/******************************************************
* Static Function Declarations
******************************************************/
#ifdef WIPE_SFLASH
static void add_wipe_data( void );
#elif defined( TEST_SFLASH_WRITE )
static void add_test_data( void );
#elif defined( TEST_SFLASH_READ )
static void read_test_data2( void );
#endif /* ifdef TEST_SFLASH_READ */
/******************************************************
* Variable Definitions
******************************************************/
static uint8_t Rx_Buffer[SECTOR_SIZE + 10]; /* A temporary buffer used for reading data from the Serial flash when performing verification */
/******************************************************************************
* This structure provides configuration parameters, and communication area
* to the TCL OpenOCD script
* It will be located at the start of RAM by the linker script
*****************************************************************************/
#if defined(__ICCARM__)
/* IAR specific */
#pragma section= "data_config_section"
const data_config_area_t data_config @ "data_config_section";
#endif /* #if defined(__ICCARM__) */
const data_config_area_t data_config =
{
.entry_point = (void*) ENTRY_ADDRESS,
.stack_addr = &link_stack_end,
.data_buffer_size = __JTAG_FLASH_WRITER_DATA_BUFFER_SIZE__,
};
/******************************************************************************
* This structure provides a transfer area for communications with the
* TCL OpenOCD script
* It will be located immediately after the data_config structure at the
* start of RAM by the linker script
*****************************************************************************/
#if defined (__ICCARM__)
/* IAR specific */
#pragma section= "data_transfer_section"
data_transfer_area_t data_transfer @ "data_transfer_section";
#else /* #if defined (__ICCARM__) */
data_transfer_area_t data_transfer;
#endif /* #if defined (__ICCARM__) */
/******************************************************
* Function Definitions
******************************************************/
int not_erased (const void *a1, size_t size)
{
uint8_t* cmp_ptr = (uint8_t *)a1;
uint8_t* end_ptr = (uint8_t *)a1 + size;
while ( ( cmp_ptr < end_ptr ) && ( *cmp_ptr == (uint8_t) 0xff ) )
{
cmp_ptr++;
}
/* No not erase if already all 0xFF*/
if ( cmp_ptr < end_ptr )
return (int)(cmp_ptr - (uint8_t *)a1 + 1);
else
return 0;
}
int main( void )
{
unsigned long pos;
// sflash_handle_t sflash_handle;
unsigned long chip_size = 0;
bool erase_once = true;
#if defined ( __IAR_SYSTEMS_ICC__ )
/* IAR allows init functions in __low_level_init(), but it is run before global
* variables have been initialised, so the following init still needs to be done
* When using GCC, this is done in crt0_GCC.c
*/
platform_init_mcu_infrastructure( );
platform_init_external_devices( );
#endif /* #elif defined ( __IAR_SYSTEMS_ICC__ ) */
//NoOS_setup_timing( );
#ifdef WIPE_SFLASH
add_wipe_data( );
#elif defined( TEST_SFLASH_WRITE )
add_test_data( );
#elif defined( TEST_SFLASH_READ )
read_test_data2( );
#endif /* ifdef TEST_SFLASH_READ */
#ifdef PLATFORM_HAS_OTP
platform_otp_setup( );
#endif /* ifdef PLATFORM_HAS_OTP */
/* loop forever */
while ( 1 == 1 )
{
flash_program_log( "Waiting for command" );
/* wait for a command to be written. */
/*@-infloopsuncon@*//* Lint: Loop variable is modified by JTAG writing directly into the data transfer memory */
while ( data_transfer.command == MFG_SPI_FLASH_COMMAND_NONE )
{
(void) platform_watchdog_kick( );
}
/*@-infloopsuncon@*/
data_transfer.result = MFG_SPI_FLASH_RESULT_IN_PROGRESS;
flash_program_log( "Received command: %s%s%s%s%s%s%s",
( data_transfer.command & MFG_SPI_FLASH_COMMAND_INITIAL_VERIFY )? "INITIAL_VERIFY " : "",
( data_transfer.command & MFG_SPI_FLASH_COMMAND_ERASE_CHIP )? "ERASE_CHIP " : "",
( data_transfer.command & MFG_SPI_FLASH_COMMAND_WRITE )? "WRITE " : "",
( data_transfer.command & MFG_SPI_FLASH_COMMAND_POST_WRITE_VERIFY )? "POST_WRITE_VERIFY " : "",
( data_transfer.command & MFG_SPI_FLASH_COMMAND_VERIFY_CHIP_ERASURE )? "VERIFY_CHIP_ERASURE " : "",
( data_transfer.command & MFG_SPI_FLASH_COMMAND_READ )? "READ " : "",
( data_transfer.command & MFG_SPI_FLASH_COMMAND_WRITE_ERASE_IF_NEEDED)? "WRTIE_ERASE_IF_NEEDED " : "",
( data_transfer.command & MFG_SPI_FLASH_COMMAND_WRITE_DONE)? "MFG_SPI_FLASH_COMMAND_WRITE_DONE " : ""
);
flash_program_log( "Destination partition: %u, Destination address: %lx", partition_remapping[data_transfer.partition], data_transfer.partition_offset );
flash_program_log( "Size: %lu", data_transfer.size );
/* Check the data size is sane - cannot be bigger than the data storage area */
if ( data_transfer.size > (unsigned long) __JTAG_FLASH_WRITER_DATA_BUFFER_SIZE__ )
{
data_transfer.result = MFG_SPI_FLASH_RESULT_SIZE_TOO_BIG_BUFFER;
flash_program_log( "Size %lu too big to for storage area %d - aborting!", data_transfer.size, __JTAG_FLASH_WRITER_DATA_BUFFER_SIZE__);
goto back_to_idle;
}
if ( (data_transfer.command & MFG_SPI_FLASH_COMMAND_READ) != 0 )
{
flash_program_log( "Reading data!" );
/* Read data from SPI FLASH memory */
pos = 0;
unsigned long position = 0;
position = pos + data_transfer.partition_offset;
if ( 0
!= MicoFlashRead( partition_remapping[data_transfer.partition], &position, data_transfer.data,
(unsigned int) data_transfer.size ) )
{
/* Read failed */
data_transfer.result = MFG_SPI_FLASH_RESULT_READ_FAILED;
flash_program_log( "Read error - abort!" );
goto back_to_idle;
}
#ifdef DEBUG_PRINT_READ_CONTENT
{
int i;
for( i = 0;i < data_transfer.size; i++ )
{
DEBUG_PRINTF( ( "%02X ", data_transfer.data[i]));
if( ( i & 0xf ) == 0xf )
{
DEBUG_PRINTF( ( "\r\n") );
}
}
}
#endif /* DEBUG_PRINT_READ_CONTENT */
data_transfer.result = MFG_SPI_FLASH_RESULT_OK;
flash_program_log( "Finished Read!" );
goto back_to_idle;
}
/* Check the data will fit on the sflash chip */
if ( (chip_size != 0) && (data_transfer.size + data_transfer.partition_offset > chip_size) )
{
data_transfer.result = MFG_SPI_FLASH_RESULT_SIZE_TOO_BIG_CHIP;
flash_program_log( "Size (%lu from address %lu) too big to fit on partition (%lu) - aborting!", data_transfer.size, data_transfer.partition_offset, chip_size );
goto back_to_idle;
}
if ( (data_transfer.command & MFG_SPI_FLASH_COMMAND_INITIAL_VERIFY) != 0 )
{
flash_program_log( "Verifying existing data!" );
/* Read data from SPI FLASH memory */
pos = 0;
while ( pos < data_transfer.size )
{
unsigned int read_size =
(data_transfer.size - pos > (unsigned long) sizeof(Rx_Buffer)) ?
(unsigned int) sizeof(Rx_Buffer) :
(unsigned int) (data_transfer.size - pos);
unsigned long position = 0;
position = pos + data_transfer.partition_offset;
if ( 0
!= MicoFlashRead( partition_remapping[data_transfer.partition], &position, Rx_Buffer,
read_size ) )
{
/* Verify Error - Chip not erased properly */
data_transfer.result = MFG_SPI_FLASH_RESULT_READ_FAILED;
flash_program_log( "Read error - abort!" );
goto back_to_idle;
}
if ( 0 != memcmp( Rx_Buffer, &data_transfer.data[pos], (size_t) read_size ) )
{
/* Existing data different */
flash_program_log( "Existing data is different - stop verification" );
/*@innerbreak@*//* Only break out of inner-most loop */
break;
}
pos += read_size;
(void) platform_watchdog_kick( );
}
if ( pos >= data_transfer.size )
{
/* Existing data matches */
/* No write required */
data_transfer.result = MFG_SPI_FLASH_RESULT_OK;
flash_program_log( "Existing data matches - successfully aborting!" );
goto back_to_idle;
}
}
if ( (data_transfer.command & MFG_SPI_FLASH_COMMAND_ERASE_CHIP) != 0 )
{
flash_program_log( "Erasing entire chip" );
/* Erase the serial flash chip */
/*delete for test*/
if ( (data_transfer.command & MFG_SPI_FLASH_COMMAND_VERIFY_CHIP_ERASURE) != 0 )
{
flash_program_log( "Verifying erasure of entire chip" );
/* Verify Erasure */
pos = 0;
while ( pos < chip_size )
{
uint8_t* cmp_ptr;
uint8_t* end_ptr;
unsigned int read_size;
read_size =
(chip_size - pos > (unsigned long) sizeof(Rx_Buffer)) ?
(unsigned int) sizeof(Rx_Buffer) : (unsigned int) (chip_size - pos);
if ( 0
!= MicoFlashRead( partition_remapping[data_transfer.partition], &pos, Rx_Buffer, read_size ) )
{
/* Verify Error - Chip not erased properly */
data_transfer.result = MFG_SPI_FLASH_RESULT_READ_FAILED;
flash_program_log( "Read error - abort!" );
goto back_to_idle;
}
cmp_ptr = Rx_Buffer;
end_ptr = &Rx_Buffer[read_size];
while ( (cmp_ptr < end_ptr) && (*cmp_ptr == (uint8_t) 0xff) )
{
cmp_ptr++;
}
if ( cmp_ptr < end_ptr )
{
/* Verify Error - Chip not erased properly */
data_transfer.result = MFG_SPI_FLASH_RESULT_ERASE_FAILED;
flash_program_log( "Chip was not erased properly - abort!" );
goto back_to_idle;
}
pos += read_size;
(void) platform_watchdog_kick( );
}
}
}
if ( (data_transfer.command & MFG_SPI_FLASH_COMMAND_WRITE) != 0 )
{
flash_program_log( "Writing location" );
/* Write the WLAN firmware into memory */
pos = 0;
while ( pos < data_transfer.size )
{
unsigned int write_size =
(data_transfer.size - pos > (unsigned long) WRITE_CHUNK_SIZE) ?
(unsigned int) WRITE_CHUNK_SIZE : (unsigned int) (data_transfer.size - pos);
unsigned long position = 0;
position = pos + data_transfer.partition_offset;
if ( 0
!= MicoFlashWrite( partition_remapping[data_transfer.partition], &position,
&data_transfer.data[pos], write_size ) )
{
/* Verify Error - Chip not erased properly */
data_transfer.result = MFG_SPI_FLASH_RESULT_WRITE_FAILED;
flash_program_log( "Write error - abort!" );
goto back_to_idle;
}
pos += write_size;
(void) platform_watchdog_kick( );
}
}
if ( (data_transfer.command & MFG_SPI_FLASH_COMMAND_WRITE_ERASE_IF_NEEDED) != 0 )
{
mico_logic_partition_t *partition_info;
uint32_t compare_start = (uint32_t) data_transfer.partition_offset % SECTOR_SIZE;
int32_t size_left_to_compare = (int32_t) data_transfer.size;
unsigned char* cmp_ptr = data_transfer.data;
pos = (data_transfer.partition_offset / SECTOR_SIZE) * SECTOR_SIZE;
/* Force erase embedded flash once before program */
partition_info = MicoFlashGetInfo( partition_remapping[data_transfer.partition] );
if( platform_flash_peripherals[partition_info->partition_owner].flash_type == FLASH_TYPE_EMBEDDED && erase_once )
{
flash_program_log( "Partition erase!" );
MicoFlashErase( partition_remapping[data_transfer.partition], 0x0, partition_info->partition_length);
erase_once = false;
}
flash_program_log( "Verifying existing data!" );
/* Read data from SPI FLASH memory */
while ( size_left_to_compare > 0 )
{
uint32_t cmp_len;
unsigned long position;
position = pos;
if ( 0
!= MicoFlashRead( partition_remapping[data_transfer.partition], &position, Rx_Buffer,
(unsigned int) SECTOR_SIZE ) )
{
/* Verify Error - Chip not read properly */
data_transfer.result = MFG_SPI_FLASH_RESULT_READ_FAILED;
flash_program_log( "Read error - abort!" );
goto back_to_idle;
}
cmp_len = (uint32_t) MIN( (int32_t) ( SECTOR_SIZE - compare_start ), size_left_to_compare );
#ifndef ALWAYS_ERASE_SECTOR
if ( 0 != memcmp( &Rx_Buffer[compare_start], cmp_ptr, cmp_len ) )
#endif /* ALWAYS_ERASE_SECTOR */
{
flash_program_log("Need to erase if non 0xFF");
/* No not erase if already all 0xFF */
if ( not_erased( &Rx_Buffer[compare_start], cmp_len ) )
{
/* Existing data different */
uint32_t offset;
flash_program_log( "Erasing sector 0x%lx", pos );
offset = pos;
if ( 0 != MicoFlashErase( partition_remapping[data_transfer.partition], offset, SECTOR_SIZE ) )
{
/* Sector Erase Error - Chip not erased properly */
data_transfer.result = MFG_SPI_FLASH_RESULT_ERASE_FAILED;
flash_program_log( "Sector erase error - abort!" );
goto back_to_idle;
}
}
#ifdef VERIFY_ERASE_SECTOR
{
#ifdef DEBUG
memset(Rx_Buffer,0xa5,SECTOR_SIZE);
#endif /* DEBUG */
if ( 0 != MicoFlashRead( partition_remapping[data_transfer.partition], &position, Rx_Buffer,
(unsigned int) SECTOR_SIZE ) )
{
/* Verify Error - Chip not read properly */
data_transfer.result = MFG_SPI_FLASH_RESULT_READ_FAILED;
flash_program_log( "Read error - abort!" );
goto back_to_idle;
}
if ( not_erased( Rx_Buffer, SECTOR_SIZE ) )
{
/* Verify Error - sector not erased properly */
data_transfer.result = MFG_SPI_FLASH_RESULT_ERASE_FAILED;
flash_program_log( "Sector was not erased properly - abort!" );
flash_program_log( "First bystes read: 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X", Rx_Buffer[0], Rx_Buffer[1], Rx_Buffer[2], Rx_Buffer[3],
Rx_Buffer[4], Rx_Buffer[5], Rx_Buffer[6], Rx_Buffer[7] );
goto back_to_idle;
}
}
#endif /* ifdef VERIFY_ERASE_SECTOR */
flash_program_log( "Writing address 0x%lx", pos );
memcpy( &Rx_Buffer[compare_start], cmp_ptr, cmp_len );
unsigned long position;
position = pos;
flash_program_log("position is 0x%lx",position);
if ( 0
!= MicoFlashWrite( partition_remapping[data_transfer.partition], &position, Rx_Buffer,
(unsigned int) SECTOR_SIZE ) )
{
/* Write Error - Chip not written properly */
data_transfer.result = MFG_SPI_FLASH_RESULT_WRITE_FAILED;
flash_program_log( "Write error - abort!" );
goto back_to_idle;
}
flash_program_log("Write success");
}
cmp_ptr += cmp_len;
size_left_to_compare -= cmp_len;
compare_start = 0;
pos += SECTOR_SIZE;
(void) platform_watchdog_kick( );
}
}
if ( (data_transfer.command & MFG_SPI_FLASH_COMMAND_POST_WRITE_VERIFY) != 0 )
{
flash_program_log( "Verifying after write" );
/* Read data from SPI FLASH memory */
pos = 0;
while ( pos < data_transfer.size )
{
unsigned int read_size =
(data_transfer.size - pos > (unsigned long) sizeof(Rx_Buffer)) ?
(unsigned int) sizeof(Rx_Buffer) : data_transfer.size - pos;
unsigned long position;
position = pos + data_transfer.partition_offset;
if ( 0
!= MicoFlashRead( partition_remapping[data_transfer.partition], &position, Rx_Buffer,
read_size ) )
{
/* Verify Error - Chip not erased properly */
data_transfer.result = MFG_SPI_FLASH_RESULT_READ_FAILED;
flash_program_log( "Read error - abort!" );
goto back_to_idle;
}
#ifdef DEBUG_PRINT_VERIFY_READ_CONTENT
{
int i;
for( i = 0;i < read_size; i++ )
{
DEBUG_PRINTF(( "%02X %02X ", Rx_Buffer[i], data_transfer.data[i]));
if( ( i & 0xf ) == 0xf )
{
DEBUG_PRINTF( ( "\r\n") );
}
}
}
#endif /* DEBUG_PRINT_VERIFY_READ_CONTENT */
if ( 0 != memcmp( Rx_Buffer, &data_transfer.data[pos], (size_t) read_size ) )
{
/* Verify Error - Read data different to written data */
data_transfer.result = MFG_SPI_FLASH_RESULT_VERIFY_AFTER_WRITE_FAILED;
flash_program_log( "Verify error - Data was not written successfully - abort!Pos=%lx", pos );
goto back_to_idle;
}
pos += read_size;
(void) platform_watchdog_kick( );
}
}
if ( (data_transfer.command & MFG_SPI_FLASH_COMMAND_WRITE_DONE) != 0 )
{
erase_once = true;
flash_program_log( " Write operation finished at partition %d", APPLICATION_FIRMWARE_PARTITION_TCL );
}
/* OK! */
data_transfer.result = MFG_SPI_FLASH_RESULT_OK;
back_to_idle:
data_transfer.command = MFG_SPI_FLASH_COMMAND_NONE;
data_transfer.partition_offset = 0;
data_transfer.size = 0;
}
return 0;
}
#if defined(__ICCARM__)
#pragma section="CSTACK"
__root void _mico_iar_program_start(void)
{
/* When the execution of the program is initiated from an external debugger */
/* it will perform a reset of the CPU followed by halt and set a program counter to program entry function __iar_program_start leaving */
/* SP unchanged */
/* Consequently, the SP value will be set to the value which was read from address 0x00000000(applicable for CM3) */
/* For apps which have an interrupt vector table placed at the start of the flash, the value of the SP will */
/* contain correct value, however for apps which have interrupt vectors shifted to a different memory location, */
/* the SP will contain garbage. On entry we must call this function which will set the SP to point to the end */
/* of the CSTACK section */
iar_set_msp(__section_end("CSTACK"));
}
#endif
#ifdef WIPE_SFLASH
static void add_wipe_data( void )
{
data_transfer.command = MFG_SPI_FLASH_COMMAND_ERASE_CHIP;
data_transfer.sflash_address = 0;
data_transfer.size = 0;
data_transfer.result = MFG_SPI_FLASH_RESULT_IN_PROGRESS;
}
#elif defined( TEST_SFLASH_WRITE )
static void add_test_data( void )
{
const char test_data[] = /* 0 */
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
/* 1024 */
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
/* 2048 */
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
/* 3072 */
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
/* 4096 */
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
"0123456789abcdef0123456789abcdef012";
/* 4643 */
data_transfer.command = MFG_SPI_FLASH_COMMAND_WRITE_ERASE_IF_NEEDED | MFG_SPI_FLASH_COMMAND_POST_WRITE_VERIFY;
// data_transfer.command |= MFG_SPI_FLASH_COMMAND_ERASE_CHIP;
data_transfer.partition = APPLICATION_FIRMWARE_PARTITION_TCL;
data_transfer.partition_offset = 0x00;
data_transfer.size = sizeof(test_data)-1;
data_transfer.result = MFG_SPI_FLASH_RESULT_IN_PROGRESS;
memcpy( data_transfer.data, test_data, sizeof(test_data)-1);
}
#elif defined( TEST_SFLASH_READ )
static void read_test_data2( void )
{
data_transfer.command = MFG_SPI_FLASH_COMMAND_READ;
data_transfer.partition = APPLICATION_FIRMWARE_PARTITION_TCL;
data_transfer.partition_offset = 0x00;
data_transfer.size = 0x1000;
data_transfer.result = MFG_SPI_FLASH_RESULT_IN_PROGRESS;
}
#endif /* ifdef TEST_SFLASH_READ */

View File

@@ -0,0 +1,356 @@
#
# sflash_write.tcl
#
# This TCL OpenOCD script runs on a PC and communicates with the embedded
# sflash_write app to allow the PC to write data into a serial flash chip
# attached to the target processor
#
# Usage example:
#
# source [find mfg_spi_flash/write_sflash.tcl]
# sflash_init "MK3165@debug"
# sflash_write_file "example.bin" 0x10 "MK3165@debug" 1
# shutdown
#
###############################################################################
# CHIP_RAM_START must be supplied by target specific TCL script
set MemoryStart $CHIP_RAM_START
###############################################################################
#
# These variables must match the ones in Apps/waf/sflash_write/sflash_write.c
#
# They rely on the linker script placing the data_config_area_t and
# data_transfer_area_t structures at the start of RAM
#
###############################################################################
# This must match data_config_area_t
set entry_address_loc [expr $MemoryStart + 0x00 ]
set stack_address_loc [expr $MemoryStart + 0x04 ]
set buffer_size_loc [expr $MemoryStart + 0x08 ]
# This must match data_transfer_area_t
set data_size_loc [expr $MemoryStart + 0x0C ]
set dest_partition_loc [expr $MemoryStart + 0x10 ]
set dest_address_loc [expr $MemoryStart + 0x14 ]
set command_loc [expr $MemoryStart + 0x18 ]
set result_loc [expr $MemoryStart + 0x1C ]
set data_loc [expr $MemoryStart + 0x20 ]
# These must match the MFG_SPI_FLASH_COMMAND defines
set COMMAND_INITIAL_VERIFY (0x01)
set COMMAND_ERASE (0x02)
set COMMAND_WRITE (0x04)
set COMMAND_POST_WRITE_VERIFY (0x08)
set COMMAND_VERIFY_CHIP_ERASURE (0x10)
set COMMAND_WRITE_DCT (0x20)
set COMMAND_READ (0x40)
set COMMAND_WRITE_ERASE_IF_NEEDED (0x80)
set COMMAND_WRITE_DONE (0x100)
# These must match the mfg_spi_flash_result_t enum
set RESULT(0xffffffff) "In Progress"
set RESULT(4294967295) "In Progress"
set RESULT(0) "OK"
set RESULT(1) "Erase Failed"
set RESULT(2) "Verify after write failed"
set RESULT(3) "Size too big for buffer"
set RESULT(4) "Size too big for chip"
set RESULT(5) "DCT location not found - has factory reset app been written?"
set RESULT(6) "Error during Write"
set RESULT(7) "Error during Read"
###############################################################################
# memread32
#
# Helper function that reads a 32 bit value from RAM and returns it
#
# address - the RAM address to read from
###############################################################################
proc memread32 {address resume_required} {
if { $resume_required == 1 } {
halt
}
mem2array memar 32 $address 1
if { $resume_required == 1} {
resume
}
return $memar(0)
}
###############################################################################
# load_image_bin
#
# Loads part of a binary file into RAM
#
# fname - filename of binary image file
# foffset - offset from the start of the binary file where data will be read
# address - the destination RAM address
# length - number of bytes to transfer
###############################################################################
proc load_image_bin {fname foffset address length } {
# Load data from fname filename at foffset offset to
# target at address. Load at most length bytes.
puts "loadimage address $address foffset $foffset $length"
load_image $fname [expr $address - $foffset] bin $address $length
}
###############################################################################
# sflash_init
#
# Prepares for writing to serial flashby loading the sflash_write app into
# memory and setting it running.
# This function assumes the following target has already been built:
# waf.sflash_write-NoOS-<PlatBusDebug>
#
# PlatBusDebug - The platform, bus and debug part of the build target
# init4390 - run initialisation for the 4390
###############################################################################
proc sflash_init { PlatBusDebug } {
global entry_address_loc
global stack_address_loc
global buffer_size_loc
global entry_address
global stack_address
global buffer_size
init
reset init
reset halt
#load_image build/sub_build.spi_flash_write@NoRTOS@$PlatBusDebug/binary/sub_build.spi_flash_write@NoRTOS@$PlatBusDebug.elf
if {[file exists mico-os/board/$PlatBusDebug/flash_prog.elf]} {
load_image mico-os/board/$PlatBusDebug/flash_prog.elf
} else {
if {[file exists board/$PlatBusDebug/flash_prog.elf]} {
load_image board/$PlatBusDebug/flash_prog.elf
} else {
error "Error: Cann't find flash_prog.elf in $PlatBusDebug directory"
exit -1;
}
}
#mdw 0x000D0000 100
#mdw $entry_address_loc 100
set entry_address [memread32 $entry_address_loc 0]
set stack_address [memread32 $stack_address_loc 0]
set buffer_size [memread32 $buffer_size_loc 0]
puts "entry_address= $entry_address"
puts "stack_address= $stack_address"
puts "buffer_size= $buffer_size"
if { $buffer_size == 0 } {
error "Error: Buffer size read from address $buffer_size_loc on target is zero"
exit -1;
}
# Setup start address
reg pc $entry_address
reg sp $stack_address
resume
sleep 500
}
###############################################################################
# program_sflash
#
# Executes a serial command by communicating to the sflash_write app
#
# fname - filename of binary image file (if command requires it)
# foffset - offset from the start of the binary file where data will be read (if command requires it)
# dataSize - number of bytes to transfer (if command requires it)
# partition- the destination flash partition (if command requires it)
# destaddr - the destination offset address on a partition (if command requires it)
# cmd - The commmand to execute (see list above)
###############################################################################
proc program_sflash { filename foffset dataSize partition destaddr cmd } {
global PlatBusDebug MemoryStart data_size_loc dest_partition_loc data_loc dest_address_loc command_loc result_loc entry_address RESULT entry_address_loc
halt
# Load the binary data into the RAM
if { ( $dataSize != 0 ) && ( $filename != "" ) } {
load_image_bin $filename $foffset $data_loc $dataSize
}
# Write the details of the data
mww $data_size_loc $dataSize
mww $dest_partition_loc $partition
mww $dest_address_loc $destaddr
mww $result_loc 0xffffffff
puts "========================dest_address_loc= $destaddr"
#mdw $entry_address_loc 100
# Write the command - This causes the writing to start
mww $command_loc $cmd
resume
set resultval 0xffffffff
set loops 0
while { ($resultval == 0xffffffff) && ( $loops < 100 ) } {
sleep 100
set resultval [memread32 $result_loc 1]
incr loops
}
puts "****************** Result: $RESULT($resultval)"
if { $resultval != 0 } {
halt
reg
error "program_sflash error, Result: $RESULT($resultval)"
exit -1;
}
}
###############################################################################
# program_sflash_done
#
# Notice flash write is finished, perfor some post actions
#
###############################################################################
proc program_sflash_done { partition } {
global RESULT dest_partition_loc result_loc command_loc COMMAND_WRITE_DONE
halt
mww $dest_partition_loc $partition
mww $command_loc [expr $COMMAND_WRITE_DONE]
resume
set resultval 0xffffffff
set loops 0
while { ($resultval == 0xffffffff) && ( $loops < 100 ) } {
sleep 100
set resultval [memread32 $result_loc 1]
incr loops
}
puts "****************** Result: $RESULT($resultval)"
if { $resultval != 0 } {
halt
reg
error "program_sflash_done error, Result: $RESULT($resultval)"
exit -1;
}
}
###############################################################################
# sflash_write_file
#
# Writes an entire binary image file to a serial flash address
# This function assumes the following target has already been built:
# waf.sflash_write-NoOS-<PlatBusDebug>
#
# filename - filename of binary image file
# partition - the destination flash partition
# destAddress - the destination flash partition offset address
# PlatBusDebug - The platform, bus and debug part of the build target
# erasechip - If 1, Erase the partition before writing.
# init4390 - run initialisation for the 4390
###############################################################################
proc sflash_write_file { filename partition destAddress PlatBusDebug erasechip } {
global COMMAND_ERASE COMMAND_INITIAL_VERIFY COMMAND_WRITE COMMAND_POST_WRITE_VERIFY buffer_size COMMAND_WRITE_ERASE_IF_NEEDED
sflash_init $PlatBusDebug
set binDataSize [file size $filename]
# set erase_command_val [expr $COMMAND_ERASE ]
set write_command_val [expr $COMMAND_WRITE_ERASE_IF_NEEDED | $COMMAND_POST_WRITE_VERIFY ]
set pos 0
# if { $erasechip } {
# puts "Erasing partition $partition"
# program_sflash $filename $pos 0 $partition $destAddress $erase_command_val
# puts "Chip Erase Done"
# }
puts "Total write size is $binDataSize to partition $partition"
while { $pos < $binDataSize } {
if { ($binDataSize - $pos) < $buffer_size } {
set writesize [expr ($binDataSize - $pos)]
} else {
set writesize $buffer_size
}
puts "writing $writesize bytes at [expr $destAddress + $pos]"
program_sflash $filename $pos $writesize $partition [expr $destAddress + $pos] $write_command_val
set pos [expr $pos + $writesize]
}
program_sflash_done $partition
}
###############################################################################
# sflash_read_file
#
# Reads data from a serial flash address
# This function assumes the following target has already been built:
# waf.sflash_write-NoOS-<PlatBusDebug>
#
# filename - output filename for binary image file
# srcAddress - the destination serial flash address
# PlatBusDebug - The platform, bus and debug part of the build target
# length - number of bytes to read
# init4390 - run initialisation for the 4390
###############################################################################
proc sflash_read_file { filename srcAddress PlatBusDebug length } {
global COMMAND_ERASE COMMAND_INITIAL_VERIFY COMMAND_WRITE COMMAND_POST_WRITE_VERIFY buffer_size COMMAND_READ data_loc
sflash_init $PlatBusDebug
set temp_file "temp.bin"
exec tools/common/Win32/echo -n > $filename
set read_command_val [expr $COMMAND_READ ]
set pos 0
puts "Total read size is $length"
while { $pos < $length } {
if { ($length - $pos) < $buffer_size } {
set readsize [expr ($length - $pos)]
} else {
set readsize $buffer_size
}
puts "reading $readsize bytes from [expr $srcAddress + $pos]"
program_sflash "" $pos $readsize [expr $srcAddress + $pos] $read_command_val
# mem2array memar 8 [expr $data_loc-8] 1024
# puts "$memar(0) $memar(1) $memar(2) $memar(3) $memar(4) $memar(5) $memar(6) $memar(7) $memar(8)"
puts "dumping image from $data_loc $readsize"
halt
dump_image $temp_file $data_loc $readsize
exec cat $temp_file >> $filename
set pos [expr $pos + $readsize]
}
}
proc sflash_erase { PlatBusDebug } {
global COMMAND_ERASE COMMAND_INITIAL_VERIFY COMMAND_WRITE COMMAND_POST_WRITE_VERIFY buffer_size COMMAND_WRITE_ERASE_IF_NEEDED
sflash_init $PlatBusDebug
set erase_command_val [expr $COMMAND_ERASE ]
puts "Erasing Chip"
program_sflash "" 0 0 0 $erase_command_val
puts "Chip Erase Done"
}

View File

@@ -0,0 +1,48 @@
#
# UNPUBLISHED PROPRIETARY SOURCE CODE
# Copyright (c) 2016 MXCHIP Inc.
#
# The contents of this file may not be disclosed to third parties, copied or
# duplicated in any form, in whole or in part, without the prior written
# permission of MXCHIP Corporation.
#
NAME := App_SFlash_write
include $(MAKEFILES_PATH)/mico_partition.mk
$(NAME)_SOURCES := sflash_write.c
ifeq ($(TOOLCHAIN_NAME),IAR)
NoRTOS_START_STACK := 10000
else
NoRTOS_START_STACK := 5024
endif
# This uses cflags instead of the normal includes to avoid being
# relative to the directory of this module
#$(NAME)_CFLAGS += -I$(SPI_FLASH_IMAGE_DIR)
#$(NAME)_DEFINES += FACTORY_RESET_AFTER_SFLASH
# blocking printf so breakpoint calls still print information
#GLOBAL_DEFINES += PRINTF_BLOCKING
#NoOS_START_STACK := 6000
#GLOBAL_LINK_SCRIPT := mfg_spi_flash_link.ld
NO_WIFI_FIRMWARE := YES
NO_WIFI := YES
GLOBAL_DEFINES += MICO_NO_WIFI
GLOBAL_DEFINES += NO_WIFI_FIRMWARE
GLOBAL_DEFINES += FIRMWARE_DOWNLOAD
ifeq ($(WIPE),1)
GLOBAL_DEFINES += WIPE_SFLASH
endif
GLOBAL_LDFLAGS += $$(CLIB_LDFLAGS_NANO)
VALID_OSNS_COMBOS := NoRTOS