📄 ssi_atmel.c
字号:
//*****************************************************************************
//
// ssi_atmel.c - SSI example.
//
// Copyright (c) 2005-2007 Luminary Micro, Inc. All rights reserved.
//
// Software License Agreement
//
// Luminary Micro, Inc. (LMI) is supplying this software for use solely and
// exclusively on LMI's microcontroller products.
//
// The software is owned by LMI and/or its suppliers, and is protected under
// applicable copyright laws. All rights are reserved. You may not combine
// this software with "viral" open-source software in order to form a larger
// program. Any use in violation of the foregoing restrictions may subject
// the user to criminal sanctions under applicable laws, as well as to civil
// liability for the breach of the terms and conditions of this license.
//
// THIS SOFTWARE IS PROVIDED "AS IS". NO WARRANTIES, WHETHER EXPRESS, IMPLIED
// OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
// LMI SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR
// CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
//
// This is part of revision 1952 of the Stellaris Peripheral Driver Library.
//
//*****************************************************************************
#include "../../../hw_ints.h"
#include "../../../hw_memmap.h"
#include "../../../hw_types.h"
#include "../../../src/debug.h"
#include "../../../src/gpio.h"
#include "../../../src/interrupt.h"
#include "../../../src/ssi.h"
#include "../../../src/sysctl.h"
#include "../../../utils/diag.h"
#include "../pdc.h"
//*****************************************************************************
//
//! \addtogroup dk_lm3s811_list
//! <h1>SSI (ssi_atmel)</h1>
//!
//! This example application uses the SSI master to communicate with the Atmel
//! AT25F1024A EEPROM that is on the development board. The first 256 bytes of
//! the EEPROM are erased and then programmed with an incrementing sequence.
//! The data is then read back to verify its correctness. The transfer is
//! managed by an interrupt handler in response to the SSI interrupt; since a
//! 256-byte read at a 1 MHz SSI bus speed takes around 2 ms, this allows a lot
//! of other processing to occur during the transfer (though that time is not
//! utilized by this example).
//
//*****************************************************************************
//*****************************************************************************
//
// The GPIO port A pin numbers for the various SSI signals.
//
//*****************************************************************************
#define SSI_CS GPIO_PIN_3
#define SSI_CLK GPIO_PIN_2
#define SSI_TX GPIO_PIN_5
#define SSI_RX GPIO_PIN_4
//*****************************************************************************
//
// Commands that can be sent to the Atmel AT25F1024A.
//
//*****************************************************************************
#define ATMEL_WREN 0x06 // Set write enable latch
#define ATMEL_WRDI 0x04 // Reset write enable latch
#define ATMEL_RDSR 0x05 // Read status register
#define ATMEL_WRSR 0x01 // Write status register
#define ATMEL_READ 0x03 // Read data from memory array
#define ATMEL_PROGRAM 0x02 // Program data into memory array
#define ATMEL_SECTOR_ERASE 0x52 // Erase one sector in memory array
#define ATMEL_CHIP_ERASE 0x62 // Erase all sectors in memory arr.
#define ATMEL_RDID 0x15 // Read manufacturer and product ID
//*****************************************************************************
//
// A buffer to contain data written to or read from the Atmel device.
//
//*****************************************************************************
static unsigned char g_pucData[260];
//*****************************************************************************
//
// The variables that track the data to be transmitted.
//
//*****************************************************************************
static unsigned char *g_pucDataOut;
static unsigned long g_ulOutCount;
static unsigned long g_ulOutExtra;
//*****************************************************************************
//
// The variables that track the data to be received.
//
//*****************************************************************************
static unsigned char *g_pucDataIn;
static unsigned long g_ulInCount;
static unsigned long g_ulInExtra;
//*****************************************************************************
//
// The error routine that is called if the driver library encounters an error.
//
//*****************************************************************************
#ifdef DEBUG
void
__error__(char *pcFilename, unsigned long ulLine)
{
}
#endif
//*****************************************************************************
//
// The interrupt handler for the SSI interrupt.
//
//*****************************************************************************
void
SSIIntHandler(void)
{
unsigned long ulStatus, ulCount, ulData;
//
// Get the reason for the interrupt.
//
ulStatus = SSIIntStatus(SSI0_BASE, true);
//
// See if the receive interrupt is being asserted.
//
if(ulStatus & (SSI_RXFF | SSI_RXTO))
{
//
// Loop as many times as required to empty the FIFO.
//
while(1)
{
//
// Read a byte from the FIFO if possible. Break out of the loop if
// the FIFO is empty.
//
if(SSIDataGetNonBlocking(SSI0_BASE, &ulData) == 0)
{
break;
}
//
// See if this byte needs to be saved.
//
if(g_ulInCount)
{
//
// Save this byte.
//
*g_pucDataIn++ = ulData;
//
// Decrement the count of bytes to save.
//
g_ulInCount--;
}
else
{
//
// Decrement the count of extra bytes to read.
//
g_ulInExtra--;
}
}
//
// See if all data has been transmitted and received.
//
if((g_ulInCount + g_ulInExtra + g_ulOutCount + g_ulOutExtra) == 0)
{
//
// All data has been transmitted and received, so disable the
// receive interrupt.
//
SSIIntDisable(SSI0_BASE, SSI_RXFF | SSI_RXTO);
//
// Deassert the SSI chip select.
//
GPIOPinWrite(GPIO_PORTA_BASE, SSI_CS, SSI_CS);
}
}
//
// See if the transmit interrupt is being asserted.
//
if(ulStatus & SSI_TXFF)
{
//
// Write up to four bytes into the FIFO.
//
for(ulCount = 0; ulCount < 4; ulCount++)
{
//
// See if there is more data to be transmitted.
//
if(g_ulOutCount)
{
//
// Transmit this byte if possible. Break out of the loop if
// the FIFO is full.
//
if(SSIDataPutNonBlocking(SSI0_BASE, *g_pucDataOut) == 0)
{
break;
}
//
// Decrement the count of bytes to be transmitted.
//
g_pucDataOut++;
g_ulOutCount--;
}
//
// See if there are more extra bytes to be transmitted.
//
else if(g_ulOutExtra)
{
//
// Transmit this extra byte if possible. Break out of the loop
// if the FIFO is full.
//
if(SSIDataPutNonBlocking(SSI0_BASE, 0) == 0)
{
break;
}
//
// Decrement the count of extra bytes to be transmitted.
//
g_ulOutExtra--;
}
//
// Otherwise, stop transmitting data.
//
else
{
//
// Disable the transmit interrupt since all data to be
// transmitted has been written into the FIFO.
//
SSIIntDisable(SSI0_BASE, SSI_TXFF);
//
// Break out of the loop since there is no more data to
// transmit.
//
break;
}
}
}
}
//*****************************************************************************
//
// This will start an interrupt driven transfer to the SSI peripheral.
//
//*****************************************************************************
void
SSITransfer(unsigned char *pucDataOut, unsigned long ulOutCount,
unsigned char *pucDataIn, unsigned long ulInCount)
{
//
// Save the output data buffer.
//
g_pucDataOut = pucDataOut;
g_ulOutCount = ulOutCount;
//
// Save the input data buffer.
//
g_pucDataIn = pucDataIn;
g_ulInCount = ulInCount;
//
// Compute the number of extra bytes to send or receive. These counts make
// the number of bytes transmitted equal to the number of bytes received;
// a requirement of the SSI peripheral.
//
if(ulInCount > ulOutCount)
{
g_ulOutExtra = ulInCount - ulOutCount;
g_ulInExtra = 0;
}
else
{
g_ulOutExtra = 0;
g_ulInExtra = ulOutCount - ulInCount;
}
//
// Assert the SSI chip select.
//
GPIOPinWrite(GPIO_PORTA_BASE, SSI_CS, 0);
//
// Enable the transmit and receive interrupts. This will start the actual
// transfer.
//
SSIIntEnable(SSI0_BASE, SSI_TXFF | SSI_RXFF | SSI_RXTO);
//
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -