aboutsummaryrefslogtreecommitdiffstats
path: root/firmware/atmel_softpack_libraries/libchip_sam3s/include/hsmci.h
blob: 940ed2b6ef4ccda28b48b3261cf54a0a2403ef66 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/* ----------------------------------------------------------------------------
 *         ATMEL Microcontroller Software Support
 * ----------------------------------------------------------------------------
 * Copyright (c) 2008, Atmel Corporation
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * - Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the disclaimer below.
 *
 * Atmel's name may not be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
 * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * ----------------------------------------------------------------------------
 */

/** \addtogroup hsmci_module Working with HSMCI
 * The HSMCI driver provides the interface to configure and use the HSMCI
 * peripheral.
 *
 * The user needs to set the number of wait states depending on the frequency used.\n
 * Configure number of cycles for flash read/write operations in the FWS field of HSMCI_FMR.
 *
 * It offers a function to send flash command to HSMCI and waits for the
 * flash to be ready.
 *
 * To send flash command, the user could do in either of following way:
 * <ul>
 * <li>Write a correct key, command and argument in HSMCI_FCR. </li>
 * <li>Or, Use IAP (In Application Programming) function which is executed from
 * ROM directly, this allows flash programming to be done by code running in flash.</li>
 * <li>Once the command is achieved, it can be detected even by polling EEFC_FSR or interrupt.
 * </ul>
 *
 * The command argument could be a page number,GPNVM number or nothing, it depends on
 * the command itself. Some useful functions in this driver could help user tranlate physical
 * flash address into a page number and vice verse.
 *
 * For more accurate information, please look at the EEFC section of the
 * Datasheet.
 *
 * Related files :\n
 * \ref hsmci_pdc.c\n
 * \ref hsmci.h.\n
*/
/*@{*/
/*@}*/

/**
 *  \file
 */

/**
 *
 * \section Purpose
 *
 * Implement MultiMediaCard(MCI) Interface Driver
 *
 * \section Usage
 *
 * -# MCI_Init(): Initializes a MCI driver instance and the underlying
 *                peripheral.
  * -# MCI_Handler() : Interrupt handler which is called by ISR handler.
 * -# MCI_SetSpeed() : Configure the  MCI CLKDIV in the _MR register
 *                     (\ref Hsmci::HSMCI_MR).
 * -# MCI_SetBusWidth() : Configure the  MCI SDCBUS in the _SDCR register
 *                        (\ref Hsmci::HSMCI_SDCR).
 * -# MCI_EnableHsMode() : Configure the MCI HSMODE in the _CFG register
 *                         (\ref Hsmci::HSMCI_CFG).
 */

#ifndef HSMCID_H
#define HSMCID_H
/** \addtogroup sdmmc_hal
 *@{
 */

/*----------------------------------------------------------------------------
 *         Headers
 *----------------------------------------------------------------------------*/

#include "chip.h"

#include <stdint.h>
#include <stdio.h>

//#include <memories/sdmmc/sdmmc_cmd.h>

/*----------------------------------------------------------------------------
 *         Constants
 *----------------------------------------------------------------------------*/

/* Transfer type */

/** MultiMedia Transfer type: no data */
#define MCI_NO_TRANSFER     0
/** MultiMedia Transfer type: Device to Host (read) */
#define MCI_START_READ      1
/** MultiMedia Transfer type: Host to Device (write) & check BUSY */
#define MCI_START_WRITE     2
/** Device to Host (read) without command */
#define MCI_READ            3
/** Host to Device (write) without command & check BUSY */
#define MCI_WRITE           4
/** MultiMedia Transfer type: STOP transfer */
#define MCI_STOP_TRANSFER   5

/** MCI Initialize clock 400K Hz */
#define MCI_INITIAL_SPEED   400000

/*----------------------------------------------------------------------------
 *         Types
 *----------------------------------------------------------------------------*/

#ifdef __cplusplus
 extern "C" {
#endif

/**
 * \brief MCI Transfer Request prepared by the application upper layer.
 *
 * This structure is sent to the Sdmmc_SendCommand function to start the transfer.
 * At the end of the transfer, the callback is invoked.
 */
typedef struct _MciCmd {

    /** Command code. */
    uint32_t cmd;
    /** Command argument. */
    uint32_t arg;
    /** Data buffer, with MCI_DMA_ENABLE defined 1, the buffer can be
     * 1, 2 or 4 bytes aligned. It has to be 4 byte aligned if no DMA.
     */
    uint8_t *pData;
    /** Size of data block in bytes. */
    uint16_t blockSize;
    /** Number of blocks to be transfered */
    uint16_t nbBlock;
    /** Response buffer. */
    uint32_t  *pResp;
    /** Optional user-provided callback function. */
    void (*callback)( uint8_t status, void *pArg ) ;
    /** Optional argument to the callback function. */
    void *pArg;
    /** SD card command option. */
    uint8_t resType:7,      /** Response */
            busyCheck:1;    /** Check busy as end of command */
    /** Indicate transfer type */
    uint8_t tranType;

    /** Indicate end of transfer status */
    uint8_t status;

    /** Command state. */
    volatile uint8_t state;
} MciCmd;

/**
 *  \brief MCI Driver
 */
typedef struct
{
    /** Pointer to a MCI peripheral. */
    Hsmci *pMciHw;
    /** Pointer to currently executing command. */
    MciCmd *pCommand;
    /** MCI peripheral identifier. */
    uint8_t mciId;
    /** Mutex. */
    volatile uint8_t semaphore;
} Mcid;

/*----------------------------------------------------------------------------
 *         Exported functions
 *----------------------------------------------------------------------------*/

extern void MCI_Disable(Hsmci *pMciHw);
extern void MCI_Enable(Hsmci *pMciHw);
extern uint8_t MCI_EnableHsMode(Mcid * pMci, uint8_t hsEnable);
extern void MCI_Init( Mcid  *pMci, Hsmci *pMciHw, uint8_t mciId, uint32_t dwMCk ) ;
extern uint32_t MCI_SetBusWidth(Mcid *pMci, uint32_t busWidth);
extern uint32_t MCI_SetSpeed(Mcid *pMci, uint32_t mciSpeed, uint32_t mck);

/* pdc if used */
extern void MCI_Reset(Mcid *pMci, uint8_t keepSettings);

#ifdef __cplusplus
}
#endif

/**@}*/
#endif //#ifndef HSMCID_H