es7243e.lua 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. -- es7243e器件地址
  2. local es7243e_address = 0x10
  3. -- es7243e初始化寄存器配置
  4. local es7243e_reg = {
  5. {0x01, 0x3A},
  6. {0x00, 0x80},
  7. {0xF9, 0x00},
  8. {0x04, 0x02},
  9. {0x04, 0x01},
  10. {0xF9, 0x01},
  11. {0x00, 0x1E},
  12. {0x01, 0x00},
  13. --radio 256
  14. {0x03, 0x20},
  15. {0x04, 0x01},
  16. {0x0D, 0},
  17. {0x05, 0x00},
  18. {0x06, 4 - 1},
  19. {0x07, 0x00},
  20. {0x08, 0xFF},
  21. {0x02, (0x00 << 7) + 0},
  22. {0x09, 0xCA},
  23. {0x0A, 0x85},
  24. {0x0B, 0xC0 + 0x00 + (0x03 << 2)},
  25. {0x0E, 191},
  26. {0x10, 0x38},
  27. {0x11, 0x16},
  28. {0x14, 0x0C},
  29. {0x15, 0x0C},
  30. {0x17, 0x02},
  31. {0x18, 0x26},
  32. {0x0F, 0x80},
  33. {0x19, 0x77},
  34. {0x1F, 0x08 + (0 << 5) - 0x00},
  35. {0x1A, 0xF4},
  36. {0x1B, 0x66},
  37. {0x1C, 0x44},
  38. {0x1E, 0x00},
  39. {0x20, 0x10 + 14},
  40. {0x21, 0x10 + 14},
  41. {0x00, 0x80 + (0 << 6)},
  42. {0x01, 0x3A},
  43. {0x16, 0x3F},
  44. {0x16, 0x00},
  45. {0x0B, 0x00 + (0x03 << 2)},
  46. {0x00, (0x80) + (1 << 6)},
  47. }
  48. -- i2s数据接收buffer
  49. local rx_buff = zbuff.create(3200)
  50. -- amr数据存放buffer,尽可能地给大一些
  51. local amr_buff = zbuff.create(10240)
  52. --创建一个amr的encoder
  53. local encoder = codec.create(codec.AMR, false)
  54. -- i2s数据接收回调
  55. local function record_cb(id, buff)
  56. if buff then
  57. log.info("I2S", id, "接收了", rx_buff:used())
  58. codec.encode(encoder, rx_buff, amr_buff) -- 对录音数据进行amr编码,成功的话这个接口会返回true
  59. end
  60. end
  61. local function record_task()
  62. sys.wait(5000)
  63. uart.setup(1, 115200) -- 开启串口1
  64. log.info("i2c initial",i2c.setup(0, i2c.FAST)) -- 开启i2c
  65. i2s.setup(0, 0, 8000, 16, 1, i2s.MODE_I2S) -- 开启i2s
  66. i2s.on(0, record_cb) -- 注册i2s接收回调
  67. i2s.recv(0, rx_buff, 3200)
  68. sys.wait(300)
  69. for i, v in pairs(es7243e_reg) do -- 初始化es7243e
  70. i2c.send(0,es7243e_address,v,1)
  71. end
  72. sys.wait(5000)
  73. i2c.send(0, es7243e_address,{0x00, (0x80) + (0 << 6)},1) -- 停止录音
  74. i2s.stop(0) -- 停止接收
  75. log.info("录音5秒结束")
  76. uart.write(1, "#!AMR\n") -- 向串口发送amr文件标识数据
  77. sys.wait(5) -- 等待5ms确保发送成功
  78. uart.write(1, amr_buff:query()) -- 向串口发送编码后的amr数据
  79. end
  80. pm.power(pm.DAC_EN, true) -- 打开es7243e芯片供电
  81. sys.taskInit(record_task)