weak current limit on spickles
[aversive.git] / projects / microb2009 / tests / spi_test / commands.c
1 /*
2  *  Copyright Droids Corporation (2008)
3  * 
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  *
18  *  Revision : $Id: commands.c,v 1.1 2009-01-30 20:42:17 zer0 Exp $
19  *
20  *  Olivier MATZ <zer0@droids-corp.org> 
21  */
22
23
24 #include <stdio.h>
25 #include <string.h>
26
27 #include <aversive/pgmspace.h>
28 #include <aversive/wait.h>
29
30 #include <parse.h>
31 #include <parse_num.h>
32 #include <parse_string.h>
33 #include <uart.h>
34 #include <spi.h>
35
36 #include "main.h"
37
38
39 /**********************************************************/
40 /* Reset */
41
42 /* this structure is filled when cmd_reset is parsed successfully */
43 struct cmd_reset_result {
44         fixed_string_t arg0;
45 };
46
47 /* function called when cmd_reset is parsed successfully */
48 static void cmd_reset_parsed(void * parsed_result, void * data)
49 {
50         reset();
51 }
52
53 prog_char str_reset_arg0[] = "reset";
54 parse_pgm_token_string_t cmd_reset_arg0 = TOKEN_STRING_INITIALIZER(struct cmd_reset_result, arg0, str_reset_arg0);
55
56 prog_char help_reset[] = "Reset the board";
57 parse_pgm_inst_t cmd_reset = {
58         .f = cmd_reset_parsed,  /* function to call */
59         .data = NULL,      /* 2nd arg of func */
60         .help_str = help_reset,
61         .tokens = {        /* token list, NULL terminated */
62                 (prog_void *)&cmd_reset_arg0, 
63                 NULL,
64         },
65 };
66
67 /**********************************************************/
68 /* Spi_Test */
69
70 /* this structure is filled when cmd_spi_test is parsed successfully */
71 struct cmd_spi_test_result {
72         fixed_string_t arg0;
73 };
74
75 /* function called when cmd_spi_test is parsed successfully */
76 static void cmd_spi_test_parsed(void * parsed_result, void * data)
77 {
78 #if 0
79         uint8_t i, ret;
80
81         for (i=0; i<3; i++) {
82                 spi_slave_select(0);
83                 ret = spi_send_and_receive_byte(i);
84                 spi_slave_deselect(0);
85                 printf_P(PSTR("Sent %d, received %d\r\n"), i, ret);
86         }
87 #else
88         printf_P(PSTR("disabled\r\n"));
89 #endif
90 }
91
92 prog_char str_spi_test_arg0[] = "spi_test";
93 parse_pgm_token_string_t cmd_spi_test_arg0 = TOKEN_STRING_INITIALIZER(struct cmd_spi_test_result, arg0, str_spi_test_arg0);
94
95 prog_char help_spi_test[] = "Test the SPI";
96 parse_pgm_inst_t cmd_spi_test = {
97         .f = cmd_spi_test_parsed,  /* function to call */
98         .data = NULL,      /* 2nd arg of func */
99         .help_str = help_spi_test,
100         .tokens = {        /* token list, NULL terminated */
101                 (prog_void *)&cmd_spi_test_arg0, 
102                 NULL,
103         },
104 };
105
106 /**********************************************************/
107 /* Bootloader */
108
109 /* this structure is filled when cmd_bootloader is parsed successfully */
110 struct cmd_bootloader_result {
111         fixed_string_t arg0;
112 };
113
114 /* function called when cmd_bootloader is parsed successfully */
115 static void cmd_bootloader_parsed(void * parsed_result, void * data)
116 {
117 #define BOOTLOADER_ADDR 0x1e000
118         if (pgm_read_byte_far(BOOTLOADER_ADDR) == 0xff) {
119                 printf_P(PSTR("Bootloader is not present\r\n"));
120                 return;
121         }
122         cli();
123         /* ... very specific :( */
124 #ifdef __AVR_ATmega128__
125         TIMSK = 0;
126         ETIMSK = 0;
127 #else
128         /* XXX */
129 #endif
130         EIMSK = 0;
131         UCSR0B = 0;
132         UCSR1B = 0;
133         SPCR = 0;
134         TWCR = 0;
135         ACSR = 0;
136         ADCSRA = 0;
137
138         __asm__ __volatile__ ("ldi r30,0x00\n");
139         __asm__ __volatile__ ("ldi r31,0xf0\n");
140         __asm__ __volatile__ ("ijmp\n");
141 }
142
143 prog_char str_bootloader_arg0[] = "bootloader";
144 parse_pgm_token_string_t cmd_bootloader_arg0 = TOKEN_STRING_INITIALIZER(struct cmd_bootloader_result, arg0, str_bootloader_arg0);
145
146 prog_char help_bootloader[] = "Bootloader the board";
147 parse_pgm_inst_t cmd_bootloader = {
148         .f = cmd_bootloader_parsed,  /* function to call */
149         .data = NULL,      /* 2nd arg of func */
150         .help_str = help_bootloader,
151         .tokens = {        /* token list, NULL terminated */
152                 (prog_void *)&cmd_bootloader_arg0, 
153                 NULL,
154         },
155 };
156
157 #ifdef notyet
158 /**********************************************************/
159 /* Encoders tests */
160
161 /* this structure is filled when cmd_encoders is parsed successfully */
162 struct cmd_encoders_result {
163         fixed_string_t arg0;
164         fixed_string_t arg1;
165 };
166
167 /* function called when cmd_encoders is parsed successfully */
168 static void cmd_encoders_parsed(void * parsed_result, void * data)
169 {
170         while(uart_recv_nowait(0) == -1) {
171                 printf_P(PSTR("% .8ld % .8ld % .8ld % .8ld\r\n"), 
172                          encoders_microb_get_value((void *)0),
173                          encoders_microb_get_value((void *)1),
174                          encoders_microb_get_value((void *)2),
175                          encoders_microb_get_value((void *)3));
176                 wait_ms(100);
177         }
178 }
179
180 prog_char str_encoders_arg0[] = "encoders";
181 parse_pgm_token_string_t cmd_encoders_arg0 = TOKEN_STRING_INITIALIZER(struct cmd_encoders_result, arg0, str_encoders_arg0);
182 prog_char str_encoders_arg1[] = "show";
183 parse_pgm_token_string_t cmd_encoders_arg1 = TOKEN_STRING_INITIALIZER(struct cmd_encoders_result, arg1, str_encoders_arg1);
184
185 prog_char help_encoders[] = "Show encoders values";
186 parse_pgm_inst_t cmd_encoders = {
187         .f = cmd_encoders_parsed,  /* function to call */
188         .data = NULL,      /* 2nd arg of func */
189         .help_str = help_encoders,
190         .tokens = {        /* token list, NULL terminated */
191                 (prog_void *)&cmd_encoders_arg0, 
192                 (prog_void *)&cmd_encoders_arg1, 
193                 NULL,
194         },
195 };
196 #endif
197
198 /**********************************************************/
199
200
201 /* in progmem */
202 parse_pgm_ctx_t main_ctx[] = {
203         (parse_pgm_inst_t *)&cmd_reset,
204         (parse_pgm_inst_t *)&cmd_spi_test,
205         (parse_pgm_inst_t *)&cmd_bootloader,
206 #ifdef notyet
207         (parse_pgm_inst_t *)&cmd_encoders,
208 #endif
209
210         NULL,
211 };