app_main.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. /* SDIO example, host (uses sdmmc_host/sdspi_host driver)
  2. This example code is in the Public Domain (or CC0 licensed, at your option.)
  3. Unless required by applicable law or agreed to in writing, this
  4. software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  5. CONDITIONS OF ANY KIND, either express or implied.
  6. */
  7. #include <stdio.h>
  8. #include <stdint.h>
  9. #include <stddef.h>
  10. #include <string.h>
  11. #include "freertos/FreeRTOS.h"
  12. #include "freertos/task.h"
  13. #include "freertos/semphr.h"
  14. #include "freertos/queue.h"
  15. #include "soc/sdmmc_periph.h"
  16. #include "soc/sdio_slave_periph.h"
  17. #include "esp_log.h"
  18. #include "esp_attr.h"
  19. #include "esp_serial_slave_link/essl_sdio.h"
  20. #include "sdkconfig.h"
  21. #include "driver/sdmmc_host.h"
  22. #include "driver/sdspi_host.h"
  23. #define TIMEOUT_MAX UINT32_MAX
  24. #define GPIO_B1 21
  25. #if CONFIG_EXAMPLE_SLAVE_B1
  26. #define SLAVE_PWR_GPIO GPIO_B1
  27. #endif
  28. /*
  29. sdio host example.
  30. This example is supposed to work together with the sdio slave example. It uses the pins as follows:
  31. * Host Slave
  32. * IO14 CLK
  33. * IO15 CMD
  34. * IO2 D0
  35. * IO4 D1
  36. * IO12 D2
  37. * IO13 D3
  38. This is the only pins that can be used in standard ESP modules. The other set of pins (6, 11, 7, 8, 9, 10)
  39. are occupied by the spi bus communicating with the flash.
  40. Protocol Above the ESP slave service:
  41. - Interrupts:
  42. 0 is used to notify the slave to read the register 0.
  43. - Registers:
  44. - 0 is the register to hold tasks. Bits:
  45. - 0: the slave should reset.
  46. - 1: the slave should send interrupts.
  47. - 2: the slave should write the shared registers acoording to the value in register 1.
  48. - 1 is the register to hold test value.
  49. - other registers will be written by the slave for testing.
  50. - FIFO:
  51. The receving FIFO is size of 256 bytes.
  52. When the host writes something to slave recv FIFO, the slave should return it as is to the sending FIFO.
  53. The example works as following process:
  54. 1. reset the slave.
  55. 2. tell the slave to write registers and read them back.
  56. 3. tell the slave to send interrupts to the host.
  57. 4. send data to slave FIFO and read them back.
  58. 5. loop step 4.
  59. */
  60. #define WRITE_BUFFER_LEN 4096
  61. #define READ_BUFFER_LEN 4096
  62. #define SLAVE_BUFFER_SIZE 128
  63. static const char TAG[] = "example_host";
  64. #define SDIO_INTERRUPT_LINE GPIO_NUM_4 //DATA1
  65. #define SLAVE_INTR_NOTIFY 0
  66. #define SLAVE_REG_JOB 0
  67. #define SLAVE_REG_VALUE 1
  68. typedef enum {
  69. JOB_IDLE = 0,
  70. JOB_RESET = 1,
  71. JOB_SEND_INT = 2,
  72. JOB_WRITE_REG = 4,
  73. } example_job_t;
  74. //host use this to inform the slave it should reset its counters
  75. esp_err_t slave_reset(essl_handle_t handle)
  76. {
  77. esp_err_t ret;
  78. ESP_LOGI(TAG, "send reset to slave...");
  79. ret = essl_write_reg(handle, 0, JOB_RESET, NULL, TIMEOUT_MAX);
  80. if (ret != ESP_OK) {
  81. return ret;
  82. }
  83. ret = essl_send_slave_intr(handle, BIT(SLAVE_INTR_NOTIFY), TIMEOUT_MAX);
  84. if (ret != ESP_OK) {
  85. return ret;
  86. }
  87. vTaskDelay(500 / portTICK_PERIOD_MS);
  88. ret = essl_wait_for_ready(handle, TIMEOUT_MAX);
  89. ESP_LOGI(TAG, "slave io ready");
  90. return ret;
  91. }
  92. #ifdef CONFIG_EXAMPLE_SDIO_OVER_SPI
  93. static void gpio_d2_set_high(void)
  94. {
  95. gpio_config_t d2_config = {
  96. .pin_bit_mask = BIT64(SDIO_SLAVE_SLOT1_IOMUX_PIN_NUM_D2),
  97. .mode = GPIO_MODE_OUTPUT,
  98. .pull_up_en = true,
  99. };
  100. gpio_config(&d2_config);
  101. gpio_set_level(SDIO_SLAVE_SLOT1_IOMUX_PIN_NUM_D2, 1);
  102. }
  103. #endif
  104. static esp_err_t print_sdio_cis_information(sdmmc_card_t* card)
  105. {
  106. const size_t cis_buffer_size = 256;
  107. uint8_t cis_buffer[cis_buffer_size];
  108. size_t cis_data_len = 1024; //specify maximum searching range to avoid infinite loop
  109. esp_err_t ret = ESP_OK;
  110. ret = sdmmc_io_get_cis_data(card, cis_buffer, cis_buffer_size, &cis_data_len);
  111. if (ret == ESP_ERR_INVALID_SIZE) {
  112. int temp_buf_size = cis_data_len;
  113. uint8_t* temp_buf = malloc(temp_buf_size);
  114. assert(temp_buf);
  115. ESP_LOGW(TAG, "CIS data longer than expected, temporary buffer allocated.");
  116. ret = sdmmc_io_get_cis_data(card, temp_buf, temp_buf_size, &cis_data_len);
  117. ESP_ERROR_CHECK(ret);
  118. sdmmc_io_print_cis_info(temp_buf, cis_data_len, NULL);
  119. free(temp_buf);
  120. } else if (ret == ESP_OK) {
  121. sdmmc_io_print_cis_info(cis_buffer, cis_data_len, NULL);
  122. } else {
  123. //including ESP_ERR_NOT_FOUND
  124. ESP_LOGE(TAG, "failed to get the entire CIS data.");
  125. abort();
  126. }
  127. return ESP_OK;
  128. }
  129. //host use this to initialize the slave card as well as SDIO registers
  130. esp_err_t slave_init(essl_handle_t* handle)
  131. {
  132. esp_err_t err;
  133. /* Probe */
  134. #ifndef CONFIG_EXAMPLE_SDIO_OVER_SPI
  135. sdmmc_host_t config = SDMMC_HOST_DEFAULT();
  136. #ifdef CONFIG_EXAMPLE_SDIO_4BIT
  137. ESP_LOGI(TAG, "Probe using SD 4-bit...\n");
  138. config.flags = SDMMC_HOST_FLAG_4BIT;
  139. #else
  140. ESP_LOGI(TAG, "Probe using SD 1-bit...\n");
  141. config.flags = SDMMC_HOST_FLAG_1BIT;
  142. #endif
  143. #ifdef CONFIG_EXAMPLE_SDIO_HIGHSPEED
  144. config.max_freq_khz = SDMMC_FREQ_HIGHSPEED;
  145. #else
  146. config.max_freq_khz = SDMMC_FREQ_DEFAULT;
  147. #endif
  148. sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
  149. /* Note: For small devkits there may be no pullups on the board.
  150. This enables the internal pullups to help evaluate the driver quickly.
  151. However the internal pullups are not sufficient and not reliable,
  152. please make sure external pullups are connected to the bus in your
  153. real design.
  154. */
  155. //slot_config.flags = SDMMC_SLOT_FLAG_INTERNAL_PULLUP;
  156. err = sdmmc_host_init();
  157. ESP_ERROR_CHECK(err);
  158. err = sdmmc_host_init_slot(SDMMC_HOST_SLOT_1, &slot_config);
  159. ESP_ERROR_CHECK(err);
  160. #else //over SPI
  161. sdspi_device_config_t dev_config = SDSPI_DEVICE_CONFIG_DEFAULT();
  162. dev_config.gpio_cs = SDIO_SLAVE_SLOT1_IOMUX_PIN_NUM_D3;
  163. dev_config.gpio_int = SDIO_SLAVE_SLOT1_IOMUX_PIN_NUM_D1;
  164. err = gpio_install_isr_service(0);
  165. ESP_ERROR_CHECK(err);
  166. spi_bus_config_t bus_config = {
  167. .mosi_io_num = SDIO_SLAVE_SLOT1_IOMUX_PIN_NUM_CMD,
  168. .miso_io_num = SDIO_SLAVE_SLOT1_IOMUX_PIN_NUM_D0,
  169. .sclk_io_num = SDIO_SLAVE_SLOT1_IOMUX_PIN_NUM_CLK,
  170. .quadwp_io_num = -1,
  171. .quadhd_io_num = -1,
  172. .max_transfer_sz = 4000,
  173. };
  174. err = spi_bus_initialize(dev_config.host_id, &bus_config, 1);
  175. ESP_ERROR_CHECK(err);
  176. sdspi_device_handle_t sdspi_handle;
  177. err = sdspi_host_init();
  178. ESP_ERROR_CHECK(err);
  179. err = sdspi_host_init_device(&slot_config, &sdspi_handle);
  180. ESP_ERROR_CHECK(err);
  181. ESP_LOGI(TAG, "Probe using SPI...\n");
  182. sdmmc_host_t config = SDSPI_HOST_DEFAULT();
  183. config.slot = sdspi_handle;
  184. //we have to pull up all the slave pins even when the pin is not used
  185. gpio_d2_set_high();
  186. #endif //over SPI
  187. sdmmc_card_t *card = (sdmmc_card_t *)malloc(sizeof(sdmmc_card_t));
  188. if (card == NULL) {
  189. return ESP_ERR_NO_MEM;
  190. }
  191. for (;;) {
  192. if (sdmmc_card_init(&config, card) == ESP_OK) {
  193. break;
  194. }
  195. ESP_LOGW(TAG, "slave init failed, retry...");
  196. vTaskDelay(1000 / portTICK_PERIOD_MS);
  197. }
  198. sdmmc_card_print_info(stdout, card);
  199. gpio_pullup_en(14);
  200. gpio_pulldown_dis(14);
  201. gpio_pullup_en(15);
  202. gpio_pulldown_dis(15);
  203. gpio_pullup_en(2);
  204. gpio_pulldown_dis(2);
  205. gpio_pullup_en(4);
  206. gpio_pulldown_dis(4);
  207. gpio_pullup_en(12);
  208. gpio_pulldown_dis(12);
  209. gpio_pullup_en(13);
  210. gpio_pulldown_dis(13);
  211. essl_sdio_config_t ser_config = {
  212. .card = card,
  213. .recv_buffer_size = SLAVE_BUFFER_SIZE,
  214. };
  215. err = essl_sdio_init_dev(handle, &ser_config);
  216. ESP_ERROR_CHECK(err);
  217. esp_err_t ret = essl_init(*handle, TIMEOUT_MAX);
  218. ESP_ERROR_CHECK(ret);
  219. ret = print_sdio_cis_information(card);
  220. ESP_ERROR_CHECK(ret);
  221. return ret;
  222. }
  223. void slave_power_on(void)
  224. {
  225. #ifdef SLAVE_PWR_GPIO
  226. int level_active;
  227. #ifdef CONFIG_EXAMPLE_SLAVE_PWR_NEGTIVE_ACTIVE
  228. level_active = 0;
  229. #else
  230. level_active = 1;
  231. #endif
  232. gpio_config_t cfg = {
  233. .pin_bit_mask = BIT64(GPIO_B1),
  234. .mode = GPIO_MODE_OUTPUT,
  235. .pull_up_en = false,
  236. .pull_down_en = false,
  237. .intr_type = GPIO_INTR_DISABLE,
  238. };
  239. gpio_config(&cfg);
  240. gpio_set_level(GPIO_B1, !level_active);
  241. vTaskDelay(100);
  242. gpio_set_level(SLAVE_PWR_GPIO, level_active);
  243. vTaskDelay(100);
  244. #endif
  245. }
  246. DMA_ATTR uint8_t rcv_buffer[READ_BUFFER_LEN];
  247. static esp_err_t get_intr(essl_handle_t handle, uint32_t* out_raw, uint32_t* out_st)
  248. {
  249. esp_err_t ret = ESP_OK;
  250. #ifndef CONFIG_EXAMPLE_NO_INTR_LINE
  251. ret = essl_wait_int(handle, 0);
  252. if (ret != ESP_OK) {
  253. return ret;
  254. }
  255. #endif
  256. ret = essl_get_intr(handle, out_raw, out_st, TIMEOUT_MAX);
  257. if (ret != ESP_OK) return ret;
  258. ret = essl_clear_intr(handle, *out_raw, TIMEOUT_MAX);
  259. if (ret != ESP_OK) return ret;
  260. ESP_LOGD(TAG, "intr: %08"PRIX32, *out_raw);
  261. return ESP_OK;
  262. }
  263. //try to get an interrupt from the slave and handle it, return if none.
  264. esp_err_t process_event(essl_handle_t handle)
  265. {
  266. uint32_t intr_raw, intr_st;
  267. esp_err_t ret = get_intr(handle, &intr_raw, &intr_st);
  268. if (ret == ESP_ERR_TIMEOUT) {
  269. return ret;
  270. }
  271. ESP_ERROR_CHECK(ret);
  272. for (int i = 0; i < 8; i++) {
  273. if (intr_raw & BIT(i)) {
  274. ESP_LOGI(TAG, "host int: %d", i);
  275. }
  276. }
  277. const int wait_ms = 50;
  278. if (intr_raw & HOST_SLC0_RX_NEW_PACKET_INT_ST) {
  279. ESP_LOGD(TAG, "new packet coming");
  280. while (1) {
  281. size_t size_read = READ_BUFFER_LEN;
  282. ret = essl_get_packet(handle, rcv_buffer, READ_BUFFER_LEN, &size_read, wait_ms);
  283. if (ret == ESP_ERR_NOT_FOUND) {
  284. ESP_LOGE(TAG, "interrupt but no data can be read");
  285. break;
  286. } else if (ret != ESP_OK && ret != ESP_ERR_NOT_FINISHED) {
  287. ESP_LOGE(TAG, "rx packet error: %08X", ret);
  288. return ret;
  289. }
  290. ESP_LOGI(TAG, "receive data, size: %d", size_read);
  291. ESP_LOG_BUFFER_HEXDUMP(TAG, rcv_buffer, size_read, ESP_LOG_INFO);
  292. if (ret == ESP_OK) {
  293. break;
  294. }
  295. }
  296. }
  297. return ESP_OK;
  298. }
  299. //tell the slave to do a job
  300. static inline esp_err_t slave_inform_job(essl_handle_t handle, example_job_t job)
  301. {
  302. esp_err_t ret;
  303. ret = essl_write_reg(handle, SLAVE_REG_JOB, job, NULL, TIMEOUT_MAX);
  304. ESP_ERROR_CHECK(ret);
  305. ret = essl_send_slave_intr(handle, BIT(SLAVE_INTR_NOTIFY), TIMEOUT_MAX);
  306. ESP_ERROR_CHECK(ret);
  307. return ret;
  308. }
  309. //tell the slave to write registers by write one of them, and read them back
  310. void job_write_reg(essl_handle_t handle, int value)
  311. {
  312. esp_err_t ret;
  313. uint8_t reg_read[60];
  314. ESP_LOGI(TAG, "========JOB: write slave reg========");
  315. ret = essl_write_reg(handle, SLAVE_REG_VALUE, value, NULL, TIMEOUT_MAX);
  316. ESP_ERROR_CHECK(ret);
  317. ret = slave_inform_job(handle, JOB_WRITE_REG);
  318. ESP_ERROR_CHECK(ret);
  319. vTaskDelay(10);
  320. for (int i = 0; i < 60; i++) {
  321. ESP_LOGD(TAG, "reading register %d", i);
  322. ret = essl_read_reg(handle, i, &reg_read[i], TIMEOUT_MAX);
  323. ESP_ERROR_CHECK(ret);
  324. }
  325. ESP_LOGI(TAG, "read registers:");
  326. ESP_LOG_BUFFER_HEXDUMP(TAG, reg_read, 64, ESP_LOG_INFO);
  327. }
  328. //the slave only load 16 buffers a time
  329. //so first 5 packets (use 1+1+8+4+1=15 buffers) are sent, the others (513, 517) failed (timeout)
  330. int packet_len[] = {6, 12, 1024, 512, 3, 513, 517};
  331. //the sending buffer should be word aligned
  332. DMA_ATTR uint8_t send_buffer[READ_BUFFER_LEN];
  333. //send packets to the slave (they will return and be handled by the interrupt handler)
  334. void job_fifo(essl_handle_t handle)
  335. {
  336. for (int i = 0; i < READ_BUFFER_LEN; i++) {
  337. send_buffer[i] = 0x46 + i * 5;
  338. }
  339. esp_err_t ret;
  340. int pointer = 0;
  341. ESP_LOGI(TAG, "========JOB: send fifos========");
  342. /* CAUTION: This example shows that we can send random length of packet to the slave.
  343. * However it takes time of two transactions if the length is not multiples of 4 bytes.
  344. * e.g. sending 6 bytes is done by sending 4 + 2 bytes each transaction.
  345. * Try to avoid unaligned packets if possible to get higher effeciency.
  346. */
  347. for (int i = 0; i < sizeof(packet_len) / sizeof(int); i++) {
  348. const int wait_ms = 50;
  349. int length = packet_len[i];
  350. ret = essl_send_packet(handle, send_buffer + pointer, length, wait_ms);
  351. if (ret == ESP_ERR_TIMEOUT) {
  352. ESP_LOGD(TAG, "several packets are expected to timeout.");
  353. } else {
  354. ESP_ERROR_CHECK(ret);
  355. ESP_LOGI(TAG, "send packet length: %d", length);
  356. }
  357. pointer += (length + 3) & (~3); //the length can be random, but data should start at the 32-bit boundary.
  358. }
  359. }
  360. //inform the slave to send interrupts to host (the interrupts will be handled in the interrupt handler)
  361. void job_getint(essl_handle_t handle)
  362. {
  363. ESP_LOGI(TAG, "========JOB: get interrupts from slave========");
  364. slave_inform_job(handle, JOB_SEND_INT);
  365. }
  366. void app_main(void)
  367. {
  368. essl_handle_t handle;
  369. esp_err_t err;
  370. //enable the power if on espressif SDIO master-slave board
  371. slave_power_on();
  372. ESP_LOGI(TAG, "host ready, start initializing slave...");
  373. err = slave_init(&handle);
  374. ESP_ERROR_CHECK(err);
  375. err = slave_reset(handle);
  376. ESP_ERROR_CHECK(err);
  377. uint32_t start, end;
  378. job_write_reg(handle, 10);
  379. int times = 2;
  380. while (1) {
  381. job_getint(handle);
  382. start = xTaskGetTickCount();
  383. while (1) {
  384. process_event(handle);
  385. vTaskDelay(1);
  386. end = xTaskGetTickCount();
  387. if ((end - start) * 1000 / CONFIG_FREERTOS_HZ > 5000) {
  388. break;
  389. }
  390. }
  391. if (--times == 0) {
  392. break;
  393. }
  394. };
  395. while (1) {
  396. job_fifo(handle);
  397. start = xTaskGetTickCount();
  398. while (1) {
  399. process_event(handle);
  400. vTaskDelay(1);
  401. end = xTaskGetTickCount();
  402. if ((end - start) * 1000 / CONFIG_FREERTOS_HZ > 2000) {
  403. break;
  404. }
  405. }
  406. }
  407. }