一.背景
曾經玩Linux時很是喜歡這種基於出串口的控制檯, 經過簡單的串口TX和RX能實現嵌入式硬件的人機交互,很是實用, 那麼STM32可否實現經過超級終端與用戶互動的構想呢? 答案是確定的,因爲這個UART控制平臺就像應用程序套上一層可訪問的外科(Shell)故而我將這種基於UART的控制平臺簡稱Shell,構架和效果以下圖:c++
這張圖箭頭指向的是輸入的指令,其他是STM32串口輸出的信息,, 能夠看到經過這些簡單的指令輸入咱們經過Shell能夠作不少事情:shell
1. 現場設備發生故障,能夠經過Shell能夠查看設備的故障狀態統計信息數據結構
2. 能實現串口程序升級(須要Shell+IAP驅動程序支持)函數
3. 能讀寫訪問參數區,實現對設備參數的本地配置工具
4. 配置多功能信號指示燈(LED燈可顯示65535種信號,同一時刻只能顯示一個.oop
5. 程序開發階段基於Shell,能夠極其方便的調試編寫的驅動程序(開發極力推薦),很是好用.學習
二.Shell基礎篇
Shell基礎程序只有三個文件:測試
console.h:用於定義STM32用於Shell的實體串口ui
shell.cshell平臺實現主體this
shell.h頭文件,任意的驅動文件可調用,就像<stdio.h>同樣
shell.c目前包含三個部件:
shell模塊(必選)Shell模塊初始化時已初始化好Led模塊
Led模塊(必選)Ledx_on(x),Ledx_off(x),Ledx_div(x),函數是對編碼信號進行控制,而不是直接對硬件實體控制,這樣每一個LED實體就像通道同樣能夠選擇很是多的信號源顯示.
精密延時模塊(可選)啓動須要對其初始化,此模塊可用於記錄時間點,並判斷時間是否到(不再用Delayms()這樣的函數浪費效率實現時序了.
三. 程序文件:
1. console.h
-
-
-
- #ifndef _SHELL_HAL_
- #define _SHELL_HAL_
- #include "stm32f10x.h"
- #include "stm32f10x_gpio.h"
- #include "stm32f10x_rcc.h"
- #include "stm32f10x_tim.h"
-
- #define CONSOLE USART3
- #define CONSOLE_TX_PORT GPIOB
- #define CONSOLE_TX_PIN GPIO_Pin_10
- #define CONSOLE_RX_PORT GPIOB
- #define CONSOLE_RX_PIN GPIO_Pin_11
-
- #define CONSOLE_GPIO_RCC_INIT() RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE)
- #define CONSOLE_UART_RCC_INIT() RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE)
-
- #define CONSOLE_UART_PRIO 7 //建議[0..15]
-
- #define CONSOLE_IRQn USART3_IRQn;
- #define CONSOLE_IRQHandler USART3_IRQHandler
-
- #define LED0_VALID 1 //非零表示使能對應的LED,0:無效
- #define LED0_PORT GPIOB
- #define LED0_PIN GPIO_Pin_13
-
- #define LED1_VALID 1 //非零表示使能對應的LED,0:無效
- #define LED1_PORT GPIOB
- #define LED1_PIN GPIO_Pin_15
-
- #define LED2_VALID 0 //非零表示使能對應的LED,0:無效
- #define LED2_PORT GPIOA
- #define LED2_PIN GPIO_Pin_11
-
- #define LED3_VALID 0 //非零表示使能對應的LED,0:無效
- #define LED3_PORT GPIOA
- #define LED3_PIN GPIO_Pin_11
-
- #define LED4_VALID 0 //非零表示使能對應的LED,0:無效
- #define LED4_PORT GPIOA
- #define LED4_PIN GPIO_Pin_11
-
- #define LED5_VALID 0 //非零表示使能對應的LED,0:無效
- #define LED5_PORT GPIOA
- #define LED5_PIN GPIO_Pin_11
-
- #define TIMEDly TIM4
- #define TIMEDly_IRQn TIM4_IRQn
- #define TIMEDly_IRQHandler TIM4_IRQHandler
-
- #define TIMEDly_RCC_INIT() RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
-
- #define LEDx_GPIO_RCC_INIT() RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE)
-
- #if LED0_VALID
- #define LED0_ON() (LED0_PORT->BRR = LED0_PIN)
- #define LED0_OFF() (LED0_PORT->BSRR = LED0_PIN)
- #define LED0_DIV() (LED0_PORT->ODR ^= LED0_PIN)
- #else
- #define LED0_ON() __NOP()
- #define LED0_OFF() __NOP()
- #define LED0_DIV() __NOP()
- #endif
-
- #if LED1_VALID
- #define LED1_ON() (LED1_PORT->BRR = LED1_PIN)
- #define LED1_OFF() (LED1_PORT->BSRR = LED1_PIN)
- #define LED1_DIV() (LED1_PORT->ODR ^= LED1_PIN)
- #else
- #define LED1_ON() __NOP()
- #define LED1_OFF() __NOP()
- #define LED1_DIV() __NOP()
- #endif
-
- #if LED2_VALID
- #define LED2_ON() (LED2_PORT->BRR = LED2_PIN)
- #define LED2_OFF() (LED2_PORT->BSRR = LED2_PIN)
- #define LED2_DIV() (LED2_PORT->ODR ^= LED2_PIN)
- #else
- #define LED2_ON() __NOP()
- #define LED2_OFF() __NOP()
- #define LED2_DIV() __NOP()
- #endif
-
- #if LED3_VALID
- #define LED3_ON() (LED3_PORT->BRR = LED3_PIN)
- #define LED3_OFF() (LED3_PORT->BSRR = LED3_PIN)
- #define LED3_DIV() (LED3_PORT->ODR ^= LED3_PIN)
- #else
- #define LED3_ON() __NOP()
- #define LED3_OFF() __NOP()
- #define LED3_DIV() __NOP()
- #endif
-
- #if LED4_VALID
- #define LED4_ON() (LED4_PORT->BSRR = LED4_PIN)
- #define LED4_OFF() (LED4_PORT->BRR = LED4_PIN)
- #define LED4_DIV() (LED4_PORT->ODR ^= LED4_PIN)
- #else
- #define LED4_ON() __NOP()
- #define LED4_OFF() __NOP()
- #define LED4_DIV() __NOP()
- #endif
-
- #if LED5_VALID
- #define LED5_ON() (LED5_PORT->BSRR = LED5_PIN)
- #define LED5_OFF() (LED5_PORT->BRR = LED5_PIN)
- #define LED5_DIV() (LED5_PORT->ODR ^= LED5_PIN)
- #else
- #define LED5_ON() __NOP()
- #define LED5_OFF() __NOP()
- #define LED5_DIV() __NOP()
- #endif
- #ifdef __GNUC__
- #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
- #else
- #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
- #endif /* __GNUC__ */
-
- #endif
2. shell.h
-
-
-
- #ifndef _SHELL_H_
- #define _SHELL_H_
- #include "stdint.h" //包含uint8_t等數據類型
- #include "stdbool.h" //包含Bool類型
- #include "stdio.h" //包含printf支持
-
- #define SHELL_VER 2 //Shell版本
- #ifndef SHELL_LED_MAX //LED實體數量
- #define SHELL_LED_MAX 4
- #endif
-
- #define SHELL_RX_MAX (256+32) //shell指令接收緩衝大小
- #define SHELL_TX_MAX (512) //shell指令發送緩衝大小
-
- extern volatile uint16_t shell_rx_rdy;
- extern volatile uint8_t shell_rx_buff[SHELL_RX_MAX+1];
-
- #if (6 == SHELL_LED_MAX)
- extern void shell_GPIO_Config(uint16_t led0_cfg,uint16_t led1_cfg,
- uint16_t led2_cfg,uint16_t led3_cfg,
- uint16_t led4_cfg,uint16_t led5_cfg);
- #elif (5 == SHELL_LED_MAX)
- extern void shell_GPIO_Config(uint16_t led0_cfg,uint16_t led1_cfg,
- uint16_t led2_cfg,uint16_t led3_cfg,
- uint16_t led4_cfg);
- #elif (4 == SHELL_LED_MAX)
- extern void shell_GPIO_Config(uint16_t led0_cfg,uint16_t led1_cfg,
- uint16_t led2_cfg,uint16_t led3_cfg);
- #elif (3 == SHELL_LED_MAX)
- extern void shell_GPIO_Config(uint16_t led0_cfg,uint16_t led1_cfg,
- uint16_t led2_cfg);
- #elif (2 == SHELL_LED_MAX)
- extern void shell_GPIO_Config(uint16_t led0_cfg,uint16_t led1_cfg);
- #elif (1 == SHELL_LED_MAX)
- extern void shell_GPIO_Config(uint16_t led0_cfg);
- #endif
-
- #if ((SHELL_LED_MAX > 6) || (SHELL_LED_MAX == 0))
- #error SHELL_LED_MAX is invaild!
- #endif
-
- extern void shell_Init(uint32_t baud);
- extern void shell_SendStr(void * ptStr);
- extern void shell_SendHex(void * ptHex,uint16_t size);
-
- extern void Ledx_config(uint8_t led_id,uint16_t msg_id);
- extern uint16_t Ledx_read(uint8_t led_id);
- extern void Ledx_on (uint16_t msg_id);
- extern void Ledx_off(uint16_t msg_id);
- extern void Ledx_div(uint16_t msg_id);
-
- extern void Delay_LibInit(void);
- extern void DlyTime_us(uint16_t us);
- extern void DlyTime_ms(uint16_t ms);
- extern void DlyWait_base(volatile uint64_t * ptCnt);
- extern uint32_t DlyWait_lost(volatile uint64_t * ptCnt);
-
- extern bool StrComp(void * buffer,void * StrCmd);
-
- extern void Shell_Invalid_Service(void);
-
- #endif
3.shell.c
-
-
-
- #include "shell.h" //包含Shell接口文件
-
- #ifdef SHELL_HAL_EXT
- #include "shell_hal.h" //本地的Shell文件
- #else
- #include "console.h" //標準的Shell文件
- #endif
-
- volatile uint16_t shell_rx_rdy = 0;
- volatile uint8_t shell_rx_buff[SHELL_RX_MAX+1]="\0";
-
- static volatile uint16_t shell_rx_index = 0;
-
- static volatile uint8_t shell_tx_buff[SHELL_TX_MAX+1]="\0";
- static volatile uint16_t shell_tx_size = 0;
- static volatile uint16_t shell_tx_index = 0;
-
- static volatile uint16_t msg_led_cfg[SHELL_LED_MAX];
-
- static volatile uint64_t TimeDlyCnt = 0;
-
- #if (6 == SHELL_LED_MAX)
- void shell_GPIO_Config( uint16_t led0_cfg,uint16_t led1_cfg,
- uint16_t led2_cfg,uint16_t led3_cfg,
- uint16_t led4_cfg,uint16_t led5_cfg)
- #elif (5 == SHELL_LED_MAX)
- void shell_GPIO_Config( uint16_t led0_cfg,uint16_t led1_cfg,
- uint16_t led2_cfg,uint16_t led3_cfg,
- uint16_t led4_cfg)
- #elif (4 == SHELL_LED_MAX)
- void shell_GPIO_Config( uint16_t led0_cfg,uint16_t led1_cfg,
- uint16_t led2_cfg,uint16_t led3_cfg)
- #elif (3 == SHELL_LED_MAX)
- void shell_GPIO_Config( uint16_t led0_cfg,uint16_t led1_cfg,
- uint16_t led2_cfg)
- #elif (2 == SHELL_LED_MAX)
- void shell_GPIO_Config(uint16_t led0_cfg,uint16_t led1_cfg)
- #elif (1 == SHELL_LED_MAX)
- void shell_GPIO_Config(uint16_t led0_cfg)
- #endif
- {
- GPIO_InitTypeDef GPIO_InitStruct;
-
-
-
- CONSOLE_GPIO_RCC_INIT();
-
-
- GPIO_InitStruct.GPIO_Pin = CONSOLE_TX_PIN;
- GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(CONSOLE_TX_PORT, &GPIO_InitStruct);
-
-
- GPIO_InitStruct.GPIO_Pin = CONSOLE_RX_PIN;
- GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPU;
- GPIO_Init(CONSOLE_RX_PORT, &GPIO_InitStruct);
-
-
- GPIO_PinLockConfig(CONSOLE_TX_PORT,CONSOLE_TX_PIN);
- GPIO_PinLockConfig(CONSOLE_RX_PORT,CONSOLE_RX_PIN);
-
-
- LEDx_GPIO_RCC_INIT();
- GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStruct.GPIO_Speed = GPIO_Speed_10MHz;
-
-
- #if (SHELL_LED_MAX > 0)
- GPIO_InitStruct.GPIO_Pin = LED0_PIN;
- GPIO_Init(LED0_PORT, &GPIO_InitStruct);
- GPIO_PinLockConfig(LED0_PORT,LED0_PIN);
- msg_led_cfg[0] = led0_cfg;
- LED0_OFF();
- #endif
- #if (SHELL_LED_MAX > 1)
- GPIO_InitStruct.GPIO_Pin = LED1_PIN;
- GPIO_Init(LED1_PORT, &GPIO_InitStruct);
- GPIO_PinLockConfig(LED1_PORT,LED1_PIN);
- msg_led_cfg[1] = led1_cfg;
- LED1_OFF();
- #endif
- #if (SHELL_LED_MAX > 2)
- GPIO_InitStruct.GPIO_Pin = LED2_PIN;
- GPIO_Init(LED2_PORT, &GPIO_InitStruct);
- GPIO_PinLockConfig(LED2_PORT,LED2_PIN);
- msg_led_cfg[2] = led2_cfg;
- LED2_OFF();
- #endif
- #if (SHELL_LED_MAX > 3)
- GPIO_InitStruct.GPIO_Pin = LED3_PIN;
- GPIO_Init(LED3_PORT, &GPIO_InitStruct);
- GPIO_PinLockConfig(LED3_PORT,LED3_PIN);
- msg_led_cfg[3] = led3_cfg;
- LED3_OFF();
- #endif
- #if (SHELL_LED_MAX > 4)
- GPIO_InitStruct.GPIO_Pin = LED4_PIN;
- GPIO_Init(LED4_PORT, &GPIO_InitStruct);
- GPIO_PinLockConfig(LED4_PORT,LED4_PIN);
- msg_led_cfg[4] = led4_cfg;
- LED4_OFF();
- #endif
- #if (SHELL_LED_MAX > 5)
- GPIO_InitStruct.GPIO_Pin = LED5_PIN;
- GPIO_Init(LED5_PORT, &GPIO_InitStruct);
- GPIO_PinLockConfig(LED5_PORT,LED5_PIN);
- msg_led_cfg[5] = led5_cfg;
- LED5_OFF();
- #endif
- }
-
- void shell_Init(uint32_t baud)
- {
- USART_InitTypeDef USART_InitStructure;
- NVIC_InitTypeDef NVIC_UART_Cfg;
-
-
-
- USART_InitStructure.USART_BaudRate = baud;
- USART_InitStructure.USART_WordLength = USART_WordLength_8b;
- USART_InitStructure.USART_StopBits = USART_StopBits_1;
- USART_InitStructure.USART_Parity = USART_Parity_No ;
- USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
- USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
- USART_InitStructure.USART_BaudRate = USART_InitStructure.USART_BaudRate;
-
-
- NVIC_UART_Cfg.NVIC_IRQChannel = CONSOLE_IRQn;
- NVIC_UART_Cfg.NVIC_IRQChannelPreemptionPriority = 1;
- NVIC_UART_Cfg.NVIC_IRQChannelSubPriority = CONSOLE_UART_PRIO;
- NVIC_UART_Cfg.NVIC_IRQChannelCmd = ENABLE;
- NVIC_UART_Cfg.NVIC_IRQChannel = NVIC_UART_Cfg.NVIC_IRQChannel;
-
-
-
- CONSOLE_UART_RCC_INIT();
- USART_Cmd(CONSOLE, DISABLE);
-
- USART_Init(CONSOLE, &USART_InitStructure);
-
- USART_ITConfig(CONSOLE, USART_IT_RXNE, ENABLE);
- USART_ITConfig(CONSOLE, USART_IT_IDLE, ENABLE);
-
- USART_Cmd(CONSOLE, ENABLE);
-
- NVIC_Init(&NVIC_UART_Cfg);
- }
-
- void Delay_LibInit(void)
- {
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
-
-
- TIMEDly_RCC_INIT();
-
-
- NVIC_InitStructure.NVIC_IRQChannel = TIMEDly_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority= 0;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
-
-
- TIM_TimeBaseStructure.TIM_Period = 65000;
- TIM_TimeBaseStructure.TIM_Prescaler = (SystemCoreClock/1000000)-1;
- TIM_TimeBaseStructure.TIM_ClockDivision = 0;
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
-
- TIM_TimeBaseInit(TIMEDly, &TIM_TimeBaseStructure);
-
-
- TIM_ITConfig(TIMEDly,TIM_IT_Update, ENABLE);
-
-
- TIM_Cmd(TIMEDly, ENABLE);
- }
-
- void DlyTime_us(uint16_t us)
- {
- uint64_t this_cnt,over_cnt,tmp_val;
-
-
- do
- {
- this_cnt = TIMEDly->CNT + TimeDlyCnt;
- if( (TIMEDly->SR & TIM_IT_Update) &&
- (TIMEDly->SR & TIM_IT_Update) &&
- (TIMEDly->SR & TIM_IT_Update) )
- {
- TIMEDly->SR = (uint16_t)~TIM_IT_Update;
- TimeDlyCnt += 65000;
- }
- tmp_val = TIMEDly->CNT + TimeDlyCnt;
- }
- while(this_cnt != tmp_val);
- over_cnt = this_cnt + us;
-
-
- do
- {
- if( (TIMEDly->SR & TIM_IT_Update) &&
- (TIMEDly->SR & TIM_IT_Update) &&
- (TIMEDly->SR & TIM_IT_Update) )
- {
- TIMEDly->SR = (uint16_t)~TIM_IT_Update;
- TimeDlyCnt += 65000;
- }
-
- this_cnt = TIMEDly->CNT + TimeDlyCnt;
- }
- while(this_cnt < over_cnt);
- }
-
- void DlyTime_ms(uint16_t ms)
- {
- uint64_t this_cnt,over_cnt,tmp_val;
-
-
- do
- {
- this_cnt = TIMEDly->CNT + TimeDlyCnt;
- if( (TIMEDly->SR & TIM_IT_Update) &&
- (TIMEDly->SR & TIM_IT_Update) &&
- (TIMEDly->SR & TIM_IT_Update) )
- {
- TIMEDly->SR = (uint16_t)~TIM_IT_Update;
- TimeDlyCnt += 65000;
- }
- tmp_val = TIMEDly->CNT + TimeDlyCnt;
- }
- while(this_cnt != tmp_val);
- over_cnt = this_cnt + (uint32_t)ms*1000;
-
-
- do
- {
- if( (TIMEDly->SR & TIM_IT_Update) &&
- (TIMEDly->SR & TIM_IT_Update) &&
- (TIMEDly->SR & TIM_IT_Update) )
- {
- TIMEDly->SR = (uint16_t)~TIM_IT_Update;
- TimeDlyCnt += 65000;
- }
- this_cnt = TIMEDly->CNT + TimeDlyCnt;
- }
- while(this_cnt < over_cnt);
- }
-
- void DlyWait_base(volatile uint64_t * ptCnt)
- {
- uint64_t this_cnt,tmp_val;
-
-
- do
- {
- this_cnt = TIMEDly->CNT + TimeDlyCnt;
- if( (TIMEDly->SR & TIM_IT_Update) &&
- (TIMEDly->SR & TIM_IT_Update) &&
- (TIMEDly->SR & TIM_IT_Update) )
- {
- TIMEDly->SR = (uint16_t)~TIM_IT_Update;
- TimeDlyCnt += 65000;
- }
- tmp_val = TIMEDly->CNT + TimeDlyCnt;
- }
- while(this_cnt != tmp_val);
- *ptCnt = this_cnt;
- }
-
- uint32_t DlyWait_lost(volatile uint64_t * ptCnt)
- {
- uint64_t this_cnt,tmp_val;
-
-
- do
- {
- this_cnt = TIMEDly->CNT + TimeDlyCnt;
- if( (TIMEDly->SR & TIM_IT_Update) &&
- (TIMEDly->SR & TIM_IT_Update) &&
- (TIMEDly->SR & TIM_IT_Update) )
- {
- TIMEDly->SR = (uint16_t)~TIM_IT_Update;
- TimeDlyCnt += 65000;
- }
- tmp_val = TIMEDly->CNT + TimeDlyCnt;
- }
- while(this_cnt != tmp_val);
-
-
- if(*ptCnt <= this_cnt)
- {
- tmp_val = this_cnt - *ptCnt;
- if(tmp_val > (65536UL*65536UL-1))
- return (uint32_t)(65536UL*65536UL-1);
- else return (uint32_t)tmp_val;
- }
- else
- {
- *ptCnt = this_cnt;
- return 0;
- }
- }
-
- void TIMEDly_IRQHandler(void)
- {
-
- if( TIMEDly->SR & TIM_IT_Update )
- {
- TIMEDly->SR = (uint16_t)~TIM_IT_Update;
- TimeDlyCnt += 65000;
-
- }
- }
-
- PUTCHAR_PROTOTYPE
- {
- Ledx_on(11);
-
- while( (CONSOLE->SR & USART_FLAG_TXE) == RESET );
-
-
-
- CONSOLE->DR = ch;
-
-
-
- Ledx_off(11);
- return ch;
- }
-
- void shell_SendStr(void * ptAsc)
- {
-
- uint16_t i,size;
- uint8_t *ptDst;
- uint8_t const *ptSrc;
-
-
- ptSrc = (uint8_t const *)ptAsc;
- size = 0;
- while(*ptSrc++){size++;}
-
-
- Ledx_on(11);
- if(size > SHELL_TX_MAX)
- {
-
- shell_tx_index = 0;
- shell_tx_size = 0;
- CONSOLE->CR1 &= ~USART_CR1_TXEIE;
-
- ptSrc = (uint8_t const *)ptAsc;
- while(size--)
- {
- while( (CONSOLE->SR & USART_FLAG_TXE) == RESET );
- CONSOLE->DR = *ptSrc++;
- }
- Ledx_off(11);
- }
- else if( !(CONSOLE->CR1 & USART_CR1_TXEIE) )
- {
-
-
- ptDst = (uint8_t *)shell_tx_buff;
- ptSrc = (uint8_t const *)ptAsc;
- for(i=0; i<size; i++)
- *ptDst++ = *ptSrc++;
-
-
- shell_tx_index = 0;
- shell_tx_size = size;
- CONSOLE->CR1 |= USART_CR1_TXEIE;
- }
- }
-
- void shell_SendHex(void * ptHex,uint16_t size)
- {
-
- uint16_t i;
- uint8_t *ptDst;
- uint8_t const *ptSrc;
-
- Ledx_on(11);
- if(size > SHELL_TX_MAX)
- {
-
- shell_tx_index = 0;
- shell_tx_size = 0;
- CONSOLE->CR1 &= ~USART_CR1_TXEIE;
-
-
- ptSrc = (uint8_t const *)ptHex;
- while(size--)
- {
- while( (CONSOLE->SR & USART_FLAG_TXE) == RESET );
- CONSOLE->DR = *ptSrc++;
- }
- Ledx_off(11);
- }
- else if( !(CONSOLE->CR1 & USART_CR1_TXEIE) )
- {
-
-
- ptDst = (uint8_t *)shell_tx_buff;
- ptSrc = (uint8_t const *)ptHex;
- for(i=0; i<size; i++)
- *ptDst++ = *ptSrc++;
-
-
- shell_tx_index = 0;
- shell_tx_size = size;
- CONSOLE->CR1 |= USART_CR1_TXEIE;
- }
- }
-
- void CONSOLE_IRQHandler(void)
- {
- uint8_t rxd_reg,txd_reg;
- uint16_t isr_reg;
-
-
- isr_reg = CONSOLE->SR;
-
- if( (CONSOLE->CR1 & USART_CR1_RXNEIE) && (isr_reg & USART_SR_RXNE) )
- {
- rxd_reg = CONSOLE->DR;
- Ledx_on(12);
- if(shell_rx_rdy)shell_rx_index = 0;
- else
- {
- if( shell_rx_index < SHELL_RX_MAX)
- {
- shell_rx_buff[shell_rx_index] = rxd_reg;
- shell_rx_index++;
- }
- else
- {
- shell_rx_index = 0;
- Ledx_off(12);
- }
- }
- }
-
- if( (CONSOLE->CR1 & USART_CR1_IDLEIE) && (isr_reg & USART_SR_IDLE) )
- {
- CONSOLE->SR;
- CONSOLE->DR;
- if(shell_rx_rdy)shell_rx_index = 0;
- else
- {
- if( (shell_rx_index >=2) && ('\r' == shell_rx_buff[shell_rx_index-2]) &&
- ('\n' == shell_rx_buff[shell_rx_index-1]) )
- {
- shell_rx_rdy = shell_rx_index;
- shell_rx_index = 0;
- Ledx_off(12);
- }
- else if( (shell_rx_index > 0) && ('\b' == shell_rx_buff[shell_rx_index-1]) )
- {
- shell_rx_index = shell_rx_index <2? 0:shell_rx_index-2;
- printf(" \b");
- }
- }
- }
-
-
- if( (CONSOLE->CR1 & USART_CR1_TXEIE) && (isr_reg & USART_SR_TXE ) )
- {
- if(shell_tx_size && (shell_tx_index < shell_tx_size) )
- {
- txd_reg = shell_tx_buff[shell_tx_index++];
- CONSOLE->DR = txd_reg;
- }
- else
- {
-
- shell_tx_index = 0;
- shell_tx_size = 0;
- CONSOLE->CR1 &= ~USART_CR1_TXEIE;
- Ledx_off(11);
- }
- }
- }
-
-
- void Shell_Invalid_Service(void)
- {
- int tx_len,i,led_id,msg_id;
- uint8_t * ptSrc;
- uint8_t * ptDst;
- uint8_t tmp_buff[64];
-
-
- if(2 > shell_rx_rdy)
- {
- shell_rx_buff[0] = 0;
- return;
- }
- else if( ('\r' == shell_rx_buff[shell_rx_rdy-2]) && ('\n' == shell_rx_buff[shell_rx_rdy-1]) )
- {
- ptSrc = (uint8_t *)shell_rx_buff;
- if(2 == shell_rx_rdy)
- {
-
- tx_len = (uint16_t)sprintf((void *)tmp_buff,"\r\nAT:OK!\r\n");
-
-
- shell_SendHex(tmp_buff,tx_len);
- }
- else if(StrComp(ptSrc,"led rd\r\n"))
- {
-
- tx_len = (uint16_t)sprintf((void *)tmp_buff,
- #if (6 == SHELL_LED_MAX)
- "->LED0=%d LED1=%d tLED2=%d LED3=%d LED4=%d LED5=%d\r\n",
- msg_led_cfg[0],msg_led_cfg[1],msg_led_cfg[2],
- msg_led_cfg[3],msg_led_cfg[4],msg_led_cfg[5]);
- #elif (5 == SHELL_LED_MAX)
- "->LED0=%d LED1=%d tLED2=%d LED3=%d LED4=%d\r\n",
- msg_led_cfg[0],msg_led_cfg[1],msg_led_cfg[2],
- msg_led_cfg[3],msg_led_cfg[4]);
- #elif (4 == SHELL_LED_MAX)
- "->LED0=%d LED1=%d tLED2=%d LED3=%d\r\n",
- msg_led_cfg[0],msg_led_cfg[1],msg_led_cfg[2],
- msg_led_cfg[3]);
- #elif (3 == SHELL_LED_MAX)
- "->LED0=%d LED1=%d tLED2=%d\r\n",
- msg_led_cfg[0],msg_led_cfg[1],msg_led_cfg[2]);
- #elif (2 == SHELL_LED_MAX)
- "->LED0=%d LED1=%d\r\n",
- msg_led_cfg[0],msg_led_cfg[1]);
- #elif (1 == SHELL_LED_MAX)
- "->LED0=%d\r\n",
- msg_led_cfg[0]);
- #endif
-
- shell_SendHex(tmp_buff,tx_len);
- }
- else if(StrComp(ptSrc,"led wr "))
- {
- if(2 != sscanf((void *)ptSrc,"%*s%*s%d=%d",&led_id,&msg_id) )goto ERROR_LOOP;
- if( (led_id>(SHELL_LED_MAX-1)) || (msg_id >65535) )goto ERROR_LOOP;
-
- Ledx_config((uint8_t)led_id,(uint16_t)msg_id);
-
- tx_len = (uint16_t)sprintf((void *)tmp_buff,
- "->LED[%d]_Msg=%d\r\n",led_id,msg_led_cfg[led_id]);
-
-
- shell_SendHex(tmp_buff,tx_len);
- }
- else goto ERROR_LOOP;
- }
- else
- {
- ERROR_LOOP:
-
- tx_len = (uint16_t)sprintf((void *)tmp_buff,"\r\nAT: Cmd Error:\t\"");
-
-
- ptDst = tmp_buff + tx_len;
- ptSrc = (uint8_t *)shell_rx_buff;
- if(shell_rx_rdy > 32)
- {
- for(i=0; i<32; i++)
- {
- if( (*ptSrc > 126) || (*ptSrc < 32) )
- {
- *ptDst++ = '?';
- ptSrc++;
- }
- else
- {
- *ptDst++ = *ptSrc++;
- }
- }
- *(ptDst-2) = '-';
- *(ptDst-1) = '>';
- tx_len += 32;
- }
- else
- {
- for(i=0; i<shell_rx_rdy; i++)
- {
- *ptDst++ = *ptSrc++;
- tx_len++;
- }
- *(ptDst-2) = '<';
- *(ptDst-1) = '-';
- }
- tx_len += (uint16_t)sprintf((void *)ptDst,"\"\r\n");
-
-
- shell_SendHex(tmp_buff,tx_len);
- }
-
-
- shell_rx_buff[0] = 0;
- shell_rx_rdy = 0;
- }
-
- bool StrComp(void * buffer,void * StrCmd)
- {
- uint8_t i;
- uint8_t * ptBuf;
- uint8_t * ptCmd;
-
- ptBuf = (uint8_t *)buffer;
- ptCmd = (uint8_t *)StrCmd;
- for(i=0; i<255; i++)
- {
- if(ptCmd[i])
- {
- if(ptBuf[i] != ptCmd[i])return false;
- }
- else
- {
- if(i)return i;
- else return false;
- }
- }
- return false;
- }
-
- void Ledx_on(uint16_t msg_id)
- {
- #if (SHELL_LED_MAX > 0)
- if(msg_id == msg_led_cfg[0])LED0_ON();
- #endif
- #if (SHELL_LED_MAX > 1)
- if(msg_id == msg_led_cfg[1])LED1_ON();
- #endif
- #if (SHELL_LED_MAX > 2)
- if(msg_id == msg_led_cfg[2])LED2_ON();
- #endif
- #if (SHELL_LED_MAX > 3)
- if(msg_id == msg_led_cfg[3])LED3_ON();
- #endif
- #if (SHELL_LED_MAX > 4)
- if(msg_id == msg_led_cfg[4])LED4_ON();
- #endif
- #if (SHELL_LED_MAX > 5)
- if(msg_id == msg_led_cfg[5])LED5_ON();
- #endif
- }
-
- void Ledx_off(uint16_t msg_id)
- {
- #if (SHELL_LED_MAX > 0)
- if(msg_id == msg_led_cfg[0])LED0_OFF();
- #endif
- #if (SHELL_LED_MAX > 1)
- if(msg_id == msg_led_cfg[1])LED1_OFF();
- #endif
- #if (SHELL_LED_MAX > 2)
- if(msg_id == msg_led_cfg[2])LED2_OFF();
- #endif
- #if (SHELL_LED_MAX > 3)
- if(msg_id == msg_led_cfg[3])LED3_OFF();
- #endif
- #if (SHELL_LED_MAX > 4)
- if(msg_id == msg_led_cfg[4])LED4_OFF();
- #endif
- #if (SHELL_LED_MAX > 5)
- if(msg_id == msg_led_cfg[5])LED5_OFF();
- #endif
- }
-
- void Ledx_div(uint16_t msg_id)
- {
- #if (SHELL_LED_MAX > 0)
- if(msg_id == msg_led_cfg[0])LED0_DIV();
- #endif
- #if (SHELL_LED_MAX > 1)
- if(msg_id == msg_led_cfg[1])LED1_DIV();
- #endif
- #if (SHELL_LED_MAX > 2)
- if(msg_id == msg_led_cfg[2])LED2_DIV();
- #endif
- #if (SHELL_LED_MAX > 3)
- if(msg_id == msg_led_cfg[3])LED3_DIV();
- #endif
- #if (SHELL_LED_MAX > 4)
- if(msg_id == msg_led_cfg[4])LED4_DIV();
- #endif
- #if (SHELL_LED_MAX > 5)
- if(msg_id == msg_led_cfg[5])LED5_DIV();
- #endif
- }
-
- void Ledx_config(uint8_t led_id,uint16_t msg_id)
- {
- if(0 == led_id) {LED0_OFF();msg_led_cfg[0]=msg_id;}
- #if(SHELL_LED_MAX > 1)
- else if(1 == led_id){LED1_OFF();msg_led_cfg[1]=msg_id;}
- #endif
- #if(SHELL_LED_MAX > 2)
- else if(2 == led_id){LED2_OFF();msg_led_cfg[2]=msg_id;}
- #endif
- #if(SHELL_LED_MAX > 3)
- else if(3 == led_id){LED3_OFF();msg_led_cfg[3]=msg_id;}
- #endif
- #if(SHELL_LED_MAX > 4)
- else if(4 == led_id){LED4_OFF();msg_led_cfg[4]=msg_id;}
- #endif
- #if(SHELL_LED_MAX > 5)
- else if(5 == led_id){LED5_OFF();msg_led_cfg[5]=msg_id;}
- #endif
- }
-
- uint16_t Ledx_read(uint8_t led_id)
- {
- if(0 == led_id) return msg_led_cfg[0];
- #if(SHELL_LED_MAX > 1)
- else if(1 == led_id)return msg_led_cfg[1];
- #endif
- #if(SHELL_LED_MAX > 2)
- else if(2 == led_id)return msg_led_cfg[2];
- #endif
- #if(SHELL_LED_MAX > 3)
- else if(3 == led_id)return msg_led_cfg[3];
- #endif
- #if(SHELL_LED_MAX > 4)
- else if(4 == led_id)return msg_led_cfg[4];
- #endif
- #if(SHELL_LED_MAX > 5)
- else if(5 == led_id)return msg_led_cfg[5];
- #endif
- else return 0;
- }
-
四. shell使用
以MDK爲例:
1. 將shell.c等文件加入工程
2. 包含Shell.h,並外部聲明shell服務程序
- extern void Shell_SPI_Service(void);
- extern void Shell_RTC_Service(void);
- extern void Shell_WIZ_Service(void);
- extern void Shell_UPAN_Service(void);
- extern void Shell_IAP_Service(uint8_t const this_ver);
- shell_GPIO_Config(30,10,46,1);
- shell_Init(460800);
3. 在main,c的大循環中添加如下代碼
- if(shell_rx_rdy)
- {
- Shell_IAP_Service(SOFT_VER);
- Shell_SPI_Service();
- Shell_WIZ_Service();
- Shell_RTC_Service();
- Shell_MCU_Service();
- Shell_VLSI_Service();
- Shell_UPAN_Service();
- Shell_Invalid_Service();
- }
4. 編譯下載文件到STM32,完成後字節在超級終端上敲擊Enter就會顯示AT:OK!的字樣表示能夠正常通信
注意事項:
1. 打開超級終端的本地回顯功能
2. 有些終端工具輸入Enter'時只輸出(\r碼) 如SecureCRT,這時須要將Enter映射按鍵(\r\n),winXP的超級終端輸入Enter是輸出(\r\n兩個碼值的)
五. RTC Shell服務文件示例
這裏展現一個Shell服務的文件模板寫法:
-
-
- #define RTC_SHELL //註釋掉時屏蔽iap shell功能
-
- #include "shell.h" //Shell支持文件,含bool,uint8_t..以及串口數據收發操做
- #include "rtc.h" //命令控制支持文件
-
- #ifdef SHELL_VER
- #if (SHELL_VER < 2)
- #error "shell版本過低"
- #endif
- #else
- #error "未找到Shell文件,或shell版本信息"
- #endif
-
- #ifdef RTC_SHELL
- extern void Shell_RTC_Service(void);
- #else
- void Shell_RTC_Service(void){;}
- #endif
-
-
- #ifdef RTC_SHELL
- const char RTC_HelpMsg[] =
- "[RTC contorls]\r\n"
- " rtc help\t\t- help.\r\n"
- " rtc rd info\t\t- Read RTC info.\r\n"
- " rtc rd time\t\t- Read RTC date and time.\r\n"
- " rtc wr time <Hour>:<Minute>:<Second> - Write time.\r\n"
- " rtc wr date <Year>-<Month>-<Day> <Week> - Warning Week=[1..7]\r\n"
- "\r\n";
-
-
- void Shell_RTC_Service(void)
- {
- uint8_t *ptRxd;
-
- int i,j,k,l;
-
-
- uint16_t retval;
- uint8_t arg[32];
- uint32_t tmp_time;
-
-
-
-
-
- i = shell_rx_rdy;
- if( (i < 2) || ('\r' != shell_rx_buff[i-2]) || ('\n' != shell_rx_buff[i-1]))return;
-
-
- ptRxd = (uint8_t *)shell_rx_buff;
- if( (' ' != shell_rx_buff[3]) || ('r' != shell_rx_buff[0]) || (i < 6) ||
- ('t' != shell_rx_buff[1]) || ('c' != shell_rx_buff[2]) ) return;
-
-
-
- ptRxd += 4;
- if(StrComp(ptRxd,"rd time"))
- {
- RTC_Sprintf_CurrTime((void *)arg);
- printf("Time:%s\r\n",arg);
- }
- else if(StrComp(ptRxd,"rd info\r\n"))
- {
-
- RTC_Sprintf_CurrTime((void *)arg);
- printf("->Time:%s\tResetCounter:%d\r\n",arg,RESET_CNT);
- RTC_Sprintf_ResetCurr((void *)arg,&tmp_time);
- printf("\tCurrReset:%s\tRun: %d Days, %d hour, %d minute\r\n",
- arg,tmp_time/(24*60),(tmp_time%(24*60))/60,tmp_time%60 );
- RTC_Sprintf_ResetLast((void *)arg,&tmp_time);
- printf("\tNextReset:%s\tRun: %d Days, %d hour, %d minute\r\n",
- arg,tmp_time/(24*60),(tmp_time%(24*60))/60,tmp_time%60 );
- }
- else if(StrComp(ptRxd,"wr time "))
- {
- retval = sscanf((void*)shell_rx_buff,"%*s%*s%*s%d:%d:%d",&i,&j,&k);
- if(3 != retval)return;
- if(RTC_TimeWrite((uint8_t)i,(uint8_t)j,(uint8_t)k) )
- {
- RTC_Sprintf_CurrTime((void *)arg);
- printf("->CurrTime:%s\r\n",arg);
- }
- else
- {
- printf("->Error Time Input!\r\n");
- shell_rx_rdy = 0;
- return;
- }
- }
- else if(StrComp(ptRxd,"wr date "))
- {
- retval = sscanf((void*)shell_rx_buff,"%*s%*s%*s%d-%d-%d %d",&i,&j,&k,&l);
- if(4 != retval)return;
- if(RTC_DateWrite((uint16_t)i,(uint8_t)j,(uint8_t)k,(uint8_t)l))
- {
- RTC_Sprintf_CurrTime((void *)arg);
- printf("->CurrTime:%s\r\n",arg);
- }
- else
- {
- printf("->Error Date Input!\r\n");
- shell_rx_rdy = 0;
- return;
- }
- }
- else if(StrComp(ptRxd,"help\r\n"))
- {
- shell_SendStr((void *)RTC_HelpMsg);
- }
- else return;
-
-
-
- shell_rx_rdy = 0;
- }
-
- #endif
更多全新視頻: www.makeru.com.cn/?t=12 嵌入式學習交流羣:561213221