Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 /***** Includes *****/ 00002 #include "mbed.h" 00003 #include "max32630fthr.h" 00004 #include "USBSerial.h" 00005 00006 #include "gpio.h" 00007 #include "mxc_config.h" 00008 #include "clkman.h" 00009 #include "ioman.h" 00010 #include "spim.h" 00011 #include "spis.h" 00012 00013 /***** Globals *****/ 00014 00015 MAX32630FTHR pegasus(MAX32630FTHR::VIO_3V3); 00016 uint8_t spiwidth = 0; 00017 00018 // Hardware serial port over DAPLink 00019 Serial daplink(P2_1, P2_0); 00020 00021 // Virtual serial port over USB 00022 USBSerial microUSB; 00023 00024 DigitalOut rLED(LED1); 00025 DigitalOut gLED(LED2); 00026 DigitalOut bLED(LED3); 00027 00028 00029 00030 /***** Functions *****/ 00031 00032 union spisRXcontainer32 { 00033 uint8_t rx8[4]; 00034 uint32_t rx32; 00035 }; 00036 00037 uint32_t spis_read_quad32bit() { 00038 spisRXcontainer32 rxbuf; 00039 00040 spis_req_t req; 00041 00042 req.len = 4; 00043 req.width = SPIS_WIDTH_4; 00044 req.tx_data = NULL; 00045 req.rx_data = rxbuf.rx8; 00046 req.deass = 0; 00047 00048 SPIS_Trans(MXC_SPIS, &req); 00049 00050 return rxbuf.rx32; 00051 } 00052 00053 uint8_t spis_read_8bit() { 00054 uint8_t rxbuf; 00055 00056 spis_req_t req; 00057 00058 req.len = 1; 00059 req.width = SPIS_WIDTH_4; 00060 req.tx_data = NULL; 00061 req.rx_data = &rxbuf; 00062 req.deass = 0; 00063 00064 SPIS_Trans(MXC_SPIS, &req); 00065 00066 return rxbuf; 00067 } 00068 00069 00070 void spis_config() { 00071 //fthr extra, set shared pin to high Z input 00072 gpio_cfg_t gpio_cfg; 00073 gpio_cfg.port = 5; 00074 gpio_cfg.mask = PIN_0 | PIN_1 | PIN_2 | PIN_3 | PIN_4 | PIN_5; 00075 gpio_cfg.func = GPIO_FUNC_GPIO; 00076 gpio_cfg.pad= GPIO_PAD_INPUT; 00077 GPIO_Config(&gpio_cfg); 00078 00079 int error; 00080 uint8_t spis_mode = 0; 00081 sys_cfg_spis_t sys_cfg; 00082 00083 //IO Config req pin, core I/O, quad, fast usr manual pg333 00084 sys_cfg.io_cfg = IOMAN_SPIS(0, 1, 1, 1); 00085 sys_cfg.clk_scale = CLKMAN_SCALE_AUTO; 00086 00087 00088 if((error = SPIS_Init(MXC_SPIS, spis_mode, &sys_cfg)) != E_NO_ERROR) { 00089 microUSB.printf("Error initializing SPIS %d\n", error); 00090 while(1) {} 00091 } else { 00092 microUSB.printf("SPIS Initialized, SPIS Clock speed %d\n", SYS_SPIS_GetFreq()); 00093 00094 //Maxim SDK doesn't provide init function to set spis quad mode 00095 spiwidth = MXC_SPIS->gen_ctrl & 0b00110000; //bits location 5:4 00096 daplink.printf("initial spi width mode: %d\r\n", (spiwidth >> 4) ); 00097 00098 MXC_SPIS->gen_ctrl &= ~(1UL << 4); //set quad 00099 MXC_SPIS->gen_ctrl |= 1UL << 5; 00100 00101 spiwidth = MXC_SPIS->gen_ctrl & 0b00110000; //bits location 5:4 00102 daplink.printf("set spi width mode : %d\r\n", (spiwidth >> 4) ); 00103 00104 } 00105 }; 00106 00107 00108 00109 // ***************************************************************************** 00110 // main() runs in its own thread in the OS 00111 // (note the calls to Thread::wait below for delays) 00112 int main() 00113 { 00114 daplink.printf("daplink serial port\r\n"); 00115 microUSB.printf("micro USB serial port\r\n"); 00116 00117 rLED = LED_ON; 00118 gLED = LED_ON; 00119 bLED = LED_OFF; 00120 00121 rLED = LED_OFF; 00122 00123 //c = microUSB.getc(); 00124 spis_config(); 00125 00126 00127 uint8_t readSpi8[8]; 00128 uint32_t readSpi32[8]; 00129 00130 uint8_t spiRxAvail; 00131 00132 spiRxAvail = SPIS_NumReadAvail(MXC_SPIS); 00133 daplink.printf("buf avail:%d\r\n", spiRxAvail); 00134 00135 while(1) { 00136 00137 if( (spiwidth<<4) == 0) { 00138 //4-wire mode 00139 while( SPIS_NumReadAvail(MXC_SPIS) < 8 ) {}; 00140 00141 for (int i = 0; i< 8; i++){ 00142 readSpi8[i] = spis_read_8bit(); 00143 } 00144 00145 for (int i = 0; i< 8; i++){ 00146 daplink.printf("spi read : 0x%08x\r\n", readSpi8[i]); 00147 } 00148 00149 } 00150 00151 if( (spiwidth<<4) == 2) { 00152 //6-wire (quad) mode 00153 00154 while( SPIS_NumReadAvail(MXC_SPIS) < 4 * 8 ) {}; 00155 00156 for (int i = 0; i< 8; i++){ 00157 readSpi32[i] = spis_read_quad32bit(); 00158 } 00159 for (int i = 0; i< 8; i++){ 00160 daplink.printf("spi read : 0x%08x\r\n", readSpi32[i]); 00161 } 00162 00163 } 00164 00165 spiRxAvail = SPIS_NumReadAvail(MXC_SPIS); 00166 daplink.printf("buf left:%d\r\n", spiRxAvail); 00167 00168 } 00169 00170 00171 00172 } 00173
Generated on Tue Jul 12 2022 20:03:21 by
