Example #1
0
bool spi_init(struct spi_bus *bus)
{
#ifdef DEBUG_LED
  /* Grab a debug LED for testing. */
  pin_enable(DEBUG_LED);
  pin_reset(DEBUG_LED);
  pin_set_mode(DEBUG_LED, PIN_MODE_OUTPUT);
  pin_set_otype(DEBUG_LED, PIN_TYPE_PUSHPULL);
  pin_set_ospeed(DEBUG_LED, PIN_SPEED_2MHZ);
#endif

#ifdef ERROR_LED
  pin_enable(ERROR_LED);
  pin_reset(ERROR_LED);
  pin_set_mode(ERROR_LED, PIN_MODE_OUTPUT);
  pin_set_otype(ERROR_LED, PIN_TYPE_PUSHPULL);
  pin_set_ospeed(ERROR_LED, PIN_SPEED_2MHZ);
#endif

  rcc_enable(bus->rcc_dev);

  /* Configure GPIO pins for SPI operation. */
  pin_enable(bus->miso_pin);
  pin_set_af(bus->miso_pin, bus->af);
  pin_set_mode(bus->miso_pin, PIN_MODE_AF);
  pin_set_pupd(bus->miso_pin, PIN_PUPD_NONE);

  pin_enable(bus->mosi_pin);
  pin_set_af(bus->mosi_pin, bus->af);
  pin_set_mode(bus->mosi_pin, PIN_MODE_AF);
  pin_set_otype(bus->mosi_pin, PIN_TYPE_PUSHPULL);

  pin_enable(bus->sck_pin);
  pin_set_af(bus->sck_pin, bus->af);
  pin_set_mode(bus->sck_pin, PIN_MODE_AF);
  pin_set_otype(bus->sck_pin, PIN_TYPE_PUSHPULL);

  bus->lock = xSemaphoreCreateMutex();
  if (bus->lock == NULL)
    goto fail;

  vSemaphoreCreateBinary(bus->complete);
  if (bus->complete == NULL)
    goto fail;

  /* Take the semaphore initially so we will block next time. */
  xSemaphoreTake(bus->complete, portMAX_DELAY);

  interrupt_set_priority(bus->irq, INTERRUPT_PRIORITY_FREERTOS_SAFE);
  return true;

fail:
  if (bus->lock != NULL)
    vSemaphoreDelete(bus->lock);
  if (bus->complete != NULL)
    vSemaphoreDelete(bus->complete);

  return false;
}
Example #2
0
static void red_led(int on) {
	if (on) {
		pin_set(pin_e1);
	} else {
		pin_reset(pin_e1);
	}
}
Example #3
0
/** Deselect a device, de-asserting its chip select pin. */
static inline void spi_device_deselect(struct spi_device *dev)
{
  if (dev->chip_select_active)
    pin_reset(dev->chip_select);
  else
    pin_set(dev->chip_select);
}
Example #4
0
static void green_led(int on) {
	if (on) {
		pin_set(pin_e0);
	} else {
		pin_reset(pin_e0);
	}
}
Example #5
0
void Initialize() {
  short int i = 0;
  // variable initialization
  LQI = 0;
  RSSI2 = 0;
  SEQ_NUMBER = 0x23;
  lost_data = 0;
  address_RX_FIFO = 0x300;
  address_TX_normal_FIFO = 0;

  for (i = 0; i < 2; i++) {
    ADDRESS_short_1[i] = 1;
    ADDRESS_short_2[i] = 2;
    PAN_ID_1[i] = 3;
    PAN_ID_2[i] = 3;
  }

  for (i = 0; i < 8; i++) {
    ADDRESS_long_1[i] = 1;
    ADDRESS_long_2[i] = 2;
  }

  // Set Chip Select pin as Output
  GPIO_Digital_Output(&GPIOD_BASE, _GPIO_PINMASK_13);
  GPIO_Digital_Output(&GPIOD_BASE, _GPIO_PINMASK_10);
  GPIO_Digital_Output(&GPIOC_BASE, _GPIO_PINMASK_2);

  // Set PD0 and PD1 as digital input
  GPIO_Digital_Input(&GPIOD_BASE, _GPIO_PINMASK_0);


  Delay_ms(5);

  // SPI config
  SPI3_Init_Advanced(_SPI_FPCLK_DIV4, _SPI_MASTER  | _SPI_8_BIT |
                     _SPI_CLK_IDLE_LOW | _SPI_FIRST_CLK_EDGE_TRANSITION |
                     _SPI_MSB_FIRST | _SPI_SS_DISABLE | _SPI_SSM_ENABLE | _SPI_SSI_1,
                     &_GPIO_MODULE_SPI3_PC10_11_12);
  TFT_BLED = 1;                             // Turn on TFT Backlight

  TFT_Init(320, 240);                       // Initialize TFT display
  TFT_Fill_Screen(CL_WHITE);                // Clear Screen

  pin_reset();                              // Activate reset from pin
  software_reset();                         // Activate software reset
  RF_reset();                               // RF reset
  set_WAKE_from_pin();                      // Set wake from pin

  set_long_address(ADDRESS_long_2);         // Set long address
  set_short_address(ADDRESS_short_2);       // Set short address
  set_PAN_ID(PAN_ID_2);                     // Set PAN_ID

  init_ZIGBEE_nonbeacon();                  // Initialize ZigBee module
  nonbeacon_PAN_coordinator_device();
  set_TX_power(31);                         // Set max TX power
  set_frame_format_filter(1);               // 1 all frames, 3 data frame only
  set_reception_mode(1);                    // 1 normal mode

  pin_wake();                               // Wake from pin
}
Example #6
0
static void led_pin_setup(struct pin *pin) {
	pin_enable(pin);
	pin_set_mode(pin, PIN_MODE_OUTPUT);
	pin_set_otype(pin, PIN_TYPE_PUSHPULL);
	pin_set_ospeed(pin, PIN_SPEED_2MHZ);
	pin_set_pupd(pin, PIN_PUPD_NONE);
	pin_reset(pin);
}
Example #7
0
PinHold::~PinHold()
{
  if( !skip ) {
    if( v ) {
      pin_reset( p );
    } else {
      pin_set( p );
    }
  }
}
Example #8
0
PinHold::PinHold( PinPlace *a_pin, bool val, bool a_skip )
  : p( a_pin ), v( val ), skip( a_skip )
{
  if( !skip ) {
    if( v ) {
      pin_set( p );
    } else {
      pin_reset( p );
    }
  }
}
Example #9
0
/**
 * Handle an IRQ for the given SPI bus.
 *
 * This is actually quite tricky---be very careful making changes to
 * this ISR.
 *
 * It's important to note here that both RXNE and TXE can be set in
 * the status register in the same interrupt.  This can happen when
 * another task disables interrupts during a transfer.
 *
 * To make sure that we never drop any received bytes, we always
 * alternate between TXing and RXing by toggling the TX/RX interrupts
 * as they occur.
 *
 * This makes us slightly less than optimal, as we could load the
 * second byte into the TX register before we've RX'ed the first byte,
 * but the additional complexity doesn't warrant complicating this ISR
 * any further until we need the extra performance.
 */
static void spi_irq(struct spi_bus *bus)
{
  portBASE_TYPE should_yield = pdFALSE;
  uint32_t sr = bus->dev->SR;

#ifdef DEBUG_LED
  pin_set(DEBUG_LED);
#endif

  if (sr & SPI_SR_RXNE) {
    *bus->transfer.rx_buf = bus->dev->DR;
    ++bus->transfer.rx_buf;
    --bus->transfer.rx_len;

    if (bus->transfer.rx_len == 0) {
      spi_cr2_clear(bus, SPI_CR2_RXNEIE);
      xSemaphoreGiveFromISR(bus->complete, &should_yield);
    } else {
      spi_cr2_set(bus, SPI_CR2_TXEIE);
    }
  } else if (sr & SPI_SR_TXE) {
    spi_cr2_clear(bus, SPI_CR2_TXEIE);

    if (bus->transfer.tx_len != 0) {
      spi_cr2_set(bus, SPI_CR2_RXNEIE);
      bus->dev->DR = *bus->transfer.tx_buf;

      ++bus->transfer.tx_buf;
      --bus->transfer.tx_len;
    }
  }

  if (sr & SPI_ERROR_BITS) {
#ifdef ERROR_LED
    pin_set(ERROR_LED);
#endif
  }

#ifdef DEBUG_LED
  pin_reset(DEBUG_LED);
#endif

  if (should_yield == pdTRUE)
    taskYIELD();
}