Observer mode and observer mode in Single Chip Microcomputer

Source: Internet
Author: User

Observer mode and observer mode in Single Chip Microcomputer

Observer Mode Applied in Single Chip Microcomputer


For more information, see http://blog.csdn.net/jdh99, :jdh.


Environment:

HOST: WIN8

Development Environment: MDK5.13

Mcu: stm32f103RB


Note:

Previously, the observer mode was applied in java. Now, the idea of this mode is applied in the single-chip program design.

Android programming: Observer mode design:

Http://blog.csdn.net/jdh99/article/details/41821295


Observer Pattern nature:

There are two modules A and B. A is the target and B is the observer. Then B can observe the changes of.

In program implementation, the process is:

1. A generate data

2. A notifies B

3. B. Process Data


Methods implemented in single-chip microcomputer:

Java implements this idea through interfaces. If the single-chip microcomputer is programmed in C language, it can be implemented through function pointers.

In the source code, the dw1000 communication module provides two observed goals: to receive and send data.


Source code:

Dw1000.h

/*************************************** * ****************************** Dw1000 communication module header file * (c) copyright 2015, jdh * All Right Reserved * Creation Time: 2015/1/5 by jdh * modification time: by jdh ************************************* * *******************************/# ifndef _ DW1000_H _# define _ DW1000_H _/*********************************** * *********************************** header file **** **************************************** * *************************/# include "world. h "/************************************* ****** **************************************** ************************//************** **************************************** ******************* **************************************** * **********/# define LEN_DW1000_BUF_RX128 /************************ **************************************** ******************************** **************************************// **************************************** ******* **************************************** * ********************/union _ Dw1000_Time {uint8_t pt [8]; uint64_t value ;}; /*************************************** ****** **************************************** * *********************/struct _ Dw1000_Rx {// receive data uint8_t buf [LEN_DW1000_BUF_RX]; // The dw1000 time for receiving data union _ Dw1000_Time time ;}; /*************************************** * ****************************** observer mode: receive data processing function pointer ************************************ * ********************************/typedef void (* T_Dw1000_Deal_Rx) (struct _ Dw1000_Rx ); /*************************************** * ****************************** observer mode: function pointer ********************************** * **********************************/typedef void (* T_Dw1000_Deal_Tx_End) (union _ Dw1000_Time ); /*************************************** ******* **************************************** * ********************/struct _ Rx_Buf_CC1100 {// receipt time T_Time time; // source IDuint16_t src_id; // Function Code uint8_t cmd; // data uint8_t data [3]; // rssiint ARG; // lqiuint8_t lqi ;}; /*************************************** ******** **************************************** **********************//**************** **************************************** * ************* interface functions: module loading ************************************** * ******************************/void dw1000_load (void ); /*************************************** ******************************* interface functions: module running ************************************** * ******************************/void dw1000_run (void ); /*************************************** ******************************* interface functions: the interrupt processing function ************************************* * *******************************/void dw1000_irq_handler (void ); /*************************************** ******************************* interface functions: determines whether it can be sent * return: 0: not allowed, 1: you can send ************************************** * *****************************/uint8_t cc1100_judge_tx (void ); /*************************************** ******************************* interface functions: send data * parameter: cmd: Function Code * id: target id * data: 3-byte data ************************************* * ******************************/void cc1100_tx (uint8_t cmd, uint16_t id, uint8_t * data ); /*************************************** ******************************* interface functions: get received data * return: receive data ************************************** * *****************************/struct _ Rx_Buf_CC1100 cc1100_get_rx_buf (void); /*************************************** ******************************* interface functions: set frequency * parameter: freq: frequency point to be set ************************************ * ********************************/void cc1100_set_freq (uint8_t freq ); /*************************************** ******************************* interface functions: registered observer: receive data ************************************** * *****************************/void dw1000_register_observer_rx (T_Dw1000_Deal_Rx function ); /*************************************** ******************************* interface functions: registered observer: ************************************** * *****************************/void dw1000_register_observer_tx_end (T_Dw1000_Deal_Tx_End function ); # endif

Dw1000.c

/*************************************** * ****************************** Main file of the dw1000 communication module * (c) copyright 2015, jdh * All Right Reserved * Creation Time: 2015/1/5 by jdh * modification time: by jdh ************************************* *********************************//***** **************************************** * ************************ header file ************** **************************************** * ***************/# include "dw1000. H "/************************************* ****** **************************************** ************************//************** **************************************** * *************** maximum number of observers ********************* **************************************** * ********/# define MAX_OBSERVER10 /************************** **************************************** * *** static variables ****** **************************************** ************************//************** **************************************** ******************** **************************************** * ********/Static T_Dw1000_Deal_Rx Observer_Rx [MAX_OBSERVER]; static uint8_t Len_Observer_Rx = 0; /*************************************** ********************************** list of sent completion observers ***** ************** **************************************** * **********/Static T_Dw1000_Deal_Tx_End Observer_Tx_End [MAX_OBSERVER]; static uint8_t Len_Observer_Tx_End = 0; /*************************************** ******* **************************************** ***********************//*************** **************************************** *********************** ******* **************************************** /Static void deal_rx (void ); /*************************************** ****** **************************************** * **********************/static void deal_tx_end (void ); /*************************************** ******** **************************************** **********************//********** **************************************** * ******************** Interface functions: module loading ************************************** * ******************************/void dw1000_load (void) {}/************************************* ********************************** interface functions: module running ************************************** * ******************************/void dw1000_run (void) {}/************************************* *********************** * ********* Interface function: register the observer: receive data ************************************** * *****************************/void dw1000_register_observer_rx (T_Dw1000_Deal_Rx function) {Observer_Rx [Len_Observer_Rx ++] = function ;} /*************************************** ******************************* interface functions: registered observer: ************************************** * *****************************/void dw1000_register_observer_tx_end (T_Dw100 0_Deal_Tx_End function) {Observer_Tx_End [Len_Observer_Tx_End ++] = function ;} /*************************************** ******************************* interface functions: the interrupt processing function ************************************* * *******************************/void dw1000_irq_handler (void) {uint32_t status = 0; uint32_t clear = 0; // will clear any events seenuint8_t resetrx; status = dwt_read32bitreg (SYS_STATUS_ID); // read status Register low 32 bitif (status & SYS_STATUS_LDEDONE) {if (status & (SYS_STATUS_LDEDONE | SYS_STATUS_RXPHD | SYS_STATUS_RXSFDD ))! = (Feature | SYS_STATUS_RXPHD | SYS_STATUS_RXSFDD) {resetrx = 0xe0; // got LDE done but other flags SFD and PHR are clear-this is a bad frame-reset the feature (); // this will clear all events // set rx resetdwt_writetodevice (PMSC_ID, 0x3, 1, & resetrx); resetrx = 0xf0; // clear RX resetdwt_writetodevice (PMSC_ID, 0x3, 1, & resetrx); // dwt_write16bitoffsetreg (SYS_CTRL_ID, 0, (uint16) SYS_CTRL_RXENAB) ;}} if (status & SYS_STATUS_RXFCG) & (status & SYS_STATUS_LDEDONE) // reset er FCS Good {// clear all receive status bits (as we are finished with this receive event) clear | = status & CLEAR_ALLRXGOOD_EVENTS; dwt_write32bitreg (SYS_STATUS_ID, clear); // write status register to clear event bits we have seen // receive processing deal_rx ();} else {if (status & sys_status_txfr) // Transmit Frame Sent {clear | = CLEAR_ALLTX_EVENTS; // clear TX event bitsdwt_write32bitreg (SYS_STATUS_ID, clear ); // write status register to clear event bits we have seen // deal_tx_end ();} else {if (status & SYS_STATUS_RXRFTO) {// receive timeout clear | = status & SYS_STATUS_RXRFTO; dwt_write32bitreg (SYS_STATUS_ID, clear); // write status register to clear event bits we have seendwt_setrxtimeout (0 ); dwt_rxenable (0);} else {// clear all identifiers clear | = CLEAR_ALLRXERROR_EVENTS; dwt_write32bitreg (SYS_STATUS_ID, clear ); // write status register to clear event bits we have seendwt_forcetrxoff (); // this will clear all events // set rx resetdwt_writetodevice (PMSC_ID, 0x3, 1, & resetrx ); resetrx = 0xf0; // clear RX reset dwt_writetodevice (PMSC_ID, 0x3, 1, & resetrx); dwt_rxenable (0 );}}}} /*************************************** ******* **************************************** * *********************/static void deal_rx (void) {struct _ Dw1000_Rx rx; uint16_t len; uint8_t I = 0; len = listen (RX_FINFO_ID, 0) & 0x3FF; if (len> = 127) {return;} dwt_write32bitreg (SYS_STATUS_ID, CLEAR_ALLRXGOOD_EVENTS); // get the receipt time dwt_readrxtimestamp (rx.time.pt); rx. time. value & = MASK_40BIT; // get the received data dwt_readfromdevice (RX_BUFFER_ID, 0, len, rx. buf); // notify the observer for (I = 0; I <Len_Observer_Rx; I ++) {Observer_Rx [I] (rx);} // uint8 resetrx; ///// resetrx = 0xe0; // got LDE done but other flags SFD and PHR are clear-this is a bad frame-reset the transceiver // dwt_forcetrxoff (); // this will clear all events /// dwt_writetodevice (PMSC_ID, 0x3, 1, & resetrx); // set rx reset /// resetrx = 0xf0; // clear RX reset // dwt_writetodevice (PMSC_ID, 0x3, 1, & resetrx); dwt_write16bitoffsetreg (SYS_CTRL_ID, 0, (uint16) SYS_CTRL_RXENAB );} /*************************************** ****** **************************************** * **********************/static void deal_tx_end (void) {union _ Dw1000_Time time; uint8_t I = 0; // get the sending time dwt_readtxtimestamp (time.pt); time. value & = MASK_40BIT; // notification observer for (I = 0; I <Len_Observer_Tx_End; I ++) {Observer_Tx_End [I] (time );}}

Observe the received data of the dw1000 module in main. c:

// Add the received data observer dw1000_register_observer_rx (deal_rx );


Processing functions:

Void deal_rx (struct _ Dw1000_Rx rx) {// process... _ nop ();}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.