Skip to content

Commit e0134a7

Browse files
authored
feat: ctmu functions and buffer fill/clear (#123)
1 parent 8c0e975 commit e0134a7

File tree

7 files changed

+242
-26
lines changed

7 files changed

+242
-26
lines changed

pslab-core.X/commands.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "instruments/wavegenerator.h"
1111
#include "registers/system/pin_manager.h"
1212
#include "instruments/sensors.h"
13+
#include "registers/converters/ctmu.h"
1314

1415
/**
1516
* This is used to check that a received secondary command does not exceed the
@@ -240,19 +241,19 @@ command_func_t* const cmd_table[NUM_PRIMARY_CMDS + 1][NUM_SECONDARY_CMDS_MAX + 1
240241
},
241242
{ // 11 COMMON
242243
// 0 1 GET_CTMU_VOLTAGE 2 GET_CAPACITANCE 3 GET_FREQUENCY
243-
Undefined, Unimplemented, MULTIMETER_GetCapacitance, Unimplemented,
244+
Undefined, MULTIMETER_GetCTMUVolts, MULTIMETER_GetCapacitance, Unimplemented,
244245
// 4 GET_INDUCTANCE 5 GET_VERSION 6 7
245246
Unimplemented, DEVICE_GetVersion, Undefined, Undefined,
246247
// 8 RETRIEVE_BUFFER 9 GET_HIGH_FREQUENCY 10 CLEAR_BUFFER 11 SET_RGB1
247-
BUFFER_Retrieve, Unimplemented, Unimplemented, Removed,
248+
BUFFER_Retrieve, Unimplemented, BUFFER_Clear, Removed,
248249
// 12 READ_PROGRAM_ADDRESS 13 WRITE_PROGRAM_ADDRESS 14 READ_DATA_ADDRESS 15 WRITE_DATA_ADDRESS
249250
Removed, Removed, DEVICE_ReadRegisterData, DEVICE_WriteRegisterData,
250251
// 16 GET_CAP_RANGE 17 SET_RGB2 18 READ_LOG 19 RESTORE_STANDALONE
251252
Unimplemented, Removed, Removed, DEVICE_Reset,
252253
// 20 GET_ALTERNATE_HIGH_FREQUENCY 21 SET_RGB_COMMON 22 SET_RGB3 23 START_CTMU
253-
Unimplemented, LIGHT_RGBPin, Removed, Unimplemented,
254+
Unimplemented, LIGHT_RGBPin, Removed, CTMU_Start,
254255
// 24 STOP_CTMU 25 START_COUNTING 26 FETCH_COUNT 27 FILL_BUFFER
255-
Unimplemented, SENSORS_StartCounter, SENSORS_GetCounter, Unimplemented,
256+
CTMU_Stop, SENSORS_StartCounter, SENSORS_GetCounter, BUFFER_Fill,
256257
},
257258
{ // 12 PASSTHROUGH
258259
// 0 1 2 3

pslab-core.X/helpers/buffer.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include <string.h>
12
#include "../bus/uart/uart1.h"
23
#include "../commands.h"
34
#include "../registers/system/pin_manager.h"
@@ -15,3 +16,26 @@ response_t BUFFER_Retrieve(void) {
1516

1617
return SUCCESS;
1718
}
19+
20+
response_t BUFFER_Fill(void) {
21+
22+
uint16_t start = UART1_ReadInt();
23+
uint16_t end = UART1_ReadInt();
24+
25+
uint16_t i;
26+
for (i = start; i < start + end; i++) {
27+
BUFFER[i] = UART1_ReadInt();
28+
}
29+
30+
return SUCCESS;
31+
}
32+
33+
response_t BUFFER_Clear(void) {
34+
35+
uint16_t start = UART1_ReadInt();
36+
uint16_t end = UART1_ReadInt();
37+
38+
memset((void *) &BUFFER[start], 0, end - start);
39+
40+
return SUCCESS;
41+
}

pslab-core.X/helpers/buffer.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,36 @@ extern "C" {
2323
* @return SUCCESS
2424
*/
2525
response_t BUFFER_Retrieve(void);
26+
27+
/**
28+
* @brief Populate BUFFER array
29+
*
30+
* @description
31+
* This command function takes two arguments over serial:
32+
* 1. The starting index in the buffer from which to fill values.
33+
* 2. The number of values to be sent.
34+
*
35+
* It does not return anything over serial.
36+
* It sends an acknowledge byte (SUCCESS)
37+
*
38+
* @return SUCCESS
39+
*/
40+
response_t BUFFER_Fill(void);
41+
42+
/**
43+
* @brief Clear BUFFER array
44+
*
45+
* @description
46+
* This command function takes two arguments over serial:
47+
* 1. The starting index in the buffer from which it should be emptied
48+
* 2. The number of indices need cleared.
49+
*
50+
* It does not return anything over serial.
51+
* It sends an acknowledge byte (SUCCESS)
52+
*
53+
* @return SUCCESS
54+
*/
55+
response_t BUFFER_Clear(void);
2656

2757
#ifdef __cplusplus
2858
}

pslab-core.X/instruments/multimeter.c

Lines changed: 56 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "../registers/memory/dma.h"
77
#include "../registers/system/pin_manager.h"
88
#include "../registers/timers/tmr5.h"
9+
#include "multimeter.h"
910

1011
#define CHARGE 0
1112
#define DISCHARGE 1
@@ -70,6 +71,30 @@ void GetCapacitance_ConfigADC_CTMU_TMR5() {
7071
TMR5_WaitForInterruptEvent();
7172
}
7273

74+
uint16_t GetVoltage_Summed(uint8_t channel) {
75+
76+
ADC1_SetOperationMode(ADC1_12BIT_AVERAGING_MODE, channel, 0);
77+
78+
ADC1_Enable();
79+
DELAY_us(20);
80+
ADC1_AutomaticSamplingEnable();
81+
82+
ADC1_WaitForInterruptEvent();
83+
84+
while (!ADC1_IsConversionComplete());
85+
86+
ADC1_AutomaticSamplingDisable();
87+
ADC1_Disable();
88+
89+
uint16_t voltage_sum =
90+
(ADC1BUF0) + (ADC1BUF1) + (ADC1BUF2) + (ADC1BUF3) +
91+
(ADC1BUF4) + (ADC1BUF5) + (ADC1BUF6) + (ADC1BUF7) +
92+
(ADC1BUF8) + (ADC1BUF9) + (ADC1BUFA) + (ADC1BUFB) +
93+
(ADC1BUFC) + (ADC1BUFD) + (ADC1BUFE) + (ADC1BUFF);
94+
95+
return voltage_sum;
96+
}
97+
7398
response_t MULTIMETER_GetVoltage(void) {
7499

75100
uint8_t channel = UART1_Read();
@@ -113,26 +138,8 @@ response_t MULTIMETER_GetVoltage(void) {
113138
response_t MULTIMETER_GetVoltageSummed(void) {
114139

115140
uint8_t channel = UART1_Read();
116-
117-
ADC1_SetOperationMode(ADC1_12BIT_AVERAGING_MODE, channel, 0);
118-
119-
ADC1_Enable();
120-
DELAY_us(20);
121-
ADC1_AutomaticSamplingEnable();
122-
123-
ADC1_WaitForInterruptEvent();
124-
125-
while (!ADC1_IsConversionComplete());
126-
127-
ADC1_AutomaticSamplingDisable();
128-
ADC1_Disable();
129-
130-
uint16_t voltage_sum =
131-
(ADC1BUF0) + (ADC1BUF1) + (ADC1BUF2) + (ADC1BUF3) +
132-
(ADC1BUF4) + (ADC1BUF5) + (ADC1BUF6) + (ADC1BUF7) +
133-
(ADC1BUF8) + (ADC1BUF9) + (ADC1BUFA) + (ADC1BUFB) +
134-
(ADC1BUFC) + (ADC1BUFD) + (ADC1BUFE) + (ADC1BUFF);
135-
141+
142+
uint16_t voltage_sum = GetVoltage_Summed(channel);
136143
UART1_WriteInt(voltage_sum);
137144

138145
return SUCCESS;
@@ -185,3 +192,32 @@ response_t MULTIMETER_GetCapacitance(void) {
185192

186193
return SUCCESS;
187194
}
195+
196+
response_t MULTIMETER_GetCTMUVolts(void) {
197+
198+
uint8_t config = UART1_Read();
199+
200+
CTMU_Initialize();
201+
// Edge delay generation
202+
CTMUCON1bits.TGEN = (config >> 7) & 0x1;
203+
// Current source output
204+
CTMUICONbits.IRNG = (config >> 5) & 0x3;
205+
206+
// Internal temperature
207+
if ((config & 0x1F) == 30) CTMU_EnableEdge2();
208+
209+
CTMU_Enable();
210+
DELAY_us(1000);
211+
CTMU_DrainOutput();
212+
DELAY_us(1500);
213+
CTMU_FloatOutput();
214+
CTMU_EnableEdge1();
215+
216+
uint16_t result = GetVoltage_Summed(config & 0x1F);
217+
218+
CTMU_DisableModule();
219+
220+
UART1_WriteInt(result);
221+
222+
return SUCCESS;
223+
}

pslab-core.X/instruments/multimeter.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,30 @@ extern "C" {
2828
* @return SUCCESS
2929
*/
3030
response_t MULTIMETER_GetCapacitance(void);
31+
32+
/**
33+
* @brief Measurements using Charge Time Measurement Unit
34+
*
35+
* @description
36+
* This command function takes one argument over serial:
37+
* 1. (uint8) Configuration byte:
38+
* | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
39+
* | DG | CSO | CHANNEL |
40+
* DG: Delay generator enable/disable (1/0)
41+
* CSO: Current source output:
42+
* 00: Base * 1000
43+
* 01: Base = 0.53 uA
44+
* 10: Base * 10
45+
* 11: Base * 100
46+
* CHANNEL: CTMU channel
47+
*
48+
* It returns nothing over serial.
49+
* It sends an acknowledge byte (SUCCESS). This will be available only after
50+
* no less than 2.5 milliseconds.
51+
*
52+
* @return SUCCESS
53+
*/
54+
response_t MULTIMETER_GetCTMUVolts(void);
3155

3256
#ifdef __cplusplus
3357
}

pslab-core.X/registers/converters/ctmu.c

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#include "../../bus/uart/uart1.h"
2+
#include "../../commands.h"
3+
#include "../../helpers/delay.h"
14
#include "ctmu.h"
25

36
void CTMU_Initialize(void) {
@@ -34,14 +37,35 @@ void CTMU_InitializeCON2(void) {
3437
CTMUCON2bits.EDG1POL = 0;
3538
// Edge 1 source is Timer 1 module
3639
CTMUCON2bits.EDG1SEL = 0b0000;
37-
// Edge 1 has not occured
40+
// Edge 1 has not occurred
3841
CTMUCON2bits.EDG1STAT = 0;
3942
// Edge 2 is level sensitive
4043
CTMUCON2bits.EDG2MOD = 0;
4144
// Edge 2 is programmed for a negative edge response
4245
CTMUCON2bits.EDG2POL = 0;
4346
// Edge 2 source is IC1 module
4447
CTMUCON2bits.EDG2SEL = 0b0000;
45-
// Edge 2 has not occured
48+
// Edge 2 has not occurred
4649
CTMUCON2bits.EDG2STAT = 0;
4750
}
51+
52+
response_t CTMU_Start(void) {
53+
54+
uint8_t config = UART1_Read();
55+
uint8_t current_trim = UART1_Read();
56+
57+
CTMU_Initialize();
58+
CTMUCON1bits.TGEN = (config >> 7) & 0x1;
59+
CTMUICONbits.ITRIM = current_trim;
60+
CTMUICONbits.IRNG = config & 0x7F;
61+
CTMU_Enable();
62+
DELAY_us(1000);
63+
CTMU_EnableEdge1();
64+
65+
return SUCCESS;
66+
}
67+
68+
response_t CTMU_Stop(void) {
69+
CTMU_DisableModule();
70+
return SUCCESS;
71+
}

pslab-core.X/registers/converters/ctmu.h

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,83 @@ extern "C" {
2020
CTMUCON1bits.CTMUEN = 0;
2121
}
2222

23+
inline static void CTMU_DrainOutput(void) {
24+
CTMUCON1bits.IDISSEN = 1;
25+
}
26+
27+
inline static void CTMU_FloatOutput(void) {
28+
CTMUCON1bits.IDISSEN = 0;
29+
}
30+
31+
inline static void CTMU_EnableEdge1(void) {
32+
CTMUCON2bits.EDG1STAT = 1;
33+
}
34+
35+
inline static void CTMU_DisableEdge1(void) {
36+
CTMUCON2bits.EDG1STAT = 0;
37+
}
38+
39+
inline static void CTMU_EnableEdge2(void) {
40+
CTMUCON2bits.EDG2STAT = 1;
41+
}
42+
43+
inline static void CTMU_DisableEdge2(void) {
44+
CTMUCON2bits.EDG2STAT = 0;
45+
}
46+
47+
inline static void CTMU_EnableEdgeDelayGeneration(void) {
48+
CTMUCON1bits.TGEN = 1;
49+
}
50+
51+
inline static void CTMU_DisableEdgeDelayGeneration(void) {
52+
CTMUCON1bits.TGEN = 0;
53+
}
54+
55+
inline static void CTMU_DisableModule(void) {
56+
CTMU_Disable();
57+
CTMU_DisableEdge1();
58+
CTMU_DisableEdge2();
59+
CTMU_DisableEdgeDelayGeneration();
60+
}
61+
62+
/**
63+
* @brief Start Charge Time Measurement Unit
64+
*
65+
* @description
66+
* This command function takes two arguments over serial:
67+
* 1. (uint8) Configuration byte:
68+
* | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
69+
* | DG | CSO |
70+
* DG: Delay generator enable/disable (1/0)
71+
* CSO: Current source output:
72+
* 00: Base * 1000
73+
* 01: Base = 0.53 uA
74+
* 10: Base * 10
75+
* 11: Base * 100
76+
* 2. (uint8) Current trim
77+
*
78+
* It returns nothing over serial.
79+
* It sends an acknowledge byte (SUCCESS).
80+
*
81+
* @return SUCCESS
82+
*/
83+
response_t CTMU_Start(void);
84+
85+
/**
86+
* @brief Stop Charge Time Measurement Unit
87+
*
88+
* @description
89+
* This command function does not take any arguments over serial. It stops
90+
* and disables CTMU module.
91+
*
92+
* It returns nothing over serial.
93+
* It sends an acknowledge byte (SUCCESS).
94+
*
95+
* @return SUCCESS
96+
*/
97+
response_t CTMU_Stop(void);
98+
99+
23100
#ifdef __cplusplus
24101
}
25102
#endif /* __cplusplus */

0 commit comments

Comments
 (0)