Ejemplo n.º 1
0
/*----------------------------------------------------------------------------*/
void uartConfigPins(struct UartBase *interface,
    const struct UartBaseConfig *config)
{
  /* Direction configuration is not needed for alternate function pins */

  if (config->rx)
  {
    /* Configure UART RX pin */
    const struct PinEntry * const pinEntry = pinFind(uartPins,
        config->rx, interface->channel);
    assert(pinEntry);

    const struct Pin pin = pinInit(config->rx);

    pinInput(pin);
    pinSetFunction(pin, pinEntry->value);
  }

  if (config->tx)
  {
    /* Configure UART TX pin */
    const struct PinEntry * const pinEntry = pinFind(uartPins,
        config->tx, interface->channel);
    assert(pinEntry);

    const struct Pin pin = pinInit(config->tx);

    pinInput(pin);
    pinSetFunction(pin, pinEntry->value);
  }
}
Ejemplo n.º 2
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;
}
Ejemplo n.º 3
0
void init( int argc, char * argv[] )
{
    if(argc > 1)
        if(Serial.init_tty(argv[1]) != 0)
            return;

    if(Serial1.init_tty(LINUX_SERIAL1_TTY) != 0)
        return;
    if(Serial2.init_tty(LINUX_SERIAL2_TTY) != 0)
        return;

    sizeof_g_APinDescription = sizeof(g_APinDescription)/sizeof(struct _PinDescription);
    sizeof_g_APinState = sizeof(g_APinState)/sizeof(struct _PinState);
    pinInit();

    /* Initialize fast path to GPIO */
    if (fastGpioPciInit())
        trace_error("Unable to initialize fast GPIO mode!");

    sizeof_g_APwmDescription = sizeof(g_APwmDescription)/sizeof(struct _PwmDescription);
    pwmInit();

    sizeof_g_AdcDescription = sizeof(g_AdcDescription)/sizeof(struct _AdcDescription);
    adcInit();

    eepromInit();
}
Ejemplo n.º 4
0
/*----------------------------------------------------------------------------*/
int main(void)
{
  const struct Pin led = pinInit(LED_PIN);
  pinOutput(led, false);

  struct Interface * const adc = init(AdcOneShot, &adcConfig);
  assert(adc);

  struct Timer * const timer = init(GpTimer, &timerConfig);
  assert(timer);
  timerSetOverflow(timer, 1000);

  bool event = false;
  timerSetCallback(timer, onTimerOverflow, &event);
  timerEnable(timer);

  while (1)
  {
    while (!event)
      barrier();
    event = false;

    pinSet(led);

    uint16_t voltage;
    const size_t bytesRead = ifRead(adc, &voltage, sizeof(voltage));
    assert(bytesRead == sizeof(voltage));
    (void)bytesRead; /* Suppress warning */

    pinReset(led);
  }

  return 0;
}
Ejemplo n.º 5
0
/*----------------------------------------------------------------------------*/
int main(void)
{
  const uint32_t maxPeriod =
      captureUnitConfig.frequency / pwmUnitConfig.frequency + 1;
  const uint32_t minPeriod =
      captureUnitConfig.frequency / pwmUnitConfig.frequency - 1;

  setupClock();

  const struct Pin led = pinInit(LED_PIN);
  pinOutput(led, false);

  struct GpTimerPwmUnit * const pwmUnit = init(GpTimerPwmUnit,
      &pwmUnitConfig);
  assert(pwmUnit);

  struct Pwm * const pwm = gpTimerPwmCreate(pwmUnit, OUTPUT_PIN);
  assert(pwm);
  pwmSetEdges(pwm, 0, pwmGetResolution(pwm) / 2);

  struct GpTimerCaptureUnit * const captureUnit = init(GpTimerCaptureUnit,
      &captureUnitConfig);
  assert(pwmUnit);

  struct Capture * const capture = gpTimerCaptureCreate(captureUnit, INPUT_PIN,
      PIN_RISING, PIN_PULLDOWN);
  assert(capture);

  uint32_t previousTime = 0;
  bool event = false;

  captureSetCallback(capture, onCaptureEvent, &event);
  captureEnable(capture);
  pwmEnable(pwm);

  while (1)
  {
    while (!event)
      barrier();
    event = false;

    const uint32_t currentTime = captureGetValue(capture);
    const uint32_t period = currentTime - previousTime;

    if (period >= minPeriod && period <= maxPeriod)
      pinReset(led);
    else
      pinSet(led);

    previousTime = currentTime;
  }

  return 0;
}
Ejemplo n.º 6
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;
}
Ejemplo n.º 7
0
Archivo: gppwm.c Proyecto: stxent/halm
/*----------------------------------------------------------------------------*/
static uint8_t configMatchPin(uint8_t channel, PinNumber key)
{
  const struct PinEntry * const pinEntry = pinFind(gpPwmPins, key, channel);
  assert(pinEntry);

  const struct Pin pin = pinInit(key);

  pinOutput(pin, false);
  pinSetFunction(pin, UNPACK_FUNCTION(pinEntry->value));

  return UNPACK_CHANNEL(pinEntry->value);
}
Ejemplo n.º 8
0
/*********************************************************************************************************
** Function name:       main
** Descriptions:        用户程序入口函数
** input parameters:    无
** output parameters:   无
** Returned value:      无
*********************************************************************************************************/
int main(void)
{
    targetInit();                                                       /* 初始化目标板,切勿删除       */
    pinInit();                                                          /* 引脚初始化                   */                                                                     
    BEEP_INIT();
    BEEP_OFF();
    while (1) {
        BEEP_OFF();
        myDelay(200);
        BEEP_ON();
        myDelay(200);
    }
}
Ejemplo n.º 9
0
/*----------------------------------------------------------------------------*/
int main(void)
{
  setupClock();

  const struct Pin led = pinInit(LED_PIN);
  pinOutput(led, false);

  struct Interface * const adc = init(AdcDma, &adcConfig);
  assert(adc);
  ifSetCallback(adc, onConversionCompleted, adc);

  struct Timer * const conversionTimer = init(GpTimer, &timerConfig);
  assert(conversionTimer);
  /*
   * The overflow frequency of the timer should be two times higher
   * than that of the hardware events for ADC.
   */
  timerSetOverflow(conversionTimer, timerConfig.frequency / (ADC_RATE * 2));

  unsigned int iteration = 0;
  size_t count;

  /* Enqueue buffers */
  while (ifGetParam(adc, IF_AVAILABLE, &count) == E_OK && count > 0)
  {
    const size_t index = iteration++ % BUFFER_COUNT;
    ifRead(adc, buffers[index], sizeof(buffers[index]));
  }

  /* Start conversion */
  timerEnable(conversionTimer);

  while (1)
  {
    while (!event)
      barrier();
    event = false;

    pinSet(led);
    while (ifGetParam(adc, IF_AVAILABLE, &count) == E_OK && count > 0)
    {
      const size_t index = iteration++ % BUFFER_COUNT;
      ifRead(adc, buffers[index], sizeof(buffers[index]));
    }
    pinReset(led);
  }

  return 0;
}
Ejemplo n.º 10
0
/*----------------------------------------------------------------------------*/
int main(void)
{
  const struct Pin led = pinInit(LED_PIN);
  pinOutput(led, false);

  struct Interface * const flash = init(Flash, 0);
  assert(flash);

  size_t flashSize, pageSize;
  enum Result res;

  pinSet(led);
  if ((res = ifGetParam(flash, IF_SIZE, &flashSize)) == E_OK)
    pinReset(led);
  assert(res == E_OK);

  pinSet(led);
  if ((res = ifGetParam(flash, IF_FLASH_PAGE_SIZE, &pageSize)) == E_OK)
    pinReset(led);
  assert(res == E_OK);

  uint8_t * const buffer = malloc(pageSize);
  for (size_t i = 0; i < pageSize; ++i)
    buffer[i] = i;

  /* Test sector erase */
  const size_t address = findNearestSector();
  assert(address < flashSize);

  pinSet(led);
  if ((res = ifSetParam(flash, IF_FLASH_ERASE_SECTOR, &address)) == E_OK)
    pinReset(led);
  assert(res == E_OK);

  pinSet(led);
  if ((res = program(flash, buffer, pageSize, address)) == E_OK)
    pinReset(led);
  assert(res == E_OK);

  pinSet(led);
  if ((res = verify(flash, buffer, pageSize, address)) == E_OK)
    pinReset(led);
  assert(res == E_OK);

  /* Page erase is not available on some parts */

  while (1);
  return 0;
}
Ejemplo n.º 11
0
/*----------------------------------------------------------------------------*/
static void deviceInit(struct DeviceDriver *device, struct Interface *interface,
    PinNumber ledNumber, uint16_t address)
{
  device->interface = interface;
  device->state = DEVICE_IDLE;
  device->desiredRate = DEVICE_CLOCK;
  device->localAddress = 0;
  device->deviceAddress = address;
  device->change = false;

  static_assert(MEMORY_ADDRESS_SIZE && MEMORY_ADDRESS_SIZE <= 2,
      "Incorrect address size");
  if (MEMORY_ADDRESS_SIZE == 2)
    device->localAddress = toBigEndian16(device->localAddress);

  device->led = pinInit(ledNumber);
  pinOutput(device->led, false);
}
Ejemplo n.º 12
0
/*----------------------------------------------------------------------------*/
int main(void)
{
  setupClock();

  const struct Pin led = pinInit(LED_PIN);
  pinOutput(led, true);

  struct GpTimerPwmUnit * const pwmUnit = init(GpTimerPwmUnit, &pwmUnitConfig);
  assert(pwmUnit);
  struct Pwm * const pwmOutput = gpTimerPwmCreate(pwmUnit, OUTPUT_PIN);
  assert(pwmOutput);
  pwmSetDuration(pwmOutput, pwmGetResolution(pwmOutput) / 2);

  struct Timer * const counter = init(GpTimerCounter, &counterConfig);
  assert(counter);

  struct Timer * const timer = init(GpTimer, &timerConfig);
  assert(timer);
  timerSetOverflow(timer, 1000);

  bool event = false;
  timerSetCallback(timer, onTimerOverflow, &event);

  timerEnable(counter);
  pwmEnable(pwmOutput);
  timerEnable(timer);

  while (1)
  {
    while (!event)
      barrier();
    event = false;

    const uint32_t period = timerGetValue(counter);
    timerSetValue(counter, 0);

    if (period >= 999 && period <= 1001)
      pinReset(led);
    else
      pinSet(led);
  }

  return 0;
}
Ejemplo n.º 13
0
void init( int argc, char * argv[] )
{
	if(argc > 1)
		if(Serial.init_tty(argv[1]) != 0)
			return;

	if(Serial1.init_tty(LINUX_SERIAL1_TTY) != 0)
		return;

	if(Serial2.init_tty(LINUX_SERIAL2_TTY) != 0)
		return;

	sizeof_g_APinDescription = sizeof(g_APinDescription)/sizeof(struct _PinDescription);
	pinInit();
	sizeof_g_APwmDescription = sizeof(g_APwmDescription)/sizeof(struct _PwmDescription);
	pwmInit();
	sizeof_g_AdcDescription = sizeof(g_AdcDescription)/sizeof(struct _AdcDescription);
	adcInit();
	sizeof_g_APinState = sizeof(g_APinState)/sizeof(struct _PinState);
}
Ejemplo n.º 14
0
/*----------------------------------------------------------------------------*/
int main(void)
{
  /* Initialize peripherals */
  struct Pin led = pinInit(LED_PIN);
  pinOutput(led, false);

  struct Timer * const timer = init(SysTickTimer, 0);
  assert(timer);
  timerSetOverflow(timer, timerGetFrequency(timer) / 2);
  timerSetCallback(timer, onTimerOverflow, &led);

  /* Initialize Work Queue */
  workQueueInit(WORK_QUEUE_SIZE);

  /* Start event generation and queue handler */
  timerEnable(timer);
  workQueueStart(0);

  return 0;
}
Ejemplo n.º 15
0
/*----------------------------------------------------------------------------*/
static void configPins(struct I2sBase *interface,
    const struct I2sBaseConfig *config)
{
  const pinNumber pinArray[] = {
      config->rx.sck, config->rx.ws, config->rx.sda, config->rx.mclk,
      config->tx.sck, config->tx.ws, config->tx.sda, config->tx.mclk
  };

  for (unsigned int index = 0; index < ARRAY_SIZE(pinArray); ++index)
  {
    if (pinArray[index])
    {
      const struct PinEntry * const pinEntry = pinFind(i2sPins, pinArray[index],
          interface->channel);
      assert(pinEntry);

      const struct Pin pin = pinInit(pinArray[index]);

      pinInput(pin);
      pinSetFunction(pin, pinEntry->value);
    }
  }
}
Ejemplo n.º 16
0
/*----------------------------------------------------------------------------*/
int main(void)
{
  static const uint32_t address = 0;
  uint8_t buffer[384];

  for (size_t i = 0; i < sizeof(buffer); ++i)
    buffer[i] = i;

  const struct Pin led = pinInit(LED_PIN);
  pinOutput(led, false);

  struct Interface * const eeprom = init(Eeprom, 0);
  assert(eeprom);

  uint32_t size;
  enum Result res;

  pinSet(led);
  if ((res = ifGetParam(eeprom, IF_SIZE, &size)) == E_OK)
    pinReset(led);
  assert(res == E_OK);

  assert(address < size);

  pinSet(led);
  if ((res = program(eeprom, buffer, sizeof(buffer), address)) == E_OK)
    pinReset(led);
  assert(res == E_OK);

  pinSet(led);
  if ((res = verify(eeprom, buffer, sizeof(buffer), address)) == E_OK)
    pinReset(led);
  assert(res == E_OK);

  while (1);
  return 0;
}
Ejemplo n.º 17
0
/*----------------------------------------------------------------------------*/
int main(void)
{
  const struct Pin led = pinInit(LED_PIN);
  pinOutput(led, true);

  struct Timer * const timer = init(GpTimer, &timerConfig);
  assert(timer);
  timerSetOverflow(timer, 500);

  bool event = false;
  timerSetCallback(timer, onTimerOverflow, &event);
  timerEnable(timer);

  while (1)
  {
    while (!event)
      barrier();
    event = false;

    pinToggle(led);
  }

  return 0;
}
Ejemplo n.º 18
0
/*----------------------------------------------------------------------------*/
static void configPins(struct SdmmcBase *interface,
                       const struct SdmmcBaseConfig *config)
{
    const pinNumber pinArray[] = {
        config->clk,
        config->cmd,
        config->dat0,
        config->dat1,
        config->dat2,
        config->dat3
    };
    bool wide = true;

    for (unsigned int index = 0; index < ARRAY_SIZE(pinArray); ++index)
    {
        if (!pinArray[index])
        {
            /* First three pins are mandatory */
            assert(index >= 3);

            wide = false;
            continue;
        }

        const struct PinEntry * const pinEntry = pinFind(sdmmcPins,
                pinArray[index], 0);
        assert(pinEntry);

        const struct Pin pin = pinInit(pinArray[index]);

        pinInput(pin);
        pinSetFunction(pin, pinEntry->value);
    }

    interface->wide = wide;
}
Ejemplo n.º 19
0
    .init = oneWireInit,
    .deinit = oneWireDeinit,

    .callback = oneWireCallback,
    .get = oneWireGet,
    .set = oneWireSet,
    .read = oneWireRead,
    .write = oneWireWrite
};
/*----------------------------------------------------------------------------*/
const struct InterfaceClass * const OneWireUart = &oneWireTable;
/*----------------------------------------------------------------------------*/
static void adjustPins(struct OneWireUart *interface __attribute__((unused)),
                       const struct OneWireUartConfig *config)
{
    pinSetType(pinInit(config->tx), PIN_OPENDRAIN);
}
/*----------------------------------------------------------------------------*/
static void beginTransmission(struct OneWireUart *interface)
{
    LPC_UART_Type * const reg = interface->base.reg;

    uartSetRate((struct UartBase *)interface, interface->resetRate);
    interface->state = STATE_RESET;
    /* Clear RX FIFO and set trigger level to 1 character */
    reg->FCR |= FCR_RX_RESET | FCR_RX_TRIGGER(RX_TRIGGER_LEVEL_1);
    reg->THR = 0xF0; /* Execute reset */
}
/*----------------------------------------------------------------------------*/
static void sendWord(struct OneWireUart *interface, uint8_t word)
{
Ejemplo n.º 20
0
/* Constructor */
MAX3421E::MAX3421E()
{
    spi_init();  
	pinInit();
}
Ejemplo n.º 21
0
int main(void){
	pinInit();
	rcInterruptInit();
	timerInit();
	
    while(1){
		// update loop every 1 ms
		if(micros() - loopTimer > LOOPTIME){
			loopTimer = micros();
			
			// check the current pulse reading for validity
			if(checkPulse(pw)){
				lastPw = pw;
			}else{
				pwGood = 0;
				pw = lastPw;
			}
			
			// bound the pulse width
			if(pw > RCMAX){
				pw = RCMAX;
			}else if(pw < RCMIN){
				pw = RCMIN;
			}
			
			// process the pulse width for output
			if(pwGood){
				rawCmd = mWRADB(pw, rawCmd, RCMIN, RCMAX, RCMID, -MAXPWM, MAXPWM, 0, RCDB, 2);
			}
			
			// get direction, 1 is forward
			if(rawCmd >= 0){
				dir = 1;
			}else{
				dir = 0;
			}
			
			cmd = rawCmd;
			// get magnitude of cmd
			if(!dir){
				cmd = -(cmd);
			}
			
			// exponential lookup and limit output
			cmd = exp1x[cmd];
			if(cmd > MAXPWM){
				cmd = MAXPWM;
			}
			
			// if failsafe isnt true, send output to motor
			if(!failsafe){
				if(dir){
					OCR1A = cmd;
					PORTD |= DIRA1;
					PORTD &= ~(DIRA2);
				}else{
					OCR1A = cmd;
					PORTD &= ~(DIRA1);
					PORTD |= DIRA2;
				}
			}else{
				// else shut off the pwm
				OCR1A = 0;
			}
			
			// failsafe check
			if(micros() - fsTimer > FSTIME){
				fsTimer = micros();
				
				if(pwGood){
					PORTD |= (SDPINA | SDPINB);
					failsafe = 0;
				}else{
					// shutdown the gate drivers
					PORTD &= ~(SDPINA | SDPINB);
					failsafe = 1;
				}
				
				pwGood = 0;
			}
		}
    }
}