Advertisement
Guest User

Untitled

a guest
Jul 29th, 2015
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.20 KB | None | 0 0
  1. /* Includes ------------------------------------------------------------------*/
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdarg.h>
  5. #include "stm32f10x.h"
  6. #include "platform_config.h"
  7. #include "com_config.h"
  8. /* Private typedef -----------------------------------------------------------*/
  9. /* Private define ------------------------------------------------------------*/
  10. /* Private macro -------------------------------------------------------------*/
  11. /* Private variables ---------------------------------------------------------*/
  12. const int8_t Welcome_Message[] = "\r\nHello\r\n";
  13. uint8_t RxData;
  14. uint8_t RxString[256];
  15. bool LEDFlag = FALSE;
  16.  
  17. /* Private function prototypes -----------------------------------------------*/
  18. void GPIO_Configuration(void);
  19. int rsscanf(const char* str, const char* format, ...);
  20. int hextoi(char ch);
  21. /* Private functions ---------------------------------------------------------*/
  22.  
  23. /**
  24. * @brief Main program.
  25. * @param None
  26. * @retval : None
  27. */
  28. int main(void)
  29. {
  30. int i = 0;
  31. char port;
  32. uint8_t value_u;
  33. uint8_t value_l;
  34. uint16_t value;
  35. int length;
  36.  
  37. // Configure board specific setting
  38. BoardInit();
  39. // Setting up COM port for Print function
  40. COM_Configuration();
  41. GPIO_Configuration();
  42.  
  43. //Send welcome messages
  44. cprintf(Welcome_Message);
  45.  
  46. // GPIO_SetBits(OB_LED_PORT, OB_LED_PIN);
  47. // GPIO_ResetBits(GPIOX_PORT, GPIOX_0_PIN );
  48.  
  49. while (1){
  50. i = 0;
  51. cprintf("> ");
  52.  
  53. while(1) {
  54. while(RX_BUFFER_IS_EMPTY);
  55. RxData = (int8_t)RECEIVE_DATA;
  56. cprintf("%c", RxData);
  57. if(RxData != '\r'){
  58. RxString[i] = RxData;
  59. RxString[i+1] = '\0';
  60. i++;
  61. continue;
  62. } else {
  63. length = i;
  64. break;
  65. }
  66. }
  67. cprintf("in: %s len:%d\n\r", RxString, length);
  68. // cprintf("What you type is : %c , %X , %u , %8b\r\n", RxData, RxData, RxData, RxData);
  69. // for(int j=0; j<i; j++)cprintf("%c(%d)", RxString[j], j);
  70. // cprintf("\n\r");
  71.  
  72. port = RxString[0];
  73. // rsscanf(&RxString[1], "%x", &value_u);
  74. // rsscanf(&RxString[3], "%x", &value_l);
  75. for(int j=1; j<5; j++){
  76. value = (value << 4 ) | (0x0f & hextoi(RxString[j]));
  77. }
  78. // cprintf("vu:%02x vl:%02x\n\r", value_u, value_l);
  79. // value = ((0xff & value_u) << 8) | (0xff & value_l);
  80.  
  81. cprintf("PORT:%c %04x \n\r", port, value);
  82. if(port == 'A' || port == 'a'){
  83. GPIO_Write(GPIOA, value);
  84. } else if(port == 'B' || port == 'b'){
  85. GPIO_Write(GPIOB, value);
  86. } else if(port == 'C' || port == 'c'){
  87. GPIO_Write(GPIOC, value);
  88. } else {
  89. cprintf("Invalid Port.\n\r");
  90. cprintf("PORTA:%04x PORTB:%04x PORTC:%04x\n\r",
  91. GPIO_ReadOutputData(GPIOA),
  92. GPIO_ReadOutputData(GPIOB),
  93. GPIO_ReadOutputData(GPIOC));
  94. }
  95. }
  96. }
  97.  
  98. /**
  99. * @brief Configure the GPIO Pins.
  100. * @param None
  101. * @retval : None
  102. */
  103. void GPIO_Configuration(void)
  104. {
  105. /* Supply APB2 clock */
  106. // RCC_APB2PeriphClockCmd(GPIOX_RCC | OB_LED_GPIO_RCC, ENABLE);
  107. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA
  108. | RCC_APB2Periph_GPIOB
  109. | RCC_APB2Periph_GPIOC
  110. , ENABLE);
  111. GPIO_InitTypeDef GPIO_InitStructure;
  112.  
  113. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
  114. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
  115. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  116. GPIO_Init(GPIOC, &GPIO_InitStructure);
  117. GPIO_SetBits(GPIOC, GPIO_Pin_All);
  118.  
  119. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
  120. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  121. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  122. GPIO_Init(GPIOB, &GPIO_InitStructure);
  123. GPIO_ResetBits(GPIOB, GPIO_Pin_All);
  124.  
  125. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15 | GPIO_Pin_14 |GPIO_Pin_13 |GPIO_Pin_12 |GPIO_Pin_11;
  126. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  127. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  128. GPIO_Init(GPIOA, &GPIO_InitStructure);
  129. GPIO_SetBits(GPIOA, GPIO_Pin_15 | GPIO_Pin_14 |GPIO_Pin_13 |GPIO_Pin_12 |GPIO_Pin_11);
  130.  
  131. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_7 |GPIO_Pin_6 |GPIO_Pin_5 |GPIO_Pin_4;
  132. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
  133. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  134. GPIO_Init(GPIOA, &GPIO_InitStructure);
  135. GPIO_ResetBits(GPIOA, GPIO_Pin_8 | GPIO_Pin_7 |GPIO_Pin_6 |GPIO_Pin_5 |GPIO_Pin_4);
  136. }
  137.  
  138. // Reduced version of scanf (%d, %x, %c, %n are supported)
  139. // %d dec integer (E.g.: 12)
  140. // %x hex integer (E.g.: 0xa0)
  141. // %b bin integer (E.g.: b1010100010)
  142. // %n hex, de or bin integer (e.g: 12, 0xa0, b1010100010)
  143. // %c any character
  144. //
  145. int rsscanf(const char* str, const char* format, ...)
  146. {
  147. va_list ap;
  148. int value, tmp;
  149. int count;
  150. int pos;
  151. char neg, fmt_code;
  152. const char* pf;
  153. va_start(ap, format);
  154. for (pf = format, count = 0; *format != 0 && *str != 0; format++, str++)
  155. {
  156. while (*format == ' ' && *format != 0)
  157. format++;
  158. if (*format == 0)
  159. break;
  160. while (*str == ' ' && *str != 0)
  161. str++;
  162. if (*str == 0)
  163. break;
  164. if (*format == '%')
  165. {
  166. format++;
  167. if (*format == 'n')
  168. {
  169. if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
  170. {
  171. fmt_code = 'x';
  172. str += 2;
  173. }
  174. else
  175. if (str[0] == 'b')
  176. {
  177. fmt_code = 'b';
  178. str++;
  179. }
  180. else
  181. fmt_code = 'd';
  182. }
  183. else
  184. fmt_code = *format;
  185. switch (fmt_code)
  186. {
  187. case 'x':
  188. case 'X':
  189. for (value = 0, pos = 0; *str != 0; str++, pos++)
  190. {
  191. if ('0' <= *str && *str <= '9')
  192. tmp = *str - '0';
  193. else
  194. if ('a' <= *str && *str <= 'f')
  195. tmp = *str - 'a' + 10;
  196. else
  197. if ('A' <= *str && *str <= 'F')
  198. tmp = *str - 'A' + 10;
  199. else
  200. break;
  201. value *= 16;
  202. value += tmp;
  203. }
  204. if (pos == 0)
  205. return count;
  206. *(va_arg(ap, int*)) = value;
  207. count++;
  208. break;
  209. case 'b':
  210. for (value = 0, pos = 0; *str != 0; str++, pos++)
  211. {
  212. if (*str != '0' && *str != '1')
  213. break;
  214. value *= 2;
  215. value += *str - '0';
  216. }
  217. if (pos == 0)
  218. return count;
  219. *(va_arg(ap, int*)) = value;
  220. count++;
  221. break;
  222. case 'd':
  223. if (*str == '-')
  224. {
  225. neg = 1;
  226. str++;
  227. }
  228. else
  229. neg = 0;
  230. for (value = 0, pos = 0; *str != 0; str++, pos++)
  231. {
  232. if ('0' <= *str && *str <= '9')
  233. value = value*10 + (int)(*str - '0');
  234. else
  235. break;
  236. }
  237. if (pos == 0)
  238. return count;
  239. *(va_arg(ap, int*)) = neg ? -value : value;
  240. count++;
  241. break;
  242. case 'c':
  243. *(va_arg(ap, char*)) = *str;
  244. count++;
  245. break;
  246. default:
  247. return count;
  248. }
  249. }
  250. else
  251. {
  252. if (*format != *str)
  253. break;
  254. }
  255. }
  256. va_end(ap);
  257. return count;
  258. }
  259.  
  260. int hextoi(char ch)
  261. {
  262. if(ch>='0' && ch<='9'){
  263. return ch - '0';
  264. } else if (ch>='a' && ch<='f'){
  265. return ch - 'a' + 10;
  266. } else {
  267. return 0;
  268. }
  269. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement