/* Simula un falso puerto de comunicaciones. */ #include "fake_com_port.h" #include #include #include #include #include #include // fake device status variables static volatile int cont=-1; static volatile int new_data_available = 0; static volatile char data_buffer = 0; static volatile int initialized=0; // internal thread that "receives" data periodically void *sensor_thread(void *arg) { const char *data = "This is a message from the communication port."; int data_length = strlen(data); struct timespec next_time; // hora de activación struct timespec my_period = {1, 100000000}; // lee la hora de la primera activación de la tarea clock_gettime(CLOCK_MONOTONIC, &next_time); // lazo que se ejecuta periódicamente while (1) { // espera al próximo periodo incr_timespec(&next_time,&my_period); CHK( clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &next_time, NULL) ); if (initialized) { // arrive new data cont ++; data_buffer = data[cont%data_length]; new_data_available = 1; //printf("Port receive %dth byte %c\n", cont, data_buffer); } } return NULL; } extern void fake_com_port_outb(unsigned short port, char val) { pthread_t th_sensor; switch (port) { case FAKE_COM_PORT_CONTROL_REG: // initialize port initialized = 1; // crea el thread "sensor_thread" a la máxima prioridad CHK( pthread_create (&th_sensor, NULL, sensor_thread, NULL) ); CHK( pthread_setschedprio(th_sensor, sched_get_priority_max(SCHED_FIFO)) ); break; default: // wrong port value: no effect printe("fake_com_port_outb: Error: wrong port %d\n", port); } } extern char fake_com_port_inb(unsigned short port) { char ret; switch (port) { case FAKE_COM_PORT_DATA_REG: // return last received byte of data new_data_available = 0; ret = data_buffer; break; case FAKE_COM_PORT_CONTROL_REG: // new data available ? ret = new_data_available; break; default: // wrong port value printe("fake_com_port_inb: Error: wrong port %d\n", port); ret = 0; } return ret; }