va_end(args);
}
-#if 0
-
-static uint bmp_read(uint reg, uint bytes)
-{
- LL_I2C_ClearFlag_STOP(I2C1);
- LL_I2C_ClearFlag_BERR(I2C1);
- LL_I2C_HandleTransfer(I2C1, 0xee, LL_I2C_ADDRSLAVE_7BIT, 1, LL_I2C_MODE_SOFTEND, LL_I2C_GENERATE_START_WRITE);
- while (!LL_I2C_IsActiveFlag_TXE(I2C1))
- ;
- LL_I2C_TransmitData8(I2C1, reg);
- while (!LL_I2C_IsActiveFlag_TC(I2C1))
- ;
- LL_I2C_HandleTransfer(I2C1, 0xef, LL_I2C_ADDRSLAVE_7BIT, bytes, LL_I2C_MODE_AUTOEND, LL_I2C_GENERATE_RESTART_7BIT_READ);
-
- uint d = 0;
- for (uint i=0; i<bytes; i++)
- {
- while (!LL_I2C_IsActiveFlag_RXNE(I2C1))
- ;
- d = (d << 8) | LL_I2C_ReceiveData8(I2C1);
- }
-
- return d;
-}
-
-static uint bmp_measure(uint type, uint bytes)
-{
- LL_I2C_HandleTransfer(I2C1, 0xee, LL_I2C_ADDRSLAVE_7BIT, 2, LL_I2C_MODE_AUTOEND, LL_I2C_GENERATE_START_WRITE);
- while (!LL_I2C_IsActiveFlag_TXE(I2C1))
- ;
- LL_I2C_TransmitData8(I2C1, 0xf4);
- while (!LL_I2C_IsActiveFlag_TXE(I2C1))
- ;
- LL_I2C_TransmitData8(I2C1, type);
- while (!LL_I2C_IsActiveFlag_STOP(I2C1))
- ;
-
- while (!LL_GPIO_IsInputPinSet(BMP_DONE_GPIO_Port, BMP_DONE_Pin))
- ;
-
- return bmp_read(0xf6, bytes);
-}
-
-// Formulae from BMP085 specs
-void bmp_recalc(uint UT, uint UP, uint oss, u16 cc[11], int *tt, int *pp)
-{
- s16 AC1 = cc[0];
- s16 AC2 = cc[1];
- s16 AC3 = cc[2];
- u16 AC4 = cc[3];
- u16 AC5 = cc[4];
- u16 AC6 = cc[5];
- s16 B1 = cc[6];
- s16 B2 = cc[7];
- s16 MB = cc[8];
- s16 MC = cc[9];
- s16 MD = cc[10];
- UP >>= (8-oss);
-
- int X1 = (UT-AC6)*AC5 / (1<<15);
- int X2 = MC*(1<<11) / (X1+MD);
- int B5 = X1 + X2;
- int T = (B5+8) / (1<<4);
- *tt = T;
-
- int B6 = B5 - 4000;
- X1 = (B2*(B6*B6/(1<<12))) / (1<<11);
- X2 = AC2 * B6 / (1<<11);
- int X3 = X1 + X2;
- int B3 = (((AC1*4 + X3) << oss) + 2) / 4;
- X1 = AC3 * B6 / (1<<13);
- X2 = (B1*(B6*B6/(1<<12))) / (1<<16);
- X3 = ((X1+X2) + 2) / (1<<2);
- uint B4 = (uint)(AC4 * (X3 + 32768)) / (1U<<15);
- uint B7 = (uint)(UP-B3) * (uint)(50000>>oss);
- int p;
- if (B7 < 0x80000000)
- p = (B7*2) / B4;
- else
- p = B7 / B4 * 2;
- X1 = (p/(1<<8)) * (p/(1<<8));
- X1 = (X1*3038) / (1<<16);
- X2 = (-7357*p) / (1<<16);
- p = p + (X1 + X2 + 3791) / (1<<4);
- *pp = p;
-}
-
-void run_test(void)
-{
- for (;;)
- {
- LL_GPIO_TogglePin(LD2_GPIO_Port, LD2_Pin);
-
- debug_puts("Constants:");
- u16 cc[11];
- for (uint i=0; i<11; i++)
- {
- cc[i] = bmp_read(0xaa + 2*i, 2);
- debug_printf(" %04x", cc[i]);
- }
- debug_puts("\r\n");
-
- uint raw_temp = bmp_measure(0x2e, 2);
- debug_printf("Raw temperature: %04x\r\n", raw_temp);
-
- uint oss = 3; // Over-sampling setting
- uint raw_press = bmp_measure(0xf4 | (oss<<6), 3);
- debug_printf("Raw pressure: %06x\r\n", raw_press);
-
- int temp, press;
- bmp_recalc(raw_temp, raw_press, oss, cc, &temp, &press);
- debug_printf("Temperature: %d ddegC\r\n", temp);
- debug_printf("Pressure: %d Pa\r\n", press);
-
- LL_mDelay(1000);
- }
-}
-
-#elif 0
-
-static const byte Gentium23x32[] = {
- 0x17, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF8, 0x1F, 0x00, 0x80, 0xFF, 0xFF, 0x00, 0xE0, 0xFF, 0xFF, 0x03, 0xF0, 0xFF, 0xFF, 0x0F, 0xF8, 0xFF, 0xFF, 0x1F, 0xFC, 0xFF, 0xFF, 0x3F, 0xFE, 0x01, 0xE0, 0x3F, 0x3E, 0x00, 0x00, 0x7F, 0x1F, 0x00, 0x00, 0x7C, 0x0F, 0x00, 0x00, 0x78, 0x0F, 0x00, 0x00, 0x78, 0x1F, 0x00, 0x00, 0x78, 0x3F, 0x00, 0x00, 0x78, 0x7F, 0x00, 0x00, 0x3E, 0xFE, 0x07, 0xC0, 0x3F, 0xFE, 0xFF, 0xFF, 0x1F, 0xFC, 0xFF, 0xFF, 0x0F, 0xF8, 0xFF, 0xFF, 0x07, 0xE0, 0xFF, 0xFF, 0x03, 0x80, 0xFF, 0xFF, 0x00, 0x00, 0xFC, 0x0F, 0x00, // Code for char 0
- 0x16, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0x00, 0x00, 0x00, 0x70, 0x00, 0x00, 0x70, 0x78, 0x00, 0x00, 0x70, 0x78, 0x00, 0x00, 0x78, 0x78, 0x00, 0x00, 0x78, 0x7C, 0x00, 0x00, 0x78, 0x3C, 0x00, 0x00, 0x78, 0xFE, 0xFF, 0xFF, 0x7F, 0xFE, 0xFF, 0xFF, 0x7F, 0xFE, 0xFF, 0xFF, 0x7F, 0xFF, 0xFF, 0xFF, 0x7F, 0xFF, 0xFF, 0xFF, 0x7F, 0xFE, 0xFF, 0xFF, 0x7F, 0x00, 0x00, 0x00, 0x78, 0x00, 0x00, 0x00, 0x78, 0x00, 0x00, 0x00, 0x78, 0x00, 0x00, 0x00, 0x70, 0x00, 0x00, 0x00, 0x70, 0x00, 0x00, 0x00, 0x70, 0x00, 0x00, 0x00, 0x00, // Code for char 1
- 0x15, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x80, 0x01, 0x00, 0x78, 0xE0, 0x03, 0x00, 0x7C, 0xF0, 0x03, 0x00, 0x7E, 0xF8, 0x03, 0x80, 0x7F, 0xFC, 0x01, 0xC0, 0x7F, 0xFE, 0x01, 0xE0, 0x7F, 0x3E, 0x00, 0xF0, 0x7F, 0x1E, 0x00, 0xFC, 0x7F, 0x0F, 0x00, 0xFE, 0x79, 0x0F, 0x00, 0xFF, 0x78, 0x0F, 0xC0, 0x7F, 0x78, 0x1F, 0xE0, 0x3F, 0x78, 0x3F, 0xF8, 0x0F, 0x78, 0xFF, 0xFF, 0x07, 0x78, 0xFE, 0xFF, 0x03, 0x78, 0xFE, 0xFF, 0x00, 0x7C, 0xFC, 0x7F, 0x80, 0x7F, 0xF8, 0x1F, 0x80, 0x7F, 0xE0, 0x07, 0x80, 0x7F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Code for char 2
- 0x15, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C, 0xC0, 0x00, 0x00, 0x1F, 0xF0, 0x01, 0x00, 0x1E, 0xF8, 0x01, 0x00, 0x3E, 0xFC, 0x01, 0x00, 0x3C, 0xFE, 0x00, 0x00, 0x7C, 0xFE, 0x00, 0x00, 0x78, 0x1E, 0xE0, 0x00, 0x78, 0x0F, 0xF0, 0x00, 0x78, 0x0F, 0xF0, 0x00, 0x78, 0x0F, 0xF0, 0x00, 0x78, 0x1F, 0xF8, 0x01, 0x7C, 0x3F, 0xFC, 0x01, 0x7C, 0xFF, 0xFF, 0x07, 0x3F, 0xFE, 0xFF, 0xFF, 0x3F, 0xFE, 0xFF, 0xFF, 0x1F, 0xFC, 0xCF, 0xFF, 0x0F, 0xF8, 0xC7, 0xFF, 0x07, 0xE0, 0x83, 0xFF, 0x03, 0x00, 0x00, 0xFE, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Code for char 3
- 0x17, 0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x00, // Code for char 5
- 0x17, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x7F, 0x00, 0x00, 0xF8, 0xFF, 0x01, 0x00, 0xFE, 0xFF, 0x07, 0x80, 0xFF, 0xFF, 0x0F, 0xC0, 0xFF, 0xFF, 0x1F, 0xE0, 0xFF, 0xFF, 0x3F, 0xF0, 0xFF, 0xC1, 0x3F, 0xF8, 0xF7, 0x00, 0x7E, 0xF8, 0x71, 0x00, 0x7C, 0xFC, 0x78, 0x00, 0x78, 0x7C, 0x78, 0x00, 0x78, 0x3E, 0x78, 0x00, 0x78, 0x1E, 0xF8, 0x00, 0x7C, 0x1E, 0xF8, 0x03, 0x3E, 0x0F, 0xF0, 0xFF, 0x3F, 0x0F, 0xF0, 0xFF, 0x1F, 0x0F, 0xE0, 0xFF, 0x0F, 0x06, 0xE0, 0xFF, 0x07, 0x00, 0x80, 0xFF, 0x03, 0x00, 0x00, 0x7E, 0x00, // Code for char 6
- 0x17, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x01, 0x00, 0x00, 0xF8, 0x01, 0x00, 0x00, 0xFC, 0x01, 0x00, 0x00, 0xFC, 0x01, 0x00, 0x20, 0x7C, 0x00, 0x00, 0x78, 0x3C, 0x00, 0x00, 0x7E, 0x3C, 0x00, 0x80, 0x7F, 0x3C, 0x00, 0xE0, 0x3F, 0x3C, 0x00, 0xF8, 0x3F, 0x3C, 0x00, 0xFE, 0x1F, 0x3C, 0x80, 0xFF, 0x0F, 0x3C, 0xE0, 0xFF, 0x01, 0x3C, 0xF8, 0x7F, 0x00, 0x3C, 0xFE, 0x0F, 0x00, 0xFC, 0xFF, 0x03, 0x00, 0xFC, 0xFF, 0x00, 0x00, 0xFC, 0x1F, 0x00, 0x00, 0xFC, 0x07, 0x00, 0x00, 0xFC, 0x01, 0x00, 0x00, 0x7C, 0x00, 0x00, 0x00, 0x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, 0x7F, 0x00, 0xC0, 0xFF, 0x1F, 0x00, 0x00, 0xFF, 0x03, 0x00, // Code for char 9
- 0x0F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE0, 0x03, 0xC0, 0x07, 0xF0, 0x07, 0xE0, 0x0F, 0xF8, 0x07, 0xF0, 0x0F, 0xF8, 0x07, 0xF0, 0x0F, 0xF8, 0x07, 0xF0, 0x0F, 0xF8, 0x07, 0xF0, 0x0F, 0xF8, 0x03, 0xF0, 0x07, 0xF0, 0x01, 0xE0, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // Code for char :
-};
-
-// Based on https://github.com/adafruit/Adafruit_SSD1306
-
-#define SSD1306_SETLOWCOLUMN 0x00
-#define SSD1306_SETHIGHCOLUMN 0x10
-#define SSD1306_MEMORYMODE 0x20
-#define SSD1306_SETSTARTLINE 0x40
-#define SSD1306_SETCONTRAST 0x81
-#define SSD1306_CHARGEPUMP 0x8D
-#define SSD1306_SEGREMAP 0xA0
-#define SSD1306_DISPLAYALLON_RESUME 0xA4
-#define SSD1306_DISPLAYALLON 0xA5
-#define SSD1306_NORMALDISPLAY 0xA6
-#define SSD1306_INVERTDISPLAY 0xA7
-#define SSD1306_SETMULTIPLEX 0xA8
-#define SSD1306_DISPLAYOFF 0xAE
-#define SSD1306_DISPLAYON 0xAF
-#define SSD1306_SETSTARTPAGE 0xB0
-#define SSD1306_COMSCANINC 0xC0
-#define SSD1306_COMSCANDEC 0xC8
-#define SSD1306_SETDISPLAYOFFSET 0xD3
-#define SSD1306_SETCOMPINS 0xDA
-#define SSD1306_SETVCOMDETECT 0xDB
-#define SSD1306_SETDISPLAYCLOCKDIV 0xD5
-#define SSD1306_SETPRECHARGE 0xD9
-#define SSD1306_NOP 0xE3
-
-static const byte display_init_cmds[] = {
- SSD1306_DISPLAYOFF,
- SSD1306_SETDISPLAYCLOCKDIV, 0x80, // the suggested ratio 0x80
- SSD1306_SETMULTIPLEX, 0x1F, // ratio 32
- SSD1306_SETDISPLAYOFFSET,0x0, // no offset
- SSD1306_SETSTARTLINE | 0x0, // line #0
- SSD1306_CHARGEPUMP, 0x14, // internal vcc
- SSD1306_MEMORYMODE, 0x02, // page mode
- SSD1306_SEGREMAP | 0x0, // column 0 mapped to SEG0
- SSD1306_COMSCANINC, // column scan direction not reversed
- SSD1306_SETCOMPINS, 0x02, // sequential COM pins, disable remap
- SSD1306_SETCONTRAST, 0x7F, // contrast level 127
- SSD1306_SETPRECHARGE, 0xF1, // pre-charge period (1, 15)
- SSD1306_SETVCOMDETECT, 0x40, // vcomh regulator level-
- SSD1306_DISPLAYALLON_RESUME,
- SSD1306_NORMALDISPLAY,
- SSD1306_DISPLAYON,
-};
-
-static void display_cmd(byte cmd)
-{
- LL_I2C_ClearFlag_STOP(I2C1);
- LL_I2C_HandleTransfer(I2C1, 0x79, LL_I2C_ADDRSLAVE_7BIT, 2, LL_I2C_MODE_AUTOEND, LL_I2C_GENERATE_START_WRITE);
- while (!LL_I2C_IsActiveFlag_TXE(I2C1))
- ;
- LL_I2C_TransmitData8(I2C1, 0x00); // Will send a command
- while (!LL_I2C_IsActiveFlag_TXE(I2C1))
- ;
- LL_I2C_TransmitData8(I2C1, cmd);
- while (!LL_I2C_IsActiveFlag_STOP(I2C1))
- ;
-}
-
-static void display_data_start(uint cnt)
-{
- LL_I2C_ClearFlag_STOP(I2C1);
- LL_I2C_HandleTransfer(I2C1, 0x79, LL_I2C_ADDRSLAVE_7BIT, cnt + 1, LL_I2C_MODE_AUTOEND, LL_I2C_GENERATE_START_WRITE);
- while (!LL_I2C_IsActiveFlag_TXE(I2C1))
- ;
- LL_I2C_TransmitData8(I2C1, 0x40); // Will send data
-}
-
-static void display_data(byte d)
-{
- while (!LL_I2C_IsActiveFlag_TXE(I2C1))
- ;
- LL_I2C_TransmitData8(I2C1, d);
-}
-
-static void display_data_end(void)
-{
- while (!LL_I2C_IsActiveFlag_STOP(I2C1))
- ;
-}
-
-static void display_init(void)
-{
- for (uint i=0; i < sizeof(display_init_cmds); i++)
- display_cmd(display_init_cmds[i]);
-
- for (uint p=0; p<4; p++)
- {
- display_cmd(SSD1306_SETSTARTPAGE + p);
- display_cmd(SSD1306_SETHIGHCOLUMN);
- display_cmd(SSD1306_SETLOWCOLUMN);
- display_data_start(128);
- for (uint i=0; i<128; i++)
- {
- byte x = 0;
- // x = Gentium23x32[(23*4+1)*(i/23) + 1 + 4*(i%23) + p];
- display_data(x);
- }
- display_data_end();
- }
-}
-
-static void counter(uint cnt)
-{
- byte d[5];
- for (uint i=0; i<5; i++)
- {
- d[4-i] = cnt % 10;
- cnt /= 10;
- }
-
- for (uint p=0; p<4; p++)
- {
- display_cmd(SSD1306_SETSTARTPAGE + p);
- display_cmd(SSD1306_SETHIGHCOLUMN);
- display_cmd(SSD1306_SETLOWCOLUMN);
- display_data_start(5*24);
- for (uint i=0; i<5; i++)
- {
- for (uint j=0; j<23; j++)
- {
- byte x = Gentium23x32[(23*4+1)*d[i] + 1 + 4*j + p];
- display_data(x);
- }
- display_data(0);
- }
- display_data_end();
- }
-}
-
void run_test(void)
{
uint cnt = 0;
- debug_puts("Init\r\n");
- display_init();
-
for (;;)
{
- LL_GPIO_TogglePin(LD2_GPIO_Port, LD2_Pin);
-
- debug_printf("Tick tock: %d\r\n", cnt);
-
- counter(cnt);
-
- LL_mDelay(1000);
+ debug_printf(">>> %d\r\n", cnt);
cnt++;
- }
-}
-
-#else
-
-// DS18B20 Temperature Sensor
-
-static volatile uint32_t ds_dma_buffer;
-
-static void ds_reset(void)
-{
- // debug_puts("DS18B20: Reset\r\n");
- LL_TIM_DisableCounter(TIM3);
- LL_TIM_SetOnePulseMode(TIM3, LL_TIM_ONEPULSEMODE_SINGLE);
-
- // DMA for reading pin state
- ds_dma_buffer = 0xdeadbeef;
- LL_DMA_SetMemoryAddress(DMA1, LL_DMA_CHANNEL_4, (uint32_t) &ds_dma_buffer);
- LL_DMA_SetPeriphAddress(DMA1, LL_DMA_CHANNEL_4, (uint32_t) &THERMO_GPIO_Port->IDR);
- LL_DMA_SetDataLength(DMA1, LL_DMA_CHANNEL_4, 1);
- LL_DMA_EnableChannel(DMA1, LL_DMA_CHANNEL_4);
-
- LL_TIM_OC_InitTypeDef oc;
-
- // CC1 is used to drive the DMA (read line state at specified time)
- LL_TIM_OC_StructInit(&oc);
- oc.OCMode = LL_TIM_OCMODE_FROZEN;
- oc.CompareValue = 560;
- LL_TIM_OC_Init(TIM3, LL_TIM_CHANNEL_CH1, &oc);
- LL_TIM_EnableDMAReq_CC1(TIM3);
- LL_TIM_CC_SetDMAReqTrigger(TIM3, LL_TIM_CCDMAREQUEST_CC);
-
- // CC2 is used to generate pulses (return line to idle state at specified time)
- LL_TIM_OC_StructInit(&oc);
- oc.OCMode = LL_TIM_OCMODE_FORCED_ACTIVE;
- oc.OCState = LL_TIM_OCSTATE_ENABLE;
- oc.CompareValue = 480;
- oc.OCPolarity = LL_TIM_OCPOLARITY_LOW;
- LL_TIM_OC_Init(TIM3, LL_TIM_CHANNEL_CH2, &oc);
-
- // Set timer period to the length of the whole transaction
- LL_TIM_SetAutoReload(TIM3, 999);
-
- // Pull line down and start timer
- LL_TIM_OC_SetMode(TIM3, LL_TIM_CHANNEL_CH2, LL_TIM_OCMODE_INACTIVE);
- LL_TIM_EnableCounter(TIM3);
-
- // Wait until the timer expires
- while (LL_TIM_IsEnabledCounter(TIM3))
- ;
- // Counter is automatically disabled at the end of cycle
-
- // Disable DMA
- LL_TIM_DisableDMAReq_CC1(TIM3);
- LL_DMA_DisableChannel(DMA1, LL_DMA_CHANNEL_4);
-
- // debug_printf("Init DMA: %08x [%u] (%u remains)\r\n", ds_dma_buffer, !!(ds_dma_buffer & THERMO_Pin), LL_DMA_GetDataLength(DMA1, LL_DMA_CHANNEL_4));
-}
-
-static void ds_send_byte(byte b)
-{
- // debug_printf("DS write: %02x\r\n", b);
- LL_TIM_SetAutoReload(TIM3, 99); // Each write slot takes 100μs
- for (uint m=1; m < 0x100; m <<= 1)
- {
- LL_TIM_OC_SetCompareCH2(TIM3, ((b & m) ? 1 : 89)); // 1: 1μs pulse, 0: 89μs pulse
- LL_TIM_OC_SetMode(TIM3, LL_TIM_CHANNEL_CH2, LL_TIM_OCMODE_FORCED_ACTIVE);
- LL_TIM_OC_SetMode(TIM3, LL_TIM_CHANNEL_CH2, LL_TIM_OCMODE_INACTIVE);
- LL_TIM_EnableCounter(TIM3);
- while (LL_TIM_IsEnabledCounter(TIM3))
- ;
- }
-}
-
-static byte ds_recv_byte(void)
-{
- LL_TIM_SetAutoReload(TIM3, 79); // Each read slot takes 80μs
- LL_TIM_OC_SetCompareCH2(TIM3, 1); // Generate 1μs pulse to start read slot
- LL_TIM_OC_SetCompareCH1(TIM3, 8); // Sample data 8μs after start of slot
- LL_TIM_EnableDMAReq_CC1(TIM3);
-
- uint out = 0;
- for (uint m=1; m < 0x100; m <<= 1)
- {
- ds_dma_buffer = 0xdeadbeef;
- LL_DMA_SetDataLength(DMA1, LL_DMA_CHANNEL_4, 1);
- LL_DMA_EnableChannel(DMA1, LL_DMA_CHANNEL_4);
- LL_TIM_OC_SetMode(TIM3, LL_TIM_CHANNEL_CH2, LL_TIM_OCMODE_FORCED_ACTIVE);
- LL_TIM_OC_SetMode(TIM3, LL_TIM_CHANNEL_CH2, LL_TIM_OCMODE_INACTIVE);
- LL_TIM_EnableCounter(TIM3);
- while (LL_TIM_IsEnabledCounter(TIM3))
- ;
- // FIXME: Using the Pin constant directly is fragile!
- // debug_printf("XXX %08x\r\n", ds_dma_buffer);
- if (ds_dma_buffer & THERMO_Pin)
- out |= m;
- LL_DMA_DisableChannel(DMA1, LL_DMA_CHANNEL_4);
- }
-
- LL_TIM_DisableDMAReq_CC1(TIM3);
- // debug_printf("DS read: %02x\r\n", out);
- return out;
-}
-
-static byte ds_buf[10];
-
-static int ds_recv_block(uint n)
-{
- uint crc = 0;
- for (uint i=0; i<n; i++)
- {
- uint b = ds_recv_byte();
- ds_buf[i] = b;
- for (uint j=0; j<8; j++)
- {
- uint k = (b & 1) ^ (crc >> 7);
- crc = (crc << 1) & 0xff;
- if (k)
- crc ^= 0x31;
- b >>= 1;
- }
- }
-
- if (crc)
- {
- debug_printf("WARNING: Invalid CRC %02x\r\n", crc);
- return 0;
- }
- return 1;
-}
-
-void run_test(void)
-{
- uint cnt = 0;
-
- debug_puts("Init\r\n");
- NVIC_DisableIRQ(TIM3_IRQn); // One day, we will handle everything from interrupts...
-
- // Identify device
- ds_reset();
- ds_send_byte(0x33);
- ds_recv_block(8);
-
- // FIXME: Configure precision
-
- for (;;)
- {
LL_GPIO_SetOutputPin(LD2_GPIO_Port, LD2_Pin);
- // debug_printf("Tick tock: %d\r\n", cnt);
-
- // Start measurement
- ds_reset();
- ds_send_byte(0xcc);
- ds_send_byte(0x44);
- while (ds_recv_byte() != 0xff)
- LL_mDelay(10);
-
- // Read scratch pad
- ds_reset();
- ds_send_byte(0xcc);
- ds_send_byte(0xbe);
- ds_recv_block(9);
- int t = (int16_t)(ds_buf[0] | (ds_buf[1] << 8));
- t = t*1000/16;
-
+ LL_GPIO_SetOutputPin(MEGGY1_GPIO_Port, MEGGY1_Pin);
+ LL_GPIO_SetOutputPin(MEGGY2_GPIO_Port, MEGGY2_Pin);
+ LL_GPIO_SetOutputPin(MEGGY3_GPIO_Port, MEGGY3_Pin);
+ if (LL_GPIO_IsInputPinSet(BLUE_BUTTON_GPIO_Port, BLUE_BUTTON_Pin))
+ LL_GPIO_SetOutputPin(MEGGY4_GPIO_Port, MEGGY4_Pin);
+ LL_mDelay(100);
LL_GPIO_ResetOutputPin(LD2_GPIO_Port, LD2_Pin);
- debug_printf("Temp: %d.%03d degC\r\n", t/1000, t%1000);
-
- LL_mDelay(1000);
- cnt++;
+ LL_GPIO_ResetOutputPin(MEGGY1_GPIO_Port, MEGGY1_Pin);
+ LL_GPIO_ResetOutputPin(MEGGY2_GPIO_Port, MEGGY2_Pin);
+ LL_GPIO_ResetOutputPin(MEGGY3_GPIO_Port, MEGGY3_Pin);
+ LL_GPIO_ResetOutputPin(MEGGY4_GPIO_Port, MEGGY4_Pin);
+ LL_mDelay(100);
}
}
-
-#endif