Help needed setting up Codec Zero

I need help setting up a Raspberry Pi Zero with the Raspberry Pi Codec Zero HAT.

Required Information

  • DietPi version | v8.17.2
  • Distro version | bullseye 1
  • Kernel version | Linux audpi 6.1.21+ #1642 Mon Apr 3 17:19:14 BST 2023 armv6l GNU/Linux
  • Architecture | armhf
  • SBC model | RPi Zero W (armv6l)
  • Power supply used | 5V 2A
  • SD card used | 64 GB, A2, U3

Steps

I’m trying to get the official RPi Codec Zero HAT working, which used to be by IQaudIO who were aquired by RPi.

  • In dietpi-config I selected iqaudio-codec as soundcard:
    [ SUB1 ] DietPi-Set_hardware > soundcard (iqaudio-codec)
    [ INFO ] DietPi-Set_hardware | Checking for required APT packages: alsa-utils
    [ INFO ] DietPi-Set_hardware | Resetting all sound card settings...
    Found hardware: "RPi_Codec_Zero" "" "" "" ""
    Hardware is initialized using a generic method
    
  • After a reboot:
    # grep -a . /proc/device-tree/hat/*
    /proc/device-tree/hat/name:hat
    /proc/device-tree/hat/product:Raspberry Pi Codec Zero
    /proc/device-tree/hat/product_id:0x0101
    /proc/device-tree/hat/product_ver:0x0001
    /proc/device-tree/hat/uuid:7c582b02-e8d3-4142-976d-24290cc45f3f
    /proc/device-tree/hat/vendor:Raspberry Pi Ltd.
    
  • The official documentation says that this HAT needs additional configuration (see here and here), but I get errors when I try it:
    # alsactl restore -f IQaudIO_Codec_StereoMIC_record_and_HP_playback.state 
    No state is present for card Zero
    alsa-lib parser.c:2179:(load_toplevel_config) Unable to find the top-level configuration file '/usr/share/alsa/ucm2/ucm.conf'.
    alsa-lib main.c:1014:(snd_use_case_mgr_open) error: failed to import hw:0 use case configuration -2
    Found hardware: "RPi_Codec_Zero" "" "" "" ""
    Hardware is initialized using a generic method
    No state is present for card Zero
    
  • I also adapted the instructions from there to make the HAT the default device:
    # cat /etc/asound.conf 
    pcm.!default {
      type hw
      card Zero
      # device 0
    }
    
    ctl.!default {
      type hw
      card 0
    }
    
  • I then try to play an audio file, but it errors (I tried the step below after another reboot):
    # aplay A.wav 
    Playing WAVE 'A.wav' : Signed 16 bit Little Endian, Rate 44100 Hz, Stereo
    aplay: pcm_write:2058: write error: Input/output error
    

What am I doing wrong?

The RPi documentation suggests using dtoverlay=rpi-codeczero only if the HAT does not show up in /proc/, but it does.

Did you try enabling audio before you ran the script…

Did you do the walkthru on the page?

EEPROM Programming

Once the write-protect line has been pulled down, the EEPROM can be programmed.

You should first install the utilities and then run the programmer. Open up a terminal window and type the following:

$ sudo apt update 
$ sudo apt install rpi-audio-utils 
$ sudo rpi-audio-flash

After starting you will be presented with a warning screen.

Yes and Yes.

I enabled the audio using dietpi-config and selected iqaudio-codec as soundcard.

I followed the walk-through on that page. My HAT is new so the EEPROM update is not required, but I tried the rpi-audio-flash since you mentioned it. It detects the HAT correctly, says EEPROM is already programmed, and exits.

Maybe @MichaIng could have a look

The problem persists but I managed to reduce the errors by at least one. I discovered the alsa-ucm-conf package and after installing it, aslactl restore is at least able to find the top-level configuration file, but still errors our:

# alsactl restore -f CZ/IQaudIO_Codec_StereoMIC_record_and_HP_playback.state
No state is present for card Zero
alsa-lib main.c:1014:(snd_use_case_mgr_open) error: failed to import hw:0 use case configuration -2
Found hardware: "RPi_Codec_Zero" "" "" "" ""
Hardware is initialized using a generic method
No state is present for card Zero

One more change I tried: I noticed dietpi-config > Audio has an Auto detection section which detects the codec-zero, so I selected that now:

Makes no change to the alsactl restore error.

Here are some related issues/posts I found on the web:

https://forums.raspberrypi.com/viewtopic.php?t=346632

Please try to remove dtoverlay=iqaudio-codec from /boot/config.txt and reboot. The instructions from RPi docs do not mention this overlay at all, so it might be simply incorrect and conflict with the Codec Zero HAT.

Use this /etc/asound.conf:

pcm.!default {
  type hw
  card 0
  device 0
}

ctl.!default {
  type hw
  card 0
}

When doing alsactl restore -f, assure that you use the correct path to the file within the Git repo that you cloned.

Thanks @MichaIng for your reply. I followed your instructions:

  1. I removed dtoverlay=iqaudio-codec from /boot/config.txt and did a reboot. After reboot, I could see the HAT was correctly detected by the kernel, as always (with or without the various dtoverlay settings I’ve tried):
# grep -a . /proc/device-tree/hat/*
/proc/device-tree/hat/name:hat
/proc/device-tree/hat/product:Raspberry Pi Codec Zero
/proc/device-tree/hat/product_id:0x0101
/proc/device-tree/hat/product_ver:0x0001
/proc/device-tree/hat/uuid:7c582b02-e8d3-4142-976d-24290cc45f3f
/proc/device-tree/hat/vendor:Raspberry Pi Ltd.
  1. My /etc/asound.conf is as you wrote:
# cat /etc/asound.conf 
pcm.!default {
	type hw
	card 0
	device 0
}

ctl.!default {
	type hw
	card 0
}
  1. The alsactl command still fails (I’ve checked the file contents):
# alsactl restore -f CZ/IQaudIO_Codec_OnboardMIC_record_and_SPK_playback.state 
No state is present for card Zero
alsa-lib main.c:1014:(snd_use_case_mgr_open) error: failed to import hw:0 use case configuration -2
Found hardware: "RPi_Codec_Zero" "" "" "" ""
Hardware is initialized using a generic method
No state is present for card Zero

I also tried alsactl restore -D "Zero" -f ... which fails in exactly the same way as above. It certainly recognises the -D Zero, because if I try -D Rubbish then it complains with Cannot find soundcard 'Rubbish'.

My next step is to try these steps in the official RPi Raspbian to see if they work there.

I get the same problem on official RPi Raspbian. Will raise it with them.

Can you show

aplay -l

I wonder where this “card Zero” is coming from. Usually they are just named “card 0”, “card 1” etc. Also the state file does not contain this word at all. But probably its just the other card name which would then show up in aplay -l as well.

No state is present for card Zero

Which makes perfectly sense as long as it does not have been saved yet, although not sure if this is what the error message says exactly. But this one does not help, does it?

alsactl store

@MichaIng I tried alsactl store instead of restore and that seems to not have any complains!

Right now, I have Raspbian installed, which has the same issue so here is the diagnosis from it:

$ aplay -L
null
    Discard all samples (playback) or generate zero samples (capture)
sysdefault
    Default Audio Device
default
output
hw:CARD=vc4hdmi,DEV=0
    vc4-hdmi, MAI PCM i2s-hifi-0
    Direct hardware device without any conversions
plughw:CARD=vc4hdmi,DEV=0
    vc4-hdmi, MAI PCM i2s-hifi-0
    Hardware device with all software conversions
sysdefault:CARD=vc4hdmi
    vc4-hdmi, MAI PCM i2s-hifi-0
    Default Audio Device
hdmi:CARD=vc4hdmi,DEV=0
    vc4-hdmi, MAI PCM i2s-hifi-0
    HDMI Audio Output
dmix:CARD=vc4hdmi,DEV=0
    vc4-hdmi, MAI PCM i2s-hifi-0
    Direct sample mixing device
hw:CARD=Zero,DEV=0
    RPi Codec Zero, Raspberry Pi Codec Zero HiFi da7213-hifi-0
    Direct hardware device without any conversions
plughw:CARD=Zero,DEV=0
    RPi Codec Zero, Raspberry Pi Codec Zero HiFi da7213-hifi-0
    Hardware device with all software conversions
sysdefault:CARD=Zero
    RPi Codec Zero, Raspberry Pi Codec Zero HiFi da7213-hifi-0
    Default Audio Device
dmix:CARD=Zero,DEV=0
    RPi Codec Zero, Raspberry Pi Codec Zero HiFi da7213-hifi-0
    Direct sample mixing device```

@MichaIng I’ve booted the Pi with DietPi now. I ran the alsa store command and that worked without any errors. I added the corresponding alsa restore command to system boot (using dietpi-software settings). I rebooted. Here is what I have now:

# aplay -L
null
    Discard all samples (playback) or generate zero samples (capture)
default
hw:CARD=Zero,DEV=0
    RPi Codec Zero, Raspberry Pi Codec Zero HiFi da7213-hifi-0
    Direct hardware device without any conversions
plughw:CARD=Zero,DEV=0
    RPi Codec Zero, Raspberry Pi Codec Zero HiFi da7213-hifi-0
    Hardware device with all software conversions
sysdefault:CARD=Zero
    RPi Codec Zero, Raspberry Pi Codec Zero HiFi da7213-hifi-0
    Default Audio Device
dmix:CARD=Zero,DEV=0
    RPi Codec Zero, Raspberry Pi Codec Zero HiFi da7213-hifi-0
    Direct sample mixing device

and

# cat /etc/asound.conf 
pcm.!default {
	type hw
	card 0
	device 0
}

ctl.!default {
	type hw
	card 0
}

However, I still cannot play a .wav file:

# aplay A.wav 
Playing WAVE 'A.wav' : Signed 16 bit Little Endian, Rate 44100 Hz, Stereo
aplay: pcm_write:2058: write error: Input/output error

I also tried changing the following to what the RPi docs suggest:

# cat /etc/asound.conf 
pcm.!default {
        type hw
        card Zero
}

but that doesn’t help either:

# aplay A.wav 
Playing WAVE 'A.wav' : Signed 16 bit Little Endian, Rate 44100 Hz, Stereo
aplay: pcm_write:2058: write error: Input/output error

I made more progress with this issue. If I specify the -D plughw device to aplay, it doesn’t just error out, but I still don’t get any sound from the connected speakers:

# aplay -vvv -D plughw  /usr/share/sounds/alsa/Noise.wav
Playing WAVE '/usr/share/sounds/alsa/Noise.wav' : Signed 16 bit Little Endian, Rate 48000 Hz, Mono
Plug PCM: Route conversion PCM (sformat=S16_LE)
  Transformation table:
    0 <- 0
    1 <- 0
Its setup is:
  stream       : PLAYBACK
  access       : RW_INTERLEAVED
  format       : S16_LE
  subformat    : STD
  channels     : 1
  rate         : 48000
  exact rate   : 48000 (48000/1)
  msbits       : 16
  buffer_size  : 24000
  period_size  : 6000
  period_time  : 125000
  tstamp_mode  : NONE
  tstamp_type  : MONOTONIC
  period_step  : 1
  avail_min    : 6000
  period_event : 0
  start_threshold  : 24000
  stop_threshold   : 24000
  silence_threshold: 0
  silence_size : 0
  boundary     : 1572864000
Slave: Hardware PCM card 0 'RPi Codec Zero' device 0 subdevice 0
Its setup is:
  stream       : PLAYBACK
  access       : MMAP_INTERLEAVED
  format       : S16_LE
  subformat    : STD
  channels     : 2
  rate         : 48000
  exact rate   : 48000 (48000/1)
  msbits       : 16
  buffer_size  : 24000
  period_size  : 6000
  period_time  : 125000
  tstamp_mode  : NONE
  tstamp_type  : MONOTONIC
  period_step  : 1
  avail_min    : 6000
  period_event : 0
  start_threshold  : 24000
  stop_threshold   : 24000
  silence_threshold: 0
  silence_size : 0
  boundary     : 1572864000
  appl_ptr     : 0
  hw_ptr       : 0
Max peak (6000 samples): 0x00001029 ###                  12%
Max peak (6000 samples): 0x00000f54 ###                  11%
Max peak (6000 samples): 0x00000fb3 ###                  12%
Max peak (6000 samples): 0x00000d30 ###                  10%

That’s how it is running now but I don’t get any sound from the speakers I’ve connected to the terminals. I don’t have an oscilloscope, but I couldn’t see any voltage on the speaker terminal pins using my multi-meter, which might not mean much. I double-checked and I am using IQaudIO_Codec_OnboardMIC_record_and_SPK_playback.state with alsactl.

What can I try next?

@MichaIng I was mistaken in my understanding of what aslactl store was doing. I realise now that it just saved the existing config to the given file. Instead, for Codec Zero, I need to load the config from the file.

The issue seems to be upstream and I have raised it with them. Will update this forum post on any future progress.

1 Like

Run alsactl store only without any further arguments, as it could overwrite the .state file with the actual control values for whichever default sound device currently active. It stores values to /var/lib/alsa/asound.state then, from where it restores them on boot. I just thought there may be an issue with alsactl restore if this default state file does not exist (yet, it is created automatically on shutdown), even when defining another state file explicitly.

Please show the output of aplay -l (lowercase “el” instead of uppercase). This shows the cards instead of the controls. I want to assure that the card name “Zero” is indeed as well “card 0”.

And please try this asound.conf, which enables the ALSA auto-conversion plugin, allowing to play audio sources with formats other than what is natively supported by the sound card:

pcm.!default {
	type plug
	slave.pcm {
		type hw
		card 0
		device 0
	}
}

ctl.!default {
	type hw
	card 0
}

This basically matches the plughw device template, just directly/cleaner to rule out any potentially wrong content of the template.

@MichaIng I finally managed to get sound out of it! After trying to be sensible, asking around and searching through every forum post and documentation I could find, I nearly gave up and decided to try something silly.

I replaced IQaudIOCODEC with Zero in the .state files supplied by Pi-foundation and it seems to have worked (when used with alsactl restore).

Here is the output you asked for:

# aplay -l
**** List of PLAYBACK Hardware Devices ****
card 0: Zero [RPi Codec Zero], device 0: Raspberry Pi Codec Zero HiFi da7213-hifi-0 [Raspberry Pi Codec Zero HiFi da7213-hifi-0]
  Subdevices: 1/1
  Subdevice #0: subdevice #0

I have switched to the asound.conf you recommended and it works fine when I play a .wav file with aplay, like before.

Thanks for your help @MichaIng !

1 Like

Ah great. This totally makes sense. All those state files contain state.IQaudIOCODEC {, so explicitly define states for the IQaudIOCODEC sound card, and none for the Zero sound card. Now also the error message becomes meaningful :smile:. As the repo states that it is for RPi Codec Zero as well, it would have been very helpful to add a hint about this to the readme, or dedicated states files for this sound card.