thomas haine
/
CIS001_v2_v2
cis001v2
Diff: main.cpp
- Revision:
- 0:e7352f4f3dcb
- Child:
- 1:5060c8ea0ccd
diff -r 000000000000 -r e7352f4f3dcb main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Thu Jun 18 12:36:05 2020 +0000 @@ -0,0 +1,769 @@ + +/* Includes ------------------------------------------------------------------*/ +//my include +#include "main.h" +#include "common_task.h" +#include "register_address.h" + + +//mbed include +#include "stm32h7xx_hal.h" +#include "mbed.h" +#include "EthernetInterface.h" +#include "TCPServer.h" +#include "TCPSocket.h" + + +/* Private variables ---------------------------------------------------------*/ + +//DCMI & DMA stuff +DCMI_HandleTypeDef hdcmi; +DMA_HandleTypeDef hdma_dcmi; + +// Virtual USB Serial port +Serial pc(USBTX, USBRX); + +// etherner stuff +EthernetInterface eth; + +TCPSocket socket; //data socket +TCPSocket cs; //com sockey + +char ServerIP[17]="10.40.63.229"; +int portnumber = 61477; +int portnumber_com = 61478; +char* commandbuffer; + +int MSG_LEN=64; + + +// SPI1 +SPI spi(PD_7, PB_4, PA_5); // mosi, miso, sclk +DigitalOut SSN(PA_15); + + +//LEDs +DigitalOut led1(LED1); //PB_0 +DigitalOut led2(LED2); //PB7 +DigitalOut led3(LED3); //PB14 + + +// GPIOS +DigitalOut RSTN(PA_0); +DigitalOut SLEEPN(PA_2); +DigitalOut CAPTURE(PA_9); +InterruptIn button_capture(PC_13); +InterruptIn irq_cis(PA_11); + + +// frame pointer and frame size +char* FRAME_BUFFER; + +const int frame_size_8b =640*480*1; //640*480 pixels de 1 byte +const int frame_size_QQVGA_8b =160*120*1; //160*120 pixels de 1 byte +const int frame_size_10b =640*480*2; //640*480 pixels de 2 byte +const int frame_size_QQVGA_10b =160*120*2; //160*120 pixels de 2 byte + +//keep track fo the number of line and frame read +int line_read=0; +int frame_read=0; +int imSize=frame_size_10b; + + +/* Private function prototypes -----------------------------------------------*/ +// functions for sending frame over ethernet +void print_start_frame(void); +void send_frame_ethernet(void); +int send_frame_ethernet_nohandshake(void); + + +// configuration functions +static void MX_GPIO_Init(void); +static void MX_DMA_Init(void); +static void MX_DCMI_Init_8b(void); +static void MX_DCMI_Init_10b(void); + +// capture functions +void start_capture_10b(); +void start_video(); +void suspend_capture(void); +void resume_capture(void); +void stop_capture(void); + + + + + +/************************************************************************************************ +* +* Ethernet function +* +************************************************************************************************/ + + +void print_start_frame(){ + int i,j,k=0; + int byte_sent=0; + + for (i=0; i<1 ; i++){ + for (k=0; k<20; k++){ + for (j=0; j<32; j++){ + //pc.printf("%3i ",(uint8_t) *(FRAME_BUFFER+i*640+k*32+j)); + sprintf(commandbuffer, "%3i ",(uint8_t) *(FRAME_BUFFER+i*640+k*32+j)); + byte_sent=cs.send(commandbuffer, strlen(commandbuffer)); + } + //pc.printf("\b\r\n"); + sprintf(commandbuffer, "\b\r\n"); + byte_sent=cs.send(commandbuffer, strlen(commandbuffer)); + + } + } +} + + + +void send_frame_ethernet(){ + + int status=0; + char rbuffer[13]="Hello server"; + char sbuffer[13] = "Hello server"; + int scount = 0; + int rcount = 0; + int i=0; + int byte_sent=0; + + sprintf(commandbuffer, "Sending frame via Ethernet \n"); + byte_sent=cs.send(commandbuffer, strlen(commandbuffer)); + + // Send a simple http request + sprintf(commandbuffer, "Sending hello \n"); + byte_sent=cs.send(commandbuffer, strlen(commandbuffer)); + + scount = socket.send(sbuffer, sizeof sbuffer); + if(scount>0) + //printf("sent %d [%.*s]\r\n", scount, strstr(sbuffer, "\r\n")-sbuffer, sbuffer); + + // Recieve a simple http response and print out the response line + // should receive hello socket + sprintf(commandbuffer, "Receiveing hello \n"); + byte_sent=cs.send(commandbuffer, strlen(commandbuffer)); + + rcount = socket.recv(&rbuffer, sizeof rbuffer); + if(rcount>0) + //printf("recv %d [%.*s]\r\n", rcount, strstr(rbuffer, "\r\n")-rbuffer, rbuffer); + + // Send a simple frame + sprintf(commandbuffer, "Sending frame\n"); + byte_sent=cs.send(commandbuffer, strlen(commandbuffer)); + + scount=0; + for (i=0;i<9600;i++){ + //FRAME_BUFFER[i*32]=127; + //FRAME_BUFFER[i*32+31]=255; + scount+= socket.send(FRAME_BUFFER+i*32, 32*sizeof(char)); + } + if(scount>0){ + sprintf(commandbuffer, "%i bytes sent\n", scount); + byte_sent=cs.send(commandbuffer, strlen(commandbuffer)); + } + + // Close the socket to return its memory and bring down the network interface + //socket.close(); + +} + + +int send_frame_ethernet_nohandshake(){ + + int scount=0; + int i=0; + int pixel_per_chunk=1024; + int byte_per_pixel=2; + int byte_sent=0; + + sprintf(commandbuffer, "%i bytes sen - chunk %d/%d - \n", scount,i,imSize/pixel_per_chunk); + byte_sent=cs.send(commandbuffer, strlen(commandbuffer)); + + // Send a simple 10b frame + scount=0; + for (i=0;i<imSize/pixel_per_chunk;i++){ + // send nb pixel *size (=2byte) at addresse frame_buffer + nb chunk sent * size (=2byte) + scount+= socket.send(FRAME_BUFFER+i*pixel_per_chunk*byte_per_pixel, pixel_per_chunk*byte_per_pixel*sizeof(char)); + + sprintf(commandbuffer, "%i bytes sent - chunk %d/%d - \n", scount,i,imSize/pixel_per_chunk); + byte_sent=cs.send(commandbuffer, strlen(commandbuffer)); + } + + sprintf(commandbuffer, "%i bytes sent\n", scount); + byte_sent=cs.send(commandbuffer, strlen(commandbuffer)); + + return scount; +} + + + +/************************************************************************************************ +* +* capture function +* +************************************************************************************************/ + + + + +//https://github.com/tomvdb/stm32f401-nucleo-basic-template/blob/master/Drivers/BSP/STM324x9I_EVAL/stm324x9i_eval_camera.c +void start_capture_10b(){ + + frame_read=0; + line_read=0; + + //memset(FRAME_BUFFER,0,(size_t) frame_size_10b); + //HAL_DCMI_Stop(&hdcmi); + //pc.printf("starting snapshot 10b\r\n"); + + // disable crop features + //HAL_DCMI_DisableCrop(&hdcmi); + + //enable DCMI + ///HAL_DCMI_Start_DMA(&hdcmi, DCMI_MODE_SNAPSHOT, (uint32_t) FRAME_BUFFER, Im_size); + HAL_DCMI_Start_DMA (&hdcmi, DCMI_MODE_SNAPSHOT,(uint32_t) FRAME_BUFFER, (uint32_t) imSize); + + //start capture + TASK_SPI_WRITE(capture_add,1); + wait_us(1000); + TASK_SPI_WRITE(capture_add,0); + +} + + + +void start_video(){ + + // pc.printf("starting continous video\r\n"); + + // disable crop features + HAL_DCMI_DisableCrop(&hdcmi); + + //enable DCMI + HAL_DCMI_Start_DMA (&hdcmi, DCMI_MODE_CONTINUOUS,(uint32_t) FRAME_BUFFER, (uint32_t) frame_size_8b); + + //start capture + TASK_SPI_WRITE(capture_add,1); + TASK_SPI_WRITE(capture_add,0); + +} + + + +void suspend_capture(void){ + // Suspend the Camera Capture // + HAL_DCMI_Suspend(&hdcmi); +} + + +void resume_capture(void) { + // Start the Camera Capture // + HAL_DCMI_Resume(&hdcmi); +} + + +void stop_capture(void) { + if(HAL_DCMI_Stop(&hdcmi) == HAL_OK){ + //pc.printf("Error stopping camera\n\r"); + } + else{ + //pc.printf("Camera stopped\n\r"); + } +} + + +// override interrupt DCMI +void HAL_DCMI_FrameEventCallback(DCMI_HandleTypeDef *hdcmi){ + send_frame_ethernet_nohandshake(); +} +void HAL_DCMI_VsyncEventCallback(DCMI_HandleTypeDef *hdcmi){ + frame_read++; +} +void HAL_DCMI_LineEventCallback (DCMI_HandleTypeDef *hdcmi){ + line_read++; +} + + + +/************************************************************************************************ +* +* main function +* +************************************************************************************************/ + +int main(void){ + + + uint8_t a,b,c,d=0; + uint8_t mclk; + uint8_t unlimited=0; + int res; + uint8_t SLEEPN_flag=0; + uint8_t PBP_flag=0; + uint8_t OEN_flag=0; + int status=0; + int byte_sent; + char host_cmd[64]; + int loopcount=0; + int button_pushed=0; + + //ethernet + char rbuffer[MSG_LEN]; + char * pch; + int rcount; + int data=0; + int add=0; + int readvalue=0; + int i=0; + int sizetosent=64; + + //************************* + /* Initialize all configured peripherals */ + //************************* + MX_GPIO_Init(); + MX_DMA_Init(); + MX_DCMI_Init_10b(); + + //************************* + // config serial print + //************************* + pc.baud(115200);//Set baudrate here. + pc.printf("*****************\r\n CIS001 \r\n*****************\r\n"); + + //************************* + // init pointer + //************************* + //initialize frame buffer for 10b photo (malloc size en byte) + FRAME_BUFFER=(char *)malloc(frame_size_10b*sizeof(char)); + //memset((void *)FRAME_BUFFER,0,(size_t) frame_size_10b/4); + + //initialize commandbuffer pointer + commandbuffer=(char *) malloc((MSG_LEN+1)*sizeof(char)); + + //************************* + // init spi + //************************* + // Setup the spi for 16 bit data (2x8), high steady state clock,second edge capture, with a 5MHz clock rate + /* mode | POL PHA + -----+-------- + 0 | 0 0 + 1 | 0 1 + 2 | 1 0 + 3 | 1 1*/ + spi.format(16,0); + spi.frequency(100000); + + //************************* + // reset leds + //************************* + led1=0; + led2=0; + led3=0; + + //************************* + //init CIS001 + //************************* + TASK_INIT(); + + //************************* + // Ethernet & socket stuff + //************************* + pc.printf("Connecting Ethernet cable....\r\n"); + eth.connect(); + SocketAddress sockaddr; + const char *mac = eth.get_mac_address(); + const char* ip = eth.get_ip_address(); + pc.printf("MAC Address is %s\r\n", mac ? mac : "No Mac"); + pc.printf("IP address is: %s\r\n", ip ? ip : "No IP"); + pc.printf("*****************\r\n"); + pc.printf("Nucleo CPU SystemCoreClock is %d MHz\r\n", SystemCoreClock/1000000); + pc.printf("*****************\r\n"); + + //************************* + //open a new socket for printing information & debug + //************************* + status=cs.open(ð); + if(status != 0){ + pc.printf("error open com interface\r\n"); + cs.close(); + return 1; + } + else{ + pc.printf("com interface open\r\n"); + } + + status=cs.connect(ServerIP, portnumber_com); + if(status !=0){ + pc.printf("error open com socket\r\n"); + cs.close(); + return 1; + }else{ + pc.printf("socket com open\r\n"); + } + + // send 10x test2 to the pc + sprintf(commandbuffer,"%64s","test2"); + pc.printf("[%s]_%d\r\n",commandbuffer,strlen(commandbuffer)); + byte_sent=0; + for(i=0; i<10;i++){ + byte_sent+=cs.send(commandbuffer, strlen(commandbuffer)); + } + + + + + //************************* + //open a new socket for data and command + //************************* + status=socket.open(ð); + if(status != 0){ + //myprint("*error open interface\r\n"); + pc.printf("failed to Open socket\n\r"); + socket.close(); + return 1; + }else{ + pc.printf("interface open\r\n"); + } + + status=socket.connect(ServerIP, portnumber); + if(status !=0){ + pc.printf("failed to connect to server\n\r"); + socket.close(); + return 1; + }else{ + pc.printf("socket open\r\n"); + } + + //socket.set_blocking(false); // non blocking + socket.set_timeout(1); // Timeout after (10)ms + + + + //************************* + // Infinite loop + //************************* + pc.printf("main loop\r\n"); + while (1){ + led2= !led2; + loopcount++; + + //************************* + // blink led every 4096*128 loop in the main loop + //************************* + if(loopcount==4096*128){ + led1=!led1; + loopcount=0; + //send tcp + /*sprintf(commandbuffer,"%64s","test3"); + byte_sent=cs.send( commandbuffer, strlen(commandbuffer)); + if (0 > byte_sent) { + pc.printf("Error sending data\n"); + return -1; + } else { + led3= ! led3; + }*/ + //myprint2(commandbuffer); + } + + + //************************* + // do soemthing when button is pressed + //************************* + if(button_capture==1 && button_pushed==0){ + pc.printf("bc\n"); + button_pushed=1; + + sprintf(commandbuffer,"%64s","bc"); + pc.printf("[%s]_%d\r\n",commandbuffer,strlen(commandbuffer)); + byte_sent=0; + for(i=0; i<2;i++){ + byte_sent+=cs.send(commandbuffer, strlen(commandbuffer)); + //myprint2(commandbuffer); + pc.printf("sent %d\r\n",i); + } + }else if(button_capture==0 && button_pushed==1){ + button_pushed=0; + } + + + + rcount = socket.recv(&rbuffer, sizeof rbuffer); + if(rcount>0){ + + pc.printf("recv %d [%.*s]\n", rcount, strstr(rbuffer, "\n") - rbuffer, rbuffer); + + pch = strtok (rbuffer," "); + + if (pch != NULL) { + pc.printf("%s\r\n", pch); + + if (strcmp (pch,"write")==0 || strcmp (pch,"WRITE")==0){ + + pch = strtok (NULL, " "); + data=atoi(pch); + pch = strtok (NULL, " "); + add=atoi(pch); + TASK_SPI_WRITE(add,data); + + sprintf(commandbuffer, "done writing %d at %d", data, add); + byte_sent=cs.send(commandbuffer,strlen(commandbuffer)); + //pc.printf("[%s]_-_%d\n\r",commandbuffer,byte_sent); + + }else if (strcmp (pch,"read")==0 || strcmp (pch,"READ")==0 ){ + + pch = strtok (NULL, " "); + add=atoi(pch); + readvalue = TASK_SPI_READ(add); + + sprintf(commandbuffer, "%d/%d", readvalue,add); + //sprintf(commandbuffer, "%64s", commandbuffer); + byte_sent=socket.send(commandbuffer,strlen(commandbuffer)); + pc.printf("[%s]_-_%d\n\r",commandbuffer,byte_sent); + + sprintf(commandbuffer, "done reading %d at %d", readvalue, add); + byte_sent=cs.send(commandbuffer,strlen(commandbuffer)); + pc.printf("[%s]_-_%d\n\r",commandbuffer,byte_sent); + + }else if (strcmp (pch,"capture")==0 ||strcmp (pch,"CAPTURE")==0){ + pch = strtok (NULL, " "); + pc.printf("[%s]\n\r",pch); + //imSize=atoi(pch); + //sprintf(commandbuffer, "try to capture image of %d bytes", imSize); + //byte_sent=cs.send(commandbuffer,strlen(commandbuffer)); + + //TODO + byte_sent=send_frame_ethernet_nohandshake(); + + sprintf(commandbuffer, "sent image of %d bytes/%d bytes", byte_sent,imSize); + byte_sent=cs.send(commandbuffer,strlen(commandbuffer)); + //pc.printf("[%s]_-_%d\n\r",commandbuffer,byte_sent); + + }else if (strcmp (pch,"reset")==0 ||strcmp (pch,"RESET")==0){ + TASK_RSTN(); + + sprintf(commandbuffer, "done reseting chip"); + byte_sent=cs.send(commandbuffer,strlen(commandbuffer)); + //pc.printf("[%s]_-_%d\n\r",commandbuffer,byte_sent); + + }else if (strcmp (pch,"exit")==0 ||strcmp (pch,"EXIT")==0){ + sprintf(commandbuffer, "close com and data sockets"); + byte_sent=cs.send(commandbuffer,strlen(commandbuffer)); + //pc.printf("[%s]_-_%d\n\r",commandbuffer,byte_sent); + status=socket.close(); + status=cs.close(); + + }else{ + sprintf(commandbuffer, "command not recoginzed"); + byte_sent=cs.send(commandbuffer,strlen(commandbuffer)); + //pc.printf("[%s]_-_%d\n\r",commandbuffer,byte_sent); + } + } else{ //tok is null + sprintf(commandbuffer, "empty tok"); + byte_sent=cs.send(commandbuffer,strlen(commandbuffer)); + //pc.printf("[%s]_-_%d\n\r",commandbuffer,byte_sent); + } + + + }else{ // non blocking timed out adn received <0 + + //sprintf(commandbuffer, "empty token\r\n"); + //myprint(commandbuffer); + if(pc.readable()){ + pc.scanf("%s", &host_cmd); + pch = strtok (rbuffer," "); + if (strcmp(host_cmd,"port")==0 || strcmp(host_cmd,"PORT")==0) { + sprintf(commandbuffer, "PORT\r\n"); + myprint(commandbuffer); + + } else if (strcmp (pch,"write")==0 || strcmp (pch,"WRITE")==0){ + + pch = strtok (NULL, " "); + data=atoi(pch); + pch = strtok (NULL, " "); + add=atoi(pch); + TASK_SPI_WRITE(add,data); + + sprintf(commandbuffer, "write %d at %d", data, add); + byte_sent=cs.send(commandbuffer,strlen(commandbuffer)); + pc.printf("[%s]%d\n\r",commandbuffer,byte_sent); + + } + } + + + } //end non blocking timed-out + + + + + } //end while(1) loop + + +} // end main + + + + + + + + + + +/************************************************************************************************ +* +* configuration function +* +************************************************************************************************/ + +/* DCMI init function */ +static void MX_DCMI_Init_8b(void) +{ + + hdcmi.Instance = DCMI; + hdcmi.Init.SynchroMode = DCMI_SYNCHRO_HARDWARE; + hdcmi.Init.PCKPolarity = DCMI_PCKPOLARITY_RISING; + //hdcmi.Init.PCKPolarity = DCMI_PCKPOLARITY_FALLING; + //the data is not valid in the parallel interface, when VSYNC or HSYNC is at that level (high or low) + hdcmi.Init.VSPolarity = DCMI_VSPOLARITY_LOW; + hdcmi.Init.HSPolarity = DCMI_HSPOLARITY_LOW; + + hdcmi.Init.CaptureRate = DCMI_CR_ALL_FRAME; + hdcmi.Init.ExtendedDataMode = DCMI_EXTEND_DATA_8B; + hdcmi.Init.JPEGMode = DCMI_JPEG_DISABLE; + hdcmi.Init.ByteSelectMode = DCMI_BSM_ALL; + hdcmi.Init.ByteSelectStart = DCMI_OEBS_ODD; + hdcmi.Init.LineSelectMode = DCMI_LSM_ALL; + hdcmi.Init.LineSelectStart = DCMI_OELS_ODD; + + if (HAL_DCMI_Init(&hdcmi) != HAL_OK) + { + Error_Handler(); + } + + + //check status + /*HAL_DCMI_StateTypeDef status; + status=HAL_DCMI_GetState(&hdcmi); + if(status != HAL_OK){ + pc.printf("DCMI_init: %i\n\r",status); + }*/ + +} + + +/** + * @brief DCMI Initialization Function + * @param None + * @retval None + */ +static void MX_DCMI_Init_10b(void) +{ + + hdcmi.Instance = DCMI; + hdcmi.Init.SynchroMode = DCMI_SYNCHRO_HARDWARE; + hdcmi.Init.PCKPolarity = DCMI_PCKPOLARITY_FALLING; + hdcmi.Init.VSPolarity = DCMI_VSPOLARITY_LOW; + hdcmi.Init.HSPolarity = DCMI_HSPOLARITY_LOW; + hdcmi.Init.CaptureRate = DCMI_CR_ALL_FRAME; + hdcmi.Init.ExtendedDataMode = DCMI_EXTEND_DATA_10B; + hdcmi.Init.JPEGMode = DCMI_JPEG_DISABLE; + hdcmi.Init.ByteSelectMode = DCMI_BSM_ALL; + hdcmi.Init.ByteSelectStart = DCMI_OEBS_ODD; + hdcmi.Init.LineSelectMode = DCMI_LSM_ALL; + hdcmi.Init.LineSelectStart = DCMI_OELS_ODD; + + if (HAL_DCMI_Init(&hdcmi) != HAL_OK) + { + Error_Handler(); + } + + + /* USER CODE BEGIN DCMI_Init 2 */ + //check status + /*HAL_DCMI_StateTypeDef status; + status=HAL_DCMI_GetState(&hdcmi); + if(status != HAL_OK){ + pc.printf("DCMI_init: %i\n\r",status); + }*/ + /* USER CODE END DCMI_Init 2 */ + +} + + + + +/** + * Enable DMA controller clock + */ +static void MX_DMA_Init(void) +{ + + /* DMA controller clock enable */ + __HAL_RCC_DMA1_CLK_ENABLE(); + + /* DMA interrupt init */ + /* DMA1_Stream0_IRQn interrupt configuration */ + HAL_NVIC_SetPriority(DMA1_Stream0_IRQn, 0, 0); + HAL_NVIC_EnableIRQ(DMA1_Stream0_IRQn); + +} + +/** + * @brief GPIO Initialization Function + * @param None + * @retval None + */ +static void MX_GPIO_Init(void) +{ + + GPIO_InitTypeDef GPIO_InitStruct = {0}; + + /* GPIO Ports Clock Enable */ + __HAL_RCC_GPIOE_CLK_ENABLE(); + __HAL_RCC_GPIOC_CLK_ENABLE(); + __HAL_RCC_GPIOF_CLK_ENABLE(); + __HAL_RCC_GPIOH_CLK_ENABLE(); + __HAL_RCC_GPIOA_CLK_ENABLE(); + __HAL_RCC_GPIOB_CLK_ENABLE(); + __HAL_RCC_GPIOD_CLK_ENABLE(); + __HAL_RCC_GPIOG_CLK_ENABLE(); + +} + + + + +/** + * @brief This function is executed in case of error occurrence. + * @retval None + */ +void Error_Handler(void) +{ + /* USER CODE BEGIN Error_Handler_Debug */ + /* User can add his own implementation to report the HAL error return state */ + + /* USER CODE END Error_Handler_Debug */ +} + +#ifdef USE_FULL_ASSERT +/** + * @brief Reports the name of the source file and the source line number + * where the assert_param error has occurred. + * @param file: pointer to the source file name + * @param line: assert_param error line source number + * @retval None + */ +void assert_failed(uint8_t *file, uint32_t line) +{ + /* USER CODE BEGIN 6 */ + /* User can add his own implementation to report the file name and line number, + tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ + /* USER CODE END 6 */ +} +#endif /* USE_FULL_ASSERT */ \ No newline at end of file