Пример #1
0
void mainLoop(void)
{
    bool resetToFw = false;
    static CrtpPacket crtpPacket;
    static bool stmStarted = false;

    while(!resetToFw) {
        EsbPacket *packet;
        buttonProcess();

        if (buttonGetState() != buttonIdle) {
            resetToFw = true;
        }

        if ((stmStarted == false) && (nrf_gpio_pin_read(UART_RX_PIN))) {
            nrf_gpio_cfg_input(UART_RTS_PIN, NRF_GPIO_PIN_NOPULL);
            uartInit();
            stmStarted = true;
        }


        if (cstate != connect_ble) {
            packet = esbGetRxPacket();
            if (packet != NULL) {

                if ( ((packet->size >= 2) &&
                        (packet->data[0]==0xff) &&
                        (packet->data[1]==0xff)) ||
                        ((packet->size >= 2) &&
                         (packet->data[0]==0xff) &&
                         (packet->data[1]==0xfe))
                   ) {
                    // Disable Bluetooth advertizing when receiving a bootloader SB packet
                    if (cstate == connect_idle) {
                        //sd_ble_gap_adv_stop();
                        cstate = connect_sb;
                    }
                }

                // If we are connected SB, the packet is read and used
                if (cstate == connect_sb) {
                    memcpy(crtpPacket.raw, packet->data, packet->size);
                    crtpPacket.datalen = packet->size-1;
                }
                esbReleaseRxPacket(packet);
            }
        }
        if (cstate != connect_sb) {
            if (bleCrazyfliesIsPacketReceived()) {
                cstate = connect_ble;

                packet = bleCrazyfliesGetRxPacket();
                memcpy(crtpPacket.raw, packet->data, packet->size);
                crtpPacket.datalen = packet->size-1;
                bleCrazyfliesReleaseRxPacket(packet);
            }
        }

        if (crtpPacket.datalen != 0xffu) {
            struct syslinkPacket slPacket;
            slPacket.type = SYSLINK_RADIO_RAW;
            memcpy(slPacket.data, crtpPacket.raw, crtpPacket.datalen+1);
            slPacket.length = crtpPacket.datalen+1;

            if (bootloaderProcess(&crtpPacket) == false) {
                // Send packet to stm32
                syslinkSend(&slPacket);

                crtpPacket.datalen = 0xFFU;
                // If packet received from stm32, send it back
                if (syslinkReceive(&slPacket)) {
                    if (slPacket.type == SYSLINK_RADIO_RAW) {
                        memcpy(crtpPacket.raw, slPacket.data, slPacket.length);
                        crtpPacket.datalen = slPacket.length-1;
                    }
                }
            }
        }
        if (crtpPacket.datalen != 0xFFU) {
            if (cstate == connect_sb) {
                EsbPacket *pk = esbGetTxPacket();
                if (pk) {
                    memcpy(pk->data, crtpPacket.raw, crtpPacket.datalen+1);
                    pk->size = crtpPacket.datalen+1;
                    esbSendTxPacket(pk);
                }
            } else if (cstate == connect_ble) {
                static EsbPacket pk;
                memcpy(pk.data, crtpPacket.raw, crtpPacket.datalen+1);
                pk.size = crtpPacket.datalen+1;
                bleCrazyfliesSendPacket(&pk);
            }
        }

        crtpPacket.datalen = 0xFFU;

        // Blink the LED
        if (NRF_TIMER1->EVENTS_COMPARE[0]) {
            NRF_TIMER1->EVENTS_COMPARE[0] = 0;
#ifndef DEBUG_TIMESLOT
            NRF_GPIOTE->TASKS_OUT[0] = 1;
#endif
        }
    }

    //Set bit 0x20 forces boot to firmware
    NRF_POWER->GPREGRET |= 0x20U;
    sd_nvic_SystemReset();

    while(1);
}
int main()
{
  GPIO_InitTypeDef gpioInit = {0};
  struct syslinkPacket slPacket;
  struct crtpPacket_s packet;
  unsigned int ledGreenTime=0;
  unsigned int ledRedTime = 0;
  unsigned int ledBlueTime = 0;


  /* Detecting if we need to boot firmware or DFU bootloader */
  bootpinInit();
  if (bootpinStartFirmware() == true) {
    if (*((uint32_t*)FIRMWARE_START) != 0xFFFFFFFFU) {
      void (*firmware)(void) __attribute__((noreturn)) = (void *)(*(uint32_t*)(FIRMWARE_START+4));
      bootpinDeinit();
      // Start firmware
      NVIC_SetVectorTable(FIRMWARE_START, 0);
      __set_MSP(*((uint32_t*)FIRMWARE_START));
      firmware();
    }
  } else if (bootpinNrfReset() == true) {
    void (*bootloader)(void) __attribute__((noreturn)) = (void *)(*(uint32_t*)(SYSTEM_BASE+4));
    bootpinDeinit();
    // Start bootloader
    NVIC_SetVectorTable(SYSTEM_BASE, 0);
    __set_MSP(*((uint32_t*)SYSTEM_BASE));
    bootloader();

  }
  bootpinDeinit();

  /* Booting CRTP Bootloader! */
  SystemInit();
  uartInit();
  

  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
  
  gpioInit.GPIO_Pin = GPIO_Pin_2;
  gpioInit.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_Init(GPIOD, &gpioInit);
  
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);

  gpioInit.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_0;
  gpioInit.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_Init(GPIOC, &gpioInit);
  GPIO_WriteBit(GPIOC, GPIO_Pin_0, 1);
  GPIO_WriteBit(GPIOC, GPIO_Pin_1, 1);

  SysTick->LOAD = (SystemCoreClock/8)/1000; // Set systick to overflow every 1ms
  SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk | SysTick_CTRL_TICKINT_Msk;
  NVIC_EnableIRQ(SysTick_IRQn);
  
  // Blue LED ON by default
  GPIO_WriteBit(GPIOD, GPIO_Pin_2, 1);

  while(1) {
    if (syslinkReceive(&slPacket)) {
      if (slPacket.type == SYSLINK_RADIO_RAW) {
        memcpy(packet.raw, slPacket.data, slPacket.length);
        packet.datalen = slPacket.length-1;

        ledGreenTime = tick;
        GPIO_WriteBit(GPIOC, GPIO_Pin_1, 0);

        if (bootloaderProcess(&packet)) {
          ledRedTime = tick;
          GPIO_WriteBit(GPIOC, GPIO_Pin_0, 0);

          memcpy(slPacket.data, packet.raw, packet.datalen+1);
          slPacket.length = packet.datalen+1;
          syslinkSend(&slPacket);
        }
      }
    }

    if (ledGreenTime!=0 && tick-ledGreenTime>10) {
      GPIO_WriteBit(GPIOC, GPIO_Pin_1, 1);
      ledGreenTime = 0;
    }
    if (ledRedTime!=0 && tick-ledRedTime>10) {
      GPIO_WriteBit(GPIOC, GPIO_Pin_0, 1);
      ledRedTime = 0;
    }

    if ((tick-ledBlueTime)>500) {
      if (GPIO_ReadInputDataBit(GPIOD, GPIO_Pin_2)) {
        GPIO_WriteBit(GPIOD, GPIO_Pin_2, 0);
      } else {
        GPIO_WriteBit(GPIOD, GPIO_Pin_2, 1);
      }
      ledBlueTime = tick;
    }
  }
  return 0;
}