qemu_test.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /* Copyright 2018 Espressif Systems (Shanghai) PTE LTD
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. #include "catch.hpp"
  16. #include "serial_io_mock.h"
  17. #include "esp_loader.h"
  18. #include "serial_io.h"
  19. #include <algorithm>
  20. #include <iostream>
  21. #include <fstream>
  22. using namespace std;
  23. #define ESP_ERR_CHECK(exp) REQUIRE( (exp) == ESP_LOADER_SUCCESS )
  24. const uint32_t APP_START_ADDRESS = 0x10000;
  25. TEST_CASE( "Can connect " )
  26. {
  27. esp_loader_connect_args_t connect_config = ESP_LOADER_CONNECT_DEFAULT();
  28. ESP_ERR_CHECK( esp_loader_connect(&connect_config) );
  29. REQUIRE( esp_loader_get_target() == ESP32_CHIP );
  30. }
  31. inline auto file_size_is(ifstream& file)
  32. {
  33. uint32_t file_size;
  34. file.seekg(0, ios::end);
  35. file_size = file.tellg();
  36. file.seekg(0);
  37. return file_size;
  38. }
  39. void flash_application(ifstream& image)
  40. {
  41. uint8_t payload[1024];
  42. int32_t count = 0;
  43. size_t image_size = file_size_is(image);
  44. ESP_ERR_CHECK( esp_loader_flash_start(APP_START_ADDRESS, image_size, sizeof(payload)) );
  45. while(image_size > 0) {
  46. size_t to_read = min(image_size, sizeof(payload));
  47. image.read((char *)payload, to_read);
  48. ESP_ERR_CHECK( esp_loader_flash_write(payload, to_read) );
  49. cout << "packet: " << count++ << " written: " << to_read << endl;
  50. image_size -= to_read;
  51. };
  52. // Omit restart
  53. // loader_flash_finish(false);
  54. }
  55. bool file_compare(ifstream& file_1, ifstream& file_2, size_t file_size)
  56. {
  57. vector<char> file_data_1(file_size);
  58. vector<char> file_data_2(file_size);
  59. file_1.read((char*) &file_data_1[0], file_size);
  60. file_2.read((char*) &file_data_2[0], file_size);
  61. return file_data_1 == file_data_2;
  62. }
  63. TEST_CASE( "Can write application to flash" )
  64. {
  65. ifstream new_image;
  66. ifstream qemu_image;
  67. new_image.open ("../hello-world.bin", ios::binary | ios::in);
  68. qemu_image.open ("../empty_file.bin", ios::binary | ios::in);
  69. REQUIRE ( new_image.is_open() );
  70. REQUIRE ( qemu_image.is_open() );
  71. flash_application(new_image);
  72. auto new_image_size = file_size_is(new_image);
  73. qemu_image.seekg(APP_START_ADDRESS);
  74. new_image.seekg(0);
  75. REQUIRE ( file_compare(new_image, qemu_image, new_image_size) );
  76. ESP_ERR_CHECK ( esp_loader_flash_verify() );
  77. // NOTE: loader_flash_finish() is not called to prevent reset of target
  78. }
  79. TEST_CASE( "Can write and read register" )
  80. {
  81. uint32_t reg_value = 0;
  82. uint32_t SPI_MOSI_DLEN_REG = 0x60002000 + 0x28;
  83. ESP_ERR_CHECK( esp_loader_write_register(SPI_MOSI_DLEN_REG, 55) );
  84. ESP_ERR_CHECK( esp_loader_read_register(SPI_MOSI_DLEN_REG, &reg_value) );
  85. REQUIRE ( reg_value == 55 );
  86. }