5 | *
6 | * This program is free software: you can redistribute it and/or modify
7 | * it under the terms of the GNU General Public License as published by
8 | * the Free Software Foundation, either version 3 of the License, or
9 | * (at your option) any later version.
10 | *
11 | * This program is distributed in the hope that it will be useful,
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | * GNU General Public License for more details.
15 | *
16 | * You should have received a copy of the GNU General Public License
17 | * along with this program. If not, see .
18 | */
19 |
20 | #include "shell.h"
21 |
22 | int shell_str_len(char *str)
23 | {
24 | int i = 0;
25 |
26 | while(str[i++] != 0);
27 |
28 | return i - 1;
29 | }
30 |
31 | int shell_str_cmp(char *str1, char *str2, int len1, int len2)
32 | {
33 | int i;
34 |
35 | if(len1 > len2) return 1;
36 |
37 | for(i = 0; i < len1; i++) {
38 | if(str1[i] != str2[i]) return 2;
39 | }
40 |
41 | // make sure we matched a whole command, and not only
42 | // a containing substring ...
43 | if(len2 > len1 && str2[i] != ' ') {
44 | return 2;
45 | }
46 |
47 | return 0;
48 | }
49 |
50 | int shell_parse_int(char *str)
51 | {
52 |
53 | int val = 0;
54 | int i = 0;
55 |
56 | char c;
57 |
58 | while((c = str[i++]) != 0) {
59 |
60 | if(c < '0' || c > '9') break;
61 |
62 | val = val * 10 + (c - '0');
63 | }
64 |
65 | return val;
66 | }
67 |
68 | int shell_arg_parser(char *cmd_line, int len, shell_cmd_args *args)
69 | {
70 | int i;
71 | int j;
72 | int spos = 0;
73 | int argc = 0;
74 |
75 | for(i = 0; i < len; i++) {
76 | // to many arguments
77 | if(argc > SHELL_MAX_ARGS) return 1;
78 |
79 | if(cmd_line[i] == ' ' || i == len - 1) {
80 | // catch last argument ...
81 | if(i == len - 1) i++;
82 |
83 | // ignore first since it is the cmd itself
84 | if(spos == 0) {
85 | spos = i;
86 | } else {
87 | // argument value to long
88 | if(i - spos > SHELL_MAX_ARG_LEN) return 2;
89 |
90 | for(j = 0; j < i - spos - 1; j++) {
91 | args->args[argc].val[j] = cmd_line[spos + 1 + j];
92 | }
93 | args->args[argc++].val[j] = 0;
94 | spos = i;
95 | }
96 | }
97 | }
98 |
99 | args->count = argc;
100 |
101 | return 0;
102 | }
103 |
104 | int shell_process_cmds(shell_cmds *cmds, char *cmd_line)
105 | {
106 | int i;
107 | int ret;
108 | int cmd_len;
109 | int cmd_line_len;
110 |
111 | shell_cmd_args args;
112 |
113 | for(i = 0; i < cmds->count; i++) {
114 |
115 | cmd_line_len = shell_str_len(cmd_line);
116 | cmd_len = shell_str_len((char *)(cmds->cmds[i].cmd));
117 |
118 | if(shell_str_cmp((char *)(cmds->cmds[i].cmd), cmd_line, cmd_len, cmd_line_len) == 0) {
119 | ret = shell_arg_parser(cmd_line, cmd_line_len, &args);
120 |
121 | if(ret == 1)
122 | return SHELL_PROCESS_ERR_ARGS_MAX;
123 | if(ret == 2)
124 | return SHELL_PROCESS_ERR_ARGS_LEN;
125 |
126 | return (cmds->cmds[i].func)(&args);
127 | }
128 | }
129 |
130 | return SHELL_PROCESS_ERR_CMD_UNKN;
131 | }
132 |
--------------------------------------------------------------------------------
/stm32/StdPeriph/zengjf/spi_master.c:
--------------------------------------------------------------------------------
1 | #include "zengjf.h"
2 |
3 | /* SPI Driver */
4 | extern ARM_DRIVER_SPI Driver_SPI1;
5 | osSemaphoreId_t spi_Thread_Semaphore;
6 |
7 | void mySPI_callback(uint32_t event)
8 | {
9 | switch (event)
10 | {
11 | case ARM_SPI_EVENT_TRANSFER_COMPLETE:
12 | /* Success: Wakeup Thread */
13 | osSemaphoreRelease (spi_Thread_Semaphore);
14 | break;
15 | case ARM_SPI_EVENT_DATA_LOST:
16 | /* Occurs in slave mode when data is requested/sent by master
17 | but send/receive/transfer operation has not been started
18 | and indicates that data is lost. Occurs also in master mode
19 | when driver cannot transfer data fast enough. */
20 | __breakpoint(0); /* Error: Call debugger or replace with custom error handling */
21 | break;
22 | case ARM_SPI_EVENT_MODE_FAULT:
23 | /* Occurs in master mode when Slave Select is deactivated and
24 | indicates Master Mode Fault. */
25 | __breakpoint(0); /* Error: Call debugger or replace with custom error handling */
26 | break;
27 | }
28 | }
29 |
30 | /* Test data buffers */
31 | uint8_t testdata_out[8] = { 0, 1, 2, 3, 4, 5, 6, 7 };
32 | uint8_t testdata_in [8];
33 |
34 | void vTaskSPI1(void* arg)
35 | {
36 | spi_Thread_Semaphore = osSemaphoreNew(1, 0, NULL);
37 | if (!spi_Thread_Semaphore) {
38 | printf("get spi_Thread_Semaphore error.\r\n"); // Semaphore object not created, handle failure
39 | }
40 |
41 | ARM_DRIVER_SPI* SPIdrv = &Driver_SPI1;
42 |
43 | /* Initialize the SPI driver */
44 | SPIdrv->Initialize(mySPI_callback);
45 | /* Power up the SPI peripheral */
46 | SPIdrv->PowerControl(ARM_POWER_FULL);
47 | /* Configure the SPI to Master, 8-bit mode @10000 kBits/sec */
48 | SPIdrv->Control(ARM_SPI_MODE_MASTER | ARM_SPI_CPOL1_CPHA1 | ARM_SPI_MSB_LSB | ARM_SPI_SS_MASTER_SW | ARM_SPI_DATA_BITS(8), 10000000);
49 |
50 | /* SS line = INACTIVE = HIGH */
51 | SPIdrv->Control(ARM_SPI_CONTROL_SS, ARM_SPI_SS_INACTIVE);
52 |
53 | /* thread loop */
54 | while (1)
55 | {
56 | /* SS line = ACTIVE = LOW */
57 | SPIdrv->Control(ARM_SPI_CONTROL_SS, ARM_SPI_SS_ACTIVE);
58 | /* Transmit some data */
59 | SPIdrv->Transfer(testdata_out, testdata_in, sizeof(testdata_out));
60 | /* Wait for completion */
61 | osSemaphoreAcquire (spi_Thread_Semaphore, osWaitForever);
62 | /* SS line = INACTIVE = HIGH */
63 | SPIdrv->Control(ARM_SPI_CONTROL_SS, ARM_SPI_SS_INACTIVE);
64 |
65 | int i = 0;
66 | for (i = 0; i < sizeof(testdata_in) / sizeof(testdata_in[0]); i++) {
67 | printf("%d\n\r", testdata_in[i]);
68 | testdata_out[i] += 1;
69 | }
70 |
71 | osDelay(1000);
72 | }
73 | }
74 |
--------------------------------------------------------------------------------
/stm32/StdPeriph/zengjf/stm32f10x_it.c:
--------------------------------------------------------------------------------
1 | /**
2 | ******************************************************************************
3 | * @file Project/STM32F10x_StdPeriph_Template/stm32f10x_it.c
4 | * @author MCD Application Team
5 | * @version V3.5.0
6 | * @date 08-April-2011
7 | * @brief Main Interrupt Service Routines.
8 | * This file provides template for all exceptions handler and
9 | * peripherals interrupt service routine.
10 | ******************************************************************************
11 | * @attention
12 | *
13 | * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
14 | * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
15 | * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
16 | * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
17 | * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
18 | * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
19 | *
20 | * © COPYRIGHT 2011 STMicroelectronics
21 | ******************************************************************************
22 | */
23 |
24 | /* Includes ------------------------------------------------------------------*/
25 | #include "zengjf.h"
26 |
27 | /** @addtogroup STM32F10x_StdPeriph_Template
28 | * @{
29 | */
30 |
31 | /* Private typedef -----------------------------------------------------------*/
32 | /* Private define ------------------------------------------------------------*/
33 | /* Private macro -------------------------------------------------------------*/
34 | /* Private variables ---------------------------------------------------------*/
35 | /* Private function prototypes -----------------------------------------------*/
36 | /* Private functions ---------------------------------------------------------*/
37 |
38 | /******************************************************************************/
39 | /* Cortex-M3 Processor Exceptions Handlers */
40 | /******************************************************************************/
41 |
42 | /**
43 | * @brief This function handles NMI exception.
44 | * @param None
45 | * @retval None
46 | */
47 | void NMI_Handler(void)
48 | {
49 | }
50 |
51 | /**
52 | * @brief This function handles Hard Fault exception.
53 | * @param None
54 | * @retval None
55 | */
56 | void HardFault_Handler(void)
57 | {
58 | /* Go to infinite loop when Hard Fault exception occurs */
59 | while (1)
60 | {
61 | }
62 | }
63 |
64 | /**
65 | * @brief This function handles Memory Manage exception.
66 | * @param None
67 | * @retval None
68 | */
69 | void MemManage_Handler(void)
70 | {
71 | /* Go to infinite loop when Memory Manage exception occurs */
72 | while (1)
73 | {
74 | }
75 | }
76 |
77 | /**
78 | * @brief This function handles Bus Fault exception.
79 | * @param None
80 | * @retval None
81 | */
82 | void BusFault_Handler(void)
83 | {
84 | /* Go to infinite loop when Bus Fault exception occurs */
85 | while (1)
86 | {
87 | }
88 | }
89 |
90 | /**
91 | * @brief This function handles Usage Fault exception.
92 | * @param None
93 | * @retval None
94 | */
95 | void UsageFault_Handler(void)
96 | {
97 | /* Go to infinite loop when Usage Fault exception occurs */
98 | while (1)
99 | {
100 | }
101 | }
102 |
103 | /**
104 | * @brief This function handles Debug Monitor exception.
105 | * @param None
106 | * @retval None
107 | */
108 | void DebugMon_Handler(void)
109 | {
110 | }
111 |
112 | /******************************************************************************/
113 | /* STM32F10x Peripherals Interrupt Handlers */
114 | /* Add here the Interrupt Handler for the used peripheral(s) (PPP), for the */
115 | /* available peripheral interrupt handler's name please refer to the startup */
116 | /* file (startup_stm32f10x_xx.s). */
117 | /******************************************************************************/
118 |
119 | /**
120 | * @brief This function handles PPP interrupt request.
121 | * @param None
122 | * @retval None
123 | */
124 | /*void PPP_IRQHandler(void)
125 | {
126 | }*/
127 |
128 | /**
129 | * @}
130 | */
131 |
132 |
133 | extern osSemaphoreId_t sid_Thread_Semaphore;
134 | extern int count;
135 |
136 | void EXTI0_IRQHandler(void)
137 | {
138 | osSemaphoreRelease (sid_Thread_Semaphore);
139 | if (--count < 0)
140 | count = 0;
141 |
142 | EXTI_ClearITPendingBit(EXTI_Line0);
143 | }
144 |
145 | void EXTI4_IRQHandler(void)
146 | {
147 | osSemaphoreRelease (sid_Thread_Semaphore);
148 | if (++count > 99)
149 | count = 99;
150 |
151 | EXTI_ClearITPendingBit(EXTI_Line4);
152 | }
153 |
154 |
155 | /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/
156 |
--------------------------------------------------------------------------------
/stm32/StdPeriph/zengjf/usart1.c:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/FolderLevel/NB_HW_Demo/a94f247ac6148317a008d1b2359c9329e8b70530/stm32/StdPeriph/zengjf/usart1.c
--------------------------------------------------------------------------------
/stm32/Test_With_Serial_Port_Command.md:
--------------------------------------------------------------------------------
1 | # STM32 Debug Infomation
2 |
3 | ## 串口工具下载
4 |
5 | * [STC ISP Tool](http://www.stcmcudata.com/STCISP/stc-isp-15xx-v6.86E.zip),使用STC提供的免费的串口工具,**不过要注意其发送缓冲区的`Enter键`就是`\r\n`**。
6 | 
7 |
8 | ## UDP Commands
9 |
10 | ### Send Data Message
11 |
12 | * 创建UDP Socket,参考《Quectel_BC95_AT_Commands_Manual_V1.7.pdf》:
13 | `AT+NSOCR=DGRAM,17,4587,1`
14 | * 使用Socket 0发送012356到23.106.155.16:9098服务器,要通过运营商服务器120.24.184.124:8010进行中转,包括目标服务器地址、端口号,总共发送12个字节数据,参考《UDP数据转发网关收发数据格式.pdf》:
15 | `AT+NSOST=0,120.24.184.124,8010,12,176A9B108A23303132333435`
16 | * **注意**:以上命令末尾都要有`\r\n`结尾。
17 |
18 | ### Server Get Date Message
19 |
20 | * 效果图:
21 | 
22 | * Data
23 | ```
24 | server received datagram from 72.228.117.183
25 | server received 12/-1 bytes: u=伾C.012345
26 | ```
27 | * 获取的数据前面不可见字符是终端设备的端口号和IP地址;
28 |
--------------------------------------------------------------------------------
/website/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
89 |
95 |
96 |
97 |
98 |
NB Hardware Demo
99 |
102 |
103 |
104 |
105 |
106 |
107 |
--------------------------------------------------------------------------------
/websocket/Makefile:
--------------------------------------------------------------------------------
1 | all:
2 | gcc main.c -lwebsockets -o lwsserver -lpthread
3 | gcc udpclient.c -o client
4 |
5 | clean:
6 | rm lwsserver
7 |
8 |
--------------------------------------------------------------------------------
/websocket/Makefile.ubuntu:
--------------------------------------------------------------------------------
1 | all:
2 | gcc main.c -lwebsockets -o lwsserver
3 |
4 | clean:
5 | rm lwsserver
6 |
7 |
--------------------------------------------------------------------------------
/websocket/client:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/FolderLevel/NB_HW_Demo/a94f247ac6148317a008d1b2359c9329e8b70530/websocket/client
--------------------------------------------------------------------------------
/websocket/lwsserver:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/FolderLevel/NB_HW_Demo/a94f247ac6148317a008d1b2359c9329e8b70530/websocket/lwsserver
--------------------------------------------------------------------------------
/websocket/udpclient.c:
--------------------------------------------------------------------------------
1 | /*
2 | * udpclient.c - A simple UDP client
3 | * usage: udpclient
4 | */
5 | #include
6 | #include
7 | #include
8 | #include
9 | #include
10 | #include
11 | #include
12 | #include
13 |
14 | #define BUFSIZE 1024
15 |
16 | /*
17 | * error - wrapper for perror
18 | */
19 | void error(char *msg) {
20 | perror(msg);
21 | exit(0);
22 | }
23 |
24 | int main(int argc, char **argv) {
25 | int sockfd, portno, n;
26 | int serverlen;
27 | struct sockaddr_in serveraddr;
28 | struct hostent *server;
29 | char *hostname;
30 | char buf[BUFSIZE];
31 |
32 | /* check command line arguments */
33 | if (argc != 3) {
34 | fprintf(stderr,"usage: %s \n", argv[0]);
35 | exit(0);
36 | }
37 | hostname = argv[1];
38 | portno = atoi(argv[2]);
39 |
40 | /* socket: create the socket */
41 | sockfd = socket(AF_INET, SOCK_DGRAM, 0);
42 | if (sockfd < 0)
43 | error("ERROR opening socket");
44 |
45 | /* gethostbyname: get the server's DNS entry */
46 | server = gethostbyname(hostname);
47 | if (server == NULL) {
48 | fprintf(stderr,"ERROR, no such host as %s\n", hostname);
49 | exit(0);
50 | }
51 |
52 | /* build the server's Internet address */
53 | bzero((char *) &serveraddr, sizeof(serveraddr));
54 | serveraddr.sin_family = AF_INET;
55 | bcopy((char *)server->h_addr,
56 | (char *)&serveraddr.sin_addr.s_addr, server->h_length);
57 | serveraddr.sin_port = htons(portno);
58 |
59 | /* get a message from the user */
60 | bzero(buf, BUFSIZE);
61 | printf("Please enter msg: ");
62 | fgets(buf, BUFSIZE, stdin);
63 |
64 | /* send the message to the server */
65 | serverlen = sizeof(serveraddr);
66 | n = sendto(sockfd, buf, strlen(buf), 0, &serveraddr, serverlen);
67 | if (n < 0)
68 | error("ERROR in sendto");
69 |
70 | bzero(buf, BUFSIZE);
71 |
72 | /* print the server's reply */
73 | n = recvfrom(sockfd, buf, BUFSIZE, 0, &serveraddr, &serverlen);
74 | if (n < 0)
75 | error("ERROR in recvfrom");
76 | printf("Echo from server %d: %s", n, buf);
77 | return 0;
78 | }
79 |
--------------------------------------------------------------------------------