Application of Observer Mode in MCU

Publisher:忠正Latest update time:2016-05-17 Source: eefocusKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
environment:

Host: WIN8

Development environment: MDK5.13

mcu: stm32f103RB

 

illustrate:

The observer pattern was previously used in Java, and now the idea of ​​this pattern is applied to microcontroller programming

Android Programming: Observer Pattern Design:

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

 

The essence of the observer pattern:

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

In the program implementation process is:

1.A Generate data

2. A notifies B

3.B Processing Data

 

Method implemented in single chip microcomputer:

In Java, this idea is implemented through interfaces. If the microcontroller is programmed in C language, it can be implemented through function pointers.

The dw1000 communication module in the source code provides two observable targets: receive data and send data complete.

 

source code:

dw1000.h

 

/*
* dw1000 communication module header file
* (c) copyright 2015, jdh
* All Rights Reserved
*New creation time: 2015/1/5 by jdh
*Modified on: 2015/1/6 by jdh
/

#ifndef _DW1000_H_
#define _DW1000_H_

/*
* head File
/

#include "world.h"

/*
* Macro definition
/

/*
* Maximum length of receive data buffer
/

#define LEN_DW1000_BUF_RX 128

/*
* data structure
/

/*
* dw1000 time structure
/

union _Dw1000_Time
{
	uint8_t pt[8];
	uint64_t value;
};

/*
* Receive data structure
/

struct _Dw1000_Rx
{
	//Receive data
	uint8_t buf[LEN_DW1000_BUF_RX];
	//dw1000 time to receive data
	union _Dw1000_Time time;
};

/*
* Observer mode: receiving data processing function pointer
/

typedef void (*T_Dw1000_Deal_Rx)(struct _Dw1000_Rx);

/*
* Observer mode: Send data to complete the processing function pointer
/

typedef void (*T_Dw1000_Deal_Tx_End)(union _Dw1000_Time);

/*
* Receive buffer
/

struct _Rx_Buf_CC1100
{
	//Receive time
	T_Time time;
	
	//Source ID
	uint16_t src_id;
	//function code
	uint8_t cmd;
	//data
	uint8_t data[3];
	//rssi
	int rssi;
	//lqi
	uint8_t lqi;
};

/*
* Functions
/

/*
* Interface function: module loading
/

void dw1000_load(void);

/*
* Interface function: module operation
/

void dw1000_run(void);

/*
* Interface function: interrupt processing function
/

void dw1000_irq_handler(void);

/*
* Interface function: Determine whether it can be sent
*Return: 0: cannot be sent, 1: can be sent
/

uint8_t cc1100_judge_tx(void);

/*
* Interface function: send data
*Parameter: cmd: function code
* id: target id
* data: 3 bytes of data
/

void cc1100_tx(uint8_t cmd,uint16_t id,uint8_t *data);

/*
* Interface function: get received data
* Return: receive data
/

struct _Rx_Buf_CC1100 cc1100_get_rx_buf(void);

/*
* Interface function: set frequency
*Parameter: freq: the frequency point to be set
/

void cc1100_set_freq(uint8_t freq);

/*
* Interface function: Register observer: Receive data
/

void dw1000_register_observer_rx(T_Dw1000_Deal_Rx function);

/*
* Interface function: Register observer: Send completed
/

void dw1000_register_observer_tx_end(T_Dw1000_Deal_Tx_End function);

#endif



 

 

 

 

dw1000.c

 

/*
* dw1000 communication module main file
* (c) copyright 2015, jdh
* All Rights Reserved
*New creation time: 2015/1/5 by jdh
*Modified on: 2015/1/6 by jdh
/

/*
* head File
/

#include "dw1000.h"

/*
* Macro definition
/

/*
* Maximum number of observers
/

#define MAX_OBSERVER 10

/*
* Static variables
/

/*
* Receive data observer list
/

static T_Dw1000_Deal_Rx Observer_Rx[MAX_OBSERVER];
static uint8_t Len_Observer_Rx = 0;

/*
* Send the completed observer list
/

static T_Dw1000_Deal_Tx_End Observer_Tx_End[MAX_OBSERVER];
static uint8_t Len_Observer_Tx_End = 0;

/*
* Static functions
/

/*
* Receive and process
/

static void deal_rx(void);

/*
* Send end processing
/

static void deal_tx_end(void);

/*
* Functions
/

/*
* Interface function: module loading
/

void dw1000_load(void)
{
    
}

/*
* Interface function: module operation
/

void dw1000_run(void)
{
	
}

/*
* Interface function: Register observer: Receive data
/

void dw1000_register_observer_rx(T_Dw1000_Deal_Rx function)
{
	Observer_Rx[Len_Observer_Rx++] = function;
}

/*
* Interface function: Register observer: Send completed
/

void dw1000_register_observer_tx_end(T_Dw1000_Deal_Tx_End function)
{
	Observer_Tx_End[Len_Observer_Tx_End++] = function;
}

/*
* Interface function: interrupt processing function
/

void dw1000_irq_handler(void)
{
	uint32_t status = 0;
	uint32_t clear = 0; // will clear any events seen
	uint8_t resetrx;
	
	status = dwt_read32bitreg(SYS_STATUS_ID); // read status register low 32bit
	if(status & SYS_STATUS_LDEDONE)
	{
		if((status & (SYS_STATUS_LDEDONE | SYS_STATUS_RXPHD | SYS_STATUS_RXSFDD)) != (SYS_STATUS_LDEDONE | 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 transceiver
			dwt_forcetrxoff(); //this will clear all events
			//set rx reset
			dwt_writetodevice(PMSC_ID, 0x3, 1, &resetrx);
			resetrx = 0xf0; //clear RX reset
			dwt_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)) // Receiver 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 and process
		deal_rx();
    }
	else
	{
		if (status & SYS_STATUS_TXFRS) // Transmit Frame Sent
		{
			clear |= CLEAR_ALLTX_EVENTS; //clear TX event bits
			dwt_write32bitreg(SYS_STATUS_ID,clear); // write status register to clear event bits we have seen
			//Send end processing
			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 seen
				dwt_setrxtimeout(0);
				dwt_rxenable(0);
			}
			else
			{
				// Clear all flags when exception occurs					 
				clear |= CLEAR_ALLRXERROR_EVENTS;
				dwt_write32bitreg(SYS_STATUS_ID,clear); // write status register to clear event bits we have seen
				dwt_forcetrxoff(); //this will clear all events
				//set rx reset
				dwt_writetodevice(PMSC_ID, 0x3, 1, &resetrx);
    		    resetrx = 0xf0; //clear RX reset
		        dwt_writetodevice(PMSC_ID, 0x3, 1, &resetrx);
				dwt_rxenable(0);	 
			}
		}
	}
}

/*
* Receive and process
/

static void deal_rx(void)
{
	struct _Dw1000_Rx rx;
	uint16_t len;
	uint8_t i = 0;
	
	len = dwt_read16bitoffsetreg(RX_FINFO_ID, 0) & 0x3FF;
	if (len >= 127)
	{
		return;
	}
	
	dwt_write32bitreg(SYS_STATUS_ID,CLEAR_ALLRXGOOD_EVENTS);
	//Get the receiving time
	dwt_readrxtimestamp(rx.time.pt);
	rx.time.value &= MASK_40BIT;
	//Get received data
	dwt_readfromdevice(RX_BUFFER_ID,0,len,rx.buf);
	
	//Notify observers
	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);			
}

/*
* Send end processing
/

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;	

	//Notify observers
	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 observer to receive data
	dw1000_register_observer_rx(deal_rx);

 

 

 

 

 

Processing function:

 

void deal_rx(struct _Dw1000_Rx rx)
 {
	 //deal with...
	 __nop();
	 __nop();
	 __nop();
 }
 
Keywords:MCU Reference address:Application of Observer Mode in MCU

Previous article:Full tutorial on developing an optical fingerprint recognition module (FPM10A) based on the STM32 microcontroller
Next article:S3C2440 Mini 2440 DMA mode to achieve Uart serial communication

Recommended ReadingLatest update time:2024-11-16 17:29

Digital PID based on single chip microcomputer to realize DC motor speed regulation (smart car)
Nowadays, many smart cars need to control the speed of the car. Whether it is a constant speed or a variable speed, in the speed control algorithm, the classic PID will live forever.               In fact, a simple 51 single-chip microcomputer can easily achieve smooth and good speed regulation, not to mention a singl
[Microcontroller]
Using a small MCU to achieve color control of LED lighting
LEDs are now ready for general lighting. LEDs offer many advantages in general lighting systems, such as longer life and higher efficiency. However, LED technology faces several challenges. One of these challenges is producing high quality white light. White LEDs consist of a blue LED and a phosphor that shifts the
[Power Management]
Using a small MCU to achieve color control of LED lighting
51 microcontroller control stepper motor hardware connection part
1. Summary: This case explains the hardware connection part of the stepper motor controlled by the 51 microcontroller. Later, we will explain the microcontroller program, S-curve acceleration and deceleration method, host computer and other related content. 2. Functional schematic diagram: 2.1, 51 microcontroller: ①
[Microcontroller]
51 microcontroller control stepper motor hardware connection part
Use of built-in EEPROM of STC12C5A60S2 microcontroller
EEPROM can be used to store some data that needs to be preserved after power failure. The STC12C5A60S2 microcontroller has 1k byte of EEPROM inside, with 2 sectors. When programming, please note that all data in one sector must be written in together, even if they are not changed. Tested code: #include STC12C5A.H #in
[Microcontroller]
Microcontroller Basics (IV): C51 Extensions to C Language
C51's extension of C language C51 differs from standard C language in terms of data structure, I/O processing, functions, etc. data structure sfr, sfr16 Special function register type variables Special Function Register (SFR) plays an important role in microcontrollers. Common SFR is 8-bit, so sfr16 is used to r
[Microcontroller]
Microcontroller Basics (IV): C51 Extensions to C Language
Design of information monitoring terminal using AVR single chip microcomputer and LCD module
At present, with the rapid development of the information industry, all kinds of ships are in urgent need of being equipped with more high-quality and low-cost small and medium-sized information monitoring terminals to monitor weather conditions and obtain navigation information to ensure navigation safety. 32-bit pro
[Microcontroller]
Design of information monitoring terminal using AVR single chip microcomputer and LCD module
The realization principle and method of automatic temperature control system for foot bath based on single chip microcomputer
    The difficulty in designing a foot bather lies in cost control and the design of a temperature control system. In recent years, the gradual maturity of switching power supply technology has provided a high-efficiency and low-cost solution for low-power power supply, abandoning the traditional low-efficiency power
[Microcontroller]
The realization principle and method of automatic temperature control system for foot bath based on single chip microcomputer
Analysis of the driving capability and pull-up resistor of 51 series MCU IO pins
When the microcontroller outputs a low level, external devices are allowed to input current into the microcontroller pins. This current is called "injection current" and the external circuit is called "injection current load". When the microcontroller outputs a high level, external devices are allowed to pull curren
[Microcontroller]
Analysis of the driving capability and pull-up resistor of 51 series MCU IO pins
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号