qemu_test.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. }
  30. inline auto file_size_is(ifstream& file)
  31. {
  32. uint32_t file_size;
  33. file.seekg(0, ios::end);
  34. file_size = file.tellg();
  35. file.seekg(0);
  36. return file_size;
  37. }
  38. void flash_application(ifstream& image)
  39. {
  40. uint8_t payload[1024];
  41. int32_t count = 0;
  42. size_t image_size = file_size_is(image);
  43. ESP_ERR_CHECK( esp_loader_flash_start(APP_START_ADDRESS, image_size, sizeof(payload)) );
  44. while(image_size > 0) {
  45. size_t to_read = min(image_size, sizeof(payload));
  46. image.read((char *)payload, to_read);
  47. ESP_ERR_CHECK( esp_loader_flash_write(payload, to_read) );
  48. cout << "packet: " << count++ << " written: " << to_read << endl;
  49. image_size -= to_read;
  50. };
  51. // Omit restart
  52. // loader_flash_finish(false);
  53. }
  54. bool file_compare(ifstream& file_1, ifstream& file_2, size_t file_size)
  55. {
  56. vector<char> file_data_1(file_size);
  57. vector<char> file_data_2(file_size);
  58. file_1.read((char*) &file_data_1[0], file_size);
  59. file_2.read((char*) &file_data_2[0], file_size);
  60. return file_data_1 == file_data_2;
  61. }
  62. TEST_CASE( "Can write application to flash" )
  63. {
  64. ifstream new_image;
  65. ifstream qemu_image;
  66. new_image.open ("../hello-world.bin", ios::binary | ios::in);
  67. qemu_image.open ("../empty_file.bin", ios::binary | ios::in);
  68. REQUIRE ( new_image.is_open() );
  69. REQUIRE ( qemu_image.is_open() );
  70. flash_application(new_image);
  71. auto new_image_size = file_size_is(new_image);
  72. qemu_image.seekg(APP_START_ADDRESS);
  73. new_image.seekg(0);
  74. REQUIRE ( file_compare(new_image, qemu_image, new_image_size) );
  75. // NOTE: loader_flash_finish() is not called to prevent reset of target
  76. }
  77. TEST_CASE( "Can write and read register" )
  78. {
  79. uint32_t reg_value = 0;
  80. uint32_t SPI_MOSI_DLEN_REG = 0x60002000 + 0x28;
  81. ESP_ERR_CHECK( esp_loader_write_register(SPI_MOSI_DLEN_REG, 55) );
  82. ESP_ERR_CHECK( esp_loader_read_register(SPI_MOSI_DLEN_REG, &reg_value) );
  83. REQUIRE ( reg_value == 55 );
  84. }