Subversion Repositories shark

Compare Revisions

Ignore whitespace Rev 721 → Rev 720

/shark/trunk/ports/servo/servo.c
140,8 → 140,6
static BYTE *RXTX_addr[4][RXTX_BUFF_MAX];
unsigned int RX_position[4] = {0,0,0,0};
unsigned int TX_position[4] = {0,0,0,0};
unsigned int RX_cycle = 0;
unsigned int TX_cycle = 0;
 
const int BaudTable[] = {
1200,
161,71 → 159,58
timer_expired = 1;
}
 
void servo_rx_error(unsigned port, unsigned type) {
void servo_rx_error(unsigned port) {
 
kern_printf("(SERVO: BUFFER ERROR PORT:%d ERR:%d)",port,type);
kern_printf("SERVO: RX ERROR COM%d\n",port);
 
}
 
void servo_indication(unsigned port, BYTE data) {
 
if (RXTX_addr[port][RX_position[port]] == NULL) {
if (data != RXTX_buff[port][RX_position[port]])
servo_rx_error(port,1);
servo_rx_error(port);
RX_position[port]++;
 
if (RX_position[port] >= RXTX_BUFF_MAX) {
RX_cycle++;
RX_position[port] = 0;
}
if ((RX_cycle == TX_cycle && RX_position[port] > TX_position[port]) ||
(RX_cycle > TX_cycle))
servo_rx_error(port,2);
 
if (RX_position[port] >= RXTX_BUFF_MAX) RX_position[port] = 0;
if (TX_position[port] == RX_position[port])
servo_rx_error(port);
} else {
*RXTX_addr[port][RX_position[port]] = data;
RX_position[port]++;
 
if (RX_position[port] >= RXTX_BUFF_MAX) {
RX_cycle++;
RX_position[port] = 0;
}
if ((RX_cycle == TX_cycle && RX_position[port] > TX_position[port]) ||
(RX_cycle > TX_cycle))
servo_rx_error(port,2);
if (RX_position[port] >= RXTX_BUFF_MAX) RX_position[port] = 0;
if (TX_position[port] == RX_position[port])
servo_rx_error(port);
}
 
cprintf("(TXRX:%d:%d)",TX_position[port],RX_position[port]);
 
}
 
void servo_confirm(unsigned port, BYTE msg_status) {
 
#ifdef SERVO_DEBUG
if (msg_status == COM_ERROR)
kern_printf("(SERVO: PORT:%d ERROR)",port);
kern_printf("(SERVO:COM%d ERROR)",port);
#endif
 
}
 
int servo_send_msg(unsigned port, BYTE *msg, unsigned len_msg, BYTE *res, unsigned len_res) {
 
int i = 0, old, oldcycle;
SYS_FLAGS f;
int i = 0, old;
old = TX_position[port];
 
f = kern_fsave();
cprintf("(SEND:%d:%d)",len_msg,len_res);
 
old = TX_position[port];
oldcycle = TX_cycle;
 
while(i < len_msg) {
RXTX_buff[port][TX_position[port]] = msg[i];
RXTX_addr[port][TX_position[port]] = NULL;
TX_position[port]++;
if (TX_position[port] >= RXTX_BUFF_MAX) {
TX_cycle++;
TX_position[port] = 0;
}
if (TX_position[port] >= RXTX_BUFF_MAX) TX_position[port] = 0;
if (TX_position[port] == RX_position[port]) {
TX_position[port] = old;
TX_cycle = oldcycle;
kern_frestore(f);
cprintf("Error\n");
return -1;
}
i++;
236,23 → 221,19
RXTX_buff[port][TX_position[port]] = 0;
RXTX_addr[port][TX_position[port]] = res+i;
TX_position[port]++;
if (TX_position[port] >= RXTX_BUFF_MAX) {
TX_cycle++;
TX_position[port] = 0;
}
if (TX_position[port] >= RXTX_BUFF_MAX) TX_position[port] = 0;
if (TX_position[port] == RX_position[port]) {
TX_position[port] = old;
TX_cycle = oldcycle;
kern_frestore(f);
cprintf("Error\n");
return -1;
}
i++;
}
 
kern_frestore(f);
 
com_irq_send(port, len_msg, msg);
 
cprintf("(TXRX:%d:%d)",TX_position[port],RX_position[port]);
 
return 0;
 
}
267,8 → 248,6
 
com_set_functions(servo_confirm,servo_indication);
 
com_irq_enable((unsigned)(port),ALL_IRQ);
 
return err;
 
}
649,17 → 628,42
int servo_turn_on(int port, int servo)
{
 
unsigned char b[2];
#ifdef SERVO_TIMEOUT_EVENT
struct timespec current_time;
#endif
unsigned char b;
int err;
int servo_port = (unsigned)(port);
 
if (servo > 15) return -1;
 
b[0] = 0x80;
b[1] = 0x10 | (servo & 0x0F);
servo_send_msg(servo_port, b, 2, NULL, 0);
timer_expired = 0;
#ifdef SERVO_TIMEOUT_EVENT
kern_gettime(&current_time);
ADDUSEC2TIMESPEC(SERVO_TIMEOUT,&current_time);
timeout_event = kern_event_post(&current_time, set_timer_expired, NULL);
#else
timeout_event = NIL;
#endif
 
return 0;
b = 0x80;
err = com_send(servo_port, b);
err = com_receive(servo_port);
if (err != (int)(b)) timer_expired = 1;
 
b = 0x10 | (servo & 0x0F);
err = com_send(servo_port, b);
err = com_receive(servo_port);
if (err != (int)(b)) timer_expired = 1;
 
if (timeout_event != NIL) kern_event_delete(timeout_event);
 
if (!timer_expired)
return 0;
else
return -1;
 
}
 
/* 1000.0000:0010.0000 */