cc2538: Add support for Coffee

Coffee is placed by default at the beginning of the flash memory, right
before the firmware. This avoids the memory gaps that there could be
before and after Coffee if it were placed after the firmware, because it
is unlikely that the end of the firmware is aligned with a flash page
boundary, and the CCA is not flash-page-aligned. Thanks to that, Coffee
is also always in the same flash area if its size remains unchanged,
even if the firmware changes, which makes it possible to keep the Coffee
files when reprogramming the firmware after a partial flash erase
command.

The default configuration of Coffee is set to use sensible values for a
typical usage on this SoC, i.e. for sensor data logging.

The default size of Coffee is set to 0 in order not to waste flash if
Coffee is unused.

COFFEE_CONF_CUSTOM_PORT can be defined to a header file to be used with
"#include" in order to override the default CC2538 port of Coffee. This
makes it possible to use Coffee with an external memory device rather
than with the internal flash memory, without having to alter the Contiki
files.

Signed-off-by: Benoît Thébaudeau <benoit.thebaudeau.dev@gmail.com>
ico
Benoît Thébaudeau 2015-07-12 21:48:18 +02:00
parent cbcf1262d6
commit 5d98cb71e2
6 changed files with 332 additions and 3 deletions

View File

@ -60,6 +60,7 @@ CONTIKI_CPU_SOURCEFILES += ecc-curve.c
CONTIKI_CPU_SOURCEFILES += dbg.c ieee-addr.c
CONTIKI_CPU_SOURCEFILES += slip-arch.c slip.c
CONTIKI_CPU_SOURCEFILES += i2c.c cc2538-temp-sensor.c vdd3-sensor.c
CONTIKI_CPU_SOURCEFILES += cfs-coffee.c cfs-coffee-arch.c
DEBUG_IO_SOURCEFILES += dbg-printf.c dbg-snprintf.c dbg-sprintf.c strformat.c
@ -111,7 +112,7 @@ CUSTOM_RULE_LINK=1
LDGENFLAGS += $(addprefix -D,$(subst $(COMMA), ,$(DEFINES)))
LDGENFLAGS += $(addprefix -I,$(SOURCEDIRS))
LDGENFLAGS += -imacros "contiki-conf.h" -imacros "dev/cc2538-dev.h"
LDGENFLAGS += -imacros "dev/flash.h"
LDGENFLAGS += -imacros "dev/flash.h" -imacros "cfs-coffee-arch.h"
LDGENFLAGS += -x c -P -E
# NB: Assumes LDSCRIPT was not overridden and is in $(OBJECTDIR)

View File

@ -35,8 +35,8 @@
*/
MEMORY
{
FLASH_FW (rx) : ORIGIN = CC2538_DEV_FLASH_ADDR,
LENGTH = CC2538_DEV_FLASH_SIZE - FLASH_CCA_SIZE
FLASH_FW (rx) : ORIGIN = COFFEE_START + COFFEE_SIZE,
LENGTH = FLASH_CCA_ADDR - (COFFEE_START + COFFEE_SIZE)
FLASH_CCA (RX) : ORIGIN = FLASH_CCA_ADDR, LENGTH = FLASH_CCA_SIZE
/*

View File

@ -0,0 +1,144 @@
/*
* Copyright (c) 2013, ADVANSEE - http://www.advansee.com/
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS 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 cc2538-cfs-coffee-arch
* @{
*
* \file
* Module for the cc2538 Coffee port
*/
#include "contiki-conf.h"
#include "sys/cc.h"
#include "cfs/cfs-coffee.h"
#include "dev/cc2538-dev.h"
#include "dev/rom-util.h"
#include "dev/flash.h"
#include "dev/watchdog.h"
#include "cpu.h"
#include "cfs-coffee-arch.h"
#include <stdint.h>
#ifndef COFFEE_CONF_CUSTOM_PORT
/*---------------------------------------------------------------------------*/
#if !COFFEE_SECTOR_SIZE || COFFEE_SECTOR_SIZE % FLASH_PAGE_SIZE
#error COFFEE_SECTOR_SIZE must be a non-zero multiple of the flash page size
#endif
#if !COFFEE_PAGE_SIZE || COFFEE_SECTOR_SIZE % COFFEE_PAGE_SIZE
#error COFFEE_PAGE_SIZE must be a divisor of COFFEE_SECTOR_SIZE
#endif
#if COFFEE_PAGE_SIZE % FLASH_WORD_SIZE
#error COFFEE_PAGE_SIZE must be a multiple of the flash word size
#endif
#if COFFEE_START % FLASH_PAGE_SIZE
#error COFFEE_START must be aligned with a flash page boundary
#endif
#if COFFEE_SIZE % COFFEE_SECTOR_SIZE
#error COFFEE_SIZE must be a multiple of COFFEE_SECTOR_SIZE
#endif
#if COFFEE_SIZE / COFFEE_PAGE_SIZE > INT16_MAX
#error Too many Coffee pages for coffee_page_t
#endif
#if COFFEE_START < CC2538_DEV_FLASH_ADDR || \
COFFEE_START + COFFEE_SIZE > FLASH_CCA_ADDR
#error Coffee does not fit in flash
#endif
/*---------------------------------------------------------------------------*/
void
cfs_coffee_arch_erase(uint16_t sector)
{
watchdog_periodic();
INTERRUPTS_DISABLE();
rom_util_page_erase(COFFEE_START + sector * COFFEE_SECTOR_SIZE,
COFFEE_SECTOR_SIZE);
INTERRUPTS_ENABLE();
}
/*---------------------------------------------------------------------------*/
void
cfs_coffee_arch_write(const void *buf, unsigned int size, cfs_offset_t offset)
{
const uint32_t *src = buf;
uint32_t flash_addr = COFFEE_START + offset;
unsigned int align;
uint32_t word, len;
uint32_t page_buf[COFFEE_PAGE_SIZE / FLASH_WORD_SIZE];
unsigned int i;
if(size && (align = flash_addr & (FLASH_WORD_SIZE - 1))) {
len = MIN(FLASH_WORD_SIZE - align, size);
word = ~((*src & ((1 << (len << 3)) - 1)) << (align << 3));
watchdog_periodic();
INTERRUPTS_DISABLE();
rom_util_program_flash(&word, flash_addr & ~(FLASH_WORD_SIZE - 1),
FLASH_WORD_SIZE);
INTERRUPTS_ENABLE();
*(const uint8_t **)&src += len;
size -= len;
flash_addr += len;
}
while(size >= FLASH_WORD_SIZE) {
len = MIN(size & ~(FLASH_WORD_SIZE - 1), COFFEE_PAGE_SIZE);
for(i = 0; i < len / FLASH_WORD_SIZE; i++) {
page_buf[i] = ~*src++;
}
watchdog_periodic();
INTERRUPTS_DISABLE();
rom_util_program_flash(page_buf, flash_addr, len);
INTERRUPTS_ENABLE();
size -= len;
flash_addr += len;
}
if(size) {
word = ~(*src & ((1 << (size << 3)) - 1));
watchdog_periodic();
INTERRUPTS_DISABLE();
rom_util_program_flash(&word, flash_addr, FLASH_WORD_SIZE);
INTERRUPTS_ENABLE();
}
}
/*---------------------------------------------------------------------------*/
void
cfs_coffee_arch_read(void *buf, unsigned int size, cfs_offset_t offset)
{
const uint8_t *src;
uint8_t *dst;
watchdog_periodic();
for(src = (const void *)(COFFEE_START + offset), dst = buf; size; size--) {
*dst++ = ~*src++;
}
}
#endif /* COFFEE_CONF_CUSTOM_PORT */
/** @} */

View File

@ -0,0 +1,182 @@
/*
* Copyright (c) 2013, ADVANSEE - http://www.advansee.com/
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS 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 cc2538
* @{
*
* \defgroup cc2538-cfs-coffee-arch cc2538 Coffee port module
*
* Module for the cc2538 Coffee port
* @{
*
* \file
* Header file for the cc2538 Coffee port module
*/
#ifndef CFS_COFFEE_ARCH_H_
#define CFS_COFFEE_ARCH_H_
#include "contiki-conf.h"
#include "cfs/cfs-coffee.h"
#include "dev/cc2538-dev.h"
#include "dev/flash.h"
#include <stdint.h>
#ifdef COFFEE_CONF_CUSTOM_PORT
#include COFFEE_CONF_CUSTOM_PORT
#else
/*---------------------------------------------------------------------------*/
/** \name Coffee port constants
* @{
*/
/** Logical sector size */
#ifdef COFFEE_CONF_SECTOR_SIZE
#define COFFEE_SECTOR_SIZE COFFEE_CONF_SECTOR_SIZE
#else
#define COFFEE_SECTOR_SIZE FLASH_PAGE_SIZE
#endif
/** Logical page size */
#ifdef COFFEE_CONF_PAGE_SIZE
#define COFFEE_PAGE_SIZE COFFEE_CONF_PAGE_SIZE
#else
#define COFFEE_PAGE_SIZE (COFFEE_SECTOR_SIZE / 8)
#endif
/** Start offset of the file system */
#ifdef COFFEE_CONF_START
#define COFFEE_START COFFEE_CONF_START
#else
#define COFFEE_START CC2538_DEV_FLASH_ADDR
#endif
/** Total size in bytes of the file system */
#ifdef COFFEE_CONF_SIZE
#define COFFEE_SIZE COFFEE_CONF_SIZE
#else
#define COFFEE_SIZE 0
#endif
/** Maximal filename length */
#ifdef COFFEE_CONF_NAME_LENGTH
#define COFFEE_NAME_LENGTH COFFEE_CONF_NAME_LENGTH
#else
#define COFFEE_NAME_LENGTH 40
#endif
/** Number of file cache entries */
#ifdef COFFEE_CONF_MAX_OPEN_FILES
#define COFFEE_MAX_OPEN_FILES COFFEE_CONF_MAX_OPEN_FILES
#else
#define COFFEE_MAX_OPEN_FILES 5
#endif
/** Number of file descriptor entries */
#ifdef COFFEE_CONF_FD_SET_SIZE
#define COFFEE_FD_SET_SIZE COFFEE_CONF_FD_SET_SIZE
#else
#define COFFEE_FD_SET_SIZE 5
#endif
/** Maximal amount of log table entries read in one batch */
#ifdef COFFEE_CONF_LOG_TABLE_LIMIT
#define COFFEE_LOG_TABLE_LIMIT COFFEE_CONF_LOG_TABLE_LIMIT
#endif
/** Default reserved file size */
#ifdef COFFEE_CONF_DYN_SIZE
#define COFFEE_DYN_SIZE COFFEE_CONF_DYN_SIZE
#else
#define COFFEE_DYN_SIZE (COFFEE_SECTOR_SIZE - 50)
#endif
/** Default micro-log size */
#ifdef COFFEE_CONF_LOG_SIZE
#define COFFEE_LOG_SIZE COFFEE_CONF_LOG_SIZE
#endif
/** Whether Coffee will use micro logs */
#ifdef COFFEE_CONF_MICRO_LOGS
#define COFFEE_MICRO_LOGS COFFEE_CONF_MICRO_LOGS
#else
#define COFFEE_MICRO_LOGS 0
#endif
/** Whether files are expected to be appended to only */
#ifdef COFFEE_CONF_APPEND_ONLY
#define COFFEE_APPEND_ONLY COFFEE_CONF_APPEND_ONLY
#else
#define COFFEE_APPEND_ONLY 1
#endif
/** @} */
/*---------------------------------------------------------------------------*/
/** \name Coffee port macros
* @{
*/
/** Erase */
#define COFFEE_ERASE(sector) \
cfs_coffee_arch_erase(sector)
/** Write */
#define COFFEE_WRITE(buf, size, offset) \
cfs_coffee_arch_write((buf), (size), (offset))
/** Read */
#define COFFEE_READ(buf, size, offset) \
cfs_coffee_arch_read((buf), (size), (offset))
/** @} */
/*---------------------------------------------------------------------------*/
/** \name Coffee port types
* @{
*/
typedef int16_t coffee_page_t; /**< Page */
/** @} */
/*---------------------------------------------------------------------------*/
/** \name Coffee port functions
* @{
*/
/** \brief Erases a device sector
* \param sector Sector to erase
*/
void cfs_coffee_arch_erase(uint16_t sector);
/** \brief Writes a buffer to the device
* \param buf Pointer to the buffer
* \param size Byte size of the buffer
* \param offset Device offset to write to
*/
void cfs_coffee_arch_write(const void *buf, unsigned int size,
cfs_offset_t offset);
/** \brief Reads from the device to a buffer
* \param buf Pointer to the buffer
* \param size Byte size of the buffer
* \param offset Device offset to read from
*/
void cfs_coffee_arch_read(void *buf, unsigned int size, cfs_offset_t offset);
/** @} */
#endif /* COFFEE_CONF_CUSTOM_PORT */
#endif /* CFS_COFFEE_ARCH_H_ */
/**
* @}
* @}
*/

View File

@ -31,6 +31,7 @@ In terms of hardware support, the following drivers have been implemented:
* ADC
* Cryptoprocessor (AES-CCM-256, SHA-256)
* Public Key Accelerator (ECDH, ECDSA)
* Flash-based port of Coffee
* SmartRF06 EB and BB peripherals
* LEDs
* Buttons

View File

@ -46,6 +46,7 @@ In terms of hardware support, the following drivers have been implemented:
* ADC
* Cryptoprocessor (AES-CCM-256, SHA-256)
* Public Key Accelerator (ECDH, ECDSA)
* Flash-based port of Coffee
* LEDs
* Buttons
* Internal/external 2.4GHz antenna switch controllable by SW.