main.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #include <stdint.h>
  2. #include "system_stm32f10x.h"
  3. #include "system_stm32f10x.c"
  4. #include "defines.h"
  5. #include "typedefs.h"
  6. #include "gpio.c"
  7. #include "rcc.c"
  8. #include "spi.c"
  9. #include "adc.c"
  10. #include "sdcard.c"
  11. #include <string.h>
  12. #define SD_HEADERS "\000voltlogger\000\001\001\000\000Voltlogger0000\0000\0000"
  13. SD_Error SD_WriteBlock_1(uint32_t WriteAddr)
  14. {
  15. Wstatus = BuffReady;
  16. BuffReady = 0;
  17. uint32_t i = 0;
  18. SD_Error rvalue = SD_RESPONSE_FAILURE;
  19. SD_CS_LOW();
  20. SD_SendCmd(SD_CMD_WRITE_SINGLE_BLOCK, WriteAddr, 0xFF);
  21. if (!SD_GetResponse(SD_RESPONSE_NO_ERROR))
  22. {
  23. SD_WriteByte(SD_DUMMY_BYTE);
  24. SD_WriteByte(0xFE);
  25. if (Wstatus == 1){
  26. for (i = 0; i < SD_BUFSIZE/2; i += 1)
  27. {
  28. SD_WriteByte(Buffer1[i]);
  29. SD_WriteByte(Buffer1[i] >> 8);
  30. }
  31. }
  32. if (Wstatus == 2){
  33. for (i = 0; i < SD_BUFSIZE/2; i += 1)
  34. {
  35. SD_WriteByte(Buffer2[i]);
  36. SD_WriteByte(Buffer2[i] >> 8);
  37. }
  38. }
  39. SD_ReadByte();
  40. SD_ReadByte();
  41. if (SD_GetDataResponse() == SD_DATA_OK)
  42. {
  43. rvalue = SD_RESPONSE_NO_ERROR;
  44. }
  45. }
  46. SD_CS_HIGH();
  47. SD_WriteByte(SD_DUMMY_BYTE);
  48. Wstatus = 0;
  49. return rvalue;
  50. }
  51. void ADC_Start (void){
  52. ADC_InitTypeDef ADC_InitStructure;
  53. GPIO_InitTypeDef GPIO_InitStructure;
  54. RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
  55. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 ;
  56. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
  57. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz ;
  58. GPIO_Init(GPIOA, &GPIO_InitStructure);
  59. ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
  60. ADC_InitStructure.ADC_ScanConvMode = ENABLE;
  61. ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
  62. ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
  63. ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
  64. ADC_InitStructure.ADC_NbrOfChannel = 0;
  65. ADC_Init(ADC1, &ADC_InitStructure);
  66. ADC_Cmd(ADC1, ENABLE);
  67. ADC_ResetCalibration(ADC1);
  68. while(ADC_GetResetCalibrationStatus(ADC1));
  69. ADC_StartCalibration(ADC1);
  70. while(ADC_GetCalibrationStatus(ADC1));
  71. ADC_SoftwareStartConvCmd(ADC1, ENABLE);
  72. }
  73. uint32_t SD_WriteHeaders() {
  74. //uint8_t header[SD_BUFSIZE];
  75. memset(Buffer1, 0, SD_BUFSIZE);
  76. memcpy(Buffer1, SD_HEADERS, sizeof(SD_HEADERS));
  77. SD_Status = SD_WriteBlock_1(0);
  78. return 1; // SD_BUFSIZE;
  79. }
  80. int main(void) {
  81. status = SD_Init();
  82. checkSDStatus();
  83. writeBufFilled = 0;
  84. // SD_WriteHeaders();
  85. SDWriteOffset = SD_BUFSIZE;
  86. SystemCoreClockUpdate();
  87. SysTick_Config(SystemCoreClock/5000);
  88. ADC_Start();
  89. while (1)
  90. {
  91. if (BuffReady != 0){
  92. check=1;
  93. SD_WriteBlock_1(SDWriteOffset);
  94. SDWriteOffset = SDWriteOffset + SD_BUFSIZE;
  95. }
  96. }
  97. }
  98. void SysTick_Handler(void) {
  99. //ADC1ConvertedValue = ADC_GetConversionValue(ADC1);
  100. ADC1ConvertedValue = 0xAA;
  101. if (Rstatus == 1 && Wstatus != 1){
  102. Buffer1[BuffCount] = TMSTP;
  103. BuffCount++;
  104. Buffer1[BuffCount] = ADC1ConvertedValue;
  105. BuffCount++;
  106. TMSTP++;
  107. if ( BuffCount == 0){
  108. Rstatus = 2;
  109. BuffReady = 1;
  110. }
  111. }
  112. if (Rstatus == 2 && Wstatus != 2){
  113. Buffer2[BuffCount] = TMSTP;
  114. BuffCount++;
  115. Buffer2[BuffCount] = ADC1ConvertedValue;
  116. BuffCount++;
  117. TMSTP++;
  118. if (BuffCount == 0){
  119. Rstatus = 1;
  120. BuffReady = 2;
  121. // BuffCount = 0;
  122. }
  123. }
  124. /*
  125. if ( BuffCount == 0 && Rstatus == 1 ){
  126. Rstatus = 2;
  127. BuffReady = 1;
  128. // BuffCount = 0;
  129. }
  130. if (BuffCount == 0 && Rstatus == 2){
  131. Rstatus = 1;
  132. BuffReady = 2;
  133. // BuffCount = 0;
  134. }
  135. */
  136. }