Issue of changing the value of variables/flags automatically

    This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.

    • Issue of changing the value of variables/flags automatically

      Hi,

      I am facing two issues in the below code. I use segger embedded studio.
      1.The value I am setting as fs_initialized = true in line number 90 (inside FRESULT fs_init() function )is changing to fs_initialized = false after the while loop or line number 143 in void sync_files(bool delete_all) function.
      2. The file_index is getting reset to value 77 after it reaches the maximum value, which is 400. So it is not coming out of the loop. But this issue can be solved if I set the optimization level to any option other than 'none'.
      I think this is because of the variable initialization issue or project settings. Please help.

      1st issue happens only if file_index reaches the value 400 in the while loop in void sync_files(bool delete_all) functions (it indicates that the NAND flash memory is full.


      C Source Code

      1. /* Includes ------------------------------------------------------------------*/
      2. #include "ffs.h"
      3. #include "nrf_log.h"
      4. #include "nrf_log_ctrl.h"
      5. #include "nrf_log_default_backends.h"
      6. #include "nand_cmds.h"
      7. #include "nand_disk.h"
      8. /* Private typedef -----------------------------------------------------------*/
      9. /* Private define ------------------------------------------------------------*/
      10. #define FFS_BUFFER_SIZE 4096
      11. #define FILE_DISCARD_SIZE 20000 /*********For test only, change 20000 for actual****/
      12. #define FILE_HEADER_SIZE 10
      13. #define ACTIVITY_RECORD_SIZE 5
      14. #define LEAD_STATUS_RECORD_SIZE 6
      15. #define ONE_HOUR 3600// 3600 /*********For test only, change 3600 for actual****/
      16. /* Private macro -------------------------------------------------------------*/
      17. /* Private variables ---------------------------------------------------------*/
      18. file_data_t files[MAX_FILES];
      19. uint16_t file_index=0;
      20. NAND_CHIP toshiba;
      21. struct dhara_nand dhara_toshiba;
      22. dhara_error_t err;
      23. time_t file_open_time = 0;
      24. FATFS nand_file_system;
      25. BYTE work[_MAX_SS];
      26. volatile bool fs_initialized = false;
      27. FIL read_file, write_file;
      28. bool write_file_valid = true, read_file_valid = true;
      29. //FIL *read_file_ptr , *write_file_ptr;
      30. uint8_t file_write_buffer[FFS_BUFFER_SIZE];
      31. volatile uint16_t file_read_buffer_index = 0 , file_write_buffer_index = 0;
      32. bool fs_files_full = false;
      33. uint16_t read_file_index = 0;
      34. bool fs_error_debug = false;
      35. /* Private function prototypes -----------------------------------------------*/
      36. FRESULT write_buffer_content();
      37. void fs_data_init();
      38. void sync_files(bool delete_all);
      39. FRESULT open_next_write_file();
      40. void prepare_file_header(uint8_t *buff);
      41. record_file_status_t open_next_file_read();
      42. void RECORD_ERROR_CHECK(record_file_status_t record_status);
      43. /* Private functions ---------------------------------------------------------*/
      44. /*
      45. NAND Flash initialization
      46. */
      47. FRESULT fs_init()
      48. {
      49. FRESULT ff_result;
      50. NRF_LOG_INFO("File System Init");
      51. spi_flash_init();
      52. nand_reset(&toshiba);
      53. DSTATUS disk_state = STA_NOINIT;
      54. nrf_delay_ms(1);
      55. fs_initialized = false;
      56. for (uint32_t retries = 3; retries && disk_state; --retries)
      57. {
      58. disk_state = disk_initialize(DEV_NAND);
      59. }
      60. if(disk_state)
      61. {
      62. NRF_LOG_ERROR("Disk Initialization failed");
      63. fs_initialized = false;
      64. return FR_DISK_ERR;
      65. }
      66. NRF_LOG_INFO("Disk Initalized");
      67. ff_result = f_mount(&nand_file_system, "", 0);
      68. if(ff_result != FR_OK && ff_result != FR_NO_FILESYSTEM)
      69. {
      70. fs_initialized = false;
      71. return ff_result;
      72. }
      73. if(ff_result == FR_NO_FILESYSTEM)
      74. {
      75. ff_result = f_mkfs("",FM_ANY,0,work,sizeof(work));
      76. if(ff_result != FR_OK)
      77. {
      78. NRF_LOG_ERROR("Make FS Fail");
      79. fs_initialized = false;
      80. return ff_result;
      81. }
      82. }
      83. fs_initialized = true;
      84. fs_data_reset(false);
      85. return FR_OK;
      86. }
      87. void fs_data_reset(bool clean)
      88. {
      89. write_file_valid = false;
      90. read_file_valid = false;
      91. memset(&files, 0, sizeof(files));
      92. device_flags.flash_storage_full = false;
      93. file_index = MIN_FILE_INDEX;
      94. sync_files(clean);
      95. }
      96. void sync_files(bool delete_all)
      97. {
      98. NRF_LOG_INFO("File sync start");
      99. FRESULT ff_result, ff_res;
      100. FILINFO fno;
      101. uint8_t file_name[10];
      102. uint16_t file_num;
      103. bool empty_file_found = false;
      104. uint16_t empty_file_index = 0;
      105. do
      106. {
      107. sprintf(file_name,"%d.txt",file_index);
      108. ff_result = f_stat(file_name, &fno);
      109. switch(ff_result)
      110. {
      111. case FR_OK : files[file_index].file_size = fno.fsize;
      112. NRF_LOG_DEBUG("%s Exists Size %d", file_name, fno.fsize);
      113. if(delete_all || empty_file_found)
      114. {
      115. NRF_LOG_INFO("Deleting");
      116. ff_res = f_unlink(file_name);
      117. }
      118. break;
      119. case FR_NO_FILE : if(!empty_file_found)
      120. {
      121. empty_file_index = file_index;
      122. empty_file_found = true;
      123. NRF_LOG_INFO("Empty File at %d",empty_file_index);
      124. }
      125. break;
      126. default : FF_ERROR_CHECK(ff_result);
      127. NRF_LOG_INFO("FF error check");
      128. break;
      129. }
      130. }while(file_index++ < MAX_FILES && empty_file_found==false);
      131. if(!empty_file_found)
      132. {
      133. device_flags.flash_storage_full = true;
      134. NRF_LOG_INFO("All files full!! No write possible");
      135. return;
      136. }
      137. if(delete_all || empty_file_index == 0)
      138. file_index = 0;
      139. else
      140. file_index = empty_file_index - 1;
      141. NRF_LOG_INFO("Start Write at index %d",file_index + 1);
      142. }
      143. void FF_ERROR_CHECK(FRESULT ff_result)
      144. {
      145. if(ff_result == FR_OK)
      146. return;
      147. switch(ff_result)
      148. {
      149. case FR_DISK_ERR : NRF_LOG_ERROR("FS Disk Error");
      150. break;
      151. case FR_INT_ERR : NRF_LOG_ERROR("FS Assertion Failed");
      152. break;
      153. case FR_NOT_READY : NRF_LOG_ERROR("FS Drive Not ready");
      154. break;
      155. case FR_NO_FILE : NRF_LOG_ERROR("File not found");
      156. break;
      157. case FR_NO_PATH : NRF_LOG_ERROR("Path not found");
      158. break;
      159. case FR_INVALID_NAME : NRF_LOG_ERROR("Path invalid");
      160. break;
      161. case FR_DENIED : NRF_LOG_ERROR("File Access Denied");
      162. break;
      163. case FR_EXIST : NRF_LOG_ERROR("File Exists");
      164. break;
      165. case FR_INVALID_OBJECT : NRF_LOG_ERROR("FS Invalid object");
      166. break;
      167. case FR_WRITE_PROTECTED : NRF_LOG_ERROR("Write protected");
      168. break;
      169. case FR_INVALID_DRIVE : NRF_LOG_ERROR("Invalid Drive");
      170. break;
      171. case FR_NOT_ENABLED : NRF_LOG_ERROR("FS No work area");
      172. break;
      173. case FR_NO_FILESYSTEM : NRF_LOG_ERROR("No File System");
      174. break;
      175. case FR_MKFS_ABORTED : NRF_LOG_ERROR("MKFS Aborted");
      176. break;
      177. case FR_TIMEOUT : NRF_LOG_ERROR("Disk timeout");
      178. break;
      179. case FR_LOCKED : NRF_LOG_ERROR("File locked");
      180. break;
      181. case FR_NOT_ENOUGH_CORE : NRF_LOG_ERROR("FS working buffer issue");
      182. break;
      183. case FR_TOO_MANY_OPEN_FILES : NRF_LOG_ERROR("Too many open files");
      184. break;
      185. case FR_INVALID_PARAMETER : NRF_LOG_ERROR("FS Invalid Param");
      186. break;
      187. default : NRF_LOG_ERROR("FS Error Unknown");
      188. break;
      189. }
      190. }
      191. void RECORD_ERROR_CHECK(record_file_status_t record_status)
      192. {
      193. if(record_status == RECORD_OK)
      194. return;
      195. switch(record_status)
      196. {
      197. case FS_NO_INIT : NRF_LOG_INFO("FS Not Init");
      198. break;
      199. case END_OF_RECORDS : NRF_LOG_INFO("End of Records");
      200. break;
      201. case RECORD_FS_ERROR : NRF_LOG_INFO("File System Error");
      202. break;
      203. default : NRF_LOG_INFO("Unknown record status");
      204. break;
      205. }
      206. }
      207. uint32_t get_total_file_size(uint32_t *num_files)
      208. {
      209. uint32_t total_file_size = 0,temp_size;
      210. *num_files=0;
      211. for(int i = 1; i < MAX_FILES; i++)
      212. {
      213. temp_size = files[i].file_size;
      214. if(temp_size > FILE_DISCARD_SIZE)
      215. {
      216. total_file_size = total_file_size + temp_size;
      217. // *num_files++;
      218. *num_files = *num_files + 1;
      219. }
      220. }
      221. return total_file_size;
      222. }
      Display All
      Please help.

      Regards,
      Vishnu Pradeep
    • New

      Hello,

      Thank you for your inquiry.
      The reported behaviour does not seem to be related to issues in SEGGER software. So please understand that we can't put much time into this request as it is user responsibility to debug and write user code.

      But as a general tip make sure all code optimizations are disabled and make sure that variables are defined in a way that the compiler may not optimize variables away. How to do this can be found in numerous online resources.
      For debugging this we recommend using breakpoints and data breakpoints to see when exactly the variable of interest is changing so you can see if the behaviour is expected.

      If you suspect an issue with the Nordic SDK source we recommend contacting Nordic support.

      Best regards,
      Nino
      Please read the forum rules before posting: Forum Rules

      Keep in mind, this is not a support forum. Its main purpose is user to user interaction.
      Our engineers will try to answer your questions between their projects if possible but this can be delayed by longer periods of time.
      Should you be entitled to support you can contact us via our support system: segger.com/ticket/

      Or you can contact us via e-mail.