From 566ebcdcc810172118dd0519225ad65846ea3cde Mon Sep 17 00:00:00 2001 From: Nélio Laranjeiro Date: Sat, 1 Mar 2008 18:51:45 +0100 Subject: Update the spi and flash module. testing in progress... --- digital/avr/modules/flash/avrconfig.h | 37 +++++++++++++++++ digital/avr/modules/flash/flash.c | 64 ++++++++++++++++++----------- digital/avr/modules/flash/flash.h | 24 +++++++++-- digital/avr/modules/flash/test/Makefile | 2 +- digital/avr/modules/flash/test/avrconfig.h | 17 ++++++-- digital/avr/modules/flash/test/test-flash.c | 10 ++--- digital/avr/modules/spi/spi.c | 53 +++++++++++++++++++----- digital/avr/modules/spi/spi.h | 36 ++++++++++++---- digital/avr/modules/spi/test/Makefile | 2 +- digital/avr/modules/spi/test/test_spi.c | 23 ++++++++--- 10 files changed, 206 insertions(+), 62 deletions(-) create mode 100644 digital/avr/modules/flash/avrconfig.h (limited to 'digital') diff --git a/digital/avr/modules/flash/avrconfig.h b/digital/avr/modules/flash/avrconfig.h new file mode 100644 index 00000000..56d64ced --- /dev/null +++ b/digital/avr/modules/flash/avrconfig.h @@ -0,0 +1,37 @@ +#ifndef avrconfig_h +#define avrconfig_h +/* avrconfig.h */ +/* avr.flash - Flash SPI AVR module. {{{ + * + * Copyright (C) 2008 Nélio Laranjeiro + * + * APBTeam: + * Web: http://apbteam.org/ + * Email: team AT apbteam DOT org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * }}} */ + +/* flash - Flash SPI AVR module. */ + +/** Flash PORT used. */ +#define AC_FLASH_PORT PORTX +/** Flash DDR used. */ +#define AC_FLASH_DDR DDRX +/** Flash SS pin. */ +#define AC_FLASH_BIT_SS N + +#endif /* avrconfig_h */ diff --git a/digital/avr/modules/flash/flash.c b/digital/avr/modules/flash/flash.c index f781097d..e964728b 100644 --- a/digital/avr/modules/flash/flash.c +++ b/digital/avr/modules/flash/flash.c @@ -65,47 +65,72 @@ flash_erase (uint8_t cmd, uint32_t start_addr) void flash_init (void) { - uint8_t rsp; + uint8_t rsp[3]; flash_global.addr = 0x0; /* send the read-ID instruction. */ - spi_send (0x90); - rsp = flash_read (0x0); + AC_FLASH_PORT |= _BV(AC_FLASH_BIT_SS); + AC_FLASH_DDR |= _BV(AC_FLASH_BIT_SS); - if (rsp == 0xBF) - proto_send1b (0x0 ,0x1); + spi_init (SPI_IT_DISABLE | SPI_ENABLE | SPI_MASTER | SPI_MSB_FIRST | + SPI_CPOL_FALLING | SPI_CPHA_SETUP | SPI_FOSC_DIV16); + + AC_FLASH_PORT &= ~_BV(AC_FLASH_BIT_SS); + spi_send (FLASH_READ_ID); + rsp[0] = spi_recv (); + rsp[1] = spi_recv (); + rsp[2] = spi_recv (); + AC_FLASH_PORT |= _BV(AC_FLASH_BIT_SS); + + proto_send3b ('f',rsp[0], rsp[1], rsp[2]); + + /* Search for the next address to start writting. */ + /*for (flash_global.addr = 0, rsp = 0xFF; rsp != 0xFF; flash_global.addr += + FLASH_PAGE_SIZE - 1) + { + rsp = flash_read (); + } + */ } /** Write in the flash byte provided in parameter. - * \param addr the address to store the data. * \param data the buffer to store the data. */ void -flash_write (uint32_t addr, uint8_t data) +flash_write (uint8_t data) { spi_init (SPI_IT_DISABLE | SPI_ENABLE | SPI_MSB_FIRST | SPI_MASTER | SPI_CPOL_RISING | SPI_CPHA_SAMPLE | SPI_FOSC_DIV2); /* Write instruction. */ - spi_send (0x2); - flash_address (addr); + spi_send (FLASH_WRITE); + flash_address (flash_global.addr); spi_send (data); + /* increment the address and verify if the address is in the range of the + * flash memory */ + FLASH_ADDRESS_INC_MASK(flash_global.addr); } /** Read the data at the address provided. - * \param addr the address of the data to read. * \return the data read. */ uint8_t -flash_read (uint32_t addr) +flash_read (void) { + uint8_t data; + spi_init (SPI_IT_DISABLE | SPI_ENABLE | SPI_MSB_FIRST | SPI_MASTER | SPI_CPOL_RISING | SPI_CPHA_SAMPLE | SPI_FOSC_DIV2); /* Send the read instruction. */ - spi_send (0x3); - flash_address (addr); - return spi_recv (); + spi_send (FLASH_READ); + flash_address (flash_global.addr); + data = spi_recv (); + + /* increment the address and verify if the address is in the range of the + * flash memory */ + FLASH_ADDRESS_INC_MASK(flash_global.addr); + return data; } /** Read a data from the flash memory from the address provided and for a @@ -131,16 +156,7 @@ flash_write_array (uint32_t addr, uint8_t *data, uint32_t length); * \return data read from the memory. */ uint8_t -flash_read_managed (uint32_t offset) -{ - if ((int32_t) flash_global.addr - (int32_t) offset < 0) - { - // Shall not happen. - // TODO Assert this. - } - - return flash_read (flash_global.addr - offset); -} +flash_read_managed (uint32_t offset); /** Read an array of data from the memory starting at the address less the * offset provided in parameters. The data read will be stored in the buffer diff --git a/digital/avr/modules/flash/flash.h b/digital/avr/modules/flash/flash.h index 73c582d4..cc12786b 100644 --- a/digital/avr/modules/flash/flash.h +++ b/digital/avr/modules/flash/flash.h @@ -27,11 +27,22 @@ #include "common.h" #include "io.h" +#define FLASH_HIGH_ADDRESS 0x1FFFFF +#define FLASH_ADDRESS_INC_MASK(val) (val = (val+1) & FLASH_HIGH_ADDRESS) + +#define FLASH_PAGE_SIZE 0x1000 +#define FLASH_PAGE_MASK (0xFFF) +#define FLASH_PAGE(val) (val & FLASH_PAGE_MASK) + #define FLASH_ERASE_FULL 0x60 #define FLASH_ERASE_4K 0x20 #define FLASH_ERASE_32K 0x52 #define FLASH_ERASE_64K 0xD8 +#define FLASH_READ_ID 0x9F +#define FLASH_READ 0x03 +#define FLASH_WRITE 0x2 + struct flash_t { /** Current Address in the flash memory. */ @@ -52,6 +63,14 @@ flash_init (void); void flash_address (uint32_t addr); +/** Flash init page. + * Initialise the page by write an value different of 0xFF to indicate that + * the page is use. + * \param addr + */ +void +flash_init_page (uint32_t addr); + /** Erase the memory. * \param erase_type the erase type.. * \param start_addr the start address. @@ -60,18 +79,17 @@ void flash_erase (uint8_t cmd, uint32_t start_addr); /** Write in the flash byte provided in parameter. - * \param addr the address to store the data. * \param data the buffer to store the data. */ void -flash_write (uint32_t addr, uint8_t data); +flash_write (uint8_t data); /** Read the data at the address provided. * \param addr the address of the data to read. * \return the data read. */ uint8_t -flash_read (uint32_t addr); +flash_read (void); /** Read a data from the flash memory from the address provided and for a * length of the number of bytes provided. diff --git a/digital/avr/modules/flash/test/Makefile b/digital/avr/modules/flash/test/Makefile index e8552eb6..9fcc8790 100644 --- a/digital/avr/modules/flash/test/Makefile +++ b/digital/avr/modules/flash/test/Makefile @@ -4,7 +4,7 @@ test_flash_SOURCES = test-flash.c MODULES = utils spi flash proto uart CONFIGFILE = avrconfig.h # atmega8, atmega8535, atmega128... -AVR_MCU = atmega8 +AVR_MCU = atmega128 # -O2 : speed # -Os : size OPTIMIZE = -O2 diff --git a/digital/avr/modules/flash/test/avrconfig.h b/digital/avr/modules/flash/test/avrconfig.h index ac0b3b3a..c4d9f159 100644 --- a/digital/avr/modules/flash/test/avrconfig.h +++ b/digital/avr/modules/flash/test/avrconfig.h @@ -32,16 +32,16 @@ /* uart - UART module. */ /** Select hardware uart for primary uart: 0, 1 or -1 to disable. */ -#define AC_UART0_PORT 0 +#define AC_UART0_PORT 1 /** Baudrate: 2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600, 76800, * 115200, 230400, 250000, 500000, 1000000. */ -#define AC_UART0_BAUDRATE 115200 +#define AC_UART0_BAUDRATE 38400 /** Send mode: * - POLLING: no interrupts. * - RING: interrupts, ring buffer. */ -#define AC_UART0_SEND_MODE RING +#define AC_UART0_SEND_MODE POLLING /** Recv mode, same as send mode. */ -#define AC_UART0_RECV_MODE RING +#define AC_UART0_RECV_MODE POLLING /** Character size: 5, 6, 7, 8, 9 (only 8 implemented). */ #define AC_UART0_CHAR_SIZE 8 /** Parity : ODD, EVEN, NONE. */ @@ -83,4 +83,13 @@ /** Support for quote parameter. */ #define AC_PROTO_QUOTE 1 +/* flash - Flash SPI AVR module. */ +/** Flash PORT used. */ +#define AC_FLASH_PORT PORTD +/** Flash DDR used. */ +#define AC_FLASH_DDR DDRD +/** Flash SS pin. */ +#define AC_FLASH_BIT_SS 5 + + #endif /* avrconfig_h */ diff --git a/digital/avr/modules/flash/test/test-flash.c b/digital/avr/modules/flash/test/test-flash.c index 5ca564a7..e90db20f 100644 --- a/digital/avr/modules/flash/test/test-flash.c +++ b/digital/avr/modules/flash/test/test-flash.c @@ -25,6 +25,7 @@ #include "common.h" #include "io.h" #include "../flash.h" +#include "modules/proto/proto.h" void proto_callback (uint8_t cmd, uint8_t size, uint8_t *args) @@ -35,12 +36,9 @@ proto_callback (uint8_t cmd, uint8_t size, uint8_t *args) int main (void) { - uint8_t data; - + proto_send0 ('z'); flash_init (); - flash_write (0x0, 0x14); - data = flash_read (0x0); - data = flash_read_managed (1); - return 1; + + while (1); } diff --git a/digital/avr/modules/spi/spi.c b/digital/avr/modules/spi/spi.c index ea8743a8..9039e267 100644 --- a/digital/avr/modules/spi/spi.c +++ b/digital/avr/modules/spi/spi.c @@ -47,8 +47,32 @@ static spi_t spi_global; void spi_init(uint8_t sprc) { + /* Master configuration. */ + if (sprc & _BV(MSTR)) + { + SPI_DDR &= ~_BV(SPI_BIT_MISO); + SPI_PORT &= ~_BV(SPI_BIT_MISO); + + SPI_PORT |= _BV(SPI_BIT_MOSI); + SPI_DDR |= _BV(SPI_BIT_MOSI); + + SPI_PORT |= _BV(SPI_BIT_SCK); + SPI_DDR |= _BV(SPI_BIT_SCK); + } + else + { + SPI_PORT |= _BV(SPI_BIT_MISO); + SPI_DDR |= _BV(SPI_BIT_MISO); + + SPI_DDR &= ~_BV(SPI_BIT_MOSI); + SPI_PORT &= ~_BV(SPI_BIT_MOSI); + + SPI_DDR &= ~_BV(SPI_BIT_SCK); + SPI_PORT &= ~_BV(SPI_BIT_SCK); + } + SPCR = sprc; - spi_global.interruption = sprc >> 7; + spi_global.interruption = SPCR & _BV(SPIE); } /** Send a data to the Slave. @@ -57,16 +81,10 @@ spi_init(uint8_t sprc) void spi_send(uint8_t data) { - // enables the SPI if not enabled. - SPCR |= SPI_ENABLE; + // Wait the end of the transmission. + while(!(SPSR & _BV(SPIF))); SPDR = data; - - if (!spi_global.interruption) - { - // Wait the end of the transmission. - while(!(SPSR & (1< #include + +#include "modules/proto/proto.h" #include "io.h" #include "../spi.h" @@ -33,13 +35,19 @@ spi_interruption_function (void *user_data, uint8_t data) { } +void +proto_callback (uint8_t cmd, uint8_t size, uint8_t *args) +{ + //TODO Still don't know what to to +} + int main (void) { - uint8_t test [10]; - uint8_t res; + //uint8_t test [10]; + //uint8_t res; - res = SPI_IT_ENABLE; + /*res = SPI_IT_ENABLE; spi_init (res); res = SPI_IT_DISABLE; spi_init (res); @@ -63,18 +71,23 @@ main (void) spi_init (res); res = SPI_CPHA_SETUP; spi_init (res); + */ //initialise the spi. spi_init (0x14); - test[0] = 0x2; + proto_send0 (PORTB); + proto_send0 (DDRB); + + + /*test[0] = 0x2; test[1] = 0x3; test[2] = 0x4; spi_send (3); res = spi_recv (); - + */ return 0; } -- cgit v1.2.3