[sw] update onewire HAL

This commit is contained in:
stnolting 2024-12-01 22:29:54 +01:00
parent 38fd3b7f9b
commit d307bf5fa1
3 changed files with 101 additions and 77 deletions

View file

@ -12,22 +12,23 @@
#define TRUE 1
static unsigned char dscrc_table[] = {
0, 94,188,226, 97, 63,221,131,194,156,126, 32,163,253, 31, 65,
157,195, 33,127,252,162, 64, 30, 95, 1,227,189, 62, 96,130,220,
35,125,159,193, 66, 28,254,160,225,191, 93, 3,128,222, 60, 98,
190,224, 2, 92,223,129, 99, 61,124, 34,192,158, 29, 67,161,255,
70, 24,250,164, 39,121,155,197,132,218, 56,102,229,187, 89, 7,
219,133,103, 57,186,228, 6, 88, 25, 71,165,251,120, 38,196,154,
101, 59,217,135, 4, 90,184,230,167,249, 27, 69,198,152,122, 36,
248,166, 68, 26,153,199, 37,123, 58,100,134,216, 91, 5,231,185,
140,210, 48,110,237,179, 81, 15, 78, 16,242,172, 47,113,147,205,
17, 79,173,243,112, 46,204,146,211,141,111, 49,178,236, 14, 80,
175,241, 19, 77,206,144,114, 44,109, 51,209,143, 12, 82,176,238,
50,108,142,208, 83, 13,239,177,240,174, 76, 18,145,207, 45,115,
202,148,118, 40,171,245, 23, 73, 8, 86,180,234,105, 55,213,139,
87, 9,235,181, 54,104,138,212,149,203, 41,119,244,170, 72, 22,
233,183, 85, 11,136,214, 52,106, 43,117,151,201, 74, 20,246,168,
116, 42,200,150, 21, 75,169,247,182,232, 10, 84,215,137,107, 53};
0, 94,188,226, 97, 63,221,131,194,156,126, 32,163,253, 31, 65,
157,195, 33,127,252,162, 64, 30, 95, 1,227,189, 62, 96,130,220,
35,125,159,193, 66, 28,254,160,225,191, 93, 3,128,222, 60, 98,
190,224, 2, 92,223,129, 99, 61,124, 34,192,158, 29, 67,161,255,
70, 24,250,164, 39,121,155,197,132,218, 56,102,229,187, 89, 7,
219,133,103, 57,186,228, 6, 88, 25, 71,165,251,120, 38,196,154,
101, 59,217,135, 4, 90,184,230,167,249, 27, 69,198,152,122, 36,
248,166, 68, 26,153,199, 37,123, 58,100,134,216, 91, 5,231,185,
140,210, 48,110,237,179, 81, 15, 78, 16,242,172, 47,113,147,205,
17, 79,173,243,112, 46,204,146,211,141,111, 49,178,236, 14, 80,
175,241, 19, 77,206,144,114, 44,109, 51,209,143, 12, 82,176,238,
50,108,142,208, 83, 13,239,177,240,174, 76, 18,145,207, 45,115,
202,148,118, 40,171,245, 23, 73, 8, 86,180,234,105, 55,213,139,
87, 9,235,181, 54,104,138,212,149,203, 41,119,244,170, 72, 22,
233,183, 85, 11,136,214, 52,106, 43,117,151,201, 74, 20,246,168,
116, 42,200,150, 21, 75,169,247,182,232, 10, 84,215,137,107, 53
};
// method declarations
int OWFirst();
@ -105,8 +106,8 @@ int OWSearch()
return FALSE;
}
// issue the search command
neorv32_onewire_write_byte_blocking(0xF0);
// issue the search command
neorv32_onewire_write_byte_blocking(0xF0);
// loop to do the search
do
@ -179,7 +180,7 @@ int OWSearch()
// check for last device
if (LastDiscrepancy == 0)
LastDeviceFlag = TRUE;
search_result = TRUE;
}
}
@ -233,7 +234,7 @@ int OWVerify()
else
rslt = FALSE;
// restore the search state
// restore the search state
for (i = 0; i < 8; i++)
ROM_NO[i] = rom_backup[i];
LastDiscrepancy = ld_backup;
@ -277,17 +278,17 @@ void OWFamilySkipSetup()
}
//--------------------------------------------------------------------------
// Calculate the CRC8 of the byte value provided with the current
// global 'crc8' value.
// Calculate the CRC8 of the byte value provided with the current
// global 'crc8' value.
// Returns current global crc8 value
//
unsigned char docrc8(unsigned char value)
{
// See Application Note 27
// TEST BUILD
crc8 = dscrc_table[crc8 ^ value];
return crc8;
}
#endif // onewire_aux_h
#endif // onewire_aux_h

View file

@ -28,7 +28,7 @@
/** ONEWIRE module prototype */
typedef volatile struct __attribute__((packed,aligned(4))) {
uint32_t CTRL; /**< offset 0: control register (#NEORV32_ONEWIRE_CTRL_enum) */
uint32_t DATA; /**< offset 4: transmission data register (#NEORV32_ONEWIRE_DATA_enum) */
uint32_t DCMD; /**< offset 4: command and data register (#NEORV32_ONEWIRE_DCMD_enum) */
} neorv32_onewire_t;
/** ONEWIRE module hardware access (#neorv32_onewire_t) */
@ -37,41 +37,53 @@ typedef volatile struct __attribute__((packed,aligned(4))) {
/** ONEWIRE control register bits */
enum NEORV32_ONEWIRE_CTRL_enum {
ONEWIRE_CTRL_EN = 0, /**< ONEWIRE control register(0) (r/w): ONEWIRE controller enable */
ONEWIRE_CTRL_PRSC0 = 1, /**< ONEWIRE control register(1) (r/w): Clock prescaler select bit 0 */
ONEWIRE_CTRL_PRSC1 = 2, /**< ONEWIRE control register(2) (r/w): Clock prescaler select bit 1 */
ONEWIRE_CTRL_CLKDIV0 = 3, /**< ONEWIRE control register(3) (r/w): Clock divider bit 0 */
ONEWIRE_CTRL_CLKDIV1 = 4, /**< ONEWIRE control register(4) (r/w): Clock divider bit 1 */
ONEWIRE_CTRL_CLKDIV2 = 5, /**< ONEWIRE control register(5) (r/w): Clock divider bit 2 */
ONEWIRE_CTRL_CLKDIV3 = 6, /**< ONEWIRE control register(6) (r/w): Clock divider bit 3 */
ONEWIRE_CTRL_CLKDIV4 = 7, /**< ONEWIRE control register(7) (r/w): Clock divider bit 4 */
ONEWIRE_CTRL_CLKDIV5 = 8, /**< ONEWIRE control register(8) (r/w): Clock divider bit 5 */
ONEWIRE_CTRL_CLKDIV6 = 9, /**< ONEWIRE control register(9) (r/w): Clock divider bit 6 */
ONEWIRE_CTRL_CLKDIV7 = 10, /**< ONEWIRE control register(10) (r/w): Clock divider bit 7 */
ONEWIRE_CTRL_TRIG_RST = 11, /**< ONEWIRE control register(11) (-/w): Trigger reset pulse, auto-clears */
ONEWIRE_CTRL_TRIG_BIT = 12, /**< ONEWIRE control register(12) (-/w): Trigger single-bit transmission, auto-clears */
ONEWIRE_CTRL_TRIG_BYTE = 13, /**< ONEWIRE control register(13) (-/w): Trigger full-byte transmission, auto-clears */
ONEWIRE_CTRL_CLEAR = 1, /**< ONEWIRE control register(1) (-/w): Clear RXT FIFO, auto-clears */
ONEWIRE_CTRL_PRSC0 = 2, /**< ONEWIRE control register(2) (r/w): Clock prescaler select bit 0 */
ONEWIRE_CTRL_PRSC1 = 3, /**< ONEWIRE control register(3) (r/w): Clock prescaler select bit 1 */
ONEWIRE_CTRL_CLKDIV0 = 4, /**< ONEWIRE control register(4) (r/w): Clock divider bit 0 */
ONEWIRE_CTRL_CLKDIV7 = 11, /**< ONEWIRE control register(11) (r/w): Clock divider bit 7 */
ONEWIRE_CTRL_SENSE = 29, /**< ONEWIRE control register(29) (r/-): Current state of the bus line */
ONEWIRE_CTRL_PRESENCE = 30, /**< ONEWIRE control register(30) (r/-): Bus presence detected */
ONEWIRE_CTRL_FIFO_LSB = 15, /**< ONEWIRE control register(15) (r/-): log2(FIFO size), LSB */
ONEWIRE_CTRL_FIFO_MSB = 18, /**< ONEWIRE control register(18) (r/-): log2(FIFO size), MSB */
ONEWIRE_CTRL_TX_FULL = 28, /**< ONEWIRE control register(28) (r/-): TX FIFO full */
ONEWIRE_CTRL_RX_AVAIL = 29, /**< ONEWIRE control register(29) (r/-): RX FIFO data available */
ONEWIRE_CTRL_SENSE = 30, /**< ONEWIRE control register(30) (r/-): Current state of the bus line */
ONEWIRE_CTRL_BUSY = 31, /**< ONEWIRE control register(31) (r/-): Operation in progress when set */
};
/** ONEWIRE receive/transmit data register bits */
enum NEORV32_ONEWIRE_DATA_enum {
ONEWIRE_DATA_LSB = 0, /**< ONEWIRE data register(0) (r/w): Receive/transmit data (8-bit) LSB */
ONEWIRE_DATA_MSB = 7 /**< ONEWIRE data register(7) (r/w): Receive/transmit data (8-bit) MSB */
/** ONEWIRE command and data register bits */
enum NEORV32_ONEWIRE_DCMD_enum {
ONEWIRE_DCMD_DATA_LSB = 0, /**< ONEWIRE data/data register(0) (r/w): Receive/transmit data (8-bit) LSB */
ONEWIRE_DCMD_DATA_MSB = 7, /**< ONEWIRE data/data register(7) (r/w): Receive/transmit data (8-bit) MSB */
ONEWIRE_DCMD_CMD_LO = 8, /**< ONEWIRE data/data register(8) (-/w): Operation command LSB */
ONEWIRE_DCMD_CMD_HI = 9, /**< ONEWIRE data/data register(9) (-/w): Operation command MSB */
ONEWIRE_DCMD_PRESENCE = 10 /**< ONEWIRE data/data register(10) (r/-): Bus presence detected */
};
/**@}*/
/**********************************************************************//**
* @name ONEWIRE DCMD commands
**************************************************************************/
/**@{*/
#define ONEWIRE_CMD_NOP (0b00) // no operation
#define ONEWIRE_CMD_BIT (0b01) // read/write single bit
#define ONEWIRE_CMD_BYTE (0b10) // read/write full byte
#define ONEWIRE_CMD_RESET (0b11) // generate reset pulse and check for presence
/**@}*/
/**********************************************************************//**
* @name Prototypes
**************************************************************************/
/**@{*/
int neorv32_onewire_available(void);
int neorv32_onewire_get_fifo_depth(void);
int neorv32_onewire_setup(uint32_t t_base);
void neorv32_onewire_enable(void);
void neorv32_onewire_disable(void);
void neorv32_onewire_flush(void);
int neorv32_onewire_sense(void);
int neorv32_onewire_busy(void);

View file

@ -34,6 +34,18 @@ int neorv32_onewire_available(void) {
}
/**********************************************************************//**
* Get ONEWIRE FIFO depth.
*
* @return FIFO depth (number of entries), zero if no FIFO implemented
**************************************************************************/
int neorv32_onewire_get_fifo_depth(void) {
uint32_t tmp = (NEORV32_ONEWIRE->CTRL >> ONEWIRE_CTRL_FIFO_LSB) & 0x0f;
return (int)(1 << tmp);
}
/**********************************************************************//**
* Reset, configure and enable ONEWIRE interface controller.
*
@ -46,7 +58,6 @@ int neorv32_onewire_setup(uint32_t t_base) {
// reset
NEORV32_ONEWIRE->CTRL = 0;
NEORV32_ONEWIRE->DATA = 0;
uint32_t t_tick;
uint32_t clkdiv;
@ -99,6 +110,15 @@ void neorv32_onewire_disable(void) {
}
/**********************************************************************//**
* Clear RTX FIFO.
**************************************************************************/
void neorv32_onewire_flush(void) {
NEORV32_ONEWIRE->CTRL &= ~(1 << ONEWIRE_CTRL_CLEAR);
}
/**********************************************************************//**
* Get current bus state.
*
@ -114,17 +134,9 @@ int neorv32_onewire_sense(void) {
}
}
// ----------------------------------------------------------------------------------------------------------------------------
// NON-BLOCKING functions
// ----------------------------------------------------------------------------------------------------------------------------
/**********************************************************************//**
* Check if ONEWIRE module is busy.
*
* @note This function is non-blocking.
*
* @return 0 if not busy, 1 if busy.
**************************************************************************/
int neorv32_onewire_busy(void) {
@ -139,6 +151,11 @@ int neorv32_onewire_busy(void) {
}
// ----------------------------------------------------------------------------------------------------------------------------
// NON-BLOCKING functions
// ----------------------------------------------------------------------------------------------------------------------------
/**********************************************************************//**
* Initiate reset pulse.
*
@ -147,7 +164,7 @@ int neorv32_onewire_busy(void) {
void neorv32_onewire_reset(void) {
// trigger reset-pulse operation
NEORV32_ONEWIRE->CTRL |= 1 << ONEWIRE_CTRL_TRIG_RST;
NEORV32_ONEWIRE->DCMD = ONEWIRE_CMD_RESET << ONEWIRE_DCMD_CMD_LO;
}
@ -161,7 +178,7 @@ void neorv32_onewire_reset(void) {
int neorv32_onewire_reset_get_presence(void) {
// check presence bit
if (NEORV32_ONEWIRE->CTRL & (1 << ONEWIRE_CTRL_PRESENCE)) {
if (NEORV32_ONEWIRE->DCMD & (1 << ONEWIRE_DCMD_PRESENCE)) {
return 0;
}
else {
@ -177,11 +194,8 @@ int neorv32_onewire_reset_get_presence(void) {
**************************************************************************/
void neorv32_onewire_read_bit(void) {
// output all-one
NEORV32_ONEWIRE->DATA = 0xff;
// trigger bit operation
NEORV32_ONEWIRE->CTRL |= (1 << ONEWIRE_CTRL_TRIG_BIT);
// trigger bit operation with data = all-one
NEORV32_ONEWIRE->DCMD = (ONEWIRE_CMD_BIT << ONEWIRE_DCMD_CMD_LO) | (0xff << ONEWIRE_DCMD_DATA_LSB);
}
@ -195,7 +209,7 @@ void neorv32_onewire_read_bit(void) {
uint8_t neorv32_onewire_read_bit_get(void) {
// return read bit
if (NEORV32_ONEWIRE->DATA & (1 << 7)) { // LSB first -> read bit is in MSB
if (NEORV32_ONEWIRE->DCMD & (1 << ONEWIRE_DCMD_DATA_MSB)) { // LSB first -> read bit is in MSB
return 1;
}
else {
@ -213,16 +227,13 @@ uint8_t neorv32_onewire_read_bit_get(void) {
**************************************************************************/
void neorv32_onewire_write_bit(uint8_t bit) {
// set replicated bit
// set replicated bit and trigger bit operation
if (bit) {
NEORV32_ONEWIRE->DATA = 0xff;
NEORV32_ONEWIRE->DCMD = (ONEWIRE_CMD_BIT << ONEWIRE_DCMD_CMD_LO) | (0xff << ONEWIRE_DCMD_DATA_LSB);
}
else {
NEORV32_ONEWIRE->DATA = 0x00;
NEORV32_ONEWIRE->DCMD = (ONEWIRE_CMD_BIT << ONEWIRE_DCMD_CMD_LO) | (0x00 << ONEWIRE_DCMD_DATA_LSB);
}
// trigger bit operation
NEORV32_ONEWIRE->CTRL |= (1 << ONEWIRE_CTRL_TRIG_BIT);
}
@ -233,11 +244,8 @@ void neorv32_onewire_write_bit(uint8_t bit) {
**************************************************************************/
void neorv32_onewire_read_byte(void) {
// output all-one
NEORV32_ONEWIRE->DATA = 0xff;
//trigger byte operation
NEORV32_ONEWIRE->CTRL |= (1 << ONEWIRE_CTRL_TRIG_BYTE);
// output all-one and trigger byte operation
NEORV32_ONEWIRE->DCMD = (ONEWIRE_CMD_BYTE << ONEWIRE_DCMD_CMD_LO) | (0xff << ONEWIRE_DCMD_DATA_LSB);
}
@ -251,7 +259,7 @@ void neorv32_onewire_read_byte(void) {
uint8_t neorv32_onewire_read_byte_get(void) {
// return read bit
return (uint8_t)(NEORV32_ONEWIRE->DATA);
return (uint8_t)(NEORV32_ONEWIRE->DCMD);
}
@ -264,11 +272,8 @@ uint8_t neorv32_onewire_read_byte_get(void) {
**************************************************************************/
void neorv32_onewire_write_byte(uint8_t byte) {
// TX data
NEORV32_ONEWIRE->DATA = (uint32_t)byte;
// and trigger byte operation
NEORV32_ONEWIRE->CTRL |= (1 << ONEWIRE_CTRL_TRIG_BYTE);
NEORV32_ONEWIRE->DCMD = (ONEWIRE_CMD_BYTE << ONEWIRE_DCMD_CMD_LO) | ((uint32_t)byte << ONEWIRE_DCMD_DATA_LSB);
}
@ -331,6 +336,9 @@ void neorv32_onewire_write_bit_blocking(uint8_t bit) {
// wait for operation to complete
while (neorv32_onewire_busy());
// discard received data
neorv32_onewire_read_byte_get();
}
@ -368,4 +376,7 @@ void neorv32_onewire_write_byte_blocking(uint8_t byte) {
// wait for operation to complete
while (neorv32_onewire_busy());
// discard received data
neorv32_onewire_read_byte_get();
}