First version that compiles with new file layout.

Had to make many changes to get it to compile across many files,
including removing the LOCAL specifier from almost all functions.
Edited the Makefile to compile all three files and link them.
Haven't tested on hardware yet, that's the next step.

Lots of small changes to avoid warnings, now that I have turned
-Wall on. This makes the code a bit "better".

Signed-off-by: A.M. Rowsell <amrowsell@frozenelectronics.ca>
This commit is contained in:
A.M. Rowsell 2019-01-20 21:13:04 -05:00
commit 48702bf328
Signed by: amr
GPG key ID: 0B6E2D8375CF79A9
7 changed files with 124 additions and 72 deletions

View file

@ -2,14 +2,14 @@
# License, v. 2.0. If a copy of the MPL was not distributed with this # License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/. # file, You can obtain one at http://mozilla.org/MPL/2.0/.
P=mqtt P=main
CC=xtensa-lx106-elf-gcc CC=xtensa-lx106-elf-gcc
AR=xtensa-lx106-elf-ar AR=xtensa-lx106-elf-ar
LDLIBS=-nostdlib -ggdb -Wl,-Map=output.map -Wl,--start-group -lm -lc -lhal -lpp -llwip -lphy -lnet80211 -lwpa -lat -lwpa2 -lmain -Wl,--end-group -lgcc LDLIBS=-nostdlib -ggdb -Wl,-Map=output.map -Wl,--start-group -lm -lc -lhal -lpp -llwip -lphy -lnet80211 -lwpa -lat -lwpa2 -lmain -Wl,--end-group -lgcc
CFLAGS= -I. -mlongcalls -std=gnu11 CFLAGS= -I. -mlongcalls -std=gnu11 -Wall
LDFLAGS=-Teagle.app.v6.ld LDFLAGS=-Teagle.app.v6.ld
OBJ=mqtt.o OBJ=main.o mqtt.o onewire.o
DEPS=mqtt.h DEPS=main.h mqtt.h onewire.h
$(P)-0x00000.bin: $(P) $(P)-0x00000.bin: $(P)
esptool.py elf2image $^ esptool.py elf2image $^
@ -22,7 +22,7 @@ flash: $(P)-0x00000.bin
esptool.py --port /dev/feather1 write_flash 0 $(P)-0x00000.bin 0x10000 $(P)-0x10000.bin esptool.py --port /dev/feather1 write_flash 0 $(P)-0x00000.bin 0x10000 $(P)-0x10000.bin
clean: clean:
rm -f $(P) $(P).o $(P)-0x00000.bin $(P)-0x10000.bin rm -f $(P) $(OBJ) $(P)-0x00000.bin $(P)-0x10000.bin
library: library:
$(CC) -c -fPIC $(CFLAGS) $(P).c -o $(P).o $(CC) -c -fPIC $(CFLAGS) $(P).c -o $(P).o

15
main.c
View file

@ -1,8 +1,16 @@
#include <stdint.h>
#include "user_interface.h"
#include "ets_sys.h"
#include "os_type.h"
#include "osapi.h"
#include "mem.h"
#include "espconn.h" #include "espconn.h"
#include "main.h"
#include "mqtt.h" #include "mqtt.h"
#include "onewire.h" #include "onewire.h"
#include "main.h"
static os_timer_t oneTimer;
static os_timer_t testTimer;
LOCAL void ICACHE_FLASH_ATTR con(void *arg) { LOCAL void ICACHE_FLASH_ATTR con(void *arg) {
os_printf("Entered con!\n"); os_printf("Entered con!\n");
@ -18,7 +26,7 @@ LOCAL void ICACHE_FLASH_ATTR pub(void *arg) {
os_printf("Entered pub!\n"); os_printf("Entered pub!\n");
mqtt_session_t *pSession = (mqtt_session_t *)arg; mqtt_session_t *pSession = (mqtt_session_t *)arg;
uint8_t data[] = "2.718281828459045"; uint8_t data[] = "2.718281828459045";
uint32_t dataLen = os_strlen(data); int32_t dataLen = os_strlen(data);
mqtt_send(pSession, &data[0], dataLen, MQTT_MSG_TYPE_PUBLISH); mqtt_send(pSession, &data[0], dataLen, MQTT_MSG_TYPE_PUBLISH);
os_timer_disarm(&oneTimer); os_timer_disarm(&oneTimer);
os_timer_setfn(&oneTimer, (os_timer_func_t *)ping, arg); os_timer_setfn(&oneTimer, (os_timer_func_t *)ping, arg);
@ -49,7 +57,7 @@ LOCAL void ICACHE_FLASH_ATTR discon(void *arg) {
} }
void ICACHE_FLASH_ATTR user_init() { void ICACHE_FLASH_ATTR user_init() {
uint8_t wifiStatus; //uint8_t wifiStatus;
LOCAL mqtt_session_t globalSession; LOCAL mqtt_session_t globalSession;
LOCAL mqtt_session_t *pGlobalSession = &globalSession; LOCAL mqtt_session_t *pGlobalSession = &globalSession;
char ssid[32] = "yourwifissid"; char ssid[32] = "yourwifissid";
@ -94,4 +102,3 @@ void ICACHE_FLASH_ATTR user_init() {
os_timer_setfn(&testTimer, (os_timer_func_t *)con, pGlobalSession); os_timer_setfn(&testTimer, (os_timer_func_t *)con, pGlobalSession);
os_timer_arm(&testTimer, 16000, 0); os_timer_arm(&testTimer, 16000, 0);
} }
#endif

3
main.h
View file

@ -1,5 +1,4 @@
#include "mqtt.h" #include "os_type.h"
#include "onewire.h"
LOCAL void ICACHE_FLASH_ATTR con(void *arg); LOCAL void ICACHE_FLASH_ATTR con(void *arg);
LOCAL void ICACHE_FLASH_ATTR pub(void *arg); LOCAL void ICACHE_FLASH_ATTR pub(void *arg);

42
mqtt.c
View file

@ -22,18 +22,14 @@
* security or QoS in this basic implementation. * security or QoS in this basic implementation.
*/ */
#ifdef DEBUG
static os_timer_t oneTimer;
static os_timer_t testTimer;
#endif
static os_timer_t MQTT_KeepAliveTimer; static os_timer_t MQTT_KeepAliveTimer;
static os_timer_t waitForWifiTimer; static os_timer_t waitForWifiTimer;
LOCAL void ICACHE_FLASH_ATTR data_sent_callback(void *arg) { void ICACHE_FLASH_ATTR data_sent_callback(void *arg) {
os_printf("Data sent!\n"); os_printf("Data sent!\n");
} }
LOCAL void ICACHE_FLASH_ATTR data_recv_callback(void *arg, char *pdata, unsigned short len) { void ICACHE_FLASH_ATTR data_recv_callback(void *arg, char *pdata, unsigned short len) {
struct espconn *pConn = arg; struct espconn *pConn = arg;
mqtt_session_t *session = pConn->reverse; mqtt_session_t *session = pConn->reverse;
// deal with received data // deal with received data
@ -73,13 +69,13 @@ LOCAL void ICACHE_FLASH_ATTR data_recv_callback(void *arg, char *pdata, unsigned
break; break;
} }
if(session->connack_cb != NULL) { if(session->connack_cb != NULL) {
session->connack_cb(pData); session->connack_cb(pdata);
} }
break; break;
case MQTT_MSG_TYPE_PUBLISH: case MQTT_MSG_TYPE_PUBLISH:
os_printf("Application message from server: %s\n", &pdata[3]); // probably incorrect os_printf("Application message from server: %s\n", &pdata[3]); // probably incorrect
if(session->publish_cb != NULL) { if(session->publish_cb != NULL) {
session->publish_cb(pData); session->publish_cb(pdata);
} }
break; break;
case MQTT_MSG_TYPE_SUBACK: case MQTT_MSG_TYPE_SUBACK:
@ -91,11 +87,24 @@ LOCAL void ICACHE_FLASH_ATTR data_recv_callback(void *arg, char *pdata, unsigned
case MQTT_MSG_TYPE_PINGRESP: case MQTT_MSG_TYPE_PINGRESP:
os_printf("Pong!\n"); os_printf("Pong!\n");
break; break;
// all remaining cases listed to avoid warnings
case MQTT_MSG_TYPE_CONNECT:
case MQTT_MSG_TYPE_PUBACK:
case MQTT_MSG_TYPE_PUBREC:
case MQTT_MSG_TYPE_PUBREL:
case MQTT_MSG_TYPE_PUBCOMP:
case MQTT_MSG_TYPE_SUBSCRIBE:
case MQTT_MSG_TYPE_UNSUBSCRIBE:
case MQTT_MSG_TYPE_PINGREQ:
case MQTT_MSG_TYPE_DISCONNECT:
default:
return;
break;
} }
} }
LOCAL void ICACHE_FLASH_ATTR connected_callback(void *arg) { void ICACHE_FLASH_ATTR connected_callback(void *arg) {
struct espconn *pConn = arg; struct espconn *pConn = arg;
mqtt_session_t *pSession = pConn->reverse; mqtt_session_t *pSession = pConn->reverse;
#ifdef DEBUG #ifdef DEBUG
@ -108,16 +117,16 @@ LOCAL void ICACHE_FLASH_ATTR connected_callback(void *arg) {
pSession->validConnection = 1; pSession->validConnection = 1;
} }
LOCAL void ICACHE_FLASH_ATTR reconnected_callback(void *arg, sint8 err) { void ICACHE_FLASH_ATTR reconnected_callback(void *arg, sint8 err) {
os_printf("Reconnected?\n"); os_printf("Reconnected?\n");
os_printf("Error code: %d\n", err); os_printf("Error code: %d\n", err);
} }
LOCAL void ICACHE_FLASH_ATTR disconnected_callback(void *arg) { void ICACHE_FLASH_ATTR disconnected_callback(void *arg) {
os_printf("Disconnected\n"); os_printf("Disconnected\n");
} }
LOCAL uint8_t ICACHE_FLASH_ATTR tcpConnect(void *arg) { uint8_t ICACHE_FLASH_ATTR tcpConnect(void *arg) {
os_timer_disarm(&waitForWifiTimer); os_timer_disarm(&waitForWifiTimer);
struct ip_info ipConfig; struct ip_info ipConfig;
mqtt_session_t *session = arg; mqtt_session_t *session = arg;
@ -164,11 +173,13 @@ LOCAL uint8_t ICACHE_FLASH_ATTR tcpConnect(void *arg) {
// set timer to try again // set timer to try again
os_timer_setfn(&waitForWifiTimer, (os_timer_func_t *)tcpConnect, session); os_timer_setfn(&waitForWifiTimer, (os_timer_func_t *)tcpConnect, session);
os_timer_arm(&waitForWifiTimer, 1000, 0); os_timer_arm(&waitForWifiTimer, 1000, 0);
return 2;
} }
return 0;
} }
LOCAL uint8_t ICACHE_FLASH_ATTR *encodeLength(uint32_t trueLength) { uint8_t ICACHE_FLASH_ATTR *encodeLength(uint32_t trueLength) {
uint8_t *encodedByte = os_zalloc(sizeof(uint8_t) * 5); // can't be more than 5 bytes uint8_t *encodedByte = os_zalloc(sizeof(uint8_t) * 5); // can't be more than 5 bytes
uint8_t numBytes = 1; uint8_t numBytes = 1;
do { do {
@ -185,12 +196,12 @@ LOCAL uint8_t ICACHE_FLASH_ATTR *encodeLength(uint32_t trueLength) {
} }
LOCAL void ICACHE_FLASH_ATTR pingAlive(void *arg) { void ICACHE_FLASH_ATTR pingAlive(void *arg) {
mqtt_session_t *pSession = (mqtt_session_t *)arg; mqtt_session_t *pSession = (mqtt_session_t *)arg;
mqtt_send(pSession, NULL, 0, MQTT_MSG_TYPE_PINGREQ); mqtt_send(pSession, NULL, 0, MQTT_MSG_TYPE_PINGREQ);
} }
LOCAL uint8_t ICACHE_FLASH_ATTR mqtt_send(mqtt_session_t *session, uint8_t *data, uint32_t len, mqtt_message_type msgType) { uint8_t ICACHE_FLASH_ATTR mqtt_send(mqtt_session_t *session, uint8_t *data, uint32_t len, mqtt_message_type msgType) {
if(session->validConnection == 1) { if(session->validConnection == 1) {
os_timer_disarm(&MQTT_KeepAliveTimer); // disable timer if we are called os_timer_disarm(&MQTT_KeepAliveTimer); // disable timer if we are called
#ifdef DEBUG #ifdef DEBUG
@ -361,4 +372,5 @@ LOCAL uint8_t ICACHE_FLASH_ATTR mqtt_send(mqtt_session_t *session, uint8_t *data
} else { } else {
os_printf("No wifi! Narf!\n"); os_printf("No wifi! Narf!\n");
} }
return 0;
} }

18
mqtt.h
View file

@ -92,9 +92,9 @@ typedef struct {
* @param arg A pointer to void, so it can be called from a timer or task * @param arg A pointer to void, so it can be called from a timer or task
* @return 0 on success * @return 0 on success
*/ */
LOCAL uint8_t ICACHE_FLASH_ATTR tcpConnect(void *arg); uint8_t ICACHE_FLASH_ATTR tcpConnect(void *arg);
LOCAL void ICACHE_FLASH_ATTR disconnected_callback(void *arg); void ICACHE_FLASH_ATTR disconnected_callback(void *arg);
LOCAL void ICACHE_FLASH_ATTR reconnected_callback(void *arg, sint8 err); void ICACHE_FLASH_ATTR reconnected_callback(void *arg, sint8 err);
/** /**
* A callback function which is called when TCP connection is successful. * A callback function which is called when TCP connection is successful.
@ -103,7 +103,7 @@ LOCAL void ICACHE_FLASH_ATTR reconnected_callback(void *arg, sint8 err);
* *
* This function is called once the TCP connection to the server is completed. This allows us to register the callbacks for received and sent data, as well as enable TCP keepalive and set validConnection in mqtt_session_t to 1 to show the connection has been successful. * This function is called once the TCP connection to the server is completed. This allows us to register the callbacks for received and sent data, as well as enable TCP keepalive and set validConnection in mqtt_session_t to 1 to show the connection has been successful.
*/ */
LOCAL void ICACHE_FLASH_ATTR connected_callback(void *arg); void ICACHE_FLASH_ATTR connected_callback(void *arg);
/** /**
* A callback function that deals with received data. * A callback function that deals with received data.
@ -114,9 +114,9 @@ LOCAL void ICACHE_FLASH_ATTR connected_callback(void *arg);
* *
* At this point the function does little more than detect what type of MQTT message is sent, and prints out debug info to the serial port. The next few versions should improve this so the user can register their own callback functions which will be called when the particular message type is receieved. * At this point the function does little more than detect what type of MQTT message is sent, and prints out debug info to the serial port. The next few versions should improve this so the user can register their own callback functions which will be called when the particular message type is receieved.
*/ */
LOCAL void ICACHE_FLASH_ATTR data_recv_callback(void *arg, char *pdata, unsigned short len); void ICACHE_FLASH_ATTR data_recv_callback(void *arg, char *pdata, unsigned short len);
LOCAL void ICACHE_FLASH_ATTR data_sent_callback(void *arg); void ICACHE_FLASH_ATTR data_sent_callback(void *arg);
LOCAL void ICACHE_FLASH_ATTR pingAlive(void *arg); void ICACHE_FLASH_ATTR pingAlive(void *arg);
/** /**
* A function which takes a uint32_t and returns a pointer to a properly formatted MQTT length. * A function which takes a uint32_t and returns a pointer to a properly formatted MQTT length.
@ -127,7 +127,7 @@ LOCAL void ICACHE_FLASH_ATTR pingAlive(void *arg);
* The MQTT standard uses a very strange method of encoding the lengths of the various sections * The MQTT standard uses a very strange method of encoding the lengths of the various sections
* of the packets. This makes it simpler to implement in code. * of the packets. This makes it simpler to implement in code.
*/ */
LOCAL uint8_t ICACHE_FLASH_ATTR *encodeLength(uint32_t trueLength); uint8_t ICACHE_FLASH_ATTR *encodeLength(uint32_t trueLength);
/** /**
* This function handles all the sending of various MQTT messages. * This function handles all the sending of various MQTT messages.
@ -137,4 +137,4 @@ LOCAL uint8_t ICACHE_FLASH_ATTR *encodeLength(uint32_t trueLength);
* @param msgType the type of message to be send, one of the mqtt_message_type * @param msgType the type of message to be send, one of the mqtt_message_type
* @return -1 in case of error, 0 otherwise * @return -1 in case of error, 0 otherwise
*/ */
LOCAL uint8_t ICACHE_FLASH_ATTR mqtt_send(mqtt_session_t *session, uint8_t *data, uint32_t len, mqtt_message_type msgType); uint8_t ICACHE_FLASH_ATTR mqtt_send(mqtt_session_t *session, uint8_t *data, uint32_t len, mqtt_message_type msgType);

View file

@ -1,38 +1,73 @@
#include <stdint.h> #include <stdint.h>
#include "os_type.h"
#include "osapi.h"
#include "user_interface.h"
#include "gpio.h" #include "gpio.h"
#include "hw_timer.c"
#include "onewire.h" #include "onewire.h"
LOCAL void timerISR(void) { sint8 ICACHE_FLASH_ATTR init(oneWire_t *owDev) {
return; uint32_t time;
}
LOCAL void reset(oneWire_t *owDev) {
// reset the ow line, check for presence? // reset the ow line, check for presence?
} time = system_get_time() + 500;
gpio_output_set(0, BIT4, BIT4, 0); // pull GPIO4 low
LOCAL void ICACHE_FLASH_ATTR transact(oneWire_t *owDev, ds18b20_cmds cmd) { while(system_get_time() < time); // delay 500uS
switch(cmd) { gpio_output_set(0, 0, 0, BIT4); // let go of GPIO4
case READ_ROM: time = system_get_time() + 60;
break; while(system_get_time() < time);
case MATCH_ROM: uint8_t presence = (uint8_t)(gpio_input_get() >> 4) & 0x1;
break; if(presence) {
case SEARCH_ROM: return 1;
break; } else {
case ALARM_SEARCH: return -1;
break; }
case SKIP_ROM: }
break;
case CONVERT_T: void ICACHE_FLASH_ATTR transact(oneWire_t *owDev, ds18b20_cmds cmd) {
break; uint32_t time;
case SCRATCH_READ: uint8_t sendBit;
break; uint8_t inBit = 0x00;
case SCRATCH_WRITE: uint8_t inByte = 0x00;
break; if((cmd == SKIP_ROM) | (cmd == CONVERT_T)) {
case SCRATCH_COPY: for(uint8_t i = 7; i >= 0; i--) {
break; sendBit = (cmd >> i) & 0x1;
case E2_RECALL: if(sendBit == 1) {
break; time = system_get_time() + 10;
gpio_output_set(0, BIT4, BIT4, 0); // pull low
while(system_get_time() < time);
gpio_output_set(0, 0, 0, BIT4); // let go
} else {
time = system_get_time() + 100;
gpio_output_set(0, BIT4, BIT4, 0); //pull low
while(system_get_time() < time);
gpio_output_set(0, 0, 0, BIT4);
}
}
} else if(cmd == SCRATCH_READ) {
for(uint8_t i = 7; i >= 0; i--) {
sendBit = (cmd >> i) & 0x1;
if(sendBit == 1) {
time = system_get_time() + 10;
gpio_output_set(0, BIT4, BIT4, 0); // pull low
while(system_get_time() < time);
gpio_output_set(0, 0, 0, BIT4); // let go
} else {
time = system_get_time() + 100;
gpio_output_set(0, BIT4, BIT4, 0); //pull low
while(system_get_time() < time);
gpio_output_set(0, 0, 0, BIT4);
}
}
// now read the scratchpad
for(uint8_t i = 0; i < 2; i++) {
for(uint8_t j = 0; j < 8; j++) {
time = system_get_time() + 8;
gpio_output_set(0, BIT4, BIT4, 0);
while(system_get_time() < time);
gpio_output_set(0, 0, 0, BIT4);
inBit = (uint8_t)(gpio_input_get() >> 4) & 0x01;
inByte |= inBit << j;
}
owDev->scratchpad[i] = inByte;
}
} }
} }

View file

@ -1,5 +1,6 @@
#include <stdint.h> #include <stdint.h>
#include "hw_timer.c" #include "os_type.h"
#include "gpio.h"
/** /**
* @struct oneWire_t * @struct oneWire_t
@ -7,14 +8,14 @@
*/ */
typedef struct { typedef struct {
uint8_t address[8]; uint8_t address[8];
uint8_t scratchpad[20]; uint8_t scratchpad[9];
} oneWire_t; } oneWire_t;
/** /**
* @enum ds18b20_cmds * @enum ds18b20_cmds
* This enum simply gives easy to read names to all the various one-wire commands that the DS18B20 can accept * This enum simply gives easy to read names to all the various one-wire commands that the DS18B20 can accept
*/ */
enum ds18b20_cmds { typedef enum {
READ_ROM = 0x33, READ_ROM = 0x33,
MATCH_ROM = 0x55, MATCH_ROM = 0x55,
SEARCH_ROM = 0xF0, SEARCH_ROM = 0xF0,
@ -25,9 +26,7 @@ enum ds18b20_cmds {
SCRATCH_WRITE = 0x4E, SCRATCH_WRITE = 0x4E,
SCRATCH_COPY = 0x48, SCRATCH_COPY = 0x48,
E2_RECALL = 0xB8 E2_RECALL = 0xB8
}; } ds18b20_cmds;
sint8 ICACHE_FLASH_ATTR init(oneWire_t *owDev);
LOCAL void timerISR(void); void ICACHE_FLASH_ATTR transact(oneWire_t *owDev, ds18b20_cmds cmd);
LOCAL void ICACHE_FLASH_ATTR reset(oneWire_t *owDev);
LOCAL void ICACHE_FLASH_ATTR transact(oneWire_t *owDev, ds18b20_cmds cmd);