Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

AT-Command parser for STM32

License

NotificationsYou must be signed in to change notification settings

nimaltd/atc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

96 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation


Please Do not Forget to get STAR, DONATE and support me on social networks. Thank you. 💖



Overview

The AT Command Library (ATC) simplifies UART communication for STM32 microcontrollers with an event-driven approach and debug capabilities. This updated version introduces AT command slave functionality, allowing the STM32 to process commands likeAT+LED=ON and respond (e.g.,+OK), making it ideal for IoT and embedded applications.

Key Features

  • Event-driven UART handling with DMA support.
  • Optional RTOS compatibility (FreeRTOS, ThreadX).
  • Debug logging when enabled.
  • AT command slave mode with custom handlers.

Installation

  1. Download the library:NimaLTD.I-CUBE-ATC.pdsc.
  2. Import it into STM32CubeMX and enable it.
  3. Configure UART:
    • Enable UART interrupt.
    • Enable TX/RX DMA in Normal Mode.
  4. In the Code Generator tab, select "Generate peripheral initialization as a pair of .c/.h files per peripheral."
  5. Generate the project code.

Getting Started

  1. Declare anATC_HandleTypeDef structure in your code.
  2. AddATC_IdleLineCallback() to the UART idle line callback (e.g., inHAL_UARTEx_RxEventCallback).
  3. Initialize the library withATC_Init().
  4. Optionally, set up events withATC_SetEvents() or AT commands withATC_SetCommands().
  5. CallATC_Loop() in your main loop to process incoming data.

Using AT Command Handlers

This library now supports AT command handling, enabling the STM32 to act as a command slave. Send commands likeAT+LED=ON from an external device, and the STM32 will execute them and respond (e.g.,+OK or+LED:ON).

Setup Steps

  1. Define Command Handlers: Create functions to process commands and generate responses.
  2. Create a Command Table: UseATC_CmdTypeDef to map command prefixes (e.g.,AT+LED=) to handlers.
  3. Register Commands: CallATC_SetCommands() to link the table to your ATC handle.
  4. Run the Loop: UseATC_Loop() to handle incoming commands and events.

Example: LED Control

Control an LED and query its state via AT commands:

#include"atc.h"// ATC handleATC_HandleTypeDefhAtc;// Command handler for setting LED statevoidHandle_LED(constchar*args,char*response){if (strcmp(args,"ON")==0)  {HAL_GPIO_WritePin(GPIOA,GPIO_PIN_5,GPIO_PIN_SET);// Adjust port/pinstrcpy(response,"+OK");  }elseif (strcmp(args,"OFF")==0)  {HAL_GPIO_WritePin(GPIOA,GPIO_PIN_5,GPIO_PIN_RESET);strcpy(response,"+OK");  }else  {strcpy(response,"+ERROR");  }}// Command handler for querying LED statevoidHandle_GetLED(constchar*args,char*response){GPIO_PinStateled_state=HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_5);strcpy(response, (led_state==GPIO_PIN_SET) ?"+LED:ON" :"+LED:OFF");}// Command tableATC_CmdTypeDefat_commands[]={  {"AT+LED?",Handle_GetLED},// Query LED state: "AT+LED?" -> "+LED:ON"  {"AT+LED=",Handle_LED},// Set LED state: "AT+LED=ON" -> "+OK"  {NULL,NULL}// Terminator};// UART handle (from STM32CubeMX)externUART_HandleTypeDefhuart1;intmain(void){// HAL initialization (from STM32CubeMX)HAL_Init();SystemClock_Config();MX_GPIO_Init();MX_DMA_Init();MX_USART1_UART_Init();// Initialize ATCATC_Init(&hAtc,&huart1,256,"Slave1");// Register AT commandsATC_SetCommands(&hAtc,at_commands);while (1)  {ATC_Loop(&hAtc);// Process commands and events  }}// Add to your UART IRQ handler (e.g., stm32f1xx_it.c)voidHAL_UARTEx_RxEventCallback(UART_HandleTypeDef*huart,uint16_tSize) {if (huart->Instance==USART1)  {// Adjust for your UART instanceATC_IdleLineCallback(&hAtc,Size);  }}

Command Responses

  • AT+LED=ON+OK (turns LED on)
  • AT+LED=OFF+OK (turns LED off)
  • AT+LED?+LED:ON or+LED:OFF (queries state)
  • Unknown command →+ERROR

Watch the Video:

Video

The old Version:https://github.com/nimaltd/ATC/archive/refs/tags/3.0.3.zip


[8]ページ先頭

©2009-2025 Movatter.jp