platform.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. // Platform-specific functions
  2. #ifndef __PLATFORM_H__
  3. #define __PLATFORM_H__
  4. #include "cpu_esp8266.h"
  5. #include "c_types.h"
  6. #include "driver/pwm.h"
  7. #include "driver/uart.h"
  8. #include "task/task.h"
  9. // Error / status codes
  10. enum
  11. {
  12. PLATFORM_ERR,
  13. PLATFORM_OK,
  14. PLATFORM_UNDERFLOW = -1
  15. };
  16. // Platform initialization
  17. int platform_init(void);
  18. void platform_int_init(void);
  19. // ****************************************************************************
  20. // KEY_LED functions
  21. uint8_t platform_key_led( uint8_t level);
  22. // *****************************************************************************
  23. // GPIO subsection
  24. #define PLATFORM_GPIO_FLOAT 0
  25. #define PLATFORM_GPIO_PULLUP 1
  26. #define PLATFORM_GPIO_INT 2
  27. #define PLATFORM_GPIO_OUTPUT 1
  28. #define PLATFORM_GPIO_OPENDRAIN 3
  29. #define PLATFORM_GPIO_INPUT 0
  30. #define PLATFORM_GPIO_HIGH 1
  31. #define PLATFORM_GPIO_LOW 0
  32. typedef uint32_t (* platform_hook_function)(uint32_t bitmask);
  33. static inline int platform_gpio_exists( unsigned pin ) { return pin < NUM_GPIO; }
  34. int platform_gpio_mode( unsigned pin, unsigned mode, unsigned pull );
  35. int platform_gpio_write( unsigned pin, unsigned level );
  36. int platform_gpio_read( unsigned pin );
  37. // Note that these functions will not be compiled in unless GPIO_INTERRUPT_ENABLE and
  38. // GPIO_INTERRUPT_HOOK_ENABLE are defined.
  39. int platform_gpio_register_intr_hook(uint32_t gpio_bits, platform_hook_function hook);
  40. #define platform_gpio_unregister_intr_hook(hook) \
  41. platform_gpio_register_intr_hook(0, hook);
  42. void platform_gpio_intr_init( unsigned pin, GPIO_INT_TYPE type );
  43. void platform_gpio_init( task_handle_t gpio_task );
  44. // *****************************************************************************
  45. // Timer subsection
  46. // Timer data type
  47. typedef uint32_t timer_data_type;
  48. // *****************************************************************************
  49. // CAN subsection
  50. // Maximum length for any CAN message
  51. #define PLATFORM_CAN_MAXLEN 8
  52. // eLua CAN ID types
  53. enum
  54. {
  55. ELUA_CAN_ID_STD = 0,
  56. ELUA_CAN_ID_EXT
  57. };
  58. static inline int platform_can_exists( unsigned id ) { return NUM_CAN && (id < NUM_CAN); }
  59. uint32_t platform_can_setup( unsigned id, uint32_t clock );
  60. int platform_can_send( unsigned id, uint32_t canid, uint8_t idtype, uint8_t len, const uint8_t *data );
  61. int platform_can_recv( unsigned id, uint32_t *canid, uint8_t *idtype, uint8_t *len, uint8_t *data );
  62. // *****************************************************************************
  63. // SPI subsection
  64. // There are 4 "virtual" SPI ports (SPI0...SPI3).
  65. #define PLATFORM_SPI_TOTAL 4
  66. // TODO: PLATFORM_SPI_TOTAL is not used - figure out purpose, or remove?
  67. // SPI mode
  68. #define PLATFORM_SPI_MASTER 1
  69. #define PLATFORM_SPI_SLAVE 0
  70. // SS values
  71. #define PLATFORM_SPI_SELECT_ON 1
  72. #define PLATFORM_SPI_SELECT_OFF 0
  73. // SPI enable/disable
  74. #define PLATFORM_SPI_ENABLE 1
  75. #define PLATFORM_SPI_DISABLE 0
  76. // SPI clock phase
  77. #define PLATFORM_SPI_CPHA_LOW 0
  78. #define PLATFORM_SPI_CPHA_HIGH 1
  79. // SPI clock polarity
  80. #define PLATFORM_SPI_CPOL_LOW 0
  81. #define PLATFORM_SPI_CPOL_HIGH 1
  82. // Data types
  83. typedef uint32_t spi_data_type;
  84. // The platform SPI functions
  85. static inline int platform_spi_exists( unsigned id ) { return id < NUM_SPI; }
  86. uint32_t platform_spi_setup( uint8_t id, int mode, unsigned cpol, unsigned cpha, uint32_t clock_div);
  87. int platform_spi_send( uint8_t id, uint8_t bitlen, spi_data_type data );
  88. spi_data_type platform_spi_send_recv( uint8_t id, uint8_t bitlen, spi_data_type data );
  89. void platform_spi_select( unsigned id, int is_select );
  90. int platform_spi_blkwrite( uint8_t id, size_t len, const uint8_t *data );
  91. int platform_spi_blkread( uint8_t id, size_t len, uint8_t *data );
  92. int platform_spi_set_mosi( uint8_t id, uint16_t offset, uint8_t bitlen, spi_data_type data );
  93. spi_data_type platform_spi_get_miso( uint8_t id, uint16_t offset, uint8_t bitlen );
  94. int platform_spi_transaction( uint8_t id, uint8_t cmd_bitlen, spi_data_type cmd_data,
  95. uint8_t addr_bitlen, spi_data_type addr_data,
  96. uint16_t mosi_bitlen, uint8_t dummy_bitlen, int16_t miso_bitlen );
  97. // *****************************************************************************
  98. // UART subsection
  99. // There are 3 "virtual" UART ports (UART0...UART2).
  100. #define PLATFORM_UART_TOTAL 3
  101. // TODO: PLATFORM_UART_TOTAL is not used - figure out purpose, or remove?
  102. // Note: Some CPUs (e.g. LM4F/TM4C) have more than 3 hardware UARTs
  103. // Parity
  104. enum
  105. {
  106. PLATFORM_UART_PARITY_NONE = 0,
  107. PLATFORM_UART_PARITY_EVEN = 1,
  108. PLATFORM_UART_PARITY_ODD = 2,
  109. PLATFORM_UART_PARITY_MARK = 3,
  110. PLATFORM_UART_PARITY_SPACE = 4
  111. };
  112. // Stop bits
  113. enum
  114. {
  115. PLATFORM_UART_STOPBITS_1 = 1,
  116. PLATFORM_UART_STOPBITS_2 = 2,
  117. PLATFORM_UART_STOPBITS_1_5 = 3
  118. };
  119. // Flow control types (this is a bit mask, one can specify PLATFORM_UART_FLOW_RTS | PLATFORM_UART_FLOW_CTS )
  120. #define PLATFORM_UART_FLOW_NONE 0
  121. #define PLATFORM_UART_FLOW_RTS 1
  122. #define PLATFORM_UART_FLOW_CTS 2
  123. // The platform UART functions
  124. static inline int platform_uart_exists( unsigned id ) { return id < NUM_UART; }
  125. uint32_t platform_uart_setup( unsigned id, uint32_t baud, int databits, int parity, int stopbits );
  126. int platform_uart_set_buffer( unsigned id, unsigned size );
  127. void platform_uart_send( unsigned id, uint8_t data );
  128. void platform_s_uart_send( unsigned id, uint8_t data );
  129. int platform_uart_recv( unsigned id, unsigned timer_id, timer_data_type timeout );
  130. int platform_s_uart_recv( unsigned id, timer_data_type timeout );
  131. int platform_uart_set_flow_control( unsigned id, int type );
  132. int platform_s_uart_set_flow_control( unsigned id, int type );
  133. void platform_uart_alt( int set );
  134. void platform_uart_get_config(unsigned id, uint32_t *baudp, uint32_t *databitsp, uint32_t *parityp, uint32_t *stopbitsp);
  135. // *****************************************************************************
  136. // PWM subsection
  137. // There are 16 "virtual" PWM channels (PWM0...PWM15)
  138. #define PLATFORM_PWM_TOTAL 16
  139. // TODO: PLATFORM_PWM_TOTAL is not used - figure out purpose, or remove?
  140. #define NORMAL_PWM_DEPTH PWM_DEPTH
  141. #define NORMAL_DUTY(d) (((unsigned)(d)*NORMAL_PWM_DEPTH) / PWM_DEPTH)
  142. #define DUTY(d) ((uint16_t)( ((unsigned)(d)*PWM_DEPTH) / NORMAL_PWM_DEPTH) )
  143. // The platform PWM functions
  144. void platform_pwm_init( void );
  145. static inline int platform_pwm_exists( unsigned id ) { return ((id < NUM_PWM) && (id > 0)); }
  146. uint32_t platform_pwm_setup( unsigned id, uint32_t frequency, unsigned duty );
  147. void platform_pwm_close( unsigned id );
  148. bool platform_pwm_start( unsigned id );
  149. void platform_pwm_stop( unsigned id );
  150. uint32_t platform_pwm_set_clock( unsigned id, uint32_t data );
  151. uint32_t platform_pwm_get_clock( unsigned id );
  152. uint32_t platform_pwm_set_duty( unsigned id, uint32_t data );
  153. uint32_t platform_pwm_get_duty( unsigned id );
  154. // *****************************************************************************
  155. // The platform ADC functions
  156. // Functions requiring platform-specific implementation
  157. int platform_adc_update_sequence(void);
  158. int platform_adc_start_sequence(void);
  159. void platform_adc_stop( unsigned id );
  160. uint32_t platform_adc_set_clock( unsigned id, uint32_t frequency);
  161. int platform_adc_check_timer_id( unsigned id, unsigned timer_id );
  162. // ADC Common Functions
  163. static inline int platform_adc_exists( unsigned id ) { return id < NUM_ADC; }
  164. uint32_t platform_adc_get_maxval( unsigned id );
  165. uint32_t platform_adc_set_smoothing( unsigned id, uint32_t length );
  166. void platform_adc_set_blocking( unsigned id, uint32_t mode );
  167. void platform_adc_set_freerunning( unsigned id, uint32_t mode );
  168. uint32_t platform_adc_is_done( unsigned id );
  169. void platform_adc_set_timer( unsigned id, uint32_t timer );
  170. // ****************************************************************************
  171. // OneWire functions
  172. static inline int platform_ow_exists( unsigned id ) { return ((id < NUM_OW) && (id > 0)); }
  173. // ****************************************************************************
  174. // Timer functions
  175. static inline int platform_tmr_exists( unsigned id ) { return id < NUM_TMR; }
  176. // *****************************************************************************
  177. // Sigma-Delta platform interface
  178. // ****************************************************************************
  179. // Sigma-Delta functions
  180. static inline int platform_sigma_delta_exists( unsigned id ) {return ((id < NUM_GPIO) && (id > 0)); }
  181. uint8_t platform_sigma_delta_setup( uint8_t pin );
  182. uint8_t platform_sigma_delta_close( uint8_t pin );
  183. void platform_sigma_delta_set_pwmduty( uint8_t duty );
  184. void platform_sigma_delta_set_prescale( uint8_t prescale );
  185. void platform_sigma_delta_set_target( uint8_t target );
  186. // *****************************************************************************
  187. // I2C platform interface
  188. // I2C speed
  189. enum
  190. {
  191. PLATFORM_I2C_SPEED_SLOW = 100000,
  192. PLATFORM_I2C_SPEED_FAST = 400000
  193. };
  194. // I2C direction
  195. enum
  196. {
  197. PLATFORM_I2C_DIRECTION_TRANSMITTER,
  198. PLATFORM_I2C_DIRECTION_RECEIVER
  199. };
  200. #ifdef NUM_I2C
  201. static inline int platform_i2c_exists( unsigned id ) { return id < NUM_I2C; }
  202. #else
  203. static inline int platform_i2c_exists( unsigned id ) { return 0; }
  204. #endif
  205. uint32_t platform_i2c_setup( unsigned id, uint8_t sda, uint8_t scl, uint32_t speed );
  206. void platform_i2c_send_start( unsigned id );
  207. void platform_i2c_send_stop( unsigned id );
  208. int platform_i2c_send_address( unsigned id, uint16_t address, int direction );
  209. int platform_i2c_send_byte( unsigned id, uint8_t data );
  210. int platform_i2c_recv_byte( unsigned id, int ack );
  211. // *****************************************************************************
  212. // Ethernet specific functions
  213. void platform_eth_send_packet( const void* src, uint32_t size );
  214. uint32_t platform_eth_get_packet_nb( void* buf, uint32_t maxlen );
  215. void platform_eth_force_interrupt(void);
  216. uint32_t platform_eth_get_elapsed_time(void);
  217. // *****************************************************************************
  218. // Internal flash erase/write functions
  219. uint32_t platform_flash_get_first_free_block_address( uint32_t *psect );
  220. uint32_t platform_flash_get_sector_of_address( uint32_t addr );
  221. uint32_t platform_flash_write( const void *from, uint32_t toaddr, uint32_t size );
  222. uint32_t platform_flash_read( void *to, uint32_t fromaddr, uint32_t size );
  223. uint32_t platform_s_flash_write( const void *from, uint32_t toaddr, uint32_t size );
  224. uint32_t platform_s_flash_read( void *to, uint32_t fromaddr, uint32_t size );
  225. uint32_t platform_flash_get_num_sectors(void);
  226. int platform_flash_erase_sector( uint32_t sector_id );
  227. /**
  228. * Translated a mapped address to a physical flash address, based on the
  229. * current flash cache mapping.
  230. * @param mapped_addr Address to translate (>= INTERNAL_FLASH_MAPPED_ADDRESS)
  231. * @return the corresponding physical flash address, or -1 if flash cache is
  232. * not currently active.
  233. * @see Cache_Read_Enable.
  234. */
  235. uint32_t platform_flash_mapped2phys (uint32_t mapped_addr);
  236. // *****************************************************************************
  237. // Allocator support
  238. void* platform_get_first_free_ram( unsigned id );
  239. void* platform_get_last_free_ram( unsigned id );
  240. // *****************************************************************************
  241. // Other glue
  242. int platform_ow_exists( unsigned id );
  243. int platform_gpio_exists( unsigned id );
  244. int platform_tmr_exists( unsigned id );
  245. // *****************************************************************************
  246. // Helper macros
  247. #define MOD_CHECK_ID( mod, id )\
  248. if( !platform_ ## mod ## _exists( id ) )\
  249. return luaL_error( L, #mod" %d does not exist", ( unsigned )id )
  250. #define MOD_CHECK_TIMER( id )\
  251. if( id == PLATFORM_TIMER_SYS_ID && !platform_timer_sys_available() )\
  252. return luaL_error( L, "the system timer is not available on this platform" );\
  253. if( !platform_tmr_exists( id ) )\
  254. return luaL_error( L, "timer %d does not exist", ( unsigned )id )\
  255. #define MOD_CHECK_RES_ID( mod, id, resmod, resid )\
  256. if( !platform_ ## mod ## _check_ ## resmod ## _id( id, resid ) )\
  257. return luaL_error( L, #resmod" %d not valid with " #mod " %d", ( unsigned )resid, ( unsigned )id )
  258. #endif