platform.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. #ifndef _PLATFORM_H_
  2. #define _PLATFORM_H_
  3. #include <stdint.h>
  4. #include <stdbool.h>
  5. #include <stdio.h>
  6. #include "sdkconfig.h"
  7. #include "cpu_esp32.h"
  8. #define PLATFORM_ALIGNMENT __attribute__((aligned(4)))
  9. #define PLATFORM_ALIGNMENT_PACKED __attribute__((aligned(4),packed))
  10. // Error / status codes
  11. enum
  12. {
  13. PLATFORM_ERR,
  14. PLATFORM_OK,
  15. PLATFORM_UNDERFLOW = -1
  16. };
  17. #if CONFIG_NODE_DEBUG
  18. # define NODE_DBG printf
  19. #else
  20. # define NODE_DBG(...) do{}while(0)
  21. #endif
  22. #if CONFIG_NODE_ERR
  23. # define NODE_ERR printf
  24. #else
  25. # define NODE_ERR(...) do{}while(0)
  26. #endif
  27. int platform_init (void);
  28. // *****************************************************************************
  29. // GPIO subsection
  30. int platform_gpio_exists( unsigned gpio );
  31. int platform_gpio_output_exists( unsigned gpio );
  32. // *****************************************************************************
  33. // UART subsection
  34. // Parity
  35. enum
  36. {
  37. PLATFORM_UART_PARITY_NONE = 0,
  38. PLATFORM_UART_PARITY_EVEN = 1,
  39. PLATFORM_UART_PARITY_ODD = 2,
  40. PLATFORM_UART_PARITY_MARK = 3,
  41. PLATFORM_UART_PARITY_SPACE = 4
  42. };
  43. // Stop bits
  44. enum
  45. {
  46. PLATFORM_UART_STOPBITS_1 = 1,
  47. PLATFORM_UART_STOPBITS_2 = 2,
  48. PLATFORM_UART_STOPBITS_1_5 = 3
  49. };
  50. // Flow control types (this is a bit mask, one can specify PLATFORM_UART_FLOW_RTS | PLATFORM_UART_FLOW_CTS )
  51. #define PLATFORM_UART_FLOW_NONE 0
  52. #define PLATFORM_UART_FLOW_RTS 1
  53. #define PLATFORM_UART_FLOW_CTS 2
  54. // The platform UART functions
  55. static inline int platform_uart_exists( unsigned id ) { return id < NUM_UART; }
  56. uint32_t platform_uart_setup( unsigned id, uint32_t baud, int databits, int parity, int stopbits );
  57. void platform_uart_send( unsigned id, uint8_t data );
  58. int platform_uart_set_flow_control( unsigned id, int type );
  59. // *****************************************************************************
  60. // Sigma-Delta subsection
  61. int platform_sigma_delta_exists( unsigned channel );
  62. uint8_t platform_sigma_delta_setup( uint8_t channel, uint8_t gpio_num );
  63. uint8_t platform_sigma_delta_close( uint8_t channel );
  64. // PWM emulation not possible, code kept for future reference
  65. //uint8_t platform_sigma_delta_set_pwmduty( uint8_t channel, uint8_t duty );
  66. uint8_t platform_sigma_delta_set_prescale( uint8_t channel, uint8_t prescale );
  67. uint8_t platform_sigma_delta_set_duty( uint8_t channel, int8_t duty );
  68. // *****************************************************************************
  69. // I2C platform interface
  70. // I2C speed
  71. enum
  72. {
  73. PLATFORM_I2C_SPEED_SLOW = 100000,
  74. PLATFORM_I2C_SPEED_FAST = 400000,
  75. PLATFORM_I2C_SPEED_FASTPLUS = 1000000
  76. };
  77. // I2C direction
  78. enum
  79. {
  80. PLATFORM_I2C_DIRECTION_TRANSMITTER,
  81. PLATFORM_I2C_DIRECTION_RECEIVER
  82. };
  83. int platform_i2c_exists( unsigned id );
  84. int platform_i2c_setup( unsigned id, uint8_t sda, uint8_t scl, uint32_t speed );
  85. int platform_i2c_send_start( unsigned id );
  86. int platform_i2c_send_stop( unsigned id );
  87. int platform_i2c_send_address( unsigned id, uint16_t address, int direction, int ack_check_en );
  88. int platform_i2c_send_byte( unsigned id, uint8_t data, int ack_check_en );
  89. // ack_val: 1 = send ACK, 0 = send NACK
  90. int platform_i2c_recv_byte( unsigned id, int ack_val );
  91. // *****************************************************************************
  92. // RMT platform interface
  93. /**
  94. * @brief Allocate an RMT channel.
  95. *
  96. * @param num_mem Number of memory blocks.
  97. *
  98. * @return
  99. * - Channel number when successful
  100. * - -1 if no channel available
  101. *
  102. */
  103. int platform_rmt_allocate( uint8_t num_mem );
  104. /**
  105. * @brief Release a previously allocated RMT channel.
  106. *
  107. * @param channel Channel number.
  108. *
  109. */
  110. void platform_rmt_release( uint8_t channel );
  111. // *****************************************************************************
  112. // Onewire platform interface
  113. typedef struct {
  114. unsigned char ROM_NO[8];
  115. uint8_t LastDiscrepancy;
  116. uint8_t LastFamilyDiscrepancy;
  117. uint8_t LastDeviceFlag;
  118. uint8_t power;
  119. } platform_onewire_bus_t;
  120. int platform_onewire_init( uint8_t gpio_num );
  121. int platform_onewire_reset( uint8_t gpio_num, uint8_t *presence );
  122. int platform_onewire_write_bytes( uint8_t gpio_num, const uint8_t *buf, uint16_t count, bool power );
  123. int platform_onewire_depower( uint8_t gpio_num );
  124. int platform_onewire_read_bytes( uint8_t gpio_num, uint8_t *buf, uint16_t count );
  125. int platform_onewire_depower( uint8_t gpio_num );
  126. void platform_onewire_reset_search( platform_onewire_bus_t *bus );
  127. void platform_onewire_target_search( uint8_t family_code, platform_onewire_bus_t *bus );
  128. uint8_t platform_onewire_search( uint8_t pin, uint8_t *newAddr, platform_onewire_bus_t *bus );
  129. uint8_t platform_onewire_crc8( const uint8_t *addr, uint8_t len );
  130. uint8_t platform_onewire_crc8( const uint8_t *addr, uint8_t len );
  131. bool platform_onewire_check_crc16( const uint8_t* input, uint16_t len, const uint8_t* inverted_crc, uint16_t crc );
  132. uint16_t platform_onewire_crc16( const uint8_t* input, uint16_t len, uint16_t crc );
  133. // *****************************************************************************
  134. // DHT platform interface
  135. #define PLATFORM_DHT11_WAKEUP_MS 20
  136. #define PLATFORM_DHT2X_WAKEUP_MS 1
  137. int platform_dht_read( uint8_t gpio_num, uint8_t wakeup_ms, uint8_t *data );
  138. // Internal flash erase/write functions
  139. uint32_t platform_flash_get_sector_of_address( uint32_t addr );
  140. uint32_t platform_flash_write( const void *from, uint32_t toaddr, uint32_t size );
  141. uint32_t platform_flash_read( void *to, uint32_t fromaddr, uint32_t size );
  142. uint32_t platform_s_flash_write( const void *from, uint32_t toaddr, uint32_t size );
  143. uint32_t platform_s_flash_read( void *to, uint32_t fromaddr, uint32_t size );
  144. uint32_t platform_flash_get_num_sectors(void);
  145. int platform_flash_erase_sector( uint32_t sector_id );
  146. // Internal flash partitions
  147. #define PLATFORM_PARTITION_TYPE_APP 0x00
  148. #define PLATFORM_PARTITION_TYPE_DATA 0x01
  149. #define PLATFORM_PARTITION_TYPE_NODEMCU 0xC2
  150. #define PLATFORM_PARTITION_SUBTYPE_APP_FACTORY 0x00
  151. #define PLATFORM_PARTITION_SUBTYPE_APP_OTA(n) (0x10+n)
  152. #define PLATFORM_PARTITION_SUBTYPE_APP_TEST 0x20
  153. #define PLATFORM_PARTITION_SUBTYPE_DATA_OTA 0x00
  154. #define PLATFORM_PARTITION_SUBTYPE_DATA_RF 0x01
  155. #define PLATFORM_PARTITION_SUBTYPE_DATA_WIFI 0x02
  156. #define PLATFORM_PARTITION_SUBTYPE_NODEMCU_SPIFFS 0x00
  157. typedef struct {
  158. uint8_t label[16];
  159. uint32_t offs;
  160. uint32_t size;
  161. uint8_t type;
  162. uint8_t subtype;
  163. } platform_partition_t;
  164. /**
  165. * Obtain partition information for the internal flash.
  166. * @param idx Which partition index to load info for.
  167. * @param info Buffer to store the info in.
  168. * @returns True if the partition info was loaded, false if not (e.g. no such
  169. * partition idx).
  170. */
  171. bool platform_partition_info (uint8_t idx, platform_partition_t *info);
  172. /**
  173. * Appends a partition entry to the partition table, if possible.
  174. * Intended for auto-creation of a SPIFFS partition.
  175. * @param info The partition definition to append.
  176. * @returns True if the partition could be added, false if not.
  177. */
  178. bool platform_partition_add (const platform_partition_t *info);
  179. // *****************************************************************************
  180. // Helper macros
  181. #define MOD_CHECK_ID( mod, id )\
  182. if( !platform_ ## mod ## _exists( id ) )\
  183. return luaL_error( L, #mod" %d does not exist", ( unsigned )id )
  184. #define MOD_CHECK_TIMER( id )\
  185. if( id == PLATFORM_TIMER_SYS_ID && !platform_timer_sys_available() )\
  186. return luaL_error( L, "the system timer is not available on this platform" );\
  187. if( !platform_tmr_exists( id ) )\
  188. return luaL_error( L, "timer %d does not exist", ( unsigned )id )\
  189. #define MOD_CHECK_RES_ID( mod, id, resmod, resid )\
  190. if( !platform_ ## mod ## _check_ ## resmod ## _id( id, resid ) )\
  191. return luaL_error( L, #resmod" %d not valid with " #mod " %d", ( unsigned )resid, ( unsigned )id )
  192. #endif