Download Altera NIOS II Processor-Embedded Systems-Lecture 06 Slides-Electrical and Computer Engineering and more Slides Embedded Systems in PDF only on Docsity!
Electrical & Computer Engineering – Embedded Systems Dr. Jeff Jackson Lecture 6-
Embedded Systems
Altera NIOS II Software Development:
Part II
Electrical & Computer Engineering – Embedded Systems Dr. Jeff Jackson Lecture 6-
Using Timer Devices
- Timer devices are hardware peripherals that count clock ticks and can generate periodic interrupt requests
- A timer device can be used to provide a number of time-related facilities, such as: - HAL system clock, alarms, the time-of-day, and time measurement
- The HAL API provides two types of timer device drivers:
- System clock driver: Supports alarms, such as you would use in a scheduler
- Timestamp driver: Supports high-resolution time measurement
- The HAL-specific API functions for accessing timer devices are defined in sys/alt_alarm.h and sys/alt_timestamp.h
Electrical & Computer Engineering – Embedded Systems Dr. Jeff Jackson Lecture 6-
System Clock Driver
• The HAL system clock driver provides a periodic
heartbeat, causing the system clock to increment on
each beat
• Software can use the system clock facilities to
execute functions at specified times, and to obtain
timing information
• A specific hardware timer peripheral is defined as
the system clock device within the BSP settings
Electrical & Computer Engineering – Embedded Systems Dr. Jeff Jackson Lecture 6-
System Clock Driver (continued)
• The HAL provides implementations of the following
standard UNIX functions:
- gettimeofday(), settimeofday(), and times()
• The times returned by these functions are based on
the HAL system clock
• The system clock measures time in clock ticks
• The HAL system clock is not the same as the clock
signal driving the NIOS II processor hardware
• The period of a HAL system clock tick is generally
much longer than the hardware system clock.
system.h defines the clock tick frequency
- This is a coarse grain timer
Electrical & Computer Engineering – Embedded Systems Dr. Jeff Jackson Lecture 6-
Alarms
- Alarms can be considered part of the exception processing system
- User functions can be registered to be executed at a specified time using the HAL alarm facility
- A software program registers an alarm by calling the function:
Electrical & Computer Engineering – Embedded Systems Dr. Jeff Jackson Lecture 6-
Alarms (continued)
- The function callback() is called after nticks have elapsed
- The input argument context is passed as the input argument to callback() when the call occurs - The HAL does not use the context parameter - It is only used as a parameter to the callback() function
- User code must allocate the alt_alarm structure, pointed to by the input argument alarm - This data structure must have a lifetime that is at least as long as that of the alarm - The best way to allocate this structure is to declare it as a static or global - For a static variable, the last value of the variable is preserved between successive calls to a function
- alt_alarm_start() initializes *alarm
Electrical & Computer Engineering – Embedded Systems Dr. Jeff Jackson Lecture 6-
Alarms (continued)
• The callback function can reset the alarm
• The return value of the registered callback function
is the number of ticks until the next call to callback
- A return value of zero indicates that the alarm should be stopped
• You can manually cancel an alarm by calling
alt_alarm_stop()
• One alarm is created for each call to
alt_alarm_start()
- Multiple alarms can run simultaneously
Electrical & Computer Engineering – Embedded Systems Dr. Jeff Jackson Lecture 6-
Using a Periodic Alarm Callback Function
Electrical & Computer Engineering – Embedded Systems Dr. Jeff Jackson Lecture 6-
Timestamp Driver (continued)
• The rate at which the timestamp counter increments
can be obtained by calling the function
alt_timestamp_freq()
• This rate is typically the hardware frequency of the
NIOS II processor system
- Usually millions of cycles per second
• The timestamp driver functions are defined
(prototyped) in the alt_timestamp.h header file
Electrical & Computer Engineering – Embedded Systems Dr. Jeff Jackson Lecture 6-
Timestamp Driver Example (part 1)
Electrical & Computer Engineering – Embedded Systems Dr. Jeff Jackson Lecture 6-
Timestamp Driver Example (part 2)
Electrical & Computer Engineering – Embedded Systems Dr. Jeff Jackson Lecture 6-
Basic Parallel Input/Output (PIO) Devices
• The HAL provides basic functions for low level
read/write access to PIO devices
- IOWR_ALTERA_AVALON_PIO_DATA
- IORD_ALTERA_AVALON_PIO_DATA
• Device names are defined in system.h
• MACROS for accessing devices via low level
register I/O read/write functions are defined in
altera_avalon_pio_regs.h
Electrical & Computer Engineering – Embedded Systems Dr. Jeff Jackson Lecture 6-
Example HAL-Based PIO Operations
volatile alt_u8 led; /* Turn the LEDs on. Active low output*/ led = 0x00; IOWR_ALTERA_AVALON_PIO_DATA(LED_PIO_BASE, led & 0xF);
/* Unpressed buttons are active high by default */ volatile alt_u8 buttons; buttons=IORD_ALTERA_AVALON_PIO_DATA(BUTTON_PIO_BASE);
Electrical & Computer Engineering – Embedded Systems Dr. Jeff Jackson Lecture 6-
Using Flash Devices
- The HAL provides a generic device model for nonvolatile flash memory devices
- Flash memories use special programming protocols to store (write) data
- The HAL API provides functions to write data to flash memory
- For example, you can use these functions to implement a flash-based file subsystem
- The HAL API also provides functions to read flash, although it is generally not necessary
- For most flash devices, programs can treat the flash memory space as simple memory when reading, and do not need to call special HAL API functions
Electrical & Computer Engineering – Embedded Systems Dr. Jeff Jackson Lecture 6-
HAL API for Flash Devices
- The API provides two levels of access to a flash device
- Simple flash access
- Functions that write buffers to flash and read them back at the block level
- In writing, if the buffer is less than a full block, these functions erase preexisting flash data above and below the newly written data
- Fine-grained flash access
- Functions that write buffers to flash and read them back at the buffer level
- In writing, if the buffer is less than a full block, these functions preserve preexisting flash data above and below the newly written data
- This functionality is generally required for managing a file subsystem
- The API functions for accessing flash devices are defined in sys/alt_flash.h
Electrical & Computer Engineering – Embedded Systems Dr. Jeff Jackson Lecture 6-
Simple Flash Access
• This interface consists of the functions
- alt_flash_open_dev()
- alt_write_flash()
- alt_read_flash(), and
- alt_flash_close_dev()
• A flash device is opened by calling
alt_flash_open_dev(), which returns a file handle to
a flash device
• This function takes a single argument that is the
name of the flash device, as defined in system.h
Electrical & Computer Engineering – Embedded Systems Dr. Jeff Jackson Lecture 6-
Simple Flash Access (continued)
• The function alt_flash_close_dev() takes a file
handle and closes the device
• The prototype for this function is:
void alt_flash_close_dev(alt_flash_fd* fd )
Electrical & Computer Engineering – Embedded Systems Dr. Jeff Jackson Lecture 6-
Block Erasure or Corruption
• Generally, flash memory is divided into blocks
• alt_write_flash() might need to erase the contents of
a block before it can write data to it
• In this case, it makes no attempt to preserve the
existing contents of the block
• This action can lead to unexpected data corruption
(erasure), if you are performing writes that do not
fall on block boundaries
• If you wish to preserve existing flash memory
contents, use the fine-grained flash functions
Electrical & Computer Engineering – Embedded Systems Dr. Jeff Jackson Lecture 6-
Block Erasure Example
- Assume an 8-kilobyte (KB) flash memory comprising two 4-KB blocks
- Assume the following action is performed:
- First write 5 KB of all 0xAA to flash memory at address 0x0000, and then write 2 KB of all 0xBB to address 0x
- After the first write succeeds (at time t(2)), the flash memory contains 5 KB of 0xAA, and the rest is empty (that is, 0xFF)
- Then the second write begins, but before writing to the second block, the block is erased
- At this point, t(3), the flash contains 4 KB of 0xAA and 4 KB of 0xFF
- After the second write finishes, at time t(4), the 2 KB of 0xFF at address 0x1000 is corrupted
Electrical & Computer Engineering – Embedded Systems Dr. Jeff Jackson Lecture 6-
Block Erasure Example
Electrical & Computer Engineering – Embedded Systems Dr. Jeff Jackson Lecture 6-
Fine-Grained Flash Access
• Three additional functions provide complete control
for writing flash contents at the highest granularity:
- alt_get_flash_info()
- alt_erase_flash_block()
- alt_write_flash_block()
• By the nature of flash memory, you cannot erase a
single address in a block
• You must erase (that is, set to all ones) an entire
block at a time
• Writing to flash memory can only change bits from 1
to 0; to change any bit from 0 to 1, you must erase
the entire block along with it
Electrical & Computer Engineering – Embedded Systems Dr. Jeff Jackson Lecture 6-
Fine-Grained Flash Access (continued)
• To alter a specific location in a block while leaving
the surrounding contents unchanged, you must:
- read out the entire contents of the block to a buffer,
- alter the value(s) in the buffer,
- erase the flash block, and finally
- write the whole block-sized buffer back to flash memory
• The fine-grained flash access functions automate
this process at the flash block level
Electrical & Computer Engineering – Embedded Systems Dr. Jeff Jackson Lecture 6-
alt_get_flash_info()
- alt_get_flash_info() gets the number of erase regions, the number of erase blocks in each region, and the size of each erase block
- The function prototype is as follows: int alt_get_flash_info ( alt_flash_fd* fd, flash_region** info, int* number_of_regions )
- If the call is successful, on return the address pointed to by number_of_regions contains the number of erase regions in the flash memory, and *info points to an array of flash_region structures
- This array is part of the file descriptor
Electrical & Computer Engineering – Embedded Systems Dr. Jeff Jackson Lecture 6-
The flash_region Structure
- The flash_region structure is defined in sys/alt_flash_types.h
- The data structure is defined as follows:
typedef struct flash_region { int offset; /* Offset of this region from start of the flash / int region_size; / Size of this erase region / int number_of_blocks; / Number of blocks in this region / int block_size; / Size of each block in this erase region */ } flash_region;
- With the information obtained by calling alt_get_flash_info(), you are in a position to erase or program individual blocks of the flash device
Electrical & Computer Engineering – Embedded Systems Dr. Jeff Jackson Lecture 6-
Using the Fine-Grained Flash Access API
Functions
Electrical & Computer Engineering – Embedded Systems Dr. Jeff Jackson Lecture 6-
Using the Fine-Grained Flash Access API
Functions