PCB configuration moved to subdirectory /pcbs

This commit is contained in:
Martin Jaeger
2019-03-10 16:42:44 +01:00
parent 676948c5ad
commit 871c8e46be
8 changed files with 19 additions and 628 deletions
+2 -229
View File
@@ -18,241 +18,14 @@
#ifdef GSM_ENABLED // otherwise don't compile code to reduce firmware size
#include "pcb.h"
#include "interface.h"
#include "thingset.h"
#include "gprs.h"
#include "MQTTPacket.h"
#include "hardware.h"
#include "eeprom.h"
#include "leds.h"
#include <inttypes.h>
GPRS SIM800(PIN_UEXT_TX, PIN_UEXT_RX, 9600, (char *)GSM_NUMBER);
DigitalOut gsm_en(PIN_UEXT_MOSI);
int mqtt_connect();
int mqtt_send_pub_packet();
int mqtt_send_sub_packet();
int mqtt_disconnect();
int mqtt_state_machine();
extern uint32_t deviceID;
extern ThingSet ts;
extern const int PUB_CHANNEL_MQTT;
// for publish messages
const int qos = 1;
const int dup_flag = 0;
const int retain_flag = 0;
int packet_id = 0;
// for subscribe messages
int qos_sub = 0; // is it different to qos of publish messages?
int packet_id_sub = 1; // can we use above packet_id also for publish messages?
int last_call = 0; // Initialise the sending interval timer
const int sleep_time = 300 ; // GSM Sleep period
enum gsm_states {
GSM_STATE_INIT, // initial state
GSM_STATE_SIM_READY, // SIM card recognized and initialized
GSM_STATE_SSL_READY, // SSL certificate imported and state set
GSM_STATE_NETWORK_AVAILABLE, // successfully registered in GSM network
GSM_STATE_IP_CONNECTED, // got IP address
GSM_STATE_TCP_MQTT, // TCP connection has been established, MQTT ongoing
GSM_STATE_SLEEP, // sleeping and waiting for wake-up
};
enum mqtt_states {
MQTT_STATE_READY, // Initial state
MQTT_CONNECTED, // MQTT connection established
MQTT_PUBLISHED, // Publish the thingset message to the server
MQTT_SUBSCRIBED, // MQTT subscribed and waiting for incoming messages
MQTT_THINGSET_PROCESSED, // Incoming messages have been processed
};
void gsm_init(void)
{
// only enable GSM module, rest is done in state machine
gsm_en = 1;
// TODO
}
// State machine for GSM module
void gsm_process(void)
{
static gsm_states state = GSM_STATE_INIT;
switch (state)
{
case GSM_STATE_INIT:
{
printf("\n IN GSM INIT");
if (SIM800.init() == -1)
return; // try again next time
/* NEED TO SET PIN?
if (SIM800.check_pin() == false) {
// pin needed --> set it
if (SIM800.set_pin(SIM_PIN) == false)
return;
}
*/
state = GSM_STATE_SIM_READY;
break;
}
case GSM_STATE_SIM_READY:
{
if (SIM800.activateSSL() == -1)
return;
if (SIM800.enableSSL() == -1) // import certificate
return;
state = GSM_STATE_SSL_READY;
break;
}
case GSM_STATE_SSL_READY:
{
if (SIM800.network_availablity() == -1)
return; // try again next time
if (SIM800.checkSignalStrength() <= 0)
return; // try again next time
state = GSM_STATE_NETWORK_AVAILABLE;
break;
}
case GSM_STATE_NETWORK_AVAILABLE:
{
if (SIM800.attach1() == -1)
return; // try again next time*/
/* APN SETTINGS BLOCKING TCP CONNECTION
if (SIM800.networkInit(APN, USERNAME, PASSWORD) == -1)
return; // try again next time
*/
if (SIM800.getIP() == -1)
return; // try again next time
state = GSM_STATE_IP_CONNECTED; // if everything was successful
break;
}
case GSM_STATE_IP_CONNECTED:
{
if (SIM800.connectTCP((char *)MQTT_HOST, (char *)MQTT_PORT) == -1)
return;
state = GSM_STATE_TCP_MQTT;
break;
}
case GSM_STATE_TCP_MQTT:
{
if (mqtt_state_machine() == -1)
return;
SIM800.closeTCP();
SIM800.GSM_sleep();
last_call = time(NULL);
state = GSM_STATE_SLEEP;
break;
}
case GSM_STATE_SLEEP:
{
// do something
break;
}
}
}
// State machine for MQTT
int mqtt_state_machine()
{
static mqtt_states state = MQTT_STATE_READY;
switch (state) {
case MQTT_STATE_READY:
if (mqtt_connect())
state = MQTT_CONNECTED;
break;
case MQTT_CONNECTED:
if (mqtt_send_pub_packet())
state = MQTT_PUBLISHED;
break;
case MQTT_PUBLISHED:
if (mqtt_send_sub_packet())
state = MQTT_SUBSCRIBED;
break;
case MQTT_SUBSCRIBED:
// Some code to read packets and pass to thingset
state = MQTT_THINGSET_PROCESSED;
break;
case MQTT_THINGSET_PROCESSED:
//Disconnect from MQTT
break;
}
return 0;
}
int mqtt_connect()
{
char id_buf[20];
snprintf(id_buf, sizeof(id_buf), "%" PRIu32, deviceID);
MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
data.clientID.cstring = id_buf;
data.keepAliveInterval = 60;
data.cleansession = 0;
data.username.cstring = (char *)MQTT_USER;
data.password.cstring = (char *)MQTT_PASS;
data.MQTTVersion = 4;
unsigned char buf[100];
int len = MQTTSerialize_connect(buf, sizeof(buf), &data);
return (SIM800.sendTCPData(buf, len) != -1);
}
int mqtt_send_pub_packet()
{
char topic_buf[50];
MQTTString topic = MQTTString_initializer;
snprintf(topic_buf, sizeof(topic_buf), "%s/%" PRIu32, MQTT_PUBLISH_TOPIC, deviceID);
topic.cstring = topic_buf;
uint8_t pub_data[100];
int len_cbor = ts.pub_msg_cbor(pub_data, sizeof(pub_data), PUB_CHANNEL_MQTT);
unsigned char mqtt_packet[200];
int len_mqtt = MQTTSerialize_publish(mqtt_packet, sizeof(mqtt_packet), dup_flag, qos, retain_flag, packet_id, topic, pub_data+1, len_cbor-1);
packet_id++;
return (SIM800.sendTCPData(mqtt_packet, len_mqtt) != -1);
}
int mqtt_send_sub_packet()
{
char topic_buf[50];
MQTTString topic = MQTTString_initializer;
snprintf(topic_buf, sizeof(topic_buf), "%s/%" PRIu32, MQTT_SUBSCRIBE_TOPIC, deviceID);
topic.cstring = topic_buf;
unsigned char buf[200];
int len = MQTTSerialize_subscribe(buf, sizeof(buf), 0, packet_id_sub, 1, &topic, &qos_sub);
packet_id++;
return (SIM800.sendTCPData(buf, len) != -1);
}
int mqtt_disconnect()
{
unsigned char buf[50];
int len = MQTTSerialize_disconnect(buf, sizeof(buf));
return (SIM800.sendTCPData(buf, len) != -1);
// TODO
}
#endif /* GSM_ENABLED */
+2 -286
View File
@@ -18,298 +18,14 @@
#ifdef LORA_ENABLED // otherwise don't compile code to reduce firmware size
#include "pcb.h"
#include "interface.h"
#include "sx1276-mbed-hal.h"
#include "thingset.h"
#include "hardware.h"
#include "eeprom.h"
#include "leds.h"
SPI spi_uext(PIN_UEXT_MOSI, PIN_UEXT_MISO, PIN_UEXT_SCK, PIN_UEXT_SSEL);
void send_dataset2();
extern Serial serial;
int last_call = 0; // Initialise the sending interval timer
int last_call_2 = 0; // Initialise the sending interval timer for sending the 2nd packet
const int interval_1 = 300; // Sending interval for lora_1 message in seconds (should be 5 minutes in production)
//const int interval_1 = 30;
const int interval_2 = 43200; // Sending interval for lora_2 message in seconds (should be 12 hours)
//const int interval_2 = 90;
const int temp_interval = 10; // Temporary interval in seconds to send a new message shortly after receiving a message
int wait_time;
int num_timeouts = 0;
// ThingSet instance and buffers
extern ThingSet ts;
extern const int PUB_CHANNEL_LORA_DAILY;
extern const int PUB_CHANNEL_LORA_REGULARLY;
uint8_t lora_resp[55] = {0xff, 0xff, 0x00, 0x00}; // These bytes are required by the radiohead library on the hub
uint8_t lora_req[51];
extern load_output_t load;
extern uint32_t deviceID;
#define DEBUG_MESSAGE 1
/* Set this flag to '1' to use the LoRa modulation or to '0' to use FSK modulation */
#define USE_MODEM_LORA 1
#define USE_MODEM_FSK !USE_MODEM_LORA
#define RF_FREQUENCY 867000000 // Hz
#define TX_OUTPUT_POWER 20 // 20 dBm
#if USE_MODEM_LORA == 1
#define LORA_BANDWIDTH 125000 // LoRa default, details in SX1276::BandwidthMap
//#define LORA_SPREADING_FACTOR LORA_SF8
#define LORA_SPREADING_FACTOR LORA_SF12
#define LORA_CODINGRATE LORA_ERROR_CODING_RATE_4_5
#define LORA_PREAMBLE_LENGTH 8 // Same for Tx and Rx
#define LORA_SYMBOL_TIMEOUT 5 // Symbols
#define LORA_FIX_LENGTH_PAYLOAD_ON false
#define LORA_FHSS_ENABLED false
#define LORA_NB_SYMB_HOP 4
#define LORA_IQ_INVERSION_ON false
#define LORA_CRC_ENABLED true
#elif USE_MODEM_FSK == 1
#define FSK_FDEV 25000 // Hz
#define FSK_DATARATE 19200 // bps
#define FSK_BANDWIDTH 50000 // Hz
#define FSK_AFC_BANDWIDTH 83333 // Hz
#define FSK_PREAMBLE_LENGTH 5 // Same for Tx and Rx
#define FSK_FIX_LENGTH_PAYLOAD_ON false
#define FSK_CRC_ENABLED true
#else
#error "Please define a modem in the compiler options."
#endif
#define RX_TIMEOUT_VALUE 5000 // in ms
/*
* Global variables declarations
*/
typedef enum
{
LOWPOWER = 0,
IDLE,
RX,
RX_TIMEOUT,
RX_ERROR,
TX,
TX_TIMEOUT,
CAD,
CAD_DONE
} AppStates_t;
volatile AppStates_t State = LOWPOWER;
/*!
* Radio events function pointer
*/
static RadioEvents_t RadioEvents;
/*
* Global variables declarations
*/
SX1276Generic Radio(NULL, RFM95_SX1276,
PIN_UEXT_MOSI, PIN_UEXT_MISO, PIN_UEXT_SCK, PIN_UEXT_SSEL, PIN_UEXT_SDA,
PIN_UEXT_TX, PIN_UEXT_RX, NC, NC, NC, NC);
// Add LED for uplink messages
void OnTxDone(void *radio, void *userThisPtr, void *userData)
{
Radio.Sleep( );
State = TX;
if (DEBUG_MESSAGE)
serial.printf("> OnTxDone\n");
}
// Add LED for Downlink messages and trigger a new uplink message in 10 seconds or so for confirmation
void OnRxDone(void *radio, void *userThisPtr, void *userData, uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr)
{
Radio.Sleep();
State = RX;
num_timeouts = 0;
if (DEBUG_MESSAGE) {
serial.printf("> OnRxDone: RssiValue=%d dBm, SnrValue=%d, Payload=%d\n", rssi, snr, payload);
for (int i = 0; i < size - 4; i++) {
serial.putc(payload[i]);
}
}
if (size > 4) {
// TODO: Implement LoRa protocol to make sure that only messages
// with matching deviceID are received
ts.process(payload + 4, size - 4, lora_resp, sizeof(lora_resp));
}
wait_time = temp_interval;
trigger_rx_led();
}
void OnTxTimeout(void *radio, void *userThisPtr, void *userData)
{
Radio.Sleep( );
State = TX_TIMEOUT;
if(DEBUG_MESSAGE)
serial.printf("> OnTxTimeout\n");
}
void OnRxTimeout(void *radio, void *userThisPtr, void *userData)
{
if (num_timeouts < 1){
if (DEBUG_MESSAGE)
serial.printf("Num RX Timeouts: %d\n", num_timeouts);
Radio.Rx(RX_TIMEOUT_VALUE);
num_timeouts++;
}
else
{
Radio.Sleep( );
State = RX_TIMEOUT;
if (DEBUG_MESSAGE)
serial.printf("> OnRxTimeout\n");
num_timeouts = 0;
if (time(NULL) > last_call_2 + interval_2)
{
send_dataset2();
}
}
}
void OnRxError(void *radio, void *userThisPtr, void *userData)
{
Radio.Sleep( );
State = RX_ERROR;
if (DEBUG_MESSAGE)
serial.printf("> OnRxError\n");
}
void lora_init(void)
{
serial.printf("Cloud Solar LoRa implementation\n");
serial.printf("Freqency: %.1f\n", (double)RF_FREQUENCY/1000000.0);
serial.printf("TXPower: %d dBm\n", TX_OUTPUT_POWER);
#if USE_MODEM_LORA == 1
serial.printf("Bandwidth: %d Hz\n", LORA_BANDWIDTH);
serial.printf("Spreading factor: SF%d\n", LORA_SPREADING_FACTOR);
#elif USE_MODEM_FSK == 1
serial.printf("Bandwidth: %d kHz\n", FSK_BANDWIDTH);
serial.printf("Baudrate: %d\n", FSK_DATARATE);
#endif
// Initialize Radio driver
RadioEvents.TxDone = OnTxDone;
RadioEvents.RxDone = OnRxDone;
RadioEvents.RxError = OnRxError;
RadioEvents.TxTimeout = OnTxTimeout;
RadioEvents.RxTimeout = OnRxTimeout;
if (Radio.Init(&RadioEvents) == false) {
serial.printf("Radio could not be detected!\n");
return;
}
switch(Radio.DetectBoardType()) {
case SX1276MB1LAS:
if (DEBUG_MESSAGE)
serial.printf(" > Board Type: SX1276MB1LAS <");
break;
case SX1276MB1MAS:
if (DEBUG_MESSAGE)
serial.printf(" > Board Type: SX1276MB1LAS <");
break;
case MURATA_SX1276:
if (DEBUG_MESSAGE)
serial.printf(" > Board Type: MURATA_SX1276_STM32L0 <");
break;
case RFM95_SX1276:
if (DEBUG_MESSAGE)
serial.printf(" > HopeRF RFM95xx <\n");
break;
default:
serial.printf(" > Board Type: unknown <");
}
Radio.SetChannel(RF_FREQUENCY);
#if USE_MODEM_LORA == 1
if (LORA_FHSS_ENABLED)
serial.printf(" > LORA FHSS Mode <\n");
if (!LORA_FHSS_ENABLED)
serial.printf(" > LORA Mode <\n");
Radio.SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH,
LORA_SPREADING_FACTOR, LORA_CODINGRATE,
LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON,
LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP,
LORA_IQ_INVERSION_ON, 2000 );
Radio.SetRxConfig( MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR,
LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH,
LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON, 0,
LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP,
LORA_IQ_INVERSION_ON, true );
#elif USE_MODEM_FSK == 1
serial.printf(" > FSK Mode <");
Radio.SetTxConfig( MODEM_FSK, TX_OUTPUT_POWER, FSK_FDEV, 0,
FSK_DATARATE, 0,
FSK_PREAMBLE_LENGTH, FSK_FIX_LENGTH_PAYLOAD_ON,
FSK_CRC_ENABLED, 0, 0, 0, 2000 );
Radio.SetRxConfig( MODEM_FSK, FSK_BANDWIDTH, FSK_DATARATE,
0, FSK_AFC_BANDWIDTH, FSK_PREAMBLE_LENGTH,
0, FSK_FIX_LENGTH_PAYLOAD_ON, 0, FSK_CRC_ENABLED,
0, 0, false, true );
#else
#error "Please define a modem in the compiler options."
#endif
// TODO
}
void lora_process(void)
{
// if the time interval to send data has passed
if (time(NULL) > last_call + wait_time)
{
// Send the next LoRa 1 UPLINK frame
int len = ts.pub_msg_cbor(lora_resp + 4, sizeof(lora_resp) - 4, PUB_CHANNEL_LORA_REGULARLY);
if (DEBUG_MESSAGE)
serial.printf("Sending dataset 1...\n");
Radio.Send(lora_resp, len + 4);
// We wait to see if there is a response from the hub
Radio.Rx(RX_TIMEOUT_VALUE);
// Update last send time
last_call = time(NULL);
wait_time = interval_1;
trigger_tx_led();
}
// TODO
}
void send_dataset2(void)
{
// Send the next LoRa 2 UPLINK frame
int len = ts.pub_msg_cbor(lora_resp + 4, sizeof(lora_resp) - 4, PUB_CHANNEL_LORA_DAILY);
if (DEBUG_MESSAGE)
serial.printf("Sending dataset 2...\n");
Radio.Send(lora_resp, len + 4);
// We wait to see if there is a response from the hub
Radio.Rx( RX_TIMEOUT_VALUE );
// Update last send time
last_call_2 = time(NULL);
trigger_tx_led();
}
#endif /* LORA_ENABLED */
+15 -11
View File
@@ -1,5 +1,5 @@
/* LibreSolar MPPT charge controller firmware
* Copyright (c) 2016-2018 Martin Jäger (www.libre.solar)
/* LibreSolar charge controller firmware
* Copyright (c) 2016-2019 Martin Jäger (www.libre.solar)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -14,11 +14,13 @@
* limitations under the License.
*/
#ifndef _PCB_H_
#define _PCB_H_
#ifndef PCB_H
#define PCB_H
/* This file includes the PCB specific settings depending on the PCB selected
* as a compiler define flag in platformio.h
/** @file
*
* @brief
* Includes PCB hardware specific settings like pin names depending on the compiler flags set in platformio.ini.
*/
// generic settings (for all charge controller boards)
@@ -26,15 +28,17 @@
#define HIGH_SIDE_VOLTAGE_MAX 55
#if defined(PCB_LS_005)
#include "pcb_ls_005.h"
#include "pcbs/pcb_ls_005.h"
#elif defined(PCB_LS_010)
#include "pcb_ls_010.h"
#include "pcbs/pcb_ls_010.h"
#elif defined(PCB_CS_02)
#include "pcb_cs_02.h"
#include "pcbs/pcb_cs_02.h"
#elif defined(PCB_CS_04)
#include "pcb_cs_04.h"
#include "pcbs/pcb_cs_04.h"
#elif defined(PCB_PWM_01)
#include "pcbs/pcb_pwm_01.h"
#else
#error "PCB has to be specified!"
#endif
#endif /* _PCB_H_ */
#endif /* PCB_H */
-102
View File
@@ -1,102 +0,0 @@
/* LibreSolar MPPT charge controller firmware
* Copyright (c) 2016-2017 Martin Jäger (www.libre.solar)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef __PCB_CS_03_H_
#define __PCB_CS_03_H_
#include "mbed.h"
// DC/DC converter settings
#define PWM_FREQUENCY 70 // kHz 50 = better for cloud solar to increase efficiency
#define PWM_TIM 3 // use TIM3 timer
#define DCDC_CURRENT_MAX 8 // PCB maximum DCDC output current
#define LOAD_CURRENT_MAX 10 // PCB maximum load switch current
#define PIN_UEXT_TX PA_2
#define PIN_UEXT_RX PA_3
#define PIN_UEXT_SCL PB_6
#define PIN_UEXT_SDA PB_7
#define PIN_UEXT_MISO PB_4
#define PIN_UEXT_MOSI PB_5
#define PIN_UEXT_SCK PB_3
#define PIN_UEXT_SSEL PA_15
#define PIN_SWD_TX PA_9
#define PIN_SWD_RX PA_10
#define PIN_LED_SOC_12 PB_13
#define PIN_LED_SOC_3 PB_2
#define PIN_LED_SOLAR PB_11
#define PIN_LED_LOAD PB_10
#define PIN_LED_GND PB_14
#define PIN_LOAD_EN PC_13
#define PIN_USB_PWR_EN PB_12
#define PIN_REF_I_DCDC PA_4
#define EEPROM_VERSION 1 // versioning of EEPROM layout (2 bytes)
// typical value for Semitec 103AT-5 thermistor: 3435
#define NTC_BETA_VALUE 3435
#define ADC_GAIN_V_BAT (126.8 / 6.8) // both voltage dividers: 120k + 6.8k
#define ADC_GAIN_V_SOLAR (126.8 / 6.8)
#define ADC_GAIN_I_LOAD (1000 / 4 / 50) // amp gain: 50, resistor: 4 mOhm
#define ADC_GAIN_I_DCDC (1000 / 4 / 50)
// position in the array written by the DMA controller
enum {
ADC_POS_V_BAT, // ADC 0 (PA_0)
ADC_POS_V_SOLAR, // ADC 1 (PA_1)
ADC_POS_TEMP_FETS, // ADC 5 (PA_5)
ADC_POS_I_LOAD, // ADC 6 (PA_6)
ADC_POS_I_DCDC, // ADC 7 (PA_7)
#if defined(STM32F0)
ADC_POS_TEMP_MCU, // ADC 16
ADC_POS_VREF_MCU, // ADC 17
#elif defined(STM32L0)
ADC_POS_VREF_MCU, // ADC 17
ADC_POS_TEMP_MCU, // ADC 18
#endif
NUM_ADC_CH // trick to get the number of enums
};
// selected ADC channels (has to match with above enum)
#if defined(STM32F0)
#define ADC_CHSEL ( \
ADC_CHSELR_CHSEL0 | \
ADC_CHSELR_CHSEL1 | \
ADC_CHSELR_CHSEL5 | \
ADC_CHSELR_CHSEL6 | \
ADC_CHSELR_CHSEL7 | \
ADC_CHSELR_CHSEL16 | \
ADC_CHSELR_CHSEL17 \
)
#elif defined(STM32L0)
#define ADC_CHSEL ( \
ADC_CHSELR_CHSEL0 | \
ADC_CHSELR_CHSEL1 | \
ADC_CHSELR_CHSEL5 | \
ADC_CHSELR_CHSEL6 | \
ADC_CHSELR_CHSEL7 | \
ADC_CHSELR_CHSEL17 | \
ADC_CHSELR_CHSEL18 \
)
#endif
#endif