void main() { int err = 0, count = 0, bytesWritten = 0; char addr[5], i = '0'; char buf[32], data[64]; uint8_t status; uint32_t ACLKfreq; BYTE temp[100]; UINT bw = 0, btw; FRESULT iFResult; DWORD sizeBuf = 0; //Initialization Functions err = InitFunction(); err |= ADC_InitFunction(); err |= I2C_InitFunc(calData); err |= InitOneWire(); //Initialize the SD card Use_SD_CARD = YES; if(SD_CardInit()) { Use_SD_CARD = NO; } //Initialize the nRF wireless module ACLKfreq = MAP_CS_getACLK(); // get ACLK value to verify it was set correctly rf_crc = RF24_CRCO; rf_addr_width = (uint8_t)ADDRESS_WIDTH; rf_speed_power = RF24_SPEED_MIN | RF24_POWER_MAX; rf_channel = 120; msprf24_init(); // All RX pipes closed by default msprf24_open_pipe(0, 1); // Open pipe#0 with Enhanced ShockBurst enabled for receiving Auto-ACKs msprf24_set_pipe_packetsize(0, (uint8_t)ADDRESS_WIDTH); // Dynamic payload length enabled (size=0) // Transmit to 'rad01' (0x72 0x61 0x64 0x30 0x31) msprf24_standby(); memcpy(addr, "\xDE\xAD\xBE\xEF\x01", 5); // addr[0] = 'r'; addr[1] = 'a'; addr[2] = 'd'; addr[3] = '0'; addr[4] = '2'; w_tx_addr(addr); w_rx_addr(0, addr); // Pipe 0 receives auto-ack's, autoacks are sent back to the TX addr so the PTX node // needs to listen to the TX addr on pipe#0 to receive them. buf[0] = '1'; buf[1] = '\0'; // w_tx_payload(1, buf); w_tx_payload_noack(1, buf); msprf24_activate_tx(); if (rf_irq & RF24_IRQ_FLAGGED) { msprf24_get_irq_reason(); // this updates rf_irq if (rf_irq & RF24_IRQ_TX) status = 1; if (rf_irq & RF24_IRQ_TXFAILED) status = 0; msprf24_irq_clear(RF24_IRQ_MASK); // Clear any/all of them } memset(buf, 0, sizeof(buf)); MAP_Interrupt_enableMaster(); // MAP_Interrupt_setPriority(INT_TA0_0, 0x00); //Entering the main loop while(1) { while(Refresh == 0) { //Keep the process locked here until 'Refresh' is set high. } MAP_ADC14_toggleConversionTrigger(); Refresh = 0; //Get light value GetLightValue(&lux, &lightIndex); //Get temperature and pressure GetBaroTemp(calData, &temperature, &pressure); //Get temperature and humidity __delay_cycles(100); dht_start_read(); int t = dht_get_temp(); int h = dht_get_rh(); __delay_cycles(100); //For debug purposes printf("Temperature: %d\n", temperature); printf("Pressure: %d\n", pressure); printf("Lux: %d\n", lux); printf("Humidity: %d\n", h); //Form the string to send to base station sprintf(buf, "<T%003dP%000006dH%003dL%00005d>", temperature, pressure, h, lux); //transmit data GPIO_setOutputHighOnPin(GPIO_PORT_P1, GPIO_PIN0); w_tx_payload((uint8_t)ADDRESS_WIDTH, buf); msprf24_activate_tx(); GPIO_setOutputLowOnPin(GPIO_PORT_P1, GPIO_PIN0); //open SD card if(Use_SD_CARD == YES) { iFResult = f_open(&g_sFileObject, "data.txt", FA_OPEN_EXISTING | FA_WRITE); count = 0; //if the first open fails, unmount and try again for 5 times while ((iFResult != FR_OK) && (count < 5)) { iFResult = f_mount(0, 0); count++; __delay_cycles(10000); iFResult = f_mount(0, &g_sFatFs); __delay_cycles(10000); iFResult |= f_open(&g_sFileObject, "data.txt", FA_WRITE | FA_CREATE_NEW); } // sizeBuf = strlen(temp) + 1; sizeBuf = sizeBuf + bw; sprintf(temp, "\n%d, %d, %d, %d ", temperature, pressure, lux, h); btw = strlen(temp); if(count < 5) { //Find the end of the file and write new data here iFResult = f_lseek(&g_sFileObject, sizeBuf); iFResult = f_write(&g_sFileObject, temp, btw, &bw); //close the file iFResult = f_close(&g_sFileObject); } } } }
int main(void) { UART_Init(); UART_SendStr("\nSTM32F103RET6 is online.\n"); SD_Init(); uint8_t b = 0; UART_SendStr("SD_CardInit: "); b = SD_CardInit(); UART_SendHex8(b); UART_SendStr("\nCard version: "); UART_SendHex8(SD_CardType); uint8_t response = 0; UART_SendStr("\nCSD: "); response = SD_Read_CSD(); if (response != 0x00) { UART_SendStr("error = "); UART_SendHex8(response); } else UART_SendBufHex((char*)&SD_CSD[0],16); UART_SendStr("\nCID: "); response = SD_Read_CID(); if (response != 0x00) { UART_SendStr("error = "); UART_SendHex8(response); } else UART_SendBufHex((char*)&SD_CID[0],16); UART_SendStr("\nMax bus clk.: "); UART_SendHex32(SD_MaxBusClkFreq); UART_SendStr("\nDevice size: "); UART_SendInt(SD_CardCapacity >> 10); UART_SendStr("Mb\n"); response = SD_Read_Block(0); if (response != 0x00) { UART_SendStr("error = "); UART_SendHex8(response); } else { UART_SendBufHexFancy((char*)&SD_sector[0],512,32,'.'); UART_SendStr("CRC16: "); UART_SendHex16(SD_CRC16_rcv); UART_SendStr(" == "); UART_SendHex16(SD_CRC16_cmp); } UART_SendChar('\n'); response = SD_Read_Block(1); if (response != 0x00) { UART_SendStr("error = "); UART_SendHex8(response); } else { UART_SendBufHexFancy((char*)&SD_sector[0],512,32,'.'); UART_SendStr("CRC16: "); UART_SendHex16(SD_CRC16_rcv); UART_SendStr(" == "); UART_SendHex16(SD_CRC16_cmp); } UART_SendChar('\n'); while(1); }