//-----------------------------------------------------------------------------
// F91x_SPI0_Master.c
//-----------------------------------------------------------------------------
// Copyright 2014 Silicon Laboratories, Inc.
// http://developer.silabs.com/legal/version/v11/Silicon_Labs_Software_License_Agreement.txt
//
// Program Description:
//
// This program configures a C8051F91x as a 4-wire SPI Single Master.
//
// The SPI clock in this example is limited to 500 kHz when used with the
// SPI0_Slave code example. During a SPI_Read, the slave needs some time to
// interpret the command and write the appropriate data to the SPI0DAT
// register, and the slave no longer has enough time to complete the
// SPI_READ_BUFFER command with a clock greater than 500 kHz. For faster SPI
// clocks, a dummy byte between the command and the first byte of Read data
// will be required.
//
// This example is intended to be used with the SPI0_Slave example.
//
// Pinout:
//
// P0.0 - SPI SCK (digital output, push-pull)
// P0.1 - SPI MISO (digital input, open-drain)
// P0.2 - SPI MOSI (digital output, push-pull)
// P0.3 - SPI NSS (digital output, push-pull)
//
// P1.6 - LED (digital output, push-pull)
//
// all other port pins unused.
//
//
// How To Test:
//
// 1) Verify that J13 and J14 are not populated.
// 2) Connect the device to another 'F91x device running SPI slave code.
// 3) Ensure that jumpers are placed on the following:
// J8: P1.6/LED1.6
// J11: PWR/WALL_PWR
// J17: VDD_PIN/PWR
// 4) Connect the USB Debug Adapter to J4.
// 5) Turn the POWER switch (SW5) to the "ON" position.
// 6) Compile and download code to a 'F91x device on a C8051F91x-TB development
// board by selecting Run -> Debug from the menus, clicking the Debug button
// in the quick menu, or pressing F11.
// 7) Run the code by selecting Run -> Resume from the menus, clicking the
// Resume button in the quick menu, or pressing F8.
// 8) If the communication passes, the LEDs on both the Master and Slave
// boards will blink slowly. If it fails, the LEDs will be OFF.
//
//
// Target: C8051F90x-C8051F91x
// Tool chain: Simplicity Studio / Keil C51 9.51
// Command Line: None
//
// Release 1.3 (BL)
// - Updated Description / How to Test
// - 14 JAN 2014
//
// Release 1.2
// - Compiled and tested for Raisonance Toolchain (FB)
// - 17 MAY 10
//
// Release 1.1
// - Port to C8051F912 from C8051F930 (JH)
// - Compile and test C8051F912_defs.h on 912 board (JH)
// - 07 JULY 2009
//
// Release 1.0
// -Initial Revision (FB)
// -19 NOV 2007
//
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include "REG51F912.H"
//-----------------------------------------------------------------------------
// Global Constants
//-----------------------------------------------------------------------------
#define SYSCLK 24500000 // Internal oscillator frequency in Hz
#define SPI_CLOCK 500000 // Maximum SPI clock
// The SPI clock is a maximum of 500 kHz
// when this example is used with
// the SPI0_Slave code example.
#define MAX_BUFFER_SIZE 8 // Maximum buffer Master will send
// Instruction Set
#define SLAVE_LED_ON 0x01 // Turn the Slave LED on
#define SLAVE_LED_OFF 0x02 // Turn the Slave LED off
#define SPI_WRITE 0x04 // Send a byte from the Master to the
// Slave
#define SPI_READ 0x08 // Send a byte from the Slave to the
// Master
#define SPI_WRITE_BUFFER 0x10 // Send a series of bytes from the
// Master to the Slave
#define SPI_READ_BUFFER 0x20 // Send a series of bytes from the Slave
// to the Master
#define ERROR_OCCURRED 0x40 // Indicator for the Slave to tell the
// Master an error occurred
#define LED_ON 0
#define LED_OFF 1
SBIT (LED, SFR_P1, 6); // LED==LED_ON means ON
//-----------------------------------------------------------------------------
// Global Variables
//-----------------------------------------------------------------------------
U8 SPI_Data = 0xA5;
U8 SPI_Data_Array[MAX_BUFFER_SIZE] = {0};
bit Error_Flag = 0;
U8 Command = 0x00;
//-----------------------------------------------------------------------------
// Function Prototypes
//-----------------------------------------------------------------------------
void PCA0_Init (void);
void Oscillator_Init (void);
void Port_Init (void);
void SPI0_Init (void);
void Init_Device (void);
void SPI_LED_On (void);
void SPI_LED_Off (void);
void SPI_Byte_Write (void);
void SPI_Byte_Read (void);
void SPI_Array_Write (void);
void SPI_Array_Read (void);
void Delay(void);
INTERRUPT_PROTO (SPI0_ISR, SPI0_IRQn);
//-----------------------------------------------------------------------------
// main() Routine
//-----------------------------------------------------------------------------
void main (void)
{
U8 test_value = 0x55;
U8 test_array[MAX_BUFFER_SIZE] = {1,2,3,4,5,6,7,8};
U8 i;
Init_Device (); // Initializes hardware peripherals
IE_EA = 1; // Enable global interrupts
LED = LED_OFF;
// TEST BEGIN --------------------------------------------------------------
SPI_Data = test_value;
// Write a value
SPI_Byte_Write ();
while (!SPI0CN_NSSMD0); // Wait until the Write transfer has
// finished
// Read the same value back
SPI_Data = 0x00;
SPI_Byte_Read ();
while (!SPI0CN_NSSMD0); // Wait until the Read transfer has
// finished
// Check if the sent value and returned value match
if (SPI_Data != test_value)
{
Error_Flag = 1;
}
// Copy test_array into SPI_Data_Array
for (i = 0; i < MAX_BUFFER_SIZE; i++)
{
SPI_Data_Array[i] = test_array[i];
}
// Send the array to the slave
SPI_Array_Write ();
while (!SPI0CN_NSSMD0); // Wait until the Write transfer has
// finished
// Clear SPI_Data_Array for the SPI_Buffer_Read function
for (i = 0; i < MAX_BUFFER_SIZE; i++)
{
SPI_Data_Array[i] = 0;
}
// Read the array back from the slave
SPI_Array_Read ();
while (!SPI0CN_NSSMD0); // Wait until the Read transfer has
// finished
// Check if the received array matches the sent array
for (i = 0; i < MAX_BUFFER_SIZE; i++)
{
if (SPI_Data_Array[i] != test_array[i])
{
Error_Flag = 1;
}
}
// END OF TEST -------------------------------------------------------------
while (1)
{
// If no error has occurred, blink the LEDs on the Master and Slave
// boards
if (Error_Flag == 0)
{
LED = LED_ON;
SPI_LED_On ();
while (!SPI0CN_NSSMD0);
Delay ();
SPI_LED_Off ();
LED = LED_OFF;
while (!SPI0CN_NSSMD0);
Delay ();
}
};
}
//-----------------------------------------------------------------------------
// Initialization Subroutines
//----------------------------------------------------------------
评论0