source: arduino-1-6-7/trunk/fuentes/arduino-ide-amd64/hardware/arduino/avr/firmwares/wifishield/wifi_dnld/src/nor_flash.c @ 4837

Last change on this file since 4837 was 4837, checked in by daduve, 2 years ago

Adding new version

File size: 3.6 KB
Line 
1/* This source file is part of the ATMEL AVR-UC3-SoftwareFramework-1.7.0 Release */
2
3/*! \page License
4 * Copyright (C) 2009, H&D Wireless AB All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
15 *
16 * 3. The name of H&D Wireless AB may not be used to endorse or promote products derived
17 * from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY H&D WIRELESS AB ``AS IS'' AND ANY EXPRESS OR IMPLIED
20 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND
22 * SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT,
23 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30#include <string.h>
31#include "compiler.h"
32#include "preprocessor.h"
33#include "board.h"
34#include "power_clocks_lib.h"
35#include "gpio.h"
36#include "spi.h"
37#include "conf_at45dbx.h"
38#include "at45dbx.h"
39#include "clocks.h"
40
41
42void flash_init(void)
43{
44        static const gpio_map_t AT45DBX_SPI_GPIO_MAP = {
45                { AT45DBX_SPI_SCK_PIN, AT45DBX_SPI_SCK_FUNCTION },
46                { AT45DBX_SPI_MISO_PIN, AT45DBX_SPI_MISO_FUNCTION },
47                { AT45DBX_SPI_MOSI_PIN, AT45DBX_SPI_MOSI_FUNCTION },
48                { AT45DBX_SPI_NPCS2_PIN, AT45DBX_SPI_NPCS2_FUNCTION },
49        };
50       
51        spi_options_t spiOptions = {
52                .reg          = AT45DBX_SPI_FIRST_NPCS,
53                .baudrate     = AT45DBX_SPI_MASTER_SPEED,
54                .bits         = AT45DBX_SPI_BITS,
55                .spck_delay   = 0,
56                .trans_delay  = 0,
57                .stay_act     = 1,
58                .spi_mode     = 0,
59                .modfdis      = 1
60        };
61   
62        gpio_enable_module(AT45DBX_SPI_GPIO_MAP,
63                           sizeof(AT45DBX_SPI_GPIO_MAP) /
64                           sizeof(AT45DBX_SPI_GPIO_MAP[0]));
65       
66        spi_initMaster(AT45DBX_SPI, &spiOptions);
67
68        spi_selectionMode(AT45DBX_SPI, 0, 0, 0);
69        spi_enable(AT45DBX_SPI);
70
71        /* put up flash reset pin */
72        gpio_set_gpio_pin(AT45DBX_CHIP_RESET);
73
74        at45dbx_init(spiOptions, FPBA_HZ);
75}
76
77void flash_write(U32 addr, const U8* buf, U32 len)
78{
79        U32 sector = addr / AT45DBX_SECTOR_SIZE;
80        U32 i;
81        Assert(addr % AT45DBX_SECTOR_SIZE == 0);
82       
83        at45dbx_write_open(sector);
84        for (i = 0; i < len; i++)
85                at45dbx_write_byte(buf[i]);
86        at45dbx_write_close();
87}
88
89void flash_read(U32 addr, U8* buf, U32 len)
90{
91        U32 sector = addr / AT45DBX_SECTOR_SIZE;
92        U32 i;
93        Assert(addr % AT45DBX_SECTOR_SIZE == 0);
94       
95        at45dbx_read_open(sector);
96        for (i = 0; i < len; i++)
97                buf[i] = at45dbx_read_byte();
98        at45dbx_read_close();
99}
Note: See TracBrowser for help on using the repository browser.