This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

i2s hal

I'm trying to make i2s working on mbed os by using NRF52 development kit board (based on nrf52832).

Looks like I'm missing something. What I managed to do so far is:

#include "mbed.h"
#include "nrf52.h"
#include <nrf_i2s.h>


int main(){

Serial pc(USBTX, USBRX);

const int frequency = 440; // frequency of square wave in Hz
const int amplitude = 500; // amplitude of square wave
const int sampleRate = 8000; // sample rate in Hz

const int halfWavelength = (sampleRate / frequency); // half wavelength of square wave
uint32_t sample = amplitude; // current sample value
int count = 0;

NRF_I2S_Type * p_i2s;

nrf_i2s_enable(p_i2s);

nrf_i2s_pins_set(p_i2s, 11, 12, NRF_I2S_PIN_NOT_CONNECTED, 13, NRF_I2S_PIN_NOT_CONNECTED);

bool ret = nrf_i2s_configure(p_i2s, NRF_I2S_MODE_MASTER, NRF_I2S_FORMAT_I2S, NRF_I2S_ALIGN_LEFT, NRF_I2S_SWIDTH_24BIT, NRF_I2S_CHANNELS_STER
EO, NRF_I2S_MCK_DISABLED, NRF_I2S_RATIO_128X);

if (ret){
pc.printf("Success\n");
} else {
pc.printf("Error \n");
}


while(1){
if (count % halfWavelength == 0) {
// invert the sample every half wavelength count multiple to generate square wave
sample = -1 * sample;
}

// write the same sample twice, once for left and once for the right channel
nrf_i2s_tx_buffer_set(p_i2s, &sample);
 nrf_i2s_tx_buffer_set(p_i2s, &sample);
 // increment the counter for the next sample
count++;
}
return 0;
}

But it's not working. Not to mention the warning that p_i2s is not initialised.
Parents Reply Children
No Data
Related