FireBLE/Source code port

来自Firefly wiki
跳转至: 导航搜索

Foreword

The source code of Quntic is based on the development kits of the company.

The development suite of Quntic contain QBlue DK and MiniDK,and QBlue DK contains MotherBoard and Evaluation Board,that in QBlue1.3.5 were introduced.

QBlue DK has more peripherals,and use the NXP LPC1768 as the master;The Mini DK is less peripherals.

The development suites of Quntic can be run directly all of the source code,but there are also several disadvantages:

  1. QN9020 is used in the development kit,because QN9021 have with small package relative to QN9020,so the IO distribution is different,just the IO is conflict with QN9021.If considering various factors of the development product need to use QN9021,is not directly applicable to the product development programs on the development kit.
  2. The price of development kit is higher,the official retail price is $69.
  3. Quntic provides only the SDK development in both English and Chinese manual for developers to use,don't open sourcing management,learning environment is single.

The difference of FireBLE and DK:

  1. QN9021 single chip solutions without MCU.
  2. Rich peripherals,Joysticks rocker, OLED screen interface,on-board MPU6050 Six axis G - Sensor.
  3. Open source management,provide the Chinese learning tutorial,BBS,community,and open source project development.

Preparation

Access to the source code

There are two ways to access to the source code of FireBLE.One way is to download the latest source code on the official website,another method is to use git tools clone code from the remote server.I make up recommend use the second method to obtain the source code,and I hope you like to use with git tools to manage and maintain the code,the more hope that users can participate in the development and maintenance of the open source project,in which to learn more things,broaden your horizons,not own a person alone.Below I introduce what is git.Although it is very shallow,but our make up have already do our best,please don't mind.

Get the source code in the official download interface

Source code is based on QBlue1.3.5,of which the main changed the button,the led pin,increased the TASK_USR,increased the OLED driver and MPU6050 driver.The code of The download interface will be updated without day,so can't real-time access to the latest revision.

Clone code with git tool in bitbucket

Using the following commands can directly clone code to the local.

 git clone https://TeeFirefly@bitbucket.org/T-Firefly/FireBLE.git

First introduce what is git and why want to use it.

Introduce the git code management tool

Git is a free and open source distributed version control system,used for or agile to efficiently handle any small large projects.

From the perspective of developers,git has the following functions:

  1. Clone database from the server to the single machine (including code and version information).
  2. Create branches on your machines and modify the code.
  3. Commit the branch code on your machines.
  4. Merging branches on your machines.
  5. Create a new branch,and fetch down the latest version of the code on the server,and then merge with their main branch.
  6. Generate patch and sent it to the developer.
  7. Pay attention the feedback of the main developer,if the main developers found there are conflicts between the two general developers (can solve the conflict between them),will ask them to solve the conflicts,and then submitted by one of them.If the main developers can solve,or without conflict,it will pass.
  8. The general solution to the conflict between developers,developers can use the pull command to resolve conflicts,after solving the conflict,please submit patches again to the main developers.

From the perspective of the main developers (assuming the main developers don't have to develop code),git has the following features:

  1. Check the email or by other ways to view submit status of the general developer.
  2. On the patch to solve the conflict(you can solve it by yourself,and also can ask the developer to solve,then resubmit it later.If it is an open source project,you need to decide which patch are useful, which don't have to).
  3. Submit the results to the public server,then notify all the developers.

At a word,convenient released code to other developers to download and development,convenient in this machine to modify code to do detailed records and backup(git is not destroyed in theory,can be back at any time to submit records),easy to share code to other developers,convenient to modify any code and is not afraid of failure code(as long as it is on the branch),convenient based on source code with a variety of methods to realize a variety of functions,without confusion.

To sum up,the git is a very convenient for developers,it can improve a lot of the efficiency,and has a lot of advantages.

The information is too little and learning cycle is long.But once learned,you will like it and rely on.

More about of the git instruction and learning,recommend this web site,it is so easy to learn the git: http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000

The habit of git code management advice

  1. Do with git code maintenance,you need to develop the habit of that complete a small feature will submit,try not to do a lot of things and then submit.Unless you can guarantee the success of each is perfect,otherwise,once appear mistake,you will find out that you don't have submitted when you want to go back to a certain point in time.And reset completely it will delete a few small function.That will be a waste of time.
  2. Reset is very dangerous,but also useful,so it is necessary to carefully read this part and flexibility in the use of it.
  3. The name of commit as much as possible to use the English.And try to clear and unified format description for the submitted content.
  4. Command line is used in conjunction with a graphical interface and used alternately.It has the efficient of Linux and friendly of Windows.
  5. Before finishing a function to open a branch,please don't directly on the master to do development,it may be full of traps.
  6. Don't abandon,don't give up.I'm not familiar with more than 80% of the function of the git,however,I'm already very not accustomed to not having git.I think re-read the above of Liao Xuefeng website,you will master the basic and then almost.


The source code structure analysis

After the success of the git clone,you will see a folder named FireBLE in the current directory,this is just clone of engineering.

Open the project as you can see,there are many folders named prj_xxx, and every project is independent.And if you need any new construction, suggest that only created under the directory,because in the configuration of keil,it have configured the relative path,if any change of path,you need to reconfigure the keil projects,otherwise you will compile error.

Another folder named after src,it mainly contains general some documents of each project,such as app,profile,driver and so on.

group sort description group sort description
startup Assembly launch configuration code profile Profile is standard implementation of service in the bluetooth specification into a specific service implementation,app and profile through the MSG communication to work together,achieve a specific function.And the basic will not change,when there is demand and you are very familiar with it,you can be customized.
main The main function, generally do not need to modify. drivers The underlying driver system.
usr User-defined file is closely related to engineering in order to realize the function.Typically require changes to the code on this folder. lib Encapsulation system libraries,which package the secret code of Quntic.
app BLE app,invoking the different service and achieve a specific function,it can directly use the accomplished app of Quntic and can also be customized. qnevb System peripherals driver file.
retarget It realizes the redefinition of getchar (),and so on.And it realizes standardized serial output.
  • For host devices,we are more developed in the app directory.For from the machine equipment,we are more developed in the usr directory.

The source code transplantation

The source code transplantation refers to source code of Quntic changes for the code of FireBLE development board to be able to run,the main content of the transplant is at the bottom of the peripheral drive.The bottom outside of source code has the following three:
  • LED
  • Button
  • Buzz

In the case of prj_proxr for transplantation.

Transplant instructions

Key driver,for example,the source code for button has the following pin definition(FireBLE/Demo_BLE/src/qnevb/button.h):

  #if !defined(QN_9021_MINIDK)
      #define LED1_PIN    (GPIO_P05)
      #define LED2_PIN    (GPIO_P04)
      #define LED3_PIN    (GPIO_P03)
      #define LED4_PIN    (GPIO_P02)
      #define LED5_PIN    (GPIO_P01)
  #else
      #define LED1_PIN    (GPIO_P03)
      #define LED2_PIN    (GPIO_P13) 
      #define LED3_PIN    (GPIO_P02)  // no pin in QN9021
      #define LED4_PIN    (GPIO_P02)  // no pin in QN9021
      #define LED5_PIN    (GPIO_P02)  // no pin in QN9021
  #endif

Just open the QN_9021_MINIDK macro definition,it can change the pin configuration.This is generally used in application configuration method.Find the QN_9021_MINIDK macro definition: FireBLE/Demo_BLE/src/app/app_config.h

  /// Evaluation Board Indication
  #if (defined(CFG_9021_MINIDK))
    #define QN_9021_MINIDK
  #endif


It seems that open QN_9021_MINIDK macro definition must define CFG_9021_MINIDK in the first.To find the CFG_9021_MINIDK: FireBLE/Demo_BLE/pro_xxx/src/usr_config.h

  /// Evaluation board indication
  // The GPIOs used for QN9021 miniDK's LED and button are different from QN9020 miniDK.
  // If the QN9021 miniDK is used, the following macro shall be defined.
  #define CFG_9021_MINIDK

In FireBLE/Demo_BLE/src/app/app_config.h to do the following definitions:

  /// FireBLE Board Indication
  #if	(defined(CFG_FireBLE))
    #define	FireBLE_platform
  #endif

In FireBLE/Demo_BLE/pro_xxx/src/usr_config.h to do the following definitions:

  /// Evaluation board indication
  // The GPIOs used for FireBLE's LED and button are different from FireBLE.
  // If the FireBLE is used, the following macro shall be defined.
  #define CFG_FireBLE

In this way,we define a development platform of FireBLE_platform,all operations in this chapter,we only need to open the macro definition of CFG_FireBLE to use the underlying driver of source code in the engineering.

Why to define CFG_9021_MINIDK before QN_9021_MINIDK?It is precisely in order to better maintain code,separate code correlation.

The app_config.h is belong to the public profile and is stored in the app folder of multiple engineering common src folder,every project used is the same code.In theory,in order to code portability,we hope that as long as the modification project in usr_config.h to achieve different switch for development platform and does not affect the use of other development platform.That is to say,without such a design,if I am in A project using the FireBLE,B is used in engineering QBlue 9021 MINI DK,because if I want to use QBlue 9021 MINI DK in B,I need to shut down the FireBLE_platform macro definition in app_config,and open 9021 MINI QBlue DK,so A project will not be able to run normally.On the other hand,the two projects are independent,totally no effect.

Add configuration definition

In FireBLE/Demo_BLE/pro_xxx/src/usr_config.h to add the following definition:

/// Evaluation board indication
// The GPIOs used for FireBLE's LED and button are different from FireBLE.
// If the FireBLE is used, the following macro shall be defined.
#define CFG_FireBLE
 
///defined it when used SWD Debug,and LED2/LED3 will do not work.
#define  CFG_SWD
 
///FireBLE Joystick button
#define	CFG_JOYSTICKS

Open the CFG_FireBLE macro to enter into the platform of FireBLE,the code can be applied to FireBLE development board.And you need to use the SWD hardware simulation to open CFG_SWD,but the LED2 and LED3 cannot use at the moment.Lights dim because as the simulation interface when using the pin is configured as a drop-down.You need to use up to five to buttons to open CFG_JOYSTICKS macro .

In FireBLE/Demo_BLE/src/app/app_config.h to add the following definitions:

/// FireBLE Board Indication
#if	(defined(CFG_FireBLE))
		#define	FireBLE_platform
#if	(defined(CFG_SWD))		
		#define	FB_SWD									1
#else
		#define	FB_SWD									0
#endif
#if	(defined(CFG_JOYSTICKS))
		#define	FB_JOYSTICKS						1
#else
		#define	FB_JOYSTICKS						0
#endif
#endif

In FireBLE/Demo_BLE/pro_xxx/src/usr_design.h to add a header file

#if		(FB_JOYSTICKS)
  #include "joysticks.h"
#endif

LED adapter

LED adapter is very simple,only need to change the LED pins which are defined as follows(FireBLE/Demo_BLE/src/qnevb/led.h):

  #if !defined(QN_9021_MINIDK)
    #if !defined(FireBLE_platform)
      #define LED1_PIN    (GPIO_P05)
      #define LED2_PIN    (GPIO_P04)
      #define LED3_PIN    (GPIO_P03)
      #define LED4_PIN    (GPIO_P02)
      #define LED5_PIN    (GPIO_P01)
    #else
      #define	LED1_PIN	 (GPIO_P27)
      #if !(FB_SWD)
        #define LED2_PIN   (GPIO_P06)
        #define LED3_PIN   (GPIO_P07)
      #else
        #define LED2_PIN    (GPIO_P02)  // no pin in QN9021
        #define LED3_PIN    (GPIO_P02)  // no pin in QN9021
      #endif
     #define LED4_PIN    (GPIO_P02)  // no pin in QN9021
     #define LED5_PIN    (GPIO_P02)  // no pin in QN9021
    #endif
  #else
    #define LED1_PIN    (GPIO_P03)
    #define LED2_PIN    (GPIO_P13)
    #define LED3_PIN    (GPIO_P02)  // no pin in QN9021
    #define LED4_PIN    (GPIO_P02)  // no pin in QN9021
    #define LED5_PIN    (GPIO_P02)  // no pin in QN9021
  #endif

About the description of FB_SWD macro definition:by default,close the SWD simulation,it need to use SWD simulation and need to open CFG_SWD macro definition in usr_config.h

///defined it when used SWD Debug,and LED2/LED3 will do not work.
#define  CFG_SWD

About the description of FB_SWD macro definition:by default, close the SWD simulation simulation, need to use SWD need in usr_config. Open CFG_SWD macro definition of h

///defined it when used SWD Debug,and LED2/LED3 will do not work.
#define  CFG_SWD

The definition of GPIO according to actual applications to modify,because SystemIOCfg is belong to usr user group,is a user-defined file.Please users according to your own requirements definition,the sample is as follows:

static void SystemIOCfg(void)
{
    // pin mux
    syscon_SetPMCR0(QN_SYSCON, P00_UART0_TXD_PIN_CTRL)
                             | P01_GPIO_1_PIN_CTRL
                             | P02_GPIO_2_PIN_CTRL
                             | P03_GPIO_3_PIN_CTRL
                             | P04_GPIO_4_PIN_CTRL
                             | P05_GPIO_5_PIN_CTRL
#if	!(FB_SWD)
			     | P06_GPIO_6_PIN_CTRL
			     | P07_GPIO_7_PIN_CTRL
#else
                             | P06_SW_DAT_PIN_CTRL
                             | P07_SW_CLK_PIN_CTRL
#endif
 
}

Joysticks adapter

Joysticks adapter is much complicated than just.First modify the definition of button pin (FireBLE/Demo_BLE/src/qnevb/button.h):

  #if !defined(QN_9021_MINIDK)
    #if !defined(FireBLE_platform)
      #define BUTTON1_PIN    (GPIO_P14)
      #define BUTTON2_PIN    (GPIO_P15)
    #else
      #define	BUTTON1_PIN	 (GPIO_P12)
      #define	BUTTON2_PIN	 (GPIO_P02)  // no pin in QN9021
    #endif
  #else
    #define BUTTON1_PIN    (GPIO_P12)
    #define BUTTON2_PIN    (GPIO_P10)
  #endif

If it is based on proxr routines to modify the program,the program already,you can compile successfully,and be able to normal execution, no matter which one direction Joysticks to press,both open and closed BLE broadcasts,and LED1 will not stop flashing when the radio on.because the Joysticks,we can't meet on this,and the next to join Joysticks direction judgment is required.Joysticks with adc to test level changes of GPIO_P30 to determine the direction of the Joysticks (interested can go to the schematic diagram),we can add a macro definition,in the case of don't need to use the Joysticks,you can not do this aspect of the recognition.

In firebllue/Demo_BLE/prj_xxx/src/usr_config.h to add

  ///define it when used Joysticks
  #define CFG_JOYSTICKS

When open the macro definition according to need to use the Joysticks.

The same as LED,due to the use to the ADC as reverse recognition,so it need to modify the pin configuration.For example, in firebllue/Demo_BLE/prj_xxx/src/system.c to add the pin configuration of GPIO,and configurate the GPIO_P30 ADC input channel.

  #if	!(FB_JOYSTICKS)
        | P30_GPIO_24_PIN_CTRL
  #else
        | P30_AIN0_PIN_CTRL
  #endif

Add the joysticks.c file of qnevb folder,this file implements the direction recognition of Joysticks.In task_app.c and task_app.h to add the following: app_task.c

#if (FB_JOYSTICKS)
		{APP_KEY_PROCESS_TIMER,						(ke_msg_func_t) app_key_process_timer_handler},
		{APP_KEY_SCAN_TIMER,						(ke_msg_func_t) app_key_scan_timer_handler},
#endif

app_task.h

#if	(FB_JOYSTICKS)
		APP_KEY_PROCESS_TIMER,
		APP_KEY_SCAN_TIMER,
#endif

Compile it again first,and see what will have some errors occur.

FireBLE joysticks error1.png

Does not contain the adc driver,in fact,there is also a file of analog.c to include(in the driver folder of the project directory on one level of the src folder).

Run again and have no error,it can seem to be used,however,it is not enough actually,it still need the following steps:

  • Five keys to use the ADC sampling,so you also need to register button in the ADC sampling to complete interruption.
#if    (FB_JOYSTICKS)
  if(KE_EVENT_OK != ke_evt_callback_set(EVENT_ADC_KEY_SAMPLE_CMP_ID,
                                            app_event_adc_key_sample_cmp_handler))
  {
       ASSERT_ERR(0);
  }
#endif

  • Add a timer events in the button event handler app_event_button1_press_handler to jump Ioysticks recognition processing function,and before the block button handler function.
  void app_event_button1_press_handler(void) 
  {
  #if ((QN_DEEP_SLEEP_EN) && (!QN_32K_RCO))
    if (sleep_env.deep_sleep)
    {
        sleep_env.deep_sleep = false;
        // start 32k xtal wakeup timer
        wakeup_32k_xtal_start_timer();
    }
  #endif
    // delay 20ms to debounce
  #if (FB_JOYSTICKS)
    ke_timer_set(APP_KEY_SCAN_TIMER,TASK_APP,2);
  #else
    ke_timer_set(APP_SYS_BUTTON_1_TIMER, TASK_APP, 2);
  #endif
    ke_evt_clear(1UL << EVENT_BUTTON1_PRESS_ID);
  }

  • The program need to initialize some parameters after awakening the button,add the following code in usr_button1_cb function:
  void usr_button1_cb(void)
  {
    // If BLE is in the sleep mode, wakeup it.
    if(ble_ext_wakeup_allow())
    {
  #if ((QN_DEEP_SLEEP_EN) && (!QN_32K_RCO))
        if (sleep_env.deep_sleep)
        {
            wakeup_32k_xtal_switch_clk();
        }
  #endif
        sw_wakeup_ble_hw();
  // #if (QN_DEEP_SLEEP_EN)
  //         // prevent deep sleep
  //         if(sleep_get_pm() == PM_DEEP_SLEEP)
  //         {
  //             sleep_set_pm(PM_SLEEP);
  //         }
  // #endif
      }
  #if (FB_JOYSTICKS)
      usr_button_env.button_st = button_press;
  #endif
    // key debounce:
    // We can set a soft timer to debounce.
    // After wakeup BLE, the timer is not calibrated immediately and it is not precise.
    // So We set a event, in the event handle, set the soft timer.
    ke_evt_set(1UL << EVENT_BUTTON1_PRESS_ID);
  }

So,the driver of Joysticks is completed transplantation,it can implement key trigger a need to perform after operation in usr_key_process_timer_handler.