In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you how to use C language to achieve CC2530 host computer under Linux, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
A brief introduction to the environment
1. Software and hardware environment
Lower computer: CC2530 OS:vmware + ubuntu
Teacher Peng uses CC2530 here, and readers can also use other boards. All we need is that the board has a serial port and can communicate with PC. At the same time, there are configurable led lights, relays and sensors that can collect data on the board.
two。 Hardware connection diagram
The hardware connection diagram is as follows:
The CC2530 has been integrated with a CH340 chip, and the USB line can be connected to the computer to be identified.
3. Identify serial port under pc
If the serial port is obtained by PC, the name is COMn [n is an integer].
Serial port under windows
4. Identify serial port under ubuntu
First of all, you need to grab the serial port by vmware (the serial port is either grabbed by windows or vmware at the same time). Click the connection as shown below:
Virtual machine grabs serial port
But often there is no ch440 driver in ubuntu, after actual testing, ubuntu14 and previous versions do not have this driver, ubuntu16 and above have this driver.
If you do not have a ch440 driver, you can install the corresponding driver in the following ways:
1 make 2 sudo make load 3 ls / dev/ttyUSB0
Ubuntu installs serial port driver
Following the above steps, the device file * * / dev/ttyUSB0** is generated.
Ls / dev/ttyUSB0-l crw-rw---- 1 root dialout 188,0 Jan 15 05:45 / dev/ttyUSB0
C: character device rw-rw----: file operation permission
188, 0: primary and secondary device number
The usb to serial port driver mentioned in sections 3 and 4 and the driver source code under linux can be obtained by replying to ch440 in the background [GH].
[note] if it is another development board, install other serial port drivers by yourself.
Second, module design
The communication between the upper computer and the lower computer is often through the serial port, and the character device ttyUSB0 [some ttyS0] is often generated under linux, so it is only necessary to operate the character device.
Next we design the software module of the upper and lower computer.
1. Signalling
In order to design the upper computer, we first need to design the instruction format sent by the upper computer to the lower computer. The upper computer sends commands to the lower computer according to the instruction format, and the lower computer needs to parse the instructions strictly according to the instruction format.
The meaning is as follows:
Device: the device to operate
Data: the corresponding device and its additional data
CRC: check code
#: signaling Terminator
The signaling format can be extended or simplified as needed.
The definition of device is as follows [can be expanded according to actual situation]:
# define DEV_ID_LED_ON 0X1 # define DEV_ID_LED_OFF 0X2 # define DEV_ID_DELAY 0X3 # define DEV_ID_GAS 0X4
[note] for the sake of understanding, we will not consider the question of efficiency for the time being.
two。 Upload data
The lower computer needs to collect the data of the sensor and upload it through the serial port. The data structure is defined as follows:
Struct data {unsigned char device; unsigned char crc; unsigned short data;}
Device equipment
Data collected by data
Crc check code
3. Function module
Now you can start to design the various functional modules of the software.
Lower computer
Flow chart of lower computer
The main task of the lower level is to receive the data sent by the upper computer through the serial port, and then parse the instruction content and operate the corresponding hardware.
Upper computer
Upper computer
The main task of the upper computer is to print the menu, which is selected by the user, then encapsulates the command according to the instruction format, and sends the command to the lower computer through the serial port.
Third, the function of the lower computer
The operation principle of cc2530 is not discussed in this paper. If it is another development board, it only needs to modify the serial port operation function.
1. LED initialization
/ * name: InitLed () * function: set the IO port corresponding to the LED lamp * entry parameters: none * out Port parameter: none * / void InitLed (void) {P1DIR | = 0x01 / / P1.0 is defined as output LED1 = 0;}
two。 Initialize UART
/ * name: InitUart () * function: serial port initialization function * entry parameter: none * exit parameter: none * * * / void InitUart (void) {PERCFG = 0x00 / / the IO position of the peripheral control register USART 0: 0 is the P0 port position 1 P0SEL = 0x0c; / / P0x02Power3 is used as the serial port (peripheral function) P2DIR & = ~ 0xC0; / / P0 is preferred as UART0 U0CSR | = 0x80; / / set to UART mode U0GCR | = 11; U0BAUD | = 216 / / the baud rate is set to 115200 UTX0IF = 0; / / the UART0 TX interrupt flag is initially set to 0 U0CSR | = 0x40; / / allow to receive IEN0 | = 0x84; / / Total interrupt allows to receive interrupt}
3. Serial port sending function
/ * name: UartSendString () * function: serial port send function * entry parameter: Data: send buffer len: send length * Export parameters: none * / void UartSendString (char * Data) Int len) {uint I For (iTun0; I = 8; return (reading);}
6. LED lamp control function
/ * name: led_opt () * function: LED lamp control function * entry parameter: RxData: received instruction flage:led operation Turn on or off * exit parameter: none * / void led_opt (char RxData []) Unsigned char flage) {switch (RxData [1]) {case 1: LED1 = (flage==DEV_ID_LED_ON)? ON:OFF Break; / * TBD for led2 led3*/ default: break;} return;}
7. Main program
/ * main program entry function * * * / void main (void) {CLKCONCMD & = ~ 0x40 / / set the clock source of the system to 32MHZ crystal oscillator while (CLKCONSTA & 0x40); / / wait for the crystal oscillator to stabilize to 32m CLKCONCMD & = ~ 0x47; / / set the system main clock frequency to 32MHZ InitLed (); / / set the corresponding IO port InitUart () of LED l / / Serial port initialization function UartState = UART0_RX / / Serial port 0 is in receive mode memset (RxData, 0, SIZE) by default While (1) {/ / receive status if (UartState = = UART0_RX) {/ / read data If you encounter a character'#'or the number of buffer characters exceeds 4, set UartState to CONTROL_DEV status if (RxBuf! = 0) {/ / take'#'as the Terminator, and receive a maximum of 4 characters if ((RxBuf! ='#') & & (count) at a time
< 4)) { RxData[count++] = RxBuf; } else { //判断数据合法性,防止溢出 if(count >= 4) {/ / clear 0 count = 0; / / clear the receive buffer memset (RxData, 0, SIZE);} else {/ / enter the sending state UartState = CONTROL_DEV;}} RxBuf = 0 }} / / Control Peripheral status if (UartState = = CONTROL_DEV) {/ / determine the validity of received data / / RxData []: | device | data | crc | # | / / check_crc: crc = device ^ data / / if (RxData [2] = (RxData [0] ^ RxData [1])) {switch (RxData [0]) {case DEV_ID_LED_ON: led_opt (RxData) DEV_ID_LED_ON) Break; case DEV_ID_LED_OFF: led_opt (RxData,DEV_ID_LED_OFF); break; case DEV_ID_DELAY: break; case DEV_ID_GAS: send_gas (); break; default: break;}} UartState = UART0_RX; count = 0 / / clear the receive buffer memset (RxData, 0, SIZE);}
Fourth, the function of the upper computer
Structural body
# define DEV_ID_LED_ON 0X1 # define DEV_ID_LED_OFF 0X2 # define DEV_ID_DELAY 0X3 # define DEV_ID_GAS 0X4 struct data {unsigned char device; unsigned char crc; unsigned short data;}
Function
Void uart_init (void) {int nset1,nset2; serial_fd = open ("/ dev/ttyUSB0", O_RDWR); if (serial_fd = =-1) {printf ("open () error\ n"); exit (1);} nset1 = set_opt (serial_fd, 115200, 8, 'Number1); if (nset2 = =-1) {printf ("set_opt () error\ n"); exit (1) }} int Menu () {int option; system ("clear"); printf ("\ n\ t\ tasking *\ n"); printf ("\ n\ t\ tasking * ALARM SYSTERM * *\ n") Printf ("\ n\ t\ tasking * 1----LED * *\ n"); printf ("\ n\ t\ tasking * 2----GAS * *\ n"); printf ("\ n\ t\ tasking * 0----EXIT * *\ n") Printf ("\ n\ t\ t choose error *\ n"); while (1) {printf ("Please choose what you want:"); scanf ("% d", & option); if (option2) printf ("\ t\ t choose error!\ n"); else break } return option;} / / RxData []: | device | data | crc | # | void led () {int lednum = 0; int onoff; char cmd [4]; / / Select led lamp while (1) {printf ("input led number: [1 2]\ n #"); scanf ("% d", & lednum); / / check if (lednum2) {printf ("invalid led number\ n"); system ("clear"); continue } else {break;}} printf ("operation: 1 on, 0 off\ n"); scanf ("% d", & onoff); if (onoff = = 1) {cmd [0] = DEV_ID_LED_ON;} else if (onoff = 0) {cmd [0] = DEV_ID_LED_OFF;} else {printf ("invalid led number\ n"); return;} cmd [1] = lednum / / fulfill crc area cmd [2] = cmd [0] ^ cmd [1]; cmd [3] ='#'; / / indicates the Terminator tcflush (serial_fd, TCIOFLUSH); int ionom0; for
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.