Пример #1
0
void
init_touch_screen(void)
{
    /*
     * Configure X1, X2, Y1 and Y2 output pins:
     */
    configure_pin(&g_pin_X1, true);
    configure_pin(&g_pin_X2, true);
    configure_pin(&g_pin_Y1, true);
    configure_pin(&g_pin_Y2, true);
}
Пример #2
0
void
configure_pins(struct header_pin pins[], uint16_t length) {
  int i;
  for(i=0; i<length; i++) {
    configure_pin(&pins[i]);
    UARTFlushTx(false);
  }
}
Пример #3
0
static void 
select_X1_X2_output_pins(void)
{
    /*
     * Re-configure X1 and X2 as output pins:
     */
    configure_pin(&g_pin_X1, true);
    configure_pin(&g_pin_X2, true);

    /*
     * Re-configure Y1 and Y2 as input pins:
     */
    configure_pin(&g_pin_Y1, false);
//#if 0
    configure_pin(&g_pin_Y2, false);
//#endif
}
Пример #4
0
			Stm32f4_PinOut(GPIO_TypeDef* 	GPIOx, uint16_t Gpio_Pin) : _GPIOx(GPIOx), _Gpio_Pin(Gpio_Pin)
			{
				configure_pin(GPIOx, Gpio_Pin, GPIO_PuPd_UP, GPIO_Mode_OUT);
			}
Пример #5
0
		inline void configure_pin(GPIO_TypeDef* 	GPIOx, uint32_t		GPIO_Pin, GPIOPuPd_TypeDef GPIOPuPd)
		{
			configure_pin(GPIOx, GPIO_Pin, GPIOPuPd, GPIO_Mode_AF);
		}
Пример #6
0
void httpd_appcall(void) {
  if(uip_conn->lport != HTONS(80)) {
    uip_abort();
    return;
  }

  struct httpd_state *hs = (struct httpd_state *)&(uip_conn->appstate);
  bool send_new_data = false;

  if(uip_connected()) {
    printf("Connected\n");
    hs->data_count = 0;
    hs->idle_count = 0;
    hs->state = 0;
    hs->done = false;
  } else if(uip_newdata()) {
    printf("New data\n");
    if(strncmp(DATA_BUF, "GET ", 4) != 0) {
      uip_abort();
      return;
    }
    printf("Got get request\n");

#define PATH_START 4
    /* Get path */
    int i;
    for(i = 4; i<uip_datalen() && DATA_BUF[i] != ' '; i++);

    char path[21];
    if( i-PATH_START > 20 ) {
      i = 20;
    }

    memcpy(path, DATA_BUF + PATH_START, i-PATH_START);
    path[i-PATH_START] = '\0';
    printf("Path: '%s'\n", path);

    if(strcmp(path, "/") == 0) {
      printf("root\n");
      hs->request_type = REQUEST_FILE;
      hs->state = FILE_ROOT;
      hs->offset = 0;
    } else if(strncmp(path, "/read", 5) == 0) {
      hs->request_type = REQUEST_READ;
    } else if(strncmp(path, "/write/", 7) == 0) {
      hs->request_type = REQUEST_WRITE;
      hs->state = REQUEST_WRITE_ERR;
      struct header_pin *connector;
      uint8_t pin;
      char value[10];

      if( !parse_path(path+7, &connector, &pin, value) ) {
	goto config_done;
      }

      if( connector[pin].config != CONFIG_OUTPUT ) {
	goto config_done;
      }

      if(value[0] == '1') {
	MAP_GPIOPinWrite(connector[pin].base, connector[pin].pin, connector[pin].pin);
      } else {
	MAP_GPIOPinWrite(connector[pin].base, connector[pin].pin, 0);
      }
      hs->state = REQUEST_WRITE_OK;
    } else if(strncmp(path, "/config/",8) == 0) {
      hs->request_type = REQUEST_CONFIG;
      struct header_pin *connector;
      uint8_t pin;
      char dir[10];

      if( !parse_path(path+8, &connector, &pin, dir) ) {
	printf("Nope\n");
	goto config_done;
      }

      if( connector[pin].config == CONFIG_NOT_USED ) {
	printf("N/A\n");
	goto config_done;
      }

      printf("Dir: '%c'\n", dir[0]);

      if( dir[0] == 'i' ) {
	printf("Input\n");
	connector[pin].config = CONFIG_INPUT;
	configure_pin(&connector[pin]);
      } else if( dir[0] == 'o' ) {
	printf("Output\n");
	connector[pin].config = CONFIG_OUTPUT;
	configure_pin(&connector[pin]);
      }
    } else {
      hs->request_type = 0;
    }
  config_done:
    send_new_data = true;
  } else if( uip_acked() ) {
    hs->data_count++;
    if( hs->done ) {
      uip_close();
    } else {
      send_new_data = true;
    }
  } else if( uip_poll() ) {
    printf("Poll\n");
    hs->idle_count++;
    if( hs->idle_count > 10 ) {
      uip_close();
    }
  }

  if( uip_rexmit() || send_new_data ) {
    printf("%p: Request type: %d\n", hs, hs->request_type);
    printf("%p: Sending data (%d)\n", hs, hs->data_count);
    switch(hs->request_type) {
    case REQUEST_READ:
      if(hs->data_count == 0) {
	hs->xmit_buf = http_json_header;
	hs->xmit_buf_size = sizeof(http_json_header)-1;
      } else if(hs->data_count == 1) {
	hs->xmit_buf = NULL;

	uint8_t buf[500];
	uint16_t i = 0;

	static char b1[] = "{\n\t\"J1\": [";
	static char b2[] = "\t\"J2\": [";
	static char b3[] = "\t\"J3\": [";
	static char b4[] = "\t\"J4\": [";
	static char bnone[] = "\"x\"";
	static char bx[] = "\n}";

	memcpy(buf+i, b1, sizeof(b1)-1);
	i+= sizeof(b1)-1;
	i += read_pins(j1, HEADER_SIZE, buf+i);
	buf[i++] = ']';
	buf[i++] = ',';
	buf[i++] = '\n';

	memcpy(buf+i, b2, sizeof(b2)-1);
	i+= sizeof(b2)-1;
	i += read_pins(j2, HEADER_SIZE, buf+i);
	buf[i++] = ']';
	buf[i++] = ',';
	buf[i++] = '\n';

	memcpy(buf+i, b3, sizeof(b3)-1);
	i+= sizeof(b3)-1;
	i += read_pins(j3, HEADER_SIZE, buf+i);
	buf[i++] = ']';
	buf[i++] = ',';
	buf[i++] = '\n';

	memcpy(buf+i, b4, sizeof(b4)-1);
	i+= sizeof(b4)-1;
	i += read_pins(j4, HEADER_SIZE, buf+i);
	buf[i++] = ']';

	memcpy(buf+i, bx, sizeof(bx)-1);
	i+= sizeof(bx)-1;

	uip_send(buf, i);
	hs->done = true;
      } else {
	hs->xmit_buf = NULL;
	uip_close();
      }
      break;
    case REQUEST_CONFIG:
      if(hs->data_count == 0) {
	hs->xmit_buf = http_json_header;
	hs->xmit_buf_size = sizeof(http_json_header)-1;
	hs->done = true;
      } /*else if(hs->data_count == 1) {
	}*/ else {
	hs->xmit_buf = NULL;
	uip_close();
      }
      break;
    case REQUEST_WRITE:
      if(hs->data_count == 0) {
	hs->xmit_buf = http_json_header;
	hs->xmit_buf_size = sizeof(http_json_header)-1;
      } else if(hs->data_count == 1) {
	if( hs->state == REQUEST_WRITE_OK ) {
	  char buf[] = "ok";
	  uip_send(buf, sizeof(buf)-1);
	} else {
	  char buf[] = "error";
	  uip_send(buf, sizeof(buf)-1);
	}
	hs->done = true;
	hs->xmit_buf = NULL;
      } else {
	hs->xmit_buf = NULL;
	uip_close();
      }
      break;
    case REQUEST_FILE:
      if(hs->data_count == 0) {
	hs->xmit_buf = http_response_header;
	hs->xmit_buf_size = sizeof(http_response_header)-1;
      } else {
	uint32_t offset = (hs->data_count-1) * TRANSFER_SIZE;
	uint32_t remain;

	remain = index_html_len - offset;

	uint16_t count = TRANSFER_SIZE;
	if( remain < TRANSFER_SIZE) {
	  count = remain;
	  hs->done = true;
	}

	printf("remain: %d\n", remain);
	printf("count: %d\n", count);
	if( index_html_len <= offset ) {
	  hs->xmit_buf = NULL;
	  uip_close();
	} else {
	  hs->xmit_buf = index_html + offset;
		hs->xmit_buf_size = count;
		//hs->xmit_buf = "test";
	  //hs->xmit_buf_size = 4;
	}
      }
      break;
    default:
      if(hs->data_count == 0) {
	hs->xmit_buf = http_404_header;
	hs->xmit_buf_size = sizeof(http_404_header)-1;
      } else if(hs->data_count == 1) {
	hs->xmit_buf = unknown_request;
	hs->xmit_buf_size = sizeof(unknown_request)-1;
	hs->done = true;
      } else {
	hs->xmit_buf = NULL;
	uip_close();
      }
      break;
    }

    if (hs->xmit_buf != NULL ) {
      uip_send(hs->xmit_buf, hs->xmit_buf_size);
    }
  }
}
Пример #7
0
int rtapi_app_main(void) {
	char name[HAL_NAME_LEN + 1];
	int n, retval;
	char *data, *token;

	num_ports = 1;
	n = 0; // port number... only one for now

	// init driver
	comp_id = hal_init(modname);
	if(comp_id < 0) {
		rtapi_print_msg(RTAPI_MSG_ERR, "%s: ERROR: hal_init() failed\n", modname);
		return -1;
	}

	// allocate port memory
	port_data = hal_malloc(num_ports * sizeof(port_data_t));
	if(port_data == 0) {
		rtapi_print_msg(RTAPI_MSG_ERR, "%s: ERROR: hal_malloc() failed\n", modname);
		hal_exit(comp_id);
		return -1;
	}

	// map control module memory
	configure_control_module();

	// configure userleds
	if(user_leds != NULL) {
		data = user_leds;
		while((token = strtok(data, ",")) != NULL) {
			int led = strtol(token, NULL, 10);

			data = NULL;

			if(user_led_gpio_pins[led].claimed != 0) {
				rtapi_print_msg(RTAPI_MSG_ERR, "%s: ERROR: userled%d is not available as a GPIO.\n", modname, led);
				hal_exit(comp_id);
				return -1;
			}

			// Add HAL pin
			retval = hal_pin_bit_newf(HAL_IN, &(port_data->led_pins[led]), comp_id, "bb_gpio.userled%d", led);

			if(retval < 0) {
				rtapi_print_msg(RTAPI_MSG_ERR, "%s: ERROR: userled %d could not export pin, err: %d\n", modname, led, retval);
				hal_exit(comp_id);
				return -1;
			}

			// Add HAL pin
			retval = hal_pin_bit_newf(HAL_IN, &(port_data->led_inv[led]), comp_id, "bb_gpio.userled%d.invert", led);

			if(retval < 0) {
				rtapi_print_msg(RTAPI_MSG_ERR, "%s: ERROR: userled %d could not export pin, err: %d\n", modname, led, retval);
				hal_exit(comp_id);
				return -1;
			}

			// Initialize HAL pin
			*(port_data->led_inv[led]) = 0;

			int gpio_num = user_led_gpio_pins[led].port_num;
			// configure gpio port if necessary
			if(gpio_ports[gpio_num] == NULL) {
				configure_gpio_port(gpio_num);
			}

			user_led_gpio_pins[led].port = gpio_ports[gpio_num];

			configure_pin(&user_led_gpio_pins[led], 'O');
		}
	}

	// configure input pins
	if(input_pins != NULL) {
		data = input_pins;
		while((token = strtok(data, ",")) != NULL) {
			int pin = strtol(token, NULL, 10);
			int header;
			bb_gpio_pin *bbpin;

			// Fixup old pin numbering scheme:
			// P8/P9 was 1xx/2xx, now 8xx/9xx
			if (pin < 300)
				pin += 700;

			if(pin < 801 || pin > 946 || (pin > 846 && pin < 901)) {
				rtapi_print_msg(RTAPI_MSG_ERR, "%s: ERROR: invalid pin number '%d'.  Valid pins are 801-846 for P8 pins, 901-946 for P9 pins.\n", modname, pin);
				hal_exit(comp_id);
				return -1;
			}

			if(pin < 900) {
				pin -= 800;
				bbpin = &p8_pins[pin];
				header = 8;
			} else {
				pin -= 900;
				bbpin = &p9_pins[pin];
				header = 9;
			}

			if(bbpin->claimed != 0) {
				rtapi_print_msg(RTAPI_MSG_ERR, "%s: ERROR: pin p%d.%02d is not available as a GPIO.\n", modname, header, pin);
				hal_exit(comp_id);
				return -1;
			}

			data = NULL; // after the first call, subsequent calls to strtok need to be on NULL

			// Add HAL pin
			retval = hal_pin_bit_newf(HAL_OUT, &(port_data->input_pins[pin + (header - 8)*PINS_PER_HEADER]), comp_id, "bb_gpio.p%d.in-%02d", header, pin);

			if(retval < 0) {
				rtapi_print_msg(RTAPI_MSG_ERR, "%s: ERROR: pin p%d.%02d could not export pin, err: %d\n", modname, header, pin, retval);
				hal_exit(comp_id);
				return -1;
			}

			// Add HAL pin
			retval = hal_pin_bit_newf(HAL_IN, &(port_data->input_inv[pin + (header - 8)*PINS_PER_HEADER]), comp_id, "bb_gpio.p%d.in-%02d.invert", header, pin);

			if(retval < 0) {
				rtapi_print_msg(RTAPI_MSG_ERR, "%s: ERROR: pin p%d.%02d could not export pin, err: %d\n", modname, header, pin, retval);
				hal_exit(comp_id);
				return -1;
			}

			// Initialize HAL pin
			*(port_data->input_inv[pin + (header - 8)*PINS_PER_HEADER]) = 0;

			int gpio_num = bbpin->port_num;
			
			// configure gpio port if necessary
			if(gpio_ports[gpio_num] == NULL) {
				configure_gpio_port(gpio_num);
			}

			bbpin->port = gpio_ports[gpio_num];

			configure_pin(bbpin, 'U');
			rtapi_print("pin %d maps to pin %d-%d, mode %d\n", pin, bbpin->port_num, bbpin->pin_num, bbpin->claimed);
		}
	}

	// configure output pins
	if(output_pins != NULL) {
		data = output_pins;
		while((token = strtok(data, ",")) != NULL) {
			int pin = strtol(token, NULL, 10);
			int header;
			bb_gpio_pin *bbpin;

			// Fixup old pin numbering scheme:
			// P8/P9 was 1xx/2xx, now 8xx/9xx
			if (pin < 300)
				pin += 700;

			if(pin < 801 || pin > 946 || (pin > 846 && pin < 901)) {
				rtapi_print_msg(RTAPI_MSG_ERR, "%s: ERROR: invalid pin number '%d'.  Valid pins are 801-846 for P8 pins, 901-946 for P9 pins.\n", modname, pin);
				hal_exit(comp_id);
				return -1;
			}

			if(pin < 900) {
				pin -= 800;
				bbpin = &p8_pins[pin];
				header = 8;
			} else {
				pin -= 900;
				bbpin = &p9_pins[pin];
				header = 9;
			}

			if(bbpin->claimed != 0) {
				rtapi_print_msg(RTAPI_MSG_ERR, "%s: ERROR: pin p%d.%02d is not available as a GPIO.\n", modname, header, pin);
				hal_exit(comp_id);
				return -1;
			}

			data = NULL; // after the first call, subsequent calls to strtok need to be on NULL

			// Add HAL pin
			retval = hal_pin_bit_newf(HAL_IN, &(port_data->output_pins[pin + (header - 8)*PINS_PER_HEADER]), comp_id, "bb_gpio.p%d.out-%02d", header, pin);

			if(retval < 0) {
				rtapi_print_msg(RTAPI_MSG_ERR, "%s: ERROR: pin p%d.%02d could not export pin, err: %d\n", modname, header, pin, retval);
				hal_exit(comp_id);
				return -1;
			}

			// Add HAL pin
			retval = hal_pin_bit_newf(HAL_IN, &(port_data->output_inv[pin + (header - 8)*PINS_PER_HEADER]), comp_id, "bb_gpio.p%d.out-%02d.invert", header, pin);

			if(retval < 0) {
				rtapi_print_msg(RTAPI_MSG_ERR, "%s: ERROR: pin p%d.%02d could not export pin, err: %d\n", modname, header, pin, retval);
				hal_exit(comp_id);
				return -1;
			}

			// Initialize HAL pin
			*(port_data->output_inv[pin + (header - 8)*PINS_PER_HEADER]) = 0;

			int gpio_num = bbpin->port_num;
			
			// configure gpio port if necessary
			if(gpio_ports[gpio_num] == NULL) {
				configure_gpio_port(gpio_num);
			}

			bbpin->port = gpio_ports[gpio_num];

			configure_pin(bbpin, 'O');
		}
	}


	// export functions
	rtapi_snprintf(name, sizeof(name), "bb_gpio.write");
	retval = hal_export_funct(name, write_port, port_data, 0, 0, comp_id);
	if(retval < 0) {
		rtapi_print_msg(RTAPI_MSG_ERR, "%s: ERROR: port %d write funct export failed\n", modname, n);
		hal_exit(comp_id);
		return -1;
	}
	
	rtapi_snprintf(name, sizeof(name), "bb_gpio.read");
	retval = hal_export_funct(name, read_port, port_data, 0, 0, comp_id);
	if(retval < 0) {
		rtapi_print_msg(RTAPI_MSG_ERR, "%s: ERROR: port %d read funct export failed\n", modname, n);
		hal_exit(comp_id);
		return -1;
	}

	rtapi_print_msg(RTAPI_MSG_INFO, "%s: installed driver\n", modname);

	hal_ready(comp_id);

	return 0;
}