wav.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #include <finsh.h>
  2. #include <dfs_posix.h>
  3. #include "stm32f10x_lib.h"
  4. rt_uint32_t wav_length;
  5. rt_uint8_t* wav_buffer;
  6. #define WAV_MP_NUM 2
  7. #define WAV_MP_BUFSZ (1024 * 1)
  8. #define STATIC_MEMPOOL
  9. #ifdef STATIC_MEMPOOL
  10. static rt_uint8_t mempool[(WAV_MP_BUFSZ + 4)* WAV_MP_NUM]; // 5k x 2
  11. static struct rt_mempool _mp;
  12. #endif
  13. static rt_mp_t mp;
  14. static rt_err_t wav_tx_done(rt_device_t dev, void *buffer)
  15. {
  16. /* release memory block to memory pool */
  17. rt_mp_free(buffer);
  18. return RT_EOK;
  19. }
  20. void wav(char* filename)
  21. {
  22. int fd;
  23. #ifdef STATIC_MEMPOOL
  24. rt_mp_init(&_mp, "wav", &mempool[0], sizeof(mempool), WAV_MP_BUFSZ);
  25. mp = &_mp;
  26. #else
  27. mp = rt_mp_create("wav", WAV_MP_NUM, WAV_MP_BUFSZ);
  28. #endif
  29. fd = open(filename, O_RDONLY, 0);
  30. if (fd >= 0)
  31. {
  32. rt_uint8_t* buf;
  33. rt_size_t len;
  34. rt_device_t device;
  35. /* open audio device and set tx done call back */
  36. device = rt_device_find("snd");
  37. rt_device_set_tx_complete(device, wav_tx_done);
  38. rt_device_open(device, RT_DEVICE_OFLAG_WRONLY);
  39. // buf = rt_mp_alloc(mp, RT_WAITING_FOREVER);
  40. // len = read(fd, (char*)buf, WAV_MP_BUFSZ);
  41. do {
  42. buf = rt_mp_alloc(mp, RT_WAITING_FOREVER);
  43. rt_kprintf("read file");
  44. len = read(fd, (char*)buf, WAV_MP_BUFSZ);
  45. rt_kprintf("...done!\n");
  46. if (len > 0) rt_device_write(device, 0, buf, len);
  47. } while (len != 0);
  48. /* close device and file */
  49. rt_device_close(device);
  50. close(fd);
  51. /* delete memory pool */
  52. #ifdef STATIC_MEMPOOL
  53. rt_mp_detach(mp);
  54. #else
  55. rt_mp_delete(mp);
  56. #endif
  57. }
  58. }
  59. FINSH_FUNCTION_EXPORT(wav, wav test)
  60. /* DMA Test */
  61. #define BufferSize 32
  62. u8 SPI2_Buffer_Tx[BufferSize] = {0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,
  63. 0x5A,0x5B,0x5C,0x5D,0x5E,0x5F,0x60,0x61,0x62,
  64. 0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6A,0x6B,
  65. 0x6C,0x6D,0x6E,0x6F,0x70};
  66. void wm(int cnt)
  67. {
  68. rt_device_t device;
  69. rt_uint8_t *buf;
  70. mp = rt_mp_create("wav", WAV_MP_NUM, WAV_MP_BUFSZ);
  71. device = rt_device_find("snd");
  72. rt_device_set_tx_complete(device, wav_tx_done);
  73. rt_device_open(device, RT_DEVICE_OFLAG_WRONLY);
  74. while(cnt)
  75. {
  76. buf = rt_mp_alloc(mp, RT_WAITING_FOREVER);
  77. rt_memcpy(buf, SPI2_Buffer_Tx, BufferSize);
  78. rt_device_write(device, 0, buf, BufferSize);
  79. cnt --;
  80. }
  81. rt_device_close(device);
  82. rt_mp_delete(mp);
  83. }
  84. FINSH_FUNCTION_EXPORT(wm, wm test)