What is SFR paging, and why is it necessary?
Special Function Register (SFR) Paging has been implemented on some Silicon Labs device families to accommodate the increased number of peripherals available on these devices.
The memory-mapped address space allotment for SFRs in the 8051 architecture is limited to 128 Bytes at addresses 0x80 to 0xFF. Special Function Registers in some Silicon Labs devices are distributed across multiple 'pages,' which are accessed using the SFRPAGE register. Paging allows addresses 0x80 to 0xFF to access more than 128 SFRs. The SFRPAGE register selects which page of 128 SFRs is accessible in the 0x80 to 0xFF SFR address space. As a result, special care must be taken to ensure that the correct SFR page is selected before reading or modifying an SFR.
As an example, the C8051F060 UART0 SFRs are on Page 0 of the device. This means code should first set the SFRPAGE register to 0x00 before performing any modifications to UART0 registers (UART0_PAGE is defined as 0x00 in the header file for convenience). Some example code for writing to the SBUF0 register is:
SFRPAGE = UART0_PAGE; // Select the correct SFR page. In this case the UART0 page
SBUF0 = 0x41; // Write into the SFR, in this case SBUF0
It is generally good practice to save the original SFRPAGE value at the beginning of a function and restore it at the end of the function to avoid overwiting SFRPAGE in the calling function. For example:
unsigned char SFRPAGE_save; // Local variable where the SFRPAGE will be preserved
SFRPAGE_save = SFRPAGE; // Save the original content of the SFRPAGE register
SFRPAGE = UART0_PAGE; // Select the SFR page. In this case the UART0 page
SBUF0 = 0x41; // Write the SFR, in this case SBUF0
SFRPAGE = SFRPAGE_save; // Restore the content of the SFRPAGE register
Certain fundamental 8051 registers, such as the Accumulator, PSW, SP, DPL, DPH, etc. are accessible on all SFR pages. Each device family datasheet contains an SFR map that describes the addresses and SFRPAGE pages for each register, where applicable.
Application Note AN131 provides more information on how SFR paging should be handled. It is available for download on the Silicon Labs Application Notes webpage: