📄 flash_api.c
字号:
/*==================================================================*
*
* Copyright (c) 2002, Motorola Inc.
* Motorola Application Note
*
* File name : flash_api.c
* Author : Mauricio Capistran-Garza
* Department : Guadalajara - SPS
*
* Description : This files contains the API functions definition.
*
* Compiler : HC08CW v3.0
*
* History :
*
*==================================================================*/
#include <stdtypes.h>
#include "flash_api.h"
/********************************************************************
* ReadByte: It reads a byte from the communication port
* and returns it.
*
* Parameters: None.
*
* Entry Conditions: None.
*
* Exit Conditions: None.
*
* Return: The byte received.
*
* Remarks: The function will not exit until a byte
* is received.
*/
Byte ReadByte(void) {
Byte _backup1, _backup2;
Byte _data;
_backup1 = COMMPORT; // Backup port values.
_backup2 = COMMPORT_DIR;
COMMPORT_DIR &= 0xFE; // Configure COMMPORT as input.
COMMPORT &= 0xFE;
GETBYTE(); // Call ROM-resident routine.
__asm sta _data;
COMMPORT = _backup1; // Restore port values.
COMMPORT_DIR = _backup2;
return _data;
}
/********************************************************************
* TransmitByte: It sends a byte out the communication port.
*
* Parameters: _data: the byte to be sent.
*
* Entry Conditions: None.
*
* Exit Conditions: None.
*
* Return: The byte received.
*
* Remarks: The function will not exit until a byte
* is received.
*/
void TransmitByte(Byte _data) {
Byte _backup1, _backup2;
_backup1 = COMMPORT; // Backup port values.
_backup2 = COMMPORT_DIR;
COMMPORT_DIR &= 0xFE; // Configure COMMPORT as input.
COMMPORT &= 0xFE;
__asm LDA _data;
PUT_BYTE(); // Call ROM-resident routine.
COMMPORT = _backup1; // Restore port values.
COMMPORT_DIR = _backup2;
return;
}
/********************************************************************
* TransmitRange: It reads a range of FLASH memory and sends
* it out the communication port
*
* Parameters: _*ini: pointer to the starting address
* of the range.
* _num: number of bytes to transmit.
*
* Entry Conditions: None.
*
* Exit Conditions: The checksum is stored in _ini;
*
* Return: SUCCESS or FAIL
*
* Remarks: _num must be less to or equal to 64
*/
Byte TransmitRange(Word *_ini, Byte _num) {
Byte _backup1, _backup2, _backup3;
Byte _status = 0;
Word _first;
_first = *_ini;
_backup1 = COMMPORT; // Backup port values.
_backup2 = COMMPORT_DIR;
COMMPORT &= 0xFE; // Configure COMMPORT as input.
COMMPORT_DIR &= 0xFE;
// Define Last Address High & Low
LADDRH = ((_first + _num - 1) & 0xFF00) >> 8;
LADDRL = ((_first + _num - 1) & 0x00FF);
__asm ldhx _first; // Define first address.
__asm lda #0x00; // Configure RDVRRNG() to transmit.
RDVRRNG(); // Call ROM-resident routine.
__asm sta _backup3; // Store checksum.
__asm clra;
__asm adc #0;
__asm sta _status; // Store status.
*_ini = _backup3;
COMMPORT = _backup1; // Restore port values.
COMMPORT_DIR = _backup2;
return _status;
}
/********************************************************************
* ProgramRange: Programs a range of FLASH.
*
* Parameters: _*ini: pointer to the starting address
* of the range.
* _num: length of the range.
*
* Entry Conditions: DATA contains the data to be programmed
*
* Exit Conditions: None.
*
* Return: None.
*
* Remarks: _num must be less to or equal to 64
*/
void ProgramRange(Word *_ini, Byte _num) {
Word _first;
_first = *_ini;
FLBPR = 0xFF; // Disables write protection.
CPUSPD = OSC; // Set Clock Bus Operation speed.
// Define Last Address High & Low
LADDRH = ((_first + _num - 1) & 0xFF00) >> 8;
LADDRL = ((_first + _num - 1) & 0x00FF);
__asm ldhx _first; // Define first address.
PRGRNGE(); // Call ROM-resident routine.
FLBPR = 0x00; // Enables write protection.
return;
}
/********************************************************************
* ProgramRangeX: Programs a range of FLASH after verifying
* the range is blank. If it isn't blank it
* doesn't programs and returns FAIL.
*
* Parameters: _*ini: pointer to the starting address
* of the range.
* _num: length of the range.
*
* Entry Conditions: DATA contains the data to be programmed
*
* Exit Conditions: None.
*
* Return: SUCCESS or FAIL.
*
* Remarks: _num must be less to or equal to 64
*/
Byte ProgramRangeX(Word *_ini, Byte _num) {
Byte _test;
Byte _i;
Word _first;
for (_i=0; _i < _num; _i++) {
_test = (*(Byte*)(*_ini + _i));
if (_test != 0xFF) {
return FAIL;
}
}
_first = *_ini;
FLBPR = 0xFF; // Disables write protection.
CPUSPD = OSC; // Set Clock Bus Operation speed.
// Define Last Address High & Low
LADDRH = ((_first + _num - 1) & 0xFF00) >> 8;
LADDRL = ((_first + _num - 1) & 0x00FF);
__asm ldhx _first; // Define first address.
PRGRNGE(); // Call ROM-resident routine.
FLBPR = 0x00; // Enables write protection.
return SUCCESS;
}
/********************************************************************
* VerifyRange: Verifies a range of FLASH against the data
* contained in DATA. It can also be used to
* read a range of FLASH into RAM.
*
* Parameters: _*ini: pointer to the starting address
* of the range.
* _num: length of the range.
*
* Entry Conditions: DATA contains the data to be verified.
*
* Exit Conditions: DATA is overwritten with contents of FLASH.
* The checksum is stored in _ini;
*
* Return: The byte received.
*
* Remarks: _num must be less to or equal to 64
*/
Byte VerifyRange(Word *_ini, Byte _num) {
Byte _backup1;
Byte _status = 0;
Word _first;
_first = *_ini;
// Define Last Address High & Low
LADDRH = ((_first + _num - 1) & 0xFF00) >> 8;
LADDRL = ((_first + _num - 1) & 0x00FF);
__asm ldhx _first; // Define first address.
__asm lda #0x01; // Config RDVRRNG() to store in RAM.
RDVRRNG(); // Call ROM-resident routine.
__asm sta _backup1; // Store checksum.
__asm clra;
__asm adc #0;
__asm sta _status; // Store status.
*_ini = _backup1;
return _status;
}
/********************************************************************
* ErasePage: It erases a PAGE of FLASH
*
* Parameters: *_page: pointer to any address within
* the PAGE to be erased.
*
* Entry Conditions: None.
*
* Exit Conditions: Interrupts are disabled.
*
* Return: None.
*
* Remarks: All bytes within that PAGE will be driven
* to 0xFF
*/
void ErasePage(Word *_page) {
Word _address;
_address = *_page;
FLBPR = 0xFF; // Enables erase/write protection.
CPUSPD = OSC; // Set Clock Bus Operation speed.
CTRLBYT &= 0xBF; // Clear bit 6 to page erase mode.
__asm ldhx _address; // Set the page to be erased.
ERARNGE(); // Call ROM-resident routine.
return;
}
/********************************************************************
* ErasePageX: It erases a PAGE of FLASH but leaves the state
* of the interrupts as it was before calling it.
*
* Parameters: *_page: pointer to any address within
* the PAGE to be erased.
*
* Entry Conditions: None.
*
* Exit Conditions: None.
*
* Return: None.
*
* Remarks: All bytes within that PAGE will be driven
* to 0xFF.
* Interrupts are disabled during the erasing
* of the flash, but are restored to its
* original state before exiting the function.
*/
void ErasePageX (Word *_page) {
Byte _backup1;
Word _address;
__asm tpa;
__asm sta _backup1; // Backup Condition Code Register
_address = *_page;
FLBPR = 0xFF; // Enables erase/write protection.
CPUSPD = OSC; // Set Clock Bus Operation speed.
CTRLBYT &= 0xBF; // Clear bit 6 to page erase mode.
__asm ldhx _address; // Set the page to be erased.
ERARNGE(); // Call ROM-resident routine.
if ((_backup1 & 0x80) != 0x80) { // Restore interrupts state.
__asm CLI;
}
return;
}
/********************************************************************
* EraseFlash: It erases the entire FLASH.
*
* Parameters: None.
*
* Entry Conditions: None.
*
* Exit Conditions: None.
*
* Return: None.
*
* Remarks: All bytes will be driven to 0xFF.
* No code in FLASH will be executed after
* this function has been called.
*/
void EraseFlash(void) {
FLBPR = 0xFF; // Enables erase/write protection.
CPUSPD = OSC; // Set Clock Bus Operation speed.
CTRLBYT |= 0x40; // Set bit 6 to flash erase mode.
ERARNGE(); // Call ROM-resident routine.
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -