3      Copyright (C) Dean Camera, 2009. 
   5   dean [at] fourwalledcubicle [dot] com 
   6       www.fourwalledcubicle.com 
  10   Copyright 2009  Dean Camera (dean [at] fourwalledcubicle [dot] com) 
  12   Permission to use, copy, modify, and distribute this software 
  13   and its documentation for any purpose and without fee is hereby 
  14   granted, provided that the above copyright notice appear in all 
  15   copies and that both that the copyright notice and this 
  16   permission notice and warranty disclaimer appear in supporting 
  17   documentation, and that the name of the author not be used in 
  18   advertising or publicity pertaining to distribution of the 
  19   software without specific, written prior permission. 
  21   The author disclaim all warranties with regard to this 
  22   software, including all implied warranties of merchantability 
  23   and fitness.  In no event shall the author be liable for any 
  24   special, indirect or consequential damages or any damages 
  25   whatsoever resulting from loss of use, data or profits, whether 
  26   in an action of contract, negligence or other tortious action, 
  27   arising out of or in connection with the use or performance of 
  33  *  This file is the master dispatch header file for the board-specific dataflash driver, for boards containing 
  34  *  dataflash ICs for external non-volatile storage. 
  36  *  User code should include this file, which will in turn include the correct dataflash driver header file for 
  37  *  the currently selected board. 
  39  *  If the BOARD value is set to BOARD_USER, this will include the /Board/Dataflash.h file in the user project 
  43 #ifndef __DATAFLASH_H__ 
  44 #define __DATAFLASH_H__ 
  47         #if !defined(__DOXYGEN__) 
  48                 #define INCLUDE_FROM_DATAFLASH_H 
  49                 #define INCLUDE_FROM_BOARD_DRIVER 
  53         #include "../AT90USBXXX/SPI.h" 
  54         #include "../../Common/Common.h" 
  56         /* Enable C linkage for C++ Compilers: */ 
  57                 #if defined(__cplusplus) 
  61         /* Public Interface - May be used in end-application: */ 
  63                         /** Returns the mask of the currently selected Dataflash chip, either DATAFLASH_NO_CHIP or a 
  64                          *  DATAFLASH_CHIPn mask (where n is the chip number). 
  66                         #define Dataflash_GetSelectedChip()          (DATAFLASH_CHIPCS_PORT & DATAFLASH_CHIPCS_MASK) 
  68                         /** Selects the dataflash chip given as a chip mask, in the form of DATAFLASH_CHIPn (where n 
  69                          *  is the chip number). 
  71                         #define Dataflash_SelectChip(mask)   MACROS{ DATAFLASH_CHIPCS_PORT = ((DATAFLASH_CHIPCS_PORT \ 
  72                                                                      & ~DATAFLASH_CHIPCS_MASK) | mask);              }MACROE 
  74                         /** Deselects the current dataflash chip, so that no dataflash is selected. */ 
  75                         #define Dataflash_DeselectChip()             Dataflash_SelectChip(DATAFLASH_NO_CHIP) 
  77                 /* Inline Functions: */ 
  78                         /** Sends a byte to the currently selected dataflash IC, and returns a byte from the dataflash. 
  80                          *  \param Byte of data to send to the dataflash 
  82                          *  \return Last response byte from the dataflash 
  84                         static inline uint8_t Dataflash_TransferByte(const uint8_t Byte
) ATTR_ALWAYS_INLINE
; 
  85                         static inline uint8_t Dataflash_TransferByte(const uint8_t Byte
) 
  87                                 return SPI_TransferByte(Byte
); 
  90                         /** Sends a byte to the currently selected dataflash IC, and ignores the next byte from the dataflash. 
  92                          *  \param Byte of data to send to the dataflash 
  94                         static inline void Dataflash_SendByte(const uint8_t Byte
) ATTR_ALWAYS_INLINE
; 
  95                         static inline void Dataflash_SendByte(const uint8_t Byte
) 
 100                         /** Sends a dummy byte to the currently selected dataflash IC, and returns the next byte from the dataflash. 
 102                          *  \return Last response byte from the dataflash 
 104                         static inline uint8_t Dataflash_ReceiveByte(void) ATTR_ALWAYS_INLINE ATTR_WARN_UNUSED_RESULT
; 
 105                         static inline uint8_t Dataflash_ReceiveByte(void) 
 107                                 return SPI_ReceiveByte(); 
 112                                 #error BOARD must be set in makefile to a value specified in BoardTypes.h. 
 113                         #elif (BOARD == BOARD_USBKEY) 
 114                                 #include "USBKEY/Dataflash.h" 
 115                         #elif (BOARD == BOARD_STK525) 
 116                                 #include "STK525/Dataflash.h" 
 117                         #elif (BOARD == BOARD_STK526) 
 118                                 #include "STK526/Dataflash.h" 
 119                         #elif (BOARD == BOARD_USER) 
 120                                 #include "Board/Dataflash.h" 
 122                                 #error The selected board does not contain a dataflash IC. 
 125                 /* Inline Functions: */ 
 126                         /** Initializes the dataflash driver (including the SPI driver) so that commands and data may be 
 127                          *  sent to an attached dataflash IC. 
 129                          *  \param PrescalerMask  SPI prescaler mask, see SPI.h documentation 
 131                         static inline void Dataflash_Init(const uint8_t PrescalerMask
) 
 133                                 DATAFLASH_CHIPCS_DDR  
|= DATAFLASH_CHIPCS_MASK
; 
 134                                 DATAFLASH_CHIPCS_PORT 
|= DATAFLASH_CHIPCS_MASK
; 
 136                                 SPI_Init(PrescalerMask
, true); 
 139                         /** Toggles the select line of the currently selected dataflash IC, so that it is ready to receive 
 142                         static inline void Dataflash_ToggleSelectedChipCS(void) 
 144                                 uint8_t SelectedChipMask 
= Dataflash_GetSelectedChip(); 
 146                                 Dataflash_DeselectChip(); 
 147                                 Dataflash_SelectChip(SelectedChipMask
); 
 150                         /** Spinloops while the currently selected dataflash is busy executing a command, such as a main 
 151                          *  memory page program or main memory to buffer transfer. 
 153                         static inline void Dataflash_WaitWhileBusy(void) 
 155                                 Dataflash_ToggleSelectedChipCS(); 
 156                                 Dataflash_SendByte(DF_CMD_GETSTATUS
); 
 157                                 while (!(Dataflash_ReceiveByte() & DF_STATUS_READY
)); 
 160                         /** Selects a dataflash IC from the given page number, which should range from 0 to 
 161                          *  ((DATAFLASH_PAGES * DATAFLASH_TOTALCHIPS) - 1). For boards containing only one 
 162                          *  dataflash IC, this will select DATAFLASH_CHIP1. If the given page number is outside 
 163                          *  the total number of pages contained in the boards dataflash ICs, all dataflash ICs 
 166                          *  \param PageAddress  Address of the page to manipulate, ranging from 
 167                          *                      ((DATAFLASH_PAGES * DATAFLASH_TOTALCHIPS) - 1). 
 169                         static inline void Dataflash_SelectChipFromPage(const uint16_t PageAddress
); 
 171                         /** Sends a set of page and buffer address bytes to the currently selected dataflash IC, for use with 
 172                          *  dataflash commands which require a complete 24-byte address. 
 174                          *  \param PageAddress  Page address within the selected dataflash IC 
 175                          *  \param BufferByte   Address within the dataflash's buffer 
 177                         static inline void Dataflash_SendAddressBytes(uint16_t PageAddress
, const uint16_t BufferByte
); 
 179         /* Disable C linkage for C++ Compilers: */ 
 180                 #if defined(__cplusplus)