Skip to content

Commit

Permalink
Extend generation of MX_Device.h (USBH instance, MCI mode) (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidLesnjak authored Jul 12, 2024
1 parent eced9ef commit 67e0a54
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 10 deletions.
77 changes: 68 additions & 9 deletions internal/stm32CubeMX/mxDevice.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,15 +174,19 @@ func writeMXdeviceH(contextMap map[string]map[string]string, srcFolder string, m
if err != nil {
return err
}
usbdHandle, err := getUSBDHandle(fMain, peripheral)
usbHandle, err := getUSBHandle(fMain, peripheral)
if err != nil {
return err
}
mciMode, err := getMCIMode(fMain, peripheral)
if err != nil {
return err
}
pins, err := getPins(contextMap, fMsp, peripheral)
if err != nil {
return err
}
err = mxDeviceWritePeripheralCfg(out, peripheral, vmode, i2cInfo, usbdHandle, pins)
err = mxDeviceWritePeripheralCfg(out, peripheral, vmode, i2cInfo, usbHandle, mciMode, pins)
if err != nil {
return err
}
Expand Down Expand Up @@ -393,9 +397,9 @@ func getI2cInfo(fMain *os.File, peripheral string) (map[string]string, error) {
return info, nil
}

// Get USB Device Handle
func getUSBDHandle(fMain *os.File, peripheral string) (string, error) {
if strings.HasPrefix(peripheral, "USB") && !strings.Contains(peripheral, "HOST") {
// Get USB Handle
func getUSBHandle(fMain *os.File, peripheral string) (string, error) {
if strings.HasPrefix(peripheral, "USB") {
_, err := fMain.Seek(0, 0)
if err != nil {
return "", err
Expand All @@ -407,7 +411,7 @@ func getUSBDHandle(fMain *os.File, peripheral string) (string, error) {
line := mainScan.Text()
line = strings.TrimSpace(line)

if strings.HasPrefix(line, "PCD_HandleTypeDef") {
if strings.HasPrefix(line, "PCD_HandleTypeDef") || strings.HasPrefix(line, "HCD_HandleTypeDef") {
line = strings.TrimSuffix(line, ";")
lineSplit := strings.Split(line, " ")
if len(lineSplit) < 2 {
Expand All @@ -431,6 +435,50 @@ func getUSBDHandle(fMain *os.File, peripheral string) (string, error) {
return "", nil
}

// Get MCI Mode
func getMCIMode(fMain *os.File, peripheral string) (string, error) {
if strings.HasPrefix(peripheral, "SDMMC") || strings.HasPrefix(peripheral, "SDIO") {
_, err := fMain.Seek(0, 0)
if err != nil {
return "", err
}

mainScan := bufio.NewScanner(fMain)
mainScan.Split(bufio.ScanLines)
for mainScan.Scan() {
line := mainScan.Text()
line = strings.TrimSpace(line)

if strings.HasPrefix(line, "MMC_HandleTypeDef") || strings.HasPrefix(line, "SD_HandleTypeDef") {
line = strings.TrimSuffix(line, ";")
lineSplit := strings.Split(line, " ")
if len(lineSplit) < 2 {
continue
}
handle := lineSplit[1]

index := getDigitAtEnd(peripheral)
if index != "" {
if getDigitAtEnd(handle) != index {
continue
}
}

mciMode := ""
if strings.HasPrefix(line, "MMC_HandleTypeDef") {
mciMode = "MMC"
}
if strings.HasPrefix(line, "SD_HandleTypeDef") {
mciMode = "SD"
}

return mciMode, nil
}
}
}
return "", nil
}

func getPinConfiguration(fMsp *os.File, peripheral string, pin string, label string) (PinDefinition, error) {
var pinInfo PinDefinition

Expand Down Expand Up @@ -549,7 +597,7 @@ func mxDeviceWriteHeader(out *bufio.Writer, fName string) error {
return err
}

func mxDeviceWritePeripheralCfg(out *bufio.Writer, peripheral string, vmode string, i2cInfo map[string]string, usbdHandle string, pins map[string]PinDefinition) error {
func mxDeviceWritePeripheralCfg(out *bufio.Writer, peripheral string, vmode string, i2cInfo map[string]string, usbHandle string, mciMode string, pins map[string]PinDefinition) error {
var err error

str := "\n/*------------------------------ " + peripheral
Expand Down Expand Up @@ -581,11 +629,22 @@ func mxDeviceWritePeripheralCfg(out *bufio.Writer, peripheral string, vmode stri
return err
}
}
if usbdHandle != "" {
if usbHandle != "" {
if _, err = out.WriteString("/* Handle */\n"); err != nil {
return err
}
if err = writeDefine(out, peripheral+"_HANDLE", usbdHandle); err != nil {
if err = writeDefine(out, peripheral+"_HANDLE", usbHandle); err != nil {
return err
}
if _, err = out.WriteString("\n"); err != nil {
return err
}
}
if mciMode != "" {
if _, err = out.WriteString("/* Mode */\n"); err != nil {
return err
}
if err = writeDefine(out, peripheral+"_MODE_"+mciMode, "1"); err != nil {
return err
}
if _, err = out.WriteString("\n"); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion internal/stm32CubeMX/mxDevice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ func Test_mxDeviceWritePeripheralCfg(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
tt.args.out = bufio.NewWriter(&b)

if err := mxDeviceWritePeripheralCfg(tt.args.out, tt.args.peripheral, tt.args.vmode, tt.args.i2cInfo, "", tt.args.pins); (err != nil) != tt.wantErr {
if err := mxDeviceWritePeripheralCfg(tt.args.out, tt.args.peripheral, tt.args.vmode, tt.args.i2cInfo, "", "", tt.args.pins); (err != nil) != tt.wantErr {
t.Errorf("mxDeviceWritePeripheralCfg() error = %v, wantErr %v", err, tt.wantErr)
}
tt.args.out.Flush()
Expand Down

0 comments on commit 67e0a54

Please sign in to comment.