2727 views|2 replies

2125

Posts

0

Resources
The OP

My opinion on how to write high-quality programs [Copy link]

I have not been involved in embedded software development for very long, only 5 years. I am neither an expert nor a veteran, I can only be considered a 5-year-old rookie. In the first 3 years, I wrote a lot of programs by myself without any expert guidance. At that time, I did not think about writing programs that were easy to understand, maintain, and transplant. I just thought about how to implement the function. So in my first project, the GSM car alarm, the logic started to get messy when I wrote 5,000 lines in assembly. I remember that when I was about to complete all the functions, the boss asked me to add one more small function, but I couldn't decide where to start based on the original program to add it, because the logical relationship of the program was too messy, and modifying one place would involve many functions. After that time, I made up my mind to avoid this problem next time I write a program, and I must make it easy to modify and have a clear logical relationship. Later, I was lucky enough to work in a foreign company as a MCU software developer. An engineer gave me a book about object-oriented programming in C language published in Taiwan (I forgot the name of the book). In that book, I found the reason why the logic of my first project, the GSM car alarm program, was messy. I read the book carefully at that time and felt really good. In that book, I learned an important concept---modular programming. With the guidance of the Taiwanese engineer, I think my MCU programming level has made a qualitative leap. In future programming, I will no longer write comments because of laziness, and will not give up maintainable, portable, and easy-to-understand solutions just because they are simple. This has gradually formed a habit. In the second year, I wrote a product program of about 80KBYTE in C, and I think the logic is clear. As long as the customer says where to modify, I can respond immediately. Now I am used to modular programming. Whether it is an 8-bit MCU or a 32-bit MPU, I will do it according to this idea. I think to write a good, high-quality program, 1. Don't be afraid of trouble, write what should be written, and do what should be done. 2. Try to design in a modular way. Although it will waste time when writing small programs, it will help you develop the habit of modular programs. 3. It is best to read more books about programming written by foreigners. You will find many things you have seen before. 4. Study PSP and CMMI courses. The following is a file that I use queues to implement the CPU SCI bottom driver. I hope it can serve as a reference. It was first used on Freescale CPUs, and later ported to 51, h8/3062, Lingyang and other MCUs. The performance is quite good, saving a lot of time in product development. The boss is very happy, and I am also very happy. . #define SCI_DRIVER /********************************************************************************************** *Copyright: xxxxxCorporation * *File name: SciDriver.c * *Author: Kenny_wang * *Version: V1.0 * *Date: * ************************************************************************************************/ #include "SciDriver\SciDriver.h" #include "SciDriver\Source\FunLst.h" #include "SciDriver\Ports\Sciconfig.h" /************************************************************************** * Constant Define * **************************************************************************************/ #define cQBufNormal 0 #define cQBufFull 1 #define cQBufEmpty 2 /**************************************************************************** * Queue structure * ********************************************************************************/ typedef struct{ unsigned char *pIn; unsigned char *pOut; unsigned char *pStart; unsigned int bLength; unsigned int wSize; }QUEUE; transmit and receive queue structure and Tx,Rx threshold variabls *********************************************** ************************************/ typedef struct{ unsigned char bTxStatus; unsigned int wTxLength; unsigned char *pbTx ; QUEUE *pqRx; unsigned char bSciType; }SciStruct; /**************************************** ******************************************* * List of Sci structure and queue * *************************************************** ****************************/ SciStruct SciList; SciStruct *pSciIndex; QUEUE QList; unsigned char bSciRxBuf[MAX_SCI_BUF_SIZE]; unsigned char *pSciBuf = bSciRxBuf; unsigned char bSciNo = 0; /****************************************************** ******************************* * Internal Function Declaration * ************** *************************************************** ****************/ void sQInit(QUEUE *pq,unsigned char *pStart,unsigned int wSize); unsigned char sQDataIn(QUEUE *pq,unsigned char bData); unsigned char sQDataOut (QUEUE *pq,unsigned char *pData); /****************************************** **************************************** *Function name: sQInit * *Parameters: pq : pointer to queue structure to be initialized * * start: start address of ring buffer * * size: the size of the ring buffer * *Description: initialize a queue structure * ************** *************************************************** ****************/ void sQInit(QUEUE *pq,unsigned char *pStart,unsigned int wSize) { pq->pIn = pStart; pq->pOut = pStart; pq->pStart = pStart; pq->bLength = 0; pq->wSize = wSize; } /***** *************************************************** ************************* *Function name: sQDataIn * *Parameters: pq: pointer to queue structure to be initialized * * data: the data to be inserted into the queue * *Returns: cQBufNormal: data has been inserted into the queue * * cQBufFull: the buffer is full * *Description: insert a data into the queue * **** *************************************************** ****************************/ unsigned char sQDataIn(QUEUE *pq,unsigned char bData) { if(pq->bLength == pq->wSize) { if(pq->pIn == pq->pStart) { *(pq->pStart + pq->wSize) = bData; } else { *(pq->pIn-1) = bData; } return(cQBufFull); } else { *(pq->pIn) = bData; pq->bLength++; if(pq->pIn == pq->pStart + pq->wSize - 1) { pq->pIn = pq->pStart; } else { pq->pIn++; } return(cQBufNormal ); } } /********************************************** ************************************* *Function name: sQDataOut * *Parameters: pq: pointer to queue structure to be initialized * * pdata: the address to save the data * *Returns: cQBufNormal: data has been inserted into the queue * * cQBufEmpty: the buffer is empty * *Description: Get a data from the queue * ************************ *************************************************** ********/ unsigned char sQDataOut(QUEUE *pq,unsigned char *pData) { if(pq->bLength == 0) { return(cQBufEmpty); } *pData = *(pq->pOut); pq->bLength--; if(pq->pOut == pq->pStart + pq->wSize - 1) { pq->pOut = pq->pStart; } else { pq ->pOut++; } return(cQBufNormal); } /************************************** ****************************************** *Function Name: sInitialSci * *Parameters: bSciId: sci id * * *bRxBuf: receive buffer start address * * wRxSize: receive buffer length * * bTxBuf: transmit buffer start address * * wTxSize: transmit buffer length * * type: sci type * *Descriptions: assign and initialize the sci control struct to sci * *************************************************** ******************************/ void sInitialSci(unsigned int wRxSize,unsigned char bType) { QUEUE *pq; SciStruct *pSci; pSci = &SciList; pSciIndex = pSci; pSci->pqRx = &QList; pq = pSci->pqRx; sQInit(pq,pSciBuf,wRxSize); pSciBuf += wRxSize; cSciTxRdy; pSci->wTxLength = 0; pSci->bSciType = bType; } /************************************ ********************************************** *Function Name: sSciRxISR * *Parameters: bSciId: sci id * *Description: This function is executed in Sci rx interrupt io2sci rx compare * * interrupt.************************************************* *******************************/ void sSciRxISR(void) { unsigned char bData; QUEUE *pq; SciStruct *pSci; pSci = pSciIndex; pq = pSci->pqRx; if(sbGetSciRxRdy() == cSciRxRdy) { sSciResetRx(); bData = sbGetSciRxData(); sQDataIn(pq,bData); } } /************************************ ****************************************** *Function Name: sSciRead * *Parameters: bSciId: sci id * * *pBuf: address to save data received * *Returns: cSciRxBufEmpty: receive buffer is empty * * cSciRxRdy: get one byte data successfully * *Description: This function is executed in AP * **************************************** ****************************************/ unsigned char sSciRead(unsigned char *pBuf) { QUEUE *pq; unsigned char bTemp; SciStruct *pSci; pSci = pSciIndex; pq = pSci->pqRx; OS_ENTER_CRITICAL(); bTemp = sQDataOut(pq,pBuf); OS_EXIT_CRITICAL(); if(bTemp == cQBufEmpty) { return(cSciRxBufEmpty); } else { return(cSciRxRdy); } } /*************** *************************************************** *************** *Function Name: sSciTxISR * *Parameters: bSciId: sci id * *Description: This function is executed in Sci Tx interrupt io2sci Tx compare * * interrupt. * ************************************ ************************************************/ void sSciTxISR(void) { SciStruct *pSci; pSci = pSciIndex; if(sbGetSciTxRdy() == cSciTxRdy) { if(pSci->wTxLength == 0) { pSci->bTxStatus = cSciTxRdy; sSciResetTx(); } else { sSciTxData(*(pSci->pbTx)); (pSci->pbTx)++; (pSci->wTxLength)--; sSciResetTx(); } } } /****************************************************** ******************************* *Function Name: sSciWrite * *Parameters: bSciId: sci id * * *pstart: start address of data to be sent * * wLength: the length of data to be sent * *Returns: cSciTxBufFull: transmit buffer is empty * * cSciTxRdy: send one byte data successfully * *Description: This function is executed in AP * ******************************************* ***********************************/ unsigned char sSciWrite(unsigned char *pStart,unsigned int wLength) { SciStruct *pSci; pSci = pSciIndex; if(pSci->bTxStatus == cSciTxBusy) { return(cSciTxBusy); } OS_ENTER_CRITICAL(); pSci->pbTx = pStart; pSci->wTxLength = wLength; pSci->bTxStatus = cSciTxBusy; sSciTxData(*(pSci->pbTx)); (pSci->pbTx)++; (pSci->wTxLength)--; OS_EXIT_CRITICAL(); return(cSciTxRdy); } /*** *************************************************** *************************** *Function Name: sbGetSciTxStatus * *Parameters: bSciId: sci id * *Returns: sci tx status cSciTxRdy * * cSciTxBusy * *Description: Get the sci transmit status * ********************************** **********************************************/ unsigned char sbGetSciTxStatus (void) { SciStruct *pSci; pSci = pSciIndex; return(pSci->bTxStatus); } /****************************************************** ******************************* *Function Name: sSetSciBaudRate * *Parameters: bSciId: sci id * *Returns: bBaudrate Sci Baudrate * *Description: Set the sci baudrate * **************************************** ******************************************/ void sSetSciBaudRate(unsigned char bBaudrate) { sSciChangeBaudRate(bBaudrate); }
This post is from Embedded System

Latest reply

[Sorry, your operation will cause your Chip Coin to be lower than the lower limit of 0 coins specified by the system. Please return to correct it and submit again.] It seems that I need to reply to a few more posts first.  Details Published on 2008-11-27 13:08
Personal signature处处留心皆学问!

40

Posts

0

Resources
2
Haha, I've learned a lesson. Let's learn.
This post is from Embedded System

186

Posts

0

Resources
3
[Sorry, your operation will cause your Chip Coin to be lower than the lower limit of 0 coins specified by the system. Please return to correct it and submit again.] It seems that I need to reply to a few more posts first.
This post is from Embedded System

Guess Your Favourite
Find a datasheet?

EEWorld Datasheet Technical Support

Related articles more>>

    EEWorld
    subscription
    account

    EEWorld
    service
    account

    Automotive
    development
    circle

    Robot
    development
    community

    Copyright © 2005-2025 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
    快速回复 返回顶部 Return list