Вот весь код
(«Телесистемы»: Конференция «Микроконтроллеры и их применение»)

миниатюрный аудио-видеорекордер mAVR

Отправлено Bill 13 апреля 2004 г. 16:51
В ответ на: Я тоже так думал :) отправлено Shadow 13 апреля 2004 г. 16:25


#include

#include "recorder.h"
#include "Smart.h"
#include "timers.h"

//
// Static variable locations
//
static char near BA_Table[NBLOCKS/8]; // Block allocation table

volatile char Buffer1[256], // SmartMedia buffers
Buffer2[256], //
*CodecPtr, // and pointers to them
*SmartPtr; //
volatile unsigned PageRdAddr, // Current page read address
PageWrAddr, // Current page write address
HighRdAddr, // High available read page number
HighWrAddr; // High available write page number

volatile char SmartState; // SmartMedia Card current state

//
// ** SmartWrByte -- input data latch cycle routine
//
static void SmartWrByte(char _byte)
{
SMART_OUTPUT = _byte; // Write a byte
SMART_CTRL &= ~WE_BIT; // Set WE signal
_NOP();
SMART_CTRL |= WE_BIT; // Remove WE signal
}

//
// ** SmartRdByte -- output data latch cycle routine
//
static char monitor SmartRdByte(void)
{
char _byte;

SMART_DDR = 0x00; // Set data port as input
SMART_OUTPUT = 0x00; //
SMART_CTRL &= ~CE_BIT; // Set CE control signal
SMART_CTRL &= ~RE_BIT; // Set RE signal
_NOP();
_byte = SMART_INPUT; // Read a byte
SMART_CTRL |= RE_BIT; // Remove RE signal
SMART_CTRL |= CE_BIT; // Remove CE control signal
return _byte;
}

//
// ** SmartWrAdr -- address latch cycle routine
//
static void SmartWrAdr(unsigned long _adr)
{
// PORTB &= ~(1<<5);
// PORTB |= (1<<6);
_CLI(); // Disable interrupts
SMART_DDR = 0xFF; // Set data port as output
SMART_CTRL &= ~CE_BIT; // Set CE control signal
SMART_CTRL |= ALE_BIT; // Set ALE control signal
SmartWrByte(_adr & 0xFF); // Send a low address byte
SmartWrByte((_adr>>8) & 0xFF); // Send a middle address byte
SmartWrByte((_adr>>16) & 0xFF); // Send a high address byte
SMART_CTRL &= ~ALE_BIT; // Remove ALE
// SMART_CTRL |= CE_BIT; // and CE control signals
// _SEI(); // Enable interrupts
// PORTB &= ~(1<<6);
}

//
// ** SmartWrCmd -- command latch cycle routine
//
void SmartWrCmd(char _cmd)
{
// PORTB |= (1<<4);
_CLI(); // Disable interrupts
SMART_DDR = 0xFF; // Set data port as output
SMART_OUTPUT = _cmd; // Send a command
SMART_CTRL |= CLE_BIT; // Set control signals
SMART_CTRL &= ~CE_BIT; //
SMART_CTRL &= ~WE_BIT; //
_NOP(); //
SMART_CTRL |= WE_BIT | CE_BIT; // Remove control signals
SMART_CTRL &= ~CLE_BIT; //
_SEI(); // Enable interrupts
// PORTB &= ~(1<<4);
// PORTB |= (1<<5);
}

//
// ** SmartRdSts -- read status routine
//
char SmartRdSts(void)
{
SmartWrCmd(CMD_RdStatus);
return SmartRdByte();
}

//
// ** SmartWait -- Wait for the erase/program operation copletion
// Returns zero if the operation fails, otherwise returns non zero result.
//
static char SmartWait(void)
{
char _sts;

while (!((_sts=SmartRdSts()) & READY_BIT)); // Wait for the operation completion
return ((_sts ^ PEERROR_BIT) & PEERROR_BIT); // Return error status
}

//
// ** SmartRdPage -- read the page to buffer routine
//
void SmartRdPage(void)
{
char *_cp; // Buffer pointer
char _cnt; // Set a byte counter

SmartState |= READ_BIT; // Set the flag
SmartWrCmd((SmartState & PAGE2_RD_BIT)? CMD_Rd1 : CMD_Rd0);
SmartWrAdr((long)PageRdAddr<<8);// Write the page address
SleepMCU(9); // Wait for the device ready
SMART_DDR = 0x00; // Set data port as input
SMART_OUTPUT = 0x00; //

_cp = SmartPtr; // Point at read buffer
_cnt = 0; // Set a byte counter
_CLI(); // Disable interrupts
SMART_CTRL &= ~CE_BIT; // Set CE control signal
do { // the page reading
SMART_CTRL &= ~RE_BIT; // Set RE signal
_NOP();
*_cp++ = SMART_INPUT; // Read a byte
SMART_CTRL |= RE_BIT; // Remove RE signal
}
while (--_cnt);
SMART_CTRL |= CE_BIT; // Remove CE control signal
_SEI(); // Enable interrupts
if (SmartState & PAGE2_RD_BIT) // Update the data
{
++PageRdAddr;
}
SmartState = PAGE2_RD_BIT ^ SmartState & ~READ_BIT; // Reset the flag
}
//
// ** SmartPrPage -- program the page from buffer routine
//
char SmartPrPage(void)
{
char *_cp; // Point at write buffer
char _cnt; // Set a byte counter

SmartState |= PROGRAM_BIT; // Set the flag
SmartWrCmd((SmartState & PAGE2_PR_BIT)? CMD_Rd1 : CMD_Rd0); // Set a page pointer
SmartWrCmd(CMD_DataIn);
SmartWrAdr((long)PageWrAddr<<8);// Write the page address
SMART_DDR = 0xFF; // Set data port as output

_cp = SmartPtr; // Point at write buffer
_cnt = 0; // Set a byte counter
_CLI();
SMART_CTRL &= ~CE_BIT; // Set CE control signal
do { // the page programming
SMART_OUTPUT = *_cp++; // Write a byte
SMART_CTRL &= ~WE_BIT; // Set WE signal
_NOP();
SMART_CTRL |= WE_BIT; // Remove WE signal
}
while (--_cnt);
SMART_CTRL |= CE_BIT; // Remove CE control signal
_SEI();
SmartWrCmd(CMD_PgProgram); // Confirm the program operation
// _cnt = SmartWait(); // Wait for the operation complete
if (SmartState & PAGE2_PR_BIT) // Update the data
{
++PageWrAddr;
}
SmartState = PAGE2_PR_BIT ^ SmartState & ~PROGRAM_BIT; // Reset the flag
return 1; // Return error status
}
//
// ** SmartErBlck -- block erase routine
//
char SmartErBlck(unsigned _blck)
{
char _sts;

_blck <<= 4; // Convert block number to page address
SmartState |= ERASE_BIT; // Set the flag
SmartWrCmd(CMD_BlkErase1); // Send Block erase setup command
_CLI(); // Disable interrupts
SMART_DDR = 0xFF; // Set data port as output
SMART_CTRL &= ~CE_BIT; // Set CE control signal
SMART_CTRL |= ALE_BIT; // Set ALE control signal
SmartWrByte(_blck & 0xFF); // Send an low address byte
SmartWrByte((_blck>>8) & 0xFF); // Send a high address byte
SMART_CTRL &= ~ALE_BIT; // Remove ALE
SMART_CTRL |= CE_BIT; // and CE control signals
SmartWrCmd(CMD_BlkErase2); // Send Erase command
_sts = SmartWait(); // Wait for the operation complete
SmartState &= ~ERASE_BIT; // Reset the flag
return _sts; // Return error status
}
//
// ** SmartCheckBad -- bad block check routines
//
char SmartCheckBad(unsigned _blck)
{
SmartState = CHECK_BIT; // Set the flag
SmartWrCmd(CMD_Rd2); // Spare Read command
SmartWrAdr(((long)_blck<<12)+261); // Read 517th column of the specified block (1st page)
SleepMCU(15); // Delay
SmartState &= ~CHECK_BIT; // Reset the flag
return SmartRdByte() != 0xFF; //
}

//
// ** InitSmart -- the routine initiates SmartMedia Card
//
char InitSmart(void)
{
char _sts;

SmartWrCmd(CMD_Reset); // Do the Reset command
while (!((_sts=SmartRdSts()) & READY_BIT));
return _sts;
}

//
// ** SmartRdID -- Smart ID read function
//
unsigned SmartRdID(void)
{
SmartWrCmd(CMD_RdId); // Send a Read Ident command
_CLI(); // Disable interrupts
SMART_DDR = 0xFF; // Set data port as output
SMART_CTRL |= ALE_BIT; // Set ALE control signal
SMART_CTRL &= ~CE_BIT; // Set CE control signal
SmartWrByte(0x00); // Send a zero address byte
SMART_CTRL |= CE_BIT; // Remove CE control signal
SMART_CTRL &= ~ALE_BIT; // Remove ALE signal
return (SmartRdByte()<<8) | SmartRdByte();
}


Составить ответ  |||  Конференция  |||  Архив

Ответы



Перейти к списку ответов  |||  Конференция  |||  Архив  |||  Главная страница  |||  Содержание  |||  Без кадра

E-mail: info@telesys.ru