├── IMAGES ├── how.txt ├── 002.jpg ├── 003.jpg └── EFEKTA_eON213z.jpg ├── PROJECT SOURCE ├── how.txt └── EINK213TH_SIMPLE_TEST │ ├── zstack-lib │ ├── README.md │ ├── inttempsens.h │ ├── senseair.h │ ├── mhz19.h │ ├── tl_resetter.h │ ├── utils.c │ ├── battery.h │ ├── commissioning.h │ ├── stdint.h │ ├── Debug.h │ ├── factory_reset.h │ ├── senseair.c │ ├── inttempsens.c │ ├── Debug.c │ ├── mhz19.c │ ├── tl_resetter.c │ ├── factory_reset.c │ ├── hal_i2c.h │ ├── bettery.c │ ├── utils.h │ ├── hal_key.h │ ├── f8wConfig.cfg │ ├── commissioning.c │ ├── hal_key.c │ └── hal_i2c.c │ ├── CC2530DB │ ├── settings │ │ └── GenericApp.reggroups │ └── GenericApp.eww │ ├── Source │ ├── version.h │ ├── .DS_Store │ ├── spi.h │ ├── version.c │ ├── stdint.h │ ├── SHTC3.h │ ├── imagedata.h │ ├── epdpaint.h │ ├── OSAL_App.c │ ├── preinclude.h │ ├── zcl_app.h │ ├── ssd1675.h │ ├── SHTC3.c │ ├── zcl_app_data.c │ ├── epdpaint.c │ ├── spi.c │ ├── ssd1675.c │ ├── hal_board_cfg.h │ └── zcl_app.c │ └── ver.py ├── SCHEME ├── Schematic_EFEKTA_eON213z.pdf └── Schematic_EFEKTA_eON213z.png ├── Gerber files └── README.md └── README.md /IMAGES/how.txt: -------------------------------------------------------------------------------- 1 | hello@efektalab.com 2 | -------------------------------------------------------------------------------- /PROJECT SOURCE/how.txt: -------------------------------------------------------------------------------- 1 | hello@efektalab.com 2 | -------------------------------------------------------------------------------- /IMAGES/002.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/smartboxchannel/EFEKTA_eON213z/HEAD/IMAGES/002.jpg -------------------------------------------------------------------------------- /IMAGES/003.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/smartboxchannel/EFEKTA_eON213z/HEAD/IMAGES/003.jpg -------------------------------------------------------------------------------- /PROJECT SOURCE/EINK213TH_SIMPLE_TEST/zstack-lib/README.md: -------------------------------------------------------------------------------- 1 | # Collection of reusable componets for Z-stack 3.0.2 -------------------------------------------------------------------------------- /IMAGES/EFEKTA_eON213z.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/smartboxchannel/EFEKTA_eON213z/HEAD/IMAGES/EFEKTA_eON213z.jpg -------------------------------------------------------------------------------- /PROJECT SOURCE/EINK213TH_SIMPLE_TEST/CC2530DB/settings/GenericApp.reggroups: -------------------------------------------------------------------------------- 1 | group = "io", "P0" 2 | group = "Unnamed2", "P1" 3 | -------------------------------------------------------------------------------- /PROJECT SOURCE/EINK213TH_SIMPLE_TEST/Source/version.h: -------------------------------------------------------------------------------- 1 | extern const uint8 zclApp_DateCode[]; 2 | extern const char zclApp_DateCodeNT[]; -------------------------------------------------------------------------------- /SCHEME/Schematic_EFEKTA_eON213z.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/smartboxchannel/EFEKTA_eON213z/HEAD/SCHEME/Schematic_EFEKTA_eON213z.pdf -------------------------------------------------------------------------------- /SCHEME/Schematic_EFEKTA_eON213z.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/smartboxchannel/EFEKTA_eON213z/HEAD/SCHEME/Schematic_EFEKTA_eON213z.png -------------------------------------------------------------------------------- /PROJECT SOURCE/EINK213TH_SIMPLE_TEST/zstack-lib/inttempsens.h: -------------------------------------------------------------------------------- 1 | #ifndef inttempsens_h 2 | int16 readTemperature(void); 3 | #define inttempsens_h 4 | #endif 5 | -------------------------------------------------------------------------------- /PROJECT SOURCE/EINK213TH_SIMPLE_TEST/Source/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/smartboxchannel/EFEKTA_eON213z/HEAD/PROJECT SOURCE/EINK213TH_SIMPLE_TEST/Source/.DS_Store -------------------------------------------------------------------------------- /Gerber files/README.md: -------------------------------------------------------------------------------- 1 | You can make your own pcb here - https://www.pcbway.com/setinvite.aspx?inviteid=550959 2 | 3 | pcb 1mm - https://www.pcbway.com/project/shareproject/EFEKTA_eON213z_2ad692dc.html 4 | 5 | 6 | -------------------------------------------------------------------------------- /PROJECT SOURCE/EINK213TH_SIMPLE_TEST/zstack-lib/senseair.h: -------------------------------------------------------------------------------- 1 | #ifndef SENSEAIR_H 2 | #define SENSEAIR_H 3 | extern void SenseAir_RequestMeasure(void); 4 | extern uint16 SenseAir_Read(void); 5 | extern void SenseAir_SetABC(bool isEnabled); 6 | #endif -------------------------------------------------------------------------------- /PROJECT SOURCE/EINK213TH_SIMPLE_TEST/zstack-lib/mhz19.h: -------------------------------------------------------------------------------- 1 | #ifndef mhz19_h 2 | #define mhz19_h 3 | extern void MHZ19_RequestMeasure(void); 4 | extern uint16 MHZ19_Read(void); 5 | extern void MHZ19_SetABC(bool isEnabled); 6 | extern void MHZ19_SetRange5000PPM(void); 7 | #endif -------------------------------------------------------------------------------- /PROJECT SOURCE/EINK213TH_SIMPLE_TEST/zstack-lib/tl_resetter.h: -------------------------------------------------------------------------------- 1 | #ifndef TL_RESETTER_H 2 | #define TL_RESETTER_H 3 | 4 | 5 | extern void zclTouchLinkRestter_Init(uint8 task_id); 6 | extern uint16 zclTouchLinkRestter_event_loop(uint8 task_id, uint16 events); 7 | extern void zclTouchLinkRestter_HandleKeys(uint8 portAndAction, uint8 keyCode); 8 | #endif -------------------------------------------------------------------------------- /PROJECT SOURCE/EINK213TH_SIMPLE_TEST/Source/spi.h: -------------------------------------------------------------------------------- 1 | #ifndef SPI_H 2 | #define SPI_H 3 | 4 | void spi_ConfigIO(void); 5 | void spi_ConfigSPI(void); 6 | void spi_ConfigGP(void); 7 | void spi_HW_WaitUs(uint16 i); 8 | 9 | extern void SPIInit(void); 10 | extern void HalLcd_HW_Init(void); 11 | extern void HalLcd_HW_Control(uint8 cmd); 12 | extern void HalLcd_HW_Write(uint8 data); 13 | 14 | #endif -------------------------------------------------------------------------------- /PROJECT SOURCE/EINK213TH_SIMPLE_TEST/CC2530DB/GenericApp.eww: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | $WS_DIR$\GenericApp.ewp 5 | 6 | 7 | 8 | ALL 9 | 10 | GenericApp 11 | EINK213TH 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /PROJECT SOURCE/EINK213TH_SIMPLE_TEST/Source/version.c: -------------------------------------------------------------------------------- 1 | 2 | #ifndef ZCL_APP_VERSION_H 3 | #define ZCL_APP_VERSION_H 4 | 5 | #ifdef __cplusplus 6 | extern "C" { 7 | #endif 8 | 9 | #include "version.h" 10 | const uint8 zclApp_DateCode[] = { 16, '1', '6', '/', '0', '2', '/', '2', '0', '2', '2', ' ', '0', '9', ':', '2', '6' }; 11 | const char zclApp_DateCodeNT[] = "16/02/2022 09:26"; 12 | 13 | #ifdef __cplusplus 14 | } 15 | #endif 16 | 17 | #endif /* ZCL_APP_VERSION_H */ 18 | -------------------------------------------------------------------------------- /PROJECT SOURCE/EINK213TH_SIMPLE_TEST/zstack-lib/utils.c: -------------------------------------------------------------------------------- 1 | #include "utils.h" 2 | #include "hal_adc.h" 3 | 4 | // #define MAX(x, y) (((x) > (y)) ? (x) : (y)) 5 | // #define MIN(x, y) (((x) < (y)) ? (x) : (y)) 6 | 7 | double mapRange(double a1, double a2, double b1, double b2, double s) { 8 | double result = b1 + (s - a1) * (b2 - b1) / (a2 - a1); 9 | return MIN(b2, MAX(result, b1)); 10 | } 11 | 12 | uint16 adcReadSampled(uint8 channel, uint8 resolution, uint8 reference, uint8 samplesCount) { 13 | HalAdcSetReference(reference); 14 | uint32 samplesSum = 0; 15 | for (uint8 i = 0; i < samplesCount; i++) { 16 | samplesSum += HalAdcRead(channel, resolution); 17 | } 18 | return samplesSum /samplesCount; 19 | } 20 | -------------------------------------------------------------------------------- /PROJECT SOURCE/EINK213TH_SIMPLE_TEST/zstack-lib/battery.h: -------------------------------------------------------------------------------- 1 | #ifndef _BATTERY_H 2 | #define _BATTERY_H 3 | //This is custom attribute 4 | #define ATTRID_POWER_CFG_BATTERY_VOLTAGE_RAW_ADC 0x0200 5 | 6 | 7 | extern uint8 zclBattery_Voltage; 8 | extern uint8 zclBattery_PercentageRemainig; 9 | extern uint16 zclBattery_RawAdc; 10 | 11 | 12 | extern uint16 getBatteryVoltage(void); 13 | extern uint8 getBatteryVoltageZCL(uint16 millivolts); 14 | extern uint8 getBatteryRemainingPercentageZCL(uint16 millivolts); 15 | extern uint8 getBatteryRemainingPercentageZCLCR2032(uint16 volt16); 16 | 17 | extern void zclBattery_Init(uint8 task_id); 18 | extern uint16 zclBattery_event_loop(uint8 task_id, uint16 events); 19 | extern void zclBattery_HandleKeys(uint8 portAndAction, uint8 keyCode); 20 | extern void zclBattery_Report(void); 21 | #endif -------------------------------------------------------------------------------- /PROJECT SOURCE/EINK213TH_SIMPLE_TEST/Source/stdint.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef _STDINT 3 | #define _STDINT 4 | 5 | #ifndef int8_t 6 | #define int8_t int8 7 | #endif 8 | #ifndef int16_t 9 | #define int16_t int16 10 | #endif 11 | #ifndef int32_t 12 | #define int32_t int32 13 | #endif 14 | #ifndef int64_t 15 | #define int64_t int64 16 | #endif 17 | #ifndef uint8_t 18 | #define uint8_t uint8 19 | #endif 20 | #ifndef uint16_t 21 | #define uint16_t uint16 22 | #endif 23 | #ifndef uint32_t 24 | #define uint32_t uint32 25 | #endif 26 | 27 | 28 | #define S8_C(x) x 29 | #define U8_C(x) x 30 | #define S16_C(x) x 31 | #define U16_C(x) x 32 | #define S32_C(x) x 33 | #define U32_C(x) x 34 | #define S64_C(x) x 35 | #define U64_C(x) x 36 | 37 | #endif 38 | -------------------------------------------------------------------------------- /PROJECT SOURCE/EINK213TH_SIMPLE_TEST/zstack-lib/commissioning.h: -------------------------------------------------------------------------------- 1 | #ifndef commissioning_h 2 | #define commissioning_h 3 | 4 | #define APP_COMMISSIONING_CLOCK_DOWN_POLING_RATE_EVT 0x0001 5 | #define APP_COMMISSIONING_END_DEVICE_REJOIN_EVT 0x0002 6 | 7 | #define APP_COMMISSIONING_END_DEVICE_REJOIN_MAX_DELAY ((uint32)1800000) // 30 minutes 30 * 60 * 1000 8 | #define APP_COMMISSIONING_END_DEVICE_REJOIN_START_DELAY 30 * 1000 // 10 seconds 9 | #define APP_COMMISSIONING_END_DEVICE_REJOIN_BACKOFF ((float) 1.2) 10 | #define APP_COMMISSIONING_END_DEVICE_REJOIN_TRIES 5 11 | 12 | 13 | 14 | 15 | extern void zclCommissioning_Init(uint8 task_id); 16 | extern uint16 zclCommissioning_event_loop(uint8 task_id, uint16 events); 17 | extern void zclCommissioning_Sleep( uint8 allow ); 18 | extern void zclCommissioning_HandleKeys(uint8 portAndAction, uint8 keyCode); 19 | 20 | #endif -------------------------------------------------------------------------------- /PROJECT SOURCE/EINK213TH_SIMPLE_TEST/zstack-lib/stdint.h: -------------------------------------------------------------------------------- 1 | #ifndef _STDINT 2 | #define _STDINT 3 | 4 | #ifndef int8_t 5 | #define int8_t int8 6 | #endif 7 | #ifndef int16_t 8 | #define int16_t int16 9 | #endif 10 | #ifndef int32_t 11 | #define int32_t int32 12 | #endif 13 | #ifndef int64_t 14 | #define int64_t int64 15 | #endif 16 | #ifndef uint8_t 17 | #define uint8_t uint8 18 | #endif 19 | #ifndef uint16_t 20 | #define uint16_t uint16 21 | #endif 22 | #ifndef uint32_t 23 | #define uint32_t uint32 24 | #endif 25 | 26 | 27 | #define S8_C(x) x 28 | #define U8_C(x) x 29 | #define S16_C(x) x 30 | #define U16_C(x) x 31 | #define S32_C(x) x 32 | #define U32_C(x) x 33 | #define S64_C(x) x 34 | #define U64_C(x) x 35 | 36 | #endif -------------------------------------------------------------------------------- /PROJECT SOURCE/EINK213TH_SIMPLE_TEST/ver.py: -------------------------------------------------------------------------------- 1 | from datetime import datetime 2 | from os.path import dirname, join 3 | cwd = dirname(__file__) 4 | now = datetime.now() 5 | dt_string = now.strftime("%d/%m/%Y %H:%M") 6 | print("date and time =", dt_string) 7 | with open(join(cwd, './Source/version.c'), 'w') as f: 8 | chars = ["'{0}'".format(char) for char in dt_string] 9 | f.write(""" 10 | #ifndef ZCL_APP_VERSION_H 11 | #define ZCL_APP_VERSION_H 12 | 13 | #ifdef __cplusplus 14 | extern "C" { 15 | #endif 16 | 17 | """ 18 | ) 19 | f.write('#include "version.h"\n') 20 | code = """const uint8 zclApp_DateCode[] = {{ {0}, {1} }};\n""".format(len(chars), ', '.join(chars)) 21 | f.write(code) 22 | code = """const char zclApp_DateCodeNT[] = \"{0}\";\n""".format(dt_string) 23 | f.write(code) 24 | f.write(""" 25 | #ifdef __cplusplus 26 | } 27 | #endif 28 | 29 | #endif /* ZCL_APP_VERSION_H */ 30 | """) -------------------------------------------------------------------------------- /PROJECT SOURCE/EINK213TH_SIMPLE_TEST/zstack-lib/Debug.h: -------------------------------------------------------------------------------- 1 | #ifndef _DEBUG_H 2 | #define _DEBUG_H 3 | 4 | #include "hal_types.h" 5 | #include "hal_uart.h" 6 | #include 7 | #include 8 | #include 9 | 10 | #define BUFFLEN 128 11 | #define BUFFER 100 12 | 13 | #define PRINT_NUMBER_TYPE long 14 | 15 | #define PAD_RIGHT 1 16 | #define PAD_ZERO 2 17 | 18 | #define PRINT_BUF_LEN 12 19 | 20 | #define PRINT_IMMEDIATE_PRINT 0 21 | 22 | 23 | #define BYTE_TO_BINARY_PATTERN "%c%c%c%c%c%c%c%c" 24 | #define BYTE_TO_BINARY(byte) \ 25 | (byte & 0x80 ? '1' : '0'), \ 26 | (byte & 0x40 ? '1' : '0'), \ 27 | (byte & 0x20 ? '1' : '0'), \ 28 | (byte & 0x10 ? '1' : '0'), \ 29 | (byte & 0x08 ? '1' : '0'), \ 30 | (byte & 0x04 ? '1' : '0'), \ 31 | (byte & 0x02 ? '1' : '0'), \ 32 | (byte & 0x01 ? '1' : '0') 33 | 34 | extern halUARTCfg_t halUARTConfig; 35 | 36 | void vprint(const char *fmt, va_list argp); 37 | extern bool DebugInit(void); 38 | extern void LREP(char *format, ...); 39 | extern void LREPMaster(uint8 *data); 40 | #endif -------------------------------------------------------------------------------- /PROJECT SOURCE/EINK213TH_SIMPLE_TEST/zstack-lib/factory_reset.h: -------------------------------------------------------------------------------- 1 | #ifndef FACTORY_RESET_H 2 | #define FACTORY_RESET_H 3 | 4 | #define FACTORY_RESET_EVT 0x1000 5 | #define FACTORY_BOOTCOUNTER_RESET_EVT 0x2000 6 | 7 | #ifndef FACTORY_RESET_HOLD_TIME_LONG 8 | #define FACTORY_RESET_HOLD_TIME_LONG ((uint32)10 * 1000) 9 | #endif 10 | 11 | #ifndef FACTORY_RESET_HOLD_TIME_FAST 12 | #define FACTORY_RESET_HOLD_TIME_FAST 1000 13 | #endif 14 | 15 | #ifndef FACTORY_RESET_BOOTCOUNTER_MAX_VALUE 16 | #define FACTORY_RESET_BOOTCOUNTER_MAX_VALUE 5 17 | #endif 18 | 19 | #ifndef FACTORY_RESET_BOOTCOUNTER_RESET_TIME 20 | #define FACTORY_RESET_BOOTCOUNTER_RESET_TIME 10 * 1000 21 | #endif 22 | 23 | #ifndef FACTORY_RESET_BY_LONG_PRESS 24 | #define FACTORY_RESET_BY_LONG_PRESS TRUE 25 | #endif 26 | 27 | 28 | #ifndef FACTORY_RESET_BY_LONG_PRESS_PORT 29 | #define FACTORY_RESET_BY_LONG_PRESS_PORT 0x00 30 | #endif 31 | 32 | 33 | #ifndef FACTORY_RESET_BY_BOOT_COUNTER 34 | #define FACTORY_RESET_BY_BOOT_COUNTER TRUE 35 | #endif 36 | 37 | extern void zclFactoryResetter_Init(uint8 task_id); 38 | extern uint16 zclFactoryResetter_loop(uint8 task_id, uint16 events); 39 | extern void zclFactoryResetter_HandleKeys(uint8 portAndAction, uint8 keyCode); 40 | #endif -------------------------------------------------------------------------------- /PROJECT SOURCE/EINK213TH_SIMPLE_TEST/Source/SHTC3.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************************** 2 | SHTC3 sensor lightweight driver 3 | Written by Andrew Lamchenko, November, 2021. 4 | **************************************************************************************************/ 5 | 6 | #ifndef SHTC3_H 7 | #define SHTC3_H 8 | 9 | #define SENSOR_ADDR 0x70 10 | 11 | #define SHTC3_ADDR_READ (0x70 << 1) | 0x01 12 | #define SHTC3_ADDR_WRITE (0x70 << 1) | 0x00 13 | 14 | #define SHTC3_CMD_WAKEUP 0x1735 15 | #define SHTC3_CMD_SLEEP 0x98B0 16 | #define SHTC3_CMD_SOFT_RESET 0x5D80 17 | 18 | 19 | #define SHTC3_CMD_NORM_READ_TEMP_FIRST 0x6678 20 | #define SHTC3_CMD_NORM_READ_TEMP_FIRST_LOW_POWER 0x1A40 21 | 22 | #define CRC_POLYNOMIAL 0x31 23 | 24 | typedef uint32_t shtc3Error; 25 | 26 | enum sht3cError_e { 27 | NO_ERROR = 0, 28 | CHECKSUM_ERROR = 1, 29 | }; 30 | 31 | extern void wakeup_sensor(void); 32 | extern void soft_reset_sensor(void); 33 | extern void sleep_sensor(void); 34 | extern void getTempHumi(float* temp, float* hum); 35 | 36 | void SHTC3_WaitUs(uint16 microSecs); 37 | void SHTC3_WaitMs(uint32_t period); 38 | 39 | #endif -------------------------------------------------------------------------------- /PROJECT SOURCE/EINK213TH_SIMPLE_TEST/zstack-lib/senseair.c: -------------------------------------------------------------------------------- 1 | #include "senseair.h" 2 | #include "Debug.h" 3 | #include "OSAL.h" 4 | #include "OnBoard.h" 5 | #include "hal_uart.h" 6 | #include "hal_led.h" 7 | 8 | #ifndef CO2_UART_PORT 9 | #define CO2_UART_PORT HAL_UART_PORT_1 10 | #endif 11 | 12 | #define SENSEAIR_RESPONSE_LENGTH 13 13 | 14 | uint8 readCO2[] = {0xFE, 0x04, 0x00, 0x00, 0x00, 0x04, 0xE5, 0xC6}; 15 | uint8 disableABC[] = {0xFE, 0x06, 0x00, 0x1F, 0x00, 0x00, 0xAC, 0x03}; 16 | uint8 enableABC[] = {0xFE, 0x60, 0x00, 0x1F, 0x00, 0xB4, 0xAC, 0x74}; 17 | 18 | void SenseAir_SetABC(bool isEnabled) { 19 | if (isEnabled) { 20 | HalUARTWrite(CO2_UART_PORT, enableABC, sizeof(enableABC) / sizeof(enableABC[0])); 21 | } else { 22 | HalUARTWrite(CO2_UART_PORT, disableABC, sizeof(disableABC) / sizeof(disableABC[0])); 23 | } 24 | } 25 | 26 | void SenseAir_RequestMeasure(void) { 27 | HalUARTWrite(CO2_UART_PORT, readCO2, sizeof(readCO2) / sizeof(readCO2[0])); 28 | } 29 | uint16 SenseAir_Read(void) { 30 | 31 | uint8 response[SENSEAIR_RESPONSE_LENGTH]; 32 | HalUARTRead(CO2_UART_PORT, (uint8 *)&response, sizeof(response) / sizeof(response[0])); 33 | 34 | if (response[0] != 0xFE || response[1] != 0x04) { 35 | LREPMaster("Invalid response\r\n"); 36 | HalLedSet(HAL_LED_ALL, HAL_LED_MODE_FLASH); 37 | return 0; 38 | } 39 | 40 | const uint8 length = response[2]; 41 | const uint16 status = (((uint16)response[3]) << 8) | response[4]; 42 | const uint16 ppm = (((uint16)response[length + 1]) << 8) | response[length + 2]; 43 | 44 | LREP("SenseAir Received CO₂=%d ppm Status=0x%X\r\n", ppm, status); 45 | 46 | return ppm; 47 | } -------------------------------------------------------------------------------- /PROJECT SOURCE/EINK213TH_SIMPLE_TEST/Source/imagedata.h: -------------------------------------------------------------------------------- 1 | 2 | extern const unsigned char LOGO[]; 3 | extern const unsigned char ESPEC[]; 4 | extern const unsigned char ZERO[]; 5 | extern const unsigned char ONE[]; 6 | extern const unsigned char TWO[]; 7 | extern const unsigned char THREE[]; 8 | extern const unsigned char FOUR[]; 9 | extern const unsigned char FIVE[]; 10 | extern const unsigned char SIX[]; 11 | extern const unsigned char SEVEN[]; 12 | extern const unsigned char EIGHT[]; 13 | extern const unsigned char NINE[]; 14 | 15 | extern const unsigned char ZERO_S[]; 16 | extern const unsigned char ONE_S[]; 17 | extern const unsigned char TWO_S[]; 18 | extern const unsigned char THREE_S[]; 19 | extern const unsigned char FOUR_S[]; 20 | extern const unsigned char FIVE_S[]; 21 | extern const unsigned char SIX_S[]; 22 | extern const unsigned char SEVEN_S[]; 23 | extern const unsigned char EIGHT_S[]; 24 | extern const unsigned char NINE_S[]; 25 | 26 | extern const unsigned char CELSIUS[]; 27 | extern const unsigned char PERCENT[]; 28 | extern const unsigned char POINT[]; 29 | 30 | extern const unsigned char IMAGE_UP[]; 31 | extern const unsigned char IMAGE_DOWN[]; 32 | 33 | extern const unsigned char bz[]; 34 | extern const unsigned char b0[]; 35 | extern const unsigned char b13[]; 36 | extern const unsigned char b25[]; 37 | extern const unsigned char b38[]; 38 | extern const unsigned char b50[]; 39 | extern const unsigned char b63[]; 40 | extern const unsigned char b75[]; 41 | extern const unsigned char b87[]; 42 | extern const unsigned char b100[]; 43 | 44 | extern const unsigned char I1[]; 45 | extern const unsigned char I2[]; 46 | 47 | extern const unsigned char IMAGE_ONNETWORK[]; 48 | extern const unsigned char IMAGE_OFFNETWORK[]; 49 | -------------------------------------------------------------------------------- /PROJECT SOURCE/EINK213TH_SIMPLE_TEST/Source/epdpaint.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef EPDPAINT_H 3 | #define EPDPAINT_H 4 | 5 | // Display orientation 6 | #define ROTATE_0 0 7 | #define ROTATE_90 1 8 | #define ROTATE_180 2 9 | #define ROTATE_270 3 10 | 11 | // Color inverse. 1 or 0 = set or reset a bit if set a colored pixel 12 | #define IF_INVERT_COLOR 1 13 | 14 | #define COLORED 0 15 | #define UNCOLORED 1 16 | 17 | extern void PaintPaint(unsigned char* image, int width, int height); 18 | extern void PaintClear(int colored); 19 | extern int PaintGetWidth(void); 20 | extern void PaintSetWidth(int width); 21 | extern int PaintGetHeight(void); 22 | extern void PaintSetHeight(int height); 23 | int GetRotate(void); 24 | extern void PaintSetRotate(int rotate); 25 | extern unsigned char* PaintGetImage(void); 26 | extern void PaintDrawAbsolutePixel(int x, int y, int colored); 27 | extern void PaintDrawPixel(int x, int y, int colored); 28 | extern void PaintDrawLine(int x0, int y0, int x1, int y1, int colored); 29 | extern void PaintDrawHorizontalLine(int x, int y, int width, int colored); 30 | extern void PaintDrawVerticalLine(int x, int y, int height, int colored); 31 | extern void PaintDrawRectangle(int x0, int y0, int x1, int y1, int colored); 32 | extern void PaintDrawFilledRectangle(int x0, int y0, int x1, int y1, int colored); 33 | extern void PaintDrawCircle(int x, int y, int radius, int colored); 34 | extern void PaintDrawFilledCircle(int x, int y, int radius, int colored); 35 | extern void PaintDrawImage(const unsigned char* imgData,int x, int y, int Width, int Height, int colored); 36 | 37 | //extern unsigned char* image[896]; 38 | extern unsigned char* image[672]; 39 | extern int pwidth; 40 | extern int pheight; 41 | extern int protate; 42 | 43 | #endif -------------------------------------------------------------------------------- /PROJECT SOURCE/EINK213TH_SIMPLE_TEST/Source/OSAL_App.c: -------------------------------------------------------------------------------- 1 | #include "OSAL.h" 2 | #include "OSAL_Tasks.h" 3 | #include "ZComDef.h" 4 | #include "hal_drivers.h" 5 | 6 | #include "APS.h" 7 | #include "ZDApp.h" 8 | #include "nwk.h" 9 | 10 | #include "bdb_interface.h" 11 | #include "zcl_app.h" 12 | #include "factory_reset.h" 13 | #include "commissioning.h" 14 | #include "Debug.h" 15 | 16 | #if defined ( MT_TASK ) 17 | #include "MT.h" 18 | #include "MT_TASK.h" 19 | #endif 20 | 21 | const pTaskEventHandlerFn tasksArr[] = {macEventLoop, 22 | nwk_event_loop, 23 | Hal_ProcessEvent, 24 | #if defined( MT_TASK ) 25 | MT_ProcessEvent, 26 | #endif 27 | APS_event_loop, 28 | ZDApp_event_loop, 29 | zcl_event_loop, 30 | bdb_event_loop, 31 | zclApp_event_loop, 32 | zclFactoryResetter_loop, 33 | zclCommissioning_event_loop 34 | }; 35 | 36 | const uint8 tasksCnt = sizeof(tasksArr) / sizeof(tasksArr[0]); 37 | uint16 *tasksEvents; 38 | 39 | void osalInitTasks(void) { 40 | DebugInit(); 41 | uint8 taskID = 0; 42 | 43 | tasksEvents = (uint16 *)osal_mem_alloc(sizeof(uint16) * tasksCnt); 44 | osal_memset(tasksEvents, 0, (sizeof(uint16) * tasksCnt)); 45 | macTaskInit(taskID++); 46 | nwk_init(taskID++); 47 | Hal_Init(taskID++); 48 | #if defined( MT_TASK ) 49 | MT_TaskInit( taskID++ ); 50 | #endif 51 | APS_Init(taskID++); 52 | ZDApp_Init(taskID++); 53 | zcl_Init(taskID++); 54 | bdb_Init(taskID++); 55 | zclApp_Init(taskID++); 56 | zclFactoryResetter_Init(taskID++); 57 | zclCommissioning_Init(taskID++); 58 | } 59 | 60 | /********************************************************************* 61 | *********************************************************************/ 62 | -------------------------------------------------------------------------------- /PROJECT SOURCE/EINK213TH_SIMPLE_TEST/zstack-lib/inttempsens.c: -------------------------------------------------------------------------------- 1 | #include "inttempsens.h" 2 | 3 | #define HAL_ADC_EOC 0x80 // End of Conversion bit 4 | #define HAL_ADC_START 0x40 // Starts Conversion 5 | #define HAL_ADC_RESOLUTION_8 0x01 6 | #define HAL_ADC_RESOLUTION_10 0x02 7 | #define HAL_ADC_RESOLUTION_12 0x03 8 | #define HAL_ADC_RESOLUTION_14 0x04 9 | #define HAL_ADC_CHN_VDD3 0x0f // VDD/3 10 | #define HAL_ADC_REF_125V 0x00 // Internal Reference (1.15V for CC2530) 11 | 12 | #define HAL_ADC_REF_115V 0x00 13 | #define HAL_ADC_DEC_256 0x20 14 | #define HAL_ADC_CHN_TEMP 0x0e /* Temperature sensor */ 15 | 16 | #define HAL_ADC_DEC_064 0x00 /* Decimate by 64 : 8-bit resolution */ 17 | #define HAL_ADC_DEC_128 0x10 /* Decimate by 128 : 10-bit resolution */ 18 | #define HAL_ADC_DEC_256 0x20 /* Decimate by 256 : 12-bit resolution */ 19 | #define HAL_ADC_DEC_512 0x30 /* Decimate by 512 : 14-bit resolution */ 20 | 21 | 22 | 23 | 24 | int16 readTemperature(void) { 25 | 26 | 27 | float temperature; 28 | uint16 value; 29 | 30 | ATEST = 0x01; 31 | TR0 |= 0x01; 32 | ADCIF = 0; 33 | 34 | 35 | //ADCCON3 = (HAL_ADC_REF_125V | HAL_ADC_DEC_256 | HAL_ADC_CHN_TEMP); 36 | ADCCON3 = 0x3E; // temp 37 | while(!ADCIF); 38 | ADCIF=0; 39 | value=ADCL; 40 | value|=((uint16) ADCH)<<8; 41 | value >>=4; 42 | 43 | 44 | 45 | //temperature = ((((float)value) * 1150.0 / 2047.0 - 743.0) / 2.45); 46 | temperature = (((float)value)-1367.5)/4.5+5.5; 47 | 48 | return (int16)(temperature * 100); 49 | 50 | /* 51 | static uint16 reference_voltage; 52 | static uint8 bCalibrate=TRUE; 53 | uint16 value; 54 | uint16 temperature; 55 | ATEST=0x01; 56 | TR0|=0x01; 57 | ADCIF=0; 58 | ADCCON3=(HAL_ADC_REF_125V|HAL_ADC_DEC_256|HAL_ADC_CHN_TEMP); 59 | while(!ADCIF); 60 | ADCIF=0; 61 | value=ADCL; 62 | value|=((uint16) ADCH)<<8; 63 | value >>=4; 64 | if(bCalibrate) 65 | { 66 | reference_voltage=value; 67 | bCalibrate=FALSE; 68 | } 69 | temperature=22+((value-reference_voltage)/4+1); //?????? 70 | return (int16)(temperature * 100); 71 | */ 72 | } 73 | -------------------------------------------------------------------------------- /PROJECT SOURCE/EINK213TH_SIMPLE_TEST/zstack-lib/Debug.c: -------------------------------------------------------------------------------- 1 | #include "Debug.h" 2 | #include "DebugTrace.h" 3 | #include "MT.h" 4 | #include "OSAL.h" 5 | #include "OSAL_Memory.h" 6 | 7 | 8 | void vprint(const char *fmt, va_list argp) { 9 | uint8 string[100]; 10 | if (0 < vsprintf((char *)string, fmt, argp)) // build string 11 | { 12 | LREPMaster(string); 13 | } 14 | } 15 | 16 | #ifdef DO_DEBUG_UART 17 | #define UART_PORT HAL_UART_PORT_0 18 | bool DebugInit() { 19 | halUARTCfg_t halUARTConfig; 20 | halUARTConfig.configured = TRUE; 21 | halUARTConfig.baudRate = HAL_UART_BR_115200; 22 | halUARTConfig.flowControl = FALSE; 23 | halUARTConfig.flowControlThreshold = 48; // this parameter indicates number of bytes left before Rx Buffer 24 | // reaches maxRxBufSize 25 | halUARTConfig.idleTimeout = 10; // this parameter indicates rx timeout period in millisecond 26 | halUARTConfig.rx.maxBufSize = 0; 27 | halUARTConfig.tx.maxBufSize = BUFFLEN; 28 | halUARTConfig.intEnable = TRUE; 29 | halUARTConfig.callBackFunc = NULL; 30 | HalUARTInit(); 31 | if (HalUARTOpen(UART_PORT, &halUARTConfig) == HAL_UART_SUCCESS) { 32 | LREPMaster("Initialized debug module \r\n"); 33 | return true; 34 | } 35 | return false; 36 | } 37 | 38 | void LREPMaster(uint8 *data) { 39 | if (data == NULL) { 40 | return; 41 | } 42 | HalUARTWrite(UART_PORT, data, osal_strlen((char *)data)); 43 | } 44 | 45 | void LREP(char *format, ...) { 46 | va_list argp; 47 | va_start(argp, format); 48 | vprint(format, argp); 49 | va_end(argp); 50 | } 51 | #elif defined(DO_DEBUG_MT) 52 | 53 | bool DebugInit() { 54 | debugThreshold = 0x04; // increase threshold as soon as we initialize debug module 55 | LREPMaster("Initialized debug module \r\n"); 56 | return TRUE; 57 | } 58 | void LREP(char *format, ...) { 59 | 60 | va_list argp; 61 | va_start(argp, format); 62 | vprint(format, argp); 63 | va_end(argp); 64 | } 65 | void LREPMaster(uint8 *data) { debug_str(data); } 66 | #else 67 | bool DebugInit() {return true;}; 68 | void LREP(char *format, ...) { 69 | va_list argp; 70 | va_start(argp, format); 71 | printf(format, argp); 72 | va_end(argp); 73 | }; 74 | void LREPMaster(uint8 *data) { 75 | printf((const char*)data); 76 | }; 77 | #endif 78 | -------------------------------------------------------------------------------- /PROJECT SOURCE/EINK213TH_SIMPLE_TEST/zstack-lib/mhz19.c: -------------------------------------------------------------------------------- 1 | #include "mhz19.h" 2 | #include "Debug.h" 3 | #include "OSAL.h" 4 | #include "OnBoard.h" 5 | #include "hal_led.h" 6 | #include "hal_uart.h" 7 | 8 | #ifndef CO2_UART_PORT 9 | #define CO2_UART_PORT HAL_UART_PORT_1 10 | #endif 11 | 12 | #define MHZ18_RESPONSE_LENGTH 13 13 | 14 | uint8 MHZ19_RESPONSE_LENGTH = 9; 15 | uint8 MHZ19_COMMAND_GET_PPM[] = {0xFF, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79}; 16 | uint8 MHZ19_COMMAND_ABC_ENABLE[] = {0xFF, 0x01, 0x79, 0xA0, 0x00, 0x00, 0x00, 0x00, 0xE6}; 17 | uint8 MHZ19_COMMAND_ABC_DISABLE[] = {0xFF, 0x01, 0x79, 0x00, 0x00, 0x00, 0x00, 0x00, 0x86}; 18 | static uint8 MHZ19_COMMAND_SET_RANGE_5000PPM[] = {0xFF, 0x01, 0x99, 0x00, 0x00, 0x00, 0x13, 0x88, 0xCB}; 19 | 20 | static void flushUART(void); 21 | 22 | static void flushUART(void) { 23 | uint8 response; 24 | while (Hal_UART_RxBufLen(CO2_UART_PORT) > 0) { 25 | HalUARTRead(CO2_UART_PORT, (uint8 *)&response, 1); 26 | } 27 | } 28 | void MHZ19_SetRange5000PPM(void) { 29 | flushUART(); 30 | HalUARTWrite(CO2_UART_PORT, MHZ19_COMMAND_SET_RANGE_5000PPM, sizeof(MHZ19_COMMAND_SET_RANGE_5000PPM) / sizeof(MHZ19_COMMAND_SET_RANGE_5000PPM[0])); 31 | } 32 | 33 | void MHZ19_SetABC(bool isEnabled) { 34 | flushUART(); 35 | if (isEnabled) { 36 | HalUARTWrite(CO2_UART_PORT, MHZ19_COMMAND_ABC_ENABLE, sizeof(MHZ19_COMMAND_ABC_ENABLE) / sizeof(MHZ19_COMMAND_ABC_ENABLE[0])); 37 | } else { 38 | HalUARTWrite(CO2_UART_PORT, MHZ19_COMMAND_ABC_DISABLE, sizeof(MHZ19_COMMAND_ABC_DISABLE) / sizeof(MHZ19_COMMAND_ABC_DISABLE[0])); 39 | } 40 | } 41 | 42 | void MHZ19_RequestMeasure(void) { 43 | flushUART(); 44 | HalUARTWrite(CO2_UART_PORT, MHZ19_COMMAND_GET_PPM, sizeof(MHZ19_COMMAND_GET_PPM) / sizeof(MHZ19_COMMAND_GET_PPM[0])); 45 | } 46 | uint16 MHZ19_Read(void) { 47 | 48 | uint8 response[MHZ18_RESPONSE_LENGTH]; 49 | HalUARTRead(CO2_UART_PORT, (uint8 *)&response, sizeof(response) / sizeof(response[0])); 50 | 51 | if (response[0] != 0xFF || response[1] != 0x86) { 52 | LREPMaster("MHZ18 Invalid response\r\n"); 53 | HalLedSet(HAL_LED_ALL, HAL_LED_MODE_FLASH); 54 | return 0; 55 | } 56 | 57 | const uint16 ppm = (((uint16)response[2]) << 8) | response[3]; 58 | const int temp = ((int)response[4]) - 40; 59 | const uint8 status = response[5]; 60 | 61 | LREP("MHZ18 Received CO₂=%d ppm Status=0x%X temp=%d\r\n", ppm, status, temp); 62 | 63 | return ppm; 64 | } -------------------------------------------------------------------------------- /PROJECT SOURCE/EINK213TH_SIMPLE_TEST/Source/preinclude.h: -------------------------------------------------------------------------------- 1 | #define TC_LINKKEY_JOIN 2 | #define NV_INIT 3 | #define NV_RESTORE 4 | 5 | #define HAL_LCD FALSE 6 | 7 | #define TP2_LEGACY_ZC 8 | //patch sdk 9 | // #define ZDSECMGR_TC_ATTEMPT_DEFAULT_KEY TRUE 10 | 11 | #define NWK_AUTO_POLL 12 | #define MULTICAST_ENABLED FALSE 13 | 14 | #define ZCL_READ 15 | #define ZCL_BASIC 16 | #define ZCL_IDENTIFY 17 | #define ZCL_REPORTING_DEVICE 18 | 19 | #define ZSTACK_DEVICE_BUILD (DEVICE_BUILD_ENDDEVICE) 20 | 21 | #define DISABLE_GREENPOWER_BASIC_PROXY 22 | #define BDB_FINDING_BINDING_CAPABILITY_ENABLED 1 23 | #define BDB_REPORTING TRUE 24 | 25 | 26 | #define ISR_KEYINTERRUPT 27 | #define HAL_BUZZER FALSE 28 | 29 | #define HAL_LED TRUE 30 | #define HAL_I2C TRUE 31 | #define BLINK_LEDS TRUE 32 | 33 | 34 | #if !defined(EINK213TH) 35 | #error "Board type must be defined" 36 | #endif 37 | 38 | #define BDB_MAX_CLUSTERENDPOINTS_REPORTING 10 39 | 40 | //spi 41 | #define HAL_LCD_MODE_PORT 0 42 | #define HAL_LCD_MODE_PIN 0 // EPD DC 43 | #define HAL_LCD_RESET_PORT 1 44 | #define HAL_LCD_RESET_PIN 1 // EPD RST 45 | #define HAL_LCD_CS_PORT 0 46 | #define HAL_LCD_CS_PIN 7 // BME280 CS 47 | #define HAL_LCD_BUSY_PORT 0 48 | #define HAL_LCD_BUSY_PIN 4 // EPD BUSY 49 | 50 | 51 | //i2c 52 | #define OCM_CLK_PORT 0 53 | #define OCM_DATA_PORT 0 54 | #define OCM_CLK_PIN 5 55 | #define OCM_DATA_PIN 6 56 | 57 | #define HAL_I2C_RETRY_CNT 1 58 | 59 | #define POWER_SAVING 60 | 61 | //#define DO_DEBUG_UART 62 | //#define DO_DEBUG_MT 63 | 64 | 65 | #ifdef DO_DEBUG_UART 66 | #define HAL_UART TRUE 67 | #define HAL_UART_DMA 1 68 | #define INT_HEAP_LEN (2685 - 0x4B - 0xBB) 69 | #endif 70 | 71 | #ifdef DO_DEBUG_MT 72 | #define HAL_UART TRUE 73 | #define HAL_UART_DMA 1 74 | #define HAL_UART_ISR 2 75 | #define INT_HEAP_LEN (2688-0xC4-0x15-0x44-0x20-0x1E) 76 | 77 | #define MT_TASK 78 | 79 | #define MT_UTIL_FUNC 80 | #define MT_UART_DEFAULT_BAUDRATE HAL_UART_BR_115200 81 | #define MT_UART_DEFAULT_OVERFLOW FALSE 82 | 83 | #define ZTOOL_P1 84 | 85 | #define MT_APP_FUNC 86 | #define MT_APP_CNF_FUNC 87 | #define MT_SYS_FUNC 88 | #define MT_ZDO_FUNC 89 | #define MT_ZDO_MGMT 90 | #define MT_DEBUG_FUNC 91 | 92 | #endif 93 | 94 | 95 | 96 | #if defined(EINK213TH) 97 | #define HAL_KEY_P2_INPUT_PINS BV(0) 98 | #endif 99 | 100 | #include "hal_board_cfg.h" 101 | 102 | #include "stdint.h" 103 | #include "stddef.h" 104 | -------------------------------------------------------------------------------- /PROJECT SOURCE/EINK213TH_SIMPLE_TEST/Source/zcl_app.h: -------------------------------------------------------------------------------- 1 | #ifndef ZCL_APP_H 2 | #define ZCL_APP_H 3 | 4 | #ifdef __cplusplus 5 | extern "C" { 6 | #endif 7 | 8 | /********************************************************************* 9 | * INCLUDES 10 | */ 11 | #include "version.h" 12 | #include "zcl.h" 13 | 14 | 15 | /********************************************************************* 16 | * CONSTANTS 17 | */ 18 | 19 | // Application Events 20 | #define APP_REPORT_EVT 0x0001 21 | #define APP_READ_SENSORS_EVT 0x0002 22 | 23 | #define APP_REPORT_DELAY ((uint32) 60000) //60 sec 24 | 25 | /********************************************************************* 26 | * MACROS 27 | */ 28 | 29 | #define R ACCESS_CONTROL_READ 30 | #define RR (R | ACCESS_REPORTABLE) 31 | 32 | #define BASIC ZCL_CLUSTER_ID_GEN_BASIC 33 | #define POWER_CFG ZCL_CLUSTER_ID_GEN_POWER_CFG 34 | #define TEMP ZCL_CLUSTER_ID_MS_TEMPERATURE_MEASUREMENT 35 | #define HUMIDITY ZCL_CLUSTER_ID_MS_RELATIVE_HUMIDITY 36 | 37 | #define ZCL_UINT8 ZCL_DATATYPE_UINT8 38 | #define ZCL_UINT16 ZCL_DATATYPE_UINT16 39 | #define ZCL_UINT32 ZCL_DATATYPE_UINT32 40 | #define ZCL_INT16 ZCL_DATATYPE_INT16 41 | #define ZCL_INT8 ZCL_DATATYPE_INT8 42 | 43 | 44 | /********************************************************************* 45 | * TYPEDEFS 46 | */ 47 | 48 | /********************************************************************* 49 | * VARIABLES 50 | */ 51 | 52 | extern SimpleDescriptionFormat_t zclApp_FirstEP; 53 | 54 | extern uint8 zclApp_BatteryVoltage; 55 | extern uint8 zclApp_BatteryPercentageRemainig; 56 | extern uint16 zclApp_BatteryVoltageRawAdc; 57 | extern int16 zclApp_Temperature_Sensor_MeasuredValue; 58 | extern uint16 zclApp_HumiditySensor_MeasuredValue; 59 | 60 | // attribute list 61 | extern CONST zclAttrRec_t zclApp_AttrsFirstEP[]; 62 | extern CONST uint8 zclApp_AttrsFirstEPCount; 63 | 64 | extern const uint8 zclApp_ManufacturerName[]; 65 | extern const uint8 zclApp_ModelId[]; 66 | extern const uint8 zclApp_PowerSource; 67 | 68 | // APP_TODO: Declare application specific attributes here 69 | 70 | /********************************************************************* 71 | * FUNCTIONS 72 | */ 73 | 74 | /* 75 | * Initialization for the task 76 | */ 77 | extern void zclApp_Init(byte task_id); 78 | 79 | /* 80 | * Event Process for the task 81 | */ 82 | extern UINT16 zclApp_event_loop(byte task_id, UINT16 events); 83 | 84 | 85 | void user_delay_ms(unsigned int delaytime); 86 | 87 | #ifdef __cplusplus 88 | } 89 | #endif 90 | 91 | #endif /* ZCL_APP_H */ 92 | -------------------------------------------------------------------------------- /PROJECT SOURCE/EINK213TH_SIMPLE_TEST/zstack-lib/tl_resetter.c: -------------------------------------------------------------------------------- 1 | #include "tl_resetter.h" 2 | #include "Debug.h" 3 | #include "Osal_Memory.h" 4 | #include "bdb.h" 5 | #include "bdb_interface.h" 6 | #include "bdb_touchlink.h" 7 | #include "bdb_touchlink_initiator.h" 8 | #include "hal_key.h" 9 | #include "hal_led.h" 10 | 11 | #ifndef TL_RESETTER_TRIGGER_KEY 12 | #define TL_RESETTER_TRIGGER_KEY 2 13 | #endif 14 | 15 | 16 | #ifndef TL_RESETTERL_START_DELAY 17 | #define TL_RESETTERL_START_DELAY 5 * 1000 18 | #endif 19 | 20 | #ifndef TL_RESETTERL_REPEAT_DELAY 21 | #define TL_RESETTERL_REPEAT_DELAY 1 * 1000 22 | #endif 23 | 24 | #ifndef TL_RESETTER_ATTEMPTS_COUNT 25 | #define TL_RESETTER_ATTEMPTS_COUNT 10 26 | #endif 27 | 28 | #define TL_RESETTER_START_TL_EVT 0x0001 29 | #define TL_RESETTER_RETRY_TL_EVT 0x0002 30 | 31 | static void zclTouchLinkResetter_StartTL(void); 32 | static ZStatus_t zclTouchLinkResetter_TL_NotifyCb(epInfoRec_t *pData); 33 | 34 | 35 | uint8 zclTouchLinkResetter_TaskId = 0; 36 | uint8 zclTouchLinkResetter_CurrentAttempt = 0; 37 | 38 | static void zclTouchLinkResetter_StartTL(void) { 39 | LREP("zclTouchLinkResetter_StartTL attempt=%d\r\n", zclTouchLinkResetter_CurrentAttempt); 40 | HalLedSet(HAL_LED_1, HAL_LED_MODE_FLASH); 41 | if (zclTouchLinkResetter_CurrentAttempt < TL_RESETTER_ATTEMPTS_COUNT) { 42 | touchLinkInitiator_StartDevDisc(); 43 | zclTouchLinkResetter_CurrentAttempt += 1; 44 | osal_start_timerEx(zclTouchLinkResetter_TaskId, TL_RESETTER_RETRY_TL_EVT, TL_RESETTERL_REPEAT_DELAY); 45 | } 46 | } 47 | 48 | ZStatus_t zclTouchLinkResetter_TL_NotifyCb(epInfoRec_t *pData) { 49 | LREPMaster("zclTouchLinkResetter_TL_NotifyCb\r\n"); 50 | osal_stop_timerEx(zclTouchLinkResetter_TaskId, TL_RESETTER_RETRY_TL_EVT); 51 | touchLinkInitiator_ResetToFNSelectedTarget(); 52 | HalLedSet(HAL_LED_1, HAL_LED_MODE_OFF); 53 | return ZSuccess; 54 | } 55 | 56 | void zclTouchLinkRestter_Init(uint8 task_id) { 57 | zclTouchLinkResetter_TaskId = task_id; 58 | touchLinkInitiator_RegisterNotifyTLCB(zclTouchLinkResetter_TL_NotifyCb); 59 | } 60 | 61 | uint16 zclTouchLinkRestter_event_loop(uint8 task_id, uint16 events) { 62 | LREP("zclTouchLinkRestter_event_loop 0x%X\r\n", events); 63 | if (events & TL_RESETTER_START_TL_EVT) { 64 | zclTouchLinkResetter_CurrentAttempt = 0; 65 | zclTouchLinkResetter_StartTL(); 66 | return (events ^ TL_RESETTER_START_TL_EVT); 67 | } 68 | 69 | if (events & TL_RESETTER_RETRY_TL_EVT) { 70 | LREPMaster("TL_RESETTER_RETRY_TL_EVT\r\n"); 71 | zclTouchLinkResetter_StartTL(); 72 | return (events ^ TL_RESETTER_RETRY_TL_EVT); 73 | } 74 | 75 | return 0; 76 | } 77 | 78 | void zclTouchLinkRestter_HandleKeys(uint8 portAndAction, uint8 keyCode) { 79 | if (portAndAction & HAL_KEY_PRESS) { 80 | if (keyCode == TL_RESETTER_TRIGGER_KEY) { 81 | osal_start_timerEx(zclTouchLinkResetter_TaskId, TL_RESETTER_START_TL_EVT, TL_RESETTERL_START_DELAY); 82 | } 83 | } else { 84 | osal_stop_timerEx(zclTouchLinkResetter_TaskId, TL_RESETTER_START_TL_EVT); 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /PROJECT SOURCE/EINK213TH_SIMPLE_TEST/Source/ssd1675.h: -------------------------------------------------------------------------------- 1 | #ifndef SSD1675_H 2 | #define SSD1675_H 3 | 4 | // Display resolution 5 | #define EPD_WIDTH 122 6 | #define EPD_HEIGHT 268 7 | 8 | // EPD2IN9 commands 9 | 10 | #define DRIVER_OUTPUT_CONTROL 0x01 11 | #define GATE_VOLTAGE_CONTROL 0x03 12 | #define SOURCE_VOLTAGE_CONTROL 0x04 13 | #define BOOSTER_SOFT_START_CONTROL 0x0C 14 | #define GATE_SCAN_START_POSITION 0x0F 15 | #define DEEP_SLEEP_MODE 0x10 16 | #define DATA_ENTRY_MODE_SETTING 0x11 17 | #define SW_RESET 0x12 18 | #define TEMPERATURE_SENSOR_CONTROL 0x1A 19 | #define BUILTINTEMPERATURE_SENSOR_CONTROL 0x18 20 | #define MASTER_ACTIVATION 0x20 21 | #define DISPLAY_UPDATE_CONTROL_1 0x21 22 | #define DISPLAY_UPDATE_CONTROL_2 0x22 23 | #define WRITE_RAM 0x24 24 | #define WRITE_RAM2 0x26 25 | #define WRITE_VCOM_REGISTER 0x2C 26 | #define WRITE_LUT_REGISTER 0x32 27 | #define PROGRAM_OTP_SELECTION 0x36 28 | #define OTP_SELECTION_CONTROL_1 0x37 29 | #define OTP_SELECTION_CONTROL_2 0x38 30 | #define SET_DUMMY_LINE_PERIOD 0x3A 31 | #define SET_GATE_TIME 0x3B 32 | #define BORDER_WAVEFORM_CONTROL 0x3C 33 | #define OPTION_LUT_END 0x3F 34 | #define SET_RAM_X_ADDRESS_START_END_POSITION 0x44 35 | #define SET_RAM_Y_ADDRESS_START_END_POSITION 0x45 36 | #define SET_RAM_X_ADDRESS_COUNTER 0x4E 37 | #define SET_RAM_Y_ADDRESS_COUNTER 0x4F 38 | #define TERMINATE_FRAME_READ_WRITE 0xFF 39 | 40 | 41 | extern const unsigned char lut_full_update[159]; 42 | extern const unsigned char lut_partial_update[159]; 43 | 44 | extern unsigned long epd_width; 45 | extern unsigned long epd_height; 46 | 47 | extern void EpdInitFull(void); 48 | extern void EpdInitPartial(void); 49 | extern void EpdSendCommand(unsigned char command); 50 | extern void EpdSendData(unsigned char data); 51 | extern void WaitUntilIdle(void); 52 | extern void EpdReset(void); 53 | 54 | extern void EpdSetFrameMemoryXY(const unsigned char* image_buffer,int x, int y, int image_width, int image_height); 55 | 56 | extern void EpdSetFrameMemory(const unsigned char* image_buffer); 57 | extern void EpdSetFrameMemoryBase(const unsigned char* image_buffer); 58 | extern void EpdClearFrameMemory(unsigned char color); 59 | extern void EpdClearFrameMemoryF(unsigned char color); 60 | extern void EpdDisplayFrame(void); 61 | extern void EpdDisplayFramePartial(void); 62 | extern void EpdSleep(void); 63 | extern void EpdSetData_Image(const unsigned char* imgData, int x, int y, int Width, int Height, int colored); 64 | 65 | extern void EpdSetLut(const unsigned char *lut); 66 | extern void EpdSetLutFull(const unsigned char *lut); 67 | extern void EpdSetMemoryArea(int x_start, int y_start, int x_end, int y_end); 68 | extern void EpdSetMemoryPointer(int x, int y); 69 | 70 | #endif /* SSD1675_H */ 71 | 72 | /* END OF FILE */ -------------------------------------------------------------------------------- /PROJECT SOURCE/EINK213TH_SIMPLE_TEST/Source/SHTC3.c: -------------------------------------------------------------------------------- 1 | /************************************************************************************************** 2 | SHTC3 sensor lightweight driver 3 | Written by Andrew Lamchenko, November, 2021. 4 | **************************************************************************************************/ 5 | #include "SHTC3.h" 6 | #include "Debug.h" 7 | #include 8 | #include "hal_i2c.h" 9 | 10 | static shtc3Error SHTC3_CheckCrc(uint8_t data[], uint8_t nbrOfBytes, 11 | uint8_t checksum); 12 | 13 | void wakeup_sensor(void) 14 | { 15 | uint16_t command = SHTC3_CMD_WAKEUP; 16 | HalI2CSend(SHTC3_ADDR_WRITE, (uint8_t*)&command, 2); 17 | SHTC3_WaitMs(1); 18 | } 19 | 20 | 21 | 22 | void soft_reset_sensor(void) 23 | { 24 | uint16_t command = SHTC3_CMD_SOFT_RESET; 25 | HalI2CSend(SHTC3_ADDR_WRITE, (uint8_t*)&command, 2); 26 | } 27 | 28 | 29 | 30 | void sleep_sensor(void) 31 | { 32 | uint16_t command = SHTC3_CMD_SLEEP; 33 | HalI2CSend(SHTC3_ADDR_WRITE, (uint8_t*)&command, 2); 34 | } 35 | 36 | 37 | 38 | void getTempHumi(float* temp, float* hum) { 39 | 40 | shtc3Error error; 41 | uint8_t maxPolling = 22; 42 | uint16_t command = SHTC3_CMD_NORM_READ_TEMP_FIRST; 43 | HalI2CSend(SHTC3_ADDR_WRITE, (uint8_t*)&command, 2); 44 | 45 | uint8 getbuf[6]; 46 | uint8 checksum; 47 | 48 | while(maxPolling--) { 49 | HalI2CReceive(SHTC3_ADDR_READ, getbuf, 6); 50 | 51 | checksum = getbuf[2]; 52 | error = SHTC3_CheckCrc(&getbuf[3], 2, checksum); 53 | 54 | checksum = getbuf[5]; 55 | error = SHTC3_CheckCrc(&getbuf[3], 2, checksum); 56 | 57 | if(error == NO_ERROR) break; 58 | 59 | SHTC3_WaitMs(1); 60 | } 61 | if(error == NO_ERROR){ 62 | uint16_t raw_temp = getbuf[0] << 8 | getbuf[1]; 63 | *temp = 175 * (float)raw_temp / 65536.0f - 45.0f; 64 | 65 | uint16_t raw_hum = getbuf[3] << 8 | getbuf[4]; 66 | *hum = 100 * (float)raw_hum / 65536.0f; 67 | }else{ 68 | *temp = -100.0; 69 | *hum = 0.0; 70 | } 71 | } 72 | 73 | 74 | void SHTC3_WaitUs(uint16 microSecs) { 75 | while(microSecs--) { 76 | /* 32 NOPs == 1 usecs */ 77 | asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); 78 | asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); 79 | asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); 80 | asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); 81 | asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); 82 | asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); 83 | asm("nop"); asm("nop"); 84 | } 85 | } 86 | 87 | 88 | void SHTC3_WaitMs(uint32_t period) { SHTC3_WaitUs(period * 1000); } 89 | 90 | 91 | static shtc3Error SHTC3_CheckCrc(uint8_t data[], uint8_t numOfBytes, 92 | uint8_t checksum){ 93 | uint8_t crc = 0xFF; 94 | 95 | for(uint8_t byteCtr = 0; byteCtr < numOfBytes; byteCtr++) { 96 | crc ^= (data[byteCtr]); 97 | for(uint8_t bit = 8; bit > 0; --bit) { 98 | if(crc & 0x80) { 99 | crc = (crc << 1) ^ CRC_POLYNOMIAL; 100 | } else { 101 | crc = (crc << 1); 102 | } 103 | } 104 | } 105 | if(crc != checksum) { 106 | return CHECKSUM_ERROR; 107 | } else { 108 | return NO_ERROR; 109 | } 110 | } -------------------------------------------------------------------------------- /PROJECT SOURCE/EINK213TH_SIMPLE_TEST/zstack-lib/factory_reset.c: -------------------------------------------------------------------------------- 1 | #include "factory_reset.h" 2 | #include "AF.h" 3 | #include "Debug.h" 4 | #include "OnBoard.h" 5 | #include "bdb.h" 6 | #include "bdb_interface.h" 7 | #include "hal_led.h" 8 | #include "ZComDef.h" 9 | #include "hal_key.h" 10 | 11 | static void zclFactoryResetter_ResetToFN(void); 12 | static void zclFactoryResetter_ProcessBootCounter(void); 13 | static void zclFactoryResetter_ResetBootCounter(void); 14 | 15 | static uint8 zclFactoryResetter_TaskID; 16 | 17 | uint16 zclFactoryResetter_loop(uint8 task_id, uint16 events) { 18 | LREP("zclFactoryResetter_loop 0x%X\r\n", events); 19 | if (events & FACTORY_RESET_EVT) { 20 | LREPMaster("FACTORY_RESET_EVT\r\n"); 21 | zclFactoryResetter_ResetToFN(); 22 | return (events ^ FACTORY_RESET_EVT); 23 | } 24 | 25 | if (events & FACTORY_BOOTCOUNTER_RESET_EVT) { 26 | LREPMaster("FACTORY_BOOTCOUNTER_RESET_EVT\r\n"); 27 | zclFactoryResetter_ResetBootCounter(); 28 | return (events ^ FACTORY_BOOTCOUNTER_RESET_EVT); 29 | } 30 | return 0; 31 | } 32 | void zclFactoryResetter_ResetBootCounter(void) { 33 | uint16 bootCnt = 0; 34 | LREPMaster("Clear boot counter\r\n"); 35 | osal_nv_write(ZCD_NV_BOOTCOUNTER, 0, sizeof(bootCnt), &bootCnt); 36 | } 37 | 38 | void zclFactoryResetter_Init(uint8 task_id) { 39 | zclFactoryResetter_TaskID = task_id; 40 | /** 41 | * We can't register more than one task, call in main app taks 42 | * zclFactoryResetter_HandleKeys(portAndAction, keyCode); 43 | * */ 44 | // RegisterForKeys(task_id); 45 | #if FACTORY_RESET_BY_BOOT_COUNTER 46 | zclFactoryResetter_ProcessBootCounter(); 47 | #endif 48 | } 49 | 50 | void zclFactoryResetter_ResetToFN(void) { 51 | HalLedSet(HAL_LED_1, HAL_LED_MODE_FLASH); 52 | LREP("bdbAttributes.bdbNodeIsOnANetwork=%d bdbAttributes.bdbCommissioningMode=0x%X\r\n", bdbAttributes.bdbNodeIsOnANetwork, bdbAttributes.bdbCommissioningMode); 53 | LREPMaster("zclFactoryResetter: Reset to FN\r\n"); 54 | bdb_resetLocalAction(); 55 | } 56 | 57 | void zclFactoryResetter_HandleKeys(uint8 portAndAction, uint8 keyCode) { 58 | #if FACTORY_RESET_BY_LONG_PRESS 59 | if (portAndAction & HAL_KEY_RELEASE) { 60 | LREPMaster("zclFactoryResetter: Key release\r\n"); 61 | osal_stop_timerEx(zclFactoryResetter_TaskID, FACTORY_RESET_EVT); 62 | } else { 63 | LREPMaster("zclFactoryResetter: Key press\r\n"); 64 | bool statTimer = true; 65 | #if FACTORY_RESET_BY_LONG_PRESS_PORT 66 | statTimer = FACTORY_RESET_BY_LONG_PRESS_PORT & portAndAction; 67 | #endif 68 | LREP("zclFactoryResetter statTimer hold timer %d\r\n", statTimer); 69 | if (statTimer) { 70 | uint32 timeout = bdbAttributes.bdbNodeIsOnANetwork ? FACTORY_RESET_HOLD_TIME_LONG : FACTORY_RESET_HOLD_TIME_FAST; 71 | osal_start_timerEx(zclFactoryResetter_TaskID, FACTORY_RESET_EVT, timeout); 72 | } 73 | } 74 | #endif 75 | } 76 | 77 | void zclFactoryResetter_ProcessBootCounter(void) { 78 | LREPMaster("zclFactoryResetter_ProcessBootCounter\r\n"); 79 | osal_start_timerEx(zclFactoryResetter_TaskID, FACTORY_BOOTCOUNTER_RESET_EVT, FACTORY_RESET_BOOTCOUNTER_RESET_TIME); 80 | 81 | uint16 bootCnt = 0; 82 | if (osal_nv_item_init(ZCD_NV_BOOTCOUNTER, sizeof(bootCnt), &bootCnt) == ZSUCCESS) { 83 | osal_nv_read(ZCD_NV_BOOTCOUNTER, 0, sizeof(bootCnt), &bootCnt); 84 | } 85 | LREP("bootCnt %d\r\n", bootCnt); 86 | bootCnt += 1; 87 | if (bootCnt >= FACTORY_RESET_BOOTCOUNTER_MAX_VALUE) { 88 | LREP("bootCnt =%d greater than, ressetting %d\r\n", bootCnt, FACTORY_RESET_BOOTCOUNTER_MAX_VALUE); 89 | bootCnt = 0; 90 | osal_stop_timerEx(zclFactoryResetter_TaskID, FACTORY_BOOTCOUNTER_RESET_EVT); 91 | osal_start_timerEx(zclFactoryResetter_TaskID, FACTORY_RESET_EVT, 5000); 92 | } 93 | osal_nv_write(ZCD_NV_BOOTCOUNTER, 0, sizeof(bootCnt), &bootCnt); 94 | } -------------------------------------------------------------------------------- /PROJECT SOURCE/EINK213TH_SIMPLE_TEST/zstack-lib/hal_i2c.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************************** 2 | Filename: hal_i2c.h 3 | Revised: $Date: 2009-01-20 06:41:30 -0800 (Tue, 20 Jan 2009) $ 4 | Revision: $Revision: 18809 $ 5 | 6 | Description: Interface for I2C driver. 7 | 8 | 9 | Copyright 2006 - 2008 Texas Instruments Incorporated. All rights reserved. 10 | 11 | IMPORTANT: Your use of this Software is limited to those specific rights 12 | granted under the terms of a software license agreement between the user 13 | who downloaded the software, his/her employer (which must be your employer) 14 | and Texas Instruments Incorporated (the "License"). You may not use this 15 | Software unless you agree to abide by the terms of the License. The License 16 | limits your use, and you acknowledge, that the Software may not be modified, 17 | copied or distributed unless embedded on a Texas Instruments microcontroller 18 | or used solely and exclusively in conjunction with a Texas Instruments radio 19 | frequency transceiver, which is integrated into your product. Other than for 20 | the foregoing purpose, you may not use, reproduce, copy, prepare derivative 21 | works of, modify, distribute, perform, display or sell this Software and/or 22 | its documentation for any purpose. 23 | 24 | YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE 25 | PROVIDED �AS IS� WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, 26 | INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE, 27 | NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL 28 | TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT, 29 | NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER 30 | LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES 31 | INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE 32 | OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT 33 | OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES 34 | (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS. 35 | 36 | Should you have any questions regarding your right to use this Software, 37 | contact Texas Instruments Incorporated at www.TI.com. 38 | **************************************************************************************************/ 39 | 40 | #ifndef HAL_I2C_H 41 | #define HAL_I2C_H 42 | 43 | 44 | #define I2C_ERROR 1 45 | #define I2C_SUCCESS 0 46 | 47 | #define OCM_READ (0x01) 48 | #define OCM_WRITE (0x00) 49 | 50 | /********************************************************************* 51 | * @fn HalI2CInit 52 | * @brief Initializes two-wire serial I/O bus 53 | * @param void 54 | * @return void 55 | */ 56 | void HalI2CInit( void ); 57 | 58 | /********************************************************************* 59 | * @fn HALI2CReceive 60 | * @brief Receives data into a buffer from an I2C slave device 61 | * @param address: address of the slave device 62 | * @param buf: target array for read characters 63 | * @param len: max number of characters to read 64 | * @return zero when successful. 65 | */ 66 | int8 HalI2CReceive(uint8 address, uint8 *buf, uint16 len); 67 | 68 | /********************************************************************* 69 | * @fn HALI2CSend 70 | * @brief Sends buffer contents to an I2C slave device 71 | * @param address: address of the slave device 72 | * @param buf - ptr to buffered data to send 73 | * @param len - number of bytes in buffer 74 | * @return zero when successful. 75 | */ 76 | int8 HalI2CSend(uint8 address, uint8 *buf, uint16 len); 77 | 78 | 79 | 80 | int8 I2C_ReadMultByte( uint8 address, uint8 reg, uint8 *buffer, uint16 len ); 81 | int8 I2C_WriteMultByte( uint8 address, uint8 reg, uint8 *buffer, uint16 len ); 82 | #endif 83 | -------------------------------------------------------------------------------- /PROJECT SOURCE/EINK213TH_SIMPLE_TEST/zstack-lib/bettery.c: -------------------------------------------------------------------------------- 1 | #include "Debug.h" 2 | #include "battery.h" 3 | #include "hal_adc.h" 4 | #include "utils.h" 5 | #include "OSAL.h" 6 | #include "zcl.h" 7 | #include "zcl_general.h" 8 | #include "bdb_interface.h" 9 | // (( 3 * 1.15 ) / (( 2^14 / 2 ) - 1 )) * 1000 (not correct) 10 | // #define MULTI (float) 0.4211939934 11 | // this coefficient calculated using 12 | // https://docs.google.com/spreadsheets/d/1qrFdMTo0ZrqtlGUoafeB3hplhU3GzDnVWuUK4M9OgNo/edit?usp=sharing 13 | #define MULTI (float)0.443 14 | 15 | #define VOLTAGE_MIN 2.0 16 | 17 | #define VOLTAGE_MAX 3.3 18 | 19 | #ifndef ZCL_BATTERY_REPORT_INTERVAL 20 | #define ZCL_BATTERY_REPORT_INTERVAL ((uint32) 1800000) //30 minutes 21 | #endif 22 | 23 | #ifndef ZCL_BATTERY_REPORT_DELAY 24 | #define ZCL_BATTERY_REPORT_DELAY 5 * 1000 25 | #endif 26 | 27 | #ifndef ZCL_BATTERY_REPORT_REPORT_CONVERTER 28 | #define ZCL_BATTERY_REPORT_REPORT_CONVERTER(millivolts) getBatteryRemainingPercentageZCLCR2032(millivolts) 29 | #endif 30 | 31 | #define POWER_CFG ZCL_CLUSTER_ID_GEN_POWER_CFG 32 | 33 | #define ZCL_BATTERY_REPORT_EVT 0x0001 34 | 35 | 36 | uint8 zclBattery_Voltage = 0xff; 37 | uint8 zclBattery_PercentageRemainig = 0xff; 38 | uint16 zclBattery_RawAdc = 0xff; 39 | 40 | uint8 getBatteryVoltageZCL(uint16 millivolts) { 41 | uint8 volt8 = (uint8)(millivolts / 100); 42 | if ((millivolts - (volt8 * 100)) > 50) { 43 | return volt8 + 1; 44 | } else { 45 | return volt8; 46 | } 47 | } 48 | // return millivolts 49 | uint16 getBatteryVoltage(void) { 50 | HalAdcSetReference(HAL_ADC_REF_125V); 51 | zclBattery_RawAdc = adcReadSampled(HAL_ADC_CHANNEL_VDD, HAL_ADC_RESOLUTION_14, HAL_ADC_REF_125V, 10); 52 | return (uint16)(zclBattery_RawAdc * MULTI); 53 | } 54 | 55 | uint8 getBatteryRemainingPercentageZCL(uint16 millivolts) { return (uint8)mapRange(VOLTAGE_MIN, VOLTAGE_MAX, 0.0, 200.0, millivolts); } 56 | 57 | uint8 getBatteryRemainingPercentageZCLCR2032(uint16 volt16) { 58 | float battery_level; 59 | if (volt16 >= 3000) { 60 | battery_level = 100; 61 | } else if (volt16 > 2900) { 62 | battery_level = 100 - ((3000 - volt16) * 58) / 100; 63 | } else if (volt16 > 2740) { 64 | battery_level = 42 - ((2900 - volt16) * 24) / 160; 65 | } else if (volt16 > 2440) { 66 | battery_level = 18 - ((2740 - volt16) * 12) / 300; 67 | } else if (volt16 > 2100) { 68 | battery_level = 6 - ((2440 - volt16) * 6) / 340; 69 | } else { 70 | battery_level = 0; 71 | } 72 | return (uint8)(battery_level * 2); 73 | } 74 | 75 | void zclBattery_Report(void) { 76 | uint16 millivolts = getBatteryVoltage(); 77 | zclBattery_Voltage = getBatteryVoltageZCL(millivolts); 78 | zclBattery_PercentageRemainig = ZCL_BATTERY_REPORT_REPORT_CONVERTER(millivolts); 79 | LREP("Battery voltageZCL=%d prc=%d voltage=%d\r\n", zclBattery_Voltage, zclBattery_PercentageRemainig, millivolts); 80 | #if BDB_REPORTING 81 | bdb_RepChangedAttrValue(1, POWER_CFG, ATTRID_POWER_CFG_BATTERY_PERCENTAGE_REMAINING); 82 | #else 83 | const uint8 NUM_ATTRIBUTES = 3; 84 | zclReportCmd_t *pReportCmd; 85 | pReportCmd = osal_mem_alloc(sizeof(zclReportCmd_t) + (NUM_ATTRIBUTES * sizeof(zclReport_t))); 86 | if (pReportCmd != NULL) { 87 | pReportCmd->numAttr = NUM_ATTRIBUTES; 88 | 89 | pReportCmd->attrList[0].attrID = ATTRID_POWER_CFG_BATTERY_VOLTAGE; 90 | pReportCmd->attrList[0].dataType = ZCL_DATATYPE_UINT8; 91 | pReportCmd->attrList[0].attrData = (void *)(&zclBattery_Voltage); 92 | 93 | pReportCmd->attrList[1].attrID = ATTRID_POWER_CFG_BATTERY_PERCENTAGE_REMAINING; 94 | pReportCmd->attrList[1].dataType = ZCL_DATATYPE_UINT8; 95 | pReportCmd->attrList[1].attrData = (void *)(&zclBattery_PercentageRemainig); 96 | 97 | pReportCmd->attrList[2].attrID = ATTRID_POWER_CFG_BATTERY_VOLTAGE_RAW_ADC; 98 | pReportCmd->attrList[2].dataType = ZCL_DATATYPE_UINT16; 99 | pReportCmd->attrList[2].attrData = (void *)(&zclBattery_RawAdc); 100 | 101 | afAddrType_t inderect_DstAddr = {.addrMode = (afAddrMode_t)AddrNotPresent, .endPoint = 0, .addr.shortAddr = 0}; 102 | zcl_SendReportCmd(1, &inderect_DstAddr, POWER_CFG, pReportCmd, ZCL_FRAME_CLIENT_SERVER_DIR, TRUE, bdb_getZCLFrameCounter()); 103 | } 104 | osal_mem_free(pReportCmd); 105 | #endif 106 | } 107 | 108 | uint8 zclBattery_TaskId = 0; 109 | 110 | void zclBattery_Init(uint8 task_id) { 111 | zclBattery_TaskId = task_id; 112 | #if BDB_REPORTING 113 | osal_start_reload_timer(zclBattery_TaskId, ZCL_BATTERY_REPORT_EVT, ZCL_BATTERY_REPORT_INTERVAL); 114 | #endif 115 | } 116 | 117 | uint16 zclBattery_event_loop(uint8 task_id, uint16 events) { 118 | LREP("zclBattery_event_loop 0x%X\r\n", events); 119 | if (events & ZCL_BATTERY_REPORT_EVT) { 120 | LREPMaster("ZCL_BATTERY_REPORT_EVT\r\n"); 121 | zclBattery_Report(); 122 | return (events ^ ZCL_BATTERY_REPORT_EVT); 123 | } 124 | return 0; 125 | } 126 | 127 | void zclBattery_HandleKeys(uint8 portAndAction, uint8 keyCode) { 128 | osal_start_timerEx(zclBattery_TaskId, ZCL_BATTERY_REPORT_EVT, ZCL_BATTERY_REPORT_DELAY); 129 | } -------------------------------------------------------------------------------- /PROJECT SOURCE/EINK213TH_SIMPLE_TEST/Source/zcl_app_data.c: -------------------------------------------------------------------------------- 1 | #include "AF.h" 2 | #include "OSAL.h" 3 | #include "ZComDef.h" 4 | #include "ZDConfig.h" 5 | 6 | #include "zcl.h" 7 | #include "zcl_general.h" 8 | #include "zcl_ms.h" 9 | #include "zcl_ha.h" 10 | 11 | #include "zcl_app.h" 12 | 13 | #include "battery.h" 14 | #include "version.h" 15 | /********************************************************************* 16 | * CONSTANTS 17 | */ 18 | 19 | #define APP_DEVICE_VERSION 2 20 | #define APP_FLAGS 0 21 | 22 | #define APP_HWVERSION 1 23 | #define APP_ZCLVERSION 1 24 | 25 | /********************************************************************* 26 | * TYPEDEFS 27 | */ 28 | 29 | /********************************************************************* 30 | * MACROS 31 | */ 32 | 33 | /********************************************************************* 34 | * GLOBAL VARIABLES 35 | */ 36 | 37 | // Global attributes 38 | const uint16 zclApp_clusterRevision_all = 0x0001; 39 | 40 | int16 zclApp_Temperature_Sensor_MeasuredValue = 0; 41 | uint16 zclApp_HumiditySensor_MeasuredValue = 0; 42 | 43 | // Basic Cluster 44 | const uint8 zclApp_HWRevision = APP_HWVERSION; 45 | const uint8 zclApp_ZCLVersion = APP_ZCLVERSION; 46 | const uint8 zclApp_ApplicationVersion = 3; 47 | const uint8 zclApp_StackVersion = 4; 48 | 49 | //{lenght, 'd', 'a', 't', 'a'} 50 | const uint8 zclApp_ManufacturerName[] = {13, 'e', 'f', 'e', 'k', 't', 'a', 'l', 'a', 'b', '.', 'c', 'o', 'm'}; 51 | const uint8 zclApp_ModelId[] = {14, 'E', 'F', 'E', 'K', 'T', 'A', '_', 'e', 'O', 'N', '2', '1', '3', 'z'}; 52 | const uint8 zclApp_PowerSource = POWER_SOURCE_BATTERY; 53 | 54 | /********************************************************************* 55 | * ATTRIBUTE DEFINITIONS - Uses REAL cluster IDs 56 | */ 57 | 58 | 59 | // msTemperatureMeasurement int16 60 | // msRelativeHumidity: uint16 61 | // msPressureMeasurement int16 62 | // msIlluminanceMeasurement uint16 63 | // #define ZCL_CLUSTER_ID_GEN_BASIC 0x0000 64 | // #define ZCL_CLUSTER_ID_GEN_POWER_CFG 0x0001 65 | // #define ZCL_CLUSTER_ID_MS_ILLUMINANCE_MEASUREMENT 0x0400 66 | // #define ZCL_CLUSTER_ID_MS_TEMPERATURE_MEASUREMENT 0x0402 67 | // #define ZCL_CLUSTER_ID_MS_PRESSURE_MEASUREMENT 0x0403 68 | 69 | CONST zclAttrRec_t zclApp_AttrsFirstEP[] = { 70 | {BASIC, {ATTRID_BASIC_ZCL_VERSION, ZCL_UINT8, R, (void *)&zclApp_ZCLVersion}}, 71 | {BASIC, {ATTRID_BASIC_APPL_VERSION, ZCL_UINT8, R, (void *)&zclApp_ApplicationVersion}}, 72 | {BASIC, {ATTRID_BASIC_STACK_VERSION, ZCL_UINT8, R, (void *)&zclApp_StackVersion}}, 73 | {BASIC, {ATTRID_BASIC_HW_VERSION, ZCL_UINT8, R, (void *)&zclApp_HWRevision}}, 74 | {BASIC, {ATTRID_BASIC_MANUFACTURER_NAME, ZCL_DATATYPE_CHAR_STR, R, (void *)zclApp_ManufacturerName}}, 75 | {BASIC, {ATTRID_BASIC_MODEL_ID, ZCL_DATATYPE_CHAR_STR, R, (void *)zclApp_ModelId}}, 76 | {BASIC, {ATTRID_BASIC_DATE_CODE, ZCL_DATATYPE_CHAR_STR, R, (void *)zclApp_DateCode}}, 77 | {BASIC, {ATTRID_BASIC_POWER_SOURCE, ZCL_DATATYPE_ENUM8, R, (void *)&zclApp_PowerSource}}, 78 | {BASIC, {ATTRID_BASIC_SW_BUILD_ID, ZCL_DATATYPE_CHAR_STR, R, (void *)zclApp_DateCode}}, 79 | {BASIC, {ATTRID_CLUSTER_REVISION, ZCL_DATATYPE_UINT16, R, (void *)&zclApp_clusterRevision_all}}, 80 | {POWER_CFG, {ATTRID_POWER_CFG_BATTERY_VOLTAGE, ZCL_UINT8, RR, (void *)&zclBattery_Voltage}}, 81 | /** 82 | * FYI: calculating battery percentage can be tricky, since this device can be powered from 2xAA or 1xCR2032 batteries 83 | * */ 84 | {POWER_CFG, {ATTRID_POWER_CFG_BATTERY_PERCENTAGE_REMAINING, ZCL_UINT8, RR, (void *)&zclBattery_PercentageRemainig}}, 85 | {POWER_CFG, {ATTRID_POWER_CFG_BATTERY_VOLTAGE_RAW_ADC, ZCL_UINT16, RR, (void *)&zclBattery_RawAdc}}, 86 | 87 | {TEMP, {ATTRID_MS_TEMPERATURE_MEASURED_VALUE, ZCL_INT16, RR, (void *)&zclApp_Temperature_Sensor_MeasuredValue}}, 88 | 89 | {HUMIDITY, {ATTRID_MS_RELATIVE_HUMIDITY_MEASURED_VALUE, ZCL_UINT16, RR, (void *)&zclApp_HumiditySensor_MeasuredValue}} 90 | }; 91 | 92 | //uint8 CONST zclApp_AttrsSecondEPCount = (sizeof(zclApp_AttrsSecondEP) / sizeof(zclApp_AttrsSecondEP[0])); 93 | uint8 CONST zclApp_AttrsFirstEPCount = (sizeof(zclApp_AttrsFirstEP) / sizeof(zclApp_AttrsFirstEP[0])); 94 | 95 | const cId_t zclApp_InClusterList[] = {ZCL_CLUSTER_ID_GEN_BASIC}; 96 | 97 | #define APP_MAX_INCLUSTERS (sizeof(zclApp_InClusterList) / sizeof(zclApp_InClusterList[0])) 98 | 99 | const cId_t zclApp_OutClusterListFirstEP[] = {POWER_CFG, TEMP, HUMIDITY}; 100 | 101 | #define APP_MAX_OUTCLUSTERS_FIRST_EP (sizeof(zclApp_OutClusterListFirstEP) / sizeof(zclApp_OutClusterListFirstEP[0])) 102 | 103 | 104 | 105 | SimpleDescriptionFormat_t zclApp_FirstEP = { 106 | 1, // int Endpoint; 107 | ZCL_HA_PROFILE_ID, // uint16 AppProfId[2]; 108 | ZCL_HA_DEVICEID_SIMPLE_SENSOR, // uint16 AppDeviceId[2]; 109 | APP_DEVICE_VERSION, // int AppDevVer:4; 110 | APP_FLAGS, // int AppFlags:4; 111 | APP_MAX_INCLUSTERS, // byte AppNumInClusters; 112 | (cId_t *)zclApp_InClusterList, // byte *pAppInClusterList; 113 | APP_MAX_OUTCLUSTERS_FIRST_EP, // byte AppNumInClusters; 114 | (cId_t *)zclApp_OutClusterListFirstEP // byte *pAppInClusterList; 115 | }; 116 | -------------------------------------------------------------------------------- /PROJECT SOURCE/EINK213TH_SIMPLE_TEST/zstack-lib/utils.h: -------------------------------------------------------------------------------- 1 | #ifndef UTILS_H 2 | #define UTILS_H 3 | extern double mapRange(double a1, double a2, double b1, double b2, double s); 4 | 5 | extern uint16 adcReadSampled(uint8 channel, uint8 resolution, uint8 reference, uint8 samplesCount); 6 | 7 | 8 | #undef P 9 | #undef INP 10 | #define INP INP 11 | #undef DIR 12 | #define DIR DIR 13 | #undef SEL 14 | #define SEL SEL 15 | 16 | // General I/O definitions 17 | #define IO_GIO 0 // General purpose I/O 18 | #define IO_PER 1 // Peripheral function 19 | #define IO_IN 0 // Input pin 20 | #define IO_OUT 1 // Output pin 21 | #define IO_PUD 0 // Pullup/pulldn input 22 | #define IO_TRI 1 // Tri-state input 23 | #define IO_PUP 0 // Pull-up input pin 24 | #define IO_PDN 1 // Pull-down input pin 25 | 26 | /* I/O PORT CONFIGURATION */ 27 | #define CAT1(x, y) x##y // Concatenates 2 strings 28 | #define CAT2(x, y) CAT1(x, y) // Forces evaluation of CAT1 29 | 30 | // OCM port I/O defintions 31 | // Builds I/O port name: PNAME(1,INP) ==> P1INP 32 | #define PNAME(y, z) CAT2(P, CAT2(y, z)) 33 | // Builds I/O bit name: BNAME(1,2) ==> P1_2 34 | #define BNAME(port, pin) CAT2(CAT2(P, port), CAT2(_, pin)) 35 | 36 | 37 | #define IO_DIR_PORT_PIN(port, pin, dir) \ 38 | { \ 39 | if (dir == IO_OUT) \ 40 | PNAME(port, DIR) |= (1 << (pin)); \ 41 | else \ 42 | PNAME(port, DIR) &= ~(1 << (pin)); \ 43 | } 44 | 45 | 46 | 47 | #define IO_FUNC_PORT_PIN(port, pin, func) \ 48 | { \ 49 | if (port < 2) { \ 50 | if (func == IO_PER) \ 51 | PNAME(port, SEL) |= (1 << (pin)); \ 52 | else \ 53 | PNAME(port, SEL) &= ~(1 << (pin)); \ 54 | } else { \ 55 | if (func == IO_PER) \ 56 | P2SEL |= (1 << (pin >> 1)); \ 57 | else \ 58 | P2SEL &= ~(1 << (pin >> 1)); \ 59 | } \ 60 | } 61 | 62 | #define IO_IMODE_PORT_PIN(port, pin, mode) \ 63 | { \ 64 | if (mode == IO_TRI) \ 65 | PNAME(port, INP) |= (1 << (pin)); \ 66 | else \ 67 | PNAME(port, INP) &= ~(1 << (pin)); \ 68 | } 69 | 70 | #define IO_PUD_PORT(port, dir) \ 71 | { \ 72 | if (dir == IO_PDN) \ 73 | P2INP |= (1 << (port + 5)); \ 74 | else \ 75 | P2INP &= ~(1 << (port + 5)); \ 76 | } 77 | #endif -------------------------------------------------------------------------------- /PROJECT SOURCE/EINK213TH_SIMPLE_TEST/zstack-lib/hal_key.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************************** 2 | Filename: hal_key.h 3 | Revised: $Date: 2007-07-06 10:42:24 -0700 (Fri, 06 Jul 2007) $ 4 | Revision: $Revision: 13579 $ 5 | 6 | Description: This file contains the interface to the KEY Service. 7 | 8 | 9 | Copyright 2005-2012 Texas Instruments Incorporated. All rights reserved. 10 | 11 | IMPORTANT: Your use of this Software is limited to those specific rights 12 | granted under the terms of a software license agreement between the user 13 | who downloaded the software, his/her employer (which must be your employer) 14 | and Texas Instruments Incorporated (the "License"). You may not use this 15 | Software unless you agree to abide by the terms of the License. The License 16 | limits your use, and you acknowledge, that the Software may not be modified, 17 | copied or distributed unless embedded on a Texas Instruments microcontroller 18 | or used solely and exclusively in conjunction with a Texas Instruments radio 19 | frequency transceiver, which is integrated into your product. Other than for 20 | the foregoing purpose, you may not use, reproduce, copy, prepare derivative 21 | works of, modify, distribute, perform, display or sell this Software and/or 22 | its documentation for any purpose. 23 | 24 | YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE 25 | PROVIDED �AS IS� WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, 26 | INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE, 27 | NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL 28 | TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT, 29 | NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER 30 | LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES 31 | INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE 32 | OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT 33 | OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES 34 | (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS. 35 | 36 | Should you have any questions regarding your right to use this Software, 37 | contact Texas Instruments Incorporated at www.TI.com. 38 | **************************************************************************************************/ 39 | 40 | #ifndef HAL_KEY_H 41 | #define HAL_KEY_H 42 | 43 | #ifdef __cplusplus 44 | extern "C" 45 | { 46 | #endif 47 | 48 | /************************************************************************************************** 49 | * INCLUDES 50 | **************************************************************************************************/ 51 | #include "hal_board.h" 52 | 53 | /************************************************************************************************** 54 | * MACROS 55 | **************************************************************************************************/ 56 | 57 | /************************************************************************************************** 58 | * CONSTANTS 59 | **************************************************************************************************/ 60 | #define HAL_KEY_BIT0 0x01 61 | #define HAL_KEY_BIT1 0x02 62 | #define HAL_KEY_BIT2 0x04 63 | #define HAL_KEY_BIT3 0x08 64 | #define HAL_KEY_BIT4 0x10 65 | #define HAL_KEY_BIT5 0x20 66 | #define HAL_KEY_BIT6 0x40 67 | #define HAL_KEY_BIT7 0x80 68 | 69 | 70 | 71 | 72 | 73 | /* Interrupt option - Enable or disable */ 74 | #define HAL_KEY_INTERRUPT_DISABLE 0x00 75 | #define HAL_KEY_INTERRUPT_ENABLE 0x01 76 | 77 | /* Key state - shift or nornal */ 78 | #define HAL_KEY_STATE_NORMAL 0x00 79 | #define HAL_KEY_STATE_SHIFT 0x01 80 | 81 | #define HAL_KEY_RISING_EDGE 0 82 | #define HAL_KEY_FALLING_EDGE 1 83 | 84 | 85 | 86 | #define HAL_KEY_PORT0 0x01 87 | #define HAL_KEY_PORT1 0x02 88 | #define HAL_KEY_PORT2 0x04 89 | 90 | #define HAL_KEY_PRESS 0x20 91 | #define HAL_KEY_RELEASE 0x40 92 | 93 | 94 | 95 | #define HAL_KEY_SW_1 0x01 // Joystick up 96 | #define HAL_KEY_SW_2 0x02 // Joystick right 97 | #define HAL_KEY_SW_5 0x04 // Joystick center 98 | #define HAL_KEY_SW_4 0x08 // Joystick left 99 | #define HAL_KEY_SW_3 0x10 // Joystick down 100 | 101 | #define HAL_KEY_SW_6 0x20 // Button S1 if available 102 | #define HAL_KEY_SW_7 0x40 // Button S2 if available 103 | 104 | /************************************************************************************************** 105 | * TYPEDEFS 106 | **************************************************************************************************/ 107 | typedef void (*halKeyCBack_t) (uint8 keys, uint8 state); 108 | 109 | /************************************************************************************************** 110 | * GLOBAL VARIABLES 111 | **************************************************************************************************/ 112 | extern bool Hal_KeyIntEnable; 113 | 114 | /************************************************************************************************** 115 | * FUNCTIONS - API 116 | **************************************************************************************************/ 117 | 118 | /* 119 | * Initialize the Key Service 120 | */ 121 | extern void HalKeyInit( void ); 122 | 123 | /* 124 | * Configure the Key Service 125 | */ 126 | extern void HalKeyConfig( bool interruptEnable, const halKeyCBack_t cback); 127 | 128 | /* 129 | * Read the Key status 130 | */ 131 | extern uint8 HalKeyRead( void); 132 | 133 | /* 134 | * Enter sleep mode, store important values 135 | */ 136 | extern void HalKeyEnterSleep ( void ); 137 | 138 | /* 139 | * Exit sleep mode, retore values 140 | */ 141 | extern uint8 HalKeyExitSleep ( void ); 142 | 143 | /* 144 | * This is for internal used by hal_driver 145 | */ 146 | extern void HalKeyPoll ( void ); 147 | 148 | /* 149 | * This is for internal used by hal_sleep 150 | */ 151 | extern bool HalKeyPressed( void ); 152 | 153 | extern uint8 hal_key_keys(void); 154 | 155 | extern uint8 hal_key_int_keys(void); 156 | 157 | /************************************************************************************************** 158 | **************************************************************************************************/ 159 | 160 | #ifdef __cplusplus 161 | } 162 | #endif 163 | 164 | #endif 165 | -------------------------------------------------------------------------------- /PROJECT SOURCE/EINK213TH_SIMPLE_TEST/zstack-lib/f8wConfig.cfg: -------------------------------------------------------------------------------- 1 | /* 2 | * f8wConfig.cfg 3 | * 4 | * Compiler command-line options used to define a TI Z-Stack ZigBee device. 5 | * To move an option from here to the project file, comment out or delete the 6 | * option from this file and enter it into the "Define Symbols" box under the 7 | * Preprocessor tab of the C/C++ Compiler Project Options. New user defined 8 | * options may be added to this file, as necessary. 9 | * 10 | * Each macro is prefixed with '-D'. The entries are to be constructed as if 11 | * they are to be on the compiler command line invocation (which they are). 12 | * 13 | * NOTE: The RHS (Right-Hand-Side) must be quoted if there are embedded blanks. 14 | * See the DEFAULT_KEY definition for an example. 15 | */ 16 | 17 | /* Enable ZigBee-Pro */ 18 | -DZIGBEEPRO 19 | 20 | /* Set to 1 to enable security. To disable set to 0 */ 21 | -DSECURE=1 22 | -DZG_SECURE_DYNAMIC=0 23 | 24 | /* Enable the Reflector */ 25 | -DREFLECTOR 26 | 27 | /* Default channel is Channel 11 - 0x0B */ 28 | // Channels are defined in the following: 29 | // 0 : 868 MHz 0x00000001 30 | // 1 - 10 : 915 MHz 0x000007FE 31 | // 11 - 26 : 2.4 GHz 0x07FFF800 32 | // 33 | //-DMAX_CHANNELS_868MHZ 0x00000001 34 | //-DMAX_CHANNELS_915MHZ 0x000007FE 35 | //-DMAX_CHANNELS_24GHZ 0x07FFF800 36 | //-DDEFAULT_CHANLIST=0x04000000 // 26 - 0x1A 37 | //-DDEFAULT_CHANLIST=0x02000000 // 25 - 0x19 38 | //-DDEFAULT_CHANLIST=0x01000000 // 24 - 0x18 39 | //-DDEFAULT_CHANLIST=0x00800000 // 23 - 0x17 40 | //-DDEFAULT_CHANLIST=0x00400000 // 22 - 0x16 41 | //-DDEFAULT_CHANLIST=0x00200000 // 21 - 0x15 42 | //-DDEFAULT_CHANLIST=0x00100000 // 20 - 0x14 43 | //-DDEFAULT_CHANLIST=0x00080000 // 19 - 0x13 44 | //-DDEFAULT_CHANLIST=0x00040000 // 18 - 0x12 45 | //-DDEFAULT_CHANLIST=0x00020000 // 17 - 0x11 46 | //-DDEFAULT_CHANLIST=0x00010000 // 16 - 0x10 47 | //-DDEFAULT_CHANLIST=0x00008000 // 15 - 0x0F 48 | //-DDEFAULT_CHANLIST=0x00004000 // 14 - 0x0E 49 | //-DDEFAULT_CHANLIST=0x00002000 // 13 - 0x0D 50 | //-DDEFAULT_CHANLIST=0x00001000 // 12 - 0x0C 51 | -DDEFAULT_CHANLIST=0x07FFF800 // ALL channels 52 | 53 | /* Define the default PAN ID. 54 | * 55 | * Setting this to a value other than 0xFFFF causes 56 | * ZDO_COORD to use this value as its PAN ID and 57 | * Routers and end devices to join PAN with this ID 58 | */ 59 | -DZDAPP_CONFIG_PAN_ID=0xFFFF 60 | 61 | /* Minimum number of milliseconds to hold off the start of the device 62 | * in the network and the minimum delay between joining cycles. 63 | */ 64 | -DNWK_START_DELAY=100 65 | 66 | /* Mask for the random joining delay. This value is masked with 67 | * the return from osal_rand() to get a random delay time for 68 | * each joining cycle. This random value is added to NWK_START_DELAY. 69 | * For example, a value of 0x007F will be a joining delay of 0 to 127 70 | * milliseconds. 71 | */ 72 | -DEXTENDED_JOINING_RANDOM_MASK=0x007F 73 | 74 | /* Minimum number of milliseconds to delay between each beacon request 75 | * in a joining cycle. 76 | */ 77 | -DBEACON_REQUEST_DELAY=100 78 | 79 | /* Mask for the random beacon request delay. This value is masked with the 80 | * return from osal_rand() to get a random delay time for each joining cycle. 81 | * This random value is added to DBEACON_REQUEST_DELAY. For example, a value 82 | * of 0x00FF will be a beacon request delay of 0 to 255 milliseconds. 83 | */ 84 | -DBEACON_REQ_DELAY_MASK=0x00FF 85 | 86 | /* Jitter mask for the link status report timer. This value is masked with the 87 | * return from osal_rand() to add a random delay to _NIB.nwkLinkStatusPeriod. 88 | * For example, a value of 0x007F allows a jitter between 0-127 milliseconds. 89 | */ 90 | -DLINK_STATUS_JITTER_MASK=0x007F 91 | 92 | /* in seconds; set to 0 to turn off route expiry */ 93 | -DROUTE_EXPIRY_TIME=30 94 | 95 | /* This number is used by polled devices, since the spec'd formula 96 | * doesn't work for sleeping end devices. For non-polled devices, 97 | * a formula is used. Value is in 2 milliseconds periods 98 | */ 99 | -DAPSC_ACK_WAIT_DURATION_POLLED=3000 100 | 101 | /* Default indirect message holding timeout value: 102 | * 1-65535 (0 -> 65536) X CNT_RTG_TIMER X RTG_TIMER_INTERVAL 103 | */ 104 | -DNWK_INDIRECT_MSG_TIMEOUT=7 105 | 106 | /* The number of simultaneous route discoveries in network */ 107 | -DMAX_RREQ_ENTRIES=8 108 | 109 | /* The maximum number of retries allowed after a transmission failure */ 110 | -DAPSC_MAX_FRAME_RETRIES=3 111 | 112 | /* Max number of times retry looking for the next hop address of a message */ 113 | -DNWK_MAX_DATA_RETRIES=2 114 | 115 | /* Number of times retry to poll parent before indicating loss of synchronization 116 | * with parent. Note that larger value will cause longer delay for the child to 117 | * rejoin the network. 118 | */ 119 | -DMAX_POLL_FAILURE_RETRIES=2 120 | 121 | /* The number of items in the broadcast table */ 122 | -DMAX_BCAST=9 123 | 124 | /* The maximum number of groups in the groups table */ 125 | -DAPS_MAX_GROUPS=16 126 | 127 | /* Number of entries in the regular routing table plus additional 128 | * entries for route repair 129 | */ 130 | -DMAX_RTG_ENTRIES=40 131 | 132 | /* Maximum number of entries in the Binding table. */ 133 | -DNWK_MAX_BINDING_ENTRIES=5 134 | 135 | /* Maximum number of cluster IDs for each binding table entry. 136 | * Note that any value other than the default value may cause a 137 | * compilation warning but Device Binding will function correctly. 138 | */ 139 | -DMAX_BINDING_CLUSTER_IDS=15 140 | 141 | /* Default security key. */ 142 | -DDEFAULT_KEY="{0}" 143 | 144 | /* Reset when ASSERT occurs, otherwise flash LEDs */ 145 | //-DASSERT_RESET 146 | 147 | /* Set the MAC MAX Frame Size (802.15.4 default is 102) */ 148 | -DMAC_MAX_FRAME_SIZE=116 149 | 150 | /* Minimum transmissions attempted for Channel Interference detection, 151 | * Frequency Agility can be disabled by setting this parameter to zero. 152 | */ 153 | -DZDNWKMGR_MIN_TRANSMISSIONS=20 154 | 155 | /* Compiler keywords */ 156 | -DCONST="const __code" 157 | -DGENERIC=__generic 158 | 159 | /**************************************** 160 | * The following are for End Devices only 161 | ***************************************/ 162 | 163 | -DRFD_RCVC_ALWAYS_ON=FALSE 164 | 165 | /* The number of milliseconds to wait between data request polls to the coordinator. */ 166 | -DPOLL_RATE=1000 167 | 168 | /* This is used after receiving a data indication to poll immediately 169 | * for queued messages...in milliseconds. 170 | */ 171 | -DQUEUED_POLL_RATE=300 172 | 173 | /* This is used after receiving a data confirmation to poll immediately 174 | * for response messages...in milliseconds 175 | */ 176 | -DRESPONSE_POLL_RATE=300 177 | 178 | /* This is used as an alternate response poll rate only for rejoin request. 179 | * This rate is determined by the response time of the parent that the device 180 | * is trying to join. 181 | */ 182 | -DREJOIN_POLL_RATE=700 183 | 184 | /* Rejoin retry backoff silent period timer duration in milliseconds - default 15 minutes according to HA test spec */ 185 | -DREJOIN_BACKOFF=900000 186 | 187 | /* Rejoin retry backoff scan timer duration in milliseconds - default 15 minutes according to HA test spec */ 188 | -DREJOIN_SCAN=900000 189 | 190 | /* Latest sample apps use LED4 and do not use S1 switch */ 191 | -DENABLE_LED4_DISABLE_S1 -------------------------------------------------------------------------------- /PROJECT SOURCE/EINK213TH_SIMPLE_TEST/Source/epdpaint.c: -------------------------------------------------------------------------------- 1 | 2 | //#include 3 | #include "epdpaint.h" 4 | 5 | unsigned char* pimage; 6 | int pwidth ; 7 | int pheight ; 8 | int protate ; 9 | 10 | void PaintPaint(unsigned char* image, int width, int height) { 11 | protate = ROTATE_0; 12 | pimage = image; 13 | /* 1 byte = 8 pixels, so the width should be the multiple of 8 */ 14 | pwidth = width % 8 ? width + 8 - (width % 8) : width; 15 | pheight = height; 16 | } 17 | 18 | void PaintSetWidth(int width) { 19 | pwidth = width % 8 ? width + 8 - (width % 8) : width; 20 | } 21 | 22 | void PaintSetHeight(int height) { 23 | pheight = height; 24 | } 25 | 26 | void PaintSetRotate(int rotate){ 27 | protate = rotate; 28 | } 29 | 30 | void PaintClear(int colored) { 31 | for (int x = 0; x < pwidth; x++) { 32 | for (int y = 0; y < pheight; y++) { 33 | PaintDrawAbsolutePixel(x, y, colored); 34 | } 35 | } 36 | } 37 | 38 | 39 | void PaintDrawPixel(int x, int y, int colored) { 40 | int point_temp; 41 | if (protate == ROTATE_0) { 42 | if(x < 0 || x >= pwidth || y < 0 || y >= pheight) { 43 | return; 44 | } 45 | PaintDrawAbsolutePixel(x, y, colored); 46 | } else if (protate == ROTATE_90) { 47 | if(x < 0 || x >= pheight || y < 0 || y >= pwidth) { 48 | return; 49 | } 50 | point_temp = x; 51 | x = pwidth - y; 52 | y = point_temp; 53 | PaintDrawAbsolutePixel(x, y, colored); 54 | } else if (protate == ROTATE_180) { 55 | if(x < 0 || x >= pwidth || y < 0 || y >= pheight) { 56 | return; 57 | } 58 | x = pwidth - x; 59 | y = pheight - y; 60 | PaintDrawAbsolutePixel(x, y, colored); 61 | } else if (protate == ROTATE_270) { 62 | if(x < 0 || x >= pheight || y < 0 || y >= pwidth) { 63 | return; 64 | } 65 | point_temp = x; 66 | x = y; 67 | y = pheight - point_temp; 68 | PaintDrawAbsolutePixel(x, y, colored); 69 | } 70 | } 71 | 72 | void PaintDrawAbsolutePixel(int x, int y, int colored) { 73 | if (x < 0 || x >= pwidth || y < 0 || y >= pheight) { 74 | return; 75 | } 76 | if (IF_INVERT_COLOR) { 77 | if (colored) { 78 | pimage[(x + y * pwidth) / 8] |= 0x80 >> (x % 8); 79 | } else { 80 | pimage[(x + y * pwidth) / 8] &= ~(0x80 >> (x % 8)); 81 | } 82 | } else { 83 | if (colored) { 84 | pimage[(x + y * pwidth) / 8] &= ~(0x80 >> (x % 8)); 85 | } else { 86 | pimage[(x + y * pwidth) / 8] |= 0x80 >> (x % 8); 87 | } 88 | } 89 | } 90 | 91 | unsigned char* PaintGetImage(void) { 92 | return pimage; 93 | } 94 | 95 | int PaintGetWidth(void) { 96 | return pwidth; 97 | } 98 | 99 | int PaintGetHeight(void) { 100 | return pheight; 101 | } 102 | 103 | void PaintDrawRectangle(int x0, int y0, int x1, int y1, int colored) { 104 | int min_x, min_y, max_x, max_y; 105 | min_x = x1 > x0 ? x0 : x1; 106 | max_x = x1 > x0 ? x1 : x0; 107 | min_y = y1 > y0 ? y0 : y1; 108 | max_y = y1 > y0 ? y1 : y0; 109 | 110 | if (protate == ROTATE_0) { 111 | PaintDrawHorizontalLine(min_x, min_y, max_x - min_x + 1, colored); 112 | } else if (protate == ROTATE_90) { 113 | PaintDrawHorizontalLine(min_x, min_y+1, max_x - min_x + 1, colored); 114 | } 115 | PaintDrawHorizontalLine(min_x, max_y, max_x - min_x + 1, colored); 116 | PaintDrawVerticalLine(min_x, min_y, max_y - min_y + 1, colored); 117 | PaintDrawVerticalLine(max_x, min_y, max_y - min_y + 1, colored); 118 | } 119 | 120 | void PaintDrawHorizontalLine(int x, int y, int line_width, int colored) { 121 | int i; 122 | for (i = x; i < x + line_width; i++) { 123 | PaintDrawPixel(i, y, colored); 124 | } 125 | } 126 | 127 | void PaintDrawVerticalLine(int x, int y, int line_height, int colored) { 128 | int i; 129 | for (i = y; i < y + line_height; i++) { 130 | PaintDrawPixel(x, i, colored); 131 | } 132 | } 133 | 134 | void PaintDrawLine(int x0, int y0, int x1, int y1, int colored) { 135 | /* Bresenham algorithm */ 136 | int dx = x1 - x0 >= 0 ? x1 - x0 : x0 - x1; 137 | int sx = x0 < x1 ? 1 : -1; 138 | int dy = y1 - y0 <= 0 ? y1 - y0 : y0 - y1; 139 | int sy = y0 < y1 ? 1 : -1; 140 | int err = dx + dy; 141 | 142 | while((x0 != x1) && (y0 != y1)) { 143 | PaintDrawPixel(x0, y0 , colored); 144 | if (2 * err >= dy) { 145 | err += dy; 146 | x0 += sx; 147 | } 148 | if (2 * err <= dx) { 149 | err += dx; 150 | y0 += sy; 151 | } 152 | } 153 | } 154 | 155 | void PaintDrawFilledRectangle(int x0, int y0, int x1, int y1, int colored) { 156 | int min_x, min_y, max_x, max_y; 157 | int i; 158 | min_x = x1 > x0 ? x0 : x1; 159 | max_x = x1 > x0 ? x1 : x0; 160 | min_y = y1 > y0 ? y0 : y1; 161 | max_y = y1 > y0 ? y1 : y0; 162 | 163 | for (i = min_x; i <= max_x; i++) { 164 | PaintDrawVerticalLine(i, min_y, max_y - min_y + 1, colored); 165 | } 166 | } 167 | 168 | void PaintDrawCircle(int x, int y, int radius, int colored) { 169 | /* Bresenham algorithm */ 170 | int x_pos = -radius; 171 | int y_pos = 0; 172 | int err = 2 - 2 * radius; 173 | int e2; 174 | 175 | do { 176 | PaintDrawPixel(x - x_pos, y + y_pos, colored); 177 | PaintDrawPixel(x + x_pos, y + y_pos, colored); 178 | PaintDrawPixel(x + x_pos, y - y_pos, colored); 179 | PaintDrawPixel(x - x_pos, y - y_pos, colored); 180 | e2 = err; 181 | if (e2 <= y_pos) { 182 | err += ++y_pos * 2 + 1; 183 | if(-x_pos == y_pos && e2 <= x_pos) { 184 | e2 = 0; 185 | } 186 | } 187 | if (e2 > x_pos) { 188 | err += ++x_pos * 2 + 1; 189 | } 190 | } while (x_pos <= 0); 191 | } 192 | 193 | void PaintDrawFilledCircle(int x, int y, int radius, int colored) { 194 | /* Bresenham algorithm */ 195 | int x_pos = -radius; 196 | int y_pos = 0; 197 | int err = 2 - 2 * radius; 198 | int e2; 199 | 200 | do { 201 | PaintDrawPixel(x - x_pos, y + y_pos, colored); 202 | PaintDrawPixel(x + x_pos, y + y_pos, colored); 203 | PaintDrawPixel(x + x_pos, y - y_pos, colored); 204 | PaintDrawPixel(x - x_pos, y - y_pos, colored); 205 | PaintDrawHorizontalLine(x + x_pos, y + y_pos, 2 * (-x_pos) + 1, colored); 206 | PaintDrawHorizontalLine(x + x_pos, y - y_pos, 2 * (-x_pos) + 1, colored); 207 | e2 = err; 208 | if (e2 <= y_pos) { 209 | err += ++y_pos * 2 + 1; 210 | if(-x_pos == y_pos && e2 <= x_pos) { 211 | e2 = 0; 212 | } 213 | } 214 | if(e2 > x_pos) { 215 | err += ++x_pos * 2 + 1; 216 | } 217 | } while(x_pos <= 0); 218 | } 219 | 220 | 221 | void PaintDrawImage(const unsigned char* imgData,int x, int y, int Width, int Height, int colored) 222 | { 223 | 224 | int i, j; 225 | const unsigned char* prt = imgData; 226 | for (j = 0; j < Height; j++) { 227 | for (i = 0; i < Width; i++) { 228 | if (* prt & (0x80 >> (i % 8))) { 229 | PaintDrawPixel(x + i, y + j, colored); 230 | } 231 | if (i % 8 == 7) { 232 | prt++; 233 | } 234 | } 235 | if (Width % 8 != 0) { 236 | prt++; 237 | } 238 | } 239 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # EFEKTA_eON213z 2 | 3 | Телеграм чат DIY Devices - https://t.me/diy_devices 4 | 5 | Продажа DIY Устройств - https://t.me/diydevmart 6 | 7 | My new zigbee project. Wireless temperature and humidity mini sensor with electronic ink display 2.13 inches, low power consumption, compact size, enclosure with magnets. The device use SHTC3 sensors, chip CC2530, battery CR2477. Support in zigbee2mqtt, zha. 8 | 9 | ### It is forbidden to manufacture devices for commercial sale, only for personal use. 10 | 11 | ### You can buy a ready-made device by writing to the mail hello@efektalab.com 12 | 13 | ### Delivery is carried out worldwide. 14 | 15 | ## You can make your own pcb here - https://www.pcbway.com/setinvite.aspx?inviteid=550959 16 | 17 | 18 | #### Sale: 19 | 20 | #### Video: https://youtu.be/5huTVfVpZgs 21 | 22 | #### zigbee2mqtt.io/supported-devices: https://www.zigbee2mqtt.io/supported-devices/#s=efekta 23 | 24 | #### Telegram DiyDev - https://t.me/diy_devices 25 | 26 | More info at http://efektalab.com/eON213z 27 | 28 | Chinese version - https://github.com/wzqvip/EFEKTA_eON213z/tree/Chinese_version 29 | 30 | 31 | 32 | ## V1_R1 33 | 34 | ![Indoor climate sensor EFEKTA eON213 Zigbee](https://github.com/smartboxchannel/EFEKTA_eON213z/blob/main/IMAGES/002.jpg) 35 | 36 | 37 | ![Indoor climate sensor EFEKTA eON213 Zigbee](https://github.com/smartboxchannel/EFEKTA_eON213z/blob/main/IMAGES/EFEKTA_eON213z.jpg) 38 | 39 | 40 | ### How to flash the device 41 | 42 | 1. Download the Smart RF Flash Programmer V1 https://www.ti.com/tool/FLASH-PROGRAMMER 43 | 44 | 2. Open the application select the HEX firmware file 45 | 46 | 3. Connect the device with wires to CCDebugger, first erase the chip, then flash it. 47 | 48 | --- 49 | 50 | ### How to install IAR 51 | 52 | https://github.com/ZigDevWiki/zigdevwiki.github.io/blob/main/docs/Begin/IAR_install.md 53 | 54 | https://github.com/sigma7i/zigbee-wiki/wiki/zigbee-firmware-install (RU) 55 | 56 | --- 57 | 58 | ### How to join: 59 | #### If device in FN(factory new) state: 60 | ##### one way 61 | 1. Open z2m, make sure that joining is prohibited 62 | 2. Insert the battery into the device 63 | 3. Click on the icon in z2m - allow joining (you have 180 seconds to add the device) 64 | 4. Go to the LOGS tab 65 | 5. Press the reset button on the device (the join procedure will begin, еhe device starts flashing the LED repeatedly) 66 | 6. Wait, in case of successfull join, device will flash led 5 times, if join failed, device will flash led 2 times 67 | 68 | ##### another way 69 | 1. Open z2m, make sure that joining is prohibited 70 | 2. Insert the battery into the device 71 | 3. Click on the icon in z2m - allow joining (you have 180 seconds to add the device) 72 | 4. Go to the LOGS tab 73 | 5. Press and hold button (1) for 2-3 seconds, until device start flashing the LED repeatedly 74 | 6. Wait, in case of successfull join, device will flash led 5 times, if join failed, device will flash led 2 times 75 | 76 | 77 | #### If device in a network: 78 | ##### one way 79 | 1. Hold button (1) for 10 seconds, this will reset device to FN(factory new) status 80 | 2. Click on the icon in z2m - allow joining (you have 180 seconds to add the device) 81 | 3. Go to the LOGS tab 82 | 5. Press and hold button (1) for 2-3 seconds, until device start flashing the LED repeatedly 83 | 6. Wait, in case of successfull join, device will flash led 5 times, if join failed, device will flash led 2 times 84 | 85 | ##### another way 86 | 1.Find the device in the list of z2m devices and delete it by applying force remove 87 | 2. Click on the icon in z2m - allow joining (you have 180 seconds to add the device) 88 | 3. Go to the LOGS tab 89 | 4. Press the reset button on the device (the join procedure will begin, еhe device starts flashing the LED repeatedly) 90 | 5. Wait, in case of successfull join, device will flash led 5 times, if join failed, device will flash led 2 times 91 | 92 | ![Efekta THP_LR \ THP](https://github.com/smartboxchannel/EFEKTA_eON213z/blob/main/IMAGES/003.jpg) 93 | 94 | ### Troubleshooting 95 | 96 | If a device does not connect to your coordinator, please try the following: 97 | 98 | 1. Power off all routers in your network. 99 | 2. Move the device near to your coordinator (about 1 meter). 100 | or if you cannot disable routers (for example, internal switches), you may try the following: 101 | 2.1. Disconnect an external antenna from your coordinator. 102 | 2.2. Move a device to your coordinator closely (1-3 centimeters). 103 | 3. Power on, power on the device. 104 | 4. Restart your coordinator (for example, restart Zigbee2MQTT if you use it). 105 | 106 | If the device has not fully passed the join 107 | 108 | 1. If the device is visible in the list of z2m devices, remove it by applying force remove 109 | 2. Restart your coordinator (for example, restart Zigbee2MQTT if you use it). 110 | 3. Click on the icon in z2m - allow joining (you have 180 seconds to add the device) 111 | 4. Go to the LOGS tab 112 | 5. Press and hold button (1) for 2-3 seconds, until device start flashing the LED repeatedly 113 | 6. Wait, in case of successfull join, device will flash led 5 times, if join failed, device will flash led 2 times 114 | 115 | 116 | 117 | ### Other checks 118 | 119 | Please, ensure the following: 120 | 121 | 1. Your power source is OK (a battery has more than 3V). You can temporarily use an external power source for testings (for example, from a debugger). 122 | 2. The RF part of your E18 board works. You can upload another firmware to it and try to pair it with your coordinator. Or you may use another coordinator and build a separate Zigbee network for testing. 123 | 3. Your coordinator has free slots for direct connections. 124 | 4. You permit joining on your coordinator. 125 | 5. Your device did not join to other opened Zigbee network. When you press and hold the button, it should flash every 3-4 seconds. It means that the device in the joining state. 126 | 127 | 128 | ## Components (BOM): 129 | 130 | CC2530 - https://ali.ski/x1m0vF (1 pc - $2.75) 131 | 132 | CC2530 - https://ali.ski/mjth48 (10 pcs) 133 | 134 | SHTC3 - https://ali.ski/yBYSsV (10 pcs - $15.76) 135 | 136 | Waveshare 250x122 2,13 - https://ali.ski/D4eOVU (1pc - $13.3) 137 | 138 | FPC FFC 0.5mm connector socket 24 pin - https://ali.ski/lCBot0 (1 p - $0.77) 139 | 140 | Cell Holder CR2477 - https://ali.ski/CTUHOc (20 pcs - $5) 141 | 142 | Micro Screws M1.4 3mm - https://ali.ski/gaFdO (100 pcs - $1) 143 | 144 | Micro Screws M1.4 5mm - https://ali.ski/gaFdO (100 pcs - $1) 145 | 146 | Micro SMD Tact Switch 2x4 2*4*3.5 - https://ali.ski/_D78Q (10 pcs - $1) 147 | 148 | Neodymium magnet 15x3x3m - https://ali.ski/44p3R (10 pcs - $7) 149 | 150 | Tantalum Capacitor 220UF 10V - https://ali.ski/Lx9iQd (10 pcs - $1,85) 151 | 152 | Inductor Power Shielded Wirewound NR5040 5x5x4mm - https://ali.ski/iblu8q (50 pcs - $3) 153 | 154 | Micro Button Tact Switch SMD 4Pin 3X4X2.5 - https://ali.ski/sGwFu (100 pcs - $1.7) 155 | 156 | SMD Mini Toggle Slide Switch 7-Pin On/Off - https://ali.ski/xz9Yt (100 pcs - $3.2) 157 | 158 | SI1308EDL - https://ali.ski/Q6Y12 (20 pcs - $5.3) 159 | OR 160 | Si1304BDL - https://ali.ski/938Klh (100 pcs - $15.5) 161 | 162 | SMD Chip Multilayer Ceramic Capacitor 0603 1UF 25V - https://ali.ski/p3yr60 (100 pcs - $1.5) 163 | 164 | SMD Chip Multilayer Ceramic Capacitor 0603 0.1UF 50V - https://ali.ski/p3yr60 (100 pcs - $1.5) 165 | 166 | SMD Chip Multilayer Ceramic Capacitor 0805 4.7UF - https://ali.ski/iFXAc (100 pcs - $1.85) 167 | 168 | MBR0530T1G - https://ali.ski/SJM7aK (100 pcs - $1.1) 169 | 170 | SMD LED 0805 - https://ali.ski/wb6ZP (100 pcs - $2) 171 | 172 | 1% SMD resistor 0.47R 0603 - https://ali.ski/bX9HUg (100 pcs - $1.2) 173 | 174 | 1% SMD Resistor Kit Assorted Kit 1R-1M 0603 - https://ali.ski/npItF (660 pcs - $1.45) 175 | 176 | 177 | SUNLU PLA Carbon Fiber Premium 3D Printer Filament - https://ali.ski/bQkNR (1 pcs - $26.5) 178 | -------------------------------------------------------------------------------- /PROJECT SOURCE/EINK213TH_SIMPLE_TEST/zstack-lib/commissioning.c: -------------------------------------------------------------------------------- 1 | #include "commissioning.h" 2 | #include "Debug.h" 3 | #include "OSAL_PwrMgr.h" 4 | #include "ZDApp.h" 5 | #include "bdb_interface.h" 6 | #include "hal_key.h" 7 | #include "hal_led.h" 8 | 9 | static void zclCommissioning_ProcessCommissioningStatus(bdbCommissioningModeMsg_t *bdbCommissioningModeMsg); 10 | static void zclCommissioning_ResetBackoffRetry(void); 11 | static void zclCommissioning_BindNotification(bdbBindNotificationData_t *data); 12 | extern bool requestNewTrustCenterLinkKey; 13 | 14 | byte rejoinsLeft = APP_COMMISSIONING_END_DEVICE_REJOIN_TRIES; 15 | uint32 rejoinDelay = APP_COMMISSIONING_END_DEVICE_REJOIN_START_DELAY; 16 | 17 | uint8 zclCommissioning_TaskId = 0; 18 | 19 | #ifndef APP_TX_POWER 20 | #define APP_TX_POWER TX_PWR_PLUS_4 21 | #endif 22 | 23 | void zclCommissioning_Init(uint8 task_id) { 24 | zclCommissioning_TaskId = task_id; 25 | 26 | bdb_RegisterCommissioningStatusCB(zclCommissioning_ProcessCommissioningStatus); 27 | bdb_RegisterBindNotificationCB(zclCommissioning_BindNotification); 28 | 29 | ZMacSetTransmitPower(APP_TX_POWER); 30 | 31 | // this is important to allow connects throught routers 32 | // to make this work, coordinator should be compiled with this flag #define TP2_LEGACY_ZC 33 | requestNewTrustCenterLinkKey = FALSE; 34 | bdb_StartCommissioning(BDB_COMMISSIONING_MODE_NWK_STEERING | BDB_COMMISSIONING_MODE_FINDING_BINDING); 35 | } 36 | 37 | static void zclCommissioning_ResetBackoffRetry(void) { 38 | rejoinsLeft = APP_COMMISSIONING_END_DEVICE_REJOIN_TRIES; 39 | rejoinDelay = APP_COMMISSIONING_END_DEVICE_REJOIN_START_DELAY; 40 | } 41 | 42 | static void zclCommissioning_OnConnect(void) { 43 | LREPMaster("zclCommissioning_OnConnect \r\n"); 44 | zclCommissioning_ResetBackoffRetry(); 45 | osal_start_timerEx(zclCommissioning_TaskId, APP_COMMISSIONING_CLOCK_DOWN_POLING_RATE_EVT, 10 * 1000); 46 | } 47 | 48 | static void zclCommissioning_ProcessCommissioningStatus(bdbCommissioningModeMsg_t *bdbCommissioningModeMsg) { 49 | LREP("bdbCommissioningMode=%d bdbCommissioningStatus=%d bdbRemainingCommissioningModes=0x%X\r\n", 50 | bdbCommissioningModeMsg->bdbCommissioningMode, bdbCommissioningModeMsg->bdbCommissioningStatus, 51 | bdbCommissioningModeMsg->bdbRemainingCommissioningModes); 52 | switch (bdbCommissioningModeMsg->bdbCommissioningMode) { 53 | case BDB_COMMISSIONING_INITIALIZATION: 54 | switch (bdbCommissioningModeMsg->bdbCommissioningStatus) { 55 | case BDB_COMMISSIONING_NO_NETWORK: 56 | LREP("No network\r\n"); 57 | HalLedBlink(HAL_LED_1, 2, 60, 800); 58 | break; 59 | case BDB_COMMISSIONING_NETWORK_RESTORED: 60 | zclCommissioning_OnConnect(); 61 | break; 62 | default: 63 | break; 64 | } 65 | break; 66 | case BDB_COMMISSIONING_NWK_STEERING: 67 | switch (bdbCommissioningModeMsg->bdbCommissioningStatus) { 68 | case BDB_COMMISSIONING_SUCCESS: 69 | HalLedBlink(HAL_LED_1, 7, 60, 400); 70 | LREPMaster("BDB_COMMISSIONING_SUCCESS\r\n"); 71 | zclCommissioning_OnConnect(); 72 | break; 73 | 74 | default: 75 | HalLedSet(HAL_LED_1, HAL_LED_MODE_BLINK); 76 | break; 77 | } 78 | 79 | break; 80 | 81 | case BDB_COMMISSIONING_PARENT_LOST: 82 | LREPMaster("BDB_COMMISSIONING_PARENT_LOST\r\n"); 83 | switch (bdbCommissioningModeMsg->bdbCommissioningStatus) { 84 | case BDB_COMMISSIONING_NETWORK_RESTORED: 85 | zclCommissioning_ResetBackoffRetry(); 86 | break; 87 | 88 | default: 89 | HalLedSet(HAL_LED_1, HAL_LED_MODE_BLINK); 90 | // // Parent not found, attempt to rejoin again after a exponential backoff delay 91 | LREP("rejoinsLeft %d rejoinDelay=%ld\r\n", rejoinsLeft, rejoinDelay); 92 | if (rejoinsLeft > 0) { 93 | rejoinDelay *= APP_COMMISSIONING_END_DEVICE_REJOIN_BACKOFF; 94 | rejoinsLeft -= 1; 95 | } else { 96 | rejoinDelay = APP_COMMISSIONING_END_DEVICE_REJOIN_MAX_DELAY; 97 | } 98 | osal_start_timerEx(zclCommissioning_TaskId, APP_COMMISSIONING_END_DEVICE_REJOIN_EVT, rejoinDelay); 99 | break; 100 | } 101 | break; 102 | default: 103 | break; 104 | } 105 | } 106 | 107 | static void zclCommissioning_ProcessIncomingMsg(zclIncomingMsg_t *pInMsg) { 108 | if (pInMsg->attrCmd) { 109 | osal_mem_free(pInMsg->attrCmd); 110 | } 111 | } 112 | 113 | void zclCommissioning_Sleep(uint8 allow) { 114 | LREP("zclCommissioning_Sleep %d\r\n", allow); 115 | #if defined(POWER_SAVING) 116 | if (allow) { 117 | NLME_SetPollRate(0); 118 | } else { 119 | NLME_SetPollRate(POLL_RATE); 120 | } 121 | #endif 122 | } 123 | 124 | uint16 zclCommissioning_event_loop(uint8 task_id, uint16 events) { 125 | if (events & SYS_EVENT_MSG) { 126 | devStates_t zclApp_NwkState; 127 | afIncomingMSGPacket_t *MSGpkt; 128 | while ((MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive(zclCommissioning_TaskId))) { 129 | 130 | switch (MSGpkt->hdr.event) { 131 | case ZDO_STATE_CHANGE: 132 | HalLedSet(HAL_LED_1, HAL_LED_MODE_BLINK); 133 | zclApp_NwkState = (devStates_t)(MSGpkt->hdr.status); 134 | LREP("NwkState=%d\r\n", zclApp_NwkState); 135 | if (zclApp_NwkState == DEV_END_DEVICE) { 136 | HalLedSet(HAL_LED_1, HAL_LED_MODE_OFF); 137 | } 138 | break; 139 | 140 | case ZCL_INCOMING_MSG: 141 | zclCommissioning_ProcessIncomingMsg((zclIncomingMsg_t *)MSGpkt); 142 | break; 143 | 144 | default: 145 | break; 146 | } 147 | 148 | // Release the memory 149 | osal_msg_deallocate((uint8 *)MSGpkt); 150 | } 151 | 152 | // return unprocessed events 153 | return (events ^ SYS_EVENT_MSG); 154 | } 155 | if (events & APP_COMMISSIONING_END_DEVICE_REJOIN_EVT) { 156 | LREPMaster("APP_END_DEVICE_REJOIN_EVT\r\n"); 157 | #if ZG_BUILD_ENDDEVICE_TYPE 158 | bdb_ZedAttemptRecoverNwk(); 159 | #endif 160 | return (events ^ APP_COMMISSIONING_END_DEVICE_REJOIN_EVT); 161 | } 162 | 163 | if (events & APP_COMMISSIONING_CLOCK_DOWN_POLING_RATE_EVT) { 164 | LREPMaster("APP_CLOCK_DOWN_POLING_RATE_EVT\r\n"); 165 | zclCommissioning_Sleep(true); 166 | return (events ^ APP_COMMISSIONING_CLOCK_DOWN_POLING_RATE_EVT); 167 | } 168 | 169 | // Discard unknown events 170 | return 0; 171 | } 172 | 173 | static void zclCommissioning_BindNotification(bdbBindNotificationData_t *data) { 174 | HalLedSet(HAL_LED_1, HAL_LED_MODE_BLINK); 175 | LREP("Recieved bind request clusterId=0x%X dstAddr=0x%X ep=%d\r\n", data->clusterId, data->dstAddr, data->ep); 176 | uint16 maxEntries = 0, usedEntries = 0; 177 | bindCapacity(&maxEntries, &usedEntries); 178 | LREP("bindCapacity %d %usedEntries %d \r\n", maxEntries, usedEntries); 179 | } 180 | 181 | void zclCommissioning_HandleKeys(uint8 portAndAction, uint8 keyCode) { 182 | if (portAndAction & HAL_KEY_PRESS) { 183 | #if ZG_BUILD_ENDDEVICE_TYPE 184 | if (devState == DEV_NWK_ORPHAN) { 185 | LREP("devState=%d try to restore network\r\n", devState); 186 | bdb_ZedAttemptRecoverNwk(); 187 | } 188 | #endif 189 | } 190 | #if defined(POWER_SAVING) 191 | NLME_SetPollRate(1); 192 | #endif 193 | } -------------------------------------------------------------------------------- /PROJECT SOURCE/EINK213TH_SIMPLE_TEST/Source/spi.c: -------------------------------------------------------------------------------- 1 | #include "spi.h" 2 | #include "Debug.h" 3 | #include "hal_key.h" 4 | #include 5 | 6 | /************************************************************************************************** 7 | * CONSTANTS 8 | **************************************************************************************************/ 9 | /* 10 | LCD pins 11 | 12 | //control 13 | P0.0 - LCD_MODE (DC EPD) 14 | P1.1 - LCD_FLASH_RESET (RST EPD) 15 | P0.7 - LCD_CS (CS EPD) 16 | P0.4 - LCD_BUSY (BUSY EPD) 17 | 18 | //spi 19 | P1.5 - CLK 20 | P1.6 - MOSI 21 | P1.7 - MISO 22 | */ 23 | 24 | /* LCD Control lines */ 25 | #ifndef HAL_LCD_MODE_POR 26 | #define HAL_LCD_MODE_PORT 0 27 | #endif 28 | #ifndef HAL_LCD_MODE_PIN 29 | #define HAL_LCD_MODE_PIN 0 30 | #endif 31 | 32 | #ifndef HAL_LCD_RESET_PORT 33 | #define HAL_LCD_RESET_PORT 1 34 | #endif 35 | #ifndef HAL_LCD_RESET_PIN 36 | #define HAL_LCD_RESET_PIN 1 37 | #endif 38 | 39 | #ifndef HAL_LCD_CS_PORT 40 | #define HAL_LCD_CS_PORT 0 41 | #endif 42 | #ifndef HAL_LCD_CS_PIN 43 | #define HAL_LCD_CS_PIN 7 44 | #endif 45 | 46 | #ifndef HAL_LCD_BUSY_PORT 47 | #define HAL_LCD_BUSY_PORT 0 48 | #endif 49 | #ifndef HAL_LCD_BUSY_PIN 50 | #define HAL_LCD_BUSY_PIN 4 51 | #endif 52 | 53 | /* LCD SPI lines */ 54 | #define HAL_LCD_CLK_PORT 1 55 | #define HAL_LCD_CLK_PIN 5 56 | 57 | #define HAL_LCD_MOSI_PORT 1 58 | #define HAL_LCD_MOSI_PIN 6 59 | 60 | #define HAL_LCD_MISO_PORT 1 61 | #define HAL_LCD_MISO_PIN 7 62 | 63 | /* SPI settings */ 64 | 65 | #define HAL_SPI_CLOCK_POL_LO 0x00 // CPOL 0 0x00, CPOL 1 0x80 66 | #define HAL_SPI_CLOCK_PHA_0 0x00 // CPHA 0 0x00, CPHA 1 0x40 67 | #define HAL_SPI_TRANSFER_MSB_FIRST 0x20 // ORDER 0 0x00 LSB first, ORDER 1 0x20 MSB first 68 | 69 | #define HAL_IO_SET(port, pin, val) HAL_IO_SET_PREP(port, pin, val) 70 | #define HAL_IO_SET_PREP(port, pin, val) st( P##port##_##pin = val; ) 71 | 72 | #define HAL_CONFIG_IO_OUTPUT(port, pin, val) HAL_CONFIG_IO_OUTPUT_PREP(port, pin, val) 73 | #define HAL_CONFIG_IO_OUTPUT_PREP(port, pin, val) st( P##port##SEL &= ~BV(pin); \ 74 | P##port##_##pin = val; \ 75 | P##port##DIR |= BV(pin); ) 76 | 77 | #define HAL_CONFIG_IO_INPUT(port, pin, val) HAL_CONFIG_IO_INPUT_PREP(port, pin, val) 78 | #define HAL_CONFIG_IO_INPUT_PREP(port, pin, val) st( P##port##SEL &= ~BV(pin); \ 79 | P##port##_##pin = val; \ 80 | P##port##DIR &= ~BV(pin); ) 81 | 82 | #define HAL_CONFIG_IO_PERIPHERAL(port, pin) HAL_CONFIG_IO_PERIPHERAL_PREP(port, pin) 83 | #define HAL_CONFIG_IO_PERIPHERAL_PREP(port, pin) st( P##port##SEL |= BV(pin); ) 84 | 85 | #define HAL_CONFIG_IO_GP(port, pin) HAL_CONFIG_IO_GP_PREP(port, pin) 86 | #define HAL_CONFIG_IO_GP_PREP(port, pin) st( P##port##SEL &= ~BV(pin); ) 87 | 88 | 89 | /* SPI interface control */ 90 | #define LCD_SPI_BEGIN() HAL_IO_SET(HAL_LCD_CS_PORT, HAL_LCD_CS_PIN, 0); /* chip select */ 91 | #define LCD_SPI_END() \ 92 | { \ 93 | asm("NOP"); \ 94 | asm("NOP"); \ 95 | asm("NOP"); \ 96 | asm("NOP"); \ 97 | HAL_IO_SET(HAL_LCD_CS_PORT, HAL_LCD_CS_PIN, 1); /* chip select */ \ 98 | } 99 | 100 | /* clear the received and transmit byte status, write tx data to buffer, wait till transmit done */ 101 | #define LCD_SPI_TX(x) { U1CSR &= ~(BV(2) | BV(1)); U1DBUF = x; while( !(U1CSR & BV(1)) ); } 102 | #define LCD_SPI_WAIT_RXRDY() { while(!(U1CSR & BV(1))); } 103 | 104 | 105 | /* Control macros */ 106 | #define LCD_DO_WRITE() HAL_IO_SET(HAL_LCD_MODE_PORT, HAL_LCD_MODE_PIN, 1); 107 | #define LCD_DO_CONTROL() HAL_IO_SET(HAL_LCD_MODE_PORT, HAL_LCD_MODE_PIN, 0); 108 | 109 | #define LCD_ACTIVATE_RESET() HAL_IO_SET(HAL_LCD_RESET_PORT, HAL_LCD_RESET_PIN, 0); 110 | #define LCD_RELEASE_RESET() HAL_IO_SET(HAL_LCD_RESET_PORT, HAL_LCD_RESET_PIN, 1); 111 | 112 | void spi_HW_WaitUs(uint16 i); 113 | 114 | void SPIInit(void); 115 | 116 | void HalLcd_HW_Init(void); 117 | void HalLcd_HW_Control(uint8 cmd); 118 | void HalLcd_HW_Write(uint8 data); 119 | 120 | 121 | void SPIInit(void) { 122 | 123 | /* Initialize LCD IO lines */ 124 | spi_ConfigIO(); 125 | 126 | /* Initialize SPI */ 127 | spi_ConfigSPI(); 128 | } 129 | 130 | 131 | void HalLcd_HW_Init(void){ 132 | /* Perform reset */ 133 | LCD_ACTIVATE_RESET(); 134 | spi_HW_WaitUs(15000); // 15 ms 135 | LCD_RELEASE_RESET(); 136 | spi_HW_WaitUs(15000); // 15 us 137 | } 138 | 139 | 140 | void spi_ConfigIO(void) 141 | { 142 | /* GPIO configuration */ 143 | HAL_CONFIG_IO_OUTPUT(HAL_LCD_MODE_PORT, HAL_LCD_MODE_PIN, 1); 144 | HAL_CONFIG_IO_OUTPUT(HAL_LCD_RESET_PORT, HAL_LCD_RESET_PIN, 1); 145 | HAL_CONFIG_IO_OUTPUT(HAL_LCD_CS_PORT, HAL_LCD_CS_PIN, 1); 146 | HAL_CONFIG_IO_INPUT (HAL_LCD_BUSY_PORT, HAL_LCD_BUSY_PIN, 0); 147 | // P2INP |= HAL_KEY_BIT5; // pull down 148 | // P2INP &= ~HAL_KEY_BIT5; // pull up 149 | } 150 | 151 | 152 | void spi_ConfigSPI(void) 153 | { 154 | /* UART/SPI Peripheral configuration */ 155 | 156 | uint8 baud_exponent; 157 | uint8 baud_mantissa; 158 | 159 | /* Set SPI on UART 1 alternative 2 */ 160 | PERCFG |= 0x02; 161 | 162 | /* Configure clk, master out and master in lines */ 163 | HAL_CONFIG_IO_PERIPHERAL(HAL_LCD_CLK_PORT, HAL_LCD_CLK_PIN); 164 | HAL_CONFIG_IO_PERIPHERAL(HAL_LCD_MOSI_PORT, HAL_LCD_MOSI_PIN); 165 | HAL_CONFIG_IO_PERIPHERAL(HAL_LCD_MISO_PORT, HAL_LCD_MISO_PIN); 166 | 167 | 168 | /* Set SPI speed to 1 MHz (the values assume system clk of 32MHz) 169 | * Confirm on board that this results in 1MHz spi clk. 170 | */ 171 | baud_exponent = 15; 172 | baud_mantissa = 0; 173 | 174 | /* Configure SPI */ 175 | U1UCR = 0x00; /* Flush and goto IDLE state. 8-N-1. */ 176 | U1CSR = 0x00; /* SPI mode, master. */ 177 | U1GCR = HAL_SPI_TRANSFER_MSB_FIRST | HAL_SPI_CLOCK_PHA_0 | HAL_SPI_CLOCK_POL_LO | baud_exponent; 178 | U1BAUD = baud_mantissa; 179 | } 180 | 181 | 182 | void spi_ConfigGP(void) 183 | { 184 | /* Set SPI on UART 1 alternative 2 */ 185 | PERCFG &= ~0x02; 186 | 187 | /* Configure clk, master out and master in lines */ 188 | HAL_CONFIG_IO_GP(HAL_LCD_CLK_PORT, HAL_LCD_CLK_PIN); 189 | HAL_CONFIG_IO_GP(HAL_LCD_MOSI_PORT, HAL_LCD_MOSI_PIN); 190 | HAL_CONFIG_IO_GP(HAL_LCD_MISO_PORT, HAL_LCD_MISO_PIN); 191 | } 192 | 193 | 194 | void spi_HW_WaitUs(uint16 microSecs) 195 | { 196 | while(microSecs--) 197 | { 198 | /* 32 NOPs == 1 usecs */ 199 | asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); 200 | asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); 201 | asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); 202 | asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); 203 | asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); 204 | asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); 205 | asm("nop"); asm("nop"); 206 | } 207 | } 208 | 209 | 210 | /************************************************************************************************** 211 | * @fn HalLcd_HW_Control 212 | * 213 | * @brief Write 1 command to the LCD 214 | * 215 | * @param uint8 cmd - command to be written to the LCD 216 | * 217 | * @return None 218 | **************************************************************************************************/ 219 | void HalLcd_HW_Control(uint8 cmd) 220 | { 221 | LCD_SPI_BEGIN(); 222 | LCD_DO_CONTROL(); 223 | LCD_SPI_TX(cmd); 224 | LCD_SPI_WAIT_RXRDY(); 225 | LCD_SPI_END(); 226 | } 227 | 228 | /************************************************************************************************** 229 | * @fn HalLcd_HW_Write 230 | * 231 | * @brief Write 1 byte to the LCD 232 | * 233 | * @param uint8 data - data to be written to the LCD 234 | * 235 | * @return None 236 | **************************************************************************************************/ 237 | void HalLcd_HW_Write(uint8 data) 238 | { 239 | LCD_SPI_BEGIN(); 240 | LCD_DO_WRITE(); 241 | LCD_SPI_TX(data); 242 | LCD_SPI_WAIT_RXRDY(); 243 | LCD_SPI_END(); 244 | } -------------------------------------------------------------------------------- /PROJECT SOURCE/EINK213TH_SIMPLE_TEST/zstack-lib/hal_key.c: -------------------------------------------------------------------------------- 1 | /************************************************************************************************** 2 | * INCLUDES 3 | **************************************************************************************************/ 4 | #include "hal_key.h" 5 | #include "Debug.h" 6 | #include "OnBoard.h" 7 | #include "utils.h" 8 | 9 | #include "hal_adc.h" 10 | #include "hal_defs.h" 11 | #include "hal_drivers.h" 12 | #include "hal_led.h" 13 | #include "hal_mcu.h" 14 | #include "hal_types.h" 15 | #include "osal.h" 16 | 17 | /************************************************************************************************** 18 | * MACROS 19 | **************************************************************************************************/ 20 | 21 | #ifndef HAL_KEY_P0_INPUT_PINS 22 | #define HAL_KEY_P0_INPUT_PINS 0x00 23 | #endif 24 | 25 | #ifndef HAL_KEY_P1_INPUT_PINS 26 | #define HAL_KEY_P1_INPUT_PINS 0x00 27 | #endif 28 | 29 | #ifndef HAL_KEY_P2_INPUT_PINS 30 | #define HAL_KEY_P2_INPUT_PINS 0x00 31 | #endif 32 | 33 | 34 | #ifndef HAL_KEY_P0_INPUT_PINS_EDGE 35 | #define HAL_KEY_P0_INPUT_PINS_EDGE HAL_KEY_FALLING_EDGE 36 | #endif 37 | 38 | #ifndef HAL_KEY_P1_INPUT_PINS_EDGE 39 | #define HAL_KEY_P1_INPUT_PINS_EDGE HAL_KEY_FALLING_EDGE 40 | #endif 41 | 42 | #ifndef HAL_KEY_P2_INPUT_PINS_EDGE 43 | #define HAL_KEY_P2_INPUT_PINS_EDGE HAL_KEY_FALLING_EDGE 44 | #endif 45 | /************************************************************************************************** 46 | * CONSTANTS 47 | **************************************************************************************************/ 48 | 49 | uint8 portNum = 0; 50 | uint8 pinNum = 0; 51 | 52 | #define HAL_KEY_DEBOUNCE_VALUE 25 53 | 54 | #define HAL_KEY_P0_EDGE_BITS HAL_KEY_BIT0 55 | #define HAL_KEY_P1_EDGE_BITS (HAL_KEY_BIT1 | HAL_KEY_BIT2) 56 | #define HAL_KEY_P2_EDGE_BITS HAL_KEY_BIT3 57 | 58 | /************************************************************************************************** 59 | * TYPEDEFS 60 | **************************************************************************************************/ 61 | 62 | /************************************************************************************************** 63 | * GLOBAL VARIABLES 64 | **************************************************************************************************/ 65 | bool Hal_KeyIntEnable; 66 | /************************************************************************************************** 67 | * FUNCTIONS - Local 68 | **************************************************************************************************/ 69 | void halProcessKeyInterrupt(uint8 portNum); 70 | 71 | void HalKeyPoll(void) { 72 | uint8 pinStatus = 0; 73 | bool isPressed = false; 74 | switch (portNum) { 75 | case HAL_KEY_PORT0: 76 | PICTL ^= HAL_KEY_P0_EDGE_BITS; // flip edge bit 77 | pinStatus = P0 & pinNum; 78 | isPressed = HAL_KEY_P0_INPUT_PINS_EDGE != !!(pinStatus); 79 | break; 80 | 81 | case HAL_KEY_PORT1: 82 | PICTL ^= HAL_KEY_P1_EDGE_BITS; // flip edge bit 83 | pinStatus = P1 & pinNum; 84 | isPressed = HAL_KEY_P1_INPUT_PINS_EDGE != !!(pinStatus); 85 | break; 86 | 87 | case HAL_KEY_PORT2: 88 | PICTL ^= HAL_KEY_P2_EDGE_BITS; // flip edge bit 89 | pinStatus = P2 & pinNum; 90 | isPressed = HAL_KEY_P2_INPUT_PINS_EDGE != !!(pinStatus); 91 | break; 92 | 93 | default: 94 | break; 95 | } 96 | LREP("portNum=0x%X pinNum=0x%X isPressed=%d\r\n", portNum, pinNum, isPressed); 97 | 98 | // LREP("pinStatus=" BYTE_TO_BINARY_PATTERN "\r\n", BYTE_TO_BINARY(pinStatus)); 99 | OnBoard_SendKeys(pinNum, (isPressed ? HAL_KEY_PRESS : HAL_KEY_RELEASE) | portNum); 100 | } 101 | 102 | void HalKeyInit(void) { 103 | #if HAL_KEY_P0_INPUT_PINS 104 | P0SEL &= ~HAL_KEY_P0_INPUT_PINS; 105 | P0DIR &= ~(HAL_KEY_P0_INPUT_PINS); 106 | #endif 107 | 108 | #if HAL_KEY_P1_INPUT_PINS 109 | P1SEL &= ~HAL_KEY_P1_INPUT_PINS; 110 | P1DIR &= ~(HAL_KEY_P1_INPUT_PINS); 111 | #endif 112 | 113 | #if HAL_KEY_P2_INPUT_PINS 114 | P2SEL &= ~HAL_KEY_P2_INPUT_PINS; 115 | P2DIR &= ~(HAL_KEY_P2_INPUT_PINS); 116 | #endif 117 | 118 | } 119 | 120 | void HalKeyConfig(bool interruptEnable, halKeyCBack_t cback) { 121 | Hal_KeyIntEnable = true; 122 | 123 | #if HAL_KEY_P0_INPUT_PINS 124 | P0IEN |= HAL_KEY_P0_INPUT_PINS; 125 | IEN1 |= HAL_KEY_BIT5; // enable port0 int 126 | P0INP &= ~HAL_KEY_P0_INPUT_PINS; // Pullup/pulldown 127 | #if (HAL_KEY_P0_INPUT_PINS_EDGE == HAL_KEY_FALLING_EDGE) 128 | P2INP &= ~HAL_KEY_BIT5; // pull up 129 | MicroWait(50); 130 | PICTL |= HAL_KEY_P0_EDGE_BITS; // set falling edge on port 131 | #else 132 | P2INP |= HAL_KEY_BIT5; // pull down 133 | MicroWait(50); 134 | PICTL &= ~(HAL_KEY_P0_EDGE_BITS); 135 | #endif 136 | 137 | #endif 138 | 139 | #if HAL_KEY_P1_INPUT_PINS 140 | P1IEN |= HAL_KEY_P1_INPUT_PINS; 141 | IEN2 |= HAL_KEY_BIT4; // enable port1 int 142 | P1INP &= ~HAL_KEY_P1_INPUT_PINS; //Pullup/pulldown 143 | #if (HAL_KEY_P1_INPUT_PINS_EDGE == HAL_KEY_FALLING_EDGE) 144 | P2INP &= ~HAL_KEY_BIT6; // pull up 145 | MicroWait(50); 146 | PICTL |= HAL_KEY_P1_EDGE_BITS; // set falling edge on port 147 | #else 148 | P2INP |= HAL_KEY_BIT6; // pull down 149 | MicroWait(50); 150 | PICTL &= ~HAL_KEY_P1_EDGE_BITS; 151 | #endif 152 | 153 | #endif 154 | 155 | #if HAL_KEY_P2_INPUT_PINS 156 | P2IEN |= HAL_KEY_P2_INPUT_PINS; 157 | IEN2 |= HAL_KEY_BIT1; // enable port2 int 158 | P2INP &= ~HAL_KEY_P2_INPUT_PINS; //Pullup/pulldown 159 | #if (HAL_KEY_P2_INPUT_PINS_EDGE == HAL_KEY_FALLING_EDGE) 160 | P2INP &= ~HAL_KEY_BIT7; // pull up 161 | MicroWait(50); 162 | PICTL |= HAL_KEY_P2_EDGE_BITS; // set falling edge on port 163 | #else 164 | P2INP |= HAL_KEY_BIT7; // pull down 165 | MicroWait(50); 166 | PICTL &= ~HAL_KEY_P2_EDGE_BITS; 167 | #endif 168 | 169 | #endif 170 | } 171 | 172 | void halProcessKeyInterrupt(uint8 _portNum) { 173 | portNum = _portNum; 174 | switch (_portNum) { 175 | case HAL_KEY_PORT0: 176 | pinNum = P0IFG & HAL_KEY_P0_INPUT_PINS; 177 | break; 178 | 179 | case HAL_KEY_PORT1: 180 | pinNum = P1IFG & HAL_KEY_P1_INPUT_PINS; 181 | break; 182 | 183 | case HAL_KEY_PORT2: 184 | pinNum = P2IFG & HAL_KEY_P2_INPUT_PINS; 185 | break; 186 | default: 187 | break; 188 | } 189 | osal_start_timerEx(Hal_TaskID, HAL_KEY_EVENT, HAL_KEY_DEBOUNCE_VALUE); 190 | } 191 | 192 | void HalKeyEnterSleep(void) { 193 | uint8 clkcmd = CLKCONCMD; 194 | uint8 clksta = CLKCONSTA; 195 | // Switch to 16MHz before setting the DC/DC to bypass to reduce risk of flash corruption 196 | CLKCONCMD = (CLKCONCMD_16MHZ | OSC_32KHZ); 197 | // wait till clock speed stablizes 198 | while (CLKCONSTA != (CLKCONCMD_16MHZ | OSC_32KHZ)) 199 | ; 200 | 201 | CLKCONCMD = clkcmd; 202 | while (CLKCONSTA != (clksta)) 203 | ; 204 | } 205 | 206 | uint8 HalKeyExitSleep(void) { 207 | uint8 clkcmd = CLKCONCMD; 208 | // Switch to 16MHz before setting the DC/DC to on to reduce risk of flash corruption 209 | CLKCONCMD = (CLKCONCMD_16MHZ | OSC_32KHZ); 210 | // wait till clock speed stablizes 211 | while (CLKCONSTA != (CLKCONCMD_16MHZ | OSC_32KHZ)) 212 | ; 213 | 214 | CLKCONCMD = clkcmd; 215 | 216 | // /* Wake up and read keys */ 217 | return (HalKeyRead()); 218 | } 219 | 220 | #if HAL_KEY_P0_INPUT_PINS 221 | HAL_ISR_FUNCTION(halKeyPort0Isr, P0INT_VECTOR) { 222 | HAL_ENTER_ISR(); 223 | 224 | if (P0IFG & HAL_KEY_P0_INPUT_PINS) { 225 | halProcessKeyInterrupt(HAL_KEY_PORT0); 226 | } 227 | 228 | P0IFG = 0; //&= ~HAL_KEY_P0_INPUT_PINS; 229 | P0IF = 0; 230 | 231 | CLEAR_SLEEP_MODE(); 232 | HAL_EXIT_ISR(); 233 | } 234 | #endif 235 | 236 | #if HAL_KEY_P1_INPUT_PINS 237 | HAL_ISR_FUNCTION(halKeyPort1Isr, P1INT_VECTOR) { 238 | HAL_ENTER_ISR(); 239 | 240 | if (P1IFG & HAL_KEY_P1_INPUT_PINS) { 241 | halProcessKeyInterrupt(HAL_KEY_PORT1); 242 | } 243 | 244 | P1IFG = 0; //&= ~HAL_KEY_P1_INPUT_PINS; 245 | P1IF = 0; 246 | 247 | CLEAR_SLEEP_MODE(); 248 | HAL_EXIT_ISR(); 249 | } 250 | #endif 251 | 252 | #if HAL_KEY_P2_INPUT_PINS 253 | HAL_ISR_FUNCTION(halKeyPort2Isr, P2INT_VECTOR) { 254 | HAL_ENTER_ISR(); 255 | 256 | if (P2IFG & HAL_KEY_P2_INPUT_PINS) { 257 | halProcessKeyInterrupt(HAL_KEY_PORT2); 258 | } 259 | 260 | P2IFG = 0; //&= ~HAL_KEY_P2_INPUT_PINS; 261 | P2IF = 0; 262 | 263 | CLEAR_SLEEP_MODE(); 264 | HAL_EXIT_ISR(); 265 | } 266 | #endif 267 | 268 | 269 | 270 | uint8 HalKeyRead ( void ){ 271 | return 0; 272 | } -------------------------------------------------------------------------------- /PROJECT SOURCE/EINK213TH_SIMPLE_TEST/Source/ssd1675.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | 5 | #include "ssd1675.h" 6 | #include "spi.h" // HalLcd_HW_Control(), HalLcd_HW_Write() 7 | #include "imagedata.h" 8 | #include "epdpaint.h" 9 | #include "utils.h" 10 | 11 | #define HAL_LCD_BUSY BNAME(HAL_LCD_BUSY_PORT, HAL_LCD_BUSY_PIN) 12 | 13 | void DelayMs(unsigned int delaytime); 14 | 15 | unsigned long epd_width = EPD_WIDTH; 16 | unsigned long epd_height = EPD_HEIGHT; 17 | 18 | 19 | void EpdInitFull(void) { 20 | EpdReset(); 21 | 22 | WaitUntilIdle(); 23 | EpdSendCommand(SW_RESET); //SWRESET 24 | WaitUntilIdle(); 25 | 26 | EpdSendCommand(0x74); //set analog block control 27 | EpdSendData(0x54); 28 | EpdSendCommand(0x7E); //set digital block control 29 | EpdSendData(0x3B); 30 | 31 | EpdSendCommand(DRIVER_OUTPUT_CONTROL); //Driver output control 32 | EpdSendData(0xF9); 33 | EpdSendData(0x00); 34 | EpdSendData(0x00); 35 | 36 | EpdSendCommand(DATA_ENTRY_MODE_SETTING); //data entry mode 37 | EpdSendData(0x03); 38 | 39 | EpdSetMemoryArea(0, 0, epd_width - 1, epd_height - 1); 40 | 41 | EpdSendCommand(BORDER_WAVEFORM_CONTROL); 42 | EpdSendData(0x03); 43 | 44 | EpdSendCommand(WRITE_VCOM_REGISTER); //VCOM Voltage 45 | EpdSendData(0x55); // 46 | 47 | EpdSendCommand(GATE_VOLTAGE_CONTROL); 48 | EpdSendData(lut_full_update[70]); 49 | 50 | EpdSendCommand(SOURCE_VOLTAGE_CONTROL); // 51 | EpdSendData(lut_full_update[71]); 52 | EpdSendData(lut_full_update[72]); 53 | EpdSendData(lut_full_update[73]); 54 | 55 | EpdSendCommand(SET_DUMMY_LINE_PERIOD); //Dummy Line 56 | EpdSendData(lut_full_update[74]); 57 | EpdSendCommand(SET_GATE_TIME); //Gate time 58 | EpdSendData(lut_full_update[75]); 59 | 60 | EpdSetMemoryPointer(0, 0); 61 | 62 | EpdSetLutFull(lut_full_update); 63 | } 64 | 65 | 66 | void EpdInitPartial(void) { 67 | EpdReset(); 68 | 69 | EpdSendCommand(WRITE_VCOM_REGISTER); //VCOM Voltage 70 | EpdSendData(0x26); 71 | 72 | WaitUntilIdle(); 73 | 74 | EpdSetLut(lut_partial_update); 75 | } 76 | 77 | 78 | void EpdSendCommand(unsigned char command) { 79 | HalLcd_HW_Control(command); 80 | } 81 | 82 | 83 | void EpdSendData(unsigned char data) { 84 | HalLcd_HW_Write(data); 85 | } 86 | 87 | 88 | void WaitUntilIdle(void) { 89 | while (1) { //LOW: idle, HIGH: busy 90 | if (HAL_LCD_BUSY == 0) 91 | break; 92 | DelayMs(20); 93 | } 94 | } 95 | 96 | 97 | 98 | void DelayMs(unsigned int delaytime) { 99 | while(delaytime--) 100 | { 101 | uint16 microSecs = 1000; 102 | while(microSecs--) 103 | { 104 | asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); 105 | } 106 | } 107 | } 108 | 109 | const unsigned char lut_full_update[]= { 110 | 0x80,0x60,0x40,0x00,0x00,0x00,0x00, //LUT0: BB: VS 0 ~7 111 | 0x10,0x60,0x20,0x00,0x00,0x00,0x00, //LUT1: BW: VS 0 ~7 112 | 0x80,0x60,0x40,0x00,0x00,0x00,0x00, //LUT2: WB: VS 0 ~7 113 | 0x10,0x60,0x20,0x00,0x00,0x00,0x00, //LUT3: WW: VS 0 ~7 114 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00, //LUT4: VCOM: VS 0 ~7 115 | 116 | 0x03,0x03,0x00,0x00,0x02, // TP0 A~D RP0 117 | 0x09,0x09,0x00,0x00,0x02, // TP1 A~D RP1 118 | 0x03,0x03,0x00,0x00,0x02, // TP2 A~D RP2 119 | 0x00,0x00,0x00,0x00,0x00, // TP3 A~D RP3 120 | 0x00,0x00,0x00,0x00,0x00, // TP4 A~D RP4 121 | 0x00,0x00,0x00,0x00,0x00, // TP5 A~D RP5 122 | 0x00,0x00,0x00,0x00,0x00, // TP6 A~D RP6 123 | 124 | 0x15,0x41,0xA8,0x32,0x30,0x0A, 125 | }; 126 | 127 | const unsigned char lut_partial_update[]= { //20 bytes 128 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00, //LUT0: BB: VS 0 ~7 129 | 0x80,0x00,0x00,0x00,0x00,0x00,0x00, //LUT1: BW: VS 0 ~7 130 | 0x40,0x00,0x00,0x00,0x00,0x00,0x00, //LUT2: WB: VS 0 ~7 131 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00, //LUT3: WW: VS 0 ~7 132 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00, //LUT4: VCOM: VS 0 ~7 133 | 134 | 0x0A,0x00,0x00,0x00,0x00, // TP0 A~D RP0 135 | 0x00,0x00,0x00,0x00,0x00, // TP1 A~D RP1 136 | 0x00,0x00,0x00,0x00,0x00, // TP2 A~D RP2 137 | 0x00,0x00,0x00,0x00,0x00, // TP3 A~D RP3 138 | 0x00,0x00,0x00,0x00,0x00, // TP4 A~D RP4 139 | 0x00,0x00,0x00,0x00,0x00, // TP5 A~D RP5 140 | 0x00,0x00,0x00,0x00,0x00, // TP6 A~D RP6 141 | 142 | 0x15,0x41,0xA8,0x32,0x30,0x0A, 143 | }; 144 | 145 | 146 | void EpdSetLutFull(const unsigned char *lut) { 147 | unsigned char count; 148 | EpdSendCommand(WRITE_LUT_REGISTER); 149 | for(count = 0; count < 70; count++) { 150 | EpdSendData(lut[count]); 151 | } 152 | /* 153 | EpdSendCommand(SET_RAM_X_ADDRESS_COUNTER); // set RAM x address count to 0; 154 | EpdSendData(0x00); 155 | EpdSendCommand(SET_RAM_Y_ADDRESS_COUNTER); // set RAM y address count to 0X127; 156 | EpdSendData(0xF9); 157 | EpdSendData(0x00); 158 | */ 159 | 160 | WaitUntilIdle(); 161 | } 162 | 163 | void EpdSetLut(const unsigned char *lut) { 164 | unsigned char count; 165 | EpdSendCommand(WRITE_LUT_REGISTER); 166 | for(count = 0; count < 70; count++) { 167 | EpdSendData(lut[count]); 168 | } 169 | 170 | EpdSendCommand(OTP_SELECTION_CONTROL_1); 171 | EpdSendData(0x00); 172 | EpdSendData(0x00); 173 | EpdSendData(0x00); 174 | EpdSendData(0x00); 175 | EpdSendData(0x40); 176 | EpdSendData(0x00); 177 | EpdSendData(0x00); 178 | 179 | EpdSendCommand(DISPLAY_UPDATE_CONTROL_2); 180 | EpdSendData(0xC0); 181 | 182 | EpdSendCommand(MASTER_ACTIVATION); 183 | 184 | WaitUntilIdle(); 185 | 186 | EpdSendCommand(BORDER_WAVEFORM_CONTROL); //BorderWavefrom 187 | EpdSendData(0x01); 188 | } 189 | 190 | 191 | void EpdSetFrameMemoryXY(const unsigned char* image_buffer, int x, int y, int image_width, int image_height) { 192 | int x_end; 193 | int y_end; 194 | 195 | if ( 196 | image_buffer == NULL || 197 | x < 0 || image_width < 0 || 198 | y < 0 || image_height < 0 199 | ) { 200 | return; 201 | } 202 | // x point must be the multiple of 8 or the last 3 bits will be ignored 203 | x &= 0xF8; 204 | image_width &= 0xF8; 205 | if (x + image_width >= epd_width) { 206 | x_end = epd_width - 1; 207 | } else { 208 | x_end = x + image_width - 1; 209 | } 210 | if (y + image_height >= epd_height) { 211 | y_end = epd_height - 1; 212 | } else { 213 | y_end = y + image_height - 1; 214 | } 215 | 216 | EpdSetMemoryArea(x, y, x_end, y_end); 217 | EpdSetMemoryPointer(x, y); 218 | 219 | 220 | EpdSendCommand(WRITE_RAM); 221 | // send the image data 222 | for (int j = 0; j < y_end - y + 1; j++) { 223 | for (int i = 0; i < (x_end - x + 1) / 8; i++) { 224 | EpdSendData(image_buffer[i + j * (image_width / 8)]); 225 | } 226 | } 227 | } 228 | 229 | void EpdSetFrameMemory(const unsigned char* image_buffer) { 230 | EpdSetMemoryArea(0, 0, epd_width - 1, epd_height - 1); 231 | EpdSetMemoryPointer(0, 0); 232 | EpdSendCommand(WRITE_RAM); 233 | /* send the image data */ 234 | for (int i = 0; i < epd_width / 8 * epd_height; i++) { 235 | EpdSendData((uint8)(image_buffer[i])); 236 | } 237 | } 238 | 239 | void EpdSetFrameMemoryBase(const unsigned char* image_buffer) { 240 | EpdSetMemoryArea(0, 0, epd_width - 1, epd_height - 1); 241 | EpdSetMemoryPointer(0, 0); 242 | EpdSendCommand(WRITE_RAM); 243 | /* send the image data */ 244 | for (int i = 0; i < epd_width / 8 * epd_height; i++) { 245 | EpdSendData((uint8)(image_buffer[i])); 246 | } 247 | EpdSendCommand(WRITE_RAM2); 248 | /* send the image data */ 249 | for (int i = 0; i < epd_width / 8 * epd_height; i++) { 250 | EpdSendData((uint8)(image_buffer[i])); 251 | } 252 | } 253 | 254 | 255 | void EpdClearFrameMemory(unsigned char color) { 256 | EpdSetMemoryArea(0, 0, epd_width - 1, epd_height - 1); 257 | EpdSetMemoryPointer(0, 0); 258 | 259 | EpdSendCommand(WRITE_RAM); 260 | // send the color data 261 | for (int i = 0; i < epd_width / 8 * epd_height; i++) { 262 | EpdSendData(color); 263 | } 264 | } 265 | 266 | void EpdClearFrameMemoryF(unsigned char color) { 267 | EpdSetMemoryArea(0, 0, epd_width - 1, epd_height - 1); 268 | EpdSetMemoryPointer(0, 0); 269 | 270 | EpdSendCommand(WRITE_RAM2); 271 | /* send the color data */ 272 | for (int i = 0; i < epd_width / 8 * epd_height; i++) { 273 | EpdSendData(0xff); 274 | } 275 | 276 | EpdSendCommand(WRITE_RAM); 277 | /* send the color data */ 278 | for (int i = 0; i < epd_width / 8 * epd_height; i++) { 279 | EpdSendData(color); 280 | } 281 | 282 | } 283 | 284 | 285 | void EpdSetMemoryArea(int x_start, int y_start, int x_end, int y_end) { 286 | EpdSendCommand(SET_RAM_X_ADDRESS_START_END_POSITION); 287 | /* x point must be the multiple of 8 or the last 3 bits will be ignored */ 288 | EpdSendData((x_start >> 3) & 0xFF); 289 | EpdSendData((x_end >> 3) & 0xFF); 290 | EpdSendCommand(SET_RAM_Y_ADDRESS_START_END_POSITION); 291 | EpdSendData(y_start & 0xFF); 292 | EpdSendData((y_start >> 8) & 0xFF); 293 | EpdSendData(y_end & 0xFF); 294 | EpdSendData((y_end >> 8) & 0xFF); 295 | } 296 | 297 | 298 | void EpdSetMemoryPointer(int x, int y) { 299 | EpdSendCommand(SET_RAM_X_ADDRESS_COUNTER); 300 | /* x point must be the multiple of 8 or the last 3 bits will be ignored */ 301 | EpdSendData((x >> 3) & 0xFF); 302 | EpdSendCommand(SET_RAM_Y_ADDRESS_COUNTER); 303 | EpdSendData(y & 0xFF); 304 | EpdSendData((y >> 8) & 0xFF); 305 | WaitUntilIdle(); 306 | } 307 | 308 | 309 | void EpdDisplayFramePartial(void) { 310 | EpdSendCommand(DISPLAY_UPDATE_CONTROL_2); 311 | EpdSendData(0x0C); 312 | //EpdSendData(0xFF); 313 | EpdSendCommand(MASTER_ACTIVATION); 314 | WaitUntilIdle(); 315 | } 316 | 317 | 318 | void EpdDisplayFrame(void) { 319 | EpdSendCommand(DISPLAY_UPDATE_CONTROL_2); 320 | EpdSendData(0xC7); 321 | //EpdSendData(0xF7); 322 | EpdSendCommand(MASTER_ACTIVATION); 323 | WaitUntilIdle(); 324 | } 325 | 326 | 327 | void EpdSleep(void) { 328 | EpdSendCommand(DEEP_SLEEP_MODE); 329 | EpdSendData(0x01); 330 | //DelayMs(100); 331 | } 332 | 333 | 334 | void EpdReset(void) { 335 | HalLcd_HW_Init(); 336 | } -------------------------------------------------------------------------------- /PROJECT SOURCE/EINK213TH_SIMPLE_TEST/Source/hal_board_cfg.h: -------------------------------------------------------------------------------- 1 | #ifndef HAL_BOARD_CFG_H 2 | #define HAL_BOARD_CFG_H 3 | 4 | 5 | /* ------------------------------------------------------------------------------------------------ 6 | * Includes 7 | * ------------------------------------------------------------------------------------------------ 8 | */ 9 | 10 | #include "hal_mcu.h" 11 | #include "hal_defs.h" 12 | #include "hal_types.h" 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | /* ------------------------------------------------------------------------------------------------ 21 | * Clock Speed 22 | * ------------------------------------------------------------------------------------------------ 23 | */ 24 | 25 | #define HAL_CPU_CLOCK_MHZ 32 26 | 27 | /* 32 kHz clock source select in CLKCONCMD */ 28 | #if !defined (OSC32K_CRYSTAL_INSTALLED) || (defined (OSC32K_CRYSTAL_INSTALLED) && (OSC32K_CRYSTAL_INSTALLED == TRUE)) 29 | #define OSC_32KHZ 0x00 /* external 32 KHz xosc */ 30 | #else 31 | #define OSC_32KHZ 0x80 /* internal 32 KHz rcosc */ 32 | #endif 33 | 34 | #define HAL_CLOCK_STABLE() 35 | 36 | /* ------------------------------------------------------------------------------------------------ 37 | * Key Release detect support 38 | * ------------------------------------------------------------------------------------------------ 39 | */ 40 | #define HAL_KEY_CODE_NOKEY 0xff 41 | 42 | 43 | /* ------------------------------------------------------------------------------------------------ 44 | * LED Configuration 45 | * ------------------------------------------------------------------------------------------------ 46 | */ 47 | 48 | // TODO: review all HAL_BOARD_CC2530EB_REV17 and replace with HAL_BOARD_CC2530RC 49 | // if applicable. 50 | #define HAL_NUM_LEDS 0 51 | 52 | #define HAL_LED_BLINK_DELAY() st( { volatile uint32 i; for (i=0; i<0x5800; i++) { }; } ) 53 | #define ACTIVE_LOW ! 54 | #define ACTIVE_HIGH !! /* double negation forces result to be '1' */ 55 | 56 | /* ------------------------------------------------------------------------------------------------ 57 | * OSAL NV implemented by internal flash pages. 58 | * ------------------------------------------------------------------------------------------------ 59 | */ 60 | 61 | // Flash is partitioned into 8 banks of 32 KB or 16 pages. 62 | #define HAL_FLASH_PAGE_PER_BANK 16 63 | // Flash is constructed of 128 pages of 2 KB. 64 | #define HAL_FLASH_PAGE_SIZE 2048 65 | #define HAL_FLASH_WORD_SIZE 4 66 | 67 | // CODE banks get mapped into the XDATA range 8000-FFFF. 68 | #define HAL_FLASH_PAGE_MAP 0x8000 69 | 70 | // The last 16 bytes of the last available page are reserved for flash lock bits. 71 | // NV page definitions must coincide with segment declaration in project *.xcl file. 72 | #if defined NON_BANKED 73 | #define HAL_FLASH_LOCK_BITS 16 74 | #define HAL_NV_PAGE_END 30 75 | #define HAL_NV_PAGE_CNT 2 76 | #else 77 | #define HAL_FLASH_LOCK_BITS 16 78 | #define HAL_NV_PAGE_END 126 79 | #define HAL_NV_PAGE_CNT 6 80 | #endif 81 | 82 | // Re-defining Z_EXTADDR_LEN here so as not to include a Z-Stack .h file. 83 | #define HAL_FLASH_IEEE_SIZE 8 84 | #define HAL_FLASH_IEEE_PAGE (HAL_NV_PAGE_END+1) 85 | #define HAL_FLASH_IEEE_OSET (HAL_FLASH_PAGE_SIZE - HAL_FLASH_LOCK_BITS - HAL_FLASH_IEEE_SIZE) 86 | #define HAL_INFOP_IEEE_OSET 0xC 87 | 88 | #define HAL_FLASH_DEV_PRIVATE_KEY_OSET 0x7D2 89 | #define HAL_FLASH_CA_PUBLIC_KEY_OSET 0x7BC 90 | #define HAL_FLASH_IMPLICIT_CERT_OSET 0x78C 91 | 92 | #define HAL_NV_PAGE_BEG (HAL_NV_PAGE_END-HAL_NV_PAGE_CNT+1) 93 | // Used by DMA macros to shift 1 to create a mask for DMA registers. 94 | #define HAL_NV_DMA_CH 0 95 | #define HAL_DMA_CH_RX 3 96 | #define HAL_DMA_CH_TX 4 97 | 98 | #define HAL_NV_DMA_GET_DESC() HAL_DMA_GET_DESC0() 99 | #define HAL_NV_DMA_SET_ADDR(a) HAL_DMA_SET_ADDR_DESC0((a)) 100 | 101 | /* ------------------------------------------------------------------------------------------------ 102 | * Serial Boot Loader: reserving the first 4 pages of flash and other memory in cc2530-sb.xcl. 103 | * ------------------------------------------------------------------------------------------------ 104 | */ 105 | 106 | #define HAL_SB_IMG_ADDR 0x2000 107 | #define HAL_SB_CRC_ADDR 0x2090 108 | // Size of internal flash less 4 pages for boot loader, 6 pages for NV, & 1 page for lock bits. 109 | #define HAL_SB_IMG_SIZE (0x40000 - 0x2000 - 0x3000 - 0x0800) 110 | 111 | /* ------------------------------------------------------------------------------------------------ 112 | * Macros 113 | * ------------------------------------------------------------------------------------------------ 114 | */ 115 | 116 | /* ----------- RF-frontend Connection Initialization ---------- */ 117 | #if defined HAL_PA_LNA || defined HAL_PA_LNA_CC2590 118 | extern void MAC_RfFrontendSetup(void); 119 | #define HAL_BOARD_RF_FRONTEND_SETUP() MAC_RfFrontendSetup() 120 | #else 121 | #define HAL_BOARD_RF_FRONTEND_SETUP() 122 | #endif 123 | 124 | 125 | /* ------------------------------------------------------------------------------------------------ 126 | * Macros 127 | * ------------------------------------------------------------------------------------------------ 128 | */ 129 | 130 | #define CLKCONCMD_VALUE (CLKCONCMD_32MHZ | OSC_32KHZ) 131 | 132 | /* ----------- Cache Prefetch control ---------- */ 133 | #define PREFETCH_ENABLE() st( FCTL = 0x08; ) 134 | #define PREFETCH_DISABLE() st( FCTL = 0x04; ) 135 | 136 | /* ----------- Board Initialization ---------- */ 137 | #define HAL_BOARD_INIT() \ 138 | { \ 139 | uint16 i; \ 140 | \ 141 | SLEEPCMD &= ~OSC_PD; /* turn on 16MHz RC and 32MHz XOSC */ \ 142 | while (!(SLEEPSTA & XOSC_STB)); /* wait for 32MHz XOSC stable */ \ 143 | asm("NOP"); /* chip bug workaround */ \ 144 | for (i=0; i<504; i++) asm("NOP"); /* Require 63us delay for all revs */ \ 145 | CLKCONCMD = CLKCONCMD_VALUE; /* Select 32MHz XOSC and the source for 32K clock */ \ 146 | while (CLKCONSTA != CLKCONCMD_VALUE); /* Wait for the change to be effective */ \ 147 | SLEEPCMD |= OSC_PD; /* turn off 16MHz RC */ \ 148 | \ 149 | /* Turn on cache prefetch mode */ \ 150 | PREFETCH_ENABLE(); \ 151 | HAL_TURN_OFF_LED1(); \ 152 | LED1_DDR |= LED1_BV; \ 153 | } 154 | 155 | /* ----------- Debounce ---------- */ 156 | #define HAL_DEBOUNCE(expr) { int i; for (i=0; i<500; i++) { if (!(expr)) i = 0; } } 157 | 158 | /* ----------- Push Buttons ---------- */ 159 | #define HAL_PUSH_BUTTON1() (0) 160 | #define HAL_PUSH_BUTTON2() (0) 161 | #define HAL_PUSH_BUTTON3() (0) 162 | #define HAL_PUSH_BUTTON4() (0) 163 | #define HAL_PUSH_BUTTON5() (0) 164 | #define HAL_PUSH_BUTTON6() (0) 165 | 166 | /* ----------- LED's ---------- */ 167 | 168 | #if defined(EINK213TH) 169 | #define LED1_BV BV(1) 170 | #define LED1_SBIT P0_1 171 | #define LED1_DDR P0DIR 172 | #define LED1_POLARITY ACTIVE_HIGH 173 | #endif 174 | /* 175 | //power pin 176 | #define LED4_BV BV(1) 177 | #define LED4_SBIT P1_2 178 | #define LED4_DDR P1DIR 179 | #define LED4_POLARITY ACTIVE_HIGH 180 | */ 181 | #define HAL_TURN_OFF_LED1() st( LED1_SBIT = LED1_POLARITY (0); ) 182 | #define HAL_TURN_OFF_LED2() asm("NOP") 183 | #define HAL_TURN_OFF_LED3() asm("NOP") 184 | #define HAL_TURN_OFF_LED4() asm("NOP") 185 | 186 | #define HAL_TURN_ON_LED1() st( LED1_SBIT = LED1_POLARITY (1); ) 187 | #define HAL_TURN_ON_LED2() asm("NOP") 188 | #define HAL_TURN_ON_LED3() asm("NOP") 189 | #define HAL_TURN_ON_LED4() asm("NOP") 190 | 191 | #define HAL_TOGGLE_LED1() st( if (LED1_SBIT) { LED1_SBIT = 0; } else { LED1_SBIT = 1;} ) 192 | #define HAL_TOGGLE_LED2() asm("NOP") 193 | #define HAL_TOGGLE_LED3() asm("NOP") 194 | #define HAL_TOGGLE_LED4() asm("NOP") 195 | 196 | #define HAL_STATE_LED1() (LED1_POLARITY (LED1_SBIT)) 197 | #define HAL_STATE_LED2() 0 198 | #define HAL_STATE_LED3() 0 199 | #define HAL_STATE_LED4() 0 200 | 201 | /* ----------- Minimum safe bus voltage ---------- */ 202 | 203 | // Vdd/3 / Internal Reference X ENOB --> (Vdd / 3) / 1.15 X 127 204 | #define VDD_2_0 74 // 2.0 V required to safely read/write internal flash. 205 | #define VDD_2_7 100 // 2.7 V required for the Numonyx device. 206 | 207 | #define VDD_MIN_RUN VDD_2_0 208 | #define VDD_MIN_NV (VDD_2_0+4) // 5% margin over minimum to survive a page erase and compaction. 209 | #define VDD_MIN_GOOD (VDD_2_0+8) // 10% margin over minimum to survive a page erase and compaction. 210 | #define VDD_MIN_XNV (VDD_2_7+5) // 5% margin over minimum to survive a page erase and compaction. 211 | 212 | /* ------------------------------------------------------------------------------------------------ 213 | * Driver Configuration 214 | * ------------------------------------------------------------------------------------------------ 215 | */ 216 | 217 | /* Set to TRUE enable H/W TIMER usage, FALSE disable it */ 218 | #ifndef HAL_TIMER 219 | #define HAL_TIMER FALSE 220 | #endif 221 | 222 | /* Set to TRUE enable ADC usage, FALSE disable it */ 223 | #ifndef HAL_ADC 224 | #define HAL_ADC TRUE 225 | #endif 226 | 227 | /* Set to TRUE enable DMA usage, FALSE disable it */ 228 | #ifndef HAL_DMA 229 | #define HAL_DMA TRUE 230 | #endif 231 | 232 | /* Set to TRUE enable Flash access, FALSE disable it */ 233 | #ifndef HAL_FLASH 234 | #define HAL_FLASH TRUE 235 | #endif 236 | 237 | /* Set to TRUE enable AES usage, FALSE disable it */ 238 | #ifndef HAL_AES 239 | #define HAL_AES TRUE 240 | #endif 241 | 242 | #ifndef HAL_AES_DMA 243 | #define HAL_AES_DMA FALSE 244 | #endif 245 | 246 | /* Set to TRUE enable LCD usage, FALSE disable it */ 247 | #ifndef HAL_LCD 248 | #define HAL_LCD FALSE 249 | #endif 250 | 251 | /* Set to TRUE enable LED usage, FALSE disable it */ 252 | #ifndef HAL_LED 253 | #define HAL_LED FALSE 254 | #endif 255 | #if (!defined BLINK_LEDS) && (HAL_LED == TRUE) 256 | #define BLINK_LEDS 257 | #endif 258 | 259 | #ifndef HAL_MOTION 260 | #define HAL_MOTION FASLE 261 | #endif 262 | 263 | /* Set to TRUE enable KEY usage, FALSE disable it */ 264 | #ifndef HAL_KEY 265 | #define HAL_KEY TRUE 266 | #endif 267 | 268 | /* Set to TRUE enable UART usage, FALSE disable it */ 269 | #ifndef HAL_UART 270 | #define HAL_UART FALSE 271 | #endif 272 | 273 | #if HAL_UART 274 | #ifndef HAL_UART_DMA 275 | #if HAL_DMA 276 | #if (defined ZAPP_P2) || (defined ZTOOL_P2) 277 | #define HAL_UART_DMA 2 278 | #else 279 | #define HAL_UART_DMA 1 280 | #endif 281 | #else 282 | #define HAL_UART_DMA 0 283 | #endif 284 | #endif 285 | 286 | #ifndef HAL_UART_ISR 287 | #if HAL_UART_DMA // Default preference for DMA over ISR. 288 | #define HAL_UART_ISR 0 289 | #elif (defined ZAPP_P2) || (defined ZTOOL_P2) 290 | #define HAL_UART_ISR 2 291 | #else 292 | #define HAL_UART_ISR 1 293 | #endif 294 | #endif 295 | 296 | #if (HAL_UART_DMA && (HAL_UART_DMA == HAL_UART_ISR)) 297 | #error HAL_UART_DMA & HAL_UART_ISR must be different. 298 | #endif 299 | 300 | // Used to set P2 priority - USART0 over USART1 if both are defined. 301 | #if ((HAL_UART_DMA == 1) || (HAL_UART_ISR == 1)) 302 | #define HAL_UART_PRIPO 0x00 303 | #else 304 | #define HAL_UART_PRIPO 0x40 305 | #endif 306 | 307 | #else 308 | #define HAL_UART_DMA 0 309 | #define HAL_UART_ISR 0 310 | #endif 311 | 312 | /* USB is not used for CC2530 configuration */ 313 | #define HAL_UART_USB 0 314 | 315 | #ifndef HAL_BUZZER 316 | #define HAL_BUZZER TRUE 317 | #endif 318 | 319 | /******************************************************************************************************* 320 | */ 321 | #endif 322 | -------------------------------------------------------------------------------- /PROJECT SOURCE/EINK213TH_SIMPLE_TEST/zstack-lib/hal_i2c.c: -------------------------------------------------------------------------------- 1 | /************************************************************************************************** 2 | Filename: hal_i2c.c 3 | Revised: $Date: 2012-09-21 06:30:38 -0700 (Fri, 21 Sep 2012) $ 4 | Revision: $Revision: 31581 $ 5 | 6 | Description: This module defines the HAL I2C API for the CC2541ST. It 7 | implements the I2C master. 8 | 9 | 10 | Copyright 2012 Texas Instruments Incorporated. All rights reserved. 11 | 12 | IMPORTANT: Your use of this Software is limited to those specific rights 13 | granted under the terms of a software license agreement between the user 14 | who downloaded the software, his/her employer (which must be your employer) 15 | and Texas Instruments Incorporated (the "License"). You may not use this 16 | Software unless you agree to abide by the terms of the License. The License 17 | limits your use, and you acknowledge, that the Software may not be modified, 18 | copied or distributed unless embedded on a Texas Instruments microcontroller 19 | or used solely and exclusively in conjunction with a Texas Instruments radio 20 | frequency transceiver, which is integrated into your product. Other than for 21 | the foregoing purpose, you may not use, reproduce, copy, prepare derivative 22 | works of, modify, distribute, perform, display or sell this Software and/or 23 | its documentation for any purpose. 24 | 25 | YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE 26 | PROVIDED �AS IS� WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, 27 | INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE, 28 | NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL 29 | TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT, 30 | NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER 31 | LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES 32 | INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE 33 | OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT 34 | OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES 35 | (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS. 36 | 37 | Should you have any questions regarding your right to use this Software, 38 | contact Texas Instruments Incorporated at www.TI.com. 39 | **************************************************************************************************/ 40 | #include "hal_i2c.h" 41 | 42 | #include "Debug.h" 43 | #include "ioCC2530.h" 44 | #include "zcomdef.h" 45 | #include "utils.h" 46 | 47 | #define STATIC static 48 | 49 | #if !defined HAL_I2C_RETRY_CNT 50 | #define HAL_I2C_RETRY_CNT 3 51 | #endif 52 | 53 | // the default cofiguration below uses P0.6 for SDA and P0.5 for SCL. 54 | // change these as needed. 55 | #ifndef OCM_CLK_PORT 56 | #define OCM_CLK_PORT 0 57 | #endif 58 | 59 | #ifndef OCM_DATA_PORT 60 | #define OCM_DATA_PORT 0 61 | #endif 62 | 63 | #ifndef OCM_CLK_PIN 64 | #define OCM_CLK_PIN 5 65 | #endif 66 | 67 | #ifndef OCM_DATA_PIN 68 | #define OCM_DATA_PIN 6 69 | #endif 70 | 71 | 72 | 73 | #define OCM_ADDRESS (0xA0) 74 | 75 | #define SMB_ACK (0) 76 | #define SMB_NAK (1) 77 | #define SEND_STOP (0) 78 | #define NOSEND_STOP (1) 79 | #define SEND_START (0) 80 | #define NOSEND_START (1) 81 | 82 | // device specific as to where the 17th address bit goes... 83 | 84 | // ************************* MACROS ************************************ 85 | #undef P 86 | 87 | 88 | 89 | // OCM port I/O defintions 90 | #define OCM_SCL BNAME(OCM_CLK_PORT, OCM_CLK_PIN) 91 | #define OCM_SDA BNAME(OCM_DATA_PORT, OCM_DATA_PIN) 92 | 93 | 94 | 95 | #define OCM_DATA_HIGH() { IO_DIR_PORT_PIN(OCM_DATA_PORT, OCM_DATA_PIN, IO_IN); } 96 | 97 | #define OCM_DATA_LOW() \ 98 | { \ 99 | IO_DIR_PORT_PIN(OCM_DATA_PORT, OCM_DATA_PIN, IO_OUT); \ 100 | OCM_SDA = 0; \ 101 | } 102 | 103 | 104 | 105 | STATIC void hali2cSend(uint8 *buffer, uint16 len, uint8 sendStart, uint8 sendStop); 106 | STATIC _Bool hali2cSendByte(uint8 dByte); 107 | STATIC void hali2cWrite(bool dBit); 108 | STATIC void hali2cClock(bool dir); 109 | STATIC void hali2cStart(void); 110 | STATIC void hali2cStop(void); 111 | STATIC void hali2cReceive(uint8 address, uint8 *buffer, uint16 len); 112 | STATIC uint8 hali2cReceiveByte(void); 113 | STATIC _Bool hali2cRead(void); 114 | STATIC void hali2cSendDeviceAddress(uint8 address); 115 | 116 | STATIC __near_func void hali2cWait(uint8); 117 | 118 | static void hali2cGroudPins(void); 119 | 120 | void hali2cGroudPins(void) { 121 | IO_DIR_PORT_PIN(OCM_DATA_PORT, OCM_DATA_PIN, IO_IN); 122 | IO_DIR_PORT_PIN(OCM_CLK_PORT, OCM_CLK_PIN, IO_IN); 123 | } 124 | 125 | STATIC uint8 s_xmemIsInit; 126 | 127 | /********************************************************************* 128 | * @fn HalI2CInit 129 | * @brief Initializes two-wire serial I/O bus 130 | * @param void 131 | * @return void 132 | */ 133 | void HalI2CInit(void) { 134 | if (!s_xmemIsInit) { 135 | s_xmemIsInit = 1; 136 | 137 | // // Set port pins as inputs 138 | // IO_DIR_PORT_PIN(OCM_CLK_PORT, OCM_CLK_PIN, IO_IN); 139 | // IO_DIR_PORT_PIN(OCM_DATA_PORT, OCM_DATA_PIN, IO_IN); 140 | // // 141 | // // Set for general I/O operation 142 | // IO_FUNC_PORT_PIN(OCM_CLK_PORT, OCM_CLK_PIN, IO_GIO); 143 | // IO_FUNC_PORT_PIN(OCM_DATA_PORT, OCM_DATA_PIN, IO_GIO); 144 | // // 145 | // // Set I/O mode for pull-up/pull-down 146 | // IO_IMODE_PORT_PIN(OCM_CLK_PORT, OCM_CLK_PIN, IO_PUD); 147 | // IO_IMODE_PORT_PIN(OCM_DATA_PORT, OCM_DATA_PIN, IO_PUD); 148 | 149 | // // Set pins to pull-up 150 | // IO_PUD_PORT(OCM_CLK_PORT, IO_PUP); 151 | // IO_PUD_PORT(OCM_DATA_PORT, IO_PUP); 152 | } 153 | } 154 | 155 | int8 HalI2CReceive(uint8 address, uint8 *buf, uint16 len) { 156 | hali2cReceive(address, buf, len); 157 | 158 | return 0; 159 | } 160 | 161 | int8 HalI2CSend(uint8 address, uint8 *buf, uint16 len) { 162 | // begin the write sequence with the address byte 163 | hali2cSendDeviceAddress(address); 164 | hali2cSend(buf, len, NOSEND_START, SEND_STOP); 165 | 166 | return 0; 167 | } 168 | /********************************************************************* 169 | * @fn hali2cSend 170 | * @brief Sends buffer contents to SM-Bus device 171 | * @param buffer - ptr to buffered data to send 172 | * @param len - number of bytes in buffer 173 | * @param sendStart - whether or not to send start condition. 174 | * @param sendStop - whether or not to send stop condition. 175 | * @return void 176 | */ 177 | STATIC void hali2cSend(uint8 *buffer, uint16 len, uint8 sendStart, uint8 sendStop) { 178 | uint16 i; 179 | uint8 retry = HAL_I2C_RETRY_CNT; 180 | 181 | if (!len) { 182 | return; 183 | } 184 | 185 | if (sendStart == SEND_START) { 186 | hali2cStart(); 187 | } 188 | 189 | for (i = 0; i < len; i++) { 190 | do { 191 | if (hali2cSendByte(buffer[i])) // takes care of ack polling 192 | { 193 | break; 194 | } 195 | } while (--retry); 196 | } 197 | 198 | if (sendStop == SEND_STOP) { 199 | hali2cStop(); 200 | } 201 | } 202 | 203 | /********************************************************************* 204 | * @fn hali2cSendByte 205 | * @brief Serialize and send one byte to SM-Bus device 206 | * @param dByte - data byte to send 207 | * @return ACK status - 0=none, 1=received 208 | */ 209 | STATIC _Bool hali2cSendByte(uint8 dByte) { 210 | uint8 i; 211 | 212 | for (i = 0; i < 8; i++) { 213 | // Send the MSB 214 | hali2cWrite(dByte & 0x80); 215 | // Next bit into MSB 216 | dByte <<= 1; 217 | } 218 | // need clock low so if the SDA transitions on the next statement the 219 | // slave doesn't stop. Also give opportunity for slave to set SDA 220 | hali2cClock(0); 221 | OCM_DATA_HIGH(); // set to input to receive ack... 222 | hali2cClock(1); 223 | hali2cWait(1); 224 | 225 | return (!OCM_SDA); // Return ACK status 226 | } 227 | 228 | /********************************************************************* 229 | * @fn hali2cWrite 230 | * @brief Send one bit to SM-Bus device 231 | * @param dBit - data bit to clock onto SM-Bus 232 | * @return void 233 | */ 234 | STATIC void hali2cWrite(bool dBit) { 235 | hali2cClock(0); 236 | hali2cWait(1); 237 | if (dBit) { 238 | OCM_DATA_HIGH(); 239 | } else { 240 | OCM_DATA_LOW(); 241 | } 242 | 243 | hali2cClock(1); 244 | hali2cWait(1); 245 | } 246 | 247 | /********************************************************************* 248 | * @fn hali2cClock 249 | * @brief Clocks the SM-Bus. If a negative edge is going out, the 250 | * I/O pin is set as an output and driven low. If a positive 251 | * edge is going out, the pin is set as an input and the pin 252 | * pull-up drives the line high. This way, the slave device 253 | * can hold the node low if longer setup time is desired. 254 | * @param dir - clock line direction 255 | * @return void 256 | */ 257 | STATIC void hali2cClock(bool dir) { 258 | uint8 maxWait = 10; 259 | if (dir) { 260 | IO_DIR_PORT_PIN(OCM_CLK_PORT, OCM_CLK_PIN, IO_IN); 261 | /* Wait until clock is high */ 262 | while (!OCM_SCL && maxWait) { 263 | hali2cWait(1); 264 | maxWait -= 1; 265 | } 266 | 267 | } else { 268 | IO_DIR_PORT_PIN(OCM_CLK_PORT, OCM_CLK_PIN, IO_OUT); 269 | OCM_SCL = 0; 270 | } 271 | hali2cWait(1); 272 | } 273 | 274 | /********************************************************************* 275 | * @fn hali2cStart 276 | * @brief Initiates SM-Bus communication. Makes sure that both the 277 | * clock and data lines of the SM-Bus are high. Then the data 278 | * line is set high and clock line is set low to start I/O. 279 | * @param void 280 | * @return void 281 | */ 282 | STATIC void hali2cStart(void) { 283 | uint8 retry = HAL_I2C_RETRY_CNT; 284 | 285 | // set SCL to input but with pull-up. if slave is pulling down it will stay down. 286 | hali2cClock(1); 287 | 288 | do { 289 | // wait for slave to release clock line... 290 | if (OCM_SCL) // wait until the line is high... 291 | { 292 | break; 293 | } 294 | hali2cWait(1); 295 | } while (--retry); 296 | 297 | // SCL low to set SDA high so the transition will be correct. 298 | hali2cClock(0); 299 | OCM_DATA_HIGH(); // SDA high 300 | hali2cClock(1); // set up for transition 301 | hali2cWait(1); 302 | OCM_DATA_LOW(); // start 303 | 304 | hali2cWait(1); 305 | hali2cClock(0); 306 | } 307 | 308 | /********************************************************************* 309 | * @fn hali2cStop 310 | * @brief Terminates SM-Bus communication. Waits unitl the data line 311 | * is low and the clock line is high. Then sets the data line 312 | * high, keeping the clock line high to stop I/O. 313 | * @param void 314 | * @return void 315 | */ 316 | STATIC void hali2cStop(void) { 317 | // Wait for clock high and data low 318 | hali2cClock(0); 319 | OCM_DATA_LOW(); // force low with SCL low 320 | hali2cWait(1); 321 | 322 | hali2cClock(1); 323 | OCM_DATA_HIGH(); // stop condition 324 | hali2cWait(1); 325 | 326 | hali2cGroudPins(); 327 | } 328 | 329 | /********************************************************************* 330 | * @fn hali2cWait 331 | * @brief Wastes a an amount of time. 332 | * @param count: down count in busy-wait 333 | * @return void 334 | */ 335 | STATIC __near_func void hali2cWait(uint8 count) { 336 | while (count--) { 337 | asm("NOP"); 338 | } 339 | } 340 | 341 | /********************************************************************* 342 | * @fn hali2cReceiveByte 343 | * @brief Read the 8 data bits. 344 | * @param void 345 | * @return character read 346 | */ 347 | STATIC uint8 hali2cReceiveByte() { 348 | int8 i, rval = 0; 349 | 350 | for (i = 7; i >= 0; --i) { 351 | if (hali2cRead()) { 352 | rval |= 1 << i; 353 | } 354 | } 355 | 356 | return rval; 357 | } 358 | /************************************************************************************************** 359 | **************************************************************************************************/ 360 | /********************************************************************* 361 | * @fn hali2cReceive 362 | * @brief reads data into a buffer 363 | * @param address: linear address on part from which to read 364 | * @param buffer: target array for read characters 365 | * @param len: max number of characters to read 366 | * @return void 367 | */ 368 | STATIC void hali2cReceive(uint8 address, uint8 *buffer, uint16 len) { 369 | // uint8 ch; 370 | uint16 i; 371 | 372 | if (!len) { 373 | return; 374 | } 375 | 376 | hali2cSendDeviceAddress(address); 377 | 378 | // ch = OCM_ADDRESS_BYTE(0, OCM_READ); 379 | // hali2cSend(&ch, 1, SEND_START, NOSEND_STOP); 380 | 381 | for (i = 0; i < len - 1; i++) { 382 | // SCL may be high. set SCL low. If SDA goes high when input 383 | // mode is set the slave won't see a STOP 384 | hali2cClock(0); 385 | OCM_DATA_HIGH(); 386 | 387 | buffer[i] = hali2cReceiveByte(); 388 | hali2cWrite(SMB_ACK); // write leaves SCL high 389 | } 390 | 391 | // condition SDA one more time... 392 | hali2cClock(0); 393 | OCM_DATA_HIGH(); 394 | buffer[i] = hali2cReceiveByte(); 395 | hali2cWrite(SMB_NAK); 396 | 397 | hali2cStop(); 398 | } 399 | 400 | /********************************************************************* 401 | * @fn hali2cRead 402 | * @brief Toggle the clock line to let the slave set the data line. 403 | * Then read the data line. 404 | * @param void 405 | * @return TRUE if bit read is 1 else FALSE 406 | */ 407 | STATIC _Bool hali2cRead(void) { 408 | // SCL low to let slave set SDA. SCL high for SDA 409 | // valid and then get bit 410 | hali2cClock(0); 411 | hali2cWait(1); 412 | hali2cClock(1); 413 | hali2cWait(1); 414 | 415 | return OCM_SDA; 416 | } 417 | 418 | /********************************************************************* 419 | * @fn hali2cSendDeviceAddress 420 | * @brief Send onlythe device address. Do ack polling 421 | * 422 | * @param void 423 | * @return none 424 | */ 425 | STATIC void hali2cSendDeviceAddress(uint8 address) { 426 | uint8 retry = HAL_I2C_RETRY_CNT; 427 | 428 | do { 429 | hali2cStart(); 430 | if (hali2cSendByte(address)) // do ack polling... 431 | { 432 | break; 433 | } 434 | } while (--retry); 435 | } 436 | 437 | // http://e2e.ti.com/support/wireless-connectivity/zigbee-and-thread/f/158/t/140917 438 | /********************************************************************* 439 | * @fn I2C_ReadMultByte 440 | * @brief reads data into a buffer 441 | * @param address: linear address on part from which to read 442 | * @param reg: internal register address on part read from 443 | * @param buffer: target array for read characters 444 | * @param len: max number of bytes to read 445 | */ 446 | int8 I2C_ReadMultByte(uint8 address, uint8 reg, uint8 *buffer, uint16 len) { 447 | uint16 i = 0; 448 | uint8 _address = 0; 449 | 450 | if (!len) { 451 | return I2C_ERROR; 452 | } 453 | 454 | /* Send START condition */ 455 | hali2cStart(); 456 | 457 | /* Set direction of transmission */ 458 | // Reset the address bit0 for write 459 | // _address &= OCM_WRITE; 460 | 461 | _address = ((address << 1) | OCM_WRITE); 462 | 463 | /* Send Address and get acknowledgement from slave*/ 464 | if (!hali2cSendByte(_address)) { 465 | return I2C_ERROR; 466 | } 467 | 468 | /* Send internal register to read from to */ 469 | if (!hali2cSendByte(reg)) 470 | return I2C_ERROR; 471 | 472 | /* Send RESTART condition */ 473 | hali2cStart(); 474 | 475 | /* Set direction of transmission */ 476 | // Reset the address bit0 for read 477 | // _address |= OCM_READ; 478 | _address = ((address << 1) | OCM_READ); 479 | /* Send Address and get acknowledgement from slave*/ 480 | if (!hali2cSendByte(_address)) 481 | return I2C_ERROR; 482 | 483 | while (len) { 484 | // SCL may be high. set SCL low. If SDA goes high when input 485 | // mode is set the slave won't see a STOP 486 | hali2cClock(0); 487 | OCM_DATA_HIGH(); 488 | buffer[i] = hali2cReceiveByte(); 489 | // Acknowledgement if not sending last byte 490 | if (len > 1) { 491 | hali2cWrite(SMB_ACK); // write leaves SCL high 492 | } 493 | 494 | // increment buffer register 495 | i++; 496 | // Decrement the read bytes counter 497 | len--; 498 | } 499 | 500 | // condition SDA one more time... 501 | hali2cClock(0); 502 | OCM_DATA_HIGH(); 503 | hali2cWrite(SMB_NAK); 504 | 505 | hali2cStop(); 506 | 507 | // condition SDA one more time... 508 | // hali2cClock(0); 509 | // OCM_DATA_HIGH(); 510 | // buffer[i] = hali2cReceiveByte(); 511 | // hali2cWrite(SMB_NAK); 512 | 513 | // hali2cStop(); 514 | return I2C_SUCCESS; 515 | } 516 | 517 | /********************************************************************* 518 | * @fn I2C_WriteMultByte 519 | * @brief reads data into a buffer 520 | * @param address: linear address on part from which to read 521 | * @param reg: internal register address on part read from 522 | * @param buffer: target array for read characters 523 | * @param len: max number of bytes to read 524 | */ 525 | int8 I2C_WriteMultByte(uint8 address, uint8 reg, uint8 *buffer, uint16 len) { 526 | uint16 i = 0; 527 | uint8 _address = 0; 528 | 529 | if (!len) { 530 | return I2C_ERROR; 531 | } 532 | 533 | /* Send START condition */ 534 | hali2cStart(); 535 | // return I2C_ERROR; 536 | 537 | /* Set direction of transmission */ 538 | // Reset the address bit0 for write 539 | // _address &= OCM_WRITE; 540 | _address = ((address << 1) | OCM_WRITE); 541 | 542 | /* Send Address and get acknowledgement from slave*/ 543 | if (!hali2cSendByte(_address)) 544 | return I2C_ERROR; 545 | 546 | /* Send internal register to read from to */ 547 | if (!hali2cSendByte(reg)) 548 | return I2C_ERROR; 549 | 550 | /* Write data into register */ 551 | // read bytes of data into buffer 552 | while (len) { 553 | // SCL may be high. set SCL low. If SDA goes high when input 554 | // mode is set the slave won't see a STOP 555 | hali2cClock(0); 556 | OCM_DATA_HIGH(); 557 | 558 | /* Send Address and get acknowledgement from slave*/ 559 | if (!hali2cSendByte(buffer[i])) 560 | return I2C_ERROR; 561 | 562 | // increment buffer register 563 | i++; 564 | // Decrement the read bytes counter 565 | len--; 566 | } 567 | 568 | hali2cStop(); 569 | return I2C_SUCCESS; 570 | } -------------------------------------------------------------------------------- /PROJECT SOURCE/EINK213TH_SIMPLE_TEST/Source/zcl_app.c: -------------------------------------------------------------------------------- 1 | 2 | #include "AF.h" 3 | #include "OSAL.h" 4 | #include "OSAL_Clock.h" 5 | #include "OSAL_PwrMgr.h" 6 | #include "ZComDef.h" 7 | #include "ZDApp.h" 8 | #include "ZDNwkMgr.h" 9 | #include "ZDObject.h" 10 | #include "math.h" 11 | 12 | #include "nwk_util.h" 13 | #include "zcl.h" 14 | #include "zcl_app.h" 15 | #include "zcl_diagnostic.h" 16 | #include "zcl_general.h" 17 | #include "zcl_ms.h" 18 | 19 | #include "bdb.h" 20 | #include "bdb_interface.h" 21 | #include "gp_interface.h" 22 | 23 | #include "Debug.h" 24 | 25 | #include "OnBoard.h" 26 | 27 | #include 28 | #include 29 | 30 | /* HAL */ 31 | #include "inttempsens.h" 32 | #include "hal_adc.h" 33 | #include "hal_drivers.h" 34 | #include "hal_key.h" 35 | #include "hal_led.h" 36 | 37 | #include "spi.h" 38 | #include "hal_i2c.h" 39 | 40 | #include "battery.h" 41 | #include "commissioning.h" 42 | #include "factory_reset.h" 43 | #include "utils.h" 44 | #include "version.h" 45 | 46 | #include "ssd1675.h" 47 | #include "SHTC3.h" 48 | 49 | 50 | #include "imagedata.h" 51 | #include "epdpaint.h" 52 | 53 | /********************************************************************* 54 | * MACROS 55 | */ 56 | #define HAL_KEY_CODE_RELEASE_KEY HAL_KEY_CODE_NOKEY 57 | 58 | #define myround(x) ((int)((x)+0.5)) 59 | 60 | /********************************************************************* 61 | * GLOBAL VARIABLES 62 | */ 63 | 64 | extern bool requestNewTrustCenterLinkKey; 65 | byte zclApp_TaskID; 66 | 67 | /********************************************************************* 68 | * LOCAL VARIABLES 69 | */ 70 | bool firstLoop = true; 71 | bool changeData = false; 72 | static uint8 currentSensorsReadingPhase = 0; 73 | int16 colorPrint = 0x00; // 00 - black on white, ff - white on black, ..still not implemented, no time :( 74 | int16 temp_old = 0; 75 | int16 tempTr = 33; 76 | uint16 hum_old = 0; 77 | uint16 humTr = 250; 78 | int16 startWork = 0; 79 | int16 sendBattCount = 0; 80 | bool pushBut = false; 81 | bool epdStart = false; 82 | bool first = false; 83 | bool upTemp; 84 | bool downTemp; 85 | uint16 last_tmp = 0;; 86 | bool upHum; 87 | bool downHum; 88 | uint16 last_hum = 0;; 89 | uint8 loopCount = 5; 90 | 91 | uint16 einkTemp; 92 | uint16 einkHum; 93 | 94 | void graphData(uint16 temp, uint16 hum); 95 | void epdGraphData(void); 96 | int massDataT[24] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; 97 | uint16 numDataT; 98 | int numMassDataT = 0; 99 | uint32 amountDataT; 100 | 101 | int massDataH[24] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; 102 | uint16 numDataH; 103 | int numMassDataH = 0; 104 | uint32 amountDataH; 105 | 106 | /********************************************************************* 107 | * LOCAL FUNCTIONS 108 | */ 109 | static void zclApp_HandleKeys(byte shift, byte keys); 110 | static void zclApp_Report(void); 111 | 112 | static void zclApp_ReadSensors(void); 113 | static void zclApp_TempHumiSens(void); 114 | 115 | void conveyor(int *Arr, int n, int l); 116 | 117 | void EpdRefresh(void); 118 | void EpdStart(void); 119 | void epdTemperatureData(uint16 tt); 120 | void epdHumidityData(uint16 hh); 121 | void epdBatteryData(uint8 b); 122 | void epdZigbeeStatusData(void); 123 | 124 | 125 | /********************************************************************* 126 | * ZCL General Profile Callback table 127 | */ 128 | static zclGeneral_AppCallbacks_t zclApp_CmdCallbacks = { 129 | NULL, // Basic Cluster Reset command 130 | NULL, // Identify Trigger Effect command 131 | NULL, // On/Off cluster commands 132 | NULL, // On/Off cluster enhanced command Off with Effect 133 | NULL, // On/Off cluster enhanced command On with Recall Global Scene 134 | NULL, // On/Off cluster enhanced command On with Timed Off 135 | NULL, // RSSI Location command 136 | NULL // RSSI Location Response command 137 | }; 138 | 139 | void zclApp_Init(byte task_id) { 140 | HalI2CInit(); 141 | SPIInit(); 142 | 143 | EpdStart(); 144 | 145 | // this is important to allow connects throught routers 146 | // to make this work, coordinator should be compiled with this flag #define TP2_LEGACY_ZC 147 | requestNewTrustCenterLinkKey = FALSE; 148 | 149 | zclApp_TaskID = task_id; 150 | 151 | zclGeneral_RegisterCmdCallbacks(1, &zclApp_CmdCallbacks); 152 | zcl_registerAttrList(zclApp_FirstEP.EndPoint, zclApp_AttrsFirstEPCount, zclApp_AttrsFirstEP); 153 | bdb_RegisterSimpleDescriptor(&zclApp_FirstEP); 154 | 155 | zcl_registerForMsg(zclApp_TaskID); 156 | 157 | // Register for all key events - This app will handle all key events 158 | RegisterForKeys(zclApp_TaskID); 159 | 160 | LREP("Started build %s \r\n", zclApp_DateCodeNT); 161 | HalLedSet(HAL_LED_1, HAL_LED_MODE_BLINK); 162 | zclApp_ReadSensors(); 163 | 164 | osal_start_reload_timer(zclApp_TaskID, APP_REPORT_EVT, APP_REPORT_DELAY); 165 | } 166 | 167 | uint16 zclApp_event_loop(uint8 task_id, uint16 events) { 168 | afIncomingMSGPacket_t *MSGpkt; 169 | 170 | (void)task_id; // Intentionally unreferenced parameter 171 | if (events & SYS_EVENT_MSG) { 172 | while ((MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive(zclApp_TaskID))) { 173 | switch (MSGpkt->hdr.event) { 174 | case KEY_CHANGE: 175 | zclApp_HandleKeys(((keyChange_t *)MSGpkt)->state, ((keyChange_t *)MSGpkt)->keys); 176 | break; 177 | case ZCL_INCOMING_MSG: 178 | if (((zclIncomingMsg_t *)MSGpkt)->attrCmd) { 179 | osal_mem_free(((zclIncomingMsg_t *)MSGpkt)->attrCmd); 180 | } 181 | break; 182 | 183 | default: 184 | break; 185 | } 186 | // Release the memory 187 | osal_msg_deallocate((uint8 *)MSGpkt); 188 | } 189 | // return unprocessed events 190 | return (events ^ SYS_EVENT_MSG); 191 | } 192 | 193 | if (events & APP_REPORT_EVT) { 194 | LREPMaster("APP_REPORT_EVT\r\n"); 195 | zclApp_Report(); 196 | return (events ^ APP_REPORT_EVT); 197 | } 198 | 199 | if (events & APP_READ_SENSORS_EVT) { 200 | LREPMaster("APP_READ_SENSORS_EVT\r\n"); 201 | zclApp_ReadSensors(); 202 | if(epdStart){ 203 | EpdRefresh(); 204 | epdStart = false; 205 | } 206 | return (events ^ APP_READ_SENSORS_EVT); 207 | } 208 | 209 | // Discard unknown events 210 | return 0; 211 | } 212 | 213 | static void zclApp_HandleKeys(byte portAndAction, byte keyCode) { 214 | LREP("zclApp_HandleKeys portAndAction=0x%X keyCode=0x%X\r\n", portAndAction, keyCode); 215 | 216 | zclFactoryResetter_HandleKeys(portAndAction, keyCode); 217 | zclCommissioning_HandleKeys(portAndAction, keyCode); 218 | 219 | if (portAndAction & HAL_KEY_RELEASE) { 220 | LREPMaster("Key press\r\n"); 221 | 222 | if (bdbAttributes.bdbNodeIsOnANetwork){ 223 | osal_start_timerEx(zclApp_TaskID, APP_READ_SENSORS_EVT, 100); 224 | }else{ 225 | osal_start_timerEx(zclApp_TaskID, APP_READ_SENSORS_EVT, 1000); 226 | } 227 | pushBut = true; 228 | } 229 | 230 | } 231 | 232 | 233 | static void zclApp_ReadSensors(void) { 234 | LREP("currentSensorsReadingPhase %d\r\n", currentSensorsReadingPhase); 235 | /** 236 | * FYI: split reading sensors into phases, so single call wouldn't block processor 237 | * for extensive ammount of time 238 | * */ 239 | switch (currentSensorsReadingPhase++) { 240 | case 0: 241 | if(startWork <= 2){ 242 | startWork++; 243 | zclBattery_Report(); 244 | pushBut = true; 245 | } 246 | 247 | if(startWork == 3){ 248 | sendBattCount++; 249 | if(sendBattCount == 720){ 250 | zclBattery_Report(); 251 | sendBattCount = 0; 252 | pushBut = true; 253 | }else{ 254 | if(pushBut){ 255 | zclBattery_Report(); 256 | sendBattCount = 0; 257 | } 258 | } 259 | } 260 | break; 261 | 262 | case 1: 263 | zclApp_TempHumiSens(); 264 | if(pushBut == true){ 265 | pushBut = false; 266 | } 267 | break; 268 | 269 | default: 270 | currentSensorsReadingPhase = 0; 271 | epdStart = true; 272 | break; 273 | } 274 | if (currentSensorsReadingPhase != 0) { 275 | osal_start_timerEx(zclApp_TaskID, APP_READ_SENSORS_EVT, 20); 276 | } 277 | } 278 | 279 | 280 | 281 | 282 | 283 | static void zclApp_TempHumiSens(void) { 284 | 285 | float t = 0.0; 286 | float h = 0.0; 287 | 288 | wakeup_sensor(); 289 | getTempHumi(&t, &h); 290 | sleep_sensor(); 291 | zclApp_Temperature_Sensor_MeasuredValue = (int16)(t*100); 292 | zclApp_HumiditySensor_MeasuredValue = (uint16)(h*100); 293 | 294 | einkTemp = myround(t*10.0); 295 | einkHum = (uint16)myround(h); 296 | 297 | graphData(einkTemp, einkHum); 298 | 299 | if(!pushBut){ 300 | if (abs(zclApp_Temperature_Sensor_MeasuredValue - temp_old) >= tempTr) { 301 | temp_old = zclApp_Temperature_Sensor_MeasuredValue; 302 | 303 | LREP("ReadIntTempSens t=%d\r\n", zclApp_Temperature_Sensor_MeasuredValue); 304 | bdb_RepChangedAttrValue(zclApp_FirstEP.EndPoint, TEMP, ATTRID_MS_TEMPERATURE_MEASURED_VALUE); 305 | changeData = true; 306 | } 307 | if (abs(zclApp_HumiditySensor_MeasuredValue - hum_old) >= humTr) { 308 | hum_old = zclApp_HumiditySensor_MeasuredValue; 309 | 310 | LREP("ReadIntTempSens t=%d\r\n", zclApp_HumiditySensor_MeasuredValue); 311 | bdb_RepChangedAttrValue(zclApp_FirstEP.EndPoint, HUMIDITY, ATTRID_MS_RELATIVE_HUMIDITY_MEASURED_VALUE); 312 | changeData = true; 313 | } 314 | }else{ 315 | temp_old = zclApp_Temperature_Sensor_MeasuredValue; 316 | 317 | LREP("ReadIntTempSens t=%d\r\n", zclApp_Temperature_Sensor_MeasuredValue); 318 | bdb_RepChangedAttrValue(zclApp_FirstEP.EndPoint, TEMP, ATTRID_MS_TEMPERATURE_MEASURED_VALUE); 319 | 320 | hum_old = zclApp_HumiditySensor_MeasuredValue; 321 | 322 | LREP("ReadIntTempSens t=%d\r\n", zclApp_HumiditySensor_MeasuredValue); 323 | bdb_RepChangedAttrValue(zclApp_FirstEP.EndPoint, HUMIDITY, ATTRID_MS_RELATIVE_HUMIDITY_MEASURED_VALUE); 324 | changeData = true; 325 | } 326 | } 327 | 328 | 329 | 330 | static void zclApp_Report(void) { osal_start_timerEx(zclApp_TaskID, APP_READ_SENSORS_EVT, 20); } 331 | 332 | 333 | /**************************************************************************** 334 | // E-ink display 335 | ****************************************************************************/ 336 | void EpdStart(void) 337 | { 338 | EpdInitFull(); 339 | EpdSetFrameMemory(LOGO); 340 | EpdDisplayFrame(); 341 | user_delay_ms(1000); 342 | EpdSetFrameMemory(ESPEC); 343 | EpdDisplayFrame(); 344 | user_delay_ms(3000); 345 | first = true; 346 | EpdSleep(); 347 | } 348 | 349 | void EpdRefresh(void) 350 | { 351 | unsigned char image[672]; 352 | PaintPaint(image, 0, 0); 353 | 354 | EpdReset(); 355 | if(first == true){ 356 | EpdClearFrameMemoryF(0xFF); 357 | EpdDisplayFrame(); 358 | first=false; 359 | } 360 | if(changeData == true){ 361 | if(firstLoop == true){ 362 | firstLoop = false; 363 | } 364 | 365 | EpdInitPartial(); 366 | 367 | loopCount--; 368 | if(loopCount == 0){ 369 | EpdInitFull(); 370 | EpdClearFrameMemoryF(0xFF); 371 | EpdDisplayFrame(); 372 | EpdInitPartial(); 373 | loopCount = 5; 374 | }else{ 375 | EpdInitPartial(); 376 | } 377 | 378 | epdTemperatureData(einkTemp); 379 | epdHumidityData(einkHum); 380 | epdZigbeeStatusData(); 381 | epdBatteryData(zclBattery_PercentageRemainig/2); 382 | epdGraphData(); 383 | 384 | if(loopCount == 0){ 385 | loopCount = 5; 386 | }else{ 387 | } 388 | 389 | EpdDisplayFramePartial(); 390 | 391 | EpdSleep(); 392 | 393 | changeData = false; 394 | } 395 | } 396 | 397 | 398 | void epdTemperatureData(uint16 tt) { 399 | 400 | byte one_t = tt / 100; 401 | byte two_t = tt % 100 / 10; 402 | byte three_t = tt % 10; 403 | 404 | PaintSetWidth(72); 405 | PaintSetHeight(44); 406 | PaintSetRotate(ROTATE_180); 407 | 408 | if (one_t == 0) { 409 | PaintClear(UNCOLORED); 410 | switch (two_t) { 411 | case 0: 412 | PaintDrawImage(ZERO, 4, 0, 67, 44, COLORED); 413 | break; 414 | case 1: 415 | PaintDrawImage(ONE, 4, 0, 67, 44, COLORED); 416 | break; 417 | case 2: 418 | PaintDrawImage(TWO, 4, 0, 67, 44, COLORED); 419 | break; 420 | case 3: 421 | PaintDrawImage(THREE, 4, 0, 67, 44, COLORED); 422 | break; 423 | case 4: 424 | PaintDrawImage(FOUR, 4, 0, 67, 44, COLORED); 425 | break; 426 | case 5: 427 | PaintDrawImage(FIVE, 4, 0, 67, 44, COLORED); 428 | break; 429 | case 6: 430 | PaintDrawImage(SIX, 4, 0, 67, 44, COLORED); 431 | break; 432 | case 7: 433 | PaintDrawImage(SEVEN, 4, 0, 67, 44, COLORED); 434 | break; 435 | case 8: 436 | PaintDrawImage(EIGHT, 4, 0, 67, 44, COLORED); 437 | break; 438 | case 9: 439 | PaintDrawImage(NINE, 4, 0, 67, 44, COLORED); 440 | break; 441 | } 442 | EpdSetFrameMemoryXY(PaintGetImage(), 24, 39, PaintGetWidth(), PaintGetHeight()); 443 | 444 | } else { 445 | PaintClear(UNCOLORED); 446 | switch (one_t) { 447 | case 0: 448 | PaintDrawImage(ZERO, 4, 0, 67, 44, COLORED); 449 | break; 450 | case 1: 451 | PaintDrawImage(ONE, 4, 0, 67, 44, COLORED); 452 | break; 453 | case 2: 454 | PaintDrawImage(TWO, 4, 0, 67, 44, COLORED); 455 | break; 456 | case 3: 457 | PaintDrawImage(THREE, 4, 0, 67, 44, COLORED); 458 | break; 459 | case 4: 460 | PaintDrawImage(FOUR, 4, 0, 67, 44, COLORED); 461 | break; 462 | case 5: 463 | PaintDrawImage(FIVE, 4, 0, 67, 44, COLORED); 464 | break; 465 | case 6: 466 | PaintDrawImage(SIX, 4, 0, 67, 44, COLORED); 467 | break; 468 | case 7: 469 | PaintDrawImage(SEVEN, 4, 0, 67, 44, COLORED); 470 | break; 471 | case 8: 472 | PaintDrawImage(EIGHT, 4, 0, 67, 44, COLORED); 473 | break; 474 | case 9: 475 | PaintDrawImage(NINE, 4, 0, 67, 44, COLORED); 476 | break; 477 | } 478 | EpdSetFrameMemoryXY(PaintGetImage(), 24, 10, PaintGetWidth(), PaintGetHeight()); 479 | 480 | PaintClear(UNCOLORED); 481 | switch (two_t) { 482 | case 0: 483 | PaintDrawImage(ZERO, 4, 0, 67, 44, COLORED); 484 | break; 485 | case 1: 486 | PaintDrawImage(ONE, 4, 0, 67, 44, COLORED); 487 | break; 488 | case 2: 489 | PaintDrawImage(TWO, 4, 0, 67, 44, COLORED); 490 | break; 491 | case 3: 492 | PaintDrawImage(THREE, 4, 0, 67, 44, COLORED); 493 | break; 494 | case 4: 495 | PaintDrawImage(FOUR, 4, 0, 67, 44, COLORED); 496 | break; 497 | case 5: 498 | PaintDrawImage(FIVE, 4, 0, 67, 44, COLORED); 499 | break; 500 | case 6: 501 | PaintDrawImage(SIX, 4, 0, 67, 44, COLORED); 502 | break; 503 | case 7: 504 | PaintDrawImage(SEVEN, 4, 0, 67, 44, COLORED); 505 | break; 506 | case 8: 507 | PaintDrawImage(EIGHT, 4, 0, 67, 44, COLORED); 508 | break; 509 | case 9: 510 | PaintDrawImage(NINE, 4, 0, 67, 44, COLORED); 511 | break; 512 | } 513 | EpdSetFrameMemoryXY(PaintGetImage(), 24, 54, PaintGetWidth(), PaintGetHeight()); 514 | 515 | PaintSetWidth(72); 516 | PaintSetHeight(7); 517 | PaintSetRotate(ROTATE_180); 518 | PaintClear(UNCOLORED); 519 | 520 | PaintDrawImage(POINT, 42, 0, 29, 7, COLORED); 521 | EpdSetFrameMemoryXY(PaintGetImage(), 24, 96, PaintGetWidth(), PaintGetHeight()); 522 | 523 | 524 | PaintSetWidth(72); 525 | PaintSetHeight(19); 526 | PaintSetRotate(ROTATE_180); 527 | PaintClear(UNCOLORED); 528 | switch (three_t) { 529 | case 0: 530 | PaintDrawImage(ZERO_S, 42, 0, 29, 19, COLORED); 531 | break; 532 | case 1: 533 | PaintDrawImage(ONE_S, 42, 0, 29, 19, COLORED); 534 | break; 535 | case 2: 536 | PaintDrawImage(TWO_S, 42, 0, 29, 19, COLORED); 537 | break; 538 | case 3: 539 | PaintDrawImage(THREE_S, 42, 0, 29, 19, COLORED); 540 | break; 541 | case 4: 542 | PaintDrawImage(FOUR_S, 42, 0, 29, 19, COLORED); 543 | break; 544 | case 5: 545 | PaintDrawImage(FIVE_S, 42, 0, 29, 19, COLORED); 546 | break; 547 | case 6: 548 | PaintDrawImage(SIX_S, 42, 0, 29, 19, COLORED); 549 | break; 550 | case 7: 551 | PaintDrawImage(SEVEN_S, 42, 0, 29, 19, COLORED); 552 | break; 553 | case 8: 554 | PaintDrawImage(EIGHT_S, 42, 0, 29, 19, COLORED); 555 | break; 556 | case 9: 557 | PaintDrawImage(NINE_S, 42, 0, 29, 19, COLORED); 558 | break; 559 | } 560 | EpdSetFrameMemoryXY(PaintGetImage(), 24, 105, PaintGetWidth(), PaintGetHeight()); 561 | 562 | PaintSetWidth(24); 563 | PaintSetHeight(22); 564 | PaintSetRotate(ROTATE_180); 565 | PaintClear(UNCOLORED); 566 | PaintDrawImage(CELSIUS, 4, 0, 16, 22, COLORED); 567 | EpdSetFrameMemoryXY(PaintGetImage(), 72, 98, PaintGetWidth(), PaintGetHeight()); 568 | 569 | 570 | PaintSetWidth(24); 571 | PaintSetHeight(12); 572 | PaintSetRotate(ROTATE_180); 573 | PaintClear(UNCOLORED); 574 | 575 | if (last_tmp != 0) { 576 | if (tt > last_tmp) { 577 | PaintDrawImage(IMAGE_UP, 5, 0, 14, 12, COLORED); 578 | upTemp = true; 579 | downTemp = false; 580 | } else if (tt < last_tmp) { 581 | PaintDrawImage(IMAGE_DOWN, 5, 0, 14, 12, COLORED); 582 | upTemp = false; 583 | downTemp = true; 584 | } else { 585 | if (upTemp == true) { 586 | PaintDrawImage(IMAGE_UP, 5, 0, 14, 12, COLORED); 587 | } 588 | if (downTemp == true) { 589 | PaintDrawImage(IMAGE_DOWN, 5, 0, 14, 12, COLORED); 590 | } 591 | } 592 | } 593 | last_tmp = tt; 594 | EpdSetFrameMemoryXY(PaintGetImage(), 100, 75, PaintGetWidth(), PaintGetHeight()); 595 | 596 | PaintSetWidth(24); 597 | PaintSetHeight(15); 598 | PaintSetRotate(ROTATE_180); 599 | PaintClear(UNCOLORED); 600 | PaintDrawImage(I1, 3, 0, 17, 15, COLORED); 601 | EpdSetFrameMemoryXY(PaintGetImage(), 96, 56, PaintGetWidth(), PaintGetHeight()); 602 | } 603 | } 604 | 605 | 606 | 607 | void epdHumidityData(uint16 hh) { 608 | 609 | byte one_h = hh / 10; 610 | byte two_h = hh % 10; 611 | 612 | 613 | PaintSetWidth(72); 614 | PaintSetHeight(44); 615 | PaintSetRotate(ROTATE_180); 616 | 617 | if (one_h == 0) { 618 | PaintClear(UNCOLORED); 619 | switch (two_h) { 620 | case 0: 621 | PaintDrawImage(ZERO, 4, 0, 67, 44, COLORED); 622 | break; 623 | case 1: 624 | PaintDrawImage(ONE, 4, 0, 67, 44, COLORED); 625 | break; 626 | case 2: 627 | PaintDrawImage(TWO, 4, 0, 67, 44, COLORED); 628 | break; 629 | case 3: 630 | PaintDrawImage(THREE, 4, 0, 67, 44, COLORED); 631 | break; 632 | case 4: 633 | PaintDrawImage(FOUR, 4, 0, 67, 44, COLORED); 634 | break; 635 | case 5: 636 | PaintDrawImage(FIVE, 4, 0, 67, 44, COLORED); 637 | break; 638 | case 6: 639 | PaintDrawImage(SIX, 4, 0, 67, 44, COLORED); 640 | break; 641 | case 7: 642 | PaintDrawImage(SEVEN, 4, 0, 67, 44, COLORED); 643 | break; 644 | case 8: 645 | PaintDrawImage(EIGHT, 4, 0, 67, 44, COLORED); 646 | break; 647 | case 9: 648 | PaintDrawImage(NINE, 4, 0, 67, 44, COLORED); 649 | break; 650 | } 651 | EpdSetFrameMemoryXY(PaintGetImage(), 22, 36, PaintGetWidth(), PaintGetHeight()); 652 | 653 | } else { 654 | PaintClear(UNCOLORED); 655 | switch (one_h) { 656 | case 0: 657 | PaintDrawImage(ZERO, 4, 0, 67, 44, COLORED); 658 | break; 659 | case 1: 660 | PaintDrawImage(ONE, 4, 0, 67, 44, COLORED); 661 | break; 662 | case 2: 663 | PaintDrawImage(TWO, 4, 0, 67, 44, COLORED); 664 | break; 665 | case 3: 666 | PaintDrawImage(THREE, 4, 0, 67, 44, COLORED); 667 | break; 668 | case 4: 669 | PaintDrawImage(FOUR, 4, 0, 67, 44, COLORED); 670 | break; 671 | case 5: 672 | PaintDrawImage(FIVE, 4, 0, 67, 44, COLORED); 673 | break; 674 | case 6: 675 | PaintDrawImage(SIX, 4, 0, 67, 44, COLORED); 676 | break; 677 | case 7: 678 | PaintDrawImage(SEVEN, 4, 0, 67, 44, COLORED); 679 | break; 680 | case 8: 681 | PaintDrawImage(EIGHT, 4, 0, 67, 44, COLORED); 682 | break; 683 | case 9: 684 | PaintDrawImage(NINE, 4, 0, 67, 44, COLORED); 685 | break; 686 | } 687 | EpdSetFrameMemoryXY(PaintGetImage(), 24, 142, PaintGetWidth(), PaintGetHeight()); 688 | 689 | PaintClear(UNCOLORED); 690 | switch (two_h) { 691 | case 0: 692 | PaintDrawImage(ZERO, 4, 0, 67, 44, COLORED); 693 | break; 694 | case 1: 695 | PaintDrawImage(ONE, 4, 0, 67, 44, COLORED); 696 | break; 697 | case 2: 698 | PaintDrawImage(TWO, 4, 0, 67, 44, COLORED); 699 | break; 700 | case 3: 701 | PaintDrawImage(THREE, 4, 0, 67, 44, COLORED); 702 | break; 703 | case 4: 704 | PaintDrawImage(FOUR, 4, 0, 67, 44, COLORED); 705 | break; 706 | case 5: 707 | PaintDrawImage(FIVE, 4, 0, 67, 44, COLORED); 708 | break; 709 | case 6: 710 | PaintDrawImage(SIX, 4, 0, 67, 44, COLORED); 711 | break; 712 | case 7: 713 | PaintDrawImage(SEVEN, 4, 0, 67, 44, COLORED); 714 | break; 715 | case 8: 716 | PaintDrawImage(EIGHT, 4, 0, 67, 44, COLORED); 717 | break; 718 | case 9: 719 | PaintDrawImage(NINE, 4, 0, 67, 44, COLORED); 720 | break; 721 | } 722 | EpdSetFrameMemoryXY(PaintGetImage(), 24, 186, PaintGetWidth(), PaintGetHeight()); 723 | 724 | PaintSetWidth(24); 725 | PaintSetHeight(22); 726 | PaintSetRotate(ROTATE_180); 727 | PaintClear(UNCOLORED); 728 | PaintDrawImage(PERCENT, 4, 0, 16, 22, COLORED); 729 | EpdSetFrameMemoryXY(PaintGetImage(), 72, 228, PaintGetWidth(), PaintGetHeight()); 730 | 731 | 732 | PaintSetWidth(24); 733 | PaintSetHeight(12); 734 | PaintSetRotate(ROTATE_180); 735 | PaintClear(UNCOLORED); 736 | 737 | if (last_hum != 0) { 738 | if (hh > last_hum) { 739 | PaintDrawImage(IMAGE_UP, 5, 0, 14, 12, COLORED); 740 | upHum = true; 741 | downHum = false; 742 | } else if (hh < last_hum) { 743 | PaintDrawImage(IMAGE_DOWN, 5, 0, 14, 12, COLORED); 744 | upHum = false; 745 | downHum = true; 746 | } else { 747 | if (upHum == true) { 748 | PaintDrawImage(IMAGE_UP, 5, 0, 14, 12, COLORED); 749 | } 750 | if (downHum == true) { 751 | PaintDrawImage(IMAGE_DOWN, 5, 0, 14, 12, COLORED); 752 | } 753 | } 754 | } 755 | last_hum = hh; 756 | EpdSetFrameMemoryXY(PaintGetImage(), 100, 167, PaintGetWidth(), PaintGetHeight()); 757 | 758 | PaintSetWidth(24); 759 | PaintSetHeight(15); 760 | PaintSetRotate(ROTATE_180); 761 | PaintClear(UNCOLORED); 762 | PaintDrawImage(I2, 4, 0, 17, 15, COLORED); 763 | EpdSetFrameMemoryXY(PaintGetImage(), 96, 185, PaintGetWidth(), PaintGetHeight()); 764 | 765 | } 766 | } 767 | 768 | 769 | 770 | 771 | void epdBatteryData(uint8 b) { 772 | 773 | PaintSetWidth(22); 774 | PaintSetHeight(25); 775 | PaintSetRotate(ROTATE_0); 776 | PaintClear(UNCOLORED); 777 | 778 | if (b <= 0) { 779 | PaintDrawImage(bz, 4, 0, 16, 25, COLORED); 780 | } 781 | if (b > 0 && b <= 13) { 782 | PaintDrawImage(b0, 4, 0, 16, 25, COLORED); 783 | } 784 | if (b > 13 && b <= 25) { 785 | PaintDrawImage(b13, 4, 0, 16, 25, COLORED); 786 | } 787 | if (b > 25 && b <= 38) { 788 | PaintDrawImage(b38, 4, 0, 16, 25, COLORED); 789 | } 790 | if (b > 38 && b <= 50) { 791 | PaintDrawImage(b50, 4, 0, 16, 25, COLORED); 792 | } 793 | if (b > 50 && b <= 63) { 794 | PaintDrawImage(b63, 4, 0, 16, 25, COLORED); 795 | } 796 | if (b > 63 && b <= 75) { 797 | PaintDrawImage(b75, 4, 0, 16, 25, COLORED); 798 | } 799 | if (b > 75 && b <= 87) { 800 | PaintDrawImage(b87, 4, 0, 16, 25, COLORED); 801 | } 802 | if (b > 87) { 803 | PaintDrawImage(b100, 4, 0, 16, 25, COLORED); 804 | } 805 | EpdSetFrameMemoryXY(PaintGetImage(), 96, 26, PaintGetWidth(), PaintGetHeight()); 806 | } 807 | 808 | 809 | 810 | 811 | void epdZigbeeStatusData() { 812 | PaintSetWidth(24); 813 | PaintSetHeight(20); 814 | PaintSetRotate(ROTATE_90); 815 | PaintClear(UNCOLORED); 816 | if ( bdbAttributes.bdbNodeIsOnANetwork ){ 817 | PaintDrawImage(IMAGE_ONNETWORK, 4, 4, 16, 16, COLORED); 818 | } else { 819 | PaintDrawImage(IMAGE_OFFNETWORK, 4, 4, 16, 16, COLORED); 820 | } 821 | EpdSetFrameMemoryXY(PaintGetImage(), 96, 206, PaintGetWidth(), PaintGetHeight()); 822 | } 823 | 824 | 825 | 826 | void epdGraphData(void){ 827 | PaintSetWidth(20); 828 | PaintSetHeight(101); 829 | PaintSetRotate(ROTATE_270); 830 | PaintClear(UNCOLORED); 831 | int shag = 96; 832 | 833 | uint16 minActualT; 834 | uint16 maxActualT; 835 | uint16 minActualH; 836 | uint16 maxActualH; 837 | 838 | /* find min */ 839 | minActualT = massDataT[0]; 840 | 841 | for (int i = 0; i < 24; i++) 842 | { 843 | if (massDataT[i] < minActualT && massDataT[i] != 0) 844 | minActualT = massDataT[i]; 845 | } 846 | 847 | /* find max */ 848 | maxActualT = massDataT[0]; 849 | 850 | for (int i = 0; i < 24; i++) 851 | { 852 | if (massDataT[i] > maxActualT && massDataT[i] !=0) 853 | maxActualT = massDataT[i]; 854 | } 855 | 856 | /* find min */ 857 | minActualH = massDataH[0]; 858 | 859 | for (int i = 0; i < 24; i++) 860 | { 861 | if (massDataH[i] < minActualH && massDataH[i] != 0) 862 | minActualH = massDataH[i]; 863 | } 864 | 865 | /* find max */ 866 | maxActualH = massDataH[0]; 867 | 868 | for (int i = 0; i < 24; i++) 869 | { 870 | if (massDataH[i] > maxActualH && massDataH[i] !=0) 871 | maxActualH = massDataH[i]; 872 | } 873 | 874 | for(int i=0; i<24; i++) 875 | { 876 | int ch = (uint16)mapRange(minActualT, maxActualT, 0.0, 14.0, massDataT[i]); 877 | 878 | if(i == 23){ 879 | PaintDrawVerticalLine(shag, 4, ch+2, COLORED); 880 | PaintDrawVerticalLine(shag+1, 4, ch+2, COLORED); 881 | PaintDrawVerticalLine(shag+2, 4, ch+2, COLORED); 882 | }else{ 883 | PaintDrawVerticalLine(shag, 4, ch+2, COLORED); 884 | PaintDrawVerticalLine(shag+1, 4, ch+2, COLORED); 885 | PaintDrawVerticalLine(shag+2, 4, ch+2, COLORED); 886 | shag = shag-4; 887 | } 888 | } 889 | 890 | EpdSetFrameMemoryXY(PaintGetImage(), 0, 14, PaintGetWidth(), PaintGetHeight()); 891 | 892 | 893 | PaintClear(UNCOLORED); 894 | shag = 96; 895 | 896 | for(int i=0; i<24; i++) 897 | { 898 | int ch = (uint16)mapRange(minActualH, maxActualH, 0.0, 14.0, massDataH[i]); 899 | 900 | if(i == 23){ 901 | PaintDrawVerticalLine(shag, 4, ch+2, COLORED); 902 | PaintDrawVerticalLine(shag+1, 4, ch+2, COLORED); 903 | PaintDrawVerticalLine(shag+2, 4, ch+2, COLORED); 904 | }else{ 905 | PaintDrawVerticalLine(shag, 4, ch+2, COLORED); 906 | PaintDrawVerticalLine(shag+1, 4, ch+2, COLORED); 907 | PaintDrawVerticalLine(shag+2, 4, ch+2, COLORED); 908 | shag = shag-4; 909 | } 910 | } 911 | 912 | EpdSetFrameMemoryXY(PaintGetImage(), 0, 140, PaintGetWidth(), PaintGetHeight()); 913 | 914 | } 915 | 916 | 917 | void user_delay_ms(unsigned int delaytime) { 918 | while(delaytime--) 919 | { 920 | uint16 microSecs = 1000; 921 | while(microSecs--) 922 | { 923 | asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); 924 | } 925 | } 926 | } 927 | 928 | void conveyor(int *Arr, int n, int l) 929 | { 930 | int i,j,c; 931 | for (i=1; i<=l; i++) 932 | { 933 | c=Arr[0]; 934 | for (j=0; j