diff --git a/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/README.md b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/README.md new file mode 100644 index 00000000..fe6618d3 --- /dev/null +++ b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/README.md @@ -0,0 +1,144 @@ +# Intelligent Home Hosting System Based on ARC EM Processor + +## Introduction + +Based on the ARC EM Starter Kit development board, this design completes a smart home hosting system that implements the following features: + +- Aquarium automatic timing and remote control feeding, automatic water circulation +- Plant growth environment monitoring, automatic watering to adjust soil moisture +- Indoor living environment monitoring, including temperature and humidity detection, sensitive gas detection, PM2.5 detection, adaptive ventilation, etc. + +All data can be uploaded to the IoT cloud platform via WiFi, support remote viewing and control of mobile APP, and also support close-range voice control. + +![Overall picture](https://github.com/BarryWang2017/2018_ARC_competition/blob/master/Overall_picture.jpg) + +[Show video](http://v.youku.com/v_show/id_XMzYzMTYyNzY4MA==.html?spm=a2h0k.11417342.searchresults.dtitle) + +## Hardware and Software Setup + +### Hardware requirements + +| Item | Quantity | Directions | +| :--------------------------------------: | :------: | :--------------------------------------- | +| [ARC EM Starter Kit(EMSK)](www.synopsys.com/dw/ipdir.php?ds=arc_em_starter_kit) | 3 | Used as the control center for three subsystems | +| [ESP8266 ESP01-S WiFi Module](www.ai-thinker.com) | 3 | Each of the three development boards is connected to the IoT cloud platform through the WiFi module | +| WiFi Router | 1 | WiFi module connects to the Internet via the WiFi router with Internet access | +| [DHT11 Temperature and humidity sensor](https://detail.tmall.com/item.htm?spm=a230r.1.14.9.7aa72013MKTmPK&id=41248630584&cm_id=140105335569ed55e27b&abbucket=17) | 2 | Used to detect the temperature and humidity of the indoor environment and the plant growth environment | +| [DS18B20 Temperature Sensor](https://detail.tmall.com/item.htm?spm=a230r.1.14.9.6c71108eERVetZ&id=41251333448&cm_id=140105335569ed55e27b&abbucket=17) | 1 | Used to detect water temperature in the aquarium | +| [MQ-2 sensitive gas detection module](https://detail.tmall.com/item.htm?spm=a1z10.3-b-s.w4011-16538328900.46.6e1c3d6cdb1qlO&id=41265308241&rn=1e61c43016672601973742e420a01586&abbucket=11) | 1 | Used to detect flammable gases and fumes in indoor environments | +| [GP2Y1051AU0F Dust sensor](https://item.taobao.com/item.htm?spm=a1z09.2.0.0.52ff2e8dhF0kKJ&id=44400981264&_u=ovrnal8ae36) | 1 | Used to detect dust and PM2.5 concentrations in indoor environments | +| Buzzer module | 1 | For hazard warning | +| [Relay module](https://detail.tmall.com/item.htm?id=543199943481&spm=a1z09.2.0.0.52ff2e8dhF0kKJ&_u=ovrnal85d05) | 1 | Used to control the fluorescent switch | +| [Adjustable voltage boosting module](https://item.taobao.com/item.htm?spm=a1z09.2.0.0.52ff2e8dhF0kKJ&id=522572949297&_u=ovrnal87651) | 1 | The output voltage of the ARC EM development board is 3.3V, some modules need 5V voltage supply, so use the boost module to output 5V voltage. | +| [Current drive module](https://detail.tmall.com/item.htm?id=536770526104&spm=a1z09.2.0.0.52ff2e8dhF0kKJ&_u=ovrnal8a8e6) | 4 | For driving small pumps and relay modules | +| SD card | 3 | Used to store applications | +| [PMOD AD2](https://store.digilentinc.com/pmod-ad2-4-channel-12-bit-a-d-converter/) | 2 | Used to acquire analog output values for MQ-2 and light sensors | +| [Light intensity sensor](https://detail.tmall.com/item.htm?spm=a1z10.3-b-s.w4011-17145939501.30.48035535tIh32d&id=20885572596&rn=96a3c5ec08d9a9684444ba554871949c&abbucket=17) | 1 | Used to collect light intensity in the environment | +| [5V stepper motor and drive module](https://detail.tmall.com/item.htm?id=38688094766&spm=a1z09.2.0.0.52ff2e8dVPMtMj&_u=ovrnal8c843) | 1 | For driving aquarium feeding devices | +| [5V small pumps](https://item.taobao.com/item.htm?spm=a1z09.2.0.0.52ff2e8dhF0kKJ&id=7184794953&_u=ovrnal8615b) | 2 | Used to water plants and aquarium water circulation | +| [Soil moisture sensor](https://detail.tmall.com/item.htm?id=37365775741&spm=a1z09.2.0.0.52ff2e8dVPMtMj&_u=ovrnal8c3c1) | 1 | Collecting plant soil moisture | +| [XFS5152CE TTS module](https://detail.tmall.com/item.htm?spm=a230r.1.14.17.19797e5cbMQ3sr&id=41363527043&cm_id=140105335569ed55e27b&abbucket=6) | 1 | Used to synthesize and play voice | +| [LD3320 Speech recognition module](https://detail.tmall.com/item.htm?spm=a230r.1.14.17.1fda7ccdpzaHwl&id=41365254224&cm_id=140105335569ed55e27b&abbucket=6) | 1 | Used for speech recognition and providing a VUI | +### Software Requirements +[ARC GNU Toolchain](https://github.com/foss-for-synopsys-dwc-arc-processors/toolchain/releases) + +[embARC Open Software Platform (OSP) ](https://github.com/foss-for-synopsys-dwc-arc-processors/embarc_osp) + +[PUTTY](https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html) + +### Hardware connection +As the aquarium box system control center, EMSK1 is responsible for collecting the water temperature of the aquarium, controlling the stepping motor to automatically feed, controlling the water pump to perform water circulation, uploading sensor data and receiving control signals through WiFi. The hardware connection is shown below. +![水族箱子系统硬件连接图](./doc/screen/Aquarius_subsystem.jpg) + +As the control center of the plant survival maintenance subsystem, EMSK2 is responsible for collecting the soil moisture of the flowerpot, the temperature and humidity in the environment, and watering the pot by controlling the water pump to adjust the soil moisture to make the plant survive in a relatively suitable environment. . These sensor data and control signals are also uploaded via WiFi. The hardware connection is shown below. +![植物生存维持子系统硬件连接图](./doc/screen/Plant_Subsystem.jpg) + +As the control center of the indoor living environment monitoring subsystem, EMSK3 is responsible for collecting indoor temperature and humidity information, detecting flammable sensitive gas, detecting light intensity, detecting PM2.5 content, and controlling indoor light intensity and ignitability by controlling fluorescent lamps and exhaust fans. Gas and the like are adjusted. At the same time, the sensor data is uploaded via WiFi and the corresponding control signals are accepted. The hardware connection diagram is as follows. +![室内环境监控子系统硬件连接图](./doc/screen/Room_Env_Subsystem.jpg) + +As the voice control assistant's control center, EMSK4 is responsible for receiving and recognizing voice commands from users, controlling other sub-devices through WiFi to react, and using voice feedback to control the corresponding control results. The hardware connection diagram is as follows.![室内环境监控子系统硬件连接图](./doc/screen/Voice_Assistant.jpg) + +## User manual + +### Download GAgent firmware into ESP8266 ESP01-S WiFi module + +Please visit the Gizwits website for detailed information.[Gizwits Introduction](http://docs.gizwits.com/en-us/overview/overview.html) [Firmware User manual](http://docs.gizwits.com/zh-cn/deviceDev/ESP8266%E4%B8%B2%E5%8F%A3%E7%83%A7%E5%86%99%E8%AF%B4%E6%98%8E.html) + + + +### Compile and run the application + +#### 0. Preparation + +- Copy the four source code folders under the src folder to `($embARC)/applications/SmartHomeSystem/ `(($embARC) for the embARC package root directory) + + +![](./doc/screen/copy_src.png) + +- Download and install device management software for viewing device status and control devices. Download link: https://download.gizwits.com/zh-cn/p/98/99 + + +#### 1. Compile and run the Aquarium System application +- Go to the Aquarius_subsystem folder and modify the Makefile according to your development board version and processor model (using the EMSK V2.2 development board, em7d processor as an example). + +![](./doc/screen/Aquarius_Makefile_change.png) + +- Open CMD or PowerShell in the Aquarius_subsystem folder and type the **make** command to compile. + +![](./doc/screen/Aquarius_make.png) + +- Connect the host and development board with a USB cable, and run the target program by entering the **make run** command in CMD or PowerShell. +- Open the device management app, bind the device and control it. + +![](./doc/screen/APP_1.gif) + + + +#### 2. Compile and run the Plant Subsystem application +- Go to the Plant_subsystem folder and modify the Makefile according to your development board version and processor model (using the EMSK V2.2 development board, em7d processor as an example). + +![](./doc/screen/Plant_Makefile_Change.png) + +- Open CMD or PowerShell in the Plant_subsystem folder and type the **make** command to compile. + +![](./doc/screen/Plant_make.png) + +- Connect the host and development board with a USB cable, and run the target program by entering the **make run** command in CMD or PowerShell. +- Open the device management app, bind the device and control it. + +![](./doc/screen/APP_2.gif) + +#### 3. Compile and run the Room environment monitoring subsystem application +- Go to the Room_Env_subsystem folder and modify the Makefile according to your development board version and processor model (using the EMSK V2.2 development board, em7d processor as an example). + +![](./doc/screen/Room_Makefile_Change.png) + +- Open CMD or PowerShell in the Room_Env_subsystem folder and type the **make** command to compile. + +![](./doc/screen/Room_make.png) + +- Connect the host and development board with a USB cable, and run the target program by entering the **make run** command in CMD or PowerShell. +- Open the device management app, bind the device and control it. + +![](./doc/screen/APP_3.gif) + +#### 4. Compile and run the Voice Assistant application +- Go to the Voice_Assistant folder and modify the Makefile according to your development board version and processor model (using the EMSK V2.2 development board, em7d processor as an example). + +![](./doc/screen/Voice_Makefile_Change.png) + +- Open CMD or PowerShell in the Voice_Assistant folder and type the **make** command to compile. + +![](./doc/screen/Voice_make.png) + +- Connect the host and development board with a USB cable, and run the target program by entering the **make run** command in CMD or PowerShell. +- Open the device management app, bind the device and control it. + +![](./doc/screen/APP_4.gif) + +For more information, see the following link: + +[Picture of Work details No.1](https://github.com/BarryWang2017/2018_ARC_competition/blob/master/close_up_1.png) +[Picture of Work details No.2](https://github.com/BarryWang2017/2018_ARC_competition/blob/master/close_up_2.png) +[Picture of Work details No.3](https://github.com/BarryWang2017/2018_ARC_competition/blob/master/close_up_3.png) +[Picture of Work details No.4](https://github.com/BarryWang2017/2018_ARC_competition/blob/master/APP_interface.png) diff --git a/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/doc/Technical_report.md b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/doc/Technical_report.md new file mode 100644 index 00000000..b18b8ab1 --- /dev/null +++ b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/doc/Technical_report.md @@ -0,0 +1,3 @@ +Technical report link: https://github.com/BarryWang2017/2018_ARC_competition/blob/master/XDU_Intelligent_Home_Hosting_System.docx + +work presentation link: https://github.com/BarryWang2017/2018_ARC_competition/blob/master/XDU_Intelligent_Home_Hosting_System.pptx \ No newline at end of file diff --git a/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/doc/screen/APP_1.gif b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/doc/screen/APP_1.gif new file mode 100644 index 00000000..dcbe6eac Binary files /dev/null and b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/doc/screen/APP_1.gif differ diff --git a/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/doc/screen/APP_2.gif b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/doc/screen/APP_2.gif new file mode 100644 index 00000000..696b6f41 Binary files /dev/null and b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/doc/screen/APP_2.gif differ diff --git a/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/doc/screen/APP_3.gif b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/doc/screen/APP_3.gif new file mode 100644 index 00000000..c8706ea6 Binary files /dev/null and b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/doc/screen/APP_3.gif differ diff --git a/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/doc/screen/APP_4.gif b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/doc/screen/APP_4.gif new file mode 100644 index 00000000..ee623ea9 Binary files /dev/null and b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/doc/screen/APP_4.gif differ diff --git a/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/doc/screen/Aquarius_Makefile_change.png b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/doc/screen/Aquarius_Makefile_change.png new file mode 100644 index 00000000..5ecfac16 Binary files /dev/null and b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/doc/screen/Aquarius_Makefile_change.png differ diff --git a/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/doc/screen/Aquarius_make.png b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/doc/screen/Aquarius_make.png new file mode 100644 index 00000000..6b31a062 Binary files /dev/null and b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/doc/screen/Aquarius_make.png differ diff --git a/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/doc/screen/Aquarius_subsystem.jpg b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/doc/screen/Aquarius_subsystem.jpg new file mode 100644 index 00000000..04774544 Binary files /dev/null and b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/doc/screen/Aquarius_subsystem.jpg differ diff --git a/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/doc/screen/ESP8266.png b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/doc/screen/ESP8266.png new file mode 100644 index 00000000..2402fbe0 Binary files /dev/null and b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/doc/screen/ESP8266.png differ diff --git a/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/doc/screen/GAgent_Download.png b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/doc/screen/GAgent_Download.png new file mode 100644 index 00000000..a6c790a9 Binary files /dev/null and b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/doc/screen/GAgent_Download.png differ diff --git a/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/doc/screen/GAgent_cfg.png b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/doc/screen/GAgent_cfg.png new file mode 100644 index 00000000..14d82def Binary files /dev/null and b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/doc/screen/GAgent_cfg.png differ diff --git a/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/doc/screen/GAgent_firmware.png b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/doc/screen/GAgent_firmware.png new file mode 100644 index 00000000..8f0c4146 Binary files /dev/null and b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/doc/screen/GAgent_firmware.png differ diff --git a/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/doc/screen/Plant_Makefile_Change.png b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/doc/screen/Plant_Makefile_Change.png new file mode 100644 index 00000000..74d61ac7 Binary files /dev/null and b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/doc/screen/Plant_Makefile_Change.png differ diff --git a/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/doc/screen/Plant_Subsystem.jpg b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/doc/screen/Plant_Subsystem.jpg new file mode 100644 index 00000000..0959acdc Binary files /dev/null and b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/doc/screen/Plant_Subsystem.jpg differ diff --git a/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/doc/screen/Plant_make.png b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/doc/screen/Plant_make.png new file mode 100644 index 00000000..e01a6f79 Binary files /dev/null and b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/doc/screen/Plant_make.png differ diff --git a/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/doc/screen/Room_Env_Subsystem.jpg b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/doc/screen/Room_Env_Subsystem.jpg new file mode 100644 index 00000000..4785045b Binary files /dev/null and b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/doc/screen/Room_Env_Subsystem.jpg differ diff --git a/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/doc/screen/Room_Makefile_Change.png b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/doc/screen/Room_Makefile_Change.png new file mode 100644 index 00000000..f737a384 Binary files /dev/null and b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/doc/screen/Room_Makefile_Change.png differ diff --git a/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/doc/screen/Room_make.png b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/doc/screen/Room_make.png new file mode 100644 index 00000000..273cedf3 Binary files /dev/null and b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/doc/screen/Room_make.png differ diff --git a/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/doc/screen/Voice_Assistant.jpg b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/doc/screen/Voice_Assistant.jpg new file mode 100644 index 00000000..bfcc9db7 Binary files /dev/null and b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/doc/screen/Voice_Assistant.jpg differ diff --git a/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/doc/screen/Voice_Makefile_change.png b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/doc/screen/Voice_Makefile_change.png new file mode 100644 index 00000000..e575e7be Binary files /dev/null and b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/doc/screen/Voice_Makefile_change.png differ diff --git a/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/doc/screen/Voice_make.png b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/doc/screen/Voice_make.png new file mode 100644 index 00000000..5c7976fe Binary files /dev/null and b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/doc/screen/Voice_make.png differ diff --git a/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/doc/screen/copy_src.png b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/doc/screen/copy_src.png new file mode 100644 index 00000000..7565346e Binary files /dev/null and b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/doc/screen/copy_src.png differ diff --git a/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/doc/screen/programming_1.png b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/doc/screen/programming_1.png new file mode 100644 index 00000000..726087d0 Binary files /dev/null and b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/doc/screen/programming_1.png differ diff --git a/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/doc/screen/programming_2.png b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/doc/screen/programming_2.png new file mode 100644 index 00000000..4314e40e Binary files /dev/null and b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/doc/screen/programming_2.png differ diff --git a/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/doc/screen/programming_3.png b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/doc/screen/programming_3.png new file mode 100644 index 00000000..8d9a8168 Binary files /dev/null and b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/doc/screen/programming_3.png differ diff --git a/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/doc/screen/programming_4.png b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/doc/screen/programming_4.png new file mode 100644 index 00000000..f402e51b Binary files /dev/null and b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/doc/screen/programming_4.png differ diff --git a/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/doc/screen/programming_5.png b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/doc/screen/programming_5.png new file mode 100644 index 00000000..54810552 Binary files /dev/null and b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/doc/screen/programming_5.png differ diff --git a/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Aquarius_subsystem/DS18B20.c b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Aquarius_subsystem/DS18B20.c new file mode 100644 index 00000000..28d50ded --- /dev/null +++ b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Aquarius_subsystem/DS18B20.c @@ -0,0 +1,140 @@ +#include "embARC.h" +#include "embARC_debug.h" +#include +#include "dev_gpio.h" + +#include "DS18B20.h" + + + + + +static void delay_us( uint32_t us) +{ + uint32_t volatile x,y; + for(x=2;x>0;x--) + for(y=us;y>0;y--); + +} + +uint8_t DS18b20_init(DEV_GPIO_PTR ds18b20_port,uint8_t ds18b20_bit) +{ + uint32_t read_data; + uint32_t CONT = 0; + uint8_t FLAG = 1; + uint32_t DS18B20_MASK = 0x01 << ds18b20_bit; + uint32_t DS18B20_WRITE_1 = 0x01 << ds18b20_bit; + + ds18b20_port->gpio_open(GPIO_DIR_OUTPUT); + // set io output + ds18b20_port->gpio_control(GPIO_CMD_SET_BIT_DIR_OUTPUT,(void *)(DS18B20_MASK)); + ds18b20_port->gpio_write(DS18B20_WRITE_1,DS18B20_MASK); + delay_us(24); + ds18b20_port->gpio_write(0x00,DS18B20_MASK); + delay_us(854); + ds18b20_port->gpio_write(DS18B20_WRITE_1,DS18B20_MASK); + // set io input + ds18b20_port->gpio_control(GPIO_CMD_SET_BIT_DIR_INPUT,(void *)(DS18B20_MASK)); + do{ + CONT++; + delay_us(12); + if(CONT > 8000)FLAG = 0; + ds18b20_port->gpio_read((&read_data),DS18B20_MASK); + read_data = (read_data & DS18B20_MASK); + }while((read_data != 0) &&(FLAG == 1)); + delay_us(547); + // set io output + ds18b20_port->gpio_control(GPIO_CMD_SET_BIT_DIR_OUTPUT,(void *)(DS18B20_MASK)); + ds18b20_port->gpio_write(DS18B20_WRITE_1,DS18B20_MASK); + + return FLAG; +} + +uint8_t DS18b20_ReadOneChar(DEV_GPIO_PTR ds18b20_port,uint8_t ds18b20_bit) +{ + uint8_t i=0; + uint32_t read_bit; + uint8_t data=0x00; + uint32_t DS18B20_MASK = 0x01 << ds18b20_bit; + uint32_t DS18B20_WRITE_1 = 0x01 << ds18b20_bit; + + ds18b20_port->gpio_open(GPIO_DIR_OUTPUT); + // set io output + ds18b20_port->gpio_control(GPIO_CMD_SET_BIT_DIR_OUTPUT,(void *)(DS18B20_MASK)); + ds18b20_port->gpio_write(DS18B20_WRITE_1,DS18B20_MASK); + delay_us(20); + + for(i=8;i>0;i--) + { + data >>= 1; + // set io output + ds18b20_port->gpio_control(GPIO_CMD_SET_BIT_DIR_OUTPUT,(void *)(DS18B20_MASK)); + ds18b20_port->gpio_write(0x0000,DS18B20_MASK); + + for(uint32_t volatile y=39;y>0;y--); //delay + + ds18b20_port->gpio_control(GPIO_CMD_SET_BIT_DIR_INPUT,(void *)(DS18B20_MASK)); + ds18b20_port->gpio_read((&read_bit),DS18B20_MASK); + read_bit = read_bit & DS18B20_MASK; + if(read_bit) data |=0x80; + else data &= 0x7f; + delay_us(52); + } + return data; +} + +void DS18b20_WriteOneChar(DEV_GPIO_PTR ds18b20_port,uint8_t ds18b20_bit, uint8_t data) +{ + + uint32_t DS18B20_MASK = 0x01 << ds18b20_bit; + uint32_t DS18B20_WRITE_1 = 0x01 << ds18b20_bit; + ds18b20_port->gpio_open(GPIO_DIR_OUTPUT); + //set io output + ds18b20_port->gpio_control(GPIO_CMD_SET_BIT_DIR_OUTPUT,(void *)(DS18B20_MASK)); + for(uint8_t i=8;i>0;i--) + { + + ds18b20_port->gpio_write(0x0000,DS18B20_MASK); + delay_us(12); + if(data & 0x01) + ds18b20_port->gpio_write(DS18B20_WRITE_1,DS18B20_MASK); + else + ds18b20_port->gpio_write(0x0000,DS18B20_MASK); + delay_us(70); + ds18b20_port->gpio_write(DS18B20_WRITE_1,DS18B20_MASK); + delay_us(3); + data >>= 1; + } +} + +void DS18b20_ReadTemperature(DEV_GPIO_PTR ds18b20_port, uint8_t ds18b20_bit, DS18B20_DEF_PTR obj) +{ + uint8_t tempL=0,tempH=0; + uint16_t temp_data; + DS18b20_init(ds18b20_port,ds18b20_bit); + DS18b20_WriteOneChar(ds18b20_port,ds18b20_bit,0xcc); + DS18b20_WriteOneChar(ds18b20_port,ds18b20_bit,0x44); + + DS18b20_init(ds18b20_port,ds18b20_bit); + DS18b20_WriteOneChar(ds18b20_port,ds18b20_bit,0xcc); + DS18b20_WriteOneChar(ds18b20_port,ds18b20_bit,0xbe); + tempL = DS18b20_ReadOneChar(ds18b20_port,ds18b20_bit); + tempH = DS18b20_ReadOneChar(ds18b20_port,ds18b20_bit); + printf("tempH = %2x, tempL = %2x\r\n",tempH,tempL); + + temp_data = tempH *256 + tempL; + + if(temp_data & 0xf800) + { + obj->temperature_sign = 1; + temp_data = (~temp_data) + 1; + } + else + { + obj->temperature_sign = 0; + } + + obj->temperature_int = (temp_data & 0x7f0)/16; + obj->temperature_decimal = temp_data & 0x0f; +} + diff --git a/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Aquarius_subsystem/DS18B20.h b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Aquarius_subsystem/DS18B20.h new file mode 100644 index 00000000..50f6b689 --- /dev/null +++ b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Aquarius_subsystem/DS18B20.h @@ -0,0 +1,21 @@ +#ifndef _DS18B20_H_ +#define _DS18B20_H_ + +/* DS18B20 sensor object type*/ +typedef struct { + uint8_t temperature_int; + uint8_t temperature_decimal; + uint8_t temperature_sign; +} DS18B20_DEF, * DS18B20_DEF_PTR; + +// extern void delay_us( uint32_t us); + +extern uint8_t DS18b20_init(DEV_GPIO_PTR ds18b20_port,uint8_t ds18b20_bit); + +extern uint8_t DS18b20_ReadOneChar(DEV_GPIO_PTR ds18b20_port,uint8_t ds18b20_bit); + +extern void DS18b20_WriteOneChar(DEV_GPIO_PTR ds18b20_port,uint8_t ds18b20_bit, uint8_t data); + +extern void DS18b20_ReadTemperature(DEV_GPIO_PTR ds18b20_port, uint8_t ds18b20_bit, DS18B20_DEF_PTR obj); + +#endif \ No newline at end of file diff --git a/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Aquarius_subsystem/esp8266_gagent_common.c b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Aquarius_subsystem/esp8266_gagent_common.c new file mode 100644 index 00000000..20705411 --- /dev/null +++ b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Aquarius_subsystem/esp8266_gagent_common.c @@ -0,0 +1,325 @@ +#include "embARC.h" +#include "embARC_debug.h" + +#include "dev_gpio.h" +#include "dev_uart.h" +#include "dev_iic.h" +#include "dev_spi.h" +#include "board.h" + +#include "esp8266_gagent_common.h" + + +void mcu2wifi_product_info(DEV_UART_PTR esp8266_uart, uint8_t *product_key, uint8_t *product_secret) +{ + uint8_t checksum=0; + int i=0; + uint8_t send_data[] = { + 0xFF,0xFF, + 0x00,0x00, + 0x02, + 0x00, + 0x00,0x00, + '0','0','0','0','0','0','0','4', + '0','0','0','0','0','0','0','2', + '0','0','0','0','0','0','0','0', + '0','0','0','0','0','0','0','0', + + + '0','0','0','0','0','0','0','0', + '0','0','0','0','0','0','0','0', + '0','0','0','0','0','0','0','0', + '0','0','0','0','0','0','0','0', + + 0x01,0x2C, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + + '0','0','0','0','0','0','0','0', + '0','0','0','0','0','0','0','0', + '0','0','0','0','0','0','0','0', + '0','0','0','0','0','0','0','0', + 0xEE + }; + send_data[2] = (sizeof(send_data)-0x04)>>8; + send_data[3] = (sizeof(send_data)-0x04)%256; + send_data[5] = sn; + for(i=0; i<32; i++) + { + send_data[40+i] = product_key[i]; + send_data[82+i] = product_secret[i]; + } + for(i=0; i<32; i++) EMBARC_PRINTF("%c",send_data[40+i]); EMBARC_PRINTF("\r\n"); + for(i=0; i<32; i++) EMBARC_PRINTF("%c",send_data[82+i]); EMBARC_PRINTF("\r\n"); + + for(uint8_t i=2;i< sizeof(send_data)-1;i++) checksum = checksum + send_data[i]; + send_data[sizeof(send_data)-1] =checksum; + + esp8266_uart->uart_write(send_data,sizeof(send_data)); +} + + +void mcu2wifi_heartbeat(DEV_UART_PTR esp8266_uart) +{ + uint8_t checksum=0; + uint8_t send_data[] = { + 0xFF,0xFF, + 0x00,0x00, + 0x08, + 0x00, + 0x00,0x00, + 0x00 + }; + send_data[2] = (sizeof(send_data)-0x04)>>8; + send_data[3] = (sizeof(send_data)-0x04)%256; + send_data[5] = sn; + // + for(uint8_t i=2;iuart_write(send_data,sizeof(send_data)); +} + + +void mcu2wifi_set_cfg_mode(DEV_UART_PTR esp8266_uart, uint8_t mode) +{ + uint8_t checksum=0; + uint8_t send_data[] = { + 0xFF,0xFF, // + 0x00,0x05, // + 0x09, // + 0x00, // + 0x00,0x00, //flags + 0x00, // + 0x00 // + }; + send_data[2] = (sizeof(send_data)-0x04)>>8; // + send_data[3] = (sizeof(send_data)-0x04)%256; // + send_data[5] = ++sn; // + + send_data[8] = mode; + // + for(uint8_t i=2;iuart_write(send_data,sizeof(send_data)); +} + + +void mcu2wifi_reset_wifi(DEV_UART_PTR esp8266_uart) +{ + uint8_t checksum=0; + uint8_t send_data[] = { + 0xFF,0xFF, // + 0x00,0x00, // + 0x0b, // + 0x00, // + 0x00,0x00, //flags + 0x00 // + }; + send_data[2] = (sizeof(send_data)-0x04)>>8; // + send_data[3] = (sizeof(send_data)-0x04)%256; // + send_data[5] = ++sn; // + // + for(uint8_t i=2;iuart_write(send_data,sizeof(send_data)); +} + + + +void mcu2wifi_wifi_statu(DEV_UART_PTR esp8266_uart,uint8_t *receive_data) +{ + uint8_t checksum=0; + uint8_t send_data[] = { + 0xFF,0xFF, // + 0x00,0x05, // + 0x0E, // + 0x00, // + 0x00,0x00, //flags + 0x00 // + }; + send_data[2] = (sizeof(send_data)-0x04)>>8; + send_data[3] = (sizeof(send_data)-0x04)%256; + send_data[5] = sn; + for(uint8_t i=2;iuart_write(send_data,sizeof(send_data)); + + uint8_t statu_bit0_7 = receive_data[9]; + uint8_t statu_bit8_15 = receive_data[8]; + + if((statu_bit0_7) %2 ) EMBARC_PRINTF("SoftAP mode is open\r\n"); + if((statu_bit0_7>>1 )%2 ) EMBARC_PRINTF("Station mode is open\r\n"); + if((statu_bit0_7>>2 )%2 ) EMBARC_PRINTF("OnBoarding mode is open\r\n"); + // + if((statu_bit0_7>>3 )%2 ) + { + EMBARC_PRINTF("Binding mode is open\r\n"); + led = gpio_get_dev(LED_PORT); + led-> gpio_open(0x20); + led-> gpio_control(GPIO_CMD_SET_BIT_DIR_OUTPUT,(void *)(0x20)); + led-> gpio_write(0x00,0x20); + } + else + { + led = gpio_get_dev(LED_PORT); + led-> gpio_open(0x20); + led-> gpio_control(GPIO_CMD_SET_BIT_DIR_OUTPUT,(void *)(0x20)); + led-> gpio_write(0x20,0x20); + } + // + if((statu_bit0_7>>4 )%2 ) + { + EMBARC_PRINTF("The WiFi module is connected to the wireless router,the RSSI = %d\r\n",(statu_bit8_15)%8); + led = gpio_get_dev(LED_PORT); + led-> gpio_open(0x07); + led-> gpio_control(GPIO_CMD_SET_BIT_DIR_OUTPUT,(void *)(0x07)); + led-> gpio_write(~((statu_bit8_15)%8),0x07); + } + else + { + led = gpio_get_dev(LED_PORT); + led-> gpio_open(0x07); + led-> gpio_control(GPIO_CMD_SET_BIT_DIR_OUTPUT,(void *)(0x07)); + led-> gpio_write(0x7,0x07); + } + //LED 3 + if((statu_bit0_7>>5 )%2 ) + { + EMBARC_PRINTF("The WiFi module is connected to the M2M server\r\n"); + led = gpio_get_dev(LED_PORT); + led-> gpio_open(0x08); + led-> gpio_control(GPIO_CMD_SET_BIT_DIR_OUTPUT,(void *)(0x08)); + led-> gpio_write(0x00,0x08); + + } + else + { + led = gpio_get_dev(LED_PORT); + led-> gpio_open(0x08); + led-> gpio_control(GPIO_CMD_SET_BIT_DIR_OUTPUT,(void *)(0x08)); + led-> gpio_write(0x08,0x08); + } + //LED 4 + if((statu_bit8_15>>3)%2 ) + { + EMBARC_PRINTF("APP online\r\n"); + led = gpio_get_dev(LED_PORT); + led-> gpio_open(0x10); + led-> gpio_control(GPIO_CMD_SET_BIT_DIR_OUTPUT,(void *)(0x10)); + led-> gpio_write(0x00,0x10); + + } + else + { + led = gpio_get_dev(LED_PORT); + led-> gpio_open(0x10); + led-> gpio_control(GPIO_CMD_SET_BIT_DIR_OUTPUT,(void *)(0x10)); + led-> gpio_write(0x10,0x10); + } + +} + + +void mcu2wifi_receive_error(DEV_UART_PTR esp8266_uart) +{ + uint8_t checksum=0; + uint8_t send_data[] = { + 0xFF,0xFF, // + 0x00,0x00, // + 0x12, // + 0x00, // + 0x00,0x00, //flags + 0x00, // + 0x00 // + }; + send_data[2] = (sizeof(send_data)-0x04)>>8; // + send_data[3] = (sizeof(send_data)-0x04)%256; // + send_data[5] = sn; // + send_data[8] = 2; + // + for(uint8_t i=2;iuart_write(send_data,sizeof(send_data)); +} + + +void mcu2wifi_request_time(DEV_UART_PTR esp8266_uart) +{ + uint8_t checksum=0; + uint8_t send_data[] = { + 0xFF,0xFF, // + 0x00,0x05, // + 0x17, // + 0x00, // + 0x00,0x00, // + 0x00 // + }; + send_data[2] = (sizeof(send_data)-0x04)>>8; // + send_data[3] = (sizeof(send_data)-0x04)%256; // + send_data[5] = ++sn; // + // + for(uint8_t i=2;iuart_write(send_data,sizeof(send_data)); +} + + +void wifi2mcu_get_time(uint8_t *receive_data) +{ + year = receive_data[8]*256+receive_data[9]; + month = receive_data[10]; + day = receive_data[11]; + hour = receive_data[12]; + minute = receive_data[13]; + second = receive_data[14]; + + EMBARC_PRINTF("Now is %d/%d/%d %d:%d:%d \r\n",year,month,day,hour,minute,second); +} + + +void mcu2wifi_restart_wifi(DEV_UART_PTR esp8266_uart) +{ + uint8_t checksum=0; + uint8_t send_data[] = { + 0xFF,0xFF, // + 0x00,0x00, // + 0x29, // + 0x00, // + 0x00,0x00, //flags + 0x00 // + }; + send_data[2] = (sizeof(send_data)-0x04)>>8; // + send_data[3] = (sizeof(send_data)-0x04)%256; // + send_data[5] = ++sn; // + // + for(uint8_t i=2;iuart_write(send_data,sizeof(send_data)); +} + + +void mcu2wifi_request_into_binding(DEV_UART_PTR esp8266_uart) +{ + uint8_t checksum=0; + uint8_t send_data[] = { + 0xFF,0xFF, // + 0x00,0x00, // + 0x15, // + 0x00, // + 0x00,0x00, //flags + 0x00 // + }; + send_data[2] = (sizeof(send_data)-0x04)>>8; // + send_data[3] = (sizeof(send_data)-0x04)%256; // + send_data[5] = ++sn; // + // + for(uint8_t i=2;iuart_write(send_data,sizeof(send_data)); +} \ No newline at end of file diff --git a/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Aquarius_subsystem/esp8266_gagent_common.h b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Aquarius_subsystem/esp8266_gagent_common.h new file mode 100644 index 00000000..fba83433 --- /dev/null +++ b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Aquarius_subsystem/esp8266_gagent_common.h @@ -0,0 +1,47 @@ +#ifndef _ESP8266_GAGENT_COMMON_H_ +#define _ESP8266_GAGENT_COMMON_H_ + +#include "dev_uart.h" + +uint8_t sn; +uint16_t year; +uint8_t month; +uint8_t day; +uint8_t hour; +uint8_t minute; +uint8_t second; + + +DEV_GPIO_PTR led; +#define LED_PORT DW_GPIO_PORT_B +#define LED_MASK 0x1ff +#define LED_DIR 0x1ff + + + +extern void mcu2wifi_product_info(DEV_UART_PTR esp8266_uart, uint8_t *product_key, uint8_t *product_secret); + +extern void mcu2wifi_heartbeat(DEV_UART_PTR esp8266_uart); + + + +extern void mcu2wifi_set_cfg_mode(DEV_UART_PTR esp8266_uart,uint8_t mode); + + + +extern void mcu2wifi_reset_wifi(DEV_UART_PTR esp8266_uart); + + +extern void mcu2wifi_wifi_statu(DEV_UART_PTR esp8266_uart,uint8_t *receive_data); + + +extern void mcu2wifi_receive_error(DEV_UART_PTR esp8266_uart); + +extern void mcu2wifi_request_time(DEV_UART_PTR esp8266_uart); +extern void wifi2mcu_get_time(uint8_t *receive_data); + +extern void mcu2wifi_restart_wifi(DEV_UART_PTR esp8266_uart); + +extern void mcu2wifi_request_into_binding(DEV_UART_PTR esp8266_uart); + +#endif \ No newline at end of file diff --git a/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Aquarius_subsystem/esp8266_gagent_user.c b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Aquarius_subsystem/esp8266_gagent_user.c new file mode 100644 index 00000000..7e02dd92 --- /dev/null +++ b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Aquarius_subsystem/esp8266_gagent_user.c @@ -0,0 +1,123 @@ +#include "embARC.h" +#include "embARC_debug.h" + +#include "dev_gpio.h" +#include "dev_uart.h" +#include "dev_iic.h" +#include "dev_spi.h" + +#include "board.h" + +#include "esp8266_gagent_user.h" + + + + + +void mcu2wifi_wifi_ctrl_dev(DEV_UART_PTR esp8266_uart,uint8_t *receive_data) +{ + + uint8_t checksum=0; + uint8_t send_data[] = { + 0xFF,0xFF, + 0x00,0x00, + 0x04, + 0x00, + 0x00,0x00, + 0x00 + }; + uint8_t i; + send_data[2] = (sizeof(send_data)-0x04)>>8; + send_data[3] = (sizeof(send_data)-0x04)%256; + send_data[5] = sn; + for(i=2;iuart_write(send_data,sizeof(send_data)); + + + i=0; + + uint8_t attr_flags[ ATTR_FLAGS_BYTES ]; + for(i=0;i> 1) & 0x07; + + if(attr_flags[0] & 0x04) + Aquarius_Feed_Freq = (attr_vals[0] >> 4) & 0x07; + + if(attr_flags[0] & 0x08) + Aquarius_Feed_Amount = attr_vals[1]; + +} + + + +void mcu2wifi_wifi_read_dev(DEV_UART_PTR esp8266_uart) +{ + uint8_t checksum=0; + uint8_t send_data[0x0A + DEV_STATUS_BYTES] = { + 0xFF,0xFF, + 0x00,0x05, + 0x04, + 0x00, + 0x00,0x00, + 0x03, + 0x00,0x00,0x00, + 0x14 + }; + send_data[2] = (0x0A + DEV_STATUS_BYTES-0x04)>>8; + send_data[3] = (0x0A + DEV_STATUS_BYTES-0x04)%256; + send_data[5] = sn; + + // Aquarius_Oxy_Freq and Aquarius_Feed_Freq and Aquarius_Light + send_data[9] = Aquarius_Feed_Freq * 16 + Aquarius_Oxy_Freq *2 + Aquarius_Light; + // Aquarius_Feed_Amount + send_data[10] = Aquarius_Feed_Amount; + // Aquarius_Temp + send_data[11] = Aquarius_Temp; + + for(uint8_t i=2;i<0x0A + DEV_STATUS_BYTES-1;i++) checksum = checksum + send_data[i]; + send_data[0x0A + DEV_STATUS_BYTES-1] =checksum; + + esp8266_uart->uart_write(send_data,0x0A + DEV_STATUS_BYTES); +} + + +void mcu2wifi_dev_report_status(DEV_UART_PTR esp8266_uart) +{ + uint8_t checksum=0; + uint8_t send_data[0x0A + DEV_STATUS_BYTES] = { + 0xFF,0xFF, + 0x00,0x05, + 0x05, + 0x00, + 0x00,0x00, + 0x04, + 0x00,0x00,0x00, + 0x14 + }; + send_data[2] = (0x0A + DEV_STATUS_BYTES-0x04)>>8; + send_data[3] = (0x0A + DEV_STATUS_BYTES-0x04)%256; + send_data[5] = ++sn; + + // Aquarius_Oxy_Freq and Aquarius_Feed_Freq and Aquarius_Light + send_data[9] = Aquarius_Feed_Freq * 16 + Aquarius_Oxy_Freq *2 + Aquarius_Light; + // Aquarius_Feed_Amount + send_data[10] = Aquarius_Feed_Amount; + // Aquarius_Temp + send_data[11] = Aquarius_Temp; + + + for(uint8_t i=2;i<0x0A + DEV_STATUS_BYTES-1;i++) checksum = checksum + send_data[i]; + send_data[0x0A + DEV_STATUS_BYTES-1] =checksum; + + esp8266_uart->uart_write(send_data,0x0A + DEV_STATUS_BYTES); +} \ No newline at end of file diff --git a/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Aquarius_subsystem/esp8266_gagent_user.h b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Aquarius_subsystem/esp8266_gagent_user.h new file mode 100644 index 00000000..c35d7fe1 --- /dev/null +++ b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Aquarius_subsystem/esp8266_gagent_user.h @@ -0,0 +1,36 @@ +#ifndef _ESP8266_GAGENT_USER_H_ +#define _ESP8266_GAGENT_USER_H_ + +#include "dev_uart.h" +#include "DS18B20.h" +#include "esp8266_gagent_common.h" + + +DEV_GPIO_PTR ds18b20_port_ptr; +DS18B20_DEF_PTR ds18b20_data; +#define DS18B20_PORT DW_GPIO_PORT_C +#define DS18B20_BIT 15 + +#define MAX_READ_CNT 32 + + +// data point +uint8_t Aquarius_Oxy_Freq; +uint8_t Aquarius_Feed_Freq; +uint8_t Aquarius_Temp; +uint8_t Aquarius_Feed_Amount; +uint8_t Aquarius_Light; + + +#define ATTR_FLAGS_BYTES (1) + +#define ATTR_VALS_BYTES (2) +extern void mcu2wifi_wifi_ctrl_dev(DEV_UART_PTR esp8266_uart,uint8_t *receive_data); + + +#define DEV_STATUS_BYTES (3) +extern void mcu2wifi_wifi_read_dev(DEV_UART_PTR esp8266_uart); + +extern void mcu2wifi_dev_report_status(DEV_UART_PTR esp8266_uart); + +#endif \ No newline at end of file diff --git a/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Aquarius_subsystem/main.c b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Aquarius_subsystem/main.c new file mode 100644 index 00000000..9daf30a7 --- /dev/null +++ b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Aquarius_subsystem/main.c @@ -0,0 +1,329 @@ +#include "embARC.h" +#include "embARC_debug.h" +#include +#include "dev_gpio.h" +#include "dev_uart.h" +#include "dev_iic.h" +#include "dev_spi.h" + +#include "../../../board/emsk/drivers/mux/mux.h" +#include "../../../board/emsk/drivers/mux/mux_hal.h" + +#include "esp8266_gagent_common.h" +#include "esp8266_gagent_user.h" +#include "step_motor.h" + + +/**************define the esp8266's interface'**************/ + +//esp8266 TX ----J1-pin3 +//esp8266 RX ----J1-pin2 +DEV_UART_PTR esp8266_uart; +#define ESP8266_UART_ID DW_UART_0_ID +#define ESP8266_UART_BAUDRATE UART_BAUDRATE_9600 + +/**********ESP8266 initial***************/ +void esp8266_init(void) +{ + // set pmod mux and uart map + set_uart_map(0xe4); + esp8266_uart = uart_get_dev(ESP8266_UART_ID); + esp8266_uart->uart_close(); + if(esp8266_uart->uart_open(ESP8266_UART_BAUDRATE) == E_OPNED) + { + esp8266_uart->uart_control(UART_CMD_SET_BAUD, (void *)(ESP8266_UART_BAUDRATE)); + } +} +/**********ESP8266 receive***************/ +uint32_t esp8266_read(uint8_t *buf, uint32_t cnt) +{ + if (esp8266_uart == NULL) return 0; + + uint32_t rd_avail; + esp8266_uart->uart_control(UART_CMD_GET_RXAVAIL, (void *)(&rd_avail)); + cnt = (cnt > rd_avail)?rd_avail:cnt; + if (cnt > 0) { + return esp8266_uart->uart_read(buf, cnt); + } + return cnt; +} +/**********ESP8266 send***************/ +uint32_t esp8266_write(uint8_t *buf, uint32_t cnt) +{ + if (esp8266_uart == NULL) return 0; + + return esp8266_uart->uart_write(buf, cnt); +} + + + +/*********Define the interface, data, and functions of the stepper motor********/ +#define STEP_MOTER_PORT DW_GPIO_PORT_C +#define STEP_MOTER_MASK (0xF0000) + + + +/*********Define pump interfaces, data and functions********/ +DEV_GPIO_PTR pump; +#define PUMP_PORT DW_GPIO_PORT_C +#define PUMP_MASK (0X800000) +#define PUMP_DIR (0X800000) +/*********open pump*************************/ +void pump_open(void) +{ + pump = gpio_get_dev(PUMP_PORT); + pump->gpio_open(PUMP_DIR); + pump->gpio_control(GPIO_CMD_SET_BIT_DIR_OUTPUT,(void *)(PUMP_MASK)); + pump->gpio_write(0xFFFFFFFF & PUMP_MASK,PUMP_MASK); +} +/*********close pump*************************/ +void pump_close(void) +{ + pump = gpio_get_dev(PUMP_PORT); + pump->gpio_open(PUMP_DIR); + pump->gpio_control(GPIO_CMD_SET_BIT_DIR_OUTPUT,(void *)(PUMP_MASK)); + pump->gpio_write(0x00000,PUMP_MASK); +} + +/*********Define the interfaces, data, and functions of the light********/ +//J5 - pin4 +DEV_GPIO_PTR light; +#define LIGHT_PORT DW_GPIO_PORT_C +#define LIGHT_MASK (0X8000000) +#define LIGHT_DIR (0X8000000) +/*********open*************************/ +void lignt_open(void) +{ + light = gpio_get_dev(LIGHT_PORT); + light->gpio_open(LIGHT_DIR); + light->gpio_control(GPIO_CMD_SET_BIT_DIR_OUTPUT,(void *)(LIGHT_MASK)); + light->gpio_write(0xFFFFFFFF & LIGHT_MASK,LIGHT_MASK); +} +/*********close*************************/ +void light_close(void) +{ + light = gpio_get_dev(LIGHT_PORT); + light->gpio_open(LIGHT_DIR); + light->gpio_control(GPIO_CMD_SET_BIT_DIR_OUTPUT,(void *)(LIGHT_MASK)); + light->gpio_write(0x00000,LIGHT_MASK); +} + + +/****************Define the interface and function of the button*********************/ +DEV_GPIO_PTR button; +#define BUTTON_MASK 0x07 +void gpio_keyL_isr(void *ptr) +{ + EMBARC_PRINTF("key 1: L\r\n"); + mcu2wifi_request_into_binding(esp8266_uart); +} +void gpio_keyR_isr(void *ptr) +{ + EMBARC_PRINTF("key 2: R\r\n"); + mcu2wifi_reset_wifi(esp8266_uart); +} +void gpio_keyA_isr(void *ptr) +{ + EMBARC_PRINTF("key 3: A\r\n"); +} +void button_init(void) +{ + DEV_GPIO_BIT_ISR bit_isr; + DEV_GPIO_INT_CFG int_cfg; + button = gpio_get_dev(DW_GPIO_PORT_A); + button ->gpio_open(BUTTON_MASK); + button ->gpio_control(GPIO_CMD_SET_BIT_DIR_INPUT, (void *)BUTTON_MASK); + int_cfg.int_bit_mask = BUTTON_MASK; + int_cfg.int_bit_type = GPIO_INT_BITS_EDGE_TRIG(BUTTON_MASK); + int_cfg.int_bit_polarity = GPIO_INT_BITS_POL_FALL_EDGE(BUTTON_MASK); + int_cfg.int_bit_debounce = GPIO_INT_BITS_ENA_DEBOUNCE(BUTTON_MASK); + button->gpio_control(GPIO_CMD_SET_BIT_INT_CFG, &int_cfg); + bit_isr.int_bit_ofs = 0; + bit_isr.int_bit_handler = gpio_keyL_isr; + button->gpio_control(GPIO_CMD_SET_BIT_ISR, (void *)(&bit_isr)); + bit_isr.int_bit_ofs = 1; + bit_isr.int_bit_handler = gpio_keyR_isr; + button->gpio_control(GPIO_CMD_SET_BIT_ISR, (void *)(&bit_isr)); + bit_isr.int_bit_ofs = 2; + bit_isr.int_bit_handler = gpio_keyA_isr; + button->gpio_control(GPIO_CMD_SET_BIT_ISR, (void *)(&bit_isr)); + + button->gpio_control(GPIO_CMD_ENA_BIT_INT, (void *)(0x7)); +} + + +/**********Timer 1 ISR***********/ +void timer0_isr(void *ptr) +{ + + timer_int_clear(TIMER_0); + float temperatuer; + ds18b20_port_ptr = gpio_get_dev(DS18B20_PORT); + DS18b20_ReadTemperature(ds18b20_port_ptr,DS18B20_BIT,ds18b20_data); + temperatuer = (ds18b20_data->temperature_int)+(ds18b20_data->temperature_decimal)*0.0625; + if(ds18b20_data->temperature_sign) temperatuer = 0-temperatuer; + Aquarius_Temp = (uint8_t)(temperatuer + 55); + printf("temperatuer is :%f , send data Aquarius_Temp is %x \r\n",temperatuer,Aquarius_Temp ); + static uint8_t report_time; + if(report_time == 6) + { + report_time = 0; + mcu2wifi_dev_report_status(esp8266_uart); + } + report_time++; +} + +void timer1_isr(void *ptr) +{ + timer_int_clear(TIMER_1); + + switch(Aquarius_Feed_Freq) + { + case 0: + // if( hour == 7 && minute == 0 ) + if( hour == 7 && minute==0 ) + step_motor_active(0, Aquarius_Feed_Amount * 514,253); + break; + case 1: + if( (hour == 7 && minute==0) ||(hour == 17 && minute==0) ) + step_motor_active(0,Aquarius_Feed_Amount*514,253); + break; + case 2: + if( (hour == 7 && minute==0) ||(hour == 12 && minute==0)||(hour == 17 && minute==0) ) + step_motor_active(0,Aquarius_Feed_Amount*514,253); + break; + case 3: + if( (hour == 7 && minute==0) || (hour == 10 && minute==30) || (hour == 14 && minute==30) || (hour == 17 && minute==0) ) + step_motor_active(0,Aquarius_Feed_Amount*514,253); + break; + case 4: + //if( (hour == 7 && minute==0) || (hour == 9 && minute==30) || (hour == 12 && minute==00) || (hour == 14 && minute==30) || (hour == 17 && minute==0) ) + if(minute % 2 ==0) + step_motor_active(0,Aquarius_Feed_Amount*514,253); + break; + case 5: + default: break; + } + + + mcu2wifi_request_time(esp8266_uart); +} + +uint8_t *product_key = "96dcc24b14ae4c69927cce0d3903fc54"; +uint8_t *product_secret = "6c43c195b1174bdaa9bac97f2db90e90"; +int main(void) +{ + cpu_lock(); + board_init(); + + timer_stop(TIMER_0); /* Stop it first since it might be enabled before */ + int_handler_install(INTNO_TIMER0, timer0_isr); + int_enable(INTNO_TIMER0); + timer_start(TIMER_0, TIMER_CTRL_IE, 10*BOARD_CPU_CLOCK); + + timer_stop(TIMER_1); /* Stop it first since it might be enabled before */ + int_handler_install(INTNO_TIMER1, timer1_isr); + int_enable(INTNO_TIMER1); + timer_start(TIMER_1, TIMER_CTRL_IE, 60*BOARD_CPU_CLOCK); + + set_pmod_mux( + PM1_UR_UART_0|PM1_LR_GPIO_A | + PM2_GPIO_AC | + PM3_GPIO_AC | + PM4_GPIO_AC | + PM5_UR_GPIO_C | PM5_LR_GPIO_A | + PM6_UR_GPIO_C | PM6_LR_GPIO_A + ); + button_init(); + cpu_unlock(); + + esp8266_init(); + step_motor_init(STEP_MOTER_PORT, STEP_MOTER_MASK); + + uint32_t rd_cnt = 0; + uint8_t read_data[MAX_READ_CNT]; + + led = gpio_get_dev(LED_PORT); + led-> gpio_open(0x80); + led-> gpio_control(GPIO_CMD_SET_BIT_DIR_OUTPUT,(void *)(0x80)); + uint8_t led_count = 0; + + + Aquarius_Feed_Amount = 1; + Aquarius_Light = 0; + while(1) + { + if((led_count)%10 == 0 ) led->gpio_write(0x80,0x80); + if((led_count)%20 == 0 ) led->gpio_write(0x00,0x80); + led_count++; + + board_delay_ms(100,0); + rd_cnt = esp8266_read(read_data,sizeof(read_data)); + if(rd_cnt) + { + for(uint8_t i=0;i0;x--) + for(y=us;y>0;y--); + +} +/* + Stepper motor initialization program + +*/ +void step_motor_init(uint32_t gpio_port, uint32_t gpio_port_mask) +{ + step_motor_port = gpio_get_dev(gpio_port); + port_bit_mask = gpio_port_mask; + + step_motor_port->gpio_open(GPIO_DIR_OUTPUT); + step_motor_port->gpio_control(GPIO_CMD_SET_BIT_DIR_OUTPUT,(void *)(port_bit_mask)); + step_motor_port->gpio_write(0x00000000,port_bit_mask); +} + + +/* + Stepper motor control program +*/ +void step_motor_active(uint8_t step_dir, uint16_t step_angle, uint8_t step_speed) +{ + switch(step_dir) + { + case 0: + for(uint16_t angle = step_angle; angle > 0; angle--) + { + delay_us( (255 - step_speed)*SPEED_FACT ); + step_motor_port->gpio_write(0x77777777,port_bit_mask); + delay_us( (255 - step_speed)*SPEED_FACT ); + step_motor_port->gpio_write(0x33333333,port_bit_mask); + delay_us( (255 - step_speed)*SPEED_FACT ); + step_motor_port->gpio_write(0xBBBBBBBB,port_bit_mask); + delay_us( (255 - step_speed)*SPEED_FACT ); + step_motor_port->gpio_write(0x99999999,port_bit_mask); + delay_us( (255 - step_speed)*SPEED_FACT ); + step_motor_port->gpio_write(0xDDDDDDDD,port_bit_mask); + delay_us( (255 - step_speed)*SPEED_FACT ); + step_motor_port->gpio_write(0xCCCCCCCC,port_bit_mask); + delay_us( (255 - step_speed)*SPEED_FACT ); + step_motor_port->gpio_write(0xEEEEEEEE,port_bit_mask); + delay_us( (255 - step_speed)*SPEED_FACT ); + step_motor_port->gpio_write(0x66666666,port_bit_mask); + delay_us( (255 - step_speed)*SPEED_FACT ); + step_motor_port->gpio_write(0x00000000,port_bit_mask); + }; + break; + case 1: + for(uint16_t angle = step_angle; angle > 0; angle--) + { + delay_us( (255 - step_speed)*SPEED_FACT ); + step_motor_port->gpio_write(0x66666666,port_bit_mask); + delay_us( (255 - step_speed)*SPEED_FACT ); + step_motor_port->gpio_write(0xEEEEEEEE,port_bit_mask); + delay_us( (255 - step_speed)*SPEED_FACT ); + step_motor_port->gpio_write(0xCCCCCCCC,port_bit_mask); + delay_us( (255 - step_speed)*SPEED_FACT); + step_motor_port->gpio_write(0xDDDDDDDD,port_bit_mask); + delay_us( (255 - step_speed)*SPEED_FACT ); + step_motor_port->gpio_write(0x99999999,port_bit_mask); + delay_us( (255 - step_speed)*SPEED_FACT ); + step_motor_port->gpio_write(0xBBBBBBBB,port_bit_mask); + delay_us( (255 - step_speed)*SPEED_FACT ); + step_motor_port->gpio_write(0x33333333,port_bit_mask); + delay_us( (255 - step_speed)*SPEED_FACT ); + step_motor_port->gpio_write(0x77777777,port_bit_mask); + delay_us( (255 - step_speed)*SPEED_FACT ); + step_motor_port->gpio_write(0x00000000,port_bit_mask); + }; + break; + default: break; + } +} \ No newline at end of file diff --git a/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Aquarius_subsystem/step_motor.h b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Aquarius_subsystem/step_motor.h new file mode 100644 index 00000000..5d6b9c18 --- /dev/null +++ b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Aquarius_subsystem/step_motor.h @@ -0,0 +1,9 @@ +#ifndef _STEP_MOTOT_H +#define _STEP_MOTOT_H + +#include "dev_gpio.h" + +extern void step_motor_init(uint32_t gpio_port, uint32_t gpio_port_mask); +extern void step_motor_active(uint8_t step_dir, uint16_t step_angle, uint8_t step_speed); + +#endif \ No newline at end of file diff --git a/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Plant_subsystem/DHT11.c b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Plant_subsystem/DHT11.c new file mode 100644 index 00000000..d9d65823 --- /dev/null +++ b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Plant_subsystem/DHT11.c @@ -0,0 +1,130 @@ +#include "embARC.h" +#include "embARC_debug.h" + +#include "dev_gpio.h" +#include "board.h" +#include "DHT11.h" + + + +static void delay_us( uint32_t us) +{ + uint32_t volatile x,y; + for(x=2;x>0;x--) + for(y=us;y>0;y--); + +} + +static uint8_t DHT11_Read_Byte(DEV_GPIO_PTR DHT11_port, uint32_t DHT11_bit) +{ + + uint8_t level_cnt=0; + uint32_t read_data=0; + uint8_t uchartemp,ucharcomdata; + + + uint32_t DHT11_MASK = 0x01 << DHT11_bit; + DHT11_port->gpio_open(GPIO_DIR_OUTPUT); + + for(uint8_t i=0;i<8;i++) + { + level_cnt=2; + do{ + DHT11_port->gpio_control(GPIO_CMD_SET_BIT_DIR_INPUT,(void *)(DHT11_MASK)); + DHT11_port->gpio_read(&read_data,DHT11_MASK); + read_data &= DHT11_MASK; + }while((!read_data) && level_cnt++); + delay_us(30); + uchartemp = 0; + + DHT11_port->gpio_read(&read_data,DHT11_MASK); + read_data &= DHT11_MASK; + if(read_data) uchartemp = 1; + + level_cnt=2; + do{ + DHT11_port->gpio_control(GPIO_CMD_SET_BIT_DIR_INPUT,(void *)(DHT11_MASK)); + DHT11_port->gpio_read(&read_data,DHT11_MASK); + read_data &= DHT11_MASK; + }while((read_data) && level_cnt++); + if(level_cnt == 1) break; + ucharcomdata <<= 1; + ucharcomdata |= uchartemp; + } + return ucharcomdata; +} + +void DHT11_Read_Data(DEV_GPIO_PTR DHT11_port, uint32_t DHT11_bit, DHT11_DEF_PTR data) +{ + uint32_t read_data=0; + uint8_t level_cnt=0; + uint8_t RH_data_H_temp,RH_data_L_temp,T_data_H_temp,T_data_L_temp,checkdata_temp; + uint8_t RH_data_H,RH_data_L,T_data_H,T_data_L,checkdata; + + uint32_t DHT11_MASK = 0x01 << DHT11_bit; + DHT11_port->gpio_open(GPIO_DIR_OUTPUT); + uint32_t DHT11_WRITE_1 = 0x01 << DHT11_bit; + + // + DHT11_port->gpio_control(GPIO_CMD_SET_BIT_DIR_OUTPUT,(void *)(DHT11_MASK)); + // + DHT11_port->gpio_write(0x00,DHT11_MASK); + // + delay_us(20000); //delay > 18ms + // + DHT11_port->gpio_write(DHT11_WRITE_1,DHT11_MASK); + // + DHT11_port->gpio_control(GPIO_CMD_SET_BIT_DIR_INPUT,(void *)(DHT11_MASK)); + // + delay_us(30); + DHT11_port->gpio_read(&read_data,DHT11_MASK); + read_data &= DHT11_MASK; + if(!read_data) + { + level_cnt = 2; + do{ + DHT11_port->gpio_control(GPIO_CMD_SET_BIT_DIR_INPUT,(void *)(DHT11_MASK)); + DHT11_port->gpio_read(&read_data,DHT11_MASK); + read_data &= DHT11_MASK; + // + }while((!read_data) && level_cnt++); + level_cnt = 2; + do{ + DHT11_port->gpio_control(GPIO_CMD_SET_BIT_DIR_INPUT,(void *)(DHT11_MASK)); + DHT11_port->gpio_read(&read_data,DHT11_MASK); + read_data &= DHT11_MASK; + // delay_us(2); + }while((read_data) && level_cnt++); + + RH_data_H_temp=DHT11_Read_Byte(DHT11_port, DHT11_bit); + RH_data_L_temp=DHT11_Read_Byte(DHT11_port, DHT11_bit); + T_data_H_temp =DHT11_Read_Byte(DHT11_port, DHT11_bit); + T_data_L_temp =DHT11_Read_Byte(DHT11_port, DHT11_bit); + checkdata_temp = DHT11_Read_Byte(DHT11_port, DHT11_bit); + // + DHT11_port->gpio_control(GPIO_CMD_SET_BIT_DIR_OUTPUT,(void *)(DHT11_MASK)); + // + DHT11_port->gpio_write(DHT11_WRITE_1,DHT11_MASK); + if(checkdata_temp == ( + RH_data_H_temp + RH_data_L_temp + + T_data_H_temp + T_data_L_temp + )) + { + RH_data_H = RH_data_H_temp; + RH_data_L = RH_data_L_temp; + T_data_H = T_data_H_temp; + T_data_L = T_data_L_temp; + checkdata = checkdata_temp; + } + data->RH_data = RH_data_H; + data->T_data = T_data_H; + } + else + { + data->RH_data = 0; + data->T_data = 0; + } + + DHT11_port->gpio_close(); +} + diff --git a/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Plant_subsystem/DHT11.h b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Plant_subsystem/DHT11.h new file mode 100644 index 00000000..52fdb510 --- /dev/null +++ b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Plant_subsystem/DHT11.h @@ -0,0 +1,13 @@ +#ifndef _DHT11_H_ +#define _DHT11_H_ + +#include "dev_gpio.h" +/* DHT11 sensor object type*/ +typedef struct { + uint8_t RH_data; + uint8_t T_data; +} DHT11_DEF, * DHT11_DEF_PTR; + +extern void DHT11_Read_Data(DEV_GPIO_PTR DHT11_port, uint32_t DHT11_bit, DHT11_DEF_PTR data); + +#endif \ No newline at end of file diff --git a/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Plant_subsystem/ad7991.c b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Plant_subsystem/ad7991.c new file mode 100644 index 00000000..e6a5ff4a --- /dev/null +++ b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Plant_subsystem/ad7991.c @@ -0,0 +1,266 @@ +/* ------------------------------------------ + * Copyright (c) 2017, Synopsys, Inc. All rights reserved. + + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + + * 1) Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + + * 2) 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. + + * 3) Neither the name of the Synopsys, Inc., 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. + * +--------------------------------------------- */ + +/** + * + * \brief PMOD AD2 ADC Driver + * \details + * Realize driver for PMOD AD2 ADC using I2C driver. + */ + +/** + * \file + * \brief pmod AD2 ADC(ad7991) driver + */ + + +//#define DBG_MORE + +#include "arc.h" +#include "arc_builtin.h" +#include "embARC_toolchain.h" +#include "embARC_error.h" +#include "embARC_debug.h" + +#include "dev_iic.h" +#include "ad7991.h" +#include "board.h" + +/** AD7991_REG_CONFIG definition */ +#define AD7991_CONFIG_BIT_TRAIL_DELAY (1 << 0) +#define AD7991_CONFIG_SAMPLE_DELAY (1 << 1) +#define AD7991_CONFIG_FLTR (1 << 2) +#define AD7991_CONFIG_REF_SEL (1 << 3) +/** AD7991_CONFIG_CH0 ~ CH3 defines see ad7991.h */ + +#define EMSK_ADC_CHECK_EXP_NORTN(EXPR) CHECK_EXP_NOERCD(EXPR, error_exit) + +/** + * \brief write ad7991 register + * \param[in] obj ad7991 object + * \param[in] val config value to write + * \retval >=0 write success, return bytes written + * \retval !E_OK write failed + */ +static int32_t _ad7991_reg_write(AD7991_DEF_PTR obj, uint8_t val) +{ + int32_t ercd = E_PAR; + DEV_IIC_PTR iic_obj = iic_get_dev(obj->dev_id); + + //dbg_printf(DBG_MORE_INFO, "[%s]%d: iic_obj 0x%x -> 0x%x\r\n", __FUNCTION__, __LINE__, iic_obj, *iic_obj); + EMSK_ADC_CHECK_EXP_NORTN(iic_obj!=NULL); + + /** make sure set the slave address */ + iic_obj->iic_control(IIC_CMD_MST_SET_TAR_ADDR, CONV2VOID(obj->slvaddr)); + ercd = iic_obj->iic_control(IIC_CMD_MST_SET_NEXT_COND, CONV2VOID(IIC_MODE_STOP)); + ercd = iic_obj->iic_write(&val, 1); + +error_exit: + return ercd; +} + +/** + * \brief read ad7991 register + * \param[in] obj ad7991 object + * \param[in] val pointer to buffer to read + * \param[in] channel_num active channel number + * \retval >=0 read success, return bytes read + * \retval dev_id); + + //dbg_printf(DBG_MORE_INFO, "[%s]%d: iic_obj 0x%x -> 0x%x\r\n", __FUNCTION__, __LINE__, iic_obj, *iic_obj); + EMSK_ADC_CHECK_EXP_NORTN(iic_obj!=NULL); + + /** make sure set the slave address */ + iic_obj->iic_control(IIC_CMD_MST_SET_TAR_ADDR, CONV2VOID(obj->slvaddr)); + /** read 2 bytes value */ + ercd = iic_obj->iic_control(IIC_CMD_MST_SET_NEXT_COND, CONV2VOID(IIC_MODE_STOP)); + ercd = iic_obj->iic_read(val, 2*channel_num); + +error_exit: + return ercd; +} + +/** + * \brief ad7991 init function + * \param[in] obj ad7991 object + * \retval E_OK init success + * \retval !E_OK init failed + */ +int32_t ad7991_adc_init(AD7991_DEF_PTR obj) +{ + int32_t ercd = E_OK; + DEV_IIC_PTR iic_obj = iic_get_dev(obj->dev_id); + + dbg_printf(DBG_MORE_INFO, "[%s]%d: iic_obj 0x%x -> 0x%x\r\n", __FUNCTION__, __LINE__, iic_obj, *iic_obj); + EMSK_ADC_CHECK_EXP_NORTN(iic_obj!=NULL); + + ercd = iic_obj->iic_open(DEV_MASTER_MODE, IIC_SPEED_HIGH); + if ((ercd == E_OK) || (ercd == E_OPNED)) { + ercd = iic_obj->iic_control(IIC_CMD_MST_SET_TAR_ADDR, CONV2VOID(obj->slvaddr)); + obj->ref_source = AD7991_REF_VDD; + obj->ref_voltage_mv = AD2_REF_VDD; + ad7991_ref_select(obj, AD7991_REF_VDD); + /*AD7991 default open 4 channel on power on, default config reg value is 0xF0*/ + ad7991_adc_channel(obj, 0xF0); + } + +error_exit: + return ercd; +} + +/** + * \brief ad7991 deinit + * \param[in] obj ad7991 object + * \retval E_OK init success + * \retval !E_OK init failed + */ +int32_t ad7991_adc_deinit(AD7991_DEF_PTR obj) +{ + int32_t ercd = E_OK; + DEV_IIC_PTR iic_obj = iic_get_dev(obj->dev_id); + ercd = iic_obj->iic_close(); + return ercd; +} + +/** + * \brief ad7991 reference voltage source selection + * \param[in] obj ad7991 object + * \param[in] source sensor working source, choose from enum sensor_op_mode + * \retval E_OK always return E_OK + */ +int32_t ad7991_ref_select(AD7991_DEF_PTR obj, int32_t source) +{ + + if(source == AD7991_REF_EXTERNAL) { + obj->ref_source = source; + obj->ref_voltage_mv = AD2_REF_EXTERNAL; + obj->reg_config |= AD7991_CONFIG_REF_SEL; + /* choose external reference uses VIN4 as REF, so there are only 3 channels available, disable CH3 here*/ + ad7991_adc_channel(obj, obj->reg_config & ~AD7991_CONFIG_CH3_EN); + } else { /*< AD7991_REF_VDD is default setting*/ + obj->ref_source = AD7991_REF_VDD; + obj->ref_voltage_mv = AD2_REF_VDD; + obj->reg_config &= ~AD7991_CONFIG_REF_SEL; + } + /** set working source bits: first clear bits to zero, then set bits to correct source */ + dbg_printf(DBG_MORE_INFO, "[%s]%d: after: reg 0x%x , source 0x%x\r\n", __FUNCTION__, __LINE__, obj->reg_config, obj->ref_source); + _ad7991_reg_write(obj, obj->reg_config); + return E_OK; +} + +/** + * \brief ad7991 adc channel setting + * \param[in] obj ad7991 object + * \param[in] channel channel selection + * \retval E_OK always return E_OK + */ +int32_t ad7991_adc_channel(AD7991_DEF_PTR obj, int8_t channel) +{ + uint8_t channel_num = 0; + if (channel & AD7991_CONFIG_CH0_EN) { + channel_num ++; + } + if (channel & AD7991_CONFIG_CH1_EN) { + channel_num ++; + } + if (channel & AD7991_CONFIG_CH2_EN) { + channel_num ++; + } + if ((obj->ref_source == AD7991_REF_VDD) && (channel & AD7991_CONFIG_CH3_EN)){ + channel_num ++; + } + /**clear channel bits in config reg and then set bits*/ + obj->reg_config = (obj->reg_config & 0x0F) | channel; + obj->channel_num = channel_num; + _ad7991_reg_write(obj, obj->reg_config); + return E_OK; +} + +/** + * \brief read 4 channel ADC converted value in 1.0 mV unit + * \param[in] obj ad7991 object + * \param[out] val need at least float [4] array to run, return latest avaiable convert data, array index corresponding to channel + * \retval E_OK read success + * \retval !E_OK read failed + */ +int32_t ad7991_adc_read(AD7991_DEF_PTR obj, float *val) +{ + int32_t ercd = E_OK; + uint8_t data[8] = {0}; + uint8_t index; + int16_t voltage; + int16_t actual_ref; + + EMSK_ADC_CHECK_EXP_NORTN(val!=NULL); + + ercd = _ad7991_reg_read(obj, data, obj->channel_num); + + if (ercd != obj->channel_num * 2) { + ercd = E_OBJ; + } else { + dbg_printf(DBG_MORE_INFO, "[%s]%d: ercd 0x%x, channel_num%d\r\n", __FUNCTION__, __LINE__, ercd, obj->channel_num); + ercd = E_OK; + dbg_printf(DBG_MORE_INFO, "[%s]%d: data[0] 0x%x; data[1] 0x%x\r\n", __FUNCTION__, __LINE__, data[0], data[1]); + dbg_printf(DBG_MORE_INFO, "[%s]%d: data[0] 0x%x; data[1] 0x%x\r\n", __FUNCTION__, __LINE__, data[2], data[3]); + dbg_printf(DBG_MORE_INFO, "[%s]%d: data[0] 0x%x; data[1] 0x%x\r\n", __FUNCTION__, __LINE__, data[4], data[5]); + dbg_printf(DBG_MORE_INFO, "[%s]%d: data[0] 0x%x; data[1] 0x%x\r\n", __FUNCTION__, __LINE__, data[6], data[7]); + if(obj->ref_source == AD7991_REF_VDD) { + actual_ref = AD2_REF_VDD; + } else { + actual_ref = AD2_REF_EXTERNAL; + } + /* use index to put value, so if there are more values than channel_num, new value will overwrite old value */ + voltage = ((int16_t)(data[0] &0x0F) << 8) + ((int16_t)data[1]); + index = data[0]>>4; + dbg_printf(DBG_LESS_INFO, "[%s]%d: voltage[0] %x\r\n", __FUNCTION__, __LINE__, voltage); + val[index] = voltage * actual_ref /4096.0; + voltage = ((int16_t)(data[2] &0x0F) << 8) + ((int16_t)data[3]); + index = data[2]>>4; + dbg_printf(DBG_LESS_INFO, "[%s]%d: voltage[1] %x\r\n", __FUNCTION__, __LINE__, voltage); + val[index] = voltage * actual_ref /4096.0; + voltage = ((int16_t)(data[4] &0x0F) << 8) + ((int16_t)data[5]); + index = data[4]>>4; + dbg_printf(DBG_LESS_INFO, "[%s]%d: voltage[2] %x\r\n", __FUNCTION__, __LINE__, voltage); + val[index] = voltage * actual_ref /4096.0; + voltage = ((int16_t)(data[6] &0x0F) << 8) + ((int16_t)data[7]); + index = data[6]>>4; + dbg_printf(DBG_LESS_INFO, "[%s]%d: voltage[3] %x\r\n\r\n", __FUNCTION__, __LINE__, voltage); + val[index] = voltage * actual_ref /4096.0; + } + +error_exit: + return ercd; +} + diff --git a/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Plant_subsystem/ad7991.h b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Plant_subsystem/ad7991.h new file mode 100644 index 00000000..8c51eaae --- /dev/null +++ b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Plant_subsystem/ad7991.h @@ -0,0 +1,105 @@ +/* ------------------------------------------ + * Copyright (c) 2017, Synopsys, Inc. All rights reserved. + + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + + * 1) Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + + * 2) 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. + + * 3) Neither the name of the Synopsys, Inc., 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. + * +--------------------------------------------- */ +/** + * \file + * \brief header file of emsk pmod AD2 ADC driver + */ + +#ifndef _AD7991_H_ +#define _AD7991_H_ + +#include "dev_iic.h" + +/** + * PMOD AD2 board (AD7991) has to choose its voltage reference form power supply, or external on board 2.048V Voltage Reference (ADR380) or VIN4 (Ch3) + * please note that when choose external reference, only 3 channels are converting + */ +#define AD2_JUMPER_REF 0 +#define AD2_JUMPER_VIN4 1 +/* switch for AD2 board Jumper, choose from AD2_JUMPER_REF or AD2_JUMPER_VIN4*/ +#define AD2_REF_JUMPER AD2_JUMPER_REF + +#define AD2_REF_VDD 3300 /*< power supply voltage is 3300mV*/ +#if AD2_REF_JUMPER == AD2_JUMPER_REF +#define AD2_REF_EXTERNAL 2048 /*< The ADR380 voltage reference on AD2 board has 2048mV output*/ +#else +#define AD2_REF_EXTERNAL 1000 /*< choose VIN4 (CH3) as reference, use 1000 to return a fraction voltage value of CH3 */ +#endif + +#define AD7991_CONFIG_CH0_EN (1 << 4) +#define AD7991_CONFIG_CH1_EN (1 << 5) +#define AD7991_CONFIG_CH2_EN (1 << 6) +#define AD7991_CONFIG_CH3_EN (1 << 7) +/** + * I2C address : Pmod AD2 (AD7991) uses AD7991-0, has only one address which is 0x28 + */ +#define AD7991_IIC_ADDRESS 0x28 + +#define ADC_I2C_SLAVE_ADDRESS AD7991_IIC_ADDRESS + +#ifdef __cplusplus +extern "C" { +#endif + +/** options for reference source parameter of ad7991_ref_select() */ +typedef enum ad7991_ref_source { + AD7991_REF_VDD, + AD7991_REF_EXTERNAL +} AD7991_REF_SOURCE; + +/** AD7991 object type */ +typedef struct { + uint32_t dev_id; + uint32_t slvaddr; + + int8_t channel_num; + int8_t reg_config; + int32_t ref_source; + int32_t ref_voltage_mv; +} AD7991_DEF, *AD7991_DEF_PTR; + +#define AD7991_DEFINE(NAME, DEVICE_ID, SLAVE_ADDRESS) \ + AD7991_DEF __ ## NAME = { \ + .dev_id = DEVICE_ID, \ + .slvaddr = SLAVE_ADDRESS, \ + }; \ + AD7991_DEF_PTR NAME = &__ ## NAME + +extern int32_t ad7991_adc_init(AD7991_DEF_PTR obj); +extern int32_t ad7991_adc_deinit(AD7991_DEF_PTR obj); +extern int32_t ad7991_ref_select(AD7991_DEF_PTR obj, int32_t source); +extern int32_t ad7991_adc_channel(AD7991_DEF_PTR obj, int8_t channel); +extern int32_t ad7991_adc_read(AD7991_DEF_PTR obj, float *val); + +#ifdef __cplusplus +} +#endif + +#endif /* _AD7991_H_ */ diff --git a/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Plant_subsystem/esp8266_gagent_common.c b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Plant_subsystem/esp8266_gagent_common.c new file mode 100644 index 00000000..43bc2d45 --- /dev/null +++ b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Plant_subsystem/esp8266_gagent_common.c @@ -0,0 +1,324 @@ +#include "embARC.h" +#include "embARC_debug.h" + +#include "dev_gpio.h" +#include "dev_uart.h" +#include "dev_iic.h" +#include "dev_spi.h" +#include "board.h" + +#include "esp8266_gagent_common.h" + + +void mcu2wifi_product_info(DEV_UART_PTR esp8266_uart, uint8_t *product_key, uint8_t *product_secret) +{ + uint8_t checksum=0; + int i=0; // + uint8_t send_data[] = { + 0xFF,0xFF, // + 0x00,0x00, // + 0x02, // + 0x00, // + 0x00,0x00, //flags + '0','0','0','0','0','0','0','4', // + '0','0','0','0','0','0','0','2', // + '0','0','0','0','0','0','0','0', // + '0','0','0','0','0','0','0','0', // + + + '0','0','0','0','0','0','0','0', // + '0','0','0','0','0','0','0','0', + '0','0','0','0','0','0','0','0', + '0','0','0','0','0','0','0','0', + + 0x01,0x2C, // + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, // + + '0','0','0','0','0','0','0','0', // + '0','0','0','0','0','0','0','0', + '0','0','0','0','0','0','0','0', + '0','0','0','0','0','0','0','0', + 0xEE // + }; + send_data[2] = (sizeof(send_data)-0x04)>>8; // + send_data[3] = (sizeof(send_data)-0x04)%256; // + send_data[5] = sn; // + for(i=0; i<32; i++) + { + send_data[40+i] = product_key[i]; + send_data[82+i] = product_secret[i]; + } + for(i=0; i<32; i++) EMBARC_PRINTF("%c",send_data[40+i]); EMBARC_PRINTF("\r\n"); + for(i=0; i<32; i++) EMBARC_PRINTF("%c",send_data[82+i]); EMBARC_PRINTF("\r\n"); + // + for(uint8_t i=2;i< sizeof(send_data)-1;i++) checksum = checksum + send_data[i]; + send_data[sizeof(send_data)-1] =checksum; + // + esp8266_uart->uart_write(send_data,sizeof(send_data)); +} + + +void mcu2wifi_heartbeat(DEV_UART_PTR esp8266_uart) +{ + uint8_t checksum=0; + uint8_t send_data[] = { + 0xFF,0xFF, // + 0x00,0x00, // + 0x08, // + 0x00, // + 0x00,0x00, //flags + 0x00 // + }; + send_data[2] = (sizeof(send_data)-0x04)>>8; // + send_data[3] = (sizeof(send_data)-0x04)%256; // + send_data[5] = sn; // + // + for(uint8_t i=2;iuart_write(send_data,sizeof(send_data)); +} + + +void mcu2wifi_set_cfg_mode(DEV_UART_PTR esp8266_uart, uint8_t mode) +{ + uint8_t checksum=0; + uint8_t send_data[] = { + 0xFF,0xFF, // + 0x00,0x05, // + 0x09, // + 0x00, // + 0x00,0x00, //flags + 0x00, // + 0x00 // + }; + send_data[2] = (sizeof(send_data)-0x04)>>8; // + send_data[3] = (sizeof(send_data)-0x04)%256; // + send_data[5] = ++sn; // + + send_data[8] = mode; + // + for(uint8_t i=2;iuart_write(send_data,sizeof(send_data)); +} + + +void mcu2wifi_reset_wifi(DEV_UART_PTR esp8266_uart) +{ + uint8_t checksum=0; + uint8_t send_data[] = { + 0xFF,0xFF, // + 0x00,0x00, // + 0x0b, // + 0x00, // + 0x00,0x00, //flags + 0x00 // + }; + send_data[2] = (sizeof(send_data)-0x04)>>8; // + send_data[3] = (sizeof(send_data)-0x04)%256; // + send_data[5] = ++sn; // + // + for(uint8_t i=2;iuart_write(send_data,sizeof(send_data)); +} + + + +void mcu2wifi_wifi_statu(DEV_UART_PTR esp8266_uart,uint8_t *receive_data) +{ + uint8_t checksum=0; + uint8_t send_data[] = { + 0xFF,0xFF, // + 0x00,0x05, // + 0x0E, // + 0x00, // + 0x00,0x00, //flags + 0x00 // + }; + send_data[2] = (sizeof(send_data)-0x04)>>8; + send_data[3] = (sizeof(send_data)-0x04)%256; + send_data[5] = sn; + for(uint8_t i=2;iuart_write(send_data,sizeof(send_data)); + + uint8_t statu_bit0_7 = receive_data[9]; + uint8_t statu_bit8_15 = receive_data[8]; + + if((statu_bit0_7) %2 ) EMBARC_PRINTF("SoftAP mode is open\r\n"); + if((statu_bit0_7>>1 )%2 ) EMBARC_PRINTF("Station mode is open\r\n"); + if((statu_bit0_7>>2 )%2 ) EMBARC_PRINTF("OnBoarding mode is open\r\n"); + //LED 5 + if((statu_bit0_7>>3 )%2 ) + { + EMBARC_PRINTF("Binding mode is open\r\n"); + led = gpio_get_dev(LED_PORT); + led-> gpio_open(0x20); + led-> gpio_control(GPIO_CMD_SET_BIT_DIR_OUTPUT,(void *)(0x20)); + led-> gpio_write(0x00,0x20); + } + else + { + led = gpio_get_dev(LED_PORT); + led-> gpio_open(0x20); + led-> gpio_control(GPIO_CMD_SET_BIT_DIR_OUTPUT,(void *)(0x20)); + led-> gpio_write(0x20,0x20); + } + //LED 0~2 + if((statu_bit0_7>>4 )%2 ) + { + EMBARC_PRINTF("The WiFi module is connected to the wireless router,the RSSI = %d\r\n",(statu_bit8_15)%8); + led = gpio_get_dev(LED_PORT); + led-> gpio_open(0x07); + led-> gpio_control(GPIO_CMD_SET_BIT_DIR_OUTPUT,(void *)(0x07)); + led-> gpio_write(~((statu_bit8_15)%8),0x07); + } + else + { + led = gpio_get_dev(LED_PORT); + led-> gpio_open(0x07); + led-> gpio_control(GPIO_CMD_SET_BIT_DIR_OUTPUT,(void *)(0x07)); + led-> gpio_write(0x7,0x07); + } + //LED 3 + if((statu_bit0_7>>5 )%2 ) + { + EMBARC_PRINTF("The WiFi module is connected to the M2M server\r\n"); + led = gpio_get_dev(LED_PORT); + led-> gpio_open(0x08); + led-> gpio_control(GPIO_CMD_SET_BIT_DIR_OUTPUT,(void *)(0x08)); + led-> gpio_write(0x00,0x08); + + } + else + { + led = gpio_get_dev(LED_PORT); + led-> gpio_open(0x08); + led-> gpio_control(GPIO_CMD_SET_BIT_DIR_OUTPUT,(void *)(0x08)); + led-> gpio_write(0x08,0x08); + } + if((statu_bit8_15>>3)%2 ) + { + EMBARC_PRINTF("APP online\r\n"); + led = gpio_get_dev(LED_PORT); + led-> gpio_open(0x10); + led-> gpio_control(GPIO_CMD_SET_BIT_DIR_OUTPUT,(void *)(0x10)); + led-> gpio_write(0x00,0x10); + + } + else + { + led = gpio_get_dev(LED_PORT); + led-> gpio_open(0x10); + led-> gpio_control(GPIO_CMD_SET_BIT_DIR_OUTPUT,(void *)(0x10)); + led-> gpio_write(0x10,0x10); + } + +} + + +void mcu2wifi_receive_error(DEV_UART_PTR esp8266_uart) +{ + uint8_t checksum=0; + uint8_t send_data[] = { + 0xFF,0xFF, // + 0x00,0x00, // + 0x12, // + 0x00, // + 0x00,0x00, //flags + 0x00, // + 0x00 // + }; + send_data[2] = (sizeof(send_data)-0x04)>>8; // + send_data[3] = (sizeof(send_data)-0x04)%256; // + send_data[5] = sn; // + send_data[8] = 2; + // + for(uint8_t i=2;iuart_write(send_data,sizeof(send_data)); +} + + +void mcu2wifi_request_time(DEV_UART_PTR esp8266_uart) +{ + uint8_t checksum=0; + uint8_t send_data[] = { + 0xFF,0xFF, // + 0x00,0x05, // + 0x17, // + 0x00, // + 0x00,0x00, //flags + 0x00 // + }; + send_data[2] = (sizeof(send_data)-0x04)>>8; // + send_data[3] = (sizeof(send_data)-0x04)%256; // + send_data[5] = ++sn; // + // + for(uint8_t i=2;iuart_write(send_data,sizeof(send_data)); +} + + +void wifi2mcu_get_time(uint8_t *receive_data) +{ + year = receive_data[8]*256+receive_data[9]; + month = receive_data[10]; + day = receive_data[11]; + hour = receive_data[12]; + minute = receive_data[13]; + second = receive_data[14]; + + EMBARC_PRINTF("Now is %d/%d/%d %d:%d:%d \r\n",year,month,day,hour,minute,second); +} + + +void mcu2wifi_restart_wifi(DEV_UART_PTR esp8266_uart) +{ + uint8_t checksum=0; + uint8_t send_data[] = { + 0xFF,0xFF, // + 0x00,0x00, // + 0x29, // + 0x00, // + 0x00,0x00, //flags + 0x00 // + }; + send_data[2] = (sizeof(send_data)-0x04)>>8; // + send_data[3] = (sizeof(send_data)-0x04)%256; // + send_data[5] = ++sn; // + // + for(uint8_t i=2;iuart_write(send_data,sizeof(send_data)); +} + + +void mcu2wifi_request_into_binding(DEV_UART_PTR esp8266_uart) +{ + uint8_t checksum=0; + uint8_t send_data[] = { + 0xFF,0xFF, // + 0x00,0x00, // + 0x15, // + 0x00, // + 0x00,0x00, //flags + 0x00 // + }; + send_data[2] = (sizeof(send_data)-0x04)>>8; // + send_data[3] = (sizeof(send_data)-0x04)%256; // + send_data[5] = ++sn; // + // + for(uint8_t i=2;iuart_write(send_data,sizeof(send_data)); +} diff --git a/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Plant_subsystem/esp8266_gagent_common.h b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Plant_subsystem/esp8266_gagent_common.h new file mode 100644 index 00000000..ebd01d7f --- /dev/null +++ b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Plant_subsystem/esp8266_gagent_common.h @@ -0,0 +1,46 @@ +#ifndef _ESP8266_GAGENT_COMMON_H_ +#define _ESP8266_GAGENT_COMMON_H_ + +#include "dev_uart.h" + +uint8_t sn; +uint16_t year; +uint8_t month; +uint8_t day; +uint8_t hour; +uint8_t minute; +uint8_t second; + + +DEV_GPIO_PTR led; +#define LED_PORT DW_GPIO_PORT_B +#define LED_MASK 0x1ff +#define LED_DIR 0x1ff + + + +extern void mcu2wifi_product_info(DEV_UART_PTR esp8266_uart, uint8_t *product_key, uint8_t *product_secret); + +extern void mcu2wifi_heartbeat(DEV_UART_PTR esp8266_uart); + + + +extern void mcu2wifi_set_cfg_mode(DEV_UART_PTR esp8266_uart,uint8_t mode); + + +extern void mcu2wifi_reset_wifi(DEV_UART_PTR esp8266_uart); + + +extern void mcu2wifi_wifi_statu(DEV_UART_PTR esp8266_uart,uint8_t *receive_data); + +extern void mcu2wifi_receive_error(DEV_UART_PTR esp8266_uart); + +extern void mcu2wifi_request_time(DEV_UART_PTR esp8266_uart); + +extern void wifi2mcu_get_time(uint8_t *receive_data); + +extern void mcu2wifi_restart_wifi(DEV_UART_PTR esp8266_uart); + +extern void mcu2wifi_request_into_binding(DEV_UART_PTR esp8266_uart); + +#endif \ No newline at end of file diff --git a/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Plant_subsystem/esp8266_gagent_user.c b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Plant_subsystem/esp8266_gagent_user.c new file mode 100644 index 00000000..d0797c74 --- /dev/null +++ b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Plant_subsystem/esp8266_gagent_user.c @@ -0,0 +1,130 @@ +#include "embARC.h" +#include "embARC_debug.h" + +#include "dev_gpio.h" +#include "dev_uart.h" +#include "dev_iic.h" +#include "dev_spi.h" + +#include "board.h" +#include "esp8266_gagent_user.h" + + +void mcu2wifi_wifi_ctrl_dev(DEV_UART_PTR esp8266_uart,uint8_t *receive_data) +{ + // + uint8_t checksum=0; + uint8_t send_data[] = { + 0xFF,0xFF, // + 0x00,0x00, // + 0x04, // + 0x01, // + 0x00,0x00, //flags + 0x14 // + }; + uint8_t i; + send_data[2] = (sizeof(send_data)-0x04)>>8; // + send_data[3] = (sizeof(send_data)-0x04)%256;// + send_data[5] = sn; // + // + for(i=2;iuart_write(send_data,sizeof(send_data)); + + // + i=0; // + // + uint8_t attr_flags[ ATTR_FLAGS_BYTES ]; + for(i=0;i>8; // + send_data[3] = (sizeof(send_data)-0x04)%256; // + send_data[5] = sn; // + // + // Plant_Watering + send_data[9] = Plant_Watering; + // WaterThreshold + send_data[10] = WaterThreshold; + // Plant_Soil_Moisture + send_data[11] = Plant_Soil_Moisture; + // Plant_Env_Temp + send_data[12] = Plant_Env_Temp; + // Plant_Env_Humidity + send_data[13] = Plant_Env_Humidity; + // Add_water + send_data[14] = 10 - day % 10; + + // + for(uint8_t i=2;iuart_write(send_data,sizeof(send_data)); +} + + +void mcu2wifi_dev_report_status(DEV_UART_PTR esp8266_uart) +{ + uint8_t checksum=0; + uint8_t send_data[10 + DEV_STATUS_BYTES] = { + 0xFF,0xFF, // + 0x00,0x05, // + 0x05, // + 0x00, // + 0x00,0x00, //flags + 0x04, //action; + 0x00,0x00,0x00,0x00,0x00, 0x00,// + 0x14 // + }; + send_data[2] = (sizeof(send_data)-0x04)>>8; // + send_data[3] = (sizeof(send_data)-0x04)%256; // + send_data[5] = ++sn; // + // + // Plant_Watering + send_data[9] = Plant_Watering; + // WaterThreshold + send_data[10] = WaterThreshold; + // Plant_Soil_Moisture + send_data[11] = Plant_Soil_Moisture; + // Plant_Env_Temp + send_data[12] = Plant_Env_Temp; + // Plant_Env_Humidity + send_data[13] = Plant_Env_Humidity; + // Add_water + send_data[14] = day % 10; + + // + for(uint8_t i=2;iuart_write(send_data,sizeof(send_data)); +} \ No newline at end of file diff --git a/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Plant_subsystem/esp8266_gagent_user.h b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Plant_subsystem/esp8266_gagent_user.h new file mode 100644 index 00000000..7f3e7084 --- /dev/null +++ b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Plant_subsystem/esp8266_gagent_user.h @@ -0,0 +1,37 @@ +#ifndef _ESP8266_GAGENT_USER_H_ +#define _ESP8266_GAGENT_USER_H_ + +#include "dev_uart.h" + +#include "esp8266_gagent_common.h" + + + + +#define MAX_READ_CNT 32 + + +// +uint8_t Plant_Env_Temp; +uint8_t Plant_Env_Humidity; +uint8_t Plant_Soil_Moisture; +uint8_t Plant_Watering; +uint8_t WaterThreshold; + + + + + +#define ATTR_FLAGS_BYTES (1) + +#define ATTR_VALS_BYTES (2) +extern void mcu2wifi_wifi_ctrl_dev(DEV_UART_PTR esp8266_uart,uint8_t *receive_data); + + +#define DEV_STATUS_BYTES (6) +extern void mcu2wifi_wifi_read_dev(DEV_UART_PTR esp8266_uart); + + +extern void mcu2wifi_dev_report_status(DEV_UART_PTR esp8266_uart); + +#endif \ No newline at end of file diff --git a/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Plant_subsystem/main.c b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Plant_subsystem/main.c new file mode 100644 index 00000000..e5c58649 --- /dev/null +++ b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Plant_subsystem/main.c @@ -0,0 +1,279 @@ +#include "embARC.h" +#include "embARC_debug.h" +#include +#include "board.h" +#include "dev_iic.h" +#include "dev_gpio.h" + +#include "../../../board/emsk/drivers/mux/mux.h" +#include "../../../board/emsk/drivers/mux/mux_hal.h" + + +#include "esp8266_gagent_common.h" +#include "esp8266_gagent_user.h" + +#include "ad7991.h" +#include "DHT11.h" + + + + +DEV_UART_PTR esp8266_uart; +#define ESP8266_UART_ID DW_UART_0_ID +#define ESP8266_UART_BAUDRATE UART_BAUDRATE_9600 + +void esp8266_init(void) +{ + // set pmod mux and uart map + set_uart_map(0xe4); + esp8266_uart = uart_get_dev(ESP8266_UART_ID); + esp8266_uart->uart_close(); + if(esp8266_uart->uart_open(ESP8266_UART_BAUDRATE) == E_OPNED) + { + esp8266_uart->uart_control(UART_CMD_SET_BAUD, (void *)(ESP8266_UART_BAUDRATE)); + } +} + +uint32_t esp8266_read(uint8_t *buf, uint32_t cnt) +{ + if (esp8266_uart == NULL) return 0; + + uint32_t rd_avail; + esp8266_uart->uart_control(UART_CMD_GET_RXAVAIL, (void *)(&rd_avail)); + cnt = (cnt > rd_avail)?rd_avail:cnt; + if (cnt > 0) { + return esp8266_uart->uart_read(buf, cnt); + } + return cnt; +} + +uint32_t esp8266_write(uint8_t *buf, uint32_t cnt) +{ + if (esp8266_uart == NULL) return 0; + + return esp8266_uart->uart_write(buf, cnt); +} + + + + +DEV_GPIO_PTR dht11; +DHT11_DEF_PTR dht11_data; +#define DHT11_PORT DW_GPIO_PORT_C +#define DHT11_BIT (19) + +void DHT11_init(void) +{ + dht11 = gpio_get_dev(DHT11_PORT); + dht11->gpio_open(GPIO_DIR_OUTPUT); +} + +AD7991_DEF_PTR ad7991; +float ad7991_data[4]; +#define AD7991_ID (0) + +void ad7991_init(void) +{ + ad7991->dev_id = AD7991_ID; + ad7991->slvaddr = ADC_I2C_SLAVE_ADDRESS; + ad7991_adc_init(ad7991); +} + + + + +//J4-pin4 +DEV_GPIO_PTR relay; +#define RELAY_PORT DW_GPIO_PORT_C +#define RELAY_BIT (23) +#define RELAY_MASK (0x01<gpio_open(0x01 << RELAY_BIT); + relay->gpio_control(GPIO_CMD_SET_BIT_DIR_OUTPUT,(void *)(RELAY_MASK)); + relay->gpio_write(0x01 << RELAY_BIT, RELAY_MASK); +} + +void relay_close(void) +{ + relay = gpio_get_dev(RELAY_PORT); + relay->gpio_open(0x01 << RELAY_BIT); + relay->gpio_control(GPIO_CMD_SET_BIT_DIR_OUTPUT,(void *)(RELAY_MASK)); + relay->gpio_write(0x00 << RELAY_BIT, RELAY_MASK); +} + + + + +DEV_GPIO_PTR button; +#define BUTTON_MASK 0x07 +void gpio_keyL_isr(void *ptr) +{ + EMBARC_PRINTF("key 1: L\r\n"); + mcu2wifi_request_into_binding(esp8266_uart); +} +void gpio_keyR_isr(void *ptr) +{ + EMBARC_PRINTF("key 2: R\r\n"); + mcu2wifi_reset_wifi(esp8266_uart); +} +void gpio_keyA_isr(void *ptr) +{ + EMBARC_PRINTF("key 3: A\r\n"); +} +void button_init(void) +{ + DEV_GPIO_BIT_ISR bit_isr; + DEV_GPIO_INT_CFG int_cfg; + button = gpio_get_dev(DW_GPIO_PORT_A); + button ->gpio_open(BUTTON_MASK); + button ->gpio_control(GPIO_CMD_SET_BIT_DIR_INPUT, (void *)BUTTON_MASK); + int_cfg.int_bit_mask = BUTTON_MASK; + int_cfg.int_bit_type = GPIO_INT_BITS_EDGE_TRIG(BUTTON_MASK); + int_cfg.int_bit_polarity = GPIO_INT_BITS_POL_FALL_EDGE(BUTTON_MASK); + int_cfg.int_bit_debounce = GPIO_INT_BITS_ENA_DEBOUNCE(BUTTON_MASK); + button->gpio_control(GPIO_CMD_SET_BIT_INT_CFG, &int_cfg); + bit_isr.int_bit_ofs = 0; + bit_isr.int_bit_handler = gpio_keyL_isr; + button->gpio_control(GPIO_CMD_SET_BIT_ISR, (void *)(&bit_isr)); + bit_isr.int_bit_ofs = 1; + bit_isr.int_bit_handler = gpio_keyR_isr; + button->gpio_control(GPIO_CMD_SET_BIT_ISR, (void *)(&bit_isr)); + bit_isr.int_bit_ofs = 2; + bit_isr.int_bit_handler = gpio_keyA_isr; + button->gpio_control(GPIO_CMD_SET_BIT_ISR, (void *)(&bit_isr)); + + button->gpio_control(GPIO_CMD_ENA_BIT_INT, (void *)(0x7)); +} + + + + +int times=0; +void timer1_isr(void *ptr) +{ + + timer_int_clear(TIMER_1); + + + DHT11_Read_Data(dht11, DHT11_BIT, dht11_data); + Plant_Env_Temp = dht11_data->T_data; + Plant_Env_Humidity =dht11_data->RH_data; + EMBARC_PRINTF("Temperature is %d, Humidity is %d%%\r\n", dht11_data->T_data,dht11_data->RH_data); + + + if(times % 2 == 0) + { + // + mcu2wifi_request_time(esp8266_uart); + } + if(times % 6 == 0) + { + // + mcu2wifi_dev_report_status(esp8266_uart); + } + times++; +} +// +uint8_t *product_key = "f2bbf37ccea74a18a08fbfcc2e03818b"; +uint8_t *product_secret = "14380ffd6f2c44db87dd441d7d8dca8c"; + +int main(void) +{ + cpu_lock(); + board_init(); + + timer_stop(TIMER_1); /* Stop it first since it might be enabled before */ + int_handler_install(INTNO_TIMER1, timer1_isr); + int_enable(INTNO_TIMER1); + timer_start(TIMER_1, TIMER_CTRL_IE, 10 * BOARD_CPU_CLOCK); + + set_pmod_mux( + PM1_UR_UART_0|PM1_LR_GPIO_A | + PM2_I2C_HRI | + PM3_GPIO_AC | + PM4_GPIO_AC | + PM5_UR_GPIO_C | PM5_LR_GPIO_A | + PM6_UR_GPIO_C | PM6_LR_GPIO_A + ); + button_init(); + cpu_unlock(); + + esp8266_init(); + DHT11_init(); + ad7991_init(); + + + + uint32_t rd_cnt = 0; + uint8_t read_data[MAX_READ_CNT]; + + led = gpio_get_dev(LED_PORT); + led-> gpio_open(0x80); + led-> gpio_control(GPIO_CMD_SET_BIT_DIR_OUTPUT,(void *)(0x80)); + uint8_t led_count = 0; + + + while(1) + { + + + if((led_count)%10 == 0 ) led->gpio_write(0x80,0x80); + if((led_count)%20 == 0 ) led->gpio_write(0x00,0x80); + led_count++; + + board_delay_ms(100,0); + rd_cnt = esp8266_read(read_data,sizeof(read_data)); + if(rd_cnt) + { + for(uint8_t i=0;i WaterThreshold + 5) + relay_close(); + } + else relay_close(); + } + return E_SYS; +} \ No newline at end of file diff --git a/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Plant_subsystem/makefile b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Plant_subsystem/makefile new file mode 100644 index 00000000..969cd818 --- /dev/null +++ b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Plant_subsystem/makefile @@ -0,0 +1,70 @@ +## embARC application makefile template ## +### You can copy this file to your application folder +### and rename it to makefile. +## + +# Application name +APPL ?= Plant_subsystem + +# Optimization Level +# Please Refer to toolchain_xxx.mk for this option +OLEVEL ?= O2 + +## +# Current Board And Core +## +BOARD ?= emsk +BD_VER ?= 22 +CUR_CORE ?= arcem7d + +## +# select debugging jtag +## +JTAG ?= usb + +## +# Set toolchain +## +TOOLCHAIN ?= gnu + +## +# Uncomment following options +# if you want to set your own heap and stack size +# Default settings see options.mk +## +#HEAPSZ ?= 8192 +#STACKSZ ?= 8192 + +## +# Uncomment following options +# if you want to add your own library into link process +# For example: +# If you want link math lib for gnu toolchain, +# you need to set the option to -lm +## +#APPL_LIBS ?= + +# +# root dir of embARC +# +EMBARC_ROOT = ../../.. + +MID_SEL = common + +# application source dirs +APPL_CSRC_DIR = . +APPL_ASMSRC_DIR = . + +# application include dirs +APPL_INC_DIR = . + +# application defines +APPL_DEFINES = + +# include current project makefile +COMMON_COMPILE_PREREQUISITES += makefile + +### Options above must be added before include options.mk ### +# include key embARC build system makefile +override EMBARC_ROOT := $(strip $(subst \,/,$(EMBARC_ROOT))) +include $(EMBARC_ROOT)/options/options.mk diff --git a/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Room_Env_subsystem/DHT11.c b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Room_Env_subsystem/DHT11.c new file mode 100644 index 00000000..d2aa07f0 --- /dev/null +++ b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Room_Env_subsystem/DHT11.c @@ -0,0 +1,131 @@ +#include "embARC.h" +#include "embARC_debug.h" + +#include "dev_gpio.h" +#include "board.h" +#include "DHT11.h" + + + +static void delay_us( uint32_t us) +{ + uint32_t volatile x,y; + for(x=2;x>0;x--) + for(y=us;y>0;y--); + +} + + +static uint8_t DHT11_Read_Byte(DEV_GPIO_PTR DHT11_port, uint32_t DHT11_bit) +{ + + uint8_t level_cnt=0; + uint32_t read_data=0; + uint8_t uchartemp,ucharcomdata; + + + uint32_t DHT11_MASK = 0x01 << DHT11_bit; + DHT11_port->gpio_open(GPIO_DIR_OUTPUT); + + for(uint8_t i=0;i<8;i++) + { + level_cnt=2; + do{ + DHT11_port->gpio_control(GPIO_CMD_SET_BIT_DIR_INPUT,(void *)(DHT11_MASK)); + DHT11_port->gpio_read(&read_data,DHT11_MASK); + read_data &= DHT11_MASK; + }while((!read_data) && level_cnt++); + delay_us(30); + uchartemp = 0; + + DHT11_port->gpio_read(&read_data,DHT11_MASK); + read_data &= DHT11_MASK; + if(read_data) uchartemp = 1; + + level_cnt=2; + do{ + DHT11_port->gpio_control(GPIO_CMD_SET_BIT_DIR_INPUT,(void *)(DHT11_MASK)); + DHT11_port->gpio_read(&read_data,DHT11_MASK); + read_data &= DHT11_MASK; + }while((read_data) && level_cnt++); + if(level_cnt == 1) break; + ucharcomdata <<= 1; + ucharcomdata |= uchartemp; + } + return ucharcomdata; +} + +void DHT11_Read_Data(DEV_GPIO_PTR DHT11_port, uint32_t DHT11_bit, DHT11_DEF_PTR data) +{ + uint32_t read_data=0; + uint8_t level_cnt=0; + uint8_t RH_data_H_temp,RH_data_L_temp,T_data_H_temp,T_data_L_temp,checkdata_temp; + uint8_t RH_data_H,RH_data_L,T_data_H,T_data_L,checkdata; + + uint32_t DHT11_MASK = 0x01 << DHT11_bit; + DHT11_port->gpio_open(GPIO_DIR_OUTPUT); + uint32_t DHT11_WRITE_1 = 0x01 << DHT11_bit; + + // + DHT11_port->gpio_control(GPIO_CMD_SET_BIT_DIR_OUTPUT,(void *)(DHT11_MASK)); + // + DHT11_port->gpio_write(0x00,DHT11_MASK); + // + delay_us(20000); //delay > 18ms + // + DHT11_port->gpio_write(DHT11_WRITE_1,DHT11_MASK); + // + DHT11_port->gpio_control(GPIO_CMD_SET_BIT_DIR_INPUT,(void *)(DHT11_MASK)); + // + delay_us(30); + DHT11_port->gpio_read(&read_data,DHT11_MASK); + read_data &= DHT11_MASK; + if(!read_data) + { + level_cnt = 2; + do{ + DHT11_port->gpio_control(GPIO_CMD_SET_BIT_DIR_INPUT,(void *)(DHT11_MASK)); + DHT11_port->gpio_read(&read_data,DHT11_MASK); + read_data &= DHT11_MASK; + // delay_us(2); + }while((!read_data) && level_cnt++); + level_cnt = 2; + do{ + DHT11_port->gpio_control(GPIO_CMD_SET_BIT_DIR_INPUT,(void *)(DHT11_MASK)); + DHT11_port->gpio_read(&read_data,DHT11_MASK); + read_data &= DHT11_MASK; + // delay_us(2); + }while((read_data) && level_cnt++); + + RH_data_H_temp=DHT11_Read_Byte(DHT11_port, DHT11_bit); + RH_data_L_temp=DHT11_Read_Byte(DHT11_port, DHT11_bit); + T_data_H_temp =DHT11_Read_Byte(DHT11_port, DHT11_bit); + T_data_L_temp =DHT11_Read_Byte(DHT11_port, DHT11_bit); + checkdata_temp = DHT11_Read_Byte(DHT11_port, DHT11_bit); + // + DHT11_port->gpio_control(GPIO_CMD_SET_BIT_DIR_OUTPUT,(void *)(DHT11_MASK)); + // + DHT11_port->gpio_write(DHT11_WRITE_1,DHT11_MASK); + if(checkdata_temp == ( + RH_data_H_temp + RH_data_L_temp + + T_data_H_temp + T_data_L_temp + )) + { + RH_data_H = RH_data_H_temp; + RH_data_L = RH_data_L_temp; + T_data_H = T_data_H_temp; + T_data_L = T_data_L_temp; + checkdata = checkdata_temp; + } + data->RH_data = RH_data_H; + data->T_data = T_data_H; + } + else + { + data->RH_data = 0; + data->T_data = 0; + } + + DHT11_port->gpio_close(); +} + diff --git a/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Room_Env_subsystem/DHT11.h b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Room_Env_subsystem/DHT11.h new file mode 100644 index 00000000..f0c51a2c --- /dev/null +++ b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Room_Env_subsystem/DHT11.h @@ -0,0 +1,9 @@ +#include "dev_gpio.h" +/* DHT11 sensor object type*/ +typedef struct { + uint8_t RH_data; + uint8_t T_data; +} DHT11_DEF, * DHT11_DEF_PTR; + +extern void DHT11_Read_Data(DEV_GPIO_PTR DHT11_port, uint32_t DHT11_bit, DHT11_DEF_PTR data); + diff --git a/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Room_Env_subsystem/GP2Y1051.c b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Room_Env_subsystem/GP2Y1051.c new file mode 100644 index 00000000..76a9eece --- /dev/null +++ b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Room_Env_subsystem/GP2Y1051.c @@ -0,0 +1,82 @@ +/* embARC HAL */ +#include "embARC.h" +#include "embARC_debug.h" +#include +#include "board.h" +#include "dev_gpio.h" //GPIO API +#include "dev_uart.h" //UART API +#include "dev_iic.h" //I2C API +#include "dev_spi.h" //SPI API + +#include "GP2Y1051.h" + + + +static void GP2Y1051_init(void) +{ + GP2Y1051_uart = uart_get_dev(GP2Y1051_UART_ID); + GP2Y1051_uart-> uart_open(GP2Y1051_UART_BAUDRATE); + GP2Y1051_uart-> uart_control(UART_CMD_SET_BAUD, (void *)(GP2Y1051_UART_BAUDRATE)); + +} +static void delay_us( uint32_t us) +{ + uint32_t volatile x,y; + for(x=2;x>0;x--) + for(y=us;y>0;y--); + +} +static uint16_t GP2Y1051_rd_once(void) +{ + GP2Y1051_init(); + if (GP2Y1051_uart == NULL) return 0; + + uint32_t rd_avail; + uint8_t rd_data[20]; + uint8_t rd_cnt = 20; + uint32_t buf=0; + uint8_t buf_cnt=0; + uint8_t count = 0; + for(;(buf_cnt<2) | (count < 10); count++) + { + board_delay_ms(110,0); + GP2Y1051_uart->uart_control(UART_CMD_GET_RXAVAIL, (void *)(&rd_avail)); + rd_cnt = ( 20 > rd_avail)?rd_avail:rd_cnt; + if (rd_cnt > 0) { + GP2Y1051_uart->uart_read(rd_data, rd_cnt); + for(uint8_t i=0;i=0 write success, return bytes written + * \retval !E_OK write failed + */ +static int32_t _ad7991_reg_write(AD7991_DEF_PTR obj, uint8_t val) +{ + int32_t ercd = E_PAR; + DEV_IIC_PTR iic_obj = iic_get_dev(obj->dev_id); + + //dbg_printf(DBG_MORE_INFO, "[%s]%d: iic_obj 0x%x -> 0x%x\r\n", __FUNCTION__, __LINE__, iic_obj, *iic_obj); + EMSK_ADC_CHECK_EXP_NORTN(iic_obj!=NULL); + + /** make sure set the slave address */ + iic_obj->iic_control(IIC_CMD_MST_SET_TAR_ADDR, CONV2VOID(obj->slvaddr)); + ercd = iic_obj->iic_control(IIC_CMD_MST_SET_NEXT_COND, CONV2VOID(IIC_MODE_STOP)); + ercd = iic_obj->iic_write(&val, 1); + +error_exit: + return ercd; +} + +/** + * \brief read ad7991 register + * \param[in] obj ad7991 object + * \param[in] val pointer to buffer to read + * \param[in] channel_num active channel number + * \retval >=0 read success, return bytes read + * \retval dev_id); + + //dbg_printf(DBG_MORE_INFO, "[%s]%d: iic_obj 0x%x -> 0x%x\r\n", __FUNCTION__, __LINE__, iic_obj, *iic_obj); + EMSK_ADC_CHECK_EXP_NORTN(iic_obj!=NULL); + + /** make sure set the slave address */ + iic_obj->iic_control(IIC_CMD_MST_SET_TAR_ADDR, CONV2VOID(obj->slvaddr)); + /** read 2 bytes value */ + ercd = iic_obj->iic_control(IIC_CMD_MST_SET_NEXT_COND, CONV2VOID(IIC_MODE_STOP)); + ercd = iic_obj->iic_read(val, 2*channel_num); + +error_exit: + return ercd; +} + +/** + * \brief ad7991 init function + * \param[in] obj ad7991 object + * \retval E_OK init success + * \retval !E_OK init failed + */ +int32_t ad7991_adc_init(AD7991_DEF_PTR obj) +{ + int32_t ercd = E_OK; + DEV_IIC_PTR iic_obj = iic_get_dev(obj->dev_id); + + dbg_printf(DBG_MORE_INFO, "[%s]%d: iic_obj 0x%x -> 0x%x\r\n", __FUNCTION__, __LINE__, iic_obj, *iic_obj); + EMSK_ADC_CHECK_EXP_NORTN(iic_obj!=NULL); + + ercd = iic_obj->iic_open(DEV_MASTER_MODE, IIC_SPEED_HIGH); + if ((ercd == E_OK) || (ercd == E_OPNED)) { + ercd = iic_obj->iic_control(IIC_CMD_MST_SET_TAR_ADDR, CONV2VOID(obj->slvaddr)); + obj->ref_source = AD7991_REF_VDD; + obj->ref_voltage_mv = AD2_REF_VDD; + ad7991_ref_select(obj, AD7991_REF_VDD); + /*AD7991 default open 4 channel on power on, default config reg value is 0xF0*/ + ad7991_adc_channel(obj, 0xF0); + } + +error_exit: + return ercd; +} + +/** + * \brief ad7991 deinit + * \param[in] obj ad7991 object + * \retval E_OK init success + * \retval !E_OK init failed + */ +int32_t ad7991_adc_deinit(AD7991_DEF_PTR obj) +{ + int32_t ercd = E_OK; + DEV_IIC_PTR iic_obj = iic_get_dev(obj->dev_id); + ercd = iic_obj->iic_close(); + return ercd; +} + +/** + * \brief ad7991 reference voltage source selection + * \param[in] obj ad7991 object + * \param[in] source sensor working source, choose from enum sensor_op_mode + * \retval E_OK always return E_OK + */ +int32_t ad7991_ref_select(AD7991_DEF_PTR obj, int32_t source) +{ + + if(source == AD7991_REF_EXTERNAL) { + obj->ref_source = source; + obj->ref_voltage_mv = AD2_REF_EXTERNAL; + obj->reg_config |= AD7991_CONFIG_REF_SEL; + /* choose external reference uses VIN4 as REF, so there are only 3 channels available, disable CH3 here*/ + ad7991_adc_channel(obj, obj->reg_config & ~AD7991_CONFIG_CH3_EN); + } else { /*< AD7991_REF_VDD is default setting*/ + obj->ref_source = AD7991_REF_VDD; + obj->ref_voltage_mv = AD2_REF_VDD; + obj->reg_config &= ~AD7991_CONFIG_REF_SEL; + } + /** set working source bits: first clear bits to zero, then set bits to correct source */ + dbg_printf(DBG_MORE_INFO, "[%s]%d: after: reg 0x%x , source 0x%x\r\n", __FUNCTION__, __LINE__, obj->reg_config, obj->ref_source); + _ad7991_reg_write(obj, obj->reg_config); + return E_OK; +} + +/** + * \brief ad7991 adc channel setting + * \param[in] obj ad7991 object + * \param[in] channel channel selection + * \retval E_OK always return E_OK + */ +int32_t ad7991_adc_channel(AD7991_DEF_PTR obj, int8_t channel) +{ + uint8_t channel_num = 0; + if (channel & AD7991_CONFIG_CH0_EN) { + channel_num ++; + } + if (channel & AD7991_CONFIG_CH1_EN) { + channel_num ++; + } + if (channel & AD7991_CONFIG_CH2_EN) { + channel_num ++; + } + if ((obj->ref_source == AD7991_REF_VDD) && (channel & AD7991_CONFIG_CH3_EN)){ + channel_num ++; + } + /**clear channel bits in config reg and then set bits*/ + obj->reg_config = (obj->reg_config & 0x0F) | channel; + obj->channel_num = channel_num; + _ad7991_reg_write(obj, obj->reg_config); + return E_OK; +} + +/** + * \brief read 4 channel ADC converted value in 1.0 mV unit + * \param[in] obj ad7991 object + * \param[out] val need at least float [4] array to run, return latest avaiable convert data, array index corresponding to channel + * \retval E_OK read success + * \retval !E_OK read failed + */ +int32_t ad7991_adc_read(AD7991_DEF_PTR obj, float *val) +{ + int32_t ercd = E_OK; + uint8_t data[8] = {0}; + uint8_t index; + int16_t voltage; + int16_t actual_ref; + + EMSK_ADC_CHECK_EXP_NORTN(val!=NULL); + + ercd = _ad7991_reg_read(obj, data, obj->channel_num); + + if (ercd != obj->channel_num * 2) { + ercd = E_OBJ; + } else { + dbg_printf(DBG_MORE_INFO, "[%s]%d: ercd 0x%x, channel_num%d\r\n", __FUNCTION__, __LINE__, ercd, obj->channel_num); + ercd = E_OK; + dbg_printf(DBG_MORE_INFO, "[%s]%d: data[0] 0x%x; data[1] 0x%x\r\n", __FUNCTION__, __LINE__, data[0], data[1]); + dbg_printf(DBG_MORE_INFO, "[%s]%d: data[0] 0x%x; data[1] 0x%x\r\n", __FUNCTION__, __LINE__, data[2], data[3]); + dbg_printf(DBG_MORE_INFO, "[%s]%d: data[0] 0x%x; data[1] 0x%x\r\n", __FUNCTION__, __LINE__, data[4], data[5]); + dbg_printf(DBG_MORE_INFO, "[%s]%d: data[0] 0x%x; data[1] 0x%x\r\n", __FUNCTION__, __LINE__, data[6], data[7]); + if(obj->ref_source == AD7991_REF_VDD) { + actual_ref = AD2_REF_VDD; + } else { + actual_ref = AD2_REF_EXTERNAL; + } + /* use index to put value, so if there are more values than channel_num, new value will overwrite old value */ + voltage = ((int16_t)(data[0] &0x0F) << 8) + ((int16_t)data[1]); + index = data[0]>>4; + dbg_printf(DBG_LESS_INFO, "[%s]%d: voltage[0] %x\r\n", __FUNCTION__, __LINE__, voltage); + val[index] = voltage * actual_ref /4096.0; + voltage = ((int16_t)(data[2] &0x0F) << 8) + ((int16_t)data[3]); + index = data[2]>>4; + dbg_printf(DBG_LESS_INFO, "[%s]%d: voltage[1] %x\r\n", __FUNCTION__, __LINE__, voltage); + val[index] = voltage * actual_ref /4096.0; + voltage = ((int16_t)(data[4] &0x0F) << 8) + ((int16_t)data[5]); + index = data[4]>>4; + dbg_printf(DBG_LESS_INFO, "[%s]%d: voltage[2] %x\r\n", __FUNCTION__, __LINE__, voltage); + val[index] = voltage * actual_ref /4096.0; + voltage = ((int16_t)(data[6] &0x0F) << 8) + ((int16_t)data[7]); + index = data[6]>>4; + dbg_printf(DBG_LESS_INFO, "[%s]%d: voltage[3] %x\r\n\r\n", __FUNCTION__, __LINE__, voltage); + val[index] = voltage * actual_ref /4096.0; + } + +error_exit: + return ercd; +} + diff --git a/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Room_Env_subsystem/ad7991.h b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Room_Env_subsystem/ad7991.h new file mode 100644 index 00000000..8c51eaae --- /dev/null +++ b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Room_Env_subsystem/ad7991.h @@ -0,0 +1,105 @@ +/* ------------------------------------------ + * Copyright (c) 2017, Synopsys, Inc. All rights reserved. + + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + + * 1) Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + + * 2) 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. + + * 3) Neither the name of the Synopsys, Inc., 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. + * +--------------------------------------------- */ +/** + * \file + * \brief header file of emsk pmod AD2 ADC driver + */ + +#ifndef _AD7991_H_ +#define _AD7991_H_ + +#include "dev_iic.h" + +/** + * PMOD AD2 board (AD7991) has to choose its voltage reference form power supply, or external on board 2.048V Voltage Reference (ADR380) or VIN4 (Ch3) + * please note that when choose external reference, only 3 channels are converting + */ +#define AD2_JUMPER_REF 0 +#define AD2_JUMPER_VIN4 1 +/* switch for AD2 board Jumper, choose from AD2_JUMPER_REF or AD2_JUMPER_VIN4*/ +#define AD2_REF_JUMPER AD2_JUMPER_REF + +#define AD2_REF_VDD 3300 /*< power supply voltage is 3300mV*/ +#if AD2_REF_JUMPER == AD2_JUMPER_REF +#define AD2_REF_EXTERNAL 2048 /*< The ADR380 voltage reference on AD2 board has 2048mV output*/ +#else +#define AD2_REF_EXTERNAL 1000 /*< choose VIN4 (CH3) as reference, use 1000 to return a fraction voltage value of CH3 */ +#endif + +#define AD7991_CONFIG_CH0_EN (1 << 4) +#define AD7991_CONFIG_CH1_EN (1 << 5) +#define AD7991_CONFIG_CH2_EN (1 << 6) +#define AD7991_CONFIG_CH3_EN (1 << 7) +/** + * I2C address : Pmod AD2 (AD7991) uses AD7991-0, has only one address which is 0x28 + */ +#define AD7991_IIC_ADDRESS 0x28 + +#define ADC_I2C_SLAVE_ADDRESS AD7991_IIC_ADDRESS + +#ifdef __cplusplus +extern "C" { +#endif + +/** options for reference source parameter of ad7991_ref_select() */ +typedef enum ad7991_ref_source { + AD7991_REF_VDD, + AD7991_REF_EXTERNAL +} AD7991_REF_SOURCE; + +/** AD7991 object type */ +typedef struct { + uint32_t dev_id; + uint32_t slvaddr; + + int8_t channel_num; + int8_t reg_config; + int32_t ref_source; + int32_t ref_voltage_mv; +} AD7991_DEF, *AD7991_DEF_PTR; + +#define AD7991_DEFINE(NAME, DEVICE_ID, SLAVE_ADDRESS) \ + AD7991_DEF __ ## NAME = { \ + .dev_id = DEVICE_ID, \ + .slvaddr = SLAVE_ADDRESS, \ + }; \ + AD7991_DEF_PTR NAME = &__ ## NAME + +extern int32_t ad7991_adc_init(AD7991_DEF_PTR obj); +extern int32_t ad7991_adc_deinit(AD7991_DEF_PTR obj); +extern int32_t ad7991_ref_select(AD7991_DEF_PTR obj, int32_t source); +extern int32_t ad7991_adc_channel(AD7991_DEF_PTR obj, int8_t channel); +extern int32_t ad7991_adc_read(AD7991_DEF_PTR obj, float *val); + +#ifdef __cplusplus +} +#endif + +#endif /* _AD7991_H_ */ diff --git a/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Room_Env_subsystem/esp8266_gagent_common.c b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Room_Env_subsystem/esp8266_gagent_common.c new file mode 100644 index 00000000..0d1bbf23 --- /dev/null +++ b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Room_Env_subsystem/esp8266_gagent_common.c @@ -0,0 +1,324 @@ +#include "embARC.h" +#include "embARC_debug.h" + +#include "dev_gpio.h" +#include "dev_uart.h" +#include "dev_iic.h" +#include "dev_spi.h" +#include "board.h" + +#include "esp8266_gagent_common.h" + + +void mcu2wifi_product_info(DEV_UART_PTR esp8266_uart, uint8_t *product_key, uint8_t *product_secret) +{ + uint8_t checksum=0; + int i=0; // + uint8_t send_data[] = { + 0xFF,0xFF, // + 0x00,0x00, // + 0x02, // + 0x00, // + 0x00,0x00, //flags + '0','0','0','0','0','0','0','4', // + '0','0','0','0','0','0','0','2', // + '0','0','0','0','0','0','0','0', // + '0','0','0','0','0','0','0','0', // + + + '0','0','0','0','0','0','0','0', // + '0','0','0','0','0','0','0','0', + '0','0','0','0','0','0','0','0', + '0','0','0','0','0','0','0','0', + + 0x01,0x2C, // + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, // + + '0','0','0','0','0','0','0','0', // + '0','0','0','0','0','0','0','0', + '0','0','0','0','0','0','0','0', + '0','0','0','0','0','0','0','0', + 0xEE //校验和 + }; + send_data[2] = (sizeof(send_data)-0x04)>>8; // + send_data[3] = (sizeof(send_data)-0x04)%256; // + send_data[5] = sn; // + for(i=0; i<32; i++) + { + send_data[40+i] = product_key[i]; + send_data[82+i] = product_secret[i]; + } + for(i=0; i<32; i++) EMBARC_PRINTF("%c",send_data[40+i]); EMBARC_PRINTF("\r\n"); + for(i=0; i<32; i++) EMBARC_PRINTF("%c",send_data[82+i]); EMBARC_PRINTF("\r\n"); + // + for(uint8_t i=2;i< sizeof(send_data)-1;i++) checksum = checksum + send_data[i]; + send_data[sizeof(send_data)-1] =checksum; + // + esp8266_uart->uart_write(send_data,sizeof(send_data)); +} + + +void mcu2wifi_heartbeat(DEV_UART_PTR esp8266_uart) +{ + uint8_t checksum=0; + uint8_t send_data[] = { + 0xFF,0xFF, // + 0x00,0x00, // + 0x08, // + 0x00, // + 0x00,0x00, //flags + 0x00 // + }; + send_data[2] = (sizeof(send_data)-0x04)>>8; // + send_data[3] = (sizeof(send_data)-0x04)%256; // + send_data[5] = sn; // + // + for(uint8_t i=2;iuart_write(send_data,sizeof(send_data)); +} + + +void mcu2wifi_set_cfg_mode(DEV_UART_PTR esp8266_uart, uint8_t mode) +{ + uint8_t checksum=0; + uint8_t send_data[] = { + 0xFF,0xFF, // + 0x00,0x05, // + 0x09, // + 0x00, // + 0x00,0x00, //flags + 0x00, // + 0x00 // + }; + send_data[2] = (sizeof(send_data)-0x04)>>8; // + send_data[3] = (sizeof(send_data)-0x04)%256; // + send_data[5] = ++sn; // + + send_data[8] = mode; + // + for(uint8_t i=2;iuart_write(send_data,sizeof(send_data)); +} + +void mcu2wifi_reset_wifi(DEV_UART_PTR esp8266_uart) +{ + uint8_t checksum=0; + uint8_t send_data[] = { + 0xFF,0xFF, // + 0x00,0x00, // + 0x0b, // + 0x00, // + 0x00,0x00, //flags + 0x00 // + }; + send_data[2] = (sizeof(send_data)-0x04)>>8; // + send_data[3] = (sizeof(send_data)-0x04)%256; // + send_data[5] = ++sn; // + // + for(uint8_t i=2;iuart_write(send_data,sizeof(send_data)); +} + + + +void mcu2wifi_wifi_statu(DEV_UART_PTR esp8266_uart,uint8_t *receive_data) +{ + uint8_t checksum=0; + uint8_t send_data[] = { + 0xFF,0xFF, // + 0x00,0x05, // + 0x0E, // + 0x00, // + 0x00,0x00, //flags + 0x00 // + }; + send_data[2] = (sizeof(send_data)-0x04)>>8; + send_data[3] = (sizeof(send_data)-0x04)%256; + send_data[5] = sn; + for(uint8_t i=2;iuart_write(send_data,sizeof(send_data)); + + uint8_t statu_bit0_7 = receive_data[9]; + uint8_t statu_bit8_15 = receive_data[8]; + + if((statu_bit0_7) %2 ) EMBARC_PRINTF("SoftAP mode is open\r\n"); + if((statu_bit0_7>>1 )%2 ) EMBARC_PRINTF("Station mode is open\r\n"); + if((statu_bit0_7>>2 )%2 ) EMBARC_PRINTF("OnBoarding mode is open\r\n"); + //LED 5 + if((statu_bit0_7>>3 )%2 ) + { + EMBARC_PRINTF("Binding mode is open\r\n"); + led = gpio_get_dev(LED_PORT); + led-> gpio_open(0x20); + led-> gpio_control(GPIO_CMD_SET_BIT_DIR_OUTPUT,(void *)(0x20)); + led-> gpio_write(0x00,0x20); + } + else + { + led = gpio_get_dev(LED_PORT); + led-> gpio_open(0x20); + led-> gpio_control(GPIO_CMD_SET_BIT_DIR_OUTPUT,(void *)(0x20)); + led-> gpio_write(0x20,0x20); + } + //LED 0~2 + if((statu_bit0_7>>4 )%2 ) + { + EMBARC_PRINTF("The WiFi module is connected to the wireless router,the RSSI = %d\r\n",(statu_bit8_15)%8); + led = gpio_get_dev(LED_PORT); + led-> gpio_open(0x07); + led-> gpio_control(GPIO_CMD_SET_BIT_DIR_OUTPUT,(void *)(0x07)); + led-> gpio_write(~((statu_bit8_15)%8),0x07); + } + else + { + led = gpio_get_dev(LED_PORT); + led-> gpio_open(0x07); + led-> gpio_control(GPIO_CMD_SET_BIT_DIR_OUTPUT,(void *)(0x07)); + led-> gpio_write(0x7,0x07); + } + //LED 3 + if((statu_bit0_7>>5 )%2 ) + { + EMBARC_PRINTF("The WiFi module is connected to the M2M server\r\n"); + led = gpio_get_dev(LED_PORT); + led-> gpio_open(0x08); + led-> gpio_control(GPIO_CMD_SET_BIT_DIR_OUTPUT,(void *)(0x08)); + led-> gpio_write(0x00,0x08); + + } + else + { + led = gpio_get_dev(LED_PORT); + led-> gpio_open(0x08); + led-> gpio_control(GPIO_CMD_SET_BIT_DIR_OUTPUT,(void *)(0x08)); + led-> gpio_write(0x08,0x08); + } + //LED 4 + if((statu_bit8_15>>3)%2 ) + { + EMBARC_PRINTF("APP online\r\n"); + led = gpio_get_dev(LED_PORT); + led-> gpio_open(0x10); + led-> gpio_control(GPIO_CMD_SET_BIT_DIR_OUTPUT,(void *)(0x10)); + led-> gpio_write(0x00,0x10); + + } + else + { + led = gpio_get_dev(LED_PORT); + led-> gpio_open(0x10); + led-> gpio_control(GPIO_CMD_SET_BIT_DIR_OUTPUT,(void *)(0x10)); + led-> gpio_write(0x10,0x10); + } + +} + + +void mcu2wifi_receive_error(DEV_UART_PTR esp8266_uart) +{ + uint8_t checksum=0; + uint8_t send_data[] = { + 0xFF,0xFF, // + 0x00,0x00, // + 0x12, // + 0x00, // + 0x00,0x00, //flags + 0x00, // + 0x00 // + }; + send_data[2] = (sizeof(send_data)-0x04)>>8; // + send_data[3] = (sizeof(send_data)-0x04)%256; // + send_data[5] = sn; // + send_data[8] = 2; + // + for(uint8_t i=2;iuart_write(send_data,sizeof(send_data)); +} + + +void mcu2wifi_request_time(DEV_UART_PTR esp8266_uart) +{ + uint8_t checksum=0; + uint8_t send_data[] = { + 0xFF,0xFF, // + 0x00,0x05, // + 0x17, // + 0x00, // + 0x00,0x00, //flags + 0x00 // + }; + send_data[2] = (sizeof(send_data)-0x04)>>8; // + send_data[3] = (sizeof(send_data)-0x04)%256; // + send_data[5] = ++sn; // + // + for(uint8_t i=2;iuart_write(send_data,sizeof(send_data)); +} + + +void wifi2mcu_get_time(uint8_t *receive_data) +{ + year = receive_data[8]*256+receive_data[9]; + month = receive_data[10]; + day = receive_data[11]; + hour = receive_data[12]; + minute = receive_data[13]; + second = receive_data[14]; + + EMBARC_PRINTF("Now is %d/%d/%d %d:%d:%d \r\n",year,month,day,hour,minute,second); +} + + +void mcu2wifi_restart_wifi(DEV_UART_PTR esp8266_uart) +{ + uint8_t checksum=0; + uint8_t send_data[] = { + 0xFF,0xFF, // + 0x00,0x00, // + 0x29, // + 0x00, // + 0x00,0x00, //flags + 0x00 // + }; + send_data[2] = (sizeof(send_data)-0x04)>>8; // + send_data[3] = (sizeof(send_data)-0x04)%256; // + send_data[5] = ++sn; // + // + for(uint8_t i=2;iuart_write(send_data,sizeof(send_data)); +} + + +void mcu2wifi_request_into_binding(DEV_UART_PTR esp8266_uart) +{ + uint8_t checksum=0; + uint8_t send_data[] = { + 0xFF,0xFF, // + 0x00,0x00, // + 0x15, // + 0x00, // + 0x00,0x00, //flags + 0x00 // + }; + send_data[2] = (sizeof(send_data)-0x04)>>8; // + send_data[3] = (sizeof(send_data)-0x04)%256; // + send_data[5] = ++sn; // + // + for(uint8_t i=2;iuart_write(send_data,sizeof(send_data)); +} diff --git a/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Room_Env_subsystem/esp8266_gagent_common.h b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Room_Env_subsystem/esp8266_gagent_common.h new file mode 100644 index 00000000..b9da43c6 --- /dev/null +++ b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Room_Env_subsystem/esp8266_gagent_common.h @@ -0,0 +1,48 @@ +#ifndef _ESP8266_GAGENT_COMMON_H_ +#define _ESP8266_GAGENT_COMMON_H_ + +#include "dev_uart.h" + +uint8_t sn; +uint16_t year; +uint8_t month; +uint8_t day; +uint8_t hour; +uint8_t minute; +uint8_t second; + + +DEV_GPIO_PTR led; +#define LED_PORT DW_GPIO_PORT_B +#define LED_MASK 0x1ff +#define LED_DIR 0x1ff + + +extern void mcu2wifi_product_info(DEV_UART_PTR esp8266_uart, uint8_t *product_key, uint8_t *product_secret); + +extern void mcu2wifi_heartbeat(DEV_UART_PTR esp8266_uart); + + + +extern void mcu2wifi_set_cfg_mode(DEV_UART_PTR esp8266_uart,uint8_t mode); + + + +extern void mcu2wifi_reset_wifi(DEV_UART_PTR esp8266_uart); + + + +extern void mcu2wifi_wifi_statu(DEV_UART_PTR esp8266_uart,uint8_t *receive_data); + + +extern void mcu2wifi_receive_error(DEV_UART_PTR esp8266_uart); + +extern void mcu2wifi_request_time(DEV_UART_PTR esp8266_uart); + +extern void wifi2mcu_get_time(uint8_t *receive_data); + +extern void mcu2wifi_restart_wifi(DEV_UART_PTR esp8266_uart); + +extern void mcu2wifi_request_into_binding(DEV_UART_PTR esp8266_uart); + +#endif \ No newline at end of file diff --git a/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Room_Env_subsystem/esp8266_gagent_user.c b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Room_Env_subsystem/esp8266_gagent_user.c new file mode 100644 index 00000000..bea3e81e --- /dev/null +++ b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Room_Env_subsystem/esp8266_gagent_user.c @@ -0,0 +1,174 @@ +#include "embARC.h" +#include "embARC_debug.h" + +#include "dev_gpio.h" +#include "dev_uart.h" +#include "dev_iic.h" +#include "dev_spi.h" + +#include "board.h" +#include "step_motor.h" +#include "esp8266_gagent_user.h" + + + + + + + +void mcu2wifi_wifi_ctrl_dev(DEV_UART_PTR esp8266_uart,uint8_t *receive_data) +{ + // + uint8_t checksum=0; + uint8_t send_data[] = { + 0xFF,0xFF, // + 0x00,0x00, // + 0x04, // + 0x01, // + 0x00,0x00, //flags + 0x14 // + }; + uint8_t i; + send_data[2] = (sizeof(send_data)-0x04)>>8; // + send_data[3] = (sizeof(send_data)-0x04)%256;// + send_data[5] = sn; // + // + for(i=2;iuart_write(send_data,sizeof(send_data)); + + // + i=0; // + // + uint8_t attr_flags[ ATTR_FLAGS_BYTES ]; + for(i=0;i>8; // + send_data[3] = (sizeof(send_data)-0x04)%256; // + send_data[5] = sn; // + // + // Leave_home + if(Leave_home) send_data[9] = send_data[9] | 0x01; + else send_data[9] = send_data[9] & 0xFE; + //Room_Lamp + if(Room_Lamp == 0) send_data[9] = send_data[9] & 0xF9; + if(Room_Lamp == 1) {send_data[9] = send_data[9] & 0xFB; send_data[9] = send_data[9] | 0x02;} + if(Room_Lamp == 2) {send_data[9] = send_data[9] | 0x04; send_data[9] = send_data[9] & 0xFD;} + //Fan_switch + if(Fan_switch == 0) send_data[9] = send_data[9] & 0xE7; + if(Fan_switch == 1) {send_data[9] = send_data[9] & 0xEF; send_data[9] = send_data[9] | 0x08;} + if(Fan_switch == 2) {send_data[9] = send_data[9] | 0x10; send_data[9] = send_data[9] & 0xF7;} + + + // Room_Env_Tepm + send_data[10] = Room_Env_Tepm; + // Room_Env_Humidity + send_data[11] = Room_Env_Humidity; + // Room_Smoke + send_data[12] = Room_Smoke; + // Room_Light + send_data[13] = Room_Light; + // Room_PM25 + send_data[14] = (uint8_t)(Room_PM25 / 256); + send_data[15] = (uint8_t)(Room_PM25 % 256); + + // Body_infrared + if(Body_infrared) send_data[16] = send_data[16] | 0x01; + else send_data[16] = send_data[16] & 0xFE; + + // + for(uint8_t i=2;iuart_write(send_data,sizeof(send_data)); +} + + +void mcu2wifi_dev_report_status(DEV_UART_PTR esp8266_uart) +{ + uint8_t checksum=0; + uint8_t send_data[] = { + 0xFF,0xFF, // + 0x00,0x05, // + 0x05, // + 0x00, // + 0x00,0x00, //flags + 0x04, //action; + 0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,// + 0x14 // + }; + send_data[2] = (sizeof(send_data)-0x04)>>8; // + send_data[3] = (sizeof(send_data)-0x04)%256; // + send_data[5] = ++sn; // + // + // Leave_home + if(Leave_home) send_data[9] = send_data[9] | 0x01; + else send_data[9] = send_data[9] & 0xFE; + //Room_Lamp + if(Room_Lamp == 0) send_data[9] = send_data[9] & 0xF9; + if(Room_Lamp == 1) {send_data[9] = send_data[9] & 0xFB; send_data[9] = send_data[9] | 0x02;} + if(Room_Lamp == 2) {send_data[9] = send_data[9] | 0x04; send_data[9] = send_data[9] & 0xFD;} + //Fan_switch + if(Fan_switch == 0) send_data[9] = send_data[9] & 0xE7; + if(Fan_switch == 1) {send_data[9] = send_data[9] & 0xEF; send_data[9] = send_data[9] | 0x08;} + if(Fan_switch == 2) {send_data[9] = send_data[9] | 0x10; send_data[9] = send_data[9] & 0xF7;} + + // Room_Env_Tepm + send_data[10] = Room_Env_Tepm; + // Room_Env_Humidity + send_data[11] = Room_Env_Humidity; + // Room_Smoke + send_data[12] = Room_Smoke; + // Room_Light + send_data[13] = Room_Light; + // Room_PM25 + send_data[14] = (uint8_t)(Room_PM25 / 256); + send_data[15] = (uint8_t)(Room_PM25 % 256); + + // Body_infrared + if(Body_infrared) send_data[16] = send_data[16] | 0x01; + else send_data[16] = send_data[16] & 0xFE; + + // + for(uint8_t i=2;iuart_write(send_data,sizeof(send_data)); +} \ No newline at end of file diff --git a/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Room_Env_subsystem/esp8266_gagent_user.h b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Room_Env_subsystem/esp8266_gagent_user.h new file mode 100644 index 00000000..b27778a3 --- /dev/null +++ b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Room_Env_subsystem/esp8266_gagent_user.h @@ -0,0 +1,46 @@ +#ifndef _ESP8266_GAGENT_USER_H_ +#define _ESP8266_GAGENT_USER_H_ + +#include "dev_uart.h" + +#include "esp8266_gagent_common.h" + +DEV_GPIO_PTR led; +#define LED_PORT DW_GPIO_PORT_B +#define LED_MASK 0x1ff +#define LED_DIR 0x1ff + + +#define MAX_READ_CNT 32 + + +// +uint8_t Room_Env_Tepm; +uint8_t Room_Env_Humidity; +uint8_t Room_Smoke; +uint8_t Room_Light; +// uint8_t Room_curtain; +uint8_t Room_Lamp; +uint16_t Room_PM25; +uint8_t Body_infrared; +uint8_t Leave_home; +uint8_t Fan_switch; + + + + + + +#define ATTR_FLAGS_BYTES (1) +// +#define ATTR_VALS_BYTES (1) +extern void mcu2wifi_wifi_ctrl_dev(DEV_UART_PTR esp8266_uart,uint8_t *receive_data); + + +#define DEV_STATUS_BYTES (8) +extern void mcu2wifi_wifi_read_dev(DEV_UART_PTR esp8266_uart); + + +extern void mcu2wifi_dev_report_status(DEV_UART_PTR esp8266_uart); + +#endif \ No newline at end of file diff --git a/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Room_Env_subsystem/main.c b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Room_Env_subsystem/main.c new file mode 100644 index 00000000..207c3c00 --- /dev/null +++ b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Room_Env_subsystem/main.c @@ -0,0 +1,358 @@ +#include "embARC.h" +#include "embARC_debug.h" +#include +#include "board.h" +#include "dev_iic.h" +#include "dev_gpio.h" + +#include "../../../board/emsk/drivers/mux/mux.h" +#include "../../../board/emsk/drivers/mux/mux_hal.h" + +#include "ad7991.h" +// #include "step_motor.h" +#include "esp8266_gagent_common.h" +#include "esp8266_gagent_user.h" + +#include "DHT11.h" +#include "GP2Y1051.h" + + +// +DEV_UART_PTR esp8266_uart; +#define ESP8266_UART_ID DW_UART_0_ID +#define ESP8266_UART_BAUDRATE UART_BAUDRATE_9600 + +void esp8266_init(void) +{ + // set pmod mux and uart map + set_uart_map(0xe4); + + esp8266_uart = uart_get_dev(ESP8266_UART_ID); + esp8266_uart->uart_close(); + if(esp8266_uart->uart_open(ESP8266_UART_BAUDRATE) == E_OPNED) + { + esp8266_uart->uart_control(UART_CMD_SET_BAUD, (void *)(ESP8266_UART_BAUDRATE)); + } +} +uint32_t esp8266_read(uint8_t *buf, uint32_t cnt) +{ + if (esp8266_uart == NULL) return 0; + + uint32_t rd_avail; + esp8266_uart->uart_control(UART_CMD_GET_RXAVAIL, (void *)(&rd_avail)); + cnt = (cnt > rd_avail)?rd_avail:cnt; + if (cnt > 0) { + return esp8266_uart->uart_read(buf, cnt); + } + return cnt; +} +uint32_t esp8266_write(uint8_t *buf, uint32_t cnt) +{ + if (esp8266_uart == NULL) return 0; + + return esp8266_uart->uart_write(buf, cnt); +} + +DEV_GPIO_PTR dht11; +DHT11_DEF_PTR dht11_data; +#define DHT11_PORT DW_GPIO_PORT_C +#define DHT11_BIT (19) +void DHT11_init(void) +{ + dht11 = gpio_get_dev(DHT11_PORT); + dht11->gpio_open(GPIO_DIR_OUTPUT); +} + +AD7991_DEF_PTR ad7991; +float ad7991_data[4]; +#define AD7991_ID (0) +void ad7991_init(void) +{ + ad7991->dev_id = AD7991_ID; + ad7991->slvaddr = ADC_I2C_SLAVE_ADDRESS; + ad7991_adc_init(ad7991); +} + +DEV_GPIO_PTR button; +#define BUTTON_MASK 0x07 +void gpio_keyL_isr(void *ptr) +{ + EMBARC_PRINTF("key 1: L\r\n"); + mcu2wifi_request_into_binding(esp8266_uart); +} +void gpio_keyR_isr(void *ptr) +{ + EMBARC_PRINTF("key 2: R\r\n"); + mcu2wifi_reset_wifi(esp8266_uart); +} +void gpio_keyA_isr(void *ptr) +{ + EMBARC_PRINTF("key 3: A\r\n"); +} +void button_init(void) +{ + DEV_GPIO_BIT_ISR bit_isr; + DEV_GPIO_INT_CFG int_cfg; + button = gpio_get_dev(DW_GPIO_PORT_A); + button ->gpio_open(BUTTON_MASK); + button ->gpio_control(GPIO_CMD_SET_BIT_DIR_INPUT, (void *)BUTTON_MASK); + int_cfg.int_bit_mask = BUTTON_MASK; + int_cfg.int_bit_type = GPIO_INT_BITS_EDGE_TRIG(BUTTON_MASK); + int_cfg.int_bit_polarity = GPIO_INT_BITS_POL_FALL_EDGE(BUTTON_MASK); + int_cfg.int_bit_debounce = GPIO_INT_BITS_ENA_DEBOUNCE(BUTTON_MASK); + button->gpio_control(GPIO_CMD_SET_BIT_INT_CFG, &int_cfg); + bit_isr.int_bit_ofs = 0; + bit_isr.int_bit_handler = gpio_keyL_isr; + button->gpio_control(GPIO_CMD_SET_BIT_ISR, (void *)(&bit_isr)); + bit_isr.int_bit_ofs = 1; + bit_isr.int_bit_handler = gpio_keyR_isr; + button->gpio_control(GPIO_CMD_SET_BIT_ISR, (void *)(&bit_isr)); + bit_isr.int_bit_ofs = 2; + bit_isr.int_bit_handler = gpio_keyA_isr; + button->gpio_control(GPIO_CMD_SET_BIT_ISR, (void *)(&bit_isr)); + + button->gpio_control(GPIO_CMD_ENA_BIT_INT, (void *)(0x7)); +} + +DEV_GPIO_PTR body_ir; +#define BODY_PORT DW_GPIO_PORT_A +#define BODY_BIT (19) +#define BODY_MASK (0x01< gpio_open(GPIO_DIR_INPUT); + body_ir-> gpio_control(GPIO_CMD_SET_BIT_DIR_INPUT,(void *)(BODY_MASK)); +} +uint32_t body_ir_read(void) +{ + uint32_t read_data; + body_ir-> gpio_read(&read_data,BODY_MASK); + return read_data; +} + +DEV_GPIO_PTR fan; +#define FAN_PORT DW_GPIO_PORT_C +#define FAN_BIT (31) +#define FAN_MASK (0x01 << FAN_BIT) + +void fan_open(void) +{ + fan = gpio_get_dev(FAN_PORT); + fan->gpio_open(FAN_MASK); + fan->gpio_control(GPIO_CMD_SET_BIT_DIR_OUTPUT,(void *)(FAN_MASK)); + fan->gpio_write(0x01<gpio_open(FAN_MASK); + fan->gpio_control(GPIO_CMD_SET_BIT_DIR_OUTPUT,(void *)(FAN_MASK)); + fan->gpio_write(0x00, FAN_MASK); +} + +DEV_GPIO_PTR buzzer; +#define BUZZER_PORT DW_GPIO_PORT_A +#define BUZZER_BIT (31) +#define BUZZER_MASK (0x01 << FAN_BIT) + +void buzzer_open(void) +{ + buzzer = gpio_get_dev(BUZZER_PORT); + buzzer->gpio_open(BUZZER_MASK); + buzzer->gpio_control(GPIO_CMD_SET_BIT_DIR_OUTPUT,(void *)(BUZZER_MASK)); + buzzer->gpio_write(0x01<gpio_open(BUZZER_MASK); + buzzer->gpio_control(GPIO_CMD_SET_BIT_DIR_OUTPUT,(void *)(BUZZER_MASK)); + buzzer->gpio_write(0x00, BUZZER_MASK); +} + +DEV_GPIO_PTR roomLamp; +#define roomLamp_PORT DW_GPIO_PORT_C +#define roomLamp_BIT (23) +#define roomLamp_MASK (0x01<gpio_open(0x01 << roomLamp_BIT); + roomLamp->gpio_control(GPIO_CMD_SET_BIT_DIR_OUTPUT,(void *)(roomLamp_MASK)); + roomLamp->gpio_write(0x01 << roomLamp_BIT, roomLamp_MASK); +} +void roomLamp_close(void) +{ + roomLamp = gpio_get_dev(roomLamp_PORT); + roomLamp->gpio_open(0x01 << roomLamp_BIT); + roomLamp->gpio_control(GPIO_CMD_SET_BIT_DIR_OUTPUT,(void *)(roomLamp_MASK)); + roomLamp->gpio_write(0x00 << roomLamp_BIT, roomLamp_MASK); +} + + + + +void timer0_isr(void *ptr) +{ + + timer_int_clear(TIMER_0); + + + mcu2wifi_request_time(esp8266_uart); + mcu2wifi_dev_report_status(esp8266_uart); + +} +void timer1_isr(void *ptr) +{ + timer_int_clear(TIMER_1); + + + + DHT11_Read_Data(dht11, DHT11_BIT, dht11_data); + Room_Env_Tepm = dht11_data->T_data; + Room_Env_Humidity =dht11_data->RH_data; + EMBARC_PRINTF("Temperature is %d, Humidity is %d%%\r\n", dht11_data->T_data,dht11_data->RH_data); + + Room_PM25 = GP2Y1051_read(); + EMBARC_PRINTF("The PM2.5 is %d ug/m3.\r\n",Room_PM25); + + +} + +uint8_t *product_key = "ea7f60661bb543dd9da050869fc611e7"; +uint8_t *product_secret = "f13add71b9d749b496cba0d84c46f0b4"; + +int main(void) +{ + cpu_lock(); + board_init(); + + timer_stop(TIMER_0); /* Stop it first since it might be enabled before */ + int_handler_install(INTNO_TIMER0, timer0_isr); + int_enable(INTNO_TIMER0); + timer_start(TIMER_0, TIMER_CTRL_IE, 10*BOARD_CPU_CLOCK); + + timer_stop(TIMER_1); /* Stop it first since it might be enabled before */ + int_handler_install(INTNO_TIMER1, timer1_isr); + int_enable(INTNO_TIMER1); + timer_start(TIMER_1, TIMER_CTRL_IE, 2*BOARD_CPU_CLOCK); + + set_pmod_mux( + PM1_UR_UART_0|PM1_LR_GPIO_A | + PM2_I2C_HRI | + PM3_GPIO_AC | + PM4_GPIO_AC | + PM5_UR_GPIO_C | PM5_LR_SPI_M2 | + PM6_UR_GPIO_C | PM6_LR_GPIO_A + ); + + button_init(); + cpu_unlock(); + + + + //step_motor_init(STEP_MOTOR_PORT, STEP_MOTOR_MASK); + DHT11_init(); + ad7991_init(); + esp8266_init(); + body_ir_init(); + + roomLamp_close(); + + uint32_t rd_cnt = 0; + uint8_t read_data[MAX_READ_CNT]; + + led = gpio_get_dev(LED_PORT); + led-> gpio_open(0x80); + led-> gpio_control(GPIO_CMD_SET_BIT_DIR_OUTPUT,(void *)(0x80)); + uint8_t led_count = 0; + + while(1) + { + if((led_count)%10 == 0 ) led->gpio_write(0x80,0x80); + if((led_count)%20 == 0 ) led->gpio_write(0x00,0x80); + led_count++; + + board_delay_ms(100,0); + rd_cnt = esp8266_read(read_data,sizeof(read_data)); + if(rd_cnt) + { + for(uint8_t i=0;i 90) roomLamp_close(); + } + else if(Room_Lamp == 1) roomLamp_open(); + else roomLamp_close(); + + // + if(Fan_switch == 0) + { + if((Room_PM25 > 135) | (Room_Smoke > 30) | (Room_Env_Tepm > 35)) fan_open(); + if((Room_PM25 < 110) & (Room_Smoke < 20) & (Room_Env_Tepm < 30) ) fan_close(); + } + else if(Fan_switch == 1) fan_open(); + else fan_close(); + // + if(Room_Smoke > 30) buzzer_open(); + else buzzer_close(); + // + if (Leave_home) + { + Body_infrared = (body_ir_read())?0x01:0x00; + if(Body_infrared) buzzer_open(); + else buzzer_close(); + } + else Body_infrared = 0; + + + end_us = board_get_cur_us(); + EMBARC_PRINTF("use %d us time \r\n",end_us - start_us); + } + return E_SYS; +} \ No newline at end of file diff --git a/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Room_Env_subsystem/makefile b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Room_Env_subsystem/makefile new file mode 100644 index 00000000..cc8aa8de --- /dev/null +++ b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Room_Env_subsystem/makefile @@ -0,0 +1,70 @@ +## embARC application makefile template ## +### You can copy this file to your application folder +### and rename it to makefile. +## + +# Application name +APPL ?= Room_Env_subsystem + +# Optimization Level +# Please Refer to toolchain_xxx.mk for this option +OLEVEL ?= O2 + +## +# Current Board And Core +## +BOARD ?= emsk +BD_VER ?= 22 +CUR_CORE ?= arcem7d + +## +# select debugging jtag +## +JTAG ?= usb + +## +# Set toolchain +## +TOOLCHAIN ?= gnu + +## +# Uncomment following options +# if you want to set your own heap and stack size +# Default settings see options.mk +## +#HEAPSZ ?= 8192 +#STACKSZ ?= 8192 + +## +# Uncomment following options +# if you want to add your own library into link process +# For example: +# If you want link math lib for gnu toolchain, +# you need to set the option to -lm +## +#APPL_LIBS ?= + +# +# root dir of embARC +# +EMBARC_ROOT = ../../.. + +MID_SEL = common + +# application source dirs +APPL_CSRC_DIR = . ./src +APPL_ASMSRC_DIR = . + +# application include dirs +APPL_INC_DIR = . + +# application defines +APPL_DEFINES = + +# include current project makefile +COMMON_COMPILE_PREREQUISITES += makefile + +### Options above must be added before include options.mk ### +# include key embARC build system makefile +override EMBARC_ROOT := $(strip $(subst \,/,$(EMBARC_ROOT))) +include $(EMBARC_ROOT)/options/options.mk diff --git a/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Room_Env_subsystem/step_motor.c b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Room_Env_subsystem/step_motor.c new file mode 100644 index 00000000..9132cb13 --- /dev/null +++ b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Room_Env_subsystem/step_motor.c @@ -0,0 +1,84 @@ +#include "embARC.h" +#include "embARC_debug.h" + +#include "dev_gpio.h" +#include "step_motor.h" + +#define SPEED_FACT 1400 + +static DEV_GPIO_PTR step_motor_port; +static uint32_t port_bit_mask; + +static void delay_us( uint32_t us) +{ + uint32_t volatile x,y; + for(x=2;x>0;x--) + for(y=us;y>0;y--); + +} + +void step_motor_init(uint32_t gpio_port, uint32_t gpio_port_mask) +{ + step_motor_port = gpio_get_dev(gpio_port); + port_bit_mask = gpio_port_mask; + + step_motor_port->gpio_open(GPIO_DIR_OUTPUT); + step_motor_port->gpio_control(GPIO_CMD_SET_BIT_DIR_OUTPUT,(void *)(port_bit_mask)); + step_motor_port->gpio_write(0x00,port_bit_mask); +} + + + +void step_motor_active(uint8_t step_dir, uint16_t step_angle, uint8_t step_speed) +{ + switch(step_dir) + { + case CW_DIR: + for(uint16_t angle = step_angle; angle > 0; angle--) + { + delay_us( (255 - step_speed)*SPEED_FACT ); + step_motor_port->gpio_write(0x77777777,port_bit_mask); + delay_us( (255 - step_speed)*SPEED_FACT ); + step_motor_port->gpio_write(0x33333333,port_bit_mask); + delay_us( (255 - step_speed)*SPEED_FACT ); + step_motor_port->gpio_write(0xBBBBBBBB,port_bit_mask); + delay_us( (255 - step_speed)*SPEED_FACT ); + step_motor_port->gpio_write(0x99999999,port_bit_mask); + delay_us( (255 - step_speed)*SPEED_FACT ); + step_motor_port->gpio_write(0xDDDDDDDD,port_bit_mask); + delay_us( (255 - step_speed)*SPEED_FACT ); + step_motor_port->gpio_write(0xCCCCCCCC,port_bit_mask); + delay_us( (255 - step_speed)*SPEED_FACT ); + step_motor_port->gpio_write(0xEEEEEEEE,port_bit_mask); + delay_us( (255 - step_speed)*SPEED_FACT ); + step_motor_port->gpio_write(0x66666666,port_bit_mask); + delay_us( (255 - step_speed)*SPEED_FACT ); + step_motor_port->gpio_write(0x00000000,port_bit_mask); + }; + break; + case CCW_DIR: + for(uint16_t angle = step_angle; angle > 0; angle--) + { + delay_us( (255 - step_speed)*SPEED_FACT ); + step_motor_port->gpio_write(0x66666666,port_bit_mask); + delay_us( (255 - step_speed)*SPEED_FACT ); + step_motor_port->gpio_write(0xEEEEEEEE,port_bit_mask); + delay_us( (255 - step_speed)*SPEED_FACT ); + step_motor_port->gpio_write(0xCCCCCCCC,port_bit_mask); + delay_us( (255 - step_speed)*SPEED_FACT); + step_motor_port->gpio_write(0xDDDDDDDD,port_bit_mask); + delay_us( (255 - step_speed)*SPEED_FACT ); + step_motor_port->gpio_write(0x99999999,port_bit_mask); + delay_us( (255 - step_speed)*SPEED_FACT ); + step_motor_port->gpio_write(0xBBBBBBBB,port_bit_mask); + delay_us( (255 - step_speed)*SPEED_FACT ); + step_motor_port->gpio_write(0x33333333,port_bit_mask); + delay_us( (255 - step_speed)*SPEED_FACT ); + step_motor_port->gpio_write(0x77777777,port_bit_mask); + delay_us( (255 - step_speed)*SPEED_FACT ); + step_motor_port->gpio_write(0x00000000,port_bit_mask); + }; + break; + default: break; + } +} \ No newline at end of file diff --git a/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Room_Env_subsystem/step_motor.h b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Room_Env_subsystem/step_motor.h new file mode 100644 index 00000000..98c9eb5d --- /dev/null +++ b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Room_Env_subsystem/step_motor.h @@ -0,0 +1,11 @@ +#ifndef _STEP_MOTOT_H +#define _STEP_MOTOT_H + +#include "dev_gpio.h" + +#define CW_DIR 0 +#define CCW_DIR 1 +extern void step_motor_init(uint32_t gpio_port, uint32_t gpio_port_mask); +extern void step_motor_active(uint8_t step_dir, uint16_t step_angle, uint8_t step_speed); + +#endif \ No newline at end of file diff --git a/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Voice_Assistant/esp8266_gagent_common.c b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Voice_Assistant/esp8266_gagent_common.c new file mode 100644 index 00000000..e5e57540 --- /dev/null +++ b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Voice_Assistant/esp8266_gagent_common.c @@ -0,0 +1,325 @@ +#include "embARC.h" +#include "embARC_debug.h" + +#include "dev_gpio.h" +#include "dev_uart.h" +#include "dev_iic.h" +#include "dev_spi.h" +#include "board.h" + +#include "esp8266_gagent_common.h" + + +void mcu2wifi_product_info(DEV_UART_PTR esp8266_uart, uint8_t *product_key, uint8_t *product_secret) +{ + uint8_t checksum=0; + int i=0; // + uint8_t send_data[] = { + 0xFF,0xFF, // + 0x00,0x00, // + 0x02, // + 0x00, // + 0x00,0x00, //flags + '0','0','0','0','0','0','0','4', // + '0','0','0','0','0','0','0','2', // + '0','0','0','0','0','0','0','0', // + '0','0','0','0','0','0','0','0', // + + + '0','0','0','0','0','0','0','0', // + '0','0','0','0','0','0','0','0', + '0','0','0','0','0','0','0','0', + '0','0','0','0','0','0','0','0', + + 0x01,0x2C, // + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, // + + '0','0','0','0','0','0','0','0', // + '0','0','0','0','0','0','0','0', + '0','0','0','0','0','0','0','0', + '0','0','0','0','0','0','0','0', + 0xEE // + }; + send_data[2] = (sizeof(send_data)-0x04)>>8; // + send_data[3] = (sizeof(send_data)-0x04)%256; // + send_data[5] = sn; // + for(i=0; i<32; i++) + { + send_data[40+i] = product_key[i]; + send_data[82+i] = product_secret[i]; + } + for(i=0; i<32; i++) EMBARC_PRINTF("%c",send_data[40+i]); EMBARC_PRINTF("\r\n"); + for(i=0; i<32; i++) EMBARC_PRINTF("%c",send_data[82+i]); EMBARC_PRINTF("\r\n"); + // + for(uint8_t i=2;i< sizeof(send_data)-1;i++) checksum = checksum + send_data[i]; + send_data[sizeof(send_data)-1] =checksum; + // + esp8266_uart->uart_write(send_data,sizeof(send_data)); +} + + + +void mcu2wifi_heartbeat(DEV_UART_PTR esp8266_uart) +{ + uint8_t checksum=0; + uint8_t send_data[] = { + 0xFF,0xFF, // + 0x00,0x00, // + 0x08, // + 0x00, // + 0x00,0x00, //flags + 0x00 // + }; + send_data[2] = (sizeof(send_data)-0x04)>>8; // + send_data[3] = (sizeof(send_data)-0x04)%256; // + send_data[5] = sn; // + // + for(uint8_t i=2;iuart_write(send_data,sizeof(send_data)); +} + + +void mcu2wifi_set_cfg_mode(DEV_UART_PTR esp8266_uart, uint8_t mode) +{ + uint8_t checksum=0; + uint8_t send_data[] = { + 0xFF,0xFF, // + 0x00,0x05, // + 0x09, // + 0x00, // + 0x00,0x00, //flags + 0x00, // + 0x00 // + }; + send_data[2] = (sizeof(send_data)-0x04)>>8; // + send_data[3] = (sizeof(send_data)-0x04)%256; // + send_data[5] = ++sn; // + + send_data[8] = mode; + // + for(uint8_t i=2;iuart_write(send_data,sizeof(send_data)); +} + + +void mcu2wifi_reset_wifi(DEV_UART_PTR esp8266_uart) +{ + uint8_t checksum=0; + uint8_t send_data[] = { + 0xFF,0xFF, // + 0x00,0x00, // + 0x0b, // + 0x00, // + 0x00,0x00, //flags + 0x00 // + }; + send_data[2] = (sizeof(send_data)-0x04)>>8; // + send_data[3] = (sizeof(send_data)-0x04)%256; // + send_data[5] = ++sn; // + // + for(uint8_t i=2;iuart_write(send_data,sizeof(send_data)); +} + + + +void mcu2wifi_wifi_statu(DEV_UART_PTR esp8266_uart,uint8_t *receive_data) +{ + uint8_t checksum=0; + uint8_t send_data[] = { + 0xFF,0xFF, // + 0x00,0x05, // + 0x0E, // + 0x00, // + 0x00,0x00, //flags + 0x00 // + }; + send_data[2] = (sizeof(send_data)-0x04)>>8; + send_data[3] = (sizeof(send_data)-0x04)%256; + send_data[5] = sn; + for(uint8_t i=2;iuart_write(send_data,sizeof(send_data)); + + uint8_t statu_bit0_7 = receive_data[9]; + uint8_t statu_bit8_15 = receive_data[8]; + + if((statu_bit0_7) %2 ) EMBARC_PRINTF("SoftAP mode is open\r\n"); + if((statu_bit0_7>>1 )%2 ) EMBARC_PRINTF("Station mode is open\r\n"); + if((statu_bit0_7>>2 )%2 ) EMBARC_PRINTF("OnBoarding mode is open\r\n"); + //LED 5 + if((statu_bit0_7>>3 )%2 ) + { + EMBARC_PRINTF("Binding mode is open\r\n"); + led = gpio_get_dev(LED_PORT); + led-> gpio_open(0x20); + led-> gpio_control(GPIO_CMD_SET_BIT_DIR_OUTPUT,(void *)(0x20)); + led-> gpio_write(0x00,0x20); + } + else + { + led = gpio_get_dev(LED_PORT); + led-> gpio_open(0x20); + led-> gpio_control(GPIO_CMD_SET_BIT_DIR_OUTPUT,(void *)(0x20)); + led-> gpio_write(0x20,0x20); + } + //LED 0~2 + if((statu_bit0_7>>4 )%2 ) + { + EMBARC_PRINTF("The WiFi module is connected to the wireless router,the RSSI = %d\r\n",(statu_bit8_15)%8); + led = gpio_get_dev(LED_PORT); + led-> gpio_open(0x07); + led-> gpio_control(GPIO_CMD_SET_BIT_DIR_OUTPUT,(void *)(0x07)); + led-> gpio_write(~((statu_bit8_15)%8),0x07); + } + else + { + led = gpio_get_dev(LED_PORT); + led-> gpio_open(0x07); + led-> gpio_control(GPIO_CMD_SET_BIT_DIR_OUTPUT,(void *)(0x07)); + led-> gpio_write(0x7,0x07); + } + //LED 3 + if((statu_bit0_7>>5 )%2 ) + { + EMBARC_PRINTF("The WiFi module is connected to the M2M server\r\n"); + led = gpio_get_dev(LED_PORT); + led-> gpio_open(0x08); + led-> gpio_control(GPIO_CMD_SET_BIT_DIR_OUTPUT,(void *)(0x08)); + led-> gpio_write(0x00,0x08); + + } + else + { + led = gpio_get_dev(LED_PORT); + led-> gpio_open(0x08); + led-> gpio_control(GPIO_CMD_SET_BIT_DIR_OUTPUT,(void *)(0x08)); + led-> gpio_write(0x08,0x08); + } + //LED 4 + if((statu_bit8_15>>3)%2 ) + { + EMBARC_PRINTF("APP online\r\n"); + led = gpio_get_dev(LED_PORT); + led-> gpio_open(0x10); + led-> gpio_control(GPIO_CMD_SET_BIT_DIR_OUTPUT,(void *)(0x10)); + led-> gpio_write(0x00,0x10); + + } + else + { + led = gpio_get_dev(LED_PORT); + led-> gpio_open(0x10); + led-> gpio_control(GPIO_CMD_SET_BIT_DIR_OUTPUT,(void *)(0x10)); + led-> gpio_write(0x10,0x10); + } + +} + + +void mcu2wifi_receive_error(DEV_UART_PTR esp8266_uart) +{ + uint8_t checksum=0; + uint8_t send_data[] = { + 0xFF,0xFF, // + 0x00,0x00, // + 0x12, // + 0x00, // + 0x00,0x00, //flags + 0x00, // + 0x00 // + }; + send_data[2] = (sizeof(send_data)-0x04)>>8; // + send_data[3] = (sizeof(send_data)-0x04)%256; // + send_data[5] = sn; // + send_data[8] = 2; + // + for(uint8_t i=2;iuart_write(send_data,sizeof(send_data)); +} + + +void mcu2wifi_request_time(DEV_UART_PTR esp8266_uart) +{ + uint8_t checksum=0; + uint8_t send_data[] = { + 0xFF,0xFF, // + 0x00,0x05, // + 0x17, // + 0x00, // + 0x00,0x00, //flags + 0x00 // + }; + send_data[2] = (sizeof(send_data)-0x04)>>8; // + send_data[3] = (sizeof(send_data)-0x04)%256; // + send_data[5] = ++sn; // + // + for(uint8_t i=2;iuart_write(send_data,sizeof(send_data)); +} + + +void wifi2mcu_get_time(uint8_t *receive_data) +{ + year = receive_data[8]*256+receive_data[9]; + month = receive_data[10]; + day = receive_data[11]; + hour = receive_data[12]; + minute = receive_data[13]; + second = receive_data[14]; + + EMBARC_PRINTF("Now is %d/%d/%d %d:%d:%d \r\n",year,month,day,hour,minute,second); +} + + +void mcu2wifi_restart_wifi(DEV_UART_PTR esp8266_uart) +{ + uint8_t checksum=0; + uint8_t send_data[] = { + 0xFF,0xFF, // + 0x00,0x00, // + 0x29, // + 0x00, // + 0x00,0x00, //flags + 0x00 // + }; + send_data[2] = (sizeof(send_data)-0x04)>>8; // + send_data[3] = (sizeof(send_data)-0x04)%256; // + send_data[5] = ++sn; // + // + for(uint8_t i=2;iuart_write(send_data,sizeof(send_data)); +} + + +void mcu2wifi_request_into_binding(DEV_UART_PTR esp8266_uart) +{ + uint8_t checksum=0; + uint8_t send_data[] = { + 0xFF,0xFF, // + 0x00,0x00, // + 0x15, // + 0x00, // + 0x00,0x00, //flags + 0x00 // + }; + send_data[2] = (sizeof(send_data)-0x04)>>8; // + send_data[3] = (sizeof(send_data)-0x04)%256; // + send_data[5] = ++sn; // + // + for(uint8_t i=2;iuart_write(send_data,sizeof(send_data)); +} \ No newline at end of file diff --git a/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Voice_Assistant/esp8266_gagent_common.h b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Voice_Assistant/esp8266_gagent_common.h new file mode 100644 index 00000000..c575db93 --- /dev/null +++ b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Voice_Assistant/esp8266_gagent_common.h @@ -0,0 +1,46 @@ +#ifndef _ESP8266_GAGENT_COMMON_H_ +#define _ESP8266_GAGENT_COMMON_H_ + +#include "dev_uart.h" + +uint8_t sn; +uint16_t year; +uint8_t month; +uint8_t day; +uint8_t hour; +uint8_t minute; +uint8_t second; + + +DEV_GPIO_PTR led; +#define LED_PORT DW_GPIO_PORT_B +#define LED_MASK 0x1ff +#define LED_DIR 0x1ff + + + +extern void mcu2wifi_product_info(DEV_UART_PTR esp8266_uart, uint8_t *product_key, uint8_t *product_secret); + +extern void mcu2wifi_heartbeat(DEV_UART_PTR esp8266_uart); + + +extern void mcu2wifi_set_cfg_mode(DEV_UART_PTR esp8266_uart,uint8_t mode); + + +extern void mcu2wifi_reset_wifi(DEV_UART_PTR esp8266_uart); + + +extern void mcu2wifi_wifi_statu(DEV_UART_PTR esp8266_uart,uint8_t *receive_data); + + +extern void mcu2wifi_receive_error(DEV_UART_PTR esp8266_uart); +/ +extern void mcu2wifi_request_time(DEV_UART_PTR esp8266_uart); + +extern void wifi2mcu_get_time(uint8_t *receive_data); + +extern void mcu2wifi_restart_wifi(DEV_UART_PTR esp8266_uart); + +extern void mcu2wifi_request_into_binding(DEV_UART_PTR esp8266_uart); + +#endif \ No newline at end of file diff --git a/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Voice_Assistant/esp8266_gagent_user.c b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Voice_Assistant/esp8266_gagent_user.c new file mode 100644 index 00000000..a4fb6eb0 --- /dev/null +++ b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Voice_Assistant/esp8266_gagent_user.c @@ -0,0 +1,100 @@ +#include "embARC.h" +#include "embARC_debug.h" + +#include "dev_gpio.h" +#include "dev_uart.h" +#include "dev_iic.h" +#include "dev_spi.h" + +#include "board.h" + +#include "esp8266_gagent_user.h" + + + +void mcu2wifi_wifi_ctrl_dev(DEV_UART_PTR esp8266_uart,uint8_t *receive_data) +{ + // + uint8_t checksum=0; + uint8_t send_data[] = { + 0xFF,0xFF, // + 0x00,0x00, // + 0x04, // + 0x00, // + 0x00,0x00, //flags + 0x00 // + }; + uint8_t i; + send_data[2] = (sizeof(send_data)-0x04)>>8; // + send_data[3] = (sizeof(send_data)-0x04)%256;// + send_data[5] = sn; // + // + for(i=2;iuart_write(send_data,sizeof(send_data)); + +} + +void mcu2wifi_wifi_read_dev(DEV_UART_PTR esp8266_uart) +{ + uint8_t checksum=0; + uint8_t send_data[0x0A + DEV_STATUS_BYTES] = { + 0xFF,0xFF, // + 0x00,0x05, // + 0x04, // + 0x00, // + 0x00,0x00, //flags + 0x03, //action; + 0x00, // + 0x14 // + }; + send_data[2] = (0x0A + DEV_STATUS_BYTES-0x04)>>8; // + send_data[3] = (0x0A + DEV_STATUS_BYTES-0x04)%256; // + send_data[5] = sn; // + // + // Aquarius_Oxy_Freq and Aquarius_Feed_Freq and Aquarius_Light + send_data[9] = Lamp_switch * 8 + Fan_switch * 4 + LeaveModel_switch * 2 + AquariusLight_switch; + // Aquarius_Feed_Amount + //send_data[10] = Aquarius_Feed_Amount; + // Aquarius_Temp + //send_data[11] = Aquarius_Temp; + + // + for(uint8_t i=2;i<0x0A + DEV_STATUS_BYTES-1;i++) checksum = checksum + send_data[i]; + send_data[0x0A + DEV_STATUS_BYTES-1] =checksum; + // + esp8266_uart->uart_write(send_data,0x0A + DEV_STATUS_BYTES); +} + + +void mcu2wifi_dev_report_status(DEV_UART_PTR esp8266_uart) +{ + uint8_t checksum=0; + uint8_t send_data[0x0A + DEV_STATUS_BYTES] = { + 0xFF,0xFF, // + 0x00,0x05, // + 0x05, // + 0x00, // + 0x00,0x00, //flags + 0x04, //action; + 0x00, // + 0x14 // + }; + send_data[2] = (0x0A + DEV_STATUS_BYTES-0x04)>>8; // + send_data[3] = (0x0A + DEV_STATUS_BYTES-0x04)%256; // + send_data[5] = ++sn; // + // + // Aquarius_Oxy_Freq and Aquarius_Feed_Freq and Aquarius_Light + send_data[9] = AquariusLight_switch * 8 + LeaveModel_switch * 4 + Fan_switch * 2 + Lamp_switch; + // Aquarius_Feed_Amount + //send_data[10] = Aquarius_Feed_Amount; + // Aquarius_Temp + //send_data[11] = Aquarius_Temp; + + // + for(uint8_t i=2;i<0x0A + DEV_STATUS_BYTES-1;i++) checksum = checksum + send_data[i]; + send_data[0x0A + DEV_STATUS_BYTES-1] =checksum; + // + esp8266_uart->uart_write(send_data,0x0A + DEV_STATUS_BYTES); +} \ No newline at end of file diff --git a/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Voice_Assistant/esp8266_gagent_user.h b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Voice_Assistant/esp8266_gagent_user.h new file mode 100644 index 00000000..20531a28 --- /dev/null +++ b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Voice_Assistant/esp8266_gagent_user.h @@ -0,0 +1,36 @@ +#ifndef _ESP8266_GAGENT_USER_H_ +#define _ESP8266_GAGENT_USER_H_ + +#include "dev_uart.h" +//#include "DS18B20.h" +#include "esp8266_gagent_common.h" + + +// DEV_GPIO_PTR ds18b20_port_ptr; +// DS18B20_DEF_PTR ds18b20_data; +// #define DS18B20_PORT DW_GPIO_PORT_C +// #define DS18B20_BIT 15 + +#define MAX_READ_CNT 32 + + +// +uint8_t Lamp_switch; // +uint8_t Fan_switch; // +uint8_t LeaveModel_switch; // +uint8_t AquariusLight_switch; // + + +#define ATTR_FLAGS_BYTES (0) +// +#define ATTR_VALS_BYTES (0) +extern void mcu2wifi_wifi_ctrl_dev(DEV_UART_PTR esp8266_uart,uint8_t *receive_data); + + +#define DEV_STATUS_BYTES (1) +extern void mcu2wifi_wifi_read_dev(DEV_UART_PTR esp8266_uart); + + +extern void mcu2wifi_dev_report_status(DEV_UART_PTR esp8266_uart); + +#endif \ No newline at end of file diff --git a/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Voice_Assistant/main.c b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Voice_Assistant/main.c new file mode 100644 index 00000000..5d594d7d --- /dev/null +++ b/arc_design_contest/2018/XDU_Intelligent_Home_Hosting_System/src/Voice_Assistant/main.c @@ -0,0 +1,359 @@ +#include "embARC.h" +#include "embARC_debug.h" +#include +#include "dev_gpio.h" +#include "dev_uart.h" +#include "dev_iic.h" +#include "dev_spi.h" + +#include "../../../board/emsk/drivers/mux/mux.h" +#include "../../../board/emsk/drivers/mux/mux_hal.h" + +#include "esp8266_gagent_common.h" +#include "esp8266_gagent_user.h" + + +DEV_UART_PTR esp8266_uart; +#define ESP8266_UART_ID DW_UART_0_ID +#define ESP8266_UART_BAUDRATE UART_BAUDRATE_9600 + +void esp8266_init(void) +{ + set_uart_map(0xe4); + esp8266_uart = uart_get_dev(ESP8266_UART_ID); + esp8266_uart->uart_close(); + if(esp8266_uart->uart_open(ESP8266_UART_BAUDRATE) == E_OPNED) + { + esp8266_uart->uart_control(UART_CMD_SET_BAUD, (void *)(ESP8266_UART_BAUDRATE)); + } +} +uint32_t esp8266_read(uint8_t *buf, uint32_t cnt) +{ + if (esp8266_uart == NULL) return 0; + + uint32_t rd_avail; + esp8266_uart->uart_control(UART_CMD_GET_RXAVAIL, (void *)(&rd_avail)); + cnt = (cnt > rd_avail)?rd_avail:cnt; + if (cnt > 0) { + return esp8266_uart->uart_read(buf, cnt); + } + return cnt; +} +uint32_t esp8266_write(uint8_t *buf, uint32_t cnt) +{ + if (esp8266_uart == NULL) return 0; + + return esp8266_uart->uart_write(buf, cnt); +} + +DEV_GPIO_PTR button; +#define BUTTON_MASK 0x07 +void gpio_keyL_isr(void *ptr) +{ + EMBARC_PRINTF("key 1: L\r\n"); + mcu2wifi_request_into_binding(esp8266_uart); +} +void gpio_keyR_isr(void *ptr) +{ + EMBARC_PRINTF("key 2: R\r\n"); + mcu2wifi_reset_wifi(esp8266_uart); +} +void gpio_keyA_isr(void *ptr) +{ + EMBARC_PRINTF("key 3: A\r\n"); +} +void button_init(void) +{ + DEV_GPIO_BIT_ISR bit_isr; + DEV_GPIO_INT_CFG int_cfg; + button = gpio_get_dev(DW_GPIO_PORT_A); + button ->gpio_open(BUTTON_MASK); + button ->gpio_control(GPIO_CMD_SET_BIT_DIR_INPUT, (void *)BUTTON_MASK); + int_cfg.int_bit_mask = BUTTON_MASK; + int_cfg.int_bit_type = GPIO_INT_BITS_EDGE_TRIG(BUTTON_MASK); + int_cfg.int_bit_polarity = GPIO_INT_BITS_POL_FALL_EDGE(BUTTON_MASK); + int_cfg.int_bit_debounce = GPIO_INT_BITS_ENA_DEBOUNCE(BUTTON_MASK); + button->gpio_control(GPIO_CMD_SET_BIT_INT_CFG, &int_cfg); + bit_isr.int_bit_ofs = 0; + bit_isr.int_bit_handler = gpio_keyL_isr; + button->gpio_control(GPIO_CMD_SET_BIT_ISR, (void *)(&bit_isr)); + bit_isr.int_bit_ofs = 1; + bit_isr.int_bit_handler = gpio_keyR_isr; + button->gpio_control(GPIO_CMD_SET_BIT_ISR, (void *)(&bit_isr)); + bit_isr.int_bit_ofs = 2; + bit_isr.int_bit_handler = gpio_keyA_isr; + button->gpio_control(GPIO_CMD_SET_BIT_ISR, (void *)(&bit_isr)); + + button->gpio_control(GPIO_CMD_ENA_BIT_INT, (void *)(0x7)); +} +DEV_UART_PTR uart2; +#define UART2_ID DW_UART_2_ID +#define UART2_BAUDRATE UART_BAUDRATE_9600 + +void uart2_init(void) +{ + + uart2 = uart_get_dev(UART2_ID); + uart2->uart_close(); + if(uart2->uart_open(UART2_BAUDRATE) == E_OPNED) + { + uart2->uart_control(UART_CMD_SET_BAUD, (void *)(UART2_BAUDRATE)); + } +} +uint32_t uart2_read(uint8_t *buf, uint32_t cnt) +{ + if (uart2 == NULL) return 0; + + uint32_t rd_avail; + uart2->uart_control(UART_CMD_GET_RXAVAIL, (void *)(&rd_avail)); + cnt = (cnt > rd_avail)?rd_avail:cnt; + if (cnt > 0) { + return uart2->uart_read(buf, cnt); + } + return cnt; +} +uint32_t uart2_write(uint8_t *buf, uint32_t cnt) +{ + if (uart2 == NULL) return 0; + + return uart2->uart_write(buf, cnt); +} + + + + + + + +void timer0_isr(void *ptr) +{ + timer_int_clear(TIMER_0); + mcu2wifi_request_time(esp8266_uart); +} + +uint8_t huanxin[] = {0xFD, 0x00, 0x01, 0xFF}; +uint8_t shuimian[] = {0xFD, 0x00, 0x01, 0x88}; +uint8_t wozai[] = {0xFD, 0x00, 0x06, 0x01, 0x01, 0xCE, 0xD2, 0xD4, 0xDA}; +uint8_t kaideng[] = {0xFD,0x00, 0x0A, 0x01, 0x01, 0xB5, 0xC6, 0xD2, 0xD1, 0xB4, 0xF2, 0xBF, 0xAA}; +uint8_t guandeng[] = {0xFD, 0x00, 0x0A, 0x01, 0x01, 0xB5, 0xC6, 0xD2, 0xD1, 0xB9, 0xD8, 0xB1, 0xD5}; +uint8_t dklijiamoshi[] = {0xFD, 0x00, 0x10, 0x01, 0x01, 0xC0, 0xEB, 0xBC, 0xD2, 0xC4, 0xA3, 0xCA, 0xBD, 0xD2, 0xD1, 0xB4, 0xF2, 0xBF, 0xAA}; +uint8_t dkyugangdeng[] = {0xFD,0x00,0x12,0x01,0x01,0xD3,0xE3,0xB8,0xD7,0xD5,0xD5,0xC3,0xF7,0xB5,0xC6,0xD2,0xD1,0xB4,0xF2,0xBF,0xAA}; +uint8_t gbyugangdeng[] = {0xFD,0x00,0x12,0x01,0x01,0xD3,0xE3,0xB8,0xD7,0xD5,0xD5,0xC3,0xF7,0xB5,0xC6,0xD2,0xD1,0xB9,0xD8,0xB1,0xD5}; +uint8_t riqi[] = {0xFD, 0x00, 0x12, 0x01, 0x01, 0xBD, 0xF1, 0xCC, 0xEC, 0xCA, 0xC7, 0x32, 0x30, 0x31, 0x38, 0x2D, 0x30, 0x37, 0x2D, 0x30, 0x33}; +uint8_t shijian[] = {0xFD, 0x00, 0x0D, 0x01, 0x01, 0xCF, 0xD6, 0xD4, 0xDA, 0xCA, 0xC7, 0x31, 0x33, 0x3A, 0x34, 0x39}; +uint8_t dkfengshan[] = {0xFD, 0x00, 0x0E, 0x01, 0x01, 0xC5, 0xC5, 0xB7, 0xE7, 0xC9, 0xC8, 0xD2, 0xD1, 0xB4, 0xF2, 0xBF, 0xAA}; +uint8_t gbfengshan[] = {0xFD, 0x00, 0x0E, 0x01, 0x01, 0xC5, 0xC5, 0xB7, 0xE7, 0xC9, 0xC8, 0xD2, 0xD1, 0xB9, 0xD8, 0xB1, 0xD5}; +uint8_t nansheng[] = {0xFD,0x00,0x07,0x01,0x01,0x5B,0x6D,0x35,0x31,0x5D}; + +// +uint8_t *product_key = "dfc5e956f7724cb4a050c892dbef0748"; +uint8_t *product_secret = "bb67c47127244067a3392b5deff3e651"; +int main(void) +{ + cpu_lock(); + board_init(); + + timer_stop(TIMER_0); /* Stop it first since it might be enabled before */ + int_handler_install(INTNO_TIMER0, timer0_isr); + int_enable(INTNO_TIMER0); + timer_start(TIMER_0, TIMER_CTRL_IE, 10*BOARD_CPU_CLOCK); + + set_pmod_mux( + PM1_UR_UART_0|PM1_LR_GPIO_A | + PM2_GPIO_AC | + PM3_GPIO_AC | + PM4_GPIO_AC | + PM5_UR_GPIO_C | PM5_LR_SPI_M2 | + PM6_UR_GPIO_C | PM6_LR_GPIO_A + ); + button_init(); + cpu_unlock(); + esp8266_init(); + uart2_init(); + + uint32_t rd_cnt = 0; + uint8_t read_data[MAX_READ_CNT]; + + led = gpio_get_dev(LED_PORT); + led-> gpio_open(0x80); + led-> gpio_control(GPIO_CMD_SET_BIT_DIR_OUTPUT,(void *)(0x80)); + uint8_t led_count = 0; + + + while(1) + { + if((led_count)%10 == 0 ) led->gpio_write(0x80,0x80); + if((led_count)%20 == 0 ) led->gpio_write(0x00,0x80); + led_count++; + + board_delay_ms(100,0); + rd_cnt = esp8266_read(read_data,sizeof(read_data)); + if(rd_cnt) + { + for(uint8_t i=0;i