6. Adding Custom Commands

CodeLess offers the option to not write firmware for the DA14531/DA14585/DA14586 platform, but you may want to add your own commands or modify existing ones to better serve your application. This section demonstrates how to add your own flavor to CodeLess.

Before jumping into that, you need two things:

6.1. Overview of Implementation

With all the necessary understanding, we create our own Custom Commands.

Overview:

  • We implement a command that can serve as a really primitive calculator (Supports only addition of two to five operands – all integers)

  • The response to the command is the sum of the two to five operands or an error if the number of arguments is less than two or more than five

  • We allow input of operands in hexadecimal or decimal format. The result is provided only as decimal in the response

  • We name our new command AT+ADD

An example of the new command: AT+ADD=1,2,3,4 (should return 10)

With the design specification in place we can start implementation.

6.2. Implementation

Open the folder containing the CodeLess SDK and go to the location <sdk_root_directory> > v_6.380.x.x > projects > target_apps > codeless > codeless_5xx > Keil_5, double-click codeless_585.uvproj to open the project in Keil development environment.

We add our command right after the USE_AT_BAUD command implementation. The implementation is simplified to be able to add custom commands by writing code in two files only, namely user_at_commands.h and user_at_commands.c as shown below:

1. user_at_commands.h

Define the USE_AT_ADD to enable the use of custom addition. You can include the code at the end of all the other definitions, i.e, after #define USE_AT_BAUD

.
.
.
#define USE_AT_BAUD //Already part of the SDK

#define USE_AT_ADD //CUSTOM

Also include in the Set of supported CodeLess AT command set.

.
.
.

#ifdef USE_AT_BAUD   //Already part of the SDK
USE_AT_BAUD /** <Set the baud rate **/
#endif

#ifdef USE_AT_ADD
AT_ADD, /**< Addition of up to 5 numbers */ //CUSTOM
#endif

2. user_at_commands.c

Now add our custom AT command in the AT commands jumptable as shown below.

.
.
.
#ifdef USE_AT_BAUD
    { AT_BAUD         ,  0,  1, "BAUD"         , user_at_baud         },
#endif

#ifdef USE_AT_ADD
    { AT_ADD   ,  2,  5, "ADD"         , user_at_add         },
#endif

The description of the syntax is depicted in Figure 39. Each custom command you create need to follow this syntax.

_images/atparam.png

Figure 39 Syntax of the AT Command

Define the function that does the job of addition for us, right after the USE_AT_PIN definition.

_images/customadd.png

Figure 40 Adding Custom Add AT Code

#ifdef USE_AT_ADD
void user_at_add(struct at_cmd_params_t* arg, char* reply_string)
{
      // Initialize the sum
    uint32_t sum = 0;

      for(uint8_t i = 0; i < arg->arg_count; i++)
        sum += ahtoi32(&arg->cmd_buffer[arg->arg_index[i]]);

      arch_sprintf(reply_string, "%d", sum);
    arg->success_flag = true;
}
#endif

6.3. Execution

The steps to execute the build, selecting the target and running the code is explained in Section 7. We can now build the code, upload it to target, and test the new command using any terminal.

_images/testrun.png

Figure 41 Example Output in Terminal