Sunday, May 17, 2020

A working DMA AUDIO output

sidboxv5begin.png The project begins to take life now; While its taken me a LONG time and most of my hair is still present!
I still have a long way to go, but I do have something that is causing us some excitement!

While working on the SDCARD (which now appears to be fully working now) I wanted to see if I could get the SDCARD to stream some audio.

At first I did this the old way as the Sidbox 4 does this, uses an interupt with a read and write audio buffer positions. While this works really well on the Sidbox 4 OS, this chip has 400Mhz and a few DMA channels, one of which is a DAC.

I can setup an audio buffer, and have that memory transferred at 441khz to the DAC, leaving the CPU free to do something else! The issue I got with this though is the DMA seems to collide a bit while using the FatFS (but thats just a current bug for now)

So the while it plays it just choses to put the new data in to the audio buffer

To Get this to work I had to wire up the following.

Note that this has NO Card Select! I was confused by this, but I found that I had to find away to tie the CS to 3.3v, the SDMMC1 port onthe STM32 takes care of everything else.

wiring-the-sdcard.jpg

So Now I have the SDCARD playing 8Bit Wav Files at 44100Hz and its done via the DMA,
all the CPU has to do when called via an interupt is to fetch another 1k of audio and stuff it in to the audio buffer.

Wednesday, May 13, 2020

SDCARD - Nucleoboard

nucleo.jpg The Nucleo-H743ZI2 - How much did I just bite off!!! Been at this problem for almost 2 weeks now, the SDCARD reader, VIA the SPI and SDMMC1 while they both worked with the example code, but those example code had other parts i didn’t need. So removing the parts that were’nt relevent to me my project, I ended up breaking the code.

IT was clearly required to create a whole new project - Now I’ve done this multiple times and each time the sodding FatFS didn’t work!

After I Found the debug infomation and actually a HUGE amount of info from a Facebook user by the name of Jeff, lent me his code for the sdcard, it wasn’t completed and didn’t work for what I needed, HOWEVER It did point me to the directions of the missing commands!

In the file

bsp_driver_sd.c
__weak uint8_t BSP_SD_Init(void)
{
  uint8_t sd_state = MSD_OK;
  /* Check if the SD card is plugged in the slot */
  //if (BSP_SD_IsDetected() != SD_PRESENT)
  //{
    //return MSD_ERROR_SD_NOT_PRESENT;
  //}
  /* HAL SD initialization */
  sd_state = HAL_SD_Init(&hsd1);

  return sd_state;
}

what I did was the comment the check to see if the card was inserted, while I will put this in later, i just wanted to know if the card worked.

The SDCard has 2.2K resistor pull-ups, connected to MOSI, and MISO - the MX IDE calls these, SDMMC1_CMD (mosi), SDMMC1_DO (miso).

on the Nucleoboard there are red, green and a yellow.

#define GREEN_PIN                                GPIO_PIN_0
#define GREEN_GPIO_PORT                          GPIOB

#define YELLOW_PIN                                GPIO_PIN_1
#define YELLOW_GPIO_PORT                          GPIOE

#define RED_PIN                                GPIO_PIN_14
#define RED_GPIO_PORT                          GPIOB

void PrepUserOI(){

	  __HAL_RCC_GPIOB_CLK_ENABLE();
	  __HAL_RCC_GPIOC_CLK_ENABLE();
	  __HAL_RCC_GPIOD_CLK_ENABLE();
	  __HAL_RCC_GPIOE_CLK_ENABLE();

	GPIO_InitTypeDef GPIO_InitStruct = {0};
	GPIO_InitStruct.Pin = GPIO_PIN_1;
	GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
	GPIO_InitStruct.Pull = GPIO_NOPULL;
	GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
	HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);

	GPIO_InitStruct.Pin = GPIO_PIN_0;
	GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
	GPIO_InitStruct.Pull = GPIO_NOPULL;
	GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
	HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

	GPIO_InitStruct.Pin = GPIO_PIN_14;
	GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
	GPIO_InitStruct.Pull = GPIO_NOPULL;
	GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
	HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}

inside the main file
the annoypart was to tell the system to START the sd_init();
sadly i couldn’t find this in the information documenations normally provided by ST.

here is how I got it working

 /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_SDMMC1_SD_Init();
  MX_FATFS_Init();
  /* USER CODE BEGIN 2 */
  PrepUserOI();
  char res;
  res=0;
  //res = disk_initialize(0);
	res = BSP_SD_Init();
	if (res == FR_OK) {
		res = disk_initialize(0);
		if (res == FR_OK) {
			res = f_mount(&fs, "0:/", 0);
			if (res == FR_OK) {
				strcpy(buffer, "Hello world, this is a test");
				if (f_open(&fil, "hello world test file.txt", FA_CREATE_ALWAYS | FA_WRITE)
						== FR_OK) {

					f_write(&fil, buffer, sizeof(buffer), &bytes);
					f_close(&fil);

				}

			}
		}

	}

the code uploaded and begin to run, and instead of the yellow pulse range of around 200khz, i got the fill 12Mhz and some Blue pulses (this is the Data to the SDcard - MOSI)

sdcard_win1.png

with this I made a backup of this as the whole ordeal was awful!

BUT I concluded that while messy, the IDE and MX did do a good job of filling in the SDMMC1 and paring up to the SDCARD FatFS 0.12C

I have included the download of this project (its only for the Fat32 access)
CubeIDE GCC - C (not C++) Download Source

I’ll write up a more detailed what I found, how I figured out how to get this system to work!