<commit_before>package main

import (
	"delay"
	"fmt"
	"rtos"

	"sdcard"

	"stm32/hal/dma"
	"stm32/hal/gpio"
	"stm32/hal/system"
	"stm32/hal/system/timer/systick"

	"stm32/hal/raw/rcc"
	"stm32/hal/raw/sdio"
)

var h Host

func init() {
	system.Setup96(8) // Setups USB/SDIO/RNG clock to 48 MHz
	systick.Setup(2e6)

	// GPIO

	gpio.A.EnableClock(true)
	cmd := gpio.A.Pin(6)
	//d1 := gpio.A.Pin(8)
	//d2 := gpio.A.Pin(9)

	gpio.B.EnableClock(true)
	//d3 := gpio.B.Pin(5)
	d0 := gpio.B.Pin(7)
	clk := gpio.B.Pin(15)

	cfg := gpio.Config{Mode: gpio.Alt, Speed: gpio.High, Pull: gpio.PullUp}
	for _, pin := range []gpio.Pin{clk, cmd, d0 /*d1, d2, d3*/} {
		pin.Setup(&cfg)
		pin.SetAltFunc(gpio.SDIO)
	}

	d := dma.DMA2
	d.EnableClock(true)
	h.dma = d.Channel(6, 4)

	rcc.RCC.SDIOEN().Set()
	h.Enable()
	h.SetFreq(400e3) // Set SDIO_CK to no more than 400 kHz (max. OD freq).
}

func main() {
	delay.Millisec(250) // For SWO output

	ocr := sdcard.V31 | sdcard.V32 | sdcard.V33 | sdcard.HCXC
	v2 := true

	fmt.Printf("\nInitializing SD card ")

	// Reset
	h.Cmd(sdcard.CMD0())
	checkErr("CMD0", h.Err(true), 0)

	// Verify card interface operating condition.
	vhs, pattern := h.Cmd(sdcard.CMD8(sdcard.V27_36, 0xAC)).R7()
	if err := h.Err(true); err != nil {
		if err == sdcard.ErrTimeout {
			ocr &^= sdcard.HCXC
			v2 = false
		} else {
			checkErr("CMD8", err, 0)
		}
	} else if vhs != sdcard.V27_36 || pattern != 0xAC {
		fmt.Printf("CMD8 bad response: %x, %x\n", vhs, pattern)
		for {
		}
	}

	// Initialize memory card.
	var oca sdcard.OCR
	for i := 0; oca&sdcard.PWUP == 0 && i < 20; i++ {
		h.Cmd(sdcard.CMD55(0))
		oca = h.Cmd(sdcard.ACMD41(ocr)).R3()
		checkErr("ACMD41", h.Err(true), 0)
		fmt.Printf(".")
		delay.Millisec(50)
	}
	if oca&sdcard.PWUP == 0 {
		fmt.Printf(" timeout\n")
		for {
		}
	}
	fmt.Printf(" OK\n\n")
	fmt.Printf("Physicaly layer version 2.00+: %t\n", v2)
	fmt.Printf("Operation Conditions Register: 0x%08X\n\n", oca)

	// Read Card Identification Register.
	cid := h.Cmd(sdcard.CMD2()).R2CID()
	checkErr("CMD2", h.Err(true), 0)

	printCID(cid)

	// Generate new Relative Card Address.
	rca, _ := h.Cmd(sdcard.CMD3()).R6()
	checkErr("CMD3", h.Err(true), 0)

	fmt.Printf("Relative Card Address: 0x%04X\n\n", rca)

	// After CMD3 card is in Data Transfer Mode (Standby State) and SDIO_CK can
	// be set to no more than 25 MHz (max. PP freq).
	h.SetFreq(25e6)

	// Read Card Specific Data register.
	csd := h.Cmd(sdcard.CMD9(rca)).R2CSD()
	checkErr("CMD9", h.Err(true), 0)

	printCSD(csd)

	// Select card (put into Transfer State).
	cs := h.Cmd(sdcard.CMD7(rca)).R1()
	checkErr("CMD7", h.Err(true), cs)

	// Disable 50k pull-up resistor on D3/CD.
	h.Cmd(sdcard.CMD55(rca))
	cs = h.Cmd(sdcard.ACMD42(false)).R1()
	checkErr("ACMD42", h.Err(true), cs)

	if ocr&sdcard.HCXC == 0 {
		// Set block size to 512 B for version < 2 or SDSC card.
		cs = h.Cmd(sdcard.CMD16(512)).R1()
		checkErr("CMD16", h.Err(true), cs)
	}

	block := make([]uint32, 512/4)

	for n := uint(0); n < 24; n++ {
		addr := n
		if ocr&sdcard.HCXC == 0 {
			addr *= 512
		}
		h.SetupDMA(dma.PTM, block)
		cs = h.Cmd(sdcard.CMD17(addr)).R1()
		checkErr("CMD17", h.Err(true), cs)
		h.StartBlockTransfer(RD)
		for {
			ev, err := h.dma.Status()
			if err != 0 {
				fmt.Printf("DMA error: %v\n", err)
				return<commit_msg>examples/nucleo-f411re/sdcard: Add delay after CMD0.<commit_after>package main

import (
	"delay"
	"fmt"
	"rtos"

	"sdcard"

	"stm32/hal/dma"
	"stm32/hal/gpio"
	"stm32/hal/system"
	"stm32/hal/system/timer/systick"

	"stm32/hal/raw/rcc"
	"stm32/hal/raw/sdio"
)

var h Host

func init() {
	system.Setup96(8) // Setups USB/SDIO/RNG clock to 48 MHz
	systick.Setup(2e6)

	// GPIO

	gpio.A.EnableClock(true)
	cmd := gpio.A.Pin(6)
	//d1 := gpio.A.Pin(8)
	//d2 := gpio.A.Pin(9)

	gpio.B.EnableClock(true)
	//d3 := gpio.B.Pin(5)
	d0 := gpio.B.Pin(7)
	clk := gpio.B.Pin(15)

	cfg := gpio.Config{Mode: gpio.Alt, Speed: gpio.High, Pull: gpio.PullUp}
	for _, pin := range []gpio.Pin{clk, cmd, d0 /*d1, d2, d3*/} {
		pin.Setup(&cfg)
		pin.SetAltFunc(gpio.SDIO)
	}

	d := dma.DMA2
	d.EnableClock(true)
	h.dma = d.Channel(6, 4)

	rcc.RCC.SDIOEN().Set()
	h.Enable()
	h.SetFreq(400e3) // Set SDIO_CK to no more than 400 kHz (max. OD freq).
}

func main() {
	delay.Millisec(250) // For SWO output

	ocr := sdcard.V31 | sdcard.V32 | sdcard.V33 | sdcard.HCXC
	v2 := true

	// Reset
	h.Cmd(sdcard.CMD0())
	checkErr("CMD0", h.Err(true), 0)
	
	delay.Millisec(2) // CMD0 may require up to 8 clock cycles to reset card.	

	// Verify card interface operating condition.
	vhs, pattern := h.Cmd(sdcard.CMD8(sdcard.V27_36, 0xAC)).R7()
	if err := h.Err(true); err != nil {
		if err == sdcard.ErrTimeout {
			ocr &^= sdcard.HCXC
			v2 = false
		} else {
			checkErr("CMD8", err, 0)
		}
	} else if vhs != sdcard.V27_36 || pattern != 0xAC {
		fmt.Printf("CMD8 bad response: %x, %x\n", vhs, pattern)
		for {
		}
	}
	fmt.Printf("\nPhysicaly layer version 2.00+: %t\n", v2)

	fmt.Printf("Initializing SD card ")
	var oca sdcard.OCR
	for i := 0; oca&sdcard.PWUP == 0 && i < 20; i++ {
		h.Cmd(sdcard.CMD55(0))
		oca = h.Cmd(sdcard.ACMD41(ocr)).R3()
		checkErr("ACMD41", h.Err(true), 0)
		fmt.Printf(".")
		delay.Millisec(50)
	}
	if oca&sdcard.PWUP == 0 {
		fmt.Printf(" timeout\n")
		for {
		}
	}
	fmt.Printf(" OK\n\n")
	fmt.Printf("Operation Conditions Register: 0x%08X\n\n", oca)

	// Read Card Identification Register.
	cid := h.Cmd(sdcard.CMD2()).R2CID()
	checkErr("CMD2", h.Err(true), 0)

	printCID(cid)

	// Generate new Relative Card Address.
	rca, _ := h.Cmd(sdcard.CMD3()).R6()
	checkErr("CMD3", h.Err(true), 0)

	fmt.Printf("Relative Card Address: 0x%04X\n\n", rca)

	// After CMD3 card is in Data Transfer Mode (Standby State) and SDIO_CK can
	// be set to no more than 25 MHz (max. PP freq).
	h.SetFreq(25e6)

	// Read Card Specific Data register.
	csd := h.Cmd(sdcard.CMD9(rca)).R2CSD()
	checkErr("CMD9", h.Err(true), 0)

	printCSD(csd)

	// Select card (put into Transfer State).
	cs := h.Cmd(sdcard.CMD7(rca)).R1()
	checkErr("CMD7", h.Err(true), cs)

	// Disable 50k pull-up resistor on D3/CD.
	h.Cmd(sdcard.CMD55(rca))
	cs = h.Cmd(sdcard.ACMD42(false)).R1()
	checkErr("ACMD42", h.Err(true), cs)

	if ocr&sdcard.HCXC == 0 {
		// Set block size to 512 B for version < 2 or SDSC card.
		cs = h.Cmd(sdcard.CMD16(512)).R1()
		checkErr("CMD16", h.Err(true), cs)
	}

	block := make([]uint32, 512/4)

	for n := uint(0); n < 16; n++ {
		addr := n
		if oca&sdcard.HCXC == 0 {
			addr *= 512
		}
		h.SetupDMA(dma.PTM, block)
		cs = h.Cmd(sdcard.CMD17(addr)).R1()
		checkErr("CMD17", h.Err(true), cs)
		h.StartBlockTransfer(RD)
		for {
			ev, err := h.dma.Status()
			if err != 0 {
				fmt.Printf("DMA error: %v\n", err)
				return
{'fraction_non_alphanumeric': 0.191527446300716, 'fraction_numerical': 0.049373508353221955, 'mean_word_length': 3.1723708774113253, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7316644', 'n_tokens_mistral': 3327, 'n_tokens_neox': 3020, 'n_words': 652}