123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305 |
- #include "nrf6350.h"
- #include "nrf_delay.h"
- #include "twi_master.h"
- #define DDRAM_ADR 0x80
- #define DDRAM_WR 0x40
- #define FUNC_SET 0x00
- #define LCD_ADDR 0x3E
- #define JS_ADDR 0x3F
- #define X 0
- #define Y 1
- #define BUF_LEN 32
- static uint8_t data_buffer[BUF_LEN];
- static uint8_t empty_str[18] = {DDRAM_WR, ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '};
- static bool nrf6350_lcd_set_instruction(uint8_t instr)
- {
- nrf_delay_us(10000);
- data_buffer[0] = FUNC_SET;
- data_buffer[1] = instr;
- return twi_master_transfer(LCD_ADDR << 1, data_buffer, 2, TWI_ISSUE_STOP);
- }
- bool nrf6350_lcd_clear(void)
- {
- nrf_delay_us(10000);
- data_buffer[0] = FUNC_SET;
- data_buffer[1] = (uint8_t)(DDRAM_ADR + LCD_UPPER_LINE);
- if (!twi_master_transfer(LCD_ADDR << 1, data_buffer, 2, TWI_ISSUE_STOP))
- return false;
- if (!twi_master_transfer(LCD_ADDR << 1, empty_str, 18, TWI_ISSUE_STOP))
- {
- return false;
- }
- data_buffer[1] = DDRAM_ADR + LCD_LOWER_LINE;
- if (!twi_master_transfer(LCD_ADDR << 1, data_buffer, 2, TWI_ISSUE_STOP))
- return false;
- if (!twi_master_transfer(LCD_ADDR << 1, empty_str, 18, TWI_ISSUE_STOP))
- return false;
- return true;
- }
- bool nrf6350_lcd_set_contrast(uint8_t contrast)
- {
- nrf_delay_us(10000);
- data_buffer[0] = FUNC_SET;
- data_buffer[1] = 0x70 | contrast;
- return twi_master_transfer(LCD_ADDR << 1, data_buffer, 2, TWI_ISSUE_STOP);
- }
- bool nrf6350_lcd_on(void)
- {
- nrf_delay_us(10000);
- data_buffer[0] = FUNC_SET;
- data_buffer[1] = 0x0C;
- return twi_master_transfer(LCD_ADDR << 1, data_buffer, 2, TWI_ISSUE_STOP);
- }
- bool nrf6350_lcd_off(void)
- {
- nrf_delay_us(10000);
- data_buffer[0] = FUNC_SET;
- data_buffer[1] = 0x08;
- return twi_master_transfer(LCD_ADDR << 1, data_buffer, 2, TWI_ISSUE_STOP);
- }
- bool nrf6350_lcd_init(void)
- {
- if (!twi_master_init())
- {
- return false;
- }
-
-
- (void)nrf6350_lcd_wake_up();
- if (!nrf6350_lcd_set_instruction(0x38))
- return false;
- if (!nrf6350_lcd_set_instruction(0x39))
- return false;
- if (!nrf6350_lcd_set_instruction(0x14))
- return false;
- if (!nrf6350_lcd_set_contrast(LCD_CONTRAST_HIGH))
- return false;
- if (!nrf6350_lcd_set_instruction(0x5F))
- return false;
- if (!nrf6350_lcd_set_instruction(0x6A))
- return false;
- nrf_delay_us(200000);
- if (!nrf6350_lcd_on())
- return false;
- if (!nrf6350_lcd_clear())
- return false;
- return nrf6350_lcd_set_instruction(0x06);
- }
- bool nrf6350_lcd_write_string(const char *p_text, uint8_t size, uint8_t line, uint8_t pos)
- {
- uint8_t i;
- data_buffer[0] = FUNC_SET;
- data_buffer[1] = DDRAM_ADR + (pos + line);
- if (!twi_master_transfer(LCD_ADDR << 1, data_buffer, 2, TWI_ISSUE_STOP))
- return false;
- if (!twi_master_transfer(LCD_ADDR << 1, empty_str, 18 - pos, TWI_ISSUE_STOP))
- return false;
- data_buffer[0] = FUNC_SET;
- data_buffer[1] = DDRAM_ADR + (pos + line);
- if (!twi_master_transfer(LCD_ADDR << 1, data_buffer, 2, TWI_ISSUE_STOP))
- return false;
- data_buffer[0] = DDRAM_WR;
- for (i=0;i<size;i++)
- {
- if (i == LCD_LLEN)
- break;
- data_buffer[i + 1] = (uint8_t) * p_text++;
- }
- return twi_master_transfer(LCD_ADDR << 1, data_buffer, i + 1, TWI_ISSUE_STOP);
- }
- bool nrf6350_js_get_value(int8_t * val)
- {
- uint8_t js_data;
- if (!twi_master_transfer(JS_ADDR << 1 | TWI_READ_BIT, data_buffer, 1, TWI_ISSUE_STOP))
- return false;
- js_data = (~data_buffer[0] & 0x1D);
- if ((js_data & 0x01) != 0)
- {
- val[X] = -1;
- }
- else if ((js_data & 0x10) != 0)
- {
- val[X] = 1;
- }
- else
- {
- val[X] = 0;
- }
- if ((js_data & 0x04) != 0)
- {
- val[Y] = 1;
- }
- else if ((js_data & 0x08) != 0)
- {
- val[Y] = -1;
- }
- else
- {
- val[Y] = 0;
- }
- return true;
- }
- bool nrf6350_js_get_status(uint8_t * js_state)
- {
- uint8_t js_data;
- if (!twi_master_transfer(JS_ADDR << 1 | TWI_READ_BIT, &js_data, 1, TWI_ISSUE_STOP))
- {
- return false;
- }
- js_data = ~js_data;
- *js_state = js_data & 0x1F;
- return true;
- }
- static bool nrf6350_lcd_write_without_recovery(uint8_t * data,
- uint8_t data_length,
- bool issue_stop_condition)
- {
- uint32_t timeout = 20000;
- if (data_length == 0)
- {
-
- return false;
- }
- NRF_TWI1->TXD = *data++;
- NRF_TWI1->TASKS_STARTTX = 1;
-
- while (true)
- {
- while (NRF_TWI1->EVENTS_TXDSENT == 0 && (--timeout))
- {
-
- }
- if (timeout == 0)
- {
- NRF_TWI1->EVENTS_STOPPED = 0;
- NRF_TWI1->TASKS_STOP = 1;
-
- while (NRF_TWI1->EVENTS_STOPPED == 0)
- {
-
- }
-
- return false;
- }
- NRF_TWI1->EVENTS_TXDSENT = 0;
- if (--data_length == 0)
- {
- break;
- }
- NRF_TWI1->TXD = *data++;
- }
-
- if (issue_stop_condition)
- {
- NRF_TWI1->EVENTS_STOPPED = 0;
- NRF_TWI1->TASKS_STOP = 1;
-
- while (NRF_TWI1->EVENTS_STOPPED == 0)
- {
-
- }
- }
- return true;
- }
- bool nrf6350_lcd_wake_up(void)
- {
- uint8_t address = (LCD_ADDR << 1);
- uint8_t dummy_data[] = {0, 0, 0, 0};
- uint8_t dummy_data_length = 4;
- bool issue_stop_condition = 0;
- bool transfer_succeeded = false;
- NRF_TWI1->ADDRESS = (address >> 1);
- transfer_succeeded = nrf6350_lcd_write_without_recovery(dummy_data,
- dummy_data_length,
- issue_stop_condition);
- NRF_TWI1->EVENTS_ERROR = 0;
- return transfer_succeeded;
- }
|