// send 32bit unsigned integer as SUMP metadata void sump_sendmeta_uint32(char type, unsigned int i) { uart_putch(type); uart_putch((i >> 24) & 0xff); uart_putch((i >> 16) & 0xff); uart_putch((i >> 8) & 0xff); uart_putch(i & 0xff); }
void print_string(int a_ch, const char* str) { while (*str) { #ifdef CONFIG_UART_TX_INTERRUPT while (1) { if (uart_putch(a_ch,*str)) break; } #else uart_putch(a_ch,*str); #endif str++; } }
// This method interacts with socket zero only, check socket zero registers in the data sheet to know how to use them // to accomplish the required tasks. uint8_t S0_initialize_socket(uint8_t eth_protocol,uint16_t tcp_port) { // this method should close socket zero if it is already closed on client side. wiznet_write(S0_MR,eth_protocol);// then it should set socket zero mode according to the passed in eth_protocol variable // then it should define the port number associated with this socket uint8_t* ptr_tcp_port = (uint8_t*)&tcp_port; wiznet_write(S0_PORT,*ptr_tcp_port); wiznet_write(S0_PORT + 1,*(ptr_tcp_port + 1)); wiznet_write(S0_CR,CR_OPEN);// then it should open the socket and wait till it is opened //while(get_socket0_status() != SOCK_INIT); // then it should make sure the socket is initialized correctly and return TRUE in that case // in case of failure return FALSE and close the socket just in case! if(get_socket0_status() != SOCK_INIT) { wiznet_write(S0_CR,CR_CLOSE); return FALSE; } printf("INIT DONE 1\n"); printf("%x\n",get_socket0_status()); uart_putch('\0',&uart_str); return TRUE; }
int uart_putch(char ch,FILE *stream) { if (ch == '\n') uart_putch('\r', stream); while (!(UCSR0A & (1<<UDRE0))); UDR0=ch; return 0; }
int uart_getch(FILE *stream) { unsigned char ch; while (!(UCSR0A & (1<<RXC0))); ch=UDR0; /* Echo the Output Back to terminal */ uart_putch(ch,stream); return ch; }
void SysTickHandler(void){ static long i = 0; buffer[i] = GPIO_PORTB_DATA_R; i++; if(i > BUFFERSIZE){ ROM_SysTickDisable(); ROM_SysTickIntDisable(); i = 0; // send it over uart for (int j = 0; j < BUFFERSIZE; j++){ uart_putch(buffer[j]); } } }
void uart_puts(char* str) { while (*str != '\0') uart_putch(*str++); }
/** Write a character to the UART console. * @param ch Character to write. */ static void uart_console_putch(char ch) { uart_putch(DEBUG_UART, ch); }
int main(void) { ansi_init(); uart_init();// Initialize UART Peripheral IntializeTimer(); // Clear the Screen of the hyper terminal <send clear code> spi_init();// Initialize SPI w5100_init();// Initial the W5100 Ethernet if(S0_initialize_socket(MR_TCP,30000)) { printf("INIT DONE 2\n"); //S0_connect(dest_ip,dest_port); } printf("%x\n",get_socket0_status()); uart_putch('\0',&uart_str); uart_flush(); host= http_extract_host(url); relativeAddress = http_extract_relativeAddress(url,strlen(host)); request= http_create_request(method,relativeAddress,httpV,host); ;//="GET / HTTP/1.1\r\nHost: www.google.com\r\nConnection: keep-alive\r\nUser-Agent: Mozilla/5.0 (windows NT 6.1; wow64) Applewebkit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 safari/537.11\r\nAccept: text/html\r\nAccept-Encoding: gzip, deflate\r\n"; //printf("%s",request); S0_connect(dest_ip,dest_port); if(get_socket0_status() == SOCK_ESTABLISHED) { printf("Connected\n"); uart_putch('\0',&uart_str); } // Loop forever int loop = 1, counter = 0; //sei(); while(loop) { // keep reading the socket zero status // and handle at least socket closed and socket established uint8_t sockstat=get_socket0_status(); switch(sockstat/*Handle all possible socket status here*/) { case SOCK_CLOSED: //printf("ERROR1\n"); //uart_putch('\0',&uart_str); //S0_connect(dest_ip,dest_port); printf("%x\n",get_socket0_status()); uart_putch('\0',&uart_str); loop = 0; break; case SOCK_ESTABLISHED: printf("SOCK_ESTABLISHED\n"); uart_putch('\0',&uart_str); // Get the client request size // And read the client Request in temp buffer // make sure the temp buffer is sending GET request S0_send((uint8_t*)request,strlen(request)); printf("Request Sent\n"); uart_putch('\0',&uart_str); uart_flush(); _delay_ms(2000); if(S0_RX_getReceivedSize() != 0) { counter++; S0_recv(buf,S0_RX_getReceivedSize()); printf("RECV Response\n"); uart_putch('\0',&uart_str); printf("%s",(char*)buf); uart_putch('\0',&uart_str); //loop =0; } // create a buffer that will hold the response and send it // if the GET request has an option to turn on/off a LED Carry that now. // finally disconnect the socket with the client. break; case SOCK_FIN_WAIT: case SOCK_CLOSING: case SOCK_TIME_WAIT: case SOCK_CLOSE_WAIT: case SOCK_LAST_ACK: loop = 0; printf("Recevied %d\n",counter); uart_putch('\0',&uart_str); close(0); // force the socket to be closed break; //default: //printf("nothing\n"); } } return 0; }
// send 8bit unsigned integer as SUMP metadata void sump_sendmeta_uint8(char type, unsigned char i) { uart_putch(type); uart_putch(i); }
// send nullterminated string over UART void uart_puts(char * s) { while (*s != 0) { uart_putch(*s); s++; } }
// main routine int main(void) { // enable lazy stacking ROM_FPUEnable(); ROM_FPULazyStackingEnable(); // run from crystal, 80 MHz ROM_SysCtlClockSet(SYSCTL_SYSDIV_2_5 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ); // enable peripherals ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0); ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA); // set UART pins GPIOPinConfigure(GPIO_PA0_U0RX); GPIOPinConfigure(GPIO_PA1_U0TX); ROM_GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1); // init PORTB ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB); GPIO_PORTB_DIR_R = 0x00; GPIO_PORTB_DEN_R = 0xff; // configure uart ROM_UARTConfigSetExpClk(UART0_BASE, ROM_SysCtlClockGet(), 115200, (UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE | UART_CONFIG_PAR_NONE)); // Enable interrupts to the processor. - not sure if this is needed... ROM_IntMasterEnable(); // main loop while (1) { if (ROM_UARTCharsAvail(UART0_BASE)) { unsigned char cmd; cmd = ROM_UARTCharGetNonBlocking(UART0_BASE); switch (cmd) { case SUMP_RESET: break; case SUMP_ARM: doticksampling(); break; case SUMP_QUERY: uart_puts("1ALS"); break; case SUMP_GET_METADATA: // device name uart_putch(0x01); uart_puts(VERSION); uart_putch(0x00); // amount of sample memory available (bytes) sump_sendmeta_uint32(0x21, BUFFERSIZE); // maximum sample rate (hz) sump_sendmeta_uint32(0x23, MAX_SAMPLERATE); // number of usable probes (short) sump_sendmeta_uint8(0x40, 0x08); // protocol version (short) sump_sendmeta_uint8(0x41, 0x02); // end of meta data uart_putch(0x00); break; case SUMP_SET_DIVIDER: { /* Set Divider (80h) When x is written, the sampling frequency is set to f = clock / (x + 1) */ unsigned long clock = 100000000; //no idea where this comes from... //these three bytes are our clock divider - lsb first unsigned char b0 = ROM_UARTCharGet(UART0_BASE); unsigned char b1 = ROM_UARTCharGet(UART0_BASE); unsigned char b2 = ROM_UARTCharGet(UART0_BASE); ROM_UARTCharGet(UART0_BASE); //eat last byte unsigned long rate = b0 | (b1 << 8) | (b2 << 16); rate = clock / (rate+1); ROM_SysTickPeriodSet(ROM_SysCtlClockGet() / rate); break; } // long commands.. consume bytes from uart case 0xC0: case 0xC4: case 0xC8: case 0xCC: case 0xC1: case 0xC5: case 0xC9: case 0xCD: case 0xC2: case 0xC6: case 0xCA: case 0xCE: case 0x81: case 0x82: ROM_UARTCharGet(UART0_BASE); ROM_UARTCharGet(UART0_BASE); ROM_UARTCharGet(UART0_BASE); ROM_UARTCharGet(UART0_BASE); break; } } } }
uint16_t S0_send(const uint8_t *buf,uint16_t buflen) { // given the required data and its length // first make sure the transmission buffer on the ship has free space enough to hold your buffer if(S0_TX_getFreeSize() < buflen) { _delay_ms(1000);// otherwise wait 1 ms and try to check the buffer length once again if(S0_TX_getFreeSize() < buflen)// maximum for 1 second then return zero (FALSE) to indicate an error (do not forget to disconnect the socket) { disconnect(0); return FALSE; } } // if there are enough size in the transmission buffer on wiznet ship start sending the data. // to do that you have to read the start address of the free space on the wiznet ship first. // make a loop to calculate the real address relative to the start address you just got from the wiznet ship // refer to the data sheet page 33 example and study it well. uint16_t BASE = gS0_TX_BASE,TX_WR=S0_TX_getWriteAddress(); uint16_t offset= TX_WR & gSn_TX_MASK(0); uint16_t start_physical_address = BASE + offset; uint16_t socket_MaxSize = gSn_TX_MASK(0) + 1; /* if overflow socket TX memory*/ if(offset + buflen > socket_MaxSize) { printf("overflow\n"); uart_putch('\0',&uart_str); uint16_t upper_size = socket_MaxSize - offset; uint16_t countByte; for(countByte=0;countByte<upper_size;++countByte) { wiznet_write(start_physical_address + countByte,*(buf + countByte)); } uint8_t *leftBuffer = buf + upper_size; uint16_t left_size = buflen - upper_size; countByte=0; for(;countByte<left_size;++countByte) { wiznet_write(BASE + countByte,*(leftBuffer + countByte)); } } else { uint16_t countByte; for(countByte=0;countByte<buflen;++countByte) { wiznet_write(start_physical_address + countByte,*(buf + countByte)); } } TX_WR += buflen; uint8_t* ptr_TX_WR = (uint8_t*)&TX_WR; wiznet_write(S0_TX_WR,*ptr_TX_WR); wiznet_write(S0_TX_WR + 1,*(ptr_TX_WR + 1)); wiznet_write(S0_CR,CR_SEND); _delay_us(5); while(wiznet_read(S0_CR)); printf("Done Sending Process\n"); uart_putch('\0',&uart_str); // finally write back the new start address for the free space on the ship. // and send command SEND to the ship so it starts sending the buffer on the wires // and wait till it is already sent. }
void clear_screen() { uart_putch(&uart0, 12); }