/* * Send AT command to SIMCOM module and check response immediately. */ static int SendCmd_Check(char *cmd, char *check) { int result = 1; char recv_str[32]; dbg_puts("Send: "); dbg_puts(cmd); dbg_puts("\n\r"); TM_USART_Puts(USART1, cmd); TM_USART_Puts(USART1, "\r\n"); if(check) { RecvResponse(recv_str); if(strlen(recv_str) > 0) { dbg_puts("Recv: "); dbg_puts(recv_str); dbg_puts("\n\r"); } if(!strstr(recv_str, check)) { result = 0; } } return result; }
int main(void) { char str[15]; //Initialize system SystemInit(); //Initialize Delay library TM_DELAY_Init(); //Initialize USART1-> TX: PA9, RX: PA10 TM_USART_Init(USART1, TM_USART_PinsPack_1, 9600); TM_USART_Puts(USART1, "ADC example"); //Initialize ADC1 on channel 0, this is pin PA0 TM_ADC_Init(ADC1, ADC_Channel_0); //Initialize ADC1 on channel 3, this is pin PA3 TM_ADC_Init(ADC1, ADC_Channel_3); while (1) { // Read ADC1 Channel0 Read ADC1 Channel3 sprintf(str, "%d: %d\n\r", TM_ADC_Read(ADC1, ADC_Channel_0), TM_ADC_Read(ADC1, ADC_Channel_3)); TM_USART_Puts(USART1, str); Delayms(100); } }
/** * Read the configuration file * Name: SDCard_readConfig * * Description: Mounts the SD card and opens the configuration file * The data in the file is extracted and placed in a buffer * The file is then closed and the SD card unmounted * * Arguments: void * * Returns: void */ void SDCard_readConfig() { char str[512]; uint32_t count; fres = f_mount(&fatFs, "SD:", 0); if (fres != FR_OK) { sprintf(str, "Error Message @%d\n\r", fres); /* Put to USART */ TM_USART_Puts(USART1, str); } if ((fres = f_open(&file, "SD:config.txt", FA_READ | FA_WRITE)) != FR_OK){ if (fres == FR_NO_FILE) { sprintf(str, "NO CONFIG FILE AVAILABLE\n\r"); TM_USART_Puts(USART1, str); } else { sprintf(str, "Failed to open file. Error: %d\n\r", fres); TM_USART_Puts(USART1, str); } } else { f_read(&file, SD_Buffer, sizeof(SD_Buffer), &count); sprintf(str, "Reading is done. Read %d bytes\n\r", count); TM_USART_Puts(USART1, str); f_close(&file); f_mount(NULL, "SD:", 1); sprintf(str, "File Contents:\n\r%s", SD_Buffer); /* Put to USART */ TM_USART_Puts(USART1, str); } }
/* * Send AT command. */ static void SendCmd(char *cmd) { dbg_puts("Send: "); dbg_puts(cmd); dbg_puts("\n\r"); TM_USART_Puts(USART1, cmd); TM_USART_Puts(USART1, "\r\n"); }
int main(void){ SystemInit(); TM_DELAY_Init(); TM_ILI9341_Init(); TM_ILI9341_SetLayer1(); /* Initialize USART2 at 115200 baud, TX: PD5, RX: PD6 */ TM_USART_Init(USART2, TM_USART_PinsPack_2, 115200); uint8_t wacc = 0x3A; // 0xA6 uint8_t racc = 0x3B; // 0xA7 // 0x2D POWER_CTL: Power-saving features control TM_I2C_Write(I2C2, wacc, 0x2D, 0x08); // 0x31 DATA_FORMAT: Data format control //TM_I2C_Write(I2C1, wacc, 0x31, 0x0B); // FULL_RES and +- 16g TM_I2C_Write(I2C2, wacc, 0x31, 0x01); // fixed resolution and +- 4g // 0x2C BW_RATE: Data rate and power mode control TM_I2C_Write(I2C2, wacc, 0x2C, 0x0A); char str[16] = {0}; sprintf(str, "delay = 100"); TM_USART_Puts(USART2, str); while(1){ TM_ILI9341_Fill(ILI9341_COLOR_WHITE); TM_ILI9341_Puts(30, 30, str, &TM_Font_11x18, ILI9341_COLOR_WHITE, ILI9341_COLOR_BLUE2); uint8_t buff[6] = {0}; int16_t tri[3] = {0}; TM_I2C_ReadMulti(I2C2, racc, 0x32, buff, 6); // original read digit tri[0] = (int16_t) ((uint16_t)buff[1] << 8 | (uint16_t)buff[0]); tri[1] = (int16_t) ((uint16_t)buff[3] << 8 | (uint16_t)buff[2]); tri[2] = (int16_t) ((uint16_t)buff[5] << 8 | (uint16_t)buff[4]); float ftri[3] = {0}, divisor = 128.0f; ftri[0] = (float) tri[0] / divisor; ftri[1] = (float) tri[1] / divisor; ftri[2] = (float) tri[2] / divisor; sprintf(str, "%.3f,%.3f,%.3f\n\r", ftri[0], ftri[1], ftri[2]); TM_USART_Puts(USART2, str); TM_ILI9341_Puts(30, 50, str, &TM_Font_11x18, ILI9341_COLOR_BLACK, ILI9341_COLOR_WHITE); Delayms(100); } }
/* Called on alarm B interrupt */ void TM_RTC_AlarmBHandler(void) { /* Show user to USART */ TM_USART_Puts(USART3, "Alarm B triggered\n"); /* Disable Alarm so it will not trigger next month at the same date and time */ //TM_RTC_DisableAlarm(TM_RTC_Alarm_A); }
/* Called on alarm A interrupt */ void TM_RTC_AlarmAHandler(void) { /* Show user to USART */ TM_USART_Puts(USART3, "Alarm A triggered\n"); /* Disable Alarm so it will not trigger next week at the same time */ //TM_RTC_DisableAlarm(TM_RTC_Alarm_A); }
/** * Receive Handler for USART1 * Name: TM_USART1_ReceiveHandler * * Description: Gives functionality when a character is received over the USART serial communication channel * Stores data received into a buffer for use in other functions * Control available for use of the backspace key, functions called when enter key pressed (carriage return) * Used to set the RTC time * * Arguments: uint8_t c - character received * * Returns: void */ void TM_USART1_ReceiveHandler(uint8_t c){ char str[256]; TM_USART_Putc(USART1, c); buffer[i] = c; i++; /* Backspace */ if (c == 0x7F){ i--; buffer[i] = 0; i--; buffer[i] = 0; } /* Carriage Return */ if (c == 13){ if (buffer[0] == 'R' && buffer[1] == 'T' && buffer[2] == 'C'){ if (isdigit(buffer[4]) && isdigit(buffer[5]) && !isdigit(buffer[6]) && isdigit(buffer[22])){ strncpy(RTC_time, (char *) buffer + 4, sizeof(buffer) - 4); /* Set RTC time */ TM_RTC_SetDateTimeString(RTC_time); } } sprintf(str, "\n\rRTC %s\n\r", RTC_time); /* Put to USART */ TM_USART_Puts(USART1, str); memset(buffer, 0, sizeof(buffer)); i = 0; } }
void set_GPS(void) { TM_GPS_Data_t position; FloatOrUInt tmp; uint32_t timeout = 60000; sprintf(buffer,"Trying to update GPS position with %ld seconds timeout\n\r",timeout/1000); TM_USART_Puts(MENU_USART, buffer); GC_CheckPosition(&position, timeout); sprintf(buffer,"GPS Position:\n\r Longitude: %3.6f\n\r Latitude: %3.6f\n\r", position.Longitude, position.Latitude); TM_USART_Puts(MENU_USART,buffer); tmp.f=position.Latitude; config(GC_SAVE,tmp.fInt,GC_LATITUDE); tmp.f=position.Longitude; config(GC_SAVE,tmp.fInt,GC_LONGITUDE); }
int main(void) { /* Init system clock for maximum system speed */ TM_RCC_InitSystem(); /* Init HAL layer */ HAL_Init(); /* Init leds */ TM_DISCO_LedInit(); /* Init button */ TM_DISCO_ButtonInit(); /* Init USART, Pins not initialized yet, 921600 bauds */ TM_USART_Init(USART6, TM_USART_PinsPack_Custom, 921600); /* Put test string */ TM_USART_Puts(USART6, "Hello world\n"); while (1) { /* Check if anything received */ while (!TM_USART_BufferEmpty(USART6)) { /* Send data back from buffer */ TM_USART_Putc(USART6, TM_USART_Getc(USART6)); } } }
int main(void) { /* Init system clock for maximum system speed */ TM_RCC_InitSystem(); /* Init HAL layer */ HAL_Init(); /* Init leds */ TM_DISCO_LedInit(); /* Init USART, TX: PC6, RX: PC7, 921600 bauds */ TM_USART_Init(USART2, TM_USART_PinsPack_1, 115200); /* Put test string */ TM_USART_Puts(USART2, "Hello world\n"); while (1) { /* Check if we have string "OK" in USART6 buffer */ if (TM_USART_FindString(USART2, "OK")) { /* Send data back from buffer */ while (!TM_USART_BufferEmpty(USART2)) { /* Send to computer */ TM_USART_Putc(USART2, TM_USART_Getc(USART6)); } } } }
int main(void) { /* Initialize system */ SystemInit(); /* Init USART2 on pins TX = PA2, RX = PA3 */ /* This pins are used on Nucleo boards for USB to UART via ST-Link */ TM_USART_Init(USART2, TM_USART_PinsPack_1, 115200); /* Say string without DMA */ TM_USART_Puts(USART2, "Hello via USART2 without DMA\n"); /* Init TX DMA for USART2 */ TM_USART_DMA_Init(USART2); /* Send data with DMA */ TM_USART_DMA_Send(USART2, (uint8_t *)USART_Buffer, strlen(USART_Buffer)); /* Wait till DMA works */ /* You can do other stuff here instead of waiting for DMA to end */ while (TM_USART_DMA_Sending(USART2)); while (1) { /* If any string arrived over USART */ /* Expecting "\n" at the end of string from USART terminal or any other source */ if (TM_USART_Gets(USART2, USART_Buffer, sizeof(USART_Buffer))) { /* Send it back over DMA */ TM_USART_DMA_Send(USART2, (uint8_t *)USART_Buffer, strlen(USART_Buffer)); /* Wait till DMA works */ /* You can do other stuff here instead of waiting for DMA to end */ while (TM_USART_DMA_Sending(USART2)); } } }
int main(void) { char buf[30]; uint8_t devices, i, j, count, device[2][8]; /* Initialize system */ SystemInit(); /* Initialize delay */ TM_DELAY_Init(); /* Initialize One Wire on pin PD0 */ TM_OneWire_Init(); /* Initialize USART, TX: PB6, RX: PB7 */ TM_USART_Init(USART1, TM_USART_PinsPack_2, 115200); /* Checks for any device on 1-wire */ devices = TM_OneWire_First(); count = 0; while (devices) { count++; for (i = 0; i < 8; i++) { device[count - 1][i] = TM_OneWire_GetROM(i); } /* Check for new device */ devices = TM_OneWire_Next(); } /* If any devices on 1-wire */ if (count > 0) { sprintf(buf, "Devices found on 1-wire: %d\n\r", count); TM_USART_Puts(USART1, buf); /* Display 64bit rom code */ for (j = 0; j < count; j++) { for (i = 0; i < 8; i++) { sprintf(buf, "0x%02X ", device[j][i]); TM_USART_Puts(USART1, buf); } TM_USART_Puts(USART1, "\n\r"); } } else { TM_USART_Puts(USART1, "No devices on OneWire.\n\r"); } while (1) { } }
/* Send message to PC */ void SendString(char* message) { #ifdef ENABLE_USART TM_USART_Puts(USART1, message); #endif #ifdef ENABLE_VCP TM_USB_VCP_Puts(message); #endif }
void getTime(void) { TM_RTC_Time_t datatime; TM_RTC_GetDateTime(&datatime, TM_RTC_Format_BIN); sprintf(buffer,"Current time: %02d:%02d:%02d, %02d.%02d.%02dr.\n\r", datatime.hours, datatime.minutes, datatime.seconds, datatime.date, datatime.month, datatime.year); TM_USART_Puts(MENU_USART,buffer); }
/** * Extract data from buffer * Name: SDCard_extractConfig * * Description: Separates the buffer array filled by reading the configuration file using delimiters * Values extracted for each expected parameter * Configuration structure was populated with the extracted values * * Arguments: void * * Returns: void */ void SDCard_extractConfig(){ char str[512]; char *str1, *str2, *param, *value; char *saveptr1, *saveptr2; int j; for (j=1, str1 = (char *) SD_Buffer; ; j++, str1 = NULL) { param = strtok_r(str1, ";", &saveptr1); if (param == NULL){ break; } for (str2 = param; ; str2 = NULL) { value = strtok_r(str2, " = ", &saveptr2); if (value == NULL) { break; } /* Extract Cutoff Frequency */ if (strstr(value, "Cutoff") != NULL){ str2 = NULL; value = strtok_r(str2, " = ", &saveptr2); config.LPF_cutOffFrequency = atoi(value); } /* Extract Sampling Frequency */ else if (strstr(value, "Adc_sampling") != NULL){ str2 = NULL; value = strtok_r(str2, " = ", &saveptr2); config.ADC_samplingRate = atoi(value); } /* Extract Classification Time */ else if (strstr(value, "Time") != NULL){ str2 = NULL; value = strtok_r(str2, " = ", &saveptr2); config.classificationTime = atoi(value) * config.ADC_samplingRate; } sprintf(str, "--> %s\n\r", value); /* Put to USART */ TM_USART_Puts(USART1, str); } } sprintf(str, "cut = %u, adc = %u, time = %u\n\r", config.LPF_cutOffFrequency, config.ADC_samplingRate, config.classificationTime); /* Put to USART */ TM_USART_Puts(USART1, str); }
/* DMA transfer complete callback */ void TM_DMA_TransferCompleteHandler(DMA_Stream_TypeDef* DMA_Stream) { /* Check for which stream we were successful */ if (DMA_Stream == TM_USART_DMA_GetStreamTX(USART)) { /* DMA transfer has finished */ /* We also have to wait for USART to finish transmitting last byte from DMA */ while (TM_USART_DMA_Transmitting(USART)); /* Send notification to the same USART port with polling mode */ TM_USART_Puts(USART, "DMA has sent data!\n"); } }
/** * Handle PC7 interrupt * Name: EXTI9_5_IRQHandler * * Description: Handles the interrupt generated by the card detect pin * This sends a debug message with the new status of the SD card * If no SD card is detected, the Error LED function is called * If it is detected then the LED is cleared * * Arguments: void * * Returns: void */ void EXTI9_5_IRQHandler(void) { char str[20]; /* Make sure that interrupt flag is set */ if (EXTI_GetITStatus(EXTI_Line7) != RESET) { if ((((FATFS_USE_DETECT_PIN_PORT)->IDR & (FATFS_USE_DETECT_PIN_PIN)) == 0 ? 0 : 1) != 0 && SDCardInserted == 1) { sprintf(str, "NO SD CARD INSERTED\n\r"); TM_USART_Puts(USART1, str); SDCardInserted = 0; LED_error(); } if ((((FATFS_USE_DETECT_PIN_PORT)->IDR & (FATFS_USE_DETECT_PIN_PIN)) == 0 ? 0 : 1) == 0 && SDCardInserted == 0) { SDCardInserted = 1; sprintf(str, "SD CARD INSERTED\n\r"); TM_USART_Puts(USART1, str); LED_clearError(); } /* Clear interrupt flag */ EXTI_ClearITPendingBit(EXTI_Line7); } }
int8_t SIM300SendMsg(const char *num, const char *msg,uint8_t *msg_ref) { TM_USART_ClearBuffer(USART1); char cmd[25]; sprintf(cmd,"AT+CMGS= %s",num); cmd[8]=0x22; //" uint8_t n=strlen(cmd); cmd[n]=0x22; //" cmd[n+1]='\0'; //Send Command SIM300Cmd(cmd); Delayms(100); TM_USART_Puts(USART1,msg); TM_USART_Putc(USART1,0x1A); while( TM_USART_Available(USART1)<(strlen(msg)+5) ); //Remove Echo TM_USART_Gets(USART1,sim300_buffer,strlen(msg)+5); uint8_t len=SIM300WaitForResponse(6000); if(len==0) return SIM300_TIMEOUT; sim300_buffer[len-1]='\0'; if(strncasecmp(sim300_buffer+2,"CMGS:",5)==0) { *msg_ref=atoi(sim300_buffer+8); TM_USART_ClearBuffer(USART1); return SIM300_OK; } else { TM_USART_ClearBuffer(USART1); return SIM300_FAIL; } }
int8_t SIM300Cmd(const char *cmd) { TM_USART_ClearBuffer(USART1); TM_USART_Puts(USART1,cmd); //Send Command TM_USART_Puts(USART1,"\r\n"); //CR //TM_USART_FlushBuffer(USART1); TM_USART_ClearBuffer(USART1); uint8_t len=strlen(cmd); len++; //Add 1 for trailing CR added to all commands uint16_t i=0; //Wait for echo while(i<10*len) { if(TM_USART_Available(USART1)<len) { i++; Delayms(10); continue; } else { //We got an echo //Now check it TM_USART_Gets(USART1,sim300_buffer,len); //Read serial Data return SIM300_OK; } } return SIM300_TIMEOUT; }
void goSleep(void) { TM_RTC_Interrupts(TM_RTC_Int_Disable); TM_USART_Puts(MENU_USART,"Going to sleep state now!\n\r"); USART_DeInit(MENU_USART); USART_DeInit(GSM_USART); USART_DeInit(GPS_USART); I2C_DeInit(I2C3); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_GPIOD | RCC_AHB1Periph_GPIOE, DISABLE); TM_RTC_Interrupts(TM_RTC_Int_1s); // 500ms;1,2,5,10,15,30,60 seconds to choose from TM_LOWPOWER_Standby(); //TM_LOWPOWER_StopUntilInterrupt(); //TM_LOWPOWER_SleepUntilInterrupt(1); //TM_LOWPOWER_EnableWakeUpPin(); }
void setTime(void) { TM_RTC_Time_t time; int tmp=0; TM_RTC_GetDateTime(&time, TM_RTC_Format_BIN); tmp = cmdlineGetArgInt(1); if (tmp > 0 && tmp < 25) time.hours=tmp; tmp = cmdlineGetArgInt(2); if (tmp > 0 && tmp < 61) time.minutes=tmp; tmp = cmdlineGetArgInt(3); if (tmp > 0 && tmp < 61) time.seconds=tmp; TM_RTC_SetDateTime(&time, TM_RTC_Format_BIN); conf_bit(SET,SETTINGS_1,TIME_SET); TM_USART_Puts(MENU_USART,"Time has been sucessfully set!\n\r"); }
void setDate(void) { TM_RTC_Time_t time; int tmp=0; TM_RTC_GetDateTime(&time, TM_RTC_Format_BIN); tmp = cmdlineGetArgInt(1); if (tmp > 0 && tmp < 32) time.date=tmp; tmp = cmdlineGetArgInt(2); if (tmp > 0 && tmp < 13) time.month=tmp; tmp = cmdlineGetArgInt(3); if (tmp >= 0 && tmp <= 99) time.year=tmp; TM_RTC_SetDateTime(&time, TM_RTC_Format_BIN); conf_bit(SET,SETTINGS_1,DATE_SET); TM_USART_Puts(MENU_USART,"Date has been sucessfully set!\n\r"); }
/* Called on wakeup interrupt */ void TM_RTC_RequestHandler() { /* Get time */ TM_RTC_GetDateTime(&datatime, TM_RTC_Format_BIN); /* Format time */ sprintf(buf, "%02d.%02d.%04d %02d:%02d:%02d Unix: %u\n", datatime.date, datatime.month, datatime.year + 2000, datatime.hours, datatime.minutes, datatime.seconds, datatime.unix ); /* Send to USART */ TM_USART_Puts(USART3, buf); /* Toggle LED */ TM_DISCO_LedToggle(LED_RED | LED_GREEN); }
int main(void) { /* Init system clock for maximum system speed */ TM_RCC_InitSystem(); /* Init HAL layer */ HAL_Init(); /* Init USART, check USART lib description for pinout */ TM_USART_Init(USART, TM_USART_PinsPack_1, 921600); /* Say string without DMA */ TM_USART_Puts(USART, "Hello via USART without DMA\n"); /* Init TX DMA for USART */ TM_USART_DMA_Init(USART); /* Enable interrupts for TX DMA */ TM_USART_DMA_EnableInterrupts(USART); /* Send data with DMA */ TM_USART_DMA_Send(USART, (uint8_t *)USART_Buffer, strlen(USART_Buffer)); /* Wait till DMA works */ /* You can do other stuff here instead of waiting for DMA to end */ while (TM_USART_DMA_Transmitting(USART)); while (1) { /* If any string arrived over USART */ /* Expecting "\n" at the end of string from USART terminal or any other source */ if (TM_USART_Gets(USART, USART_Buffer, sizeof(USART_Buffer))) { /* Send it back over DMA */ TM_USART_DMA_Send(USART, (uint8_t *)USART_Buffer, strlen(USART_Buffer)); /* Wait till DMA works */ /* You can do other stuff here instead of waiting for DMA to end */ while (TM_USART_DMA_Transmitting(USART)); } } }
int main(void) { uint8_t c; //Initialize system SystemInit(); TM_DELAY_Init(); //Initialize USART1 at 9600 baud, TX: PA9, RX: PA10 TM_USART_Init(USART1, TM_USART_PinsPack_1, 9600); //Put string to terminal TM_USART_Puts(USART1, "Hello world\n\r"); while (1) { //Get character from internal buffer c = TM_USART_Getc(USART1); if (c) { //If anything received, put it back to terminal TM_USART_Putc(USART1, c); } } }
void zspmStat (void) { //TM_I2C_Init(ZSPM_I2C, ZSPM_I2C_PINSPACK, ZSPM_I2C_SPEED); if (!TM_I2C_IsDeviceConnected(ZSPM_I2C, ZSPM_I2C_ADDRESS)) { TM_USART_Puts(MENU_USART,"ZMDI charger is not available\r\n"); } else { uint8_t test=0; uint8_t tmp=0; TM_I2C_Write(ZSPM_I2C, ZSPM_I2C_ADDRESS, ZSPM_CONFIG_ENABLE, BIT_EN_CFG); printf("Register 0: %x\n\r",TM_I2C_Read(ZSPM_I2C, ZSPM_I2C_ADDRESS, ZSPM_STATUS)); printf("Register 2: %x\n\r",TM_I2C_Read(ZSPM_I2C, ZSPM_I2C_ADDRESS, ZSPM_CONFIG1)); printf("Register 3: %x\n\r",TM_I2C_Read(ZSPM_I2C, ZSPM_I2C_ADDRESS, ZSPM_CONFIG2)); printf("Register 4: %x\n\r",TM_I2C_Read(ZSPM_I2C, ZSPM_I2C_ADDRESS, ZSPM_CONFIG3)); printf("Register 5: %x\n\r",TM_I2C_Read(ZSPM_I2C, ZSPM_I2C_ADDRESS, ZSPM_CONFIG4)); printf("Register 6: %x\n\r",TM_I2C_Read(ZSPM_I2C, ZSPM_I2C_ADDRESS, ZSPM_CONFIG5)); test=TM_I2C_Read(ZSPM_I2C, ZSPM_I2C_ADDRESS, ZSPM_CONFIG1); tmp=(test&BIT_PRE_CHRG)>>6; if(tmp==0x00) printf("Precharge current 50mA\n\r"); else if (tmp==0x01) printf("Precharge current 100mA\n\r"); else if (tmp==0x02) printf("Precharge current 185mA\n\r"); else if (tmp==0x03) printf("Precharge current 370mA\n\r"); } }
int main(void) { char buffer[20]; /* Initialize system */ SystemInit(); /* Initialize delay */ TM_DELAY_Init(); /* Initialize USART1, TX: PB6 */ TM_USART_Init(USART1, TM_USART_PinsPack_2, 115200); /* Initialize random number generator */ TM_RNG_Init(); while (1) { /* Get number */ sprintf(buffer, "Number: %u\n", TM_RNG_Get()); /* Put to USART */ TM_USART_Puts(USART1, buffer); Delayms(1000); } }
/** * Write data to SD card * Name: SDCard_writeData * * Description: If the SD card is detected, it is mounted and the correct file is opened * The string is created using the time from the RTC, the classification Value and the data * The file is then appended to include the new line of text * * Arguments: data_type type - enumerated type to determine what type of data is to be written * uint32_t data[] - data array * const char * classificationValue - String of classification result * * Returns: void */ void SDCard_writeData(data_type type, uint32_t data [], const char *classificationValue) { char str[codebookSize * codebookSize * 3] = {0}, fileName[14], time[25]; char contents[codebookSize * codebookSize * 3] = {0}; uint32_t count; uint32_t cnt; int i; /* Only attempt the write function if SD card is inserted */ if (SDCardInserted){ /* Get time and date */ TM_RTC_GetDateTime(&datatime, TM_RTC_Format_BIN); sprintf(time, "%02d.%02d.%04d %02d:%02d:%02d ", datatime.date, datatime.month, datatime.year + 2000, datatime.hours, datatime.minutes, datatime.seconds ); /* Set filename */ if (type == SMatrix_type){ sprintf(fileName, "SD:SMatrix.txt"); } /* Mount SD card and open file */ if ((fres = f_mount(&fatFs, "SD:", 0)) == FR_OK){ /* If file does not exist then create it */ if ((fres = f_open(&fil, fileName, FA_READ | FA_WRITE)) == FR_NO_FILE){ fres = f_open(&fil, fileName, FA_CREATE_ALWAYS | FA_READ | FA_WRITE); } /* If file does exist navigate to the end of the file */ else { f_read(&fil, str, sizeof(str), &cnt); f_lseek(&fil, cnt); } if (fres != FR_OK){ sprintf(str, "Error Message @%d\n\r", fres); TM_USART_Puts(USART1, str); } else { /* S Matrix data transmission */ if (type == SMatrix_type){ /* Populate string with S Matrix values */ for (i = 0; i < codebookSize; i++){ if (i == 0) { sprintf(contents, "%s%s%u, ", time, classificationValue, ((uint32_t *)data)[i]); } else if (i == 27) { sprintf(contents, "%s%u\n\r", contents, ((uint32_t *)data)[i]); } else { sprintf(contents, "%s%u, ", contents, ((uint32_t *)data)[i]); } } /* Append end of the file */ count = f_puts(contents, &fil); } /* Put to USART */ TM_USART_Puts(USART1, contents); sprintf(str, "Writing is done. Written %d bytes. Error = %d\n\r", count, fres); TM_USART_Puts(USART1, str); f_close(&fil); f_mount(NULL, "SD:", 1); } } else { sprintf(str, "Error Message @%d\n\r", fres); /* Put to USART */ TM_USART_Puts(USART1, str); } } }
int main(void) { char buffer[50]; /* Working structure */ TM_BMP180_t BMP180_Data; /* Initialize system */ SystemInit(); /* Initialize delay */ TM_DELAY_Init(); /* Initialize USART1, 11500baud, TX: PB6 */ TM_USART_Init(USART1, TM_USART_PinsPack_2, 115200); /* Initialize BMP180 pressure sensor */ if (TM_BMP180_Init(&BMP180_Data) == TM_BMP180_Result_Ok) { /* Init OK */ TM_USART_Puts(USART1, "BMP180 configured and ready to use\n\n"); } else { /* Device error */ TM_USART_Puts(USART1, "BMP180 error\n\n"); while (1); } /* Imagine, we are at 1000 meters above the sea */ /* And we read pressure of 95000 pascals */ /* Pressure right on the sea is */ sprintf(buffer, "Pressure right above the sea: %d pascals\n", TM_BMP180_GetPressureAtSeaLevel(95000, 1000)); TM_USART_Puts(USART1, buffer); sprintf(buffer, "Data were calculated from pressure %d pascals at know altitude %d meters\n\n\n", 95000, 1000); TM_USART_Puts(USART1, buffer); while (1) { /* Start temperature conversion */ TM_BMP180_StartTemperature(&BMP180_Data); /* Wait delay in microseconds */ /* You can do other things here instead of delay */ Delay(BMP180_Data.Delay); /* Read temperature first */ TM_BMP180_ReadTemperature(&BMP180_Data); /* Start pressure conversion at ultra high resolution */ TM_BMP180_StartPressure(&BMP180_Data, TM_BMP180_Oversampling_UltraHighResolution); /* Wait delay in microseconds */ /* You can do other things here instead of delay */ Delay(BMP180_Data.Delay); /* Read pressure value */ TM_BMP180_ReadPressure(&BMP180_Data); /* Format data and print to USART */ sprintf(buffer, "Temp: %2.3f degrees\nPressure: %6d Pascals\nAltitude at current pressure: %3.2f meters\n\n", BMP180_Data.Temperature, BMP180_Data.Pressure, BMP180_Data.Altitude ); /* Send to USART */ TM_USART_Puts(USART1, buffer); /* Some delay */ Delayms(1000); } }