Daiki Kato / Mbed OS GR-PEACH_NTSC_in_2ch Featured

Dependencies:   GR-PEACH_video LCD_shield_config

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "DisplayBace.h"
00003 #include "rtos.h"
00004 
00005 #define VIDEO_YCBCR422         (0)
00006 #define VIDEO_RGB888           (1)
00007 #define VIDEO_RGB565           (2)
00008 
00009 /**** User Selection *********/
00010 /** Camera setting **/
00011 #define VIDEO_INPUT_FORMAT     (VIDEO_YCBCR422)    /* Select  VIDEO_YCBCR422 or VIDEO_RGB888 or VIDEO_RGB565        */
00012 #define VIDEO_PAL              (0)                 /* Select  0(NTSC) or 1(PAL) If selecting VIDEO_CVBS, this parameter is not referenced.) */
00013 /** LCD setting **/
00014 #define LCD_TYPE               (0)                 /* Select  0(4.3inch) or 1(7.1inch) */
00015 /*****************************/
00016 
00017 /** LCD shield config **/
00018 #if (LCD_TYPE == 0)
00019   #include "LCD_shield_config_4_3inch.h"
00020 #else
00021   #include "LCD_shield_config_7_1inch.h"
00022 #endif
00023 
00024 /** Video and Grapics (GRAPHICS_LAYER_0) parameter **/
00025 /* NTSC or PAL */
00026 #if VIDEO_PAL == 0
00027   #define COL_SYS              (DisplayBase::COL_SYS_NTSC_358)
00028 #else
00029   #define COL_SYS              (DisplayBase::COL_SYS_PAL_443)
00030 #endif
00031 
00032 /* Video input and LCD layer 0 output */
00033 #if VIDEO_INPUT_FORMAT == VIDEO_YCBCR422
00034   #define VIDEO_FORMAT         (DisplayBase::VIDEO_FORMAT_YCBCR422)
00035   #define GRAPHICS_FORMAT      (DisplayBase::GRAPHICS_FORMAT_YCBCR422)
00036   #define WR_RD_WRSWA          (DisplayBase::WR_RD_WRSWA_NON)
00037 #elif VIDEO_INPUT_FORMAT == VIDEO_RGB565
00038   #define VIDEO_FORMAT         (DisplayBase::VIDEO_FORMAT_RGB565)
00039   #define GRAPHICS_FORMAT      (DisplayBase::GRAPHICS_FORMAT_RGB565)
00040   #define WR_RD_WRSWA          (DisplayBase::WR_RD_WRSWA_32_16BIT)
00041 #else
00042   #define VIDEO_FORMAT         (DisplayBase::VIDEO_FORMAT_RGB888)
00043   #define GRAPHICS_FORMAT      (DisplayBase::GRAPHICS_FORMAT_RGB888)
00044   #define WR_RD_WRSWA          (DisplayBase::WR_RD_WRSWA_32BIT)
00045 #endif
00046 
00047 /*! Frame buffer stride: Frame buffer stride should be set to a multiple of 32 or 128
00048     in accordance with the frame buffer burst transfer mode. */
00049 /* FRAME BUFFER Parameter GRAPHICS_LAYER_0 */
00050 #if ( VIDEO_INPUT_FORMAT == VIDEO_YCBCR422 || VIDEO_INPUT_FORMAT == VIDEO_RGB565 )
00051   #define FRAME_BUFFER_BYTE_PER_PIXEL (2u)
00052 #else
00053   #define FRAME_BUFFER_BYTE_PER_PIXEL (4u)
00054 #endif
00055 #define FRAME_BUFFER_STRIDE           (((LCD_PIXEL_WIDTH * FRAME_BUFFER_BYTE_PER_PIXEL) + 31u) & ~31u)
00056 
00057 static DisplayBase Display;
00058 static DigitalOut  lcd_pwon(P7_15);
00059 static DigitalOut  lcd_blon(P8_1);
00060 static PwmOut      lcd_cntrst(P8_15);
00061 static DigitalOut  led_blue(LED_BLUE);
00062 
00063 #if defined(__ICCARM__)
00064 /* 32 bytes aligned */
00065 #pragma data_alignment=32
00066 static uint8_t user_frame_buffer0[FRAME_BUFFER_STRIDE * LCD_PIXEL_HEIGHT];
00067 #pragma data_alignment=4
00068 #else
00069 /* 32 bytes aligned */
00070 static uint8_t user_frame_buffer0[FRAME_BUFFER_STRIDE * LCD_PIXEL_HEIGHT]__attribute((aligned(32)));
00071 #endif
00072 static bool graphics_init_end = false;
00073 
00074 /****** LCD ******/
00075 static void Init_LCD_Display(void) {
00076     DisplayBase::graphics_error_t error;
00077     DisplayBase::lcd_config_t lcd_config;
00078     PinName lvds_pin[8] = {
00079         /* data pin */
00080         P5_7, P5_6, P5_5, P5_4, P5_3, P5_2, P5_1, P5_0
00081     };
00082 
00083     lcd_pwon = 0;
00084     lcd_blon = 0;
00085     Thread::wait(100);
00086     lcd_pwon = 1;
00087     lcd_blon = 1;
00088 
00089     Display.Graphics_Lvds_Port_Init(lvds_pin, 8);
00090 
00091     /* Graphics initialization process */
00092     lcd_config = LcdCfgTbl_LCD_shield;
00093     error = Display.Graphics_init(&lcd_config);
00094     if (error != DisplayBase::GRAPHICS_OK) {
00095         printf("Line %d, error %d\n", __LINE__, error);
00096         mbed_die();
00097     }
00098     graphics_init_end = true;
00099 }
00100 
00101 static void Start_LCD_Display(uint8_t * p_buf) {
00102     DisplayBase::rect_t rect;
00103 
00104     rect.vs = 0;
00105     rect.vw = LCD_PIXEL_HEIGHT;
00106     rect.hs = 0;
00107     rect.hw = LCD_PIXEL_WIDTH;
00108     Display.Graphics_Read_Setting(
00109         DisplayBase::GRAPHICS_LAYER_0,
00110         (void *)p_buf,
00111         FRAME_BUFFER_STRIDE,
00112         GRAPHICS_FORMAT,
00113         WR_RD_WRSWA,
00114         &rect
00115     );
00116     Display.Graphics_Start(DisplayBase::GRAPHICS_LAYER_0);
00117 }
00118 
00119 /****** Video ******/
00120 static void Init_Video(void) {
00121     DisplayBase::graphics_error_t error;
00122 
00123     /* Graphics initialization process */
00124     if (graphics_init_end == false) {
00125         /* When not initializing LCD, this processing is needed. */
00126         error = Display.Graphics_init(NULL);
00127         if (error != DisplayBase::GRAPHICS_OK) {
00128             printf("Line %d, error %d\n", __LINE__, error);
00129             mbed_die();
00130         }
00131         graphics_init_end = true;
00132     }
00133 
00134     error = Display.Graphics_Video_init( DisplayBase::INPUT_SEL_VDEC, NULL);
00135     if( error != DisplayBase::GRAPHICS_OK ) {
00136         printf("Line %d, error %d\n", __LINE__, error);
00137         mbed_die();
00138     }
00139 }
00140 
00141 static void Start_Video(DisplayBase::video_input_channel_t ch, uint8_t * p_frame_buffer,
00142  uint16_t pos_x, uint16_t pos_y, uint16_t width, uint16_t height) {
00143     DisplayBase::graphics_error_t error;
00144     uint8_t * p_buf;
00145 
00146     p_buf = p_frame_buffer + (FRAME_BUFFER_BYTE_PER_PIXEL * pos_x) + (FRAME_BUFFER_STRIDE * pos_y);
00147 
00148     /* Video capture setting (progressive form fixed) */
00149     error = Display.Video_Write_Setting(
00150                 ch,
00151                 COL_SYS,
00152                 p_buf,
00153                 FRAME_BUFFER_STRIDE,
00154                 VIDEO_FORMAT,
00155                 WR_RD_WRSWA,
00156                 (height & ~7u),      /* A multiple of 8 */
00157                 (width  & ~15u)      /* A multiple of 16 */
00158             );
00159     if (error != DisplayBase::GRAPHICS_OK) {
00160         printf("Line %d, error %d\n", __LINE__, error);
00161         mbed_die();
00162     }
00163 
00164     /* Video write process start */
00165     error = Display.Video_Start(ch);
00166     if (error != DisplayBase::GRAPHICS_OK) {
00167         printf("Line %d, error %d\n", __LINE__, error);
00168         mbed_die();
00169     }
00170 
00171     /* Video write process stop */
00172     error = Display.Video_Stop(ch);
00173     if (error != DisplayBase::GRAPHICS_OK) {
00174         printf("Line %d, error %d\n", __LINE__, error);
00175         mbed_die();
00176     }
00177 
00178     /* Video write process start */
00179     error = Display.Video_Start(ch);
00180     if (error != DisplayBase::GRAPHICS_OK) {
00181         printf("Line %d, error %d\n", __LINE__, error);
00182         mbed_die();
00183     }
00184 }
00185 
00186 /****** Cache control ******/
00187 static void dcache_clean(void * p_buf, uint32_t size){
00188     uint32_t start_addr = (uint32_t)p_buf & 0xFFFFFFE0;
00189     uint32_t end_addr   = (uint32_t)p_buf + size;
00190     uint32_t addr;
00191 
00192     /* Data cache clean */
00193     for (addr = start_addr; addr < end_addr; addr += 0x20) {
00194         __v7_clean_dcache_mva((void *)addr);
00195     }
00196 }
00197 
00198 /****** main ******/
00199 int main(void) {
00200     /* Initialization of LCD */
00201     Init_LCD_Display();    /* When using LCD, please call before than Init_Video(). */
00202 
00203     /* Initialization of Video */
00204     Init_Video();
00205 
00206     /* Initialization memory */
00207 #if VIDEO_INPUT_FORMAT == VIDEO_YCBCR422
00208     for (int i = 0; i < sizeof(user_frame_buffer0); i += 2) {
00209         user_frame_buffer0[i + 0] = 0x10;
00210         user_frame_buffer0[i + 1] = 0x80;
00211     }
00212 #else
00213     memset(user_frame_buffer0, 0, sizeof(user_frame_buffer0));
00214 #endif
00215     dcache_clean(user_frame_buffer0, sizeof(user_frame_buffer0));
00216 
00217     /* Start of Video ch0 */
00218     Start_Video(
00219         DisplayBase::VIDEO_INPUT_CHANNEL_0,   /* Video input channe */
00220         user_frame_buffer0,                   /* Output buffer */
00221         0,                                    /* The x coordinate of the upper-left corner */
00222         0,                                    /* The y coordinate of the upper-left corner */
00223         (LCD_PIXEL_WIDTH / 2),                /* width  (A multiple of 16) */
00224         LCD_PIXEL_HEIGHT                      /* height (A multiple of 8) */
00225     );
00226 
00227     /* Start of Video ch1 */
00228     Start_Video(
00229         DisplayBase::VIDEO_INPUT_CHANNEL_1,   /* Video input channe */
00230         user_frame_buffer0,                   /* Output buffer */
00231         (LCD_PIXEL_WIDTH / 2),                /* The x coordinate of the upper-left corner */
00232         0,                                    /* The y coordinate of the upper-left corner */
00233         (LCD_PIXEL_WIDTH / 2),                /* width  (A multiple of 16) */
00234         LCD_PIXEL_HEIGHT                      /* height (A multiple of 8) */
00235     );
00236 
00237     /* Start of LCD */
00238     Start_LCD_Display(&user_frame_buffer0[0]);
00239 
00240     /* Backlight on */
00241     Thread::wait(200);
00242     lcd_cntrst.write(1.0);
00243 
00244     while (1) {
00245         led_blue = !led_blue;
00246         Thread::wait(1000);
00247     }
00248 }