ucg_nodemcu_hal.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // Do not use the code from ucg submodule and skip the complete source here
  2. // if the ucg module is not selected.
  3. // Reason: The whole ucg submodule code tree might not even exist in this case.
  4. #include "user_modules.h"
  5. #ifdef LUA_USE_MODULES_UCG
  6. #include <string.h>
  7. #include <stdlib.h>
  8. #include "platform.h"
  9. #include "user_interface.h"
  10. #define USE_PIN_LIST
  11. #include "ucg_nodemcu_hal.h"
  12. #define delayMicroseconds os_delay_us
  13. static spi_data_type cache;
  14. static uint8_t cached;
  15. #define CACHED_TRANSFER(dat, num) cache = cached = 0; \
  16. while( arg > 0 ) { \
  17. if (cached == 4) { \
  18. platform_spi_transaction( 1, 0, 0, 32, cache, 0, 0, 0 ); \
  19. cache = cached = 0; \
  20. } \
  21. cache = (cache << num*8) | dat; \
  22. cached += num; \
  23. arg--; \
  24. } \
  25. if (cached > 0) { \
  26. platform_spi_transaction( 1, 0, 0, cached * 8, cache, 0, 0, 0 ); \
  27. }
  28. int16_t ucg_com_nodemcu_hw_spi(ucg_t *ucg, int16_t msg, uint16_t arg, uint8_t *data)
  29. {
  30. switch(msg)
  31. {
  32. case UCG_COM_MSG_POWER_UP:
  33. /* "data" is a pointer to ucg_com_info_t structure with the following information: */
  34. /* ((ucg_com_info_t *)data)->serial_clk_speed value in nanoseconds */
  35. /* ((ucg_com_info_t *)data)->parallel_clk_speed value in nanoseconds */
  36. /* setup pins */
  37. // we assume that the SPI interface was already initialized
  38. // just care for the /CS and D/C pins
  39. //platform_gpio_write( ucg->pin_list[0], value );
  40. if ( ucg->pin_list[UCG_PIN_RST] != UCG_PIN_VAL_NONE )
  41. platform_gpio_mode( ucg->pin_list[UCG_PIN_RST], PLATFORM_GPIO_OUTPUT, PLATFORM_GPIO_FLOAT );
  42. platform_gpio_mode( ucg->pin_list[UCG_PIN_CD], PLATFORM_GPIO_OUTPUT, PLATFORM_GPIO_FLOAT );
  43. if ( ucg->pin_list[UCG_PIN_CS] != UCG_PIN_VAL_NONE )
  44. platform_gpio_mode( ucg->pin_list[UCG_PIN_CS], PLATFORM_GPIO_OUTPUT, PLATFORM_GPIO_FLOAT );
  45. break;
  46. case UCG_COM_MSG_POWER_DOWN:
  47. break;
  48. case UCG_COM_MSG_DELAY:
  49. delayMicroseconds(arg);
  50. break;
  51. case UCG_COM_MSG_CHANGE_RESET_LINE:
  52. if ( ucg->pin_list[UCG_PIN_RST] != UCG_PIN_VAL_NONE )
  53. platform_gpio_write( ucg->pin_list[UCG_PIN_RST], arg );
  54. break;
  55. case UCG_COM_MSG_CHANGE_CS_LINE:
  56. if ( ucg->pin_list[UCG_PIN_CS] != UCG_PIN_VAL_NONE )
  57. platform_gpio_write( ucg->pin_list[UCG_PIN_CS], arg );
  58. break;
  59. case UCG_COM_MSG_CHANGE_CD_LINE:
  60. platform_gpio_write( ucg->pin_list[UCG_PIN_CD], arg );
  61. break;
  62. case UCG_COM_MSG_SEND_BYTE:
  63. platform_spi_send( 1, 8, arg );
  64. break;
  65. case UCG_COM_MSG_REPEAT_1_BYTE:
  66. CACHED_TRANSFER(data[0], 1);
  67. break;
  68. case UCG_COM_MSG_REPEAT_2_BYTES:
  69. CACHED_TRANSFER((data[0] << 8) | data[1], 2);
  70. break;
  71. case UCG_COM_MSG_REPEAT_3_BYTES:
  72. while( arg > 0 ) {
  73. platform_spi_transaction( 1, 0, 0, 24, (data[0] << 16) | (data[1] << 8) | data[2], 0, 0, 0 );
  74. arg--;
  75. }
  76. break;
  77. case UCG_COM_MSG_SEND_STR:
  78. CACHED_TRANSFER(*data++, 1);
  79. break;
  80. case UCG_COM_MSG_SEND_CD_DATA_SEQUENCE:
  81. while(arg > 0)
  82. {
  83. if ( *data != 0 )
  84. {
  85. /* set the data line directly, ignore the setting from UCG_CFG_CD */
  86. if ( *data == 1 )
  87. {
  88. platform_gpio_write( ucg->pin_list[UCG_PIN_CD], 0 );
  89. }
  90. else
  91. {
  92. platform_gpio_write( ucg->pin_list[UCG_PIN_CD], 1 );
  93. }
  94. }
  95. data++;
  96. platform_spi_send( 1, 8, *data );
  97. data++;
  98. arg--;
  99. }
  100. break;
  101. }
  102. return 1;
  103. }
  104. #endif /* LUA_USE_MODULES_UCG */