9 #include "../firmware.h"
11 #include "inc/hw_memmap.h"
12 #include "inc/hw_types.h"
14 #include "driverlib/gpio.h"
15 #include "driverlib/i2c.h"
16 #include "driverlib/pin_map.h"
17 #include "driverlib/sysctl.h"
23 #define GPS_SERIAL UART_GNSS
24 #define CAM_SERIAL UART_CAMERA
26 #define DEBUG_SERIAL GPS_SERIAL
28 #define sz(a) a,sizeof(a)
32 char LK_POWERUP[] = {0x0d, 0x0a, 0x00, 0x0d, 0x0a, 0x49, 0x6e, 0x69, 0x74, 0x20, 0x65, 0x6e, 0x64, 0x0d, 0x0a};
35 char LK_BAUDRATE_19200[] ={0x56, 0x00, 0x24, 0x03, 0x01, 0x56, 0xe4};
36 char LK_BAUDRATE_38400[] = {0x56, 0x00, 0x24, 0x03, 0x01, 0x2a, 0xf2};
37 char LK_BAUDRATE_RE[] = {0x76, 0x00, 0x24, 0x00, 0x00};
39 char LK_RESOLUTION_VGA[] = {0x56, 0x00, 0x54, 0x01, 0x00};
40 char LK_RESOLUTION_800[] = {0x56, 0x00, 0x54, 0x01, 0x1D};
41 char LK_RESOLUTION_1280[] = {0x56, 0x00, 0x54, 0x01, 0x1B};
42 char LK_RESOLUTION_1600[] = {0x56, 0x00, 0x54, 0x01, 0x21};
43 char LK_RESOLUTION_RE[] = {0x76, 0x00, 0x54, 0x00, 0x00};
45 char LK_COMPRESSION[] = {0x56, 0x00, 0x31, 0x05, 0x01, 0x01, 0x12, 0x04};
46 char LK_COMPRESSION_RE[] = {0x76, 0x00, 0x31, 0x00, 0x00};
48 char LK_RESET[] = {0x56, 0x00, 0x26, 0x00};
51 char LK_RESET_RE[] = {0x0d, 0x0a, 0x49, 0x6e, 0x69, 0x74, 0x20, 0x65, 0x6e, 0x64, 0x0d, 0x0a};
53 char LK_PICTURE[] = {0x56, 0x00, 0x36, 0x01, 0x00};
54 char LK_PICTURE_RE[] = {0x76, 0x00, 0x36, 0x00, 0x00};
55 char LK_JPEGSIZE[] = {0x56, 0x00, 0x34, 0x01, 0x00};
56 char LK_JPEGSIZE_RE[] = {0x76, 0x00, 0x34, 0x00, 0x04};
57 char LK_STOP[] = {0x56, 0x00, 0x36, 0x01, 0x03};
58 char LK_STOP_RE[] = {0x76, 0x00, 0x36, 0x00, 0x00};
60 char LK_READPICTURE[] = {0x56, 0x00, 0x32, 0x0C, 0x00, 0x0A};
61 char LK_PICTURE_TIME[] = {0x00, 0x0A};
62 char LK_READPICTURE_RE[] = {0x76, 0x00, 0x32, 0x00, 0x00};
63 char JPEG_START[] = {0xFF, 0xD8};
64 char JPEG_END[] = {0xFF, 0xD9};
70 #define STRING_BUFFER_LENGTH 20 // rough hack for testing
72 static char string_buffer[STRING_BUFFER_LENGTH];
73 static char string_buffer2[STRING_BUFFER_LENGTH];
75 static void UART_putnum(
unsigned int serialport,
signed long x)
77 itoa(abs(x),string_buffer,16);
78 unsigned int len=strlen(string_buffer);
79 unsigned int targetlen=6-len;
80 strcpy(string_buffer2,
"+00000");
81 strcpy(string_buffer2+targetlen,string_buffer);
82 if (x<0) string_buffer2[0]=
'-';
86 static void UART_putstr(
unsigned int serialport,
char *s1,
signed long x,
char *s2)
89 UART_putnum(serialport,x);
93 #define DISP3(x,y,z) UART_putstr(DEBUG_SERIAL,x,y,z); // x and z are strings, y is a number shown as signed base10 16bit
94 #define DISP2(y,z) UART_putstr(DEBUG_SERIAL,NULL,y,z);
95 #define DISP1(x) UART_puts(DEBUG_SERIAL,x);
101 unsigned int UART_getw(
long int serial)
107 unsigned int UART_getw4(
long int serial)
110 return (c1<<24)|(c2<<16)|(c3<<8)|(c4<<0);
114 void mem_packet_send (
char *start_addr,
unsigned int len)
124 for (
char *addr=start_addr;addr<start_addr+len;addr++)
126 unsigned int s_addr= (int) start_addr;
136 bool match_string(
char *data,
unsigned int len)
139 for (
int i=0;i<len;i++) {
char c=
UART_getc(CAM_SERIAL);
if (c!=data[i])
return false; }
143 unsigned int getcamword()
145 return UART_getw4(CAM_SERIAL);
150 void writeword4(
int serial,
unsigned int w)
159 void writecamword(
unsigned int w)
161 writeword4(CAM_SERIAL,w);
164 #define CAMWRITE(a) UART_putb(CAM_SERIAL,a,sizeof(a)); // send this message
166 #define CAMREAD(a) while (!match_string(a,sizeof(a))) {} // wait for this sequence (forever if necessary)
168 unsigned int get_picture_part(
unsigned int offset,
unsigned int len,
char *storage_addr)
170 if (((((
int)storage_addr)>>10)*1024)!=storage_addr) DISP3(
"Bad storage address for picture part",storage_addr,
"\n\r");
177 unsigned int pages_to_clear=(len+1023)>>10;
180 for (
int i=0;i<pages_to_clear;i++)
181 if (FlashErase(storage_addr+i*1024)==-1) DISP1(
"PROTECTED FLASH!");
185 char flash_word_size=4;
186 char flash_buffer[flash_word_size];
188 char endfoundcount=0;
200 DISP1(
"Sending message.\n")
202 CAMWRITE(LK_READPICTURE);
203 writecamword(offset);
205 CAMWRITE(LK_PICTURE_TIME);
206 CAMREAD(LK_READPICTURE_RE);
213 while ((endfoundcount<2) && (i<len))
219 if (c==JPEG_END[endfoundcount]) endfoundcount+=1;
else endfoundcount=0;
221 if (j==flash_word_size) {j=0;
222 if (FlashProgram(flash_buffer,storage_addr+i,flash_word_size)==-1)
224 DISP3(
"FLASH PROGRAMMING ERROR ",storage_addr+i,
"\r\n");
225 DISP3(
"tried to set to ",(
unsigned int) *flash_buffer,
"\r\n");
226 DISP3(
"actually set to ",(
unsigned int) storage_addr[i],
"\r\n");
241 unsigned int picturelength=i+j;
245 while (j<flash_word_size) {flash_buffer[j++]=0;}
246 FlashProgram(flash_buffer,storage_addr+i,flash_word_size);
253 DISP3(
"Downloaded ",picturelength,
" bytes of image data\n\r")
254 return picturelength;
257 unsigned int take_picture(
char *picture_storage)
259 if (((
unsigned int) picture_storage)&3!=0) {DISP1(
"address must be word aligned");
return 0;}
261 DISP3(
" Take Picture Storage Address: ",picture_storage,
"\n\r");
263 CAMWRITE(LK_RESET);CAMREAD(LK_RESET_RE);DISP1(
"Initialised OK\n\r");
267 CAMWRITE(LK_RESOLUTION_1600);CAMREAD(LK_RESOLUTION_RE);DISP1(
"Resolution OK\n\r");
268 CAMWRITE(LK_COMPRESSION);
UART_putc(CAM_SERIAL,0x10);CAMREAD(LK_COMPRESSION_RE);DISP1(
"Compression OK\n\r");
269 CAMWRITE(LK_PICTURE);CAMREAD(LK_PICTURE_RE);DISP1(
"Take Picture OK\n\r");
270 CAMWRITE(LK_JPEGSIZE);CAMREAD(LK_JPEGSIZE_RE);
unsigned int jpegsize=getcamword();
271 DISP3(
"Picture size is ",jpegsize,
" bytes of image data\n\r");
286 unsigned int picturelength=get_picture_part(0,jpegsize,picture_storage);
288 DISP1(
"Copy complete \r\n");
290 DISP3(
"Downloaded ",picturelength,
" bytes of image data\n\r")
291 return picturelength;
294 void send_packet(cmd,value)
296 bool packet_not_sent=
true;
301 writeword4(DEBUG_SERIAL,value);
310 void ack(
unsigned char cmd)
318 unsigned int receive_packet(
unsigned char cmd)
321 while (c!=cmd) {c=
UART_getc(DEBUG_SERIAL);DISP(c);}
322 unsigned int data=UART_getw4(DEBUG_SERIAL);
330 setupwatchdoginterrupt();
332 UART_init(DEBUG_SERIAL, 115200); DISP1(
"\n\n\r Satellite UART Camera test.\n\r")
337 char *picture_storage;
343 char *ad;
unsigned int len;
351 picture_storage=UART_getw4(DEBUG_SERIAL);
355 unsigned int picturelength=take_picture(picture_storage);
359 send_packet(
'$',picturelength);
367 ad=UART_getw4(DEBUG_SERIAL);
368 len=UART_getw(DEBUG_SERIAL);
371 mem_packet_send(ad,len);
void Delay_ms(uint32_t milliseconds)
void UART_init(uint8_t uart_num, uint32_t baudrate)
char UART_getc(uint8_t uart_num)
int main(void)
Runs all module tests.
void UART_puts(uint8_t uart_num, char *str)
void UART_putc(uint8_t uart_num, char c)