Preliminary main mbed library for nexpaq development
libraries/tests/mbed/spifi1/main.cpp@0:6c56fb4bc5f0, 2016-11-04 (annotated)
- Committer:
- nexpaq
- Date:
- Fri Nov 04 20:27:58 2016 +0000
- Revision:
- 0:6c56fb4bc5f0
Moving to library for sharing updates
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nexpaq | 0:6c56fb4bc5f0 | 1 | #include "test_env.h" |
nexpaq | 0:6c56fb4bc5f0 | 2 | #include "mbed.h" |
nexpaq | 0:6c56fb4bc5f0 | 3 | #include "spifi_rom_api.h" |
nexpaq | 0:6c56fb4bc5f0 | 4 | |
nexpaq | 0:6c56fb4bc5f0 | 5 | __attribute__((section("SPIFI_MEM"))) const unsigned char cube_image[] = { |
nexpaq | 0:6c56fb4bc5f0 | 6 | 137,80,78,71,13,10,26,10,0,0,0,13,73,72,68,82, |
nexpaq | 0:6c56fb4bc5f0 | 7 | 0,0,0,150,0,0,0,200,8,2,0,0,0,133,231,143, |
nexpaq | 0:6c56fb4bc5f0 | 8 | 50,0,0,0,1,115,82,71,66,0,174,206,28,233,0,0, |
nexpaq | 0:6c56fb4bc5f0 | 9 | 0,4,103,65,77,65,0,0,177,143,11,252,97,5,0,0, |
nexpaq | 0:6c56fb4bc5f0 | 10 | 0,9,112,72,89,115,0,0,14,195,0,0,14,195,1,199, |
nexpaq | 0:6c56fb4bc5f0 | 11 | 111,168,100,0,0,156,193,73,68,65,84,120,94,236,253,103, |
nexpaq | 0:6c56fb4bc5f0 | 12 | 124,27,233,125,54,140,14,48,24,0,51,24,244,94,9,246, |
nexpaq | 0:6c56fb4bc5f0 | 13 | 0,0,0,0,73,69,78,68,174,66,96,130}; |
nexpaq | 0:6c56fb4bc5f0 | 14 | |
nexpaq | 0:6c56fb4bc5f0 | 15 | int cube_image_sz = sizeof(cube_image); |
nexpaq | 0:6c56fb4bc5f0 | 16 | |
nexpaq | 0:6c56fb4bc5f0 | 17 | const unsigned char cube_image_ref[] = { |
nexpaq | 0:6c56fb4bc5f0 | 18 | 137,80,78,71,13,10,26,10,0,0,0,13,73,72,68,82, |
nexpaq | 0:6c56fb4bc5f0 | 19 | 0,0,0,150,0,0,0,200,8,2,0,0,0,133,231,143, |
nexpaq | 0:6c56fb4bc5f0 | 20 | 50,0,0,0,1,115,82,71,66,0,174,206,28,233,0,0, |
nexpaq | 0:6c56fb4bc5f0 | 21 | 0,4,103,65,77,65,0,0,177,143,11,252,97,5,0,0, |
nexpaq | 0:6c56fb4bc5f0 | 22 | 0,9,112,72,89,115,0,0,14,195,0,0,14,195,1,199, |
nexpaq | 0:6c56fb4bc5f0 | 23 | 111,168,100,0,0,156,193,73,68,65,84,120,94,236,253,103, |
nexpaq | 0:6c56fb4bc5f0 | 24 | 124,27,233,125,54,140,14,48,24,0,51,24,244,94,9,246, |
nexpaq | 0:6c56fb4bc5f0 | 25 | 0,0,0,0,73,69,78,68,174,66,96,130}; |
nexpaq | 0:6c56fb4bc5f0 | 26 | |
nexpaq | 0:6c56fb4bc5f0 | 27 | int cube_image_ref_sz = sizeof(cube_image_ref); |
nexpaq | 0:6c56fb4bc5f0 | 28 | |
nexpaq | 0:6c56fb4bc5f0 | 29 | /* |
nexpaq | 0:6c56fb4bc5f0 | 30 | * The SPIFI_ROM_PTR (0x1FFF1FF8) points to an area where the pointers to |
nexpaq | 0:6c56fb4bc5f0 | 31 | * different drivers in ROM are stored. |
nexpaq | 0:6c56fb4bc5f0 | 32 | */ |
nexpaq | 0:6c56fb4bc5f0 | 33 | typedef struct { |
nexpaq | 0:6c56fb4bc5f0 | 34 | /*const*/ unsigned p_usbd; // USBROMD |
nexpaq | 0:6c56fb4bc5f0 | 35 | /*const*/ unsigned p_clib; |
nexpaq | 0:6c56fb4bc5f0 | 36 | /*const*/ unsigned p_cand; |
nexpaq | 0:6c56fb4bc5f0 | 37 | /*const*/ unsigned p_pwrd; // PWRROMD |
nexpaq | 0:6c56fb4bc5f0 | 38 | /*const*/ unsigned p_promd; // DIVROMD |
nexpaq | 0:6c56fb4bc5f0 | 39 | /*const*/ SPIFI_RTNS *pSPIFID; // SPIFIROMD |
nexpaq | 0:6c56fb4bc5f0 | 40 | /*const*/ unsigned p_dev3; |
nexpaq | 0:6c56fb4bc5f0 | 41 | /*const*/ unsigned p_dev4; |
nexpaq | 0:6c56fb4bc5f0 | 42 | } ROM; |
nexpaq | 0:6c56fb4bc5f0 | 43 | |
nexpaq | 0:6c56fb4bc5f0 | 44 | #define ROM_DRIVERS_PTR ((ROM *)(*((unsigned int *)SPIFI_ROM_PTR))) |
nexpaq | 0:6c56fb4bc5f0 | 45 | #define IS_ADDR_IN_SPIFI(__addr) ( (((uint32_t)(__addr)) & 0xff000000) == SPIFI_MEM_BASE ) |
nexpaq | 0:6c56fb4bc5f0 | 46 | #define IS_ADDR_IN_IFLASH(__addr) ( (((uint32_t)(__addr)) & 0xff000000) == 0x10000000 ) |
nexpaq | 0:6c56fb4bc5f0 | 47 | |
nexpaq | 0:6c56fb4bc5f0 | 48 | static void initialize_spifi(void) |
nexpaq | 0:6c56fb4bc5f0 | 49 | { |
nexpaq | 0:6c56fb4bc5f0 | 50 | SPIFIobj* obj = (SPIFIobj*)malloc(sizeof(SPIFIobj)); |
nexpaq | 0:6c56fb4bc5f0 | 51 | if (obj == NULL) { |
nexpaq | 0:6c56fb4bc5f0 | 52 | // Failed to allocate memory for ROM data |
nexpaq | 0:6c56fb4bc5f0 | 53 | notify_completion(false); |
nexpaq | 0:6c56fb4bc5f0 | 54 | } |
nexpaq | 0:6c56fb4bc5f0 | 55 | |
nexpaq | 0:6c56fb4bc5f0 | 56 | // Turn on SPIFI block as it is disabled on reset |
nexpaq | 0:6c56fb4bc5f0 | 57 | LPC_SC->PCONP |= 0x00010000; |
nexpaq | 0:6c56fb4bc5f0 | 58 | |
nexpaq | 0:6c56fb4bc5f0 | 59 | // pinsel for SPIFI |
nexpaq | 0:6c56fb4bc5f0 | 60 | LPC_IOCON->P2_7 = 5; /* SPIFI_CSN @ P2.7 */ |
nexpaq | 0:6c56fb4bc5f0 | 61 | LPC_IOCON->P0_22 = 5; /* SPIFI_CLK @ P0.22 */ |
nexpaq | 0:6c56fb4bc5f0 | 62 | LPC_IOCON->P0_15 = 5; /* SPIFI_IO2 @ P0.15 */ |
nexpaq | 0:6c56fb4bc5f0 | 63 | LPC_IOCON->P0_16 = 5; /* SPIFI_IO3 @ P0.16 */ |
nexpaq | 0:6c56fb4bc5f0 | 64 | LPC_IOCON->P0_17 = 5; /* SPIFI_IO1 @ P0.17 */ |
nexpaq | 0:6c56fb4bc5f0 | 65 | LPC_IOCON->P0_18 = 5; /* SPIFI_IO0 @ P0.18 */ |
nexpaq | 0:6c56fb4bc5f0 | 66 | |
nexpaq | 0:6c56fb4bc5f0 | 67 | uint32_t spifi_clk_div = (*((volatile uint32_t*)0x400FC1B4)) & 0x1f; |
nexpaq | 0:6c56fb4bc5f0 | 68 | uint32_t spifi_clk_mhz = (SystemCoreClock / spifi_clk_div) / 1000000; |
nexpaq | 0:6c56fb4bc5f0 | 69 | |
nexpaq | 0:6c56fb4bc5f0 | 70 | const SPIFI_RTNS* _spifi = ROM_DRIVERS_PTR->pSPIFID; |
nexpaq | 0:6c56fb4bc5f0 | 71 | |
nexpaq | 0:6c56fb4bc5f0 | 72 | /* Typical time tCS is 20 ns min, we give 200 ns to be on safer side */ |
nexpaq | 0:6c56fb4bc5f0 | 73 | int rc = _spifi->spifi_init (obj, spifi_clk_mhz/5, S_FULLCLK+S_RCVCLK, spifi_clk_mhz); |
nexpaq | 0:6c56fb4bc5f0 | 74 | if (rc) { |
nexpaq | 0:6c56fb4bc5f0 | 75 | // Failed to initialize SPIFI |
nexpaq | 0:6c56fb4bc5f0 | 76 | notify_completion(false); |
nexpaq | 0:6c56fb4bc5f0 | 77 | } |
nexpaq | 0:6c56fb4bc5f0 | 78 | } |
nexpaq | 0:6c56fb4bc5f0 | 79 | |
nexpaq | 0:6c56fb4bc5f0 | 80 | int main() { |
nexpaq | 0:6c56fb4bc5f0 | 81 | |
nexpaq | 0:6c56fb4bc5f0 | 82 | initialize_spifi(); |
nexpaq | 0:6c56fb4bc5f0 | 83 | |
nexpaq | 0:6c56fb4bc5f0 | 84 | // Make sure that cube_image is placed in SPIFI |
nexpaq | 0:6c56fb4bc5f0 | 85 | if (!IS_ADDR_IN_SPIFI(cube_image)) { |
nexpaq | 0:6c56fb4bc5f0 | 86 | notify_completion(false); |
nexpaq | 0:6c56fb4bc5f0 | 87 | } |
nexpaq | 0:6c56fb4bc5f0 | 88 | |
nexpaq | 0:6c56fb4bc5f0 | 89 | // Make sure that cube_image_ref is in IFLASH |
nexpaq | 0:6c56fb4bc5f0 | 90 | if (IS_ADDR_IN_SPIFI(cube_image_ref)) { |
nexpaq | 0:6c56fb4bc5f0 | 91 | notify_completion(false); |
nexpaq | 0:6c56fb4bc5f0 | 92 | } |
nexpaq | 0:6c56fb4bc5f0 | 93 | |
nexpaq | 0:6c56fb4bc5f0 | 94 | // Compare content |
nexpaq | 0:6c56fb4bc5f0 | 95 | if (cube_image_sz != cube_image_ref_sz) { |
nexpaq | 0:6c56fb4bc5f0 | 96 | notify_completion(false); |
nexpaq | 0:6c56fb4bc5f0 | 97 | } else { |
nexpaq | 0:6c56fb4bc5f0 | 98 | int i = 0; |
nexpaq | 0:6c56fb4bc5f0 | 99 | for (; i < cube_image_sz; i++) { |
nexpaq | 0:6c56fb4bc5f0 | 100 | if (cube_image[i] != cube_image_ref[i]) { |
nexpaq | 0:6c56fb4bc5f0 | 101 | notify_completion(false); |
nexpaq | 0:6c56fb4bc5f0 | 102 | } |
nexpaq | 0:6c56fb4bc5f0 | 103 | } |
nexpaq | 0:6c56fb4bc5f0 | 104 | } |
nexpaq | 0:6c56fb4bc5f0 | 105 | |
nexpaq | 0:6c56fb4bc5f0 | 106 | notify_completion(true); |
nexpaq | 0:6c56fb4bc5f0 | 107 | } |