UNI/O med STM32F7 og FreeRtos
For at kunne kommunikere med en UNI/O eeprom med en STM32F7, som kører FreeRtos, har jeg lavet et lille test program. Jeg har taget udgangspunkt i et eksempel skrevet af Stephen Early (Thanks Steve 🙂 – https://github.com/martmaiste/UNIO/blob/master/UNIO.h )
/* Copyright (C) 2011 by Stephen Early <steve@greenend.org.uk> Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. Modified to fit to STM32F7 + FreeRtos by Flemming Jahn (flemming@familien-jahn.dk) */ #include "stm32f7xx_hal.h" #include "FreeRTOS.h" #include "task.h" #include "../../AppBootShared/Delay.h" #include "test.h" #include "Utility/TraceDebug/trace.h" #define UNIO_STARTHEADER 0x55 #define UNIO_READ 0x03 #define UNIO_CRRD 0x06 #define UNIO_WRITE 0x6c #define UNIO_WREN 0x96 #define UNIO_WRDI 0x91 #define UNIO_RDSR 0x05 #define UNIO_WRSR 0x6e #define UNIO_ERAL 0x6d #define UNIO_SETAL 0x67 // The following are defined in the datasheet as _minimum_ times, in // microseconds. There is no maximum. #define UNIO_TSTBY 600 #define UNIO_TSS 10 #define UNIO_THDR 5 #define UNIO_QUARTER_BIT 10 // Add this to all the times defined above, to be on the safe side! #define UNIO_FUDGE_FACTOR 0 // For testing we use GPIO PC12 #define GPIO_PIN GPIO_PIN_12 #define GPIO_PIN_NUMBER 12 #define GPIO_PORT GPIOC #define DEVICE_ADDR 0xA0 static void set_bus(bool state) { if (state) { HAL_GPIO_WritePin(GPIO_PORT, GPIO_PIN, GPIO_PIN_SET); } else { HAL_GPIO_WritePin(GPIO_PORT, GPIO_PIN, GPIO_PIN_RESET); } } // Using HAL_GPIO_Init takes too long time, so we do change of port mode directly (The code is copied from the HAL_GPIO_Iint function) void confPort(uint32_t outputMode) { const auto GPIO_OUTPUT_TYPE = ((uint32_t) 0x00000010U); const auto position = GPIO_PIN_NUMBER; auto temp = GPIO_PORT->OTYPER; temp &= ~(GPIO_OTYPER_OT_0 << position); temp |= (((outputMode & GPIO_OUTPUT_TYPE) >> 4) << position); GPIO_PORT->OTYPER = temp; } void confPortAsOutput() { confPort (GPIO_MODE_OUTPUT_PP); } void confPortAsInput() { confPort (GPIO_MODE_OUTPUT_OD); // We use Open Drain, since that still allows us to read the PIN as an input. set_bus(1); } #define UNIO_OUTPUT() do { confPortAsOutput(); } while (0) #define UNIO_INPUT() do { confPortAsInput(); } while (0) void initUniio() { GPIO_InitTypeDef GPIO_InitStruct; GPIO_InitStruct.Pin = GPIO_PIN; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM; HAL_GPIO_Init(GPIO_PORT, &GPIO_InitStruct); set_bus(1); } static bool read_bus(void) { return HAL_GPIO_ReadPin(GPIO_PORT, GPIO_PIN); } /* If multiple commands are to be issued to a device without a standby pulse in between, the bus must be held high for at least UNIO_TSS between the end of one command and the start of the next. */ static void unio_inter_command_gap(void) { set_bus(1); delayMicroseconds(UNIO_TSS + UNIO_FUDGE_FACTOR); } /* Send a standby pulse on the bus. After power-on or brown-out reset, the device requires a low-to-high transition on the bus at the start of the standby pulse. To be conservative, we take the bus low for UNIO_TSS, then high for UNIO_TSTBY. */ static void unio_standby_pulse(void) { UNIO_OUTPUT() ; set_bus(0); delayMicroseconds(UNIO_TSS + UNIO_FUDGE_FACTOR); set_bus(1); delayMicroseconds(UNIO_TSTBY + UNIO_FUDGE_FACTOR); } /* While bit-banging, all delays are expressed in terms of quarter bits. We use the same code path for reading and writing. During a write, we perform dummy reads at 1/4 and 3/4 of the way through each bit time. During a read we perform a dummy write at the start and 1/2 way through each bit time. */ static volatile bool rwbit(bool w) { bool a, b; set_bus(!w); delayMicroseconds(UNIO_QUARTER_BIT); a = read_bus(); delayMicroseconds(UNIO_QUARTER_BIT); set_bus(w); delayMicroseconds(UNIO_QUARTER_BIT); b = read_bus(); delayMicroseconds(UNIO_QUARTER_BIT); // TRACE("test", T_I, "a:%d,b:%d, T:%d", a,b,b&&!a); return b && !a; } static bool read_bit(void) { bool b; UNIO_INPUT() ; b = rwbit(1); UNIO_OUTPUT() ; return b; } static bool send_byte(uint8_t b, bool mak) { for (int i = 0; i < 8; i++) { rwbit(b & 0x80); b <<= 1; } rwbit(mak); return read_bit(); } static bool read_byte(uint8_t*b, bool mak) { uint8_t data = 0; UNIO_INPUT() ; for (int i = 0; i < 8; i++) { data = (data << 1) | rwbit(1); } UNIO_OUTPUT() ; *b = data; rwbit(mak); return read_bit(); } /* Send data on the bus. If end is true, send NoMAK after the last byte; otherwise send MAK. */ static bool unio_send(const uint8_t *data, uint32_t length, bool end) { for (uint32_t i = 0; i < length; i++) { /* Rules for sending MAK: if it's the last byte and end is true, send NoMAK. Otherwise send MAK. */ if (!send_byte(data[i], !(((i + 1) == length) && end))) return false; } return true; } /* Read data from the bus. After reading 'length' bytes, send NoMAK to terminate the command. */ static bool unio_read(uint8_t *data, uint32_t length) { for (uint32_t i = 0; i < length; i++) { if (!read_byte(data + i, !((i + 1) == length))) return false; } return true; } /* Send a start header on the bus. Hold the bus low for UNIO_THDR, then transmit UNIO_STARTHEADER. There is a time slot for SAK after this transmission, but we must ignore the value because no slave devices will drive the bus; if there is more than one device connected then that could cause bus contention. */ static void unio_start_header(void) { set_bus(0); delayMicroseconds(UNIO_THDR + UNIO_FUDGE_FACTOR); send_byte(UNIO_STARTHEADER, true); } nanodeUNIO::nanodeUNIO(uint8_t address) { addr = address; initUniio(); } #define fail() do { sei(); return false; } while (0) void sei() { taskEXIT_CRITICAL(); } void cli() { taskENTER_CRITICAL(); } bool nanodeUNIO::read(uint8_t *buffer, uint32_t address, uint32_t length) { uint8_t cmd[4]; cmd[0] = addr; cmd[1] = UNIO_READ; cmd[2] = (uint8_t)(address >> 8); cmd[3] = (uint8_t)(address & 0xff); unio_standby_pulse(); cli(); unio_start_header(); if (!unio_send(cmd, 4, false)) fail() ; if (!unio_read(buffer, length)) fail() ; sei(); return true; } bool nanodeUNIO::start_write(const uint8_t *buffer, uint32_t address, uint32_t length) { uint8_t cmd[4]; if (((address & 0x0f) + length) > 16) return false; // would cross page boundary cmd[0] = addr; cmd[1] = UNIO_WRITE; cmd[2] = (uint8_t)(address >> 8); cmd[3] = (uint8_t)(address & 0xff); unio_standby_pulse(); cli(); unio_start_header(); if (!unio_send(cmd, 4, false)) fail() ; if (!unio_send(buffer, length, true)) fail() ; sei(); return true; } bool nanodeUNIO::enable_write(void) { uint8_t cmd[2]; cmd[0] = addr; cmd[1] = UNIO_WREN; unio_standby_pulse(); cli(); unio_start_header(); if (!unio_send(cmd, 2, true)) fail() ; sei(); return true; } bool nanodeUNIO::disable_write(void) { uint8_t cmd[2]; cmd[0] = addr; cmd[1] = UNIO_WRDI; unio_standby_pulse(); cli(); unio_start_header(); if (!unio_send(cmd, 2, true)) fail() ; sei(); return true; } bool nanodeUNIO::read_status(uint8_t *status) { uint8_t cmd[2]; cmd[0] = addr; cmd[1] = UNIO_RDSR; unio_standby_pulse(); cli(); unio_start_header(); if (!unio_send(cmd, 2, false)) fail() ; if (!unio_read(status, 1)) fail() ; sei(); return true; } bool nanodeUNIO::write_status(uint8_t status) { uint8_t cmd[3]; cmd[0] = addr; cmd[1] = UNIO_WRSR; cmd[2] = status; unio_standby_pulse(); cli(); unio_start_header(); if (!unio_send(cmd, 3, true)) fail() ; sei(); return true; } bool nanodeUNIO::await_write_complete(void) { uint8_t cmd[2]; uint8_t status; cmd[0] = addr; cmd[1] = UNIO_RDSR; unio_standby_pulse(); /* Here we issue RDSR commands back-to-back until the WIP bit in the status register is cleared. Note that this isn't absolutely the most efficient way to monitor this bit; after sending the command we could read as many uint8_ts as we like as long as we send MAK after each uint8_t. The unio_read() function isn't set up to do this, though, and it's not really performance-critical compared to the eeprom write time! We re-enable interrupts briefly between each command so that any background tasks like updating millis() continue to happen.*/ do { unio_inter_command_gap(); cli(); unio_start_header(); if (!unio_send(cmd, 2, false)) fail() ; if (!unio_read(&status, 1)) fail() ; sei(); } while (status & 0x01); return true; } bool nanodeUNIO::simple_write(const uint8_t *buffer, uint32_t address, uint32_t length) { uint32_t wlen; while (length > 0) { wlen = length; if (((address & 0x0f) + wlen) > 16) { /* Write would cross a page boundary. Truncate the write to the page boundary. */ wlen = 16 - (address & 0x0f); } if (!enable_write()) return false; if (!start_write(buffer, address, wlen)) return false; if (!await_write_complete()) return false; buffer += wlen; address += wlen; length -= wlen; } return true; } #include <stdio.h> #include <string.h> static const int BUF_SIZE = 32; char *array_to_str(char * str, uint8_t *array, unsigned int n) { int r; if (n == 0) return 0; if (n == 1) r = sprintf(str, "0x%X", array[0]); else r = sprintf(str, "0x%X ", array[0]); array_to_str(str + r, array + 1, n - 1); return str; } void unioTask(void *argument) { uint8_t readBuffer[BUF_SIZE]; nanodeUNIO unio(DEVICE_ADDR); unio.read(readBuffer, 0, BUF_SIZE); // Dummy read - Not quite sure why the first read fails uint32_t addr = 0; auto badCnt = 0; // For counting the number times read didn't work auto goodCnt = 0; // For counting the number times read worked while (1) { memset(readBuffer, 0x0, BUF_SIZE); unio.read(readBuffer, addr, BUF_SIZE); // Just for testing that we can do many reads. Address 31 contains 0x52 with the CHIP I have used for this testing if (readBuffer[31] != 0x52) { char outputStr[228]; TRACE("test", T_I, "BadCnt:%d, GoodCnt:%d, Addr:%d, Data:%s", badCnt++, goodCnt, addr, array_to_str(outputStr, readBuffer, BUF_SIZE)); vTaskDelay(100); } else { goodCnt++; } vTaskDelay(4); } }