diff --git a/Makefile b/Makefile index be6a43f..301df07 100644 --- a/Makefile +++ b/Makefile @@ -2,14 +2,14 @@ # 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/. -P=mqtt +P=main CC=xtensa-lx106-elf-gcc 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 -CFLAGS= -I. -mlongcalls -std=gnu11 +CFLAGS= -I. -mlongcalls -std=gnu11 -Wall LDFLAGS=-Teagle.app.v6.ld -OBJ=mqtt.o -DEPS=mqtt.h +OBJ=main.o mqtt.o onewire.o +DEPS=main.h mqtt.h onewire.h $(P)-0x00000.bin: $(P) 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 clean: - rm -f $(P) $(P).o $(P)-0x00000.bin $(P)-0x10000.bin + rm -f $(P) $(OBJ) $(P)-0x00000.bin $(P)-0x10000.bin library: $(CC) -c -fPIC $(CFLAGS) $(P).c -o $(P).o diff --git a/main.c b/main.c index f56d3f8..79ef9c6 100644 --- a/main.c +++ b/main.c @@ -1,8 +1,16 @@ +#include +#include "user_interface.h" +#include "ets_sys.h" +#include "os_type.h" +#include "osapi.h" +#include "mem.h" #include "espconn.h" -#include "main.h" #include "mqtt.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) { os_printf("Entered con!\n"); @@ -18,7 +26,7 @@ LOCAL void ICACHE_FLASH_ATTR pub(void *arg) { os_printf("Entered pub!\n"); mqtt_session_t *pSession = (mqtt_session_t *)arg; 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); os_timer_disarm(&oneTimer); 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() { - uint8_t wifiStatus; + //uint8_t wifiStatus; LOCAL mqtt_session_t globalSession; LOCAL mqtt_session_t *pGlobalSession = &globalSession; 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_arm(&testTimer, 16000, 0); } -#endif diff --git a/main.h b/main.h index b0fd905..846495c 100644 --- a/main.h +++ b/main.h @@ -1,5 +1,4 @@ -#include "mqtt.h" -#include "onewire.h" +#include "os_type.h" LOCAL void ICACHE_FLASH_ATTR con(void *arg); LOCAL void ICACHE_FLASH_ATTR pub(void *arg); diff --git a/mqtt.c b/mqtt.c index 66a3acc..5b2f0fd 100644 --- a/mqtt.c +++ b/mqtt.c @@ -22,18 +22,14 @@ * 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 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"); } -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; mqtt_session_t *session = pConn->reverse; // deal with received data @@ -73,13 +69,13 @@ LOCAL void ICACHE_FLASH_ATTR data_recv_callback(void *arg, char *pdata, unsigned break; } if(session->connack_cb != NULL) { - session->connack_cb(pData); + session->connack_cb(pdata); } break; case MQTT_MSG_TYPE_PUBLISH: os_printf("Application message from server: %s\n", &pdata[3]); // probably incorrect if(session->publish_cb != NULL) { - session->publish_cb(pData); + session->publish_cb(pdata); } break; 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: os_printf("Pong!\n"); 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; mqtt_session_t *pSession = pConn->reverse; #ifdef DEBUG @@ -108,16 +117,16 @@ LOCAL void ICACHE_FLASH_ATTR connected_callback(void *arg) { 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("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"); } -LOCAL uint8_t ICACHE_FLASH_ATTR tcpConnect(void *arg) { +uint8_t ICACHE_FLASH_ATTR tcpConnect(void *arg) { os_timer_disarm(&waitForWifiTimer); struct ip_info ipConfig; mqtt_session_t *session = arg; @@ -164,11 +173,13 @@ LOCAL uint8_t ICACHE_FLASH_ATTR tcpConnect(void *arg) { // set timer to try again os_timer_setfn(&waitForWifiTimer, (os_timer_func_t *)tcpConnect, session); 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 numBytes = 1; 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_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) { os_timer_disarm(&MQTT_KeepAliveTimer); // disable timer if we are called #ifdef DEBUG @@ -361,4 +372,5 @@ LOCAL uint8_t ICACHE_FLASH_ATTR mqtt_send(mqtt_session_t *session, uint8_t *data } else { os_printf("No wifi! Narf!\n"); } + return 0; } diff --git a/mqtt.h b/mqtt.h index 8772637..9f347b7 100644 --- a/mqtt.h +++ b/mqtt.h @@ -92,9 +92,9 @@ typedef struct { * @param arg A pointer to void, so it can be called from a timer or task * @return 0 on success */ -LOCAL uint8_t ICACHE_FLASH_ATTR tcpConnect(void *arg); -LOCAL void ICACHE_FLASH_ATTR disconnected_callback(void *arg); -LOCAL void ICACHE_FLASH_ATTR reconnected_callback(void *arg, sint8 err); +uint8_t ICACHE_FLASH_ATTR tcpConnect(void *arg); +void ICACHE_FLASH_ATTR disconnected_callback(void *arg); +void ICACHE_FLASH_ATTR reconnected_callback(void *arg, sint8 err); /** * 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. */ -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. @@ -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. */ -LOCAL void ICACHE_FLASH_ATTR data_recv_callback(void *arg, char *pdata, unsigned short len); -LOCAL void ICACHE_FLASH_ATTR data_sent_callback(void *arg); -LOCAL void ICACHE_FLASH_ATTR pingAlive(void *arg); +void ICACHE_FLASH_ATTR data_recv_callback(void *arg, char *pdata, unsigned short len); +void ICACHE_FLASH_ATTR data_sent_callback(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. @@ -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 * 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. @@ -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 * @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); diff --git a/onewire.c b/onewire.c index 85ae1d5..f6b973e 100644 --- a/onewire.c +++ b/onewire.c @@ -1,38 +1,73 @@ #include +#include "os_type.h" +#include "osapi.h" +#include "user_interface.h" #include "gpio.h" -#include "hw_timer.c" #include "onewire.h" -LOCAL void timerISR(void) { - return; -} - -LOCAL void reset(oneWire_t *owDev) { +sint8 ICACHE_FLASH_ATTR init(oneWire_t *owDev) { + uint32_t time; // reset the ow line, check for presence? + time = system_get_time() + 500; + gpio_output_set(0, BIT4, BIT4, 0); // pull GPIO4 low + while(system_get_time() < time); // delay 500uS + gpio_output_set(0, 0, 0, BIT4); // let go of GPIO4 + time = system_get_time() + 60; + while(system_get_time() < time); + uint8_t presence = (uint8_t)(gpio_input_get() >> 4) & 0x1; + if(presence) { + return 1; + } else { + return -1; + } } -LOCAL void ICACHE_FLASH_ATTR transact(oneWire_t *owDev, ds18b20_cmds cmd) { - switch(cmd) { - case READ_ROM: - break; - case MATCH_ROM: - break; - case SEARCH_ROM: - break; - case ALARM_SEARCH: - break; - case SKIP_ROM: - break; - case CONVERT_T: - break; - case SCRATCH_READ: - break; - case SCRATCH_WRITE: - break; - case SCRATCH_COPY: - break; - case E2_RECALL: - break; +void ICACHE_FLASH_ATTR transact(oneWire_t *owDev, ds18b20_cmds cmd) { + uint32_t time; + uint8_t sendBit; + uint8_t inBit = 0x00; + uint8_t inByte = 0x00; + if((cmd == SKIP_ROM) | (cmd == CONVERT_T)) { + 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); + } + } + } 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; + } } - } diff --git a/onewire.h b/onewire.h index 2e4c474..6c36d46 100644 --- a/onewire.h +++ b/onewire.h @@ -1,5 +1,6 @@ #include -#include "hw_timer.c" +#include "os_type.h" +#include "gpio.h" /** * @struct oneWire_t @@ -7,14 +8,14 @@ */ typedef struct { uint8_t address[8]; - uint8_t scratchpad[20]; + uint8_t scratchpad[9]; } oneWire_t; /** * @enum ds18b20_cmds * 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, MATCH_ROM = 0x55, SEARCH_ROM = 0xF0, @@ -25,9 +26,7 @@ enum ds18b20_cmds { SCRATCH_WRITE = 0x4E, SCRATCH_COPY = 0x48, E2_RECALL = 0xB8 -}; +} ds18b20_cmds; - -LOCAL void timerISR(void); -LOCAL void ICACHE_FLASH_ATTR reset(oneWire_t *owDev); -LOCAL void ICACHE_FLASH_ATTR transact(oneWire_t *owDev, ds18b20_cmds cmd); +sint8 ICACHE_FLASH_ATTR init(oneWire_t *owDev); +void ICACHE_FLASH_ATTR transact(oneWire_t *owDev, ds18b20_cmds cmd);