knx
ETS configurable knx-stack
libretiny_platform.cpp
Go to the documentation of this file.
1 #include "libretiny_platform.h"
2 
3 #ifdef LIBRETINY
4 #include <Arduino.h>
5 #include "knx/bits.h"
6 
7 #include <lwip/netif.h>
8 
9 #ifndef KNX_SERIAL
10 #define KNX_SERIAL Serial
11 #endif
12 
13 #ifndef KNX_FLASH_OFFSET
14 #error "KNX_FLASH_OFFSET is not defined. E.g. 0x1DB000 for BK7231N"
15 #elif (KNX_FLASH_OFFSET % 4096) != 0
16 #error "KNX_FLASH_OFFSET must be a multiple of 4096"
17 #endif
18 
19 static uint8_t NVS_buffer[KNX_FLASH_SIZE];
20 
22 #ifndef KNX_NO_DEFAULT_UART
23  : ArduinoPlatform(&KNX_SERIAL)
24 #endif
25 {
26  _memoryType = Flash;
27 }
28 
30 {
32 }
33 
35 {
36  return WiFi.localIP();
37 }
38 
40 {
41  return WiFi.subnetMask();
42 }
43 
45 {
46  return WiFi.gatewayIP();
47 }
48 
49 void LibretinyPlatform::macAddress(uint8_t * addr)
50 {
51  WiFi.macAddress(addr);
52 }
53 
55 {
56  return lt_cpu_get_mac_id();
57 }
58 
60 {
61  println("restart");
62  lt_reboot();
63 }
64 
65 void LibretinyPlatform::setupMultiCast(uint32_t addr, uint16_t port)
66 {
67  //workaround for libretiny bug: NETIF_FLAG_IGMP is not set by default
68  struct netif *netif;
69  for (netif = netif_list; netif != NULL; netif = netif->next)
70  {
71  netif->flags |= NETIF_FLAG_IGMP;
72  }
73 
74  IPAddress mcastaddr(htonl(addr));
75  KNX_DEBUG_SERIAL.printf("setup multicast addr: %d.%d.%d.%d port: %d ip: %d.%d.%d.%d\n", mcastaddr[0], mcastaddr[1], mcastaddr[2], mcastaddr[3], port, WiFi.localIP()[0], WiFi.localIP()[1], WiFi.localIP()[2], WiFi.localIP()[3]);
76  uint8_t result = _udp.beginMulticast(mcastaddr, port);
77  KNX_DEBUG_SERIAL.printf("multicast setup result %d\n", result);
78 }
79 
81 {
82  _udp.stop();
83 }
84 
85 bool LibretinyPlatform::sendBytesMultiCast(uint8_t * buffer, uint16_t len)
86 {
87  _udp.beginMulticastPacket();
88  _udp.write(buffer, len);
89  _udp.endPacket();
90  return true;
91 }
92 
93 int LibretinyPlatform::readBytesMultiCast(uint8_t * buffer, uint16_t maxLen)
94 {
95  int len = _udp.parsePacket();
96 
97  if (len == 0)
98  return 0;
99 
100  if (len > maxLen)
101  {
102  KNX_DEBUG_SERIAL.printf("udp buffer to small. was %d, needed %d\n", maxLen, len);
103  fatalError();
104  }
105 
106  _udp.read(buffer, len);
107  return len;
108 }
109 
110 bool LibretinyPlatform::sendBytesUniCast(uint32_t addr, uint16_t port, uint8_t* buffer, uint16_t len)
111 {
112  IPAddress ucastaddr(htonl(addr));
113  println("sendBytesUniCast endPacket fail");
114 
115  if(_udp.beginPacket(ucastaddr, port) == 1)
116  {
117  _udp.write(buffer, len);
118 
119  if(_udp.endPacket() == 0)
120  println("sendBytesUniCast endPacket fail");
121  }
122  else
123  println("sendBytesUniCast beginPacket fail");
124 
125  return true;
126 }
127 
129 {
130  return 16;
131 }
132 
134 {
135  return 256;
136 }
137 
139 {
140  lt_flash_read(KNX_FLASH_OFFSET, NVS_buffer, KNX_FLASH_SIZE);
141  return NVS_buffer;
142 }
143 
145 {
146  if(KNX_FLASH_SIZE <= 0)
147  return 0;
148  else
149  return ( (KNX_FLASH_SIZE - 1) / (flashPageSize() * flashEraseBlockSize())) + 1;
150 }
151 
152 void LibretinyPlatform::flashErase(uint16_t eraseBlockNum)
153 {
154  // 16 pages x 256byte/page = 4096byte
155  lt_flash_erase_block(KNX_FLASH_OFFSET + eraseBlockNum * flashPageSize() * flashEraseBlockSize());
156 }
157 
158 void LibretinyPlatform::flashWritePage(uint16_t pageNumber, uint8_t* data)
159 {
160  lt_flash_write(KNX_FLASH_OFFSET + pageNumber * flashPageSize(), data, flashPageSize());
161 }
162 
164 {
166  {
167  lt_flash_erase_block(KNX_FLASH_OFFSET + _bufferedEraseblockNumber * flashPageSize() * flashEraseBlockSize());
169  _bufferedEraseblockDirty = false;
170  }
171 }
172 
173 #endif
void println(const char *s)
bool sendBytesUniCast(uint32_t addr, uint16_t port, uint8_t *buffer, uint16_t len) override
int readBytesMultiCast(uint8_t *buffer, uint16_t maxLen) override
bool sendBytesMultiCast(uint8_t *buffer, uint16_t len) override
virtual size_t flashEraseBlockSize()
virtual void flashWritePage(uint16_t pageNumber, uint8_t *data)
uint32_t uniqueSerialNumber() override
virtual size_t userFlashSizeEraseBlocks()
void macAddress(uint8_t *addr) override
virtual void flashErase(uint16_t eraseBlockNum)
uint32_t currentDefaultGateway() override
void setupMultiCast(uint32_t addr, uint16_t port) override
virtual size_t flashPageSize()
virtual uint8_t * userFlashStart()
uint32_t currentSubnetMask() override
uint32_t currentIpAddress() override
void closeMultiCast() override
uint8_t * _eraseblockBuffer
Definition: platform.h:142
NvMemoryType _memoryType
Definition: platform.h:130
int32_t _bufferedEraseblockNumber
Definition: platform.h:143
bool _bufferedEraseblockDirty
Definition: platform.h:144
@ Flash
Definition: platform.h:27