drivers/ecos_lpc2138_sja1000/canOpenDriver.c
changeset 0 4472ee7c6c3e
child 3 d9cf34cd6823
equal deleted inserted replaced
-1:000000000000 0:4472ee7c6c3e
       
     1 /*
       
     2 This file is part of CanFestival, a library implementing CanOpen Stack.
       
     3 
       
     4  Author: Christian Fortin (canfestival@canopencanada.ca)
       
     5 
       
     6 See COPYING file for copyrights details.
       
     7 
       
     8 This library is free software; you can redistribute it and/or
       
     9 modify it under the terms of the GNU Lesser General Public
       
    10 License as published by the Free Software Foundation; either
       
    11 version 2.1 of the License, or (at your option) any later version.
       
    12 
       
    13 This library is distributed in the hope that it will be useful,
       
    14 but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    16 Lesser General Public License for more details.
       
    17 
       
    18 You should have received a copy of the GNU Lesser General Public
       
    19 License along with this library; if not, write to the Free Software
       
    20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
       
    21 */
       
    22 
       
    23 #include <stdlib.h>
       
    24 
       
    25 #include <sys/time.h>
       
    26 #include <signal.h>
       
    27 
       
    28 #include <cyg/kernel/kapi.h>
       
    29 #include <cyg/hal/hal_arch.h>
       
    30 
       
    31 #include "applicfg.h"
       
    32 #include <data.h>
       
    33 #include <def.h>
       
    34 #include <can.h>
       
    35 #include <can_driver.h>
       
    36 #include <objdictdef.h>
       
    37 #include <objacces.h>
       
    38 
       
    39 #include "lpc2138_pinout.h"
       
    40 #include "lpc2138_defs.h"
       
    41 #include "lpc2138.h"
       
    42 
       
    43 #include "sja1000.h"
       
    44 
       
    45 #include "time_slicer.h"
       
    46 
       
    47 
       
    48 /*
       
    49 	SEND/RECEIVE
       
    50 */
       
    51 CAN_HANDLE canOpen(s_BOARD *board)
       
    52 {
       
    53 	return NULL;
       
    54 }
       
    55 
       
    56 /***************************************************************************/
       
    57 int canClose(CAN_HANDLE fd0)
       
    58 {
       
    59 	return 0;
       
    60 }
       
    61 
       
    62 UNS8 canReceive(CAN_HANDLE fd0, Message *m)
       
    63 /*
       
    64 Message *m :
       
    65 	typedef struct {
       
    66 	  SHORT_CAN cob_id;     // l'ID du mesg
       
    67 	  UNS8 rtr;             // remote transmission request. 0 if not rtr,
       
    68         	                // 1 for a rtr message
       
    69 	  UNS8 len;             // message length (0 to 8)
       
    70 	  UNS8 data[8];         // data
       
    71 	} Message;
       
    72 
       
    73 Fill the structure "Message" with data from the CAN receive buffer
       
    74 
       
    75 return : 0
       
    76 */
       
    77 {
       
    78 /*
       
    79 	the sja1000 must be set to the PeliCAN mode
       
    80 */
       
    81     m->cob_id.w = sja1000_read(16) + (sja1000_read(17)<<8); // IO_PORTS_16(CAN0 + CANRCVID) >> 5
       
    82 
       
    83     m->rtr = (sja1000_read(17) >> 4) & 0x01; // (IO_PORTS_8(CAN0 + CANRCVID + 1) >> 4) & 0x01; 
       
    84 
       
    85     m->len = sja1000_read(18);
       
    86 
       
    87     m->data[0] = sja1000_read(19);
       
    88     m->data[1] = sja1000_read(20);
       
    89     m->data[2] = sja1000_read(21);
       
    90     m->data[3] = sja1000_read(22);
       
    91     m->data[4] = sja1000_read(23);
       
    92     m->data[5] = sja1000_read(24);
       
    93     m->data[6] = sja1000_read(25);
       
    94     m->data[7] = sja1000_read(26);
       
    95 
       
    96     sja1000_write(CMR, 1<<RRB );        // release fifo
       
    97 
       
    98     return 0;
       
    99 }
       
   100 
       
   101 
       
   102 UNS8 canSend(CAN_HANDLE fd0, Message *m)
       
   103 /*
       
   104 Message *m :
       
   105 	typedef struct {
       
   106 	  SHORT_CAN cob_id;     // l'ID du mesg
       
   107 	  UNS8 rtr;                     // remote transmission request. 0 if not rtr,
       
   108         	                        // 1 for a rtr message
       
   109 	  UNS8 len;                     // message length (0 to 8)
       
   110 	  UNS8 data[8];         // data
       
   111 	} Message;
       
   112 
       
   113 Send the content of the structure "Message" to the CAN transmit buffer
       
   114 
       
   115 return : 0 if OK, 1 if error
       
   116 */
       
   117 {
       
   118     unsigned char rec_buf;
       
   119 
       
   120     do
       
   121     {
       
   122         rec_buf = sja1000_read(SR);
       
   123     }
       
   124     while ( (rec_buf & (1<<TBS))==0);           // loop until TBS high
       
   125 
       
   126     sja1000_write(16, m->cob_id.w & 0xff); 
       
   127     sja1000_write(17, (m->cob_id.w >> 8) & 0xff);
       
   128     sja1000_write(18, m->len);
       
   129 
       
   130     sja1000_write(19, m->data[0]); // tx data 1
       
   131     sja1000_write(20, m->data[1]); // tx data 2
       
   132     sja1000_write(21, m->data[2]); // tx data 3
       
   133     sja1000_write(22, m->data[3]); // tx data 4
       
   134     sja1000_write(23, m->data[4]); // tx data 5
       
   135     sja1000_write(24, m->data[5]); // tx data 6
       
   136     sja1000_write(25, m->data[6]); // tx data 7
       
   137     sja1000_write(26, m->data[7]); // tx data 8
       
   138 
       
   139     sja1000_write(CMR,( (0<<SRR) | (0<<CDO) | (0<<RRB) | (0<<AT) | (1<<TR)));
       
   140     do
       
   141     {
       
   142         rec_buf = sja1000_read(SR);
       
   143     }
       
   144     while ( (rec_buf & (1<<TBS))==0);           // loop until TBS high
       
   145 
       
   146     return 0;
       
   147 }
       
   148 
       
   149 
       
   150 /*
       
   151 	SEQUENTIAL I/O TO FLASH
       
   152 	those functions are for continous writing and read
       
   153 */
       
   154 
       
   155 
       
   156 int nvram_open(void)
       
   157 {
       
   158 	/* some actions to initialise the flash */
       
   159 	data_len = 0;
       
   160 
       
   161 	data_addr = 0; 
       
   162 
       
   163 	data_page = (unsigned int *)malloc(sizeof(unsigned int) * 64);
       
   164 	memset(data_page, 0, sizeof(unsigned int)*64);
       
   165 
       
   166 	if (data_page == NULL)
       
   167 		return -1;
       
   168 
       
   169 	return 0;
       
   170 }
       
   171 
       
   172 
       
   173 void nvram_close(void)
       
   174 {
       
   175 	/* some actions to end accessing the flash */
       
   176 	free(data_page);
       
   177 }
       
   178 
       
   179 int _get_data_len(int type)
       
   180 {
       
   181 	int len = 0; /* number of bytes */
       
   182 	switch(type)
       
   183 	{
       
   184 		case  boolean:
       
   185 			len = 1;
       
   186 			break;
       
   187 
       
   188 		case  int8:
       
   189 		case  uint8:
       
   190 			len = 1;
       
   191 			break;
       
   192 		case  int16:
       
   193 		case  uint16:
       
   194 			len = 2;
       
   195 			break;
       
   196 		case  int24:
       
   197 		case  uint24:
       
   198 			len = 3;
       
   199 			break;
       
   200 		case  int32:
       
   201 		case  uint32:
       
   202 		case  real32:
       
   203 			len = 4;
       
   204 			break;
       
   205 		case  int40:
       
   206 		case  uint40:
       
   207 			len = 5;
       
   208 			break;
       
   209 		case  int48:
       
   210 		case  uint48:
       
   211 			len = 6;
       
   212 			break;
       
   213 		case  int56:
       
   214 		case  uint56:
       
   215 			len = 7;
       
   216 			break;
       
   217 		case  int64:
       
   218 		case  uint64:
       
   219 		case  real64:
       
   220 			len = 8;
       
   221 			break;
       
   222 #if 0
       
   223 /* TO DO */
       
   224 		case  visible_string:
       
   225 		case  octet_string:
       
   226 		case  unicode_string:
       
   227 		case  time_of_day:
       
   228 		case  time_difference:
       
   229 #endif
       
   230 	}
       
   231 
       
   232 	return len;
       
   233 }
       
   234 
       
   235 
       
   236 char nvram_write(int type, int access_attr, void *data)
       
   237 /* return 0 if successfull */
       
   238 {
       
   239 	int len = _get_data_len(type);
       
   240 
       
   241 	if (data_len+len > 256)
       
   242 	{
       
   243 		iat_flash_write_page(data_addr);
       
   244 		data_len = 0;
       
   245 		data_addr += 256;
       
   246 	}
       
   247 		
       
   248 	memcpy(((char *)data_page)+data_len, data, len);
       
   249 
       
   250 	data_len += len;
       
   251 
       
   252 	return 0;
       
   253 }
       
   254 
       
   255 
       
   256 char nvram_read(int type, int access_attr, void *data)
       
   257 /* return 0 if successful */
       
   258 {
       
   259 	int len = _get_data_len(type);
       
   260 
       
   261 	if (data_len+len > 256)
       
   262 	{
       
   263 		data_addr += 256;
       
   264 		iat_flash_read_page(data_addr);
       
   265 		data_len = 0;		
       
   266 	}
       
   267 
       
   268 	memcpy(data, ((char *)data_page)+data_len, len);
       
   269 
       
   270 	data_len += len;
       
   271 
       
   272 	return 0;
       
   273 }
       
   274 
       
   275 
       
   276 /*
       
   277 	LED
       
   278 */
       
   279 
       
   280 void led_set_redgreen(unsigned char bits)
       
   281 {
       
   282 	lpc2138_redgreenled_set(bits);
       
   283 }
       
   284