dev_sound.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #include <stdio.h>
  2. #include "minilisp.h"
  3. #include "alloc.h"
  4. #include "stream.h"
  5. #include "compiler_new.h"
  6. #include <unistd.h>
  7. #include <fcntl.h>
  8. typedef struct DMA_CB {
  9. uint32_t info;
  10. uint32_t source;
  11. uint32_t dest;
  12. uint32_t len;
  13. uint32_t stride;
  14. uint32_t next_block;
  15. } DMA_CB;
  16. uint32_t silence[] __attribute__((aligned(16))) = {0,0,0,0};
  17. static DMA_CB dma_cb __attribute__((aligned(32)));
  18. Cell* soundfs_open(Cell* path_cell) {
  19. return alloc_int(1);
  20. }
  21. Cell* soundfs_read() {
  22. return alloc_int(1);
  23. }
  24. Cell* soundfs_write(Cell* stream, Cell* packet) {
  25. //ethernet_tx(packet->addr,packet->size);
  26. uint32_t PBASE = PERIPHERAL_BASE;
  27. // enable phone jack (GPIO 40+45) PWM0/PWM1
  28. mmio_write(GPIO_BASE+GPIO_GPFSEL4, GPIO_FSEL0_ALT0 + GPIO_FSEL5_ALT0);
  29. printf("[soundfs] gpios enabled\r\n");
  30. dma_cb.info = DMA_DEST_DREQ + DMA_PERMAP_5 + DMA_SRC_INC;
  31. dma_cb.source = (uint32_t)silence;
  32. dma_cb.dest = 0x7E000000 + PWM_BASE + PWM_FIF1;
  33. dma_cb.len = 2*4;
  34. dma_cb.next_block = (uint32_t)&dma_cb;
  35. // test
  36. dma_cb.source = (uint32_t)packet->addr;
  37. dma_cb.len = (uint32_t)packet->size;
  38. printf("DMA block source changed to %p\r\n",dma_cb.source);
  39. printf("[soundfs] dma_cb created\r\n");
  40. mmio_write(PBASE+ CM_BASE+CM_PWMDIV, CM_PASSWORD + 0x2000); // Bits 0..11 Fractional Part Of Divisor = 0, Bits 12..23 Integer Part Of Divisor = 2);
  41. mmio_write(PBASE+ CM_BASE+CM_PWMCTL, CM_PASSWORD + CM_ENAB + CM_SRC_OSCILLATOR);
  42. printf("[soundfs] clock enabled\r\n");
  43. mmio_write(PBASE+ PWM_BASE+PWM_RNG1, 0x1b4);
  44. mmio_write(PBASE+ PWM_BASE+PWM_RNG2, 0x1b4);
  45. mmio_write(PBASE+ PWM_BASE+PWM_CTL, PWM_USEF2 + PWM_PWEN2 + PWM_USEF1 + PWM_PWEN1 + PWM_CLRF1);
  46. mmio_write(PBASE+ PWM_BASE+PWM_DMAC, PWM_ENAB+0x0001);
  47. printf("[soundfs] PWM enabled\r\n");
  48. mmio_write(PBASE+ DMA_ENABLE, DMA_EN0);
  49. printf("[soundfs] DMA enabled\r\n");
  50. mmio_write(PBASE+ DMA0_BASE+DMA_CONBLK_AD, (uint32_t)&dma_cb);
  51. mmio_write(PBASE+ DMA0_BASE+DMA_CS, DMA_ACTIVE);
  52. printf("[soundfs] DMA BASE enabled\r\n");
  53. return alloc_int(1);
  54. }
  55. void mount_soundfs() {
  56. fs_mount_builtin("/sound", soundfs_open, soundfs_read, soundfs_write, 0, 0);
  57. printf("[soundfs] mounted\r\n");
  58. }