drivers/ecos_lpc2138_sja1000/canOpenDriver.c
author etisserant
Thu, 18 May 2006 21:55:48 +0200
changeset 16 1684806cf7e1
parent 3 d9cf34cd6823
child 18 2fc8aa46980b
permissions -rw-r--r--
Re-orginized led and nvram related code, for generic target.
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
     1
/*
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
     2
This file is part of CanFestival, a library implementing CanOpen Stack.
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
     3
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
     4
 Author: Christian Fortin (canfestival@canopencanada.ca)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
     5
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
     6
See COPYING file for copyrights details.
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
     7
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
     8
This library is free software; you can redistribute it and/or
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
     9
modify it under the terms of the GNU Lesser General Public
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    10
License as published by the Free Software Foundation; either
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    11
version 2.1 of the License, or (at your option) any later version.
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    12
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    13
This library is distributed in the hope that it will be useful,
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    14
but WITHOUT ANY WARRANTY; without even the implied warranty of
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    16
Lesser General Public License for more details.
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    17
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    18
You should have received a copy of the GNU Lesser General Public
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    19
License along with this library; if not, write to the Free Software
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    20
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    21
*/
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    22
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    23
#include <stdlib.h>
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    24
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    25
#include <sys/time.h>
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    26
#include <signal.h>
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    27
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    28
#include <cyg/kernel/kapi.h>
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    29
#include <cyg/hal/hal_arch.h>
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    30
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    31
#include "applicfg.h"
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    32
#include <data.h>
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    33
#include <def.h>
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    34
#include <can.h>
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    35
#include <can_driver.h>
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    36
#include <objdictdef.h>
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    37
#include <objacces.h>
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    38
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    39
#include "lpc2138_pinout.h"
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    40
#include "lpc2138_defs.h"
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    41
#include "lpc2138.h"
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    42
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    43
#include "sja1000.h"
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    44
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    45
#include "time_slicer.h"
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    46
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    47
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    48
/*
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    49
	SEND/RECEIVE
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    50
*/
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    51
CAN_HANDLE canOpen(s_BOARD *board)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    52
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    53
	return NULL;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    54
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    55
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    56
/***************************************************************************/
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    57
int canClose(CAN_HANDLE fd0)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    58
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    59
	return 0;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    60
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    61
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    62
UNS8 canReceive(CAN_HANDLE fd0, Message *m)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    63
/*
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    64
Message *m :
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    65
	typedef struct {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    66
	  SHORT_CAN cob_id;     // l'ID du mesg
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    67
	  UNS8 rtr;             // remote transmission request. 0 if not rtr,
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    68
        	                // 1 for a rtr message
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    69
	  UNS8 len;             // message length (0 to 8)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    70
	  UNS8 data[8];         // data
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    71
	} Message;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    72
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    73
Fill the structure "Message" with data from the CAN receive buffer
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    74
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    75
return : 0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    76
*/
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    77
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    78
/*
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    79
	the sja1000 must be set to the PeliCAN mode
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    80
*/
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    81
    m->cob_id.w = sja1000_read(16) + (sja1000_read(17)<<8); // IO_PORTS_16(CAN0 + CANRCVID) >> 5
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    82
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    83
    m->rtr = (sja1000_read(17) >> 4) & 0x01; // (IO_PORTS_8(CAN0 + CANRCVID + 1) >> 4) & 0x01; 
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    84
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    85
    m->len = sja1000_read(18);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    86
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    87
    m->data[0] = sja1000_read(19);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    88
    m->data[1] = sja1000_read(20);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    89
    m->data[2] = sja1000_read(21);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    90
    m->data[3] = sja1000_read(22);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    91
    m->data[4] = sja1000_read(23);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    92
    m->data[5] = sja1000_read(24);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    93
    m->data[6] = sja1000_read(25);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    94
    m->data[7] = sja1000_read(26);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    95
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    96
    sja1000_write(CMR, 1<<RRB );        // release fifo
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    97
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    98
    return 0;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
    99
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   100
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   101
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   102
UNS8 canSend(CAN_HANDLE fd0, Message *m)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   103
/*
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   104
Message *m :
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   105
	typedef struct {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   106
	  SHORT_CAN cob_id;     // l'ID du mesg
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   107
	  UNS8 rtr;                     // remote transmission request. 0 if not rtr,
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   108
        	                        // 1 for a rtr message
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   109
	  UNS8 len;                     // message length (0 to 8)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   110
	  UNS8 data[8];         // data
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   111
	} Message;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   112
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   113
Send the content of the structure "Message" to the CAN transmit buffer
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   114
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   115
return : 0 if OK, 1 if error
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   116
*/
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   117
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   118
    unsigned char rec_buf;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   119
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   120
    do
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   121
    {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   122
        rec_buf = sja1000_read(SR);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   123
    }
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   124
    while ( (rec_buf & (1<<TBS))==0);           // loop until TBS high
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   125
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   126
    sja1000_write(16, m->cob_id.w & 0xff); 
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   127
    sja1000_write(17, (m->cob_id.w >> 8) & 0xff);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   128
    sja1000_write(18, m->len);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   129
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   130
    sja1000_write(19, m->data[0]); // tx data 1
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   131
    sja1000_write(20, m->data[1]); // tx data 2
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   132
    sja1000_write(21, m->data[2]); // tx data 3
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   133
    sja1000_write(22, m->data[3]); // tx data 4
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   134
    sja1000_write(23, m->data[4]); // tx data 5
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   135
    sja1000_write(24, m->data[5]); // tx data 6
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   136
    sja1000_write(25, m->data[6]); // tx data 7
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   137
    sja1000_write(26, m->data[7]); // tx data 8
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   138
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   139
    sja1000_write(CMR,( (0<<SRR) | (0<<CDO) | (0<<RRB) | (0<<AT) | (1<<TR)));
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   140
    do
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   141
    {
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   142
        rec_buf = sja1000_read(SR);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   143
    }
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   144
    while ( (rec_buf & (1<<TBS))==0);           // loop until TBS high
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   145
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   146
    return 0;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   147
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   148
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   149
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   150
/*
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   151
	SEQUENTIAL I/O TO FLASH
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   152
	those functions are for continous writing and read
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   153
*/
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   154
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   155
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   156
int nvram_open(void)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   157
{
3
d9cf34cd6823 ADDED LED to TESTMASTERSLAVE. It looks nice!
oremeq
parents: 0
diff changeset
   158
	int n = NVRAM_BLOCK_SIZE / sizeof(unsigned int);
d9cf34cd6823 ADDED LED to TESTMASTERSLAVE. It looks nice!
oremeq
parents: 0
diff changeset
   159
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   160
	/* some actions to initialise the flash */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   161
	data_len = 0;
3
d9cf34cd6823 ADDED LED to TESTMASTERSLAVE. It looks nice!
oremeq
parents: 0
diff changeset
   162
	data_num_pages = 0;
d9cf34cd6823 ADDED LED to TESTMASTERSLAVE. It looks nice!
oremeq
parents: 0
diff changeset
   163
d9cf34cd6823 ADDED LED to TESTMASTERSLAVE. It looks nice!
oremeq
parents: 0
diff changeset
   164
	data_page = (unsigned int *)malloc(sizeof(unsigned int) * n);
d9cf34cd6823 ADDED LED to TESTMASTERSLAVE. It looks nice!
oremeq
parents: 0
diff changeset
   165
	memset(data_page, 0, sizeof(unsigned int)*n);
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   166
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   167
	if (data_page == NULL)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   168
		return -1;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   169
3
d9cf34cd6823 ADDED LED to TESTMASTERSLAVE. It looks nice!
oremeq
parents: 0
diff changeset
   170
	regs_page = (unsigned int *)malloc(sizeof(unsigned int) * n);
d9cf34cd6823 ADDED LED to TESTMASTERSLAVE. It looks nice!
oremeq
parents: 0
diff changeset
   171
	memset(regs_page, 0, sizeof(unsigned int)*n);
d9cf34cd6823 ADDED LED to TESTMASTERSLAVE. It looks nice!
oremeq
parents: 0
diff changeset
   172
	if (regs_page == NULL)
d9cf34cd6823 ADDED LED to TESTMASTERSLAVE. It looks nice!
oremeq
parents: 0
diff changeset
   173
		return -2;
d9cf34cd6823 ADDED LED to TESTMASTERSLAVE. It looks nice!
oremeq
parents: 0
diff changeset
   174
d9cf34cd6823 ADDED LED to TESTMASTERSLAVE. It looks nice!
oremeq
parents: 0
diff changeset
   175
	iat_flash_read_regs();
d9cf34cd6823 ADDED LED to TESTMASTERSLAVE. It looks nice!
oremeq
parents: 0
diff changeset
   176
d9cf34cd6823 ADDED LED to TESTMASTERSLAVE. It looks nice!
oremeq
parents: 0
diff changeset
   177
	/* start the data at the location specified in the registers */ 
d9cf34cd6823 ADDED LED to TESTMASTERSLAVE. It looks nice!
oremeq
parents: 0
diff changeset
   178
	if (0) /* for now it is 0, but put here a test to know whether
d9cf34cd6823 ADDED LED to TESTMASTERSLAVE. It looks nice!
oremeq
parents: 0
diff changeset
   179
                  or not the NVRAM has been written before */
d9cf34cd6823 ADDED LED to TESTMASTERSLAVE. It looks nice!
oremeq
parents: 0
diff changeset
   180
		data_addr = regs_page[1];
d9cf34cd6823 ADDED LED to TESTMASTERSLAVE. It looks nice!
oremeq
parents: 0
diff changeset
   181
	else
d9cf34cd6823 ADDED LED to TESTMASTERSLAVE. It looks nice!
oremeq
parents: 0
diff changeset
   182
		data_addr = NVRAM_BLOCK_SIZE; /* let start at block 1 */
d9cf34cd6823 ADDED LED to TESTMASTERSLAVE. It looks nice!
oremeq
parents: 0
diff changeset
   183
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   184
	return 0;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   185
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   186
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   187
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   188
void nvram_close(void)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   189
{
3
d9cf34cd6823 ADDED LED to TESTMASTERSLAVE. It looks nice!
oremeq
parents: 0
diff changeset
   190
	/* write the last page before closing */
d9cf34cd6823 ADDED LED to TESTMASTERSLAVE. It looks nice!
oremeq
parents: 0
diff changeset
   191
	iat_flash_write_page(data_addr);
d9cf34cd6823 ADDED LED to TESTMASTERSLAVE. It looks nice!
oremeq
parents: 0
diff changeset
   192
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   193
	/* some actions to end accessing the flash */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   194
	free(data_page);
3
d9cf34cd6823 ADDED LED to TESTMASTERSLAVE. It looks nice!
oremeq
parents: 0
diff changeset
   195
d9cf34cd6823 ADDED LED to TESTMASTERSLAVE. It looks nice!
oremeq
parents: 0
diff changeset
   196
	regs_page[4] = data_num_pages;
d9cf34cd6823 ADDED LED to TESTMASTERSLAVE. It looks nice!
oremeq
parents: 0
diff changeset
   197
	/* write the registers to the NVRAM before closing */
d9cf34cd6823 ADDED LED to TESTMASTERSLAVE. It looks nice!
oremeq
parents: 0
diff changeset
   198
	iat_flash_write_regs();
d9cf34cd6823 ADDED LED to TESTMASTERSLAVE. It looks nice!
oremeq
parents: 0
diff changeset
   199
	free(regs_page);
d9cf34cd6823 ADDED LED to TESTMASTERSLAVE. It looks nice!
oremeq
parents: 0
diff changeset
   200
}
d9cf34cd6823 ADDED LED to TESTMASTERSLAVE. It looks nice!
oremeq
parents: 0
diff changeset
   201
d9cf34cd6823 ADDED LED to TESTMASTERSLAVE. It looks nice!
oremeq
parents: 0
diff changeset
   202
d9cf34cd6823 ADDED LED to TESTMASTERSLAVE. It looks nice!
oremeq
parents: 0
diff changeset
   203
void nvram_set_pos(UNS32 pos)
d9cf34cd6823 ADDED LED to TESTMASTERSLAVE. It looks nice!
oremeq
parents: 0
diff changeset
   204
/* set the current position in the NVRAM to pos */
d9cf34cd6823 ADDED LED to TESTMASTERSLAVE. It looks nice!
oremeq
parents: 0
diff changeset
   205
{
d9cf34cd6823 ADDED LED to TESTMASTERSLAVE. It looks nice!
oremeq
parents: 0
diff changeset
   206
}
d9cf34cd6823 ADDED LED to TESTMASTERSLAVE. It looks nice!
oremeq
parents: 0
diff changeset
   207
d9cf34cd6823 ADDED LED to TESTMASTERSLAVE. It looks nice!
oremeq
parents: 0
diff changeset
   208
d9cf34cd6823 ADDED LED to TESTMASTERSLAVE. It looks nice!
oremeq
parents: 0
diff changeset
   209
void nvram_new_firmwave()
d9cf34cd6823 ADDED LED to TESTMASTERSLAVE. It looks nice!
oremeq
parents: 0
diff changeset
   210
{
d9cf34cd6823 ADDED LED to TESTMASTERSLAVE. It looks nice!
oremeq
parents: 0
diff changeset
   211
/*
d9cf34cd6823 ADDED LED to TESTMASTERSLAVE. It looks nice!
oremeq
parents: 0
diff changeset
   212
	this function is called whenever a new firmware is about
d9cf34cd6823 ADDED LED to TESTMASTERSLAVE. It looks nice!
oremeq
parents: 0
diff changeset
   213
	to be written in the NVRAM
d9cf34cd6823 ADDED LED to TESTMASTERSLAVE. It looks nice!
oremeq
parents: 0
diff changeset
   214
*/
d9cf34cd6823 ADDED LED to TESTMASTERSLAVE. It looks nice!
oremeq
parents: 0
diff changeset
   215
	data_addr = regs_page[1] + regs_page[4]*NVRAM_BLOCK_SIZE;
d9cf34cd6823 ADDED LED to TESTMASTERSLAVE. It looks nice!
oremeq
parents: 0
diff changeset
   216
	if (data_addr > NVRAM_MAX_SIZE)
d9cf34cd6823 ADDED LED to TESTMASTERSLAVE. It looks nice!
oremeq
parents: 0
diff changeset
   217
		data_addr = NVRAM_BLOCK_SIZE;
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   218
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   219
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   220
int _get_data_len(int type)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   221
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   222
	int len = 0; /* number of bytes */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   223
	switch(type)
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   224
	{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   225
		case  boolean:
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   226
			len = 1;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   227
			break;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   228
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   229
		case  int8:
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   230
		case  uint8:
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   231
			len = 1;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   232
			break;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   233
		case  int16:
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   234
		case  uint16:
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   235
			len = 2;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   236
			break;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   237
		case  int24:
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   238
		case  uint24:
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   239
			len = 3;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   240
			break;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   241
		case  int32:
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   242
		case  uint32:
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   243
		case  real32:
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   244
			len = 4;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   245
			break;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   246
		case  int40:
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   247
		case  uint40:
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   248
			len = 5;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   249
			break;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   250
		case  int48:
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   251
		case  uint48:
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   252
			len = 6;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   253
			break;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   254
		case  int56:
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   255
		case  uint56:
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   256
			len = 7;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   257
			break;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   258
		case  int64:
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   259
		case  uint64:
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   260
		case  real64:
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   261
			len = 8;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   262
			break;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   263
#if 0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   264
/* TO DO */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   265
		case  visible_string:
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   266
		case  octet_string:
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   267
		case  unicode_string:
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   268
		case  time_of_day:
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   269
		case  time_difference:
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   270
#endif
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   271
	}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   272
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   273
	return len;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   274
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   275
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   276
3
d9cf34cd6823 ADDED LED to TESTMASTERSLAVE. It looks nice!
oremeq
parents: 0
diff changeset
   277
char nvram_write_data(int type, int access_attr, void *data)
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   278
/* return 0 if successfull */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   279
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   280
	int len = _get_data_len(type);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   281
3
d9cf34cd6823 ADDED LED to TESTMASTERSLAVE. It looks nice!
oremeq
parents: 0
diff changeset
   282
	if (data_len+len > NVRAM_BLOCK_SIZE)
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   283
	{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   284
		iat_flash_write_page(data_addr);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   285
		data_len = 0;
3
d9cf34cd6823 ADDED LED to TESTMASTERSLAVE. It looks nice!
oremeq
parents: 0
diff changeset
   286
		data_addr += NVRAM_BLOCK_SIZE; 
d9cf34cd6823 ADDED LED to TESTMASTERSLAVE. It looks nice!
oremeq
parents: 0
diff changeset
   287
d9cf34cd6823 ADDED LED to TESTMASTERSLAVE. It looks nice!
oremeq
parents: 0
diff changeset
   288
		/* wrap-around address pointer */
d9cf34cd6823 ADDED LED to TESTMASTERSLAVE. It looks nice!
oremeq
parents: 0
diff changeset
   289
		if (data_addr > NVRAM_MAX_SIZE)
d9cf34cd6823 ADDED LED to TESTMASTERSLAVE. It looks nice!
oremeq
parents: 0
diff changeset
   290
			data_addr = NVRAM_BLOCK_SIZE;
d9cf34cd6823 ADDED LED to TESTMASTERSLAVE. It looks nice!
oremeq
parents: 0
diff changeset
   291
d9cf34cd6823 ADDED LED to TESTMASTERSLAVE. It looks nice!
oremeq
parents: 0
diff changeset
   292
		data_num_pages++;
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   293
	}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   294
		
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   295
	memcpy(((char *)data_page)+data_len, data, len);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   296
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   297
	data_len += len;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   298
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   299
	return 0;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   300
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   301
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   302
3
d9cf34cd6823 ADDED LED to TESTMASTERSLAVE. It looks nice!
oremeq
parents: 0
diff changeset
   303
char nvram_read_data(int type, int access_attr, void *data)
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   304
/* return 0 if successful */
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   305
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   306
	int len = _get_data_len(type);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   307
3
d9cf34cd6823 ADDED LED to TESTMASTERSLAVE. It looks nice!
oremeq
parents: 0
diff changeset
   308
	if (data_len+len > NVRAM_BLOCK_SIZE)
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   309
	{
3
d9cf34cd6823 ADDED LED to TESTMASTERSLAVE. It looks nice!
oremeq
parents: 0
diff changeset
   310
		data_addr += NVRAM_BLOCK_SIZE;
d9cf34cd6823 ADDED LED to TESTMASTERSLAVE. It looks nice!
oremeq
parents: 0
diff changeset
   311
d9cf34cd6823 ADDED LED to TESTMASTERSLAVE. It looks nice!
oremeq
parents: 0
diff changeset
   312
		/* wrap-around address pointer */
d9cf34cd6823 ADDED LED to TESTMASTERSLAVE. It looks nice!
oremeq
parents: 0
diff changeset
   313
		if (data_addr > NVRAM_MAX_SIZE)
d9cf34cd6823 ADDED LED to TESTMASTERSLAVE. It looks nice!
oremeq
parents: 0
diff changeset
   314
			data_addr = NVRAM_BLOCK_SIZE;
d9cf34cd6823 ADDED LED to TESTMASTERSLAVE. It looks nice!
oremeq
parents: 0
diff changeset
   315
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   316
		iat_flash_read_page(data_addr);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   317
		data_len = 0;		
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   318
	}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   319
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   320
	memcpy(data, ((char *)data_page)+data_len, len);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   321
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   322
	data_len += len;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   323
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   324
	return 0;
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   325
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   326
3
d9cf34cd6823 ADDED LED to TESTMASTERSLAVE. It looks nice!
oremeq
parents: 0
diff changeset
   327
/*
d9cf34cd6823 ADDED LED to TESTMASTERSLAVE. It looks nice!
oremeq
parents: 0
diff changeset
   328
	NVRAM registers at block 0
d9cf34cd6823 ADDED LED to TESTMASTERSLAVE. It looks nice!
oremeq
parents: 0
diff changeset
   329
	pos        description
d9cf34cd6823 ADDED LED to TESTMASTERSLAVE. It looks nice!
oremeq
parents: 0
diff changeset
   330
	0          version of the current dictionnary
d9cf34cd6823 ADDED LED to TESTMASTERSLAVE. It looks nice!
oremeq
parents: 0
diff changeset
   331
	1          starting address for data block
d9cf34cd6823 ADDED LED to TESTMASTERSLAVE. It looks nice!
oremeq
parents: 0
diff changeset
   332
	2          date of last writing
d9cf34cd6823 ADDED LED to TESTMASTERSLAVE. It looks nice!
oremeq
parents: 0
diff changeset
   333
	3          address of the previous dictionnary          
d9cf34cd6823 ADDED LED to TESTMASTERSLAVE. It looks nice!
oremeq
parents: 0
diff changeset
   334
	4          size in pages of the current dict
d9cf34cd6823 ADDED LED to TESTMASTERSLAVE. It looks nice!
oremeq
parents: 0
diff changeset
   335
*/
d9cf34cd6823 ADDED LED to TESTMASTERSLAVE. It looks nice!
oremeq
parents: 0
diff changeset
   336
void nvram_write_reg(UNS32 reg, UNS16 pos)
d9cf34cd6823 ADDED LED to TESTMASTERSLAVE. It looks nice!
oremeq
parents: 0
diff changeset
   337
/* write reg at the position in the data block 0 */
d9cf34cd6823 ADDED LED to TESTMASTERSLAVE. It looks nice!
oremeq
parents: 0
diff changeset
   338
{
d9cf34cd6823 ADDED LED to TESTMASTERSLAVE. It looks nice!
oremeq
parents: 0
diff changeset
   339
	regs_page[pos] = reg;
d9cf34cd6823 ADDED LED to TESTMASTERSLAVE. It looks nice!
oremeq
parents: 0
diff changeset
   340
}
d9cf34cd6823 ADDED LED to TESTMASTERSLAVE. It looks nice!
oremeq
parents: 0
diff changeset
   341
d9cf34cd6823 ADDED LED to TESTMASTERSLAVE. It looks nice!
oremeq
parents: 0
diff changeset
   342
UNS32 nvram_read_reg(UNS16 pos)
d9cf34cd6823 ADDED LED to TESTMASTERSLAVE. It looks nice!
oremeq
parents: 0
diff changeset
   343
/* read reg at the position in the data block 0 */
d9cf34cd6823 ADDED LED to TESTMASTERSLAVE. It looks nice!
oremeq
parents: 0
diff changeset
   344
{
d9cf34cd6823 ADDED LED to TESTMASTERSLAVE. It looks nice!
oremeq
parents: 0
diff changeset
   345
	return regs_page[pos];
d9cf34cd6823 ADDED LED to TESTMASTERSLAVE. It looks nice!
oremeq
parents: 0
diff changeset
   346
}
d9cf34cd6823 ADDED LED to TESTMASTERSLAVE. It looks nice!
oremeq
parents: 0
diff changeset
   347
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   348
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   349
/*
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   350
	LED
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   351
*/
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   352
3
d9cf34cd6823 ADDED LED to TESTMASTERSLAVE. It looks nice!
oremeq
parents: 0
diff changeset
   353
void led_set_redgreen(UNS8 bits)
d9cf34cd6823 ADDED LED to TESTMASTERSLAVE. It looks nice!
oremeq
parents: 0
diff changeset
   354
/* bits : each bit of this uns8 is assigned a led 
d9cf34cd6823 ADDED LED to TESTMASTERSLAVE. It looks nice!
oremeq
parents: 0
diff changeset
   355
          0=off, 1=on
d9cf34cd6823 ADDED LED to TESTMASTERSLAVE. It looks nice!
oremeq
parents: 0
diff changeset
   356
*/
0
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   357
{
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   358
	lpc2138_redgreenled_set(bits);
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   359
}
4472ee7c6c3e Commit a new cvs repo.
etisserant
parents:
diff changeset
   360