Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Code Styling Guide for embedded systems

Status
Not open for further replies.
Sorry, I started it, however, so much bad code above, I need to go sleep. My example was to try and show how not to write code!! Again, sorry. So much unreadable code, I'm sure it will be unratherable in the morning. Goodnight.

Mike.
 
I know its just me but I cant read the indentations well like that, I have to have them inline or I cant tell which is grouped with which. Also since I didnt learn the capital and lower case thing for variables when I started I now find it hard to stick to that, one reason something like this is essential for people learning to program.
 
Inspired by this topic I wrote a post in the Code Repository. I think it represents the style I like.. style and structure.
I write this kind of small test projects for individual modules. Small project gives more motivation to clean up the code. After testing and cleaning the module is ready to be used in a bigger project.
https://www.electro-tech-online.com/threads/handling-hardware-and-software-events.147299/
I was just looking at the code, any chance you want to do a tutorial on structures in C? :D (yes its cheeky but if you dont ask you dont get :D)
 
I was just looking at the code, any chance you want to do a tutorial on structures in C? :D (yes its cheeky but if you dont ask you dont get :D)

I would like to. But I am not a good tutorial writer.. even in my native language. I think I can write good and simple programs that showcase some important point.
"Structures in C".. do you mean 'how to structure code', or how to use the struct data type? I could write something during the weekend.
 
Sorry I meant the structure data type.

LG
 
couldnt get code tags working right so cant post the sample
 
Use

Code tag.PNG


This should work for you.
 
Use

View attachment 97599

This should work for you.
Shows how long since I posted code! I forgot the =C bit, thanks.

In C what is the function of this Blah blah blah ?:D

C:
#include "platform.h" #include <stdio.h> #include "ata.h" #include "serial.h" #include "fat32.h"
void main(void) { int8_t retval; uint8_t sectorbyte; uint16_t byteix; #if defined(ATA_USE_ID) HDINFO hd0info; #endif
InitComms();
printf("\n\rPIC ATA interface test\n\r");
printf("Init ATA: "); if((retval = ATA_Init(ATA_MASTER)) != ATA_OK) { printf("ATA error %d\n\r", retval); } else { printf("OK\n\r"); }
/* DEBUG: dump the first sector */ printf("\n\rReading sector 0: "); if((retval = ATA_SetSectorLBAForRead(0)) != ATA_OK) { printf("ATA error %d\n\r", retval); } else { printf("LBA set\n\r"); } for(byteix = 0; byteix < 512; byteix++) { sectorbyte = ATA_ReadSectorByte(); if(!(byteix % 16)) printf("\n\r%03X: ", byteix); printf(" %02X", sectorbyte); }
/* DEBUG: dump the BPB of the FAT32 FS */ printf("\n\r\n\rReading sector 0x00004ABC: "); if((retval = ATA_SetSectorLBAForRead(0x00004ABC)) != ATA_OK) { printf("ATA error %d\n\r", retval); } else { printf("LBA set\n\r"); } for(byteix = 0; byteix < 512; byteix++) { sectorbyte = ATA_ReadSectorByte(); if(!(byteix % 16)) printf("\n\r%03X: ", byteix); printf(" %02X", sectorbyte); }
#if defined(ATA_USE_ID) printf("\n\r\n\rRead drive info: "); if((retval = ATA_ReadDriveInfo(&hd0info)) != ATA_OK) { printf("ATA error %d\n\r", retval); } else { printf("\n\r\tDrive model: %s\n\r\tRevision: %s\n\r\tSerial #: %s" "\n\r\tCylinders: %u\n\r\tHeads: %u\n\r\tSectors: %u\n\r", hd0info.model, hd0info.fwRev, hd0info.serialNum, hd0info.cyls, hd0info.heads, hd0info.sectors); } #endif
#ifdef FAT32_ENABLED printf("\n\rMount FAT32: "); if((retval = FAT32_Mount(0)) != FAT32_OK) { printf("Error %d\n\r", retval); } else { DirDesc testdirdesc; FD testfd; uint8_t databyte;
printf("OK\n\r");
if(retval = FAT32_DirOpen(&testdirdesc, "\\") != FAT32_OK) { printf("Error %d\n\r", retval); } else { printf("\n\rRoot dir contents:\n\r"); retval = FAT32_DirLoadNextEntry(&testdirdesc); while((retval == FAT32_OK) || (retval == FAT32_DIRENTRY_IS_DIR)) { printf("%-12s", testdirdesc.currentDirEntryName); if(retval == FAT32_DIRENTRY_IS_DIR) { printf("\t<dir>\n\r"); } else { printf("\n\r"); } retval = FAT32_DirLoadNextEntry(&testdirdesc); } if(retval != FAT32_EODIRENTRYS) { printf("Error %d loading next dir record\n\r", retval); }
printf("\n\rRe-opening root dir: "); if((retval = FAT32_DirOpen(&testdirdesc, "\\")) != FAT32_OK) { printf("Error %d\n\r", retval); } else { printf("OK\n\r"); }
printf("\n\rOpening file HELLO.TXT: "); if((retval = FAT32_FileOpen(&testfd, &testdirdesc, "HELLO.TXT")) != FAT32_OK) { printf("Error %d\n\r", retval); } else { printf("OK\n\r");
printf("\n\rPrinting file HELLO.TXT:\n\r"); retval = FAT32_FileRead(&testfd, 1, &databyte); while(retval == FAT32_OK) { printf("%c", databyte); retval = FAT32_FileRead(&testfd, 1, &databyte); } printf("\n\r"); } } } #endif }

I am still doing something wrong

C:
#include "platform.h"
#include <stdio.h>
#include "ata.h"
#include "serial.h"
#include "fat32.h"

void main(void)
{
  int8_t retval;
  uint8_t sectorbyte;
  uint16_t byteix;
#if defined(ATA_USE_ID)
  HDINFO hd0info;
#endif

  InitComms();

  printf("\n\rPIC ATA interface test\n\r");

  printf("Init ATA: ");
  if((retval = ATA_Init(ATA_MASTER)) != ATA_OK)
  {
  printf("ATA error %d\n\r", retval);
  }
  else
  {
  printf("OK\n\r");
  }

  /* DEBUG: dump the first sector */
  printf("\n\rReading sector 0: ");
  if((retval = ATA_SetSectorLBAForRead(0)) != ATA_OK)
  {
  printf("ATA error %d\n\r", retval);
  }
  else
  {
  printf("LBA set\n\r");
  }
  for(byteix = 0; byteix < 512; byteix++)
  {
  sectorbyte = ATA_ReadSectorByte();
  if(!(byteix % 16))
  printf("\n\r%03X: ", byteix);
  printf(" %02X", sectorbyte);
  }

  /* DEBUG: dump the BPB of the FAT32 FS */
  printf("\n\r\n\rReading sector 0x00004ABC: ");
  if((retval = ATA_SetSectorLBAForRead(0x00004ABC)) != ATA_OK)
  {
  printf("ATA error %d\n\r", retval);
  }
  else
  {
  printf("LBA set\n\r");
  }
  for(byteix = 0; byteix < 512; byteix++)
  {
  sectorbyte = ATA_ReadSectorByte();
  if(!(byteix % 16))
  printf("\n\r%03X: ", byteix);
  printf(" %02X", sectorbyte);
  }

#if defined(ATA_USE_ID)
  printf("\n\r\n\rRead drive info: ");
  if((retval = ATA_ReadDriveInfo(&hd0info)) != ATA_OK)
  {
  printf("ATA error %d\n\r", retval);
  }
  else
  {
  printf("\n\r\tDrive model: %s\n\r\tRevision: %s\n\r\tSerial #: %s"
  "\n\r\tCylinders: %u\n\r\tHeads: %u\n\r\tSectors: %u\n\r",
  hd0info.model,
  hd0info.fwRev,
  hd0info.serialNum,
  hd0info.cyls,
  hd0info.heads,
  hd0info.sectors);
  }
#endif

#ifdef FAT32_ENABLED
  printf("\n\rMount FAT32: ");
  if((retval = FAT32_Mount(0)) != FAT32_OK)
  {
  printf("Error %d\n\r", retval);
  }
  else
  {
  DirDesc testdirdesc;
  FD testfd;
  uint8_t databyte;

  printf("OK\n\r");

  if(retval = FAT32_DirOpen(&testdirdesc, "\\") != FAT32_OK)
  {
  printf("Error %d\n\r", retval);
  }
  else
  {
  printf("\n\rRoot dir contents:\n\r");
  retval = FAT32_DirLoadNextEntry(&testdirdesc);
  while((retval == FAT32_OK) || (retval == FAT32_DIRENTRY_IS_DIR))
  {
  printf("%-12s", testdirdesc.currentDirEntryName);
  if(retval == FAT32_DIRENTRY_IS_DIR)
  {
  printf("\t<dir>\n\r");
  }
  else
  {
  printf("\n\r");
  }
  retval = FAT32_DirLoadNextEntry(&testdirdesc);
  }
  if(retval != FAT32_EODIRENTRYS)
  {
  printf("Error %d loading next dir record\n\r", retval);
  }

  printf("\n\rRe-opening root dir: ");
  if((retval = FAT32_DirOpen(&testdirdesc, "\\")) != FAT32_OK)
  {
  printf("Error %d\n\r", retval);
  }
  else
  {
  printf("OK\n\r");
  }

  printf("\n\rOpening file HELLO.TXT: ");
  if((retval = FAT32_FileOpen(&testfd, &testdirdesc, "HELLO.TXT")) != FAT32_OK)
  {
  printf("Error %d\n\r", retval);
  }
  else
  {
  printf("OK\n\r");

  printf("\n\rPrinting file HELLO.TXT:\n\r");
  retval = FAT32_FileRead(&testfd, 1, &databyte);
  while(retval == FAT32_OK)
  {
  printf("%c", databyte);
  retval = FAT32_FileRead(&testfd, 1, &databyte);
  }
  printf("\n\r");
  }
  }
  }
#endif
}

Thats odd it dosnt post well straight from github you have to paste into Prog notepad first,also its stripped out the formatting
 
Last edited:
C:
  }
#endif

#ifdef FAT32_ENABLED
  printf("\n\rMount FAT32: ");
  if((retval = FAT32_Mount(0)) != FAT32_OK)
  {
  printf("Error %d\n\r", retval);
  }
  else
  {
  DirDesc testdirdesc;
  FD testfd;
  uint8_t databyte;

  printf("OK\n\r");

  if(retval = FAT32_DirOpen(&testdirdesc, "\\") != FAT32_OK)
  {
  printf("Error %d\n\r", retval);
  }
  else
  {
  printf("\n\rRoot dir contents:\n\r");
  retval = FAT32_DirLoadNextEntry(&testdirdesc);
  while((retval == FAT32_OK) || (retval == FAT32_DIRENTRY_IS_DIR))
  {
  printf("%-12s", testdirdesc.currentDirEntryName);
  if(retval == FAT32_DIRENTRY_IS_DIR)
  {
  printf("\t<dir>\n\r");
  }
  else
  {
  printf("\n\r");
  }
  retval = FAT32_DirLoadNextEntry(&testdirdesc);
  }
  if(retval != FAT32_EODIRENTRYS)
  {
  printf("Error %d loading next dir record\n\r", retval);
  }

  printf("\n\rRe-opening root dir: ");
  if((retval = FAT32_DirOpen(&testdirdesc, "\\")) != FAT32_OK)
  {
  printf("Error %d\n\r", retval);
  }
  else
  {
  printf("OK\n\r");
  }

  printf("\n\rOpening file HELLO.TXT: ");
  if((retval = FAT32_FileOpen(&testfd, &testdirdesc, "HELLO.TXT")) != FAT32_OK)
  {
  printf("Error %d\n\r", retval);
  }
  else
  {
  printf("OK\n\r");

  printf("\n\rPrinting file HELLO.TXT:\n\r");
  retval = FAT32_FileRead(&testfd, 1, &databyte);
  while(retval == FAT32_OK)
  {
  printf("%c", databyte);
  retval = FAT32_FileRead(&testfd, 1, &databyte);
  }
  printf("\n\r");
  }
  }
  }
#endif
}
}

it should have formatted like this

CODE.JPG
 
What do you mean by Prog notepad? Windows Notepad program?

It looks as if the original github was in Unix or Mac text file format. The biggest difference is in their method of ending lines. Windows uses a CRLF (carriage return and line feed) pair, while Unix terminates a line with just an LF character. Notepad doesn't convert cleanly; Textpad and Notepad++ have an option to specify the file format when saving. It appears that the text editor you used cleaned up "white space" and thus you lost your formatting. I don't have that problem with Textpad, but have experinced it elsewhere. I use the Arduino IDE almost exclusively and it has a built in tool to format/indent the code in a clean manner. You could post-process your file in an IDE to rebuild the formatting.
 
I use programmers notepad 2. I will look into the line ending thanks.

Actually if you look at the pic LF+CF are on so the formatting should have been as in the pic
 
Have a look at Crimson editor, free and formats the code real sweet, kinda like what my wife says I am, sweet:)
 

This is about the only time I use goto. It's much cleaner and clearer this way in a device driver to handle error conditions in kernel mode. It's unlikely you will need these types of structures with a embedded system unless it's using an OS of some sort with memory management.
C:
static int32_t spigert_spi_probe(struct spi_device * spi)
{
struct comedi_spigert *pdata;
int32_t ret;

pdata = kzalloc(sizeof(struct comedi_spigert), GFP_KERNEL);
if (!pdata)
return -ENOMEM;

spi->dev.platform_data = pdata;
pdata->tx_buff = kzalloc(SPI_BUFF_SIZE, GFP_KERNEL | GFP_DMA);
if (!pdata->tx_buff) {
ret = -ENOMEM;
goto kfree_exit;
}
pdata->rx_buff = kzalloc(SPI_BUFF_SIZE, GFP_KERNEL | GFP_DMA);
if (!pdata->rx_buff) {
ret = -ENOMEM;
goto kfree_tx_exit;
}

// code  skipped

/*
* Check for basic errors
*/
ret = spi_w8r8(spi, 0); /* check for spi comm error */
if (ret < 0) {
dev_err(&spi->dev, "spi comm error\n");
ret = -EIO;
goto kfree_rx_exit;
}

/* setup comedi part of driver */
if (spi->chip_select == CSnA) {
ret = comedi_driver_register(&daqgert_driver);
if (ret < 0)
goto kfree_rx_exit;

if (gert_autoload)
ret = comedi_auto_config(&spi->master->dev,
&daqgert_driver, 0);
if (ret < 0)
goto kfree_rx_exit;
}
return 0;

kfree_rx_exit:
kfree(pdata->rx_buff);
kfree_tx_exit:
kfree(pdata->tx_buff);
kfree_exit:
kfree(pdata);
return ret;
}
[CODE]
 
Last edited:
I might give those a try, looking some code examples on here it seems other have the formatting messed with in the same way.
 
Status
Not open for further replies.

Latest threads

Back
Top