Satellite Observers Workbench. NOT yet complete, just published for forum posters to \"cherry pick\" pieces of code as requiered as an example.

Dependencies:   mbed

Committer:
AjK
Date:
Mon Oct 11 10:34:55 2010 +0000
Revision:
0:0a841b89d614
Totally Alpha quality as this project isn\t completed. Just publishing it as it answers many questions asked in the forums

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AjK 0:0a841b89d614 1 /****************************************************************************
AjK 0:0a841b89d614 2 * Copyright 2010 Andy Kirkham, Stellar Technologies Ltd
AjK 0:0a841b89d614 3 *
AjK 0:0a841b89d614 4 * This file is part of the Satellite Observers Workbench (SOWB).
AjK 0:0a841b89d614 5 *
AjK 0:0a841b89d614 6 * SOWB is free software: you can redistribute it and/or modify
AjK 0:0a841b89d614 7 * it under the terms of the GNU General Public License as published by
AjK 0:0a841b89d614 8 * the Free Software Foundation, either version 3 of the License, or
AjK 0:0a841b89d614 9 * (at your option) any later version.
AjK 0:0a841b89d614 10 *
AjK 0:0a841b89d614 11 * SOWB is distributed in the hope that it will be useful,
AjK 0:0a841b89d614 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
AjK 0:0a841b89d614 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
AjK 0:0a841b89d614 14 * GNU General Public License for more details.
AjK 0:0a841b89d614 15 *
AjK 0:0a841b89d614 16 * You should have received a copy of the GNU General Public License
AjK 0:0a841b89d614 17 * along with SOWB. If not, see <http://www.gnu.org/licenses/>.
AjK 0:0a841b89d614 18 *
AjK 0:0a841b89d614 19 * $Id: main.cpp 5 2010-07-12 20:51:11Z ajk $
AjK 0:0a841b89d614 20 *
AjK 0:0a841b89d614 21 ***************************************************************************/
AjK 0:0a841b89d614 22
AjK 0:0a841b89d614 23 #ifdef NEVERCOMPILETHIS
AjK 0:0a841b89d614 24
AjK 0:0a841b89d614 25 #include "mbed.h"
AjK 0:0a841b89d614 26 #include "nexstar.h"
AjK 0:0a841b89d614 27 #include "utils.h"
AjK 0:0a841b89d614 28 #include "debug.h"
AjK 0:0a841b89d614 29 #include "main.h"
AjK 0:0a841b89d614 30
AjK 0:0a841b89d614 31 DigitalOut led1(LED1);
AjK 0:0a841b89d614 32 DigitalOut led2(LED2);
AjK 0:0a841b89d614 33 DigitalOut led3(LED3);
AjK 0:0a841b89d614 34 DigitalOut led4(LED4);
AjK 0:0a841b89d614 35
AjK 0:0a841b89d614 36 /* The main place holder data structure for the Nexstar. */
AjK 0:0a841b89d614 37 NEXSTAR_DATA nexstar;
AjK 0:0a841b89d614 38
AjK 0:0a841b89d614 39 /* A timer to look for serial comms failure. */
AjK 0:0a841b89d614 40 OneMS timeout;
AjK 0:0a841b89d614 41
AjK 0:0a841b89d614 42 /** nexstar_process(void)
AjK 0:0a841b89d614 43 *
AjK 0:0a841b89d614 44 * Standard module _process function.
AjK 0:0a841b89d614 45 */
AjK 0:0a841b89d614 46 void nexstar_process(void) {
AjK 0:0a841b89d614 47 int i, j, q;
AjK 0:0a841b89d614 48 signed char status, type;
AjK 0:0a841b89d614 49
AjK 0:0a841b89d614 50 BLAT4;
AjK 0:0a841b89d614 51
AjK 0:0a841b89d614 52 /* Multiple queued requests will start automatically after the previous
AjK 0:0a841b89d614 53 request (see the ISR for details). However, it is possible that when
AjK 0:0a841b89d614 54 the ISR detects the completion of a request there are, at that time,
AjK 0:0a841b89d614 55 no more pending requests in the queue. So loop over the request queue
AjK 0:0a841b89d614 56 and if none are in progress, initiate a request transfer to the Nexstar. */
AjK 0:0a841b89d614 57 if (! nexstar_request_count_status(NEXSTAR_REQUEST_IN_PROGRESS)) {
AjK 0:0a841b89d614 58 if (nexstar_request_count_status(NEXSTAR_REQUEST_PENDING)) {
AjK 0:0a841b89d614 59 for (i = 0, q = nexstar.currentRequest; i < NEXSTAR_NUM_OF_PACKETS; i++) {
AjK 0:0a841b89d614 60 if (nexstar.commsPackets[q].requestStatus == NEXSTAR_REQUEST_PENDING) {
AjK 0:0a841b89d614 61 nexstar.currentRequest = q;
AjK 0:0a841b89d614 62 nexstar_send_request(q);
AjK 0:0a841b89d614 63 i = NEXSTAR_NUM_OF_PACKETS;
AjK 0:0a841b89d614 64 }
AjK 0:0a841b89d614 65 else {
AjK 0:0a841b89d614 66 NEXSTAR_NEXT_REQUEST(q);
AjK 0:0a841b89d614 67 }
AjK 0:0a841b89d614 68 }
AjK 0:0a841b89d614 69 }
AjK 0:0a841b89d614 70 }
AjK 0:0a841b89d614 71
AjK 0:0a841b89d614 72 /* look through all the comms request packets and see if any need processing. */
AjK 0:0a841b89d614 73 /*
AjK 0:0a841b89d614 74 for (i = 0; i < NEXSTAR_NUM_OF_PACKETS; i++) {
AjK 0:0a841b89d614 75 status = nexstar.commsPackets[i].requestStatus;
AjK 0:0a841b89d614 76 type = nexstar.commsPackets[i].requestType;
AjK 0:0a841b89d614 77 if (status == NEXSTAR_REQUEST_TIMEOUT) {
AjK 0:0a841b89d614 78 for (j = 0; j < NEXSTAR_NUM_OF_PACKETS; j++) {
AjK 0:0a841b89d614 79 REQUEST_SET_PACKET_STATUS(j, NEXSTAR_REQUEST_IDLE);
AjK 0:0a841b89d614 80 nexstar_request_packet_reset(&nexstar.commsPackets[j]);
AjK 0:0a841b89d614 81 }
AjK 0:0a841b89d614 82 NEXSTAR_SET_STATUS(NEXSTAR_NOT_CONNECTED);
AjK 0:0a841b89d614 83 }
AjK 0:0a841b89d614 84 else if (status == NEXSTAR_REQUEST_COMPLETE) {
AjK 0:0a841b89d614 85 switch (type) {
AjK 0:0a841b89d614 86 case NEXSTAR_ECHO_COMMS:
AjK 0:0a841b89d614 87 nexstar_request_echo(NEXSTAR_PROCESS_REQUEST, i);
AjK 0:0a841b89d614 88 break;
AjK 0:0a841b89d614 89 case NEXSTAR_ASK_IS_ALIGNED:
AjK 0:0a841b89d614 90 nexstar_request_get_aligned(NEXSTAR_PROCESS_REQUEST, i);
AjK 0:0a841b89d614 91 break;
AjK 0:0a841b89d614 92 case NEXSTAR_ASK_ALTAZM_POSITION:
AjK 0:0a841b89d614 93 nexstar_request_get_altazm(NEXSTAR_PROCESS_REQUEST, i);
AjK 0:0a841b89d614 94 break;
AjK 0:0a841b89d614 95 }
AjK 0:0a841b89d614 96 }
AjK 0:0a841b89d614 97 }
AjK 0:0a841b89d614 98 */
AjK 0:0a841b89d614 99
AjK 0:0a841b89d614 100 if (nexstar.commsPackets[nexstar.currentRequest].requestStatus; == NEXSTAR_REQUEST_COMPLETE) {
AjK 0:0a841b89d614 101 switch (type) {
AjK 0:0a841b89d614 102 case NEXSTAR_ECHO_COMMS:
AjK 0:0a841b89d614 103 nexstar_request_echo(NEXSTAR_PROCESS_REQUEST, nexstar.currentRequest);
AjK 0:0a841b89d614 104 break;
AjK 0:0a841b89d614 105 case NEXSTAR_ASK_IS_ALIGNED:
AjK 0:0a841b89d614 106 nexstar_request_get_aligned(NEXSTAR_PROCESS_REQUEST, nexstar.currentRequest);
AjK 0:0a841b89d614 107 break;
AjK 0:0a841b89d614 108 case NEXSTAR_ASK_ALTAZM_POSITION:
AjK 0:0a841b89d614 109 nexstar_request_get_altazm(NEXSTAR_PROCESS_REQUEST, nexstar.currentRequest);
AjK 0:0a841b89d614 110 break;
AjK 0:0a841b89d614 111 }
AjK 0:0a841b89d614 112 }
AjK 0:0a841b89d614 113
AjK 0:0a841b89d614 114 /* If the Nexstar is in an unknown state attempt to ask it for
AjK 0:0a841b89d614 115 alignment. Once we get a positive response we at least know
AjK 0:0a841b89d614 116 a Nexstar is connected and responding. Until we have that
AjK 0:0a841b89d614 117 knowledge there is little else we can do. */
AjK 0:0a841b89d614 118 switch(nexstar.status) {
AjK 0:0a841b89d614 119 case NEXSTAR_NOT_CONNECTED:
AjK 0:0a841b89d614 120 if (! nexstar_request_count_request_type(NEXSTAR_ECHO_COMMS)) {
AjK 0:0a841b89d614 121 Uart2_flush_rxfifo();
AjK 0:0a841b89d614 122 nexstar_request_echo(NEXSTAR_SEND_REQUEST, 0);
AjK 0:0a841b89d614 123 }
AjK 0:0a841b89d614 124 return;
AjK 0:0a841b89d614 125
AjK 0:0a841b89d614 126 case NEXSTAR_CONNECTED:
AjK 0:0a841b89d614 127 case NEXSTAR_NOT_ALIGNED:
AjK 0:0a841b89d614 128 if (! nexstar_request_count_request_type(NEXSTAR_ASK_IS_ALIGNED)) {
AjK 0:0a841b89d614 129 Uart2_flush_rxfifo();
AjK 0:0a841b89d614 130 nexstar_request_get_aligned(NEXSTAR_SEND_REQUEST, 0);
AjK 0:0a841b89d614 131 }
AjK 0:0a841b89d614 132 return;
AjK 0:0a841b89d614 133
AjK 0:0a841b89d614 134 default:
AjK 0:0a841b89d614 135 debug.printf("I don't know what to do! status is %d\r\n", nexstar.status);
AjK 0:0a841b89d614 136 break;
AjK 0:0a841b89d614 137 }
AjK 0:0a841b89d614 138
AjK 0:0a841b89d614 139 /* As possible as often, request the Nexstar's pointing position. */
AjK 0:0a841b89d614 140 if (! nexstar_request_count_request_type(NEXSTAR_ASK_ALTAZM_POSITION)) {
AjK 0:0a841b89d614 141 nexstar_request_get_altazm(NEXSTAR_SEND_REQUEST, 0);
AjK 0:0a841b89d614 142 }
AjK 0:0a841b89d614 143
AjK 0:0a841b89d614 144
AjK 0:0a841b89d614 145 }
AjK 0:0a841b89d614 146
AjK 0:0a841b89d614 147 /** nexstar_init(void)
AjK 0:0a841b89d614 148 *
AjK 0:0a841b89d614 149 * Standard module _init function.
AjK 0:0a841b89d614 150 */
AjK 0:0a841b89d614 151 void nexstar_init(void) {
AjK 0:0a841b89d614 152
AjK 0:0a841b89d614 153 /* Setup the global mode of this Nexstar. */
AjK 0:0a841b89d614 154 NEXSTAR_SET_STATUS(NEXSTAR_NOT_CONNECTED);
AjK 0:0a841b89d614 155
AjK 0:0a841b89d614 156 /* Initialise the comms packets and buffers. */
AjK 0:0a841b89d614 157 nexstar.currentRequest = 0;
AjK 0:0a841b89d614 158 nexstar.availableRequest = 0;
AjK 0:0a841b89d614 159 for (int i = 0; i < NEXSTAR_NUM_OF_PACKETS; i++) {
AjK 0:0a841b89d614 160 nexstar_request_packet_reset(&nexstar.commsPackets[i]);
AjK 0:0a841b89d614 161 //nexstar.commsPackets[i].requestType = 0;
AjK 0:0a841b89d614 162 //nexstar.commsPackets[i].requestStatus = NEXSTAR_REQUEST_IDLE;
AjK 0:0a841b89d614 163 //nexstar.commsPackets[i].bufferPointer = 0;
AjK 0:0a841b89d614 164 //nexstar.commsPackets[i].callback = NULL;
AjK 0:0a841b89d614 165 //memset(nexstar.commsPackets[i].buffer, 0, NEXSTAR_PACKET_BUFFER_SIZE);
AjK 0:0a841b89d614 166 }
AjK 0:0a841b89d614 167
AjK 0:0a841b89d614 168 /* Prepare the one-shot timeout timers. */
AjK 0:0a841b89d614 169 timeout.mode(ONEMS_MODE_TIMEOUT_CALLBACK);
AjK 0:0a841b89d614 170 timeout.attach(nexstar_timeout);
AjK 0:0a841b89d614 171
AjK 0:0a841b89d614 172 /* Initialise the UART we use. */
AjK 0:0a841b89d614 173 Uart2_init();
AjK 0:0a841b89d614 174 }
AjK 0:0a841b89d614 175
AjK 0:0a841b89d614 176 /** nexstar_request_packet_reset
AjK 0:0a841b89d614 177 *
AjK 0:0a841b89d614 178 * Return a request packet back to default state.
AjK 0:0a841b89d614 179 *
AjK 0:0a841b89d614 180 * @param NEXSTAR_COMMS_PACKET *p A pointer to the packet to reset.
AjK 0:0a841b89d614 181 */
AjK 0:0a841b89d614 182 void nexstar_request_packet_reset(NEXSTAR_COMMS_PACKET *p) {
AjK 0:0a841b89d614 183 p->requestType = 0;
AjK 0:0a841b89d614 184 p->bufferPointer = 0;
AjK 0:0a841b89d614 185 p->callback = NULL;
AjK 0:0a841b89d614 186 memset(p->buffer, 0, NEXSTAR_PACKET_BUFFER_SIZE);
AjK 0:0a841b89d614 187 p->requestStatus = NEXSTAR_REQUEST_IDLE;
AjK 0:0a841b89d614 188 }
AjK 0:0a841b89d614 189
AjK 0:0a841b89d614 190 /** nexstar_p(void)
AjK 0:0a841b89d614 191 *
AjK 0:0a841b89d614 192 * Get a pointer to the data structure for the Nexstar.
AjK 0:0a841b89d614 193 * Used by the API to get data.
AjK 0:0a841b89d614 194 */
AjK 0:0a841b89d614 195 NEXSTAR_DATA * nexstar_p(void) {
AjK 0:0a841b89d614 196 return &nexstar;
AjK 0:0a841b89d614 197 }
AjK 0:0a841b89d614 198
AjK 0:0a841b89d614 199 /** nexstar_request_count_request_type(char requestType)
AjK 0:0a841b89d614 200 *
AjK 0:0a841b89d614 201 * Used to find out if any of the packets are currently marked as the supplied request type.
AjK 0:0a841b89d614 202 *
AjK 0:0a841b89d614 203 * @param char The status to test.
AjK 0:0a841b89d614 204 * @return bool True otherwise false
AjK 0:0a841b89d614 205 */
AjK 0:0a841b89d614 206 int nexstar_request_count_request_type(char requestType) {
AjK 0:0a841b89d614 207 int count, i;
AjK 0:0a841b89d614 208
AjK 0:0a841b89d614 209 for (count = 0, i = 0; i < NEXSTAR_NUM_OF_PACKETS; i++) {
AjK 0:0a841b89d614 210 if (nexstar.commsPackets[i].requestType == requestType) {
AjK 0:0a841b89d614 211 count++;;
AjK 0:0a841b89d614 212 }
AjK 0:0a841b89d614 213 }
AjK 0:0a841b89d614 214 return count;
AjK 0:0a841b89d614 215 }
AjK 0:0a841b89d614 216
AjK 0:0a841b89d614 217 /** nexstar_request_count_status (char status)
AjK 0:0a841b89d614 218 *
AjK 0:0a841b89d614 219 * Used to find out if any of the packets are currently marked as the supplied status.
AjK 0:0a841b89d614 220 *
AjK 0:0a841b89d614 221 * @param char The status to test.
AjK 0:0a841b89d614 222 * @return bool True otherwise false
AjK 0:0a841b89d614 223 */
AjK 0:0a841b89d614 224 int nexstar_request_count_status(char status) {
AjK 0:0a841b89d614 225 int count, i;
AjK 0:0a841b89d614 226
AjK 0:0a841b89d614 227 for (count = 0, i = 0; i < NEXSTAR_NUM_OF_PACKETS; i++) {
AjK 0:0a841b89d614 228 if (nexstar.commsPackets[i].requestStatus == status) {
AjK 0:0a841b89d614 229 count++;;
AjK 0:0a841b89d614 230 }
AjK 0:0a841b89d614 231 }
AjK 0:0a841b89d614 232 return count;
AjK 0:0a841b89d614 233 }
AjK 0:0a841b89d614 234
AjK 0:0a841b89d614 235 /** nexstar_timeout(class OneMS *q)
AjK 0:0a841b89d614 236 *
AjK 0:0a841b89d614 237 * A callback for the timeout timer.
AjK 0:0a841b89d614 238 *
AjK 0:0a841b89d614 239 * @param pointer class OneMS that invoked the callback.
AjK 0:0a841b89d614 240 */
AjK 0:0a841b89d614 241 void nexstar_timeout(class OneMS *q) {
AjK 0:0a841b89d614 242 q->stop(); /* Ensure we stop this timer. */
AjK 0:0a841b89d614 243
AjK 0:0a841b89d614 244 /* Mark this request timed out. */
AjK 0:0a841b89d614 245 REQUEST_SET_PACKET_STATUS(nexstar.currentRequest, NEXSTAR_REQUEST_TIMEOUT);
AjK 0:0a841b89d614 246 }
AjK 0:0a841b89d614 247
AjK 0:0a841b89d614 248 /** nexstar_request_get_altazm(int mode, unsigned char handle)
AjK 0:0a841b89d614 249 *
AjK 0:0a841b89d614 250 * Used to either create a request to get the current AltAzm position of the Nextsra
AjK 0:0a841b89d614 251 * or to process a previously completed request.
AjK 0:0a841b89d614 252 *
AjK 0:0a841b89d614 253 * @param int mode Determine what action to create, queue request or process packet.
AjK 0:0a841b89d614 254 * @param unsigned char handle If processing a request this is the index of the packet to be processed.
AjK 0:0a841b89d614 255 * @return zero on failure or error, non-zero otherwise.
AjK 0:0a841b89d614 256 */
AjK 0:0a841b89d614 257 int nexstar_request_get_altazm(int mode, unsigned char handle) {
AjK 0:0a841b89d614 258
AjK 0:0a841b89d614 259 /* Create a request to get the ALT/AZM and queue it. */
AjK 0:0a841b89d614 260 if (mode == NEXSTAR_SEND_REQUEST) {
AjK 0:0a841b89d614 261 REQUEST_HANDLE;
AjK 0:0a841b89d614 262 nexstar.commsPackets[handle].buffer[0] = 'Z';
AjK 0:0a841b89d614 263 REQUEST_SET(NEXSTAR_ASK_ALTAZM_POSITION, 1);
AjK 0:0a841b89d614 264 led3 = 1;
AjK 0:0a841b89d614 265 return 1;
AjK 0:0a841b89d614 266 }
AjK 0:0a841b89d614 267
AjK 0:0a841b89d614 268 /* Given a completed request, parse the data and update our information. */
AjK 0:0a841b89d614 269 if (mode == NEXSTAR_PROCESS_REQUEST) {
AjK 0:0a841b89d614 270 if (nexstar.commsPackets[handle].requestStatus != NEXSTAR_REQUEST_COMPLETE) return 0;
AjK 0:0a841b89d614 271 char *buffer = nexstar.commsPackets[handle].buffer;
AjK 0:0a841b89d614 272 REQUEST_BUFFER_CHECK(9, 0);
AjK 0:0a841b89d614 273 nexstar.currentAltRaw = hex2bin(buffer + 5, 4);
AjK 0:0a841b89d614 274 nexstar.currentAlt = (double)(((double)nexstar.currentAltRaw / 65536.) * 360.);
AjK 0:0a841b89d614 275 nexstar.currentAzmRaw = hex2bin(buffer + 0, 4);
AjK 0:0a841b89d614 276 nexstar.currentAzm = (double)(((double)nexstar.currentAzmRaw / 65536.) * 360.);
AjK 0:0a841b89d614 277 nexstar_request_packet_reset(&nexstar.commsPackets[handle]);
AjK 0:0a841b89d614 278 REQUEST_SET_PACKET_STATUS(handle, NEXSTAR_REQUEST_IDLE);
AjK 0:0a841b89d614 279 led3 = 0;
AjK 0:0a841b89d614 280 return 1;
AjK 0:0a841b89d614 281 }
AjK 0:0a841b89d614 282
AjK 0:0a841b89d614 283 return 0;
AjK 0:0a841b89d614 284 }
AjK 0:0a841b89d614 285
AjK 0:0a841b89d614 286 /** nexstar_request_get_aligned(int mode, unsigned char handle)
AjK 0:0a841b89d614 287 *
AjK 0:0a841b89d614 288 * Used to either create a request to check teh Nexstar is aligned
AjK 0:0a841b89d614 289 * or to process a previously completed request.
AjK 0:0a841b89d614 290 *
AjK 0:0a841b89d614 291 * @param int mode Determine what action to create, queue request or process packet.
AjK 0:0a841b89d614 292 * @param unsigned char handle If processing a request this is the index of the packet to be processed.
AjK 0:0a841b89d614 293 * @return zero on failure or error, non-zero otherwise.
AjK 0:0a841b89d614 294 */
AjK 0:0a841b89d614 295 int nexstar_request_get_aligned(int mode, unsigned char handle) {
AjK 0:0a841b89d614 296
AjK 0:0a841b89d614 297 /* Create a request to get the ALT/AZM and queue it. */
AjK 0:0a841b89d614 298 if (mode == NEXSTAR_SEND_REQUEST) {
AjK 0:0a841b89d614 299 REQUEST_HANDLE;
AjK 0:0a841b89d614 300 nexstar.commsPackets[handle].buffer[0] = 'J';
AjK 0:0a841b89d614 301 nexstar.commsPackets[handle].timeout = 2000;
AjK 0:0a841b89d614 302 REQUEST_SET(NEXSTAR_ASK_IS_ALIGNED, 1);
AjK 0:0a841b89d614 303 led2 = 1;
AjK 0:0a841b89d614 304 return 1;
AjK 0:0a841b89d614 305 }
AjK 0:0a841b89d614 306
AjK 0:0a841b89d614 307 /* Given a completed request, parse the data and update our information. */
AjK 0:0a841b89d614 308 if (mode == NEXSTAR_PROCESS_REQUEST) {
AjK 0:0a841b89d614 309 if (nexstar.commsPackets[handle].requestStatus == NEXSTAR_REQUEST_TIMEOUT) {
AjK 0:0a841b89d614 310 NEXSTAR_SET_STATUS(NEXSTAR_NOT_CONNECTED);
AjK 0:0a841b89d614 311 nexstar_request_packet_reset(&nexstar.commsPackets[handle]);
AjK 0:0a841b89d614 312 led2 = 0;
AjK 0:0a841b89d614 313 return 0;
AjK 0:0a841b89d614 314 }
AjK 0:0a841b89d614 315 if (nexstar.commsPackets[handle].requestStatus != NEXSTAR_REQUEST_COMPLETE) return 0;
AjK 0:0a841b89d614 316 char *buffer = nexstar.commsPackets[handle].buffer;
AjK 0:0a841b89d614 317 REQUEST_BUFFER_CHECK(1, 0);
AjK 0:0a841b89d614 318 NEXSTAR_SET_STATUS((buffer[0] == 1) ? NEXSTAR_ALIGNED : NEXSTAR_NOT_ALIGNED);
AjK 0:0a841b89d614 319 nexstar_request_packet_reset(&nexstar.commsPackets[handle]);
AjK 0:0a841b89d614 320 REQUEST_SET_PACKET_STATUS(handle, NEXSTAR_REQUEST_IDLE);
AjK 0:0a841b89d614 321 led2 = 0;
AjK 0:0a841b89d614 322 return 1;
AjK 0:0a841b89d614 323 }
AjK 0:0a841b89d614 324
AjK 0:0a841b89d614 325 return 0;
AjK 0:0a841b89d614 326 }
AjK 0:0a841b89d614 327
AjK 0:0a841b89d614 328 /** nexstar_request_echo(int mode, unsigned char handle)
AjK 0:0a841b89d614 329 *
AjK 0:0a841b89d614 330 * Used to either echo a data byte off the Nexstar.
AjK 0:0a841b89d614 331 *
AjK 0:0a841b89d614 332 * @param int mode Determine what action to create, queue request or process packet.
AjK 0:0a841b89d614 333 * @param unsigned char handle If processing a request this is the index of the packet to be processed.
AjK 0:0a841b89d614 334 * @return zero on failure or error, non-zero otherwise.
AjK 0:0a841b89d614 335 */
AjK 0:0a841b89d614 336 int nexstar_request_echo(int mode, unsigned char handle) {
AjK 0:0a841b89d614 337
AjK 0:0a841b89d614 338 /* Create a request to get the ALT/AZM and queue it. */
AjK 0:0a841b89d614 339 if (mode == NEXSTAR_SEND_REQUEST) {
AjK 0:0a841b89d614 340 REQUEST_HANDLE;
AjK 0:0a841b89d614 341 nexstar.commsPackets[handle].buffer[0] = 'K';
AjK 0:0a841b89d614 342 nexstar.commsPackets[handle].buffer[1] = 'k';
AjK 0:0a841b89d614 343 nexstar.commsPackets[handle].timeout = 2000;
AjK 0:0a841b89d614 344 REQUEST_SET(NEXSTAR_ECHO_COMMS, 2);
AjK 0:0a841b89d614 345 led1 = 1;
AjK 0:0a841b89d614 346 return 1;
AjK 0:0a841b89d614 347 }
AjK 0:0a841b89d614 348
AjK 0:0a841b89d614 349 /* Given a completed request, parse the data and update our information. */
AjK 0:0a841b89d614 350 if (mode == NEXSTAR_PROCESS_REQUEST) {
AjK 0:0a841b89d614 351 led3 = 1;
AjK 0:0a841b89d614 352 if (nexstar.commsPackets[handle].requestStatus == NEXSTAR_REQUEST_TIMEOUT) {
AjK 0:0a841b89d614 353 NEXSTAR_SET_STATUS(NEXSTAR_NOT_CONNECTED);
AjK 0:0a841b89d614 354 nexstar_request_packet_reset(&nexstar.commsPackets[handle]);
AjK 0:0a841b89d614 355 led1 = 0;
AjK 0:0a841b89d614 356 return 0;
AjK 0:0a841b89d614 357 }
AjK 0:0a841b89d614 358 if (nexstar.commsPackets[handle].requestStatus != NEXSTAR_REQUEST_COMPLETE) return 0;
AjK 0:0a841b89d614 359 char *buffer = nexstar.commsPackets[handle].buffer;
AjK 0:0a841b89d614 360 REQUEST_BUFFER_CHECK(1, 0);
AjK 0:0a841b89d614 361 if (buffer[0] == 'k') debug.printf("YES! ");
AjK 0:0a841b89d614 362 else debug.printf("NO! ");
AjK 0:0a841b89d614 363
AjK 0:0a841b89d614 364 NEXSTAR_SET_STATUS((buffer[0] == 'k') ? NEXSTAR_CONNECTED : NEXSTAR_NOT_CONNECTED);
AjK 0:0a841b89d614 365 nexstar_request_packet_reset(&nexstar.commsPackets[handle]);
AjK 0:0a841b89d614 366 REQUEST_SET_PACKET_STATUS(handle, NEXSTAR_REQUEST_IDLE);
AjK 0:0a841b89d614 367 led1 = 0;
AjK 0:0a841b89d614 368 return 1;
AjK 0:0a841b89d614 369 }
AjK 0:0a841b89d614 370
AjK 0:0a841b89d614 371 return 0;
AjK 0:0a841b89d614 372 }
AjK 0:0a841b89d614 373
AjK 0:0a841b89d614 374 /** nexstar_request_set_tracking_mode(int mode, unsigned char handle, char tracking)
AjK 0:0a841b89d614 375 *
AjK 0:0a841b89d614 376 * Used to set the tracking mode of the Nexstar
AjK 0:0a841b89d614 377 *
AjK 0:0a841b89d614 378 * @param int mode Determine what action to create, queue request or process packet.
AjK 0:0a841b89d614 379 * @param unsigned char handle If processing a request this is the index of the packet to be processed.
AjK 0:0a841b89d614 380 * @param char tracking The mode to send.
AjK 0:0a841b89d614 381 * @return zero on failure or error, non-zero otherwise.
AjK 0:0a841b89d614 382 */
AjK 0:0a841b89d614 383 int nexstar_request_set_tracking_mode(int mode, unsigned char handle, char tracking) {
AjK 0:0a841b89d614 384
AjK 0:0a841b89d614 385 /* Create request and queue. */
AjK 0:0a841b89d614 386 if (mode == NEXSTAR_SEND_REQUEST) {
AjK 0:0a841b89d614 387 REQUEST_HANDLE;
AjK 0:0a841b89d614 388 nexstar.commsPackets[handle].buffer[0] = 'T';
AjK 0:0a841b89d614 389 nexstar.commsPackets[handle].buffer[1] = tracking;
AjK 0:0a841b89d614 390 REQUEST_SET(NEXSTAR_SET_TRACKING, 2);
AjK 0:0a841b89d614 391 return 1;
AjK 0:0a841b89d614 392 }
AjK 0:0a841b89d614 393
AjK 0:0a841b89d614 394 /* Given a completed request, parse the data and update our information. */
AjK 0:0a841b89d614 395 if (mode == NEXSTAR_PROCESS_REQUEST) {
AjK 0:0a841b89d614 396 if (nexstar.commsPackets[handle].requestStatus != NEXSTAR_REQUEST_COMPLETE) return 0;
AjK 0:0a841b89d614 397 char *buffer = nexstar.commsPackets[handle].buffer;
AjK 0:0a841b89d614 398 REQUEST_BUFFER_CHECK(0, 0);
AjK 0:0a841b89d614 399 nexstar_request_packet_reset(&nexstar.commsPackets[handle]);
AjK 0:0a841b89d614 400 REQUEST_SET_PACKET_STATUS(handle, NEXSTAR_REQUEST_IDLE);
AjK 0:0a841b89d614 401 return 1;
AjK 0:0a841b89d614 402 }
AjK 0:0a841b89d614 403
AjK 0:0a841b89d614 404 return 0;
AjK 0:0a841b89d614 405 }
AjK 0:0a841b89d614 406
AjK 0:0a841b89d614 407 /** nexstar_request_get_tracking_mode(int mode, unsigned char handle)
AjK 0:0a841b89d614 408 *
AjK 0:0a841b89d614 409 * Used to find out what tracking mode Nexstar is in.
AjK 0:0a841b89d614 410 *
AjK 0:0a841b89d614 411 * @param int mode Determine what action to create, queue request or process packet.
AjK 0:0a841b89d614 412 * @param unsigned char handle If processing a request this is the index of the packet to be processed.
AjK 0:0a841b89d614 413 * @return zero on failure or error, non-zero otherwise.
AjK 0:0a841b89d614 414 */
AjK 0:0a841b89d614 415 int nexstar_request_get_tracking_mode(int mode, unsigned char handle) {
AjK 0:0a841b89d614 416
AjK 0:0a841b89d614 417 /* Create request and queue. */
AjK 0:0a841b89d614 418 if (mode == NEXSTAR_SEND_REQUEST) {
AjK 0:0a841b89d614 419 REQUEST_HANDLE;
AjK 0:0a841b89d614 420 nexstar.commsPackets[handle].buffer[0] = 't';
AjK 0:0a841b89d614 421 REQUEST_SET(NEXSTAR_ASK_TRACKING, 1);
AjK 0:0a841b89d614 422 return 1;
AjK 0:0a841b89d614 423 }
AjK 0:0a841b89d614 424
AjK 0:0a841b89d614 425 /* Given a completed request, parse the data and update our information. */
AjK 0:0a841b89d614 426 if (mode == NEXSTAR_PROCESS_REQUEST) {
AjK 0:0a841b89d614 427 if (nexstar.commsPackets[handle].requestStatus != NEXSTAR_REQUEST_COMPLETE) return 0;
AjK 0:0a841b89d614 428 char *buffer = nexstar.commsPackets[handle].buffer;
AjK 0:0a841b89d614 429 REQUEST_BUFFER_CHECK(1, 0);
AjK 0:0a841b89d614 430 nexstar.trackingMode = buffer[0];
AjK 0:0a841b89d614 431 nexstar_request_packet_reset(&nexstar.commsPackets[handle]);
AjK 0:0a841b89d614 432 REQUEST_SET_PACKET_STATUS(handle, NEXSTAR_REQUEST_IDLE);
AjK 0:0a841b89d614 433 return 1;
AjK 0:0a841b89d614 434 }
AjK 0:0a841b89d614 435
AjK 0:0a841b89d614 436 return 0;
AjK 0:0a841b89d614 437 }
AjK 0:0a841b89d614 438
AjK 0:0a841b89d614 439 /** nexstar_request_set_slew(int mode, unsigned char handle, double alt, double azm)
AjK 0:0a841b89d614 440 *
AjK 0:0a841b89d614 441 * Used to set a variable slew rate.
AjK 0:0a841b89d614 442 *
AjK 0:0a841b89d614 443 * @param int mode Determine what action to create, queue request or process packet.
AjK 0:0a841b89d614 444 * @param unsigned char handle If processing a request this is the index of the packet to be processed.
AjK 0:0a841b89d614 445 * @param double alt The alt slew rate in degrees per second.
AjK 0:0a841b89d614 446 * @param double azm The azm slew rate in degrees per second.
AjK 0:0a841b89d614 447 * @return zero on failure or error, non-zero otherwise.
AjK 0:0a841b89d614 448 */
AjK 0:0a841b89d614 449 int nexstar_request_set_slew(int mode, unsigned char handle, double alt, double azm) {
AjK 0:0a841b89d614 450 int i;
AjK 0:0a841b89d614 451
AjK 0:0a841b89d614 452 /* Create request and queue. */
AjK 0:0a841b89d614 453 if (mode == NEXSTAR_SEND_REQUEST) {
AjK 0:0a841b89d614 454 REQUEST_HANDLE;
AjK 0:0a841b89d614 455 nexstar.commsPackets[handle].buffer[0] = 'P';
AjK 0:0a841b89d614 456 nexstar.commsPackets[handle].buffer[1] = '\x3';
AjK 0:0a841b89d614 457 nexstar.commsPackets[handle].buffer[2] = '\x11';
AjK 0:0a841b89d614 458 nexstar.commsPackets[handle].buffer[3] = alt > 0 ? '\x6' : '\x7';
AjK 0:0a841b89d614 459 i = ((int)(alt * 3600.)) * 4;
AjK 0:0a841b89d614 460 nexstar.commsPackets[handle].buffer[4] = (char)((i & 0xFF00) >> 8);
AjK 0:0a841b89d614 461 nexstar.commsPackets[handle].buffer[5] = (char)(i & 0xFF);
AjK 0:0a841b89d614 462 nexstar.commsPackets[handle].buffer[6] = '\0';
AjK 0:0a841b89d614 463 nexstar.commsPackets[handle].buffer[7] = '\0';
AjK 0:0a841b89d614 464 REQUEST_SET(NEXSTAR_SET_ALT_RATE, 8);
AjK 0:0a841b89d614 465
AjK 0:0a841b89d614 466 REQUEST_HANDLE;
AjK 0:0a841b89d614 467 nexstar.commsPackets[handle].buffer[0] = 'P';
AjK 0:0a841b89d614 468 nexstar.commsPackets[handle].buffer[1] = '\x3';
AjK 0:0a841b89d614 469 nexstar.commsPackets[handle].buffer[2] = '\x10';
AjK 0:0a841b89d614 470 nexstar.commsPackets[handle].buffer[3] = azm > 0 ? '\x6' : '\x7';
AjK 0:0a841b89d614 471 i = ((int)(azm * 3600.)) * 4;
AjK 0:0a841b89d614 472 nexstar.commsPackets[handle].buffer[4] = (char)((i & 0xFF00) >> 8);
AjK 0:0a841b89d614 473 nexstar.commsPackets[handle].buffer[5] = (char)(i & 0xFF);
AjK 0:0a841b89d614 474 nexstar.commsPackets[handle].buffer[6] = '\0';
AjK 0:0a841b89d614 475 nexstar.commsPackets[handle].buffer[7] = '\0';
AjK 0:0a841b89d614 476 REQUEST_SET(NEXSTAR_SET_AZM_RATE, 8);
AjK 0:0a841b89d614 477 return 1;
AjK 0:0a841b89d614 478 }
AjK 0:0a841b89d614 479
AjK 0:0a841b89d614 480 /* Given a completed request, parse the data and update our information. */
AjK 0:0a841b89d614 481 if (mode == NEXSTAR_PROCESS_REQUEST) {
AjK 0:0a841b89d614 482 if (nexstar.commsPackets[handle].requestStatus != NEXSTAR_REQUEST_COMPLETE) return 0;
AjK 0:0a841b89d614 483 char *buffer = nexstar.commsPackets[handle].buffer;
AjK 0:0a841b89d614 484 REQUEST_BUFFER_CHECK(0, 0);
AjK 0:0a841b89d614 485 nexstar_request_packet_reset(&nexstar.commsPackets[handle]);
AjK 0:0a841b89d614 486 REQUEST_SET_PACKET_STATUS(handle, NEXSTAR_REQUEST_IDLE);
AjK 0:0a841b89d614 487 return 1;
AjK 0:0a841b89d614 488 }
AjK 0:0a841b89d614 489
AjK 0:0a841b89d614 490 return 0;
AjK 0:0a841b89d614 491 }
AjK 0:0a841b89d614 492
AjK 0:0a841b89d614 493 /** nexstar_request_set_location(int mode, unsigned char handle, NEXSTAR_LOCATION *location)
AjK 0:0a841b89d614 494 *
AjK 0:0a841b89d614 495 * Used to set the Nexstar's location.
AjK 0:0a841b89d614 496 *
AjK 0:0a841b89d614 497 * @param int mode Determine what action to create, queue request or process packet.
AjK 0:0a841b89d614 498 * @param unsigned char handle If processing a request this is the index of the packet to be processed.
AjK 0:0a841b89d614 499 * @param NEXSTAR_LOCATION *location A pointer to a data struct holding the data.
AjK 0:0a841b89d614 500 * @return zero on failure or error, non-zero otherwise.
AjK 0:0a841b89d614 501 */
AjK 0:0a841b89d614 502 int nexstar_request_set_location(int mode, unsigned char handle, NEXSTAR_LOCATION *location) {
AjK 0:0a841b89d614 503
AjK 0:0a841b89d614 504 /* Create request and queue. */
AjK 0:0a841b89d614 505 if (mode == NEXSTAR_SEND_REQUEST) {
AjK 0:0a841b89d614 506 REQUEST_HANDLE;
AjK 0:0a841b89d614 507 nexstar.commsPackets[handle].buffer[0] = 'W';
AjK 0:0a841b89d614 508 memcpy((char *)nexstar.commsPackets[handle].buffer + 1, (char *)location, 8);
AjK 0:0a841b89d614 509 REQUEST_SET(NEXSTAR_SET_LOCATION, 9);
AjK 0:0a841b89d614 510 return 1;
AjK 0:0a841b89d614 511 }
AjK 0:0a841b89d614 512
AjK 0:0a841b89d614 513 /* Given a completed request, parse the data and update our information. */
AjK 0:0a841b89d614 514 if (mode == NEXSTAR_PROCESS_REQUEST) {
AjK 0:0a841b89d614 515 if (nexstar.commsPackets[handle].requestStatus != NEXSTAR_REQUEST_COMPLETE) return 0;
AjK 0:0a841b89d614 516 char *buffer = nexstar.commsPackets[handle].buffer;
AjK 0:0a841b89d614 517 REQUEST_BUFFER_CHECK(1, 0);
AjK 0:0a841b89d614 518 nexstar.trackingMode = buffer[0];
AjK 0:0a841b89d614 519 nexstar_request_packet_reset(&nexstar.commsPackets[handle]);
AjK 0:0a841b89d614 520 REQUEST_SET_PACKET_STATUS(handle, NEXSTAR_REQUEST_IDLE);
AjK 0:0a841b89d614 521 return 1;
AjK 0:0a841b89d614 522 }
AjK 0:0a841b89d614 523
AjK 0:0a841b89d614 524 return 0;
AjK 0:0a841b89d614 525 }
AjK 0:0a841b89d614 526
AjK 0:0a841b89d614 527 /** nexstar_request_set_time(int mode, unsigned char handle, NEXSTAR_TIME *time)
AjK 0:0a841b89d614 528 *
AjK 0:0a841b89d614 529 * Used to set the Nexstar time.
AjK 0:0a841b89d614 530 *
AjK 0:0a841b89d614 531 * @param int mode Determine what action to create, queue request or process packet.
AjK 0:0a841b89d614 532 * @param unsigned char handle If processing a request this is the index of the packet to be processed.
AjK 0:0a841b89d614 533 * @param NEXSTAR_TIME *time A pointer to a data struct holding the data.
AjK 0:0a841b89d614 534 * @return zero on failure or error, non-zero otherwise.
AjK 0:0a841b89d614 535 */
AjK 0:0a841b89d614 536 int nexstar_request_set_time(int mode, unsigned char handle, NEXSTAR_TIME *time) {
AjK 0:0a841b89d614 537
AjK 0:0a841b89d614 538 /* Create request and queue. */
AjK 0:0a841b89d614 539 if (mode == NEXSTAR_SEND_REQUEST) {
AjK 0:0a841b89d614 540 REQUEST_HANDLE;
AjK 0:0a841b89d614 541 nexstar.commsPackets[handle].buffer[0] = 'H';
AjK 0:0a841b89d614 542 memcpy((char *)nexstar.commsPackets[handle].buffer + 1, (char *)time, 8);
AjK 0:0a841b89d614 543 REQUEST_SET(NEXSTAR_SET_TIME, 9);
AjK 0:0a841b89d614 544 return 1;
AjK 0:0a841b89d614 545 }
AjK 0:0a841b89d614 546
AjK 0:0a841b89d614 547 /* Given a completed request, parse the data and update our information. */
AjK 0:0a841b89d614 548 if (mode == NEXSTAR_PROCESS_REQUEST) {
AjK 0:0a841b89d614 549 if (nexstar.commsPackets[handle].requestStatus != NEXSTAR_REQUEST_COMPLETE) return 0;
AjK 0:0a841b89d614 550 char *buffer = nexstar.commsPackets[handle].buffer;
AjK 0:0a841b89d614 551 REQUEST_BUFFER_CHECK(0, 0);
AjK 0:0a841b89d614 552 nexstar_request_packet_reset(&nexstar.commsPackets[handle]);
AjK 0:0a841b89d614 553 REQUEST_SET_PACKET_STATUS(handle, NEXSTAR_REQUEST_IDLE);
AjK 0:0a841b89d614 554 return 1;
AjK 0:0a841b89d614 555 }
AjK 0:0a841b89d614 556
AjK 0:0a841b89d614 557 return 0;
AjK 0:0a841b89d614 558 }
AjK 0:0a841b89d614 559
AjK 0:0a841b89d614 560 /** nexstar_request_get_version(int mode, unsigned char handle)
AjK 0:0a841b89d614 561 *
AjK 0:0a841b89d614 562 * Used to set the Nexstar software version.
AjK 0:0a841b89d614 563 *
AjK 0:0a841b89d614 564 * @param int mode Determine what action to create, queue request or process packet.
AjK 0:0a841b89d614 565 * @param unsigned char handle If processing a request this is the index of the packet to be processed.
AjK 0:0a841b89d614 566 * @return zero on failure or error, non-zero otherwise.
AjK 0:0a841b89d614 567 */
AjK 0:0a841b89d614 568 int nexstar_request_get_version(int mode, unsigned char handle) {
AjK 0:0a841b89d614 569
AjK 0:0a841b89d614 570 /* Create request and queue. */
AjK 0:0a841b89d614 571 if (mode == NEXSTAR_SEND_REQUEST) {
AjK 0:0a841b89d614 572 REQUEST_HANDLE;
AjK 0:0a841b89d614 573 nexstar.commsPackets[handle].buffer[0] = 'V';
AjK 0:0a841b89d614 574 REQUEST_SET(NEXSTAR_ASK_VERSION, 1);
AjK 0:0a841b89d614 575 return 1;
AjK 0:0a841b89d614 576 }
AjK 0:0a841b89d614 577
AjK 0:0a841b89d614 578 /* Given a completed request, parse the data and update our information. */
AjK 0:0a841b89d614 579 if (mode == NEXSTAR_PROCESS_REQUEST) {
AjK 0:0a841b89d614 580 if (nexstar.commsPackets[handle].requestStatus != NEXSTAR_REQUEST_COMPLETE) return 0;
AjK 0:0a841b89d614 581 char *buffer = nexstar.commsPackets[handle].buffer;
AjK 0:0a841b89d614 582 REQUEST_BUFFER_CHECK(2, 0);
AjK 0:0a841b89d614 583 nexstar.version = (buffer[0] << 8) | buffer[1];
AjK 0:0a841b89d614 584 nexstar_request_packet_reset(&nexstar.commsPackets[handle]);
AjK 0:0a841b89d614 585 REQUEST_SET_PACKET_STATUS(handle, NEXSTAR_REQUEST_IDLE);
AjK 0:0a841b89d614 586 return 1;
AjK 0:0a841b89d614 587 }
AjK 0:0a841b89d614 588
AjK 0:0a841b89d614 589 return 0;
AjK 0:0a841b89d614 590 }
AjK 0:0a841b89d614 591
AjK 0:0a841b89d614 592 /** nexstar_request_raw(int mode, unsigned char handle, NEXSTAR_COMMS_PACKET *q)
AjK 0:0a841b89d614 593 *
AjK 0:0a841b89d614 594 * Used for external callers to setup a REQUEST based on an externally defined packet structure.
AjK 0:0a841b89d614 595 *
AjK 0:0a841b89d614 596 * This function allows other parts of the system to make Nextstar requests that are not covered
AjK 0:0a841b89d614 597 * by the API or just want to handle a request via a callback.
AjK 0:0a841b89d614 598 *
AjK 0:0a841b89d614 599 * @param int mode Determine what action to create, queue request or process packet.
AjK 0:0a841b89d614 600 * @param unsigned char handle If processing a request this is the index of the packet to be processed.
AjK 0:0a841b89d614 601 * @param pointer A pointer to a predefined comms request packet to copy into our queue.
AjK 0:0a841b89d614 602 * @return zero on failure or error, non-zero otherwise.
AjK 0:0a841b89d614 603 */
AjK 0:0a841b89d614 604 int nexstar_request_raw(int mode, unsigned char handle, NEXSTAR_COMMS_PACKET *q) {
AjK 0:0a841b89d614 605
AjK 0:0a841b89d614 606 /* Create request and queue. */
AjK 0:0a841b89d614 607 if (mode == NEXSTAR_SEND_REQUEST) {
AjK 0:0a841b89d614 608 /* Externally handled requests must have a callback defined as a completion handler. */
AjK 0:0a841b89d614 609 if (q->callback == NULL) {
AjK 0:0a841b89d614 610 return 0;
AjK 0:0a841b89d614 611 }
AjK 0:0a841b89d614 612 REQUEST_HANDLE;
AjK 0:0a841b89d614 613 memcpy(&nexstar.commsPackets[handle], q, sizeof(NEXSTAR_COMMS_PACKET));
AjK 0:0a841b89d614 614 REQUEST_SET(NEXSTAR_REQUEST_RAW, 0);
AjK 0:0a841b89d614 615 return 1;
AjK 0:0a841b89d614 616 }
AjK 0:0a841b89d614 617
AjK 0:0a841b89d614 618 /* Given a completed request, parse the data and update our information. */
AjK 0:0a841b89d614 619 if (mode == NEXSTAR_PROCESS_REQUEST) {
AjK 0:0a841b89d614 620 if (nexstar.commsPackets[handle].requestStatus != NEXSTAR_REQUEST_COMPLETE) return 0;
AjK 0:0a841b89d614 621 if (nexstar.commsPackets[handle].callback != NULL) {
AjK 0:0a841b89d614 622 (nexstar.commsPackets[handle].callback)(&nexstar.commsPackets[handle]);
AjK 0:0a841b89d614 623 }
AjK 0:0a841b89d614 624 nexstar_request_packet_reset(&nexstar.commsPackets[handle]);
AjK 0:0a841b89d614 625 REQUEST_SET_PACKET_STATUS(handle, NEXSTAR_REQUEST_IDLE);
AjK 0:0a841b89d614 626 return 1;
AjK 0:0a841b89d614 627 }
AjK 0:0a841b89d614 628
AjK 0:0a841b89d614 629 return 0;
AjK 0:0a841b89d614 630 }
AjK 0:0a841b89d614 631
AjK 0:0a841b89d614 632
AjK 0:0a841b89d614 633 /** nexstar_send_request(int handle)
AjK 0:0a841b89d614 634 *
AjK 0:0a841b89d614 635 * Used to send a request packet to Nexstar via the serial port.
AjK 0:0a841b89d614 636 *
AjK 0:0a841b89d614 637 * @param int handle The packet to send.
AjK 0:0a841b89d614 638 * @return zero on failure, non-zero otherwise.
AjK 0:0a841b89d614 639 */
AjK 0:0a841b89d614 640 int nexstar_send_request(int handle) {
AjK 0:0a841b89d614 641 if (nexstar.commsPackets[handle].requestStatus == NEXSTAR_REQUEST_PENDING) {
AjK 0:0a841b89d614 642 nexstar.commsPackets[handle].requestStatus = NEXSTAR_REQUEST_IN_PROGRESS;
AjK 0:0a841b89d614 643 if (Uart2_puts(nexstar.commsPackets[handle].buffer, (int)nexstar.commsPackets[handle].txLength) != 0) {
AjK 0:0a841b89d614 644 memset(nexstar.commsPackets[handle].buffer, 0, NEXSTAR_PACKET_BUFFER_SIZE);
AjK 0:0a841b89d614 645 nexstar.commsPackets[handle].bufferPointer = 0;
AjK 0:0a841b89d614 646 // Note, we only start the timeout timer AFTER the TxFIFO goes empty, see below.
AjK 0:0a841b89d614 647 return 1;
AjK 0:0a841b89d614 648 }
AjK 0:0a841b89d614 649 else {
AjK 0:0a841b89d614 650 /* If we failed to fill the Tx FIFo then switch this request back to pending. */
AjK 0:0a841b89d614 651 REQUEST_SET_PACKET_STATUS(handle, NEXSTAR_REQUEST_PENDING);
AjK 0:0a841b89d614 652 return 0;
AjK 0:0a841b89d614 653 }
AjK 0:0a841b89d614 654 }
AjK 0:0a841b89d614 655 return 0;
AjK 0:0a841b89d614 656 }
AjK 0:0a841b89d614 657
AjK 0:0a841b89d614 658 /** UART2_IRQHandler(void)
AjK 0:0a841b89d614 659 *
AjK 0:0a841b89d614 660 * The interrupt service routine for the UART2.
AjK 0:0a841b89d614 661 */
AjK 0:0a841b89d614 662 extern "C" void UART2_IRQHandler(void) {
AjK 0:0a841b89d614 663 char c;
AjK 0:0a841b89d614 664 uint32_t iir;
AjK 0:0a841b89d614 665
AjK 0:0a841b89d614 666 /* Get the interrupt identification register which also resets IIR. */
AjK 0:0a841b89d614 667 iir = LPC_UART2->IIR;
AjK 0:0a841b89d614 668
AjK 0:0a841b89d614 669 if (iir & 0x0001) {
AjK 0:0a841b89d614 670 /* Eh? Then why did we interrupt? */
AjK 0:0a841b89d614 671 return;
AjK 0:0a841b89d614 672 }
AjK 0:0a841b89d614 673
AjK 0:0a841b89d614 674 /* Do we have incoming data? */
AjK 0:0a841b89d614 675 if (iir & UART_ISSET_RDA) {
AjK 0:0a841b89d614 676 c = (char)LPC_UART2->RBR;
AjK 0:0a841b89d614 677 //debug.printf(" %c", c & 0x7f);
AjK 0:0a841b89d614 678 if (nexstar.commsPackets[nexstar.currentRequest].requestStatus != NEXSTAR_REQUEST_IN_PROGRESS) {
AjK 0:0a841b89d614 679 return;
AjK 0:0a841b89d614 680 }
AjK 0:0a841b89d614 681 nexstar.commsPackets[nexstar.currentRequest].buffer[nexstar.commsPackets[nexstar.currentRequest].bufferPointer] = c;
AjK 0:0a841b89d614 682 nexstar.commsPackets[nexstar.currentRequest].bufferPointer++;
AjK 0:0a841b89d614 683 nexstar.commsPackets[nexstar.currentRequest].bufferPointer &= (NEXSTAR_PACKET_BUFFER_SIZE-1);
AjK 0:0a841b89d614 684 if (c == '#' || c == '\r' || c == '\n') {
AjK 0:0a841b89d614 685 timeout.stop(); /* Halt the timeout timer. */
AjK 0:0a841b89d614 686 REQUEST_SET_PACKET_STATUS(nexstar.currentRequest, NEXSTAR_REQUEST_COMPLETE);
AjK 0:0a841b89d614 687
AjK 0:0a841b89d614 688 /* Advance to the next request packet and if pending start it. */
AjK 0:0a841b89d614 689 //NEXSTAR_NEXT_REQUEST(nexstar.currentRequest);
AjK 0:0a841b89d614 690 //nexstar_send_request(nexstar.currentRequest);
AjK 0:0a841b89d614 691 //debug.printf("\r\n");
AjK 0:0a841b89d614 692 }
AjK 0:0a841b89d614 693 }
AjK 0:0a841b89d614 694
AjK 0:0a841b89d614 695 /* If the THRE goes empty AND the TxFIFO is also empty then a command
AjK 0:0a841b89d614 696 was just sent to the Nexstar. Start the serial timeout timer so we
AjK 0:0a841b89d614 697 only make a timeout measurement AFTER we have sent our command.
AjK 0:0a841b89d614 698 That way we don't include our serial transmit time within the timeout
AjK 0:0a841b89d614 699 specified by the timer. */
AjK 0:0a841b89d614 700 if (iir & UART_ISSET_THRE) {
AjK 0:0a841b89d614 701 if ((LPC_UART2->FIFOLVL & UART_ISSET_FIFOLVL_TXFULL) == 0) {
AjK 0:0a841b89d614 702 timeout.start(nexstar.commsPackets[nexstar.currentRequest].timeout == 0 ? NEXSTAR_SERIAL_TIMEOUT : (uint32_t)nexstar.commsPackets[nexstar.currentRequest].timeout);
AjK 0:0a841b89d614 703 }
AjK 0:0a841b89d614 704 }
AjK 0:0a841b89d614 705
AjK 0:0a841b89d614 706 if (iir & UART_ISSET_RLS) {
AjK 0:0a841b89d614 707 // Do nothing for now other than read the iir register and clear the interrupt.
AjK 0:0a841b89d614 708 }
AjK 0:0a841b89d614 709 }
AjK 0:0a841b89d614 710
AjK 0:0a841b89d614 711 /** Uart2_init
AjK 0:0a841b89d614 712 *
AjK 0:0a841b89d614 713 * Initialise UART2 to our requirements for Nexstar.
AjK 0:0a841b89d614 714 */
AjK 0:0a841b89d614 715 void Uart2_init (void) {
AjK 0:0a841b89d614 716
AjK 0:0a841b89d614 717 /* Switch on UART2. */
AjK 0:0a841b89d614 718 LPC_SC->PCONP |= UART2_ORMASK_PCONP;
AjK 0:0a841b89d614 719
AjK 0:0a841b89d614 720 /* Set PCLK == CCLK, 96Mhz */
AjK 0:0a841b89d614 721 LPC_SC->PCLKSEL1 |= UART2_ORMASK_PCLKSEL1;
AjK 0:0a841b89d614 722
AjK 0:0a841b89d614 723 /* Enable the tx/rx to pins and select pin mode. */
AjK 0:0a841b89d614 724 LPC_PINCON->PINSEL0 |= UART2_ORMASK_PINSEL0;
AjK 0:0a841b89d614 725 LPC_PINCON->PINMODE0 |= UART2_ORMASK_PINMODE0;
AjK 0:0a841b89d614 726
AjK 0:0a841b89d614 727 /* Set the divisor values for 9600 and then set 8,n,1 */
AjK 0:0a841b89d614 728 LPC_UART2->LCR = UART2_SET_LCR_DLAB;
AjK 0:0a841b89d614 729 LPC_UART2->DLL = UART2_SET_DLLSB;
AjK 0:0a841b89d614 730 LPC_UART2->DLM = UART2_SET_DLMSB;
AjK 0:0a841b89d614 731 LPC_UART2->LCR = UART2_SET_LCR;
AjK 0:0a841b89d614 732
AjK 0:0a841b89d614 733 /* Enable FIFOs and then clear them. */
AjK 0:0a841b89d614 734 LPC_UART2->FCR = UART2_SET_FCR;
AjK 0:0a841b89d614 735 LPC_UART2->FCR = UART2_SET_FCR_CLEAR;
AjK 0:0a841b89d614 736
AjK 0:0a841b89d614 737 /* Enable the RDA and THRE interrupts. */
AjK 0:0a841b89d614 738 LPC_UART2->IER = UART2_SET_IER;
AjK 0:0a841b89d614 739
AjK 0:0a841b89d614 740 /* Now it's time to do interrupts. */
AjK 0:0a841b89d614 741 NVIC_SetVector(UART2_IRQn, (uint32_t)UART2_IRQHandler);
AjK 0:0a841b89d614 742 NVIC_EnableIRQ(UART2_IRQn);
AjK 0:0a841b89d614 743 }
AjK 0:0a841b89d614 744
AjK 0:0a841b89d614 745 /** Uart2_flush_rxfifo
AjK 0:0a841b89d614 746 *
AjK 0:0a841b89d614 747 * Flush the input RX fifo to make sure it's empty.
AjK 0:0a841b89d614 748 */
AjK 0:0a841b89d614 749 void Uart2_flush_rxfifo(void) {
AjK 0:0a841b89d614 750 uint8_t c;
AjK 0:0a841b89d614 751 while (LPC_UART2->LSR & 0x1) {
AjK 0:0a841b89d614 752 c = LPC_UART2->RBR;
AjK 0:0a841b89d614 753 }
AjK 0:0a841b89d614 754 }
AjK 0:0a841b89d614 755
AjK 0:0a841b89d614 756 /** Uart2_putc
AjK 0:0a841b89d614 757 *
AjK 0:0a841b89d614 758 * Put the character given into the TxFIFO.
AjK 0:0a841b89d614 759 * By design there should always be room in
AjK 0:0a841b89d614 760 * the fifo, but check it to make sure.
AjK 0:0a841b89d614 761 *
AjK 0:0a841b89d614 762 * @param char The character to write.
AjK 0:0a841b89d614 763 * @return int zero on failure, non-zero otherwise.
AjK 0:0a841b89d614 764 */
AjK 0:0a841b89d614 765 int Uart2_putc(char c) {
AjK 0:0a841b89d614 766 /* Check the TxFIFO is not full. */
AjK 0:0a841b89d614 767 if ((LPC_UART2->FIFOLVL & UART_ISSET_FIFOLVL_TXFULL) != 0) return 0;
AjK 0:0a841b89d614 768 LPC_UART2->THR = (uint8_t)c;
AjK 0:0a841b89d614 769 return -1;
AjK 0:0a841b89d614 770 }
AjK 0:0a841b89d614 771
AjK 0:0a841b89d614 772 /** Uart2_puts
AjK 0:0a841b89d614 773 *
AjK 0:0a841b89d614 774 * Put the string given into the TX FIFO.
AjK 0:0a841b89d614 775 * By design there should always be room in
AjK 0:0a841b89d614 776 * the fifo, but check it to make sure.
AjK 0:0a841b89d614 777 *
AjK 0:0a841b89d614 778 * @param char *s Pointer The string to write.
AjK 0:0a841b89d614 779 * @return int zero on failure, non-zero (number of chars written) otherwise.
AjK 0:0a841b89d614 780 */
AjK 0:0a841b89d614 781 int Uart2_puts(char *s, int len) {
AjK 0:0a841b89d614 782 int i;
AjK 0:0a841b89d614 783
AjK 0:0a841b89d614 784 /* Check the FIFO is empty, as it should always be by design. */
AjK 0:0a841b89d614 785 if ((LPC_UART2->FIFOLVL & UART_ISSET_FIFOLVL_TXFULL) == 0) {
AjK 0:0a841b89d614 786 for (i = 0; i < len; i++) {
AjK 0:0a841b89d614 787 LPC_UART2->THR = (uint8_t)s[i];
AjK 0:0a841b89d614 788 debug.printf("%c", s[i]);
AjK 0:0a841b89d614 789 }
AjK 0:0a841b89d614 790 debug.printf("\r\n");
AjK 0:0a841b89d614 791 }
AjK 0:0a841b89d614 792 else {
AjK 0:0a841b89d614 793 return 0;
AjK 0:0a841b89d614 794 }
AjK 0:0a841b89d614 795 return i;
AjK 0:0a841b89d614 796 }
AjK 0:0a841b89d614 797
AjK 0:0a841b89d614 798 #endif