-2

I am new with Embedded firmware development and first time working with MC60. I have to write my own header files and libraries for all the features I'll be using in MC60, be it UART, BLE, GSM, GPS, etc. I want to know how can I write a header file in C, that can send an AT command to MC60 from the MCU and just fetch the response. The MCU to be used is still not decided however, for reference I want a script in C that can just customize the AT commands of MC60 like we have for Arduino LCD commands by using library LiquidCrystal.h, If anyone can tell me how to write one or two commands of the BLE module in a header file then that can act as reference for me to write other commands all by myself.

I am following this PDF document of AT commands for BLE and it consists of all the commands that I want to customize in my header file. https://github.com/nkolban/esp32-snippets/files/2105752/Quectel_MC60_BLE_AT_Commands_Manual_V1.1.pdf

  • You may be confusing the functionality you *discover* through a header file with the actual source file *implementing* it, and also the fact that Arduino is C++ where there are more ways to have functionality itself in a header than the extremely limited ones in C. If you view the full source of the Arduino LiquidCrystal library you'll see there's probably more to it than the .h file. You won't be able to exactly translate that mechanism to C though you can build quite serviceable interfaces. Or you can use C++, something definitely done but controversial in an embedded setting. – Chris Stratton Dec 20 '18 at 15:59
  • Header files are simply included by the pre-processor; they contain C code, nothing more. Normally they contain declarative code only. That is to say, the header file alone is the easy bit but will not get you what you want. LiquidCrystal.h _is not_ a library; it is the declarative header file for a library. What you are asking is how to write a library or use multiple translation units in an application. That is dealt with broadly elsewhere. There's nothing magic about libraries - its just code like any other. – Clifford Dec 20 '18 at 19:25
  • It is not at all clear what you mean by "Customize"; it implies modifying the behaviour of the MC60's response to its AT commands. If you are simply wanting to send AT commands from an Arduino, I don't see how that could be termed "customization", that is merely _composing_ and sending existing commands. – Clifford Dec 20 '18 at 19:31

1 Answers1

1

I want to know how can I write a header file in C, that can send an AT command

From simplewikipedia header file:

In computer programming, a header file can be thought of as a dictionary a compiler uses if it comes across a word it does not understand.

From wikipedia source code:

The source code of a program is specially designed to facilitate the work of computer programmers.

You can't write a header file that will do an action. The source code does the "work" of the computer, header file serves as a dictionary.

I want to know how can I write a header file in C

There are plenty tutorials on how to write header files in C over the net. The tutorial on tutorialspoint is one of them.

if anyone can tell me how to write one or two commands of the BLE module

Opinion: rule of a thumb - google "github I want this" and you will get code samples.

libraries for all the features I'll be using in MC60, be it UART, BLE, GSM, GPS

Stackoverflow is not a coding service.

Start your way up or down. Create an abstraction. Create API and write libraries that support your abstraction. Work way up (or down) and create all relevant source files.

Arduino has many libraries you can use. AT commands are plain data you send through communication link - mostly through an universal asynchronous receive-transmitted (UART), but not only. The documentation you linked is exact - it lists all available AT commands you can use with your device. Read it.

can send an AT command to MC60 from the MCU and just fetch the response.

All the serial communications on arduino is described in Serial. You can get many examples of serial communications on arduino online. Note that arduino libraries are in C++, not in C. You can write your own abstraction for the uart communication (or have no abstraction at all). Download the datasheet/reference manual for your device, read it and start implementing the needed functionality in your program.

I want a script in C that can just customize the AT commands

// abstract API to send the data pointed to by pointer with ptrsize bytes through UART
void uart_send(void *ptr, size_t ptrsize);
// abstract API to read the data from uart and place it at memory pointed to by ptr
// reads as much as ptrsize bytes or until timeout_ms miliseconds timeout
void uart_read(void *ptr, size_t ptrsize, int timeout_ms);

// buffer
char buf[256];

// "customize" buf to the "Power on/off BT" at command that powers up the device
snprintf(buf, sizeof(buf), "AT+QBTPWR=1\r\n");
// power up the device
uart_send(buf, strlen(buf));

// read the response from the device with 1 second timeout
uart_read(buf, sizeof(buf), 1000);
// check the response
if (strcmp(buf, "OK\r\n") != 0) { 
    fprintf(stderr, "Device didn't repond with OK!\n");
    abort();
}


// "customize" buf to have the "Power on/off BT" at command that powers down the device
snprintf(buf, sizeof(buf), "AT+QBTPWR=0\r\n");
// power down the device
uart_send(buf, strlen(buf));

uart_read(buf, sizeof(buf), 1000);
if (strcmp(buf, "OK\r\n") != 0) { 
    fprintf(stderr, "Device didn't repond with OK!\n");
    abort();
}

Above I have customized the command to power up or down the device. With simple snprintf you can use all the family printf format modifiers, including "%d".

int state = 1;
snprintf(buf, sizeof(buf), "AT+QBTPWR=%d\r\n", state);
KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • In what manner is the command "customized", rather then simply _composed_? I appreciate that is a term used in the question, but it is entirely unclear what it means there too. – Clifford Dec 20 '18 at 19:33
  • 1
    @ShreyanshDubey In real life, it would probably be `size_t uart_read(...) // returns actual number of rx bytes`, and you would continuously feed received data into a fifo buffer for parsing. – vgru Dec 21 '18 at 22:15
  • I see this is like something i wanted thanks! – Shreyansh Dubey Dec 23 '18 at 17:44