Commit 0d43f25b authored by Karolis Tarasauskas's avatar Karolis Tarasauskas
Browse files

Added simple AT command query on WWAN modem

parent 90e46134
......@@ -3,39 +3,56 @@
*/
#include <stdio.h>
#include <string.h>
#include "errno.h"
#include "sdkconfig.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include "driver/uart.h"
#include "esp_log.h"
//-------Simple GPO module----------------
struct GPO_dev_t{
//-------Simple GPIO module----------------
struct GPIO_dev_t{
int pin;
};
int GPO_init(struct GPO_dev_t *dev, int pin);
void GPO_setState(struct GPO_dev_t *dev, bool state);
int GPIO_init(struct GPIO_dev_t *dev, int pin, bool output);
void GPIO_setDirection(struct GPIO_dev_t *dev, bool output);
void GPIO_setState(struct GPIO_dev_t *dev, bool state);
int GPO_init(struct GPO_dev_t *dev, int pin){
int GPIO_init(struct GPIO_dev_t *dev, int pin, bool output){
dev->pin = pin;
gpio_pad_select_gpio(dev->pin);
gpio_set_direction(dev->pin, GPIO_MODE_OUTPUT);
GPO_setState(dev, false);
GPIO_setDirection(dev, output);
GPIO_setState(dev, false);
return 0;
}
void GPO_setState(struct GPO_dev_t *dev, bool state){
void GPIO_setDirection(struct GPIO_dev_t *dev, bool output){
if(output)
gpio_set_direction(dev->pin, GPIO_MODE_OUTPUT);
else
gpio_set_direction(dev->pin, GPIO_MODE_INPUT);
}
void GPIO_setState(struct GPIO_dev_t *dev, bool state){
gpio_set_level(dev->pin, state);
}
//-------Simple GPO module----------------
int GPIO_getState(struct GPIO_dev_t *dev){
return gpio_get_level(dev->pin);
}
//-------Simple GPIO module----------------
......@@ -52,21 +69,107 @@ void GPO_setState(struct GPO_dev_t *dev, bool state){
#define LED3_PIN 14
#define LED4_PIN 16
#define WWAN_TX_PIN 27
#define WWAN_RX_PIN 26
#define WWAN_PWRKEY_PIN 4
#define WWAN_STATUS_PIN 22
#define RX_BUFF_SIZE 1024
static const uart_config_t wwan_uart_config={
.baud_rate = 115200,
.data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_DISABLE,
.stop_bits = UART_STOP_BITS_1,
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
.source_clk = UART_SCLK_APB,
};
struct WWAN_dev_t{
int tx_pin;
int rx_pin;
int pwr_pin;
int status_pin;
struct GPIO_dev_t pwrkey;
struct GPIO_dev_t status;
};
static void WWAN_init(struct WWAN_dev_t *dev, int tx_pin, int rx_pin, int pwr_pin, int status_pin){
if(!dev)
return;
memset(dev, 0, sizeof(struct WWAN_dev_t));
dev->tx_pin = tx_pin;
dev->rx_pin = rx_pin;
dev->pwr_pin = pwr_pin;
dev->status_pin = status_pin;
GPIO_init(&dev->pwrkey, dev->pwr_pin, true);
GPIO_init(&dev->status, dev->status_pin, false);
uart_driver_install(UART_NUM_1, RX_BUFF_SIZE * 2, 0, 0, NULL, 0);
uart_param_config(UART_NUM_1, &wwan_uart_config);
uart_set_pin(UART_NUM_1, tx_pin, rx_pin, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
}
static int WWAN_setState(struct WWAN_dev_t *dev, bool enable){
//read out current state
int currState = GPIO_getState(&dev->status);
//we're already at that state
if(currState == enable)
return 0;
//else transition into what we want
GPIO_setState(&dev->pwrkey, true);
vTaskDelay(1200 / portTICK_PERIOD_MS);
GPIO_setState(&dev->pwrkey, false);
//wait half a second + 2sec, and see if we reached desired state:
vTaskDelay(2500 / portTICK_PERIOD_MS);
int newState = GPIO_getState(&dev->status);
newState=GPIO_getState(&dev->status);
if(newState != enable)
return -EIO;
return 0;
}
static int WWAN_send(struct WWAN_dev_t *dev, const void *data, size_t len){
return uart_write_bytes(UART_NUM_1, data, len);
}
static int WWAN_receive(struct WWAN_dev_t *dev, uint8_t *data, size_t max_len){
return uart_read_bytes(UART_NUM_1, data, max_len, 1000 / portTICK_RATE_MS);
}
void app_main(void)
{
struct GPO_dev_t lock, buzz;
struct GPO_dev_t led[4];
struct GPIO_dev_t lock, buzz;
struct GPIO_dev_t led[4];
struct WWAN_dev_t wwan;
printf("Hello world!\n");
GPO_init(&lock, LOCK_PIN);
GPO_init(&buzz, BUZZ_PIN);
GPIO_init(&lock, LOCK_PIN, true);
GPIO_init(&buzz, BUZZ_PIN, true);
GPO_init(&led[0], LED1_PIN);
GPO_init(&led[1], LED2_PIN);
GPO_init(&led[2], LED3_PIN);
GPO_init(&led[3], LED4_PIN);
GPIO_init(&led[0], LED1_PIN, true);
GPIO_init(&led[1], LED2_PIN, true);
GPIO_init(&led[2], LED3_PIN, true);
GPIO_init(&led[3], LED4_PIN, true);
WWAN_init(&wwan, WWAN_TX_PIN, WWAN_RX_PIN, WWAN_PWRKEY_PIN, WWAN_STATUS_PIN);
while(1){
......@@ -78,22 +181,22 @@ void app_main(void)
int i;
for(i=0;i<4;i++){
GPO_setState(&led[i], true);
GPIO_setState(&led[i], true);
if(i>0)
GPO_setState(&led[i-1], false);
GPIO_setState(&led[i-1], false);
vTaskDelay(500 / portTICK_PERIOD_MS);
}
GPO_setState(&led[3], false);
GPIO_setState(&led[3], false);
vTaskDelay(500 / portTICK_PERIOD_MS);
ESP_LOGW("MAIN", "BUZZER test");
//now try to run the buzzer
GPO_setState(&buzz, true);
GPIO_setState(&buzz, true);
vTaskDelay(500 / portTICK_PERIOD_MS);
GPO_setState(&buzz, false);
GPIO_setState(&buzz, false);
vTaskDelay(500 / portTICK_PERIOD_MS);
ESP_LOGW("MAIN", "ADC test");
......@@ -103,13 +206,53 @@ void app_main(void)
ESP_LOGW("MAIN", "LOCK test");
//and run LOCK
GPO_setState(&lock, true);
GPIO_setState(&lock, true);
vTaskDelay(500 / portTICK_PERIOD_MS);
GPO_setState(&lock, false);
GPIO_setState(&lock, false);
vTaskDelay(500 / portTICK_PERIOD_MS);
//TODO GSM test is separate
ESP_LOGW("MAIN", "GSM test");
int ret = WWAN_setState(&wwan, false);
ESP_LOGW("MAIN", "WWAN disable=%d", ret);
vTaskDelay(5000 / portTICK_PERIOD_MS);
ret = WWAN_setState(&wwan, true);
ESP_LOGW("MAIN", "WWAN enable=%d", ret);
vTaskDelay(5000 / portTICK_PERIOD_MS);
while(1){
//now try to ask AT command to get any response:
ret = WWAN_send(&wwan, "AT\r\n", 4);
ESP_LOGW("MAIN", "sent to gsm %d", ret);
//try to receive something back:
uint8_t buffer[128];
memset(buffer, 0 , 128);
ret = WWAN_receive(&wwan, buffer, 127);
if(ret > 0 ){
ESP_LOGW("MAIN", "receive back [%d B] = %s", ret, buffer);
if(!(memcmp(buffer, "AT\r\r\nOK\r\n", 9))){
ESP_LOGW("MAIN", "WWAN RESPONSE OK");
break;
}
}else{
ESP_LOGW("MAIN", "WWAN receive back failed with %d", ret);
}
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
ret = WWAN_setState(&wwan, false);
ESP_LOGW("MAIN", "WWAN disable=%d", ret);
vTaskDelay(5000 / portTICK_PERIOD_MS);
ESP_LOGW("MAIN", "testing complete");
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment