psramstream.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * Copyright (c) 2019 Actions Semiconductor Co., Ltd
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /**
  7. * @file psram stream interface
  8. */
  9. #include <mem_manager.h>
  10. #include <string.h>
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. #include <assert.h>
  14. #include "psram_stream.h"
  15. #include "stream_internal.h"
  16. /** cace info ,used for cache stream */
  17. typedef struct
  18. {
  19. /** hanlde of file fp*/
  20. int base_off;
  21. /** size of cache file */
  22. int size ;
  23. /** mutex used for sync*/
  24. os_mutex lock;
  25. } cache_info_t;
  26. int psram_stream_open(io_stream_t handle,stream_mode mode)
  27. {
  28. handle->mode = mode;
  29. SYS_LOG_DBG("mode %d \n",mode);
  30. return 0;
  31. }
  32. int psram_stream_read(io_stream_t handle, unsigned char *buf,int num)
  33. {
  34. int brw;
  35. int read_len = 0;
  36. int file_off = 0;
  37. cache_info_t *info = (cache_info_t *)handle->data;
  38. assert(info);
  39. brw = os_mutex_lock(&info->lock, OS_FOREVER);
  40. if (brw < 0) {
  41. SYS_LOG_ERR("lock failed %d \n",res);
  42. return brw;
  43. }
  44. file_off = handle->rofs % info->size;
  45. if (file_off + num > info->size) {
  46. brw = info->size - file_off;
  47. psram_read(info->base_off + file_off, buf, brw);
  48. num -= brw;
  49. handle->rofs += brw;
  50. read_len += brw;
  51. file_off = handle->rofs % info->size;
  52. }
  53. psram_read(info->base_off + file_off,
  54. buf + read_len , num);
  55. handle->rofs += num;
  56. read_len += num;
  57. err_out:
  58. os_mutex_unlock(&info->lock);
  59. return read_len;
  60. }
  61. int psram_stream_write(io_stream_t handle, unsigned char *buf,int num)
  62. {
  63. uint32_t brw = 0;
  64. int wirte_len = 0;
  65. int file_off = 0;
  66. cache_info_t *info = (cache_info_t *)handle->data;
  67. assert(info);
  68. if (os_mutex_lock(&info->lock, OS_FOREVER) < 0) {
  69. SYS_LOG_ERR("lock failed %d \n",res);
  70. return -1;
  71. }
  72. file_off = handle->wofs % info->size;
  73. if (file_off + num > info->size) {
  74. brw = info->size - file_off;
  75. psram_write(info->base_off + file_off , buf, brw);
  76. num -= brw;
  77. handle->wofs += brw;
  78. wirte_len += brw;
  79. file_off = handle->wofs % info->size ;
  80. }
  81. psram_write(info->base_off + file_off,
  82. buf + wirte_len , num);
  83. handle->wofs += num;
  84. wirte_len += num;
  85. err_out:
  86. os_mutex_unlock(&info->lock);
  87. return wirte_len;
  88. }
  89. int psram_stream_seek(io_stream_t handle, int offset, seek_dir origin)
  90. {
  91. SYS_LOG_DBG("handle->rofs %d ,handle->wofs %d offset %d target_off %d \n",
  92. handle->rofs ,handle->wofs, offset, offset);
  93. handle->rofs = offset;
  94. return 0;
  95. }
  96. int psram_stream_tell(io_stream_t handle)
  97. {
  98. int res;
  99. cache_info_t *info = (cache_info_t *)handle->data;
  100. assert(info);
  101. res = os_mutex_lock(&info->lock, OS_FOREVER);
  102. if (res < 0) {
  103. SYS_LOG_ERR("lock failed %d \n",res);
  104. res = res;
  105. goto exit;
  106. }
  107. res = handle->rofs;
  108. exit:
  109. os_mutex_unlock(&info->lock);
  110. return res ;
  111. }
  112. int psram_stream_close(io_stream_t handle)
  113. {
  114. int res;
  115. cache_info_t *info = (cache_info_t *)handle->data;
  116. assert(info);
  117. res = os_mutex_lock(&info->lock, OS_FOREVER);
  118. if (res < 0) {
  119. SYS_LOG_ERR("lock failed %d \n",res);
  120. return res;
  121. }
  122. handle->state = STATE_CLOSE;
  123. exit:
  124. os_mutex_unlock(&info->lock);
  125. return res;
  126. }
  127. int psram_stream_destroy(io_stream_t handle)
  128. {
  129. int res;
  130. cache_info_t *info = (cache_info_t *)handle->data;
  131. assert(info);
  132. res = os_mutex_lock(&info->lock, OS_FOREVER);
  133. if (res < 0) {
  134. SYS_LOG_ERR("lock failed %d \n",res);
  135. return res;
  136. }
  137. psram_free(info->base_off);
  138. os_mutex_unlock(&info->lock);
  139. mem_free(info);
  140. handle->data = NULL;
  141. return res;
  142. }
  143. int psram_stream_init(io_stream_t handle, void *param)
  144. {
  145. cache_info_t *info = NULL;
  146. struct cache_t *cache = (struct cache_t *)param;
  147. info = mem_malloc(sizeof(cache_info_t));
  148. if (!info) {
  149. SYS_LOG_ERR("malloc failed \n");
  150. return -ENOMEM;
  151. }
  152. info->base_off = psram_alloc(cache->size);
  153. if(info->base_off < 0)
  154. {
  155. mem_free(info);
  156. return -ENOMEM;
  157. }
  158. info->size = cache->size;
  159. os_mutex_init(&info->lock);
  160. os_sem_init(&info->data_sem, 0, 1);
  161. os_sem_init(&info->can_write_sem, 0, 1);
  162. SYS_LOG_INF("size:%d, name: %s base_off: 0x%x\n",
  163. cache->size,cache->name,info->base_off);
  164. handle->cache_size = cache->min_size;
  165. handle->write_pending = cache->write_pending;
  166. handle->total_size = cache->size;
  167. handle->data = info;
  168. return 0;
  169. }
  170. const stream_ops_t psram_stream_ops = {
  171. .init = psram_stream_init,
  172. .open = psram_stream_open,
  173. .read = psram_stream_read,
  174. .seek = psram_stream_seek,
  175. .tell = psram_stream_tell,
  176. .write = psram_stream_write,
  177. .close = psram_stream_close,
  178. .destroy = psram_stream_destroy,
  179. };
  180. io_stream_t psram_stream_create(struct psram_info_t *param)
  181. {
  182. return stream_create(&psram_stream_ops, param);
  183. }