Skip to content
Permalink
2952b665ce
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
268 lines (245 sloc) 10.2 KB
/******************************************************************************
* File Name: main.c
*
* Description: This is the source code for the XMC MCU: Flash Program Example
* for ModusToolbox. This example shows how to erase a sector of
* the flash, write data to the sector and check the data for
* correctness. Success is indicated by toggling a LED.
*
* Related Document: See README.md
*
******************************************************************************
*
* Copyright (c) 2015-2022, Infineon Technologies AG
* All rights reserved.
*
* Boost Software License - Version 1.0 - August 17th, 2003
*
* Permission is hereby granted, free of charge, to any person or organization
* obtaining a copy of the software and accompanying documentation covered by
* this license (the "Software") to use, reproduce, display, distribute,
* execute, and transmit the Software, and to prepare derivative works of the
* Software, and to permit third-parties to whom the Software is furnished to
* do so, all subject to the following:
*
* The copyright notices in the Software and this entire statement, including
* the above license grant, this restriction and the following disclaimer,
* must be included in all copies of the Software, in whole or in part, and
* all derivative works of the Software, unless such copies or derivative
* works are solely in the form of machine-executable object code generated by
* a source language processor.
*
* 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, TITLE AND NON-INFRINGEMENT. IN NO EVENT
* SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
* FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*****************************************************************************/
/******************************************************************************
* File Name: main.c
*
* Description: This is the source code for the XMC MCU: Flash Program Example
* for ModusToolbox. This example shows how to erase a sector of
* the flash, write data to the sector and check the data for
* correctness. Success is indicated by toggling a LED.
*
* Related Document: See README.md
*
*
*******************************************************************************
* Copyright 2022, Cypress Semiconductor Corporation (an Infineon company) or
* an affiliate of Cypress Semiconductor Corporation. All rights reserved.
*
* This software, including source code, documentation and related
* materials ("Software") is owned by Cypress Semiconductor Corporation
* or one of its affiliates ("Cypress") and is protected by and subject to
* worldwide patent protection (United States and foreign),
* United States copyright laws and international treaty provisions.
* Therefore, you may use this Software only as provided in the license
* agreement accompanying the software package from which you
* obtained this Software ("EULA").
* If no EULA applies, Cypress hereby grants you a personal, non-exclusive,
* non-transferable license to copy, modify, and compile the Software
* source code solely for use in connection with Cypress's
* integrated circuit products. Any reproduction, modification, translation,
* compilation, or representation of this Software except as specified
* above is prohibited without the express written permission of Cypress.
*
* Disclaimer: THIS SOFTWARE IS PROVIDED AS-IS, WITH NO WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, NONINFRINGEMENT, IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. Cypress
* reserves the right to make changes to the Software without notice. Cypress
* does not assume any liability arising out of the application or use of the
* Software or any product or circuit described in the Software. Cypress does
* not authorize its products for use in any products where a malfunction or
* failure of the Cypress product may reasonably be expected to result in
* significant property damage, injury or death ("High Risk Product"). By
* including Cypress's product in a High Risk Product, the manufacturer
* of such system or application assumes all risk of such use and in doing
* so agrees to indemnify Cypress against all liability.
*******************************************************************************/
#include "cybsp.h"
#include "cy_utils.h"
#include "xmc_flash.h"
#include "cy_retarget_io.h"
#include <stdio.h>
/*******************************************************************************
* Defines
*******************************************************************************/
/* Declarations for LED toggle timing */
#define TICKS_PER_SECOND 1000
#define TICKS_WAIT 500
/* Declaration of the start address for different kits to demonstrate flash programming */
#if (UC_FAMILY == XMC1)
#if (UC_SERIES == XMC11)
#define XMC_SECTOR_ADDR (uint32_t *)0x10010000U
#else
#define XMC_SECTOR_ADDR (uint32_t *)0x10032000U
#endif
#endif
#if (UC_FAMILY == XMC4)
#define XMC_SECTOR_ADDR XMC_FLASH_SECTOR_7
#endif
/* Define macro to enable/disable printing of debug messages */
#define ENABLE_XMC_DEBUG_PRINT (0)
/*******************************************************************************
* Variables
*******************************************************************************/
/* Test data used to demonstrate programming of the flash */
uint32_t data[XMC_FLASH_WORDS_PER_PAGE] =
{
0x11111111U, 0x22222222U, 0x33333333U, 0x44444444U,
0x55555555U, 0x66666666U, 0x77777777U, 0x88888888U,
0x99999999U, 0xaaaaaaaaU, 0xbbbbbbbbU, 0xccccccccU,
0xddddddddU, 0xeeeeeeeeU, 0xffffffffU, 0000000000U,
0x11111111U, 0x22222222U, 0x33333333U, 0x44444444U,
0x55555555U, 0x66666666U, 0x77777777U, 0x88888888U,
0x99999999U, 0xaaaaaaaaU, 0xbbbbbbbbU, 0xccccccccU,
0xddddddddU, 0xeeeeeeeeU, 0xffffffffU, 0000000000U,
0x11111111U, 0x22222222U, 0x33333333U, 0x44444444U,
0x55555555U, 0x66666666U, 0x77777777U, 0x88888888U,
0x99999999U, 0xaaaaaaaaU, 0xbbbbbbbbU, 0xccccccccU,
0xddddddddU, 0xeeeeeeeeU, 0xffffffffU, 0000000000U,
0x11111111U, 0x22222222U, 0x33333333U, 0x44444444U,
0x55555555U, 0x66666666U, 0x77777777U, 0x88888888U,
0x99999999U, 0xaaaaaaaaU, 0xbbbbbbbbU, 0xccccccccU,
0xddddddddU, 0xeeeeeeeeU, 0xffffffffU, 0000000000U
};
/*******************************************************************************
* Function Name: SysTick_Handler
********************************************************************************
* Summary:
* Function called by system timer every millisecond.
*
* Parameters:
* void
*
* Return:
* void
*
*******************************************************************************/
void SysTick_Handler(void)
{
static uint32_t ticks = 0;
ticks++;
if (ticks == TICKS_WAIT)
{
/* Toggle LED */
XMC_GPIO_ToggleOutput(CYBSP_USER_LED_PORT, CYBSP_USER_LED_PIN);
ticks = 0;
}
}
/*******************************************************************************
* Function Name: main
********************************************************************************
* Summary:
* This is the main function.
* A LED will be turned on before the start of programming the flash.
* Programming is executed in three steps:
* 1. Erase a sector inside flash.
* 2. Program the first page (256 bytes) of the sector with the data
* and turn off the LED.
* 3. Compare the data to confirm its correctness. If correct, toggle the LED.
*
* Parameters:
* none
*
* Return:
* int
*
*******************************************************************************/
int main(void)
{
cy_rslt_t result;
const uint8_t *ptr_flash_data;
const uint8_t *ptr_ram_data;
uint32_t count;
uint32_t errors_found;
/* Initialize the device and board peripherals */
result = cybsp_init();
if (result != CY_RSLT_SUCCESS)
{
CY_ASSERT(0);
}
/* Initialize printf retarget */
cy_retarget_io_init(CYBSP_DEBUG_UART_HW);
#if ENABLE_XMC_DEBUG_PRINT
printf("Initialization done\r\n");
#endif
/* Turn LED on before flash programming starts */
XMC_GPIO_SetMode(CYBSP_USER_LED_PORT, CYBSP_USER_LED_PIN, XMC_GPIO_MODE_OUTPUT_PUSH_PULL);
/* Note: XMC1000 and XMC4000 Families have inverted polarity on LED */
#if (UC_FAMILY == XMC1)
XMC_GPIO_SetOutputLow(CYBSP_USER_LED_PORT, CYBSP_USER_LED_PIN);
#endif
#if (UC_FAMILY == XMC4)
XMC_GPIO_SetOutputHigh(CYBSP_USER_LED_PORT, CYBSP_USER_LED_PIN);
#endif
/* Step 1: Erase Sector */
XMC_FLASH_EraseSector(XMC_SECTOR_ADDR);
#if ENABLE_XMC_DEBUG_PRINT
printf("Sector erased\r\n");
#endif
/* Step 2: Program data into flash */
XMC_FLASH_ProgramPage(XMC_SECTOR_ADDR, data);
#if ENABLE_XMC_DEBUG_PRINT
printf("Programmed data into flash\r\n");
#endif
/* Step 3: Check for errors */
ptr_flash_data = (uint8_t*) XMC_SECTOR_ADDR;
ptr_ram_data = (uint8_t*) data;
errors_found = 0;
for (count = 0; count < sizeof(data); ++count)
{
if (*ptr_flash_data != *ptr_ram_data)
{
/* Count every wrong byte found */
++errors_found;
}
++ptr_flash_data;
++ptr_ram_data;
}
/* Turn LED off after flash programming is finished */
XMC_GPIO_SetMode(CYBSP_USER_LED_PORT, CYBSP_USER_LED_PIN, XMC_GPIO_MODE_OUTPUT_PUSH_PULL);
/* Note: XMC1000 and XMC4000 Families have inverted polarity on LED */
#if (UC_FAMILY == XMC1)
XMC_GPIO_SetOutputHigh(CYBSP_USER_LED_PORT, CYBSP_USER_LED_PIN);
#endif
#if (UC_FAMILY == XMC4)
XMC_GPIO_SetOutputLow(CYBSP_USER_LED_PORT, CYBSP_USER_LED_PIN);
#endif
/* Check if no incorrect data was found */
if (errors_found == 0)
{
/* Toggle LED to indicate success by starting system timer */
SysTick_Config(SystemCoreClock / TICKS_PER_SECOND);
#if ENABLE_XMC_DEBUG_PRINT
printf("Toggle LED to indicate success by starting system timer\r\n");
#endif
}
while(1);
}
/* [] END OF FILE */