drivers/unix/unix.c
changeset 442 010c26b9ba89
parent 401 2c90876b9751
child 454 bc000083297a
equal deleted inserted replaced
441:968697561674 442:010c26b9ba89
    50 #include "canfestival.h"
    50 #include "canfestival.h"
    51 #include "timers_driver.h"
    51 #include "timers_driver.h"
    52 
    52 
    53 #define MAX_NB_CAN_PORTS 16
    53 #define MAX_NB_CAN_PORTS 16
    54 
    54 
       
    55 /** CAN port structure */
    55 typedef struct {
    56 typedef struct {
    56   char used;
    57   char used;  /**< flag indicating CAN port usage, will be used to abort Receiver task*/
    57   CAN_HANDLE fd;
    58   CAN_HANDLE fd; /**< CAN port file descriptor*/
    58   TASK_HANDLE receiveTask;
    59   TASK_HANDLE receiveTask; /**< CAN Receiver task*/
    59   CO_Data* d;
    60   CO_Data* d; /**< CAN object data*/
    60 } CANPort;
    61 } CANPort;
    61 
    62 
    62 #include "can_driver.h"
    63 #include "can_driver.h"
    63 
    64 
    64 /*Declares the funtion pointers for dll binding or simple protos*/
    65 /*Declares the funtion pointers for dll binding or simple protos*/
    82 		return 0;
    83 		return 0;
    83 	}
    84 	}
    84 	return -1;
    85 	return -1;
    85 }
    86 }
    86 
    87 
    87 /*Loads the dll and get funcs ptr*/
    88 /**
       
    89  * Loads the dll and get funcs ptr
       
    90  *
       
    91  * @param driver_name String containing driver's dynamic library name
       
    92  * @return Library handle
       
    93  */
    88 LIB_HANDLE LoadCanDriver(char* driver_name)
    94 LIB_HANDLE LoadCanDriver(char* driver_name)
    89 {
    95 {
    90 	LIB_HANDLE handle = NULL;
    96 	LIB_HANDLE handle = NULL;
    91 	char *error;
    97 	char *error;
    92 	
    98 	
   120 {
   126 {
   121 	return DLL_CALL(canReceive)(port->fd, Message *m);
   127 	return DLL_CALL(canReceive)(port->fd, Message *m);
   122 }
   128 }
   123 */
   129 */
   124 
   130 
       
   131 /**
       
   132  * CAN send routine
       
   133  * @param port CAN port
       
   134  * @param m CAN message
       
   135  * @return success or error
       
   136  */
   125 UNS8 canSend(CAN_PORT port, Message *m)
   137 UNS8 canSend(CAN_PORT port, Message *m)
   126 {
   138 {
   127 	if(port){
   139 	if(port){
   128 		UNS8 res;
   140 		UNS8 res;
   129 	        //LeaveMutex();
   141 	        //LeaveMutex();
   132 		return res; // OK
   144 		return res; // OK
   133 	}               
   145 	}               
   134 	return 1; // NOT OK
   146 	return 1; // NOT OK
   135 }
   147 }
   136 
   148 
       
   149 /**
       
   150  * CAN Receiver Task
       
   151  * @param port CAN port
       
   152  */
   137 void canReceiveLoop(CAN_PORT port)
   153 void canReceiveLoop(CAN_PORT port)
   138 {
   154 {
   139        Message m;
   155        Message m;
   140 
   156 
   141        while (1) {
   157        while (((CANPort*)port)->used) {
   142                if (DLL_CALL(canReceive)(((CANPort*)port)->fd, &m) != 0)
   158                if (DLL_CALL(canReceive)(((CANPort*)port)->fd, &m) != 0)
   143                        break;
   159                        break;
   144 
   160 
   145                EnterMutex();
   161                EnterMutex();
   146                canDispatch(((CANPort*)port)->d, &m);
   162                canDispatch(((CANPort*)port)->d, &m);
   147                LeaveMutex();
   163                LeaveMutex();
   148        }
   164        }
   149 }
   165 }
       
   166 
       
   167 /**
       
   168  * CAN open routine
       
   169  * @param board device name and baudrate
       
   170  * @param d CAN object data
       
   171  * @return valid CAN_PORT pointer or NULL
       
   172  */
   150 CAN_PORT canOpen(s_BOARD *board, CO_Data * d)
   173 CAN_PORT canOpen(s_BOARD *board, CO_Data * d)
   151 {
   174 {
   152 	int i;
   175 	int i;
   153 	for(i=0; i < MAX_NB_CAN_PORTS; i++)
   176 	for(i=0; i < MAX_NB_CAN_PORTS; i++)
   154 	{
   177 	{
   178         	MSG("CanOpen : Cannot open board {busname='%s',baudrate='%s'}\n",board->busname, board->baudrate);
   201         	MSG("CanOpen : Cannot open board {busname='%s',baudrate='%s'}\n",board->busname, board->baudrate);
   179 		return NULL;
   202 		return NULL;
   180 	}
   203 	}
   181 }
   204 }
   182 
   205 
       
   206 /**
       
   207  * CAN close routine
       
   208  * @param d CAN object data
       
   209  * @return success or error
       
   210  */
   183 int canClose(CO_Data * d)
   211 int canClose(CO_Data * d)
   184 {
   212 {
       
   213 	UNS8 res;
       
   214 
   185 	EnterMutex();
   215 	EnterMutex();
   186 	((CANPort*)d->canHandle)->used = 0;
   216 	((CANPort*)d->canHandle)->used = 0;
   187 	CANPort* tmp = (CANPort*)d->canHandle;
   217 	CANPort* tmp = (CANPort*)d->canHandle;
   188 	d->canHandle = NULL;
   218 	d->canHandle = NULL;
   189 	LeaveMutex();
   219 	LeaveMutex();
   190 	
   220 	
   191 	int res = DLL_CALL(canClose)(tmp->fd);
   221 	// close CAN port
   192 	
   222 	res = DLL_CALL(canClose)(tmp->fd);
       
   223 
       
   224 	// kill receiver task
   193 	WaitReceiveTaskEnd(&tmp->receiveTask);
   225 	WaitReceiveTaskEnd(&tmp->receiveTask);
       
   226 	
   194 	return res;
   227 	return res;
   195 }
   228 }
   196 
   229 
   197 
   230 
       
   231 /**
       
   232  * CAN change baudrate routine
       
   233  * @param port CAN port
       
   234  * @param baud baudrate
       
   235  * @return success or error
       
   236  */
   198 UNS8 canChangeBaudRate(CAN_PORT port, char* baud)
   237 UNS8 canChangeBaudRate(CAN_PORT port, char* baud)
   199 {
   238 {
   200    if(port){
   239    if(port){
   201 		UNS8 res;
   240 		UNS8 res;
   202 	        //LeaveMutex();
   241 	        //LeaveMutex();