PageRenderTime 25ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/firmware/at91sam7/openbeacon-estimator/application/cmd.c

https://github.com/bomma/openbeacon
C | 293 lines | 250 code | 32 blank | 11 comment | 52 complexity | 3811a62c30f3a1427350337df34ab624 MD5 | raw file
  1. #include <AT91SAM7.h>
  2. #include <FreeRTOS.h>
  3. #include <task.h>
  4. #include <queue.h>
  5. #include <USB-CDC.h>
  6. #include <board.h>
  7. #include <beacontypes.h>
  8. #include <dbgu.h>
  9. #include <env.h>
  10. #include "proto.h"
  11. #include "cmd.h"
  12. #include "nRF24L01/nRF_API.h"
  13. #include "nRF24L01/nRF_HW.h"
  14. #include "led.h"
  15. #include "nRF24L01/nRF_CMD.h"
  16. xQueueHandle xCmdQueue;
  17. xTaskHandle xCmdTask;
  18. xTaskHandle xCmdRecvUsbTask;
  19. /**********************************************************************/
  20. char rs232_buffer[1024];
  21. unsigned int rs232_buffer_pos;
  22. void vSendByte(char data)
  23. {
  24. if(rs232_buffer_pos<sizeof(rs232_buffer))
  25. rs232_buffer[rs232_buffer_pos++]=data;
  26. if(data=='\r')
  27. {
  28. AT91F_DBGU_Frame(rs232_buffer,rs232_buffer_pos);
  29. rs232_buffer_pos=0;
  30. }
  31. vUSBSendByte(data);
  32. }
  33. void DumpUIntToUSB(unsigned int data)
  34. {
  35. int i=0;
  36. unsigned char buffer[10],*p=&buffer[sizeof(buffer)];
  37. do {
  38. *--p='0'+(unsigned char)(data%10);
  39. data/=10;
  40. i++;
  41. } while(data);
  42. while(i--)
  43. vSendByte(*p++);
  44. }
  45. /**********************************************************************/
  46. void DumpStringToUSB(char* text)
  47. {
  48. unsigned char data;
  49. if(text)
  50. while((data=*text++)!=0)
  51. vSendByte(data);
  52. }
  53. /**********************************************************************/
  54. static inline unsigned char HexChar(unsigned char nibble)
  55. {
  56. return nibble + ((nibble<0x0A) ? '0':('A'-0xA));
  57. }
  58. /**********************************************************************/
  59. void DumpBufferToUSB(char* buffer, int len)
  60. {
  61. int i;
  62. for(i=0; i<len; i++) {
  63. vSendByte(HexChar( *buffer >> 4));
  64. vSendByte(HexChar( *buffer++ & 0xf));
  65. }
  66. }
  67. /**********************************************************************/
  68. int atoiEx(const char * nptr)
  69. {
  70. int sign=1, i=0, curval=0;
  71. while(nptr[i] == ' ' && nptr[i] != 0)
  72. i++;
  73. if(nptr[i] != 0)
  74. {
  75. if(nptr[i] == '-')
  76. {
  77. sign *= -1;
  78. i++;
  79. }
  80. else
  81. if(nptr[i] == '+')
  82. i++;
  83. while(nptr[i] != 0 && nptr[i] >= '0' && nptr[i] <= '9')
  84. curval = curval * 10 + (nptr[i++] - '0');
  85. }
  86. return sign * curval;
  87. }
  88. /**********************************************************************/
  89. void prvExecCommand(u_int32_t cmd, portCHAR *args) {
  90. int i,h,m,s;
  91. i=0;
  92. switch(cmd)
  93. {
  94. case '4':
  95. case '3':
  96. case '2':
  97. case '1':
  98. env.e.mode=cmd-'0';
  99. DumpStringToUSB(" * Switched to transmit mode at power level ");
  100. DumpUIntToUSB(env.e.mode);
  101. DumpStringToUSB("\n\r");
  102. break;
  103. case '0':
  104. DumpStringToUSB(" * switched to RX mode\n\r");
  105. break;
  106. case 'S':
  107. env_store();
  108. DumpStringToUSB(" * Stored environment variables\n\r");
  109. break;
  110. case 'FIFO':
  111. i=PtSetFifoLifetimeSeconds(atoiEx(args));
  112. if(i<0)
  113. DumpStringToUSB(" * Invalid FIFO cache lifetime parameter\n\r");
  114. else
  115. {
  116. DumpStringToUSB("FIFO lifetime set to ");
  117. DumpUIntToUSB(i);
  118. DumpStringToUSB("\n\r");
  119. }
  120. break;
  121. case 'I':
  122. i=atoiEx(args);
  123. if(i!=0) {
  124. env.e.reader_id = i;
  125. DumpStringToUSB("Reader ID set to ");
  126. DumpUIntToUSB(env.e.reader_id);
  127. DumpStringToUSB("\n\r");
  128. }
  129. break;
  130. case 'C':
  131. DumpStringToUSB(
  132. " *****************************************************\n\r"
  133. " * Current configuration: *\n\r"
  134. " *****************************************************\n\r"
  135. " *\n\r");
  136. DumpStringToUSB(" * Uptime is ");
  137. s=xTaskGetTickCount()/1000;
  138. h=s/3600;
  139. s%=3600;
  140. m=s/60;
  141. s%=60;
  142. DumpUIntToUSB(h);
  143. DumpStringToUSB("h:");
  144. DumpUIntToUSB(m);
  145. DumpStringToUSB("m:");
  146. DumpUIntToUSB(s);
  147. DumpStringToUSB("s");
  148. DumpStringToUSB("\n\r");
  149. DumpStringToUSB(" * The reader id is ");
  150. DumpUIntToUSB(env.e.reader_id);
  151. DumpStringToUSB("\n\r");
  152. DumpStringToUSB(" * The mode is ");
  153. DumpUIntToUSB(env.e.mode);
  154. DumpStringToUSB("\n\r");
  155. DumpStringToUSB(" * The channel is ");
  156. DumpUIntToUSB(nRFAPI_GetChannel());
  157. DumpStringToUSB("\n\r");
  158. DumpStringToUSB(" * The FIFO cache lifetime is ");
  159. DumpUIntToUSB(PtGetFifoLifetimeSeconds());
  160. DumpStringToUSB("s\n\r");
  161. DumpStringToUSB(
  162. " *\n\r"
  163. " *****************************************************\n\r"
  164. );
  165. break;
  166. case 'H':
  167. case '?':
  168. DumpStringToUSB(
  169. " *****************************************************\n\r"
  170. " * OpenBeacon USB terminal *\n\r"
  171. " * (C) 2007 Milosch Meriac <meriac@openbeacon.de> *\n\r"
  172. " *****************************************************\n\r"
  173. " *\n\r"
  174. " * S - store transmitter settings\n\r"
  175. " * C - print configuration\n\r"
  176. " * I [id] - set reader id\n\r"
  177. " * FIFO [sec] - set FIFO cache lifetime in seconds\n\r"
  178. " * 0 - receive only mode\n\r"
  179. " * 1..4 - automatic transmit at selected power levels\n\r"
  180. " * ?,H - display this help screen\n\r"
  181. " *\n\r"
  182. " *****************************************************\n\r"
  183. );
  184. break;
  185. }
  186. }
  187. /**********************************************************************/
  188. // A task to execute commands
  189. void vCmdCode(void *pvParameters) {
  190. (void) pvParameters;
  191. u_int32_t cmd;
  192. portBASE_TYPE i, j=0;
  193. for(;;) {
  194. cmd_type next_command;
  195. cmd = j = 0;
  196. if( xQueueReceive(xCmdQueue, &next_command, ( portTickType ) 100 ) ) {
  197. DumpStringToUSB("Command received:");
  198. DumpStringToUSB(next_command.command);
  199. DumpStringToUSB("\n\r");
  200. while(next_command.command[j] == ' ' && next_command.command[j] != 0) {
  201. j++;
  202. }
  203. for(i=0;i<4;i++) {
  204. portCHAR cByte = next_command.command[i+j];
  205. if(next_command.command[i+j] == 0 || next_command.command[i+j] == ' ')
  206. break;
  207. if(cByte>='a' && cByte<='z') {
  208. cmd = (cmd<<8) | (cByte+('A'-'a'));
  209. } else cmd = (cmd<<8) | cByte;
  210. }
  211. while(next_command.command[i+j] == ' ' && next_command.command[i+j] != 0) {
  212. i++;
  213. }
  214. prvExecCommand(cmd, next_command.command+i+j);
  215. } else {
  216. }
  217. }
  218. }
  219. /**********************************************************************/
  220. // A task to read commands from USB
  221. void vCmdRecvUsbCode(void *pvParameters) {
  222. portBASE_TYPE len=0;
  223. cmd_type next_command = { source: SRC_USB, command: ""};
  224. (void) pvParameters;
  225. for( ;; ) {
  226. if(vUSBRecvByte(&next_command.command[len], 1, 100)) {
  227. next_command.command[len+1] = 0;
  228. DumpStringToUSB(next_command.command + len);
  229. if(next_command.command[len] == '\n' || next_command.command[len] == '\r') {
  230. next_command.command[len] = 0;
  231. if(len > 0) {
  232. if( xQueueSend(xCmdQueue, &next_command, 0) != pdTRUE) {
  233. DumpStringToUSB("Queue full, command can't be processed.\n");
  234. }
  235. len=0;
  236. }
  237. } else len++;
  238. if(len >= MAX_CMD_LEN-1) {
  239. DumpStringToUSB("ERROR: Command too long. Ignored.");
  240. len=0;
  241. }
  242. }
  243. }
  244. }
  245. portBASE_TYPE vCmdInit(void) {
  246. rs232_buffer_pos=0;
  247. /* FIXME Maybe modify to use pointers? */
  248. xCmdQueue = xQueueCreate( 10, sizeof(cmd_type) );
  249. if(xCmdQueue == 0) {
  250. return 0;
  251. }
  252. if(xTaskCreate(vCmdCode, (signed portCHAR *)"CMD", TASK_CMD_STACK, NULL,
  253. TASK_CMD_PRIORITY, &xCmdTask) != pdPASS) {
  254. return 0;
  255. }
  256. if(xTaskCreate(vCmdRecvUsbCode, (signed portCHAR *)"CMDUSB", TASK_CMD_STACK, NULL,
  257. TASK_CMD_PRIORITY, &xCmdRecvUsbTask) != pdPASS) {
  258. return 0;
  259. }
  260. return 1;
  261. }