2024-12-11 16:21:57 +08:00
|
|
|
|
#include "bsp_ml307r.h"
|
|
|
|
|
#include "at_chat.h"
|
|
|
|
|
#include "at_port.h"
|
|
|
|
|
#include "bsp_uart.h"
|
|
|
|
|
#include "log.h"
|
|
|
|
|
#include "stdio.h"
|
|
|
|
|
#include "CONFIG.h"
|
|
|
|
|
|
|
|
|
|
#undef LOG_ENABLE
|
|
|
|
|
#define LOG_ENABLE 1
|
|
|
|
|
|
|
|
|
|
#define ML307_PERIODIC_EVT (0x0001 << 0)
|
|
|
|
|
|
|
|
|
|
static tmosTaskID ml307_task_id = INVALID_TASK_ID;
|
|
|
|
|
|
|
|
|
|
at_obj_t *ml307r_obj;
|
2025-03-20 11:26:08 +08:00
|
|
|
|
uint8_t databuf[5] = {0x01,0xaa,0x12,0xde,0x39};
|
2024-12-11 16:21:57 +08:00
|
|
|
|
|
|
|
|
|
void At_Debug(const char *fmt, ...)
|
|
|
|
|
{
|
|
|
|
|
va_list args;
|
|
|
|
|
va_start(args, fmt);
|
|
|
|
|
vprintf(fmt, args);
|
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Ml307_Delay_Ms(uint16_t ms)
|
|
|
|
|
{
|
|
|
|
|
DelayMs(ms);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Ml307_Delay_us(uint16_t us)
|
|
|
|
|
{
|
|
|
|
|
DelayUs(us);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const at_adapter_t at_adapter =
|
|
|
|
|
{
|
|
|
|
|
.write = BSP_Uart1_Send_Data,
|
|
|
|
|
.read = BSP_Uart1_Receive_Data,
|
|
|
|
|
.debug = At_Debug,
|
|
|
|
|
.recv_bufsize = 256,
|
|
|
|
|
.urc_bufsize = 512
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//ģ<><C4A3><EFBFBD><D7BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
static int URC_Module_Ready_Cb(at_urc_info_t *info)
|
|
|
|
|
{
|
|
|
|
|
logDebug("module ready complete\r\n");
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief urc<EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD>
|
|
|
|
|
*/
|
|
|
|
|
static const urc_item_t urc_table[] =
|
|
|
|
|
{
|
|
|
|
|
{.prefix = "+CPIN: READY", .endmark = '\n', .handler = URC_Module_Ready_Cb},//ģ<><C4A3><EFBFBD><D7BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static void Ml307_Power_On(void)
|
|
|
|
|
{
|
|
|
|
|
#define ML307_POWER_ON_TIME 3
|
|
|
|
|
|
|
|
|
|
GPIOB_SetBits(ML307_PWR_PIN);
|
|
|
|
|
Ml307_Delay_Ms(ML307_POWER_ON_TIME * 1000);
|
|
|
|
|
GPIOB_ResetBits(ML307_PWR_PIN);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void Ml307_Power_Off(void)
|
|
|
|
|
{
|
|
|
|
|
#define M307_POWER_OFF_TIME 4
|
|
|
|
|
GPIOB_SetBits(ML307_PWR_PIN);
|
|
|
|
|
Ml307_Delay_Ms(M307_POWER_OFF_TIME * 1000);
|
|
|
|
|
GPIOB_ResetBits(ML307_PWR_PIN);
|
|
|
|
|
Ml307_Delay_Ms(100);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int Ml307r_Gpio_Init(void)
|
|
|
|
|
{
|
|
|
|
|
GPIOB_SetBits(ENABLE_3_8_V);
|
|
|
|
|
GPIOB_ModeCfg(ENABLE_3_8_V, GPIO_ModeOut_PP_5mA);
|
|
|
|
|
|
|
|
|
|
GPIOB_ResetBits(ML307_RST_PIN);
|
|
|
|
|
GPIOB_ModeCfg(ML307_RST_PIN, GPIO_ModeOut_PP_5mA);
|
|
|
|
|
|
|
|
|
|
GPIOB_ModeCfg(USIM_DECT_PIN, GPIO_ModeIN_Floating);
|
|
|
|
|
|
|
|
|
|
GPIOB_ModeCfg(ML307_PWR_PIN, GPIO_ModeOut_PP_5mA);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int Module_Read_State(at_env_t *e)
|
|
|
|
|
{
|
|
|
|
|
switch (e->state)
|
|
|
|
|
{
|
|
|
|
|
case 0:
|
|
|
|
|
e->obj->adap->debug("Check if the Module is ready\r\n");
|
|
|
|
|
e->println(e, "AT");
|
|
|
|
|
e->reset_timer(e);
|
|
|
|
|
e->state++;
|
|
|
|
|
break;
|
|
|
|
|
case 1:
|
|
|
|
|
if (e->contains(e, "OK"))
|
|
|
|
|
{
|
|
|
|
|
e->recvclr(e);
|
|
|
|
|
e->finish(e, AT_RESP_OK);
|
|
|
|
|
e->obj->adap->debug("Module is ready\r\n");
|
|
|
|
|
}
|
|
|
|
|
if (e->is_timeout(e, 5000))
|
|
|
|
|
{
|
|
|
|
|
e->state--;
|
|
|
|
|
if (++e->i > 3)
|
|
|
|
|
{
|
|
|
|
|
e->obj->adap->debug("Module error\r\n");
|
|
|
|
|
e->finish(e, AT_RESP_ERROR);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void BSP_Read_Module(void)
|
|
|
|
|
{
|
|
|
|
|
at_do_work(ml307r_obj, NULL, Module_Read_State); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ч
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-20 11:26:08 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
*/
|
|
|
|
|
static void simcom_init_callback(at_response_t *r)
|
2024-12-11 16:21:57 +08:00
|
|
|
|
{
|
2025-03-20 11:26:08 +08:00
|
|
|
|
printf("SIM800C Init %s!\r\n",r->code == AT_RESP_OK ? "ok" : "error");
|
2024-12-11 16:21:57 +08:00
|
|
|
|
}
|
|
|
|
|
/*
|
2025-03-20 11:26:08 +08:00
|
|
|
|
* @brief ģ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>
|
|
|
|
|
*/
|
|
|
|
|
static void simcom_init(void)
|
2024-12-11 16:21:57 +08:00
|
|
|
|
{
|
|
|
|
|
at_attr_t attr;
|
2025-03-20 11:26:08 +08:00
|
|
|
|
static const char *cmds[] = {
|
|
|
|
|
"AT+GSN=1",
|
|
|
|
|
"AT+CIMI",
|
|
|
|
|
"AT+MCCID",
|
|
|
|
|
"AT+CPIN?",
|
|
|
|
|
"AT+CEREG?",
|
|
|
|
|
"AT+CSQ",
|
|
|
|
|
NULL,
|
|
|
|
|
};
|
2024-12-11 16:21:57 +08:00
|
|
|
|
at_attr_deinit(&attr);
|
2025-03-20 11:26:08 +08:00
|
|
|
|
attr.cb = simcom_init_callback; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ص<EFBFBD>
|
|
|
|
|
at_send_multiline(ml307r_obj, &attr, cmds);
|
2024-12-11 16:21:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-03-20 11:26:08 +08:00
|
|
|
|
/*
|
|
|
|
|
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>豸
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
static int BSP_Module_Connect_CtWing_Handle(at_env_t *e)
|
2024-12-11 16:21:57 +08:00
|
|
|
|
{
|
2025-03-20 11:26:08 +08:00
|
|
|
|
switch (e->state)
|
2024-12-11 16:21:57 +08:00
|
|
|
|
{
|
2025-03-20 11:26:08 +08:00
|
|
|
|
case 0:
|
|
|
|
|
logDebug("create device...\r\n");
|
2025-03-31 17:33:05 +08:00
|
|
|
|
e->println(e, "AT+MIPOPEN=0,\"TCP\",\"8.135.10.183\",32665,,1"); // 8.135.10.183,32994
|
2025-03-20 11:26:08 +08:00
|
|
|
|
e->reset_timer(e);
|
|
|
|
|
e->state++;
|
|
|
|
|
break;
|
|
|
|
|
case 1:
|
|
|
|
|
if (e->contains(e, "CONNECT"))
|
|
|
|
|
{
|
|
|
|
|
logDebug("create device complete\r\n");
|
|
|
|
|
e->finish(e, AT_RESP_OK);
|
|
|
|
|
}
|
|
|
|
|
else if (e->is_timeout(e, 5000))
|
|
|
|
|
{
|
|
|
|
|
e->state--;
|
|
|
|
|
if (++e->i > 3)
|
|
|
|
|
{
|
|
|
|
|
logDebug("create device error\r\n");
|
|
|
|
|
e->finish(e, AT_RESP_ERROR);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
2024-12-11 16:21:57 +08:00
|
|
|
|
}
|
2025-03-20 11:26:08 +08:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
//<2F><><EFBFBD>ӷ<EFBFBD><D3B7><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
static void BSP_Module_Connect_CtWing(void)
|
|
|
|
|
{
|
|
|
|
|
at_do_work(ml307r_obj, NULL, BSP_Module_Connect_CtWing_Handle);//<2F><><EFBFBD><EFBFBD><EFBFBD>豸
|
|
|
|
|
}
|
|
|
|
|
static int Module_Restart_Work(at_env_t *e)
|
|
|
|
|
{
|
|
|
|
|
switch (e->state)
|
2024-12-11 16:21:57 +08:00
|
|
|
|
{
|
2025-03-20 11:26:08 +08:00
|
|
|
|
case 0:
|
|
|
|
|
logDebug("restart module...\r\n");
|
|
|
|
|
e->println(e, "AT+CFUN=1,1");//<2F><><EFBFBD><EFBFBD>ģ<EFBFBD><C4A3>
|
|
|
|
|
e->reset_timer(e);
|
|
|
|
|
e->state++;
|
|
|
|
|
break;
|
|
|
|
|
case 1:
|
|
|
|
|
if (e->contains(e, "OK"))
|
|
|
|
|
{
|
|
|
|
|
logDebug("restart module complete\r\n");
|
|
|
|
|
e->reset_timer(e);
|
|
|
|
|
e->state++;
|
|
|
|
|
}
|
|
|
|
|
if (e->is_timeout(e, 5000))
|
|
|
|
|
{
|
|
|
|
|
e->state--;
|
|
|
|
|
if (++e->i > 3)
|
|
|
|
|
{
|
|
|
|
|
logDebug("Module restart error\r\n");
|
|
|
|
|
e->finish(e, AT_RESP_ERROR);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 2:
|
|
|
|
|
logDebug("Check if the Module is ready\r\n");
|
|
|
|
|
e->println(e, "AT");
|
|
|
|
|
e->reset_timer(e);
|
|
|
|
|
e->state++;
|
|
|
|
|
break;
|
|
|
|
|
case 3:
|
|
|
|
|
if (e->contains(e, "OK"))
|
|
|
|
|
{
|
|
|
|
|
e->recvclr(e);
|
|
|
|
|
e->finish(e, AT_RESP_OK);
|
|
|
|
|
logDebug("Module is ready\r\n");
|
|
|
|
|
}
|
|
|
|
|
if (e->is_timeout(e, 5000))
|
|
|
|
|
{
|
|
|
|
|
e->state--;
|
|
|
|
|
if (++e->i > 3)
|
|
|
|
|
{
|
|
|
|
|
logDebug("Module error\r\n");
|
|
|
|
|
e->finish(e, AT_RESP_ERROR);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
2024-12-11 16:21:57 +08:00
|
|
|
|
}
|
2025-03-20 11:26:08 +08:00
|
|
|
|
//<2F><><EFBFBD><EFBFBD>ģ<EFBFBD><C4A3>
|
|
|
|
|
static void BSP_Restart_Module(void)
|
2024-12-11 16:21:57 +08:00
|
|
|
|
{
|
2025-03-20 11:26:08 +08:00
|
|
|
|
at_do_work(ml307r_obj, NULL, Module_Restart_Work);//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ч
|
2024-12-11 16:21:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Ml307r_Loop(void)
|
|
|
|
|
{
|
|
|
|
|
at_obj_process(ml307r_obj);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
__HIGH_CODE
|
|
|
|
|
__attribute__((noinline))
|
|
|
|
|
uint16_t Ml307r_Handle(uint8_t task_id, uint16_t events)
|
|
|
|
|
{
|
|
|
|
|
if (events & ML307_PERIODIC_EVT)
|
|
|
|
|
{
|
|
|
|
|
Ml307r_Loop();
|
2025-03-31 17:33:05 +08:00
|
|
|
|
tmos_start_task(ml307_task_id, ML307_PERIODIC_EVT, MS1_TO_SYSTEM_TIME(1000)); //5
|
2024-12-11 16:21:57 +08:00
|
|
|
|
return (events ^ ML307_PERIODIC_EVT);
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2025-03-20 11:26:08 +08:00
|
|
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
void BSP_Module_Send_Data(uint8_t* data, uint8_t len)
|
|
|
|
|
{
|
|
|
|
|
at_attr_t attr;
|
|
|
|
|
at_send_data(ml307r_obj, &attr, data, len);
|
|
|
|
|
}
|
2024-12-11 16:21:57 +08:00
|
|
|
|
|
2025-03-31 17:33:05 +08:00
|
|
|
|
// <20>ϵ籣<CFB5><E7B1A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϱ<EFBFBD>
|
|
|
|
|
void BSP_Module_Emergency_Send(uint8_t* data, uint8_t len)
|
|
|
|
|
{
|
|
|
|
|
if (ml307_task_id != INVALID_TASK_ID) {
|
|
|
|
|
GPIOB_SetBits(ENABLE_3_8_V); // ȷ<><C8B7>3.8V<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
BSP_Read_Module(); // ȷ<><C8B7>ģ<EFBFBD><C4A3><EFBFBD>ڹ<EFBFBD><DAB9><EFBFBD>״̬
|
|
|
|
|
at_attr_t attr;
|
|
|
|
|
at_send_data(ml307r_obj, &attr, data, len);
|
|
|
|
|
Ml307_Power_Off();
|
|
|
|
|
logDebug("Power failure protection data has been sent\n");
|
|
|
|
|
} else {
|
|
|
|
|
BSP_Ml307r_Init(); // <20><><EFBFBD><EFBFBD>ģ<EFBFBD><C4A3>δ<EFBFBD><CEB4>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD>ȳ<EFBFBD>ʼ<EFBFBD><CABC>
|
|
|
|
|
logDebug("4G initialization\n");
|
|
|
|
|
at_attr_t attr;
|
|
|
|
|
at_send_data(ml307r_obj, &attr, data, len);
|
|
|
|
|
Ml307_Power_Off();
|
|
|
|
|
logDebug("Power failure protection data has been sent\n");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-11 16:21:57 +08:00
|
|
|
|
void BSP_Ml307r_Init(void)
|
|
|
|
|
{
|
|
|
|
|
Ml307r_Gpio_Init();
|
|
|
|
|
Ml307_Power_On();
|
|
|
|
|
BSP_UART1_Init();
|
|
|
|
|
|
|
|
|
|
ml307r_obj = at_obj_create(&at_adapter);//Create AT communication object
|
|
|
|
|
at_obj_set_urc(ml307r_obj, urc_table, sizeof(urc_table) / sizeof(urc_table[0]));
|
|
|
|
|
|
|
|
|
|
ml307_task_id = TMOS_ProcessEventRegister(Ml307r_Handle);
|
|
|
|
|
tmos_set_event(ml307_task_id, ML307_PERIODIC_EVT);
|
|
|
|
|
|
|
|
|
|
BSP_Read_Module();
|
2025-03-20 11:26:08 +08:00
|
|
|
|
simcom_init();
|
|
|
|
|
BSP_Module_Connect_CtWing();
|
|
|
|
|
BSP_Module_Send_Data(databuf, 5);
|
2025-03-31 17:33:05 +08:00
|
|
|
|
// Ml307_Power_Off();
|
2024-12-11 16:21:57 +08:00
|
|
|
|
}
|
|
|
|
|
|