Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Includes ------------------------------------------------------------------*/
- #include <stdio.h>
- #include <string.h>
- #include <stdarg.h>
- #include "stm32f10x.h"
- #include "platform_config.h"
- #include "com_config.h"
- /* Private typedef -----------------------------------------------------------*/
- /* Private define ------------------------------------------------------------*/
- /* Private macro -------------------------------------------------------------*/
- /* Private variables ---------------------------------------------------------*/
- const int8_t Welcome_Message[] = "\r\nHello\r\n";
- uint8_t RxData;
- uint8_t RxString[256];
- bool LEDFlag = FALSE;
- /* Private function prototypes -----------------------------------------------*/
- void GPIO_Configuration(void);
- int rsscanf(const char* str, const char* format, ...);
- int hextoi(char ch);
- /* Private functions ---------------------------------------------------------*/
- /**
- * @brief Main program.
- * @param None
- * @retval : None
- */
- int main(void)
- {
- int i = 0;
- char port;
- uint8_t value_u;
- uint8_t value_l;
- uint16_t value;
- int length;
- // Configure board specific setting
- BoardInit();
- // Setting up COM port for Print function
- COM_Configuration();
- GPIO_Configuration();
- //Send welcome messages
- cprintf(Welcome_Message);
- // GPIO_SetBits(OB_LED_PORT, OB_LED_PIN);
- // GPIO_ResetBits(GPIOX_PORT, GPIOX_0_PIN );
- while (1){
- i = 0;
- cprintf("> ");
- while(1) {
- while(RX_BUFFER_IS_EMPTY);
- RxData = (int8_t)RECEIVE_DATA;
- cprintf("%c", RxData);
- if(RxData != '\r'){
- RxString[i] = RxData;
- RxString[i+1] = '\0';
- i++;
- continue;
- } else {
- length = i;
- break;
- }
- }
- cprintf("in: %s len:%d\n\r", RxString, length);
- // cprintf("What you type is : %c , %X , %u , %8b\r\n", RxData, RxData, RxData, RxData);
- // for(int j=0; j<i; j++)cprintf("%c(%d)", RxString[j], j);
- // cprintf("\n\r");
- port = RxString[0];
- // rsscanf(&RxString[1], "%x", &value_u);
- // rsscanf(&RxString[3], "%x", &value_l);
- for(int j=1; j<5; j++){
- value = (value << 4 ) | (0x0f & hextoi(RxString[j]));
- }
- // cprintf("vu:%02x vl:%02x\n\r", value_u, value_l);
- // value = ((0xff & value_u) << 8) | (0xff & value_l);
- cprintf("PORT:%c %04x \n\r", port, value);
- if(port == 'A' || port == 'a'){
- GPIO_Write(GPIOA, value);
- } else if(port == 'B' || port == 'b'){
- GPIO_Write(GPIOB, value);
- } else if(port == 'C' || port == 'c'){
- GPIO_Write(GPIOC, value);
- } else {
- cprintf("Invalid Port.\n\r");
- cprintf("PORTA:%04x PORTB:%04x PORTC:%04x\n\r",
- GPIO_ReadOutputData(GPIOA),
- GPIO_ReadOutputData(GPIOB),
- GPIO_ReadOutputData(GPIOC));
- }
- }
- }
- /**
- * @brief Configure the GPIO Pins.
- * @param None
- * @retval : None
- */
- void GPIO_Configuration(void)
- {
- /* Supply APB2 clock */
- // RCC_APB2PeriphClockCmd(GPIOX_RCC | OB_LED_GPIO_RCC, ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA
- | RCC_APB2Periph_GPIOB
- | RCC_APB2Periph_GPIOC
- , ENABLE);
- GPIO_InitTypeDef GPIO_InitStructure;
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOC, &GPIO_InitStructure);
- GPIO_SetBits(GPIOC, GPIO_Pin_All);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- GPIO_ResetBits(GPIOB, GPIO_Pin_All);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15 | GPIO_Pin_14 |GPIO_Pin_13 |GPIO_Pin_12 |GPIO_Pin_11;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- GPIO_SetBits(GPIOA, GPIO_Pin_15 | GPIO_Pin_14 |GPIO_Pin_13 |GPIO_Pin_12 |GPIO_Pin_11);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_7 |GPIO_Pin_6 |GPIO_Pin_5 |GPIO_Pin_4;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- GPIO_ResetBits(GPIOA, GPIO_Pin_8 | GPIO_Pin_7 |GPIO_Pin_6 |GPIO_Pin_5 |GPIO_Pin_4);
- }
- // Reduced version of scanf (%d, %x, %c, %n are supported)
- // %d dec integer (E.g.: 12)
- // %x hex integer (E.g.: 0xa0)
- // %b bin integer (E.g.: b1010100010)
- // %n hex, de or bin integer (e.g: 12, 0xa0, b1010100010)
- // %c any character
- //
- int rsscanf(const char* str, const char* format, ...)
- {
- va_list ap;
- int value, tmp;
- int count;
- int pos;
- char neg, fmt_code;
- const char* pf;
- va_start(ap, format);
- for (pf = format, count = 0; *format != 0 && *str != 0; format++, str++)
- {
- while (*format == ' ' && *format != 0)
- format++;
- if (*format == 0)
- break;
- while (*str == ' ' && *str != 0)
- str++;
- if (*str == 0)
- break;
- if (*format == '%')
- {
- format++;
- if (*format == 'n')
- {
- if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
- {
- fmt_code = 'x';
- str += 2;
- }
- else
- if (str[0] == 'b')
- {
- fmt_code = 'b';
- str++;
- }
- else
- fmt_code = 'd';
- }
- else
- fmt_code = *format;
- switch (fmt_code)
- {
- case 'x':
- case 'X':
- for (value = 0, pos = 0; *str != 0; str++, pos++)
- {
- if ('0' <= *str && *str <= '9')
- tmp = *str - '0';
- else
- if ('a' <= *str && *str <= 'f')
- tmp = *str - 'a' + 10;
- else
- if ('A' <= *str && *str <= 'F')
- tmp = *str - 'A' + 10;
- else
- break;
- value *= 16;
- value += tmp;
- }
- if (pos == 0)
- return count;
- *(va_arg(ap, int*)) = value;
- count++;
- break;
- case 'b':
- for (value = 0, pos = 0; *str != 0; str++, pos++)
- {
- if (*str != '0' && *str != '1')
- break;
- value *= 2;
- value += *str - '0';
- }
- if (pos == 0)
- return count;
- *(va_arg(ap, int*)) = value;
- count++;
- break;
- case 'd':
- if (*str == '-')
- {
- neg = 1;
- str++;
- }
- else
- neg = 0;
- for (value = 0, pos = 0; *str != 0; str++, pos++)
- {
- if ('0' <= *str && *str <= '9')
- value = value*10 + (int)(*str - '0');
- else
- break;
- }
- if (pos == 0)
- return count;
- *(va_arg(ap, int*)) = neg ? -value : value;
- count++;
- break;
- case 'c':
- *(va_arg(ap, char*)) = *str;
- count++;
- break;
- default:
- return count;
- }
- }
- else
- {
- if (*format != *str)
- break;
- }
- }
- va_end(ap);
- return count;
- }
- int hextoi(char ch)
- {
- if(ch>='0' && ch<='9'){
- return ch - '0';
- } else if (ch>='a' && ch<='f'){
- return ch - 'a' + 10;
- } else {
- return 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement