main.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. * Project: Arrow
  3. * Author: curiousmuch
  4. */
  5. #include <stdio.h>
  6. #include "freertos/FreeRTOS.h"
  7. #include "freertos/task.h"
  8. #include "driver/gpio.h"
  9. #include "sdkconfig.h"
  10. // Includes for CC1120 Driver
  11. #include "driver/spi_master.h"
  12. #include "esp_err.h"
  13. #include "cc1120.h"
  14. #include "cc1120_protocol.h"
  15. // CC1120 - ESP32 I/O
  16. // NOTE: Logic Probe is connecting to RESET - Pin1
  17. #define CC1120_RESET 22
  18. #define CC1120_CS 5
  19. #define CC1120_SCLK 18
  20. #define CC1120_MOSI 23
  21. #define CC1120_MISO 19
  22. #define CC1120_GPIO0 36
  23. #define CC1120_GPIO0_RTC 0
  24. #define CC1120_GPIO2 39
  25. #define CC1120_GPIO2_RTC 3
  26. #define CC1120_GPIO3 34
  27. #define CC1120_GPIO3_RTC 4
  28. #define CC1120_WRITE_BIT 0
  29. #define CC1120_READ_BIT BIT(1)
  30. #define CC1120_BURST_BIT BIT(0)
  31. // Public Configurations for CC1120 SPI Driver
  32. spi_bus_config_t bus_config =
  33. {
  34. .miso_io_num = CC1120_MISO,
  35. .mosi_io_num = CC1120_MOSI,
  36. .sclk_io_num = CC1120_SCLK,
  37. .quadwp_io_num = -1,
  38. .quadhd_io_num = -1,
  39. .max_transfer_sz = 150
  40. };
  41. spi_device_interface_config_t interface_config =
  42. {
  43. .command_bits = 2,
  44. .address_bits = 6,
  45. .dummy_bits = 0,
  46. .mode = 0,
  47. .spics_io_num = CC1120_CS,
  48. .clock_speed_hz = SPI_MASTER_FREQ_8M,
  49. .flags = SPI_DEVICE_HALFDUPLEX,
  50. .queue_size = 1
  51. };
  52. spi_device_handle_t spi;
  53. // Private CC1120 Driver Functions
  54. esp_err_t cc1120_gpio_init(void)
  55. {
  56. gpio_config_t reset_pin_config =
  57. {
  58. .pin_bit_mask = (uint64_t)BIT64(CC1120_RESET),
  59. .mode = GPIO_MODE_OUTPUT,
  60. .pull_up_en = GPIO_PULLUP_DISABLE,
  61. .pull_down_en = GPIO_PULLDOWN_DISABLE,
  62. .intr_type = GPIO_INTR_DISABLE
  63. };
  64. gpio_config_t gpio_pin_config =
  65. {
  66. .pin_bit_mask = (uint64_t) (BIT64(CC1120_GPIO0)|BIT64(CC1120_GPIO2)|BIT64(CC1120_GPIO3)),
  67. .mode = GPIO_MODE_INPUT,
  68. .pull_up_en = GPIO_PULLUP_DISABLE,
  69. .pull_down_en = GPIO_PULLDOWN_DISABLE,
  70. .intr_type = GPIO_INTR_DISABLE
  71. };
  72. gpio_config(&reset_pin_config);
  73. gpio_config(&gpio_pin_config);
  74. gpio_set_level(CC1120_RESET, 1);
  75. return ESP_OK;
  76. }
  77. esp_err_t cc1120_spi_init(void)
  78. {
  79. esp_err_t ret;
  80. ret = spi_bus_initialize(VSPI_HOST, &bus_config, 1); // this uses DMA channel 1
  81. ESP_ERROR_CHECK(ret);
  82. ret = spi_bus_add_device(VSPI_HOST, &interface_config, &spi);
  83. ESP_ERROR_CHECK(ret);
  84. return ESP_OK;
  85. }
  86. esp_err_t cc1120_spi_write_byte(uint16_t addr, uint8_t data)
  87. {
  88. esp_err_t ret;
  89. spi_transaction_t tx_trans =
  90. {
  91. .flags = SPI_TRANS_USE_TXDATA,
  92. .cmd = CC1120_WRITE_BIT,
  93. .addr = addr,
  94. .length = 8,
  95. .rxlength = 0,
  96. .tx_data[0] = data
  97. };
  98. if ((addr & 0xFF00) != 0) // send data with extended address in command field
  99. {
  100. tx_trans.flags |= (SPI_TRANS_VARIABLE_CMD | SPI_TRANS_VARIABLE_ADDR);
  101. spi_transaction_ext_t tx_trans_ext =
  102. {
  103. .base = tx_trans,
  104. .command_bits = 2,
  105. .address_bits = 14
  106. };
  107. ret = spi_device_transmit(spi, (spi_transaction_t*)&tx_trans_ext);
  108. }
  109. else
  110. {
  111. ret = spi_device_transmit(spi, &tx_trans);
  112. }
  113. ESP_ERROR_CHECK(ret);
  114. return ESP_OK;
  115. }
  116. esp_err_t cc1120_spi_write_bytes(uint16_t addr, uint8_t* data, uint8_t len)
  117. {
  118. esp_err_t ret;
  119. spi_transaction_t tx_trans =
  120. {
  121. .cmd = (CC1120_WRITE_BIT | CC1120_BURST_BIT),
  122. .addr = addr,
  123. .length = 8*len,
  124. .tx_buffer = data
  125. };
  126. if ((addr & 0xFF00) != 0) // send data with extended address in command field
  127. {
  128. tx_trans.flags |= (SPI_TRANS_VARIABLE_CMD | SPI_TRANS_VARIABLE_ADDR);
  129. spi_transaction_ext_t tx_trans_ext =
  130. {
  131. .base = tx_trans,
  132. .command_bits = 2,
  133. .address_bits = 14
  134. };
  135. ret = spi_device_transmit(spi, (spi_transaction_t*)&tx_trans_ext);
  136. }
  137. else
  138. {
  139. ret = spi_device_transmit(spi, &tx_trans);
  140. }
  141. ESP_ERROR_CHECK(ret);
  142. return ESP_OK;
  143. }
  144. esp_err_t cc1120_spi_read_byte(uint16_t addr, uint8_t* data)
  145. {
  146. esp_err_t ret;
  147. spi_transaction_t rx_trans =
  148. {
  149. .flags = SPI_TRANS_USE_RXDATA,
  150. .cmd = CC1120_READ_BIT,
  151. .addr = addr,
  152. .length = 8,
  153. .rxlength = 8,
  154. };
  155. if ((addr & 0xFF00) != 0) // read data with extended address in command field
  156. {
  157. rx_trans.flags |= (SPI_TRANS_VARIABLE_CMD | SPI_TRANS_VARIABLE_ADDR);
  158. spi_transaction_ext_t rx_trans_ext =
  159. {
  160. .base = rx_trans,
  161. .command_bits = 2,
  162. .address_bits = 14
  163. };
  164. ret = spi_device_transmit(spi, (spi_transaction_t*)&rx_trans_ext);
  165. }
  166. else
  167. {
  168. ret = spi_device_transmit(spi, &rx_trans);
  169. }
  170. ESP_ERROR_CHECK(ret);
  171. *data = rx_trans.rx_data[0];
  172. return ESP_OK;
  173. }
  174. esp_err_t cc1120_spi_read_bytes(uint16_t addr, uint8_t* data, uint8_t len)
  175. {
  176. esp_err_t ret;
  177. spi_transaction_t rx_trans =
  178. {
  179. .cmd = (CC1120_READ_BIT | CC1120_BURST_BIT),
  180. .addr = addr,
  181. .length = 8*len,
  182. .rxlength = 8*len,
  183. .rx_buffer = data
  184. };
  185. if ((addr & 0xFF00) != 0) // read data with extended address in command field
  186. {
  187. rx_trans.flags |= (SPI_TRANS_VARIABLE_CMD | SPI_TRANS_VARIABLE_ADDR);
  188. spi_transaction_ext_t rx_trans_ext =
  189. {
  190. .base = rx_trans,
  191. .command_bits = 2,
  192. .address_bits = 14
  193. };
  194. ret = spi_device_transmit(spi, (spi_transaction_t*)&rx_trans_ext);
  195. }
  196. else
  197. {
  198. ret = spi_device_transmit(spi, &rx_trans);
  199. }
  200. ESP_ERROR_CHECK(ret);
  201. return ESP_OK;
  202. }
  203. esp_err_t cc1120_spi_strobe(uint8_t cmd)
  204. {
  205. esp_err_t ret;
  206. spi_transaction_t strobe_trans =
  207. {
  208. .cmd = CC1120_WRITE_BIT,
  209. .addr = cmd,
  210. .length = 0,
  211. };
  212. ret = spi_device_transmit(spi, &strobe_trans);
  213. ESP_ERROR_CHECK(ret);
  214. return ESP_OK;
  215. }
  216. // Public CC1120 Driver Functions
  217. // These function should have there own error codes as they're dependent upon the radio and
  218. // not the ESP32 :)
  219. esp_err_t cc1120_radio_reset(void)
  220. {
  221. return ESP_OK;
  222. }
  223. esp_err_t cc1120_radio_frequency(uint32_t freq)
  224. {
  225. return ESP_OK;
  226. }
  227. esp_err_t cc1120_radio_sleep(void)
  228. {
  229. return ESP_OK;
  230. }
  231. esp_err_t cc1120_radio_power(uint8_t txPower)
  232. {
  233. return ESP_OK;
  234. }
  235. esp_err_t cc1120_radio_init(void)
  236. {
  237. cc1120_gpio_init();
  238. cc1120_spi_init();
  239. return ESP_OK;
  240. }
  241. void app_main()
  242. {
  243. cc1120_radio_init();
  244. printf("Hello\n");
  245. uint8_t data;
  246. // function test loop
  247. while(1)
  248. {
  249. //cc1120_spi_write_byte(CC112X_PKT_CFG2, 0x0F);
  250. //cc1120_spi_write_byte(CC112X_FS_CAL0, 0x0F);
  251. cc1120_spi_read_byte(CC112X_PARTNUMBER, &data);
  252. cc1120_spi_strobe(CC112X_STX);
  253. vTaskDelay(30/portTICK_RATE_MS);
  254. cc1120_spi_strobe(CC112X_SRX);
  255. printf("data: %x\n", data);
  256. //esp_task_wdt_reset();
  257. vTaskDelay(1000/portTICK_RATE_MS);
  258. }
  259. }