Commit 9cab7513 authored by Vowstar's avatar Vowstar
Browse files

Merge pull request #1 from MarsTechHAN/master

Update to MarsTechHAN's nodemcu-firmware
parents a2d1e5ff ad1ec8fb
eclipse.preferences.version=1 eclipse.preferences.version=1
environment/project/cdt.managedbuild.toolchain.gnu.cross.base.992255352/PATH/delimiter=; environment/project/cdt.managedbuild.toolchain.gnu.cross.base.992255352/PATH/delimiter=;
environment/project/cdt.managedbuild.toolchain.gnu.cross.base.992255352/PATH/operation=append environment/project/cdt.managedbuild.toolchain.gnu.cross.base.992255352/PATH/operation=append
environment/project/cdt.managedbuild.toolchain.gnu.cross.base.992255352/PATH/value=C\:\\Espressif\\xtensa-lx106-elf\\bin environment/project/cdt.managedbuild.toolchain.gnu.cross.base.992255352/PATH/value=C\:\\Espressif\\xtensa-lx106-elf\\bin;C\:\\MinGW\\bin;C\:\\MinGW\\msys\\1.0\\bin;C\:\\Python27
environment/project/cdt.managedbuild.toolchain.gnu.cross.base.992255352/append=true environment/project/cdt.managedbuild.toolchain.gnu.cross.base.992255352/append=true
environment/project/cdt.managedbuild.toolchain.gnu.cross.base.992255352/appendContributed=true environment/project/cdt.managedbuild.toolchain.gnu.cross.base.992255352/appendContributed=true
...@@ -30,7 +30,11 @@ ifeq ($(OS),Windows_NT) ...@@ -30,7 +30,11 @@ ifeq ($(OS),Windows_NT)
OBJCOPY = xtensa-lx106-elf-objcopy OBJCOPY = xtensa-lx106-elf-objcopy
endif endif
FIRMWAREDIR = ..\\bin\\ FIRMWAREDIR = ..\\bin\\
ESPPORT = com1 ifndef COMPORT
ESPPORT = com1
else
ESPPORT = $(COMPORT)
endif
ifeq ($(PROCESSOR_ARCHITECTURE),AMD64) ifeq ($(PROCESSOR_ARCHITECTURE),AMD64)
# ->AMD64 # ->AMD64
endif endif
...@@ -40,7 +44,11 @@ ifeq ($(OS),Windows_NT) ...@@ -40,7 +44,11 @@ ifeq ($(OS),Windows_NT)
else else
# We are under other system, may be Linux. Assume using gcc. # We are under other system, may be Linux. Assume using gcc.
# Can we use -fdata-sections? # Can we use -fdata-sections?
ESPPORT = /dev/ttyUSB0 ifndef COMPORT
ESPPORT = /dev/ttyUSB0
else
ESPPORT = $(COMPORT)
endif
CCFLAGS += -Os -ffunction-sections -fno-jump-tables CCFLAGS += -Os -ffunction-sections -fno-jump-tables
AR = xtensa-lx106-elf-ar AR = xtensa-lx106-elf-ar
CC = xtensa-lx106-elf-gcc CC = xtensa-lx106-elf-gcc
......
# **NodeMcu** # # **NodeMcu** #
version 0.9.5 version 0.9.5
###A lua based firmware for wifi-soc esp8266 ###A lua based firmware for wifi-soc esp8266
Build on [ESP8266 sdk 0.9.5](http://bbs.espressif.com/viewtopic.php?f=7&t=104)<br /> Build on [ESP8266 sdk 0.9.5](http://bbs.espressif.com/viewtopic.php?f=5&t=154)<br />
Lua core based on [eLua project](http://www.eluaproject.net/)<br /> Lua core based on [eLua project](http://www.eluaproject.net/)<br />
File system based on [spiffs](https://github.com/pellepl/spiffs)<br /> File system based on [spiffs](https://github.com/pellepl/spiffs)<br />
Open source development kit for NodeMCU [nodemcu-devkit](https://github.com/nodemcu/nodemcu-devkit)<br /> Open source development kit for NodeMCU [nodemcu-devkit](https://github.com/nodemcu/nodemcu-devkit)<br />
Flash tool for NodeMCU [nodemcu-flasher](https://github.com/nodemcu/nodemcu-flasher)<br /> Flash tool for NodeMCU [nodemcu-flasher](https://github.com/nodemcu/nodemcu-flasher)<br />
wiki: [nodemcu wiki](https://github.com/nodemcu/nodemcu-firmware/wiki)<br /> wiki: [nodemcu wiki](https://github.com/nodemcu/nodemcu-firmware/wiki)<br />
api: [nodemcu api](https://github.com/nodemcu/nodemcu-firmware/wiki/nodemcu_api_en)<br />
home: [nodemcu.com](http://www.nodemcu.com)<br /> home: [nodemcu.com](http://www.nodemcu.com)<br />
bbs: [中文论坛Chinese bbs](http://bbs.nodemcu.com)<br /> bbs: [Chinese bbs](http://bbs.nodemcu.com)<br />
Tencent QQ group QQ群: 309957875<br /> Tencent QQ group: 309957875<br />
# Summary # Summary
- Easy to access wireless router - Easy to access wireless router
- Based on Lua 5.1.4 (without *io, math, debug, os* module.) - Based on Lua 5.1.4 (without *io, math, debug, os* module.)
- Event-Drive programming preferred. - Event-Drive programming preferred.
- Build-in file, timer, pwm, i2c, 1-wire, net, gpio, wifi, adc, uart and system api. - Build-in file, timer, pwm, i2c, spi, 1-wire, net, mqtt, gpio, wifi, adc, uart and system api.
- GPIO pin re-mapped, use the index to access gpio, i2c, pwm. - GPIO pin re-mapped, use the index to access gpio, i2c, pwm.
# To Do List (pull requests are very welcomed) # To Do List (pull requests are very welcomed)
- fix wifi smart connect - fix wifi smart connect
- add spi module - add spi module (done)
- add mqtt module - add mqtt module (done)
- add coap module - add coap module
- cross compiler
# Change log # Change log
2015-01-27<br />
support floating point LUA.<br />
use macro LUA_NUMBER_INTEGRAL in user_config.h control this feature.<br />
LUA_NUMBER_INTEGRAL to disable floating point support,<br />
// LUA_NUMBER_INTEGRAL to enable floating point support.<br />
fix tmr.time(). #132<br />
fix filesystem length. #113<br />
fix ssl reboots. #134<br />
build pre_build bin.
2015-01-26<br />
applied sdk095_patch1 to sdk 0.9.5.<br />
added LUA examples and modules [by dvv](https://github.com/dvv). <br />
added node.readvdd33() API [by alonewolfx2](https://github.com/alonewolfx2).<br />
build pre_build bin.
2015-01-24<br />
migrate to sdk 0.9.5 release.<br />
tmr.time() now return second(not precise yet). <br />
build pre_build bin.
2015-01-23<br />
merge mqtt branch to master.<br />
build pre_build bin.
2015-01-18<br />
merge mqtt module to [new branch mqtt](https://github.com/nodemcu/nodemcu-firmware/tree/mqtt) from [https://github.com/tuanpmt/esp_mqtt](https://github.com/tuanpmt/esp_mqtt).<br />
merge spi module from iabdalkader:spi. <br />
fix #110,set local port to random in client mode.<br />
modify gpio.read to NOT set pin to input mode automatic.<br />
add PATH env with C:\MinGW\bin;C:\MinGW\msys\1.0\bin;C:\Python27 in eclipse project. resolve #103.
2015-01-08<br /> 2015-01-08<br />
fix net.socket:send() issue when multi sends are called. <br /> fix net.socket:send() issue when multi sends are called. <br />
*NOTE*: if data length is bigger than 1460, send next packet AFTER "sent" callback is called.<br /> *NOTE*: if data length is bigger than 1460, send next packet AFTER "sent" callback is called.<br />
fix file.read() api, take 0xFF as a regular byte, not EOF.<br /> fix file.read() api, take 0xFF as a regular byte, not EOF.<br />
pre_build/latest/nodemcu_512k_latest.bin is removed. use pre_build/latest/nodemcu_latest.bin instead. pre_build/latest/nodemcu_512k_latest.bin is removed. use pre_build/latest/nodemcu_latest.bin instead.
2015-01-07<br /> [more change log](https://github.com/nodemcu/nodemcu-firmware/wiki)<br />
retrive more ram back.<br />
add api file.format() to rebuild file system.<br />
rename "NodeMcu" to "NodeMCU" in firmware.<br />
add some check for file system op.
[more change log](https://github.com/nodemcu/nodemcu-firmware/wiki/nodemcu_api_en#change_log)<br />
[更多变更日志](https://github.com/nodemcu/nodemcu-firmware/wiki/nodemcu_api_cn#change_log)
##GPIO NEW TABLE ( Build 20141219 and later) ##GPIO NEW TABLE ( Build 20141219 and later)
...@@ -75,39 +102,6 @@ add some check for file system op. ...@@ -75,39 +102,6 @@ add some check for file system op.
</table> </table>
#### [*] D0(GPIO16) can only be used as gpio read/write. no interrupt supported. no pwm/i2c/ow supported. #### [*] D0(GPIO16) can only be used as gpio read/write. no interrupt supported. no pwm/i2c/ow supported.
##GPIO OLD TABLE (Before build 20141212)
<a id="old_gpio_map"></a>
<table>
<tr>
<th scope="col">IO index</th><th scope="col">ESP8266 pin</th><th scope="col">IO index</th><th scope="col">ESP8266 pin</th>
</tr>
<tr>
<td>0</td><td>GPIO12</td><td>8</td><td>GPIO0</td>
</tr>
<tr>
<td>1</td><td>GPIO13</td><td>9</td><td>GPIO2</td>
</tr>
<tr>
<td>2</td><td>GPIO14</td><td>10</td><td>GPIO4</td>
</tr>
<tr>
<td>3</td><td>GPIO15</td><td>11</td><td>GPIO5</td>
</tr>
<tr>
<td>4</td><td>GPIO3</td><td></td><td></td>
</tr>
<tr>
<td>5</td><td>GPIO1</td><td></td><td></td>
</tr>
<tr>
<td>6</td><td>GPIO9</td><td></td><td></td>
</tr>
<tr>
<td>7</td><td>GPIO10</td<td></td><td></td>
</tr>
</table>
#Build option #Build option
####file ./app/include/user_config.h ####file ./app/include/user_config.h
```c ```c
...@@ -132,6 +126,8 @@ add some check for file system op. ...@@ -132,6 +126,8 @@ add some check for file system op.
#define LUA_USE_MODULES_OW #define LUA_USE_MODULES_OW
#define LUA_USE_MODULES_BIT #define LUA_USE_MODULES_BIT
#endif /* LUA_USE_MODULES */ #endif /* LUA_USE_MODULES */
...
// LUA_NUMBER_INTEGRAL
``` ```
#Flash the firmware #Flash the firmware
...@@ -140,10 +136,8 @@ for most esp8266 modules, just pull GPIO0 down and restart.<br /> ...@@ -140,10 +136,8 @@ for most esp8266 modules, just pull GPIO0 down and restart.<br />
You can use the [nodemcu-flasher](https://github.com/nodemcu/nodemcu-flasher) to burn the firmware. You can use the [nodemcu-flasher](https://github.com/nodemcu/nodemcu-flasher) to burn the firmware.
Or, if you build your own bin from source code.<br /> Or, if you build your own bin from source code.<br />
eagle.app.v6.flash.bin: 0x00000<br /> 0x00000.bin: 0x00000<br />
eagle.app.v6.irom0text.bin: 0x10000<br /> 0x10000.bin: 0x10000<br />
esp_init_data_default.bin: 0x7c000<br />
blank.bin: 0x7e000<br />
*Better run file.format() after flash* *Better run file.format() after flash*
...@@ -155,11 +149,13 @@ baudrate:9600 ...@@ -155,11 +149,13 @@ baudrate:9600
####Connect to your ap ####Connect to your ap
```lua ```lua
print(wifi.sta.getip()) ip = wifi.sta.getip()
--0.0.0.0 print(ip)
--nil
wifi.setmode(wifi.STATION) wifi.setmode(wifi.STATION)
wifi.sta.config("SSID","password") wifi.sta.config("SSID","password")
print(wifi.sta.getip()) ip = wifi.sta.getip()
print(ip)
--192.168.18.110 --192.168.18.110
``` ```
...@@ -197,6 +193,56 @@ baudrate:9600 ...@@ -197,6 +193,56 @@ baudrate:9600
end) end)
``` ```
####Connect to MQTT Broker
```lua
-- init mqtt client with keepalive timer 120sec
m = mqtt.Client("clientid", 120, "user", "password")
-- setup Last Will and Testament (optional)
-- Broker will publish a message with qos = 0, retain = 0, data = "offline"
-- to topic "/lwt" if client don't send keepalive packet
m:lwt("/lwt", "offline", 0, 0)
m:on("connect", function(con) print ("connected") end)
m:on("offline", function(con) print ("offline") end)
-- on publish message receive event
m:on("message", function(conn, topic, data)
print(topic .. ":" )
if data ~= nil then
print(data)
end
end)
-- for secure: m:connect("192.168.11.118", 1880, 1)
m:connect("192.168.11.118", 1880, 0, function(conn) print("connected") end)
-- subscribe topic with qos = 0
m:subscribe("/topic",0, function(conn) print("subscribe success") end)
-- publish a message with data = hello, QoS = 0, retain = 0
m:publish("/topic","hello",0,0, function(conn) print("sent") end)
m:close();
-- you can call m:connect again
```
#### UDP client and server
```lua
-- a udp server
s=net.createServer(net.UDP)
s:on("receive",function(s,c) print(c) end)
s:listen(5683)
-- a udp client
cu=net.createConnection(net.UDP)
cu:on("receive",function(cu,c) print(c) end)
cu:connect(5683,"192.168.18.101")
cu:send("hello")
```
####Do something shining ####Do something shining
```lua ```lua
function led(r,g,b) function led(r,g,b)
......
...@@ -26,11 +26,11 @@ SUBDIRS= \ ...@@ -26,11 +26,11 @@ SUBDIRS= \
driver \ driver \
lwip \ lwip \
json \ json \
ssl \
upgrade \ upgrade \
platform \ platform \
libc \ libc \
lua \ lua \
mqtt \
smart \ smart \
wofs \ wofs \
modules \ modules \
...@@ -41,6 +41,7 @@ endif # } PDIR ...@@ -41,6 +41,7 @@ endif # } PDIR
APPDIR = . APPDIR = .
LDDIR = ../ld LDDIR = ../ld
CCFLAGS += -Os
TARGET_LDFLAGS = \ TARGET_LDFLAGS = \
-nostdlib \ -nostdlib \
...@@ -48,7 +49,6 @@ TARGET_LDFLAGS = \ ...@@ -48,7 +49,6 @@ TARGET_LDFLAGS = \
--longcalls \ --longcalls \
--text-section-literals --text-section-literals
ifeq ($(FLAVOR),debug) ifeq ($(FLAVOR),debug)
TARGET_LDFLAGS += -g -O2 TARGET_LDFLAGS += -g -O2
endif endif
...@@ -72,11 +72,11 @@ COMPONENTS_eagle.app.v6 = \ ...@@ -72,11 +72,11 @@ COMPONENTS_eagle.app.v6 = \
driver/libdriver.a \ driver/libdriver.a \
lwip/liblwip.a \ lwip/liblwip.a \
json/libjson.a \ json/libjson.a \
ssl/libssl.a \
upgrade/libupgrade.a \ upgrade/libupgrade.a \
platform/libplatform.a \ platform/libplatform.a \
libc/liblibc.a \ libc/liblibc.a \
lua/liblua.a \ lua/liblua.a \
mqtt/mqtt.a \
smart/smart.a \ smart/smart.a \
wofs/wofs.a \ wofs/wofs.a \
spiffs/spiffs.a \ spiffs/spiffs.a \
...@@ -99,6 +99,8 @@ LINKFLAGS_eagle.app.v6 = \ ...@@ -99,6 +99,8 @@ LINKFLAGS_eagle.app.v6 = \
-lwpa \ -lwpa \
-lmain \ -lmain \
-ljson \ -ljson \
-lsmartconfig \
-lssl \
$(DEP_LIBS_eagle.app.v6) \ $(DEP_LIBS_eagle.app.v6) \
-Wl,--end-group -Wl,--end-group
......
...@@ -219,6 +219,45 @@ i2c_master_getAck(void) ...@@ -219,6 +219,45 @@ i2c_master_getAck(void)
return retVal; return retVal;
} }
/******************************************************************************
* FunctionName : i2c_master_checkAck
* Description : get dev response
* Parameters : NONE
* Returns : true : get ack ; false : get nack
*******************************************************************************/
bool ICACHE_FLASH_ATTR
i2c_master_checkAck(void)
{
if(i2c_master_getAck()){
return FALSE;
}else{
return TRUE;
}
}
/******************************************************************************
* FunctionName : i2c_master_send_ack
* Description : response ack
* Parameters : NONE
* Returns : NONE
*******************************************************************************/
void ICACHE_FLASH_ATTR
i2c_master_send_ack(void)
{
i2c_master_setAck(0x0);
}
/******************************************************************************
* FunctionName : i2c_master_send_nack
* Description : response nack
* Parameters : NONE
* Returns : NONE
*******************************************************************************/
void ICACHE_FLASH_ATTR
i2c_master_send_nack(void)
{
i2c_master_setAck(0x1);
}
/****************************************************************************** /******************************************************************************
* FunctionName : i2c_master_readByte * FunctionName : i2c_master_readByte
* Description : read Byte from i2c bus * Description : read Byte from i2c bus
......
...@@ -349,11 +349,6 @@ pwm_init(uint16 freq, uint16 *duty) ...@@ -349,11 +349,6 @@ pwm_init(uint16 freq, uint16 *duty)
{ {
uint8 i; uint8 i;
ETS_FRC_TIMER1_INTR_ATTACH(pwm_tim1_intr_handler, NULL);
TM1_EDGE_INT_ENABLE();
ETS_FRC1_INTR_ENABLE();
RTC_CLR_REG_MASK(FRC1_INT_ADDRESS, FRC1_INT_CLR_MASK);
RTC_REG_WRITE(FRC1_CTRL_ADDRESS, //FRC2_AUTO_RELOAD| RTC_REG_WRITE(FRC1_CTRL_ADDRESS, //FRC2_AUTO_RELOAD|
DIVDED_BY_16 DIVDED_BY_16
| FRC1_ENABLE_TIMER | FRC1_ENABLE_TIMER
...@@ -377,6 +372,10 @@ pwm_init(uint16 freq, uint16 *duty) ...@@ -377,6 +372,10 @@ pwm_init(uint16 freq, uint16 *duty)
// pwm_set_freq_duty(freq, duty); // pwm_set_freq_duty(freq, duty);
pwm_start(); pwm_start();
ETS_FRC_TIMER1_INTR_ATTACH(pwm_tim1_intr_handler, NULL);
TM1_EDGE_INT_ENABLE();
ETS_FRC1_INTR_ENABLE();
} }
bool ICACHE_FLASH_ATTR bool ICACHE_FLASH_ATTR
......
...@@ -64,12 +64,11 @@ void spi_lcd_9bit_write(uint8 spi_no,uint8 high_bit,uint8 low_8bit) ...@@ -64,12 +64,11 @@ void spi_lcd_9bit_write(uint8 spi_no,uint8 high_bit,uint8 low_8bit)
* Description : SPI master initial function for common byte units transmission * Description : SPI master initial function for common byte units transmission
* Parameters : uint8 spi_no - SPI module number, Only "SPI" and "HSPI" are valid * Parameters : uint8 spi_no - SPI module number, Only "SPI" and "HSPI" are valid
*******************************************************************************/ *******************************************************************************/
void spi_master_init(uint8 spi_no) void spi_master_init(uint8 spi_no, unsigned cpol, unsigned cpha, unsigned databits, uint32_t clock)
{ {
uint32 regvalue; uint32 regvalue;
if(spi_no>1) return; //handle invalid input number if(spi_no>1) return; //handle invalid input number
if(spi_no==SPI){ if(spi_no==SPI){
WRITE_PERI_REG(PERIPHS_IO_MUX, 0x005); WRITE_PERI_REG(PERIPHS_IO_MUX, 0x005);
...@@ -86,13 +85,33 @@ void spi_master_init(uint8 spi_no) ...@@ -86,13 +85,33 @@ void spi_master_init(uint8 spi_no)
PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDO_U, 2);//configure io to spi mode PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDO_U, 2);//configure io to spi mode
} }
SET_PERI_REG_MASK(SPI_USER(spi_no), SPI_CS_SETUP|SPI_CS_HOLD|SPI_USR_COMMAND|SPI_USR_MOSI); SET_PERI_REG_MASK(SPI_USER(spi_no), SPI_CS_SETUP|SPI_CS_HOLD|SPI_DOUTDIN|SPI_USR_MOSI);
CLEAR_PERI_REG_MASK(SPI_USER(spi_no), SPI_FLASH_MODE);
//set clock polarity
// TODO: This doesn't work
//if (cpol == 1) {
// SET_PERI_REG_MASK(SPI_CTRL2(spi_no), (SPI_CK_OUT_HIGH_MODE<<SPI_CK_OUT_HIGH_MODE_S));
//} else {
// SET_PERI_REG_MASK(SPI_CTRL2(spi_no), (SPI_CK_OUT_LOW_MODE<<SPI_CK_OUT_LOW_MODE_S));
//}
//os_printf("SPI_CTRL2 is %08x\n",READ_PERI_REG(SPI_CTRL2(spi_no)));
//set clock phase
if (cpha == 1) {
SET_PERI_REG_MASK(SPI_USER(spi_no), SPI_CK_OUT_EDGE|SPI_CK_I_EDGE);
} else {
CLEAR_PERI_REG_MASK(SPI_USER(spi_no), SPI_CK_OUT_EDGE|SPI_CK_I_EDGE);
}
CLEAR_PERI_REG_MASK(SPI_USER(HSPI), SPI_FLASH_MODE|SPI_WR_BYTE_ORDER|SPI_USR_MISO|
SPI_RD_BYTE_ORDER|SPI_USR_ADDR|SPI_USR_COMMAND|SPI_USR_DUMMY);
//clear Daul or Quad lines transmission mode //clear Daul or Quad lines transmission mode
CLEAR_PERI_REG_MASK(SPI_CTRL(spi_no), SPI_QIO_MODE|SPI_DIO_MODE|SPI_DOUT_MODE|SPI_QOUT_MODE); CLEAR_PERI_REG_MASK(SPI_CTRL(spi_no), SPI_QIO_MODE|SPI_DIO_MODE|SPI_DOUT_MODE|SPI_QOUT_MODE);
// SPI clock=CPU clock/8
WRITE_PERI_REG(SPI_CLOCK(spi_no), WRITE_PERI_REG(SPI_CLOCK(spi_no),
((1&SPI_CLKDIV_PRE)<<SPI_CLKDIV_PRE_S)|
((3&SPI_CLKCNT_N)<<SPI_CLKCNT_N_S)| ((3&SPI_CLKCNT_N)<<SPI_CLKCNT_N_S)|
((1&SPI_CLKCNT_H)<<SPI_CLKCNT_H_S)| ((1&SPI_CLKCNT_H)<<SPI_CLKCNT_H_S)|
((3&SPI_CLKCNT_L)<<SPI_CLKCNT_L_S)); //clear bit 31,set SPI clock div ((3&SPI_CLKCNT_L)<<SPI_CLKCNT_L_S)); //clear bit 31,set SPI clock div
...@@ -109,21 +128,19 @@ void spi_master_init(uint8 spi_no) ...@@ -109,21 +128,19 @@ void spi_master_init(uint8 spi_no)
* Parameters : uint8 spi_no - SPI module number, Only "SPI" and "HSPI" are valid * Parameters : uint8 spi_no - SPI module number, Only "SPI" and "HSPI" are valid
* uint8 data- transmitted data * uint8 data- transmitted data
*******************************************************************************/ *******************************************************************************/
void spi_mast_byte_write(uint8 spi_no,uint8 data) void spi_mast_byte_write(uint8 spi_no, uint8 *data)
{ {
uint32 regvalue; if(spi_no>1) return; //handle invalid input number
if(spi_no>1) return; //handle invalid input number while(READ_PERI_REG(SPI_CMD(spi_no))&SPI_USR);
while(READ_PERI_REG(SPI_CMD(spi_no))&SPI_USR); WRITE_PERI_REG(SPI_W0(HSPI), *data);
CLEAR_PERI_REG_MASK(SPI_USER(spi_no), SPI_USR_MOSI|SPI_USR_MISO);
//SPI_FLASH_USER2 bit28-31 is cmd length,cmd bit length is value(0-15)+1, SET_PERI_REG_MASK(SPI_CMD(spi_no), SPI_USR);
// bit15-0 is cmd value. while(READ_PERI_REG(SPI_CMD(spi_no))&SPI_USR);
WRITE_PERI_REG(SPI_USER2(spi_no),
((7&SPI_USR_COMMAND_BITLEN)<<SPI_USR_COMMAND_BITLEN_S)|((uint32)data)); *data = (uint8)(READ_PERI_REG(SPI_W0(spi_no))&0xff);
SET_PERI_REG_MASK(SPI_CMD(spi_no), SPI_USR); }
}
/****************************************************************************** /******************************************************************************
* FunctionName : spi_byte_write_espslave * FunctionName : spi_byte_write_espslave
......
...@@ -63,6 +63,11 @@ void i2c_master_setAck(uint8 level); ...@@ -63,6 +63,11 @@ void i2c_master_setAck(uint8 level);
uint8 i2c_master_getAck(void); uint8 i2c_master_getAck(void);
uint8 i2c_master_readByte(void); uint8 i2c_master_readByte(void);
void i2c_master_writeByte(uint8 wrdata); void i2c_master_writeByte(uint8 wrdata);
bool i2c_master_checkAck(void);
void i2c_master_send_ack(void);
void i2c_master_send_nack(void);
uint8 i2c_master_get_pinSDA(); uint8 i2c_master_get_pinSDA();
uint8 i2c_master_get_pinSCL(); uint8 i2c_master_get_pinSCL();
......
...@@ -18,9 +18,9 @@ void spi_lcd_mode_init(uint8 spi_no); ...@@ -18,9 +18,9 @@ void spi_lcd_mode_init(uint8 spi_no);
void spi_lcd_9bit_write(uint8 spi_no,uint8 high_bit,uint8 low_8bit); void spi_lcd_9bit_write(uint8 spi_no,uint8 high_bit,uint8 low_8bit);
//spi master init funtion //spi master init funtion
void spi_master_init(uint8 spi_no); void spi_master_init(uint8 spi_no, unsigned cpol, unsigned cpha, unsigned databits, uint32_t clock);
//use spi send 8bit data //use spi send 8bit data
void spi_mast_byte_write(uint8 spi_no,uint8 data); void spi_mast_byte_write(uint8 spi_no,uint8 *data);
//transmit data to esp8266 slave buffer,which needs 16bit transmission , //transmit data to esp8266 slave buffer,which needs 16bit transmission ,
//first byte is master command 0x04, second byte is master data //first byte is master command 0x04, second byte is master data
......
...@@ -39,6 +39,11 @@ ...@@ -39,6 +39,11 @@
#define SPI_MISO_DELAY_NUM_S 18 #define SPI_MISO_DELAY_NUM_S 18
#define SPI_MISO_DELAY_MODE 0x00000003 #define SPI_MISO_DELAY_MODE 0x00000003
#define SPI_MISO_DELAY_MODE_S 16 #define SPI_MISO_DELAY_MODE_S 16
#define SPI_CK_OUT_HIGH_MODE 0x0000000F
#define SPI_CK_OUT_HIGH_MODE_S 12
#define SPI_CK_OUT_LOW_MODE 0x0000000F
#define SPI_CK_OUT_LOW_MODE_S 8
#define SPI_CLOCK(i) (REG_SPI_BASE(i) + 0x18) #define SPI_CLOCK(i) (REG_SPI_BASE(i) + 0x18)
#define SPI_CLK_EQU_SYSCLK (BIT(31)) #define SPI_CLK_EQU_SYSCLK (BIT(31))
#define SPI_CLKDIV_PRE 0x00001FFF #define SPI_CLKDIV_PRE 0x00001FFF
...@@ -73,6 +78,7 @@ ...@@ -73,6 +78,7 @@
#define SPI_CS_SETUP (BIT(5)) #define SPI_CS_SETUP (BIT(5))
#define SPI_CS_HOLD (BIT(4)) #define SPI_CS_HOLD (BIT(4))
#define SPI_FLASH_MODE (BIT(2)) #define SPI_FLASH_MODE (BIT(2))
#define SPI_DOUTDIN (BIT(0))
#define SPI_USER1(i) (REG_SPI_BASE(i) + 0x20) #define SPI_USER1(i) (REG_SPI_BASE(i) + 0x20)
#define SPI_USR_ADDR_BITLEN 0x0000003F #define SPI_USR_ADDR_BITLEN 0x0000003F
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
#define NODE_VERSION_INTERNAL 0U #define NODE_VERSION_INTERNAL 0U
#define NODE_VERSION "NodeMCU 0.9.5" #define NODE_VERSION "NodeMCU 0.9.5"
#define BUILD_DATE "build 20150108" #define BUILD_DATE "build 20150127"
// #define FLASH_512K // #define FLASH_512K
// #define FLASH_1M // #define FLASH_1M
...@@ -18,6 +18,9 @@ ...@@ -18,6 +18,9 @@
#define FULL_VERSION_FOR_USER #define FULL_VERSION_FOR_USER
#ifdef DEVELOP_VERSION #ifdef DEVELOP_VERSION
#define USE_OPTIMIZE_PRINTF
#define NODE_DEBUG #define NODE_DEBUG
#endif /* DEVELOP_VERSION */ #endif /* DEVELOP_VERSION */
...@@ -38,7 +41,7 @@ ...@@ -38,7 +41,7 @@
#define ICACHE_STORE_TYPEDEF_ATTR __attribute__((aligned(4),packed)) #define ICACHE_STORE_TYPEDEF_ATTR __attribute__((aligned(4),packed))
#define ICACHE_STORE_ATTR __attribute__((aligned(4))) #define ICACHE_STORE_ATTR __attribute__((aligned(4)))
#define ICACHE_RAM_ATTR __attribute__((section(".iram0.text"))) #define ICACHE_RAM_ATTR __attribute__((section(".iram0.text")))
#define ICACHE_RODATA_ATTR __attribute__((section(".rodata2.text"))) // #define ICACHE_RODATA_ATTR __attribute__((section(".rodata2.text")))
#define CLIENT_SSL_ENABLE #define CLIENT_SSL_ENABLE
#define GPIO_INTERRUPT_ENABLE #define GPIO_INTERRUPT_ENABLE
...@@ -55,14 +58,19 @@ ...@@ -55,14 +58,19 @@
#define LUA_USE_MODULES_NET #define LUA_USE_MODULES_NET
#define LUA_USE_MODULES_PWM #define LUA_USE_MODULES_PWM
#define LUA_USE_MODULES_I2C #define LUA_USE_MODULES_I2C
#define LUA_USE_MODULES_SPI
#define LUA_USE_MODULES_TMR #define LUA_USE_MODULES_TMR
#define LUA_USE_MODULES_ADC #define LUA_USE_MODULES_ADC
#define LUA_USE_MODULES_UART #define LUA_USE_MODULES_UART
#define LUA_USE_MODULES_OW #define LUA_USE_MODULES_OW
#define LUA_USE_MODULES_BIT #define LUA_USE_MODULES_BIT
#define LUA_USE_MODULES_MQTT
#endif /* LUA_USE_MODULES */ #endif /* LUA_USE_MODULES */
#define LUA_NUMBER_INTEGRAL // #define LUA_NUMBER_INTEGRAL
#ifndef LUA_NUMBER_INTEGRAL
#define PRINTF_LONG_SUPPORT
#endif
#define LUA_OPTRAM #define LUA_OPTRAM
#ifdef LUA_OPTRAM #ifdef LUA_OPTRAM
......
#include "c_math.h" #include "c_math.h"
#include "c_types.h" #include "c_types.h"
double floor(double x)
{
return (double) (x < 0.f ? (((int) x) - 1) : ((int) x));
}
double pow(double x, double y)
{
#define MAXEXP 2031 /* (MAX_EXP * 16) - 1 */
#define MINEXP -2047 /* (MIN_EXP * 16) - 1 */
#define HUGE MAXFLOAT
double a1[] =
{
1.0,
0.95760328069857365,
0.91700404320467123,
0.87812608018664974,
0.84089641525371454,
0.80524516597462716,
0.77110541270397041,
0.73841307296974966,
0.70710678118654752,
0.67712777346844637,
0.64841977732550483,
0.62092890603674203,
0.59460355750136054,
0.56939431737834583,
0.54525386633262883,
0.52213689121370692,
0.50000000000000000
};
double a2[] =
{
0.24114209503420288E-17,
0.92291566937243079E-18,
-0.15241915231122319E-17,
-0.35421849765286817E-17,
-0.31286215245415074E-17,
-0.44654376565694490E-17,
0.29306999570789681E-17,
0.11260851040933474E-17
};
double p1 = 0.833333333333332114e-1;
double p2 = 0.125000000005037992e-1;
double p3 = 0.223214212859242590e-2;
double p4 = 0.434457756721631196e-3;
double q1 = 0.693147180559945296e0;
double q2 = 0.240226506959095371e0;
double q3 = 0.555041086640855953e-1;
double q4 = 0.961812905951724170e-2;
double q5 = 0.133335413135857847e-2;
double q6 = 0.154002904409897646e-3;
double q7 = 0.149288526805956082e-4;
double k = 0.442695040888963407;
double frexp(), g, ldexp(), r, u1, u2, v, w, w1, w2, y1, y2, z;
int iw1, m, p;
if (y == 0.0)
return (1.0);
if (x <= 0.0)
{
if (x == 0.0)
{
if (y > 0.0)
return (x);
//cmemsg(FP_POWO, &y);
//return(HUGE);
}
else
{
//cmemsg(FP_POWN, &x);
x = -x;
}
}
g = frexp(x, &m);
p = 0;
if (g <= a1[8])
p = 8;
if (g <= a1[p + 4])
p += 4;
if (g <= a1[p + 2])
p += 2;
p++;
z = ((g - a1[p]) - a2[p / 2]) / (g + a1[p]);
z += z;
v = z * z;
r = (((p4 * v + p3) * v + p2) * v + p1) * v * z;
r += k * r;
u2 = (r + z * k) + z;
u1 = 0.0625 * (double)(16 * m - p);
y1 = 0.0625 * (double)((int)(16.0 * y));
y2 = y - y1;
w = u2 * y + u1 * y2;
w1 = 0.0625 * (double)((int)(16.0 * w));
w2 = w - w1;
w = w1 + u1 * y1;
w1 = 0.0625 * (double)((int)(16.0 * w));
w2 += (w - w1);
w = 0.0625 * (double)((int)(16.0 * w2));
iw1 = 16.0 * (w1 + w);
w2 -= w;
while (w2 > 0.0)
{
iw1++;
w2 -= 0.0625;
}
if (iw1 > MAXEXP)
{
//cmemsg(FP_POWO, &y);
return (HUGE);
}
if (iw1 < MINEXP)
{
//cmemsg(FP_POWU, &y);
return (0.0);
}
m = iw1 / 16;
if (iw1 >= 0)
m++;
p = 16 * m - iw1;
z = ((((((q7 * w2 + q6) * w2 + q5) * w2 + q4) * w2 + q3) * w2 + q2) * w2 + q1) * w2;
z = a1[p] + a1[p] * z;
return (ldexp(z, m));
}
#if 0 #if 0
#ifndef __math_68881 #ifndef __math_68881
double atan(double x){ double atan(double x)
return x; {
return x;
} }
double cos(double x){ double cos(double x)
return x; {
return x;
} }
double sin(double x){ double sin(double x)
return x; {
return x;
} }
double tan(double x){ double tan(double x)
return x; {
return x;
} }
double tanh(double x){ double tanh(double x)
return x; {
return x;
} }
double frexp(double x, int *y){ double frexp(double x, int *y)
return x; {
return x;
} }
double modf(double x, double *y){ double modf(double x, double *y)
return x; {
return x;
} }
double ceil(double x){ double ceil(double x)
return x; {
return x;
} }
double fabs(double x){ double fabs(double x)
return x; {
return x;
} }
double floor(double x){ double floor(double x)
return x; {
return x;
} }
#endif /* ! defined (__math_68881) */ #endif /* ! defined (__math_68881) */
...@@ -39,41 +174,53 @@ double floor(double x){ ...@@ -39,41 +174,53 @@ double floor(double x){
#ifndef _REENT_ONLY #ifndef _REENT_ONLY
#ifndef __math_68881 #ifndef __math_68881
double acos(double x){ double acos(double x)
return x; {
return x;
} }
double asin(double x){ double asin(double x)
return x; {
return x;
} }
double atan2(double x, double y){ double atan2(double x, double y)
return x; {
return x;
} }
double cosh(double x){ double cosh(double x)
return x; {
return x;
} }
double sinh(double x){ double sinh(double x)
return x; {
return x;
} }
double exp(double x){ double exp(double x)
return x; {
return x;
} }
double ldexp(double x, int y){ double ldexp(double x, int y)
return x; {
return x;
} }
double log(double x){ double log(double x)
return x; {
return x;
} }
double log10(double x){ double log10(double x)
return x; {
return x;
} }
double pow(double x, double y){ double pow(double x, double y)
return x; {
return x;
} }
double sqrt(double x){ double sqrt(double x)
return x; {
return x;
} }
double fmod(double x, double y){ double fmod(double x, double y)
return x; {
return x;
} }
#endif /* ! defined (__math_68881) */ #endif /* ! defined (__math_68881) */
#endif /* ! defined (_REENT_ONLY) */ #endif /* ! defined (_REENT_ONLY) */
......
#ifndef _C_MATH_H_ #ifndef _C_MATH_H_
#define _C_MATH_H_ #define _C_MATH_H_
#include <math.h> #include <math.h>
double floor(double);
double pow(double, double);
#if 0 #if 0
#ifndef HUGE_VAL #ifndef HUGE_VAL
#define HUGE_VAL (1.0e99) #define HUGE_VAL (1.0e99)
......
...@@ -12,44 +12,469 @@ int c_stderr = 1001; ...@@ -12,44 +12,469 @@ int c_stderr = 1001;
// FILE *c_tmpfile(void){ // FILE *c_tmpfile(void){
// } // }
// int c_putchar(int c){ // int c_putchar(int c){
// } // }
// int c_printf(const char *c, ...){ // int c_printf(const char *c, ...){
// } // }
// int c_sprintf(char *c, const char *s, ...){ // int c_sprintf(char *c, const char *s, ...){
// } // }
// int c_fprintf(FILE *f, const char *s, ...){ // int c_fprintf(FILE *f, const char *s, ...){
// } // }
// int c_fscanf(FILE *f, const char *s, ...){ // int c_fscanf(FILE *f, const char *s, ...){
// } // }
// int c_fclose(FILE *f){ // int c_fclose(FILE *f){
// } // }
// int c_fflush(FILE *f){ // int c_fflush(FILE *f){
// } // }
// int c_setvbuf(FILE *f, char *c, int d, size_t t){ // int c_setvbuf(FILE *f, char *c, int d, size_t t){
// } // }
// void c_clearerr(FILE *f){ // void c_clearerr(FILE *f){
// } // }
// int c_fseek(FILE *f, long l, int d){ // int c_fseek(FILE *f, long l, int d){
// } // }
// long c_ftell( FILE *f){ // long c_ftell( FILE *f){
// } // }
// int c_fputs(const char *c, FILE *f){ // int c_fputs(const char *c, FILE *f){
// } // }
// char *c_fgets(char *c, int d, FILE *f){ // char *c_fgets(char *c, int d, FILE *f){
// } // }
// int c_ungetc(int d, FILE *f){ // int c_ungetc(int d, FILE *f){
// } // }
// size_t c_fread(void *p, size_t _size, size_t _n, FILE *f){ // size_t c_fread(void *p, size_t _size, size_t _n, FILE *f){
// } // }
// size_t c_fwrite(const void *p, size_t _size, size_t _n, FILE *f){ // size_t c_fwrite(const void *p, size_t _size, size_t _n, FILE *f){
// } // }
// int c_feof(FILE *f){ // int c_feof(FILE *f){
// } // }
// int c_ferror(FILE *f){ // int c_ferror(FILE *f){
// } // }
// int c_getc(FILE *f){ // int c_getc(FILE *f){
// } // }
#if defined( LUA_NUMBER_INTEGRAL )
#else
/*
File: printf.c
Copyright (c) 2004,2012 Kustaa Nyholm / SpareTimeLabs
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this list
of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or other
materials provided with the distribution.
Neither the name of the Kustaa Nyholm or SpareTimeLabs nor the names of its
contributors may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
OF SUCH DAMAGE.
----------------------------------------------------------------------
This library is realy just two files: 'printf.h' and 'printf.c'.
They provide a simple and small (+200 loc) printf functionality to
be used in embedded systems.
I've found them so usefull in debugging that I do not bother with a
debugger at all.
They are distributed in source form, so to use them, just compile them
into your project.
Two printf variants are provided: printf and sprintf.
The formats supported by this implementation are: 'd' 'u' 'c' 's' 'x' 'X'.
Zero padding and field width are also supported.
If the library is compiled with 'PRINTF_SUPPORT_LONG' defined then the
long specifier is also
supported. Note that this will pull in some long math routines (pun intended!)
and thus make your executable noticably longer.
The memory foot print of course depends on the target cpu, compiler and
compiler options, but a rough guestimate (based on a H8S target) is about
1.4 kB for code and some twenty 'int's and 'char's, say 60 bytes of stack space.
Not too bad. Your milage may vary. By hacking the source code you can
get rid of some hunred bytes, I'm sure, but personally I feel the balance of
functionality and flexibility versus code size is close to optimal for
many embedded systems.
To use the printf you need to supply your own character output function,
something like :
void putc ( void* p, char c)
{
while (!SERIAL_PORT_EMPTY) ;
SERIAL_PORT_TX_REGISTER = c;
}
Before you can call printf you need to initialize it to use your
character output function with something like:
init_printf(NULL,putc);
Notice the 'NULL' in 'init_printf' and the parameter 'void* p' in 'putc',
the NULL (or any pointer) you pass into the 'init_printf' will eventually be
passed to your 'putc' routine. This allows you to pass some storage space (or
anything realy) to the character output function, if necessary.
This is not often needed but it was implemented like that because it made
implementing the sprintf function so neat (look at the source code).
The code is re-entrant, except for the 'init_printf' function, so it
is safe to call it from interupts too, although this may result in mixed output.
If you rely on re-entrancy, take care that your 'putc' function is re-entrant!
The printf and sprintf functions are actually macros that translate to
'tfp_printf' and 'tfp_sprintf'. This makes it possible
to use them along with 'stdio.h' printf's in a single source file.
You just need to undef the names before you include the 'stdio.h'.
Note that these are not function like macros, so if you have variables
or struct members with these names, things will explode in your face.
Without variadic macros this is the best we can do to wrap these
fucnction. If it is a problem just give up the macros and use the
functions directly or rename them.
For further details see source code.
regs Kusti, 23.10.2004
*/
/*
Add lightweight %g support by vowstar, <vowstar@gmail.com>
NodeMCU Team, 26.1.2015
*/
typedef void (*putcf) (void *, char);
#ifdef PRINTF_LONG_SUPPORT
static int uli2a(unsigned long int num, unsigned int base, int uc, char *bf)
{
int n = 0;
unsigned long int d = 1;
int len = 1;
while (num / d >= base)
{
d *= base;
len ++;
}
while (d != 0)
{
int dgt = num / d;
num %= d;
d /= base;
if (n || dgt > 0 || d == 0)
{
*bf++ = dgt + (dgt < 10 ? '0' : (uc ? 'A' : 'a') - 10);
++n;
}
}
*bf = 0;
return len;
}
static int li2a (long num, char *bf)
{
int len = 0;
if (num < 0)
{
num = -num;
*bf++ = '-';
len ++;
}
len += uli2a(num, 10, 0, bf);
return len;
}
#endif
static int ui2a(unsigned int num, unsigned int base, int uc, char *bf)
{
int n = 0;
unsigned int d = 1;
int len = 1;
while (num / d >= base)
{
d *= base;
len ++;
}
while (d != 0)
{
int dgt = num / d;
num %= d;
d /= base;
if (n || dgt > 0 || d == 0)
{
*bf++ = dgt + (dgt < 10 ? '0' : (uc ? 'A' : 'a') - 10);
++n;
}
}
*bf = 0;
return len;
}
static int i2a (int num, char *bf)
{
int len = 0;
if (num < 0)
{
num = -num;
*bf++ = '-';
len ++;
}
len += ui2a(num, 10, 0, bf);
return len;
}
// Converts a floating point number to string.
static int d2a(double num, char *bf)
{
int len = 0;
double ipart = 0;
double fpart = 0;
double absnum = num;
// Add sign
if (absnum < 0)
{
absnum = -absnum;
*bf++ = '-';
// len must add 1 when return
// but can't add at here
}
// Extract integer part
ipart = (int)absnum;
// Extract floating part
fpart = absnum - (double)ipart;
// convert integer part to string
#ifdef PRINTF_LONG_SUPPORT
len += li2a(ipart, bf);
#else
len += i2a(ipart, bf);
#endif
#ifndef EPSILON
#define EPSILON ((double)(0.00000001))
#endif
if (fpart < EPSILON)
{
// fpart is zero
}
else
{
bf += len;
// add dot
*bf ++ = '.';
len += 1;
// add zero after dot
while (fpart < 0.1)
{
fpart *= 10;
*bf ++ = '0';
len += 1;
}
while ((fpart < (double)1.0 / EPSILON) && ((fpart - (int)fpart) > EPSILON))
{
fpart = fpart * 10;
}
#ifdef PRINTF_LONG_SUPPORT
len += li2a((int)fpart, bf);
#else
len += i2a((int)fpart, bf);
#endif
}
#undef EPSILON
if (num < 0)
{
len ++;
}
return len;
}
static int a2d(char ch)
{
if (ch >= '0' && ch <= '9')
return ch - '0';
else if (ch >= 'a' && ch <= 'f')
return ch - 'a' + 10;
else if (ch >= 'A' && ch <= 'F')
return ch - 'A' + 10;
else return -1;
}
static char a2i(char ch, char **src, int base, int *nump)
{
char *p = *src;
int num = 0;
int digit;
while ((digit = a2d(ch)) >= 0)
{
if (digit > base) break;
num = num * base + digit;
ch = *p++;
}
*src = p;
*nump = num;
return ch;
}
static void putchw(void *putp, putcf putf, int n, char z, char *bf)
{
char fc = z ? '0' : ' ';
char ch;
char *p = bf;
while (*p++ && n > 0)
n--;
while (n-- > 0)
putf(putp, fc);
while ((ch = *bf++))
putf(putp, ch);
}
void c_format(void *putp, putcf putf, char *fmt, va_list va)
{
char bf[12];
char ch;
while ((ch = *(fmt++)))
{
if (ch != '%')
putf(putp, ch);
else
{
char lz = 0;
#ifdef PRINTF_LONG_SUPPORT
char lng = 0;
#endif
int w = 0;
ch = *(fmt++);
if (ch == '0')
{
ch = *(fmt++);
lz = 1;
}
if (ch >= '0' && ch <= '9')
{
ch = a2i(ch, &fmt, 10, &w);
}
#ifdef PRINTF_LONG_SUPPORT
if (ch == 'l')
{
ch = *(fmt++);
lng = 1;
}
#endif
switch (ch)
{
case 0:
goto abort;
case 'u' :
{
#ifdef PRINTF_LONG_SUPPORT
if (lng)
uli2a(va_arg(va, unsigned long int), 10, 0, bf);
else
#endif
ui2a(va_arg(va, unsigned int), 10, 0, bf);
putchw(putp, putf, w, lz, bf);
break;
}
case 'o' :
{
#ifdef PRINTF_LONG_SUPPORT
if (lng)
uli2a(va_arg(va, unsigned long int), 8, 0, bf);
else
#endif
ui2a(va_arg(va, unsigned int), 8, 0, bf);
putchw(putp, putf, w, lz, bf);
break;
}
case 'd' : case 'i':
{
#ifdef PRINTF_LONG_SUPPORT
if (lng)
li2a(va_arg(va, unsigned long int), bf);
else
#endif
i2a(va_arg(va, int), bf);
putchw(putp, putf, w, lz, bf);
break;
}
case 'x': case 'X' :
#ifdef PRINTF_LONG_SUPPORT
if (lng)
uli2a(va_arg(va, unsigned long int), 16, (ch == 'X'), bf);
else
#endif
ui2a(va_arg(va, unsigned int), 16, (ch == 'X'), bf);
putchw(putp, putf, w, lz, bf);
break;
case 'e': case 'E': case 'f':
case 'g': case 'G':
{
d2a(va_arg(va, double), bf);
putchw(putp, putf, w, lz, bf);
break;
}
case 'c' :
putf(putp, (char)(va_arg(va, int)));
break;
case 's' :
putchw(putp, putf, w, 0, va_arg(va, char *));
break;
case '%' :
putf(putp, ch);
default:
break;
}
}
}
abort:;
}
static void putcp(void *p, char c)
{
*(*((char **)p))++ = c;
}
void c_sprintf(char *s, char *fmt, ...)
{
va_list va;
va_start(va, fmt);
c_format(&s, putcp, fmt, va);
putcp(&s, 0);
va_end(va);
}
#endif
...@@ -47,12 +47,22 @@ extern int c_stderr; ...@@ -47,12 +47,22 @@ extern int c_stderr;
#define SEEK_END 2 /* set file offset to EOF plus offset */ #define SEEK_END 2 /* set file offset to EOF plus offset */
#endif #endif
#define c_malloc os_malloc
#define c_zalloc os_zalloc
#define c_free os_free
extern void output_redirect(const char *str); extern void output_redirect(const char *str);
#define c_puts output_redirect #define c_puts output_redirect
// #define c_printf os_printf // #define c_printf os_printf
// int c_printf(const char *c, ...); // int c_printf(const char *c, ...);
#if defined( LUA_NUMBER_INTEGRAL )
#define c_sprintf os_sprintf #define c_sprintf os_sprintf
#else
#include "c_stdarg.h"
void c_sprintf(char* s,char *fmt, ...);
#endif
// #define c_vsprintf ets_vsprintf // #define c_vsprintf ets_vsprintf
#define c_printf(...) do { \ #define c_printf(...) do { \
unsigned char __print_buf[BUFSIZ]; \ unsigned char __print_buf[BUFSIZ]; \
......
...@@ -20,27 +20,27 @@ const char *c_getenv(const char *__string){ ...@@ -20,27 +20,27 @@ const char *c_getenv(const char *__string){
} }
// make sure there is enough memory before real malloc, otherwise malloc will panic and reset // make sure there is enough memory before real malloc, otherwise malloc will panic and reset
void *c_malloc(size_t __size){ // void *c_malloc(size_t __size){
if(__size>system_get_free_heap_size()){ // if(__size>system_get_free_heap_size()){
NODE_ERR("malloc: not enough memory\n"); // NODE_ERR("malloc: not enough memory\n");
return NULL; // return NULL;
} // }
return (void *)os_malloc(__size); // return (void *)os_malloc(__size);
} // }
void *c_zalloc(size_t __size){ // void *c_zalloc(size_t __size){
if(__size>system_get_free_heap_size()){ // if(__size>system_get_free_heap_size()){
NODE_ERR("zalloc: not enough memory\n"); // NODE_ERR("zalloc: not enough memory\n");
return NULL; // return NULL;
} // }
return (void *)os_zalloc(__size); // return (void *)os_zalloc(__size);
} // }
void c_free(void *p){ // void c_free(void *p){
// NODE_ERR("free1: %d\n", system_get_free_heap_size()); // // NODE_ERR("free1: %d\n", system_get_free_heap_size());
os_free(p); // os_free(p);
// NODE_ERR("-free1: %d\n", system_get_free_heap_size()); // // NODE_ERR("-free1: %d\n", system_get_free_heap_size());
} // }
// int c_rand(void){ // int c_rand(void){
...@@ -50,8 +50,207 @@ void c_free(void *p){ ...@@ -50,8 +50,207 @@ void c_free(void *p){
// int c_atoi(const char *__nptr){ // int c_atoi(const char *__nptr){
// } // }
// double c_strtod(const char *__n, char **__end_PTR){ #include <_ansi.h>
// } //#include <reent.h>
#include <string.h>
//#include "mprec.h"
double c_strtod(const char *string, char **endPtr)
{
int maxExponent = 511; /* Largest possible base 10 exponent. Any
* exponent larger than this will already
* produce underflow or overflow, so there's
* no need to worry about additional digits.
*/
double powersOf10[] = { /* Table giving binary powers of 10. Entry */
10., /* is 10^2^i. Used to convert decimal */
100., /* exponents into floating-point numbers. */
1.0e4,
1.0e8,
1.0e16,
1.0e32,
1.0e64,
1.0e128,
1.0e256
};
int sign, expSign = FALSE;
double fraction, dblExp, *d;
register const char *p;
register int c;
int exp = 0; /* Exponent read from "EX" field. */
int fracExp = 0; /* Exponent that derives from the fractional
* part. Under normal circumstatnces, it is
* the negative of the number of digits in F.
* However, if I is very long, the last digits
* of I get dropped (otherwise a long I with a
* large negative exponent could cause an
* unnecessary overflow on I alone). In this
* case, fracExp is incremented one for each
* dropped digit. */
int mantSize; /* Number of digits in mantissa. */
int decPt; /* Number of mantissa digits BEFORE decimal
* point. */
const char *pExp; /* Temporarily holds location of exponent
* in string. */
/*
* Strip off leading blanks and check for a sign.
*/
p = string;
while (isspace((unsigned char)(*p))) {
p += 1;
}
if (*p == '-') {
sign = TRUE;
p += 1;
} else {
if (*p == '+') {
p += 1;
}
sign = FALSE;
}
/*
* Count the number of digits in the mantissa (including the decimal
* point), and also locate the decimal point.
*/
decPt = -1;
for (mantSize = 0; ; mantSize += 1)
{
c = *p;
if (!isdigit(c)) {
if ((c != '.') || (decPt >= 0)) {
break;
}
decPt = mantSize;
}
p += 1;
}
/*
* Now suck up the digits in the mantissa. Use two integers to
* collect 9 digits each (this is faster than using floating-point).
* If the mantissa has more than 18 digits, ignore the extras, since
* they can't affect the value anyway.
*/
pExp = p;
p -= mantSize;
if (decPt < 0) {
decPt = mantSize;
} else {
mantSize -= 1; /* One of the digits was the point. */
}
if (mantSize > 18) {
fracExp = decPt - 18;
mantSize = 18;
} else {
fracExp = decPt - mantSize;
}
if (mantSize == 0) {
fraction = 0.0;
p = string;
goto done;
} else {
int frac1, frac2;
frac1 = 0;
for ( ; mantSize > 9; mantSize -= 1)
{
c = *p;
p += 1;
if (c == '.') {
c = *p;
p += 1;
}
frac1 = 10*frac1 + (c - '0');
}
frac2 = 0;
for (; mantSize > 0; mantSize -= 1)
{
c = *p;
p += 1;
if (c == '.') {
c = *p;
p += 1;
}
frac2 = 10*frac2 + (c - '0');
}
fraction = (1.0e9 * frac1) + frac2;
}
/*
* Skim off the exponent.
*/
p = pExp;
if ((*p == 'E') || (*p == 'e')) {
p += 1;
if (*p == '-') {
expSign = TRUE;
p += 1;
} else {
if (*p == '+') {
p += 1;
}
expSign = FALSE;
}
if (!isdigit((unsigned char)(*p))) {
p = pExp;
goto done;
}
while (isdigit((unsigned char)(*p))) {
exp = exp * 10 + (*p - '0');
p += 1;
}
}
if (expSign) {
exp = fracExp - exp;
} else {
exp = fracExp + exp;
}
/*
* Generate a floating-point number that represents the exponent.
* Do this by processing the exponent one bit at a time to combine
* many powers of 2 of 10. Then combine the exponent with the
* fraction.
*/
if (exp < 0) {
expSign = TRUE;
exp = -exp;
} else {
expSign = FALSE;
}
if (exp > maxExponent) {
exp = maxExponent;
// errno = ERANGE;
}
dblExp = 1.0;
for (d = powersOf10; exp != 0; exp >>= 1, d += 1) {
if (exp & 01) {
dblExp *= *d;
}
}
if (expSign) {
fraction /= dblExp;
} else {
fraction *= dblExp;
}
done:
if (endPtr != NULL) {
*endPtr = (char *) p;
}
if (sign) {
return -fraction;
}
return fraction;
}
// long c_strtol(const char *__n, char **__end_PTR, int __base){ // long c_strtol(const char *__n, char **__end_PTR, int __base){
// } // }
// unsigned long c_strtoul(const char *__n, char **__end_PTR, int __base){ // unsigned long c_strtoul(const char *__n, char **__end_PTR, int __base){
......
...@@ -36,7 +36,7 @@ ...@@ -36,7 +36,7 @@
#define c_abs abs #define c_abs abs
#define c_atoi atoi #define c_atoi atoi
// #define c_strtod strtod //#define c_strtod strtod
#define c_strtol strtol #define c_strtol strtol
#define c_strtoul strtoul #define c_strtoul strtoul
...@@ -55,7 +55,7 @@ void c_free(void *); ...@@ -55,7 +55,7 @@ void c_free(void *);
// void c_srand(unsigned int __seed); // void c_srand(unsigned int __seed);
// int c_atoi(const char *__nptr); // int c_atoi(const char *__nptr);
// double c_strtod(const char *__n, char **__end_PTR); double c_strtod(const char *__n, char **__end_PTR);
// // long c_strtol(const char *__n, char **__end_PTR, int __base); // // long c_strtol(const char *__n, char **__end_PTR, int __base);
// unsigned long c_strtoul(const char *__n, char **__end_PTR, int __base); // unsigned long c_strtoul(const char *__n, char **__end_PTR, int __base);
// // long long c_strtoll(const char *__n, char **__end_PTR, int __base); // // long long c_strtoll(const char *__n, char **__end_PTR, int __base);
......
...@@ -601,7 +601,8 @@ extern int readline4lua(const char *prompt, char *buffer, int length); ...@@ -601,7 +601,8 @@ extern int readline4lua(const char *prompt, char *buffer, int length);
#endif // #if !defined LUA_INTEGRAL_LONGLONG #endif // #if !defined LUA_INTEGRAL_LONGLONG
#else #else
#define LUA_NUMBER_SCAN "%lf" #define LUA_NUMBER_SCAN "%lf"
#define LUA_NUMBER_FMT "%.14g" //#define LUA_NUMBER_FMT "%.14g"
#define LUA_NUMBER_FMT "%g"
#endif // #if defined LUA_NUMBER_INTEGRAL #endif // #if defined LUA_NUMBER_INTEGRAL
#define lua_number2str(s,n) c_sprintf((s), LUA_NUMBER_FMT, (n)) #define lua_number2str(s,n) c_sprintf((s), LUA_NUMBER_FMT, (n))
#define LUAI_MAXNUMBER2STR 32 /* 16 digits, sign, point, and \0 */ #define LUAI_MAXNUMBER2STR 32 /* 16 digits, sign, point, and \0 */
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
*/ */
#include "c_string.h" #include "c_string.h"
#include "c_types.h"
#define lundump_c #define lundump_c
#define LUA_CORE #define LUA_CORE
...@@ -141,13 +142,11 @@ static lua_Number LoadNumber(LoadState* S) ...@@ -141,13 +142,11 @@ static lua_Number LoadNumber(LoadState* S)
LoadVar(S,y); LoadVar(S,y);
x = (lua_Number)y; x = (lua_Number)y;
} break; } break;
#if 0
case 8: { case 8: {
int64_t y; int64_t y;
LoadVar(S,y); LoadVar(S,y);
x = (lua_Number)y; x = (lua_Number)y;
} break; } break;
#endif
default: lua_assert(0); default: lua_assert(0);
} }
} }
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include "c_stdio.h" #include "c_stdio.h"
#include "c_stdlib.h" #include "c_stdlib.h"
#include "c_string.h" #include "c_string.h"
#include "c_math.h"
#define lvm_c #define lvm_c
#define LUA_CORE #define LUA_CORE
......
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