Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
I was just looking at the code, any chance you want to do a tutorial on structures in C? (yes its cheeky but if you dont ask you dont get )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? (yes its cheeky but if you dont ask you dont get )
Shows how long since I posted code! I forgot the =C bit, thanks.
#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 }
#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
}
}
#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
}
}
Error handling in some situations is the usual example. https://eli.thegreenplace.net/2009/04/27/using-goto-for-error-handling-in-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]