forked from gary/BCU
2
0
Fork 0
BCU/library/drv_peripheral/drv_eg25gminipice.c

137 lines
4.0 KiB
C
Raw Normal View History

2025-02-12 17:28:30 +08:00
/******************************************************************************
* @file drv_eg25gminipice.c
* @brief drv_eg25gminipice.c drivers
* @version V1.0
* @author Gary
* @copyright
******************************************************************************/
#include "drv_eg25gminipice.h"
#define UART2_BAUDRATE 115200
2025-02-14 18:40:14 +08:00
#define MAX_RETRY 3
//4Gʵ<47><CAB5><EFBFBD><EFBFBD>
static uint8_t sim_rec_buf[MAX_RECV_LEN] = {0};
LTE4G_STATIC_INIT(sim_item, 1, 0, 0, 1024,sim_rec_buf);
void drv_sim_push_data(Lte4GItem* item, uint8_t *buf, uint16_t len)
{
uint16_t i = 0;
if((item != NULL) && (item->buf_pos + len < item->buf_size))
{
for (i = 0; i < len; i++)
{
item->buf[item->buf_pos++] = buf[i];
}
}
}
2025-02-12 17:28:30 +08:00
void drv_uart2_Init(void)
{
RCC->APB1ENR |= RCC_APB1ENR_USART2EN; // ʹ<><CAB9> USART2 ʱ<><CAB1>
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN; // ʹ<><CAB9> GPIOA ʱ<><CAB1>
GPIOA->MODER |= (2 << (2 * 2)) | (2 << (3 * 2)); // PA2(TX), PA3(RX) <20><><EFBFBD><EFBFBD>ģʽ
GPIOA->AFR[0] |= (7 << (2 * 4)) | (7 << (3 * 4)); // <20><><EFBFBD><EFBFBD> AF7 (USART2)
USART2->BRR = SystemCoreClock / UART2_BAUDRATE;
USART2->CR1 = USART_CR1_TE | USART_CR1_RE | USART_CR1_UE; // ʹ<>ܴ<EFBFBD><DCB4>ڡ<EFBFBD><DAA1><EFBFBD><EFBFBD>ͺͽ<CDBA><CDBD><EFBFBD>
}
void drv_uart2_sendbyte(char c)
{
while (!(USART2->SR & USART_SR_TXE)); // <20>ȴ<EFBFBD><C8B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
USART2->DR = c;
}
void drv_uart2_sendString(const char *str)
{
while (*str) {
drv_uart2_sendbyte(*str++);
}
}
void drv_send_at_cmd(const char *cmd, int delay_ms)
{
drv_uart2_sendString(cmd);
drv_uart2_sendString("\r\n"); // AT ָ<><D6B8><EFBFBD><EFBFBD> "\r\n" <20><>β
2025-02-14 18:40:14 +08:00
kit_time_dly_ms(30);
2025-02-12 17:28:30 +08:00
}
2025-02-14 18:40:14 +08:00
2025-02-12 17:28:30 +08:00
void drv_eg25g_init(void)
{
2025-02-14 18:40:14 +08:00
drv_send_at_cmd("AT", 500); // 1. <20><><EFBFBD><EFBFBD> AT ָ<><D6B8>
drv_send_at_cmd("AT+CPIN?", 500); // 2. <20><>ѯ SIM <20><>״̬
drv_send_at_cmd("AT+COPS=0", 500); // 3. <20><>ģ<EFBFBD><C4A3><EFBFBD>Զ<EFBFBD>ѡ<EFBFBD><D1A1><EFBFBD><EFBFBD>Ӫ<EFBFBD><D3AA>
drv_send_at_cmd("AT+CEREG?", 1000); // 4. <20><>ѯ 4G ע<><D7A2>״̬
// drv_send_at_cmd("AT+CGDCONT=1,\"IP\",\"your_apn\"", 500); // 5. <20><><EFBFBD><EFBFBD> APN <20><><EFBFBD><EFBFBD>֧<EFBFBD><D6A7><EFBFBD>Զ<EFBFBD>ʶ<EFBFBD><CAB6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD>apn
drv_send_at_cmd("AT+CGACT=1,1", 1000); // 6. <20><><EFBFBD><EFBFBD> PDP<44><50><EFBFBD><EFBFBD>ȡ IP<49><50>
drv_send_at_cmd("AT+CGPADDR=1", 500); // 7. <20><>ѯ<EFBFBD><D1AF><EFBFBD><EFBFBD><EFBFBD><EFBFBD> IP <20><>ַ
}
// <20><><EFBFBD><EFBFBD> AT ָ<><EFBFBD>ȴ<EFBFBD><C8B4><EFBFBD><EFBFBD><EFBFBD>ֵ,<2C><><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><DAB4>Ż<EFBFBD><C5BB><EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
int drv_send_at_cmd2(const char *cmd, int delay_ms)
{
for (int i = 0; i < MAX_RETRY; i++) // ʧ<><CAA7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> MAX_RETRY <20><>
{
drv_uart2_sendString(cmd);
drv_uart2_sendString("\r\n"); // AT ָ<><D6B8><EFBFBD><EFBFBD> "\r\n" <20><>β
kit_time_dly_ms(30);
if (sim_item.buf_pos > 0 && strstr((const char*)sim_item.buf, "OK")) // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ
{
return 1; // <20>ɹ<EFBFBD>
}
}
return 0; // ʧ<><CAA7>
}
void drv_eg25g_init2(void)
{
if (!drv_send_at_cmd2("AT", 500)) return; // 1. <20><><EFBFBD><EFBFBD> AT ָ<><D6B8>
if (!drv_send_at_cmd2("AT+CPIN?", 500)) return; // 2. <20><>ѯ SIM <20><>״̬
if (!drv_send_at_cmd2("AT+COPS=0", 500)) return; // 3. <20><>ģ<EFBFBD><C4A3><EFBFBD>Զ<EFBFBD>ѡ<EFBFBD><D1A1><EFBFBD><EFBFBD>Ӫ<EFBFBD><D3AA>
if (!drv_send_at_cmd2("AT+CEREG?", 1000)) return; // 4. <20><>ѯ 4G ע<><D7A2>״̬
if (!drv_send_at_cmd2("AT+CGDCONT=1,\"IP\",\"your_apn\"", 500)) return; // 5. <20><><EFBFBD><EFBFBD> APN
if (!drv_send_at_cmd2("AT+CGACT=1,1", 1000)) return; // 6. <20><><EFBFBD><EFBFBD> PDP<44><50><EFBFBD><EFBFBD>ȡ IP<49><50>
if (!drv_send_at_cmd2("AT+CGPADDR=1", 500)) return; // 7. <20><>ѯ<EFBFBD><D1AF><EFBFBD><EFBFBD><EFBFBD><EFBFBD> IP <20><>ַ
2025-02-12 17:28:30 +08:00
}
void drv_mqtt_connect(void)
{
drv_send_at_cmd("AT+QMTCFG=\"recv/mode\",0,0,1", 500);
drv_send_at_cmd("AT+QMTOPEN=0,\"mqtt.example.com\",1883", 5000);
drv_send_at_cmd("AT+QMTCONN=0,\"client_id\",\"username\",\"password\"", 5000);
}
void drv_mqtt_publish(const char *topic, const char *message)
{
char cmd[128];
sprintf(cmd, "AT+QMTPUB=0,0,0,0,\"%s\",\"%s\"", topic, message);
drv_send_at_cmd(cmd, 500);
}
2025-02-14 18:40:14 +08:00
2025-02-12 17:28:30 +08:00
/*
2025-02-14 18:40:14 +08:00
2025-02-12 17:28:30 +08:00
int main(void) {
SystemInit();
drv_uart2_Init();
drv_eg25g_init();
drv_mqtt_connect();
while (1)
{
drv_mqtt_publish("sensor/data", "{\"temperature\":25.3}");
for (volatile int i = 0; i < 10000000; i++); // <20><><EFBFBD>ͼ<EFBFBD><CDBC><EFBFBD>
}
}
2025-02-14 18:40:14 +08:00
2025-02-12 17:28:30 +08:00
*/