Microchip® Advanced Software Framework

mpu_example1.c File Reference

Example of usage of the Software Framework MPU driver in AVR UC3.

Copyright (c) 2014-2018 Microchip Technology Inc. and its subsidiaries.

#include <stdint.h>
#include "board.h"
#include "compiler.h"
#include "print_funcs.h"
#include "sysclk.h"
#include "gpio.h"
#include "mpu.h"
#include "cycle_counter.h"

Macros

Regions
#define NB_CONFIG_MPU_ENTRIES   4
 
#define REGION_0_FLASH_INDEX   0
 
#define REGION_1_RAM_INDEX   1
 
#define REGION_2_STACK_INDEX   2
 
#define REGION_3_PERIPHERALS_INDEX   3
 

Enumerations

enum  exception_status {
  NO_EXCEPTION = 0,
  TLB_MULTIPLE_HIT_EXCEPTION = 1,
  ITLB_MISS_EXCEPTION = 2,
  ITLB_PROTECTION_EXCEPTION = 3,
  DTLB_WRITE_PROTECTION_EXCEPTION = 4,
  DTLB_WRITE_MISS_EXCEPTION = 5,
  DTLB_READ_PROTECTION_EXCEPTION = 6,
  DTLB_READ_MISS_EXCEPTION = 7,
  UNEXPECTED_EXCEPTION = -1,
  NO_EXCEPTION = 0,
  TLB_MULTIPLE_HIT_EXCEPTION = 1,
  ITLB_MISS_EXCEPTION = 2,
  ITLB_PROTECTION_EXCEPTION = 3,
  DTLB_WRITE_PROTECTION_EXCEPTION = 4,
  DTLB_WRITE_MISS_EXCEPTION = 5,
  DTLB_READ_PROTECTION_EXCEPTION = 6,
  DTLB_READ_MISS_EXCEPTION = 7,
  UNEXPECTED_EXCEPTION = -1
}
 Exception State. More...
 

Functions

static void configure_memory_area (void)
 Configure the access permissions for different memory regions. More...
 
static void forbidden_access_func (void)
 Function with restricted access. More...
 
void handle_exception (uint32_t *sp, uint32_t exception_address, uint32_t exception_cause)
 Handle exception; called by the _evba assembly function. More...
 
static void infinite_led_chaser (void)
 LED chaser function. More...
 
int main (void)
 Main function. More...
 
static void mpu_configuration_error (void)
 Error occurred during MPU configuration. More...
 

Variables

mpu_entry_t dmpu_entries [NB_CONFIG_MPU_ENTRIES]
 
enum exception_status exception_state = NO_EXCEPTION
 

#define NB_CONFIG_MPU_ENTRIES   4
#define REGION_0_FLASH_INDEX   0

Referenced by configure_memory_area().

#define REGION_1_RAM_INDEX   1

Referenced by configure_memory_area().

#define REGION_2_STACK_INDEX   2

Referenced by configure_memory_area().

#define REGION_3_PERIPHERALS_INDEX   3

Referenced by configure_memory_area().

Exception State.

Enumerator
NO_EXCEPTION 

No exception has occurred.

TLB_MULTIPLE_HIT_EXCEPTION 

The TLB Multiple Hit Exception is generated when an access hits in multiple MPU regions.

This is usually caused by programming error.

ITLB_MISS_EXCEPTION 

The ITLB Miss exception is generated when the MPU is enabled and the instruction memory access does not hit in any regions.

ITLB_PROTECTION_EXCEPTION 

The ITLB Protection exception is generated when the instruction memory access violates the access rights specified by the protection region in which the address lies.

DTLB_WRITE_PROTECTION_EXCEPTION 

The DTLB Protection exception is generated when the data memory write violates the access rights specified by the protection region in which the address lies.

DTLB_WRITE_MISS_EXCEPTION 

The DTLB Write Miss exception is generated when the MPU is enabled and the data memory write access does not hit in any regions.

DTLB_READ_PROTECTION_EXCEPTION 

The DTLB Protection exception is generated when the data memory read violates the access rights specified by the protection region in which the address lies.

DTLB_READ_MISS_EXCEPTION 

The DTLB Read Miss exception is generated when the MPU is enabled and the data memory read access does not hit in any regions.

UNEXPECTED_EXCEPTION 

Any other unexpected exception.

NO_EXCEPTION 

No exception has occurred.

TLB_MULTIPLE_HIT_EXCEPTION 

The TLB Multiple Hit Exception is generated when an access hits in multiple MPU regions.

This is usually caused by programming error.

ITLB_MISS_EXCEPTION 

The ITLB Miss exception is generated when the MPU is enabled and the instruction memory access does not hit in any regions.

ITLB_PROTECTION_EXCEPTION 

The ITLB Protection exception is generated when the instruction memory access violates the access rights specified by the protection region in which the address lies.

DTLB_WRITE_PROTECTION_EXCEPTION 

The DTLB Protection exception is generated when the data memory write violates the access rights specified by the protection region in which the address lies.

DTLB_WRITE_MISS_EXCEPTION 

The DTLB Write Miss exception is generated when the MPU is enabled and the data memory write access does not hit in any regions.

DTLB_READ_PROTECTION_EXCEPTION 

The DTLB Protection exception is generated when the data memory read violates the access rights specified by the protection region in which the address lies.

DTLB_READ_MISS_EXCEPTION 

The DTLB Read Miss exception is generated when the MPU is enabled and the data memory read access does not hit in any regions.

UNEXPECTED_EXCEPTION 

Any other unexpected exception.

static void configure_memory_area ( void  )
static

Configure the access permissions for different memory regions.

Assign different access permissions to the memory regions

  • Flash Region 0, subregions 0 to 11 - Read / eXecute
  • Flash Region 0, subregions 12 to 15 - Read
  • RAM Region 1, subregions 0 to 14 - Read / Write
  • RAM Region 1, subregion 15 - Read
  • Stack Region 2, all subregions - Read / Write
  • PBA memory map Region 3, all subregions - Read / Write
Note
WARNING NOTE: there are limitations concerning the region size; See doc32002 "AVR32UC Technical Reference Manual Complete" Table 6-1. "Protection region sizes implied by the Size field". The mpu_convert_kbsize_to_eregionsize() makes sure this part of the spec is respected.

References _stack, mpu_entry_t::addr, enable_mpu(), Get_system_register, MPU_APRA_ID, MPU_APRB_ID, mpu_configuration_error(), mpu_convert_kbsize_to_eregionsize(), MPU_PRIVR_UNPRIVNONE, MPU_PRIVRW_UNPRIVNONE, MPU_PRIVRX_UNPRIVRX, MPU_REGION_SIZE_4KB, MPU_REGION_SIZE_64KB, print_dbg(), print_dbg_hex(), REGION_0_FLASH_INDEX, REGION_1_RAM_INDEX, REGION_2_STACK_INDEX, REGION_3_PERIPHERALS_INDEX, select_subregion(), set_access_permissions(), set_mpu_entry(), mpu_entry_t::size, and mpu_entry_t::valid.

Referenced by main().

static void forbidden_access_func ( void  )
static

Function with restricted access.

Forbidden_access_func() is placed in the section .ForbiddenText. This region is configured as non-executable in configure_memory_area(). .ForbiddenText is a section created for this example, placed in a well-known location in Flash. See the linker script bundled with the example.

Referenced by main().

void handle_exception ( uint32_t *  sp,
uint32_t  exception_address,
uint32_t  exception_cause 
)

Handle exception; called by the _evba assembly function.

Parameters
*sppointer to stack before the exception (provided through R12. At this point, sp[0]=SR, sp[1]=PC.
exception_addressaddress of the offending instruction
exception_causeexception cause ((EVBA offset of the exception)>>2)
Note
Test exception cause register AVR32_ECR. AVR32_ECR is updated with a value equal to the 9 lowest bits of the EVBA offset of the exception shifted 2 bits to the right.

References disable_mpu(), DTLB_READ_MISS_EXCEPTION, DTLB_READ_PROTECTION_EXCEPTION, DTLB_WRITE_MISS_EXCEPTION, DTLB_WRITE_PROTECTION_EXCEPTION, exception_state, ITLB_MISS_EXCEPTION, ITLB_PROTECTION_EXCEPTION, TLB_MULTIPLE_HIT_EXCEPTION, and UNEXPECTED_EXCEPTION.

static void infinite_led_chaser ( void  )
static

LED chaser function.

This function is called when the example has completed successfully. LED0 & LED1 blink alternatively forever after the example completion.

References i, and max.

Referenced by main().

int main ( void  )

Main function.

This example describes how to setup the AVR32 UC3 MPU with protected regions.
The following exceptions arises in the main function when trying to access the protected regions.

  • AVR32_EVBA_OFFSET_ITLB_PROT -> On trying to execute from a Read only (eXecute disabled)Flash region
  • AVR32_EVBA_OFFSET_DTLB_PROT_W -> On writing to Read only region in RAM

References configure_memory_area(), cpu_delay_ms(), cpu_irq_enable, DTLB_WRITE_PROTECTION_EXCEPTION, Enable_global_exception, exception_state, forbidden_access_func(), FOSC0, infinite_led_chaser(), init_dbg_rs232(), ITLB_PROTECTION_EXCEPTION, print_dbg(), sysclk_get_pba_hz(), and sysclk_init().

static void mpu_configuration_error ( void  )
static

Error occurred during MPU configuration.

This function is called when an error occurs in configuring MPU. LEDs are toggled infinitely in a loop.

References i.

Referenced by configure_memory_area().

enum exception_status exception_state = NO_EXCEPTION

Referenced by handle_exception(), and main().