示例#1
0
/*----------------------------------------------------------------------------*/
static enum Result interfaceInit(void *object, const void *configBase)
{
  const struct InterfaceWrapperConfig * const config = configBase;
  struct InterfaceWrapper * const interface = object;

  interface->pipe = config->pipe;
  interface->rx = pinInit(config->rx);
  assert(pinValid(interface->rx));
  pinOutput(interface->rx, 0);
  interface->tx = pinInit(config->tx);
  assert(pinValid(interface->tx));
  pinOutput(interface->tx, 0);

  return E_OK;
}
// digitalRead([pin])
void ICACHE_FLASH_ATTR
at_setupReadPinCmd(uint8_t id, char *pPara)
{
    int result = 0, err = 0, flag = 0;
    uint8 buffer[32] = {0};
    pPara++; // skip '='

    //get the first parameter (uint8_t)
    // digit
    flag = at_get_next_int_dec(&pPara, &result, &err);

    // flag must be true because there are more parameters
    //if ((flag == FALSE) && (result > 0) )
	if (err > 0)
	{
		at_port_print("Bad input");
        at_response_error();
        return;
    }
	
	uint8_t pin = result;
	
	// Don't go any further if the pin is un-usable or non-existant
	if (!pinValid(pin))
	{
		at_response_error();
		return;
	}
	
	uint8_t value = GPIO_INPUT_GET(pin);
	os_sprintf(buffer, "%d\r\n", value);
	
	at_port_print(buffer);
	at_response_ok();
}
示例#3
0
/*----------------------------------------------------------------------------*/
static enum Result pinInterruptInit(void *object, const void *configBase)
{
  const struct PinInterruptConfig * const config = configBase;
  assert(config);

  const struct Pin input = pinInit(config->pin);
  assert(pinValid(input));

  /* Try to allocate a new channel */
  struct PinInterrupt * const interrupt = object;
  const int channel = setInstance(interrupt);

  if (channel == -1)
    return E_BUSY;

  /* Configure the pin */
  pinInput(input);
  pinSetPull(input, config->pull);

  interrupt->callback = 0;
  interrupt->channel = channel;
  interrupt->enabled = false;
  interrupt->event = config->event;
  interrupt->pin = input.data;

  const uint8_t index = interrupt->channel >> 2;
  const uint32_t mask = 1UL << interrupt->channel;

  /* Enable peripheral */
  if (!sysClockStatus(CLK_PINT))
    sysClockEnable(CLK_PINT);

  /* Select pin and port */
  LPC_SYSCON->PINTSEL[index] =
      (LPC_SYSCON->PINTSEL[index] & ~PINTSEL_CHANNEL_MASK(channel))
      | PINTSEL_CHANNEL(channel, input.data.port, input.data.offset);
  /* Configure interrupt as edge sensitive */
  LPC_GPIO_INT->ISEL &= ~mask;
  /* Configure edge sensitivity options */
  if (config->event == PIN_RISING || config->event == PIN_TOGGLE)
    LPC_GPIO_INT->SIENR = mask;
  if (config->event == PIN_FALLING || config->event == PIN_TOGGLE)
    LPC_GPIO_INT->SIENF = mask;

#ifdef CONFIG_PM
  /* Interrupt will wake the controller from low-power modes */
  LPC_SYSCON->STARTERP0 |= STARTERP0_PINT(interrupt->channel);
#endif

  /* Configure interrupt priority, interrupt is disabled by default */
  irqSetPriority(calcVector(interrupt->channel), config->priority);

  return E_OK;
}
// digitalWrite([pin], [value])
void ICACHE_FLASH_ATTR
at_setupWritePinCmd(uint8_t id, char *pPara)
{
    int result = 0, err = 0, flag = 0;
    uint8 buffer[32] = {0};
    pPara++; // skip '='

    //get the first parameter (uint8_t)
    // digit
    flag = at_get_next_int_dec(&pPara, &result, &err);

    // flag must be true because there are more parameters
    if ((flag == FALSE) && (result > 0) )
	{
        at_response_error();
        return;
    }
	
	uint8_t pin = result;
	
	// Don't go any further if the pin is un-usable or non-existant
	if (!pinValid(pin))
	{
		at_response_error();
		return;
	}
	
	if (*pPara++ != ',') { // skip ','
		at_response_error();
		return;
	}
	
	char value = *pPara++;
	
	if ((value == 'h') || (value == 'H') || (value == '1'))
	{
		GPIO_OUTPUT_SET(pin, 1);
		os_sprintf(buffer, "%d=HIGH\r\n", pin);
	}
	else if ((value == 'l') || (value == 'L') || (value == '0'))
	{
		GPIO_OUTPUT_SET(pin, 0);
		os_sprintf(buffer, "%d=LOW\r\n", pin);
	}
	else
	{
		at_response_error();
		return;
	}
	
	at_port_print(buffer);
	at_response_ok();
}
// pinMode([pin], [direction])
void ICACHE_FLASH_ATTR
at_setupPinModeCmd(uint8_t id, char *pPara)
{
    int result = 0, err = 0, flag = 0;
    uint8 buffer[32] = {0};
    pPara++; // skip '='

    //get the first parameter (uint8_t)
    // digit
    flag = at_get_next_int_dec(&pPara, &result, &err);

    // flag must be true because there are more parameters
    if ((flag == FALSE) && (result > 0) )
	{
        at_response_error();
        return;
    }
	
	uint8_t pin = result;
	
	// Don't go any further if the pin is un-usable or non-existant
	if (!pinValid(pin))
	{
		at_response_error();
		return;
	}
	
	if (*pPara++ != ',') { // skip ','
		at_response_error();
		return;
	}
	
	char mode = *pPara++;
	
	if ((mode == 'i') || (mode == 'I'))
	{
		if (pin == STATUS_LED_PIN)
		{
			wifi_status_led_uninstall();
		}
		PIN_FUNC_SELECT(pinMap[pin], pinFunction[pin]);
		GPIO_DIS_OUTPUT(pin);
		PIN_PULLUP_DIS(pinMap[pin]);
		os_sprintf(buffer, "%d=INPUT\r\n", pin);
	}
	else if ((mode == 'o') || (mode == 'O'))
	{
		if (pin == STATUS_LED_PIN)
		{
			wifi_status_led_uninstall();
		}
		PIN_FUNC_SELECT(pinMap[pin], pinFunction[pin]);
		GPIO_OUTPUT_SET(pin, 0);
		os_sprintf(buffer, "%d=OUTPUT\r\n", pin);
	}
	else if ((mode == 'p') || (mode == 'P'))
	{
		if (pin == STATUS_LED_PIN)
		{
			wifi_status_led_uninstall();
		}
		PIN_FUNC_SELECT(pinMap[pin], pinFunction[pin]);
		GPIO_DIS_OUTPUT(pin);
		PIN_PULLUP_EN(pinMap[pin]);
		os_sprintf(buffer, "%d=INPUT_PULLUP\r\n", pin);
	}
	else
	{
		at_response_error();
		return;
	}
	
	at_port_print(buffer);
	at_response_ok();
}